xref: /freebsd/sys/kern/kern_lockf.c (revision bc02f1d98ddb492228c223906ddda11be87b48fb)
19454b2d8SWarner Losh /*-
292dc7331SDavid Greenman  * Copyright (c) 1982, 1986, 1989, 1993
392dc7331SDavid Greenman  *	The Regents of the University of California.  All rights reserved.
492dc7331SDavid Greenman  *
592dc7331SDavid Greenman  * This code is derived from software contributed to Berkeley by
692dc7331SDavid Greenman  * Scooter Morris at Genentech Inc.
792dc7331SDavid Greenman  *
892dc7331SDavid Greenman  * Redistribution and use in source and binary forms, with or without
992dc7331SDavid Greenman  * modification, are permitted provided that the following conditions
1092dc7331SDavid Greenman  * are met:
1192dc7331SDavid Greenman  * 1. Redistributions of source code must retain the above copyright
1292dc7331SDavid Greenman  *    notice, this list of conditions and the following disclaimer.
1392dc7331SDavid Greenman  * 2. Redistributions in binary form must reproduce the above copyright
1492dc7331SDavid Greenman  *    notice, this list of conditions and the following disclaimer in the
1592dc7331SDavid Greenman  *    documentation and/or other materials provided with the distribution.
1692dc7331SDavid Greenman  * 4. Neither the name of the University nor the names of its contributors
1792dc7331SDavid Greenman  *    may be used to endorse or promote products derived from this software
1892dc7331SDavid Greenman  *    without specific prior written permission.
1992dc7331SDavid Greenman  *
2092dc7331SDavid Greenman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2192dc7331SDavid Greenman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2292dc7331SDavid Greenman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2392dc7331SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2492dc7331SDavid Greenman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2592dc7331SDavid Greenman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2692dc7331SDavid Greenman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2792dc7331SDavid Greenman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2892dc7331SDavid Greenman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2992dc7331SDavid Greenman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3092dc7331SDavid Greenman  * SUCH DAMAGE.
3192dc7331SDavid Greenman  *
3292dc7331SDavid Greenman  *	@(#)ufs_lockf.c	8.3 (Berkeley) 1/6/94
3392dc7331SDavid Greenman  */
3492dc7331SDavid Greenman 
35677b542eSDavid E. O'Brien #include <sys/cdefs.h>
36677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
37677b542eSDavid E. O'Brien 
383f2076daSEivind Eklund #include "opt_debug_lockf.h"
393f2076daSEivind Eklund 
4092dc7331SDavid Greenman #include <sys/param.h>
4192dc7331SDavid Greenman #include <sys/systm.h>
421c5bb3eaSPeter Wemm #include <sys/kernel.h>
43104a9b7eSAlexander Kabaev #include <sys/limits.h>
441cd52ec3SBruce Evans #include <sys/lock.h>
457f52a691SPoul-Henning Kamp #include <sys/mount.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
4792dc7331SDavid Greenman #include <sys/proc.h>
48b71fec07SBruce Evans #include <sys/unistd.h>
4992dc7331SDavid Greenman #include <sys/vnode.h>
5092dc7331SDavid Greenman #include <sys/malloc.h>
5192dc7331SDavid Greenman #include <sys/fcntl.h>
5292dc7331SDavid Greenman #include <sys/lockf.h>
5392dc7331SDavid Greenman 
5492dc7331SDavid Greenman /*
5592dc7331SDavid Greenman  * This variable controls the maximum number of processes that will
5692dc7331SDavid Greenman  * be checked in doing deadlock detection.
5792dc7331SDavid Greenman  */
58996c772fSJohn Dyson static int maxlockdepth = MAXDEPTH;
5992dc7331SDavid Greenman 
6092dc7331SDavid Greenman #ifdef LOCKF_DEBUG
61996c772fSJohn Dyson #include <sys/sysctl.h>
62a8687b6dSBruce Evans 
63a8687b6dSBruce Evans #include <ufs/ufs/quota.h>
64a8687b6dSBruce Evans #include <ufs/ufs/inode.h>
65a8687b6dSBruce Evans 
6655166637SPoul-Henning Kamp 
677f725eacSBruce Evans static int	lockf_debug = 0;
687f725eacSBruce Evans SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
6992dc7331SDavid Greenman #endif
7092dc7331SDavid Greenman 
71603c8667SAlfred Perlstein MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
7255166637SPoul-Henning Kamp 
7392dc7331SDavid Greenman #define NOLOCKF (struct lockf *)0
7492dc7331SDavid Greenman #define SELF	0x1
7592dc7331SDavid Greenman #define OTHERS	0x2
76bc02f1d9SJeff Roberson static int	 lf_clearlock(struct lockf *, struct lockf **);
774d77a549SAlfred Perlstein static int	 lf_findoverlap(struct lockf *,
784d77a549SAlfred Perlstein 	    struct lockf *, int, struct lockf ***, struct lockf **);
7987b6de2bSPoul-Henning Kamp static struct lockf *
804d77a549SAlfred Perlstein 	 lf_getblock(struct lockf *);
814d77a549SAlfred Perlstein static int	 lf_getlock(struct lockf *, struct flock *);
82bc02f1d9SJeff Roberson static int	 lf_setlock(struct lockf *, struct vnode *, struct lockf **);
83bc02f1d9SJeff Roberson static void	 lf_split(struct lockf *, struct lockf *, struct lockf **);
844d77a549SAlfred Perlstein static void	 lf_wakelock(struct lockf *);
85013e6650SJeff Roberson #ifdef LOCKF_DEBUG
86013e6650SJeff Roberson static void	 lf_print(char *, struct lockf *);
87013e6650SJeff Roberson static void	 lf_printlist(char *, struct lockf *);
88013e6650SJeff Roberson #endif
8992dc7331SDavid Greenman 
9092dc7331SDavid Greenman /*
9192dc7331SDavid Greenman  * Advisory record locking support
9292dc7331SDavid Greenman  */
9392dc7331SDavid Greenman int
9492dc7331SDavid Greenman lf_advlock(ap, head, size)
9592dc7331SDavid Greenman 	struct vop_advlock_args /* {
9692dc7331SDavid Greenman 		struct vnode *a_vp;
9792dc7331SDavid Greenman 		caddr_t  a_id;
9892dc7331SDavid Greenman 		int  a_op;
9992dc7331SDavid Greenman 		struct flock *a_fl;
10092dc7331SDavid Greenman 		int  a_flags;
10192dc7331SDavid Greenman 	} */ *ap;
10292dc7331SDavid Greenman 	struct lockf **head;
10392dc7331SDavid Greenman 	u_quad_t size;
10492dc7331SDavid Greenman {
105bc02f1d9SJeff Roberson 	struct flock *fl = ap->a_fl;
106bc02f1d9SJeff Roberson 	struct lockf *lock;
107bc02f1d9SJeff Roberson 	struct vnode *vp = ap->a_vp;
108c4778eedSAndrey A. Chernov 	off_t start, end, oadd;
109bc02f1d9SJeff Roberson 	struct lockf *split;
11092dc7331SDavid Greenman 	int error;
11192dc7331SDavid Greenman 
11292dc7331SDavid Greenman 	/*
11392dc7331SDavid Greenman 	 * Convert the flock structure into a start and end.
11492dc7331SDavid Greenman 	 */
11592dc7331SDavid Greenman 	switch (fl->l_whence) {
11692dc7331SDavid Greenman 
11792dc7331SDavid Greenman 	case SEEK_SET:
11892dc7331SDavid Greenman 	case SEEK_CUR:
11992dc7331SDavid Greenman 		/*
12092dc7331SDavid Greenman 		 * Caller is responsible for adding any necessary offset
12192dc7331SDavid Greenman 		 * when SEEK_CUR is used.
12292dc7331SDavid Greenman 		 */
12392dc7331SDavid Greenman 		start = fl->l_start;
12492dc7331SDavid Greenman 		break;
12592dc7331SDavid Greenman 
12692dc7331SDavid Greenman 	case SEEK_END:
127c8e76343SAndrey A. Chernov 		if (size > OFF_MAX ||
128bc02f1d9SJeff Roberson 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
129bc02f1d9SJeff Roberson 			return (EOVERFLOW);
13092dc7331SDavid Greenman 		start = size + fl->l_start;
13192dc7331SDavid Greenman 		break;
13292dc7331SDavid Greenman 
13392dc7331SDavid Greenman 	default:
134bc02f1d9SJeff Roberson 		return (EINVAL);
13592dc7331SDavid Greenman 	}
136bc02f1d9SJeff Roberson 	if (start < 0)
137bc02f1d9SJeff Roberson 		return (EINVAL);
138f510e1c2SAndrey A. Chernov 	if (fl->l_len < 0) {
139bc02f1d9SJeff Roberson 		if (start == 0)
140bc02f1d9SJeff Roberson 			return (EINVAL);
141f510e1c2SAndrey A. Chernov 		end = start - 1;
14262be011eSAndrey A. Chernov 		start += fl->l_len;
143bc02f1d9SJeff Roberson 		if (start < 0)
144bc02f1d9SJeff Roberson 			return (EINVAL);
145f510e1c2SAndrey A. Chernov 	} else if (fl->l_len == 0)
14692dc7331SDavid Greenman 		end = -1;
147a88bd8aaSBruce Evans 	else {
148c4778eedSAndrey A. Chernov 		oadd = fl->l_len - 1;
149bc02f1d9SJeff Roberson 		if (oadd > OFF_MAX - start)
150bc02f1d9SJeff Roberson 			return (EOVERFLOW);
15169cc1d0dSAndrey A. Chernov 		end = start + oadd;
152a88bd8aaSBruce Evans 	}
153a88bd8aaSBruce Evans 	/*
154a88bd8aaSBruce Evans 	 * Avoid the common case of unlocking when inode has no locks.
155a88bd8aaSBruce Evans 	 */
156a88bd8aaSBruce Evans 	if (*head == (struct lockf *)0) {
157a88bd8aaSBruce Evans 		if (ap->a_op != F_SETLK) {
158a88bd8aaSBruce Evans 			fl->l_type = F_UNLCK;
159bc02f1d9SJeff Roberson 			return (0);
160a88bd8aaSBruce Evans 		}
161a88bd8aaSBruce Evans 	}
16292dc7331SDavid Greenman 	/*
163bc02f1d9SJeff Roberson 	 * Allocate a spare structure in case we have to split.
164bc02f1d9SJeff Roberson 	 */
165bc02f1d9SJeff Roberson 	split = NULL;
166bc02f1d9SJeff Roberson 	if (ap->a_op == F_SETLK || ap->a_op == F_UNLCK)
167bc02f1d9SJeff Roberson 		MALLOC(split, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
168bc02f1d9SJeff Roberson 	/*
16992dc7331SDavid Greenman 	 * Create the lockf structure
17092dc7331SDavid Greenman 	 */
171a163d034SWarner Losh 	MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
17292dc7331SDavid Greenman 	lock->lf_start = start;
17392dc7331SDavid Greenman 	lock->lf_end = end;
17492dc7331SDavid Greenman 	lock->lf_id = ap->a_id;
17559aff5fcSAlfred Perlstein 	/*
17659aff5fcSAlfred Perlstein 	 * XXX The problem is that VTOI is ufs specific, so it will
17759aff5fcSAlfred Perlstein 	 * break LOCKF_DEBUG for all other FS's other than UFS because
17859aff5fcSAlfred Perlstein 	 * it casts the vnode->data ptr to struct inode *.
17959aff5fcSAlfred Perlstein 	 */
18059aff5fcSAlfred Perlstein /*	lock->lf_inode = VTOI(ap->a_vp); */
18159aff5fcSAlfred Perlstein 	lock->lf_inode = (struct inode *)0;
18292dc7331SDavid Greenman 	lock->lf_type = fl->l_type;
183996c772fSJohn Dyson 	lock->lf_head = head;
18492dc7331SDavid Greenman 	lock->lf_next = (struct lockf *)0;
185996c772fSJohn Dyson 	TAILQ_INIT(&lock->lf_blkhd);
18692dc7331SDavid Greenman 	lock->lf_flags = ap->a_flags;
18792dc7331SDavid Greenman 	/*
18892dc7331SDavid Greenman 	 * Do the requested operation.
18992dc7331SDavid Greenman 	 */
190bc02f1d9SJeff Roberson 	VI_LOCK(vp);
19192dc7331SDavid Greenman 	switch(ap->a_op) {
19292dc7331SDavid Greenman 	case F_SETLK:
193bc02f1d9SJeff Roberson 		error = lf_setlock(lock, vp, &split);
194bc02f1d9SJeff Roberson 		break;
19592dc7331SDavid Greenman 
19692dc7331SDavid Greenman 	case F_UNLCK:
197bc02f1d9SJeff Roberson 		error = lf_clearlock(lock, &split);
19892dc7331SDavid Greenman 		FREE(lock, M_LOCKF);
199bc02f1d9SJeff Roberson 		break;
20092dc7331SDavid Greenman 
20192dc7331SDavid Greenman 	case F_GETLK:
20292dc7331SDavid Greenman 		error = lf_getlock(lock, fl);
20392dc7331SDavid Greenman 		FREE(lock, M_LOCKF);
204bc02f1d9SJeff Roberson 		break;
20592dc7331SDavid Greenman 
20692dc7331SDavid Greenman 	default:
20792dc7331SDavid Greenman 		free(lock, M_LOCKF);
208013e6650SJeff Roberson 		error = EINVAL;
209bc02f1d9SJeff Roberson 		break;
21092dc7331SDavid Greenman 	}
211bc02f1d9SJeff Roberson 	VI_UNLOCK(vp);
212bc02f1d9SJeff Roberson 	if (split)
213bc02f1d9SJeff Roberson 		FREE(split, M_LOCKF);
214013e6650SJeff Roberson 	return (error);
21592dc7331SDavid Greenman }
21692dc7331SDavid Greenman 
21792dc7331SDavid Greenman /*
21892dc7331SDavid Greenman  * Set a byte-range lock.
21992dc7331SDavid Greenman  */
22087b6de2bSPoul-Henning Kamp static int
221bc02f1d9SJeff Roberson lf_setlock(lock, vp, split)
222bc02f1d9SJeff Roberson 	struct lockf *lock;
223bc02f1d9SJeff Roberson 	struct vnode *vp;
224bc02f1d9SJeff Roberson 	struct lockf **split;
22592dc7331SDavid Greenman {
226bc02f1d9SJeff Roberson 	struct lockf *block;
22792dc7331SDavid Greenman 	struct lockf **head = lock->lf_head;
22892dc7331SDavid Greenman 	struct lockf **prev, *overlap, *ltmp;
22992dc7331SDavid Greenman 	static char lockstr[] = "lockf";
23092dc7331SDavid Greenman 	int ovcase, priority, needtolink, error;
23192dc7331SDavid Greenman 
23292dc7331SDavid Greenman #ifdef LOCKF_DEBUG
23392dc7331SDavid Greenman 	if (lockf_debug & 1)
23492dc7331SDavid Greenman 		lf_print("lf_setlock", lock);
23592dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
23692dc7331SDavid Greenman 
23792dc7331SDavid Greenman 	/*
23892dc7331SDavid Greenman 	 * Set the priority
23992dc7331SDavid Greenman 	 */
24092dc7331SDavid Greenman 	priority = PLOCK;
24192dc7331SDavid Greenman 	if (lock->lf_type == F_WRLCK)
24292dc7331SDavid Greenman 		priority += 4;
24392dc7331SDavid Greenman 	priority |= PCATCH;
24492dc7331SDavid Greenman 	/*
24592dc7331SDavid Greenman 	 * Scan lock list for this file looking for locks that would block us.
24692dc7331SDavid Greenman 	 */
247bb56ec4aSPoul-Henning Kamp 	while ((block = lf_getblock(lock))) {
24892dc7331SDavid Greenman 		/*
24992dc7331SDavid Greenman 		 * Free the structure and return if nonblocking.
25092dc7331SDavid Greenman 		 */
25192dc7331SDavid Greenman 		if ((lock->lf_flags & F_WAIT) == 0) {
25292dc7331SDavid Greenman 			FREE(lock, M_LOCKF);
25392dc7331SDavid Greenman 			return (EAGAIN);
25492dc7331SDavid Greenman 		}
25592dc7331SDavid Greenman 		/*
25692dc7331SDavid Greenman 		 * We are blocked. Since flock style locks cover
25792dc7331SDavid Greenman 		 * the whole file, there is no chance for deadlock.
25892dc7331SDavid Greenman 		 * For byte-range locks we must check for deadlock.
25992dc7331SDavid Greenman 		 *
26092dc7331SDavid Greenman 		 * Deadlock detection is done by looking through the
26192dc7331SDavid Greenman 		 * wait channels to see if there are any cycles that
26292dc7331SDavid Greenman 		 * involve us. MAXDEPTH is set just to make sure we
26392dc7331SDavid Greenman 		 * do not go off into neverland.
26492dc7331SDavid Greenman 		 */
26592dc7331SDavid Greenman 		if ((lock->lf_flags & F_POSIX) &&
26692dc7331SDavid Greenman 		    (block->lf_flags & F_POSIX)) {
267982d11f8SJeff Roberson 			struct proc *wproc;
268982d11f8SJeff Roberson 			struct proc *nproc;
269b40ce416SJulian Elischer 			struct thread *td;
270982d11f8SJeff Roberson 			struct lockf *waitblock;
27192dc7331SDavid Greenman 			int i = 0;
27292dc7331SDavid Greenman 
27392dc7331SDavid Greenman 			/* The block is waiting on something */
27492dc7331SDavid Greenman 			wproc = (struct proc *)block->lf_id;
275982d11f8SJeff Roberson restart:
276982d11f8SJeff Roberson 			nproc = NULL;
277982d11f8SJeff Roberson 			PROC_SLOCK(wproc);
278b40ce416SJulian Elischer 			FOREACH_THREAD_IN_PROC(wproc, td) {
279982d11f8SJeff Roberson 				thread_lock(td);
280b40ce416SJulian Elischer 				while (td->td_wchan &&
281b40ce416SJulian Elischer 				    (td->td_wmesg == lockstr) &&
28292dc7331SDavid Greenman 				    (i++ < maxlockdepth)) {
283b40ce416SJulian Elischer 					waitblock = (struct lockf *)td->td_wchan;
28492dc7331SDavid Greenman 					/* Get the owner of the blocking lock */
28592dc7331SDavid Greenman 					waitblock = waitblock->lf_next;
28692dc7331SDavid Greenman 					if ((waitblock->lf_flags & F_POSIX) == 0)
28792dc7331SDavid Greenman 						break;
288982d11f8SJeff Roberson 					nproc = (struct proc *)waitblock->lf_id;
289982d11f8SJeff Roberson 					if (nproc == (struct proc *)lock->lf_id) {
290982d11f8SJeff Roberson 						PROC_SUNLOCK(wproc);
291982d11f8SJeff Roberson 						thread_unlock(td);
29292dc7331SDavid Greenman 						free(lock, M_LOCKF);
29392dc7331SDavid Greenman 						return (EDEADLK);
29492dc7331SDavid Greenman 					}
29592dc7331SDavid Greenman 				}
296982d11f8SJeff Roberson 				thread_unlock(td);
297b40ce416SJulian Elischer 			}
298982d11f8SJeff Roberson 			PROC_SUNLOCK(wproc);
299982d11f8SJeff Roberson 			wproc = nproc;
300982d11f8SJeff Roberson 			if (wproc)
301982d11f8SJeff Roberson 				goto restart;
30292dc7331SDavid Greenman 		}
30392dc7331SDavid Greenman 		/*
30492dc7331SDavid Greenman 		 * For flock type locks, we must first remove
30592dc7331SDavid Greenman 		 * any shared locks that we hold before we sleep
30692dc7331SDavid Greenman 		 * waiting for an exclusive lock.
30792dc7331SDavid Greenman 		 */
30892dc7331SDavid Greenman 		if ((lock->lf_flags & F_FLOCK) &&
30992dc7331SDavid Greenman 		    lock->lf_type == F_WRLCK) {
31092dc7331SDavid Greenman 			lock->lf_type = F_UNLCK;
311bc02f1d9SJeff Roberson 			(void) lf_clearlock(lock, split);
31292dc7331SDavid Greenman 			lock->lf_type = F_WRLCK;
31392dc7331SDavid Greenman 		}
31492dc7331SDavid Greenman 		/*
31592dc7331SDavid Greenman 		 * Add our lock to the blocked list and sleep until we're free.
31692dc7331SDavid Greenman 		 * Remember who blocked us (for deadlock detection).
31792dc7331SDavid Greenman 		 */
31892dc7331SDavid Greenman 		lock->lf_next = block;
319996c772fSJohn Dyson 		TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
32092dc7331SDavid Greenman #ifdef LOCKF_DEBUG
32192dc7331SDavid Greenman 		if (lockf_debug & 1) {
32292dc7331SDavid Greenman 			lf_print("lf_setlock: blocking on", block);
32392dc7331SDavid Greenman 			lf_printlist("lf_setlock", block);
32492dc7331SDavid Greenman 		}
32592dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
326bc02f1d9SJeff Roberson 		error = msleep(lock, VI_MTX(vp), priority, lockstr, 0);
32792dc7331SDavid Greenman 		/*
3281168ab08SBruce Evans 		 * We may have been awakened by a signal and/or by a
3291168ab08SBruce Evans 		 * debugger continuing us (in which cases we must remove
3301168ab08SBruce Evans 		 * ourselves from the blocked list) and/or by another
3311168ab08SBruce Evans 		 * process releasing a lock (in which case we have
3321168ab08SBruce Evans 		 * already been removed from the blocked list and our
333996c772fSJohn Dyson 		 * lf_next field set to NOLOCKF).
33492dc7331SDavid Greenman 		 */
3351168ab08SBruce Evans 		if (lock->lf_next) {
3361168ab08SBruce Evans 			TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
3371168ab08SBruce Evans 			lock->lf_next = NOLOCKF;
3381168ab08SBruce Evans 		}
3391168ab08SBruce Evans 		if (error) {
34092dc7331SDavid Greenman 			free(lock, M_LOCKF);
34192dc7331SDavid Greenman 			return (error);
34292dc7331SDavid Greenman 		}
34392dc7331SDavid Greenman 	}
34492dc7331SDavid Greenman 	/*
34592dc7331SDavid Greenman 	 * No blocks!!  Add the lock.  Note that we will
34692dc7331SDavid Greenman 	 * downgrade or upgrade any overlapping locks this
34792dc7331SDavid Greenman 	 * process already owns.
34892dc7331SDavid Greenman 	 *
34992dc7331SDavid Greenman 	 * Skip over locks owned by other processes.
35092dc7331SDavid Greenman 	 * Handle any locks that overlap and are owned by ourselves.
35192dc7331SDavid Greenman 	 */
35292dc7331SDavid Greenman 	prev = head;
35392dc7331SDavid Greenman 	block = *head;
35492dc7331SDavid Greenman 	needtolink = 1;
35592dc7331SDavid Greenman 	for (;;) {
356bb56ec4aSPoul-Henning Kamp 		ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
357bb56ec4aSPoul-Henning Kamp 		if (ovcase)
35892dc7331SDavid Greenman 			block = overlap->lf_next;
35992dc7331SDavid Greenman 		/*
36092dc7331SDavid Greenman 		 * Six cases:
36192dc7331SDavid Greenman 		 *	0) no overlap
36292dc7331SDavid Greenman 		 *	1) overlap == lock
36392dc7331SDavid Greenman 		 *	2) overlap contains lock
36492dc7331SDavid Greenman 		 *	3) lock contains overlap
36592dc7331SDavid Greenman 		 *	4) overlap starts before lock
36692dc7331SDavid Greenman 		 *	5) overlap ends after lock
36792dc7331SDavid Greenman 		 */
36892dc7331SDavid Greenman 		switch (ovcase) {
36992dc7331SDavid Greenman 		case 0: /* no overlap */
37092dc7331SDavid Greenman 			if (needtolink) {
37192dc7331SDavid Greenman 				*prev = lock;
37292dc7331SDavid Greenman 				lock->lf_next = overlap;
37392dc7331SDavid Greenman 			}
37492dc7331SDavid Greenman 			break;
37592dc7331SDavid Greenman 
37692dc7331SDavid Greenman 		case 1: /* overlap == lock */
37792dc7331SDavid Greenman 			/*
37892dc7331SDavid Greenman 			 * If downgrading lock, others may be
37992dc7331SDavid Greenman 			 * able to acquire it.
38092dc7331SDavid Greenman 			 */
38192dc7331SDavid Greenman 			if (lock->lf_type == F_RDLCK &&
38292dc7331SDavid Greenman 			    overlap->lf_type == F_WRLCK)
38392dc7331SDavid Greenman 				lf_wakelock(overlap);
38492dc7331SDavid Greenman 			overlap->lf_type = lock->lf_type;
38592dc7331SDavid Greenman 			FREE(lock, M_LOCKF);
38692dc7331SDavid Greenman 			lock = overlap; /* for debug output below */
38792dc7331SDavid Greenman 			break;
38892dc7331SDavid Greenman 
38992dc7331SDavid Greenman 		case 2: /* overlap contains lock */
39092dc7331SDavid Greenman 			/*
39192dc7331SDavid Greenman 			 * Check for common starting point and different types.
39292dc7331SDavid Greenman 			 */
39392dc7331SDavid Greenman 			if (overlap->lf_type == lock->lf_type) {
39492dc7331SDavid Greenman 				free(lock, M_LOCKF);
39592dc7331SDavid Greenman 				lock = overlap; /* for debug output below */
39692dc7331SDavid Greenman 				break;
39792dc7331SDavid Greenman 			}
39892dc7331SDavid Greenman 			if (overlap->lf_start == lock->lf_start) {
39992dc7331SDavid Greenman 				*prev = lock;
40092dc7331SDavid Greenman 				lock->lf_next = overlap;
40192dc7331SDavid Greenman 				overlap->lf_start = lock->lf_end + 1;
40292dc7331SDavid Greenman 			} else
403bc02f1d9SJeff Roberson 				lf_split(overlap, lock, split);
40492dc7331SDavid Greenman 			lf_wakelock(overlap);
40592dc7331SDavid Greenman 			break;
40692dc7331SDavid Greenman 
40792dc7331SDavid Greenman 		case 3: /* lock contains overlap */
40892dc7331SDavid Greenman 			/*
40992dc7331SDavid Greenman 			 * If downgrading lock, others may be able to
41092dc7331SDavid Greenman 			 * acquire it, otherwise take the list.
41192dc7331SDavid Greenman 			 */
41292dc7331SDavid Greenman 			if (lock->lf_type == F_RDLCK &&
41392dc7331SDavid Greenman 			    overlap->lf_type == F_WRLCK) {
41492dc7331SDavid Greenman 				lf_wakelock(overlap);
41592dc7331SDavid Greenman 			} else {
4161b727751SPoul-Henning Kamp 				while (!TAILQ_EMPTY(&overlap->lf_blkhd)) {
4171b727751SPoul-Henning Kamp 					ltmp = TAILQ_FIRST(&overlap->lf_blkhd);
418996c772fSJohn Dyson 					TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
419996c772fSJohn Dyson 					    lf_block);
420996c772fSJohn Dyson 					TAILQ_INSERT_TAIL(&lock->lf_blkhd,
421996c772fSJohn Dyson 					    ltmp, lf_block);
422db72e058SDmitrij Tejblum 					ltmp->lf_next = lock;
423996c772fSJohn Dyson 				}
42492dc7331SDavid Greenman 			}
42592dc7331SDavid Greenman 			/*
42692dc7331SDavid Greenman 			 * Add the new lock if necessary and delete the overlap.
42792dc7331SDavid Greenman 			 */
42892dc7331SDavid Greenman 			if (needtolink) {
42992dc7331SDavid Greenman 				*prev = lock;
43092dc7331SDavid Greenman 				lock->lf_next = overlap->lf_next;
43192dc7331SDavid Greenman 				prev = &lock->lf_next;
43292dc7331SDavid Greenman 				needtolink = 0;
43392dc7331SDavid Greenman 			} else
43492dc7331SDavid Greenman 				*prev = overlap->lf_next;
43592dc7331SDavid Greenman 			free(overlap, M_LOCKF);
43692dc7331SDavid Greenman 			continue;
43792dc7331SDavid Greenman 
43892dc7331SDavid Greenman 		case 4: /* overlap starts before lock */
43992dc7331SDavid Greenman 			/*
44092dc7331SDavid Greenman 			 * Add lock after overlap on the list.
44192dc7331SDavid Greenman 			 */
44292dc7331SDavid Greenman 			lock->lf_next = overlap->lf_next;
44392dc7331SDavid Greenman 			overlap->lf_next = lock;
44492dc7331SDavid Greenman 			overlap->lf_end = lock->lf_start - 1;
44592dc7331SDavid Greenman 			prev = &lock->lf_next;
44692dc7331SDavid Greenman 			lf_wakelock(overlap);
44792dc7331SDavid Greenman 			needtolink = 0;
44892dc7331SDavid Greenman 			continue;
44992dc7331SDavid Greenman 
45092dc7331SDavid Greenman 		case 5: /* overlap ends after lock */
45192dc7331SDavid Greenman 			/*
45292dc7331SDavid Greenman 			 * Add the new lock before overlap.
45392dc7331SDavid Greenman 			 */
45492dc7331SDavid Greenman 			if (needtolink) {
45592dc7331SDavid Greenman 				*prev = lock;
45692dc7331SDavid Greenman 				lock->lf_next = overlap;
45792dc7331SDavid Greenman 			}
45892dc7331SDavid Greenman 			overlap->lf_start = lock->lf_end + 1;
45992dc7331SDavid Greenman 			lf_wakelock(overlap);
46092dc7331SDavid Greenman 			break;
46192dc7331SDavid Greenman 		}
46292dc7331SDavid Greenman 		break;
46392dc7331SDavid Greenman 	}
46492dc7331SDavid Greenman #ifdef LOCKF_DEBUG
46592dc7331SDavid Greenman 	if (lockf_debug & 1) {
46692dc7331SDavid Greenman 		lf_print("lf_setlock: got the lock", lock);
46792dc7331SDavid Greenman 		lf_printlist("lf_setlock", lock);
46892dc7331SDavid Greenman 	}
46992dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
47092dc7331SDavid Greenman 	return (0);
47192dc7331SDavid Greenman }
47292dc7331SDavid Greenman 
47392dc7331SDavid Greenman /*
47492dc7331SDavid Greenman  * Remove a byte-range lock on an inode.
47592dc7331SDavid Greenman  *
47692dc7331SDavid Greenman  * Generally, find the lock (or an overlap to that lock)
47792dc7331SDavid Greenman  * and remove it (or shrink it), then wakeup anyone we can.
47892dc7331SDavid Greenman  */
47987b6de2bSPoul-Henning Kamp static int
480bc02f1d9SJeff Roberson lf_clearlock(unlock, split)
481bc02f1d9SJeff Roberson 	struct lockf *unlock;
482bc02f1d9SJeff Roberson 	struct lockf **split;
48392dc7331SDavid Greenman {
48492dc7331SDavid Greenman 	struct lockf **head = unlock->lf_head;
48592dc7331SDavid Greenman 	register struct lockf *lf = *head;
48692dc7331SDavid Greenman 	struct lockf *overlap, **prev;
48792dc7331SDavid Greenman 	int ovcase;
48892dc7331SDavid Greenman 
48992dc7331SDavid Greenman 	if (lf == NOLOCKF)
49092dc7331SDavid Greenman 		return (0);
49192dc7331SDavid Greenman #ifdef LOCKF_DEBUG
49292dc7331SDavid Greenman 	if (unlock->lf_type != F_UNLCK)
49392dc7331SDavid Greenman 		panic("lf_clearlock: bad type");
49492dc7331SDavid Greenman 	if (lockf_debug & 1)
49592dc7331SDavid Greenman 		lf_print("lf_clearlock", unlock);
49692dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
49792dc7331SDavid Greenman 	prev = head;
498bb56ec4aSPoul-Henning Kamp 	while ((ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
49992dc7331SDavid Greenman 		/*
50092dc7331SDavid Greenman 		 * Wakeup the list of locks to be retried.
50192dc7331SDavid Greenman 		 */
50292dc7331SDavid Greenman 		lf_wakelock(overlap);
50392dc7331SDavid Greenman 
50492dc7331SDavid Greenman 		switch (ovcase) {
50592dc7331SDavid Greenman 
50692dc7331SDavid Greenman 		case 1: /* overlap == lock */
50792dc7331SDavid Greenman 			*prev = overlap->lf_next;
50892dc7331SDavid Greenman 			FREE(overlap, M_LOCKF);
50992dc7331SDavid Greenman 			break;
51092dc7331SDavid Greenman 
51192dc7331SDavid Greenman 		case 2: /* overlap contains lock: split it */
51292dc7331SDavid Greenman 			if (overlap->lf_start == unlock->lf_start) {
51392dc7331SDavid Greenman 				overlap->lf_start = unlock->lf_end + 1;
51492dc7331SDavid Greenman 				break;
51592dc7331SDavid Greenman 			}
516bc02f1d9SJeff Roberson 			lf_split(overlap, unlock, split);
51792dc7331SDavid Greenman 			overlap->lf_next = unlock->lf_next;
51892dc7331SDavid Greenman 			break;
51992dc7331SDavid Greenman 
52092dc7331SDavid Greenman 		case 3: /* lock contains overlap */
52192dc7331SDavid Greenman 			*prev = overlap->lf_next;
52292dc7331SDavid Greenman 			lf = overlap->lf_next;
52392dc7331SDavid Greenman 			free(overlap, M_LOCKF);
52492dc7331SDavid Greenman 			continue;
52592dc7331SDavid Greenman 
52692dc7331SDavid Greenman 		case 4: /* overlap starts before lock */
52792dc7331SDavid Greenman 			overlap->lf_end = unlock->lf_start - 1;
52892dc7331SDavid Greenman 			prev = &overlap->lf_next;
52992dc7331SDavid Greenman 			lf = overlap->lf_next;
53092dc7331SDavid Greenman 			continue;
53192dc7331SDavid Greenman 
53292dc7331SDavid Greenman 		case 5: /* overlap ends after lock */
53392dc7331SDavid Greenman 			overlap->lf_start = unlock->lf_end + 1;
53492dc7331SDavid Greenman 			break;
53592dc7331SDavid Greenman 		}
53692dc7331SDavid Greenman 		break;
53792dc7331SDavid Greenman 	}
53892dc7331SDavid Greenman #ifdef LOCKF_DEBUG
53992dc7331SDavid Greenman 	if (lockf_debug & 1)
54092dc7331SDavid Greenman 		lf_printlist("lf_clearlock", unlock);
54192dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
54292dc7331SDavid Greenman 	return (0);
54392dc7331SDavid Greenman }
54492dc7331SDavid Greenman 
54592dc7331SDavid Greenman /*
54692dc7331SDavid Greenman  * Check whether there is a blocking lock,
54792dc7331SDavid Greenman  * and if so return its process identifier.
54892dc7331SDavid Greenman  */
54987b6de2bSPoul-Henning Kamp static int
55092dc7331SDavid Greenman lf_getlock(lock, fl)
55192dc7331SDavid Greenman 	register struct lockf *lock;
55292dc7331SDavid Greenman 	register struct flock *fl;
55392dc7331SDavid Greenman {
55492dc7331SDavid Greenman 	register struct lockf *block;
55592dc7331SDavid Greenman 
55692dc7331SDavid Greenman #ifdef LOCKF_DEBUG
55792dc7331SDavid Greenman 	if (lockf_debug & 1)
55892dc7331SDavid Greenman 		lf_print("lf_getlock", lock);
55992dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
56092dc7331SDavid Greenman 
561bb56ec4aSPoul-Henning Kamp 	if ((block = lf_getblock(lock))) {
56292dc7331SDavid Greenman 		fl->l_type = block->lf_type;
56392dc7331SDavid Greenman 		fl->l_whence = SEEK_SET;
56492dc7331SDavid Greenman 		fl->l_start = block->lf_start;
56592dc7331SDavid Greenman 		if (block->lf_end == -1)
56692dc7331SDavid Greenman 			fl->l_len = 0;
56792dc7331SDavid Greenman 		else
56892dc7331SDavid Greenman 			fl->l_len = block->lf_end - block->lf_start + 1;
56992dc7331SDavid Greenman 		if (block->lf_flags & F_POSIX)
57092dc7331SDavid Greenman 			fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
57192dc7331SDavid Greenman 		else
57292dc7331SDavid Greenman 			fl->l_pid = -1;
57392dc7331SDavid Greenman 	} else {
57492dc7331SDavid Greenman 		fl->l_type = F_UNLCK;
57592dc7331SDavid Greenman 	}
57692dc7331SDavid Greenman 	return (0);
57792dc7331SDavid Greenman }
57892dc7331SDavid Greenman 
57992dc7331SDavid Greenman /*
58092dc7331SDavid Greenman  * Walk the list of locks for an inode and
58192dc7331SDavid Greenman  * return the first blocking lock.
58292dc7331SDavid Greenman  */
58387b6de2bSPoul-Henning Kamp static struct lockf *
58492dc7331SDavid Greenman lf_getblock(lock)
58592dc7331SDavid Greenman 	register struct lockf *lock;
58692dc7331SDavid Greenman {
58792dc7331SDavid Greenman 	struct lockf **prev, *overlap, *lf = *(lock->lf_head);
58892dc7331SDavid Greenman 	int ovcase;
58992dc7331SDavid Greenman 
59092dc7331SDavid Greenman 	prev = lock->lf_head;
591bb56ec4aSPoul-Henning Kamp 	while ((ovcase = lf_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
59292dc7331SDavid Greenman 		/*
59392dc7331SDavid Greenman 		 * We've found an overlap, see if it blocks us
59492dc7331SDavid Greenman 		 */
59592dc7331SDavid Greenman 		if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
59692dc7331SDavid Greenman 			return (overlap);
59792dc7331SDavid Greenman 		/*
59892dc7331SDavid Greenman 		 * Nope, point to the next one on the list and
59992dc7331SDavid Greenman 		 * see if it blocks us
60092dc7331SDavid Greenman 		 */
60192dc7331SDavid Greenman 		lf = overlap->lf_next;
60292dc7331SDavid Greenman 	}
60392dc7331SDavid Greenman 	return (NOLOCKF);
60492dc7331SDavid Greenman }
60592dc7331SDavid Greenman 
60692dc7331SDavid Greenman /*
60792dc7331SDavid Greenman  * Walk the list of locks for an inode to
60892dc7331SDavid Greenman  * find an overlapping lock (if any).
60992dc7331SDavid Greenman  *
61092dc7331SDavid Greenman  * NOTE: this returns only the FIRST overlapping lock.  There
61192dc7331SDavid Greenman  *	 may be more than one.
61292dc7331SDavid Greenman  */
61387b6de2bSPoul-Henning Kamp static int
61492dc7331SDavid Greenman lf_findoverlap(lf, lock, type, prev, overlap)
61592dc7331SDavid Greenman 	register struct lockf *lf;
61692dc7331SDavid Greenman 	struct lockf *lock;
61792dc7331SDavid Greenman 	int type;
61892dc7331SDavid Greenman 	struct lockf ***prev;
61992dc7331SDavid Greenman 	struct lockf **overlap;
62092dc7331SDavid Greenman {
62192dc7331SDavid Greenman 	off_t start, end;
62292dc7331SDavid Greenman 
62392dc7331SDavid Greenman 	*overlap = lf;
62492dc7331SDavid Greenman 	if (lf == NOLOCKF)
62592dc7331SDavid Greenman 		return (0);
62692dc7331SDavid Greenman #ifdef LOCKF_DEBUG
62792dc7331SDavid Greenman 	if (lockf_debug & 2)
62892dc7331SDavid Greenman 		lf_print("lf_findoverlap: looking for overlap in", lock);
62992dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
63092dc7331SDavid Greenman 	start = lock->lf_start;
63192dc7331SDavid Greenman 	end = lock->lf_end;
63292dc7331SDavid Greenman 	while (lf != NOLOCKF) {
63392dc7331SDavid Greenman 		if (((type & SELF) && lf->lf_id != lock->lf_id) ||
63492dc7331SDavid Greenman 		    ((type & OTHERS) && lf->lf_id == lock->lf_id)) {
63592dc7331SDavid Greenman 			*prev = &lf->lf_next;
63692dc7331SDavid Greenman 			*overlap = lf = lf->lf_next;
63792dc7331SDavid Greenman 			continue;
63892dc7331SDavid Greenman 		}
63992dc7331SDavid Greenman #ifdef LOCKF_DEBUG
64092dc7331SDavid Greenman 		if (lockf_debug & 2)
64192dc7331SDavid Greenman 			lf_print("\tchecking", lf);
64292dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
64392dc7331SDavid Greenman 		/*
64492dc7331SDavid Greenman 		 * OK, check for overlap
64592dc7331SDavid Greenman 		 *
64692dc7331SDavid Greenman 		 * Six cases:
64792dc7331SDavid Greenman 		 *	0) no overlap
64892dc7331SDavid Greenman 		 *	1) overlap == lock
64992dc7331SDavid Greenman 		 *	2) overlap contains lock
65092dc7331SDavid Greenman 		 *	3) lock contains overlap
65192dc7331SDavid Greenman 		 *	4) overlap starts before lock
65292dc7331SDavid Greenman 		 *	5) overlap ends after lock
65392dc7331SDavid Greenman 		 */
65492dc7331SDavid Greenman 		if ((lf->lf_end != -1 && start > lf->lf_end) ||
65592dc7331SDavid Greenman 		    (end != -1 && lf->lf_start > end)) {
65692dc7331SDavid Greenman 			/* Case 0 */
65792dc7331SDavid Greenman #ifdef LOCKF_DEBUG
65892dc7331SDavid Greenman 			if (lockf_debug & 2)
65992dc7331SDavid Greenman 				printf("no overlap\n");
66092dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
66192dc7331SDavid Greenman 			if ((type & SELF) && end != -1 && lf->lf_start > end)
66292dc7331SDavid Greenman 				return (0);
66392dc7331SDavid Greenman 			*prev = &lf->lf_next;
66492dc7331SDavid Greenman 			*overlap = lf = lf->lf_next;
66592dc7331SDavid Greenman 			continue;
66692dc7331SDavid Greenman 		}
66792dc7331SDavid Greenman 		if ((lf->lf_start == start) && (lf->lf_end == end)) {
66892dc7331SDavid Greenman 			/* Case 1 */
66992dc7331SDavid Greenman #ifdef LOCKF_DEBUG
67092dc7331SDavid Greenman 			if (lockf_debug & 2)
67192dc7331SDavid Greenman 				printf("overlap == lock\n");
67292dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
67392dc7331SDavid Greenman 			return (1);
67492dc7331SDavid Greenman 		}
67592dc7331SDavid Greenman 		if ((lf->lf_start <= start) &&
67692dc7331SDavid Greenman 		    (end != -1) &&
67792dc7331SDavid Greenman 		    ((lf->lf_end >= end) || (lf->lf_end == -1))) {
67892dc7331SDavid Greenman 			/* Case 2 */
67992dc7331SDavid Greenman #ifdef LOCKF_DEBUG
68092dc7331SDavid Greenman 			if (lockf_debug & 2)
68192dc7331SDavid Greenman 				printf("overlap contains lock\n");
68292dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
68392dc7331SDavid Greenman 			return (2);
68492dc7331SDavid Greenman 		}
68592dc7331SDavid Greenman 		if (start <= lf->lf_start &&
68692dc7331SDavid Greenman 		           (end == -1 ||
68792dc7331SDavid Greenman 			   (lf->lf_end != -1 && end >= lf->lf_end))) {
68892dc7331SDavid Greenman 			/* Case 3 */
68992dc7331SDavid Greenman #ifdef LOCKF_DEBUG
69092dc7331SDavid Greenman 			if (lockf_debug & 2)
69192dc7331SDavid Greenman 				printf("lock contains overlap\n");
69292dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
69392dc7331SDavid Greenman 			return (3);
69492dc7331SDavid Greenman 		}
69592dc7331SDavid Greenman 		if ((lf->lf_start < start) &&
69692dc7331SDavid Greenman 			((lf->lf_end >= start) || (lf->lf_end == -1))) {
69792dc7331SDavid Greenman 			/* Case 4 */
69892dc7331SDavid Greenman #ifdef LOCKF_DEBUG
69992dc7331SDavid Greenman 			if (lockf_debug & 2)
70092dc7331SDavid Greenman 				printf("overlap starts before lock\n");
70192dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
70292dc7331SDavid Greenman 			return (4);
70392dc7331SDavid Greenman 		}
70492dc7331SDavid Greenman 		if ((lf->lf_start > start) &&
70592dc7331SDavid Greenman 			(end != -1) &&
70692dc7331SDavid Greenman 			((lf->lf_end > end) || (lf->lf_end == -1))) {
70792dc7331SDavid Greenman 			/* Case 5 */
70892dc7331SDavid Greenman #ifdef LOCKF_DEBUG
70992dc7331SDavid Greenman 			if (lockf_debug & 2)
71092dc7331SDavid Greenman 				printf("overlap ends after lock\n");
71192dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
71292dc7331SDavid Greenman 			return (5);
71392dc7331SDavid Greenman 		}
71492dc7331SDavid Greenman 		panic("lf_findoverlap: default");
71592dc7331SDavid Greenman 	}
71692dc7331SDavid Greenman 	return (0);
71792dc7331SDavid Greenman }
71892dc7331SDavid Greenman 
71992dc7331SDavid Greenman /*
72092dc7331SDavid Greenman  * Split a lock and a contained region into
72192dc7331SDavid Greenman  * two or three locks as necessary.
72292dc7331SDavid Greenman  */
72387b6de2bSPoul-Henning Kamp static void
724bc02f1d9SJeff Roberson lf_split(lock1, lock2, split)
725bc02f1d9SJeff Roberson 	struct lockf *lock1;
726bc02f1d9SJeff Roberson 	struct lockf *lock2;
727bc02f1d9SJeff Roberson 	struct lockf **split;
72892dc7331SDavid Greenman {
729bc02f1d9SJeff Roberson 	struct lockf *splitlock;
73092dc7331SDavid Greenman 
73192dc7331SDavid Greenman #ifdef LOCKF_DEBUG
73292dc7331SDavid Greenman 	if (lockf_debug & 2) {
73392dc7331SDavid Greenman 		lf_print("lf_split", lock1);
73492dc7331SDavid Greenman 		lf_print("splitting from", lock2);
73592dc7331SDavid Greenman 	}
73692dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
73792dc7331SDavid Greenman 	/*
73892dc7331SDavid Greenman 	 * Check to see if spliting into only two pieces.
73992dc7331SDavid Greenman 	 */
74092dc7331SDavid Greenman 	if (lock1->lf_start == lock2->lf_start) {
74192dc7331SDavid Greenman 		lock1->lf_start = lock2->lf_end + 1;
74292dc7331SDavid Greenman 		lock2->lf_next = lock1;
74392dc7331SDavid Greenman 		return;
74492dc7331SDavid Greenman 	}
74592dc7331SDavid Greenman 	if (lock1->lf_end == lock2->lf_end) {
74692dc7331SDavid Greenman 		lock1->lf_end = lock2->lf_start - 1;
74792dc7331SDavid Greenman 		lock2->lf_next = lock1->lf_next;
74892dc7331SDavid Greenman 		lock1->lf_next = lock2;
74992dc7331SDavid Greenman 		return;
75092dc7331SDavid Greenman 	}
75192dc7331SDavid Greenman 	/*
75292dc7331SDavid Greenman 	 * Make a new lock consisting of the last part of
753bc02f1d9SJeff Roberson 	 * the encompassing lock.  We use the preallocated
754bc02f1d9SJeff Roberson 	 * splitlock so we don't have to block.
75592dc7331SDavid Greenman 	 */
756bc02f1d9SJeff Roberson 	splitlock = *split;
757bc02f1d9SJeff Roberson 	*split = NULL;
75880208239SAlfred Perlstein 	bcopy(lock1, splitlock, sizeof *splitlock);
75992dc7331SDavid Greenman 	splitlock->lf_start = lock2->lf_end + 1;
760996c772fSJohn Dyson 	TAILQ_INIT(&splitlock->lf_blkhd);
76192dc7331SDavid Greenman 	lock1->lf_end = lock2->lf_start - 1;
76292dc7331SDavid Greenman 	/*
76392dc7331SDavid Greenman 	 * OK, now link it in
76492dc7331SDavid Greenman 	 */
76592dc7331SDavid Greenman 	splitlock->lf_next = lock1->lf_next;
76692dc7331SDavid Greenman 	lock2->lf_next = splitlock;
76792dc7331SDavid Greenman 	lock1->lf_next = lock2;
76892dc7331SDavid Greenman }
76992dc7331SDavid Greenman 
77092dc7331SDavid Greenman /*
77192dc7331SDavid Greenman  * Wakeup a blocklist
77292dc7331SDavid Greenman  */
77387b6de2bSPoul-Henning Kamp static void
77492dc7331SDavid Greenman lf_wakelock(listhead)
77592dc7331SDavid Greenman 	struct lockf *listhead;
77692dc7331SDavid Greenman {
777996c772fSJohn Dyson 	register struct lockf *wakelock;
77892dc7331SDavid Greenman 
7791b727751SPoul-Henning Kamp 	while (!TAILQ_EMPTY(&listhead->lf_blkhd)) {
7801b727751SPoul-Henning Kamp 		wakelock = TAILQ_FIRST(&listhead->lf_blkhd);
781996c772fSJohn Dyson 		TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
78292dc7331SDavid Greenman 		wakelock->lf_next = NOLOCKF;
78392dc7331SDavid Greenman #ifdef LOCKF_DEBUG
78492dc7331SDavid Greenman 		if (lockf_debug & 2)
78592dc7331SDavid Greenman 			lf_print("lf_wakelock: awakening", wakelock);
78692dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
78780208239SAlfred Perlstein 		wakeup(wakelock);
78892dc7331SDavid Greenman 	}
78992dc7331SDavid Greenman }
79092dc7331SDavid Greenman 
79192dc7331SDavid Greenman #ifdef LOCKF_DEBUG
79292dc7331SDavid Greenman /*
79392dc7331SDavid Greenman  * Print out a lock.
79492dc7331SDavid Greenman  */
795013e6650SJeff Roberson static void
79692dc7331SDavid Greenman lf_print(tag, lock)
79792dc7331SDavid Greenman 	char *tag;
79892dc7331SDavid Greenman 	register struct lockf *lock;
79992dc7331SDavid Greenman {
80092dc7331SDavid Greenman 
801d974cf4dSBruce Evans 	printf("%s: lock %p for ", tag, (void *)lock);
80292dc7331SDavid Greenman 	if (lock->lf_flags & F_POSIX)
803d974cf4dSBruce Evans 		printf("proc %ld", (long)((struct proc *)lock->lf_id)->p_pid);
80492dc7331SDavid Greenman 	else
805d974cf4dSBruce Evans 		printf("id %p", (void *)lock->lf_id);
80659aff5fcSAlfred Perlstein 	if (lock->lf_inode != (struct inode *)0)
8077933351aSPoul-Henning Kamp 		printf(" in ino %ju on dev <%s>, %s, start %jd, end %jd",
808a7a00d05SMaxime Henrion 		    (uintmax_t)lock->lf_inode->i_number,
8097933351aSPoul-Henning Kamp 		    devtoname(lock->lf_inode->i_dev),
81092dc7331SDavid Greenman 		    lock->lf_type == F_RDLCK ? "shared" :
81192dc7331SDavid Greenman 		    lock->lf_type == F_WRLCK ? "exclusive" :
812a7a00d05SMaxime Henrion 		    lock->lf_type == F_UNLCK ? "unlock" : "unknown",
813a7a00d05SMaxime Henrion 		    (intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
81459aff5fcSAlfred Perlstein 	else
815a7a00d05SMaxime Henrion 		printf(" %s, start %jd, end %jd",
81659aff5fcSAlfred Perlstein 		    lock->lf_type == F_RDLCK ? "shared" :
81759aff5fcSAlfred Perlstein 		    lock->lf_type == F_WRLCK ? "exclusive" :
818a7a00d05SMaxime Henrion 		    lock->lf_type == F_UNLCK ? "unlock" : "unknown",
819a7a00d05SMaxime Henrion 		    (intmax_t)lock->lf_start, (intmax_t)lock->lf_end);
8201b727751SPoul-Henning Kamp 	if (!TAILQ_EMPTY(&lock->lf_blkhd))
8211b727751SPoul-Henning Kamp 		printf(" block %p\n", (void *)TAILQ_FIRST(&lock->lf_blkhd));
82292dc7331SDavid Greenman 	else
82392dc7331SDavid Greenman 		printf("\n");
82492dc7331SDavid Greenman }
82592dc7331SDavid Greenman 
826013e6650SJeff Roberson static void
82792dc7331SDavid Greenman lf_printlist(tag, lock)
82892dc7331SDavid Greenman 	char *tag;
82992dc7331SDavid Greenman 	struct lockf *lock;
83092dc7331SDavid Greenman {
831996c772fSJohn Dyson 	register struct lockf *lf, *blk;
83292dc7331SDavid Greenman 
83359aff5fcSAlfred Perlstein 	if (lock->lf_inode == (struct inode *)0)
83459aff5fcSAlfred Perlstein 		return;
83559aff5fcSAlfred Perlstein 
83697eb8cfaSPoul-Henning Kamp 	printf("%s: Lock list for ino %ju on dev <%s>:\n",
837a7a00d05SMaxime Henrion 	    tag, (uintmax_t)lock->lf_inode->i_number,
83897eb8cfaSPoul-Henning Kamp 	    devtoname(lock->lf_inode->i_dev));
83992dc7331SDavid Greenman 	for (lf = lock->lf_inode->i_lockf; lf; lf = lf->lf_next) {
840d974cf4dSBruce Evans 		printf("\tlock %p for ",(void *)lf);
84192dc7331SDavid Greenman 		if (lf->lf_flags & F_POSIX)
842d974cf4dSBruce Evans 			printf("proc %ld",
843d974cf4dSBruce Evans 			    (long)((struct proc *)lf->lf_id)->p_pid);
84492dc7331SDavid Greenman 		else
845d974cf4dSBruce Evans 			printf("id %p", (void *)lf->lf_id);
846a7a00d05SMaxime Henrion 		printf(", %s, start %jd, end %jd",
84792dc7331SDavid Greenman 		    lf->lf_type == F_RDLCK ? "shared" :
84892dc7331SDavid Greenman 		    lf->lf_type == F_WRLCK ? "exclusive" :
84992dc7331SDavid Greenman 		    lf->lf_type == F_UNLCK ? "unlock" :
850a7a00d05SMaxime Henrion 		    "unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
8511b727751SPoul-Henning Kamp 		TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
852d974cf4dSBruce Evans 			printf("\n\t\tlock request %p for ", (void *)blk);
853996c772fSJohn Dyson 			if (blk->lf_flags & F_POSIX)
854d974cf4dSBruce Evans 				printf("proc %ld",
855d974cf4dSBruce Evans 				    (long)((struct proc *)blk->lf_id)->p_pid);
85692dc7331SDavid Greenman 			else
857d974cf4dSBruce Evans 				printf("id %p", (void *)blk->lf_id);
858a7a00d05SMaxime Henrion 			printf(", %s, start %jd, end %jd",
859996c772fSJohn Dyson 			    blk->lf_type == F_RDLCK ? "shared" :
860996c772fSJohn Dyson 			    blk->lf_type == F_WRLCK ? "exclusive" :
861996c772fSJohn Dyson 			    blk->lf_type == F_UNLCK ? "unlock" :
862a7a00d05SMaxime Henrion 			    "unknown", (intmax_t)blk->lf_start,
863a7a00d05SMaxime Henrion 			    (intmax_t)blk->lf_end);
8641b727751SPoul-Henning Kamp 			if (!TAILQ_EMPTY(&blk->lf_blkhd))
865996c772fSJohn Dyson 				panic("lf_printlist: bad list");
866996c772fSJohn Dyson 		}
86792dc7331SDavid Greenman 		printf("\n");
86892dc7331SDavid Greenman 	}
86992dc7331SDavid Greenman }
87092dc7331SDavid Greenman #endif /* LOCKF_DEBUG */
871