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