blob: 04e2c1fdb157afeb0bc8fe278a74909cd239126b [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>
122#include <linux/module.h>
123#include <linux/security.h>
124#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#include <linux/syscalls.h>
126#include <linux/time.h>
Dipankar Sarma4fb3a532005-09-16 19:28:13 -0700127#include <linux/rcupdate.h>
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000128#include <linux/pid_namespace.h>
Jeff Layton48f74182013-06-21 08:58:18 -0400129#include <linux/hashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#include <asm/uaccess.h>
132
133#define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
134#define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
135#define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
136
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -0400137static bool lease_breaking(struct file_lock *fl)
138{
J. Bruce Fields778fc542011-07-26 18:25:49 -0400139 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
140}
141
142static int target_leasetype(struct file_lock *fl)
143{
144 if (fl->fl_flags & FL_UNLOCK_PENDING)
145 return F_UNLCK;
146 if (fl->fl_flags & FL_DOWNGRADE_PENDING)
147 return F_RDLCK;
148 return fl->fl_type;
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -0400149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151int leases_enable = 1;
152int lease_break_time = 45;
153
154#define for_each_lock(inode, lockp) \
155 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
156
Jeff Layton1c8c6012013-06-21 08:58:15 -0400157/*
158 * The global file_lock_list is only used for displaying /proc/locks. Protected
159 * by the file_lock_lock.
160 */
Jeff Layton139ca042013-06-21 08:58:17 -0400161static HLIST_HEAD(file_lock_list);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400162static DEFINE_SPINLOCK(file_lock_lock);
Jeff Layton88974692013-06-21 08:58:14 -0400163
Jeff Layton1c8c6012013-06-21 08:58:15 -0400164/*
Jeff Layton48f74182013-06-21 08:58:18 -0400165 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
Jeff Layton7b2296a2013-06-21 08:58:20 -0400166 * It is protected by blocked_lock_lock.
Jeff Layton48f74182013-06-21 08:58:18 -0400167 *
168 * We hash locks by lockowner in order to optimize searching for the lock a
169 * particular lockowner is waiting on.
170 *
171 * FIXME: make this value scale via some heuristic? We generally will want more
172 * buckets when we have more lockowners holding locks, but that's a little
173 * difficult to determine without knowing what the workload will look like.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400174 */
Jeff Layton48f74182013-06-21 08:58:18 -0400175#define BLOCKED_HASH_BITS 7
176static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
Jeff Layton88974692013-06-21 08:58:14 -0400177
Jeff Layton1c8c6012013-06-21 08:58:15 -0400178/*
Jeff Layton7b2296a2013-06-21 08:58:20 -0400179 * This lock protects the blocked_hash. Generally, if you're accessing it, you
180 * want to be holding this lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400181 *
182 * In addition, it also protects the fl->fl_block list, and the fl->fl_next
183 * pointer for file_lock structures that are acting as lock requests (in
184 * contrast to those that are acting as records of acquired locks).
185 *
186 * Note that when we acquire this lock in order to change the above fields,
187 * we often hold the i_lock as well. In certain cases, when reading the fields
188 * protected by this lock, we can skip acquiring it iff we already hold the
189 * i_lock.
190 *
191 * In particular, adding an entry to the fl_block list requires that you hold
192 * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
193 * an entry from the list however only requires the file_lock_lock.
194 */
Jeff Layton7b2296a2013-06-21 08:58:20 -0400195static DEFINE_SPINLOCK(blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Christoph Lametere18b8902006-12-06 20:33:20 -0800197static struct kmem_cache *filelock_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Miklos Szerediee19cc42011-07-07 13:06:09 +0200199static void locks_init_lock_heads(struct file_lock *fl)
Miklos Szeredia51cb912011-07-06 12:33:55 +0200200{
Jeff Layton139ca042013-06-21 08:58:17 -0400201 INIT_HLIST_NODE(&fl->fl_link);
Miklos Szerediee19cc42011-07-07 13:06:09 +0200202 INIT_LIST_HEAD(&fl->fl_block);
203 init_waitqueue_head(&fl->fl_wait);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Allocate an empty lock structure. */
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200207struct file_lock *locks_alloc_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200209 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200210
211 if (fl)
Miklos Szerediee19cc42011-07-07 13:06:09 +0200212 locks_init_lock_heads(fl);
Miklos Szeredia51cb912011-07-06 12:33:55 +0200213
214 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +0200216EXPORT_SYMBOL_GPL(locks_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500218void locks_release_private(struct file_lock *fl)
Trond Myklebust47831f32006-03-20 13:44:05 -0500219{
220 if (fl->fl_ops) {
221 if (fl->fl_ops->fl_release_private)
222 fl->fl_ops->fl_release_private(fl);
223 fl->fl_ops = NULL;
224 }
J. Bruce Fields068535f2012-08-01 07:56:16 -0400225 fl->fl_lmops = NULL;
Trond Myklebust47831f32006-03-20 13:44:05 -0500226
227}
Felix Blyakhera9e61e22009-03-31 15:12:56 -0500228EXPORT_SYMBOL_GPL(locks_release_private);
Trond Myklebust47831f32006-03-20 13:44:05 -0500229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* Free a lock which is not in use. */
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400231void locks_free_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Miklos Szeredi5ce29642006-03-31 02:30:29 -0800233 BUG_ON(waitqueue_active(&fl->fl_wait));
234 BUG_ON(!list_empty(&fl->fl_block));
Jeff Layton139ca042013-06-21 08:58:17 -0400235 BUG_ON(!hlist_unhashed(&fl->fl_link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Trond Myklebust47831f32006-03-20 13:44:05 -0500237 locks_release_private(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kmem_cache_free(filelock_cache, fl);
239}
J. Bruce Fields05fa3132010-10-30 17:31:15 -0400240EXPORT_SYMBOL(locks_free_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242void locks_init_lock(struct file_lock *fl)
243{
Miklos Szerediee19cc42011-07-07 13:06:09 +0200244 memset(fl, 0, sizeof(struct file_lock));
245 locks_init_lock_heads(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248EXPORT_SYMBOL(locks_init_lock);
249
Trond Myklebust47831f32006-03-20 13:44:05 -0500250static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
251{
252 if (fl->fl_ops) {
253 if (fl->fl_ops->fl_copy_lock)
254 fl->fl_ops->fl_copy_lock(new, fl);
255 new->fl_ops = fl->fl_ops;
256 }
Christoph Hellwigbb8430a2010-10-31 08:35:31 -0400257 if (fl->fl_lmops)
Trond Myklebust47831f32006-03-20 13:44:05 -0500258 new->fl_lmops = fl->fl_lmops;
Trond Myklebust47831f32006-03-20 13:44:05 -0500259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 * Initialize a new lock from an existing file_lock structure.
263 */
J. Bruce Fields1a747ee2008-04-24 10:08:22 -0400264void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 new->fl_owner = fl->fl_owner;
267 new->fl_pid = fl->fl_pid;
Trond Myklebust09969052006-03-20 13:44:38 -0500268 new->fl_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 new->fl_flags = fl->fl_flags;
270 new->fl_type = fl->fl_type;
271 new->fl_start = fl->fl_start;
272 new->fl_end = fl->fl_end;
Trond Myklebust09969052006-03-20 13:44:38 -0500273 new->fl_ops = NULL;
274 new->fl_lmops = NULL;
275}
Roland Dreier3dd7b712008-04-25 15:32:51 -0700276EXPORT_SYMBOL(__locks_copy_lock);
Trond Myklebust09969052006-03-20 13:44:38 -0500277
278void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
279{
280 locks_release_private(new);
281
282 __locks_copy_lock(new, fl);
283 new->fl_file = fl->fl_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 new->fl_ops = fl->fl_ops;
285 new->fl_lmops = fl->fl_lmops;
Trond Myklebust47831f32006-03-20 13:44:05 -0500286
287 locks_copy_private(new, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290EXPORT_SYMBOL(locks_copy_lock);
291
292static inline int flock_translate_cmd(int cmd) {
293 if (cmd & LOCK_MAND)
294 return cmd & (LOCK_MAND | LOCK_RW);
295 switch (cmd) {
296 case LOCK_SH:
297 return F_RDLCK;
298 case LOCK_EX:
299 return F_WRLCK;
300 case LOCK_UN:
301 return F_UNLCK;
302 }
303 return -EINVAL;
304}
305
306/* Fill in a file_lock structure with an appropriate FLOCK lock. */
307static int flock_make_lock(struct file *filp, struct file_lock **lock,
308 unsigned int cmd)
309{
310 struct file_lock *fl;
311 int type = flock_translate_cmd(cmd);
312 if (type < 0)
313 return type;
314
315 fl = locks_alloc_lock();
316 if (fl == NULL)
317 return -ENOMEM;
318
319 fl->fl_file = filp;
320 fl->fl_pid = current->tgid;
321 fl->fl_flags = FL_FLOCK;
322 fl->fl_type = type;
323 fl->fl_end = OFFSET_MAX;
324
325 *lock = fl;
326 return 0;
327}
328
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400329static int assign_type(struct file_lock *fl, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 switch (type) {
332 case F_RDLCK:
333 case F_WRLCK:
334 case F_UNLCK:
335 fl->fl_type = type;
336 break;
337 default:
338 return -EINVAL;
339 }
340 return 0;
341}
342
343/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
344 * style lock.
345 */
346static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
347 struct flock *l)
348{
349 off_t start, end;
350
351 switch (l->l_whence) {
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700352 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 start = 0;
354 break;
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700355 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 start = filp->f_pos;
357 break;
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700358 case SEEK_END:
Al Viro496ad9a2013-01-23 17:07:38 -0500359 start = i_size_read(file_inode(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 default:
362 return -EINVAL;
363 }
364
365 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
366 POSIX-2001 defines it. */
367 start += l->l_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (start < 0)
369 return -EINVAL;
Trond Myklebust4c780a42005-10-18 14:20:21 -0700370 fl->fl_end = OFFSET_MAX;
371 if (l->l_len > 0) {
372 end = start + l->l_len - 1;
373 fl->fl_end = end;
374 } else if (l->l_len < 0) {
375 end = start - 1;
376 fl->fl_end = end;
377 start += l->l_len;
378 if (start < 0)
379 return -EINVAL;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 fl->fl_start = start; /* we record the absolute position */
Trond Myklebust4c780a42005-10-18 14:20:21 -0700382 if (fl->fl_end < fl->fl_start)
383 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 fl->fl_owner = current->files;
386 fl->fl_pid = current->tgid;
387 fl->fl_file = filp;
388 fl->fl_flags = FL_POSIX;
389 fl->fl_ops = NULL;
390 fl->fl_lmops = NULL;
391
392 return assign_type(fl, l->l_type);
393}
394
395#if BITS_PER_LONG == 32
396static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
397 struct flock64 *l)
398{
399 loff_t start;
400
401 switch (l->l_whence) {
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700402 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 start = 0;
404 break;
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700405 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 start = filp->f_pos;
407 break;
Josef 'Jeff' Sipekf5579f82006-09-30 23:27:35 -0700408 case SEEK_END:
Al Viro496ad9a2013-01-23 17:07:38 -0500409 start = i_size_read(file_inode(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411 default:
412 return -EINVAL;
413 }
414
Trond Myklebust4c780a42005-10-18 14:20:21 -0700415 start += l->l_start;
416 if (start < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVAL;
Trond Myklebust4c780a42005-10-18 14:20:21 -0700418 fl->fl_end = OFFSET_MAX;
419 if (l->l_len > 0) {
420 fl->fl_end = start + l->l_len - 1;
421 } else if (l->l_len < 0) {
422 fl->fl_end = start - 1;
423 start += l->l_len;
424 if (start < 0)
425 return -EINVAL;
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 fl->fl_start = start; /* we record the absolute position */
Trond Myklebust4c780a42005-10-18 14:20:21 -0700428 if (fl->fl_end < fl->fl_start)
429 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 fl->fl_owner = current->files;
432 fl->fl_pid = current->tgid;
433 fl->fl_file = filp;
434 fl->fl_flags = FL_POSIX;
435 fl->fl_ops = NULL;
436 fl->fl_lmops = NULL;
437
Namhyung Kimf32cb532011-01-17 15:45:59 +0900438 return assign_type(fl, l->l_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440#endif
441
442/* default lease lock manager operations */
443static void lease_break_callback(struct file_lock *fl)
444{
445 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
446}
447
Alexey Dobriyan7b021962009-09-21 17:01:12 -0700448static const struct lock_manager_operations lease_manager_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400449 .lm_break = lease_break_callback,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400450 .lm_change = lease_modify,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
453/*
454 * Initialize a lease, use the default lock manager operations
455 */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400456static int lease_init(struct file *filp, long type, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 {
Trond Myklebust75dff552006-05-07 23:02:42 -0400458 if (assign_type(fl, type) != 0)
459 return -EINVAL;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 fl->fl_owner = current->files;
462 fl->fl_pid = current->tgid;
463
464 fl->fl_file = filp;
465 fl->fl_flags = FL_LEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 fl->fl_start = 0;
467 fl->fl_end = OFFSET_MAX;
468 fl->fl_ops = NULL;
469 fl->fl_lmops = &lease_manager_ops;
470 return 0;
471}
472
473/* Allocate a file_lock initialised to this type of lease */
J. Bruce Fields0ec4f432012-07-23 15:17:17 -0400474static struct file_lock *lease_alloc(struct file *filp, long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct file_lock *fl = locks_alloc_lock();
Trond Myklebust75dff552006-05-07 23:02:42 -0400477 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (fl == NULL)
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500480 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 error = lease_init(filp, type, fl);
Trond Myklebust75dff552006-05-07 23:02:42 -0400483 if (error) {
484 locks_free_lock(fl);
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500485 return ERR_PTR(error);
Trond Myklebust75dff552006-05-07 23:02:42 -0400486 }
J. Bruce Fieldse32b8ee2007-03-01 14:34:35 -0500487 return fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/* Check if two locks overlap each other.
491 */
492static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
493{
494 return ((fl1->fl_end >= fl2->fl_start) &&
495 (fl2->fl_end >= fl1->fl_start));
496}
497
498/*
499 * Check whether two locks have the same owner.
500 */
Matt Mackall33443c42006-01-08 01:05:22 -0800501static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400503 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return fl2->fl_lmops == fl1->fl_lmops &&
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400505 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return fl1->fl_owner == fl2->fl_owner;
507}
508
Jeff Layton88974692013-06-21 08:58:14 -0400509static inline void
510locks_insert_global_locks(struct file_lock *fl)
511{
Jeff Layton1c8c6012013-06-21 08:58:15 -0400512 spin_lock(&file_lock_lock);
Jeff Layton139ca042013-06-21 08:58:17 -0400513 hlist_add_head(&fl->fl_link, &file_lock_list);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400514 spin_unlock(&file_lock_lock);
Jeff Layton88974692013-06-21 08:58:14 -0400515}
516
517static inline void
518locks_delete_global_locks(struct file_lock *fl)
519{
Jeff Layton1c8c6012013-06-21 08:58:15 -0400520 spin_lock(&file_lock_lock);
Jeff Layton139ca042013-06-21 08:58:17 -0400521 hlist_del_init(&fl->fl_link);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400522 spin_unlock(&file_lock_lock);
Jeff Layton88974692013-06-21 08:58:14 -0400523}
524
Jeff Layton3999e492013-06-21 08:58:19 -0400525static unsigned long
526posix_owner_key(struct file_lock *fl)
527{
528 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
529 return fl->fl_lmops->lm_owner_key(fl);
530 return (unsigned long)fl->fl_owner;
531}
532
Jeff Layton88974692013-06-21 08:58:14 -0400533static inline void
534locks_insert_global_blocked(struct file_lock *waiter)
535{
Jeff Layton3999e492013-06-21 08:58:19 -0400536 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
Jeff Layton88974692013-06-21 08:58:14 -0400537}
538
539static inline void
540locks_delete_global_blocked(struct file_lock *waiter)
541{
Jeff Layton48f74182013-06-21 08:58:18 -0400542 hash_del(&waiter->fl_link);
Jeff Layton88974692013-06-21 08:58:14 -0400543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* Remove waiter from blocker's block list.
546 * When blocker ends up pointing to itself then the list is empty.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400547 *
Jeff Layton7b2296a2013-06-21 08:58:20 -0400548 * Must be called with blocked_lock_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Matt Mackall33443c42006-01-08 01:05:22 -0800550static void __locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jeff Layton88974692013-06-21 08:58:14 -0400552 locks_delete_global_blocked(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 list_del_init(&waiter->fl_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 waiter->fl_next = NULL;
555}
556
Jeff Layton1a9e64a2013-06-21 08:58:10 -0400557static void locks_delete_block(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400559 spin_lock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 __locks_delete_block(waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400561 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/* Insert waiter into blocker's block list.
565 * We use a circular list so that processes can be easily woken up in
566 * the order they blocked. The documentation doesn't require this but
567 * it seems like the reasonable thing to do.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400568 *
Jeff Layton7b2296a2013-06-21 08:58:20 -0400569 * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
Jeff Layton4e8c7652013-06-21 08:58:16 -0400570 * list itself is protected by the file_lock_list, but by ensuring that the
Jeff Layton7b2296a2013-06-21 08:58:20 -0400571 * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
Jeff Layton4e8c7652013-06-21 08:58:16 -0400572 * in some cases when we see that the fl_block list is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Jeff Layton1c8c6012013-06-21 08:58:15 -0400574static void __locks_insert_block(struct file_lock *blocker,
575 struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
J. Bruce Fields6dc0fe82006-03-26 01:37:24 -0800577 BUG_ON(!list_empty(&waiter->fl_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 waiter->fl_next = blocker;
Jeff Layton88974692013-06-21 08:58:14 -0400579 list_add_tail(&waiter->fl_block, &blocker->fl_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (IS_POSIX(blocker))
Jeff Layton1c8c6012013-06-21 08:58:15 -0400581 locks_insert_global_blocked(waiter);
582}
583
584/* Must be called with i_lock held. */
585static void locks_insert_block(struct file_lock *blocker,
586 struct file_lock *waiter)
587{
Jeff Layton7b2296a2013-06-21 08:58:20 -0400588 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400589 __locks_insert_block(blocker, waiter);
Jeff Layton7b2296a2013-06-21 08:58:20 -0400590 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Jeff Layton1cb36012013-06-21 08:58:12 -0400593/*
594 * Wake up processes blocked waiting for blocker.
595 *
Jeff Layton1c8c6012013-06-21 08:58:15 -0400596 * Must be called with the inode->i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598static void locks_wake_up_blocks(struct file_lock *blocker)
599{
Jeff Layton4e8c7652013-06-21 08:58:16 -0400600 /*
601 * Avoid taking global lock if list is empty. This is safe since new
602 * blocked requests are only added to the list under the i_lock, and
603 * the i_lock is always held here. Note that removal from the fl_block
604 * list does not require the i_lock, so we must recheck list_empty()
Jeff Layton7b2296a2013-06-21 08:58:20 -0400605 * after acquiring the blocked_lock_lock.
Jeff Layton4e8c7652013-06-21 08:58:16 -0400606 */
607 if (list_empty(&blocker->fl_block))
608 return;
609
Jeff Layton7b2296a2013-06-21 08:58:20 -0400610 spin_lock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 while (!list_empty(&blocker->fl_block)) {
Pavel Emelyanovf0c1cd02007-09-19 16:44:07 +0400612 struct file_lock *waiter;
613
614 waiter = list_first_entry(&blocker->fl_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct file_lock, fl_block);
616 __locks_delete_block(waiter);
J. Bruce Fields8fb47a42011-07-20 20:21:59 -0400617 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
618 waiter->fl_lmops->lm_notify(waiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 else
620 wake_up(&waiter->fl_wait);
621 }
Jeff Layton7b2296a2013-06-21 08:58:20 -0400622 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/* Insert file lock fl into an inode's lock list at the position indicated
626 * by pos. At the same time add the lock to the global file lock list.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400627 *
628 * Must be called with the i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
631{
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000632 fl->fl_nspid = get_pid(task_tgid(current));
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* insert into file's list */
635 fl->fl_next = *pos;
636 *pos = fl;
Jeff Layton88974692013-06-21 08:58:14 -0400637
638 locks_insert_global_locks(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
642 * Delete a lock and then free it.
643 * Wake up processes that are blocked waiting for this lock,
644 * notify the FS that the lock has been cleared and
645 * finally free the lock.
Jeff Layton1c8c6012013-06-21 08:58:15 -0400646 *
647 * Must be called with the i_lock held!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
649static void locks_delete_lock(struct file_lock **thisfl_p)
650{
651 struct file_lock *fl = *thisfl_p;
652
Jeff Layton88974692013-06-21 08:58:14 -0400653 locks_delete_global_locks(fl);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *thisfl_p = fl->fl_next;
656 fl->fl_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000658 if (fl->fl_nspid) {
659 put_pid(fl->fl_nspid);
660 fl->fl_nspid = NULL;
661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 locks_wake_up_blocks(fl);
664 locks_free_lock(fl);
665}
666
667/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
668 * checks for shared/exclusive status of overlapping locks.
669 */
670static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
671{
672 if (sys_fl->fl_type == F_WRLCK)
673 return 1;
674 if (caller_fl->fl_type == F_WRLCK)
675 return 1;
676 return 0;
677}
678
679/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
680 * checking before calling the locks_conflict().
681 */
682static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
683{
684 /* POSIX locks owned by the same process do not conflict with
685 * each other.
686 */
687 if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
688 return (0);
689
690 /* Check whether they overlap */
691 if (!locks_overlap(caller_fl, sys_fl))
692 return 0;
693
694 return (locks_conflict(caller_fl, sys_fl));
695}
696
697/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
698 * checking before calling the locks_conflict().
699 */
700static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
701{
702 /* FLOCK locks referring to the same filp do not conflict with
703 * each other.
704 */
705 if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
706 return (0);
707 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
708 return 0;
709
710 return (locks_conflict(caller_fl, sys_fl));
711}
712
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400713void
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500714posix_test_lock(struct file *filp, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct file_lock *cfl;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400717 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jeff Layton1c8c6012013-06-21 08:58:15 -0400719 spin_lock(&inode->i_lock);
Al Viro496ad9a2013-01-23 17:07:38 -0500720 for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!IS_POSIX(cfl))
722 continue;
J. Bruce Fieldsb842e242007-05-10 19:02:07 -0400723 if (posix_locks_conflict(fl, cfl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 }
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000726 if (cfl) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500727 __locks_copy_lock(fl, cfl);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000728 if (cfl->fl_nspid)
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800729 fl->fl_pid = pid_vnr(cfl->fl_nspid);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +0000730 } else
J. Bruce Fields129a84d2007-05-10 18:38:43 -0400731 fl->fl_type = F_UNLCK;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400732 spin_unlock(&inode->i_lock);
J. Bruce Fields6d34ac12007-05-11 16:09:32 -0400733 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735EXPORT_SYMBOL(posix_test_lock);
736
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400737/*
738 * Deadlock detection:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400740 * We attempt to detect deadlocks that are due purely to posix file
741 * locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400743 * We assume that a task can be waiting for at most one lock at a time.
744 * So for any acquired lock, the process holding that lock may be
745 * waiting on at most one other lock. That lock in turns may be held by
746 * someone waiting for at most one other lock. Given a requested lock
747 * caller_fl which is about to wait for a conflicting lock block_fl, we
748 * follow this chain of waiters to ensure we are not about to create a
749 * cycle.
J. Bruce Fields97855b42007-10-30 11:20:02 -0400750 *
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400751 * Since we do this before we ever put a process to sleep on a lock, we
752 * are ensured that there is never a cycle; that is what guarantees that
753 * the while() loop in posix_locks_deadlock() eventually completes.
754 *
755 * Note: the above assumption may not be true when handling lock
756 * requests from a broken NFS client. It may also fail in the presence
757 * of tasks (such as posix threads) sharing the same open file table.
758 *
759 * To handle those cases, we just bail out after a few iterations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
J. Bruce Fields97855b42007-10-30 11:20:02 -0400761
762#define MAX_DEADLK_ITERATIONS 10
763
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400764/* Find a lock that the owner of the given block_fl is blocking on. */
765static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
766{
767 struct file_lock *fl;
768
Jeff Layton3999e492013-06-21 08:58:19 -0400769 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400770 if (posix_same_owner(fl, block_fl))
771 return fl->fl_next;
772 }
773 return NULL;
774}
775
Jeff Layton7b2296a2013-06-21 08:58:20 -0400776/* Must be called with the blocked_lock_lock held! */
Adrian Bunkb0904e12006-06-23 02:05:13 -0700777static int posix_locks_deadlock(struct file_lock *caller_fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct file_lock *block_fl)
779{
J. Bruce Fields97855b42007-10-30 11:20:02 -0400780 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
J. Bruce Fieldsb5331842007-10-26 18:05:40 -0400782 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
783 if (i++ > MAX_DEADLK_ITERATIONS)
784 return 0;
785 if (posix_same_owner(caller_fl, block_fl))
786 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 return 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
J. Bruce Fields02888f42007-09-12 15:45:07 -0400792 * after any leases, but before any posix locks.
Trond Myklebustf475ae92006-06-29 16:38:32 -0400793 *
794 * Note that if called with an FL_EXISTS argument, the caller may determine
795 * whether or not a lock was successfully freed by testing the return
796 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Trond Myklebust993dfa82006-03-31 02:30:55 -0800798static int flock_lock_file(struct file *filp, struct file_lock *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Trond Myklebust993dfa82006-03-31 02:30:55 -0800800 struct file_lock *new_fl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct file_lock **before;
Al Viro496ad9a2013-01-23 17:07:38 -0500802 struct inode * inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int error = 0;
804 int found = 0;
805
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200806 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
807 new_fl = locks_alloc_lock();
808 if (!new_fl)
809 return -ENOMEM;
810 }
811
Jeff Layton1c8c6012013-06-21 08:58:15 -0400812 spin_lock(&inode->i_lock);
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400813 if (request->fl_flags & FL_ACCESS)
814 goto find_conflict;
Pavel Emelyanov84d535a2007-09-11 16:38:13 +0400815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 for_each_lock(inode, before) {
817 struct file_lock *fl = *before;
818 if (IS_POSIX(fl))
819 break;
820 if (IS_LEASE(fl))
821 continue;
822 if (filp != fl->fl_file)
823 continue;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800824 if (request->fl_type == fl->fl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto out;
826 found = 1;
827 locks_delete_lock(before);
828 break;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Trond Myklebustf475ae92006-06-29 16:38:32 -0400831 if (request->fl_type == F_UNLCK) {
832 if ((request->fl_flags & FL_EXISTS) && !found)
833 error = -ENOENT;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800834 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -0400835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
838 * If a higher-priority process was blocked on the old file lock,
839 * give it the opportunity to lock the file.
840 */
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200841 if (found) {
Jeff Layton1c8c6012013-06-21 08:58:15 -0400842 spin_unlock(&inode->i_lock);
Frederic Weisbeckerdef01bc2009-07-16 15:44:29 +0200843 cond_resched();
Jeff Layton1c8c6012013-06-21 08:58:15 -0400844 spin_lock(&inode->i_lock);
Arnd Bergmannb89f4322010-09-18 15:09:31 +0200845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400847find_conflict:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 for_each_lock(inode, before) {
849 struct file_lock *fl = *before;
850 if (IS_POSIX(fl))
851 break;
852 if (IS_LEASE(fl))
853 continue;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800854 if (!flock_locks_conflict(request, fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 continue;
856 error = -EAGAIN;
Miklos Szeredibde74e42008-07-25 01:48:57 -0700857 if (!(request->fl_flags & FL_SLEEP))
858 goto out;
859 error = FILE_LOCK_DEFERRED;
860 locks_insert_block(fl, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
862 }
Trond Myklebustf07f18d2006-06-29 16:38:37 -0400863 if (request->fl_flags & FL_ACCESS)
864 goto out;
Trond Myklebust993dfa82006-03-31 02:30:55 -0800865 locks_copy_lock(new_fl, request);
Pavel Emelyanov0e2f6db2007-09-11 15:24:01 -0700866 locks_insert_lock(before, new_fl);
Trond Myklebust993dfa82006-03-31 02:30:55 -0800867 new_fl = NULL;
Kirill Korotaev9cedc192006-06-14 17:59:35 +0400868 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870out:
Jeff Layton1c8c6012013-06-21 08:58:15 -0400871 spin_unlock(&inode->i_lock);
Trond Myklebust993dfa82006-03-31 02:30:55 -0800872 if (new_fl)
873 locks_free_lock(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return error;
875}
876
Marc Eshel150b3932007-01-18 16:15:35 -0500877static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct file_lock *fl;
Miklos Szeredi39005d02006-06-23 02:05:10 -0700880 struct file_lock *new_fl = NULL;
881 struct file_lock *new_fl2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct file_lock *left = NULL;
883 struct file_lock *right = NULL;
884 struct file_lock **before;
Jeff Laytonb9746ef2013-06-21 08:58:13 -0400885 int error;
886 bool added = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /*
889 * We may need two file_lock structures for this operation,
890 * so we get them in advance to avoid races.
Miklos Szeredi39005d02006-06-23 02:05:10 -0700891 *
892 * In some cases we can be sure, that no new locks will be needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Miklos Szeredi39005d02006-06-23 02:05:10 -0700894 if (!(request->fl_flags & FL_ACCESS) &&
895 (request->fl_type != F_UNLCK ||
896 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
897 new_fl = locks_alloc_lock();
898 new_fl2 = locks_alloc_lock();
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jeff Layton1c8c6012013-06-21 08:58:15 -0400901 spin_lock(&inode->i_lock);
Jeff Layton1cb36012013-06-21 08:58:12 -0400902 /*
903 * New lock request. Walk all POSIX locks and look for conflicts. If
904 * there are any, either return error or put the request on the
Jeff Layton48f74182013-06-21 08:58:18 -0400905 * blocker's list of waiters and the global blocked_hash.
Jeff Layton1cb36012013-06-21 08:58:12 -0400906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (request->fl_type != F_UNLCK) {
908 for_each_lock(inode, before) {
J. Bruce Fields526985b2006-11-14 16:54:36 -0500909 fl = *before;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!IS_POSIX(fl))
911 continue;
912 if (!posix_locks_conflict(request, fl))
913 continue;
Andy Adamson5842add2006-03-26 01:37:26 -0800914 if (conflock)
J. Bruce Fields1a747ee2008-04-24 10:08:22 -0400915 __locks_copy_lock(conflock, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 error = -EAGAIN;
917 if (!(request->fl_flags & FL_SLEEP))
918 goto out;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400919 /*
920 * Deadlock detection and insertion into the blocked
921 * locks list must be done while holding the same lock!
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 error = -EDEADLK;
Jeff Layton7b2296a2013-06-21 08:58:20 -0400924 spin_lock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -0400925 if (likely(!posix_locks_deadlock(request, fl))) {
926 error = FILE_LOCK_DEFERRED;
927 __locks_insert_block(fl, request);
928 }
Jeff Layton7b2296a2013-06-21 08:58:20 -0400929 spin_unlock(&blocked_lock_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto out;
931 }
932 }
933
934 /* If we're just looking for a conflict, we're done. */
935 error = 0;
936 if (request->fl_flags & FL_ACCESS)
937 goto out;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * Find the first old lock with the same owner as the new lock.
941 */
942
943 before = &inode->i_flock;
944
945 /* First skip locks owned by other processes. */
946 while ((fl = *before) && (!IS_POSIX(fl) ||
947 !posix_same_owner(request, fl))) {
948 before = &fl->fl_next;
949 }
950
Jeff Layton1cb36012013-06-21 08:58:12 -0400951 /* Process locks with this owner. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 while ((fl = *before) && posix_same_owner(request, fl)) {
953 /* Detect adjacent or overlapping regions (if same lock type)
954 */
955 if (request->fl_type == fl->fl_type) {
Olaf Kirch449231d2005-08-25 16:25:35 -0700956 /* In all comparisons of start vs end, use
957 * "start - 1" rather than "end + 1". If end
958 * is OFFSET_MAX, end + 1 will become negative.
959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (fl->fl_end < request->fl_start - 1)
961 goto next_lock;
962 /* If the next lock in the list has entirely bigger
963 * addresses than the new one, insert the lock here.
964 */
Olaf Kirch449231d2005-08-25 16:25:35 -0700965 if (fl->fl_start - 1 > request->fl_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
967
968 /* If we come here, the new and old lock are of the
969 * same type and adjacent or overlapping. Make one
970 * lock yielding from the lower start address of both
971 * locks to the higher end address.
972 */
973 if (fl->fl_start > request->fl_start)
974 fl->fl_start = request->fl_start;
975 else
976 request->fl_start = fl->fl_start;
977 if (fl->fl_end < request->fl_end)
978 fl->fl_end = request->fl_end;
979 else
980 request->fl_end = fl->fl_end;
981 if (added) {
982 locks_delete_lock(before);
983 continue;
984 }
985 request = fl;
Jeff Laytonb9746ef2013-06-21 08:58:13 -0400986 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 else {
989 /* Processing for different lock types is a bit
990 * more complex.
991 */
992 if (fl->fl_end < request->fl_start)
993 goto next_lock;
994 if (fl->fl_start > request->fl_end)
995 break;
996 if (request->fl_type == F_UNLCK)
Jeff Laytonb9746ef2013-06-21 08:58:13 -0400997 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (fl->fl_start < request->fl_start)
999 left = fl;
1000 /* If the next lock in the list has a higher end
1001 * address than the new one, insert the new one here.
1002 */
1003 if (fl->fl_end > request->fl_end) {
1004 right = fl;
1005 break;
1006 }
1007 if (fl->fl_start >= request->fl_start) {
1008 /* The new lock completely replaces an old
1009 * one (This may happen several times).
1010 */
1011 if (added) {
1012 locks_delete_lock(before);
1013 continue;
1014 }
1015 /* Replace the old lock with the new one.
1016 * Wake up anybody waiting for the old one,
1017 * as the change in lock type might satisfy
1018 * their needs.
1019 */
1020 locks_wake_up_blocks(fl);
1021 fl->fl_start = request->fl_start;
1022 fl->fl_end = request->fl_end;
1023 fl->fl_type = request->fl_type;
Trond Myklebust47831f32006-03-20 13:44:05 -05001024 locks_release_private(fl);
1025 locks_copy_private(fl, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 request = fl;
Jeff Laytonb9746ef2013-06-21 08:58:13 -04001027 added = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 }
1030 /* Go on to next lock.
1031 */
1032 next_lock:
1033 before = &fl->fl_next;
1034 }
1035
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001036 /*
Jeff Layton1cb36012013-06-21 08:58:12 -04001037 * The above code only modifies existing locks in case of merging or
1038 * replacing. If new lock(s) need to be inserted all modifications are
1039 * done below this, so it's safe yet to bail out.
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001040 */
1041 error = -ENOLCK; /* "no luck" */
1042 if (right && left == right && !new_fl2)
1043 goto out;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 error = 0;
1046 if (!added) {
Trond Myklebustf475ae92006-06-29 16:38:32 -04001047 if (request->fl_type == F_UNLCK) {
1048 if (request->fl_flags & FL_EXISTS)
1049 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto out;
Trond Myklebustf475ae92006-06-29 16:38:32 -04001051 }
Miklos Szeredi0d9a4902006-06-23 02:05:09 -07001052
1053 if (!new_fl) {
1054 error = -ENOLCK;
1055 goto out;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 locks_copy_lock(new_fl, request);
1058 locks_insert_lock(before, new_fl);
1059 new_fl = NULL;
1060 }
1061 if (right) {
1062 if (left == right) {
1063 /* The new lock breaks the old one in two pieces,
1064 * so we have to use the second new lock.
1065 */
1066 left = new_fl2;
1067 new_fl2 = NULL;
1068 locks_copy_lock(left, right);
1069 locks_insert_lock(before, left);
1070 }
1071 right->fl_start = request->fl_end + 1;
1072 locks_wake_up_blocks(right);
1073 }
1074 if (left) {
1075 left->fl_end = request->fl_start - 1;
1076 locks_wake_up_blocks(left);
1077 }
1078 out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04001079 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Free any unused locks.
1082 */
1083 if (new_fl)
1084 locks_free_lock(new_fl);
1085 if (new_fl2)
1086 locks_free_lock(new_fl2);
1087 return error;
1088}
1089
1090/**
1091 * posix_lock_file - Apply a POSIX-style lock to a file
1092 * @filp: The file to apply the lock to
1093 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05001094 * @conflock: Place to return a copy of the conflicting lock, if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 *
1096 * Add a POSIX style lock to a file.
1097 * We merge adjacent & overlapping locks whenever possible.
1098 * POSIX locks are sorted by owner task, then by starting address
Trond Myklebustf475ae92006-06-29 16:38:32 -04001099 *
1100 * Note that if called with an FL_EXISTS argument, the caller may determine
1101 * whether or not a lock was successfully freed by testing the return
1102 * value for -ENOENT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 */
Marc Eshel150b3932007-01-18 16:15:35 -05001104int posix_lock_file(struct file *filp, struct file_lock *fl,
Andy Adamson5842add2006-03-26 01:37:26 -08001105 struct file_lock *conflock)
1106{
Al Viro496ad9a2013-01-23 17:07:38 -05001107 return __posix_lock_file(file_inode(filp), fl, conflock);
Andy Adamson5842add2006-03-26 01:37:26 -08001108}
Marc Eshel150b3932007-01-18 16:15:35 -05001109EXPORT_SYMBOL(posix_lock_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111/**
1112 * posix_lock_file_wait - Apply a POSIX-style lock to a file
1113 * @filp: The file to apply the lock to
1114 * @fl: The lock to be applied
1115 *
1116 * Add a POSIX style lock to a file.
1117 * We merge adjacent & overlapping locks whenever possible.
1118 * POSIX locks are sorted by owner task, then by starting address
1119 */
1120int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
1121{
1122 int error;
1123 might_sleep ();
1124 for (;;) {
Marc Eshel150b3932007-01-18 16:15:35 -05001125 error = posix_lock_file(filp, fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001126 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
1128 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1129 if (!error)
1130 continue;
1131
1132 locks_delete_block(fl);
1133 break;
1134 }
1135 return error;
1136}
1137EXPORT_SYMBOL(posix_lock_file_wait);
1138
1139/**
1140 * locks_mandatory_locked - Check for an active lock
1141 * @inode: the file to check
1142 *
1143 * Searches the inode's list of locks to find any POSIX locks which conflict.
1144 * This function is called from locks_verify_locked() only.
1145 */
1146int locks_mandatory_locked(struct inode *inode)
1147{
1148 fl_owner_t owner = current->files;
1149 struct file_lock *fl;
1150
1151 /*
1152 * Search the lock list for this inode for any POSIX locks.
1153 */
Jeff Layton1c8c6012013-06-21 08:58:15 -04001154 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1156 if (!IS_POSIX(fl))
1157 continue;
1158 if (fl->fl_owner != owner)
1159 break;
1160 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04001161 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return fl ? -EAGAIN : 0;
1163}
1164
1165/**
1166 * locks_mandatory_area - Check for a conflicting lock
1167 * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
1168 * for shared
1169 * @inode: the file to check
1170 * @filp: how the file was opened (if it was)
1171 * @offset: start of area to check
1172 * @count: length of area to check
1173 *
1174 * Searches the inode's list of locks to find any POSIX locks which conflict.
1175 * This function is called from rw_verify_area() and
1176 * locks_verify_truncate().
1177 */
1178int locks_mandatory_area(int read_write, struct inode *inode,
1179 struct file *filp, loff_t offset,
1180 size_t count)
1181{
1182 struct file_lock fl;
1183 int error;
1184
1185 locks_init_lock(&fl);
1186 fl.fl_owner = current->files;
1187 fl.fl_pid = current->tgid;
1188 fl.fl_file = filp;
1189 fl.fl_flags = FL_POSIX | FL_ACCESS;
1190 if (filp && !(filp->f_flags & O_NONBLOCK))
1191 fl.fl_flags |= FL_SLEEP;
1192 fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
1193 fl.fl_start = offset;
1194 fl.fl_end = offset + count - 1;
1195
1196 for (;;) {
Marc Eshel150b3932007-01-18 16:15:35 -05001197 error = __posix_lock_file(inode, &fl, NULL);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001198 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 break;
1200 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1201 if (!error) {
1202 /*
1203 * If we've been sleeping someone might have
1204 * changed the permissions behind our back.
1205 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07001206 if (__mandatory_lock(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 continue;
1208 }
1209
1210 locks_delete_block(&fl);
1211 break;
1212 }
1213
1214 return error;
1215}
1216
1217EXPORT_SYMBOL(locks_mandatory_area);
1218
J. Bruce Fields778fc542011-07-26 18:25:49 -04001219static void lease_clear_pending(struct file_lock *fl, int arg)
1220{
1221 switch (arg) {
1222 case F_UNLCK:
1223 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1224 /* fall through: */
1225 case F_RDLCK:
1226 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1227 }
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/* We already had a lease on this file; just change its type */
1231int lease_modify(struct file_lock **before, int arg)
1232{
1233 struct file_lock *fl = *before;
1234 int error = assign_type(fl, arg);
1235
1236 if (error)
1237 return error;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001238 lease_clear_pending(fl, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 locks_wake_up_blocks(fl);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001240 if (arg == F_UNLCK) {
1241 struct file *filp = fl->fl_file;
1242
1243 f_delown(filp);
1244 filp->f_owner.signum = 0;
J. Bruce Fields96d6d592012-07-27 16:18:00 -04001245 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1246 if (fl->fl_fasync != NULL) {
1247 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1248 fl->fl_fasync = NULL;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 locks_delete_lock(before);
Filipe Brandenburger3b6e2722012-07-27 00:42:52 -04001251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
1255EXPORT_SYMBOL(lease_modify);
1256
J. Bruce Fields778fc542011-07-26 18:25:49 -04001257static bool past_time(unsigned long then)
1258{
1259 if (!then)
1260 /* 0 is a special value meaning "this never expires": */
1261 return false;
1262 return time_after(jiffies, then);
1263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265static void time_out_leases(struct inode *inode)
1266{
1267 struct file_lock **before;
1268 struct file_lock *fl;
1269
1270 before = &inode->i_flock;
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04001271 while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001272 if (past_time(fl->fl_downgrade_time))
1273 lease_modify(before, F_RDLCK);
1274 if (past_time(fl->fl_break_time))
1275 lease_modify(before, F_UNLCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (fl == *before) /* lease_modify may have freed fl */
1277 before = &fl->fl_next;
1278 }
1279}
1280
1281/**
1282 * __break_lease - revoke all outstanding leases on file
1283 * @inode: the inode of the file to return
1284 * @mode: the open mode (read or write)
1285 *
david m. richter87250dd2007-05-09 16:10:27 -04001286 * break_lease (inlined for speed) has checked there already is at least
1287 * some kind of lock (maybe a lease) on this file. Leases are broken on
1288 * a call to open() or truncate(). This function can sleep unless you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * specified %O_NONBLOCK to your open().
1290 */
1291int __break_lease(struct inode *inode, unsigned int mode)
1292{
J. Bruce Fields778fc542011-07-26 18:25:49 -04001293 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct file_lock *new_fl, *flock;
1295 struct file_lock *fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned long break_time;
1297 int i_have_this_lease = 0;
Al Viro8737c932009-12-24 06:47:55 -05001298 int want_write = (mode & O_ACCMODE) != O_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Al Viro8737c932009-12-24 06:47:55 -05001300 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001301 if (IS_ERR(new_fl))
1302 return PTR_ERR(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jeff Layton1c8c6012013-06-21 08:58:15 -04001304 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 time_out_leases(inode);
1307
1308 flock = inode->i_flock;
1309 if ((flock == NULL) || !IS_LEASE(flock))
1310 goto out;
1311
J. Bruce Fields778fc542011-07-26 18:25:49 -04001312 if (!locks_conflict(flock, new_fl))
1313 goto out;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
1316 if (fl->fl_owner == current->files)
1317 i_have_this_lease = 1;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break_time = 0;
1320 if (lease_break_time > 0) {
1321 break_time = jiffies + lease_break_time * HZ;
1322 if (break_time == 0)
1323 break_time++; /* so that 0 means no break time */
1324 }
1325
1326 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001327 if (want_write) {
1328 if (fl->fl_flags & FL_UNLOCK_PENDING)
1329 continue;
1330 fl->fl_flags |= FL_UNLOCK_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 fl->fl_break_time = break_time;
J. Bruce Fields778fc542011-07-26 18:25:49 -04001332 } else {
1333 if (lease_breaking(flock))
1334 continue;
1335 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1336 fl->fl_downgrade_time = break_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
J. Bruce Fields778fc542011-07-26 18:25:49 -04001338 fl->fl_lmops->lm_break(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 if (i_have_this_lease || (mode & O_NONBLOCK)) {
1342 error = -EWOULDBLOCK;
1343 goto out;
1344 }
1345
1346restart:
1347 break_time = flock->fl_break_time;
1348 if (break_time != 0) {
1349 break_time -= jiffies;
1350 if (break_time == 0)
1351 break_time++;
1352 }
Matthew Wilcox4321e012008-01-14 21:28:30 -07001353 locks_insert_block(flock, new_fl);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001354 spin_unlock(&inode->i_lock);
Matthew Wilcox4321e012008-01-14 21:28:30 -07001355 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1356 !new_fl->fl_next, break_time);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001357 spin_lock(&inode->i_lock);
1358 locks_delete_block(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (error >= 0) {
1360 if (error == 0)
1361 time_out_leases(inode);
J. Bruce Fields778fc542011-07-26 18:25:49 -04001362 /*
1363 * Wait for the next conflicting lease that has not been
1364 * broken yet
1365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 for (flock = inode->i_flock; flock && IS_LEASE(flock);
1367 flock = flock->fl_next) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001368 if (locks_conflict(new_fl, flock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto restart;
1370 }
1371 error = 0;
1372 }
1373
1374out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04001375 spin_unlock(&inode->i_lock);
Linus Torvalds6d4b9e32011-12-26 10:25:26 -08001376 locks_free_lock(new_fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return error;
1378}
1379
1380EXPORT_SYMBOL(__break_lease);
1381
1382/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001383 * lease_get_mtime - get the last modified time of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * @inode: the inode
1385 * @time: pointer to a timespec which will contain the last modified time
1386 *
1387 * This is to force NFS clients to flush their caches for files with
1388 * exclusive leases. The justification is that if someone has an
Randy Dunlapa6b91912008-03-19 17:01:00 -07001389 * exclusive lease, then they could be modifying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391void lease_get_mtime(struct inode *inode, struct timespec *time)
1392{
1393 struct file_lock *flock = inode->i_flock;
Jeff Layton0ee5c6d2012-08-02 15:46:30 -04001394 if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *time = current_fs_time(inode->i_sb);
1396 else
1397 *time = inode->i_mtime;
1398}
1399
1400EXPORT_SYMBOL(lease_get_mtime);
1401
1402/**
1403 * fcntl_getlease - Enquire what lease is currently active
1404 * @filp: the file
1405 *
1406 * The value returned by this function will be one of
1407 * (if no lease break is pending):
1408 *
1409 * %F_RDLCK to indicate a shared lease is held.
1410 *
1411 * %F_WRLCK to indicate an exclusive lease is held.
1412 *
1413 * %F_UNLCK to indicate no lease is held.
1414 *
1415 * (if a lease break is pending):
1416 *
1417 * %F_RDLCK to indicate an exclusive lease needs to be
1418 * changed to a shared lease (or removed).
1419 *
1420 * %F_UNLCK to indicate the lease needs to be removed.
1421 *
1422 * XXX: sfr & willy disagree over whether F_INPROGRESS
1423 * should be returned to userspace.
1424 */
1425int fcntl_getlease(struct file *filp)
1426{
1427 struct file_lock *fl;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001428 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int type = F_UNLCK;
1430
Jeff Layton1c8c6012013-06-21 08:58:15 -04001431 spin_lock(&inode->i_lock);
Al Viro496ad9a2013-01-23 17:07:38 -05001432 time_out_leases(file_inode(filp));
1433 for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 fl = fl->fl_next) {
1435 if (fl->fl_file == filp) {
J. Bruce Fields778fc542011-07-26 18:25:49 -04001436 type = target_leasetype(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
1438 }
1439 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04001440 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return type;
1442}
1443
Jeff Laytond4f22d12013-06-21 08:58:11 -04001444static int generic_add_lease(struct file *filp, long arg, struct file_lock **flp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
KAMBAROV, ZAUR7eaae282005-07-07 17:57:06 -07001446 struct file_lock *fl, **before, **my_before = NULL, *lease;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001447 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct inode *inode = dentry->d_inode;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
J. Bruce Fields096657b2010-10-30 17:31:14 -04001451 lease = *flp;
1452
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001453 error = -EAGAIN;
1454 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
J. Bruce Fields096657b2010-10-30 17:31:14 -04001455 goto out;
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001456 if ((arg == F_WRLCK)
1457 && ((dentry->d_count > 1)
1458 || (atomic_read(&inode->i_count) > 1)))
J. Bruce Fields096657b2010-10-30 17:31:14 -04001459 goto out;
Pavel Emelyanov85c59582007-09-20 12:45:02 +04001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /*
1462 * At this point, we know that if there is an exclusive
1463 * lease on this file, then we hold it on this filp
1464 * (otherwise our open of this file would have blocked).
1465 * And if we are trying to acquire an exclusive lease,
1466 * then the file is not open by anyone (including us)
1467 * except for this filp.
1468 */
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001469 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 for (before = &inode->i_flock;
1471 ((fl = *before) != NULL) && IS_LEASE(fl);
1472 before = &fl->fl_next) {
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001473 if (fl->fl_file == filp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 my_before = before;
J. Bruce Fieldsc1f24ef2011-08-19 10:59:49 -04001475 continue;
1476 }
1477 /*
1478 * No exclusive leases if someone else has a lease on
1479 * this file:
1480 */
1481 if (arg == F_WRLCK)
1482 goto out;
1483 /*
1484 * Modifying our existing lease is OK, but no getting a
1485 * new lease if someone else is opening for write:
1486 */
1487 if (fl->fl_flags & FL_UNLOCK_PENDING)
1488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (my_before != NULL) {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001492 error = lease->fl_lmops->lm_change(my_before, arg);
Christoph Hellwig51ee4b82010-10-31 08:35:10 -04001493 if (!error)
1494 *flp = *my_before;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 goto out;
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 error = -EINVAL;
1499 if (!leases_enable)
1500 goto out;
1501
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +02001502 locks_insert_lock(before, lease);
Pavel Emelyanov85c59582007-09-20 12:45:02 +04001503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505out:
1506 return error;
1507}
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001508
Jeff Laytond4f22d12013-06-21 08:58:11 -04001509static int generic_delete_lease(struct file *filp, struct file_lock **flp)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001510{
1511 struct file_lock *fl, **before;
1512 struct dentry *dentry = filp->f_path.dentry;
1513 struct inode *inode = dentry->d_inode;
1514
1515 for (before = &inode->i_flock;
1516 ((fl = *before) != NULL) && IS_LEASE(fl);
1517 before = &fl->fl_next) {
1518 if (fl->fl_file != filp)
1519 continue;
1520 return (*flp)->fl_lmops->lm_change(before, F_UNLCK);
1521 }
1522 return -EAGAIN;
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/**
1526 * generic_setlease - sets a lease on an open file
1527 * @filp: file pointer
1528 * @arg: type of lease to obtain
1529 * @flp: input - file_lock to use, output - file_lock inserted
1530 *
1531 * The (input) flp->fl_lmops->lm_break function is required
1532 * by break_lease().
1533 *
Jeff Layton1c8c6012013-06-21 08:58:15 -04001534 * Called with inode->i_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
1536int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
1537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct dentry *dentry = filp->f_path.dentry;
1539 struct inode *inode = dentry->d_inode;
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001540 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08001542 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001543 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (!S_ISREG(inode->i_mode))
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 error = security_file_lock(filp, arg);
1547 if (error)
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001548 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 time_out_leases(inode);
1551
1552 BUG_ON(!(*flp)->fl_lmops->lm_break);
1553
J. Bruce Fields8335ebd2011-09-21 08:34:32 -04001554 switch (arg) {
1555 case F_UNLCK:
1556 return generic_delete_lease(filp, flp);
1557 case F_RDLCK:
1558 case F_WRLCK:
1559 return generic_add_lease(filp, arg, flp);
1560 default:
Dave Jones8d657eb2012-07-13 13:35:36 -04001561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
Christoph Hellwig0af1a452007-07-31 00:39:22 -07001564EXPORT_SYMBOL(generic_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001566static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1567{
1568 if (filp->f_op && filp->f_op->setlease)
1569 return filp->f_op->setlease(filp, arg, lease);
1570 else
1571 return generic_setlease(filp, arg, lease);
1572}
1573
1574/**
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001575 * vfs_setlease - sets a lease on an open file
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 * @filp: file pointer
1577 * @arg: type of lease to obtain
1578 * @lease: file_lock to use
1579 *
1580 * Call this to establish a lease on the file.
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001581 * The (*lease)->fl_lmops->lm_break operation must be set; if not,
J. Bruce Fieldsf9ffed22006-11-14 15:51:40 -05001582 * break_lease will oops!
1583 *
1584 * This will call the filesystem's setlease file method, if
1585 * defined. Note that there is no getlease method; instead, the
1586 * filesystem setlease method should call back to setlease() to
1587 * add a lease to the inode's lease list, where fcntl_getlease() can
1588 * find it. Since fcntl_getlease() only reports whether the current
1589 * task holds a lease, a cluster filesystem need only do this for
1590 * leases held by processes on this node.
1591 *
1592 * There is also no break_lease method; filesystems that
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001593 * handle their own leases should break leases themselves from the
J. Bruce Fieldsf9ffed22006-11-14 15:51:40 -05001594 * filesystem's open, create, and (on truncate) setattr methods.
1595 *
1596 * Warning: the only current setlease methods exist only to disable
1597 * leases in certain cases. More vfs changes may be required to
1598 * allow a full filesystem lease implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
1600
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001601int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Jeff Layton1c8c6012013-06-21 08:58:15 -04001603 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int error;
1605
Jeff Layton1c8c6012013-06-21 08:58:15 -04001606 spin_lock(&inode->i_lock);
Arnd Bergmannb89f4322010-09-18 15:09:31 +02001607 error = __vfs_setlease(filp, arg, lease);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001608 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return error;
1611}
J. Bruce Fieldsa9933ce2007-06-07 17:09:49 -04001612EXPORT_SYMBOL_GPL(vfs_setlease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001614static int do_fcntl_delete_lease(struct file *filp)
1615{
1616 struct file_lock fl, *flp = &fl;
1617
1618 lease_init(filp, F_UNLCK, flp);
1619
1620 return vfs_setlease(filp, F_UNLCK, &flp);
1621}
1622
1623static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
J. Bruce Fields3df057a2010-11-03 16:49:44 -04001625 struct file_lock *fl, *ret;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001626 struct inode *inode = file_inode(filp);
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001627 struct fasync_struct *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int error;
1629
Arnd Bergmannc5b1f0d2010-10-27 15:46:08 +02001630 fl = lease_alloc(filp, arg);
1631 if (IS_ERR(fl))
1632 return PTR_ERR(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001634 new = fasync_alloc();
1635 if (!new) {
1636 locks_free_lock(fl);
1637 return -ENOMEM;
1638 }
J. Bruce Fields3df057a2010-11-03 16:49:44 -04001639 ret = fl;
Jeff Layton1c8c6012013-06-21 08:58:15 -04001640 spin_lock(&inode->i_lock);
J. Bruce Fields8896b932010-11-03 18:09:18 -04001641 error = __vfs_setlease(filp, arg, &ret);
Christoph Hellwig51ee4b82010-10-31 08:35:10 -04001642 if (error) {
Jeff Layton1c8c6012013-06-21 08:58:15 -04001643 spin_unlock(&inode->i_lock);
Christoph Hellwig51ee4b82010-10-31 08:35:10 -04001644 locks_free_lock(fl);
1645 goto out_free_fasync;
1646 }
J. Bruce Fields3df057a2010-11-03 16:49:44 -04001647 if (ret != fl)
1648 locks_free_lock(fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001650 /*
1651 * fasync_insert_entry() returns the old entry if any.
1652 * If there was no old entry, then it used 'new' and
1653 * inserted it into the fasync list. Clear new so that
1654 * we don't release it here.
1655 */
J. Bruce Fields3df057a2010-11-03 16:49:44 -04001656 if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001657 new = NULL;
1658
J. Bruce Fields8896b932010-11-03 18:09:18 -04001659 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
Jeff Layton1c8c6012013-06-21 08:58:15 -04001660 spin_unlock(&inode->i_lock);
Christoph Hellwig51ee4b82010-10-31 08:35:10 -04001661
1662out_free_fasync:
Linus Torvaldsf7347ce2010-10-27 12:38:12 -04001663 if (new)
1664 fasync_free(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return error;
1666}
1667
1668/**
J. Bruce Fields0ceaf6c2010-10-30 17:31:13 -04001669 * fcntl_setlease - sets a lease on an open file
1670 * @fd: open file descriptor
1671 * @filp: file pointer
1672 * @arg: type of lease to obtain
1673 *
1674 * Call this fcntl to establish a lease on the file.
1675 * Note that you also need to call %F_SETSIG to
1676 * receive a signal when the lease is broken.
1677 */
1678int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1679{
1680 if (arg == F_UNLCK)
1681 return do_fcntl_delete_lease(filp);
1682 return do_fcntl_add_lease(fd, filp, arg);
1683}
1684
1685/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * flock_lock_file_wait - Apply a FLOCK-style lock to a file
1687 * @filp: The file to apply the lock to
1688 * @fl: The lock to be applied
1689 *
1690 * Add a FLOCK style lock to a file.
1691 */
1692int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
1693{
1694 int error;
1695 might_sleep();
1696 for (;;) {
1697 error = flock_lock_file(filp, fl);
Miklos Szeredibde74e42008-07-25 01:48:57 -07001698 if (error != FILE_LOCK_DEFERRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 break;
1700 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1701 if (!error)
1702 continue;
1703
1704 locks_delete_block(fl);
1705 break;
1706 }
1707 return error;
1708}
1709
1710EXPORT_SYMBOL(flock_lock_file_wait);
1711
1712/**
1713 * sys_flock: - flock() system call.
1714 * @fd: the file descriptor to lock.
1715 * @cmd: the type of lock to apply.
1716 *
1717 * Apply a %FL_FLOCK style lock to an open file descriptor.
1718 * The @cmd can be one of
1719 *
1720 * %LOCK_SH -- a shared lock.
1721 *
1722 * %LOCK_EX -- an exclusive lock.
1723 *
1724 * %LOCK_UN -- remove an existing lock.
1725 *
1726 * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
1727 *
1728 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1729 * processes read and write access respectively.
1730 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001731SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Al Viro2903ff02012-08-28 12:52:22 -04001733 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct file_lock *lock;
1735 int can_sleep, unlock;
1736 int error;
1737
1738 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -04001739 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto out;
1741
1742 can_sleep = !(cmd & LOCK_NB);
1743 cmd &= ~LOCK_NB;
1744 unlock = (cmd == LOCK_UN);
1745
Al Viroaeb5d722008-09-02 15:28:45 -04001746 if (!unlock && !(cmd & LOCK_MAND) &&
Al Viro2903ff02012-08-28 12:52:22 -04001747 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto out_putf;
1749
Al Viro2903ff02012-08-28 12:52:22 -04001750 error = flock_make_lock(f.file, &lock, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (error)
1752 goto out_putf;
1753 if (can_sleep)
1754 lock->fl_flags |= FL_SLEEP;
1755
Al Viro2903ff02012-08-28 12:52:22 -04001756 error = security_file_lock(f.file, lock->fl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (error)
1758 goto out_free;
1759
Al Viro2903ff02012-08-28 12:52:22 -04001760 if (f.file->f_op && f.file->f_op->flock)
1761 error = f.file->f_op->flock(f.file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 (can_sleep) ? F_SETLKW : F_SETLK,
1763 lock);
1764 else
Al Viro2903ff02012-08-28 12:52:22 -04001765 error = flock_lock_file_wait(f.file, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 out_free:
Trond Myklebust993dfa82006-03-31 02:30:55 -08001768 locks_free_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 out_putf:
Al Viro2903ff02012-08-28 12:52:22 -04001771 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 out:
1773 return error;
1774}
1775
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05001776/**
1777 * vfs_test_lock - test file byte range lock
1778 * @filp: The file to test lock for
J. Bruce Fields6924c552007-05-11 16:22:50 -04001779 * @fl: The lock to test; also used to hold result
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05001780 *
1781 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
1782 * setting conf->fl_type to something other than F_UNLCK.
1783 */
1784int vfs_test_lock(struct file *filp, struct file_lock *fl)
1785{
1786 if (filp->f_op && filp->f_op->lock)
1787 return filp->f_op->lock(filp, F_GETLK, fl);
1788 posix_test_lock(filp, fl);
1789 return 0;
1790}
1791EXPORT_SYMBOL_GPL(vfs_test_lock);
1792
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05001793static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1794{
1795 flock->l_pid = fl->fl_pid;
1796#if BITS_PER_LONG == 32
1797 /*
1798 * Make sure we can represent the posix lock via
1799 * legacy 32bit flock.
1800 */
1801 if (fl->fl_start > OFFT_OFFSET_MAX)
1802 return -EOVERFLOW;
1803 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1804 return -EOVERFLOW;
1805#endif
1806 flock->l_start = fl->fl_start;
1807 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1808 fl->fl_end - fl->fl_start + 1;
1809 flock->l_whence = 0;
J. Bruce Fields129a84d2007-05-10 18:38:43 -04001810 flock->l_type = fl->fl_type;
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05001811 return 0;
1812}
1813
1814#if BITS_PER_LONG == 32
1815static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1816{
1817 flock->l_pid = fl->fl_pid;
1818 flock->l_start = fl->fl_start;
1819 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1820 fl->fl_end - fl->fl_start + 1;
1821 flock->l_whence = 0;
1822 flock->l_type = fl->fl_type;
1823}
1824#endif
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826/* Report the first existing lock that would conflict with l.
1827 * This implements the F_GETLK command of fcntl().
1828 */
1829int fcntl_getlk(struct file *filp, struct flock __user *l)
1830{
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001831 struct file_lock file_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct flock flock;
1833 int error;
1834
1835 error = -EFAULT;
1836 if (copy_from_user(&flock, l, sizeof(flock)))
1837 goto out;
1838 error = -EINVAL;
1839 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1840 goto out;
1841
1842 error = flock_to_posix_lock(filp, &file_lock, &flock);
1843 if (error)
1844 goto out;
1845
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05001846 error = vfs_test_lock(filp, &file_lock);
1847 if (error)
1848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001850 flock.l_type = file_lock.fl_type;
1851 if (file_lock.fl_type != F_UNLCK) {
1852 error = posix_lock_to_flock(&flock, &file_lock);
J. Bruce Fieldsc2fa1b82007-02-20 16:10:11 -05001853 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856 error = -EFAULT;
1857 if (!copy_to_user(l, &flock, sizeof(flock)))
1858 error = 0;
1859out:
1860 return error;
1861}
1862
Marc Eshel7723ec92007-01-18 15:08:55 -05001863/**
1864 * vfs_lock_file - file byte range lock
1865 * @filp: The file to apply the lock to
1866 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
1867 * @fl: The lock to be applied
Marc Eshel150b3932007-01-18 16:15:35 -05001868 * @conf: Place to return a copy of the conflicting lock, if found.
1869 *
1870 * A caller that doesn't care about the conflicting lock may pass NULL
1871 * as the final argument.
1872 *
1873 * If the filesystem defines a private ->lock() method, then @conf will
1874 * be left unchanged; so a caller that cares should initialize it to
1875 * some acceptable default.
Marc Eshel2beb6612006-12-05 23:31:28 -05001876 *
1877 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
1878 * locks, the ->lock() interface may return asynchronously, before the lock has
1879 * been granted or denied by the underlying filesystem, if (and only if)
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001880 * lm_grant is set. Callers expecting ->lock() to return asynchronously
Marc Eshel2beb6612006-12-05 23:31:28 -05001881 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
1882 * the request is for a blocking lock. When ->lock() does return asynchronously,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001883 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
Marc Eshel2beb6612006-12-05 23:31:28 -05001884 * request completes.
1885 * If the request is for non-blocking lock the file system should return
Miklos Szeredibde74e42008-07-25 01:48:57 -07001886 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
1887 * with the result. If the request timed out the callback routine will return a
Marc Eshel2beb6612006-12-05 23:31:28 -05001888 * nonzero return code and the file system should release the lock. The file
1889 * system is also responsible to keep a corresponding posix lock when it
1890 * grants a lock so the VFS can find out which locks are locally held and do
1891 * the correct lock cleanup when required.
1892 * The underlying filesystem must not drop the kernel lock or call
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001893 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
Marc Eshel2beb6612006-12-05 23:31:28 -05001894 * return code.
Marc Eshel7723ec92007-01-18 15:08:55 -05001895 */
Marc Eshel150b3932007-01-18 16:15:35 -05001896int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
Marc Eshel7723ec92007-01-18 15:08:55 -05001897{
1898 if (filp->f_op && filp->f_op->lock)
1899 return filp->f_op->lock(filp, cmd, fl);
1900 else
Marc Eshel150b3932007-01-18 16:15:35 -05001901 return posix_lock_file(filp, fl, conf);
Marc Eshel7723ec92007-01-18 15:08:55 -05001902}
1903EXPORT_SYMBOL_GPL(vfs_lock_file);
1904
Miklos Szeredib648a6d2008-07-25 01:48:57 -07001905static int do_lock_file_wait(struct file *filp, unsigned int cmd,
1906 struct file_lock *fl)
1907{
1908 int error;
1909
1910 error = security_file_lock(filp, fl->fl_type);
1911 if (error)
1912 return error;
1913
Miklos Szeredi764c76b2008-07-25 01:48:58 -07001914 for (;;) {
1915 error = vfs_lock_file(filp, cmd, fl, NULL);
1916 if (error != FILE_LOCK_DEFERRED)
Miklos Szeredib648a6d2008-07-25 01:48:57 -07001917 break;
Miklos Szeredi764c76b2008-07-25 01:48:58 -07001918 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1919 if (!error)
1920 continue;
1921
1922 locks_delete_block(fl);
1923 break;
Miklos Szeredib648a6d2008-07-25 01:48:57 -07001924 }
1925
1926 return error;
1927}
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929/* Apply the lock described by l to an open file descriptor.
1930 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
1931 */
Peter Staubachc2936212005-07-27 11:45:09 -07001932int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
1933 struct flock __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct file_lock *file_lock = locks_alloc_lock();
1936 struct flock flock;
1937 struct inode *inode;
Al Viro0b2bac22008-05-06 13:58:34 -04001938 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 int error;
1940
1941 if (file_lock == NULL)
1942 return -ENOLCK;
1943
1944 /*
1945 * This might block, so we do it before checking the inode.
1946 */
1947 error = -EFAULT;
1948 if (copy_from_user(&flock, l, sizeof(flock)))
1949 goto out;
1950
Al Viro496ad9a2013-01-23 17:07:38 -05001951 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* Don't allow mandatory locks on files that may be memory mapped
1954 * and shared.
1955 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07001956 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 error = -EAGAIN;
1958 goto out;
1959 }
1960
Peter Staubachc2936212005-07-27 11:45:09 -07001961again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 error = flock_to_posix_lock(filp, file_lock, &flock);
1963 if (error)
1964 goto out;
1965 if (cmd == F_SETLKW) {
1966 file_lock->fl_flags |= FL_SLEEP;
1967 }
1968
1969 error = -EBADF;
1970 switch (flock.l_type) {
1971 case F_RDLCK:
1972 if (!(filp->f_mode & FMODE_READ))
1973 goto out;
1974 break;
1975 case F_WRLCK:
1976 if (!(filp->f_mode & FMODE_WRITE))
1977 goto out;
1978 break;
1979 case F_UNLCK:
1980 break;
1981 default:
1982 error = -EINVAL;
1983 goto out;
1984 }
1985
Miklos Szeredib648a6d2008-07-25 01:48:57 -07001986 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Peter Staubachc2936212005-07-27 11:45:09 -07001988 /*
1989 * Attempt to detect a close/fcntl race and recover by
1990 * releasing the lock that was just acquired.
1991 */
Al Viro0b2bac22008-05-06 13:58:34 -04001992 /*
1993 * we need that spin_lock here - it prevents reordering between
1994 * update of inode->i_flock and check for it done in close().
1995 * rcu_read_lock() wouldn't do.
1996 */
1997 spin_lock(&current->files->file_lock);
1998 f = fcheck(fd);
1999 spin_unlock(&current->files->file_lock);
2000 if (!error && f != filp && flock.l_type != F_UNLCK) {
Peter Staubachc2936212005-07-27 11:45:09 -07002001 flock.l_type = F_UNLCK;
2002 goto again;
2003 }
2004
2005out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 locks_free_lock(file_lock);
2007 return error;
2008}
2009
2010#if BITS_PER_LONG == 32
2011/* Report the first existing lock that would conflict with l.
2012 * This implements the F_GETLK command of fcntl().
2013 */
2014int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
2015{
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002016 struct file_lock file_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct flock64 flock;
2018 int error;
2019
2020 error = -EFAULT;
2021 if (copy_from_user(&flock, l, sizeof(flock)))
2022 goto out;
2023 error = -EINVAL;
2024 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
2025 goto out;
2026
2027 error = flock64_to_posix_lock(filp, &file_lock, &flock);
2028 if (error)
2029 goto out;
2030
J. Bruce Fields3ee17ab2007-02-21 00:58:50 -05002031 error = vfs_test_lock(filp, &file_lock);
2032 if (error)
2033 goto out;
2034
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002035 flock.l_type = file_lock.fl_type;
2036 if (file_lock.fl_type != F_UNLCK)
2037 posix_lock_to_flock64(&flock, &file_lock);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 error = -EFAULT;
2040 if (!copy_to_user(l, &flock, sizeof(flock)))
2041 error = 0;
2042
2043out:
2044 return error;
2045}
2046
2047/* Apply the lock described by l to an open file descriptor.
2048 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2049 */
Peter Staubachc2936212005-07-27 11:45:09 -07002050int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2051 struct flock64 __user *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct file_lock *file_lock = locks_alloc_lock();
2054 struct flock64 flock;
2055 struct inode *inode;
Al Viro0b2bac22008-05-06 13:58:34 -04002056 struct file *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int error;
2058
2059 if (file_lock == NULL)
2060 return -ENOLCK;
2061
2062 /*
2063 * This might block, so we do it before checking the inode.
2064 */
2065 error = -EFAULT;
2066 if (copy_from_user(&flock, l, sizeof(flock)))
2067 goto out;
2068
Al Viro496ad9a2013-01-23 17:07:38 -05002069 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* Don't allow mandatory locks on files that may be memory mapped
2072 * and shared.
2073 */
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002074 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 error = -EAGAIN;
2076 goto out;
2077 }
2078
Peter Staubachc2936212005-07-27 11:45:09 -07002079again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 error = flock64_to_posix_lock(filp, file_lock, &flock);
2081 if (error)
2082 goto out;
2083 if (cmd == F_SETLKW64) {
2084 file_lock->fl_flags |= FL_SLEEP;
2085 }
2086
2087 error = -EBADF;
2088 switch (flock.l_type) {
2089 case F_RDLCK:
2090 if (!(filp->f_mode & FMODE_READ))
2091 goto out;
2092 break;
2093 case F_WRLCK:
2094 if (!(filp->f_mode & FMODE_WRITE))
2095 goto out;
2096 break;
2097 case F_UNLCK:
2098 break;
2099 default:
2100 error = -EINVAL;
2101 goto out;
2102 }
2103
Miklos Szeredib648a6d2008-07-25 01:48:57 -07002104 error = do_lock_file_wait(filp, cmd, file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Peter Staubachc2936212005-07-27 11:45:09 -07002106 /*
2107 * Attempt to detect a close/fcntl race and recover by
2108 * releasing the lock that was just acquired.
2109 */
Al Viro0b2bac22008-05-06 13:58:34 -04002110 spin_lock(&current->files->file_lock);
2111 f = fcheck(fd);
2112 spin_unlock(&current->files->file_lock);
2113 if (!error && f != filp && flock.l_type != F_UNLCK) {
Peter Staubachc2936212005-07-27 11:45:09 -07002114 flock.l_type = F_UNLCK;
2115 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
2118out:
2119 locks_free_lock(file_lock);
2120 return error;
2121}
2122#endif /* BITS_PER_LONG == 32 */
2123
2124/*
2125 * This function is called when the file is being removed
2126 * from the task's fd array. POSIX locks belonging to this task
2127 * are deleted at this time.
2128 */
2129void locks_remove_posix(struct file *filp, fl_owner_t owner)
2130{
Miklos Szerediff7b86b2006-06-23 02:05:11 -07002131 struct file_lock lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /*
2134 * If there are no locks held on this file, we don't need to call
2135 * posix_lock_file(). Another process could be setting a lock on this
2136 * file at the same time, but we wouldn't remove that lock anyway.
2137 */
Al Viro496ad9a2013-01-23 17:07:38 -05002138 if (!file_inode(filp)->i_flock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return;
2140
2141 lock.fl_type = F_UNLCK;
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07002142 lock.fl_flags = FL_POSIX | FL_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 lock.fl_start = 0;
2144 lock.fl_end = OFFSET_MAX;
2145 lock.fl_owner = owner;
2146 lock.fl_pid = current->tgid;
2147 lock.fl_file = filp;
2148 lock.fl_ops = NULL;
2149 lock.fl_lmops = NULL;
2150
Marc Eshel150b3932007-01-18 16:15:35 -05002151 vfs_lock_file(filp, F_SETLK, &lock, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2154 lock.fl_ops->fl_release_private(&lock);
2155}
2156
2157EXPORT_SYMBOL(locks_remove_posix);
2158
2159/*
2160 * This function is called on the last close of an open file.
2161 */
2162void locks_remove_flock(struct file *filp)
2163{
Al Viro496ad9a2013-01-23 17:07:38 -05002164 struct inode * inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct file_lock *fl;
2166 struct file_lock **before;
2167
2168 if (!inode->i_flock)
2169 return;
2170
2171 if (filp->f_op && filp->f_op->flock) {
2172 struct file_lock fl = {
2173 .fl_pid = current->tgid,
2174 .fl_file = filp,
2175 .fl_flags = FL_FLOCK,
2176 .fl_type = F_UNLCK,
2177 .fl_end = OFFSET_MAX,
2178 };
2179 filp->f_op->flock(filp, F_SETLKW, &fl);
Trond Myklebust80fec4c2005-06-22 17:16:31 +00002180 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2181 fl.fl_ops->fl_release_private(&fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
Jeff Layton1c8c6012013-06-21 08:58:15 -04002184 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 before = &inode->i_flock;
2186
2187 while ((fl = *before) != NULL) {
2188 if (fl->fl_file == filp) {
Peter Staubachc2936212005-07-27 11:45:09 -07002189 if (IS_FLOCK(fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 locks_delete_lock(before);
2191 continue;
2192 }
2193 if (IS_LEASE(fl)) {
2194 lease_modify(before, F_UNLCK);
2195 continue;
2196 }
2197 /* What? */
2198 BUG();
2199 }
2200 before = &fl->fl_next;
2201 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04002202 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 * posix_unblock_lock - stop waiting for a file lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * @waiter: the lock which was waiting
2208 *
2209 * lockd needs to block waiting for locks.
2210 */
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002211int
Jeff Laytonf891a292013-06-21 08:58:09 -04002212posix_unblock_lock(struct file_lock *waiter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002214 int status = 0;
2215
Jeff Layton7b2296a2013-06-21 08:58:20 -04002216 spin_lock(&blocked_lock_lock);
J. Bruce Fields5996a292006-01-03 09:55:44 +01002217 if (waiter->fl_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 __locks_delete_block(waiter);
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002219 else
2220 status = -ENOENT;
Jeff Layton7b2296a2013-06-21 08:58:20 -04002221 spin_unlock(&blocked_lock_lock);
J. Bruce Fields64a318e2006-01-03 09:55:46 +01002222 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224EXPORT_SYMBOL(posix_unblock_lock);
2225
Marc Eshel9b9d2ab2007-01-18 17:52:58 -05002226/**
2227 * vfs_cancel_lock - file byte range unblock lock
2228 * @filp: The file to apply the unblock to
2229 * @fl: The lock to be unblocked
2230 *
2231 * Used by lock managers to cancel blocked requests
2232 */
2233int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2234{
2235 if (filp->f_op && filp->f_op->lock)
2236 return filp->f_op->lock(filp, F_CANCELLK, fl);
2237 return 0;
2238}
2239
2240EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2241
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002242#ifdef CONFIG_PROC_FS
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002243#include <linux/proc_fs.h>
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002244#include <linux/seq_file.h>
2245
2246static void lock_get_status(struct seq_file *f, struct file_lock *fl,
Jerome Marchand99dc8292010-10-26 14:22:33 -07002247 loff_t id, char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 struct inode *inode = NULL;
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002250 unsigned int fl_pid;
2251
2252 if (fl->fl_nspid)
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -08002253 fl_pid = pid_vnr(fl->fl_nspid);
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002254 else
2255 fl_pid = fl->fl_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 if (fl->fl_file != NULL)
Al Viro496ad9a2013-01-23 17:07:38 -05002258 inode = file_inode(fl->fl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jerome Marchand99dc8292010-10-26 14:22:33 -07002260 seq_printf(f, "%lld:%s ", id, pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (IS_POSIX(fl)) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002262 seq_printf(f, "%6s %s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
2264 (inode == NULL) ? "*NOINODE*" :
Pavel Emelyanova16877c2007-10-01 14:41:11 -07002265 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 } else if (IS_FLOCK(fl)) {
2267 if (fl->fl_type & LOCK_MAND) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002268 seq_printf(f, "FLOCK MSNFS ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002270 seq_printf(f, "FLOCK ADVISORY ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 } else if (IS_LEASE(fl)) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002273 seq_printf(f, "LEASE ");
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002274 if (lease_breaking(fl))
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002275 seq_printf(f, "BREAKING ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 else if (fl->fl_file)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002277 seq_printf(f, "ACTIVE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 else
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002279 seq_printf(f, "BREAKER ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002281 seq_printf(f, "UNKNOWN UNKNOWN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 if (fl->fl_type & LOCK_MAND) {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002284 seq_printf(f, "%s ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 (fl->fl_type & LOCK_READ)
2286 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2287 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2288 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002289 seq_printf(f, "%s ",
J. Bruce Fieldsab83fa42011-07-26 20:10:51 -04002290 (lease_breaking(fl))
Jeff Layton0ee5c6d2012-08-02 15:46:30 -04002291 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2292 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294 if (inode) {
2295#ifdef WE_CAN_BREAK_LSLK_NOW
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002296 seq_printf(f, "%d %s:%ld ", fl_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 inode->i_sb->s_id, inode->i_ino);
2298#else
2299 /* userspace relies on this representation of dev_t ;-( */
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002300 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 MAJOR(inode->i_sb->s_dev),
2302 MINOR(inode->i_sb->s_dev), inode->i_ino);
2303#endif
2304 } else {
Vitaliy Gusevab1f1612008-01-17 00:07:08 +00002305 seq_printf(f, "%d <none>:0 ", fl_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 if (IS_POSIX(fl)) {
2308 if (fl->fl_end == OFFSET_MAX)
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002309 seq_printf(f, "%Ld EOF\n", fl->fl_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 else
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002311 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 } else {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002313 seq_printf(f, "0 EOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315}
2316
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002317static int locks_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002319 struct file_lock *fl, *bfl;
2320
Jeff Layton139ca042013-06-21 08:58:17 -04002321 fl = hlist_entry(v, struct file_lock, fl_link);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002322
Jerome Marchand99dc8292010-10-26 14:22:33 -07002323 lock_get_status(f, fl, *((loff_t *)f->private), "");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002324
2325 list_for_each_entry(bfl, &fl->fl_block, fl_block)
Jerome Marchand99dc8292010-10-26 14:22:33 -07002326 lock_get_status(f, bfl, *((loff_t *)f->private), " ->");
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002327
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002331static void *locks_start(struct seq_file *f, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Jerome Marchand99dc8292010-10-26 14:22:33 -07002333 loff_t *p = f->private;
2334
Jeff Layton1c8c6012013-06-21 08:58:15 -04002335 spin_lock(&file_lock_lock);
Jeff Layton7b2296a2013-06-21 08:58:20 -04002336 spin_lock(&blocked_lock_lock);
Jerome Marchand99dc8292010-10-26 14:22:33 -07002337 *p = (*pos + 1);
Jeff Layton139ca042013-06-21 08:58:17 -04002338 return seq_hlist_start(&file_lock_list, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002341static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2342{
Jerome Marchand99dc8292010-10-26 14:22:33 -07002343 loff_t *p = f->private;
2344 ++*p;
Jeff Layton139ca042013-06-21 08:58:17 -04002345 return seq_hlist_next(v, &file_lock_list, pos);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002346}
2347
2348static void locks_stop(struct seq_file *f, void *v)
2349{
Jeff Layton7b2296a2013-06-21 08:58:20 -04002350 spin_unlock(&blocked_lock_lock);
Jeff Layton1c8c6012013-06-21 08:58:15 -04002351 spin_unlock(&file_lock_lock);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002352}
2353
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002354static const struct seq_operations locks_seq_operations = {
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002355 .start = locks_start,
2356 .next = locks_next,
2357 .stop = locks_stop,
2358 .show = locks_show,
2359};
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002360
2361static int locks_open(struct inode *inode, struct file *filp)
2362{
Jerome Marchand99dc8292010-10-26 14:22:33 -07002363 return seq_open_private(filp, &locks_seq_operations, sizeof(loff_t));
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002364}
2365
2366static const struct file_operations proc_locks_operations = {
2367 .open = locks_open,
2368 .read = seq_read,
2369 .llseek = seq_lseek,
Jerome Marchand99dc8292010-10-26 14:22:33 -07002370 .release = seq_release_private,
Alexey Dobriyand8ba7a32008-10-04 22:34:18 +04002371};
2372
2373static int __init proc_locks_init(void)
2374{
2375 proc_create("locks", 0, NULL, &proc_locks_operations);
2376 return 0;
2377}
2378module_init(proc_locks_init);
Pavel Emelyanov7f8ada92007-10-01 14:41:15 -07002379#endif
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381/**
2382 * lock_may_read - checks that the region is free of locks
2383 * @inode: the inode that is being read
2384 * @start: the first byte to read
2385 * @len: the number of bytes to read
2386 *
2387 * Emulates Windows locking requirements. Whole-file
2388 * mandatory locks (share modes) can prohibit a read and
2389 * byte-range POSIX locks can prohibit a read if they overlap.
2390 *
2391 * N.B. this function is only ever called
2392 * from knfsd and ownership of locks is never checked.
2393 */
2394int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
2395{
2396 struct file_lock *fl;
2397 int result = 1;
Jeff Layton1c8c6012013-06-21 08:58:15 -04002398
2399 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2401 if (IS_POSIX(fl)) {
2402 if (fl->fl_type == F_RDLCK)
2403 continue;
2404 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2405 continue;
2406 } else if (IS_FLOCK(fl)) {
2407 if (!(fl->fl_type & LOCK_MAND))
2408 continue;
2409 if (fl->fl_type & LOCK_READ)
2410 continue;
2411 } else
2412 continue;
2413 result = 0;
2414 break;
2415 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04002416 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return result;
2418}
2419
2420EXPORT_SYMBOL(lock_may_read);
2421
2422/**
2423 * lock_may_write - checks that the region is free of locks
2424 * @inode: the inode that is being written
2425 * @start: the first byte to write
2426 * @len: the number of bytes to write
2427 *
2428 * Emulates Windows locking requirements. Whole-file
2429 * mandatory locks (share modes) can prohibit a write and
2430 * byte-range POSIX locks can prohibit a write if they overlap.
2431 *
2432 * N.B. this function is only ever called
2433 * from knfsd and ownership of locks is never checked.
2434 */
2435int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
2436{
2437 struct file_lock *fl;
2438 int result = 1;
Jeff Layton1c8c6012013-06-21 08:58:15 -04002439
2440 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2442 if (IS_POSIX(fl)) {
2443 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2444 continue;
2445 } else if (IS_FLOCK(fl)) {
2446 if (!(fl->fl_type & LOCK_MAND))
2447 continue;
2448 if (fl->fl_type & LOCK_WRITE)
2449 continue;
2450 } else
2451 continue;
2452 result = 0;
2453 break;
2454 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04002455 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return result;
2457}
2458
2459EXPORT_SYMBOL(lock_may_write);
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static int __init filelock_init(void)
2462{
2463 filelock_cache = kmem_cache_create("file_lock_cache",
Miklos Szerediee19cc42011-07-07 13:06:09 +02002464 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
2469core_initcall(filelock_init);