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