When a kernel-created thread exits, properly remove it from gd_tdallq and
[dragonfly.git] / sys / kern / kern_lockf.c
1 /*
2  * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Scooter Morris at Genentech Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
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.16 2004/06/25 15:32:18 joerg Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/proc.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>
53
54 #include <sys/lockf.h>
55 #include <machine/limits.h>     /* for LLONG_MAX */
56
57 #ifdef INVARIANTS
58 int lf_global_counter = 0;
59 #endif
60 #ifdef LOCKF_DEBUG
61 int lf_print_ranges = 0;
62
63 static void     lf_print_lock(const struct lockf *);
64 #endif
65
66 static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
67
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,
77                                 off_t, off_t, int);
78 static void     lf_destroy_range(struct lockf_range *, int);
79
80 static int      lf_setlock(struct lockf *, struct proc *, int, int,
81                            off_t, off_t);
82 static int      lf_clearlock(struct lockf *, struct proc *, int, int,
83                              off_t, off_t);
84 static int      lf_getlock(struct flock *, struct lockf *, struct proc *,
85                            int, int, off_t, off_t);
86
87 static int      lf_count_change(struct proc *, int);
88
89 /*
90  * Change the POSIX lock accounting for the given process.
91  */
92 void
93 lf_count_adjust(struct proc *p, int increase)
94 {
95         struct uidinfo *uip;
96
97         KKASSERT(p != NULL);
98
99         uip = p->p_ucred->cr_uidinfo;
100
101         if (increase)
102                 uip->ui_posixlocks += p->p_numposixlocks;
103         else
104                 uip->ui_posixlocks -= p->p_numposixlocks;
105
106         KASSERT(uip->ui_posixlocks >= 0,
107                 ("Negative number of POSIX locks held by %s user: %d.",
108                  increase ? "new" : "old", uip->ui_posixlocks));
109 }
110
111 static int
112 lf_count_change(struct proc *owner, int diff)
113 {
114         struct uidinfo *uip;
115         int max;
116
117         /* we might actually not have a process context */
118         if (owner == NULL)
119                 return(0);
120
121         uip = owner->p_ucred->cr_uidinfo;
122
123         max = MIN(owner->p_rlimit[RLIMIT_POSIXLOCKS].rlim_cur,
124                   maxposixlocksperuid);
125         if (diff > 0 && owner->p_ucred->cr_uid != 0 && max != -1 &&
126             uip->ui_posixlocks >= max )
127                 return(1);
128
129         uip->ui_posixlocks += diff;
130
131         KASSERT(uip->ui_posixlocks >= 0,
132                 ("Negative number of POSIX locks held by user: %d.",
133                  uip->ui_posixlocks));
134
135         return(0);
136 }
137
138 /*
139  * Advisory record locking support
140  */
141 int
142 lf_advlock(struct vop_advlock_args *ap, struct lockf *lock, u_quad_t size)
143 {
144         struct flock *fl = ap->a_fl;
145         struct proc *owner;
146         off_t start, end;
147         int type, flags, error;
148         lwkt_tokref ilock;
149
150         /*
151          * Convert the flock structure into a start and end.
152          */
153         switch (fl->l_whence) {
154         case SEEK_SET:
155         case SEEK_CUR:
156                 /*
157                  * Caller is responsible for adding any necessary offset
158                  * when SEEK_CUR is used.
159                  */
160                 start = fl->l_start;
161                 break;
162
163         case SEEK_END:
164                 start = size + fl->l_start;
165                 break;
166
167         default:
168                 return(EINVAL);
169         }
170         if (start < 0)
171                 return(EINVAL);
172         if (fl->l_len == 0) {
173                 flags |= F_NOEND;
174                 end = LLONG_MAX;
175         } else {
176                 end = start + fl->l_len - 1;
177                 if (end < start)
178                         return(EINVAL);
179         }
180         
181         flags = ap->a_flags;
182         type = fl->l_type;
183         /*
184          * This isn't really correct for flock-style locks,
185          * but the current handling is somewhat broken anyway.
186          */
187         owner = (struct proc *)ap->a_id;
188
189         /*
190          * Do the requested operation.
191          */
192         lwkt_gettoken(&ilock, lwkt_token_pool_get(lock));
193
194         if (lock->init_done == 0) {
195                 TAILQ_INIT(&lock->lf_range);
196                 TAILQ_INIT(&lock->lf_blocked);
197                 lock->init_done = 1;
198         }
199
200         switch(ap->a_op) {
201         case F_SETLK:
202                 error = lf_setlock(lock, owner, type, flags, start, end);
203                 break;
204
205         case F_UNLCK:
206                 error = lf_clearlock(lock, owner, type, flags, start, end);
207                 break;
208
209         case F_GETLK:
210                 error = lf_getlock(fl, lock, owner, type, flags, start, end);
211                 break;
212
213         default:
214                 error = EINVAL;
215                 break;
216         }
217         lwkt_reltoken(&ilock);
218         return(error);
219 }
220
221 static int
222 lf_setlock(struct lockf *lock, struct proc *owner, int type, int flags,
223            off_t start, off_t end)
224 {
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;
229         int error = 0;
230         
231         /* for restauration in case of hitting the POSIX lock limit below */
232         struct lockf_range *orig_first_match = NULL;
233         off_t orig_end = -1;
234         int orig_flags = 0;
235
236 restart:
237         if (new_range1 == NULL)
238                 new_range1 = lf_alloc_range();
239         if (new_range2 == NULL)
240                 new_range2 = lf_alloc_range();
241         first_match = NULL;
242         insert_point = NULL;
243         wakeup_needed = 0;
244
245 #ifdef LOCKF_DEBUG
246         if (lf_print_ranges)
247                 lf_print_lock(lock);
248 #endif
249
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)
254                         continue;
255                 if (range->lf_owner == owner) {
256                         if (first_match == NULL)
257                                 first_match = range;
258                         continue;
259                 }
260                 if (type == F_WRLCK || range->lf_type == F_WRLCK)
261                         break;
262         }
263
264         if (range != NULL) {
265                 struct lockf_range *brange;
266
267                 if ((flags & F_WAIT) == 0) {
268                         error = EAGAIN;
269                         goto do_cleanup;
270                 }
271
272                 /*
273                  * We are blocked. For POSIX locks we have to check
274                  * for deadlocks and return with EDEADLK. This is done
275                  * by checking wether range->lf_owner is already
276                  * blocked.
277                  *
278                  * Since flock-style locks cover the whole file, a
279                  * deadlock between those is nearly impossible.
280                  * This can only occur if a process tries to lock the
281                  * same inode exclusively while holding a shared lock
282                  * with another descriptor.
283                  * XXX How can we cleanly detect this?
284                  * XXX The current mixing of flock & fcntl/lockf is evil.
285                  *
286                  * Handle existing locks of flock-style like POSIX locks.
287                  */
288                 if (flags & F_POSIX) {
289                         TAILQ_FOREACH(brange, &lock->lf_blocked, lf_link)
290                                 if (brange->lf_owner == range->lf_owner) {
291                                         error = EDEADLK;
292                                         goto do_cleanup;
293                                 }
294                 }
295                 
296                 /*
297                  * For flock-style locks, we must first remove
298                  * any shared locks that we hold before we sleep
299                  * waiting for an exclusive lock.
300                  */
301                 if ((flags & F_FLOCK) && type == F_WRLCK)
302                         lf_clearlock(lock, owner, type, flags, start, end);
303
304                 brange = new_range1;
305                 new_range1 = NULL;
306                 lf_create_range(brange, owner, type, 0, start, end, 0);
307                 TAILQ_INSERT_TAIL(&lock->lf_blocked, brange, lf_link);
308                 error = tsleep(brange, PCATCH, "lockf", 0);
309
310                 /*
311                  * We may have been awaked by a signal and/or by a
312                  * debugger continuing us (in which case we must remove
313                  * ourselves from the blocked list) and/or by another
314                  * process releasing/downgrading a lock (in which case
315                  * we have already been removed from the blocked list
316                  * and our lf_flags field is 1).
317                  */
318                 if (brange->lf_flags == 0)
319                         TAILQ_REMOVE(&lock->lf_blocked, brange, lf_link);
320                 lf_destroy_range(brange, 0);
321
322                 if (error)
323                         goto do_cleanup;
324                 goto restart;
325         }
326
327         if (first_match == NULL) {
328                 if (flags & F_POSIX) {
329                         if (lf_count_change(owner, 1)) {
330                                 error = ENOLCK;
331                                 goto do_cleanup;
332                         }
333                 }
334                 range = new_range1;
335                 new_range1 = NULL;
336                 lf_create_range(range, owner, type, flags, start, end, 1);
337                 if (insert_point != NULL)
338                         TAILQ_INSERT_BEFORE(insert_point, range, lf_link);
339                 else
340                         TAILQ_INSERT_TAIL(&lock->lf_range, range, lf_link);
341                 goto do_wakeup;
342         }
343
344         lock_needed = 1;
345
346         if (lf_overlap_left(first_match, start, end)) {
347                 KKASSERT((flags & F_POSIX) != 0);
348                 if (first_match->lf_end > end) {
349                         if (first_match->lf_type == type)
350                                 goto do_wakeup;
351                         if (lf_count_change(owner, 2)) {
352                                 error = ENOLCK;
353                                 goto do_cleanup;
354                         }
355                         range = new_range1;
356                         new_range1 = NULL;
357                         lf_create_range(range, owner, type, flags,
358                                         start, end, 1);
359                         if (insert_point != NULL)
360                                 TAILQ_INSERT_BEFORE(insert_point, range,
361                                                     lf_link);
362                         else
363                                 TAILQ_INSERT_TAIL(&lock->lf_range, range,
364                                                   lf_link);
365                         insert_point = range;
366                         range = new_range2;
367                         new_range2 = NULL;
368                         lf_create_range(range, owner, first_match->lf_type,
369                                         first_match->lf_flags, end + 1,
370                                         first_match->lf_end, 1);
371                         TAILQ_INSERT_AFTER(&lock->lf_range, insert_point,
372                                            range, lf_link);
373                         first_match->lf_flags &= ~F_NOEND;
374                         first_match->lf_end = start - 1;
375                         if (type == F_RDLCK)
376                                 wakeup_needed = 1;
377                         goto do_wakeup;
378                 }
379                 /*
380                  * left match, but not right match
381                  *
382                  * handle the lf_type != type case directly,
383                  * merge the other case with the !lock_needed path.
384                  */
385                 if (first_match->lf_type != type) {
386                         /*
387                          * This is needed if the lockf acquisition below fails.
388                          */
389                         orig_first_match = first_match;
390                         orig_end = first_match->lf_end;
391                         orig_flags = first_match->lf_flags;
392                         first_match->lf_end = start - 1;
393                         first_match->lf_flags &= ~F_NOEND;
394                         if (type == F_RDLCK)
395                                 wakeup_needed = 1;
396                         /* Try to find the next matching range */
397                         range = TAILQ_NEXT(first_match, lf_link);
398                         while (range != NULL) {
399                                 if (range->lf_owner == owner &&
400                                     lf_overlap(range, start, end))
401                                         break;
402                                 range = TAILQ_NEXT(range, lf_link);
403                         }
404                         if (range == NULL)
405                                 goto do_wakeup;
406                         first_match = range;
407                         /* fall through to !left_match behaviour */
408                 } else {
409                         first_match->lf_end = end;
410                         first_match->lf_flags |= flags & F_NOEND;
411                         lock_needed = 0;
412                 }
413         }
414
415         if (lf_overlap_embedded(first_match, start, end)) {
416                 if (first_match != insert_point) {
417                         TAILQ_REMOVE(&lock->lf_range, first_match, lf_link);
418                         TAILQ_INSERT_BEFORE(insert_point, first_match, lf_link);
419                 }
420                 first_match->lf_start = start;
421                 first_match->lf_end = end;
422                 first_match->lf_flags |= flags & F_NOEND;
423                 first_match->lf_type = type;
424                 lock_needed = 0;                
425         }
426
427         if (lock_needed == 0) {
428                 struct lockf_range *nrange;
429
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);
434                                 continue;
435                         }
436                         if (lf_overlap_embedded(range, start, end)) {
437                                 nrange = TAILQ_NEXT(range, lf_link);
438                                 TAILQ_REMOVE(&lock->lf_range, range,
439                                              lf_link);
440                                 lf_count_change(owner, -1);
441                                 lf_destroy_range(range, 1);
442                                 range = nrange;
443                                 continue;
444                         }
445                         if (lf_overlap_right(range, start, end) == 0) {
446                                 range = TAILQ_NEXT(range, lf_link);
447                                 continue;
448                         }
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)
455                                                 break;
456                                         nrange = TAILQ_NEXT(nrange, lf_link);
457                                 }
458                                 if (nrange != NULL)
459                                         TAILQ_INSERT_BEFORE(nrange, range,
460                                                             lf_link);
461                                 else
462                                         TAILQ_INSERT_TAIL(&lock->lf_range,
463                                                           range, lf_link);
464                                 break;
465                         }
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);
472                         break;
473                 }
474                 goto do_wakeup;
475         }
476
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,
483                                              lf_link);
484                                 TAILQ_INSERT_BEFORE(insert_point, first_match,
485                                                     lf_link);
486                         }
487                         goto do_wakeup;
488                 }
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;
493                         }
494                         error = ENOLCK;
495                         goto do_cleanup;
496                 }
497                 first_match->lf_start = end + 1;
498                 KKASSERT(new_range1 != NULL);
499                 range = new_range1;
500                 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)
507                                 break;
508                         range = TAILQ_NEXT(range, lf_link);
509                 }
510                 if (range != NULL)
511                         TAILQ_INSERT_BEFORE(range, first_match, lf_link);
512                 else
513                         TAILQ_INSERT_TAIL(&lock->lf_range, first_match, lf_link);
514                 goto do_wakeup;
515         }
516
517 do_wakeup:
518 #ifdef LOCKF_DEBUG
519         if (lf_print_ranges)
520                 lf_print_lock(lock);
521 #endif
522         if (wakeup_needed)
523                 lf_wakeup(lock, start, end);
524         error = 0;
525 do_cleanup:
526         if (new_range1 != NULL)
527                 lf_destroy_range(new_range1, 0);
528         if (new_range2 != NULL)
529                 lf_destroy_range(new_range2, 0);
530         return(error);
531 }
532
533 static int
534 lf_clearlock(struct lockf *lock, struct proc *owner, int type, int flags,
535              off_t start, off_t end)
536 {
537         struct lockf_range *range, *trange;
538         struct lockf_range *new_range;
539         int error = 0;
540
541         new_range = lf_alloc_range();
542
543         TAILQ_FOREACH_MUTABLE(range, &lock->lf_range, lf_link, trange) {
544                 if (range->lf_end < start)
545                         continue;
546                 if (range->lf_start > end)
547                         break;
548                 if (range->lf_owner != owner)
549                         continue;
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);
556                         continue;
557                 }
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;
562
563                                 if (lf_count_change(owner, 1)) {
564                                         error = ENOLCK;
565                                         goto do_cleanup;
566                                 }
567                                 nrange = new_range;
568                                 new_range = NULL;
569                                 lf_create_range(nrange, range->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)
577                                                 break;
578                                 if (range != NULL)
579                                         TAILQ_INSERT_BEFORE(range, nrange,
580                                                             lf_link);
581                                 else
582                                         TAILQ_INSERT_TAIL(&lock->lf_range,
583                                                           nrange, lf_link);
584                                 break;
585                         }
586                         range->lf_end = start - 1;
587                         range->lf_flags &= ~F_NOEND;
588                         continue;
589                 }
590                 if (lf_overlap_right2(range, start, end)) {
591                         struct lockf_range *nrange = range;
592
593                         KKASSERT(range->lf_flags & F_POSIX);
594
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)
600                                         break;
601                         if (range != NULL)
602                                 TAILQ_INSERT_BEFORE(range, nrange, lf_link);
603                         else
604                                 TAILQ_INSERT_TAIL(&lock->lf_range, nrange,
605                                                   lf_link);
606                         break;
607                 }
608         }
609
610         lf_wakeup(lock, start, end);
611         error = 0;
612
613 do_cleanup:
614         if (new_range != NULL)
615                 lf_destroy_range(new_range, 0);
616
617         return(error);
618 }
619
620 /*
621  * Check whether there is a blocking lock,
622  * and if so return its process identifier.
623  */
624 static int
625 lf_getlock(struct flock *fl, struct lockf *lock, struct proc *owner,
626            int type, int flags, off_t start, off_t end)
627 {
628         struct lockf_range *range;
629
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))
634                         break;
635         if (range == NULL) {
636                 fl->l_type = F_UNLCK;
637                 return(0);
638         }
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)
643                 fl->l_len = 0;
644         else
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;
648         else
649                 fl->l_pid = -1;
650         return(0);
651 }
652
653 /*
654  * Check wether range and [start, end] overlap.
655  */
656 static int
657 lf_overlap(const struct lockf_range *range, off_t start, off_t end)
658 {
659         if (range->lf_start >= start && range->lf_start <= end)
660                 return(1);
661         else if (start >= range->lf_start && start <= range->lf_end)
662                 return(1);
663         else
664                 return(0);
665 }
666
667 /*
668  * Wakeup pending lock attempts.
669  */
670 static void
671 lf_wakeup(struct lockf *lock, off_t start, off_t end)
672 {
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)
676                         continue;
677                 TAILQ_REMOVE(&lock->lf_blocked, range, lf_link);
678                 range->lf_flags = 1;
679                 wakeup(range);
680         }
681 }
682
683 static int
684 lf_overlap_left(const struct lockf_range *range, off_t start, off_t end)
685 {
686         if (range->lf_start < start && range->lf_end >= start - 1 &&
687             range->lf_end <= end)
688                 return(1);
689         else
690                 return(0);
691                 
692 }
693
694 static int
695 lf_overlap_right(const struct lockf_range *range, off_t start, off_t end)
696 {
697         if (range->lf_end > end && range->lf_start >= start &&
698             range->lf_start - 1 <= end)
699                 return(1);
700         else
701                 return(0);
702 }
703
704 static int
705 lf_overlap_left2(const struct lockf_range *range, off_t start, off_t end)
706 {
707         if (range->lf_start < start && range->lf_end >= start &&
708             range->lf_end <= end)
709                 return(1);
710         else
711                 return(0);
712                 
713 }
714
715 static int
716 lf_overlap_right2(const struct lockf_range *range, off_t start, off_t end)
717 {
718         if (range->lf_end > end && range->lf_start >= start &&
719             range->lf_start <= end)
720                 return(1);
721         else
722                 return(0);
723 }
724
725 static int
726 lf_overlap_embedded(const struct lockf_range *range, off_t start, off_t end)
727 {
728         if (range->lf_start >= start && range->lf_end <= end)
729                 return(1);
730         else
731                 return(0);
732 }
733
734 /*
735  * Allocate a range structure and initialize it sufficiently such that
736  * lf_destroy_range() does not barf.
737  */
738 static struct lockf_range *
739 lf_alloc_range(void)
740 {
741         struct lockf_range *range;
742
743 #ifdef INVARIANTS
744         lf_global_counter++;
745 #endif
746         range = malloc(sizeof(struct lockf_range), M_LOCKF, M_WAITOK);
747         range->lf_owner = NULL;
748         return(range);
749 }
750
751 static void
752 lf_create_range(struct lockf_range *range, struct proc *owner, int type,
753                 int flags, off_t start, off_t end, int accounting)
754 {
755         KKASSERT(start <= end);
756         if (owner != NULL && (flags & F_POSIX) && accounting)
757                 ++owner->p_numposixlocks;
758         range->lf_type = type;
759         range->lf_flags = flags;
760         range->lf_start = start;
761         range->lf_end = end;
762         range->lf_owner = owner;
763
764 #ifdef LOCKF_DEBUG
765         if (lf_print_ranges)
766                 printf("lf_create_range: %lld..%lld\n", range->lf_start,
767                        range->lf_end);
768 #endif
769 }
770
771 static void
772 lf_destroy_range(struct lockf_range *range, int accounting)
773 {
774         struct proc *owner = range->lf_owner;
775         int flags = range->lf_flags;
776
777 #ifdef LOCKF_DEBUG
778         if (lf_print_ranges)
779                 printf("lf_destroy_range: %lld..%lld\n", range->lf_start,
780                        range->lf_end);
781 #endif
782
783         free(range, M_LOCKF);
784         if (owner != NULL && (flags & F_POSIX) && accounting) {
785                 --owner->p_numposixlocks;
786                 KASSERT(owner->p_numposixlocks >= 0,
787                         ("Negative number of POSIX locks held by process: %d",
788                          owner->p_numposixlocks));
789         }
790
791 #ifdef INVARIANTS
792         lf_global_counter--;
793         KKASSERT(lf_global_counter>=0);
794 #endif
795 }
796
797 #ifdef LOCKF_DEBUG
798 static void
799 lf_print_lock(const struct lockf *lock)
800 {
801         struct lockf_range *range;
802
803         if (TAILQ_EMPTY(&lock->lf_range))
804                 printf("lockf %p: no ranges locked\n", lock);
805         else
806                 printf("lockf %p:\n", lock);
807         TAILQ_FOREACH(range, &lock->lf_range, lf_link)
808                 printf("\t%lld..%lld type %s owned by %d\n",
809                        range->lf_start, range->lf_end,
810                        range->lf_type == F_RDLCK ? "shared" : "exclusive",
811                        range->lf_flags & F_POSIX ? range->lf_owner->p_pid : -1);
812         if (TAILQ_EMPTY(&lock->lf_blocked))
813                 printf("no process waiting for range\n");
814         else
815                 printf("blocked locks:");
816         TAILQ_FOREACH(range, &lock->lf_range, lf_link)
817                 printf("\t%lld..%lld type %s waiting on %p\n",
818                        range->lf_start, range->lf_end,
819                        range->lf_type == F_RDLCK ? "shared" : "exclusive",
820                        range);
821 }
822 #endif /* LOCKF_DEBUG */