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 '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.
107 * Use slab allocator instead of kmalloc/kfree.
108 * Use generic list implementation from <linux/list.h>.
109 * Sped up posix_locks_deadlock by only considering blocked locks.
110 * Matthew Wilcox <willy@debian.org>, March, 2000.
112 * Leases and LOCK_MAND
113 * Matthew Wilcox <willy@debian.org>, June, 2000.
114 * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
117 #include <linux/capability.h>
118 #include <linux/file.h>
119 #include <linux/fdtable.h>
120 #include <linux/fs.h>
121 #include <linux/init.h>
122 #include <linux/module.h>
123 #include <linux/security.h>
124 #include <linux/slab.h>
125 #include <linux/syscalls.h>
126 #include <linux/time.h>
127 #include <linux/rcupdate.h>
128 #include <linux/pid_namespace.h>
130 #include <asm/uaccess.h>
132 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
133 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
134 #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
136 static bool lease_breaking(struct file_lock *fl)
138 return fl->fl_flags & FL_INPROGRESS;
141 int leases_enable = 1;
142 int lease_break_time = 45;
144 #define for_each_lock(inode, lockp) \
145 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
147 static LIST_HEAD(file_lock_list);
148 static LIST_HEAD(blocked_list);
149 static DEFINE_SPINLOCK(file_lock_lock);
152 * Protects the two list heads above, plus the inode->i_flock list
154 void lock_flocks(void)
156 spin_lock(&file_lock_lock);
158 EXPORT_SYMBOL_GPL(lock_flocks);
160 void unlock_flocks(void)
162 spin_unlock(&file_lock_lock);
164 EXPORT_SYMBOL_GPL(unlock_flocks);
166 static struct kmem_cache *filelock_cache __read_mostly;
168 static void locks_init_lock_heads(struct file_lock *fl)
170 INIT_LIST_HEAD(&fl->fl_link);
171 INIT_LIST_HEAD(&fl->fl_block);
172 init_waitqueue_head(&fl->fl_wait);
175 /* Allocate an empty lock structure. */
176 struct file_lock *locks_alloc_lock(void)
178 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
181 locks_init_lock_heads(fl);
185 EXPORT_SYMBOL_GPL(locks_alloc_lock);
187 void locks_release_private(struct file_lock *fl)
190 if (fl->fl_ops->fl_release_private)
191 fl->fl_ops->fl_release_private(fl);
195 if (fl->fl_lmops->lm_release_private)
196 fl->fl_lmops->lm_release_private(fl);
201 EXPORT_SYMBOL_GPL(locks_release_private);
203 /* Free a lock which is not in use. */
204 void locks_free_lock(struct file_lock *fl)
206 BUG_ON(waitqueue_active(&fl->fl_wait));
207 BUG_ON(!list_empty(&fl->fl_block));
208 BUG_ON(!list_empty(&fl->fl_link));
210 locks_release_private(fl);
211 kmem_cache_free(filelock_cache, fl);
213 EXPORT_SYMBOL(locks_free_lock);
215 void locks_init_lock(struct file_lock *fl)
217 memset(fl, 0, sizeof(struct file_lock));
218 locks_init_lock_heads(fl);
221 EXPORT_SYMBOL(locks_init_lock);
223 static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
226 if (fl->fl_ops->fl_copy_lock)
227 fl->fl_ops->fl_copy_lock(new, fl);
228 new->fl_ops = fl->fl_ops;
231 new->fl_lmops = fl->fl_lmops;
235 * Initialize a new lock from an existing file_lock structure.
237 void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
239 new->fl_owner = fl->fl_owner;
240 new->fl_pid = fl->fl_pid;
242 new->fl_flags = fl->fl_flags;
243 new->fl_type = fl->fl_type;
244 new->fl_start = fl->fl_start;
245 new->fl_end = fl->fl_end;
247 new->fl_lmops = NULL;
249 EXPORT_SYMBOL(__locks_copy_lock);
251 void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
253 locks_release_private(new);
255 __locks_copy_lock(new, fl);
256 new->fl_file = fl->fl_file;
257 new->fl_ops = fl->fl_ops;
258 new->fl_lmops = fl->fl_lmops;
260 locks_copy_private(new, fl);
263 EXPORT_SYMBOL(locks_copy_lock);
265 static inline int flock_translate_cmd(int cmd) {
267 return cmd & (LOCK_MAND | LOCK_RW);
279 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
280 static int flock_make_lock(struct file *filp, struct file_lock **lock,
283 struct file_lock *fl;
284 int type = flock_translate_cmd(cmd);
288 fl = locks_alloc_lock();
293 fl->fl_pid = current->tgid;
294 fl->fl_flags = FL_FLOCK;
296 fl->fl_end = OFFSET_MAX;
302 static int assign_type(struct file_lock *fl, int type)
316 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
319 static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
324 switch (l->l_whence) {
332 start = i_size_read(filp->f_path.dentry->d_inode);
338 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
339 POSIX-2001 defines it. */
343 fl->fl_end = OFFSET_MAX;
345 end = start + l->l_len - 1;
347 } else if (l->l_len < 0) {
354 fl->fl_start = start; /* we record the absolute position */
355 if (fl->fl_end < fl->fl_start)
358 fl->fl_owner = current->files;
359 fl->fl_pid = current->tgid;
361 fl->fl_flags = FL_POSIX;
365 return assign_type(fl, l->l_type);
368 #if BITS_PER_LONG == 32
369 static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
374 switch (l->l_whence) {
382 start = i_size_read(filp->f_path.dentry->d_inode);
391 fl->fl_end = OFFSET_MAX;
393 fl->fl_end = start + l->l_len - 1;
394 } else if (l->l_len < 0) {
395 fl->fl_end = start - 1;
400 fl->fl_start = start; /* we record the absolute position */
401 if (fl->fl_end < fl->fl_start)
404 fl->fl_owner = current->files;
405 fl->fl_pid = current->tgid;
407 fl->fl_flags = FL_POSIX;
411 return assign_type(fl, l->l_type);
415 /* default lease lock manager operations */
416 static void lease_break_callback(struct file_lock *fl)
418 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
421 static void lease_release_private_callback(struct file_lock *fl)
426 f_delown(fl->fl_file);
427 fl->fl_file->f_owner.signum = 0;
430 static const struct lock_manager_operations lease_manager_ops = {
431 .lm_break = lease_break_callback,
432 .lm_release_private = lease_release_private_callback,
433 .lm_change = lease_modify,
437 * Initialize a lease, use the default lock manager operations
439 static int lease_init(struct file *filp, int type, struct file_lock *fl)
441 if (assign_type(fl, type) != 0)
444 fl->fl_owner = current->files;
445 fl->fl_pid = current->tgid;
448 fl->fl_flags = FL_LEASE;
450 fl->fl_end = OFFSET_MAX;
452 fl->fl_lmops = &lease_manager_ops;
456 /* Allocate a file_lock initialised to this type of lease */
457 static struct file_lock *lease_alloc(struct file *filp, int type)
459 struct file_lock *fl = locks_alloc_lock();
463 return ERR_PTR(error);
465 error = lease_init(filp, type, fl);
468 return ERR_PTR(error);
473 /* Check if two locks overlap each other.
475 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
477 return ((fl1->fl_end >= fl2->fl_start) &&
478 (fl2->fl_end >= fl1->fl_start));
482 * Check whether two locks have the same owner.
484 static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
486 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
487 return fl2->fl_lmops == fl1->fl_lmops &&
488 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
489 return fl1->fl_owner == fl2->fl_owner;
492 /* Remove waiter from blocker's block list.
493 * When blocker ends up pointing to itself then the list is empty.
495 static void __locks_delete_block(struct file_lock *waiter)
497 list_del_init(&waiter->fl_block);
498 list_del_init(&waiter->fl_link);
499 waiter->fl_next = NULL;
504 static void locks_delete_block(struct file_lock *waiter)
507 __locks_delete_block(waiter);
511 /* Insert waiter into blocker's block list.
512 * We use a circular list so that processes can be easily woken up in
513 * the order they blocked. The documentation doesn't require this but
514 * it seems like the reasonable thing to do.
516 static void locks_insert_block(struct file_lock *blocker,
517 struct file_lock *waiter)
519 BUG_ON(!list_empty(&waiter->fl_block));
520 list_add_tail(&waiter->fl_block, &blocker->fl_block);
521 waiter->fl_next = blocker;
522 if (IS_POSIX(blocker))
523 list_add(&waiter->fl_link, &blocked_list);
526 /* Wake up processes blocked waiting for blocker.
527 * If told to wait then schedule the processes until the block list
528 * is empty, otherwise empty the block list ourselves.
530 static void locks_wake_up_blocks(struct file_lock *blocker)
532 while (!list_empty(&blocker->fl_block)) {
533 struct file_lock *waiter;
535 waiter = list_first_entry(&blocker->fl_block,
536 struct file_lock, fl_block);
537 __locks_delete_block(waiter);
538 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
539 waiter->fl_lmops->lm_notify(waiter);
541 wake_up(&waiter->fl_wait);
545 /* Insert file lock fl into an inode's lock list at the position indicated
546 * by pos. At the same time add the lock to the global file lock list.
548 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
550 list_add(&fl->fl_link, &file_lock_list);
552 fl->fl_nspid = get_pid(task_tgid(current));
554 /* insert into file's list */
560 * Delete a lock and then free it.
561 * Wake up processes that are blocked waiting for this lock,
562 * notify the FS that the lock has been cleared and
563 * finally free the lock.
565 static void locks_delete_lock(struct file_lock **thisfl_p)
567 struct file_lock *fl = *thisfl_p;
569 *thisfl_p = fl->fl_next;
571 list_del_init(&fl->fl_link);
573 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
574 if (fl->fl_fasync != NULL) {
575 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
576 fl->fl_fasync = NULL;
580 put_pid(fl->fl_nspid);
584 locks_wake_up_blocks(fl);
588 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
589 * checks for shared/exclusive status of overlapping locks.
591 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
593 if (sys_fl->fl_type == F_WRLCK)
595 if (caller_fl->fl_type == F_WRLCK)
600 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
601 * checking before calling the locks_conflict().
603 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
605 /* POSIX locks owned by the same process do not conflict with
608 if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
611 /* Check whether they overlap */
612 if (!locks_overlap(caller_fl, sys_fl))
615 return (locks_conflict(caller_fl, sys_fl));
618 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
619 * checking before calling the locks_conflict().
621 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
623 /* FLOCK locks referring to the same filp do not conflict with
626 if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
628 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
631 return (locks_conflict(caller_fl, sys_fl));
635 posix_test_lock(struct file *filp, struct file_lock *fl)
637 struct file_lock *cfl;
640 for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
643 if (posix_locks_conflict(fl, cfl))
647 __locks_copy_lock(fl, cfl);
649 fl->fl_pid = pid_vnr(cfl->fl_nspid);
651 fl->fl_type = F_UNLCK;
655 EXPORT_SYMBOL(posix_test_lock);
658 * Deadlock detection:
660 * We attempt to detect deadlocks that are due purely to posix file
663 * We assume that a task can be waiting for at most one lock at a time.
664 * So for any acquired lock, the process holding that lock may be
665 * waiting on at most one other lock. That lock in turns may be held by
666 * someone waiting for at most one other lock. Given a requested lock
667 * caller_fl which is about to wait for a conflicting lock block_fl, we
668 * follow this chain of waiters to ensure we are not about to create a
671 * Since we do this before we ever put a process to sleep on a lock, we
672 * are ensured that there is never a cycle; that is what guarantees that
673 * the while() loop in posix_locks_deadlock() eventually completes.
675 * Note: the above assumption may not be true when handling lock
676 * requests from a broken NFS client. It may also fail in the presence
677 * of tasks (such as posix threads) sharing the same open file table.
679 * To handle those cases, we just bail out after a few iterations.
682 #define MAX_DEADLK_ITERATIONS 10
684 /* Find a lock that the owner of the given block_fl is blocking on. */
685 static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
687 struct file_lock *fl;
689 list_for_each_entry(fl, &blocked_list, fl_link) {
690 if (posix_same_owner(fl, block_fl))
696 static int posix_locks_deadlock(struct file_lock *caller_fl,
697 struct file_lock *block_fl)
701 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
702 if (i++ > MAX_DEADLK_ITERATIONS)
704 if (posix_same_owner(caller_fl, block_fl))
710 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
711 * after any leases, but before any posix locks.
713 * Note that if called with an FL_EXISTS argument, the caller may determine
714 * whether or not a lock was successfully freed by testing the return
717 static int flock_lock_file(struct file *filp, struct file_lock *request)
719 struct file_lock *new_fl = NULL;
720 struct file_lock **before;
721 struct inode * inode = filp->f_path.dentry->d_inode;
725 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
726 new_fl = locks_alloc_lock();
732 if (request->fl_flags & FL_ACCESS)
735 for_each_lock(inode, before) {
736 struct file_lock *fl = *before;
741 if (filp != fl->fl_file)
743 if (request->fl_type == fl->fl_type)
746 locks_delete_lock(before);
750 if (request->fl_type == F_UNLCK) {
751 if ((request->fl_flags & FL_EXISTS) && !found)
757 * If a higher-priority process was blocked on the old file lock,
758 * give it the opportunity to lock the file.
767 for_each_lock(inode, before) {
768 struct file_lock *fl = *before;
773 if (!flock_locks_conflict(request, fl))
776 if (!(request->fl_flags & FL_SLEEP))
778 error = FILE_LOCK_DEFERRED;
779 locks_insert_block(fl, request);
782 if (request->fl_flags & FL_ACCESS)
784 locks_copy_lock(new_fl, request);
785 locks_insert_lock(before, new_fl);
792 locks_free_lock(new_fl);
796 static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
798 struct file_lock *fl;
799 struct file_lock *new_fl = NULL;
800 struct file_lock *new_fl2 = NULL;
801 struct file_lock *left = NULL;
802 struct file_lock *right = NULL;
803 struct file_lock **before;
804 int error, added = 0;
807 * We may need two file_lock structures for this operation,
808 * so we get them in advance to avoid races.
810 * In some cases we can be sure, that no new locks will be needed
812 if (!(request->fl_flags & FL_ACCESS) &&
813 (request->fl_type != F_UNLCK ||
814 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
815 new_fl = locks_alloc_lock();
816 new_fl2 = locks_alloc_lock();
820 if (request->fl_type != F_UNLCK) {
821 for_each_lock(inode, before) {
825 if (!posix_locks_conflict(request, fl))
828 __locks_copy_lock(conflock, fl);
830 if (!(request->fl_flags & FL_SLEEP))
833 if (posix_locks_deadlock(request, fl))
835 error = FILE_LOCK_DEFERRED;
836 locks_insert_block(fl, request);
841 /* If we're just looking for a conflict, we're done. */
843 if (request->fl_flags & FL_ACCESS)
847 * Find the first old lock with the same owner as the new lock.
850 before = &inode->i_flock;
852 /* First skip locks owned by other processes. */
853 while ((fl = *before) && (!IS_POSIX(fl) ||
854 !posix_same_owner(request, fl))) {
855 before = &fl->fl_next;
858 /* Process locks with this owner. */
859 while ((fl = *before) && posix_same_owner(request, fl)) {
860 /* Detect adjacent or overlapping regions (if same lock type)
862 if (request->fl_type == fl->fl_type) {
863 /* In all comparisons of start vs end, use
864 * "start - 1" rather than "end + 1". If end
865 * is OFFSET_MAX, end + 1 will become negative.
867 if (fl->fl_end < request->fl_start - 1)
869 /* If the next lock in the list has entirely bigger
870 * addresses than the new one, insert the lock here.
872 if (fl->fl_start - 1 > request->fl_end)
875 /* If we come here, the new and old lock are of the
876 * same type and adjacent or overlapping. Make one
877 * lock yielding from the lower start address of both
878 * locks to the higher end address.
880 if (fl->fl_start > request->fl_start)
881 fl->fl_start = request->fl_start;
883 request->fl_start = fl->fl_start;
884 if (fl->fl_end < request->fl_end)
885 fl->fl_end = request->fl_end;
887 request->fl_end = fl->fl_end;
889 locks_delete_lock(before);
896 /* Processing for different lock types is a bit
899 if (fl->fl_end < request->fl_start)
901 if (fl->fl_start > request->fl_end)
903 if (request->fl_type == F_UNLCK)
905 if (fl->fl_start < request->fl_start)
907 /* If the next lock in the list has a higher end
908 * address than the new one, insert the new one here.
910 if (fl->fl_end > request->fl_end) {
914 if (fl->fl_start >= request->fl_start) {
915 /* The new lock completely replaces an old
916 * one (This may happen several times).
919 locks_delete_lock(before);
922 /* Replace the old lock with the new one.
923 * Wake up anybody waiting for the old one,
924 * as the change in lock type might satisfy
927 locks_wake_up_blocks(fl);
928 fl->fl_start = request->fl_start;
929 fl->fl_end = request->fl_end;
930 fl->fl_type = request->fl_type;
931 locks_release_private(fl);
932 locks_copy_private(fl, request);
937 /* Go on to next lock.
940 before = &fl->fl_next;
944 * The above code only modifies existing locks in case of
945 * merging or replacing. If new lock(s) need to be inserted
946 * all modifications are done bellow this, so it's safe yet to
949 error = -ENOLCK; /* "no luck" */
950 if (right && left == right && !new_fl2)
955 if (request->fl_type == F_UNLCK) {
956 if (request->fl_flags & FL_EXISTS)
965 locks_copy_lock(new_fl, request);
966 locks_insert_lock(before, new_fl);
971 /* The new lock breaks the old one in two pieces,
972 * so we have to use the second new lock.
976 locks_copy_lock(left, right);
977 locks_insert_lock(before, left);
979 right->fl_start = request->fl_end + 1;
980 locks_wake_up_blocks(right);
983 left->fl_end = request->fl_start - 1;
984 locks_wake_up_blocks(left);
989 * Free any unused locks.
992 locks_free_lock(new_fl);
994 locks_free_lock(new_fl2);
999 * posix_lock_file - Apply a POSIX-style lock to a file
1000 * @filp: The file to apply the lock to
1001 * @fl: The lock to be applied
1002 * @conflock: Place to return a copy of the conflicting lock, if found.
1004 * Add a POSIX style lock to a file.
1005 * We merge adjacent & overlapping locks whenever possible.
1006 * POSIX locks are sorted by owner task, then by starting address
1008 * Note that if called with an FL_EXISTS argument, the caller may determine
1009 * whether or not a lock was successfully freed by testing the return
1010 * value for -ENOENT.
1012 int posix_lock_file(struct file *filp, struct file_lock *fl,
1013 struct file_lock *conflock)
1015 return __posix_lock_file(filp->f_path.dentry->d_inode, fl, conflock);
1017 EXPORT_SYMBOL(posix_lock_file);
1020 * posix_lock_file_wait - Apply a POSIX-style lock to a file
1021 * @filp: The file to apply the lock to
1022 * @fl: The lock to be applied
1024 * Add a POSIX style lock to a file.
1025 * We merge adjacent & overlapping locks whenever possible.
1026 * POSIX locks are sorted by owner task, then by starting address
1028 int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
1033 error = posix_lock_file(filp, fl, NULL);
1034 if (error != FILE_LOCK_DEFERRED)
1036 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1040 locks_delete_block(fl);
1045 EXPORT_SYMBOL(posix_lock_file_wait);
1048 * locks_mandatory_locked - Check for an active lock
1049 * @inode: the file to check
1051 * Searches the inode's list of locks to find any POSIX locks which conflict.
1052 * This function is called from locks_verify_locked() only.
1054 int locks_mandatory_locked(struct inode *inode)
1056 fl_owner_t owner = current->files;
1057 struct file_lock *fl;
1060 * Search the lock list for this inode for any POSIX locks.
1063 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1066 if (fl->fl_owner != owner)
1070 return fl ? -EAGAIN : 0;
1074 * locks_mandatory_area - Check for a conflicting lock
1075 * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
1077 * @inode: the file to check
1078 * @filp: how the file was opened (if it was)
1079 * @offset: start of area to check
1080 * @count: length of area to check
1082 * Searches the inode's list of locks to find any POSIX locks which conflict.
1083 * This function is called from rw_verify_area() and
1084 * locks_verify_truncate().
1086 int locks_mandatory_area(int read_write, struct inode *inode,
1087 struct file *filp, loff_t offset,
1090 struct file_lock fl;
1093 locks_init_lock(&fl);
1094 fl.fl_owner = current->files;
1095 fl.fl_pid = current->tgid;
1097 fl.fl_flags = FL_POSIX | FL_ACCESS;
1098 if (filp && !(filp->f_flags & O_NONBLOCK))
1099 fl.fl_flags |= FL_SLEEP;
1100 fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
1101 fl.fl_start = offset;
1102 fl.fl_end = offset + count - 1;
1105 error = __posix_lock_file(inode, &fl, NULL);
1106 if (error != FILE_LOCK_DEFERRED)
1108 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1111 * If we've been sleeping someone might have
1112 * changed the permissions behind our back.
1114 if (__mandatory_lock(inode))
1118 locks_delete_block(&fl);
1125 EXPORT_SYMBOL(locks_mandatory_area);
1127 /* We already had a lease on this file; just change its type */
1128 int lease_modify(struct file_lock **before, int arg)
1130 struct file_lock *fl = *before;
1131 int error = assign_type(fl, arg);
1135 fl->fl_flags &= ~FL_INPROGRESS;
1136 locks_wake_up_blocks(fl);
1138 locks_delete_lock(before);
1142 EXPORT_SYMBOL(lease_modify);
1144 static void time_out_leases(struct inode *inode)
1146 struct file_lock **before;
1147 struct file_lock *fl;
1149 before = &inode->i_flock;
1150 while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
1151 if ((fl->fl_break_time == 0)
1152 || time_before(jiffies, fl->fl_break_time)) {
1153 before = &fl->fl_next;
1156 lease_modify(before, fl->fl_type);
1157 if (fl == *before) /* lease_modify may have freed fl */
1158 before = &fl->fl_next;
1163 * __break_lease - revoke all outstanding leases on file
1164 * @inode: the inode of the file to return
1165 * @mode: the open mode (read or write)
1167 * break_lease (inlined for speed) has checked there already is at least
1168 * some kind of lock (maybe a lease) on this file. Leases are broken on
1169 * a call to open() or truncate(). This function can sleep unless you
1170 * specified %O_NONBLOCK to your open().
1172 int __break_lease(struct inode *inode, unsigned int mode)
1174 int error = 0, future;
1175 struct file_lock *new_fl, *flock;
1176 struct file_lock *fl;
1177 unsigned long break_time;
1178 int i_have_this_lease = 0;
1179 int want_write = (mode & O_ACCMODE) != O_RDONLY;
1181 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
1185 time_out_leases(inode);
1187 flock = inode->i_flock;
1188 if ((flock == NULL) || !IS_LEASE(flock))
1191 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
1192 if (fl->fl_owner == current->files)
1193 i_have_this_lease = 1;
1196 /* If we want write access, we have to revoke any lease. */
1198 } else if (lease_breaking(flock)) {
1199 /* If the lease is already being broken, we just leave it */
1200 future = flock->fl_type;
1201 } else if (flock->fl_type & F_WRLCK) {
1202 /* Downgrade the exclusive lease to a read-only lease. */
1205 /* the existing lease was read-only, so we can read too. */
1209 if (IS_ERR(new_fl) && !i_have_this_lease
1210 && ((mode & O_NONBLOCK) == 0)) {
1211 error = PTR_ERR(new_fl);
1216 if (lease_break_time > 0) {
1217 break_time = jiffies + lease_break_time * HZ;
1218 if (break_time == 0)
1219 break_time++; /* so that 0 means no break time */
1222 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1223 if (fl->fl_type != future) {
1224 fl->fl_type = future;
1225 fl->fl_flags |= FL_INPROGRESS;
1226 fl->fl_break_time = break_time;
1227 /* lease must have lmops break callback */
1228 fl->fl_lmops->lm_break(fl);
1232 if (i_have_this_lease || (mode & O_NONBLOCK)) {
1233 error = -EWOULDBLOCK;
1238 break_time = flock->fl_break_time;
1239 if (break_time != 0) {
1240 break_time -= jiffies;
1241 if (break_time == 0)
1244 locks_insert_block(flock, new_fl);
1246 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1247 !new_fl->fl_next, break_time);
1249 __locks_delete_block(new_fl);
1252 time_out_leases(inode);
1253 /* Wait for the next lease that has not been broken yet */
1254 for (flock = inode->i_flock; flock && IS_LEASE(flock);
1255 flock = flock->fl_next) {
1256 if (lease_breaking(flock))
1264 if (!IS_ERR(new_fl))
1265 locks_free_lock(new_fl);
1269 EXPORT_SYMBOL(__break_lease);
1272 * lease_get_mtime - get the last modified time of an inode
1274 * @time: pointer to a timespec which will contain the last modified time
1276 * This is to force NFS clients to flush their caches for files with
1277 * exclusive leases. The justification is that if someone has an
1278 * exclusive lease, then they could be modifying it.
1280 void lease_get_mtime(struct inode *inode, struct timespec *time)
1282 struct file_lock *flock = inode->i_flock;
1283 if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
1284 *time = current_fs_time(inode->i_sb);
1286 *time = inode->i_mtime;
1289 EXPORT_SYMBOL(lease_get_mtime);
1292 * fcntl_getlease - Enquire what lease is currently active
1295 * The value returned by this function will be one of
1296 * (if no lease break is pending):
1298 * %F_RDLCK to indicate a shared lease is held.
1300 * %F_WRLCK to indicate an exclusive lease is held.
1302 * %F_UNLCK to indicate no lease is held.
1304 * (if a lease break is pending):
1306 * %F_RDLCK to indicate an exclusive lease needs to be
1307 * changed to a shared lease (or removed).
1309 * %F_UNLCK to indicate the lease needs to be removed.
1311 * XXX: sfr & willy disagree over whether F_INPROGRESS
1312 * should be returned to userspace.
1314 int fcntl_getlease(struct file *filp)
1316 struct file_lock *fl;
1320 time_out_leases(filp->f_path.dentry->d_inode);
1321 for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl);
1323 if (fl->fl_file == filp) {
1333 * generic_setlease - sets a lease on an open file
1334 * @filp: file pointer
1335 * @arg: type of lease to obtain
1336 * @flp: input - file_lock to use, output - file_lock inserted
1338 * The (input) flp->fl_lmops->lm_break function is required
1341 * Called with file_lock_lock held.
1343 int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
1345 struct file_lock *fl, **before, **my_before = NULL, *lease;
1346 struct dentry *dentry = filp->f_path.dentry;
1347 struct inode *inode = dentry->d_inode;
1348 int error, rdlease_count = 0, wrlease_count = 0;
1353 if ((current_fsuid() != inode->i_uid) && !capable(CAP_LEASE))
1356 if (!S_ISREG(inode->i_mode))
1358 error = security_file_lock(filp, arg);
1362 time_out_leases(inode);
1364 BUG_ON(!(*flp)->fl_lmops->lm_break);
1366 if (arg != F_UNLCK) {
1368 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
1370 if ((arg == F_WRLCK)
1371 && ((dentry->d_count > 1)
1372 || (atomic_read(&inode->i_count) > 1)))
1377 * At this point, we know that if there is an exclusive
1378 * lease on this file, then we hold it on this filp
1379 * (otherwise our open of this file would have blocked).
1380 * And if we are trying to acquire an exclusive lease,
1381 * then the file is not open by anyone (including us)
1382 * except for this filp.
1384 for (before = &inode->i_flock;
1385 ((fl = *before) != NULL) && IS_LEASE(fl);
1386 before = &fl->fl_next) {
1387 if (fl->fl_file == filp)
1389 else if ((fl->fl_type == F_UNLCK) && lease_breaking(fl))
1391 * Someone is in the process of opening this
1392 * file for writing so we may not take an
1393 * exclusive lease on it.
1401 if ((arg == F_RDLCK && (wrlease_count > 0)) ||
1402 (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
1405 if (my_before != NULL) {
1406 error = lease->fl_lmops->lm_change(my_before, arg);
1419 locks_insert_lock(before, lease);
1425 EXPORT_SYMBOL(generic_setlease);
1427 static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1429 if (filp->f_op && filp->f_op->setlease)
1430 return filp->f_op->setlease(filp, arg, lease);
1432 return generic_setlease(filp, arg, lease);
1436 * vfs_setlease - sets a lease on an open file
1437 * @filp: file pointer
1438 * @arg: type of lease to obtain
1439 * @lease: file_lock to use
1441 * Call this to establish a lease on the file.
1442 * The (*lease)->fl_lmops->lm_break operation must be set; if not,
1443 * break_lease will oops!
1445 * This will call the filesystem's setlease file method, if
1446 * defined. Note that there is no getlease method; instead, the
1447 * filesystem setlease method should call back to setlease() to
1448 * add a lease to the inode's lease list, where fcntl_getlease() can
1449 * find it. Since fcntl_getlease() only reports whether the current
1450 * task holds a lease, a cluster filesystem need only do this for
1451 * leases held by processes on this node.
1453 * There is also no break_lease method; filesystems that
1454 * handle their own leases should break leases themselves from the
1455 * filesystem's open, create, and (on truncate) setattr methods.
1457 * Warning: the only current setlease methods exist only to disable
1458 * leases in certain cases. More vfs changes may be required to
1459 * allow a full filesystem lease implementation.
1462 int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1467 error = __vfs_setlease(filp, arg, lease);
1472 EXPORT_SYMBOL_GPL(vfs_setlease);
1474 static int do_fcntl_delete_lease(struct file *filp)
1476 struct file_lock fl, *flp = &fl;
1478 lease_init(filp, F_UNLCK, flp);
1480 return vfs_setlease(filp, F_UNLCK, &flp);
1483 static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
1485 struct file_lock *fl, *ret;
1486 struct fasync_struct *new;
1489 fl = lease_alloc(filp, arg);
1493 new = fasync_alloc();
1495 locks_free_lock(fl);
1500 error = __vfs_setlease(filp, arg, &ret);
1503 locks_free_lock(fl);
1504 goto out_free_fasync;
1507 locks_free_lock(fl);
1510 * fasync_insert_entry() returns the old entry if any.
1511 * If there was no old entry, then it used 'new' and
1512 * inserted it into the fasync list. Clear new so that
1513 * we don't release it here.
1515 if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
1518 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
1528 * fcntl_setlease - sets a lease on an open file
1529 * @fd: open file descriptor
1530 * @filp: file pointer
1531 * @arg: type of lease to obtain
1533 * Call this fcntl to establish a lease on the file.
1534 * Note that you also need to call %F_SETSIG to
1535 * receive a signal when the lease is broken.
1537 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1540 return do_fcntl_delete_lease(filp);
1541 return do_fcntl_add_lease(fd, filp, arg);
1545 * flock_lock_file_wait - Apply a FLOCK-style lock to a file
1546 * @filp: The file to apply the lock to
1547 * @fl: The lock to be applied
1549 * Add a FLOCK style lock to a file.
1551 int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
1556 error = flock_lock_file(filp, fl);
1557 if (error != FILE_LOCK_DEFERRED)
1559 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1563 locks_delete_block(fl);
1569 EXPORT_SYMBOL(flock_lock_file_wait);
1572 * sys_flock: - flock() system call.
1573 * @fd: the file descriptor to lock.
1574 * @cmd: the type of lock to apply.
1576 * Apply a %FL_FLOCK style lock to an open file descriptor.
1577 * The @cmd can be one of
1579 * %LOCK_SH -- a shared lock.
1581 * %LOCK_EX -- an exclusive lock.
1583 * %LOCK_UN -- remove an existing lock.
1585 * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
1587 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1588 * processes read and write access respectively.
1590 SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
1593 struct file_lock *lock;
1594 int can_sleep, unlock;
1602 can_sleep = !(cmd & LOCK_NB);
1604 unlock = (cmd == LOCK_UN);
1606 if (!unlock && !(cmd & LOCK_MAND) &&
1607 !(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
1610 error = flock_make_lock(filp, &lock, cmd);
1614 lock->fl_flags |= FL_SLEEP;
1616 error = security_file_lock(filp, lock->fl_type);
1620 if (filp->f_op && filp->f_op->flock)
1621 error = filp->f_op->flock(filp,
1622 (can_sleep) ? F_SETLKW : F_SETLK,
1625 error = flock_lock_file_wait(filp, lock);
1628 locks_free_lock(lock);
1637 * vfs_test_lock - test file byte range lock
1638 * @filp: The file to test lock for
1639 * @fl: The lock to test; also used to hold result
1641 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
1642 * setting conf->fl_type to something other than F_UNLCK.
1644 int vfs_test_lock(struct file *filp, struct file_lock *fl)
1646 if (filp->f_op && filp->f_op->lock)
1647 return filp->f_op->lock(filp, F_GETLK, fl);
1648 posix_test_lock(filp, fl);
1651 EXPORT_SYMBOL_GPL(vfs_test_lock);
1653 static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1655 flock->l_pid = fl->fl_pid;
1656 #if BITS_PER_LONG == 32
1658 * Make sure we can represent the posix lock via
1659 * legacy 32bit flock.
1661 if (fl->fl_start > OFFT_OFFSET_MAX)
1663 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1666 flock->l_start = fl->fl_start;
1667 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1668 fl->fl_end - fl->fl_start + 1;
1669 flock->l_whence = 0;
1670 flock->l_type = fl->fl_type;
1674 #if BITS_PER_LONG == 32
1675 static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1677 flock->l_pid = fl->fl_pid;
1678 flock->l_start = fl->fl_start;
1679 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1680 fl->fl_end - fl->fl_start + 1;
1681 flock->l_whence = 0;
1682 flock->l_type = fl->fl_type;
1686 /* Report the first existing lock that would conflict with l.
1687 * This implements the F_GETLK command of fcntl().
1689 int fcntl_getlk(struct file *filp, struct flock __user *l)
1691 struct file_lock file_lock;
1696 if (copy_from_user(&flock, l, sizeof(flock)))
1699 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1702 error = flock_to_posix_lock(filp, &file_lock, &flock);
1706 error = vfs_test_lock(filp, &file_lock);
1710 flock.l_type = file_lock.fl_type;
1711 if (file_lock.fl_type != F_UNLCK) {
1712 error = posix_lock_to_flock(&flock, &file_lock);
1717 if (!copy_to_user(l, &flock, sizeof(flock)))
1724 * vfs_lock_file - file byte range lock
1725 * @filp: The file to apply the lock to
1726 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
1727 * @fl: The lock to be applied
1728 * @conf: Place to return a copy of the conflicting lock, if found.
1730 * A caller that doesn't care about the conflicting lock may pass NULL
1731 * as the final argument.
1733 * If the filesystem defines a private ->lock() method, then @conf will
1734 * be left unchanged; so a caller that cares should initialize it to
1735 * some acceptable default.
1737 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
1738 * locks, the ->lock() interface may return asynchronously, before the lock has
1739 * been granted or denied by the underlying filesystem, if (and only if)
1740 * lm_grant is set. Callers expecting ->lock() to return asynchronously
1741 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
1742 * the request is for a blocking lock. When ->lock() does return asynchronously,
1743 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
1744 * request completes.
1745 * If the request is for non-blocking lock the file system should return
1746 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
1747 * with the result. If the request timed out the callback routine will return a
1748 * nonzero return code and the file system should release the lock. The file
1749 * system is also responsible to keep a corresponding posix lock when it
1750 * grants a lock so the VFS can find out which locks are locally held and do
1751 * the correct lock cleanup when required.
1752 * The underlying filesystem must not drop the kernel lock or call
1753 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
1756 int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
1758 if (filp->f_op && filp->f_op->lock)
1759 return filp->f_op->lock(filp, cmd, fl);
1761 return posix_lock_file(filp, fl, conf);
1763 EXPORT_SYMBOL_GPL(vfs_lock_file);
1765 static int do_lock_file_wait(struct file *filp, unsigned int cmd,
1766 struct file_lock *fl)
1770 error = security_file_lock(filp, fl->fl_type);
1775 error = vfs_lock_file(filp, cmd, fl, NULL);
1776 if (error != FILE_LOCK_DEFERRED)
1778 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1782 locks_delete_block(fl);
1789 /* Apply the lock described by l to an open file descriptor.
1790 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1792 int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
1793 struct flock __user *l)
1795 struct file_lock *file_lock = locks_alloc_lock();
1797 struct inode *inode;
1801 if (file_lock == NULL)
1805 * This might block, so we do it before checking the inode.
1808 if (copy_from_user(&flock, l, sizeof(flock)))
1811 inode = filp->f_path.dentry->d_inode;
1813 /* Don't allow mandatory locks on files that may be memory mapped
1816 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
1822 error = flock_to_posix_lock(filp, file_lock, &flock);
1825 if (cmd == F_SETLKW) {
1826 file_lock->fl_flags |= FL_SLEEP;
1830 switch (flock.l_type) {
1832 if (!(filp->f_mode & FMODE_READ))
1836 if (!(filp->f_mode & FMODE_WRITE))
1846 error = do_lock_file_wait(filp, cmd, file_lock);
1849 * Attempt to detect a close/fcntl race and recover by
1850 * releasing the lock that was just acquired.
1853 * we need that spin_lock here - it prevents reordering between
1854 * update of inode->i_flock and check for it done in close().
1855 * rcu_read_lock() wouldn't do.
1857 spin_lock(¤t->files->file_lock);
1859 spin_unlock(¤t->files->file_lock);
1860 if (!error && f != filp && flock.l_type != F_UNLCK) {
1861 flock.l_type = F_UNLCK;
1866 locks_free_lock(file_lock);
1870 #if BITS_PER_LONG == 32
1871 /* Report the first existing lock that would conflict with l.
1872 * This implements the F_GETLK command of fcntl().
1874 int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
1876 struct file_lock file_lock;
1877 struct flock64 flock;
1881 if (copy_from_user(&flock, l, sizeof(flock)))
1884 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1887 error = flock64_to_posix_lock(filp, &file_lock, &flock);
1891 error = vfs_test_lock(filp, &file_lock);
1895 flock.l_type = file_lock.fl_type;
1896 if (file_lock.fl_type != F_UNLCK)
1897 posix_lock_to_flock64(&flock, &file_lock);
1900 if (!copy_to_user(l, &flock, sizeof(flock)))
1907 /* Apply the lock described by l to an open file descriptor.
1908 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1910 int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
1911 struct flock64 __user *l)
1913 struct file_lock *file_lock = locks_alloc_lock();
1914 struct flock64 flock;
1915 struct inode *inode;
1919 if (file_lock == NULL)
1923 * This might block, so we do it before checking the inode.
1926 if (copy_from_user(&flock, l, sizeof(flock)))
1929 inode = filp->f_path.dentry->d_inode;
1931 /* Don't allow mandatory locks on files that may be memory mapped
1934 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
1940 error = flock64_to_posix_lock(filp, file_lock, &flock);
1943 if (cmd == F_SETLKW64) {
1944 file_lock->fl_flags |= FL_SLEEP;
1948 switch (flock.l_type) {
1950 if (!(filp->f_mode & FMODE_READ))
1954 if (!(filp->f_mode & FMODE_WRITE))
1964 error = do_lock_file_wait(filp, cmd, file_lock);
1967 * Attempt to detect a close/fcntl race and recover by
1968 * releasing the lock that was just acquired.
1970 spin_lock(¤t->files->file_lock);
1972 spin_unlock(¤t->files->file_lock);
1973 if (!error && f != filp && flock.l_type != F_UNLCK) {
1974 flock.l_type = F_UNLCK;
1979 locks_free_lock(file_lock);
1982 #endif /* BITS_PER_LONG == 32 */
1985 * This function is called when the file is being removed
1986 * from the task's fd array. POSIX locks belonging to this task
1987 * are deleted at this time.
1989 void locks_remove_posix(struct file *filp, fl_owner_t owner)
1991 struct file_lock lock;
1994 * If there are no locks held on this file, we don't need to call
1995 * posix_lock_file(). Another process could be setting a lock on this
1996 * file at the same time, but we wouldn't remove that lock anyway.
1998 if (!filp->f_path.dentry->d_inode->i_flock)
2001 lock.fl_type = F_UNLCK;
2002 lock.fl_flags = FL_POSIX | FL_CLOSE;
2004 lock.fl_end = OFFSET_MAX;
2005 lock.fl_owner = owner;
2006 lock.fl_pid = current->tgid;
2007 lock.fl_file = filp;
2009 lock.fl_lmops = NULL;
2011 vfs_lock_file(filp, F_SETLK, &lock, NULL);
2013 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2014 lock.fl_ops->fl_release_private(&lock);
2017 EXPORT_SYMBOL(locks_remove_posix);
2020 * This function is called on the last close of an open file.
2022 void locks_remove_flock(struct file *filp)
2024 struct inode * inode = filp->f_path.dentry->d_inode;
2025 struct file_lock *fl;
2026 struct file_lock **before;
2028 if (!inode->i_flock)
2031 if (filp->f_op && filp->f_op->flock) {
2032 struct file_lock fl = {
2033 .fl_pid = current->tgid,
2035 .fl_flags = FL_FLOCK,
2037 .fl_end = OFFSET_MAX,
2039 filp->f_op->flock(filp, F_SETLKW, &fl);
2040 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2041 fl.fl_ops->fl_release_private(&fl);
2045 before = &inode->i_flock;
2047 while ((fl = *before) != NULL) {
2048 if (fl->fl_file == filp) {
2050 locks_delete_lock(before);
2054 lease_modify(before, F_UNLCK);
2060 before = &fl->fl_next;
2066 * posix_unblock_lock - stop waiting for a file lock
2067 * @filp: how the file was opened
2068 * @waiter: the lock which was waiting
2070 * lockd needs to block waiting for locks.
2073 posix_unblock_lock(struct file *filp, struct file_lock *waiter)
2078 if (waiter->fl_next)
2079 __locks_delete_block(waiter);
2086 EXPORT_SYMBOL(posix_unblock_lock);
2089 * vfs_cancel_lock - file byte range unblock lock
2090 * @filp: The file to apply the unblock to
2091 * @fl: The lock to be unblocked
2093 * Used by lock managers to cancel blocked requests
2095 int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2097 if (filp->f_op && filp->f_op->lock)
2098 return filp->f_op->lock(filp, F_CANCELLK, fl);
2102 EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2104 #ifdef CONFIG_PROC_FS
2105 #include <linux/proc_fs.h>
2106 #include <linux/seq_file.h>
2108 static void lock_get_status(struct seq_file *f, struct file_lock *fl,
2109 loff_t id, char *pfx)
2111 struct inode *inode = NULL;
2112 unsigned int fl_pid;
2115 fl_pid = pid_vnr(fl->fl_nspid);
2117 fl_pid = fl->fl_pid;
2119 if (fl->fl_file != NULL)
2120 inode = fl->fl_file->f_path.dentry->d_inode;
2122 seq_printf(f, "%lld:%s ", id, pfx);
2124 seq_printf(f, "%6s %s ",
2125 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
2126 (inode == NULL) ? "*NOINODE*" :
2127 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
2128 } else if (IS_FLOCK(fl)) {
2129 if (fl->fl_type & LOCK_MAND) {
2130 seq_printf(f, "FLOCK MSNFS ");
2132 seq_printf(f, "FLOCK ADVISORY ");
2134 } else if (IS_LEASE(fl)) {
2135 seq_printf(f, "LEASE ");
2136 if (lease_breaking(fl))
2137 seq_printf(f, "BREAKING ");
2138 else if (fl->fl_file)
2139 seq_printf(f, "ACTIVE ");
2141 seq_printf(f, "BREAKER ");
2143 seq_printf(f, "UNKNOWN UNKNOWN ");
2145 if (fl->fl_type & LOCK_MAND) {
2146 seq_printf(f, "%s ",
2147 (fl->fl_type & LOCK_READ)
2148 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2149 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2151 seq_printf(f, "%s ",
2152 (lease_breaking(fl))
2153 ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
2154 : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
2157 #ifdef WE_CAN_BREAK_LSLK_NOW
2158 seq_printf(f, "%d %s:%ld ", fl_pid,
2159 inode->i_sb->s_id, inode->i_ino);
2161 /* userspace relies on this representation of dev_t ;-( */
2162 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
2163 MAJOR(inode->i_sb->s_dev),
2164 MINOR(inode->i_sb->s_dev), inode->i_ino);
2167 seq_printf(f, "%d <none>:0 ", fl_pid);
2170 if (fl->fl_end == OFFSET_MAX)
2171 seq_printf(f, "%Ld EOF\n", fl->fl_start);
2173 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
2175 seq_printf(f, "0 EOF\n");
2179 static int locks_show(struct seq_file *f, void *v)
2181 struct file_lock *fl, *bfl;
2183 fl = list_entry(v, struct file_lock, fl_link);
2185 lock_get_status(f, fl, *((loff_t *)f->private), "");
2187 list_for_each_entry(bfl, &fl->fl_block, fl_block)
2188 lock_get_status(f, bfl, *((loff_t *)f->private), " ->");
2193 static void *locks_start(struct seq_file *f, loff_t *pos)
2195 loff_t *p = f->private;
2199 return seq_list_start(&file_lock_list, *pos);
2202 static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2204 loff_t *p = f->private;
2206 return seq_list_next(v, &file_lock_list, pos);
2209 static void locks_stop(struct seq_file *f, void *v)
2214 static const struct seq_operations locks_seq_operations = {
2215 .start = locks_start,
2221 static int locks_open(struct inode *inode, struct file *filp)
2223 return seq_open_private(filp, &locks_seq_operations, sizeof(loff_t));
2226 static const struct file_operations proc_locks_operations = {
2229 .llseek = seq_lseek,
2230 .release = seq_release_private,
2233 static int __init proc_locks_init(void)
2235 proc_create("locks", 0, NULL, &proc_locks_operations);
2238 module_init(proc_locks_init);
2242 * lock_may_read - checks that the region is free of locks
2243 * @inode: the inode that is being read
2244 * @start: the first byte to read
2245 * @len: the number of bytes to read
2247 * Emulates Windows locking requirements. Whole-file
2248 * mandatory locks (share modes) can prohibit a read and
2249 * byte-range POSIX locks can prohibit a read if they overlap.
2251 * N.B. this function is only ever called
2252 * from knfsd and ownership of locks is never checked.
2254 int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
2256 struct file_lock *fl;
2259 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2261 if (fl->fl_type == F_RDLCK)
2263 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2265 } else if (IS_FLOCK(fl)) {
2266 if (!(fl->fl_type & LOCK_MAND))
2268 if (fl->fl_type & LOCK_READ)
2279 EXPORT_SYMBOL(lock_may_read);
2282 * lock_may_write - checks that the region is free of locks
2283 * @inode: the inode that is being written
2284 * @start: the first byte to write
2285 * @len: the number of bytes to write
2287 * Emulates Windows locking requirements. Whole-file
2288 * mandatory locks (share modes) can prohibit a write and
2289 * byte-range POSIX locks can prohibit a write if they overlap.
2291 * N.B. this function is only ever called
2292 * from knfsd and ownership of locks is never checked.
2294 int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
2296 struct file_lock *fl;
2299 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2301 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2303 } else if (IS_FLOCK(fl)) {
2304 if (!(fl->fl_type & LOCK_MAND))
2306 if (fl->fl_type & LOCK_WRITE)
2317 EXPORT_SYMBOL(lock_may_write);
2319 static int __init filelock_init(void)
2321 filelock_cache = kmem_cache_create("file_lock_cache",
2322 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2327 core_initcall(filelock_init);