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