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/filesystems/mandatory-locking.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/security.h>
123 #include <linux/slab.h>
124 #include <linux/syscalls.h>
125 #include <linux/time.h>
126 #include <linux/rcupdate.h>
127 #include <linux/pid_namespace.h>
128 #include <linux/hashtable.h>
129 #include <linux/percpu.h>
131 #define CREATE_TRACE_POINTS
132 #include <trace/events/filelock.h>
134 #include <linux/uaccess.h>
136 #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
137 #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
138 #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
139 #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
140 #define IS_REMOTELCK(fl) (fl->fl_pid <= 0)
142 static inline bool is_remote_lock(struct file *filp)
144 return likely(!(filp->f_path.dentry->d_sb->s_flags & SB_NOREMOTELOCK));
147 static bool lease_breaking(struct file_lock *fl)
149 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
152 static int target_leasetype(struct file_lock *fl)
154 if (fl->fl_flags & FL_UNLOCK_PENDING)
156 if (fl->fl_flags & FL_DOWNGRADE_PENDING)
161 int leases_enable = 1;
162 int lease_break_time = 45;
165 * The global file_lock_list is only used for displaying /proc/locks, so we
166 * keep a list on each CPU, with each list protected by its own spinlock.
167 * Global serialization is done using file_rwsem.
169 * Note that alterations to the list also require that the relevant flc_lock is
172 struct file_lock_list_struct {
174 struct hlist_head hlist;
176 static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
177 DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
180 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
181 * It is protected by blocked_lock_lock.
183 * We hash locks by lockowner in order to optimize searching for the lock a
184 * particular lockowner is waiting on.
186 * FIXME: make this value scale via some heuristic? We generally will want more
187 * buckets when we have more lockowners holding locks, but that's a little
188 * difficult to determine without knowing what the workload will look like.
190 #define BLOCKED_HASH_BITS 7
191 static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
194 * This lock protects the blocked_hash. Generally, if you're accessing it, you
195 * want to be holding this lock.
197 * In addition, it also protects the fl->fl_block list, and the fl->fl_next
198 * pointer for file_lock structures that are acting as lock requests (in
199 * contrast to those that are acting as records of acquired locks).
201 * Note that when we acquire this lock in order to change the above fields,
202 * we often hold the flc_lock as well. In certain cases, when reading the fields
203 * protected by this lock, we can skip acquiring it iff we already hold the
206 static DEFINE_SPINLOCK(blocked_lock_lock);
208 static struct kmem_cache *flctx_cache __read_mostly;
209 static struct kmem_cache *filelock_cache __read_mostly;
211 static struct file_lock_context *
212 locks_get_lock_context(struct inode *inode, int type)
214 struct file_lock_context *ctx;
216 /* paired with cmpxchg() below */
217 ctx = smp_load_acquire(&inode->i_flctx);
218 if (likely(ctx) || type == F_UNLCK)
221 ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
225 spin_lock_init(&ctx->flc_lock);
226 INIT_LIST_HEAD(&ctx->flc_flock);
227 INIT_LIST_HEAD(&ctx->flc_posix);
228 INIT_LIST_HEAD(&ctx->flc_lease);
231 * Assign the pointer if it's not already assigned. If it is, then
232 * free the context we just allocated.
234 if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
235 kmem_cache_free(flctx_cache, ctx);
236 ctx = smp_load_acquire(&inode->i_flctx);
239 trace_locks_get_lock_context(inode, type, ctx);
244 locks_dump_ctx_list(struct list_head *list, char *list_type)
246 struct file_lock *fl;
248 list_for_each_entry(fl, list, fl_list) {
249 pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
254 locks_check_ctx_lists(struct inode *inode)
256 struct file_lock_context *ctx = inode->i_flctx;
258 if (unlikely(!list_empty(&ctx->flc_flock) ||
259 !list_empty(&ctx->flc_posix) ||
260 !list_empty(&ctx->flc_lease))) {
261 pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
262 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
264 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
265 locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
266 locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
271 locks_check_ctx_file_list(struct file *filp, struct list_head *list,
274 struct file_lock *fl;
275 struct inode *inode = locks_inode(filp);
277 list_for_each_entry(fl, list, fl_list)
278 if (fl->fl_file == filp)
279 pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
280 " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
281 list_type, MAJOR(inode->i_sb->s_dev),
282 MINOR(inode->i_sb->s_dev), inode->i_ino,
283 fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
287 locks_free_lock_context(struct inode *inode)
289 struct file_lock_context *ctx = inode->i_flctx;
292 locks_check_ctx_lists(inode);
293 kmem_cache_free(flctx_cache, ctx);
297 static void locks_init_lock_heads(struct file_lock *fl)
299 INIT_HLIST_NODE(&fl->fl_link);
300 INIT_LIST_HEAD(&fl->fl_list);
301 INIT_LIST_HEAD(&fl->fl_block);
302 init_waitqueue_head(&fl->fl_wait);
305 /* Allocate an empty lock structure. */
306 struct file_lock *locks_alloc_lock(void)
308 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
311 locks_init_lock_heads(fl);
315 EXPORT_SYMBOL_GPL(locks_alloc_lock);
317 void locks_release_private(struct file_lock *fl)
320 if (fl->fl_ops->fl_release_private)
321 fl->fl_ops->fl_release_private(fl);
326 if (fl->fl_lmops->lm_put_owner) {
327 fl->fl_lmops->lm_put_owner(fl->fl_owner);
333 EXPORT_SYMBOL_GPL(locks_release_private);
335 /* Free a lock which is not in use. */
336 void locks_free_lock(struct file_lock *fl)
338 BUG_ON(waitqueue_active(&fl->fl_wait));
339 BUG_ON(!list_empty(&fl->fl_list));
340 BUG_ON(!list_empty(&fl->fl_block));
341 BUG_ON(!hlist_unhashed(&fl->fl_link));
343 locks_release_private(fl);
344 kmem_cache_free(filelock_cache, fl);
346 EXPORT_SYMBOL(locks_free_lock);
349 locks_dispose_list(struct list_head *dispose)
351 struct file_lock *fl;
353 while (!list_empty(dispose)) {
354 fl = list_first_entry(dispose, struct file_lock, fl_list);
355 list_del_init(&fl->fl_list);
360 void locks_init_lock(struct file_lock *fl)
362 memset(fl, 0, sizeof(struct file_lock));
363 locks_init_lock_heads(fl);
366 EXPORT_SYMBOL(locks_init_lock);
369 * Initialize a new lock from an existing file_lock structure.
371 void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
373 new->fl_owner = fl->fl_owner;
374 new->fl_pid = fl->fl_pid;
376 new->fl_flags = fl->fl_flags;
377 new->fl_type = fl->fl_type;
378 new->fl_start = fl->fl_start;
379 new->fl_end = fl->fl_end;
380 new->fl_lmops = fl->fl_lmops;
384 if (fl->fl_lmops->lm_get_owner)
385 fl->fl_lmops->lm_get_owner(fl->fl_owner);
388 EXPORT_SYMBOL(locks_copy_conflock);
390 void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
392 /* "new" must be a freshly-initialized lock */
393 WARN_ON_ONCE(new->fl_ops);
395 locks_copy_conflock(new, fl);
397 new->fl_file = fl->fl_file;
398 new->fl_ops = fl->fl_ops;
401 if (fl->fl_ops->fl_copy_lock)
402 fl->fl_ops->fl_copy_lock(new, fl);
406 EXPORT_SYMBOL(locks_copy_lock);
408 static inline int flock_translate_cmd(int cmd) {
410 return cmd & (LOCK_MAND | LOCK_RW);
422 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
423 static struct file_lock *
424 flock_make_lock(struct file *filp, unsigned int cmd)
426 struct file_lock *fl;
427 int type = flock_translate_cmd(cmd);
430 return ERR_PTR(type);
432 fl = locks_alloc_lock();
434 return ERR_PTR(-ENOMEM);
438 fl->fl_pid = current->tgid;
439 fl->fl_flags = FL_FLOCK;
441 fl->fl_end = OFFSET_MAX;
446 static int assign_type(struct file_lock *fl, long type)
460 static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
463 switch (l->l_whence) {
468 fl->fl_start = filp->f_pos;
471 fl->fl_start = i_size_read(file_inode(filp));
476 if (l->l_start > OFFSET_MAX - fl->fl_start)
478 fl->fl_start += l->l_start;
479 if (fl->fl_start < 0)
482 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
483 POSIX-2001 defines it. */
485 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
487 fl->fl_end = fl->fl_start + l->l_len - 1;
489 } else if (l->l_len < 0) {
490 if (fl->fl_start + l->l_len < 0)
492 fl->fl_end = fl->fl_start - 1;
493 fl->fl_start += l->l_len;
495 fl->fl_end = OFFSET_MAX;
497 fl->fl_owner = current->files;
498 fl->fl_pid = current->tgid;
500 fl->fl_flags = FL_POSIX;
504 return assign_type(fl, l->l_type);
507 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
510 static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
513 struct flock64 ll = {
515 .l_whence = l->l_whence,
516 .l_start = l->l_start,
520 return flock64_to_posix_lock(filp, fl, &ll);
523 /* default lease lock manager operations */
525 lease_break_callback(struct file_lock *fl)
527 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
532 lease_setup(struct file_lock *fl, void **priv)
534 struct file *filp = fl->fl_file;
535 struct fasync_struct *fa = *priv;
538 * fasync_insert_entry() returns the old entry if any. If there was no
539 * old entry, then it used "priv" and inserted it into the fasync list.
540 * Clear the pointer to indicate that it shouldn't be freed.
542 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
545 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
548 static const struct lock_manager_operations lease_manager_ops = {
549 .lm_break = lease_break_callback,
550 .lm_change = lease_modify,
551 .lm_setup = lease_setup,
555 * Initialize a lease, use the default lock manager operations
557 static int lease_init(struct file *filp, long type, struct file_lock *fl)
559 if (assign_type(fl, type) != 0)
563 fl->fl_pid = current->tgid;
566 fl->fl_flags = FL_LEASE;
568 fl->fl_end = OFFSET_MAX;
570 fl->fl_lmops = &lease_manager_ops;
574 /* Allocate a file_lock initialised to this type of lease */
575 static struct file_lock *lease_alloc(struct file *filp, long type)
577 struct file_lock *fl = locks_alloc_lock();
581 return ERR_PTR(error);
583 error = lease_init(filp, type, fl);
586 return ERR_PTR(error);
591 /* Check if two locks overlap each other.
593 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
595 return ((fl1->fl_end >= fl2->fl_start) &&
596 (fl2->fl_end >= fl1->fl_start));
600 * Check whether two locks have the same owner.
602 static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
604 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
605 return fl2->fl_lmops == fl1->fl_lmops &&
606 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
607 return fl1->fl_owner == fl2->fl_owner;
610 /* Must be called with the flc_lock held! */
611 static void locks_insert_global_locks(struct file_lock *fl)
613 struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
615 percpu_rwsem_assert_held(&file_rwsem);
617 spin_lock(&fll->lock);
618 fl->fl_link_cpu = smp_processor_id();
619 hlist_add_head(&fl->fl_link, &fll->hlist);
620 spin_unlock(&fll->lock);
623 /* Must be called with the flc_lock held! */
624 static void locks_delete_global_locks(struct file_lock *fl)
626 struct file_lock_list_struct *fll;
628 percpu_rwsem_assert_held(&file_rwsem);
631 * Avoid taking lock if already unhashed. This is safe since this check
632 * is done while holding the flc_lock, and new insertions into the list
633 * also require that it be held.
635 if (hlist_unhashed(&fl->fl_link))
638 fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
639 spin_lock(&fll->lock);
640 hlist_del_init(&fl->fl_link);
641 spin_unlock(&fll->lock);
645 posix_owner_key(struct file_lock *fl)
647 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
648 return fl->fl_lmops->lm_owner_key(fl);
649 return (unsigned long)fl->fl_owner;
652 static void locks_insert_global_blocked(struct file_lock *waiter)
654 lockdep_assert_held(&blocked_lock_lock);
656 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
659 static void locks_delete_global_blocked(struct file_lock *waiter)
661 lockdep_assert_held(&blocked_lock_lock);
663 hash_del(&waiter->fl_link);
666 /* Remove waiter from blocker's block list.
667 * When blocker ends up pointing to itself then the list is empty.
669 * Must be called with blocked_lock_lock held.
671 static void __locks_delete_block(struct file_lock *waiter)
673 locks_delete_global_blocked(waiter);
674 list_del_init(&waiter->fl_block);
675 waiter->fl_next = NULL;
678 static void locks_delete_block(struct file_lock *waiter)
680 spin_lock(&blocked_lock_lock);
681 __locks_delete_block(waiter);
682 spin_unlock(&blocked_lock_lock);
685 /* Insert waiter into blocker's block list.
686 * We use a circular list so that processes can be easily woken up in
687 * the order they blocked. The documentation doesn't require this but
688 * it seems like the reasonable thing to do.
690 * Must be called with both the flc_lock and blocked_lock_lock held. The
691 * fl_block list itself is protected by the blocked_lock_lock, but by ensuring
692 * that the flc_lock is also held on insertions we can avoid taking the
693 * blocked_lock_lock in some cases when we see that the fl_block list is empty.
695 static void __locks_insert_block(struct file_lock *blocker,
696 struct file_lock *waiter)
698 BUG_ON(!list_empty(&waiter->fl_block));
699 waiter->fl_next = blocker;
700 list_add_tail(&waiter->fl_block, &blocker->fl_block);
701 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
702 locks_insert_global_blocked(waiter);
705 /* Must be called with flc_lock held. */
706 static void locks_insert_block(struct file_lock *blocker,
707 struct file_lock *waiter)
709 spin_lock(&blocked_lock_lock);
710 __locks_insert_block(blocker, waiter);
711 spin_unlock(&blocked_lock_lock);
715 * Wake up processes blocked waiting for blocker.
717 * Must be called with the inode->flc_lock held!
719 static void locks_wake_up_blocks(struct file_lock *blocker)
722 * Avoid taking global lock if list is empty. This is safe since new
723 * blocked requests are only added to the list under the flc_lock, and
724 * the flc_lock is always held here. Note that removal from the fl_block
725 * list does not require the flc_lock, so we must recheck list_empty()
726 * after acquiring the blocked_lock_lock.
728 if (list_empty(&blocker->fl_block))
731 spin_lock(&blocked_lock_lock);
732 while (!list_empty(&blocker->fl_block)) {
733 struct file_lock *waiter;
735 waiter = list_first_entry(&blocker->fl_block,
736 struct file_lock, fl_block);
737 __locks_delete_block(waiter);
738 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
739 waiter->fl_lmops->lm_notify(waiter);
741 wake_up(&waiter->fl_wait);
743 spin_unlock(&blocked_lock_lock);
747 locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
749 list_add_tail(&fl->fl_list, before);
750 locks_insert_global_locks(fl);
754 locks_unlink_lock_ctx(struct file_lock *fl)
756 locks_delete_global_locks(fl);
757 list_del_init(&fl->fl_list);
758 locks_wake_up_blocks(fl);
762 locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
764 locks_unlink_lock_ctx(fl);
766 list_add(&fl->fl_list, dispose);
771 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
772 * checks for shared/exclusive status of overlapping locks.
774 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
776 if (sys_fl->fl_type == F_WRLCK)
778 if (caller_fl->fl_type == F_WRLCK)
783 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
784 * checking before calling the locks_conflict().
786 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
788 /* POSIX locks owned by the same process do not conflict with
791 if (posix_same_owner(caller_fl, sys_fl))
794 /* Check whether they overlap */
795 if (!locks_overlap(caller_fl, sys_fl))
798 return (locks_conflict(caller_fl, sys_fl));
801 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
802 * checking before calling the locks_conflict().
804 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
806 /* FLOCK locks referring to the same filp do not conflict with
809 if (caller_fl->fl_file == sys_fl->fl_file)
811 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
814 return (locks_conflict(caller_fl, sys_fl));
818 posix_test_lock(struct file *filp, struct file_lock *fl)
820 struct file_lock *cfl;
821 struct file_lock_context *ctx;
822 struct inode *inode = locks_inode(filp);
824 ctx = smp_load_acquire(&inode->i_flctx);
825 if (!ctx || list_empty_careful(&ctx->flc_posix)) {
826 fl->fl_type = F_UNLCK;
830 spin_lock(&ctx->flc_lock);
831 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
832 if (posix_locks_conflict(fl, cfl)) {
833 locks_copy_conflock(fl, cfl);
837 fl->fl_type = F_UNLCK;
839 spin_unlock(&ctx->flc_lock);
842 EXPORT_SYMBOL(posix_test_lock);
845 * Deadlock detection:
847 * We attempt to detect deadlocks that are due purely to posix file
850 * We assume that a task can be waiting for at most one lock at a time.
851 * So for any acquired lock, the process holding that lock may be
852 * waiting on at most one other lock. That lock in turns may be held by
853 * someone waiting for at most one other lock. Given a requested lock
854 * caller_fl which is about to wait for a conflicting lock block_fl, we
855 * follow this chain of waiters to ensure we are not about to create a
858 * Since we do this before we ever put a process to sleep on a lock, we
859 * are ensured that there is never a cycle; that is what guarantees that
860 * the while() loop in posix_locks_deadlock() eventually completes.
862 * Note: the above assumption may not be true when handling lock
863 * requests from a broken NFS client. It may also fail in the presence
864 * of tasks (such as posix threads) sharing the same open file table.
865 * To handle those cases, we just bail out after a few iterations.
867 * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
868 * Because the owner is not even nominally tied to a thread of
869 * execution, the deadlock detection below can't reasonably work well. Just
872 * In principle, we could do a more limited deadlock detection on FL_OFDLCK
873 * locks that just checks for the case where two tasks are attempting to
874 * upgrade from read to write locks on the same inode.
877 #define MAX_DEADLK_ITERATIONS 10
879 /* Find a lock that the owner of the given block_fl is blocking on. */
880 static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
882 struct file_lock *fl;
884 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
885 if (posix_same_owner(fl, block_fl))
891 /* Must be called with the blocked_lock_lock held! */
892 static int posix_locks_deadlock(struct file_lock *caller_fl,
893 struct file_lock *block_fl)
897 lockdep_assert_held(&blocked_lock_lock);
900 * This deadlock detector can't reasonably detect deadlocks with
901 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
903 if (IS_OFDLCK(caller_fl))
906 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
907 if (i++ > MAX_DEADLK_ITERATIONS)
909 if (posix_same_owner(caller_fl, block_fl))
915 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
916 * after any leases, but before any posix locks.
918 * Note that if called with an FL_EXISTS argument, the caller may determine
919 * whether or not a lock was successfully freed by testing the return
922 static int flock_lock_inode(struct inode *inode, struct file_lock *request)
924 struct file_lock *new_fl = NULL;
925 struct file_lock *fl;
926 struct file_lock_context *ctx;
931 ctx = locks_get_lock_context(inode, request->fl_type);
933 if (request->fl_type != F_UNLCK)
935 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
938 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
939 new_fl = locks_alloc_lock();
944 percpu_down_read_preempt_disable(&file_rwsem);
945 spin_lock(&ctx->flc_lock);
946 if (request->fl_flags & FL_ACCESS)
949 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
950 if (request->fl_file != fl->fl_file)
952 if (request->fl_type == fl->fl_type)
955 locks_delete_lock_ctx(fl, &dispose);
959 if (request->fl_type == F_UNLCK) {
960 if ((request->fl_flags & FL_EXISTS) && !found)
966 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
967 if (!flock_locks_conflict(request, fl))
970 if (!(request->fl_flags & FL_SLEEP))
972 error = FILE_LOCK_DEFERRED;
973 locks_insert_block(fl, request);
976 if (request->fl_flags & FL_ACCESS)
978 locks_copy_lock(new_fl, request);
979 locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
984 spin_unlock(&ctx->flc_lock);
985 percpu_up_read_preempt_enable(&file_rwsem);
987 locks_free_lock(new_fl);
988 locks_dispose_list(&dispose);
989 trace_flock_lock_inode(inode, request, error);
993 static int posix_lock_inode(struct inode *inode, struct file_lock *request,
994 struct file_lock *conflock)
996 struct file_lock *fl, *tmp;
997 struct file_lock *new_fl = NULL;
998 struct file_lock *new_fl2 = NULL;
999 struct file_lock *left = NULL;
1000 struct file_lock *right = NULL;
1001 struct file_lock_context *ctx;
1006 ctx = locks_get_lock_context(inode, request->fl_type);
1008 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
1011 * We may need two file_lock structures for this operation,
1012 * so we get them in advance to avoid races.
1014 * In some cases we can be sure, that no new locks will be needed
1016 if (!(request->fl_flags & FL_ACCESS) &&
1017 (request->fl_type != F_UNLCK ||
1018 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
1019 new_fl = locks_alloc_lock();
1020 new_fl2 = locks_alloc_lock();
1023 percpu_down_read_preempt_disable(&file_rwsem);
1024 spin_lock(&ctx->flc_lock);
1026 * New lock request. Walk all POSIX locks and look for conflicts. If
1027 * there are any, either return error or put the request on the
1028 * blocker's list of waiters and the global blocked_hash.
1030 if (request->fl_type != F_UNLCK) {
1031 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1032 if (!posix_locks_conflict(request, fl))
1035 locks_copy_conflock(conflock, fl);
1037 if (!(request->fl_flags & FL_SLEEP))
1040 * Deadlock detection and insertion into the blocked
1041 * locks list must be done while holding the same lock!
1044 spin_lock(&blocked_lock_lock);
1045 if (likely(!posix_locks_deadlock(request, fl))) {
1046 error = FILE_LOCK_DEFERRED;
1047 __locks_insert_block(fl, request);
1049 spin_unlock(&blocked_lock_lock);
1054 /* If we're just looking for a conflict, we're done. */
1056 if (request->fl_flags & FL_ACCESS)
1059 /* Find the first old lock with the same owner as the new lock */
1060 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1061 if (posix_same_owner(request, fl))
1065 /* Process locks with this owner. */
1066 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1067 if (!posix_same_owner(request, fl))
1070 /* Detect adjacent or overlapping regions (if same lock type) */
1071 if (request->fl_type == fl->fl_type) {
1072 /* In all comparisons of start vs end, use
1073 * "start - 1" rather than "end + 1". If end
1074 * is OFFSET_MAX, end + 1 will become negative.
1076 if (fl->fl_end < request->fl_start - 1)
1078 /* If the next lock in the list has entirely bigger
1079 * addresses than the new one, insert the lock here.
1081 if (fl->fl_start - 1 > request->fl_end)
1084 /* If we come here, the new and old lock are of the
1085 * same type and adjacent or overlapping. Make one
1086 * lock yielding from the lower start address of both
1087 * locks to the higher end address.
1089 if (fl->fl_start > request->fl_start)
1090 fl->fl_start = request->fl_start;
1092 request->fl_start = fl->fl_start;
1093 if (fl->fl_end < request->fl_end)
1094 fl->fl_end = request->fl_end;
1096 request->fl_end = fl->fl_end;
1098 locks_delete_lock_ctx(fl, &dispose);
1104 /* Processing for different lock types is a bit
1107 if (fl->fl_end < request->fl_start)
1109 if (fl->fl_start > request->fl_end)
1111 if (request->fl_type == F_UNLCK)
1113 if (fl->fl_start < request->fl_start)
1115 /* If the next lock in the list has a higher end
1116 * address than the new one, insert the new one here.
1118 if (fl->fl_end > request->fl_end) {
1122 if (fl->fl_start >= request->fl_start) {
1123 /* The new lock completely replaces an old
1124 * one (This may happen several times).
1127 locks_delete_lock_ctx(fl, &dispose);
1131 * Replace the old lock with new_fl, and
1132 * remove the old one. It's safe to do the
1133 * insert here since we know that we won't be
1134 * using new_fl later, and that the lock is
1135 * just replacing an existing lock.
1140 locks_copy_lock(new_fl, request);
1143 locks_insert_lock_ctx(request, &fl->fl_list);
1144 locks_delete_lock_ctx(fl, &dispose);
1151 * The above code only modifies existing locks in case of merging or
1152 * replacing. If new lock(s) need to be inserted all modifications are
1153 * done below this, so it's safe yet to bail out.
1155 error = -ENOLCK; /* "no luck" */
1156 if (right && left == right && !new_fl2)
1161 if (request->fl_type == F_UNLCK) {
1162 if (request->fl_flags & FL_EXISTS)
1171 locks_copy_lock(new_fl, request);
1172 locks_insert_lock_ctx(new_fl, &fl->fl_list);
1177 if (left == right) {
1178 /* The new lock breaks the old one in two pieces,
1179 * so we have to use the second new lock.
1183 locks_copy_lock(left, right);
1184 locks_insert_lock_ctx(left, &fl->fl_list);
1186 right->fl_start = request->fl_end + 1;
1187 locks_wake_up_blocks(right);
1190 left->fl_end = request->fl_start - 1;
1191 locks_wake_up_blocks(left);
1194 spin_unlock(&ctx->flc_lock);
1195 percpu_up_read_preempt_enable(&file_rwsem);
1197 * Free any unused locks.
1200 locks_free_lock(new_fl);
1202 locks_free_lock(new_fl2);
1203 locks_dispose_list(&dispose);
1204 trace_posix_lock_inode(inode, request, error);
1210 * posix_lock_file - Apply a POSIX-style lock to a file
1211 * @filp: The file to apply the lock to
1212 * @fl: The lock to be applied
1213 * @conflock: Place to return a copy of the conflicting lock, if found.
1215 * Add a POSIX style lock to a file.
1216 * We merge adjacent & overlapping locks whenever possible.
1217 * POSIX locks are sorted by owner task, then by starting address
1219 * Note that if called with an FL_EXISTS argument, the caller may determine
1220 * whether or not a lock was successfully freed by testing the return
1221 * value for -ENOENT.
1223 int posix_lock_file(struct file *filp, struct file_lock *fl,
1224 struct file_lock *conflock)
1226 return posix_lock_inode(locks_inode(filp), fl, conflock);
1228 EXPORT_SYMBOL(posix_lock_file);
1231 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1232 * @inode: inode of file to which lock request should be applied
1233 * @fl: The lock to be applied
1235 * Apply a POSIX style lock request to an inode.
1237 static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1242 error = posix_lock_inode(inode, fl, NULL);
1243 if (error != FILE_LOCK_DEFERRED)
1245 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1249 locks_delete_block(fl);
1255 #ifdef CONFIG_MANDATORY_FILE_LOCKING
1257 * locks_mandatory_locked - Check for an active lock
1258 * @file: the file to check
1260 * Searches the inode's list of locks to find any POSIX locks which conflict.
1261 * This function is called from locks_verify_locked() only.
1263 int locks_mandatory_locked(struct file *file)
1266 struct inode *inode = locks_inode(file);
1267 struct file_lock_context *ctx;
1268 struct file_lock *fl;
1270 ctx = smp_load_acquire(&inode->i_flctx);
1271 if (!ctx || list_empty_careful(&ctx->flc_posix))
1275 * Search the lock list for this inode for any POSIX locks.
1277 spin_lock(&ctx->flc_lock);
1279 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1280 if (fl->fl_owner != current->files &&
1281 fl->fl_owner != file) {
1286 spin_unlock(&ctx->flc_lock);
1291 * locks_mandatory_area - Check for a conflicting lock
1292 * @inode: the file to check
1293 * @filp: how the file was opened (if it was)
1294 * @start: first byte in the file to check
1295 * @end: lastbyte in the file to check
1296 * @type: %F_WRLCK for a write lock, else %F_RDLCK
1298 * Searches the inode's list of locks to find any POSIX locks which conflict.
1300 int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
1301 loff_t end, unsigned char type)
1303 struct file_lock fl;
1307 locks_init_lock(&fl);
1308 fl.fl_pid = current->tgid;
1310 fl.fl_flags = FL_POSIX | FL_ACCESS;
1311 if (filp && !(filp->f_flags & O_NONBLOCK))
1314 fl.fl_start = start;
1320 fl.fl_flags &= ~FL_SLEEP;
1321 error = posix_lock_inode(inode, &fl, NULL);
1327 fl.fl_flags |= FL_SLEEP;
1328 fl.fl_owner = current->files;
1329 error = posix_lock_inode(inode, &fl, NULL);
1330 if (error != FILE_LOCK_DEFERRED)
1332 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1335 * If we've been sleeping someone might have
1336 * changed the permissions behind our back.
1338 if (__mandatory_lock(inode))
1342 locks_delete_block(&fl);
1349 EXPORT_SYMBOL(locks_mandatory_area);
1350 #endif /* CONFIG_MANDATORY_FILE_LOCKING */
1352 static void lease_clear_pending(struct file_lock *fl, int arg)
1356 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1359 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1363 /* We already had a lease on this file; just change its type */
1364 int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
1366 int error = assign_type(fl, arg);
1370 lease_clear_pending(fl, arg);
1371 locks_wake_up_blocks(fl);
1372 if (arg == F_UNLCK) {
1373 struct file *filp = fl->fl_file;
1376 filp->f_owner.signum = 0;
1377 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1378 if (fl->fl_fasync != NULL) {
1379 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1380 fl->fl_fasync = NULL;
1382 locks_delete_lock_ctx(fl, dispose);
1386 EXPORT_SYMBOL(lease_modify);
1388 static bool past_time(unsigned long then)
1391 /* 0 is a special value meaning "this never expires": */
1393 return time_after(jiffies, then);
1396 static void time_out_leases(struct inode *inode, struct list_head *dispose)
1398 struct file_lock_context *ctx = inode->i_flctx;
1399 struct file_lock *fl, *tmp;
1401 lockdep_assert_held(&ctx->flc_lock);
1403 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1404 trace_time_out_leases(inode, fl);
1405 if (past_time(fl->fl_downgrade_time))
1406 lease_modify(fl, F_RDLCK, dispose);
1407 if (past_time(fl->fl_break_time))
1408 lease_modify(fl, F_UNLCK, dispose);
1412 static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1414 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
1416 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1418 return locks_conflict(breaker, lease);
1422 any_leases_conflict(struct inode *inode, struct file_lock *breaker)
1424 struct file_lock_context *ctx = inode->i_flctx;
1425 struct file_lock *fl;
1427 lockdep_assert_held(&ctx->flc_lock);
1429 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1430 if (leases_conflict(fl, breaker))
1437 * __break_lease - revoke all outstanding leases on file
1438 * @inode: the inode of the file to return
1439 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1441 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
1444 * break_lease (inlined for speed) has checked there already is at least
1445 * some kind of lock (maybe a lease) on this file. Leases are broken on
1446 * a call to open() or truncate(). This function can sleep unless you
1447 * specified %O_NONBLOCK to your open().
1449 int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1452 struct file_lock_context *ctx;
1453 struct file_lock *new_fl, *fl, *tmp;
1454 unsigned long break_time;
1455 int want_write = (mode & O_ACCMODE) != O_RDONLY;
1458 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
1460 return PTR_ERR(new_fl);
1461 new_fl->fl_flags = type;
1463 /* typically we will check that ctx is non-NULL before calling */
1464 ctx = smp_load_acquire(&inode->i_flctx);
1470 percpu_down_read_preempt_disable(&file_rwsem);
1471 spin_lock(&ctx->flc_lock);
1473 time_out_leases(inode, &dispose);
1475 if (!any_leases_conflict(inode, new_fl))
1479 if (lease_break_time > 0) {
1480 break_time = jiffies + lease_break_time * HZ;
1481 if (break_time == 0)
1482 break_time++; /* so that 0 means no break time */
1485 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1486 if (!leases_conflict(fl, new_fl))
1489 if (fl->fl_flags & FL_UNLOCK_PENDING)
1491 fl->fl_flags |= FL_UNLOCK_PENDING;
1492 fl->fl_break_time = break_time;
1494 if (lease_breaking(fl))
1496 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1497 fl->fl_downgrade_time = break_time;
1499 if (fl->fl_lmops->lm_break(fl))
1500 locks_delete_lock_ctx(fl, &dispose);
1503 if (list_empty(&ctx->flc_lease))
1506 if (mode & O_NONBLOCK) {
1507 trace_break_lease_noblock(inode, new_fl);
1508 error = -EWOULDBLOCK;
1513 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
1514 break_time = fl->fl_break_time;
1515 if (break_time != 0)
1516 break_time -= jiffies;
1517 if (break_time == 0)
1519 locks_insert_block(fl, new_fl);
1520 trace_break_lease_block(inode, new_fl);
1521 spin_unlock(&ctx->flc_lock);
1522 percpu_up_read_preempt_enable(&file_rwsem);
1524 locks_dispose_list(&dispose);
1525 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1526 !new_fl->fl_next, break_time);
1528 percpu_down_read_preempt_disable(&file_rwsem);
1529 spin_lock(&ctx->flc_lock);
1530 trace_break_lease_unblock(inode, new_fl);
1531 locks_delete_block(new_fl);
1534 * Wait for the next conflicting lease that has not been
1538 time_out_leases(inode, &dispose);
1539 if (any_leases_conflict(inode, new_fl))
1544 spin_unlock(&ctx->flc_lock);
1545 percpu_up_read_preempt_enable(&file_rwsem);
1546 locks_dispose_list(&dispose);
1547 locks_free_lock(new_fl);
1551 EXPORT_SYMBOL(__break_lease);
1554 * lease_get_mtime - update modified time of an inode with exclusive lease
1556 * @time: pointer to a timespec which contains the last modified time
1558 * This is to force NFS clients to flush their caches for files with
1559 * exclusive leases. The justification is that if someone has an
1560 * exclusive lease, then they could be modifying it.
1562 void lease_get_mtime(struct inode *inode, struct timespec64 *time)
1564 bool has_lease = false;
1565 struct file_lock_context *ctx;
1566 struct file_lock *fl;
1568 ctx = smp_load_acquire(&inode->i_flctx);
1569 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
1570 spin_lock(&ctx->flc_lock);
1571 fl = list_first_entry_or_null(&ctx->flc_lease,
1572 struct file_lock, fl_list);
1573 if (fl && (fl->fl_type == F_WRLCK))
1575 spin_unlock(&ctx->flc_lock);
1579 *time = current_time(inode);
1582 EXPORT_SYMBOL(lease_get_mtime);
1585 * fcntl_getlease - Enquire what lease is currently active
1588 * The value returned by this function will be one of
1589 * (if no lease break is pending):
1591 * %F_RDLCK to indicate a shared lease is held.
1593 * %F_WRLCK to indicate an exclusive lease is held.
1595 * %F_UNLCK to indicate no lease is held.
1597 * (if a lease break is pending):
1599 * %F_RDLCK to indicate an exclusive lease needs to be
1600 * changed to a shared lease (or removed).
1602 * %F_UNLCK to indicate the lease needs to be removed.
1604 * XXX: sfr & willy disagree over whether F_INPROGRESS
1605 * should be returned to userspace.
1607 int fcntl_getlease(struct file *filp)
1609 struct file_lock *fl;
1610 struct inode *inode = locks_inode(filp);
1611 struct file_lock_context *ctx;
1615 ctx = smp_load_acquire(&inode->i_flctx);
1616 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
1617 percpu_down_read_preempt_disable(&file_rwsem);
1618 spin_lock(&ctx->flc_lock);
1619 time_out_leases(inode, &dispose);
1620 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1621 if (fl->fl_file != filp)
1623 type = target_leasetype(fl);
1626 spin_unlock(&ctx->flc_lock);
1627 percpu_up_read_preempt_enable(&file_rwsem);
1629 locks_dispose_list(&dispose);
1635 * check_conflicting_open - see if the given dentry points to a file that has
1636 * an existing open that would conflict with the
1638 * @dentry: dentry to check
1639 * @arg: type of lease that we're trying to acquire
1640 * @flags: current lock flags
1642 * Check to see if there's an existing open fd on this file that would
1643 * conflict with the lease we're trying to set.
1646 check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
1649 struct inode *inode = dentry->d_inode;
1651 if (flags & FL_LAYOUT)
1654 if ((arg == F_RDLCK) &&
1655 (atomic_read(&d_real_inode(dentry)->i_writecount) > 0))
1658 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
1659 (atomic_read(&inode->i_count) > 1)))
1666 generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
1668 struct file_lock *fl, *my_fl = NULL, *lease;
1669 struct dentry *dentry = filp->f_path.dentry;
1670 struct inode *inode = dentry->d_inode;
1671 struct file_lock_context *ctx;
1672 bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1677 trace_generic_add_lease(inode, lease);
1679 /* Note that arg is never F_UNLCK here */
1680 ctx = locks_get_lock_context(inode, arg);
1685 * In the delegation case we need mutual exclusion with
1686 * a number of operations that take the i_mutex. We trylock
1687 * because delegations are an optional optimization, and if
1688 * there's some chance of a conflict--we'd rather not
1689 * bother, maybe that's a sign this just isn't a good file to
1690 * hand out a delegation on.
1692 if (is_deleg && !inode_trylock(inode))
1695 if (is_deleg && arg == F_WRLCK) {
1696 /* Write delegations are not currently supported: */
1697 inode_unlock(inode);
1702 percpu_down_read_preempt_disable(&file_rwsem);
1703 spin_lock(&ctx->flc_lock);
1704 time_out_leases(inode, &dispose);
1705 error = check_conflicting_open(dentry, arg, lease->fl_flags);
1710 * At this point, we know that if there is an exclusive
1711 * lease on this file, then we hold it on this filp
1712 * (otherwise our open of this file would have blocked).
1713 * And if we are trying to acquire an exclusive lease,
1714 * then the file is not open by anyone (including us)
1715 * except for this filp.
1718 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1719 if (fl->fl_file == filp &&
1720 fl->fl_owner == lease->fl_owner) {
1726 * No exclusive leases if someone else has a lease on
1732 * Modifying our existing lease is OK, but no getting a
1733 * new lease if someone else is opening for write:
1735 if (fl->fl_flags & FL_UNLOCK_PENDING)
1739 if (my_fl != NULL) {
1741 error = lease->fl_lmops->lm_change(lease, arg, &dispose);
1751 locks_insert_lock_ctx(lease, &ctx->flc_lease);
1753 * The check in break_lease() is lockless. It's possible for another
1754 * open to race in after we did the earlier check for a conflicting
1755 * open but before the lease was inserted. Check again for a
1756 * conflicting open and cancel the lease if there is one.
1758 * We also add a barrier here to ensure that the insertion of the lock
1759 * precedes these checks.
1762 error = check_conflicting_open(dentry, arg, lease->fl_flags);
1764 locks_unlink_lock_ctx(lease);
1769 if (lease->fl_lmops->lm_setup)
1770 lease->fl_lmops->lm_setup(lease, priv);
1772 spin_unlock(&ctx->flc_lock);
1773 percpu_up_read_preempt_enable(&file_rwsem);
1774 locks_dispose_list(&dispose);
1776 inode_unlock(inode);
1777 if (!error && !my_fl)
1782 static int generic_delete_lease(struct file *filp, void *owner)
1784 int error = -EAGAIN;
1785 struct file_lock *fl, *victim = NULL;
1786 struct inode *inode = locks_inode(filp);
1787 struct file_lock_context *ctx;
1790 ctx = smp_load_acquire(&inode->i_flctx);
1792 trace_generic_delete_lease(inode, NULL);
1796 percpu_down_read_preempt_disable(&file_rwsem);
1797 spin_lock(&ctx->flc_lock);
1798 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1799 if (fl->fl_file == filp &&
1800 fl->fl_owner == owner) {
1805 trace_generic_delete_lease(inode, victim);
1807 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
1808 spin_unlock(&ctx->flc_lock);
1809 percpu_up_read_preempt_enable(&file_rwsem);
1810 locks_dispose_list(&dispose);
1815 * generic_setlease - sets a lease on an open file
1816 * @filp: file pointer
1817 * @arg: type of lease to obtain
1818 * @flp: input - file_lock to use, output - file_lock inserted
1819 * @priv: private data for lm_setup (may be NULL if lm_setup
1820 * doesn't require it)
1822 * The (input) flp->fl_lmops->lm_break function is required
1825 int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1828 struct inode *inode = locks_inode(filp);
1831 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
1833 if (!S_ISREG(inode->i_mode))
1835 error = security_file_lock(filp, arg);
1841 return generic_delete_lease(filp, *priv);
1844 if (!(*flp)->fl_lmops->lm_break) {
1849 return generic_add_lease(filp, arg, flp, priv);
1854 EXPORT_SYMBOL(generic_setlease);
1857 * vfs_setlease - sets a lease on an open file
1858 * @filp: file pointer
1859 * @arg: type of lease to obtain
1860 * @lease: file_lock to use when adding a lease
1861 * @priv: private info for lm_setup when adding a lease (may be
1862 * NULL if lm_setup doesn't require it)
1864 * Call this to establish a lease on the file. The "lease" argument is not
1865 * used for F_UNLCK requests and may be NULL. For commands that set or alter
1866 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
1867 * set; if not, this function will return -ENOLCK (and generate a scary-looking
1870 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1871 * may be NULL if the lm_setup operation doesn't require it.
1874 vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
1876 if (filp->f_op->setlease && is_remote_lock(filp))
1877 return filp->f_op->setlease(filp, arg, lease, priv);
1879 return generic_setlease(filp, arg, lease, priv);
1881 EXPORT_SYMBOL_GPL(vfs_setlease);
1883 static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
1885 struct file_lock *fl;
1886 struct fasync_struct *new;
1889 fl = lease_alloc(filp, arg);
1893 new = fasync_alloc();
1895 locks_free_lock(fl);
1900 error = vfs_setlease(filp, arg, &fl, (void **)&new);
1902 locks_free_lock(fl);
1909 * fcntl_setlease - sets a lease on an open file
1910 * @fd: open file descriptor
1911 * @filp: file pointer
1912 * @arg: type of lease to obtain
1914 * Call this fcntl to establish a lease on the file.
1915 * Note that you also need to call %F_SETSIG to
1916 * receive a signal when the lease is broken.
1918 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1921 return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
1922 return do_fcntl_add_lease(fd, filp, arg);
1926 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
1927 * @inode: inode of the file to apply to
1928 * @fl: The lock to be applied
1930 * Apply a FLOCK style lock request to an inode.
1932 static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1937 error = flock_lock_inode(inode, fl);
1938 if (error != FILE_LOCK_DEFERRED)
1940 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1944 locks_delete_block(fl);
1951 * locks_lock_inode_wait - Apply a lock to an inode
1952 * @inode: inode of the file to apply to
1953 * @fl: The lock to be applied
1955 * Apply a POSIX or FLOCK style lock request to an inode.
1957 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1960 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
1962 res = posix_lock_inode_wait(inode, fl);
1965 res = flock_lock_inode_wait(inode, fl);
1972 EXPORT_SYMBOL(locks_lock_inode_wait);
1975 * sys_flock: - flock() system call.
1976 * @fd: the file descriptor to lock.
1977 * @cmd: the type of lock to apply.
1979 * Apply a %FL_FLOCK style lock to an open file descriptor.
1980 * The @cmd can be one of:
1982 * - %LOCK_SH -- a shared lock.
1983 * - %LOCK_EX -- an exclusive lock.
1984 * - %LOCK_UN -- remove an existing lock.
1985 * - %LOCK_MAND -- a 'mandatory' flock.
1986 * This exists to emulate Windows Share Modes.
1988 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1989 * processes read and write access respectively.
1991 SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
1993 struct fd f = fdget(fd);
1994 struct file_lock *lock;
1995 int can_sleep, unlock;
2002 can_sleep = !(cmd & LOCK_NB);
2004 unlock = (cmd == LOCK_UN);
2006 if (!unlock && !(cmd & LOCK_MAND) &&
2007 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
2010 lock = flock_make_lock(f.file, cmd);
2012 error = PTR_ERR(lock);
2017 lock->fl_flags |= FL_SLEEP;
2019 error = security_file_lock(f.file, lock->fl_type);
2023 if (f.file->f_op->flock && is_remote_lock(f.file))
2024 error = f.file->f_op->flock(f.file,
2025 (can_sleep) ? F_SETLKW : F_SETLK,
2028 error = locks_lock_file_wait(f.file, lock);
2031 locks_free_lock(lock);
2040 * vfs_test_lock - test file byte range lock
2041 * @filp: The file to test lock for
2042 * @fl: The lock to test; also used to hold result
2044 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2045 * setting conf->fl_type to something other than F_UNLCK.
2047 int vfs_test_lock(struct file *filp, struct file_lock *fl)
2049 if (filp->f_op->lock && is_remote_lock(filp))
2050 return filp->f_op->lock(filp, F_GETLK, fl);
2051 posix_test_lock(filp, fl);
2054 EXPORT_SYMBOL_GPL(vfs_test_lock);
2057 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
2058 * @fl: The file_lock who's fl_pid should be translated
2059 * @ns: The namespace into which the pid should be translated
2061 * Used to tranlate a fl_pid into a namespace virtual pid number
2063 static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns)
2070 if (IS_REMOTELCK(fl))
2073 * If the flock owner process is dead and its pid has been already
2074 * freed, the translation below won't work, but we still want to show
2075 * flock owner pid number in init pidns.
2077 if (ns == &init_pid_ns)
2078 return (pid_t)fl->fl_pid;
2081 pid = find_pid_ns(fl->fl_pid, &init_pid_ns);
2082 vnr = pid_nr_ns(pid, ns);
2087 static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2089 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2090 #if BITS_PER_LONG == 32
2092 * Make sure we can represent the posix lock via
2093 * legacy 32bit flock.
2095 if (fl->fl_start > OFFT_OFFSET_MAX)
2097 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2100 flock->l_start = fl->fl_start;
2101 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2102 fl->fl_end - fl->fl_start + 1;
2103 flock->l_whence = 0;
2104 flock->l_type = fl->fl_type;
2108 #if BITS_PER_LONG == 32
2109 static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2111 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2112 flock->l_start = fl->fl_start;
2113 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2114 fl->fl_end - fl->fl_start + 1;
2115 flock->l_whence = 0;
2116 flock->l_type = fl->fl_type;
2120 /* Report the first existing lock that would conflict with l.
2121 * This implements the F_GETLK command of fcntl().
2123 int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
2125 struct file_lock *fl;
2128 fl = locks_alloc_lock();
2132 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
2135 error = flock_to_posix_lock(filp, fl, flock);
2139 if (cmd == F_OFD_GETLK) {
2141 if (flock->l_pid != 0)
2145 fl->fl_flags |= FL_OFDLCK;
2146 fl->fl_owner = filp;
2149 error = vfs_test_lock(filp, fl);
2153 flock->l_type = fl->fl_type;
2154 if (fl->fl_type != F_UNLCK) {
2155 error = posix_lock_to_flock(flock, fl);
2160 locks_free_lock(fl);
2165 * vfs_lock_file - file byte range lock
2166 * @filp: The file to apply the lock to
2167 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2168 * @fl: The lock to be applied
2169 * @conf: Place to return a copy of the conflicting lock, if found.
2171 * A caller that doesn't care about the conflicting lock may pass NULL
2172 * as the final argument.
2174 * If the filesystem defines a private ->lock() method, then @conf will
2175 * be left unchanged; so a caller that cares should initialize it to
2176 * some acceptable default.
2178 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2179 * locks, the ->lock() interface may return asynchronously, before the lock has
2180 * been granted or denied by the underlying filesystem, if (and only if)
2181 * lm_grant is set. Callers expecting ->lock() to return asynchronously
2182 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
2183 * the request is for a blocking lock. When ->lock() does return asynchronously,
2184 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
2185 * request completes.
2186 * If the request is for non-blocking lock the file system should return
2187 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2188 * with the result. If the request timed out the callback routine will return a
2189 * nonzero return code and the file system should release the lock. The file
2190 * system is also responsible to keep a corresponding posix lock when it
2191 * grants a lock so the VFS can find out which locks are locally held and do
2192 * the correct lock cleanup when required.
2193 * The underlying filesystem must not drop the kernel lock or call
2194 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
2197 int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
2199 if (filp->f_op->lock && is_remote_lock(filp))
2200 return filp->f_op->lock(filp, cmd, fl);
2202 return posix_lock_file(filp, fl, conf);
2204 EXPORT_SYMBOL_GPL(vfs_lock_file);
2206 static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2207 struct file_lock *fl)
2211 error = security_file_lock(filp, fl->fl_type);
2216 error = vfs_lock_file(filp, cmd, fl, NULL);
2217 if (error != FILE_LOCK_DEFERRED)
2219 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2223 locks_delete_block(fl);
2230 /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2232 check_fmode_for_setlk(struct file_lock *fl)
2234 switch (fl->fl_type) {
2236 if (!(fl->fl_file->f_mode & FMODE_READ))
2240 if (!(fl->fl_file->f_mode & FMODE_WRITE))
2246 /* Apply the lock described by l to an open file descriptor.
2247 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2249 int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2250 struct flock *flock)
2252 struct file_lock *file_lock = locks_alloc_lock();
2253 struct inode *inode = locks_inode(filp);
2257 if (file_lock == NULL)
2260 /* Don't allow mandatory locks on files that may be memory mapped
2263 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
2268 error = flock_to_posix_lock(filp, file_lock, flock);
2272 error = check_fmode_for_setlk(file_lock);
2277 * If the cmd is requesting file-private locks, then set the
2278 * FL_OFDLCK flag and override the owner.
2283 if (flock->l_pid != 0)
2287 file_lock->fl_flags |= FL_OFDLCK;
2288 file_lock->fl_owner = filp;
2292 if (flock->l_pid != 0)
2296 file_lock->fl_flags |= FL_OFDLCK;
2297 file_lock->fl_owner = filp;
2300 file_lock->fl_flags |= FL_SLEEP;
2303 error = do_lock_file_wait(filp, cmd, file_lock);
2306 * Attempt to detect a close/fcntl race and recover by releasing the
2307 * lock that was just acquired. There is no need to do that when we're
2308 * unlocking though, or for OFD locks.
2310 if (!error && file_lock->fl_type != F_UNLCK &&
2311 !(file_lock->fl_flags & FL_OFDLCK)) {
2313 * We need that spin_lock here - it prevents reordering between
2314 * update of i_flctx->flc_posix and check for it done in
2315 * close(). rcu_read_lock() wouldn't do.
2317 spin_lock(¤t->files->file_lock);
2319 spin_unlock(¤t->files->file_lock);
2321 file_lock->fl_type = F_UNLCK;
2322 error = do_lock_file_wait(filp, cmd, file_lock);
2323 WARN_ON_ONCE(error);
2328 trace_fcntl_setlk(inode, file_lock, error);
2329 locks_free_lock(file_lock);
2333 #if BITS_PER_LONG == 32
2334 /* Report the first existing lock that would conflict with l.
2335 * This implements the F_GETLK command of fcntl().
2337 int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
2339 struct file_lock *fl;
2342 fl = locks_alloc_lock();
2347 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
2350 error = flock64_to_posix_lock(filp, fl, flock);
2354 if (cmd == F_OFD_GETLK) {
2356 if (flock->l_pid != 0)
2360 fl->fl_flags |= FL_OFDLCK;
2361 fl->fl_owner = filp;
2364 error = vfs_test_lock(filp, fl);
2368 flock->l_type = fl->fl_type;
2369 if (fl->fl_type != F_UNLCK)
2370 posix_lock_to_flock64(flock, fl);
2373 locks_free_lock(fl);
2377 /* Apply the lock described by l to an open file descriptor.
2378 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2380 int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2381 struct flock64 *flock)
2383 struct file_lock *file_lock = locks_alloc_lock();
2384 struct inode *inode = locks_inode(filp);
2388 if (file_lock == NULL)
2391 /* Don't allow mandatory locks on files that may be memory mapped
2394 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
2399 error = flock64_to_posix_lock(filp, file_lock, flock);
2403 error = check_fmode_for_setlk(file_lock);
2408 * If the cmd is requesting file-private locks, then set the
2409 * FL_OFDLCK flag and override the owner.
2414 if (flock->l_pid != 0)
2418 file_lock->fl_flags |= FL_OFDLCK;
2419 file_lock->fl_owner = filp;
2423 if (flock->l_pid != 0)
2427 file_lock->fl_flags |= FL_OFDLCK;
2428 file_lock->fl_owner = filp;
2431 file_lock->fl_flags |= FL_SLEEP;
2434 error = do_lock_file_wait(filp, cmd, file_lock);
2437 * Attempt to detect a close/fcntl race and recover by releasing the
2438 * lock that was just acquired. There is no need to do that when we're
2439 * unlocking though, or for OFD locks.
2441 if (!error && file_lock->fl_type != F_UNLCK &&
2442 !(file_lock->fl_flags & FL_OFDLCK)) {
2444 * We need that spin_lock here - it prevents reordering between
2445 * update of i_flctx->flc_posix and check for it done in
2446 * close(). rcu_read_lock() wouldn't do.
2448 spin_lock(¤t->files->file_lock);
2450 spin_unlock(¤t->files->file_lock);
2452 file_lock->fl_type = F_UNLCK;
2453 error = do_lock_file_wait(filp, cmd, file_lock);
2454 WARN_ON_ONCE(error);
2459 locks_free_lock(file_lock);
2462 #endif /* BITS_PER_LONG == 32 */
2465 * This function is called when the file is being removed
2466 * from the task's fd array. POSIX locks belonging to this task
2467 * are deleted at this time.
2469 void locks_remove_posix(struct file *filp, fl_owner_t owner)
2472 struct inode *inode = locks_inode(filp);
2473 struct file_lock lock;
2474 struct file_lock_context *ctx;
2477 * If there are no locks held on this file, we don't need to call
2478 * posix_lock_file(). Another process could be setting a lock on this
2479 * file at the same time, but we wouldn't remove that lock anyway.
2481 ctx = smp_load_acquire(&inode->i_flctx);
2482 if (!ctx || list_empty(&ctx->flc_posix))
2485 lock.fl_type = F_UNLCK;
2486 lock.fl_flags = FL_POSIX | FL_CLOSE;
2488 lock.fl_end = OFFSET_MAX;
2489 lock.fl_owner = owner;
2490 lock.fl_pid = current->tgid;
2491 lock.fl_file = filp;
2493 lock.fl_lmops = NULL;
2495 error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
2497 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2498 lock.fl_ops->fl_release_private(&lock);
2499 trace_locks_remove_posix(inode, &lock, error);
2502 EXPORT_SYMBOL(locks_remove_posix);
2504 /* The i_flctx must be valid when calling into here */
2506 locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2508 struct file_lock fl = {
2510 .fl_pid = current->tgid,
2512 .fl_flags = FL_FLOCK | FL_CLOSE,
2514 .fl_end = OFFSET_MAX,
2516 struct inode *inode = locks_inode(filp);
2518 if (list_empty(&flctx->flc_flock))
2521 if (filp->f_op->flock && is_remote_lock(filp))
2522 filp->f_op->flock(filp, F_SETLKW, &fl);
2524 flock_lock_inode(inode, &fl);
2526 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2527 fl.fl_ops->fl_release_private(&fl);
2530 /* The i_flctx must be valid when calling into here */
2532 locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
2534 struct file_lock *fl, *tmp;
2537 if (list_empty(&ctx->flc_lease))
2540 percpu_down_read_preempt_disable(&file_rwsem);
2541 spin_lock(&ctx->flc_lock);
2542 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2543 if (filp == fl->fl_file)
2544 lease_modify(fl, F_UNLCK, &dispose);
2545 spin_unlock(&ctx->flc_lock);
2546 percpu_up_read_preempt_enable(&file_rwsem);
2548 locks_dispose_list(&dispose);
2552 * This function is called on the last close of an open file.
2554 void locks_remove_file(struct file *filp)
2556 struct file_lock_context *ctx;
2558 ctx = smp_load_acquire(&locks_inode(filp)->i_flctx);
2562 /* remove any OFD locks */
2563 locks_remove_posix(filp, filp);
2565 /* remove flock locks */
2566 locks_remove_flock(filp, ctx);
2568 /* remove any leases */
2569 locks_remove_lease(filp, ctx);
2571 spin_lock(&ctx->flc_lock);
2572 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
2573 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
2574 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
2575 spin_unlock(&ctx->flc_lock);
2579 * posix_unblock_lock - stop waiting for a file lock
2580 * @waiter: the lock which was waiting
2582 * lockd needs to block waiting for locks.
2585 posix_unblock_lock(struct file_lock *waiter)
2589 spin_lock(&blocked_lock_lock);
2590 if (waiter->fl_next)
2591 __locks_delete_block(waiter);
2594 spin_unlock(&blocked_lock_lock);
2597 EXPORT_SYMBOL(posix_unblock_lock);
2600 * vfs_cancel_lock - file byte range unblock lock
2601 * @filp: The file to apply the unblock to
2602 * @fl: The lock to be unblocked
2604 * Used by lock managers to cancel blocked requests
2606 int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2608 if (filp->f_op->lock && is_remote_lock(filp))
2609 return filp->f_op->lock(filp, F_CANCELLK, fl);
2613 EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2615 #ifdef CONFIG_PROC_FS
2616 #include <linux/proc_fs.h>
2617 #include <linux/seq_file.h>
2619 struct locks_iterator {
2624 static void lock_get_status(struct seq_file *f, struct file_lock *fl,
2625 loff_t id, char *pfx)
2627 struct inode *inode = NULL;
2628 unsigned int fl_pid;
2629 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
2631 fl_pid = locks_translate_pid(fl, proc_pidns);
2633 * If lock owner is dead (and pid is freed) or not visible in current
2634 * pidns, zero is shown as a pid value. Check lock info from
2635 * init_pid_ns to get saved lock pid value.
2638 if (fl->fl_file != NULL)
2639 inode = locks_inode(fl->fl_file);
2641 seq_printf(f, "%lld:%s ", id, pfx);
2643 if (fl->fl_flags & FL_ACCESS)
2644 seq_puts(f, "ACCESS");
2645 else if (IS_OFDLCK(fl))
2646 seq_puts(f, "OFDLCK");
2648 seq_puts(f, "POSIX ");
2650 seq_printf(f, " %s ",
2651 (inode == NULL) ? "*NOINODE*" :
2652 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
2653 } else if (IS_FLOCK(fl)) {
2654 if (fl->fl_type & LOCK_MAND) {
2655 seq_puts(f, "FLOCK MSNFS ");
2657 seq_puts(f, "FLOCK ADVISORY ");
2659 } else if (IS_LEASE(fl)) {
2660 if (fl->fl_flags & FL_DELEG)
2661 seq_puts(f, "DELEG ");
2663 seq_puts(f, "LEASE ");
2665 if (lease_breaking(fl))
2666 seq_puts(f, "BREAKING ");
2667 else if (fl->fl_file)
2668 seq_puts(f, "ACTIVE ");
2670 seq_puts(f, "BREAKER ");
2672 seq_puts(f, "UNKNOWN UNKNOWN ");
2674 if (fl->fl_type & LOCK_MAND) {
2675 seq_printf(f, "%s ",
2676 (fl->fl_type & LOCK_READ)
2677 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2678 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2680 seq_printf(f, "%s ",
2681 (lease_breaking(fl))
2682 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2683 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
2686 /* userspace relies on this representation of dev_t */
2687 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
2688 MAJOR(inode->i_sb->s_dev),
2689 MINOR(inode->i_sb->s_dev), inode->i_ino);
2691 seq_printf(f, "%d <none>:0 ", fl_pid);
2694 if (fl->fl_end == OFFSET_MAX)
2695 seq_printf(f, "%Ld EOF\n", fl->fl_start);
2697 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
2699 seq_puts(f, "0 EOF\n");
2703 static int locks_show(struct seq_file *f, void *v)
2705 struct locks_iterator *iter = f->private;
2706 struct file_lock *fl, *bfl;
2707 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
2709 fl = hlist_entry(v, struct file_lock, fl_link);
2711 if (locks_translate_pid(fl, proc_pidns) == 0)
2714 lock_get_status(f, fl, iter->li_pos, "");
2716 list_for_each_entry(bfl, &fl->fl_block, fl_block)
2717 lock_get_status(f, bfl, iter->li_pos, " ->");
2722 static void __show_fd_locks(struct seq_file *f,
2723 struct list_head *head, int *id,
2724 struct file *filp, struct files_struct *files)
2726 struct file_lock *fl;
2728 list_for_each_entry(fl, head, fl_list) {
2730 if (filp != fl->fl_file)
2732 if (fl->fl_owner != files &&
2733 fl->fl_owner != filp)
2737 seq_puts(f, "lock:\t");
2738 lock_get_status(f, fl, *id, "");
2742 void show_fd_locks(struct seq_file *f,
2743 struct file *filp, struct files_struct *files)
2745 struct inode *inode = locks_inode(filp);
2746 struct file_lock_context *ctx;
2749 ctx = smp_load_acquire(&inode->i_flctx);
2753 spin_lock(&ctx->flc_lock);
2754 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
2755 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
2756 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
2757 spin_unlock(&ctx->flc_lock);
2760 static void *locks_start(struct seq_file *f, loff_t *pos)
2761 __acquires(&blocked_lock_lock)
2763 struct locks_iterator *iter = f->private;
2765 iter->li_pos = *pos + 1;
2766 percpu_down_write(&file_rwsem);
2767 spin_lock(&blocked_lock_lock);
2768 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
2771 static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2773 struct locks_iterator *iter = f->private;
2776 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
2779 static void locks_stop(struct seq_file *f, void *v)
2780 __releases(&blocked_lock_lock)
2782 spin_unlock(&blocked_lock_lock);
2783 percpu_up_write(&file_rwsem);
2786 static const struct seq_operations locks_seq_operations = {
2787 .start = locks_start,
2793 static int __init proc_locks_init(void)
2795 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
2796 sizeof(struct locks_iterator), NULL);
2799 fs_initcall(proc_locks_init);
2802 static int __init filelock_init(void)
2806 flctx_cache = kmem_cache_create("file_lock_ctx",
2807 sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
2809 filelock_cache = kmem_cache_create("file_lock_cache",
2810 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2813 for_each_possible_cpu(i) {
2814 struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
2816 spin_lock_init(&fll->lock);
2817 INIT_HLIST_HEAD(&fll->hlist);
2823 core_initcall(filelock_init);