Home | History | Annotate | Line # | Download | only in kern
vfs_lockf.c revision 1.41
      1 /*	$NetBSD: vfs_lockf.c,v 1.41 2005/05/09 11:10:07 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Scooter Morris at Genentech Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)ufs_lockf.c	8.4 (Berkeley) 10/26/94
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: vfs_lockf.c,v 1.41 2005/05/09 11:10:07 christos Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/file.h>
     44 #include <sys/proc.h>
     45 #include <sys/vnode.h>
     46 #include <sys/pool.h>
     47 #include <sys/fcntl.h>
     48 #include <sys/lockf.h>
     49 
     50 POOL_INIT(lockfpool, sizeof(struct lockf), 0, 0, 0, "lockfpl",
     51     &pool_allocator_nointr);
     52 
     53 /*
     54  * This variable controls the maximum number of processes that will
     55  * be checked in doing deadlock detection.
     56  */
     57 int maxlockdepth = MAXDEPTH;
     58 
     59 #ifdef LOCKF_DEBUG
     60 int	lockf_debug = 0;
     61 #endif
     62 
     63 #define NOLOCKF (struct lockf *)0
     64 #define SELF	0x1
     65 #define OTHERS	0x2
     66 
     67 static int lf_clearlock(struct lockf *, struct lockf **);
     68 static int lf_findoverlap(struct lockf *,
     69 	    struct lockf *, int, struct lockf ***, struct lockf **);
     70 static struct lockf *lf_getblock(struct lockf *);
     71 static int lf_getlock(struct lockf *, struct flock *);
     72 static int lf_setlock(struct lockf *, struct lockf **, struct simplelock *);
     73 static void lf_split(struct lockf *, struct lockf *, struct lockf **);
     74 static void lf_wakelock(struct lockf *);
     75 static struct lockf *lf_alloc(uid_t, int);
     76 static void lf_free(struct lockf *);
     77 
     78 
     79 #ifdef LOCKF_DEBUG
     80 static void lf_print(char *, struct lockf *);
     81 static void lf_printlist(char *, struct lockf *);
     82 #endif
     83 
     84 /*
     85  * XXX TODO
     86  * Misc cleanups: "caddr_t id" should be visible in the API as a
     87  * "struct proc *".
     88  * (This requires rototilling all VFS's which support advisory locking).
     89  */
     90 
     91 /*
     92  * If there's a lot of lock contention on a single vnode, locking
     93  * schemes which allow for more paralleism would be needed.  Given how
     94  * infrequently byte-range locks are actually used in typical BSD
     95  * code, a more complex approach probably isn't worth it.
     96  */
     97 
     98 /*
     99  * We enforce a limit on locks by uid, so that a single user cannot
    100  * run the kernel out of memory.  For now, the limit is pretty coarse.
    101  * There is no limit on root.
    102  *
    103  * Splitting a lock will always succeed, regardless of current allocations.
    104  * If you're slightly above the limit, we still have to permit an allocation
    105  * so that the unlock can succeed.  If the unlocking causes too many splits,
    106  * however, you're totally cutoff.
    107  */
    108 int maxlocksperuid = 1024;
    109 
    110 /*
    111  * 3 options for allowfail.
    112  * 0 - always allocate.  1 - cutoff at limit.  2 - cutoff at double limit.
    113  */
    114 struct lockf *
    115 lf_alloc(uid_t uid, int allowfail)
    116 {
    117 	struct uidinfo *uip;
    118 	struct lockf *lock;
    119 	int s;
    120 
    121 	uip = uid_find(uid);
    122 	UILOCK(uip, s);
    123 	if (uid && allowfail && uip->ui_lockcnt >
    124 	    (allowfail == 1 ? maxlocksperuid : (maxlocksperuid * 2))) {
    125 		UIUNLOCK(uip, s);
    126 		return NULL;
    127 	}
    128 	uip->ui_lockcnt++;
    129 	UIUNLOCK(uip, s);
    130 	lock = pool_get(&lockfpool, PR_WAITOK);
    131 	lock->lf_uid = uid;
    132 	return lock;
    133 }
    134 
    135 void
    136 lf_free(struct lockf *lock)
    137 {
    138 	struct uidinfo *uip;
    139 	int s;
    140 
    141 	uip = uid_find(lock->lf_uid);
    142 	UILOCK(uip, s);
    143 	uip->ui_lockcnt--;
    144 	simple_unlock(&uip->ui_slock);
    145 	UIUNLOCK(uip, s);
    146 	pool_put(&lockfpool, lock);
    147 }
    148 
    149 /*
    150  * Do an advisory lock operation.
    151  */
    152 int
    153 lf_advlock(struct vop_advlock_args *ap, struct lockf **head, off_t size)
    154 {
    155 	struct proc *p = curproc;
    156 	struct flock *fl = ap->a_fl;
    157 	struct lockf *lock = NULL;
    158 	struct lockf *sparelock;
    159 	struct simplelock *interlock = &ap->a_vp->v_interlock;
    160 	off_t start, end;
    161 	int error = 0;
    162 
    163 	/*
    164 	 * Convert the flock structure into a start and end.
    165 	 */
    166 	switch (fl->l_whence) {
    167 	case SEEK_SET:
    168 	case SEEK_CUR:
    169 		/*
    170 		 * Caller is responsible for adding any necessary offset
    171 		 * when SEEK_CUR is used.
    172 		 */
    173 		start = fl->l_start;
    174 		break;
    175 
    176 	case SEEK_END:
    177 		start = size + fl->l_start;
    178 		break;
    179 
    180 	default:
    181 		return EINVAL;
    182 	}
    183 	if (start < 0)
    184 		return EINVAL;
    185 
    186 	/*
    187 	 * allocate locks before acquire simple lock.
    188 	 * we need two locks in the worst case.
    189 	 */
    190 	switch (ap->a_op) {
    191 	case F_SETLK:
    192 	case F_UNLCK:
    193 		/*
    194 		 * XXX for F_UNLCK case, we can re-use lock.
    195 		 */
    196 		if ((fl->l_type & F_FLOCK) == 0) {
    197 			/*
    198 			 * byte-range lock might need one more lock.
    199 			 */
    200 			sparelock = lf_alloc(p->p_ucred->cr_uid, 0);
    201 			if (sparelock == NULL) {
    202 				error = ENOMEM;
    203 				goto quit;
    204 			}
    205 			break;
    206 		}
    207 		/* FALLTHROUGH */
    208 
    209 	case F_GETLK:
    210 		sparelock = NULL;
    211 		break;
    212 
    213 	default:
    214 		return EINVAL;
    215 	}
    216 
    217 	lock = lf_alloc(p->p_ucred->cr_uid, ap->a_op != F_UNLCK ? 1 : 2);
    218 	if (lock == NULL) {
    219 		error = ENOMEM;
    220 		goto quit;
    221 	}
    222 
    223 	simple_lock(interlock);
    224 
    225 	/*
    226 	 * Avoid the common case of unlocking when inode has no locks.
    227 	 */
    228 	if (*head == (struct lockf *)0) {
    229 		if (ap->a_op != F_SETLK) {
    230 			fl->l_type = F_UNLCK;
    231 			error = 0;
    232 			goto quit_unlock;
    233 		}
    234 	}
    235 
    236 	if (fl->l_len == 0)
    237 		end = -1;
    238 	else
    239 		end = start + fl->l_len - 1;
    240 	/*
    241 	 * Create the lockf structure.
    242 	 */
    243 	lock->lf_start = start;
    244 	lock->lf_end = end;
    245 	/* XXX NJWLWP
    246 	 * I don't want to make the entire VFS universe use LWPs, because
    247 	 * they don't need them, for the most part. This is an exception,
    248 	 * and a kluge.
    249 	 */
    250 
    251 	lock->lf_head = head;
    252 	lock->lf_type = fl->l_type;
    253 	lock->lf_next = (struct lockf *)0;
    254 	TAILQ_INIT(&lock->lf_blkhd);
    255 	lock->lf_flags = ap->a_flags;
    256 	if (lock->lf_flags & F_POSIX) {
    257 		KASSERT(curproc == (struct proc *)ap->a_id);
    258 	}
    259 	lock->lf_id = (struct proc *)ap->a_id;
    260 	lock->lf_lwp = curlwp;
    261 
    262 	/*
    263 	 * Do the requested operation.
    264 	 */
    265 	switch (ap->a_op) {
    266 
    267 	case F_SETLK:
    268 		error = lf_setlock(lock, &sparelock, interlock);
    269 		lock = NULL; /* lf_setlock freed it */
    270 		break;
    271 
    272 	case F_UNLCK:
    273 		error = lf_clearlock(lock, &sparelock);
    274 		break;
    275 
    276 	case F_GETLK:
    277 		error = lf_getlock(lock, fl);
    278 		break;
    279 
    280 	default:
    281 		break;
    282 		/* NOTREACHED */
    283 	}
    284 
    285 quit_unlock:
    286 	simple_unlock(interlock);
    287 quit:
    288 	if (lock)
    289 		lf_free(lock);
    290 	if (sparelock)
    291 		lf_free(sparelock);
    292 
    293 	return error;
    294 }
    295 
    296 /*
    297  * Set a byte-range lock.
    298  */
    299 static int
    300 lf_setlock(struct lockf *lock, struct lockf **sparelock,
    301     struct simplelock *interlock)
    302 {
    303 	struct lockf *block;
    304 	struct lockf **head = lock->lf_head;
    305 	struct lockf **prev, *overlap, *ltmp;
    306 	static char lockstr[] = "lockf";
    307 	int ovcase, priority, needtolink, error;
    308 
    309 #ifdef LOCKF_DEBUG
    310 	if (lockf_debug & 1)
    311 		lf_print("lf_setlock", lock);
    312 #endif /* LOCKF_DEBUG */
    313 
    314 	/*
    315 	 * Set the priority
    316 	 */
    317 	priority = PLOCK;
    318 	if (lock->lf_type == F_WRLCK)
    319 		priority += 4;
    320 	priority |= PCATCH;
    321 	/*
    322 	 * Scan lock list for this file looking for locks that would block us.
    323 	 */
    324 	while ((block = lf_getblock(lock)) != NULL) {
    325 		/*
    326 		 * Free the structure and return if nonblocking.
    327 		 */
    328 		if ((lock->lf_flags & F_WAIT) == 0) {
    329 			lf_free(lock);
    330 			return EAGAIN;
    331 		}
    332 		/*
    333 		 * We are blocked. Since flock style locks cover
    334 		 * the whole file, there is no chance for deadlock.
    335 		 * For byte-range locks we must check for deadlock.
    336 		 *
    337 		 * Deadlock detection is done by looking through the
    338 		 * wait channels to see if there are any cycles that
    339 		 * involve us. MAXDEPTH is set just to make sure we
    340 		 * do not go off into neverneverland.
    341 		 */
    342 		if ((lock->lf_flags & F_POSIX) &&
    343 		    (block->lf_flags & F_POSIX)) {
    344 			struct lwp *wlwp;
    345 			struct lockf *waitblock;
    346 			int i = 0;
    347 
    348 			/*
    349 			 * The block is waiting on something.  if_lwp will be
    350 			 * 0 once the lock is granted, so we terminate the
    351 			 * loop if we find this.
    352 			 */
    353 			wlwp = block->lf_lwp;
    354 			while (wlwp && (i++ < maxlockdepth)) {
    355 				waitblock = (struct lockf *)wlwp->l_wchan;
    356 				/* Get the owner of the blocking lock */
    357 				waitblock = waitblock->lf_next;
    358 				if ((waitblock->lf_flags & F_POSIX) == 0)
    359 					break;
    360 				wlwp = waitblock->lf_lwp;
    361 				if (wlwp == lock->lf_lwp) {
    362 					lf_free(lock);
    363 					return EDEADLK;
    364 				}
    365 			}
    366 			/*
    367 			 * If we're still following a dependency chain
    368 			 * after maxlockdepth iterations, assume we're in
    369 			 * a cycle to be safe.
    370 			 */
    371 			if (i >= maxlockdepth) {
    372 				lf_free(lock);
    373 				return EDEADLK;
    374 			}
    375 		}
    376 		/*
    377 		 * For flock type locks, we must first remove
    378 		 * any shared locks that we hold before we sleep
    379 		 * waiting for an exclusive lock.
    380 		 */
    381 		if ((lock->lf_flags & F_FLOCK) &&
    382 		    lock->lf_type == F_WRLCK) {
    383 			lock->lf_type = F_UNLCK;
    384 			(void) lf_clearlock(lock, NULL);
    385 			lock->lf_type = F_WRLCK;
    386 		}
    387 		/*
    388 		 * Add our lock to the blocked list and sleep until we're free.
    389 		 * Remember who blocked us (for deadlock detection).
    390 		 */
    391 		lock->lf_next = block;
    392 		TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
    393 #ifdef LOCKF_DEBUG
    394 		if (lockf_debug & 1) {
    395 			lf_print("lf_setlock: blocking on", block);
    396 			lf_printlist("lf_setlock", block);
    397 		}
    398 #endif /* LOCKF_DEBUG */
    399 		error = ltsleep(lock, priority, lockstr, 0, interlock);
    400 
    401 		/*
    402 		 * We may have been awakened by a signal (in
    403 		 * which case we must remove ourselves from the
    404 		 * blocked list) and/or by another process
    405 		 * releasing a lock (in which case we have already
    406 		 * been removed from the blocked list and our
    407 		 * lf_next field set to NOLOCKF).
    408 		 */
    409 		if (lock->lf_next != NOLOCKF) {
    410 			TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
    411 			lock->lf_next = NOLOCKF;
    412 		}
    413 		if (error) {
    414 			lf_free(lock);
    415 			return error;
    416 		}
    417 	}
    418 	/*
    419 	 * No blocks!!  Add the lock.  Note that we will
    420 	 * downgrade or upgrade any overlapping locks this
    421 	 * process already owns.
    422 	 *
    423 	 * Skip over locks owned by other processes.
    424 	 * Handle any locks that overlap and are owned by ourselves.
    425 	 */
    426 	lock->lf_lwp = 0;
    427 	prev = head;
    428 	block = *head;
    429 	needtolink = 1;
    430 	for (;;) {
    431 		ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
    432 		if (ovcase)
    433 			block = overlap->lf_next;
    434 		/*
    435 		 * Six cases:
    436 		 *	0) no overlap
    437 		 *	1) overlap == lock
    438 		 *	2) overlap contains lock
    439 		 *	3) lock contains overlap
    440 		 *	4) overlap starts before lock
    441 		 *	5) overlap ends after lock
    442 		 */
    443 		switch (ovcase) {
    444 		case 0: /* no overlap */
    445 			if (needtolink) {
    446 				*prev = lock;
    447 				lock->lf_next = overlap;
    448 			}
    449 			break;
    450 
    451 		case 1: /* overlap == lock */
    452 			/*
    453 			 * If downgrading lock, others may be
    454 			 * able to acquire it.
    455 			 */
    456 			if (lock->lf_type == F_RDLCK &&
    457 			    overlap->lf_type == F_WRLCK)
    458 				lf_wakelock(overlap);
    459 			overlap->lf_type = lock->lf_type;
    460 			lf_free(lock);
    461 			lock = overlap; /* for debug output below */
    462 			break;
    463 
    464 		case 2: /* overlap contains lock */
    465 			/*
    466 			 * Check for common starting point and different types.
    467 			 */
    468 			if (overlap->lf_type == lock->lf_type) {
    469 				lf_free(lock);
    470 				lock = overlap; /* for debug output below */
    471 				break;
    472 			}
    473 			if (overlap->lf_start == lock->lf_start) {
    474 				*prev = lock;
    475 				lock->lf_next = overlap;
    476 				overlap->lf_start = lock->lf_end + 1;
    477 			} else
    478 				lf_split(overlap, lock, sparelock);
    479 			lf_wakelock(overlap);
    480 			break;
    481 
    482 		case 3: /* lock contains overlap */
    483 			/*
    484 			 * If downgrading lock, others may be able to
    485 			 * acquire it, otherwise take the list.
    486 			 */
    487 			if (lock->lf_type == F_RDLCK &&
    488 			    overlap->lf_type == F_WRLCK) {
    489 				lf_wakelock(overlap);
    490 			} else {
    491 				while ((ltmp = TAILQ_FIRST(&overlap->lf_blkhd))) {
    492 					KASSERT(ltmp->lf_next == overlap);
    493 					TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
    494 					    lf_block);
    495 					ltmp->lf_next = lock;
    496 					TAILQ_INSERT_TAIL(&lock->lf_blkhd,
    497 					    ltmp, lf_block);
    498 				}
    499 			}
    500 			/*
    501 			 * Add the new lock if necessary and delete the overlap.
    502 			 */
    503 			if (needtolink) {
    504 				*prev = lock;
    505 				lock->lf_next = overlap->lf_next;
    506 				prev = &lock->lf_next;
    507 				needtolink = 0;
    508 			} else
    509 				*prev = overlap->lf_next;
    510 			lf_free(overlap);
    511 			continue;
    512 
    513 		case 4: /* overlap starts before lock */
    514 			/*
    515 			 * Add lock after overlap on the list.
    516 			 */
    517 			lock->lf_next = overlap->lf_next;
    518 			overlap->lf_next = lock;
    519 			overlap->lf_end = lock->lf_start - 1;
    520 			prev = &lock->lf_next;
    521 			lf_wakelock(overlap);
    522 			needtolink = 0;
    523 			continue;
    524 
    525 		case 5: /* overlap ends after lock */
    526 			/*
    527 			 * Add the new lock before overlap.
    528 			 */
    529 			if (needtolink) {
    530 				*prev = lock;
    531 				lock->lf_next = overlap;
    532 			}
    533 			overlap->lf_start = lock->lf_end + 1;
    534 			lf_wakelock(overlap);
    535 			break;
    536 		}
    537 		break;
    538 	}
    539 #ifdef LOCKF_DEBUG
    540 	if (lockf_debug & 1) {
    541 		lf_print("lf_setlock: got the lock", lock);
    542 		lf_printlist("lf_setlock", lock);
    543 	}
    544 #endif /* LOCKF_DEBUG */
    545 	return 0;
    546 }
    547 
    548 /*
    549  * Remove a byte-range lock on an inode.
    550  *
    551  * Generally, find the lock (or an overlap to that lock)
    552  * and remove it (or shrink it), then wakeup anyone we can.
    553  */
    554 static int
    555 lf_clearlock(struct lockf *unlock, struct lockf **sparelock)
    556 {
    557 	struct lockf **head = unlock->lf_head;
    558 	struct lockf *lf = *head;
    559 	struct lockf *overlap, **prev;
    560 	int ovcase;
    561 
    562 	if (lf == NOLOCKF)
    563 		return 0;
    564 #ifdef LOCKF_DEBUG
    565 	if (unlock->lf_type != F_UNLCK)
    566 		panic("lf_clearlock: bad type");
    567 	if (lockf_debug & 1)
    568 		lf_print("lf_clearlock", unlock);
    569 #endif /* LOCKF_DEBUG */
    570 	prev = head;
    571 	while ((ovcase = lf_findoverlap(lf, unlock, SELF,
    572 					&prev, &overlap)) != 0) {
    573 		/*
    574 		 * Wakeup the list of locks to be retried.
    575 		 */
    576 		lf_wakelock(overlap);
    577 
    578 		switch (ovcase) {
    579 
    580 		case 1: /* overlap == lock */
    581 			*prev = overlap->lf_next;
    582 			lf_free(overlap);
    583 			break;
    584 
    585 		case 2: /* overlap contains lock: split it */
    586 			if (overlap->lf_start == unlock->lf_start) {
    587 				overlap->lf_start = unlock->lf_end + 1;
    588 				break;
    589 			}
    590 			lf_split(overlap, unlock, sparelock);
    591 			overlap->lf_next = unlock->lf_next;
    592 			break;
    593 
    594 		case 3: /* lock contains overlap */
    595 			*prev = overlap->lf_next;
    596 			lf = overlap->lf_next;
    597 			lf_free(overlap);
    598 			continue;
    599 
    600 		case 4: /* overlap starts before lock */
    601 			overlap->lf_end = unlock->lf_start - 1;
    602 			prev = &overlap->lf_next;
    603 			lf = overlap->lf_next;
    604 			continue;
    605 
    606 		case 5: /* overlap ends after lock */
    607 			overlap->lf_start = unlock->lf_end + 1;
    608 			break;
    609 		}
    610 		break;
    611 	}
    612 #ifdef LOCKF_DEBUG
    613 	if (lockf_debug & 1)
    614 		lf_printlist("lf_clearlock", unlock);
    615 #endif /* LOCKF_DEBUG */
    616 	return 0;
    617 }
    618 
    619 /*
    620  * Check whether there is a blocking lock,
    621  * and if so return its process identifier.
    622  */
    623 static int
    624 lf_getlock(struct lockf *lock, struct flock *fl)
    625 {
    626 	struct lockf *block;
    627 
    628 #ifdef LOCKF_DEBUG
    629 	if (lockf_debug & 1)
    630 		lf_print("lf_getlock", lock);
    631 #endif /* LOCKF_DEBUG */
    632 
    633 	if ((block = lf_getblock(lock)) != NULL) {
    634 		fl->l_type = block->lf_type;
    635 		fl->l_whence = SEEK_SET;
    636 		fl->l_start = block->lf_start;
    637 		if (block->lf_end == -1)
    638 			fl->l_len = 0;
    639 		else
    640 			fl->l_len = block->lf_end - block->lf_start + 1;
    641 		if (block->lf_flags & F_POSIX)
    642 			fl->l_pid = ((struct proc *)block->lf_id)->p_pid;
    643 		else
    644 			fl->l_pid = -1;
    645 	} else {
    646 		fl->l_type = F_UNLCK;
    647 	}
    648 	return 0;
    649 }
    650 
    651 /*
    652  * Walk the list of locks for an inode and
    653  * return the first blocking lock.
    654  */
    655 static struct lockf *
    656 lf_getblock(struct lockf *lock)
    657 {
    658 	struct lockf **prev, *overlap, *lf = *(lock->lf_head);
    659 
    660 	prev = lock->lf_head;
    661 	while (lf_findoverlap(lf, lock, OTHERS, &prev, &overlap) != 0) {
    662 		/*
    663 		 * We've found an overlap, see if it blocks us
    664 		 */
    665 		if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
    666 			return overlap;
    667 		/*
    668 		 * Nope, point to the next one on the list and
    669 		 * see if it blocks us
    670 		 */
    671 		lf = overlap->lf_next;
    672 	}
    673 	return NOLOCKF;
    674 }
    675 
    676 /*
    677  * Walk the list of locks for an inode to
    678  * find an overlapping lock (if any).
    679  *
    680  * NOTE: this returns only the FIRST overlapping lock.  There
    681  *	 may be more than one.
    682  */
    683 static int
    684 lf_findoverlap(struct lockf *lf, struct lockf *lock, int type,
    685     struct lockf ***prev, struct lockf **overlap)
    686 {
    687 	off_t start, end;
    688 
    689 	*overlap = lf;
    690 	if (lf == NOLOCKF)
    691 		return 0;
    692 #ifdef LOCKF_DEBUG
    693 	if (lockf_debug & 2)
    694 		lf_print("lf_findoverlap: looking for overlap in", lock);
    695 #endif /* LOCKF_DEBUG */
    696 	start = lock->lf_start;
    697 	end = lock->lf_end;
    698 	while (lf != NOLOCKF) {
    699 		if (((type == SELF) && lf->lf_id != lock->lf_id) ||
    700 		    ((type == OTHERS) && lf->lf_id == lock->lf_id)) {
    701 			*prev = &lf->lf_next;
    702 			*overlap = lf = lf->lf_next;
    703 			continue;
    704 		}
    705 #ifdef LOCKF_DEBUG
    706 		if (lockf_debug & 2)
    707 			lf_print("\tchecking", lf);
    708 #endif /* LOCKF_DEBUG */
    709 		/*
    710 		 * OK, check for overlap
    711 		 *
    712 		 * Six cases:
    713 		 *	0) no overlap
    714 		 *	1) overlap == lock
    715 		 *	2) overlap contains lock
    716 		 *	3) lock contains overlap
    717 		 *	4) overlap starts before lock
    718 		 *	5) overlap ends after lock
    719 		 */
    720 		if ((lf->lf_end != -1 && start > lf->lf_end) ||
    721 		    (end != -1 && lf->lf_start > end)) {
    722 			/* Case 0 */
    723 #ifdef LOCKF_DEBUG
    724 			if (lockf_debug & 2)
    725 				printf("no overlap\n");
    726 #endif /* LOCKF_DEBUG */
    727 			if ((type & SELF) && end != -1 && lf->lf_start > end)
    728 				return 0;
    729 			*prev = &lf->lf_next;
    730 			*overlap = lf = lf->lf_next;
    731 			continue;
    732 		}
    733 		if ((lf->lf_start == start) && (lf->lf_end == end)) {
    734 			/* Case 1 */
    735 #ifdef LOCKF_DEBUG
    736 			if (lockf_debug & 2)
    737 				printf("overlap == lock\n");
    738 #endif /* LOCKF_DEBUG */
    739 			return 1;
    740 		}
    741 		if ((lf->lf_start <= start) &&
    742 		    (end != -1) &&
    743 		    ((lf->lf_end >= end) || (lf->lf_end == -1))) {
    744 			/* Case 2 */
    745 #ifdef LOCKF_DEBUG
    746 			if (lockf_debug & 2)
    747 				printf("overlap contains lock\n");
    748 #endif /* LOCKF_DEBUG */
    749 			return 2;
    750 		}
    751 		if (start <= lf->lf_start &&
    752 		           (end == -1 ||
    753 			   (lf->lf_end != -1 && end >= lf->lf_end))) {
    754 			/* Case 3 */
    755 #ifdef LOCKF_DEBUG
    756 			if (lockf_debug & 2)
    757 				printf("lock contains overlap\n");
    758 #endif /* LOCKF_DEBUG */
    759 			return 3;
    760 		}
    761 		if ((lf->lf_start < start) &&
    762 			((lf->lf_end >= start) || (lf->lf_end == -1))) {
    763 			/* Case 4 */
    764 #ifdef LOCKF_DEBUG
    765 			if (lockf_debug & 2)
    766 				printf("overlap starts before lock\n");
    767 #endif /* LOCKF_DEBUG */
    768 			return 4;
    769 		}
    770 		if ((lf->lf_start > start) &&
    771 			(end != -1) &&
    772 			((lf->lf_end > end) || (lf->lf_end == -1))) {
    773 			/* Case 5 */
    774 #ifdef LOCKF_DEBUG
    775 			if (lockf_debug & 2)
    776 				printf("overlap ends after lock\n");
    777 #endif /* LOCKF_DEBUG */
    778 			return 5;
    779 		}
    780 		panic("lf_findoverlap: default");
    781 	}
    782 	return 0;
    783 }
    784 
    785 /*
    786  * Split a lock and a contained region into
    787  * two or three locks as necessary.
    788  */
    789 static void
    790 lf_split(struct lockf *lock1, struct lockf *lock2, struct lockf **sparelock)
    791 {
    792 	struct lockf *splitlock;
    793 
    794 #ifdef LOCKF_DEBUG
    795 	if (lockf_debug & 2) {
    796 		lf_print("lf_split", lock1);
    797 		lf_print("splitting from", lock2);
    798 	}
    799 #endif /* LOCKF_DEBUG */
    800 	/*
    801 	 * Check to see if spliting into only two pieces.
    802 	 */
    803 	if (lock1->lf_start == lock2->lf_start) {
    804 		lock1->lf_start = lock2->lf_end + 1;
    805 		lock2->lf_next = lock1;
    806 		return;
    807 	}
    808 	if (lock1->lf_end == lock2->lf_end) {
    809 		lock1->lf_end = lock2->lf_start - 1;
    810 		lock2->lf_next = lock1->lf_next;
    811 		lock1->lf_next = lock2;
    812 		return;
    813 	}
    814 	/*
    815 	 * Make a new lock consisting of the last part of
    816 	 * the encompassing lock
    817 	 */
    818 	splitlock = *sparelock;
    819 	*sparelock = NULL;
    820 	memcpy(splitlock, lock1, sizeof(*splitlock));
    821 	splitlock->lf_start = lock2->lf_end + 1;
    822 	TAILQ_INIT(&splitlock->lf_blkhd);
    823 	lock1->lf_end = lock2->lf_start - 1;
    824 	/*
    825 	 * OK, now link it in
    826 	 */
    827 	splitlock->lf_next = lock1->lf_next;
    828 	lock2->lf_next = splitlock;
    829 	lock1->lf_next = lock2;
    830 }
    831 
    832 /*
    833  * Wakeup a blocklist
    834  */
    835 static void
    836 lf_wakelock(struct lockf *listhead)
    837 {
    838 	struct lockf *wakelock;
    839 
    840 	while ((wakelock = TAILQ_FIRST(&listhead->lf_blkhd))) {
    841 		KASSERT(wakelock->lf_next == listhead);
    842 		TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
    843 		wakelock->lf_next = NOLOCKF;
    844 #ifdef LOCKF_DEBUG
    845 		if (lockf_debug & 2)
    846 			lf_print("lf_wakelock: awakening", wakelock);
    847 #endif
    848 		wakeup(wakelock);
    849 	}
    850 }
    851 
    852 #ifdef LOCKF_DEBUG
    853 /*
    854  * Print out a lock.
    855  */
    856 static void
    857 lf_print(char *tag, struct lockf *lock)
    858 {
    859 
    860 	printf("%s: lock %p for ", tag, lock);
    861 	if (lock->lf_flags & F_POSIX)
    862 		printf("proc %d", ((struct proc *)lock->lf_id)->p_pid);
    863 	else
    864 		printf("file 0x%p", (struct file *)lock->lf_id);
    865 	printf(" %s, start %qx, end %qx",
    866 		lock->lf_type == F_RDLCK ? "shared" :
    867 		lock->lf_type == F_WRLCK ? "exclusive" :
    868 		lock->lf_type == F_UNLCK ? "unlock" :
    869 		"unknown", lock->lf_start, lock->lf_end);
    870 	if (TAILQ_FIRST(&lock->lf_blkhd))
    871 		printf(" block %p\n", TAILQ_FIRST(&lock->lf_blkhd));
    872 	else
    873 		printf("\n");
    874 }
    875 
    876 static void
    877 lf_printlist(char *tag, struct lockf *lock)
    878 {
    879 	struct lockf *lf, *blk;
    880 
    881 	printf("%s: Lock list:\n", tag);
    882 	for (lf = *lock->lf_head; lf; lf = lf->lf_next) {
    883 		printf("\tlock %p for ", lf);
    884 		if (lf->lf_flags & F_POSIX)
    885 			printf("proc %d", ((struct proc *)lf->lf_id)->p_pid);
    886 		else
    887 			printf("file 0x%p", (struct file *)lf->lf_id);
    888 		printf(", %s, start %qx, end %qx",
    889 			lf->lf_type == F_RDLCK ? "shared" :
    890 			lf->lf_type == F_WRLCK ? "exclusive" :
    891 			lf->lf_type == F_UNLCK ? "unlock" :
    892 			"unknown", lf->lf_start, lf->lf_end);
    893 		TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
    894 			if (blk->lf_flags & F_POSIX)
    895 				printf("proc %d",
    896 				    ((struct proc *)blk->lf_id)->p_pid);
    897 			else
    898 				printf("file 0x%p", (struct file *)blk->lf_id);
    899 			printf(", %s, start %qx, end %qx",
    900 				blk->lf_type == F_RDLCK ? "shared" :
    901 				blk->lf_type == F_WRLCK ? "exclusive" :
    902 				blk->lf_type == F_UNLCK ? "unlock" :
    903 				"unknown", blk->lf_start, blk->lf_end);
    904 			if (TAILQ_FIRST(&blk->lf_blkhd))
    905 				 panic("lf_printlist: bad list");
    906 		}
    907 		printf("\n");
    908 	}
    909 }
    910 #endif /* LOCKF_DEBUG */
    911