Import 2.3.7pre9
[davej-history.git] / fs / locks.c
blobdee45cabce4dda9c21994ea5b9e167252aa634e1
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 (blocker->fl_prevblock == NULL)
197 /* No previous waiters - list is empty */
198 prevblock = blocker;
199 else
200 /* Previous waiters exist - add to end of list */
201 prevblock = blocker->fl_prevblock;
203 prevblock->fl_nextblock = waiter;
204 blocker->fl_prevblock = waiter;
205 waiter->fl_nextblock = blocker;
206 waiter->fl_prevblock = prevblock;
208 return;
211 /* Remove waiter from blocker's block list.
212 * When blocker ends up pointing to itself then the list is empty.
214 static void locks_delete_block(struct file_lock *blocker,
215 struct file_lock *waiter)
217 struct file_lock *nextblock;
218 struct file_lock *prevblock;
220 nextblock = waiter->fl_nextblock;
221 prevblock = waiter->fl_prevblock;
223 if (nextblock == NULL)
224 return;
226 nextblock->fl_prevblock = prevblock;
227 prevblock->fl_nextblock = nextblock;
229 waiter->fl_prevblock = waiter->fl_nextblock = NULL;
230 if (blocker->fl_nextblock == blocker)
231 /* No more locks on blocker's blocked list */
232 blocker->fl_prevblock = blocker->fl_nextblock = NULL;
233 return;
236 /* The following two are for the benefit of lockd.
238 void
239 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
241 locks_insert_block(blocker, waiter);
242 return;
245 void
246 posix_unblock_lock(struct file_lock *waiter)
248 if (waiter->fl_prevblock)
249 locks_delete_block(waiter->fl_prevblock, waiter);
250 return;
253 /* Wake up processes blocked waiting for blocker.
254 * If told to wait then schedule the processes until the block list
255 * is empty, otherwise empty the block list ourselves.
257 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
259 struct file_lock *waiter;
261 while ((waiter = blocker->fl_nextblock) != NULL) {
262 /* N.B. Is it possible for the notify function to block?? */
263 if (waiter->fl_notify)
264 waiter->fl_notify(waiter);
265 wake_up(&waiter->fl_wait);
266 if (wait) {
267 /* Let the blocked process remove waiter from the
268 * block list when it gets scheduled.
270 current->policy |= SCHED_YIELD;
271 schedule();
272 } else {
273 /* Remove waiter from the block list, because by the
274 * time it wakes up blocker won't exist any more.
276 locks_delete_block(blocker, waiter);
279 return;
282 /* flock() system call entry point. Apply a FL_FLOCK style lock to
283 * an open file descriptor.
285 asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
287 struct file_lock file_lock;
288 struct file *filp;
289 int error;
291 lock_kernel();
292 error = -EBADF;
293 filp = fget(fd);
294 if (!filp)
295 goto out;
296 error = -EINVAL;
297 if (!flock_make_lock(filp, &file_lock, cmd))
298 goto out_putf;
299 error = -EBADF;
300 if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
301 goto out_putf;
302 error = flock_lock_file(filp, &file_lock,
303 (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
304 out_putf:
305 fput(filp);
306 out:
307 unlock_kernel();
308 return (error);
311 /* Report the first existing lock that would conflict with l.
312 * This implements the F_GETLK command of fcntl().
314 int fcntl_getlk(unsigned int fd, struct flock *l)
316 struct file *filp;
317 struct file_lock *fl,file_lock;
318 struct flock flock;
319 int error;
321 error = -EFAULT;
322 if (copy_from_user(&flock, l, sizeof(flock)))
323 goto out;
324 error = -EINVAL;
325 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
326 goto out;
328 error = -EBADF;
329 filp = fget(fd);
330 if (!filp)
331 goto out;
333 error = -EINVAL;
334 if (!filp->f_dentry || !filp->f_dentry->d_inode)
335 goto out_putf;
337 if (!posix_make_lock(filp, &file_lock, &flock))
338 goto out_putf;
340 if (filp->f_op->lock) {
341 error = filp->f_op->lock(filp, F_GETLK, &file_lock);
342 if (error < 0)
343 goto out_putf;
344 fl = &file_lock;
345 } else {
346 fl = posix_test_lock(filp, &file_lock);
349 flock.l_type = F_UNLCK;
350 if (fl != NULL) {
351 flock.l_pid = fl->fl_pid;
352 flock.l_start = fl->fl_start;
353 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
354 fl->fl_end - fl->fl_start + 1;
355 flock.l_whence = 0;
356 flock.l_type = fl->fl_type;
358 error = -EFAULT;
359 if (!copy_to_user(l, &flock, sizeof(flock)))
360 error = 0;
362 out_putf:
363 fput(filp);
364 out:
365 return error;
368 /* Apply the lock described by l to an open file descriptor.
369 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
371 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
373 struct file *filp;
374 struct file_lock file_lock;
375 struct flock flock;
376 struct dentry * dentry;
377 struct inode *inode;
378 int error;
381 * This might block, so we do it before checking the inode.
383 error = -EFAULT;
384 if (copy_from_user(&flock, l, sizeof(flock)))
385 goto out;
387 /* Get arguments and validate them ...
390 error = -EBADF;
391 filp = fget(fd);
392 if (!filp)
393 goto out;
395 error = -EINVAL;
396 if (!(dentry = filp->f_dentry))
397 goto out_putf;
398 if (!(inode = dentry->d_inode))
399 goto out_putf;
401 /* Don't allow mandatory locks on files that may be memory mapped
402 * and shared.
404 if (IS_MANDLOCK(inode) &&
405 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
406 inode->i_mmap) {
407 struct vm_area_struct *vma = inode->i_mmap;
408 error = -EAGAIN;
409 do {
410 if (vma->vm_flags & VM_MAYSHARE)
411 goto out_putf;
412 } while ((vma = vma->vm_next_share) != NULL);
415 error = -EINVAL;
416 if (!posix_make_lock(filp, &file_lock, &flock))
417 goto out_putf;
419 error = -EBADF;
420 switch (flock.l_type) {
421 case F_RDLCK:
422 if (!(filp->f_mode & FMODE_READ))
423 goto out_putf;
424 break;
425 case F_WRLCK:
426 if (!(filp->f_mode & FMODE_WRITE))
427 goto out_putf;
428 break;
429 case F_UNLCK:
430 break;
431 case F_SHLCK:
432 case F_EXLCK:
433 #ifdef __sparc__
434 /* warn a bit for now, but don't overdo it */
436 static int count = 0;
437 if (!count) {
438 count=1;
439 printk(KERN_WARNING
440 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
441 current->pid, current->comm);
444 if (!(filp->f_mode & 3))
445 goto out_putf;
446 break;
447 #endif
448 default:
449 error = -EINVAL;
450 goto out_putf;
453 if (filp->f_op->lock != NULL) {
454 error = filp->f_op->lock(filp, cmd, &file_lock);
455 if (error < 0)
456 goto out_putf;
458 error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
460 out_putf:
461 fput(filp);
462 out:
463 return error;
467 * This function is called when the file is being removed
468 * from the task's fd array.
470 void locks_remove_posix(struct file *filp, fl_owner_t owner)
472 struct inode * inode = filp->f_dentry->d_inode;
473 struct file_lock file_lock, *fl;
474 struct file_lock **before;
477 * For POSIX locks we free all locks on this file for the given task.
479 repeat:
480 before = &inode->i_flock;
481 while ((fl = *before) != NULL) {
482 if ((fl->fl_flags & FL_POSIX) && fl->fl_owner == owner) {
483 int (*lock)(struct file *, int, struct file_lock *);
484 lock = filp->f_op->lock;
485 if (lock) {
486 file_lock = *fl;
487 file_lock.fl_type = F_UNLCK;
489 locks_delete_lock(before, 0);
490 if (lock) {
491 lock(filp, F_SETLK, &file_lock);
492 /* List may have changed: */
493 goto repeat;
495 continue;
497 before = &fl->fl_next;
502 * This function is called on the last close of an open file.
504 void locks_remove_flock(struct file *filp)
506 struct inode * inode = filp->f_dentry->d_inode;
507 struct file_lock file_lock, *fl;
508 struct file_lock **before;
510 repeat:
511 before = &inode->i_flock;
512 while ((fl = *before) != NULL) {
513 if ((fl->fl_flags & FL_FLOCK) && fl->fl_file == filp) {
514 int (*lock)(struct file *, int, struct file_lock *);
515 lock = filp->f_op->lock;
516 if (lock) {
517 file_lock = *fl;
518 file_lock.fl_type = F_UNLCK;
520 locks_delete_lock(before, 0);
521 if (lock) {
522 lock(filp, F_SETLK, &file_lock);
523 /* List may have changed: */
524 goto repeat;
526 continue;
528 before = &fl->fl_next;
532 struct file_lock *
533 posix_test_lock(struct file *filp, struct file_lock *fl)
535 struct file_lock *cfl;
537 for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
538 if (!(cfl->fl_flags & FL_POSIX))
539 continue;
540 if (posix_locks_conflict(cfl, fl))
541 break;
544 return (cfl);
547 int locks_verify_locked(struct inode *inode)
549 /* Candidates for mandatory locking have the setgid bit set
550 * but no group execute bit - an otherwise meaningless combination.
552 if (IS_MANDLOCK(inode) &&
553 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
554 return (locks_mandatory_locked(inode));
555 return (0);
558 int locks_verify_area(int read_write, struct inode *inode, struct file *filp,
559 loff_t offset, size_t count)
561 /* Candidates for mandatory locking have the setgid bit set
562 * but no group execute bit - an otherwise meaningless combination.
564 if (IS_MANDLOCK(inode) &&
565 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
566 return (locks_mandatory_area(read_write, inode, filp, offset,
567 count));
568 return (0);
571 int locks_mandatory_locked(struct inode *inode)
573 fl_owner_t owner = current->files;
574 struct file_lock *fl;
576 /* Search the lock list for this inode for any POSIX locks.
578 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
579 if (!(fl->fl_flags & FL_POSIX))
580 continue;
581 if (fl->fl_owner != owner)
582 return (-EAGAIN);
584 return (0);
587 int locks_mandatory_area(int read_write, struct inode *inode,
588 struct file *filp, loff_t offset,
589 size_t count)
591 struct file_lock *fl;
592 struct file_lock tfl;
594 memset(&tfl, 0, sizeof(tfl));
596 tfl.fl_file = filp;
597 tfl.fl_flags = FL_POSIX | FL_ACCESS;
598 tfl.fl_owner = current->files;
599 tfl.fl_pid = current->pid;
600 init_waitqueue_head(&tfl.fl_wait);
601 tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
602 tfl.fl_start = offset;
603 tfl.fl_end = offset + count - 1;
605 repeat:
606 /* Search the lock list for this inode for locks that conflict with
607 * the proposed read/write.
609 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
610 if (!(fl->fl_flags & FL_POSIX))
611 continue;
612 /* Block for writes against a "read" lock,
613 * and both reads and writes against a "write" lock.
615 if (posix_locks_conflict(fl, &tfl)) {
616 if (filp && (filp->f_flags & O_NONBLOCK))
617 return (-EAGAIN);
618 if (signal_pending(current))
619 return (-ERESTARTSYS);
620 if (posix_locks_deadlock(&tfl, fl))
621 return (-EDEADLK);
623 locks_insert_block(fl, &tfl);
624 interruptible_sleep_on(&tfl.fl_wait);
625 locks_delete_block(fl, &tfl);
627 if (signal_pending(current))
628 return (-ERESTARTSYS);
629 /* If we've been sleeping someone might have
630 * changed the permissions behind our back.
632 if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
633 break;
634 goto repeat;
637 return (0);
640 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
641 * style lock.
643 static int posix_make_lock(struct file *filp, struct file_lock *fl,
644 struct flock *l)
646 off_t start;
648 memset(fl, 0, sizeof(*fl));
650 init_waitqueue_head(&fl->fl_wait);
651 fl->fl_flags = FL_POSIX;
653 switch (l->l_type) {
654 case F_RDLCK:
655 case F_WRLCK:
656 case F_UNLCK:
657 fl->fl_type = l->l_type;
658 break;
659 default:
660 return (0);
663 switch (l->l_whence) {
664 case 0: /*SEEK_SET*/
665 start = 0;
666 break;
667 case 1: /*SEEK_CUR*/
668 start = filp->f_pos;
669 break;
670 case 2: /*SEEK_END*/
671 start = filp->f_dentry->d_inode->i_size;
672 break;
673 default:
674 return (0);
677 if (((start += l->l_start) < 0) || (l->l_len < 0))
678 return (0);
679 fl->fl_start = start; /* we record the absolute position */
680 if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
681 fl->fl_end = OFFSET_MAX;
683 fl->fl_file = filp;
684 fl->fl_owner = current->files;
685 fl->fl_pid = current->pid;
687 return (1);
690 /* Verify a call to flock() and fill in a file_lock structure with
691 * an appropriate FLOCK lock.
693 static int flock_make_lock(struct file *filp, struct file_lock *fl,
694 unsigned int cmd)
696 memset(fl, 0, sizeof(*fl));
698 init_waitqueue_head(&fl->fl_wait);
699 if (!filp->f_dentry) /* just in case */
700 return (0);
702 switch (cmd & ~LOCK_NB) {
703 case LOCK_SH:
704 fl->fl_type = F_RDLCK;
705 break;
706 case LOCK_EX:
707 fl->fl_type = F_WRLCK;
708 break;
709 case LOCK_UN:
710 fl->fl_type = F_UNLCK;
711 break;
712 default:
713 return (0);
716 fl->fl_flags = FL_FLOCK;
717 fl->fl_start = 0;
718 fl->fl_end = OFFSET_MAX;
719 fl->fl_file = filp;
720 fl->fl_owner = NULL;
722 return (1);
725 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
726 * checking before calling the locks_conflict().
728 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
730 /* POSIX locks owned by the same process do not conflict with
731 * each other.
733 if (!(sys_fl->fl_flags & FL_POSIX) ||
734 locks_same_owner(caller_fl, sys_fl))
735 return (0);
737 return (locks_conflict(caller_fl, sys_fl));
740 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
741 * checking before calling the locks_conflict().
743 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
745 /* FLOCK locks referring to the same filp do not conflict with
746 * each other.
748 if (!(sys_fl->fl_flags & FL_FLOCK) ||
749 (caller_fl->fl_file == sys_fl->fl_file))
750 return (0);
752 return (locks_conflict(caller_fl, sys_fl));
755 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
756 * checks for overlapping locks and shared/exclusive status.
758 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
760 if (!locks_overlap(caller_fl, sys_fl))
761 return (0);
763 switch (caller_fl->fl_type) {
764 case F_RDLCK:
765 return (sys_fl->fl_type == F_WRLCK);
767 case F_WRLCK:
768 return (1);
770 default:
771 printk("locks_conflict(): impossible lock type - %d\n",
772 caller_fl->fl_type);
773 break;
775 return (0); /* This should never happen */
778 /* This function tests for deadlock condition before putting a process to
779 * sleep. The detection scheme is no longer recursive. Recursive was neat,
780 * but dangerous - we risked stack corruption if the lock data was bad, or
781 * if the recursion was too deep for any other reason.
783 * We rely on the fact that a task can only be on one lock's wait queue
784 * at a time. When we find blocked_task on a wait queue we can re-search
785 * with blocked_task equal to that queue's owner, until either blocked_task
786 * isn't found, or blocked_task is found on a queue owned by my_task.
788 * Note: the above assumption may not be true when handling lock requests
789 * from a broken NFS client. But broken NFS clients have a lot more to
790 * worry about than proper deadlock detection anyway... --okir
792 static int posix_locks_deadlock(struct file_lock *caller_fl,
793 struct file_lock *block_fl)
795 struct file_lock *fl;
796 struct file_lock *bfl;
797 void *caller_owner, *blocked_owner;
798 unsigned int caller_pid, blocked_pid;
800 caller_owner = caller_fl->fl_owner;
801 caller_pid = caller_fl->fl_pid;
802 blocked_owner = block_fl->fl_owner;
803 blocked_pid = block_fl->fl_pid;
805 next_task:
806 if (caller_owner == blocked_owner && caller_pid == blocked_pid)
807 return (1);
808 for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
809 if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
810 continue;
811 for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
812 if (bfl->fl_owner == blocked_owner &&
813 bfl->fl_pid == blocked_pid) {
814 if (fl->fl_owner == caller_owner &&
815 fl->fl_pid == caller_pid) {
816 return (1);
818 blocked_owner = fl->fl_owner;
819 blocked_pid = fl->fl_pid;
820 goto next_task;
824 return (0);
827 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
828 * the head of the list, but that's secret knowledge known only to the next
829 * two functions.
831 static int flock_lock_file(struct file *filp, struct file_lock *caller,
832 unsigned int wait)
834 struct file_lock *fl;
835 struct file_lock *new_fl = NULL;
836 struct file_lock **before;
837 struct inode * inode = filp->f_dentry->d_inode;
838 int error, change;
839 int unlock = (caller->fl_type == F_UNLCK);
842 * If we need a new lock, get it in advance to avoid races.
844 if (!unlock) {
845 error = -ENOLCK;
846 new_fl = locks_alloc_lock(caller);
847 if (!new_fl)
848 goto out;
851 error = 0;
852 search:
853 change = 0;
854 before = &inode->i_flock;
855 while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
856 if (caller->fl_file == fl->fl_file) {
857 if (caller->fl_type == fl->fl_type)
858 goto out;
859 change = 1;
860 break;
862 before = &fl->fl_next;
864 /* change means that we are changing the type of an existing lock, or
865 * or else unlocking it.
867 if (change) {
868 /* N.B. What if the wait argument is false? */
869 locks_delete_lock(before, !unlock);
871 * If we waited, another lock may have been added ...
873 if (!unlock)
874 goto search;
876 if (unlock)
877 goto out;
879 repeat:
880 /* Check signals each time we start */
881 error = -ERESTARTSYS;
882 if (signal_pending(current))
883 goto out;
884 for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
885 fl = fl->fl_next) {
886 if (!flock_locks_conflict(new_fl, fl))
887 continue;
888 error = -EAGAIN;
889 if (!wait)
890 goto out;
891 locks_insert_block(fl, new_fl);
892 interruptible_sleep_on(&new_fl->fl_wait);
893 locks_delete_block(fl, new_fl);
894 goto repeat;
896 locks_insert_lock(&inode->i_flock, new_fl);
897 new_fl = NULL;
898 error = 0;
900 out:
901 if (new_fl)
902 locks_free_lock(new_fl);
903 return error;
906 /* Add a POSIX style lock to a file.
907 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
908 * task, then by starting address
910 * Kai Petzke writes:
911 * To make freeing a lock much faster, we keep a pointer to the lock before the
912 * actual one. But the real gain of the new coding was, that lock_it() and
913 * unlock_it() became one function.
915 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
918 int posix_lock_file(struct file *filp, struct file_lock *caller,
919 unsigned int wait)
921 struct file_lock *fl;
922 struct file_lock *new_fl, *new_fl2;
923 struct file_lock *left = NULL;
924 struct file_lock *right = NULL;
925 struct file_lock **before;
926 struct inode * inode = filp->f_dentry->d_inode;
927 int error, added = 0;
930 * We may need two file_lock structures for this operation,
931 * so we get them in advance to avoid races.
933 new_fl = locks_empty_lock();
934 new_fl2 = locks_empty_lock();
935 error = -ENOLCK; /* "no luck" */
936 if (!(new_fl && new_fl2))
937 goto out;
939 if (caller->fl_type != F_UNLCK) {
940 repeat:
941 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
942 if (!(fl->fl_flags & FL_POSIX))
943 continue;
944 if (!posix_locks_conflict(caller, fl))
945 continue;
946 error = -EAGAIN;
947 if (!wait)
948 goto out;
949 error = -EDEADLK;
950 if (posix_locks_deadlock(caller, fl))
951 goto out;
952 error = -ERESTARTSYS;
953 if (signal_pending(current))
954 goto out;
955 locks_insert_block(fl, caller);
956 interruptible_sleep_on(&caller->fl_wait);
957 locks_delete_block(fl, caller);
958 goto repeat;
963 * We've allocated the new locks in advance, so there are no
964 * errors possible (and no blocking operations) from here on.
966 * Find the first old lock with the same owner as the new lock.
969 before = &inode->i_flock;
971 /* First skip locks owned by other processes.
973 while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
974 !locks_same_owner(caller, fl))) {
975 before = &fl->fl_next;
978 /* Process locks with this owner.
980 while ((fl = *before) && locks_same_owner(caller, fl)) {
981 /* Detect adjacent or overlapping regions (if same lock type)
983 if (caller->fl_type == fl->fl_type) {
984 if (fl->fl_end < caller->fl_start - 1)
985 goto next_lock;
986 /* If the next lock in the list has entirely bigger
987 * addresses than the new one, insert the lock here.
989 if (fl->fl_start > caller->fl_end + 1)
990 break;
992 /* If we come here, the new and old lock are of the
993 * same type and adjacent or overlapping. Make one
994 * lock yielding from the lower start address of both
995 * locks to the higher end address.
997 if (fl->fl_start > caller->fl_start)
998 fl->fl_start = caller->fl_start;
999 else
1000 caller->fl_start = fl->fl_start;
1001 if (fl->fl_end < caller->fl_end)
1002 fl->fl_end = caller->fl_end;
1003 else
1004 caller->fl_end = fl->fl_end;
1005 if (added) {
1006 locks_delete_lock(before, 0);
1007 continue;
1009 caller = fl;
1010 added = 1;
1012 else {
1013 /* Processing for different lock types is a bit
1014 * more complex.
1016 if (fl->fl_end < caller->fl_start)
1017 goto next_lock;
1018 if (fl->fl_start > caller->fl_end)
1019 break;
1020 if (caller->fl_type == F_UNLCK)
1021 added = 1;
1022 if (fl->fl_start < caller->fl_start)
1023 left = fl;
1024 /* If the next lock in the list has a higher end
1025 * address than the new one, insert the new one here.
1027 if (fl->fl_end > caller->fl_end) {
1028 right = fl;
1029 break;
1031 if (fl->fl_start >= caller->fl_start) {
1032 /* The new lock completely replaces an old
1033 * one (This may happen several times).
1035 if (added) {
1036 locks_delete_lock(before, 0);
1037 continue;
1039 /* Replace the old lock with the new one.
1040 * Wake up anybody waiting for the old one,
1041 * as the change in lock type might satisfy
1042 * their needs.
1044 locks_wake_up_blocks(fl, 0);
1045 fl->fl_start = caller->fl_start;
1046 fl->fl_end = caller->fl_end;
1047 fl->fl_type = caller->fl_type;
1048 fl->fl_u = caller->fl_u;
1049 caller = fl;
1050 added = 1;
1053 /* Go on to next lock.
1055 next_lock:
1056 before = &fl->fl_next;
1059 error = 0;
1060 if (!added) {
1061 if (caller->fl_type == F_UNLCK)
1062 goto out;
1063 locks_init_lock(new_fl, caller);
1064 locks_insert_lock(before, new_fl);
1065 new_fl = NULL;
1067 if (right) {
1068 if (left == right) {
1069 /* The new lock breaks the old one in two pieces,
1070 * so we have to use the second new lock (in this
1071 * case, even F_UNLCK may fail!).
1073 left = locks_init_lock(new_fl2, right);
1074 locks_insert_lock(before, left);
1075 new_fl2 = NULL;
1077 right->fl_start = caller->fl_end + 1;
1078 locks_wake_up_blocks(right, 0);
1080 if (left) {
1081 left->fl_end = caller->fl_start - 1;
1082 locks_wake_up_blocks(left, 0);
1084 out:
1086 * Free any unused locks. (They haven't
1087 * ever been used, so we use kfree().)
1089 if (new_fl)
1090 kfree(new_fl);
1091 if (new_fl2)
1092 kfree(new_fl2);
1093 return error;
1097 * Allocate an empty lock structure. We can use GFP_KERNEL now that
1098 * all allocations are done in advance.
1100 static struct file_lock *locks_empty_lock(void)
1102 /* Okay, let's make a new file_lock structure... */
1103 return ((struct file_lock *) kmalloc(sizeof(struct file_lock),
1104 GFP_KERNEL));
1108 * Initialize a new lock from an existing file_lock structure.
1110 static struct file_lock *locks_init_lock(struct file_lock *new,
1111 struct file_lock *fl)
1113 if (new) {
1114 memset(new, 0, sizeof(*new));
1115 new->fl_owner = fl->fl_owner;
1116 new->fl_pid = fl->fl_pid;
1117 init_waitqueue_head(&new->fl_wait);
1118 new->fl_file = fl->fl_file;
1119 new->fl_flags = fl->fl_flags;
1120 new->fl_type = fl->fl_type;
1121 new->fl_start = fl->fl_start;
1122 new->fl_end = fl->fl_end;
1123 new->fl_notify = fl->fl_notify;
1124 new->fl_u = fl->fl_u;
1126 return new;
1129 /* Insert file lock fl into an inode's lock list at the position indicated
1130 * by pos. At the same time add the lock to the global file lock list.
1132 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
1134 fl->fl_nextlink = file_lock_table;
1135 fl->fl_prevlink = NULL;
1136 if (file_lock_table != NULL)
1137 file_lock_table->fl_prevlink = fl;
1138 file_lock_table = fl;
1139 fl->fl_next = *pos; /* insert into file's list */
1140 *pos = fl;
1142 return;
1145 /* Delete a lock and free it.
1146 * First remove our lock from the active lock lists. Then call
1147 * locks_wake_up_blocks() to wake up processes that are blocked
1148 * waiting for this lock. Finally free the lock structure.
1150 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
1152 struct file_lock *thisfl;
1153 struct file_lock *prevfl;
1154 struct file_lock *nextfl;
1156 thisfl = *thisfl_p;
1157 *thisfl_p = thisfl->fl_next;
1159 prevfl = thisfl->fl_prevlink;
1160 nextfl = thisfl->fl_nextlink;
1162 if (nextfl != NULL)
1163 nextfl->fl_prevlink = prevfl;
1165 if (prevfl != NULL)
1166 prevfl->fl_nextlink = nextfl;
1167 else
1168 file_lock_table = nextfl;
1170 locks_wake_up_blocks(thisfl, wait);
1171 locks_free_lock(thisfl);
1173 return;
1177 static char *lock_get_status(struct file_lock *fl, int id, char *pfx)
1179 static char temp[155];
1180 char *p = temp;
1181 struct inode *inode;
1183 inode = fl->fl_file->f_dentry->d_inode;
1185 p += sprintf(p, "%d:%s ", id, pfx);
1186 if (fl->fl_flags & FL_POSIX) {
1187 p += sprintf(p, "%6s %s ",
1188 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1189 (IS_MANDLOCK(inode) &&
1190 (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1191 "MANDATORY" : "ADVISORY ");
1193 else {
1194 p += sprintf(p, "FLOCK ADVISORY ");
1196 p += sprintf(p, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
1197 p += sprintf(p, "%d %s:%ld %ld %ld ",
1198 fl->fl_pid,
1199 kdevname(inode->i_dev), inode->i_ino, fl->fl_start,
1200 fl->fl_end);
1201 sprintf(p, "%08lx %08lx %08lx %08lx %08lx\n",
1202 (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
1203 (long)fl->fl_next, (long)fl->fl_nextblock);
1204 return (temp);
1207 static inline int copy_lock_status(char *p, char **q, off_t pos, int len,
1208 off_t offset, off_t length)
1210 off_t i;
1212 i = pos - offset;
1213 if (i > 0) {
1214 if (i >= length) {
1215 i = len + length - i;
1216 memcpy(*q, p, i);
1217 *q += i;
1218 return (0);
1220 if (i < len) {
1221 p += len - i;
1223 else
1224 i = len;
1225 memcpy(*q, p, i);
1226 *q += i;
1229 return (1);
1232 int get_locks_status(char *buffer, char **start, off_t offset, off_t length)
1234 struct file_lock *fl;
1235 struct file_lock *bfl;
1236 char *p;
1237 char *q = buffer;
1238 off_t i, len, pos = 0;
1240 for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
1241 p = lock_get_status(fl, i, "");
1242 len = strlen(p);
1243 pos += len;
1244 if (!copy_lock_status(p, &q, pos, len, offset, length))
1245 goto done;
1246 if ((bfl = fl->fl_nextblock) == NULL)
1247 continue;
1248 do {
1249 p = lock_get_status(bfl, i, " ->");
1250 len = strlen(p);
1251 pos += len;
1252 if (!copy_lock_status(p, &q, pos, len, offset, length))
1253 goto done;
1254 } while ((bfl = bfl->fl_nextblock) != fl);
1256 done:
1257 if (q != buffer)
1258 *start = buffer;
1259 return (q - buffer);