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