Home | History | Annotate | Line # | Download | only in kern
vfs_lockf.c revision 1.11
      1  1.11       jtk /*	$NetBSD: vfs_lockf.c,v 1.11 1997/04/10 23:46:18 jtk 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.5       cgd  *	@(#)ufs_lockf.c	8.3 (Berkeley) 1/6/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.4   mycroft  * Do an advisory lock operation.
     67   1.1        ws  */
     68   1.4   mycroft int
     69   1.1        ws lf_advlock(head, size, id, op, fl, flags)
     70   1.1        ws 	struct lockf **head;
     71   1.3       cgd 	off_t size;
     72   1.1        ws 	caddr_t id;
     73   1.1        ws 	int op;
     74   1.1        ws 	register struct flock *fl;
     75   1.1        ws 	int flags;
     76   1.1        ws {
     77   1.1        ws 	register struct lockf *lock;
     78   1.1        ws 	off_t start, end;
     79   1.1        ws 	int error;
     80   1.1        ws 
     81   1.1        ws 	/*
     82   1.1        ws 	 * Convert the flock structure into a start and end.
     83   1.1        ws 	 */
     84   1.1        ws 	switch (fl->l_whence) {
     85   1.1        ws 	case SEEK_SET:
     86   1.1        ws 	case SEEK_CUR:
     87   1.1        ws 		/*
     88   1.1        ws 		 * Caller is responsible for adding any necessary offset
     89   1.1        ws 		 * when SEEK_CUR is used.
     90   1.1        ws 		 */
     91   1.1        ws 		start = fl->l_start;
     92   1.1        ws 		break;
     93   1.1        ws 
     94   1.1        ws 	case SEEK_END:
     95   1.1        ws 		start = size + fl->l_start;
     96   1.1        ws 		break;
     97   1.1        ws 
     98   1.1        ws 	default:
     99   1.1        ws 		return (EINVAL);
    100   1.1        ws 	}
    101   1.1        ws 	if (start < 0)
    102   1.1        ws 		return (EINVAL);
    103  1.10    kleink 
    104  1.10    kleink 	/*
    105  1.10    kleink 	 * Avoid the common case of unlocking when inode has no locks.
    106  1.10    kleink 	 */
    107  1.10    kleink 	if (*head == (struct lockf *)0) {
    108  1.10    kleink 		if (op != F_SETLK) {
    109  1.10    kleink 			fl->l_type = F_UNLCK;
    110  1.10    kleink 			return (0);
    111  1.10    kleink 		}
    112  1.10    kleink 	}
    113  1.10    kleink 
    114   1.1        ws 	if (fl->l_len == 0)
    115   1.1        ws 		end = -1;
    116   1.1        ws 	else
    117   1.1        ws 		end = start + fl->l_len - 1;
    118   1.1        ws 	/*
    119   1.4   mycroft 	 * Create the lockf structure.
    120   1.1        ws 	 */
    121   1.1        ws 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
    122   1.1        ws 	lock->lf_start = start;
    123   1.1        ws 	lock->lf_end = end;
    124   1.1        ws 	lock->lf_id = id;
    125   1.1        ws 	lock->lf_head = head;
    126   1.1        ws 	lock->lf_type = fl->l_type;
    127   1.1        ws 	lock->lf_next = (struct lockf *)0;
    128   1.1        ws 	lock->lf_block = (struct lockf *)0;
    129   1.1        ws 	lock->lf_flags = flags;
    130   1.1        ws 	/*
    131   1.1        ws 	 * Do the requested operation.
    132   1.1        ws 	 */
    133   1.4   mycroft 	switch (op) {
    134   1.4   mycroft 
    135   1.1        ws 	case F_SETLK:
    136   1.1        ws 		return (lf_setlock(lock));
    137   1.1        ws 
    138   1.1        ws 	case F_UNLCK:
    139   1.1        ws 		error = lf_clearlock(lock);
    140   1.1        ws 		FREE(lock, M_LOCKF);
    141   1.1        ws 		return (error);
    142   1.1        ws 
    143   1.1        ws 	case F_GETLK:
    144   1.1        ws 		error = lf_getlock(lock, fl);
    145   1.1        ws 		FREE(lock, M_LOCKF);
    146   1.1        ws 		return (error);
    147   1.4   mycroft 
    148   1.1        ws 	default:
    149   1.4   mycroft 		FREE(lock, M_LOCKF);
    150   1.1        ws 		return (EINVAL);
    151   1.1        ws 	}
    152   1.1        ws 	/* NOTREACHED */
    153   1.1        ws }
    154   1.1        ws 
    155   1.1        ws /*
    156   1.1        ws  * Set a byte-range lock.
    157   1.1        ws  */
    158   1.4   mycroft int
    159   1.1        ws lf_setlock(lock)
    160   1.1        ws 	register struct lockf *lock;
    161   1.1        ws {
    162   1.1        ws 	register struct lockf *block;
    163   1.1        ws 	struct lockf **head = lock->lf_head;
    164   1.1        ws 	struct lockf **prev, *overlap, *ltmp;
    165   1.1        ws 	static char lockstr[] = "lockf";
    166   1.1        ws 	int ovcase, priority, needtolink, error;
    167   1.1        ws 
    168   1.1        ws #ifdef LOCKF_DEBUG
    169   1.1        ws 	if (lockf_debug & 1)
    170   1.1        ws 		lf_print("lf_setlock", lock);
    171   1.1        ws #endif /* LOCKF_DEBUG */
    172   1.1        ws 
    173   1.1        ws 	/*
    174   1.1        ws 	 * Set the priority
    175   1.1        ws 	 */
    176   1.1        ws 	priority = PLOCK;
    177   1.1        ws 	if (lock->lf_type == F_WRLCK)
    178   1.1        ws 		priority += 4;
    179   1.1        ws 	priority |= PCATCH;
    180   1.1        ws 	/*
    181   1.1        ws 	 * Scan lock list for this file looking for locks that would block us.
    182   1.1        ws 	 */
    183   1.7  christos 	while ((block = lf_getblock(lock)) != NULL) {
    184   1.1        ws 		/*
    185   1.1        ws 		 * Free the structure and return if nonblocking.
    186   1.1        ws 		 */
    187   1.1        ws 		if ((lock->lf_flags & F_WAIT) == 0) {
    188   1.1        ws 			FREE(lock, M_LOCKF);
    189   1.1        ws 			return (EAGAIN);
    190   1.1        ws 		}
    191   1.1        ws 		/*
    192   1.1        ws 		 * We are blocked. Since flock style locks cover
    193   1.1        ws 		 * the whole file, there is no chance for deadlock.
    194   1.1        ws 		 * For byte-range locks we must check for deadlock.
    195   1.1        ws 		 *
    196   1.1        ws 		 * Deadlock detection is done by looking through the
    197   1.1        ws 		 * wait channels to see if there are any cycles that
    198   1.1        ws 		 * involve us. MAXDEPTH is set just to make sure we
    199   1.1        ws 		 * do not go off into neverland.
    200   1.1        ws 		 */
    201   1.1        ws 		if ((lock->lf_flags & F_POSIX) &&
    202   1.1        ws 		    (block->lf_flags & F_POSIX)) {
    203   1.1        ws 			register struct proc *wproc;
    204   1.1        ws 			register struct lockf *waitblock;
    205   1.1        ws 			int i = 0;
    206   1.1        ws 
    207   1.1        ws 			/* The block is waiting on something */
    208   1.1        ws 			wproc = (struct proc *)block->lf_id;
    209   1.1        ws 			while (wproc->p_wchan &&
    210   1.1        ws 			       (wproc->p_wmesg == lockstr) &&
    211   1.1        ws 			       (i++ < maxlockdepth)) {
    212   1.1        ws 				waitblock = (struct lockf *)wproc->p_wchan;
    213   1.1        ws 				/* Get the owner of the blocking lock */
    214   1.1        ws 				waitblock = waitblock->lf_next;
    215   1.1        ws 				if ((waitblock->lf_flags & F_POSIX) == 0)
    216   1.1        ws 					break;
    217   1.1        ws 				wproc = (struct proc *)waitblock->lf_id;
    218   1.1        ws 				if (wproc == (struct proc *)lock->lf_id) {
    219   1.1        ws 					free(lock, M_LOCKF);
    220   1.1        ws 					return (EDEADLK);
    221   1.1        ws 				}
    222   1.1        ws 			}
    223   1.1        ws 		}
    224   1.1        ws 		/*
    225   1.1        ws 		 * For flock type locks, we must first remove
    226   1.1        ws 		 * any shared locks that we hold before we sleep
    227   1.1        ws 		 * waiting for an exclusive lock.
    228   1.1        ws 		 */
    229   1.1        ws 		if ((lock->lf_flags & F_FLOCK) &&
    230   1.1        ws 		    lock->lf_type == F_WRLCK) {
    231   1.1        ws 			lock->lf_type = F_UNLCK;
    232   1.1        ws 			(void) lf_clearlock(lock);
    233   1.1        ws 			lock->lf_type = F_WRLCK;
    234   1.1        ws 		}
    235   1.1        ws 		/*
    236   1.1        ws 		 * Add our lock to the blocked list and sleep until we're free.
    237   1.1        ws 		 * Remember who blocked us (for deadlock detection).
    238   1.1        ws 		 */
    239   1.1        ws 		lock->lf_next = block;
    240   1.1        ws 		lf_addblock(block, lock);
    241   1.1        ws #ifdef LOCKF_DEBUG
    242   1.1        ws 		if (lockf_debug & 1) {
    243   1.1        ws 			lf_print("lf_setlock: blocking on", block);
    244   1.1        ws 			lf_printlist("lf_setlock", block);
    245   1.1        ws 		}
    246   1.1        ws #endif /* LOCKF_DEBUG */
    247   1.7  christos 		error = tsleep((caddr_t)lock, priority, lockstr, 0);
    248   1.7  christos 		if (error) {
    249   1.1        ws 			/*
    250   1.1        ws 			 * Delete ourselves from the waiting to lock list.
    251   1.1        ws 			 */
    252   1.1        ws 			for (block = lock->lf_next;
    253   1.1        ws 			     block != NOLOCKF;
    254   1.1        ws 			     block = block->lf_block) {
    255   1.1        ws 				if (block->lf_block != lock)
    256   1.1        ws 					continue;
    257   1.1        ws 				block->lf_block = block->lf_block->lf_block;
    258   1.4   mycroft 				break;
    259   1.1        ws 			}
    260   1.4   mycroft 			/*
    261   1.4   mycroft 			 * If we did not find ourselves on the list, but
    262   1.4   mycroft 			 * are still linked onto a lock list, then something
    263   1.4   mycroft 			 * is very wrong.
    264   1.4   mycroft 			 */
    265   1.4   mycroft 			if (block == NOLOCKF && lock->lf_next != NOLOCKF)
    266   1.4   mycroft 				panic("lf_setlock: lost lock");
    267   1.4   mycroft 			free(lock, M_LOCKF);
    268   1.4   mycroft 			return (error);
    269   1.1        ws 		}
    270   1.1        ws 	}
    271   1.1        ws 	/*
    272   1.1        ws 	 * No blocks!!  Add the lock.  Note that we will
    273   1.1        ws 	 * downgrade or upgrade any overlapping locks this
    274   1.1        ws 	 * process already owns.
    275   1.1        ws 	 *
    276   1.1        ws 	 * Skip over locks owned by other processes.
    277   1.1        ws 	 * Handle any locks that overlap and are owned by ourselves.
    278   1.1        ws 	 */
    279   1.1        ws 	prev = head;
    280   1.1        ws 	block = *head;
    281   1.1        ws 	needtolink = 1;
    282   1.1        ws 	for (;;) {
    283   1.7  christos 		ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
    284   1.7  christos 		if (ovcase)
    285   1.1        ws 			block = overlap->lf_next;
    286   1.1        ws 		/*
    287   1.1        ws 		 * Six cases:
    288   1.1        ws 		 *	0) no overlap
    289   1.1        ws 		 *	1) overlap == lock
    290   1.1        ws 		 *	2) overlap contains lock
    291   1.1        ws 		 *	3) lock contains overlap
    292   1.1        ws 		 *	4) overlap starts before lock
    293   1.1        ws 		 *	5) overlap ends after lock
    294   1.1        ws 		 */
    295   1.1        ws 		switch (ovcase) {
    296   1.1        ws 		case 0: /* no overlap */
    297   1.1        ws 			if (needtolink) {
    298   1.1        ws 				*prev = lock;
    299   1.1        ws 				lock->lf_next = overlap;
    300   1.1        ws 			}
    301   1.1        ws 			break;
    302   1.1        ws 
    303   1.1        ws 		case 1: /* overlap == lock */
    304   1.1        ws 			/*
    305   1.1        ws 			 * If downgrading lock, others may be
    306   1.1        ws 			 * able to acquire it.
    307   1.1        ws 			 */
    308   1.1        ws 			if (lock->lf_type == F_RDLCK &&
    309   1.1        ws 			    overlap->lf_type == F_WRLCK)
    310   1.1        ws 				lf_wakelock(overlap);
    311   1.1        ws 			overlap->lf_type = lock->lf_type;
    312   1.1        ws 			FREE(lock, M_LOCKF);
    313   1.1        ws 			lock = overlap; /* for debug output below */
    314   1.1        ws 			break;
    315   1.1        ws 
    316   1.1        ws 		case 2: /* overlap contains lock */
    317   1.1        ws 			/*
    318   1.1        ws 			 * Check for common starting point and different types.
    319   1.1        ws 			 */
    320   1.1        ws 			if (overlap->lf_type == lock->lf_type) {
    321   1.1        ws 				free(lock, M_LOCKF);
    322   1.1        ws 				lock = overlap; /* for debug output below */
    323   1.1        ws 				break;
    324   1.1        ws 			}
    325   1.1        ws 			if (overlap->lf_start == lock->lf_start) {
    326   1.1        ws 				*prev = lock;
    327   1.1        ws 				lock->lf_next = overlap;
    328   1.1        ws 				overlap->lf_start = lock->lf_end + 1;
    329   1.1        ws 			} else
    330   1.1        ws 				lf_split(overlap, lock);
    331   1.1        ws 			lf_wakelock(overlap);
    332   1.1        ws 			break;
    333   1.1        ws 
    334   1.1        ws 		case 3: /* lock contains overlap */
    335   1.1        ws 			/*
    336   1.1        ws 			 * If downgrading lock, others may be able to
    337   1.1        ws 			 * acquire it, otherwise take the list.
    338   1.1        ws 			 */
    339   1.1        ws 			if (lock->lf_type == F_RDLCK &&
    340   1.1        ws 			    overlap->lf_type == F_WRLCK) {
    341   1.1        ws 				lf_wakelock(overlap);
    342   1.1        ws 			} else {
    343   1.1        ws 				ltmp = lock->lf_block;
    344   1.1        ws 				lock->lf_block = overlap->lf_block;
    345   1.1        ws 				lf_addblock(lock, ltmp);
    346   1.1        ws 			}
    347   1.1        ws 			/*
    348   1.1        ws 			 * Add the new lock if necessary and delete the overlap.
    349   1.1        ws 			 */
    350   1.1        ws 			if (needtolink) {
    351   1.1        ws 				*prev = lock;
    352   1.1        ws 				lock->lf_next = overlap->lf_next;
    353   1.1        ws 				prev = &lock->lf_next;
    354   1.1        ws 				needtolink = 0;
    355   1.1        ws 			} else
    356   1.1        ws 				*prev = overlap->lf_next;
    357   1.1        ws 			free(overlap, M_LOCKF);
    358   1.1        ws 			continue;
    359   1.1        ws 
    360   1.1        ws 		case 4: /* overlap starts before lock */
    361   1.1        ws 			/*
    362   1.1        ws 			 * Add lock after overlap on the list.
    363   1.1        ws 			 */
    364   1.1        ws 			lock->lf_next = overlap->lf_next;
    365   1.1        ws 			overlap->lf_next = lock;
    366   1.1        ws 			overlap->lf_end = lock->lf_start - 1;
    367   1.1        ws 			prev = &lock->lf_next;
    368   1.1        ws 			lf_wakelock(overlap);
    369   1.1        ws 			needtolink = 0;
    370   1.1        ws 			continue;
    371   1.1        ws 
    372   1.1        ws 		case 5: /* overlap ends after lock */
    373   1.1        ws 			/*
    374   1.1        ws 			 * Add the new lock before overlap.
    375   1.1        ws 			 */
    376   1.1        ws 			if (needtolink) {
    377   1.1        ws 				*prev = lock;
    378   1.1        ws 				lock->lf_next = overlap;
    379   1.1        ws 			}
    380   1.1        ws 			overlap->lf_start = lock->lf_end + 1;
    381   1.1        ws 			lf_wakelock(overlap);
    382   1.1        ws 			break;
    383   1.1        ws 		}
    384   1.1        ws 		break;
    385   1.1        ws 	}
    386   1.1        ws #ifdef LOCKF_DEBUG
    387   1.1        ws 	if (lockf_debug & 1) {
    388   1.1        ws 		lf_print("lf_setlock: got the lock", lock);
    389   1.1        ws 		lf_printlist("lf_setlock", lock);
    390   1.1        ws 	}
    391   1.1        ws #endif /* LOCKF_DEBUG */
    392   1.1        ws 	return (0);
    393   1.1        ws }
    394   1.1        ws 
    395   1.1        ws /*
    396   1.1        ws  * Remove a byte-range lock on an inode.
    397   1.1        ws  *
    398   1.1        ws  * Generally, find the lock (or an overlap to that lock)
    399   1.1        ws  * and remove it (or shrink it), then wakeup anyone we can.
    400   1.1        ws  */
    401   1.4   mycroft int
    402   1.1        ws lf_clearlock(unlock)
    403   1.1        ws 	register struct lockf *unlock;
    404   1.1        ws {
    405   1.1        ws 	struct lockf **head = unlock->lf_head;
    406   1.1        ws 	register struct lockf *lf = *head;
    407   1.1        ws 	struct lockf *overlap, **prev;
    408   1.1        ws 	int ovcase;
    409   1.1        ws 
    410   1.1        ws 	if (lf == NOLOCKF)
    411   1.1        ws 		return (0);
    412   1.1        ws #ifdef LOCKF_DEBUG
    413   1.1        ws 	if (unlock->lf_type != F_UNLCK)
    414   1.1        ws 		panic("lf_clearlock: bad type");
    415   1.1        ws 	if (lockf_debug & 1)
    416   1.1        ws 		lf_print("lf_clearlock", unlock);
    417   1.1        ws #endif /* LOCKF_DEBUG */
    418   1.1        ws 	prev = head;
    419   1.7  christos 	while ((ovcase = lf_findoverlap(lf, unlock, SELF,
    420   1.7  christos 					&prev, &overlap)) != 0) {
    421   1.1        ws 		/*
    422   1.1        ws 		 * Wakeup the list of locks to be retried.
    423   1.1        ws 		 */
    424   1.1        ws 		lf_wakelock(overlap);
    425   1.1        ws 
    426   1.1        ws 		switch (ovcase) {
    427   1.1        ws 
    428   1.1        ws 		case 1: /* overlap == lock */
    429   1.1        ws 			*prev = overlap->lf_next;
    430   1.1        ws 			FREE(overlap, M_LOCKF);
    431   1.1        ws 			break;
    432   1.1        ws 
    433   1.1        ws 		case 2: /* overlap contains lock: split it */
    434   1.1        ws 			if (overlap->lf_start == unlock->lf_start) {
    435   1.1        ws 				overlap->lf_start = unlock->lf_end + 1;
    436   1.1        ws 				break;
    437   1.1        ws 			}
    438   1.1        ws 			lf_split(overlap, unlock);
    439   1.1        ws 			overlap->lf_next = unlock->lf_next;
    440   1.1        ws 			break;
    441   1.1        ws 
    442   1.1        ws 		case 3: /* lock contains overlap */
    443   1.1        ws 			*prev = overlap->lf_next;
    444   1.1        ws 			lf = overlap->lf_next;
    445   1.1        ws 			free(overlap, M_LOCKF);
    446   1.1        ws 			continue;
    447   1.1        ws 
    448   1.1        ws 		case 4: /* overlap starts before lock */
    449   1.1        ws 			overlap->lf_end = unlock->lf_start - 1;
    450   1.1        ws 			prev = &overlap->lf_next;
    451   1.1        ws 			lf = overlap->lf_next;
    452   1.1        ws 			continue;
    453   1.1        ws 
    454   1.1        ws 		case 5: /* overlap ends after lock */
    455   1.1        ws 			overlap->lf_start = unlock->lf_end + 1;
    456   1.1        ws 			break;
    457   1.1        ws 		}
    458   1.1        ws 		break;
    459   1.1        ws 	}
    460   1.1        ws #ifdef LOCKF_DEBUG
    461   1.1        ws 	if (lockf_debug & 1)
    462   1.1        ws 		lf_printlist("lf_clearlock", unlock);
    463   1.1        ws #endif /* LOCKF_DEBUG */
    464   1.1        ws 	return (0);
    465   1.1        ws }
    466   1.1        ws 
    467   1.1        ws /*
    468   1.1        ws  * Check whether there is a blocking lock,
    469   1.1        ws  * and if so return its process identifier.
    470   1.1        ws  */
    471   1.4   mycroft int
    472   1.1        ws lf_getlock(lock, fl)
    473   1.1        ws 	register struct lockf *lock;
    474   1.1        ws 	register struct flock *fl;
    475   1.1        ws {
    476   1.1        ws 	register struct lockf *block;
    477   1.1        ws 
    478   1.1        ws #ifdef LOCKF_DEBUG
    479   1.1        ws 	if (lockf_debug & 1)
    480   1.1        ws 		lf_print("lf_getlock", lock);
    481   1.1        ws #endif /* LOCKF_DEBUG */
    482   1.1        ws 
    483   1.7  christos 	if ((block = lf_getblock(lock)) != NULL) {
    484   1.1        ws 		fl->l_type = block->lf_type;
    485   1.1        ws 		fl->l_whence = SEEK_SET;
    486   1.1        ws 		fl->l_start = block->lf_start;
    487   1.1        ws 		if (block->lf_end == -1)
    488   1.1        ws 			fl->l_len = 0;
    489   1.1        ws 		else
    490   1.1        ws 			fl->l_len = block->lf_end - block->lf_start + 1;
    491   1.1        ws 		if (block->lf_flags & F_POSIX)
    492   1.1        ws 			fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
    493   1.1        ws 		else
    494   1.1        ws 			fl->l_pid = -1;
    495   1.1        ws 	} else {
    496   1.1        ws 		fl->l_type = F_UNLCK;
    497   1.1        ws 	}
    498   1.1        ws 	return (0);
    499   1.1        ws }
    500   1.1        ws 
    501   1.1        ws /*
    502   1.1        ws  * Walk the list of locks for an inode and
    503   1.1        ws  * return the first blocking lock.
    504   1.1        ws  */
    505   1.1        ws struct lockf *
    506   1.1        ws lf_getblock(lock)
    507   1.1        ws 	register struct lockf *lock;
    508   1.1        ws {
    509   1.1        ws 	struct lockf **prev, *overlap, *lf = *(lock->lf_head);
    510   1.1        ws 	int ovcase;
    511   1.1        ws 
    512   1.1        ws 	prev = lock->lf_head;
    513   1.7  christos 	while ((ovcase = lf_findoverlap(lf, lock, OTHERS,
    514   1.7  christos 					&prev, &overlap)) != 0) {
    515   1.1        ws 		/*
    516   1.1        ws 		 * We've found an overlap, see if it blocks us
    517   1.1        ws 		 */
    518   1.1        ws 		if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
    519   1.1        ws 			return (overlap);
    520   1.1        ws 		/*
    521   1.1        ws 		 * Nope, point to the next one on the list and
    522   1.1        ws 		 * see if it blocks us
    523   1.1        ws 		 */
    524   1.1        ws 		lf = overlap->lf_next;
    525   1.1        ws 	}
    526   1.1        ws 	return (NOLOCKF);
    527   1.1        ws }
    528   1.1        ws 
    529   1.1        ws /*
    530   1.1        ws  * Walk the list of locks for an inode to
    531   1.1        ws  * find an overlapping lock (if any).
    532   1.1        ws  *
    533   1.1        ws  * NOTE: this returns only the FIRST overlapping lock.  There
    534   1.1        ws  *	 may be more than one.
    535   1.1        ws  */
    536   1.4   mycroft int
    537   1.1        ws lf_findoverlap(lf, lock, type, prev, overlap)
    538   1.1        ws 	register struct lockf *lf;
    539   1.1        ws 	struct lockf *lock;
    540   1.1        ws 	int type;
    541   1.1        ws 	struct lockf ***prev;
    542   1.1        ws 	struct lockf **overlap;
    543   1.1        ws {
    544   1.1        ws 	off_t start, end;
    545   1.1        ws 
    546   1.1        ws 	*overlap = lf;
    547   1.1        ws 	if (lf == NOLOCKF)
    548   1.1        ws 		return (0);
    549   1.1        ws #ifdef LOCKF_DEBUG
    550   1.1        ws 	if (lockf_debug & 2)
    551   1.1        ws 		lf_print("lf_findoverlap: looking for overlap in", lock);
    552   1.1        ws #endif /* LOCKF_DEBUG */
    553   1.1        ws 	start = lock->lf_start;
    554   1.1        ws 	end = lock->lf_end;
    555   1.1        ws 	while (lf != NOLOCKF) {
    556   1.1        ws 		if (((type & SELF) && lf->lf_id != lock->lf_id) ||
    557   1.1        ws 		    ((type & OTHERS) && lf->lf_id == lock->lf_id)) {
    558   1.1        ws 			*prev = &lf->lf_next;
    559   1.1        ws 			*overlap = lf = lf->lf_next;
    560   1.1        ws 			continue;
    561   1.1        ws 		}
    562   1.1        ws #ifdef LOCKF_DEBUG
    563   1.1        ws 		if (lockf_debug & 2)
    564   1.1        ws 			lf_print("\tchecking", lf);
    565   1.1        ws #endif /* LOCKF_DEBUG */
    566   1.1        ws 		/*
    567   1.1        ws 		 * OK, check for overlap
    568   1.1        ws 		 *
    569   1.1        ws 		 * Six cases:
    570   1.1        ws 		 *	0) no overlap
    571   1.1        ws 		 *	1) overlap == lock
    572   1.1        ws 		 *	2) overlap contains lock
    573   1.1        ws 		 *	3) lock contains overlap
    574   1.1        ws 		 *	4) overlap starts before lock
    575   1.1        ws 		 *	5) overlap ends after lock
    576   1.1        ws 		 */
    577   1.1        ws 		if ((lf->lf_end != -1 && start > lf->lf_end) ||
    578   1.1        ws 		    (end != -1 && lf->lf_start > end)) {
    579   1.1        ws 			/* Case 0 */
    580   1.1        ws #ifdef LOCKF_DEBUG
    581   1.1        ws 			if (lockf_debug & 2)
    582   1.9  christos 				printf("no overlap\n");
    583   1.1        ws #endif /* LOCKF_DEBUG */
    584   1.1        ws 			if ((type & SELF) && end != -1 && lf->lf_start > end)
    585   1.1        ws 				return (0);
    586   1.1        ws 			*prev = &lf->lf_next;
    587   1.1        ws 			*overlap = lf = lf->lf_next;
    588   1.1        ws 			continue;
    589   1.1        ws 		}
    590   1.1        ws 		if ((lf->lf_start == start) && (lf->lf_end == end)) {
    591   1.1        ws 			/* Case 1 */
    592   1.1        ws #ifdef LOCKF_DEBUG
    593   1.1        ws 			if (lockf_debug & 2)
    594   1.9  christos 				printf("overlap == lock\n");
    595   1.1        ws #endif /* LOCKF_DEBUG */
    596   1.1        ws 			return (1);
    597   1.1        ws 		}
    598   1.1        ws 		if ((lf->lf_start <= start) &&
    599   1.1        ws 		    (end != -1) &&
    600   1.1        ws 		    ((lf->lf_end >= end) || (lf->lf_end == -1))) {
    601   1.1        ws 			/* Case 2 */
    602   1.1        ws #ifdef LOCKF_DEBUG
    603   1.1        ws 			if (lockf_debug & 2)
    604   1.9  christos 				printf("overlap contains lock\n");
    605   1.1        ws #endif /* LOCKF_DEBUG */
    606   1.1        ws 			return (2);
    607   1.1        ws 		}
    608   1.1        ws 		if (start <= lf->lf_start &&
    609   1.4   mycroft 		           (end == -1 ||
    610   1.1        ws 			   (lf->lf_end != -1 && end >= lf->lf_end))) {
    611   1.1        ws 			/* Case 3 */
    612   1.1        ws #ifdef LOCKF_DEBUG
    613   1.1        ws 			if (lockf_debug & 2)
    614   1.9  christos 				printf("lock contains overlap\n");
    615   1.1        ws #endif /* LOCKF_DEBUG */
    616   1.1        ws 			return (3);
    617   1.1        ws 		}
    618   1.1        ws 		if ((lf->lf_start < start) &&
    619   1.1        ws 			((lf->lf_end >= start) || (lf->lf_end == -1))) {
    620   1.1        ws 			/* Case 4 */
    621   1.1        ws #ifdef LOCKF_DEBUG
    622   1.1        ws 			if (lockf_debug & 2)
    623   1.9  christos 				printf("overlap starts before lock\n");
    624   1.1        ws #endif /* LOCKF_DEBUG */
    625   1.1        ws 			return (4);
    626   1.1        ws 		}
    627   1.1        ws 		if ((lf->lf_start > start) &&
    628   1.1        ws 			(end != -1) &&
    629   1.1        ws 			((lf->lf_end > end) || (lf->lf_end == -1))) {
    630   1.1        ws 			/* Case 5 */
    631   1.1        ws #ifdef LOCKF_DEBUG
    632   1.1        ws 			if (lockf_debug & 2)
    633   1.9  christos 				printf("overlap ends after lock\n");
    634   1.1        ws #endif /* LOCKF_DEBUG */
    635   1.1        ws 			return (5);
    636   1.1        ws 		}
    637   1.1        ws 		panic("lf_findoverlap: default");
    638   1.1        ws 	}
    639   1.1        ws 	return (0);
    640   1.1        ws }
    641   1.1        ws 
    642   1.1        ws /*
    643   1.1        ws  * Add a lock to the end of the blocked list.
    644   1.1        ws  */
    645   1.3       cgd void
    646   1.1        ws lf_addblock(lock, blocked)
    647   1.1        ws 	struct lockf *lock;
    648   1.1        ws 	struct lockf *blocked;
    649   1.1        ws {
    650   1.1        ws 	register struct lockf *lf;
    651   1.1        ws 
    652   1.1        ws 	if (blocked == NOLOCKF)
    653   1.1        ws 		return;
    654   1.1        ws #ifdef LOCKF_DEBUG
    655   1.1        ws 	if (lockf_debug & 2) {
    656   1.1        ws 		lf_print("addblock: adding", blocked);
    657   1.1        ws 		lf_print("to blocked list of", lock);
    658   1.1        ws 	}
    659   1.1        ws #endif /* LOCKF_DEBUG */
    660   1.1        ws 	if ((lf = lock->lf_block) == NOLOCKF) {
    661   1.1        ws 		lock->lf_block = blocked;
    662   1.1        ws 		return;
    663   1.1        ws 	}
    664   1.1        ws 	while (lf->lf_block != NOLOCKF)
    665   1.1        ws 		lf = lf->lf_block;
    666   1.1        ws 	lf->lf_block = blocked;
    667   1.1        ws 	return;
    668   1.1        ws }
    669   1.1        ws 
    670   1.1        ws /*
    671   1.1        ws  * Split a lock and a contained region into
    672   1.1        ws  * two or three locks as necessary.
    673   1.1        ws  */
    674   1.2       cgd void
    675   1.1        ws lf_split(lock1, lock2)
    676   1.1        ws 	register struct lockf *lock1;
    677   1.1        ws 	register struct lockf *lock2;
    678   1.1        ws {
    679   1.1        ws 	register struct lockf *splitlock;
    680   1.1        ws 
    681   1.1        ws #ifdef LOCKF_DEBUG
    682   1.1        ws 	if (lockf_debug & 2) {
    683   1.1        ws 		lf_print("lf_split", lock1);
    684   1.1        ws 		lf_print("splitting from", lock2);
    685   1.1        ws 	}
    686   1.1        ws #endif /* LOCKF_DEBUG */
    687   1.1        ws 	/*
    688   1.1        ws 	 * Check to see if spliting into only two pieces.
    689   1.1        ws 	 */
    690   1.1        ws 	if (lock1->lf_start == lock2->lf_start) {
    691   1.1        ws 		lock1->lf_start = lock2->lf_end + 1;
    692   1.1        ws 		lock2->lf_next = lock1;
    693   1.1        ws 		return;
    694   1.1        ws 	}
    695   1.1        ws 	if (lock1->lf_end == lock2->lf_end) {
    696   1.1        ws 		lock1->lf_end = lock2->lf_start - 1;
    697   1.1        ws 		lock2->lf_next = lock1->lf_next;
    698   1.1        ws 		lock1->lf_next = lock2;
    699   1.1        ws 		return;
    700   1.1        ws 	}
    701   1.1        ws 	/*
    702   1.1        ws 	 * Make a new lock consisting of the last part of
    703   1.1        ws 	 * the encompassing lock
    704   1.1        ws 	 */
    705   1.1        ws 	MALLOC(splitlock, struct lockf *, sizeof *splitlock, M_LOCKF, M_WAITOK);
    706   1.1        ws 	bcopy((caddr_t)lock1, (caddr_t)splitlock, sizeof *splitlock);
    707   1.1        ws 	splitlock->lf_start = lock2->lf_end + 1;
    708   1.1        ws 	splitlock->lf_block = NOLOCKF;
    709   1.1        ws 	lock1->lf_end = lock2->lf_start - 1;
    710   1.1        ws 	/*
    711   1.1        ws 	 * OK, now link it in
    712   1.1        ws 	 */
    713   1.1        ws 	splitlock->lf_next = lock1->lf_next;
    714   1.1        ws 	lock2->lf_next = splitlock;
    715   1.1        ws 	lock1->lf_next = lock2;
    716   1.1        ws }
    717   1.1        ws 
    718   1.1        ws /*
    719   1.1        ws  * Wakeup a blocklist
    720   1.1        ws  */
    721   1.2       cgd void
    722   1.1        ws lf_wakelock(listhead)
    723   1.1        ws 	struct lockf *listhead;
    724   1.1        ws {
    725   1.1        ws 	register struct lockf *blocklist, *wakelock;
    726   1.1        ws 
    727   1.1        ws 	blocklist = listhead->lf_block;
    728   1.1        ws 	listhead->lf_block = NOLOCKF;
    729   1.1        ws 	while (blocklist != NOLOCKF) {
    730   1.1        ws 		wakelock = blocklist;
    731   1.1        ws 		blocklist = blocklist->lf_block;
    732   1.1        ws 		wakelock->lf_block = NOLOCKF;
    733   1.1        ws 		wakelock->lf_next = NOLOCKF;
    734   1.1        ws #ifdef LOCKF_DEBUG
    735   1.1        ws 		if (lockf_debug & 2)
    736   1.1        ws 			lf_print("lf_wakelock: awakening", wakelock);
    737   1.1        ws #endif /* LOCKF_DEBUG */
    738   1.1        ws 		wakeup((caddr_t)wakelock);
    739   1.1        ws 	}
    740   1.1        ws }
    741   1.1        ws 
    742   1.1        ws #ifdef LOCKF_DEBUG
    743   1.1        ws /*
    744   1.1        ws  * Print out a lock.
    745   1.1        ws  */
    746   1.4   mycroft void
    747   1.1        ws lf_print(tag, lock)
    748   1.1        ws 	char *tag;
    749   1.1        ws 	register struct lockf *lock;
    750   1.1        ws {
    751   1.1        ws 
    752   1.9  christos 	printf("%s: lock %p for ", tag, lock);
    753   1.1        ws 	if (lock->lf_flags & F_POSIX)
    754   1.9  christos 		printf("proc %d", ((struct proc *)(lock->lf_id))->p_pid);
    755   1.1        ws 	else
    756  1.11       jtk 		printf("id 0x%p", lock->lf_id);
    757  1.11       jtk 	printf(" %s, start %qx, end %qx",
    758   1.1        ws 		lock->lf_type == F_RDLCK ? "shared" :
    759   1.1        ws 		lock->lf_type == F_WRLCK ? "exclusive" :
    760   1.1        ws 		lock->lf_type == F_UNLCK ? "unlock" :
    761   1.1        ws 		"unknown", lock->lf_start, lock->lf_end);
    762   1.1        ws 	if (lock->lf_block)
    763   1.9  christos 		printf(" block %p\n", lock->lf_block);
    764   1.1        ws 	else
    765   1.9  christos 		printf("\n");
    766   1.1        ws }
    767   1.1        ws 
    768   1.4   mycroft void
    769   1.1        ws lf_printlist(tag, lock)
    770   1.1        ws 	char *tag;
    771   1.1        ws 	struct lockf *lock;
    772   1.1        ws {
    773   1.1        ws 	register struct lockf *lf;
    774   1.1        ws 
    775  1.11       jtk 	printf("%s: Lock list:\n", tag);
    776  1.11       jtk 	for (lf = lock; lf; lf = lf->lf_block) {
    777   1.9  christos 		printf("\tlock %p for ", lf);
    778   1.1        ws 		if (lf->lf_flags & F_POSIX)
    779   1.9  christos 			printf("proc %d", ((struct proc *)(lf->lf_id))->p_pid);
    780   1.1        ws 		else
    781  1.11       jtk 			printf("id 0x%p", lf->lf_id);
    782  1.11       jtk 		printf(", %s, start %qx, end %qx",
    783   1.1        ws 			lf->lf_type == F_RDLCK ? "shared" :
    784   1.1        ws 			lf->lf_type == F_WRLCK ? "exclusive" :
    785   1.1        ws 			lf->lf_type == F_UNLCK ? "unlock" :
    786   1.1        ws 			"unknown", lf->lf_start, lf->lf_end);
    787   1.1        ws 		if (lf->lf_block)
    788   1.9  christos 			printf(" block %p\n", lf->lf_block);
    789   1.1        ws 		else
    790   1.9  christos 			printf("\n");
    791   1.1        ws 	}
    792   1.1        ws }
    793   1.1        ws #endif /* LOCKF_DEBUG */
    794