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