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.
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/fs.h>
120 #include <linux/init.h>
121 #include <linux/module.h>
122 #include <linux/security.h>
123 #include <linux/slab.h>
124 #include <linux/smp_lock.h>
125 #include <linux/time.h>
127 #include <asm/semaphore.h>
128 #include <asm/uaccess.h>
130 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
131 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
132 #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
134 int leases_enable = 1;
135 int lease_break_time = 45;
137 #define for_each_lock(inode, lockp) \
138 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
140 LIST_HEAD(file_lock_list);
142 EXPORT_SYMBOL(file_lock_list);
144 static LIST_HEAD(blocked_list);
146 static kmem_cache_t *filelock_cache;
148 /* Allocate an empty lock structure. */
149 static struct file_lock *locks_alloc_lock(void)
151 return kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
154 /* Free a lock which is not in use. */
155 static inline void locks_free_lock(struct file_lock *fl)
161 if (waitqueue_active(&fl->fl_wait))
162 panic("Attempting to free lock with active wait queue");
164 if (!list_empty(&fl->fl_block))
165 panic("Attempting to free lock with active block list");
167 if (!list_empty(&fl->fl_link))
168 panic("Attempting to free lock on active lock list");
170 kmem_cache_free(filelock_cache, fl);
173 void locks_init_lock(struct file_lock *fl)
175 INIT_LIST_HEAD(&fl->fl_link);
176 INIT_LIST_HEAD(&fl->fl_block);
177 init_waitqueue_head(&fl->fl_wait);
179 fl->fl_fasync = NULL;
185 fl->fl_start = fl->fl_end = 0;
186 fl->fl_notify = NULL;
187 fl->fl_insert = NULL;
188 fl->fl_remove = NULL;
191 EXPORT_SYMBOL(locks_init_lock);
194 * Initialises the fields of the file lock which are invariant for
197 static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags)
199 struct file_lock *lock = (struct file_lock *) foo;
201 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) !=
202 SLAB_CTOR_CONSTRUCTOR)
205 locks_init_lock(lock);
209 * Initialize a new lock from an existing file_lock structure.
211 void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
213 new->fl_owner = fl->fl_owner;
214 new->fl_pid = fl->fl_pid;
215 new->fl_file = fl->fl_file;
216 new->fl_flags = fl->fl_flags;
217 new->fl_type = fl->fl_type;
218 new->fl_start = fl->fl_start;
219 new->fl_end = fl->fl_end;
220 new->fl_notify = fl->fl_notify;
221 new->fl_insert = fl->fl_insert;
222 new->fl_remove = fl->fl_remove;
223 new->fl_u = fl->fl_u;
226 EXPORT_SYMBOL(locks_copy_lock);
228 static inline int flock_translate_cmd(int cmd) {
230 return cmd & (LOCK_MAND | LOCK_RW);
242 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
243 static int flock_make_lock(struct file *filp, struct file_lock **lock,
246 struct file_lock *fl;
247 int type = flock_translate_cmd(cmd);
251 fl = locks_alloc_lock();
256 fl->fl_pid = current->tgid;
257 fl->fl_flags = FL_FLOCK;
259 fl->fl_end = OFFSET_MAX;
265 static int assign_type(struct file_lock *fl, int type)
279 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
282 static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
287 switch (l->l_whence) {
295 start = i_size_read(filp->f_dentry->d_inode);
301 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
302 POSIX-2001 defines it. */
304 end = start + l->l_len - 1;
312 if (l->l_len > 0 && end < 0)
315 fl->fl_start = start; /* we record the absolute position */
318 fl->fl_end = OFFSET_MAX;
320 fl->fl_owner = current->files;
321 fl->fl_pid = current->tgid;
323 fl->fl_flags = FL_POSIX;
324 fl->fl_notify = NULL;
325 fl->fl_insert = NULL;
326 fl->fl_remove = NULL;
328 return assign_type(fl, l->l_type);
331 #if BITS_PER_LONG == 32
332 static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
337 switch (l->l_whence) {
345 start = i_size_read(filp->f_dentry->d_inode);
351 if (((start += l->l_start) < 0) || (l->l_len < 0))
353 fl->fl_end = start + l->l_len - 1;
354 if (l->l_len > 0 && fl->fl_end < 0)
356 fl->fl_start = start; /* we record the absolute position */
358 fl->fl_end = OFFSET_MAX;
360 fl->fl_owner = current->files;
361 fl->fl_pid = current->tgid;
363 fl->fl_flags = FL_POSIX;
364 fl->fl_notify = NULL;
365 fl->fl_insert = NULL;
366 fl->fl_remove = NULL;
372 fl->fl_type = l->l_type;
382 /* Allocate a file_lock initialised to this type of lease */
383 static int lease_alloc(struct file *filp, int type, struct file_lock **flp)
385 struct file_lock *fl = locks_alloc_lock();
389 fl->fl_owner = current->files;
390 fl->fl_pid = current->tgid;
393 fl->fl_flags = FL_LEASE;
394 if (assign_type(fl, type) != 0) {
399 fl->fl_end = OFFSET_MAX;
400 fl->fl_notify = NULL;
401 fl->fl_insert = NULL;
402 fl->fl_remove = NULL;
408 /* Check if two locks overlap each other.
410 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
412 return ((fl1->fl_end >= fl2->fl_start) &&
413 (fl2->fl_end >= fl1->fl_start));
417 * Check whether two locks have the same owner. The apparently superfluous
418 * check for fl_pid enables us to distinguish between locks set by lockd.
421 posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
423 return (fl1->fl_owner == fl2->fl_owner) &&
424 (fl1->fl_pid == fl2->fl_pid);
427 /* Remove waiter from blocker's block list.
428 * When blocker ends up pointing to itself then the list is empty.
430 static inline void __locks_delete_block(struct file_lock *waiter)
432 list_del_init(&waiter->fl_block);
433 list_del_init(&waiter->fl_link);
434 waiter->fl_next = NULL;
439 static void locks_delete_block(struct file_lock *waiter)
442 __locks_delete_block(waiter);
446 /* Insert waiter into blocker's block list.
447 * We use a circular list so that processes can be easily woken up in
448 * the order they blocked. The documentation doesn't require this but
449 * it seems like the reasonable thing to do.
451 static void locks_insert_block(struct file_lock *blocker,
452 struct file_lock *waiter)
454 if (!list_empty(&waiter->fl_block)) {
455 printk(KERN_ERR "locks_insert_block: removing duplicated lock "
456 "(pid=%d %Ld-%Ld type=%d)\n", waiter->fl_pid,
457 waiter->fl_start, waiter->fl_end, waiter->fl_type);
458 __locks_delete_block(waiter);
460 list_add_tail(&waiter->fl_block, &blocker->fl_block);
461 waiter->fl_next = blocker;
462 list_add(&waiter->fl_link, &blocked_list);
465 /* Wake up processes blocked waiting for blocker.
466 * If told to wait then schedule the processes until the block list
467 * is empty, otherwise empty the block list ourselves.
469 static void locks_wake_up_blocks(struct file_lock *blocker)
471 while (!list_empty(&blocker->fl_block)) {
472 struct file_lock *waiter = list_entry(blocker->fl_block.next,
473 struct file_lock, fl_block);
474 __locks_delete_block(waiter);
475 if (waiter->fl_notify)
476 waiter->fl_notify(waiter);
478 wake_up(&waiter->fl_wait);
482 /* Insert file lock fl into an inode's lock list at the position indicated
483 * by pos. At the same time add the lock to the global file lock list.
485 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
487 list_add(&fl->fl_link, &file_lock_list);
489 /* insert into file's list */
498 * Delete a lock and then free it.
499 * Wake up processes that are blocked waiting for this lock,
500 * notify the FS that the lock has been cleared and
501 * finally free the lock.
503 static void locks_delete_lock(struct file_lock **thisfl_p)
505 struct file_lock *fl = *thisfl_p;
507 *thisfl_p = fl->fl_next;
509 list_del_init(&fl->fl_link);
511 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
512 if (fl->fl_fasync != NULL) {
513 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
514 fl->fl_fasync = NULL;
520 locks_wake_up_blocks(fl);
524 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
525 * checks for shared/exclusive status of overlapping locks.
527 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
529 if (sys_fl->fl_type == F_WRLCK)
531 if (caller_fl->fl_type == F_WRLCK)
536 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
537 * checking before calling the locks_conflict().
539 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
541 /* POSIX locks owned by the same process do not conflict with
544 if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
547 /* Check whether they overlap */
548 if (!locks_overlap(caller_fl, sys_fl))
551 return (locks_conflict(caller_fl, sys_fl));
554 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
555 * checking before calling the locks_conflict().
557 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
559 /* FLOCK locks referring to the same filp do not conflict with
562 if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
564 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
567 return (locks_conflict(caller_fl, sys_fl));
570 static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout)
573 DECLARE_WAITQUEUE(wait, current);
575 __set_current_state(TASK_INTERRUPTIBLE);
576 add_wait_queue(fl_wait, &wait);
580 result = schedule_timeout(timeout);
581 if (signal_pending(current))
582 result = -ERESTARTSYS;
583 remove_wait_queue(fl_wait, &wait);
584 __set_current_state(TASK_RUNNING);
588 static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time)
591 locks_insert_block(blocker, waiter);
592 result = interruptible_sleep_on_locked(&waiter->fl_wait, time);
593 __locks_delete_block(waiter);
598 posix_test_lock(struct file *filp, struct file_lock *fl)
600 struct file_lock *cfl;
603 for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
606 if (posix_locks_conflict(cfl, fl))
614 EXPORT_SYMBOL(posix_test_lock);
616 /* This function tests for deadlock condition before putting a process to
617 * sleep. The detection scheme is no longer recursive. Recursive was neat,
618 * but dangerous - we risked stack corruption if the lock data was bad, or
619 * if the recursion was too deep for any other reason.
621 * We rely on the fact that a task can only be on one lock's wait queue
622 * at a time. When we find blocked_task on a wait queue we can re-search
623 * with blocked_task equal to that queue's owner, until either blocked_task
624 * isn't found, or blocked_task is found on a queue owned by my_task.
626 * Note: the above assumption may not be true when handling lock requests
627 * from a broken NFS client. But broken NFS clients have a lot more to
628 * worry about than proper deadlock detection anyway... --okir
630 int posix_locks_deadlock(struct file_lock *caller_fl,
631 struct file_lock *block_fl)
633 struct list_head *tmp;
634 fl_owner_t caller_owner, blocked_owner;
635 unsigned int caller_pid, blocked_pid;
637 caller_owner = caller_fl->fl_owner;
638 caller_pid = caller_fl->fl_pid;
639 blocked_owner = block_fl->fl_owner;
640 blocked_pid = block_fl->fl_pid;
643 if (caller_owner == blocked_owner && caller_pid == blocked_pid)
645 list_for_each(tmp, &blocked_list) {
646 struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
647 if ((fl->fl_owner == blocked_owner)
648 && (fl->fl_pid == blocked_pid)) {
650 blocked_owner = fl->fl_owner;
651 blocked_pid = fl->fl_pid;
658 EXPORT_SYMBOL(posix_locks_deadlock);
660 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
661 * at the head of the list, but that's secret knowledge known only to
662 * flock_lock_file and posix_lock_file.
664 static int flock_lock_file(struct file *filp, struct file_lock *new_fl)
666 struct file_lock **before;
667 struct inode * inode = filp->f_dentry->d_inode;
672 for_each_lock(inode, before) {
673 struct file_lock *fl = *before;
678 if (filp != fl->fl_file)
680 if (new_fl->fl_type == fl->fl_type)
683 locks_delete_lock(before);
688 if (new_fl->fl_type == F_UNLCK)
692 * If a higher-priority process was blocked on the old file lock,
693 * give it the opportunity to lock the file.
699 for_each_lock(inode, before) {
700 struct file_lock *fl = *before;
705 if (!flock_locks_conflict(new_fl, fl))
708 if (new_fl->fl_flags & FL_SLEEP) {
709 locks_insert_block(fl, new_fl);
713 locks_insert_lock(&inode->i_flock, new_fl);
721 EXPORT_SYMBOL(posix_lock_file);
723 static int __posix_lock_file(struct inode *inode, struct file_lock *request)
725 struct file_lock *fl;
726 struct file_lock *new_fl, *new_fl2;
727 struct file_lock *left = NULL;
728 struct file_lock *right = NULL;
729 struct file_lock **before;
730 int error, added = 0;
733 * We may need two file_lock structures for this operation,
734 * so we get them in advance to avoid races.
736 new_fl = locks_alloc_lock();
737 new_fl2 = locks_alloc_lock();
740 if (request->fl_type != F_UNLCK) {
741 for_each_lock(inode, before) {
742 struct file_lock *fl = *before;
745 if (!posix_locks_conflict(request, fl))
748 if (!(request->fl_flags & FL_SLEEP))
751 if (posix_locks_deadlock(request, fl))
754 locks_insert_block(fl, request);
759 /* If we're just looking for a conflict, we're done. */
761 if (request->fl_flags & FL_ACCESS)
764 error = -ENOLCK; /* "no luck" */
765 if (!(new_fl && new_fl2))
769 * We've allocated the new locks in advance, so there are no
770 * errors possible (and no blocking operations) from here on.
772 * Find the first old lock with the same owner as the new lock.
775 before = &inode->i_flock;
777 /* First skip locks owned by other processes. */
778 while ((fl = *before) && (!IS_POSIX(fl) ||
779 !posix_same_owner(request, fl))) {
780 before = &fl->fl_next;
783 /* Process locks with this owner. */
784 while ((fl = *before) && posix_same_owner(request, fl)) {
785 /* Detect adjacent or overlapping regions (if same lock type)
787 if (request->fl_type == fl->fl_type) {
788 if (fl->fl_end < request->fl_start - 1)
790 /* If the next lock in the list has entirely bigger
791 * addresses than the new one, insert the lock here.
793 if (fl->fl_start > request->fl_end + 1)
796 /* If we come here, the new and old lock are of the
797 * same type and adjacent or overlapping. Make one
798 * lock yielding from the lower start address of both
799 * locks to the higher end address.
801 if (fl->fl_start > request->fl_start)
802 fl->fl_start = request->fl_start;
804 request->fl_start = fl->fl_start;
805 if (fl->fl_end < request->fl_end)
806 fl->fl_end = request->fl_end;
808 request->fl_end = fl->fl_end;
810 locks_delete_lock(before);
817 /* Processing for different lock types is a bit
820 if (fl->fl_end < request->fl_start)
822 if (fl->fl_start > request->fl_end)
824 if (request->fl_type == F_UNLCK)
826 if (fl->fl_start < request->fl_start)
828 /* If the next lock in the list has a higher end
829 * address than the new one, insert the new one here.
831 if (fl->fl_end > request->fl_end) {
835 if (fl->fl_start >= request->fl_start) {
836 /* The new lock completely replaces an old
837 * one (This may happen several times).
840 locks_delete_lock(before);
843 /* Replace the old lock with the new one.
844 * Wake up anybody waiting for the old one,
845 * as the change in lock type might satisfy
848 locks_wake_up_blocks(fl);
849 fl->fl_start = request->fl_start;
850 fl->fl_end = request->fl_end;
851 fl->fl_type = request->fl_type;
852 fl->fl_u = request->fl_u;
857 /* Go on to next lock.
860 before = &fl->fl_next;
865 if (request->fl_type == F_UNLCK)
867 locks_copy_lock(new_fl, request);
868 locks_insert_lock(before, new_fl);
873 /* The new lock breaks the old one in two pieces,
874 * so we have to use the second new lock.
878 locks_copy_lock(left, right);
879 locks_insert_lock(before, left);
881 right->fl_start = request->fl_end + 1;
882 locks_wake_up_blocks(right);
885 left->fl_end = request->fl_start - 1;
886 locks_wake_up_blocks(left);
891 * Free any unused locks.
894 locks_free_lock(new_fl);
896 locks_free_lock(new_fl2);
901 * posix_lock_file - Apply a POSIX-style lock to a file
902 * @filp: The file to apply the lock to
903 * @fl: The lock to be applied
905 * Add a POSIX style lock to a file.
906 * We merge adjacent & overlapping locks whenever possible.
907 * POSIX locks are sorted by owner task, then by starting address
909 int posix_lock_file(struct file *filp, struct file_lock *fl)
911 return __posix_lock_file(filp->f_dentry->d_inode, fl);
915 * locks_mandatory_locked - Check for an active lock
916 * @inode: the file to check
918 * Searches the inode's list of locks to find any POSIX locks which conflict.
919 * This function is called from locks_verify_locked() only.
921 int locks_mandatory_locked(struct inode *inode)
923 fl_owner_t owner = current->files;
924 struct file_lock *fl;
927 * Search the lock list for this inode for any POSIX locks.
930 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
933 if (fl->fl_owner != owner)
937 return fl ? -EAGAIN : 0;
941 * locks_mandatory_area - Check for a conflicting lock
942 * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
944 * @inode: the file to check
945 * @filp: how the file was opened (if it was)
946 * @offset: start of area to check
947 * @count: length of area to check
949 * Searches the inode's list of locks to find any POSIX locks which conflict.
950 * This function is called from locks_verify_area() and
951 * locks_verify_truncate().
953 int locks_mandatory_area(int read_write, struct inode *inode,
954 struct file *filp, loff_t offset,
960 locks_init_lock(&fl);
961 fl.fl_owner = current->files;
962 fl.fl_pid = current->tgid;
964 fl.fl_flags = FL_POSIX | FL_ACCESS;
965 if (filp && !(filp->f_flags & O_NONBLOCK))
966 fl.fl_flags |= FL_SLEEP;
967 fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
968 fl.fl_start = offset;
969 fl.fl_end = offset + count - 1;
972 error = __posix_lock_file(inode, &fl);
973 if (error != -EAGAIN)
975 if (!(fl.fl_flags & FL_SLEEP))
977 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
980 * If we've been sleeping someone might have
981 * changed the permissions behind our back.
983 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
987 locks_delete_block(&fl);
994 EXPORT_SYMBOL(locks_mandatory_area);
996 /* We already had a lease on this file; just change its type */
997 static int lease_modify(struct file_lock **before, int arg)
999 struct file_lock *fl = *before;
1000 int error = assign_type(fl, arg);
1004 locks_wake_up_blocks(fl);
1005 if (arg == F_UNLCK) {
1006 struct file *filp = fl->fl_file;
1009 filp->f_owner.signum = 0;
1010 locks_delete_lock(before);
1015 static void time_out_leases(struct inode *inode)
1017 struct file_lock **before;
1018 struct file_lock *fl;
1020 before = &inode->i_flock;
1021 while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) {
1022 if ((fl->fl_break_time == 0)
1023 || time_before(jiffies, fl->fl_break_time)) {
1024 before = &fl->fl_next;
1027 printk(KERN_INFO "lease broken - owner pid = %d\n", fl->fl_pid);
1028 lease_modify(before, fl->fl_type & ~F_INPROGRESS);
1029 if (fl == *before) /* lease_modify may have freed fl */
1030 before = &fl->fl_next;
1035 * __break_lease - revoke all outstanding leases on file
1036 * @inode: the inode of the file to return
1037 * @mode: the open mode (read or write)
1039 * break_lease (inlined for speed) has checked there already
1040 * is a lease on this file. Leases are broken on a call to open()
1041 * or truncate(). This function can sleep unless you
1042 * specified %O_NONBLOCK to your open().
1044 int __break_lease(struct inode *inode, unsigned int mode)
1046 int error = 0, future;
1047 struct file_lock *new_fl, *flock;
1048 struct file_lock *fl;
1050 unsigned long break_time;
1051 int i_have_this_lease = 0;
1053 alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK,
1058 time_out_leases(inode);
1060 flock = inode->i_flock;
1061 if ((flock == NULL) || !IS_LEASE(flock))
1064 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
1065 if (fl->fl_owner == current->files)
1066 i_have_this_lease = 1;
1068 if (mode & FMODE_WRITE) {
1069 /* If we want write access, we have to revoke any lease. */
1070 future = F_UNLCK | F_INPROGRESS;
1071 } else if (flock->fl_type & F_INPROGRESS) {
1072 /* If the lease is already being broken, we just leave it */
1073 future = flock->fl_type;
1074 } else if (flock->fl_type & F_WRLCK) {
1075 /* Downgrade the exclusive lease to a read-only lease. */
1076 future = F_RDLCK | F_INPROGRESS;
1078 /* the existing lease was read-only, so we can read too. */
1082 if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) {
1088 if (lease_break_time > 0) {
1089 break_time = jiffies + lease_break_time * HZ;
1090 if (break_time == 0)
1091 break_time++; /* so that 0 means no break time */
1094 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1095 if (fl->fl_type != future) {
1096 fl->fl_type = future;
1097 fl->fl_break_time = break_time;
1098 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
1102 if (i_have_this_lease || (mode & O_NONBLOCK)) {
1103 error = -EWOULDBLOCK;
1108 break_time = flock->fl_break_time;
1109 if (break_time != 0) {
1110 break_time -= jiffies;
1111 if (break_time == 0)
1114 error = locks_block_on_timeout(flock, new_fl, break_time);
1117 time_out_leases(inode);
1118 /* Wait for the next lease that has not been broken yet */
1119 for (flock = inode->i_flock; flock && IS_LEASE(flock);
1120 flock = flock->fl_next) {
1121 if (flock->fl_type & F_INPROGRESS)
1130 locks_free_lock(new_fl);
1134 EXPORT_SYMBOL(__break_lease);
1139 * @time: pointer to a timespec which will contain the last modified time
1141 * This is to force NFS clients to flush their caches for files with
1142 * exclusive leases. The justification is that if someone has an
1143 * exclusive lease, then they could be modifiying it.
1145 void lease_get_mtime(struct inode *inode, struct timespec *time)
1147 struct file_lock *flock = inode->i_flock;
1148 if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
1149 *time = CURRENT_TIME;
1151 *time = inode->i_mtime;
1154 EXPORT_SYMBOL(lease_get_mtime);
1157 * fcntl_getlease - Enquire what lease is currently active
1160 * The value returned by this function will be one of
1161 * (if no lease break is pending):
1163 * %F_RDLCK to indicate a shared lease is held.
1165 * %F_WRLCK to indicate an exclusive lease is held.
1167 * %F_UNLCK to indicate no lease is held.
1169 * (if a lease break is pending):
1171 * %F_RDLCK to indicate an exclusive lease needs to be
1172 * changed to a shared lease (or removed).
1174 * %F_UNLCK to indicate the lease needs to be removed.
1176 * XXX: sfr & willy disagree over whether F_INPROGRESS
1177 * should be returned to userspace.
1179 int fcntl_getlease(struct file *filp)
1181 struct file_lock *fl;
1185 time_out_leases(filp->f_dentry->d_inode);
1186 for (fl = filp->f_dentry->d_inode->i_flock; fl && IS_LEASE(fl);
1188 if (fl->fl_file == filp) {
1189 type = fl->fl_type & ~F_INPROGRESS;
1198 * fcntl_setlease - sets a lease on an open file
1199 * @fd: open file descriptor
1200 * @filp: file pointer
1201 * @arg: type of lease to obtain
1203 * Call this fcntl to establish a lease on the file.
1204 * Note that you also need to call %F_SETSIG to
1205 * receive a signal when the lease is broken.
1207 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1209 struct file_lock *fl, **before, **my_before = NULL;
1210 struct dentry *dentry;
1211 struct inode *inode;
1212 int error, rdlease_count = 0, wrlease_count = 0;
1214 dentry = filp->f_dentry;
1215 inode = dentry->d_inode;
1217 if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
1219 if (!S_ISREG(inode->i_mode))
1221 error = security_file_lock(filp, arg);
1227 time_out_leases(inode);
1230 * FIXME: What about F_RDLCK and files open for writing?
1233 if ((arg == F_WRLCK)
1234 && ((atomic_read(&dentry->d_count) > 1)
1235 || (atomic_read(&inode->i_count) > 1)))
1239 * At this point, we know that if there is an exclusive
1240 * lease on this file, then we hold it on this filp
1241 * (otherwise our open of this file would have blocked).
1242 * And if we are trying to acquire an exclusive lease,
1243 * then the file is not open by anyone (including us)
1244 * except for this filp.
1246 for (before = &inode->i_flock;
1247 ((fl = *before) != NULL) && IS_LEASE(fl);
1248 before = &fl->fl_next) {
1249 if (fl->fl_file == filp)
1251 else if (fl->fl_type == (F_INPROGRESS | F_UNLCK))
1253 * Someone is in the process of opening this
1254 * file for writing so we may not take an
1255 * exclusive lease on it.
1262 if ((arg == F_RDLCK && (wrlease_count > 0)) ||
1263 (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
1266 if (my_before != NULL) {
1267 error = lease_modify(my_before, arg);
1279 error = lease_alloc(filp, arg, &fl);
1283 error = fasync_helper(fd, filp, 1, &fl->fl_fasync);
1285 locks_free_lock(fl);
1289 locks_insert_lock(before, fl);
1291 error = f_setown(filp, current->pid, 0);
1298 * sys_flock: - flock() system call.
1299 * @fd: the file descriptor to lock.
1300 * @cmd: the type of lock to apply.
1302 * Apply a %FL_FLOCK style lock to an open file descriptor.
1303 * The @cmd can be one of
1305 * %LOCK_SH -- a shared lock.
1307 * %LOCK_EX -- an exclusive lock.
1309 * %LOCK_UN -- remove an existing lock.
1311 * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
1313 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1314 * processes read and write access respectively.
1316 asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
1319 struct file_lock *lock;
1320 int can_sleep, unlock;
1328 can_sleep = !(cmd & LOCK_NB);
1330 unlock = (cmd == LOCK_UN);
1332 if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3))
1335 error = flock_make_lock(filp, &lock, cmd);
1339 lock->fl_flags |= FL_SLEEP;
1341 error = security_file_lock(filp, cmd);
1346 error = flock_lock_file(filp, lock);
1347 if ((error != -EAGAIN) || !can_sleep)
1349 error = wait_event_interruptible(lock->fl_wait, !lock->fl_next);
1353 locks_delete_block(lock);
1358 if (list_empty(&lock->fl_link)) {
1359 locks_free_lock(lock);
1368 /* Report the first existing lock that would conflict with l.
1369 * This implements the F_GETLK command of fcntl().
1371 int fcntl_getlk(struct file *filp, struct flock __user *l)
1373 struct file_lock *fl, file_lock;
1378 if (copy_from_user(&flock, l, sizeof(flock)))
1381 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1384 error = flock_to_posix_lock(filp, &file_lock, &flock);
1388 if (filp->f_op && filp->f_op->lock) {
1389 error = filp->f_op->lock(filp, F_GETLK, &file_lock);
1392 else if (error == LOCK_USE_CLNT)
1393 /* Bypass for NFS with no locking - 2.0.36 compat */
1394 fl = posix_test_lock(filp, &file_lock);
1396 fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
1398 fl = posix_test_lock(filp, &file_lock);
1401 flock.l_type = F_UNLCK;
1403 flock.l_pid = fl->fl_pid;
1404 #if BITS_PER_LONG == 32
1406 * Make sure we can represent the posix lock via
1407 * legacy 32bit flock.
1410 if (fl->fl_start > OFFT_OFFSET_MAX)
1412 if ((fl->fl_end != OFFSET_MAX)
1413 && (fl->fl_end > OFFT_OFFSET_MAX))
1416 flock.l_start = fl->fl_start;
1417 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
1418 fl->fl_end - fl->fl_start + 1;
1420 flock.l_type = fl->fl_type;
1423 if (!copy_to_user(l, &flock, sizeof(flock)))
1430 /* Apply the lock described by l to an open file descriptor.
1431 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1433 int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
1435 struct file_lock *file_lock = locks_alloc_lock();
1437 struct inode *inode;
1440 if (file_lock == NULL)
1444 * This might block, so we do it before checking the inode.
1447 if (copy_from_user(&flock, l, sizeof(flock)))
1450 inode = filp->f_dentry->d_inode;
1452 /* Don't allow mandatory locks on files that may be memory mapped
1455 if (IS_MANDLOCK(inode) &&
1456 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
1457 mapping_writably_mapped(filp->f_mapping)) {
1462 error = flock_to_posix_lock(filp, file_lock, &flock);
1465 if (cmd == F_SETLKW) {
1466 file_lock->fl_flags |= FL_SLEEP;
1470 switch (flock.l_type) {
1472 if (!(filp->f_mode & FMODE_READ))
1476 if (!(filp->f_mode & FMODE_WRITE))
1486 error = security_file_lock(filp, file_lock->fl_type);
1490 if (filp->f_op && filp->f_op->lock != NULL) {
1491 error = filp->f_op->lock(filp, cmd, file_lock);
1497 error = __posix_lock_file(inode, file_lock);
1498 if ((error != -EAGAIN) || (cmd == F_SETLK))
1500 error = wait_event_interruptible(file_lock->fl_wait,
1501 !file_lock->fl_next);
1505 locks_delete_block(file_lock);
1510 locks_free_lock(file_lock);
1514 #if BITS_PER_LONG == 32
1515 /* Report the first existing lock that would conflict with l.
1516 * This implements the F_GETLK command of fcntl().
1518 int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
1520 struct file_lock *fl, file_lock;
1521 struct flock64 flock;
1525 if (copy_from_user(&flock, l, sizeof(flock)))
1528 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1531 error = flock64_to_posix_lock(filp, &file_lock, &flock);
1535 if (filp->f_op && filp->f_op->lock) {
1536 error = filp->f_op->lock(filp, F_GETLK, &file_lock);
1539 else if (error == LOCK_USE_CLNT)
1540 /* Bypass for NFS with no locking - 2.0.36 compat */
1541 fl = posix_test_lock(filp, &file_lock);
1543 fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
1545 fl = posix_test_lock(filp, &file_lock);
1548 flock.l_type = F_UNLCK;
1550 flock.l_pid = fl->fl_pid;
1551 flock.l_start = fl->fl_start;
1552 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
1553 fl->fl_end - fl->fl_start + 1;
1555 flock.l_type = fl->fl_type;
1558 if (!copy_to_user(l, &flock, sizeof(flock)))
1565 /* Apply the lock described by l to an open file descriptor.
1566 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1568 int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
1570 struct file_lock *file_lock = locks_alloc_lock();
1571 struct flock64 flock;
1572 struct inode *inode;
1575 if (file_lock == NULL)
1579 * This might block, so we do it before checking the inode.
1582 if (copy_from_user(&flock, l, sizeof(flock)))
1585 inode = filp->f_dentry->d_inode;
1587 /* Don't allow mandatory locks on files that may be memory mapped
1590 if (IS_MANDLOCK(inode) &&
1591 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
1592 mapping_writably_mapped(filp->f_mapping)) {
1597 error = flock64_to_posix_lock(filp, file_lock, &flock);
1600 if (cmd == F_SETLKW64) {
1601 file_lock->fl_flags |= FL_SLEEP;
1605 switch (flock.l_type) {
1607 if (!(filp->f_mode & FMODE_READ))
1611 if (!(filp->f_mode & FMODE_WRITE))
1621 error = security_file_lock(filp, file_lock->fl_type);
1625 if (filp->f_op && filp->f_op->lock != NULL) {
1626 error = filp->f_op->lock(filp, cmd, file_lock);
1632 error = __posix_lock_file(inode, file_lock);
1633 if ((error != -EAGAIN) || (cmd == F_SETLK64))
1635 error = wait_event_interruptible(file_lock->fl_wait,
1636 !file_lock->fl_next);
1640 locks_delete_block(file_lock);
1645 locks_free_lock(file_lock);
1648 #endif /* BITS_PER_LONG == 32 */
1651 * This function is called when the file is being removed
1652 * from the task's fd array. POSIX locks belonging to this task
1653 * are deleted at this time.
1655 void locks_remove_posix(struct file *filp, fl_owner_t owner)
1657 struct file_lock lock, **before;
1660 * If there are no locks held on this file, we don't need to call
1661 * posix_lock_file(). Another process could be setting a lock on this
1662 * file at the same time, but we wouldn't remove that lock anyway.
1664 before = &filp->f_dentry->d_inode->i_flock;
1665 if (*before == NULL)
1668 lock.fl_type = F_UNLCK;
1669 lock.fl_flags = FL_POSIX;
1671 lock.fl_end = OFFSET_MAX;
1672 lock.fl_owner = owner;
1673 lock.fl_pid = current->tgid;
1674 lock.fl_file = filp;
1676 if (filp->f_op && filp->f_op->lock != NULL) {
1677 filp->f_op->lock(filp, F_SETLK, &lock);
1678 /* Ignore any error -- we must remove the locks anyway */
1681 /* Can't use posix_lock_file here; we need to remove it no matter
1682 * which pid we have.
1685 while (*before != NULL) {
1686 struct file_lock *fl = *before;
1687 if (IS_POSIX(fl) && (fl->fl_owner == owner)) {
1688 locks_delete_lock(before);
1691 before = &fl->fl_next;
1696 EXPORT_SYMBOL(locks_remove_posix);
1699 * This function is called on the last close of an open file.
1701 void locks_remove_flock(struct file *filp)
1703 struct inode * inode = filp->f_dentry->d_inode;
1704 struct file_lock *fl;
1705 struct file_lock **before;
1707 if (!inode->i_flock)
1711 before = &inode->i_flock;
1713 while ((fl = *before) != NULL) {
1714 if (fl->fl_file == filp) {
1716 locks_delete_lock(before);
1720 lease_modify(before, F_UNLCK);
1723 /* FL_POSIX locks of this process have already been
1724 * removed in filp_close->locks_remove_posix.
1728 before = &fl->fl_next;
1734 * posix_block_lock - blocks waiting for a file lock
1735 * @blocker: the lock which is blocking
1736 * @waiter: the lock which conflicts and has to wait
1738 * lockd needs to block waiting for locks.
1741 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
1743 locks_insert_block(blocker, waiter);
1746 EXPORT_SYMBOL(posix_block_lock);
1749 * posix_unblock_lock - stop waiting for a file lock
1750 * @filp: how the file was opened
1751 * @waiter: the lock which was waiting
1753 * lockd needs to block waiting for locks.
1756 posix_unblock_lock(struct file *filp, struct file_lock *waiter)
1759 * A remote machine may cancel the lock request after it's been
1760 * granted locally. If that happens, we need to delete the lock.
1763 if (waiter->fl_next) {
1764 __locks_delete_block(waiter);
1768 waiter->fl_type = F_UNLCK;
1769 posix_lock_file(filp, waiter);
1773 EXPORT_SYMBOL(posix_unblock_lock);
1775 static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
1777 struct inode *inode = NULL;
1779 if (fl->fl_file != NULL)
1780 inode = fl->fl_file->f_dentry->d_inode;
1782 out += sprintf(out, "%d:%s ", id, pfx);
1784 out += sprintf(out, "%6s %s ",
1785 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1786 (inode == NULL) ? "*NOINODE*" :
1787 (IS_MANDLOCK(inode) &&
1788 (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1789 "MANDATORY" : "ADVISORY ");
1790 } else if (IS_FLOCK(fl)) {
1791 if (fl->fl_type & LOCK_MAND) {
1792 out += sprintf(out, "FLOCK MSNFS ");
1794 out += sprintf(out, "FLOCK ADVISORY ");
1796 } else if (IS_LEASE(fl)) {
1797 out += sprintf(out, "LEASE ");
1798 if (fl->fl_type & F_INPROGRESS)
1799 out += sprintf(out, "BREAKING ");
1800 else if (fl->fl_file)
1801 out += sprintf(out, "ACTIVE ");
1803 out += sprintf(out, "BREAKER ");
1805 out += sprintf(out, "UNKNOWN UNKNOWN ");
1807 if (fl->fl_type & LOCK_MAND) {
1808 out += sprintf(out, "%s ",
1809 (fl->fl_type & LOCK_READ)
1810 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
1811 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
1813 out += sprintf(out, "%s ",
1814 (fl->fl_type & F_INPROGRESS)
1815 ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
1816 : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
1819 #ifdef WE_CAN_BREAK_LSLK_NOW
1820 out += sprintf(out, "%d %s:%ld ", fl->fl_pid,
1821 inode->i_sb->s_id, inode->i_ino);
1823 /* userspace relies on this representation of dev_t ;-( */
1824 out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid,
1825 MAJOR(inode->i_sb->s_dev),
1826 MINOR(inode->i_sb->s_dev), inode->i_ino);
1829 out += sprintf(out, "%d <none>:0 ", fl->fl_pid);
1832 if (fl->fl_end == OFFSET_MAX)
1833 out += sprintf(out, "%Ld EOF\n", fl->fl_start);
1835 out += sprintf(out, "%Ld %Ld\n", fl->fl_start,
1838 out += sprintf(out, "0 EOF\n");
1842 static void move_lock_status(char **p, off_t* pos, off_t offset)
1846 if(*pos >= offset) {
1847 /* the complete line is valid */
1852 if(*pos+len > offset) {
1853 /* use the second part of the line */
1854 int i = offset-*pos;
1855 memmove(*p,*p+i,len-i);
1860 /* discard the complete line */
1865 * get_locks_status - reports lock usage in /proc/locks
1866 * @buffer: address in userspace to write into
1868 * @offset: how far we are through the buffer
1869 * @length: how much to read
1872 int get_locks_status(char *buffer, char **start, off_t offset, int length)
1874 struct list_head *tmp;
1880 list_for_each(tmp, &file_lock_list) {
1881 struct list_head *btmp;
1882 struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
1883 lock_get_status(q, fl, ++i, "");
1884 move_lock_status(&q, &pos, offset);
1886 if(pos >= offset+length)
1889 list_for_each(btmp, &fl->fl_block) {
1890 struct file_lock *bfl = list_entry(btmp,
1891 struct file_lock, fl_block);
1892 lock_get_status(q, bfl, i, " ->");
1893 move_lock_status(&q, &pos, offset);
1895 if(pos >= offset+length)
1902 if(q-buffer < length)
1908 * lock_may_read - checks that the region is free of locks
1909 * @inode: the inode that is being read
1910 * @start: the first byte to read
1911 * @len: the number of bytes to read
1913 * Emulates Windows locking requirements. Whole-file
1914 * mandatory locks (share modes) can prohibit a read and
1915 * byte-range POSIX locks can prohibit a read if they overlap.
1917 * N.B. this function is only ever called
1918 * from knfsd and ownership of locks is never checked.
1920 int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
1922 struct file_lock *fl;
1925 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1927 if (fl->fl_type == F_RDLCK)
1929 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
1931 } else if (IS_FLOCK(fl)) {
1932 if (!(fl->fl_type & LOCK_MAND))
1934 if (fl->fl_type & LOCK_READ)
1945 EXPORT_SYMBOL(lock_may_read);
1948 * lock_may_write - checks that the region is free of locks
1949 * @inode: the inode that is being written
1950 * @start: the first byte to write
1951 * @len: the number of bytes to write
1953 * Emulates Windows locking requirements. Whole-file
1954 * mandatory locks (share modes) can prohibit a write and
1955 * byte-range POSIX locks can prohibit a write if they overlap.
1957 * N.B. this function is only ever called
1958 * from knfsd and ownership of locks is never checked.
1960 int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
1962 struct file_lock *fl;
1965 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1967 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
1969 } else if (IS_FLOCK(fl)) {
1970 if (!(fl->fl_type & LOCK_MAND))
1972 if (fl->fl_type & LOCK_WRITE)
1983 EXPORT_SYMBOL(lock_may_write);
1985 static inline void __steal_locks(struct file *file, fl_owner_t from)
1987 struct inode *inode = file->f_dentry->d_inode;
1988 struct file_lock *fl = inode->i_flock;
1991 if (fl->fl_file == file && fl->fl_owner == from)
1992 fl->fl_owner = current->files;
1997 /* When getting ready for executing a binary, we make sure that current
1998 * has a files_struct on its own. Before dropping the old files_struct,
1999 * we take over ownership of all locks for all file descriptors we own.
2000 * Note that we may accidentally steal a lock for a file that a sibling
2001 * has created since the unshare_files() call.
2003 void steal_locks(fl_owner_t from)
2005 struct files_struct *files = current->files;
2016 if (i >= files->max_fdset || i >= files->max_fds)
2018 set = files->open_fds->fds_bits[j++];
2021 struct file *file = files->fd[i];
2023 __steal_locks(file, from);
2031 EXPORT_SYMBOL(steal_locks);
2033 static int __init filelock_init(void)
2035 filelock_cache = kmem_cache_create("file_lock_cache",
2036 sizeof(struct file_lock), 0, SLAB_PANIC,
2041 module_init(filelock_init)