elf2coff isn't a generic tool for all linux ports
[linux-2.6/linux-mips.git] / fs / locks.c
blob909c2eacb426785b87e7dd35744ddcecde6214e9
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 (begemot@bgm.rosprint.net), 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/sched.h>
110 #include <linux/kernel.h>
111 #include <linux/errno.h>
112 #include <linux/stat.h>
113 #include <linux/fcntl.h>
114 #include <linux/smp.h>
115 #include <linux/smp_lock.h>
117 #include <asm/uaccess.h>
119 #define OFFSET_MAX ((off_t)0x7fffffff) /* FIXME: move elsewhere? */
121 static int flock_make_lock(struct file *filp, struct file_lock *fl,
122 unsigned int cmd);
123 static int posix_make_lock(struct file *filp, struct file_lock *fl,
124 struct flock *l);
125 static int flock_locks_conflict(struct file_lock *caller_fl,
126 struct file_lock *sys_fl);
127 static int posix_locks_conflict(struct file_lock *caller_fl,
128 struct file_lock *sys_fl);
129 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
130 static int flock_lock_file(struct file *filp, struct file_lock *caller,
131 unsigned int wait);
132 static int posix_locks_deadlock(struct file_lock *caller,
133 struct file_lock *blocker);
135 static struct file_lock *locks_alloc_lock(struct file_lock *fl);
136 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
137 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait);
138 static char *lock_get_status(struct file_lock *fl, int id, char *pfx);
140 static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter);
141 static void locks_delete_block(struct file_lock *blocker, struct file_lock *waiter);
142 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait);
144 struct file_lock *file_lock_table = NULL;
146 /* Free lock not inserted in any queue.
148 static inline void locks_free_lock(struct file_lock *fl)
150 if (waitqueue_active(&fl->fl_wait))
151 panic("Aarggh: attempting to free lock with active wait queue - shoot Andy");
153 if (fl->fl_nextblock != NULL || fl->fl_prevblock != NULL)
154 panic("Aarggh: attempting to free lock with active block list - shoot Andy");
156 kfree(fl);
157 return;
160 /* Check if two locks overlap each other.
162 static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
164 return ((fl1->fl_end >= fl2->fl_start) &&
165 (fl2->fl_end >= fl1->fl_start));
168 /* Check whether two locks have the same owner
170 static inline int
171 locks_same_owner(struct file_lock *fl1, struct file_lock *fl2)
173 return (fl1->fl_owner == fl2->fl_owner) &&
174 (fl1->fl_pid == fl2->fl_pid);
177 /* Insert waiter into blocker's block list.
178 * We use a circular list so that processes can be easily woken up in
179 * the order they blocked. The documentation doesn't require this but
180 * it seems seems like the reasonable thing to do.
182 static void locks_insert_block(struct file_lock *blocker,
183 struct file_lock *waiter)
185 struct file_lock *prevblock;
187 if (blocker->fl_prevblock == NULL)
188 /* No previous waiters - list is empty */
189 prevblock = blocker;
190 else
191 /* Previous waiters exist - add to end of list */
192 prevblock = blocker->fl_prevblock;
194 prevblock->fl_nextblock = waiter;
195 blocker->fl_prevblock = waiter;
196 waiter->fl_nextblock = blocker;
197 waiter->fl_prevblock = prevblock;
199 return;
202 /* Remove waiter from blocker's block list.
203 * When blocker ends up pointing to itself then the list is empty.
205 static void locks_delete_block(struct file_lock *blocker,
206 struct file_lock *waiter)
208 struct file_lock *nextblock;
209 struct file_lock *prevblock;
211 nextblock = waiter->fl_nextblock;
212 prevblock = waiter->fl_prevblock;
214 if (nextblock == NULL)
215 return;
217 nextblock->fl_prevblock = prevblock;
218 prevblock->fl_nextblock = nextblock;
220 waiter->fl_prevblock = waiter->fl_nextblock = NULL;
221 if (blocker->fl_nextblock == blocker)
222 /* No more locks on blocker's blocked list */
223 blocker->fl_prevblock = blocker->fl_nextblock = NULL;
224 return;
227 /* The following two are for the benefit of lockd.
229 void
230 posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
232 locks_insert_block(blocker, waiter);
233 return;
236 void
237 posix_unblock_lock(struct file_lock *waiter)
239 if (waiter->fl_prevblock)
240 locks_delete_block(waiter->fl_prevblock, waiter);
241 return;
244 /* Wake up processes blocked waiting for blocker.
245 * If told to wait then schedule the processes until the block list
246 * is empty, otherwise empty the block list ourselves.
248 static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
250 struct file_lock *waiter;
252 while ((waiter = blocker->fl_nextblock) != NULL) {
253 if (waiter->fl_notify)
254 waiter->fl_notify(waiter);
255 wake_up(&waiter->fl_wait);
256 if (wait)
257 /* Let the blocked process remove waiter from the
258 * block list when it gets scheduled.
260 schedule();
261 else
262 /* Remove waiter from the block list, because by the
263 * time it wakes up blocker won't exist any more.
265 locks_delete_block(blocker, waiter);
267 return;
270 /* flock() system call entry point. Apply a FL_FLOCK style lock to
271 * an open file descriptor.
273 asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
275 struct file_lock file_lock;
276 struct file *filp;
277 int error;
279 lock_kernel();
280 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
281 error = -EBADF;
282 else if (!flock_make_lock(filp, &file_lock, cmd))
283 error = -EINVAL;
284 else if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
285 error = -EBADF;
286 else
287 error = flock_lock_file(filp, &file_lock,
288 (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
289 unlock_kernel();
290 return (error);
293 /* Report the first existing lock that would conflict with l.
294 * This implements the F_GETLK command of fcntl().
296 int fcntl_getlk(unsigned int fd, struct flock *l)
298 struct flock flock;
299 struct file *filp;
300 struct dentry *dentry;
301 struct inode *inode;
302 struct file_lock *fl,file_lock;
303 int error;
305 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
306 return -EBADF;
307 if (copy_from_user(&flock, l, sizeof(flock)))
308 return -EFAULT;
310 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
311 return -EINVAL;
313 dentry = filp->f_dentry;
314 if (!dentry)
315 return -EINVAL;
317 inode = dentry->d_inode;
318 if (!inode)
319 return -EINVAL;
321 if (!posix_make_lock(filp, &file_lock, &flock))
322 return -EINVAL;
324 if (filp->f_op->lock) {
325 error = filp->f_op->lock(inode, filp, F_GETLK, &file_lock);
326 if (error < 0)
327 return error;
328 fl = &file_lock;
329 } else {
330 fl = posix_test_lock(filp, &file_lock);
333 if (fl != NULL) {
334 flock.l_pid = fl->fl_pid;
335 flock.l_start = fl->fl_start;
336 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
337 fl->fl_end - fl->fl_start + 1;
338 flock.l_whence = 0;
339 flock.l_type = fl->fl_type;
340 return (copy_to_user(l, &flock, sizeof(flock)) ? -EFAULT : 0);
341 } else {
342 flock.l_type = F_UNLCK;
345 return (copy_to_user(l, &flock, sizeof(flock)) ? -EFAULT : 0);
348 /* Apply the lock described by l to an open file descriptor.
349 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
351 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
353 struct file *filp;
354 struct file_lock file_lock;
355 struct flock flock;
356 struct dentry * dentry;
357 struct inode *inode;
358 int error;
360 /* Get arguments and validate them ...
363 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
364 return -EBADF;
366 if (!(dentry = filp->f_dentry))
367 return -EINVAL;
369 if (!(inode = dentry->d_inode))
370 return -EINVAL;
372 /* Don't allow mandatory locks on files that may be memory mapped
373 * and shared.
375 if (IS_MANDLOCK(inode) &&
376 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
377 inode->i_mmap) {
378 struct vm_area_struct *vma = inode->i_mmap;
379 do {
380 if (vma->vm_flags & VM_MAYSHARE)
381 return (-EAGAIN);
382 } while ((vma = vma->vm_next_share) != NULL);
385 if (copy_from_user(&flock, l, sizeof(flock)))
386 return (-EFAULT);
387 if (!posix_make_lock(filp, &file_lock, &flock))
388 return (-EINVAL);
390 switch (flock.l_type) {
391 case F_RDLCK:
392 if (!(filp->f_mode & 1))
393 return (-EBADF);
394 break;
395 case F_WRLCK:
396 if (!(filp->f_mode & 2))
397 return (-EBADF);
398 break;
399 case F_UNLCK:
400 break;
401 case F_SHLCK:
402 case F_EXLCK:
403 #ifdef __sparc__
404 /* warn a bit for now, but don't overdo it */
406 static int count = 0;
407 if (!count) {
408 count=1;
409 printk(KERN_WARNING
410 "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
411 current->pid, current->comm);
414 if (!(filp->f_mode & 3))
415 return (-EBADF);
416 break;
417 #endif
418 default:
419 return (-EINVAL);
422 if (filp->f_op->lock != NULL) {
423 error = filp->f_op->lock(inode, filp, cmd, &file_lock);
424 if (error < 0)
425 return (error);
428 return (posix_lock_file(filp, &file_lock, cmd == F_SETLKW));
431 /* This function is called when the file is closed.
433 void locks_remove_locks(struct task_struct *task, struct file *filp)
435 struct file_lock file_lock, *fl;
436 struct file_lock **before;
437 struct inode * inode;
439 /* For POSIX locks we free all locks on this file for the given task.
440 * For FLOCK we only free locks on this *open* file if it is the last
441 * close on that file.
443 inode = filp->f_dentry->d_inode;
444 before = &inode->i_flock;
446 while ((fl = *before) != NULL) {
447 if (((fl->fl_flags & FL_POSIX) && (fl->fl_owner == task)) ||
448 ((fl->fl_flags & FL_FLOCK) && (fl->fl_file == filp) &&
449 (filp->f_count == 1))) {
450 file_lock = *fl;
451 locks_delete_lock(before, 0);
452 if (filp->f_op->lock) {
453 file_lock.fl_type = F_UNLCK;
454 filp->f_op->lock(inode, filp, F_SETLK, &file_lock);
455 /* List may have changed: */
456 before = &inode->i_flock;
458 } else {
459 before = &fl->fl_next;
463 return;
466 struct file_lock *
467 posix_test_lock(struct file *filp, struct file_lock *fl)
469 struct file_lock *cfl;
471 for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
472 if (!(cfl->fl_flags & FL_POSIX))
473 continue;
474 if (posix_locks_conflict(cfl, fl))
475 break;
478 return (cfl);
481 int locks_verify_locked(struct inode *inode)
483 /* Candidates for mandatory locking have the setgid bit set
484 * but no group execute bit - an otherwise meaningless combination.
486 if (IS_MANDLOCK(inode) &&
487 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
488 return (locks_mandatory_locked(inode));
489 return (0);
492 int locks_verify_area(int read_write, struct inode *inode, struct file *filp,
493 unsigned int offset, unsigned int count)
495 /* Candidates for mandatory locking have the setgid bit set
496 * but no group execute bit - an otherwise meaningless combination.
498 if (IS_MANDLOCK(inode) &&
499 (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
500 return (locks_mandatory_area(read_write, inode, filp, offset,
501 count));
502 return (0);
505 int locks_mandatory_locked(struct inode *inode)
507 struct file_lock *fl;
509 /* Search the lock list for this inode for any POSIX locks.
511 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
512 if (!(fl->fl_flags & FL_POSIX))
513 continue;
514 if (fl->fl_owner != current)
515 return (-EAGAIN);
517 return (0);
520 int locks_mandatory_area(int read_write, struct inode *inode,
521 struct file *filp, unsigned int offset,
522 unsigned int count)
524 struct file_lock *fl;
525 struct file_lock tfl;
527 memset(&tfl, 0, sizeof(tfl));
529 tfl.fl_file = filp;
530 tfl.fl_flags = FL_POSIX | FL_ACCESS;
531 tfl.fl_owner = current;
532 tfl.fl_pid = current->pid;
533 tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
534 tfl.fl_start = offset;
535 tfl.fl_end = offset + count - 1;
537 repeat:
538 /* Search the lock list for this inode for locks that conflict with
539 * the proposed read/write.
541 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
542 if (!(fl->fl_flags & FL_POSIX))
543 continue;
544 /* Block for writes against a "read" lock,
545 * and both reads and writes against a "write" lock.
547 if (posix_locks_conflict(fl, &tfl)) {
548 if (filp && (filp->f_flags & O_NONBLOCK))
549 return (-EAGAIN);
550 if (current->signal & ~current->blocked)
551 return (-ERESTARTSYS);
552 if (posix_locks_deadlock(&tfl, fl))
553 return (-EDEADLK);
555 locks_insert_block(fl, &tfl);
556 interruptible_sleep_on(&tfl.fl_wait);
557 locks_delete_block(fl, &tfl);
559 if (current->signal & ~current->blocked)
560 return (-ERESTARTSYS);
561 /* If we've been sleeping someone might have
562 * changed the permissions behind our back.
564 if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
565 break;
566 goto repeat;
569 return (0);
572 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
573 * style lock.
575 static int posix_make_lock(struct file *filp, struct file_lock *fl,
576 struct flock *l)
578 off_t start;
580 memset(fl, 0, sizeof(*fl));
582 fl->fl_flags = FL_POSIX;
584 switch (l->l_type) {
585 case F_RDLCK:
586 case F_WRLCK:
587 case F_UNLCK:
588 fl->fl_type = l->l_type;
589 break;
590 default:
591 return (0);
594 switch (l->l_whence) {
595 case 0: /*SEEK_SET*/
596 start = 0;
597 break;
598 case 1: /*SEEK_CUR*/
599 start = filp->f_pos;
600 break;
601 case 2: /*SEEK_END*/
602 start = filp->f_dentry->d_inode->i_size;
603 break;
604 default:
605 return (0);
608 if (((start += l->l_start) < 0) || (l->l_len < 0))
609 return (0);
610 fl->fl_start = start; /* we record the absolute position */
611 if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
612 fl->fl_end = OFFSET_MAX;
614 fl->fl_file = filp;
615 fl->fl_owner = current;
616 fl->fl_pid = current->pid;
618 return (1);
621 /* Verify a call to flock() and fill in a file_lock structure with
622 * an appropriate FLOCK lock.
624 static int flock_make_lock(struct file *filp, struct file_lock *fl,
625 unsigned int cmd)
627 memset(fl, 0, sizeof(*fl));
629 if (!filp->f_dentry) /* just in case */
630 return (0);
632 switch (cmd & ~LOCK_NB) {
633 case LOCK_SH:
634 fl->fl_type = F_RDLCK;
635 break;
636 case LOCK_EX:
637 fl->fl_type = F_WRLCK;
638 break;
639 case LOCK_UN:
640 fl->fl_type = F_UNLCK;
641 break;
642 default:
643 return (0);
646 fl->fl_flags = FL_FLOCK;
647 fl->fl_start = 0;
648 fl->fl_end = OFFSET_MAX;
649 fl->fl_file = filp;
650 fl->fl_owner = NULL;
652 return (1);
655 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
656 * checking before calling the locks_conflict().
658 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
660 /* POSIX locks owned by the same process do not conflict with
661 * each other.
663 if (!(sys_fl->fl_flags & FL_POSIX) ||
664 locks_same_owner(caller_fl, sys_fl))
665 return (0);
667 return (locks_conflict(caller_fl, sys_fl));
670 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
671 * checking before calling the locks_conflict().
673 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
675 /* FLOCK locks referring to the same filp do not conflict with
676 * each other.
678 if (!(sys_fl->fl_flags & FL_FLOCK) ||
679 (caller_fl->fl_file == sys_fl->fl_file))
680 return (0);
682 return (locks_conflict(caller_fl, sys_fl));
685 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
686 * checks for overlapping locks and shared/exclusive status.
688 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
690 if (!locks_overlap(caller_fl, sys_fl))
691 return (0);
693 switch (caller_fl->fl_type) {
694 case F_RDLCK:
695 return (sys_fl->fl_type == F_WRLCK);
697 case F_WRLCK:
698 return (1);
700 default:
701 printk("locks_conflict(): impossible lock type - %d\n",
702 caller_fl->fl_type);
703 break;
705 return (0); /* This should never happen */
708 /* This function tests for deadlock condition before putting a process to
709 * sleep. The detection scheme is no longer recursive. Recursive was neat,
710 * but dangerous - we risked stack corruption if the lock data was bad, or
711 * if the recursion was too deep for any other reason.
713 * We rely on the fact that a task can only be on one lock's wait queue
714 * at a time. When we find blocked_task on a wait queue we can re-search
715 * with blocked_task equal to that queue's owner, until either blocked_task
716 * isn't found, or blocked_task is found on a queue owned by my_task.
718 * Note: the above assumption may not be true when handling lock requests
719 * from a broken NFS client. But broken NFS clients have a lot more to
720 * worry about than proper deadlock detection anyway... --okir
722 static int posix_locks_deadlock(struct file_lock *caller_fl,
723 struct file_lock *block_fl)
725 struct file_lock *fl;
726 struct file_lock *bfl;
727 void *caller_owner, *blocked_owner;
728 unsigned int caller_pid, blocked_pid;
730 caller_owner = caller_fl->fl_owner;
731 caller_pid = caller_fl->fl_pid;
732 blocked_owner = block_fl->fl_owner;
733 blocked_pid = block_fl->fl_pid;
735 next_task:
736 if (caller_owner == blocked_owner && caller_pid == blocked_pid)
737 return (1);
738 for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
739 if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
740 continue;
741 for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
742 if (bfl->fl_owner == blocked_owner &&
743 bfl->fl_pid == blocked_pid) {
744 if (fl->fl_owner == caller_owner &&
745 fl->fl_pid == caller_pid) {
746 return (1);
748 blocked_owner = fl->fl_owner;
749 blocked_pid = fl->fl_pid;
750 goto next_task;
754 return (0);
757 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
758 * the head of the list, but that's secret knowledge known only to the next
759 * two functions.
761 static int flock_lock_file(struct file *filp, struct file_lock *caller,
762 unsigned int wait)
764 struct file_lock *fl;
765 struct file_lock *new_fl;
766 struct file_lock **before;
767 struct inode * inode = filp->f_dentry->d_inode;
768 int change = 0;
770 before = &inode->i_flock;
771 while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
772 if (caller->fl_file == fl->fl_file) {
773 if (caller->fl_type == fl->fl_type)
774 return (0);
775 change = 1;
776 break;
778 before = &fl->fl_next;
780 /* change means that we are changing the type of an existing lock, or
781 * or else unlocking it.
783 if (change)
784 locks_delete_lock(before, caller->fl_type != F_UNLCK);
785 if (caller->fl_type == F_UNLCK)
786 return (0);
787 if ((new_fl = locks_alloc_lock(caller)) == NULL)
788 return (-ENOLCK);
789 repeat:
790 for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
791 fl = fl->fl_next) {
792 if (!flock_locks_conflict(new_fl, fl))
793 continue;
794 if (!wait) {
795 locks_free_lock(new_fl);
796 return (-EAGAIN);
798 if (current->signal & ~current->blocked) {
799 /* Note: new_fl is not in any queue at this
800 * point, so we must use locks_free_lock()
801 * instead of locks_delete_lock()
802 * Dmitry Gorodchanin 09/02/96.
804 locks_free_lock(new_fl);
805 return (-ERESTARTSYS);
807 locks_insert_block(fl, new_fl);
808 interruptible_sleep_on(&new_fl->fl_wait);
809 locks_delete_block(fl, new_fl);
810 if (current->signal & ~current->blocked) {
811 /* Awakened by a signal. Free the new
812 * lock and return an error.
814 locks_free_lock(new_fl);
815 return (-ERESTARTSYS);
817 goto repeat;
819 locks_insert_lock(&inode->i_flock, new_fl);
820 return (0);
823 /* Add a POSIX style lock to a file.
824 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
825 * task, then by starting address
827 * Kai Petzke writes:
828 * To make freeing a lock much faster, we keep a pointer to the lock before the
829 * actual one. But the real gain of the new coding was, that lock_it() and
830 * unlock_it() became one function.
832 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
835 int posix_lock_file(struct file *filp, struct file_lock *caller,
836 unsigned int wait)
838 struct file_lock *fl;
839 struct file_lock *new_fl;
840 struct file_lock *left = NULL;
841 struct file_lock *right = NULL;
842 struct file_lock **before;
843 struct inode * inode = filp->f_dentry->d_inode;
844 int added = 0;
846 if (caller->fl_type != F_UNLCK) {
847 repeat:
848 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
849 if (!(fl->fl_flags & FL_POSIX))
850 continue;
851 if (!posix_locks_conflict(caller, fl))
852 continue;
853 if (!wait)
854 return (-EAGAIN);
855 if (current->signal & ~current->blocked)
856 return (-ERESTARTSYS);
857 if (posix_locks_deadlock(caller, fl))
858 return (-EDEADLK);
859 locks_insert_block(fl, caller);
860 interruptible_sleep_on(&caller->fl_wait);
861 locks_delete_block(fl, caller);
862 if (current->signal & ~current->blocked)
863 return (-ERESTARTSYS);
864 goto repeat;
868 /* Find the first old lock with the same owner as the new lock.
871 before = &inode->i_flock;
873 /* First skip locks owned by other processes.
875 while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
876 !locks_same_owner(caller, fl))) {
877 before = &fl->fl_next;
880 /* Process locks with this owner.
882 while ((fl = *before) && locks_same_owner(caller, fl)) {
883 /* Detect adjacent or overlapping regions (if same lock type)
885 if (caller->fl_type == fl->fl_type) {
886 if (fl->fl_end < caller->fl_start - 1)
887 goto next_lock;
888 /* If the next lock in the list has entirely bigger
889 * addresses than the new one, insert the lock here.
891 if (fl->fl_start > caller->fl_end + 1)
892 break;
894 /* If we come here, the new and old lock are of the
895 * same type and adjacent or overlapping. Make one
896 * lock yielding from the lower start address of both
897 * locks to the higher end address.
899 if (fl->fl_start > caller->fl_start)
900 fl->fl_start = caller->fl_start;
901 else
902 caller->fl_start = fl->fl_start;
903 if (fl->fl_end < caller->fl_end)
904 fl->fl_end = caller->fl_end;
905 else
906 caller->fl_end = fl->fl_end;
907 if (added) {
908 locks_delete_lock(before, 0);
909 continue;
911 caller = fl;
912 added = 1;
914 else {
915 /* Processing for different lock types is a bit
916 * more complex.
918 if (fl->fl_end < caller->fl_start)
919 goto next_lock;
920 if (fl->fl_start > caller->fl_end)
921 break;
922 if (caller->fl_type == F_UNLCK)
923 added = 1;
924 if (fl->fl_start < caller->fl_start)
925 left = fl;
926 /* If the next lock in the list has a higher end
927 * address than the new one, insert the new one here.
929 if (fl->fl_end > caller->fl_end) {
930 right = fl;
931 break;
933 if (fl->fl_start >= caller->fl_start) {
934 /* The new lock completely replaces an old
935 * one (This may happen several times).
937 if (added) {
938 locks_delete_lock(before, 0);
939 continue;
941 /* Replace the old lock with the new one.
942 * Wake up anybody waiting for the old one,
943 * as the change in lock type might satisfy
944 * their needs.
946 locks_wake_up_blocks(fl, 0);
947 fl->fl_start = caller->fl_start;
948 fl->fl_end = caller->fl_end;
949 fl->fl_type = caller->fl_type;
950 fl->fl_u = caller->fl_u;
951 caller = fl;
952 added = 1;
955 /* Go on to next lock.
957 next_lock:
958 before = &fl->fl_next;
961 if (!added) {
962 if (caller->fl_type == F_UNLCK)
963 return (0);
964 if ((new_fl = locks_alloc_lock(caller)) == NULL)
965 return (-ENOLCK);
966 locks_insert_lock(before, new_fl);
968 if (right) {
969 if (left == right) {
970 /* The new lock breaks the old one in two pieces, so we
971 * have to allocate one more lock (in this case, even
972 * F_UNLCK may fail!).
974 if ((left = locks_alloc_lock(right)) == NULL) {
975 if (!added)
976 locks_delete_lock(before, 0);
977 return (-ENOLCK);
979 locks_insert_lock(before, left);
981 right->fl_start = caller->fl_end + 1;
982 locks_wake_up_blocks(right, 0);
984 if (left) {
985 left->fl_end = caller->fl_start - 1;
986 locks_wake_up_blocks(left, 0);
988 return (0);
991 /* Allocate new lock.
992 * Initialize its fields from fl. The lock is not inserted into any
993 * lists until locks_insert_lock() or locks_insert_block() are called.
995 static struct file_lock *locks_alloc_lock(struct file_lock *fl)
997 struct file_lock *tmp;
999 /* Okay, let's make a new file_lock structure... */
1000 if ((tmp = (struct file_lock *)kmalloc(sizeof(struct file_lock),
1001 GFP_ATOMIC)) == NULL)
1002 return (tmp);
1004 memset(tmp, 0, sizeof(*tmp));
1006 tmp->fl_flags = fl->fl_flags;
1007 tmp->fl_owner = fl->fl_owner;
1008 tmp->fl_pid = fl->fl_pid;
1009 tmp->fl_file = fl->fl_file;
1010 tmp->fl_type = fl->fl_type;
1011 tmp->fl_start = fl->fl_start;
1012 tmp->fl_end = fl->fl_end;
1013 tmp->fl_notify = fl->fl_notify;
1014 tmp->fl_u = fl->fl_u;
1016 return (tmp);
1019 /* Insert file lock fl into an inode's lock list at the position indicated
1020 * by pos. At the same time add the lock to the global file lock list.
1022 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
1024 fl->fl_nextlink = file_lock_table;
1025 fl->fl_prevlink = NULL;
1026 if (file_lock_table != NULL)
1027 file_lock_table->fl_prevlink = fl;
1028 file_lock_table = fl;
1029 fl->fl_next = *pos; /* insert into file's list */
1030 *pos = fl;
1032 return;
1035 /* Delete a lock and free it.
1036 * First remove our lock from the active lock lists. Then call
1037 * locks_wake_up_blocks() to wake up processes that are blocked
1038 * waiting for this lock. Finally free the lock structure.
1040 static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
1042 struct file_lock *thisfl;
1043 struct file_lock *prevfl;
1044 struct file_lock *nextfl;
1046 thisfl = *thisfl_p;
1047 *thisfl_p = thisfl->fl_next;
1049 prevfl = thisfl->fl_prevlink;
1050 nextfl = thisfl->fl_nextlink;
1052 if (nextfl != NULL)
1053 nextfl->fl_prevlink = prevfl;
1055 if (prevfl != NULL)
1056 prevfl->fl_nextlink = nextfl;
1057 else
1058 file_lock_table = nextfl;
1060 locks_wake_up_blocks(thisfl, wait);
1061 locks_free_lock(thisfl);
1063 return;
1067 static char *lock_get_status(struct file_lock *fl, int id, char *pfx)
1069 static char temp[129];
1070 char *p = temp;
1071 struct inode *inode;
1073 inode = fl->fl_file->f_dentry->d_inode;
1075 p += sprintf(p, "%d:%s ", id, pfx);
1076 if (fl->fl_flags & FL_POSIX) {
1077 p += sprintf(p, "%6s %s ",
1078 (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1079 (IS_MANDLOCK(inode) &&
1080 (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1081 "MANDATORY" : "ADVISORY ");
1083 else {
1084 p += sprintf(p, "FLOCK ADVISORY ");
1086 p += sprintf(p, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
1087 p += sprintf(p, "%d %s:%ld %ld %ld ",
1088 fl->fl_pid,
1089 kdevname(inode->i_dev), inode->i_ino, fl->fl_start,
1090 fl->fl_end);
1091 sprintf(p, "%08lx %08lx %08lx %08lx %08lx\n",
1092 (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
1093 (long)fl->fl_next, (long)fl->fl_nextblock);
1094 return (temp);
1097 static inline int copy_lock_status(char *p, char **q, off_t pos, int len,
1098 off_t offset, int length)
1100 int i;
1102 i = pos - offset;
1103 if (i > 0) {
1104 if (i >= length) {
1105 i = len + length - i;
1106 memcpy(*q, p, i);
1107 *q += i;
1108 return (0);
1110 if (i < len) {
1111 p += len - i;
1113 else
1114 i = len;
1115 memcpy(*q, p, i);
1116 *q += i;
1119 return (1);
1122 int get_locks_status(char *buffer, char **start, off_t offset, int length)
1124 struct file_lock *fl;
1125 struct file_lock *bfl;
1126 char *p;
1127 char *q = buffer;
1128 int i;
1129 int len;
1130 off_t pos = 0;
1132 for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
1133 p = lock_get_status(fl, i, "");
1134 len = strlen(p);
1135 pos += len;
1136 if (!copy_lock_status(p, &q, pos, len, offset, length))
1137 goto done;
1138 if ((bfl = fl->fl_nextblock) == NULL)
1139 continue;
1140 do {
1141 p = lock_get_status(bfl, i, " ->");
1142 len = strlen(p);
1143 pos += len;
1144 if (!copy_lock_status(p, &q, pos, len, offset, length))
1145 goto done;
1146 } while ((bfl = bfl->fl_nextblock) != fl);
1148 done:
1149 if (q != buffer)
1150 *start = buffer;
1151 return (q - buffer);