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