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.8 2004/05/04 17:00:55 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;
150 if (lock->init_done == 0) {
151 TAILQ_INIT(&lock->lf_range);
152 TAILQ_INIT(&lock->lf_blocked);
157 * Convert the flock structure into a start and end.
159 switch (fl->l_whence) {
163 * Caller is responsible for adding any necessary offset
164 * when SEEK_CUR is used.
170 start = size + fl->l_start;
178 if (fl->l_len == 0) {
182 end = start + fl->l_len - 1;
190 * This isn't really correct for flock-style locks,
191 * but the current handling is somewhat broken anyway.
193 owner = (struct proc *)ap->a_id;
196 * Do the requested operation.
200 return(lf_setlock(lock, owner, type, flags, start, end));
203 return(lf_clearlock(lock, owner, type, flags, start, end));
206 return(lf_getlock(fl, lock, owner, type, flags, start, end));
215 lf_setlock(struct lockf *lock, struct proc *owner, int type, int flags,
216 off_t start, off_t end)
218 struct lockf_range *range, *first_match, *insert_point;
219 int wakeup_needed, lock_needed;
220 /* pre-allocation to avoid blocking in the middle of the algorithm */
221 struct lockf_range *new_range1 = NULL, *new_range2 = NULL;
224 /* for restauration in case of hitting the POSIX lock limit below */
225 struct lockf_range *orig_first_match = NULL;
230 if (new_range1 == NULL)
231 new_range1 = lf_alloc_range();
232 if (new_range2 == NULL)
233 new_range2 = lf_alloc_range();
243 TAILQ_FOREACH(range, &lock->lf_range, lf_link) {
244 if (insert_point == NULL && range->lf_start >= start)
245 insert_point = range;
246 if (lf_overlap(range, start, end) == 0)
248 if (range->lf_owner == owner) {
249 if (first_match == NULL)
253 if (type == F_WRLCK || range->lf_type == F_WRLCK)
258 struct lockf_range *brange;
261 if ((flags & F_WAIT) == 0) {
267 * We are blocked. For POSIX locks we have to check
268 * for deadlocks and return with EDEADLK. This is done
269 * by checking wether range->lf_owner is already
272 * Since flock-style locks cover the whole file, a
273 * deadlock between those is nearly impossible.
274 * This can only occur if a process tries to lock the
275 * same inode exclusively while holding a shared lock
276 * with another descriptor.
277 * XXX How can we cleanly detect this?
278 * XXX The current mixing of flock & fcntl/lockf is evil.
280 * Handle existing locks of flock-style like POSIX locks.
282 if (flags & F_POSIX) {
283 TAILQ_FOREACH(brange, &lock->lf_blocked, lf_link)
284 if (brange->lf_owner == range->lf_owner) {
291 * For flock-style locks, we must first remove
292 * any shared locks that we hold before we sleep
293 * waiting for an exclusive lock.
295 if ((flags & F_FLOCK) && type == F_WRLCK)
296 lf_clearlock(lock, owner, type, flags, start, end);
300 lf_create_range(brange, owner, type, 0, start, end, 0);
301 TAILQ_INSERT_TAIL(&lock->lf_blocked, brange, lf_link);
302 error = tsleep(brange, PCATCH, "lockf", 0);
305 * We may have been awaked by a signal and/or by a
306 * debugger continuing us (in which case we must remove
307 * ourselves from the blocked list) and/or by another
308 * process releasing/downgrading a lock (in which case
309 * we have already been removed from the blocked list
310 * and our lf_flags field is 1).
312 if (brange->lf_flags == 0)
313 TAILQ_REMOVE(&lock->lf_blocked, brange, lf_link);
314 lf_destroy_range(brange, 0);
321 if (first_match == NULL) {
322 if (flags & F_POSIX) {
323 if (lf_count_change(owner, 1)) {
330 lf_create_range(range, owner, type, flags, start, end, 1);
331 if (insert_point != NULL)
332 TAILQ_INSERT_BEFORE(insert_point, range, lf_link);
334 TAILQ_INSERT_TAIL(&lock->lf_range, range, lf_link);
340 if (lf_overlap_left(first_match, start, end)) {
341 KKASSERT((flags & F_POSIX) != 0);
342 if (first_match->lf_end > end) {
343 if (first_match->lf_type == type)
345 if (lf_count_change(owner, 2)) {
351 lf_create_range(range, owner, type, flags,
353 if (insert_point != NULL)
354 TAILQ_INSERT_BEFORE(insert_point, range,
357 TAILQ_INSERT_TAIL(&lock->lf_range, range,
359 insert_point = range;
362 lf_create_range(range, owner, first_match->lf_type,
363 first_match->lf_flags, end + 1,
364 first_match->lf_end, 1);
365 TAILQ_INSERT_AFTER(&lock->lf_range, insert_point,
367 first_match->lf_flags &= ~F_NOEND;
368 first_match->lf_end = start - 1;
374 * left match, but not right match
376 * handle the lf_type != type case directly,
377 * merge the other case with the !lock_needed path.
379 if (first_match->lf_type != type) {
381 * This is needed if the lockf acquisition below fails.
383 orig_first_match = first_match;
384 orig_end = first_match->lf_end;
385 orig_flags = first_match->lf_flags;
386 first_match->lf_end = start - 1;
387 first_match->lf_flags &= ~F_NOEND;
390 /* Try to find the next matching range */
391 range = TAILQ_NEXT(first_match, lf_link);
392 while (range != NULL) {
393 if (range->lf_owner == owner &&
394 lf_overlap(range, start, end))
396 range = TAILQ_NEXT(range, lf_link);
401 /* fall through to !left_match behaviour */
403 first_match->lf_end = end;
404 first_match->lf_flags |= flags & F_NOEND;
409 if (lf_overlap_embedded(first_match, start, end)) {
410 if (first_match != insert_point) {
411 TAILQ_REMOVE(&lock->lf_range, first_match, lf_link);
412 TAILQ_INSERT_BEFORE(insert_point, first_match, lf_link);
414 first_match->lf_start = start;
415 first_match->lf_end = end;
416 first_match->lf_flags |= flags & F_NOEND;
420 if (lock_needed == 0) {
421 struct lockf_range *nrange;
423 range = TAILQ_NEXT(first_match, lf_link);
424 while (range != NULL) {
425 if (range->lf_owner != owner) {
426 range = TAILQ_NEXT(range, lf_link);
429 if (lf_overlap_embedded(range, start, end)) {
430 nrange = TAILQ_NEXT(range, lf_link);
431 TAILQ_REMOVE(&lock->lf_range, range,
433 lf_count_change(owner, -1);
434 lf_destroy_range(range, 1);
438 if (lf_overlap_right(range, start, end) == 0) {
439 range = TAILQ_NEXT(range, lf_link);
442 if (range->lf_type != type) {
443 range->lf_start = end + 1;
444 nrange = TAILQ_NEXT(range, lf_link);
445 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
446 while (nrange != NULL) {
447 if (nrange->lf_start >= end + 1)
449 nrange = TAILQ_NEXT(nrange, lf_link);
452 TAILQ_INSERT_BEFORE(nrange, range,
455 TAILQ_INSERT_TAIL(&lock->lf_range,
459 first_match->lf_end = range->lf_end;
460 first_match->lf_flags |=
461 range->lf_flags & F_NOEND;
462 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
463 lf_count_change(owner, -1);
464 lf_destroy_range(range, 1);
470 if (lf_overlap_right(first_match, start, end)) {
471 KKASSERT((flags & F_POSIX) != 0);
472 if (first_match->lf_type == type) {
473 first_match->lf_start = start;
474 if (first_match != insert_point) {
475 TAILQ_REMOVE(&lock->lf_range, first_match,
477 TAILQ_INSERT_BEFORE(insert_point, first_match,
482 if (lf_count_change(owner, 1)) {
483 if (orig_first_match != NULL) {
484 orig_first_match->lf_end = orig_end;
485 orig_first_match->lf_flags = orig_end;
490 first_match->lf_start = end + 1;
491 KKASSERT(new_range1 != NULL);
494 lf_create_range(range, owner, type, flags, start, end, 1);
495 TAILQ_INSERT_BEFORE(insert_point, range, lf_link);
496 range = TAILQ_NEXT(first_match, lf_link);
497 TAILQ_REMOVE(&lock->lf_range, first_match, lf_link);
498 while (range != NULL) {
499 if (range->lf_start >= first_match->lf_start)
501 range = TAILQ_NEXT(range, lf_link);
504 TAILQ_INSERT_BEFORE(range, first_match, lf_link);
506 TAILQ_INSERT_TAIL(&lock->lf_range, first_match, lf_link);
516 lf_wakeup(lock, start, end);
519 if (new_range1 != NULL)
520 lf_destroy_range(new_range1, 0);
521 if (new_range2 != NULL)
522 lf_destroy_range(new_range2, 0);
527 lf_clearlock(struct lockf *lock, struct proc *owner, int type, int flags,
528 off_t start, off_t end)
530 struct lockf_range *range, *trange;
531 struct lockf_range *new_range;
534 new_range = lf_alloc_range();
536 TAILQ_FOREACH_MUTABLE(range, &lock->lf_range, lf_link, trange) {
537 if (range->lf_end < start)
539 if (range->lf_start > end)
541 if (range->lf_owner != owner)
543 if (lf_overlap_embedded(range, start, end)) {
544 TAILQ_REMOVE(&lock->lf_range, range, lf_link);
545 /* flock-locks are equal */
546 if (range->lf_flags & F_POSIX)
547 lf_count_change(owner, -1);
548 lf_destroy_range(range, 1);
551 if (lf_overlap_left2(range, start, end)) {
552 KKASSERT(range->lf_flags & F_POSIX);
553 if (lf_overlap_right2(range, start, end)) {
554 struct lockf_range *nrange;
556 if (lf_count_change(owner, 1)) {
562 lf_create_range(nrange, nrange->lf_owner,
563 range->lf_type, range->lf_flags,
564 end + 1, range->lf_end, 1);
565 range->lf_end = start;
566 range->lf_flags &= ~F_NOEND;
567 for (; range != NULL;
568 range = TAILQ_NEXT(range, lf_link))
569 if (range->lf_start >= nrange->lf_start)
572 TAILQ_INSERT_BEFORE(range, nrange,
575 TAILQ_INSERT_TAIL(&lock->lf_range,
579 range->lf_end = start - 1;
580 range->lf_flags &= ~F_NOEND;
583 if (lf_overlap_right2(range, start, end)) {
584 struct lockf_range *nrange = range;
586 KKASSERT(range->lf_flags & F_POSIX);
588 range = TAILQ_NEXT(range, lf_link);
589 TAILQ_REMOVE(&lock->lf_range, nrange, lf_link);
590 for (; range != NULL;
591 range = TAILQ_NEXT(range, lf_link))
592 if (range->lf_start >= nrange->lf_start)
595 TAILQ_INSERT_BEFORE(range, nrange, lf_link);
597 TAILQ_INSERT_TAIL(&lock->lf_range, nrange,
603 lf_wakeup(lock, start, end);
607 if (new_range != NULL)
608 lf_destroy_range(new_range, 0);
614 * Check whether there is a blocking lock,
615 * and if so return its process identifier.
618 lf_getlock(struct flock *fl, struct lockf *lock, struct proc *owner,
619 int type, int flags, off_t start, off_t end)
621 struct lockf_range *range;
623 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
624 if (range->lf_owner != owner &&
625 lf_overlap(range, start, end) &&
626 (type == F_WRLCK || range->lf_type == F_WRLCK))
629 fl->l_type = F_UNLCK;
632 fl->l_type = range->lf_type;
633 fl->l_whence = SEEK_SET;
634 fl->l_start = range->lf_start;
635 if (range->lf_flags & F_NOEND)
638 fl->l_len = range->lf_end - range->lf_start + 1;
639 if (range->lf_owner != NULL && (range->lf_flags & F_POSIX))
640 fl->l_pid = range->lf_owner->p_pid;
647 * Check wether range and [start, end] overlap.
650 lf_overlap(const struct lockf_range *range, off_t start, off_t end)
652 if (range->lf_start >= start && range->lf_start <= end)
654 else if (start >= range->lf_start && start <= range->lf_end)
661 * Wakeup pending lock attempts.
664 lf_wakeup(struct lockf *lock, off_t start, off_t end)
666 struct lockf_range *range, *nrange;
667 TAILQ_FOREACH_MUTABLE(range, &lock->lf_blocked, lf_link, nrange) {
668 if (lf_overlap(range, start, end) == 0)
670 TAILQ_REMOVE(&lock->lf_blocked, range, lf_link);
677 lf_overlap_left(const struct lockf_range *range, off_t start, off_t end)
679 if (range->lf_start < start && range->lf_end >= start - 1 &&
680 range->lf_end <= end)
688 lf_overlap_right(const struct lockf_range *range, off_t start, off_t end)
690 if (range->lf_end > end && range->lf_start >= start &&
691 range->lf_start - 1 <= end)
698 lf_overlap_left2(const struct lockf_range *range, off_t start, off_t end)
700 if (range->lf_start < start && range->lf_end >= start &&
701 range->lf_end <= end)
709 lf_overlap_right2(const struct lockf_range *range, off_t start, off_t end)
711 if (range->lf_end > end && range->lf_start >= start &&
712 range->lf_start <= end)
719 lf_overlap_embedded(const struct lockf_range *range, off_t start, off_t end)
721 if (range->lf_start >= start && range->lf_end <= end)
727 static struct lockf_range *
733 return(malloc(sizeof(struct lockf_range), M_LOCKF, M_WAITOK));
737 lf_create_range(struct lockf_range *range, struct proc *owner, int type,
738 int flags, off_t start, off_t end, int accounting)
740 KKASSERT(start <= end);
741 if (owner != NULL && (flags & F_POSIX) && accounting)
742 ++owner->p_numposixlocks;
743 range->lf_type = type;
744 range->lf_flags = flags;
745 range->lf_start = start;
747 range->lf_owner = owner;
751 printf("lf_create_range: %lld..%lld\n", range->lf_start,
757 lf_destroy_range(struct lockf_range *range, int accounting)
759 struct proc *owner = range->lf_owner;
760 int flags = range->lf_flags;
764 printf("lf_destroy_range: %lld..%lld\n", range->lf_start,
768 free(range, M_LOCKF);
769 if (owner != NULL && (flags & F_POSIX) && accounting) {
770 --owner->p_numposixlocks;
771 KASSERT(owner->p_numposixlocks >= 0,
772 ("Negative number of POSIX locks held by process: %d",
773 owner->p_numposixlocks));
778 KKASSERT(lf_global_counter>=0);
784 lf_print_lock(const struct lockf *lock)
786 struct lockf_range *range;
788 if (TAILQ_EMPTY(&lock->lf_range))
789 printf("lockf %p: no ranges locked\n", lock);
791 printf("lockf %p:\n", lock);
792 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
793 printf("\t%lld..%lld type %s owned by %d\n",
794 range->lf_start, range->lf_end,
795 range->lf_type == F_RDLCK ? "shared" : "exclusive",
796 range->lf_flags & F_POSIX ? range->lf_owner->p_pid : -1);
797 if (TAILQ_EMPTY(&lock->lf_blocked))
798 printf("no process waiting for range\n");
800 printf("blocked locks:");
801 TAILQ_FOREACH(range, &lock->lf_range, lf_link)
802 printf("\t%lld..%lld type %s waiting on %p\n",
803 range->lf_start, range->lf_end,
804 range->lf_type == F_RDLCK ? "shared" : "exclusive",
807 #endif /* LOCKF_DEBUG */