2 * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from software contributed to Berkeley by
33 * Scooter Morris at Genentech Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
62 #include <sys/cdefs.h>
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
67 #include <sys/mount.h>
69 #include <sys/signalvar.h>
70 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/vnode_if.h>
75 #include <sys/malloc.h>
76 #include <sys/fcntl.h>
77 #include <sys/lockf.h>
79 #include <kern/policy_internal.h>
81 #include <sys/file_internal.h>
83 #if (DEVELOPMENT || DEBUG)
84 #define LOCKF_DEBUGGING 1
87 #ifdef LOCKF_DEBUGGING
88 #include <sys/sysctl.h>
89 void lf_print(const char *tag
, struct lockf
*lock
);
90 void lf_printlist(const char *tag
, struct lockf
*lock
);
92 #define LF_DBG_LOCKOP (1 << 0) /* setlk, getlk, clearlk */
93 #define LF_DBG_LIST (1 << 1) /* split, coalesce */
94 #define LF_DBG_IMPINH (1 << 2) /* importance inheritance */
95 #define LF_DBG_TRACE (1 << 3) /* errors, exit */
96 #define LF_DBG_DEADLOCK (1 << 4) /* deadlock detection */
98 static int lockf_debug
= 0; /* was 2, could be 3 ;-) */
99 SYSCTL_INT(_debug
, OID_AUTO
, lockf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lockf_debug
, 0, "");
102 * If there is no mask bit selector, or there is one, and the selector is
103 * set, then output the debugging diagnostic.
105 #define LOCKF_DEBUG(mask, ...) \
107 if (!(mask) || ((mask) & lockf_debug)) { \
108 printf("%s>", __FUNCTION__); \
109 printf(__VA_ARGS__); \
113 #define LOCKF_DEBUGP(mask) \
115 ((mask) & lockf_debug); \
117 #else /* !LOCKF_DEBUGGING */
118 #define LOCKF_DEBUG(mask, ...) /* mask */
119 #endif /* !LOCKF_DEBUGGING */
121 MALLOC_DEFINE(M_LOCKF
, "lockf", "Byte-range locking structures");
123 #define NOLOCKF (struct lockf *)0
126 #define OFF_MAX 0x7fffffffffffffffULL /* max off_t */
129 * Overlapping lock states
134 OVERLAP_CONTAINS_LOCK
,
135 OVERLAP_CONTAINED_BY_LOCK
,
136 OVERLAP_STARTS_BEFORE_LOCK
,
137 OVERLAP_ENDS_AFTER_LOCK
140 static int lf_clearlock(struct lockf
*);
141 static overlap_t
lf_findoverlap(struct lockf
*,
142 struct lockf
*, int, struct lockf
***, struct lockf
**);
143 static struct lockf
*lf_getblock(struct lockf
*, pid_t
);
144 static int lf_getlock(struct lockf
*, struct flock
*, pid_t
);
145 static int lf_setlock(struct lockf
*, struct timespec
*);
146 static int lf_split(struct lockf
*, struct lockf
*);
147 static void lf_wakelock(struct lockf
*, boolean_t
);
148 #if IMPORTANCE_INHERITANCE
149 static void lf_hold_assertion(task_t
, struct lockf
*);
150 static void lf_jump_to_queue_head(struct lockf
*, struct lockf
*);
151 static void lf_drop_assertion(struct lockf
*);
152 static void lf_boost_blocking_proc(struct lockf
*, struct lockf
*);
153 static void lf_adjust_assertion(struct lockf
*block
);
154 #endif /* IMPORTANCE_INHERITANCE */
159 * Description: Advisory record locking support
161 * Parameters: ap Argument pointer to a vnop_advlock_args
162 * argument descriptor structure for the
163 * lock operation to be attempted.
168 * ENOLCK Number of locked regions exceeds limit
173 * lf_setlock:ETIMEDOUT
174 * lf_clearlock:ENOLCK
177 * Notes: We return ENOLCK when we run out of memory to support locks; as
178 * such, there is no specific expectation limit other than the
179 * amount of available resources.
182 lf_advlock(struct vnop_advlock_args
*ap
)
184 struct vnode
*vp
= ap
->a_vp
;
185 struct flock
*fl
= ap
->a_fl
;
186 vfs_context_t context
= ap
->a_context
;
188 off_t start
, end
, oadd
;
191 struct lockf
**head
= &vp
->v_lockf
;
193 /* XXX HFS may need a !vnode_isreg(vp) EISDIR error here */
196 * Avoid the common case of unlocking when inode has no locks.
198 if (*head
== (struct lockf
*)0) {
199 if (ap
->a_op
!= F_SETLK
) {
200 fl
->l_type
= F_UNLCK
;
201 LOCKF_DEBUG(LF_DBG_TRACE
,
202 "lf_advlock: '%s' unlock without lock\n",
203 vfs_context_proc(context
)->p_comm
);
209 * Convert the flock structure into a start and end.
211 switch (fl
->l_whence
) {
215 * Caller is responsible for adding any necessary offset
216 * when SEEK_CUR is used.
224 * It's OK to cast the u_quad_t to and off_t here, since they
225 * are the same storage size, and the value of the returned
226 * contents will never overflow into the sign bit. We need to
227 * do this because we will use size to force range checks.
229 if ((error
= vnode_size(vp
, (off_t
*)&size
, context
))) {
230 LOCKF_DEBUG(LF_DBG_TRACE
,
231 "lf_advlock: vnode_getattr failed: %d\n", error
);
235 if (size
> OFF_MAX
||
237 size
> (u_quad_t
)(OFF_MAX
- fl
->l_start
))) {
240 start
= size
+ fl
->l_start
;
244 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: unknown whence %d\n",
249 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: start < 0 (%qd)\n",
255 LOCKF_DEBUG(LF_DBG_TRACE
,
256 "lf_advlock: len < 0 & start == 0\n");
262 LOCKF_DEBUG(LF_DBG_TRACE
,
263 "lf_advlock: start < 0 (%qd)\n", start
);
266 } else if (fl
->l_len
== 0) {
269 oadd
= fl
->l_len
- 1;
270 if (oadd
> (off_t
)(OFF_MAX
- start
)) {
271 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: overflow\n");
277 * Create the lockf structure
279 MALLOC(lock
, struct lockf
*, sizeof *lock
, M_LOCKF
, M_WAITOK
);
283 lock
->lf_start
= start
;
285 lock
->lf_id
= ap
->a_id
;
287 lock
->lf_type
= fl
->l_type
;
288 lock
->lf_head
= head
;
289 lock
->lf_next
= (struct lockf
*)0;
290 TAILQ_INIT(&lock
->lf_blkhd
);
291 lock
->lf_flags
= ap
->a_flags
;
292 #if IMPORTANCE_INHERITANCE
293 lock
->lf_boosted
= LF_NOT_BOOSTED
;
295 if (ap
->a_flags
& F_POSIX
) {
296 lock
->lf_owner
= (struct proc
*)lock
->lf_id
;
298 lock
->lf_owner
= NULL
;
301 if (ap
->a_flags
& F_FLOCK
) {
302 lock
->lf_flags
|= F_WAKE1_SAFE
;
305 lck_mtx_lock(&vp
->v_lock
); /* protect the lockf list */
307 * Do the requested operation.
312 * For F_OFD_* locks, lf_id is the fileglob.
313 * Record an "lf_owner" iff this is a confined fd
314 * i.e. it cannot escape this process and will be
315 * F_UNLCKed before the owner exits. (This is
316 * the implicit guarantee needed to ensure lf_owner
317 * remains a valid reference here.)
319 if (ap
->a_flags
& F_OFD_LOCK
) {
320 struct fileglob
*fg
= (void *)lock
->lf_id
;
321 if (fg
->fg_lflags
& FG_CONFINED
) {
322 lock
->lf_owner
= current_proc();
325 error
= lf_setlock(lock
, ap
->a_timeout
);
329 error
= lf_clearlock(lock
);
334 error
= lf_getlock(lock
, fl
, -1);
339 error
= lf_getlock(lock
, fl
, fl
->l_pid
);
348 lck_mtx_unlock(&vp
->v_lock
); /* done manipulating the list */
350 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: normal exit: %d\n", error
);
355 * Empty the queue of msleeping requests for a lock on the given vnode.
356 * Called with the vnode already locked. Used for forced unmount, where
357 * a flock(2) invoker sleeping on a blocked lock holds an iocount reference
358 * that prevents the vnode from ever being drained. Force unmounting wins.
361 lf_abort_advlocks(vnode_t vp
)
365 if ((lock
= vp
->v_lockf
) == NULL
) {
369 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
371 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
374 TAILQ_FOREACH(tlock
, &lock
->lf_blkhd
, lf_block
) {
376 * Setting this flag should cause all
377 * currently blocked F_SETLK request to
378 * return to userland with an errno.
380 tlock
->lf_flags
|= F_ABORT
;
382 lf_wakelock(lock
, TRUE
);
387 * Take any lock attempts which are currently blocked by a given lock ("from")
388 * and mark them as blocked by a different lock ("to"). Used in the case
389 * where a byte range currently occupied by "from" is to be occupied by "to."
392 lf_move_blocked(struct lockf
*to
, struct lockf
*from
)
396 TAILQ_FOREACH(tlock
, &from
->lf_blkhd
, lf_block
) {
400 TAILQ_CONCAT(&to
->lf_blkhd
, &from
->lf_blkhd
, lf_block
);
404 * lf_coalesce_adjacent
406 * Description: Helper function: when setting a lock, coalesce adjacent
407 * locks. Needed because adjacent locks are not overlapping,
408 * but POSIX requires that they be coalesced.
410 * Parameters: lock The new lock which may be adjacent
411 * to already locked regions, and which
412 * should therefore be coalesced with them
417 lf_coalesce_adjacent(struct lockf
*lock
)
419 struct lockf
**lf
= lock
->lf_head
;
421 while (*lf
!= NOLOCKF
) {
422 /* reject locks that obviously could not be coalesced */
424 ((*lf
)->lf_id
!= lock
->lf_id
) ||
425 ((*lf
)->lf_type
!= lock
->lf_type
)) {
426 lf
= &(*lf
)->lf_next
;
431 * NOTE: Assumes that if two locks are adjacent on the number line
432 * and belong to the same owner, then they are adjacent on the list.
434 if ((*lf
)->lf_end
!= -1 &&
435 ((*lf
)->lf_end
+ 1) == lock
->lf_start
) {
436 struct lockf
*adjacent
= *lf
;
438 LOCKF_DEBUG(LF_DBG_LIST
, "lf_coalesce_adjacent: coalesce adjacent previous\n");
439 lock
->lf_start
= (*lf
)->lf_start
;
441 lf
= &(*lf
)->lf_next
;
443 lf_move_blocked(lock
, adjacent
);
445 FREE(adjacent
, M_LOCKF
);
448 /* If the lock starts adjacent to us, we can coalesce it */
449 if (lock
->lf_end
!= -1 &&
450 (lock
->lf_end
+ 1) == (*lf
)->lf_start
) {
451 struct lockf
*adjacent
= *lf
;
453 LOCKF_DEBUG(LF_DBG_LIST
, "lf_coalesce_adjacent: coalesce adjacent following\n");
454 lock
->lf_end
= (*lf
)->lf_end
;
455 lock
->lf_next
= (*lf
)->lf_next
;
458 lf_move_blocked(lock
, adjacent
);
460 FREE(adjacent
, M_LOCKF
);
464 /* no matching conditions; go on to next lock */
465 lf
= &(*lf
)->lf_next
;
472 * Description: Set a byte-range lock.
474 * Parameters: lock The lock structure describing the lock
475 * to be set; allocated by the caller, it
476 * will be linked into the lock list if
477 * the set is successful, and freed if the
478 * set is unsuccessful.
480 * timeout Timeout specified in the case of
487 * lf_clearlock:ENOLCK
491 * Notes: We add the lock to the provisional lock list. We do not
492 * coalesce at this time; this has implications for other lock
493 * requestors in the blocker search mechanism.
496 lf_setlock(struct lockf
*lock
, struct timespec
*timeout
)
499 struct lockf
**head
= lock
->lf_head
;
500 struct lockf
**prev
, *overlap
, *ltmp
;
501 static char lockstr
[] = "lockf";
502 int priority
, needtolink
, error
;
503 struct vnode
*vp
= lock
->lf_vnode
;
506 #ifdef LOCKF_DEBUGGING
507 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
508 lf_print("lf_setlock", lock
);
509 lf_printlist("lf_setlock(in)", lock
);
511 #endif /* LOCKF_DEBUGGING */
512 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p Looking for deadlock, vnode %p\n", lock
, lock
->lf_vnode
);
518 if (lock
->lf_type
== F_WRLCK
) {
524 * Scan lock list for this file looking for locks that would block us.
526 while ((block
= lf_getblock(lock
, -1))) {
528 * Free the structure and return if nonblocking.
530 if ((lock
->lf_flags
& F_WAIT
) == 0) {
531 DTRACE_FSINFO(advlock__nowait
, vnode_t
, vp
);
536 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p found blocking lock %p\n", lock
, block
);
539 * We are blocked. Since flock style locks cover
540 * the whole file, there is no chance for deadlock.
542 * OFD byte-range locks currently do NOT support
543 * deadlock detection.
545 * For POSIX byte-range locks we must check for deadlock.
547 * Deadlock detection is done by looking through the
548 * wait channels to see if there are any cycles that
551 if ((lock
->lf_flags
& F_POSIX
) &&
552 (block
->lf_flags
& F_POSIX
)) {
556 /* The block is waiting on something */
557 wproc
= block
->lf_owner
;
559 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p owned by pid %d\n", lock
, proc_pid(wproc
));
560 TAILQ_FOREACH(ut
, &wproc
->p_uthlist
, uu_list
) {
562 * If the thread is asleep (uu_wchan != 0)
563 * in this code (uu_wmesg == lockstr)
564 * check to see if the lock is blocked behind
565 * someone blocked behind us.
567 if ((ut
->uu_wchan
!= NULL
) && (ut
->uu_wmesg
== lockstr
)) {
568 struct lockf
*waitblock
= (struct lockf
*)ut
->uu_wchan
;
569 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p which is also blocked on lock %p vnode %p\n", lock
, waitblock
, waitblock
->lf_vnode
);
571 vnode_t othervp
= NULL
;
572 if (waitblock
->lf_vnode
!= vp
) {
574 * This thread in wproc is waiting for a lock
575 * on a different vnode; grab the lock on it
576 * that protects lf_next while we examine it.
578 othervp
= waitblock
->lf_vnode
;
579 if (!lck_mtx_try_lock(&othervp
->v_lock
)) {
581 * avoid kernel deadlock: drop all
582 * locks, pause for a bit to let the
583 * other thread do what it needs to do,
584 * then (because we drop and retake
585 * v_lock) retry the scan.
588 static struct timespec ts
= {
590 .tv_nsec
= 10 * NSEC_PER_MSEC
,
592 (void) msleep(lock
, &vp
->v_lock
, priority
, lockstr
, &ts
);
593 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p contention for vp %p => restart\n", lock
, othervp
);
599 * Get the lock blocking the lock
600 * which would block us, and make
601 * certain it hasn't become unblocked
602 * (been granted, e.g. between the time
603 * we called lf_getblock, and the time
604 * we successfully acquired the
607 waitblock
= waitblock
->lf_next
;
608 if (waitblock
== NULL
) {
610 lck_mtx_unlock(&othervp
->v_lock
);
612 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p with no lf_next\n", lock
);
615 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p which is also blocked on lock %p vnode %p\n", lock
, waitblock
, waitblock
->lf_vnode
);
618 * Make sure it's an advisory range
619 * lock and not any other kind of lock;
620 * if we mix lock types, it's our own
623 if ((waitblock
->lf_flags
& F_POSIX
) == 0) {
625 lck_mtx_unlock(&othervp
->v_lock
);
631 * If the owner of the lock that's
632 * blocking a lock that's blocking us
633 * getting the requested lock, then we
634 * would deadlock, so error out.
636 struct proc
*bproc
= waitblock
->lf_owner
;
637 const boolean_t deadlocked
= bproc
== lock
->lf_owner
;
640 lck_mtx_unlock(&othervp
->v_lock
);
642 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p owned by pid %d\n", lock
, proc_pid(bproc
));
644 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p which is me, so EDEADLK\n", lock
);
650 LOCKF_DEBUG(LF_DBG_DEADLOCK
, "lock %p bottom of thread loop\n", lock
);
656 * For flock type locks, we must first remove
657 * any shared locks that we hold before we sleep
658 * waiting for an exclusive lock.
660 if ((lock
->lf_flags
& F_FLOCK
) &&
661 lock
->lf_type
== F_WRLCK
) {
662 lock
->lf_type
= F_UNLCK
;
663 if ((error
= lf_clearlock(lock
)) != 0) {
667 lock
->lf_type
= F_WRLCK
;
670 * Add our lock to the blocked list and sleep until we're free.
671 * Remember who blocked us (for deadlock detection).
673 lock
->lf_next
= block
;
674 TAILQ_INSERT_TAIL(&block
->lf_blkhd
, lock
, lf_block
);
676 if (!(lock
->lf_flags
& F_FLOCK
)) {
677 block
->lf_flags
&= ~F_WAKE1_SAFE
;
680 #if IMPORTANCE_INHERITANCE
682 * Importance donation is done only for cases where the
683 * owning task can be unambiguously determined.
685 * POSIX type locks are not inherited by child processes;
686 * we maintain a 1:1 mapping between a lock and its owning
689 * Flock type locks are inherited across fork() and there is
690 * no 1:1 mapping in the general case. However, the fileglobs
691 * used by OFD locks *may* be confined to the process that
692 * created them, and thus have an "owner", in which case
693 * we also attempt importance donation.
695 if ((lock
->lf_flags
& block
->lf_flags
& F_POSIX
) != 0) {
696 lf_boost_blocking_proc(lock
, block
);
697 } else if ((lock
->lf_flags
& block
->lf_flags
& F_OFD_LOCK
) &&
698 lock
->lf_owner
!= block
->lf_owner
&&
699 NULL
!= lock
->lf_owner
&& NULL
!= block
->lf_owner
) {
700 lf_boost_blocking_proc(lock
, block
);
702 #endif /* IMPORTANCE_INHERITANCE */
704 #ifdef LOCKF_DEBUGGING
705 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
706 lf_print("lf_setlock: blocking on", block
);
707 lf_printlist("lf_setlock(block)", block
);
709 #endif /* LOCKF_DEBUGGING */
710 DTRACE_FSINFO(advlock__wait
, vnode_t
, vp
);
712 error
= msleep(lock
, &vp
->v_lock
, priority
, lockstr
, timeout
);
714 if (error
== 0 && (lock
->lf_flags
& F_ABORT
) != 0) {
720 * lf_wakelock() always sets wakelock->lf_next to
721 * NULL before a wakeup; so we've been woken early
722 * - perhaps by a debugger, signal or other event.
724 * Remove 'lock' from the block list (avoids double-add
725 * in the spurious case, which would create a cycle)
727 TAILQ_REMOVE(&lock
->lf_next
->lf_blkhd
, lock
, lf_block
);
728 #if IMPORTANCE_INHERITANCE
730 * Adjust the boost on lf_next.
732 lf_adjust_assertion(lock
->lf_next
);
733 #endif /* IMPORTANCE_INHERITANCE */
734 lock
->lf_next
= NULL
;
738 * If this was a spurious wakeup, retry
740 printf("%s: spurious wakeup, retrying lock\n",
746 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
747 if ((block
= lf_getblock(lock
, -1)) != NULL
) {
748 lf_move_blocked(block
, lock
);
753 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
754 lf_wakelock(lock
, TRUE
);
757 /* Return ETIMEDOUT if timeout occoured. */
758 if (error
== EWOULDBLOCK
) {
766 * No blocks!! Add the lock. Note that we will
767 * downgrade or upgrade any overlapping locks this
768 * process already owns.
770 * Skip over locks owned by other processes.
771 * Handle any locks that overlap and are owned by ourselves.
777 ovcase
= lf_findoverlap(block
, lock
, SELF
, &prev
, &overlap
);
779 block
= overlap
->lf_next
;
785 * 2) overlap contains lock
786 * 3) lock contains overlap
787 * 4) overlap starts before lock
788 * 5) overlap ends after lock
794 lock
->lf_next
= overlap
;
798 case OVERLAP_EQUALS_LOCK
:
800 * If downgrading lock, others may be
801 * able to acquire it.
803 if (lock
->lf_type
== F_RDLCK
&&
804 overlap
->lf_type
== F_WRLCK
) {
805 lf_wakelock(overlap
, TRUE
);
807 overlap
->lf_type
= lock
->lf_type
;
809 lock
= overlap
; /* for lf_coalesce_adjacent() */
812 case OVERLAP_CONTAINS_LOCK
:
814 * Check for common starting point and different types.
816 if (overlap
->lf_type
== lock
->lf_type
) {
818 lock
= overlap
; /* for lf_coalesce_adjacent() */
821 if (overlap
->lf_start
== lock
->lf_start
) {
823 lock
->lf_next
= overlap
;
824 overlap
->lf_start
= lock
->lf_end
+ 1;
827 * If we can't split the lock, we can't
828 * grant it. Claim a system limit for the
831 if (lf_split(overlap
, lock
)) {
836 lf_wakelock(overlap
, TRUE
);
839 case OVERLAP_CONTAINED_BY_LOCK
:
841 * If downgrading lock, others may be able to
842 * acquire it, otherwise take the list.
844 if (lock
->lf_type
== F_RDLCK
&&
845 overlap
->lf_type
== F_WRLCK
) {
846 lf_wakelock(overlap
, TRUE
);
848 while (!TAILQ_EMPTY(&overlap
->lf_blkhd
)) {
849 ltmp
= TAILQ_FIRST(&overlap
->lf_blkhd
);
850 TAILQ_REMOVE(&overlap
->lf_blkhd
, ltmp
,
852 TAILQ_INSERT_TAIL(&lock
->lf_blkhd
,
854 ltmp
->lf_next
= lock
;
858 * Add the new lock if necessary and delete the overlap.
862 lock
->lf_next
= overlap
->lf_next
;
863 prev
= &lock
->lf_next
;
866 *prev
= overlap
->lf_next
;
868 FREE(overlap
, M_LOCKF
);
871 case OVERLAP_STARTS_BEFORE_LOCK
:
873 * Add lock after overlap on the list.
875 lock
->lf_next
= overlap
->lf_next
;
876 overlap
->lf_next
= lock
;
877 overlap
->lf_end
= lock
->lf_start
- 1;
878 prev
= &lock
->lf_next
;
879 lf_wakelock(overlap
, TRUE
);
883 case OVERLAP_ENDS_AFTER_LOCK
:
885 * Add the new lock before overlap.
889 lock
->lf_next
= overlap
;
891 overlap
->lf_start
= lock
->lf_end
+ 1;
892 lf_wakelock(overlap
, TRUE
);
897 /* Coalesce adjacent locks with identical attributes */
898 lf_coalesce_adjacent(lock
);
899 #ifdef LOCKF_DEBUGGING
900 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
901 lf_print("lf_setlock: got the lock", lock
);
902 lf_printlist("lf_setlock(out)", lock
);
904 #endif /* LOCKF_DEBUGGING */
912 * Description: Remove a byte-range lock on an vnode. Generally, find the
913 * lock (or an overlap to that lock) and remove it (or shrink
914 * it), then wakeup anyone we can.
916 * Parameters: unlock The lock to clear
921 * Notes: A caller may unlock all the locks owned by the caller by
922 * specifying the entire file range; locks owned by other
923 * callers are not effected by this operation.
926 lf_clearlock(struct lockf
*unlock
)
928 struct lockf
**head
= unlock
->lf_head
;
929 struct lockf
*lf
= *head
;
930 struct lockf
*overlap
, **prev
;
936 #ifdef LOCKF_DEBUGGING
937 if (unlock
->lf_type
!= F_UNLCK
) {
938 panic("lf_clearlock: bad type");
940 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
941 lf_print("lf_clearlock", unlock
);
943 #endif /* LOCKF_DEBUGGING */
945 while ((ovcase
= lf_findoverlap(lf
, unlock
, SELF
, &prev
, &overlap
)) != OVERLAP_NONE
) {
947 * Wakeup the list of locks to be retried.
949 lf_wakelock(overlap
, FALSE
);
950 #if IMPORTANCE_INHERITANCE
951 if (overlap
->lf_boosted
== LF_BOOSTED
) {
952 lf_drop_assertion(overlap
);
954 #endif /* IMPORTANCE_INHERITANCE */
957 case OVERLAP_NONE
: /* satisfy compiler enum/switch */
960 case OVERLAP_EQUALS_LOCK
:
961 *prev
= overlap
->lf_next
;
962 FREE(overlap
, M_LOCKF
);
965 case OVERLAP_CONTAINS_LOCK
: /* split it */
966 if (overlap
->lf_start
== unlock
->lf_start
) {
967 overlap
->lf_start
= unlock
->lf_end
+ 1;
971 * If we can't split the lock, we can't grant it.
972 * Claim a system limit for the resource shortage.
974 if (lf_split(overlap
, unlock
)) {
977 overlap
->lf_next
= unlock
->lf_next
;
980 case OVERLAP_CONTAINED_BY_LOCK
:
981 *prev
= overlap
->lf_next
;
982 lf
= overlap
->lf_next
;
983 FREE(overlap
, M_LOCKF
);
986 case OVERLAP_STARTS_BEFORE_LOCK
:
987 overlap
->lf_end
= unlock
->lf_start
- 1;
988 prev
= &overlap
->lf_next
;
989 lf
= overlap
->lf_next
;
992 case OVERLAP_ENDS_AFTER_LOCK
:
993 overlap
->lf_start
= unlock
->lf_end
+ 1;
998 #ifdef LOCKF_DEBUGGING
999 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
1000 lf_printlist("lf_clearlock", unlock
);
1002 #endif /* LOCKF_DEBUGGING */
1010 * Description: Check whether there is a blocking lock, and if so return
1011 * its process identifier into the lock being requested.
1013 * Parameters: lock Pointer to lock to test for blocks
1014 * fl Pointer to flock structure to receive
1015 * the blocking lock information, if a
1016 * blocking lock is found.
1017 * matchpid -1, or pid value to match in lookup.
1019 * Returns: 0 Success
1022 * *fl Contents modified to reflect the
1023 * blocking lock, if one is found; not
1024 * modified otherwise
1026 * Notes: fl->l_pid will be (-1) for file locks and will only be set to
1027 * the blocking process ID for advisory record locks.
1030 lf_getlock(struct lockf
*lock
, struct flock
*fl
, pid_t matchpid
)
1032 struct lockf
*block
;
1034 #ifdef LOCKF_DEBUGGING
1035 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
1036 lf_print("lf_getlock", lock
);
1038 #endif /* LOCKF_DEBUGGING */
1040 if ((block
= lf_getblock(lock
, matchpid
))) {
1041 fl
->l_type
= block
->lf_type
;
1042 fl
->l_whence
= SEEK_SET
;
1043 fl
->l_start
= block
->lf_start
;
1044 if (block
->lf_end
== -1) {
1047 fl
->l_len
= block
->lf_end
- block
->lf_start
+ 1;
1049 if (NULL
!= block
->lf_owner
) {
1051 * lf_owner is only non-NULL when the lock
1052 * "owner" can be unambiguously determined
1054 fl
->l_pid
= proc_pid(block
->lf_owner
);
1059 fl
->l_type
= F_UNLCK
;
1067 * Description: Walk the list of locks for an inode and return the first
1068 * blocking lock. A lock is considered blocking if we are not
1069 * the lock owner; otherwise, we are permitted to upgrade or
1070 * downgrade it, and it's not considered blocking.
1072 * Parameters: lock The lock for which we are interested
1073 * in obtaining the blocking lock, if any
1074 * matchpid -1, or pid value to match in lookup.
1076 * Returns: NOLOCKF No blocking lock exists
1077 * !NOLOCKF The address of the blocking lock's
1080 static struct lockf
*
1081 lf_getblock(struct lockf
*lock
, pid_t matchpid
)
1083 struct lockf
**prev
, *overlap
, *lf
= *(lock
->lf_head
);
1085 for (prev
= lock
->lf_head
;
1086 lf_findoverlap(lf
, lock
, OTHERS
, &prev
, &overlap
) != OVERLAP_NONE
;
1087 lf
= overlap
->lf_next
) {
1091 * If we're matching pids, and it's a record lock,
1092 * or it's an OFD lock on a process-confined fd,
1093 * but the pid doesn't match, then keep on looking ..
1095 if (matchpid
!= -1 &&
1096 (overlap
->lf_flags
& (F_POSIX
| F_OFD_LOCK
)) != 0 &&
1097 proc_pid(overlap
->lf_owner
) != matchpid
) {
1104 if ((lock
->lf_type
== F_WRLCK
|| overlap
->lf_type
== F_WRLCK
)) {
1115 * Description: Walk the list of locks to find an overlapping lock (if any).
1117 * Parameters: lf First lock on lock list
1118 * lock The lock we are checking for an overlap
1120 * prev pointer to pointer pointer to contain
1121 * address of pointer to previous lock
1122 * pointer to overlapping lock, if overlap
1123 * overlap pointer to pointer to contain address
1124 * of overlapping lock
1126 * Returns: OVERLAP_NONE
1127 * OVERLAP_EQUALS_LOCK
1128 * OVERLAP_CONTAINS_LOCK
1129 * OVERLAP_CONTAINED_BY_LOCK
1130 * OVERLAP_STARTS_BEFORE_LOCK
1131 * OVERLAP_ENDS_AFTER_LOCK
1134 * *prev The address of the next pointer in the
1135 * lock previous to the overlapping lock;
1136 * this is generally used to relink the
1137 * lock list, avoiding a second iteration.
1138 * *overlap The pointer to the overlapping lock
1139 * itself; this is used to return data in
1140 * the check == OTHERS case, and for the
1141 * caller to modify the overlapping lock,
1142 * in the check == SELF case
1144 * Note: This returns only the FIRST overlapping lock. There may be
1145 * more than one. lf_getlock will return the first blocking lock,
1146 * while lf_setlock will iterate over all overlapping locks to
1148 * The check parameter can be SELF, meaning we are looking for
1149 * overlapping locks owned by us, or it can be OTHERS, meaning
1150 * we are looking for overlapping locks owned by someone else so
1151 * we can report a blocking lock on an F_GETLK request.
1153 * The value of *overlap and *prev are modified, even if there is
1154 * no overlapping lock found; always check the return code.
1157 lf_findoverlap(struct lockf
*lf
, struct lockf
*lock
, int type
,
1158 struct lockf
***prev
, struct lockf
**overlap
)
1164 if (lf
== NOLOCKF
) {
1167 #ifdef LOCKF_DEBUGGING
1168 if (LOCKF_DEBUGP(LF_DBG_LIST
)) {
1169 lf_print("lf_findoverlap: looking for overlap in", lock
);
1171 #endif /* LOCKF_DEBUGGING */
1172 start
= lock
->lf_start
;
1174 while (lf
!= NOLOCKF
) {
1175 if (((type
& SELF
) && lf
->lf_id
!= lock
->lf_id
) ||
1176 ((type
& OTHERS
) && lf
->lf_id
== lock
->lf_id
)) {
1178 * Locks belonging to one process are adjacent on the
1179 * list, so if we've found any locks belonging to us,
1180 * and we're now seeing something else, then we've
1181 * examined all "self" locks. Note that bailing out
1182 * here is quite important; for coalescing, we assume
1183 * numerically adjacent locks from the same owner to
1184 * be adjacent on the list.
1186 if ((type
& SELF
) && found_self
) {
1187 return OVERLAP_NONE
;
1190 *prev
= &lf
->lf_next
;
1191 *overlap
= lf
= lf
->lf_next
;
1195 if ((type
& SELF
)) {
1199 #ifdef LOCKF_DEBUGGING
1200 if (LOCKF_DEBUGP(LF_DBG_LIST
)) {
1201 lf_print("\tchecking", lf
);
1203 #endif /* LOCKF_DEBUGGING */
1205 * OK, check for overlap
1207 if ((lf
->lf_end
!= -1 && start
> lf
->lf_end
) ||
1208 (end
!= -1 && lf
->lf_start
> end
)) {
1210 LOCKF_DEBUG(LF_DBG_LIST
, "no overlap\n");
1213 * NOTE: assumes that locks for the same process are
1214 * nonintersecting and ordered.
1216 if ((type
& SELF
) && end
!= -1 && lf
->lf_start
> end
) {
1217 return OVERLAP_NONE
;
1219 *prev
= &lf
->lf_next
;
1220 *overlap
= lf
= lf
->lf_next
;
1223 if ((lf
->lf_start
== start
) && (lf
->lf_end
== end
)) {
1224 LOCKF_DEBUG(LF_DBG_LIST
, "overlap == lock\n");
1225 return OVERLAP_EQUALS_LOCK
;
1227 if ((lf
->lf_start
<= start
) &&
1229 ((lf
->lf_end
>= end
) || (lf
->lf_end
== -1))) {
1230 LOCKF_DEBUG(LF_DBG_LIST
, "overlap contains lock\n");
1231 return OVERLAP_CONTAINS_LOCK
;
1233 if (start
<= lf
->lf_start
&&
1235 (lf
->lf_end
!= -1 && end
>= lf
->lf_end
))) {
1236 LOCKF_DEBUG(LF_DBG_LIST
, "lock contains overlap\n");
1237 return OVERLAP_CONTAINED_BY_LOCK
;
1239 if ((lf
->lf_start
< start
) &&
1240 ((lf
->lf_end
>= start
) || (lf
->lf_end
== -1))) {
1241 LOCKF_DEBUG(LF_DBG_LIST
, "overlap starts before lock\n");
1242 return OVERLAP_STARTS_BEFORE_LOCK
;
1244 if ((lf
->lf_start
> start
) &&
1246 ((lf
->lf_end
> end
) || (lf
->lf_end
== -1))) {
1247 LOCKF_DEBUG(LF_DBG_LIST
, "overlap ends after lock\n");
1248 return OVERLAP_ENDS_AFTER_LOCK
;
1250 panic("lf_findoverlap: default");
1252 return OVERLAP_NONE
;
1259 * Description: Split a lock and a contained region into two or three locks
1262 * Parameters: lock1 Lock to split
1263 * lock2 Overlapping lock region requiring the
1264 * split (upgrade/downgrade/unlock)
1266 * Returns: 0 Success
1267 * ENOLCK No memory for new lock
1270 * *lock1 Modified original lock
1271 * *lock2 Overlapping lock (inserted into list)
1272 * (new lock) Potential new lock inserted into list
1273 * if split results in 3 locks
1275 * Notes: This operation can only fail if the split would result in three
1276 * locks, and there is insufficient memory to allocate the third
1277 * lock; in that case, neither of the locks will be modified.
1280 lf_split(struct lockf
*lock1
, struct lockf
*lock2
)
1282 struct lockf
*splitlock
;
1284 #ifdef LOCKF_DEBUGGING
1285 if (LOCKF_DEBUGP(LF_DBG_LIST
)) {
1286 lf_print("lf_split", lock1
);
1287 lf_print("splitting from", lock2
);
1289 #endif /* LOCKF_DEBUGGING */
1291 * Check to see if splitting into only two pieces.
1293 if (lock1
->lf_start
== lock2
->lf_start
) {
1294 lock1
->lf_start
= lock2
->lf_end
+ 1;
1295 lock2
->lf_next
= lock1
;
1298 if (lock1
->lf_end
== lock2
->lf_end
) {
1299 lock1
->lf_end
= lock2
->lf_start
- 1;
1300 lock2
->lf_next
= lock1
->lf_next
;
1301 lock1
->lf_next
= lock2
;
1305 * Make a new lock consisting of the last part of
1306 * the encompassing lock
1308 MALLOC(splitlock
, struct lockf
*, sizeof *splitlock
, M_LOCKF
, M_WAITOK
);
1309 if (splitlock
== NULL
) {
1312 bcopy(lock1
, splitlock
, sizeof *splitlock
);
1313 splitlock
->lf_start
= lock2
->lf_end
+ 1;
1314 TAILQ_INIT(&splitlock
->lf_blkhd
);
1315 lock1
->lf_end
= lock2
->lf_start
- 1;
1317 * OK, now link it in
1319 splitlock
->lf_next
= lock1
->lf_next
;
1320 lock2
->lf_next
= splitlock
;
1321 lock1
->lf_next
= lock2
;
1330 * Wakeup a blocklist in the case of a downgrade or unlock, since others
1331 * waiting on the lock may now be able to acquire it.
1333 * Parameters: listhead Lock list head on which waiters may
1334 * have pending locks
1338 * Notes: This function iterates a list of locks and wakes all waiters,
1339 * rather than only waiters for the contended regions. Because
1340 * of this, for heavily contended files, this can result in a
1341 * "thundering herd" situation. Refactoring the code could make
1342 * this operation more efficient, if heavy contention ever results
1343 * in a real-world performance problem.
1346 lf_wakelock(struct lockf
*listhead
, boolean_t force_all
)
1348 struct lockf
*wakelock
;
1349 boolean_t wake_all
= TRUE
;
1351 if (force_all
== FALSE
&& (listhead
->lf_flags
& F_WAKE1_SAFE
)) {
1355 while (!TAILQ_EMPTY(&listhead
->lf_blkhd
)) {
1356 wakelock
= TAILQ_FIRST(&listhead
->lf_blkhd
);
1357 TAILQ_REMOVE(&listhead
->lf_blkhd
, wakelock
, lf_block
);
1359 wakelock
->lf_next
= NOLOCKF
;
1360 #ifdef LOCKF_DEBUGGING
1361 if (LOCKF_DEBUGP(LF_DBG_LOCKOP
)) {
1362 lf_print("lf_wakelock: awakening", wakelock
);
1364 #endif /* LOCKF_DEBUGGING */
1365 if (wake_all
== FALSE
) {
1367 * If there are items on the list head block list,
1368 * move them to the wakelock list instead, and then
1369 * correct their lf_next pointers.
1371 if (!TAILQ_EMPTY(&listhead
->lf_blkhd
)) {
1372 TAILQ_CONCAT(&wakelock
->lf_blkhd
, &listhead
->lf_blkhd
, lf_block
);
1374 struct lockf
*tlock
;
1376 TAILQ_FOREACH(tlock
, &wakelock
->lf_blkhd
, lf_block
) {
1377 if (TAILQ_NEXT(tlock
, lf_block
) == tlock
) {
1378 /* See rdar://10887303 */
1379 panic("cycle in wakelock list");
1381 tlock
->lf_next
= wakelock
;
1387 if (wake_all
== FALSE
) {
1394 #ifdef LOCKF_DEBUGGING
1395 #define GET_LF_OWNER_PID(lf) (proc_pid((lf)->lf_owner))
1400 * Print out a lock; lock information is prefixed by the string in 'tag'
1402 * Parameters: tag A string tag for debugging
1403 * lock The lock whose information should be
1409 lf_print(const char *tag
, struct lockf
*lock
)
1411 printf("%s: lock %p for ", tag
, (void *)lock
);
1412 if (lock
->lf_flags
& F_POSIX
) {
1413 printf("proc %p (owner %d)",
1414 lock
->lf_id
, GET_LF_OWNER_PID(lock
));
1415 } else if (lock
->lf_flags
& F_OFD_LOCK
) {
1416 printf("fg %p (owner %d)",
1417 lock
->lf_id
, GET_LF_OWNER_PID(lock
));
1419 printf("id %p", (void *)lock
->lf_id
);
1421 if (lock
->lf_vnode
!= 0) {
1422 printf(" in vno %p, %s, start 0x%016llx, end 0x%016llx",
1424 lock
->lf_type
== F_RDLCK
? "shared" :
1425 lock
->lf_type
== F_WRLCK
? "exclusive" :
1426 lock
->lf_type
== F_UNLCK
? "unlock" : "unknown",
1427 (intmax_t)lock
->lf_start
, (intmax_t)lock
->lf_end
);
1429 printf(" %s, start 0x%016llx, end 0x%016llx",
1430 lock
->lf_type
== F_RDLCK
? "shared" :
1431 lock
->lf_type
== F_WRLCK
? "exclusive" :
1432 lock
->lf_type
== F_UNLCK
? "unlock" : "unknown",
1433 (intmax_t)lock
->lf_start
, (intmax_t)lock
->lf_end
);
1435 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
1436 printf(" block %p\n", (void *)TAILQ_FIRST(&lock
->lf_blkhd
));
1444 * lf_printlist DEBUG
1446 * Print out a lock list for the vnode associated with 'lock'; lock information
1447 * is prefixed by the string in 'tag'
1449 * Parameters: tag A string tag for debugging
1450 * lock The lock whose vnode's lock list should
1456 lf_printlist(const char *tag
, struct lockf
*lock
)
1458 struct lockf
*lf
, *blk
;
1460 if (lock
->lf_vnode
== 0) {
1464 printf("%s: Lock list for vno %p:\n",
1465 tag
, lock
->lf_vnode
);
1466 for (lf
= lock
->lf_vnode
->v_lockf
; lf
; lf
= lf
->lf_next
) {
1467 printf("\tlock %p for ", (void *)lf
);
1468 if (lf
->lf_flags
& F_POSIX
) {
1469 printf("proc %p (owner %d)",
1470 lf
->lf_id
, GET_LF_OWNER_PID(lf
));
1471 } else if (lf
->lf_flags
& F_OFD_LOCK
) {
1472 printf("fg %p (owner %d)",
1473 lf
->lf_id
, GET_LF_OWNER_PID(lf
));
1475 printf("id %p", (void *)lf
->lf_id
);
1477 printf(", %s, start 0x%016llx, end 0x%016llx",
1478 lf
->lf_type
== F_RDLCK
? "shared" :
1479 lf
->lf_type
== F_WRLCK
? "exclusive" :
1480 lf
->lf_type
== F_UNLCK
? "unlock" :
1481 "unknown", (intmax_t)lf
->lf_start
, (intmax_t)lf
->lf_end
);
1482 TAILQ_FOREACH(blk
, &lf
->lf_blkhd
, lf_block
) {
1483 printf("\n\t\tlock request %p for ", (void *)blk
);
1484 if (blk
->lf_flags
& F_POSIX
) {
1485 printf("proc %p (owner %d)",
1486 blk
->lf_id
, GET_LF_OWNER_PID(blk
));
1487 } else if (blk
->lf_flags
& F_OFD_LOCK
) {
1488 printf("fg %p (owner %d)",
1489 blk
->lf_id
, GET_LF_OWNER_PID(blk
));
1491 printf("id %p", (void *)blk
->lf_id
);
1493 printf(", %s, start 0x%016llx, end 0x%016llx",
1494 blk
->lf_type
== F_RDLCK
? "shared" :
1495 blk
->lf_type
== F_WRLCK
? "exclusive" :
1496 blk
->lf_type
== F_UNLCK
? "unlock" :
1497 "unknown", (intmax_t)blk
->lf_start
,
1498 (intmax_t)blk
->lf_end
);
1499 if (!TAILQ_EMPTY(&blk
->lf_blkhd
)) {
1500 panic("lf_printlist: bad list");
1506 #endif /* LOCKF_DEBUGGING */
1508 #if IMPORTANCE_INHERITANCE
1513 * Call task importance hold assertion on the owner of the lock.
1515 * Parameters: block_task Owner of the lock blocking
1518 * block lock on which the current thread
1523 * Notes: The task reference on block_task is not needed to be hold since
1524 * the current thread has vnode lock and block_task has a file
1525 * lock, thus removing file lock in exit requires block_task to
1526 * grab the vnode lock.
1529 lf_hold_assertion(task_t block_task
, struct lockf
*block
)
1531 if (task_importance_hold_file_lock_assertion(block_task
, 1) == 0) {
1532 block
->lf_boosted
= LF_BOOSTED
;
1533 LOCKF_DEBUG(LF_DBG_IMPINH
,
1534 "lf: importance hold file lock assert on pid %d lock %p\n",
1535 proc_pid(block
->lf_owner
), block
);
1541 * lf_jump_to_queue_head
1543 * Jump the lock from the tail of the block queue to the head of
1546 * Parameters: block lockf struct containing the
1548 * lock lockf struct to be jumped to the
1554 lf_jump_to_queue_head(struct lockf
*block
, struct lockf
*lock
)
1556 /* Move the lock to the head of the block queue. */
1557 TAILQ_REMOVE(&block
->lf_blkhd
, lock
, lf_block
);
1558 TAILQ_INSERT_HEAD(&block
->lf_blkhd
, lock
, lf_block
);
1565 * Drops the task hold assertion.
1567 * Parameters: block lockf struct holding the assertion.
1572 lf_drop_assertion(struct lockf
*block
)
1574 LOCKF_DEBUG(LF_DBG_IMPINH
, "lf: %d: dropping assertion for lock %p\n",
1575 proc_pid(block
->lf_owner
), block
);
1577 task_t current_task
= proc_task(block
->lf_owner
);
1578 task_importance_drop_file_lock_assertion(current_task
, 1);
1579 block
->lf_boosted
= LF_NOT_BOOSTED
;
1583 * lf_adjust_assertion
1585 * Adjusts importance assertion of file lock. Goes through
1586 * all the blocking locks and checks if the file lock needs
1587 * to be boosted anymore.
1589 * Parameters: block lockf structure which needs to be adjusted.
1594 lf_adjust_assertion(struct lockf
*block
)
1596 boolean_t drop_boost
= TRUE
;
1599 /* Return if the lock is not boosted */
1600 if (block
->lf_boosted
== LF_NOT_BOOSTED
) {
1604 TAILQ_FOREACH(next
, &block
->lf_blkhd
, lf_block
) {
1605 /* Check if block and next are same type of locks */
1606 if (((block
->lf_flags
& next
->lf_flags
& F_POSIX
) != 0) ||
1607 ((block
->lf_flags
& next
->lf_flags
& F_OFD_LOCK
) &&
1608 (block
->lf_owner
!= next
->lf_owner
) &&
1609 (NULL
!= block
->lf_owner
&& NULL
!= next
->lf_owner
))) {
1610 /* Check if next would be boosting block */
1611 if (task_is_importance_donor(proc_task(next
->lf_owner
)) &&
1612 task_is_importance_receiver_type(proc_task(block
->lf_owner
))) {
1613 /* Found a lock boosting block */
1621 lf_drop_assertion(block
);
1626 lf_boost_blocking_proc(struct lockf
*lock
, struct lockf
*block
)
1628 task_t ltask
= proc_task(lock
->lf_owner
);
1629 task_t btask
= proc_task(block
->lf_owner
);
1632 * Check if ltask can donate importance. The
1633 * check of imp_donor bit is done without holding
1634 * any lock. The value may change after you read it,
1635 * but it is ok to boost a task while someone else is
1638 * TODO: Support live inheritance on file locks.
1640 if (task_is_importance_donor(ltask
)) {
1641 LOCKF_DEBUG(LF_DBG_IMPINH
,
1642 "lf: %d: attempt to boost pid %d that holds lock %p\n",
1643 proc_pid(lock
->lf_owner
), proc_pid(block
->lf_owner
), block
);
1645 if (block
->lf_boosted
!= LF_BOOSTED
&&
1646 task_is_importance_receiver_type(btask
)) {
1647 lf_hold_assertion(btask
, block
);
1649 lf_jump_to_queue_head(block
, lock
);
1652 #endif /* IMPORTANCE_INHERITANCE */