This should hopefully fix the warnings reported.
[linux-2.6/linux-mips.git] / fs / locks.c
blob1661a4a5c80ed85631bae645f1a31e5a59be1df3
1 /*
2 * linux/fs/locks.c
4 * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
5 * Doug Evans (dje@spiff.uucp), August 07, 1992
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.
12 * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
13 * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
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
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
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
31 * Implemented two lock personalities - FL_FLOCK and FL_POSIX.
33 * FL_POSIX locks are created with calls to fcntl() and lockf() through the
34 * fcntl() system call. They have the semantics described above.
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.
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).
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
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.
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.
61 * Initial implementation of mandatory locks. SunOS turned out to be
62 * a rotten model, so I implemented the "obvious" semantics.
63 * See 'linux/Documentation/mandatory.txt' for details.
64 * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
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.
72 * Tidied up block list handling. Added '/proc/locks' interface.
73 * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
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.
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.
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.
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.
94 * Made the block list a circular list to minimise searching in the list.
95 * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
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.
101 * Some adaptations for NFS support.
102 * Olaf Kirch (okir@monad.swb.de), Dec 1996,
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.
108 #include <linux/malloc.h>
109 #include <linux/file.h>
110 #include <linux/smp_lock.h>
112 #include <asm/uaccess.h>
114 #define OFFSET_MAX ((off_t)LONG_MAX) /* FIXME: move elsewhere? */
116 static int flock_make_lock(struct file *filp, struct file_lock *fl,
117 unsigned int cmd);
118 static int posix_make_lock(struct file *filp, struct file_lock *fl,
119 struct flock *l);
120 static int flock_locks_conflict(struct file_lock *caller_fl,
121 struct file_lock *sys_fl);
122 static int posix_locks_conflict(struct file_lock *caller_fl,
123 struct file_lock *sys_fl);
124 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
125 static int flock_lock_file(struct file *filp, struct file_lock *caller,
126 unsigned int wait);
127 static int posix_locks_deadlock(struct file_lock *caller,
128 struct file_lock *blocker);
130 static struct file_lock *locks_empty_lock(void);
131 static struct file_lock *locks_init_lock(struct file_lock *,
132 struct file_lock *);
133 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
134 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait);
135 static char *lock_get_status(struct file_lock *fl, int id, char *pfx);
137 static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter);
138 static void locks_delete_block(struct file_lock *blocker, struct file_lock *waiter);
139 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait);
141 struct file_lock *file_lock_table = NULL;
143 /* Allocate a new lock, and initialize its fields from fl.
144 * The lock is not inserted into any lists until locks_insert_lock() or
145 * locks_insert_block() are called.
147 static inline struct file_lock *locks_alloc_lock(struct file_lock *fl)
149 return locks_init_lock(locks_empty_lock(), fl);
152 /* Free lock not inserted in any queue.
154 static inline void locks_free_lock(struct file_lock *fl)
156 if (waitqueue_active(&fl->fl_wait))
157 panic("Attempting to free lock with active wait queue");
159 if (fl->fl_nextblock != NULL || fl->fl_prevblock != NULL)
160 panic("Attempting to free lock with active block list");
162 kfree(fl);
163 return;
166 /* Check if two locks overlap each other.
168 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
170 return ((fl1->fl_end >= fl2->fl_start) &&
171 (fl2->fl_end >= fl1->fl_start));
175 * Check whether two locks have the same owner
176 * N.B. Do we need the test on PID as well as owner?
177 * (Clone tasks should be considered as one "owner".)
179 static inline int
180 locks_same_owner(struct file_lock *fl1, struct file_lock *fl2)
182 return (fl1->fl_owner == fl2->fl_owner) &&
183 (fl1->fl_pid == fl2->fl_pid);
186 /* Insert waiter into blocker's block list.
187 * We use a circular list so that processes can be easily woken up in
188 * the order they blocked. The documentation doesn't require this but
189 * it seems like the reasonable thing to do.
191 static void locks_insert_block(struct file_lock *blocker,
192 struct file_lock *waiter)
194 struct file_lock *prevblock;
196 if (waiter->fl_prevblock) {
197 printk(KERN_ERR "locks_insert_block: remove duplicated lock "
198 "(pid=%d %ld-%ld type=%d)\n",
199 waiter->fl_pid, waiter->fl_start,
200 waiter->fl_end, waiter->fl_type);
201 locks_delete_block(waiter->fl_prevblock, waiter);
204 if (blocker->fl_prevblock == NULL)
205 /* No previous waiters - list is empty */
206 prevblock = blocker;
207 else
208 /* Previous waiters exist - add to end of list */
209 prevblock = blocker->fl_prevblock;
211 prevblock->fl_nextblock = waiter;
212 blocker->fl_prevblock = waiter;
213 waiter->fl_nextblock = blocker;
214 waiter->fl_prevblock = prevblock;
216 return;
219 /* Remove waiter from blocker's block list.
220 * When blocker ends up pointing to itself then the list is empty.
222 static void locks_delete_block(struct file_lock *blocker,
223 struct file_lock *waiter)
225 struct file_lock *nextblock;
226 struct file_lock *prevblock;
228 nextblock = waiter->fl_nextblock;
229 prevblock = waiter->fl_prevblock;
231 if (nextblock == NULL)
232 return;
234 nextblock->fl_prevblock = prevblock;
235 prevblock->fl_nextblock = nextblock;
237 waiter->fl_prevblock = waiter->fl_nextblock = NULL;
238 if (blocker->fl_nextblock == blocker)
239 /* No more locks on blocker's blocked list */
240 blocker->fl_prevblock = blocker->fl_nextblock = NULL;
241 return;
244 /* The following two are for the benefit of lockd.
246 void
247 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
249 locks_insert_block(blocker, waiter);
250 return;
253 void
254 posix_unblock_lock(struct file_lock *waiter)
256 if (waiter->fl_prevblock)
257 locks_delete_block(waiter->fl_prevblock, waiter);
258 return;
261 /* Wake up processes blocked waiting for blocker.
262 * If told to wait then schedule the processes until the block list
263 * is empty, otherwise empty the block list ourselves.
265 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
267 struct file_lock *waiter;
269 while ((waiter = blocker->fl_nextblock) != NULL) {
270 /* N.B. Is it possible for the notify function to block?? */
271 if (waiter->fl_notify)
272 waiter->fl_notify(waiter);
273 wake_up(&waiter->fl_wait);
274 if (wait) {
275 /* Let the blocked process remove waiter from the
276 * block list when it gets scheduled.
278 current->policy |= SCHED_YIELD;
279 schedule();
280 } else {
281 /* Remove waiter from the block list, because by the
282 * time it wakes up blocker won't exist any more.
284 locks_delete_block(blocker, waiter);
287 return;
290 /* flock() system call entry point. Apply a FL_FLOCK style lock to
291 * an open file descriptor.
293 asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
295 struct file_lock file_lock;
296 struct file *filp;
297 int error;
299 lock_kernel();
300 error = -EBADF;
301 filp = fget(fd);
302 if (!filp)
303 goto out;
304 error = -EINVAL;
305 if (!flock_make_lock(filp, &file_lock, cmd))
306 goto out_putf;
307 error = -EBADF;
308 if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
309 goto out_putf;
310 error = flock_lock_file(filp, &file_lock,
311 (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
312 out_putf:
313 fput(filp);
314 out:
315 unlock_kernel();
316 return (error);
319 /* Report the first existing lock that would conflict with l.
320 * This implements the F_GETLK command of fcntl().
322 int fcntl_getlk(unsigned int fd, struct flock *l)
324 struct file *filp;
325 struct file_lock *fl,file_lock;
326 struct flock flock;
327 int error;
329 error = -EFAULT;
330 if (copy_from_user(&flock, l, sizeof(flock)))
331 goto out;
332 error = -EINVAL;
333 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
334 goto out;
336 error = -EBADF;
337 filp = fget(fd);
338 if (!filp)
339 goto out;
341 error = -EINVAL;
342 if (!filp->f_dentry || !filp->f_dentry->d_inode)
343 goto out_putf;
345 if (!posix_make_lock(filp, &file_lock, &flock))
346 goto out_putf;
348 if (filp->f_op->lock) {
349 error = filp->f_op->lock(filp, F_GETLK, &file_lock);
350 if (error < 0)
351 goto out_putf;
352 else if (error == LOCK_USE_CLNT)
353 /* Bypass for NFS with no locking - 2.0.36 compat */
354 fl = posix_test_lock(filp, &file_lock);
355 else
356 fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
357 } else {
358 fl = posix_test_lock(filp, &file_lock);
361 flock.l_type = F_UNLCK;
362 if (fl != NULL) {
363 flock.l_pid = fl->fl_pid;
364 flock.l_start = fl->fl_start;
365 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
366 fl->fl_end - fl->fl_start + 1;
367 flock.l_whence = 0;
368 flock.l_type = fl->fl_type;
370 error = -EFAULT;
371 if (!copy_to_user(l, &flock, sizeof(flock)))
372 error = 0;
374 out_putf:
375 fput(filp);
376 out:
377 return error;
380 /* Apply the lock described by l to an open file descriptor.
381 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
383 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
385 struct file *filp;
386 struct file_lock file_lock;
387 struct flock flock;
388 struct dentry * dentry;
389 struct inode *inode;
390 int error;
393 * This might block, so we do it before checking the inode.
395 error = -EFAULT;
396 if (copy_from_user(&flock, l, sizeof(flock)))
397 goto out;
399 /* Get arguments and validate them ...
402 error = -EBADF;
403 filp = fget(fd);
404 if (!filp)
405 goto out;
407 error = -EINVAL;
408 if (!(dentry = filp->f_dentry))
409 goto out_putf;
410 if (!(inode = dentry->d_inode))
411 goto out_putf;
413 /* Don't allow mandatory locks on files that may be memory mapped
414 * and shared.
416 if (IS_MANDLOCK(inode) &&
417 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
418 struct vm_area_struct *vma;
419 struct address_space *mapping = inode->i_mapping;
420 spin_lock(&mapping->i_shared_lock);
421 for(vma = mapping->i_mmap;vma;vma = vma->vm_next_share) {
422 if (!(vma->vm_flags & VM_MAYSHARE))
423 continue;
424 spin_unlock(&mapping->i_shared_lock);
425 error = -EAGAIN;
426 goto out_putf;
428 spin_unlock(&mapping->i_shared_lock);
431 error = -EINVAL;
432 if (!posix_make_lock(filp, &file_lock, &flock))
433 goto out_putf;
435 error = -EBADF;
436 switch (flock.l_type) {
437 case F_RDLCK:
438 if (!(filp->f_mode & FMODE_READ))
439 goto out_putf;
440 break;
441 case F_WRLCK:
442 if (!(filp->f_mode & FMODE_WRITE))
443 goto out_putf;
444 break;
445 case F_UNLCK:
446 break;
447 case F_SHLCK:
448 case F_EXLCK:
449 #ifdef __sparc__
450 /* warn a bit for now, but don't overdo it */
452 static int count = 0;
453 if (!count) {
454 count=1;
455 printk(KERN_WARNING
456 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
457 current->pid, current->comm);
460 if (!(filp->f_mode & 3))
461 goto out_putf;
462 break;
463 #endif
464 default:
465 error = -EINVAL;
466 goto out_putf;
469 if (filp->f_op->lock != NULL) {
470 error = filp->f_op->lock(filp, cmd, &file_lock);
471 if (error < 0)
472 goto out_putf;
474 error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
476 out_putf:
477 fput(filp);
478 out:
479 return error;
483 * This function is called when the file is being removed
484 * from the task's fd array.
486 void locks_remove_posix(struct file *filp, fl_owner_t owner)
488 struct inode * inode = filp->f_dentry->d_inode;
489 struct file_lock file_lock, *fl;
490 struct file_lock **before;
493 * For POSIX locks we free all locks on this file for the given task.
495 repeat:
496 before = &inode->i_flock;
497 while ((fl = *before) != NULL) {
498 if ((fl->fl_flags & FL_POSIX) && fl->fl_owner == owner) {
499 int (*lock)(struct file *, int, struct file_lock *);
500 lock = filp->f_op->lock;
501 if (lock) {
502 file_lock = *fl;
503 file_lock.fl_type = F_UNLCK;
505 locks_delete_lock(before, 0);
506 if (lock) {
507 lock(filp, F_SETLK, &file_lock);
508 /* List may have changed: */
509 goto repeat;
511 continue;
513 before = &fl->fl_next;
518 * This function is called on the last close of an open file.
520 void locks_remove_flock(struct file *filp)
522 struct inode * inode = filp->f_dentry->d_inode;
523 struct file_lock file_lock, *fl;
524 struct file_lock **before;
526 repeat:
527 before = &inode->i_flock;
528 while ((fl = *before) != NULL) {
529 if ((fl->fl_flags & FL_FLOCK) && fl->fl_file == filp) {
530 int (*lock)(struct file *, int, struct file_lock *);
531 lock = NULL;
532 if (filp->f_op)
533 lock = filp->f_op->lock;
534 if (lock) {
535 file_lock = *fl;
536 file_lock.fl_type = F_UNLCK;
538 locks_delete_lock(before, 0);
539 if (lock) {
540 lock(filp, F_SETLK, &file_lock);
541 /* List may have changed: */
542 goto repeat;
544 continue;
546 before = &fl->fl_next;
550 struct file_lock *
551 posix_test_lock(struct file *filp, struct file_lock *fl)
553 struct file_lock *cfl;
555 for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
556 if (!(cfl->fl_flags & FL_POSIX))
557 continue;
558 if (posix_locks_conflict(cfl, fl))
559 break;
562 return (cfl);
565 int locks_mandatory_locked(struct inode *inode)
567 fl_owner_t owner = current->files;
568 struct file_lock *fl;
571 * Search the lock list for this inode for any POSIX locks.
573 lock_kernel();
574 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
575 if (!(fl->fl_flags & FL_POSIX))
576 continue;
577 if (fl->fl_owner != owner)
578 break;
580 unlock_kernel();
581 return fl ? -EAGAIN : 0;
584 int locks_mandatory_area(int read_write, struct inode *inode,
585 struct file *filp, loff_t offset,
586 size_t count)
588 struct file_lock *fl;
589 struct file_lock tfl;
590 int error;
592 memset(&tfl, 0, sizeof(tfl));
594 tfl.fl_file = filp;
595 tfl.fl_flags = FL_POSIX | FL_ACCESS;
596 tfl.fl_owner = current->files;
597 tfl.fl_pid = current->pid;
598 init_waitqueue_head(&tfl.fl_wait);
599 tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
600 tfl.fl_start = offset;
601 tfl.fl_end = offset + count - 1;
603 error = 0;
604 lock_kernel();
606 repeat:
607 /* Search the lock list for this inode for locks that conflict with
608 * the proposed read/write.
610 for (fl = inode->i_flock; ; fl = fl->fl_next) {
611 error = 0;
612 if (!fl)
613 break;
614 if (!(fl->fl_flags & FL_POSIX))
615 continue;
616 /* Block for writes against a "read" lock,
617 * and both reads and writes against a "write" lock.
619 if (posix_locks_conflict(fl, &tfl)) {
620 error = -EAGAIN;
621 if (filp && (filp->f_flags & O_NONBLOCK))
622 break;
623 error = -ERESTARTSYS;
624 if (signal_pending(current))
625 break;
626 error = -EDEADLK;
627 if (posix_locks_deadlock(&tfl, fl))
628 break;
630 locks_insert_block(fl, &tfl);
631 interruptible_sleep_on(&tfl.fl_wait);
632 locks_delete_block(fl, &tfl);
635 * If we've been sleeping someone might have
636 * changed the permissions behind our back.
638 if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
639 break;
640 goto repeat;
643 unlock_kernel();
644 return error;
647 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
648 * style lock.
650 static int posix_make_lock(struct file *filp, struct file_lock *fl,
651 struct flock *l)
653 off_t start;
655 memset(fl, 0, sizeof(*fl));
657 init_waitqueue_head(&fl->fl_wait);
658 fl->fl_flags = FL_POSIX;
660 switch (l->l_type) {
661 case F_RDLCK:
662 case F_WRLCK:
663 case F_UNLCK:
664 fl->fl_type = l->l_type;
665 break;
666 default:
667 return (0);
670 switch (l->l_whence) {
671 case 0: /*SEEK_SET*/
672 start = 0;
673 break;
674 case 1: /*SEEK_CUR*/
675 start = filp->f_pos;
676 break;
677 case 2: /*SEEK_END*/
678 start = filp->f_dentry->d_inode->i_size;
679 break;
680 default:
681 return (0);
684 if (((start += l->l_start) < 0) || (l->l_len < 0))
685 return (0);
686 fl->fl_start = start; /* we record the absolute position */
687 if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
688 fl->fl_end = OFFSET_MAX;
690 fl->fl_file = filp;
691 fl->fl_owner = current->files;
692 fl->fl_pid = current->pid;
694 return (1);
697 /* Verify a call to flock() and fill in a file_lock structure with
698 * an appropriate FLOCK lock.
700 static int flock_make_lock(struct file *filp, struct file_lock *fl,
701 unsigned int cmd)
703 memset(fl, 0, sizeof(*fl));
705 init_waitqueue_head(&fl->fl_wait);
706 if (!filp->f_dentry) /* just in case */
707 return (0);
709 switch (cmd & ~LOCK_NB) {
710 case LOCK_SH:
711 fl->fl_type = F_RDLCK;
712 break;
713 case LOCK_EX:
714 fl->fl_type = F_WRLCK;
715 break;
716 case LOCK_UN:
717 fl->fl_type = F_UNLCK;
718 break;
719 default:
720 return (0);
723 fl->fl_flags = FL_FLOCK;
724 fl->fl_start = 0;
725 fl->fl_end = OFFSET_MAX;
726 fl->fl_file = filp;
727 fl->fl_owner = NULL;
729 return (1);
732 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
733 * checking before calling the locks_conflict().
735 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
737 /* POSIX locks owned by the same process do not conflict with
738 * each other.
740 if (!(sys_fl->fl_flags & FL_POSIX) ||
741 locks_same_owner(caller_fl, sys_fl))
742 return (0);
744 return (locks_conflict(caller_fl, sys_fl));
747 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
748 * checking before calling the locks_conflict().
750 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
752 /* FLOCK locks referring to the same filp do not conflict with
753 * each other.
755 if (!(sys_fl->fl_flags & FL_FLOCK) ||
756 (caller_fl->fl_file == sys_fl->fl_file))
757 return (0);
759 return (locks_conflict(caller_fl, sys_fl));
762 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
763 * checks for overlapping locks and shared/exclusive status.
765 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
767 if (!locks_overlap(caller_fl, sys_fl))
768 return (0);
770 switch (caller_fl->fl_type) {
771 case F_RDLCK:
772 return (sys_fl->fl_type == F_WRLCK);
774 case F_WRLCK:
775 return (1);
777 default:
778 printk("locks_conflict(): impossible lock type - %d\n",
779 caller_fl->fl_type);
780 break;
782 return (0); /* This should never happen */
785 /* This function tests for deadlock condition before putting a process to
786 * sleep. The detection scheme is no longer recursive. Recursive was neat,
787 * but dangerous - we risked stack corruption if the lock data was bad, or
788 * if the recursion was too deep for any other reason.
790 * We rely on the fact that a task can only be on one lock's wait queue
791 * at a time. When we find blocked_task on a wait queue we can re-search
792 * with blocked_task equal to that queue's owner, until either blocked_task
793 * isn't found, or blocked_task is found on a queue owned by my_task.
795 * Note: the above assumption may not be true when handling lock requests
796 * from a broken NFS client. But broken NFS clients have a lot more to
797 * worry about than proper deadlock detection anyway... --okir
799 static int posix_locks_deadlock(struct file_lock *caller_fl,
800 struct file_lock *block_fl)
802 struct file_lock *fl;
803 struct file_lock *bfl;
804 void *caller_owner, *blocked_owner;
805 unsigned int caller_pid, blocked_pid;
807 caller_owner = caller_fl->fl_owner;
808 caller_pid = caller_fl->fl_pid;
809 blocked_owner = block_fl->fl_owner;
810 blocked_pid = block_fl->fl_pid;
812 next_task:
813 if (caller_owner == blocked_owner && caller_pid == blocked_pid)
814 return (1);
815 for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
816 if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
817 continue;
818 for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
819 if (bfl->fl_owner == blocked_owner &&
820 bfl->fl_pid == blocked_pid) {
821 if (fl->fl_owner == caller_owner &&
822 fl->fl_pid == caller_pid) {
823 return (1);
825 blocked_owner = fl->fl_owner;
826 blocked_pid = fl->fl_pid;
827 goto next_task;
831 return (0);
834 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
835 * the head of the list, but that's secret knowledge known only to the next
836 * two functions.
838 static int flock_lock_file(struct file *filp, struct file_lock *caller,
839 unsigned int wait)
841 struct file_lock *fl;
842 struct file_lock *new_fl = NULL;
843 struct file_lock **before;
844 struct inode * inode = filp->f_dentry->d_inode;
845 int error, change;
846 int unlock = (caller->fl_type == F_UNLCK);
849 * If we need a new lock, get it in advance to avoid races.
851 if (!unlock) {
852 error = -ENOLCK;
853 new_fl = locks_alloc_lock(caller);
854 if (!new_fl)
855 goto out;
858 error = 0;
859 search:
860 change = 0;
861 before = &inode->i_flock;
862 while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
863 if (caller->fl_file == fl->fl_file) {
864 if (caller->fl_type == fl->fl_type)
865 goto out;
866 change = 1;
867 break;
869 before = &fl->fl_next;
871 /* change means that we are changing the type of an existing lock, or
872 * or else unlocking it.
874 if (change) {
875 /* N.B. What if the wait argument is false? */
876 locks_delete_lock(before, !unlock);
878 * If we waited, another lock may have been added ...
880 if (!unlock)
881 goto search;
883 if (unlock)
884 goto out;
886 repeat:
887 /* Check signals each time we start */
888 error = -ERESTARTSYS;
889 if (signal_pending(current))
890 goto out;
891 for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
892 fl = fl->fl_next) {
893 if (!flock_locks_conflict(new_fl, fl))
894 continue;
895 error = -EAGAIN;
896 if (!wait)
897 goto out;
898 locks_insert_block(fl, new_fl);
899 interruptible_sleep_on(&new_fl->fl_wait);
900 locks_delete_block(fl, new_fl);
901 goto repeat;
903 locks_insert_lock(&inode->i_flock, new_fl);
904 new_fl = NULL;
905 error = 0;
907 out:
908 if (new_fl)
909 locks_free_lock(new_fl);
910 return error;
913 /* Add a POSIX style lock to a file.
914 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
915 * task, then by starting address
917 * Kai Petzke writes:
918 * To make freeing a lock much faster, we keep a pointer to the lock before the
919 * actual one. But the real gain of the new coding was, that lock_it() and
920 * unlock_it() became one function.
922 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
925 int posix_lock_file(struct file *filp, struct file_lock *caller,
926 unsigned int wait)
928 struct file_lock *fl;
929 struct file_lock *new_fl, *new_fl2;
930 struct file_lock *left = NULL;
931 struct file_lock *right = NULL;
932 struct file_lock **before;
933 struct inode * inode = filp->f_dentry->d_inode;
934 int error, added = 0;
937 * We may need two file_lock structures for this operation,
938 * so we get them in advance to avoid races.
940 new_fl = locks_empty_lock();
941 new_fl2 = locks_empty_lock();
942 error = -ENOLCK; /* "no luck" */
943 if (!(new_fl && new_fl2))
944 goto out;
946 if (caller->fl_type != F_UNLCK) {
947 repeat:
948 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
949 if (!(fl->fl_flags & FL_POSIX))
950 continue;
951 if (!posix_locks_conflict(caller, fl))
952 continue;
953 error = -EAGAIN;
954 if (!wait)
955 goto out;
956 error = -EDEADLK;
957 if (posix_locks_deadlock(caller, fl))
958 goto out;
959 error = -ERESTARTSYS;
960 if (signal_pending(current))
961 goto out;
962 locks_insert_block(fl, caller);
963 interruptible_sleep_on(&caller->fl_wait);
964 locks_delete_block(fl, caller);
965 goto repeat;
970 * We've allocated the new locks in advance, so there are no
971 * errors possible (and no blocking operations) from here on.
973 * Find the first old lock with the same owner as the new lock.
976 before = &inode->i_flock;
978 /* First skip locks owned by other processes.
980 while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
981 !locks_same_owner(caller, fl))) {
982 before = &fl->fl_next;
985 /* Process locks with this owner.
987 while ((fl = *before) && locks_same_owner(caller, fl)) {
988 /* Detect adjacent or overlapping regions (if same lock type)
990 if (caller->fl_type == fl->fl_type) {
991 if (fl->fl_end < caller->fl_start - 1)
992 goto next_lock;
993 /* If the next lock in the list has entirely bigger
994 * addresses than the new one, insert the lock here.
996 if (fl->fl_start > caller->fl_end + 1)
997 break;
999 /* If we come here, the new and old lock are of the
1000 * same type and adjacent or overlapping. Make one
1001 * lock yielding from the lower start address of both
1002 * locks to the higher end address.
1004 if (fl->fl_start > caller->fl_start)
1005 fl->fl_start = caller->fl_start;
1006 else
1007 caller->fl_start = fl->fl_start;
1008 if (fl->fl_end < caller->fl_end)
1009 fl->fl_end = caller->fl_end;
1010 else
1011 caller->fl_end = fl->fl_end;
1012 if (added) {
1013 locks_delete_lock(before, 0);
1014 continue;
1016 caller = fl;
1017 added = 1;
1019 else {
1020 /* Processing for different lock types is a bit
1021 * more complex.
1023 if (fl->fl_end < caller->fl_start)
1024 goto next_lock;
1025 if (fl->fl_start > caller->fl_end)
1026 break;
1027 if (caller->fl_type == F_UNLCK)
1028 added = 1;
1029 if (fl->fl_start < caller->fl_start)
1030 left = fl;
1031 /* If the next lock in the list has a higher end
1032 * address than the new one, insert the new one here.
1034 if (fl->fl_end > caller->fl_end) {
1035 right = fl;
1036 break;
1038 if (fl->fl_start >= caller->fl_start) {
1039 /* The new lock completely replaces an old
1040 * one (This may happen several times).
1042 if (added) {
1043 locks_delete_lock(before, 0);
1044 continue;
1046 /* Replace the old lock with the new one.
1047 * Wake up anybody waiting for the old one,
1048 * as the change in lock type might satisfy
1049 * their needs.
1051 locks_wake_up_blocks(fl, 0);
1052 fl->fl_start = caller->fl_start;
1053 fl->fl_end = caller->fl_end;
1054 fl->fl_type = caller->fl_type;
1055 fl->fl_u = caller->fl_u;
1056 caller = fl;
1057 added = 1;
1060 /* Go on to next lock.
1062 next_lock:
1063 before = &fl->fl_next;
1066 error = 0;
1067 if (!added) {
1068 if (caller->fl_type == F_UNLCK)
1069 goto out;
1070 locks_init_lock(new_fl, caller);
1071 locks_insert_lock(before, new_fl);
1072 new_fl = NULL;
1074 if (right) {
1075 if (left == right) {
1076 /* The new lock breaks the old one in two pieces,
1077 * so we have to use the second new lock (in this
1078 * case, even F_UNLCK may fail!).
1080 left = locks_init_lock(new_fl2, right);
1081 locks_insert_lock(before, left);
1082 new_fl2 = NULL;
1084 right->fl_start = caller->fl_end + 1;
1085 locks_wake_up_blocks(right, 0);
1087 if (left) {
1088 left->fl_end = caller->fl_start - 1;
1089 locks_wake_up_blocks(left, 0);
1091 out:
1093 * Free any unused locks. (They haven't
1094 * ever been used, so we use kfree().)
1096 if (new_fl)
1097 kfree(new_fl);
1098 if (new_fl2)
1099 kfree(new_fl2);
1100 return error;
1104 * Allocate an empty lock structure. We can use GFP_KERNEL now that
1105 * all allocations are done in advance.
1107 static struct file_lock *locks_empty_lock(void)
1109 /* Okay, let's make a new file_lock structure... */
1110 return ((struct file_lock *) kmalloc(sizeof(struct file_lock),
1111 GFP_KERNEL));
1115 * Initialize a new lock from an existing file_lock structure.
1117 static struct file_lock *locks_init_lock(struct file_lock *new,
1118 struct file_lock *fl)
1120 if (new) {
1121 memset(new, 0, sizeof(*new));
1122 new->fl_owner = fl->fl_owner;
1123 new->fl_pid = fl->fl_pid;
1124 init_waitqueue_head(&new->fl_wait);
1125 new->fl_file = fl->fl_file;
1126 new->fl_flags = fl->fl_flags;
1127 new->fl_type = fl->fl_type;
1128 new->fl_start = fl->fl_start;
1129 new->fl_end = fl->fl_end;
1130 new->fl_notify = fl->fl_notify;
1131 new->fl_u = fl->fl_u;
1133 return new;
1136 /* Insert file lock fl into an inode's lock list at the position indicated
1137 * by pos. At the same time add the lock to the global file lock list.
1139 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
1141 fl->fl_nextlink = file_lock_table;
1142 fl->fl_prevlink = NULL;
1143 if (file_lock_table != NULL)
1144 file_lock_table->fl_prevlink = fl;
1145 file_lock_table = fl;
1146 fl->fl_next = *pos; /* insert into file's list */
1147 *pos = fl;
1149 return;
1152 /* Delete a lock and free it.
1153 * First remove our lock from the active lock lists. Then call
1154 * locks_wake_up_blocks() to wake up processes that are blocked
1155 * waiting for this lock. Finally free the lock structure.
1157 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
1159 struct file_lock *thisfl;
1160 struct file_lock *prevfl;
1161 struct file_lock *nextfl;
1163 thisfl = *thisfl_p;
1164 *thisfl_p = thisfl->fl_next;
1166 prevfl = thisfl->fl_prevlink;
1167 nextfl = thisfl->fl_nextlink;
1169 if (nextfl != NULL)
1170 nextfl->fl_prevlink = prevfl;
1172 if (prevfl != NULL)
1173 prevfl->fl_nextlink = nextfl;
1174 else
1175 file_lock_table = nextfl;
1177 locks_wake_up_blocks(thisfl, wait);
1178 locks_free_lock(thisfl);
1180 return;
1184 static char *lock_get_status(struct file_lock *fl, int id, char *pfx)
1186 static char temp[155];
1187 char *p = temp;
1188 struct inode *inode;
1190 inode = fl->fl_file->f_dentry->d_inode;
1192 p += sprintf(p, "%d:%s ", id, pfx);
1193 if (fl->fl_flags & FL_POSIX) {
1194 p += sprintf(p, "%6s %s ",
1195 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1196 (IS_MANDLOCK(inode) &&
1197 (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1198 "MANDATORY" : "ADVISORY ");
1200 else {
1201 p += sprintf(p, "FLOCK ADVISORY ");
1203 p += sprintf(p, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
1204 p += sprintf(p, "%d %s:%ld %ld %ld ",
1205 fl->fl_pid,
1206 kdevname(inode->i_dev), inode->i_ino, fl->fl_start,
1207 fl->fl_end);
1208 sprintf(p, "%08lx %08lx %08lx %08lx %08lx\n",
1209 (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
1210 (long)fl->fl_next, (long)fl->fl_nextblock);
1211 return (temp);
1214 static inline int copy_lock_status(char *p, char **q, off_t pos, int len,
1215 off_t offset, off_t length)
1217 off_t i;
1219 i = pos - offset;
1220 if (i > 0) {
1221 if (i >= length) {
1222 i = len + length - i;
1223 memcpy(*q, p, i);
1224 *q += i;
1225 return (0);
1227 if (i < len) {
1228 p += len - i;
1230 else
1231 i = len;
1232 memcpy(*q, p, i);
1233 *q += i;
1236 return (1);
1239 int get_locks_status(char *buffer, char **start, off_t offset, off_t length)
1241 struct file_lock *fl;
1242 struct file_lock *bfl;
1243 char *p;
1244 char *q = buffer;
1245 off_t i, len, pos = 0;
1247 for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
1248 p = lock_get_status(fl, i, "");
1249 len = strlen(p);
1250 pos += len;
1251 if (!copy_lock_status(p, &q, pos, len, offset, length))
1252 goto done;
1253 if ((bfl = fl->fl_nextblock) == NULL)
1254 continue;
1255 do {
1256 p = lock_get_status(bfl, i, " ->");
1257 len = strlen(p);
1258 pos += len;
1259 if (!copy_lock_status(p, &q, pos, len, offset, length))
1260 goto done;
1261 } while ((bfl = bfl->fl_nextblock) != fl);
1263 done:
1264 if (q != buffer)
1265 *start = buffer;
1266 return (q - buffer);