xref: /linux/fs/locks.c (revision 42b16d3ac371a2fac9b6f08fd75f23f34ba3955a)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/locks.c
41da177e4SLinus Torvalds  *
5e9728cc7SJ. Bruce Fields  * We implement four types of file locks: BSD locks, posix locks, open
6e9728cc7SJ. Bruce Fields  * file description locks, and leases.  For details about BSD locks,
7e9728cc7SJ. Bruce Fields  * see the flock(2) man page; for details about the other three, see
8e9728cc7SJ. Bruce Fields  * fcntl(2).
91da177e4SLinus Torvalds  *
10fd7732e0SNeilBrown  *
11fd7732e0SNeilBrown  * Locking conflicts and dependencies:
12fd7732e0SNeilBrown  * If multiple threads attempt to lock the same byte (or flock the same file)
13fd7732e0SNeilBrown  * only one can be granted the lock, and other must wait their turn.
14fd7732e0SNeilBrown  * The first lock has been "applied" or "granted", the others are "waiting"
15fd7732e0SNeilBrown  * and are "blocked" by the "applied" lock..
16fd7732e0SNeilBrown  *
17fd7732e0SNeilBrown  * Waiting and applied locks are all kept in trees whose properties are:
18fd7732e0SNeilBrown  *
19fd7732e0SNeilBrown  *	- the root of a tree may be an applied or waiting lock.
20fd7732e0SNeilBrown  *	- every other node in the tree is a waiting lock that
21fd7732e0SNeilBrown  *	  conflicts with every ancestor of that node.
22fd7732e0SNeilBrown  *
23fd7732e0SNeilBrown  * Every such tree begins life as a waiting singleton which obviously
24fd7732e0SNeilBrown  * satisfies the above properties.
25fd7732e0SNeilBrown  *
26fd7732e0SNeilBrown  * The only ways we modify trees preserve these properties:
27fd7732e0SNeilBrown  *
28fd7732e0SNeilBrown  *	1. We may add a new leaf node, but only after first verifying that it
29fd7732e0SNeilBrown  *	   conflicts with all of its ancestors.
30fd7732e0SNeilBrown  *	2. We may remove the root of a tree, creating a new singleton
31fd7732e0SNeilBrown  *	   tree from the root and N new trees rooted in the immediate
32fd7732e0SNeilBrown  *	   children.
33fd7732e0SNeilBrown  *	3. If the root of a tree is not currently an applied lock, we may
34fd7732e0SNeilBrown  *	   apply it (if possible).
35fd7732e0SNeilBrown  *	4. We may upgrade the root of the tree (either extend its range,
36fd7732e0SNeilBrown  *	   or upgrade its entire range from read to write).
37fd7732e0SNeilBrown  *
38fd7732e0SNeilBrown  * When an applied lock is modified in a way that reduces or downgrades any
39fd7732e0SNeilBrown  * part of its range, we remove all its children (2 above).  This particularly
40fd7732e0SNeilBrown  * happens when a lock is unlocked.
41fd7732e0SNeilBrown  *
42fd7732e0SNeilBrown  * For each of those child trees we "wake up" the thread which is
43fd7732e0SNeilBrown  * waiting for the lock so it can continue handling as follows: if the
44fd7732e0SNeilBrown  * root of the tree applies, we do so (3).  If it doesn't, it must
45fd7732e0SNeilBrown  * conflict with some applied lock.  We remove (wake up) all of its children
46fd7732e0SNeilBrown  * (2), and add it is a new leaf to the tree rooted in the applied
47fd7732e0SNeilBrown  * lock (1).  We then repeat the process recursively with those
48fd7732e0SNeilBrown  * children.
49fd7732e0SNeilBrown  *
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds #include <linux/capability.h>
521da177e4SLinus Torvalds #include <linux/file.h>
539f3acc31SAl Viro #include <linux/fdtable.h>
545970e15dSJeff Layton #include <linux/filelock.h>
551da177e4SLinus Torvalds #include <linux/fs.h>
561da177e4SLinus Torvalds #include <linux/init.h>
571da177e4SLinus Torvalds #include <linux/security.h>
581da177e4SLinus Torvalds #include <linux/slab.h>
591da177e4SLinus Torvalds #include <linux/syscalls.h>
601da177e4SLinus Torvalds #include <linux/time.h>
614fb3a538SDipankar Sarma #include <linux/rcupdate.h>
62ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
6348f74186SJeff Layton #include <linux/hashtable.h>
647012b02aSJeff Layton #include <linux/percpu.h>
65dd81faa8SLuis Chamberlain #include <linux/sysctl.h>
661da177e4SLinus Torvalds 
6762af4f1fSJeff Layton #define CREATE_TRACE_POINTS
6862af4f1fSJeff Layton #include <trace/events/filelock.h>
6962af4f1fSJeff Layton 
707c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
711da177e4SLinus Torvalds 
file_lock(struct file_lock_core * flc)721a62c22aSJeff Layton static struct file_lock *file_lock(struct file_lock_core *flc)
731a62c22aSJeff Layton {
741a62c22aSJeff Layton 	return container_of(flc, struct file_lock, c);
751a62c22aSJeff Layton }
761a62c22aSJeff Layton 
file_lease(struct file_lock_core * flc)77c69ff407SJeff Layton static struct file_lease *file_lease(struct file_lock_core *flc)
78c69ff407SJeff Layton {
79c69ff407SJeff Layton 	return container_of(flc, struct file_lease, c);
80c69ff407SJeff Layton }
81c69ff407SJeff Layton 
lease_breaking(struct file_lease * fl)82c69ff407SJeff Layton static bool lease_breaking(struct file_lease *fl)
83ab83fa4bSJ. Bruce Fields {
844ca52f53SJeff Layton 	return fl->c.flc_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
85778fc546SJ. Bruce Fields }
86778fc546SJ. Bruce Fields 
target_leasetype(struct file_lease * fl)87c69ff407SJeff Layton static int target_leasetype(struct file_lease *fl)
88778fc546SJ. Bruce Fields {
894ca52f53SJeff Layton 	if (fl->c.flc_flags & FL_UNLOCK_PENDING)
90778fc546SJ. Bruce Fields 		return F_UNLCK;
914ca52f53SJeff Layton 	if (fl->c.flc_flags & FL_DOWNGRADE_PENDING)
92778fc546SJ. Bruce Fields 		return F_RDLCK;
934ca52f53SJeff Layton 	return fl->c.flc_type;
94ab83fa4bSJ. Bruce Fields }
95ab83fa4bSJ. Bruce Fields 
96dd81faa8SLuis Chamberlain static int leases_enable = 1;
97dd81faa8SLuis Chamberlain static int lease_break_time = 45;
98dd81faa8SLuis Chamberlain 
99dd81faa8SLuis Chamberlain #ifdef CONFIG_SYSCTL
100dd81faa8SLuis Chamberlain static struct ctl_table locks_sysctls[] = {
101dd81faa8SLuis Chamberlain 	{
102dd81faa8SLuis Chamberlain 		.procname	= "leases-enable",
103dd81faa8SLuis Chamberlain 		.data		= &leases_enable,
104dd81faa8SLuis Chamberlain 		.maxlen		= sizeof(int),
105dd81faa8SLuis Chamberlain 		.mode		= 0644,
106dd81faa8SLuis Chamberlain 		.proc_handler	= proc_dointvec,
107dd81faa8SLuis Chamberlain 	},
108dd81faa8SLuis Chamberlain #ifdef CONFIG_MMU
109dd81faa8SLuis Chamberlain 	{
110dd81faa8SLuis Chamberlain 		.procname	= "lease-break-time",
111dd81faa8SLuis Chamberlain 		.data		= &lease_break_time,
112dd81faa8SLuis Chamberlain 		.maxlen		= sizeof(int),
113dd81faa8SLuis Chamberlain 		.mode		= 0644,
114dd81faa8SLuis Chamberlain 		.proc_handler	= proc_dointvec,
115dd81faa8SLuis Chamberlain 	},
116dd81faa8SLuis Chamberlain #endif /* CONFIG_MMU */
117dd81faa8SLuis Chamberlain };
118dd81faa8SLuis Chamberlain 
init_fs_locks_sysctls(void)119dd81faa8SLuis Chamberlain static int __init init_fs_locks_sysctls(void)
120dd81faa8SLuis Chamberlain {
121dd81faa8SLuis Chamberlain 	register_sysctl_init("fs", locks_sysctls);
122dd81faa8SLuis Chamberlain 	return 0;
123dd81faa8SLuis Chamberlain }
124dd81faa8SLuis Chamberlain early_initcall(init_fs_locks_sysctls);
125dd81faa8SLuis Chamberlain #endif /* CONFIG_SYSCTL */
1261da177e4SLinus Torvalds 
1271c8c601aSJeff Layton /*
1287012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1297c3f654dSPeter Zijlstra  * keep a list on each CPU, with each list protected by its own spinlock.
1307c3f654dSPeter Zijlstra  * Global serialization is done using file_rwsem.
1317c3f654dSPeter Zijlstra  *
1327c3f654dSPeter Zijlstra  * Note that alterations to the list also require that the relevant flc_lock is
1337c3f654dSPeter Zijlstra  * held.
1341c8c601aSJeff Layton  */
1357c3f654dSPeter Zijlstra struct file_lock_list_struct {
1367c3f654dSPeter Zijlstra 	spinlock_t		lock;
1377c3f654dSPeter Zijlstra 	struct hlist_head	hlist;
1387c3f654dSPeter Zijlstra };
1397c3f654dSPeter Zijlstra static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
140aba37660SPeter Zijlstra DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
14188974691SJeff Layton 
142eb82dd39SJeff Layton 
1431c8c601aSJeff Layton /*
14448f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1457b2296afSJeff Layton  * It is protected by blocked_lock_lock.
14648f74186SJeff Layton  *
14748f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
14848f74186SJeff Layton  * particular lockowner is waiting on.
14948f74186SJeff Layton  *
15048f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
15148f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
15248f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1531c8c601aSJeff Layton  */
15448f74186SJeff Layton #define BLOCKED_HASH_BITS	7
15548f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
15688974691SJeff Layton 
1571c8c601aSJeff Layton /*
1587b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1597b2296afSJeff Layton  * want to be holding this lock.
1601c8c601aSJeff Layton  *
161ada5c1daSNeilBrown  * In addition, it also protects the fl->fl_blocked_requests list, and the
162ada5c1daSNeilBrown  * fl->fl_blocker pointer for file_lock structures that are acting as lock
163ada5c1daSNeilBrown  * requests (in contrast to those that are acting as records of acquired locks).
1641c8c601aSJeff Layton  *
1651c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1666109c850SJeff Layton  * we often hold the flc_lock as well. In certain cases, when reading the fields
1671c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1686109c850SJeff Layton  * flc_lock.
1691c8c601aSJeff Layton  */
1707b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
1711da177e4SLinus Torvalds 
17268279f9cSAlexey Dobriyan static struct kmem_cache *flctx_cache __ro_after_init;
17368279f9cSAlexey Dobriyan static struct kmem_cache *filelock_cache __ro_after_init;
174c69ff407SJeff Layton static struct kmem_cache *filelease_cache __ro_after_init;
1751da177e4SLinus Torvalds 
1764a075e39SJeff Layton static struct file_lock_context *
locks_get_lock_context(struct inode * inode,int type)1775c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type)
1784a075e39SJeff Layton {
179128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1804a075e39SJeff Layton 
181128a3785SDmitry Vyukov 	/* paired with cmpxchg() below */
182401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
183128a3785SDmitry Vyukov 	if (likely(ctx) || type == F_UNLCK)
1844a075e39SJeff Layton 		goto out;
1854a075e39SJeff Layton 
186128a3785SDmitry Vyukov 	ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
187128a3785SDmitry Vyukov 	if (!ctx)
1884a075e39SJeff Layton 		goto out;
1894a075e39SJeff Layton 
190128a3785SDmitry Vyukov 	spin_lock_init(&ctx->flc_lock);
191128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_flock);
192128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_posix);
193128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_lease);
1944a075e39SJeff Layton 
1954a075e39SJeff Layton 	/*
1964a075e39SJeff Layton 	 * Assign the pointer if it's not already assigned. If it is, then
1974a075e39SJeff Layton 	 * free the context we just allocated.
1984a075e39SJeff Layton 	 */
199128a3785SDmitry Vyukov 	if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
200128a3785SDmitry Vyukov 		kmem_cache_free(flctx_cache, ctx);
201401a8b8fSJeff Layton 		ctx = locks_inode_context(inode);
202128a3785SDmitry Vyukov 	}
2034a075e39SJeff Layton out:
2041890910fSJeff Layton 	trace_locks_get_lock_context(inode, type, ctx);
205128a3785SDmitry Vyukov 	return ctx;
2064a075e39SJeff Layton }
2074a075e39SJeff Layton 
208e24dadabSJeff Layton static void
locks_dump_ctx_list(struct list_head * list,char * list_type)209e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type)
210e24dadabSJeff Layton {
211fde49518SJeff Layton 	struct file_lock_core *flc;
212e24dadabSJeff Layton 
213fde49518SJeff Layton 	list_for_each_entry(flc, list, flc_list)
214fde49518SJeff Layton 		pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
215fde49518SJeff Layton 			list_type, flc->flc_owner, flc->flc_flags,
216fde49518SJeff Layton 			flc->flc_type, flc->flc_pid);
217e24dadabSJeff Layton }
218e24dadabSJeff Layton 
219e24dadabSJeff Layton static void
locks_check_ctx_lists(struct inode * inode)220e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode)
221e24dadabSJeff Layton {
222e24dadabSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
223e24dadabSJeff Layton 
224e24dadabSJeff Layton 	if (unlikely(!list_empty(&ctx->flc_flock) ||
225e24dadabSJeff Layton 		     !list_empty(&ctx->flc_posix) ||
226e24dadabSJeff Layton 		     !list_empty(&ctx->flc_lease))) {
227e24dadabSJeff Layton 		pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
228e24dadabSJeff Layton 			MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
229e24dadabSJeff Layton 			inode->i_ino);
230e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
231e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
232e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
233e24dadabSJeff Layton 	}
234e24dadabSJeff Layton }
235e24dadabSJeff Layton 
2363953704fSBenjamin Coddington static void
locks_check_ctx_file_list(struct file * filp,struct list_head * list,char * list_type)237fde49518SJeff Layton locks_check_ctx_file_list(struct file *filp, struct list_head *list, char *list_type)
2383953704fSBenjamin Coddington {
239fde49518SJeff Layton 	struct file_lock_core *flc;
240c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
2413953704fSBenjamin Coddington 
242fde49518SJeff Layton 	list_for_each_entry(flc, list, flc_list)
243fde49518SJeff Layton 		if (flc->flc_file == filp)
2443953704fSBenjamin Coddington 			pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
2453953704fSBenjamin Coddington 				" fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
2463953704fSBenjamin Coddington 				list_type, MAJOR(inode->i_sb->s_dev),
2473953704fSBenjamin Coddington 				MINOR(inode->i_sb->s_dev), inode->i_ino,
248fde49518SJeff Layton 				flc->flc_owner, flc->flc_flags,
249fde49518SJeff Layton 				flc->flc_type, flc->flc_pid);
2503953704fSBenjamin Coddington }
2513953704fSBenjamin Coddington 
2524a075e39SJeff Layton void
locks_free_lock_context(struct inode * inode)253f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode)
2544a075e39SJeff Layton {
255401a8b8fSJeff Layton 	struct file_lock_context *ctx = locks_inode_context(inode);
256f27a0fe0SJeff Layton 
257e24dadabSJeff Layton 	if (unlikely(ctx)) {
258e24dadabSJeff Layton 		locks_check_ctx_lists(inode);
2594a075e39SJeff Layton 		kmem_cache_free(flctx_cache, ctx);
2604a075e39SJeff Layton 	}
2614a075e39SJeff Layton }
2624a075e39SJeff Layton 
locks_init_lock_heads(struct file_lock_core * flc)2634ca52f53SJeff Layton static void locks_init_lock_heads(struct file_lock_core *flc)
264a51cb91dSMiklos Szeredi {
2654ca52f53SJeff Layton 	INIT_HLIST_NODE(&flc->flc_link);
2664ca52f53SJeff Layton 	INIT_LIST_HEAD(&flc->flc_list);
2674ca52f53SJeff Layton 	INIT_LIST_HEAD(&flc->flc_blocked_requests);
2684ca52f53SJeff Layton 	INIT_LIST_HEAD(&flc->flc_blocked_member);
2694ca52f53SJeff Layton 	init_waitqueue_head(&flc->flc_wait);
270a51cb91dSMiklos Szeredi }
271a51cb91dSMiklos Szeredi 
2721da177e4SLinus Torvalds /* Allocate an empty lock structure. */
locks_alloc_lock(void)273c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2741da177e4SLinus Torvalds {
275ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
276a51cb91dSMiklos Szeredi 
277a51cb91dSMiklos Szeredi 	if (fl)
2784ca52f53SJeff Layton 		locks_init_lock_heads(&fl->c);
279a51cb91dSMiklos Szeredi 
280a51cb91dSMiklos Szeredi 	return fl;
2811da177e4SLinus Torvalds }
282c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2831da177e4SLinus Torvalds 
284c69ff407SJeff Layton /* Allocate an empty lock structure. */
locks_alloc_lease(void)285c69ff407SJeff Layton struct file_lease *locks_alloc_lease(void)
286c69ff407SJeff Layton {
287c69ff407SJeff Layton 	struct file_lease *fl = kmem_cache_zalloc(filelease_cache, GFP_KERNEL);
288c69ff407SJeff Layton 
289c69ff407SJeff Layton 	if (fl)
290c69ff407SJeff Layton 		locks_init_lock_heads(&fl->c);
291c69ff407SJeff Layton 
292c69ff407SJeff Layton 	return fl;
293c69ff407SJeff Layton }
294c69ff407SJeff Layton EXPORT_SYMBOL_GPL(locks_alloc_lease);
295c69ff407SJeff Layton 
locks_release_private(struct file_lock * fl)296a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
29747831f35STrond Myklebust {
298fde49518SJeff Layton 	struct file_lock_core *flc = &fl->c;
299fde49518SJeff Layton 
300fde49518SJeff Layton 	BUG_ON(waitqueue_active(&flc->flc_wait));
301fde49518SJeff Layton 	BUG_ON(!list_empty(&flc->flc_list));
302fde49518SJeff Layton 	BUG_ON(!list_empty(&flc->flc_blocked_requests));
303fde49518SJeff Layton 	BUG_ON(!list_empty(&flc->flc_blocked_member));
304fde49518SJeff Layton 	BUG_ON(!hlist_unhashed(&flc->flc_link));
3055926459eSNeilBrown 
30647831f35STrond Myklebust 	if (fl->fl_ops) {
30747831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
30847831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
30947831f35STrond Myklebust 		fl->fl_ops = NULL;
31047831f35STrond Myklebust 	}
31147831f35STrond Myklebust 
3125c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
313cae80b30SJeff Layton 		if (fl->fl_lmops->lm_put_owner) {
314fde49518SJeff Layton 			fl->fl_lmops->lm_put_owner(flc->flc_owner);
315fde49518SJeff Layton 			flc->flc_owner = NULL;
316cae80b30SJeff Layton 		}
3175c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
3185c97d7b1SKinglong Mee 	}
31947831f35STrond Myklebust }
320a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
32147831f35STrond Myklebust 
322591502c5SDai Ngo /**
323591502c5SDai Ngo  * locks_owner_has_blockers - Check for blocking lock requests
324591502c5SDai Ngo  * @flctx: file lock context
325591502c5SDai Ngo  * @owner: lock owner
326591502c5SDai Ngo  *
327591502c5SDai Ngo  * Return values:
328591502c5SDai Ngo  *   %true: @owner has at least one blocker
329591502c5SDai Ngo  *   %false: @owner has no blockers
330591502c5SDai Ngo  */
locks_owner_has_blockers(struct file_lock_context * flctx,fl_owner_t owner)331fde49518SJeff Layton bool locks_owner_has_blockers(struct file_lock_context *flctx, fl_owner_t owner)
332591502c5SDai Ngo {
333fde49518SJeff Layton 	struct file_lock_core *flc;
334591502c5SDai Ngo 
335591502c5SDai Ngo 	spin_lock(&flctx->flc_lock);
336fde49518SJeff Layton 	list_for_each_entry(flc, &flctx->flc_posix, flc_list) {
337fde49518SJeff Layton 		if (flc->flc_owner != owner)
338591502c5SDai Ngo 			continue;
339fde49518SJeff Layton 		if (!list_empty(&flc->flc_blocked_requests)) {
340591502c5SDai Ngo 			spin_unlock(&flctx->flc_lock);
341591502c5SDai Ngo 			return true;
342591502c5SDai Ngo 		}
343591502c5SDai Ngo 	}
344591502c5SDai Ngo 	spin_unlock(&flctx->flc_lock);
345591502c5SDai Ngo 	return false;
346591502c5SDai Ngo }
347591502c5SDai Ngo EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
348591502c5SDai Ngo 
3491da177e4SLinus Torvalds /* Free a lock which is not in use. */
locks_free_lock(struct file_lock * fl)35005fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
3511da177e4SLinus Torvalds {
35247831f35STrond Myklebust 	locks_release_private(fl);
3531da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
3541da177e4SLinus Torvalds }
35505fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
3561da177e4SLinus Torvalds 
357c69ff407SJeff Layton /* Free a lease which is not in use. */
locks_free_lease(struct file_lease * fl)358c69ff407SJeff Layton void locks_free_lease(struct file_lease *fl)
359c69ff407SJeff Layton {
360c69ff407SJeff Layton 	kmem_cache_free(filelease_cache, fl);
361c69ff407SJeff Layton }
362c69ff407SJeff Layton EXPORT_SYMBOL(locks_free_lease);
363c69ff407SJeff Layton 
364ed9814d8SJeff Layton static void
locks_dispose_list(struct list_head * dispose)365ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
366ed9814d8SJeff Layton {
367c69ff407SJeff Layton 	struct file_lock_core *flc;
368ed9814d8SJeff Layton 
369ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
370c69ff407SJeff Layton 		flc = list_first_entry(dispose, struct file_lock_core, flc_list);
371c69ff407SJeff Layton 		list_del_init(&flc->flc_list);
372c69ff407SJeff Layton 		if (flc->flc_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
373c69ff407SJeff Layton 			locks_free_lease(file_lease(flc));
374c69ff407SJeff Layton 		else
375c69ff407SJeff Layton 			locks_free_lock(file_lock(flc));
376ed9814d8SJeff Layton 	}
377ed9814d8SJeff Layton }
378ed9814d8SJeff Layton 
locks_init_lock(struct file_lock * fl)3791da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
3801da177e4SLinus Torvalds {
381ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
3824ca52f53SJeff Layton 	locks_init_lock_heads(&fl->c);
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
3851da177e4SLinus Torvalds 
locks_init_lease(struct file_lease * fl)386c69ff407SJeff Layton void locks_init_lease(struct file_lease *fl)
387c69ff407SJeff Layton {
388c69ff407SJeff Layton 	memset(fl, 0, sizeof(*fl));
389c69ff407SJeff Layton 	locks_init_lock_heads(&fl->c);
390c69ff407SJeff Layton }
391c69ff407SJeff Layton EXPORT_SYMBOL(locks_init_lease);
392c69ff407SJeff Layton 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
3951da177e4SLinus Torvalds  */
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)3963fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
3971da177e4SLinus Torvalds {
3984ca52f53SJeff Layton 	new->c.flc_owner = fl->c.flc_owner;
3994ca52f53SJeff Layton 	new->c.flc_pid = fl->c.flc_pid;
4004ca52f53SJeff Layton 	new->c.flc_file = NULL;
4014ca52f53SJeff Layton 	new->c.flc_flags = fl->c.flc_flags;
4024ca52f53SJeff Layton 	new->c.flc_type = fl->c.flc_type;
4031da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
4041da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
405f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
4060996905fSTrond Myklebust 	new->fl_ops = NULL;
407f328296eSKinglong Mee 
408f328296eSKinglong Mee 	if (fl->fl_lmops) {
409f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
4104ca52f53SJeff Layton 			fl->fl_lmops->lm_get_owner(fl->c.flc_owner);
411f328296eSKinglong Mee 	}
4120996905fSTrond Myklebust }
4133fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
4140996905fSTrond Myklebust 
locks_copy_lock(struct file_lock * new,struct file_lock * fl)4150996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4160996905fSTrond Myklebust {
417566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
418566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
4190996905fSTrond Myklebust 
4203fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
421f328296eSKinglong Mee 
4224ca52f53SJeff Layton 	new->c.flc_file = fl->c.flc_file;
4231da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
42447831f35STrond Myklebust 
425f328296eSKinglong Mee 	if (fl->fl_ops) {
426f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
427f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
428f328296eSKinglong Mee 	}
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
4311da177e4SLinus Torvalds 
locks_move_blocks(struct file_lock * new,struct file_lock * fl)4325946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
4335946c431SNeilBrown {
4345946c431SNeilBrown 	struct file_lock *f;
4355946c431SNeilBrown 
4365946c431SNeilBrown 	/*
4375946c431SNeilBrown 	 * As ctx->flc_lock is held, new requests cannot be added to
438b6aaba5bSJeff Layton 	 * ->flc_blocked_requests, so we don't need a lock to check if it
4395946c431SNeilBrown 	 * is empty.
4405946c431SNeilBrown 	 */
4414ca52f53SJeff Layton 	if (list_empty(&fl->c.flc_blocked_requests))
4425946c431SNeilBrown 		return;
4435946c431SNeilBrown 	spin_lock(&blocked_lock_lock);
4444ca52f53SJeff Layton 	list_splice_init(&fl->c.flc_blocked_requests,
4454ca52f53SJeff Layton 			 &new->c.flc_blocked_requests);
4464ca52f53SJeff Layton 	list_for_each_entry(f, &new->c.flc_blocked_requests,
4474ca52f53SJeff Layton 			    c.flc_blocked_member)
448b6aaba5bSJeff Layton 		f->c.flc_blocker = &new->c;
4495946c431SNeilBrown 	spin_unlock(&blocked_lock_lock);
4505946c431SNeilBrown }
4515946c431SNeilBrown 
flock_translate_cmd(int cmd)4521da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
4531da177e4SLinus Torvalds 	switch (cmd) {
4541da177e4SLinus Torvalds 	case LOCK_SH:
4551da177e4SLinus Torvalds 		return F_RDLCK;
4561da177e4SLinus Torvalds 	case LOCK_EX:
4571da177e4SLinus Torvalds 		return F_WRLCK;
4581da177e4SLinus Torvalds 	case LOCK_UN:
4591da177e4SLinus Torvalds 		return F_UNLCK;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 	return -EINVAL;
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
flock_make_lock(struct file * filp,struct file_lock * fl,int type)4654149be7bSKuniyuki Iwashima static void flock_make_lock(struct file *filp, struct file_lock *fl, int type)
4661da177e4SLinus Torvalds {
467d6367d62SNeilBrown 	locks_init_lock(fl);
4681da177e4SLinus Torvalds 
4694ca52f53SJeff Layton 	fl->c.flc_file = filp;
4704ca52f53SJeff Layton 	fl->c.flc_owner = filp;
4714ca52f53SJeff Layton 	fl->c.flc_pid = current->tgid;
4724ca52f53SJeff Layton 	fl->c.flc_flags = FL_FLOCK;
4734ca52f53SJeff Layton 	fl->c.flc_type = type;
4741da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
assign_type(struct file_lock_core * flc,int type)477d9077f7bSJeff Layton static int assign_type(struct file_lock_core *flc, int type)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	switch (type) {
4801da177e4SLinus Torvalds 	case F_RDLCK:
4811da177e4SLinus Torvalds 	case F_WRLCK:
4821da177e4SLinus Torvalds 	case F_UNLCK:
483d9077f7bSJeff Layton 		flc->flc_type = type;
4841da177e4SLinus Torvalds 		break;
4851da177e4SLinus Torvalds 	default:
4861da177e4SLinus Torvalds 		return -EINVAL;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 	return 0;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
flock64_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock64 * l)491ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
492ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
493ef12e72aSJ. Bruce Fields {
494ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
495ef12e72aSJ. Bruce Fields 	case SEEK_SET:
496ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
497ef12e72aSJ. Bruce Fields 		break;
498ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
499ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
500ef12e72aSJ. Bruce Fields 		break;
501ef12e72aSJ. Bruce Fields 	case SEEK_END:
502ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
503ef12e72aSJ. Bruce Fields 		break;
504ef12e72aSJ. Bruce Fields 	default:
505ef12e72aSJ. Bruce Fields 		return -EINVAL;
506ef12e72aSJ. Bruce Fields 	}
507ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
508ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
509ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
510ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
511ef12e72aSJ. Bruce Fields 		return -EINVAL;
512ef12e72aSJ. Bruce Fields 
513ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
514ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
515ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
516ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
517ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
51816238415SLuo Meng 		fl->fl_end = fl->fl_start + (l->l_len - 1);
519ef12e72aSJ. Bruce Fields 
520ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
521ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
522ef12e72aSJ. Bruce Fields 			return -EINVAL;
523ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
524ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
525ef12e72aSJ. Bruce Fields 	} else
526ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
527ef12e72aSJ. Bruce Fields 
5284ca52f53SJeff Layton 	fl->c.flc_owner = current->files;
5294ca52f53SJeff Layton 	fl->c.flc_pid = current->tgid;
5304ca52f53SJeff Layton 	fl->c.flc_file = filp;
5314ca52f53SJeff Layton 	fl->c.flc_flags = FL_POSIX;
532ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
533ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
534ef12e72aSJ. Bruce Fields 
535d9077f7bSJeff Layton 	return assign_type(&fl->c, l->l_type);
536ef12e72aSJ. Bruce Fields }
537ef12e72aSJ. Bruce Fields 
5381da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
5391da177e4SLinus Torvalds  * style lock.
5401da177e4SLinus Torvalds  */
flock_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock * l)5411da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
5421da177e4SLinus Torvalds 			       struct flock *l)
5431da177e4SLinus Torvalds {
544ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
545ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
546ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
547ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
548ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
549ef12e72aSJ. Bruce Fields 	};
5501da177e4SLinus Torvalds 
551ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /* default lease lock manager operations */
5554d01b7f5SJeff Layton static bool
lease_break_callback(struct file_lease * fl)556c69ff407SJeff Layton lease_break_callback(struct file_lease *fl)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
5594d01b7f5SJeff Layton 	return false;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621c7dd2ffSJeff Layton static void
lease_setup(struct file_lease * fl,void ** priv)563c69ff407SJeff Layton lease_setup(struct file_lease *fl, void **priv)
5641c7dd2ffSJeff Layton {
5654ca52f53SJeff Layton 	struct file *filp = fl->c.flc_file;
5661c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
5671c7dd2ffSJeff Layton 
5681c7dd2ffSJeff Layton 	/*
5691c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
5701c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
5711c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
5721c7dd2ffSJeff Layton 	 */
5731c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
5741c7dd2ffSJeff Layton 		*priv = NULL;
5751c7dd2ffSJeff Layton 
57601919134SEric W. Biederman 	__f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
5771c7dd2ffSJeff Layton }
5781c7dd2ffSJeff Layton 
579c69ff407SJeff Layton static const struct lease_manager_operations lease_manager_ops = {
5808fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
5818fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
5821c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
5831da177e4SLinus Torvalds };
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds /*
5861da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
5871da177e4SLinus Torvalds  */
lease_init(struct file * filp,int type,struct file_lease * fl)588c69ff407SJeff Layton static int lease_init(struct file *filp, int type, struct file_lease *fl)
5891da177e4SLinus Torvalds {
590d9077f7bSJeff Layton 	if (assign_type(&fl->c, type) != 0)
59175dff55aSTrond Myklebust 		return -EINVAL;
59275dff55aSTrond Myklebust 
5934ca52f53SJeff Layton 	fl->c.flc_owner = filp;
5944ca52f53SJeff Layton 	fl->c.flc_pid = current->tgid;
5951da177e4SLinus Torvalds 
5964ca52f53SJeff Layton 	fl->c.flc_file = filp;
5974ca52f53SJeff Layton 	fl->c.flc_flags = FL_LEASE;
5981da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
5991da177e4SLinus Torvalds 	return 0;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
lease_alloc(struct file * filp,int type)603c69ff407SJeff Layton static struct file_lease *lease_alloc(struct file *filp, int type)
6041da177e4SLinus Torvalds {
605c69ff407SJeff Layton 	struct file_lease *fl = locks_alloc_lease();
60675dff55aSTrond Myklebust 	int error = -ENOMEM;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (fl == NULL)
609e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
61275dff55aSTrond Myklebust 	if (error) {
613c69ff407SJeff Layton 		locks_free_lease(fl);
614e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
61575dff55aSTrond Myklebust 	}
616e32b8ee2SJ. Bruce Fields 	return fl;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds /* Check if two locks overlap each other.
6201da177e4SLinus Torvalds  */
locks_overlap(struct file_lock * fl1,struct file_lock * fl2)6211da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
6241da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds /*
6281da177e4SLinus Torvalds  * Check whether two locks have the same owner.
6291da177e4SLinus Torvalds  */
posix_same_owner(struct file_lock_core * fl1,struct file_lock_core * fl2)6309bb430a8SJeff Layton static int posix_same_owner(struct file_lock_core *fl1, struct file_lock_core *fl2)
6311da177e4SLinus Torvalds {
6329bb430a8SJeff Layton 	return fl1->flc_owner == fl2->flc_owner;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6356109c850SJeff Layton /* Must be called with the flc_lock held! */
locks_insert_global_locks(struct file_lock_core * flc)6364629172fSJeff Layton static void locks_insert_global_locks(struct file_lock_core *flc)
63788974691SJeff Layton {
6387c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
6397c3f654dSPeter Zijlstra 
640aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
641aba37660SPeter Zijlstra 
6427c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
6434629172fSJeff Layton 	flc->flc_link_cpu = smp_processor_id();
6444629172fSJeff Layton 	hlist_add_head(&flc->flc_link, &fll->hlist);
6457c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
64688974691SJeff Layton }
64788974691SJeff Layton 
6486109c850SJeff Layton /* Must be called with the flc_lock held! */
locks_delete_global_locks(struct file_lock_core * flc)6494629172fSJeff Layton static void locks_delete_global_locks(struct file_lock_core *flc)
65088974691SJeff Layton {
6517c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll;
6527c3f654dSPeter Zijlstra 
653aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
654aba37660SPeter Zijlstra 
6557012b02aSJeff Layton 	/*
6567012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
6576109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
6587012b02aSJeff Layton 	 * also require that it be held.
6597012b02aSJeff Layton 	 */
6604629172fSJeff Layton 	if (hlist_unhashed(&flc->flc_link))
6617012b02aSJeff Layton 		return;
6627c3f654dSPeter Zijlstra 
6634629172fSJeff Layton 	fll = per_cpu_ptr(&file_lock_list, flc->flc_link_cpu);
6647c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
6654629172fSJeff Layton 	hlist_del_init(&flc->flc_link);
6667c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
66788974691SJeff Layton }
66888974691SJeff Layton 
6693999e493SJeff Layton static unsigned long
posix_owner_key(struct file_lock_core * flc)670ad399740SJeff Layton posix_owner_key(struct file_lock_core *flc)
6713999e493SJeff Layton {
672ad399740SJeff Layton 	return (unsigned long) flc->flc_owner;
6733999e493SJeff Layton }
6743999e493SJeff Layton 
locks_insert_global_blocked(struct file_lock_core * waiter)6751a6c75d4SJeff Layton static void locks_insert_global_blocked(struct file_lock_core *waiter)
67688974691SJeff Layton {
677663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
678663d5af7SDaniel Wagner 
6791a6c75d4SJeff Layton 	hash_add(blocked_hash, &waiter->flc_link, posix_owner_key(waiter));
68088974691SJeff Layton }
68188974691SJeff Layton 
locks_delete_global_blocked(struct file_lock_core * waiter)6821a6c75d4SJeff Layton static void locks_delete_global_blocked(struct file_lock_core *waiter)
68388974691SJeff Layton {
684663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
685663d5af7SDaniel Wagner 
6861a6c75d4SJeff Layton 	hash_del(&waiter->flc_link);
68788974691SJeff Layton }
68888974691SJeff Layton 
6891da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
6901da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
6911c8c601aSJeff Layton  *
6927b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
6931da177e4SLinus Torvalds  */
__locks_unlink_block(struct file_lock_core * waiter)694269a6194SJeff Layton static void __locks_unlink_block(struct file_lock_core *waiter)
6951da177e4SLinus Torvalds {
6961a62c22aSJeff Layton 	locks_delete_global_blocked(waiter);
6971a62c22aSJeff Layton 	list_del_init(&waiter->flc_blocked_member);
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
__locks_wake_up_blocks(struct file_lock_core * blocker)7001a62c22aSJeff Layton static void __locks_wake_up_blocks(struct file_lock_core *blocker)
701ad6bbd8bSNeilBrown {
7021a62c22aSJeff Layton 	while (!list_empty(&blocker->flc_blocked_requests)) {
7031a62c22aSJeff Layton 		struct file_lock_core *waiter;
7041a62c22aSJeff Layton 		struct file_lock *fl;
705ad6bbd8bSNeilBrown 
7061a62c22aSJeff Layton 		waiter = list_first_entry(&blocker->flc_blocked_requests,
7071a62c22aSJeff Layton 					  struct file_lock_core, flc_blocked_member);
7081a62c22aSJeff Layton 
7091a62c22aSJeff Layton 		fl = file_lock(waiter);
710269a6194SJeff Layton 		__locks_unlink_block(waiter);
7111a62c22aSJeff Layton 		if ((waiter->flc_flags & (FL_POSIX | FL_FLOCK)) &&
7121a62c22aSJeff Layton 		    fl->fl_lmops && fl->fl_lmops->lm_notify)
7131a62c22aSJeff Layton 			fl->fl_lmops->lm_notify(fl);
714ad6bbd8bSNeilBrown 		else
7151a62c22aSJeff Layton 			locks_wake_up(fl);
716dcf23ac3SLinus Torvalds 
717dcf23ac3SLinus Torvalds 		/*
7181a62c22aSJeff Layton 		 * The setting of flc_blocker to NULL marks the "done"
719dcf23ac3SLinus Torvalds 		 * point in deleting a block. Paired with acquire at the top
720dcf23ac3SLinus Torvalds 		 * of locks_delete_block().
721dcf23ac3SLinus Torvalds 		 */
7221a62c22aSJeff Layton 		smp_store_release(&waiter->flc_blocker, NULL);
723ad6bbd8bSNeilBrown 	}
724ad6bbd8bSNeilBrown }
725ad6bbd8bSNeilBrown 
__locks_delete_block(struct file_lock_core * waiter)726269a6194SJeff Layton static int __locks_delete_block(struct file_lock_core *waiter)
7271da177e4SLinus Torvalds {
728cb03f94fSNeilBrown 	int status = -ENOENT;
729cb03f94fSNeilBrown 
730dcf23ac3SLinus Torvalds 	/*
731dcf23ac3SLinus Torvalds 	 * If fl_blocker is NULL, it won't be set again as this thread "owns"
732dcf23ac3SLinus Torvalds 	 * the lock and is the only one that might try to claim the lock.
733dcf23ac3SLinus Torvalds 	 *
734dcf23ac3SLinus Torvalds 	 * We use acquire/release to manage fl_blocker so that we can
735dcf23ac3SLinus Torvalds 	 * optimize away taking the blocked_lock_lock in many cases.
736dcf23ac3SLinus Torvalds 	 *
737dcf23ac3SLinus Torvalds 	 * The smp_load_acquire guarantees two things:
738dcf23ac3SLinus Torvalds 	 *
739dcf23ac3SLinus Torvalds 	 * 1/ that fl_blocked_requests can be tested locklessly. If something
740dcf23ac3SLinus Torvalds 	 * was recently added to that list it must have been in a locked region
741dcf23ac3SLinus Torvalds 	 * *before* the locked region when fl_blocker was set to NULL.
742dcf23ac3SLinus Torvalds 	 *
743dcf23ac3SLinus Torvalds 	 * 2/ that no other thread is accessing 'waiter', so it is safe to free
744dcf23ac3SLinus Torvalds 	 * it.  __locks_wake_up_blocks is careful not to touch waiter after
745dcf23ac3SLinus Torvalds 	 * fl_blocker is released.
746dcf23ac3SLinus Torvalds 	 *
747dcf23ac3SLinus Torvalds 	 * If a lockless check of fl_blocker shows it to be NULL, we know that
748dcf23ac3SLinus Torvalds 	 * no new locks can be inserted into its fl_blocked_requests list, and
749dcf23ac3SLinus Torvalds 	 * can avoid doing anything further if the list is empty.
750dcf23ac3SLinus Torvalds 	 */
751e8a166cfSJeff Layton 	if (!smp_load_acquire(&waiter->flc_blocker) &&
752e8a166cfSJeff Layton 	    list_empty(&waiter->flc_blocked_requests))
753dcf23ac3SLinus Torvalds 		return status;
754dcf23ac3SLinus Torvalds 
7557b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
756e8a166cfSJeff Layton 	if (waiter->flc_blocker)
757cb03f94fSNeilBrown 		status = 0;
758e8a166cfSJeff Layton 	__locks_wake_up_blocks(waiter);
759269a6194SJeff Layton 	__locks_unlink_block(waiter);
760dcf23ac3SLinus Torvalds 
761dcf23ac3SLinus Torvalds 	/*
762dcf23ac3SLinus Torvalds 	 * The setting of fl_blocker to NULL marks the "done" point in deleting
763dcf23ac3SLinus Torvalds 	 * a block. Paired with acquire at the top of this function.
764dcf23ac3SLinus Torvalds 	 */
765e8a166cfSJeff Layton 	smp_store_release(&waiter->flc_blocker, NULL);
7667b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
767cb03f94fSNeilBrown 	return status;
7681da177e4SLinus Torvalds }
769269a6194SJeff Layton 
770269a6194SJeff Layton /**
771269a6194SJeff Layton  *	locks_delete_block - stop waiting for a file lock
772269a6194SJeff Layton  *	@waiter: the lock which was waiting
773269a6194SJeff Layton  *
774269a6194SJeff Layton  *	lockd/nfsd need to disconnect the lock while working on it.
775269a6194SJeff Layton  */
locks_delete_block(struct file_lock * waiter)776269a6194SJeff Layton int locks_delete_block(struct file_lock *waiter)
777269a6194SJeff Layton {
778269a6194SJeff Layton 	return __locks_delete_block(&waiter->c);
779269a6194SJeff Layton }
780cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
7831da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
7841da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
7851da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
7861c8c601aSJeff Layton  *
7876109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
788ada5c1daSNeilBrown  * fl_blocked_requests list itself is protected by the blocked_lock_lock,
789ada5c1daSNeilBrown  * but by ensuring that the flc_lock is also held on insertions we can avoid
790ada5c1daSNeilBrown  * taking the blocked_lock_lock in some cases when we see that the
791ada5c1daSNeilBrown  * fl_blocked_requests list is empty.
792fd7732e0SNeilBrown  *
793fd7732e0SNeilBrown  * Rather than just adding to the list, we check for conflicts with any existing
794fd7732e0SNeilBrown  * waiters, and add beneath any waiter that blocks the new waiter.
795fd7732e0SNeilBrown  * Thus wakeups don't happen until needed.
7961da177e4SLinus Torvalds  */
__locks_insert_block(struct file_lock_core * blocker,struct file_lock_core * waiter,bool conflict (struct file_lock_core *,struct file_lock_core *))797269a6194SJeff Layton static void __locks_insert_block(struct file_lock_core *blocker,
798269a6194SJeff Layton 				 struct file_lock_core *waiter,
799b6be3714SJeff Layton 				 bool conflict(struct file_lock_core *,
800b6be3714SJeff Layton 					       struct file_lock_core *))
8011da177e4SLinus Torvalds {
802b6be3714SJeff Layton 	struct file_lock_core *flc;
803fd7732e0SNeilBrown 
804b6be3714SJeff Layton 	BUG_ON(!list_empty(&waiter->flc_blocked_member));
805fd7732e0SNeilBrown new_blocker:
806b6be3714SJeff Layton 	list_for_each_entry(flc, &blocker->flc_blocked_requests, flc_blocked_member)
807b6be3714SJeff Layton 		if (conflict(flc, waiter)) {
808b6be3714SJeff Layton 			blocker =  flc;
809fd7732e0SNeilBrown 			goto new_blocker;
810fd7732e0SNeilBrown 		}
811b6aaba5bSJeff Layton 	waiter->flc_blocker = blocker;
812b6be3714SJeff Layton 	list_add_tail(&waiter->flc_blocked_member,
813b6be3714SJeff Layton 		      &blocker->flc_blocked_requests);
8145946c431SNeilBrown 
81514786d94SJeff Layton 	if ((blocker->flc_flags & (FL_POSIX|FL_OFDLCK)) == FL_POSIX)
816b6be3714SJeff Layton 		locks_insert_global_blocked(waiter);
817b6be3714SJeff Layton 
818b6be3714SJeff Layton 	/* The requests in waiter->flc_blocked are known to conflict with
8195946c431SNeilBrown 	 * waiter, but might not conflict with blocker, or the requests
8205946c431SNeilBrown 	 * and lock which block it.  So they all need to be woken.
8215946c431SNeilBrown 	 */
822b6be3714SJeff Layton 	__locks_wake_up_blocks(waiter);
8231c8c601aSJeff Layton }
8241c8c601aSJeff Layton 
8256109c850SJeff Layton /* Must be called with flc_lock held. */
locks_insert_block(struct file_lock_core * blocker,struct file_lock_core * waiter,bool conflict (struct file_lock_core *,struct file_lock_core *))826269a6194SJeff Layton static void locks_insert_block(struct file_lock_core *blocker,
827269a6194SJeff Layton 			       struct file_lock_core *waiter,
828b6be3714SJeff Layton 			       bool conflict(struct file_lock_core *,
829b6be3714SJeff Layton 					     struct file_lock_core *))
8301c8c601aSJeff Layton {
8317b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
832fd7732e0SNeilBrown 	__locks_insert_block(blocker, waiter, conflict);
8337b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361cb36012SJeff Layton /*
8371cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
8381cb36012SJeff Layton  *
8396109c850SJeff Layton  * Must be called with the inode->flc_lock held!
8401da177e4SLinus Torvalds  */
locks_wake_up_blocks(struct file_lock_core * blocker)841347d49fdSJeff Layton static void locks_wake_up_blocks(struct file_lock_core *blocker)
8421da177e4SLinus Torvalds {
8434e8c765dSJeff Layton 	/*
8444e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
8456109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
846ada5c1daSNeilBrown 	 * the flc_lock is always held here. Note that removal from the
847ada5c1daSNeilBrown 	 * fl_blocked_requests list does not require the flc_lock, so we must
848ada5c1daSNeilBrown 	 * recheck list_empty() after acquiring the blocked_lock_lock.
8494e8c765dSJeff Layton 	 */
850347d49fdSJeff Layton 	if (list_empty(&blocker->flc_blocked_requests))
8514e8c765dSJeff Layton 		return;
8524e8c765dSJeff Layton 
8537b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
854347d49fdSJeff Layton 	__locks_wake_up_blocks(blocker);
8557b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8585263e31eSJeff Layton static void
locks_insert_lock_ctx(struct file_lock_core * fl,struct list_head * before)8597c18509bSJeff Layton locks_insert_lock_ctx(struct file_lock_core *fl, struct list_head *before)
8605263e31eSJeff Layton {
8617c18509bSJeff Layton 	list_add_tail(&fl->flc_list, before);
8627c18509bSJeff Layton 	locks_insert_global_locks(fl);
8635263e31eSJeff Layton }
8645263e31eSJeff Layton 
8658634b51fSJeff Layton static void
locks_unlink_lock_ctx(struct file_lock_core * fl)8667c18509bSJeff Layton locks_unlink_lock_ctx(struct file_lock_core *fl)
8671da177e4SLinus Torvalds {
8687c18509bSJeff Layton 	locks_delete_global_locks(fl);
8697c18509bSJeff Layton 	list_del_init(&fl->flc_list);
8707c18509bSJeff Layton 	locks_wake_up_blocks(fl);
87124cbe784SJeff Layton }
87224cbe784SJeff Layton 
8735263e31eSJeff Layton static void
locks_delete_lock_ctx(struct file_lock_core * fl,struct list_head * dispose)8747c18509bSJeff Layton locks_delete_lock_ctx(struct file_lock_core *fl, struct list_head *dispose)
8755263e31eSJeff Layton {
876e084c1bdSJeff Layton 	locks_unlink_lock_ctx(fl);
8778634b51fSJeff Layton 	if (dispose)
8787c18509bSJeff Layton 		list_add(&fl->flc_list, dispose);
8798634b51fSJeff Layton 	else
8807c18509bSJeff Layton 		locks_free_lock(file_lock(fl));
8815263e31eSJeff Layton }
8825263e31eSJeff Layton 
8831da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
8841da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
8851da177e4SLinus Torvalds  */
locks_conflict(struct file_lock_core * caller_flc,struct file_lock_core * sys_flc)886b6be3714SJeff Layton static bool locks_conflict(struct file_lock_core *caller_flc,
887b6be3714SJeff Layton 			   struct file_lock_core *sys_flc)
8881da177e4SLinus Torvalds {
889b6be3714SJeff Layton 	if (sys_flc->flc_type == F_WRLCK)
890c0e15908SNeilBrown 		return true;
891b6be3714SJeff Layton 	if (caller_flc->flc_type == F_WRLCK)
892c0e15908SNeilBrown 		return true;
893c0e15908SNeilBrown 	return false;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
8971da177e4SLinus Torvalds  * checking before calling the locks_conflict().
8981da177e4SLinus Torvalds  */
posix_locks_conflict(struct file_lock_core * caller_flc,struct file_lock_core * sys_flc)899b6be3714SJeff Layton static bool posix_locks_conflict(struct file_lock_core *caller_flc,
900b6be3714SJeff Layton 				 struct file_lock_core *sys_flc)
9011da177e4SLinus Torvalds {
902b6be3714SJeff Layton 	struct file_lock *caller_fl = file_lock(caller_flc);
903b6be3714SJeff Layton 	struct file_lock *sys_fl = file_lock(sys_flc);
904b6be3714SJeff Layton 
9051da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
9061da177e4SLinus Torvalds 	 * each other.
9071da177e4SLinus Torvalds 	 */
908b6be3714SJeff Layton 	if (posix_same_owner(caller_flc, sys_flc))
909c0e15908SNeilBrown 		return false;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* Check whether they overlap */
9121da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
913c0e15908SNeilBrown 		return false;
9141da177e4SLinus Torvalds 
915b6be3714SJeff Layton 	return locks_conflict(caller_flc, sys_flc);
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
9186c9007f6SStas Sergeev /* Determine if lock sys_fl blocks lock caller_fl. Used on xx_GETLK
9196c9007f6SStas Sergeev  * path so checks for additional GETLK-specific things like F_UNLCK.
9206c9007f6SStas Sergeev  */
posix_test_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)9216c9007f6SStas Sergeev static bool posix_test_locks_conflict(struct file_lock *caller_fl,
9226c9007f6SStas Sergeev 				      struct file_lock *sys_fl)
9236c9007f6SStas Sergeev {
924b6be3714SJeff Layton 	struct file_lock_core *caller = &caller_fl->c;
925b6be3714SJeff Layton 	struct file_lock_core *sys = &sys_fl->c;
926b6be3714SJeff Layton 
9276c9007f6SStas Sergeev 	/* F_UNLCK checks any locks on the same fd. */
92875cabec0SJeff Layton 	if (lock_is_unlock(caller_fl)) {
929b6be3714SJeff Layton 		if (!posix_same_owner(caller, sys))
9306c9007f6SStas Sergeev 			return false;
9316c9007f6SStas Sergeev 		return locks_overlap(caller_fl, sys_fl);
9326c9007f6SStas Sergeev 	}
933b6be3714SJeff Layton 	return posix_locks_conflict(caller, sys);
9346c9007f6SStas Sergeev }
9356c9007f6SStas Sergeev 
9361da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
9371da177e4SLinus Torvalds  * checking before calling the locks_conflict().
9381da177e4SLinus Torvalds  */
flock_locks_conflict(struct file_lock_core * caller_flc,struct file_lock_core * sys_flc)939b6be3714SJeff Layton static bool flock_locks_conflict(struct file_lock_core *caller_flc,
940b6be3714SJeff Layton 				 struct file_lock_core *sys_flc)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
9431da177e4SLinus Torvalds 	 * each other.
9441da177e4SLinus Torvalds 	 */
945b6be3714SJeff Layton 	if (caller_flc->flc_file == sys_flc->flc_file)
946c0e15908SNeilBrown 		return false;
9471da177e4SLinus Torvalds 
948b6be3714SJeff Layton 	return locks_conflict(caller_flc, sys_flc);
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9516d34ac19SJ. Bruce Fields void
posix_test_lock(struct file * filp,struct file_lock * fl)9529d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct file_lock *cfl;
955bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
956c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
9572443da22SDai Ngo 	void *owner;
9582443da22SDai Ngo 	void (*func)(void);
9591da177e4SLinus Torvalds 
960401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
961bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
9624ca52f53SJeff Layton 		fl->c.flc_type = F_UNLCK;
963bd61e0a9SJeff Layton 		return;
9641da177e4SLinus Torvalds 	}
965bd61e0a9SJeff Layton 
9662443da22SDai Ngo retry:
9676109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
9684ca52f53SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, c.flc_list) {
9696c9007f6SStas Sergeev 		if (!posix_test_locks_conflict(fl, cfl))
9702443da22SDai Ngo 			continue;
9712443da22SDai Ngo 		if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable
9722443da22SDai Ngo 			&& (*cfl->fl_lmops->lm_lock_expirable)(cfl)) {
9732443da22SDai Ngo 			owner = cfl->fl_lmops->lm_mod_owner;
9742443da22SDai Ngo 			func = cfl->fl_lmops->lm_expire_lock;
9752443da22SDai Ngo 			__module_get(owner);
9762443da22SDai Ngo 			spin_unlock(&ctx->flc_lock);
9772443da22SDai Ngo 			(*func)();
9782443da22SDai Ngo 			module_put(owner);
9792443da22SDai Ngo 			goto retry;
9802443da22SDai Ngo 		}
9813fe0fff1SKinglong Mee 		locks_copy_conflock(fl, cfl);
982bd61e0a9SJeff Layton 		goto out;
983bd61e0a9SJeff Layton 	}
9844ca52f53SJeff Layton 	fl->c.flc_type = F_UNLCK;
985bd61e0a9SJeff Layton out:
9866109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
9876d34ac19SJ. Bruce Fields 	return;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
9901da177e4SLinus Torvalds 
991b533184fSJ. Bruce Fields /*
992b533184fSJ. Bruce Fields  * Deadlock detection:
9931da177e4SLinus Torvalds  *
994b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
995b533184fSJ. Bruce Fields  * locks.
9961da177e4SLinus Torvalds  *
997b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
998b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
999b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
1000b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
1001b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
1002b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
1003b533184fSJ. Bruce Fields  * cycle.
100497855b49SJ. Bruce Fields  *
1005b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
1006b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
1007b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
1008b533184fSJ. Bruce Fields  *
1009b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
1010b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
1011b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
1012b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
101357b65325SJeff Layton  *
1014cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
101557b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
101657b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
101757b65325SJeff Layton  * skip it for those.
101857b65325SJeff Layton  *
1019cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
102057b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
102157b65325SJeff Layton  * upgrade from read to write locks on the same inode.
10221da177e4SLinus Torvalds  */
102397855b49SJ. Bruce Fields 
102497855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
102597855b49SJ. Bruce Fields 
1026b6be3714SJeff Layton /* Find a lock that the owner of the given @blocker is blocking on. */
what_owner_is_waiting_for(struct file_lock_core * blocker)1027b6be3714SJeff Layton static struct file_lock_core *what_owner_is_waiting_for(struct file_lock_core *blocker)
1028b533184fSJ. Bruce Fields {
1029b6be3714SJeff Layton 	struct file_lock_core *flc;
1030b533184fSJ. Bruce Fields 
1031b6be3714SJeff Layton 	hash_for_each_possible(blocked_hash, flc, flc_link, posix_owner_key(blocker)) {
1032b6be3714SJeff Layton 		if (posix_same_owner(flc, blocker)) {
1033b6be3714SJeff Layton 			while (flc->flc_blocker)
1034b6aaba5bSJeff Layton 				flc = flc->flc_blocker;
1035b6be3714SJeff Layton 			return flc;
10365946c431SNeilBrown 		}
1037b533184fSJ. Bruce Fields 	}
1038b533184fSJ. Bruce Fields 	return NULL;
1039b533184fSJ. Bruce Fields }
1040b533184fSJ. Bruce Fields 
10417b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
posix_locks_deadlock(struct file_lock * caller_fl,struct file_lock * block_fl)1042b6be3714SJeff Layton static bool posix_locks_deadlock(struct file_lock *caller_fl,
10431da177e4SLinus Torvalds 				 struct file_lock *block_fl)
10441da177e4SLinus Torvalds {
1045b6be3714SJeff Layton 	struct file_lock_core *caller = &caller_fl->c;
1046b6be3714SJeff Layton 	struct file_lock_core *blocker = &block_fl->c;
104797855b49SJ. Bruce Fields 	int i = 0;
10481da177e4SLinus Torvalds 
1049663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
1050663d5af7SDaniel Wagner 
105157b65325SJeff Layton 	/*
105257b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
1053cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
105457b65325SJeff Layton 	 */
1055b6be3714SJeff Layton 	if (caller->flc_flags & FL_OFDLCK)
1056b6be3714SJeff Layton 		return false;
105757b65325SJeff Layton 
1058b6be3714SJeff Layton 	while ((blocker = what_owner_is_waiting_for(blocker))) {
105997855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
1060b6be3714SJeff Layton 			return false;
1061b6be3714SJeff Layton 		if (posix_same_owner(caller, blocker))
1062b6be3714SJeff Layton 			return true;
10631da177e4SLinus Torvalds 	}
1064b6be3714SJeff Layton 	return false;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
106802888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
1069f475ae95STrond Myklebust  *
1070f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1071f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1072f475ae95STrond Myklebust  * value for -ENOENT.
10731da177e4SLinus Torvalds  */
flock_lock_inode(struct inode * inode,struct file_lock * request)1074bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request)
10751da177e4SLinus Torvalds {
1076993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
10775263e31eSJeff Layton 	struct file_lock *fl;
10785263e31eSJeff Layton 	struct file_lock_context *ctx;
10791da177e4SLinus Torvalds 	int error = 0;
10805263e31eSJeff Layton 	bool found = false;
1081ed9814d8SJeff Layton 	LIST_HEAD(dispose);
10821da177e4SLinus Torvalds 
10834ca52f53SJeff Layton 	ctx = locks_get_lock_context(inode, request->c.flc_type);
10845c1c669aSJeff Layton 	if (!ctx) {
10854ca52f53SJeff Layton 		if (request->c.flc_type != F_UNLCK)
10865263e31eSJeff Layton 			return -ENOMEM;
10874ca52f53SJeff Layton 		return (request->c.flc_flags & FL_EXISTS) ? -ENOENT : 0;
10885c1c669aSJeff Layton 	}
10895263e31eSJeff Layton 
10904ca52f53SJeff Layton 	if (!(request->c.flc_flags & FL_ACCESS) && (request->c.flc_type != F_UNLCK)) {
1091b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
1092b89f4321SArnd Bergmann 		if (!new_fl)
1093b89f4321SArnd Bergmann 			return -ENOMEM;
1094b89f4321SArnd Bergmann 	}
1095b89f4321SArnd Bergmann 
109602e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
10976109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
10984ca52f53SJeff Layton 	if (request->c.flc_flags & FL_ACCESS)
1099f07f18ddSTrond Myklebust 		goto find_conflict;
110084d535adSPavel Emelyanov 
11014ca52f53SJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) {
11024ca52f53SJeff Layton 		if (request->c.flc_file != fl->c.flc_file)
11031da177e4SLinus Torvalds 			continue;
11044ca52f53SJeff Layton 		if (request->c.flc_type == fl->c.flc_type)
11051da177e4SLinus Torvalds 			goto out;
11065263e31eSJeff Layton 		found = true;
11077c18509bSJeff Layton 		locks_delete_lock_ctx(&fl->c, &dispose);
11081da177e4SLinus Torvalds 		break;
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 
111175cabec0SJeff Layton 	if (lock_is_unlock(request)) {
11124ca52f53SJeff Layton 		if ((request->c.flc_flags & FL_EXISTS) && !found)
1113f475ae95STrond Myklebust 			error = -ENOENT;
1114993dfa87STrond Myklebust 		goto out;
1115f475ae95STrond Myklebust 	}
11161da177e4SLinus Torvalds 
1117f07f18ddSTrond Myklebust find_conflict:
11184ca52f53SJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, c.flc_list) {
1119b6be3714SJeff Layton 		if (!flock_locks_conflict(&request->c, &fl->c))
11201da177e4SLinus Torvalds 			continue;
11211da177e4SLinus Torvalds 		error = -EAGAIN;
11224ca52f53SJeff Layton 		if (!(request->c.flc_flags & FL_SLEEP))
1123bde74e4bSMiklos Szeredi 			goto out;
1124bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
1125269a6194SJeff Layton 		locks_insert_block(&fl->c, &request->c, flock_locks_conflict);
11261da177e4SLinus Torvalds 		goto out;
11271da177e4SLinus Torvalds 	}
11284ca52f53SJeff Layton 	if (request->c.flc_flags & FL_ACCESS)
1129f07f18ddSTrond Myklebust 		goto out;
1130993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
11315946c431SNeilBrown 	locks_move_blocks(new_fl, request);
11327c18509bSJeff Layton 	locks_insert_lock_ctx(&new_fl->c, &ctx->flc_flock);
1133993dfa87STrond Myklebust 	new_fl = NULL;
11349cedc194SKirill Korotaev 	error = 0;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds out:
11376109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
113802e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1139993dfa87STrond Myklebust 	if (new_fl)
1140993dfa87STrond Myklebust 		locks_free_lock(new_fl);
1141ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
1142c883da31SJeff Layton 	trace_flock_lock_inode(inode, request, error);
11431da177e4SLinus Torvalds 	return error;
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
posix_lock_inode(struct inode * inode,struct file_lock * request,struct file_lock * conflock)1146b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1147b4d629a3SJeff Layton 			    struct file_lock *conflock)
11481da177e4SLinus Torvalds {
1149bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
115039005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
115139005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
11521da177e4SLinus Torvalds 	struct file_lock *left = NULL;
11531da177e4SLinus Torvalds 	struct file_lock *right = NULL;
1154bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
1155b9746ef8SJeff Layton 	int error;
1156b9746ef8SJeff Layton 	bool added = false;
1157ed9814d8SJeff Layton 	LIST_HEAD(dispose);
11582443da22SDai Ngo 	void *owner;
11592443da22SDai Ngo 	void (*func)(void);
11601da177e4SLinus Torvalds 
11614ca52f53SJeff Layton 	ctx = locks_get_lock_context(inode, request->c.flc_type);
1162bd61e0a9SJeff Layton 	if (!ctx)
116375cabec0SJeff Layton 		return lock_is_unlock(request) ? 0 : -ENOMEM;
1164bd61e0a9SJeff Layton 
11651da177e4SLinus Torvalds 	/*
11661da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
11671da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
116839005d02SMiklos Szeredi 	 *
116939005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
11701da177e4SLinus Torvalds 	 */
11714ca52f53SJeff Layton 	if (!(request->c.flc_flags & FL_ACCESS) &&
11724ca52f53SJeff Layton 	    (request->c.flc_type != F_UNLCK ||
117339005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
11741da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
11751da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
117639005d02SMiklos Szeredi 	}
11771da177e4SLinus Torvalds 
11782443da22SDai Ngo retry:
117902e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
11806109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
11811cb36012SJeff Layton 	/*
11821cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
11831cb36012SJeff Layton 	 * there are any, either return error or put the request on the
118448f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
11851cb36012SJeff Layton 	 */
11864ca52f53SJeff Layton 	if (request->c.flc_type != F_UNLCK) {
11874ca52f53SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) {
1188b6be3714SJeff Layton 			if (!posix_locks_conflict(&request->c, &fl->c))
11891da177e4SLinus Torvalds 				continue;
11902443da22SDai Ngo 			if (fl->fl_lmops && fl->fl_lmops->lm_lock_expirable
11912443da22SDai Ngo 				&& (*fl->fl_lmops->lm_lock_expirable)(fl)) {
11922443da22SDai Ngo 				owner = fl->fl_lmops->lm_mod_owner;
11932443da22SDai Ngo 				func = fl->fl_lmops->lm_expire_lock;
11942443da22SDai Ngo 				__module_get(owner);
11952443da22SDai Ngo 				spin_unlock(&ctx->flc_lock);
11962443da22SDai Ngo 				percpu_up_read(&file_rwsem);
11972443da22SDai Ngo 				(*func)();
11982443da22SDai Ngo 				module_put(owner);
11992443da22SDai Ngo 				goto retry;
12002443da22SDai Ngo 			}
12015842add2SAndy Adamson 			if (conflock)
12023fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
12031da177e4SLinus Torvalds 			error = -EAGAIN;
12044ca52f53SJeff Layton 			if (!(request->c.flc_flags & FL_SLEEP))
12051da177e4SLinus Torvalds 				goto out;
12061c8c601aSJeff Layton 			/*
12071c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
12081c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
12091c8c601aSJeff Layton 			 */
12101da177e4SLinus Torvalds 			error = -EDEADLK;
12117b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
1212945ab8f6SJeff Layton 			/*
1213945ab8f6SJeff Layton 			 * Ensure that we don't find any locks blocked on this
1214945ab8f6SJeff Layton 			 * request during deadlock detection.
1215945ab8f6SJeff Layton 			 */
12161a62c22aSJeff Layton 			__locks_wake_up_blocks(&request->c);
12171c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
1218bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
1219269a6194SJeff Layton 				__locks_insert_block(&fl->c, &request->c,
1220fd7732e0SNeilBrown 						     posix_locks_conflict);
12211c8c601aSJeff Layton 			}
12227b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
12231da177e4SLinus Torvalds 			goto out;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
12281da177e4SLinus Torvalds 	error = 0;
12294ca52f53SJeff Layton 	if (request->c.flc_flags & FL_ACCESS)
12301da177e4SLinus Torvalds 		goto out;
12311da177e4SLinus Torvalds 
1232bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
12334ca52f53SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, c.flc_list) {
12349bb430a8SJeff Layton 		if (posix_same_owner(&request->c, &fl->c))
1235bd61e0a9SJeff Layton 			break;
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	/* Process locks with this owner. */
12394ca52f53SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, c.flc_list) {
12409bb430a8SJeff Layton 		if (!posix_same_owner(&request->c, &fl->c))
1241bd61e0a9SJeff Layton 			break;
1242bd61e0a9SJeff Layton 
1243bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
12444ca52f53SJeff Layton 		if (request->c.flc_type == fl->c.flc_type) {
1245449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1246449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1247449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1248449231d6SOlaf Kirch 			 */
12491da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1250bd61e0a9SJeff Layton 				continue;
12511da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
12521da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
12531da177e4SLinus Torvalds 			 */
1254449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
12551da177e4SLinus Torvalds 				break;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
12581da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
12591da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
12601da177e4SLinus Torvalds 			 * locks to the higher end address.
12611da177e4SLinus Torvalds 			 */
12621da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
12631da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
12641da177e4SLinus Torvalds 			else
12651da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
12661da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
12671da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
12681da177e4SLinus Torvalds 			else
12691da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
12701da177e4SLinus Torvalds 			if (added) {
12717c18509bSJeff Layton 				locks_delete_lock_ctx(&fl->c, &dispose);
12721da177e4SLinus Torvalds 				continue;
12731da177e4SLinus Torvalds 			}
12741da177e4SLinus Torvalds 			request = fl;
1275b9746ef8SJeff Layton 			added = true;
1276bd61e0a9SJeff Layton 		} else {
12771da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
12781da177e4SLinus Torvalds 			 * more complex.
12791da177e4SLinus Torvalds 			 */
12801da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1281bd61e0a9SJeff Layton 				continue;
12821da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
12831da177e4SLinus Torvalds 				break;
128475cabec0SJeff Layton 			if (lock_is_unlock(request))
1285b9746ef8SJeff Layton 				added = true;
12861da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
12871da177e4SLinus Torvalds 				left = fl;
12881da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
12891da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
12901da177e4SLinus Torvalds 			 */
12911da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
12921da177e4SLinus Torvalds 				right = fl;
12931da177e4SLinus Torvalds 				break;
12941da177e4SLinus Torvalds 			}
12951da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
12961da177e4SLinus Torvalds 				/* The new lock completely replaces an old
12971da177e4SLinus Torvalds 				 * one (This may happen several times).
12981da177e4SLinus Torvalds 				 */
12991da177e4SLinus Torvalds 				if (added) {
13007c18509bSJeff Layton 					locks_delete_lock_ctx(&fl->c, &dispose);
13011da177e4SLinus Torvalds 					continue;
13021da177e4SLinus Torvalds 				}
1303b84d49f9SJeff Layton 				/*
1304b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1305b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1306b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1307b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1308b84d49f9SJeff Layton 				 * just replacing an existing lock.
13091da177e4SLinus Torvalds 				 */
1310b84d49f9SJeff Layton 				error = -ENOLCK;
1311b84d49f9SJeff Layton 				if (!new_fl)
1312b84d49f9SJeff Layton 					goto out;
1313b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
13145ef15968Syangerkun 				locks_move_blocks(new_fl, request);
1315b84d49f9SJeff Layton 				request = new_fl;
1316b84d49f9SJeff Layton 				new_fl = NULL;
13177c18509bSJeff Layton 				locks_insert_lock_ctx(&request->c,
13184ca52f53SJeff Layton 						      &fl->c.flc_list);
13197c18509bSJeff Layton 				locks_delete_lock_ctx(&fl->c, &dispose);
1320b9746ef8SJeff Layton 				added = true;
13211da177e4SLinus Torvalds 			}
13221da177e4SLinus Torvalds 		}
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13250d9a490aSMiklos Szeredi 	/*
13261cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
13271cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
13281cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
13290d9a490aSMiklos Szeredi 	 */
13300d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
13310d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
13320d9a490aSMiklos Szeredi 		goto out;
13330d9a490aSMiklos Szeredi 
13341da177e4SLinus Torvalds 	error = 0;
13351da177e4SLinus Torvalds 	if (!added) {
133675cabec0SJeff Layton 		if (lock_is_unlock(request)) {
13374ca52f53SJeff Layton 			if (request->c.flc_flags & FL_EXISTS)
1338f475ae95STrond Myklebust 				error = -ENOENT;
13391da177e4SLinus Torvalds 			goto out;
1340f475ae95STrond Myklebust 		}
13410d9a490aSMiklos Szeredi 
13420d9a490aSMiklos Szeredi 		if (!new_fl) {
13430d9a490aSMiklos Szeredi 			error = -ENOLCK;
13440d9a490aSMiklos Szeredi 			goto out;
13450d9a490aSMiklos Szeredi 		}
13461da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
13475946c431SNeilBrown 		locks_move_blocks(new_fl, request);
13487c18509bSJeff Layton 		locks_insert_lock_ctx(&new_fl->c, &fl->c.flc_list);
13492e2f756fSJeff Layton 		fl = new_fl;
13501da177e4SLinus Torvalds 		new_fl = NULL;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 	if (right) {
13531da177e4SLinus Torvalds 		if (left == right) {
13541da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
13551da177e4SLinus Torvalds 			 * so we have to use the second new lock.
13561da177e4SLinus Torvalds 			 */
13571da177e4SLinus Torvalds 			left = new_fl2;
13581da177e4SLinus Torvalds 			new_fl2 = NULL;
13591da177e4SLinus Torvalds 			locks_copy_lock(left, right);
13607c18509bSJeff Layton 			locks_insert_lock_ctx(&left->c, &fl->c.flc_list);
13611da177e4SLinus Torvalds 		}
13621da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
1363347d49fdSJeff Layton 		locks_wake_up_blocks(&right->c);
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 	if (left) {
13661da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
1367347d49fdSJeff Layton 		locks_wake_up_blocks(&left->c);
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds  out:
13701b3ec4f7SJeff Layton 	trace_posix_lock_inode(inode, request, error);
13716109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
137202e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
13731da177e4SLinus Torvalds 	/*
13741da177e4SLinus Torvalds 	 * Free any unused locks.
13751da177e4SLinus Torvalds 	 */
13761da177e4SLinus Torvalds 	if (new_fl)
13771da177e4SLinus Torvalds 		locks_free_lock(new_fl);
13781da177e4SLinus Torvalds 	if (new_fl2)
13791da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1380ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
13811890910fSJeff Layton 
13821da177e4SLinus Torvalds 	return error;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /**
13861da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
13871da177e4SLinus Torvalds  * @filp: The file to apply the lock to
13881da177e4SLinus Torvalds  * @fl: The lock to be applied
1389150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
13901da177e4SLinus Torvalds  *
13911da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
13921da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
13931da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1394f475ae95STrond Myklebust  *
1395f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1396f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1397f475ae95STrond Myklebust  * value for -ENOENT.
13981da177e4SLinus Torvalds  */
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)1399150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
14005842add2SAndy Adamson 			struct file_lock *conflock)
14015842add2SAndy Adamson {
1402c65454a9SJeff Layton 	return posix_lock_inode(file_inode(filp), fl, conflock);
14035842add2SAndy Adamson }
1404150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds /**
140729d01b22SJeff Layton  * posix_lock_inode_wait - Apply a POSIX-style lock to a file
140829d01b22SJeff Layton  * @inode: inode of file to which lock request should be applied
14091da177e4SLinus Torvalds  * @fl: The lock to be applied
14101da177e4SLinus Torvalds  *
1411616fb38fSBenjamin Coddington  * Apply a POSIX style lock request to an inode.
14121da177e4SLinus Torvalds  */
posix_lock_inode_wait(struct inode * inode,struct file_lock * fl)1413616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	int error;
14161da177e4SLinus Torvalds 	might_sleep ();
14171da177e4SLinus Torvalds 	for (;;) {
1418b4d629a3SJeff Layton 		error = posix_lock_inode(inode, fl, NULL);
1419bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
14201da177e4SLinus Torvalds 			break;
14214ca52f53SJeff Layton 		error = wait_event_interruptible(fl->c.flc_wait,
14224ca52f53SJeff Layton 						 list_empty(&fl->c.flc_blocked_member));
142316306a61SNeilBrown 		if (error)
14241da177e4SLinus Torvalds 			break;
14251da177e4SLinus Torvalds 	}
142616306a61SNeilBrown 	locks_delete_block(fl);
14271da177e4SLinus Torvalds 	return error;
14281da177e4SLinus Torvalds }
142929d01b22SJeff Layton 
lease_clear_pending(struct file_lease * fl,int arg)1430c69ff407SJeff Layton static void lease_clear_pending(struct file_lease *fl, int arg)
1431778fc546SJ. Bruce Fields {
1432778fc546SJ. Bruce Fields 	switch (arg) {
1433778fc546SJ. Bruce Fields 	case F_UNLCK:
14344ca52f53SJeff Layton 		fl->c.flc_flags &= ~FL_UNLOCK_PENDING;
1435df561f66SGustavo A. R. Silva 		fallthrough;
1436778fc546SJ. Bruce Fields 	case F_RDLCK:
14374ca52f53SJeff Layton 		fl->c.flc_flags &= ~FL_DOWNGRADE_PENDING;
1438778fc546SJ. Bruce Fields 	}
1439778fc546SJ. Bruce Fields }
1440778fc546SJ. Bruce Fields 
14411da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
lease_modify(struct file_lease * fl,int arg,struct list_head * dispose)1442c69ff407SJeff Layton int lease_modify(struct file_lease *fl, int arg, struct list_head *dispose)
14431da177e4SLinus Torvalds {
1444d9077f7bSJeff Layton 	int error = assign_type(&fl->c, arg);
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	if (error)
14471da177e4SLinus Torvalds 		return error;
1448778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
1449347d49fdSJeff Layton 	locks_wake_up_blocks(&fl->c);
14503b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
14514ca52f53SJeff Layton 		struct file *filp = fl->c.flc_file;
14523b6e2723SFilipe Brandenburger 
14533b6e2723SFilipe Brandenburger 		f_delown(filp);
14543b6e2723SFilipe Brandenburger 		file_f_owner(filp)->signum = 0;
14554ca52f53SJeff Layton 		fasync_helper(0, fl->c.flc_file, 0, &fl->fl_fasync);
145696d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
145796d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
145896d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
145996d6d59cSJ. Bruce Fields 		}
14607c18509bSJeff Layton 		locks_delete_lock_ctx(&fl->c, dispose);
14613b6e2723SFilipe Brandenburger 	}
14621da177e4SLinus Torvalds 	return 0;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
14651da177e4SLinus Torvalds 
past_time(unsigned long then)1466778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1467778fc546SJ. Bruce Fields {
1468778fc546SJ. Bruce Fields 	if (!then)
1469778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1470778fc546SJ. Bruce Fields 		return false;
1471778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1472778fc546SJ. Bruce Fields }
1473778fc546SJ. Bruce Fields 
time_out_leases(struct inode * inode,struct list_head * dispose)1474c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
14751da177e4SLinus Torvalds {
14768634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
1477c69ff407SJeff Layton 	struct file_lease *fl, *tmp;
14781da177e4SLinus Torvalds 
14796109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1480f82b4b67SJeff Layton 
14814ca52f53SJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) {
148262af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1483778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
14847448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1485778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
14867448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
leases_conflict(struct file_lock_core * lc,struct file_lock_core * bc)1490b6be3714SJeff Layton static bool leases_conflict(struct file_lock_core *lc, struct file_lock_core *bc)
1491df4e8d2cSJ. Bruce Fields {
1492d51f527fSIra Weiny 	bool rc;
1493c69ff407SJeff Layton 	struct file_lease *lease = file_lease(lc);
1494c69ff407SJeff Layton 	struct file_lease *breaker = file_lease(bc);
1495d51f527fSIra Weiny 
149628df3d15SJ. Bruce Fields 	if (lease->fl_lmops->lm_breaker_owns_lease
149728df3d15SJ. Bruce Fields 			&& lease->fl_lmops->lm_breaker_owns_lease(lease))
149828df3d15SJ. Bruce Fields 		return false;
1499b6be3714SJeff Layton 	if ((bc->flc_flags & FL_LAYOUT) != (lc->flc_flags & FL_LAYOUT)) {
1500d51f527fSIra Weiny 		rc = false;
1501d51f527fSIra Weiny 		goto trace;
1502d51f527fSIra Weiny 	}
1503b6be3714SJeff Layton 	if ((bc->flc_flags & FL_DELEG) && (lc->flc_flags & FL_LEASE)) {
1504d51f527fSIra Weiny 		rc = false;
1505d51f527fSIra Weiny 		goto trace;
1506d51f527fSIra Weiny 	}
1507d51f527fSIra Weiny 
1508b6be3714SJeff Layton 	rc = locks_conflict(bc, lc);
1509d51f527fSIra Weiny trace:
1510d51f527fSIra Weiny 	trace_leases_conflict(rc, lease, breaker);
1511d51f527fSIra Weiny 	return rc;
1512df4e8d2cSJ. Bruce Fields }
1513df4e8d2cSJ. Bruce Fields 
151403d12ddfSJeff Layton static bool
any_leases_conflict(struct inode * inode,struct file_lease * breaker)1515c69ff407SJeff Layton any_leases_conflict(struct inode *inode, struct file_lease *breaker)
151603d12ddfSJeff Layton {
15178634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
1518b6be3714SJeff Layton 	struct file_lock_core *flc;
151903d12ddfSJeff Layton 
15206109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
152103d12ddfSJeff Layton 
1522b6be3714SJeff Layton 	list_for_each_entry(flc, &ctx->flc_lease, flc_list) {
1523b6be3714SJeff Layton 		if (leases_conflict(flc, &breaker->c))
152403d12ddfSJeff Layton 			return true;
152503d12ddfSJeff Layton 	}
152603d12ddfSJeff Layton 	return false;
152703d12ddfSJeff Layton }
152803d12ddfSJeff Layton 
15291da177e4SLinus Torvalds /**
15301da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
15311da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1532df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1533df4e8d2cSJ. Bruce Fields  *	    break all leases
1534df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1535df4e8d2cSJ. Bruce Fields  *	    only delegations
15361da177e4SLinus Torvalds  *
153787250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
153887250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
153987250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
15401da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
15411da177e4SLinus Torvalds  */
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)1542df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
15431da177e4SLinus Torvalds {
1544778fc546SJ. Bruce Fields 	int error = 0;
1545128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1546c69ff407SJeff Layton 	struct file_lease *new_fl, *fl, *tmp;
15471da177e4SLinus Torvalds 	unsigned long break_time;
15488737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1549c45198edSJeff Layton 	LIST_HEAD(dispose);
15501da177e4SLinus Torvalds 
15518737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
15526d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
15536d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
15544ca52f53SJeff Layton 	new_fl->c.flc_flags = type;
15551da177e4SLinus Torvalds 
15568634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
1557401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
15588634b51fSJeff Layton 	if (!ctx) {
15598634b51fSJeff Layton 		WARN_ON_ONCE(1);
1560cfddf9f4SWenwen Wang 		goto free_lock;
15618634b51fSJeff Layton 	}
15628634b51fSJeff Layton 
156302e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
15646109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
15651da177e4SLinus Torvalds 
1566c45198edSJeff Layton 	time_out_leases(inode, &dispose);
15671da177e4SLinus Torvalds 
156803d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1569df4e8d2cSJ. Bruce Fields 		goto out;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	break_time = 0;
15721da177e4SLinus Torvalds 	if (lease_break_time > 0) {
15731da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
15741da177e4SLinus Torvalds 		if (break_time == 0)
15751da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
15761da177e4SLinus Torvalds 	}
15771da177e4SLinus Torvalds 
15784ca52f53SJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list) {
1579b6be3714SJeff Layton 		if (!leases_conflict(&fl->c, &new_fl->c))
1580df4e8d2cSJ. Bruce Fields 			continue;
1581778fc546SJ. Bruce Fields 		if (want_write) {
15824ca52f53SJeff Layton 			if (fl->c.flc_flags & FL_UNLOCK_PENDING)
1583778fc546SJ. Bruce Fields 				continue;
15844ca52f53SJeff Layton 			fl->c.flc_flags |= FL_UNLOCK_PENDING;
15851da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1586778fc546SJ. Bruce Fields 		} else {
15878634b51fSJeff Layton 			if (lease_breaking(fl))
1588778fc546SJ. Bruce Fields 				continue;
15894ca52f53SJeff Layton 			fl->c.flc_flags |= FL_DOWNGRADE_PENDING;
1590778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
15911da177e4SLinus Torvalds 		}
15924d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
15937c18509bSJeff Layton 			locks_delete_lock_ctx(&fl->c, &dispose);
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
15968634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
15974d01b7f5SJeff Layton 		goto out;
15984d01b7f5SJeff Layton 
1599843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
160062af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
16011da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
16021da177e4SLinus Torvalds 		goto out;
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds restart:
1606c69ff407SJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lease, c.flc_list);
16078634b51fSJeff Layton 	break_time = fl->fl_break_time;
1608f1c6bb2cSJeff Layton 	if (break_time != 0)
16091da177e4SLinus Torvalds 		break_time -= jiffies;
16101da177e4SLinus Torvalds 	if (break_time == 0)
16111da177e4SLinus Torvalds 		break_time++;
1612269a6194SJeff Layton 	locks_insert_block(&fl->c, &new_fl->c, leases_conflict);
161362af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
16146109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
161502e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1616aba37660SPeter Zijlstra 
1617c45198edSJeff Layton 	locks_dispose_list(&dispose);
16184ca52f53SJeff Layton 	error = wait_event_interruptible_timeout(new_fl->c.flc_wait,
16194ca52f53SJeff Layton 						 list_empty(&new_fl->c.flc_blocked_member),
1620dcf23ac3SLinus Torvalds 						 break_time);
1621aba37660SPeter Zijlstra 
162202e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
16236109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
162462af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
1625c69ff407SJeff Layton 	__locks_delete_block(&new_fl->c);
16261da177e4SLinus Torvalds 	if (error >= 0) {
1627778fc546SJ. Bruce Fields 		/*
1628778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1629778fc546SJ. Bruce Fields 		 * broken yet
1630778fc546SJ. Bruce Fields 		 */
163103d12ddfSJeff Layton 		if (error == 0)
163203d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
163303d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
16341da177e4SLinus Torvalds 			goto restart;
16351da177e4SLinus Torvalds 		error = 0;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds out:
16386109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
163902e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1640c45198edSJeff Layton 	locks_dispose_list(&dispose);
1641cfddf9f4SWenwen Wang free_lock:
1642c69ff407SJeff Layton 	locks_free_lease(new_fl);
16431da177e4SLinus Torvalds 	return error;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds /**
164876c47948SAmir Goldstein  *	lease_get_mtime - update modified time of an inode with exclusive lease
16491da177e4SLinus Torvalds  *	@inode: the inode
165076c47948SAmir Goldstein  *      @time:  pointer to a timespec which contains the last modified time
16511da177e4SLinus Torvalds  *
16521da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
16531da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1654a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
16551da177e4SLinus Torvalds  */
lease_get_mtime(struct inode * inode,struct timespec64 * time)165695582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time)
16571da177e4SLinus Torvalds {
1658bfe86024SJeff Layton 	bool has_lease = false;
1659128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1660c69ff407SJeff Layton 	struct file_lock_core *flc;
1661bfe86024SJeff Layton 
1662401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
16638634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
16646109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1665c69ff407SJeff Layton 		flc = list_first_entry_or_null(&ctx->flc_lease,
1666c69ff407SJeff Layton 					       struct file_lock_core, flc_list);
1667c69ff407SJeff Layton 		if (flc && flc->flc_type == F_WRLCK)
1668bfe86024SJeff Layton 			has_lease = true;
16696109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1670bfe86024SJeff Layton 	}
1671bfe86024SJeff Layton 
1672bfe86024SJeff Layton 	if (has_lease)
1673c2050a45SDeepa Dinamani 		*time = current_time(inode);
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds /**
16781da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
16791da177e4SLinus Torvalds  *	@filp: the file
16801da177e4SLinus Torvalds  *
16811da177e4SLinus Torvalds  *	The value returned by this function will be one of
16821da177e4SLinus Torvalds  *	(if no lease break is pending):
16831da177e4SLinus Torvalds  *
16841da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
16851da177e4SLinus Torvalds  *
16861da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
16871da177e4SLinus Torvalds  *
16881da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
16891da177e4SLinus Torvalds  *
16901da177e4SLinus Torvalds  *	(if a lease break is pending):
16911da177e4SLinus Torvalds  *
16921da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
16931da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
16941da177e4SLinus Torvalds  *
16951da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
16961da177e4SLinus Torvalds  *
16971da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
16981da177e4SLinus Torvalds  *	should be returned to userspace.
16991da177e4SLinus Torvalds  */
fcntl_getlease(struct file * filp)17001da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
17011da177e4SLinus Torvalds {
1702c69ff407SJeff Layton 	struct file_lease *fl;
1703c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
1704128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
17051da177e4SLinus Torvalds 	int type = F_UNLCK;
1706c45198edSJeff Layton 	LIST_HEAD(dispose);
17071da177e4SLinus Torvalds 
1708401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
17098634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
171002e525b2SPeter Zijlstra 		percpu_down_read(&file_rwsem);
17116109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1712c568d683SMiklos Szeredi 		time_out_leases(inode, &dispose);
17134ca52f53SJeff Layton 		list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
17144ca52f53SJeff Layton 			if (fl->c.flc_file != filp)
17158634b51fSJeff Layton 				continue;
1716778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
17171da177e4SLinus Torvalds 			break;
17181da177e4SLinus Torvalds 		}
17196109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
172002e525b2SPeter Zijlstra 		percpu_up_read(&file_rwsem);
17215f43086bSPeter Zijlstra 
1722c45198edSJeff Layton 		locks_dispose_list(&dispose);
17238634b51fSJeff Layton 	}
17241da177e4SLinus Torvalds 	return type;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
172724cbe784SJeff Layton /**
1728387e3746SAmir Goldstein  * check_conflicting_open - see if the given file points to an inode that has
172924cbe784SJeff Layton  *			    an existing open that would conflict with the
173024cbe784SJeff Layton  *			    desired lease.
1731387e3746SAmir Goldstein  * @filp:	file to check
173224cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
17337fadc59cSRandy Dunlap  * @flags:	current lock flags
173424cbe784SJeff Layton  *
173524cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
173624cbe784SJeff Layton  * conflict with the lease we're trying to set.
173724cbe784SJeff Layton  */
173824cbe784SJeff Layton static int
check_conflicting_open(struct file * filp,const int arg,int flags)1739ed5f17f6SLuca Vizzarro check_conflicting_open(struct file *filp, const int arg, int flags)
174024cbe784SJeff Layton {
1741c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
1742387e3746SAmir Goldstein 	int self_wcount = 0, self_rcount = 0;
174324cbe784SJeff Layton 
174411afe9f7SChristoph Hellwig 	if (flags & FL_LAYOUT)
174511afe9f7SChristoph Hellwig 		return 0;
1746aba2072fSJ. Bruce Fields 	if (flags & FL_DELEG)
1747aba2072fSJ. Bruce Fields 		/* We leave these checks to the caller */
1748aba2072fSJ. Bruce Fields 		return 0;
174911afe9f7SChristoph Hellwig 
1750387e3746SAmir Goldstein 	if (arg == F_RDLCK)
1751387e3746SAmir Goldstein 		return inode_is_open_for_write(inode) ? -EAGAIN : 0;
1752387e3746SAmir Goldstein 	else if (arg != F_WRLCK)
1753387e3746SAmir Goldstein 		return 0;
1754387e3746SAmir Goldstein 
1755387e3746SAmir Goldstein 	/*
1756387e3746SAmir Goldstein 	 * Make sure that only read/write count is from lease requestor.
1757387e3746SAmir Goldstein 	 * Note that this will result in denying write leases when i_writecount
1758387e3746SAmir Goldstein 	 * is negative, which is what we want.  (We shouldn't grant write leases
1759387e3746SAmir Goldstein 	 * on files open for execution.)
1760387e3746SAmir Goldstein 	 */
1761387e3746SAmir Goldstein 	if (filp->f_mode & FMODE_WRITE)
1762387e3746SAmir Goldstein 		self_wcount = 1;
1763387e3746SAmir Goldstein 	else if (filp->f_mode & FMODE_READ)
1764387e3746SAmir Goldstein 		self_rcount = 1;
1765387e3746SAmir Goldstein 
1766387e3746SAmir Goldstein 	if (atomic_read(&inode->i_writecount) != self_wcount ||
1767387e3746SAmir Goldstein 	    atomic_read(&inode->i_readcount) != self_rcount)
176824cbe784SJeff Layton 		return -EAGAIN;
176924cbe784SJeff Layton 
1770387e3746SAmir Goldstein 	return 0;
177124cbe784SJeff Layton }
177224cbe784SJeff Layton 
1773e6f5c789SJeff Layton static int
generic_add_lease(struct file * filp,int arg,struct file_lease ** flp,void ** priv)1774c69ff407SJeff Layton generic_add_lease(struct file *filp, int arg, struct file_lease **flp, void **priv)
17751da177e4SLinus Torvalds {
1776c69ff407SJeff Layton 	struct file_lease *fl, *my_fl = NULL, *lease;
1777c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
17788634b51fSJeff Layton 	struct file_lock_context *ctx;
17794ca52f53SJeff Layton 	bool is_deleg = (*flp)->c.flc_flags & FL_DELEG;
1780c1f24ef4SJ. Bruce Fields 	int error;
1781c45198edSJeff Layton 	LIST_HEAD(dispose);
17821da177e4SLinus Torvalds 
1783096657b6SJ. Bruce Fields 	lease = *flp;
178462af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
178562af4f1fSJeff Layton 
17865c1c669aSJeff Layton 	error = file_f_owner_allocate(filp);
17875c1c669aSJeff Layton 	if (error)
17888634b51fSJeff Layton 		return error;
17898634b51fSJeff Layton 
17908634b51fSJeff Layton 	/* Note that arg is never F_UNLCK here */
1791df4e8d2cSJ. Bruce Fields 	ctx = locks_get_lock_context(inode, arg);
1792df4e8d2cSJ. Bruce Fields 	if (!ctx)
1793df4e8d2cSJ. Bruce Fields 		return -ENOMEM;
1794df4e8d2cSJ. Bruce Fields 
1795df4e8d2cSJ. Bruce Fields 	/*
1796df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1797df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1798df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
17995955102cSAl Viro 	 * there's some chance of a conflict--we'd rather not
1800df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1801df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
180202e525b2SPeter Zijlstra 	 */
18036109c850SJeff Layton 	if (is_deleg && !inode_trylock(inode))
1804c45198edSJeff Layton 		return -EAGAIN;
18054ca52f53SJeff Layton 
180624cbe784SJeff Layton 	percpu_down_read(&file_rwsem);
18071da177e4SLinus Torvalds 	spin_lock(&ctx->flc_lock);
180885c59580SPavel Emelyanov 	time_out_leases(inode, &dispose);
18091da177e4SLinus Torvalds 	error = check_conflicting_open(filp, arg, lease->c.flc_flags);
18101da177e4SLinus Torvalds 	if (error)
18111da177e4SLinus Torvalds 		goto out;
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	/*
18141da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
18151da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
18161da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
1817c1f24ef4SJ. Bruce Fields 	 * And if we are trying to acquire an exclusive lease,
18184ca52f53SJeff Layton 	 * then the file is not open by anyone (including us)
18194ca52f53SJeff Layton 	 * except for this filp.
18204ca52f53SJeff Layton 	 */
18218634b51fSJeff Layton 	error = -EAGAIN;
1822c1f24ef4SJ. Bruce Fields 	list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
18231da177e4SLinus Torvalds 		if (fl->c.flc_file == filp &&
18248634b51fSJeff Layton 		    fl->c.flc_owner == lease->c.flc_owner) {
1825c1f24ef4SJ. Bruce Fields 			my_fl = fl;
1826c1f24ef4SJ. Bruce Fields 			continue;
1827c1f24ef4SJ. Bruce Fields 		}
1828c1f24ef4SJ. Bruce Fields 
1829c1f24ef4SJ. Bruce Fields 		/*
18301da177e4SLinus Torvalds 		 * No exclusive leases if someone else has a lease on
1831c1f24ef4SJ. Bruce Fields 		 * this file:
1832c1f24ef4SJ. Bruce Fields 		 */
1833c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
1834c1f24ef4SJ. Bruce Fields 			goto out;
18354ca52f53SJeff Layton 		/*
1836c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1837c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
18381da177e4SLinus Torvalds 		 */
18398634b51fSJeff Layton 		if (fl->c.flc_flags & FL_UNLOCK_PENDING)
18400164bf02SJeff Layton 			goto out;
18410164bf02SJeff Layton 	}
18421c7dd2ffSJeff Layton 
18431da177e4SLinus Torvalds 	if (my_fl != NULL) {
18441c7dd2ffSJeff Layton 		lease = my_fl;
18451da177e4SLinus Torvalds 		error = lease->fl_lmops->lm_change(lease, arg, &dispose);
18461da177e4SLinus Torvalds 		if (error)
18471da177e4SLinus Torvalds 			goto out;
18481da177e4SLinus Torvalds 		goto out_setup;
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 
18517c18509bSJeff Layton 	error = -EINVAL;
185224cbe784SJeff Layton 	if (!leases_enable)
185324cbe784SJeff Layton 		goto out;
185424cbe784SJeff Layton 
185524cbe784SJeff Layton 	locks_insert_lock_ctx(&lease->c, &ctx->flc_lease);
185624cbe784SJeff Layton 	/*
185724cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
185824cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
185924cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
186024cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
186124cbe784SJeff Layton 	 *
18624ca52f53SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
18638634b51fSJeff Layton 	 * precedes these checks.
18647c18509bSJeff Layton 	 */
18658634b51fSJeff Layton 	smp_mb();
18668634b51fSJeff Layton 	error = check_conflicting_open(filp, arg, lease->c.flc_flags);
18671c7dd2ffSJeff Layton 	if (error) {
18681c7dd2ffSJeff Layton 		locks_unlink_lock_ctx(&lease->c);
18691c7dd2ffSJeff Layton 		goto out;
18701c7dd2ffSJeff Layton 	}
18711da177e4SLinus Torvalds 
18726109c850SJeff Layton out_setup:
187302e525b2SPeter Zijlstra 	if (lease->fl_lmops->lm_setup)
1874c45198edSJeff Layton 		lease->fl_lmops->lm_setup(lease, priv);
1875df4e8d2cSJ. Bruce Fields out:
18765955102cSAl Viro 	spin_unlock(&ctx->flc_lock);
18778634b51fSJeff Layton 	percpu_up_read(&file_rwsem);
18781c7dd2ffSJeff Layton 	locks_dispose_list(&dispose);
18791da177e4SLinus Torvalds 	if (is_deleg)
18801da177e4SLinus Torvalds 		inode_unlock(inode);
18818335ebd9SJ. Bruce Fields 	if (!error && !my_fl)
18822ab99ee1SChristoph Hellwig 		*flp = NULL;
18838335ebd9SJ. Bruce Fields 	return error;
18840efaa7e8SJeff Layton }
1885c69ff407SJeff Layton 
generic_delete_lease(struct file * filp,void * owner)1886c65454a9SJeff Layton static int generic_delete_lease(struct file *filp, void *owner)
1887128a3785SDmitry Vyukov {
1888c45198edSJeff Layton 	int error = -EAGAIN;
18898335ebd9SJ. Bruce Fields 	struct file_lease *fl, *victim = NULL;
1890401a8b8fSJeff Layton 	struct inode *inode = file_inode(filp);
18918634b51fSJeff Layton 	struct file_lock_context *ctx;
18928634b51fSJeff Layton 	LIST_HEAD(dispose);
18938634b51fSJeff Layton 
18948634b51fSJeff Layton 	ctx = locks_inode_context(inode);
18958634b51fSJeff Layton 	if (!ctx) {
189602e525b2SPeter Zijlstra 		trace_generic_delete_lease(inode, NULL);
18976109c850SJeff Layton 		return error;
18984ca52f53SJeff Layton 	}
18994ca52f53SJeff Layton 
19004ca52f53SJeff Layton 	percpu_down_read(&file_rwsem);
19018634b51fSJeff Layton 	spin_lock(&ctx->flc_lock);
19020efaa7e8SJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, c.flc_list) {
19038335ebd9SJ. Bruce Fields 		if (fl->c.flc_file == filp &&
19048634b51fSJeff Layton 		    fl->c.flc_owner == owner) {
1905a9b1b455SJeff Layton 			victim = fl;
19068634b51fSJeff Layton 			break;
19077448cc37SJeff Layton 		}
19086109c850SJeff Layton 	}
190902e525b2SPeter Zijlstra 	trace_generic_delete_lease(inode, victim);
1910c45198edSJeff Layton 	if (victim)
19110efaa7e8SJeff Layton 		error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
19128335ebd9SJ. Bruce Fields 	spin_unlock(&ctx->flc_lock);
19138335ebd9SJ. Bruce Fields 	percpu_up_read(&file_rwsem);
19141da177e4SLinus Torvalds 	locks_dispose_list(&dispose);
19151da177e4SLinus Torvalds 	return error;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds /**
19191c7dd2ffSJeff Layton  *	generic_setlease	-	sets a lease on an open file
19201c7dd2ffSJeff Layton  *	@filp:	file pointer
19211da177e4SLinus Torvalds  *	@arg:	type of lease to obtain
19221da177e4SLinus Torvalds  *	@flp:	input - file_lock to use, output - file_lock inserted
19231da177e4SLinus Torvalds  *	@priv:	private data for lm_setup (may be NULL if lm_setup
19241da177e4SLinus Torvalds  *		doesn't require it)
1925c69ff407SJeff Layton  *
1926e6f5c789SJeff Layton  *	The (input) flp->fl_lmops->lm_break function is required
19271da177e4SLinus Torvalds  *	by break_lease().
19288335ebd9SJ. Bruce Fields  */
generic_setlease(struct file * filp,int arg,struct file_lease ** flp,void ** priv)19298335ebd9SJ. Bruce Fields int generic_setlease(struct file *filp, int arg, struct file_lease **flp,
19302ab99ee1SChristoph Hellwig 			void **priv)
19318335ebd9SJ. Bruce Fields {
19328335ebd9SJ. Bruce Fields 	switch (arg) {
19330efaa7e8SJeff Layton 	case F_UNLCK:
19340efaa7e8SJeff Layton 		return generic_delete_lease(filp, *priv);
19350efaa7e8SJeff Layton 	case F_RDLCK:
19360efaa7e8SJeff Layton 	case F_WRLCK:
193711afe9f7SChristoph Hellwig 		if (!(*flp)->fl_lmops->lm_break) {
1938e6f5c789SJeff Layton 			WARN_ON_ONCE(1);
19398335ebd9SJ. Bruce Fields 			return -ENOLCK;
19408d657eb3SDave Jones 		}
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 		return generic_add_lease(filp, arg, flp, priv);
19430af1a450SChristoph Hellwig 	default:
19441da177e4SLinus Torvalds 		return -EINVAL;
194518f6622eSJeff Layton 	}
194618f6622eSJeff Layton }
194718f6622eSJeff Layton EXPORT_SYMBOL(generic_setlease);
194818f6622eSJeff Layton 
194918f6622eSJeff Layton /*
195018f6622eSJeff Layton  * Kernel subsystems can register to be notified on any attempt to set
195118f6622eSJeff Layton  * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd
195218f6622eSJeff Layton  * to close files that it may have cached when there is an attempt to set a
195318f6622eSJeff Layton  * conflicting lease.
195418f6622eSJeff Layton  */
195518f6622eSJeff Layton static struct srcu_notifier_head lease_notifier_chain;
195618f6622eSJeff Layton 
195718f6622eSJeff Layton static inline void
lease_notifier_chain_init(void)195818f6622eSJeff Layton lease_notifier_chain_init(void)
195918f6622eSJeff Layton {
1960c69ff407SJeff Layton 	srcu_init_notifier_head(&lease_notifier_chain);
196118f6622eSJeff Layton }
196218f6622eSJeff Layton 
196318f6622eSJeff Layton static inline void
setlease_notifier(int arg,struct file_lease * lease)196418f6622eSJeff Layton setlease_notifier(int arg, struct file_lease *lease)
196518f6622eSJeff Layton {
196618f6622eSJeff Layton 	if (arg != F_UNLCK)
196718f6622eSJeff Layton 		srcu_notifier_call_chain(&lease_notifier_chain, arg, lease);
196818f6622eSJeff Layton }
196918f6622eSJeff Layton 
lease_register_notifier(struct notifier_block * nb)197018f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb)
197118f6622eSJeff Layton {
197218f6622eSJeff Layton 	return srcu_notifier_chain_register(&lease_notifier_chain, nb);
197318f6622eSJeff Layton }
197418f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier);
197518f6622eSJeff Layton 
lease_unregister_notifier(struct notifier_block * nb)197618f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb)
197718f6622eSJeff Layton {
19787b800101SJeff Layton 	srcu_notifier_chain_unregister(&lease_notifier_chain, nb);
19797b800101SJeff Layton }
19807b800101SJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier);
19817b800101SJeff Layton 
19827b800101SJeff Layton 
19837b800101SJeff Layton int
kernel_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)19847b800101SJeff Layton kernel_setlease(struct file *filp, int arg, struct file_lease **lease, void **priv)
19857b800101SJeff Layton {
19867b800101SJeff Layton 	if (lease)
19877b800101SJeff Layton 		setlease_notifier(arg, *lease);
19887b800101SJeff Layton 	if (filp->f_op->setlease)
19897b800101SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
19907b800101SJeff Layton 	else
19911da177e4SLinus Torvalds 		return generic_setlease(filp, arg, lease, priv);
1992a9933ceaSJ. Bruce Fields }
19931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(kernel_setlease);
19941da177e4SLinus Torvalds 
1995e51673aaSJeff Layton /**
19961c7dd2ffSJeff Layton  * vfs_setlease        -       sets a lease on an open file
19971c7dd2ffSJeff Layton  * @filp:	file pointer
19981da177e4SLinus Torvalds  * @arg:	type of lease to obtain
1999e51673aaSJeff Layton  * @lease:	file_lock to use when adding a lease
2000e51673aaSJeff Layton  * @priv:	private info for lm_setup when adding a lease (may be
200180b79dd0SMauro Carvalho Chehab  *		NULL if lm_setup doesn't require it)
200280b79dd0SMauro Carvalho Chehab  *
2003e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
20041c7dd2ffSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
20051c7dd2ffSJeff Layton  * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
20061c7dd2ffSJeff Layton  * set; if not, this function will return -ENOLCK (and generate a scary-looking
20071da177e4SLinus Torvalds  * stack trace).
2008e6f5c789SJeff Layton  *
2009c69ff407SJeff Layton  * The "priv" pointer is passed directly to the lm_setup function as-is. It
20101da177e4SLinus Torvalds  * may be NULL if the lm_setup operation doesn't require it.
20117b800101SJeff Layton  */
20127b800101SJeff Layton int
vfs_setlease(struct file * filp,int arg,struct file_lease ** lease,void ** priv)20137b800101SJeff Layton vfs_setlease(struct file *filp, int arg, struct file_lease **lease, void **priv)
20147b800101SJeff Layton {
20157b800101SJeff Layton 	struct inode *inode = file_inode(filp);
20167b800101SJeff Layton 	vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(filp), inode);
20177b800101SJeff Layton 	int error;
20187b800101SJeff Layton 
20197b800101SJeff Layton 	if ((!vfsuid_eq_kuid(vfsuid, current_fsuid())) && !capable(CAP_LEASE))
20207b800101SJeff Layton 		return -EACCES;
20217b800101SJeff Layton 	if (!S_ISREG(inode->i_mode))
20227b800101SJeff Layton 		return -EINVAL;
20231da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
2024a9933ceaSJ. Bruce Fields 	if (error)
20251da177e4SLinus Torvalds 		return error;
2026ed5f17f6SLuca Vizzarro 	return kernel_setlease(filp, arg, lease, priv);
20271da177e4SLinus Torvalds }
2028c69ff407SJeff Layton EXPORT_SYMBOL_GPL(vfs_setlease);
2029f7347ce4SLinus Torvalds 
do_fcntl_add_lease(unsigned int fd,struct file * filp,int arg)20301da177e4SLinus Torvalds static int do_fcntl_add_lease(unsigned int fd, struct file *filp, int arg)
20311da177e4SLinus Torvalds {
2032c5b1f0d9SArnd Bergmann 	struct file_lease *fl;
2033c5b1f0d9SArnd Bergmann 	struct fasync_struct *new;
2034c5b1f0d9SArnd Bergmann 	int error;
20351da177e4SLinus Torvalds 
2036f7347ce4SLinus Torvalds 	fl = lease_alloc(filp, arg);
2037f7347ce4SLinus Torvalds 	if (IS_ERR(fl))
2038c69ff407SJeff Layton 		return PTR_ERR(fl);
2039f7347ce4SLinus Torvalds 
2040f7347ce4SLinus Torvalds 	new = fasync_alloc();
20411c7dd2ffSJeff Layton 	if (!new) {
20421da177e4SLinus Torvalds 		locks_free_lease(fl);
20431c7dd2ffSJeff Layton 		return -ENOMEM;
20442dfb928fSJeff Layton 	}
2045c69ff407SJeff Layton 	new->fa_fd = fd;
2046f7347ce4SLinus Torvalds 
2047f7347ce4SLinus Torvalds 	error = vfs_setlease(filp, arg, &fl, (void **)&new);
20481da177e4SLinus Torvalds 	if (fl)
20491da177e4SLinus Torvalds 		locks_free_lease(fl);
20501da177e4SLinus Torvalds 	if (new)
20511da177e4SLinus Torvalds 		fasync_free(new);
20520ceaf6c7SJ. Bruce Fields 	return error;
20530ceaf6c7SJ. Bruce Fields }
20540ceaf6c7SJ. Bruce Fields 
20550ceaf6c7SJ. Bruce Fields /**
20560ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
20570ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
20580ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
20590ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
20600ceaf6c7SJ. Bruce Fields  *
2061ed5f17f6SLuca Vizzarro  *	Call this fcntl to establish a lease on the file.
20620ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
20630ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
20642ab99ee1SChristoph Hellwig  */
fcntl_setlease(unsigned int fd,struct file * filp,int arg)20650ceaf6c7SJ. Bruce Fields int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
20660ceaf6c7SJ. Bruce Fields {
20670ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
20680ceaf6c7SJ. Bruce Fields 		return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
206929d01b22SJeff Layton 	return do_fcntl_add_lease(fd, filp, arg);
207029d01b22SJeff Layton }
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds /**
207329d01b22SJeff Layton  * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
20741da177e4SLinus Torvalds  * @inode: inode of the file to apply to
2075616fb38fSBenjamin Coddington  * @fl: The lock to be applied
20761da177e4SLinus Torvalds  *
20771da177e4SLinus Torvalds  * Apply a FLOCK style lock request to an inode.
20781da177e4SLinus Torvalds  */
flock_lock_inode_wait(struct inode * inode,struct file_lock * fl)20791da177e4SLinus Torvalds static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
208029d01b22SJeff Layton {
2081bde74e4bSMiklos Szeredi 	int error;
20821da177e4SLinus Torvalds 	might_sleep();
20834ca52f53SJeff Layton 	for (;;) {
20844ca52f53SJeff Layton 		error = flock_lock_inode(inode, fl);
208516306a61SNeilBrown 		if (error != FILE_LOCK_DEFERRED)
20861da177e4SLinus Torvalds 			break;
20871da177e4SLinus Torvalds 		error = wait_event_interruptible(fl->c.flc_wait,
208816306a61SNeilBrown 						 list_empty(&fl->c.flc_blocked_member));
20891da177e4SLinus Torvalds 		if (error)
20901da177e4SLinus Torvalds 			break;
20911da177e4SLinus Torvalds 	}
209229d01b22SJeff Layton 	locks_delete_block(fl);
2093e55c34a6SBenjamin Coddington 	return error;
2094e55c34a6SBenjamin Coddington }
2095e55c34a6SBenjamin Coddington 
2096e55c34a6SBenjamin Coddington /**
2097e55c34a6SBenjamin Coddington  * locks_lock_inode_wait - Apply a lock to an inode
2098e55c34a6SBenjamin Coddington  * @inode: inode of the file to apply to
2099e55c34a6SBenjamin Coddington  * @fl: The lock to be applied
2100e55c34a6SBenjamin Coddington  *
2101e55c34a6SBenjamin Coddington  * Apply a POSIX or FLOCK style lock request to an inode.
21024ca52f53SJeff Layton  */
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)2103e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
2104e55c34a6SBenjamin Coddington {
2105e55c34a6SBenjamin Coddington 	int res = 0;
2106e55c34a6SBenjamin Coddington 	switch (fl->c.flc_flags & (FL_POSIX|FL_FLOCK)) {
2107e55c34a6SBenjamin Coddington 		case FL_POSIX:
2108e55c34a6SBenjamin Coddington 			res = posix_lock_inode_wait(inode, fl);
2109e55c34a6SBenjamin Coddington 			break;
2110e55c34a6SBenjamin Coddington 		case FL_FLOCK:
2111e55c34a6SBenjamin Coddington 			res = flock_lock_inode_wait(inode, fl);
2112e55c34a6SBenjamin Coddington 			break;
2113e55c34a6SBenjamin Coddington 		default:
2114e55c34a6SBenjamin Coddington 			BUG();
2115e55c34a6SBenjamin Coddington 	}
2116e55c34a6SBenjamin Coddington 	return res;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds EXPORT_SYMBOL(locks_lock_inode_wait);
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds /**
21211da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
212280b79dd0SMauro Carvalho Chehab  *	@fd: the file descriptor to lock.
21231da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
212480b79dd0SMauro Carvalho Chehab  *
212580b79dd0SMauro Carvalho Chehab  *	Apply a %FL_FLOCK style lock to an open file descriptor.
212680b79dd0SMauro Carvalho Chehab  *	The @cmd can be one of:
212790f7d7a0SJeff Layton  *
21281da177e4SLinus Torvalds  *	- %LOCK_SH -- a shared lock.
212990f7d7a0SJeff Layton  *	- %LOCK_EX -- an exclusive lock.
21301da177e4SLinus Torvalds  *	- %LOCK_UN -- remove an existing lock.
2131002c8976SHeiko Carstens  *	- %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED)
21321da177e4SLinus Torvalds  *
2133db4abb4aSKuniyuki Iwashima  *	%LOCK_MAND support has been removed from the kernel.
21344149be7bSKuniyuki Iwashima  */
SYSCALL_DEFINE2(flock,unsigned int,fd,unsigned int,cmd)2135db4abb4aSKuniyuki Iwashima SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21361da177e4SLinus Torvalds {
213790f7d7a0SJeff Layton 	int can_sleep, error, type;
213890f7d7a0SJeff Layton 	struct file_lock fl;
213990f7d7a0SJeff Layton 	struct fd f;
214090f7d7a0SJeff Layton 
214190f7d7a0SJeff Layton 	/*
214290f7d7a0SJeff Layton 	 * LOCK_MAND locks were broken for a long time in that they never
214390f7d7a0SJeff Layton 	 * conflicted with one another and didn't prevent any sort of open,
214490f7d7a0SJeff Layton 	 * read or write activity.
214590f7d7a0SJeff Layton 	 *
2146f2f2494cSAndi Kleen 	 * Just ignore these requests now, to preserve legacy behavior, but
2147db4abb4aSKuniyuki Iwashima 	 * throw a warning to let people know that they don't actually work.
214890f7d7a0SJeff Layton 	 */
214990f7d7a0SJeff Layton 	if (cmd & LOCK_MAND) {
2150db4abb4aSKuniyuki Iwashima 		pr_warn_once("%s(%d): Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n", current->comm, current->pid);
2151db4abb4aSKuniyuki Iwashima 		return 0;
2152db4abb4aSKuniyuki Iwashima 	}
2153db4abb4aSKuniyuki Iwashima 
2154db4abb4aSKuniyuki Iwashima 	type = flock_translate_cmd(cmd & ~LOCK_NB);
2155db4abb4aSKuniyuki Iwashima 	if (type < 0)
2156db4abb4aSKuniyuki Iwashima 		return type;
2157db4abb4aSKuniyuki Iwashima 
2158db4abb4aSKuniyuki Iwashima 	error = -EBADF;
2159db4abb4aSKuniyuki Iwashima 	f = fdget(fd);
21601da177e4SLinus Torvalds 	if (!fd_file(f))
21616e129d00SJeff Layton 		return error;
21624149be7bSKuniyuki Iwashima 
21631da177e4SLinus Torvalds 	if (type != F_UNLCK && !(fd_file(f)->f_mode & (FMODE_READ | FMODE_WRITE)))
21644ca52f53SJeff Layton 		goto out_putf;
21651da177e4SLinus Torvalds 
21664149be7bSKuniyuki Iwashima 	flock_make_lock(fd_file(f), &fl, type);
21671da177e4SLinus Torvalds 
2168db4abb4aSKuniyuki Iwashima 	error = security_file_lock(fd_file(f), fl.c.flc_type);
2169db4abb4aSKuniyuki Iwashima 	if (error)
21704ca52f53SJeff Layton 		goto out_putf;
2171db4abb4aSKuniyuki Iwashima 
2172de2a4a50SMiklos Szeredi 	can_sleep = !(cmd & LOCK_NB);
21732903ff01SAl Viro 	if (can_sleep)
21741da177e4SLinus Torvalds 		fl.c.flc_flags |= FL_SLEEP;
21754149be7bSKuniyuki Iwashima 
21761da177e4SLinus Torvalds 	if (fd_file(f)->f_op->flock)
21774149be7bSKuniyuki Iwashima 		error = fd_file(f)->f_op->flock(fd_file(f),
21781da177e4SLinus Torvalds 					    (can_sleep) ? F_SETLKW : F_SETLK,
2179932c29a1SDavid Howells 					    &fl);
21801da177e4SLinus Torvalds 	else
21812903ff01SAl Viro 		error = locks_lock_file_wait(fd_file(f), &fl);
2182db4abb4aSKuniyuki Iwashima 
21831da177e4SLinus Torvalds 	locks_release_private(&fl);
21841da177e4SLinus Torvalds  out_putf:
21851da177e4SLinus Torvalds 	fdput(f);
21863ee17abdSJ. Bruce Fields 
21873ee17abdSJ. Bruce Fields 	return error;
21883ee17abdSJ. Bruce Fields }
21896924c554SJ. Bruce Fields 
21903ee17abdSJ. Bruce Fields /**
21913ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
21923ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
21933ee17abdSJ. Bruce Fields  * @fl: The lock to test; also used to hold result
21943ee17abdSJ. Bruce Fields  *
21953ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
21964ca52f53SJeff Layton  * setting conf->fl_type to something other than F_UNLCK.
2197de2a4a50SMiklos Szeredi  */
vfs_test_lock(struct file * filp,struct file_lock * fl)21983ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
21993ee17abdSJ. Bruce Fields {
22003ee17abdSJ. Bruce Fields 	WARN_ON_ONCE(filp != fl->c.flc_file);
22013ee17abdSJ. Bruce Fields 	if (filp->f_op->lock)
22023ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
22033ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
22049d5b86acSBenjamin Coddington 	return 0;
22059d5b86acSBenjamin Coddington }
22069d5b86acSBenjamin Coddington EXPORT_SYMBOL_GPL(vfs_test_lock);
22079d5b86acSBenjamin Coddington 
22089d5b86acSBenjamin Coddington /**
2209bd4c4680SJakub Wilk  * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
22109d5b86acSBenjamin Coddington  * @fl: The file_lock who's fl_pid should be translated
2211ae7eb16eSJeff Layton  * @ns: The namespace into which the pid should be translated
22129d5b86acSBenjamin Coddington  *
22139d5b86acSBenjamin Coddington  * Used to translate a fl_pid into a namespace virtual pid number
22149d5b86acSBenjamin Coddington  */
locks_translate_pid(struct file_lock_core * fl,struct pid_namespace * ns)22159d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock_core *fl, struct pid_namespace *ns)
2216ae7eb16eSJeff Layton {
22179d5b86acSBenjamin Coddington 	pid_t vnr;
22183d40f781SJeff Layton 	struct pid *pid;
22193d40f781SJeff Layton 
2220ae7eb16eSJeff Layton 	if (fl->flc_flags & FL_OFDLCK)
2221ae7eb16eSJeff Layton 		return -1;
22223d40f781SJeff Layton 
2223826d7bc9SKonstantin Khorenko 	/* Remote locks report a negative pid value */
2224826d7bc9SKonstantin Khorenko 	if (fl->flc_pid <= 0)
2225826d7bc9SKonstantin Khorenko 		return fl->flc_pid;
2226826d7bc9SKonstantin Khorenko 
2227826d7bc9SKonstantin Khorenko 	/*
2228826d7bc9SKonstantin Khorenko 	 * If the flock owner process is dead and its pid has been already
2229ae7eb16eSJeff Layton 	 * freed, the translation below won't work, but we still want to show
22309d5b86acSBenjamin Coddington 	 * flock owner pid number in init pidns.
22319d5b86acSBenjamin Coddington 	 */
2232ae7eb16eSJeff Layton 	if (ns == &init_pid_ns)
22339d5b86acSBenjamin Coddington 		return (pid_t) fl->flc_pid;
22349d5b86acSBenjamin Coddington 
22359d5b86acSBenjamin Coddington 	rcu_read_lock();
22369d5b86acSBenjamin Coddington 	pid = find_pid_ns(fl->flc_pid, &init_pid_ns);
22379d5b86acSBenjamin Coddington 	vnr = pid_nr_ns(pid, ns);
2238c2fa1b8aSJ. Bruce Fields 	rcu_read_unlock();
2239c2fa1b8aSJ. Bruce Fields 	return vnr;
2240ae7eb16eSJeff Layton }
2241c2fa1b8aSJ. Bruce Fields 
posix_lock_to_flock(struct flock * flock,struct file_lock * fl)2242c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2243c2fa1b8aSJ. Bruce Fields {
2244c2fa1b8aSJ. Bruce Fields 	flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current));
2245c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
2246c2fa1b8aSJ. Bruce Fields 	/*
2247c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
2248c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
2249c2fa1b8aSJ. Bruce Fields 	 */
2250c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
2251c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2252c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2253c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2254c2fa1b8aSJ. Bruce Fields #endif
22554ca52f53SJeff Layton 	flock->l_start = fl->fl_start;
2256c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2257c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2258c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2259c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->c.flc_type;
2260c2fa1b8aSJ. Bruce Fields 	return 0;
2261c2fa1b8aSJ. Bruce Fields }
2262ae7eb16eSJeff Layton 
2263c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
posix_lock_to_flock64(struct flock64 * flock,struct file_lock * fl)2264c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2265c2fa1b8aSJ. Bruce Fields {
2266c2fa1b8aSJ. Bruce Fields 	flock->l_pid = locks_translate_pid(&fl->c, task_active_pid_ns(current));
22674ca52f53SJeff Layton 	flock->l_start = fl->fl_start;
2268c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2269c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2270c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
22711da177e4SLinus Torvalds 	flock->l_type = fl->c.flc_type;
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds #endif
2274a75d30c7SChristoph Hellwig 
22751da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
227652306e88SBenjamin Coddington  * This implements the F_GETLK command of fcntl().
22771da177e4SLinus Torvalds  */
fcntl_getlk(struct file * filp,unsigned int cmd,struct flock * flock)22781da177e4SLinus Torvalds int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
227952306e88SBenjamin Coddington {
228052306e88SBenjamin Coddington 	struct file_lock *fl;
228152306e88SBenjamin Coddington 	int error;
22821da177e4SLinus Torvalds 
22836c9007f6SStas Sergeev 	fl = locks_alloc_lock();
22846c9007f6SStas Sergeev 	if (fl == NULL)
22851da177e4SLinus Torvalds 		return -ENOMEM;
22861da177e4SLinus Torvalds 	error = -EINVAL;
228752306e88SBenjamin Coddington 	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
22881da177e4SLinus Torvalds 			&& flock->l_type != F_WRLCK)
22891da177e4SLinus Torvalds 		goto out;
22901da177e4SLinus Torvalds 
22910d3f7a2dSJeff Layton 	error = flock_to_posix_lock(filp, fl, flock);
229290478939SJeff Layton 	if (error)
2293a75d30c7SChristoph Hellwig 		goto out;
229490478939SJeff Layton 
229590478939SJeff Layton 	if (cmd == F_OFD_GETLK) {
22964ca52f53SJeff Layton 		error = -EINVAL;
22974ca52f53SJeff Layton 		if (flock->l_pid != 0)
22985d50ffd7SJeff Layton 			goto out;
22995d50ffd7SJeff Layton 
230052306e88SBenjamin Coddington 		fl->c.flc_flags |= FL_OFDLCK;
23013ee17abdSJ. Bruce Fields 		fl->c.flc_owner = filp;
23021da177e4SLinus Torvalds 	}
23031da177e4SLinus Torvalds 
23044ca52f53SJeff Layton 	error = vfs_test_lock(filp, fl);
23054ca52f53SJeff Layton 	if (error)
230652306e88SBenjamin Coddington 		goto out;
2307c2fa1b8aSJ. Bruce Fields 
230852306e88SBenjamin Coddington 	flock->l_type = fl->c.flc_type;
23091da177e4SLinus Torvalds 	if (fl->c.flc_type != F_UNLCK) {
23101da177e4SLinus Torvalds 		error = posix_lock_to_flock(flock, fl);
231152306e88SBenjamin Coddington 		if (error)
23121da177e4SLinus Torvalds 			goto out;
23131da177e4SLinus Torvalds 	}
23141da177e4SLinus Torvalds out:
23157723ec97SMarc Eshel 	locks_free_lock(fl);
23167723ec97SMarc Eshel 	return error;
23177723ec97SMarc Eshel }
23187723ec97SMarc Eshel 
23197723ec97SMarc Eshel /**
2320150b3934SMarc Eshel  * vfs_lock_file - file byte range lock
2321150b3934SMarc Eshel  * @filp: The file to apply the lock to
2322150b3934SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2323150b3934SMarc Eshel  * @fl: The lock to be applied
2324150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2325150b3934SMarc Eshel  *
2326150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2327150b3934SMarc Eshel  * as the final argument.
23282beb6614SMarc Eshel  *
23292beb6614SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
23302beb6614SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
23312beb6614SMarc Eshel  * some acceptable default.
2332e70da176SAlexander Aring  *
2333e70da176SAlexander Aring  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2334e70da176SAlexander Aring  * locks, the ->lock() interface may return asynchronously, before the lock has
2335e70da176SAlexander Aring  * been granted or denied by the underlying filesystem, if (and only if)
2336e70da176SAlexander Aring  * lm_grant is set. Additionally EXPORT_OP_ASYNC_LOCK in export_operations
2337e70da176SAlexander Aring  * flags need to be set.
2338e70da176SAlexander Aring  *
23392beb6614SMarc Eshel  * Callers expecting ->lock() to return asynchronously will only use F_SETLK,
2340bde74e4bSMiklos Szeredi  * not F_SETLKW; they will set FL_SLEEP if (and only if) the request is for a
2341bde74e4bSMiklos Szeredi  * blocking lock. When ->lock() does return asynchronously, it must return
23422beb6614SMarc Eshel  * FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock request completes.
23432beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
23442beb6614SMarc Eshel  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
23452beb6614SMarc Eshel  * with the result. If the request timed out the callback routine will return a
23462beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
23478fb47a4fSJ. Bruce Fields  * system is also responsible to keep a corresponding posix lock when it
23482beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
23497723ec97SMarc Eshel  * the correct lock cleanup when required.
2350150b3934SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
23517723ec97SMarc Eshel  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
23524ca52f53SJeff Layton  * return code.
2353de2a4a50SMiklos Szeredi  */
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)23547723ec97SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
23557723ec97SMarc Eshel {
2356150b3934SMarc Eshel 	WARN_ON_ONCE(filp != fl->c.flc_file);
23577723ec97SMarc Eshel 	if (filp->f_op->lock)
23587723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
23597723ec97SMarc Eshel 	else
2360b648a6deSMiklos Szeredi 		return posix_lock_file(filp, fl, conf);
2361b648a6deSMiklos Szeredi }
2362b648a6deSMiklos Szeredi EXPORT_SYMBOL_GPL(vfs_lock_file);
2363b648a6deSMiklos Szeredi 
do_lock_file_wait(struct file * filp,unsigned int cmd,struct file_lock * fl)2364b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
23654ca52f53SJeff Layton 			     struct file_lock *fl)
2366b648a6deSMiklos Szeredi {
2367b648a6deSMiklos Szeredi 	int error;
2368b648a6deSMiklos Szeredi 
2369b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->c.flc_type);
2370764c76b3SMiklos Szeredi 	if (error)
2371b648a6deSMiklos Szeredi 		return error;
2372b648a6deSMiklos Szeredi 
23734ca52f53SJeff Layton 	for (;;) {
23744ca52f53SJeff Layton 		error = vfs_lock_file(filp, cmd, fl, NULL);
237516306a61SNeilBrown 		if (error != FILE_LOCK_DEFERRED)
2376b648a6deSMiklos Szeredi 			break;
2377b648a6deSMiklos Szeredi 		error = wait_event_interruptible(fl->c.flc_wait,
237816306a61SNeilBrown 						 list_empty(&fl->c.flc_blocked_member));
2379b648a6deSMiklos Szeredi 		if (error)
2380b648a6deSMiklos Szeredi 			break;
2381b648a6deSMiklos Szeredi 	}
2382b648a6deSMiklos Szeredi 	locks_delete_block(fl);
23836ca7d910SBenjamin Coddington 
2384cf01f4eeSJeff Layton 	return error;
2385cf01f4eeSJeff Layton }
2386cf01f4eeSJeff Layton 
23874ca52f53SJeff Layton /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2388cf01f4eeSJeff Layton static int
check_fmode_for_setlk(struct file_lock * fl)23894ca52f53SJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2390cf01f4eeSJeff Layton {
2391cf01f4eeSJeff Layton 	switch (fl->c.flc_type) {
2392cf01f4eeSJeff Layton 	case F_RDLCK:
23934ca52f53SJeff Layton 		if (!(fl->c.flc_file->f_mode & FMODE_READ))
2394cf01f4eeSJeff Layton 			return -EBADF;
2395cf01f4eeSJeff Layton 		break;
2396cf01f4eeSJeff Layton 	case F_WRLCK:
2397cf01f4eeSJeff Layton 		if (!(fl->c.flc_file->f_mode & FMODE_WRITE))
2398cf01f4eeSJeff Layton 			return -EBADF;
23991da177e4SLinus Torvalds 	}
24001da177e4SLinus Torvalds 	return 0;
24011da177e4SLinus Torvalds }
2402c293621bSPeter Staubach 
2403a75d30c7SChristoph Hellwig /* Apply the lock described by l to an open file descriptor.
24041da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
24051da177e4SLinus Torvalds  */
fcntl_setlk(unsigned int fd,struct file * filp,unsigned int cmd,struct flock * flock)2406c65454a9SJeff Layton int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
24070b2bac2fSAl Viro 		struct flock *flock)
24081da177e4SLinus Torvalds {
24091da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
24101da177e4SLinus Torvalds 	struct inode *inode = file_inode(filp);
24111da177e4SLinus Torvalds 	struct file *f;
24121da177e4SLinus Torvalds 	int error;
2413a75d30c7SChristoph Hellwig 
24141da177e4SLinus Torvalds 	if (file_lock == NULL)
24151da177e4SLinus Torvalds 		return -ENOLCK;
24165d50ffd7SJeff Layton 
2417cf01f4eeSJeff Layton 	error = flock_to_posix_lock(filp, file_lock, flock);
2418cf01f4eeSJeff Layton 	if (error)
2419cf01f4eeSJeff Layton 		goto out;
2420cf01f4eeSJeff Layton 
24215d50ffd7SJeff Layton 	error = check_fmode_for_setlk(file_lock);
24225d50ffd7SJeff Layton 	if (error)
2423cff2fce5SJeff Layton 		goto out;
24245d50ffd7SJeff Layton 
24255d50ffd7SJeff Layton 	/*
24260d3f7a2dSJeff Layton 	 * If the cmd is requesting file-private locks, then set the
242790478939SJeff Layton 	 * FL_OFDLCK flag and override the owner.
2428a75d30c7SChristoph Hellwig 	 */
242990478939SJeff Layton 	switch (cmd) {
243090478939SJeff Layton 	case F_OFD_SETLK:
24315d50ffd7SJeff Layton 		error = -EINVAL;
24324ca52f53SJeff Layton 		if (flock->l_pid != 0)
24334ca52f53SJeff Layton 			goto out;
24345d50ffd7SJeff Layton 
24350d3f7a2dSJeff Layton 		cmd = F_SETLK;
243690478939SJeff Layton 		file_lock->c.flc_flags |= FL_OFDLCK;
2437a75d30c7SChristoph Hellwig 		file_lock->c.flc_owner = filp;
243890478939SJeff Layton 		break;
243990478939SJeff Layton 	case F_OFD_SETLKW:
24405d50ffd7SJeff Layton 		error = -EINVAL;
24414ca52f53SJeff Layton 		if (flock->l_pid != 0)
24424ca52f53SJeff Layton 			goto out;
2443df561f66SGustavo A. R. Silva 
24445d50ffd7SJeff Layton 		cmd = F_SETLKW;
24454ca52f53SJeff Layton 		file_lock->c.flc_flags |= FL_OFDLCK;
24461da177e4SLinus Torvalds 		file_lock->c.flc_owner = filp;
24471da177e4SLinus Torvalds 		fallthrough;
2448b648a6deSMiklos Szeredi 	case F_SETLKW:
2449c293621bSPeter Staubach 		file_lock->c.flc_flags |= FL_SLEEP;
2450c293621bSPeter Staubach 	}
24513cad1bc0SJann Horn 
24523cad1bc0SJann Horn 	error = do_lock_file_wait(filp, cmd, file_lock);
24533cad1bc0SJann Horn 
24540752ba80SJeff Layton 	/*
2455c293621bSPeter Staubach 	 * Detect close/fcntl races and recover by zapping all POSIX locks
24564ca52f53SJeff Layton 	 * associated with this file and our files_struct, just like on
24574ca52f53SJeff Layton 	 * filp_flush(). There is no need to do that when we're
2458120ce2b0SEric W. Biederman 	 * unlocking though, or for OFD locks.
24590b2bac2fSAl Viro 	 */
24607f3697e2SJeff Layton 	if (!error && file_lock->c.flc_type != F_UNLCK &&
24617f3697e2SJeff Layton 	    !(file_lock->c.flc_flags & FL_OFDLCK)) {
24627f3697e2SJeff Layton 		struct files_struct *files = current->files;
24630b2bac2fSAl Viro 		/*
2464120ce2b0SEric W. Biederman 		 * We need that spin_lock here - it prevents reordering between
2465120ce2b0SEric W. Biederman 		 * update of i_flctx->flc_posix and check for it done in
2466120ce2b0SEric W. Biederman 		 * close(). rcu_read_lock() wouldn't do.
24677f3697e2SJeff Layton 		 */
24683cad1bc0SJann Horn 		spin_lock(&files->file_lock);
24697f3697e2SJeff Layton 		f = files_lookup_fd_locked(files, fd);
2470c293621bSPeter Staubach 		spin_unlock(&files->file_lock);
24717f3697e2SJeff Layton 		if (f != filp) {
24721da177e4SLinus Torvalds 			locks_remove_posix(filp, files);
24731890910fSJeff Layton 			error = -EBADF;
24741da177e4SLinus Torvalds 		}
24751da177e4SLinus Torvalds 	}
24761da177e4SLinus Torvalds out:
24771da177e4SLinus Torvalds 	trace_fcntl_setlk(inode, file_lock, error);
24781da177e4SLinus Torvalds 	locks_free_lock(file_lock);
24791da177e4SLinus Torvalds 	return error;
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
2482a75d30c7SChristoph Hellwig #if BITS_PER_LONG == 32
24831da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
248452306e88SBenjamin Coddington  * This implements the F_GETLK command of fcntl().
24851da177e4SLinus Torvalds  */
fcntl_getlk64(struct file * filp,unsigned int cmd,struct flock64 * flock)24861da177e4SLinus Torvalds int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
248752306e88SBenjamin Coddington {
248852306e88SBenjamin Coddington 	struct file_lock *fl;
248952306e88SBenjamin Coddington 	int error;
249052306e88SBenjamin Coddington 
24911da177e4SLinus Torvalds 	fl = locks_alloc_lock();
24926c9007f6SStas Sergeev 	if (fl == NULL)
24936c9007f6SStas Sergeev 		return -ENOMEM;
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	error = -EINVAL;
249652306e88SBenjamin Coddington 	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
24971da177e4SLinus Torvalds 			&& flock->l_type != F_WRLCK)
24981da177e4SLinus Torvalds 		goto out;
24991da177e4SLinus Torvalds 
25000d3f7a2dSJeff Layton 	error = flock64_to_posix_lock(filp, fl, flock);
250190478939SJeff Layton 	if (error)
2502a75d30c7SChristoph Hellwig 		goto out;
250390478939SJeff Layton 
250490478939SJeff Layton 	if (cmd == F_OFD_GETLK) {
25054ca52f53SJeff Layton 		error = -EINVAL;
25064ca52f53SJeff Layton 		if (flock->l_pid != 0)
25075d50ffd7SJeff Layton 			goto out;
25085d50ffd7SJeff Layton 
250952306e88SBenjamin Coddington 		fl->c.flc_flags |= FL_OFDLCK;
25103ee17abdSJ. Bruce Fields 		fl->c.flc_owner = filp;
25111da177e4SLinus Torvalds 	}
25121da177e4SLinus Torvalds 
25134ca52f53SJeff Layton 	error = vfs_test_lock(filp, fl);
25144ca52f53SJeff Layton 	if (error)
251552306e88SBenjamin Coddington 		goto out;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	flock->l_type = fl->c.flc_type;
251852306e88SBenjamin Coddington 	if (fl->c.flc_type != F_UNLCK)
25191da177e4SLinus Torvalds 		posix_lock_to_flock64(flock, fl);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds out:
25221da177e4SLinus Torvalds 	locks_free_lock(fl);
25231da177e4SLinus Torvalds 	return error;
25241da177e4SLinus Torvalds }
2525c293621bSPeter Staubach 
2526a75d30c7SChristoph Hellwig /* Apply the lock described by l to an open file descriptor.
25271da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
25281da177e4SLinus Torvalds  */
fcntl_setlk64(unsigned int fd,struct file * filp,unsigned int cmd,struct flock64 * flock)25290b2bac2fSAl Viro int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
25301da177e4SLinus Torvalds 		struct flock64 *flock)
25311da177e4SLinus Torvalds {
25321da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
25331da177e4SLinus Torvalds 	struct file *f;
25341da177e4SLinus Torvalds 	int error;
2535a75d30c7SChristoph Hellwig 
25361da177e4SLinus Torvalds 	if (file_lock == NULL)
25371da177e4SLinus Torvalds 		return -ENOLCK;
25385d50ffd7SJeff Layton 
2539cf01f4eeSJeff Layton 	error = flock64_to_posix_lock(filp, file_lock, flock);
2540cf01f4eeSJeff Layton 	if (error)
2541cf01f4eeSJeff Layton 		goto out;
2542cf01f4eeSJeff Layton 
25435d50ffd7SJeff Layton 	error = check_fmode_for_setlk(file_lock);
25445d50ffd7SJeff Layton 	if (error)
2545cff2fce5SJeff Layton 		goto out;
25465d50ffd7SJeff Layton 
25475d50ffd7SJeff Layton 	/*
25480d3f7a2dSJeff Layton 	 * If the cmd is requesting file-private locks, then set the
254990478939SJeff Layton 	 * FL_OFDLCK flag and override the owner.
2550a75d30c7SChristoph Hellwig 	 */
255190478939SJeff Layton 	switch (cmd) {
255290478939SJeff Layton 	case F_OFD_SETLK:
25535d50ffd7SJeff Layton 		error = -EINVAL;
25544ca52f53SJeff Layton 		if (flock->l_pid != 0)
25554ca52f53SJeff Layton 			goto out;
25565d50ffd7SJeff Layton 
25570d3f7a2dSJeff Layton 		cmd = F_SETLK64;
255890478939SJeff Layton 		file_lock->c.flc_flags |= FL_OFDLCK;
2559a75d30c7SChristoph Hellwig 		file_lock->c.flc_owner = filp;
256090478939SJeff Layton 		break;
256190478939SJeff Layton 	case F_OFD_SETLKW:
25625d50ffd7SJeff Layton 		error = -EINVAL;
25634ca52f53SJeff Layton 		if (flock->l_pid != 0)
25644ca52f53SJeff Layton 			goto out;
2565df561f66SGustavo A. R. Silva 
25665d50ffd7SJeff Layton 		cmd = F_SETLKW64;
25674ca52f53SJeff Layton 		file_lock->c.flc_flags |= FL_OFDLCK;
25681da177e4SLinus Torvalds 		file_lock->c.flc_owner = filp;
25691da177e4SLinus Torvalds 		fallthrough;
2570b648a6deSMiklos Szeredi 	case F_SETLKW64:
2571c293621bSPeter Staubach 		file_lock->c.flc_flags |= FL_SLEEP;
2572c293621bSPeter Staubach 	}
2573f8138f2aSJann Horn 
2574f8138f2aSJann Horn 	error = do_lock_file_wait(filp, cmd, file_lock);
2575f8138f2aSJann Horn 
25760752ba80SJeff Layton 	/*
2577c293621bSPeter Staubach 	 * Detect close/fcntl races and recover by zapping all POSIX locks
25784ca52f53SJeff Layton 	 * associated with this file and our files_struct, just like on
25794ca52f53SJeff Layton 	 * filp_flush(). There is no need to do that when we're
2580120ce2b0SEric W. Biederman 	 * unlocking though, or for OFD locks.
25817f3697e2SJeff Layton 	 */
25827f3697e2SJeff Layton 	if (!error && file_lock->c.flc_type != F_UNLCK &&
25837f3697e2SJeff Layton 	    !(file_lock->c.flc_flags & FL_OFDLCK)) {
25847f3697e2SJeff Layton 		struct files_struct *files = current->files;
25857f3697e2SJeff Layton 		/*
2586120ce2b0SEric W. Biederman 		 * We need that spin_lock here - it prevents reordering between
2587120ce2b0SEric W. Biederman 		 * update of i_flctx->flc_posix and check for it done in
2588120ce2b0SEric W. Biederman 		 * close(). rcu_read_lock() wouldn't do.
25897f3697e2SJeff Layton 		 */
2590f8138f2aSJann Horn 		spin_lock(&files->file_lock);
25917f3697e2SJeff Layton 		f = files_lookup_fd_locked(files, fd);
2592c293621bSPeter Staubach 		spin_unlock(&files->file_lock);
25937f3697e2SJeff Layton 		if (f != filp) {
25941da177e4SLinus Torvalds 			locks_remove_posix(filp, files);
25951da177e4SLinus Torvalds 			error = -EBADF;
25961da177e4SLinus Torvalds 		}
25971da177e4SLinus Torvalds 	}
25981da177e4SLinus Torvalds out:
25991da177e4SLinus Torvalds 	locks_free_lock(file_lock);
26001da177e4SLinus Torvalds 	return error;
26011da177e4SLinus Torvalds }
26021da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds /*
26051da177e4SLinus Torvalds  * This function is called when the file is being removed
26061da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
26071890910fSJeff Layton  * are deleted at this time.
2608c65454a9SJeff Layton  */
locks_remove_posix(struct file * filp,fl_owner_t owner)2609ff7b86b8SMiklos Szeredi void locks_remove_posix(struct file *filp, fl_owner_t owner)
2610128a3785SDmitry Vyukov {
26111da177e4SLinus Torvalds 	int error;
26121da177e4SLinus Torvalds 	struct inode *inode = file_inode(filp);
26131da177e4SLinus Torvalds 	struct file_lock lock;
26141da177e4SLinus Torvalds 	struct file_lock_context *ctx;
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	/*
2617401a8b8fSJeff Layton 	 * If there are no locks held on this file, we don't need to call
2618bd61e0a9SJeff Layton 	 * posix_lock_file().  Another process could be setting a lock on this
26191da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
26201da177e4SLinus Torvalds 	 */
2621d6367d62SNeilBrown 	ctx = locks_inode_context(inode);
26224ca52f53SJeff Layton 	if (!ctx || list_empty(&ctx->flc_posix))
26234ca52f53SJeff Layton 		return;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	locks_init_lock(&lock);
26264ca52f53SJeff Layton 	lock.c.flc_type = F_UNLCK;
26274ca52f53SJeff Layton 	lock.c.flc_flags = FL_POSIX | FL_CLOSE;
26284ca52f53SJeff Layton 	lock.fl_start = 0;
26291da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
26301da177e4SLinus Torvalds 	lock.c.flc_owner = owner;
26311da177e4SLinus Torvalds 	lock.c.flc_pid = current->tgid;
26321890910fSJeff Layton 	lock.c.flc_file = filp;
26331da177e4SLinus Torvalds 	lock.fl_ops = NULL;
26341da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
26351da177e4SLinus Torvalds 
2636c568d683SMiklos Szeredi 	error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
26391da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
26403d8e560dSJeff Layton 	trace_locks_remove_posix(inode, &lock, error);
2641dd459bb1SJeff Layton }
2642128a3785SDmitry Vyukov EXPORT_SYMBOL(locks_remove_posix);
2643dd459bb1SJeff Layton 
2644d6367d62SNeilBrown /* The i_flctx must be valid when calling into here */
2645c65454a9SJeff Layton static void
locks_remove_flock(struct file * filp,struct file_lock_context * flctx)2646dd459bb1SJeff Layton locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
26473d8e560dSJeff Layton {
2648dd459bb1SJeff Layton 	struct file_lock fl;
2649dd459bb1SJeff Layton 	struct inode *inode = file_inode(filp);
26504149be7bSKuniyuki Iwashima 
26514ca52f53SJeff Layton 	if (list_empty(&flctx->flc_flock))
2652d6367d62SNeilBrown 		return;
2653de2a4a50SMiklos Szeredi 
2654dd459bb1SJeff Layton 	flock_make_lock(filp, &fl, F_UNLCK);
2655dd459bb1SJeff Layton 	fl.c.flc_flags |= FL_CLOSE;
2656bcd7f78dSJeff Layton 
2657dd459bb1SJeff Layton 	if (filp->f_op->flock)
2658dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2659dd459bb1SJeff Layton 	else
2660dd459bb1SJeff Layton 		flock_lock_inode(inode, &fl);
2661dd459bb1SJeff Layton 
26623d8e560dSJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
26638634b51fSJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2664128a3785SDmitry Vyukov }
26658634b51fSJeff Layton 
2666c69ff407SJeff Layton /* The i_flctx must be valid when calling into here */
26678634b51fSJeff Layton static void
locks_remove_lease(struct file * filp,struct file_lock_context * ctx)26688634b51fSJeff Layton locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
26693d8e560dSJeff Layton {
26708634b51fSJeff Layton 	struct file_lease *fl, *tmp;
26718634b51fSJeff Layton 	LIST_HEAD(dispose);
267202e525b2SPeter Zijlstra 
26736109c850SJeff Layton 	if (list_empty(&ctx->flc_lease))
26744ca52f53SJeff Layton 		return;
26754ca52f53SJeff Layton 
26767448cc37SJeff Layton 	percpu_down_read(&file_rwsem);
26776109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
267802e525b2SPeter Zijlstra 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, c.flc_list)
26795f43086bSPeter Zijlstra 		if (filp == fl->c.flc_file)
26808634b51fSJeff Layton 			lease_modify(fl, F_UNLCK, &dispose);
26818634b51fSJeff Layton 	spin_unlock(&ctx->flc_lock);
26828634b51fSJeff Layton 	percpu_up_read(&file_rwsem);
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	locks_dispose_list(&dispose);
26851da177e4SLinus Torvalds }
268678ed8a13SJeff Layton 
26871da177e4SLinus Torvalds /*
2688128a3785SDmitry Vyukov  * This function is called on the last close of an open file.
2689128a3785SDmitry Vyukov  */
locks_remove_file(struct file * filp)2690c65454a9SJeff Layton void locks_remove_file(struct file *filp)
2691128a3785SDmitry Vyukov {
26923d8e560dSJeff Layton 	struct file_lock_context *ctx;
26933d8e560dSJeff Layton 
2694dd459bb1SJeff Layton 	ctx = locks_inode_context(file_inode(filp));
269573a8f5f7SChristoph Hellwig 	if (!ctx)
26965d50ffd7SJeff Layton 		return;
2697dd459bb1SJeff Layton 
2698128a3785SDmitry Vyukov 	/* remove any OFD locks */
2699dd459bb1SJeff Layton 	locks_remove_posix(filp, filp);
27008634b51fSJeff Layton 
2701128a3785SDmitry Vyukov 	/* remove flock locks */
27023953704fSBenjamin Coddington 	locks_remove_flock(filp, ctx);
27033953704fSBenjamin Coddington 
27043953704fSBenjamin Coddington 	/* remove any leases */
27053953704fSBenjamin Coddington 	locks_remove_lease(filp, ctx);
27063953704fSBenjamin Coddington 
27073953704fSBenjamin Coddington 	spin_lock(&ctx->flc_lock);
27081da177e4SLinus Torvalds 	locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
27091da177e4SLinus Torvalds 	locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
27101da177e4SLinus Torvalds 	locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
27119b9d2ab4SMarc Eshel 	spin_unlock(&ctx->flc_lock);
27129b9d2ab4SMarc Eshel }
27139b9d2ab4SMarc Eshel 
27149b9d2ab4SMarc Eshel /**
27159b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
27169b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
27179b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
27189b9d2ab4SMarc Eshel  *
27194ca52f53SJeff Layton  * Used by lock managers to cancel blocked requests
2720de2a4a50SMiklos Szeredi  */
vfs_cancel_lock(struct file * filp,struct file_lock * fl)27219b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
27229b9d2ab4SMarc Eshel {
27239b9d2ab4SMarc Eshel 	WARN_ON_ONCE(filp != fl->c.flc_file);
27249b9d2ab4SMarc Eshel 	if (filp->f_op->lock)
27259b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
2726ab1ddef9SJeff Layton 	return 0;
2727ab1ddef9SJeff Layton }
2728ab1ddef9SJeff Layton EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2729ab1ddef9SJeff Layton 
2730ab1ddef9SJeff Layton /**
2731ab1ddef9SJeff Layton  * vfs_inode_has_locks - are any file locks held on @inode?
2732ab1ddef9SJeff Layton  * @inode: inode to check for locks
2733ab1ddef9SJeff Layton  *
2734ab1ddef9SJeff Layton  * Return true if there are any FL_POSIX or FL_FLOCK locks currently
2735ab1ddef9SJeff Layton  * set on @inode.
2736ab1ddef9SJeff Layton  */
vfs_inode_has_locks(struct inode * inode)2737ab1ddef9SJeff Layton bool vfs_inode_has_locks(struct inode *inode)
2738401a8b8fSJeff Layton {
2739ab1ddef9SJeff Layton 	struct file_lock_context *ctx;
2740ab1ddef9SJeff Layton 	bool ret;
2741ab1ddef9SJeff Layton 
2742ab1ddef9SJeff Layton 	ctx = locks_inode_context(inode);
2743ab1ddef9SJeff Layton 	if (!ctx)
2744ab1ddef9SJeff Layton 		return false;
2745ab1ddef9SJeff Layton 
2746ab1ddef9SJeff Layton 	spin_lock(&ctx->flc_lock);
2747ab1ddef9SJeff Layton 	ret = !list_empty(&ctx->flc_posix) || !list_empty(&ctx->flc_flock);
2748ab1ddef9SJeff Layton 	spin_unlock(&ctx->flc_lock);
27497f8ada98SPavel Emelyanov 	return ret;
2750d8ba7a36SAlexey Dobriyan }
27517f8ada98SPavel Emelyanov EXPORT_SYMBOL_GPL(vfs_inode_has_locks);
27527f8ada98SPavel Emelyanov 
27537012b02aSJeff Layton #ifdef CONFIG_PROC_FS
27547012b02aSJeff Layton #include <linux/proc_fs.h>
27557012b02aSJeff Layton #include <linux/seq_file.h>
27567012b02aSJeff Layton 
27577012b02aSJeff Layton struct locks_iterator {
2758a1c2af32SJeff Layton 	int	li_cpu;
2759b8da9b10SLuo Longjun 	loff_t	li_pos;
27601da177e4SLinus Torvalds };
27611da177e4SLinus Torvalds 
lock_get_status(struct seq_file * f,struct file_lock_core * flc,loff_t id,char * pfx,int repeat)27626021d62cSJeff Layton static void lock_get_status(struct seq_file *f, struct file_lock_core *flc,
27639d78edeaSAlexey Gladkov 			    loff_t id, char *pfx, int repeat)
2764a1c2af32SJeff Layton {
2765a1c2af32SJeff Layton 	struct inode *inode = NULL;
2766d67fd44fSNikolay Borisov 	unsigned int pid;
2767a1c2af32SJeff Layton 	struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
2768a1c2af32SJeff Layton 	int type = flc->flc_type;
2769d67fd44fSNikolay Borisov 	struct file_lock *fl = file_lock(flc);
27701cf8e5deSKonstantin Khorenko 
27711cf8e5deSKonstantin Khorenko 	pid = locks_translate_pid(flc, proc_pidns);
27721cf8e5deSKonstantin Khorenko 
2773d67fd44fSNikolay Borisov 	/*
2774a1c2af32SJeff Layton 	 * If lock owner is dead (and pid is freed) or not visible in current
2775a1c2af32SJeff Layton 	 * pidns, zero is shown as a pid value. Check lock info from
27761da177e4SLinus Torvalds 	 * init_pid_ns to get saved lock pid value.
2777b8da9b10SLuo Longjun 	 */
2778b8da9b10SLuo Longjun 	if (flc->flc_file != NULL)
2779b8da9b10SLuo Longjun 		inode = file_inode(flc->flc_file);
2780b8da9b10SLuo Longjun 
2781b8da9b10SLuo Longjun 	seq_printf(f, "%lld: ", id);
2782a1c2af32SJeff Layton 
2783a1c2af32SJeff Layton 	if (repeat)
27845315c26aSFabian Frederick 		seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx);
2785a1c2af32SJeff Layton 
27865315c26aSFabian Frederick 	if (flc->flc_flags & FL_POSIX) {
2787c918d42aSJeff Layton 		if (flc->flc_flags & FL_ACCESS)
27885315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2789c918d42aSJeff Layton 		else if (flc->flc_flags & FL_OFDLCK)
2790c918d42aSJeff Layton 			seq_puts(f, "OFDLCK");
2791f7e33bdbSJeff Layton 		else
2792a1c2af32SJeff Layton 			seq_puts(f, "POSIX ");
27935315c26aSFabian Frederick 
2794a1c2af32SJeff Layton 		seq_printf(f, " %s ",
2795c69ff407SJeff Layton 			     (inode == NULL) ? "*NOINODE*" : "ADVISORY ");
2796c69ff407SJeff Layton 	} else if (flc->flc_flags & FL_FLOCK) {
2797c69ff407SJeff Layton 		seq_puts(f, "FLOCK  ADVISORY  ");
27983d40f781SJeff Layton 	} else if (flc->flc_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT)) {
2799a1c2af32SJeff Layton 		struct file_lease *lease = file_lease(flc);
28008144f1f6SJeff Layton 
28018144f1f6SJeff Layton 		type = target_leasetype(lease);
28025315c26aSFabian Frederick 
28038144f1f6SJeff Layton 		if (flc->flc_flags & FL_DELEG)
2804c69ff407SJeff Layton 			seq_puts(f, "DELEG  ");
28055315c26aSFabian Frederick 		else
2806a1c2af32SJeff Layton 			seq_puts(f, "LEASE  ");
28075315c26aSFabian Frederick 
28081da177e4SLinus Torvalds 		if (lease_breaking(lease))
28095315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
28101da177e4SLinus Torvalds 		else if (flc->flc_file)
28115315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
28121da177e4SLinus Torvalds 		else
281343e4cb94SPavel Begunkov 			seq_puts(f, "BREAKER   ");
281443e4cb94SPavel Begunkov 	} else {
281543e4cb94SPavel Begunkov 		seq_puts(f, "UNKNOWN UNKNOWN  ");
28161da177e4SLinus Torvalds 	}
28173648888eSJeff Layton 
28186021d62cSJeff Layton 	seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" :
28191da177e4SLinus Torvalds 			     (type == F_RDLCK) ? "READ" : "UNLCK");
28201da177e4SLinus Torvalds 	if (inode) {
28211da177e4SLinus Torvalds 		/* userspace relies on this representation of dev_t */
28226021d62cSJeff Layton 		seq_printf(f, "%d %02x:%02x:%lu ", pid,
28231da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
2824a1c2af32SJeff Layton 				MINOR(inode->i_sb->s_dev), inode->i_ino);
28251da177e4SLinus Torvalds 	} else {
28267f8ada98SPavel Emelyanov 		seq_printf(f, "%d <none>:0 ", pid);
28271da177e4SLinus Torvalds 	}
28287f8ada98SPavel Emelyanov 	if (flc->flc_flags & FL_POSIX) {
28291da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
28305315c26aSFabian Frederick 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
28311da177e4SLinus Torvalds 		else
28321da177e4SLinus Torvalds 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
28331da177e4SLinus Torvalds 	} else {
2834a1c2af32SJeff Layton 		seq_puts(f, "0 EOF\n");
2835b8da9b10SLuo Longjun 	}
2836a1c2af32SJeff Layton }
2837b8da9b10SLuo Longjun 
get_next_blocked_member(struct file_lock_core * node)2838b8da9b10SLuo Longjun static struct file_lock_core *get_next_blocked_member(struct file_lock_core *node)
2839a1c2af32SJeff Layton {
2840b8da9b10SLuo Longjun 	struct file_lock_core *tmp;
2841b8da9b10SLuo Longjun 
2842b8da9b10SLuo Longjun 	/* NULL node or root node */
2843a1c2af32SJeff Layton 	if (node == NULL || node->flc_blocker == NULL)
2844a1c2af32SJeff Layton 		return NULL;
2845a1c2af32SJeff Layton 
2846b8da9b10SLuo Longjun 	/* Next member in the linked list could be itself */
2847b8da9b10SLuo Longjun 	tmp = list_next_entry(node, flc_blocked_member);
2848b8da9b10SLuo Longjun 	if (list_entry_is_head(tmp, &node->flc_blocker->flc_blocked_requests,
2849b8da9b10SLuo Longjun 			       flc_blocked_member)
2850b8da9b10SLuo Longjun 		|| tmp == node) {
2851b8da9b10SLuo Longjun 		return NULL;
2852b8da9b10SLuo Longjun 	}
28537f8ada98SPavel Emelyanov 
28541da177e4SLinus Torvalds 	return tmp;
28557012b02aSJeff Layton }
2856a1c2af32SJeff Layton 
locks_show(struct seq_file * f,void * v)28579d78edeaSAlexey Gladkov static int locks_show(struct seq_file *f, void *v)
2858b8da9b10SLuo Longjun {
28597f8ada98SPavel Emelyanov 	struct locks_iterator *iter = f->private;
2860a1c2af32SJeff Layton 	struct file_lock_core *cur, *tmp;
28617f8ada98SPavel Emelyanov 	struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
2862a1c2af32SJeff Layton 	int level = 0;
2863d67fd44fSNikolay Borisov 
2864d67fd44fSNikolay Borisov 	cur = hlist_entry(v, struct file_lock_core, flc_link);
2865a1c2af32SJeff Layton 
28664ca52f53SJeff Layton 	if (locks_translate_pid(cur, proc_pidns) == 0)
2867a1c2af32SJeff Layton 		return 0;
2868b8da9b10SLuo Longjun 
2869b8da9b10SLuo Longjun 	/* View this crossed linked list as a binary tree, the first member of flc_blocked_requests
2870b8da9b10SLuo Longjun 	 * is the left child of current node, the next silibing in flc_blocked_member is the
2871b8da9b10SLuo Longjun 	 * right child, we can alse get the parent of current node from flc_blocker, so this
2872b8da9b10SLuo Longjun 	 * question becomes traversal of a binary tree
2873b8da9b10SLuo Longjun 	 */
2874b8da9b10SLuo Longjun 	while (cur != NULL) {
28757f8ada98SPavel Emelyanov 		if (level)
2876a1c2af32SJeff Layton 			lock_get_status(f, cur, iter->li_pos, "-> ", level);
2877b8da9b10SLuo Longjun 		else
2878a1c2af32SJeff Layton 			lock_get_status(f, cur, iter->li_pos, "", level);
2879a1c2af32SJeff Layton 
2880a1c2af32SJeff Layton 		if (!list_empty(&cur->flc_blocked_requests)) {
2881b8da9b10SLuo Longjun 			/* Turn left */
2882b8da9b10SLuo Longjun 			cur = list_first_entry_or_null(&cur->flc_blocked_requests,
2883b8da9b10SLuo Longjun 						       struct file_lock_core,
2884b8da9b10SLuo Longjun 						       flc_blocked_member);
2885b8da9b10SLuo Longjun 			level++;
2886a1c2af32SJeff Layton 		} else {
2887a1c2af32SJeff Layton 			/* Turn right */
2888b8da9b10SLuo Longjun 			tmp = get_next_blocked_member(cur);
2889b8da9b10SLuo Longjun 			/* Fall back to parent node */
2890b8da9b10SLuo Longjun 			while (tmp == NULL && cur->flc_blocker != NULL) {
2891b8da9b10SLuo Longjun 				cur = cur->flc_blocker;
2892b8da9b10SLuo Longjun 				level--;
2893b8da9b10SLuo Longjun 				tmp = get_next_blocked_member(cur);
28947f8ada98SPavel Emelyanov 			}
28957f8ada98SPavel Emelyanov 			cur = tmp;
28961da177e4SLinus Torvalds 		}
28971da177e4SLinus Torvalds 	}
28986c8c9031SAndrey Vagin 
28996c8c9031SAndrey Vagin 	return 0;
29006c8c9031SAndrey Vagin }
29016c8c9031SAndrey Vagin 
__show_fd_locks(struct seq_file * f,struct list_head * head,int * id,struct file * filp,struct files_struct * files)2902a1c2af32SJeff Layton static void __show_fd_locks(struct seq_file *f,
29036c8c9031SAndrey Vagin 			struct list_head *head, int *id,
2904a1c2af32SJeff Layton 			struct file *filp, struct files_struct *files)
29056c8c9031SAndrey Vagin {
2906a1c2af32SJeff Layton 	struct file_lock_core *fl;
29076c8c9031SAndrey Vagin 
2908a1c2af32SJeff Layton 	list_for_each_entry(fl, head, flc_list) {
29096c8c9031SAndrey Vagin 
29106c8c9031SAndrey Vagin 		if (filp != fl->flc_file)
29116c8c9031SAndrey Vagin 			continue;
29126c8c9031SAndrey Vagin 		if (fl->flc_owner != files && fl->flc_owner != filp)
2913b8da9b10SLuo Longjun 			continue;
29146c8c9031SAndrey Vagin 
29156c8c9031SAndrey Vagin 		(*id)++;
29166c8c9031SAndrey Vagin 		seq_puts(f, "lock:\t");
29176c8c9031SAndrey Vagin 		lock_get_status(f, fl, *id, "", 0);
29186c8c9031SAndrey Vagin 	}
29196c8c9031SAndrey Vagin }
2920c65454a9SJeff Layton 
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)29216c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f,
29226c8c9031SAndrey Vagin 		  struct file *filp, struct files_struct *files)
29236c8c9031SAndrey Vagin {
2924401a8b8fSJeff Layton 	struct inode *inode = file_inode(filp);
29256c8c9031SAndrey Vagin 	struct file_lock_context *ctx;
29266c8c9031SAndrey Vagin 	int id = 0;
29276c8c9031SAndrey Vagin 
29286c8c9031SAndrey Vagin 	ctx = locks_inode_context(inode);
29296c8c9031SAndrey Vagin 	if (!ctx)
29306c8c9031SAndrey Vagin 		return;
29316c8c9031SAndrey Vagin 
29326c8c9031SAndrey Vagin 	spin_lock(&ctx->flc_lock);
29336c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
29346c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
29357f8ada98SPavel Emelyanov 	__show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
2936b03dfdecSJeff Layton 	spin_unlock(&ctx->flc_lock);
29371da177e4SLinus Torvalds }
29387012b02aSJeff Layton 
locks_start(struct seq_file * f,loff_t * pos)293999dc8292SJerome Marchand static void *locks_start(struct seq_file *f, loff_t *pos)
29407012b02aSJeff Layton 	__acquires(&blocked_lock_lock)
2941aba37660SPeter Zijlstra {
29427b2296afSJeff Layton 	struct locks_iterator *iter = f->private;
29437c3f654dSPeter Zijlstra 
29441da177e4SLinus Torvalds 	iter->li_pos = *pos + 1;
29457f8ada98SPavel Emelyanov 	percpu_down_write(&file_rwsem);
29467f8ada98SPavel Emelyanov 	spin_lock(&blocked_lock_lock);
29477f8ada98SPavel Emelyanov 	return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
29487012b02aSJeff Layton }
29497012b02aSJeff Layton 
locks_next(struct seq_file * f,void * v,loff_t * pos)29507012b02aSJeff Layton static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
29517c3f654dSPeter Zijlstra {
29521da177e4SLinus Torvalds 	struct locks_iterator *iter = f->private;
29537f8ada98SPavel Emelyanov 
29547f8ada98SPavel Emelyanov 	++iter->li_pos;
2955b03dfdecSJeff Layton 	return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
29567f8ada98SPavel Emelyanov }
29577b2296afSJeff Layton 
locks_stop(struct seq_file * f,void * v)2958aba37660SPeter Zijlstra static void locks_stop(struct seq_file *f, void *v)
29591da177e4SLinus Torvalds 	__releases(&blocked_lock_lock)
29601da177e4SLinus Torvalds {
2961d8ba7a36SAlexey Dobriyan 	spin_unlock(&blocked_lock_lock);
29627f8ada98SPavel Emelyanov 	percpu_up_write(&file_rwsem);
29637f8ada98SPavel Emelyanov }
29647f8ada98SPavel Emelyanov 
29657f8ada98SPavel Emelyanov static const struct seq_operations locks_seq_operations = {
29667f8ada98SPavel Emelyanov 	.start	= locks_start,
2967d8ba7a36SAlexey Dobriyan 	.next	= locks_next,
2968d8ba7a36SAlexey Dobriyan 	.stop	= locks_stop,
2969d8ba7a36SAlexey Dobriyan 	.show	= locks_show,
297044414d82SChristoph Hellwig };
297144414d82SChristoph Hellwig 
proc_locks_init(void)2972d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2973d8ba7a36SAlexey Dobriyan {
297491899226SPaul Gortmaker 	proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
29757f8ada98SPavel Emelyanov 			sizeof(struct locks_iterator), NULL);
29767f8ada98SPavel Emelyanov 	return 0;
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds fs_initcall(proc_locks_init);
29797012b02aSJeff Layton #endif
29807012b02aSJeff Layton 
filelock_init(void)29814a075e39SJeff Layton static int __init filelock_init(void)
29823754707bSLinus Torvalds {
29834a075e39SJeff Layton 	int i;
29841da177e4SLinus Torvalds 
29853754707bSLinus Torvalds 	flctx_cache = kmem_cache_create("file_lock_ctx",
2986ee19cc40SMiklos Szeredi 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
2987*3f65f3c0SOmar Sandoval 
2988c69ff407SJeff Layton 	filelock_cache = kmem_cache_create("file_lock_cache",
2989c69ff407SJeff Layton 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
29907c3f654dSPeter Zijlstra 
29917c3f654dSPeter Zijlstra 	filelease_cache = kmem_cache_create("file_lease_cache",
29927c3f654dSPeter Zijlstra 			sizeof(struct file_lease), 0, SLAB_PANIC, NULL);
29937c3f654dSPeter Zijlstra 
29947c3f654dSPeter Zijlstra 	for_each_possible_cpu(i) {
29957c3f654dSPeter Zijlstra 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
29967012b02aSJeff Layton 
299718f6622eSJeff Layton 		spin_lock_init(&fll->lock);
29981da177e4SLinus Torvalds 		INIT_HLIST_HEAD(&fll->hlist);
29991da177e4SLinus Torvalds 	}
30001da177e4SLinus Torvalds 
3001 	lease_notifier_chain_init();
3002 	return 0;
3003 }
3004 core_initcall(filelock_init);
3005