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