xref: /linux/fs/locks.c (revision 2ab99ee12440e66ec1efd2a98599010471de785e)
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)
140617588d5SJ. Bruce Fields #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG))
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 *
2064a075e39SJeff Layton locks_get_lock_context(struct inode *inode)
2074a075e39SJeff Layton {
2084a075e39SJeff Layton 	struct file_lock_context *new;
2094a075e39SJeff Layton 
2104a075e39SJeff Layton 	if (likely(inode->i_flctx))
2114a075e39SJeff Layton 		goto out;
2124a075e39SJeff Layton 
2134a075e39SJeff Layton 	new = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
2144a075e39SJeff Layton 	if (!new)
2154a075e39SJeff Layton 		goto out;
2164a075e39SJeff Layton 
2176109c850SJeff Layton 	spin_lock_init(&new->flc_lock);
2184a075e39SJeff Layton 	INIT_LIST_HEAD(&new->flc_flock);
219bd61e0a9SJeff Layton 	INIT_LIST_HEAD(&new->flc_posix);
2208634b51fSJeff Layton 	INIT_LIST_HEAD(&new->flc_lease);
2214a075e39SJeff Layton 
2224a075e39SJeff Layton 	/*
2234a075e39SJeff Layton 	 * Assign the pointer if it's not already assigned. If it is, then
2244a075e39SJeff Layton 	 * free the context we just allocated.
2254a075e39SJeff Layton 	 */
2264a075e39SJeff Layton 	spin_lock(&inode->i_lock);
2274a075e39SJeff Layton 	if (likely(!inode->i_flctx)) {
2284a075e39SJeff Layton 		inode->i_flctx = new;
2294a075e39SJeff Layton 		new = NULL;
2304a075e39SJeff Layton 	}
2314a075e39SJeff Layton 	spin_unlock(&inode->i_lock);
2324a075e39SJeff Layton 
2334a075e39SJeff Layton 	if (new)
2344a075e39SJeff Layton 		kmem_cache_free(flctx_cache, new);
2354a075e39SJeff Layton out:
2364a075e39SJeff Layton 	return inode->i_flctx;
2374a075e39SJeff Layton }
2384a075e39SJeff Layton 
2394a075e39SJeff Layton void
2404a075e39SJeff Layton locks_free_lock_context(struct file_lock_context *ctx)
2414a075e39SJeff Layton {
2424a075e39SJeff Layton 	if (ctx) {
2434a075e39SJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_flock));
244bd61e0a9SJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_posix));
2458634b51fSJeff Layton 		WARN_ON_ONCE(!list_empty(&ctx->flc_lease));
2464a075e39SJeff Layton 		kmem_cache_free(flctx_cache, ctx);
2474a075e39SJeff Layton 	}
2484a075e39SJeff Layton }
2494a075e39SJeff Layton 
250ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
251a51cb91dSMiklos Szeredi {
252139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
2536dee60f6SJeff Layton 	INIT_LIST_HEAD(&fl->fl_list);
254ee19cc40SMiklos Szeredi 	INIT_LIST_HEAD(&fl->fl_block);
255ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
256a51cb91dSMiklos Szeredi }
257a51cb91dSMiklos Szeredi 
2581da177e4SLinus Torvalds /* Allocate an empty lock structure. */
259c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2601da177e4SLinus Torvalds {
261ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
262a51cb91dSMiklos Szeredi 
263a51cb91dSMiklos Szeredi 	if (fl)
264ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
265a51cb91dSMiklos Szeredi 
266a51cb91dSMiklos Szeredi 	return fl;
2671da177e4SLinus Torvalds }
268c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2691da177e4SLinus Torvalds 
270a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
27147831f35STrond Myklebust {
27247831f35STrond Myklebust 	if (fl->fl_ops) {
27347831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
27447831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
27547831f35STrond Myklebust 		fl->fl_ops = NULL;
27647831f35STrond Myklebust 	}
27747831f35STrond Myklebust 
2785c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
2795c97d7b1SKinglong Mee 		if (fl->fl_lmops->lm_put_owner)
2805c97d7b1SKinglong Mee 			fl->fl_lmops->lm_put_owner(fl);
2815c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
2825c97d7b1SKinglong Mee 	}
28347831f35STrond Myklebust }
284a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
28547831f35STrond Myklebust 
2861da177e4SLinus Torvalds /* Free a lock which is not in use. */
28705fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
2881da177e4SLinus Torvalds {
2895ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
2906dee60f6SJeff Layton 	BUG_ON(!list_empty(&fl->fl_list));
2915ce29646SMiklos Szeredi 	BUG_ON(!list_empty(&fl->fl_block));
292139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2931da177e4SLinus Torvalds 
29447831f35STrond Myklebust 	locks_release_private(fl);
2951da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
2961da177e4SLinus Torvalds }
29705fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
2981da177e4SLinus Torvalds 
299ed9814d8SJeff Layton static void
300ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
301ed9814d8SJeff Layton {
302ed9814d8SJeff Layton 	struct file_lock *fl;
303ed9814d8SJeff Layton 
304ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
3056dee60f6SJeff Layton 		fl = list_first_entry(dispose, struct file_lock, fl_list);
3066dee60f6SJeff Layton 		list_del_init(&fl->fl_list);
307ed9814d8SJeff Layton 		locks_free_lock(fl);
308ed9814d8SJeff Layton 	}
309ed9814d8SJeff Layton }
310ed9814d8SJeff Layton 
3111da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
3121da177e4SLinus Torvalds {
313ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
314ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
3211da177e4SLinus Torvalds  */
3223fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
3251da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
3260996905fSTrond Myklebust 	new->fl_file = NULL;
3271da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
3281da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
3291da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
3301da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
331f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
3320996905fSTrond Myklebust 	new->fl_ops = NULL;
333f328296eSKinglong Mee 
334f328296eSKinglong Mee 	if (fl->fl_lmops) {
335f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
336f328296eSKinglong Mee 			fl->fl_lmops->lm_get_owner(new, fl);
337f328296eSKinglong Mee 	}
3380996905fSTrond Myklebust }
3393fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
3400996905fSTrond Myklebust 
3410996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
3420996905fSTrond Myklebust {
343566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
344566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
3450996905fSTrond Myklebust 
3463fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
347f328296eSKinglong Mee 
3480996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
3491da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
35047831f35STrond Myklebust 
351f328296eSKinglong Mee 	if (fl->fl_ops) {
352f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
353f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
354f328296eSKinglong Mee 	}
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
3601da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
3611da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
3621da177e4SLinus Torvalds 	switch (cmd) {
3631da177e4SLinus Torvalds 	case LOCK_SH:
3641da177e4SLinus Torvalds 		return F_RDLCK;
3651da177e4SLinus Torvalds 	case LOCK_EX:
3661da177e4SLinus Torvalds 		return F_WRLCK;
3671da177e4SLinus Torvalds 	case LOCK_UN:
3681da177e4SLinus Torvalds 		return F_UNLCK;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 	return -EINVAL;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
3746e129d00SJeff Layton static struct file_lock *
3756e129d00SJeff Layton flock_make_lock(struct file *filp, unsigned int cmd)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct file_lock *fl;
3781da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
3796e129d00SJeff Layton 
3801da177e4SLinus Torvalds 	if (type < 0)
3816e129d00SJeff Layton 		return ERR_PTR(type);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	fl = locks_alloc_lock();
3841da177e4SLinus Torvalds 	if (fl == NULL)
3856e129d00SJeff Layton 		return ERR_PTR(-ENOMEM);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	fl->fl_file = filp;
38873a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
3891da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
3901da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
3911da177e4SLinus Torvalds 	fl->fl_type = type;
3921da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
3931da177e4SLinus Torvalds 
3946e129d00SJeff Layton 	return fl;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3970ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	switch (type) {
4001da177e4SLinus Torvalds 	case F_RDLCK:
4011da177e4SLinus Torvalds 	case F_WRLCK:
4021da177e4SLinus Torvalds 	case F_UNLCK:
4031da177e4SLinus Torvalds 		fl->fl_type = type;
4041da177e4SLinus Torvalds 		break;
4051da177e4SLinus Torvalds 	default:
4061da177e4SLinus Torvalds 		return -EINVAL;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 	return 0;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
411ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
412ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
413ef12e72aSJ. Bruce Fields {
414ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
415ef12e72aSJ. Bruce Fields 	case SEEK_SET:
416ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
417ef12e72aSJ. Bruce Fields 		break;
418ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
419ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
420ef12e72aSJ. Bruce Fields 		break;
421ef12e72aSJ. Bruce Fields 	case SEEK_END:
422ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
423ef12e72aSJ. Bruce Fields 		break;
424ef12e72aSJ. Bruce Fields 	default:
425ef12e72aSJ. Bruce Fields 		return -EINVAL;
426ef12e72aSJ. Bruce Fields 	}
427ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
428ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
429ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
430ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
431ef12e72aSJ. Bruce Fields 		return -EINVAL;
432ef12e72aSJ. Bruce Fields 
433ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
434ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
435ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
436ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
437ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
438ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
439ef12e72aSJ. Bruce Fields 
440ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
441ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
442ef12e72aSJ. Bruce Fields 			return -EINVAL;
443ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
444ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
445ef12e72aSJ. Bruce Fields 	} else
446ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
447ef12e72aSJ. Bruce Fields 
448ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
449ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
450ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
451ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
452ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
453ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
454ef12e72aSJ. Bruce Fields 
455ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
456ef12e72aSJ. Bruce Fields }
457ef12e72aSJ. Bruce Fields 
4581da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
4591da177e4SLinus Torvalds  * style lock.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
4621da177e4SLinus Torvalds 			       struct flock *l)
4631da177e4SLinus Torvalds {
464ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
465ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
466ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
467ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
468ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
469ef12e72aSJ. Bruce Fields 	};
4701da177e4SLinus Torvalds 
471ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /* default lease lock manager operations */
4754d01b7f5SJeff Layton static bool
4764d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4794d01b7f5SJeff Layton 	return false;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821c7dd2ffSJeff Layton static void
4831c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv)
4841c7dd2ffSJeff Layton {
4851c7dd2ffSJeff Layton 	struct file *filp = fl->fl_file;
4861c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
4871c7dd2ffSJeff Layton 
4881c7dd2ffSJeff Layton 	/*
4891c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
4901c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
4911c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
4921c7dd2ffSJeff Layton 	 */
4931c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
4941c7dd2ffSJeff Layton 		*priv = NULL;
4951c7dd2ffSJeff Layton 
4961c7dd2ffSJeff Layton 	__f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
4971c7dd2ffSJeff Layton }
4981c7dd2ffSJeff Layton 
4997b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
5008fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
5018fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
5021c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
5031da177e4SLinus Torvalds };
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /*
5061da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
5071da177e4SLinus Torvalds  */
5080ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
5091da177e4SLinus Torvalds  {
51075dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
51175dff55aSTrond Myklebust 		return -EINVAL;
51275dff55aSTrond Myklebust 
5137ca76311SJeff Layton 	fl->fl_owner = filp;
5141da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	fl->fl_file = filp;
5171da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
5181da177e4SLinus Torvalds 	fl->fl_start = 0;
5191da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
5201da177e4SLinus Torvalds 	fl->fl_ops = NULL;
5211da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
5221da177e4SLinus Torvalds 	return 0;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
5260ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
52975dff55aSTrond Myklebust 	int error = -ENOMEM;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	if (fl == NULL)
532e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
53575dff55aSTrond Myklebust 	if (error) {
53675dff55aSTrond Myklebust 		locks_free_lock(fl);
537e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
53875dff55aSTrond Myklebust 	}
539e32b8ee2SJ. Bruce Fields 	return fl;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /* Check if two locks overlap each other.
5431da177e4SLinus Torvalds  */
5441da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
5471da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /*
5511da177e4SLinus Torvalds  * Check whether two locks have the same owner.
5521da177e4SLinus Torvalds  */
55333443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
5541da177e4SLinus Torvalds {
5558fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
5561da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
5578fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
5581da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5616109c850SJeff Layton /* Must be called with the flc_lock held! */
5626ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
56388974691SJeff Layton {
5647012b02aSJeff Layton 	lg_local_lock(&file_lock_lglock);
5657012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
5667012b02aSJeff Layton 	hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
5677012b02aSJeff Layton 	lg_local_unlock(&file_lock_lglock);
56888974691SJeff Layton }
56988974691SJeff Layton 
5706109c850SJeff Layton /* Must be called with the flc_lock held! */
5716ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
57288974691SJeff Layton {
5737012b02aSJeff Layton 	/*
5747012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
5756109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
5767012b02aSJeff Layton 	 * also require that it be held.
5777012b02aSJeff Layton 	 */
5787012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
5797012b02aSJeff Layton 		return;
5807012b02aSJeff Layton 	lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
581139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
5827012b02aSJeff Layton 	lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
58388974691SJeff Layton }
58488974691SJeff Layton 
5853999e493SJeff Layton static unsigned long
5863999e493SJeff Layton posix_owner_key(struct file_lock *fl)
5873999e493SJeff Layton {
5883999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
5893999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
5903999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
5913999e493SJeff Layton }
5923999e493SJeff Layton 
5936ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
59488974691SJeff Layton {
5953999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
59688974691SJeff Layton }
59788974691SJeff Layton 
5986ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
59988974691SJeff Layton {
60048f74186SJeff Layton 	hash_del(&waiter->fl_link);
60188974691SJeff Layton }
60288974691SJeff Layton 
6031da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
6041da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
6051c8c601aSJeff Layton  *
6067b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
6071da177e4SLinus Torvalds  */
60833443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
6091da177e4SLinus Torvalds {
61088974691SJeff Layton 	locks_delete_global_blocked(waiter);
6111da177e4SLinus Torvalds 	list_del_init(&waiter->fl_block);
6121da177e4SLinus Torvalds 	waiter->fl_next = NULL;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151a9e64a7SJeff Layton static void locks_delete_block(struct file_lock *waiter)
6161da177e4SLinus Torvalds {
6177b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6181da177e4SLinus Torvalds 	__locks_delete_block(waiter);
6197b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
6231da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
6241da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
6251da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
6261c8c601aSJeff Layton  *
6276109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
6286109c850SJeff Layton  * fl_block list itself is protected by the blocked_lock_lock, but by ensuring
6296109c850SJeff Layton  * that the flc_lock is also held on insertions we can avoid taking the
6306109c850SJeff Layton  * blocked_lock_lock in some cases when we see that the fl_block list is empty.
6311da177e4SLinus Torvalds  */
6321c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
6331da177e4SLinus Torvalds 					struct file_lock *waiter)
6341da177e4SLinus Torvalds {
6356dc0fe8fSJ. Bruce Fields 	BUG_ON(!list_empty(&waiter->fl_block));
6361da177e4SLinus Torvalds 	waiter->fl_next = blocker;
63788974691SJeff Layton 	list_add_tail(&waiter->fl_block, &blocker->fl_block);
638cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
6391c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
6401c8c601aSJeff Layton }
6411c8c601aSJeff Layton 
6426109c850SJeff Layton /* Must be called with flc_lock held. */
6431c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
6441c8c601aSJeff Layton 					struct file_lock *waiter)
6451c8c601aSJeff Layton {
6467b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6471c8c601aSJeff Layton 	__locks_insert_block(blocker, waiter);
6487b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511cb36012SJeff Layton /*
6521cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
6531cb36012SJeff Layton  *
6546109c850SJeff Layton  * Must be called with the inode->flc_lock held!
6551da177e4SLinus Torvalds  */
6561da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
6571da177e4SLinus Torvalds {
6584e8c765dSJeff Layton 	/*
6594e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
6606109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
6616109c850SJeff Layton 	 * the flc_lock is always held here. Note that removal from the fl_block
6626109c850SJeff Layton 	 * list does not require the flc_lock, so we must recheck list_empty()
6637b2296afSJeff Layton 	 * after acquiring the blocked_lock_lock.
6644e8c765dSJeff Layton 	 */
6654e8c765dSJeff Layton 	if (list_empty(&blocker->fl_block))
6664e8c765dSJeff Layton 		return;
6674e8c765dSJeff Layton 
6687b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
6691da177e4SLinus Torvalds 	while (!list_empty(&blocker->fl_block)) {
670f0c1cd0eSPavel Emelyanov 		struct file_lock *waiter;
671f0c1cd0eSPavel Emelyanov 
672f0c1cd0eSPavel Emelyanov 		waiter = list_first_entry(&blocker->fl_block,
6731da177e4SLinus Torvalds 				struct file_lock, fl_block);
6741da177e4SLinus Torvalds 		__locks_delete_block(waiter);
6758fb47a4fSJ. Bruce Fields 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
6768fb47a4fSJ. Bruce Fields 			waiter->fl_lmops->lm_notify(waiter);
6771da177e4SLinus Torvalds 		else
6781da177e4SLinus Torvalds 			wake_up(&waiter->fl_wait);
6791da177e4SLinus Torvalds 	}
6807b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6835263e31eSJeff Layton static void
6849bd0f45bSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, int *counter,
6859bd0f45bSJeff Layton 		      struct list_head *before)
6865263e31eSJeff Layton {
6875263e31eSJeff Layton 	fl->fl_nspid = get_pid(task_tgid(current));
6885263e31eSJeff Layton 	list_add_tail(&fl->fl_list, before);
6899bd0f45bSJeff Layton 	++*counter;
6905263e31eSJeff Layton 	locks_insert_global_locks(fl);
6915263e31eSJeff Layton }
6925263e31eSJeff Layton 
6938634b51fSJeff Layton static void
6949bd0f45bSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl, int *counter)
6951da177e4SLinus Torvalds {
69688974691SJeff Layton 	locks_delete_global_locks(fl);
6978634b51fSJeff Layton 	list_del_init(&fl->fl_list);
6989bd0f45bSJeff Layton 	--*counter;
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
7079bd0f45bSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, int *counter,
7089bd0f45bSJeff Layton 		      struct list_head *dispose)
7095263e31eSJeff Layton {
7109bd0f45bSJeff Layton 	locks_unlink_lock_ctx(fl, counter);
7118634b51fSJeff Layton 	if (dispose)
7128634b51fSJeff Layton 		list_add(&fl->fl_list, dispose);
7138634b51fSJeff Layton 	else
7148634b51fSJeff Layton 		locks_free_lock(fl);
7155263e31eSJeff Layton }
7165263e31eSJeff Layton 
7171da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
7181da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
7191da177e4SLinus Torvalds  */
7201da177e4SLinus Torvalds static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
7231da177e4SLinus Torvalds 		return 1;
7241da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
7251da177e4SLinus Torvalds 		return 1;
7261da177e4SLinus Torvalds 	return 0;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
7301da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7311da177e4SLinus Torvalds  */
7321da177e4SLinus Torvalds static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
7351da177e4SLinus Torvalds 	 * each other.
7361da177e4SLinus Torvalds 	 */
7371da177e4SLinus Torvalds 	if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
7381da177e4SLinus Torvalds 		return (0);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* Check whether they overlap */
7411da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
7421da177e4SLinus Torvalds 		return 0;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
7481da177e4SLinus Torvalds  * checking before calling the locks_conflict().
7491da177e4SLinus Torvalds  */
7501da177e4SLinus Torvalds static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
7531da177e4SLinus Torvalds 	 * each other.
7541da177e4SLinus Torvalds 	 */
7551da177e4SLinus Torvalds 	if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
7561da177e4SLinus Torvalds 		return (0);
7571da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
7581da177e4SLinus Torvalds 		return 0;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	return (locks_conflict(caller_fl, sys_fl));
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7636d34ac19SJ. Bruce Fields void
7649d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	struct file_lock *cfl;
767bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
7681c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
7691da177e4SLinus Torvalds 
770bd61e0a9SJeff Layton 	ctx = inode->i_flctx;
771bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
772bd61e0a9SJeff Layton 		fl->fl_type = F_UNLCK;
773bd61e0a9SJeff Layton 		return;
7741da177e4SLinus Torvalds 	}
775bd61e0a9SJeff Layton 
7766109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
777bd61e0a9SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
778bd61e0a9SJeff Layton 		if (posix_locks_conflict(fl, cfl)) {
7793fe0fff1SKinglong Mee 			locks_copy_conflock(fl, cfl);
780ab1f1611SVitaliy Gusev 			if (cfl->fl_nspid)
7816c5f3e7bSPavel Emelyanov 				fl->fl_pid = pid_vnr(cfl->fl_nspid);
782bd61e0a9SJeff Layton 			goto out;
783bd61e0a9SJeff Layton 		}
784bd61e0a9SJeff Layton 	}
785129a84deSJ. Bruce Fields 	fl->fl_type = F_UNLCK;
786bd61e0a9SJeff Layton out:
7876109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
7886d34ac19SJ. Bruce Fields 	return;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
7911da177e4SLinus Torvalds 
792b533184fSJ. Bruce Fields /*
793b533184fSJ. Bruce Fields  * Deadlock detection:
7941da177e4SLinus Torvalds  *
795b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
796b533184fSJ. Bruce Fields  * locks.
7971da177e4SLinus Torvalds  *
798b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
799b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
800b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
801b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
802b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
803b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
804b533184fSJ. Bruce Fields  * cycle.
80597855b49SJ. Bruce Fields  *
806b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
807b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
808b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
809b533184fSJ. Bruce Fields  *
810b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
811b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
812b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
813b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
81457b65325SJeff Layton  *
815cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
81657b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
81757b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
81857b65325SJeff Layton  * skip it for those.
81957b65325SJeff Layton  *
820cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
82157b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
82257b65325SJeff Layton  * upgrade from read to write locks on the same inode.
8231da177e4SLinus Torvalds  */
82497855b49SJ. Bruce Fields 
82597855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
82697855b49SJ. Bruce Fields 
827b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
828b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
829b533184fSJ. Bruce Fields {
830b533184fSJ. Bruce Fields 	struct file_lock *fl;
831b533184fSJ. Bruce Fields 
8323999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
833b533184fSJ. Bruce Fields 		if (posix_same_owner(fl, block_fl))
834b533184fSJ. Bruce Fields 			return fl->fl_next;
835b533184fSJ. Bruce Fields 	}
836b533184fSJ. Bruce Fields 	return NULL;
837b533184fSJ. Bruce Fields }
838b533184fSJ. Bruce Fields 
8397b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
840b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
8411da177e4SLinus Torvalds 				struct file_lock *block_fl)
8421da177e4SLinus Torvalds {
84397855b49SJ. Bruce Fields 	int i = 0;
8441da177e4SLinus Torvalds 
84557b65325SJeff Layton 	/*
84657b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
847cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
84857b65325SJeff Layton 	 */
849cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
85057b65325SJeff Layton 		return 0;
85157b65325SJeff Layton 
852b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
85397855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
85497855b49SJ. Bruce Fields 			return 0;
855b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
856b533184fSJ. Bruce Fields 			return 1;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 	return 0;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
86202888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
863f475ae95STrond Myklebust  *
864f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
865f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
866f475ae95STrond Myklebust  * value for -ENOENT.
8671da177e4SLinus Torvalds  */
868993dfa87STrond Myklebust static int flock_lock_file(struct file *filp, struct file_lock *request)
8691da177e4SLinus Torvalds {
870993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
8715263e31eSJeff Layton 	struct file_lock *fl;
8725263e31eSJeff Layton 	struct file_lock_context *ctx;
873496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
8741da177e4SLinus Torvalds 	int error = 0;
8755263e31eSJeff Layton 	bool found = false;
876ed9814d8SJeff Layton 	LIST_HEAD(dispose);
8771da177e4SLinus Torvalds 
8785263e31eSJeff Layton 	ctx = locks_get_lock_context(inode);
8795263e31eSJeff Layton 	if (!ctx)
8805263e31eSJeff Layton 		return -ENOMEM;
8815263e31eSJeff Layton 
882b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
883b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
884b89f4321SArnd Bergmann 		if (!new_fl)
885b89f4321SArnd Bergmann 			return -ENOMEM;
886b89f4321SArnd Bergmann 	}
887b89f4321SArnd Bergmann 
8886109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
889f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
890f07f18ddSTrond Myklebust 		goto find_conflict;
89184d535adSPavel Emelyanov 
8925263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
8931da177e4SLinus Torvalds 		if (filp != fl->fl_file)
8941da177e4SLinus Torvalds 			continue;
895993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
8961da177e4SLinus Torvalds 			goto out;
8975263e31eSJeff Layton 		found = true;
8989bd0f45bSJeff Layton 		locks_delete_lock_ctx(fl, &ctx->flc_flock_cnt, &dispose);
8991da177e4SLinus Torvalds 		break;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
902f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
903f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
904f475ae95STrond Myklebust 			error = -ENOENT;
905993dfa87STrond Myklebust 		goto out;
906f475ae95STrond Myklebust 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	/*
9091da177e4SLinus Torvalds 	 * If a higher-priority process was blocked on the old file lock,
9101da177e4SLinus Torvalds 	 * give it the opportunity to lock the file.
9111da177e4SLinus Torvalds 	 */
912b89f4321SArnd Bergmann 	if (found) {
9136109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
914def01bc5SFrederic Weisbecker 		cond_resched();
9156109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
916b89f4321SArnd Bergmann 	}
9171da177e4SLinus Torvalds 
918f07f18ddSTrond Myklebust find_conflict:
9195263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
920993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
9211da177e4SLinus Torvalds 			continue;
9221da177e4SLinus Torvalds 		error = -EAGAIN;
923bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
924bde74e4bSMiklos Szeredi 			goto out;
925bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
926993dfa87STrond Myklebust 		locks_insert_block(fl, request);
9271da177e4SLinus Torvalds 		goto out;
9281da177e4SLinus Torvalds 	}
929f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
930f07f18ddSTrond Myklebust 		goto out;
931993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
9329bd0f45bSJeff Layton 	locks_insert_lock_ctx(new_fl, &ctx->flc_flock_cnt, &ctx->flc_flock);
933993dfa87STrond Myklebust 	new_fl = NULL;
9349cedc194SKirill Korotaev 	error = 0;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds out:
9376109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
938993dfa87STrond Myklebust 	if (new_fl)
939993dfa87STrond Myklebust 		locks_free_lock(new_fl);
940ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
9411da177e4SLinus Torvalds 	return error;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944150b3934SMarc Eshel static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
9451da177e4SLinus Torvalds {
946bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
94739005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
94839005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
9491da177e4SLinus Torvalds 	struct file_lock *left = NULL;
9501da177e4SLinus Torvalds 	struct file_lock *right = NULL;
951bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
952b9746ef8SJeff Layton 	int error;
953b9746ef8SJeff Layton 	bool added = false;
954ed9814d8SJeff Layton 	LIST_HEAD(dispose);
9551da177e4SLinus Torvalds 
956bd61e0a9SJeff Layton 	ctx = locks_get_lock_context(inode);
957bd61e0a9SJeff Layton 	if (!ctx)
958bd61e0a9SJeff Layton 		return -ENOMEM;
959bd61e0a9SJeff Layton 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
9621da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
96339005d02SMiklos Szeredi 	 *
96439005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
9651da177e4SLinus Torvalds 	 */
96639005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
96739005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
96839005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
9691da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
9701da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
97139005d02SMiklos Szeredi 	}
9721da177e4SLinus Torvalds 
9736109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
9741cb36012SJeff Layton 	/*
9751cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
9761cb36012SJeff Layton 	 * there are any, either return error or put the request on the
97748f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
9781cb36012SJeff Layton 	 */
9791da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
980bd61e0a9SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
9811da177e4SLinus Torvalds 			if (!IS_POSIX(fl))
9821da177e4SLinus Torvalds 				continue;
9831da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
9841da177e4SLinus Torvalds 				continue;
9855842add2SAndy Adamson 			if (conflock)
9863fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
9871da177e4SLinus Torvalds 			error = -EAGAIN;
9881da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
9891da177e4SLinus Torvalds 				goto out;
9901c8c601aSJeff Layton 			/*
9911c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
9921c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
9931c8c601aSJeff Layton 			 */
9941da177e4SLinus Torvalds 			error = -EDEADLK;
9957b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
9961c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
997bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
9981c8c601aSJeff Layton 				__locks_insert_block(fl, request);
9991c8c601aSJeff Layton 			}
10007b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
10011da177e4SLinus Torvalds 			goto out;
10021da177e4SLinus Torvalds   		}
10031da177e4SLinus Torvalds   	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
10061da177e4SLinus Torvalds 	error = 0;
10071da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
10081da177e4SLinus Torvalds 		goto out;
10091da177e4SLinus Torvalds 
1010bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
1011bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1012bd61e0a9SJeff Layton 		if (posix_same_owner(request, fl))
1013bd61e0a9SJeff Layton 			break;
10141da177e4SLinus Torvalds 	}
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/* Process locks with this owner. */
1017bd61e0a9SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1018bd61e0a9SJeff Layton 		if (!posix_same_owner(request, fl))
1019bd61e0a9SJeff Layton 			break;
1020bd61e0a9SJeff Layton 
1021bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
10221da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
1023449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1024449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1025449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1026449231d6SOlaf Kirch 			 */
10271da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1028bd61e0a9SJeff Layton 				continue;
10291da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
10301da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
10311da177e4SLinus Torvalds 			 */
1032449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
10331da177e4SLinus Torvalds 				break;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
10361da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
10371da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
10381da177e4SLinus Torvalds 			 * locks to the higher end address.
10391da177e4SLinus Torvalds 			 */
10401da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
10411da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
10421da177e4SLinus Torvalds 			else
10431da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
10441da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
10451da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
10461da177e4SLinus Torvalds 			else
10471da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
10481da177e4SLinus Torvalds 			if (added) {
10499bd0f45bSJeff Layton 				locks_delete_lock_ctx(fl, &ctx->flc_posix_cnt,
10509bd0f45bSJeff Layton 							&dispose);
10511da177e4SLinus Torvalds 				continue;
10521da177e4SLinus Torvalds 			}
10531da177e4SLinus Torvalds 			request = fl;
1054b9746ef8SJeff Layton 			added = true;
1055bd61e0a9SJeff Layton 		} else {
10561da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
10571da177e4SLinus Torvalds 			 * more complex.
10581da177e4SLinus Torvalds 			 */
10591da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1060bd61e0a9SJeff Layton 				continue;
10611da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
10621da177e4SLinus Torvalds 				break;
10631da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1064b9746ef8SJeff Layton 				added = true;
10651da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
10661da177e4SLinus Torvalds 				left = fl;
10671da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
10681da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
10691da177e4SLinus Torvalds 			 */
10701da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
10711da177e4SLinus Torvalds 				right = fl;
10721da177e4SLinus Torvalds 				break;
10731da177e4SLinus Torvalds 			}
10741da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
10751da177e4SLinus Torvalds 				/* The new lock completely replaces an old
10761da177e4SLinus Torvalds 				 * one (This may happen several times).
10771da177e4SLinus Torvalds 				 */
10781da177e4SLinus Torvalds 				if (added) {
10799bd0f45bSJeff Layton 					locks_delete_lock_ctx(fl,
10809bd0f45bSJeff Layton 						&ctx->flc_posix_cnt, &dispose);
10811da177e4SLinus Torvalds 					continue;
10821da177e4SLinus Torvalds 				}
1083b84d49f9SJeff Layton 				/*
1084b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1085b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1086b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1087b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1088b84d49f9SJeff Layton 				 * just replacing an existing lock.
10891da177e4SLinus Torvalds 				 */
1090b84d49f9SJeff Layton 				error = -ENOLCK;
1091b84d49f9SJeff Layton 				if (!new_fl)
1092b84d49f9SJeff Layton 					goto out;
1093b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
1094b84d49f9SJeff Layton 				request = new_fl;
1095b84d49f9SJeff Layton 				new_fl = NULL;
10969bd0f45bSJeff Layton 				locks_insert_lock_ctx(request,
10979bd0f45bSJeff Layton 					&ctx->flc_posix_cnt, &fl->fl_list);
10989bd0f45bSJeff Layton 				locks_delete_lock_ctx(fl,
10999bd0f45bSJeff Layton 					&ctx->flc_posix_cnt, &dispose);
1100b9746ef8SJeff Layton 				added = true;
11011da177e4SLinus Torvalds 			}
11021da177e4SLinus Torvalds 		}
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
11050d9a490aSMiklos Szeredi 	/*
11061cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
11071cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
11081cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
11090d9a490aSMiklos Szeredi 	 */
11100d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
11110d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
11120d9a490aSMiklos Szeredi 		goto out;
11130d9a490aSMiklos Szeredi 
11141da177e4SLinus Torvalds 	error = 0;
11151da177e4SLinus Torvalds 	if (!added) {
1116f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1117f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1118f475ae95STrond Myklebust 				error = -ENOENT;
11191da177e4SLinus Torvalds 			goto out;
1120f475ae95STrond Myklebust 		}
11210d9a490aSMiklos Szeredi 
11220d9a490aSMiklos Szeredi 		if (!new_fl) {
11230d9a490aSMiklos Szeredi 			error = -ENOLCK;
11240d9a490aSMiklos Szeredi 			goto out;
11250d9a490aSMiklos Szeredi 		}
11261da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
11279bd0f45bSJeff Layton 		locks_insert_lock_ctx(new_fl, &ctx->flc_posix_cnt,
11289bd0f45bSJeff Layton 					&fl->fl_list);
11291da177e4SLinus Torvalds 		new_fl = NULL;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 	if (right) {
11321da177e4SLinus Torvalds 		if (left == right) {
11331da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
11341da177e4SLinus Torvalds 			 * so we have to use the second new lock.
11351da177e4SLinus Torvalds 			 */
11361da177e4SLinus Torvalds 			left = new_fl2;
11371da177e4SLinus Torvalds 			new_fl2 = NULL;
11381da177e4SLinus Torvalds 			locks_copy_lock(left, right);
11399bd0f45bSJeff Layton 			locks_insert_lock_ctx(left, &ctx->flc_posix_cnt,
11409bd0f45bSJeff Layton 						&fl->fl_list);
11411da177e4SLinus Torvalds 		}
11421da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
11431da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 	if (left) {
11461da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
11471da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds  out:
11506109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
11511da177e4SLinus Torvalds 	/*
11521da177e4SLinus Torvalds 	 * Free any unused locks.
11531da177e4SLinus Torvalds 	 */
11541da177e4SLinus Torvalds 	if (new_fl)
11551da177e4SLinus Torvalds 		locks_free_lock(new_fl);
11561da177e4SLinus Torvalds 	if (new_fl2)
11571da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1158ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
11591da177e4SLinus Torvalds 	return error;
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds /**
11631da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
11641da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11651da177e4SLinus Torvalds  * @fl: The lock to be applied
1166150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
11671da177e4SLinus Torvalds  *
11681da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11691da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11701da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1171f475ae95STrond Myklebust  *
1172f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1173f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1174f475ae95STrond Myklebust  * value for -ENOENT.
11751da177e4SLinus Torvalds  */
1176150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
11775842add2SAndy Adamson 			struct file_lock *conflock)
11785842add2SAndy Adamson {
1179496ad9aaSAl Viro 	return __posix_lock_file(file_inode(filp), fl, conflock);
11805842add2SAndy Adamson }
1181150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds /**
11841da177e4SLinus Torvalds  * posix_lock_file_wait - Apply a POSIX-style lock to a file
11851da177e4SLinus Torvalds  * @filp: The file to apply the lock to
11861da177e4SLinus Torvalds  * @fl: The lock to be applied
11871da177e4SLinus Torvalds  *
11881da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
11891da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
11901da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
11911da177e4SLinus Torvalds  */
11921da177e4SLinus Torvalds int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	int error;
11951da177e4SLinus Torvalds 	might_sleep ();
11961da177e4SLinus Torvalds 	for (;;) {
1197150b3934SMarc Eshel 		error = posix_lock_file(filp, fl, NULL);
1198bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
11991da177e4SLinus Torvalds 			break;
12001da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
12011da177e4SLinus Torvalds 		if (!error)
12021da177e4SLinus Torvalds 			continue;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 		locks_delete_block(fl);
12051da177e4SLinus Torvalds 		break;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 	return error;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds EXPORT_SYMBOL(posix_lock_file_wait);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /**
12121da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1213d7a06983SJeff Layton  * @file: the file to check
12141da177e4SLinus Torvalds  *
12151da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
12161da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
12171da177e4SLinus Torvalds  */
1218d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
12191da177e4SLinus Torvalds {
1220bd61e0a9SJeff Layton 	int ret;
1221d7a06983SJeff Layton 	struct inode *inode = file_inode(file);
1222bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
12231da177e4SLinus Torvalds 	struct file_lock *fl;
12241da177e4SLinus Torvalds 
1225bd61e0a9SJeff Layton 	ctx = inode->i_flctx;
1226bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix))
1227bd61e0a9SJeff Layton 		return 0;
1228bd61e0a9SJeff Layton 
12291da177e4SLinus Torvalds 	/*
12301da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
12311da177e4SLinus Torvalds 	 */
12326109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1233bd61e0a9SJeff Layton 	ret = 0;
1234bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
123573a8f5f7SChristoph Hellwig 		if (fl->fl_owner != current->files &&
1236bd61e0a9SJeff Layton 		    fl->fl_owner != file) {
1237bd61e0a9SJeff Layton 			ret = -EAGAIN;
12381da177e4SLinus Torvalds 			break;
12391da177e4SLinus Torvalds 		}
1240bd61e0a9SJeff Layton 	}
12416109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1242bd61e0a9SJeff Layton 	return ret;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /**
12461da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
12471da177e4SLinus Torvalds  * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
12481da177e4SLinus Torvalds  *		for shared
12491da177e4SLinus Torvalds  * @inode:      the file to check
12501da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
12511da177e4SLinus Torvalds  * @offset:     start of area to check
12521da177e4SLinus Torvalds  * @count:      length of area to check
12531da177e4SLinus Torvalds  *
12541da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
12551da177e4SLinus Torvalds  * This function is called from rw_verify_area() and
12561da177e4SLinus Torvalds  * locks_verify_truncate().
12571da177e4SLinus Torvalds  */
12581da177e4SLinus Torvalds int locks_mandatory_area(int read_write, struct inode *inode,
12591da177e4SLinus Torvalds 			 struct file *filp, loff_t offset,
12601da177e4SLinus Torvalds 			 size_t count)
12611da177e4SLinus Torvalds {
12621da177e4SLinus Torvalds 	struct file_lock fl;
12631da177e4SLinus Torvalds 	int error;
126429723adeSJeff Layton 	bool sleep = false;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	locks_init_lock(&fl);
12671da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
12681da177e4SLinus Torvalds 	fl.fl_file = filp;
12691da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
12701da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
127129723adeSJeff Layton 		sleep = true;
12721da177e4SLinus Torvalds 	fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
12731da177e4SLinus Torvalds 	fl.fl_start = offset;
12741da177e4SLinus Torvalds 	fl.fl_end = offset + count - 1;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	for (;;) {
127729723adeSJeff Layton 		if (filp) {
127873a8f5f7SChristoph Hellwig 			fl.fl_owner = filp;
127929723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
128029723adeSJeff Layton 			error = __posix_lock_file(inode, &fl, NULL);
128129723adeSJeff Layton 			if (!error)
128229723adeSJeff Layton 				break;
128329723adeSJeff Layton 		}
128429723adeSJeff Layton 
128529723adeSJeff Layton 		if (sleep)
128629723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
128729723adeSJeff Layton 		fl.fl_owner = current->files;
1288150b3934SMarc Eshel 		error = __posix_lock_file(inode, &fl, NULL);
1289bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
12901da177e4SLinus Torvalds 			break;
12911da177e4SLinus Torvalds 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
12921da177e4SLinus Torvalds 		if (!error) {
12931da177e4SLinus Torvalds 			/*
12941da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
12951da177e4SLinus Torvalds 			 * changed the permissions behind our back.
12961da177e4SLinus Torvalds 			 */
1297a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
12981da177e4SLinus Torvalds 				continue;
12991da177e4SLinus Torvalds 		}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 		locks_delete_block(&fl);
13021da177e4SLinus Torvalds 		break;
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	return error;
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
13091da177e4SLinus Torvalds 
1310778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1311778fc546SJ. Bruce Fields {
1312778fc546SJ. Bruce Fields 	switch (arg) {
1313778fc546SJ. Bruce Fields 	case F_UNLCK:
1314778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1315778fc546SJ. Bruce Fields 		/* fall through: */
1316778fc546SJ. Bruce Fields 	case F_RDLCK:
1317778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1318778fc546SJ. Bruce Fields 	}
1319778fc546SJ. Bruce Fields }
1320778fc546SJ. Bruce Fields 
13211da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
13227448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
13231da177e4SLinus Torvalds {
13249bd0f45bSJeff Layton 	struct file_lock_context *flctx;
13251da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	if (error)
13281da177e4SLinus Torvalds 		return error;
1329778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
13301da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
13313b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
13323b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
13333b6e2723SFilipe Brandenburger 
13349bd0f45bSJeff Layton 		flctx = file_inode(filp)->i_flctx;
13353b6e2723SFilipe Brandenburger 		f_delown(filp);
13363b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
133796d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
133896d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
133996d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
134096d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
134196d6d59cSJ. Bruce Fields 		}
13429bd0f45bSJeff Layton 		locks_delete_lock_ctx(fl, &flctx->flc_lease_cnt, dispose);
13433b6e2723SFilipe Brandenburger 	}
13441da177e4SLinus Torvalds 	return 0;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
13471da177e4SLinus Torvalds 
1348778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1349778fc546SJ. Bruce Fields {
1350778fc546SJ. Bruce Fields 	if (!then)
1351778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1352778fc546SJ. Bruce Fields 		return false;
1353778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1354778fc546SJ. Bruce Fields }
1355778fc546SJ. Bruce Fields 
1356c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
13571da177e4SLinus Torvalds {
13588634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
13598634b51fSJeff Layton 	struct file_lock *fl, *tmp;
13601da177e4SLinus Torvalds 
13616109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1362f82b4b67SJeff Layton 
13638634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
136462af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1365778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
13667448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1367778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
13687448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
1372df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1373df4e8d2cSJ. Bruce Fields {
1374df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1375df4e8d2cSJ. Bruce Fields 		return false;
1376df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1377df4e8d2cSJ. Bruce Fields }
1378df4e8d2cSJ. Bruce Fields 
137903d12ddfSJeff Layton static bool
138003d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker)
138103d12ddfSJeff Layton {
13828634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
138303d12ddfSJeff Layton 	struct file_lock *fl;
138403d12ddfSJeff Layton 
13856109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
138603d12ddfSJeff Layton 
13878634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
138803d12ddfSJeff Layton 		if (leases_conflict(fl, breaker))
138903d12ddfSJeff Layton 			return true;
139003d12ddfSJeff Layton 	}
139103d12ddfSJeff Layton 	return false;
139203d12ddfSJeff Layton }
139303d12ddfSJeff Layton 
13941da177e4SLinus Torvalds /**
13951da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
13961da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1397df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1398df4e8d2cSJ. Bruce Fields  *	    break all leases
1399df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1400df4e8d2cSJ. Bruce Fields  *	    only delegations
14011da177e4SLinus Torvalds  *
140287250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
140387250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
140487250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
14051da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
14061da177e4SLinus Torvalds  */
1407df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
14081da177e4SLinus Torvalds {
1409778fc546SJ. Bruce Fields 	int error = 0;
141003d12ddfSJeff Layton 	struct file_lock *new_fl;
14118634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
14128634b51fSJeff Layton 	struct file_lock *fl;
14131da177e4SLinus Torvalds 	unsigned long break_time;
14148737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1415c45198edSJeff Layton 	LIST_HEAD(dispose);
14161da177e4SLinus Torvalds 
14178737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
14186d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
14196d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1420df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
14211da177e4SLinus Torvalds 
14228634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
14238634b51fSJeff Layton 	if (!ctx) {
14248634b51fSJeff Layton 		WARN_ON_ONCE(1);
14258634b51fSJeff Layton 		return error;
14268634b51fSJeff Layton 	}
14278634b51fSJeff Layton 
14286109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
14291da177e4SLinus Torvalds 
1430c45198edSJeff Layton 	time_out_leases(inode, &dispose);
14311da177e4SLinus Torvalds 
143203d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1433df4e8d2cSJ. Bruce Fields 		goto out;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	break_time = 0;
14361da177e4SLinus Torvalds 	if (lease_break_time > 0) {
14371da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
14381da177e4SLinus Torvalds 		if (break_time == 0)
14391da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
14401da177e4SLinus Torvalds 	}
14411da177e4SLinus Torvalds 
14428634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1443df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1444df4e8d2cSJ. Bruce Fields 			continue;
1445778fc546SJ. Bruce Fields 		if (want_write) {
1446778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1447778fc546SJ. Bruce Fields 				continue;
1448778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
14491da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1450778fc546SJ. Bruce Fields 		} else {
14518634b51fSJeff Layton 			if (lease_breaking(fl))
1452778fc546SJ. Bruce Fields 				continue;
1453778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1454778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
14551da177e4SLinus Torvalds 		}
14564d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
14579bd0f45bSJeff Layton 			locks_delete_lock_ctx(fl, &ctx->flc_lease_cnt,
14589bd0f45bSJeff Layton 						&dispose);
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
14618634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
14624d01b7f5SJeff Layton 		goto out;
14634d01b7f5SJeff Layton 
1464843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
146562af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
14661da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
14671da177e4SLinus Torvalds 		goto out;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds restart:
14718634b51fSJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
14728634b51fSJeff Layton 	break_time = fl->fl_break_time;
1473f1c6bb2cSJeff Layton 	if (break_time != 0)
14741da177e4SLinus Torvalds 		break_time -= jiffies;
14751da177e4SLinus Torvalds 	if (break_time == 0)
14761da177e4SLinus Torvalds 		break_time++;
14778634b51fSJeff Layton 	locks_insert_block(fl, new_fl);
147862af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
14796109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1480c45198edSJeff Layton 	locks_dispose_list(&dispose);
14814321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
14824321e01eSMatthew Wilcox 						!new_fl->fl_next, break_time);
14836109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
148462af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
14851c8c601aSJeff Layton 	locks_delete_block(new_fl);
14861da177e4SLinus Torvalds 	if (error >= 0) {
1487778fc546SJ. Bruce Fields 		/*
1488778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1489778fc546SJ. Bruce Fields 		 * broken yet
1490778fc546SJ. Bruce Fields 		 */
149103d12ddfSJeff Layton 		if (error == 0)
149203d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
149303d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
14941da177e4SLinus Torvalds 			goto restart;
14951da177e4SLinus Torvalds 		error = 0;
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds out:
14986109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1499c45198edSJeff Layton 	locks_dispose_list(&dispose);
15001da177e4SLinus Torvalds 	locks_free_lock(new_fl);
15011da177e4SLinus Torvalds 	return error;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /**
1507a6b91919SRandy Dunlap  *	lease_get_mtime - get the last modified time of an inode
15081da177e4SLinus Torvalds  *	@inode: the inode
15091da177e4SLinus Torvalds  *      @time:  pointer to a timespec which will contain the last modified time
15101da177e4SLinus Torvalds  *
15111da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
15121da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1513a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
15141da177e4SLinus Torvalds  */
15151da177e4SLinus Torvalds void lease_get_mtime(struct inode *inode, struct timespec *time)
15161da177e4SLinus Torvalds {
1517bfe86024SJeff Layton 	bool has_lease = false;
15188634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
15198634b51fSJeff Layton 	struct file_lock *fl;
1520bfe86024SJeff Layton 
15218634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
15226109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
15238634b51fSJeff Layton 		if (!list_empty(&ctx->flc_lease)) {
15248634b51fSJeff Layton 			fl = list_first_entry(&ctx->flc_lease,
15258634b51fSJeff Layton 						struct file_lock, fl_list);
15268634b51fSJeff Layton 			if (fl->fl_type == F_WRLCK)
1527bfe86024SJeff Layton 				has_lease = true;
15288634b51fSJeff Layton 		}
15296109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1530bfe86024SJeff Layton 	}
1531bfe86024SJeff Layton 
1532bfe86024SJeff Layton 	if (has_lease)
15331da177e4SLinus Torvalds 		*time = current_fs_time(inode->i_sb);
15341da177e4SLinus Torvalds 	else
15351da177e4SLinus Torvalds 		*time = inode->i_mtime;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds /**
15411da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
15421da177e4SLinus Torvalds  *	@filp: the file
15431da177e4SLinus Torvalds  *
15441da177e4SLinus Torvalds  *	The value returned by this function will be one of
15451da177e4SLinus Torvalds  *	(if no lease break is pending):
15461da177e4SLinus Torvalds  *
15471da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
15481da177e4SLinus Torvalds  *
15491da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
15501da177e4SLinus Torvalds  *
15511da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
15521da177e4SLinus Torvalds  *
15531da177e4SLinus Torvalds  *	(if a lease break is pending):
15541da177e4SLinus Torvalds  *
15551da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
15561da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
15571da177e4SLinus Torvalds  *
15581da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
15591da177e4SLinus Torvalds  *
15601da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
15611da177e4SLinus Torvalds  *	should be returned to userspace.
15621da177e4SLinus Torvalds  */
15631da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	struct file_lock *fl;
15661c8c601aSJeff Layton 	struct inode *inode = file_inode(filp);
15678634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
15681da177e4SLinus Torvalds 	int type = F_UNLCK;
1569c45198edSJeff Layton 	LIST_HEAD(dispose);
15701da177e4SLinus Torvalds 
15718634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
15726109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1573c45198edSJeff Layton 		time_out_leases(file_inode(filp), &dispose);
15748634b51fSJeff Layton 		list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
15758634b51fSJeff Layton 			if (fl->fl_file != filp)
15768634b51fSJeff Layton 				continue;
1577778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
15781da177e4SLinus Torvalds 			break;
15791da177e4SLinus Torvalds 		}
15806109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1581c45198edSJeff Layton 		locks_dispose_list(&dispose);
15828634b51fSJeff Layton 	}
15831da177e4SLinus Torvalds 	return type;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
158624cbe784SJeff Layton /**
158724cbe784SJeff Layton  * check_conflicting_open - see if the given dentry points to a file that has
158824cbe784SJeff Layton  * 			    an existing open that would conflict with the
158924cbe784SJeff Layton  * 			    desired lease.
159024cbe784SJeff Layton  * @dentry:	dentry to check
159124cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
159224cbe784SJeff Layton  *
159324cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
159424cbe784SJeff Layton  * conflict with the lease we're trying to set.
159524cbe784SJeff Layton  */
159624cbe784SJeff Layton static int
159724cbe784SJeff Layton check_conflicting_open(const struct dentry *dentry, const long arg)
159824cbe784SJeff Layton {
159924cbe784SJeff Layton 	int ret = 0;
160024cbe784SJeff Layton 	struct inode *inode = dentry->d_inode;
160124cbe784SJeff Layton 
160224cbe784SJeff Layton 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
160324cbe784SJeff Layton 		return -EAGAIN;
160424cbe784SJeff Layton 
160524cbe784SJeff Layton 	if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
160624cbe784SJeff Layton 	    (atomic_read(&inode->i_count) > 1)))
160724cbe784SJeff Layton 		ret = -EAGAIN;
160824cbe784SJeff Layton 
160924cbe784SJeff Layton 	return ret;
161024cbe784SJeff Layton }
161124cbe784SJeff Layton 
1612e6f5c789SJeff Layton static int
1613e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
16141da177e4SLinus Torvalds {
16158634b51fSJeff Layton 	struct file_lock *fl, *my_fl = NULL, *lease;
16160f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
16171da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16188634b51fSJeff Layton 	struct file_lock_context *ctx;
1619df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1620c1f24ef4SJ. Bruce Fields 	int error;
1621c45198edSJeff Layton 	LIST_HEAD(dispose);
16221da177e4SLinus Torvalds 
1623096657b6SJ. Bruce Fields 	lease = *flp;
162462af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
162562af4f1fSJeff Layton 
16268634b51fSJeff Layton 	ctx = locks_get_lock_context(inode);
16278634b51fSJeff Layton 	if (!ctx)
16288634b51fSJeff Layton 		return -ENOMEM;
16298634b51fSJeff Layton 
1630df4e8d2cSJ. Bruce Fields 	/*
1631df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1632df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1633df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1634df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1635df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1636df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1637df4e8d2cSJ. Bruce Fields 	 */
1638df4e8d2cSJ. Bruce Fields 	if (is_deleg && !mutex_trylock(&inode->i_mutex))
1639df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1640df4e8d2cSJ. Bruce Fields 
1641df4e8d2cSJ. Bruce Fields 	if (is_deleg && arg == F_WRLCK) {
1642df4e8d2cSJ. Bruce Fields 		/* Write delegations are not currently supported: */
16434fdb793fSDan Carpenter 		mutex_unlock(&inode->i_mutex);
1644df4e8d2cSJ. Bruce Fields 		WARN_ON_ONCE(1);
1645df4e8d2cSJ. Bruce Fields 		return -EINVAL;
1646df4e8d2cSJ. Bruce Fields 	}
1647096657b6SJ. Bruce Fields 
16486109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1649c45198edSJeff Layton 	time_out_leases(inode, &dispose);
165024cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
165124cbe784SJeff Layton 	if (error)
16521da177e4SLinus Torvalds 		goto out;
165385c59580SPavel Emelyanov 
16541da177e4SLinus Torvalds 	/*
16551da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
16561da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
16571da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
16581da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
16591da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
16601da177e4SLinus Torvalds 	 * except for this filp.
16611da177e4SLinus Torvalds 	 */
1662c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
16638634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1664*2ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
1665*2ab99ee1SChristoph Hellwig 		    fl->fl_owner == lease->fl_owner) {
16668634b51fSJeff Layton 			my_fl = fl;
1667c1f24ef4SJ. Bruce Fields 			continue;
16681da177e4SLinus Torvalds 		}
16698634b51fSJeff Layton 
1670c1f24ef4SJ. Bruce Fields 		/*
1671c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1672c1f24ef4SJ. Bruce Fields 		 * this file:
1673c1f24ef4SJ. Bruce Fields 		 */
1674c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
16751da177e4SLinus Torvalds 			goto out;
1676c1f24ef4SJ. Bruce Fields 		/*
1677c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1678c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1679c1f24ef4SJ. Bruce Fields 		 */
1680c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1681c1f24ef4SJ. Bruce Fields 			goto out;
1682c1f24ef4SJ. Bruce Fields 	}
16831da177e4SLinus Torvalds 
16848634b51fSJeff Layton 	if (my_fl != NULL) {
16857448cc37SJeff Layton 		error = lease->fl_lmops->lm_change(my_fl, arg, &dispose);
16861c7dd2ffSJeff Layton 		if (error)
16871da177e4SLinus Torvalds 			goto out;
16881c7dd2ffSJeff Layton 		goto out_setup;
16891da177e4SLinus Torvalds 	}
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	error = -EINVAL;
16921da177e4SLinus Torvalds 	if (!leases_enable)
16931da177e4SLinus Torvalds 		goto out;
16941da177e4SLinus Torvalds 
16959bd0f45bSJeff Layton 	locks_insert_lock_ctx(lease, &ctx->flc_lease_cnt, &ctx->flc_lease);
169624cbe784SJeff Layton 	/*
169724cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
169824cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
169924cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
170024cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
170124cbe784SJeff Layton 	 *
170224cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
170324cbe784SJeff Layton 	 * precedes these checks.
170424cbe784SJeff Layton 	 */
170524cbe784SJeff Layton 	smp_mb();
170624cbe784SJeff Layton 	error = check_conflicting_open(dentry, arg);
17078634b51fSJeff Layton 	if (error) {
17089bd0f45bSJeff Layton 		locks_unlink_lock_ctx(lease, &ctx->flc_lease_cnt);
17098634b51fSJeff Layton 		goto out;
17108634b51fSJeff Layton 	}
17111c7dd2ffSJeff Layton 
17121c7dd2ffSJeff Layton out_setup:
17131c7dd2ffSJeff Layton 	if (lease->fl_lmops->lm_setup)
17141c7dd2ffSJeff Layton 		lease->fl_lmops->lm_setup(lease, priv);
17151da177e4SLinus Torvalds out:
17166109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1717c45198edSJeff Layton 	locks_dispose_list(&dispose);
1718df4e8d2cSJ. Bruce Fields 	if (is_deleg)
1719df4e8d2cSJ. Bruce Fields 		mutex_unlock(&inode->i_mutex);
17208634b51fSJeff Layton 	if (!error && !my_fl)
17211c7dd2ffSJeff Layton 		*flp = NULL;
17221da177e4SLinus Torvalds 	return error;
17231da177e4SLinus Torvalds }
17248335ebd9SJ. Bruce Fields 
1725*2ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner)
17268335ebd9SJ. Bruce Fields {
17270efaa7e8SJeff Layton 	int error = -EAGAIN;
17288634b51fSJeff Layton 	struct file_lock *fl, *victim = NULL;
17298335ebd9SJ. Bruce Fields 	struct dentry *dentry = filp->f_path.dentry;
17308335ebd9SJ. Bruce Fields 	struct inode *inode = dentry->d_inode;
17318634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
1732c45198edSJeff Layton 	LIST_HEAD(dispose);
17338335ebd9SJ. Bruce Fields 
17348634b51fSJeff Layton 	if (!ctx) {
17358634b51fSJeff Layton 		trace_generic_delete_lease(inode, NULL);
17368634b51fSJeff Layton 		return error;
17378634b51fSJeff Layton 	}
17388634b51fSJeff Layton 
17396109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
17408634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1741*2ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
1742*2ab99ee1SChristoph Hellwig 		    fl->fl_owner == owner) {
17438634b51fSJeff Layton 			victim = fl;
17440efaa7e8SJeff Layton 			break;
17458335ebd9SJ. Bruce Fields 		}
17468634b51fSJeff Layton 	}
17470efaa7e8SJeff Layton 	trace_generic_delete_lease(inode, fl);
17488634b51fSJeff Layton 	if (victim)
17497448cc37SJeff Layton 		error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
17506109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1751c45198edSJeff Layton 	locks_dispose_list(&dispose);
17520efaa7e8SJeff Layton 	return error;
17538335ebd9SJ. Bruce Fields }
17548335ebd9SJ. Bruce Fields 
17551da177e4SLinus Torvalds /**
17561da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
17571da177e4SLinus Torvalds  *	@filp:	file pointer
17581da177e4SLinus Torvalds  *	@arg:	type of lease to obtain
17591da177e4SLinus Torvalds  *	@flp:	input - file_lock to use, output - file_lock inserted
17601c7dd2ffSJeff Layton  *	@priv:	private data for lm_setup (may be NULL if lm_setup
17611c7dd2ffSJeff Layton  *		doesn't require it)
17621da177e4SLinus Torvalds  *
17631da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
17641da177e4SLinus Torvalds  *	by break_lease().
17651da177e4SLinus Torvalds  */
1766e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1767e6f5c789SJeff Layton 			void **priv)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	struct dentry *dentry = filp->f_path.dentry;
17701da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
17718335ebd9SJ. Bruce Fields 	int error;
17721da177e4SLinus Torvalds 
17738e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
17748335ebd9SJ. Bruce Fields 		return -EACCES;
17751da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
17768335ebd9SJ. Bruce Fields 		return -EINVAL;
17771da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
17781da177e4SLinus Torvalds 	if (error)
17798335ebd9SJ. Bruce Fields 		return error;
17801da177e4SLinus Torvalds 
17818335ebd9SJ. Bruce Fields 	switch (arg) {
17828335ebd9SJ. Bruce Fields 	case F_UNLCK:
1783*2ab99ee1SChristoph Hellwig 		return generic_delete_lease(filp, *priv);
17848335ebd9SJ. Bruce Fields 	case F_RDLCK:
17858335ebd9SJ. Bruce Fields 	case F_WRLCK:
17860efaa7e8SJeff Layton 		if (!(*flp)->fl_lmops->lm_break) {
17870efaa7e8SJeff Layton 			WARN_ON_ONCE(1);
17880efaa7e8SJeff Layton 			return -ENOLCK;
17890efaa7e8SJeff Layton 		}
1790e6f5c789SJeff Layton 		return generic_add_lease(filp, arg, flp, priv);
17918335ebd9SJ. Bruce Fields 	default:
17928d657eb3SDave Jones 		return -EINVAL;
17931da177e4SLinus Torvalds 	}
17941da177e4SLinus Torvalds }
17950af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds /**
1798a9933ceaSJ. Bruce Fields  * vfs_setlease        -       sets a lease on an open file
17991da177e4SLinus Torvalds  * @filp:	file pointer
18001da177e4SLinus Torvalds  * @arg:	type of lease to obtain
1801e51673aaSJeff Layton  * @lease:	file_lock to use when adding a lease
18021c7dd2ffSJeff Layton  * @priv:	private info for lm_setup when adding a lease (may be
18031c7dd2ffSJeff Layton  * 		NULL if lm_setup doesn't require it)
18041da177e4SLinus Torvalds  *
1805e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
1806e51673aaSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
1807e51673aaSJeff Layton  * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
1808e51673aaSJeff Layton  * if not, this function will return -ENOLCK (and generate a scary-looking
1809e51673aaSJeff Layton  * stack trace).
18101c7dd2ffSJeff Layton  *
18111c7dd2ffSJeff Layton  * The "priv" pointer is passed directly to the lm_setup function as-is. It
18121c7dd2ffSJeff Layton  * may be NULL if the lm_setup operation doesn't require it.
18131da177e4SLinus Torvalds  */
1814e6f5c789SJeff Layton int
1815e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
18161da177e4SLinus Torvalds {
18171c7dd2ffSJeff Layton 	if (filp->f_op->setlease)
1818f82b4b67SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
18191c7dd2ffSJeff Layton 	else
1820f82b4b67SJeff Layton 		return generic_setlease(filp, arg, lease, priv);
18211da177e4SLinus Torvalds }
1822a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
18231da177e4SLinus Torvalds 
18240ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
18251da177e4SLinus Torvalds {
18261c7dd2ffSJeff Layton 	struct file_lock *fl;
1827f7347ce4SLinus Torvalds 	struct fasync_struct *new;
18281da177e4SLinus Torvalds 	int error;
18291da177e4SLinus Torvalds 
1830c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1831c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1832c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
18331da177e4SLinus Torvalds 
1834f7347ce4SLinus Torvalds 	new = fasync_alloc();
1835f7347ce4SLinus Torvalds 	if (!new) {
1836f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1837f7347ce4SLinus Torvalds 		return -ENOMEM;
1838f7347ce4SLinus Torvalds 	}
18391c7dd2ffSJeff Layton 	new->fa_fd = fd;
18401da177e4SLinus Torvalds 
18411c7dd2ffSJeff Layton 	error = vfs_setlease(filp, arg, &fl, (void **)&new);
18422dfb928fSJeff Layton 	if (fl)
18432dfb928fSJeff Layton 		locks_free_lock(fl);
1844f7347ce4SLinus Torvalds 	if (new)
1845f7347ce4SLinus Torvalds 		fasync_free(new);
18461da177e4SLinus Torvalds 	return error;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds /**
18500ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
18510ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
18520ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
18530ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
18540ceaf6c7SJ. Bruce Fields  *
18550ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
18560ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
18570ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
18580ceaf6c7SJ. Bruce Fields  */
18590ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
18600ceaf6c7SJ. Bruce Fields {
18610ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
1862*2ab99ee1SChristoph Hellwig 		return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
18630ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
18640ceaf6c7SJ. Bruce Fields }
18650ceaf6c7SJ. Bruce Fields 
18660ceaf6c7SJ. Bruce Fields /**
18671da177e4SLinus Torvalds  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
18681da177e4SLinus Torvalds  * @filp: The file to apply the lock to
18691da177e4SLinus Torvalds  * @fl: The lock to be applied
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  * Add a FLOCK style lock to a file.
18721da177e4SLinus Torvalds  */
18731da177e4SLinus Torvalds int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds 	int error;
18761da177e4SLinus Torvalds 	might_sleep();
18771da177e4SLinus Torvalds 	for (;;) {
18781da177e4SLinus Torvalds 		error = flock_lock_file(filp, fl);
1879bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
18801da177e4SLinus Torvalds 			break;
18811da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
18821da177e4SLinus Torvalds 		if (!error)
18831da177e4SLinus Torvalds 			continue;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 		locks_delete_block(fl);
18861da177e4SLinus Torvalds 		break;
18871da177e4SLinus Torvalds 	}
18881da177e4SLinus Torvalds 	return error;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds EXPORT_SYMBOL(flock_lock_file_wait);
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds /**
18941da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
18951da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
18961da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
18971da177e4SLinus Torvalds  *
18981da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
18991da177e4SLinus Torvalds  *	The @cmd can be one of
19001da177e4SLinus Torvalds  *
19011da177e4SLinus Torvalds  *	%LOCK_SH -- a shared lock.
19021da177e4SLinus Torvalds  *
19031da177e4SLinus Torvalds  *	%LOCK_EX -- an exclusive lock.
19041da177e4SLinus Torvalds  *
19051da177e4SLinus Torvalds  *	%LOCK_UN -- remove an existing lock.
19061da177e4SLinus Torvalds  *
19071da177e4SLinus Torvalds  *	%LOCK_MAND -- a `mandatory' flock.  This exists to emulate Windows Share Modes.
19081da177e4SLinus Torvalds  *
19091da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
19101da177e4SLinus Torvalds  *	processes read and write access respectively.
19111da177e4SLinus Torvalds  */
1912002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
19131da177e4SLinus Torvalds {
19142903ff01SAl Viro 	struct fd f = fdget(fd);
19151da177e4SLinus Torvalds 	struct file_lock *lock;
19161da177e4SLinus Torvalds 	int can_sleep, unlock;
19171da177e4SLinus Torvalds 	int error;
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	error = -EBADF;
19202903ff01SAl Viro 	if (!f.file)
19211da177e4SLinus Torvalds 		goto out;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
19241da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
19251da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
19261da177e4SLinus Torvalds 
1927aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
19282903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
19291da177e4SLinus Torvalds 		goto out_putf;
19301da177e4SLinus Torvalds 
19316e129d00SJeff Layton 	lock = flock_make_lock(f.file, cmd);
19326e129d00SJeff Layton 	if (IS_ERR(lock)) {
19336e129d00SJeff Layton 		error = PTR_ERR(lock);
19341da177e4SLinus Torvalds 		goto out_putf;
19356e129d00SJeff Layton 	}
19366e129d00SJeff Layton 
19371da177e4SLinus Torvalds 	if (can_sleep)
19381da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
19391da177e4SLinus Torvalds 
19402903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
19411da177e4SLinus Torvalds 	if (error)
19421da177e4SLinus Torvalds 		goto out_free;
19431da177e4SLinus Torvalds 
194472c2d531SAl Viro 	if (f.file->f_op->flock)
19452903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
19461da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
19471da177e4SLinus Torvalds 					  lock);
19481da177e4SLinus Torvalds 	else
19492903ff01SAl Viro 		error = flock_lock_file_wait(f.file, lock);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds  out_free:
19521da177e4SLinus Torvalds 	locks_free_lock(lock);
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds  out_putf:
19552903ff01SAl Viro 	fdput(f);
19561da177e4SLinus Torvalds  out:
19571da177e4SLinus Torvalds 	return error;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
19603ee17abdSJ. Bruce Fields /**
19613ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
19623ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
19636924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
19643ee17abdSJ. Bruce Fields  *
19653ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
19663ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
19673ee17abdSJ. Bruce Fields  */
19683ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
19693ee17abdSJ. Bruce Fields {
197072c2d531SAl Viro 	if (filp->f_op->lock)
19713ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
19723ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
19733ee17abdSJ. Bruce Fields 	return 0;
19743ee17abdSJ. Bruce Fields }
19753ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
19763ee17abdSJ. Bruce Fields 
1977c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1978c2fa1b8aSJ. Bruce Fields {
1979cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1980c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1981c2fa1b8aSJ. Bruce Fields 	/*
1982c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
1983c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
1984c2fa1b8aSJ. Bruce Fields 	 */
1985c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
1986c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1987c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1988c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
1989c2fa1b8aSJ. Bruce Fields #endif
1990c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
1991c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1992c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
1993c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
1994129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
1995c2fa1b8aSJ. Bruce Fields 	return 0;
1996c2fa1b8aSJ. Bruce Fields }
1997c2fa1b8aSJ. Bruce Fields 
1998c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
1999c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2000c2fa1b8aSJ. Bruce Fields {
2001cff2fce5SJeff Layton 	flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
2002c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
2003c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2004c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2005c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2006c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2007c2fa1b8aSJ. Bruce Fields }
2008c2fa1b8aSJ. Bruce Fields #endif
2009c2fa1b8aSJ. Bruce Fields 
20101da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
20111da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
20121da177e4SLinus Torvalds  */
2013c1e62b8fSJeff Layton int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
20141da177e4SLinus Torvalds {
20159d6a8c5cSMarc Eshel 	struct file_lock file_lock;
20161da177e4SLinus Torvalds 	struct flock flock;
20171da177e4SLinus Torvalds 	int error;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	error = -EFAULT;
20201da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
20211da177e4SLinus Torvalds 		goto out;
20221da177e4SLinus Torvalds 	error = -EINVAL;
20231da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
20241da177e4SLinus Torvalds 		goto out;
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, &file_lock, &flock);
20271da177e4SLinus Torvalds 	if (error)
20281da177e4SLinus Torvalds 		goto out;
20291da177e4SLinus Torvalds 
20300d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
203190478939SJeff Layton 		error = -EINVAL;
203290478939SJeff Layton 		if (flock.l_pid != 0)
203390478939SJeff Layton 			goto out;
203490478939SJeff Layton 
20355d50ffd7SJeff Layton 		cmd = F_GETLK;
2036cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
203773a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
20385d50ffd7SJeff Layton 	}
20395d50ffd7SJeff Layton 
20403ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
20413ee17abdSJ. Bruce Fields 	if (error)
20421da177e4SLinus Torvalds 		goto out;
20431da177e4SLinus Torvalds 
20449d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
20459d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
20469d6a8c5cSMarc Eshel 		error = posix_lock_to_flock(&flock, &file_lock);
2047c2fa1b8aSJ. Bruce Fields 		if (error)
2048f328296eSKinglong Mee 			goto rel_priv;
20491da177e4SLinus Torvalds 	}
20501da177e4SLinus Torvalds 	error = -EFAULT;
20511da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
20521da177e4SLinus Torvalds 		error = 0;
2053f328296eSKinglong Mee rel_priv:
2054f328296eSKinglong Mee 	locks_release_private(&file_lock);
20551da177e4SLinus Torvalds out:
20561da177e4SLinus Torvalds 	return error;
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds 
20597723ec97SMarc Eshel /**
20607723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
20617723ec97SMarc Eshel  * @filp: The file to apply the lock to
20627723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
20637723ec97SMarc Eshel  * @fl: The lock to be applied
2064150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2065150b3934SMarc Eshel  *
2066150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2067150b3934SMarc Eshel  * as the final argument.
2068150b3934SMarc Eshel  *
2069150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
2070150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
2071150b3934SMarc Eshel  * some acceptable default.
20722beb6614SMarc Eshel  *
20732beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
20742beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
20752beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
20768fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
20772beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
20782beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
20798fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
20802beb6614SMarc Eshel  * request completes.
20812beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
2082bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2083bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
20842beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
20852beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
20862beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
20872beb6614SMarc Eshel  * the correct lock cleanup when required.
20882beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
20898fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
20902beb6614SMarc Eshel  * return code.
20917723ec97SMarc Eshel  */
2092150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
20937723ec97SMarc Eshel {
209472c2d531SAl Viro 	if (filp->f_op->lock)
20957723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
20967723ec97SMarc Eshel 	else
2097150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
20987723ec97SMarc Eshel }
20997723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
21007723ec97SMarc Eshel 
2101b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2102b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2103b648a6deSMiklos Szeredi {
2104b648a6deSMiklos Szeredi 	int error;
2105b648a6deSMiklos Szeredi 
2106b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2107b648a6deSMiklos Szeredi 	if (error)
2108b648a6deSMiklos Szeredi 		return error;
2109b648a6deSMiklos Szeredi 
2110b648a6deSMiklos Szeredi 	for (;;) {
2111764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2112b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2113b648a6deSMiklos Szeredi 			break;
2114764c76b3SMiklos Szeredi 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2115b648a6deSMiklos Szeredi 		if (!error)
2116b648a6deSMiklos Szeredi 			continue;
2117b648a6deSMiklos Szeredi 
2118b648a6deSMiklos Szeredi 		locks_delete_block(fl);
2119b648a6deSMiklos Szeredi 		break;
2120b648a6deSMiklos Szeredi 	}
2121b648a6deSMiklos Szeredi 
2122b648a6deSMiklos Szeredi 	return error;
2123b648a6deSMiklos Szeredi }
2124b648a6deSMiklos Szeredi 
2125cf01f4eeSJeff Layton /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
2126cf01f4eeSJeff Layton static int
2127cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2128cf01f4eeSJeff Layton {
2129cf01f4eeSJeff Layton 	switch (fl->fl_type) {
2130cf01f4eeSJeff Layton 	case F_RDLCK:
2131cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_READ))
2132cf01f4eeSJeff Layton 			return -EBADF;
2133cf01f4eeSJeff Layton 		break;
2134cf01f4eeSJeff Layton 	case F_WRLCK:
2135cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_WRITE))
2136cf01f4eeSJeff Layton 			return -EBADF;
2137cf01f4eeSJeff Layton 	}
2138cf01f4eeSJeff Layton 	return 0;
2139cf01f4eeSJeff Layton }
2140cf01f4eeSJeff Layton 
21411da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
21421da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
21431da177e4SLinus Torvalds  */
2144c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2145c293621bSPeter Staubach 		struct flock __user *l)
21461da177e4SLinus Torvalds {
21471da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
21481da177e4SLinus Torvalds 	struct flock flock;
21491da177e4SLinus Torvalds 	struct inode *inode;
21500b2bac2fSAl Viro 	struct file *f;
21511da177e4SLinus Torvalds 	int error;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	if (file_lock == NULL)
21541da177e4SLinus Torvalds 		return -ENOLCK;
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	/*
21571da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
21581da177e4SLinus Torvalds 	 */
21591da177e4SLinus Torvalds 	error = -EFAULT;
21601da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
21611da177e4SLinus Torvalds 		goto out;
21621da177e4SLinus Torvalds 
2163496ad9aaSAl Viro 	inode = file_inode(filp);
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
21661da177e4SLinus Torvalds 	 * and shared.
21671da177e4SLinus Torvalds 	 */
2168a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
21691da177e4SLinus Torvalds 		error = -EAGAIN;
21701da177e4SLinus Torvalds 		goto out;
21711da177e4SLinus Torvalds 	}
21721da177e4SLinus Torvalds 
2173c293621bSPeter Staubach again:
21741da177e4SLinus Torvalds 	error = flock_to_posix_lock(filp, file_lock, &flock);
21751da177e4SLinus Torvalds 	if (error)
21761da177e4SLinus Torvalds 		goto out;
21775d50ffd7SJeff Layton 
2178cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2179cf01f4eeSJeff Layton 	if (error)
2180cf01f4eeSJeff Layton 		goto out;
2181cf01f4eeSJeff Layton 
21825d50ffd7SJeff Layton 	/*
21835d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2184cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
21855d50ffd7SJeff Layton 	 */
21865d50ffd7SJeff Layton 	switch (cmd) {
21870d3f7a2dSJeff Layton 	case F_OFD_SETLK:
218890478939SJeff Layton 		error = -EINVAL;
218990478939SJeff Layton 		if (flock.l_pid != 0)
219090478939SJeff Layton 			goto out;
219190478939SJeff Layton 
21925d50ffd7SJeff Layton 		cmd = F_SETLK;
2193cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
219473a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
21955d50ffd7SJeff Layton 		break;
21960d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
219790478939SJeff Layton 		error = -EINVAL;
219890478939SJeff Layton 		if (flock.l_pid != 0)
219990478939SJeff Layton 			goto out;
220090478939SJeff Layton 
22015d50ffd7SJeff Layton 		cmd = F_SETLKW;
2202cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
220373a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
22045d50ffd7SJeff Layton 		/* Fallthrough */
22055d50ffd7SJeff Layton 	case F_SETLKW:
22061da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
22071da177e4SLinus Torvalds 	}
22081da177e4SLinus Torvalds 
2209b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2210c293621bSPeter Staubach 
2211c293621bSPeter Staubach 	/*
2212c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2213c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2214c293621bSPeter Staubach 	 */
22150b2bac2fSAl Viro 	/*
22160b2bac2fSAl Viro 	 * we need that spin_lock here - it prevents reordering between
22178116bf4cSJeff Layton 	 * update of i_flctx->flc_posix and check for it done in close().
22180b2bac2fSAl Viro 	 * rcu_read_lock() wouldn't do.
22190b2bac2fSAl Viro 	 */
22200b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
22210b2bac2fSAl Viro 	f = fcheck(fd);
22220b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
22230b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2224c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2225c293621bSPeter Staubach 		goto again;
2226c293621bSPeter Staubach 	}
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds out:
22291da177e4SLinus Torvalds 	locks_free_lock(file_lock);
22301da177e4SLinus Torvalds 	return error;
22311da177e4SLinus Torvalds }
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds #if BITS_PER_LONG == 32
22341da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
22351da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
22361da177e4SLinus Torvalds  */
2237c1e62b8fSJeff Layton int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
22381da177e4SLinus Torvalds {
22399d6a8c5cSMarc Eshel 	struct file_lock file_lock;
22401da177e4SLinus Torvalds 	struct flock64 flock;
22411da177e4SLinus Torvalds 	int error;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	error = -EFAULT;
22441da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
22451da177e4SLinus Torvalds 		goto out;
22461da177e4SLinus Torvalds 	error = -EINVAL;
22471da177e4SLinus Torvalds 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
22481da177e4SLinus Torvalds 		goto out;
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, &file_lock, &flock);
22511da177e4SLinus Torvalds 	if (error)
22521da177e4SLinus Torvalds 		goto out;
22531da177e4SLinus Torvalds 
22540d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
225590478939SJeff Layton 		error = -EINVAL;
225690478939SJeff Layton 		if (flock.l_pid != 0)
225790478939SJeff Layton 			goto out;
225890478939SJeff Layton 
22595d50ffd7SJeff Layton 		cmd = F_GETLK64;
2260cff2fce5SJeff Layton 		file_lock.fl_flags |= FL_OFDLCK;
226173a8f5f7SChristoph Hellwig 		file_lock.fl_owner = filp;
22625d50ffd7SJeff Layton 	}
22635d50ffd7SJeff Layton 
22643ee17abdSJ. Bruce Fields 	error = vfs_test_lock(filp, &file_lock);
22653ee17abdSJ. Bruce Fields 	if (error)
22661da177e4SLinus Torvalds 		goto out;
22671da177e4SLinus Torvalds 
22689d6a8c5cSMarc Eshel 	flock.l_type = file_lock.fl_type;
22699d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK)
22709d6a8c5cSMarc Eshel 		posix_lock_to_flock64(&flock, &file_lock);
22719d6a8c5cSMarc Eshel 
22721da177e4SLinus Torvalds 	error = -EFAULT;
22731da177e4SLinus Torvalds 	if (!copy_to_user(l, &flock, sizeof(flock)))
22741da177e4SLinus Torvalds 		error = 0;
22751da177e4SLinus Torvalds 
2276f328296eSKinglong Mee 	locks_release_private(&file_lock);
22771da177e4SLinus Torvalds out:
22781da177e4SLinus Torvalds 	return error;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
22821da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
22831da177e4SLinus Torvalds  */
2284c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2285c293621bSPeter Staubach 		struct flock64 __user *l)
22861da177e4SLinus Torvalds {
22871da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
22881da177e4SLinus Torvalds 	struct flock64 flock;
22891da177e4SLinus Torvalds 	struct inode *inode;
22900b2bac2fSAl Viro 	struct file *f;
22911da177e4SLinus Torvalds 	int error;
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	if (file_lock == NULL)
22941da177e4SLinus Torvalds 		return -ENOLCK;
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	/*
22971da177e4SLinus Torvalds 	 * This might block, so we do it before checking the inode.
22981da177e4SLinus Torvalds 	 */
22991da177e4SLinus Torvalds 	error = -EFAULT;
23001da177e4SLinus Torvalds 	if (copy_from_user(&flock, l, sizeof(flock)))
23011da177e4SLinus Torvalds 		goto out;
23021da177e4SLinus Torvalds 
2303496ad9aaSAl Viro 	inode = file_inode(filp);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
23061da177e4SLinus Torvalds 	 * and shared.
23071da177e4SLinus Torvalds 	 */
2308a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
23091da177e4SLinus Torvalds 		error = -EAGAIN;
23101da177e4SLinus Torvalds 		goto out;
23111da177e4SLinus Torvalds 	}
23121da177e4SLinus Torvalds 
2313c293621bSPeter Staubach again:
23141da177e4SLinus Torvalds 	error = flock64_to_posix_lock(filp, file_lock, &flock);
23151da177e4SLinus Torvalds 	if (error)
23161da177e4SLinus Torvalds 		goto out;
23175d50ffd7SJeff Layton 
2318cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2319cf01f4eeSJeff Layton 	if (error)
2320cf01f4eeSJeff Layton 		goto out;
2321cf01f4eeSJeff Layton 
23225d50ffd7SJeff Layton 	/*
23235d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2324cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
23255d50ffd7SJeff Layton 	 */
23265d50ffd7SJeff Layton 	switch (cmd) {
23270d3f7a2dSJeff Layton 	case F_OFD_SETLK:
232890478939SJeff Layton 		error = -EINVAL;
232990478939SJeff Layton 		if (flock.l_pid != 0)
233090478939SJeff Layton 			goto out;
233190478939SJeff Layton 
23325d50ffd7SJeff Layton 		cmd = F_SETLK64;
2333cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
233473a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
23355d50ffd7SJeff Layton 		break;
23360d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
233790478939SJeff Layton 		error = -EINVAL;
233890478939SJeff Layton 		if (flock.l_pid != 0)
233990478939SJeff Layton 			goto out;
234090478939SJeff Layton 
23415d50ffd7SJeff Layton 		cmd = F_SETLKW64;
2342cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
234373a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
23445d50ffd7SJeff Layton 		/* Fallthrough */
23455d50ffd7SJeff Layton 	case F_SETLKW64:
23461da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
23471da177e4SLinus Torvalds 	}
23481da177e4SLinus Torvalds 
2349b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2350c293621bSPeter Staubach 
2351c293621bSPeter Staubach 	/*
2352c293621bSPeter Staubach 	 * Attempt to detect a close/fcntl race and recover by
2353c293621bSPeter Staubach 	 * releasing the lock that was just acquired.
2354c293621bSPeter Staubach 	 */
23550b2bac2fSAl Viro 	spin_lock(&current->files->file_lock);
23560b2bac2fSAl Viro 	f = fcheck(fd);
23570b2bac2fSAl Viro 	spin_unlock(&current->files->file_lock);
23580b2bac2fSAl Viro 	if (!error && f != filp && flock.l_type != F_UNLCK) {
2359c293621bSPeter Staubach 		flock.l_type = F_UNLCK;
2360c293621bSPeter Staubach 		goto again;
2361c293621bSPeter Staubach 	}
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds out:
23641da177e4SLinus Torvalds 	locks_free_lock(file_lock);
23651da177e4SLinus Torvalds 	return error;
23661da177e4SLinus Torvalds }
23671da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds /*
23701da177e4SLinus Torvalds  * This function is called when the file is being removed
23711da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
23721da177e4SLinus Torvalds  * are deleted at this time.
23731da177e4SLinus Torvalds  */
23741da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
23751da177e4SLinus Torvalds {
2376ff7b86b8SMiklos Szeredi 	struct file_lock lock;
2377bd61e0a9SJeff Layton 	struct file_lock_context *ctx = file_inode(filp)->i_flctx;
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	/*
23801da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
23811da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
23821da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
23831da177e4SLinus Torvalds 	 */
2384bd61e0a9SJeff Layton 	if (!ctx || list_empty(&ctx->flc_posix))
23851da177e4SLinus Torvalds 		return;
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
238875e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
23891da177e4SLinus Torvalds 	lock.fl_start = 0;
23901da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
23911da177e4SLinus Torvalds 	lock.fl_owner = owner;
23921da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
23931da177e4SLinus Torvalds 	lock.fl_file = filp;
23941da177e4SLinus Torvalds 	lock.fl_ops = NULL;
23951da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
23961da177e4SLinus Torvalds 
2397150b3934SMarc Eshel 	vfs_lock_file(filp, F_SETLK, &lock, NULL);
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
24001da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
24041da177e4SLinus Torvalds 
24053d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
2406dd459bb1SJeff Layton static void
2407dd459bb1SJeff Layton locks_remove_flock(struct file *filp)
2408dd459bb1SJeff Layton {
2409dd459bb1SJeff Layton 	struct file_lock fl = {
2410dd459bb1SJeff Layton 		.fl_owner = filp,
2411dd459bb1SJeff Layton 		.fl_pid = current->tgid,
2412dd459bb1SJeff Layton 		.fl_file = filp,
2413dd459bb1SJeff Layton 		.fl_flags = FL_FLOCK,
2414dd459bb1SJeff Layton 		.fl_type = F_UNLCK,
2415dd459bb1SJeff Layton 		.fl_end = OFFSET_MAX,
2416dd459bb1SJeff Layton 	};
24175263e31eSJeff Layton 	struct file_lock_context *flctx = file_inode(filp)->i_flctx;
2418dd459bb1SJeff Layton 
24193d8e560dSJeff Layton 	if (list_empty(&flctx->flc_flock))
2420dd459bb1SJeff Layton 		return;
2421dd459bb1SJeff Layton 
2422dd459bb1SJeff Layton 	if (filp->f_op->flock)
2423dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2424dd459bb1SJeff Layton 	else
2425dd459bb1SJeff Layton 		flock_lock_file(filp, &fl);
2426dd459bb1SJeff Layton 
2427dd459bb1SJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
2428dd459bb1SJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2429dd459bb1SJeff Layton }
2430dd459bb1SJeff Layton 
24313d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
24328634b51fSJeff Layton static void
24338634b51fSJeff Layton locks_remove_lease(struct file *filp)
24348634b51fSJeff Layton {
24358634b51fSJeff Layton 	struct inode *inode = file_inode(filp);
24368634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
24378634b51fSJeff Layton 	struct file_lock *fl, *tmp;
24388634b51fSJeff Layton 	LIST_HEAD(dispose);
24398634b51fSJeff Layton 
24403d8e560dSJeff Layton 	if (list_empty(&ctx->flc_lease))
24418634b51fSJeff Layton 		return;
24428634b51fSJeff Layton 
24436109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
24448634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
24457448cc37SJeff Layton 		lease_modify(fl, F_UNLCK, &dispose);
24466109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
24478634b51fSJeff Layton 	locks_dispose_list(&dispose);
24488634b51fSJeff Layton }
24498634b51fSJeff Layton 
24501da177e4SLinus Torvalds /*
24511da177e4SLinus Torvalds  * This function is called on the last close of an open file.
24521da177e4SLinus Torvalds  */
245378ed8a13SJeff Layton void locks_remove_file(struct file *filp)
24541da177e4SLinus Torvalds {
24553d8e560dSJeff Layton 	if (!file_inode(filp)->i_flctx)
24563d8e560dSJeff Layton 		return;
24573d8e560dSJeff Layton 
2458dd459bb1SJeff Layton 	/* remove any OFD locks */
245973a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
24605d50ffd7SJeff Layton 
2461dd459bb1SJeff Layton 	/* remove flock locks */
2462dd459bb1SJeff Layton 	locks_remove_flock(filp);
2463dd459bb1SJeff Layton 
24648634b51fSJeff Layton 	/* remove any leases */
24658634b51fSJeff Layton 	locks_remove_lease(filp);
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds /**
24691da177e4SLinus Torvalds  *	posix_unblock_lock - stop waiting for a file lock
24701da177e4SLinus Torvalds  *	@waiter: the lock which was waiting
24711da177e4SLinus Torvalds  *
24721da177e4SLinus Torvalds  *	lockd needs to block waiting for locks.
24731da177e4SLinus Torvalds  */
247464a318eeSJ. Bruce Fields int
2475f891a29fSJeff Layton posix_unblock_lock(struct file_lock *waiter)
24761da177e4SLinus Torvalds {
247764a318eeSJ. Bruce Fields 	int status = 0;
247864a318eeSJ. Bruce Fields 
24797b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
24805996a298SJ. Bruce Fields 	if (waiter->fl_next)
24811da177e4SLinus Torvalds 		__locks_delete_block(waiter);
248264a318eeSJ. Bruce Fields 	else
248364a318eeSJ. Bruce Fields 		status = -ENOENT;
24847b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
248564a318eeSJ. Bruce Fields 	return status;
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds EXPORT_SYMBOL(posix_unblock_lock);
24881da177e4SLinus Torvalds 
24899b9d2ab4SMarc Eshel /**
24909b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
24919b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
24929b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
24939b9d2ab4SMarc Eshel  *
24949b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
24959b9d2ab4SMarc Eshel  */
24969b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
24979b9d2ab4SMarc Eshel {
249872c2d531SAl Viro 	if (filp->f_op->lock)
24999b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
25009b9d2ab4SMarc Eshel 	return 0;
25019b9d2ab4SMarc Eshel }
25029b9d2ab4SMarc Eshel 
25039b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
25049b9d2ab4SMarc Eshel 
25057f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2506d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
25077f8ada98SPavel Emelyanov #include <linux/seq_file.h>
25087f8ada98SPavel Emelyanov 
25097012b02aSJeff Layton struct locks_iterator {
25107012b02aSJeff Layton 	int	li_cpu;
25117012b02aSJeff Layton 	loff_t	li_pos;
25127012b02aSJeff Layton };
25137012b02aSJeff Layton 
25147f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
251599dc8292SJerome Marchand 			    loff_t id, char *pfx)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct inode *inode = NULL;
2518ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2519ab1f1611SVitaliy Gusev 
2520ab1f1611SVitaliy Gusev 	if (fl->fl_nspid)
25216c5f3e7bSPavel Emelyanov 		fl_pid = pid_vnr(fl->fl_nspid);
2522ab1f1611SVitaliy Gusev 	else
2523ab1f1611SVitaliy Gusev 		fl_pid = fl->fl_pid;
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2526496ad9aaSAl Viro 		inode = file_inode(fl->fl_file);
25271da177e4SLinus Torvalds 
252899dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
25291da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2530c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
25315315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2532cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
25335315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2534c918d42aSJeff Layton 		else
25355315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2536c918d42aSJeff Layton 
2537c918d42aSJeff Layton 		seq_printf(f, " %s ",
25381da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2539a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
25401da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
25411da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
25425315c26aSFabian Frederick 			seq_puts(f, "FLOCK  MSNFS     ");
25431da177e4SLinus Torvalds 		} else {
25445315c26aSFabian Frederick 			seq_puts(f, "FLOCK  ADVISORY  ");
25451da177e4SLinus Torvalds 		}
25461da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
25478144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
25488144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
25498144f1f6SJeff Layton 		else
25505315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
25518144f1f6SJeff Layton 
2552ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
25535315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
25541da177e4SLinus Torvalds 		else if (fl->fl_file)
25555315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
25561da177e4SLinus Torvalds 		else
25575315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
25581da177e4SLinus Torvalds 	} else {
25595315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
25601da177e4SLinus Torvalds 	}
25611da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
25627f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
25631da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
25641da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
25651da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
25661da177e4SLinus Torvalds 	} else {
25677f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2568ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
25690ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
25700ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
25711da177e4SLinus Torvalds 	}
25721da177e4SLinus Torvalds 	if (inode) {
25731da177e4SLinus Torvalds #ifdef WE_CAN_BREAK_LSLK_NOW
2574ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %s:%ld ", fl_pid,
25751da177e4SLinus Torvalds 				inode->i_sb->s_id, inode->i_ino);
25761da177e4SLinus Torvalds #else
25771da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t ;-( */
2578ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
25791da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
25801da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
25811da177e4SLinus Torvalds #endif
25821da177e4SLinus Torvalds 	} else {
2583ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
25841da177e4SLinus Torvalds 	}
25851da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
25861da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
25877f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
25881da177e4SLinus Torvalds 		else
25897f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
25901da177e4SLinus Torvalds 	} else {
25915315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
25921da177e4SLinus Torvalds 	}
25931da177e4SLinus Torvalds }
25941da177e4SLinus Torvalds 
25957f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
25961da177e4SLinus Torvalds {
25977012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
25987f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
25997f8ada98SPavel Emelyanov 
2600139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
26017f8ada98SPavel Emelyanov 
26027012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
26037f8ada98SPavel Emelyanov 
26047f8ada98SPavel Emelyanov 	list_for_each_entry(bfl, &fl->fl_block, fl_block)
26057012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
26067f8ada98SPavel Emelyanov 
26077f8ada98SPavel Emelyanov 	return 0;
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
26107f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2611b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
26121da177e4SLinus Torvalds {
26137012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
261499dc8292SJerome Marchand 
26157012b02aSJeff Layton 	iter->li_pos = *pos + 1;
26167012b02aSJeff Layton 	lg_global_lock(&file_lock_lglock);
26177b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
26187012b02aSJeff Layton 	return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
26191da177e4SLinus Torvalds }
26207f8ada98SPavel Emelyanov 
26217f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
26227f8ada98SPavel Emelyanov {
26237012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
26247012b02aSJeff Layton 
26257012b02aSJeff Layton 	++iter->li_pos;
26267012b02aSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
26271da177e4SLinus Torvalds }
26287f8ada98SPavel Emelyanov 
26297f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2630b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
26317f8ada98SPavel Emelyanov {
26327b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
26337012b02aSJeff Layton 	lg_global_unlock(&file_lock_lglock);
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
2636d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
26377f8ada98SPavel Emelyanov 	.start	= locks_start,
26387f8ada98SPavel Emelyanov 	.next	= locks_next,
26397f8ada98SPavel Emelyanov 	.stop	= locks_stop,
26407f8ada98SPavel Emelyanov 	.show	= locks_show,
26417f8ada98SPavel Emelyanov };
2642d8ba7a36SAlexey Dobriyan 
2643d8ba7a36SAlexey Dobriyan static int locks_open(struct inode *inode, struct file *filp)
2644d8ba7a36SAlexey Dobriyan {
26457012b02aSJeff Layton 	return seq_open_private(filp, &locks_seq_operations,
26467012b02aSJeff Layton 					sizeof(struct locks_iterator));
2647d8ba7a36SAlexey Dobriyan }
2648d8ba7a36SAlexey Dobriyan 
2649d8ba7a36SAlexey Dobriyan static const struct file_operations proc_locks_operations = {
2650d8ba7a36SAlexey Dobriyan 	.open		= locks_open,
2651d8ba7a36SAlexey Dobriyan 	.read		= seq_read,
2652d8ba7a36SAlexey Dobriyan 	.llseek		= seq_lseek,
265399dc8292SJerome Marchand 	.release	= seq_release_private,
2654d8ba7a36SAlexey Dobriyan };
2655d8ba7a36SAlexey Dobriyan 
2656d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2657d8ba7a36SAlexey Dobriyan {
2658d8ba7a36SAlexey Dobriyan 	proc_create("locks", 0, NULL, &proc_locks_operations);
2659d8ba7a36SAlexey Dobriyan 	return 0;
2660d8ba7a36SAlexey Dobriyan }
2661d8ba7a36SAlexey Dobriyan module_init(proc_locks_init);
26627f8ada98SPavel Emelyanov #endif
26637f8ada98SPavel Emelyanov 
26641da177e4SLinus Torvalds static int __init filelock_init(void)
26651da177e4SLinus Torvalds {
26667012b02aSJeff Layton 	int i;
26677012b02aSJeff Layton 
26684a075e39SJeff Layton 	flctx_cache = kmem_cache_create("file_lock_ctx",
26694a075e39SJeff Layton 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
26704a075e39SJeff Layton 
26711da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2672ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2673ee19cc40SMiklos Szeredi 
26747012b02aSJeff Layton 	lg_lock_init(&file_lock_lglock, "file_lock_lglock");
26757012b02aSJeff Layton 
26767012b02aSJeff Layton 	for_each_possible_cpu(i)
26777012b02aSJeff Layton 		INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
26787012b02aSJeff Layton 
26791da177e4SLinus Torvalds 	return 0;
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds 
26821da177e4SLinus Torvalds core_initcall(filelock_init);
2683