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