]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lockf.c
2 * Copyright (c) 1999,2001-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 /* (c) 1997-1998,2001 Apple Computer, Inc. All Rights Reserved */
28 * Copyright (c) 1982, 1986, 1989, 1993
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software contributed to Berkeley by
32 * Scooter Morris at Genentech Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * derived from @(#)ufs_lockf.c 8.4 (Berkeley) 10/26/94
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
71 #include <sys/vnode.h>
72 #include <sys/malloc.h>
73 #include <sys/fcntl.h>
75 #include "hfs_cnode.h"
76 #include "hfs_lockf.h"
79 * This variable controls the maximum number of processes that will
80 * be checked in doing deadlock detection.
82 int hfsmaxlockdepth
= MAXDEPTH
;
86 #include <sys/sysctl.h>
88 struct ctldebug debug4
= { "lockf_debug", &lockf_debug
};
91 #define NOLOCKF (struct hfslockf *)0
96 * Set a byte-range lock.
100 register struct hfslockf
*lock
;
102 register struct hfslockf
*block
;
103 struct filefork
*fork
= lock
->lf_fork
;
104 struct hfslockf
**prev
, *overlap
, *ltmp
;
105 static char lockstr
[] = "hfslockf";
106 int ovcase
, priority
, needtolink
, error
;
110 hfs_lprint("hfs_setlock", lock
);
111 #endif /* LOCKF_DEBUG */
117 if (lock
->lf_type
== F_WRLCK
)
121 * Scan lock list for this file looking for locks that would block us.
123 while ((block
= hfs_getblock(lock
))) {
125 * Free the structure and return if nonblocking.
127 if ((lock
->lf_flags
& F_WAIT
) == 0) {
132 * We are blocked. Since flock style locks cover
133 * the whole file, there is no chance for deadlock.
134 * For byte-range locks we must check for deadlock.
136 * Deadlock detection is done by looking through the
137 * wait channels to see if there are any cycles that
138 * involve us. MAXDEPTH is set just to make sure we
139 * do not go off into neverland.
141 if ((lock
->lf_flags
& F_POSIX
) &&
142 (block
->lf_flags
& F_POSIX
)) {
143 register struct proc
*wproc
;
144 register struct hfslockf
*waitblock
;
147 /* The block is waiting on something */
148 wproc
= (struct proc
*)block
->lf_id
;
149 while (wproc
->p_wchan
&&
150 (wproc
->p_wmesg
== lockstr
) &&
151 (i
++ < hfsmaxlockdepth
)) {
152 waitblock
= (struct hfslockf
*)wproc
->p_wchan
;
153 /* Get the owner of the blocking lock */
154 waitblock
= waitblock
->lf_next
;
155 if ((waitblock
->lf_flags
& F_POSIX
) == 0)
157 wproc
= (struct proc
*)waitblock
->lf_id
;
158 if (wproc
== (struct proc
*)lock
->lf_id
) {
159 _FREE(lock
, M_LOCKF
);
165 * For flock type locks, we must first remove
166 * any shared locks that we hold before we sleep
167 * waiting for an exclusive lock.
169 if ((lock
->lf_flags
& F_FLOCK
) &&
170 lock
->lf_type
== F_WRLCK
) {
171 lock
->lf_type
= F_UNLCK
;
172 (void) hfs_clearlock(lock
);
173 lock
->lf_type
= F_WRLCK
;
176 * Add our lock to the blocked list and sleep until we're free.
177 * Remember who blocked us (for deadlock detection).
179 lock
->lf_next
= block
;
180 TAILQ_INSERT_TAIL(&block
->lf_blkhd
, lock
, lf_block
);
182 if (lockf_debug
& 1) {
183 hfs_lprint("hfs_setlock: blocking on", block
);
184 hfs_lprintlist("hfs_setlock", block
);
186 #endif /* LOCKF_DEBUG */
187 if ((error
= tsleep((caddr_t
)lock
, priority
, lockstr
, 0))) {
189 * We may have been awakened by a signal (in
190 * which case we must remove ourselves from the
191 * blocked list) and/or by another process
192 * releasing a lock (in which case we have already
193 * been removed from the blocked list and our
194 * lf_next field set to NOLOCKF).
197 TAILQ_REMOVE(&lock
->lf_next
->lf_blkhd
, lock
,
199 _FREE(lock
, M_LOCKF
);
204 * No blocks!! Add the lock. Note that we will
205 * downgrade or upgrade any overlapping locks this
206 * process already owns.
208 * Skip over locks owned by other processes.
209 * Handle any locks that overlap and are owned by ourselves.
211 prev
= &fork
->ff_lockf
;
212 block
= fork
->ff_lockf
;
215 if ((ovcase
= hfs_findoverlap(block
, lock
, SELF
, &prev
, &overlap
)))
216 block
= overlap
->lf_next
;
221 * 2) overlap contains lock
222 * 3) lock contains overlap
223 * 4) overlap starts before lock
224 * 5) overlap ends after lock
227 case 0: /* no overlap */
230 lock
->lf_next
= overlap
;
234 case 1: /* overlap == lock */
236 * If downgrading lock, others may be
237 * able to acquire it.
239 if (lock
->lf_type
== F_RDLCK
&&
240 overlap
->lf_type
== F_WRLCK
)
241 hfs_wakelock(overlap
);
242 overlap
->lf_type
= lock
->lf_type
;
244 lock
= overlap
; /* for debug output below */
247 case 2: /* overlap contains lock */
249 * Check for common starting point and different types.
251 if (overlap
->lf_type
== lock
->lf_type
) {
252 _FREE(lock
, M_LOCKF
);
253 lock
= overlap
; /* for debug output below */
256 if (overlap
->lf_start
== lock
->lf_start
) {
258 lock
->lf_next
= overlap
;
259 overlap
->lf_start
= lock
->lf_end
+ 1;
261 hfs_split(overlap
, lock
);
262 hfs_wakelock(overlap
);
265 case 3: /* lock contains overlap */
267 * If downgrading lock, others may be able to
268 * acquire it, otherwise take the list.
270 if (lock
->lf_type
== F_RDLCK
&&
271 overlap
->lf_type
== F_WRLCK
) {
272 hfs_wakelock(overlap
);
274 while ((ltmp
= overlap
->lf_blkhd
.tqh_first
)) {
275 TAILQ_REMOVE(&overlap
->lf_blkhd
, ltmp
,
277 TAILQ_INSERT_TAIL(&lock
->lf_blkhd
,
282 * Add the new lock if necessary and delete the overlap.
286 lock
->lf_next
= overlap
->lf_next
;
287 prev
= &lock
->lf_next
;
290 *prev
= overlap
->lf_next
;
291 _FREE(overlap
, M_LOCKF
);
294 case 4: /* overlap starts before lock */
296 * Add lock after overlap on the list.
298 lock
->lf_next
= overlap
->lf_next
;
299 overlap
->lf_next
= lock
;
300 overlap
->lf_end
= lock
->lf_start
- 1;
301 prev
= &lock
->lf_next
;
302 hfs_wakelock(overlap
);
306 case 5: /* overlap ends after lock */
308 * Add the new lock before overlap.
312 lock
->lf_next
= overlap
;
314 overlap
->lf_start
= lock
->lf_end
+ 1;
315 hfs_wakelock(overlap
);
321 if (lockf_debug
& 1) {
322 hfs_lprint("hfs_setlock: got the lock", lock
);
323 hfs_lprintlist("hfs_setlock", lock
);
325 #endif /* LOCKF_DEBUG */
330 * Remove a file fork's byte-range lock.
332 * Generally, find the lock (or an overlap to that lock)
333 * and remove it (or shrink it), then wakeup anyone we can.
336 hfs_clearlock(unlock
)
337 register struct hfslockf
*unlock
;
339 struct filefork
*fork
= unlock
->lf_fork
;
340 register struct hfslockf
*lf
= fork
->ff_lockf
;
341 struct hfslockf
*overlap
, **prev
;
347 if (unlock
->lf_type
!= F_UNLCK
)
348 panic("hfs_clearlock: bad type");
350 hfs_lprint("hfs_clearlock", unlock
);
351 #endif /* LOCKF_DEBUG */
352 prev
= &fork
->ff_lockf
;
353 while ((ovcase
= hfs_findoverlap(lf
, unlock
, SELF
, &prev
, &overlap
))) {
355 * Wakeup the list of locks to be retried.
357 hfs_wakelock(overlap
);
361 case 1: /* overlap == lock */
362 *prev
= overlap
->lf_next
;
363 FREE(overlap
, M_LOCKF
);
366 case 2: /* overlap contains lock: split it */
367 if (overlap
->lf_start
== unlock
->lf_start
) {
368 overlap
->lf_start
= unlock
->lf_end
+ 1;
371 hfs_split(overlap
, unlock
);
372 overlap
->lf_next
= unlock
->lf_next
;
375 case 3: /* lock contains overlap */
376 *prev
= overlap
->lf_next
;
377 lf
= overlap
->lf_next
;
378 _FREE(overlap
, M_LOCKF
);
381 case 4: /* overlap starts before lock */
382 overlap
->lf_end
= unlock
->lf_start
- 1;
383 prev
= &overlap
->lf_next
;
384 lf
= overlap
->lf_next
;
387 case 5: /* overlap ends after lock */
388 overlap
->lf_start
= unlock
->lf_end
+ 1;
395 hfs_lprintlist("hfs_clearlock", unlock
);
396 #endif /* LOCKF_DEBUG */
401 * Check whether there is a blocking lock,
402 * and if so return its process identifier.
405 hfs_getlock(lock
, fl
)
406 register struct hfslockf
*lock
;
407 register struct flock
*fl
;
409 register struct hfslockf
*block
;
413 hfs_lprint("hfs_getlock", lock
);
414 #endif /* LOCKF_DEBUG */
416 if ((block
= hfs_getblock(lock
))) {
417 fl
->l_type
= block
->lf_type
;
418 fl
->l_whence
= SEEK_SET
;
419 fl
->l_start
= block
->lf_start
;
420 if (block
->lf_end
== -1)
423 fl
->l_len
= block
->lf_end
- block
->lf_start
+ 1;
424 if (block
->lf_flags
& F_POSIX
)
425 fl
->l_pid
= ((struct proc
*)(block
->lf_id
))->p_pid
;
429 fl
->l_type
= F_UNLCK
;
435 * Walk a file fork's list of locks and
436 * return the first blocking lock.
440 register struct hfslockf
*lock
;
442 struct hfslockf
**prev
, *overlap
, *lf
= lock
->lf_fork
->ff_lockf
;
445 prev
= &lock
->lf_fork
->ff_lockf
;
446 while ((ovcase
= hfs_findoverlap(lf
, lock
, OTHERS
, &prev
, &overlap
))) {
448 * We've found an overlap, see if it blocks us
450 if ((lock
->lf_type
== F_WRLCK
|| overlap
->lf_type
== F_WRLCK
))
453 * Nope, point to the next one on the list and
454 * see if it blocks us
456 lf
= overlap
->lf_next
;
462 * Walk a file fork's list of locks to
463 * find an overlapping lock (if any).
465 * NOTE: this returns only the FIRST overlapping lock. There
466 * may be more than one.
469 hfs_findoverlap(lf
, lock
, type
, prev
, overlap
)
470 register struct hfslockf
*lf
;
471 struct hfslockf
*lock
;
473 struct hfslockf
***prev
;
474 struct hfslockf
**overlap
;
483 hfs_lprint("hfs_findoverlap: looking for overlap in", lock
);
484 #endif /* LOCKF_DEBUG */
485 start
= lock
->lf_start
;
487 while (lf
!= NOLOCKF
) {
488 if (((type
& SELF
) && lf
->lf_id
!= lock
->lf_id
) ||
489 ((type
& OTHERS
) && lf
->lf_id
== lock
->lf_id
)) {
490 *prev
= &lf
->lf_next
;
491 *overlap
= lf
= lf
->lf_next
;
496 hfs_lprint("\tchecking", lf
);
497 #endif /* LOCKF_DEBUG */
499 * OK, check for overlap
504 * 2) overlap contains lock
505 * 3) lock contains overlap
506 * 4) overlap starts before lock
507 * 5) overlap ends after lock
509 if ((lf
->lf_end
!= -1 && start
> lf
->lf_end
) ||
510 (end
!= -1 && lf
->lf_start
> end
)) {
514 printf("no overlap\n");
515 #endif /* LOCKF_DEBUG */
516 if ((type
& SELF
) && end
!= -1 && lf
->lf_start
> end
)
518 *prev
= &lf
->lf_next
;
519 *overlap
= lf
= lf
->lf_next
;
522 if ((lf
->lf_start
== start
) && (lf
->lf_end
== end
)) {
526 printf("overlap == lock\n");
527 #endif /* LOCKF_DEBUG */
530 if ((lf
->lf_start
<= start
) &&
532 ((lf
->lf_end
>= end
) || (lf
->lf_end
== -1))) {
536 printf("overlap contains lock\n");
537 #endif /* LOCKF_DEBUG */
540 if (start
<= lf
->lf_start
&&
542 (lf
->lf_end
!= -1 && end
>= lf
->lf_end
))) {
546 printf("lock contains overlap\n");
547 #endif /* LOCKF_DEBUG */
550 if ((lf
->lf_start
< start
) &&
551 ((lf
->lf_end
>= start
) || (lf
->lf_end
== -1))) {
555 printf("overlap starts before lock\n");
556 #endif /* LOCKF_DEBUG */
559 if ((lf
->lf_start
> start
) &&
561 ((lf
->lf_end
> end
) || (lf
->lf_end
== -1))) {
565 printf("overlap ends after lock\n");
566 #endif /* LOCKF_DEBUG */
569 panic("hfs_findoverlap: default");
575 * Split a lock and a contained region into
576 * two or three locks as necessary.
579 hfs_split(lock1
, lock2
)
580 register struct hfslockf
*lock1
;
581 register struct hfslockf
*lock2
;
583 register struct hfslockf
*splitlock
;
586 if (lockf_debug
& 2) {
587 hfs_lprint("hfs_split", lock1
);
588 hfs_lprint("splitting from", lock2
);
590 #endif /* LOCKF_DEBUG */
592 * Check to see if spliting into only two pieces.
594 if (lock1
->lf_start
== lock2
->lf_start
) {
595 lock1
->lf_start
= lock2
->lf_end
+ 1;
596 lock2
->lf_next
= lock1
;
599 if (lock1
->lf_end
== lock2
->lf_end
) {
600 lock1
->lf_end
= lock2
->lf_start
- 1;
601 lock2
->lf_next
= lock1
->lf_next
;
602 lock1
->lf_next
= lock2
;
606 * Make a new lock consisting of the last part of
607 * the encompassing lock
609 MALLOC(splitlock
, struct hfslockf
*, sizeof *splitlock
, M_LOCKF
, M_WAITOK
);
610 bcopy((caddr_t
)lock1
, (caddr_t
)splitlock
, sizeof *splitlock
);
611 splitlock
->lf_start
= lock2
->lf_end
+ 1;
612 TAILQ_INIT(&splitlock
->lf_blkhd
);
613 lock1
->lf_end
= lock2
->lf_start
- 1;
617 splitlock
->lf_next
= lock1
->lf_next
;
618 lock2
->lf_next
= splitlock
;
619 lock1
->lf_next
= lock2
;
626 hfs_wakelock(listhead
)
627 struct hfslockf
*listhead
;
629 register struct hfslockf
*wakelock
;
631 while ((wakelock
= listhead
->lf_blkhd
.tqh_first
)) {
632 TAILQ_REMOVE(&listhead
->lf_blkhd
, wakelock
, lf_block
);
633 wakelock
->lf_next
= NOLOCKF
;
636 hfs_lprint("hfs_wakelock: awakening", wakelock
);
637 #endif /* LOCKF_DEBUG */
638 wakeup((caddr_t
)wakelock
);
646 hfs_lprint(tag
, lock
)
648 register struct hfslockf
*lock
;
651 printf("%s: lock 0x%lx for ", tag
, lock
);
652 if (lock
->lf_flags
& F_POSIX
)
653 printf("proc %d", ((struct proc
*)(lock
->lf_id
))->p_pid
);
655 printf("id 0x%x", lock
->lf_id
);
656 printf(" in ino %d on dev <%d, %d>, %s, start %d, end %d",
657 FTOC(lock
->lf_fork
)->c_fileid
,
658 major(FTOC(lock
->lf_fork
)->c_dev
),
659 minor(FTOC(lock
->lf_fork
)->c_dev
),
660 lock
->lf_type
== F_RDLCK
? "shared" :
661 lock
->lf_type
== F_WRLCK
? "exclusive" :
662 lock
->lf_type
== F_UNLCK
? "unlock" :
663 "unknown", lock
->lf_start
, lock
->lf_end
);
664 if (lock
->lf_blkhd
.tqh_first
)
665 printf(" block 0x%x\n", lock
->lf_blkhd
.tqh_first
);
670 hfs_lprintlist(tag
, lock
)
672 struct hfslockf
*lock
;
674 register struct hfslockf
*lf
, *blk
;
676 printf("%s: Lock list for ino %d on dev <%d, %d>:\n",
677 tag
, FTOC(lock
->lf_fork
)->i_number
,
678 major(FTOC(lock
->lf_fork
)->c_dev
),
679 minor(FTOC(lock
->lf_fork
)->c_dev
));
680 for (lf
= lock
->lf_fork
->ff_lockf
; lf
; lf
= lf
->lf_next
) {
681 printf("\tlock 0x%lx for ", lf
);
682 if (lf
->lf_flags
& F_POSIX
)
683 printf("proc %d", ((struct proc
*)(lf
->lf_id
))->p_pid
);
685 printf("id 0x%x", lf
->lf_id
);
686 printf(", %s, start %d, end %d",
687 lf
->lf_type
== F_RDLCK
? "shared" :
688 lf
->lf_type
== F_WRLCK
? "exclusive" :
689 lf
->lf_type
== F_UNLCK
? "unlock" :
690 "unknown", lf
->lf_start
, lf
->lf_end
);
691 for (blk
= lf
->lf_blkhd
.tqh_first
; blk
;
692 blk
= blk
->lf_block
.tqe_next
) {
693 printf("\n\t\tlock request 0x%lx for ", blk
);
694 if (blk
->lf_flags
& F_POSIX
)
696 ((struct proc
*)(blk
->lf_id
))->p_pid
);
698 printf("id 0x%x", blk
->lf_id
);
699 printf(", %s, start %d, end %d",
700 blk
->lf_type
== F_RDLCK
? "shared" :
701 blk
->lf_type
== F_WRLCK
? "exclusive" :
702 blk
->lf_type
== F_UNLCK
? "unlock" :
703 "unknown", blk
->lf_start
, blk
->lf_end
);
704 if (blk
->lf_blkhd
.tqh_first
)
705 panic("hfs_lprintlist: bad list");
710 #endif /* LOCKF_DEBUG */