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