2 * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Scooter Morris at Genentech Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
39 * $FreeBSD: src/sys/kern/kern_lockf.c,v 1.25 1999/11/16 16:28:56 phk Exp $
40 * $DragonFly: src/sys/kern/kern_lockf.c,v 1.9 2004/05/05 09:17:04 joerg Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
48 #include <sys/unistd.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
52 #include <sys/resourcevar.h>
54 #include <sys/lockf.h>
55 #include <machine/limits.h> /* for LLONG_MAX */
58 int lf_global_counter = 0;
61 int lf_print_ranges = 0;
63 static void lf_print_lock(const struct lockf *);
66 static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
68 static void lf_wakeup(struct lockf *, off_t, off_t);
69 static int lf_overlap(const struct lockf_range *, off_t, off_t);
70 static int lf_overlap_left(const struct lockf_range *, off_t, off_t);
71 static int lf_overlap_right(const struct lockf_range *, off_t, off_t);
72 static int lf_overlap_left2(const struct lockf_range *, off_t, off_t);
73 static int lf_overlap_right2(const struct lockf_range *, off_t, off_t);
74 static int lf_overlap_embedded(const struct lockf_range *, off_t, off_t);
75 static struct lockf_range *lf_alloc_range(void);
76 static void lf_create_range(struct lockf_range *, struct proc *, int, int,
78 static void lf_destroy_range(struct lockf_range *, int);
80 static int lf_setlock(struct lockf *, struct proc *, int, int,
82 static int lf_clearlock(struct lockf *, struct proc *, int, int,
84 static int lf_getlock(struct flock *, struct lockf *, struct proc *,
85 int, int, off_t, off_t);
87 static int lf_count_change(struct proc *, int);
90 * Change the POSIX lock accounting for the given process.
93 lf_count_adjust(struct proc *p, struct uidinfo *newowner)
99 uip = p->p_ucred->cr_uidinfo;
101 newowner->ui_posixlocks += p->p_numposixlocks;
102 uip->ui_posixlocks -= p->p_numposixlocks;
104 KASSERT(uip->ui_posixlocks >= 0,
105 ("Negative number of POSIX locks held by old user: %d.",
106 uip->ui_posixlocks));
107 KASSERT(newowner->ui_posixlocks >= 0,
108 ("Negative number of POSIX locks held by new user: %d.",
109 newowner->ui_posixlocks));
113 lf_count_change(struct proc *owner, int diff)
118 /* we might actually not have a process context */
122 uip = owner->p_ucred->cr_uidinfo;
124 max = MIN(owner->p_rlimit[RLIMIT_POSIXLOCKS].rlim_cur,
125 maxposixlocksperuid);
126 if (diff > 0 && owner->p_ucred->cr_uid != 0 && max != -1 &&
127 uip->ui_posixlocks >= max )
130 uip->ui_posixlocks += diff;
132 KASSERT(uip->ui_posixlocks >= 0,
133 ("Negative number of POSIX locks held by user: %d.",
134 uip->ui_posixlocks));
140 * Advisory record locking support
143 lf_advlock(struct vop_advlock_args *ap, struct lockf *lock, u_quad_t size)
145 struct flock *fl = ap->a_fl;
148 int type, flags, error;
151 if (lock->init_done == 0) {
152 TAILQ_INIT(&lock->lf_range);
153 TAILQ_INIT(&lock->lf_blocked);
158 * Convert the flock structure into a start and end.
160 switch (fl->l_whence) {
164 * Caller is responsible for adding any necessary offset
165 * when SEEK_CUR is used.
171 start = size + fl->l_start;
179 if (fl->l_len == 0) {
183 end = start + fl->l_len - 1;
191 * This isn't really correct for flock-style locks,
192 * but the current handling is somewhat broken anyway.
194 owner = (struct proc *)ap->a_id;
197 * Do the requested operation.
199 lwkt_gettoken(&ilock, lwkt_token_pool_get(lock));
202 error = lf_setlock(lock, owner, type, flags, start, end);
206 error = lf_clearlock(lock, owner, type, flags, start, end);
210 error = lf_getlock(fl, lock, owner, type, flags, start, end);
217 lwkt_reltoken(&ilock);
222 lf_setlock(struct lockf *lock, struct proc *owner, int type, int flags,
223 off_t start, off_t end)
225 struct lockf_range *range, *first_match, *insert_point;
226 int wakeup_needed, lock_needed;
227 /* pre-allocation to avoid blocking in the middle of the algorithm */
228 struct lockf_range *new_range1 = NULL, *new_range2 = NULL;
231 /* for restauration in case of hitting the POSIX lock limit below */
232 struct lockf_range *orig_first_match = NULL;
237 if (new_range1 == NULL)
238 new_range1 = lf_alloc_range();
239 if (new_range2 == NULL)
240 new_range2 = lf_alloc_range();
250 TAILQ_FOREACH(range, &lock->lf_range, lf_link) {
251 if (insert_point == NULL && range->lf_start >= start)
252 insert_point = range;
253 if (lf_overlap(range, start, end) == 0)
255 if (range->lf_owner == owner) {
256 if (first_match == NULL)
260 if (type == F_WRLCK || range->lf_type == F_WRLCK)
265 struct lockf_range *brange;
268 if ((flags & F_WAIT) == 0) {
274 * We are blocked. For POSIX locks we have to check
275 * for deadlocks and return with EDEADLK. This is done
276 * by checking wether range->lf_owner is already
279 * Since flock-style locks cover the whole file, a
280 * deadlock between those is nearly impossible.
281 * This can only occur if a process tries to lock the
282 * same inode exclusively while holding a shared lock
283 * with another descriptor.
284 * XXX How can we cleanly detect this?
285 * XXX The current mixing of flock & fcntl/lockf is evil.
287 * Handle existing locks of flock-style like POSIX locks.
289 if (flags & F_POSIX) {
290 TAILQ_FOREACH(brange, &lock->lf_blocked, lf_link)
291 if (brange->lf_owner == range->lf_owner) {
298 * For flock-style locks, we must first remove
299 * any shared locks that we hold before we sleep
300 * waiting for an exclusive lock.
302 if ((flags & F_FLOCK) && type == F_WRLCK)
303 lf_clearlock(lock, owner, type, flags, start, end);
307 lf_create_range(brange, owner, type, 0, start, end, 0);
308 TAILQ_INSERT_TAIL(&lock->lf_blocked, brange, lf_link);
309 error = tsleep(brange, PCATCH, "lockf", 0);
312 * We may have been awaked by a signal and/or by a
313 * debugger continuing us (in which case we must remove
314 * ourselves from the blocked list) and/or by another
315 * process releasing/downgrading a lock (in which case
316 * we have already been removed from the blocked list
317 * and our lf_flags field is 1).
319 if (brange->lf_flags == 0)
320 TAILQ_REMOVE(&lock->lf_blocked, brange, lf_link);
321 lf_destroy_range(brange, 0);
328 if (first_match == NULL) {
329 if (flags & F_POSIX) {
330 if (lf_count_change(owner, 1)) {
337 lf_create_range(range, owner, type, flags, start, end, 1);
338 if (insert_point != NULL)
339 TAILQ_INSERT_BEFORE(insert_point, range, lf_link);
341 TAILQ_INSERT_TAIL(&lock->lf_range, range, lf_link);
347 if (lf_overlap_left(first_match, start, end)) {
348 KKASSERT((flags & F_POSIX) != 0);
349 if (first_match->lf_end > end) {
350 if (first_match->lf_type == type)
352 if (lf_count_change(owner, 2)) {
358 lf_create_range(range, owner, type, flags,
360 if (insert_point != NULL)
361 TAILQ_INSERT_BEFORE(insert_point, range,
364 TAILQ_INSERT_TAIL(&lock->lf_range, range,
366 insert_point = range;
369 lf_create_range(range, owner, first_match->lf_type,
370 first_match->lf_flags, end + 1,
371 first_match->lf_end, 1);
372 TAILQ_INSERT_AFTER(&lock->lf_range, insert_point,
374 first_match->lf_flags &= ~F_NOEND;
375 first_match->lf_end = start - 1;
381 * left match, but not right match
383 * handle the lf_type != type case directly,
384 * merge the other case with the !lock_needed path.
386 if (first_match->lf_type != type) {
388 * This is needed if the lockf acquisition below fails.
390 orig_first_match = first_match;
391 orig_end = first_match->lf_end;
392 orig_flags = first_match->lf_flags;
393 first_match->lf_end = start - 1;
394 first_match->lf_flags &= ~F_NOEND;
397 /* Try to find the next matching range */
398 range = TAILQ_NEXT(first_match, lf_link);
399 while (range != NULL) {
400 if (range->lf_owner == owner &&
401 lf_overlap(range, start, end))
403 range = TAILQ_NEXT(range, lf_link);
408 /* fall through to !left_match behaviour */
410 first_match->lf_end = end;
411 first_match->lf_flags |= flags & F_NOEND;
416 if (lf_overlap_embedded(first_match, start, end)) {
417 if (first_match != insert_point) {
418 TAILQ_REMOVE(&lock->lf_range, first_match, lf_link);
419 TAILQ_INSERT_BEFORE(insert_point, first_match, lf_link);
421 first_match->lf_start = start;
422 first_match->lf_end = end;
423 first_match->lf_flags |= flags & F_NOEND;
427 if (lock_needed == 0) {
428 struct lockf_range *nrange;
430 range = TAILQ_NEXT(first_match, lf_link);
431 while (range != NULL) {
432 if (range->lf_owner != owner) {
433 range = TAILQ_NEXT(range, lf_link);
436 if (lf_overlap_embedded(range, start, end)) {
437 nrange = TAILQ_NEXT(range, lf_link);
438 TAILQ_REMOVE(&lock->lf_range, range,
440 lf_count_change(owner, -1);
441 lf_destroy_range(range, 1);
445 if (lf_overlap_right(range, start, end) == 0) {
446 range = TAILQ_NEXT(range, lf_link);
449 if (range->lf_type != type) {
450 range->lf_start = end + 1;
451 nrange = TAILQ_NEXT(range, lf_link);
452 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
453 while (nrange != NULL) {
454 if (nrange->lf_start >= end + 1)
456 nrange = TAILQ_NEXT(nrange, lf_link);
459 TAILQ_INSERT_BEFORE(nrange, range,
462 TAILQ_INSERT_TAIL(&lock->lf_range,
466 first_match->lf_end = range->lf_end;
467 first_match->lf_flags |=
468 range->lf_flags & F_NOEND;
469 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
470 lf_count_change(owner, -1);
471 lf_destroy_range(range, 1);
477 if (lf_overlap_right(first_match, start, end)) {
478 KKASSERT((flags & F_POSIX) != 0);
479 if (first_match->lf_type == type) {
480 first_match->lf_start = start;
481 if (first_match != insert_point) {
482 TAILQ_REMOVE(&lock->lf_range, first_match,
484 TAILQ_INSERT_BEFORE(insert_point, first_match,
489 if (lf_count_change(owner, 1)) {
490 if (orig_first_match != NULL) {
491 orig_first_match->lf_end = orig_end;
492 orig_first_match->lf_flags = orig_end;
497 first_match->lf_start = end + 1;
498 KKASSERT(new_range1 != NULL);
501 lf_create_range(range, owner, type, flags, start, end, 1);
502 TAILQ_INSERT_BEFORE(insert_point, range, lf_link);
503 range = TAILQ_NEXT(first_match, lf_link);
504 TAILQ_REMOVE(&lock->lf_range, first_match, lf_link);
505 while (range != NULL) {
506 if (range->lf_start >= first_match->lf_start)
508 range = TAILQ_NEXT(range, lf_link);
511 TAILQ_INSERT_BEFORE(range, first_match, lf_link);
513 TAILQ_INSERT_TAIL(&lock->lf_range, first_match, lf_link);
523 lf_wakeup(lock, start, end);
526 if (new_range1 != NULL)
527 lf_destroy_range(new_range1, 0);
528 if (new_range2 != NULL)
529 lf_destroy_range(new_range2, 0);
534 lf_clearlock(struct lockf *lock, struct proc *owner, int type, int flags,
535 off_t start, off_t end)
537 struct lockf_range *range, *trange;
538 struct lockf_range *new_range;
541 new_range = lf_alloc_range();
543 TAILQ_FOREACH_MUTABLE(range, &lock->lf_range, lf_link, trange) {
544 if (range->lf_end < start)
546 if (range->lf_start > end)
548 if (range->lf_owner != owner)
550 if (lf_overlap_embedded(range, start, end)) {
551 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
552 /* flock-locks are equal */
553 if (range->lf_flags & F_POSIX)
554 lf_count_change(owner, -1);
555 lf_destroy_range(range, 1);
558 if (lf_overlap_left2(range, start, end)) {
559 KKASSERT(range->lf_flags & F_POSIX);
560 if (lf_overlap_right2(range, start, end)) {
561 struct lockf_range *nrange;
563 if (lf_count_change(owner, 1)) {
569 lf_create_range(nrange, nrange->lf_owner,
570 range->lf_type, range->lf_flags,
571 end + 1, range->lf_end, 1);
572 range->lf_end = start;
573 range->lf_flags &= ~F_NOEND;
574 for (; range != NULL;
575 range = TAILQ_NEXT(range, lf_link))
576 if (range->lf_start >= nrange->lf_start)
579 TAILQ_INSERT_BEFORE(range, nrange,
582 TAILQ_INSERT_TAIL(&lock->lf_range,
586 range->lf_end = start - 1;
587 range->lf_flags &= ~F_NOEND;
590 if (lf_overlap_right2(range, start, end)) {
591 struct lockf_range *nrange = range;
593 KKASSERT(range->lf_flags & F_POSIX);
595 range = TAILQ_NEXT(range, lf_link);
596 TAILQ_REMOVE(&lock->lf_range, nrange, lf_link);
597 for (; range != NULL;
598 range = TAILQ_NEXT(range, lf_link))
599 if (range->lf_start >= nrange->lf_start)
602 TAILQ_INSERT_BEFORE(range, nrange, lf_link);
604 TAILQ_INSERT_TAIL(&lock->lf_range, nrange,
610 lf_wakeup(lock, start, end);
614 if (new_range != NULL)
615 lf_destroy_range(new_range, 0);
621 * Check whether there is a blocking lock,
622 * and if so return its process identifier.
625 lf_getlock(struct flock *fl, struct lockf *lock, struct proc *owner,
626 int type, int flags, off_t start, off_t end)
628 struct lockf_range *range;
630 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
631 if (range->lf_owner != owner &&
632 lf_overlap(range, start, end) &&
633 (type == F_WRLCK || range->lf_type == F_WRLCK))
636 fl->l_type = F_UNLCK;
639 fl->l_type = range->lf_type;
640 fl->l_whence = SEEK_SET;
641 fl->l_start = range->lf_start;
642 if (range->lf_flags & F_NOEND)
645 fl->l_len = range->lf_end - range->lf_start + 1;
646 if (range->lf_owner != NULL && (range->lf_flags & F_POSIX))
647 fl->l_pid = range->lf_owner->p_pid;
654 * Check wether range and [start, end] overlap.
657 lf_overlap(const struct lockf_range *range, off_t start, off_t end)
659 if (range->lf_start >= start && range->lf_start <= end)
661 else if (start >= range->lf_start && start <= range->lf_end)
668 * Wakeup pending lock attempts.
671 lf_wakeup(struct lockf *lock, off_t start, off_t end)
673 struct lockf_range *range, *nrange;
674 TAILQ_FOREACH_MUTABLE(range, &lock->lf_blocked, lf_link, nrange) {
675 if (lf_overlap(range, start, end) == 0)
677 TAILQ_REMOVE(&lock->lf_blocked, range, lf_link);
684 lf_overlap_left(const struct lockf_range *range, off_t start, off_t end)
686 if (range->lf_start < start && range->lf_end >= start - 1 &&
687 range->lf_end <= end)
695 lf_overlap_right(const struct lockf_range *range, off_t start, off_t end)
697 if (range->lf_end > end && range->lf_start >= start &&
698 range->lf_start - 1 <= end)
705 lf_overlap_left2(const struct lockf_range *range, off_t start, off_t end)
707 if (range->lf_start < start && range->lf_end >= start &&
708 range->lf_end <= end)
716 lf_overlap_right2(const struct lockf_range *range, off_t start, off_t end)
718 if (range->lf_end > end && range->lf_start >= start &&
719 range->lf_start <= end)
726 lf_overlap_embedded(const struct lockf_range *range, off_t start, off_t end)
728 if (range->lf_start >= start && range->lf_end <= end)
734 static struct lockf_range *
740 return(malloc(sizeof(struct lockf_range), M_LOCKF, M_WAITOK));
744 lf_create_range(struct lockf_range *range, struct proc *owner, int type,
745 int flags, off_t start, off_t end, int accounting)
747 KKASSERT(start <= end);
748 if (owner != NULL && (flags & F_POSIX) && accounting)
749 ++owner->p_numposixlocks;
750 range->lf_type = type;
751 range->lf_flags = flags;
752 range->lf_start = start;
754 range->lf_owner = owner;
758 printf("lf_create_range: %lld..%lld\n", range->lf_start,
764 lf_destroy_range(struct lockf_range *range, int accounting)
766 struct proc *owner = range->lf_owner;
767 int flags = range->lf_flags;
771 printf("lf_destroy_range: %lld..%lld\n", range->lf_start,
775 free(range, M_LOCKF);
776 if (owner != NULL && (flags & F_POSIX) && accounting) {
777 --owner->p_numposixlocks;
778 KASSERT(owner->p_numposixlocks >= 0,
779 ("Negative number of POSIX locks held by process: %d",
780 owner->p_numposixlocks));
785 KKASSERT(lf_global_counter>=0);
791 lf_print_lock(const struct lockf *lock)
793 struct lockf_range *range;
795 if (TAILQ_EMPTY(&lock->lf_range))
796 printf("lockf %p: no ranges locked\n", lock);
798 printf("lockf %p:\n", lock);
799 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
800 printf("\t%lld..%lld type %s owned by %d\n",
801 range->lf_start, range->lf_end,
802 range->lf_type == F_RDLCK ? "shared" : "exclusive",
803 range->lf_flags & F_POSIX ? range->lf_owner->p_pid : -1);
804 if (TAILQ_EMPTY(&lock->lf_blocked))
805 printf("no process waiting for range\n");
807 printf("blocked locks:");
808 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
809 printf("\t%lld..%lld type %s waiting on %p\n",
810 range->lf_start, range->lf_end,
811 range->lf_type == F_RDLCK ? "shared" : "exclusive",
814 #endif /* LOCKF_DEBUG */