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