2 * Copyright (c) 2015 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>
84 * This variable controls the maximum number of processes that will
85 * be checked in doing deadlock detection.
87 static int maxlockdepth
= MAXDEPTH
;
89 #if (DEVELOPMENT || DEBUG)
90 #define LOCKF_DEBUGGING 1
93 #ifdef LOCKF_DEBUGGING
94 #include <sys/sysctl.h>
95 void lf_print(const char *tag
, struct lockf
*lock
);
96 void lf_printlist(const char *tag
, struct lockf
*lock
);
98 #define LF_DBG_LOCKOP (1 << 0) /* setlk, getlk, clearlk */
99 #define LF_DBG_LIST (1 << 1) /* split, coalesce */
100 #define LF_DBG_IMPINH (1 << 2) /* importance inheritance */
101 #define LF_DBG_TRACE (1 << 3) /* errors, exit */
103 static int lockf_debug
= 0; /* was 2, could be 3 ;-) */
104 SYSCTL_INT(_debug
, OID_AUTO
, lockf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lockf_debug
, 0, "");
107 * If there is no mask bit selector, or there is one, and the selector is
108 * set, then output the debugging diagnostic.
110 #define LOCKF_DEBUG(mask, ...) \
112 if( !(mask) || ((mask) & lockf_debug)) { \
113 printf(__VA_ARGS__); \
116 #else /* !LOCKF_DEBUGGING */
117 #define LOCKF_DEBUG(mask, ...) /* mask */
118 #endif /* !LOCKF_DEBUGGING */
120 MALLOC_DEFINE(M_LOCKF
, "lockf", "Byte-range locking structures");
122 #define NOLOCKF (struct lockf *)0
125 #define OFF_MAX 0x7fffffffffffffffULL /* max off_t */
128 * Overlapping lock states
133 OVERLAP_CONTAINS_LOCK
,
134 OVERLAP_CONTAINED_BY_LOCK
,
135 OVERLAP_STARTS_BEFORE_LOCK
,
136 OVERLAP_ENDS_AFTER_LOCK
139 static int lf_clearlock(struct lockf
*);
140 static overlap_t
lf_findoverlap(struct lockf
*,
141 struct lockf
*, int, struct lockf
***, struct lockf
**);
142 static struct lockf
*lf_getblock(struct lockf
*, pid_t
);
143 static int lf_getlock(struct lockf
*, struct flock
*, pid_t
);
144 static int lf_setlock(struct lockf
*, struct timespec
*);
145 static int lf_split(struct lockf
*, struct lockf
*);
146 static void lf_wakelock(struct lockf
*, boolean_t
);
147 #if IMPORTANCE_INHERITANCE
148 static void lf_hold_assertion(task_t
, struct lockf
*);
149 static void lf_jump_to_queue_head(struct lockf
*, struct lockf
*);
150 static void lf_drop_assertion(struct lockf
*);
151 static void lf_boost_blocking_proc(struct lockf
*, struct lockf
*);
152 #endif /* IMPORTANCE_INHERITANCE */
157 * Description: Advisory record locking support
159 * Parameters: ap Argument pointer to a vnop_advlock_args
160 * argument descriptor structure for the
161 * lock operation to be attempted.
166 * ENOLCK Number of locked regions exceeds limit
171 * lf_setlock:ETIMEDOUT
172 * lf_clearlock:ENOLCK
175 * Notes: We return ENOLCK when we run out of memory to support locks; as
176 * such, there is no specific expectation limit other than the
177 * amount of available resources.
180 lf_advlock(struct vnop_advlock_args
*ap
)
182 struct vnode
*vp
= ap
->a_vp
;
183 struct flock
*fl
= ap
->a_fl
;
184 vfs_context_t context
= ap
->a_context
;
186 off_t start
, end
, oadd
;
189 struct lockf
**head
= &vp
->v_lockf
;
191 /* XXX HFS may need a !vnode_isreg(vp) EISDIR error here */
194 * Avoid the common case of unlocking when inode has no locks.
196 if (*head
== (struct lockf
*)0) {
197 if (ap
->a_op
!= F_SETLK
) {
198 fl
->l_type
= F_UNLCK
;
199 LOCKF_DEBUG(LF_DBG_TRACE
,
200 "lf_advlock: '%s' unlock without lock\n",
201 vfs_context_proc(context
)->p_comm
);
207 * Convert the flock structure into a start and end.
209 switch (fl
->l_whence
) {
214 * Caller is responsible for adding any necessary offset
215 * when SEEK_CUR is used.
223 * It's OK to cast the u_quad_t to and off_t here, since they
224 * are the same storage size, and the value of the returned
225 * contents will never overflow into the sign bit. We need to
226 * do this because we will use size to force range checks.
228 if ((error
= vnode_size(vp
, (off_t
*)&size
, context
))) {
229 LOCKF_DEBUG(LF_DBG_TRACE
,
230 "lf_advlock: vnode_getattr failed: %d\n", error
);
234 if (size
> OFF_MAX
||
236 size
> (u_quad_t
)(OFF_MAX
- fl
->l_start
)))
238 start
= size
+ fl
->l_start
;
242 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: unknown whence %d\n",
247 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: start < 0 (%qd)\n",
253 LOCKF_DEBUG(LF_DBG_TRACE
,
254 "lf_advlock: len < 0 & start == 0\n");
260 LOCKF_DEBUG(LF_DBG_TRACE
,
261 "lf_advlock: start < 0 (%qd)\n", start
);
264 } else if (fl
->l_len
== 0)
267 oadd
= fl
->l_len
- 1;
268 if (oadd
> (off_t
)(OFF_MAX
- start
)) {
269 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: overflow\n");
275 * Create the lockf structure
277 MALLOC(lock
, struct lockf
*, sizeof *lock
, M_LOCKF
, M_WAITOK
);
280 lock
->lf_start
= start
;
282 lock
->lf_id
= ap
->a_id
;
284 lock
->lf_type
= fl
->l_type
;
285 lock
->lf_head
= head
;
286 lock
->lf_next
= (struct lockf
*)0;
287 TAILQ_INIT(&lock
->lf_blkhd
);
288 lock
->lf_flags
= ap
->a_flags
;
289 #if IMPORTANCE_INHERITANCE
290 lock
->lf_boosted
= LF_NOT_BOOSTED
;
292 if (ap
->a_flags
& F_POSIX
)
293 lock
->lf_owner
= (struct proc
*)lock
->lf_id
;
295 lock
->lf_owner
= NULL
;
297 if (ap
->a_flags
& F_FLOCK
)
298 lock
->lf_flags
|= F_WAKE1_SAFE
;
300 lck_mtx_lock(&vp
->v_lock
); /* protect the lockf list */
302 * Do the requested operation.
307 * For F_OFD_* locks, lf_id is the fileglob.
308 * Record an "lf_owner" iff this is a confined fd
309 * i.e. it cannot escape this process and will be
310 * F_UNLCKed before the owner exits. (This is
311 * the implicit guarantee needed to ensure lf_owner
312 * remains a valid reference here.)
314 if (ap
->a_flags
& F_OFD_LOCK
) {
315 struct fileglob
*fg
= (void *)lock
->lf_id
;
316 if (fg
->fg_lflags
& FG_CONFINED
)
317 lock
->lf_owner
= current_proc();
319 error
= lf_setlock(lock
, ap
->a_timeout
);
323 error
= lf_clearlock(lock
);
328 error
= lf_getlock(lock
, fl
, -1);
338 lck_mtx_unlock(&vp
->v_lock
); /* done manipulating the list */
340 LOCKF_DEBUG(LF_DBG_TRACE
, "lf_advlock: normal exit: %d\n", error
);
345 * Empty the queue of msleeping requests for a lock on the given vnode.
346 * Called with the vnode already locked. Used for forced unmount, where
347 * a flock(2) invoker sleeping on a blocked lock holds an iocount reference
348 * that prevents the vnode from ever being drained. Force unmounting wins.
351 lf_abort_advlocks(vnode_t vp
)
355 if ((lock
= vp
->v_lockf
) == NULL
)
358 lck_mtx_assert(&vp
->v_lock
, LCK_MTX_ASSERT_OWNED
);
360 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
363 TAILQ_FOREACH(tlock
, &lock
->lf_blkhd
, lf_block
) {
365 * Setting this flag should cause all
366 * currently blocked F_SETLK request to
367 * return to userland with an errno.
369 tlock
->lf_flags
|= F_ABORT
;
371 lf_wakelock(lock
, TRUE
);
376 * Take any lock attempts which are currently blocked by a given lock ("from")
377 * and mark them as blocked by a different lock ("to"). Used in the case
378 * where a byte range currently occupied by "from" is to be occupied by "to."
381 lf_move_blocked(struct lockf
*to
, struct lockf
*from
)
385 TAILQ_FOREACH(tlock
, &from
->lf_blkhd
, lf_block
) {
389 TAILQ_CONCAT(&to
->lf_blkhd
, &from
->lf_blkhd
, lf_block
);
393 * lf_coalesce_adjacent
395 * Description: Helper function: when setting a lock, coalesce adjacent
396 * locks. Needed because adjacent locks are not overlapping,
397 * but POSIX requires that they be coalesced.
399 * Parameters: lock The new lock which may be adjacent
400 * to already locked regions, and which
401 * should therefore be coalesced with them
406 lf_coalesce_adjacent(struct lockf
*lock
)
408 struct lockf
**lf
= lock
->lf_head
;
410 while (*lf
!= NOLOCKF
) {
411 /* reject locks that obviously could not be coalesced */
413 ((*lf
)->lf_id
!= lock
->lf_id
) ||
414 ((*lf
)->lf_type
!= lock
->lf_type
)) {
415 lf
= &(*lf
)->lf_next
;
420 * NOTE: Assumes that if two locks are adjacent on the number line
421 * and belong to the same owner, then they are adjacent on the list.
423 if ((*lf
)->lf_end
!= -1 &&
424 ((*lf
)->lf_end
+ 1) == lock
->lf_start
) {
425 struct lockf
*adjacent
= *lf
;
427 LOCKF_DEBUG(LF_DBG_LIST
, "lf_coalesce_adjacent: coalesce adjacent previous\n");
428 lock
->lf_start
= (*lf
)->lf_start
;
430 lf
= &(*lf
)->lf_next
;
432 lf_move_blocked(lock
, adjacent
);
434 FREE(adjacent
, M_LOCKF
);
437 /* If the lock starts adjacent to us, we can coalesce it */
438 if (lock
->lf_end
!= -1 &&
439 (lock
->lf_end
+ 1) == (*lf
)->lf_start
) {
440 struct lockf
*adjacent
= *lf
;
442 LOCKF_DEBUG(LF_DBG_LIST
, "lf_coalesce_adjacent: coalesce adjacent following\n");
443 lock
->lf_end
= (*lf
)->lf_end
;
444 lock
->lf_next
= (*lf
)->lf_next
;
447 lf_move_blocked(lock
, adjacent
);
449 FREE(adjacent
, M_LOCKF
);
453 /* no matching conditions; go on to next lock */
454 lf
= &(*lf
)->lf_next
;
461 * Description: Set a byte-range lock.
463 * Parameters: lock The lock structure describing the lock
464 * to be set; allocated by the caller, it
465 * will be linked into the lock list if
466 * the set is successful, and freed if the
467 * set is unsuccessful.
469 * timeout Timeout specified in the case of
476 * lf_clearlock:ENOLCK
480 * Notes: We add the lock to the provisional lock list. We do not
481 * coalesce at this time; this has implications for other lock
482 * requestors in the blocker search mechanism.
485 lf_setlock(struct lockf
*lock
, struct timespec
*timeout
)
488 struct lockf
**head
= lock
->lf_head
;
489 struct lockf
**prev
, *overlap
, *ltmp
;
490 static char lockstr
[] = "lockf";
491 int priority
, needtolink
, error
;
492 struct vnode
*vp
= lock
->lf_vnode
;
495 #ifdef LOCKF_DEBUGGING
496 if (lockf_debug
& LF_DBG_LOCKOP
) {
497 lf_print("lf_setlock", lock
);
498 lf_printlist("lf_setlock(in)", lock
);
500 #endif /* LOCKF_DEBUGGING */
506 if (lock
->lf_type
== F_WRLCK
)
510 * Scan lock list for this file looking for locks that would block us.
512 while ((block
= lf_getblock(lock
, -1))) {
514 * Free the structure and return if nonblocking.
516 if ((lock
->lf_flags
& F_WAIT
) == 0) {
517 DTRACE_FSINFO(advlock__nowait
, vnode_t
, vp
);
523 * We are blocked. Since flock style locks cover
524 * the whole file, there is no chance for deadlock.
526 * OFD byte-range locks currently do NOT support
527 * deadlock detection.
529 * For POSIX byte-range locks we must check for deadlock.
531 * Deadlock detection is done by looking through the
532 * wait channels to see if there are any cycles that
533 * involve us. MAXDEPTH is set just to make sure we
534 * do not go off into neverland.
536 if ((lock
->lf_flags
& F_POSIX
) &&
537 (block
->lf_flags
& F_POSIX
)) {
538 struct proc
*wproc
, *bproc
;
540 struct lockf
*waitblock
;
543 /* The block is waiting on something */
544 wproc
= block
->lf_owner
;
546 TAILQ_FOREACH(ut
, &wproc
->p_uthlist
, uu_list
) {
548 * While the thread is asleep (uu_wchan != 0)
549 * in this code (uu_wmesg == lockstr)
550 * and we have not exceeded the maximum cycle
551 * depth (i < maxlockdepth), then check for a
552 * cycle to see if the lock is blocked behind
553 * someone blocked behind us.
555 while (((waitblock
= (struct lockf
*)ut
->uu_wchan
) != NULL
) &&
556 ut
->uu_wmesg
== lockstr
&&
557 (i
++ < maxlockdepth
)) {
558 waitblock
= (struct lockf
*)ut
->uu_wchan
;
560 * Get the lock blocking the lock
561 * which would block us, and make
562 * certain it hasn't come unblocked
563 * (been granted, e.g. between the time
564 * we called lf_getblock, and the time
565 * we successfully acquired the
568 waitblock
= waitblock
->lf_next
;
569 if (waitblock
== NULL
)
573 * Make sure it's an advisory range
574 * lock and not any other kind of lock;
575 * if we mix lock types, it's our own
578 if ((waitblock
->lf_flags
& F_POSIX
) == 0)
582 * If the owner of the lock that's
583 * blocking a lock that's blocking us
584 * getting the requested lock, then we
585 * would deadlock, so error out.
587 bproc
= waitblock
->lf_owner
;
588 if (bproc
== lock
->lf_owner
) {
599 * For flock type locks, we must first remove
600 * any shared locks that we hold before we sleep
601 * waiting for an exclusive lock.
603 if ((lock
->lf_flags
& F_FLOCK
) &&
604 lock
->lf_type
== F_WRLCK
) {
605 lock
->lf_type
= F_UNLCK
;
606 if ((error
= lf_clearlock(lock
)) != 0) {
610 lock
->lf_type
= F_WRLCK
;
613 * Add our lock to the blocked list and sleep until we're free.
614 * Remember who blocked us (for deadlock detection).
616 lock
->lf_next
= block
;
617 TAILQ_INSERT_TAIL(&block
->lf_blkhd
, lock
, lf_block
);
619 if ( !(lock
->lf_flags
& F_FLOCK
))
620 block
->lf_flags
&= ~F_WAKE1_SAFE
;
622 #if IMPORTANCE_INHERITANCE
624 * Importance donation is done only for cases where the
625 * owning task can be unambiguously determined.
627 * POSIX type locks are not inherited by child processes;
628 * we maintain a 1:1 mapping between a lock and its owning
631 * Flock type locks are inherited across fork() and there is
632 * no 1:1 mapping in the general case. However, the fileglobs
633 * used by OFD locks *may* be confined to the process that
634 * created them, and thus have an "owner", in which case
635 * we also attempt importance donation.
637 if ((lock
->lf_flags
& block
->lf_flags
& F_POSIX
) != 0)
638 lf_boost_blocking_proc(lock
, block
);
639 else if ((lock
->lf_flags
& block
->lf_flags
& F_OFD_LOCK
) &&
640 lock
->lf_owner
!= block
->lf_owner
&&
641 NULL
!= lock
->lf_owner
&& NULL
!= block
->lf_owner
)
642 lf_boost_blocking_proc(lock
, block
);
643 #endif /* IMPORTANCE_INHERITANCE */
645 #ifdef LOCKF_DEBUGGING
646 if (lockf_debug
& LF_DBG_LOCKOP
) {
647 lf_print("lf_setlock: blocking on", block
);
648 lf_printlist("lf_setlock(block)", block
);
650 #endif /* LOCKF_DEBUGGING */
651 DTRACE_FSINFO(advlock__wait
, vnode_t
, vp
);
653 error
= msleep(lock
, &vp
->v_lock
, priority
, lockstr
, timeout
);
655 if (error
== 0 && (lock
->lf_flags
& F_ABORT
) != 0)
660 * lf_wakelock() always sets wakelock->lf_next to
661 * NULL before a wakeup; so we've been woken early
662 * - perhaps by a debugger, signal or other event.
664 * Remove 'lock' from the block list (avoids double-add
665 * in the spurious case, which would create a cycle)
667 TAILQ_REMOVE(&lock
->lf_next
->lf_blkhd
, lock
, lf_block
);
668 lock
->lf_next
= NULL
;
672 * If this was a spurious wakeup, retry
674 printf("%s: spurious wakeup, retrying lock\n",
680 if (!TAILQ_EMPTY(&lock
->lf_blkhd
)) {
681 if ((block
= lf_getblock(lock
, -1)) != NULL
)
682 lf_move_blocked(block
, lock
);
686 if (!TAILQ_EMPTY(&lock
->lf_blkhd
))
687 lf_wakelock(lock
, TRUE
);
689 /* Return ETIMEDOUT if timeout occoured. */
690 if (error
== EWOULDBLOCK
) {
698 * No blocks!! Add the lock. Note that we will
699 * downgrade or upgrade any overlapping locks this
700 * process already owns.
702 * Skip over locks owned by other processes.
703 * Handle any locks that overlap and are owned by ourselves.
709 ovcase
= lf_findoverlap(block
, lock
, SELF
, &prev
, &overlap
);
711 block
= overlap
->lf_next
;
716 * 2) overlap contains lock
717 * 3) lock contains overlap
718 * 4) overlap starts before lock
719 * 5) overlap ends after lock
725 lock
->lf_next
= overlap
;
729 case OVERLAP_EQUALS_LOCK
:
731 * If downgrading lock, others may be
732 * able to acquire it.
734 if (lock
->lf_type
== F_RDLCK
&&
735 overlap
->lf_type
== F_WRLCK
)
736 lf_wakelock(overlap
, TRUE
);
737 overlap
->lf_type
= lock
->lf_type
;
739 lock
= overlap
; /* for lf_coalesce_adjacent() */
742 case OVERLAP_CONTAINS_LOCK
:
744 * Check for common starting point and different types.
746 if (overlap
->lf_type
== lock
->lf_type
) {
748 lock
= overlap
; /* for lf_coalesce_adjacent() */
751 if (overlap
->lf_start
== lock
->lf_start
) {
753 lock
->lf_next
= overlap
;
754 overlap
->lf_start
= lock
->lf_end
+ 1;
757 * If we can't split the lock, we can't
758 * grant it. Claim a system limit for the
761 if (lf_split(overlap
, lock
)) {
766 lf_wakelock(overlap
, TRUE
);
769 case OVERLAP_CONTAINED_BY_LOCK
:
771 * If downgrading lock, others may be able to
772 * acquire it, otherwise take the list.
774 if (lock
->lf_type
== F_RDLCK
&&
775 overlap
->lf_type
== F_WRLCK
) {
776 lf_wakelock(overlap
, TRUE
);
778 while (!TAILQ_EMPTY(&overlap
->lf_blkhd
)) {
779 ltmp
= TAILQ_FIRST(&overlap
->lf_blkhd
);
780 TAILQ_REMOVE(&overlap
->lf_blkhd
, ltmp
,
782 TAILQ_INSERT_TAIL(&lock
->lf_blkhd
,
784 ltmp
->lf_next
= lock
;
788 * Add the new lock if necessary and delete the overlap.
792 lock
->lf_next
= overlap
->lf_next
;
793 prev
= &lock
->lf_next
;
796 *prev
= overlap
->lf_next
;
797 FREE(overlap
, M_LOCKF
);
800 case OVERLAP_STARTS_BEFORE_LOCK
:
802 * Add lock after overlap on the list.
804 lock
->lf_next
= overlap
->lf_next
;
805 overlap
->lf_next
= lock
;
806 overlap
->lf_end
= lock
->lf_start
- 1;
807 prev
= &lock
->lf_next
;
808 lf_wakelock(overlap
, TRUE
);
812 case OVERLAP_ENDS_AFTER_LOCK
:
814 * Add the new lock before overlap.
818 lock
->lf_next
= overlap
;
820 overlap
->lf_start
= lock
->lf_end
+ 1;
821 lf_wakelock(overlap
, TRUE
);
826 /* Coalesce adjacent locks with identical attributes */
827 lf_coalesce_adjacent(lock
);
828 #ifdef LOCKF_DEBUGGING
829 if (lockf_debug
& LF_DBG_LOCKOP
) {
830 lf_print("lf_setlock: got the lock", lock
);
831 lf_printlist("lf_setlock(out)", lock
);
833 #endif /* LOCKF_DEBUGGING */
841 * Description: Remove a byte-range lock on an vnode. Generally, find the
842 * lock (or an overlap to that lock) and remove it (or shrink
843 * it), then wakeup anyone we can.
845 * Parameters: unlock The lock to clear
850 * Notes: A caller may unlock all the locks owned by the caller by
851 * specifying the entire file range; locks owned by other
852 * callers are not effected by this operation.
855 lf_clearlock(struct lockf
*unlock
)
857 struct lockf
**head
= unlock
->lf_head
;
858 struct lockf
*lf
= *head
;
859 struct lockf
*overlap
, **prev
;
864 #ifdef LOCKF_DEBUGGING
865 if (unlock
->lf_type
!= F_UNLCK
)
866 panic("lf_clearlock: bad type");
867 if (lockf_debug
& LF_DBG_LOCKOP
)
868 lf_print("lf_clearlock", unlock
);
869 #endif /* LOCKF_DEBUGGING */
871 while ((ovcase
= lf_findoverlap(lf
, unlock
, SELF
, &prev
, &overlap
)) != OVERLAP_NONE
) {
873 * Wakeup the list of locks to be retried.
875 lf_wakelock(overlap
, FALSE
);
876 #if IMPORTANCE_INHERITANCE
877 if (overlap
->lf_boosted
== LF_BOOSTED
) {
878 lf_drop_assertion(overlap
);
880 #endif /* IMPORTANCE_INHERITANCE */
883 case OVERLAP_NONE
: /* satisfy compiler enum/switch */
886 case OVERLAP_EQUALS_LOCK
:
887 *prev
= overlap
->lf_next
;
888 FREE(overlap
, M_LOCKF
);
891 case OVERLAP_CONTAINS_LOCK
: /* split it */
892 if (overlap
->lf_start
== unlock
->lf_start
) {
893 overlap
->lf_start
= unlock
->lf_end
+ 1;
897 * If we can't split the lock, we can't grant it.
898 * Claim a system limit for the resource shortage.
900 if (lf_split(overlap
, unlock
))
902 overlap
->lf_next
= unlock
->lf_next
;
905 case OVERLAP_CONTAINED_BY_LOCK
:
906 *prev
= overlap
->lf_next
;
907 lf
= overlap
->lf_next
;
908 FREE(overlap
, M_LOCKF
);
911 case OVERLAP_STARTS_BEFORE_LOCK
:
912 overlap
->lf_end
= unlock
->lf_start
- 1;
913 prev
= &overlap
->lf_next
;
914 lf
= overlap
->lf_next
;
917 case OVERLAP_ENDS_AFTER_LOCK
:
918 overlap
->lf_start
= unlock
->lf_end
+ 1;
923 #ifdef LOCKF_DEBUGGING
924 if (lockf_debug
& LF_DBG_LOCKOP
)
925 lf_printlist("lf_clearlock", unlock
);
926 #endif /* LOCKF_DEBUGGING */
934 * Description: Check whether there is a blocking lock, and if so return
935 * its process identifier into the lock being requested.
937 * Parameters: lock Pointer to lock to test for blocks
938 * fl Pointer to flock structure to receive
939 * the blocking lock information, if a
940 * blocking lock is found.
941 * matchpid -1, or pid value to match in lookup.
946 * *fl Contents modified to reflect the
947 * blocking lock, if one is found; not
950 * Notes: fl->l_pid will be (-1) for file locks and will only be set to
951 * the blocking process ID for advisory record locks.
954 lf_getlock(struct lockf
*lock
, struct flock
*fl
, pid_t matchpid
)
958 #ifdef LOCKF_DEBUGGING
959 if (lockf_debug
& LF_DBG_LOCKOP
)
960 lf_print("lf_getlock", lock
);
961 #endif /* LOCKF_DEBUGGING */
963 if ((block
= lf_getblock(lock
, matchpid
))) {
964 fl
->l_type
= block
->lf_type
;
965 fl
->l_whence
= SEEK_SET
;
966 fl
->l_start
= block
->lf_start
;
967 if (block
->lf_end
== -1)
970 fl
->l_len
= block
->lf_end
- block
->lf_start
+ 1;
971 if (NULL
!= block
->lf_owner
) {
973 * lf_owner is only non-NULL when the lock
974 * "owner" can be unambiguously determined
976 fl
->l_pid
= proc_pid(block
->lf_owner
);
980 fl
->l_type
= F_UNLCK
;
988 * Description: Walk the list of locks for an inode and return the first
989 * blocking lock. A lock is considered blocking if we are not
990 * the lock owner; otherwise, we are permitted to upgrade or
991 * downgrade it, and it's not considered blocking.
993 * Parameters: lock The lock for which we are interested
994 * in obtaining the blocking lock, if any
995 * matchpid -1, or pid value to match in lookup.
997 * Returns: NOLOCKF No blocking lock exists
998 * !NOLOCKF The address of the blocking lock's
1001 static struct lockf
*
1002 lf_getblock(struct lockf
*lock
, pid_t matchpid
)
1004 struct lockf
**prev
, *overlap
, *lf
= *(lock
->lf_head
);
1006 for (prev
= lock
->lf_head
;
1007 lf_findoverlap(lf
, lock
, OTHERS
, &prev
, &overlap
) != OVERLAP_NONE
;
1008 lf
= overlap
->lf_next
) {
1012 * If we're matching pids, and it's a record lock,
1013 * or it's an OFD lock on a process-confined fd,
1014 * but the pid doesn't match, then keep on looking ..
1016 if (matchpid
!= -1 &&
1017 (overlap
->lf_flags
& (F_POSIX
|F_OFD_LOCK
)) != 0 &&
1018 proc_pid(overlap
->lf_owner
) != matchpid
)
1024 if ((lock
->lf_type
== F_WRLCK
|| overlap
->lf_type
== F_WRLCK
))
1034 * Description: Walk the list of locks to find an overlapping lock (if any).
1036 * Parameters: lf First lock on lock list
1037 * lock The lock we are checking for an overlap
1039 * prev pointer to pointer pointer to contain
1040 * address of pointer to previous lock
1041 * pointer to overlapping lock, if overlap
1042 * overlap pointer to pointer to contain address
1043 * of overlapping lock
1045 * Returns: OVERLAP_NONE
1046 * OVERLAP_EQUALS_LOCK
1047 * OVERLAP_CONTAINS_LOCK
1048 * OVERLAP_CONTAINED_BY_LOCK
1049 * OVERLAP_STARTS_BEFORE_LOCK
1050 * OVERLAP_ENDS_AFTER_LOCK
1053 * *prev The address of the next pointer in the
1054 * lock previous to the overlapping lock;
1055 * this is generally used to relink the
1056 * lock list, avoiding a second iteration.
1057 * *overlap The pointer to the overlapping lock
1058 * itself; this is used to return data in
1059 * the check == OTHERS case, and for the
1060 * caller to modify the overlapping lock,
1061 * in the check == SELF case
1063 * Note: This returns only the FIRST overlapping lock. There may be
1064 * more than one. lf_getlock will return the first blocking lock,
1065 * while lf_setlock will iterate over all overlapping locks to
1067 * The check parameter can be SELF, meaning we are looking for
1068 * overlapping locks owned by us, or it can be OTHERS, meaning
1069 * we are looking for overlapping locks owned by someone else so
1070 * we can report a blocking lock on an F_GETLK request.
1072 * The value of *overlap and *prev are modified, even if there is
1073 * no overlapping lock found; always check the return code.
1076 lf_findoverlap(struct lockf
*lf
, struct lockf
*lock
, int type
,
1077 struct lockf
***prev
, struct lockf
**overlap
)
1085 #ifdef LOCKF_DEBUGGING
1086 if (lockf_debug
& LF_DBG_LIST
)
1087 lf_print("lf_findoverlap: looking for overlap in", lock
);
1088 #endif /* LOCKF_DEBUGGING */
1089 start
= lock
->lf_start
;
1091 while (lf
!= NOLOCKF
) {
1092 if (((type
& SELF
) && lf
->lf_id
!= lock
->lf_id
) ||
1093 ((type
& OTHERS
) && lf
->lf_id
== lock
->lf_id
)) {
1095 * Locks belonging to one process are adjacent on the
1096 * list, so if we've found any locks belonging to us,
1097 * and we're now seeing something else, then we've
1098 * examined all "self" locks. Note that bailing out
1099 * here is quite important; for coalescing, we assume
1100 * numerically adjacent locks from the same owner to
1101 * be adjacent on the list.
1103 if ((type
& SELF
) && found_self
) {
1104 return OVERLAP_NONE
;
1107 *prev
= &lf
->lf_next
;
1108 *overlap
= lf
= lf
->lf_next
;
1112 if ((type
& SELF
)) {
1116 #ifdef LOCKF_DEBUGGING
1117 if (lockf_debug
& LF_DBG_LIST
)
1118 lf_print("\tchecking", lf
);
1119 #endif /* LOCKF_DEBUGGING */
1121 * OK, check for overlap
1123 if ((lf
->lf_end
!= -1 && start
> lf
->lf_end
) ||
1124 (end
!= -1 && lf
->lf_start
> end
)) {
1126 LOCKF_DEBUG(LF_DBG_LIST
, "no overlap\n");
1129 * NOTE: assumes that locks for the same process are
1130 * nonintersecting and ordered.
1132 if ((type
& SELF
) && end
!= -1 && lf
->lf_start
> end
)
1133 return (OVERLAP_NONE
);
1134 *prev
= &lf
->lf_next
;
1135 *overlap
= lf
= lf
->lf_next
;
1138 if ((lf
->lf_start
== start
) && (lf
->lf_end
== end
)) {
1139 LOCKF_DEBUG(LF_DBG_LIST
, "overlap == lock\n");
1140 return (OVERLAP_EQUALS_LOCK
);
1142 if ((lf
->lf_start
<= start
) &&
1144 ((lf
->lf_end
>= end
) || (lf
->lf_end
== -1))) {
1145 LOCKF_DEBUG(LF_DBG_LIST
, "overlap contains lock\n");
1146 return (OVERLAP_CONTAINS_LOCK
);
1148 if (start
<= lf
->lf_start
&&
1150 (lf
->lf_end
!= -1 && end
>= lf
->lf_end
))) {
1151 LOCKF_DEBUG(LF_DBG_LIST
, "lock contains overlap\n");
1152 return (OVERLAP_CONTAINED_BY_LOCK
);
1154 if ((lf
->lf_start
< start
) &&
1155 ((lf
->lf_end
>= start
) || (lf
->lf_end
== -1))) {
1156 LOCKF_DEBUG(LF_DBG_LIST
, "overlap starts before lock\n");
1157 return (OVERLAP_STARTS_BEFORE_LOCK
);
1159 if ((lf
->lf_start
> start
) &&
1161 ((lf
->lf_end
> end
) || (lf
->lf_end
== -1))) {
1162 LOCKF_DEBUG(LF_DBG_LIST
, "overlap ends after lock\n");
1163 return (OVERLAP_ENDS_AFTER_LOCK
);
1165 panic("lf_findoverlap: default");
1167 return (OVERLAP_NONE
);
1174 * Description: Split a lock and a contained region into two or three locks
1177 * Parameters: lock1 Lock to split
1178 * lock2 Overlapping lock region requiring the
1179 * split (upgrade/downgrade/unlock)
1181 * Returns: 0 Success
1182 * ENOLCK No memory for new lock
1185 * *lock1 Modified original lock
1186 * *lock2 Overlapping lock (inserted into list)
1187 * (new lock) Potential new lock inserted into list
1188 * if split results in 3 locks
1190 * Notes: This operation can only fail if the split would result in three
1191 * locks, and there is insufficient memory to allocate the third
1192 * lock; in that case, neither of the locks will be modified.
1195 lf_split(struct lockf
*lock1
, struct lockf
*lock2
)
1197 struct lockf
*splitlock
;
1199 #ifdef LOCKF_DEBUGGING
1200 if (lockf_debug
& LF_DBG_LIST
) {
1201 lf_print("lf_split", lock1
);
1202 lf_print("splitting from", lock2
);
1204 #endif /* LOCKF_DEBUGGING */
1206 * Check to see if splitting into only two pieces.
1208 if (lock1
->lf_start
== lock2
->lf_start
) {
1209 lock1
->lf_start
= lock2
->lf_end
+ 1;
1210 lock2
->lf_next
= lock1
;
1213 if (lock1
->lf_end
== lock2
->lf_end
) {
1214 lock1
->lf_end
= lock2
->lf_start
- 1;
1215 lock2
->lf_next
= lock1
->lf_next
;
1216 lock1
->lf_next
= lock2
;
1220 * Make a new lock consisting of the last part of
1221 * the encompassing lock
1223 MALLOC(splitlock
, struct lockf
*, sizeof *splitlock
, M_LOCKF
, M_WAITOK
);
1224 if (splitlock
== NULL
)
1226 bcopy(lock1
, splitlock
, sizeof *splitlock
);
1227 splitlock
->lf_start
= lock2
->lf_end
+ 1;
1228 TAILQ_INIT(&splitlock
->lf_blkhd
);
1229 lock1
->lf_end
= lock2
->lf_start
- 1;
1231 * OK, now link it in
1233 splitlock
->lf_next
= lock1
->lf_next
;
1234 lock2
->lf_next
= splitlock
;
1235 lock1
->lf_next
= lock2
;
1244 * Wakeup a blocklist in the case of a downgrade or unlock, since others
1245 * waiting on the lock may now be able to acquire it.
1247 * Parameters: listhead Lock list head on which waiters may
1248 * have pending locks
1252 * Notes: This function iterates a list of locks and wakes all waiters,
1253 * rather than only waiters for the contended regions. Because
1254 * of this, for heavily contended files, this can result in a
1255 * "thundering herd" situation. Refactoring the code could make
1256 * this operation more efficient, if heavy contention ever results
1257 * in a real-world performance problem.
1260 lf_wakelock(struct lockf
*listhead
, boolean_t force_all
)
1262 struct lockf
*wakelock
;
1263 boolean_t wake_all
= TRUE
;
1265 if (force_all
== FALSE
&& (listhead
->lf_flags
& F_WAKE1_SAFE
))
1268 while (!TAILQ_EMPTY(&listhead
->lf_blkhd
)) {
1269 wakelock
= TAILQ_FIRST(&listhead
->lf_blkhd
);
1270 TAILQ_REMOVE(&listhead
->lf_blkhd
, wakelock
, lf_block
);
1272 wakelock
->lf_next
= NOLOCKF
;
1273 #ifdef LOCKF_DEBUGGING
1274 if (lockf_debug
& LF_DBG_LOCKOP
)
1275 lf_print("lf_wakelock: awakening", wakelock
);
1276 #endif /* LOCKF_DEBUGGING */
1277 if (wake_all
== FALSE
) {
1279 * If there are items on the list head block list,
1280 * move them to the wakelock list instead, and then
1281 * correct their lf_next pointers.
1283 if (!TAILQ_EMPTY(&listhead
->lf_blkhd
)) {
1284 TAILQ_CONCAT(&wakelock
->lf_blkhd
, &listhead
->lf_blkhd
, lf_block
);
1286 struct lockf
*tlock
;
1288 TAILQ_FOREACH(tlock
, &wakelock
->lf_blkhd
, lf_block
) {
1289 if (TAILQ_NEXT(tlock
, lf_block
) == tlock
) {
1290 /* See rdar://10887303 */
1291 panic("cycle in wakelock list");
1293 tlock
->lf_next
= wakelock
;
1299 if (wake_all
== FALSE
)
1305 #ifdef LOCKF_DEBUGGING
1306 #define GET_LF_OWNER_PID(lf) (proc_pid((lf)->lf_owner))
1311 * Print out a lock; lock information is prefixed by the string in 'tag'
1313 * Parameters: tag A string tag for debugging
1314 * lock The lock whose information should be
1320 lf_print(const char *tag
, struct lockf
*lock
)
1322 printf("%s: lock %p for ", tag
, (void *)lock
);
1323 if (lock
->lf_flags
& F_POSIX
)
1324 printf("proc %p (owner %d)",
1325 lock
->lf_id
, GET_LF_OWNER_PID(lock
));
1326 else if (lock
->lf_flags
& F_OFD_LOCK
)
1327 printf("fg %p (owner %d)",
1328 lock
->lf_id
, GET_LF_OWNER_PID(lock
));
1330 printf("id %p", (void *)lock
->lf_id
);
1331 if (lock
->lf_vnode
!= 0)
1332 printf(" in vno %p, %s, start 0x%016llx, end 0x%016llx",
1334 lock
->lf_type
== F_RDLCK
? "shared" :
1335 lock
->lf_type
== F_WRLCK
? "exclusive" :
1336 lock
->lf_type
== F_UNLCK
? "unlock" : "unknown",
1337 (intmax_t)lock
->lf_start
, (intmax_t)lock
->lf_end
);
1339 printf(" %s, start 0x%016llx, end 0x%016llx",
1340 lock
->lf_type
== F_RDLCK
? "shared" :
1341 lock
->lf_type
== F_WRLCK
? "exclusive" :
1342 lock
->lf_type
== F_UNLCK
? "unlock" : "unknown",
1343 (intmax_t)lock
->lf_start
, (intmax_t)lock
->lf_end
);
1344 if (!TAILQ_EMPTY(&lock
->lf_blkhd
))
1345 printf(" block %p\n", (void *)TAILQ_FIRST(&lock
->lf_blkhd
));
1352 * lf_printlist DEBUG
1354 * Print out a lock list for the vnode associated with 'lock'; lock information
1355 * is prefixed by the string in 'tag'
1357 * Parameters: tag A string tag for debugging
1358 * lock The lock whose vnode's lock list should
1364 lf_printlist(const char *tag
, struct lockf
*lock
)
1366 struct lockf
*lf
, *blk
;
1368 if (lock
->lf_vnode
== 0)
1371 printf("%s: Lock list for vno %p:\n",
1372 tag
, lock
->lf_vnode
);
1373 for (lf
= lock
->lf_vnode
->v_lockf
; lf
; lf
= lf
->lf_next
) {
1374 printf("\tlock %p for ",(void *)lf
);
1375 if (lf
->lf_flags
& F_POSIX
)
1376 printf("proc %p (owner %d)",
1377 lf
->lf_id
, GET_LF_OWNER_PID(lf
));
1378 else if (lf
->lf_flags
& F_OFD_LOCK
)
1379 printf("fg %p (owner %d)",
1380 lf
->lf_id
, GET_LF_OWNER_PID(lf
));
1382 printf("id %p", (void *)lf
->lf_id
);
1383 printf(", %s, start 0x%016llx, end 0x%016llx",
1384 lf
->lf_type
== F_RDLCK
? "shared" :
1385 lf
->lf_type
== F_WRLCK
? "exclusive" :
1386 lf
->lf_type
== F_UNLCK
? "unlock" :
1387 "unknown", (intmax_t)lf
->lf_start
, (intmax_t)lf
->lf_end
);
1388 TAILQ_FOREACH(blk
, &lf
->lf_blkhd
, lf_block
) {
1389 printf("\n\t\tlock request %p for ", (void *)blk
);
1390 if (blk
->lf_flags
& F_POSIX
)
1391 printf("proc %p (owner %d)",
1392 blk
->lf_id
, GET_LF_OWNER_PID(blk
));
1393 else if (blk
->lf_flags
& F_OFD_LOCK
)
1394 printf("fg %p (owner %d)",
1395 blk
->lf_id
, GET_LF_OWNER_PID(blk
));
1397 printf("id %p", (void *)blk
->lf_id
);
1398 printf(", %s, start 0x%016llx, end 0x%016llx",
1399 blk
->lf_type
== F_RDLCK
? "shared" :
1400 blk
->lf_type
== F_WRLCK
? "exclusive" :
1401 blk
->lf_type
== F_UNLCK
? "unlock" :
1402 "unknown", (intmax_t)blk
->lf_start
,
1403 (intmax_t)blk
->lf_end
);
1404 if (!TAILQ_EMPTY(&blk
->lf_blkhd
))
1405 panic("lf_printlist: bad list");
1410 #endif /* LOCKF_DEBUGGING */
1412 #if IMPORTANCE_INHERITANCE
1417 * Call task importance hold assertion on the owner of the lock.
1419 * Parameters: block_task Owner of the lock blocking
1422 * block lock on which the current thread
1427 * Notes: The task reference on block_task is not needed to be hold since
1428 * the current thread has vnode lock and block_task has a file
1429 * lock, thus removing file lock in exit requires block_task to
1430 * grab the vnode lock.
1433 lf_hold_assertion(task_t block_task
, struct lockf
*block
)
1435 if (task_importance_hold_file_lock_assertion(block_task
, 1) == 0) {
1436 block
->lf_boosted
= LF_BOOSTED
;
1437 LOCKF_DEBUG(LF_DBG_IMPINH
,
1438 "lf: importance hold file lock assert on pid %d lock %p\n",
1439 proc_pid(block
->lf_owner
), block
);
1445 * lf_jump_to_queue_head
1447 * Jump the lock from the tail of the block queue to the head of
1450 * Parameters: block lockf struct containing the
1452 * lock lockf struct to be jumped to the
1458 lf_jump_to_queue_head(struct lockf
*block
, struct lockf
*lock
)
1460 /* Move the lock to the head of the block queue. */
1461 TAILQ_REMOVE(&block
->lf_blkhd
, lock
, lf_block
);
1462 TAILQ_INSERT_HEAD(&block
->lf_blkhd
, lock
, lf_block
);
1469 * Drops the task hold assertion.
1471 * Parameters: block lockf struct holding the assertion.
1476 lf_drop_assertion(struct lockf
*block
)
1478 LOCKF_DEBUG(LF_DBG_IMPINH
, "lf: %d: dropping assertion for lock %p\n",
1479 proc_pid(block
->lf_owner
), block
);
1481 task_t current_task
= proc_task(block
->lf_owner
);
1482 task_importance_drop_file_lock_assertion(current_task
, 1);
1483 block
->lf_boosted
= LF_NOT_BOOSTED
;
1487 lf_boost_blocking_proc(struct lockf
*lock
, struct lockf
*block
)
1489 task_t ltask
= proc_task(lock
->lf_owner
);
1490 task_t btask
= proc_task(block
->lf_owner
);
1493 * Check if ltask can donate importance. The
1494 * check of imp_donor bit is done without holding
1495 * any lock. The value may change after you read it,
1496 * but it is ok to boost a task while someone else is
1499 * TODO: Support live inheritance on file locks.
1501 if (task_is_importance_donor(ltask
)) {
1502 LOCKF_DEBUG(LF_DBG_IMPINH
,
1503 "lf: %d: attempt to boost pid %d that holds lock %p\n",
1504 proc_pid(lock
->lf_owner
), proc_pid(block
->lf_owner
), block
);
1506 if (block
->lf_boosted
!= LF_BOOSTED
&&
1507 task_is_importance_receiver_type(btask
)) {
1508 lf_hold_assertion(btask
, block
);
1510 lf_jump_to_queue_head(block
, lock
);
1513 #endif /* IMPORTANCE_INHERITANCE */