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