blob: 8f609ec03364c6aafc56f485a6bc491d914d2ecb [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>
130#include <linux/lglock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jeff Layton62af4f12014-05-09 14:13:05 -0400132#define CREATE_TRACE_POINTS
133#include <trace/events/filelock.h>
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <asm/uaccess.h>
136
137#define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
138#define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
Christoph Hellwig11afe9f2015-01-21 19:17:03 +0100139#define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400140#define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
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
161 * keep a list on each CPU, with each list protected by its own spinlock via
162 * the file_lock_lglock. Note that alterations to the list also require that
Jeff Layton6109c852015-01-16 15:05:57 -0500163 * the relevant flc_lock is held.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400164 */
Jeff Layton7012b022013-06-21 08:58:22 -0400165DEFINE_STATIC_LGLOCK(file_lock_lglock);
166static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
Peter Zijlstraaba37662015-06-22 14:16:33 +0200167DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
Jeff Layton88974692013-06-21 08:58:14 -0400168
Jeff Layton1c8c6012013-06-21 08:58:15 -0400169/*
Jeff Layton48f74182013-06-21 08:58:18 -0400170 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
Jeff Layton7b2296a2013-06-21 08:58:20 -0400171 * It is protected by blocked_lock_lock.
Jeff Layton48f74182013-06-21 08:58:18 -0400172 *
173 * We hash locks by lockowner in order to optimize searching for the lock a
174 * particular lockowner is waiting on.
175 *
176 * FIXME: make this value scale via some heuristic? We generally will want more
177 * buckets when we have more lockowners holding locks, but that's a little
178 * difficult to determine without knowing what the workload will look like.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400179 */
Jeff Layton48f74182013-06-21 08:58:18 -0400180#define BLOCKED_HASH_BITS 7
181static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
Jeff Layton88974692013-06-21 08:58:14 -0400182
Jeff Layton1c8c6012013-06-21 08:58:15 -0400183/*
Jeff Layton7b2296a2013-06-21 08:58:20 -0400184 * This lock protects the blocked_hash. Generally, if you're accessing it, you
185 * want to be holding this lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400186 *
187 * In addition, it also protects the fl->fl_block list, and the fl->fl_next
188 * pointer for file_lock structures that are acting as lock requests (in
189 * contrast to those that are acting as records of acquired locks).
190 *
191 * Note that when we acquire this lock in order to change the above fields,
Jeff Layton6109c852015-01-16 15:05:57 -0500192 * we often hold the flc_lock as well. In certain cases, when reading the fields
Jeff Layton1c8c6012013-06-21 08:58:15 -0400193 * protected by this lock, we can skip acquiring it iff we already hold the
Jeff Layton6109c852015-01-16 15:05:57 -0500194 * flc_lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400195 *
196 * In particular, adding an entry to the fl_block list requires that you hold
Jeff Layton6109c852015-01-16 15:05:57 -0500197 * both the flc_lock and the blocked_lock_lock (acquired in that order).
198 * Deleting an entry from the list however only requires the file_lock_lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400199 */
Jeff Layton7b2296a2013-06-21 08:58:20 -0400200static DEFINE_SPINLOCK(blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jeff Layton4a075e32015-01-16 15:05:54 -0500202static struct kmem_cache *flctx_cache __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -0800203static struct kmem_cache *filelock_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jeff Layton4a075e32015-01-16 15:05:54 -0500205static struct file_lock_context *
Jeff Layton5c1c6692015-04-03 09:04:03 -0400206locks_get_lock_context(struct inode *inode, int type)
Jeff Layton4a075e32015-01-16 15:05:54 -0500207{
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200208 struct file_lock_context *ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500209
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200210 /* paired with cmpxchg() below */
211 ctx = smp_load_acquire(&inode->i_flctx);
212 if (likely(ctx) || type == F_UNLCK)
Jeff Layton4a075e32015-01-16 15:05:54 -0500213 goto out;
214
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200215 ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
216 if (!ctx)
Jeff Layton4a075e32015-01-16 15:05:54 -0500217 goto out;
218
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200219 spin_lock_init(&ctx->flc_lock);
220 INIT_LIST_HEAD(&ctx->flc_flock);
221 INIT_LIST_HEAD(&ctx->flc_posix);
222 INIT_LIST_HEAD(&ctx->flc_lease);
Jeff Layton4a075e32015-01-16 15:05:54 -0500223
224 /*
225 * Assign the pointer if it's not already assigned. If it is, then
226 * free the context we just allocated.
227 */
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200228 if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
229 kmem_cache_free(flctx_cache, ctx);
230 ctx = smp_load_acquire(&inode->i_flctx);
231 }
Jeff Layton4a075e32015-01-16 15:05:54 -0500232out:
Jeff Layton18909102016-01-06 21:26:10 -0500233 trace_locks_get_lock_context(inode, type, ctx);
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200234 return ctx;
Jeff Layton4a075e32015-01-16 15:05:54 -0500235}
236
Jeff Laytone24dada2016-01-06 21:28:41 -0500237static void
238locks_dump_ctx_list(struct list_head *list, char *list_type)
239{
240 struct file_lock *fl;
241
242 list_for_each_entry(fl, list, fl_list) {
243 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);
244 }
245}
246
247static void
248locks_check_ctx_lists(struct inode *inode)
249{
250 struct file_lock_context *ctx = inode->i_flctx;
251
252 if (unlikely(!list_empty(&ctx->flc_flock) ||
253 !list_empty(&ctx->flc_posix) ||
254 !list_empty(&ctx->flc_lease))) {
255 pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
256 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
257 inode->i_ino);
258 locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
259 locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
260 locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
261 }
262}
263
Jeff Layton4a075e32015-01-16 15:05:54 -0500264void
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500265locks_free_lock_context(struct inode *inode)
Jeff Layton4a075e32015-01-16 15:05:54 -0500266{
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500267 struct file_lock_context *ctx = inode->i_flctx;
268
Jeff Laytone24dada2016-01-06 21:28:41 -0500269 if (unlikely(ctx)) {
270 locks_check_ctx_lists(inode);
Jeff Layton4a075e32015-01-16 15:05:54 -0500271 kmem_cache_free(flctx_cache, ctx);
272 }
273}
274
Miklos Szerediee19cc42011-07-07 13:06:09 +0200275static void locks_init_lock_heads(struct file_lock *fl)
Miklos Szeredia51cb912011-07-06 12:33:55 +0200276{
Jeff Layton139ca042013-06-21 08:58:17 -0400277 INIT_HLIST_NODE(&fl->fl_link);
Jeff Layton6dee60f2015-01-16 15:05:54 -0500278 INIT_LIST_HEAD(&fl->fl_list);
Miklos Szerediee19cc42011-07-07 13:06:09 +0200279 INIT_LIST_HEAD(&fl->fl_block);
280 init_waitqueue_head(&fl->fl_wait);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/* Allocate an empty lock structure. */
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200284struct file_lock *locks_alloc_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200286 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200287
288 if (fl)
Miklos Szerediee19cc42011-07-07 13:06:09 +0200289 locks_init_lock_heads(fl);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200290
291 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200293EXPORT_SYMBOL_GPL(locks_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500295void locks_release_private(struct file_lock *fl)
Trond Myklebust47831f32006-03-20 13:44:05 -0500296{
297 if (fl->fl_ops) {
298 if (fl->fl_ops->fl_release_private)
299 fl->fl_ops->fl_release_private(fl);
300 fl->fl_ops = NULL;
301 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500302
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400303 if (fl->fl_lmops) {
Jeff Laytoncae80b32015-04-03 09:04:04 -0400304 if (fl->fl_lmops->lm_put_owner) {
305 fl->fl_lmops->lm_put_owner(fl->fl_owner);
306 fl->fl_owner = NULL;
307 }
Kinglong Mee5c97d7b2014-08-22 10:18:43 -0400308 fl->fl_lmops = NULL;
309 }
Trond Myklebust47831f32006-03-20 13:44:05 -0500310}
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500311EXPORT_SYMBOL_GPL(locks_release_private);
Trond Myklebust47831f32006-03-20 13:44:05 -0500312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/* Free a lock which is not in use. */
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400314void locks_free_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Miklos Szeredi5ce29642006-03-31 02:30:29 -0800316 BUG_ON(waitqueue_active(&fl->fl_wait));
Jeff Layton6dee60f2015-01-16 15:05:54 -0500317 BUG_ON(!list_empty(&fl->fl_list));
Miklos Szeredi5ce29642006-03-31 02:30:29 -0800318 BUG_ON(!list_empty(&fl->fl_block));
Jeff Layton139ca042013-06-21 08:58:17 -0400319 BUG_ON(!hlist_unhashed(&fl->fl_link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Trond Myklebust47831f32006-03-20 13:44:05 -0500321 locks_release_private(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 kmem_cache_free(filelock_cache, fl);
323}
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400324EXPORT_SYMBOL(locks_free_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jeff Laytoned9814d2014-08-11 14:20:31 -0400326static void
327locks_dispose_list(struct list_head *dispose)
328{
329 struct file_lock *fl;
330
331 while (!list_empty(dispose)) {
Jeff Layton6dee60f2015-01-16 15:05:54 -0500332 fl = list_first_entry(dispose, struct file_lock, fl_list);
333 list_del_init(&fl->fl_list);
Jeff Laytoned9814d2014-08-11 14:20:31 -0400334 locks_free_lock(fl);
335 }
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338void locks_init_lock(struct file_lock *fl)
339{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200340 memset(fl, 0, sizeof(struct file_lock));
341 locks_init_lock_heads(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344EXPORT_SYMBOL(locks_init_lock);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * Initialize a new lock from an existing file_lock structure.
348 */
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400349void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 new->fl_owner = fl->fl_owner;
352 new->fl_pid = fl->fl_pid;
Trond Myklebust09969052006-03-20 13:44:38 -0500353 new->fl_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 new->fl_flags = fl->fl_flags;
355 new->fl_type = fl->fl_type;
356 new->fl_start = fl->fl_start;
357 new->fl_end = fl->fl_end;
Kinglong Meef3282962014-08-22 10:18:43 -0400358 new->fl_lmops = fl->fl_lmops;
Trond Myklebust09969052006-03-20 13:44:38 -0500359 new->fl_ops = NULL;
Kinglong Meef3282962014-08-22 10:18:43 -0400360
361 if (fl->fl_lmops) {
362 if (fl->fl_lmops->lm_get_owner)
Jeff Laytoncae80b32015-04-03 09:04:04 -0400363 fl->fl_lmops->lm_get_owner(fl->fl_owner);
Kinglong Meef3282962014-08-22 10:18:43 -0400364 }
Trond Myklebust09969052006-03-20 13:44:38 -0500365}
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400366EXPORT_SYMBOL(locks_copy_conflock);
Trond Myklebust09969052006-03-20 13:44:38 -0500367
368void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
369{
Jeff Layton566709b2014-08-11 14:09:35 -0400370 /* "new" must be a freshly-initialized lock */
371 WARN_ON_ONCE(new->fl_ops);
Trond Myklebust09969052006-03-20 13:44:38 -0500372
Kinglong Mee3fe0fff12014-08-22 10:18:42 -0400373 locks_copy_conflock(new, fl);
Kinglong Meef3282962014-08-22 10:18:43 -0400374
Trond Myklebust09969052006-03-20 13:44:38 -0500375 new->fl_file = fl->fl_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 new->fl_ops = fl->fl_ops;
Trond Myklebust47831f32006-03-20 13:44:05 -0500377
Kinglong Meef3282962014-08-22 10:18:43 -0400378 if (fl->fl_ops) {
379 if (fl->fl_ops->fl_copy_lock)
380 fl->fl_ops->fl_copy_lock(new, fl);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384EXPORT_SYMBOL(locks_copy_lock);
385
386static inline int flock_translate_cmd(int cmd) {
387 if (cmd & LOCK_MAND)
388 return cmd & (LOCK_MAND | LOCK_RW);
389 switch (cmd) {
390 case LOCK_SH:
391 return F_RDLCK;
392 case LOCK_EX:
393 return F_WRLCK;
394 case LOCK_UN:
395 return F_UNLCK;
396 }
397 return -EINVAL;
398}
399
400/* Fill in a file_lock structure with an appropriate FLOCK lock. */
Jeff Layton6e129d002014-09-04 10:25:06 -0400401static struct file_lock *
402flock_make_lock(struct file *filp, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct file_lock *fl;
405 int type = flock_translate_cmd(cmd);
Jeff Layton6e129d002014-09-04 10:25:06 -0400406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (type < 0)
Jeff Layton6e129d002014-09-04 10:25:06 -0400408 return ERR_PTR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 fl = locks_alloc_lock();
411 if (fl == NULL)
Jeff Layton6e129d002014-09-04 10:25:06 -0400412 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 fl->fl_file = filp;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +0200415 fl->fl_owner = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 fl->fl_pid = current->tgid;
417 fl->fl_flags = FL_FLOCK;
418 fl->fl_type = type;
419 fl->fl_end = OFFSET_MAX;
420
Jeff Layton6e129d002014-09-04 10:25:06 -0400421 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400424static int assign_type(struct file_lock *fl, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 switch (type) {
427 case F_RDLCK:
428 case F_WRLCK:
429 case F_UNLCK:
430 fl->fl_type = type;
431 break;
432 default:
433 return -EINVAL;
434 }
435 return 0;
436}
437
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500438static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
439 struct flock64 *l)
440{
441 switch (l->l_whence) {
442 case SEEK_SET:
443 fl->fl_start = 0;
444 break;
445 case SEEK_CUR:
446 fl->fl_start = filp->f_pos;
447 break;
448 case SEEK_END:
449 fl->fl_start = i_size_read(file_inode(filp));
450 break;
451 default:
452 return -EINVAL;
453 }
454 if (l->l_start > OFFSET_MAX - fl->fl_start)
455 return -EOVERFLOW;
456 fl->fl_start += l->l_start;
457 if (fl->fl_start < 0)
458 return -EINVAL;
459
460 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
461 POSIX-2001 defines it. */
462 if (l->l_len > 0) {
463 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
464 return -EOVERFLOW;
465 fl->fl_end = fl->fl_start + l->l_len - 1;
466
467 } else if (l->l_len < 0) {
468 if (fl->fl_start + l->l_len < 0)
469 return -EINVAL;
470 fl->fl_end = fl->fl_start - 1;
471 fl->fl_start += l->l_len;
472 } else
473 fl->fl_end = OFFSET_MAX;
474
475 fl->fl_owner = current->files;
476 fl->fl_pid = current->tgid;
477 fl->fl_file = filp;
478 fl->fl_flags = FL_POSIX;
479 fl->fl_ops = NULL;
480 fl->fl_lmops = NULL;
481
482 return assign_type(fl, l->l_type);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
486 * style lock.
487 */
488static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
489 struct flock *l)
490{
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500491 struct flock64 ll = {
492 .l_type = l->l_type,
493 .l_whence = l->l_whence,
494 .l_start = l->l_start,
495 .l_len = l->l_len,
496 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
J. Bruce Fieldsef12e722014-02-03 12:13:08 -0500498 return flock64_to_posix_lock(filp, fl, &ll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* default lease lock manager operations */
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400502static bool
503lease_break_callback(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
Jeff Layton4d01b7f2014-09-01 15:06:54 -0400506 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400509static void
510lease_setup(struct file_lock *fl, void **priv)
511{
512 struct file *filp = fl->fl_file;
513 struct fasync_struct *fa = *priv;
514
515 /*
516 * fasync_insert_entry() returns the old entry if any. If there was no
517 * old entry, then it used "priv" and inserted it into the fasync list.
518 * Clear the pointer to indicate that it shouldn't be freed.
519 */
520 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
521 *priv = NULL;
522
523 __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
524}
525
Alexey Dobriyan7b021962009-09-21 17:01:12 -0700526static const struct lock_manager_operations lease_manager_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400527 .lm_break = lease_break_callback,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400528 .lm_change = lease_modify,
Jeff Layton1c7dd2f2014-08-22 10:55:47 -0400529 .lm_setup = lease_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530};
531
532/*
533 * Initialize a lease, use the default lock manager operations
534 */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400535static int lease_init(struct file *filp, long type, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 {
Trond Myklebust75dff552006-05-07 23:02:42 -0400537 if (assign_type(fl, type) != 0)
538 return -EINVAL;
539
Jeff Layton7ca76312014-09-01 19:04:48 -0400540 fl->fl_owner = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 fl->fl_pid = current->tgid;
542
543 fl->fl_file = filp;
544 fl->fl_flags = FL_LEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 fl->fl_start = 0;
546 fl->fl_end = OFFSET_MAX;
547 fl->fl_ops = NULL;
548 fl->fl_lmops = &lease_manager_ops;
549 return 0;
550}
551
552/* Allocate a file_lock initialised to this type of lease */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400553static struct file_lock *lease_alloc(struct file *filp, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct file_lock *fl = locks_alloc_lock();
Trond Myklebust75dff552006-05-07 23:02:42 -0400556 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (fl == NULL)
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500559 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 error = lease_init(filp, type, fl);
Trond Myklebust75dff552006-05-07 23:02:42 -0400562 if (error) {
563 locks_free_lock(fl);
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500564 return ERR_PTR(error);
Trond Myklebust75dff552006-05-07 23:02:42 -0400565 }
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500566 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/* Check if two locks overlap each other.
570 */
571static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
572{
573 return ((fl1->fl_end >= fl2->fl_start) &&
574 (fl2->fl_end >= fl1->fl_start));
575}
576
577/*
578 * Check whether two locks have the same owner.
579 */
Matt Mackall33443c42006-01-08 01:05:22 -0800580static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400582 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return fl2->fl_lmops == fl1->fl_lmops &&
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400584 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return fl1->fl_owner == fl2->fl_owner;
586}
587
Jeff Layton6109c852015-01-16 15:05:57 -0500588/* Must be called with the flc_lock held! */
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500589static void locks_insert_global_locks(struct file_lock *fl)
Jeff Layton88974692013-06-21 08:58:14 -0400590{
Peter Zijlstraaba37662015-06-22 14:16:33 +0200591 percpu_rwsem_assert_held(&file_rwsem);
592
Jeff Layton7012b022013-06-21 08:58:22 -0400593 lg_local_lock(&file_lock_lglock);
594 fl->fl_link_cpu = smp_processor_id();
595 hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
596 lg_local_unlock(&file_lock_lglock);
Jeff Layton88974692013-06-21 08:58:14 -0400597}
598
Jeff Layton6109c852015-01-16 15:05:57 -0500599/* Must be called with the flc_lock held! */
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500600static void locks_delete_global_locks(struct file_lock *fl)
Jeff Layton88974692013-06-21 08:58:14 -0400601{
Peter Zijlstraaba37662015-06-22 14:16:33 +0200602 percpu_rwsem_assert_held(&file_rwsem);
603
Jeff Layton7012b022013-06-21 08:58:22 -0400604 /*
605 * Avoid taking lock if already unhashed. This is safe since this check
Jeff Layton6109c852015-01-16 15:05:57 -0500606 * is done while holding the flc_lock, and new insertions into the list
Jeff Layton7012b022013-06-21 08:58:22 -0400607 * also require that it be held.
608 */
609 if (hlist_unhashed(&fl->fl_link))
610 return;
611 lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
Jeff Layton139ca042013-06-21 08:58:17 -0400612 hlist_del_init(&fl->fl_link);
Jeff Layton7012b022013-06-21 08:58:22 -0400613 lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
Jeff Layton88974692013-06-21 08:58:14 -0400614}
615
Jeff Layton3999e492013-06-21 08:58:19 -0400616static unsigned long
617posix_owner_key(struct file_lock *fl)
618{
619 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
620 return fl->fl_lmops->lm_owner_key(fl);
621 return (unsigned long)fl->fl_owner;
622}
623
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500624static void locks_insert_global_blocked(struct file_lock *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400625{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400626 lockdep_assert_held(&blocked_lock_lock);
627
Jeff Layton3999e492013-06-21 08:58:19 -0400628 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
Jeff Layton88974692013-06-21 08:58:14 -0400629}
630
Jeff Layton6ca10ed2014-02-03 12:13:07 -0500631static void locks_delete_global_blocked(struct file_lock *waiter)
Jeff Layton88974692013-06-21 08:58:14 -0400632{
Daniel Wagner663d5af2015-04-03 09:04:03 -0400633 lockdep_assert_held(&blocked_lock_lock);
634
Jeff Layton48f74182013-06-21 08:58:18 -0400635 hash_del(&waiter->fl_link);
Jeff Layton88974692013-06-21 08:58:14 -0400636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* Remove waiter from blocker's block list.
639 * When blocker ends up pointing to itself then the list is empty.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400640 *
Jeff Layton7b2296a2013-06-21 08:58:20 -0400641 * Must be called with blocked_lock_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Matt Mackall33443c42006-01-08 01:05:22 -0800643static void __locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jeff Layton88974692013-06-21 08:58:14 -0400645 locks_delete_global_blocked(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 list_del_init(&waiter->fl_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 waiter->fl_next = NULL;
648}
649
Jeff Layton1a9e64a2013-06-21 08:58:10 -0400650static void locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400652 spin_lock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 __locks_delete_block(waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400654 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/* Insert waiter into blocker's block list.
658 * We use a circular list so that processes can be easily woken up in
659 * the order they blocked. The documentation doesn't require this but
660 * it seems like the reasonable thing to do.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400661 *
Jeff Layton6109c852015-01-16 15:05:57 -0500662 * Must be called with both the flc_lock and blocked_lock_lock held. The
663 * fl_block list itself is protected by the blocked_lock_lock, but by ensuring
664 * that the flc_lock is also held on insertions we can avoid taking the
665 * blocked_lock_lock in some cases when we see that the fl_block list is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Jeff Layton1c8c6012013-06-21 08:58:15 -0400667static void __locks_insert_block(struct file_lock *blocker,
668 struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
J. Bruce Fields6dc0fe82006-03-26 01:37:24 -0800670 BUG_ON(!list_empty(&waiter->fl_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 waiter->fl_next = blocker;
Jeff Layton88974692013-06-21 08:58:14 -0400672 list_add_tail(&waiter->fl_block, &blocker->fl_block);
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400673 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
Jeff Layton1c8c6012013-06-21 08:58:15 -0400674 locks_insert_global_blocked(waiter);
675}
676
Jeff Layton6109c852015-01-16 15:05:57 -0500677/* Must be called with flc_lock held. */
Jeff Layton1c8c6012013-06-21 08:58:15 -0400678static void locks_insert_block(struct file_lock *blocker,
679 struct file_lock *waiter)
680{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400681 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400682 __locks_insert_block(blocker, waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400683 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Jeff Layton1cb36012013-06-21 08:58:12 -0400686/*
687 * Wake up processes blocked waiting for blocker.
688 *
Jeff Layton6109c852015-01-16 15:05:57 -0500689 * Must be called with the inode->flc_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
691static void locks_wake_up_blocks(struct file_lock *blocker)
692{
Jeff Layton4e8c7652013-06-21 08:58:16 -0400693 /*
694 * Avoid taking global lock if list is empty. This is safe since new
Jeff Layton6109c852015-01-16 15:05:57 -0500695 * blocked requests are only added to the list under the flc_lock, and
696 * the flc_lock is always held here. Note that removal from the fl_block
697 * list does not require the flc_lock, so we must recheck list_empty()
Jeff Layton7b2296a2013-06-21 08:58:20 -0400698 * after acquiring the blocked_lock_lock.
Jeff Layton4e8c7652013-06-21 08:58:16 -0400699 */
700 if (list_empty(&blocker->fl_block))
701 return;
702
Jeff Layton7b2296a2013-06-21 08:58:20 -0400703 spin_lock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 while (!list_empty(&blocker->fl_block)) {
Pavel Emelyanovf0c1cd02007-09-19 16:44:07 +0400705 struct file_lock *waiter;
706
707 waiter = list_first_entry(&blocker->fl_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct file_lock, fl_block);
709 __locks_delete_block(waiter);
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400710 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
711 waiter->fl_lmops->lm_notify(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 else
713 wake_up(&waiter->fl_wait);
714 }
Jeff Layton7b2296a2013-06-21 08:58:20 -0400715 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Jeff Layton5263e312015-01-16 15:05:55 -0500718static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500719locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
Jeff Layton5263e312015-01-16 15:05:55 -0500720{
721 fl->fl_nspid = get_pid(task_tgid(current));
722 list_add_tail(&fl->fl_list, before);
723 locks_insert_global_locks(fl);
724}
725
Jeff Layton8634b512015-01-16 15:05:55 -0500726static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500727locks_unlink_lock_ctx(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jeff Layton88974692013-06-21 08:58:14 -0400729 locks_delete_global_locks(fl);
Jeff Layton8634b512015-01-16 15:05:55 -0500730 list_del_init(&fl->fl_list);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000731 if (fl->fl_nspid) {
732 put_pid(fl->fl_nspid);
733 fl->fl_nspid = NULL;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 locks_wake_up_blocks(fl);
Jeff Layton24cbe782014-02-03 12:13:06 -0500736}
737
Jeff Layton5263e312015-01-16 15:05:55 -0500738static void
Jeff Laytone084c1b2015-02-16 14:32:03 -0500739locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
Jeff Layton5263e312015-01-16 15:05:55 -0500740{
Jeff Laytone084c1b2015-02-16 14:32:03 -0500741 locks_unlink_lock_ctx(fl);
Jeff Layton8634b512015-01-16 15:05:55 -0500742 if (dispose)
743 list_add(&fl->fl_list, dispose);
744 else
745 locks_free_lock(fl);
Jeff Layton5263e312015-01-16 15:05:55 -0500746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
749 * checks for shared/exclusive status of overlapping locks.
750 */
751static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
752{
753 if (sys_fl->fl_type == F_WRLCK)
754 return 1;
755 if (caller_fl->fl_type == F_WRLCK)
756 return 1;
757 return 0;
758}
759
760/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
761 * checking before calling the locks_conflict().
762 */
763static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
764{
765 /* POSIX locks owned by the same process do not conflict with
766 * each other.
767 */
Jeff Layton9b8c8692015-04-03 09:04:02 -0400768 if (posix_same_owner(caller_fl, sys_fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return (0);
770
771 /* Check whether they overlap */
772 if (!locks_overlap(caller_fl, sys_fl))
773 return 0;
774
775 return (locks_conflict(caller_fl, sys_fl));
776}
777
778/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
779 * checking before calling the locks_conflict().
780 */
781static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
782{
783 /* FLOCK locks referring to the same filp do not conflict with
784 * each other.
785 */
Jeff Layton9b8c8692015-04-03 09:04:02 -0400786 if (caller_fl->fl_file == sys_fl->fl_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return (0);
788 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
789 return 0;
790
791 return (locks_conflict(caller_fl, sys_fl));
792}
793
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400794void
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500795posix_test_lock(struct file *filp, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct file_lock *cfl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500798 struct file_lock_context *ctx;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400799 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dmitry Vyukov128a3782015-09-21 09:43:06 +0200801 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500802 if (!ctx || list_empty_careful(&ctx->flc_posix)) {
J. Bruce Fields129a84d2007-05-10 18:38:43 -0400803 fl->fl_type = F_UNLCK;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500804 return;
805 }
806
Jeff Layton6109c852015-01-16 15:05:57 -0500807 spin_lock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500808 list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
809 if (posix_locks_conflict(fl, cfl)) {
810 locks_copy_conflock(fl, cfl);
811 if (cfl->fl_nspid)
812 fl->fl_pid = pid_vnr(cfl->fl_nspid);
813 goto out;
814 }
815 }
816 fl->fl_type = F_UNLCK;
817out:
Jeff Layton6109c852015-01-16 15:05:57 -0500818 spin_unlock(&ctx->flc_lock);
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821EXPORT_SYMBOL(posix_test_lock);
822
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400823/*
824 * Deadlock detection:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400826 * We attempt to detect deadlocks that are due purely to posix file
827 * locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400829 * We assume that a task can be waiting for at most one lock at a time.
830 * So for any acquired lock, the process holding that lock may be
831 * waiting on at most one other lock. That lock in turns may be held by
832 * someone waiting for at most one other lock. Given a requested lock
833 * caller_fl which is about to wait for a conflicting lock block_fl, we
834 * follow this chain of waiters to ensure we are not about to create a
835 * cycle.
J. Bruce Fields97855b42007-10-30 11:20:02 -0400836 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400837 * Since we do this before we ever put a process to sleep on a lock, we
838 * are ensured that there is never a cycle; that is what guarantees that
839 * the while() loop in posix_locks_deadlock() eventually completes.
840 *
841 * Note: the above assumption may not be true when handling lock
842 * requests from a broken NFS client. It may also fail in the presence
843 * of tasks (such as posix threads) sharing the same open file table.
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400844 * To handle those cases, we just bail out after a few iterations.
Jeff Layton57b65322014-02-03 12:13:09 -0500845 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400846 * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
Jeff Layton57b65322014-02-03 12:13:09 -0500847 * Because the owner is not even nominally tied to a thread of
848 * execution, the deadlock detection below can't reasonably work well. Just
849 * skip it for those.
850 *
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400851 * In principle, we could do a more limited deadlock detection on FL_OFDLCK
Jeff Layton57b65322014-02-03 12:13:09 -0500852 * locks that just checks for the case where two tasks are attempting to
853 * upgrade from read to write locks on the same inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
J. Bruce Fields97855b42007-10-30 11:20:02 -0400855
856#define MAX_DEADLK_ITERATIONS 10
857
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400858/* Find a lock that the owner of the given block_fl is blocking on. */
859static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
860{
861 struct file_lock *fl;
862
Jeff Layton3999e492013-06-21 08:58:19 -0400863 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400864 if (posix_same_owner(fl, block_fl))
865 return fl->fl_next;
866 }
867 return NULL;
868}
869
Jeff Layton7b2296a2013-06-21 08:58:20 -0400870/* Must be called with the blocked_lock_lock held! */
Adrian Bunkb0904e12006-06-23 02:05:13 -0700871static int posix_locks_deadlock(struct file_lock *caller_fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct file_lock *block_fl)
873{
J. Bruce Fields97855b42007-10-30 11:20:02 -0400874 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Daniel Wagner663d5af2015-04-03 09:04:03 -0400876 lockdep_assert_held(&blocked_lock_lock);
877
Jeff Layton57b65322014-02-03 12:13:09 -0500878 /*
879 * This deadlock detector can't reasonably detect deadlocks with
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400880 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
Jeff Layton57b65322014-02-03 12:13:09 -0500881 */
Jeff Laytoncff2fce2014-04-22 08:24:32 -0400882 if (IS_OFDLCK(caller_fl))
Jeff Layton57b65322014-02-03 12:13:09 -0500883 return 0;
884
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400885 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
886 if (i++ > MAX_DEADLK_ITERATIONS)
887 return 0;
888 if (posix_same_owner(caller_fl, block_fl))
889 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 return 0;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
J. Bruce Fields02888f42007-09-12 15:45:07 -0400895 * after any leases, but before any posix locks.
Trond Myklebustf475ae92006-06-29 16:38:32 -0400896 *
897 * Note that if called with an FL_EXISTS argument, the caller may determine
898 * whether or not a lock was successfully freed by testing the return
899 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Jeff Laytonbcd7f782015-07-11 06:43:02 -0400901static int flock_lock_inode(struct inode *inode, struct file_lock *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Trond Myklebust993dfa82006-03-31 02:30:55 -0800903 struct file_lock *new_fl = NULL;
Jeff Layton5263e312015-01-16 15:05:55 -0500904 struct file_lock *fl;
905 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int error = 0;
Jeff Layton5263e312015-01-16 15:05:55 -0500907 bool found = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -0400908 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jeff Layton5c1c6692015-04-03 09:04:03 -0400910 ctx = locks_get_lock_context(inode, request->fl_type);
911 if (!ctx) {
912 if (request->fl_type != F_UNLCK)
913 return -ENOMEM;
914 return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
915 }
Jeff Layton5263e312015-01-16 15:05:55 -0500916
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200917 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
918 new_fl = locks_alloc_lock();
919 if (!new_fl)
920 return -ENOMEM;
921 }
922
Peter Zijlstraaba37662015-06-22 14:16:33 +0200923 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -0500924 spin_lock(&ctx->flc_lock);
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400925 if (request->fl_flags & FL_ACCESS)
926 goto find_conflict;
Pavel Emelyanov84d535a2007-09-11 16:38:13 +0400927
Jeff Layton5263e312015-01-16 15:05:55 -0500928 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
Jeff Laytonbcd7f782015-07-11 06:43:02 -0400929 if (request->fl_file != fl->fl_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 continue;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800931 if (request->fl_type == fl->fl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto out;
Jeff Layton5263e312015-01-16 15:05:55 -0500933 found = true;
Jeff Laytone084c1b2015-02-16 14:32:03 -0500934 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Trond Myklebustf475ae92006-06-29 16:38:32 -0400938 if (request->fl_type == F_UNLCK) {
939 if ((request->fl_flags & FL_EXISTS) && !found)
940 error = -ENOENT;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800941 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -0400942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400944find_conflict:
Jeff Layton5263e312015-01-16 15:05:55 -0500945 list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
Trond Myklebust993dfa82006-03-31 02:30:55 -0800946 if (!flock_locks_conflict(request, fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 continue;
948 error = -EAGAIN;
Miklos Szeredibde74e42008-07-25 01:48:57 -0700949 if (!(request->fl_flags & FL_SLEEP))
950 goto out;
951 error = FILE_LOCK_DEFERRED;
952 locks_insert_block(fl, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto out;
954 }
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400955 if (request->fl_flags & FL_ACCESS)
956 goto out;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800957 locks_copy_lock(new_fl, request);
Jeff Laytone084c1b2015-02-16 14:32:03 -0500958 locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
Trond Myklebust993dfa82006-03-31 02:30:55 -0800959 new_fl = NULL;
Kirill Korotaev9cedc192006-06-14 17:59:35 +0400960 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962out:
Jeff Layton6109c852015-01-16 15:05:57 -0500963 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +0200964 percpu_up_read(&file_rwsem);
Trond Myklebust993dfa82006-03-31 02:30:55 -0800965 if (new_fl)
966 locks_free_lock(new_fl);
Jeff Laytoned9814d2014-08-11 14:20:31 -0400967 locks_dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return error;
969}
970
Jeff Laytonb4d629a2016-01-07 18:27:42 -0500971static int posix_lock_inode(struct inode *inode, struct file_lock *request,
972 struct file_lock *conflock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500974 struct file_lock *fl, *tmp;
Miklos Szeredi39005d02006-06-23 02:05:10 -0700975 struct file_lock *new_fl = NULL;
976 struct file_lock *new_fl2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct file_lock *left = NULL;
978 struct file_lock *right = NULL;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500979 struct file_lock_context *ctx;
Jeff Laytonb9746ef2013-06-21 08:58:13 -0400980 int error;
981 bool added = false;
Jeff Laytoned9814d2014-08-11 14:20:31 -0400982 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Jeff Layton5c1c6692015-04-03 09:04:03 -0400984 ctx = locks_get_lock_context(inode, request->fl_type);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500985 if (!ctx)
Jeff Layton5c1c6692015-04-03 09:04:03 -0400986 return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /*
989 * We may need two file_lock structures for this operation,
990 * so we get them in advance to avoid races.
Miklos Szeredi39005d02006-06-23 02:05:10 -0700991 *
992 * In some cases we can be sure, that no new locks will be needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
Miklos Szeredi39005d02006-06-23 02:05:10 -0700994 if (!(request->fl_flags & FL_ACCESS) &&
995 (request->fl_type != F_UNLCK ||
996 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
997 new_fl = locks_alloc_lock();
998 new_fl2 = locks_alloc_lock();
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Peter Zijlstraaba37662015-06-22 14:16:33 +02001001 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001002 spin_lock(&ctx->flc_lock);
Jeff Layton1cb36012013-06-21 08:58:12 -04001003 /*
1004 * New lock request. Walk all POSIX locks and look for conflicts. If
1005 * there are any, either return error or put the request on the
Jeff Layton48f74182013-06-21 08:58:18 -04001006 * blocker's list of waiters and the global blocked_hash.
Jeff Layton1cb36012013-06-21 08:58:12 -04001007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (request->fl_type != F_UNLCK) {
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001009 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!posix_locks_conflict(request, fl))
1011 continue;
Andy Adamson5842add2006-03-26 01:37:26 -08001012 if (conflock)
Kinglong Mee3fe0fff12014-08-22 10:18:42 -04001013 locks_copy_conflock(conflock, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 error = -EAGAIN;
1015 if (!(request->fl_flags & FL_SLEEP))
1016 goto out;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001017 /*
1018 * Deadlock detection and insertion into the blocked
1019 * locks list must be done while holding the same lock!
1020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 error = -EDEADLK;
Jeff Layton7b2296a2013-06-21 08:58:20 -04001022 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001023 if (likely(!posix_locks_deadlock(request, fl))) {
1024 error = FILE_LOCK_DEFERRED;
1025 __locks_insert_block(fl, request);
1026 }
Jeff Layton7b2296a2013-06-21 08:58:20 -04001027 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto out;
1029 }
1030 }
1031
1032 /* If we're just looking for a conflict, we're done. */
1033 error = 0;
1034 if (request->fl_flags & FL_ACCESS)
1035 goto out;
1036
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001037 /* Find the first old lock with the same owner as the new lock */
1038 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
1039 if (posix_same_owner(request, fl))
1040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
Jeff Layton1cb36012013-06-21 08:58:12 -04001043 /* Process locks with this owner. */
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001044 list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
1045 if (!posix_same_owner(request, fl))
1046 break;
1047
1048 /* Detect adjacent or overlapping regions (if same lock type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (request->fl_type == fl->fl_type) {
Olaf Kirch449231d2005-08-25 16:25:35 -07001050 /* In all comparisons of start vs end, use
1051 * "start - 1" rather than "end + 1". If end
1052 * is OFFSET_MAX, end + 1 will become negative.
1053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (fl->fl_end < request->fl_start - 1)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001055 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* If the next lock in the list has entirely bigger
1057 * addresses than the new one, insert the lock here.
1058 */
Olaf Kirch449231d2005-08-25 16:25:35 -07001059 if (fl->fl_start - 1 > request->fl_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061
1062 /* If we come here, the new and old lock are of the
1063 * same type and adjacent or overlapping. Make one
1064 * lock yielding from the lower start address of both
1065 * locks to the higher end address.
1066 */
1067 if (fl->fl_start > request->fl_start)
1068 fl->fl_start = request->fl_start;
1069 else
1070 request->fl_start = fl->fl_start;
1071 if (fl->fl_end < request->fl_end)
1072 fl->fl_end = request->fl_end;
1073 else
1074 request->fl_end = fl->fl_end;
1075 if (added) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001076 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 continue;
1078 }
1079 request = fl;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001080 added = true;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001081 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* Processing for different lock types is a bit
1083 * more complex.
1084 */
1085 if (fl->fl_end < request->fl_start)
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001086 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (fl->fl_start > request->fl_end)
1088 break;
1089 if (request->fl_type == F_UNLCK)
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001090 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (fl->fl_start < request->fl_start)
1092 left = fl;
1093 /* If the next lock in the list has a higher end
1094 * address than the new one, insert the new one here.
1095 */
1096 if (fl->fl_end > request->fl_end) {
1097 right = fl;
1098 break;
1099 }
1100 if (fl->fl_start >= request->fl_start) {
1101 /* The new lock completely replaces an old
1102 * one (This may happen several times).
1103 */
1104 if (added) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001105 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 continue;
1107 }
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001108 /*
1109 * Replace the old lock with new_fl, and
1110 * remove the old one. It's safe to do the
1111 * insert here since we know that we won't be
1112 * using new_fl later, and that the lock is
1113 * just replacing an existing lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Jeff Laytonb84d49f2014-08-12 08:03:49 -04001115 error = -ENOLCK;
1116 if (!new_fl)
1117 goto out;
1118 locks_copy_lock(new_fl, request);
1119 request = new_fl;
1120 new_fl = NULL;
Jeff Laytone084c1b2015-02-16 14:32:03 -05001121 locks_insert_lock_ctx(request, &fl->fl_list);
1122 locks_delete_lock_ctx(fl, &dispose);
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001123 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001128 /*
Jeff Layton1cb36012013-06-21 08:58:12 -04001129 * The above code only modifies existing locks in case of merging or
1130 * replacing. If new lock(s) need to be inserted all modifications are
1131 * done below this, so it's safe yet to bail out.
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001132 */
1133 error = -ENOLCK; /* "no luck" */
1134 if (right && left == right && !new_fl2)
1135 goto out;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 error = 0;
1138 if (!added) {
Trond Myklebustf475ae92006-06-29 16:38:32 -04001139 if (request->fl_type == F_UNLCK) {
1140 if (request->fl_flags & FL_EXISTS)
1141 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -04001143 }
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001144
1145 if (!new_fl) {
1146 error = -ENOLCK;
1147 goto out;
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 locks_copy_lock(new_fl, request);
Jeff Laytone084c1b2015-02-16 14:32:03 -05001150 locks_insert_lock_ctx(new_fl, &fl->fl_list);
Jeff Layton2e2f756f2015-02-17 17:08:23 -05001151 fl = new_fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 new_fl = NULL;
1153 }
1154 if (right) {
1155 if (left == right) {
1156 /* The new lock breaks the old one in two pieces,
1157 * so we have to use the second new lock.
1158 */
1159 left = new_fl2;
1160 new_fl2 = NULL;
1161 locks_copy_lock(left, right);
Jeff Laytone084c1b2015-02-16 14:32:03 -05001162 locks_insert_lock_ctx(left, &fl->fl_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 right->fl_start = request->fl_end + 1;
1165 locks_wake_up_blocks(right);
1166 }
1167 if (left) {
1168 left->fl_end = request->fl_start - 1;
1169 locks_wake_up_blocks(left);
1170 }
1171 out:
Jeff Layton6109c852015-01-16 15:05:57 -05001172 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001173 percpu_up_read(&file_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Free any unused locks.
1176 */
1177 if (new_fl)
1178 locks_free_lock(new_fl);
1179 if (new_fl2)
1180 locks_free_lock(new_fl2);
Jeff Laytoned9814d2014-08-11 14:20:31 -04001181 locks_dispose_list(&dispose);
Jeff Layton18909102016-01-06 21:26:10 -05001182 trace_posix_lock_inode(inode, request, error);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return error;
1185}
1186
1187/**
1188 * posix_lock_file - Apply a POSIX-style lock to a file
1189 * @filp: The file to apply the lock to
1190 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05001191 * @conflock: Place to return a copy of the conflicting lock, if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Add a POSIX style lock to a file.
1194 * We merge adjacent & overlapping locks whenever possible.
1195 * POSIX locks are sorted by owner task, then by starting address
Trond Myklebustf475ae92006-06-29 16:38:32 -04001196 *
1197 * Note that if called with an FL_EXISTS argument, the caller may determine
1198 * whether or not a lock was successfully freed by testing the return
1199 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Marc Eshel150b3932007-01-18 16:15:35 -05001201int posix_lock_file(struct file *filp, struct file_lock *fl,
Andy Adamson5842add2006-03-26 01:37:26 -08001202 struct file_lock *conflock)
1203{
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001204 return posix_lock_inode(file_inode(filp), fl, conflock);
Andy Adamson5842add2006-03-26 01:37:26 -08001205}
Marc Eshel150b3932007-01-18 16:15:35 -05001206EXPORT_SYMBOL(posix_lock_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/**
Jeff Layton29d01b22015-07-11 06:43:02 -04001209 * posix_lock_inode_wait - Apply a POSIX-style lock to a file
1210 * @inode: inode of file to which lock request should be applied
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * @fl: The lock to be applied
1212 *
Benjamin Coddington616fb382015-10-22 13:38:15 -04001213 * Apply a POSIX style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04001215static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 int error;
1218 might_sleep ();
1219 for (;;) {
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001220 error = posix_lock_inode(inode, fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001221 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1224 if (!error)
1225 continue;
1226
1227 locks_delete_block(fl);
1228 break;
1229 }
1230 return error;
1231}
Jeff Layton29d01b22015-07-11 06:43:02 -04001232
Jeff Layton9e8925b2015-11-16 09:49:34 -05001233#ifdef CONFIG_MANDATORY_FILE_LOCKING
Jeff Layton29d01b22015-07-11 06:43:02 -04001234/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * locks_mandatory_locked - Check for an active lock
Jeff Laytond7a06982014-03-10 09:54:15 -04001236 * @file: the file to check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 *
1238 * Searches the inode's list of locks to find any POSIX locks which conflict.
1239 * This function is called from locks_verify_locked() only.
1240 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001241int locks_mandatory_locked(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001243 int ret;
Jeff Laytond7a06982014-03-10 09:54:15 -04001244 struct inode *inode = file_inode(file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001245 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct file_lock *fl;
1247
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001248 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001249 if (!ctx || list_empty_careful(&ctx->flc_posix))
1250 return 0;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * Search the lock list for this inode for any POSIX locks.
1254 */
Jeff Layton6109c852015-01-16 15:05:57 -05001255 spin_lock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001256 ret = 0;
1257 list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02001258 if (fl->fl_owner != current->files &&
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001259 fl->fl_owner != file) {
1260 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Jeff Layton6109c852015-01-16 15:05:57 -05001264 spin_unlock(&ctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/**
1269 * locks_mandatory_area - Check for a conflicting lock
Christoph Hellwigacc15572015-12-03 12:59:49 +01001270 * @inode: the file to check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * @filp: how the file was opened (if it was)
Christoph Hellwigacc15572015-12-03 12:59:49 +01001272 * @start: first byte in the file to check
1273 * @end: lastbyte in the file to check
1274 * @type: %F_WRLCK for a write lock, else %F_RDLCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 *
1276 * Searches the inode's list of locks to find any POSIX locks which conflict.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
Christoph Hellwigacc15572015-12-03 12:59:49 +01001278int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
1279 loff_t end, unsigned char type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct file_lock fl;
1282 int error;
Jeff Layton29723ad2014-03-10 09:54:19 -04001283 bool sleep = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 locks_init_lock(&fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 fl.fl_pid = current->tgid;
1287 fl.fl_file = filp;
1288 fl.fl_flags = FL_POSIX | FL_ACCESS;
1289 if (filp && !(filp->f_flags & O_NONBLOCK))
Jeff Layton29723ad2014-03-10 09:54:19 -04001290 sleep = true;
Christoph Hellwigacc15572015-12-03 12:59:49 +01001291 fl.fl_type = type;
1292 fl.fl_start = start;
1293 fl.fl_end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 for (;;) {
Jeff Layton29723ad2014-03-10 09:54:19 -04001296 if (filp) {
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02001297 fl.fl_owner = filp;
Jeff Layton29723ad2014-03-10 09:54:19 -04001298 fl.fl_flags &= ~FL_SLEEP;
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001299 error = posix_lock_inode(inode, &fl, NULL);
Jeff Layton29723ad2014-03-10 09:54:19 -04001300 if (!error)
1301 break;
1302 }
1303
1304 if (sleep)
1305 fl.fl_flags |= FL_SLEEP;
1306 fl.fl_owner = current->files;
Jeff Laytonb4d629a2016-01-07 18:27:42 -05001307 error = posix_lock_inode(inode, &fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001308 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1311 if (!error) {
1312 /*
1313 * If we've been sleeping someone might have
1314 * changed the permissions behind our back.
1315 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07001316 if (__mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 continue;
1318 }
1319
1320 locks_delete_block(&fl);
1321 break;
1322 }
1323
1324 return error;
1325}
1326
1327EXPORT_SYMBOL(locks_mandatory_area);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001328#endif /* CONFIG_MANDATORY_FILE_LOCKING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
J. Bruce Fields778fc542011-07-26 18:25:49 -04001330static void lease_clear_pending(struct file_lock *fl, int arg)
1331{
1332 switch (arg) {
1333 case F_UNLCK:
1334 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1335 /* fall through: */
1336 case F_RDLCK:
1337 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1338 }
1339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/* We already had a lease on this file; just change its type */
Jeff Layton7448cc32015-01-16 15:05:57 -05001342int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int error = assign_type(fl, arg);
1345
1346 if (error)
1347 return error;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001348 lease_clear_pending(fl, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 locks_wake_up_blocks(fl);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001350 if (arg == F_UNLCK) {
1351 struct file *filp = fl->fl_file;
1352
1353 f_delown(filp);
1354 filp->f_owner.signum = 0;
J. Bruce Fields96d6d592012-07-27 16:18:00 -04001355 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1356 if (fl->fl_fasync != NULL) {
1357 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1358 fl->fl_fasync = NULL;
1359 }
Jeff Laytone084c1b2015-02-16 14:32:03 -05001360 locks_delete_lock_ctx(fl, dispose);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364EXPORT_SYMBOL(lease_modify);
1365
J. Bruce Fields778fc542011-07-26 18:25:49 -04001366static bool past_time(unsigned long then)
1367{
1368 if (!then)
1369 /* 0 is a special value meaning "this never expires": */
1370 return false;
1371 return time_after(jiffies, then);
1372}
1373
Jeff Laytonc45198e2014-09-01 07:12:07 -04001374static void time_out_leases(struct inode *inode, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Jeff Layton8634b512015-01-16 15:05:55 -05001376 struct file_lock_context *ctx = inode->i_flctx;
1377 struct file_lock *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Jeff Layton6109c852015-01-16 15:05:57 -05001379 lockdep_assert_held(&ctx->flc_lock);
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001380
Jeff Layton8634b512015-01-16 15:05:55 -05001381 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001382 trace_time_out_leases(inode, fl);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001383 if (past_time(fl->fl_downgrade_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001384 lease_modify(fl, F_RDLCK, dispose);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001385 if (past_time(fl->fl_break_time))
Jeff Layton7448cc32015-01-16 15:05:57 -05001386 lease_modify(fl, F_UNLCK, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388}
1389
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001390static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1391{
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001392 if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT))
1393 return false;
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001394 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1395 return false;
1396 return locks_conflict(breaker, lease);
1397}
1398
Jeff Layton03d12dd2014-09-01 14:53:41 -04001399static bool
1400any_leases_conflict(struct inode *inode, struct file_lock *breaker)
1401{
Jeff Layton8634b512015-01-16 15:05:55 -05001402 struct file_lock_context *ctx = inode->i_flctx;
Jeff Layton03d12dd2014-09-01 14:53:41 -04001403 struct file_lock *fl;
1404
Jeff Layton6109c852015-01-16 15:05:57 -05001405 lockdep_assert_held(&ctx->flc_lock);
Jeff Layton03d12dd2014-09-01 14:53:41 -04001406
Jeff Layton8634b512015-01-16 15:05:55 -05001407 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Jeff Layton03d12dd2014-09-01 14:53:41 -04001408 if (leases_conflict(fl, breaker))
1409 return true;
1410 }
1411 return false;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/**
1415 * __break_lease - revoke all outstanding leases on file
1416 * @inode: the inode of the file to return
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001417 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1418 * break all leases
1419 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
1420 * only delegations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *
david m. richter87250dd2007-05-09 16:10:27 -04001422 * break_lease (inlined for speed) has checked there already is at least
1423 * some kind of lock (maybe a lease) on this file. Leases are broken on
1424 * a call to open() or truncate(). This function can sleep unless you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * specified %O_NONBLOCK to your open().
1426 */
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001427int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
J. Bruce Fields778fc542011-07-26 18:25:49 -04001429 int error = 0;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001430 struct file_lock_context *ctx;
Yan, Zhenga9011252015-03-27 10:34:20 +08001431 struct file_lock *new_fl, *fl, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 unsigned long break_time;
Al Viro8737c932009-12-24 06:47:55 -05001433 int want_write = (mode & O_ACCMODE) != O_RDONLY;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001434 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Al Viro8737c932009-12-24 06:47:55 -05001436 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001437 if (IS_ERR(new_fl))
1438 return PTR_ERR(new_fl);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001439 new_fl->fl_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jeff Layton8634b512015-01-16 15:05:55 -05001441 /* typically we will check that ctx is non-NULL before calling */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001442 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001443 if (!ctx) {
1444 WARN_ON_ONCE(1);
1445 return error;
1446 }
1447
Peter Zijlstraaba37662015-06-22 14:16:33 +02001448 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001449 spin_lock(&ctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Jeff Laytonc45198e2014-09-01 07:12:07 -04001451 time_out_leases(inode, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jeff Layton03d12dd2014-09-01 14:53:41 -04001453 if (!any_leases_conflict(inode, new_fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001454 goto out;
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break_time = 0;
1457 if (lease_break_time > 0) {
1458 break_time = jiffies + lease_break_time * HZ;
1459 if (break_time == 0)
1460 break_time++; /* so that 0 means no break time */
1461 }
1462
Yan, Zhenga9011252015-03-27 10:34:20 +08001463 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001464 if (!leases_conflict(fl, new_fl))
1465 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001466 if (want_write) {
1467 if (fl->fl_flags & FL_UNLOCK_PENDING)
1468 continue;
1469 fl->fl_flags |= FL_UNLOCK_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 fl->fl_break_time = break_time;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001471 } else {
Jeff Layton8634b512015-01-16 15:05:55 -05001472 if (lease_breaking(fl))
J. Bruce Fields778fc542011-07-26 18:25:49 -04001473 continue;
1474 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1475 fl->fl_downgrade_time = break_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001477 if (fl->fl_lmops->lm_break(fl))
Jeff Laytone084c1b2015-02-16 14:32:03 -05001478 locks_delete_lock_ctx(fl, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
Jeff Layton8634b512015-01-16 15:05:55 -05001481 if (list_empty(&ctx->flc_lease))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04001482 goto out;
1483
Jeff Layton843c6b22014-09-01 14:27:43 -04001484 if (mode & O_NONBLOCK) {
Jeff Layton62af4f12014-05-09 14:13:05 -04001485 trace_break_lease_noblock(inode, new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 error = -EWOULDBLOCK;
1487 goto out;
1488 }
1489
1490restart:
Jeff Layton8634b512015-01-16 15:05:55 -05001491 fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
1492 break_time = fl->fl_break_time;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001493 if (break_time != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break_time -= jiffies;
Jeff Laytonf1c6bb22014-04-15 06:17:49 -04001495 if (break_time == 0)
1496 break_time++;
Jeff Layton8634b512015-01-16 15:05:55 -05001497 locks_insert_block(fl, new_fl);
Jeff Layton62af4f12014-05-09 14:13:05 -04001498 trace_break_lease_block(inode, new_fl);
Jeff Layton6109c852015-01-16 15:05:57 -05001499 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001500 percpu_up_read(&file_rwsem);
1501
Jeff Laytonc45198e2014-09-01 07:12:07 -04001502 locks_dispose_list(&dispose);
Matthew Wilcox4321e012008-01-14 21:28:30 -07001503 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1504 !new_fl->fl_next, break_time);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001505
1506 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001507 spin_lock(&ctx->flc_lock);
Jeff Layton62af4f12014-05-09 14:13:05 -04001508 trace_break_lease_unblock(inode, new_fl);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001509 locks_delete_block(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (error >= 0) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001511 /*
1512 * Wait for the next conflicting lease that has not been
1513 * broken yet
1514 */
Jeff Layton03d12dd2014-09-01 14:53:41 -04001515 if (error == 0)
1516 time_out_leases(inode, &dispose);
1517 if (any_leases_conflict(inode, new_fl))
1518 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 error = 0;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521out:
Jeff Layton6109c852015-01-16 15:05:57 -05001522 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001523 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001524 locks_dispose_list(&dispose);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001525 locks_free_lock(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return error;
1527}
1528
1529EXPORT_SYMBOL(__break_lease);
1530
1531/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001532 * lease_get_mtime - get the last modified time of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * @inode: the inode
1534 * @time: pointer to a timespec which will contain the last modified time
1535 *
1536 * This is to force NFS clients to flush their caches for files with
1537 * exclusive leases. The justification is that if someone has an
Randy Dunlapa6b91912008-03-19 17:01:00 -07001538 * exclusive lease, then they could be modifying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
1540void lease_get_mtime(struct inode *inode, struct timespec *time)
1541{
Jeff Laytonbfe86022014-08-22 10:18:44 -04001542 bool has_lease = false;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001543 struct file_lock_context *ctx;
Jeff Layton8634b512015-01-16 15:05:55 -05001544 struct file_lock *fl;
Jeff Laytonbfe86022014-08-22 10:18:44 -04001545
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001546 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001547 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Jeff Layton6109c852015-01-16 15:05:57 -05001548 spin_lock(&ctx->flc_lock);
Geliang Tang8ace5df2015-11-18 21:40:33 +08001549 fl = list_first_entry_or_null(&ctx->flc_lease,
1550 struct file_lock, fl_list);
1551 if (fl && (fl->fl_type == F_WRLCK))
1552 has_lease = true;
Jeff Layton6109c852015-01-16 15:05:57 -05001553 spin_unlock(&ctx->flc_lock);
Jeff Laytonbfe86022014-08-22 10:18:44 -04001554 }
1555
1556 if (has_lease)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *time = current_fs_time(inode->i_sb);
1558 else
1559 *time = inode->i_mtime;
1560}
1561
1562EXPORT_SYMBOL(lease_get_mtime);
1563
1564/**
1565 * fcntl_getlease - Enquire what lease is currently active
1566 * @filp: the file
1567 *
1568 * The value returned by this function will be one of
1569 * (if no lease break is pending):
1570 *
1571 * %F_RDLCK to indicate a shared lease is held.
1572 *
1573 * %F_WRLCK to indicate an exclusive lease is held.
1574 *
1575 * %F_UNLCK to indicate no lease is held.
1576 *
1577 * (if a lease break is pending):
1578 *
1579 * %F_RDLCK to indicate an exclusive lease needs to be
1580 * changed to a shared lease (or removed).
1581 *
1582 * %F_UNLCK to indicate the lease needs to be removed.
1583 *
1584 * XXX: sfr & willy disagree over whether F_INPROGRESS
1585 * should be returned to userspace.
1586 */
1587int fcntl_getlease(struct file *filp)
1588{
1589 struct file_lock *fl;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001590 struct inode *inode = file_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001591 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int type = F_UNLCK;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001593 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001595 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001596 if (ctx && !list_empty_careful(&ctx->flc_lease)) {
Jeff Layton6109c852015-01-16 15:05:57 -05001597 spin_lock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05001598 time_out_leases(file_inode(filp), &dispose);
1599 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
1600 if (fl->fl_file != filp)
1601 continue;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001602 type = target_leasetype(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
1604 }
Jeff Layton6109c852015-01-16 15:05:57 -05001605 spin_unlock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05001606 locks_dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return type;
1609}
1610
Jeff Layton24cbe782014-02-03 12:13:06 -05001611/**
1612 * check_conflicting_open - see if the given dentry points to a file that has
1613 * an existing open that would conflict with the
1614 * desired lease.
1615 * @dentry: dentry to check
1616 * @arg: type of lease that we're trying to acquire
Randy Dunlap7fadc592015-08-09 18:43:17 -07001617 * @flags: current lock flags
Jeff Layton24cbe782014-02-03 12:13:06 -05001618 *
1619 * Check to see if there's an existing open fd on this file that would
1620 * conflict with the lease we're trying to set.
1621 */
1622static int
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001623check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
Jeff Layton24cbe782014-02-03 12:13:06 -05001624{
1625 int ret = 0;
1626 struct inode *inode = dentry->d_inode;
1627
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001628 if (flags & FL_LAYOUT)
1629 return 0;
1630
Jeff Layton24cbe782014-02-03 12:13:06 -05001631 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
1632 return -EAGAIN;
1633
1634 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
1635 (atomic_read(&inode->i_count) > 1)))
1636 ret = -EAGAIN;
1637
1638 return ret;
1639}
1640
Jeff Laytone6f5c782014-08-22 10:40:25 -04001641static int
1642generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Jeff Layton8634b512015-01-16 15:05:55 -05001644 struct file_lock *fl, *my_fl = NULL, *lease;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001645 struct dentry *dentry = filp->f_path.dentry;
Miklos Szeredi6343a212016-07-01 14:56:07 +02001646 struct inode *inode = file_inode(filp);
Jeff Layton8634b512015-01-16 15:05:55 -05001647 struct file_lock_context *ctx;
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001648 bool is_deleg = (*flp)->fl_flags & FL_DELEG;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001649 int error;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001650 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
J. Bruce Fields096657b2010-10-30 17:31:14 -04001652 lease = *flp;
Jeff Layton62af4f12014-05-09 14:13:05 -04001653 trace_generic_add_lease(inode, lease);
1654
Jeff Layton5c1c6692015-04-03 09:04:03 -04001655 /* Note that arg is never F_UNLCK here */
1656 ctx = locks_get_lock_context(inode, arg);
Jeff Layton8634b512015-01-16 15:05:55 -05001657 if (!ctx)
1658 return -ENOMEM;
1659
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001660 /*
1661 * In the delegation case we need mutual exclusion with
1662 * a number of operations that take the i_mutex. We trylock
1663 * because delegations are an optional optimization, and if
1664 * there's some chance of a conflict--we'd rather not
1665 * bother, maybe that's a sign this just isn't a good file to
1666 * hand out a delegation on.
1667 */
Al Viro59551022016-01-22 15:40:57 -05001668 if (is_deleg && !inode_trylock(inode))
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001669 return -EAGAIN;
1670
1671 if (is_deleg && arg == F_WRLCK) {
1672 /* Write delegations are not currently supported: */
Al Viro59551022016-01-22 15:40:57 -05001673 inode_unlock(inode);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001674 WARN_ON_ONCE(1);
1675 return -EINVAL;
1676 }
J. Bruce Fields096657b2010-10-30 17:31:14 -04001677
Peter Zijlstraaba37662015-06-22 14:16:33 +02001678 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001679 spin_lock(&ctx->flc_lock);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001680 time_out_leases(inode, &dispose);
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001681 error = check_conflicting_open(dentry, arg, lease->fl_flags);
Jeff Layton24cbe782014-02-03 12:13:06 -05001682 if (error)
J. Bruce Fields096657b2010-10-30 17:31:14 -04001683 goto out;
Pavel Emelyanov85c59582007-09-20 12:45:02 +04001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
1686 * At this point, we know that if there is an exclusive
1687 * lease on this file, then we hold it on this filp
1688 * (otherwise our open of this file would have blocked).
1689 * And if we are trying to acquire an exclusive lease,
1690 * then the file is not open by anyone (including us)
1691 * except for this filp.
1692 */
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001693 error = -EAGAIN;
Jeff Layton8634b512015-01-16 15:05:55 -05001694 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001695 if (fl->fl_file == filp &&
1696 fl->fl_owner == lease->fl_owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001697 my_fl = fl;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001698 continue;
1699 }
Jeff Layton8634b512015-01-16 15:05:55 -05001700
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001701 /*
1702 * No exclusive leases if someone else has a lease on
1703 * this file:
1704 */
1705 if (arg == F_WRLCK)
1706 goto out;
1707 /*
1708 * Modifying our existing lease is OK, but no getting a
1709 * new lease if someone else is opening for write:
1710 */
1711 if (fl->fl_flags & FL_UNLOCK_PENDING)
1712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
Jeff Layton8634b512015-01-16 15:05:55 -05001715 if (my_fl != NULL) {
Jeff Layton0164bf02015-03-04 17:34:32 -05001716 lease = my_fl;
1717 error = lease->fl_lmops->lm_change(lease, arg, &dispose);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001718 if (error)
1719 goto out;
1720 goto out_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 error = -EINVAL;
1724 if (!leases_enable)
1725 goto out;
1726
Jeff Laytone084c1b2015-02-16 14:32:03 -05001727 locks_insert_lock_ctx(lease, &ctx->flc_lease);
Jeff Layton24cbe782014-02-03 12:13:06 -05001728 /*
1729 * The check in break_lease() is lockless. It's possible for another
1730 * open to race in after we did the earlier check for a conflicting
1731 * open but before the lease was inserted. Check again for a
1732 * conflicting open and cancel the lease if there is one.
1733 *
1734 * We also add a barrier here to ensure that the insertion of the lock
1735 * precedes these checks.
1736 */
1737 smp_mb();
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001738 error = check_conflicting_open(dentry, arg, lease->fl_flags);
Jeff Layton8634b512015-01-16 15:05:55 -05001739 if (error) {
Jeff Laytone084c1b2015-02-16 14:32:03 -05001740 locks_unlink_lock_ctx(lease);
Jeff Layton8634b512015-01-16 15:05:55 -05001741 goto out;
1742 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001743
1744out_setup:
1745 if (lease->fl_lmops->lm_setup)
1746 lease->fl_lmops->lm_setup(lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747out:
Jeff Layton6109c852015-01-16 15:05:57 -05001748 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001749 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001750 locks_dispose_list(&dispose);
J. Bruce Fieldsdf4e8d22012-03-05 13:18:59 -05001751 if (is_deleg)
Al Viro59551022016-01-22 15:40:57 -05001752 inode_unlock(inode);
Jeff Layton8634b512015-01-16 15:05:55 -05001753 if (!error && !my_fl)
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001754 *flp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return error;
1756}
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001757
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001758static int generic_delete_lease(struct file *filp, void *owner)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001759{
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001760 int error = -EAGAIN;
Jeff Layton8634b512015-01-16 15:05:55 -05001761 struct file_lock *fl, *victim = NULL;
Benjamin Coddington6ca7d912015-10-15 09:07:07 -04001762 struct inode *inode = file_inode(filp);
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001763 struct file_lock_context *ctx;
Jeff Laytonc45198e2014-09-01 07:12:07 -04001764 LIST_HEAD(dispose);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001765
Dmitry Vyukov128a3782015-09-21 09:43:06 +02001766 ctx = smp_load_acquire(&inode->i_flctx);
Jeff Layton8634b512015-01-16 15:05:55 -05001767 if (!ctx) {
1768 trace_generic_delete_lease(inode, NULL);
1769 return error;
1770 }
1771
Peter Zijlstraaba37662015-06-22 14:16:33 +02001772 percpu_down_read(&file_rwsem);
Jeff Layton6109c852015-01-16 15:05:57 -05001773 spin_lock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05001774 list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001775 if (fl->fl_file == filp &&
1776 fl->fl_owner == owner) {
Jeff Layton8634b512015-01-16 15:05:55 -05001777 victim = fl;
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001778 break;
Jeff Layton8634b512015-01-16 15:05:55 -05001779 }
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001780 }
Jeff Laytona9b1b452015-03-14 09:45:35 -04001781 trace_generic_delete_lease(inode, victim);
Jeff Layton8634b512015-01-16 15:05:55 -05001782 if (victim)
Jeff Layton7448cc32015-01-16 15:05:57 -05001783 error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05001784 spin_unlock(&ctx->flc_lock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02001785 percpu_up_read(&file_rwsem);
Jeff Laytonc45198e2014-09-01 07:12:07 -04001786 locks_dispose_list(&dispose);
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001787 return error;
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790/**
1791 * generic_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001792 * @filp: file pointer
1793 * @arg: type of lease to obtain
1794 * @flp: input - file_lock to use, output - file_lock inserted
1795 * @priv: private data for lm_setup (may be NULL if lm_setup
1796 * doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 *
1798 * The (input) flp->fl_lmops->lm_break function is required
1799 * by break_lease().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04001801int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1802 void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Benjamin Coddington6ca7d912015-10-15 09:07:07 -04001804 struct inode *inode = file_inode(filp);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001805 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001807 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001808 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (!S_ISREG(inode->i_mode))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001810 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 error = security_file_lock(filp, arg);
1812 if (error)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001813 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001815 switch (arg) {
1816 case F_UNLCK:
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001817 return generic_delete_lease(filp, *priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001818 case F_RDLCK:
1819 case F_WRLCK:
Jeff Layton0efaa7e2014-08-22 10:18:45 -04001820 if (!(*flp)->fl_lmops->lm_break) {
1821 WARN_ON_ONCE(1);
1822 return -ENOLCK;
1823 }
Christoph Hellwig11afe9f2015-01-21 19:17:03 +01001824
Jeff Laytone6f5c782014-08-22 10:40:25 -04001825 return generic_add_lease(filp, arg, flp, priv);
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001826 default:
Dave Jones8d657eb2012-07-13 13:35:36 -04001827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
Christoph Hellwig0af1a452007-07-31 00:39:22 -07001830EXPORT_SYMBOL(generic_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001832/**
Jeff Laytone51673a2014-08-22 18:13:28 -04001833 * vfs_setlease - sets a lease on an open file
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001834 * @filp: file pointer
1835 * @arg: type of lease to obtain
1836 * @lease: file_lock to use when adding a lease
1837 * @priv: private info for lm_setup when adding a lease (may be
1838 * NULL if lm_setup doesn't require it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 *
Jeff Laytone51673a2014-08-22 18:13:28 -04001840 * Call this to establish a lease on the file. The "lease" argument is not
1841 * used for F_UNLCK requests and may be NULL. For commands that set or alter
1842 * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
1843 * if not, this function will return -ENOLCK (and generate a scary-looking
1844 * stack trace).
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001845 *
1846 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1847 * may be NULL if the lm_setup operation doesn't require it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04001849int
1850vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001852 if (filp->f_op->setlease)
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001853 return filp->f_op->setlease(filp, arg, lease, priv);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001854 else
Jeff Laytonf82b4b62014-08-22 18:50:48 -04001855 return generic_setlease(filp, arg, lease, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001857EXPORT_SYMBOL_GPL(vfs_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001859static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001861 struct file_lock *fl;
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001862 struct fasync_struct *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 int error;
1864
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +02001865 fl = lease_alloc(filp, arg);
1866 if (IS_ERR(fl))
1867 return PTR_ERR(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001869 new = fasync_alloc();
1870 if (!new) {
1871 locks_free_lock(fl);
1872 return -ENOMEM;
1873 }
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001874 new->fa_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04001876 error = vfs_setlease(filp, arg, &fl, (void **)&new);
Jeff Layton2dfb9282014-08-11 18:14:12 -04001877 if (fl)
1878 locks_free_lock(fl);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001879 if (new)
1880 fasync_free(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return error;
1882}
1883
1884/**
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001885 * fcntl_setlease - sets a lease on an open file
1886 * @fd: open file descriptor
1887 * @filp: file pointer
1888 * @arg: type of lease to obtain
1889 *
1890 * Call this fcntl to establish a lease on the file.
1891 * Note that you also need to call %F_SETSIG to
1892 * receive a signal when the lease is broken.
1893 */
1894int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1895{
1896 if (arg == F_UNLCK)
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +01001897 return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001898 return do_fcntl_add_lease(fd, filp, arg);
1899}
1900
1901/**
Jeff Layton29d01b22015-07-11 06:43:02 -04001902 * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
1903 * @inode: inode of the file to apply to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * @fl: The lock to be applied
1905 *
Jeff Layton29d01b22015-07-11 06:43:02 -04001906 * Apply a FLOCK style lock request to an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Benjamin Coddington616fb382015-10-22 13:38:15 -04001908static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 int error;
1911 might_sleep();
1912 for (;;) {
Jeff Layton29d01b22015-07-11 06:43:02 -04001913 error = flock_lock_inode(inode, fl);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001914 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
1916 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1917 if (!error)
1918 continue;
1919
1920 locks_delete_block(fl);
1921 break;
1922 }
1923 return error;
1924}
1925
Jeff Layton29d01b22015-07-11 06:43:02 -04001926/**
Benjamin Coddingtone55c34a2015-10-22 13:38:13 -04001927 * locks_lock_inode_wait - Apply a lock to an inode
1928 * @inode: inode of the file to apply to
1929 * @fl: The lock to be applied
1930 *
1931 * Apply a POSIX or FLOCK style lock request to an inode.
1932 */
1933int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1934{
1935 int res = 0;
1936 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
1937 case FL_POSIX:
1938 res = posix_lock_inode_wait(inode, fl);
1939 break;
1940 case FL_FLOCK:
1941 res = flock_lock_inode_wait(inode, fl);
1942 break;
1943 default:
1944 BUG();
1945 }
1946 return res;
1947}
1948EXPORT_SYMBOL(locks_lock_inode_wait);
1949
1950/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 * sys_flock: - flock() system call.
1952 * @fd: the file descriptor to lock.
1953 * @cmd: the type of lock to apply.
1954 *
1955 * Apply a %FL_FLOCK style lock to an open file descriptor.
1956 * The @cmd can be one of
1957 *
1958 * %LOCK_SH -- a shared lock.
1959 *
1960 * %LOCK_EX -- an exclusive lock.
1961 *
1962 * %LOCK_UN -- remove an existing lock.
1963 *
1964 * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
1965 *
1966 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1967 * processes read and write access respectively.
1968 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001969SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Al Viro2903ff02012-08-28 12:52:22 -04001971 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct file_lock *lock;
1973 int can_sleep, unlock;
1974 int error;
1975
1976 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001977 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 goto out;
1979
1980 can_sleep = !(cmd & LOCK_NB);
1981 cmd &= ~LOCK_NB;
1982 unlock = (cmd == LOCK_UN);
1983
Al Viroaeb5d722008-09-02 15:28:45 -04001984 if (!unlock && !(cmd & LOCK_MAND) &&
Al Viro2903ff02012-08-28 12:52:22 -04001985 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto out_putf;
1987
Jeff Layton6e129d002014-09-04 10:25:06 -04001988 lock = flock_make_lock(f.file, cmd);
1989 if (IS_ERR(lock)) {
1990 error = PTR_ERR(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto out_putf;
Jeff Layton6e129d002014-09-04 10:25:06 -04001992 }
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (can_sleep)
1995 lock->fl_flags |= FL_SLEEP;
1996
Al Viro2903ff02012-08-28 12:52:22 -04001997 error = security_file_lock(f.file, lock->fl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (error)
1999 goto out_free;
2000
Al Viro72c2d532013-09-22 16:27:52 -04002001 if (f.file->f_op->flock)
Al Viro2903ff02012-08-28 12:52:22 -04002002 error = f.file->f_op->flock(f.file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 (can_sleep) ? F_SETLKW : F_SETLK,
2004 lock);
2005 else
Benjamin Coddington4f656362015-10-22 13:38:14 -04002006 error = locks_lock_file_wait(f.file, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 out_free:
Trond Myklebust993dfa82006-03-31 02:30:55 -08002009 locks_free_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 out_putf:
Al Viro2903ff02012-08-28 12:52:22 -04002012 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 out:
2014 return error;
2015}
2016
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002017/**
2018 * vfs_test_lock - test file byte range lock
2019 * @filp: The file to test lock for
J. Bruce Fields6924c552007-05-11 16:22:50 -04002020 * @fl: The lock to test; also used to hold result
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002021 *
2022 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
2023 * setting conf->fl_type to something other than F_UNLCK.
2024 */
2025int vfs_test_lock(struct file *filp, struct file_lock *fl)
2026{
Al Viro72c2d532013-09-22 16:27:52 -04002027 if (filp->f_op->lock)
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002028 return filp->f_op->lock(filp, F_GETLK, fl);
2029 posix_test_lock(filp, fl);
2030 return 0;
2031}
2032EXPORT_SYMBOL_GPL(vfs_test_lock);
2033
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002034static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
2035{
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002036 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002037#if BITS_PER_LONG == 32
2038 /*
2039 * Make sure we can represent the posix lock via
2040 * legacy 32bit flock.
2041 */
2042 if (fl->fl_start > OFFT_OFFSET_MAX)
2043 return -EOVERFLOW;
2044 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
2045 return -EOVERFLOW;
2046#endif
2047 flock->l_start = fl->fl_start;
2048 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2049 fl->fl_end - fl->fl_start + 1;
2050 flock->l_whence = 0;
J. Bruce Fields129a84d2007-05-10 18:38:43 -04002051 flock->l_type = fl->fl_type;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002052 return 0;
2053}
2054
2055#if BITS_PER_LONG == 32
2056static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
2057{
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002058 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002059 flock->l_start = fl->fl_start;
2060 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
2061 fl->fl_end - fl->fl_start + 1;
2062 flock->l_whence = 0;
2063 flock->l_type = fl->fl_type;
2064}
2065#endif
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067/* Report the first existing lock that would conflict with l.
2068 * This implements the F_GETLK command of fcntl().
2069 */
Jeff Laytonc1e62b82014-02-03 12:13:09 -05002070int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002072 struct file_lock file_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct flock flock;
2074 int error;
2075
2076 error = -EFAULT;
2077 if (copy_from_user(&flock, l, sizeof(flock)))
2078 goto out;
2079 error = -EINVAL;
2080 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
2081 goto out;
2082
2083 error = flock_to_posix_lock(filp, &file_lock, &flock);
2084 if (error)
2085 goto out;
2086
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002087 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002088 error = -EINVAL;
2089 if (flock.l_pid != 0)
2090 goto out;
2091
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002092 cmd = F_GETLK;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002093 file_lock.fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002094 file_lock.fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002095 }
2096
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002097 error = vfs_test_lock(filp, &file_lock);
2098 if (error)
2099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002101 flock.l_type = file_lock.fl_type;
2102 if (file_lock.fl_type != F_UNLCK) {
2103 error = posix_lock_to_flock(&flock, &file_lock);
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05002104 if (error)
Kinglong Meef3282962014-08-22 10:18:43 -04002105 goto rel_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 error = -EFAULT;
2108 if (!copy_to_user(l, &flock, sizeof(flock)))
2109 error = 0;
Kinglong Meef3282962014-08-22 10:18:43 -04002110rel_priv:
2111 locks_release_private(&file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112out:
2113 return error;
2114}
2115
Marc Eshel7723ec92007-01-18 15:08:55 -05002116/**
2117 * vfs_lock_file - file byte range lock
2118 * @filp: The file to apply the lock to
2119 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2120 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05002121 * @conf: Place to return a copy of the conflicting lock, if found.
2122 *
2123 * A caller that doesn't care about the conflicting lock may pass NULL
2124 * as the final argument.
2125 *
2126 * If the filesystem defines a private ->lock() method, then @conf will
2127 * be left unchanged; so a caller that cares should initialize it to
2128 * some acceptable default.
Marc Eshel2beb6612006-12-05 23:31:28 -05002129 *
2130 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2131 * locks, the ->lock() interface may return asynchronously, before the lock has
2132 * been granted or denied by the underlying filesystem, if (and only if)
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002133 * lm_grant is set. Callers expecting ->lock() to return asynchronously
Marc Eshel2beb6612006-12-05 23:31:28 -05002134 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
2135 * the request is for a blocking lock. When ->lock() does return asynchronously,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002136 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
Marc Eshel2beb6612006-12-05 23:31:28 -05002137 * request completes.
2138 * If the request is for non-blocking lock the file system should return
Miklos Szeredibde74e42008-07-25 01:48:57 -07002139 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2140 * with the result. If the request timed out the callback routine will return a
Marc Eshel2beb6612006-12-05 23:31:28 -05002141 * nonzero return code and the file system should release the lock. The file
2142 * system is also responsible to keep a corresponding posix lock when it
2143 * grants a lock so the VFS can find out which locks are locally held and do
2144 * the correct lock cleanup when required.
2145 * The underlying filesystem must not drop the kernel lock or call
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002146 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
Marc Eshel2beb6612006-12-05 23:31:28 -05002147 * return code.
Marc Eshel7723ec92007-01-18 15:08:55 -05002148 */
Marc Eshel150b3932007-01-18 16:15:35 -05002149int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
Marc Eshel7723ec92007-01-18 15:08:55 -05002150{
Al Viro72c2d532013-09-22 16:27:52 -04002151 if (filp->f_op->lock)
Marc Eshel7723ec92007-01-18 15:08:55 -05002152 return filp->f_op->lock(filp, cmd, fl);
2153 else
Marc Eshel150b3932007-01-18 16:15:35 -05002154 return posix_lock_file(filp, fl, conf);
Marc Eshel7723ec92007-01-18 15:08:55 -05002155}
2156EXPORT_SYMBOL_GPL(vfs_lock_file);
2157
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002158static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2159 struct file_lock *fl)
2160{
2161 int error;
2162
2163 error = security_file_lock(filp, fl->fl_type);
2164 if (error)
2165 return error;
2166
Miklos Szeredi764c76b2008-07-25 01:48:58 -07002167 for (;;) {
2168 error = vfs_lock_file(filp, cmd, fl, NULL);
2169 if (error != FILE_LOCK_DEFERRED)
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002170 break;
Miklos Szeredi764c76b2008-07-25 01:48:58 -07002171 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2172 if (!error)
2173 continue;
2174
2175 locks_delete_block(fl);
2176 break;
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002177 }
2178
2179 return error;
2180}
2181
Benjamin Coddington6ca7d912015-10-15 09:07:07 -04002182/* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002183static int
2184check_fmode_for_setlk(struct file_lock *fl)
2185{
2186 switch (fl->fl_type) {
2187 case F_RDLCK:
2188 if (!(fl->fl_file->f_mode & FMODE_READ))
2189 return -EBADF;
2190 break;
2191 case F_WRLCK:
2192 if (!(fl->fl_file->f_mode & FMODE_WRITE))
2193 return -EBADF;
2194 }
2195 return 0;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198/* Apply the lock described by l to an open file descriptor.
2199 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2200 */
Peter Staubachc2936212005-07-27 11:45:09 -07002201int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2202 struct flock __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct file_lock *file_lock = locks_alloc_lock();
2205 struct flock flock;
2206 struct inode *inode;
Al Viro0b2bac22008-05-06 13:58:34 -04002207 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int error;
2209
2210 if (file_lock == NULL)
2211 return -ENOLCK;
2212
Jeff Layton18909102016-01-06 21:26:10 -05002213 inode = file_inode(filp);
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * This might block, so we do it before checking the inode.
2217 */
2218 error = -EFAULT;
2219 if (copy_from_user(&flock, l, sizeof(flock)))
2220 goto out;
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /* Don't allow mandatory locks on files that may be memory mapped
2223 * and shared.
2224 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002225 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 error = -EAGAIN;
2227 goto out;
2228 }
2229
2230 error = flock_to_posix_lock(filp, file_lock, &flock);
2231 if (error)
2232 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002233
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002234 error = check_fmode_for_setlk(file_lock);
2235 if (error)
2236 goto out;
2237
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002238 /*
2239 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002240 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002241 */
2242 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002243 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002244 error = -EINVAL;
2245 if (flock.l_pid != 0)
2246 goto out;
2247
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002248 cmd = F_SETLK;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002249 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002250 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002251 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002252 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002253 error = -EINVAL;
2254 if (flock.l_pid != 0)
2255 goto out;
2256
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002257 cmd = F_SETLKW;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002258 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002259 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002260 /* Fallthrough */
2261 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 file_lock->fl_flags |= FL_SLEEP;
2263 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002264
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002265 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Peter Staubachc2936212005-07-27 11:45:09 -07002267 /*
Jeff Layton0752ba82016-01-08 07:30:43 -05002268 * Attempt to detect a close/fcntl race and recover by releasing the
2269 * lock that was just acquired. There is no need to do that when we're
2270 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002271 */
Jeff Layton0752ba82016-01-08 07:30:43 -05002272 if (!error && file_lock->fl_type != F_UNLCK &&
2273 !(file_lock->fl_flags & FL_OFDLCK)) {
Jeff Layton7f3697e2016-01-07 16:38:10 -05002274 /*
2275 * We need that spin_lock here - it prevents reordering between
2276 * update of i_flctx->flc_posix and check for it done in
2277 * close(). rcu_read_lock() wouldn't do.
2278 */
2279 spin_lock(&current->files->file_lock);
2280 f = fcheck(fd);
2281 spin_unlock(&current->files->file_lock);
2282 if (f != filp) {
2283 file_lock->fl_type = F_UNLCK;
2284 error = do_lock_file_wait(filp, cmd, file_lock);
2285 WARN_ON_ONCE(error);
2286 error = -EBADF;
2287 }
Peter Staubachc2936212005-07-27 11:45:09 -07002288 }
Peter Staubachc2936212005-07-27 11:45:09 -07002289out:
Jeff Layton18909102016-01-06 21:26:10 -05002290 trace_fcntl_setlk(inode, file_lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 locks_free_lock(file_lock);
2292 return error;
2293}
2294
2295#if BITS_PER_LONG == 32
2296/* Report the first existing lock that would conflict with l.
2297 * This implements the F_GETLK command of fcntl().
2298 */
Jeff Laytonc1e62b82014-02-03 12:13:09 -05002299int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002301 struct file_lock file_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct flock64 flock;
2303 int error;
2304
2305 error = -EFAULT;
2306 if (copy_from_user(&flock, l, sizeof(flock)))
2307 goto out;
2308 error = -EINVAL;
2309 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
2310 goto out;
2311
2312 error = flock64_to_posix_lock(filp, &file_lock, &flock);
2313 if (error)
2314 goto out;
2315
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002316 if (cmd == F_OFD_GETLK) {
Jeff Layton90478932014-03-04 10:30:23 -05002317 error = -EINVAL;
2318 if (flock.l_pid != 0)
2319 goto out;
2320
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002321 cmd = F_GETLK64;
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 }
2325
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002326 error = vfs_test_lock(filp, &file_lock);
2327 if (error)
2328 goto out;
2329
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002330 flock.l_type = file_lock.fl_type;
2331 if (file_lock.fl_type != F_UNLCK)
2332 posix_lock_to_flock64(&flock, &file_lock);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 error = -EFAULT;
2335 if (!copy_to_user(l, &flock, sizeof(flock)))
2336 error = 0;
Kinglong Meef3282962014-08-22 10:18:43 -04002337
2338 locks_release_private(&file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339out:
2340 return error;
2341}
2342
2343/* Apply the lock described by l to an open file descriptor.
2344 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2345 */
Peter Staubachc2936212005-07-27 11:45:09 -07002346int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2347 struct flock64 __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct file_lock *file_lock = locks_alloc_lock();
2350 struct flock64 flock;
2351 struct inode *inode;
Al Viro0b2bac22008-05-06 13:58:34 -04002352 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int error;
2354
2355 if (file_lock == NULL)
2356 return -ENOLCK;
2357
2358 /*
2359 * This might block, so we do it before checking the inode.
2360 */
2361 error = -EFAULT;
2362 if (copy_from_user(&flock, l, sizeof(flock)))
2363 goto out;
2364
Al Viro496ad9a2013-01-23 17:07:38 -05002365 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /* Don't allow mandatory locks on files that may be memory mapped
2368 * and shared.
2369 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002370 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 error = -EAGAIN;
2372 goto out;
2373 }
2374
2375 error = flock64_to_posix_lock(filp, file_lock, &flock);
2376 if (error)
2377 goto out;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002378
Jeff Laytoncf01f4e2014-05-09 11:41:54 -04002379 error = check_fmode_for_setlk(file_lock);
2380 if (error)
2381 goto out;
2382
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002383 /*
2384 * If the cmd is requesting file-private locks, then set the
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002385 * FL_OFDLCK flag and override the owner.
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002386 */
2387 switch (cmd) {
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002388 case F_OFD_SETLK:
Jeff Layton90478932014-03-04 10:30:23 -05002389 error = -EINVAL;
2390 if (flock.l_pid != 0)
2391 goto out;
2392
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002393 cmd = F_SETLK64;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002394 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002395 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002396 break;
Jeff Layton0d3f7a22014-04-22 08:23:58 -04002397 case F_OFD_SETLKW:
Jeff Layton90478932014-03-04 10:30:23 -05002398 error = -EINVAL;
2399 if (flock.l_pid != 0)
2400 goto out;
2401
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002402 cmd = F_SETLKW64;
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002403 file_lock->fl_flags |= FL_OFDLCK;
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002404 file_lock->fl_owner = filp;
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002405 /* Fallthrough */
2406 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 file_lock->fl_flags |= FL_SLEEP;
2408 }
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002409
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002410 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Peter Staubachc2936212005-07-27 11:45:09 -07002412 /*
Jeff Layton0752ba82016-01-08 07:30:43 -05002413 * Attempt to detect a close/fcntl race and recover by releasing the
2414 * lock that was just acquired. There is no need to do that when we're
2415 * unlocking though, or for OFD locks.
Peter Staubachc2936212005-07-27 11:45:09 -07002416 */
Jeff Layton0752ba82016-01-08 07:30:43 -05002417 if (!error && file_lock->fl_type != F_UNLCK &&
2418 !(file_lock->fl_flags & FL_OFDLCK)) {
Jeff Layton7f3697e2016-01-07 16:38:10 -05002419 /*
2420 * We need that spin_lock here - it prevents reordering between
2421 * update of i_flctx->flc_posix and check for it done in
2422 * close(). rcu_read_lock() wouldn't do.
2423 */
2424 spin_lock(&current->files->file_lock);
2425 f = fcheck(fd);
2426 spin_unlock(&current->files->file_lock);
2427 if (f != filp) {
2428 file_lock->fl_type = F_UNLCK;
2429 error = do_lock_file_wait(filp, cmd, file_lock);
2430 WARN_ON_ONCE(error);
2431 error = -EBADF;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434out:
2435 locks_free_lock(file_lock);
2436 return error;
2437}
2438#endif /* BITS_PER_LONG == 32 */
2439
2440/*
2441 * This function is called when the file is being removed
2442 * from the task's fd array. POSIX locks belonging to this task
2443 * are deleted at this time.
2444 */
2445void locks_remove_posix(struct file *filp, fl_owner_t owner)
2446{
Jeff Layton18909102016-01-06 21:26:10 -05002447 int error;
Miklos Szerediff7b86b2006-06-23 02:05:11 -07002448 struct file_lock lock;
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002449 struct file_lock_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /*
2452 * If there are no locks held on this file, we don't need to call
2453 * posix_lock_file(). Another process could be setting a lock on this
2454 * file at the same time, but we wouldn't remove that lock anyway.
2455 */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002456 ctx = smp_load_acquire(&file_inode(filp)->i_flctx);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05002457 if (!ctx || list_empty(&ctx->flc_posix))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return;
2459
2460 lock.fl_type = F_UNLCK;
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07002461 lock.fl_flags = FL_POSIX | FL_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 lock.fl_start = 0;
2463 lock.fl_end = OFFSET_MAX;
2464 lock.fl_owner = owner;
2465 lock.fl_pid = current->tgid;
2466 lock.fl_file = filp;
2467 lock.fl_ops = NULL;
2468 lock.fl_lmops = NULL;
2469
Jeff Layton18909102016-01-06 21:26:10 -05002470 error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2473 lock.fl_ops->fl_release_private(&lock);
Jeff Layton18909102016-01-06 21:26:10 -05002474 trace_locks_remove_posix(file_inode(filp), &lock, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477EXPORT_SYMBOL(locks_remove_posix);
2478
Jeff Layton3d8e5602015-01-16 15:05:58 -05002479/* The i_flctx must be valid when calling into here */
Jeff Laytondd459bb2015-01-16 15:05:54 -05002480static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002481locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
Jeff Laytondd459bb2015-01-16 15:05:54 -05002482{
2483 struct file_lock fl = {
2484 .fl_owner = filp,
2485 .fl_pid = current->tgid,
2486 .fl_file = filp,
2487 .fl_flags = FL_FLOCK,
2488 .fl_type = F_UNLCK,
2489 .fl_end = OFFSET_MAX,
2490 };
Jeff Laytonbcd7f782015-07-11 06:43:02 -04002491 struct inode *inode = file_inode(filp);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002492
Jeff Layton3d8e5602015-01-16 15:05:58 -05002493 if (list_empty(&flctx->flc_flock))
Jeff Laytondd459bb2015-01-16 15:05:54 -05002494 return;
2495
2496 if (filp->f_op->flock)
2497 filp->f_op->flock(filp, F_SETLKW, &fl);
2498 else
Jeff Laytonbcd7f782015-07-11 06:43:02 -04002499 flock_lock_inode(inode, &fl);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002500
2501 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2502 fl.fl_ops->fl_release_private(&fl);
2503}
2504
Jeff Layton3d8e5602015-01-16 15:05:58 -05002505/* The i_flctx must be valid when calling into here */
Jeff Layton8634b512015-01-16 15:05:55 -05002506static void
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002507locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
Jeff Layton8634b512015-01-16 15:05:55 -05002508{
Jeff Layton8634b512015-01-16 15:05:55 -05002509 struct file_lock *fl, *tmp;
2510 LIST_HEAD(dispose);
2511
Jeff Layton3d8e5602015-01-16 15:05:58 -05002512 if (list_empty(&ctx->flc_lease))
Jeff Layton8634b512015-01-16 15:05:55 -05002513 return;
2514
Jeff Layton6109c852015-01-16 15:05:57 -05002515 spin_lock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05002516 list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
Jeff Laytonc4e136c2015-02-16 19:37:42 -05002517 if (filp == fl->fl_file)
2518 lease_modify(fl, F_UNLCK, &dispose);
Jeff Layton6109c852015-01-16 15:05:57 -05002519 spin_unlock(&ctx->flc_lock);
Jeff Layton8634b512015-01-16 15:05:55 -05002520 locks_dispose_list(&dispose);
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523/*
2524 * This function is called on the last close of an open file.
2525 */
Jeff Layton78ed8a12014-02-03 12:13:08 -05002526void locks_remove_file(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002528 struct file_lock_context *ctx;
2529
2530 ctx = smp_load_acquire(&file_inode(filp)->i_flctx);
2531 if (!ctx)
Jeff Layton3d8e5602015-01-16 15:05:58 -05002532 return;
2533
Jeff Laytondd459bb2015-01-16 15:05:54 -05002534 /* remove any OFD locks */
Christoph Hellwig73a8f5f2014-07-13 17:00:38 +02002535 locks_remove_posix(filp, filp);
Jeff Layton5d50ffd2014-02-03 12:13:10 -05002536
Jeff Laytondd459bb2015-01-16 15:05:54 -05002537 /* remove flock locks */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002538 locks_remove_flock(filp, ctx);
Jeff Laytondd459bb2015-01-16 15:05:54 -05002539
Jeff Layton8634b512015-01-16 15:05:55 -05002540 /* remove any leases */
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002541 locks_remove_lease(filp, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
2544/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * posix_unblock_lock - stop waiting for a file lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * @waiter: the lock which was waiting
2547 *
2548 * lockd needs to block waiting for locks.
2549 */
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002550int
Jeff Laytonf891a292013-06-21 08:58:09 -04002551posix_unblock_lock(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002553 int status = 0;
2554
Jeff Layton7b2296a2013-06-21 08:58:20 -04002555 spin_lock(&blocked_lock_lock);
J. Bruce Fields5996a292006-01-03 09:55:44 +01002556 if (waiter->fl_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 __locks_delete_block(waiter);
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002558 else
2559 status = -ENOENT;
Jeff Layton7b2296a2013-06-21 08:58:20 -04002560 spin_unlock(&blocked_lock_lock);
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563EXPORT_SYMBOL(posix_unblock_lock);
2564
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002565/**
2566 * vfs_cancel_lock - file byte range unblock lock
2567 * @filp: The file to apply the unblock to
2568 * @fl: The lock to be unblocked
2569 *
2570 * Used by lock managers to cancel blocked requests
2571 */
2572int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2573{
Al Viro72c2d532013-09-22 16:27:52 -04002574 if (filp->f_op->lock)
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002575 return filp->f_op->lock(filp, F_CANCELLK, fl);
2576 return 0;
2577}
2578
2579EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2580
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002581#ifdef CONFIG_PROC_FS
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002582#include <linux/proc_fs.h>
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002583#include <linux/seq_file.h>
2584
Jeff Layton7012b022013-06-21 08:58:22 -04002585struct locks_iterator {
2586 int li_cpu;
2587 loff_t li_pos;
2588};
2589
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002590static void lock_get_status(struct seq_file *f, struct file_lock *fl,
Jerome Marchand99dc8292010-10-26 14:22:33 -07002591 loff_t id, char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct inode *inode = NULL;
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002594 unsigned int fl_pid;
2595
2596 if (fl->fl_nspid)
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -08002597 fl_pid = pid_vnr(fl->fl_nspid);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002598 else
2599 fl_pid = fl->fl_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 if (fl->fl_file != NULL)
Al Viro496ad9a2013-01-23 17:07:38 -05002602 inode = file_inode(fl->fl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Jerome Marchand99dc8292010-10-26 14:22:33 -07002604 seq_printf(f, "%lld:%s ", id, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (IS_POSIX(fl)) {
Jeff Laytonc918d422014-02-03 12:13:09 -05002606 if (fl->fl_flags & FL_ACCESS)
Fabian Frederick5315c262014-05-09 14:13:05 -04002607 seq_puts(f, "ACCESS");
Jeff Laytoncff2fce2014-04-22 08:24:32 -04002608 else if (IS_OFDLCK(fl))
Fabian Frederick5315c262014-05-09 14:13:05 -04002609 seq_puts(f, "OFDLCK");
Jeff Laytonc918d422014-02-03 12:13:09 -05002610 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002611 seq_puts(f, "POSIX ");
Jeff Laytonc918d422014-02-03 12:13:09 -05002612
2613 seq_printf(f, " %s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 (inode == NULL) ? "*NOINODE*" :
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002615 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 } else if (IS_FLOCK(fl)) {
2617 if (fl->fl_type & LOCK_MAND) {
Fabian Frederick5315c262014-05-09 14:13:05 -04002618 seq_puts(f, "FLOCK MSNFS ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002620 seq_puts(f, "FLOCK ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622 } else if (IS_LEASE(fl)) {
Jeff Layton8144f1f2014-08-11 13:36:54 -04002623 if (fl->fl_flags & FL_DELEG)
2624 seq_puts(f, "DELEG ");
2625 else
2626 seq_puts(f, "LEASE ");
2627
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002628 if (lease_breaking(fl))
Fabian Frederick5315c262014-05-09 14:13:05 -04002629 seq_puts(f, "BREAKING ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 else if (fl->fl_file)
Fabian Frederick5315c262014-05-09 14:13:05 -04002631 seq_puts(f, "ACTIVE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 else
Fabian Frederick5315c262014-05-09 14:13:05 -04002633 seq_puts(f, "BREAKER ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002635 seq_puts(f, "UNKNOWN UNKNOWN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637 if (fl->fl_type & LOCK_MAND) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002638 seq_printf(f, "%s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 (fl->fl_type & LOCK_READ)
2640 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2641 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2642 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002643 seq_printf(f, "%s ",
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002644 (lease_breaking(fl))
Jeff Layton0ee5c6d2012-08-02 15:46:30 -04002645 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2646 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648 if (inode) {
Jeff Layton36488882015-04-03 09:04:04 -04002649 /* userspace relies on this representation of dev_t */
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002650 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 MAJOR(inode->i_sb->s_dev),
2652 MINOR(inode->i_sb->s_dev), inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 } else {
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002654 seq_printf(f, "%d <none>:0 ", fl_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 if (IS_POSIX(fl)) {
2657 if (fl->fl_end == OFFSET_MAX)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002658 seq_printf(f, "%Ld EOF\n", fl->fl_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 else
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002660 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 } else {
Fabian Frederick5315c262014-05-09 14:13:05 -04002662 seq_puts(f, "0 EOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664}
2665
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002666static int locks_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Jeff Layton7012b022013-06-21 08:58:22 -04002668 struct locks_iterator *iter = f->private;
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002669 struct file_lock *fl, *bfl;
2670
Jeff Layton139ca042013-06-21 08:58:17 -04002671 fl = hlist_entry(v, struct file_lock, fl_link);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002672
Jeff Layton7012b022013-06-21 08:58:22 -04002673 lock_get_status(f, fl, iter->li_pos, "");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002674
2675 list_for_each_entry(bfl, &fl->fl_block, fl_block)
Jeff Layton7012b022013-06-21 08:58:22 -04002676 lock_get_status(f, bfl, iter->li_pos, " ->");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002677
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002681static void __show_fd_locks(struct seq_file *f,
2682 struct list_head *head, int *id,
2683 struct file *filp, struct files_struct *files)
2684{
2685 struct file_lock *fl;
2686
2687 list_for_each_entry(fl, head, fl_list) {
2688
2689 if (filp != fl->fl_file)
2690 continue;
2691 if (fl->fl_owner != files &&
2692 fl->fl_owner != filp)
2693 continue;
2694
2695 (*id)++;
2696 seq_puts(f, "lock:\t");
2697 lock_get_status(f, fl, *id, "");
2698 }
2699}
2700
2701void show_fd_locks(struct seq_file *f,
2702 struct file *filp, struct files_struct *files)
2703{
2704 struct inode *inode = file_inode(filp);
2705 struct file_lock_context *ctx;
2706 int id = 0;
2707
Dmitry Vyukov128a3782015-09-21 09:43:06 +02002708 ctx = smp_load_acquire(&inode->i_flctx);
Andrey Vagin6c8c9032015-04-16 12:49:38 -07002709 if (!ctx)
2710 return;
2711
2712 spin_lock(&ctx->flc_lock);
2713 __show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
2714 __show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
2715 __show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
2716 spin_unlock(&ctx->flc_lock);
2717}
2718
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002719static void *locks_start(struct seq_file *f, loff_t *pos)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002720 __acquires(&blocked_lock_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Jeff Layton7012b022013-06-21 08:58:22 -04002722 struct locks_iterator *iter = f->private;
Jerome Marchand99dc8292010-10-26 14:22:33 -07002723
Jeff Layton7012b022013-06-21 08:58:22 -04002724 iter->li_pos = *pos + 1;
Peter Zijlstraaba37662015-06-22 14:16:33 +02002725 percpu_down_write(&file_rwsem);
Jeff Layton7012b022013-06-21 08:58:22 -04002726 lg_global_lock(&file_lock_lglock);
Jeff Layton7b2296a2013-06-21 08:58:20 -04002727 spin_lock(&blocked_lock_lock);
Jeff Layton7012b022013-06-21 08:58:22 -04002728 return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002731static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2732{
Jeff Layton7012b022013-06-21 08:58:22 -04002733 struct locks_iterator *iter = f->private;
2734
2735 ++iter->li_pos;
2736 return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002737}
2738
2739static void locks_stop(struct seq_file *f, void *v)
Jeff Laytonb03dfdec2014-02-03 12:13:07 -05002740 __releases(&blocked_lock_lock)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002741{
Jeff Layton7b2296a2013-06-21 08:58:20 -04002742 spin_unlock(&blocked_lock_lock);
Jeff Layton7012b022013-06-21 08:58:22 -04002743 lg_global_unlock(&file_lock_lglock);
Peter Zijlstraaba37662015-06-22 14:16:33 +02002744 percpu_up_write(&file_rwsem);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002745}
2746
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002747static const struct seq_operations locks_seq_operations = {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002748 .start = locks_start,
2749 .next = locks_next,
2750 .stop = locks_stop,
2751 .show = locks_show,
2752};
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002753
2754static int locks_open(struct inode *inode, struct file *filp)
2755{
Jeff Layton7012b022013-06-21 08:58:22 -04002756 return seq_open_private(filp, &locks_seq_operations,
2757 sizeof(struct locks_iterator));
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002758}
2759
2760static const struct file_operations proc_locks_operations = {
2761 .open = locks_open,
2762 .read = seq_read,
2763 .llseek = seq_lseek,
Jerome Marchand99dc8292010-10-26 14:22:33 -07002764 .release = seq_release_private,
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002765};
2766
2767static int __init proc_locks_init(void)
2768{
2769 proc_create("locks", 0, NULL, &proc_locks_operations);
2770 return 0;
2771}
Paul Gortmaker91899222015-12-17 14:11:03 -05002772fs_initcall(proc_locks_init);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002773#endif
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775static int __init filelock_init(void)
2776{
Jeff Layton7012b022013-06-21 08:58:22 -04002777 int i;
2778
Jeff Layton4a075e32015-01-16 15:05:54 -05002779 flctx_cache = kmem_cache_create("file_lock_ctx",
2780 sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 filelock_cache = kmem_cache_create("file_lock_cache",
Miklos Szerediee19cc42011-07-07 13:06:09 +02002783 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2784
Jeff Layton7012b022013-06-21 08:58:22 -04002785 lg_lock_init(&file_lock_lglock, "file_lock_lglock");
2786
2787 for_each_possible_cpu(i)
2788 INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return 0;
2791}
2792
2793core_initcall(filelock_init);