4 * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
5 * Doug Evans (dje@spiff.uucp), August 07, 1992
7 * Deadlock detection added.
8 * FIXME: one thing isn't handled yet:
9 * - mandatory locks (requires lots of changes elsewhere)
10 * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
12 * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
13 * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
15 * Converted file_lock_table to a linked list from an array, which eliminates
16 * the limits on how many active file locks are open.
17 * Chad Page (pageone@netcom.com), November 27, 1994
19 * Removed dependency on file descriptors. dup()'ed file descriptors now
20 * get the same locks as the original file descriptors, and a close() on
21 * any file descriptor removes ALL the locks on the file for the current
22 * process. Since locks still depend on the process id, locks are inherited
23 * after an exec() but not after a fork(). This agrees with POSIX, and both
24 * BSD and SVR4 practice.
25 * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
27 * Scrapped free list which is redundant now that we allocate locks
28 * dynamically with kmalloc()/kfree().
29 * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
31 * Implemented two lock personalities - FL_FLOCK and FL_POSIX.
33 * FL_POSIX locks are created with calls to fcntl() and lockf() through the
34 * fcntl() system call. They have the semantics described above.
36 * FL_FLOCK locks are created with calls to flock(), through the flock()
37 * system call, which is new. Old C libraries implement flock() via fcntl()
38 * and will continue to use the old, broken implementation.
40 * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
41 * with a file pointer (filp). As a result they can be shared by a parent
42 * process and its children after a fork(). They are removed when the last
43 * file descriptor referring to the file pointer is closed (unless explicitly
46 * FL_FLOCK locks never deadlock, an existing lock is always removed before
47 * upgrading from shared to exclusive (or vice versa). When this happens
48 * any processes blocked by the current lock are woken up and allowed to
49 * run before the new lock is applied.
50 * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
52 * Removed some race conditions in flock_lock_file(), marked other possible
53 * races. Just grep for FIXME to see them.
54 * Dmitry Gorodchanin (begemot@bgm.rosprint.net), February 09, 1996.
56 * Addressed Dmitry's concerns. Deadlock checking no longer recursive.
57 * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
58 * once we've checked for blocking and deadlocking.
59 * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
61 * Initial implementation of mandatory locks. SunOS turned out to be
62 * a rotten model, so I implemented the "obvious" semantics.
63 * See 'linux/Documentation/mandatory.txt' for details.
64 * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
66 * Don't allow mandatory locks on mmap()'ed files. Added simple functions to
67 * check if a file has mandatory locks, used by mmap(), open() and creat() to
68 * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
70 * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
72 * Tidied up block list handling. Added '/proc/locks' interface.
73 * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
75 * Fixed deadlock condition for pathological code that mixes calls to
76 * flock() and fcntl().
77 * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
79 * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
80 * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
81 * guarantee sensible behaviour in the case where file system modules might
82 * be compiled with different options than the kernel itself.
83 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
85 * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
86 * (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
87 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
89 * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
90 * locks. Changed process synchronisation to avoid dereferencing locks that
91 * have already been freed.
92 * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
94 * Made the block list a circular list to minimise searching in the list.
95 * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
97 * Made mandatory locking a mount option. Default is not to allow mandatory
99 * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
101 * Some adaptations for NFS support.
102 * Olaf Kirch (okir@monad.swb.de), Dec 1996,
104 * Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
105 * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
108 #include <linux/malloc.h>
109 #include <linux/sched.h>
110 #include <linux/kernel.h>
111 #include <linux/errno.h>
112 #include <linux/stat.h>
113 #include <linux/fcntl.h>
114 #include <linux/smp.h>
115 #include <linux/smp_lock.h>
117 #include <asm/uaccess.h>
119 #define OFFSET_MAX ((off_t)0x7fffffff) /* FIXME: move elsewhere? */
121 static int flock_make_lock(struct file
*filp
, struct file_lock
*fl
,
123 static int posix_make_lock(struct file
*filp
, struct file_lock
*fl
,
125 static int flock_locks_conflict(struct file_lock
*caller_fl
,
126 struct file_lock
*sys_fl
);
127 static int posix_locks_conflict(struct file_lock
*caller_fl
,
128 struct file_lock
*sys_fl
);
129 static int locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
);
130 static int flock_lock_file(struct file
*filp
, struct file_lock
*caller
,
132 static int posix_locks_deadlock(struct file_lock
*caller
,
133 struct file_lock
*blocker
);
135 static struct file_lock
*locks_alloc_lock(struct file_lock
*fl
);
136 static void locks_insert_lock(struct file_lock
**pos
, struct file_lock
*fl
);
137 static void locks_delete_lock(struct file_lock
**thisfl_p
, unsigned int wait
);
138 static char *lock_get_status(struct file_lock
*fl
, int id
, char *pfx
);
140 static void locks_insert_block(struct file_lock
*blocker
, struct file_lock
*waiter
);
141 static void locks_delete_block(struct file_lock
*blocker
, struct file_lock
*waiter
);
142 static void locks_wake_up_blocks(struct file_lock
*blocker
, unsigned int wait
);
144 struct file_lock
*file_lock_table
= NULL
;
146 /* Free lock not inserted in any queue.
148 static inline void locks_free_lock(struct file_lock
*fl
)
150 if (waitqueue_active(&fl
->fl_wait
))
151 panic("Aarggh: attempting to free lock with active wait queue - shoot Andy");
153 if (fl
->fl_nextblock
!= NULL
|| fl
->fl_prevblock
!= NULL
)
154 panic("Aarggh: attempting to free lock with active block list - shoot Andy");
160 /* Check if two locks overlap each other.
162 static inline int locks_overlap(struct file_lock
*fl1
, struct file_lock
*fl2
)
164 return ((fl1
->fl_end
>= fl2
->fl_start
) &&
165 (fl2
->fl_end
>= fl1
->fl_start
));
168 /* Check whether two locks have the same owner
171 locks_same_owner(struct file_lock
*fl1
, struct file_lock
*fl2
)
173 return (fl1
->fl_owner
== fl2
->fl_owner
) &&
174 (fl1
->fl_pid
== fl2
->fl_pid
);
177 /* Insert waiter into blocker's block list.
178 * We use a circular list so that processes can be easily woken up in
179 * the order they blocked. The documentation doesn't require this but
180 * it seems seems like the reasonable thing to do.
182 static void locks_insert_block(struct file_lock
*blocker
,
183 struct file_lock
*waiter
)
185 struct file_lock
*prevblock
;
187 if (blocker
->fl_prevblock
== NULL
)
188 /* No previous waiters - list is empty */
191 /* Previous waiters exist - add to end of list */
192 prevblock
= blocker
->fl_prevblock
;
194 prevblock
->fl_nextblock
= waiter
;
195 blocker
->fl_prevblock
= waiter
;
196 waiter
->fl_nextblock
= blocker
;
197 waiter
->fl_prevblock
= prevblock
;
202 /* Remove waiter from blocker's block list.
203 * When blocker ends up pointing to itself then the list is empty.
205 static void locks_delete_block(struct file_lock
*blocker
,
206 struct file_lock
*waiter
)
208 struct file_lock
*nextblock
;
209 struct file_lock
*prevblock
;
211 nextblock
= waiter
->fl_nextblock
;
212 prevblock
= waiter
->fl_prevblock
;
214 if (nextblock
== NULL
)
217 nextblock
->fl_prevblock
= prevblock
;
218 prevblock
->fl_nextblock
= nextblock
;
220 waiter
->fl_prevblock
= waiter
->fl_nextblock
= NULL
;
221 if (blocker
->fl_nextblock
== blocker
)
222 /* No more locks on blocker's blocked list */
223 blocker
->fl_prevblock
= blocker
->fl_nextblock
= NULL
;
227 /* The following two are for the benefit of lockd.
230 posix_block_lock(struct file_lock
*blocker
, struct file_lock
*waiter
)
232 locks_insert_block(blocker
, waiter
);
237 posix_unblock_lock(struct file_lock
*waiter
)
239 if (waiter
->fl_prevblock
)
240 locks_delete_block(waiter
->fl_prevblock
, waiter
);
244 /* Wake up processes blocked waiting for blocker.
245 * If told to wait then schedule the processes until the block list
246 * is empty, otherwise empty the block list ourselves.
248 static void locks_wake_up_blocks(struct file_lock
*blocker
, unsigned int wait
)
250 struct file_lock
*waiter
;
252 while ((waiter
= blocker
->fl_nextblock
) != NULL
) {
253 if (waiter
->fl_notify
)
254 waiter
->fl_notify(waiter
);
255 wake_up(&waiter
->fl_wait
);
257 /* Let the blocked process remove waiter from the
258 * block list when it gets scheduled.
262 /* Remove waiter from the block list, because by the
263 * time it wakes up blocker won't exist any more.
265 locks_delete_block(blocker
, waiter
);
270 /* flock() system call entry point. Apply a FL_FLOCK style lock to
271 * an open file descriptor.
273 asmlinkage
int sys_flock(unsigned int fd
, unsigned int cmd
)
275 struct file_lock file_lock
;
280 if ((fd
>= NR_OPEN
) || !(filp
= current
->files
->fd
[fd
]))
282 else if (!flock_make_lock(filp
, &file_lock
, cmd
))
284 else if ((file_lock
.fl_type
!= F_UNLCK
) && !(filp
->f_mode
& 3))
287 error
= flock_lock_file(filp
, &file_lock
,
288 (cmd
& (LOCK_UN
| LOCK_NB
)) ? 0 : 1);
293 /* Report the first existing lock that would conflict with l.
294 * This implements the F_GETLK command of fcntl().
296 int fcntl_getlk(unsigned int fd
, struct flock
*l
)
300 struct dentry
*dentry
;
302 struct file_lock
*fl
,file_lock
;
305 if ((fd
>= NR_OPEN
) || !(filp
= current
->files
->fd
[fd
]))
307 if (copy_from_user(&flock
, l
, sizeof(flock
)))
310 if ((flock
.l_type
!= F_RDLCK
) && (flock
.l_type
!= F_WRLCK
))
313 dentry
= filp
->f_dentry
;
317 inode
= dentry
->d_inode
;
321 if (!posix_make_lock(filp
, &file_lock
, &flock
))
324 if (filp
->f_op
->lock
) {
325 error
= filp
->f_op
->lock(inode
, filp
, F_GETLK
, &file_lock
);
330 fl
= posix_test_lock(filp
, &file_lock
);
334 flock
.l_pid
= fl
->fl_pid
;
335 flock
.l_start
= fl
->fl_start
;
336 flock
.l_len
= fl
->fl_end
== OFFSET_MAX
? 0 :
337 fl
->fl_end
- fl
->fl_start
+ 1;
339 flock
.l_type
= fl
->fl_type
;
340 return (copy_to_user(l
, &flock
, sizeof(flock
)) ? -EFAULT
: 0);
342 flock
.l_type
= F_UNLCK
;
345 return (copy_to_user(l
, &flock
, sizeof(flock
)) ? -EFAULT
: 0);
348 /* Apply the lock described by l to an open file descriptor.
349 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
351 int fcntl_setlk(unsigned int fd
, unsigned int cmd
, struct flock
*l
)
354 struct file_lock file_lock
;
356 struct dentry
* dentry
;
360 /* Get arguments and validate them ...
363 if ((fd
>= NR_OPEN
) || !(filp
= current
->files
->fd
[fd
]))
366 if (!(dentry
= filp
->f_dentry
))
369 if (!(inode
= dentry
->d_inode
))
372 /* Don't allow mandatory locks on files that may be memory mapped
375 if (IS_MANDLOCK(inode
) &&
376 (inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
&&
378 struct vm_area_struct
*vma
= inode
->i_mmap
;
380 if (vma
->vm_flags
& VM_MAYSHARE
)
382 } while ((vma
= vma
->vm_next_share
) != NULL
);
385 if (copy_from_user(&flock
, l
, sizeof(flock
)))
387 if (!posix_make_lock(filp
, &file_lock
, &flock
))
390 switch (flock
.l_type
) {
392 if (!(filp
->f_mode
& 1))
396 if (!(filp
->f_mode
& 2))
404 /* warn a bit for now, but don't overdo it */
406 static int count
= 0;
410 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
411 current
->pid
, current
->comm
);
414 if (!(filp
->f_mode
& 3))
422 if (filp
->f_op
->lock
!= NULL
) {
423 error
= filp
->f_op
->lock(inode
, filp
, cmd
, &file_lock
);
428 return (posix_lock_file(filp
, &file_lock
, cmd
== F_SETLKW
));
431 /* This function is called when the file is closed.
433 void locks_remove_locks(struct task_struct
*task
, struct file
*filp
)
435 struct file_lock file_lock
, *fl
;
436 struct file_lock
**before
;
437 struct inode
* inode
;
439 /* For POSIX locks we free all locks on this file for the given task.
440 * For FLOCK we only free locks on this *open* file if it is the last
441 * close on that file.
443 inode
= filp
->f_dentry
->d_inode
;
444 before
= &inode
->i_flock
;
446 while ((fl
= *before
) != NULL
) {
447 if (((fl
->fl_flags
& FL_POSIX
) && (fl
->fl_owner
== task
)) ||
448 ((fl
->fl_flags
& FL_FLOCK
) && (fl
->fl_file
== filp
) &&
449 (filp
->f_count
== 1))) {
451 locks_delete_lock(before
, 0);
452 if (filp
->f_op
->lock
) {
453 file_lock
.fl_type
= F_UNLCK
;
454 filp
->f_op
->lock(inode
, filp
, F_SETLK
, &file_lock
);
455 /* List may have changed: */
456 before
= &inode
->i_flock
;
459 before
= &fl
->fl_next
;
467 posix_test_lock(struct file
*filp
, struct file_lock
*fl
)
469 struct file_lock
*cfl
;
471 for (cfl
= filp
->f_dentry
->d_inode
->i_flock
; cfl
; cfl
= cfl
->fl_next
) {
472 if (!(cfl
->fl_flags
& FL_POSIX
))
474 if (posix_locks_conflict(cfl
, fl
))
481 int locks_verify_locked(struct inode
*inode
)
483 /* Candidates for mandatory locking have the setgid bit set
484 * but no group execute bit - an otherwise meaningless combination.
486 if (IS_MANDLOCK(inode
) &&
487 (inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
488 return (locks_mandatory_locked(inode
));
492 int locks_verify_area(int read_write
, struct inode
*inode
, struct file
*filp
,
493 unsigned int offset
, unsigned int count
)
495 /* Candidates for mandatory locking have the setgid bit set
496 * but no group execute bit - an otherwise meaningless combination.
498 if (IS_MANDLOCK(inode
) &&
499 (inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
)
500 return (locks_mandatory_area(read_write
, inode
, filp
, offset
,
505 int locks_mandatory_locked(struct inode
*inode
)
507 struct file_lock
*fl
;
509 /* Search the lock list for this inode for any POSIX locks.
511 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
512 if (!(fl
->fl_flags
& FL_POSIX
))
514 if (fl
->fl_owner
!= current
)
520 int locks_mandatory_area(int read_write
, struct inode
*inode
,
521 struct file
*filp
, unsigned int offset
,
524 struct file_lock
*fl
;
525 struct file_lock tfl
;
527 memset(&tfl
, 0, sizeof(tfl
));
530 tfl
.fl_flags
= FL_POSIX
| FL_ACCESS
;
531 tfl
.fl_owner
= current
;
532 tfl
.fl_pid
= current
->pid
;
533 tfl
.fl_type
= (read_write
== FLOCK_VERIFY_WRITE
) ? F_WRLCK
: F_RDLCK
;
534 tfl
.fl_start
= offset
;
535 tfl
.fl_end
= offset
+ count
- 1;
538 /* Search the lock list for this inode for locks that conflict with
539 * the proposed read/write.
541 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
542 if (!(fl
->fl_flags
& FL_POSIX
))
544 /* Block for writes against a "read" lock,
545 * and both reads and writes against a "write" lock.
547 if (posix_locks_conflict(fl
, &tfl
)) {
548 if (filp
&& (filp
->f_flags
& O_NONBLOCK
))
550 if (current
->signal
& ~current
->blocked
)
551 return (-ERESTARTSYS
);
552 if (posix_locks_deadlock(&tfl
, fl
))
555 locks_insert_block(fl
, &tfl
);
556 interruptible_sleep_on(&tfl
.fl_wait
);
557 locks_delete_block(fl
, &tfl
);
559 if (current
->signal
& ~current
->blocked
)
560 return (-ERESTARTSYS
);
561 /* If we've been sleeping someone might have
562 * changed the permissions behind our back.
564 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) != S_ISGID
)
572 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
575 static int posix_make_lock(struct file
*filp
, struct file_lock
*fl
,
580 memset(fl
, 0, sizeof(*fl
));
582 fl
->fl_flags
= FL_POSIX
;
588 fl
->fl_type
= l
->l_type
;
594 switch (l
->l_whence
) {
602 start
= filp
->f_dentry
->d_inode
->i_size
;
608 if (((start
+= l
->l_start
) < 0) || (l
->l_len
< 0))
610 fl
->fl_start
= start
; /* we record the absolute position */
611 if ((l
->l_len
== 0) || ((fl
->fl_end
= start
+ l
->l_len
- 1) < 0))
612 fl
->fl_end
= OFFSET_MAX
;
615 fl
->fl_owner
= current
;
616 fl
->fl_pid
= current
->pid
;
621 /* Verify a call to flock() and fill in a file_lock structure with
622 * an appropriate FLOCK lock.
624 static int flock_make_lock(struct file
*filp
, struct file_lock
*fl
,
627 memset(fl
, 0, sizeof(*fl
));
629 if (!filp
->f_dentry
) /* just in case */
632 switch (cmd
& ~LOCK_NB
) {
634 fl
->fl_type
= F_RDLCK
;
637 fl
->fl_type
= F_WRLCK
;
640 fl
->fl_type
= F_UNLCK
;
646 fl
->fl_flags
= FL_FLOCK
;
648 fl
->fl_end
= OFFSET_MAX
;
655 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
656 * checking before calling the locks_conflict().
658 static int posix_locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
660 /* POSIX locks owned by the same process do not conflict with
663 if (!(sys_fl
->fl_flags
& FL_POSIX
) ||
664 locks_same_owner(caller_fl
, sys_fl
))
667 return (locks_conflict(caller_fl
, sys_fl
));
670 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
671 * checking before calling the locks_conflict().
673 static int flock_locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
675 /* FLOCK locks referring to the same filp do not conflict with
678 if (!(sys_fl
->fl_flags
& FL_FLOCK
) ||
679 (caller_fl
->fl_file
== sys_fl
->fl_file
))
682 return (locks_conflict(caller_fl
, sys_fl
));
685 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
686 * checks for overlapping locks and shared/exclusive status.
688 static int locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
690 if (!locks_overlap(caller_fl
, sys_fl
))
693 switch (caller_fl
->fl_type
) {
695 return (sys_fl
->fl_type
== F_WRLCK
);
701 printk("locks_conflict(): impossible lock type - %d\n",
705 return (0); /* This should never happen */
708 /* This function tests for deadlock condition before putting a process to
709 * sleep. The detection scheme is no longer recursive. Recursive was neat,
710 * but dangerous - we risked stack corruption if the lock data was bad, or
711 * if the recursion was too deep for any other reason.
713 * We rely on the fact that a task can only be on one lock's wait queue
714 * at a time. When we find blocked_task on a wait queue we can re-search
715 * with blocked_task equal to that queue's owner, until either blocked_task
716 * isn't found, or blocked_task is found on a queue owned by my_task.
718 * Note: the above assumption may not be true when handling lock requests
719 * from a broken NFS client. But broken NFS clients have a lot more to
720 * worry about than proper deadlock detection anyway... --okir
722 static int posix_locks_deadlock(struct file_lock
*caller_fl
,
723 struct file_lock
*block_fl
)
725 struct file_lock
*fl
;
726 struct file_lock
*bfl
;
727 void *caller_owner
, *blocked_owner
;
728 unsigned int caller_pid
, blocked_pid
;
730 caller_owner
= caller_fl
->fl_owner
;
731 caller_pid
= caller_fl
->fl_pid
;
732 blocked_owner
= block_fl
->fl_owner
;
733 blocked_pid
= block_fl
->fl_pid
;
736 if (caller_owner
== blocked_owner
&& caller_pid
== blocked_pid
)
738 for (fl
= file_lock_table
; fl
!= NULL
; fl
= fl
->fl_nextlink
) {
739 if (fl
->fl_owner
== NULL
|| fl
->fl_nextblock
== NULL
)
741 for (bfl
= fl
->fl_nextblock
; bfl
!= fl
; bfl
= bfl
->fl_nextblock
) {
742 if (bfl
->fl_owner
== blocked_owner
&&
743 bfl
->fl_pid
== blocked_pid
) {
744 if (fl
->fl_owner
== caller_owner
&&
745 fl
->fl_pid
== caller_pid
) {
748 blocked_owner
= fl
->fl_owner
;
749 blocked_pid
= fl
->fl_pid
;
757 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
758 * the head of the list, but that's secret knowledge known only to the next
761 static int flock_lock_file(struct file
*filp
, struct file_lock
*caller
,
764 struct file_lock
*fl
;
765 struct file_lock
*new_fl
;
766 struct file_lock
**before
;
767 struct inode
* inode
= filp
->f_dentry
->d_inode
;
770 before
= &inode
->i_flock
;
771 while (((fl
= *before
) != NULL
) && (fl
->fl_flags
& FL_FLOCK
)) {
772 if (caller
->fl_file
== fl
->fl_file
) {
773 if (caller
->fl_type
== fl
->fl_type
)
778 before
= &fl
->fl_next
;
780 /* change means that we are changing the type of an existing lock, or
781 * or else unlocking it.
784 locks_delete_lock(before
, caller
->fl_type
!= F_UNLCK
);
785 if (caller
->fl_type
== F_UNLCK
)
787 if ((new_fl
= locks_alloc_lock(caller
)) == NULL
)
790 for (fl
= inode
->i_flock
; (fl
!= NULL
) && (fl
->fl_flags
& FL_FLOCK
);
792 if (!flock_locks_conflict(new_fl
, fl
))
795 locks_free_lock(new_fl
);
798 if (current
->signal
& ~current
->blocked
) {
799 /* Note: new_fl is not in any queue at this
800 * point, so we must use locks_free_lock()
801 * instead of locks_delete_lock()
802 * Dmitry Gorodchanin 09/02/96.
804 locks_free_lock(new_fl
);
805 return (-ERESTARTSYS
);
807 locks_insert_block(fl
, new_fl
);
808 interruptible_sleep_on(&new_fl
->fl_wait
);
809 locks_delete_block(fl
, new_fl
);
810 if (current
->signal
& ~current
->blocked
) {
811 /* Awakened by a signal. Free the new
812 * lock and return an error.
814 locks_free_lock(new_fl
);
815 return (-ERESTARTSYS
);
819 locks_insert_lock(&inode
->i_flock
, new_fl
);
823 /* Add a POSIX style lock to a file.
824 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
825 * task, then by starting address
828 * To make freeing a lock much faster, we keep a pointer to the lock before the
829 * actual one. But the real gain of the new coding was, that lock_it() and
830 * unlock_it() became one function.
832 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
835 int posix_lock_file(struct file
*filp
, struct file_lock
*caller
,
838 struct file_lock
*fl
;
839 struct file_lock
*new_fl
;
840 struct file_lock
*left
= NULL
;
841 struct file_lock
*right
= NULL
;
842 struct file_lock
**before
;
843 struct inode
* inode
= filp
->f_dentry
->d_inode
;
846 if (caller
->fl_type
!= F_UNLCK
) {
848 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
849 if (!(fl
->fl_flags
& FL_POSIX
))
851 if (!posix_locks_conflict(caller
, fl
))
855 if (current
->signal
& ~current
->blocked
)
856 return (-ERESTARTSYS
);
857 if (posix_locks_deadlock(caller
, fl
))
859 locks_insert_block(fl
, caller
);
860 interruptible_sleep_on(&caller
->fl_wait
);
861 locks_delete_block(fl
, caller
);
862 if (current
->signal
& ~current
->blocked
)
863 return (-ERESTARTSYS
);
868 /* Find the first old lock with the same owner as the new lock.
871 before
= &inode
->i_flock
;
873 /* First skip locks owned by other processes.
875 while ((fl
= *before
) && (!(fl
->fl_flags
& FL_POSIX
) ||
876 !locks_same_owner(caller
, fl
))) {
877 before
= &fl
->fl_next
;
880 /* Process locks with this owner.
882 while ((fl
= *before
) && locks_same_owner(caller
, fl
)) {
883 /* Detect adjacent or overlapping regions (if same lock type)
885 if (caller
->fl_type
== fl
->fl_type
) {
886 if (fl
->fl_end
< caller
->fl_start
- 1)
888 /* If the next lock in the list has entirely bigger
889 * addresses than the new one, insert the lock here.
891 if (fl
->fl_start
> caller
->fl_end
+ 1)
894 /* If we come here, the new and old lock are of the
895 * same type and adjacent or overlapping. Make one
896 * lock yielding from the lower start address of both
897 * locks to the higher end address.
899 if (fl
->fl_start
> caller
->fl_start
)
900 fl
->fl_start
= caller
->fl_start
;
902 caller
->fl_start
= fl
->fl_start
;
903 if (fl
->fl_end
< caller
->fl_end
)
904 fl
->fl_end
= caller
->fl_end
;
906 caller
->fl_end
= fl
->fl_end
;
908 locks_delete_lock(before
, 0);
915 /* Processing for different lock types is a bit
918 if (fl
->fl_end
< caller
->fl_start
)
920 if (fl
->fl_start
> caller
->fl_end
)
922 if (caller
->fl_type
== F_UNLCK
)
924 if (fl
->fl_start
< caller
->fl_start
)
926 /* If the next lock in the list has a higher end
927 * address than the new one, insert the new one here.
929 if (fl
->fl_end
> caller
->fl_end
) {
933 if (fl
->fl_start
>= caller
->fl_start
) {
934 /* The new lock completely replaces an old
935 * one (This may happen several times).
938 locks_delete_lock(before
, 0);
941 /* Replace the old lock with the new one.
942 * Wake up anybody waiting for the old one,
943 * as the change in lock type might satisfy
946 locks_wake_up_blocks(fl
, 0);
947 fl
->fl_start
= caller
->fl_start
;
948 fl
->fl_end
= caller
->fl_end
;
949 fl
->fl_type
= caller
->fl_type
;
950 fl
->fl_u
= caller
->fl_u
;
955 /* Go on to next lock.
958 before
= &fl
->fl_next
;
962 if (caller
->fl_type
== F_UNLCK
)
964 if ((new_fl
= locks_alloc_lock(caller
)) == NULL
)
966 locks_insert_lock(before
, new_fl
);
970 /* The new lock breaks the old one in two pieces, so we
971 * have to allocate one more lock (in this case, even
972 * F_UNLCK may fail!).
974 if ((left
= locks_alloc_lock(right
)) == NULL
) {
976 locks_delete_lock(before
, 0);
979 locks_insert_lock(before
, left
);
981 right
->fl_start
= caller
->fl_end
+ 1;
982 locks_wake_up_blocks(right
, 0);
985 left
->fl_end
= caller
->fl_start
- 1;
986 locks_wake_up_blocks(left
, 0);
991 /* Allocate new lock.
992 * Initialize its fields from fl. The lock is not inserted into any
993 * lists until locks_insert_lock() or locks_insert_block() are called.
995 static struct file_lock
*locks_alloc_lock(struct file_lock
*fl
)
997 struct file_lock
*tmp
;
999 /* Okay, let's make a new file_lock structure... */
1000 if ((tmp
= (struct file_lock
*)kmalloc(sizeof(struct file_lock
),
1001 GFP_ATOMIC
)) == NULL
)
1004 memset(tmp
, 0, sizeof(*tmp
));
1006 tmp
->fl_flags
= fl
->fl_flags
;
1007 tmp
->fl_owner
= fl
->fl_owner
;
1008 tmp
->fl_pid
= fl
->fl_pid
;
1009 tmp
->fl_file
= fl
->fl_file
;
1010 tmp
->fl_type
= fl
->fl_type
;
1011 tmp
->fl_start
= fl
->fl_start
;
1012 tmp
->fl_end
= fl
->fl_end
;
1013 tmp
->fl_notify
= fl
->fl_notify
;
1014 tmp
->fl_u
= fl
->fl_u
;
1019 /* Insert file lock fl into an inode's lock list at the position indicated
1020 * by pos. At the same time add the lock to the global file lock list.
1022 static void locks_insert_lock(struct file_lock
**pos
, struct file_lock
*fl
)
1024 fl
->fl_nextlink
= file_lock_table
;
1025 fl
->fl_prevlink
= NULL
;
1026 if (file_lock_table
!= NULL
)
1027 file_lock_table
->fl_prevlink
= fl
;
1028 file_lock_table
= fl
;
1029 fl
->fl_next
= *pos
; /* insert into file's list */
1035 /* Delete a lock and free it.
1036 * First remove our lock from the active lock lists. Then call
1037 * locks_wake_up_blocks() to wake up processes that are blocked
1038 * waiting for this lock. Finally free the lock structure.
1040 static void locks_delete_lock(struct file_lock
**thisfl_p
, unsigned int wait
)
1042 struct file_lock
*thisfl
;
1043 struct file_lock
*prevfl
;
1044 struct file_lock
*nextfl
;
1047 *thisfl_p
= thisfl
->fl_next
;
1049 prevfl
= thisfl
->fl_prevlink
;
1050 nextfl
= thisfl
->fl_nextlink
;
1053 nextfl
->fl_prevlink
= prevfl
;
1056 prevfl
->fl_nextlink
= nextfl
;
1058 file_lock_table
= nextfl
;
1060 locks_wake_up_blocks(thisfl
, wait
);
1061 locks_free_lock(thisfl
);
1067 static char *lock_get_status(struct file_lock
*fl
, int id
, char *pfx
)
1069 static char temp
[129];
1071 struct inode
*inode
;
1073 inode
= fl
->fl_file
->f_dentry
->d_inode
;
1075 p
+= sprintf(p
, "%d:%s ", id
, pfx
);
1076 if (fl
->fl_flags
& FL_POSIX
) {
1077 p
+= sprintf(p
, "%6s %s ",
1078 (fl
->fl_flags
& FL_ACCESS
) ? "ACCESS" : "POSIX ",
1079 (IS_MANDLOCK(inode
) &&
1080 (inode
->i_mode
& (S_IXGRP
| S_ISGID
)) == S_ISGID
) ?
1081 "MANDATORY" : "ADVISORY ");
1084 p
+= sprintf(p
, "FLOCK ADVISORY ");
1086 p
+= sprintf(p
, "%s ", (fl
->fl_type
== F_RDLCK
) ? "READ " : "WRITE");
1087 p
+= sprintf(p
, "%d %s:%ld %ld %ld ",
1089 kdevname(inode
->i_dev
), inode
->i_ino
, fl
->fl_start
,
1091 sprintf(p
, "%08lx %08lx %08lx %08lx %08lx\n",
1092 (long)fl
, (long)fl
->fl_prevlink
, (long)fl
->fl_nextlink
,
1093 (long)fl
->fl_next
, (long)fl
->fl_nextblock
);
1097 static inline int copy_lock_status(char *p
, char **q
, off_t pos
, int len
,
1098 off_t offset
, int length
)
1105 i
= len
+ length
- i
;
1122 int get_locks_status(char *buffer
, char **start
, off_t offset
, int length
)
1124 struct file_lock
*fl
;
1125 struct file_lock
*bfl
;
1132 for (fl
= file_lock_table
, i
= 1; fl
!= NULL
; fl
= fl
->fl_nextlink
, i
++) {
1133 p
= lock_get_status(fl
, i
, "");
1136 if (!copy_lock_status(p
, &q
, pos
, len
, offset
, length
))
1138 if ((bfl
= fl
->fl_nextblock
) == NULL
)
1141 p
= lock_get_status(bfl
, i
, " ->");
1144 if (!copy_lock_status(p
, &q
, pos
, len
, offset
, length
))
1146 } while ((bfl
= bfl
->fl_nextblock
) != fl
);
1151 return (q
- buffer
);