xref: /linux/fs/locks.c (revision 945ab8f6de94430c23a82f3cf2e3f6d6f2945ff7)
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 EXPORT_SYMBOL(locks_init_lock);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /*
4051da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
4061da177e4SLinus Torvalds  */
4073fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
4101da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
4110996905fSTrond Myklebust 	new->fl_file = NULL;
4121da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
4131da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
4141da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
4151da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
416f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
4170996905fSTrond Myklebust 	new->fl_ops = NULL;
418f328296eSKinglong Mee 
419f328296eSKinglong Mee 	if (fl->fl_lmops) {
420f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
421cae80b30SJeff Layton 			fl->fl_lmops->lm_get_owner(fl->fl_owner);
422f328296eSKinglong Mee 	}
4230996905fSTrond Myklebust }
4243fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
4250996905fSTrond Myklebust 
4260996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4270996905fSTrond Myklebust {
428566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
429566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
4300996905fSTrond Myklebust 
4313fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
432f328296eSKinglong Mee 
4330996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
4341da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
43547831f35STrond Myklebust 
436f328296eSKinglong Mee 	if (fl->fl_ops) {
437f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
438f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
439f328296eSKinglong Mee 	}
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
4421da177e4SLinus Torvalds 
4435946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
4445946c431SNeilBrown {
4455946c431SNeilBrown 	struct file_lock *f;
4465946c431SNeilBrown 
4475946c431SNeilBrown 	/*
4485946c431SNeilBrown 	 * As ctx->flc_lock is held, new requests cannot be added to
4495946c431SNeilBrown 	 * ->fl_blocked_requests, so we don't need a lock to check if it
4505946c431SNeilBrown 	 * is empty.
4515946c431SNeilBrown 	 */
4525946c431SNeilBrown 	if (list_empty(&fl->fl_blocked_requests))
4535946c431SNeilBrown 		return;
4545946c431SNeilBrown 	spin_lock(&blocked_lock_lock);
4555946c431SNeilBrown 	list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
456bf77ae4cSNeilBrown 	list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member)
4575946c431SNeilBrown 		f->fl_blocker = new;
4585946c431SNeilBrown 	spin_unlock(&blocked_lock_lock);
4595946c431SNeilBrown }
4605946c431SNeilBrown 
4611da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
4621da177e4SLinus Torvalds 	if (cmd & LOCK_MAND)
4631da177e4SLinus Torvalds 		return cmd & (LOCK_MAND | LOCK_RW);
4641da177e4SLinus Torvalds 	switch (cmd) {
4651da177e4SLinus Torvalds 	case LOCK_SH:
4661da177e4SLinus Torvalds 		return F_RDLCK;
4671da177e4SLinus Torvalds 	case LOCK_EX:
4681da177e4SLinus Torvalds 		return F_WRLCK;
4691da177e4SLinus Torvalds 	case LOCK_UN:
4701da177e4SLinus Torvalds 		return F_UNLCK;
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 	return -EINVAL;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
4766e129d00SJeff Layton static struct file_lock *
477d6367d62SNeilBrown flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	int type = flock_translate_cmd(cmd);
4806e129d00SJeff Layton 
4811da177e4SLinus Torvalds 	if (type < 0)
4826e129d00SJeff Layton 		return ERR_PTR(type);
4831da177e4SLinus Torvalds 
484d6367d62SNeilBrown 	if (fl == NULL) {
4851da177e4SLinus Torvalds 		fl = locks_alloc_lock();
4861da177e4SLinus Torvalds 		if (fl == NULL)
4876e129d00SJeff Layton 			return ERR_PTR(-ENOMEM);
488d6367d62SNeilBrown 	} else {
489d6367d62SNeilBrown 		locks_init_lock(fl);
490d6367d62SNeilBrown 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	fl->fl_file = filp;
49373a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
4941da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4951da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
4961da177e4SLinus Torvalds 	fl->fl_type = type;
4971da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4981da177e4SLinus Torvalds 
4996e129d00SJeff Layton 	return fl;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5020ec4f431SJ. Bruce Fields static int assign_type(struct file_lock *fl, long type)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	switch (type) {
5051da177e4SLinus Torvalds 	case F_RDLCK:
5061da177e4SLinus Torvalds 	case F_WRLCK:
5071da177e4SLinus Torvalds 	case F_UNLCK:
5081da177e4SLinus Torvalds 		fl->fl_type = type;
5091da177e4SLinus Torvalds 		break;
5101da177e4SLinus Torvalds 	default:
5111da177e4SLinus Torvalds 		return -EINVAL;
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 	return 0;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
516ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
517ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
518ef12e72aSJ. Bruce Fields {
519ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
520ef12e72aSJ. Bruce Fields 	case SEEK_SET:
521ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
522ef12e72aSJ. Bruce Fields 		break;
523ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
524ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
525ef12e72aSJ. Bruce Fields 		break;
526ef12e72aSJ. Bruce Fields 	case SEEK_END:
527ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
528ef12e72aSJ. Bruce Fields 		break;
529ef12e72aSJ. Bruce Fields 	default:
530ef12e72aSJ. Bruce Fields 		return -EINVAL;
531ef12e72aSJ. Bruce Fields 	}
532ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
533ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
534ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
535ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
536ef12e72aSJ. Bruce Fields 		return -EINVAL;
537ef12e72aSJ. Bruce Fields 
538ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
539ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
540ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
541ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
542ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
543ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start + l->l_len - 1;
544ef12e72aSJ. Bruce Fields 
545ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
546ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
547ef12e72aSJ. Bruce Fields 			return -EINVAL;
548ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
549ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
550ef12e72aSJ. Bruce Fields 	} else
551ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
552ef12e72aSJ. Bruce Fields 
553ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
554ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
555ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
556ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
557ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
558ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
559ef12e72aSJ. Bruce Fields 
560ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
561ef12e72aSJ. Bruce Fields }
562ef12e72aSJ. Bruce Fields 
5631da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
5641da177e4SLinus Torvalds  * style lock.
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
5671da177e4SLinus Torvalds 			       struct flock *l)
5681da177e4SLinus Torvalds {
569ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
570ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
571ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
572ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
573ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
574ef12e72aSJ. Bruce Fields 	};
5751da177e4SLinus Torvalds 
576ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /* default lease lock manager operations */
5804d01b7f5SJeff Layton static bool
5814d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
5844d01b7f5SJeff Layton 	return false;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871c7dd2ffSJeff Layton static void
5881c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv)
5891c7dd2ffSJeff Layton {
5901c7dd2ffSJeff Layton 	struct file *filp = fl->fl_file;
5911c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
5921c7dd2ffSJeff Layton 
5931c7dd2ffSJeff Layton 	/*
5941c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
5951c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
5961c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
5971c7dd2ffSJeff Layton 	 */
5981c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
5991c7dd2ffSJeff Layton 		*priv = NULL;
6001c7dd2ffSJeff Layton 
60101919134SEric W. Biederman 	__f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
6021c7dd2ffSJeff Layton }
6031c7dd2ffSJeff Layton 
6047b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
6058fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
6068fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
6071c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
6081da177e4SLinus Torvalds };
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
6111da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
6121da177e4SLinus Torvalds  */
6130ec4f431SJ. Bruce Fields static int lease_init(struct file *filp, long type, struct file_lock *fl)
6141da177e4SLinus Torvalds {
61575dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
61675dff55aSTrond Myklebust 		return -EINVAL;
61775dff55aSTrond Myklebust 
6187ca76311SJeff Layton 	fl->fl_owner = filp;
6191da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	fl->fl_file = filp;
6221da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
6231da177e4SLinus Torvalds 	fl->fl_start = 0;
6241da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
6251da177e4SLinus Torvalds 	fl->fl_ops = NULL;
6261da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
6271da177e4SLinus Torvalds 	return 0;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
6310ec4f431SJ. Bruce Fields static struct file_lock *lease_alloc(struct file *filp, long type)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
63475dff55aSTrond Myklebust 	int error = -ENOMEM;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (fl == NULL)
637e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
64075dff55aSTrond Myklebust 	if (error) {
64175dff55aSTrond Myklebust 		locks_free_lock(fl);
642e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
64375dff55aSTrond Myklebust 	}
644e32b8ee2SJ. Bruce Fields 	return fl;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds /* Check if two locks overlap each other.
6481da177e4SLinus Torvalds  */
6491da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
6501da177e4SLinus Torvalds {
6511da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
6521da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /*
6561da177e4SLinus Torvalds  * Check whether two locks have the same owner.
6571da177e4SLinus Torvalds  */
65833443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
6591da177e4SLinus Torvalds {
6608fb47a4fSJ. Bruce Fields 	if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
6611da177e4SLinus Torvalds 		return fl2->fl_lmops == fl1->fl_lmops &&
6628fb47a4fSJ. Bruce Fields 			fl1->fl_lmops->lm_compare_owner(fl1, fl2);
6631da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6666109c850SJeff Layton /* Must be called with the flc_lock held! */
6676ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
66888974691SJeff Layton {
6697c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
6707c3f654dSPeter Zijlstra 
671aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
672aba37660SPeter Zijlstra 
6737c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
6747012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
6757c3f654dSPeter Zijlstra 	hlist_add_head(&fl->fl_link, &fll->hlist);
6767c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
67788974691SJeff Layton }
67888974691SJeff Layton 
6796109c850SJeff Layton /* Must be called with the flc_lock held! */
6806ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
68188974691SJeff Layton {
6827c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll;
6837c3f654dSPeter Zijlstra 
684aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
685aba37660SPeter Zijlstra 
6867012b02aSJeff Layton 	/*
6877012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
6886109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
6897012b02aSJeff Layton 	 * also require that it be held.
6907012b02aSJeff Layton 	 */
6917012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
6927012b02aSJeff Layton 		return;
6937c3f654dSPeter Zijlstra 
6947c3f654dSPeter Zijlstra 	fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
6957c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
696139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
6977c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
69888974691SJeff Layton }
69988974691SJeff Layton 
7003999e493SJeff Layton static unsigned long
7013999e493SJeff Layton posix_owner_key(struct file_lock *fl)
7023999e493SJeff Layton {
7033999e493SJeff Layton 	if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
7043999e493SJeff Layton 		return fl->fl_lmops->lm_owner_key(fl);
7053999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
7063999e493SJeff Layton }
7073999e493SJeff Layton 
7086ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
70988974691SJeff Layton {
710663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
711663d5af7SDaniel Wagner 
7123999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
71388974691SJeff Layton }
71488974691SJeff Layton 
7156ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
71688974691SJeff Layton {
717663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
718663d5af7SDaniel Wagner 
71948f74186SJeff Layton 	hash_del(&waiter->fl_link);
72088974691SJeff Layton }
72188974691SJeff Layton 
7221da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
7231da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
7241c8c601aSJeff Layton  *
7257b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
7261da177e4SLinus Torvalds  */
72733443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
7281da177e4SLinus Torvalds {
72988974691SJeff Layton 	locks_delete_global_blocked(waiter);
730ada5c1daSNeilBrown 	list_del_init(&waiter->fl_blocked_member);
731ada5c1daSNeilBrown 	waiter->fl_blocker = NULL;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734ad6bbd8bSNeilBrown static void __locks_wake_up_blocks(struct file_lock *blocker)
735ad6bbd8bSNeilBrown {
736ad6bbd8bSNeilBrown 	while (!list_empty(&blocker->fl_blocked_requests)) {
737ad6bbd8bSNeilBrown 		struct file_lock *waiter;
738ad6bbd8bSNeilBrown 
739ad6bbd8bSNeilBrown 		waiter = list_first_entry(&blocker->fl_blocked_requests,
740ad6bbd8bSNeilBrown 					  struct file_lock, fl_blocked_member);
741ad6bbd8bSNeilBrown 		__locks_delete_block(waiter);
742ad6bbd8bSNeilBrown 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
743ad6bbd8bSNeilBrown 			waiter->fl_lmops->lm_notify(waiter);
744ad6bbd8bSNeilBrown 		else
745ad6bbd8bSNeilBrown 			wake_up(&waiter->fl_wait);
746ad6bbd8bSNeilBrown 	}
747ad6bbd8bSNeilBrown }
748ad6bbd8bSNeilBrown 
749cb03f94fSNeilBrown /**
750cb03f94fSNeilBrown  *	locks_delete_lock - stop waiting for a file lock
751cb03f94fSNeilBrown  *	@waiter: the lock which was waiting
752cb03f94fSNeilBrown  *
753cb03f94fSNeilBrown  *	lockd/nfsd need to disconnect the lock while working on it.
754cb03f94fSNeilBrown  */
755cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter)
7561da177e4SLinus Torvalds {
757cb03f94fSNeilBrown 	int status = -ENOENT;
758cb03f94fSNeilBrown 
75916306a61SNeilBrown 	/*
76016306a61SNeilBrown 	 * If fl_blocker is NULL, it won't be set again as this thread
76116306a61SNeilBrown 	 * "owns" the lock and is the only one that might try to claim
76216306a61SNeilBrown 	 * the lock.  So it is safe to test fl_blocker locklessly.
76316306a61SNeilBrown 	 * Also if fl_blocker is NULL, this waiter is not listed on
76416306a61SNeilBrown 	 * fl_blocked_requests for some lock, so no other request can
76516306a61SNeilBrown 	 * be added to the list of fl_blocked_requests for this
76616306a61SNeilBrown 	 * request.  So if fl_blocker is NULL, it is safe to
76716306a61SNeilBrown 	 * locklessly check if fl_blocked_requests is empty.  If both
76816306a61SNeilBrown 	 * of these checks succeed, there is no need to take the lock.
76916306a61SNeilBrown 	 */
77016306a61SNeilBrown 	if (waiter->fl_blocker == NULL &&
77116306a61SNeilBrown 	    list_empty(&waiter->fl_blocked_requests))
772cb03f94fSNeilBrown 		return status;
7737b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
774cb03f94fSNeilBrown 	if (waiter->fl_blocker)
775cb03f94fSNeilBrown 		status = 0;
7765946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
7771da177e4SLinus Torvalds 	__locks_delete_block(waiter);
7787b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
779cb03f94fSNeilBrown 	return status;
7801da177e4SLinus Torvalds }
781cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
7841da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
7851da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
7861da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
7871c8c601aSJeff Layton  *
7886109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
789ada5c1daSNeilBrown  * fl_blocked_requests list itself is protected by the blocked_lock_lock,
790ada5c1daSNeilBrown  * but by ensuring that the flc_lock is also held on insertions we can avoid
791ada5c1daSNeilBrown  * taking the blocked_lock_lock in some cases when we see that the
792ada5c1daSNeilBrown  * fl_blocked_requests list is empty.
793fd7732e0SNeilBrown  *
794fd7732e0SNeilBrown  * Rather than just adding to the list, we check for conflicts with any existing
795fd7732e0SNeilBrown  * waiters, and add beneath any waiter that blocks the new waiter.
796fd7732e0SNeilBrown  * Thus wakeups don't happen until needed.
7971da177e4SLinus Torvalds  */
7981c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
799fd7732e0SNeilBrown 				 struct file_lock *waiter,
800fd7732e0SNeilBrown 				 bool conflict(struct file_lock *,
801fd7732e0SNeilBrown 					       struct file_lock *))
8021da177e4SLinus Torvalds {
803fd7732e0SNeilBrown 	struct file_lock *fl;
804ada5c1daSNeilBrown 	BUG_ON(!list_empty(&waiter->fl_blocked_member));
805fd7732e0SNeilBrown 
806fd7732e0SNeilBrown new_blocker:
807fd7732e0SNeilBrown 	list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member)
808fd7732e0SNeilBrown 		if (conflict(fl, waiter)) {
809fd7732e0SNeilBrown 			blocker =  fl;
810fd7732e0SNeilBrown 			goto new_blocker;
811fd7732e0SNeilBrown 		}
812ada5c1daSNeilBrown 	waiter->fl_blocker = blocker;
813ada5c1daSNeilBrown 	list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
814cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
8151c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
8165946c431SNeilBrown 
8175946c431SNeilBrown 	/* The requests in waiter->fl_blocked are known to conflict with
8185946c431SNeilBrown 	 * waiter, but might not conflict with blocker, or the requests
8195946c431SNeilBrown 	 * and lock which block it.  So they all need to be woken.
8205946c431SNeilBrown 	 */
8215946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
8221c8c601aSJeff Layton }
8231c8c601aSJeff Layton 
8246109c850SJeff Layton /* Must be called with flc_lock held. */
8251c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
826fd7732e0SNeilBrown 			       struct file_lock *waiter,
827fd7732e0SNeilBrown 			       bool conflict(struct file_lock *,
828fd7732e0SNeilBrown 					     struct file_lock *))
8291c8c601aSJeff Layton {
8307b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
831fd7732e0SNeilBrown 	__locks_insert_block(blocker, waiter, conflict);
8327b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351cb36012SJeff Layton /*
8361cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
8371cb36012SJeff Layton  *
8386109c850SJeff Layton  * Must be called with the inode->flc_lock held!
8391da177e4SLinus Torvalds  */
8401da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
8411da177e4SLinus Torvalds {
8424e8c765dSJeff Layton 	/*
8434e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
8446109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
845ada5c1daSNeilBrown 	 * the flc_lock is always held here. Note that removal from the
846ada5c1daSNeilBrown 	 * fl_blocked_requests list does not require the flc_lock, so we must
847ada5c1daSNeilBrown 	 * recheck list_empty() after acquiring the blocked_lock_lock.
8484e8c765dSJeff Layton 	 */
849ada5c1daSNeilBrown 	if (list_empty(&blocker->fl_blocked_requests))
8504e8c765dSJeff Layton 		return;
8514e8c765dSJeff Layton 
8527b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
853ad6bbd8bSNeilBrown 	__locks_wake_up_blocks(blocker);
8547b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8575263e31eSJeff Layton static void
858e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
8595263e31eSJeff Layton {
8605263e31eSJeff Layton 	list_add_tail(&fl->fl_list, before);
8615263e31eSJeff Layton 	locks_insert_global_locks(fl);
8625263e31eSJeff Layton }
8635263e31eSJeff Layton 
8648634b51fSJeff Layton static void
865e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl)
8661da177e4SLinus Torvalds {
86788974691SJeff Layton 	locks_delete_global_locks(fl);
8688634b51fSJeff Layton 	list_del_init(&fl->fl_list);
8691da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
87024cbe784SJeff Layton }
87124cbe784SJeff Layton 
8725263e31eSJeff Layton static void
873e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
8745263e31eSJeff Layton {
875e084c1bdSJeff Layton 	locks_unlink_lock_ctx(fl);
8768634b51fSJeff Layton 	if (dispose)
8778634b51fSJeff Layton 		list_add(&fl->fl_list, dispose);
8788634b51fSJeff Layton 	else
8798634b51fSJeff Layton 		locks_free_lock(fl);
8805263e31eSJeff Layton }
8815263e31eSJeff Layton 
8821da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
8831da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
8841da177e4SLinus Torvalds  */
885c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl,
886c0e15908SNeilBrown 			   struct file_lock *sys_fl)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
889c0e15908SNeilBrown 		return true;
8901da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
891c0e15908SNeilBrown 		return true;
892c0e15908SNeilBrown 	return false;
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
8961da177e4SLinus Torvalds  * checking before calling the locks_conflict().
8971da177e4SLinus Torvalds  */
898c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl,
899c0e15908SNeilBrown 				 struct file_lock *sys_fl)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
9021da177e4SLinus Torvalds 	 * each other.
9031da177e4SLinus Torvalds 	 */
9049b8c8695SJeff Layton 	if (posix_same_owner(caller_fl, sys_fl))
905c0e15908SNeilBrown 		return false;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Check whether they overlap */
9081da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
909c0e15908SNeilBrown 		return false;
9101da177e4SLinus Torvalds 
911c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
9151da177e4SLinus Torvalds  * checking before calling the locks_conflict().
9161da177e4SLinus Torvalds  */
917c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl,
918c0e15908SNeilBrown 				 struct file_lock *sys_fl)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
9211da177e4SLinus Torvalds 	 * each other.
9221da177e4SLinus Torvalds 	 */
9239b8c8695SJeff Layton 	if (caller_fl->fl_file == sys_fl->fl_file)
924c0e15908SNeilBrown 		return false;
9251da177e4SLinus Torvalds 	if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
926c0e15908SNeilBrown 		return false;
9271da177e4SLinus Torvalds 
928c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
9316d34ac19SJ. Bruce Fields void
9329d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct file_lock *cfl;
935bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
936c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
9371da177e4SLinus Torvalds 
938128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
939bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
940bd61e0a9SJeff Layton 		fl->fl_type = F_UNLCK;
941bd61e0a9SJeff Layton 		return;
9421da177e4SLinus Torvalds 	}
943bd61e0a9SJeff Layton 
9446109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
945bd61e0a9SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
946bd61e0a9SJeff Layton 		if (posix_locks_conflict(fl, cfl)) {
9473fe0fff1SKinglong Mee 			locks_copy_conflock(fl, cfl);
948bd61e0a9SJeff Layton 			goto out;
949bd61e0a9SJeff Layton 		}
950bd61e0a9SJeff Layton 	}
951129a84deSJ. Bruce Fields 	fl->fl_type = F_UNLCK;
952bd61e0a9SJeff Layton out:
9536109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
9546d34ac19SJ. Bruce Fields 	return;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
9571da177e4SLinus Torvalds 
958b533184fSJ. Bruce Fields /*
959b533184fSJ. Bruce Fields  * Deadlock detection:
9601da177e4SLinus Torvalds  *
961b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
962b533184fSJ. Bruce Fields  * locks.
9631da177e4SLinus Torvalds  *
964b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
965b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
966b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
967b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
968b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
969b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
970b533184fSJ. Bruce Fields  * cycle.
97197855b49SJ. Bruce Fields  *
972b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
973b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
974b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
975b533184fSJ. Bruce Fields  *
976b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
977b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
978b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
979b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
98057b65325SJeff Layton  *
981cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
98257b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
98357b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
98457b65325SJeff Layton  * skip it for those.
98557b65325SJeff Layton  *
986cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
98757b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
98857b65325SJeff Layton  * upgrade from read to write locks on the same inode.
9891da177e4SLinus Torvalds  */
99097855b49SJ. Bruce Fields 
99197855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
99297855b49SJ. Bruce Fields 
993b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
994b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
995b533184fSJ. Bruce Fields {
996b533184fSJ. Bruce Fields 	struct file_lock *fl;
997b533184fSJ. Bruce Fields 
9983999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
9995946c431SNeilBrown 		if (posix_same_owner(fl, block_fl)) {
10005946c431SNeilBrown 			while (fl->fl_blocker)
10015946c431SNeilBrown 				fl = fl->fl_blocker;
10025946c431SNeilBrown 			return fl;
10035946c431SNeilBrown 		}
1004b533184fSJ. Bruce Fields 	}
1005b533184fSJ. Bruce Fields 	return NULL;
1006b533184fSJ. Bruce Fields }
1007b533184fSJ. Bruce Fields 
10087b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
1009b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
10101da177e4SLinus Torvalds 				struct file_lock *block_fl)
10111da177e4SLinus Torvalds {
101297855b49SJ. Bruce Fields 	int i = 0;
10131da177e4SLinus Torvalds 
1014663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
1015663d5af7SDaniel Wagner 
101657b65325SJeff Layton 	/*
101757b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
1018cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
101957b65325SJeff Layton 	 */
1020cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
102157b65325SJeff Layton 		return 0;
102257b65325SJeff Layton 
1023b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
102497855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
102597855b49SJ. Bruce Fields 			return 0;
1026b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
1027b533184fSJ. Bruce Fields 			return 1;
10281da177e4SLinus Torvalds 	}
10291da177e4SLinus Torvalds 	return 0;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
103302888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
1034f475ae95STrond Myklebust  *
1035f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1036f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1037f475ae95STrond Myklebust  * value for -ENOENT.
10381da177e4SLinus Torvalds  */
1039bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request)
10401da177e4SLinus Torvalds {
1041993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
10425263e31eSJeff Layton 	struct file_lock *fl;
10435263e31eSJeff Layton 	struct file_lock_context *ctx;
10441da177e4SLinus Torvalds 	int error = 0;
10455263e31eSJeff Layton 	bool found = false;
1046ed9814d8SJeff Layton 	LIST_HEAD(dispose);
10471da177e4SLinus Torvalds 
10485c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
10495c1c669aSJeff Layton 	if (!ctx) {
10505c1c669aSJeff Layton 		if (request->fl_type != F_UNLCK)
10515263e31eSJeff Layton 			return -ENOMEM;
10525c1c669aSJeff Layton 		return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
10535c1c669aSJeff Layton 	}
10545263e31eSJeff Layton 
1055b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
1056b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
1057b89f4321SArnd Bergmann 		if (!new_fl)
1058b89f4321SArnd Bergmann 			return -ENOMEM;
1059b89f4321SArnd Bergmann 	}
1060b89f4321SArnd Bergmann 
106102e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
10626109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1063f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1064f07f18ddSTrond Myklebust 		goto find_conflict;
106584d535adSPavel Emelyanov 
10665263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1067bcd7f78dSJeff Layton 		if (request->fl_file != fl->fl_file)
10681da177e4SLinus Torvalds 			continue;
1069993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
10701da177e4SLinus Torvalds 			goto out;
10715263e31eSJeff Layton 		found = true;
1072e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, &dispose);
10731da177e4SLinus Torvalds 		break;
10741da177e4SLinus Torvalds 	}
10751da177e4SLinus Torvalds 
1076f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
1077f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
1078f475ae95STrond Myklebust 			error = -ENOENT;
1079993dfa87STrond Myklebust 		goto out;
1080f475ae95STrond Myklebust 	}
10811da177e4SLinus Torvalds 
1082f07f18ddSTrond Myklebust find_conflict:
10835263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1084993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
10851da177e4SLinus Torvalds 			continue;
10861da177e4SLinus Torvalds 		error = -EAGAIN;
1087bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
1088bde74e4bSMiklos Szeredi 			goto out;
1089bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
1090fd7732e0SNeilBrown 		locks_insert_block(fl, request, flock_locks_conflict);
10911da177e4SLinus Torvalds 		goto out;
10921da177e4SLinus Torvalds 	}
1093f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1094f07f18ddSTrond Myklebust 		goto out;
1095993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
10965946c431SNeilBrown 	locks_move_blocks(new_fl, request);
1097e084c1bdSJeff Layton 	locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
1098993dfa87STrond Myklebust 	new_fl = NULL;
10999cedc194SKirill Korotaev 	error = 0;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds out:
11026109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
110302e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1104993dfa87STrond Myklebust 	if (new_fl)
1105993dfa87STrond Myklebust 		locks_free_lock(new_fl);
1106ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
1107c883da31SJeff Layton 	trace_flock_lock_inode(inode, request, error);
11081da177e4SLinus Torvalds 	return error;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1112b4d629a3SJeff Layton 			    struct file_lock *conflock)
11131da177e4SLinus Torvalds {
1114bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
111539005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
111639005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
11171da177e4SLinus Torvalds 	struct file_lock *left = NULL;
11181da177e4SLinus Torvalds 	struct file_lock *right = NULL;
1119bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
1120b9746ef8SJeff Layton 	int error;
1121b9746ef8SJeff Layton 	bool added = false;
1122ed9814d8SJeff Layton 	LIST_HEAD(dispose);
11231da177e4SLinus Torvalds 
11245c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
1125bd61e0a9SJeff Layton 	if (!ctx)
11265c1c669aSJeff Layton 		return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
1127bd61e0a9SJeff Layton 
11281da177e4SLinus Torvalds 	/*
11291da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
11301da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
113139005d02SMiklos Szeredi 	 *
113239005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
11331da177e4SLinus Torvalds 	 */
113439005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
113539005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
113639005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
11371da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
11381da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
113939005d02SMiklos Szeredi 	}
11401da177e4SLinus Torvalds 
114102e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
11426109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
11431cb36012SJeff Layton 	/*
11441cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
11451cb36012SJeff Layton 	 * there are any, either return error or put the request on the
114648f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
11471cb36012SJeff Layton 	 */
11481da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
1149bd61e0a9SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
11501da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
11511da177e4SLinus Torvalds 				continue;
11525842add2SAndy Adamson 			if (conflock)
11533fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
11541da177e4SLinus Torvalds 			error = -EAGAIN;
11551da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
11561da177e4SLinus Torvalds 				goto out;
11571c8c601aSJeff Layton 			/*
11581c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
11591c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
11601c8c601aSJeff Layton 			 */
11611da177e4SLinus Torvalds 			error = -EDEADLK;
11627b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
1163*945ab8f6SJeff Layton 			/*
1164*945ab8f6SJeff Layton 			 * Ensure that we don't find any locks blocked on this
1165*945ab8f6SJeff Layton 			 * request during deadlock detection.
1166*945ab8f6SJeff Layton 			 */
1167*945ab8f6SJeff Layton 			__locks_wake_up_blocks(request);
11681c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
1169bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
1170fd7732e0SNeilBrown 				__locks_insert_block(fl, request,
1171fd7732e0SNeilBrown 						     posix_locks_conflict);
11721c8c601aSJeff Layton 			}
11737b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
11741da177e4SLinus Torvalds 			goto out;
11751da177e4SLinus Torvalds 		}
11761da177e4SLinus Torvalds 	}
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
11791da177e4SLinus Torvalds 	error = 0;
11801da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
11811da177e4SLinus Torvalds 		goto out;
11821da177e4SLinus Torvalds 
1183bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
1184bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1185bd61e0a9SJeff Layton 		if (posix_same_owner(request, fl))
1186bd61e0a9SJeff Layton 			break;
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	/* Process locks with this owner. */
1190bd61e0a9SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1191bd61e0a9SJeff Layton 		if (!posix_same_owner(request, fl))
1192bd61e0a9SJeff Layton 			break;
1193bd61e0a9SJeff Layton 
1194bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
11951da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
1196449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1197449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1198449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1199449231d6SOlaf Kirch 			 */
12001da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1201bd61e0a9SJeff Layton 				continue;
12021da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
12031da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
12041da177e4SLinus Torvalds 			 */
1205449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
12061da177e4SLinus Torvalds 				break;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
12091da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
12101da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
12111da177e4SLinus Torvalds 			 * locks to the higher end address.
12121da177e4SLinus Torvalds 			 */
12131da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
12141da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
12151da177e4SLinus Torvalds 			else
12161da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
12171da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
12181da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
12191da177e4SLinus Torvalds 			else
12201da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
12211da177e4SLinus Torvalds 			if (added) {
1222e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
12231da177e4SLinus Torvalds 				continue;
12241da177e4SLinus Torvalds 			}
12251da177e4SLinus Torvalds 			request = fl;
1226b9746ef8SJeff Layton 			added = true;
1227bd61e0a9SJeff Layton 		} else {
12281da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
12291da177e4SLinus Torvalds 			 * more complex.
12301da177e4SLinus Torvalds 			 */
12311da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1232bd61e0a9SJeff Layton 				continue;
12331da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
12341da177e4SLinus Torvalds 				break;
12351da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1236b9746ef8SJeff Layton 				added = true;
12371da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
12381da177e4SLinus Torvalds 				left = fl;
12391da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
12401da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
12411da177e4SLinus Torvalds 			 */
12421da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
12431da177e4SLinus Torvalds 				right = fl;
12441da177e4SLinus Torvalds 				break;
12451da177e4SLinus Torvalds 			}
12461da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
12471da177e4SLinus Torvalds 				/* The new lock completely replaces an old
12481da177e4SLinus Torvalds 				 * one (This may happen several times).
12491da177e4SLinus Torvalds 				 */
12501da177e4SLinus Torvalds 				if (added) {
1251e084c1bdSJeff Layton 					locks_delete_lock_ctx(fl, &dispose);
12521da177e4SLinus Torvalds 					continue;
12531da177e4SLinus Torvalds 				}
1254b84d49f9SJeff Layton 				/*
1255b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1256b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1257b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1258b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1259b84d49f9SJeff Layton 				 * just replacing an existing lock.
12601da177e4SLinus Torvalds 				 */
1261b84d49f9SJeff Layton 				error = -ENOLCK;
1262b84d49f9SJeff Layton 				if (!new_fl)
1263b84d49f9SJeff Layton 					goto out;
1264b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
1265b84d49f9SJeff Layton 				request = new_fl;
1266b84d49f9SJeff Layton 				new_fl = NULL;
1267e084c1bdSJeff Layton 				locks_insert_lock_ctx(request, &fl->fl_list);
1268e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
1269b9746ef8SJeff Layton 				added = true;
12701da177e4SLinus Torvalds 			}
12711da177e4SLinus Torvalds 		}
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
12740d9a490aSMiklos Szeredi 	/*
12751cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
12761cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
12771cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
12780d9a490aSMiklos Szeredi 	 */
12790d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
12800d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
12810d9a490aSMiklos Szeredi 		goto out;
12820d9a490aSMiklos Szeredi 
12831da177e4SLinus Torvalds 	error = 0;
12841da177e4SLinus Torvalds 	if (!added) {
1285f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1286f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1287f475ae95STrond Myklebust 				error = -ENOENT;
12881da177e4SLinus Torvalds 			goto out;
1289f475ae95STrond Myklebust 		}
12900d9a490aSMiklos Szeredi 
12910d9a490aSMiklos Szeredi 		if (!new_fl) {
12920d9a490aSMiklos Szeredi 			error = -ENOLCK;
12930d9a490aSMiklos Szeredi 			goto out;
12940d9a490aSMiklos Szeredi 		}
12951da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
12965946c431SNeilBrown 		locks_move_blocks(new_fl, request);
1297e084c1bdSJeff Layton 		locks_insert_lock_ctx(new_fl, &fl->fl_list);
12982e2f756fSJeff Layton 		fl = new_fl;
12991da177e4SLinus Torvalds 		new_fl = NULL;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	if (right) {
13021da177e4SLinus Torvalds 		if (left == right) {
13031da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
13041da177e4SLinus Torvalds 			 * so we have to use the second new lock.
13051da177e4SLinus Torvalds 			 */
13061da177e4SLinus Torvalds 			left = new_fl2;
13071da177e4SLinus Torvalds 			new_fl2 = NULL;
13081da177e4SLinus Torvalds 			locks_copy_lock(left, right);
1309e084c1bdSJeff Layton 			locks_insert_lock_ctx(left, &fl->fl_list);
13101da177e4SLinus Torvalds 		}
13111da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
13121da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 	if (left) {
13151da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
13161da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds  out:
13196109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
132002e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
13211da177e4SLinus Torvalds 	/*
13221da177e4SLinus Torvalds 	 * Free any unused locks.
13231da177e4SLinus Torvalds 	 */
13241da177e4SLinus Torvalds 	if (new_fl)
13251da177e4SLinus Torvalds 		locks_free_lock(new_fl);
13261da177e4SLinus Torvalds 	if (new_fl2)
13271da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1328ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
13291890910fSJeff Layton 	trace_posix_lock_inode(inode, request, error);
13301890910fSJeff Layton 
13311da177e4SLinus Torvalds 	return error;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds /**
13351da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
13361da177e4SLinus Torvalds  * @filp: The file to apply the lock to
13371da177e4SLinus Torvalds  * @fl: The lock to be applied
1338150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
13391da177e4SLinus Torvalds  *
13401da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
13411da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
13421da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1343f475ae95STrond Myklebust  *
1344f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1345f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1346f475ae95STrond Myklebust  * value for -ENOENT.
13471da177e4SLinus Torvalds  */
1348150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
13495842add2SAndy Adamson 			struct file_lock *conflock)
13505842add2SAndy Adamson {
1351c568d683SMiklos Szeredi 	return posix_lock_inode(locks_inode(filp), fl, conflock);
13525842add2SAndy Adamson }
1353150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds /**
135629d01b22SJeff Layton  * posix_lock_inode_wait - Apply a POSIX-style lock to a file
135729d01b22SJeff Layton  * @inode: inode of file to which lock request should be applied
13581da177e4SLinus Torvalds  * @fl: The lock to be applied
13591da177e4SLinus Torvalds  *
1360616fb38fSBenjamin Coddington  * Apply a POSIX style lock request to an inode.
13611da177e4SLinus Torvalds  */
1362616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
13631da177e4SLinus Torvalds {
13641da177e4SLinus Torvalds 	int error;
13651da177e4SLinus Torvalds 	might_sleep ();
13661da177e4SLinus Torvalds 	for (;;) {
1367b4d629a3SJeff Layton 		error = posix_lock_inode(inode, fl, NULL);
1368bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
13691da177e4SLinus Torvalds 			break;
1370ada5c1daSNeilBrown 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
137116306a61SNeilBrown 		if (error)
13721da177e4SLinus Torvalds 			break;
13731da177e4SLinus Torvalds 	}
137416306a61SNeilBrown 	locks_delete_block(fl);
13751da177e4SLinus Torvalds 	return error;
13761da177e4SLinus Torvalds }
137729d01b22SJeff Layton 
13789e8925b6SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING
137929d01b22SJeff Layton /**
13801da177e4SLinus Torvalds  * locks_mandatory_locked - Check for an active lock
1381d7a06983SJeff Layton  * @file: the file to check
13821da177e4SLinus Torvalds  *
13831da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
13841da177e4SLinus Torvalds  * This function is called from locks_verify_locked() only.
13851da177e4SLinus Torvalds  */
1386d7a06983SJeff Layton int locks_mandatory_locked(struct file *file)
13871da177e4SLinus Torvalds {
1388bd61e0a9SJeff Layton 	int ret;
1389c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(file);
1390bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
13911da177e4SLinus Torvalds 	struct file_lock *fl;
13921da177e4SLinus Torvalds 
1393128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
1394bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix))
1395bd61e0a9SJeff Layton 		return 0;
1396bd61e0a9SJeff Layton 
13971da177e4SLinus Torvalds 	/*
13981da177e4SLinus Torvalds 	 * Search the lock list for this inode for any POSIX locks.
13991da177e4SLinus Torvalds 	 */
14006109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1401bd61e0a9SJeff Layton 	ret = 0;
1402bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
140373a8f5f7SChristoph Hellwig 		if (fl->fl_owner != current->files &&
1404bd61e0a9SJeff Layton 		    fl->fl_owner != file) {
1405bd61e0a9SJeff Layton 			ret = -EAGAIN;
14061da177e4SLinus Torvalds 			break;
14071da177e4SLinus Torvalds 		}
1408bd61e0a9SJeff Layton 	}
14096109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
1410bd61e0a9SJeff Layton 	return ret;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /**
14141da177e4SLinus Torvalds  * locks_mandatory_area - Check for a conflicting lock
14151da177e4SLinus Torvalds  * @inode:	the file to check
14161da177e4SLinus Torvalds  * @filp:       how the file was opened (if it was)
1417acc15575SChristoph Hellwig  * @start:	first byte in the file to check
1418acc15575SChristoph Hellwig  * @end:	lastbyte in the file to check
1419acc15575SChristoph Hellwig  * @type:	%F_WRLCK for a write lock, else %F_RDLCK
14201da177e4SLinus Torvalds  *
14211da177e4SLinus Torvalds  * Searches the inode's list of locks to find any POSIX locks which conflict.
14221da177e4SLinus Torvalds  */
1423acc15575SChristoph Hellwig int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
1424acc15575SChristoph Hellwig 			 loff_t end, unsigned char type)
14251da177e4SLinus Torvalds {
14261da177e4SLinus Torvalds 	struct file_lock fl;
14271da177e4SLinus Torvalds 	int error;
142829723adeSJeff Layton 	bool sleep = false;
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	locks_init_lock(&fl);
14311da177e4SLinus Torvalds 	fl.fl_pid = current->tgid;
14321da177e4SLinus Torvalds 	fl.fl_file = filp;
14331da177e4SLinus Torvalds 	fl.fl_flags = FL_POSIX | FL_ACCESS;
14341da177e4SLinus Torvalds 	if (filp && !(filp->f_flags & O_NONBLOCK))
143529723adeSJeff Layton 		sleep = true;
1436acc15575SChristoph Hellwig 	fl.fl_type = type;
1437acc15575SChristoph Hellwig 	fl.fl_start = start;
1438acc15575SChristoph Hellwig 	fl.fl_end = end;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	for (;;) {
144129723adeSJeff Layton 		if (filp) {
144273a8f5f7SChristoph Hellwig 			fl.fl_owner = filp;
144329723adeSJeff Layton 			fl.fl_flags &= ~FL_SLEEP;
1444b4d629a3SJeff Layton 			error = posix_lock_inode(inode, &fl, NULL);
144529723adeSJeff Layton 			if (!error)
144629723adeSJeff Layton 				break;
144729723adeSJeff Layton 		}
144829723adeSJeff Layton 
144929723adeSJeff Layton 		if (sleep)
145029723adeSJeff Layton 			fl.fl_flags |= FL_SLEEP;
145129723adeSJeff Layton 		fl.fl_owner = current->files;
1452b4d629a3SJeff Layton 		error = posix_lock_inode(inode, &fl, NULL);
1453bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
14541da177e4SLinus Torvalds 			break;
1455ada5c1daSNeilBrown 		error = wait_event_interruptible(fl.fl_wait, !fl.fl_blocker);
14561da177e4SLinus Torvalds 		if (!error) {
14571da177e4SLinus Torvalds 			/*
14581da177e4SLinus Torvalds 			 * If we've been sleeping someone might have
14591da177e4SLinus Torvalds 			 * changed the permissions behind our back.
14601da177e4SLinus Torvalds 			 */
1461a16877caSPavel Emelyanov 			if (__mandatory_lock(inode))
14621da177e4SLinus Torvalds 				continue;
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 		break;
14661da177e4SLinus Torvalds 	}
146716306a61SNeilBrown 	locks_delete_block(&fl);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	return error;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds EXPORT_SYMBOL(locks_mandatory_area);
14729e8925b6SJeff Layton #endif /* CONFIG_MANDATORY_FILE_LOCKING */
14731da177e4SLinus Torvalds 
1474778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1475778fc546SJ. Bruce Fields {
1476778fc546SJ. Bruce Fields 	switch (arg) {
1477778fc546SJ. Bruce Fields 	case F_UNLCK:
1478778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1479778fc546SJ. Bruce Fields 		/* fall through: */
1480778fc546SJ. Bruce Fields 	case F_RDLCK:
1481778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1482778fc546SJ. Bruce Fields 	}
1483778fc546SJ. Bruce Fields }
1484778fc546SJ. Bruce Fields 
14851da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
14867448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	if (error)
14911da177e4SLinus Torvalds 		return error;
1492778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
14931da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
14943b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
14953b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
14963b6e2723SFilipe Brandenburger 
14973b6e2723SFilipe Brandenburger 		f_delown(filp);
14983b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
149996d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
150096d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
150196d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
150296d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
150396d6d59cSJ. Bruce Fields 		}
1504e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, dispose);
15053b6e2723SFilipe Brandenburger 	}
15061da177e4SLinus Torvalds 	return 0;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
15091da177e4SLinus Torvalds 
1510778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1511778fc546SJ. Bruce Fields {
1512778fc546SJ. Bruce Fields 	if (!then)
1513778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1514778fc546SJ. Bruce Fields 		return false;
1515778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1516778fc546SJ. Bruce Fields }
1517778fc546SJ. Bruce Fields 
1518c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
15191da177e4SLinus Torvalds {
15208634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
15218634b51fSJeff Layton 	struct file_lock *fl, *tmp;
15221da177e4SLinus Torvalds 
15236109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1524f82b4b67SJeff Layton 
15258634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
152662af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1527778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
15287448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1529778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
15307448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
1534df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1535df4e8d2cSJ. Bruce Fields {
153611afe9f7SChristoph Hellwig 	if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
153711afe9f7SChristoph Hellwig 		return false;
1538df4e8d2cSJ. Bruce Fields 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1539df4e8d2cSJ. Bruce Fields 		return false;
1540df4e8d2cSJ. Bruce Fields 	return locks_conflict(breaker, lease);
1541df4e8d2cSJ. Bruce Fields }
1542df4e8d2cSJ. Bruce Fields 
154303d12ddfSJeff Layton static bool
154403d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker)
154503d12ddfSJeff Layton {
15468634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
154703d12ddfSJeff Layton 	struct file_lock *fl;
154803d12ddfSJeff Layton 
15496109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
155003d12ddfSJeff Layton 
15518634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
155203d12ddfSJeff Layton 		if (leases_conflict(fl, breaker))
155303d12ddfSJeff Layton 			return true;
155403d12ddfSJeff Layton 	}
155503d12ddfSJeff Layton 	return false;
155603d12ddfSJeff Layton }
155703d12ddfSJeff Layton 
15581da177e4SLinus Torvalds /**
15591da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
15601da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1561df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1562df4e8d2cSJ. Bruce Fields  *	    break all leases
1563df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1564df4e8d2cSJ. Bruce Fields  *	    only delegations
15651da177e4SLinus Torvalds  *
156687250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
156787250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
156887250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
15691da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
15701da177e4SLinus Torvalds  */
1571df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
15721da177e4SLinus Torvalds {
1573778fc546SJ. Bruce Fields 	int error = 0;
1574128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1575a901125cSYan, Zheng 	struct file_lock *new_fl, *fl, *tmp;
15761da177e4SLinus Torvalds 	unsigned long break_time;
15778737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1578c45198edSJeff Layton 	LIST_HEAD(dispose);
15791da177e4SLinus Torvalds 
15808737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
15816d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
15826d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1583df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
15841da177e4SLinus Torvalds 
15858634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
1586128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
15878634b51fSJeff Layton 	if (!ctx) {
15888634b51fSJeff Layton 		WARN_ON_ONCE(1);
15898634b51fSJeff Layton 		return error;
15908634b51fSJeff Layton 	}
15918634b51fSJeff Layton 
159202e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
15936109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
15941da177e4SLinus Torvalds 
1595c45198edSJeff Layton 	time_out_leases(inode, &dispose);
15961da177e4SLinus Torvalds 
159703d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1598df4e8d2cSJ. Bruce Fields 		goto out;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	break_time = 0;
16011da177e4SLinus Torvalds 	if (lease_break_time > 0) {
16021da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
16031da177e4SLinus Torvalds 		if (break_time == 0)
16041da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 
1607a901125cSYan, Zheng 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1608df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1609df4e8d2cSJ. Bruce Fields 			continue;
1610778fc546SJ. Bruce Fields 		if (want_write) {
1611778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1612778fc546SJ. Bruce Fields 				continue;
1613778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
16141da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1615778fc546SJ. Bruce Fields 		} else {
16168634b51fSJeff Layton 			if (lease_breaking(fl))
1617778fc546SJ. Bruce Fields 				continue;
1618778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1619778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
16201da177e4SLinus Torvalds 		}
16214d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
1622e084c1bdSJeff Layton 			locks_delete_lock_ctx(fl, &dispose);
16231da177e4SLinus Torvalds 	}
16241da177e4SLinus Torvalds 
16258634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
16264d01b7f5SJeff Layton 		goto out;
16274d01b7f5SJeff Layton 
1628843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
162962af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
16301da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
16311da177e4SLinus Torvalds 		goto out;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds restart:
16358634b51fSJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
16368634b51fSJeff Layton 	break_time = fl->fl_break_time;
1637f1c6bb2cSJeff Layton 	if (break_time != 0)
16381da177e4SLinus Torvalds 		break_time -= jiffies;
16391da177e4SLinus Torvalds 	if (break_time == 0)
16401da177e4SLinus Torvalds 		break_time++;
1641fd7732e0SNeilBrown 	locks_insert_block(fl, new_fl, leases_conflict);
164262af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
16436109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
164402e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1645aba37660SPeter Zijlstra 
1646c45198edSJeff Layton 	locks_dispose_list(&dispose);
16474321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
1648ada5c1daSNeilBrown 						!new_fl->fl_blocker, break_time);
1649aba37660SPeter Zijlstra 
165002e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
16516109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
165262af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
16531c8c601aSJeff Layton 	locks_delete_block(new_fl);
16541da177e4SLinus Torvalds 	if (error >= 0) {
1655778fc546SJ. Bruce Fields 		/*
1656778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1657778fc546SJ. Bruce Fields 		 * broken yet
1658778fc546SJ. Bruce Fields 		 */
165903d12ddfSJeff Layton 		if (error == 0)
166003d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
166103d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
16621da177e4SLinus Torvalds 			goto restart;
16631da177e4SLinus Torvalds 		error = 0;
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds out:
16666109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
166702e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1668c45198edSJeff Layton 	locks_dispose_list(&dispose);
16691da177e4SLinus Torvalds 	locks_free_lock(new_fl);
16701da177e4SLinus Torvalds 	return error;
16711da177e4SLinus Torvalds }
16721da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds /**
167576c47948SAmir Goldstein  *	lease_get_mtime - update modified time of an inode with exclusive lease
16761da177e4SLinus Torvalds  *	@inode: the inode
167776c47948SAmir Goldstein  *      @time:  pointer to a timespec which contains the last modified time
16781da177e4SLinus Torvalds  *
16791da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
16801da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1681a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
16821da177e4SLinus Torvalds  */
168395582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time)
16841da177e4SLinus Torvalds {
1685bfe86024SJeff Layton 	bool has_lease = false;
1686128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
16878634b51fSJeff Layton 	struct file_lock *fl;
1688bfe86024SJeff Layton 
1689128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
16908634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
16916109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
16928ace5dfbSGeliang Tang 		fl = list_first_entry_or_null(&ctx->flc_lease,
16938634b51fSJeff Layton 					      struct file_lock, fl_list);
16948ace5dfbSGeliang Tang 		if (fl && (fl->fl_type == F_WRLCK))
1695bfe86024SJeff Layton 			has_lease = true;
16966109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1697bfe86024SJeff Layton 	}
1698bfe86024SJeff Layton 
1699bfe86024SJeff Layton 	if (has_lease)
1700c2050a45SDeepa Dinamani 		*time = current_time(inode);
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)) {
173702e525b2SPeter Zijlstra 		percpu_down_read(&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);
174702e525b2SPeter Zijlstra 		percpu_up_read(&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 
1774052b8cfaSNikolay Borisov 	if ((arg == F_RDLCK) && inode_is_open_for_write(inode))
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 
182102e525b2SPeter Zijlstra 	percpu_down_read(&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);
189202e525b2SPeter Zijlstra 	percpu_up_read(&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 
191502e525b2SPeter Zijlstra 	percpu_down_read(&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);
192802e525b2SPeter Zijlstra 	percpu_up_read(&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 EXPORT_SYMBOL(locks_remove_posix);
26181da177e4SLinus Torvalds 
26193d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
2620dd459bb1SJeff Layton static void
2621128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2622dd459bb1SJeff Layton {
2623d6367d62SNeilBrown 	struct file_lock fl;
2624c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
2625dd459bb1SJeff Layton 
26263d8e560dSJeff Layton 	if (list_empty(&flctx->flc_flock))
2627dd459bb1SJeff Layton 		return;
2628dd459bb1SJeff Layton 
2629d6367d62SNeilBrown 	flock_make_lock(filp, LOCK_UN, &fl);
2630d6367d62SNeilBrown 	fl.fl_flags |= FL_CLOSE;
2631d6367d62SNeilBrown 
2632de2a4a50SMiklos Szeredi 	if (filp->f_op->flock)
2633dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2634dd459bb1SJeff Layton 	else
2635bcd7f78dSJeff Layton 		flock_lock_inode(inode, &fl);
2636dd459bb1SJeff Layton 
2637dd459bb1SJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
2638dd459bb1SJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2639dd459bb1SJeff Layton }
2640dd459bb1SJeff Layton 
26413d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
26428634b51fSJeff Layton static void
2643128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
26448634b51fSJeff Layton {
26458634b51fSJeff Layton 	struct file_lock *fl, *tmp;
26468634b51fSJeff Layton 	LIST_HEAD(dispose);
26478634b51fSJeff Layton 
26483d8e560dSJeff Layton 	if (list_empty(&ctx->flc_lease))
26498634b51fSJeff Layton 		return;
26508634b51fSJeff Layton 
265102e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
26526109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
26538634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2654c4e136cdSJeff Layton 		if (filp == fl->fl_file)
26557448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, &dispose);
26566109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
265702e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
26585f43086bSPeter Zijlstra 
26598634b51fSJeff Layton 	locks_dispose_list(&dispose);
26608634b51fSJeff Layton }
26618634b51fSJeff Layton 
26621da177e4SLinus Torvalds /*
26631da177e4SLinus Torvalds  * This function is called on the last close of an open file.
26641da177e4SLinus Torvalds  */
266578ed8a13SJeff Layton void locks_remove_file(struct file *filp)
26661da177e4SLinus Torvalds {
2667128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2668128a3785SDmitry Vyukov 
2669c568d683SMiklos Szeredi 	ctx = smp_load_acquire(&locks_inode(filp)->i_flctx);
2670128a3785SDmitry Vyukov 	if (!ctx)
26713d8e560dSJeff Layton 		return;
26723d8e560dSJeff Layton 
2673dd459bb1SJeff Layton 	/* remove any OFD locks */
267473a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
26755d50ffd7SJeff Layton 
2676dd459bb1SJeff Layton 	/* remove flock locks */
2677128a3785SDmitry Vyukov 	locks_remove_flock(filp, ctx);
2678dd459bb1SJeff Layton 
26798634b51fSJeff Layton 	/* remove any leases */
2680128a3785SDmitry Vyukov 	locks_remove_lease(filp, ctx);
26813953704fSBenjamin Coddington 
26823953704fSBenjamin Coddington 	spin_lock(&ctx->flc_lock);
26833953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
26843953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
26853953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
26863953704fSBenjamin Coddington 	spin_unlock(&ctx->flc_lock);
26871da177e4SLinus Torvalds }
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds /**
26909b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
26919b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
26929b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
26939b9d2ab4SMarc Eshel  *
26949b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
26959b9d2ab4SMarc Eshel  */
26969b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
26979b9d2ab4SMarc Eshel {
2698de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
26999b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
27009b9d2ab4SMarc Eshel 	return 0;
27019b9d2ab4SMarc Eshel }
27029b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
27039b9d2ab4SMarc Eshel 
27047f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2705d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
27067f8ada98SPavel Emelyanov #include <linux/seq_file.h>
27077f8ada98SPavel Emelyanov 
27087012b02aSJeff Layton struct locks_iterator {
27097012b02aSJeff Layton 	int	li_cpu;
27107012b02aSJeff Layton 	loff_t	li_pos;
27117012b02aSJeff Layton };
27127012b02aSJeff Layton 
27137f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
271499dc8292SJerome Marchand 			    loff_t id, char *pfx)
27151da177e4SLinus Torvalds {
27161da177e4SLinus Torvalds 	struct inode *inode = NULL;
2717ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
2718d67fd44fSNikolay Borisov 	struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
2719d67fd44fSNikolay Borisov 
27209d5b86acSBenjamin Coddington 	fl_pid = locks_translate_pid(fl, proc_pidns);
2721d67fd44fSNikolay Borisov 	/*
27221cf8e5deSKonstantin Khorenko 	 * If lock owner is dead (and pid is freed) or not visible in current
27231cf8e5deSKonstantin Khorenko 	 * pidns, zero is shown as a pid value. Check lock info from
27241cf8e5deSKonstantin Khorenko 	 * init_pid_ns to get saved lock pid value.
2725d67fd44fSNikolay Borisov 	 */
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2728c568d683SMiklos Szeredi 		inode = locks_inode(fl->fl_file);
27291da177e4SLinus Torvalds 
273099dc8292SJerome Marchand 	seq_printf(f, "%lld:%s ", id, pfx);
27311da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2732c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
27335315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2734cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
27355315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2736c918d42aSJeff Layton 		else
27375315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2738c918d42aSJeff Layton 
2739c918d42aSJeff Layton 		seq_printf(f, " %s ",
27401da177e4SLinus Torvalds 			     (inode == NULL) ? "*NOINODE*" :
2741a16877caSPavel Emelyanov 			     mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
27421da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
27431da177e4SLinus Torvalds 		if (fl->fl_type & LOCK_MAND) {
27445315c26aSFabian Frederick 			seq_puts(f, "FLOCK  MSNFS     ");
27451da177e4SLinus Torvalds 		} else {
27465315c26aSFabian Frederick 			seq_puts(f, "FLOCK  ADVISORY  ");
27471da177e4SLinus Torvalds 		}
27481da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
27498144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
27508144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
27518144f1f6SJeff Layton 		else
27525315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
27538144f1f6SJeff Layton 
2754ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
27555315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
27561da177e4SLinus Torvalds 		else if (fl->fl_file)
27575315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
27581da177e4SLinus Torvalds 		else
27595315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
27601da177e4SLinus Torvalds 	} else {
27615315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
27621da177e4SLinus Torvalds 	}
27631da177e4SLinus Torvalds 	if (fl->fl_type & LOCK_MAND) {
27647f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
27651da177e4SLinus Torvalds 			       (fl->fl_type & LOCK_READ)
27661da177e4SLinus Torvalds 			       ? (fl->fl_type & LOCK_WRITE) ? "RW   " : "READ "
27671da177e4SLinus Torvalds 			       : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
27681da177e4SLinus Torvalds 	} else {
27697f8ada98SPavel Emelyanov 		seq_printf(f, "%s ",
2770ab83fa4bSJ. Bruce Fields 			       (lease_breaking(fl))
27710ee5c6d6SJeff Layton 			       ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
27720ee5c6d6SJeff Layton 			       : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
27731da177e4SLinus Torvalds 	}
27741da177e4SLinus Torvalds 	if (inode) {
27753648888eSJeff Layton 		/* userspace relies on this representation of dev_t */
2776ab1f1611SVitaliy Gusev 		seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
27771da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
27781da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
27791da177e4SLinus Torvalds 	} else {
2780ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
27811da177e4SLinus Torvalds 	}
27821da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
27831da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
27847f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
27851da177e4SLinus Torvalds 		else
27867f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
27871da177e4SLinus Torvalds 	} else {
27885315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
27891da177e4SLinus Torvalds 	}
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
27927f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
27931da177e4SLinus Torvalds {
27947012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
27957f8ada98SPavel Emelyanov 	struct file_lock *fl, *bfl;
2796d67fd44fSNikolay Borisov 	struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
27977f8ada98SPavel Emelyanov 
2798139ca04eSJeff Layton 	fl = hlist_entry(v, struct file_lock, fl_link);
27997f8ada98SPavel Emelyanov 
28009d5b86acSBenjamin Coddington 	if (locks_translate_pid(fl, proc_pidns) == 0)
2801d67fd44fSNikolay Borisov 		return 0;
2802d67fd44fSNikolay Borisov 
28037012b02aSJeff Layton 	lock_get_status(f, fl, iter->li_pos, "");
28047f8ada98SPavel Emelyanov 
2805ada5c1daSNeilBrown 	list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member)
28067012b02aSJeff Layton 		lock_get_status(f, bfl, iter->li_pos, " ->");
28077f8ada98SPavel Emelyanov 
28087f8ada98SPavel Emelyanov 	return 0;
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28116c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f,
28126c8c9031SAndrey Vagin 			struct list_head *head, int *id,
28136c8c9031SAndrey Vagin 			struct file *filp, struct files_struct *files)
28146c8c9031SAndrey Vagin {
28156c8c9031SAndrey Vagin 	struct file_lock *fl;
28166c8c9031SAndrey Vagin 
28176c8c9031SAndrey Vagin 	list_for_each_entry(fl, head, fl_list) {
28186c8c9031SAndrey Vagin 
28196c8c9031SAndrey Vagin 		if (filp != fl->fl_file)
28206c8c9031SAndrey Vagin 			continue;
28216c8c9031SAndrey Vagin 		if (fl->fl_owner != files &&
28226c8c9031SAndrey Vagin 		    fl->fl_owner != filp)
28236c8c9031SAndrey Vagin 			continue;
28246c8c9031SAndrey Vagin 
28256c8c9031SAndrey Vagin 		(*id)++;
28266c8c9031SAndrey Vagin 		seq_puts(f, "lock:\t");
28276c8c9031SAndrey Vagin 		lock_get_status(f, fl, *id, "");
28286c8c9031SAndrey Vagin 	}
28296c8c9031SAndrey Vagin }
28306c8c9031SAndrey Vagin 
28316c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f,
28326c8c9031SAndrey Vagin 		  struct file *filp, struct files_struct *files)
28336c8c9031SAndrey Vagin {
2834c568d683SMiklos Szeredi 	struct inode *inode = locks_inode(filp);
28356c8c9031SAndrey Vagin 	struct file_lock_context *ctx;
28366c8c9031SAndrey Vagin 	int id = 0;
28376c8c9031SAndrey Vagin 
2838128a3785SDmitry Vyukov 	ctx = smp_load_acquire(&inode->i_flctx);
28396c8c9031SAndrey Vagin 	if (!ctx)
28406c8c9031SAndrey Vagin 		return;
28416c8c9031SAndrey Vagin 
28426c8c9031SAndrey Vagin 	spin_lock(&ctx->flc_lock);
28436c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
28446c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
28456c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
28466c8c9031SAndrey Vagin 	spin_unlock(&ctx->flc_lock);
28476c8c9031SAndrey Vagin }
28486c8c9031SAndrey Vagin 
28497f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2850b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
28511da177e4SLinus Torvalds {
28527012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
285399dc8292SJerome Marchand 
28547012b02aSJeff Layton 	iter->li_pos = *pos + 1;
2855aba37660SPeter Zijlstra 	percpu_down_write(&file_rwsem);
28567b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
28577c3f654dSPeter Zijlstra 	return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
28581da177e4SLinus Torvalds }
28597f8ada98SPavel Emelyanov 
28607f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
28617f8ada98SPavel Emelyanov {
28627012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
28637012b02aSJeff Layton 
28647012b02aSJeff Layton 	++iter->li_pos;
28657c3f654dSPeter Zijlstra 	return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
28661da177e4SLinus Torvalds }
28677f8ada98SPavel Emelyanov 
28687f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2869b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
28707f8ada98SPavel Emelyanov {
28717b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
2872aba37660SPeter Zijlstra 	percpu_up_write(&file_rwsem);
28731da177e4SLinus Torvalds }
28741da177e4SLinus Torvalds 
2875d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
28767f8ada98SPavel Emelyanov 	.start	= locks_start,
28777f8ada98SPavel Emelyanov 	.next	= locks_next,
28787f8ada98SPavel Emelyanov 	.stop	= locks_stop,
28797f8ada98SPavel Emelyanov 	.show	= locks_show,
28807f8ada98SPavel Emelyanov };
2881d8ba7a36SAlexey Dobriyan 
2882d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2883d8ba7a36SAlexey Dobriyan {
288444414d82SChristoph Hellwig 	proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
288544414d82SChristoph Hellwig 			sizeof(struct locks_iterator), NULL);
2886d8ba7a36SAlexey Dobriyan 	return 0;
2887d8ba7a36SAlexey Dobriyan }
288891899226SPaul Gortmaker fs_initcall(proc_locks_init);
28897f8ada98SPavel Emelyanov #endif
28907f8ada98SPavel Emelyanov 
28911da177e4SLinus Torvalds static int __init filelock_init(void)
28921da177e4SLinus Torvalds {
28937012b02aSJeff Layton 	int i;
28947012b02aSJeff Layton 
28954a075e39SJeff Layton 	flctx_cache = kmem_cache_create("file_lock_ctx",
28964a075e39SJeff Layton 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
28974a075e39SJeff Layton 
28981da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
2899ee19cc40SMiklos Szeredi 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2900ee19cc40SMiklos Szeredi 
29017c3f654dSPeter Zijlstra 	for_each_possible_cpu(i) {
29027c3f654dSPeter Zijlstra 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
29037c3f654dSPeter Zijlstra 
29047c3f654dSPeter Zijlstra 		spin_lock_init(&fll->lock);
29057c3f654dSPeter Zijlstra 		INIT_HLIST_HEAD(&fll->hlist);
29067c3f654dSPeter Zijlstra 	}
29077012b02aSJeff Layton 
29081da177e4SLinus Torvalds 	return 0;
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds core_initcall(filelock_init);
2911