kern_lockf.c revision 41059
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.19 1998/07/29 17:38:14 bde 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 */
58static 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
68static int	lockf_debug = 0;
69SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
70#endif
71
72static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
73
74#define NOLOCKF (struct lockf *)0
75#define SELF	0x1
76#define OTHERS	0x2
77static int	 lf_clearlock __P((struct lockf *));
78static int	 lf_findoverlap __P((struct lockf *,
79	    struct lockf *, int, struct lockf ***, struct lockf **));
80static struct lockf *
81	 lf_getblock __P((struct lockf *));
82static int	 lf_getlock __P((struct lockf *, struct flock *));
83static int	 lf_setlock __P((struct lockf *));
84static void	 lf_split __P((struct lockf *, struct lockf *));
85static void	 lf_wakelock __P((struct lockf *));
86
87/*
88 * Advisory record locking support
89 */
90int
91lf_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 */
186static int
187lf_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) {
364					TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
365					    lf_block);
366					TAILQ_INSERT_TAIL(&lock->lf_blkhd,
367					    ltmp, lf_block);
368				}
369			}
370			/*
371			 * Add the new lock if necessary and delete the overlap.
372			 */
373			if (needtolink) {
374				*prev = lock;
375				lock->lf_next = overlap->lf_next;
376				prev = &lock->lf_next;
377				needtolink = 0;
378			} else
379				*prev = overlap->lf_next;
380			free(overlap, M_LOCKF);
381			continue;
382
383		case 4: /* overlap starts before lock */
384			/*
385			 * Add lock after overlap on the list.
386			 */
387			lock->lf_next = overlap->lf_next;
388			overlap->lf_next = lock;
389			overlap->lf_end = lock->lf_start - 1;
390			prev = &lock->lf_next;
391			lf_wakelock(overlap);
392			needtolink = 0;
393			continue;
394
395		case 5: /* overlap ends after lock */
396			/*
397			 * Add the new lock before overlap.
398			 */
399			if (needtolink) {
400				*prev = lock;
401				lock->lf_next = overlap;
402			}
403			overlap->lf_start = lock->lf_end + 1;
404			lf_wakelock(overlap);
405			break;
406		}
407		break;
408	}
409#ifdef LOCKF_DEBUG
410	if (lockf_debug & 1) {
411		lf_print("lf_setlock: got the lock", lock);
412		lf_printlist("lf_setlock", lock);
413	}
414#endif /* LOCKF_DEBUG */
415	return (0);
416}
417
418/*
419 * Remove a byte-range lock on an inode.
420 *
421 * Generally, find the lock (or an overlap to that lock)
422 * and remove it (or shrink it), then wakeup anyone we can.
423 */
424static int
425lf_clearlock(unlock)
426	register struct lockf *unlock;
427{
428	struct lockf **head = unlock->lf_head;
429	register struct lockf *lf = *head;
430	struct lockf *overlap, **prev;
431	int ovcase;
432
433	if (lf == NOLOCKF)
434		return (0);
435#ifdef LOCKF_DEBUG
436	if (unlock->lf_type != F_UNLCK)
437		panic("lf_clearlock: bad type");
438	if (lockf_debug & 1)
439		lf_print("lf_clearlock", unlock);
440#endif /* LOCKF_DEBUG */
441	prev = head;
442	while ((ovcase = lf_findoverlap(lf, unlock, SELF, &prev, &overlap))) {
443		/*
444		 * Wakeup the list of locks to be retried.
445		 */
446		lf_wakelock(overlap);
447
448		switch (ovcase) {
449
450		case 1: /* overlap == lock */
451			*prev = overlap->lf_next;
452			FREE(overlap, M_LOCKF);
453			break;
454
455		case 2: /* overlap contains lock: split it */
456			if (overlap->lf_start == unlock->lf_start) {
457				overlap->lf_start = unlock->lf_end + 1;
458				break;
459			}
460			lf_split(overlap, unlock);
461			overlap->lf_next = unlock->lf_next;
462			break;
463
464		case 3: /* lock contains overlap */
465			*prev = overlap->lf_next;
466			lf = overlap->lf_next;
467			free(overlap, M_LOCKF);
468			continue;
469
470		case 4: /* overlap starts before lock */
471			overlap->lf_end = unlock->lf_start - 1;
472			prev = &overlap->lf_next;
473			lf = overlap->lf_next;
474			continue;
475
476		case 5: /* overlap ends after lock */
477			overlap->lf_start = unlock->lf_end + 1;
478			break;
479		}
480		break;
481	}
482#ifdef LOCKF_DEBUG
483	if (lockf_debug & 1)
484		lf_printlist("lf_clearlock", unlock);
485#endif /* LOCKF_DEBUG */
486	return (0);
487}
488
489/*
490 * Check whether there is a blocking lock,
491 * and if so return its process identifier.
492 */
493static int
494lf_getlock(lock, fl)
495	register struct lockf *lock;
496	register struct flock *fl;
497{
498	register struct lockf *block;
499
500#ifdef LOCKF_DEBUG
501	if (lockf_debug & 1)
502		lf_print("lf_getlock", lock);
503#endif /* LOCKF_DEBUG */
504
505	if ((block = lf_getblock(lock))) {
506		fl->l_type = block->lf_type;
507		fl->l_whence = SEEK_SET;
508		fl->l_start = block->lf_start;
509		if (block->lf_end == -1)
510			fl->l_len = 0;
511		else
512			fl->l_len = block->lf_end - block->lf_start + 1;
513		if (block->lf_flags & F_POSIX)
514			fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
515		else
516			fl->l_pid = -1;
517	} else {
518		fl->l_type = F_UNLCK;
519	}
520	return (0);
521}
522
523/*
524 * Walk the list of locks for an inode and
525 * return the first blocking lock.
526 */
527static struct lockf *
528lf_getblock(lock)
529	register struct lockf *lock;
530{
531	struct lockf **prev, *overlap, *lf = *(lock->lf_head);
532	int ovcase;
533
534	prev = lock->lf_head;
535	while ((ovcase = lf_findoverlap(lf, lock, OTHERS, &prev, &overlap))) {
536		/*
537		 * We've found an overlap, see if it blocks us
538		 */
539		if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
540			return (overlap);
541		/*
542		 * Nope, point to the next one on the list and
543		 * see if it blocks us
544		 */
545		lf = overlap->lf_next;
546	}
547	return (NOLOCKF);
548}
549
550/*
551 * Walk the list of locks for an inode to
552 * find an overlapping lock (if any).
553 *
554 * NOTE: this returns only the FIRST overlapping lock.  There
555 *	 may be more than one.
556 */
557static int
558lf_findoverlap(lf, lock, type, prev, overlap)
559	register struct lockf *lf;
560	struct lockf *lock;
561	int type;
562	struct lockf ***prev;
563	struct lockf **overlap;
564{
565	off_t start, end;
566
567	*overlap = lf;
568	if (lf == NOLOCKF)
569		return (0);
570#ifdef LOCKF_DEBUG
571	if (lockf_debug & 2)
572		lf_print("lf_findoverlap: looking for overlap in", lock);
573#endif /* LOCKF_DEBUG */
574	start = lock->lf_start;
575	end = lock->lf_end;
576	while (lf != NOLOCKF) {
577		if (((type & SELF) && lf->lf_id != lock->lf_id) ||
578		    ((type & OTHERS) && lf->lf_id == lock->lf_id)) {
579			*prev = &lf->lf_next;
580			*overlap = lf = lf->lf_next;
581			continue;
582		}
583#ifdef LOCKF_DEBUG
584		if (lockf_debug & 2)
585			lf_print("\tchecking", lf);
586#endif /* LOCKF_DEBUG */
587		/*
588		 * OK, check for overlap
589		 *
590		 * Six cases:
591		 *	0) no overlap
592		 *	1) overlap == lock
593		 *	2) overlap contains lock
594		 *	3) lock contains overlap
595		 *	4) overlap starts before lock
596		 *	5) overlap ends after lock
597		 */
598		if ((lf->lf_end != -1 && start > lf->lf_end) ||
599		    (end != -1 && lf->lf_start > end)) {
600			/* Case 0 */
601#ifdef LOCKF_DEBUG
602			if (lockf_debug & 2)
603				printf("no overlap\n");
604#endif /* LOCKF_DEBUG */
605			if ((type & SELF) && end != -1 && lf->lf_start > end)
606				return (0);
607			*prev = &lf->lf_next;
608			*overlap = lf = lf->lf_next;
609			continue;
610		}
611		if ((lf->lf_start == start) && (lf->lf_end == end)) {
612			/* Case 1 */
613#ifdef LOCKF_DEBUG
614			if (lockf_debug & 2)
615				printf("overlap == lock\n");
616#endif /* LOCKF_DEBUG */
617			return (1);
618		}
619		if ((lf->lf_start <= start) &&
620		    (end != -1) &&
621		    ((lf->lf_end >= end) || (lf->lf_end == -1))) {
622			/* Case 2 */
623#ifdef LOCKF_DEBUG
624			if (lockf_debug & 2)
625				printf("overlap contains lock\n");
626#endif /* LOCKF_DEBUG */
627			return (2);
628		}
629		if (start <= lf->lf_start &&
630		           (end == -1 ||
631			   (lf->lf_end != -1 && end >= lf->lf_end))) {
632			/* Case 3 */
633#ifdef LOCKF_DEBUG
634			if (lockf_debug & 2)
635				printf("lock contains overlap\n");
636#endif /* LOCKF_DEBUG */
637			return (3);
638		}
639		if ((lf->lf_start < start) &&
640			((lf->lf_end >= start) || (lf->lf_end == -1))) {
641			/* Case 4 */
642#ifdef LOCKF_DEBUG
643			if (lockf_debug & 2)
644				printf("overlap starts before lock\n");
645#endif /* LOCKF_DEBUG */
646			return (4);
647		}
648		if ((lf->lf_start > start) &&
649			(end != -1) &&
650			((lf->lf_end > end) || (lf->lf_end == -1))) {
651			/* Case 5 */
652#ifdef LOCKF_DEBUG
653			if (lockf_debug & 2)
654				printf("overlap ends after lock\n");
655#endif /* LOCKF_DEBUG */
656			return (5);
657		}
658		panic("lf_findoverlap: default");
659	}
660	return (0);
661}
662
663/*
664 * Split a lock and a contained region into
665 * two or three locks as necessary.
666 */
667static void
668lf_split(lock1, lock2)
669	register struct lockf *lock1;
670	register struct lockf *lock2;
671{
672	register struct lockf *splitlock;
673
674#ifdef LOCKF_DEBUG
675	if (lockf_debug & 2) {
676		lf_print("lf_split", lock1);
677		lf_print("splitting from", lock2);
678	}
679#endif /* LOCKF_DEBUG */
680	/*
681	 * Check to see if spliting into only two pieces.
682	 */
683	if (lock1->lf_start == lock2->lf_start) {
684		lock1->lf_start = lock2->lf_end + 1;
685		lock2->lf_next = lock1;
686		return;
687	}
688	if (lock1->lf_end == lock2->lf_end) {
689		lock1->lf_end = lock2->lf_start - 1;
690		lock2->lf_next = lock1->lf_next;
691		lock1->lf_next = lock2;
692		return;
693	}
694	/*
695	 * Make a new lock consisting of the last part of
696	 * the encompassing lock
697	 */
698	MALLOC(splitlock, struct lockf *, sizeof *splitlock, M_LOCKF, M_WAITOK);
699	bcopy((caddr_t)lock1, (caddr_t)splitlock, sizeof *splitlock);
700	splitlock->lf_start = lock2->lf_end + 1;
701	TAILQ_INIT(&splitlock->lf_blkhd);
702	lock1->lf_end = lock2->lf_start - 1;
703	/*
704	 * OK, now link it in
705	 */
706	splitlock->lf_next = lock1->lf_next;
707	lock2->lf_next = splitlock;
708	lock1->lf_next = lock2;
709}
710
711/*
712 * Wakeup a blocklist
713 */
714static void
715lf_wakelock(listhead)
716	struct lockf *listhead;
717{
718	register struct lockf *wakelock;
719
720	while (wakelock = listhead->lf_blkhd.tqh_first) {
721		TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
722		wakelock->lf_next = NOLOCKF;
723#ifdef LOCKF_DEBUG
724		if (lockf_debug & 2)
725			lf_print("lf_wakelock: awakening", wakelock);
726#endif /* LOCKF_DEBUG */
727		wakeup((caddr_t)wakelock);
728	}
729}
730
731#ifdef LOCKF_DEBUG
732/*
733 * Print out a lock.
734 */
735void
736lf_print(tag, lock)
737	char *tag;
738	register struct lockf *lock;
739{
740
741	printf("%s: lock %p for ", tag, (void *)lock);
742	if (lock->lf_flags & F_POSIX)
743		printf("proc %ld", (long)((struct proc *)lock->lf_id)->p_pid);
744	else
745		printf("id %p", (void *)lock->lf_id);
746	/* XXX no %qd in kernel.  Truncate. */
747	printf(" in ino %lu on dev <%d, %d>, %s, start %ld, end %ld",
748	    (u_long)lock->lf_inode->i_number,
749	    major(lock->lf_inode->i_dev),
750	    minor(lock->lf_inode->i_dev),
751	    lock->lf_type == F_RDLCK ? "shared" :
752	    lock->lf_type == F_WRLCK ? "exclusive" :
753	    lock->lf_type == F_UNLCK ? "unlock" :
754	    "unknown", (long)lock->lf_start, (long)lock->lf_end);
755	if (lock->lf_blkhd.tqh_first)
756		printf(" block %p\n", (void *)lock->lf_blkhd.tqh_first);
757	else
758		printf("\n");
759}
760
761void
762lf_printlist(tag, lock)
763	char *tag;
764	struct lockf *lock;
765{
766	register struct lockf *lf, *blk;
767
768	printf("%s: Lock list for ino %lu on dev <%d, %d>:\n",
769	    tag, (u_long)lock->lf_inode->i_number,
770	    major(lock->lf_inode->i_dev),
771	    minor(lock->lf_inode->i_dev));
772	for (lf = lock->lf_inode->i_lockf; lf; lf = lf->lf_next) {
773		printf("\tlock %p for ",(void *)lf);
774		if (lf->lf_flags & F_POSIX)
775			printf("proc %ld",
776			    (long)((struct proc *)lf->lf_id)->p_pid);
777		else
778			printf("id %p", (void *)lf->lf_id);
779		/* XXX no %qd in kernel.  Truncate. */
780		printf(", %s, start %ld, end %ld",
781		    lf->lf_type == F_RDLCK ? "shared" :
782		    lf->lf_type == F_WRLCK ? "exclusive" :
783		    lf->lf_type == F_UNLCK ? "unlock" :
784		    "unknown", (long)lf->lf_start, (long)lf->lf_end);
785		for (blk = lf->lf_blkhd.tqh_first; blk;
786		     blk = blk->lf_block.tqe_next) {
787			printf("\n\t\tlock request %p for ", (void *)blk);
788			if (blk->lf_flags & F_POSIX)
789				printf("proc %ld",
790				    (long)((struct proc *)blk->lf_id)->p_pid);
791			else
792				printf("id %p", (void *)blk->lf_id);
793			/* XXX no %qd in kernel.  Truncate. */
794			printf(", %s, start %ld, end %ld",
795			    blk->lf_type == F_RDLCK ? "shared" :
796			    blk->lf_type == F_WRLCK ? "exclusive" :
797			    blk->lf_type == F_UNLCK ? "unlock" :
798			    "unknown", (long)blk->lf_start,
799			    (long)blk->lf_end);
800			if (blk->lf_blkhd.tqh_first)
801				panic("lf_printlist: bad list");
802		}
803		printf("\n");
804	}
805}
806#endif /* LOCKF_DEBUG */
807