xref: /linux/fs/locks.c (revision 128a37852234c1bd68eee4e7447f5362778009b8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/locks.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
51da177e4SLinus Torvalds  *  Doug Evans (dje@spiff.uucp), August 07, 1992
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Deadlock detection added.
81da177e4SLinus Torvalds  *  FIXME: one thing isn't handled yet:
91da177e4SLinus Torvalds  *	- mandatory locks (requires lots of changes elsewhere)
101da177e4SLinus Torvalds  *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
131da177e4SLinus Torvalds  *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *  Converted file_lock_table to a linked list from an array, which eliminates
161da177e4SLinus Torvalds  *  the limits on how many active file locks are open.
171da177e4SLinus Torvalds  *  Chad Page (pageone@netcom.com), November 27, 1994
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  Removed dependency on file descriptors. dup()'ed file descriptors now
201da177e4SLinus Torvalds  *  get the same locks as the original file descriptors, and a close() on
211da177e4SLinus Torvalds  *  any file descriptor removes ALL the locks on the file for the current
221da177e4SLinus Torvalds  *  process. Since locks still depend on the process id, locks are inherited
231da177e4SLinus Torvalds  *  after an exec() but not after a fork(). This agrees with POSIX, and both
241da177e4SLinus Torvalds  *  BSD and SVR4 practice.
251da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Scrapped free list which is redundant now that we allocate locks
281da177e4SLinus Torvalds  *  dynamically with kmalloc()/kfree().
291da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
341da177e4SLinus Torvalds  *  fcntl() system call. They have the semantics described above.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  FL_FLOCK locks are created with calls to flock(), through the flock()
371da177e4SLinus Torvalds  *  system call, which is new. Old C libraries implement flock() via fcntl()
381da177e4SLinus Torvalds  *  and will continue to use the old, broken implementation.
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
411da177e4SLinus Torvalds  *  with a file pointer (filp). As a result they can be shared by a parent
421da177e4SLinus Torvalds  *  process and its children after a fork(). They are removed when the last
431da177e4SLinus Torvalds  *  file descriptor referring to the file pointer is closed (unless explicitly
441da177e4SLinus Torvalds  *  unlocked).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  *  FL_FLOCK locks never deadlock, an existing lock is always removed before
471da177e4SLinus Torvalds  *  upgrading from shared to exclusive (or vice versa). When this happens
481da177e4SLinus Torvalds  *  any processes blocked by the current lock are woken up and allowed to
491da177e4SLinus Torvalds  *  run before the new lock is applied.
501da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  *  Removed some race conditions in flock_lock_file(), marked other possible
531da177e4SLinus Torvalds  *  races. Just grep for FIXME to see them.
541da177e4SLinus Torvalds  *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
571da177e4SLinus Torvalds  *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
581da177e4SLinus Torvalds  *  once we've checked for blocking and deadlocking.
591da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  *  Initial implementation of mandatory locks. SunOS turned out to be
621da177e4SLinus Torvalds  *  a rotten model, so I implemented the "obvious" semantics.
63395cf969SPaul Bolle  *  See 'Documentation/filesystems/mandatory-locking.txt' for details.
641da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
671da177e4SLinus Torvalds  *  check if a file has mandatory locks, used by mmap(), open() and creat() to
681da177e4SLinus Torvalds  *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
691da177e4SLinus Torvalds  *  Manual, Section 2.
701da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
711da177e4SLinus Torvalds  *
721da177e4SLinus Torvalds  *  Tidied up block list handling. Added '/proc/locks' interface.
731da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *  Fixed deadlock condition for pathological code that mixes calls to
761da177e4SLinus Torvalds  *  flock() and fcntl().
771da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
801da177e4SLinus Torvalds  *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
811da177e4SLinus Torvalds  *  guarantee sensible behaviour in the case where file system modules might
821da177e4SLinus Torvalds  *  be compiled with different options than the kernel itself.
831da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
861da177e4SLinus Torvalds  *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
871da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
901da177e4SLinus Torvalds  *  locks. Changed process synchronisation to avoid dereferencing locks that
911da177e4SLinus Torvalds  *  have already been freed.
921da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  *  Made the block list a circular list to minimise searching in the list.
951da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  *  Made mandatory locking a mount option. Default is not to allow mandatory
981da177e4SLinus Torvalds  *  locking.
991da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  *  Some adaptations for NFS support.
1021da177e4SLinus Torvalds  *  Olaf Kirch (okir@monad.swb.de), Dec 1996,
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
1051da177e4SLinus Torvalds  *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  *  Use slab allocator instead of kmalloc/kfree.
1081da177e4SLinus Torvalds  *  Use generic list implementation from <linux/list.h>.
1091da177e4SLinus Torvalds  *  Sped up posix_locks_deadlock by only considering blocked locks.
1101da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, March, 2000.
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  *  Leases and LOCK_MAND
1131da177e4SLinus Torvalds  *  Matthew Wilcox <willy@debian.org>, June, 2000.
1141da177e4SLinus Torvalds  *  Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #include <linux/capability.h>
1181da177e4SLinus Torvalds #include <linux/file.h>
1199f3acc31SAl Viro #include <linux/fdtable.h>
1201da177e4SLinus Torvalds #include <linux/fs.h>
1211da177e4SLinus Torvalds #include <linux/init.h>
1221da177e4SLinus Torvalds #include <linux/module.h>
1231da177e4SLinus Torvalds #include <linux/security.h>
1241da177e4SLinus Torvalds #include <linux/slab.h>
1251da177e4SLinus Torvalds #include <linux/syscalls.h>
1261da177e4SLinus Torvalds #include <linux/time.h>
1274fb3a538SDipankar Sarma #include <linux/rcupdate.h>
128ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
12948f74186SJeff Layton #include <linux/hashtable.h>
1307012b02aSJeff Layton #include <linux/percpu.h>
1317012b02aSJeff Layton #include <linux/lglock.h>
1321da177e4SLinus Torvalds 
13362af4f1fSJeff Layton #define CREATE_TRACE_POINTS
13462af4f1fSJeff Layton #include <trace/events/filelock.h>
13562af4f1fSJeff Layton 
1361da177e4SLinus Torvalds #include <asm/uaccess.h>
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1391da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
14011afe9f7SChristoph Hellwig #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
141cff2fce5SJeff Layton #define IS_OFDLCK(fl)	(fl->fl_flags & FL_OFDLCK)
1421da177e4SLinus Torvalds 
143ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
144ab83fa4bSJ. Bruce Fields {
145778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
146778fc546SJ. Bruce Fields }
147778fc546SJ. Bruce Fields 
148778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
149778fc546SJ. Bruce Fields {
150778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
151778fc546SJ. Bruce Fields 		return F_UNLCK;
152778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
153778fc546SJ. Bruce Fields 		return F_RDLCK;
154778fc546SJ. Bruce Fields 	return fl->fl_type;
155ab83fa4bSJ. Bruce Fields }
156ab83fa4bSJ. Bruce Fields 
1571da177e4SLinus Torvalds int leases_enable = 1;
1581da177e4SLinus Torvalds int lease_break_time = 45;
1591da177e4SLinus Torvalds 
1601c8c601aSJeff Layton /*
1617012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1627012b02aSJeff Layton  * keep a list on each CPU, with each list protected by its own spinlock via
1637012b02aSJeff Layton  * the file_lock_lglock. Note that alterations to the list also require that
1646109c850SJeff Layton  * the relevant flc_lock is held.
1651c8c601aSJeff Layton  */
1667012b02aSJeff Layton DEFINE_STATIC_LGLOCK(file_lock_lglock);
1677012b02aSJeff Layton static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
16888974691SJeff Layton 
1691c8c601aSJeff Layton /*
17048f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1717b2296afSJeff Layton  * It is protected by blocked_lock_lock.
17248f74186SJeff Layton  *
17348f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
17448f74186SJeff Layton  * particular lockowner is waiting on.
17548f74186SJeff Layton  *
17648f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
17748f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
17848f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1791c8c601aSJeff Layton  */
18048f74186SJeff Layton #define BLOCKED_HASH_BITS	7
18148f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
18288974691SJeff Layton 
1831c8c601aSJeff Layton /*
1847b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1857b2296afSJeff Layton  * want to be holding this lock.
1861c8c601aSJeff Layton  *
1871c8c601aSJeff Layton  * In addition, it also protects the fl->fl_block list, and the fl->fl_next
1881c8c601aSJeff Layton  * pointer for file_lock structures that are acting as lock requests (in
1891c8c601aSJeff Layton  * contrast to those that are acting as records of acquired locks).
1901c8c601aSJeff Layton  *
1911c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1926109c850SJeff Layton  * we often hold the flc_lock as well. In certain cases, when reading the fields
1931c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1946109c850SJeff Layton  * flc_lock.
1951c8c601aSJeff Layton  *
1961c8c601aSJeff Layton  * In particular, adding an entry to the fl_block list requires that you hold
1976109c850SJeff Layton  * both the flc_lock and the blocked_lock_lock (acquired in that order).
1986109c850SJeff Layton  * Deleting an entry from the list however only requires the file_lock_lock.
1991c8c601aSJeff Layton  */
2007b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
2011da177e4SLinus Torvalds 
2024a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly;
203e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
2041da177e4SLinus Torvalds 
2054a075e39SJeff Layton static struct file_lock_context *
2065c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type)
2074a075e39SJeff Layton {
208*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2094a075e39SJeff Layton 
210*128a3785SDmitry Vyukov 	/* paired with cmpxchg() below */
211*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
212*128a3785SDmitry Vyukov 	if (likely(ctx) || type == F_UNLCK)
2134a075e39SJeff Layton 		goto out;
2144a075e39SJeff Layton 
215*128a3785SDmitry Vyukov 	ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
216*128a3785SDmitry Vyukov 	if (!ctx)
2174a075e39SJeff Layton 		goto out;
2184a075e39SJeff Layton 
219*128a3785SDmitry Vyukov 	spin_lock_init(&ctx->flc_lock);
220*128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_flock);
221*128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_posix);
222*128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_lease);
2234a075e39SJeff Layton 
2244a075e39SJeff Layton 	/*
2254a075e39SJeff Layton 	 * Assign the pointer if it's not already assigned. If it is, then
2264a075e39SJeff Layton 	 * free the context we just allocated.
2274a075e39SJeff Layton 	 */
228*128a3785SDmitry Vyukov 	if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
229*128a3785SDmitry Vyukov 		kmem_cache_free(flctx_cache, ctx);
230*128a3785SDmitry Vyukov 		ctx = smp_load_acquire(&inode->i_flctx);
231*128a3785SDmitry Vyukov 	}
2324a075e39SJeff Layton out:
233*128a3785SDmitry Vyukov 	return ctx;
2344a075e39SJeff Layton }
2354a075e39SJeff Layton 
2364a075e39SJeff Layton void
2374a075e39SJeff Layton locks_free_lock_context(struct file_lock_context *ctx)
2384a075e39SJeff Layton {
2394a075e39SJeff Layton 	if (ctx) {
2404a075e39SJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_flock));
241bd61e0a9SJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_posix));
2428634b51fSJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_lease));
2434a075e39SJeff Layton 		kmem_cache_free(flctx_cache, ctx);
2444a075e39SJeff Layton 	}
2454a075e39SJeff Layton }
2464a075e39SJeff Layton 
247ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
248a51cb91dSMiklos Szeredi {
249139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
2506dee60f6SJeff Layton 	INIT_LIST_HEAD(&fl->fl_list);
251ee19cc40SMiklos Szeredi 	INIT_LIST_HEAD(&fl->fl_block);
252ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
253a51cb91dSMiklos Szeredi }
254a51cb91dSMiklos Szeredi 
2551da177e4SLinus Torvalds /* Allocate an empty lock structure. */
256c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2571da177e4SLinus Torvalds {
258ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
259a51cb91dSMiklos Szeredi 
260a51cb91dSMiklos Szeredi 	if (fl)
261ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
262a51cb91dSMiklos Szeredi 
263a51cb91dSMiklos Szeredi 	return fl;
2641da177e4SLinus Torvalds }
265c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2661da177e4SLinus Torvalds 
267a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
26847831f35STrond Myklebust {
26947831f35STrond Myklebust 	if (fl->fl_ops) {
27047831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
27147831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
27247831f35STrond Myklebust 		fl->fl_ops = NULL;
27347831f35STrond Myklebust 	}
27447831f35STrond Myklebust 
2755c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
276cae80b30SJeff Layton 		if (fl->fl_lmops->lm_put_owner) {
277cae80b30SJeff Layton 			fl->fl_lmops->lm_put_owner(fl->fl_owner);
278cae80b30SJeff Layton 			fl->fl_owner = NULL;
279cae80b30SJeff Layton 		}
2805c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
2815c97d7b1SKinglong Mee 	}
28247831f35STrond Myklebust }
283a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
28447831f35STrond Myklebust 
2851da177e4SLinus Torvalds /* Free a lock which is not in use. */
28605fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
2871da177e4SLinus Torvalds {
2885ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
2896dee60f6SJeff Layton 	BUG_ON(!list_empty(&fl->fl_list));
2905ce29646SMiklos Szeredi 	BUG_ON(!list_empty(&fl->fl_block));
291139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2921da177e4SLinus Torvalds 
29347831f35STrond Myklebust 	locks_release_private(fl);
2941da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
2951da177e4SLinus Torvalds }
29605fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
2971da177e4SLinus Torvalds 
298ed9814d8SJeff Layton static void
299ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
300ed9814d8SJeff Layton {
301ed9814d8SJeff Layton 	struct file_lock *fl;
302ed9814d8SJeff Layton 
303ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
3046dee60f6SJeff Layton 		fl = list_first_entry(dispose, struct file_lock, fl_list);
3056dee60f6SJeff Layton 		list_del_init(&fl->fl_list);
306ed9814d8SJeff Layton 		locks_free_lock(fl);
307ed9814d8SJeff Layton 	}
308ed9814d8SJeff Layton }
309ed9814d8SJeff Layton 
3101da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
3111da177e4SLinus Torvalds {
312ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
313ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
3201da177e4SLinus Torvalds  */
3213fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
3241da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
3250996905fSTrond Myklebust 	new->fl_file = NULL;
3261da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
3271da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
3281da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
3291da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
330f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
3310996905fSTrond Myklebust 	new->fl_ops = NULL;
332f328296eSKinglong Mee 
333f328296eSKinglong Mee 	if (fl->fl_lmops) {
334f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
335cae80b30SJeff Layton 			fl->fl_lmops->lm_get_owner(fl->fl_owner);
336f328296eSKinglong Mee 	}
3370996905fSTrond Myklebust }
3383fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
3390996905fSTrond Myklebust 
3400996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
3410996905fSTrond Myklebust {
342566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
343566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
3440996905fSTrond Myklebust 
3453fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
346f328296eSKinglong Mee 
3470996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
3481da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
34947831f35STrond Myklebust 
350f328296eSKinglong Mee 	if (fl->fl_ops) {
351f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
352f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
353f328296eSKinglong Mee 	}
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
3591da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
3601da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
3611da177e4SLinus Torvalds 	switch (cmd) {
3621da177e4SLinus Torvalds 	case LOCK_SH:
3631da177e4SLinus Torvalds 		return F_RDLCK;
3641da177e4SLinus Torvalds 	case LOCK_EX:
3651da177e4SLinus Torvalds 		return F_WRLCK;
3661da177e4SLinus Torvalds 	case LOCK_UN:
3671da177e4SLinus Torvalds 		return F_UNLCK;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	return -EINVAL;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
3736e129d00SJeff Layton static struct file_lock *
3746e129d00SJeff Layton flock_make_lock(struct file *filp, unsigned int cmd)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct file_lock *fl;
3771da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
3786e129d00SJeff Layton 
3791da177e4SLinus Torvalds 	if (type < 0)
3806e129d00SJeff Layton 		return ERR_PTR(type);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	fl = locks_alloc_lock();
3831da177e4SLinus Torvalds 	if (fl == NULL)
3846e129d00SJeff Layton 		return ERR_PTR(-ENOMEM);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	fl->fl_file = filp;
38773a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
3881da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3891da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
3901da177e4SLinus Torvalds 	fl->fl_type = type;
3911da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3921da177e4SLinus Torvalds 
3936e129d00SJeff Layton 	return fl;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3960ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	switch (type) {
3991da177e4SLinus Torvalds 	case F_RDLCK:
4001da177e4SLinus Torvalds 	case F_WRLCK:
4011da177e4SLinus Torvalds 	case F_UNLCK:
4021da177e4SLinus Torvalds 		fl->fl_type = type;
4031da177e4SLinus Torvalds 		break;
4041da177e4SLinus Torvalds 	default:
4051da177e4SLinus Torvalds 		return -EINVAL;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 	return 0;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
410ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
411ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
412ef12e72aSJ. Bruce Fields {
413ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
414ef12e72aSJ. Bruce Fields 	case SEEK_SET:
415ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
416ef12e72aSJ. Bruce Fields 		break;
417ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
418ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
419ef12e72aSJ. Bruce Fields 		break;
420ef12e72aSJ. Bruce Fields 	case SEEK_END:
421ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
422ef12e72aSJ. Bruce Fields 		break;
423ef12e72aSJ. Bruce Fields 	default:
424ef12e72aSJ. Bruce Fields 		return -EINVAL;
425ef12e72aSJ. Bruce Fields 	}
426ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
427ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
428ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
429ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
430ef12e72aSJ. Bruce Fields 		return -EINVAL;
431ef12e72aSJ. Bruce Fields 
432ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
433ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
434ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
435ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
436ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
437ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
438ef12e72aSJ. Bruce Fields 
439ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
440ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
441ef12e72aSJ. Bruce Fields 			return -EINVAL;
442ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
443ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
444ef12e72aSJ. Bruce Fields 	} else
445ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
446ef12e72aSJ. Bruce Fields 
447ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
448ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
449ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
450ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
451ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
452ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
453ef12e72aSJ. Bruce Fields 
454ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
455ef12e72aSJ. Bruce Fields }
456ef12e72aSJ. Bruce Fields 
4571da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
4581da177e4SLinus Torvalds  * style lock.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
4611da177e4SLinus Torvalds 			       struct flock *l)
4621da177e4SLinus Torvalds {
463ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
464ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
465ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
466ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
467ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
468ef12e72aSJ. Bruce Fields 	};
4691da177e4SLinus Torvalds 
470ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds /* default lease lock manager operations */
4744d01b7f5SJeff Layton static bool
4754d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4784d01b7f5SJeff Layton 	return false;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811c7dd2ffSJeff Layton static void
4821c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv)
4831c7dd2ffSJeff Layton {
4841c7dd2ffSJeff Layton 	struct file *filp = fl->fl_file;
4851c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
4861c7dd2ffSJeff Layton 
4871c7dd2ffSJeff Layton 	/*
4881c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
4891c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
4901c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
4911c7dd2ffSJeff Layton 	 */
4921c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
4931c7dd2ffSJeff Layton 		*priv = NULL;
4941c7dd2ffSJeff Layton 
4951c7dd2ffSJeff Layton 	__f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
4961c7dd2ffSJeff Layton }
4971c7dd2ffSJeff Layton 
4987b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
4998fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
5008fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
5011c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
5021da177e4SLinus Torvalds };
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /*
5051da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
5061da177e4SLinus Torvalds  */
5070ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
5081da177e4SLinus Torvalds  {
50975dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
51075dff55aSTrond Myklebust 		return -EINVAL;
51175dff55aSTrond Myklebust 
5127ca76311SJeff Layton 	fl->fl_owner = filp;
5131da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	fl->fl_file = filp;
5161da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
5171da177e4SLinus Torvalds 	fl->fl_start = 0;
5181da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
5191da177e4SLinus Torvalds 	fl->fl_ops = NULL;
5201da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
5211da177e4SLinus Torvalds 	return 0;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
5250ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
52875dff55aSTrond Myklebust 	int error = -ENOMEM;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	if (fl == NULL)
531e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
53475dff55aSTrond Myklebust 	if (error) {
53575dff55aSTrond Myklebust 		locks_free_lock(fl);
536e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
53775dff55aSTrond Myklebust 	}
538e32b8ee2SJ. Bruce Fields 	return fl;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /* Check if two locks overlap each other.
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
5461da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * Check whether two locks have the same owner.
5511da177e4SLinus Torvalds  */
55233443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
5531da177e4SLinus Torvalds {
5548fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
5551da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
5568fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
5571da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5606109c850SJeff Layton /* Must be called with the flc_lock held! */
5616ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
56288974691SJeff Layton {
5637012b02aSJeff Layton 	lg_local_lock(&file_lock_lglock);
5647012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
5657012b02aSJeff Layton 	hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
5667012b02aSJeff Layton 	lg_local_unlock(&file_lock_lglock);
56788974691SJeff Layton }
56888974691SJeff Layton 
5696109c850SJeff Layton /* Must be called with the flc_lock held! */
5706ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
57188974691SJeff Layton {
5727012b02aSJeff Layton 	/*
5737012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
5746109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
5757012b02aSJeff Layton 	 * also require that it be held.
5767012b02aSJeff Layton 	 */
5777012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
5787012b02aSJeff Layton 		return;
5797012b02aSJeff Layton 	lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
580139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
5817012b02aSJeff Layton 	lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
58288974691SJeff Layton }
58388974691SJeff Layton 
5843999e493SJeff Layton static unsigned long
5853999e493SJeff Layton posix_owner_key(struct file_lock *fl)
5863999e493SJeff Layton {
5873999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
5883999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
5893999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
5903999e493SJeff Layton }
5913999e493SJeff Layton 
5926ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
59388974691SJeff Layton {
594663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
595663d5af7SDaniel Wagner 
5963999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
59788974691SJeff Layton }
59888974691SJeff Layton 
5996ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
60088974691SJeff Layton {
601663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
602663d5af7SDaniel Wagner 
60348f74186SJeff Layton 	hash_del(&waiter->fl_link);
60488974691SJeff Layton }
60588974691SJeff Layton 
6061da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
6071da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
6081c8c601aSJeff Layton  *
6097b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
6101da177e4SLinus Torvalds  */
61133443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
6121da177e4SLinus Torvalds {
61388974691SJeff Layton 	locks_delete_global_blocked(waiter);
6141da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
6151da177e4SLinus Torvalds 	waiter->fl_next = NULL;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter)
6191da177e4SLinus Torvalds {
6207b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6211da177e4SLinus Torvalds 	__locks_delete_block(waiter);
6227b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
6261da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
6271da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
6281da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
6291c8c601aSJeff Layton  *
6306109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
6316109c850SJeff Layton  * fl_block list itself is protected by the blocked_lock_lock, but by ensuring
6326109c850SJeff Layton  * that the flc_lock is also held on insertions we can avoid taking the
6336109c850SJeff Layton  * blocked_lock_lock in some cases when we see that the fl_block list is empty.
6341da177e4SLinus Torvalds  */
6351c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
6361da177e4SLinus Torvalds 					struct file_lock *waiter)
6371da177e4SLinus Torvalds {
6386dc0fe8fSJ. Bruce Fields 	BUG_ON(!list_empty(&waiter->fl_block));
6391da177e4SLinus Torvalds 	waiter->fl_next = blocker;
64088974691SJeff Layton 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
641cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
6421c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
6431c8c601aSJeff Layton }
6441c8c601aSJeff Layton 
6456109c850SJeff Layton /* Must be called with flc_lock held. */
6461c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
6471c8c601aSJeff Layton 					struct file_lock *waiter)
6481c8c601aSJeff Layton {
6497b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6501c8c601aSJeff Layton 	__locks_insert_block(blocker, waiter);
6517b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541cb36012SJeff Layton /*
6551cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
6561cb36012SJeff Layton  *
6576109c850SJeff Layton  * Must be called with the inode->flc_lock held!
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
6601da177e4SLinus Torvalds {
6614e8c765dSJeff Layton 	/*
6624e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
6636109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
6646109c850SJeff Layton 	 * the flc_lock is always held here. Note that removal from the fl_block
6656109c850SJeff Layton 	 * list does not require the flc_lock, so we must recheck list_empty()
6667b2296afSJeff Layton 	 * after acquiring the blocked_lock_lock.
6674e8c765dSJeff Layton 	 */
6684e8c765dSJeff Layton 	if (list_empty(&blocker->fl_block))
6694e8c765dSJeff Layton 		return;
6704e8c765dSJeff Layton 
6717b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6721da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
673f0c1cd0eSPavel Emelyanov 		struct file_lock *waiter;
674f0c1cd0eSPavel Emelyanov 
675f0c1cd0eSPavel Emelyanov 		waiter = list_first_entry(&blocker->fl_block,
6761da177e4SLinus Torvalds 				struct file_lock, fl_block);
6771da177e4SLinus Torvalds 		__locks_delete_block(waiter);
6788fb47a4fSJ. Bruce Fields 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
6798fb47a4fSJ. Bruce Fields 			waiter->fl_lmops->lm_notify(waiter);
6801da177e4SLinus Torvalds 		else
6811da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
6821da177e4SLinus Torvalds 	}
6837b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6865263e31eSJeff Layton static void
687e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
6885263e31eSJeff Layton {
6895263e31eSJeff Layton 	fl->fl_nspid = get_pid(task_tgid(current));
6905263e31eSJeff Layton 	list_add_tail(&fl->fl_list, before);
6915263e31eSJeff Layton 	locks_insert_global_locks(fl);
6925263e31eSJeff Layton }
6935263e31eSJeff Layton 
6948634b51fSJeff Layton static void
695e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl)
6961da177e4SLinus Torvalds {
69788974691SJeff Layton 	locks_delete_global_locks(fl);
6988634b51fSJeff Layton 	list_del_init(&fl->fl_list);
699ab1f1611SVitaliy Gusev 	if (fl->fl_nspid) {
700ab1f1611SVitaliy Gusev 		put_pid(fl->fl_nspid);
701ab1f1611SVitaliy Gusev 		fl->fl_nspid = NULL;
702ab1f1611SVitaliy Gusev 	}
7031da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
70424cbe784SJeff Layton }
70524cbe784SJeff Layton 
7065263e31eSJeff Layton static void
707e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
7085263e31eSJeff Layton {
709e084c1bdSJeff Layton 	locks_unlink_lock_ctx(fl);
7108634b51fSJeff Layton 	if (dispose)
7118634b51fSJeff Layton 		list_add(&fl->fl_list, dispose);
7128634b51fSJeff Layton 	else
7138634b51fSJeff Layton 		locks_free_lock(fl);
7145263e31eSJeff Layton }
7155263e31eSJeff Layton 
7161da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
7171da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
7221da177e4SLinus Torvalds 		return 1;
7231da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
7241da177e4SLinus Torvalds 		return 1;
7251da177e4SLinus Torvalds 	return 0;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
7291da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7301da177e4SLinus Torvalds  */
7311da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
7341da177e4SLinus Torvalds 	 * each other.
7351da177e4SLinus Torvalds 	 */
7369b8c8695SJeff Layton 	if (posix_same_owner(caller_fl, sys_fl))
7371da177e4SLinus Torvalds 		return (0);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/* Check whether they overlap */
7401da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
7411da177e4SLinus Torvalds 		return 0;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
7471da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
7521da177e4SLinus Torvalds 	 * each other.
7531da177e4SLinus Torvalds 	 */
7549b8c8695SJeff Layton 	if (caller_fl->fl_file == sys_fl->fl_file)
7551da177e4SLinus Torvalds 		return (0);
7561da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
7571da177e4SLinus Torvalds 		return 0;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7626d34ac19SJ. Bruce Fields void
7639d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	struct file_lock *cfl;
766bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
7671c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
7681da177e4SLinus Torvalds 
769*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
770bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
771bd61e0a9SJeff Layton 		fl->fl_type = F_UNLCK;
772bd61e0a9SJeff Layton 		return;
7731da177e4SLinus Torvalds 	}
774bd61e0a9SJeff Layton 
7756109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
776bd61e0a9SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
777bd61e0a9SJeff Layton 		if (posix_locks_conflict(fl, cfl)) {
7783fe0fff1SKinglong Mee 			locks_copy_conflock(fl, cfl);
779ab1f1611SVitaliy Gusev 			if (cfl->fl_nspid)
7806c5f3e7bSPavel Emelyanov 				fl->fl_pid = pid_vnr(cfl->fl_nspid);
781bd61e0a9SJeff Layton 			goto out;
782bd61e0a9SJeff Layton 		}
783bd61e0a9SJeff Layton 	}
784129a84deSJ. Bruce Fields 	fl->fl_type = F_UNLCK;
785bd61e0a9SJeff Layton out:
7866109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
7876d34ac19SJ. Bruce Fields 	return;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7901da177e4SLinus Torvalds 
791b533184fSJ. Bruce Fields /*
792b533184fSJ. Bruce Fields  * Deadlock detection:
7931da177e4SLinus Torvalds  *
794b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
795b533184fSJ. Bruce Fields  * locks.
7961da177e4SLinus Torvalds  *
797b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
798b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
799b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
800b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
801b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
802b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
803b533184fSJ. Bruce Fields  * cycle.
80497855b49SJ. Bruce Fields  *
805b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
806b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
807b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
808b533184fSJ. Bruce Fields  *
809b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
810b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
811b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
812b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
81357b65325SJeff Layton  *
814cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
81557b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
81657b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
81757b65325SJeff Layton  * skip it for those.
81857b65325SJeff Layton  *
819cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
82057b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
82157b65325SJeff Layton  * upgrade from read to write locks on the same inode.
8221da177e4SLinus Torvalds  */
82397855b49SJ. Bruce Fields 
82497855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
82597855b49SJ. Bruce Fields 
826b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
827b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
828b533184fSJ. Bruce Fields {
829b533184fSJ. Bruce Fields 	struct file_lock *fl;
830b533184fSJ. Bruce Fields 
8313999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
832b533184fSJ. Bruce Fields 		if (posix_same_owner(fl, block_fl))
833b533184fSJ. Bruce Fields 			return fl->fl_next;
834b533184fSJ. Bruce Fields 	}
835b533184fSJ. Bruce Fields 	return NULL;
836b533184fSJ. Bruce Fields }
837b533184fSJ. Bruce Fields 
8387b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
839b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
8401da177e4SLinus Torvalds 				struct file_lock *block_fl)
8411da177e4SLinus Torvalds {
84297855b49SJ. Bruce Fields 	int i = 0;
8431da177e4SLinus Torvalds 
844663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
845663d5af7SDaniel Wagner 
84657b65325SJeff Layton 	/*
84757b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
848cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
84957b65325SJeff Layton 	 */
850cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
85157b65325SJeff Layton 		return 0;
85257b65325SJeff Layton 
853b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
85497855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
85597855b49SJ. Bruce Fields 			return 0;
856b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
857b533184fSJ. Bruce Fields 			return 1;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 	return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
86302888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
864f475ae95STrond Myklebust  *
865f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
866f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
867f475ae95STrond Myklebust  * value for -ENOENT.
8681da177e4SLinus Torvalds  */
869bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request)
8701da177e4SLinus Torvalds {
871993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
8725263e31eSJeff Layton 	struct file_lock *fl;
8735263e31eSJeff Layton 	struct file_lock_context *ctx;
8741da177e4SLinus Torvalds 	int error = 0;
8755263e31eSJeff Layton 	bool found = false;
876ed9814d8SJeff Layton 	LIST_HEAD(dispose);
8771da177e4SLinus Torvalds 
8785c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
8795c1c669aSJeff Layton 	if (!ctx) {
8805c1c669aSJeff Layton 		if (request->fl_type != F_UNLCK)
8815263e31eSJeff Layton 			return -ENOMEM;
8825c1c669aSJeff Layton 		return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
8835c1c669aSJeff Layton 	}
8845263e31eSJeff Layton 
885b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
886b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
887b89f4321SArnd Bergmann 		if (!new_fl)
888b89f4321SArnd Bergmann 			return -ENOMEM;
889b89f4321SArnd Bergmann 	}
890b89f4321SArnd Bergmann 
8916109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
892f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
893f07f18ddSTrond Myklebust 		goto find_conflict;
89484d535adSPavel Emelyanov 
8955263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
896bcd7f78dSJeff Layton 		if (request->fl_file != fl->fl_file)
8971da177e4SLinus Torvalds 			continue;
898993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
8991da177e4SLinus Torvalds 			goto out;
9005263e31eSJeff Layton 		found = true;
901e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, &dispose);
9021da177e4SLinus Torvalds 		break;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
905f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
906f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
907f475ae95STrond Myklebust 			error = -ENOENT;
908993dfa87STrond Myklebust 		goto out;
909f475ae95STrond Myklebust 	}
9101da177e4SLinus Torvalds 
911f07f18ddSTrond Myklebust find_conflict:
9125263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
913993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
9141da177e4SLinus Torvalds 			continue;
9151da177e4SLinus Torvalds 		error = -EAGAIN;
916bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
917bde74e4bSMiklos Szeredi 			goto out;
918bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
919993dfa87STrond Myklebust 		locks_insert_block(fl, request);
9201da177e4SLinus Torvalds 		goto out;
9211da177e4SLinus Torvalds 	}
922f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
923f07f18ddSTrond Myklebust 		goto out;
924993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
925e084c1bdSJeff Layton 	locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
926993dfa87STrond Myklebust 	new_fl = NULL;
9279cedc194SKirill Korotaev 	error = 0;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds out:
9306109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
931993dfa87STrond Myklebust 	if (new_fl)
932993dfa87STrond Myklebust 		locks_free_lock(new_fl);
933ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
9341da177e4SLinus Torvalds 	return error;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
937150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
9381da177e4SLinus Torvalds {
939bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
94039005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
94139005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
9421da177e4SLinus Torvalds 	struct file_lock *left = NULL;
9431da177e4SLinus Torvalds 	struct file_lock *right = NULL;
944bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
945b9746ef8SJeff Layton 	int error;
946b9746ef8SJeff Layton 	bool added = false;
947ed9814d8SJeff Layton 	LIST_HEAD(dispose);
9481da177e4SLinus Torvalds 
9495c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
950bd61e0a9SJeff Layton 	if (!ctx)
9515c1c669aSJeff Layton 		return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
952bd61e0a9SJeff Layton 
9531da177e4SLinus Torvalds 	/*
9541da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
9551da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
95639005d02SMiklos Szeredi 	 *
95739005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
9581da177e4SLinus Torvalds 	 */
95939005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
96039005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
96139005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
9621da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
9631da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
96439005d02SMiklos Szeredi 	}
9651da177e4SLinus Torvalds 
9666109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
9671cb36012SJeff Layton 	/*
9681cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
9691cb36012SJeff Layton 	 * there are any, either return error or put the request on the
97048f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
9711cb36012SJeff Layton 	 */
9721da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
973bd61e0a9SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
9741da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
9751da177e4SLinus Torvalds 				continue;
9765842add2SAndy Adamson 			if (conflock)
9773fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
9781da177e4SLinus Torvalds 			error = -EAGAIN;
9791da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
9801da177e4SLinus Torvalds 				goto out;
9811c8c601aSJeff Layton 			/*
9821c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
9831c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
9841c8c601aSJeff Layton 			 */
9851da177e4SLinus Torvalds 			error = -EDEADLK;
9867b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
9871c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
988bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
9891c8c601aSJeff Layton 				__locks_insert_block(fl, request);
9901c8c601aSJeff Layton 			}
9917b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
9921da177e4SLinus Torvalds 			goto out;
9931da177e4SLinus Torvalds   		}
9941da177e4SLinus Torvalds   	}
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
9971da177e4SLinus Torvalds 	error = 0;
9981da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
9991da177e4SLinus Torvalds 		goto out;
10001da177e4SLinus Torvalds 
1001bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
1002bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1003bd61e0a9SJeff Layton 		if (posix_same_owner(request, fl))
1004bd61e0a9SJeff Layton 			break;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	/* Process locks with this owner. */
1008bd61e0a9SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1009bd61e0a9SJeff Layton 		if (!posix_same_owner(request, fl))
1010bd61e0a9SJeff Layton 			break;
1011bd61e0a9SJeff Layton 
1012bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
10131da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
1014449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1015449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1016449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1017449231d6SOlaf Kirch 			 */
10181da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1019bd61e0a9SJeff Layton 				continue;
10201da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
10211da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
10221da177e4SLinus Torvalds 			 */
1023449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
10241da177e4SLinus Torvalds 				break;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
10271da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
10281da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
10291da177e4SLinus Torvalds 			 * locks to the higher end address.
10301da177e4SLinus Torvalds 			 */
10311da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
10321da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
10331da177e4SLinus Torvalds 			else
10341da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
10351da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
10361da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
10371da177e4SLinus Torvalds 			else
10381da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
10391da177e4SLinus Torvalds 			if (added) {
1040e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
10411da177e4SLinus Torvalds 				continue;
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 			request = fl;
1044b9746ef8SJeff Layton 			added = true;
1045bd61e0a9SJeff Layton 		} else {
10461da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
10471da177e4SLinus Torvalds 			 * more complex.
10481da177e4SLinus Torvalds 			 */
10491da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1050bd61e0a9SJeff Layton 				continue;
10511da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
10521da177e4SLinus Torvalds 				break;
10531da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1054b9746ef8SJeff Layton 				added = true;
10551da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
10561da177e4SLinus Torvalds 				left = fl;
10571da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
10581da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
10591da177e4SLinus Torvalds 			 */
10601da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
10611da177e4SLinus Torvalds 				right = fl;
10621da177e4SLinus Torvalds 				break;
10631da177e4SLinus Torvalds 			}
10641da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
10651da177e4SLinus Torvalds 				/* The new lock completely replaces an old
10661da177e4SLinus Torvalds 				 * one (This may happen several times).
10671da177e4SLinus Torvalds 				 */
10681da177e4SLinus Torvalds 				if (added) {
1069e084c1bdSJeff Layton 					locks_delete_lock_ctx(fl, &dispose);
10701da177e4SLinus Torvalds 					continue;
10711da177e4SLinus Torvalds 				}
1072b84d49f9SJeff Layton 				/*
1073b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1074b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1075b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1076b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1077b84d49f9SJeff Layton 				 * just replacing an existing lock.
10781da177e4SLinus Torvalds 				 */
1079b84d49f9SJeff Layton 				error = -ENOLCK;
1080b84d49f9SJeff Layton 				if (!new_fl)
1081b84d49f9SJeff Layton 					goto out;
1082b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
1083b84d49f9SJeff Layton 				request = new_fl;
1084b84d49f9SJeff Layton 				new_fl = NULL;
1085e084c1bdSJeff Layton 				locks_insert_lock_ctx(request, &fl->fl_list);
1086e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
1087b9746ef8SJeff Layton 				added = true;
10881da177e4SLinus Torvalds 			}
10891da177e4SLinus Torvalds 		}
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10920d9a490aSMiklos Szeredi 	/*
10931cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
10941cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
10951cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
10960d9a490aSMiklos Szeredi 	 */
10970d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
10980d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
10990d9a490aSMiklos Szeredi 		goto out;
11000d9a490aSMiklos Szeredi 
11011da177e4SLinus Torvalds 	error = 0;
11021da177e4SLinus Torvalds 	if (!added) {
1103f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1104f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1105f475ae95STrond Myklebust 				error = -ENOENT;
11061da177e4SLinus Torvalds 			goto out;
1107f475ae95STrond Myklebust 		}
11080d9a490aSMiklos Szeredi 
11090d9a490aSMiklos Szeredi 		if (!new_fl) {
11100d9a490aSMiklos Szeredi 			error = -ENOLCK;
11110d9a490aSMiklos Szeredi 			goto out;
11120d9a490aSMiklos Szeredi 		}
11131da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
1114e084c1bdSJeff Layton 		locks_insert_lock_ctx(new_fl, &fl->fl_list);
11152e2f756fSJeff Layton 		fl = new_fl;
11161da177e4SLinus Torvalds 		new_fl = NULL;
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 	if (right) {
11191da177e4SLinus Torvalds 		if (left == right) {
11201da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
11211da177e4SLinus Torvalds 			 * so we have to use the second new lock.
11221da177e4SLinus Torvalds 			 */
11231da177e4SLinus Torvalds 			left = new_fl2;
11241da177e4SLinus Torvalds 			new_fl2 = NULL;
11251da177e4SLinus Torvalds 			locks_copy_lock(left, right);
1126e084c1bdSJeff Layton 			locks_insert_lock_ctx(left, &fl->fl_list);
11271da177e4SLinus Torvalds 		}
11281da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
11291da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 	if (left) {
11321da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
11331da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
11341da177e4SLinus Torvalds 	}
11351da177e4SLinus Torvalds  out:
11366109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
11371da177e4SLinus Torvalds 	/*
11381da177e4SLinus Torvalds 	 * Free any unused locks.
11391da177e4SLinus Torvalds 	 */
11401da177e4SLinus Torvalds 	if (new_fl)
11411da177e4SLinus Torvalds 		locks_free_lock(new_fl);
11421da177e4SLinus Torvalds 	if (new_fl2)
11431da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1144ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
11451da177e4SLinus Torvalds 	return error;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /**
11491da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
11501da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11511da177e4SLinus Torvalds  * @fl: The lock to be applied
1152150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
11531da177e4SLinus Torvalds  *
11541da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11551da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11561da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1157f475ae95STrond Myklebust  *
1158f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1159f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1160f475ae95STrond Myklebust  * value for -ENOENT.
11611da177e4SLinus Torvalds  */
1162150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
11635842add2SAndy Adamson 			struct file_lock *conflock)
11645842add2SAndy Adamson {
1165496ad9aaSAl Viro 	return __posix_lock_file(file_inode(filp), fl, conflock);
11665842add2SAndy Adamson }
1167150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /**
117029d01b22SJeff Layton  * posix_lock_inode_wait - Apply a POSIX-style lock to a file
117129d01b22SJeff Layton  * @inode: inode of file to which lock request should be applied
11721da177e4SLinus Torvalds  * @fl: The lock to be applied
11731da177e4SLinus Torvalds  *
117429d01b22SJeff Layton  * Variant of posix_lock_file_wait that does not take a filp, and so can be
117529d01b22SJeff Layton  * used after the filp has already been torn down.
11761da177e4SLinus Torvalds  */
117729d01b22SJeff Layton int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	int error;
11801da177e4SLinus Torvalds 	might_sleep ();
11811da177e4SLinus Torvalds 	for (;;) {
118229d01b22SJeff Layton 		error = __posix_lock_file(inode, fl, NULL);
1183bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
11841da177e4SLinus Torvalds 			break;
11851da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
11861da177e4SLinus Torvalds 		if (!error)
11871da177e4SLinus Torvalds 			continue;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 		locks_delete_block(fl);
11901da177e4SLinus Torvalds 		break;
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds 	return error;
11931da177e4SLinus Torvalds }
119429d01b22SJeff Layton EXPORT_SYMBOL(posix_lock_inode_wait);
119529d01b22SJeff Layton 
119629d01b22SJeff Layton /**
11971da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1198d7a06983SJeff Layton  * @file: the file to check
11991da177e4SLinus Torvalds  *
12001da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
12011da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
12021da177e4SLinus Torvalds  */
1203d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
12041da177e4SLinus Torvalds {
1205bd61e0a9SJeff Layton 	int ret;
1206d7a06983SJeff Layton 	struct inode *inode = file_inode(file);
1207bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
12081da177e4SLinus Torvalds 	struct file_lock *fl;
12091da177e4SLinus Torvalds 
1210*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
1211bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix))
1212bd61e0a9SJeff Layton 		return 0;
1213bd61e0a9SJeff Layton 
12141da177e4SLinus Torvalds 	/*
12151da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
12161da177e4SLinus Torvalds 	 */
12176109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1218bd61e0a9SJeff Layton 	ret = 0;
1219bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
122073a8f5f7SChristoph Hellwig 		if (fl->fl_owner != current->files &&
1221bd61e0a9SJeff Layton 		    fl->fl_owner != file) {
1222bd61e0a9SJeff Layton 			ret = -EAGAIN;
12231da177e4SLinus Torvalds 			break;
12241da177e4SLinus Torvalds 		}
1225bd61e0a9SJeff Layton 	}
12266109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1227bd61e0a9SJeff Layton 	return ret;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /**
12311da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
12321da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
12331da177e4SLinus Torvalds  *		for shared
12341da177e4SLinus Torvalds  * @inode:      the file to check
12351da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
12361da177e4SLinus Torvalds  * @offset:     start of area to check
12371da177e4SLinus Torvalds  * @count:      length of area to check
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
12401da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
12411da177e4SLinus Torvalds  * locks_verify_truncate().
12421da177e4SLinus Torvalds  */
12431da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
12441da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
12451da177e4SLinus Torvalds 			 size_t count)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds 	struct file_lock fl;
12481da177e4SLinus Torvalds 	int error;
124929723adeSJeff Layton 	bool sleep = false;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	locks_init_lock(&fl);
12521da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
12531da177e4SLinus Torvalds 	fl.fl_file = filp;
12541da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
12551da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
125629723adeSJeff Layton 		sleep = true;
12571da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
12581da177e4SLinus Torvalds 	fl.fl_start = offset;
12591da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	for (;;) {
126229723adeSJeff Layton 		if (filp) {
126373a8f5f7SChristoph Hellwig 			fl.fl_owner = filp;
126429723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
126529723adeSJeff Layton 			error = __posix_lock_file(inode, &fl, NULL);
126629723adeSJeff Layton 			if (!error)
126729723adeSJeff Layton 				break;
126829723adeSJeff Layton 		}
126929723adeSJeff Layton 
127029723adeSJeff Layton 		if (sleep)
127129723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
127229723adeSJeff Layton 		fl.fl_owner = current->files;
1273150b3934SMarc Eshel 		error = __posix_lock_file(inode, &fl, NULL);
1274bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
12751da177e4SLinus Torvalds 			break;
12761da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
12771da177e4SLinus Torvalds 		if (!error) {
12781da177e4SLinus Torvalds 			/*
12791da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
12801da177e4SLinus Torvalds 			 * changed the permissions behind our back.
12811da177e4SLinus Torvalds 			 */
1282a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
12831da177e4SLinus Torvalds 				continue;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 		locks_delete_block(&fl);
12871da177e4SLinus Torvalds 		break;
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	return error;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
12941da177e4SLinus Torvalds 
1295778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1296778fc546SJ. Bruce Fields {
1297778fc546SJ. Bruce Fields 	switch (arg) {
1298778fc546SJ. Bruce Fields 	case F_UNLCK:
1299778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1300778fc546SJ. Bruce Fields 		/* fall through: */
1301778fc546SJ. Bruce Fields 	case F_RDLCK:
1302778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1303778fc546SJ. Bruce Fields 	}
1304778fc546SJ. Bruce Fields }
1305778fc546SJ. Bruce Fields 
13061da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
13077448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	if (error)
13121da177e4SLinus Torvalds 		return error;
1313778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
13141da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
13153b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
13163b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
13173b6e2723SFilipe Brandenburger 
13183b6e2723SFilipe Brandenburger 		f_delown(filp);
13193b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
132096d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
132196d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
132296d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
132396d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
132496d6d59cSJ. Bruce Fields 		}
1325e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, dispose);
13263b6e2723SFilipe Brandenburger 	}
13271da177e4SLinus Torvalds 	return 0;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
13301da177e4SLinus Torvalds 
1331778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1332778fc546SJ. Bruce Fields {
1333778fc546SJ. Bruce Fields 	if (!then)
1334778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1335778fc546SJ. Bruce Fields 		return false;
1336778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1337778fc546SJ. Bruce Fields }
1338778fc546SJ. Bruce Fields 
1339c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
13401da177e4SLinus Torvalds {
13418634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
13428634b51fSJeff Layton 	struct file_lock *fl, *tmp;
13431da177e4SLinus Torvalds 
13446109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1345f82b4b67SJeff Layton 
13468634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
134762af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1348778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
13497448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1350778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
13517448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
1355df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1356df4e8d2cSJ. Bruce Fields {
135711afe9f7SChristoph Hellwig 	if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
135811afe9f7SChristoph Hellwig 		return false;
1359df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1360df4e8d2cSJ. Bruce Fields 		return false;
1361df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1362df4e8d2cSJ. Bruce Fields }
1363df4e8d2cSJ. Bruce Fields 
136403d12ddfSJeff Layton static bool
136503d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker)
136603d12ddfSJeff Layton {
13678634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
136803d12ddfSJeff Layton 	struct file_lock *fl;
136903d12ddfSJeff Layton 
13706109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
137103d12ddfSJeff Layton 
13728634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
137303d12ddfSJeff Layton 		if (leases_conflict(fl, breaker))
137403d12ddfSJeff Layton 			return true;
137503d12ddfSJeff Layton 	}
137603d12ddfSJeff Layton 	return false;
137703d12ddfSJeff Layton }
137803d12ddfSJeff Layton 
13791da177e4SLinus Torvalds /**
13801da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
13811da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1382df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1383df4e8d2cSJ. Bruce Fields  *	    break all leases
1384df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1385df4e8d2cSJ. Bruce Fields  *	    only delegations
13861da177e4SLinus Torvalds  *
138787250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
138887250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
138987250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
13901da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
13911da177e4SLinus Torvalds  */
1392df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
13931da177e4SLinus Torvalds {
1394778fc546SJ. Bruce Fields 	int error = 0;
1395*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1396a901125cSYan, Zheng 	struct file_lock *new_fl, *fl, *tmp;
13971da177e4SLinus Torvalds 	unsigned long break_time;
13988737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1399c45198edSJeff Layton 	LIST_HEAD(dispose);
14001da177e4SLinus Torvalds 
14018737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
14026d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
14036d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1404df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
14051da177e4SLinus Torvalds 
14068634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
1407*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
14088634b51fSJeff Layton 	if (!ctx) {
14098634b51fSJeff Layton 		WARN_ON_ONCE(1);
14108634b51fSJeff Layton 		return error;
14118634b51fSJeff Layton 	}
14128634b51fSJeff Layton 
14136109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
14141da177e4SLinus Torvalds 
1415c45198edSJeff Layton 	time_out_leases(inode, &dispose);
14161da177e4SLinus Torvalds 
141703d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1418df4e8d2cSJ. Bruce Fields 		goto out;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	break_time = 0;
14211da177e4SLinus Torvalds 	if (lease_break_time > 0) {
14221da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
14231da177e4SLinus Torvalds 		if (break_time == 0)
14241da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
14251da177e4SLinus Torvalds 	}
14261da177e4SLinus Torvalds 
1427a901125cSYan, Zheng 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1428df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1429df4e8d2cSJ. Bruce Fields 			continue;
1430778fc546SJ. Bruce Fields 		if (want_write) {
1431778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1432778fc546SJ. Bruce Fields 				continue;
1433778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
14341da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1435778fc546SJ. Bruce Fields 		} else {
14368634b51fSJeff Layton 			if (lease_breaking(fl))
1437778fc546SJ. Bruce Fields 				continue;
1438778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1439778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
14401da177e4SLinus Torvalds 		}
14414d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
1442e084c1bdSJeff Layton 			locks_delete_lock_ctx(fl, &dispose);
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
14458634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
14464d01b7f5SJeff Layton 		goto out;
14474d01b7f5SJeff Layton 
1448843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
144962af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
14501da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
14511da177e4SLinus Torvalds 		goto out;
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds restart:
14558634b51fSJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
14568634b51fSJeff Layton 	break_time = fl->fl_break_time;
1457f1c6bb2cSJeff Layton 	if (break_time != 0)
14581da177e4SLinus Torvalds 		break_time -= jiffies;
14591da177e4SLinus Torvalds 	if (break_time == 0)
14601da177e4SLinus Torvalds 		break_time++;
14618634b51fSJeff Layton 	locks_insert_block(fl, new_fl);
146262af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
14636109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1464c45198edSJeff Layton 	locks_dispose_list(&dispose);
14654321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
14664321e01eSMatthew Wilcox 						!new_fl->fl_next, break_time);
14676109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
146862af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
14691c8c601aSJeff Layton 	locks_delete_block(new_fl);
14701da177e4SLinus Torvalds 	if (error >= 0) {
1471778fc546SJ. Bruce Fields 		/*
1472778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1473778fc546SJ. Bruce Fields 		 * broken yet
1474778fc546SJ. Bruce Fields 		 */
147503d12ddfSJeff Layton 		if (error == 0)
147603d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
147703d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
14781da177e4SLinus Torvalds 			goto restart;
14791da177e4SLinus Torvalds 		error = 0;
14801da177e4SLinus Torvalds 	}
14811da177e4SLinus Torvalds out:
14826109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1483c45198edSJeff Layton 	locks_dispose_list(&dispose);
14841da177e4SLinus Torvalds 	locks_free_lock(new_fl);
14851da177e4SLinus Torvalds 	return error;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds /**
1491a6b91919SRandy Dunlap  *	lease_get_mtime - get the last modified time of an inode
14921da177e4SLinus Torvalds  *	@inode: the inode
14931da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
14941da177e4SLinus Torvalds  *
14951da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
14961da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1497a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
14981da177e4SLinus Torvalds  */
14991da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
15001da177e4SLinus Torvalds {
1501bfe86024SJeff Layton 	bool has_lease = false;
1502*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
15038634b51fSJeff Layton 	struct file_lock *fl;
1504bfe86024SJeff Layton 
1505*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
15068634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
15076109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
15088634b51fSJeff Layton 		if (!list_empty(&ctx->flc_lease)) {
15098634b51fSJeff Layton 			fl = list_first_entry(&ctx->flc_lease,
15108634b51fSJeff Layton 						struct file_lock, fl_list);
15118634b51fSJeff Layton 			if (fl->fl_type == F_WRLCK)
1512bfe86024SJeff Layton 				has_lease = true;
15138634b51fSJeff Layton 		}
15146109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1515bfe86024SJeff Layton 	}
1516bfe86024SJeff Layton 
1517bfe86024SJeff Layton 	if (has_lease)
15181da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
15191da177e4SLinus Torvalds 	else
15201da177e4SLinus Torvalds 		*time = inode->i_mtime;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds /**
15261da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
15271da177e4SLinus Torvalds  *	@filp: the file
15281da177e4SLinus Torvalds  *
15291da177e4SLinus Torvalds  *	The value returned by this function will be one of
15301da177e4SLinus Torvalds  *	(if no lease break is pending):
15311da177e4SLinus Torvalds  *
15321da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
15331da177e4SLinus Torvalds  *
15341da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
15351da177e4SLinus Torvalds  *
15361da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
15371da177e4SLinus Torvalds  *
15381da177e4SLinus Torvalds  *	(if a lease break is pending):
15391da177e4SLinus Torvalds  *
15401da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
15411da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
15421da177e4SLinus Torvalds  *
15431da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
15441da177e4SLinus Torvalds  *
15451da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
15461da177e4SLinus Torvalds  *	should be returned to userspace.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	struct file_lock *fl;
15511c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
1552*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
15531da177e4SLinus Torvalds 	int type = F_UNLCK;
1554c45198edSJeff Layton 	LIST_HEAD(dispose);
15551da177e4SLinus Torvalds 
1556*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
15578634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
15586109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1559c45198edSJeff Layton 		time_out_leases(file_inode(filp), &dispose);
15608634b51fSJeff Layton 		list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
15618634b51fSJeff Layton 			if (fl->fl_file != filp)
15628634b51fSJeff Layton 				continue;
1563778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
15641da177e4SLinus Torvalds 			break;
15651da177e4SLinus Torvalds 		}
15666109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1567c45198edSJeff Layton 		locks_dispose_list(&dispose);
15688634b51fSJeff Layton 	}
15691da177e4SLinus Torvalds 	return type;
15701da177e4SLinus Torvalds }
15711da177e4SLinus Torvalds 
157224cbe784SJeff Layton /**
157324cbe784SJeff Layton  * check_conflicting_open - see if the given dentry points to a file that has
157424cbe784SJeff Layton  * 			    an existing open that would conflict with the
157524cbe784SJeff Layton  * 			    desired lease.
157624cbe784SJeff Layton  * @dentry:	dentry to check
157724cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
15787fadc59cSRandy Dunlap  * @flags:	current lock flags
157924cbe784SJeff Layton  *
158024cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
158124cbe784SJeff Layton  * conflict with the lease we're trying to set.
158224cbe784SJeff Layton  */
158324cbe784SJeff Layton static int
158411afe9f7SChristoph Hellwig check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
158524cbe784SJeff Layton {
158624cbe784SJeff Layton 	int ret = 0;
158724cbe784SJeff Layton 	struct inode *inode = dentry->d_inode;
158824cbe784SJeff Layton 
158911afe9f7SChristoph Hellwig 	if (flags & FL_LAYOUT)
159011afe9f7SChristoph Hellwig 		return 0;
159111afe9f7SChristoph Hellwig 
159224cbe784SJeff Layton 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
159324cbe784SJeff Layton 		return -EAGAIN;
159424cbe784SJeff Layton 
159524cbe784SJeff Layton 	if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
159624cbe784SJeff Layton 	    (atomic_read(&inode->i_count) > 1)))
159724cbe784SJeff Layton 		ret = -EAGAIN;
159824cbe784SJeff Layton 
159924cbe784SJeff Layton 	return ret;
160024cbe784SJeff Layton }
160124cbe784SJeff Layton 
1602e6f5c789SJeff Layton static int
1603e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
16041da177e4SLinus Torvalds {
16058634b51fSJeff Layton 	struct file_lock *fl, *my_fl = NULL, *lease;
16060f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
16071da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16088634b51fSJeff Layton 	struct file_lock_context *ctx;
1609df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1610c1f24ef4SJ. Bruce Fields 	int error;
1611c45198edSJeff Layton 	LIST_HEAD(dispose);
16121da177e4SLinus Torvalds 
1613096657b6SJ. Bruce Fields 	lease = *flp;
161462af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
161562af4f1fSJeff Layton 
16165c1c669aSJeff Layton 	/* Note that arg is never F_UNLCK here */
16175c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, arg);
16188634b51fSJeff Layton 	if (!ctx)
16198634b51fSJeff Layton 		return -ENOMEM;
16208634b51fSJeff Layton 
1621df4e8d2cSJ. Bruce Fields 	/*
1622df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1623df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1624df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1625df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1626df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1627df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1628df4e8d2cSJ. Bruce Fields 	 */
1629df4e8d2cSJ. Bruce Fields 	if (is_deleg && !mutex_trylock(&inode->i_mutex))
1630df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1631df4e8d2cSJ. Bruce Fields 
1632df4e8d2cSJ. Bruce Fields 	if (is_deleg && arg == F_WRLCK) {
1633df4e8d2cSJ. Bruce Fields 		/* Write delegations are not currently supported: */
16344fdb793fSDan Carpenter 		mutex_unlock(&inode->i_mutex);
1635df4e8d2cSJ. Bruce Fields 		WARN_ON_ONCE(1);
1636df4e8d2cSJ. Bruce Fields 		return -EINVAL;
1637df4e8d2cSJ. Bruce Fields 	}
1638096657b6SJ. Bruce Fields 
16396109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1640c45198edSJeff Layton 	time_out_leases(inode, &dispose);
164111afe9f7SChristoph Hellwig 	error = check_conflicting_open(dentry, arg, lease->fl_flags);
164224cbe784SJeff Layton 	if (error)
16431da177e4SLinus Torvalds 		goto out;
164485c59580SPavel Emelyanov 
16451da177e4SLinus Torvalds 	/*
16461da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
16471da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
16481da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
16491da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
16501da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
16511da177e4SLinus Torvalds 	 * except for this filp.
16521da177e4SLinus Torvalds 	 */
1653c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
16548634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
16552ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
16562ab99ee1SChristoph Hellwig 		    fl->fl_owner == lease->fl_owner) {
16578634b51fSJeff Layton 			my_fl = fl;
1658c1f24ef4SJ. Bruce Fields 			continue;
16591da177e4SLinus Torvalds 		}
16608634b51fSJeff Layton 
1661c1f24ef4SJ. Bruce Fields 		/*
1662c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1663c1f24ef4SJ. Bruce Fields 		 * this file:
1664c1f24ef4SJ. Bruce Fields 		 */
1665c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
16661da177e4SLinus Torvalds 			goto out;
1667c1f24ef4SJ. Bruce Fields 		/*
1668c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1669c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1670c1f24ef4SJ. Bruce Fields 		 */
1671c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1672c1f24ef4SJ. Bruce Fields 			goto out;
1673c1f24ef4SJ. Bruce Fields 	}
16741da177e4SLinus Torvalds 
16758634b51fSJeff Layton 	if (my_fl != NULL) {
16760164bf02SJeff Layton 		lease = my_fl;
16770164bf02SJeff Layton 		error = lease->fl_lmops->lm_change(lease, arg, &dispose);
16781c7dd2ffSJeff Layton 		if (error)
16791da177e4SLinus Torvalds 			goto out;
16801c7dd2ffSJeff Layton 		goto out_setup;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	error = -EINVAL;
16841da177e4SLinus Torvalds 	if (!leases_enable)
16851da177e4SLinus Torvalds 		goto out;
16861da177e4SLinus Torvalds 
1687e084c1bdSJeff Layton 	locks_insert_lock_ctx(lease, &ctx->flc_lease);
168824cbe784SJeff Layton 	/*
168924cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
169024cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
169124cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
169224cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
169324cbe784SJeff Layton 	 *
169424cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
169524cbe784SJeff Layton 	 * precedes these checks.
169624cbe784SJeff Layton 	 */
169724cbe784SJeff Layton 	smp_mb();
169811afe9f7SChristoph Hellwig 	error = check_conflicting_open(dentry, arg, lease->fl_flags);
16998634b51fSJeff Layton 	if (error) {
1700e084c1bdSJeff Layton 		locks_unlink_lock_ctx(lease);
17018634b51fSJeff Layton 		goto out;
17028634b51fSJeff Layton 	}
17031c7dd2ffSJeff Layton 
17041c7dd2ffSJeff Layton out_setup:
17051c7dd2ffSJeff Layton 	if (lease->fl_lmops->lm_setup)
17061c7dd2ffSJeff Layton 		lease->fl_lmops->lm_setup(lease, priv);
17071da177e4SLinus Torvalds out:
17086109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1709c45198edSJeff Layton 	locks_dispose_list(&dispose);
1710df4e8d2cSJ. Bruce Fields 	if (is_deleg)
1711df4e8d2cSJ. Bruce Fields 		mutex_unlock(&inode->i_mutex);
17128634b51fSJeff Layton 	if (!error && !my_fl)
17131c7dd2ffSJeff Layton 		*flp = NULL;
17141da177e4SLinus Torvalds 	return error;
17151da177e4SLinus Torvalds }
17168335ebd9SJ. Bruce Fields 
17172ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner)
17188335ebd9SJ. Bruce Fields {
17190efaa7e8SJeff Layton 	int error = -EAGAIN;
17208634b51fSJeff Layton 	struct file_lock *fl, *victim = NULL;
17218335ebd9SJ. Bruce Fields 	struct dentry *dentry = filp->f_path.dentry;
17228335ebd9SJ. Bruce Fields 	struct inode *inode = dentry->d_inode;
1723*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1724c45198edSJeff Layton 	LIST_HEAD(dispose);
17258335ebd9SJ. Bruce Fields 
1726*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
17278634b51fSJeff Layton 	if (!ctx) {
17288634b51fSJeff Layton 		trace_generic_delete_lease(inode, NULL);
17298634b51fSJeff Layton 		return error;
17308634b51fSJeff Layton 	}
17318634b51fSJeff Layton 
17326109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
17338634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
17342ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
17352ab99ee1SChristoph Hellwig 		    fl->fl_owner == owner) {
17368634b51fSJeff Layton 			victim = fl;
17370efaa7e8SJeff Layton 			break;
17388335ebd9SJ. Bruce Fields 		}
17398634b51fSJeff Layton 	}
1740a9b1b455SJeff Layton 	trace_generic_delete_lease(inode, victim);
17418634b51fSJeff Layton 	if (victim)
17427448cc37SJeff Layton 		error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
17436109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1744c45198edSJeff Layton 	locks_dispose_list(&dispose);
17450efaa7e8SJeff Layton 	return error;
17468335ebd9SJ. Bruce Fields }
17478335ebd9SJ. Bruce Fields 
17481da177e4SLinus Torvalds /**
17491da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
17501da177e4SLinus Torvalds  *	@filp:	file pointer
17511da177e4SLinus Torvalds  *	@arg:	type of lease to obtain
17521da177e4SLinus Torvalds  *	@flp:	input - file_lock to use, output - file_lock inserted
17531c7dd2ffSJeff Layton  *	@priv:	private data for lm_setup (may be NULL if lm_setup
17541c7dd2ffSJeff Layton  *		doesn't require it)
17551da177e4SLinus Torvalds  *
17561da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
17571da177e4SLinus Torvalds  *	by break_lease().
17581da177e4SLinus Torvalds  */
1759e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1760e6f5c789SJeff Layton 			void **priv)
17611da177e4SLinus Torvalds {
17621da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_path.dentry;
17631da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17648335ebd9SJ. Bruce Fields 	int error;
17651da177e4SLinus Torvalds 
17668e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
17678335ebd9SJ. Bruce Fields 		return -EACCES;
17681da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
17698335ebd9SJ. Bruce Fields 		return -EINVAL;
17701da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
17711da177e4SLinus Torvalds 	if (error)
17728335ebd9SJ. Bruce Fields 		return error;
17731da177e4SLinus Torvalds 
17748335ebd9SJ. Bruce Fields 	switch (arg) {
17758335ebd9SJ. Bruce Fields 	case F_UNLCK:
17762ab99ee1SChristoph Hellwig 		return generic_delete_lease(filp, *priv);
17778335ebd9SJ. Bruce Fields 	case F_RDLCK:
17788335ebd9SJ. Bruce Fields 	case F_WRLCK:
17790efaa7e8SJeff Layton 		if (!(*flp)->fl_lmops->lm_break) {
17800efaa7e8SJeff Layton 			WARN_ON_ONCE(1);
17810efaa7e8SJeff Layton 			return -ENOLCK;
17820efaa7e8SJeff Layton 		}
178311afe9f7SChristoph Hellwig 
1784e6f5c789SJeff Layton 		return generic_add_lease(filp, arg, flp, priv);
17858335ebd9SJ. Bruce Fields 	default:
17868d657eb3SDave Jones 		return -EINVAL;
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds }
17890af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds /**
1792a9933ceaSJ. Bruce Fields  * vfs_setlease        -       sets a lease on an open file
17931da177e4SLinus Torvalds  * @filp:	file pointer
17941da177e4SLinus Torvalds  * @arg:	type of lease to obtain
1795e51673aaSJeff Layton  * @lease:	file_lock to use when adding a lease
17961c7dd2ffSJeff Layton  * @priv:	private info for lm_setup when adding a lease (may be
17971c7dd2ffSJeff Layton  * 		NULL if lm_setup doesn't require it)
17981da177e4SLinus Torvalds  *
1799e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
1800e51673aaSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
1801e51673aaSJeff Layton  * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
1802e51673aaSJeff Layton  * if not, this function will return -ENOLCK (and generate a scary-looking
1803e51673aaSJeff Layton  * stack trace).
18041c7dd2ffSJeff Layton  *
18051c7dd2ffSJeff Layton  * The "priv" pointer is passed directly to the lm_setup function as-is. It
18061c7dd2ffSJeff Layton  * may be NULL if the lm_setup operation doesn't require it.
18071da177e4SLinus Torvalds  */
1808e6f5c789SJeff Layton int
1809e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
18101da177e4SLinus Torvalds {
18111c7dd2ffSJeff Layton 	if (filp->f_op->setlease)
1812f82b4b67SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
18131c7dd2ffSJeff Layton 	else
1814f82b4b67SJeff Layton 		return generic_setlease(filp, arg, lease, priv);
18151da177e4SLinus Torvalds }
1816a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
18171da177e4SLinus Torvalds 
18180ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
18191da177e4SLinus Torvalds {
18201c7dd2ffSJeff Layton 	struct file_lock *fl;
1821f7347ce4SLinus Torvalds 	struct fasync_struct *new;
18221da177e4SLinus Torvalds 	int error;
18231da177e4SLinus Torvalds 
1824c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1825c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1826c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
18271da177e4SLinus Torvalds 
1828f7347ce4SLinus Torvalds 	new = fasync_alloc();
1829f7347ce4SLinus Torvalds 	if (!new) {
1830f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1831f7347ce4SLinus Torvalds 		return -ENOMEM;
1832f7347ce4SLinus Torvalds 	}
18331c7dd2ffSJeff Layton 	new->fa_fd = fd;
18341da177e4SLinus Torvalds 
18351c7dd2ffSJeff Layton 	error = vfs_setlease(filp, arg, &fl, (void **)&new);
18362dfb928fSJeff Layton 	if (fl)
18372dfb928fSJeff Layton 		locks_free_lock(fl);
1838f7347ce4SLinus Torvalds 	if (new)
1839f7347ce4SLinus Torvalds 		fasync_free(new);
18401da177e4SLinus Torvalds 	return error;
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /**
18440ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
18450ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
18460ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
18470ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
18480ceaf6c7SJ. Bruce Fields  *
18490ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
18500ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
18510ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
18520ceaf6c7SJ. Bruce Fields  */
18530ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
18540ceaf6c7SJ. Bruce Fields {
18550ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
18562ab99ee1SChristoph Hellwig 		return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
18570ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
18580ceaf6c7SJ. Bruce Fields }
18590ceaf6c7SJ. Bruce Fields 
18600ceaf6c7SJ. Bruce Fields /**
186129d01b22SJeff Layton  * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
186229d01b22SJeff Layton  * @inode: inode of the file to apply to
18631da177e4SLinus Torvalds  * @fl: The lock to be applied
18641da177e4SLinus Torvalds  *
186529d01b22SJeff Layton  * Apply a FLOCK style lock request to an inode.
18661da177e4SLinus Torvalds  */
186729d01b22SJeff Layton int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
18681da177e4SLinus Torvalds {
18691da177e4SLinus Torvalds 	int error;
18701da177e4SLinus Torvalds 	might_sleep();
18711da177e4SLinus Torvalds 	for (;;) {
187229d01b22SJeff Layton 		error = flock_lock_inode(inode, fl);
1873bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
18741da177e4SLinus Torvalds 			break;
18751da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
18761da177e4SLinus Torvalds 		if (!error)
18771da177e4SLinus Torvalds 			continue;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 		locks_delete_block(fl);
18801da177e4SLinus Torvalds 		break;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 	return error;
18831da177e4SLinus Torvalds }
188429d01b22SJeff Layton EXPORT_SYMBOL(flock_lock_inode_wait);
18851da177e4SLinus Torvalds 
188629d01b22SJeff Layton /**
18871da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
18881da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
18891da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
18901da177e4SLinus Torvalds  *
18911da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
18921da177e4SLinus Torvalds  *	The @cmd can be one of
18931da177e4SLinus Torvalds  *
18941da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
18951da177e4SLinus Torvalds  *
18961da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
18971da177e4SLinus Torvalds  *
18981da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
18991da177e4SLinus Torvalds  *
19001da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
19011da177e4SLinus Torvalds  *
19021da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
19031da177e4SLinus Torvalds  *	processes read and write access respectively.
19041da177e4SLinus Torvalds  */
1905002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
19061da177e4SLinus Torvalds {
19072903ff01SAl Viro 	struct fd f = fdget(fd);
19081da177e4SLinus Torvalds 	struct file_lock *lock;
19091da177e4SLinus Torvalds 	int can_sleep, unlock;
19101da177e4SLinus Torvalds 	int error;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	error = -EBADF;
19132903ff01SAl Viro 	if (!f.file)
19141da177e4SLinus Torvalds 		goto out;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
19171da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
19181da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
19191da177e4SLinus Torvalds 
1920aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
19212903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
19221da177e4SLinus Torvalds 		goto out_putf;
19231da177e4SLinus Torvalds 
19246e129d00SJeff Layton 	lock = flock_make_lock(f.file, cmd);
19256e129d00SJeff Layton 	if (IS_ERR(lock)) {
19266e129d00SJeff Layton 		error = PTR_ERR(lock);
19271da177e4SLinus Torvalds 		goto out_putf;
19286e129d00SJeff Layton 	}
19296e129d00SJeff Layton 
19301da177e4SLinus Torvalds 	if (can_sleep)
19311da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
19321da177e4SLinus Torvalds 
19332903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
19341da177e4SLinus Torvalds 	if (error)
19351da177e4SLinus Torvalds 		goto out_free;
19361da177e4SLinus Torvalds 
193772c2d531SAl Viro 	if (f.file->f_op->flock)
19382903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
19391da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
19401da177e4SLinus Torvalds 					  lock);
19411da177e4SLinus Torvalds 	else
19422903ff01SAl Viro 		error = flock_lock_file_wait(f.file, lock);
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds  out_free:
19451da177e4SLinus Torvalds 	locks_free_lock(lock);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds  out_putf:
19482903ff01SAl Viro 	fdput(f);
19491da177e4SLinus Torvalds  out:
19501da177e4SLinus Torvalds 	return error;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19533ee17abdSJ. Bruce Fields /**
19543ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
19553ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
19566924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
19573ee17abdSJ. Bruce Fields  *
19583ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
19593ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
19603ee17abdSJ. Bruce Fields  */
19613ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
19623ee17abdSJ. Bruce Fields {
196372c2d531SAl Viro 	if (filp->f_op->lock)
19643ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
19653ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
19663ee17abdSJ. Bruce Fields 	return 0;
19673ee17abdSJ. Bruce Fields }
19683ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
19693ee17abdSJ. Bruce Fields 
1970c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1971c2fa1b8aSJ. Bruce Fields {
1972cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1973c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1974c2fa1b8aSJ. Bruce Fields 	/*
1975c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
1976c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
1977c2fa1b8aSJ. Bruce Fields 	 */
1978c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
1979c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1980c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1981c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1982c2fa1b8aSJ. Bruce Fields #endif
1983c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1984c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1985c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1986c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1987129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1988c2fa1b8aSJ. Bruce Fields 	return 0;
1989c2fa1b8aSJ. Bruce Fields }
1990c2fa1b8aSJ. Bruce Fields 
1991c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1992c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1993c2fa1b8aSJ. Bruce Fields {
1994cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1995c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1996c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1997c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1998c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1999c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2000c2fa1b8aSJ. Bruce Fields }
2001c2fa1b8aSJ. Bruce Fields #endif
2002c2fa1b8aSJ. Bruce Fields 
20031da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
20041da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
20051da177e4SLinus Torvalds  */
2006c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
20071da177e4SLinus Torvalds {
20089d6a8c5cSMarc Eshel 	struct file_lock file_lock;
20091da177e4SLinus Torvalds 	struct flock flock;
20101da177e4SLinus Torvalds 	int error;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	error = -EFAULT;
20131da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20141da177e4SLinus Torvalds 		goto out;
20151da177e4SLinus Torvalds 	error = -EINVAL;
20161da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
20171da177e4SLinus Torvalds 		goto out;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
20201da177e4SLinus Torvalds 	if (error)
20211da177e4SLinus Torvalds 		goto out;
20221da177e4SLinus Torvalds 
20230d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
202490478939SJeff Layton 		error = -EINVAL;
202590478939SJeff Layton 		if (flock.l_pid != 0)
202690478939SJeff Layton 			goto out;
202790478939SJeff Layton 
20285d50ffd7SJeff Layton 		cmd = F_GETLK;
2029cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
203073a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
20315d50ffd7SJeff Layton 	}
20325d50ffd7SJeff Layton 
20333ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
20343ee17abdSJ. Bruce Fields 	if (error)
20351da177e4SLinus Torvalds 		goto out;
20361da177e4SLinus Torvalds 
20379d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
20389d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
20399d6a8c5cSMarc Eshel 		error = posix_lock_to_flock(&flock, &file_lock);
2040c2fa1b8aSJ. Bruce Fields 		if (error)
2041f328296eSKinglong Mee 			goto rel_priv;
20421da177e4SLinus Torvalds 	}
20431da177e4SLinus Torvalds 	error = -EFAULT;
20441da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
20451da177e4SLinus Torvalds 		error = 0;
2046f328296eSKinglong Mee rel_priv:
2047f328296eSKinglong Mee 	locks_release_private(&file_lock);
20481da177e4SLinus Torvalds out:
20491da177e4SLinus Torvalds 	return error;
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
20527723ec97SMarc Eshel /**
20537723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
20547723ec97SMarc Eshel  * @filp: The file to apply the lock to
20557723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
20567723ec97SMarc Eshel  * @fl: The lock to be applied
2057150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2058150b3934SMarc Eshel  *
2059150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2060150b3934SMarc Eshel  * as the final argument.
2061150b3934SMarc Eshel  *
2062150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
2063150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
2064150b3934SMarc Eshel  * some acceptable default.
20652beb6614SMarc Eshel  *
20662beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
20672beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
20682beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
20698fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
20702beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
20712beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
20728fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
20732beb6614SMarc Eshel  * request completes.
20742beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
2075bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2076bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
20772beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
20782beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
20792beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
20802beb6614SMarc Eshel  * the correct lock cleanup when required.
20812beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
20828fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
20832beb6614SMarc Eshel  * return code.
20847723ec97SMarc Eshel  */
2085150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
20867723ec97SMarc Eshel {
208772c2d531SAl Viro 	if (filp->f_op->lock)
20887723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
20897723ec97SMarc Eshel 	else
2090150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
20917723ec97SMarc Eshel }
20927723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
20937723ec97SMarc Eshel 
2094b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2095b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2096b648a6deSMiklos Szeredi {
2097b648a6deSMiklos Szeredi 	int error;
2098b648a6deSMiklos Szeredi 
2099b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2100b648a6deSMiklos Szeredi 	if (error)
2101b648a6deSMiklos Szeredi 		return error;
2102b648a6deSMiklos Szeredi 
2103b648a6deSMiklos Szeredi 	for (;;) {
2104764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2105b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2106b648a6deSMiklos Szeredi 			break;
2107764c76b3SMiklos Szeredi 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2108b648a6deSMiklos Szeredi 		if (!error)
2109b648a6deSMiklos Szeredi 			continue;
2110b648a6deSMiklos Szeredi 
2111b648a6deSMiklos Szeredi 		locks_delete_block(fl);
2112b648a6deSMiklos Szeredi 		break;
2113b648a6deSMiklos Szeredi 	}
2114b648a6deSMiklos Szeredi 
2115b648a6deSMiklos Szeredi 	return error;
2116b648a6deSMiklos Szeredi }
2117b648a6deSMiklos Szeredi 
2118cf01f4eeSJeff Layton /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
2119cf01f4eeSJeff Layton static int
2120cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2121cf01f4eeSJeff Layton {
2122cf01f4eeSJeff Layton 	switch (fl->fl_type) {
2123cf01f4eeSJeff Layton 	case F_RDLCK:
2124cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_READ))
2125cf01f4eeSJeff Layton 			return -EBADF;
2126cf01f4eeSJeff Layton 		break;
2127cf01f4eeSJeff Layton 	case F_WRLCK:
2128cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_WRITE))
2129cf01f4eeSJeff Layton 			return -EBADF;
2130cf01f4eeSJeff Layton 	}
2131cf01f4eeSJeff Layton 	return 0;
2132cf01f4eeSJeff Layton }
2133cf01f4eeSJeff Layton 
21341da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
21351da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
21361da177e4SLinus Torvalds  */
2137c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2138c293621bSPeter Staubach 		struct flock __user *l)
21391da177e4SLinus Torvalds {
21401da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
21411da177e4SLinus Torvalds 	struct flock flock;
21421da177e4SLinus Torvalds 	struct inode *inode;
21430b2bac2fSAl Viro 	struct file *f;
21441da177e4SLinus Torvalds 	int error;
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	if (file_lock == NULL)
21471da177e4SLinus Torvalds 		return -ENOLCK;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	/*
21501da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
21511da177e4SLinus Torvalds 	 */
21521da177e4SLinus Torvalds 	error = -EFAULT;
21531da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
21541da177e4SLinus Torvalds 		goto out;
21551da177e4SLinus Torvalds 
2156496ad9aaSAl Viro 	inode = file_inode(filp);
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
21591da177e4SLinus Torvalds 	 * and shared.
21601da177e4SLinus Torvalds 	 */
2161a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
21621da177e4SLinus Torvalds 		error = -EAGAIN;
21631da177e4SLinus Torvalds 		goto out;
21641da177e4SLinus Torvalds 	}
21651da177e4SLinus Torvalds 
2166c293621bSPeter Staubach again:
21671da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
21681da177e4SLinus Torvalds 	if (error)
21691da177e4SLinus Torvalds 		goto out;
21705d50ffd7SJeff Layton 
2171cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2172cf01f4eeSJeff Layton 	if (error)
2173cf01f4eeSJeff Layton 		goto out;
2174cf01f4eeSJeff Layton 
21755d50ffd7SJeff Layton 	/*
21765d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2177cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
21785d50ffd7SJeff Layton 	 */
21795d50ffd7SJeff Layton 	switch (cmd) {
21800d3f7a2dSJeff Layton 	case F_OFD_SETLK:
218190478939SJeff Layton 		error = -EINVAL;
218290478939SJeff Layton 		if (flock.l_pid != 0)
218390478939SJeff Layton 			goto out;
218490478939SJeff Layton 
21855d50ffd7SJeff Layton 		cmd = F_SETLK;
2186cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
218773a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
21885d50ffd7SJeff Layton 		break;
21890d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
219090478939SJeff Layton 		error = -EINVAL;
219190478939SJeff Layton 		if (flock.l_pid != 0)
219290478939SJeff Layton 			goto out;
219390478939SJeff Layton 
21945d50ffd7SJeff Layton 		cmd = F_SETLKW;
2195cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
219673a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
21975d50ffd7SJeff Layton 		/* Fallthrough */
21985d50ffd7SJeff Layton 	case F_SETLKW:
21991da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
22001da177e4SLinus Torvalds 	}
22011da177e4SLinus Torvalds 
2202b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2203c293621bSPeter Staubach 
2204c293621bSPeter Staubach 	/*
2205c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2206c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2207c293621bSPeter Staubach 	 */
22080b2bac2fSAl Viro 	/*
22090b2bac2fSAl Viro 	 * we need that spin_lock here - it prevents reordering between
22108116bf4cSJeff Layton 	 * update of i_flctx->flc_posix and check for it done in close().
22110b2bac2fSAl Viro 	 * rcu_read_lock() wouldn't do.
22120b2bac2fSAl Viro 	 */
22130b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
22140b2bac2fSAl Viro 	f = fcheck(fd);
22150b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
22160b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2217c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2218c293621bSPeter Staubach 		goto again;
2219c293621bSPeter Staubach 	}
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds out:
22221da177e4SLinus Torvalds 	locks_free_lock(file_lock);
22231da177e4SLinus Torvalds 	return error;
22241da177e4SLinus Torvalds }
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds #if BITS_PER_LONG == 32
22271da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
22281da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
22291da177e4SLinus Torvalds  */
2230c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
22311da177e4SLinus Torvalds {
22329d6a8c5cSMarc Eshel 	struct file_lock file_lock;
22331da177e4SLinus Torvalds 	struct flock64 flock;
22341da177e4SLinus Torvalds 	int error;
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	error = -EFAULT;
22371da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
22381da177e4SLinus Torvalds 		goto out;
22391da177e4SLinus Torvalds 	error = -EINVAL;
22401da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
22411da177e4SLinus Torvalds 		goto out;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
22441da177e4SLinus Torvalds 	if (error)
22451da177e4SLinus Torvalds 		goto out;
22461da177e4SLinus Torvalds 
22470d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
224890478939SJeff Layton 		error = -EINVAL;
224990478939SJeff Layton 		if (flock.l_pid != 0)
225090478939SJeff Layton 			goto out;
225190478939SJeff Layton 
22525d50ffd7SJeff Layton 		cmd = F_GETLK64;
2253cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
225473a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
22555d50ffd7SJeff Layton 	}
22565d50ffd7SJeff Layton 
22573ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
22583ee17abdSJ. Bruce Fields 	if (error)
22591da177e4SLinus Torvalds 		goto out;
22601da177e4SLinus Torvalds 
22619d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
22629d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK)
22639d6a8c5cSMarc Eshel 		posix_lock_to_flock64(&flock, &file_lock);
22649d6a8c5cSMarc Eshel 
22651da177e4SLinus Torvalds 	error = -EFAULT;
22661da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
22671da177e4SLinus Torvalds 		error = 0;
22681da177e4SLinus Torvalds 
2269f328296eSKinglong Mee 	locks_release_private(&file_lock);
22701da177e4SLinus Torvalds out:
22711da177e4SLinus Torvalds 	return error;
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
22751da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
22761da177e4SLinus Torvalds  */
2277c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2278c293621bSPeter Staubach 		struct flock64 __user *l)
22791da177e4SLinus Torvalds {
22801da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
22811da177e4SLinus Torvalds 	struct flock64 flock;
22821da177e4SLinus Torvalds 	struct inode *inode;
22830b2bac2fSAl Viro 	struct file *f;
22841da177e4SLinus Torvalds 	int error;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	if (file_lock == NULL)
22871da177e4SLinus Torvalds 		return -ENOLCK;
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 	/*
22901da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
22911da177e4SLinus Torvalds 	 */
22921da177e4SLinus Torvalds 	error = -EFAULT;
22931da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
22941da177e4SLinus Torvalds 		goto out;
22951da177e4SLinus Torvalds 
2296496ad9aaSAl Viro 	inode = file_inode(filp);
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
22991da177e4SLinus Torvalds 	 * and shared.
23001da177e4SLinus Torvalds 	 */
2301a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
23021da177e4SLinus Torvalds 		error = -EAGAIN;
23031da177e4SLinus Torvalds 		goto out;
23041da177e4SLinus Torvalds 	}
23051da177e4SLinus Torvalds 
2306c293621bSPeter Staubach again:
23071da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
23081da177e4SLinus Torvalds 	if (error)
23091da177e4SLinus Torvalds 		goto out;
23105d50ffd7SJeff Layton 
2311cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2312cf01f4eeSJeff Layton 	if (error)
2313cf01f4eeSJeff Layton 		goto out;
2314cf01f4eeSJeff Layton 
23155d50ffd7SJeff Layton 	/*
23165d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2317cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
23185d50ffd7SJeff Layton 	 */
23195d50ffd7SJeff Layton 	switch (cmd) {
23200d3f7a2dSJeff Layton 	case F_OFD_SETLK:
232190478939SJeff Layton 		error = -EINVAL;
232290478939SJeff Layton 		if (flock.l_pid != 0)
232390478939SJeff Layton 			goto out;
232490478939SJeff Layton 
23255d50ffd7SJeff Layton 		cmd = F_SETLK64;
2326cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
232773a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
23285d50ffd7SJeff Layton 		break;
23290d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
233090478939SJeff Layton 		error = -EINVAL;
233190478939SJeff Layton 		if (flock.l_pid != 0)
233290478939SJeff Layton 			goto out;
233390478939SJeff Layton 
23345d50ffd7SJeff Layton 		cmd = F_SETLKW64;
2335cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
233673a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
23375d50ffd7SJeff Layton 		/* Fallthrough */
23385d50ffd7SJeff Layton 	case F_SETLKW64:
23391da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
23401da177e4SLinus Torvalds 	}
23411da177e4SLinus Torvalds 
2342b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2343c293621bSPeter Staubach 
2344c293621bSPeter Staubach 	/*
2345c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2346c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2347c293621bSPeter Staubach 	 */
23480b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
23490b2bac2fSAl Viro 	f = fcheck(fd);
23500b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
23510b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2352c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2353c293621bSPeter Staubach 		goto again;
2354c293621bSPeter Staubach 	}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds out:
23571da177e4SLinus Torvalds 	locks_free_lock(file_lock);
23581da177e4SLinus Torvalds 	return error;
23591da177e4SLinus Torvalds }
23601da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds /*
23631da177e4SLinus Torvalds  * This function is called when the file is being removed
23641da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
23651da177e4SLinus Torvalds  * are deleted at this time.
23661da177e4SLinus Torvalds  */
23671da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
23681da177e4SLinus Torvalds {
2369ff7b86b8SMiklos Szeredi 	struct file_lock lock;
2370*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	/*
23731da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
23741da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
23751da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
23761da177e4SLinus Torvalds 	 */
2377*128a3785SDmitry Vyukov 	ctx =  smp_load_acquire(&file_inode(filp)->i_flctx);
2378bd61e0a9SJeff Layton 	if (!ctx || list_empty(&ctx->flc_posix))
23791da177e4SLinus Torvalds 		return;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
238275e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
23831da177e4SLinus Torvalds 	lock.fl_start = 0;
23841da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
23851da177e4SLinus Torvalds 	lock.fl_owner = owner;
23861da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
23871da177e4SLinus Torvalds 	lock.fl_file = filp;
23881da177e4SLinus Torvalds 	lock.fl_ops = NULL;
23891da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
23901da177e4SLinus Torvalds 
2391150b3934SMarc Eshel 	vfs_lock_file(filp, F_SETLK, &lock, NULL);
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
23941da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
23981da177e4SLinus Torvalds 
23993d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
2400dd459bb1SJeff Layton static void
2401*128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2402dd459bb1SJeff Layton {
2403dd459bb1SJeff Layton 	struct file_lock fl = {
2404dd459bb1SJeff Layton 		.fl_owner = filp,
2405dd459bb1SJeff Layton 		.fl_pid = current->tgid,
2406dd459bb1SJeff Layton 		.fl_file = filp,
2407dd459bb1SJeff Layton 		.fl_flags = FL_FLOCK,
2408dd459bb1SJeff Layton 		.fl_type = F_UNLCK,
2409dd459bb1SJeff Layton 		.fl_end = OFFSET_MAX,
2410dd459bb1SJeff Layton 	};
2411bcd7f78dSJeff Layton 	struct inode *inode = file_inode(filp);
2412dd459bb1SJeff Layton 
24133d8e560dSJeff Layton 	if (list_empty(&flctx->flc_flock))
2414dd459bb1SJeff Layton 		return;
2415dd459bb1SJeff Layton 
2416dd459bb1SJeff Layton 	if (filp->f_op->flock)
2417dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2418dd459bb1SJeff Layton 	else
2419bcd7f78dSJeff Layton 		flock_lock_inode(inode, &fl);
2420dd459bb1SJeff Layton 
2421dd459bb1SJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
2422dd459bb1SJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2423dd459bb1SJeff Layton }
2424dd459bb1SJeff Layton 
24253d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
24268634b51fSJeff Layton static void
2427*128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
24288634b51fSJeff Layton {
24298634b51fSJeff Layton 	struct file_lock *fl, *tmp;
24308634b51fSJeff Layton 	LIST_HEAD(dispose);
24318634b51fSJeff Layton 
24323d8e560dSJeff Layton 	if (list_empty(&ctx->flc_lease))
24338634b51fSJeff Layton 		return;
24348634b51fSJeff Layton 
24356109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
24368634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2437c4e136cdSJeff Layton 		if (filp == fl->fl_file)
24387448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, &dispose);
24396109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
24408634b51fSJeff Layton 	locks_dispose_list(&dispose);
24418634b51fSJeff Layton }
24428634b51fSJeff Layton 
24431da177e4SLinus Torvalds /*
24441da177e4SLinus Torvalds  * This function is called on the last close of an open file.
24451da177e4SLinus Torvalds  */
244678ed8a13SJeff Layton void locks_remove_file(struct file *filp)
24471da177e4SLinus Torvalds {
2448*128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2449*128a3785SDmitry Vyukov 
2450*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&file_inode(filp)->i_flctx);
2451*128a3785SDmitry Vyukov 	if (!ctx)
24523d8e560dSJeff Layton 		return;
24533d8e560dSJeff Layton 
2454dd459bb1SJeff Layton 	/* remove any OFD locks */
245573a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
24565d50ffd7SJeff Layton 
2457dd459bb1SJeff Layton 	/* remove flock locks */
2458*128a3785SDmitry Vyukov 	locks_remove_flock(filp, ctx);
2459dd459bb1SJeff Layton 
24608634b51fSJeff Layton 	/* remove any leases */
2461*128a3785SDmitry Vyukov 	locks_remove_lease(filp, ctx);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds /**
24651da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
24661da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
24671da177e4SLinus Torvalds  *
24681da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
24691da177e4SLinus Torvalds  */
247064a318eeSJ. Bruce Fields int
2471f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter)
24721da177e4SLinus Torvalds {
247364a318eeSJ. Bruce Fields 	int status = 0;
247464a318eeSJ. Bruce Fields 
24757b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
24765996a298SJ. Bruce Fields 	if (waiter->fl_next)
24771da177e4SLinus Torvalds 		__locks_delete_block(waiter);
247864a318eeSJ. Bruce Fields 	else
247964a318eeSJ. Bruce Fields 		status = -ENOENT;
24807b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
248164a318eeSJ. Bruce Fields 	return status;
24821da177e4SLinus Torvalds }
24831da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
24841da177e4SLinus Torvalds 
24859b9d2ab4SMarc Eshel /**
24869b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
24879b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
24889b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
24899b9d2ab4SMarc Eshel  *
24909b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
24919b9d2ab4SMarc Eshel  */
24929b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
24939b9d2ab4SMarc Eshel {
249472c2d531SAl Viro 	if (filp->f_op->lock)
24959b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
24969b9d2ab4SMarc Eshel 	return 0;
24979b9d2ab4SMarc Eshel }
24989b9d2ab4SMarc Eshel 
24999b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
25009b9d2ab4SMarc Eshel 
25017f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2502d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
25037f8ada98SPavel Emelyanov #include <linux/seq_file.h>
25047f8ada98SPavel Emelyanov 
25057012b02aSJeff Layton struct locks_iterator {
25067012b02aSJeff Layton 	int	li_cpu;
25077012b02aSJeff Layton 	loff_t	li_pos;
25087012b02aSJeff Layton };
25097012b02aSJeff Layton 
25107f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
251199dc8292SJerome Marchand 			    loff_t id, char *pfx)
25121da177e4SLinus Torvalds {
25131da177e4SLinus Torvalds 	struct inode *inode = NULL;
2514ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2515ab1f1611SVitaliy Gusev 
2516ab1f1611SVitaliy Gusev 	if (fl->fl_nspid)
25176c5f3e7bSPavel Emelyanov 		fl_pid = pid_vnr(fl->fl_nspid);
2518ab1f1611SVitaliy Gusev 	else
2519ab1f1611SVitaliy Gusev 		fl_pid = fl->fl_pid;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2522496ad9aaSAl Viro 		inode = file_inode(fl->fl_file);
25231da177e4SLinus Torvalds 
252499dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
25251da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2526c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
25275315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2528cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
25295315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2530c918d42aSJeff Layton 		else
25315315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2532c918d42aSJeff Layton 
2533c918d42aSJeff Layton 		seq_printf(f, " %s ",
25341da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2535a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
25361da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
25371da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
25385315c26aSFabian Frederick 			seq_puts(f, "FLOCK  MSNFS     ");
25391da177e4SLinus Torvalds 		} else {
25405315c26aSFabian Frederick 			seq_puts(f, "FLOCK  ADVISORY  ");
25411da177e4SLinus Torvalds 		}
25421da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
25438144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
25448144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
25458144f1f6SJeff Layton 		else
25465315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
25478144f1f6SJeff Layton 
2548ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
25495315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
25501da177e4SLinus Torvalds 		else if (fl->fl_file)
25515315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
25521da177e4SLinus Torvalds 		else
25535315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
25541da177e4SLinus Torvalds 	} else {
25555315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
25561da177e4SLinus Torvalds 	}
25571da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
25587f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
25591da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
25601da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
25611da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
25621da177e4SLinus Torvalds 	} else {
25637f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2564ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
25650ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
25660ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
25671da177e4SLinus Torvalds 	}
25681da177e4SLinus Torvalds 	if (inode) {
25693648888eSJeff Layton 		/* userspace relies on this representation of dev_t */
2570ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
25711da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
25721da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
25731da177e4SLinus Torvalds 	} else {
2574ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
25751da177e4SLinus Torvalds 	}
25761da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
25771da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
25787f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
25791da177e4SLinus Torvalds 		else
25807f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
25811da177e4SLinus Torvalds 	} else {
25825315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
25831da177e4SLinus Torvalds 	}
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
25867f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
25871da177e4SLinus Torvalds {
25887012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
25897f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
25907f8ada98SPavel Emelyanov 
2591139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
25927f8ada98SPavel Emelyanov 
25937012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
25947f8ada98SPavel Emelyanov 
25957f8ada98SPavel Emelyanov 	list_for_each_entry(bfl, &fl->fl_block, fl_block)
25967012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
25977f8ada98SPavel Emelyanov 
25987f8ada98SPavel Emelyanov 	return 0;
25991da177e4SLinus Torvalds }
26001da177e4SLinus Torvalds 
26016c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f,
26026c8c9031SAndrey Vagin 			struct list_head *head, int *id,
26036c8c9031SAndrey Vagin 			struct file *filp, struct files_struct *files)
26046c8c9031SAndrey Vagin {
26056c8c9031SAndrey Vagin 	struct file_lock *fl;
26066c8c9031SAndrey Vagin 
26076c8c9031SAndrey Vagin 	list_for_each_entry(fl, head, fl_list) {
26086c8c9031SAndrey Vagin 
26096c8c9031SAndrey Vagin 		if (filp != fl->fl_file)
26106c8c9031SAndrey Vagin 			continue;
26116c8c9031SAndrey Vagin 		if (fl->fl_owner != files &&
26126c8c9031SAndrey Vagin 		    fl->fl_owner != filp)
26136c8c9031SAndrey Vagin 			continue;
26146c8c9031SAndrey Vagin 
26156c8c9031SAndrey Vagin 		(*id)++;
26166c8c9031SAndrey Vagin 		seq_puts(f, "lock:\t");
26176c8c9031SAndrey Vagin 		lock_get_status(f, fl, *id, "");
26186c8c9031SAndrey Vagin 	}
26196c8c9031SAndrey Vagin }
26206c8c9031SAndrey Vagin 
26216c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f,
26226c8c9031SAndrey Vagin 		  struct file *filp, struct files_struct *files)
26236c8c9031SAndrey Vagin {
26246c8c9031SAndrey Vagin 	struct inode *inode = file_inode(filp);
26256c8c9031SAndrey Vagin 	struct file_lock_context *ctx;
26266c8c9031SAndrey Vagin 	int id = 0;
26276c8c9031SAndrey Vagin 
2628*128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
26296c8c9031SAndrey Vagin 	if (!ctx)
26306c8c9031SAndrey Vagin 		return;
26316c8c9031SAndrey Vagin 
26326c8c9031SAndrey Vagin 	spin_lock(&ctx->flc_lock);
26336c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
26346c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
26356c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
26366c8c9031SAndrey Vagin 	spin_unlock(&ctx->flc_lock);
26376c8c9031SAndrey Vagin }
26386c8c9031SAndrey Vagin 
26397f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2640b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
26411da177e4SLinus Torvalds {
26427012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
264399dc8292SJerome Marchand 
26447012b02aSJeff Layton 	iter->li_pos = *pos + 1;
26457012b02aSJeff Layton 	lg_global_lock(&file_lock_lglock);
26467b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
26477012b02aSJeff Layton 	return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
26481da177e4SLinus Torvalds }
26497f8ada98SPavel Emelyanov 
26507f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
26517f8ada98SPavel Emelyanov {
26527012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
26537012b02aSJeff Layton 
26547012b02aSJeff Layton 	++iter->li_pos;
26557012b02aSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
26561da177e4SLinus Torvalds }
26577f8ada98SPavel Emelyanov 
26587f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2659b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
26607f8ada98SPavel Emelyanov {
26617b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
26627012b02aSJeff Layton 	lg_global_unlock(&file_lock_lglock);
26631da177e4SLinus Torvalds }
26641da177e4SLinus Torvalds 
2665d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
26667f8ada98SPavel Emelyanov 	.start	= locks_start,
26677f8ada98SPavel Emelyanov 	.next	= locks_next,
26687f8ada98SPavel Emelyanov 	.stop	= locks_stop,
26697f8ada98SPavel Emelyanov 	.show	= locks_show,
26707f8ada98SPavel Emelyanov };
2671d8ba7a36SAlexey Dobriyan 
2672d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp)
2673d8ba7a36SAlexey Dobriyan {
26747012b02aSJeff Layton 	return seq_open_private(filp, &locks_seq_operations,
26757012b02aSJeff Layton 					sizeof(struct locks_iterator));
2676d8ba7a36SAlexey Dobriyan }
2677d8ba7a36SAlexey Dobriyan 
2678d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = {
2679d8ba7a36SAlexey Dobriyan 	.open		= locks_open,
2680d8ba7a36SAlexey Dobriyan 	.read		= seq_read,
2681d8ba7a36SAlexey Dobriyan 	.llseek		= seq_lseek,
268299dc8292SJerome Marchand 	.release	= seq_release_private,
2683d8ba7a36SAlexey Dobriyan };
2684d8ba7a36SAlexey Dobriyan 
2685d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2686d8ba7a36SAlexey Dobriyan {
2687d8ba7a36SAlexey Dobriyan 	proc_create("locks", 0, NULL, &proc_locks_operations);
2688d8ba7a36SAlexey Dobriyan 	return 0;
2689d8ba7a36SAlexey Dobriyan }
2690d8ba7a36SAlexey Dobriyan module_init(proc_locks_init);
26917f8ada98SPavel Emelyanov #endif
26927f8ada98SPavel Emelyanov 
26931da177e4SLinus Torvalds static int __init filelock_init(void)
26941da177e4SLinus Torvalds {
26957012b02aSJeff Layton 	int i;
26967012b02aSJeff Layton 
26974a075e39SJeff Layton 	flctx_cache = kmem_cache_create("file_lock_ctx",
26984a075e39SJeff Layton 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
26994a075e39SJeff Layton 
27001da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2701ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2702ee19cc40SMiklos Szeredi 
27037012b02aSJeff Layton 	lg_lock_init(&file_lock_lglock, "file_lock_lglock");
27047012b02aSJeff Layton 
27057012b02aSJeff Layton 	for_each_possible_cpu(i)
27067012b02aSJeff Layton 		INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
27077012b02aSJeff Layton 
27081da177e4SLinus Torvalds 	return 0;
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds core_initcall(filelock_init);
2712