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