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