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 (pgmdsg@ibi.com), 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/file.h>
110 #include <linux/smp_lock.h>
112 #include <asm/uaccess.h>
114 #define OFFSET_MAX ((off_t)LONG_MAX) /* FIXME: move elsewhere? */
116 static int flock_make_lock(struct file
*filp
, struct file_lock
*fl
,
118 static int posix_make_lock(struct file
*filp
, struct file_lock
*fl
,
120 static int flock_locks_conflict(struct file_lock
*caller_fl
,
121 struct file_lock
*sys_fl
);
122 static int posix_locks_conflict(struct file_lock
*caller_fl
,
123 struct file_lock
*sys_fl
);
124 static int locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
);
125 static int flock_lock_file(struct file
*filp
, struct file_lock
*caller
,
127 static int posix_locks_deadlock(struct file_lock
*caller
,
128 struct file_lock
*blocker
);
130 static struct file_lock
*locks_empty_lock(void);
131 static struct file_lock
*locks_init_lock(struct file_lock
*,
133 static void locks_insert_lock(struct file_lock
**pos
, struct file_lock
*fl
);
134 static void locks_delete_lock(struct file_lock
**thisfl_p
, unsigned int wait
);
135 static char *lock_get_status(struct file_lock
*fl
, int id
, char *pfx
);
137 static void locks_insert_block(struct file_lock
*blocker
, struct file_lock
*waiter
);
138 static void locks_delete_block(struct file_lock
*blocker
, struct file_lock
*waiter
);
139 static void locks_wake_up_blocks(struct file_lock
*blocker
, unsigned int wait
);
141 struct file_lock
*file_lock_table
= NULL
;
143 /* Allocate a new lock, and initialize its fields from fl.
144 * The lock is not inserted into any lists until locks_insert_lock() or
145 * locks_insert_block() are called.
147 static inline struct file_lock
*locks_alloc_lock(struct file_lock
*fl
)
149 return locks_init_lock(locks_empty_lock(), fl
);
152 /* Free lock not inserted in any queue.
154 static inline void locks_free_lock(struct file_lock
*fl
)
156 if (waitqueue_active(&fl
->fl_wait
))
157 panic("Attempting to free lock with active wait queue");
159 if (fl
->fl_nextblock
!= NULL
|| fl
->fl_prevblock
!= NULL
)
160 panic("Attempting to free lock with active block list");
166 /* Check if two locks overlap each other.
168 static inline int locks_overlap(struct file_lock
*fl1
, struct file_lock
*fl2
)
170 return ((fl1
->fl_end
>= fl2
->fl_start
) &&
171 (fl2
->fl_end
>= fl1
->fl_start
));
175 * Check whether two locks have the same owner
176 * N.B. Do we need the test on PID as well as owner?
177 * (Clone tasks should be considered as one "owner".)
180 locks_same_owner(struct file_lock
*fl1
, struct file_lock
*fl2
)
182 return (fl1
->fl_owner
== fl2
->fl_owner
) &&
183 (fl1
->fl_pid
== fl2
->fl_pid
);
186 /* Insert waiter into blocker's block list.
187 * We use a circular list so that processes can be easily woken up in
188 * the order they blocked. The documentation doesn't require this but
189 * it seems like the reasonable thing to do.
191 static void locks_insert_block(struct file_lock
*blocker
,
192 struct file_lock
*waiter
)
194 struct file_lock
*prevblock
;
196 if (waiter
->fl_prevblock
) {
197 printk(KERN_ERR
"locks_insert_block: remove duplicated lock "
198 "(pid=%d %ld-%ld type=%d)\n",
199 waiter
->fl_pid
, waiter
->fl_start
,
200 waiter
->fl_end
, waiter
->fl_type
);
201 locks_delete_block(waiter
->fl_prevblock
, waiter
);
204 if (blocker
->fl_prevblock
== NULL
)
205 /* No previous waiters - list is empty */
208 /* Previous waiters exist - add to end of list */
209 prevblock
= blocker
->fl_prevblock
;
211 prevblock
->fl_nextblock
= waiter
;
212 blocker
->fl_prevblock
= waiter
;
213 waiter
->fl_nextblock
= blocker
;
214 waiter
->fl_prevblock
= prevblock
;
219 /* Remove waiter from blocker's block list.
220 * When blocker ends up pointing to itself then the list is empty.
222 static void locks_delete_block(struct file_lock
*blocker
,
223 struct file_lock
*waiter
)
225 struct file_lock
*nextblock
;
226 struct file_lock
*prevblock
;
228 nextblock
= waiter
->fl_nextblock
;
229 prevblock
= waiter
->fl_prevblock
;
231 if (nextblock
== NULL
)
234 nextblock
->fl_prevblock
= prevblock
;
235 prevblock
->fl_nextblock
= nextblock
;
237 waiter
->fl_prevblock
= waiter
->fl_nextblock
= NULL
;
238 if (blocker
->fl_nextblock
== blocker
)
239 /* No more locks on blocker's blocked list */
240 blocker
->fl_prevblock
= blocker
->fl_nextblock
= NULL
;
244 /* The following two are for the benefit of lockd.
247 posix_block_lock(struct file_lock
*blocker
, struct file_lock
*waiter
)
249 locks_insert_block(blocker
, waiter
);
254 posix_unblock_lock(struct file_lock
*waiter
)
256 if (waiter
->fl_prevblock
)
257 locks_delete_block(waiter
->fl_prevblock
, waiter
);
261 /* Wake up processes blocked waiting for blocker.
262 * If told to wait then schedule the processes until the block list
263 * is empty, otherwise empty the block list ourselves.
265 static void locks_wake_up_blocks(struct file_lock
*blocker
, unsigned int wait
)
267 struct file_lock
*waiter
;
269 while ((waiter
= blocker
->fl_nextblock
) != NULL
) {
270 /* N.B. Is it possible for the notify function to block?? */
271 if (waiter
->fl_notify
)
272 waiter
->fl_notify(waiter
);
273 wake_up(&waiter
->fl_wait
);
275 /* Let the blocked process remove waiter from the
276 * block list when it gets scheduled.
278 current
->policy
|= SCHED_YIELD
;
281 /* Remove waiter from the block list, because by the
282 * time it wakes up blocker won't exist any more.
284 locks_delete_block(blocker
, waiter
);
290 /* flock() system call entry point. Apply a FL_FLOCK style lock to
291 * an open file descriptor.
293 asmlinkage
long sys_flock(unsigned int fd
, unsigned int cmd
)
295 struct file_lock file_lock
;
305 if (!flock_make_lock(filp
, &file_lock
, cmd
))
308 if ((file_lock
.fl_type
!= F_UNLCK
) && !(filp
->f_mode
& 3))
310 error
= flock_lock_file(filp
, &file_lock
,
311 (cmd
& (LOCK_UN
| LOCK_NB
)) ? 0 : 1);
319 /* Report the first existing lock that would conflict with l.
320 * This implements the F_GETLK command of fcntl().
322 int fcntl_getlk(unsigned int fd
, struct flock
*l
)
325 struct file_lock
*fl
,file_lock
;
330 if (copy_from_user(&flock
, l
, sizeof(flock
)))
333 if ((flock
.l_type
!= F_RDLCK
) && (flock
.l_type
!= F_WRLCK
))
342 if (!filp
->f_dentry
|| !filp
->f_dentry
->d_inode
)
345 if (!posix_make_lock(filp
, &file_lock
, &flock
))
348 if (filp
->f_op
->lock
) {
349 error
= filp
->f_op
->lock(filp
, F_GETLK
, &file_lock
);
352 else if (error
== LOCK_USE_CLNT
)
353 /* Bypass for NFS with no locking - 2.0.36 compat */
354 fl
= posix_test_lock(filp
, &file_lock
);
356 fl
= (file_lock
.fl_type
== F_UNLCK
? NULL
: &file_lock
);
358 fl
= posix_test_lock(filp
, &file_lock
);
361 flock
.l_type
= F_UNLCK
;
363 flock
.l_pid
= fl
->fl_pid
;
364 flock
.l_start
= fl
->fl_start
;
365 flock
.l_len
= fl
->fl_end
== OFFSET_MAX
? 0 :
366 fl
->fl_end
- fl
->fl_start
+ 1;
368 flock
.l_type
= fl
->fl_type
;
371 if (!copy_to_user(l
, &flock
, sizeof(flock
)))
380 /* Apply the lock described by l to an open file descriptor.
381 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
383 int fcntl_setlk(unsigned int fd
, unsigned int cmd
, struct flock
*l
)
386 struct file_lock file_lock
;
388 struct dentry
* dentry
;
393 * This might block, so we do it before checking the inode.
396 if (copy_from_user(&flock
, l
, sizeof(flock
)))
399 /* Get arguments and validate them ...
408 if (!(dentry
= filp
->f_dentry
))
410 if (!(inode
= dentry
->d_inode
))
413 /* Don't allow mandatory locks on files that may be memory mapped
416 if (IS_MANDLOCK(inode
) &&
417 (inode
->i_mode
& (S_ISGID
| S_IXGRP
)) == S_ISGID
) {
418 struct vm_area_struct
*vma
;
419 spin_lock(&inode
->i_shared_lock
);
420 for(vma
= inode
->i_mmap
;vma
;vma
= vma
->vm_next_share
) {
421 if (!(vma
->vm_flags
& VM_MAYSHARE
))
423 spin_unlock(&inode
->i_shared_lock
);
427 spin_unlock(&inode
->i_shared_lock
);
431 if (!posix_make_lock(filp
, &file_lock
, &flock
))
435 switch (flock
.l_type
) {
437 if (!(filp
->f_mode
& FMODE_READ
))
441 if (!(filp
->f_mode
& FMODE_WRITE
))
449 /* warn a bit for now, but don't overdo it */
451 static int count
= 0;
455 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
456 current
->pid
, current
->comm
);
459 if (!(filp
->f_mode
& 3))
468 if (filp
->f_op
->lock
!= NULL
) {
469 error
= filp
->f_op
->lock(filp
, cmd
, &file_lock
);
473 error
= posix_lock_file(filp
, &file_lock
, cmd
== F_SETLKW
);
482 * This function is called when the file is being removed
483 * from the task's fd array.
485 void locks_remove_posix(struct file
*filp
, fl_owner_t owner
)
487 struct inode
* inode
= filp
->f_dentry
->d_inode
;
488 struct file_lock file_lock
, *fl
;
489 struct file_lock
**before
;
492 * For POSIX locks we free all locks on this file for the given task.
495 before
= &inode
->i_flock
;
496 while ((fl
= *before
) != NULL
) {
497 if ((fl
->fl_flags
& FL_POSIX
) && fl
->fl_owner
== owner
) {
498 int (*lock
)(struct file
*, int, struct file_lock
*);
499 lock
= filp
->f_op
->lock
;
502 file_lock
.fl_type
= F_UNLCK
;
504 locks_delete_lock(before
, 0);
506 lock(filp
, F_SETLK
, &file_lock
);
507 /* List may have changed: */
512 before
= &fl
->fl_next
;
517 * This function is called on the last close of an open file.
519 void locks_remove_flock(struct file
*filp
)
521 struct inode
* inode
= filp
->f_dentry
->d_inode
;
522 struct file_lock file_lock
, *fl
;
523 struct file_lock
**before
;
526 before
= &inode
->i_flock
;
527 while ((fl
= *before
) != NULL
) {
528 if ((fl
->fl_flags
& FL_FLOCK
) && fl
->fl_file
== filp
) {
529 int (*lock
)(struct file
*, int, struct file_lock
*);
532 lock
= filp
->f_op
->lock
;
535 file_lock
.fl_type
= F_UNLCK
;
537 locks_delete_lock(before
, 0);
539 lock(filp
, F_SETLK
, &file_lock
);
540 /* List may have changed: */
545 before
= &fl
->fl_next
;
550 posix_test_lock(struct file
*filp
, struct file_lock
*fl
)
552 struct file_lock
*cfl
;
554 for (cfl
= filp
->f_dentry
->d_inode
->i_flock
; cfl
; cfl
= cfl
->fl_next
) {
555 if (!(cfl
->fl_flags
& FL_POSIX
))
557 if (posix_locks_conflict(cfl
, fl
))
564 int locks_mandatory_locked(struct inode
*inode
)
566 fl_owner_t owner
= current
->files
;
567 struct file_lock
*fl
;
570 * Search the lock list for this inode for any POSIX locks.
573 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
574 if (!(fl
->fl_flags
& FL_POSIX
))
576 if (fl
->fl_owner
!= owner
)
580 return fl
? -EAGAIN
: 0;
583 int locks_mandatory_area(int read_write
, struct inode
*inode
,
584 struct file
*filp
, loff_t offset
,
587 struct file_lock
*fl
;
588 struct file_lock tfl
;
591 memset(&tfl
, 0, sizeof(tfl
));
594 tfl
.fl_flags
= FL_POSIX
| FL_ACCESS
;
595 tfl
.fl_owner
= current
->files
;
596 tfl
.fl_pid
= current
->pid
;
597 init_waitqueue_head(&tfl
.fl_wait
);
598 tfl
.fl_type
= (read_write
== FLOCK_VERIFY_WRITE
) ? F_WRLCK
: F_RDLCK
;
599 tfl
.fl_start
= offset
;
600 tfl
.fl_end
= offset
+ count
- 1;
606 /* Search the lock list for this inode for locks that conflict with
607 * the proposed read/write.
609 for (fl
= inode
->i_flock
; ; fl
= fl
->fl_next
) {
613 if (!(fl
->fl_flags
& FL_POSIX
))
615 /* Block for writes against a "read" lock,
616 * and both reads and writes against a "write" lock.
618 if (posix_locks_conflict(fl
, &tfl
)) {
620 if (filp
&& (filp
->f_flags
& O_NONBLOCK
))
622 error
= -ERESTARTSYS
;
623 if (signal_pending(current
))
626 if (posix_locks_deadlock(&tfl
, fl
))
629 locks_insert_block(fl
, &tfl
);
630 interruptible_sleep_on(&tfl
.fl_wait
);
631 locks_delete_block(fl
, &tfl
);
634 * If we've been sleeping someone might have
635 * changed the permissions behind our back.
637 if ((inode
->i_mode
& (S_ISGID
| S_IXGRP
)) != S_ISGID
)
646 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
649 static int posix_make_lock(struct file
*filp
, struct file_lock
*fl
,
654 memset(fl
, 0, sizeof(*fl
));
656 init_waitqueue_head(&fl
->fl_wait
);
657 fl
->fl_flags
= FL_POSIX
;
663 fl
->fl_type
= l
->l_type
;
669 switch (l
->l_whence
) {
677 start
= filp
->f_dentry
->d_inode
->i_size
;
683 if (((start
+= l
->l_start
) < 0) || (l
->l_len
< 0))
685 fl
->fl_start
= start
; /* we record the absolute position */
686 if ((l
->l_len
== 0) || ((fl
->fl_end
= start
+ l
->l_len
- 1) < 0))
687 fl
->fl_end
= OFFSET_MAX
;
690 fl
->fl_owner
= current
->files
;
691 fl
->fl_pid
= current
->pid
;
696 /* Verify a call to flock() and fill in a file_lock structure with
697 * an appropriate FLOCK lock.
699 static int flock_make_lock(struct file
*filp
, struct file_lock
*fl
,
702 memset(fl
, 0, sizeof(*fl
));
704 init_waitqueue_head(&fl
->fl_wait
);
705 if (!filp
->f_dentry
) /* just in case */
708 switch (cmd
& ~LOCK_NB
) {
710 fl
->fl_type
= F_RDLCK
;
713 fl
->fl_type
= F_WRLCK
;
716 fl
->fl_type
= F_UNLCK
;
722 fl
->fl_flags
= FL_FLOCK
;
724 fl
->fl_end
= OFFSET_MAX
;
731 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
732 * checking before calling the locks_conflict().
734 static int posix_locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
736 /* POSIX locks owned by the same process do not conflict with
739 if (!(sys_fl
->fl_flags
& FL_POSIX
) ||
740 locks_same_owner(caller_fl
, sys_fl
))
743 return (locks_conflict(caller_fl
, sys_fl
));
746 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
747 * checking before calling the locks_conflict().
749 static int flock_locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
751 /* FLOCK locks referring to the same filp do not conflict with
754 if (!(sys_fl
->fl_flags
& FL_FLOCK
) ||
755 (caller_fl
->fl_file
== sys_fl
->fl_file
))
758 return (locks_conflict(caller_fl
, sys_fl
));
761 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
762 * checks for overlapping locks and shared/exclusive status.
764 static int locks_conflict(struct file_lock
*caller_fl
, struct file_lock
*sys_fl
)
766 if (!locks_overlap(caller_fl
, sys_fl
))
769 switch (caller_fl
->fl_type
) {
771 return (sys_fl
->fl_type
== F_WRLCK
);
777 printk("locks_conflict(): impossible lock type - %d\n",
781 return (0); /* This should never happen */
784 /* This function tests for deadlock condition before putting a process to
785 * sleep. The detection scheme is no longer recursive. Recursive was neat,
786 * but dangerous - we risked stack corruption if the lock data was bad, or
787 * if the recursion was too deep for any other reason.
789 * We rely on the fact that a task can only be on one lock's wait queue
790 * at a time. When we find blocked_task on a wait queue we can re-search
791 * with blocked_task equal to that queue's owner, until either blocked_task
792 * isn't found, or blocked_task is found on a queue owned by my_task.
794 * Note: the above assumption may not be true when handling lock requests
795 * from a broken NFS client. But broken NFS clients have a lot more to
796 * worry about than proper deadlock detection anyway... --okir
798 static int posix_locks_deadlock(struct file_lock
*caller_fl
,
799 struct file_lock
*block_fl
)
801 struct file_lock
*fl
;
802 struct file_lock
*bfl
;
803 void *caller_owner
, *blocked_owner
;
804 unsigned int caller_pid
, blocked_pid
;
806 caller_owner
= caller_fl
->fl_owner
;
807 caller_pid
= caller_fl
->fl_pid
;
808 blocked_owner
= block_fl
->fl_owner
;
809 blocked_pid
= block_fl
->fl_pid
;
812 if (caller_owner
== blocked_owner
&& caller_pid
== blocked_pid
)
814 for (fl
= file_lock_table
; fl
!= NULL
; fl
= fl
->fl_nextlink
) {
815 if (fl
->fl_owner
== NULL
|| fl
->fl_nextblock
== NULL
)
817 for (bfl
= fl
->fl_nextblock
; bfl
!= fl
; bfl
= bfl
->fl_nextblock
) {
818 if (bfl
->fl_owner
== blocked_owner
&&
819 bfl
->fl_pid
== blocked_pid
) {
820 if (fl
->fl_owner
== caller_owner
&&
821 fl
->fl_pid
== caller_pid
) {
824 blocked_owner
= fl
->fl_owner
;
825 blocked_pid
= fl
->fl_pid
;
833 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
834 * the head of the list, but that's secret knowledge known only to the next
837 static int flock_lock_file(struct file
*filp
, struct file_lock
*caller
,
840 struct file_lock
*fl
;
841 struct file_lock
*new_fl
= NULL
;
842 struct file_lock
**before
;
843 struct inode
* inode
= filp
->f_dentry
->d_inode
;
845 int unlock
= (caller
->fl_type
== F_UNLCK
);
848 * If we need a new lock, get it in advance to avoid races.
852 new_fl
= locks_alloc_lock(caller
);
860 before
= &inode
->i_flock
;
861 while (((fl
= *before
) != NULL
) && (fl
->fl_flags
& FL_FLOCK
)) {
862 if (caller
->fl_file
== fl
->fl_file
) {
863 if (caller
->fl_type
== fl
->fl_type
)
868 before
= &fl
->fl_next
;
870 /* change means that we are changing the type of an existing lock, or
871 * or else unlocking it.
874 /* N.B. What if the wait argument is false? */
875 locks_delete_lock(before
, !unlock
);
877 * If we waited, another lock may have been added ...
886 /* Check signals each time we start */
887 error
= -ERESTARTSYS
;
888 if (signal_pending(current
))
890 for (fl
= inode
->i_flock
; (fl
!= NULL
) && (fl
->fl_flags
& FL_FLOCK
);
892 if (!flock_locks_conflict(new_fl
, fl
))
897 locks_insert_block(fl
, new_fl
);
898 interruptible_sleep_on(&new_fl
->fl_wait
);
899 locks_delete_block(fl
, new_fl
);
902 locks_insert_lock(&inode
->i_flock
, new_fl
);
908 locks_free_lock(new_fl
);
912 /* Add a POSIX style lock to a file.
913 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
914 * task, then by starting address
917 * To make freeing a lock much faster, we keep a pointer to the lock before the
918 * actual one. But the real gain of the new coding was, that lock_it() and
919 * unlock_it() became one function.
921 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
924 int posix_lock_file(struct file
*filp
, struct file_lock
*caller
,
927 struct file_lock
*fl
;
928 struct file_lock
*new_fl
, *new_fl2
;
929 struct file_lock
*left
= NULL
;
930 struct file_lock
*right
= NULL
;
931 struct file_lock
**before
;
932 struct inode
* inode
= filp
->f_dentry
->d_inode
;
933 int error
, added
= 0;
936 * We may need two file_lock structures for this operation,
937 * so we get them in advance to avoid races.
939 new_fl
= locks_empty_lock();
940 new_fl2
= locks_empty_lock();
941 error
= -ENOLCK
; /* "no luck" */
942 if (!(new_fl
&& new_fl2
))
945 if (caller
->fl_type
!= F_UNLCK
) {
947 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
948 if (!(fl
->fl_flags
& FL_POSIX
))
950 if (!posix_locks_conflict(caller
, fl
))
956 if (posix_locks_deadlock(caller
, fl
))
958 error
= -ERESTARTSYS
;
959 if (signal_pending(current
))
961 locks_insert_block(fl
, caller
);
962 interruptible_sleep_on(&caller
->fl_wait
);
963 locks_delete_block(fl
, caller
);
969 * We've allocated the new locks in advance, so there are no
970 * errors possible (and no blocking operations) from here on.
972 * Find the first old lock with the same owner as the new lock.
975 before
= &inode
->i_flock
;
977 /* First skip locks owned by other processes.
979 while ((fl
= *before
) && (!(fl
->fl_flags
& FL_POSIX
) ||
980 !locks_same_owner(caller
, fl
))) {
981 before
= &fl
->fl_next
;
984 /* Process locks with this owner.
986 while ((fl
= *before
) && locks_same_owner(caller
, fl
)) {
987 /* Detect adjacent or overlapping regions (if same lock type)
989 if (caller
->fl_type
== fl
->fl_type
) {
990 if (fl
->fl_end
< caller
->fl_start
- 1)
992 /* If the next lock in the list has entirely bigger
993 * addresses than the new one, insert the lock here.
995 if (fl
->fl_start
> caller
->fl_end
+ 1)
998 /* If we come here, the new and old lock are of the
999 * same type and adjacent or overlapping. Make one
1000 * lock yielding from the lower start address of both
1001 * locks to the higher end address.
1003 if (fl
->fl_start
> caller
->fl_start
)
1004 fl
->fl_start
= caller
->fl_start
;
1006 caller
->fl_start
= fl
->fl_start
;
1007 if (fl
->fl_end
< caller
->fl_end
)
1008 fl
->fl_end
= caller
->fl_end
;
1010 caller
->fl_end
= fl
->fl_end
;
1012 locks_delete_lock(before
, 0);
1019 /* Processing for different lock types is a bit
1022 if (fl
->fl_end
< caller
->fl_start
)
1024 if (fl
->fl_start
> caller
->fl_end
)
1026 if (caller
->fl_type
== F_UNLCK
)
1028 if (fl
->fl_start
< caller
->fl_start
)
1030 /* If the next lock in the list has a higher end
1031 * address than the new one, insert the new one here.
1033 if (fl
->fl_end
> caller
->fl_end
) {
1037 if (fl
->fl_start
>= caller
->fl_start
) {
1038 /* The new lock completely replaces an old
1039 * one (This may happen several times).
1042 locks_delete_lock(before
, 0);
1045 /* Replace the old lock with the new one.
1046 * Wake up anybody waiting for the old one,
1047 * as the change in lock type might satisfy
1050 locks_wake_up_blocks(fl
, 0);
1051 fl
->fl_start
= caller
->fl_start
;
1052 fl
->fl_end
= caller
->fl_end
;
1053 fl
->fl_type
= caller
->fl_type
;
1054 fl
->fl_u
= caller
->fl_u
;
1059 /* Go on to next lock.
1062 before
= &fl
->fl_next
;
1067 if (caller
->fl_type
== F_UNLCK
)
1069 locks_init_lock(new_fl
, caller
);
1070 locks_insert_lock(before
, new_fl
);
1074 if (left
== right
) {
1075 /* The new lock breaks the old one in two pieces,
1076 * so we have to use the second new lock (in this
1077 * case, even F_UNLCK may fail!).
1079 left
= locks_init_lock(new_fl2
, right
);
1080 locks_insert_lock(before
, left
);
1083 right
->fl_start
= caller
->fl_end
+ 1;
1084 locks_wake_up_blocks(right
, 0);
1087 left
->fl_end
= caller
->fl_start
- 1;
1088 locks_wake_up_blocks(left
, 0);
1092 * Free any unused locks. (They haven't
1093 * ever been used, so we use kfree().)
1103 * Allocate an empty lock structure. We can use GFP_KERNEL now that
1104 * all allocations are done in advance.
1106 static struct file_lock
*locks_empty_lock(void)
1108 /* Okay, let's make a new file_lock structure... */
1109 return ((struct file_lock
*) kmalloc(sizeof(struct file_lock
),
1114 * Initialize a new lock from an existing file_lock structure.
1116 static struct file_lock
*locks_init_lock(struct file_lock
*new,
1117 struct file_lock
*fl
)
1120 memset(new, 0, sizeof(*new));
1121 new->fl_owner
= fl
->fl_owner
;
1122 new->fl_pid
= fl
->fl_pid
;
1123 init_waitqueue_head(&new->fl_wait
);
1124 new->fl_file
= fl
->fl_file
;
1125 new->fl_flags
= fl
->fl_flags
;
1126 new->fl_type
= fl
->fl_type
;
1127 new->fl_start
= fl
->fl_start
;
1128 new->fl_end
= fl
->fl_end
;
1129 new->fl_notify
= fl
->fl_notify
;
1130 new->fl_u
= fl
->fl_u
;
1135 /* Insert file lock fl into an inode's lock list at the position indicated
1136 * by pos. At the same time add the lock to the global file lock list.
1138 static void locks_insert_lock(struct file_lock
**pos
, struct file_lock
*fl
)
1140 fl
->fl_nextlink
= file_lock_table
;
1141 fl
->fl_prevlink
= NULL
;
1142 if (file_lock_table
!= NULL
)
1143 file_lock_table
->fl_prevlink
= fl
;
1144 file_lock_table
= fl
;
1145 fl
->fl_next
= *pos
; /* insert into file's list */
1151 /* Delete a lock and free it.
1152 * First remove our lock from the active lock lists. Then call
1153 * locks_wake_up_blocks() to wake up processes that are blocked
1154 * waiting for this lock. Finally free the lock structure.
1156 static void locks_delete_lock(struct file_lock
**thisfl_p
, unsigned int wait
)
1158 struct file_lock
*thisfl
;
1159 struct file_lock
*prevfl
;
1160 struct file_lock
*nextfl
;
1163 *thisfl_p
= thisfl
->fl_next
;
1165 prevfl
= thisfl
->fl_prevlink
;
1166 nextfl
= thisfl
->fl_nextlink
;
1169 nextfl
->fl_prevlink
= prevfl
;
1172 prevfl
->fl_nextlink
= nextfl
;
1174 file_lock_table
= nextfl
;
1176 locks_wake_up_blocks(thisfl
, wait
);
1177 locks_free_lock(thisfl
);
1183 static char *lock_get_status(struct file_lock
*fl
, int id
, char *pfx
)
1185 static char temp
[155];
1187 struct inode
*inode
;
1189 inode
= fl
->fl_file
->f_dentry
->d_inode
;
1191 p
+= sprintf(p
, "%d:%s ", id
, pfx
);
1192 if (fl
->fl_flags
& FL_POSIX
) {
1193 p
+= sprintf(p
, "%6s %s ",
1194 (fl
->fl_flags
& FL_ACCESS
) ? "ACCESS" : "POSIX ",
1195 (IS_MANDLOCK(inode
) &&
1196 (inode
->i_mode
& (S_IXGRP
| S_ISGID
)) == S_ISGID
) ?
1197 "MANDATORY" : "ADVISORY ");
1200 p
+= sprintf(p
, "FLOCK ADVISORY ");
1202 p
+= sprintf(p
, "%s ", (fl
->fl_type
== F_RDLCK
) ? "READ " : "WRITE");
1203 p
+= sprintf(p
, "%d %s:%ld %ld %ld ",
1205 kdevname(inode
->i_dev
), inode
->i_ino
, fl
->fl_start
,
1207 sprintf(p
, "%08lx %08lx %08lx %08lx %08lx\n",
1208 (long)fl
, (long)fl
->fl_prevlink
, (long)fl
->fl_nextlink
,
1209 (long)fl
->fl_next
, (long)fl
->fl_nextblock
);
1213 static inline int copy_lock_status(char *p
, char **q
, off_t pos
, int len
,
1214 off_t offset
, off_t length
)
1221 i
= len
+ length
- i
;
1238 int get_locks_status(char *buffer
, char **start
, off_t offset
, off_t length
)
1240 struct file_lock
*fl
;
1241 struct file_lock
*bfl
;
1244 off_t i
, len
, pos
= 0;
1246 for (fl
= file_lock_table
, i
= 1; fl
!= NULL
; fl
= fl
->fl_nextlink
, i
++) {
1247 p
= lock_get_status(fl
, i
, "");
1250 if (!copy_lock_status(p
, &q
, pos
, len
, offset
, length
))
1252 if ((bfl
= fl
->fl_nextblock
) == NULL
)
1255 p
= lock_get_status(bfl
, i
, " ->");
1258 if (!copy_lock_status(p
, &q
, pos
, len
, offset
, length
))
1260 } while ((bfl
= bfl
->fl_nextblock
) != fl
);
1265 return (q
- buffer
);