2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Scooter Morris at Genentech Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_debug_lockf.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
52 #include <sys/lockf.h>
55 * This variable controls the maximum number of processes that will
56 * be checked in doing deadlock detection.
58 static int maxlockdepth = MAXDEPTH;
61 #include <sys/sysctl.h>
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/inode.h>
67 static int lockf_debug = 0;
68 SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
71 MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
73 #define NOLOCKF (struct lockf *)0
76 static int lf_clearlock(struct lockf *, struct lockf **);
77 static int lf_findoverlap(struct lockf *,
78 struct lockf *, int, struct lockf ***, struct lockf **);
80 lf_getblock(struct lockf *);
81 static int lf_getlock(struct lockf *, struct flock *);
82 static int lf_setlock(struct lockf *, struct vnode *, struct lockf **);
83 static void lf_split(struct lockf *, struct lockf *, struct lockf **);
84 static void lf_wakelock(struct lockf *);
86 static void lf_print(char *, struct lockf *);
87 static void lf_printlist(char *, struct lockf *);
91 * Advisory record locking support
94 lf_advlock(ap, head, size)
95 struct vop_advlock_args /* {
105 struct flock *fl = ap->a_fl;
107 struct vnode *vp = ap->a_vp;
108 off_t start, end, oadd;
109 struct lockf *clean, *n;
113 * Convert the flock structure into a start and end.
115 switch (fl->l_whence) {
120 * Caller is responsible for adding any necessary offset
121 * when SEEK_CUR is used.
127 if (size > OFF_MAX ||
128 (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
130 start = size + fl->l_start;
145 } else if (fl->l_len == 0)
148 oadd = fl->l_len - 1;
149 if (oadd > OFF_MAX - start)
154 * Avoid the common case of unlocking when inode has no locks.
156 if (*head == (struct lockf *)0) {
157 if (ap->a_op != F_SETLK) {
158 fl->l_type = F_UNLCK;
163 * Allocate a spare structure in case we have to split.
166 if (ap->a_op == F_SETLK || ap->a_op == F_UNLCK) {
167 MALLOC(clean, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
168 clean->lf_next = NULL;
171 * Create the lockf structure
173 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
174 lock->lf_start = start;
176 lock->lf_id = ap->a_id;
178 * XXX The problem is that VTOI is ufs specific, so it will
179 * break LOCKF_DEBUG for all other FS's other than UFS because
180 * it casts the vnode->data ptr to struct inode *.
182 /* lock->lf_inode = VTOI(ap->a_vp); */
183 lock->lf_inode = (struct inode *)0;
184 lock->lf_type = fl->l_type;
185 lock->lf_head = head;
186 lock->lf_next = (struct lockf *)0;
187 TAILQ_INIT(&lock->lf_blkhd);
188 lock->lf_flags = ap->a_flags;
190 * Do the requested operation.
195 error = lf_setlock(lock, vp, &clean);
199 error = lf_clearlock(lock, &clean);
200 lock->lf_next = clean;
205 error = lf_getlock(lock, fl);
206 lock->lf_next = clean;
211 lock->lf_next = clean;
217 for (lock = clean; lock != NULL; ) {
226 * Set a byte-range lock.
229 lf_setlock(lock, vp, clean)
232 struct lockf **clean;
235 struct lockf **head = lock->lf_head;
236 struct lockf **prev, *overlap, *ltmp;
237 static char lockstr[] = "lockf";
238 int ovcase, priority, needtolink, error;
242 lf_print("lf_setlock", lock);
243 #endif /* LOCKF_DEBUG */
249 if (lock->lf_type == F_WRLCK)
253 * Scan lock list for this file looking for locks that would block us.
255 while ((block = lf_getblock(lock))) {
257 * Free the structure and return if nonblocking.
259 if ((lock->lf_flags & F_WAIT) == 0) {
260 lock->lf_next = *clean;
265 * We are blocked. Since flock style locks cover
266 * the whole file, there is no chance for deadlock.
267 * For byte-range locks we must check for deadlock.
269 * Deadlock detection is done by looking through the
270 * wait channels to see if there are any cycles that
271 * involve us. MAXDEPTH is set just to make sure we
272 * do not go off into neverland.
274 if ((lock->lf_flags & F_POSIX) &&
275 (block->lf_flags & F_POSIX)) {
279 struct lockf *waitblock;
282 /* The block is waiting on something */
283 wproc = (struct proc *)block->lf_id;
287 FOREACH_THREAD_IN_PROC(wproc, td) {
289 while (td->td_wchan &&
290 (td->td_wmesg == lockstr) &&
291 (i++ < maxlockdepth)) {
292 waitblock = (struct lockf *)td->td_wchan;
293 /* Get the owner of the blocking lock */
294 waitblock = waitblock->lf_next;
295 if ((waitblock->lf_flags & F_POSIX) == 0)
297 nproc = (struct proc *)waitblock->lf_id;
298 if (nproc == (struct proc *)lock->lf_id) {
301 lock->lf_next = *clean;
314 * For flock type locks, we must first remove
315 * any shared locks that we hold before we sleep
316 * waiting for an exclusive lock.
318 if ((lock->lf_flags & F_FLOCK) &&
319 lock->lf_type == F_WRLCK) {
320 lock->lf_type = F_UNLCK;
321 (void) lf_clearlock(lock, clean);
322 lock->lf_type = F_WRLCK;
325 * Add our lock to the blocked list and sleep until we're free.
326 * Remember who blocked us (for deadlock detection).
328 lock->lf_next = block;
329 TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
331 if (lockf_debug & 1) {
332 lf_print("lf_setlock: blocking on", block);
333 lf_printlist("lf_setlock", block);
335 #endif /* LOCKF_DEBUG */
336 error = msleep(lock, VI_MTX(vp), priority, lockstr, 0);
338 * We may have been awakened by a signal and/or by a
339 * debugger continuing us (in which cases we must remove
340 * ourselves from the blocked list) and/or by another
341 * process releasing a lock (in which case we have
342 * already been removed from the blocked list and our
343 * lf_next field set to NOLOCKF).
346 TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
347 lock->lf_next = NOLOCKF;
350 lock->lf_next = *clean;
356 * No blocks!! Add the lock. Note that we will
357 * downgrade or upgrade any overlapping locks this
358 * process already owns.
360 * Skip over locks owned by other processes.
361 * Handle any locks that overlap and are owned by ourselves.
367 ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
369 block = overlap->lf_next;
374 * 2) overlap contains lock
375 * 3) lock contains overlap
376 * 4) overlap starts before lock
377 * 5) overlap ends after lock
380 case 0: /* no overlap */
383 lock->lf_next = overlap;
387 case 1: /* overlap == lock */
389 * If downgrading lock, others may be
390 * able to acquire it.
392 if (lock->lf_type == F_RDLCK &&
393 overlap->lf_type == F_WRLCK)
394 lf_wakelock(overlap);
395 overlap->lf_type = lock->lf_type;
396 lock->lf_next = *clean;
398 lock = overlap; /* for debug output below */
401 case 2: /* overlap contains lock */
403 * Check for common starting point and different types.
405 if (overlap->lf_type == lock->lf_type) {
406 lock->lf_next = *clean;
408 lock = overlap; /* for debug output below */
411 if (overlap->lf_start == lock->lf_start) {
413 lock->lf_next = overlap;
414 overlap->lf_start = lock->lf_end + 1;
416 lf_split(overlap, lock, clean);
417 lf_wakelock(overlap);
420 case 3: /* lock contains overlap */
422 * If downgrading lock, others may be able to
423 * acquire it, otherwise take the list.
425 if (lock->lf_type == F_RDLCK &&
426 overlap->lf_type == F_WRLCK) {
427 lf_wakelock(overlap);
429 while (!TAILQ_EMPTY(&overlap->lf_blkhd)) {
430 ltmp = TAILQ_FIRST(&overlap->lf_blkhd);
431 TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
433 TAILQ_INSERT_TAIL(&lock->lf_blkhd,
435 ltmp->lf_next = lock;
439 * Add the new lock if necessary and delete the overlap.
443 lock->lf_next = overlap->lf_next;
444 prev = &lock->lf_next;
447 *prev = overlap->lf_next;
448 overlap->lf_next = *clean;
452 case 4: /* overlap starts before lock */
454 * Add lock after overlap on the list.
456 lock->lf_next = overlap->lf_next;
457 overlap->lf_next = lock;
458 overlap->lf_end = lock->lf_start - 1;
459 prev = &lock->lf_next;
460 lf_wakelock(overlap);
464 case 5: /* overlap ends after lock */
466 * Add the new lock before overlap.
470 lock->lf_next = overlap;
472 overlap->lf_start = lock->lf_end + 1;
473 lf_wakelock(overlap);
479 if (lockf_debug & 1) {
480 lf_print("lf_setlock: got the lock", lock);
481 lf_printlist("lf_setlock", lock);
483 #endif /* LOCKF_DEBUG */
488 * Remove a byte-range lock on an inode.
490 * Generally, find the lock (or an overlap to that lock)
491 * and remove it (or shrink it), then wakeup anyone we can.
494 lf_clearlock(unlock, clean)
495 struct lockf *unlock;
496 struct lockf **clean;
498 struct lockf **head = unlock->lf_head;
499 register struct lockf *lf = *head;
500 struct lockf *overlap, **prev;
506 if (unlock->lf_type != F_UNLCK)
507 panic("lf_clearlock: bad type");
509 lf_print("lf_clearlock", unlock);
510 #endif /* LOCKF_DEBUG */
512 while ((ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
514 * Wakeup the list of locks to be retried.
516 lf_wakelock(overlap);
520 case 1: /* overlap == lock */
521 *prev = overlap->lf_next;
522 overlap->lf_next = *clean;
526 case 2: /* overlap contains lock: split it */
527 if (overlap->lf_start == unlock->lf_start) {
528 overlap->lf_start = unlock->lf_end + 1;
531 lf_split(overlap, unlock, clean);
532 overlap->lf_next = unlock->lf_next;
535 case 3: /* lock contains overlap */
536 *prev = overlap->lf_next;
537 lf = overlap->lf_next;
538 overlap->lf_next = *clean;
542 case 4: /* overlap starts before lock */
543 overlap->lf_end = unlock->lf_start - 1;
544 prev = &overlap->lf_next;
545 lf = overlap->lf_next;
548 case 5: /* overlap ends after lock */
549 overlap->lf_start = unlock->lf_end + 1;
556 lf_printlist("lf_clearlock", unlock);
557 #endif /* LOCKF_DEBUG */
562 * Check whether there is a blocking lock,
563 * and if so return its process identifier.
567 register struct lockf *lock;
568 register struct flock *fl;
570 register struct lockf *block;
574 lf_print("lf_getlock", lock);
575 #endif /* LOCKF_DEBUG */
577 if ((block = lf_getblock(lock))) {
578 fl->l_type = block->lf_type;
579 fl->l_whence = SEEK_SET;
580 fl->l_start = block->lf_start;
581 if (block->lf_end == -1)
584 fl->l_len = block->lf_end - block->lf_start + 1;
585 if (block->lf_flags & F_POSIX)
586 fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
590 fl->l_type = F_UNLCK;
596 * Walk the list of locks for an inode and
597 * return the first blocking lock.
599 static struct lockf *
601 register struct lockf *lock;
603 struct lockf **prev, *overlap, *lf = *(lock->lf_head);
606 prev = lock->lf_head;
607 while ((ovcase = lf_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
609 * We've found an overlap, see if it blocks us
611 if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
614 * Nope, point to the next one on the list and
615 * see if it blocks us
617 lf = overlap->lf_next;
623 * Walk the list of locks for an inode to
624 * find an overlapping lock (if any).
626 * NOTE: this returns only the FIRST overlapping lock. There
627 * may be more than one.
630 lf_findoverlap(lf, lock, type, prev, overlap)
631 register struct lockf *lf;
634 struct lockf ***prev;
635 struct lockf **overlap;
644 lf_print("lf_findoverlap: looking for overlap in", lock);
645 #endif /* LOCKF_DEBUG */
646 start = lock->lf_start;
648 while (lf != NOLOCKF) {
649 if (((type & SELF) && lf->lf_id != lock->lf_id) ||
650 ((type & OTHERS) && lf->lf_id == lock->lf_id)) {
651 *prev = &lf->lf_next;
652 *overlap = lf = lf->lf_next;
657 lf_print("\tchecking", lf);
658 #endif /* LOCKF_DEBUG */
660 * OK, check for overlap
665 * 2) overlap contains lock
666 * 3) lock contains overlap
667 * 4) overlap starts before lock
668 * 5) overlap ends after lock
670 if ((lf->lf_end != -1 && start > lf->lf_end) ||
671 (end != -1 && lf->lf_start > end)) {
675 printf("no overlap\n");
676 #endif /* LOCKF_DEBUG */
677 if ((type & SELF) && end != -1 && lf->lf_start > end)
679 *prev = &lf->lf_next;
680 *overlap = lf = lf->lf_next;
683 if ((lf->lf_start == start) && (lf->lf_end == end)) {
687 printf("overlap == lock\n");
688 #endif /* LOCKF_DEBUG */
691 if ((lf->lf_start <= start) &&
693 ((lf->lf_end >= end) || (lf->lf_end == -1))) {
697 printf("overlap contains lock\n");
698 #endif /* LOCKF_DEBUG */
701 if (start <= lf->lf_start &&
703 (lf->lf_end != -1 && end >= lf->lf_end))) {
707 printf("lock contains overlap\n");
708 #endif /* LOCKF_DEBUG */
711 if ((lf->lf_start < start) &&
712 ((lf->lf_end >= start) || (lf->lf_end == -1))) {
716 printf("overlap starts before lock\n");
717 #endif /* LOCKF_DEBUG */
720 if ((lf->lf_start > start) &&
722 ((lf->lf_end > end) || (lf->lf_end == -1))) {
726 printf("overlap ends after lock\n");
727 #endif /* LOCKF_DEBUG */
730 panic("lf_findoverlap: default");
736 * Split a lock and a contained region into
737 * two or three locks as necessary.
740 lf_split(lock1, lock2, split)
743 struct lockf **split;
745 struct lockf *splitlock;
748 if (lockf_debug & 2) {
749 lf_print("lf_split", lock1);
750 lf_print("splitting from", lock2);
752 #endif /* LOCKF_DEBUG */
754 * Check to see if spliting into only two pieces.
756 if (lock1->lf_start == lock2->lf_start) {
757 lock1->lf_start = lock2->lf_end + 1;
758 lock2->lf_next = lock1;
761 if (lock1->lf_end == lock2->lf_end) {
762 lock1->lf_end = lock2->lf_start - 1;
763 lock2->lf_next = lock1->lf_next;
764 lock1->lf_next = lock2;
768 * Make a new lock consisting of the last part of
769 * the encompassing lock. We use the preallocated
770 * splitlock so we don't have to block.
773 KASSERT(splitlock != NULL, ("no split"));
774 *split = splitlock->lf_next;
775 bcopy(lock1, splitlock, sizeof *splitlock);
776 splitlock->lf_start = lock2->lf_end + 1;
777 TAILQ_INIT(&splitlock->lf_blkhd);
778 lock1->lf_end = lock2->lf_start - 1;
782 splitlock->lf_next = lock1->lf_next;
783 lock2->lf_next = splitlock;
784 lock1->lf_next = lock2;
791 lf_wakelock(listhead)
792 struct lockf *listhead;
794 register struct lockf *wakelock;
796 while (!TAILQ_EMPTY(&listhead->lf_blkhd)) {
797 wakelock = TAILQ_FIRST(&listhead->lf_blkhd);
798 TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
799 wakelock->lf_next = NOLOCKF;
802 lf_print("lf_wakelock: awakening", wakelock);
803 #endif /* LOCKF_DEBUG */
815 register struct lockf *lock;
818 printf("%s: lock %p for ", tag, (void *)lock);
819 if (lock->lf_flags & F_POSIX)
820 printf("proc %ld", (long)((struct proc *)lock->lf_id)->p_pid);
822 printf("id %p", (void *)lock->lf_id);
823 if (lock->lf_inode != (struct inode *)0)
824 printf(" in ino %ju on dev <%s>, %s, start %jd, end %jd",
825 (uintmax_t)lock->lf_inode->i_number,
826 devtoname(lock->lf_inode->i_dev),
827 lock->lf_type == F_RDLCK ? "shared" :
828 lock->lf_type == F_WRLCK ? "exclusive" :
829 lock->lf_type == F_UNLCK ? "unlock" : "unknown",
830 (intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
832 printf(" %s, start %jd, end %jd",
833 lock->lf_type == F_RDLCK ? "shared" :
834 lock->lf_type == F_WRLCK ? "exclusive" :
835 lock->lf_type == F_UNLCK ? "unlock" : "unknown",
836 (intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
837 if (!TAILQ_EMPTY(&lock->lf_blkhd))
838 printf(" block %p\n", (void *)TAILQ_FIRST(&lock->lf_blkhd));
844 lf_printlist(tag, lock)
848 register struct lockf *lf, *blk;
850 if (lock->lf_inode == (struct inode *)0)
853 printf("%s: Lock list for ino %ju on dev <%s>:\n",
854 tag, (uintmax_t)lock->lf_inode->i_number,
855 devtoname(lock->lf_inode->i_dev));
856 for (lf = lock->lf_inode->i_lockf; lf; lf = lf->lf_next) {
857 printf("\tlock %p for ",(void *)lf);
858 if (lf->lf_flags & F_POSIX)
860 (long)((struct proc *)lf->lf_id)->p_pid);
862 printf("id %p", (void *)lf->lf_id);
863 printf(", %s, start %jd, end %jd",
864 lf->lf_type == F_RDLCK ? "shared" :
865 lf->lf_type == F_WRLCK ? "exclusive" :
866 lf->lf_type == F_UNLCK ? "unlock" :
867 "unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
868 TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
869 printf("\n\t\tlock request %p for ", (void *)blk);
870 if (blk->lf_flags & F_POSIX)
872 (long)((struct proc *)blk->lf_id)->p_pid);
874 printf("id %p", (void *)blk->lf_id);
875 printf(", %s, start %jd, end %jd",
876 blk->lf_type == F_RDLCK ? "shared" :
877 blk->lf_type == F_WRLCK ? "exclusive" :
878 blk->lf_type == F_UNLCK ? "unlock" :
879 "unknown", (intmax_t)blk->lf_start,
880 (intmax_t)blk->lf_end);
881 if (!TAILQ_EMPTY(&blk->lf_blkhd))
882 panic("lf_printlist: bad list");
887 #endif /* LOCKF_DEBUG */