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