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