locks: have locks_release_file use flock_lock_file to release generic flock locks
[linux-2.6-block.git] / fs / locks.c
CommitLineData
1da177e4
LT
1/*
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.
395cf969 63 * See 'Documentation/filesystems/mandatory-locking.txt' for details.
1da177e4
LT
64 * 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>
9f3acc31 119#include <linux/fdtable.h>
1da177e4
LT
120#include <linux/fs.h>
121#include <linux/init.h>
122#include <linux/module.h>
123#include <linux/security.h>
124#include <linux/slab.h>
1da177e4
LT
125#include <linux/syscalls.h>
126#include <linux/time.h>
4fb3a538 127#include <linux/rcupdate.h>
ab1f1611 128#include <linux/pid_namespace.h>
48f74186 129#include <linux/hashtable.h>
7012b02a
JL
130#include <linux/percpu.h>
131#include <linux/lglock.h>
1da177e4 132
62af4f1f
JL
133#define CREATE_TRACE_POINTS
134#include <trace/events/filelock.h>
135
1da177e4
LT
136#include <asm/uaccess.h>
137
138#define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
139#define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
617588d5 140#define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG))
cff2fce5 141#define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
1da177e4 142
ab83fa4b
BF
143static bool lease_breaking(struct file_lock *fl)
144{
778fc546
BF
145 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
146}
147
148static int target_leasetype(struct file_lock *fl)
149{
150 if (fl->fl_flags & FL_UNLOCK_PENDING)
151 return F_UNLCK;
152 if (fl->fl_flags & FL_DOWNGRADE_PENDING)
153 return F_RDLCK;
154 return fl->fl_type;
ab83fa4b
BF
155}
156
1da177e4
LT
157int leases_enable = 1;
158int lease_break_time = 45;
159
160#define for_each_lock(inode, lockp) \
161 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
162
1c8c601a 163/*
7012b02a
JL
164 * The global file_lock_list is only used for displaying /proc/locks, so we
165 * keep a list on each CPU, with each list protected by its own spinlock via
166 * the file_lock_lglock. Note that alterations to the list also require that
167 * the relevant i_lock is held.
1c8c601a 168 */
7012b02a
JL
169DEFINE_STATIC_LGLOCK(file_lock_lglock);
170static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
88974691 171
1c8c601a 172/*
48f74186 173 * The blocked_hash is used to find POSIX lock loops for deadlock detection.
7b2296af 174 * It is protected by blocked_lock_lock.
48f74186
JL
175 *
176 * We hash locks by lockowner in order to optimize searching for the lock a
177 * particular lockowner is waiting on.
178 *
179 * FIXME: make this value scale via some heuristic? We generally will want more
180 * buckets when we have more lockowners holding locks, but that's a little
181 * difficult to determine without knowing what the workload will look like.
1c8c601a 182 */
48f74186
JL
183#define BLOCKED_HASH_BITS 7
184static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
88974691 185
1c8c601a 186/*
7b2296af
JL
187 * This lock protects the blocked_hash. Generally, if you're accessing it, you
188 * want to be holding this lock.
1c8c601a
JL
189 *
190 * In addition, it also protects the fl->fl_block list, and the fl->fl_next
191 * pointer for file_lock structures that are acting as lock requests (in
192 * contrast to those that are acting as records of acquired locks).
193 *
194 * Note that when we acquire this lock in order to change the above fields,
195 * we often hold the i_lock as well. In certain cases, when reading the fields
196 * protected by this lock, we can skip acquiring it iff we already hold the
197 * i_lock.
198 *
199 * In particular, adding an entry to the fl_block list requires that you hold
200 * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
201 * an entry from the list however only requires the file_lock_lock.
202 */
7b2296af 203static DEFINE_SPINLOCK(blocked_lock_lock);
1da177e4 204
e18b890b 205static struct kmem_cache *filelock_cache __read_mostly;
1da177e4 206
ee19cc40 207static void locks_init_lock_heads(struct file_lock *fl)
a51cb91d 208{
139ca04e 209 INIT_HLIST_NODE(&fl->fl_link);
6dee60f6 210 INIT_LIST_HEAD(&fl->fl_list);
ee19cc40
MS
211 INIT_LIST_HEAD(&fl->fl_block);
212 init_waitqueue_head(&fl->fl_wait);
a51cb91d
MS
213}
214
1da177e4 215/* Allocate an empty lock structure. */
c5b1f0d9 216struct file_lock *locks_alloc_lock(void)
1da177e4 217{
ee19cc40 218 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
a51cb91d
MS
219
220 if (fl)
ee19cc40 221 locks_init_lock_heads(fl);
a51cb91d
MS
222
223 return fl;
1da177e4 224}
c5b1f0d9 225EXPORT_SYMBOL_GPL(locks_alloc_lock);
1da177e4 226
a9e61e25 227void locks_release_private(struct file_lock *fl)
47831f35
TM
228{
229 if (fl->fl_ops) {
230 if (fl->fl_ops->fl_release_private)
231 fl->fl_ops->fl_release_private(fl);
232 fl->fl_ops = NULL;
233 }
47831f35 234
5c97d7b1
KM
235 if (fl->fl_lmops) {
236 if (fl->fl_lmops->lm_put_owner)
237 fl->fl_lmops->lm_put_owner(fl);
238 fl->fl_lmops = NULL;
239 }
47831f35 240}
a9e61e25 241EXPORT_SYMBOL_GPL(locks_release_private);
47831f35 242
1da177e4 243/* Free a lock which is not in use. */
05fa3135 244void locks_free_lock(struct file_lock *fl)
1da177e4 245{
5ce29646 246 BUG_ON(waitqueue_active(&fl->fl_wait));
6dee60f6 247 BUG_ON(!list_empty(&fl->fl_list));
5ce29646 248 BUG_ON(!list_empty(&fl->fl_block));
139ca04e 249 BUG_ON(!hlist_unhashed(&fl->fl_link));
1da177e4 250
47831f35 251 locks_release_private(fl);
1da177e4
LT
252 kmem_cache_free(filelock_cache, fl);
253}
05fa3135 254EXPORT_SYMBOL(locks_free_lock);
1da177e4 255
ed9814d8
JL
256static void
257locks_dispose_list(struct list_head *dispose)
258{
259 struct file_lock *fl;
260
261 while (!list_empty(dispose)) {
6dee60f6
JL
262 fl = list_first_entry(dispose, struct file_lock, fl_list);
263 list_del_init(&fl->fl_list);
ed9814d8
JL
264 locks_free_lock(fl);
265 }
266}
267
1da177e4
LT
268void locks_init_lock(struct file_lock *fl)
269{
ee19cc40
MS
270 memset(fl, 0, sizeof(struct file_lock));
271 locks_init_lock_heads(fl);
1da177e4
LT
272}
273
274EXPORT_SYMBOL(locks_init_lock);
275
1da177e4
LT
276/*
277 * Initialize a new lock from an existing file_lock structure.
278 */
3fe0fff1 279void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
1da177e4
LT
280{
281 new->fl_owner = fl->fl_owner;
282 new->fl_pid = fl->fl_pid;
0996905f 283 new->fl_file = NULL;
1da177e4
LT
284 new->fl_flags = fl->fl_flags;
285 new->fl_type = fl->fl_type;
286 new->fl_start = fl->fl_start;
287 new->fl_end = fl->fl_end;
f328296e 288 new->fl_lmops = fl->fl_lmops;
0996905f 289 new->fl_ops = NULL;
f328296e
KM
290
291 if (fl->fl_lmops) {
292 if (fl->fl_lmops->lm_get_owner)
293 fl->fl_lmops->lm_get_owner(new, fl);
294 }
0996905f 295}
3fe0fff1 296EXPORT_SYMBOL(locks_copy_conflock);
0996905f
TM
297
298void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
299{
566709bd
JL
300 /* "new" must be a freshly-initialized lock */
301 WARN_ON_ONCE(new->fl_ops);
0996905f 302
3fe0fff1 303 locks_copy_conflock(new, fl);
f328296e 304
0996905f 305 new->fl_file = fl->fl_file;
1da177e4 306 new->fl_ops = fl->fl_ops;
47831f35 307
f328296e
KM
308 if (fl->fl_ops) {
309 if (fl->fl_ops->fl_copy_lock)
310 fl->fl_ops->fl_copy_lock(new, fl);
311 }
1da177e4
LT
312}
313
314EXPORT_SYMBOL(locks_copy_lock);
315
316static inline int flock_translate_cmd(int cmd) {
317 if (cmd & LOCK_MAND)
318 return cmd & (LOCK_MAND | LOCK_RW);
319 switch (cmd) {
320 case LOCK_SH:
321 return F_RDLCK;
322 case LOCK_EX:
323 return F_WRLCK;
324 case LOCK_UN:
325 return F_UNLCK;
326 }
327 return -EINVAL;
328}
329
330/* Fill in a file_lock structure with an appropriate FLOCK lock. */
6e129d00
JL
331static struct file_lock *
332flock_make_lock(struct file *filp, unsigned int cmd)
1da177e4
LT
333{
334 struct file_lock *fl;
335 int type = flock_translate_cmd(cmd);
6e129d00 336
1da177e4 337 if (type < 0)
6e129d00 338 return ERR_PTR(type);
1da177e4
LT
339
340 fl = locks_alloc_lock();
341 if (fl == NULL)
6e129d00 342 return ERR_PTR(-ENOMEM);
1da177e4
LT
343
344 fl->fl_file = filp;
73a8f5f7 345 fl->fl_owner = filp;
1da177e4
LT
346 fl->fl_pid = current->tgid;
347 fl->fl_flags = FL_FLOCK;
348 fl->fl_type = type;
349 fl->fl_end = OFFSET_MAX;
350
6e129d00 351 return fl;
1da177e4
LT
352}
353
0ec4f431 354static int assign_type(struct file_lock *fl, long type)
1da177e4
LT
355{
356 switch (type) {
357 case F_RDLCK:
358 case F_WRLCK:
359 case F_UNLCK:
360 fl->fl_type = type;
361 break;
362 default:
363 return -EINVAL;
364 }
365 return 0;
366}
367
ef12e72a
BF
368static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
369 struct flock64 *l)
1da177e4 370{
1da177e4 371 switch (l->l_whence) {
f5579f8c 372 case SEEK_SET:
ef12e72a 373 fl->fl_start = 0;
1da177e4 374 break;
f5579f8c 375 case SEEK_CUR:
ef12e72a 376 fl->fl_start = filp->f_pos;
1da177e4 377 break;
f5579f8c 378 case SEEK_END:
ef12e72a 379 fl->fl_start = i_size_read(file_inode(filp));
1da177e4
LT
380 break;
381 default:
382 return -EINVAL;
383 }
ef12e72a
BF
384 if (l->l_start > OFFSET_MAX - fl->fl_start)
385 return -EOVERFLOW;
386 fl->fl_start += l->l_start;
387 if (fl->fl_start < 0)
388 return -EINVAL;
1da177e4
LT
389
390 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
391 POSIX-2001 defines it. */
4c780a46 392 if (l->l_len > 0) {
ef12e72a
BF
393 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
394 return -EOVERFLOW;
395 fl->fl_end = fl->fl_start + l->l_len - 1;
396
4c780a46 397 } else if (l->l_len < 0) {
ef12e72a 398 if (fl->fl_start + l->l_len < 0)
4c780a46 399 return -EINVAL;
ef12e72a
BF
400 fl->fl_end = fl->fl_start - 1;
401 fl->fl_start += l->l_len;
402 } else
403 fl->fl_end = OFFSET_MAX;
404
1da177e4
LT
405 fl->fl_owner = current->files;
406 fl->fl_pid = current->tgid;
407 fl->fl_file = filp;
408 fl->fl_flags = FL_POSIX;
409 fl->fl_ops = NULL;
410 fl->fl_lmops = NULL;
411
412 return assign_type(fl, l->l_type);
413}
414
ef12e72a
BF
415/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
416 * style lock.
417 */
418static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
419 struct flock *l)
1da177e4 420{
ef12e72a
BF
421 struct flock64 ll = {
422 .l_type = l->l_type,
423 .l_whence = l->l_whence,
424 .l_start = l->l_start,
425 .l_len = l->l_len,
426 };
427
428 return flock64_to_posix_lock(filp, fl, &ll);
1da177e4 429}
1da177e4
LT
430
431/* default lease lock manager operations */
4d01b7f5
JL
432static bool
433lease_break_callback(struct file_lock *fl)
1da177e4
LT
434{
435 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
4d01b7f5 436 return false;
1da177e4
LT
437}
438
1c7dd2ff
JL
439static void
440lease_setup(struct file_lock *fl, void **priv)
441{
442 struct file *filp = fl->fl_file;
443 struct fasync_struct *fa = *priv;
444
445 /*
446 * fasync_insert_entry() returns the old entry if any. If there was no
447 * old entry, then it used "priv" and inserted it into the fasync list.
448 * Clear the pointer to indicate that it shouldn't be freed.
449 */
450 if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
451 *priv = NULL;
452
453 __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
454}
455
7b021967 456static const struct lock_manager_operations lease_manager_ops = {
8fb47a4f 457 .lm_break = lease_break_callback,
8fb47a4f 458 .lm_change = lease_modify,
1c7dd2ff 459 .lm_setup = lease_setup,
1da177e4
LT
460};
461
462/*
463 * Initialize a lease, use the default lock manager operations
464 */
0ec4f431 465static int lease_init(struct file *filp, long type, struct file_lock *fl)
1da177e4 466 {
75dff55a
TM
467 if (assign_type(fl, type) != 0)
468 return -EINVAL;
469
7ca76311 470 fl->fl_owner = filp;
1da177e4
LT
471 fl->fl_pid = current->tgid;
472
473 fl->fl_file = filp;
474 fl->fl_flags = FL_LEASE;
1da177e4
LT
475 fl->fl_start = 0;
476 fl->fl_end = OFFSET_MAX;
477 fl->fl_ops = NULL;
478 fl->fl_lmops = &lease_manager_ops;
479 return 0;
480}
481
482/* Allocate a file_lock initialised to this type of lease */
0ec4f431 483static struct file_lock *lease_alloc(struct file *filp, long type)
1da177e4
LT
484{
485 struct file_lock *fl = locks_alloc_lock();
75dff55a 486 int error = -ENOMEM;
1da177e4
LT
487
488 if (fl == NULL)
e32b8ee2 489 return ERR_PTR(error);
1da177e4
LT
490
491 error = lease_init(filp, type, fl);
75dff55a
TM
492 if (error) {
493 locks_free_lock(fl);
e32b8ee2 494 return ERR_PTR(error);
75dff55a 495 }
e32b8ee2 496 return fl;
1da177e4
LT
497}
498
499/* Check if two locks overlap each other.
500 */
501static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
502{
503 return ((fl1->fl_end >= fl2->fl_start) &&
504 (fl2->fl_end >= fl1->fl_start));
505}
506
507/*
508 * Check whether two locks have the same owner.
509 */
33443c42 510static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
1da177e4 511{
8fb47a4f 512 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
1da177e4 513 return fl2->fl_lmops == fl1->fl_lmops &&
8fb47a4f 514 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
1da177e4
LT
515 return fl1->fl_owner == fl2->fl_owner;
516}
517
7012b02a 518/* Must be called with the i_lock held! */
6ca10ed8 519static void locks_insert_global_locks(struct file_lock *fl)
88974691 520{
7012b02a
JL
521 lg_local_lock(&file_lock_lglock);
522 fl->fl_link_cpu = smp_processor_id();
523 hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
524 lg_local_unlock(&file_lock_lglock);
88974691
JL
525}
526
7012b02a 527/* Must be called with the i_lock held! */
6ca10ed8 528static void locks_delete_global_locks(struct file_lock *fl)
88974691 529{
7012b02a
JL
530 /*
531 * Avoid taking lock if already unhashed. This is safe since this check
532 * is done while holding the i_lock, and new insertions into the list
533 * also require that it be held.
534 */
535 if (hlist_unhashed(&fl->fl_link))
536 return;
537 lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
139ca04e 538 hlist_del_init(&fl->fl_link);
7012b02a 539 lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
88974691
JL
540}
541
3999e493
JL
542static unsigned long
543posix_owner_key(struct file_lock *fl)
544{
545 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
546 return fl->fl_lmops->lm_owner_key(fl);
547 return (unsigned long)fl->fl_owner;
548}
549
6ca10ed8 550static void locks_insert_global_blocked(struct file_lock *waiter)
88974691 551{
3999e493 552 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
88974691
JL
553}
554
6ca10ed8 555static void locks_delete_global_blocked(struct file_lock *waiter)
88974691 556{
48f74186 557 hash_del(&waiter->fl_link);
88974691
JL
558}
559
1da177e4
LT
560/* Remove waiter from blocker's block list.
561 * When blocker ends up pointing to itself then the list is empty.
1c8c601a 562 *
7b2296af 563 * Must be called with blocked_lock_lock held.
1da177e4 564 */
33443c42 565static void __locks_delete_block(struct file_lock *waiter)
1da177e4 566{
88974691 567 locks_delete_global_blocked(waiter);
1da177e4 568 list_del_init(&waiter->fl_block);
1da177e4
LT
569 waiter->fl_next = NULL;
570}
571
1a9e64a7 572static void locks_delete_block(struct file_lock *waiter)
1da177e4 573{
7b2296af 574 spin_lock(&blocked_lock_lock);
1da177e4 575 __locks_delete_block(waiter);
7b2296af 576 spin_unlock(&blocked_lock_lock);
1da177e4
LT
577}
578
579/* Insert waiter into blocker's block list.
580 * We use a circular list so that processes can be easily woken up in
581 * the order they blocked. The documentation doesn't require this but
582 * it seems like the reasonable thing to do.
1c8c601a 583 *
7b2296af 584 * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
46dad760 585 * list itself is protected by the blocked_lock_lock, but by ensuring that the
7b2296af 586 * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
4e8c765d 587 * in some cases when we see that the fl_block list is empty.
1da177e4 588 */
1c8c601a
JL
589static void __locks_insert_block(struct file_lock *blocker,
590 struct file_lock *waiter)
1da177e4 591{
6dc0fe8f 592 BUG_ON(!list_empty(&waiter->fl_block));
1da177e4 593 waiter->fl_next = blocker;
88974691 594 list_add_tail(&waiter->fl_block, &blocker->fl_block);
cff2fce5 595 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
1c8c601a
JL
596 locks_insert_global_blocked(waiter);
597}
598
599/* Must be called with i_lock held. */
600static void locks_insert_block(struct file_lock *blocker,
601 struct file_lock *waiter)
602{
7b2296af 603 spin_lock(&blocked_lock_lock);
1c8c601a 604 __locks_insert_block(blocker, waiter);
7b2296af 605 spin_unlock(&blocked_lock_lock);
1da177e4
LT
606}
607
1cb36012
JL
608/*
609 * Wake up processes blocked waiting for blocker.
610 *
1c8c601a 611 * Must be called with the inode->i_lock held!
1da177e4
LT
612 */
613static void locks_wake_up_blocks(struct file_lock *blocker)
614{
4e8c765d
JL
615 /*
616 * Avoid taking global lock if list is empty. This is safe since new
617 * blocked requests are only added to the list under the i_lock, and
618 * the i_lock is always held here. Note that removal from the fl_block
619 * list does not require the i_lock, so we must recheck list_empty()
7b2296af 620 * after acquiring the blocked_lock_lock.
4e8c765d
JL
621 */
622 if (list_empty(&blocker->fl_block))
623 return;
624
7b2296af 625 spin_lock(&blocked_lock_lock);
1da177e4 626 while (!list_empty(&blocker->fl_block)) {
f0c1cd0e
PE
627 struct file_lock *waiter;
628
629 waiter = list_first_entry(&blocker->fl_block,
1da177e4
LT
630 struct file_lock, fl_block);
631 __locks_delete_block(waiter);
8fb47a4f
BF
632 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
633 waiter->fl_lmops->lm_notify(waiter);
1da177e4
LT
634 else
635 wake_up(&waiter->fl_wait);
636 }
7b2296af 637 spin_unlock(&blocked_lock_lock);
1da177e4
LT
638}
639
640/* Insert file lock fl into an inode's lock list at the position indicated
641 * by pos. At the same time add the lock to the global file lock list.
1c8c601a
JL
642 *
643 * Must be called with the i_lock held!
1da177e4
LT
644 */
645static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
646{
ab1f1611
VG
647 fl->fl_nspid = get_pid(task_tgid(current));
648
1da177e4
LT
649 /* insert into file's list */
650 fl->fl_next = *pos;
651 *pos = fl;
88974691
JL
652
653 locks_insert_global_locks(fl);
1da177e4
LT
654}
655
24cbe784
JL
656/**
657 * locks_delete_lock - Delete a lock and then free it.
658 * @thisfl_p: pointer that points to the fl_next field of the previous
659 * inode->i_flock list entry
660 *
661 * Unlink a lock from all lists and free the namespace reference, but don't
662 * free it yet. Wake up processes that are blocked waiting for this lock and
663 * notify the FS that the lock has been cleared.
1c8c601a
JL
664 *
665 * Must be called with the i_lock held!
1da177e4 666 */
24cbe784 667static void locks_unlink_lock(struct file_lock **thisfl_p)
1da177e4
LT
668{
669 struct file_lock *fl = *thisfl_p;
670
88974691
JL
671 locks_delete_global_locks(fl);
672
1da177e4
LT
673 *thisfl_p = fl->fl_next;
674 fl->fl_next = NULL;
1da177e4 675
ab1f1611
VG
676 if (fl->fl_nspid) {
677 put_pid(fl->fl_nspid);
678 fl->fl_nspid = NULL;
679 }
680
1da177e4 681 locks_wake_up_blocks(fl);
24cbe784
JL
682}
683
684/*
685 * Unlink a lock from all lists and free it.
686 *
687 * Must be called with i_lock held!
688 */
ed9814d8
JL
689static void locks_delete_lock(struct file_lock **thisfl_p,
690 struct list_head *dispose)
24cbe784
JL
691{
692 struct file_lock *fl = *thisfl_p;
693
694 locks_unlink_lock(thisfl_p);
ed9814d8 695 if (dispose)
6dee60f6 696 list_add(&fl->fl_list, dispose);
ed9814d8
JL
697 else
698 locks_free_lock(fl);
1da177e4
LT
699}
700
701/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
702 * checks for shared/exclusive status of overlapping locks.
703 */
704static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
705{
706 if (sys_fl->fl_type == F_WRLCK)
707 return 1;
708 if (caller_fl->fl_type == F_WRLCK)
709 return 1;
710 return 0;
711}
712
713/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
714 * checking before calling the locks_conflict().
715 */
716static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
717{
718 /* POSIX locks owned by the same process do not conflict with
719 * each other.
720 */
721 if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
722 return (0);
723
724 /* Check whether they overlap */
725 if (!locks_overlap(caller_fl, sys_fl))
726 return 0;
727
728 return (locks_conflict(caller_fl, sys_fl));
729}
730
731/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
732 * checking before calling the locks_conflict().
733 */
734static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
735{
736 /* FLOCK locks referring to the same filp do not conflict with
737 * each other.
738 */
739 if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
740 return (0);
741 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
742 return 0;
743
744 return (locks_conflict(caller_fl, sys_fl));
745}
746
6d34ac19 747void
9d6a8c5c 748posix_test_lock(struct file *filp, struct file_lock *fl)
1da177e4
LT
749{
750 struct file_lock *cfl;
1c8c601a 751 struct inode *inode = file_inode(filp);
1da177e4 752
1c8c601a 753 spin_lock(&inode->i_lock);
496ad9aa 754 for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
1da177e4
LT
755 if (!IS_POSIX(cfl))
756 continue;
b842e240 757 if (posix_locks_conflict(fl, cfl))
1da177e4
LT
758 break;
759 }
ab1f1611 760 if (cfl) {
3fe0fff1 761 locks_copy_conflock(fl, cfl);
ab1f1611 762 if (cfl->fl_nspid)
6c5f3e7b 763 fl->fl_pid = pid_vnr(cfl->fl_nspid);
ab1f1611 764 } else
129a84de 765 fl->fl_type = F_UNLCK;
1c8c601a 766 spin_unlock(&inode->i_lock);
6d34ac19 767 return;
1da177e4 768}
1da177e4
LT
769EXPORT_SYMBOL(posix_test_lock);
770
b533184f
BF
771/*
772 * Deadlock detection:
773 *
774 * We attempt to detect deadlocks that are due purely to posix file
775 * locks.
1da177e4 776 *
b533184f
BF
777 * We assume that a task can be waiting for at most one lock at a time.
778 * So for any acquired lock, the process holding that lock may be
779 * waiting on at most one other lock. That lock in turns may be held by
780 * someone waiting for at most one other lock. Given a requested lock
781 * caller_fl which is about to wait for a conflicting lock block_fl, we
782 * follow this chain of waiters to ensure we are not about to create a
783 * cycle.
1da177e4 784 *
b533184f
BF
785 * Since we do this before we ever put a process to sleep on a lock, we
786 * are ensured that there is never a cycle; that is what guarantees that
787 * the while() loop in posix_locks_deadlock() eventually completes.
97855b49 788 *
b533184f
BF
789 * Note: the above assumption may not be true when handling lock
790 * requests from a broken NFS client. It may also fail in the presence
791 * of tasks (such as posix threads) sharing the same open file table.
b533184f 792 * To handle those cases, we just bail out after a few iterations.
57b65325 793 *
cff2fce5 794 * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
57b65325
JL
795 * Because the owner is not even nominally tied to a thread of
796 * execution, the deadlock detection below can't reasonably work well. Just
797 * skip it for those.
798 *
cff2fce5 799 * In principle, we could do a more limited deadlock detection on FL_OFDLCK
57b65325
JL
800 * locks that just checks for the case where two tasks are attempting to
801 * upgrade from read to write locks on the same inode.
1da177e4 802 */
97855b49
BF
803
804#define MAX_DEADLK_ITERATIONS 10
805
b533184f
BF
806/* Find a lock that the owner of the given block_fl is blocking on. */
807static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
808{
809 struct file_lock *fl;
810
3999e493 811 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
b533184f
BF
812 if (posix_same_owner(fl, block_fl))
813 return fl->fl_next;
814 }
815 return NULL;
816}
817
7b2296af 818/* Must be called with the blocked_lock_lock held! */
b0904e14 819static int posix_locks_deadlock(struct file_lock *caller_fl,
1da177e4
LT
820 struct file_lock *block_fl)
821{
97855b49 822 int i = 0;
1da177e4 823
57b65325
JL
824 /*
825 * This deadlock detector can't reasonably detect deadlocks with
cff2fce5 826 * FL_OFDLCK locks, since they aren't owned by a process, per-se.
57b65325 827 */
cff2fce5 828 if (IS_OFDLCK(caller_fl))
57b65325
JL
829 return 0;
830
b533184f
BF
831 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
832 if (i++ > MAX_DEADLK_ITERATIONS)
833 return 0;
834 if (posix_same_owner(caller_fl, block_fl))
835 return 1;
1da177e4
LT
836 }
837 return 0;
838}
839
1da177e4 840/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
02888f41 841 * after any leases, but before any posix locks.
f475ae95
TM
842 *
843 * Note that if called with an FL_EXISTS argument, the caller may determine
844 * whether or not a lock was successfully freed by testing the return
845 * value for -ENOENT.
1da177e4 846 */
993dfa87 847static int flock_lock_file(struct file *filp, struct file_lock *request)
1da177e4 848{
993dfa87 849 struct file_lock *new_fl = NULL;
1da177e4 850 struct file_lock **before;
496ad9aa 851 struct inode * inode = file_inode(filp);
1da177e4
LT
852 int error = 0;
853 int found = 0;
ed9814d8 854 LIST_HEAD(dispose);
1da177e4 855
b89f4321 856 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
84d535ad 857 new_fl = locks_alloc_lock();
b89f4321
AB
858 if (!new_fl)
859 return -ENOMEM;
84d535ad
PE
860 }
861
1c8c601a 862 spin_lock(&inode->i_lock);
b89f4321
AB
863 if (request->fl_flags & FL_ACCESS)
864 goto find_conflict;
865
1da177e4
LT
866 for_each_lock(inode, before) {
867 struct file_lock *fl = *before;
868 if (IS_POSIX(fl))
869 break;
870 if (IS_LEASE(fl))
871 continue;
872 if (filp != fl->fl_file)
873 continue;
993dfa87 874 if (request->fl_type == fl->fl_type)
1da177e4
LT
875 goto out;
876 found = 1;
ed9814d8 877 locks_delete_lock(before, &dispose);
1da177e4
LT
878 break;
879 }
1da177e4 880
f475ae95
TM
881 if (request->fl_type == F_UNLCK) {
882 if ((request->fl_flags & FL_EXISTS) && !found)
883 error = -ENOENT;
993dfa87 884 goto out;
f475ae95 885 }
1da177e4
LT
886
887 /*
888 * If a higher-priority process was blocked on the old file lock,
889 * give it the opportunity to lock the file.
890 */
b89f4321 891 if (found) {
1c8c601a 892 spin_unlock(&inode->i_lock);
def01bc5 893 cond_resched();
1c8c601a 894 spin_lock(&inode->i_lock);
b89f4321 895 }
1da177e4 896
f07f18dd 897find_conflict:
1da177e4
LT
898 for_each_lock(inode, before) {
899 struct file_lock *fl = *before;
900 if (IS_POSIX(fl))
901 break;
902 if (IS_LEASE(fl))
903 continue;
993dfa87 904 if (!flock_locks_conflict(request, fl))
1da177e4
LT
905 continue;
906 error = -EAGAIN;
bde74e4b
MS
907 if (!(request->fl_flags & FL_SLEEP))
908 goto out;
909 error = FILE_LOCK_DEFERRED;
910 locks_insert_block(fl, request);
1da177e4
LT
911 goto out;
912 }
f07f18dd
TM
913 if (request->fl_flags & FL_ACCESS)
914 goto out;
993dfa87 915 locks_copy_lock(new_fl, request);
0e2f6db8 916 locks_insert_lock(before, new_fl);
993dfa87 917 new_fl = NULL;
9cedc194 918 error = 0;
1da177e4
LT
919
920out:
1c8c601a 921 spin_unlock(&inode->i_lock);
993dfa87
TM
922 if (new_fl)
923 locks_free_lock(new_fl);
ed9814d8 924 locks_dispose_list(&dispose);
1da177e4
LT
925 return error;
926}
927
150b3934 928static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
1da177e4
LT
929{
930 struct file_lock *fl;
39005d02
MS
931 struct file_lock *new_fl = NULL;
932 struct file_lock *new_fl2 = NULL;
1da177e4
LT
933 struct file_lock *left = NULL;
934 struct file_lock *right = NULL;
935 struct file_lock **before;
b9746ef8
JL
936 int error;
937 bool added = false;
ed9814d8 938 LIST_HEAD(dispose);
1da177e4
LT
939
940 /*
941 * We may need two file_lock structures for this operation,
942 * so we get them in advance to avoid races.
39005d02
MS
943 *
944 * In some cases we can be sure, that no new locks will be needed
1da177e4 945 */
39005d02
MS
946 if (!(request->fl_flags & FL_ACCESS) &&
947 (request->fl_type != F_UNLCK ||
948 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
949 new_fl = locks_alloc_lock();
950 new_fl2 = locks_alloc_lock();
951 }
1da177e4 952
1c8c601a 953 spin_lock(&inode->i_lock);
1cb36012
JL
954 /*
955 * New lock request. Walk all POSIX locks and look for conflicts. If
956 * there are any, either return error or put the request on the
48f74186 957 * blocker's list of waiters and the global blocked_hash.
1cb36012 958 */
1da177e4
LT
959 if (request->fl_type != F_UNLCK) {
960 for_each_lock(inode, before) {
526985b9 961 fl = *before;
1da177e4
LT
962 if (!IS_POSIX(fl))
963 continue;
964 if (!posix_locks_conflict(request, fl))
965 continue;
5842add2 966 if (conflock)
3fe0fff1 967 locks_copy_conflock(conflock, fl);
1da177e4
LT
968 error = -EAGAIN;
969 if (!(request->fl_flags & FL_SLEEP))
970 goto out;
1c8c601a
JL
971 /*
972 * Deadlock detection and insertion into the blocked
973 * locks list must be done while holding the same lock!
974 */
1da177e4 975 error = -EDEADLK;
7b2296af 976 spin_lock(&blocked_lock_lock);
1c8c601a
JL
977 if (likely(!posix_locks_deadlock(request, fl))) {
978 error = FILE_LOCK_DEFERRED;
979 __locks_insert_block(fl, request);
980 }
7b2296af 981 spin_unlock(&blocked_lock_lock);
1da177e4
LT
982 goto out;
983 }
984 }
985
986 /* If we're just looking for a conflict, we're done. */
987 error = 0;
988 if (request->fl_flags & FL_ACCESS)
989 goto out;
990
1da177e4 991 /*
1da177e4
LT
992 * Find the first old lock with the same owner as the new lock.
993 */
994
995 before = &inode->i_flock;
996
997 /* First skip locks owned by other processes. */
998 while ((fl = *before) && (!IS_POSIX(fl) ||
999 !posix_same_owner(request, fl))) {
1000 before = &fl->fl_next;
1001 }
1002
1cb36012 1003 /* Process locks with this owner. */
1da177e4
LT
1004 while ((fl = *before) && posix_same_owner(request, fl)) {
1005 /* Detect adjacent or overlapping regions (if same lock type)
1006 */
1007 if (request->fl_type == fl->fl_type) {
449231d6
OK
1008 /* In all comparisons of start vs end, use
1009 * "start - 1" rather than "end + 1". If end
1010 * is OFFSET_MAX, end + 1 will become negative.
1011 */
1da177e4
LT
1012 if (fl->fl_end < request->fl_start - 1)
1013 goto next_lock;
1014 /* If the next lock in the list has entirely bigger
1015 * addresses than the new one, insert the lock here.
1016 */
449231d6 1017 if (fl->fl_start - 1 > request->fl_end)
1da177e4
LT
1018 break;
1019
1020 /* If we come here, the new and old lock are of the
1021 * same type and adjacent or overlapping. Make one
1022 * lock yielding from the lower start address of both
1023 * locks to the higher end address.
1024 */
1025 if (fl->fl_start > request->fl_start)
1026 fl->fl_start = request->fl_start;
1027 else
1028 request->fl_start = fl->fl_start;
1029 if (fl->fl_end < request->fl_end)
1030 fl->fl_end = request->fl_end;
1031 else
1032 request->fl_end = fl->fl_end;
1033 if (added) {
ed9814d8 1034 locks_delete_lock(before, &dispose);
1da177e4
LT
1035 continue;
1036 }
1037 request = fl;
b9746ef8 1038 added = true;
1da177e4
LT
1039 }
1040 else {
1041 /* Processing for different lock types is a bit
1042 * more complex.
1043 */
1044 if (fl->fl_end < request->fl_start)
1045 goto next_lock;
1046 if (fl->fl_start > request->fl_end)
1047 break;
1048 if (request->fl_type == F_UNLCK)
b9746ef8 1049 added = true;
1da177e4
LT
1050 if (fl->fl_start < request->fl_start)
1051 left = fl;
1052 /* If the next lock in the list has a higher end
1053 * address than the new one, insert the new one here.
1054 */
1055 if (fl->fl_end > request->fl_end) {
1056 right = fl;
1057 break;
1058 }
1059 if (fl->fl_start >= request->fl_start) {
1060 /* The new lock completely replaces an old
1061 * one (This may happen several times).
1062 */
1063 if (added) {
ed9814d8 1064 locks_delete_lock(before, &dispose);
1da177e4
LT
1065 continue;
1066 }
b84d49f9
JL
1067 /*
1068 * Replace the old lock with new_fl, and
1069 * remove the old one. It's safe to do the
1070 * insert here since we know that we won't be
1071 * using new_fl later, and that the lock is
1072 * just replacing an existing lock.
1da177e4 1073 */
b84d49f9
JL
1074 error = -ENOLCK;
1075 if (!new_fl)
1076 goto out;
1077 locks_copy_lock(new_fl, request);
1078 request = new_fl;
1079 new_fl = NULL;
ed9814d8 1080 locks_delete_lock(before, &dispose);
b84d49f9 1081 locks_insert_lock(before, request);
b9746ef8 1082 added = true;
1da177e4
LT
1083 }
1084 }
1085 /* Go on to next lock.
1086 */
1087 next_lock:
1088 before = &fl->fl_next;
1089 }
1090
0d9a490a 1091 /*
1cb36012
JL
1092 * The above code only modifies existing locks in case of merging or
1093 * replacing. If new lock(s) need to be inserted all modifications are
1094 * done below this, so it's safe yet to bail out.
0d9a490a
MS
1095 */
1096 error = -ENOLCK; /* "no luck" */
1097 if (right && left == right && !new_fl2)
1098 goto out;
1099
1da177e4
LT
1100 error = 0;
1101 if (!added) {
f475ae95
TM
1102 if (request->fl_type == F_UNLCK) {
1103 if (request->fl_flags & FL_EXISTS)
1104 error = -ENOENT;
1da177e4 1105 goto out;
f475ae95 1106 }
0d9a490a
MS
1107
1108 if (!new_fl) {
1109 error = -ENOLCK;
1110 goto out;
1111 }
1da177e4
LT
1112 locks_copy_lock(new_fl, request);
1113 locks_insert_lock(before, new_fl);
1114 new_fl = NULL;
1115 }
1116 if (right) {
1117 if (left == right) {
1118 /* The new lock breaks the old one in two pieces,
1119 * so we have to use the second new lock.
1120 */
1121 left = new_fl2;
1122 new_fl2 = NULL;
1123 locks_copy_lock(left, right);
1124 locks_insert_lock(before, left);
1125 }
1126 right->fl_start = request->fl_end + 1;
1127 locks_wake_up_blocks(right);
1128 }
1129 if (left) {
1130 left->fl_end = request->fl_start - 1;
1131 locks_wake_up_blocks(left);
1132 }
1133 out:
1c8c601a 1134 spin_unlock(&inode->i_lock);
1da177e4
LT
1135 /*
1136 * Free any unused locks.
1137 */
1138 if (new_fl)
1139 locks_free_lock(new_fl);
1140 if (new_fl2)
1141 locks_free_lock(new_fl2);
ed9814d8 1142 locks_dispose_list(&dispose);
1da177e4
LT
1143 return error;
1144}
1145
1146/**
1147 * posix_lock_file - Apply a POSIX-style lock to a file
1148 * @filp: The file to apply the lock to
1149 * @fl: The lock to be applied
150b3934 1150 * @conflock: Place to return a copy of the conflicting lock, if found.
1da177e4
LT
1151 *
1152 * Add a POSIX style lock to a file.
1153 * We merge adjacent & overlapping locks whenever possible.
1154 * POSIX locks are sorted by owner task, then by starting address
f475ae95
TM
1155 *
1156 * Note that if called with an FL_EXISTS argument, the caller may determine
1157 * whether or not a lock was successfully freed by testing the return
1158 * value for -ENOENT.
1da177e4 1159 */
150b3934 1160int posix_lock_file(struct file *filp, struct file_lock *fl,
5842add2
AA
1161 struct file_lock *conflock)
1162{
496ad9aa 1163 return __posix_lock_file(file_inode(filp), fl, conflock);
1da177e4 1164}
150b3934 1165EXPORT_SYMBOL(posix_lock_file);
1da177e4
LT
1166
1167/**
1168 * posix_lock_file_wait - Apply a POSIX-style lock to a file
1169 * @filp: The file to apply the lock to
1170 * @fl: The lock to be applied
1171 *
1172 * Add a POSIX style lock to a file.
1173 * We merge adjacent & overlapping locks whenever possible.
1174 * POSIX locks are sorted by owner task, then by starting address
1175 */
1176int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
1177{
1178 int error;
1179 might_sleep ();
1180 for (;;) {
150b3934 1181 error = posix_lock_file(filp, fl, NULL);
bde74e4b 1182 if (error != FILE_LOCK_DEFERRED)
1da177e4
LT
1183 break;
1184 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1185 if (!error)
1186 continue;
1187
1188 locks_delete_block(fl);
1189 break;
1190 }
1191 return error;
1192}
1193EXPORT_SYMBOL(posix_lock_file_wait);
1194
1195/**
1196 * locks_mandatory_locked - Check for an active lock
d7a06983 1197 * @file: the file to check
1da177e4
LT
1198 *
1199 * Searches the inode's list of locks to find any POSIX locks which conflict.
1200 * This function is called from locks_verify_locked() only.
1201 */
d7a06983 1202int locks_mandatory_locked(struct file *file)
1da177e4 1203{
d7a06983 1204 struct inode *inode = file_inode(file);
1da177e4
LT
1205 struct file_lock *fl;
1206
1207 /*
1208 * Search the lock list for this inode for any POSIX locks.
1209 */
1c8c601a 1210 spin_lock(&inode->i_lock);
1da177e4
LT
1211 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1212 if (!IS_POSIX(fl))
1213 continue;
73a8f5f7
CH
1214 if (fl->fl_owner != current->files &&
1215 fl->fl_owner != file)
1da177e4
LT
1216 break;
1217 }
1c8c601a 1218 spin_unlock(&inode->i_lock);
1da177e4
LT
1219 return fl ? -EAGAIN : 0;
1220}
1221
1222/**
1223 * locks_mandatory_area - Check for a conflicting lock
1224 * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
1225 * for shared
1226 * @inode: the file to check
1227 * @filp: how the file was opened (if it was)
1228 * @offset: start of area to check
1229 * @count: length of area to check
1230 *
1231 * Searches the inode's list of locks to find any POSIX locks which conflict.
1232 * This function is called from rw_verify_area() and
1233 * locks_verify_truncate().
1234 */
1235int locks_mandatory_area(int read_write, struct inode *inode,
1236 struct file *filp, loff_t offset,
1237 size_t count)
1238{
1239 struct file_lock fl;
1240 int error;
29723ade 1241 bool sleep = false;
1da177e4
LT
1242
1243 locks_init_lock(&fl);
1da177e4
LT
1244 fl.fl_pid = current->tgid;
1245 fl.fl_file = filp;
1246 fl.fl_flags = FL_POSIX | FL_ACCESS;
1247 if (filp && !(filp->f_flags & O_NONBLOCK))
29723ade 1248 sleep = true;
1da177e4
LT
1249 fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
1250 fl.fl_start = offset;
1251 fl.fl_end = offset + count - 1;
1252
1253 for (;;) {
29723ade 1254 if (filp) {
73a8f5f7 1255 fl.fl_owner = filp;
29723ade
JL
1256 fl.fl_flags &= ~FL_SLEEP;
1257 error = __posix_lock_file(inode, &fl, NULL);
1258 if (!error)
1259 break;
1260 }
1261
1262 if (sleep)
1263 fl.fl_flags |= FL_SLEEP;
1264 fl.fl_owner = current->files;
150b3934 1265 error = __posix_lock_file(inode, &fl, NULL);
bde74e4b 1266 if (error != FILE_LOCK_DEFERRED)
1da177e4
LT
1267 break;
1268 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1269 if (!error) {
1270 /*
1271 * If we've been sleeping someone might have
1272 * changed the permissions behind our back.
1273 */
a16877ca 1274 if (__mandatory_lock(inode))
1da177e4
LT
1275 continue;
1276 }
1277
1278 locks_delete_block(&fl);
1279 break;
1280 }
1281
1282 return error;
1283}
1284
1285EXPORT_SYMBOL(locks_mandatory_area);
1286
778fc546
BF
1287static void lease_clear_pending(struct file_lock *fl, int arg)
1288{
1289 switch (arg) {
1290 case F_UNLCK:
1291 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1292 /* fall through: */
1293 case F_RDLCK:
1294 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1295 }
1296}
1297
1da177e4 1298/* We already had a lease on this file; just change its type */
c45198ed 1299int lease_modify(struct file_lock **before, int arg, struct list_head *dispose)
1da177e4
LT
1300{
1301 struct file_lock *fl = *before;
1302 int error = assign_type(fl, arg);
1303
1304 if (error)
1305 return error;
778fc546 1306 lease_clear_pending(fl, arg);
1da177e4 1307 locks_wake_up_blocks(fl);
3b6e2723
FB
1308 if (arg == F_UNLCK) {
1309 struct file *filp = fl->fl_file;
1310
1311 f_delown(filp);
1312 filp->f_owner.signum = 0;
96d6d59c
BF
1313 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1314 if (fl->fl_fasync != NULL) {
1315 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1316 fl->fl_fasync = NULL;
1317 }
c45198ed 1318 locks_delete_lock(before, dispose);
3b6e2723 1319 }
1da177e4
LT
1320 return 0;
1321}
1da177e4
LT
1322EXPORT_SYMBOL(lease_modify);
1323
778fc546
BF
1324static bool past_time(unsigned long then)
1325{
1326 if (!then)
1327 /* 0 is a special value meaning "this never expires": */
1328 return false;
1329 return time_after(jiffies, then);
1330}
1331
c45198ed 1332static void time_out_leases(struct inode *inode, struct list_head *dispose)
1da177e4
LT
1333{
1334 struct file_lock **before;
1335 struct file_lock *fl;
1336
f82b4b67
JL
1337 lockdep_assert_held(&inode->i_lock);
1338
1da177e4 1339 before = &inode->i_flock;
ab83fa4b 1340 while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
62af4f1f 1341 trace_time_out_leases(inode, fl);
778fc546 1342 if (past_time(fl->fl_downgrade_time))
c45198ed 1343 lease_modify(before, F_RDLCK, dispose);
778fc546 1344 if (past_time(fl->fl_break_time))
c45198ed 1345 lease_modify(before, F_UNLCK, dispose);
1da177e4
LT
1346 if (fl == *before) /* lease_modify may have freed fl */
1347 before = &fl->fl_next;
1348 }
1349}
1350
df4e8d2c
BF
1351static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1352{
1353 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1354 return false;
1355 return locks_conflict(breaker, lease);
1356}
1357
03d12ddf
JL
1358static bool
1359any_leases_conflict(struct inode *inode, struct file_lock *breaker)
1360{
1361 struct file_lock *fl;
1362
1363 lockdep_assert_held(&inode->i_lock);
1364
1365 for (fl = inode->i_flock ; fl && IS_LEASE(fl); fl = fl->fl_next) {
1366 if (leases_conflict(fl, breaker))
1367 return true;
1368 }
1369 return false;
1370}
1371
1da177e4
LT
1372/**
1373 * __break_lease - revoke all outstanding leases on file
1374 * @inode: the inode of the file to return
df4e8d2c
BF
1375 * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
1376 * break all leases
1377 * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
1378 * only delegations
1da177e4 1379 *
87250dd2 1380 * break_lease (inlined for speed) has checked there already is at least
1381 * some kind of lock (maybe a lease) on this file. Leases are broken on
1382 * a call to open() or truncate(). This function can sleep unless you
1da177e4
LT
1383 * specified %O_NONBLOCK to your open().
1384 */
df4e8d2c 1385int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1da177e4 1386{
778fc546 1387 int error = 0;
03d12ddf 1388 struct file_lock *new_fl;
4d01b7f5 1389 struct file_lock *fl, **before;
1da177e4 1390 unsigned long break_time;
8737c930 1391 int want_write = (mode & O_ACCMODE) != O_RDONLY;
c45198ed 1392 LIST_HEAD(dispose);
1da177e4 1393
8737c930 1394 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
6d4b9e38
LT
1395 if (IS_ERR(new_fl))
1396 return PTR_ERR(new_fl);
df4e8d2c 1397 new_fl->fl_flags = type;
1da177e4 1398
1c8c601a 1399 spin_lock(&inode->i_lock);
1da177e4 1400
c45198ed 1401 time_out_leases(inode, &dispose);
1da177e4 1402
03d12ddf 1403 if (!any_leases_conflict(inode, new_fl))
778fc546
BF
1404 goto out;
1405
1da177e4
LT
1406 break_time = 0;
1407 if (lease_break_time > 0) {
1408 break_time = jiffies + lease_break_time * HZ;
1409 if (break_time == 0)
1410 break_time++; /* so that 0 means no break time */
1411 }
1412
4d01b7f5
JL
1413 for (before = &inode->i_flock;
1414 ((fl = *before) != NULL) && IS_LEASE(fl);
1415 before = &fl->fl_next) {
df4e8d2c
BF
1416 if (!leases_conflict(fl, new_fl))
1417 continue;
778fc546
BF
1418 if (want_write) {
1419 if (fl->fl_flags & FL_UNLOCK_PENDING)
1420 continue;
1421 fl->fl_flags |= FL_UNLOCK_PENDING;
1da177e4 1422 fl->fl_break_time = break_time;
778fc546 1423 } else {
03d12ddf 1424 if (lease_breaking(inode->i_flock))
778fc546
BF
1425 continue;
1426 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1427 fl->fl_downgrade_time = break_time;
1da177e4 1428 }
4d01b7f5
JL
1429 if (fl->fl_lmops->lm_break(fl))
1430 locks_delete_lock(before, &dispose);
1da177e4
LT
1431 }
1432
4d01b7f5
JL
1433 fl = inode->i_flock;
1434 if (!fl || !IS_LEASE(fl))
1435 goto out;
1436
843c6b2f 1437 if (mode & O_NONBLOCK) {
62af4f1f 1438 trace_break_lease_noblock(inode, new_fl);
1da177e4
LT
1439 error = -EWOULDBLOCK;
1440 goto out;
1441 }
1442
1443restart:
03d12ddf 1444 break_time = inode->i_flock->fl_break_time;
f1c6bb2c 1445 if (break_time != 0)
1da177e4 1446 break_time -= jiffies;
f1c6bb2c
JL
1447 if (break_time == 0)
1448 break_time++;
03d12ddf 1449 locks_insert_block(inode->i_flock, new_fl);
62af4f1f 1450 trace_break_lease_block(inode, new_fl);
1c8c601a 1451 spin_unlock(&inode->i_lock);
c45198ed 1452 locks_dispose_list(&dispose);
4321e01e
MW
1453 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1454 !new_fl->fl_next, break_time);
1c8c601a 1455 spin_lock(&inode->i_lock);
62af4f1f 1456 trace_break_lease_unblock(inode, new_fl);
1c8c601a 1457 locks_delete_block(new_fl);
1da177e4 1458 if (error >= 0) {
778fc546
BF
1459 /*
1460 * Wait for the next conflicting lease that has not been
1461 * broken yet
1462 */
03d12ddf
JL
1463 if (error == 0)
1464 time_out_leases(inode, &dispose);
1465 if (any_leases_conflict(inode, new_fl))
1466 goto restart;
1467
1da177e4
LT
1468 error = 0;
1469 }
1470
1471out:
1c8c601a 1472 spin_unlock(&inode->i_lock);
c45198ed 1473 locks_dispose_list(&dispose);
6d4b9e38 1474 locks_free_lock(new_fl);
1da177e4
LT
1475 return error;
1476}
1477
1478EXPORT_SYMBOL(__break_lease);
1479
1480/**
a6b91919 1481 * lease_get_mtime - get the last modified time of an inode
1da177e4
LT
1482 * @inode: the inode
1483 * @time: pointer to a timespec which will contain the last modified time
1484 *
1485 * This is to force NFS clients to flush their caches for files with
1486 * exclusive leases. The justification is that if someone has an
a6b91919 1487 * exclusive lease, then they could be modifying it.
1da177e4
LT
1488 */
1489void lease_get_mtime(struct inode *inode, struct timespec *time)
1490{
bfe86024
JL
1491 bool has_lease = false;
1492 struct file_lock *flock;
1493
1494 if (inode->i_flock) {
1495 spin_lock(&inode->i_lock);
1496 flock = inode->i_flock;
1497 if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
1498 has_lease = true;
1499 spin_unlock(&inode->i_lock);
1500 }
1501
1502 if (has_lease)
1da177e4
LT
1503 *time = current_fs_time(inode->i_sb);
1504 else
1505 *time = inode->i_mtime;
1506}
1507
1508EXPORT_SYMBOL(lease_get_mtime);
1509
1510/**
1511 * fcntl_getlease - Enquire what lease is currently active
1512 * @filp: the file
1513 *
1514 * The value returned by this function will be one of
1515 * (if no lease break is pending):
1516 *
1517 * %F_RDLCK to indicate a shared lease is held.
1518 *
1519 * %F_WRLCK to indicate an exclusive lease is held.
1520 *
1521 * %F_UNLCK to indicate no lease is held.
1522 *
1523 * (if a lease break is pending):
1524 *
1525 * %F_RDLCK to indicate an exclusive lease needs to be
1526 * changed to a shared lease (or removed).
1527 *
1528 * %F_UNLCK to indicate the lease needs to be removed.
1529 *
1530 * XXX: sfr & willy disagree over whether F_INPROGRESS
1531 * should be returned to userspace.
1532 */
1533int fcntl_getlease(struct file *filp)
1534{
1535 struct file_lock *fl;
1c8c601a 1536 struct inode *inode = file_inode(filp);
1da177e4 1537 int type = F_UNLCK;
c45198ed 1538 LIST_HEAD(dispose);
1da177e4 1539
1c8c601a 1540 spin_lock(&inode->i_lock);
c45198ed 1541 time_out_leases(file_inode(filp), &dispose);
496ad9aa 1542 for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
1da177e4
LT
1543 fl = fl->fl_next) {
1544 if (fl->fl_file == filp) {
778fc546 1545 type = target_leasetype(fl);
1da177e4
LT
1546 break;
1547 }
1548 }
1c8c601a 1549 spin_unlock(&inode->i_lock);
c45198ed 1550 locks_dispose_list(&dispose);
1da177e4
LT
1551 return type;
1552}
1553
24cbe784
JL
1554/**
1555 * check_conflicting_open - see if the given dentry points to a file that has
1556 * an existing open that would conflict with the
1557 * desired lease.
1558 * @dentry: dentry to check
1559 * @arg: type of lease that we're trying to acquire
1560 *
1561 * Check to see if there's an existing open fd on this file that would
1562 * conflict with the lease we're trying to set.
1563 */
1564static int
1565check_conflicting_open(const struct dentry *dentry, const long arg)
1566{
1567 int ret = 0;
1568 struct inode *inode = dentry->d_inode;
1569
1570 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
1571 return -EAGAIN;
1572
1573 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
1574 (atomic_read(&inode->i_count) > 1)))
1575 ret = -EAGAIN;
1576
1577 return ret;
1578}
1579
e6f5c789
JL
1580static int
1581generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
1da177e4 1582{
7eaae282 1583 struct file_lock *fl, **before, **my_before = NULL, *lease;
0f7fc9e4 1584 struct dentry *dentry = filp->f_path.dentry;
1da177e4 1585 struct inode *inode = dentry->d_inode;
df4e8d2c 1586 bool is_deleg = (*flp)->fl_flags & FL_DELEG;
c1f24ef4 1587 int error;
c45198ed 1588 LIST_HEAD(dispose);
1da177e4 1589
096657b6 1590 lease = *flp;
62af4f1f
JL
1591 trace_generic_add_lease(inode, lease);
1592
df4e8d2c
BF
1593 /*
1594 * In the delegation case we need mutual exclusion with
1595 * a number of operations that take the i_mutex. We trylock
1596 * because delegations are an optional optimization, and if
1597 * there's some chance of a conflict--we'd rather not
1598 * bother, maybe that's a sign this just isn't a good file to
1599 * hand out a delegation on.
1600 */
1601 if (is_deleg && !mutex_trylock(&inode->i_mutex))
1602 return -EAGAIN;
1603
1604 if (is_deleg && arg == F_WRLCK) {
1605 /* Write delegations are not currently supported: */
4fdb793f 1606 mutex_unlock(&inode->i_mutex);
df4e8d2c
BF
1607 WARN_ON_ONCE(1);
1608 return -EINVAL;
1609 }
096657b6 1610
f82b4b67 1611 spin_lock(&inode->i_lock);
c45198ed 1612 time_out_leases(inode, &dispose);
24cbe784
JL
1613 error = check_conflicting_open(dentry, arg);
1614 if (error)
096657b6 1615 goto out;
6d5e8b05 1616
1da177e4
LT
1617 /*
1618 * At this point, we know that if there is an exclusive
1619 * lease on this file, then we hold it on this filp
1620 * (otherwise our open of this file would have blocked).
1621 * And if we are trying to acquire an exclusive lease,
1622 * then the file is not open by anyone (including us)
1623 * except for this filp.
1624 */
c1f24ef4 1625 error = -EAGAIN;
1da177e4
LT
1626 for (before = &inode->i_flock;
1627 ((fl = *before) != NULL) && IS_LEASE(fl);
1628 before = &fl->fl_next) {
c1f24ef4 1629 if (fl->fl_file == filp) {
1da177e4 1630 my_before = before;
c1f24ef4
BF
1631 continue;
1632 }
1633 /*
1634 * No exclusive leases if someone else has a lease on
1635 * this file:
1636 */
1637 if (arg == F_WRLCK)
1638 goto out;
1639 /*
1640 * Modifying our existing lease is OK, but no getting a
1641 * new lease if someone else is opening for write:
1642 */
1643 if (fl->fl_flags & FL_UNLOCK_PENDING)
1644 goto out;
1da177e4
LT
1645 }
1646
1da177e4 1647 if (my_before != NULL) {
1c7dd2ff 1648 lease = *my_before;
c45198ed 1649 error = lease->fl_lmops->lm_change(my_before, arg, &dispose);
1c7dd2ff
JL
1650 if (error)
1651 goto out;
1652 goto out_setup;
1da177e4
LT
1653 }
1654
1da177e4
LT
1655 error = -EINVAL;
1656 if (!leases_enable)
1657 goto out;
1658
c5b1f0d9 1659 locks_insert_lock(before, lease);
24cbe784
JL
1660 /*
1661 * The check in break_lease() is lockless. It's possible for another
1662 * open to race in after we did the earlier check for a conflicting
1663 * open but before the lease was inserted. Check again for a
1664 * conflicting open and cancel the lease if there is one.
1665 *
1666 * We also add a barrier here to ensure that the insertion of the lock
1667 * precedes these checks.
1668 */
1669 smp_mb();
1670 error = check_conflicting_open(dentry, arg);
1671 if (error)
e6f5c789 1672 goto out_unlink;
1c7dd2ff
JL
1673
1674out_setup:
1675 if (lease->fl_lmops->lm_setup)
1676 lease->fl_lmops->lm_setup(lease, priv);
1da177e4 1677out:
f82b4b67 1678 spin_unlock(&inode->i_lock);
c45198ed 1679 locks_dispose_list(&dispose);
df4e8d2c
BF
1680 if (is_deleg)
1681 mutex_unlock(&inode->i_mutex);
1c7dd2ff
JL
1682 if (!error && !my_before)
1683 *flp = NULL;
1da177e4 1684 return error;
e6f5c789
JL
1685out_unlink:
1686 locks_unlink_lock(before);
1687 goto out;
1da177e4 1688}
8335ebd9 1689
0efaa7e8 1690static int generic_delete_lease(struct file *filp)
8335ebd9 1691{
0efaa7e8 1692 int error = -EAGAIN;
8335ebd9
BF
1693 struct file_lock *fl, **before;
1694 struct dentry *dentry = filp->f_path.dentry;
1695 struct inode *inode = dentry->d_inode;
c45198ed 1696 LIST_HEAD(dispose);
8335ebd9 1697
f82b4b67 1698 spin_lock(&inode->i_lock);
c45198ed 1699 time_out_leases(inode, &dispose);
8335ebd9
BF
1700 for (before = &inode->i_flock;
1701 ((fl = *before) != NULL) && IS_LEASE(fl);
1702 before = &fl->fl_next) {
0efaa7e8
JL
1703 if (fl->fl_file == filp)
1704 break;
8335ebd9 1705 }
0efaa7e8 1706 trace_generic_delete_lease(inode, fl);
52d304eb 1707 if (fl && IS_LEASE(fl))
c45198ed 1708 error = fl->fl_lmops->lm_change(before, F_UNLCK, &dispose);
f82b4b67 1709 spin_unlock(&inode->i_lock);
c45198ed 1710 locks_dispose_list(&dispose);
0efaa7e8 1711 return error;
8335ebd9
BF
1712}
1713
1714/**
1715 * generic_setlease - sets a lease on an open file
1c7dd2ff
JL
1716 * @filp: file pointer
1717 * @arg: type of lease to obtain
1718 * @flp: input - file_lock to use, output - file_lock inserted
1719 * @priv: private data for lm_setup (may be NULL if lm_setup
1720 * doesn't require it)
8335ebd9
BF
1721 *
1722 * The (input) flp->fl_lmops->lm_break function is required
1723 * by break_lease().
8335ebd9 1724 */
e6f5c789
JL
1725int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
1726 void **priv)
8335ebd9
BF
1727{
1728 struct dentry *dentry = filp->f_path.dentry;
1729 struct inode *inode = dentry->d_inode;
1730 int error;
1731
8e96e3b7 1732 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
8335ebd9
BF
1733 return -EACCES;
1734 if (!S_ISREG(inode->i_mode))
1735 return -EINVAL;
1736 error = security_file_lock(filp, arg);
1737 if (error)
1738 return error;
1739
8335ebd9
BF
1740 switch (arg) {
1741 case F_UNLCK:
0efaa7e8 1742 return generic_delete_lease(filp);
8335ebd9
BF
1743 case F_RDLCK:
1744 case F_WRLCK:
0efaa7e8
JL
1745 if (!(*flp)->fl_lmops->lm_break) {
1746 WARN_ON_ONCE(1);
1747 return -ENOLCK;
1748 }
e6f5c789 1749 return generic_add_lease(filp, arg, flp, priv);
8335ebd9 1750 default:
8d657eb3 1751 return -EINVAL;
8335ebd9
BF
1752 }
1753}
0af1a450 1754EXPORT_SYMBOL(generic_setlease);
1da177e4 1755
b89f4321 1756/**
e51673aa 1757 * vfs_setlease - sets a lease on an open file
1c7dd2ff
JL
1758 * @filp: file pointer
1759 * @arg: type of lease to obtain
1760 * @lease: file_lock to use when adding a lease
1761 * @priv: private info for lm_setup when adding a lease (may be
1762 * NULL if lm_setup doesn't require it)
e51673aa
JL
1763 *
1764 * Call this to establish a lease on the file. The "lease" argument is not
1765 * used for F_UNLCK requests and may be NULL. For commands that set or alter
1766 * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
1767 * if not, this function will return -ENOLCK (and generate a scary-looking
1768 * stack trace).
1c7dd2ff
JL
1769 *
1770 * The "priv" pointer is passed directly to the lm_setup function as-is. It
1771 * may be NULL if the lm_setup operation doesn't require it.
1da177e4 1772 */
e6f5c789
JL
1773int
1774vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
1da177e4 1775{
1c7dd2ff 1776 if (filp->f_op->setlease)
f82b4b67 1777 return filp->f_op->setlease(filp, arg, lease, priv);
1c7dd2ff 1778 else
f82b4b67 1779 return generic_setlease(filp, arg, lease, priv);
1da177e4 1780}
a9933cea 1781EXPORT_SYMBOL_GPL(vfs_setlease);
1da177e4 1782
0ceaf6c7 1783static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
1da177e4 1784{
1c7dd2ff 1785 struct file_lock *fl;
f7347ce4 1786 struct fasync_struct *new;
1da177e4
LT
1787 int error;
1788
c5b1f0d9
AB
1789 fl = lease_alloc(filp, arg);
1790 if (IS_ERR(fl))
1791 return PTR_ERR(fl);
1da177e4 1792
f7347ce4
LT
1793 new = fasync_alloc();
1794 if (!new) {
1795 locks_free_lock(fl);
1796 return -ENOMEM;
1797 }
1c7dd2ff 1798 new->fa_fd = fd;
f7347ce4 1799
1c7dd2ff 1800 error = vfs_setlease(filp, arg, &fl, (void **)&new);
2dfb928f
JL
1801 if (fl)
1802 locks_free_lock(fl);
f7347ce4
LT
1803 if (new)
1804 fasync_free(new);
1da177e4
LT
1805 return error;
1806}
1807
0ceaf6c7
BF
1808/**
1809 * fcntl_setlease - sets a lease on an open file
1810 * @fd: open file descriptor
1811 * @filp: file pointer
1812 * @arg: type of lease to obtain
1813 *
1814 * Call this fcntl to establish a lease on the file.
1815 * Note that you also need to call %F_SETSIG to
1816 * receive a signal when the lease is broken.
1817 */
1818int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1819{
1820 if (arg == F_UNLCK)
e6f5c789 1821 return vfs_setlease(filp, F_UNLCK, NULL, NULL);
0ceaf6c7
BF
1822 return do_fcntl_add_lease(fd, filp, arg);
1823}
1824
1da177e4
LT
1825/**
1826 * flock_lock_file_wait - Apply a FLOCK-style lock to a file
1827 * @filp: The file to apply the lock to
1828 * @fl: The lock to be applied
1829 *
1830 * Add a FLOCK style lock to a file.
1831 */
1832int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
1833{
1834 int error;
1835 might_sleep();
1836 for (;;) {
1837 error = flock_lock_file(filp, fl);
bde74e4b 1838 if (error != FILE_LOCK_DEFERRED)
1da177e4
LT
1839 break;
1840 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1841 if (!error)
1842 continue;
1843
1844 locks_delete_block(fl);
1845 break;
1846 }
1847 return error;
1848}
1849
1850EXPORT_SYMBOL(flock_lock_file_wait);
1851
1852/**
1853 * sys_flock: - flock() system call.
1854 * @fd: the file descriptor to lock.
1855 * @cmd: the type of lock to apply.
1856 *
1857 * Apply a %FL_FLOCK style lock to an open file descriptor.
1858 * The @cmd can be one of
1859 *
1860 * %LOCK_SH -- a shared lock.
1861 *
1862 * %LOCK_EX -- an exclusive lock.
1863 *
1864 * %LOCK_UN -- remove an existing lock.
1865 *
1866 * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
1867 *
1868 * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
1869 * processes read and write access respectively.
1870 */
002c8976 1871SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
1da177e4 1872{
2903ff01 1873 struct fd f = fdget(fd);
1da177e4
LT
1874 struct file_lock *lock;
1875 int can_sleep, unlock;
1876 int error;
1877
1878 error = -EBADF;
2903ff01 1879 if (!f.file)
1da177e4
LT
1880 goto out;
1881
1882 can_sleep = !(cmd & LOCK_NB);
1883 cmd &= ~LOCK_NB;
1884 unlock = (cmd == LOCK_UN);
1885
aeb5d727 1886 if (!unlock && !(cmd & LOCK_MAND) &&
2903ff01 1887 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
1da177e4
LT
1888 goto out_putf;
1889
6e129d00
JL
1890 lock = flock_make_lock(f.file, cmd);
1891 if (IS_ERR(lock)) {
1892 error = PTR_ERR(lock);
1da177e4 1893 goto out_putf;
6e129d00
JL
1894 }
1895
1da177e4
LT
1896 if (can_sleep)
1897 lock->fl_flags |= FL_SLEEP;
1898
2903ff01 1899 error = security_file_lock(f.file, lock->fl_type);
1da177e4
LT
1900 if (error)
1901 goto out_free;
1902
72c2d531 1903 if (f.file->f_op->flock)
2903ff01 1904 error = f.file->f_op->flock(f.file,
1da177e4
LT
1905 (can_sleep) ? F_SETLKW : F_SETLK,
1906 lock);
1907 else
2903ff01 1908 error = flock_lock_file_wait(f.file, lock);
1da177e4
LT
1909
1910 out_free:
993dfa87 1911 locks_free_lock(lock);
1da177e4
LT
1912
1913 out_putf:
2903ff01 1914 fdput(f);
1da177e4
LT
1915 out:
1916 return error;
1917}
1918
3ee17abd
BF
1919/**
1920 * vfs_test_lock - test file byte range lock
1921 * @filp: The file to test lock for
6924c554 1922 * @fl: The lock to test; also used to hold result
3ee17abd
BF
1923 *
1924 * Returns -ERRNO on failure. Indicates presence of conflicting lock by
1925 * setting conf->fl_type to something other than F_UNLCK.
1926 */
1927int vfs_test_lock(struct file *filp, struct file_lock *fl)
1928{
72c2d531 1929 if (filp->f_op->lock)
3ee17abd
BF
1930 return filp->f_op->lock(filp, F_GETLK, fl);
1931 posix_test_lock(filp, fl);
1932 return 0;
1933}
1934EXPORT_SYMBOL_GPL(vfs_test_lock);
1935
c2fa1b8a
BF
1936static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1937{
cff2fce5 1938 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
c2fa1b8a
BF
1939#if BITS_PER_LONG == 32
1940 /*
1941 * Make sure we can represent the posix lock via
1942 * legacy 32bit flock.
1943 */
1944 if (fl->fl_start > OFFT_OFFSET_MAX)
1945 return -EOVERFLOW;
1946 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1947 return -EOVERFLOW;
1948#endif
1949 flock->l_start = fl->fl_start;
1950 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1951 fl->fl_end - fl->fl_start + 1;
1952 flock->l_whence = 0;
129a84de 1953 flock->l_type = fl->fl_type;
c2fa1b8a
BF
1954 return 0;
1955}
1956
1957#if BITS_PER_LONG == 32
1958static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1959{
cff2fce5 1960 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
c2fa1b8a
BF
1961 flock->l_start = fl->fl_start;
1962 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1963 fl->fl_end - fl->fl_start + 1;
1964 flock->l_whence = 0;
1965 flock->l_type = fl->fl_type;
1966}
1967#endif
1968
1da177e4
LT
1969/* Report the first existing lock that would conflict with l.
1970 * This implements the F_GETLK command of fcntl().
1971 */
c1e62b8f 1972int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
1da177e4 1973{
9d6a8c5c 1974 struct file_lock file_lock;
1da177e4
LT
1975 struct flock flock;
1976 int error;
1977
1978 error = -EFAULT;
1979 if (copy_from_user(&flock, l, sizeof(flock)))
1980 goto out;
1981 error = -EINVAL;
1982 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1983 goto out;
1984
1985 error = flock_to_posix_lock(filp, &file_lock, &flock);
1986 if (error)
1987 goto out;
1988
0d3f7a2d 1989 if (cmd == F_OFD_GETLK) {
90478939
JL
1990 error = -EINVAL;
1991 if (flock.l_pid != 0)
1992 goto out;
1993
5d50ffd7 1994 cmd = F_GETLK;
cff2fce5 1995 file_lock.fl_flags |= FL_OFDLCK;
73a8f5f7 1996 file_lock.fl_owner = filp;
5d50ffd7
JL
1997 }
1998
3ee17abd
BF
1999 error = vfs_test_lock(filp, &file_lock);
2000 if (error)
2001 goto out;
1da177e4 2002
9d6a8c5c
ME
2003 flock.l_type = file_lock.fl_type;
2004 if (file_lock.fl_type != F_UNLCK) {
2005 error = posix_lock_to_flock(&flock, &file_lock);
c2fa1b8a 2006 if (error)
f328296e 2007 goto rel_priv;
1da177e4
LT
2008 }
2009 error = -EFAULT;
2010 if (!copy_to_user(l, &flock, sizeof(flock)))
2011 error = 0;
f328296e
KM
2012rel_priv:
2013 locks_release_private(&file_lock);
1da177e4
LT
2014out:
2015 return error;
2016}
2017
7723ec97
ME
2018/**
2019 * vfs_lock_file - file byte range lock
2020 * @filp: The file to apply the lock to
2021 * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
2022 * @fl: The lock to be applied
150b3934
ME
2023 * @conf: Place to return a copy of the conflicting lock, if found.
2024 *
2025 * A caller that doesn't care about the conflicting lock may pass NULL
2026 * as the final argument.
2027 *
2028 * If the filesystem defines a private ->lock() method, then @conf will
2029 * be left unchanged; so a caller that cares should initialize it to
2030 * some acceptable default.
2beb6614
ME
2031 *
2032 * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
2033 * locks, the ->lock() interface may return asynchronously, before the lock has
2034 * been granted or denied by the underlying filesystem, if (and only if)
8fb47a4f 2035 * lm_grant is set. Callers expecting ->lock() to return asynchronously
2beb6614
ME
2036 * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
2037 * the request is for a blocking lock. When ->lock() does return asynchronously,
8fb47a4f 2038 * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
2beb6614
ME
2039 * request completes.
2040 * If the request is for non-blocking lock the file system should return
bde74e4b
MS
2041 * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
2042 * with the result. If the request timed out the callback routine will return a
2beb6614
ME
2043 * nonzero return code and the file system should release the lock. The file
2044 * system is also responsible to keep a corresponding posix lock when it
2045 * grants a lock so the VFS can find out which locks are locally held and do
2046 * the correct lock cleanup when required.
2047 * The underlying filesystem must not drop the kernel lock or call
8fb47a4f 2048 * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
2beb6614 2049 * return code.
7723ec97 2050 */
150b3934 2051int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
7723ec97 2052{
72c2d531 2053 if (filp->f_op->lock)
7723ec97
ME
2054 return filp->f_op->lock(filp, cmd, fl);
2055 else
150b3934 2056 return posix_lock_file(filp, fl, conf);
7723ec97
ME
2057}
2058EXPORT_SYMBOL_GPL(vfs_lock_file);
2059
b648a6de
MS
2060static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2061 struct file_lock *fl)
2062{
2063 int error;
2064
2065 error = security_file_lock(filp, fl->fl_type);
2066 if (error)
2067 return error;
2068
764c76b3
MS
2069 for (;;) {
2070 error = vfs_lock_file(filp, cmd, fl, NULL);
2071 if (error != FILE_LOCK_DEFERRED)
b648a6de 2072 break;
764c76b3
MS
2073 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2074 if (!error)
2075 continue;
2076
2077 locks_delete_block(fl);
2078 break;
b648a6de
MS
2079 }
2080
2081 return error;
2082}
2083
cf01f4ee
JL
2084/* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
2085static int
2086check_fmode_for_setlk(struct file_lock *fl)
2087{
2088 switch (fl->fl_type) {
2089 case F_RDLCK:
2090 if (!(fl->fl_file->f_mode & FMODE_READ))
2091 return -EBADF;
2092 break;
2093 case F_WRLCK:
2094 if (!(fl->fl_file->f_mode & FMODE_WRITE))
2095 return -EBADF;
2096 }
2097 return 0;
2098}
2099
1da177e4
LT
2100/* Apply the lock described by l to an open file descriptor.
2101 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2102 */
c293621b
PS
2103int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2104 struct flock __user *l)
1da177e4
LT
2105{
2106 struct file_lock *file_lock = locks_alloc_lock();
2107 struct flock flock;
2108 struct inode *inode;
0b2bac2f 2109 struct file *f;
1da177e4
LT
2110 int error;
2111
2112 if (file_lock == NULL)
2113 return -ENOLCK;
2114
2115 /*
2116 * This might block, so we do it before checking the inode.
2117 */
2118 error = -EFAULT;
2119 if (copy_from_user(&flock, l, sizeof(flock)))
2120 goto out;
2121
496ad9aa 2122 inode = file_inode(filp);
1da177e4
LT
2123
2124 /* Don't allow mandatory locks on files that may be memory mapped
2125 * and shared.
2126 */
a16877ca 2127 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
1da177e4
LT
2128 error = -EAGAIN;
2129 goto out;
2130 }
2131
c293621b 2132again:
1da177e4
LT
2133 error = flock_to_posix_lock(filp, file_lock, &flock);
2134 if (error)
2135 goto out;
5d50ffd7 2136
cf01f4ee
JL
2137 error = check_fmode_for_setlk(file_lock);
2138 if (error)
2139 goto out;
2140
5d50ffd7
JL
2141 /*
2142 * If the cmd is requesting file-private locks, then set the
cff2fce5 2143 * FL_OFDLCK flag and override the owner.
5d50ffd7
JL
2144 */
2145 switch (cmd) {
0d3f7a2d 2146 case F_OFD_SETLK:
90478939
JL
2147 error = -EINVAL;
2148 if (flock.l_pid != 0)
2149 goto out;
2150
5d50ffd7 2151 cmd = F_SETLK;
cff2fce5 2152 file_lock->fl_flags |= FL_OFDLCK;
73a8f5f7 2153 file_lock->fl_owner = filp;
5d50ffd7 2154 break;
0d3f7a2d 2155 case F_OFD_SETLKW:
90478939
JL
2156 error = -EINVAL;
2157 if (flock.l_pid != 0)
2158 goto out;
2159
5d50ffd7 2160 cmd = F_SETLKW;
cff2fce5 2161 file_lock->fl_flags |= FL_OFDLCK;
73a8f5f7 2162 file_lock->fl_owner = filp;
5d50ffd7
JL
2163 /* Fallthrough */
2164 case F_SETLKW:
1da177e4
LT
2165 file_lock->fl_flags |= FL_SLEEP;
2166 }
5d50ffd7 2167
b648a6de 2168 error = do_lock_file_wait(filp, cmd, file_lock);
1da177e4 2169
c293621b
PS
2170 /*
2171 * Attempt to detect a close/fcntl race and recover by
2172 * releasing the lock that was just acquired.
2173 */
0b2bac2f
AV
2174 /*
2175 * we need that spin_lock here - it prevents reordering between
2176 * update of inode->i_flock and check for it done in close().
2177 * rcu_read_lock() wouldn't do.
2178 */
2179 spin_lock(&current->files->file_lock);
2180 f = fcheck(fd);
2181 spin_unlock(&current->files->file_lock);
2182 if (!error && f != filp && flock.l_type != F_UNLCK) {
c293621b
PS
2183 flock.l_type = F_UNLCK;
2184 goto again;
1da177e4
LT
2185 }
2186
c293621b 2187out:
1da177e4
LT
2188 locks_free_lock(file_lock);
2189 return error;
2190}
2191
2192#if BITS_PER_LONG == 32
2193/* Report the first existing lock that would conflict with l.
2194 * This implements the F_GETLK command of fcntl().
2195 */
c1e62b8f 2196int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
1da177e4 2197{
9d6a8c5c 2198 struct file_lock file_lock;
1da177e4
LT
2199 struct flock64 flock;
2200 int error;
2201
2202 error = -EFAULT;
2203 if (copy_from_user(&flock, l, sizeof(flock)))
2204 goto out;
2205 error = -EINVAL;
2206 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
2207 goto out;
2208
2209 error = flock64_to_posix_lock(filp, &file_lock, &flock);
2210 if (error)
2211 goto out;
2212
0d3f7a2d 2213 if (cmd == F_OFD_GETLK) {
90478939
JL
2214 error = -EINVAL;
2215 if (flock.l_pid != 0)
2216 goto out;
2217
5d50ffd7 2218 cmd = F_GETLK64;
cff2fce5 2219 file_lock.fl_flags |= FL_OFDLCK;
73a8f5f7 2220 file_lock.fl_owner = filp;
5d50ffd7
JL
2221 }
2222
3ee17abd
BF
2223 error = vfs_test_lock(filp, &file_lock);
2224 if (error)
2225 goto out;
2226
9d6a8c5c
ME
2227 flock.l_type = file_lock.fl_type;
2228 if (file_lock.fl_type != F_UNLCK)
2229 posix_lock_to_flock64(&flock, &file_lock);
2230
1da177e4
LT
2231 error = -EFAULT;
2232 if (!copy_to_user(l, &flock, sizeof(flock)))
2233 error = 0;
f328296e
KM
2234
2235 locks_release_private(&file_lock);
1da177e4
LT
2236out:
2237 return error;
2238}
2239
2240/* Apply the lock described by l to an open file descriptor.
2241 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
2242 */
c293621b
PS
2243int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2244 struct flock64 __user *l)
1da177e4
LT
2245{
2246 struct file_lock *file_lock = locks_alloc_lock();
2247 struct flock64 flock;
2248 struct inode *inode;
0b2bac2f 2249 struct file *f;
1da177e4
LT
2250 int error;
2251
2252 if (file_lock == NULL)
2253 return -ENOLCK;
2254
2255 /*
2256 * This might block, so we do it before checking the inode.
2257 */
2258 error = -EFAULT;
2259 if (copy_from_user(&flock, l, sizeof(flock)))
2260 goto out;
2261
496ad9aa 2262 inode = file_inode(filp);
1da177e4
LT
2263
2264 /* Don't allow mandatory locks on files that may be memory mapped
2265 * and shared.
2266 */
a16877ca 2267 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
1da177e4
LT
2268 error = -EAGAIN;
2269 goto out;
2270 }
2271
c293621b 2272again:
1da177e4
LT
2273 error = flock64_to_posix_lock(filp, file_lock, &flock);
2274 if (error)
2275 goto out;
5d50ffd7 2276
cf01f4ee
JL
2277 error = check_fmode_for_setlk(file_lock);
2278 if (error)
2279 goto out;
2280
5d50ffd7
JL
2281 /*
2282 * If the cmd is requesting file-private locks, then set the
cff2fce5 2283 * FL_OFDLCK flag and override the owner.
5d50ffd7
JL
2284 */
2285 switch (cmd) {
0d3f7a2d 2286 case F_OFD_SETLK:
90478939
JL
2287 error = -EINVAL;
2288 if (flock.l_pid != 0)
2289 goto out;
2290
5d50ffd7 2291 cmd = F_SETLK64;
cff2fce5 2292 file_lock->fl_flags |= FL_OFDLCK;
73a8f5f7 2293 file_lock->fl_owner = filp;
5d50ffd7 2294 break;
0d3f7a2d 2295 case F_OFD_SETLKW:
90478939
JL
2296 error = -EINVAL;
2297 if (flock.l_pid != 0)
2298 goto out;
2299
5d50ffd7 2300 cmd = F_SETLKW64;
cff2fce5 2301 file_lock->fl_flags |= FL_OFDLCK;
73a8f5f7 2302 file_lock->fl_owner = filp;
5d50ffd7
JL
2303 /* Fallthrough */
2304 case F_SETLKW64:
1da177e4
LT
2305 file_lock->fl_flags |= FL_SLEEP;
2306 }
5d50ffd7 2307
b648a6de 2308 error = do_lock_file_wait(filp, cmd, file_lock);
1da177e4 2309
c293621b
PS
2310 /*
2311 * Attempt to detect a close/fcntl race and recover by
2312 * releasing the lock that was just acquired.
2313 */
0b2bac2f
AV
2314 spin_lock(&current->files->file_lock);
2315 f = fcheck(fd);
2316 spin_unlock(&current->files->file_lock);
2317 if (!error && f != filp && flock.l_type != F_UNLCK) {
c293621b
PS
2318 flock.l_type = F_UNLCK;
2319 goto again;
1da177e4
LT
2320 }
2321
2322out:
2323 locks_free_lock(file_lock);
2324 return error;
2325}
2326#endif /* BITS_PER_LONG == 32 */
2327
2328/*
2329 * This function is called when the file is being removed
2330 * from the task's fd array. POSIX locks belonging to this task
2331 * are deleted at this time.
2332 */
2333void locks_remove_posix(struct file *filp, fl_owner_t owner)
2334{
ff7b86b8 2335 struct file_lock lock;
1da177e4
LT
2336
2337 /*
2338 * If there are no locks held on this file, we don't need to call
2339 * posix_lock_file(). Another process could be setting a lock on this
2340 * file at the same time, but we wouldn't remove that lock anyway.
2341 */
496ad9aa 2342 if (!file_inode(filp)->i_flock)
1da177e4
LT
2343 return;
2344
2345 lock.fl_type = F_UNLCK;
75e1fcc0 2346 lock.fl_flags = FL_POSIX | FL_CLOSE;
1da177e4
LT
2347 lock.fl_start = 0;
2348 lock.fl_end = OFFSET_MAX;
2349 lock.fl_owner = owner;
2350 lock.fl_pid = current->tgid;
2351 lock.fl_file = filp;
2352 lock.fl_ops = NULL;
2353 lock.fl_lmops = NULL;
2354
150b3934 2355 vfs_lock_file(filp, F_SETLK, &lock, NULL);
1da177e4 2356
1da177e4
LT
2357 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2358 lock.fl_ops->fl_release_private(&lock);
2359}
2360
2361EXPORT_SYMBOL(locks_remove_posix);
2362
dd459bb1
JL
2363static void
2364locks_remove_flock(struct file *filp)
2365{
2366 struct file_lock fl = {
2367 .fl_owner = filp,
2368 .fl_pid = current->tgid,
2369 .fl_file = filp,
2370 .fl_flags = FL_FLOCK,
2371 .fl_type = F_UNLCK,
2372 .fl_end = OFFSET_MAX,
2373 };
2374
2375 if (!file_inode(filp)->i_flock)
2376 return;
2377
2378 if (filp->f_op->flock)
2379 filp->f_op->flock(filp, F_SETLKW, &fl);
2380 else
2381 flock_lock_file(filp, &fl);
2382
2383 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2384 fl.fl_ops->fl_release_private(&fl);
2385}
2386
1da177e4
LT
2387/*
2388 * This function is called on the last close of an open file.
2389 */
78ed8a13 2390void locks_remove_file(struct file *filp)
1da177e4 2391{
496ad9aa 2392 struct inode * inode = file_inode(filp);
1da177e4
LT
2393 struct file_lock *fl;
2394 struct file_lock **before;
ed9814d8 2395 LIST_HEAD(dispose);
1da177e4 2396
dd459bb1 2397 /* remove any OFD locks */
73a8f5f7 2398 locks_remove_posix(filp, filp);
5d50ffd7 2399
dd459bb1
JL
2400 /* remove flock locks */
2401 locks_remove_flock(filp);
2402
2403 if (!inode->i_flock)
2404 return;
1da177e4 2405
1c8c601a 2406 spin_lock(&inode->i_lock);
1da177e4
LT
2407 before = &inode->i_flock;
2408
2409 while ((fl = *before) != NULL) {
2410 if (fl->fl_file == filp) {
1da177e4 2411 if (IS_LEASE(fl)) {
c45198ed 2412 lease_modify(before, F_UNLCK, &dispose);
1da177e4
LT
2413 continue;
2414 }
8c3cac5e
JL
2415
2416 /*
2417 * There's a leftover lock on the list of a type that
2418 * we didn't expect to see. Most likely a classic
2419 * POSIX lock that ended up not getting released
2420 * properly, or that raced onto the list somehow. Log
2421 * some info about it and then just remove it from
2422 * the list.
2423 */
dd459bb1 2424 WARN(1, "leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n",
8c3cac5e
JL
2425 MAJOR(inode->i_sb->s_dev),
2426 MINOR(inode->i_sb->s_dev), inode->i_ino,
2427 fl->fl_type, fl->fl_flags,
2428 fl->fl_start, fl->fl_end);
2429
ed9814d8 2430 locks_delete_lock(before, &dispose);
8c3cac5e 2431 continue;
1da177e4
LT
2432 }
2433 before = &fl->fl_next;
2434 }
1c8c601a 2435 spin_unlock(&inode->i_lock);
ed9814d8 2436 locks_dispose_list(&dispose);
1da177e4
LT
2437}
2438
1da177e4
LT
2439/**
2440 * posix_unblock_lock - stop waiting for a file lock
1da177e4
LT
2441 * @waiter: the lock which was waiting
2442 *
2443 * lockd needs to block waiting for locks.
2444 */
64a318ee 2445int
f891a29f 2446posix_unblock_lock(struct file_lock *waiter)
1da177e4 2447{
64a318ee
BF
2448 int status = 0;
2449
7b2296af 2450 spin_lock(&blocked_lock_lock);
5996a298 2451 if (waiter->fl_next)
1da177e4 2452 __locks_delete_block(waiter);
64a318ee
BF
2453 else
2454 status = -ENOENT;
7b2296af 2455 spin_unlock(&blocked_lock_lock);
64a318ee 2456 return status;
1da177e4 2457}
1da177e4
LT
2458EXPORT_SYMBOL(posix_unblock_lock);
2459
9b9d2ab4
ME
2460/**
2461 * vfs_cancel_lock - file byte range unblock lock
2462 * @filp: The file to apply the unblock to
2463 * @fl: The lock to be unblocked
2464 *
2465 * Used by lock managers to cancel blocked requests
2466 */
2467int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2468{
72c2d531 2469 if (filp->f_op->lock)
9b9d2ab4
ME
2470 return filp->f_op->lock(filp, F_CANCELLK, fl);
2471 return 0;
2472}
2473
2474EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2475
7f8ada98 2476#ifdef CONFIG_PROC_FS
d8ba7a36 2477#include <linux/proc_fs.h>
7f8ada98
PE
2478#include <linux/seq_file.h>
2479
7012b02a
JL
2480struct locks_iterator {
2481 int li_cpu;
2482 loff_t li_pos;
2483};
2484
7f8ada98 2485static void lock_get_status(struct seq_file *f, struct file_lock *fl,
99dc8292 2486 loff_t id, char *pfx)
1da177e4
LT
2487{
2488 struct inode *inode = NULL;
ab1f1611
VG
2489 unsigned int fl_pid;
2490
2491 if (fl->fl_nspid)
6c5f3e7b 2492 fl_pid = pid_vnr(fl->fl_nspid);
ab1f1611
VG
2493 else
2494 fl_pid = fl->fl_pid;
1da177e4
LT
2495
2496 if (fl->fl_file != NULL)
496ad9aa 2497 inode = file_inode(fl->fl_file);
1da177e4 2498
99dc8292 2499 seq_printf(f, "%lld:%s ", id, pfx);
1da177e4 2500 if (IS_POSIX(fl)) {
c918d42a 2501 if (fl->fl_flags & FL_ACCESS)
5315c26a 2502 seq_puts(f, "ACCESS");
cff2fce5 2503 else if (IS_OFDLCK(fl))
5315c26a 2504 seq_puts(f, "OFDLCK");
c918d42a 2505 else
5315c26a 2506 seq_puts(f, "POSIX ");
c918d42a
JL
2507
2508 seq_printf(f, " %s ",
1da177e4 2509 (inode == NULL) ? "*NOINODE*" :
a16877ca 2510 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
1da177e4
LT
2511 } else if (IS_FLOCK(fl)) {
2512 if (fl->fl_type & LOCK_MAND) {
5315c26a 2513 seq_puts(f, "FLOCK MSNFS ");
1da177e4 2514 } else {
5315c26a 2515 seq_puts(f, "FLOCK ADVISORY ");
1da177e4
LT
2516 }
2517 } else if (IS_LEASE(fl)) {
8144f1f6
JL
2518 if (fl->fl_flags & FL_DELEG)
2519 seq_puts(f, "DELEG ");
2520 else
2521 seq_puts(f, "LEASE ");
2522
ab83fa4b 2523 if (lease_breaking(fl))
5315c26a 2524 seq_puts(f, "BREAKING ");
1da177e4 2525 else if (fl->fl_file)
5315c26a 2526 seq_puts(f, "ACTIVE ");
1da177e4 2527 else
5315c26a 2528 seq_puts(f, "BREAKER ");
1da177e4 2529 } else {
5315c26a 2530 seq_puts(f, "UNKNOWN UNKNOWN ");
1da177e4
LT
2531 }
2532 if (fl->fl_type & LOCK_MAND) {
7f8ada98 2533 seq_printf(f, "%s ",
1da177e4
LT
2534 (fl->fl_type & LOCK_READ)
2535 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2536 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2537 } else {
7f8ada98 2538 seq_printf(f, "%s ",
ab83fa4b 2539 (lease_breaking(fl))
0ee5c6d6
JL
2540 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2541 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
1da177e4
LT
2542 }
2543 if (inode) {
2544#ifdef WE_CAN_BREAK_LSLK_NOW
ab1f1611 2545 seq_printf(f, "%d %s:%ld ", fl_pid,
1da177e4
LT
2546 inode->i_sb->s_id, inode->i_ino);
2547#else
2548 /* userspace relies on this representation of dev_t ;-( */
ab1f1611 2549 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
1da177e4
LT
2550 MAJOR(inode->i_sb->s_dev),
2551 MINOR(inode->i_sb->s_dev), inode->i_ino);
2552#endif
2553 } else {
ab1f1611 2554 seq_printf(f, "%d <none>:0 ", fl_pid);
1da177e4
LT
2555 }
2556 if (IS_POSIX(fl)) {
2557 if (fl->fl_end == OFFSET_MAX)
7f8ada98 2558 seq_printf(f, "%Ld EOF\n", fl->fl_start);
1da177e4 2559 else
7f8ada98 2560 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
1da177e4 2561 } else {
5315c26a 2562 seq_puts(f, "0 EOF\n");
1da177e4
LT
2563 }
2564}
2565
7f8ada98 2566static int locks_show(struct seq_file *f, void *v)
1da177e4 2567{
7012b02a 2568 struct locks_iterator *iter = f->private;
7f8ada98 2569 struct file_lock *fl, *bfl;
1da177e4 2570
139ca04e 2571 fl = hlist_entry(v, struct file_lock, fl_link);
1da177e4 2572
7012b02a 2573 lock_get_status(f, fl, iter->li_pos, "");
1da177e4 2574
7f8ada98 2575 list_for_each_entry(bfl, &fl->fl_block, fl_block)
7012b02a 2576 lock_get_status(f, bfl, iter->li_pos, " ->");
094f2825 2577
7f8ada98
PE
2578 return 0;
2579}
1da177e4 2580
7f8ada98 2581static void *locks_start(struct seq_file *f, loff_t *pos)
b03dfdec 2582 __acquires(&blocked_lock_lock)
7f8ada98 2583{
7012b02a 2584 struct locks_iterator *iter = f->private;
99dc8292 2585
7012b02a
JL
2586 iter->li_pos = *pos + 1;
2587 lg_global_lock(&file_lock_lglock);
7b2296af 2588 spin_lock(&blocked_lock_lock);
7012b02a 2589 return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
7f8ada98 2590}
1da177e4 2591
7f8ada98
PE
2592static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2593{
7012b02a
JL
2594 struct locks_iterator *iter = f->private;
2595
2596 ++iter->li_pos;
2597 return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
7f8ada98 2598}
1da177e4 2599
7f8ada98 2600static void locks_stop(struct seq_file *f, void *v)
b03dfdec 2601 __releases(&blocked_lock_lock)
7f8ada98 2602{
7b2296af 2603 spin_unlock(&blocked_lock_lock);
7012b02a 2604 lg_global_unlock(&file_lock_lglock);
1da177e4
LT
2605}
2606
d8ba7a36 2607static const struct seq_operations locks_seq_operations = {
7f8ada98
PE
2608 .start = locks_start,
2609 .next = locks_next,
2610 .stop = locks_stop,
2611 .show = locks_show,
2612};
d8ba7a36
AD
2613
2614static int locks_open(struct inode *inode, struct file *filp)
2615{
7012b02a
JL
2616 return seq_open_private(filp, &locks_seq_operations,
2617 sizeof(struct locks_iterator));
d8ba7a36
AD
2618}
2619
2620static const struct file_operations proc_locks_operations = {
2621 .open = locks_open,
2622 .read = seq_read,
2623 .llseek = seq_lseek,
99dc8292 2624 .release = seq_release_private,
d8ba7a36
AD
2625};
2626
2627static int __init proc_locks_init(void)
2628{
2629 proc_create("locks", 0, NULL, &proc_locks_operations);
2630 return 0;
2631}
2632module_init(proc_locks_init);
7f8ada98
PE
2633#endif
2634
1da177e4
LT
2635static int __init filelock_init(void)
2636{
7012b02a
JL
2637 int i;
2638
1da177e4 2639 filelock_cache = kmem_cache_create("file_lock_cache",
ee19cc40
MS
2640 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2641
7012b02a
JL
2642 lg_lock_init(&file_lock_lglock, "file_lock_lglock");
2643
2644 for_each_possible_cpu(i)
2645 INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
2646
1da177e4
LT
2647 return 0;
2648}
2649
2650core_initcall(filelock_init);