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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
37 * $FreeBSD: src/sys/kern/kern_lockf.c,v 1.25 1999/11/16 16:28:56 phk Exp $
38 * $DragonFly: src/sys/kern/kern_lockf.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
41 #include "opt_debug_lockf.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
53 #include <sys/lockf.h>
56 * This variable controls the maximum number of processes that will
57 * be checked in doing deadlock detection.
59 static int maxlockdepth = MAXDEPTH;
62 #include <sys/kernel.h>
63 #include <sys/sysctl.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/inode.h>
69 static int lockf_debug = 0;
70 SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
73 static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
75 #define NOLOCKF (struct lockf *)0
78 static int lf_clearlock __P((struct lockf *));
79 static int lf_findoverlap __P((struct lockf *,
80 struct lockf *, int, struct lockf ***, struct lockf **));
82 lf_getblock __P((struct lockf *));
83 static int lf_getlock __P((struct lockf *, struct flock *));
84 static int lf_setlock __P((struct lockf *));
85 static void lf_split __P((struct lockf *, struct lockf *));
86 static void lf_wakelock __P((struct lockf *));
89 * Advisory record locking support
92 lf_advlock(ap, head, size)
93 struct vop_advlock_args /* {
103 register struct flock *fl = ap->a_fl;
104 register struct lockf *lock;
109 * Convert the flock structure into a start and end.
111 switch (fl->l_whence) {
116 * Caller is responsible for adding any necessary offset
117 * when SEEK_CUR is used.
123 start = size + fl->l_start;
134 end = start + fl->l_len - 1;
139 * Avoid the common case of unlocking when inode has no locks.
141 if (*head == (struct lockf *)0) {
142 if (ap->a_op != F_SETLK) {
143 fl->l_type = F_UNLCK;
148 * Create the lockf structure
150 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
151 lock->lf_start = start;
153 lock->lf_id = ap->a_id;
154 /* lock->lf_inode = ip; */ /* XXX JH */
155 lock->lf_type = fl->l_type;
156 lock->lf_head = head;
157 lock->lf_next = (struct lockf *)0;
158 TAILQ_INIT(&lock->lf_blkhd);
159 lock->lf_flags = ap->a_flags;
161 * Do the requested operation.
165 return (lf_setlock(lock));
168 error = lf_clearlock(lock);
173 error = lf_getlock(lock, fl);
185 * Set a byte-range lock.
189 register struct lockf *lock;
191 register struct lockf *block;
192 struct lockf **head = lock->lf_head;
193 struct lockf **prev, *overlap, *ltmp;
194 static char lockstr[] = "lockf";
195 int ovcase, priority, needtolink, error;
199 lf_print("lf_setlock", lock);
200 #endif /* LOCKF_DEBUG */
206 if (lock->lf_type == F_WRLCK)
210 * Scan lock list for this file looking for locks that would block us.
212 while ((block = lf_getblock(lock))) {
214 * Free the structure and return if nonblocking.
216 if ((lock->lf_flags & F_WAIT) == 0) {
221 * We are blocked. Since flock style locks cover
222 * the whole file, there is no chance for deadlock.
223 * For byte-range locks we must check for deadlock.
225 * Deadlock detection is done by looking through the
226 * wait channels to see if there are any cycles that
227 * involve us. MAXDEPTH is set just to make sure we
228 * do not go off into neverland.
230 if ((lock->lf_flags & F_POSIX) &&
231 (block->lf_flags & F_POSIX)) {
232 register struct proc *wproc;
233 register struct lockf *waitblock;
236 /* The block is waiting on something */
237 wproc = (struct proc *)block->lf_id;
238 while (wproc->p_wchan &&
239 (wproc->p_wmesg == lockstr) &&
240 (i++ < maxlockdepth)) {
241 waitblock = (struct lockf *)wproc->p_wchan;
242 /* Get the owner of the blocking lock */
243 waitblock = waitblock->lf_next;
244 if ((waitblock->lf_flags & F_POSIX) == 0)
246 wproc = (struct proc *)waitblock->lf_id;
247 if (wproc == (struct proc *)lock->lf_id) {
254 * For flock type locks, we must first remove
255 * any shared locks that we hold before we sleep
256 * waiting for an exclusive lock.
258 if ((lock->lf_flags & F_FLOCK) &&
259 lock->lf_type == F_WRLCK) {
260 lock->lf_type = F_UNLCK;
261 (void) lf_clearlock(lock);
262 lock->lf_type = F_WRLCK;
265 * Add our lock to the blocked list and sleep until we're free.
266 * Remember who blocked us (for deadlock detection).
268 lock->lf_next = block;
269 TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
271 if (lockf_debug & 1) {
272 lf_print("lf_setlock: blocking on", block);
273 lf_printlist("lf_setlock", block);
275 #endif /* LOCKF_DEBUG */
276 error = tsleep((caddr_t)lock, priority, lockstr, 0);
278 * We may have been awakened by a signal and/or by a
279 * debugger continuing us (in which cases we must remove
280 * ourselves from the blocked list) and/or by another
281 * process releasing a lock (in which case we have
282 * already been removed from the blocked list and our
283 * lf_next field set to NOLOCKF).
286 TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
287 lock->lf_next = NOLOCKF;
295 * No blocks!! Add the lock. Note that we will
296 * downgrade or upgrade any overlapping locks this
297 * process already owns.
299 * Skip over locks owned by other processes.
300 * Handle any locks that overlap and are owned by ourselves.
306 ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
308 block = overlap->lf_next;
313 * 2) overlap contains lock
314 * 3) lock contains overlap
315 * 4) overlap starts before lock
316 * 5) overlap ends after lock
319 case 0: /* no overlap */
322 lock->lf_next = overlap;
326 case 1: /* overlap == lock */
328 * If downgrading lock, others may be
329 * able to acquire it.
331 if (lock->lf_type == F_RDLCK &&
332 overlap->lf_type == F_WRLCK)
333 lf_wakelock(overlap);
334 overlap->lf_type = lock->lf_type;
336 lock = overlap; /* for debug output below */
339 case 2: /* overlap contains lock */
341 * Check for common starting point and different types.
343 if (overlap->lf_type == lock->lf_type) {
345 lock = overlap; /* for debug output below */
348 if (overlap->lf_start == lock->lf_start) {
350 lock->lf_next = overlap;
351 overlap->lf_start = lock->lf_end + 1;
353 lf_split(overlap, lock);
354 lf_wakelock(overlap);
357 case 3: /* lock contains overlap */
359 * If downgrading lock, others may be able to
360 * acquire it, otherwise take the list.
362 if (lock->lf_type == F_RDLCK &&
363 overlap->lf_type == F_WRLCK) {
364 lf_wakelock(overlap);
366 while (!TAILQ_EMPTY(&overlap->lf_blkhd)) {
367 ltmp = TAILQ_FIRST(&overlap->lf_blkhd);
368 TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
370 TAILQ_INSERT_TAIL(&lock->lf_blkhd,
372 ltmp->lf_next = lock;
376 * Add the new lock if necessary and delete the overlap.
380 lock->lf_next = overlap->lf_next;
381 prev = &lock->lf_next;
384 *prev = overlap->lf_next;
385 free(overlap, M_LOCKF);
388 case 4: /* overlap starts before lock */
390 * Add lock after overlap on the list.
392 lock->lf_next = overlap->lf_next;
393 overlap->lf_next = lock;
394 overlap->lf_end = lock->lf_start - 1;
395 prev = &lock->lf_next;
396 lf_wakelock(overlap);
400 case 5: /* overlap ends after lock */
402 * Add the new lock before overlap.
406 lock->lf_next = overlap;
408 overlap->lf_start = lock->lf_end + 1;
409 lf_wakelock(overlap);
415 if (lockf_debug & 1) {
416 lf_print("lf_setlock: got the lock", lock);
417 lf_printlist("lf_setlock", lock);
419 #endif /* LOCKF_DEBUG */
424 * Remove a byte-range lock on an inode.
426 * Generally, find the lock (or an overlap to that lock)
427 * and remove it (or shrink it), then wakeup anyone we can.
431 register struct lockf *unlock;
433 struct lockf **head = unlock->lf_head;
434 register struct lockf *lf = *head;
435 struct lockf *overlap, **prev;
441 if (unlock->lf_type != F_UNLCK)
442 panic("lf_clearlock: bad type");
444 lf_print("lf_clearlock", unlock);
445 #endif /* LOCKF_DEBUG */
447 while ((ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
449 * Wakeup the list of locks to be retried.
451 lf_wakelock(overlap);
455 case 1: /* overlap == lock */
456 *prev = overlap->lf_next;
457 FREE(overlap, M_LOCKF);
460 case 2: /* overlap contains lock: split it */
461 if (overlap->lf_start == unlock->lf_start) {
462 overlap->lf_start = unlock->lf_end + 1;
465 lf_split(overlap, unlock);
466 overlap->lf_next = unlock->lf_next;
469 case 3: /* lock contains overlap */
470 *prev = overlap->lf_next;
471 lf = overlap->lf_next;
472 free(overlap, M_LOCKF);
475 case 4: /* overlap starts before lock */
476 overlap->lf_end = unlock->lf_start - 1;
477 prev = &overlap->lf_next;
478 lf = overlap->lf_next;
481 case 5: /* overlap ends after lock */
482 overlap->lf_start = unlock->lf_end + 1;
489 lf_printlist("lf_clearlock", unlock);
490 #endif /* LOCKF_DEBUG */
495 * Check whether there is a blocking lock,
496 * and if so return its process identifier.
500 register struct lockf *lock;
501 register struct flock *fl;
503 register struct lockf *block;
507 lf_print("lf_getlock", lock);
508 #endif /* LOCKF_DEBUG */
510 if ((block = lf_getblock(lock))) {
511 fl->l_type = block->lf_type;
512 fl->l_whence = SEEK_SET;
513 fl->l_start = block->lf_start;
514 if (block->lf_end == -1)
517 fl->l_len = block->lf_end - block->lf_start + 1;
518 if (block->lf_flags & F_POSIX)
519 fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
523 fl->l_type = F_UNLCK;
529 * Walk the list of locks for an inode and
530 * return the first blocking lock.
532 static struct lockf *
534 register struct lockf *lock;
536 struct lockf **prev, *overlap, *lf = *(lock->lf_head);
539 prev = lock->lf_head;
540 while ((ovcase = lf_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
542 * We've found an overlap, see if it blocks us
544 if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
547 * Nope, point to the next one on the list and
548 * see if it blocks us
550 lf = overlap->lf_next;
556 * Walk the list of locks for an inode to
557 * find an overlapping lock (if any).
559 * NOTE: this returns only the FIRST overlapping lock. There
560 * may be more than one.
563 lf_findoverlap(lf, lock, type, prev, overlap)
564 register struct lockf *lf;
567 struct lockf ***prev;
568 struct lockf **overlap;
577 lf_print("lf_findoverlap: looking for overlap in", lock);
578 #endif /* LOCKF_DEBUG */
579 start = lock->lf_start;
581 while (lf != NOLOCKF) {
582 if (((type & SELF) && lf->lf_id != lock->lf_id) ||
583 ((type & OTHERS) && lf->lf_id == lock->lf_id)) {
584 *prev = &lf->lf_next;
585 *overlap = lf = lf->lf_next;
590 lf_print("\tchecking", lf);
591 #endif /* LOCKF_DEBUG */
593 * OK, check for overlap
598 * 2) overlap contains lock
599 * 3) lock contains overlap
600 * 4) overlap starts before lock
601 * 5) overlap ends after lock
603 if ((lf->lf_end != -1 && start > lf->lf_end) ||
604 (end != -1 && lf->lf_start > end)) {
608 printf("no overlap\n");
609 #endif /* LOCKF_DEBUG */
610 if ((type & SELF) && end != -1 && lf->lf_start > end)
612 *prev = &lf->lf_next;
613 *overlap = lf = lf->lf_next;
616 if ((lf->lf_start == start) && (lf->lf_end == end)) {
620 printf("overlap == lock\n");
621 #endif /* LOCKF_DEBUG */
624 if ((lf->lf_start <= start) &&
626 ((lf->lf_end >= end) || (lf->lf_end == -1))) {
630 printf("overlap contains lock\n");
631 #endif /* LOCKF_DEBUG */
634 if (start <= lf->lf_start &&
636 (lf->lf_end != -1 && end >= lf->lf_end))) {
640 printf("lock contains overlap\n");
641 #endif /* LOCKF_DEBUG */
644 if ((lf->lf_start < start) &&
645 ((lf->lf_end >= start) || (lf->lf_end == -1))) {
649 printf("overlap starts before lock\n");
650 #endif /* LOCKF_DEBUG */
653 if ((lf->lf_start > start) &&
655 ((lf->lf_end > end) || (lf->lf_end == -1))) {
659 printf("overlap ends after lock\n");
660 #endif /* LOCKF_DEBUG */
663 panic("lf_findoverlap: default");
669 * Split a lock and a contained region into
670 * two or three locks as necessary.
673 lf_split(lock1, lock2)
674 register struct lockf *lock1;
675 register struct lockf *lock2;
677 register struct lockf *splitlock;
680 if (lockf_debug & 2) {
681 lf_print("lf_split", lock1);
682 lf_print("splitting from", lock2);
684 #endif /* LOCKF_DEBUG */
686 * Check to see if spliting into only two pieces.
688 if (lock1->lf_start == lock2->lf_start) {
689 lock1->lf_start = lock2->lf_end + 1;
690 lock2->lf_next = lock1;
693 if (lock1->lf_end == lock2->lf_end) {
694 lock1->lf_end = lock2->lf_start - 1;
695 lock2->lf_next = lock1->lf_next;
696 lock1->lf_next = lock2;
700 * Make a new lock consisting of the last part of
701 * the encompassing lock
703 MALLOC(splitlock, struct lockf *, sizeof *splitlock, M_LOCKF, M_WAITOK);
704 bcopy((caddr_t)lock1, (caddr_t)splitlock, sizeof *splitlock);
705 splitlock->lf_start = lock2->lf_end + 1;
706 TAILQ_INIT(&splitlock->lf_blkhd);
707 lock1->lf_end = lock2->lf_start - 1;
711 splitlock->lf_next = lock1->lf_next;
712 lock2->lf_next = splitlock;
713 lock1->lf_next = lock2;
720 lf_wakelock(listhead)
721 struct lockf *listhead;
723 register struct lockf *wakelock;
725 while (!TAILQ_EMPTY(&listhead->lf_blkhd)) {
726 wakelock = TAILQ_FIRST(&listhead->lf_blkhd);
727 TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
728 wakelock->lf_next = NOLOCKF;
731 lf_print("lf_wakelock: awakening", wakelock);
732 #endif /* LOCKF_DEBUG */
733 wakeup((caddr_t)wakelock);
744 register struct lockf *lock;
747 printf("%s: lock %p for ", tag, (void *)lock);
748 if (lock->lf_flags & F_POSIX)
749 printf("proc %ld", (long)((struct proc *)lock->lf_id)->p_pid);
751 printf("id %p", (void *)lock->lf_id);
752 /* XXX no %qd in kernel. Truncate. */
753 printf(" in ino %lu on dev <%d, %d>, %s, start %ld, end %ld",
754 (u_long)lock->lf_inode->i_number,
755 major(lock->lf_inode->i_dev),
756 minor(lock->lf_inode->i_dev),
757 lock->lf_type == F_RDLCK ? "shared" :
758 lock->lf_type == F_WRLCK ? "exclusive" :
759 lock->lf_type == F_UNLCK ? "unlock" :
760 "unknown", (long)lock->lf_start, (long)lock->lf_end);
761 if (!TAILQ_EMPTY(&lock->lf_blkhd))
762 printf(" block %p\n", (void *)TAILQ_FIRST(&lock->lf_blkhd));
768 lf_printlist(tag, lock)
772 register struct lockf *lf, *blk;
774 printf("%s: Lock list for ino %lu on dev <%d, %d>:\n",
775 tag, (u_long)lock->lf_inode->i_number,
776 major(lock->lf_inode->i_dev),
777 minor(lock->lf_inode->i_dev));
778 for (lf = lock->lf_inode->i_lockf; lf; lf = lf->lf_next) {
779 printf("\tlock %p for ",(void *)lf);
780 if (lf->lf_flags & F_POSIX)
782 (long)((struct proc *)lf->lf_id)->p_pid);
784 printf("id %p", (void *)lf->lf_id);
785 /* XXX no %qd in kernel. Truncate. */
786 printf(", %s, start %ld, end %ld",
787 lf->lf_type == F_RDLCK ? "shared" :
788 lf->lf_type == F_WRLCK ? "exclusive" :
789 lf->lf_type == F_UNLCK ? "unlock" :
790 "unknown", (long)lf->lf_start, (long)lf->lf_end);
791 TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
792 printf("\n\t\tlock request %p for ", (void *)blk);
793 if (blk->lf_flags & F_POSIX)
795 (long)((struct proc *)blk->lf_id)->p_pid);
797 printf("id %p", (void *)blk->lf_id);
798 /* XXX no %qd in kernel. Truncate. */
799 printf(", %s, start %ld, end %ld",
800 blk->lf_type == F_RDLCK ? "shared" :
801 blk->lf_type == F_WRLCK ? "exclusive" :
802 blk->lf_type == F_UNLCK ? "unlock" :
803 "unknown", (long)blk->lf_start,
805 if (!TAILQ_EMPTY(&blk->lf_blkhd))
806 panic("lf_printlist: bad list");
811 #endif /* LOCKF_DEBUG */