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. | |
63 | * See 'Documentation/mandatory.txt' for details. | |
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> | |
119 | #include <linux/fs.h> | |
120 | #include <linux/init.h> | |
121 | #include <linux/module.h> | |
122 | #include <linux/security.h> | |
123 | #include <linux/slab.h> | |
124 | #include <linux/smp_lock.h> | |
125 | #include <linux/syscalls.h> | |
126 | #include <linux/time.h> | |
4fb3a538 | 127 | #include <linux/rcupdate.h> |
1da177e4 LT |
128 | |
129 | #include <asm/semaphore.h> | |
130 | #include <asm/uaccess.h> | |
131 | ||
132 | #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX) | |
133 | #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK) | |
134 | #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE) | |
135 | ||
136 | int leases_enable = 1; | |
137 | int lease_break_time = 45; | |
138 | ||
139 | #define for_each_lock(inode, lockp) \ | |
140 | for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next) | |
141 | ||
26bcbf96 | 142 | static LIST_HEAD(file_lock_list); |
1da177e4 LT |
143 | static LIST_HEAD(blocked_list); |
144 | ||
fa3536cc | 145 | static kmem_cache_t *filelock_cache __read_mostly; |
1da177e4 LT |
146 | |
147 | /* Allocate an empty lock structure. */ | |
148 | static struct file_lock *locks_alloc_lock(void) | |
149 | { | |
150 | return kmem_cache_alloc(filelock_cache, SLAB_KERNEL); | |
151 | } | |
152 | ||
47831f35 TM |
153 | static void locks_release_private(struct file_lock *fl) |
154 | { | |
155 | if (fl->fl_ops) { | |
156 | if (fl->fl_ops->fl_release_private) | |
157 | fl->fl_ops->fl_release_private(fl); | |
158 | fl->fl_ops = NULL; | |
159 | } | |
160 | if (fl->fl_lmops) { | |
161 | if (fl->fl_lmops->fl_release_private) | |
162 | fl->fl_lmops->fl_release_private(fl); | |
163 | fl->fl_lmops = NULL; | |
164 | } | |
165 | ||
166 | } | |
167 | ||
1da177e4 | 168 | /* Free a lock which is not in use. */ |
33443c42 | 169 | static void locks_free_lock(struct file_lock *fl) |
1da177e4 | 170 | { |
5ce29646 MS |
171 | BUG_ON(waitqueue_active(&fl->fl_wait)); |
172 | BUG_ON(!list_empty(&fl->fl_block)); | |
173 | BUG_ON(!list_empty(&fl->fl_link)); | |
1da177e4 | 174 | |
47831f35 | 175 | locks_release_private(fl); |
1da177e4 LT |
176 | kmem_cache_free(filelock_cache, fl); |
177 | } | |
178 | ||
179 | void locks_init_lock(struct file_lock *fl) | |
180 | { | |
181 | INIT_LIST_HEAD(&fl->fl_link); | |
182 | INIT_LIST_HEAD(&fl->fl_block); | |
183 | init_waitqueue_head(&fl->fl_wait); | |
184 | fl->fl_next = NULL; | |
185 | fl->fl_fasync = NULL; | |
186 | fl->fl_owner = NULL; | |
187 | fl->fl_pid = 0; | |
188 | fl->fl_file = NULL; | |
189 | fl->fl_flags = 0; | |
190 | fl->fl_type = 0; | |
191 | fl->fl_start = fl->fl_end = 0; | |
192 | fl->fl_ops = NULL; | |
193 | fl->fl_lmops = NULL; | |
194 | } | |
195 | ||
196 | EXPORT_SYMBOL(locks_init_lock); | |
197 | ||
198 | /* | |
199 | * Initialises the fields of the file lock which are invariant for | |
200 | * free file_locks. | |
201 | */ | |
202 | static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags) | |
203 | { | |
204 | struct file_lock *lock = (struct file_lock *) foo; | |
205 | ||
206 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) != | |
207 | SLAB_CTOR_CONSTRUCTOR) | |
208 | return; | |
209 | ||
210 | locks_init_lock(lock); | |
211 | } | |
212 | ||
47831f35 TM |
213 | static void locks_copy_private(struct file_lock *new, struct file_lock *fl) |
214 | { | |
215 | if (fl->fl_ops) { | |
216 | if (fl->fl_ops->fl_copy_lock) | |
217 | fl->fl_ops->fl_copy_lock(new, fl); | |
218 | new->fl_ops = fl->fl_ops; | |
219 | } | |
220 | if (fl->fl_lmops) { | |
221 | if (fl->fl_lmops->fl_copy_lock) | |
222 | fl->fl_lmops->fl_copy_lock(new, fl); | |
223 | new->fl_lmops = fl->fl_lmops; | |
224 | } | |
225 | } | |
226 | ||
1da177e4 LT |
227 | /* |
228 | * Initialize a new lock from an existing file_lock structure. | |
229 | */ | |
0996905f | 230 | static void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl) |
1da177e4 LT |
231 | { |
232 | new->fl_owner = fl->fl_owner; | |
233 | new->fl_pid = fl->fl_pid; | |
0996905f | 234 | new->fl_file = NULL; |
1da177e4 LT |
235 | new->fl_flags = fl->fl_flags; |
236 | new->fl_type = fl->fl_type; | |
237 | new->fl_start = fl->fl_start; | |
238 | new->fl_end = fl->fl_end; | |
0996905f TM |
239 | new->fl_ops = NULL; |
240 | new->fl_lmops = NULL; | |
241 | } | |
242 | ||
243 | void locks_copy_lock(struct file_lock *new, struct file_lock *fl) | |
244 | { | |
245 | locks_release_private(new); | |
246 | ||
247 | __locks_copy_lock(new, fl); | |
248 | new->fl_file = fl->fl_file; | |
1da177e4 LT |
249 | new->fl_ops = fl->fl_ops; |
250 | new->fl_lmops = fl->fl_lmops; | |
47831f35 TM |
251 | |
252 | locks_copy_private(new, fl); | |
1da177e4 LT |
253 | } |
254 | ||
255 | EXPORT_SYMBOL(locks_copy_lock); | |
256 | ||
257 | static inline int flock_translate_cmd(int cmd) { | |
258 | if (cmd & LOCK_MAND) | |
259 | return cmd & (LOCK_MAND | LOCK_RW); | |
260 | switch (cmd) { | |
261 | case LOCK_SH: | |
262 | return F_RDLCK; | |
263 | case LOCK_EX: | |
264 | return F_WRLCK; | |
265 | case LOCK_UN: | |
266 | return F_UNLCK; | |
267 | } | |
268 | return -EINVAL; | |
269 | } | |
270 | ||
271 | /* Fill in a file_lock structure with an appropriate FLOCK lock. */ | |
272 | static int flock_make_lock(struct file *filp, struct file_lock **lock, | |
273 | unsigned int cmd) | |
274 | { | |
275 | struct file_lock *fl; | |
276 | int type = flock_translate_cmd(cmd); | |
277 | if (type < 0) | |
278 | return type; | |
279 | ||
280 | fl = locks_alloc_lock(); | |
281 | if (fl == NULL) | |
282 | return -ENOMEM; | |
283 | ||
284 | fl->fl_file = filp; | |
285 | fl->fl_pid = current->tgid; | |
286 | fl->fl_flags = FL_FLOCK; | |
287 | fl->fl_type = type; | |
288 | fl->fl_end = OFFSET_MAX; | |
289 | ||
290 | *lock = fl; | |
291 | return 0; | |
292 | } | |
293 | ||
294 | static int assign_type(struct file_lock *fl, int type) | |
295 | { | |
296 | switch (type) { | |
297 | case F_RDLCK: | |
298 | case F_WRLCK: | |
299 | case F_UNLCK: | |
300 | fl->fl_type = type; | |
301 | break; | |
302 | default: | |
303 | return -EINVAL; | |
304 | } | |
305 | return 0; | |
306 | } | |
307 | ||
308 | /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX | |
309 | * style lock. | |
310 | */ | |
311 | static int flock_to_posix_lock(struct file *filp, struct file_lock *fl, | |
312 | struct flock *l) | |
313 | { | |
314 | off_t start, end; | |
315 | ||
316 | switch (l->l_whence) { | |
317 | case 0: /*SEEK_SET*/ | |
318 | start = 0; | |
319 | break; | |
320 | case 1: /*SEEK_CUR*/ | |
321 | start = filp->f_pos; | |
322 | break; | |
323 | case 2: /*SEEK_END*/ | |
324 | start = i_size_read(filp->f_dentry->d_inode); | |
325 | break; | |
326 | default: | |
327 | return -EINVAL; | |
328 | } | |
329 | ||
330 | /* POSIX-1996 leaves the case l->l_len < 0 undefined; | |
331 | POSIX-2001 defines it. */ | |
332 | start += l->l_start; | |
4c780a46 TM |
333 | if (start < 0) |
334 | return -EINVAL; | |
335 | fl->fl_end = OFFSET_MAX; | |
336 | if (l->l_len > 0) { | |
337 | end = start + l->l_len - 1; | |
338 | fl->fl_end = end; | |
339 | } else if (l->l_len < 0) { | |
1da177e4 | 340 | end = start - 1; |
4c780a46 | 341 | fl->fl_end = end; |
1da177e4 | 342 | start += l->l_len; |
4c780a46 TM |
343 | if (start < 0) |
344 | return -EINVAL; | |
1da177e4 | 345 | } |
1da177e4 | 346 | fl->fl_start = start; /* we record the absolute position */ |
4c780a46 TM |
347 | if (fl->fl_end < fl->fl_start) |
348 | return -EOVERFLOW; | |
1da177e4 LT |
349 | |
350 | fl->fl_owner = current->files; | |
351 | fl->fl_pid = current->tgid; | |
352 | fl->fl_file = filp; | |
353 | fl->fl_flags = FL_POSIX; | |
354 | fl->fl_ops = NULL; | |
355 | fl->fl_lmops = NULL; | |
356 | ||
357 | return assign_type(fl, l->l_type); | |
358 | } | |
359 | ||
360 | #if BITS_PER_LONG == 32 | |
361 | static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl, | |
362 | struct flock64 *l) | |
363 | { | |
364 | loff_t start; | |
365 | ||
366 | switch (l->l_whence) { | |
367 | case 0: /*SEEK_SET*/ | |
368 | start = 0; | |
369 | break; | |
370 | case 1: /*SEEK_CUR*/ | |
371 | start = filp->f_pos; | |
372 | break; | |
373 | case 2: /*SEEK_END*/ | |
374 | start = i_size_read(filp->f_dentry->d_inode); | |
375 | break; | |
376 | default: | |
377 | return -EINVAL; | |
378 | } | |
379 | ||
4c780a46 TM |
380 | start += l->l_start; |
381 | if (start < 0) | |
1da177e4 | 382 | return -EINVAL; |
4c780a46 TM |
383 | fl->fl_end = OFFSET_MAX; |
384 | if (l->l_len > 0) { | |
385 | fl->fl_end = start + l->l_len - 1; | |
386 | } else if (l->l_len < 0) { | |
387 | fl->fl_end = start - 1; | |
388 | start += l->l_len; | |
389 | if (start < 0) | |
390 | return -EINVAL; | |
391 | } | |
1da177e4 | 392 | fl->fl_start = start; /* we record the absolute position */ |
4c780a46 TM |
393 | if (fl->fl_end < fl->fl_start) |
394 | return -EOVERFLOW; | |
1da177e4 LT |
395 | |
396 | fl->fl_owner = current->files; | |
397 | fl->fl_pid = current->tgid; | |
398 | fl->fl_file = filp; | |
399 | fl->fl_flags = FL_POSIX; | |
400 | fl->fl_ops = NULL; | |
401 | fl->fl_lmops = NULL; | |
402 | ||
403 | switch (l->l_type) { | |
404 | case F_RDLCK: | |
405 | case F_WRLCK: | |
406 | case F_UNLCK: | |
407 | fl->fl_type = l->l_type; | |
408 | break; | |
409 | default: | |
410 | return -EINVAL; | |
411 | } | |
412 | ||
413 | return (0); | |
414 | } | |
415 | #endif | |
416 | ||
417 | /* default lease lock manager operations */ | |
418 | static void lease_break_callback(struct file_lock *fl) | |
419 | { | |
420 | kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); | |
421 | } | |
422 | ||
423 | static void lease_release_private_callback(struct file_lock *fl) | |
424 | { | |
425 | if (!fl->fl_file) | |
426 | return; | |
427 | ||
428 | f_delown(fl->fl_file); | |
429 | fl->fl_file->f_owner.signum = 0; | |
430 | } | |
431 | ||
75c96f85 | 432 | static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try) |
1da177e4 LT |
433 | { |
434 | return fl->fl_file == try->fl_file; | |
435 | } | |
436 | ||
75c96f85 | 437 | static struct lock_manager_operations lease_manager_ops = { |
1da177e4 LT |
438 | .fl_break = lease_break_callback, |
439 | .fl_release_private = lease_release_private_callback, | |
440 | .fl_mylease = lease_mylease_callback, | |
441 | .fl_change = lease_modify, | |
442 | }; | |
443 | ||
444 | /* | |
445 | * Initialize a lease, use the default lock manager operations | |
446 | */ | |
447 | static int lease_init(struct file *filp, int type, struct file_lock *fl) | |
448 | { | |
449 | fl->fl_owner = current->files; | |
450 | fl->fl_pid = current->tgid; | |
451 | ||
452 | fl->fl_file = filp; | |
453 | fl->fl_flags = FL_LEASE; | |
454 | if (assign_type(fl, type) != 0) { | |
455 | locks_free_lock(fl); | |
456 | return -EINVAL; | |
457 | } | |
458 | fl->fl_start = 0; | |
459 | fl->fl_end = OFFSET_MAX; | |
460 | fl->fl_ops = NULL; | |
461 | fl->fl_lmops = &lease_manager_ops; | |
462 | return 0; | |
463 | } | |
464 | ||
465 | /* Allocate a file_lock initialised to this type of lease */ | |
466 | static int lease_alloc(struct file *filp, int type, struct file_lock **flp) | |
467 | { | |
468 | struct file_lock *fl = locks_alloc_lock(); | |
469 | int error; | |
470 | ||
471 | if (fl == NULL) | |
472 | return -ENOMEM; | |
473 | ||
474 | error = lease_init(filp, type, fl); | |
475 | if (error) | |
476 | return error; | |
477 | *flp = fl; | |
478 | return 0; | |
479 | } | |
480 | ||
481 | /* Check if two locks overlap each other. | |
482 | */ | |
483 | static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) | |
484 | { | |
485 | return ((fl1->fl_end >= fl2->fl_start) && | |
486 | (fl2->fl_end >= fl1->fl_start)); | |
487 | } | |
488 | ||
489 | /* | |
490 | * Check whether two locks have the same owner. | |
491 | */ | |
33443c42 | 492 | static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) |
1da177e4 LT |
493 | { |
494 | if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner) | |
495 | return fl2->fl_lmops == fl1->fl_lmops && | |
496 | fl1->fl_lmops->fl_compare_owner(fl1, fl2); | |
497 | return fl1->fl_owner == fl2->fl_owner; | |
498 | } | |
499 | ||
500 | /* Remove waiter from blocker's block list. | |
501 | * When blocker ends up pointing to itself then the list is empty. | |
502 | */ | |
33443c42 | 503 | static void __locks_delete_block(struct file_lock *waiter) |
1da177e4 LT |
504 | { |
505 | list_del_init(&waiter->fl_block); | |
506 | list_del_init(&waiter->fl_link); | |
507 | waiter->fl_next = NULL; | |
508 | } | |
509 | ||
510 | /* | |
511 | */ | |
512 | static void locks_delete_block(struct file_lock *waiter) | |
513 | { | |
514 | lock_kernel(); | |
515 | __locks_delete_block(waiter); | |
516 | unlock_kernel(); | |
517 | } | |
518 | ||
519 | /* Insert waiter into blocker's block list. | |
520 | * We use a circular list so that processes can be easily woken up in | |
521 | * the order they blocked. The documentation doesn't require this but | |
522 | * it seems like the reasonable thing to do. | |
523 | */ | |
524 | static void locks_insert_block(struct file_lock *blocker, | |
525 | struct file_lock *waiter) | |
526 | { | |
6dc0fe8f | 527 | BUG_ON(!list_empty(&waiter->fl_block)); |
1da177e4 LT |
528 | list_add_tail(&waiter->fl_block, &blocker->fl_block); |
529 | waiter->fl_next = blocker; | |
530 | if (IS_POSIX(blocker)) | |
531 | list_add(&waiter->fl_link, &blocked_list); | |
532 | } | |
533 | ||
534 | /* Wake up processes blocked waiting for blocker. | |
535 | * If told to wait then schedule the processes until the block list | |
536 | * is empty, otherwise empty the block list ourselves. | |
537 | */ | |
538 | static void locks_wake_up_blocks(struct file_lock *blocker) | |
539 | { | |
540 | while (!list_empty(&blocker->fl_block)) { | |
541 | struct file_lock *waiter = list_entry(blocker->fl_block.next, | |
542 | struct file_lock, fl_block); | |
543 | __locks_delete_block(waiter); | |
544 | if (waiter->fl_lmops && waiter->fl_lmops->fl_notify) | |
545 | waiter->fl_lmops->fl_notify(waiter); | |
546 | else | |
547 | wake_up(&waiter->fl_wait); | |
548 | } | |
549 | } | |
550 | ||
551 | /* Insert file lock fl into an inode's lock list at the position indicated | |
552 | * by pos. At the same time add the lock to the global file lock list. | |
553 | */ | |
554 | static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) | |
555 | { | |
556 | list_add(&fl->fl_link, &file_lock_list); | |
557 | ||
558 | /* insert into file's list */ | |
559 | fl->fl_next = *pos; | |
560 | *pos = fl; | |
561 | ||
562 | if (fl->fl_ops && fl->fl_ops->fl_insert) | |
563 | fl->fl_ops->fl_insert(fl); | |
564 | } | |
565 | ||
566 | /* | |
567 | * Delete a lock and then free it. | |
568 | * Wake up processes that are blocked waiting for this lock, | |
569 | * notify the FS that the lock has been cleared and | |
570 | * finally free the lock. | |
571 | */ | |
572 | static void locks_delete_lock(struct file_lock **thisfl_p) | |
573 | { | |
574 | struct file_lock *fl = *thisfl_p; | |
575 | ||
576 | *thisfl_p = fl->fl_next; | |
577 | fl->fl_next = NULL; | |
578 | list_del_init(&fl->fl_link); | |
579 | ||
580 | fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); | |
581 | if (fl->fl_fasync != NULL) { | |
582 | printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); | |
583 | fl->fl_fasync = NULL; | |
584 | } | |
585 | ||
586 | if (fl->fl_ops && fl->fl_ops->fl_remove) | |
587 | fl->fl_ops->fl_remove(fl); | |
588 | ||
589 | locks_wake_up_blocks(fl); | |
590 | locks_free_lock(fl); | |
591 | } | |
592 | ||
593 | /* Determine if lock sys_fl blocks lock caller_fl. Common functionality | |
594 | * checks for shared/exclusive status of overlapping locks. | |
595 | */ | |
596 | static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) | |
597 | { | |
598 | if (sys_fl->fl_type == F_WRLCK) | |
599 | return 1; | |
600 | if (caller_fl->fl_type == F_WRLCK) | |
601 | return 1; | |
602 | return 0; | |
603 | } | |
604 | ||
605 | /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific | |
606 | * checking before calling the locks_conflict(). | |
607 | */ | |
608 | static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) | |
609 | { | |
610 | /* POSIX locks owned by the same process do not conflict with | |
611 | * each other. | |
612 | */ | |
613 | if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) | |
614 | return (0); | |
615 | ||
616 | /* Check whether they overlap */ | |
617 | if (!locks_overlap(caller_fl, sys_fl)) | |
618 | return 0; | |
619 | ||
620 | return (locks_conflict(caller_fl, sys_fl)); | |
621 | } | |
622 | ||
623 | /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific | |
624 | * checking before calling the locks_conflict(). | |
625 | */ | |
626 | static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) | |
627 | { | |
628 | /* FLOCK locks referring to the same filp do not conflict with | |
629 | * each other. | |
630 | */ | |
631 | if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) | |
632 | return (0); | |
633 | if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) | |
634 | return 0; | |
635 | ||
636 | return (locks_conflict(caller_fl, sys_fl)); | |
637 | } | |
638 | ||
639 | static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout) | |
640 | { | |
641 | int result = 0; | |
642 | DECLARE_WAITQUEUE(wait, current); | |
643 | ||
644 | __set_current_state(TASK_INTERRUPTIBLE); | |
645 | add_wait_queue(fl_wait, &wait); | |
646 | if (timeout == 0) | |
647 | schedule(); | |
648 | else | |
649 | result = schedule_timeout(timeout); | |
650 | if (signal_pending(current)) | |
651 | result = -ERESTARTSYS; | |
652 | remove_wait_queue(fl_wait, &wait); | |
653 | __set_current_state(TASK_RUNNING); | |
654 | return result; | |
655 | } | |
656 | ||
657 | static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time) | |
658 | { | |
659 | int result; | |
660 | locks_insert_block(blocker, waiter); | |
661 | result = interruptible_sleep_on_locked(&waiter->fl_wait, time); | |
662 | __locks_delete_block(waiter); | |
663 | return result; | |
664 | } | |
665 | ||
8dc7c311 AA |
666 | int |
667 | posix_test_lock(struct file *filp, struct file_lock *fl, | |
668 | struct file_lock *conflock) | |
1da177e4 LT |
669 | { |
670 | struct file_lock *cfl; | |
671 | ||
672 | lock_kernel(); | |
673 | for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) { | |
674 | if (!IS_POSIX(cfl)) | |
675 | continue; | |
676 | if (posix_locks_conflict(cfl, fl)) | |
677 | break; | |
678 | } | |
8dc7c311 | 679 | if (cfl) { |
0996905f | 680 | __locks_copy_lock(conflock, cfl); |
8dc7c311 AA |
681 | unlock_kernel(); |
682 | return 1; | |
683 | } | |
1da177e4 | 684 | unlock_kernel(); |
8dc7c311 | 685 | return 0; |
1da177e4 LT |
686 | } |
687 | ||
688 | EXPORT_SYMBOL(posix_test_lock); | |
689 | ||
690 | /* This function tests for deadlock condition before putting a process to | |
691 | * sleep. The detection scheme is no longer recursive. Recursive was neat, | |
692 | * but dangerous - we risked stack corruption if the lock data was bad, or | |
693 | * if the recursion was too deep for any other reason. | |
694 | * | |
695 | * We rely on the fact that a task can only be on one lock's wait queue | |
696 | * at a time. When we find blocked_task on a wait queue we can re-search | |
697 | * with blocked_task equal to that queue's owner, until either blocked_task | |
698 | * isn't found, or blocked_task is found on a queue owned by my_task. | |
699 | * | |
700 | * Note: the above assumption may not be true when handling lock requests | |
701 | * from a broken NFS client. But broken NFS clients have a lot more to | |
702 | * worry about than proper deadlock detection anyway... --okir | |
703 | */ | |
704 | int posix_locks_deadlock(struct file_lock *caller_fl, | |
705 | struct file_lock *block_fl) | |
706 | { | |
707 | struct list_head *tmp; | |
708 | ||
709 | next_task: | |
710 | if (posix_same_owner(caller_fl, block_fl)) | |
711 | return 1; | |
712 | list_for_each(tmp, &blocked_list) { | |
713 | struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link); | |
714 | if (posix_same_owner(fl, block_fl)) { | |
715 | fl = fl->fl_next; | |
716 | block_fl = fl; | |
717 | goto next_task; | |
718 | } | |
719 | } | |
720 | return 0; | |
721 | } | |
722 | ||
723 | EXPORT_SYMBOL(posix_locks_deadlock); | |
724 | ||
725 | /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks | |
726 | * at the head of the list, but that's secret knowledge known only to | |
727 | * flock_lock_file and posix_lock_file. | |
728 | */ | |
729 | static int flock_lock_file(struct file *filp, struct file_lock *new_fl) | |
730 | { | |
731 | struct file_lock **before; | |
732 | struct inode * inode = filp->f_dentry->d_inode; | |
733 | int error = 0; | |
734 | int found = 0; | |
735 | ||
736 | lock_kernel(); | |
737 | for_each_lock(inode, before) { | |
738 | struct file_lock *fl = *before; | |
739 | if (IS_POSIX(fl)) | |
740 | break; | |
741 | if (IS_LEASE(fl)) | |
742 | continue; | |
743 | if (filp != fl->fl_file) | |
744 | continue; | |
745 | if (new_fl->fl_type == fl->fl_type) | |
746 | goto out; | |
747 | found = 1; | |
748 | locks_delete_lock(before); | |
749 | break; | |
750 | } | |
751 | unlock_kernel(); | |
752 | ||
753 | if (new_fl->fl_type == F_UNLCK) | |
754 | return 0; | |
755 | ||
756 | /* | |
757 | * If a higher-priority process was blocked on the old file lock, | |
758 | * give it the opportunity to lock the file. | |
759 | */ | |
760 | if (found) | |
761 | cond_resched(); | |
762 | ||
763 | lock_kernel(); | |
764 | for_each_lock(inode, before) { | |
765 | struct file_lock *fl = *before; | |
766 | if (IS_POSIX(fl)) | |
767 | break; | |
768 | if (IS_LEASE(fl)) | |
769 | continue; | |
770 | if (!flock_locks_conflict(new_fl, fl)) | |
771 | continue; | |
772 | error = -EAGAIN; | |
773 | if (new_fl->fl_flags & FL_SLEEP) { | |
774 | locks_insert_block(fl, new_fl); | |
775 | } | |
776 | goto out; | |
777 | } | |
778 | locks_insert_lock(&inode->i_flock, new_fl); | |
779 | error = 0; | |
780 | ||
781 | out: | |
782 | unlock_kernel(); | |
783 | return error; | |
784 | } | |
785 | ||
5842add2 | 786 | static int __posix_lock_file_conf(struct inode *inode, struct file_lock *request, struct file_lock *conflock) |
1da177e4 LT |
787 | { |
788 | struct file_lock *fl; | |
789 | struct file_lock *new_fl, *new_fl2; | |
790 | struct file_lock *left = NULL; | |
791 | struct file_lock *right = NULL; | |
792 | struct file_lock **before; | |
793 | int error, added = 0; | |
794 | ||
795 | /* | |
796 | * We may need two file_lock structures for this operation, | |
797 | * so we get them in advance to avoid races. | |
798 | */ | |
799 | new_fl = locks_alloc_lock(); | |
800 | new_fl2 = locks_alloc_lock(); | |
801 | ||
802 | lock_kernel(); | |
803 | if (request->fl_type != F_UNLCK) { | |
804 | for_each_lock(inode, before) { | |
805 | struct file_lock *fl = *before; | |
806 | if (!IS_POSIX(fl)) | |
807 | continue; | |
808 | if (!posix_locks_conflict(request, fl)) | |
809 | continue; | |
5842add2 AA |
810 | if (conflock) |
811 | locks_copy_lock(conflock, fl); | |
1da177e4 LT |
812 | error = -EAGAIN; |
813 | if (!(request->fl_flags & FL_SLEEP)) | |
814 | goto out; | |
815 | error = -EDEADLK; | |
816 | if (posix_locks_deadlock(request, fl)) | |
817 | goto out; | |
818 | error = -EAGAIN; | |
819 | locks_insert_block(fl, request); | |
820 | goto out; | |
821 | } | |
822 | } | |
823 | ||
824 | /* If we're just looking for a conflict, we're done. */ | |
825 | error = 0; | |
826 | if (request->fl_flags & FL_ACCESS) | |
827 | goto out; | |
828 | ||
829 | error = -ENOLCK; /* "no luck" */ | |
830 | if (!(new_fl && new_fl2)) | |
831 | goto out; | |
832 | ||
833 | /* | |
834 | * We've allocated the new locks in advance, so there are no | |
835 | * errors possible (and no blocking operations) from here on. | |
836 | * | |
837 | * Find the first old lock with the same owner as the new lock. | |
838 | */ | |
839 | ||
840 | before = &inode->i_flock; | |
841 | ||
842 | /* First skip locks owned by other processes. */ | |
843 | while ((fl = *before) && (!IS_POSIX(fl) || | |
844 | !posix_same_owner(request, fl))) { | |
845 | before = &fl->fl_next; | |
846 | } | |
847 | ||
848 | /* Process locks with this owner. */ | |
849 | while ((fl = *before) && posix_same_owner(request, fl)) { | |
850 | /* Detect adjacent or overlapping regions (if same lock type) | |
851 | */ | |
852 | if (request->fl_type == fl->fl_type) { | |
449231d6 OK |
853 | /* In all comparisons of start vs end, use |
854 | * "start - 1" rather than "end + 1". If end | |
855 | * is OFFSET_MAX, end + 1 will become negative. | |
856 | */ | |
1da177e4 LT |
857 | if (fl->fl_end < request->fl_start - 1) |
858 | goto next_lock; | |
859 | /* If the next lock in the list has entirely bigger | |
860 | * addresses than the new one, insert the lock here. | |
861 | */ | |
449231d6 | 862 | if (fl->fl_start - 1 > request->fl_end) |
1da177e4 LT |
863 | break; |
864 | ||
865 | /* If we come here, the new and old lock are of the | |
866 | * same type and adjacent or overlapping. Make one | |
867 | * lock yielding from the lower start address of both | |
868 | * locks to the higher end address. | |
869 | */ | |
870 | if (fl->fl_start > request->fl_start) | |
871 | fl->fl_start = request->fl_start; | |
872 | else | |
873 | request->fl_start = fl->fl_start; | |
874 | if (fl->fl_end < request->fl_end) | |
875 | fl->fl_end = request->fl_end; | |
876 | else | |
877 | request->fl_end = fl->fl_end; | |
878 | if (added) { | |
879 | locks_delete_lock(before); | |
880 | continue; | |
881 | } | |
882 | request = fl; | |
883 | added = 1; | |
884 | } | |
885 | else { | |
886 | /* Processing for different lock types is a bit | |
887 | * more complex. | |
888 | */ | |
889 | if (fl->fl_end < request->fl_start) | |
890 | goto next_lock; | |
891 | if (fl->fl_start > request->fl_end) | |
892 | break; | |
893 | if (request->fl_type == F_UNLCK) | |
894 | added = 1; | |
895 | if (fl->fl_start < request->fl_start) | |
896 | left = fl; | |
897 | /* If the next lock in the list has a higher end | |
898 | * address than the new one, insert the new one here. | |
899 | */ | |
900 | if (fl->fl_end > request->fl_end) { | |
901 | right = fl; | |
902 | break; | |
903 | } | |
904 | if (fl->fl_start >= request->fl_start) { | |
905 | /* The new lock completely replaces an old | |
906 | * one (This may happen several times). | |
907 | */ | |
908 | if (added) { | |
909 | locks_delete_lock(before); | |
910 | continue; | |
911 | } | |
912 | /* Replace the old lock with the new one. | |
913 | * Wake up anybody waiting for the old one, | |
914 | * as the change in lock type might satisfy | |
915 | * their needs. | |
916 | */ | |
917 | locks_wake_up_blocks(fl); | |
918 | fl->fl_start = request->fl_start; | |
919 | fl->fl_end = request->fl_end; | |
920 | fl->fl_type = request->fl_type; | |
47831f35 TM |
921 | locks_release_private(fl); |
922 | locks_copy_private(fl, request); | |
1da177e4 LT |
923 | request = fl; |
924 | added = 1; | |
925 | } | |
926 | } | |
927 | /* Go on to next lock. | |
928 | */ | |
929 | next_lock: | |
930 | before = &fl->fl_next; | |
931 | } | |
932 | ||
933 | error = 0; | |
934 | if (!added) { | |
935 | if (request->fl_type == F_UNLCK) | |
936 | goto out; | |
937 | locks_copy_lock(new_fl, request); | |
938 | locks_insert_lock(before, new_fl); | |
939 | new_fl = NULL; | |
940 | } | |
941 | if (right) { | |
942 | if (left == right) { | |
943 | /* The new lock breaks the old one in two pieces, | |
944 | * so we have to use the second new lock. | |
945 | */ | |
946 | left = new_fl2; | |
947 | new_fl2 = NULL; | |
948 | locks_copy_lock(left, right); | |
949 | locks_insert_lock(before, left); | |
950 | } | |
951 | right->fl_start = request->fl_end + 1; | |
952 | locks_wake_up_blocks(right); | |
953 | } | |
954 | if (left) { | |
955 | left->fl_end = request->fl_start - 1; | |
956 | locks_wake_up_blocks(left); | |
957 | } | |
958 | out: | |
959 | unlock_kernel(); | |
960 | /* | |
961 | * Free any unused locks. | |
962 | */ | |
963 | if (new_fl) | |
964 | locks_free_lock(new_fl); | |
965 | if (new_fl2) | |
966 | locks_free_lock(new_fl2); | |
967 | return error; | |
968 | } | |
969 | ||
970 | /** | |
971 | * posix_lock_file - Apply a POSIX-style lock to a file | |
972 | * @filp: The file to apply the lock to | |
973 | * @fl: The lock to be applied | |
974 | * | |
975 | * Add a POSIX style lock to a file. | |
976 | * We merge adjacent & overlapping locks whenever possible. | |
977 | * POSIX locks are sorted by owner task, then by starting address | |
978 | */ | |
979 | int posix_lock_file(struct file *filp, struct file_lock *fl) | |
980 | { | |
5842add2 AA |
981 | return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, NULL); |
982 | } | |
983 | EXPORT_SYMBOL(posix_lock_file); | |
984 | ||
985 | /** | |
986 | * posix_lock_file_conf - Apply a POSIX-style lock to a file | |
987 | * @filp: The file to apply the lock to | |
988 | * @fl: The lock to be applied | |
989 | * @conflock: Place to return a copy of the conflicting lock, if found. | |
990 | * | |
991 | * Except for the conflock parameter, acts just like posix_lock_file. | |
992 | */ | |
993 | int posix_lock_file_conf(struct file *filp, struct file_lock *fl, | |
994 | struct file_lock *conflock) | |
995 | { | |
996 | return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, conflock); | |
1da177e4 | 997 | } |
5842add2 | 998 | EXPORT_SYMBOL(posix_lock_file_conf); |
1da177e4 LT |
999 | |
1000 | /** | |
1001 | * posix_lock_file_wait - Apply a POSIX-style lock to a file | |
1002 | * @filp: The file to apply the lock to | |
1003 | * @fl: The lock to be applied | |
1004 | * | |
1005 | * Add a POSIX style lock to a file. | |
1006 | * We merge adjacent & overlapping locks whenever possible. | |
1007 | * POSIX locks are sorted by owner task, then by starting address | |
1008 | */ | |
1009 | int posix_lock_file_wait(struct file *filp, struct file_lock *fl) | |
1010 | { | |
1011 | int error; | |
1012 | might_sleep (); | |
1013 | for (;;) { | |
5842add2 | 1014 | error = posix_lock_file(filp, fl); |
1da177e4 LT |
1015 | if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) |
1016 | break; | |
1017 | error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); | |
1018 | if (!error) | |
1019 | continue; | |
1020 | ||
1021 | locks_delete_block(fl); | |
1022 | break; | |
1023 | } | |
1024 | return error; | |
1025 | } | |
1026 | EXPORT_SYMBOL(posix_lock_file_wait); | |
1027 | ||
1028 | /** | |
1029 | * locks_mandatory_locked - Check for an active lock | |
1030 | * @inode: the file to check | |
1031 | * | |
1032 | * Searches the inode's list of locks to find any POSIX locks which conflict. | |
1033 | * This function is called from locks_verify_locked() only. | |
1034 | */ | |
1035 | int locks_mandatory_locked(struct inode *inode) | |
1036 | { | |
1037 | fl_owner_t owner = current->files; | |
1038 | struct file_lock *fl; | |
1039 | ||
1040 | /* | |
1041 | * Search the lock list for this inode for any POSIX locks. | |
1042 | */ | |
1043 | lock_kernel(); | |
1044 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { | |
1045 | if (!IS_POSIX(fl)) | |
1046 | continue; | |
1047 | if (fl->fl_owner != owner) | |
1048 | break; | |
1049 | } | |
1050 | unlock_kernel(); | |
1051 | return fl ? -EAGAIN : 0; | |
1052 | } | |
1053 | ||
1054 | /** | |
1055 | * locks_mandatory_area - Check for a conflicting lock | |
1056 | * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ | |
1057 | * for shared | |
1058 | * @inode: the file to check | |
1059 | * @filp: how the file was opened (if it was) | |
1060 | * @offset: start of area to check | |
1061 | * @count: length of area to check | |
1062 | * | |
1063 | * Searches the inode's list of locks to find any POSIX locks which conflict. | |
1064 | * This function is called from rw_verify_area() and | |
1065 | * locks_verify_truncate(). | |
1066 | */ | |
1067 | int locks_mandatory_area(int read_write, struct inode *inode, | |
1068 | struct file *filp, loff_t offset, | |
1069 | size_t count) | |
1070 | { | |
1071 | struct file_lock fl; | |
1072 | int error; | |
1073 | ||
1074 | locks_init_lock(&fl); | |
1075 | fl.fl_owner = current->files; | |
1076 | fl.fl_pid = current->tgid; | |
1077 | fl.fl_file = filp; | |
1078 | fl.fl_flags = FL_POSIX | FL_ACCESS; | |
1079 | if (filp && !(filp->f_flags & O_NONBLOCK)) | |
1080 | fl.fl_flags |= FL_SLEEP; | |
1081 | fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; | |
1082 | fl.fl_start = offset; | |
1083 | fl.fl_end = offset + count - 1; | |
1084 | ||
1085 | for (;;) { | |
5842add2 | 1086 | error = __posix_lock_file_conf(inode, &fl, NULL); |
1da177e4 LT |
1087 | if (error != -EAGAIN) |
1088 | break; | |
1089 | if (!(fl.fl_flags & FL_SLEEP)) | |
1090 | break; | |
1091 | error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); | |
1092 | if (!error) { | |
1093 | /* | |
1094 | * If we've been sleeping someone might have | |
1095 | * changed the permissions behind our back. | |
1096 | */ | |
1097 | if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) | |
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | locks_delete_block(&fl); | |
1102 | break; | |
1103 | } | |
1104 | ||
1105 | return error; | |
1106 | } | |
1107 | ||
1108 | EXPORT_SYMBOL(locks_mandatory_area); | |
1109 | ||
1110 | /* We already had a lease on this file; just change its type */ | |
1111 | int lease_modify(struct file_lock **before, int arg) | |
1112 | { | |
1113 | struct file_lock *fl = *before; | |
1114 | int error = assign_type(fl, arg); | |
1115 | ||
1116 | if (error) | |
1117 | return error; | |
1118 | locks_wake_up_blocks(fl); | |
1119 | if (arg == F_UNLCK) | |
1120 | locks_delete_lock(before); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | EXPORT_SYMBOL(lease_modify); | |
1125 | ||
1126 | static void time_out_leases(struct inode *inode) | |
1127 | { | |
1128 | struct file_lock **before; | |
1129 | struct file_lock *fl; | |
1130 | ||
1131 | before = &inode->i_flock; | |
1132 | while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) { | |
1133 | if ((fl->fl_break_time == 0) | |
1134 | || time_before(jiffies, fl->fl_break_time)) { | |
1135 | before = &fl->fl_next; | |
1136 | continue; | |
1137 | } | |
1da177e4 LT |
1138 | lease_modify(before, fl->fl_type & ~F_INPROGRESS); |
1139 | if (fl == *before) /* lease_modify may have freed fl */ | |
1140 | before = &fl->fl_next; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | /** | |
1145 | * __break_lease - revoke all outstanding leases on file | |
1146 | * @inode: the inode of the file to return | |
1147 | * @mode: the open mode (read or write) | |
1148 | * | |
1149 | * break_lease (inlined for speed) has checked there already | |
1150 | * is a lease on this file. Leases are broken on a call to open() | |
1151 | * or truncate(). This function can sleep unless you | |
1152 | * specified %O_NONBLOCK to your open(). | |
1153 | */ | |
1154 | int __break_lease(struct inode *inode, unsigned int mode) | |
1155 | { | |
1156 | int error = 0, future; | |
1157 | struct file_lock *new_fl, *flock; | |
1158 | struct file_lock *fl; | |
1159 | int alloc_err; | |
1160 | unsigned long break_time; | |
1161 | int i_have_this_lease = 0; | |
1162 | ||
1163 | alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK, | |
1164 | &new_fl); | |
1165 | ||
1166 | lock_kernel(); | |
1167 | ||
1168 | time_out_leases(inode); | |
1169 | ||
1170 | flock = inode->i_flock; | |
1171 | if ((flock == NULL) || !IS_LEASE(flock)) | |
1172 | goto out; | |
1173 | ||
1174 | for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) | |
1175 | if (fl->fl_owner == current->files) | |
1176 | i_have_this_lease = 1; | |
1177 | ||
1178 | if (mode & FMODE_WRITE) { | |
1179 | /* If we want write access, we have to revoke any lease. */ | |
1180 | future = F_UNLCK | F_INPROGRESS; | |
1181 | } else if (flock->fl_type & F_INPROGRESS) { | |
1182 | /* If the lease is already being broken, we just leave it */ | |
1183 | future = flock->fl_type; | |
1184 | } else if (flock->fl_type & F_WRLCK) { | |
1185 | /* Downgrade the exclusive lease to a read-only lease. */ | |
1186 | future = F_RDLCK | F_INPROGRESS; | |
1187 | } else { | |
1188 | /* the existing lease was read-only, so we can read too. */ | |
1189 | goto out; | |
1190 | } | |
1191 | ||
1192 | if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) { | |
1193 | error = alloc_err; | |
1194 | goto out; | |
1195 | } | |
1196 | ||
1197 | break_time = 0; | |
1198 | if (lease_break_time > 0) { | |
1199 | break_time = jiffies + lease_break_time * HZ; | |
1200 | if (break_time == 0) | |
1201 | break_time++; /* so that 0 means no break time */ | |
1202 | } | |
1203 | ||
1204 | for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) { | |
1205 | if (fl->fl_type != future) { | |
1206 | fl->fl_type = future; | |
1207 | fl->fl_break_time = break_time; | |
1208 | /* lease must have lmops break callback */ | |
1209 | fl->fl_lmops->fl_break(fl); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | if (i_have_this_lease || (mode & O_NONBLOCK)) { | |
1214 | error = -EWOULDBLOCK; | |
1215 | goto out; | |
1216 | } | |
1217 | ||
1218 | restart: | |
1219 | break_time = flock->fl_break_time; | |
1220 | if (break_time != 0) { | |
1221 | break_time -= jiffies; | |
1222 | if (break_time == 0) | |
1223 | break_time++; | |
1224 | } | |
1225 | error = locks_block_on_timeout(flock, new_fl, break_time); | |
1226 | if (error >= 0) { | |
1227 | if (error == 0) | |
1228 | time_out_leases(inode); | |
1229 | /* Wait for the next lease that has not been broken yet */ | |
1230 | for (flock = inode->i_flock; flock && IS_LEASE(flock); | |
1231 | flock = flock->fl_next) { | |
1232 | if (flock->fl_type & F_INPROGRESS) | |
1233 | goto restart; | |
1234 | } | |
1235 | error = 0; | |
1236 | } | |
1237 | ||
1238 | out: | |
1239 | unlock_kernel(); | |
1240 | if (!alloc_err) | |
1241 | locks_free_lock(new_fl); | |
1242 | return error; | |
1243 | } | |
1244 | ||
1245 | EXPORT_SYMBOL(__break_lease); | |
1246 | ||
1247 | /** | |
1248 | * lease_get_mtime | |
1249 | * @inode: the inode | |
1250 | * @time: pointer to a timespec which will contain the last modified time | |
1251 | * | |
1252 | * This is to force NFS clients to flush their caches for files with | |
1253 | * exclusive leases. The justification is that if someone has an | |
1254 | * exclusive lease, then they could be modifiying it. | |
1255 | */ | |
1256 | void lease_get_mtime(struct inode *inode, struct timespec *time) | |
1257 | { | |
1258 | struct file_lock *flock = inode->i_flock; | |
1259 | if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK)) | |
1260 | *time = current_fs_time(inode->i_sb); | |
1261 | else | |
1262 | *time = inode->i_mtime; | |
1263 | } | |
1264 | ||
1265 | EXPORT_SYMBOL(lease_get_mtime); | |
1266 | ||
1267 | /** | |
1268 | * fcntl_getlease - Enquire what lease is currently active | |
1269 | * @filp: the file | |
1270 | * | |
1271 | * The value returned by this function will be one of | |
1272 | * (if no lease break is pending): | |
1273 | * | |
1274 | * %F_RDLCK to indicate a shared lease is held. | |
1275 | * | |
1276 | * %F_WRLCK to indicate an exclusive lease is held. | |
1277 | * | |
1278 | * %F_UNLCK to indicate no lease is held. | |
1279 | * | |
1280 | * (if a lease break is pending): | |
1281 | * | |
1282 | * %F_RDLCK to indicate an exclusive lease needs to be | |
1283 | * changed to a shared lease (or removed). | |
1284 | * | |
1285 | * %F_UNLCK to indicate the lease needs to be removed. | |
1286 | * | |
1287 | * XXX: sfr & willy disagree over whether F_INPROGRESS | |
1288 | * should be returned to userspace. | |
1289 | */ | |
1290 | int fcntl_getlease(struct file *filp) | |
1291 | { | |
1292 | struct file_lock *fl; | |
1293 | int type = F_UNLCK; | |
1294 | ||
1295 | lock_kernel(); | |
1296 | time_out_leases(filp->f_dentry->d_inode); | |
1297 | for (fl = filp->f_dentry->d_inode->i_flock; fl && IS_LEASE(fl); | |
1298 | fl = fl->fl_next) { | |
1299 | if (fl->fl_file == filp) { | |
1300 | type = fl->fl_type & ~F_INPROGRESS; | |
1301 | break; | |
1302 | } | |
1303 | } | |
1304 | unlock_kernel(); | |
1305 | return type; | |
1306 | } | |
1307 | ||
1308 | /** | |
1309 | * __setlease - sets a lease on an open file | |
1310 | * @filp: file pointer | |
1311 | * @arg: type of lease to obtain | |
1312 | * @flp: input - file_lock to use, output - file_lock inserted | |
1313 | * | |
1314 | * The (input) flp->fl_lmops->fl_break function is required | |
1315 | * by break_lease(). | |
1316 | * | |
1317 | * Called with kernel lock held. | |
1318 | */ | |
75c96f85 | 1319 | static int __setlease(struct file *filp, long arg, struct file_lock **flp) |
1da177e4 | 1320 | { |
7eaae282 | 1321 | struct file_lock *fl, **before, **my_before = NULL, *lease; |
1da177e4 LT |
1322 | struct dentry *dentry = filp->f_dentry; |
1323 | struct inode *inode = dentry->d_inode; | |
1324 | int error, rdlease_count = 0, wrlease_count = 0; | |
1325 | ||
1326 | time_out_leases(inode); | |
1327 | ||
1328 | error = -EINVAL; | |
1329 | if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break) | |
1330 | goto out; | |
1331 | ||
7eaae282 KZ |
1332 | lease = *flp; |
1333 | ||
1da177e4 LT |
1334 | error = -EAGAIN; |
1335 | if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) | |
1336 | goto out; | |
1337 | if ((arg == F_WRLCK) | |
1338 | && ((atomic_read(&dentry->d_count) > 1) | |
1339 | || (atomic_read(&inode->i_count) > 1))) | |
1340 | goto out; | |
1341 | ||
1342 | /* | |
1343 | * At this point, we know that if there is an exclusive | |
1344 | * lease on this file, then we hold it on this filp | |
1345 | * (otherwise our open of this file would have blocked). | |
1346 | * And if we are trying to acquire an exclusive lease, | |
1347 | * then the file is not open by anyone (including us) | |
1348 | * except for this filp. | |
1349 | */ | |
1350 | for (before = &inode->i_flock; | |
1351 | ((fl = *before) != NULL) && IS_LEASE(fl); | |
1352 | before = &fl->fl_next) { | |
1353 | if (lease->fl_lmops->fl_mylease(fl, lease)) | |
1354 | my_before = before; | |
1355 | else if (fl->fl_type == (F_INPROGRESS | F_UNLCK)) | |
1356 | /* | |
1357 | * Someone is in the process of opening this | |
1358 | * file for writing so we may not take an | |
1359 | * exclusive lease on it. | |
1360 | */ | |
1361 | wrlease_count++; | |
1362 | else | |
1363 | rdlease_count++; | |
1364 | } | |
1365 | ||
1366 | if ((arg == F_RDLCK && (wrlease_count > 0)) || | |
1367 | (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0))) | |
1368 | goto out; | |
1369 | ||
1370 | if (my_before != NULL) { | |
1371 | error = lease->fl_lmops->fl_change(my_before, arg); | |
1372 | goto out; | |
1373 | } | |
1374 | ||
1375 | error = 0; | |
1376 | if (arg == F_UNLCK) | |
1377 | goto out; | |
1378 | ||
1379 | error = -EINVAL; | |
1380 | if (!leases_enable) | |
1381 | goto out; | |
1382 | ||
1383 | error = lease_alloc(filp, arg, &fl); | |
1384 | if (error) | |
1385 | goto out; | |
1386 | ||
1387 | locks_copy_lock(fl, lease); | |
1388 | ||
1389 | locks_insert_lock(before, fl); | |
1390 | ||
1391 | *flp = fl; | |
1392 | out: | |
1393 | return error; | |
1394 | } | |
1395 | ||
1396 | /** | |
1397 | * setlease - sets a lease on an open file | |
1398 | * @filp: file pointer | |
1399 | * @arg: type of lease to obtain | |
1400 | * @lease: file_lock to use | |
1401 | * | |
1402 | * Call this to establish a lease on the file. | |
1403 | * The fl_lmops fl_break function is required by break_lease | |
1404 | */ | |
1405 | ||
1406 | int setlease(struct file *filp, long arg, struct file_lock **lease) | |
1407 | { | |
1408 | struct dentry *dentry = filp->f_dentry; | |
1409 | struct inode *inode = dentry->d_inode; | |
1410 | int error; | |
1411 | ||
1412 | if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) | |
1413 | return -EACCES; | |
1414 | if (!S_ISREG(inode->i_mode)) | |
1415 | return -EINVAL; | |
1416 | error = security_file_lock(filp, arg); | |
1417 | if (error) | |
1418 | return error; | |
1419 | ||
1420 | lock_kernel(); | |
1421 | error = __setlease(filp, arg, lease); | |
1422 | unlock_kernel(); | |
1423 | ||
1424 | return error; | |
1425 | } | |
1426 | ||
1427 | EXPORT_SYMBOL(setlease); | |
1428 | ||
1429 | /** | |
1430 | * fcntl_setlease - sets a lease on an open file | |
1431 | * @fd: open file descriptor | |
1432 | * @filp: file pointer | |
1433 | * @arg: type of lease to obtain | |
1434 | * | |
1435 | * Call this fcntl to establish a lease on the file. | |
1436 | * Note that you also need to call %F_SETSIG to | |
1437 | * receive a signal when the lease is broken. | |
1438 | */ | |
1439 | int fcntl_setlease(unsigned int fd, struct file *filp, long arg) | |
1440 | { | |
1441 | struct file_lock fl, *flp = &fl; | |
1442 | struct dentry *dentry = filp->f_dentry; | |
1443 | struct inode *inode = dentry->d_inode; | |
1444 | int error; | |
1445 | ||
1446 | if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) | |
1447 | return -EACCES; | |
1448 | if (!S_ISREG(inode->i_mode)) | |
1449 | return -EINVAL; | |
1450 | error = security_file_lock(filp, arg); | |
1451 | if (error) | |
1452 | return error; | |
1453 | ||
1454 | locks_init_lock(&fl); | |
1455 | error = lease_init(filp, arg, &fl); | |
1456 | if (error) | |
1457 | return error; | |
1458 | ||
1459 | lock_kernel(); | |
1460 | ||
1461 | error = __setlease(filp, arg, &flp); | |
dc15ae14 | 1462 | if (error || arg == F_UNLCK) |
1da177e4 LT |
1463 | goto out_unlock; |
1464 | ||
1465 | error = fasync_helper(fd, filp, 1, &flp->fl_fasync); | |
1466 | if (error < 0) { | |
1467 | /* remove lease just inserted by __setlease */ | |
1468 | flp->fl_type = F_UNLCK | F_INPROGRESS; | |
1469 | flp->fl_break_time = jiffies- 10; | |
1470 | time_out_leases(inode); | |
1471 | goto out_unlock; | |
1472 | } | |
1473 | ||
1474 | error = f_setown(filp, current->pid, 0); | |
1475 | out_unlock: | |
1476 | unlock_kernel(); | |
1477 | return error; | |
1478 | } | |
1479 | ||
1480 | /** | |
1481 | * flock_lock_file_wait - Apply a FLOCK-style lock to a file | |
1482 | * @filp: The file to apply the lock to | |
1483 | * @fl: The lock to be applied | |
1484 | * | |
1485 | * Add a FLOCK style lock to a file. | |
1486 | */ | |
1487 | int flock_lock_file_wait(struct file *filp, struct file_lock *fl) | |
1488 | { | |
1489 | int error; | |
1490 | might_sleep(); | |
1491 | for (;;) { | |
1492 | error = flock_lock_file(filp, fl); | |
1493 | if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) | |
1494 | break; | |
1495 | error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); | |
1496 | if (!error) | |
1497 | continue; | |
1498 | ||
1499 | locks_delete_block(fl); | |
1500 | break; | |
1501 | } | |
1502 | return error; | |
1503 | } | |
1504 | ||
1505 | EXPORT_SYMBOL(flock_lock_file_wait); | |
1506 | ||
1507 | /** | |
1508 | * sys_flock: - flock() system call. | |
1509 | * @fd: the file descriptor to lock. | |
1510 | * @cmd: the type of lock to apply. | |
1511 | * | |
1512 | * Apply a %FL_FLOCK style lock to an open file descriptor. | |
1513 | * The @cmd can be one of | |
1514 | * | |
1515 | * %LOCK_SH -- a shared lock. | |
1516 | * | |
1517 | * %LOCK_EX -- an exclusive lock. | |
1518 | * | |
1519 | * %LOCK_UN -- remove an existing lock. | |
1520 | * | |
1521 | * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. | |
1522 | * | |
1523 | * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other | |
1524 | * processes read and write access respectively. | |
1525 | */ | |
1526 | asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) | |
1527 | { | |
1528 | struct file *filp; | |
1529 | struct file_lock *lock; | |
1530 | int can_sleep, unlock; | |
1531 | int error; | |
1532 | ||
1533 | error = -EBADF; | |
1534 | filp = fget(fd); | |
1535 | if (!filp) | |
1536 | goto out; | |
1537 | ||
1538 | can_sleep = !(cmd & LOCK_NB); | |
1539 | cmd &= ~LOCK_NB; | |
1540 | unlock = (cmd == LOCK_UN); | |
1541 | ||
1542 | if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3)) | |
1543 | goto out_putf; | |
1544 | ||
1545 | error = flock_make_lock(filp, &lock, cmd); | |
1546 | if (error) | |
1547 | goto out_putf; | |
1548 | if (can_sleep) | |
1549 | lock->fl_flags |= FL_SLEEP; | |
1550 | ||
1551 | error = security_file_lock(filp, cmd); | |
1552 | if (error) | |
1553 | goto out_free; | |
1554 | ||
1555 | if (filp->f_op && filp->f_op->flock) | |
1556 | error = filp->f_op->flock(filp, | |
1557 | (can_sleep) ? F_SETLKW : F_SETLK, | |
1558 | lock); | |
1559 | else | |
1560 | error = flock_lock_file_wait(filp, lock); | |
1561 | ||
1562 | out_free: | |
1563 | if (list_empty(&lock->fl_link)) { | |
1564 | locks_free_lock(lock); | |
1565 | } | |
1566 | ||
1567 | out_putf: | |
1568 | fput(filp); | |
1569 | out: | |
1570 | return error; | |
1571 | } | |
1572 | ||
1573 | /* Report the first existing lock that would conflict with l. | |
1574 | * This implements the F_GETLK command of fcntl(). | |
1575 | */ | |
1576 | int fcntl_getlk(struct file *filp, struct flock __user *l) | |
1577 | { | |
8dc7c311 | 1578 | struct file_lock *fl, cfl, file_lock; |
1da177e4 LT |
1579 | struct flock flock; |
1580 | int error; | |
1581 | ||
1582 | error = -EFAULT; | |
1583 | if (copy_from_user(&flock, l, sizeof(flock))) | |
1584 | goto out; | |
1585 | error = -EINVAL; | |
1586 | if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) | |
1587 | goto out; | |
1588 | ||
1589 | error = flock_to_posix_lock(filp, &file_lock, &flock); | |
1590 | if (error) | |
1591 | goto out; | |
1592 | ||
1593 | if (filp->f_op && filp->f_op->lock) { | |
1594 | error = filp->f_op->lock(filp, F_GETLK, &file_lock); | |
80fec4c6 TM |
1595 | if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) |
1596 | file_lock.fl_ops->fl_release_private(&file_lock); | |
1da177e4 LT |
1597 | if (error < 0) |
1598 | goto out; | |
1599 | else | |
1600 | fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock); | |
1601 | } else { | |
8dc7c311 | 1602 | fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL); |
1da177e4 LT |
1603 | } |
1604 | ||
1605 | flock.l_type = F_UNLCK; | |
1606 | if (fl != NULL) { | |
1607 | flock.l_pid = fl->fl_pid; | |
1608 | #if BITS_PER_LONG == 32 | |
1609 | /* | |
1610 | * Make sure we can represent the posix lock via | |
1611 | * legacy 32bit flock. | |
1612 | */ | |
1613 | error = -EOVERFLOW; | |
1614 | if (fl->fl_start > OFFT_OFFSET_MAX) | |
1615 | goto out; | |
1616 | if ((fl->fl_end != OFFSET_MAX) | |
1617 | && (fl->fl_end > OFFT_OFFSET_MAX)) | |
1618 | goto out; | |
1619 | #endif | |
1620 | flock.l_start = fl->fl_start; | |
1621 | flock.l_len = fl->fl_end == OFFSET_MAX ? 0 : | |
1622 | fl->fl_end - fl->fl_start + 1; | |
1623 | flock.l_whence = 0; | |
1624 | flock.l_type = fl->fl_type; | |
1625 | } | |
1626 | error = -EFAULT; | |
1627 | if (!copy_to_user(l, &flock, sizeof(flock))) | |
1628 | error = 0; | |
1629 | out: | |
1630 | return error; | |
1631 | } | |
1632 | ||
1633 | /* Apply the lock described by l to an open file descriptor. | |
1634 | * This implements both the F_SETLK and F_SETLKW commands of fcntl(). | |
1635 | */ | |
c293621b PS |
1636 | int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, |
1637 | struct flock __user *l) | |
1da177e4 LT |
1638 | { |
1639 | struct file_lock *file_lock = locks_alloc_lock(); | |
1640 | struct flock flock; | |
1641 | struct inode *inode; | |
1642 | int error; | |
1643 | ||
1644 | if (file_lock == NULL) | |
1645 | return -ENOLCK; | |
1646 | ||
1647 | /* | |
1648 | * This might block, so we do it before checking the inode. | |
1649 | */ | |
1650 | error = -EFAULT; | |
1651 | if (copy_from_user(&flock, l, sizeof(flock))) | |
1652 | goto out; | |
1653 | ||
1654 | inode = filp->f_dentry->d_inode; | |
1655 | ||
1656 | /* Don't allow mandatory locks on files that may be memory mapped | |
1657 | * and shared. | |
1658 | */ | |
1659 | if (IS_MANDLOCK(inode) && | |
1660 | (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && | |
1661 | mapping_writably_mapped(filp->f_mapping)) { | |
1662 | error = -EAGAIN; | |
1663 | goto out; | |
1664 | } | |
1665 | ||
c293621b | 1666 | again: |
1da177e4 LT |
1667 | error = flock_to_posix_lock(filp, file_lock, &flock); |
1668 | if (error) | |
1669 | goto out; | |
1670 | if (cmd == F_SETLKW) { | |
1671 | file_lock->fl_flags |= FL_SLEEP; | |
1672 | } | |
1673 | ||
1674 | error = -EBADF; | |
1675 | switch (flock.l_type) { | |
1676 | case F_RDLCK: | |
1677 | if (!(filp->f_mode & FMODE_READ)) | |
1678 | goto out; | |
1679 | break; | |
1680 | case F_WRLCK: | |
1681 | if (!(filp->f_mode & FMODE_WRITE)) | |
1682 | goto out; | |
1683 | break; | |
1684 | case F_UNLCK: | |
1685 | break; | |
1686 | default: | |
1687 | error = -EINVAL; | |
1688 | goto out; | |
1689 | } | |
1690 | ||
1691 | error = security_file_lock(filp, file_lock->fl_type); | |
1692 | if (error) | |
1693 | goto out; | |
1694 | ||
c293621b | 1695 | if (filp->f_op && filp->f_op->lock != NULL) |
1da177e4 | 1696 | error = filp->f_op->lock(filp, cmd, file_lock); |
c293621b PS |
1697 | else { |
1698 | for (;;) { | |
5842add2 | 1699 | error = posix_lock_file(filp, file_lock); |
c293621b PS |
1700 | if ((error != -EAGAIN) || (cmd == F_SETLK)) |
1701 | break; | |
1702 | error = wait_event_interruptible(file_lock->fl_wait, | |
1703 | !file_lock->fl_next); | |
1704 | if (!error) | |
1705 | continue; | |
1da177e4 | 1706 | |
c293621b | 1707 | locks_delete_block(file_lock); |
1da177e4 | 1708 | break; |
c293621b PS |
1709 | } |
1710 | } | |
1da177e4 | 1711 | |
c293621b PS |
1712 | /* |
1713 | * Attempt to detect a close/fcntl race and recover by | |
1714 | * releasing the lock that was just acquired. | |
1715 | */ | |
1716 | if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { | |
1717 | flock.l_type = F_UNLCK; | |
1718 | goto again; | |
1da177e4 LT |
1719 | } |
1720 | ||
c293621b | 1721 | out: |
1da177e4 LT |
1722 | locks_free_lock(file_lock); |
1723 | return error; | |
1724 | } | |
1725 | ||
1726 | #if BITS_PER_LONG == 32 | |
1727 | /* Report the first existing lock that would conflict with l. | |
1728 | * This implements the F_GETLK command of fcntl(). | |
1729 | */ | |
1730 | int fcntl_getlk64(struct file *filp, struct flock64 __user *l) | |
1731 | { | |
8dc7c311 | 1732 | struct file_lock *fl, cfl, file_lock; |
1da177e4 LT |
1733 | struct flock64 flock; |
1734 | int error; | |
1735 | ||
1736 | error = -EFAULT; | |
1737 | if (copy_from_user(&flock, l, sizeof(flock))) | |
1738 | goto out; | |
1739 | error = -EINVAL; | |
1740 | if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) | |
1741 | goto out; | |
1742 | ||
1743 | error = flock64_to_posix_lock(filp, &file_lock, &flock); | |
1744 | if (error) | |
1745 | goto out; | |
1746 | ||
1747 | if (filp->f_op && filp->f_op->lock) { | |
1748 | error = filp->f_op->lock(filp, F_GETLK, &file_lock); | |
80fec4c6 TM |
1749 | if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) |
1750 | file_lock.fl_ops->fl_release_private(&file_lock); | |
1da177e4 LT |
1751 | if (error < 0) |
1752 | goto out; | |
1753 | else | |
1754 | fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock); | |
1755 | } else { | |
8dc7c311 | 1756 | fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL); |
1da177e4 LT |
1757 | } |
1758 | ||
1759 | flock.l_type = F_UNLCK; | |
1760 | if (fl != NULL) { | |
1761 | flock.l_pid = fl->fl_pid; | |
1762 | flock.l_start = fl->fl_start; | |
1763 | flock.l_len = fl->fl_end == OFFSET_MAX ? 0 : | |
1764 | fl->fl_end - fl->fl_start + 1; | |
1765 | flock.l_whence = 0; | |
1766 | flock.l_type = fl->fl_type; | |
1767 | } | |
1768 | error = -EFAULT; | |
1769 | if (!copy_to_user(l, &flock, sizeof(flock))) | |
1770 | error = 0; | |
1771 | ||
1772 | out: | |
1773 | return error; | |
1774 | } | |
1775 | ||
1776 | /* Apply the lock described by l to an open file descriptor. | |
1777 | * This implements both the F_SETLK and F_SETLKW commands of fcntl(). | |
1778 | */ | |
c293621b PS |
1779 | int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, |
1780 | struct flock64 __user *l) | |
1da177e4 LT |
1781 | { |
1782 | struct file_lock *file_lock = locks_alloc_lock(); | |
1783 | struct flock64 flock; | |
1784 | struct inode *inode; | |
1785 | int error; | |
1786 | ||
1787 | if (file_lock == NULL) | |
1788 | return -ENOLCK; | |
1789 | ||
1790 | /* | |
1791 | * This might block, so we do it before checking the inode. | |
1792 | */ | |
1793 | error = -EFAULT; | |
1794 | if (copy_from_user(&flock, l, sizeof(flock))) | |
1795 | goto out; | |
1796 | ||
1797 | inode = filp->f_dentry->d_inode; | |
1798 | ||
1799 | /* Don't allow mandatory locks on files that may be memory mapped | |
1800 | * and shared. | |
1801 | */ | |
1802 | if (IS_MANDLOCK(inode) && | |
1803 | (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && | |
1804 | mapping_writably_mapped(filp->f_mapping)) { | |
1805 | error = -EAGAIN; | |
1806 | goto out; | |
1807 | } | |
1808 | ||
c293621b | 1809 | again: |
1da177e4 LT |
1810 | error = flock64_to_posix_lock(filp, file_lock, &flock); |
1811 | if (error) | |
1812 | goto out; | |
1813 | if (cmd == F_SETLKW64) { | |
1814 | file_lock->fl_flags |= FL_SLEEP; | |
1815 | } | |
1816 | ||
1817 | error = -EBADF; | |
1818 | switch (flock.l_type) { | |
1819 | case F_RDLCK: | |
1820 | if (!(filp->f_mode & FMODE_READ)) | |
1821 | goto out; | |
1822 | break; | |
1823 | case F_WRLCK: | |
1824 | if (!(filp->f_mode & FMODE_WRITE)) | |
1825 | goto out; | |
1826 | break; | |
1827 | case F_UNLCK: | |
1828 | break; | |
1829 | default: | |
1830 | error = -EINVAL; | |
1831 | goto out; | |
1832 | } | |
1833 | ||
1834 | error = security_file_lock(filp, file_lock->fl_type); | |
1835 | if (error) | |
1836 | goto out; | |
1837 | ||
c293621b | 1838 | if (filp->f_op && filp->f_op->lock != NULL) |
1da177e4 | 1839 | error = filp->f_op->lock(filp, cmd, file_lock); |
c293621b PS |
1840 | else { |
1841 | for (;;) { | |
5842add2 | 1842 | error = posix_lock_file(filp, file_lock); |
c293621b PS |
1843 | if ((error != -EAGAIN) || (cmd == F_SETLK64)) |
1844 | break; | |
1845 | error = wait_event_interruptible(file_lock->fl_wait, | |
1846 | !file_lock->fl_next); | |
1847 | if (!error) | |
1848 | continue; | |
1da177e4 | 1849 | |
c293621b | 1850 | locks_delete_block(file_lock); |
1da177e4 | 1851 | break; |
c293621b PS |
1852 | } |
1853 | } | |
1da177e4 | 1854 | |
c293621b PS |
1855 | /* |
1856 | * Attempt to detect a close/fcntl race and recover by | |
1857 | * releasing the lock that was just acquired. | |
1858 | */ | |
1859 | if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { | |
1860 | flock.l_type = F_UNLCK; | |
1861 | goto again; | |
1da177e4 LT |
1862 | } |
1863 | ||
1864 | out: | |
1865 | locks_free_lock(file_lock); | |
1866 | return error; | |
1867 | } | |
1868 | #endif /* BITS_PER_LONG == 32 */ | |
1869 | ||
1870 | /* | |
1871 | * This function is called when the file is being removed | |
1872 | * from the task's fd array. POSIX locks belonging to this task | |
1873 | * are deleted at this time. | |
1874 | */ | |
1875 | void locks_remove_posix(struct file *filp, fl_owner_t owner) | |
1876 | { | |
1877 | struct file_lock lock, **before; | |
1878 | ||
1879 | /* | |
1880 | * If there are no locks held on this file, we don't need to call | |
1881 | * posix_lock_file(). Another process could be setting a lock on this | |
1882 | * file at the same time, but we wouldn't remove that lock anyway. | |
1883 | */ | |
1884 | before = &filp->f_dentry->d_inode->i_flock; | |
1885 | if (*before == NULL) | |
1886 | return; | |
1887 | ||
1888 | lock.fl_type = F_UNLCK; | |
1889 | lock.fl_flags = FL_POSIX; | |
1890 | lock.fl_start = 0; | |
1891 | lock.fl_end = OFFSET_MAX; | |
1892 | lock.fl_owner = owner; | |
1893 | lock.fl_pid = current->tgid; | |
1894 | lock.fl_file = filp; | |
1895 | lock.fl_ops = NULL; | |
1896 | lock.fl_lmops = NULL; | |
1897 | ||
1898 | if (filp->f_op && filp->f_op->lock != NULL) { | |
1899 | filp->f_op->lock(filp, F_SETLK, &lock); | |
1900 | goto out; | |
1901 | } | |
1902 | ||
1903 | /* Can't use posix_lock_file here; we need to remove it no matter | |
1904 | * which pid we have. | |
1905 | */ | |
1906 | lock_kernel(); | |
1907 | while (*before != NULL) { | |
1908 | struct file_lock *fl = *before; | |
1909 | if (IS_POSIX(fl) && posix_same_owner(fl, &lock)) { | |
1910 | locks_delete_lock(before); | |
1911 | continue; | |
1912 | } | |
1913 | before = &fl->fl_next; | |
1914 | } | |
1915 | unlock_kernel(); | |
1916 | out: | |
1917 | if (lock.fl_ops && lock.fl_ops->fl_release_private) | |
1918 | lock.fl_ops->fl_release_private(&lock); | |
1919 | } | |
1920 | ||
1921 | EXPORT_SYMBOL(locks_remove_posix); | |
1922 | ||
1923 | /* | |
1924 | * This function is called on the last close of an open file. | |
1925 | */ | |
1926 | void locks_remove_flock(struct file *filp) | |
1927 | { | |
1928 | struct inode * inode = filp->f_dentry->d_inode; | |
1929 | struct file_lock *fl; | |
1930 | struct file_lock **before; | |
1931 | ||
1932 | if (!inode->i_flock) | |
1933 | return; | |
1934 | ||
1935 | if (filp->f_op && filp->f_op->flock) { | |
1936 | struct file_lock fl = { | |
1937 | .fl_pid = current->tgid, | |
1938 | .fl_file = filp, | |
1939 | .fl_flags = FL_FLOCK, | |
1940 | .fl_type = F_UNLCK, | |
1941 | .fl_end = OFFSET_MAX, | |
1942 | }; | |
1943 | filp->f_op->flock(filp, F_SETLKW, &fl); | |
80fec4c6 TM |
1944 | if (fl.fl_ops && fl.fl_ops->fl_release_private) |
1945 | fl.fl_ops->fl_release_private(&fl); | |
1da177e4 LT |
1946 | } |
1947 | ||
1948 | lock_kernel(); | |
1949 | before = &inode->i_flock; | |
1950 | ||
1951 | while ((fl = *before) != NULL) { | |
1952 | if (fl->fl_file == filp) { | |
c293621b | 1953 | if (IS_FLOCK(fl)) { |
1da177e4 LT |
1954 | locks_delete_lock(before); |
1955 | continue; | |
1956 | } | |
1957 | if (IS_LEASE(fl)) { | |
1958 | lease_modify(before, F_UNLCK); | |
1959 | continue; | |
1960 | } | |
1961 | /* What? */ | |
1962 | BUG(); | |
1963 | } | |
1964 | before = &fl->fl_next; | |
1965 | } | |
1966 | unlock_kernel(); | |
1967 | } | |
1968 | ||
1da177e4 LT |
1969 | /** |
1970 | * posix_unblock_lock - stop waiting for a file lock | |
1971 | * @filp: how the file was opened | |
1972 | * @waiter: the lock which was waiting | |
1973 | * | |
1974 | * lockd needs to block waiting for locks. | |
1975 | */ | |
64a318ee | 1976 | int |
1da177e4 LT |
1977 | posix_unblock_lock(struct file *filp, struct file_lock *waiter) |
1978 | { | |
64a318ee BF |
1979 | int status = 0; |
1980 | ||
1da177e4 | 1981 | lock_kernel(); |
5996a298 | 1982 | if (waiter->fl_next) |
1da177e4 | 1983 | __locks_delete_block(waiter); |
64a318ee BF |
1984 | else |
1985 | status = -ENOENT; | |
5996a298 | 1986 | unlock_kernel(); |
64a318ee | 1987 | return status; |
1da177e4 LT |
1988 | } |
1989 | ||
1990 | EXPORT_SYMBOL(posix_unblock_lock); | |
1991 | ||
1992 | static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx) | |
1993 | { | |
1994 | struct inode *inode = NULL; | |
1995 | ||
1996 | if (fl->fl_file != NULL) | |
1997 | inode = fl->fl_file->f_dentry->d_inode; | |
1998 | ||
1999 | out += sprintf(out, "%d:%s ", id, pfx); | |
2000 | if (IS_POSIX(fl)) { | |
2001 | out += sprintf(out, "%6s %s ", | |
2002 | (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ", | |
2003 | (inode == NULL) ? "*NOINODE*" : | |
2004 | (IS_MANDLOCK(inode) && | |
2005 | (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ? | |
2006 | "MANDATORY" : "ADVISORY "); | |
2007 | } else if (IS_FLOCK(fl)) { | |
2008 | if (fl->fl_type & LOCK_MAND) { | |
2009 | out += sprintf(out, "FLOCK MSNFS "); | |
2010 | } else { | |
2011 | out += sprintf(out, "FLOCK ADVISORY "); | |
2012 | } | |
2013 | } else if (IS_LEASE(fl)) { | |
2014 | out += sprintf(out, "LEASE "); | |
2015 | if (fl->fl_type & F_INPROGRESS) | |
2016 | out += sprintf(out, "BREAKING "); | |
2017 | else if (fl->fl_file) | |
2018 | out += sprintf(out, "ACTIVE "); | |
2019 | else | |
2020 | out += sprintf(out, "BREAKER "); | |
2021 | } else { | |
2022 | out += sprintf(out, "UNKNOWN UNKNOWN "); | |
2023 | } | |
2024 | if (fl->fl_type & LOCK_MAND) { | |
2025 | out += sprintf(out, "%s ", | |
2026 | (fl->fl_type & LOCK_READ) | |
2027 | ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " | |
2028 | : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); | |
2029 | } else { | |
2030 | out += sprintf(out, "%s ", | |
2031 | (fl->fl_type & F_INPROGRESS) | |
2032 | ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ " | |
2033 | : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ "); | |
2034 | } | |
2035 | if (inode) { | |
2036 | #ifdef WE_CAN_BREAK_LSLK_NOW | |
2037 | out += sprintf(out, "%d %s:%ld ", fl->fl_pid, | |
2038 | inode->i_sb->s_id, inode->i_ino); | |
2039 | #else | |
2040 | /* userspace relies on this representation of dev_t ;-( */ | |
2041 | out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid, | |
2042 | MAJOR(inode->i_sb->s_dev), | |
2043 | MINOR(inode->i_sb->s_dev), inode->i_ino); | |
2044 | #endif | |
2045 | } else { | |
2046 | out += sprintf(out, "%d <none>:0 ", fl->fl_pid); | |
2047 | } | |
2048 | if (IS_POSIX(fl)) { | |
2049 | if (fl->fl_end == OFFSET_MAX) | |
2050 | out += sprintf(out, "%Ld EOF\n", fl->fl_start); | |
2051 | else | |
2052 | out += sprintf(out, "%Ld %Ld\n", fl->fl_start, | |
2053 | fl->fl_end); | |
2054 | } else { | |
2055 | out += sprintf(out, "0 EOF\n"); | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | static void move_lock_status(char **p, off_t* pos, off_t offset) | |
2060 | { | |
2061 | int len; | |
2062 | len = strlen(*p); | |
2063 | if(*pos >= offset) { | |
2064 | /* the complete line is valid */ | |
2065 | *p += len; | |
2066 | *pos += len; | |
2067 | return; | |
2068 | } | |
2069 | if(*pos+len > offset) { | |
2070 | /* use the second part of the line */ | |
2071 | int i = offset-*pos; | |
2072 | memmove(*p,*p+i,len-i); | |
2073 | *p += len-i; | |
2074 | *pos += len; | |
2075 | return; | |
2076 | } | |
2077 | /* discard the complete line */ | |
2078 | *pos += len; | |
2079 | } | |
2080 | ||
2081 | /** | |
2082 | * get_locks_status - reports lock usage in /proc/locks | |
2083 | * @buffer: address in userspace to write into | |
2084 | * @start: ? | |
2085 | * @offset: how far we are through the buffer | |
2086 | * @length: how much to read | |
2087 | */ | |
2088 | ||
2089 | int get_locks_status(char *buffer, char **start, off_t offset, int length) | |
2090 | { | |
2091 | struct list_head *tmp; | |
2092 | char *q = buffer; | |
2093 | off_t pos = 0; | |
2094 | int i = 0; | |
2095 | ||
2096 | lock_kernel(); | |
2097 | list_for_each(tmp, &file_lock_list) { | |
2098 | struct list_head *btmp; | |
2099 | struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link); | |
2100 | lock_get_status(q, fl, ++i, ""); | |
2101 | move_lock_status(&q, &pos, offset); | |
2102 | ||
2103 | if(pos >= offset+length) | |
2104 | goto done; | |
2105 | ||
2106 | list_for_each(btmp, &fl->fl_block) { | |
2107 | struct file_lock *bfl = list_entry(btmp, | |
2108 | struct file_lock, fl_block); | |
2109 | lock_get_status(q, bfl, i, " ->"); | |
2110 | move_lock_status(&q, &pos, offset); | |
2111 | ||
2112 | if(pos >= offset+length) | |
2113 | goto done; | |
2114 | } | |
2115 | } | |
2116 | done: | |
2117 | unlock_kernel(); | |
2118 | *start = buffer; | |
2119 | if(q-buffer < length) | |
2120 | return (q-buffer); | |
2121 | return length; | |
2122 | } | |
2123 | ||
2124 | /** | |
2125 | * lock_may_read - checks that the region is free of locks | |
2126 | * @inode: the inode that is being read | |
2127 | * @start: the first byte to read | |
2128 | * @len: the number of bytes to read | |
2129 | * | |
2130 | * Emulates Windows locking requirements. Whole-file | |
2131 | * mandatory locks (share modes) can prohibit a read and | |
2132 | * byte-range POSIX locks can prohibit a read if they overlap. | |
2133 | * | |
2134 | * N.B. this function is only ever called | |
2135 | * from knfsd and ownership of locks is never checked. | |
2136 | */ | |
2137 | int lock_may_read(struct inode *inode, loff_t start, unsigned long len) | |
2138 | { | |
2139 | struct file_lock *fl; | |
2140 | int result = 1; | |
2141 | lock_kernel(); | |
2142 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { | |
2143 | if (IS_POSIX(fl)) { | |
2144 | if (fl->fl_type == F_RDLCK) | |
2145 | continue; | |
2146 | if ((fl->fl_end < start) || (fl->fl_start > (start + len))) | |
2147 | continue; | |
2148 | } else if (IS_FLOCK(fl)) { | |
2149 | if (!(fl->fl_type & LOCK_MAND)) | |
2150 | continue; | |
2151 | if (fl->fl_type & LOCK_READ) | |
2152 | continue; | |
2153 | } else | |
2154 | continue; | |
2155 | result = 0; | |
2156 | break; | |
2157 | } | |
2158 | unlock_kernel(); | |
2159 | return result; | |
2160 | } | |
2161 | ||
2162 | EXPORT_SYMBOL(lock_may_read); | |
2163 | ||
2164 | /** | |
2165 | * lock_may_write - checks that the region is free of locks | |
2166 | * @inode: the inode that is being written | |
2167 | * @start: the first byte to write | |
2168 | * @len: the number of bytes to write | |
2169 | * | |
2170 | * Emulates Windows locking requirements. Whole-file | |
2171 | * mandatory locks (share modes) can prohibit a write and | |
2172 | * byte-range POSIX locks can prohibit a write if they overlap. | |
2173 | * | |
2174 | * N.B. this function is only ever called | |
2175 | * from knfsd and ownership of locks is never checked. | |
2176 | */ | |
2177 | int lock_may_write(struct inode *inode, loff_t start, unsigned long len) | |
2178 | { | |
2179 | struct file_lock *fl; | |
2180 | int result = 1; | |
2181 | lock_kernel(); | |
2182 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { | |
2183 | if (IS_POSIX(fl)) { | |
2184 | if ((fl->fl_end < start) || (fl->fl_start > (start + len))) | |
2185 | continue; | |
2186 | } else if (IS_FLOCK(fl)) { | |
2187 | if (!(fl->fl_type & LOCK_MAND)) | |
2188 | continue; | |
2189 | if (fl->fl_type & LOCK_WRITE) | |
2190 | continue; | |
2191 | } else | |
2192 | continue; | |
2193 | result = 0; | |
2194 | break; | |
2195 | } | |
2196 | unlock_kernel(); | |
2197 | return result; | |
2198 | } | |
2199 | ||
2200 | EXPORT_SYMBOL(lock_may_write); | |
2201 | ||
2202 | static inline void __steal_locks(struct file *file, fl_owner_t from) | |
2203 | { | |
2204 | struct inode *inode = file->f_dentry->d_inode; | |
2205 | struct file_lock *fl = inode->i_flock; | |
2206 | ||
2207 | while (fl) { | |
2208 | if (fl->fl_file == file && fl->fl_owner == from) | |
2209 | fl->fl_owner = current->files; | |
2210 | fl = fl->fl_next; | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | /* When getting ready for executing a binary, we make sure that current | |
2215 | * has a files_struct on its own. Before dropping the old files_struct, | |
2216 | * we take over ownership of all locks for all file descriptors we own. | |
2217 | * Note that we may accidentally steal a lock for a file that a sibling | |
2218 | * has created since the unshare_files() call. | |
2219 | */ | |
2220 | void steal_locks(fl_owner_t from) | |
2221 | { | |
2222 | struct files_struct *files = current->files; | |
2223 | int i, j; | |
badf1662 | 2224 | struct fdtable *fdt; |
1da177e4 LT |
2225 | |
2226 | if (from == files) | |
2227 | return; | |
2228 | ||
2229 | lock_kernel(); | |
2230 | j = 0; | |
4fb3a538 | 2231 | rcu_read_lock(); |
badf1662 | 2232 | fdt = files_fdtable(files); |
1da177e4 LT |
2233 | for (;;) { |
2234 | unsigned long set; | |
2235 | i = j * __NFDBITS; | |
badf1662 | 2236 | if (i >= fdt->max_fdset || i >= fdt->max_fds) |
1da177e4 | 2237 | break; |
badf1662 | 2238 | set = fdt->open_fds->fds_bits[j++]; |
1da177e4 LT |
2239 | while (set) { |
2240 | if (set & 1) { | |
badf1662 | 2241 | struct file *file = fdt->fd[i]; |
1da177e4 LT |
2242 | if (file) |
2243 | __steal_locks(file, from); | |
2244 | } | |
2245 | i++; | |
2246 | set >>= 1; | |
2247 | } | |
2248 | } | |
4fb3a538 | 2249 | rcu_read_unlock(); |
1da177e4 LT |
2250 | unlock_kernel(); |
2251 | } | |
2252 | EXPORT_SYMBOL(steal_locks); | |
2253 | ||
2254 | static int __init filelock_init(void) | |
2255 | { | |
2256 | filelock_cache = kmem_cache_create("file_lock_cache", | |
2257 | sizeof(struct file_lock), 0, SLAB_PANIC, | |
2258 | init_once, NULL); | |
2259 | return 0; | |
2260 | } | |
2261 | ||
2262 | core_initcall(filelock_init); |