xref: /linux/fs/locks.c (revision cb03f94ffb070b13bc0fa58b4ef4fdb558418d27)
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.
115fd7732e0SNeilBrown  *
116fd7732e0SNeilBrown  * Locking conflicts and dependencies:
117fd7732e0SNeilBrown  * If multiple threads attempt to lock the same byte (or flock the same file)
118fd7732e0SNeilBrown  * only one can be granted the lock, and other must wait their turn.
119fd7732e0SNeilBrown  * The first lock has been "applied" or "granted", the others are "waiting"
120fd7732e0SNeilBrown  * and are "blocked" by the "applied" lock..
121fd7732e0SNeilBrown  *
122fd7732e0SNeilBrown  * Waiting and applied locks are all kept in trees whose properties are:
123fd7732e0SNeilBrown  *
124fd7732e0SNeilBrown  *	- the root of a tree may be an applied or waiting lock.
125fd7732e0SNeilBrown  *	- every other node in the tree is a waiting lock that
126fd7732e0SNeilBrown  *	  conflicts with every ancestor of that node.
127fd7732e0SNeilBrown  *
128fd7732e0SNeilBrown  * Every such tree begins life as a waiting singleton which obviously
129fd7732e0SNeilBrown  * satisfies the above properties.
130fd7732e0SNeilBrown  *
131fd7732e0SNeilBrown  * The only ways we modify trees preserve these properties:
132fd7732e0SNeilBrown  *
133fd7732e0SNeilBrown  *	1. We may add a new leaf node, but only after first verifying that it
134fd7732e0SNeilBrown  *	   conflicts with all of its ancestors.
135fd7732e0SNeilBrown  *	2. We may remove the root of a tree, creating a new singleton
136fd7732e0SNeilBrown  *	   tree from the root and N new trees rooted in the immediate
137fd7732e0SNeilBrown  *	   children.
138fd7732e0SNeilBrown  *	3. If the root of a tree is not currently an applied lock, we may
139fd7732e0SNeilBrown  *	   apply it (if possible).
140fd7732e0SNeilBrown  *	4. We may upgrade the root of the tree (either extend its range,
141fd7732e0SNeilBrown  *	   or upgrade its entire range from read to write).
142fd7732e0SNeilBrown  *
143fd7732e0SNeilBrown  * When an applied lock is modified in a way that reduces or downgrades any
144fd7732e0SNeilBrown  * part of its range, we remove all its children (2 above).  This particularly
145fd7732e0SNeilBrown  * happens when a lock is unlocked.
146fd7732e0SNeilBrown  *
147fd7732e0SNeilBrown  * For each of those child trees we "wake up" the thread which is
148fd7732e0SNeilBrown  * waiting for the lock so it can continue handling as follows: if the
149fd7732e0SNeilBrown  * root of the tree applies, we do so (3).  If it doesn't, it must
150fd7732e0SNeilBrown  * conflict with some applied lock.  We remove (wake up) all of its children
151fd7732e0SNeilBrown  * (2), and add it is a new leaf to the tree rooted in the applied
152fd7732e0SNeilBrown  * lock (1).  We then repeat the process recursively with those
153fd7732e0SNeilBrown  * children.
154fd7732e0SNeilBrown  *
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #include <linux/capability.h>
1581da177e4SLinus Torvalds #include <linux/file.h>
1599f3acc31SAl Viro #include <linux/fdtable.h>
1601da177e4SLinus Torvalds #include <linux/fs.h>
1611da177e4SLinus Torvalds #include <linux/init.h>
1621da177e4SLinus Torvalds #include <linux/security.h>
1631da177e4SLinus Torvalds #include <linux/slab.h>
1641da177e4SLinus Torvalds #include <linux/syscalls.h>
1651da177e4SLinus Torvalds #include <linux/time.h>
1664fb3a538SDipankar Sarma #include <linux/rcupdate.h>
167ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
16848f74186SJeff Layton #include <linux/hashtable.h>
1697012b02aSJeff Layton #include <linux/percpu.h>
1701da177e4SLinus Torvalds 
17162af4f1fSJeff Layton #define CREATE_TRACE_POINTS
17262af4f1fSJeff Layton #include <trace/events/filelock.h>
17362af4f1fSJeff Layton 
1747c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
1771da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
17811afe9f7SChristoph Hellwig #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
179cff2fce5SJeff Layton #define IS_OFDLCK(fl)	(fl->fl_flags & FL_OFDLCK)
1809d5b86acSBenjamin Coddington #define IS_REMOTELCK(fl)	(fl->fl_pid <= 0)
1811da177e4SLinus Torvalds 
182ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
183ab83fa4bSJ. Bruce Fields {
184778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
185778fc546SJ. Bruce Fields }
186778fc546SJ. Bruce Fields 
187778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
188778fc546SJ. Bruce Fields {
189778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
190778fc546SJ. Bruce Fields 		return F_UNLCK;
191778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
192778fc546SJ. Bruce Fields 		return F_RDLCK;
193778fc546SJ. Bruce Fields 	return fl->fl_type;
194ab83fa4bSJ. Bruce Fields }
195ab83fa4bSJ. Bruce Fields 
1961da177e4SLinus Torvalds int leases_enable = 1;
1971da177e4SLinus Torvalds int lease_break_time = 45;
1981da177e4SLinus Torvalds 
1991c8c601aSJeff Layton /*
2007012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
2017c3f654dSPeter Zijlstra  * keep a list on each CPU, with each list protected by its own spinlock.
2027c3f654dSPeter Zijlstra  * Global serialization is done using file_rwsem.
2037c3f654dSPeter Zijlstra  *
2047c3f654dSPeter Zijlstra  * Note that alterations to the list also require that the relevant flc_lock is
2057c3f654dSPeter Zijlstra  * held.
2061c8c601aSJeff Layton  */
2077c3f654dSPeter Zijlstra struct file_lock_list_struct {
2087c3f654dSPeter Zijlstra 	spinlock_t		lock;
2097c3f654dSPeter Zijlstra 	struct hlist_head	hlist;
2107c3f654dSPeter Zijlstra };
2117c3f654dSPeter Zijlstra static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
212aba37660SPeter Zijlstra DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
21388974691SJeff Layton 
2141c8c601aSJeff Layton /*
21548f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
2167b2296afSJeff Layton  * It is protected by blocked_lock_lock.
21748f74186SJeff Layton  *
21848f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
21948f74186SJeff Layton  * particular lockowner is waiting on.
22048f74186SJeff Layton  *
22148f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
22248f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
22348f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
2241c8c601aSJeff Layton  */
22548f74186SJeff Layton #define BLOCKED_HASH_BITS	7
22648f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
22788974691SJeff Layton 
2281c8c601aSJeff Layton /*
2297b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
2307b2296afSJeff Layton  * want to be holding this lock.
2311c8c601aSJeff Layton  *
232ada5c1daSNeilBrown  * In addition, it also protects the fl->fl_blocked_requests list, and the
233ada5c1daSNeilBrown  * fl->fl_blocker pointer for file_lock structures that are acting as lock
234ada5c1daSNeilBrown  * requests (in contrast to those that are acting as records of acquired locks).
2351c8c601aSJeff Layton  *
2361c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
2376109c850SJeff Layton  * we often hold the flc_lock as well. In certain cases, when reading the fields
2381c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
2396109c850SJeff Layton  * flc_lock.
2401c8c601aSJeff Layton  */
2417b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
2421da177e4SLinus Torvalds 
2434a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly;
244e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
2451da177e4SLinus Torvalds 
2464a075e39SJeff Layton static struct file_lock_context *
2475c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type)
2484a075e39SJeff Layton {
249128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2504a075e39SJeff Layton 
251128a3785SDmitry Vyukov 	/* paired with cmpxchg() below */
252128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
253128a3785SDmitry Vyukov 	if (likely(ctx) || type == F_UNLCK)
2544a075e39SJeff Layton 		goto out;
2554a075e39SJeff Layton 
256128a3785SDmitry Vyukov 	ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
257128a3785SDmitry Vyukov 	if (!ctx)
2584a075e39SJeff Layton 		goto out;
2594a075e39SJeff Layton 
260128a3785SDmitry Vyukov 	spin_lock_init(&ctx->flc_lock);
261128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_flock);
262128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_posix);
263128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_lease);
2644a075e39SJeff Layton 
2654a075e39SJeff Layton 	/*
2664a075e39SJeff Layton 	 * Assign the pointer if it's not already assigned. If it is, then
2674a075e39SJeff Layton 	 * free the context we just allocated.
2684a075e39SJeff Layton 	 */
269128a3785SDmitry Vyukov 	if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
270128a3785SDmitry Vyukov 		kmem_cache_free(flctx_cache, ctx);
271128a3785SDmitry Vyukov 		ctx = smp_load_acquire(&inode->i_flctx);
272128a3785SDmitry Vyukov 	}
2734a075e39SJeff Layton out:
2741890910fSJeff Layton 	trace_locks_get_lock_context(inode, type, ctx);
275128a3785SDmitry Vyukov 	return ctx;
2764a075e39SJeff Layton }
2774a075e39SJeff Layton 
278e24dadabSJeff Layton static void
279e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type)
280e24dadabSJeff Layton {
281e24dadabSJeff Layton 	struct file_lock *fl;
282e24dadabSJeff Layton 
283e24dadabSJeff Layton 	list_for_each_entry(fl, list, fl_list) {
284e24dadabSJeff Layton 		pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
285e24dadabSJeff Layton 	}
286e24dadabSJeff Layton }
287e24dadabSJeff Layton 
288e24dadabSJeff Layton static void
289e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode)
290e24dadabSJeff Layton {
291e24dadabSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
292e24dadabSJeff Layton 
293e24dadabSJeff Layton 	if (unlikely(!list_empty(&ctx->flc_flock) ||
294e24dadabSJeff Layton 		     !list_empty(&ctx->flc_posix) ||
295e24dadabSJeff Layton 		     !list_empty(&ctx->flc_lease))) {
296e24dadabSJeff Layton 		pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
297e24dadabSJeff Layton 			MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
298e24dadabSJeff Layton 			inode->i_ino);
299e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
300e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
301e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
302e24dadabSJeff Layton 	}
303e24dadabSJeff Layton }
304e24dadabSJeff Layton 
3053953704fSBenjamin Coddington static void
3063953704fSBenjamin Coddington locks_check_ctx_file_list(struct file *filp, struct list_head *list,
3073953704fSBenjamin Coddington 				char *list_type)
3083953704fSBenjamin Coddington {
3093953704fSBenjamin Coddington 	struct file_lock *fl;
3103953704fSBenjamin Coddington 	struct inode *inode = locks_inode(filp);
3113953704fSBenjamin Coddington 
3123953704fSBenjamin Coddington 	list_for_each_entry(fl, list, fl_list)
3133953704fSBenjamin Coddington 		if (fl->fl_file == filp)
3143953704fSBenjamin Coddington 			pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
3153953704fSBenjamin Coddington 				" fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
3163953704fSBenjamin Coddington 				list_type, MAJOR(inode->i_sb->s_dev),
3173953704fSBenjamin Coddington 				MINOR(inode->i_sb->s_dev), inode->i_ino,
3183953704fSBenjamin Coddington 				fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
3193953704fSBenjamin Coddington }
3203953704fSBenjamin Coddington 
3214a075e39SJeff Layton void
322f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode)
3234a075e39SJeff Layton {
324f27a0fe0SJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
325f27a0fe0SJeff Layton 
326e24dadabSJeff Layton 	if (unlikely(ctx)) {
327e24dadabSJeff Layton 		locks_check_ctx_lists(inode);
3284a075e39SJeff Layton 		kmem_cache_free(flctx_cache, ctx);
3294a075e39SJeff Layton 	}
3304a075e39SJeff Layton }
3314a075e39SJeff Layton 
332ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
333a51cb91dSMiklos Szeredi {
334139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
3356dee60f6SJeff Layton 	INIT_LIST_HEAD(&fl->fl_list);
336ada5c1daSNeilBrown 	INIT_LIST_HEAD(&fl->fl_blocked_requests);
337ada5c1daSNeilBrown 	INIT_LIST_HEAD(&fl->fl_blocked_member);
338ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
339a51cb91dSMiklos Szeredi }
340a51cb91dSMiklos Szeredi 
3411da177e4SLinus Torvalds /* Allocate an empty lock structure. */
342c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
3431da177e4SLinus Torvalds {
344ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
345a51cb91dSMiklos Szeredi 
346a51cb91dSMiklos Szeredi 	if (fl)
347ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
348a51cb91dSMiklos Szeredi 
349a51cb91dSMiklos Szeredi 	return fl;
3501da177e4SLinus Torvalds }
351c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
3521da177e4SLinus Torvalds 
353a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
35447831f35STrond Myklebust {
35547831f35STrond Myklebust 	if (fl->fl_ops) {
35647831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
35747831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
35847831f35STrond Myklebust 		fl->fl_ops = NULL;
35947831f35STrond Myklebust 	}
36047831f35STrond Myklebust 
3615c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
362cae80b30SJeff Layton 		if (fl->fl_lmops->lm_put_owner) {
363cae80b30SJeff Layton 			fl->fl_lmops->lm_put_owner(fl->fl_owner);
364cae80b30SJeff Layton 			fl->fl_owner = NULL;
365cae80b30SJeff Layton 		}
3665c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
3675c97d7b1SKinglong Mee 	}
36847831f35STrond Myklebust }
369a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
37047831f35STrond Myklebust 
3711da177e4SLinus Torvalds /* Free a lock which is not in use. */
37205fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
3731da177e4SLinus Torvalds {
3745ce29646SMiklos Szeredi 	BUG_ON(waitqueue_active(&fl->fl_wait));
3756dee60f6SJeff Layton 	BUG_ON(!list_empty(&fl->fl_list));
376ada5c1daSNeilBrown 	BUG_ON(!list_empty(&fl->fl_blocked_requests));
377ada5c1daSNeilBrown 	BUG_ON(!list_empty(&fl->fl_blocked_member));
378139ca04eSJeff Layton 	BUG_ON(!hlist_unhashed(&fl->fl_link));
3791da177e4SLinus Torvalds 
38047831f35STrond Myklebust 	locks_release_private(fl);
3811da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
3821da177e4SLinus Torvalds }
38305fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
3841da177e4SLinus Torvalds 
385ed9814d8SJeff Layton static void
386ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
387ed9814d8SJeff Layton {
388ed9814d8SJeff Layton 	struct file_lock *fl;
389ed9814d8SJeff Layton 
390ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
3916dee60f6SJeff Layton 		fl = list_first_entry(dispose, struct file_lock, fl_list);
3926dee60f6SJeff Layton 		list_del_init(&fl->fl_list);
393ed9814d8SJeff Layton 		locks_free_lock(fl);
394ed9814d8SJeff Layton 	}
395ed9814d8SJeff Layton }
396ed9814d8SJeff Layton 
3971da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
3981da177e4SLinus Torvalds {
399ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
400ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4061da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
4071da177e4SLinus Torvalds  */
4083fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
4111da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
4120996905fSTrond Myklebust 	new->fl_file = NULL;
4131da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
4141da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
4151da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
4161da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
417f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
4180996905fSTrond Myklebust 	new->fl_ops = NULL;
419f328296eSKinglong Mee 
420f328296eSKinglong Mee 	if (fl->fl_lmops) {
421f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
422cae80b30SJeff Layton 			fl->fl_lmops->lm_get_owner(fl->fl_owner);
423f328296eSKinglong Mee 	}
4240996905fSTrond Myklebust }
4253fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
4260996905fSTrond Myklebust 
4270996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4280996905fSTrond Myklebust {
429566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
430566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
4310996905fSTrond Myklebust 
4323fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
433f328296eSKinglong Mee 
4340996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
4351da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
43647831f35STrond Myklebust 
437f328296eSKinglong Mee 	if (fl->fl_ops) {
438f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
439f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
440f328296eSKinglong Mee 	}
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
4441da177e4SLinus Torvalds 
4455946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
4465946c431SNeilBrown {
4475946c431SNeilBrown 	struct file_lock *f;
4485946c431SNeilBrown 
4495946c431SNeilBrown 	/*
4505946c431SNeilBrown 	 * As ctx->flc_lock is held, new requests cannot be added to
4515946c431SNeilBrown 	 * ->fl_blocked_requests, so we don't need a lock to check if it
4525946c431SNeilBrown 	 * is empty.
4535946c431SNeilBrown 	 */
4545946c431SNeilBrown 	if (list_empty(&fl->fl_blocked_requests))
4555946c431SNeilBrown 		return;
4565946c431SNeilBrown 	spin_lock(&blocked_lock_lock);
4575946c431SNeilBrown 	list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
4585946c431SNeilBrown 	list_for_each_entry(f, &fl->fl_blocked_requests, fl_blocked_member)
4595946c431SNeilBrown 		f->fl_blocker = new;
4605946c431SNeilBrown 	spin_unlock(&blocked_lock_lock);
4615946c431SNeilBrown }
4625946c431SNeilBrown 
4631da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
4641da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
4651da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
4661da177e4SLinus Torvalds 	switch (cmd) {
4671da177e4SLinus Torvalds 	case LOCK_SH:
4681da177e4SLinus Torvalds 		return F_RDLCK;
4691da177e4SLinus Torvalds 	case LOCK_EX:
4701da177e4SLinus Torvalds 		return F_WRLCK;
4711da177e4SLinus Torvalds 	case LOCK_UN:
4721da177e4SLinus Torvalds 		return F_UNLCK;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 	return -EINVAL;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
4786e129d00SJeff Layton static struct file_lock *
479d6367d62SNeilBrown flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
4826e129d00SJeff Layton 
4831da177e4SLinus Torvalds 	if (type < 0)
4846e129d00SJeff Layton 		return ERR_PTR(type);
4851da177e4SLinus Torvalds 
486d6367d62SNeilBrown 	if (fl == NULL) {
4871da177e4SLinus Torvalds 		fl = locks_alloc_lock();
4881da177e4SLinus Torvalds 		if (fl == NULL)
4896e129d00SJeff Layton 			return ERR_PTR(-ENOMEM);
490d6367d62SNeilBrown 	} else {
491d6367d62SNeilBrown 		locks_init_lock(fl);
492d6367d62SNeilBrown 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	fl->fl_file = filp;
49573a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
4961da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4971da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
4981da177e4SLinus Torvalds 	fl->fl_type = type;
4991da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
5001da177e4SLinus Torvalds 
5016e129d00SJeff Layton 	return fl;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5040ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	switch (type) {
5071da177e4SLinus Torvalds 	case F_RDLCK:
5081da177e4SLinus Torvalds 	case F_WRLCK:
5091da177e4SLinus Torvalds 	case F_UNLCK:
5101da177e4SLinus Torvalds 		fl->fl_type = type;
5111da177e4SLinus Torvalds 		break;
5121da177e4SLinus Torvalds 	default:
5131da177e4SLinus Torvalds 		return -EINVAL;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 	return 0;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
519ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
520ef12e72aSJ. Bruce Fields {
521ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
522ef12e72aSJ. Bruce Fields 	case SEEK_SET:
523ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
524ef12e72aSJ. Bruce Fields 		break;
525ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
526ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
527ef12e72aSJ. Bruce Fields 		break;
528ef12e72aSJ. Bruce Fields 	case SEEK_END:
529ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
530ef12e72aSJ. Bruce Fields 		break;
531ef12e72aSJ. Bruce Fields 	default:
532ef12e72aSJ. Bruce Fields 		return -EINVAL;
533ef12e72aSJ. Bruce Fields 	}
534ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
535ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
536ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
537ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
538ef12e72aSJ. Bruce Fields 		return -EINVAL;
539ef12e72aSJ. Bruce Fields 
540ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
541ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
542ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
543ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
544ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
545ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
546ef12e72aSJ. Bruce Fields 
547ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
548ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
549ef12e72aSJ. Bruce Fields 			return -EINVAL;
550ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
551ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
552ef12e72aSJ. Bruce Fields 	} else
553ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
554ef12e72aSJ. Bruce Fields 
555ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
556ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
557ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
558ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
559ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
560ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
561ef12e72aSJ. Bruce Fields 
562ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
563ef12e72aSJ. Bruce Fields }
564ef12e72aSJ. Bruce Fields 
5651da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
5661da177e4SLinus Torvalds  * style lock.
5671da177e4SLinus Torvalds  */
5681da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
5691da177e4SLinus Torvalds 			       struct flock *l)
5701da177e4SLinus Torvalds {
571ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
572ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
573ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
574ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
575ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
576ef12e72aSJ. Bruce Fields 	};
5771da177e4SLinus Torvalds 
578ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /* default lease lock manager operations */
5824d01b7f5SJeff Layton static bool
5834d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
5864d01b7f5SJeff Layton 	return false;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891c7dd2ffSJeff Layton static void
5901c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv)
5911c7dd2ffSJeff Layton {
5921c7dd2ffSJeff Layton 	struct file *filp = fl->fl_file;
5931c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
5941c7dd2ffSJeff Layton 
5951c7dd2ffSJeff Layton 	/*
5961c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
5971c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
5981c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
5991c7dd2ffSJeff Layton 	 */
6001c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
6011c7dd2ffSJeff Layton 		*priv = NULL;
6021c7dd2ffSJeff Layton 
60301919134SEric W. Biederman 	__f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
6041c7dd2ffSJeff Layton }
6051c7dd2ffSJeff Layton 
6067b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
6078fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
6088fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
6091c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
6101da177e4SLinus Torvalds };
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
6141da177e4SLinus Torvalds  */
6150ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
6161da177e4SLinus Torvalds {
61775dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
61875dff55aSTrond Myklebust 		return -EINVAL;
61975dff55aSTrond Myklebust 
6207ca76311SJeff Layton 	fl->fl_owner = filp;
6211da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	fl->fl_file = filp;
6241da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
6251da177e4SLinus Torvalds 	fl->fl_start = 0;
6261da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
6271da177e4SLinus Torvalds 	fl->fl_ops = NULL;
6281da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
6291da177e4SLinus Torvalds 	return 0;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
6330ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
63675dff55aSTrond Myklebust 	int error = -ENOMEM;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (fl == NULL)
639e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
64275dff55aSTrond Myklebust 	if (error) {
64375dff55aSTrond Myklebust 		locks_free_lock(fl);
644e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
64575dff55aSTrond Myklebust 	}
646e32b8ee2SJ. Bruce Fields 	return fl;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /* Check if two locks overlap each other.
6501da177e4SLinus Torvalds  */
6511da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
6541da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  * Check whether two locks have the same owner.
6591da177e4SLinus Torvalds  */
66033443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
6611da177e4SLinus Torvalds {
6628fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
6631da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
6648fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
6651da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6686109c850SJeff Layton /* Must be called with the flc_lock held! */
6696ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
67088974691SJeff Layton {
6717c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
6727c3f654dSPeter Zijlstra 
673aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
674aba37660SPeter Zijlstra 
6757c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
6767012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
6777c3f654dSPeter Zijlstra 	hlist_add_head(&fl->fl_link, &fll->hlist);
6787c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
67988974691SJeff Layton }
68088974691SJeff Layton 
6816109c850SJeff Layton /* Must be called with the flc_lock held! */
6826ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
68388974691SJeff Layton {
6847c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll;
6857c3f654dSPeter Zijlstra 
686aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
687aba37660SPeter Zijlstra 
6887012b02aSJeff Layton 	/*
6897012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
6906109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
6917012b02aSJeff Layton 	 * also require that it be held.
6927012b02aSJeff Layton 	 */
6937012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
6947012b02aSJeff Layton 		return;
6957c3f654dSPeter Zijlstra 
6967c3f654dSPeter Zijlstra 	fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
6977c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
698139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
6997c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
70088974691SJeff Layton }
70188974691SJeff Layton 
7023999e493SJeff Layton static unsigned long
7033999e493SJeff Layton posix_owner_key(struct file_lock *fl)
7043999e493SJeff Layton {
7053999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
7063999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
7073999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
7083999e493SJeff Layton }
7093999e493SJeff Layton 
7106ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
71188974691SJeff Layton {
712663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
713663d5af7SDaniel Wagner 
7143999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
71588974691SJeff Layton }
71688974691SJeff Layton 
7176ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
71888974691SJeff Layton {
719663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
720663d5af7SDaniel Wagner 
72148f74186SJeff Layton 	hash_del(&waiter->fl_link);
72288974691SJeff Layton }
72388974691SJeff Layton 
7241da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
7251da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
7261c8c601aSJeff Layton  *
7277b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
7281da177e4SLinus Torvalds  */
72933443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
7301da177e4SLinus Torvalds {
73188974691SJeff Layton 	locks_delete_global_blocked(waiter);
732ada5c1daSNeilBrown 	list_del_init(&waiter->fl_blocked_member);
733ada5c1daSNeilBrown 	waiter->fl_blocker = NULL;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
736ad6bbd8bSNeilBrown static void __locks_wake_up_blocks(struct file_lock *blocker)
737ad6bbd8bSNeilBrown {
738ad6bbd8bSNeilBrown 	while (!list_empty(&blocker->fl_blocked_requests)) {
739ad6bbd8bSNeilBrown 		struct file_lock *waiter;
740ad6bbd8bSNeilBrown 
741ad6bbd8bSNeilBrown 		waiter = list_first_entry(&blocker->fl_blocked_requests,
742ad6bbd8bSNeilBrown 					  struct file_lock, fl_blocked_member);
743ad6bbd8bSNeilBrown 		__locks_delete_block(waiter);
744ad6bbd8bSNeilBrown 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
745ad6bbd8bSNeilBrown 			waiter->fl_lmops->lm_notify(waiter);
746ad6bbd8bSNeilBrown 		else
747ad6bbd8bSNeilBrown 			wake_up(&waiter->fl_wait);
748ad6bbd8bSNeilBrown 	}
749ad6bbd8bSNeilBrown }
750ad6bbd8bSNeilBrown 
751*cb03f94fSNeilBrown /**
752*cb03f94fSNeilBrown  *	locks_delete_lock - stop waiting for a file lock
753*cb03f94fSNeilBrown  *	@waiter: the lock which was waiting
754*cb03f94fSNeilBrown  *
755*cb03f94fSNeilBrown  *	lockd/nfsd need to disconnect the lock while working on it.
756*cb03f94fSNeilBrown  */
757*cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter)
7581da177e4SLinus Torvalds {
759*cb03f94fSNeilBrown 	int status = -ENOENT;
760*cb03f94fSNeilBrown 
76116306a61SNeilBrown 	/*
76216306a61SNeilBrown 	 * If fl_blocker is NULL, it won't be set again as this thread
76316306a61SNeilBrown 	 * "owns" the lock and is the only one that might try to claim
76416306a61SNeilBrown 	 * the lock.  So it is safe to test fl_blocker locklessly.
76516306a61SNeilBrown 	 * Also if fl_blocker is NULL, this waiter is not listed on
76616306a61SNeilBrown 	 * fl_blocked_requests for some lock, so no other request can
76716306a61SNeilBrown 	 * be added to the list of fl_blocked_requests for this
76816306a61SNeilBrown 	 * request.  So if fl_blocker is NULL, it is safe to
76916306a61SNeilBrown 	 * locklessly check if fl_blocked_requests is empty.  If both
77016306a61SNeilBrown 	 * of these checks succeed, there is no need to take the lock.
77116306a61SNeilBrown 	 */
77216306a61SNeilBrown 	if (waiter->fl_blocker == NULL &&
77316306a61SNeilBrown 	    list_empty(&waiter->fl_blocked_requests))
774*cb03f94fSNeilBrown 		return status;
7757b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
776*cb03f94fSNeilBrown 	if (waiter->fl_blocker)
777*cb03f94fSNeilBrown 		status = 0;
7785946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
7791da177e4SLinus Torvalds 	__locks_delete_block(waiter);
7807b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
781*cb03f94fSNeilBrown 	return status;
7821da177e4SLinus Torvalds }
783*cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
7861da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
7871da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
7881da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
7891c8c601aSJeff Layton  *
7906109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
791ada5c1daSNeilBrown  * fl_blocked_requests list itself is protected by the blocked_lock_lock,
792ada5c1daSNeilBrown  * but by ensuring that the flc_lock is also held on insertions we can avoid
793ada5c1daSNeilBrown  * taking the blocked_lock_lock in some cases when we see that the
794ada5c1daSNeilBrown  * fl_blocked_requests list is empty.
795fd7732e0SNeilBrown  *
796fd7732e0SNeilBrown  * Rather than just adding to the list, we check for conflicts with any existing
797fd7732e0SNeilBrown  * waiters, and add beneath any waiter that blocks the new waiter.
798fd7732e0SNeilBrown  * Thus wakeups don't happen until needed.
7991da177e4SLinus Torvalds  */
8001c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
801fd7732e0SNeilBrown 				 struct file_lock *waiter,
802fd7732e0SNeilBrown 				 bool conflict(struct file_lock *,
803fd7732e0SNeilBrown 					       struct file_lock *))
8041da177e4SLinus Torvalds {
805fd7732e0SNeilBrown 	struct file_lock *fl;
806ada5c1daSNeilBrown 	BUG_ON(!list_empty(&waiter->fl_blocked_member));
807fd7732e0SNeilBrown 
808fd7732e0SNeilBrown new_blocker:
809fd7732e0SNeilBrown 	list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member)
810fd7732e0SNeilBrown 		if (conflict(fl, waiter)) {
811fd7732e0SNeilBrown 			blocker =  fl;
812fd7732e0SNeilBrown 			goto new_blocker;
813fd7732e0SNeilBrown 		}
814ada5c1daSNeilBrown 	waiter->fl_blocker = blocker;
815ada5c1daSNeilBrown 	list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
816cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
8171c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
8185946c431SNeilBrown 
8195946c431SNeilBrown 	/* The requests in waiter->fl_blocked are known to conflict with
8205946c431SNeilBrown 	 * waiter, but might not conflict with blocker, or the requests
8215946c431SNeilBrown 	 * and lock which block it.  So they all need to be woken.
8225946c431SNeilBrown 	 */
8235946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
8241c8c601aSJeff Layton }
8251c8c601aSJeff Layton 
8266109c850SJeff Layton /* Must be called with flc_lock held. */
8271c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
828fd7732e0SNeilBrown 			       struct file_lock *waiter,
829fd7732e0SNeilBrown 			       bool conflict(struct file_lock *,
830fd7732e0SNeilBrown 					     struct file_lock *))
8311c8c601aSJeff Layton {
8327b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
833fd7732e0SNeilBrown 	__locks_insert_block(blocker, waiter, conflict);
8347b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371cb36012SJeff Layton /*
8381cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
8391cb36012SJeff Layton  *
8406109c850SJeff Layton  * Must be called with the inode->flc_lock held!
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
8431da177e4SLinus Torvalds {
8444e8c765dSJeff Layton 	/*
8454e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
8466109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
847ada5c1daSNeilBrown 	 * the flc_lock is always held here. Note that removal from the
848ada5c1daSNeilBrown 	 * fl_blocked_requests list does not require the flc_lock, so we must
849ada5c1daSNeilBrown 	 * recheck list_empty() after acquiring the blocked_lock_lock.
8504e8c765dSJeff Layton 	 */
851ada5c1daSNeilBrown 	if (list_empty(&blocker->fl_blocked_requests))
8524e8c765dSJeff Layton 		return;
8534e8c765dSJeff Layton 
8547b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
855ad6bbd8bSNeilBrown 	__locks_wake_up_blocks(blocker);
8567b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
8595263e31eSJeff Layton static void
860e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
8615263e31eSJeff Layton {
8625263e31eSJeff Layton 	list_add_tail(&fl->fl_list, before);
8635263e31eSJeff Layton 	locks_insert_global_locks(fl);
8645263e31eSJeff Layton }
8655263e31eSJeff Layton 
8668634b51fSJeff Layton static void
867e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl)
8681da177e4SLinus Torvalds {
86988974691SJeff Layton 	locks_delete_global_locks(fl);
8708634b51fSJeff Layton 	list_del_init(&fl->fl_list);
8711da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
87224cbe784SJeff Layton }
87324cbe784SJeff Layton 
8745263e31eSJeff Layton static void
875e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
8765263e31eSJeff Layton {
877e084c1bdSJeff Layton 	locks_unlink_lock_ctx(fl);
8788634b51fSJeff Layton 	if (dispose)
8798634b51fSJeff Layton 		list_add(&fl->fl_list, dispose);
8808634b51fSJeff Layton 	else
8818634b51fSJeff Layton 		locks_free_lock(fl);
8825263e31eSJeff Layton }
8835263e31eSJeff Layton 
8841da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
8851da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
8861da177e4SLinus Torvalds  */
887c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl,
888c0e15908SNeilBrown 			   struct file_lock *sys_fl)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
891c0e15908SNeilBrown 		return true;
8921da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
893c0e15908SNeilBrown 		return true;
894c0e15908SNeilBrown 	return false;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
8981da177e4SLinus Torvalds  * checking before calling the locks_conflict().
8991da177e4SLinus Torvalds  */
900c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl,
901c0e15908SNeilBrown 				 struct file_lock *sys_fl)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
9041da177e4SLinus Torvalds 	 * each other.
9051da177e4SLinus Torvalds 	 */
9069b8c8695SJeff Layton 	if (posix_same_owner(caller_fl, sys_fl))
907c0e15908SNeilBrown 		return false;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/* Check whether they overlap */
9101da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
911c0e15908SNeilBrown 		return false;
9121da177e4SLinus Torvalds 
913c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
9171da177e4SLinus Torvalds  * checking before calling the locks_conflict().
9181da177e4SLinus Torvalds  */
919c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl,
920c0e15908SNeilBrown 				 struct file_lock *sys_fl)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
9231da177e4SLinus Torvalds 	 * each other.
9241da177e4SLinus Torvalds 	 */
9259b8c8695SJeff Layton 	if (caller_fl->fl_file == sys_fl->fl_file)
926c0e15908SNeilBrown 		return false;
9271da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
928c0e15908SNeilBrown 		return false;
9291da177e4SLinus Torvalds 
930c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9336d34ac19SJ. Bruce Fields void
9349d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	struct file_lock *cfl;
937bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
938c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
9391da177e4SLinus Torvalds 
940128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
941bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
942bd61e0a9SJeff Layton 		fl->fl_type = F_UNLCK;
943bd61e0a9SJeff Layton 		return;
9441da177e4SLinus Torvalds 	}
945bd61e0a9SJeff Layton 
9466109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
947bd61e0a9SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
948bd61e0a9SJeff Layton 		if (posix_locks_conflict(fl, cfl)) {
9493fe0fff1SKinglong Mee 			locks_copy_conflock(fl, cfl);
950bd61e0a9SJeff Layton 			goto out;
951bd61e0a9SJeff Layton 		}
952bd61e0a9SJeff Layton 	}
953129a84deSJ. Bruce Fields 	fl->fl_type = F_UNLCK;
954bd61e0a9SJeff Layton out:
9556109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
9566d34ac19SJ. Bruce Fields 	return;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
9591da177e4SLinus Torvalds 
960b533184fSJ. Bruce Fields /*
961b533184fSJ. Bruce Fields  * Deadlock detection:
9621da177e4SLinus Torvalds  *
963b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
964b533184fSJ. Bruce Fields  * locks.
9651da177e4SLinus Torvalds  *
966b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
967b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
968b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
969b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
970b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
971b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
972b533184fSJ. Bruce Fields  * cycle.
97397855b49SJ. Bruce Fields  *
974b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
975b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
976b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
977b533184fSJ. Bruce Fields  *
978b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
979b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
980b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
981b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
98257b65325SJeff Layton  *
983cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
98457b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
98557b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
98657b65325SJeff Layton  * skip it for those.
98757b65325SJeff Layton  *
988cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
98957b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
99057b65325SJeff Layton  * upgrade from read to write locks on the same inode.
9911da177e4SLinus Torvalds  */
99297855b49SJ. Bruce Fields 
99397855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
99497855b49SJ. Bruce Fields 
995b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
996b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
997b533184fSJ. Bruce Fields {
998b533184fSJ. Bruce Fields 	struct file_lock *fl;
999b533184fSJ. Bruce Fields 
10003999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
10015946c431SNeilBrown 		if (posix_same_owner(fl, block_fl)) {
10025946c431SNeilBrown 			while (fl->fl_blocker)
10035946c431SNeilBrown 				fl = fl->fl_blocker;
10045946c431SNeilBrown 			return fl;
10055946c431SNeilBrown 		}
1006b533184fSJ. Bruce Fields 	}
1007b533184fSJ. Bruce Fields 	return NULL;
1008b533184fSJ. Bruce Fields }
1009b533184fSJ. Bruce Fields 
10107b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
1011b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
10121da177e4SLinus Torvalds 				struct file_lock *block_fl)
10131da177e4SLinus Torvalds {
101497855b49SJ. Bruce Fields 	int i = 0;
10151da177e4SLinus Torvalds 
1016663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
1017663d5af7SDaniel Wagner 
101857b65325SJeff Layton 	/*
101957b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
1020cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
102157b65325SJeff Layton 	 */
1022cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
102357b65325SJeff Layton 		return 0;
102457b65325SJeff Layton 
1025b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
102697855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
102797855b49SJ. Bruce Fields 			return 0;
1028b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
1029b533184fSJ. Bruce Fields 			return 1;
10301da177e4SLinus Torvalds 	}
10311da177e4SLinus Torvalds 	return 0;
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
103502888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
1036f475ae95STrond Myklebust  *
1037f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1038f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1039f475ae95STrond Myklebust  * value for -ENOENT.
10401da177e4SLinus Torvalds  */
1041bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request)
10421da177e4SLinus Torvalds {
1043993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
10445263e31eSJeff Layton 	struct file_lock *fl;
10455263e31eSJeff Layton 	struct file_lock_context *ctx;
10461da177e4SLinus Torvalds 	int error = 0;
10475263e31eSJeff Layton 	bool found = false;
1048ed9814d8SJeff Layton 	LIST_HEAD(dispose);
10491da177e4SLinus Torvalds 
10505c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
10515c1c669aSJeff Layton 	if (!ctx) {
10525c1c669aSJeff Layton 		if (request->fl_type != F_UNLCK)
10535263e31eSJeff Layton 			return -ENOMEM;
10545c1c669aSJeff Layton 		return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
10555c1c669aSJeff Layton 	}
10565263e31eSJeff Layton 
1057b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
1058b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
1059b89f4321SArnd Bergmann 		if (!new_fl)
1060b89f4321SArnd Bergmann 			return -ENOMEM;
1061b89f4321SArnd Bergmann 	}
1062b89f4321SArnd Bergmann 
106387709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
10646109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1065f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1066f07f18ddSTrond Myklebust 		goto find_conflict;
106784d535adSPavel Emelyanov 
10685263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1069bcd7f78dSJeff Layton 		if (request->fl_file != fl->fl_file)
10701da177e4SLinus Torvalds 			continue;
1071993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
10721da177e4SLinus Torvalds 			goto out;
10735263e31eSJeff Layton 		found = true;
1074e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, &dispose);
10751da177e4SLinus Torvalds 		break;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
1078f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
1079f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
1080f475ae95STrond Myklebust 			error = -ENOENT;
1081993dfa87STrond Myklebust 		goto out;
1082f475ae95STrond Myklebust 	}
10831da177e4SLinus Torvalds 
1084f07f18ddSTrond Myklebust find_conflict:
10855263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1086993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
10871da177e4SLinus Torvalds 			continue;
10881da177e4SLinus Torvalds 		error = -EAGAIN;
1089bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
1090bde74e4bSMiklos Szeredi 			goto out;
1091bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
1092fd7732e0SNeilBrown 		locks_insert_block(fl, request, flock_locks_conflict);
10931da177e4SLinus Torvalds 		goto out;
10941da177e4SLinus Torvalds 	}
1095f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1096f07f18ddSTrond Myklebust 		goto out;
1097993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
10985946c431SNeilBrown 	locks_move_blocks(new_fl, request);
1099e084c1bdSJeff Layton 	locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
1100993dfa87STrond Myklebust 	new_fl = NULL;
11019cedc194SKirill Korotaev 	error = 0;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds out:
11046109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
110587709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
1106993dfa87STrond Myklebust 	if (new_fl)
1107993dfa87STrond Myklebust 		locks_free_lock(new_fl);
1108ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
1109c883da31SJeff Layton 	trace_flock_lock_inode(inode, request, error);
11101da177e4SLinus Torvalds 	return error;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
1113b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1114b4d629a3SJeff Layton 			    struct file_lock *conflock)
11151da177e4SLinus Torvalds {
1116bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
111739005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
111839005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
11191da177e4SLinus Torvalds 	struct file_lock *left = NULL;
11201da177e4SLinus Torvalds 	struct file_lock *right = NULL;
1121bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
1122b9746ef8SJeff Layton 	int error;
1123b9746ef8SJeff Layton 	bool added = false;
1124ed9814d8SJeff Layton 	LIST_HEAD(dispose);
11251da177e4SLinus Torvalds 
11265c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
1127bd61e0a9SJeff Layton 	if (!ctx)
11285c1c669aSJeff Layton 		return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
1129bd61e0a9SJeff Layton 
11301da177e4SLinus Torvalds 	/*
11311da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
11321da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
113339005d02SMiklos Szeredi 	 *
113439005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
11351da177e4SLinus Torvalds 	 */
113639005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
113739005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
113839005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
11391da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
11401da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
114139005d02SMiklos Szeredi 	}
11421da177e4SLinus Torvalds 
114387709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
11446109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
11451cb36012SJeff Layton 	/*
11461cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
11471cb36012SJeff Layton 	 * there are any, either return error or put the request on the
114848f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
11491cb36012SJeff Layton 	 */
11501da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
1151bd61e0a9SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
11521da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
11531da177e4SLinus Torvalds 				continue;
11545842add2SAndy Adamson 			if (conflock)
11553fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
11561da177e4SLinus Torvalds 			error = -EAGAIN;
11571da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
11581da177e4SLinus Torvalds 				goto out;
11591c8c601aSJeff Layton 			/*
11601c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
11611c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
11621c8c601aSJeff Layton 			 */
11631da177e4SLinus Torvalds 			error = -EDEADLK;
11647b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
11651c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
1166bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
1167fd7732e0SNeilBrown 				__locks_insert_block(fl, request,
1168fd7732e0SNeilBrown 						     posix_locks_conflict);
11691c8c601aSJeff Layton 			}
11707b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
11711da177e4SLinus Torvalds 			goto out;
11721da177e4SLinus Torvalds   		}
11731da177e4SLinus Torvalds   	}
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
11761da177e4SLinus Torvalds 	error = 0;
11771da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
11781da177e4SLinus Torvalds 		goto out;
11791da177e4SLinus Torvalds 
1180bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
1181bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1182bd61e0a9SJeff Layton 		if (posix_same_owner(request, fl))
1183bd61e0a9SJeff Layton 			break;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/* Process locks with this owner. */
1187bd61e0a9SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1188bd61e0a9SJeff Layton 		if (!posix_same_owner(request, fl))
1189bd61e0a9SJeff Layton 			break;
1190bd61e0a9SJeff Layton 
1191bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
11921da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
1193449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1194449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1195449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1196449231d6SOlaf Kirch 			 */
11971da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1198bd61e0a9SJeff Layton 				continue;
11991da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
12001da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
12011da177e4SLinus Torvalds 			 */
1202449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
12031da177e4SLinus Torvalds 				break;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
12061da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
12071da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
12081da177e4SLinus Torvalds 			 * locks to the higher end address.
12091da177e4SLinus Torvalds 			 */
12101da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
12111da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
12121da177e4SLinus Torvalds 			else
12131da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
12141da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
12151da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
12161da177e4SLinus Torvalds 			else
12171da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
12181da177e4SLinus Torvalds 			if (added) {
1219e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
12201da177e4SLinus Torvalds 				continue;
12211da177e4SLinus Torvalds 			}
12221da177e4SLinus Torvalds 			request = fl;
1223b9746ef8SJeff Layton 			added = true;
1224bd61e0a9SJeff Layton 		} else {
12251da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
12261da177e4SLinus Torvalds 			 * more complex.
12271da177e4SLinus Torvalds 			 */
12281da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1229bd61e0a9SJeff Layton 				continue;
12301da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
12311da177e4SLinus Torvalds 				break;
12321da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1233b9746ef8SJeff Layton 				added = true;
12341da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
12351da177e4SLinus Torvalds 				left = fl;
12361da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
12371da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
12381da177e4SLinus Torvalds 			 */
12391da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
12401da177e4SLinus Torvalds 				right = fl;
12411da177e4SLinus Torvalds 				break;
12421da177e4SLinus Torvalds 			}
12431da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
12441da177e4SLinus Torvalds 				/* The new lock completely replaces an old
12451da177e4SLinus Torvalds 				 * one (This may happen several times).
12461da177e4SLinus Torvalds 				 */
12471da177e4SLinus Torvalds 				if (added) {
1248e084c1bdSJeff Layton 					locks_delete_lock_ctx(fl, &dispose);
12491da177e4SLinus Torvalds 					continue;
12501da177e4SLinus Torvalds 				}
1251b84d49f9SJeff Layton 				/*
1252b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1253b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1254b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1255b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1256b84d49f9SJeff Layton 				 * just replacing an existing lock.
12571da177e4SLinus Torvalds 				 */
1258b84d49f9SJeff Layton 				error = -ENOLCK;
1259b84d49f9SJeff Layton 				if (!new_fl)
1260b84d49f9SJeff Layton 					goto out;
1261b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
1262b84d49f9SJeff Layton 				request = new_fl;
1263b84d49f9SJeff Layton 				new_fl = NULL;
1264e084c1bdSJeff Layton 				locks_insert_lock_ctx(request, &fl->fl_list);
1265e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
1266b9746ef8SJeff Layton 				added = true;
12671da177e4SLinus Torvalds 			}
12681da177e4SLinus Torvalds 		}
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 
12710d9a490aSMiklos Szeredi 	/*
12721cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
12731cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
12741cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
12750d9a490aSMiklos Szeredi 	 */
12760d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
12770d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
12780d9a490aSMiklos Szeredi 		goto out;
12790d9a490aSMiklos Szeredi 
12801da177e4SLinus Torvalds 	error = 0;
12811da177e4SLinus Torvalds 	if (!added) {
1282f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1283f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1284f475ae95STrond Myklebust 				error = -ENOENT;
12851da177e4SLinus Torvalds 			goto out;
1286f475ae95STrond Myklebust 		}
12870d9a490aSMiklos Szeredi 
12880d9a490aSMiklos Szeredi 		if (!new_fl) {
12890d9a490aSMiklos Szeredi 			error = -ENOLCK;
12900d9a490aSMiklos Szeredi 			goto out;
12910d9a490aSMiklos Szeredi 		}
12921da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
12935946c431SNeilBrown 		locks_move_blocks(new_fl, request);
1294e084c1bdSJeff Layton 		locks_insert_lock_ctx(new_fl, &fl->fl_list);
12952e2f756fSJeff Layton 		fl = new_fl;
12961da177e4SLinus Torvalds 		new_fl = NULL;
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 	if (right) {
12991da177e4SLinus Torvalds 		if (left == right) {
13001da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
13011da177e4SLinus Torvalds 			 * so we have to use the second new lock.
13021da177e4SLinus Torvalds 			 */
13031da177e4SLinus Torvalds 			left = new_fl2;
13041da177e4SLinus Torvalds 			new_fl2 = NULL;
13051da177e4SLinus Torvalds 			locks_copy_lock(left, right);
1306e084c1bdSJeff Layton 			locks_insert_lock_ctx(left, &fl->fl_list);
13071da177e4SLinus Torvalds 		}
13081da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
13091da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 	if (left) {
13121da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
13131da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
13141da177e4SLinus Torvalds 	}
13151da177e4SLinus Torvalds  out:
13166109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
131787709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
13181da177e4SLinus Torvalds 	/*
13191da177e4SLinus Torvalds 	 * Free any unused locks.
13201da177e4SLinus Torvalds 	 */
13211da177e4SLinus Torvalds 	if (new_fl)
13221da177e4SLinus Torvalds 		locks_free_lock(new_fl);
13231da177e4SLinus Torvalds 	if (new_fl2)
13241da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1325ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
13261890910fSJeff Layton 	trace_posix_lock_inode(inode, request, error);
13271890910fSJeff Layton 
13281da177e4SLinus Torvalds 	return error;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds /**
13321da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
13331da177e4SLinus Torvalds  * @filp: The file to apply the lock to
13341da177e4SLinus Torvalds  * @fl: The lock to be applied
1335150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
13361da177e4SLinus Torvalds  *
13371da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
13381da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
13391da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1340f475ae95STrond Myklebust  *
1341f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1342f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1343f475ae95STrond Myklebust  * value for -ENOENT.
13441da177e4SLinus Torvalds  */
1345150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
13465842add2SAndy Adamson 			struct file_lock *conflock)
13475842add2SAndy Adamson {
1348c568d683SMiklos Szeredi 	return posix_lock_inode(locks_inode(filp), fl, conflock);
13495842add2SAndy Adamson }
1350150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds /**
135329d01b22SJeff Layton  * posix_lock_inode_wait - Apply a POSIX-style lock to a file
135429d01b22SJeff Layton  * @inode: inode of file to which lock request should be applied
13551da177e4SLinus Torvalds  * @fl: The lock to be applied
13561da177e4SLinus Torvalds  *
1357616fb38fSBenjamin Coddington  * Apply a POSIX style lock request to an inode.
13581da177e4SLinus Torvalds  */
1359616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	int error;
13621da177e4SLinus Torvalds 	might_sleep ();
13631da177e4SLinus Torvalds 	for (;;) {
1364b4d629a3SJeff Layton 		error = posix_lock_inode(inode, fl, NULL);
1365bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
13661da177e4SLinus Torvalds 			break;
1367ada5c1daSNeilBrown 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
136816306a61SNeilBrown 		if (error)
13691da177e4SLinus Torvalds 			break;
13701da177e4SLinus Torvalds 	}
137116306a61SNeilBrown 	locks_delete_block(fl);
13721da177e4SLinus Torvalds 	return error;
13731da177e4SLinus Torvalds }
137429d01b22SJeff Layton 
13759e8925b6SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING
137629d01b22SJeff Layton /**
13771da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1378d7a06983SJeff Layton  * @file: the file to check
13791da177e4SLinus Torvalds  *
13801da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
13811da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
13821da177e4SLinus Torvalds  */
1383d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
13841da177e4SLinus Torvalds {
1385bd61e0a9SJeff Layton 	int ret;
1386c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(file);
1387bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
13881da177e4SLinus Torvalds 	struct file_lock *fl;
13891da177e4SLinus Torvalds 
1390128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
1391bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix))
1392bd61e0a9SJeff Layton 		return 0;
1393bd61e0a9SJeff Layton 
13941da177e4SLinus Torvalds 	/*
13951da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
13961da177e4SLinus Torvalds 	 */
13976109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1398bd61e0a9SJeff Layton 	ret = 0;
1399bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
140073a8f5f7SChristoph Hellwig 		if (fl->fl_owner != current->files &&
1401bd61e0a9SJeff Layton 		    fl->fl_owner != file) {
1402bd61e0a9SJeff Layton 			ret = -EAGAIN;
14031da177e4SLinus Torvalds 			break;
14041da177e4SLinus Torvalds 		}
1405bd61e0a9SJeff Layton 	}
14066109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1407bd61e0a9SJeff Layton 	return ret;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds /**
14111da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
14121da177e4SLinus Torvalds  * @inode:	the file to check
14131da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
1414acc15575SChristoph Hellwig  * @start:	first byte in the file to check
1415acc15575SChristoph Hellwig  * @end:	lastbyte in the file to check
1416acc15575SChristoph Hellwig  * @type:	%F_WRLCK for a write lock, else %F_RDLCK
14171da177e4SLinus Torvalds  *
14181da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
14191da177e4SLinus Torvalds  */
1420acc15575SChristoph Hellwig int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
1421acc15575SChristoph Hellwig 			 loff_t end, unsigned char type)
14221da177e4SLinus Torvalds {
14231da177e4SLinus Torvalds 	struct file_lock fl;
14241da177e4SLinus Torvalds 	int error;
142529723adeSJeff Layton 	bool sleep = false;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	locks_init_lock(&fl);
14281da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
14291da177e4SLinus Torvalds 	fl.fl_file = filp;
14301da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
14311da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
143229723adeSJeff Layton 		sleep = true;
1433acc15575SChristoph Hellwig 	fl.fl_type = type;
1434acc15575SChristoph Hellwig 	fl.fl_start = start;
1435acc15575SChristoph Hellwig 	fl.fl_end = end;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	for (;;) {
143829723adeSJeff Layton 		if (filp) {
143973a8f5f7SChristoph Hellwig 			fl.fl_owner = filp;
144029723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
1441b4d629a3SJeff Layton 			error = posix_lock_inode(inode, &fl, NULL);
144229723adeSJeff Layton 			if (!error)
144329723adeSJeff Layton 				break;
144429723adeSJeff Layton 		}
144529723adeSJeff Layton 
144629723adeSJeff Layton 		if (sleep)
144729723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
144829723adeSJeff Layton 		fl.fl_owner = current->files;
1449b4d629a3SJeff Layton 		error = posix_lock_inode(inode, &fl, NULL);
1450bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
14511da177e4SLinus Torvalds 			break;
1452ada5c1daSNeilBrown 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_blocker);
14531da177e4SLinus Torvalds 		if (!error) {
14541da177e4SLinus Torvalds 			/*
14551da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
14561da177e4SLinus Torvalds 			 * changed the permissions behind our back.
14571da177e4SLinus Torvalds 			 */
1458a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
14591da177e4SLinus Torvalds 				continue;
14601da177e4SLinus Torvalds 		}
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 		break;
14631da177e4SLinus Torvalds 	}
146416306a61SNeilBrown 	locks_delete_block(&fl);
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	return error;
14671da177e4SLinus Torvalds }
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
14709e8925b6SJeff Layton #endif /* CONFIG_MANDATORY_FILE_LOCKING */
14711da177e4SLinus Torvalds 
1472778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1473778fc546SJ. Bruce Fields {
1474778fc546SJ. Bruce Fields 	switch (arg) {
1475778fc546SJ. Bruce Fields 	case F_UNLCK:
1476778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1477778fc546SJ. Bruce Fields 		/* fall through: */
1478778fc546SJ. Bruce Fields 	case F_RDLCK:
1479778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1480778fc546SJ. Bruce Fields 	}
1481778fc546SJ. Bruce Fields }
1482778fc546SJ. Bruce Fields 
14831da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
14847448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	if (error)
14891da177e4SLinus Torvalds 		return error;
1490778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
14911da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
14923b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
14933b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
14943b6e2723SFilipe Brandenburger 
14953b6e2723SFilipe Brandenburger 		f_delown(filp);
14963b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
149796d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
149896d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
149996d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
150096d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
150196d6d59cSJ. Bruce Fields 		}
1502e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, dispose);
15033b6e2723SFilipe Brandenburger 	}
15041da177e4SLinus Torvalds 	return 0;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
15071da177e4SLinus Torvalds 
1508778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1509778fc546SJ. Bruce Fields {
1510778fc546SJ. Bruce Fields 	if (!then)
1511778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1512778fc546SJ. Bruce Fields 		return false;
1513778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1514778fc546SJ. Bruce Fields }
1515778fc546SJ. Bruce Fields 
1516c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
15171da177e4SLinus Torvalds {
15188634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
15198634b51fSJeff Layton 	struct file_lock *fl, *tmp;
15201da177e4SLinus Torvalds 
15216109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1522f82b4b67SJeff Layton 
15238634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
152462af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1525778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
15267448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1527778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
15287448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
1532df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1533df4e8d2cSJ. Bruce Fields {
153411afe9f7SChristoph Hellwig 	if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
153511afe9f7SChristoph Hellwig 		return false;
1536df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1537df4e8d2cSJ. Bruce Fields 		return false;
1538df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1539df4e8d2cSJ. Bruce Fields }
1540df4e8d2cSJ. Bruce Fields 
154103d12ddfSJeff Layton static bool
154203d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker)
154303d12ddfSJeff Layton {
15448634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
154503d12ddfSJeff Layton 	struct file_lock *fl;
154603d12ddfSJeff Layton 
15476109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
154803d12ddfSJeff Layton 
15498634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
155003d12ddfSJeff Layton 		if (leases_conflict(fl, breaker))
155103d12ddfSJeff Layton 			return true;
155203d12ddfSJeff Layton 	}
155303d12ddfSJeff Layton 	return false;
155403d12ddfSJeff Layton }
155503d12ddfSJeff Layton 
15561da177e4SLinus Torvalds /**
15571da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
15581da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1559df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1560df4e8d2cSJ. Bruce Fields  *	    break all leases
1561df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1562df4e8d2cSJ. Bruce Fields  *	    only delegations
15631da177e4SLinus Torvalds  *
156487250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
156587250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
156687250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
15671da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
15681da177e4SLinus Torvalds  */
1569df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
15701da177e4SLinus Torvalds {
1571778fc546SJ. Bruce Fields 	int error = 0;
1572128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1573a901125cSYan, Zheng 	struct file_lock *new_fl, *fl, *tmp;
15741da177e4SLinus Torvalds 	unsigned long break_time;
15758737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1576c45198edSJeff Layton 	LIST_HEAD(dispose);
15771da177e4SLinus Torvalds 
15788737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
15796d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
15806d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1581df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
15821da177e4SLinus Torvalds 
15838634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
1584128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
15858634b51fSJeff Layton 	if (!ctx) {
15868634b51fSJeff Layton 		WARN_ON_ONCE(1);
15878634b51fSJeff Layton 		return error;
15888634b51fSJeff Layton 	}
15898634b51fSJeff Layton 
159087709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
15916109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
15921da177e4SLinus Torvalds 
1593c45198edSJeff Layton 	time_out_leases(inode, &dispose);
15941da177e4SLinus Torvalds 
159503d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1596df4e8d2cSJ. Bruce Fields 		goto out;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	break_time = 0;
15991da177e4SLinus Torvalds 	if (lease_break_time > 0) {
16001da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
16011da177e4SLinus Torvalds 		if (break_time == 0)
16021da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
1605a901125cSYan, Zheng 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1606df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1607df4e8d2cSJ. Bruce Fields 			continue;
1608778fc546SJ. Bruce Fields 		if (want_write) {
1609778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1610778fc546SJ. Bruce Fields 				continue;
1611778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
16121da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1613778fc546SJ. Bruce Fields 		} else {
16148634b51fSJeff Layton 			if (lease_breaking(fl))
1615778fc546SJ. Bruce Fields 				continue;
1616778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1617778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
16181da177e4SLinus Torvalds 		}
16194d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
1620e084c1bdSJeff Layton 			locks_delete_lock_ctx(fl, &dispose);
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds 
16238634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
16244d01b7f5SJeff Layton 		goto out;
16254d01b7f5SJeff Layton 
1626843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
162762af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
16281da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
16291da177e4SLinus Torvalds 		goto out;
16301da177e4SLinus Torvalds 	}
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds restart:
16338634b51fSJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
16348634b51fSJeff Layton 	break_time = fl->fl_break_time;
1635f1c6bb2cSJeff Layton 	if (break_time != 0)
16361da177e4SLinus Torvalds 		break_time -= jiffies;
16371da177e4SLinus Torvalds 	if (break_time == 0)
16381da177e4SLinus Torvalds 		break_time++;
1639fd7732e0SNeilBrown 	locks_insert_block(fl, new_fl, leases_conflict);
164062af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
16416109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
164287709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
1643aba37660SPeter Zijlstra 
1644c45198edSJeff Layton 	locks_dispose_list(&dispose);
16454321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
1646ada5c1daSNeilBrown 						!new_fl->fl_blocker, break_time);
1647aba37660SPeter Zijlstra 
164887709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
16496109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
165062af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
16511c8c601aSJeff Layton 	locks_delete_block(new_fl);
16521da177e4SLinus Torvalds 	if (error >= 0) {
1653778fc546SJ. Bruce Fields 		/*
1654778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1655778fc546SJ. Bruce Fields 		 * broken yet
1656778fc546SJ. Bruce Fields 		 */
165703d12ddfSJeff Layton 		if (error == 0)
165803d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
165903d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
16601da177e4SLinus Torvalds 			goto restart;
16611da177e4SLinus Torvalds 		error = 0;
16621da177e4SLinus Torvalds 	}
16631da177e4SLinus Torvalds out:
16646109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
166587709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
1666c45198edSJeff Layton 	locks_dispose_list(&dispose);
16671da177e4SLinus Torvalds 	locks_free_lock(new_fl);
16681da177e4SLinus Torvalds 	return error;
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds /**
167476c47948SAmir Goldstein  *	lease_get_mtime - update modified time of an inode with exclusive lease
16751da177e4SLinus Torvalds  *	@inode: the inode
167676c47948SAmir Goldstein  *      @time:  pointer to a timespec which contains the last modified time
16771da177e4SLinus Torvalds  *
16781da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
16791da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1680a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
16811da177e4SLinus Torvalds  */
168295582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time)
16831da177e4SLinus Torvalds {
1684bfe86024SJeff Layton 	bool has_lease = false;
1685128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
16868634b51fSJeff Layton 	struct file_lock *fl;
1687bfe86024SJeff Layton 
1688128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
16898634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
16906109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
16918ace5dfbSGeliang Tang 		fl = list_first_entry_or_null(&ctx->flc_lease,
16928634b51fSJeff Layton 					      struct file_lock, fl_list);
16938ace5dfbSGeliang Tang 		if (fl && (fl->fl_type == F_WRLCK))
1694bfe86024SJeff Layton 			has_lease = true;
16956109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1696bfe86024SJeff Layton 	}
1697bfe86024SJeff Layton 
1698bfe86024SJeff Layton 	if (has_lease)
1699c2050a45SDeepa Dinamani 		*time = current_time(inode);
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /**
17051da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
17061da177e4SLinus Torvalds  *	@filp: the file
17071da177e4SLinus Torvalds  *
17081da177e4SLinus Torvalds  *	The value returned by this function will be one of
17091da177e4SLinus Torvalds  *	(if no lease break is pending):
17101da177e4SLinus Torvalds  *
17111da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
17121da177e4SLinus Torvalds  *
17131da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
17141da177e4SLinus Torvalds  *
17151da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
17161da177e4SLinus Torvalds  *
17171da177e4SLinus Torvalds  *	(if a lease break is pending):
17181da177e4SLinus Torvalds  *
17191da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
17201da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
17211da177e4SLinus Torvalds  *
17221da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
17231da177e4SLinus Torvalds  *
17241da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
17251da177e4SLinus Torvalds  *	should be returned to userspace.
17261da177e4SLinus Torvalds  */
17271da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
17281da177e4SLinus Torvalds {
17291da177e4SLinus Torvalds 	struct file_lock *fl;
1730c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
1731128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
17321da177e4SLinus Torvalds 	int type = F_UNLCK;
1733c45198edSJeff Layton 	LIST_HEAD(dispose);
17341da177e4SLinus Torvalds 
1735128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
17368634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
17375f43086bSPeter Zijlstra 		percpu_down_read_preempt_disable(&file_rwsem);
17386109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1739c568d683SMiklos Szeredi 		time_out_leases(inode, &dispose);
17408634b51fSJeff Layton 		list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
17418634b51fSJeff Layton 			if (fl->fl_file != filp)
17428634b51fSJeff Layton 				continue;
1743778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
17441da177e4SLinus Torvalds 			break;
17451da177e4SLinus Torvalds 		}
17466109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
17475f43086bSPeter Zijlstra 		percpu_up_read_preempt_enable(&file_rwsem);
17485f43086bSPeter Zijlstra 
1749c45198edSJeff Layton 		locks_dispose_list(&dispose);
17508634b51fSJeff Layton 	}
17511da177e4SLinus Torvalds 	return type;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
175424cbe784SJeff Layton /**
175524cbe784SJeff Layton  * check_conflicting_open - see if the given dentry points to a file that has
175624cbe784SJeff Layton  * 			    an existing open that would conflict with the
175724cbe784SJeff Layton  * 			    desired lease.
175824cbe784SJeff Layton  * @dentry:	dentry to check
175924cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
17607fadc59cSRandy Dunlap  * @flags:	current lock flags
176124cbe784SJeff Layton  *
176224cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
176324cbe784SJeff Layton  * conflict with the lease we're trying to set.
176424cbe784SJeff Layton  */
176524cbe784SJeff Layton static int
176611afe9f7SChristoph Hellwig check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
176724cbe784SJeff Layton {
176824cbe784SJeff Layton 	int ret = 0;
176924cbe784SJeff Layton 	struct inode *inode = dentry->d_inode;
177024cbe784SJeff Layton 
177111afe9f7SChristoph Hellwig 	if (flags & FL_LAYOUT)
177211afe9f7SChristoph Hellwig 		return 0;
177311afe9f7SChristoph Hellwig 
17748cf9ee50SMiklos Szeredi 	if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
177524cbe784SJeff Layton 		return -EAGAIN;
177624cbe784SJeff Layton 
177724cbe784SJeff Layton 	if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
177824cbe784SJeff Layton 	    (atomic_read(&inode->i_count) > 1)))
177924cbe784SJeff Layton 		ret = -EAGAIN;
178024cbe784SJeff Layton 
178124cbe784SJeff Layton 	return ret;
178224cbe784SJeff Layton }
178324cbe784SJeff Layton 
1784e6f5c789SJeff Layton static int
1785e6f5c789SJeff Layton generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
17861da177e4SLinus Torvalds {
17878634b51fSJeff Layton 	struct file_lock *fl, *my_fl = NULL, *lease;
17880f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
1789c568d683SMiklos Szeredi 	struct inode *inode = dentry->d_inode;
17908634b51fSJeff Layton 	struct file_lock_context *ctx;
1791df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1792c1f24ef4SJ. Bruce Fields 	int error;
1793c45198edSJeff Layton 	LIST_HEAD(dispose);
17941da177e4SLinus Torvalds 
1795096657b6SJ. Bruce Fields 	lease = *flp;
179662af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
179762af4f1fSJeff Layton 
17985c1c669aSJeff Layton 	/* Note that arg is never F_UNLCK here */
17995c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, arg);
18008634b51fSJeff Layton 	if (!ctx)
18018634b51fSJeff Layton 		return -ENOMEM;
18028634b51fSJeff Layton 
1803df4e8d2cSJ. Bruce Fields 	/*
1804df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1805df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1806df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1807df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1808df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1809df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1810df4e8d2cSJ. Bruce Fields 	 */
18115955102cSAl Viro 	if (is_deleg && !inode_trylock(inode))
1812df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1813df4e8d2cSJ. Bruce Fields 
1814df4e8d2cSJ. Bruce Fields 	if (is_deleg && arg == F_WRLCK) {
1815df4e8d2cSJ. Bruce Fields 		/* Write delegations are not currently supported: */
18165955102cSAl Viro 		inode_unlock(inode);
1817df4e8d2cSJ. Bruce Fields 		WARN_ON_ONCE(1);
1818df4e8d2cSJ. Bruce Fields 		return -EINVAL;
1819df4e8d2cSJ. Bruce Fields 	}
1820096657b6SJ. Bruce Fields 
182187709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
18226109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1823c45198edSJeff Layton 	time_out_leases(inode, &dispose);
182411afe9f7SChristoph Hellwig 	error = check_conflicting_open(dentry, arg, lease->fl_flags);
182524cbe784SJeff Layton 	if (error)
18261da177e4SLinus Torvalds 		goto out;
182785c59580SPavel Emelyanov 
18281da177e4SLinus Torvalds 	/*
18291da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
18301da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
18311da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
18321da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
18331da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
18341da177e4SLinus Torvalds 	 * except for this filp.
18351da177e4SLinus Torvalds 	 */
1836c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
18378634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
18382ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
18392ab99ee1SChristoph Hellwig 		    fl->fl_owner == lease->fl_owner) {
18408634b51fSJeff Layton 			my_fl = fl;
1841c1f24ef4SJ. Bruce Fields 			continue;
18421da177e4SLinus Torvalds 		}
18438634b51fSJeff Layton 
1844c1f24ef4SJ. Bruce Fields 		/*
1845c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1846c1f24ef4SJ. Bruce Fields 		 * this file:
1847c1f24ef4SJ. Bruce Fields 		 */
1848c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
18491da177e4SLinus Torvalds 			goto out;
1850c1f24ef4SJ. Bruce Fields 		/*
1851c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1852c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1853c1f24ef4SJ. Bruce Fields 		 */
1854c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1855c1f24ef4SJ. Bruce Fields 			goto out;
1856c1f24ef4SJ. Bruce Fields 	}
18571da177e4SLinus Torvalds 
18588634b51fSJeff Layton 	if (my_fl != NULL) {
18590164bf02SJeff Layton 		lease = my_fl;
18600164bf02SJeff Layton 		error = lease->fl_lmops->lm_change(lease, arg, &dispose);
18611c7dd2ffSJeff Layton 		if (error)
18621da177e4SLinus Torvalds 			goto out;
18631c7dd2ffSJeff Layton 		goto out_setup;
18641da177e4SLinus Torvalds 	}
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	error = -EINVAL;
18671da177e4SLinus Torvalds 	if (!leases_enable)
18681da177e4SLinus Torvalds 		goto out;
18691da177e4SLinus Torvalds 
1870e084c1bdSJeff Layton 	locks_insert_lock_ctx(lease, &ctx->flc_lease);
187124cbe784SJeff Layton 	/*
187224cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
187324cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
187424cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
187524cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
187624cbe784SJeff Layton 	 *
187724cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
187824cbe784SJeff Layton 	 * precedes these checks.
187924cbe784SJeff Layton 	 */
188024cbe784SJeff Layton 	smp_mb();
188111afe9f7SChristoph Hellwig 	error = check_conflicting_open(dentry, arg, lease->fl_flags);
18828634b51fSJeff Layton 	if (error) {
1883e084c1bdSJeff Layton 		locks_unlink_lock_ctx(lease);
18848634b51fSJeff Layton 		goto out;
18858634b51fSJeff Layton 	}
18861c7dd2ffSJeff Layton 
18871c7dd2ffSJeff Layton out_setup:
18881c7dd2ffSJeff Layton 	if (lease->fl_lmops->lm_setup)
18891c7dd2ffSJeff Layton 		lease->fl_lmops->lm_setup(lease, priv);
18901da177e4SLinus Torvalds out:
18916109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
189287709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
1893c45198edSJeff Layton 	locks_dispose_list(&dispose);
1894df4e8d2cSJ. Bruce Fields 	if (is_deleg)
18955955102cSAl Viro 		inode_unlock(inode);
18968634b51fSJeff Layton 	if (!error && !my_fl)
18971c7dd2ffSJeff Layton 		*flp = NULL;
18981da177e4SLinus Torvalds 	return error;
18991da177e4SLinus Torvalds }
19008335ebd9SJ. Bruce Fields 
19012ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner)
19028335ebd9SJ. Bruce Fields {
19030efaa7e8SJeff Layton 	int error = -EAGAIN;
19048634b51fSJeff Layton 	struct file_lock *fl, *victim = NULL;
1905c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
1906128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1907c45198edSJeff Layton 	LIST_HEAD(dispose);
19088335ebd9SJ. Bruce Fields 
1909128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
19108634b51fSJeff Layton 	if (!ctx) {
19118634b51fSJeff Layton 		trace_generic_delete_lease(inode, NULL);
19128634b51fSJeff Layton 		return error;
19138634b51fSJeff Layton 	}
19148634b51fSJeff Layton 
191587709e28SPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
19166109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
19178634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
19182ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
19192ab99ee1SChristoph Hellwig 		    fl->fl_owner == owner) {
19208634b51fSJeff Layton 			victim = fl;
19210efaa7e8SJeff Layton 			break;
19228335ebd9SJ. Bruce Fields 		}
19238634b51fSJeff Layton 	}
1924a9b1b455SJeff Layton 	trace_generic_delete_lease(inode, victim);
19258634b51fSJeff Layton 	if (victim)
19267448cc37SJeff Layton 		error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
19276109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
192887709e28SPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
1929c45198edSJeff Layton 	locks_dispose_list(&dispose);
19300efaa7e8SJeff Layton 	return error;
19318335ebd9SJ. Bruce Fields }
19328335ebd9SJ. Bruce Fields 
19331da177e4SLinus Torvalds /**
19341da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
19351da177e4SLinus Torvalds  *	@filp:	file pointer
19361da177e4SLinus Torvalds  *	@arg:	type of lease to obtain
19371da177e4SLinus Torvalds  *	@flp:	input - file_lock to use, output - file_lock inserted
19381c7dd2ffSJeff Layton  *	@priv:	private data for lm_setup (may be NULL if lm_setup
19391c7dd2ffSJeff Layton  *		doesn't require it)
19401da177e4SLinus Torvalds  *
19411da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
19421da177e4SLinus Torvalds  *	by break_lease().
19431da177e4SLinus Torvalds  */
1944e6f5c789SJeff Layton int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1945e6f5c789SJeff Layton 			void **priv)
19461da177e4SLinus Torvalds {
1947c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
19488335ebd9SJ. Bruce Fields 	int error;
19491da177e4SLinus Torvalds 
19508e96e3b7SEric W. Biederman 	if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
19518335ebd9SJ. Bruce Fields 		return -EACCES;
19521da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
19538335ebd9SJ. Bruce Fields 		return -EINVAL;
19541da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
19551da177e4SLinus Torvalds 	if (error)
19568335ebd9SJ. Bruce Fields 		return error;
19571da177e4SLinus Torvalds 
19588335ebd9SJ. Bruce Fields 	switch (arg) {
19598335ebd9SJ. Bruce Fields 	case F_UNLCK:
19602ab99ee1SChristoph Hellwig 		return generic_delete_lease(filp, *priv);
19618335ebd9SJ. Bruce Fields 	case F_RDLCK:
19628335ebd9SJ. Bruce Fields 	case F_WRLCK:
19630efaa7e8SJeff Layton 		if (!(*flp)->fl_lmops->lm_break) {
19640efaa7e8SJeff Layton 			WARN_ON_ONCE(1);
19650efaa7e8SJeff Layton 			return -ENOLCK;
19660efaa7e8SJeff Layton 		}
196711afe9f7SChristoph Hellwig 
1968e6f5c789SJeff Layton 		return generic_add_lease(filp, arg, flp, priv);
19698335ebd9SJ. Bruce Fields 	default:
19708d657eb3SDave Jones 		return -EINVAL;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds }
19730af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds /**
1976a9933ceaSJ. Bruce Fields  * vfs_setlease        -       sets a lease on an open file
19771da177e4SLinus Torvalds  * @filp:	file pointer
19781da177e4SLinus Torvalds  * @arg:	type of lease to obtain
1979e51673aaSJeff Layton  * @lease:	file_lock to use when adding a lease
19801c7dd2ffSJeff Layton  * @priv:	private info for lm_setup when adding a lease (may be
19811c7dd2ffSJeff Layton  * 		NULL if lm_setup doesn't require it)
19821da177e4SLinus Torvalds  *
1983e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
1984e51673aaSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
198580b79dd0SMauro Carvalho Chehab  * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
198680b79dd0SMauro Carvalho Chehab  * set; if not, this function will return -ENOLCK (and generate a scary-looking
1987e51673aaSJeff Layton  * stack trace).
19881c7dd2ffSJeff Layton  *
19891c7dd2ffSJeff Layton  * The "priv" pointer is passed directly to the lm_setup function as-is. It
19901c7dd2ffSJeff Layton  * may be NULL if the lm_setup operation doesn't require it.
19911da177e4SLinus Torvalds  */
1992e6f5c789SJeff Layton int
1993e6f5c789SJeff Layton vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
19941da177e4SLinus Torvalds {
1995de2a4a50SMiklos Szeredi 	if (filp->f_op->setlease)
1996f82b4b67SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
19971c7dd2ffSJeff Layton 	else
1998f82b4b67SJeff Layton 		return generic_setlease(filp, arg, lease, priv);
19991da177e4SLinus Torvalds }
2000a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
20011da177e4SLinus Torvalds 
20020ceaf6c7SJ. Bruce Fields static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
20031da177e4SLinus Torvalds {
20041c7dd2ffSJeff Layton 	struct file_lock *fl;
2005f7347ce4SLinus Torvalds 	struct fasync_struct *new;
20061da177e4SLinus Torvalds 	int error;
20071da177e4SLinus Torvalds 
2008c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
2009c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
2010c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
20111da177e4SLinus Torvalds 
2012f7347ce4SLinus Torvalds 	new = fasync_alloc();
2013f7347ce4SLinus Torvalds 	if (!new) {
2014f7347ce4SLinus Torvalds 		locks_free_lock(fl);
2015f7347ce4SLinus Torvalds 		return -ENOMEM;
2016f7347ce4SLinus Torvalds 	}
20171c7dd2ffSJeff Layton 	new->fa_fd = fd;
20181da177e4SLinus Torvalds 
20191c7dd2ffSJeff Layton 	error = vfs_setlease(filp, arg, &fl, (void **)&new);
20202dfb928fSJeff Layton 	if (fl)
20212dfb928fSJeff Layton 		locks_free_lock(fl);
2022f7347ce4SLinus Torvalds 	if (new)
2023f7347ce4SLinus Torvalds 		fasync_free(new);
20241da177e4SLinus Torvalds 	return error;
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds /**
20280ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
20290ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
20300ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
20310ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
20320ceaf6c7SJ. Bruce Fields  *
20330ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
20340ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
20350ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
20360ceaf6c7SJ. Bruce Fields  */
20370ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
20380ceaf6c7SJ. Bruce Fields {
20390ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
20402ab99ee1SChristoph Hellwig 		return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
20410ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
20420ceaf6c7SJ. Bruce Fields }
20430ceaf6c7SJ. Bruce Fields 
20440ceaf6c7SJ. Bruce Fields /**
204529d01b22SJeff Layton  * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
204629d01b22SJeff Layton  * @inode: inode of the file to apply to
20471da177e4SLinus Torvalds  * @fl: The lock to be applied
20481da177e4SLinus Torvalds  *
204929d01b22SJeff Layton  * Apply a FLOCK style lock request to an inode.
20501da177e4SLinus Torvalds  */
2051616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	int error;
20541da177e4SLinus Torvalds 	might_sleep();
20551da177e4SLinus Torvalds 	for (;;) {
205629d01b22SJeff Layton 		error = flock_lock_inode(inode, fl);
2057bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
20581da177e4SLinus Torvalds 			break;
2059ada5c1daSNeilBrown 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
206016306a61SNeilBrown 		if (error)
20611da177e4SLinus Torvalds 			break;
20621da177e4SLinus Torvalds 	}
206316306a61SNeilBrown 	locks_delete_block(fl);
20641da177e4SLinus Torvalds 	return error;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
206729d01b22SJeff Layton /**
2068e55c34a6SBenjamin Coddington  * locks_lock_inode_wait - Apply a lock to an inode
2069e55c34a6SBenjamin Coddington  * @inode: inode of the file to apply to
2070e55c34a6SBenjamin Coddington  * @fl: The lock to be applied
2071e55c34a6SBenjamin Coddington  *
2072e55c34a6SBenjamin Coddington  * Apply a POSIX or FLOCK style lock request to an inode.
2073e55c34a6SBenjamin Coddington  */
2074e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
2075e55c34a6SBenjamin Coddington {
2076e55c34a6SBenjamin Coddington 	int res = 0;
2077e55c34a6SBenjamin Coddington 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2078e55c34a6SBenjamin Coddington 		case FL_POSIX:
2079e55c34a6SBenjamin Coddington 			res = posix_lock_inode_wait(inode, fl);
2080e55c34a6SBenjamin Coddington 			break;
2081e55c34a6SBenjamin Coddington 		case FL_FLOCK:
2082e55c34a6SBenjamin Coddington 			res = flock_lock_inode_wait(inode, fl);
2083e55c34a6SBenjamin Coddington 			break;
2084e55c34a6SBenjamin Coddington 		default:
2085e55c34a6SBenjamin Coddington 			BUG();
2086e55c34a6SBenjamin Coddington 	}
2087e55c34a6SBenjamin Coddington 	return res;
2088e55c34a6SBenjamin Coddington }
2089e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait);
2090e55c34a6SBenjamin Coddington 
2091e55c34a6SBenjamin Coddington /**
20921da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
20931da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
20941da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
20951da177e4SLinus Torvalds  *
20961da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
209780b79dd0SMauro Carvalho Chehab  *	The @cmd can be one of:
20981da177e4SLinus Torvalds  *
209980b79dd0SMauro Carvalho Chehab  *	- %LOCK_SH -- a shared lock.
210080b79dd0SMauro Carvalho Chehab  *	- %LOCK_EX -- an exclusive lock.
210180b79dd0SMauro Carvalho Chehab  *	- %LOCK_UN -- remove an existing lock.
210280b79dd0SMauro Carvalho Chehab  *	- %LOCK_MAND -- a 'mandatory' flock.
210380b79dd0SMauro Carvalho Chehab  *	  This exists to emulate Windows Share Modes.
21041da177e4SLinus Torvalds  *
21051da177e4SLinus Torvalds  *	%LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
21061da177e4SLinus Torvalds  *	processes read and write access respectively.
21071da177e4SLinus Torvalds  */
2108002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21091da177e4SLinus Torvalds {
21102903ff01SAl Viro 	struct fd f = fdget(fd);
21111da177e4SLinus Torvalds 	struct file_lock *lock;
21121da177e4SLinus Torvalds 	int can_sleep, unlock;
21131da177e4SLinus Torvalds 	int error;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	error = -EBADF;
21162903ff01SAl Viro 	if (!f.file)
21171da177e4SLinus Torvalds 		goto out;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	can_sleep = !(cmd & LOCK_NB);
21201da177e4SLinus Torvalds 	cmd &= ~LOCK_NB;
21211da177e4SLinus Torvalds 	unlock = (cmd == LOCK_UN);
21221da177e4SLinus Torvalds 
2123aeb5d727SAl Viro 	if (!unlock && !(cmd & LOCK_MAND) &&
21242903ff01SAl Viro 	    !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
21251da177e4SLinus Torvalds 		goto out_putf;
21261da177e4SLinus Torvalds 
2127d6367d62SNeilBrown 	lock = flock_make_lock(f.file, cmd, NULL);
21286e129d00SJeff Layton 	if (IS_ERR(lock)) {
21296e129d00SJeff Layton 		error = PTR_ERR(lock);
21301da177e4SLinus Torvalds 		goto out_putf;
21316e129d00SJeff Layton 	}
21326e129d00SJeff Layton 
21331da177e4SLinus Torvalds 	if (can_sleep)
21341da177e4SLinus Torvalds 		lock->fl_flags |= FL_SLEEP;
21351da177e4SLinus Torvalds 
21362903ff01SAl Viro 	error = security_file_lock(f.file, lock->fl_type);
21371da177e4SLinus Torvalds 	if (error)
21381da177e4SLinus Torvalds 		goto out_free;
21391da177e4SLinus Torvalds 
2140de2a4a50SMiklos Szeredi 	if (f.file->f_op->flock)
21412903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
21421da177e4SLinus Torvalds 					  (can_sleep) ? F_SETLKW : F_SETLK,
21431da177e4SLinus Torvalds 					  lock);
21441da177e4SLinus Torvalds 	else
21454f656367SBenjamin Coddington 		error = locks_lock_file_wait(f.file, lock);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds  out_free:
21481da177e4SLinus Torvalds 	locks_free_lock(lock);
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds  out_putf:
21512903ff01SAl Viro 	fdput(f);
21521da177e4SLinus Torvalds  out:
21531da177e4SLinus Torvalds 	return error;
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
21563ee17abdSJ. Bruce Fields /**
21573ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
21583ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
21596924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
21603ee17abdSJ. Bruce Fields  *
21613ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
21623ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
21633ee17abdSJ. Bruce Fields  */
21643ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
21653ee17abdSJ. Bruce Fields {
2166de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
21673ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
21683ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
21693ee17abdSJ. Bruce Fields 	return 0;
21703ee17abdSJ. Bruce Fields }
21713ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
21723ee17abdSJ. Bruce Fields 
21739d5b86acSBenjamin Coddington /**
21749d5b86acSBenjamin Coddington  * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
21759d5b86acSBenjamin Coddington  * @fl: The file_lock who's fl_pid should be translated
21769d5b86acSBenjamin Coddington  * @ns: The namespace into which the pid should be translated
21779d5b86acSBenjamin Coddington  *
21789d5b86acSBenjamin Coddington  * Used to tranlate a fl_pid into a namespace virtual pid number
21799d5b86acSBenjamin Coddington  */
21809d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns)
21819d5b86acSBenjamin Coddington {
21829d5b86acSBenjamin Coddington 	pid_t vnr;
21839d5b86acSBenjamin Coddington 	struct pid *pid;
21849d5b86acSBenjamin Coddington 
21859d5b86acSBenjamin Coddington 	if (IS_OFDLCK(fl))
21869d5b86acSBenjamin Coddington 		return -1;
21879d5b86acSBenjamin Coddington 	if (IS_REMOTELCK(fl))
21889d5b86acSBenjamin Coddington 		return fl->fl_pid;
2189826d7bc9SKonstantin Khorenko 	/*
2190826d7bc9SKonstantin Khorenko 	 * If the flock owner process is dead and its pid has been already
2191826d7bc9SKonstantin Khorenko 	 * freed, the translation below won't work, but we still want to show
2192826d7bc9SKonstantin Khorenko 	 * flock owner pid number in init pidns.
2193826d7bc9SKonstantin Khorenko 	 */
2194826d7bc9SKonstantin Khorenko 	if (ns == &init_pid_ns)
2195826d7bc9SKonstantin Khorenko 		return (pid_t)fl->fl_pid;
21969d5b86acSBenjamin Coddington 
21979d5b86acSBenjamin Coddington 	rcu_read_lock();
21989d5b86acSBenjamin Coddington 	pid = find_pid_ns(fl->fl_pid, &init_pid_ns);
21999d5b86acSBenjamin Coddington 	vnr = pid_nr_ns(pid, ns);
22009d5b86acSBenjamin Coddington 	rcu_read_unlock();
22019d5b86acSBenjamin Coddington 	return vnr;
22029d5b86acSBenjamin Coddington }
22039d5b86acSBenjamin Coddington 
2204c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2205c2fa1b8aSJ. Bruce Fields {
22069d5b86acSBenjamin Coddington 	flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2207c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
2208c2fa1b8aSJ. Bruce Fields 	/*
2209c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
2210c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
2211c2fa1b8aSJ. Bruce Fields 	 */
2212c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
2213c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2214c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2215c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2216c2fa1b8aSJ. Bruce Fields #endif
2217c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
2218c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2219c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2220c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2221129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2222c2fa1b8aSJ. Bruce Fields 	return 0;
2223c2fa1b8aSJ. Bruce Fields }
2224c2fa1b8aSJ. Bruce Fields 
2225c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
2226c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2227c2fa1b8aSJ. Bruce Fields {
22289d5b86acSBenjamin Coddington 	flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2229c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
2230c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2231c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2232c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2233c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2234c2fa1b8aSJ. Bruce Fields }
2235c2fa1b8aSJ. Bruce Fields #endif
2236c2fa1b8aSJ. Bruce Fields 
22371da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
22381da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
22391da177e4SLinus Torvalds  */
2240a75d30c7SChristoph Hellwig int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
22411da177e4SLinus Torvalds {
224252306e88SBenjamin Coddington 	struct file_lock *fl;
22431da177e4SLinus Torvalds 	int error;
22441da177e4SLinus Torvalds 
224552306e88SBenjamin Coddington 	fl = locks_alloc_lock();
224652306e88SBenjamin Coddington 	if (fl == NULL)
224752306e88SBenjamin Coddington 		return -ENOMEM;
22481da177e4SLinus Torvalds 	error = -EINVAL;
2249a75d30c7SChristoph Hellwig 	if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
22501da177e4SLinus Torvalds 		goto out;
22511da177e4SLinus Torvalds 
225252306e88SBenjamin Coddington 	error = flock_to_posix_lock(filp, fl, flock);
22531da177e4SLinus Torvalds 	if (error)
22541da177e4SLinus Torvalds 		goto out;
22551da177e4SLinus Torvalds 
22560d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
225790478939SJeff Layton 		error = -EINVAL;
2258a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
225990478939SJeff Layton 			goto out;
226090478939SJeff Layton 
22615d50ffd7SJeff Layton 		cmd = F_GETLK;
226252306e88SBenjamin Coddington 		fl->fl_flags |= FL_OFDLCK;
226352306e88SBenjamin Coddington 		fl->fl_owner = filp;
22645d50ffd7SJeff Layton 	}
22655d50ffd7SJeff Layton 
226652306e88SBenjamin Coddington 	error = vfs_test_lock(filp, fl);
22673ee17abdSJ. Bruce Fields 	if (error)
22681da177e4SLinus Torvalds 		goto out;
22691da177e4SLinus Torvalds 
227052306e88SBenjamin Coddington 	flock->l_type = fl->fl_type;
227152306e88SBenjamin Coddington 	if (fl->fl_type != F_UNLCK) {
227252306e88SBenjamin Coddington 		error = posix_lock_to_flock(flock, fl);
2273c2fa1b8aSJ. Bruce Fields 		if (error)
227452306e88SBenjamin Coddington 			goto out;
22751da177e4SLinus Torvalds 	}
22761da177e4SLinus Torvalds out:
227752306e88SBenjamin Coddington 	locks_free_lock(fl);
22781da177e4SLinus Torvalds 	return error;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
22817723ec97SMarc Eshel /**
22827723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
22837723ec97SMarc Eshel  * @filp: The file to apply the lock to
22847723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
22857723ec97SMarc Eshel  * @fl: The lock to be applied
2286150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2287150b3934SMarc Eshel  *
2288150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2289150b3934SMarc Eshel  * as the final argument.
2290150b3934SMarc Eshel  *
2291150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
2292150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
2293150b3934SMarc Eshel  * some acceptable default.
22942beb6614SMarc Eshel  *
22952beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
22962beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
22972beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
22988fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
22992beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
23002beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
23018fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
23022beb6614SMarc Eshel  * request completes.
23032beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
2304bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2305bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
23062beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
23072beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
23082beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
23092beb6614SMarc Eshel  * the correct lock cleanup when required.
23102beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
23118fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
23122beb6614SMarc Eshel  * return code.
23137723ec97SMarc Eshel  */
2314150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
23157723ec97SMarc Eshel {
2316de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
23177723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
23187723ec97SMarc Eshel 	else
2319150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
23207723ec97SMarc Eshel }
23217723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
23227723ec97SMarc Eshel 
2323b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2324b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2325b648a6deSMiklos Szeredi {
2326b648a6deSMiklos Szeredi 	int error;
2327b648a6deSMiklos Szeredi 
2328b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2329b648a6deSMiklos Szeredi 	if (error)
2330b648a6deSMiklos Szeredi 		return error;
2331b648a6deSMiklos Szeredi 
2332b648a6deSMiklos Szeredi 	for (;;) {
2333764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2334b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2335b648a6deSMiklos Szeredi 			break;
2336ada5c1daSNeilBrown 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
233716306a61SNeilBrown 		if (error)
2338b648a6deSMiklos Szeredi 			break;
2339b648a6deSMiklos Szeredi 	}
234016306a61SNeilBrown 	locks_delete_block(fl);
2341b648a6deSMiklos Szeredi 
2342b648a6deSMiklos Szeredi 	return error;
2343b648a6deSMiklos Szeredi }
2344b648a6deSMiklos Szeredi 
23456ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2346cf01f4eeSJeff Layton static int
2347cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2348cf01f4eeSJeff Layton {
2349cf01f4eeSJeff Layton 	switch (fl->fl_type) {
2350cf01f4eeSJeff Layton 	case F_RDLCK:
2351cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_READ))
2352cf01f4eeSJeff Layton 			return -EBADF;
2353cf01f4eeSJeff Layton 		break;
2354cf01f4eeSJeff Layton 	case F_WRLCK:
2355cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_WRITE))
2356cf01f4eeSJeff Layton 			return -EBADF;
2357cf01f4eeSJeff Layton 	}
2358cf01f4eeSJeff Layton 	return 0;
2359cf01f4eeSJeff Layton }
2360cf01f4eeSJeff Layton 
23611da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
23621da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
23631da177e4SLinus Torvalds  */
2364c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2365a75d30c7SChristoph Hellwig 		struct flock *flock)
23661da177e4SLinus Torvalds {
23671da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
2368a75d30c7SChristoph Hellwig 	struct inode *inode = locks_inode(filp);
23690b2bac2fSAl Viro 	struct file *f;
23701da177e4SLinus Torvalds 	int error;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	if (file_lock == NULL)
23731da177e4SLinus Torvalds 		return -ENOLCK;
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
23761da177e4SLinus Torvalds 	 * and shared.
23771da177e4SLinus Torvalds 	 */
2378a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
23791da177e4SLinus Torvalds 		error = -EAGAIN;
23801da177e4SLinus Torvalds 		goto out;
23811da177e4SLinus Torvalds 	}
23821da177e4SLinus Torvalds 
2383a75d30c7SChristoph Hellwig 	error = flock_to_posix_lock(filp, file_lock, flock);
23841da177e4SLinus Torvalds 	if (error)
23851da177e4SLinus Torvalds 		goto out;
23865d50ffd7SJeff Layton 
2387cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2388cf01f4eeSJeff Layton 	if (error)
2389cf01f4eeSJeff Layton 		goto out;
2390cf01f4eeSJeff Layton 
23915d50ffd7SJeff Layton 	/*
23925d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2393cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
23945d50ffd7SJeff Layton 	 */
23955d50ffd7SJeff Layton 	switch (cmd) {
23960d3f7a2dSJeff Layton 	case F_OFD_SETLK:
239790478939SJeff Layton 		error = -EINVAL;
2398a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
239990478939SJeff Layton 			goto out;
240090478939SJeff Layton 
24015d50ffd7SJeff Layton 		cmd = F_SETLK;
2402cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
240373a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
24045d50ffd7SJeff Layton 		break;
24050d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
240690478939SJeff Layton 		error = -EINVAL;
2407a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
240890478939SJeff Layton 			goto out;
240990478939SJeff Layton 
24105d50ffd7SJeff Layton 		cmd = F_SETLKW;
2411cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
241273a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
24135d50ffd7SJeff Layton 		/* Fallthrough */
24145d50ffd7SJeff Layton 	case F_SETLKW:
24151da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
24161da177e4SLinus Torvalds 	}
24171da177e4SLinus Torvalds 
2418b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2419c293621bSPeter Staubach 
2420c293621bSPeter Staubach 	/*
24210752ba80SJeff Layton 	 * Attempt to detect a close/fcntl race and recover by releasing the
24220752ba80SJeff Layton 	 * lock that was just acquired. There is no need to do that when we're
24230752ba80SJeff Layton 	 * unlocking though, or for OFD locks.
2424c293621bSPeter Staubach 	 */
24250752ba80SJeff Layton 	if (!error && file_lock->fl_type != F_UNLCK &&
24260752ba80SJeff Layton 	    !(file_lock->fl_flags & FL_OFDLCK)) {
24270b2bac2fSAl Viro 		/*
24287f3697e2SJeff Layton 		 * We need that spin_lock here - it prevents reordering between
24297f3697e2SJeff Layton 		 * update of i_flctx->flc_posix and check for it done in
24307f3697e2SJeff Layton 		 * close(). rcu_read_lock() wouldn't do.
24310b2bac2fSAl Viro 		 */
24320b2bac2fSAl Viro 		spin_lock(&current->files->file_lock);
24330b2bac2fSAl Viro 		f = fcheck(fd);
24340b2bac2fSAl Viro 		spin_unlock(&current->files->file_lock);
24357f3697e2SJeff Layton 		if (f != filp) {
24367f3697e2SJeff Layton 			file_lock->fl_type = F_UNLCK;
24377f3697e2SJeff Layton 			error = do_lock_file_wait(filp, cmd, file_lock);
24387f3697e2SJeff Layton 			WARN_ON_ONCE(error);
24397f3697e2SJeff Layton 			error = -EBADF;
2440c293621bSPeter Staubach 		}
24417f3697e2SJeff Layton 	}
24421da177e4SLinus Torvalds out:
24431890910fSJeff Layton 	trace_fcntl_setlk(inode, file_lock, error);
24441da177e4SLinus Torvalds 	locks_free_lock(file_lock);
24451da177e4SLinus Torvalds 	return error;
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds #if BITS_PER_LONG == 32
24491da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
24501da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
24511da177e4SLinus Torvalds  */
2452a75d30c7SChristoph Hellwig int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
24531da177e4SLinus Torvalds {
245452306e88SBenjamin Coddington 	struct file_lock *fl;
24551da177e4SLinus Torvalds 	int error;
24561da177e4SLinus Torvalds 
245752306e88SBenjamin Coddington 	fl = locks_alloc_lock();
245852306e88SBenjamin Coddington 	if (fl == NULL)
245952306e88SBenjamin Coddington 		return -ENOMEM;
246052306e88SBenjamin Coddington 
24611da177e4SLinus Torvalds 	error = -EINVAL;
2462a75d30c7SChristoph Hellwig 	if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
24631da177e4SLinus Torvalds 		goto out;
24641da177e4SLinus Torvalds 
246552306e88SBenjamin Coddington 	error = flock64_to_posix_lock(filp, fl, flock);
24661da177e4SLinus Torvalds 	if (error)
24671da177e4SLinus Torvalds 		goto out;
24681da177e4SLinus Torvalds 
24690d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
247090478939SJeff Layton 		error = -EINVAL;
2471a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
247290478939SJeff Layton 			goto out;
247390478939SJeff Layton 
24745d50ffd7SJeff Layton 		cmd = F_GETLK64;
247552306e88SBenjamin Coddington 		fl->fl_flags |= FL_OFDLCK;
247652306e88SBenjamin Coddington 		fl->fl_owner = filp;
24775d50ffd7SJeff Layton 	}
24785d50ffd7SJeff Layton 
247952306e88SBenjamin Coddington 	error = vfs_test_lock(filp, fl);
24803ee17abdSJ. Bruce Fields 	if (error)
24811da177e4SLinus Torvalds 		goto out;
24821da177e4SLinus Torvalds 
248352306e88SBenjamin Coddington 	flock->l_type = fl->fl_type;
248452306e88SBenjamin Coddington 	if (fl->fl_type != F_UNLCK)
248552306e88SBenjamin Coddington 		posix_lock_to_flock64(flock, fl);
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds out:
248852306e88SBenjamin Coddington 	locks_free_lock(fl);
24891da177e4SLinus Torvalds 	return error;
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
24931da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
24941da177e4SLinus Torvalds  */
2495c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2496a75d30c7SChristoph Hellwig 		struct flock64 *flock)
24971da177e4SLinus Torvalds {
24981da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
2499a75d30c7SChristoph Hellwig 	struct inode *inode = locks_inode(filp);
25000b2bac2fSAl Viro 	struct file *f;
25011da177e4SLinus Torvalds 	int error;
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 	if (file_lock == NULL)
25041da177e4SLinus Torvalds 		return -ENOLCK;
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 	/* Don't allow mandatory locks on files that may be memory mapped
25071da177e4SLinus Torvalds 	 * and shared.
25081da177e4SLinus Torvalds 	 */
2509a16877caSPavel Emelyanov 	if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
25101da177e4SLinus Torvalds 		error = -EAGAIN;
25111da177e4SLinus Torvalds 		goto out;
25121da177e4SLinus Torvalds 	}
25131da177e4SLinus Torvalds 
2514a75d30c7SChristoph Hellwig 	error = flock64_to_posix_lock(filp, file_lock, flock);
25151da177e4SLinus Torvalds 	if (error)
25161da177e4SLinus Torvalds 		goto out;
25175d50ffd7SJeff Layton 
2518cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2519cf01f4eeSJeff Layton 	if (error)
2520cf01f4eeSJeff Layton 		goto out;
2521cf01f4eeSJeff Layton 
25225d50ffd7SJeff Layton 	/*
25235d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2524cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
25255d50ffd7SJeff Layton 	 */
25265d50ffd7SJeff Layton 	switch (cmd) {
25270d3f7a2dSJeff Layton 	case F_OFD_SETLK:
252890478939SJeff Layton 		error = -EINVAL;
2529a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
253090478939SJeff Layton 			goto out;
253190478939SJeff Layton 
25325d50ffd7SJeff Layton 		cmd = F_SETLK64;
2533cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
253473a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
25355d50ffd7SJeff Layton 		break;
25360d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
253790478939SJeff Layton 		error = -EINVAL;
2538a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
253990478939SJeff Layton 			goto out;
254090478939SJeff Layton 
25415d50ffd7SJeff Layton 		cmd = F_SETLKW64;
2542cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
254373a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
25445d50ffd7SJeff Layton 		/* Fallthrough */
25455d50ffd7SJeff Layton 	case F_SETLKW64:
25461da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 
2549b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2550c293621bSPeter Staubach 
2551c293621bSPeter Staubach 	/*
25520752ba80SJeff Layton 	 * Attempt to detect a close/fcntl race and recover by releasing the
25530752ba80SJeff Layton 	 * lock that was just acquired. There is no need to do that when we're
25540752ba80SJeff Layton 	 * unlocking though, or for OFD locks.
2555c293621bSPeter Staubach 	 */
25560752ba80SJeff Layton 	if (!error && file_lock->fl_type != F_UNLCK &&
25570752ba80SJeff Layton 	    !(file_lock->fl_flags & FL_OFDLCK)) {
25587f3697e2SJeff Layton 		/*
25597f3697e2SJeff Layton 		 * We need that spin_lock here - it prevents reordering between
25607f3697e2SJeff Layton 		 * update of i_flctx->flc_posix and check for it done in
25617f3697e2SJeff Layton 		 * close(). rcu_read_lock() wouldn't do.
25627f3697e2SJeff Layton 		 */
25630b2bac2fSAl Viro 		spin_lock(&current->files->file_lock);
25640b2bac2fSAl Viro 		f = fcheck(fd);
25650b2bac2fSAl Viro 		spin_unlock(&current->files->file_lock);
25667f3697e2SJeff Layton 		if (f != filp) {
25677f3697e2SJeff Layton 			file_lock->fl_type = F_UNLCK;
25687f3697e2SJeff Layton 			error = do_lock_file_wait(filp, cmd, file_lock);
25697f3697e2SJeff Layton 			WARN_ON_ONCE(error);
25707f3697e2SJeff Layton 			error = -EBADF;
2571c293621bSPeter Staubach 		}
25727f3697e2SJeff Layton 	}
25731da177e4SLinus Torvalds out:
25741da177e4SLinus Torvalds 	locks_free_lock(file_lock);
25751da177e4SLinus Torvalds 	return error;
25761da177e4SLinus Torvalds }
25771da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds /*
25801da177e4SLinus Torvalds  * This function is called when the file is being removed
25811da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
25821da177e4SLinus Torvalds  * are deleted at this time.
25831da177e4SLinus Torvalds  */
25841da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
25851da177e4SLinus Torvalds {
25861890910fSJeff Layton 	int error;
2587c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
2588ff7b86b8SMiklos Szeredi 	struct file_lock lock;
2589128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	/*
25921da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
25931da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
25941da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
25951da177e4SLinus Torvalds 	 */
2596c568d683SMiklos Szeredi 	ctx =  smp_load_acquire(&inode->i_flctx);
2597bd61e0a9SJeff Layton 	if (!ctx || list_empty(&ctx->flc_posix))
25981da177e4SLinus Torvalds 		return;
25991da177e4SLinus Torvalds 
2600d6367d62SNeilBrown 	locks_init_lock(&lock);
26011da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
260275e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
26031da177e4SLinus Torvalds 	lock.fl_start = 0;
26041da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
26051da177e4SLinus Torvalds 	lock.fl_owner = owner;
26061da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
26071da177e4SLinus Torvalds 	lock.fl_file = filp;
26081da177e4SLinus Torvalds 	lock.fl_ops = NULL;
26091da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
26101da177e4SLinus Torvalds 
26111890910fSJeff Layton 	error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
26141da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
2615c568d683SMiklos Szeredi 	trace_locks_remove_posix(inode, &lock, error);
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
26191da177e4SLinus Torvalds 
26203d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
2621dd459bb1SJeff Layton static void
2622128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2623dd459bb1SJeff Layton {
2624d6367d62SNeilBrown 	struct file_lock fl;
2625c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
2626dd459bb1SJeff Layton 
26273d8e560dSJeff Layton 	if (list_empty(&flctx->flc_flock))
2628dd459bb1SJeff Layton 		return;
2629dd459bb1SJeff Layton 
2630d6367d62SNeilBrown 	flock_make_lock(filp, LOCK_UN, &fl);
2631d6367d62SNeilBrown 	fl.fl_flags |= FL_CLOSE;
2632d6367d62SNeilBrown 
2633de2a4a50SMiklos Szeredi 	if (filp->f_op->flock)
2634dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2635dd459bb1SJeff Layton 	else
2636bcd7f78dSJeff Layton 		flock_lock_inode(inode, &fl);
2637dd459bb1SJeff Layton 
2638dd459bb1SJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
2639dd459bb1SJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2640dd459bb1SJeff Layton }
2641dd459bb1SJeff Layton 
26423d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
26438634b51fSJeff Layton static void
2644128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
26458634b51fSJeff Layton {
26468634b51fSJeff Layton 	struct file_lock *fl, *tmp;
26478634b51fSJeff Layton 	LIST_HEAD(dispose);
26488634b51fSJeff Layton 
26493d8e560dSJeff Layton 	if (list_empty(&ctx->flc_lease))
26508634b51fSJeff Layton 		return;
26518634b51fSJeff Layton 
26525f43086bSPeter Zijlstra 	percpu_down_read_preempt_disable(&file_rwsem);
26536109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
26548634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2655c4e136cdSJeff Layton 		if (filp == fl->fl_file)
26567448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, &dispose);
26576109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
26585f43086bSPeter Zijlstra 	percpu_up_read_preempt_enable(&file_rwsem);
26595f43086bSPeter Zijlstra 
26608634b51fSJeff Layton 	locks_dispose_list(&dispose);
26618634b51fSJeff Layton }
26628634b51fSJeff Layton 
26631da177e4SLinus Torvalds /*
26641da177e4SLinus Torvalds  * This function is called on the last close of an open file.
26651da177e4SLinus Torvalds  */
266678ed8a13SJeff Layton void locks_remove_file(struct file *filp)
26671da177e4SLinus Torvalds {
2668128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2669128a3785SDmitry Vyukov 
2670c568d683SMiklos Szeredi 	ctx = smp_load_acquire(&locks_inode(filp)->i_flctx);
2671128a3785SDmitry Vyukov 	if (!ctx)
26723d8e560dSJeff Layton 		return;
26733d8e560dSJeff Layton 
2674dd459bb1SJeff Layton 	/* remove any OFD locks */
267573a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
26765d50ffd7SJeff Layton 
2677dd459bb1SJeff Layton 	/* remove flock locks */
2678128a3785SDmitry Vyukov 	locks_remove_flock(filp, ctx);
2679dd459bb1SJeff Layton 
26808634b51fSJeff Layton 	/* remove any leases */
2681128a3785SDmitry Vyukov 	locks_remove_lease(filp, ctx);
26823953704fSBenjamin Coddington 
26833953704fSBenjamin Coddington 	spin_lock(&ctx->flc_lock);
26843953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
26853953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
26863953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
26873953704fSBenjamin Coddington 	spin_unlock(&ctx->flc_lock);
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds /**
26919b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
26929b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
26939b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
26949b9d2ab4SMarc Eshel  *
26959b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
26969b9d2ab4SMarc Eshel  */
26979b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
26989b9d2ab4SMarc Eshel {
2699de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
27009b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
27019b9d2ab4SMarc Eshel 	return 0;
27029b9d2ab4SMarc Eshel }
27039b9d2ab4SMarc Eshel 
27049b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
27059b9d2ab4SMarc Eshel 
27067f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2707d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
27087f8ada98SPavel Emelyanov #include <linux/seq_file.h>
27097f8ada98SPavel Emelyanov 
27107012b02aSJeff Layton struct locks_iterator {
27117012b02aSJeff Layton 	int	li_cpu;
27127012b02aSJeff Layton 	loff_t	li_pos;
27137012b02aSJeff Layton };
27147012b02aSJeff Layton 
27157f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
271699dc8292SJerome Marchand 			    loff_t id, char *pfx)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	struct inode *inode = NULL;
2719ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2720d67fd44fSNikolay Borisov 	struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
2721d67fd44fSNikolay Borisov 
27229d5b86acSBenjamin Coddington 	fl_pid = locks_translate_pid(fl, proc_pidns);
2723d67fd44fSNikolay Borisov 	/*
27241cf8e5deSKonstantin Khorenko 	 * If lock owner is dead (and pid is freed) or not visible in current
27251cf8e5deSKonstantin Khorenko 	 * pidns, zero is shown as a pid value. Check lock info from
27261cf8e5deSKonstantin Khorenko 	 * init_pid_ns to get saved lock pid value.
2727d67fd44fSNikolay Borisov 	 */
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2730c568d683SMiklos Szeredi 		inode = locks_inode(fl->fl_file);
27311da177e4SLinus Torvalds 
273299dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
27331da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2734c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
27355315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2736cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
27375315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2738c918d42aSJeff Layton 		else
27395315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2740c918d42aSJeff Layton 
2741c918d42aSJeff Layton 		seq_printf(f, " %s ",
27421da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2743a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
27441da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
27451da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
27465315c26aSFabian Frederick 			seq_puts(f, "FLOCK  MSNFS     ");
27471da177e4SLinus Torvalds 		} else {
27485315c26aSFabian Frederick 			seq_puts(f, "FLOCK  ADVISORY  ");
27491da177e4SLinus Torvalds 		}
27501da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
27518144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
27528144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
27538144f1f6SJeff Layton 		else
27545315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
27558144f1f6SJeff Layton 
2756ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
27575315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
27581da177e4SLinus Torvalds 		else if (fl->fl_file)
27595315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
27601da177e4SLinus Torvalds 		else
27615315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
27621da177e4SLinus Torvalds 	} else {
27635315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
27641da177e4SLinus Torvalds 	}
27651da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
27667f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
27671da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
27681da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
27691da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
27701da177e4SLinus Torvalds 	} else {
27717f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2772ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
27730ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
27740ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
27751da177e4SLinus Torvalds 	}
27761da177e4SLinus Torvalds 	if (inode) {
27773648888eSJeff Layton 		/* userspace relies on this representation of dev_t */
2778ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
27791da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
27801da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
27811da177e4SLinus Torvalds 	} else {
2782ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
27831da177e4SLinus Torvalds 	}
27841da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
27851da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
27867f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
27871da177e4SLinus Torvalds 		else
27887f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
27891da177e4SLinus Torvalds 	} else {
27905315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
27911da177e4SLinus Torvalds 	}
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds 
27947f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
27951da177e4SLinus Torvalds {
27967012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
27977f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
2798d67fd44fSNikolay Borisov 	struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
27997f8ada98SPavel Emelyanov 
2800139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
28017f8ada98SPavel Emelyanov 
28029d5b86acSBenjamin Coddington 	if (locks_translate_pid(fl, proc_pidns) == 0)
2803d67fd44fSNikolay Borisov 		return 0;
2804d67fd44fSNikolay Borisov 
28057012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
28067f8ada98SPavel Emelyanov 
2807ada5c1daSNeilBrown 	list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member)
28087012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
28097f8ada98SPavel Emelyanov 
28107f8ada98SPavel Emelyanov 	return 0;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28136c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f,
28146c8c9031SAndrey Vagin 			struct list_head *head, int *id,
28156c8c9031SAndrey Vagin 			struct file *filp, struct files_struct *files)
28166c8c9031SAndrey Vagin {
28176c8c9031SAndrey Vagin 	struct file_lock *fl;
28186c8c9031SAndrey Vagin 
28196c8c9031SAndrey Vagin 	list_for_each_entry(fl, head, fl_list) {
28206c8c9031SAndrey Vagin 
28216c8c9031SAndrey Vagin 		if (filp != fl->fl_file)
28226c8c9031SAndrey Vagin 			continue;
28236c8c9031SAndrey Vagin 		if (fl->fl_owner != files &&
28246c8c9031SAndrey Vagin 		    fl->fl_owner != filp)
28256c8c9031SAndrey Vagin 			continue;
28266c8c9031SAndrey Vagin 
28276c8c9031SAndrey Vagin 		(*id)++;
28286c8c9031SAndrey Vagin 		seq_puts(f, "lock:\t");
28296c8c9031SAndrey Vagin 		lock_get_status(f, fl, *id, "");
28306c8c9031SAndrey Vagin 	}
28316c8c9031SAndrey Vagin }
28326c8c9031SAndrey Vagin 
28336c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f,
28346c8c9031SAndrey Vagin 		  struct file *filp, struct files_struct *files)
28356c8c9031SAndrey Vagin {
2836c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
28376c8c9031SAndrey Vagin 	struct file_lock_context *ctx;
28386c8c9031SAndrey Vagin 	int id = 0;
28396c8c9031SAndrey Vagin 
2840128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
28416c8c9031SAndrey Vagin 	if (!ctx)
28426c8c9031SAndrey Vagin 		return;
28436c8c9031SAndrey Vagin 
28446c8c9031SAndrey Vagin 	spin_lock(&ctx->flc_lock);
28456c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
28466c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
28476c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
28486c8c9031SAndrey Vagin 	spin_unlock(&ctx->flc_lock);
28496c8c9031SAndrey Vagin }
28506c8c9031SAndrey Vagin 
28517f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2852b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
28531da177e4SLinus Torvalds {
28547012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
285599dc8292SJerome Marchand 
28567012b02aSJeff Layton 	iter->li_pos = *pos + 1;
2857aba37660SPeter Zijlstra 	percpu_down_write(&file_rwsem);
28587b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
28597c3f654dSPeter Zijlstra 	return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
28601da177e4SLinus Torvalds }
28617f8ada98SPavel Emelyanov 
28627f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
28637f8ada98SPavel Emelyanov {
28647012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
28657012b02aSJeff Layton 
28667012b02aSJeff Layton 	++iter->li_pos;
28677c3f654dSPeter Zijlstra 	return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
28681da177e4SLinus Torvalds }
28697f8ada98SPavel Emelyanov 
28707f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2871b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
28727f8ada98SPavel Emelyanov {
28737b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
2874aba37660SPeter Zijlstra 	percpu_up_write(&file_rwsem);
28751da177e4SLinus Torvalds }
28761da177e4SLinus Torvalds 
2877d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
28787f8ada98SPavel Emelyanov 	.start	= locks_start,
28797f8ada98SPavel Emelyanov 	.next	= locks_next,
28807f8ada98SPavel Emelyanov 	.stop	= locks_stop,
28817f8ada98SPavel Emelyanov 	.show	= locks_show,
28827f8ada98SPavel Emelyanov };
2883d8ba7a36SAlexey Dobriyan 
2884d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2885d8ba7a36SAlexey Dobriyan {
288644414d82SChristoph Hellwig 	proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
288744414d82SChristoph Hellwig 			sizeof(struct locks_iterator), NULL);
2888d8ba7a36SAlexey Dobriyan 	return 0;
2889d8ba7a36SAlexey Dobriyan }
289091899226SPaul Gortmaker fs_initcall(proc_locks_init);
28917f8ada98SPavel Emelyanov #endif
28927f8ada98SPavel Emelyanov 
28931da177e4SLinus Torvalds static int __init filelock_init(void)
28941da177e4SLinus Torvalds {
28957012b02aSJeff Layton 	int i;
28967012b02aSJeff Layton 
28974a075e39SJeff Layton 	flctx_cache = kmem_cache_create("file_lock_ctx",
28984a075e39SJeff Layton 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
28994a075e39SJeff Layton 
29001da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2901ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2902ee19cc40SMiklos Szeredi 
29037012b02aSJeff Layton 
29047c3f654dSPeter Zijlstra 	for_each_possible_cpu(i) {
29057c3f654dSPeter Zijlstra 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
29067c3f654dSPeter Zijlstra 
29077c3f654dSPeter Zijlstra 		spin_lock_init(&fll->lock);
29087c3f654dSPeter Zijlstra 		INIT_HLIST_HEAD(&fll->hlist);
29097c3f654dSPeter Zijlstra 	}
29107012b02aSJeff Layton 
29111da177e4SLinus Torvalds 	return 0;
29121da177e4SLinus Torvalds }
29131da177e4SLinus Torvalds 
29141da177e4SLinus Torvalds core_initcall(filelock_init);
2915