blob: 1260c265ba62d24443816b7d8a6f59be50810f88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/locks.c
3 *
4 * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
5 * Doug Evans (dje@spiff.uucp), August 07, 1992
6 *
7 * Deadlock detection added.
8 * FIXME: one thing isn't handled yet:
9 * - mandatory locks (requires lots of changes elsewhere)
10 * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
11 *
12 * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
13 * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
14 *
15 * Converted file_lock_table to a linked list from an array, which eliminates
16 * the limits on how many active file locks are open.
17 * Chad Page (pageone@netcom.com), November 27, 1994
18 *
19 * Removed dependency on file descriptors. dup()'ed file descriptors now
20 * get the same locks as the original file descriptors, and a close() on
21 * any file descriptor removes ALL the locks on the file for the current
22 * process. Since locks still depend on the process id, locks are inherited
23 * after an exec() but not after a fork(). This agrees with POSIX, and both
24 * BSD and SVR4 practice.
25 * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
26 *
27 * Scrapped free list which is redundant now that we allocate locks
28 * dynamically with kmalloc()/kfree().
29 * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
30 *
31 * Implemented two lock personalities - FL_FLOCK and FL_POSIX.
32 *
33 * FL_POSIX locks are created with calls to fcntl() and lockf() through the
34 * fcntl() system call. They have the semantics described above.
35 *
36 * FL_FLOCK locks are created with calls to flock(), through the flock()
37 * system call, which is new. Old C libraries implement flock() via fcntl()
38 * and will continue to use the old, broken implementation.
39 *
40 * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
41 * with a file pointer (filp). As a result they can be shared by a parent
42 * process and its children after a fork(). They are removed when the last
43 * file descriptor referring to the file pointer is closed (unless explicitly
44 * unlocked).
45 *
46 * FL_FLOCK locks never deadlock, an existing lock is always removed before
47 * upgrading from shared to exclusive (or vice versa). When this happens
48 * any processes blocked by the current lock are woken up and allowed to
49 * run before the new lock is applied.
50 * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
51 *
52 * Removed some race conditions in flock_lock_file(), marked other possible
53 * races. Just grep for FIXME to see them.
54 * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
55 *
56 * Addressed Dmitry's concerns. Deadlock checking no longer recursive.
57 * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
58 * once we've checked for blocking and deadlocking.
59 * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
60 *
61 * Initial implementation of mandatory locks. SunOS turned out to be
62 * a rotten model, so I implemented the "obvious" semantics.
Paul Bolle395cf962011-08-15 02:02:26 +020063 * See 'Documentation/filesystems/mandatory-locking.txt' for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
65 *
66 * Don't allow mandatory locks on mmap()'ed files. Added simple functions to
67 * check if a file has mandatory locks, used by mmap(), open() and creat() to
68 * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
69 * Manual, Section 2.
70 * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
71 *
72 * Tidied up block list handling. Added '/proc/locks' interface.
73 * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
74 *
75 * Fixed deadlock condition for pathological code that mixes calls to
76 * flock() and fcntl().
77 * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
78 *
79 * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
80 * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
81 * guarantee sensible behaviour in the case where file system modules might
82 * be compiled with different options than the kernel itself.
83 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
84 *
85 * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
86 * (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
87 * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
88 *
89 * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
90 * locks. Changed process synchronisation to avoid dereferencing locks that
91 * have already been freed.
92 * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
93 *
94 * Made the block list a circular list to minimise searching in the list.
95 * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
96 *
97 * Made mandatory locking a mount option. Default is not to allow mandatory
98 * locking.
99 * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
100 *
101 * Some adaptations for NFS support.
102 * Olaf Kirch (okir@monad.swb.de), Dec 1996,
103 *
104 * Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
105 * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
106 *
107 * Use slab allocator instead of kmalloc/kfree.
108 * Use generic list implementation from <linux/list.h>.
109 * Sped up posix_locks_deadlock by only considering blocked locks.
110 * Matthew Wilcox <willy@debian.org>, March, 2000.
111 *
112 * Leases and LOCK_MAND
113 * Matthew Wilcox <willy@debian.org>, June, 2000.
114 * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
115 */
116
117#include <linux/capability.h>
118#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -0400119#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/fs.h>
121#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/security.h>
123#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/syscalls.h>
125#include <linux/time.h>
Dipankar Sarma4fb3a532005-09-16 19:28:13 -0700126#include <linux/rcupdate.h>
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000127#include <linux/pid_namespace.h>
Jeff Layton48f74182013-06-21 08:58:18 -0400128#include <linux/hashtable.h>
Jeff Layton7012b022013-06-21 08:58:22 -0400129#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Layton62af4f12014-05-09 14:13:05 -0400131#define CREATE_TRACE_POINTS
132#include <trace/events/filelock.h>
133
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800134#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136#define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
137#define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
Christoph Hellwig11afe9f2015-01-21 19:17:03 +0100138#define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400139#define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -0400140#define IS_REMOTELCK(fl) (fl->fl_pid <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -0400142static bool lease_breaking(struct file_lock *fl)
143{
J. Bruce Fields778fc542011-07-26 18:25:49 -0400144 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
145}
146
147static int target_leasetype(struct file_lock *fl)
148{
149 if (fl->fl_flags & FL_UNLOCK_PENDING)
150 return F_UNLCK;
151 if (fl->fl_flags & FL_DOWNGRADE_PENDING)
152 return F_RDLCK;
153 return fl->fl_type;
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -0400154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156int leases_enable = 1;
157int lease_break_time = 45;
158
Jeff Layton1c8c6012013-06-21 08:58:15 -0400159/*
Jeff Layton7012b022013-06-21 08:58:22 -0400160 * The global file_lock_list is only used for displaying /proc/locks, so we
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200161 * keep a list on each CPU, with each list protected by its own spinlock.
162 * Global serialization is done using file_rwsem.
163 *
164 * Note that alterations to the list also require that the relevant flc_lock is
165 * held.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400166 */
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200167struct file_lock_list_struct {
168 spinlock_t lock;
169 struct hlist_head hlist;
170};
171static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
Peter Zijlstraaba37662015-06-22 14:16:33 +0200172DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
Jeff Layton88974692013-06-21 08:58:14 -0400173
Jeff Layton1c8c6012013-06-21 08:58:15 -0400174/*
Jeff Layton48f74182013-06-21 08:58:18 -0400175 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
Jeff Layton7b2296a2013-06-21 08:58:20 -0400176 * It is protected by blocked_lock_lock.
Jeff Layton48f74182013-06-21 08:58:18 -0400177 *
178 * We hash locks by lockowner in order to optimize searching for the lock a
179 * particular lockowner is waiting on.
180 *
181 * FIXME: make this value scale via some heuristic? We generally will want more
182 * buckets when we have more lockowners holding locks, but that's a little
183 * difficult to determine without knowing what the workload will look like.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400184 */
Jeff Layton48f74182013-06-21 08:58:18 -0400185#define BLOCKED_HASH_BITS 7
186static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
Jeff Layton88974692013-06-21 08:58:14 -0400187
Jeff Layton1c8c6012013-06-21 08:58:15 -0400188/*
Jeff Layton7b2296a2013-06-21 08:58:20 -0400189 * This lock protects the blocked_hash. Generally, if you're accessing it, you
190 * want to be holding this lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400191 *
NeilBrownada5c1d2018-11-30 10:04:08 +1100192 * In addition, it also protects the fl->fl_blocked_requests list, and the
193 * fl->fl_blocker pointer for file_lock structures that are acting as lock
194 * requests (in contrast to those that are acting as records of acquired locks).
Jeff Layton1c8c6012013-06-21 08:58:15 -0400195 *
196 * Note that when we acquire this lock in order to change the above fields,
Jeff Layton6109c852015-01-16 15:05:57 -0500197 * we often hold the flc_lock as well. In certain cases, when reading the fields
Jeff Layton1c8c6012013-06-21 08:58:15 -0400198 * protected by this lock, we can skip acquiring it iff we already hold the
Jeff Layton6109c852015-01-16 15:05:57 -0500199 * flc_lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400200 */
Jeff Layton7b2296a2013-06-21 08:58:20 -0400201static DEFINE_SPINLOCK(blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jeff Layton4a075e32015-01-16 15:05:54 -0500203static struct kmem_cache *flctx_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -0800204static struct kmem_cache *filelock_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jeff Layton4a075e32015-01-16 15:05:54 -0500206static struct file_lock_context *
Jeff Layton5c1c6692015-04-03 09:04:03 -0400207locks_get_lock_context(struct inode *inode, int type)
Jeff Layton4a075e32015-01-16 15:05:54 -0500208{
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200209 struct file_lock_context *ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500210
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200211 /* paired with cmpxchg() below */
212 ctx = smp_load_acquire(&inode->i_flctx);
213 if (likely(ctx) || type == F_UNLCK)
Jeff Layton4a075e32015-01-16 15:05:54 -0500214 goto out;
215
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200216 ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
217 if (!ctx)
Jeff Layton4a075e32015-01-16 15:05:54 -0500218 goto out;
219
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200220 spin_lock_init(&ctx->flc_lock);
221 INIT_LIST_HEAD(&ctx->flc_flock);
222 INIT_LIST_HEAD(&ctx->flc_posix);
223 INIT_LIST_HEAD(&ctx->flc_lease);
Jeff Layton4a075e32015-01-16 15:05:54 -0500224
225 /*
226 * Assign the pointer if it's not already assigned. If it is, then
227 * free the context we just allocated.
228 */
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200229 if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
230 kmem_cache_free(flctx_cache, ctx);
231 ctx = smp_load_acquire(&inode->i_flctx);
232 }
Jeff Layton4a075e32015-01-16 15:05:54 -0500233out:
Jeff Layton18909102016-01-06 21:26:10 -0500234 trace_locks_get_lock_context(inode, type, ctx);
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200235 return ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500236}
237
Jeff Laytone24dada2016-01-06 21:28:41 -0500238static void
239locks_dump_ctx_list(struct list_head *list, char *list_type)
240{
241 struct file_lock *fl;
242
243 list_for_each_entry(fl, list, fl_list) {
244 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);
245 }
246}
247
248static void
249locks_check_ctx_lists(struct inode *inode)
250{
251 struct file_lock_context *ctx = inode->i_flctx;
252
253 if (unlikely(!list_empty(&ctx->flc_flock) ||
254 !list_empty(&ctx->flc_posix) ||
255 !list_empty(&ctx->flc_lease))) {
256 pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
257 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
258 inode->i_ino);
259 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
260 locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
261 locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
262 }
263}
264
Benjamin Coddington39537042017-07-21 13:36:25 -0400265static void
266locks_check_ctx_file_list(struct file *filp, struct list_head *list,
267 char *list_type)
268{
269 struct file_lock *fl;
270 struct inode *inode = locks_inode(filp);
271
272 list_for_each_entry(fl, list, fl_list)
273 if (fl->fl_file == filp)
274 pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
275 " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
276 list_type, MAJOR(inode->i_sb->s_dev),
277 MINOR(inode->i_sb->s_dev), inode->i_ino,
278 fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
279}
280
Jeff Layton4a075e32015-01-16 15:05:54 -0500281void
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500282locks_free_lock_context(struct inode *inode)
Jeff Layton4a075e32015-01-16 15:05:54 -0500283{
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500284 struct file_lock_context *ctx = inode->i_flctx;
285
Jeff Laytone24dada2016-01-06 21:28:41 -0500286 if (unlikely(ctx)) {
287 locks_check_ctx_lists(inode);
Jeff Layton4a075e32015-01-16 15:05:54 -0500288 kmem_cache_free(flctx_cache, ctx);
289 }
290}
291
Miklos Szerediee19cc42011-07-07 13:06:09 +0200292static void locks_init_lock_heads(struct file_lock *fl)
Miklos Szeredia51cb912011-07-06 12:33:55 +0200293{
Jeff Layton139ca042013-06-21 08:58:17 -0400294 INIT_HLIST_NODE(&fl->fl_link);
Jeff Layton6dee60f2015-01-16 15:05:54 -0500295 INIT_LIST_HEAD(&fl->fl_list);
NeilBrownada5c1d2018-11-30 10:04:08 +1100296 INIT_LIST_HEAD(&fl->fl_blocked_requests);
297 INIT_LIST_HEAD(&fl->fl_blocked_member);
Miklos Szerediee19cc42011-07-07 13:06:09 +0200298 init_waitqueue_head(&fl->fl_wait);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* Allocate an empty lock structure. */
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200302struct file_lock *locks_alloc_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200304 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200305
306 if (fl)
Miklos Szerediee19cc42011-07-07 13:06:09 +0200307 locks_init_lock_heads(fl);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200308
309 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200311EXPORT_SYMBOL_GPL(locks_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500313void locks_release_private(struct file_lock *fl)
Trond Myklebust47831f32006-03-20 13:44:05 -0500314{
315 if (fl->fl_ops) {
316 if (fl->fl_ops->fl_release_private)
317 fl->fl_ops->fl_release_private(fl);
318 fl->fl_ops = NULL;
319 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500320
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400321 if (fl->fl_lmops) {
Jeff Laytoncae80b32015-04-03 09:04:04 -0400322 if (fl->fl_lmops->lm_put_owner) {
323 fl->fl_lmops->lm_put_owner(fl->fl_owner);
324 fl->fl_owner = NULL;
325 }
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400326 fl->fl_lmops = NULL;
327 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500328}
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500329EXPORT_SYMBOL_GPL(locks_release_private);
Trond Myklebust47831f32006-03-20 13:44:05 -0500330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/* Free a lock which is not in use. */
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400332void locks_free_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Miklos Szeredi5ce29642006-03-31 02:30:29 -0800334 BUG_ON(waitqueue_active(&fl->fl_wait));
Jeff Layton6dee60f2015-01-16 15:05:54 -0500335 BUG_ON(!list_empty(&fl->fl_list));
NeilBrownada5c1d2018-11-30 10:04:08 +1100336 BUG_ON(!list_empty(&fl->fl_blocked_requests));
337 BUG_ON(!list_empty(&fl->fl_blocked_member));
Jeff Layton139ca042013-06-21 08:58:17 -0400338 BUG_ON(!hlist_unhashed(&fl->fl_link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Trond Myklebust47831f32006-03-20 13:44:05 -0500340 locks_release_private(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 kmem_cache_free(filelock_cache, fl);
342}
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400343EXPORT_SYMBOL(locks_free_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Jeff Laytoned9814d2014-08-11 14:20:31 -0400345static void
346locks_dispose_list(struct list_head *dispose)
347{
348 struct file_lock *fl;
349
350 while (!list_empty(dispose)) {
Jeff Layton6dee60f2015-01-16 15:05:54 -0500351 fl = list_first_entry(dispose, struct file_lock, fl_list);
352 list_del_init(&fl->fl_list);
Jeff Laytoned9814d2014-08-11 14:20:31 -0400353 locks_free_lock(fl);
354 }
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357void locks_init_lock(struct file_lock *fl)
358{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200359 memset(fl, 0, sizeof(struct file_lock));
360 locks_init_lock_heads(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363EXPORT_SYMBOL(locks_init_lock);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
366 * Initialize a new lock from an existing file_lock structure.
367 */
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400368void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 new->fl_owner = fl->fl_owner;
371 new->fl_pid = fl->fl_pid;
Trond Myklebust09969052006-03-20 13:44:38 -0500372 new->fl_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 new->fl_flags = fl->fl_flags;
374 new->fl_type = fl->fl_type;
375 new->fl_start = fl->fl_start;
376 new->fl_end = fl->fl_end;
Kinglong Meef3282962014-08-22 10:18:43 -0400377 new->fl_lmops = fl->fl_lmops;
Trond Myklebust09969052006-03-20 13:44:38 -0500378 new->fl_ops = NULL;
Kinglong Meef3282962014-08-22 10:18:43 -0400379
380 if (fl->fl_lmops) {
381 if (fl->fl_lmops->lm_get_owner)
Jeff Laytoncae80b32015-04-03 09:04:04 -0400382 fl->fl_lmops->lm_get_owner(fl->fl_owner);
Kinglong Meef3282962014-08-22 10:18:43 -0400383 }
Trond Myklebust09969052006-03-20 13:44:38 -0500384}
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400385EXPORT_SYMBOL(locks_copy_conflock);
Trond Myklebust09969052006-03-20 13:44:38 -0500386
387void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
388{
Jeff Layton566709b2014-08-11 14:09:35 -0400389 /* "new" must be a freshly-initialized lock */
390 WARN_ON_ONCE(new->fl_ops);
Trond Myklebust09969052006-03-20 13:44:38 -0500391
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400392 locks_copy_conflock(new, fl);
Kinglong Meef3282962014-08-22 10:18:43 -0400393
Trond Myklebust09969052006-03-20 13:44:38 -0500394 new->fl_file = fl->fl_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 new->fl_ops = fl->fl_ops;
Trond Myklebust47831f32006-03-20 13:44:05 -0500396
Kinglong Meef3282962014-08-22 10:18:43 -0400397 if (fl->fl_ops) {
398 if (fl->fl_ops->fl_copy_lock)
399 fl->fl_ops->fl_copy_lock(new, fl);
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403EXPORT_SYMBOL(locks_copy_lock);
404
NeilBrown5946c432018-11-30 10:04:08 +1100405static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
406{
407 struct file_lock *f;
408
409 /*
410 * As ctx->flc_lock is held, new requests cannot be added to
411 * ->fl_blocked_requests, so we don't need a lock to check if it
412 * is empty.
413 */
414 if (list_empty(&fl->fl_blocked_requests))
415 return;
416 spin_lock(&blocked_lock_lock);
417 list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
418 list_for_each_entry(f, &fl->fl_blocked_requests, fl_blocked_member)
419 f->fl_blocker = new;
420 spin_unlock(&blocked_lock_lock);
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static inline int flock_translate_cmd(int cmd) {
424 if (cmd & LOCK_MAND)
425 return cmd & (LOCK_MAND | LOCK_RW);
426 switch (cmd) {
427 case LOCK_SH:
428 return F_RDLCK;
429 case LOCK_EX:
430 return F_WRLCK;
431 case LOCK_UN:
432 return F_UNLCK;
433 }
434 return -EINVAL;
435}
436
437/* Fill in a file_lock structure with an appropriate FLOCK lock. */
Jeff Layton6e129d002014-09-04 10:25:06 -0400438static struct file_lock *
NeilBrownd6367d62018-11-30 10:04:08 +1100439flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int type = flock_translate_cmd(cmd);
Jeff Layton6e129d002014-09-04 10:25:06 -0400442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (type < 0)
Jeff Layton6e129d002014-09-04 10:25:06 -0400444 return ERR_PTR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
NeilBrownd6367d62018-11-30 10:04:08 +1100446 if (fl == NULL) {
447 fl = locks_alloc_lock();
448 if (fl == NULL)
449 return ERR_PTR(-ENOMEM);
450 } else {
451 locks_init_lock(fl);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 fl->fl_file = filp;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +0200455 fl->fl_owner = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 fl->fl_pid = current->tgid;
457 fl->fl_flags = FL_FLOCK;
458 fl->fl_type = type;
459 fl->fl_end = OFFSET_MAX;
460
Jeff Layton6e129d002014-09-04 10:25:06 -0400461 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400464static int assign_type(struct file_lock *fl, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 switch (type) {
467 case F_RDLCK:
468 case F_WRLCK:
469 case F_UNLCK:
470 fl->fl_type = type;
471 break;
472 default:
473 return -EINVAL;
474 }
475 return 0;
476}
477
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500478static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
479 struct flock64 *l)
480{
481 switch (l->l_whence) {
482 case SEEK_SET:
483 fl->fl_start = 0;
484 break;
485 case SEEK_CUR:
486 fl->fl_start = filp->f_pos;
487 break;
488 case SEEK_END:
489 fl->fl_start = i_size_read(file_inode(filp));
490 break;
491 default:
492 return -EINVAL;
493 }
494 if (l->l_start > OFFSET_MAX - fl->fl_start)
495 return -EOVERFLOW;
496 fl->fl_start += l->l_start;
497 if (fl->fl_start < 0)
498 return -EINVAL;
499
500 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
501 POSIX-2001 defines it. */
502 if (l->l_len > 0) {
503 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
504 return -EOVERFLOW;
505 fl->fl_end = fl->fl_start + l->l_len - 1;
506
507 } else if (l->l_len < 0) {
508 if (fl->fl_start + l->l_len < 0)
509 return -EINVAL;
510 fl->fl_end = fl->fl_start - 1;
511 fl->fl_start += l->l_len;
512 } else
513 fl->fl_end = OFFSET_MAX;
514
515 fl->fl_owner = current->files;
516 fl->fl_pid = current->tgid;
517 fl->fl_file = filp;
518 fl->fl_flags = FL_POSIX;
519 fl->fl_ops = NULL;
520 fl->fl_lmops = NULL;
521
522 return assign_type(fl, l->l_type);
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
526 * style lock.
527 */
528static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
529 struct flock *l)
530{
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500531 struct flock64 ll = {
532 .l_type = l->l_type,
533 .l_whence = l->l_whence,
534 .l_start = l->l_start,
535 .l_len = l->l_len,
536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500538 return flock64_to_posix_lock(filp, fl, &ll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/* default lease lock manager operations */
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400542static bool
543lease_break_callback(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400546 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400549static void
550lease_setup(struct file_lock *fl, void **priv)
551{
552 struct file *filp = fl->fl_file;
553 struct fasync_struct *fa = *priv;
554
555 /*
556 * fasync_insert_entry() returns the old entry if any. If there was no
557 * old entry, then it used "priv" and inserted it into the fasync list.
558 * Clear the pointer to indicate that it shouldn't be freed.
559 */
560 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
561 *priv = NULL;
562
Eric W. Biederman01919132017-07-16 22:05:57 -0500563 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400564}
565
Alexey Dobriyan7b021962009-09-21 17:01:12 -0700566static const struct lock_manager_operations lease_manager_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400567 .lm_break = lease_break_callback,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400568 .lm_change = lease_modify,
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400569 .lm_setup = lease_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570};
571
572/*
573 * Initialize a lease, use the default lock manager operations
574 */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400575static int lease_init(struct file *filp, long type, struct file_lock *fl)
Joe Perches447a5642018-03-21 15:09:32 -0700576{
Trond Myklebust75dff552006-05-07 23:02:42 -0400577 if (assign_type(fl, type) != 0)
578 return -EINVAL;
579
Jeff Layton7ca76312014-09-01 19:04:48 -0400580 fl->fl_owner = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 fl->fl_pid = current->tgid;
582
583 fl->fl_file = filp;
584 fl->fl_flags = FL_LEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 fl->fl_start = 0;
586 fl->fl_end = OFFSET_MAX;
587 fl->fl_ops = NULL;
588 fl->fl_lmops = &lease_manager_ops;
589 return 0;
590}
591
592/* Allocate a file_lock initialised to this type of lease */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400593static struct file_lock *lease_alloc(struct file *filp, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct file_lock *fl = locks_alloc_lock();
Trond Myklebust75dff552006-05-07 23:02:42 -0400596 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (fl == NULL)
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500599 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 error = lease_init(filp, type, fl);
Trond Myklebust75dff552006-05-07 23:02:42 -0400602 if (error) {
603 locks_free_lock(fl);
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500604 return ERR_PTR(error);
Trond Myklebust75dff552006-05-07 23:02:42 -0400605 }
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500606 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/* Check if two locks overlap each other.
610 */
611static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
612{
613 return ((fl1->fl_end >= fl2->fl_start) &&
614 (fl2->fl_end >= fl1->fl_start));
615}
616
617/*
618 * Check whether two locks have the same owner.
619 */
Matt Mackall33443c42006-01-08 01:05:22 -0800620static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400622 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return fl2->fl_lmops == fl1->fl_lmops &&
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400624 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return fl1->fl_owner == fl2->fl_owner;
626}
627
Jeff Layton6109c852015-01-16 15:05:57 -0500628/* Must be called with the flc_lock held! */
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500629static void locks_insert_global_locks(struct file_lock *fl)
Jeff Layton88974692013-06-21 08:58:14 -0400630{
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200631 struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
632
Peter Zijlstraaba37662015-06-22 14:16:33 +0200633 percpu_rwsem_assert_held(&file_rwsem);
634
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200635 spin_lock(&fll->lock);
Jeff Layton7012b022013-06-21 08:58:22 -0400636 fl->fl_link_cpu = smp_processor_id();
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200637 hlist_add_head(&fl->fl_link, &fll->hlist);
638 spin_unlock(&fll->lock);
Jeff Layton88974692013-06-21 08:58:14 -0400639}
640
Jeff Layton6109c852015-01-16 15:05:57 -0500641/* Must be called with the flc_lock held! */
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500642static void locks_delete_global_locks(struct file_lock *fl)
Jeff Layton88974692013-06-21 08:58:14 -0400643{
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200644 struct file_lock_list_struct *fll;
645
Peter Zijlstraaba37662015-06-22 14:16:33 +0200646 percpu_rwsem_assert_held(&file_rwsem);
647
Jeff Layton7012b022013-06-21 08:58:22 -0400648 /*
649 * Avoid taking lock if already unhashed. This is safe since this check
Jeff Layton6109c852015-01-16 15:05:57 -0500650 * is done while holding the flc_lock, and new insertions into the list
Jeff Layton7012b022013-06-21 08:58:22 -0400651 * also require that it be held.
652 */
653 if (hlist_unhashed(&fl->fl_link))
654 return;
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200655
656 fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
657 spin_lock(&fll->lock);
Jeff Layton139ca042013-06-21 08:58:17 -0400658 hlist_del_init(&fl->fl_link);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +0200659 spin_unlock(&fll->lock);
Jeff Layton88974692013-06-21 08:58:14 -0400660}
661
Jeff Layton3999e492013-06-21 08:58:19 -0400662static unsigned long
663posix_owner_key(struct file_lock *fl)
664{
665 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
666 return fl->fl_lmops->lm_owner_key(fl);
667 return (unsigned long)fl->fl_owner;
668}
669
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500670static void locks_insert_global_blocked(struct file_lock *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400671{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400672 lockdep_assert_held(&blocked_lock_lock);
673
Jeff Layton3999e492013-06-21 08:58:19 -0400674 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
Jeff Layton88974692013-06-21 08:58:14 -0400675}
676
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500677static void locks_delete_global_blocked(struct file_lock *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400678{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400679 lockdep_assert_held(&blocked_lock_lock);
680
Jeff Layton48f74182013-06-21 08:58:18 -0400681 hash_del(&waiter->fl_link);
Jeff Layton88974692013-06-21 08:58:14 -0400682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/* Remove waiter from blocker's block list.
685 * When blocker ends up pointing to itself then the list is empty.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400686 *
Jeff Layton7b2296a2013-06-21 08:58:20 -0400687 * Must be called with blocked_lock_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Matt Mackall33443c42006-01-08 01:05:22 -0800689static void __locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jeff Layton88974692013-06-21 08:58:14 -0400691 locks_delete_global_blocked(waiter);
NeilBrownada5c1d2018-11-30 10:04:08 +1100692 list_del_init(&waiter->fl_blocked_member);
693 waiter->fl_blocker = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrownad6bbd82018-11-30 10:04:08 +1100696static void __locks_wake_up_blocks(struct file_lock *blocker)
697{
698 while (!list_empty(&blocker->fl_blocked_requests)) {
699 struct file_lock *waiter;
700
701 waiter = list_first_entry(&blocker->fl_blocked_requests,
702 struct file_lock, fl_blocked_member);
703 __locks_delete_block(waiter);
704 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
705 waiter->fl_lmops->lm_notify(waiter);
706 else
707 wake_up(&waiter->fl_wait);
708 }
709}
710
Jeff Layton1a9e64a2013-06-21 08:58:10 -0400711static void locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400713 spin_lock(&blocked_lock_lock);
NeilBrown5946c432018-11-30 10:04:08 +1100714 __locks_wake_up_blocks(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 __locks_delete_block(waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400716 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/* Insert waiter into blocker's block list.
720 * We use a circular list so that processes can be easily woken up in
721 * the order they blocked. The documentation doesn't require this but
722 * it seems like the reasonable thing to do.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400723 *
Jeff Layton6109c852015-01-16 15:05:57 -0500724 * Must be called with both the flc_lock and blocked_lock_lock held. The
NeilBrownada5c1d2018-11-30 10:04:08 +1100725 * fl_blocked_requests list itself is protected by the blocked_lock_lock,
726 * but by ensuring that the flc_lock is also held on insertions we can avoid
727 * taking the blocked_lock_lock in some cases when we see that the
728 * fl_blocked_requests list is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Jeff Layton1c8c6012013-06-21 08:58:15 -0400730static void __locks_insert_block(struct file_lock *blocker,
731 struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
NeilBrownada5c1d2018-11-30 10:04:08 +1100733 BUG_ON(!list_empty(&waiter->fl_blocked_member));
734 waiter->fl_blocker = blocker;
735 list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400736 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
Jeff Layton1c8c6012013-06-21 08:58:15 -0400737 locks_insert_global_blocked(waiter);
NeilBrown5946c432018-11-30 10:04:08 +1100738
739 /* The requests in waiter->fl_blocked are known to conflict with
740 * waiter, but might not conflict with blocker, or the requests
741 * and lock which block it. So they all need to be woken.
742 */
743 __locks_wake_up_blocks(waiter);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400744}
745
Jeff Layton6109c852015-01-16 15:05:57 -0500746/* Must be called with flc_lock held. */
Jeff Layton1c8c6012013-06-21 08:58:15 -0400747static void locks_insert_block(struct file_lock *blocker,
748 struct file_lock *waiter)
749{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400750 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400751 __locks_insert_block(blocker, waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400752 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Jeff Layton1cb36012013-06-21 08:58:12 -0400755/*
756 * Wake up processes blocked waiting for blocker.
757 *
Jeff Layton6109c852015-01-16 15:05:57 -0500758 * Must be called with the inode->flc_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
760static void locks_wake_up_blocks(struct file_lock *blocker)
761{
Jeff Layton4e8c7652013-06-21 08:58:16 -0400762 /*
763 * Avoid taking global lock if list is empty. This is safe since new
Jeff Layton6109c852015-01-16 15:05:57 -0500764 * blocked requests are only added to the list under the flc_lock, and
NeilBrownada5c1d2018-11-30 10:04:08 +1100765 * the flc_lock is always held here. Note that removal from the
766 * fl_blocked_requests list does not require the flc_lock, so we must
767 * recheck list_empty() after acquiring the blocked_lock_lock.
Jeff Layton4e8c7652013-06-21 08:58:16 -0400768 */
NeilBrownada5c1d2018-11-30 10:04:08 +1100769 if (list_empty(&blocker->fl_blocked_requests))
Jeff Layton4e8c7652013-06-21 08:58:16 -0400770 return;
771
Jeff Layton7b2296a2013-06-21 08:58:20 -0400772 spin_lock(&blocked_lock_lock);
NeilBrownad6bbd82018-11-30 10:04:08 +1100773 __locks_wake_up_blocks(blocker);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400774 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Jeff Layton5263e312015-01-16 15:05:55 -0500777static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500778locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
Jeff Layton5263e312015-01-16 15:05:55 -0500779{
Jeff Layton5263e312015-01-16 15:05:55 -0500780 list_add_tail(&fl->fl_list, before);
781 locks_insert_global_locks(fl);
782}
783
Jeff Layton8634b512015-01-16 15:05:55 -0500784static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500785locks_unlink_lock_ctx(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jeff Layton88974692013-06-21 08:58:14 -0400787 locks_delete_global_locks(fl);
Jeff Layton8634b512015-01-16 15:05:55 -0500788 list_del_init(&fl->fl_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 locks_wake_up_blocks(fl);
Jeff Layton24cbe782014-02-03 12:13:06 -0500790}
791
Jeff Layton5263e312015-01-16 15:05:55 -0500792static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500793locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
Jeff Layton5263e312015-01-16 15:05:55 -0500794{
Jeff Laytone084c1b2015-02-16 14:32:03 -0500795 locks_unlink_lock_ctx(fl);
Jeff Layton8634b512015-01-16 15:05:55 -0500796 if (dispose)
797 list_add(&fl->fl_list, dispose);
798 else
799 locks_free_lock(fl);
Jeff Layton5263e312015-01-16 15:05:55 -0500800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
803 * checks for shared/exclusive status of overlapping locks.
804 */
805static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
806{
807 if (sys_fl->fl_type == F_WRLCK)
808 return 1;
809 if (caller_fl->fl_type == F_WRLCK)
810 return 1;
811 return 0;
812}
813
814/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
815 * checking before calling the locks_conflict().
816 */
817static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
818{
819 /* POSIX locks owned by the same process do not conflict with
820 * each other.
821 */
Jeff Layton9b8c8692015-04-03 09:04:02 -0400822 if (posix_same_owner(caller_fl, sys_fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return (0);
824
825 /* Check whether they overlap */
826 if (!locks_overlap(caller_fl, sys_fl))
827 return 0;
828
829 return (locks_conflict(caller_fl, sys_fl));
830}
831
832/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
833 * checking before calling the locks_conflict().
834 */
835static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
836{
837 /* FLOCK locks referring to the same filp do not conflict with
838 * each other.
839 */
Jeff Layton9b8c8692015-04-03 09:04:02 -0400840 if (caller_fl->fl_file == sys_fl->fl_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return (0);
842 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
843 return 0;
844
845 return (locks_conflict(caller_fl, sys_fl));
846}
847
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400848void
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500849posix_test_lock(struct file *filp, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct file_lock *cfl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500852 struct file_lock_context *ctx;
Miklos Szeredic568d682016-09-16 12:44:20 +0200853 struct inode *inode = locks_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200855 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500856 if (!ctx || list_empty_careful(&ctx->flc_posix)) {
J. Bruce Fields129a84d2007-05-10 18:38:43 -0400857 fl->fl_type = F_UNLCK;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500858 return;
859 }
860
Jeff Layton6109c852015-01-16 15:05:57 -0500861 spin_lock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500862 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
863 if (posix_locks_conflict(fl, cfl)) {
864 locks_copy_conflock(fl, cfl);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500865 goto out;
866 }
867 }
868 fl->fl_type = F_UNLCK;
869out:
Jeff Layton6109c852015-01-16 15:05:57 -0500870 spin_unlock(&ctx->flc_lock);
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400871 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873EXPORT_SYMBOL(posix_test_lock);
874
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400875/*
876 * Deadlock detection:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400878 * We attempt to detect deadlocks that are due purely to posix file
879 * locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400881 * We assume that a task can be waiting for at most one lock at a time.
882 * So for any acquired lock, the process holding that lock may be
883 * waiting on at most one other lock. That lock in turns may be held by
884 * someone waiting for at most one other lock. Given a requested lock
885 * caller_fl which is about to wait for a conflicting lock block_fl, we
886 * follow this chain of waiters to ensure we are not about to create a
887 * cycle.
J. Bruce Fields97855b42007-10-30 11:20:02 -0400888 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400889 * Since we do this before we ever put a process to sleep on a lock, we
890 * are ensured that there is never a cycle; that is what guarantees that
891 * the while() loop in posix_locks_deadlock() eventually completes.
892 *
893 * Note: the above assumption may not be true when handling lock
894 * requests from a broken NFS client. It may also fail in the presence
895 * of tasks (such as posix threads) sharing the same open file table.
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400896 * To handle those cases, we just bail out after a few iterations.
Jeff Layton57b65322014-02-03 12:13:09 -0500897 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400898 * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
Jeff Layton57b65322014-02-03 12:13:09 -0500899 * Because the owner is not even nominally tied to a thread of
900 * execution, the deadlock detection below can't reasonably work well. Just
901 * skip it for those.
902 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400903 * In principle, we could do a more limited deadlock detection on FL_OFDLCK
Jeff Layton57b65322014-02-03 12:13:09 -0500904 * locks that just checks for the case where two tasks are attempting to
905 * upgrade from read to write locks on the same inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
J. Bruce Fields97855b42007-10-30 11:20:02 -0400907
908#define MAX_DEADLK_ITERATIONS 10
909
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400910/* Find a lock that the owner of the given block_fl is blocking on. */
911static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
912{
913 struct file_lock *fl;
914
Jeff Layton3999e492013-06-21 08:58:19 -0400915 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
NeilBrown5946c432018-11-30 10:04:08 +1100916 if (posix_same_owner(fl, block_fl)) {
917 while (fl->fl_blocker)
918 fl = fl->fl_blocker;
919 return fl;
920 }
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400921 }
922 return NULL;
923}
924
Jeff Layton7b2296a2013-06-21 08:58:20 -0400925/* Must be called with the blocked_lock_lock held! */
Adrian Bunkb0904e12006-06-23 02:05:13 -0700926static int posix_locks_deadlock(struct file_lock *caller_fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct file_lock *block_fl)
928{
J. Bruce Fields97855b42007-10-30 11:20:02 -0400929 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Daniel Wagner663d5af2015-04-03 09:04:03 -0400931 lockdep_assert_held(&blocked_lock_lock);
932
Jeff Layton57b65322014-02-03 12:13:09 -0500933 /*
934 * This deadlock detector can't reasonably detect deadlocks with
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400935 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
Jeff Layton57b65322014-02-03 12:13:09 -0500936 */
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400937 if (IS_OFDLCK(caller_fl))
Jeff Layton57b65322014-02-03 12:13:09 -0500938 return 0;
939
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400940 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
941 if (i++ > MAX_DEADLK_ITERATIONS)
942 return 0;
943 if (posix_same_owner(caller_fl, block_fl))
944 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 return 0;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
J. Bruce Fields02888f42007-09-12 15:45:07 -0400950 * after any leases, but before any posix locks.
Trond Myklebustf475ae92006-06-29 16:38:32 -0400951 *
952 * Note that if called with an FL_EXISTS argument, the caller may determine
953 * whether or not a lock was successfully freed by testing the return
954 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Jeff Laytonbcd7f78d2015-07-11 06:43:02 -0400956static int flock_lock_inode(struct inode *inode, struct file_lock *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Trond Myklebust993dfa82006-03-31 02:30:55 -0800958 struct file_lock *new_fl = NULL;
Jeff Layton5263e312015-01-16 15:05:55 -0500959 struct file_lock *fl;
960 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int error = 0;
Jeff Layton5263e312015-01-16 15:05:55 -0500962 bool found = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -0400963 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jeff Layton5c1c6692015-04-03 09:04:03 -0400965 ctx = locks_get_lock_context(inode, request->fl_type);
966 if (!ctx) {
967 if (request->fl_type != F_UNLCK)
968 return -ENOMEM;
969 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
970 }
Jeff Layton5263e312015-01-16 15:05:55 -0500971
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200972 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
973 new_fl = locks_alloc_lock();
974 if (!new_fl)
975 return -ENOMEM;
976 }
977
Peter Zijlstra87709e22016-05-30 16:48:35 +0200978 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -0500979 spin_lock(&ctx->flc_lock);
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400980 if (request->fl_flags & FL_ACCESS)
981 goto find_conflict;
Pavel Emelyanov84d535a2007-09-11 16:38:13 +0400982
Jeff Layton5263e312015-01-16 15:05:55 -0500983 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
Jeff Laytonbcd7f78d2015-07-11 06:43:02 -0400984 if (request->fl_file != fl->fl_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 continue;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800986 if (request->fl_type == fl->fl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto out;
Jeff Layton5263e312015-01-16 15:05:55 -0500988 found = true;
Jeff Laytone084c1b2015-02-16 14:32:03 -0500989 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Trond Myklebustf475ae92006-06-29 16:38:32 -0400993 if (request->fl_type == F_UNLCK) {
994 if ((request->fl_flags & FL_EXISTS) && !found)
995 error = -ENOENT;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800996 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -0400997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400999find_conflict:
Jeff Layton5263e312015-01-16 15:05:55 -05001000 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
Trond Myklebust993dfa82006-03-31 02:30:55 -08001001 if (!flock_locks_conflict(request, fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 continue;
1003 error = -EAGAIN;
Miklos Szeredibde74e42008-07-25 01:48:57 -07001004 if (!(request->fl_flags & FL_SLEEP))
1005 goto out;
1006 error = FILE_LOCK_DEFERRED;
1007 locks_insert_block(fl, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
1009 }
Trond Myklebustf07f18d2006-06-29 16:38:37 -04001010 if (request->fl_flags & FL_ACCESS)
1011 goto out;
Trond Myklebust993dfa82006-03-31 02:30:55 -08001012 locks_copy_lock(new_fl, request);
NeilBrown5946c432018-11-30 10:04:08 +11001013 locks_move_blocks(new_fl, request);
Jeff Laytone084c1b2015-02-16 14:32:03 -05001014 locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
Trond Myklebust993dfa82006-03-31 02:30:55 -08001015 new_fl = NULL;
Kirill Korotaev9cedc192006-06-14 17:59:35 +04001016 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018out:
Jeff Layton6109c852015-01-16 15:05:57 -05001019 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001020 percpu_up_read_preempt_enable(&file_rwsem);
Trond Myklebust993dfa82006-03-31 02:30:55 -08001021 if (new_fl)
1022 locks_free_lock(new_fl);
Jeff Laytoned9814d2014-08-11 14:20:31 -04001023 locks_dispose_list(&dispose);
Jeff Laytonc883da32018-07-30 07:54:56 -04001024 trace_flock_lock_inode(inode, request, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return error;
1026}
1027
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001028static int posix_lock_inode(struct inode *inode, struct file_lock *request,
1029 struct file_lock *conflock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001031 struct file_lock *fl, *tmp;
Miklos Szeredi39005d02006-06-23 02:05:10 -07001032 struct file_lock *new_fl = NULL;
1033 struct file_lock *new_fl2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct file_lock *left = NULL;
1035 struct file_lock *right = NULL;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001036 struct file_lock_context *ctx;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001037 int error;
1038 bool added = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -04001039 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jeff Layton5c1c6692015-04-03 09:04:03 -04001041 ctx = locks_get_lock_context(inode, request->fl_type);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001042 if (!ctx)
Jeff Layton5c1c6692015-04-03 09:04:03 -04001043 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * We may need two file_lock structures for this operation,
1047 * so we get them in advance to avoid races.
Miklos Szeredi39005d02006-06-23 02:05:10 -07001048 *
1049 * In some cases we can be sure, that no new locks will be needed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Miklos Szeredi39005d02006-06-23 02:05:10 -07001051 if (!(request->fl_flags & FL_ACCESS) &&
1052 (request->fl_type != F_UNLCK ||
1053 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
1054 new_fl = locks_alloc_lock();
1055 new_fl2 = locks_alloc_lock();
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Peter Zijlstra87709e22016-05-30 16:48:35 +02001058 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001059 spin_lock(&ctx->flc_lock);
Jeff Layton1cb36012013-06-21 08:58:12 -04001060 /*
1061 * New lock request. Walk all POSIX locks and look for conflicts. If
1062 * there are any, either return error or put the request on the
Jeff Layton48f74182013-06-21 08:58:18 -04001063 * blocker's list of waiters and the global blocked_hash.
Jeff Layton1cb36012013-06-21 08:58:12 -04001064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (request->fl_type != F_UNLCK) {
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001066 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!posix_locks_conflict(request, fl))
1068 continue;
Andy Adamson5842add2006-03-26 01:37:26 -08001069 if (conflock)
Kinglong Mee3fe0fff12014-08-22 10:18:42 -04001070 locks_copy_conflock(conflock, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 error = -EAGAIN;
1072 if (!(request->fl_flags & FL_SLEEP))
1073 goto out;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001074 /*
1075 * Deadlock detection and insertion into the blocked
1076 * locks list must be done while holding the same lock!
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 error = -EDEADLK;
Jeff Layton7b2296a2013-06-21 08:58:20 -04001079 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001080 if (likely(!posix_locks_deadlock(request, fl))) {
1081 error = FILE_LOCK_DEFERRED;
1082 __locks_insert_block(fl, request);
1083 }
Jeff Layton7b2296a2013-06-21 08:58:20 -04001084 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto out;
1086 }
1087 }
1088
1089 /* If we're just looking for a conflict, we're done. */
1090 error = 0;
1091 if (request->fl_flags & FL_ACCESS)
1092 goto out;
1093
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001094 /* Find the first old lock with the same owner as the new lock */
1095 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1096 if (posix_same_owner(request, fl))
1097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Jeff Layton1cb36012013-06-21 08:58:12 -04001100 /* Process locks with this owner. */
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001101 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1102 if (!posix_same_owner(request, fl))
1103 break;
1104
1105 /* Detect adjacent or overlapping regions (if same lock type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (request->fl_type == fl->fl_type) {
Olaf Kirch449231d2005-08-25 16:25:35 -07001107 /* In all comparisons of start vs end, use
1108 * "start - 1" rather than "end + 1". If end
1109 * is OFFSET_MAX, end + 1 will become negative.
1110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (fl->fl_end < request->fl_start - 1)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001112 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* If the next lock in the list has entirely bigger
1114 * addresses than the new one, insert the lock here.
1115 */
Olaf Kirch449231d2005-08-25 16:25:35 -07001116 if (fl->fl_start - 1 > request->fl_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
1118
1119 /* If we come here, the new and old lock are of the
1120 * same type and adjacent or overlapping. Make one
1121 * lock yielding from the lower start address of both
1122 * locks to the higher end address.
1123 */
1124 if (fl->fl_start > request->fl_start)
1125 fl->fl_start = request->fl_start;
1126 else
1127 request->fl_start = fl->fl_start;
1128 if (fl->fl_end < request->fl_end)
1129 fl->fl_end = request->fl_end;
1130 else
1131 request->fl_end = fl->fl_end;
1132 if (added) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001133 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 continue;
1135 }
1136 request = fl;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001137 added = true;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001138 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* Processing for different lock types is a bit
1140 * more complex.
1141 */
1142 if (fl->fl_end < request->fl_start)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001143 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (fl->fl_start > request->fl_end)
1145 break;
1146 if (request->fl_type == F_UNLCK)
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001147 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (fl->fl_start < request->fl_start)
1149 left = fl;
1150 /* If the next lock in the list has a higher end
1151 * address than the new one, insert the new one here.
1152 */
1153 if (fl->fl_end > request->fl_end) {
1154 right = fl;
1155 break;
1156 }
1157 if (fl->fl_start >= request->fl_start) {
1158 /* The new lock completely replaces an old
1159 * one (This may happen several times).
1160 */
1161 if (added) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001162 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 continue;
1164 }
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001165 /*
1166 * Replace the old lock with new_fl, and
1167 * remove the old one. It's safe to do the
1168 * insert here since we know that we won't be
1169 * using new_fl later, and that the lock is
1170 * just replacing an existing lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001172 error = -ENOLCK;
1173 if (!new_fl)
1174 goto out;
1175 locks_copy_lock(new_fl, request);
1176 request = new_fl;
1177 new_fl = NULL;
Jeff Laytone084c1b2015-02-16 14:32:03 -05001178 locks_insert_lock_ctx(request, &fl->fl_list);
1179 locks_delete_lock_ctx(fl, &dispose);
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001180 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001185 /*
Jeff Layton1cb36012013-06-21 08:58:12 -04001186 * The above code only modifies existing locks in case of merging or
1187 * replacing. If new lock(s) need to be inserted all modifications are
1188 * done below this, so it's safe yet to bail out.
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001189 */
1190 error = -ENOLCK; /* "no luck" */
1191 if (right && left == right && !new_fl2)
1192 goto out;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 error = 0;
1195 if (!added) {
Trond Myklebustf475ae92006-06-29 16:38:32 -04001196 if (request->fl_type == F_UNLCK) {
1197 if (request->fl_flags & FL_EXISTS)
1198 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -04001200 }
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001201
1202 if (!new_fl) {
1203 error = -ENOLCK;
1204 goto out;
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 locks_copy_lock(new_fl, request);
NeilBrown5946c432018-11-30 10:04:08 +11001207 locks_move_blocks(new_fl, request);
Jeff Laytone084c1b2015-02-16 14:32:03 -05001208 locks_insert_lock_ctx(new_fl, &fl->fl_list);
Jeff Layton2e2f756f2015-02-17 17:08:23 -05001209 fl = new_fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 new_fl = NULL;
1211 }
1212 if (right) {
1213 if (left == right) {
1214 /* The new lock breaks the old one in two pieces,
1215 * so we have to use the second new lock.
1216 */
1217 left = new_fl2;
1218 new_fl2 = NULL;
1219 locks_copy_lock(left, right);
Jeff Laytone084c1b2015-02-16 14:32:03 -05001220 locks_insert_lock_ctx(left, &fl->fl_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 right->fl_start = request->fl_end + 1;
1223 locks_wake_up_blocks(right);
1224 }
1225 if (left) {
1226 left->fl_end = request->fl_start - 1;
1227 locks_wake_up_blocks(left);
1228 }
1229 out:
Jeff Layton6109c852015-01-16 15:05:57 -05001230 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001231 percpu_up_read_preempt_enable(&file_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * Free any unused locks.
1234 */
1235 if (new_fl)
1236 locks_free_lock(new_fl);
1237 if (new_fl2)
1238 locks_free_lock(new_fl2);
Jeff Laytoned9814d2014-08-11 14:20:31 -04001239 locks_dispose_list(&dispose);
Jeff Layton18909102016-01-06 21:26:10 -05001240 trace_posix_lock_inode(inode, request, error);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return error;
1243}
1244
1245/**
1246 * posix_lock_file - Apply a POSIX-style lock to a file
1247 * @filp: The file to apply the lock to
1248 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05001249 * @conflock: Place to return a copy of the conflicting lock, if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
1251 * Add a POSIX style lock to a file.
1252 * We merge adjacent & overlapping locks whenever possible.
1253 * POSIX locks are sorted by owner task, then by starting address
Trond Myklebustf475ae92006-06-29 16:38:32 -04001254 *
1255 * Note that if called with an FL_EXISTS argument, the caller may determine
1256 * whether or not a lock was successfully freed by testing the return
1257 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Marc Eshel150b3932007-01-18 16:15:35 -05001259int posix_lock_file(struct file *filp, struct file_lock *fl,
Andy Adamson5842add2006-03-26 01:37:26 -08001260 struct file_lock *conflock)
1261{
Miklos Szeredic568d682016-09-16 12:44:20 +02001262 return posix_lock_inode(locks_inode(filp), fl, conflock);
Andy Adamson5842add2006-03-26 01:37:26 -08001263}
Marc Eshel150b3932007-01-18 16:15:35 -05001264EXPORT_SYMBOL(posix_lock_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266/**
Jeff Layton29d01b22015-07-11 06:43:02 -04001267 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1268 * @inode: inode of file to which lock request should be applied
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * @fl: The lock to be applied
1270 *
Benjamin Coddington616fb382015-10-22 13:38:15 -04001271 * Apply a POSIX style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04001273static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 int error;
1276 might_sleep ();
1277 for (;;) {
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001278 error = posix_lock_inode(inode, fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001279 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
NeilBrownada5c1d2018-11-30 10:04:08 +11001281 error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!error)
1283 continue;
1284
1285 locks_delete_block(fl);
1286 break;
1287 }
1288 return error;
1289}
Jeff Layton29d01b22015-07-11 06:43:02 -04001290
Jeff Layton9e8925b2015-11-16 09:49:34 -05001291#ifdef CONFIG_MANDATORY_FILE_LOCKING
Jeff Layton29d01b22015-07-11 06:43:02 -04001292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * locks_mandatory_locked - Check for an active lock
Jeff Laytond7a06982014-03-10 09:54:15 -04001294 * @file: the file to check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
1296 * Searches the inode's list of locks to find any POSIX locks which conflict.
1297 * This function is called from locks_verify_locked() only.
1298 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001299int locks_mandatory_locked(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001301 int ret;
Miklos Szeredic568d682016-09-16 12:44:20 +02001302 struct inode *inode = locks_inode(file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001303 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct file_lock *fl;
1305
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001306 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001307 if (!ctx || list_empty_careful(&ctx->flc_posix))
1308 return 0;
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Search the lock list for this inode for any POSIX locks.
1312 */
Jeff Layton6109c852015-01-16 15:05:57 -05001313 spin_lock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001314 ret = 0;
1315 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02001316 if (fl->fl_owner != current->files &&
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001317 fl->fl_owner != file) {
1318 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Jeff Layton6109c852015-01-16 15:05:57 -05001322 spin_unlock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001323 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/**
1327 * locks_mandatory_area - Check for a conflicting lock
Christoph Hellwigacc15572015-12-03 12:59:49 +01001328 * @inode: the file to check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * @filp: how the file was opened (if it was)
Christoph Hellwigacc15572015-12-03 12:59:49 +01001330 * @start: first byte in the file to check
1331 * @end: lastbyte in the file to check
1332 * @type: %F_WRLCK for a write lock, else %F_RDLCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 * Searches the inode's list of locks to find any POSIX locks which conflict.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Christoph Hellwigacc15572015-12-03 12:59:49 +01001336int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
1337 loff_t end, unsigned char type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct file_lock fl;
1340 int error;
Jeff Layton29723ad2014-03-10 09:54:19 -04001341 bool sleep = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 locks_init_lock(&fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 fl.fl_pid = current->tgid;
1345 fl.fl_file = filp;
1346 fl.fl_flags = FL_POSIX | FL_ACCESS;
1347 if (filp && !(filp->f_flags & O_NONBLOCK))
Jeff Layton29723ad2014-03-10 09:54:19 -04001348 sleep = true;
Christoph Hellwigacc15572015-12-03 12:59:49 +01001349 fl.fl_type = type;
1350 fl.fl_start = start;
1351 fl.fl_end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 for (;;) {
Jeff Layton29723ad2014-03-10 09:54:19 -04001354 if (filp) {
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02001355 fl.fl_owner = filp;
Jeff Layton29723ad2014-03-10 09:54:19 -04001356 fl.fl_flags &= ~FL_SLEEP;
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001357 error = posix_lock_inode(inode, &fl, NULL);
Jeff Layton29723ad2014-03-10 09:54:19 -04001358 if (!error)
1359 break;
1360 }
1361
1362 if (sleep)
1363 fl.fl_flags |= FL_SLEEP;
1364 fl.fl_owner = current->files;
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001365 error = posix_lock_inode(inode, &fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001366 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
NeilBrownada5c1d2018-11-30 10:04:08 +11001368 error = wait_event_interruptible(fl.fl_wait, !fl.fl_blocker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (!error) {
1370 /*
1371 * If we've been sleeping someone might have
1372 * changed the permissions behind our back.
1373 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07001374 if (__mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 continue;
1376 }
1377
1378 locks_delete_block(&fl);
1379 break;
1380 }
1381
1382 return error;
1383}
1384
1385EXPORT_SYMBOL(locks_mandatory_area);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001386#endif /* CONFIG_MANDATORY_FILE_LOCKING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
J. Bruce Fields778fc542011-07-26 18:25:49 -04001388static void lease_clear_pending(struct file_lock *fl, int arg)
1389{
1390 switch (arg) {
1391 case F_UNLCK:
1392 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1393 /* fall through: */
1394 case F_RDLCK:
1395 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1396 }
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/* We already had a lease on this file; just change its type */
Jeff Layton7448cc32015-01-16 15:05:57 -05001400int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int error = assign_type(fl, arg);
1403
1404 if (error)
1405 return error;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001406 lease_clear_pending(fl, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 locks_wake_up_blocks(fl);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001408 if (arg == F_UNLCK) {
1409 struct file *filp = fl->fl_file;
1410
1411 f_delown(filp);
1412 filp->f_owner.signum = 0;
J. Bruce Fields96d6d592012-07-27 16:18:00 -04001413 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1414 if (fl->fl_fasync != NULL) {
1415 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1416 fl->fl_fasync = NULL;
1417 }
Jeff Laytone084c1b2015-02-16 14:32:03 -05001418 locks_delete_lock_ctx(fl, dispose);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422EXPORT_SYMBOL(lease_modify);
1423
J. Bruce Fields778fc542011-07-26 18:25:49 -04001424static bool past_time(unsigned long then)
1425{
1426 if (!then)
1427 /* 0 is a special value meaning "this never expires": */
1428 return false;
1429 return time_after(jiffies, then);
1430}
1431
Jeff Laytonc45198e2014-09-01 07:12:07 -04001432static void time_out_leases(struct inode *inode, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Jeff Layton8634b512015-01-16 15:05:55 -05001434 struct file_lock_context *ctx = inode->i_flctx;
1435 struct file_lock *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jeff Layton6109c852015-01-16 15:05:57 -05001437 lockdep_assert_held(&ctx->flc_lock);
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001438
Jeff Layton8634b512015-01-16 15:05:55 -05001439 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001440 trace_time_out_leases(inode, fl);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001441 if (past_time(fl->fl_downgrade_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001442 lease_modify(fl, F_RDLCK, dispose);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001443 if (past_time(fl->fl_break_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001444 lease_modify(fl, F_UNLCK, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446}
1447
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001448static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1449{
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001450 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
1451 return false;
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001452 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1453 return false;
1454 return locks_conflict(breaker, lease);
1455}
1456
Jeff Layton03d12dd2014-09-01 14:53:41 -04001457static bool
1458any_leases_conflict(struct inode *inode, struct file_lock *breaker)
1459{
Jeff Layton8634b512015-01-16 15:05:55 -05001460 struct file_lock_context *ctx = inode->i_flctx;
Jeff Layton03d12dd2014-09-01 14:53:41 -04001461 struct file_lock *fl;
1462
Jeff Layton6109c852015-01-16 15:05:57 -05001463 lockdep_assert_held(&ctx->flc_lock);
Jeff Layton03d12dd2014-09-01 14:53:41 -04001464
Jeff Layton8634b512015-01-16 15:05:55 -05001465 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Jeff Layton03d12dd2014-09-01 14:53:41 -04001466 if (leases_conflict(fl, breaker))
1467 return true;
1468 }
1469 return false;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
1473 * __break_lease - revoke all outstanding leases on file
1474 * @inode: the inode of the file to return
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001475 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1476 * break all leases
1477 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
1478 * only delegations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
david m. richter87250dd2007-05-09 16:10:27 -04001480 * break_lease (inlined for speed) has checked there already is at least
1481 * some kind of lock (maybe a lease) on this file. Leases are broken on
1482 * a call to open() or truncate(). This function can sleep unless you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * specified %O_NONBLOCK to your open().
1484 */
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001485int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
J. Bruce Fields778fc542011-07-26 18:25:49 -04001487 int error = 0;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001488 struct file_lock_context *ctx;
Yan, Zhenga9011252015-03-27 10:34:20 +08001489 struct file_lock *new_fl, *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 unsigned long break_time;
Al Viro8737c932009-12-24 06:47:55 -05001491 int want_write = (mode & O_ACCMODE) != O_RDONLY;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001492 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Al Viro8737c932009-12-24 06:47:55 -05001494 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001495 if (IS_ERR(new_fl))
1496 return PTR_ERR(new_fl);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001497 new_fl->fl_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Jeff Layton8634b512015-01-16 15:05:55 -05001499 /* typically we will check that ctx is non-NULL before calling */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001500 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001501 if (!ctx) {
1502 WARN_ON_ONCE(1);
1503 return error;
1504 }
1505
Peter Zijlstra87709e22016-05-30 16:48:35 +02001506 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001507 spin_lock(&ctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jeff Laytonc45198e2014-09-01 07:12:07 -04001509 time_out_leases(inode, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Jeff Layton03d12dd2014-09-01 14:53:41 -04001511 if (!any_leases_conflict(inode, new_fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001512 goto out;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break_time = 0;
1515 if (lease_break_time > 0) {
1516 break_time = jiffies + lease_break_time * HZ;
1517 if (break_time == 0)
1518 break_time++; /* so that 0 means no break time */
1519 }
1520
Yan, Zhenga9011252015-03-27 10:34:20 +08001521 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001522 if (!leases_conflict(fl, new_fl))
1523 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001524 if (want_write) {
1525 if (fl->fl_flags & FL_UNLOCK_PENDING)
1526 continue;
1527 fl->fl_flags |= FL_UNLOCK_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 fl->fl_break_time = break_time;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001529 } else {
Jeff Layton8634b512015-01-16 15:05:55 -05001530 if (lease_breaking(fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001531 continue;
1532 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1533 fl->fl_downgrade_time = break_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001535 if (fl->fl_lmops->lm_break(fl))
Jeff Laytone084c1b2015-02-16 14:32:03 -05001536 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Jeff Layton8634b512015-01-16 15:05:55 -05001539 if (list_empty(&ctx->flc_lease))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001540 goto out;
1541
Jeff Layton843c6b22014-09-01 14:27:43 -04001542 if (mode & O_NONBLOCK) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001543 trace_break_lease_noblock(inode, new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 error = -EWOULDBLOCK;
1545 goto out;
1546 }
1547
1548restart:
Jeff Layton8634b512015-01-16 15:05:55 -05001549 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
1550 break_time = fl->fl_break_time;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001551 if (break_time != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break_time -= jiffies;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001553 if (break_time == 0)
1554 break_time++;
Jeff Layton8634b512015-01-16 15:05:55 -05001555 locks_insert_block(fl, new_fl);
Jeff Layton62af4f12014-05-09 14:13:05 -04001556 trace_break_lease_block(inode, new_fl);
Jeff Layton6109c852015-01-16 15:05:57 -05001557 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001558 percpu_up_read_preempt_enable(&file_rwsem);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001559
Jeff Laytonc45198e2014-09-01 07:12:07 -04001560 locks_dispose_list(&dispose);
Matthew Wilcox4321e012008-01-14 21:28:30 -07001561 error = wait_event_interruptible_timeout(new_fl->fl_wait,
NeilBrownada5c1d2018-11-30 10:04:08 +11001562 !new_fl->fl_blocker, break_time);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001563
Peter Zijlstra87709e22016-05-30 16:48:35 +02001564 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001565 spin_lock(&ctx->flc_lock);
Jeff Layton62af4f12014-05-09 14:13:05 -04001566 trace_break_lease_unblock(inode, new_fl);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001567 locks_delete_block(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (error >= 0) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001569 /*
1570 * Wait for the next conflicting lease that has not been
1571 * broken yet
1572 */
Jeff Layton03d12dd2014-09-01 14:53:41 -04001573 if (error == 0)
1574 time_out_leases(inode, &dispose);
1575 if (any_leases_conflict(inode, new_fl))
1576 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 error = 0;
1578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out:
Jeff Layton6109c852015-01-16 15:05:57 -05001580 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001581 percpu_up_read_preempt_enable(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001582 locks_dispose_list(&dispose);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001583 locks_free_lock(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return error;
1585}
1586
1587EXPORT_SYMBOL(__break_lease);
1588
1589/**
Amir Goldstein76c47942018-01-03 17:14:34 +02001590 * lease_get_mtime - update modified time of an inode with exclusive lease
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * @inode: the inode
Amir Goldstein76c47942018-01-03 17:14:34 +02001592 * @time: pointer to a timespec which contains the last modified time
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *
1594 * This is to force NFS clients to flush their caches for files with
1595 * exclusive leases. The justification is that if someone has an
Randy Dunlapa6b91912008-03-19 17:01:00 -07001596 * exclusive lease, then they could be modifying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07001598void lease_get_mtime(struct inode *inode, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Jeff Laytonbfe86022014-08-22 10:18:44 -04001600 bool has_lease = false;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001601 struct file_lock_context *ctx;
Jeff Layton8634b512015-01-16 15:05:55 -05001602 struct file_lock *fl;
Jeff Laytonbfe86022014-08-22 10:18:44 -04001603
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001604 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001605 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Jeff Layton6109c852015-01-16 15:05:57 -05001606 spin_lock(&ctx->flc_lock);
Geliang Tang8ace5df2015-11-18 21:40:33 +08001607 fl = list_first_entry_or_null(&ctx->flc_lease,
1608 struct file_lock, fl_list);
1609 if (fl && (fl->fl_type == F_WRLCK))
1610 has_lease = true;
Jeff Layton6109c852015-01-16 15:05:57 -05001611 spin_unlock(&ctx->flc_lock);
Jeff Laytonbfe86022014-08-22 10:18:44 -04001612 }
1613
1614 if (has_lease)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001615 *time = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618EXPORT_SYMBOL(lease_get_mtime);
1619
1620/**
1621 * fcntl_getlease - Enquire what lease is currently active
1622 * @filp: the file
1623 *
1624 * The value returned by this function will be one of
1625 * (if no lease break is pending):
1626 *
1627 * %F_RDLCK to indicate a shared lease is held.
1628 *
1629 * %F_WRLCK to indicate an exclusive lease is held.
1630 *
1631 * %F_UNLCK to indicate no lease is held.
1632 *
1633 * (if a lease break is pending):
1634 *
1635 * %F_RDLCK to indicate an exclusive lease needs to be
1636 * changed to a shared lease (or removed).
1637 *
1638 * %F_UNLCK to indicate the lease needs to be removed.
1639 *
1640 * XXX: sfr & willy disagree over whether F_INPROGRESS
1641 * should be returned to userspace.
1642 */
1643int fcntl_getlease(struct file *filp)
1644{
1645 struct file_lock *fl;
Miklos Szeredic568d682016-09-16 12:44:20 +02001646 struct inode *inode = locks_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001647 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int type = F_UNLCK;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001649 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001651 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001652 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Peter Zijlstra5f430862016-10-08 10:12:28 +02001653 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001654 spin_lock(&ctx->flc_lock);
Miklos Szeredic568d682016-09-16 12:44:20 +02001655 time_out_leases(inode, &dispose);
Jeff Layton8634b512015-01-16 15:05:55 -05001656 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1657 if (fl->fl_file != filp)
1658 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001659 type = target_leasetype(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 }
Jeff Layton6109c852015-01-16 15:05:57 -05001662 spin_unlock(&ctx->flc_lock);
Peter Zijlstra5f430862016-10-08 10:12:28 +02001663 percpu_up_read_preempt_enable(&file_rwsem);
1664
Jeff Layton8634b512015-01-16 15:05:55 -05001665 locks_dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return type;
1668}
1669
Jeff Layton24cbe782014-02-03 12:13:06 -05001670/**
1671 * check_conflicting_open - see if the given dentry points to a file that has
1672 * an existing open that would conflict with the
1673 * desired lease.
1674 * @dentry: dentry to check
1675 * @arg: type of lease that we're trying to acquire
Randy Dunlap7fadc592015-08-09 18:43:17 -07001676 * @flags: current lock flags
Jeff Layton24cbe782014-02-03 12:13:06 -05001677 *
1678 * Check to see if there's an existing open fd on this file that would
1679 * conflict with the lease we're trying to set.
1680 */
1681static int
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001682check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
Jeff Layton24cbe782014-02-03 12:13:06 -05001683{
1684 int ret = 0;
1685 struct inode *inode = dentry->d_inode;
1686
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001687 if (flags & FL_LAYOUT)
1688 return 0;
1689
Miklos Szeredi8cf9ee52018-07-18 15:44:43 +02001690 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
Jeff Layton24cbe782014-02-03 12:13:06 -05001691 return -EAGAIN;
1692
1693 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
1694 (atomic_read(&inode->i_count) > 1)))
1695 ret = -EAGAIN;
1696
1697 return ret;
1698}
1699
Jeff Laytone6f5c782014-08-22 10:40:25 -04001700static int
1701generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Jeff Layton8634b512015-01-16 15:05:55 -05001703 struct file_lock *fl, *my_fl = NULL, *lease;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001704 struct dentry *dentry = filp->f_path.dentry;
Miklos Szeredic568d682016-09-16 12:44:20 +02001705 struct inode *inode = dentry->d_inode;
Jeff Layton8634b512015-01-16 15:05:55 -05001706 struct file_lock_context *ctx;
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001707 bool is_deleg = (*flp)->fl_flags & FL_DELEG;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001708 int error;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001709 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
J. Bruce Fields096657b2010-10-30 17:31:14 -04001711 lease = *flp;
Jeff Layton62af4f12014-05-09 14:13:05 -04001712 trace_generic_add_lease(inode, lease);
1713
Jeff Layton5c1c6692015-04-03 09:04:03 -04001714 /* Note that arg is never F_UNLCK here */
1715 ctx = locks_get_lock_context(inode, arg);
Jeff Layton8634b512015-01-16 15:05:55 -05001716 if (!ctx)
1717 return -ENOMEM;
1718
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001719 /*
1720 * In the delegation case we need mutual exclusion with
1721 * a number of operations that take the i_mutex. We trylock
1722 * because delegations are an optional optimization, and if
1723 * there's some chance of a conflict--we'd rather not
1724 * bother, maybe that's a sign this just isn't a good file to
1725 * hand out a delegation on.
1726 */
Al Viro59551022016-01-22 15:40:57 -05001727 if (is_deleg && !inode_trylock(inode))
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001728 return -EAGAIN;
1729
1730 if (is_deleg && arg == F_WRLCK) {
1731 /* Write delegations are not currently supported: */
Al Viro59551022016-01-22 15:40:57 -05001732 inode_unlock(inode);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001733 WARN_ON_ONCE(1);
1734 return -EINVAL;
1735 }
J. Bruce Fields096657b2010-10-30 17:31:14 -04001736
Peter Zijlstra87709e22016-05-30 16:48:35 +02001737 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001738 spin_lock(&ctx->flc_lock);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001739 time_out_leases(inode, &dispose);
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001740 error = check_conflicting_open(dentry, arg, lease->fl_flags);
Jeff Layton24cbe782014-02-03 12:13:06 -05001741 if (error)
J. Bruce Fields096657b2010-10-30 17:31:14 -04001742 goto out;
Pavel Emelyanov85c59582007-09-20 12:45:02 +04001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /*
1745 * At this point, we know that if there is an exclusive
1746 * lease on this file, then we hold it on this filp
1747 * (otherwise our open of this file would have blocked).
1748 * And if we are trying to acquire an exclusive lease,
1749 * then the file is not open by anyone (including us)
1750 * except for this filp.
1751 */
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001752 error = -EAGAIN;
Jeff Layton8634b512015-01-16 15:05:55 -05001753 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001754 if (fl->fl_file == filp &&
1755 fl->fl_owner == lease->fl_owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001756 my_fl = fl;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001757 continue;
1758 }
Jeff Layton8634b512015-01-16 15:05:55 -05001759
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001760 /*
1761 * No exclusive leases if someone else has a lease on
1762 * this file:
1763 */
1764 if (arg == F_WRLCK)
1765 goto out;
1766 /*
1767 * Modifying our existing lease is OK, but no getting a
1768 * new lease if someone else is opening for write:
1769 */
1770 if (fl->fl_flags & FL_UNLOCK_PENDING)
1771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Jeff Layton8634b512015-01-16 15:05:55 -05001774 if (my_fl != NULL) {
Jeff Layton0164bf02015-03-04 17:34:32 -05001775 lease = my_fl;
1776 error = lease->fl_lmops->lm_change(lease, arg, &dispose);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001777 if (error)
1778 goto out;
1779 goto out_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 error = -EINVAL;
1783 if (!leases_enable)
1784 goto out;
1785
Jeff Laytone084c1b2015-02-16 14:32:03 -05001786 locks_insert_lock_ctx(lease, &ctx->flc_lease);
Jeff Layton24cbe782014-02-03 12:13:06 -05001787 /*
1788 * The check in break_lease() is lockless. It's possible for another
1789 * open to race in after we did the earlier check for a conflicting
1790 * open but before the lease was inserted. Check again for a
1791 * conflicting open and cancel the lease if there is one.
1792 *
1793 * We also add a barrier here to ensure that the insertion of the lock
1794 * precedes these checks.
1795 */
1796 smp_mb();
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001797 error = check_conflicting_open(dentry, arg, lease->fl_flags);
Jeff Layton8634b512015-01-16 15:05:55 -05001798 if (error) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001799 locks_unlink_lock_ctx(lease);
Jeff Layton8634b512015-01-16 15:05:55 -05001800 goto out;
1801 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001802
1803out_setup:
1804 if (lease->fl_lmops->lm_setup)
1805 lease->fl_lmops->lm_setup(lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806out:
Jeff Layton6109c852015-01-16 15:05:57 -05001807 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001808 percpu_up_read_preempt_enable(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001809 locks_dispose_list(&dispose);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001810 if (is_deleg)
Al Viro59551022016-01-22 15:40:57 -05001811 inode_unlock(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001812 if (!error && !my_fl)
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001813 *flp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return error;
1815}
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001816
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001817static int generic_delete_lease(struct file *filp, void *owner)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001818{
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001819 int error = -EAGAIN;
Jeff Layton8634b512015-01-16 15:05:55 -05001820 struct file_lock *fl, *victim = NULL;
Miklos Szeredic568d682016-09-16 12:44:20 +02001821 struct inode *inode = locks_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001822 struct file_lock_context *ctx;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001823 LIST_HEAD(dispose);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001824
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001825 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001826 if (!ctx) {
1827 trace_generic_delete_lease(inode, NULL);
1828 return error;
1829 }
1830
Peter Zijlstra87709e22016-05-30 16:48:35 +02001831 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001832 spin_lock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05001833 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001834 if (fl->fl_file == filp &&
1835 fl->fl_owner == owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001836 victim = fl;
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001837 break;
Jeff Layton8634b512015-01-16 15:05:55 -05001838 }
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001839 }
Jeff Laytona9b1b452015-03-14 09:45:35 -04001840 trace_generic_delete_lease(inode, victim);
Jeff Layton8634b512015-01-16 15:05:55 -05001841 if (victim)
Jeff Layton7448cc32015-01-16 15:05:57 -05001842 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05001843 spin_unlock(&ctx->flc_lock);
Peter Zijlstra87709e22016-05-30 16:48:35 +02001844 percpu_up_read_preempt_enable(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001845 locks_dispose_list(&dispose);
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001846 return error;
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/**
1850 * generic_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001851 * @filp: file pointer
1852 * @arg: type of lease to obtain
1853 * @flp: input - file_lock to use, output - file_lock inserted
1854 * @priv: private data for lm_setup (may be NULL if lm_setup
1855 * doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 *
1857 * The (input) flp->fl_lmops->lm_break function is required
1858 * by break_lease().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04001860int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1861 void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Miklos Szeredic568d682016-09-16 12:44:20 +02001863 struct inode *inode = locks_inode(filp);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001864 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001866 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001867 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (!S_ISREG(inode->i_mode))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 error = security_file_lock(filp, arg);
1871 if (error)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001874 switch (arg) {
1875 case F_UNLCK:
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001876 return generic_delete_lease(filp, *priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001877 case F_RDLCK:
1878 case F_WRLCK:
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001879 if (!(*flp)->fl_lmops->lm_break) {
1880 WARN_ON_ONCE(1);
1881 return -ENOLCK;
1882 }
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001883
Jeff Laytone6f5c782014-08-22 10:40:25 -04001884 return generic_add_lease(filp, arg, flp, priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001885 default:
Dave Jones8d657eb2012-07-13 13:35:36 -04001886 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
Christoph Hellwig0af1a452007-07-31 00:39:22 -07001889EXPORT_SYMBOL(generic_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001891/**
Jeff Laytone51673a2014-08-22 18:13:28 -04001892 * vfs_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001893 * @filp: file pointer
1894 * @arg: type of lease to obtain
1895 * @lease: file_lock to use when adding a lease
1896 * @priv: private info for lm_setup when adding a lease (may be
1897 * NULL if lm_setup doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 *
Jeff Laytone51673a2014-08-22 18:13:28 -04001899 * Call this to establish a lease on the file. The "lease" argument is not
1900 * used for F_UNLCK requests and may be NULL. For commands that set or alter
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04001901 * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
1902 * set; if not, this function will return -ENOLCK (and generate a scary-looking
Jeff Laytone51673a2014-08-22 18:13:28 -04001903 * stack trace).
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001904 *
1905 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1906 * may be NULL if the lm_setup operation doesn't require it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04001908int
1909vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Miklos Szeredide2a4a52018-07-18 15:44:43 +02001911 if (filp->f_op->setlease)
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001912 return filp->f_op->setlease(filp, arg, lease, priv);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001913 else
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001914 return generic_setlease(filp, arg, lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001916EXPORT_SYMBOL_GPL(vfs_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001918static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001920 struct file_lock *fl;
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001921 struct fasync_struct *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 int error;
1923
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +02001924 fl = lease_alloc(filp, arg);
1925 if (IS_ERR(fl))
1926 return PTR_ERR(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001928 new = fasync_alloc();
1929 if (!new) {
1930 locks_free_lock(fl);
1931 return -ENOMEM;
1932 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001933 new->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001935 error = vfs_setlease(filp, arg, &fl, (void **)&new);
Jeff Layton2dfb9282014-08-11 18:14:12 -04001936 if (fl)
1937 locks_free_lock(fl);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001938 if (new)
1939 fasync_free(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return error;
1941}
1942
1943/**
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001944 * fcntl_setlease - sets a lease on an open file
1945 * @fd: open file descriptor
1946 * @filp: file pointer
1947 * @arg: type of lease to obtain
1948 *
1949 * Call this fcntl to establish a lease on the file.
1950 * Note that you also need to call %F_SETSIG to
1951 * receive a signal when the lease is broken.
1952 */
1953int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1954{
1955 if (arg == F_UNLCK)
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001956 return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001957 return do_fcntl_add_lease(fd, filp, arg);
1958}
1959
1960/**
Jeff Layton29d01b22015-07-11 06:43:02 -04001961 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
1962 * @inode: inode of the file to apply to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * @fl: The lock to be applied
1964 *
Jeff Layton29d01b22015-07-11 06:43:02 -04001965 * Apply a FLOCK style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04001967static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 int error;
1970 might_sleep();
1971 for (;;) {
Jeff Layton29d01b22015-07-11 06:43:02 -04001972 error = flock_lock_inode(inode, fl);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001973 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
NeilBrownada5c1d2018-11-30 10:04:08 +11001975 error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (!error)
1977 continue;
1978
1979 locks_delete_block(fl);
1980 break;
1981 }
1982 return error;
1983}
1984
Jeff Layton29d01b22015-07-11 06:43:02 -04001985/**
Benjamin Coddingtone55c34a2015-10-22 13:38:13 -04001986 * locks_lock_inode_wait - Apply a lock to an inode
1987 * @inode: inode of the file to apply to
1988 * @fl: The lock to be applied
1989 *
1990 * Apply a POSIX or FLOCK style lock request to an inode.
1991 */
1992int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1993{
1994 int res = 0;
1995 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
1996 case FL_POSIX:
1997 res = posix_lock_inode_wait(inode, fl);
1998 break;
1999 case FL_FLOCK:
2000 res = flock_lock_inode_wait(inode, fl);
2001 break;
2002 default:
2003 BUG();
2004 }
2005 return res;
2006}
2007EXPORT_SYMBOL(locks_lock_inode_wait);
2008
2009/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * sys_flock: - flock() system call.
2011 * @fd: the file descriptor to lock.
2012 * @cmd: the type of lock to apply.
2013 *
2014 * Apply a %FL_FLOCK style lock to an open file descriptor.
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04002015 * The @cmd can be one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 *
Mauro Carvalho Chehab80b79dd2017-05-27 06:07:18 -04002017 * - %LOCK_SH -- a shared lock.
2018 * - %LOCK_EX -- an exclusive lock.
2019 * - %LOCK_UN -- remove an existing lock.
2020 * - %LOCK_MAND -- a 'mandatory' flock.
2021 * This exists to emulate Windows Share Modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 *
2023 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
2024 * processes read and write access respectively.
2025 */
Heiko Carstens002c8972009-01-14 14:14:18 +01002026SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Al Viro2903ff02012-08-28 12:52:22 -04002028 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct file_lock *lock;
2030 int can_sleep, unlock;
2031 int error;
2032
2033 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04002034 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 goto out;
2036
2037 can_sleep = !(cmd & LOCK_NB);
2038 cmd &= ~LOCK_NB;
2039 unlock = (cmd == LOCK_UN);
2040
Al Viroaeb5d722008-09-02 15:28:45 -04002041 if (!unlock && !(cmd & LOCK_MAND) &&
Al Viro2903ff02012-08-28 12:52:22 -04002042 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 goto out_putf;
2044
NeilBrownd6367d62018-11-30 10:04:08 +11002045 lock = flock_make_lock(f.file, cmd, NULL);
Jeff Layton6e129d002014-09-04 10:25:06 -04002046 if (IS_ERR(lock)) {
2047 error = PTR_ERR(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 goto out_putf;
Jeff Layton6e129d002014-09-04 10:25:06 -04002049 }
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (can_sleep)
2052 lock->fl_flags |= FL_SLEEP;
2053
Al Viro2903ff02012-08-28 12:52:22 -04002054 error = security_file_lock(f.file, lock->fl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (error)
2056 goto out_free;
2057
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002058 if (f.file->f_op->flock)
Al Viro2903ff02012-08-28 12:52:22 -04002059 error = f.file->f_op->flock(f.file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 (can_sleep) ? F_SETLKW : F_SETLK,
2061 lock);
2062 else
Benjamin Coddington4f656362015-10-22 13:38:14 -04002063 error = locks_lock_file_wait(f.file, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 out_free:
Trond Myklebust993dfa82006-03-31 02:30:55 -08002066 locks_free_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 out_putf:
Al Viro2903ff02012-08-28 12:52:22 -04002069 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 out:
2071 return error;
2072}
2073
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002074/**
2075 * vfs_test_lock - test file byte range lock
2076 * @filp: The file to test lock for
J. Bruce Fields6924c552007-05-11 16:22:50 -04002077 * @fl: The lock to test; also used to hold result
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002078 *
2079 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2080 * setting conf->fl_type to something other than F_UNLCK.
2081 */
2082int vfs_test_lock(struct file *filp, struct file_lock *fl)
2083{
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002084 if (filp->f_op->lock)
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002085 return filp->f_op->lock(filp, F_GETLK, fl);
2086 posix_test_lock(filp, fl);
2087 return 0;
2088}
2089EXPORT_SYMBOL_GPL(vfs_test_lock);
2090
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002091/**
2092 * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
2093 * @fl: The file_lock who's fl_pid should be translated
2094 * @ns: The namespace into which the pid should be translated
2095 *
2096 * Used to tranlate a fl_pid into a namespace virtual pid number
2097 */
2098static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns)
2099{
2100 pid_t vnr;
2101 struct pid *pid;
2102
2103 if (IS_OFDLCK(fl))
2104 return -1;
2105 if (IS_REMOTELCK(fl))
2106 return fl->fl_pid;
Konstantin Khorenko826d7bc2018-06-08 17:27:11 +03002107 /*
2108 * If the flock owner process is dead and its pid has been already
2109 * freed, the translation below won't work, but we still want to show
2110 * flock owner pid number in init pidns.
2111 */
2112 if (ns == &init_pid_ns)
2113 return (pid_t)fl->fl_pid;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002114
2115 rcu_read_lock();
2116 pid = find_pid_ns(fl->fl_pid, &init_pid_ns);
2117 vnr = pid_nr_ns(pid, ns);
2118 rcu_read_unlock();
2119 return vnr;
2120}
2121
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002122static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2123{
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002124 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002125#if BITS_PER_LONG == 32
2126 /*
2127 * Make sure we can represent the posix lock via
2128 * legacy 32bit flock.
2129 */
2130 if (fl->fl_start > OFFT_OFFSET_MAX)
2131 return -EOVERFLOW;
2132 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2133 return -EOVERFLOW;
2134#endif
2135 flock->l_start = fl->fl_start;
2136 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2137 fl->fl_end - fl->fl_start + 1;
2138 flock->l_whence = 0;
J. Bruce Fields129a84d2007-05-10 18:38:43 -04002139 flock->l_type = fl->fl_type;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002140 return 0;
2141}
2142
2143#if BITS_PER_LONG == 32
2144static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2145{
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002146 flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002147 flock->l_start = fl->fl_start;
2148 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2149 fl->fl_end - fl->fl_start + 1;
2150 flock->l_whence = 0;
2151 flock->l_type = fl->fl_type;
2152}
2153#endif
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/* Report the first existing lock that would conflict with l.
2156 * This implements the F_GETLK command of fcntl().
2157 */
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002158int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Benjamin Coddington52306e82017-07-16 10:28:21 -04002160 struct file_lock *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int error;
2162
Benjamin Coddington52306e82017-07-16 10:28:21 -04002163 fl = locks_alloc_lock();
2164 if (fl == NULL)
2165 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002167 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto out;
2169
Benjamin Coddington52306e82017-07-16 10:28:21 -04002170 error = flock_to_posix_lock(filp, fl, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (error)
2172 goto out;
2173
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002174 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002175 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002176 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002177 goto out;
2178
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002179 cmd = F_GETLK;
Benjamin Coddington52306e82017-07-16 10:28:21 -04002180 fl->fl_flags |= FL_OFDLCK;
2181 fl->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002182 }
2183
Benjamin Coddington52306e82017-07-16 10:28:21 -04002184 error = vfs_test_lock(filp, fl);
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002185 if (error)
2186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Benjamin Coddington52306e82017-07-16 10:28:21 -04002188 flock->l_type = fl->fl_type;
2189 if (fl->fl_type != F_UNLCK) {
2190 error = posix_lock_to_flock(flock, fl);
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002191 if (error)
Benjamin Coddington52306e82017-07-16 10:28:21 -04002192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194out:
Benjamin Coddington52306e82017-07-16 10:28:21 -04002195 locks_free_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return error;
2197}
2198
Marc Eshel7723ec92007-01-18 15:08:55 -05002199/**
2200 * vfs_lock_file - file byte range lock
2201 * @filp: The file to apply the lock to
2202 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2203 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05002204 * @conf: Place to return a copy of the conflicting lock, if found.
2205 *
2206 * A caller that doesn't care about the conflicting lock may pass NULL
2207 * as the final argument.
2208 *
2209 * If the filesystem defines a private ->lock() method, then @conf will
2210 * be left unchanged; so a caller that cares should initialize it to
2211 * some acceptable default.
Marc Eshel2beb6612006-12-05 23:31:28 -05002212 *
2213 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2214 * locks, the ->lock() interface may return asynchronously, before the lock has
2215 * been granted or denied by the underlying filesystem, if (and only if)
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002216 * lm_grant is set. Callers expecting ->lock() to return asynchronously
Marc Eshel2beb6612006-12-05 23:31:28 -05002217 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
2218 * the request is for a blocking lock. When ->lock() does return asynchronously,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002219 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
Marc Eshel2beb6612006-12-05 23:31:28 -05002220 * request completes.
2221 * If the request is for non-blocking lock the file system should return
Miklos Szeredibde74e42008-07-25 01:48:57 -07002222 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2223 * with the result. If the request timed out the callback routine will return a
Marc Eshel2beb6612006-12-05 23:31:28 -05002224 * nonzero return code and the file system should release the lock. The file
2225 * system is also responsible to keep a corresponding posix lock when it
2226 * grants a lock so the VFS can find out which locks are locally held and do
2227 * the correct lock cleanup when required.
2228 * The underlying filesystem must not drop the kernel lock or call
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002229 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
Marc Eshel2beb6612006-12-05 23:31:28 -05002230 * return code.
Marc Eshel7723ec92007-01-18 15:08:55 -05002231 */
Marc Eshel150b3932007-01-18 16:15:35 -05002232int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
Marc Eshel7723ec92007-01-18 15:08:55 -05002233{
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002234 if (filp->f_op->lock)
Marc Eshel7723ec92007-01-18 15:08:55 -05002235 return filp->f_op->lock(filp, cmd, fl);
2236 else
Marc Eshel150b3932007-01-18 16:15:35 -05002237 return posix_lock_file(filp, fl, conf);
Marc Eshel7723ec92007-01-18 15:08:55 -05002238}
2239EXPORT_SYMBOL_GPL(vfs_lock_file);
2240
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002241static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2242 struct file_lock *fl)
2243{
2244 int error;
2245
2246 error = security_file_lock(filp, fl->fl_type);
2247 if (error)
2248 return error;
2249
Miklos Szeredi764c76b2008-07-25 01:48:58 -07002250 for (;;) {
2251 error = vfs_lock_file(filp, cmd, fl, NULL);
2252 if (error != FILE_LOCK_DEFERRED)
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002253 break;
NeilBrownada5c1d2018-11-30 10:04:08 +11002254 error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
Miklos Szeredi764c76b2008-07-25 01:48:58 -07002255 if (!error)
2256 continue;
2257
2258 locks_delete_block(fl);
2259 break;
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002260 }
2261
2262 return error;
2263}
2264
Benjamin Coddington6ca7d912015-10-15 09:07:07 -04002265/* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002266static int
2267check_fmode_for_setlk(struct file_lock *fl)
2268{
2269 switch (fl->fl_type) {
2270 case F_RDLCK:
2271 if (!(fl->fl_file->f_mode & FMODE_READ))
2272 return -EBADF;
2273 break;
2274 case F_WRLCK:
2275 if (!(fl->fl_file->f_mode & FMODE_WRITE))
2276 return -EBADF;
2277 }
2278 return 0;
2279}
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281/* Apply the lock described by l to an open file descriptor.
2282 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2283 */
Peter Staubachc2936212005-07-27 11:45:09 -07002284int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002285 struct flock *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct file_lock *file_lock = locks_alloc_lock();
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002288 struct inode *inode = locks_inode(filp);
Al Viro0b2bac22008-05-06 13:58:34 -04002289 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int error;
2291
2292 if (file_lock == NULL)
2293 return -ENOLCK;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* Don't allow mandatory locks on files that may be memory mapped
2296 * and shared.
2297 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002298 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 error = -EAGAIN;
2300 goto out;
2301 }
2302
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002303 error = flock_to_posix_lock(filp, file_lock, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (error)
2305 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002306
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002307 error = check_fmode_for_setlk(file_lock);
2308 if (error)
2309 goto out;
2310
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002311 /*
2312 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002313 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002314 */
2315 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002316 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002317 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002318 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002319 goto out;
2320
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002321 cmd = F_SETLK;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002322 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002323 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002324 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002325 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002326 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002327 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002328 goto out;
2329
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002330 cmd = F_SETLKW;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002331 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002332 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002333 /* Fallthrough */
2334 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 file_lock->fl_flags |= FL_SLEEP;
2336 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002337
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002338 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Peter Staubachc2936212005-07-27 11:45:09 -07002340 /*
Jeff Layton0752ba82016-01-08 07:30:43 -05002341 * Attempt to detect a close/fcntl race and recover by releasing the
2342 * lock that was just acquired. There is no need to do that when we're
2343 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002344 */
Jeff Layton0752ba82016-01-08 07:30:43 -05002345 if (!error && file_lock->fl_type != F_UNLCK &&
2346 !(file_lock->fl_flags & FL_OFDLCK)) {
Jeff Layton7f3697e2016-01-07 16:38:10 -05002347 /*
2348 * We need that spin_lock here - it prevents reordering between
2349 * update of i_flctx->flc_posix and check for it done in
2350 * close(). rcu_read_lock() wouldn't do.
2351 */
2352 spin_lock(&current->files->file_lock);
2353 f = fcheck(fd);
2354 spin_unlock(&current->files->file_lock);
2355 if (f != filp) {
2356 file_lock->fl_type = F_UNLCK;
2357 error = do_lock_file_wait(filp, cmd, file_lock);
2358 WARN_ON_ONCE(error);
2359 error = -EBADF;
2360 }
Peter Staubachc2936212005-07-27 11:45:09 -07002361 }
Peter Staubachc2936212005-07-27 11:45:09 -07002362out:
Jeff Layton18909102016-01-06 21:26:10 -05002363 trace_fcntl_setlk(inode, file_lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 locks_free_lock(file_lock);
2365 return error;
2366}
2367
2368#if BITS_PER_LONG == 32
2369/* Report the first existing lock that would conflict with l.
2370 * This implements the F_GETLK command of fcntl().
2371 */
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002372int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Benjamin Coddington52306e82017-07-16 10:28:21 -04002374 struct file_lock *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int error;
2376
Benjamin Coddington52306e82017-07-16 10:28:21 -04002377 fl = locks_alloc_lock();
2378 if (fl == NULL)
2379 return -ENOMEM;
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002382 if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 goto out;
2384
Benjamin Coddington52306e82017-07-16 10:28:21 -04002385 error = flock64_to_posix_lock(filp, fl, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (error)
2387 goto out;
2388
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002389 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002390 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002391 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002392 goto out;
2393
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002394 cmd = F_GETLK64;
Benjamin Coddington52306e82017-07-16 10:28:21 -04002395 fl->fl_flags |= FL_OFDLCK;
2396 fl->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002397 }
2398
Benjamin Coddington52306e82017-07-16 10:28:21 -04002399 error = vfs_test_lock(filp, fl);
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002400 if (error)
2401 goto out;
2402
Benjamin Coddington52306e82017-07-16 10:28:21 -04002403 flock->l_type = fl->fl_type;
2404 if (fl->fl_type != F_UNLCK)
2405 posix_lock_to_flock64(flock, fl);
Kinglong Meef3282962014-08-22 10:18:43 -04002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407out:
Benjamin Coddington52306e82017-07-16 10:28:21 -04002408 locks_free_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return error;
2410}
2411
2412/* Apply the lock described by l to an open file descriptor.
2413 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2414 */
Peter Staubachc2936212005-07-27 11:45:09 -07002415int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002416 struct flock64 *flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 struct file_lock *file_lock = locks_alloc_lock();
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002419 struct inode *inode = locks_inode(filp);
Al Viro0b2bac22008-05-06 13:58:34 -04002420 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 int error;
2422
2423 if (file_lock == NULL)
2424 return -ENOLCK;
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* Don't allow mandatory locks on files that may be memory mapped
2427 * and shared.
2428 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002429 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 error = -EAGAIN;
2431 goto out;
2432 }
2433
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002434 error = flock64_to_posix_lock(filp, file_lock, flock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (error)
2436 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002437
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002438 error = check_fmode_for_setlk(file_lock);
2439 if (error)
2440 goto out;
2441
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002442 /*
2443 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002444 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002445 */
2446 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002447 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002448 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002449 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002450 goto out;
2451
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002452 cmd = F_SETLK64;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002453 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002454 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002455 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002456 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002457 error = -EINVAL;
Christoph Hellwiga75d30c2017-05-27 06:07:19 -04002458 if (flock->l_pid != 0)
Jeff Layton90478932014-03-04 10:30:23 -05002459 goto out;
2460
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002461 cmd = F_SETLKW64;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002462 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002463 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002464 /* Fallthrough */
2465 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 file_lock->fl_flags |= FL_SLEEP;
2467 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002468
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002469 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Peter Staubachc2936212005-07-27 11:45:09 -07002471 /*
Jeff Layton0752ba82016-01-08 07:30:43 -05002472 * Attempt to detect a close/fcntl race and recover by releasing the
2473 * lock that was just acquired. There is no need to do that when we're
2474 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002475 */
Jeff Layton0752ba82016-01-08 07:30:43 -05002476 if (!error && file_lock->fl_type != F_UNLCK &&
2477 !(file_lock->fl_flags & FL_OFDLCK)) {
Jeff Layton7f3697e2016-01-07 16:38:10 -05002478 /*
2479 * We need that spin_lock here - it prevents reordering between
2480 * update of i_flctx->flc_posix and check for it done in
2481 * close(). rcu_read_lock() wouldn't do.
2482 */
2483 spin_lock(&current->files->file_lock);
2484 f = fcheck(fd);
2485 spin_unlock(&current->files->file_lock);
2486 if (f != filp) {
2487 file_lock->fl_type = F_UNLCK;
2488 error = do_lock_file_wait(filp, cmd, file_lock);
2489 WARN_ON_ONCE(error);
2490 error = -EBADF;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493out:
2494 locks_free_lock(file_lock);
2495 return error;
2496}
2497#endif /* BITS_PER_LONG == 32 */
2498
2499/*
2500 * This function is called when the file is being removed
2501 * from the task's fd array. POSIX locks belonging to this task
2502 * are deleted at this time.
2503 */
2504void locks_remove_posix(struct file *filp, fl_owner_t owner)
2505{
Jeff Layton18909102016-01-06 21:26:10 -05002506 int error;
Miklos Szeredic568d682016-09-16 12:44:20 +02002507 struct inode *inode = locks_inode(filp);
Miklos Szerediff7b86b2006-06-23 02:05:11 -07002508 struct file_lock lock;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002509 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /*
2512 * If there are no locks held on this file, we don't need to call
2513 * posix_lock_file(). Another process could be setting a lock on this
2514 * file at the same time, but we wouldn't remove that lock anyway.
2515 */
Miklos Szeredic568d682016-09-16 12:44:20 +02002516 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05002517 if (!ctx || list_empty(&ctx->flc_posix))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return;
2519
NeilBrownd6367d62018-11-30 10:04:08 +11002520 locks_init_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 lock.fl_type = F_UNLCK;
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07002522 lock.fl_flags = FL_POSIX | FL_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 lock.fl_start = 0;
2524 lock.fl_end = OFFSET_MAX;
2525 lock.fl_owner = owner;
2526 lock.fl_pid = current->tgid;
2527 lock.fl_file = filp;
2528 lock.fl_ops = NULL;
2529 lock.fl_lmops = NULL;
2530
Jeff Layton18909102016-01-06 21:26:10 -05002531 error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2534 lock.fl_ops->fl_release_private(&lock);
Miklos Szeredic568d682016-09-16 12:44:20 +02002535 trace_locks_remove_posix(inode, &lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538EXPORT_SYMBOL(locks_remove_posix);
2539
Jeff Layton3d8e5602015-01-16 15:05:58 -05002540/* The i_flctx must be valid when calling into here */
Jeff Laytondd459bb2015-01-16 15:05:54 -05002541static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002542locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
Jeff Laytondd459bb2015-01-16 15:05:54 -05002543{
NeilBrownd6367d62018-11-30 10:04:08 +11002544 struct file_lock fl;
Miklos Szeredic568d682016-09-16 12:44:20 +02002545 struct inode *inode = locks_inode(filp);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002546
Jeff Layton3d8e5602015-01-16 15:05:58 -05002547 if (list_empty(&flctx->flc_flock))
Jeff Laytondd459bb2015-01-16 15:05:54 -05002548 return;
2549
NeilBrownd6367d62018-11-30 10:04:08 +11002550 flock_make_lock(filp, LOCK_UN, &fl);
2551 fl.fl_flags |= FL_CLOSE;
2552
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002553 if (filp->f_op->flock)
Jeff Laytondd459bb2015-01-16 15:05:54 -05002554 filp->f_op->flock(filp, F_SETLKW, &fl);
2555 else
Jeff Laytonbcd7f78d2015-07-11 06:43:02 -04002556 flock_lock_inode(inode, &fl);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002557
2558 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2559 fl.fl_ops->fl_release_private(&fl);
2560}
2561
Jeff Layton3d8e5602015-01-16 15:05:58 -05002562/* The i_flctx must be valid when calling into here */
Jeff Layton8634b512015-01-16 15:05:55 -05002563static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002564locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
Jeff Layton8634b512015-01-16 15:05:55 -05002565{
Jeff Layton8634b512015-01-16 15:05:55 -05002566 struct file_lock *fl, *tmp;
2567 LIST_HEAD(dispose);
2568
Jeff Layton3d8e5602015-01-16 15:05:58 -05002569 if (list_empty(&ctx->flc_lease))
Jeff Layton8634b512015-01-16 15:05:55 -05002570 return;
2571
Peter Zijlstra5f430862016-10-08 10:12:28 +02002572 percpu_down_read_preempt_disable(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05002573 spin_lock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05002574 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
Jeff Laytonc4e136c2015-02-16 19:37:42 -05002575 if (filp == fl->fl_file)
2576 lease_modify(fl, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05002577 spin_unlock(&ctx->flc_lock);
Peter Zijlstra5f430862016-10-08 10:12:28 +02002578 percpu_up_read_preempt_enable(&file_rwsem);
2579
Jeff Layton8634b512015-01-16 15:05:55 -05002580 locks_dispose_list(&dispose);
2581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/*
2584 * This function is called on the last close of an open file.
2585 */
Jeff Layton78ed8a12014-02-03 12:13:08 -05002586void locks_remove_file(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002588 struct file_lock_context *ctx;
2589
Miklos Szeredic568d682016-09-16 12:44:20 +02002590 ctx = smp_load_acquire(&locks_inode(filp)->i_flctx);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002591 if (!ctx)
Jeff Layton3d8e5602015-01-16 15:05:58 -05002592 return;
2593
Jeff Laytondd459bb2015-01-16 15:05:54 -05002594 /* remove any OFD locks */
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002595 locks_remove_posix(filp, filp);
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002596
Jeff Laytondd459bb2015-01-16 15:05:54 -05002597 /* remove flock locks */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002598 locks_remove_flock(filp, ctx);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002599
Jeff Layton8634b512015-01-16 15:05:55 -05002600 /* remove any leases */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002601 locks_remove_lease(filp, ctx);
Benjamin Coddington39537042017-07-21 13:36:25 -04002602
2603 spin_lock(&ctx->flc_lock);
2604 locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
2605 locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
2606 locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
2607 spin_unlock(&ctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
2610/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 * posix_unblock_lock - stop waiting for a file lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 * @waiter: the lock which was waiting
2613 *
2614 * lockd needs to block waiting for locks.
2615 */
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002616int
Jeff Laytonf891a292013-06-21 08:58:09 -04002617posix_unblock_lock(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
NeilBrown5946c432018-11-30 10:04:08 +11002619 int status = -ENOENT;
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002620
Jeff Layton7b2296a2013-06-21 08:58:20 -04002621 spin_lock(&blocked_lock_lock);
NeilBrown5946c432018-11-30 10:04:08 +11002622 if (waiter->fl_blocker) {
2623 __locks_wake_up_blocks(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 __locks_delete_block(waiter);
NeilBrown5946c432018-11-30 10:04:08 +11002625 status = 0;
2626 }
Jeff Layton7b2296a2013-06-21 08:58:20 -04002627 spin_unlock(&blocked_lock_lock);
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002628 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630EXPORT_SYMBOL(posix_unblock_lock);
2631
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002632/**
2633 * vfs_cancel_lock - file byte range unblock lock
2634 * @filp: The file to apply the unblock to
2635 * @fl: The lock to be unblocked
2636 *
2637 * Used by lock managers to cancel blocked requests
2638 */
2639int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2640{
Miklos Szeredide2a4a52018-07-18 15:44:43 +02002641 if (filp->f_op->lock)
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002642 return filp->f_op->lock(filp, F_CANCELLK, fl);
2643 return 0;
2644}
2645
2646EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2647
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002648#ifdef CONFIG_PROC_FS
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002649#include <linux/proc_fs.h>
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002650#include <linux/seq_file.h>
2651
Jeff Layton7012b022013-06-21 08:58:22 -04002652struct locks_iterator {
2653 int li_cpu;
2654 loff_t li_pos;
2655};
2656
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002657static void lock_get_status(struct seq_file *f, struct file_lock *fl,
Jerome Marchand99dc8292010-10-26 14:22:33 -07002658 loff_t id, char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 struct inode *inode = NULL;
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002661 unsigned int fl_pid;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002662 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002663
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002664 fl_pid = locks_translate_pid(fl, proc_pidns);
2665 /*
Konstantin Khorenko1cf8e5d2018-06-08 17:27:12 +03002666 * If lock owner is dead (and pid is freed) or not visible in current
2667 * pidns, zero is shown as a pid value. Check lock info from
2668 * init_pid_ns to get saved lock pid value.
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 if (fl->fl_file != NULL)
Miklos Szeredic568d682016-09-16 12:44:20 +02002672 inode = locks_inode(fl->fl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Jerome Marchand99dc8292010-10-26 14:22:33 -07002674 seq_printf(f, "%lld:%s ", id, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (IS_POSIX(fl)) {
Jeff Laytonc918d422014-02-03 12:13:09 -05002676 if (fl->fl_flags & FL_ACCESS)
Fabian Frederick5315c262014-05-09 14:13:05 -04002677 seq_puts(f, "ACCESS");
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002678 else if (IS_OFDLCK(fl))
Fabian Frederick5315c262014-05-09 14:13:05 -04002679 seq_puts(f, "OFDLCK");
Jeff Laytonc918d422014-02-03 12:13:09 -05002680 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002681 seq_puts(f, "POSIX ");
Jeff Laytonc918d422014-02-03 12:13:09 -05002682
2683 seq_printf(f, " %s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 (inode == NULL) ? "*NOINODE*" :
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002685 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 } else if (IS_FLOCK(fl)) {
2687 if (fl->fl_type & LOCK_MAND) {
Fabian Frederick5315c262014-05-09 14:13:05 -04002688 seq_puts(f, "FLOCK MSNFS ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002690 seq_puts(f, "FLOCK ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692 } else if (IS_LEASE(fl)) {
Jeff Layton8144f1f2014-08-11 13:36:54 -04002693 if (fl->fl_flags & FL_DELEG)
2694 seq_puts(f, "DELEG ");
2695 else
2696 seq_puts(f, "LEASE ");
2697
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002698 if (lease_breaking(fl))
Fabian Frederick5315c262014-05-09 14:13:05 -04002699 seq_puts(f, "BREAKING ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 else if (fl->fl_file)
Fabian Frederick5315c262014-05-09 14:13:05 -04002701 seq_puts(f, "ACTIVE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002703 seq_puts(f, "BREAKER ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002705 seq_puts(f, "UNKNOWN UNKNOWN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707 if (fl->fl_type & LOCK_MAND) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002708 seq_printf(f, "%s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 (fl->fl_type & LOCK_READ)
2710 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2711 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2712 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002713 seq_printf(f, "%s ",
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002714 (lease_breaking(fl))
Jeff Layton0ee5c6d2012-08-02 15:46:30 -04002715 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2716 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718 if (inode) {
Jeff Layton36488882015-04-03 09:04:04 -04002719 /* userspace relies on this representation of dev_t */
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002720 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 MAJOR(inode->i_sb->s_dev),
2722 MINOR(inode->i_sb->s_dev), inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 } else {
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002724 seq_printf(f, "%d <none>:0 ", fl_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726 if (IS_POSIX(fl)) {
2727 if (fl->fl_end == OFFSET_MAX)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002728 seq_printf(f, "%Ld EOF\n", fl->fl_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 else
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002730 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002732 seq_puts(f, "0 EOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734}
2735
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002736static int locks_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Jeff Layton7012b022013-06-21 08:58:22 -04002738 struct locks_iterator *iter = f->private;
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002739 struct file_lock *fl, *bfl;
Nikolay Borisovd67fd442016-08-17 16:18:46 -04002740 struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002741
Jeff Layton139ca042013-06-21 08:58:17 -04002742 fl = hlist_entry(v, struct file_lock, fl_link);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002743
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002744 if (locks_translate_pid(fl, proc_pidns) == 0)
Nikolay Borisovd67fd442016-08-17 16:18:46 -04002745 return 0;
2746
Jeff Layton7012b022013-06-21 08:58:22 -04002747 lock_get_status(f, fl, iter->li_pos, "");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002748
NeilBrownada5c1d2018-11-30 10:04:08 +11002749 list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member)
Jeff Layton7012b022013-06-21 08:58:22 -04002750 lock_get_status(f, bfl, iter->li_pos, " ->");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002751
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002755static void __show_fd_locks(struct seq_file *f,
2756 struct list_head *head, int *id,
2757 struct file *filp, struct files_struct *files)
2758{
2759 struct file_lock *fl;
2760
2761 list_for_each_entry(fl, head, fl_list) {
2762
2763 if (filp != fl->fl_file)
2764 continue;
2765 if (fl->fl_owner != files &&
2766 fl->fl_owner != filp)
2767 continue;
2768
2769 (*id)++;
2770 seq_puts(f, "lock:\t");
2771 lock_get_status(f, fl, *id, "");
2772 }
2773}
2774
2775void show_fd_locks(struct seq_file *f,
2776 struct file *filp, struct files_struct *files)
2777{
Miklos Szeredic568d682016-09-16 12:44:20 +02002778 struct inode *inode = locks_inode(filp);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002779 struct file_lock_context *ctx;
2780 int id = 0;
2781
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002782 ctx = smp_load_acquire(&inode->i_flctx);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002783 if (!ctx)
2784 return;
2785
2786 spin_lock(&ctx->flc_lock);
2787 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
2788 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
2789 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
2790 spin_unlock(&ctx->flc_lock);
2791}
2792
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002793static void *locks_start(struct seq_file *f, loff_t *pos)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002794 __acquires(&blocked_lock_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Jeff Layton7012b022013-06-21 08:58:22 -04002796 struct locks_iterator *iter = f->private;
Jerome Marchand99dc8292010-10-26 14:22:33 -07002797
Jeff Layton7012b022013-06-21 08:58:22 -04002798 iter->li_pos = *pos + 1;
Peter Zijlstraaba37662015-06-22 14:16:33 +02002799 percpu_down_write(&file_rwsem);
Jeff Layton7b2296a2013-06-21 08:58:20 -04002800 spin_lock(&blocked_lock_lock);
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002801 return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002804static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2805{
Jeff Layton7012b022013-06-21 08:58:22 -04002806 struct locks_iterator *iter = f->private;
2807
2808 ++iter->li_pos;
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002809 return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002810}
2811
2812static void locks_stop(struct seq_file *f, void *v)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002813 __releases(&blocked_lock_lock)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002814{
Jeff Layton7b2296a2013-06-21 08:58:20 -04002815 spin_unlock(&blocked_lock_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02002816 percpu_up_write(&file_rwsem);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002817}
2818
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002819static const struct seq_operations locks_seq_operations = {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002820 .start = locks_start,
2821 .next = locks_next,
2822 .stop = locks_stop,
2823 .show = locks_show,
2824};
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002825
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002826static int __init proc_locks_init(void)
2827{
Christoph Hellwig44414d82018-04-24 17:05:17 +02002828 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
2829 sizeof(struct locks_iterator), NULL);
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002830 return 0;
2831}
Paul Gortmaker91899222015-12-17 14:11:03 -05002832fs_initcall(proc_locks_init);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002833#endif
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835static int __init filelock_init(void)
2836{
Jeff Layton7012b022013-06-21 08:58:22 -04002837 int i;
2838
Jeff Layton4a075e32015-01-16 15:05:54 -05002839 flctx_cache = kmem_cache_create("file_lock_ctx",
2840 sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 filelock_cache = kmem_cache_create("file_lock_cache",
Miklos Szerediee19cc42011-07-07 13:06:09 +02002843 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2844
Jeff Layton7012b022013-06-21 08:58:22 -04002845
Peter Zijlstra7c3f6542015-06-22 14:16:34 +02002846 for_each_possible_cpu(i) {
2847 struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
2848
2849 spin_lock_init(&fll->lock);
2850 INIT_HLIST_HEAD(&fll->hlist);
2851 }
Jeff Layton7012b022013-06-21 08:58:22 -04002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
2854}
2855
2856core_initcall(filelock_init);