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