Linux-2.4.0-test2
[davej-history.git] / fs / locks.c
blob015b8e87a6b922ad64e41649ce42e2c679fc7395
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 static int flock_make_lock(struct file *filp, struct file_lock *fl,
115 unsigned int cmd);
116 static int posix_make_lock(struct file *filp, struct file_lock *fl,
117 struct flock *l);
118 static int flock_locks_conflict(struct file_lock *caller_fl,
119 struct file_lock *sys_fl);
120 static int posix_locks_conflict(struct file_lock *caller_fl,
121 struct file_lock *sys_fl);
122 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
123 static int flock_lock_file(struct file *filp, struct file_lock *caller,
124 unsigned int wait);
125 static int posix_locks_deadlock(struct file_lock *caller,
126 struct file_lock *blocker);
128 static struct file_lock *locks_empty_lock(void);
129 static struct file_lock *locks_init_lock(struct file_lock *,
130 struct file_lock *);
131 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
132 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait);
133 static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx);
135 static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter);
136 static void locks_delete_block(struct file_lock *blocker, struct file_lock *waiter);
137 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait);
139 struct file_lock *file_lock_table = NULL;
141 /* Allocate a new lock, and initialize its fields from fl.
142 * The lock is not inserted into any lists until locks_insert_lock() or
143 * locks_insert_block() are called.
145 static inline struct file_lock *locks_alloc_lock(struct file_lock *fl)
147 return locks_init_lock(locks_empty_lock(), fl);
150 /* Free lock not inserted in any queue.
152 static inline void locks_free_lock(struct file_lock *fl)
154 if (waitqueue_active(&fl->fl_wait))
155 panic("Attempting to free lock with active wait queue");
157 if (fl->fl_nextblock != NULL || fl->fl_prevblock != NULL)
158 panic("Attempting to free lock with active block list");
160 kfree(fl);
161 return;
164 /* Check if two locks overlap each other.
166 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
168 return ((fl1->fl_end >= fl2->fl_start) &&
169 (fl2->fl_end >= fl1->fl_start));
173 * Check whether two locks have the same owner
174 * N.B. Do we need the test on PID as well as owner?
175 * (Clone tasks should be considered as one "owner".)
177 static inline int
178 locks_same_owner(struct file_lock *fl1, struct file_lock *fl2)
180 return (fl1->fl_owner == fl2->fl_owner) &&
181 (fl1->fl_pid == fl2->fl_pid);
184 /* Insert waiter into blocker's block list.
185 * We use a circular list so that processes can be easily woken up in
186 * the order they blocked. The documentation doesn't require this but
187 * it seems like the reasonable thing to do.
189 static void locks_insert_block(struct file_lock *blocker,
190 struct file_lock *waiter)
192 struct file_lock *prevblock;
194 if (waiter->fl_prevblock) {
195 printk(KERN_ERR "locks_insert_block: remove duplicated lock "
196 "(pid=%d %Ld-%Ld type=%d)\n",
197 waiter->fl_pid, (long long)waiter->fl_start,
198 (long long)waiter->fl_end, waiter->fl_type);
199 locks_delete_block(waiter->fl_prevblock, waiter);
202 if (blocker->fl_prevblock == NULL)
203 /* No previous waiters - list is empty */
204 prevblock = blocker;
205 else
206 /* Previous waiters exist - add to end of list */
207 prevblock = blocker->fl_prevblock;
209 prevblock->fl_nextblock = waiter;
210 blocker->fl_prevblock = waiter;
211 waiter->fl_nextblock = blocker;
212 waiter->fl_prevblock = prevblock;
214 return;
217 /* Remove waiter from blocker's block list.
218 * When blocker ends up pointing to itself then the list is empty.
220 static void locks_delete_block(struct file_lock *blocker,
221 struct file_lock *waiter)
223 struct file_lock *nextblock;
224 struct file_lock *prevblock;
226 nextblock = waiter->fl_nextblock;
227 prevblock = waiter->fl_prevblock;
229 if (nextblock == NULL)
230 return;
232 nextblock->fl_prevblock = prevblock;
233 prevblock->fl_nextblock = nextblock;
235 waiter->fl_prevblock = waiter->fl_nextblock = NULL;
236 if (blocker->fl_nextblock == blocker)
237 /* No more locks on blocker's blocked list */
238 blocker->fl_prevblock = blocker->fl_nextblock = NULL;
239 return;
242 /* The following two are for the benefit of lockd.
244 void
245 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
247 locks_insert_block(blocker, waiter);
248 return;
251 void
252 posix_unblock_lock(struct file_lock *waiter)
254 if (waiter->fl_prevblock)
255 locks_delete_block(waiter->fl_prevblock, waiter);
256 return;
259 /* Wake up processes blocked waiting for blocker.
260 * If told to wait then schedule the processes until the block list
261 * is empty, otherwise empty the block list ourselves.
263 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
265 struct file_lock *waiter;
267 while ((waiter = blocker->fl_nextblock) != NULL) {
268 /* N.B. Is it possible for the notify function to block?? */
269 if (waiter->fl_notify)
270 waiter->fl_notify(waiter);
271 wake_up(&waiter->fl_wait);
272 if (wait) {
273 /* Let the blocked process remove waiter from the
274 * block list when it gets scheduled.
276 current->policy |= SCHED_YIELD;
277 schedule();
278 } else {
279 /* Remove waiter from the block list, because by the
280 * time it wakes up blocker won't exist any more.
282 locks_delete_block(blocker, waiter);
285 return;
288 /* flock() system call entry point. Apply a FL_FLOCK style lock to
289 * an open file descriptor.
291 asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
293 struct file_lock file_lock;
294 struct file *filp;
295 int error;
297 lock_kernel();
298 error = -EBADF;
299 filp = fget(fd);
300 if (!filp)
301 goto out;
302 error = -EINVAL;
303 if (!flock_make_lock(filp, &file_lock, cmd))
304 goto out_putf;
305 error = -EBADF;
306 if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
307 goto out_putf;
308 error = flock_lock_file(filp, &file_lock,
309 (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
310 out_putf:
311 fput(filp);
312 out:
313 unlock_kernel();
314 return (error);
317 /* Report the first existing lock that would conflict with l.
318 * This implements the F_GETLK command of fcntl().
320 int fcntl_getlk(unsigned int fd, struct flock *l)
322 struct file *filp;
323 struct file_lock *fl,file_lock;
324 struct flock flock;
325 int error;
327 error = -EFAULT;
328 if (copy_from_user(&flock, l, sizeof(flock)))
329 goto out;
330 error = -EINVAL;
331 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
332 goto out;
334 error = -EBADF;
335 filp = fget(fd);
336 if (!filp)
337 goto out;
339 if (!posix_make_lock(filp, &file_lock, &flock))
340 goto out_putf;
342 if (filp->f_op->lock) {
343 error = filp->f_op->lock(filp, F_GETLK, &file_lock);
344 if (error < 0)
345 goto out_putf;
346 else if (error == LOCK_USE_CLNT)
347 /* Bypass for NFS with no locking - 2.0.36 compat */
348 fl = posix_test_lock(filp, &file_lock);
349 else
350 fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
351 } else {
352 fl = posix_test_lock(filp, &file_lock);
355 flock.l_type = F_UNLCK;
356 if (fl != NULL) {
357 flock.l_pid = fl->fl_pid;
358 flock.l_start = fl->fl_start;
359 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
360 fl->fl_end - fl->fl_start + 1;
361 flock.l_whence = 0;
362 flock.l_type = fl->fl_type;
364 error = -EFAULT;
365 if (!copy_to_user(l, &flock, sizeof(flock)))
366 error = 0;
368 out_putf:
369 fput(filp);
370 out:
371 return error;
374 /* Apply the lock described by l to an open file descriptor.
375 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
377 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
379 struct file *filp;
380 struct file_lock file_lock;
381 struct flock flock;
382 struct inode *inode;
383 int error;
386 * This might block, so we do it before checking the inode.
388 error = -EFAULT;
389 if (copy_from_user(&flock, l, sizeof(flock)))
390 goto out;
392 /* Get arguments and validate them ...
395 error = -EBADF;
396 filp = fget(fd);
397 if (!filp)
398 goto out;
400 error = -EINVAL;
401 inode = filp->f_dentry->d_inode;
403 /* Don't allow mandatory locks on files that may be memory mapped
404 * and shared.
406 if (IS_MANDLOCK(inode) &&
407 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
408 struct vm_area_struct *vma;
409 struct address_space *mapping = inode->i_mapping;
410 spin_lock(&mapping->i_shared_lock);
411 for(vma = mapping->i_mmap;vma;vma = vma->vm_next_share) {
412 if (!(vma->vm_flags & VM_MAYSHARE))
413 continue;
414 spin_unlock(&mapping->i_shared_lock);
415 error = -EAGAIN;
416 goto out_putf;
418 spin_unlock(&mapping->i_shared_lock);
421 error = -EINVAL;
422 if (!posix_make_lock(filp, &file_lock, &flock))
423 goto out_putf;
425 error = -EBADF;
426 switch (flock.l_type) {
427 case F_RDLCK:
428 if (!(filp->f_mode & FMODE_READ))
429 goto out_putf;
430 break;
431 case F_WRLCK:
432 if (!(filp->f_mode & FMODE_WRITE))
433 goto out_putf;
434 break;
435 case F_UNLCK:
436 break;
437 case F_SHLCK:
438 case F_EXLCK:
439 #ifdef __sparc__
440 /* warn a bit for now, but don't overdo it */
442 static int count = 0;
443 if (!count) {
444 count=1;
445 printk(KERN_WARNING
446 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
447 current->pid, current->comm);
450 if (!(filp->f_mode & 3))
451 goto out_putf;
452 break;
453 #endif
454 default:
455 error = -EINVAL;
456 goto out_putf;
459 if (filp->f_op->lock != NULL) {
460 error = filp->f_op->lock(filp, cmd, &file_lock);
461 if (error < 0)
462 goto out_putf;
464 error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
466 out_putf:
467 fput(filp);
468 out:
469 return error;
473 * This function is called when the file is being removed
474 * from the task's fd array.
476 void locks_remove_posix(struct file *filp, fl_owner_t owner)
478 struct inode * inode = filp->f_dentry->d_inode;
479 struct file_lock file_lock, *fl;
480 struct file_lock **before;
483 * For POSIX locks we free all locks on this file for the given task.
485 repeat:
486 before = &inode->i_flock;
487 while ((fl = *before) != NULL) {
488 if ((fl->fl_flags & FL_POSIX) && fl->fl_owner == owner) {
489 int (*lock)(struct file *, int, struct file_lock *);
490 lock = filp->f_op->lock;
491 if (lock) {
492 file_lock = *fl;
493 file_lock.fl_type = F_UNLCK;
495 locks_delete_lock(before, 0);
496 if (lock) {
497 lock(filp, F_SETLK, &file_lock);
498 /* List may have changed: */
499 goto repeat;
501 continue;
503 before = &fl->fl_next;
508 * This function is called on the last close of an open file.
510 void locks_remove_flock(struct file *filp)
512 struct inode * inode = filp->f_dentry->d_inode;
513 struct file_lock file_lock, *fl;
514 struct file_lock **before;
516 repeat:
517 before = &inode->i_flock;
518 while ((fl = *before) != NULL) {
519 if ((fl->fl_flags & FL_FLOCK) && fl->fl_file == filp) {
520 int (*lock)(struct file *, int, struct file_lock *);
521 lock = NULL;
522 if (filp->f_op)
523 lock = filp->f_op->lock;
524 if (lock) {
525 file_lock = *fl;
526 file_lock.fl_type = F_UNLCK;
528 locks_delete_lock(before, 0);
529 if (lock) {
530 lock(filp, F_SETLK, &file_lock);
531 /* List may have changed: */
532 goto repeat;
534 continue;
536 before = &fl->fl_next;
540 struct file_lock *
541 posix_test_lock(struct file *filp, struct file_lock *fl)
543 struct file_lock *cfl;
545 for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
546 if (!(cfl->fl_flags & FL_POSIX))
547 continue;
548 if (posix_locks_conflict(cfl, fl))
549 break;
552 return (cfl);
555 int locks_mandatory_locked(struct inode *inode)
557 fl_owner_t owner = current->files;
558 struct file_lock *fl;
561 * Search the lock list for this inode for any POSIX locks.
563 lock_kernel();
564 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
565 if (!(fl->fl_flags & FL_POSIX))
566 continue;
567 if (fl->fl_owner != owner)
568 break;
570 unlock_kernel();
571 return fl ? -EAGAIN : 0;
574 int locks_mandatory_area(int read_write, struct inode *inode,
575 struct file *filp, loff_t offset,
576 size_t count)
578 struct file_lock *fl;
579 struct file_lock tfl;
580 int error;
582 memset(&tfl, 0, sizeof(tfl));
584 tfl.fl_file = filp;
585 tfl.fl_flags = FL_POSIX | FL_ACCESS;
586 tfl.fl_owner = current->files;
587 tfl.fl_pid = current->pid;
588 init_waitqueue_head(&tfl.fl_wait);
589 tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
590 tfl.fl_start = offset;
591 tfl.fl_end = offset + count - 1;
593 error = 0;
594 lock_kernel();
596 repeat:
597 /* Search the lock list for this inode for locks that conflict with
598 * the proposed read/write.
600 for (fl = inode->i_flock; ; fl = fl->fl_next) {
601 error = 0;
602 if (!fl)
603 break;
604 if (!(fl->fl_flags & FL_POSIX))
605 continue;
606 /* Block for writes against a "read" lock,
607 * and both reads and writes against a "write" lock.
609 if (posix_locks_conflict(&tfl, fl)) {
610 error = -EAGAIN;
611 if (filp && (filp->f_flags & O_NONBLOCK))
612 break;
613 error = -ERESTARTSYS;
614 if (signal_pending(current))
615 break;
616 error = -EDEADLK;
617 if (posix_locks_deadlock(&tfl, fl))
618 break;
620 locks_insert_block(fl, &tfl);
621 interruptible_sleep_on(&tfl.fl_wait);
622 locks_delete_block(fl, &tfl);
625 * If we've been sleeping someone might have
626 * changed the permissions behind our back.
628 if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
629 break;
630 goto repeat;
633 unlock_kernel();
634 return error;
637 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
638 * style lock.
640 static int posix_make_lock(struct file *filp, struct file_lock *fl,
641 struct flock *l)
643 loff_t start;
645 memset(fl, 0, sizeof(*fl));
647 init_waitqueue_head(&fl->fl_wait);
648 fl->fl_flags = FL_POSIX;
650 switch (l->l_type) {
651 case F_RDLCK:
652 case F_WRLCK:
653 case F_UNLCK:
654 fl->fl_type = l->l_type;
655 break;
656 default:
657 return (0);
660 switch (l->l_whence) {
661 case 0: /*SEEK_SET*/
662 start = 0;
663 break;
664 case 1: /*SEEK_CUR*/
665 start = filp->f_pos;
666 break;
667 case 2: /*SEEK_END*/
668 start = filp->f_dentry->d_inode->i_size;
669 break;
670 default:
671 return (0);
674 if (((start += l->l_start) < 0) || (l->l_len < 0))
675 return (0);
676 fl->fl_end = start + l->l_len - 1;
677 if (l->l_len > 0 && fl->fl_end < 0)
678 return (0);
679 fl->fl_start = start; /* we record the absolute position */
680 if (l->l_len == 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);
700 switch (cmd & ~LOCK_NB) {
701 case LOCK_SH:
702 fl->fl_type = F_RDLCK;
703 break;
704 case LOCK_EX:
705 fl->fl_type = F_WRLCK;
706 break;
707 case LOCK_UN:
708 fl->fl_type = F_UNLCK;
709 break;
710 default:
711 return (0);
714 fl->fl_flags = FL_FLOCK;
715 fl->fl_start = 0;
716 fl->fl_end = OFFSET_MAX;
717 fl->fl_file = filp;
718 fl->fl_owner = NULL;
720 return (1);
723 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
724 * checking before calling the locks_conflict().
726 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
728 /* POSIX locks owned by the same process do not conflict with
729 * each other.
731 if (!(sys_fl->fl_flags & FL_POSIX) ||
732 locks_same_owner(caller_fl, sys_fl))
733 return (0);
735 return (locks_conflict(caller_fl, sys_fl));
738 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
739 * checking before calling the locks_conflict().
741 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
743 /* FLOCK locks referring to the same filp do not conflict with
744 * each other.
746 if (!(sys_fl->fl_flags & FL_FLOCK) ||
747 (caller_fl->fl_file == sys_fl->fl_file))
748 return (0);
750 return (locks_conflict(caller_fl, sys_fl));
753 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
754 * checks for overlapping locks and shared/exclusive status.
756 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
758 if (!locks_overlap(caller_fl, sys_fl))
759 return (0);
761 switch (caller_fl->fl_type) {
762 case F_RDLCK:
763 return (sys_fl->fl_type == F_WRLCK);
765 case F_WRLCK:
766 return (1);
768 default:
769 printk("locks_conflict(): impossible lock type - %d\n",
770 caller_fl->fl_type);
771 break;
773 return (0); /* This should never happen */
776 /* This function tests for deadlock condition before putting a process to
777 * sleep. The detection scheme is no longer recursive. Recursive was neat,
778 * but dangerous - we risked stack corruption if the lock data was bad, or
779 * if the recursion was too deep for any other reason.
781 * We rely on the fact that a task can only be on one lock's wait queue
782 * at a time. When we find blocked_task on a wait queue we can re-search
783 * with blocked_task equal to that queue's owner, until either blocked_task
784 * isn't found, or blocked_task is found on a queue owned by my_task.
786 * Note: the above assumption may not be true when handling lock requests
787 * from a broken NFS client. But broken NFS clients have a lot more to
788 * worry about than proper deadlock detection anyway... --okir
790 static int posix_locks_deadlock(struct file_lock *caller_fl,
791 struct file_lock *block_fl)
793 struct file_lock *fl;
794 struct file_lock *bfl;
795 void *caller_owner, *blocked_owner;
796 unsigned int caller_pid, blocked_pid;
798 caller_owner = caller_fl->fl_owner;
799 caller_pid = caller_fl->fl_pid;
800 blocked_owner = block_fl->fl_owner;
801 blocked_pid = block_fl->fl_pid;
803 next_task:
804 if (caller_owner == blocked_owner && caller_pid == blocked_pid)
805 return (1);
806 for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
807 if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
808 continue;
809 for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
810 if (bfl->fl_owner == blocked_owner &&
811 bfl->fl_pid == blocked_pid) {
812 if (fl->fl_owner == caller_owner &&
813 fl->fl_pid == caller_pid) {
814 return (1);
816 blocked_owner = fl->fl_owner;
817 blocked_pid = fl->fl_pid;
818 goto next_task;
822 return (0);
825 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
826 * the head of the list, but that's secret knowledge known only to the next
827 * two functions.
829 static int flock_lock_file(struct file *filp, struct file_lock *caller,
830 unsigned int wait)
832 struct file_lock *fl;
833 struct file_lock *new_fl = NULL;
834 struct file_lock **before;
835 struct inode * inode = filp->f_dentry->d_inode;
836 int error, change;
837 int unlock = (caller->fl_type == F_UNLCK);
840 * If we need a new lock, get it in advance to avoid races.
842 if (!unlock) {
843 error = -ENOLCK;
844 new_fl = locks_alloc_lock(caller);
845 if (!new_fl)
846 goto out;
849 error = 0;
850 search:
851 change = 0;
852 before = &inode->i_flock;
853 while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
854 if (caller->fl_file == fl->fl_file) {
855 if (caller->fl_type == fl->fl_type)
856 goto out;
857 change = 1;
858 break;
860 before = &fl->fl_next;
862 /* change means that we are changing the type of an existing lock, or
863 * or else unlocking it.
865 if (change) {
866 /* N.B. What if the wait argument is false? */
867 locks_delete_lock(before, !unlock);
869 * If we waited, another lock may have been added ...
871 if (!unlock)
872 goto search;
874 if (unlock)
875 goto out;
877 repeat:
878 /* Check signals each time we start */
879 error = -ERESTARTSYS;
880 if (signal_pending(current))
881 goto out;
882 for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
883 fl = fl->fl_next) {
884 if (!flock_locks_conflict(new_fl, fl))
885 continue;
886 error = -EAGAIN;
887 if (!wait)
888 goto out;
889 locks_insert_block(fl, new_fl);
890 interruptible_sleep_on(&new_fl->fl_wait);
891 locks_delete_block(fl, new_fl);
892 goto repeat;
894 locks_insert_lock(&inode->i_flock, new_fl);
895 new_fl = NULL;
896 error = 0;
898 out:
899 if (new_fl)
900 locks_free_lock(new_fl);
901 return error;
904 /* Add a POSIX style lock to a file.
905 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
906 * task, then by starting address
908 * Kai Petzke writes:
909 * To make freeing a lock much faster, we keep a pointer to the lock before the
910 * actual one. But the real gain of the new coding was, that lock_it() and
911 * unlock_it() became one function.
913 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
916 int posix_lock_file(struct file *filp, struct file_lock *caller,
917 unsigned int wait)
919 struct file_lock *fl;
920 struct file_lock *new_fl, *new_fl2;
921 struct file_lock *left = NULL;
922 struct file_lock *right = NULL;
923 struct file_lock **before;
924 struct inode * inode = filp->f_dentry->d_inode;
925 int error, added = 0;
928 * We may need two file_lock structures for this operation,
929 * so we get them in advance to avoid races.
931 new_fl = locks_empty_lock();
932 new_fl2 = locks_empty_lock();
933 error = -ENOLCK; /* "no luck" */
934 if (!(new_fl && new_fl2))
935 goto out;
937 if (caller->fl_type != F_UNLCK) {
938 repeat:
939 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
940 if (!(fl->fl_flags & FL_POSIX))
941 continue;
942 if (!posix_locks_conflict(caller, fl))
943 continue;
944 error = -EAGAIN;
945 if (!wait)
946 goto out;
947 error = -EDEADLK;
948 if (posix_locks_deadlock(caller, fl))
949 goto out;
950 error = -ERESTARTSYS;
951 if (signal_pending(current))
952 goto out;
953 locks_insert_block(fl, caller);
954 interruptible_sleep_on(&caller->fl_wait);
955 locks_delete_block(fl, caller);
956 goto repeat;
961 * We've allocated the new locks in advance, so there are no
962 * errors possible (and no blocking operations) from here on.
964 * Find the first old lock with the same owner as the new lock.
967 before = &inode->i_flock;
969 /* First skip locks owned by other processes.
971 while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
972 !locks_same_owner(caller, fl))) {
973 before = &fl->fl_next;
976 /* Process locks with this owner.
978 while ((fl = *before) && locks_same_owner(caller, fl)) {
979 /* Detect adjacent or overlapping regions (if same lock type)
981 if (caller->fl_type == fl->fl_type) {
982 if (fl->fl_end < caller->fl_start - 1)
983 goto next_lock;
984 /* If the next lock in the list has entirely bigger
985 * addresses than the new one, insert the lock here.
987 if (fl->fl_start > caller->fl_end + 1)
988 break;
990 /* If we come here, the new and old lock are of the
991 * same type and adjacent or overlapping. Make one
992 * lock yielding from the lower start address of both
993 * locks to the higher end address.
995 if (fl->fl_start > caller->fl_start)
996 fl->fl_start = caller->fl_start;
997 else
998 caller->fl_start = fl->fl_start;
999 if (fl->fl_end < caller->fl_end)
1000 fl->fl_end = caller->fl_end;
1001 else
1002 caller->fl_end = fl->fl_end;
1003 if (added) {
1004 locks_delete_lock(before, 0);
1005 continue;
1007 caller = fl;
1008 added = 1;
1010 else {
1011 /* Processing for different lock types is a bit
1012 * more complex.
1014 if (fl->fl_end < caller->fl_start)
1015 goto next_lock;
1016 if (fl->fl_start > caller->fl_end)
1017 break;
1018 if (caller->fl_type == F_UNLCK)
1019 added = 1;
1020 if (fl->fl_start < caller->fl_start)
1021 left = fl;
1022 /* If the next lock in the list has a higher end
1023 * address than the new one, insert the new one here.
1025 if (fl->fl_end > caller->fl_end) {
1026 right = fl;
1027 break;
1029 if (fl->fl_start >= caller->fl_start) {
1030 /* The new lock completely replaces an old
1031 * one (This may happen several times).
1033 if (added) {
1034 locks_delete_lock(before, 0);
1035 continue;
1037 /* Replace the old lock with the new one.
1038 * Wake up anybody waiting for the old one,
1039 * as the change in lock type might satisfy
1040 * their needs.
1042 locks_wake_up_blocks(fl, 0);
1043 fl->fl_start = caller->fl_start;
1044 fl->fl_end = caller->fl_end;
1045 fl->fl_type = caller->fl_type;
1046 fl->fl_u = caller->fl_u;
1047 caller = fl;
1048 added = 1;
1051 /* Go on to next lock.
1053 next_lock:
1054 before = &fl->fl_next;
1057 error = 0;
1058 if (!added) {
1059 if (caller->fl_type == F_UNLCK)
1060 goto out;
1061 locks_init_lock(new_fl, caller);
1062 locks_insert_lock(before, new_fl);
1063 new_fl = NULL;
1065 if (right) {
1066 if (left == right) {
1067 /* The new lock breaks the old one in two pieces,
1068 * so we have to use the second new lock (in this
1069 * case, even F_UNLCK may fail!).
1071 left = locks_init_lock(new_fl2, right);
1072 locks_insert_lock(before, left);
1073 new_fl2 = NULL;
1075 right->fl_start = caller->fl_end + 1;
1076 locks_wake_up_blocks(right, 0);
1078 if (left) {
1079 left->fl_end = caller->fl_start - 1;
1080 locks_wake_up_blocks(left, 0);
1082 out:
1084 * Free any unused locks. (They haven't
1085 * ever been used, so we use kfree().)
1087 if (new_fl)
1088 kfree(new_fl);
1089 if (new_fl2)
1090 kfree(new_fl2);
1091 return error;
1095 * Allocate an empty lock structure. We can use GFP_KERNEL now that
1096 * all allocations are done in advance.
1098 static struct file_lock *locks_empty_lock(void)
1100 /* Okay, let's make a new file_lock structure... */
1101 return ((struct file_lock *) kmalloc(sizeof(struct file_lock),
1102 GFP_KERNEL));
1106 * Initialize a new lock from an existing file_lock structure.
1108 static struct file_lock *locks_init_lock(struct file_lock *new,
1109 struct file_lock *fl)
1111 if (new) {
1112 memset(new, 0, sizeof(*new));
1113 new->fl_owner = fl->fl_owner;
1114 new->fl_pid = fl->fl_pid;
1115 init_waitqueue_head(&new->fl_wait);
1116 new->fl_file = fl->fl_file;
1117 new->fl_flags = fl->fl_flags;
1118 new->fl_type = fl->fl_type;
1119 new->fl_start = fl->fl_start;
1120 new->fl_end = fl->fl_end;
1121 new->fl_notify = fl->fl_notify;
1122 new->fl_insert = fl->fl_insert;
1123 new->fl_remove = fl->fl_remove;
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 if (fl->fl_insert)
1143 fl->fl_insert(fl);
1145 return;
1148 /* Delete a lock and free it.
1149 * First remove our lock from the active lock lists. Then call
1150 * locks_wake_up_blocks() to wake up processes that are blocked
1151 * waiting for this lock. Finally free the lock structure.
1153 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
1155 struct file_lock *thisfl;
1156 struct file_lock *prevfl;
1157 struct file_lock *nextfl;
1159 thisfl = *thisfl_p;
1160 *thisfl_p = thisfl->fl_next;
1162 prevfl = thisfl->fl_prevlink;
1163 nextfl = thisfl->fl_nextlink;
1165 if (nextfl != NULL)
1166 nextfl->fl_prevlink = prevfl;
1168 if (prevfl != NULL)
1169 prevfl->fl_nextlink = nextfl;
1170 else
1171 file_lock_table = nextfl;
1173 if (thisfl->fl_remove)
1174 thisfl->fl_remove(thisfl);
1176 locks_wake_up_blocks(thisfl, wait);
1177 locks_free_lock(thisfl);
1179 return;
1182 static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
1184 struct inode *inode;
1186 inode = fl->fl_file->f_dentry->d_inode;
1188 out += sprintf(out, "%d:%s ", id, pfx);
1189 if (fl->fl_flags & FL_POSIX) {
1190 out += sprintf(out, "%6s %s ",
1191 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1192 (IS_MANDLOCK(inode) &&
1193 (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1194 "MANDATORY" : "ADVISORY ");
1196 else {
1197 out += sprintf(out, "FLOCK ADVISORY ");
1199 out += sprintf(out, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
1200 out += sprintf(out, "%d %s:%ld %Ld %Ld ",
1201 fl->fl_pid,
1202 kdevname(inode->i_dev), inode->i_ino,
1203 (long long)fl->fl_start, (long long)fl->fl_end);
1204 sprintf(out, "%08lx %08lx %08lx %08lx %08lx\n",
1205 (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
1206 (long)fl->fl_next, (long)fl->fl_nextblock);
1209 static void move_lock_status(char **p, off_t* pos, off_t offset)
1211 int len;
1212 len = strlen(*p);
1213 if(*pos >= offset) {
1214 /* the complete line is valid */
1215 *p += len;
1216 *pos += len;
1217 return;
1219 if(*pos+len > offset) {
1220 /* use the second part of the line */
1221 int i = offset-*pos;
1222 memmove(*p,*p+i,len-i);
1223 *p += len-i;
1224 *pos += len;
1225 return;
1227 /* discard the complete line */
1228 *pos += len;
1231 int get_locks_status(char *buffer, char **start, off_t offset, int length)
1233 struct file_lock *fl;
1234 struct file_lock *bfl;
1235 char *q = buffer;
1236 off_t pos = 0;
1237 int i;
1239 for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
1240 lock_get_status(q, fl, i, "");
1241 move_lock_status(&q, &pos, offset);
1243 if(pos >= offset+length)
1244 goto done;
1246 if ((bfl = fl->fl_nextblock) == NULL)
1247 continue;
1248 do {
1249 lock_get_status(q, bfl, i, " ->");
1250 move_lock_status(&q, &pos, offset);
1252 if(pos >= offset+length)
1253 goto done;
1254 } while ((bfl = bfl->fl_nextblock) != fl);
1256 done:
1257 *start = buffer;
1258 if(q-buffer < length)
1259 return (q-buffer);
1260 return length;