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