xref: /openbmc/linux/fs/locks.c (revision bf74ee101d3403d72dac5997be083ac41028c66b)
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 
521da177e4SLinus Torvalds #include <linux/capability.h>
531da177e4SLinus Torvalds #include <linux/file.h>
549f3acc31SAl Viro #include <linux/fdtable.h>
555970e15dSJeff Layton #include <linux/filelock.h>
561da177e4SLinus Torvalds #include <linux/fs.h>
571da177e4SLinus Torvalds #include <linux/init.h>
581da177e4SLinus Torvalds #include <linux/security.h>
591da177e4SLinus Torvalds #include <linux/slab.h>
601da177e4SLinus Torvalds #include <linux/syscalls.h>
611da177e4SLinus Torvalds #include <linux/time.h>
624fb3a538SDipankar Sarma #include <linux/rcupdate.h>
63ab1f1611SVitaliy Gusev #include <linux/pid_namespace.h>
6448f74186SJeff Layton #include <linux/hashtable.h>
657012b02aSJeff Layton #include <linux/percpu.h>
66dd81faa8SLuis Chamberlain #include <linux/sysctl.h>
671da177e4SLinus Torvalds 
6862af4f1fSJeff Layton #define CREATE_TRACE_POINTS
6962af4f1fSJeff Layton #include <trace/events/filelock.h>
7062af4f1fSJeff Layton 
717c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
741da177e4SLinus Torvalds #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
7511afe9f7SChristoph Hellwig #define IS_LEASE(fl)	(fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
76cff2fce5SJeff Layton #define IS_OFDLCK(fl)	(fl->fl_flags & FL_OFDLCK)
779d5b86acSBenjamin Coddington #define IS_REMOTELCK(fl)	(fl->fl_pid <= 0)
781da177e4SLinus Torvalds 
lease_breaking(struct file_lock * fl)79ab83fa4bSJ. Bruce Fields static bool lease_breaking(struct file_lock *fl)
80ab83fa4bSJ. Bruce Fields {
81778fc546SJ. Bruce Fields 	return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
82778fc546SJ. Bruce Fields }
83778fc546SJ. Bruce Fields 
target_leasetype(struct file_lock * fl)84778fc546SJ. Bruce Fields static int target_leasetype(struct file_lock *fl)
85778fc546SJ. Bruce Fields {
86778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_UNLOCK_PENDING)
87778fc546SJ. Bruce Fields 		return F_UNLCK;
88778fc546SJ. Bruce Fields 	if (fl->fl_flags & FL_DOWNGRADE_PENDING)
89778fc546SJ. Bruce Fields 		return F_RDLCK;
90778fc546SJ. Bruce Fields 	return fl->fl_type;
91ab83fa4bSJ. Bruce Fields }
92ab83fa4bSJ. Bruce Fields 
93dd81faa8SLuis Chamberlain static int leases_enable = 1;
94dd81faa8SLuis Chamberlain static int lease_break_time = 45;
95dd81faa8SLuis Chamberlain 
96dd81faa8SLuis Chamberlain #ifdef CONFIG_SYSCTL
97dd81faa8SLuis Chamberlain static struct ctl_table locks_sysctls[] = {
98dd81faa8SLuis Chamberlain 	{
99dd81faa8SLuis Chamberlain 		.procname	= "leases-enable",
100dd81faa8SLuis Chamberlain 		.data		= &leases_enable,
101dd81faa8SLuis Chamberlain 		.maxlen		= sizeof(int),
102dd81faa8SLuis Chamberlain 		.mode		= 0644,
103dd81faa8SLuis Chamberlain 		.proc_handler	= proc_dointvec,
104dd81faa8SLuis Chamberlain 	},
105dd81faa8SLuis Chamberlain #ifdef CONFIG_MMU
106dd81faa8SLuis Chamberlain 	{
107dd81faa8SLuis Chamberlain 		.procname	= "lease-break-time",
108dd81faa8SLuis Chamberlain 		.data		= &lease_break_time,
109dd81faa8SLuis Chamberlain 		.maxlen		= sizeof(int),
110dd81faa8SLuis Chamberlain 		.mode		= 0644,
111dd81faa8SLuis Chamberlain 		.proc_handler	= proc_dointvec,
112dd81faa8SLuis Chamberlain 	},
113dd81faa8SLuis Chamberlain #endif /* CONFIG_MMU */
114dd81faa8SLuis Chamberlain 	{}
115dd81faa8SLuis Chamberlain };
116dd81faa8SLuis Chamberlain 
init_fs_locks_sysctls(void)117dd81faa8SLuis Chamberlain static int __init init_fs_locks_sysctls(void)
118dd81faa8SLuis Chamberlain {
119dd81faa8SLuis Chamberlain 	register_sysctl_init("fs", locks_sysctls);
120dd81faa8SLuis Chamberlain 	return 0;
121dd81faa8SLuis Chamberlain }
122dd81faa8SLuis Chamberlain early_initcall(init_fs_locks_sysctls);
123dd81faa8SLuis Chamberlain #endif /* CONFIG_SYSCTL */
1241da177e4SLinus Torvalds 
1251c8c601aSJeff Layton /*
1267012b02aSJeff Layton  * The global file_lock_list is only used for displaying /proc/locks, so we
1277c3f654dSPeter Zijlstra  * keep a list on each CPU, with each list protected by its own spinlock.
1287c3f654dSPeter Zijlstra  * Global serialization is done using file_rwsem.
1297c3f654dSPeter Zijlstra  *
1307c3f654dSPeter Zijlstra  * Note that alterations to the list also require that the relevant flc_lock is
1317c3f654dSPeter Zijlstra  * held.
1321c8c601aSJeff Layton  */
1337c3f654dSPeter Zijlstra struct file_lock_list_struct {
1347c3f654dSPeter Zijlstra 	spinlock_t		lock;
1357c3f654dSPeter Zijlstra 	struct hlist_head	hlist;
1367c3f654dSPeter Zijlstra };
1377c3f654dSPeter Zijlstra static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
138aba37660SPeter Zijlstra DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
13988974691SJeff Layton 
140eb82dd39SJeff Layton 
1411c8c601aSJeff Layton /*
14248f74186SJeff Layton  * The blocked_hash is used to find POSIX lock loops for deadlock detection.
1437b2296afSJeff Layton  * It is protected by blocked_lock_lock.
14448f74186SJeff Layton  *
14548f74186SJeff Layton  * We hash locks by lockowner in order to optimize searching for the lock a
14648f74186SJeff Layton  * particular lockowner is waiting on.
14748f74186SJeff Layton  *
14848f74186SJeff Layton  * FIXME: make this value scale via some heuristic? We generally will want more
14948f74186SJeff Layton  * buckets when we have more lockowners holding locks, but that's a little
15048f74186SJeff Layton  * difficult to determine without knowing what the workload will look like.
1511c8c601aSJeff Layton  */
15248f74186SJeff Layton #define BLOCKED_HASH_BITS	7
15348f74186SJeff Layton static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
15488974691SJeff Layton 
1551c8c601aSJeff Layton /*
1567b2296afSJeff Layton  * This lock protects the blocked_hash. Generally, if you're accessing it, you
1577b2296afSJeff Layton  * want to be holding this lock.
1581c8c601aSJeff Layton  *
159ada5c1daSNeilBrown  * In addition, it also protects the fl->fl_blocked_requests list, and the
160ada5c1daSNeilBrown  * fl->fl_blocker pointer for file_lock structures that are acting as lock
161ada5c1daSNeilBrown  * requests (in contrast to those that are acting as records of acquired locks).
1621c8c601aSJeff Layton  *
1631c8c601aSJeff Layton  * Note that when we acquire this lock in order to change the above fields,
1646109c850SJeff Layton  * we often hold the flc_lock as well. In certain cases, when reading the fields
1651c8c601aSJeff Layton  * protected by this lock, we can skip acquiring it iff we already hold the
1666109c850SJeff Layton  * flc_lock.
1671c8c601aSJeff Layton  */
1687b2296afSJeff Layton static DEFINE_SPINLOCK(blocked_lock_lock);
1691da177e4SLinus Torvalds 
1704a075e39SJeff Layton static struct kmem_cache *flctx_cache __read_mostly;
171e18b890bSChristoph Lameter static struct kmem_cache *filelock_cache __read_mostly;
1721da177e4SLinus Torvalds 
1734a075e39SJeff Layton static struct file_lock_context *
locks_get_lock_context(struct inode * inode,int type)1745c1c669aSJeff Layton locks_get_lock_context(struct inode *inode, int type)
1754a075e39SJeff Layton {
176128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1774a075e39SJeff Layton 
178128a3785SDmitry Vyukov 	/* paired with cmpxchg() below */
179401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
180128a3785SDmitry Vyukov 	if (likely(ctx) || type == F_UNLCK)
1814a075e39SJeff Layton 		goto out;
1824a075e39SJeff Layton 
183128a3785SDmitry Vyukov 	ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
184128a3785SDmitry Vyukov 	if (!ctx)
1854a075e39SJeff Layton 		goto out;
1864a075e39SJeff Layton 
187128a3785SDmitry Vyukov 	spin_lock_init(&ctx->flc_lock);
188128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_flock);
189128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_posix);
190128a3785SDmitry Vyukov 	INIT_LIST_HEAD(&ctx->flc_lease);
1914a075e39SJeff Layton 
1924a075e39SJeff Layton 	/*
1934a075e39SJeff Layton 	 * Assign the pointer if it's not already assigned. If it is, then
1944a075e39SJeff Layton 	 * free the context we just allocated.
1954a075e39SJeff Layton 	 */
196128a3785SDmitry Vyukov 	if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
197128a3785SDmitry Vyukov 		kmem_cache_free(flctx_cache, ctx);
198401a8b8fSJeff Layton 		ctx = locks_inode_context(inode);
199128a3785SDmitry Vyukov 	}
2004a075e39SJeff Layton out:
2011890910fSJeff Layton 	trace_locks_get_lock_context(inode, type, ctx);
202128a3785SDmitry Vyukov 	return ctx;
2034a075e39SJeff Layton }
2044a075e39SJeff Layton 
205e24dadabSJeff Layton static void
locks_dump_ctx_list(struct list_head * list,char * list_type)206e24dadabSJeff Layton locks_dump_ctx_list(struct list_head *list, char *list_type)
207e24dadabSJeff Layton {
208e24dadabSJeff Layton 	struct file_lock *fl;
209e24dadabSJeff Layton 
210e24dadabSJeff Layton 	list_for_each_entry(fl, list, fl_list) {
211e24dadabSJeff Layton 		pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
212e24dadabSJeff Layton 	}
213e24dadabSJeff Layton }
214e24dadabSJeff Layton 
215e24dadabSJeff Layton static void
locks_check_ctx_lists(struct inode * inode)216e24dadabSJeff Layton locks_check_ctx_lists(struct inode *inode)
217e24dadabSJeff Layton {
218e24dadabSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
219e24dadabSJeff Layton 
220e24dadabSJeff Layton 	if (unlikely(!list_empty(&ctx->flc_flock) ||
221e24dadabSJeff Layton 		     !list_empty(&ctx->flc_posix) ||
222e24dadabSJeff Layton 		     !list_empty(&ctx->flc_lease))) {
223e24dadabSJeff Layton 		pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
224e24dadabSJeff Layton 			MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
225e24dadabSJeff Layton 			inode->i_ino);
226e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
227e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
228e24dadabSJeff Layton 		locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
229e24dadabSJeff Layton 	}
230e24dadabSJeff Layton }
231e24dadabSJeff Layton 
2323953704fSBenjamin Coddington static void
locks_check_ctx_file_list(struct file * filp,struct list_head * list,char * list_type)2333953704fSBenjamin Coddington locks_check_ctx_file_list(struct file *filp, struct list_head *list,
2343953704fSBenjamin Coddington 				char *list_type)
2353953704fSBenjamin Coddington {
2363953704fSBenjamin Coddington 	struct file_lock *fl;
237c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
2383953704fSBenjamin Coddington 
2393953704fSBenjamin Coddington 	list_for_each_entry(fl, list, fl_list)
2403953704fSBenjamin Coddington 		if (fl->fl_file == filp)
2413953704fSBenjamin Coddington 			pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
2423953704fSBenjamin Coddington 				" fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
2433953704fSBenjamin Coddington 				list_type, MAJOR(inode->i_sb->s_dev),
2443953704fSBenjamin Coddington 				MINOR(inode->i_sb->s_dev), inode->i_ino,
2453953704fSBenjamin Coddington 				fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
2463953704fSBenjamin Coddington }
2473953704fSBenjamin Coddington 
2484a075e39SJeff Layton void
locks_free_lock_context(struct inode * inode)249f27a0fe0SJeff Layton locks_free_lock_context(struct inode *inode)
2504a075e39SJeff Layton {
251401a8b8fSJeff Layton 	struct file_lock_context *ctx = locks_inode_context(inode);
252f27a0fe0SJeff Layton 
253e24dadabSJeff Layton 	if (unlikely(ctx)) {
254e24dadabSJeff Layton 		locks_check_ctx_lists(inode);
2554a075e39SJeff Layton 		kmem_cache_free(flctx_cache, ctx);
2564a075e39SJeff Layton 	}
2574a075e39SJeff Layton }
2584a075e39SJeff Layton 
locks_init_lock_heads(struct file_lock * fl)259ee19cc40SMiklos Szeredi static void locks_init_lock_heads(struct file_lock *fl)
260a51cb91dSMiklos Szeredi {
261139ca04eSJeff Layton 	INIT_HLIST_NODE(&fl->fl_link);
2626dee60f6SJeff Layton 	INIT_LIST_HEAD(&fl->fl_list);
263ada5c1daSNeilBrown 	INIT_LIST_HEAD(&fl->fl_blocked_requests);
264ada5c1daSNeilBrown 	INIT_LIST_HEAD(&fl->fl_blocked_member);
265ee19cc40SMiklos Szeredi 	init_waitqueue_head(&fl->fl_wait);
266a51cb91dSMiklos Szeredi }
267a51cb91dSMiklos Szeredi 
2681da177e4SLinus Torvalds /* Allocate an empty lock structure. */
locks_alloc_lock(void)269c5b1f0d9SArnd Bergmann struct file_lock *locks_alloc_lock(void)
2701da177e4SLinus Torvalds {
271ee19cc40SMiklos Szeredi 	struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
272a51cb91dSMiklos Szeredi 
273a51cb91dSMiklos Szeredi 	if (fl)
274ee19cc40SMiklos Szeredi 		locks_init_lock_heads(fl);
275a51cb91dSMiklos Szeredi 
276a51cb91dSMiklos Szeredi 	return fl;
2771da177e4SLinus Torvalds }
278c5b1f0d9SArnd Bergmann EXPORT_SYMBOL_GPL(locks_alloc_lock);
2791da177e4SLinus Torvalds 
locks_release_private(struct file_lock * fl)280a9e61e25SFelix Blyakher void locks_release_private(struct file_lock *fl)
28147831f35STrond Myklebust {
2825926459eSNeilBrown 	BUG_ON(waitqueue_active(&fl->fl_wait));
2835926459eSNeilBrown 	BUG_ON(!list_empty(&fl->fl_list));
2845926459eSNeilBrown 	BUG_ON(!list_empty(&fl->fl_blocked_requests));
2855926459eSNeilBrown 	BUG_ON(!list_empty(&fl->fl_blocked_member));
2865926459eSNeilBrown 	BUG_ON(!hlist_unhashed(&fl->fl_link));
2875926459eSNeilBrown 
28847831f35STrond Myklebust 	if (fl->fl_ops) {
28947831f35STrond Myklebust 		if (fl->fl_ops->fl_release_private)
29047831f35STrond Myklebust 			fl->fl_ops->fl_release_private(fl);
29147831f35STrond Myklebust 		fl->fl_ops = NULL;
29247831f35STrond Myklebust 	}
29347831f35STrond Myklebust 
2945c97d7b1SKinglong Mee 	if (fl->fl_lmops) {
295cae80b30SJeff Layton 		if (fl->fl_lmops->lm_put_owner) {
296cae80b30SJeff Layton 			fl->fl_lmops->lm_put_owner(fl->fl_owner);
297cae80b30SJeff Layton 			fl->fl_owner = NULL;
298cae80b30SJeff Layton 		}
2995c97d7b1SKinglong Mee 		fl->fl_lmops = NULL;
3005c97d7b1SKinglong Mee 	}
30147831f35STrond Myklebust }
302a9e61e25SFelix Blyakher EXPORT_SYMBOL_GPL(locks_release_private);
30347831f35STrond Myklebust 
304591502c5SDai Ngo /**
305591502c5SDai Ngo  * locks_owner_has_blockers - Check for blocking lock requests
306591502c5SDai Ngo  * @flctx: file lock context
307591502c5SDai Ngo  * @owner: lock owner
308591502c5SDai Ngo  *
309591502c5SDai Ngo  * Return values:
310591502c5SDai Ngo  *   %true: @owner has at least one blocker
311591502c5SDai Ngo  *   %false: @owner has no blockers
312591502c5SDai Ngo  */
locks_owner_has_blockers(struct file_lock_context * flctx,fl_owner_t owner)313591502c5SDai Ngo bool locks_owner_has_blockers(struct file_lock_context *flctx,
314591502c5SDai Ngo 		fl_owner_t owner)
315591502c5SDai Ngo {
316591502c5SDai Ngo 	struct file_lock *fl;
317591502c5SDai Ngo 
318591502c5SDai Ngo 	spin_lock(&flctx->flc_lock);
319591502c5SDai Ngo 	list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
320591502c5SDai Ngo 		if (fl->fl_owner != owner)
321591502c5SDai Ngo 			continue;
322591502c5SDai Ngo 		if (!list_empty(&fl->fl_blocked_requests)) {
323591502c5SDai Ngo 			spin_unlock(&flctx->flc_lock);
324591502c5SDai Ngo 			return true;
325591502c5SDai Ngo 		}
326591502c5SDai Ngo 	}
327591502c5SDai Ngo 	spin_unlock(&flctx->flc_lock);
328591502c5SDai Ngo 	return false;
329591502c5SDai Ngo }
330591502c5SDai Ngo EXPORT_SYMBOL_GPL(locks_owner_has_blockers);
331591502c5SDai Ngo 
3321da177e4SLinus Torvalds /* Free a lock which is not in use. */
locks_free_lock(struct file_lock * fl)33305fa3135SJ. Bruce Fields void locks_free_lock(struct file_lock *fl)
3341da177e4SLinus Torvalds {
33547831f35STrond Myklebust 	locks_release_private(fl);
3361da177e4SLinus Torvalds 	kmem_cache_free(filelock_cache, fl);
3371da177e4SLinus Torvalds }
33805fa3135SJ. Bruce Fields EXPORT_SYMBOL(locks_free_lock);
3391da177e4SLinus Torvalds 
340ed9814d8SJeff Layton static void
locks_dispose_list(struct list_head * dispose)341ed9814d8SJeff Layton locks_dispose_list(struct list_head *dispose)
342ed9814d8SJeff Layton {
343ed9814d8SJeff Layton 	struct file_lock *fl;
344ed9814d8SJeff Layton 
345ed9814d8SJeff Layton 	while (!list_empty(dispose)) {
3466dee60f6SJeff Layton 		fl = list_first_entry(dispose, struct file_lock, fl_list);
3476dee60f6SJeff Layton 		list_del_init(&fl->fl_list);
348ed9814d8SJeff Layton 		locks_free_lock(fl);
349ed9814d8SJeff Layton 	}
350ed9814d8SJeff Layton }
351ed9814d8SJeff Layton 
locks_init_lock(struct file_lock * fl)3521da177e4SLinus Torvalds void locks_init_lock(struct file_lock *fl)
3531da177e4SLinus Torvalds {
354ee19cc40SMiklos Szeredi 	memset(fl, 0, sizeof(struct file_lock));
355ee19cc40SMiklos Szeredi 	locks_init_lock_heads(fl);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds EXPORT_SYMBOL(locks_init_lock);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds  * Initialize a new lock from an existing file_lock structure.
3611da177e4SLinus Torvalds  */
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)3623fe0fff1SKinglong Mee void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	new->fl_owner = fl->fl_owner;
3651da177e4SLinus Torvalds 	new->fl_pid = fl->fl_pid;
3660996905fSTrond Myklebust 	new->fl_file = NULL;
3671da177e4SLinus Torvalds 	new->fl_flags = fl->fl_flags;
3681da177e4SLinus Torvalds 	new->fl_type = fl->fl_type;
3691da177e4SLinus Torvalds 	new->fl_start = fl->fl_start;
3701da177e4SLinus Torvalds 	new->fl_end = fl->fl_end;
371f328296eSKinglong Mee 	new->fl_lmops = fl->fl_lmops;
3720996905fSTrond Myklebust 	new->fl_ops = NULL;
373f328296eSKinglong Mee 
374f328296eSKinglong Mee 	if (fl->fl_lmops) {
375f328296eSKinglong Mee 		if (fl->fl_lmops->lm_get_owner)
376cae80b30SJeff Layton 			fl->fl_lmops->lm_get_owner(fl->fl_owner);
377f328296eSKinglong Mee 	}
3780996905fSTrond Myklebust }
3793fe0fff1SKinglong Mee EXPORT_SYMBOL(locks_copy_conflock);
3800996905fSTrond Myklebust 
locks_copy_lock(struct file_lock * new,struct file_lock * fl)3810996905fSTrond Myklebust void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
3820996905fSTrond Myklebust {
383566709bdSJeff Layton 	/* "new" must be a freshly-initialized lock */
384566709bdSJeff Layton 	WARN_ON_ONCE(new->fl_ops);
3850996905fSTrond Myklebust 
3863fe0fff1SKinglong Mee 	locks_copy_conflock(new, fl);
387f328296eSKinglong Mee 
3880996905fSTrond Myklebust 	new->fl_file = fl->fl_file;
3891da177e4SLinus Torvalds 	new->fl_ops = fl->fl_ops;
39047831f35STrond Myklebust 
391f328296eSKinglong Mee 	if (fl->fl_ops) {
392f328296eSKinglong Mee 		if (fl->fl_ops->fl_copy_lock)
393f328296eSKinglong Mee 			fl->fl_ops->fl_copy_lock(new, fl);
394f328296eSKinglong Mee 	}
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds EXPORT_SYMBOL(locks_copy_lock);
3971da177e4SLinus Torvalds 
locks_move_blocks(struct file_lock * new,struct file_lock * fl)3985946c431SNeilBrown static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
3995946c431SNeilBrown {
4005946c431SNeilBrown 	struct file_lock *f;
4015946c431SNeilBrown 
4025946c431SNeilBrown 	/*
4035946c431SNeilBrown 	 * As ctx->flc_lock is held, new requests cannot be added to
4045946c431SNeilBrown 	 * ->fl_blocked_requests, so we don't need a lock to check if it
4055946c431SNeilBrown 	 * is empty.
4065946c431SNeilBrown 	 */
4075946c431SNeilBrown 	if (list_empty(&fl->fl_blocked_requests))
4085946c431SNeilBrown 		return;
4095946c431SNeilBrown 	spin_lock(&blocked_lock_lock);
4105946c431SNeilBrown 	list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
411bf77ae4cSNeilBrown 	list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member)
4125946c431SNeilBrown 		f->fl_blocker = new;
4135946c431SNeilBrown 	spin_unlock(&blocked_lock_lock);
4145946c431SNeilBrown }
4155946c431SNeilBrown 
flock_translate_cmd(int cmd)4161da177e4SLinus Torvalds static inline int flock_translate_cmd(int cmd) {
4171da177e4SLinus Torvalds 	switch (cmd) {
4181da177e4SLinus Torvalds 	case LOCK_SH:
4191da177e4SLinus Torvalds 		return F_RDLCK;
4201da177e4SLinus Torvalds 	case LOCK_EX:
4211da177e4SLinus Torvalds 		return F_WRLCK;
4221da177e4SLinus Torvalds 	case LOCK_UN:
4231da177e4SLinus Torvalds 		return F_UNLCK;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	return -EINVAL;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /* Fill in a file_lock structure with an appropriate FLOCK lock. */
flock_make_lock(struct file * filp,struct file_lock * fl,int type)4294149be7bSKuniyuki Iwashima static void flock_make_lock(struct file *filp, struct file_lock *fl, int type)
4301da177e4SLinus Torvalds {
431d6367d62SNeilBrown 	locks_init_lock(fl);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	fl->fl_file = filp;
43473a8f5f7SChristoph Hellwig 	fl->fl_owner = filp;
4351da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
4361da177e4SLinus Torvalds 	fl->fl_flags = FL_FLOCK;
4371da177e4SLinus Torvalds 	fl->fl_type = type;
4381da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
assign_type(struct file_lock * fl,int type)441ed5f17f6SLuca Vizzarro static int assign_type(struct file_lock *fl, int type)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	switch (type) {
4441da177e4SLinus Torvalds 	case F_RDLCK:
4451da177e4SLinus Torvalds 	case F_WRLCK:
4461da177e4SLinus Torvalds 	case F_UNLCK:
4471da177e4SLinus Torvalds 		fl->fl_type = type;
4481da177e4SLinus Torvalds 		break;
4491da177e4SLinus Torvalds 	default:
4501da177e4SLinus Torvalds 		return -EINVAL;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 	return 0;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
flock64_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock64 * l)455ef12e72aSJ. Bruce Fields static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
456ef12e72aSJ. Bruce Fields 				 struct flock64 *l)
457ef12e72aSJ. Bruce Fields {
458ef12e72aSJ. Bruce Fields 	switch (l->l_whence) {
459ef12e72aSJ. Bruce Fields 	case SEEK_SET:
460ef12e72aSJ. Bruce Fields 		fl->fl_start = 0;
461ef12e72aSJ. Bruce Fields 		break;
462ef12e72aSJ. Bruce Fields 	case SEEK_CUR:
463ef12e72aSJ. Bruce Fields 		fl->fl_start = filp->f_pos;
464ef12e72aSJ. Bruce Fields 		break;
465ef12e72aSJ. Bruce Fields 	case SEEK_END:
466ef12e72aSJ. Bruce Fields 		fl->fl_start = i_size_read(file_inode(filp));
467ef12e72aSJ. Bruce Fields 		break;
468ef12e72aSJ. Bruce Fields 	default:
469ef12e72aSJ. Bruce Fields 		return -EINVAL;
470ef12e72aSJ. Bruce Fields 	}
471ef12e72aSJ. Bruce Fields 	if (l->l_start > OFFSET_MAX - fl->fl_start)
472ef12e72aSJ. Bruce Fields 		return -EOVERFLOW;
473ef12e72aSJ. Bruce Fields 	fl->fl_start += l->l_start;
474ef12e72aSJ. Bruce Fields 	if (fl->fl_start < 0)
475ef12e72aSJ. Bruce Fields 		return -EINVAL;
476ef12e72aSJ. Bruce Fields 
477ef12e72aSJ. Bruce Fields 	/* POSIX-1996 leaves the case l->l_len < 0 undefined;
478ef12e72aSJ. Bruce Fields 	   POSIX-2001 defines it. */
479ef12e72aSJ. Bruce Fields 	if (l->l_len > 0) {
480ef12e72aSJ. Bruce Fields 		if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
481ef12e72aSJ. Bruce Fields 			return -EOVERFLOW;
48216238415SLuo Meng 		fl->fl_end = fl->fl_start + (l->l_len - 1);
483ef12e72aSJ. Bruce Fields 
484ef12e72aSJ. Bruce Fields 	} else if (l->l_len < 0) {
485ef12e72aSJ. Bruce Fields 		if (fl->fl_start + l->l_len < 0)
486ef12e72aSJ. Bruce Fields 			return -EINVAL;
487ef12e72aSJ. Bruce Fields 		fl->fl_end = fl->fl_start - 1;
488ef12e72aSJ. Bruce Fields 		fl->fl_start += l->l_len;
489ef12e72aSJ. Bruce Fields 	} else
490ef12e72aSJ. Bruce Fields 		fl->fl_end = OFFSET_MAX;
491ef12e72aSJ. Bruce Fields 
492ef12e72aSJ. Bruce Fields 	fl->fl_owner = current->files;
493ef12e72aSJ. Bruce Fields 	fl->fl_pid = current->tgid;
494ef12e72aSJ. Bruce Fields 	fl->fl_file = filp;
495ef12e72aSJ. Bruce Fields 	fl->fl_flags = FL_POSIX;
496ef12e72aSJ. Bruce Fields 	fl->fl_ops = NULL;
497ef12e72aSJ. Bruce Fields 	fl->fl_lmops = NULL;
498ef12e72aSJ. Bruce Fields 
499ef12e72aSJ. Bruce Fields 	return assign_type(fl, l->l_type);
500ef12e72aSJ. Bruce Fields }
501ef12e72aSJ. Bruce Fields 
5021da177e4SLinus Torvalds /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
5031da177e4SLinus Torvalds  * style lock.
5041da177e4SLinus Torvalds  */
flock_to_posix_lock(struct file * filp,struct file_lock * fl,struct flock * l)5051da177e4SLinus Torvalds static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
5061da177e4SLinus Torvalds 			       struct flock *l)
5071da177e4SLinus Torvalds {
508ef12e72aSJ. Bruce Fields 	struct flock64 ll = {
509ef12e72aSJ. Bruce Fields 		.l_type = l->l_type,
510ef12e72aSJ. Bruce Fields 		.l_whence = l->l_whence,
511ef12e72aSJ. Bruce Fields 		.l_start = l->l_start,
512ef12e72aSJ. Bruce Fields 		.l_len = l->l_len,
513ef12e72aSJ. Bruce Fields 	};
5141da177e4SLinus Torvalds 
515ef12e72aSJ. Bruce Fields 	return flock64_to_posix_lock(filp, fl, &ll);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /* default lease lock manager operations */
5194d01b7f5SJeff Layton static bool
lease_break_callback(struct file_lock * fl)5204d01b7f5SJeff Layton lease_break_callback(struct file_lock *fl)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
5234d01b7f5SJeff Layton 	return false;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261c7dd2ffSJeff Layton static void
lease_setup(struct file_lock * fl,void ** priv)5271c7dd2ffSJeff Layton lease_setup(struct file_lock *fl, void **priv)
5281c7dd2ffSJeff Layton {
5291c7dd2ffSJeff Layton 	struct file *filp = fl->fl_file;
5301c7dd2ffSJeff Layton 	struct fasync_struct *fa = *priv;
5311c7dd2ffSJeff Layton 
5321c7dd2ffSJeff Layton 	/*
5331c7dd2ffSJeff Layton 	 * fasync_insert_entry() returns the old entry if any. If there was no
5341c7dd2ffSJeff Layton 	 * old entry, then it used "priv" and inserted it into the fasync list.
5351c7dd2ffSJeff Layton 	 * Clear the pointer to indicate that it shouldn't be freed.
5361c7dd2ffSJeff Layton 	 */
5371c7dd2ffSJeff Layton 	if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
5381c7dd2ffSJeff Layton 		*priv = NULL;
5391c7dd2ffSJeff Layton 
54001919134SEric W. Biederman 	__f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
5411c7dd2ffSJeff Layton }
5421c7dd2ffSJeff Layton 
5437b021967SAlexey Dobriyan static const struct lock_manager_operations lease_manager_ops = {
5448fb47a4fSJ. Bruce Fields 	.lm_break = lease_break_callback,
5458fb47a4fSJ. Bruce Fields 	.lm_change = lease_modify,
5461c7dd2ffSJeff Layton 	.lm_setup = lease_setup,
5471da177e4SLinus Torvalds };
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * Initialize a lease, use the default lock manager operations
5511da177e4SLinus Torvalds  */
lease_init(struct file * filp,int type,struct file_lock * fl)552ed5f17f6SLuca Vizzarro static int lease_init(struct file *filp, int type, struct file_lock *fl)
5531da177e4SLinus Torvalds {
55475dff55aSTrond Myklebust 	if (assign_type(fl, type) != 0)
55575dff55aSTrond Myklebust 		return -EINVAL;
55675dff55aSTrond Myklebust 
5577ca76311SJeff Layton 	fl->fl_owner = filp;
5581da177e4SLinus Torvalds 	fl->fl_pid = current->tgid;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	fl->fl_file = filp;
5611da177e4SLinus Torvalds 	fl->fl_flags = FL_LEASE;
5621da177e4SLinus Torvalds 	fl->fl_start = 0;
5631da177e4SLinus Torvalds 	fl->fl_end = OFFSET_MAX;
5641da177e4SLinus Torvalds 	fl->fl_ops = NULL;
5651da177e4SLinus Torvalds 	fl->fl_lmops = &lease_manager_ops;
5661da177e4SLinus Torvalds 	return 0;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /* Allocate a file_lock initialised to this type of lease */
lease_alloc(struct file * filp,int type)570ed5f17f6SLuca Vizzarro static struct file_lock *lease_alloc(struct file *filp, int type)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	struct file_lock *fl = locks_alloc_lock();
57375dff55aSTrond Myklebust 	int error = -ENOMEM;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (fl == NULL)
576e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	error = lease_init(filp, type, fl);
57975dff55aSTrond Myklebust 	if (error) {
58075dff55aSTrond Myklebust 		locks_free_lock(fl);
581e32b8ee2SJ. Bruce Fields 		return ERR_PTR(error);
58275dff55aSTrond Myklebust 	}
583e32b8ee2SJ. Bruce Fields 	return fl;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /* Check if two locks overlap each other.
5871da177e4SLinus Torvalds  */
locks_overlap(struct file_lock * fl1,struct file_lock * fl2)5881da177e4SLinus Torvalds static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	return ((fl1->fl_end >= fl2->fl_start) &&
5911da177e4SLinus Torvalds 		(fl2->fl_end >= fl1->fl_start));
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /*
5951da177e4SLinus Torvalds  * Check whether two locks have the same owner.
5961da177e4SLinus Torvalds  */
posix_same_owner(struct file_lock * fl1,struct file_lock * fl2)59733443c42SMatt Mackall static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	return fl1->fl_owner == fl2->fl_owner;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6026109c850SJeff Layton /* Must be called with the flc_lock held! */
locks_insert_global_locks(struct file_lock * fl)6036ca10ed8SJeff Layton static void locks_insert_global_locks(struct file_lock *fl)
60488974691SJeff Layton {
6057c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
6067c3f654dSPeter Zijlstra 
607aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
608aba37660SPeter Zijlstra 
6097c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
6107012b02aSJeff Layton 	fl->fl_link_cpu = smp_processor_id();
6117c3f654dSPeter Zijlstra 	hlist_add_head(&fl->fl_link, &fll->hlist);
6127c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
61388974691SJeff Layton }
61488974691SJeff Layton 
6156109c850SJeff Layton /* Must be called with the flc_lock held! */
locks_delete_global_locks(struct file_lock * fl)6166ca10ed8SJeff Layton static void locks_delete_global_locks(struct file_lock *fl)
61788974691SJeff Layton {
6187c3f654dSPeter Zijlstra 	struct file_lock_list_struct *fll;
6197c3f654dSPeter Zijlstra 
620aba37660SPeter Zijlstra 	percpu_rwsem_assert_held(&file_rwsem);
621aba37660SPeter Zijlstra 
6227012b02aSJeff Layton 	/*
6237012b02aSJeff Layton 	 * Avoid taking lock if already unhashed. This is safe since this check
6246109c850SJeff Layton 	 * is done while holding the flc_lock, and new insertions into the list
6257012b02aSJeff Layton 	 * also require that it be held.
6267012b02aSJeff Layton 	 */
6277012b02aSJeff Layton 	if (hlist_unhashed(&fl->fl_link))
6287012b02aSJeff Layton 		return;
6297c3f654dSPeter Zijlstra 
6307c3f654dSPeter Zijlstra 	fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
6317c3f654dSPeter Zijlstra 	spin_lock(&fll->lock);
632139ca04eSJeff Layton 	hlist_del_init(&fl->fl_link);
6337c3f654dSPeter Zijlstra 	spin_unlock(&fll->lock);
63488974691SJeff Layton }
63588974691SJeff Layton 
6363999e493SJeff Layton static unsigned long
posix_owner_key(struct file_lock * fl)6373999e493SJeff Layton posix_owner_key(struct file_lock *fl)
6383999e493SJeff Layton {
6393999e493SJeff Layton 	return (unsigned long)fl->fl_owner;
6403999e493SJeff Layton }
6413999e493SJeff Layton 
locks_insert_global_blocked(struct file_lock * waiter)6426ca10ed8SJeff Layton static void locks_insert_global_blocked(struct file_lock *waiter)
64388974691SJeff Layton {
644663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
645663d5af7SDaniel Wagner 
6463999e493SJeff Layton 	hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
64788974691SJeff Layton }
64888974691SJeff Layton 
locks_delete_global_blocked(struct file_lock * waiter)6496ca10ed8SJeff Layton static void locks_delete_global_blocked(struct file_lock *waiter)
65088974691SJeff Layton {
651663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
652663d5af7SDaniel Wagner 
65348f74186SJeff Layton 	hash_del(&waiter->fl_link);
65488974691SJeff Layton }
65588974691SJeff Layton 
6561da177e4SLinus Torvalds /* Remove waiter from blocker's block list.
6571da177e4SLinus Torvalds  * When blocker ends up pointing to itself then the list is empty.
6581c8c601aSJeff Layton  *
6597b2296afSJeff Layton  * Must be called with blocked_lock_lock held.
6601da177e4SLinus Torvalds  */
__locks_delete_block(struct file_lock * waiter)66133443c42SMatt Mackall static void __locks_delete_block(struct file_lock *waiter)
6621da177e4SLinus Torvalds {
66388974691SJeff Layton 	locks_delete_global_blocked(waiter);
664ada5c1daSNeilBrown 	list_del_init(&waiter->fl_blocked_member);
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
__locks_wake_up_blocks(struct file_lock * blocker)667ad6bbd8bSNeilBrown static void __locks_wake_up_blocks(struct file_lock *blocker)
668ad6bbd8bSNeilBrown {
669ad6bbd8bSNeilBrown 	while (!list_empty(&blocker->fl_blocked_requests)) {
670ad6bbd8bSNeilBrown 		struct file_lock *waiter;
671ad6bbd8bSNeilBrown 
672ad6bbd8bSNeilBrown 		waiter = list_first_entry(&blocker->fl_blocked_requests,
673ad6bbd8bSNeilBrown 					  struct file_lock, fl_blocked_member);
674ad6bbd8bSNeilBrown 		__locks_delete_block(waiter);
675ad6bbd8bSNeilBrown 		if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
676ad6bbd8bSNeilBrown 			waiter->fl_lmops->lm_notify(waiter);
677ad6bbd8bSNeilBrown 		else
678ad6bbd8bSNeilBrown 			wake_up(&waiter->fl_wait);
679dcf23ac3SLinus Torvalds 
680dcf23ac3SLinus Torvalds 		/*
681dcf23ac3SLinus Torvalds 		 * The setting of fl_blocker to NULL marks the "done"
682dcf23ac3SLinus Torvalds 		 * point in deleting a block. Paired with acquire at the top
683dcf23ac3SLinus Torvalds 		 * of locks_delete_block().
684dcf23ac3SLinus Torvalds 		 */
685dcf23ac3SLinus Torvalds 		smp_store_release(&waiter->fl_blocker, NULL);
686ad6bbd8bSNeilBrown 	}
687ad6bbd8bSNeilBrown }
688ad6bbd8bSNeilBrown 
689cb03f94fSNeilBrown /**
690529adfe8SMauro Carvalho Chehab  *	locks_delete_block - stop waiting for a file lock
691cb03f94fSNeilBrown  *	@waiter: the lock which was waiting
692cb03f94fSNeilBrown  *
693cb03f94fSNeilBrown  *	lockd/nfsd need to disconnect the lock while working on it.
694cb03f94fSNeilBrown  */
locks_delete_block(struct file_lock * waiter)695cb03f94fSNeilBrown int locks_delete_block(struct file_lock *waiter)
6961da177e4SLinus Torvalds {
697cb03f94fSNeilBrown 	int status = -ENOENT;
698cb03f94fSNeilBrown 
699dcf23ac3SLinus Torvalds 	/*
700dcf23ac3SLinus Torvalds 	 * If fl_blocker is NULL, it won't be set again as this thread "owns"
701dcf23ac3SLinus Torvalds 	 * the lock and is the only one that might try to claim the lock.
702dcf23ac3SLinus Torvalds 	 *
703dcf23ac3SLinus Torvalds 	 * We use acquire/release to manage fl_blocker so that we can
704dcf23ac3SLinus Torvalds 	 * optimize away taking the blocked_lock_lock in many cases.
705dcf23ac3SLinus Torvalds 	 *
706dcf23ac3SLinus Torvalds 	 * The smp_load_acquire guarantees two things:
707dcf23ac3SLinus Torvalds 	 *
708dcf23ac3SLinus Torvalds 	 * 1/ that fl_blocked_requests can be tested locklessly. If something
709dcf23ac3SLinus Torvalds 	 * was recently added to that list it must have been in a locked region
710dcf23ac3SLinus Torvalds 	 * *before* the locked region when fl_blocker was set to NULL.
711dcf23ac3SLinus Torvalds 	 *
712dcf23ac3SLinus Torvalds 	 * 2/ that no other thread is accessing 'waiter', so it is safe to free
713dcf23ac3SLinus Torvalds 	 * it.  __locks_wake_up_blocks is careful not to touch waiter after
714dcf23ac3SLinus Torvalds 	 * fl_blocker is released.
715dcf23ac3SLinus Torvalds 	 *
716dcf23ac3SLinus Torvalds 	 * If a lockless check of fl_blocker shows it to be NULL, we know that
717dcf23ac3SLinus Torvalds 	 * no new locks can be inserted into its fl_blocked_requests list, and
718dcf23ac3SLinus Torvalds 	 * can avoid doing anything further if the list is empty.
719dcf23ac3SLinus Torvalds 	 */
720dcf23ac3SLinus Torvalds 	if (!smp_load_acquire(&waiter->fl_blocker) &&
721dcf23ac3SLinus Torvalds 	    list_empty(&waiter->fl_blocked_requests))
722dcf23ac3SLinus Torvalds 		return status;
723dcf23ac3SLinus Torvalds 
7247b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
725cb03f94fSNeilBrown 	if (waiter->fl_blocker)
726cb03f94fSNeilBrown 		status = 0;
7275946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
7281da177e4SLinus Torvalds 	__locks_delete_block(waiter);
729dcf23ac3SLinus Torvalds 
730dcf23ac3SLinus Torvalds 	/*
731dcf23ac3SLinus Torvalds 	 * The setting of fl_blocker to NULL marks the "done" point in deleting
732dcf23ac3SLinus Torvalds 	 * a block. Paired with acquire at the top of this function.
733dcf23ac3SLinus Torvalds 	 */
734dcf23ac3SLinus Torvalds 	smp_store_release(&waiter->fl_blocker, NULL);
7357b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
736cb03f94fSNeilBrown 	return status;
7371da177e4SLinus Torvalds }
738cb03f94fSNeilBrown EXPORT_SYMBOL(locks_delete_block);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Insert waiter into blocker's block list.
7411da177e4SLinus Torvalds  * We use a circular list so that processes can be easily woken up in
7421da177e4SLinus Torvalds  * the order they blocked. The documentation doesn't require this but
7431da177e4SLinus Torvalds  * it seems like the reasonable thing to do.
7441c8c601aSJeff Layton  *
7456109c850SJeff Layton  * Must be called with both the flc_lock and blocked_lock_lock held. The
746ada5c1daSNeilBrown  * fl_blocked_requests list itself is protected by the blocked_lock_lock,
747ada5c1daSNeilBrown  * but by ensuring that the flc_lock is also held on insertions we can avoid
748ada5c1daSNeilBrown  * taking the blocked_lock_lock in some cases when we see that the
749ada5c1daSNeilBrown  * fl_blocked_requests list is empty.
750fd7732e0SNeilBrown  *
751fd7732e0SNeilBrown  * Rather than just adding to the list, we check for conflicts with any existing
752fd7732e0SNeilBrown  * waiters, and add beneath any waiter that blocks the new waiter.
753fd7732e0SNeilBrown  * Thus wakeups don't happen until needed.
7541da177e4SLinus Torvalds  */
__locks_insert_block(struct file_lock * blocker,struct file_lock * waiter,bool conflict (struct file_lock *,struct file_lock *))7551c8c601aSJeff Layton static void __locks_insert_block(struct file_lock *blocker,
756fd7732e0SNeilBrown 				 struct file_lock *waiter,
757fd7732e0SNeilBrown 				 bool conflict(struct file_lock *,
758fd7732e0SNeilBrown 					       struct file_lock *))
7591da177e4SLinus Torvalds {
760fd7732e0SNeilBrown 	struct file_lock *fl;
761ada5c1daSNeilBrown 	BUG_ON(!list_empty(&waiter->fl_blocked_member));
762fd7732e0SNeilBrown 
763fd7732e0SNeilBrown new_blocker:
764fd7732e0SNeilBrown 	list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member)
765fd7732e0SNeilBrown 		if (conflict(fl, waiter)) {
766fd7732e0SNeilBrown 			blocker =  fl;
767fd7732e0SNeilBrown 			goto new_blocker;
768fd7732e0SNeilBrown 		}
769ada5c1daSNeilBrown 	waiter->fl_blocker = blocker;
770ada5c1daSNeilBrown 	list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
771cff2fce5SJeff Layton 	if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
7721c8c601aSJeff Layton 		locks_insert_global_blocked(waiter);
7735946c431SNeilBrown 
7745946c431SNeilBrown 	/* The requests in waiter->fl_blocked are known to conflict with
7755946c431SNeilBrown 	 * waiter, but might not conflict with blocker, or the requests
7765946c431SNeilBrown 	 * and lock which block it.  So they all need to be woken.
7775946c431SNeilBrown 	 */
7785946c431SNeilBrown 	__locks_wake_up_blocks(waiter);
7791c8c601aSJeff Layton }
7801c8c601aSJeff Layton 
7816109c850SJeff Layton /* Must be called with flc_lock held. */
locks_insert_block(struct file_lock * blocker,struct file_lock * waiter,bool conflict (struct file_lock *,struct file_lock *))7821c8c601aSJeff Layton static void locks_insert_block(struct file_lock *blocker,
783fd7732e0SNeilBrown 			       struct file_lock *waiter,
784fd7732e0SNeilBrown 			       bool conflict(struct file_lock *,
785fd7732e0SNeilBrown 					     struct file_lock *))
7861c8c601aSJeff Layton {
7877b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
788fd7732e0SNeilBrown 	__locks_insert_block(blocker, waiter, conflict);
7897b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921cb36012SJeff Layton /*
7931cb36012SJeff Layton  * Wake up processes blocked waiting for blocker.
7941cb36012SJeff Layton  *
7956109c850SJeff Layton  * Must be called with the inode->flc_lock held!
7961da177e4SLinus Torvalds  */
locks_wake_up_blocks(struct file_lock * blocker)7971da177e4SLinus Torvalds static void locks_wake_up_blocks(struct file_lock *blocker)
7981da177e4SLinus Torvalds {
7994e8c765dSJeff Layton 	/*
8004e8c765dSJeff Layton 	 * Avoid taking global lock if list is empty. This is safe since new
8016109c850SJeff Layton 	 * blocked requests are only added to the list under the flc_lock, and
802ada5c1daSNeilBrown 	 * the flc_lock is always held here. Note that removal from the
803ada5c1daSNeilBrown 	 * fl_blocked_requests list does not require the flc_lock, so we must
804ada5c1daSNeilBrown 	 * recheck list_empty() after acquiring the blocked_lock_lock.
8054e8c765dSJeff Layton 	 */
806ada5c1daSNeilBrown 	if (list_empty(&blocker->fl_blocked_requests))
8074e8c765dSJeff Layton 		return;
8084e8c765dSJeff Layton 
8097b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
810ad6bbd8bSNeilBrown 	__locks_wake_up_blocks(blocker);
8117b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8145263e31eSJeff Layton static void
locks_insert_lock_ctx(struct file_lock * fl,struct list_head * before)815e084c1bdSJeff Layton locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
8165263e31eSJeff Layton {
8175263e31eSJeff Layton 	list_add_tail(&fl->fl_list, before);
8185263e31eSJeff Layton 	locks_insert_global_locks(fl);
8195263e31eSJeff Layton }
8205263e31eSJeff Layton 
8218634b51fSJeff Layton static void
locks_unlink_lock_ctx(struct file_lock * fl)822e084c1bdSJeff Layton locks_unlink_lock_ctx(struct file_lock *fl)
8231da177e4SLinus Torvalds {
82488974691SJeff Layton 	locks_delete_global_locks(fl);
8258634b51fSJeff Layton 	list_del_init(&fl->fl_list);
8261da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
82724cbe784SJeff Layton }
82824cbe784SJeff Layton 
8295263e31eSJeff Layton static void
locks_delete_lock_ctx(struct file_lock * fl,struct list_head * dispose)830e084c1bdSJeff Layton locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
8315263e31eSJeff Layton {
832e084c1bdSJeff Layton 	locks_unlink_lock_ctx(fl);
8338634b51fSJeff Layton 	if (dispose)
8348634b51fSJeff Layton 		list_add(&fl->fl_list, dispose);
8358634b51fSJeff Layton 	else
8368634b51fSJeff Layton 		locks_free_lock(fl);
8375263e31eSJeff Layton }
8385263e31eSJeff Layton 
8391da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
8401da177e4SLinus Torvalds  * checks for shared/exclusive status of overlapping locks.
8411da177e4SLinus Torvalds  */
locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)842c0e15908SNeilBrown static bool locks_conflict(struct file_lock *caller_fl,
843c0e15908SNeilBrown 			   struct file_lock *sys_fl)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	if (sys_fl->fl_type == F_WRLCK)
846c0e15908SNeilBrown 		return true;
8471da177e4SLinus Torvalds 	if (caller_fl->fl_type == F_WRLCK)
848c0e15908SNeilBrown 		return true;
849c0e15908SNeilBrown 	return false;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
8531da177e4SLinus Torvalds  * checking before calling the locks_conflict().
8541da177e4SLinus Torvalds  */
posix_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)855c0e15908SNeilBrown static bool posix_locks_conflict(struct file_lock *caller_fl,
856c0e15908SNeilBrown 				 struct file_lock *sys_fl)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	/* POSIX locks owned by the same process do not conflict with
8591da177e4SLinus Torvalds 	 * each other.
8601da177e4SLinus Torvalds 	 */
8619b8c8695SJeff Layton 	if (posix_same_owner(caller_fl, sys_fl))
862c0e15908SNeilBrown 		return false;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	/* Check whether they overlap */
8651da177e4SLinus Torvalds 	if (!locks_overlap(caller_fl, sys_fl))
866c0e15908SNeilBrown 		return false;
8671da177e4SLinus Torvalds 
868c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
8716c9007f6SStas Sergeev /* Determine if lock sys_fl blocks lock caller_fl. Used on xx_GETLK
8726c9007f6SStas Sergeev  * path so checks for additional GETLK-specific things like F_UNLCK.
8736c9007f6SStas Sergeev  */
posix_test_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)8746c9007f6SStas Sergeev static bool posix_test_locks_conflict(struct file_lock *caller_fl,
8756c9007f6SStas Sergeev 				      struct file_lock *sys_fl)
8766c9007f6SStas Sergeev {
8776c9007f6SStas Sergeev 	/* F_UNLCK checks any locks on the same fd. */
8786c9007f6SStas Sergeev 	if (caller_fl->fl_type == F_UNLCK) {
8796c9007f6SStas Sergeev 		if (!posix_same_owner(caller_fl, sys_fl))
8806c9007f6SStas Sergeev 			return false;
8816c9007f6SStas Sergeev 		return locks_overlap(caller_fl, sys_fl);
8826c9007f6SStas Sergeev 	}
8836c9007f6SStas Sergeev 	return posix_locks_conflict(caller_fl, sys_fl);
8846c9007f6SStas Sergeev }
8856c9007f6SStas Sergeev 
8861da177e4SLinus Torvalds /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
8871da177e4SLinus Torvalds  * checking before calling the locks_conflict().
8881da177e4SLinus Torvalds  */
flock_locks_conflict(struct file_lock * caller_fl,struct file_lock * sys_fl)889c0e15908SNeilBrown static bool flock_locks_conflict(struct file_lock *caller_fl,
890c0e15908SNeilBrown 				 struct file_lock *sys_fl)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	/* FLOCK locks referring to the same filp do not conflict with
8931da177e4SLinus Torvalds 	 * each other.
8941da177e4SLinus Torvalds 	 */
8959b8c8695SJeff Layton 	if (caller_fl->fl_file == sys_fl->fl_file)
896c0e15908SNeilBrown 		return false;
8971da177e4SLinus Torvalds 
898c0e15908SNeilBrown 	return locks_conflict(caller_fl, sys_fl);
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9016d34ac19SJ. Bruce Fields void
posix_test_lock(struct file * filp,struct file_lock * fl)9029d6a8c5cSMarc Eshel posix_test_lock(struct file *filp, struct file_lock *fl)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct file_lock *cfl;
905bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
906c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
9072443da22SDai Ngo 	void *owner;
9082443da22SDai Ngo 	void (*func)(void);
9091da177e4SLinus Torvalds 
910401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
911bd61e0a9SJeff Layton 	if (!ctx || list_empty_careful(&ctx->flc_posix)) {
912bd61e0a9SJeff Layton 		fl->fl_type = F_UNLCK;
913bd61e0a9SJeff Layton 		return;
9141da177e4SLinus Torvalds 	}
915bd61e0a9SJeff Layton 
9162443da22SDai Ngo retry:
9176109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
918bd61e0a9SJeff Layton 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
9196c9007f6SStas Sergeev 		if (!posix_test_locks_conflict(fl, cfl))
9202443da22SDai Ngo 			continue;
9212443da22SDai Ngo 		if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable
9222443da22SDai Ngo 			&& (*cfl->fl_lmops->lm_lock_expirable)(cfl)) {
9232443da22SDai Ngo 			owner = cfl->fl_lmops->lm_mod_owner;
9242443da22SDai Ngo 			func = cfl->fl_lmops->lm_expire_lock;
9252443da22SDai Ngo 			__module_get(owner);
9262443da22SDai Ngo 			spin_unlock(&ctx->flc_lock);
9272443da22SDai Ngo 			(*func)();
9282443da22SDai Ngo 			module_put(owner);
9292443da22SDai Ngo 			goto retry;
9302443da22SDai Ngo 		}
9313fe0fff1SKinglong Mee 		locks_copy_conflock(fl, cfl);
932bd61e0a9SJeff Layton 		goto out;
933bd61e0a9SJeff Layton 	}
934129a84deSJ. Bruce Fields 	fl->fl_type = F_UNLCK;
935bd61e0a9SJeff Layton out:
9366109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
9376d34ac19SJ. Bruce Fields 	return;
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds EXPORT_SYMBOL(posix_test_lock);
9401da177e4SLinus Torvalds 
941b533184fSJ. Bruce Fields /*
942b533184fSJ. Bruce Fields  * Deadlock detection:
9431da177e4SLinus Torvalds  *
944b533184fSJ. Bruce Fields  * We attempt to detect deadlocks that are due purely to posix file
945b533184fSJ. Bruce Fields  * locks.
9461da177e4SLinus Torvalds  *
947b533184fSJ. Bruce Fields  * We assume that a task can be waiting for at most one lock at a time.
948b533184fSJ. Bruce Fields  * So for any acquired lock, the process holding that lock may be
949b533184fSJ. Bruce Fields  * waiting on at most one other lock.  That lock in turns may be held by
950b533184fSJ. Bruce Fields  * someone waiting for at most one other lock.  Given a requested lock
951b533184fSJ. Bruce Fields  * caller_fl which is about to wait for a conflicting lock block_fl, we
952b533184fSJ. Bruce Fields  * follow this chain of waiters to ensure we are not about to create a
953b533184fSJ. Bruce Fields  * cycle.
95497855b49SJ. Bruce Fields  *
955b533184fSJ. Bruce Fields  * Since we do this before we ever put a process to sleep on a lock, we
956b533184fSJ. Bruce Fields  * are ensured that there is never a cycle; that is what guarantees that
957b533184fSJ. Bruce Fields  * the while() loop in posix_locks_deadlock() eventually completes.
958b533184fSJ. Bruce Fields  *
959b533184fSJ. Bruce Fields  * Note: the above assumption may not be true when handling lock
960b533184fSJ. Bruce Fields  * requests from a broken NFS client. It may also fail in the presence
961b533184fSJ. Bruce Fields  * of tasks (such as posix threads) sharing the same open file table.
962b533184fSJ. Bruce Fields  * To handle those cases, we just bail out after a few iterations.
96357b65325SJeff Layton  *
964cff2fce5SJeff Layton  * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
96557b65325SJeff Layton  * Because the owner is not even nominally tied to a thread of
96657b65325SJeff Layton  * execution, the deadlock detection below can't reasonably work well. Just
96757b65325SJeff Layton  * skip it for those.
96857b65325SJeff Layton  *
969cff2fce5SJeff Layton  * In principle, we could do a more limited deadlock detection on FL_OFDLCK
97057b65325SJeff Layton  * locks that just checks for the case where two tasks are attempting to
97157b65325SJeff Layton  * upgrade from read to write locks on the same inode.
9721da177e4SLinus Torvalds  */
97397855b49SJ. Bruce Fields 
97497855b49SJ. Bruce Fields #define MAX_DEADLK_ITERATIONS 10
97597855b49SJ. Bruce Fields 
976b533184fSJ. Bruce Fields /* Find a lock that the owner of the given block_fl is blocking on. */
what_owner_is_waiting_for(struct file_lock * block_fl)977b533184fSJ. Bruce Fields static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
978b533184fSJ. Bruce Fields {
979b533184fSJ. Bruce Fields 	struct file_lock *fl;
980b533184fSJ. Bruce Fields 
9813999e493SJeff Layton 	hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
9825946c431SNeilBrown 		if (posix_same_owner(fl, block_fl)) {
9835946c431SNeilBrown 			while (fl->fl_blocker)
9845946c431SNeilBrown 				fl = fl->fl_blocker;
9855946c431SNeilBrown 			return fl;
9865946c431SNeilBrown 		}
987b533184fSJ. Bruce Fields 	}
988b533184fSJ. Bruce Fields 	return NULL;
989b533184fSJ. Bruce Fields }
990b533184fSJ. Bruce Fields 
9917b2296afSJeff Layton /* Must be called with the blocked_lock_lock held! */
posix_locks_deadlock(struct file_lock * caller_fl,struct file_lock * block_fl)992b0904e14SAdrian Bunk static int posix_locks_deadlock(struct file_lock *caller_fl,
9931da177e4SLinus Torvalds 				struct file_lock *block_fl)
9941da177e4SLinus Torvalds {
99597855b49SJ. Bruce Fields 	int i = 0;
9961da177e4SLinus Torvalds 
997663d5af7SDaniel Wagner 	lockdep_assert_held(&blocked_lock_lock);
998663d5af7SDaniel Wagner 
99957b65325SJeff Layton 	/*
100057b65325SJeff Layton 	 * This deadlock detector can't reasonably detect deadlocks with
1001cff2fce5SJeff Layton 	 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
100257b65325SJeff Layton 	 */
1003cff2fce5SJeff Layton 	if (IS_OFDLCK(caller_fl))
100457b65325SJeff Layton 		return 0;
100557b65325SJeff Layton 
1006b533184fSJ. Bruce Fields 	while ((block_fl = what_owner_is_waiting_for(block_fl))) {
100797855b49SJ. Bruce Fields 		if (i++ > MAX_DEADLK_ITERATIONS)
100897855b49SJ. Bruce Fields 			return 0;
1009b533184fSJ. Bruce Fields 		if (posix_same_owner(caller_fl, block_fl))
1010b533184fSJ. Bruce Fields 			return 1;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 	return 0;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
101602888f41SJ. Bruce Fields  * after any leases, but before any posix locks.
1017f475ae95STrond Myklebust  *
1018f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1019f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1020f475ae95STrond Myklebust  * value for -ENOENT.
10211da177e4SLinus Torvalds  */
flock_lock_inode(struct inode * inode,struct file_lock * request)1022bcd7f78dSJeff Layton static int flock_lock_inode(struct inode *inode, struct file_lock *request)
10231da177e4SLinus Torvalds {
1024993dfa87STrond Myklebust 	struct file_lock *new_fl = NULL;
10255263e31eSJeff Layton 	struct file_lock *fl;
10265263e31eSJeff Layton 	struct file_lock_context *ctx;
10271da177e4SLinus Torvalds 	int error = 0;
10285263e31eSJeff Layton 	bool found = false;
1029ed9814d8SJeff Layton 	LIST_HEAD(dispose);
10301da177e4SLinus Torvalds 
10315c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
10325c1c669aSJeff Layton 	if (!ctx) {
10335c1c669aSJeff Layton 		if (request->fl_type != F_UNLCK)
10345263e31eSJeff Layton 			return -ENOMEM;
10355c1c669aSJeff Layton 		return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
10365c1c669aSJeff Layton 	}
10375263e31eSJeff Layton 
1038b89f4321SArnd Bergmann 	if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
1039b89f4321SArnd Bergmann 		new_fl = locks_alloc_lock();
1040b89f4321SArnd Bergmann 		if (!new_fl)
1041b89f4321SArnd Bergmann 			return -ENOMEM;
1042b89f4321SArnd Bergmann 	}
1043b89f4321SArnd Bergmann 
104402e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
10456109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1046f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1047f07f18ddSTrond Myklebust 		goto find_conflict;
104884d535adSPavel Emelyanov 
10495263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1050bcd7f78dSJeff Layton 		if (request->fl_file != fl->fl_file)
10511da177e4SLinus Torvalds 			continue;
1052993dfa87STrond Myklebust 		if (request->fl_type == fl->fl_type)
10531da177e4SLinus Torvalds 			goto out;
10545263e31eSJeff Layton 		found = true;
1055e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, &dispose);
10561da177e4SLinus Torvalds 		break;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 
1059f475ae95STrond Myklebust 	if (request->fl_type == F_UNLCK) {
1060f475ae95STrond Myklebust 		if ((request->fl_flags & FL_EXISTS) && !found)
1061f475ae95STrond Myklebust 			error = -ENOENT;
1062993dfa87STrond Myklebust 		goto out;
1063f475ae95STrond Myklebust 	}
10641da177e4SLinus Torvalds 
1065f07f18ddSTrond Myklebust find_conflict:
10665263e31eSJeff Layton 	list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
1067993dfa87STrond Myklebust 		if (!flock_locks_conflict(request, fl))
10681da177e4SLinus Torvalds 			continue;
10691da177e4SLinus Torvalds 		error = -EAGAIN;
1070bde74e4bSMiklos Szeredi 		if (!(request->fl_flags & FL_SLEEP))
1071bde74e4bSMiklos Szeredi 			goto out;
1072bde74e4bSMiklos Szeredi 		error = FILE_LOCK_DEFERRED;
1073fd7732e0SNeilBrown 		locks_insert_block(fl, request, flock_locks_conflict);
10741da177e4SLinus Torvalds 		goto out;
10751da177e4SLinus Torvalds 	}
1076f07f18ddSTrond Myklebust 	if (request->fl_flags & FL_ACCESS)
1077f07f18ddSTrond Myklebust 		goto out;
1078993dfa87STrond Myklebust 	locks_copy_lock(new_fl, request);
10795946c431SNeilBrown 	locks_move_blocks(new_fl, request);
1080e084c1bdSJeff Layton 	locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
1081993dfa87STrond Myklebust 	new_fl = NULL;
10829cedc194SKirill Korotaev 	error = 0;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds out:
10856109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
108602e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1087993dfa87STrond Myklebust 	if (new_fl)
1088993dfa87STrond Myklebust 		locks_free_lock(new_fl);
1089ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
1090c883da31SJeff Layton 	trace_flock_lock_inode(inode, request, error);
10911da177e4SLinus Torvalds 	return error;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
posix_lock_inode(struct inode * inode,struct file_lock * request,struct file_lock * conflock)1094b4d629a3SJeff Layton static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1095b4d629a3SJeff Layton 			    struct file_lock *conflock)
10961da177e4SLinus Torvalds {
1097bd61e0a9SJeff Layton 	struct file_lock *fl, *tmp;
109839005d02SMiklos Szeredi 	struct file_lock *new_fl = NULL;
109939005d02SMiklos Szeredi 	struct file_lock *new_fl2 = NULL;
11001da177e4SLinus Torvalds 	struct file_lock *left = NULL;
11011da177e4SLinus Torvalds 	struct file_lock *right = NULL;
1102bd61e0a9SJeff Layton 	struct file_lock_context *ctx;
1103b9746ef8SJeff Layton 	int error;
1104b9746ef8SJeff Layton 	bool added = false;
1105ed9814d8SJeff Layton 	LIST_HEAD(dispose);
11062443da22SDai Ngo 	void *owner;
11072443da22SDai Ngo 	void (*func)(void);
11081da177e4SLinus Torvalds 
11095c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, request->fl_type);
1110bd61e0a9SJeff Layton 	if (!ctx)
11115c1c669aSJeff Layton 		return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
1112bd61e0a9SJeff Layton 
11131da177e4SLinus Torvalds 	/*
11141da177e4SLinus Torvalds 	 * We may need two file_lock structures for this operation,
11151da177e4SLinus Torvalds 	 * so we get them in advance to avoid races.
111639005d02SMiklos Szeredi 	 *
111739005d02SMiklos Szeredi 	 * In some cases we can be sure, that no new locks will be needed
11181da177e4SLinus Torvalds 	 */
111939005d02SMiklos Szeredi 	if (!(request->fl_flags & FL_ACCESS) &&
112039005d02SMiklos Szeredi 	    (request->fl_type != F_UNLCK ||
112139005d02SMiklos Szeredi 	     request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
11221da177e4SLinus Torvalds 		new_fl = locks_alloc_lock();
11231da177e4SLinus Torvalds 		new_fl2 = locks_alloc_lock();
112439005d02SMiklos Szeredi 	}
11251da177e4SLinus Torvalds 
11262443da22SDai Ngo retry:
112702e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
11286109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
11291cb36012SJeff Layton 	/*
11301cb36012SJeff Layton 	 * New lock request. Walk all POSIX locks and look for conflicts. If
11311cb36012SJeff Layton 	 * there are any, either return error or put the request on the
113248f74186SJeff Layton 	 * blocker's list of waiters and the global blocked_hash.
11331cb36012SJeff Layton 	 */
11341da177e4SLinus Torvalds 	if (request->fl_type != F_UNLCK) {
1135bd61e0a9SJeff Layton 		list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
11361da177e4SLinus Torvalds 			if (!posix_locks_conflict(request, fl))
11371da177e4SLinus Torvalds 				continue;
11382443da22SDai Ngo 			if (fl->fl_lmops && fl->fl_lmops->lm_lock_expirable
11392443da22SDai Ngo 				&& (*fl->fl_lmops->lm_lock_expirable)(fl)) {
11402443da22SDai Ngo 				owner = fl->fl_lmops->lm_mod_owner;
11412443da22SDai Ngo 				func = fl->fl_lmops->lm_expire_lock;
11422443da22SDai Ngo 				__module_get(owner);
11432443da22SDai Ngo 				spin_unlock(&ctx->flc_lock);
11442443da22SDai Ngo 				percpu_up_read(&file_rwsem);
11452443da22SDai Ngo 				(*func)();
11462443da22SDai Ngo 				module_put(owner);
11472443da22SDai Ngo 				goto retry;
11482443da22SDai Ngo 			}
11495842add2SAndy Adamson 			if (conflock)
11503fe0fff1SKinglong Mee 				locks_copy_conflock(conflock, fl);
11511da177e4SLinus Torvalds 			error = -EAGAIN;
11521da177e4SLinus Torvalds 			if (!(request->fl_flags & FL_SLEEP))
11531da177e4SLinus Torvalds 				goto out;
11541c8c601aSJeff Layton 			/*
11551c8c601aSJeff Layton 			 * Deadlock detection and insertion into the blocked
11561c8c601aSJeff Layton 			 * locks list must be done while holding the same lock!
11571c8c601aSJeff Layton 			 */
11581da177e4SLinus Torvalds 			error = -EDEADLK;
11597b2296afSJeff Layton 			spin_lock(&blocked_lock_lock);
1160945ab8f6SJeff Layton 			/*
1161945ab8f6SJeff Layton 			 * Ensure that we don't find any locks blocked on this
1162945ab8f6SJeff Layton 			 * request during deadlock detection.
1163945ab8f6SJeff Layton 			 */
1164945ab8f6SJeff Layton 			__locks_wake_up_blocks(request);
11651c8c601aSJeff Layton 			if (likely(!posix_locks_deadlock(request, fl))) {
1166bde74e4bSMiklos Szeredi 				error = FILE_LOCK_DEFERRED;
1167fd7732e0SNeilBrown 				__locks_insert_block(fl, request,
1168fd7732e0SNeilBrown 						     posix_locks_conflict);
11691c8c601aSJeff Layton 			}
11707b2296afSJeff Layton 			spin_unlock(&blocked_lock_lock);
11711da177e4SLinus Torvalds 			goto out;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/* If we're just looking for a conflict, we're done. */
11761da177e4SLinus Torvalds 	error = 0;
11771da177e4SLinus Torvalds 	if (request->fl_flags & FL_ACCESS)
11781da177e4SLinus Torvalds 		goto out;
11791da177e4SLinus Torvalds 
1180bd61e0a9SJeff Layton 	/* Find the first old lock with the same owner as the new lock */
1181bd61e0a9SJeff Layton 	list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1182bd61e0a9SJeff Layton 		if (posix_same_owner(request, fl))
1183bd61e0a9SJeff Layton 			break;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/* Process locks with this owner. */
1187bd61e0a9SJeff Layton 	list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1188bd61e0a9SJeff Layton 		if (!posix_same_owner(request, fl))
1189bd61e0a9SJeff Layton 			break;
1190bd61e0a9SJeff Layton 
1191bd61e0a9SJeff Layton 		/* Detect adjacent or overlapping regions (if same lock type) */
11921da177e4SLinus Torvalds 		if (request->fl_type == fl->fl_type) {
1193449231d6SOlaf Kirch 			/* In all comparisons of start vs end, use
1194449231d6SOlaf Kirch 			 * "start - 1" rather than "end + 1". If end
1195449231d6SOlaf Kirch 			 * is OFFSET_MAX, end + 1 will become negative.
1196449231d6SOlaf Kirch 			 */
11971da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start - 1)
1198bd61e0a9SJeff Layton 				continue;
11991da177e4SLinus Torvalds 			/* If the next lock in the list has entirely bigger
12001da177e4SLinus Torvalds 			 * addresses than the new one, insert the lock here.
12011da177e4SLinus Torvalds 			 */
1202449231d6SOlaf Kirch 			if (fl->fl_start - 1 > request->fl_end)
12031da177e4SLinus Torvalds 				break;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 			/* If we come here, the new and old lock are of the
12061da177e4SLinus Torvalds 			 * same type and adjacent or overlapping. Make one
12071da177e4SLinus Torvalds 			 * lock yielding from the lower start address of both
12081da177e4SLinus Torvalds 			 * locks to the higher end address.
12091da177e4SLinus Torvalds 			 */
12101da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_start)
12111da177e4SLinus Torvalds 				fl->fl_start = request->fl_start;
12121da177e4SLinus Torvalds 			else
12131da177e4SLinus Torvalds 				request->fl_start = fl->fl_start;
12141da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_end)
12151da177e4SLinus Torvalds 				fl->fl_end = request->fl_end;
12161da177e4SLinus Torvalds 			else
12171da177e4SLinus Torvalds 				request->fl_end = fl->fl_end;
12181da177e4SLinus Torvalds 			if (added) {
1219e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
12201da177e4SLinus Torvalds 				continue;
12211da177e4SLinus Torvalds 			}
12221da177e4SLinus Torvalds 			request = fl;
1223b9746ef8SJeff Layton 			added = true;
1224bd61e0a9SJeff Layton 		} else {
12251da177e4SLinus Torvalds 			/* Processing for different lock types is a bit
12261da177e4SLinus Torvalds 			 * more complex.
12271da177e4SLinus Torvalds 			 */
12281da177e4SLinus Torvalds 			if (fl->fl_end < request->fl_start)
1229bd61e0a9SJeff Layton 				continue;
12301da177e4SLinus Torvalds 			if (fl->fl_start > request->fl_end)
12311da177e4SLinus Torvalds 				break;
12321da177e4SLinus Torvalds 			if (request->fl_type == F_UNLCK)
1233b9746ef8SJeff Layton 				added = true;
12341da177e4SLinus Torvalds 			if (fl->fl_start < request->fl_start)
12351da177e4SLinus Torvalds 				left = fl;
12361da177e4SLinus Torvalds 			/* If the next lock in the list has a higher end
12371da177e4SLinus Torvalds 			 * address than the new one, insert the new one here.
12381da177e4SLinus Torvalds 			 */
12391da177e4SLinus Torvalds 			if (fl->fl_end > request->fl_end) {
12401da177e4SLinus Torvalds 				right = fl;
12411da177e4SLinus Torvalds 				break;
12421da177e4SLinus Torvalds 			}
12431da177e4SLinus Torvalds 			if (fl->fl_start >= request->fl_start) {
12441da177e4SLinus Torvalds 				/* The new lock completely replaces an old
12451da177e4SLinus Torvalds 				 * one (This may happen several times).
12461da177e4SLinus Torvalds 				 */
12471da177e4SLinus Torvalds 				if (added) {
1248e084c1bdSJeff Layton 					locks_delete_lock_ctx(fl, &dispose);
12491da177e4SLinus Torvalds 					continue;
12501da177e4SLinus Torvalds 				}
1251b84d49f9SJeff Layton 				/*
1252b84d49f9SJeff Layton 				 * Replace the old lock with new_fl, and
1253b84d49f9SJeff Layton 				 * remove the old one. It's safe to do the
1254b84d49f9SJeff Layton 				 * insert here since we know that we won't be
1255b84d49f9SJeff Layton 				 * using new_fl later, and that the lock is
1256b84d49f9SJeff Layton 				 * just replacing an existing lock.
12571da177e4SLinus Torvalds 				 */
1258b84d49f9SJeff Layton 				error = -ENOLCK;
1259b84d49f9SJeff Layton 				if (!new_fl)
1260b84d49f9SJeff Layton 					goto out;
1261b84d49f9SJeff Layton 				locks_copy_lock(new_fl, request);
12625ef15968Syangerkun 				locks_move_blocks(new_fl, request);
1263b84d49f9SJeff Layton 				request = new_fl;
1264b84d49f9SJeff Layton 				new_fl = NULL;
1265e084c1bdSJeff Layton 				locks_insert_lock_ctx(request, &fl->fl_list);
1266e084c1bdSJeff Layton 				locks_delete_lock_ctx(fl, &dispose);
1267b9746ef8SJeff Layton 				added = true;
12681da177e4SLinus Torvalds 			}
12691da177e4SLinus Torvalds 		}
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 
12720d9a490aSMiklos Szeredi 	/*
12731cb36012SJeff Layton 	 * The above code only modifies existing locks in case of merging or
12741cb36012SJeff Layton 	 * replacing. If new lock(s) need to be inserted all modifications are
12751cb36012SJeff Layton 	 * done below this, so it's safe yet to bail out.
12760d9a490aSMiklos Szeredi 	 */
12770d9a490aSMiklos Szeredi 	error = -ENOLCK; /* "no luck" */
12780d9a490aSMiklos Szeredi 	if (right && left == right && !new_fl2)
12790d9a490aSMiklos Szeredi 		goto out;
12800d9a490aSMiklos Szeredi 
12811da177e4SLinus Torvalds 	error = 0;
12821da177e4SLinus Torvalds 	if (!added) {
1283f475ae95STrond Myklebust 		if (request->fl_type == F_UNLCK) {
1284f475ae95STrond Myklebust 			if (request->fl_flags & FL_EXISTS)
1285f475ae95STrond Myklebust 				error = -ENOENT;
12861da177e4SLinus Torvalds 			goto out;
1287f475ae95STrond Myklebust 		}
12880d9a490aSMiklos Szeredi 
12890d9a490aSMiklos Szeredi 		if (!new_fl) {
12900d9a490aSMiklos Szeredi 			error = -ENOLCK;
12910d9a490aSMiklos Szeredi 			goto out;
12920d9a490aSMiklos Szeredi 		}
12931da177e4SLinus Torvalds 		locks_copy_lock(new_fl, request);
12945946c431SNeilBrown 		locks_move_blocks(new_fl, request);
1295e084c1bdSJeff Layton 		locks_insert_lock_ctx(new_fl, &fl->fl_list);
12962e2f756fSJeff Layton 		fl = new_fl;
12971da177e4SLinus Torvalds 		new_fl = NULL;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	if (right) {
13001da177e4SLinus Torvalds 		if (left == right) {
13011da177e4SLinus Torvalds 			/* The new lock breaks the old one in two pieces,
13021da177e4SLinus Torvalds 			 * so we have to use the second new lock.
13031da177e4SLinus Torvalds 			 */
13041da177e4SLinus Torvalds 			left = new_fl2;
13051da177e4SLinus Torvalds 			new_fl2 = NULL;
13061da177e4SLinus Torvalds 			locks_copy_lock(left, right);
1307e084c1bdSJeff Layton 			locks_insert_lock_ctx(left, &fl->fl_list);
13081da177e4SLinus Torvalds 		}
13091da177e4SLinus Torvalds 		right->fl_start = request->fl_end + 1;
13101da177e4SLinus Torvalds 		locks_wake_up_blocks(right);
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 	if (left) {
13131da177e4SLinus Torvalds 		left->fl_end = request->fl_start - 1;
13141da177e4SLinus Torvalds 		locks_wake_up_blocks(left);
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds  out:
1317432b06b6SJeff Layton 	trace_posix_lock_inode(inode, request, error);
13186109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
131902e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
13201da177e4SLinus Torvalds 	/*
13211da177e4SLinus Torvalds 	 * Free any unused locks.
13221da177e4SLinus Torvalds 	 */
13231da177e4SLinus Torvalds 	if (new_fl)
13241da177e4SLinus Torvalds 		locks_free_lock(new_fl);
13251da177e4SLinus Torvalds 	if (new_fl2)
13261da177e4SLinus Torvalds 		locks_free_lock(new_fl2);
1327ed9814d8SJeff Layton 	locks_dispose_list(&dispose);
13281890910fSJeff Layton 
13291da177e4SLinus Torvalds 	return error;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /**
13331da177e4SLinus Torvalds  * posix_lock_file - Apply a POSIX-style lock to a file
13341da177e4SLinus Torvalds  * @filp: The file to apply the lock to
13351da177e4SLinus Torvalds  * @fl: The lock to be applied
1336150b3934SMarc Eshel  * @conflock: Place to return a copy of the conflicting lock, if found.
13371da177e4SLinus Torvalds  *
13381da177e4SLinus Torvalds  * Add a POSIX style lock to a file.
13391da177e4SLinus Torvalds  * We merge adjacent & overlapping locks whenever possible.
13401da177e4SLinus Torvalds  * POSIX locks are sorted by owner task, then by starting address
1341f475ae95STrond Myklebust  *
1342f475ae95STrond Myklebust  * Note that if called with an FL_EXISTS argument, the caller may determine
1343f475ae95STrond Myklebust  * whether or not a lock was successfully freed by testing the return
1344f475ae95STrond Myklebust  * value for -ENOENT.
13451da177e4SLinus Torvalds  */
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)1346150b3934SMarc Eshel int posix_lock_file(struct file *filp, struct file_lock *fl,
13475842add2SAndy Adamson 			struct file_lock *conflock)
13485842add2SAndy Adamson {
1349c65454a9SJeff Layton 	return posix_lock_inode(file_inode(filp), fl, conflock);
13505842add2SAndy Adamson }
1351150b3934SMarc Eshel EXPORT_SYMBOL(posix_lock_file);
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds /**
135429d01b22SJeff Layton  * posix_lock_inode_wait - Apply a POSIX-style lock to a file
135529d01b22SJeff Layton  * @inode: inode of file to which lock request should be applied
13561da177e4SLinus Torvalds  * @fl: The lock to be applied
13571da177e4SLinus Torvalds  *
1358616fb38fSBenjamin Coddington  * Apply a POSIX style lock request to an inode.
13591da177e4SLinus Torvalds  */
posix_lock_inode_wait(struct inode * inode,struct file_lock * fl)1360616fb38fSBenjamin Coddington static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	int error;
13631da177e4SLinus Torvalds 	might_sleep ();
13641da177e4SLinus Torvalds 	for (;;) {
1365b4d629a3SJeff Layton 		error = posix_lock_inode(inode, fl, NULL);
1366bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
13671da177e4SLinus Torvalds 			break;
1368dcf23ac3SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait,
1369dcf23ac3SLinus Torvalds 					list_empty(&fl->fl_blocked_member));
137016306a61SNeilBrown 		if (error)
13711da177e4SLinus Torvalds 			break;
13721da177e4SLinus Torvalds 	}
137316306a61SNeilBrown 	locks_delete_block(fl);
13741da177e4SLinus Torvalds 	return error;
13751da177e4SLinus Torvalds }
137629d01b22SJeff Layton 
lease_clear_pending(struct file_lock * fl,int arg)1377778fc546SJ. Bruce Fields static void lease_clear_pending(struct file_lock *fl, int arg)
1378778fc546SJ. Bruce Fields {
1379778fc546SJ. Bruce Fields 	switch (arg) {
1380778fc546SJ. Bruce Fields 	case F_UNLCK:
1381778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_UNLOCK_PENDING;
1382df561f66SGustavo A. R. Silva 		fallthrough;
1383778fc546SJ. Bruce Fields 	case F_RDLCK:
1384778fc546SJ. Bruce Fields 		fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1385778fc546SJ. Bruce Fields 	}
1386778fc546SJ. Bruce Fields }
1387778fc546SJ. Bruce Fields 
13881da177e4SLinus Torvalds /* We already had a lease on this file; just change its type */
lease_modify(struct file_lock * fl,int arg,struct list_head * dispose)13897448cc37SJeff Layton int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
13901da177e4SLinus Torvalds {
13911da177e4SLinus Torvalds 	int error = assign_type(fl, arg);
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	if (error)
13941da177e4SLinus Torvalds 		return error;
1395778fc546SJ. Bruce Fields 	lease_clear_pending(fl, arg);
13961da177e4SLinus Torvalds 	locks_wake_up_blocks(fl);
13973b6e2723SFilipe Brandenburger 	if (arg == F_UNLCK) {
13983b6e2723SFilipe Brandenburger 		struct file *filp = fl->fl_file;
13993b6e2723SFilipe Brandenburger 
14003b6e2723SFilipe Brandenburger 		f_delown(filp);
14013b6e2723SFilipe Brandenburger 		filp->f_owner.signum = 0;
140296d6d59cSJ. Bruce Fields 		fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
140396d6d59cSJ. Bruce Fields 		if (fl->fl_fasync != NULL) {
140496d6d59cSJ. Bruce Fields 			printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
140596d6d59cSJ. Bruce Fields 			fl->fl_fasync = NULL;
140696d6d59cSJ. Bruce Fields 		}
1407e084c1bdSJeff Layton 		locks_delete_lock_ctx(fl, dispose);
14083b6e2723SFilipe Brandenburger 	}
14091da177e4SLinus Torvalds 	return 0;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds EXPORT_SYMBOL(lease_modify);
14121da177e4SLinus Torvalds 
past_time(unsigned long then)1413778fc546SJ. Bruce Fields static bool past_time(unsigned long then)
1414778fc546SJ. Bruce Fields {
1415778fc546SJ. Bruce Fields 	if (!then)
1416778fc546SJ. Bruce Fields 		/* 0 is a special value meaning "this never expires": */
1417778fc546SJ. Bruce Fields 		return false;
1418778fc546SJ. Bruce Fields 	return time_after(jiffies, then);
1419778fc546SJ. Bruce Fields }
1420778fc546SJ. Bruce Fields 
time_out_leases(struct inode * inode,struct list_head * dispose)1421c45198edSJeff Layton static void time_out_leases(struct inode *inode, struct list_head *dispose)
14221da177e4SLinus Torvalds {
14238634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
14248634b51fSJeff Layton 	struct file_lock *fl, *tmp;
14251da177e4SLinus Torvalds 
14266109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
1427f82b4b67SJeff Layton 
14288634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
142962af4f1fSJeff Layton 		trace_time_out_leases(inode, fl);
1430778fc546SJ. Bruce Fields 		if (past_time(fl->fl_downgrade_time))
14317448cc37SJeff Layton 			lease_modify(fl, F_RDLCK, dispose);
1432778fc546SJ. Bruce Fields 		if (past_time(fl->fl_break_time))
14337448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, dispose);
14341da177e4SLinus Torvalds 	}
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds 
leases_conflict(struct file_lock * lease,struct file_lock * breaker)1437df4e8d2cSJ. Bruce Fields static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1438df4e8d2cSJ. Bruce Fields {
1439d51f527fSIra Weiny 	bool rc;
1440d51f527fSIra Weiny 
144128df3d15SJ. Bruce Fields 	if (lease->fl_lmops->lm_breaker_owns_lease
144228df3d15SJ. Bruce Fields 			&& lease->fl_lmops->lm_breaker_owns_lease(lease))
144328df3d15SJ. Bruce Fields 		return false;
1444d51f527fSIra Weiny 	if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) {
1445d51f527fSIra Weiny 		rc = false;
1446d51f527fSIra Weiny 		goto trace;
1447d51f527fSIra Weiny 	}
1448d51f527fSIra Weiny 	if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) {
1449d51f527fSIra Weiny 		rc = false;
1450d51f527fSIra Weiny 		goto trace;
1451d51f527fSIra Weiny 	}
1452d51f527fSIra Weiny 
1453d51f527fSIra Weiny 	rc = locks_conflict(breaker, lease);
1454d51f527fSIra Weiny trace:
1455d51f527fSIra Weiny 	trace_leases_conflict(rc, lease, breaker);
1456d51f527fSIra Weiny 	return rc;
1457df4e8d2cSJ. Bruce Fields }
1458df4e8d2cSJ. Bruce Fields 
145903d12ddfSJeff Layton static bool
any_leases_conflict(struct inode * inode,struct file_lock * breaker)146003d12ddfSJeff Layton any_leases_conflict(struct inode *inode, struct file_lock *breaker)
146103d12ddfSJeff Layton {
14628634b51fSJeff Layton 	struct file_lock_context *ctx = inode->i_flctx;
146303d12ddfSJeff Layton 	struct file_lock *fl;
146403d12ddfSJeff Layton 
14656109c850SJeff Layton 	lockdep_assert_held(&ctx->flc_lock);
146603d12ddfSJeff Layton 
14678634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
146803d12ddfSJeff Layton 		if (leases_conflict(fl, breaker))
146903d12ddfSJeff Layton 			return true;
147003d12ddfSJeff Layton 	}
147103d12ddfSJeff Layton 	return false;
147203d12ddfSJeff Layton }
147303d12ddfSJeff Layton 
14741da177e4SLinus Torvalds /**
14751da177e4SLinus Torvalds  *	__break_lease	-	revoke all outstanding leases on file
14761da177e4SLinus Torvalds  *	@inode: the inode of the file to return
1477df4e8d2cSJ. Bruce Fields  *	@mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1478df4e8d2cSJ. Bruce Fields  *	    break all leases
1479df4e8d2cSJ. Bruce Fields  *	@type: FL_LEASE: break leases and delegations; FL_DELEG: break
1480df4e8d2cSJ. Bruce Fields  *	    only delegations
14811da177e4SLinus Torvalds  *
148287250dd2Sdavid m. richter  *	break_lease (inlined for speed) has checked there already is at least
148387250dd2Sdavid m. richter  *	some kind of lock (maybe a lease) on this file.  Leases are broken on
148487250dd2Sdavid m. richter  *	a call to open() or truncate().  This function can sleep unless you
14851da177e4SLinus Torvalds  *	specified %O_NONBLOCK to your open().
14861da177e4SLinus Torvalds  */
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)1487df4e8d2cSJ. Bruce Fields int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
14881da177e4SLinus Torvalds {
1489778fc546SJ. Bruce Fields 	int error = 0;
1490128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1491a901125cSYan, Zheng 	struct file_lock *new_fl, *fl, *tmp;
14921da177e4SLinus Torvalds 	unsigned long break_time;
14938737c930SAl Viro 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
1494c45198edSJeff Layton 	LIST_HEAD(dispose);
14951da177e4SLinus Torvalds 
14968737c930SAl Viro 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
14976d4b9e38SLinus Torvalds 	if (IS_ERR(new_fl))
14986d4b9e38SLinus Torvalds 		return PTR_ERR(new_fl);
1499df4e8d2cSJ. Bruce Fields 	new_fl->fl_flags = type;
15001da177e4SLinus Torvalds 
15018634b51fSJeff Layton 	/* typically we will check that ctx is non-NULL before calling */
1502401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
15038634b51fSJeff Layton 	if (!ctx) {
15048634b51fSJeff Layton 		WARN_ON_ONCE(1);
1505cfddf9f4SWenwen Wang 		goto free_lock;
15068634b51fSJeff Layton 	}
15078634b51fSJeff Layton 
150802e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
15096109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
15101da177e4SLinus Torvalds 
1511c45198edSJeff Layton 	time_out_leases(inode, &dispose);
15121da177e4SLinus Torvalds 
151303d12ddfSJeff Layton 	if (!any_leases_conflict(inode, new_fl))
1514df4e8d2cSJ. Bruce Fields 		goto out;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	break_time = 0;
15171da177e4SLinus Torvalds 	if (lease_break_time > 0) {
15181da177e4SLinus Torvalds 		break_time = jiffies + lease_break_time * HZ;
15191da177e4SLinus Torvalds 		if (break_time == 0)
15201da177e4SLinus Torvalds 			break_time++;	/* so that 0 means no break time */
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
1523a901125cSYan, Zheng 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
1524df4e8d2cSJ. Bruce Fields 		if (!leases_conflict(fl, new_fl))
1525df4e8d2cSJ. Bruce Fields 			continue;
1526778fc546SJ. Bruce Fields 		if (want_write) {
1527778fc546SJ. Bruce Fields 			if (fl->fl_flags & FL_UNLOCK_PENDING)
1528778fc546SJ. Bruce Fields 				continue;
1529778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_UNLOCK_PENDING;
15301da177e4SLinus Torvalds 			fl->fl_break_time = break_time;
1531778fc546SJ. Bruce Fields 		} else {
15328634b51fSJeff Layton 			if (lease_breaking(fl))
1533778fc546SJ. Bruce Fields 				continue;
1534778fc546SJ. Bruce Fields 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
1535778fc546SJ. Bruce Fields 			fl->fl_downgrade_time = break_time;
15361da177e4SLinus Torvalds 		}
15374d01b7f5SJeff Layton 		if (fl->fl_lmops->lm_break(fl))
1538e084c1bdSJeff Layton 			locks_delete_lock_ctx(fl, &dispose);
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 
15418634b51fSJeff Layton 	if (list_empty(&ctx->flc_lease))
15424d01b7f5SJeff Layton 		goto out;
15434d01b7f5SJeff Layton 
1544843c6b2fSJeff Layton 	if (mode & O_NONBLOCK) {
154562af4f1fSJeff Layton 		trace_break_lease_noblock(inode, new_fl);
15461da177e4SLinus Torvalds 		error = -EWOULDBLOCK;
15471da177e4SLinus Torvalds 		goto out;
15481da177e4SLinus Torvalds 	}
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds restart:
15518634b51fSJeff Layton 	fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
15528634b51fSJeff Layton 	break_time = fl->fl_break_time;
1553f1c6bb2cSJeff Layton 	if (break_time != 0)
15541da177e4SLinus Torvalds 		break_time -= jiffies;
15551da177e4SLinus Torvalds 	if (break_time == 0)
15561da177e4SLinus Torvalds 		break_time++;
1557fd7732e0SNeilBrown 	locks_insert_block(fl, new_fl, leases_conflict);
155862af4f1fSJeff Layton 	trace_break_lease_block(inode, new_fl);
15596109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
156002e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1561aba37660SPeter Zijlstra 
1562c45198edSJeff Layton 	locks_dispose_list(&dispose);
15634321e01eSMatthew Wilcox 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
1564dcf23ac3SLinus Torvalds 					list_empty(&new_fl->fl_blocked_member),
1565dcf23ac3SLinus Torvalds 					break_time);
1566aba37660SPeter Zijlstra 
156702e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
15686109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
156962af4f1fSJeff Layton 	trace_break_lease_unblock(inode, new_fl);
15701c8c601aSJeff Layton 	locks_delete_block(new_fl);
15711da177e4SLinus Torvalds 	if (error >= 0) {
1572778fc546SJ. Bruce Fields 		/*
1573778fc546SJ. Bruce Fields 		 * Wait for the next conflicting lease that has not been
1574778fc546SJ. Bruce Fields 		 * broken yet
1575778fc546SJ. Bruce Fields 		 */
157603d12ddfSJeff Layton 		if (error == 0)
157703d12ddfSJeff Layton 			time_out_leases(inode, &dispose);
157803d12ddfSJeff Layton 		if (any_leases_conflict(inode, new_fl))
15791da177e4SLinus Torvalds 			goto restart;
15801da177e4SLinus Torvalds 		error = 0;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds out:
15836109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
158402e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1585c45198edSJeff Layton 	locks_dispose_list(&dispose);
1586cfddf9f4SWenwen Wang free_lock:
15871da177e4SLinus Torvalds 	locks_free_lock(new_fl);
15881da177e4SLinus Torvalds 	return error;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds EXPORT_SYMBOL(__break_lease);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds /**
159376c47948SAmir Goldstein  *	lease_get_mtime - update modified time of an inode with exclusive lease
15941da177e4SLinus Torvalds  *	@inode: the inode
159576c47948SAmir Goldstein  *      @time:  pointer to a timespec which contains the last modified time
15961da177e4SLinus Torvalds  *
15971da177e4SLinus Torvalds  * This is to force NFS clients to flush their caches for files with
15981da177e4SLinus Torvalds  * exclusive leases.  The justification is that if someone has an
1599a6b91919SRandy Dunlap  * exclusive lease, then they could be modifying it.
16001da177e4SLinus Torvalds  */
lease_get_mtime(struct inode * inode,struct timespec64 * time)160195582b00SDeepa Dinamani void lease_get_mtime(struct inode *inode, struct timespec64 *time)
16021da177e4SLinus Torvalds {
1603bfe86024SJeff Layton 	bool has_lease = false;
1604128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
16058634b51fSJeff Layton 	struct file_lock *fl;
1606bfe86024SJeff Layton 
1607401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
16088634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
16096109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
16108ace5dfbSGeliang Tang 		fl = list_first_entry_or_null(&ctx->flc_lease,
16118634b51fSJeff Layton 					      struct file_lock, fl_list);
16128ace5dfbSGeliang Tang 		if (fl && (fl->fl_type == F_WRLCK))
1613bfe86024SJeff Layton 			has_lease = true;
16146109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
1615bfe86024SJeff Layton 	}
1616bfe86024SJeff Layton 
1617bfe86024SJeff Layton 	if (has_lease)
1618c2050a45SDeepa Dinamani 		*time = current_time(inode);
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds EXPORT_SYMBOL(lease_get_mtime);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds /**
16231da177e4SLinus Torvalds  *	fcntl_getlease - Enquire what lease is currently active
16241da177e4SLinus Torvalds  *	@filp: the file
16251da177e4SLinus Torvalds  *
16261da177e4SLinus Torvalds  *	The value returned by this function will be one of
16271da177e4SLinus Torvalds  *	(if no lease break is pending):
16281da177e4SLinus Torvalds  *
16291da177e4SLinus Torvalds  *	%F_RDLCK to indicate a shared lease is held.
16301da177e4SLinus Torvalds  *
16311da177e4SLinus Torvalds  *	%F_WRLCK to indicate an exclusive lease is held.
16321da177e4SLinus Torvalds  *
16331da177e4SLinus Torvalds  *	%F_UNLCK to indicate no lease is held.
16341da177e4SLinus Torvalds  *
16351da177e4SLinus Torvalds  *	(if a lease break is pending):
16361da177e4SLinus Torvalds  *
16371da177e4SLinus Torvalds  *	%F_RDLCK to indicate an exclusive lease needs to be
16381da177e4SLinus Torvalds  *		changed to a shared lease (or removed).
16391da177e4SLinus Torvalds  *
16401da177e4SLinus Torvalds  *	%F_UNLCK to indicate the lease needs to be removed.
16411da177e4SLinus Torvalds  *
16421da177e4SLinus Torvalds  *	XXX: sfr & willy disagree over whether F_INPROGRESS
16431da177e4SLinus Torvalds  *	should be returned to userspace.
16441da177e4SLinus Torvalds  */
fcntl_getlease(struct file * filp)16451da177e4SLinus Torvalds int fcntl_getlease(struct file *filp)
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds 	struct file_lock *fl;
1648c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
1649128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
16501da177e4SLinus Torvalds 	int type = F_UNLCK;
1651c45198edSJeff Layton 	LIST_HEAD(dispose);
16521da177e4SLinus Torvalds 
1653401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
16548634b51fSJeff Layton 	if (ctx && !list_empty_careful(&ctx->flc_lease)) {
165502e525b2SPeter Zijlstra 		percpu_down_read(&file_rwsem);
16566109c850SJeff Layton 		spin_lock(&ctx->flc_lock);
1657c568d683SMiklos Szeredi 		time_out_leases(inode, &dispose);
16588634b51fSJeff Layton 		list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
16598634b51fSJeff Layton 			if (fl->fl_file != filp)
16608634b51fSJeff Layton 				continue;
1661778fc546SJ. Bruce Fields 			type = target_leasetype(fl);
16621da177e4SLinus Torvalds 			break;
16631da177e4SLinus Torvalds 		}
16646109c850SJeff Layton 		spin_unlock(&ctx->flc_lock);
166502e525b2SPeter Zijlstra 		percpu_up_read(&file_rwsem);
16665f43086bSPeter Zijlstra 
1667c45198edSJeff Layton 		locks_dispose_list(&dispose);
16688634b51fSJeff Layton 	}
16691da177e4SLinus Torvalds 	return type;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
167224cbe784SJeff Layton /**
1673387e3746SAmir Goldstein  * check_conflicting_open - see if the given file points to an inode that has
167424cbe784SJeff Layton  *			    an existing open that would conflict with the
167524cbe784SJeff Layton  *			    desired lease.
1676387e3746SAmir Goldstein  * @filp:	file to check
167724cbe784SJeff Layton  * @arg:	type of lease that we're trying to acquire
16787fadc59cSRandy Dunlap  * @flags:	current lock flags
167924cbe784SJeff Layton  *
168024cbe784SJeff Layton  * Check to see if there's an existing open fd on this file that would
168124cbe784SJeff Layton  * conflict with the lease we're trying to set.
168224cbe784SJeff Layton  */
168324cbe784SJeff Layton static int
check_conflicting_open(struct file * filp,const int arg,int flags)1684ed5f17f6SLuca Vizzarro check_conflicting_open(struct file *filp, const int arg, int flags)
168524cbe784SJeff Layton {
1686c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
1687387e3746SAmir Goldstein 	int self_wcount = 0, self_rcount = 0;
168824cbe784SJeff Layton 
168911afe9f7SChristoph Hellwig 	if (flags & FL_LAYOUT)
169011afe9f7SChristoph Hellwig 		return 0;
1691aba2072fSJ. Bruce Fields 	if (flags & FL_DELEG)
1692aba2072fSJ. Bruce Fields 		/* We leave these checks to the caller */
1693aba2072fSJ. Bruce Fields 		return 0;
169411afe9f7SChristoph Hellwig 
1695387e3746SAmir Goldstein 	if (arg == F_RDLCK)
1696387e3746SAmir Goldstein 		return inode_is_open_for_write(inode) ? -EAGAIN : 0;
1697387e3746SAmir Goldstein 	else if (arg != F_WRLCK)
1698387e3746SAmir Goldstein 		return 0;
1699387e3746SAmir Goldstein 
1700387e3746SAmir Goldstein 	/*
1701387e3746SAmir Goldstein 	 * Make sure that only read/write count is from lease requestor.
1702387e3746SAmir Goldstein 	 * Note that this will result in denying write leases when i_writecount
1703387e3746SAmir Goldstein 	 * is negative, which is what we want.  (We shouldn't grant write leases
1704387e3746SAmir Goldstein 	 * on files open for execution.)
1705387e3746SAmir Goldstein 	 */
1706387e3746SAmir Goldstein 	if (filp->f_mode & FMODE_WRITE)
1707387e3746SAmir Goldstein 		self_wcount = 1;
1708387e3746SAmir Goldstein 	else if (filp->f_mode & FMODE_READ)
1709387e3746SAmir Goldstein 		self_rcount = 1;
1710387e3746SAmir Goldstein 
1711387e3746SAmir Goldstein 	if (atomic_read(&inode->i_writecount) != self_wcount ||
1712387e3746SAmir Goldstein 	    atomic_read(&inode->i_readcount) != self_rcount)
171324cbe784SJeff Layton 		return -EAGAIN;
171424cbe784SJeff Layton 
1715387e3746SAmir Goldstein 	return 0;
171624cbe784SJeff Layton }
171724cbe784SJeff Layton 
1718e6f5c789SJeff Layton static int
generic_add_lease(struct file * filp,int arg,struct file_lock ** flp,void ** priv)1719ed5f17f6SLuca Vizzarro generic_add_lease(struct file *filp, int arg, struct file_lock **flp, void **priv)
17201da177e4SLinus Torvalds {
17218634b51fSJeff Layton 	struct file_lock *fl, *my_fl = NULL, *lease;
1722c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
17238634b51fSJeff Layton 	struct file_lock_context *ctx;
1724df4e8d2cSJ. Bruce Fields 	bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1725c1f24ef4SJ. Bruce Fields 	int error;
1726c45198edSJeff Layton 	LIST_HEAD(dispose);
17271da177e4SLinus Torvalds 
1728096657b6SJ. Bruce Fields 	lease = *flp;
172962af4f1fSJeff Layton 	trace_generic_add_lease(inode, lease);
173062af4f1fSJeff Layton 
17315c1c669aSJeff Layton 	/* Note that arg is never F_UNLCK here */
17325c1c669aSJeff Layton 	ctx = locks_get_lock_context(inode, arg);
17338634b51fSJeff Layton 	if (!ctx)
17348634b51fSJeff Layton 		return -ENOMEM;
17358634b51fSJeff Layton 
1736df4e8d2cSJ. Bruce Fields 	/*
1737df4e8d2cSJ. Bruce Fields 	 * In the delegation case we need mutual exclusion with
1738df4e8d2cSJ. Bruce Fields 	 * a number of operations that take the i_mutex.  We trylock
1739df4e8d2cSJ. Bruce Fields 	 * because delegations are an optional optimization, and if
1740df4e8d2cSJ. Bruce Fields 	 * there's some chance of a conflict--we'd rather not
1741df4e8d2cSJ. Bruce Fields 	 * bother, maybe that's a sign this just isn't a good file to
1742df4e8d2cSJ. Bruce Fields 	 * hand out a delegation on.
1743df4e8d2cSJ. Bruce Fields 	 */
17445955102cSAl Viro 	if (is_deleg && !inode_trylock(inode))
1745df4e8d2cSJ. Bruce Fields 		return -EAGAIN;
1746df4e8d2cSJ. Bruce Fields 
174702e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
17486109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
1749c45198edSJeff Layton 	time_out_leases(inode, &dispose);
1750387e3746SAmir Goldstein 	error = check_conflicting_open(filp, arg, lease->fl_flags);
175124cbe784SJeff Layton 	if (error)
17521da177e4SLinus Torvalds 		goto out;
175385c59580SPavel Emelyanov 
17541da177e4SLinus Torvalds 	/*
17551da177e4SLinus Torvalds 	 * At this point, we know that if there is an exclusive
17561da177e4SLinus Torvalds 	 * lease on this file, then we hold it on this filp
17571da177e4SLinus Torvalds 	 * (otherwise our open of this file would have blocked).
17581da177e4SLinus Torvalds 	 * And if we are trying to acquire an exclusive lease,
17591da177e4SLinus Torvalds 	 * then the file is not open by anyone (including us)
17601da177e4SLinus Torvalds 	 * except for this filp.
17611da177e4SLinus Torvalds 	 */
1762c1f24ef4SJ. Bruce Fields 	error = -EAGAIN;
17638634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
17642ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
17652ab99ee1SChristoph Hellwig 		    fl->fl_owner == lease->fl_owner) {
17668634b51fSJeff Layton 			my_fl = fl;
1767c1f24ef4SJ. Bruce Fields 			continue;
17681da177e4SLinus Torvalds 		}
17698634b51fSJeff Layton 
1770c1f24ef4SJ. Bruce Fields 		/*
1771c1f24ef4SJ. Bruce Fields 		 * No exclusive leases if someone else has a lease on
1772c1f24ef4SJ. Bruce Fields 		 * this file:
1773c1f24ef4SJ. Bruce Fields 		 */
1774c1f24ef4SJ. Bruce Fields 		if (arg == F_WRLCK)
17751da177e4SLinus Torvalds 			goto out;
1776c1f24ef4SJ. Bruce Fields 		/*
1777c1f24ef4SJ. Bruce Fields 		 * Modifying our existing lease is OK, but no getting a
1778c1f24ef4SJ. Bruce Fields 		 * new lease if someone else is opening for write:
1779c1f24ef4SJ. Bruce Fields 		 */
1780c1f24ef4SJ. Bruce Fields 		if (fl->fl_flags & FL_UNLOCK_PENDING)
1781c1f24ef4SJ. Bruce Fields 			goto out;
1782c1f24ef4SJ. Bruce Fields 	}
17831da177e4SLinus Torvalds 
17848634b51fSJeff Layton 	if (my_fl != NULL) {
17850164bf02SJeff Layton 		lease = my_fl;
17860164bf02SJeff Layton 		error = lease->fl_lmops->lm_change(lease, arg, &dispose);
17871c7dd2ffSJeff Layton 		if (error)
17881da177e4SLinus Torvalds 			goto out;
17891c7dd2ffSJeff Layton 		goto out_setup;
17901da177e4SLinus Torvalds 	}
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	error = -EINVAL;
17931da177e4SLinus Torvalds 	if (!leases_enable)
17941da177e4SLinus Torvalds 		goto out;
17951da177e4SLinus Torvalds 
1796e084c1bdSJeff Layton 	locks_insert_lock_ctx(lease, &ctx->flc_lease);
179724cbe784SJeff Layton 	/*
179824cbe784SJeff Layton 	 * The check in break_lease() is lockless. It's possible for another
179924cbe784SJeff Layton 	 * open to race in after we did the earlier check for a conflicting
180024cbe784SJeff Layton 	 * open but before the lease was inserted. Check again for a
180124cbe784SJeff Layton 	 * conflicting open and cancel the lease if there is one.
180224cbe784SJeff Layton 	 *
180324cbe784SJeff Layton 	 * We also add a barrier here to ensure that the insertion of the lock
180424cbe784SJeff Layton 	 * precedes these checks.
180524cbe784SJeff Layton 	 */
180624cbe784SJeff Layton 	smp_mb();
1807387e3746SAmir Goldstein 	error = check_conflicting_open(filp, arg, lease->fl_flags);
18088634b51fSJeff Layton 	if (error) {
1809e084c1bdSJeff Layton 		locks_unlink_lock_ctx(lease);
18108634b51fSJeff Layton 		goto out;
18118634b51fSJeff Layton 	}
18121c7dd2ffSJeff Layton 
18131c7dd2ffSJeff Layton out_setup:
18141c7dd2ffSJeff Layton 	if (lease->fl_lmops->lm_setup)
18151c7dd2ffSJeff Layton 		lease->fl_lmops->lm_setup(lease, priv);
18161da177e4SLinus Torvalds out:
18176109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
181802e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1819c45198edSJeff Layton 	locks_dispose_list(&dispose);
1820df4e8d2cSJ. Bruce Fields 	if (is_deleg)
18215955102cSAl Viro 		inode_unlock(inode);
18228634b51fSJeff Layton 	if (!error && !my_fl)
18231c7dd2ffSJeff Layton 		*flp = NULL;
18241da177e4SLinus Torvalds 	return error;
18251da177e4SLinus Torvalds }
18268335ebd9SJ. Bruce Fields 
generic_delete_lease(struct file * filp,void * owner)18272ab99ee1SChristoph Hellwig static int generic_delete_lease(struct file *filp, void *owner)
18288335ebd9SJ. Bruce Fields {
18290efaa7e8SJeff Layton 	int error = -EAGAIN;
18308634b51fSJeff Layton 	struct file_lock *fl, *victim = NULL;
1831c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
1832128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
1833c45198edSJeff Layton 	LIST_HEAD(dispose);
18348335ebd9SJ. Bruce Fields 
1835401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
18368634b51fSJeff Layton 	if (!ctx) {
18378634b51fSJeff Layton 		trace_generic_delete_lease(inode, NULL);
18388634b51fSJeff Layton 		return error;
18398634b51fSJeff Layton 	}
18408634b51fSJeff Layton 
184102e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
18426109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
18438634b51fSJeff Layton 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
18442ab99ee1SChristoph Hellwig 		if (fl->fl_file == filp &&
18452ab99ee1SChristoph Hellwig 		    fl->fl_owner == owner) {
18468634b51fSJeff Layton 			victim = fl;
18470efaa7e8SJeff Layton 			break;
18488335ebd9SJ. Bruce Fields 		}
18498634b51fSJeff Layton 	}
1850a9b1b455SJeff Layton 	trace_generic_delete_lease(inode, victim);
18518634b51fSJeff Layton 	if (victim)
18527448cc37SJeff Layton 		error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
18536109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
185402e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
1855c45198edSJeff Layton 	locks_dispose_list(&dispose);
18560efaa7e8SJeff Layton 	return error;
18578335ebd9SJ. Bruce Fields }
18588335ebd9SJ. Bruce Fields 
18591da177e4SLinus Torvalds /**
18601da177e4SLinus Torvalds  *	generic_setlease	-	sets a lease on an open file
18611da177e4SLinus Torvalds  *	@filp:	file pointer
18621da177e4SLinus Torvalds  *	@arg:	type of lease to obtain
18631da177e4SLinus Torvalds  *	@flp:	input - file_lock to use, output - file_lock inserted
18641c7dd2ffSJeff Layton  *	@priv:	private data for lm_setup (may be NULL if lm_setup
18651c7dd2ffSJeff Layton  *		doesn't require it)
18661da177e4SLinus Torvalds  *
18671da177e4SLinus Torvalds  *	The (input) flp->fl_lmops->lm_break function is required
18681da177e4SLinus Torvalds  *	by break_lease().
18691da177e4SLinus Torvalds  */
generic_setlease(struct file * filp,int arg,struct file_lock ** flp,void ** priv)1870ed5f17f6SLuca Vizzarro int generic_setlease(struct file *filp, int arg, struct file_lock **flp,
1871e6f5c789SJeff Layton 			void **priv)
18721da177e4SLinus Torvalds {
1873c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
187442d0c4bdSSeth Forshee 	vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(filp), inode);
18758335ebd9SJ. Bruce Fields 	int error;
18761da177e4SLinus Torvalds 
187742d0c4bdSSeth Forshee 	if ((!vfsuid_eq_kuid(vfsuid, current_fsuid())) && !capable(CAP_LEASE))
18788335ebd9SJ. Bruce Fields 		return -EACCES;
18791da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
18808335ebd9SJ. Bruce Fields 		return -EINVAL;
18811da177e4SLinus Torvalds 	error = security_file_lock(filp, arg);
18821da177e4SLinus Torvalds 	if (error)
18838335ebd9SJ. Bruce Fields 		return error;
18841da177e4SLinus Torvalds 
18858335ebd9SJ. Bruce Fields 	switch (arg) {
18868335ebd9SJ. Bruce Fields 	case F_UNLCK:
18872ab99ee1SChristoph Hellwig 		return generic_delete_lease(filp, *priv);
18888335ebd9SJ. Bruce Fields 	case F_RDLCK:
18898335ebd9SJ. Bruce Fields 	case F_WRLCK:
18900efaa7e8SJeff Layton 		if (!(*flp)->fl_lmops->lm_break) {
18910efaa7e8SJeff Layton 			WARN_ON_ONCE(1);
18920efaa7e8SJeff Layton 			return -ENOLCK;
18930efaa7e8SJeff Layton 		}
189411afe9f7SChristoph Hellwig 
1895e6f5c789SJeff Layton 		return generic_add_lease(filp, arg, flp, priv);
18968335ebd9SJ. Bruce Fields 	default:
18978d657eb3SDave Jones 		return -EINVAL;
18981da177e4SLinus Torvalds 	}
18991da177e4SLinus Torvalds }
19000af1a450SChristoph Hellwig EXPORT_SYMBOL(generic_setlease);
19011da177e4SLinus Torvalds 
190218f6622eSJeff Layton /*
190318f6622eSJeff Layton  * Kernel subsystems can register to be notified on any attempt to set
190418f6622eSJeff Layton  * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd
190518f6622eSJeff Layton  * to close files that it may have cached when there is an attempt to set a
190618f6622eSJeff Layton  * conflicting lease.
190718f6622eSJeff Layton  */
190818f6622eSJeff Layton static struct srcu_notifier_head lease_notifier_chain;
190918f6622eSJeff Layton 
191018f6622eSJeff Layton static inline void
lease_notifier_chain_init(void)191118f6622eSJeff Layton lease_notifier_chain_init(void)
191218f6622eSJeff Layton {
191318f6622eSJeff Layton 	srcu_init_notifier_head(&lease_notifier_chain);
191418f6622eSJeff Layton }
191518f6622eSJeff Layton 
191618f6622eSJeff Layton static inline void
setlease_notifier(int arg,struct file_lock * lease)1917ed5f17f6SLuca Vizzarro setlease_notifier(int arg, struct file_lock *lease)
191818f6622eSJeff Layton {
191918f6622eSJeff Layton 	if (arg != F_UNLCK)
192018f6622eSJeff Layton 		srcu_notifier_call_chain(&lease_notifier_chain, arg, lease);
192118f6622eSJeff Layton }
192218f6622eSJeff Layton 
lease_register_notifier(struct notifier_block * nb)192318f6622eSJeff Layton int lease_register_notifier(struct notifier_block *nb)
192418f6622eSJeff Layton {
192518f6622eSJeff Layton 	return srcu_notifier_chain_register(&lease_notifier_chain, nb);
192618f6622eSJeff Layton }
192718f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_register_notifier);
192818f6622eSJeff Layton 
lease_unregister_notifier(struct notifier_block * nb)192918f6622eSJeff Layton void lease_unregister_notifier(struct notifier_block *nb)
193018f6622eSJeff Layton {
193118f6622eSJeff Layton 	srcu_notifier_chain_unregister(&lease_notifier_chain, nb);
193218f6622eSJeff Layton }
193318f6622eSJeff Layton EXPORT_SYMBOL_GPL(lease_unregister_notifier);
193418f6622eSJeff Layton 
19351da177e4SLinus Torvalds /**
1936a9933ceaSJ. Bruce Fields  * vfs_setlease        -       sets a lease on an open file
19371da177e4SLinus Torvalds  * @filp:	file pointer
19381da177e4SLinus Torvalds  * @arg:	type of lease to obtain
1939e51673aaSJeff Layton  * @lease:	file_lock to use when adding a lease
19401c7dd2ffSJeff Layton  * @priv:	private info for lm_setup when adding a lease (may be
19411c7dd2ffSJeff Layton  *		NULL if lm_setup doesn't require it)
19421da177e4SLinus Torvalds  *
1943e51673aaSJeff Layton  * Call this to establish a lease on the file. The "lease" argument is not
1944e51673aaSJeff Layton  * used for F_UNLCK requests and may be NULL. For commands that set or alter
194580b79dd0SMauro Carvalho Chehab  * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
194680b79dd0SMauro Carvalho Chehab  * set; if not, this function will return -ENOLCK (and generate a scary-looking
1947e51673aaSJeff Layton  * stack trace).
19481c7dd2ffSJeff Layton  *
19491c7dd2ffSJeff Layton  * The "priv" pointer is passed directly to the lm_setup function as-is. It
19501c7dd2ffSJeff Layton  * may be NULL if the lm_setup operation doesn't require it.
19511da177e4SLinus Torvalds  */
1952e6f5c789SJeff Layton int
vfs_setlease(struct file * filp,int arg,struct file_lock ** lease,void ** priv)1953ed5f17f6SLuca Vizzarro vfs_setlease(struct file *filp, int arg, struct file_lock **lease, void **priv)
19541da177e4SLinus Torvalds {
195518f6622eSJeff Layton 	if (lease)
195618f6622eSJeff Layton 		setlease_notifier(arg, *lease);
1957de2a4a50SMiklos Szeredi 	if (filp->f_op->setlease)
1958f82b4b67SJeff Layton 		return filp->f_op->setlease(filp, arg, lease, priv);
19591c7dd2ffSJeff Layton 	else
1960f82b4b67SJeff Layton 		return generic_setlease(filp, arg, lease, priv);
19611da177e4SLinus Torvalds }
1962a9933ceaSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_setlease);
19631da177e4SLinus Torvalds 
do_fcntl_add_lease(unsigned int fd,struct file * filp,int arg)1964ed5f17f6SLuca Vizzarro static int do_fcntl_add_lease(unsigned int fd, struct file *filp, int arg)
19651da177e4SLinus Torvalds {
19661c7dd2ffSJeff Layton 	struct file_lock *fl;
1967f7347ce4SLinus Torvalds 	struct fasync_struct *new;
19681da177e4SLinus Torvalds 	int error;
19691da177e4SLinus Torvalds 
1970c5b1f0d9SArnd Bergmann 	fl = lease_alloc(filp, arg);
1971c5b1f0d9SArnd Bergmann 	if (IS_ERR(fl))
1972c5b1f0d9SArnd Bergmann 		return PTR_ERR(fl);
19731da177e4SLinus Torvalds 
1974f7347ce4SLinus Torvalds 	new = fasync_alloc();
1975f7347ce4SLinus Torvalds 	if (!new) {
1976f7347ce4SLinus Torvalds 		locks_free_lock(fl);
1977f7347ce4SLinus Torvalds 		return -ENOMEM;
1978f7347ce4SLinus Torvalds 	}
19791c7dd2ffSJeff Layton 	new->fa_fd = fd;
19801da177e4SLinus Torvalds 
19811c7dd2ffSJeff Layton 	error = vfs_setlease(filp, arg, &fl, (void **)&new);
19822dfb928fSJeff Layton 	if (fl)
19832dfb928fSJeff Layton 		locks_free_lock(fl);
1984f7347ce4SLinus Torvalds 	if (new)
1985f7347ce4SLinus Torvalds 		fasync_free(new);
19861da177e4SLinus Torvalds 	return error;
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds /**
19900ceaf6c7SJ. Bruce Fields  *	fcntl_setlease	-	sets a lease on an open file
19910ceaf6c7SJ. Bruce Fields  *	@fd: open file descriptor
19920ceaf6c7SJ. Bruce Fields  *	@filp: file pointer
19930ceaf6c7SJ. Bruce Fields  *	@arg: type of lease to obtain
19940ceaf6c7SJ. Bruce Fields  *
19950ceaf6c7SJ. Bruce Fields  *	Call this fcntl to establish a lease on the file.
19960ceaf6c7SJ. Bruce Fields  *	Note that you also need to call %F_SETSIG to
19970ceaf6c7SJ. Bruce Fields  *	receive a signal when the lease is broken.
19980ceaf6c7SJ. Bruce Fields  */
fcntl_setlease(unsigned int fd,struct file * filp,int arg)1999ed5f17f6SLuca Vizzarro int fcntl_setlease(unsigned int fd, struct file *filp, int arg)
20000ceaf6c7SJ. Bruce Fields {
20010ceaf6c7SJ. Bruce Fields 	if (arg == F_UNLCK)
20022ab99ee1SChristoph Hellwig 		return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
20030ceaf6c7SJ. Bruce Fields 	return do_fcntl_add_lease(fd, filp, arg);
20040ceaf6c7SJ. Bruce Fields }
20050ceaf6c7SJ. Bruce Fields 
20060ceaf6c7SJ. Bruce Fields /**
200729d01b22SJeff Layton  * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
200829d01b22SJeff Layton  * @inode: inode of the file to apply to
20091da177e4SLinus Torvalds  * @fl: The lock to be applied
20101da177e4SLinus Torvalds  *
201129d01b22SJeff Layton  * Apply a FLOCK style lock request to an inode.
20121da177e4SLinus Torvalds  */
flock_lock_inode_wait(struct inode * inode,struct file_lock * fl)2013616fb38fSBenjamin Coddington static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
20141da177e4SLinus Torvalds {
20151da177e4SLinus Torvalds 	int error;
20161da177e4SLinus Torvalds 	might_sleep();
20171da177e4SLinus Torvalds 	for (;;) {
201829d01b22SJeff Layton 		error = flock_lock_inode(inode, fl);
2019bde74e4bSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
20201da177e4SLinus Torvalds 			break;
2021dcf23ac3SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait,
2022dcf23ac3SLinus Torvalds 				list_empty(&fl->fl_blocked_member));
202316306a61SNeilBrown 		if (error)
20241da177e4SLinus Torvalds 			break;
20251da177e4SLinus Torvalds 	}
202616306a61SNeilBrown 	locks_delete_block(fl);
20271da177e4SLinus Torvalds 	return error;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
203029d01b22SJeff Layton /**
2031e55c34a6SBenjamin Coddington  * locks_lock_inode_wait - Apply a lock to an inode
2032e55c34a6SBenjamin Coddington  * @inode: inode of the file to apply to
2033e55c34a6SBenjamin Coddington  * @fl: The lock to be applied
2034e55c34a6SBenjamin Coddington  *
2035e55c34a6SBenjamin Coddington  * Apply a POSIX or FLOCK style lock request to an inode.
2036e55c34a6SBenjamin Coddington  */
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)2037e55c34a6SBenjamin Coddington int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
2038e55c34a6SBenjamin Coddington {
2039e55c34a6SBenjamin Coddington 	int res = 0;
2040e55c34a6SBenjamin Coddington 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2041e55c34a6SBenjamin Coddington 		case FL_POSIX:
2042e55c34a6SBenjamin Coddington 			res = posix_lock_inode_wait(inode, fl);
2043e55c34a6SBenjamin Coddington 			break;
2044e55c34a6SBenjamin Coddington 		case FL_FLOCK:
2045e55c34a6SBenjamin Coddington 			res = flock_lock_inode_wait(inode, fl);
2046e55c34a6SBenjamin Coddington 			break;
2047e55c34a6SBenjamin Coddington 		default:
2048e55c34a6SBenjamin Coddington 			BUG();
2049e55c34a6SBenjamin Coddington 	}
2050e55c34a6SBenjamin Coddington 	return res;
2051e55c34a6SBenjamin Coddington }
2052e55c34a6SBenjamin Coddington EXPORT_SYMBOL(locks_lock_inode_wait);
2053e55c34a6SBenjamin Coddington 
2054e55c34a6SBenjamin Coddington /**
20551da177e4SLinus Torvalds  *	sys_flock: - flock() system call.
20561da177e4SLinus Torvalds  *	@fd: the file descriptor to lock.
20571da177e4SLinus Torvalds  *	@cmd: the type of lock to apply.
20581da177e4SLinus Torvalds  *
20591da177e4SLinus Torvalds  *	Apply a %FL_FLOCK style lock to an open file descriptor.
206080b79dd0SMauro Carvalho Chehab  *	The @cmd can be one of:
20611da177e4SLinus Torvalds  *
206280b79dd0SMauro Carvalho Chehab  *	- %LOCK_SH -- a shared lock.
206380b79dd0SMauro Carvalho Chehab  *	- %LOCK_EX -- an exclusive lock.
206480b79dd0SMauro Carvalho Chehab  *	- %LOCK_UN -- remove an existing lock.
206590f7d7a0SJeff Layton  *	- %LOCK_MAND -- a 'mandatory' flock. (DEPRECATED)
20661da177e4SLinus Torvalds  *
206790f7d7a0SJeff Layton  *	%LOCK_MAND support has been removed from the kernel.
20681da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(flock,unsigned int,fd,unsigned int,cmd)2069002c8976SHeiko Carstens SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
20701da177e4SLinus Torvalds {
2071db4abb4aSKuniyuki Iwashima 	int can_sleep, error, type;
20724149be7bSKuniyuki Iwashima 	struct file_lock fl;
2073db4abb4aSKuniyuki Iwashima 	struct fd f;
20741da177e4SLinus Torvalds 
207590f7d7a0SJeff Layton 	/*
207690f7d7a0SJeff Layton 	 * LOCK_MAND locks were broken for a long time in that they never
207790f7d7a0SJeff Layton 	 * conflicted with one another and didn't prevent any sort of open,
207890f7d7a0SJeff Layton 	 * read or write activity.
207990f7d7a0SJeff Layton 	 *
208090f7d7a0SJeff Layton 	 * Just ignore these requests now, to preserve legacy behavior, but
208190f7d7a0SJeff Layton 	 * throw a warning to let people know that they don't actually work.
208290f7d7a0SJeff Layton 	 */
208390f7d7a0SJeff Layton 	if (cmd & LOCK_MAND) {
2084f2f2494cSAndi Kleen 		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);
2085db4abb4aSKuniyuki Iwashima 		return 0;
208690f7d7a0SJeff Layton 	}
208790f7d7a0SJeff Layton 
2088db4abb4aSKuniyuki Iwashima 	type = flock_translate_cmd(cmd & ~LOCK_NB);
2089db4abb4aSKuniyuki Iwashima 	if (type < 0)
2090db4abb4aSKuniyuki Iwashima 		return type;
2091db4abb4aSKuniyuki Iwashima 
2092db4abb4aSKuniyuki Iwashima 	error = -EBADF;
2093db4abb4aSKuniyuki Iwashima 	f = fdget(fd);
2094db4abb4aSKuniyuki Iwashima 	if (!f.file)
2095db4abb4aSKuniyuki Iwashima 		return error;
2096db4abb4aSKuniyuki Iwashima 
2097db4abb4aSKuniyuki Iwashima 	if (type != F_UNLCK && !(f.file->f_mode & (FMODE_READ | FMODE_WRITE)))
20981da177e4SLinus Torvalds 		goto out_putf;
20996e129d00SJeff Layton 
21004149be7bSKuniyuki Iwashima 	flock_make_lock(f.file, &fl, type);
21011da177e4SLinus Torvalds 
21024149be7bSKuniyuki Iwashima 	error = security_file_lock(f.file, fl.fl_type);
21031da177e4SLinus Torvalds 	if (error)
21044149be7bSKuniyuki Iwashima 		goto out_putf;
21051da177e4SLinus Torvalds 
2106db4abb4aSKuniyuki Iwashima 	can_sleep = !(cmd & LOCK_NB);
2107db4abb4aSKuniyuki Iwashima 	if (can_sleep)
2108db4abb4aSKuniyuki Iwashima 		fl.fl_flags |= FL_SLEEP;
2109db4abb4aSKuniyuki Iwashima 
2110de2a4a50SMiklos Szeredi 	if (f.file->f_op->flock)
21112903ff01SAl Viro 		error = f.file->f_op->flock(f.file,
21121da177e4SLinus Torvalds 					    (can_sleep) ? F_SETLKW : F_SETLK,
21134149be7bSKuniyuki Iwashima 					    &fl);
21141da177e4SLinus Torvalds 	else
21154149be7bSKuniyuki Iwashima 		error = locks_lock_file_wait(f.file, &fl);
21161da177e4SLinus Torvalds 
2117932c29a1SDavid Howells 	locks_release_private(&fl);
21181da177e4SLinus Torvalds  out_putf:
21192903ff01SAl Viro 	fdput(f);
2120db4abb4aSKuniyuki Iwashima 
21211da177e4SLinus Torvalds 	return error;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
21243ee17abdSJ. Bruce Fields /**
21253ee17abdSJ. Bruce Fields  * vfs_test_lock - test file byte range lock
21263ee17abdSJ. Bruce Fields  * @filp: The file to test lock for
21276924c554SJ. Bruce Fields  * @fl: The lock to test; also used to hold result
21283ee17abdSJ. Bruce Fields  *
21293ee17abdSJ. Bruce Fields  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
21303ee17abdSJ. Bruce Fields  * setting conf->fl_type to something other than F_UNLCK.
21313ee17abdSJ. Bruce Fields  */
vfs_test_lock(struct file * filp,struct file_lock * fl)21323ee17abdSJ. Bruce Fields int vfs_test_lock(struct file *filp, struct file_lock *fl)
21333ee17abdSJ. Bruce Fields {
21347e8e5cc8SJeff Layton 	WARN_ON_ONCE(filp != fl->fl_file);
2135de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
21363ee17abdSJ. Bruce Fields 		return filp->f_op->lock(filp, F_GETLK, fl);
21373ee17abdSJ. Bruce Fields 	posix_test_lock(filp, fl);
21383ee17abdSJ. Bruce Fields 	return 0;
21393ee17abdSJ. Bruce Fields }
21403ee17abdSJ. Bruce Fields EXPORT_SYMBOL_GPL(vfs_test_lock);
21413ee17abdSJ. Bruce Fields 
21429d5b86acSBenjamin Coddington /**
21439d5b86acSBenjamin Coddington  * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
21449d5b86acSBenjamin Coddington  * @fl: The file_lock who's fl_pid should be translated
21459d5b86acSBenjamin Coddington  * @ns: The namespace into which the pid should be translated
21469d5b86acSBenjamin Coddington  *
2147bd4c4680SJakub Wilk  * Used to translate a fl_pid into a namespace virtual pid number
21489d5b86acSBenjamin Coddington  */
locks_translate_pid(struct file_lock * fl,struct pid_namespace * ns)21499d5b86acSBenjamin Coddington static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns)
21509d5b86acSBenjamin Coddington {
21519d5b86acSBenjamin Coddington 	pid_t vnr;
21529d5b86acSBenjamin Coddington 	struct pid *pid;
21539d5b86acSBenjamin Coddington 
21549d5b86acSBenjamin Coddington 	if (IS_OFDLCK(fl))
21559d5b86acSBenjamin Coddington 		return -1;
21569d5b86acSBenjamin Coddington 	if (IS_REMOTELCK(fl))
21579d5b86acSBenjamin Coddington 		return fl->fl_pid;
2158826d7bc9SKonstantin Khorenko 	/*
2159826d7bc9SKonstantin Khorenko 	 * If the flock owner process is dead and its pid has been already
2160826d7bc9SKonstantin Khorenko 	 * freed, the translation below won't work, but we still want to show
2161826d7bc9SKonstantin Khorenko 	 * flock owner pid number in init pidns.
2162826d7bc9SKonstantin Khorenko 	 */
2163826d7bc9SKonstantin Khorenko 	if (ns == &init_pid_ns)
2164826d7bc9SKonstantin Khorenko 		return (pid_t)fl->fl_pid;
21659d5b86acSBenjamin Coddington 
21669d5b86acSBenjamin Coddington 	rcu_read_lock();
21679d5b86acSBenjamin Coddington 	pid = find_pid_ns(fl->fl_pid, &init_pid_ns);
21689d5b86acSBenjamin Coddington 	vnr = pid_nr_ns(pid, ns);
21699d5b86acSBenjamin Coddington 	rcu_read_unlock();
21709d5b86acSBenjamin Coddington 	return vnr;
21719d5b86acSBenjamin Coddington }
21729d5b86acSBenjamin Coddington 
posix_lock_to_flock(struct flock * flock,struct file_lock * fl)2173c2fa1b8aSJ. Bruce Fields static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2174c2fa1b8aSJ. Bruce Fields {
21759d5b86acSBenjamin Coddington 	flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2176c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
2177c2fa1b8aSJ. Bruce Fields 	/*
2178c2fa1b8aSJ. Bruce Fields 	 * Make sure we can represent the posix lock via
2179c2fa1b8aSJ. Bruce Fields 	 * legacy 32bit flock.
2180c2fa1b8aSJ. Bruce Fields 	 */
2181c2fa1b8aSJ. Bruce Fields 	if (fl->fl_start > OFFT_OFFSET_MAX)
2182c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2183c2fa1b8aSJ. Bruce Fields 	if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2184c2fa1b8aSJ. Bruce Fields 		return -EOVERFLOW;
2185c2fa1b8aSJ. Bruce Fields #endif
2186c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
2187c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2188c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2189c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2190129a84deSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2191c2fa1b8aSJ. Bruce Fields 	return 0;
2192c2fa1b8aSJ. Bruce Fields }
2193c2fa1b8aSJ. Bruce Fields 
2194c2fa1b8aSJ. Bruce Fields #if BITS_PER_LONG == 32
posix_lock_to_flock64(struct flock64 * flock,struct file_lock * fl)2195c2fa1b8aSJ. Bruce Fields static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2196c2fa1b8aSJ. Bruce Fields {
21979d5b86acSBenjamin Coddington 	flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
2198c2fa1b8aSJ. Bruce Fields 	flock->l_start = fl->fl_start;
2199c2fa1b8aSJ. Bruce Fields 	flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2200c2fa1b8aSJ. Bruce Fields 		fl->fl_end - fl->fl_start + 1;
2201c2fa1b8aSJ. Bruce Fields 	flock->l_whence = 0;
2202c2fa1b8aSJ. Bruce Fields 	flock->l_type = fl->fl_type;
2203c2fa1b8aSJ. Bruce Fields }
2204c2fa1b8aSJ. Bruce Fields #endif
2205c2fa1b8aSJ. Bruce Fields 
22061da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
22071da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
22081da177e4SLinus Torvalds  */
fcntl_getlk(struct file * filp,unsigned int cmd,struct flock * flock)2209a75d30c7SChristoph Hellwig int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
22101da177e4SLinus Torvalds {
221152306e88SBenjamin Coddington 	struct file_lock *fl;
22121da177e4SLinus Torvalds 	int error;
22131da177e4SLinus Torvalds 
221452306e88SBenjamin Coddington 	fl = locks_alloc_lock();
221552306e88SBenjamin Coddington 	if (fl == NULL)
221652306e88SBenjamin Coddington 		return -ENOMEM;
22171da177e4SLinus Torvalds 	error = -EINVAL;
22186c9007f6SStas Sergeev 	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
22196c9007f6SStas Sergeev 			&& flock->l_type != F_WRLCK)
22201da177e4SLinus Torvalds 		goto out;
22211da177e4SLinus Torvalds 
222252306e88SBenjamin Coddington 	error = flock_to_posix_lock(filp, fl, flock);
22231da177e4SLinus Torvalds 	if (error)
22241da177e4SLinus Torvalds 		goto out;
22251da177e4SLinus Torvalds 
22260d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
222790478939SJeff Layton 		error = -EINVAL;
2228a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
222990478939SJeff Layton 			goto out;
223090478939SJeff Layton 
223152306e88SBenjamin Coddington 		fl->fl_flags |= FL_OFDLCK;
223252306e88SBenjamin Coddington 		fl->fl_owner = filp;
22335d50ffd7SJeff Layton 	}
22345d50ffd7SJeff Layton 
223552306e88SBenjamin Coddington 	error = vfs_test_lock(filp, fl);
22363ee17abdSJ. Bruce Fields 	if (error)
22371da177e4SLinus Torvalds 		goto out;
22381da177e4SLinus Torvalds 
223952306e88SBenjamin Coddington 	flock->l_type = fl->fl_type;
224052306e88SBenjamin Coddington 	if (fl->fl_type != F_UNLCK) {
224152306e88SBenjamin Coddington 		error = posix_lock_to_flock(flock, fl);
2242c2fa1b8aSJ. Bruce Fields 		if (error)
224352306e88SBenjamin Coddington 			goto out;
22441da177e4SLinus Torvalds 	}
22451da177e4SLinus Torvalds out:
224652306e88SBenjamin Coddington 	locks_free_lock(fl);
22471da177e4SLinus Torvalds 	return error;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
22507723ec97SMarc Eshel /**
22517723ec97SMarc Eshel  * vfs_lock_file - file byte range lock
22527723ec97SMarc Eshel  * @filp: The file to apply the lock to
22537723ec97SMarc Eshel  * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
22547723ec97SMarc Eshel  * @fl: The lock to be applied
2255150b3934SMarc Eshel  * @conf: Place to return a copy of the conflicting lock, if found.
2256150b3934SMarc Eshel  *
2257150b3934SMarc Eshel  * A caller that doesn't care about the conflicting lock may pass NULL
2258150b3934SMarc Eshel  * as the final argument.
2259150b3934SMarc Eshel  *
2260150b3934SMarc Eshel  * If the filesystem defines a private ->lock() method, then @conf will
2261150b3934SMarc Eshel  * be left unchanged; so a caller that cares should initialize it to
2262150b3934SMarc Eshel  * some acceptable default.
22632beb6614SMarc Eshel  *
22642beb6614SMarc Eshel  * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
22652beb6614SMarc Eshel  * locks, the ->lock() interface may return asynchronously, before the lock has
22662beb6614SMarc Eshel  * been granted or denied by the underlying filesystem, if (and only if)
22678fb47a4fSJ. Bruce Fields  * lm_grant is set. Callers expecting ->lock() to return asynchronously
22682beb6614SMarc Eshel  * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
22692beb6614SMarc Eshel  * the request is for a blocking lock. When ->lock() does return asynchronously,
22708fb47a4fSJ. Bruce Fields  * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
22712beb6614SMarc Eshel  * request completes.
22722beb6614SMarc Eshel  * If the request is for non-blocking lock the file system should return
2273bde74e4bSMiklos Szeredi  * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2274bde74e4bSMiklos Szeredi  * with the result. If the request timed out the callback routine will return a
22752beb6614SMarc Eshel  * nonzero return code and the file system should release the lock. The file
22762beb6614SMarc Eshel  * system is also responsible to keep a corresponding posix lock when it
22772beb6614SMarc Eshel  * grants a lock so the VFS can find out which locks are locally held and do
22782beb6614SMarc Eshel  * the correct lock cleanup when required.
22792beb6614SMarc Eshel  * The underlying filesystem must not drop the kernel lock or call
22808fb47a4fSJ. Bruce Fields  * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
22812beb6614SMarc Eshel  * return code.
22827723ec97SMarc Eshel  */
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)2283150b3934SMarc Eshel int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
22847723ec97SMarc Eshel {
22857e8e5cc8SJeff Layton 	WARN_ON_ONCE(filp != fl->fl_file);
2286de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
22877723ec97SMarc Eshel 		return filp->f_op->lock(filp, cmd, fl);
22887723ec97SMarc Eshel 	else
2289150b3934SMarc Eshel 		return posix_lock_file(filp, fl, conf);
22907723ec97SMarc Eshel }
22917723ec97SMarc Eshel EXPORT_SYMBOL_GPL(vfs_lock_file);
22927723ec97SMarc Eshel 
do_lock_file_wait(struct file * filp,unsigned int cmd,struct file_lock * fl)2293b648a6deSMiklos Szeredi static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2294b648a6deSMiklos Szeredi 			     struct file_lock *fl)
2295b648a6deSMiklos Szeredi {
2296b648a6deSMiklos Szeredi 	int error;
2297b648a6deSMiklos Szeredi 
2298b648a6deSMiklos Szeredi 	error = security_file_lock(filp, fl->fl_type);
2299b648a6deSMiklos Szeredi 	if (error)
2300b648a6deSMiklos Szeredi 		return error;
2301b648a6deSMiklos Szeredi 
2302b648a6deSMiklos Szeredi 	for (;;) {
2303764c76b3SMiklos Szeredi 		error = vfs_lock_file(filp, cmd, fl, NULL);
2304b648a6deSMiklos Szeredi 		if (error != FILE_LOCK_DEFERRED)
2305b648a6deSMiklos Szeredi 			break;
2306dcf23ac3SLinus Torvalds 		error = wait_event_interruptible(fl->fl_wait,
2307dcf23ac3SLinus Torvalds 					list_empty(&fl->fl_blocked_member));
230816306a61SNeilBrown 		if (error)
2309b648a6deSMiklos Szeredi 			break;
2310b648a6deSMiklos Szeredi 	}
231116306a61SNeilBrown 	locks_delete_block(fl);
2312b648a6deSMiklos Szeredi 
2313b648a6deSMiklos Szeredi 	return error;
2314b648a6deSMiklos Szeredi }
2315b648a6deSMiklos Szeredi 
23166ca7d910SBenjamin Coddington /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
2317cf01f4eeSJeff Layton static int
check_fmode_for_setlk(struct file_lock * fl)2318cf01f4eeSJeff Layton check_fmode_for_setlk(struct file_lock *fl)
2319cf01f4eeSJeff Layton {
2320cf01f4eeSJeff Layton 	switch (fl->fl_type) {
2321cf01f4eeSJeff Layton 	case F_RDLCK:
2322cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_READ))
2323cf01f4eeSJeff Layton 			return -EBADF;
2324cf01f4eeSJeff Layton 		break;
2325cf01f4eeSJeff Layton 	case F_WRLCK:
2326cf01f4eeSJeff Layton 		if (!(fl->fl_file->f_mode & FMODE_WRITE))
2327cf01f4eeSJeff Layton 			return -EBADF;
2328cf01f4eeSJeff Layton 	}
2329cf01f4eeSJeff Layton 	return 0;
2330cf01f4eeSJeff Layton }
2331cf01f4eeSJeff Layton 
23321da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
23331da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
23341da177e4SLinus Torvalds  */
fcntl_setlk(unsigned int fd,struct file * filp,unsigned int cmd,struct flock * flock)2335c293621bSPeter Staubach int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2336a75d30c7SChristoph Hellwig 		struct flock *flock)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
2339c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
23400b2bac2fSAl Viro 	struct file *f;
23411da177e4SLinus Torvalds 	int error;
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	if (file_lock == NULL)
23441da177e4SLinus Torvalds 		return -ENOLCK;
23451da177e4SLinus Torvalds 
2346a75d30c7SChristoph Hellwig 	error = flock_to_posix_lock(filp, file_lock, flock);
23471da177e4SLinus Torvalds 	if (error)
23481da177e4SLinus Torvalds 		goto out;
23495d50ffd7SJeff Layton 
2350cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2351cf01f4eeSJeff Layton 	if (error)
2352cf01f4eeSJeff Layton 		goto out;
2353cf01f4eeSJeff Layton 
23545d50ffd7SJeff Layton 	/*
23555d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2356cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
23575d50ffd7SJeff Layton 	 */
23585d50ffd7SJeff Layton 	switch (cmd) {
23590d3f7a2dSJeff Layton 	case F_OFD_SETLK:
236090478939SJeff Layton 		error = -EINVAL;
2361a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
236290478939SJeff Layton 			goto out;
236390478939SJeff Layton 
23645d50ffd7SJeff Layton 		cmd = F_SETLK;
2365cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
236673a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
23675d50ffd7SJeff Layton 		break;
23680d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
236990478939SJeff Layton 		error = -EINVAL;
2370a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
237190478939SJeff Layton 			goto out;
237290478939SJeff Layton 
23735d50ffd7SJeff Layton 		cmd = F_SETLKW;
2374cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
237573a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
2376df561f66SGustavo A. R. Silva 		fallthrough;
23775d50ffd7SJeff Layton 	case F_SETLKW:
23781da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
23791da177e4SLinus Torvalds 	}
23801da177e4SLinus Torvalds 
2381b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2382c293621bSPeter Staubach 
2383c293621bSPeter Staubach 	/*
23845f5d0799SJann Horn 	 * Detect close/fcntl races and recover by zapping all POSIX locks
23855f5d0799SJann Horn 	 * associated with this file and our files_struct, just like on
23865f5d0799SJann Horn 	 * filp_flush(). There is no need to do that when we're
23870752ba80SJeff Layton 	 * unlocking though, or for OFD locks.
2388c293621bSPeter Staubach 	 */
23890752ba80SJeff Layton 	if (!error && file_lock->fl_type != F_UNLCK &&
23900752ba80SJeff Layton 	    !(file_lock->fl_flags & FL_OFDLCK)) {
2391120ce2b0SEric W. Biederman 		struct files_struct *files = current->files;
23920b2bac2fSAl Viro 		/*
23937f3697e2SJeff Layton 		 * We need that spin_lock here - it prevents reordering between
23947f3697e2SJeff Layton 		 * update of i_flctx->flc_posix and check for it done in
23957f3697e2SJeff Layton 		 * close(). rcu_read_lock() wouldn't do.
23960b2bac2fSAl Viro 		 */
2397120ce2b0SEric W. Biederman 		spin_lock(&files->file_lock);
2398120ce2b0SEric W. Biederman 		f = files_lookup_fd_locked(files, fd);
2399120ce2b0SEric W. Biederman 		spin_unlock(&files->file_lock);
24007f3697e2SJeff Layton 		if (f != filp) {
24015f5d0799SJann Horn 			locks_remove_posix(filp, files);
24027f3697e2SJeff Layton 			error = -EBADF;
2403c293621bSPeter Staubach 		}
24047f3697e2SJeff Layton 	}
24051da177e4SLinus Torvalds out:
24061890910fSJeff Layton 	trace_fcntl_setlk(inode, file_lock, error);
24071da177e4SLinus Torvalds 	locks_free_lock(file_lock);
24081da177e4SLinus Torvalds 	return error;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds #if BITS_PER_LONG == 32
24121da177e4SLinus Torvalds /* Report the first existing lock that would conflict with l.
24131da177e4SLinus Torvalds  * This implements the F_GETLK command of fcntl().
24141da177e4SLinus Torvalds  */
fcntl_getlk64(struct file * filp,unsigned int cmd,struct flock64 * flock)2415a75d30c7SChristoph Hellwig int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
24161da177e4SLinus Torvalds {
241752306e88SBenjamin Coddington 	struct file_lock *fl;
24181da177e4SLinus Torvalds 	int error;
24191da177e4SLinus Torvalds 
242052306e88SBenjamin Coddington 	fl = locks_alloc_lock();
242152306e88SBenjamin Coddington 	if (fl == NULL)
242252306e88SBenjamin Coddington 		return -ENOMEM;
242352306e88SBenjamin Coddington 
24241da177e4SLinus Torvalds 	error = -EINVAL;
24256c9007f6SStas Sergeev 	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
24266c9007f6SStas Sergeev 			&& flock->l_type != F_WRLCK)
24271da177e4SLinus Torvalds 		goto out;
24281da177e4SLinus Torvalds 
242952306e88SBenjamin Coddington 	error = flock64_to_posix_lock(filp, fl, flock);
24301da177e4SLinus Torvalds 	if (error)
24311da177e4SLinus Torvalds 		goto out;
24321da177e4SLinus Torvalds 
24330d3f7a2dSJeff Layton 	if (cmd == F_OFD_GETLK) {
243490478939SJeff Layton 		error = -EINVAL;
2435a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
243690478939SJeff Layton 			goto out;
243790478939SJeff Layton 
243852306e88SBenjamin Coddington 		fl->fl_flags |= FL_OFDLCK;
243952306e88SBenjamin Coddington 		fl->fl_owner = filp;
24405d50ffd7SJeff Layton 	}
24415d50ffd7SJeff Layton 
244252306e88SBenjamin Coddington 	error = vfs_test_lock(filp, fl);
24433ee17abdSJ. Bruce Fields 	if (error)
24441da177e4SLinus Torvalds 		goto out;
24451da177e4SLinus Torvalds 
244652306e88SBenjamin Coddington 	flock->l_type = fl->fl_type;
244752306e88SBenjamin Coddington 	if (fl->fl_type != F_UNLCK)
244852306e88SBenjamin Coddington 		posix_lock_to_flock64(flock, fl);
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds out:
245152306e88SBenjamin Coddington 	locks_free_lock(fl);
24521da177e4SLinus Torvalds 	return error;
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds /* Apply the lock described by l to an open file descriptor.
24561da177e4SLinus Torvalds  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
24571da177e4SLinus Torvalds  */
fcntl_setlk64(unsigned int fd,struct file * filp,unsigned int cmd,struct flock64 * flock)2458c293621bSPeter Staubach int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2459a75d30c7SChristoph Hellwig 		struct flock64 *flock)
24601da177e4SLinus Torvalds {
24611da177e4SLinus Torvalds 	struct file_lock *file_lock = locks_alloc_lock();
24620b2bac2fSAl Viro 	struct file *f;
24631da177e4SLinus Torvalds 	int error;
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 	if (file_lock == NULL)
24661da177e4SLinus Torvalds 		return -ENOLCK;
24671da177e4SLinus Torvalds 
2468a75d30c7SChristoph Hellwig 	error = flock64_to_posix_lock(filp, file_lock, flock);
24691da177e4SLinus Torvalds 	if (error)
24701da177e4SLinus Torvalds 		goto out;
24715d50ffd7SJeff Layton 
2472cf01f4eeSJeff Layton 	error = check_fmode_for_setlk(file_lock);
2473cf01f4eeSJeff Layton 	if (error)
2474cf01f4eeSJeff Layton 		goto out;
2475cf01f4eeSJeff Layton 
24765d50ffd7SJeff Layton 	/*
24775d50ffd7SJeff Layton 	 * If the cmd is requesting file-private locks, then set the
2478cff2fce5SJeff Layton 	 * FL_OFDLCK flag and override the owner.
24795d50ffd7SJeff Layton 	 */
24805d50ffd7SJeff Layton 	switch (cmd) {
24810d3f7a2dSJeff Layton 	case F_OFD_SETLK:
248290478939SJeff Layton 		error = -EINVAL;
2483a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
248490478939SJeff Layton 			goto out;
248590478939SJeff Layton 
24865d50ffd7SJeff Layton 		cmd = F_SETLK64;
2487cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
248873a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
24895d50ffd7SJeff Layton 		break;
24900d3f7a2dSJeff Layton 	case F_OFD_SETLKW:
249190478939SJeff Layton 		error = -EINVAL;
2492a75d30c7SChristoph Hellwig 		if (flock->l_pid != 0)
249390478939SJeff Layton 			goto out;
249490478939SJeff Layton 
24955d50ffd7SJeff Layton 		cmd = F_SETLKW64;
2496cff2fce5SJeff Layton 		file_lock->fl_flags |= FL_OFDLCK;
249773a8f5f7SChristoph Hellwig 		file_lock->fl_owner = filp;
2498df561f66SGustavo A. R. Silva 		fallthrough;
24995d50ffd7SJeff Layton 	case F_SETLKW64:
25001da177e4SLinus Torvalds 		file_lock->fl_flags |= FL_SLEEP;
25011da177e4SLinus Torvalds 	}
25021da177e4SLinus Torvalds 
2503b648a6deSMiklos Szeredi 	error = do_lock_file_wait(filp, cmd, file_lock);
2504c293621bSPeter Staubach 
2505c293621bSPeter Staubach 	/*
2506*73ae3495SJann Horn 	 * Detect close/fcntl races and recover by zapping all POSIX locks
2507*73ae3495SJann Horn 	 * associated with this file and our files_struct, just like on
2508*73ae3495SJann Horn 	 * filp_flush(). There is no need to do that when we're
25090752ba80SJeff Layton 	 * unlocking though, or for OFD locks.
2510c293621bSPeter Staubach 	 */
25110752ba80SJeff Layton 	if (!error && file_lock->fl_type != F_UNLCK &&
25120752ba80SJeff Layton 	    !(file_lock->fl_flags & FL_OFDLCK)) {
2513120ce2b0SEric W. Biederman 		struct files_struct *files = current->files;
25147f3697e2SJeff Layton 		/*
25157f3697e2SJeff Layton 		 * We need that spin_lock here - it prevents reordering between
25167f3697e2SJeff Layton 		 * update of i_flctx->flc_posix and check for it done in
25177f3697e2SJeff Layton 		 * close(). rcu_read_lock() wouldn't do.
25187f3697e2SJeff Layton 		 */
2519120ce2b0SEric W. Biederman 		spin_lock(&files->file_lock);
2520120ce2b0SEric W. Biederman 		f = files_lookup_fd_locked(files, fd);
2521120ce2b0SEric W. Biederman 		spin_unlock(&files->file_lock);
25227f3697e2SJeff Layton 		if (f != filp) {
2523*73ae3495SJann Horn 			locks_remove_posix(filp, files);
25247f3697e2SJeff Layton 			error = -EBADF;
2525c293621bSPeter Staubach 		}
25267f3697e2SJeff Layton 	}
25271da177e4SLinus Torvalds out:
25281da177e4SLinus Torvalds 	locks_free_lock(file_lock);
25291da177e4SLinus Torvalds 	return error;
25301da177e4SLinus Torvalds }
25311da177e4SLinus Torvalds #endif /* BITS_PER_LONG == 32 */
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds /*
25341da177e4SLinus Torvalds  * This function is called when the file is being removed
25351da177e4SLinus Torvalds  * from the task's fd array.  POSIX locks belonging to this task
25361da177e4SLinus Torvalds  * are deleted at this time.
25371da177e4SLinus Torvalds  */
locks_remove_posix(struct file * filp,fl_owner_t owner)25381da177e4SLinus Torvalds void locks_remove_posix(struct file *filp, fl_owner_t owner)
25391da177e4SLinus Torvalds {
25401890910fSJeff Layton 	int error;
2541c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
2542ff7b86b8SMiklos Szeredi 	struct file_lock lock;
2543128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	/*
25461da177e4SLinus Torvalds 	 * If there are no locks held on this file, we don't need to call
25471da177e4SLinus Torvalds 	 * posix_lock_file().  Another process could be setting a lock on this
25481da177e4SLinus Torvalds 	 * file at the same time, but we wouldn't remove that lock anyway.
25491da177e4SLinus Torvalds 	 */
2550401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
2551bd61e0a9SJeff Layton 	if (!ctx || list_empty(&ctx->flc_posix))
25521da177e4SLinus Torvalds 		return;
25531da177e4SLinus Torvalds 
2554d6367d62SNeilBrown 	locks_init_lock(&lock);
25551da177e4SLinus Torvalds 	lock.fl_type = F_UNLCK;
255675e1fcc0SMiklos Szeredi 	lock.fl_flags = FL_POSIX | FL_CLOSE;
25571da177e4SLinus Torvalds 	lock.fl_start = 0;
25581da177e4SLinus Torvalds 	lock.fl_end = OFFSET_MAX;
25591da177e4SLinus Torvalds 	lock.fl_owner = owner;
25601da177e4SLinus Torvalds 	lock.fl_pid = current->tgid;
25611da177e4SLinus Torvalds 	lock.fl_file = filp;
25621da177e4SLinus Torvalds 	lock.fl_ops = NULL;
25631da177e4SLinus Torvalds 	lock.fl_lmops = NULL;
25641da177e4SLinus Torvalds 
25651890910fSJeff Layton 	error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	if (lock.fl_ops && lock.fl_ops->fl_release_private)
25681da177e4SLinus Torvalds 		lock.fl_ops->fl_release_private(&lock);
2569c568d683SMiklos Szeredi 	trace_locks_remove_posix(inode, &lock, error);
25701da177e4SLinus Torvalds }
25711da177e4SLinus Torvalds EXPORT_SYMBOL(locks_remove_posix);
25721da177e4SLinus Torvalds 
25733d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
2574dd459bb1SJeff Layton static void
locks_remove_flock(struct file * filp,struct file_lock_context * flctx)2575128a3785SDmitry Vyukov locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2576dd459bb1SJeff Layton {
2577d6367d62SNeilBrown 	struct file_lock fl;
2578c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
2579dd459bb1SJeff Layton 
25803d8e560dSJeff Layton 	if (list_empty(&flctx->flc_flock))
2581dd459bb1SJeff Layton 		return;
2582dd459bb1SJeff Layton 
25834149be7bSKuniyuki Iwashima 	flock_make_lock(filp, &fl, F_UNLCK);
2584d6367d62SNeilBrown 	fl.fl_flags |= FL_CLOSE;
2585d6367d62SNeilBrown 
2586de2a4a50SMiklos Szeredi 	if (filp->f_op->flock)
2587dd459bb1SJeff Layton 		filp->f_op->flock(filp, F_SETLKW, &fl);
2588dd459bb1SJeff Layton 	else
2589bcd7f78dSJeff Layton 		flock_lock_inode(inode, &fl);
2590dd459bb1SJeff Layton 
2591dd459bb1SJeff Layton 	if (fl.fl_ops && fl.fl_ops->fl_release_private)
2592dd459bb1SJeff Layton 		fl.fl_ops->fl_release_private(&fl);
2593dd459bb1SJeff Layton }
2594dd459bb1SJeff Layton 
25953d8e560dSJeff Layton /* The i_flctx must be valid when calling into here */
25968634b51fSJeff Layton static void
locks_remove_lease(struct file * filp,struct file_lock_context * ctx)2597128a3785SDmitry Vyukov locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
25988634b51fSJeff Layton {
25998634b51fSJeff Layton 	struct file_lock *fl, *tmp;
26008634b51fSJeff Layton 	LIST_HEAD(dispose);
26018634b51fSJeff Layton 
26023d8e560dSJeff Layton 	if (list_empty(&ctx->flc_lease))
26038634b51fSJeff Layton 		return;
26048634b51fSJeff Layton 
260502e525b2SPeter Zijlstra 	percpu_down_read(&file_rwsem);
26066109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
26078634b51fSJeff Layton 	list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
2608c4e136cdSJeff Layton 		if (filp == fl->fl_file)
26097448cc37SJeff Layton 			lease_modify(fl, F_UNLCK, &dispose);
26106109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
261102e525b2SPeter Zijlstra 	percpu_up_read(&file_rwsem);
26125f43086bSPeter Zijlstra 
26138634b51fSJeff Layton 	locks_dispose_list(&dispose);
26148634b51fSJeff Layton }
26158634b51fSJeff Layton 
26161da177e4SLinus Torvalds /*
26171da177e4SLinus Torvalds  * This function is called on the last close of an open file.
26181da177e4SLinus Torvalds  */
locks_remove_file(struct file * filp)261978ed8a13SJeff Layton void locks_remove_file(struct file *filp)
26201da177e4SLinus Torvalds {
2621128a3785SDmitry Vyukov 	struct file_lock_context *ctx;
2622128a3785SDmitry Vyukov 
2623c65454a9SJeff Layton 	ctx = locks_inode_context(file_inode(filp));
2624128a3785SDmitry Vyukov 	if (!ctx)
26253d8e560dSJeff Layton 		return;
26263d8e560dSJeff Layton 
2627dd459bb1SJeff Layton 	/* remove any OFD locks */
262873a8f5f7SChristoph Hellwig 	locks_remove_posix(filp, filp);
26295d50ffd7SJeff Layton 
2630dd459bb1SJeff Layton 	/* remove flock locks */
2631128a3785SDmitry Vyukov 	locks_remove_flock(filp, ctx);
2632dd459bb1SJeff Layton 
26338634b51fSJeff Layton 	/* remove any leases */
2634128a3785SDmitry Vyukov 	locks_remove_lease(filp, ctx);
26353953704fSBenjamin Coddington 
26363953704fSBenjamin Coddington 	spin_lock(&ctx->flc_lock);
26373953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
26383953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
26393953704fSBenjamin Coddington 	locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
26403953704fSBenjamin Coddington 	spin_unlock(&ctx->flc_lock);
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds /**
26449b9d2ab4SMarc Eshel  * vfs_cancel_lock - file byte range unblock lock
26459b9d2ab4SMarc Eshel  * @filp: The file to apply the unblock to
26469b9d2ab4SMarc Eshel  * @fl: The lock to be unblocked
26479b9d2ab4SMarc Eshel  *
26489b9d2ab4SMarc Eshel  * Used by lock managers to cancel blocked requests
26499b9d2ab4SMarc Eshel  */
vfs_cancel_lock(struct file * filp,struct file_lock * fl)26509b9d2ab4SMarc Eshel int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
26519b9d2ab4SMarc Eshel {
26527e8e5cc8SJeff Layton 	WARN_ON_ONCE(filp != fl->fl_file);
2653de2a4a50SMiklos Szeredi 	if (filp->f_op->lock)
26549b9d2ab4SMarc Eshel 		return filp->f_op->lock(filp, F_CANCELLK, fl);
26559b9d2ab4SMarc Eshel 	return 0;
26569b9d2ab4SMarc Eshel }
26579b9d2ab4SMarc Eshel EXPORT_SYMBOL_GPL(vfs_cancel_lock);
26589b9d2ab4SMarc Eshel 
2659ab1ddef9SJeff Layton /**
2660ab1ddef9SJeff Layton  * vfs_inode_has_locks - are any file locks held on @inode?
2661ab1ddef9SJeff Layton  * @inode: inode to check for locks
2662ab1ddef9SJeff Layton  *
2663ab1ddef9SJeff Layton  * Return true if there are any FL_POSIX or FL_FLOCK locks currently
2664ab1ddef9SJeff Layton  * set on @inode.
2665ab1ddef9SJeff Layton  */
vfs_inode_has_locks(struct inode * inode)2666ab1ddef9SJeff Layton bool vfs_inode_has_locks(struct inode *inode)
2667ab1ddef9SJeff Layton {
2668ab1ddef9SJeff Layton 	struct file_lock_context *ctx;
2669ab1ddef9SJeff Layton 	bool ret;
2670ab1ddef9SJeff Layton 
2671401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
2672ab1ddef9SJeff Layton 	if (!ctx)
2673ab1ddef9SJeff Layton 		return false;
2674ab1ddef9SJeff Layton 
2675ab1ddef9SJeff Layton 	spin_lock(&ctx->flc_lock);
2676ab1ddef9SJeff Layton 	ret = !list_empty(&ctx->flc_posix) || !list_empty(&ctx->flc_flock);
2677ab1ddef9SJeff Layton 	spin_unlock(&ctx->flc_lock);
2678ab1ddef9SJeff Layton 	return ret;
2679ab1ddef9SJeff Layton }
2680ab1ddef9SJeff Layton EXPORT_SYMBOL_GPL(vfs_inode_has_locks);
2681ab1ddef9SJeff Layton 
26827f8ada98SPavel Emelyanov #ifdef CONFIG_PROC_FS
2683d8ba7a36SAlexey Dobriyan #include <linux/proc_fs.h>
26847f8ada98SPavel Emelyanov #include <linux/seq_file.h>
26857f8ada98SPavel Emelyanov 
26867012b02aSJeff Layton struct locks_iterator {
26877012b02aSJeff Layton 	int	li_cpu;
26887012b02aSJeff Layton 	loff_t	li_pos;
26897012b02aSJeff Layton };
26907012b02aSJeff Layton 
lock_get_status(struct seq_file * f,struct file_lock * fl,loff_t id,char * pfx,int repeat)26917f8ada98SPavel Emelyanov static void lock_get_status(struct seq_file *f, struct file_lock *fl,
2692b8da9b10SLuo Longjun 			    loff_t id, char *pfx, int repeat)
26931da177e4SLinus Torvalds {
26941da177e4SLinus Torvalds 	struct inode *inode = NULL;
2695ab1f1611SVitaliy Gusev 	unsigned int fl_pid;
26969d78edeaSAlexey Gladkov 	struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
269790f7d7a0SJeff Layton 	int type;
2698d67fd44fSNikolay Borisov 
26999d5b86acSBenjamin Coddington 	fl_pid = locks_translate_pid(fl, proc_pidns);
2700d67fd44fSNikolay Borisov 	/*
27011cf8e5deSKonstantin Khorenko 	 * If lock owner is dead (and pid is freed) or not visible in current
27021cf8e5deSKonstantin Khorenko 	 * pidns, zero is shown as a pid value. Check lock info from
27031cf8e5deSKonstantin Khorenko 	 * init_pid_ns to get saved lock pid value.
2704d67fd44fSNikolay Borisov 	 */
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	if (fl->fl_file != NULL)
2707c65454a9SJeff Layton 		inode = file_inode(fl->fl_file);
27081da177e4SLinus Torvalds 
2709b8da9b10SLuo Longjun 	seq_printf(f, "%lld: ", id);
2710b8da9b10SLuo Longjun 
2711b8da9b10SLuo Longjun 	if (repeat)
2712b8da9b10SLuo Longjun 		seq_printf(f, "%*s", repeat - 1 + (int)strlen(pfx), pfx);
2713b8da9b10SLuo Longjun 
27141da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
2715c918d42aSJeff Layton 		if (fl->fl_flags & FL_ACCESS)
27165315c26aSFabian Frederick 			seq_puts(f, "ACCESS");
2717cff2fce5SJeff Layton 		else if (IS_OFDLCK(fl))
27185315c26aSFabian Frederick 			seq_puts(f, "OFDLCK");
2719c918d42aSJeff Layton 		else
27205315c26aSFabian Frederick 			seq_puts(f, "POSIX ");
2721c918d42aSJeff Layton 
2722c918d42aSJeff Layton 		seq_printf(f, " %s ",
2723f7e33bdbSJeff Layton 			     (inode == NULL) ? "*NOINODE*" : "ADVISORY ");
27241da177e4SLinus Torvalds 	} else if (IS_FLOCK(fl)) {
27255315c26aSFabian Frederick 		seq_puts(f, "FLOCK  ADVISORY  ");
27261da177e4SLinus Torvalds 	} else if (IS_LEASE(fl)) {
27278144f1f6SJeff Layton 		if (fl->fl_flags & FL_DELEG)
27288144f1f6SJeff Layton 			seq_puts(f, "DELEG  ");
27298144f1f6SJeff Layton 		else
27305315c26aSFabian Frederick 			seq_puts(f, "LEASE  ");
27318144f1f6SJeff Layton 
2732ab83fa4bSJ. Bruce Fields 		if (lease_breaking(fl))
27335315c26aSFabian Frederick 			seq_puts(f, "BREAKING  ");
27341da177e4SLinus Torvalds 		else if (fl->fl_file)
27355315c26aSFabian Frederick 			seq_puts(f, "ACTIVE    ");
27361da177e4SLinus Torvalds 		else
27375315c26aSFabian Frederick 			seq_puts(f, "BREAKER   ");
27381da177e4SLinus Torvalds 	} else {
27395315c26aSFabian Frederick 		seq_puts(f, "UNKNOWN UNKNOWN  ");
27401da177e4SLinus Torvalds 	}
274190f7d7a0SJeff Layton 	type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type;
274243e4cb94SPavel Begunkov 
274343e4cb94SPavel Begunkov 	seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" :
274443e4cb94SPavel Begunkov 			     (type == F_RDLCK) ? "READ" : "UNLCK");
27451da177e4SLinus Torvalds 	if (inode) {
27463648888eSJeff Layton 		/* userspace relies on this representation of dev_t */
274798ca480aSAmir Goldstein 		seq_printf(f, "%d %02x:%02x:%lu ", fl_pid,
27481da177e4SLinus Torvalds 				MAJOR(inode->i_sb->s_dev),
27491da177e4SLinus Torvalds 				MINOR(inode->i_sb->s_dev), inode->i_ino);
27501da177e4SLinus Torvalds 	} else {
2751ab1f1611SVitaliy Gusev 		seq_printf(f, "%d <none>:0 ", fl_pid);
27521da177e4SLinus Torvalds 	}
27531da177e4SLinus Torvalds 	if (IS_POSIX(fl)) {
27541da177e4SLinus Torvalds 		if (fl->fl_end == OFFSET_MAX)
27557f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld EOF\n", fl->fl_start);
27561da177e4SLinus Torvalds 		else
27577f8ada98SPavel Emelyanov 			seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
27581da177e4SLinus Torvalds 	} else {
27595315c26aSFabian Frederick 		seq_puts(f, "0 EOF\n");
27601da177e4SLinus Torvalds 	}
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds 
get_next_blocked_member(struct file_lock * node)2763b8da9b10SLuo Longjun static struct file_lock *get_next_blocked_member(struct file_lock *node)
2764b8da9b10SLuo Longjun {
2765b8da9b10SLuo Longjun 	struct file_lock *tmp;
2766b8da9b10SLuo Longjun 
2767b8da9b10SLuo Longjun 	/* NULL node or root node */
2768b8da9b10SLuo Longjun 	if (node == NULL || node->fl_blocker == NULL)
2769b8da9b10SLuo Longjun 		return NULL;
2770b8da9b10SLuo Longjun 
2771b8da9b10SLuo Longjun 	/* Next member in the linked list could be itself */
2772b8da9b10SLuo Longjun 	tmp = list_next_entry(node, fl_blocked_member);
2773b8da9b10SLuo Longjun 	if (list_entry_is_head(tmp, &node->fl_blocker->fl_blocked_requests, fl_blocked_member)
2774b8da9b10SLuo Longjun 		|| tmp == node) {
2775b8da9b10SLuo Longjun 		return NULL;
2776b8da9b10SLuo Longjun 	}
2777b8da9b10SLuo Longjun 
2778b8da9b10SLuo Longjun 	return tmp;
2779b8da9b10SLuo Longjun }
2780b8da9b10SLuo Longjun 
locks_show(struct seq_file * f,void * v)27817f8ada98SPavel Emelyanov static int locks_show(struct seq_file *f, void *v)
27821da177e4SLinus Torvalds {
27837012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
2784b8da9b10SLuo Longjun 	struct file_lock *cur, *tmp;
27859d78edeaSAlexey Gladkov 	struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
2786b8da9b10SLuo Longjun 	int level = 0;
27877f8ada98SPavel Emelyanov 
2788b8da9b10SLuo Longjun 	cur = hlist_entry(v, struct file_lock, fl_link);
27897f8ada98SPavel Emelyanov 
2790b8da9b10SLuo Longjun 	if (locks_translate_pid(cur, proc_pidns) == 0)
2791d67fd44fSNikolay Borisov 		return 0;
2792d67fd44fSNikolay Borisov 
2793b8da9b10SLuo Longjun 	/* View this crossed linked list as a binary tree, the first member of fl_blocked_requests
2794b8da9b10SLuo Longjun 	 * is the left child of current node, the next silibing in fl_blocked_member is the
2795b8da9b10SLuo Longjun 	 * right child, we can alse get the parent of current node from fl_blocker, so this
2796b8da9b10SLuo Longjun 	 * question becomes traversal of a binary tree
2797b8da9b10SLuo Longjun 	 */
2798b8da9b10SLuo Longjun 	while (cur != NULL) {
2799b8da9b10SLuo Longjun 		if (level)
2800b8da9b10SLuo Longjun 			lock_get_status(f, cur, iter->li_pos, "-> ", level);
2801b8da9b10SLuo Longjun 		else
2802b8da9b10SLuo Longjun 			lock_get_status(f, cur, iter->li_pos, "", level);
28037f8ada98SPavel Emelyanov 
2804b8da9b10SLuo Longjun 		if (!list_empty(&cur->fl_blocked_requests)) {
2805b8da9b10SLuo Longjun 			/* Turn left */
2806b8da9b10SLuo Longjun 			cur = list_first_entry_or_null(&cur->fl_blocked_requests,
2807b8da9b10SLuo Longjun 				struct file_lock, fl_blocked_member);
2808b8da9b10SLuo Longjun 			level++;
2809b8da9b10SLuo Longjun 		} else {
2810b8da9b10SLuo Longjun 			/* Turn right */
2811b8da9b10SLuo Longjun 			tmp = get_next_blocked_member(cur);
2812b8da9b10SLuo Longjun 			/* Fall back to parent node */
2813b8da9b10SLuo Longjun 			while (tmp == NULL && cur->fl_blocker != NULL) {
2814b8da9b10SLuo Longjun 				cur = cur->fl_blocker;
2815b8da9b10SLuo Longjun 				level--;
2816b8da9b10SLuo Longjun 				tmp = get_next_blocked_member(cur);
2817b8da9b10SLuo Longjun 			}
2818b8da9b10SLuo Longjun 			cur = tmp;
2819b8da9b10SLuo Longjun 		}
2820b8da9b10SLuo Longjun 	}
28217f8ada98SPavel Emelyanov 
28227f8ada98SPavel Emelyanov 	return 0;
28231da177e4SLinus Torvalds }
28241da177e4SLinus Torvalds 
__show_fd_locks(struct seq_file * f,struct list_head * head,int * id,struct file * filp,struct files_struct * files)28256c8c9031SAndrey Vagin static void __show_fd_locks(struct seq_file *f,
28266c8c9031SAndrey Vagin 			struct list_head *head, int *id,
28276c8c9031SAndrey Vagin 			struct file *filp, struct files_struct *files)
28286c8c9031SAndrey Vagin {
28296c8c9031SAndrey Vagin 	struct file_lock *fl;
28306c8c9031SAndrey Vagin 
28316c8c9031SAndrey Vagin 	list_for_each_entry(fl, head, fl_list) {
28326c8c9031SAndrey Vagin 
28336c8c9031SAndrey Vagin 		if (filp != fl->fl_file)
28346c8c9031SAndrey Vagin 			continue;
28356c8c9031SAndrey Vagin 		if (fl->fl_owner != files &&
28366c8c9031SAndrey Vagin 		    fl->fl_owner != filp)
28376c8c9031SAndrey Vagin 			continue;
28386c8c9031SAndrey Vagin 
28396c8c9031SAndrey Vagin 		(*id)++;
28406c8c9031SAndrey Vagin 		seq_puts(f, "lock:\t");
2841b8da9b10SLuo Longjun 		lock_get_status(f, fl, *id, "", 0);
28426c8c9031SAndrey Vagin 	}
28436c8c9031SAndrey Vagin }
28446c8c9031SAndrey Vagin 
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)28456c8c9031SAndrey Vagin void show_fd_locks(struct seq_file *f,
28466c8c9031SAndrey Vagin 		  struct file *filp, struct files_struct *files)
28476c8c9031SAndrey Vagin {
2848c65454a9SJeff Layton 	struct inode *inode = file_inode(filp);
28496c8c9031SAndrey Vagin 	struct file_lock_context *ctx;
28506c8c9031SAndrey Vagin 	int id = 0;
28516c8c9031SAndrey Vagin 
2852401a8b8fSJeff Layton 	ctx = locks_inode_context(inode);
28536c8c9031SAndrey Vagin 	if (!ctx)
28546c8c9031SAndrey Vagin 		return;
28556c8c9031SAndrey Vagin 
28566c8c9031SAndrey Vagin 	spin_lock(&ctx->flc_lock);
28576c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
28586c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
28596c8c9031SAndrey Vagin 	__show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
28606c8c9031SAndrey Vagin 	spin_unlock(&ctx->flc_lock);
28616c8c9031SAndrey Vagin }
28626c8c9031SAndrey Vagin 
locks_start(struct seq_file * f,loff_t * pos)28637f8ada98SPavel Emelyanov static void *locks_start(struct seq_file *f, loff_t *pos)
2864b03dfdecSJeff Layton 	__acquires(&blocked_lock_lock)
28651da177e4SLinus Torvalds {
28667012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
286799dc8292SJerome Marchand 
28687012b02aSJeff Layton 	iter->li_pos = *pos + 1;
2869aba37660SPeter Zijlstra 	percpu_down_write(&file_rwsem);
28707b2296afSJeff Layton 	spin_lock(&blocked_lock_lock);
28717c3f654dSPeter Zijlstra 	return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
28721da177e4SLinus Torvalds }
28737f8ada98SPavel Emelyanov 
locks_next(struct seq_file * f,void * v,loff_t * pos)28747f8ada98SPavel Emelyanov static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
28757f8ada98SPavel Emelyanov {
28767012b02aSJeff Layton 	struct locks_iterator *iter = f->private;
28777012b02aSJeff Layton 
28787012b02aSJeff Layton 	++iter->li_pos;
28797c3f654dSPeter Zijlstra 	return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
28801da177e4SLinus Torvalds }
28817f8ada98SPavel Emelyanov 
locks_stop(struct seq_file * f,void * v)28827f8ada98SPavel Emelyanov static void locks_stop(struct seq_file *f, void *v)
2883b03dfdecSJeff Layton 	__releases(&blocked_lock_lock)
28847f8ada98SPavel Emelyanov {
28857b2296afSJeff Layton 	spin_unlock(&blocked_lock_lock);
2886aba37660SPeter Zijlstra 	percpu_up_write(&file_rwsem);
28871da177e4SLinus Torvalds }
28881da177e4SLinus Torvalds 
2889d8ba7a36SAlexey Dobriyan static const struct seq_operations locks_seq_operations = {
28907f8ada98SPavel Emelyanov 	.start	= locks_start,
28917f8ada98SPavel Emelyanov 	.next	= locks_next,
28927f8ada98SPavel Emelyanov 	.stop	= locks_stop,
28937f8ada98SPavel Emelyanov 	.show	= locks_show,
28947f8ada98SPavel Emelyanov };
2895d8ba7a36SAlexey Dobriyan 
proc_locks_init(void)2896d8ba7a36SAlexey Dobriyan static int __init proc_locks_init(void)
2897d8ba7a36SAlexey Dobriyan {
289844414d82SChristoph Hellwig 	proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
289944414d82SChristoph Hellwig 			sizeof(struct locks_iterator), NULL);
2900d8ba7a36SAlexey Dobriyan 	return 0;
2901d8ba7a36SAlexey Dobriyan }
290291899226SPaul Gortmaker fs_initcall(proc_locks_init);
29037f8ada98SPavel Emelyanov #endif
29047f8ada98SPavel Emelyanov 
filelock_init(void)29051da177e4SLinus Torvalds static int __init filelock_init(void)
29061da177e4SLinus Torvalds {
29077012b02aSJeff Layton 	int i;
29087012b02aSJeff Layton 
29094a075e39SJeff Layton 	flctx_cache = kmem_cache_create("file_lock_ctx",
29103754707bSLinus Torvalds 			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
29114a075e39SJeff Layton 
29121da177e4SLinus Torvalds 	filelock_cache = kmem_cache_create("file_lock_cache",
29133754707bSLinus Torvalds 			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2914ee19cc40SMiklos Szeredi 
29157c3f654dSPeter Zijlstra 	for_each_possible_cpu(i) {
29167c3f654dSPeter Zijlstra 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
29177c3f654dSPeter Zijlstra 
29187c3f654dSPeter Zijlstra 		spin_lock_init(&fll->lock);
29197c3f654dSPeter Zijlstra 		INIT_HLIST_HEAD(&fll->hlist);
29207c3f654dSPeter Zijlstra 	}
29217012b02aSJeff Layton 
292218f6622eSJeff Layton 	lease_notifier_chain_init();
29231da177e4SLinus Torvalds 	return 0;
29241da177e4SLinus Torvalds }
29251da177e4SLinus Torvalds core_initcall(filelock_init);
2926