2 * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
3 * Copyright (c) 2006-2018 Matthew Dillon <dillon@backplane.com>. All rights reserved.
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Scooter Morris at Genentech Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
36 * $FreeBSD: src/sys/kern/kern_lockf.c,v 1.25 1999/11/16 16:28:56 phk Exp $
39 #include "opt_debug_lockf.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
46 #include <sys/unistd.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/fcntl.h>
50 #include <sys/resourcevar.h>
52 #include <sys/lockf.h>
53 #include <machine/limits.h> /* for LLONG_MAX */
54 #include <machine/stdarg.h>
56 #include <sys/spinlock2.h>
59 int lf_global_counter
= 0;
63 int lf_print_ranges
= 0;
65 static void _lf_print_lock(const struct lockf
*);
66 static void _lf_printf(const char *, ...) __printflike(1, 2);
68 #define lf_print_lock(lock) if (lf_print_ranges) _lf_print_lock(lock)
69 #define lf_printf(ctl, args...) if (lf_print_ranges) _lf_printf(ctl, args)
71 #define lf_print_lock(lock)
72 #define lf_printf(ctl, args...)
75 static MALLOC_DEFINE(M_LOCKF
, "lockf", "Byte-range locking structures");
77 static void lf_wakeup(struct lockf
*, off_t
, off_t
);
78 static struct lockf_range
*lf_alloc_range(void);
79 static void lf_create_range(struct lockf_range
*, struct proc
*, int, int,
81 static void lf_insert(struct lockf_range_list
*list
,
82 struct lockf_range
*elm
,
83 struct lockf_range
*insert_point
);
84 static void lf_destroy_range(struct lockf_range
*);
86 static int lf_setlock(struct lockf
*, struct proc
*, int, int,
88 static int lf_getlock(struct flock
*, struct lockf
*, struct proc
*,
89 int, int, off_t
, off_t
);
91 static int lf_count_change(struct proc
*, int);
94 * Return TRUE (non-zero) if the type and posix flags match.
98 lf_match(struct lockf_range
*range
, int type
, int flags
)
100 if (range
->lf_type
!= type
)
102 if ((range
->lf_flags
^ flags
) & F_POSIX
)
108 * Check whether range and [start, end] overlap.
112 lf_overlap(const struct lockf_range
*range
, off_t start
, off_t end
)
114 if (range
->lf_start
>= start
&& range
->lf_start
<= end
)
116 else if (start
>= range
->lf_start
&& start
<= range
->lf_end
)
124 * Change the POSIX lock accounting for the given process.
127 lf_count_adjust(struct proc
*p
, int increase
)
130 struct uidcount
*pup
;
135 uip
= p
->p_ucred
->cr_uidinfo
;
136 pup
= &uip
->ui_pcpu
[mycpuid
];
139 for (n
= 0; n
< ncpus
; ++n
)
140 pup
->pu_posixlocks
+= p
->p_uidpcpu
[n
].pu_posixlocks
;
142 for (n
= 0; n
< ncpus
; ++n
)
143 pup
->pu_posixlocks
-= p
->p_uidpcpu
[n
].pu_posixlocks
;
146 if (pup
->pu_posixlocks
< -PUP_LIMIT
||
147 pup
->pu_posixlocks
> PUP_LIMIT
) {
148 atomic_add_int(&uip
->ui_posixlocks
, pup
->pu_posixlocks
);
149 pup
->pu_posixlocks
= 0;
154 lf_count_change(struct proc
*owner
, int diff
)
159 /* we might actually not have a process context */
163 uip
= owner
->p_ucred
->cr_uidinfo
;
165 max
= MIN(owner
->p_rlimit
[RLIMIT_POSIXLOCKS
].rlim_cur
,
166 maxposixlocksperuid
);
168 if (diff
> 0 && owner
->p_ucred
->cr_uid
!= 0 && max
!= -1 &&
169 uip
->ui_posixlocks
>= max
) {
172 struct uidcount
*pup
;
175 pup
= &uip
->ui_pcpu
[cpu
];
176 pup
->pu_posixlocks
+= diff
;
177 if (pup
->pu_posixlocks
< -PUP_LIMIT
||
178 pup
->pu_posixlocks
> PUP_LIMIT
) {
179 atomic_add_int(&uip
->ui_posixlocks
, pup
->pu_posixlocks
);
180 pup
->pu_posixlocks
= 0;
182 owner
->p_uidpcpu
[cpu
].pu_posixlocks
+= diff
;
189 * Advisory record locking support
192 lf_advlock(struct vop_advlock_args
*ap
, struct lockf
*lock
, u_quad_t size
)
194 struct flock
*fl
= ap
->a_fl
;
197 int type
, flags
, error
;
201 * Convert the flock structure into a start and end.
203 switch (fl
->l_whence
) {
207 * Caller is responsible for adding any necessary offset
208 * when SEEK_CUR is used.
214 start
= size
+ fl
->l_start
;
224 if (fl
->l_len
== 0) {
227 } else if (fl
->l_len
< 0) {
230 end
= start
+ fl
->l_len
- 1;
237 * This isn't really correct for flock-style locks,
238 * but the current handling is somewhat broken anyway.
240 owner
= (struct proc
*)ap
->a_id
;
243 * Do the requested operation.
245 token
= lwkt_getpooltoken(lock
);
247 if (lock
->init_done
== 0) {
248 TAILQ_INIT(&lock
->lf_range
);
249 TAILQ_INIT(&lock
->lf_blocked
);
256 * NOTE: It is possible for both lf_range and lf_blocked to
257 * be empty if we block and get woken up, but another process
258 * then gets in and issues an unlock. So VMAYHAVELOCKS must
259 * be set after the lf_setlock() operation completes rather
262 error
= lf_setlock(lock
, owner
, type
, flags
, start
, end
);
263 vsetflags(ap
->a_vp
, VMAYHAVELOCKS
);
267 error
= lf_setlock(lock
, owner
, type
, flags
, start
, end
);
268 if (TAILQ_EMPTY(&lock
->lf_range
) &&
269 TAILQ_EMPTY(&lock
->lf_blocked
)) {
270 vclrflags(ap
->a_vp
, VMAYHAVELOCKS
);
275 error
= lf_getlock(fl
, lock
, owner
, type
, flags
, start
, end
);
282 lwkt_reltoken(token
);
287 lf_setlock(struct lockf
*lock
, struct proc
*owner
, int type
, int flags
,
288 off_t start
, off_t end
)
290 struct lockf_range
*range
;
291 struct lockf_range
*brange
;
292 struct lockf_range
*next
;
293 struct lockf_range
*first_match
;
294 struct lockf_range
*last_match
;
295 struct lockf_range
*insert_point
;
296 struct lockf_range
*new_range1
;
297 struct lockf_range
*new_range2
;
303 struct lockf_range_list deadlist
;
311 * Preallocate two ranges so we don't have to worry about blocking
312 * in the middle of the lock code.
314 if (new_range1
== NULL
)
315 new_range1
= lf_alloc_range();
316 if (new_range2
== NULL
)
317 new_range2
= lf_alloc_range();
326 * Locate the insertion point for the new lock (the first range
327 * with an lf_start >= start).
329 * Locate the first and latch ranges owned by us that overlap
330 * the requested range.
332 TAILQ_FOREACH(range
, &lock
->lf_range
, lf_link
) {
333 if (insert_point
== NULL
&& range
->lf_start
>= start
)
334 insert_point
= range
;
337 * Skip non-overlapping locks. Locks are sorted by lf_start
338 * So we can terminate the search when lf_start exceeds the
339 * requested range (insert_point is still guarenteed to be
342 if (range
->lf_end
< start
)
344 if (range
->lf_start
> end
) {
350 * Overlapping lock. Set first_match and last_match if we
353 if (range
->lf_owner
== owner
) {
354 if (first_match
== NULL
)
361 * If we aren't the owner check for a conflicting lock. Only
364 if (type
!= F_UNLCK
) {
365 if (type
== F_WRLCK
|| range
->lf_type
== F_WRLCK
)
371 * If a conflicting lock was observed, block or fail as appropriate.
372 * (this code is skipped when unlocking)
375 if ((flags
& F_WAIT
) == 0) {
381 * We are blocked. For POSIX locks we have to check
382 * for deadlocks and return with EDEADLK. This is done
383 * by checking whether range->lf_owner is already
386 * Since flock-style locks cover the whole file, a
387 * deadlock between those is nearly impossible.
388 * This can only occur if a process tries to lock the
389 * same inode exclusively while holding a shared lock
390 * with another descriptor.
391 * XXX How can we cleanly detect this?
392 * XXX The current mixing of flock & fcntl/lockf is evil.
394 * Handle existing locks of flock-style like POSIX locks.
396 if (flags
& F_POSIX
) {
397 TAILQ_FOREACH(brange
, &lock
->lf_blocked
, lf_link
) {
398 if (brange
->lf_owner
== range
->lf_owner
) {
406 * For flock-style locks, we must first remove
407 * any shared locks that we hold before we sleep
408 * waiting for an exclusive lock.
410 if ((flags
& F_POSIX
) == 0 && type
== F_WRLCK
)
411 lf_setlock(lock
, owner
, F_UNLCK
, 0, start
, end
);
415 lf_create_range(brange
, owner
, type
, 0, start
, end
);
416 TAILQ_INSERT_TAIL(&lock
->lf_blocked
, brange
, lf_link
);
417 error
= tsleep(brange
, PCATCH
, "lockf", 0);
420 * We may have been awaked by a signal and/or by a
421 * debugger continuing us (in which case we must remove
422 * ourselves from the blocked list) and/or by another
423 * process releasing/downgrading a lock (in which case
424 * we have already been removed from the blocked list
425 * and our lf_flags field is 1).
427 * Sleep if it looks like we might be livelocking.
429 if (brange
->lf_flags
== 0)
430 TAILQ_REMOVE(&lock
->lf_blocked
, brange
, lf_link
);
432 tsleep(brange
, 0, "lockfz", 2);
435 lf_destroy_range(brange
);
443 * If there are no overlapping locks owned by us then creating
444 * the new lock is easy. This is the most common case.
446 if (first_match
== NULL
) {
449 if (flags
& F_POSIX
) {
450 if (lf_count_change(owner
, 1)) {
457 lf_create_range(range
, owner
, type
, flags
, start
, end
);
458 lf_insert(&lock
->lf_range
, range
, insert_point
);
463 * double_clip - Calculate a special case where TWO locks may have
464 * to be added due to the new lock breaking up an
465 * existing incompatible lock in the middle.
467 * unlock_override - Calculate a special case where NO locks
468 * need to be created. This occurs when an unlock
469 * does not clip any locks at the front and rear.
471 * WARNING! closef() and fdrop() assume that an F_UNLCK of the
472 * entire range will always succeed so the unlock_override
477 if (first_match
->lf_start
< start
) {
478 if (first_match
== last_match
&& last_match
->lf_end
> end
)
480 } else if (type
== F_UNLCK
&& last_match
->lf_end
<= end
) {
485 * Figure out the worst case net increase in POSIX locks and account
486 * for it now before we start modifying things. If neither the
487 * first or last locks match we have an issue. If there is only
488 * one overlapping range which needs to be clipped on both ends
489 * we wind up having to create up to two new locks, else only one.
491 * When unlocking the worst case is always 1 new lock if our
492 * unlock request cuts the middle out of an existing lock range.
494 * count represents the 'cleanup' adjustment needed. It starts
495 * negative, is incremented whenever we create a new POSIX lock,
496 * and decremented whenever we delete an existing one. At the
497 * end of the day it had better be <= 0 or we didn't calculate the
498 * worse case properly here.
501 if ((flags
& F_POSIX
) && !unlock_override
) {
502 if (!lf_match(first_match
, type
, flags
) &&
503 !lf_match(last_match
, type
, flags
)
505 if (double_clip
&& type
!= F_UNLCK
)
510 if (count
&& lf_count_change(owner
, -count
)) {
515 /* else flock style lock which encompasses entire range */
518 * Create and insert the lock represented the requested range.
519 * Adjust the net POSIX lock count. We have to move our insertion
520 * point since brange now represents the first record >= start.
522 * When unlocking, no new lock is inserted but we still clip.
524 if (type
!= F_UNLCK
) {
527 lf_create_range(brange
, owner
, type
, flags
, start
, end
);
528 lf_insert(&lock
->lf_range
, brange
, insert_point
);
529 insert_point
= brange
;
537 * Handle the double_clip case. This is the only case where
538 * we wind up having to add TWO locks.
541 KKASSERT(first_match
== last_match
);
542 last_match
= new_range2
;
544 lf_create_range(last_match
, first_match
->lf_owner
,
545 first_match
->lf_type
, first_match
->lf_flags
,
546 end
+ 1, first_match
->lf_end
);
547 first_match
->lf_end
= start
- 1;
548 first_match
->lf_flags
&= ~F_NOEND
;
551 * Figure out where to insert the right side clip.
553 lf_insert(&lock
->lf_range
, last_match
, first_match
);
554 if (last_match
->lf_flags
& F_POSIX
)
559 * Clip or destroy the locks between first_match and last_match,
560 * inclusive. Ignore the primary lock we created (brange). Note
561 * that if double-clipped, first_match and last_match will be
562 * outside our clipping range. Otherwise first_match and last_match
565 * We have already taken care of any double clipping.
567 * The insert_point may become invalid as we delete records, do not
568 * use that pointer any more. Also, when removing something other
569 * then 'range' we have to check to see if the item we are removing
570 * is 'next' and adjust 'next' properly.
572 * NOTE: brange will be NULL if F_UNLCKing.
574 TAILQ_INIT(&deadlist
);
577 while ((range
= next
) != NULL
) {
578 next
= TAILQ_NEXT(range
, lf_link
);
581 * Ignore elements that we do not own and ignore the
582 * primary request range which we just created.
584 if (range
->lf_owner
!= owner
|| range
== brange
)
588 * We may have to wakeup a waiter when downgrading a lock.
592 if (type
== F_RDLCK
&& range
->lf_type
== F_WRLCK
)
596 * Clip left. This can only occur on first_match.
598 * Merge the left clip with brange if possible. This must
599 * be done specifically, not in the optimized merge heuristic
600 * below, since we may have counted on it in our 'count'
603 if (range
->lf_start
< start
) {
604 KKASSERT(range
== first_match
);
606 range
->lf_end
>= start
- 1 &&
607 lf_match(range
, type
, flags
)) {
608 range
->lf_end
= brange
->lf_end
;
609 range
->lf_flags
|= brange
->lf_flags
& F_NOEND
;
611 * Removing something other then 'range',
612 * adjust 'next' if necessary.
615 next
= TAILQ_NEXT(next
, lf_link
);
616 TAILQ_REMOVE(&lock
->lf_range
, brange
, lf_link
);
617 if (brange
->lf_flags
& F_POSIX
)
619 TAILQ_INSERT_TAIL(&deadlist
, brange
, lf_link
);
621 } else if (range
->lf_end
>= start
) {
622 range
->lf_end
= start
- 1;
624 range
->lf_flags
&= ~F_NOEND
;
626 if (range
== last_match
)
632 * Clip right. This can only occur on last_match.
634 * Merge the right clip if possible. This must be done
635 * specifically, not in the optimized merge heuristic
636 * below, since we may have counted on it in our 'count'
639 * Since we are adjusting lf_start, we have to move the
640 * record to maintain the sorted list. Since lf_start is
641 * only getting larger we can use the next element as the
642 * insert point (we don't have to backtrack).
644 if (range
->lf_end
> end
) {
645 KKASSERT(range
== last_match
);
647 range
->lf_start
<= end
+ 1 &&
648 lf_match(range
, type
, flags
)) {
649 brange
->lf_end
= range
->lf_end
;
650 brange
->lf_flags
|= range
->lf_flags
& F_NOEND
;
651 TAILQ_REMOVE(&lock
->lf_range
, range
, lf_link
);
652 if (range
->lf_flags
& F_POSIX
)
654 TAILQ_INSERT_TAIL(&deadlist
, range
, lf_link
);
655 } else if (range
->lf_start
<= end
) {
656 range
->lf_start
= end
+ 1;
657 TAILQ_REMOVE(&lock
->lf_range
, range
, lf_link
);
658 lf_insert(&lock
->lf_range
, range
, next
);
660 /* range == last_match, we are done */
665 * The record must be entirely enclosed. Note that the
666 * record could be first_match or last_match, and will be
669 KKASSERT(range
->lf_start
>= start
&& range
->lf_end
<= end
);
670 TAILQ_REMOVE(&lock
->lf_range
, range
, lf_link
);
671 if (range
->lf_flags
& F_POSIX
)
673 TAILQ_INSERT_TAIL(&deadlist
, range
, lf_link
);
674 if (range
== last_match
)
679 * Attempt to merge locks adjacent to brange. For example, we may
680 * have had to clip first_match and/or last_match, and they might
681 * be adjacent. Or there might simply have been an adjacent lock
684 * Don't get fancy, just check adjacent elements in the list if they
685 * happen to be owned by us.
687 * This case only gets hit if we have a situation where a shared
688 * and exclusive lock are adjacent, and the exclusive lock is
689 * downgraded to shared or the shared lock is upgraded to exclusive.
692 range
= TAILQ_PREV(brange
, lockf_range_list
, lf_link
);
694 range
->lf_owner
== owner
&&
695 range
->lf_end
== brange
->lf_start
- 1 &&
696 lf_match(range
, type
, flags
)
699 * Extend range to cover brange and scrap brange.
701 range
->lf_end
= brange
->lf_end
;
702 range
->lf_flags
|= brange
->lf_flags
& F_NOEND
;
703 TAILQ_REMOVE(&lock
->lf_range
, brange
, lf_link
);
704 if (brange
->lf_flags
& F_POSIX
)
706 TAILQ_INSERT_TAIL(&deadlist
, brange
, lf_link
);
709 range
= TAILQ_NEXT(brange
, lf_link
);
711 range
->lf_owner
== owner
&&
712 range
->lf_start
== brange
->lf_end
+ 1 &&
713 lf_match(range
, type
, flags
)
716 * Extend brange to cover range and scrap range.
718 brange
->lf_end
= range
->lf_end
;
719 brange
->lf_flags
|= range
->lf_flags
& F_NOEND
;
720 TAILQ_REMOVE(&lock
->lf_range
, range
, lf_link
);
721 if (range
->lf_flags
& F_POSIX
)
723 TAILQ_INSERT_TAIL(&deadlist
, range
, lf_link
);
728 * Destroy deleted elements. We didn't want to do it in the loop
729 * because the free() might have blocked.
731 * Adjust the count for any posix locks we thought we might create
734 while ((range
= TAILQ_FIRST(&deadlist
)) != NULL
) {
735 TAILQ_REMOVE(&deadlist
, range
, lf_link
);
736 lf_destroy_range(range
);
739 KKASSERT(count
<= 0);
741 lf_count_change(owner
, count
);
745 lf_wakeup(lock
, start
, end
);
748 if (new_range1
!= NULL
)
749 lf_destroy_range(new_range1
);
750 if (new_range2
!= NULL
)
751 lf_destroy_range(new_range2
);
756 * Check whether there is a blocking lock,
757 * and if so return its process identifier.
760 lf_getlock(struct flock
*fl
, struct lockf
*lock
, struct proc
*owner
,
761 int type
, int flags
, off_t start
, off_t end
)
763 struct lockf_range
*range
;
765 TAILQ_FOREACH(range
, &lock
->lf_range
, lf_link
)
766 if (range
->lf_owner
!= owner
&&
767 lf_overlap(range
, start
, end
) &&
768 (type
== F_WRLCK
|| range
->lf_type
== F_WRLCK
))
771 fl
->l_type
= F_UNLCK
;
774 fl
->l_type
= range
->lf_type
;
775 fl
->l_whence
= SEEK_SET
;
776 fl
->l_start
= range
->lf_start
;
777 if (range
->lf_flags
& F_NOEND
)
780 fl
->l_len
= range
->lf_end
- range
->lf_start
+ 1;
781 if (range
->lf_owner
!= NULL
&& (range
->lf_flags
& F_POSIX
))
782 fl
->l_pid
= range
->lf_owner
->p_pid
;
789 * Wakeup pending lock attempts. Theoretically we can stop as soon as
790 * we encounter an exclusive request that covers the whole range (at least
791 * insofar as the sleep code above calls lf_wakeup() if it would otherwise
792 * exit instead of loop), but for now just wakeup all overlapping
796 lf_wakeup(struct lockf
*lock
, off_t start
, off_t end
)
798 struct lockf_range
*range
, *nrange
;
800 TAILQ_FOREACH_MUTABLE(range
, &lock
->lf_blocked
, lf_link
, nrange
) {
801 if (lf_overlap(range
, start
, end
) == 0)
803 TAILQ_REMOVE(&lock
->lf_blocked
, range
, lf_link
);
810 * Allocate a range structure and initialize it sufficiently such that
811 * lf_destroy_range() does not barf.
813 static struct lockf_range
*
816 struct lockf_range
*range
;
819 atomic_add_int(&lf_global_counter
, 1);
821 range
= kmalloc(sizeof(struct lockf_range
), M_LOCKF
, M_WAITOK
);
822 range
->lf_owner
= NULL
;
827 lf_insert(struct lockf_range_list
*list
, struct lockf_range
*elm
,
828 struct lockf_range
*insert_point
)
830 while (insert_point
&& insert_point
->lf_start
< elm
->lf_start
)
831 insert_point
= TAILQ_NEXT(insert_point
, lf_link
);
832 if (insert_point
!= NULL
)
833 TAILQ_INSERT_BEFORE(insert_point
, elm
, lf_link
);
835 TAILQ_INSERT_TAIL(list
, elm
, lf_link
);
839 lf_create_range(struct lockf_range
*range
, struct proc
*owner
, int type
,
840 int flags
, off_t start
, off_t end
)
842 KKASSERT(start
<= end
);
843 range
->lf_type
= type
;
844 range
->lf_flags
= flags
;
845 range
->lf_start
= start
;
847 range
->lf_owner
= owner
;
849 lf_printf("lf_create_range: %ju..%ju\n",
850 (uintmax_t)range
->lf_start
, (uintmax_t)range
->lf_end
);
854 lf_destroy_range(struct lockf_range
*range
)
856 lf_printf("lf_destroy_range: %ju..%ju\n",
857 (uintmax_t)range
->lf_start
, (uintmax_t)range
->lf_end
);
858 kfree(range
, M_LOCKF
);
860 atomic_add_int(&lf_global_counter
, -1);
861 KKASSERT(lf_global_counter
>= 0);
868 _lf_printf(const char *ctl
, ...)
873 if (lf_print_ranges
) {
874 if ((p
= curproc
) != NULL
)
875 kprintf("pid %d (%s): ", p
->p_pid
, p
->p_comm
);
883 _lf_print_lock(const struct lockf
*lock
)
885 struct lockf_range
*range
;
887 if (lf_print_ranges
== 0)
890 if (TAILQ_EMPTY(&lock
->lf_range
)) {
891 lf_printf("lockf %p: no ranges locked\n", lock
);
893 lf_printf("lockf %p:\n", lock
);
895 TAILQ_FOREACH(range
, &lock
->lf_range
, lf_link
)
896 kprintf("\t%jd..%jd type %s owned by %d\n",
897 (uintmax_t)range
->lf_start
, (uintmax_t)range
->lf_end
,
898 range
->lf_type
== F_RDLCK
? "shared" : "exclusive",
899 range
->lf_flags
& F_POSIX
? range
->lf_owner
->p_pid
: -1);
900 if (TAILQ_EMPTY(&lock
->lf_blocked
))
901 kprintf("no process waiting for range\n");
903 kprintf("blocked locks:");
904 TAILQ_FOREACH(range
, &lock
->lf_blocked
, lf_link
)
905 kprintf("\t%jd..%jd type %s waiting on %p\n",
906 (uintmax_t)range
->lf_start
, (uintmax_t)range
->lf_end
,
907 range
->lf_type
== F_RDLCK
? "shared" : "exclusive",
910 #endif /* LOCKF_DEBUG */