DragonFlyBSD Kernel Audit
sys/kern/kern_lockf.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
/*
 * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>.  All rights reserved.
 * Copyright (c) 2006-2018 Matthew Dillon <dillon@backplane.com>.  All rights reserved.
 *
 * Copyright (c) 1982, 1986, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Scooter Morris at Genentech Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)ufs_lockf.c	8.3 (Berkeley) 1/6/94
 * $FreeBSD: src/sys/kern/kern_lockf.c,v 1.25 1999/11/16 16:28:56 phk Exp $
 */

#include "opt_debug_lockf.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/proc.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/fcntl.h>
#include <sys/resourcevar.h>

#include <sys/lockf.h>
#include <machine/limits.h>	/* for LLONG_MAX */
#include <machine/stdarg.h>

#include <sys/spinlock2.h>

struct lf_pcpu {
	struct lockf_range *free1;
	struct lockf_range *free2;
} __cachealign;

static struct lf_pcpu	*lf_pcpu_array;

#ifdef LOCKF_DEBUG
int lf_print_ranges = 0;

static void _lf_print_lock(const struct lockf *);
static void _lf_printf(const char *, ...) __printflike(1, 2);

#define lf_print_lock(lock) if (lf_print_ranges) _lf_print_lock(lock)
#define lf_printf(ctl, args...)	if (lf_print_ranges) _lf_printf(ctl, args)
#else
#define lf_print_lock(lock)
#define lf_printf(ctl, args...)
#endif

static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");

static void	lf_wakeup(struct lockf *, off_t, off_t);
static struct lockf_range *lf_alloc_range(void);
static void	lf_create_range(struct lockf_range *, struct proc *, int, int,
				off_t, off_t);
static void	lf_insert(struct lockf_range_list *list,
				struct lockf_range *elm,
				struct lockf_range *insert_point);
static void	lf_destroy_range(struct lockf_range *);

static int	lf_setlock(struct lockf *, struct proc *, int, int,
			   off_t, off_t);
static int	lf_getlock(struct flock *, struct lockf *, struct proc *,
			   int, int, off_t, off_t);

static int	lf_count_change(struct proc *, int);

/*
 * Return TRUE (non-zero) if the type and posix flags match.
 */
static __inline
int
lf_match(struct lockf_range *range, int type, int flags)
{
	if (range->lf_type != type)
		return(0);
	if ((range->lf_flags ^ flags) & F_POSIX)
		return(0);
	return(1);
}

/*
 * Check whether range and [start, end] overlap.
 */
static __inline
int
lf_overlap(const struct lockf_range *range, off_t start, off_t end)
{
	if (range->lf_start >= start && range->lf_start <= end)
		return(1);
	else if (start >= range->lf_start && start <= range->lf_end)
		return(1);
	else
		return(0);
}


/*
 * Change the POSIX lock accounting for the given process.
 */
void
lf_count_adjust(struct proc *p, int increase)
{
	struct uidinfo *uip;
	struct uidcount *pup;
	int n;

	KKASSERT(p != NULL);

	uip = p->p_ucred->cr_uidinfo;
	pup = &uip->ui_pcpu[mycpuid];

	if (increase) {
		for (n = 0; n < ncpus; ++n)
			pup->pu_posixlocks += p->p_uidpcpu[n].pu_posixlocks;
	} else {
		for (n = 0; n < ncpus; ++n)
			pup->pu_posixlocks -= p->p_uidpcpu[n].pu_posixlocks;
	}

	if (pup->pu_posixlocks < -PUP_LIMIT ||
	    pup->pu_posixlocks > PUP_LIMIT) {
		atomic_add_int(&uip->ui_posixlocks, pup->pu_posixlocks);
		pup->pu_posixlocks = 0;
	}
}

static int
lf_count_change(struct proc *owner, int diff)
{
	struct uidinfo *uip;
	int max, ret;

	/* we might actually not have a process context */
	if (owner == NULL)
		return(0);

	uip = owner->p_ucred->cr_uidinfo;

	max = MIN(owner->p_rlimit[RLIMIT_POSIXLOCKS].rlim_cur,
		  maxposixlocksperuid);

	if (diff > 0 && owner->p_ucred->cr_uid != 0 && max != -1 &&
	    uip->ui_posixlocks >= max ) {
		ret = 1;
	} else {
		struct uidcount *pup;
		int cpu = mycpuid;

		pup = &uip->ui_pcpu[cpu];
		pup->pu_posixlocks += diff;
		if (pup->pu_posixlocks < -PUP_LIMIT ||
		    pup->pu_posixlocks > PUP_LIMIT) {
			atomic_add_int(&uip->ui_posixlocks, pup->pu_posixlocks);
			pup->pu_posixlocks = 0;
		}
		owner->p_uidpcpu[cpu].pu_posixlocks += diff;
		ret = 0;
	}
	return ret;
}

/*
 * Advisory record locking support
 */
int
lf_advlock(struct vop_advlock_args *ap, struct lockf *lock, u_quad_t size)
{
	struct flock *fl = ap->a_fl;
	struct proc *owner;
	off_t start, end;
	int type, flags, error;
	lwkt_token_t token;

	/*
	 * Convert the flock structure into a start and end.
	 */
	switch (fl->l_whence) {
	case SEEK_SET:
	case SEEK_CUR:
		/*
		 * Caller is responsible for adding any necessary offset
		 * when SEEK_CUR is used.
		 */
		start = fl->l_start;
		break;

	case SEEK_END:
		start = size + fl->l_start;
		break;

	default:
		return(EINVAL);
	}

	flags = ap->a_flags;
	if (start < 0)
		return(EINVAL);
	if (fl->l_len == 0) {
		flags |= F_NOEND;
		end = LLONG_MAX;
	} else if (fl->l_len < 0) {
		return(EINVAL);
	} else {
		end = start + fl->l_len - 1;
		if (end < start)
			return(EINVAL);
	}
	
	type = fl->l_type;
	/*
	 * This isn't really correct for flock-style locks,
	 * but the current handling is somewhat broken anyway.
	 */
	owner = (struct proc *)ap->a_id;

	/*
	 * Do the requested operation.
	 */
	token = lwkt_getpooltoken(lock);

	if (lock->init_done == 0) {
		TAILQ_INIT(&lock->lf_range);
		TAILQ_INIT(&lock->lf_blocked);
		lock->init_done = 1;
	}

	switch(ap->a_op) {
	case F_SETLK:
		/*
		 * NOTE: It is possible for both lf_range and lf_blocked to
		 * be empty if we block and get woken up, but another process
		 * then gets in and issues an unlock.  So VMAYHAVELOCKS must
		 * be set after the lf_setlock() operation completes rather
		 * then before.
		 */
		error = lf_setlock(lock, owner, type, flags, start, end);
		if ((ap->a_vp->v_flag & VMAYHAVELOCKS) == 0)
			vsetflags(ap->a_vp, VMAYHAVELOCKS);
		break;

	case F_UNLCK:
		error = lf_setlock(lock, owner, type, flags, start, end);
#if 0
		/*
		 * XXX REMOVED. don't bother doing this in the critical path.
		 * close() overhead is minimal.
		 */
		if (TAILQ_EMPTY(&lock->lf_range) &&
		    TAILQ_EMPTY(&lock->lf_blocked)) {
			vclrflags(ap->a_vp, VMAYHAVELOCKS);
		}
#endif
		break;

	case F_GETLK:
		error = lf_getlock(fl, lock, owner, type, flags, start, end);
		break;

	default:
		error = EINVAL;
		break;
	}
	lwkt_reltoken(token);
	return(error);
}

static int
lf_setlock(struct lockf *lock, struct proc *owner, int type, int flags,
	   off_t start, off_t end)
{
	struct lockf_range *range;
	struct lockf_range *brange;
	struct lockf_range *next;
	struct lockf_range *first_match;
	struct lockf_range *last_match;
	struct lockf_range *insert_point;
	struct lockf_range *new_range1;
	struct lockf_range *new_range2;
	int wakeup_needed;
	int double_clip;
	int unlock_override;
	int error = 0;
	int count;
	struct lockf_range_list deadlist;

	new_range1 = NULL;
	new_range2 = NULL;
	count = 0;

restart:
	/*
	 * Preallocate two ranges so we don't have to worry about blocking
	 * in the middle of the lock code.
	 */
	if (new_range1 == NULL)
		new_range1 = lf_alloc_range();
	if (new_range2 == NULL)
		new_range2 = lf_alloc_range();
	first_match = NULL;
	last_match = NULL;
	insert_point = NULL;
	wakeup_needed = 0;

	lf_print_lock(lock);

	/*
	 * Locate the insertion point for the new lock (the first range
	 * with an lf_start >= start).
	 *
	 * Locate the first and latch ranges owned by us that overlap
	 * the requested range.
	 */
	TAILQ_FOREACH(range, &lock->lf_range, lf_link) {
		if (insert_point == NULL && range->lf_start >= start)
			insert_point = range;

		/*
		 * Skip non-overlapping locks.  Locks are sorted by lf_start
		 * So we can terminate the search when lf_start exceeds the
		 * requested range (insert_point is still guarenteed to be
		 * set properly).
		 */
		if (range->lf_end < start)
			continue;
		if (range->lf_start > end) {
			range = NULL;
			break;
		}

		/*
		 * Overlapping lock.  Set first_match and last_match if we
		 * are the owner.
		 */
		if (range->lf_owner == owner) {
			if (first_match == NULL)
				first_match = range;
			last_match = range;
			continue;
		}

		/*
		 * If we aren't the owner check for a conflicting lock.  Only
		 * if not unlocking.
		 */
		if (type != F_UNLCK) {
			if (type == F_WRLCK || range->lf_type == F_WRLCK)
				break;
		}
	}

	/*
	 * If a conflicting lock was observed, block or fail as appropriate.
	 * (this code is skipped when unlocking)
	 */
	if (range != NULL) {
		if ((flags & F_WAIT) == 0) {
			error = EAGAIN;
			goto do_cleanup;
		}

		/*
		 * We are blocked. For POSIX locks we have to check
		 * for deadlocks and return with EDEADLK. This is done
		 * by checking whether range->lf_owner is already
		 * blocked.
		 *
		 * Since flock-style locks cover the whole file, a
		 * deadlock between those is nearly impossible.
		 * This can only occur if a process tries to lock the
		 * same inode exclusively while holding a shared lock
		 * with another descriptor.
		 * XXX How can we cleanly detect this?
		 * XXX The current mixing of flock & fcntl/lockf is evil.
		 *
		 * Handle existing locks of flock-style like POSIX locks.
		 */
		if (flags & F_POSIX) {
			TAILQ_FOREACH(brange, &lock->lf_blocked, lf_link) {
				if (brange->lf_owner == range->lf_owner) {
					error = EDEADLK;
					goto do_cleanup;
				}
			}
		}
		
		/*
		 * For flock-style locks, we must first remove
		 * any shared locks that we hold before we sleep
		 * waiting for an exclusive lock.
		 */
		if ((flags & F_POSIX) == 0 && type == F_WRLCK)
			lf_setlock(lock, owner, F_UNLCK, 0, start, end);

		brange = new_range1;
		new_range1 = NULL;
		lf_create_range(brange, owner, type, 0, start, end);
		TAILQ_INSERT_TAIL(&lock->lf_blocked, brange, lf_link);
		error = tsleep(brange, PCATCH, "lockf", 0);

		/*
		 * We may have been awaked by a signal and/or by a
		 * debugger continuing us (in which case we must remove
		 * ourselves from the blocked list) and/or by another
		 * process releasing/downgrading a lock (in which case
		 * we have already been removed from the blocked list
		 * and our lf_flags field is 1).
		 *
		 * Sleep if it looks like we might be livelocking.
		 */
		if (brange->lf_flags == 0)
			TAILQ_REMOVE(&lock->lf_blocked, brange, lf_link);
		if (error == 0 && count == 2)
			tsleep(brange, 0, "lockfz", 2);
		else
			++count;
		lf_destroy_range(brange);

		if (error)
			goto do_cleanup;
		goto restart;
	}

	/*
	 * If there are no overlapping locks owned by us then creating
	 * the new lock is easy.  This is the most common case.
	 */
	if (first_match == NULL) {
		if (type == F_UNLCK)
			goto do_wakeup;
		if (flags & F_POSIX) {
			if (lf_count_change(owner, 1)) {
				error = ENOLCK;
				goto do_cleanup;
			}
		}
		range = new_range1;
		new_range1 = NULL;
		lf_create_range(range, owner, type, flags, start, end);
		lf_insert(&lock->lf_range, range, insert_point);
		goto do_wakeup;
	}

	/*
	 * double_clip - Calculate a special case where TWO locks may have
	 *		 to be added due to the new lock breaking up an
	 *		 existing incompatible lock in the middle.
	 *
	 * unlock_override - Calculate a special case where NO locks
	 *		 need to be created.  This occurs when an unlock
	 *		 does not clip any locks at the front and rear.
	 *
	 * WARNING!  closef() and fdrop() assume that an F_UNLCK of the
	 *	     entire range will always succeed so the unlock_override
	 *	     case is mandatory.
	 */
	double_clip = 0;
	unlock_override = 0;
	if (first_match->lf_start < start) {
		if (first_match == last_match && last_match->lf_end > end)
			double_clip = 1;
	} else if (type == F_UNLCK && last_match->lf_end <= end) {
		unlock_override = 1;
	}

	/*
	 * Figure out the worst case net increase in POSIX locks and account
	 * for it now before we start modifying things.  If neither the
	 * first or last locks match we have an issue.  If there is only
	 * one overlapping range which needs to be clipped on both ends
	 * we wind up having to create up to two new locks, else only one.
	 *
	 * When unlocking the worst case is always 1 new lock if our
	 * unlock request cuts the middle out of an existing lock range.
	 *
	 * count represents the 'cleanup' adjustment needed.  It starts
	 * negative, is incremented whenever we create a new POSIX lock,
	 * and decremented whenever we delete an existing one.  At the
	 * end of the day it had better be <= 0 or we didn't calculate the
	 * worse case properly here.
	 */
	count = 0;
	if ((flags & F_POSIX) && !unlock_override) {
		if (!lf_match(first_match, type, flags) &&
		    !lf_match(last_match, type, flags)
		) {
			if (double_clip && type != F_UNLCK)
				count = -2;
			else
				count = -1;
		}
		if (count && lf_count_change(owner, -count)) {
			error = ENOLCK;
			goto do_cleanup;
		}
	}
	/* else flock style lock which encompasses entire range */

	/*
	 * Create and insert the lock represented the requested range.
	 * Adjust the net POSIX lock count.  We have to move our insertion
	 * point since brange now represents the first record >= start.
	 *
	 * When unlocking, no new lock is inserted but we still clip.
	 */
	if (type != F_UNLCK) {
		brange = new_range1;
		new_range1 = NULL;
		lf_create_range(brange, owner, type, flags, start, end);
		lf_insert(&lock->lf_range, brange, insert_point);
		insert_point = brange;
		if (flags & F_POSIX)
			++count;
	} else {
		brange = NULL;
	}

	/*
	 * Handle the double_clip case.  This is the only case where
	 * we wind up having to add TWO locks.
	 */
	if (double_clip) {
		KKASSERT(first_match == last_match);
		last_match = new_range2;
		new_range2 = NULL;
		lf_create_range(last_match, first_match->lf_owner,
				first_match->lf_type, first_match->lf_flags,
				end + 1, first_match->lf_end);
		first_match->lf_end = start - 1;
		first_match->lf_flags &= ~F_NOEND;

		/*
		 * Figure out where to insert the right side clip.
		 */
		lf_insert(&lock->lf_range, last_match, first_match);
		if (last_match->lf_flags & F_POSIX)
			++count;
	}

	/*
	 * Clip or destroy the locks between first_match and last_match,
	 * inclusive.  Ignore the primary lock we created (brange).  Note
	 * that if double-clipped, first_match and last_match will be
	 * outside our clipping range.  Otherwise first_match and last_match
	 * will be deleted.
	 *
	 * We have already taken care of any double clipping.
	 *
	 * The insert_point may become invalid as we delete records, do not
	 * use that pointer any more.  Also, when removing something other
	 * then 'range' we have to check to see if the item we are removing
	 * is 'next' and adjust 'next' properly.
	 *
	 * NOTE: brange will be NULL if F_UNLCKing.
	 */
	TAILQ_INIT(&deadlist);
	next = first_match;

	while ((range = next) != NULL) {
		next = TAILQ_NEXT(range, lf_link);

		/*
		 * Ignore elements that we do not own and ignore the
		 * primary request range which we just created.
		 */
		if (range->lf_owner != owner || range == brange)
			continue;

		/*
		 * We may have to wakeup a waiter when downgrading a lock.
		 */
		if (type == F_UNLCK)
			wakeup_needed = 1;
		if (type == F_RDLCK && range->lf_type == F_WRLCK)
			wakeup_needed = 1;

		/*
		 * Clip left.  This can only occur on first_match. 
		 *
		 * Merge the left clip with brange if possible.  This must
		 * be done specifically, not in the optimized merge heuristic
		 * below, since we may have counted on it in our 'count'
		 * calculation above.
		 */
		if (range->lf_start < start) {
			KKASSERT(range == first_match);
			if (brange &&
			    range->lf_end >= start - 1 &&
			    lf_match(range, type, flags)) {
				range->lf_end = brange->lf_end;
				range->lf_flags |= brange->lf_flags & F_NOEND;
				/*
				 * Removing something other then 'range',
				 * adjust 'next' if necessary.
				 */
				if (next == brange)
					next = TAILQ_NEXT(next, lf_link);
				TAILQ_REMOVE(&lock->lf_range, brange, lf_link);
				if (brange->lf_flags & F_POSIX)
					--count;
				TAILQ_INSERT_TAIL(&deadlist, brange, lf_link);
				brange = range;
			} else if (range->lf_end >= start) {
				range->lf_end = start - 1;
				if (type != F_UNLCK)
					range->lf_flags &= ~F_NOEND;
			}
			if (range == last_match)
				break;
			continue;
		}

		/*
		 * Clip right.  This can only occur on last_match. 
		 *
		 * Merge the right clip if possible.  This must be done
		 * specifically, not in the optimized merge heuristic
		 * below, since we may have counted on it in our 'count'
		 * calculation.
		 *
		 * Since we are adjusting lf_start, we have to move the
		 * record to maintain the sorted list.  Since lf_start is
		 * only getting larger we can use the next element as the
		 * insert point (we don't have to backtrack).
		 */
		if (range->lf_end > end) {
			KKASSERT(range == last_match);
			if (brange &&
			    range->lf_start <= end + 1 && 
			    lf_match(range, type, flags)) {
				brange->lf_end = range->lf_end;
				brange->lf_flags |= range->lf_flags & F_NOEND;
				TAILQ_REMOVE(&lock->lf_range, range, lf_link);
				if (range->lf_flags & F_POSIX)
					--count;
				TAILQ_INSERT_TAIL(&deadlist, range, lf_link);
			} else if (range->lf_start <= end) {
				range->lf_start = end + 1;
				TAILQ_REMOVE(&lock->lf_range, range, lf_link);
				lf_insert(&lock->lf_range, range, next);
			}
			/* range == last_match, we are done */
			break;
		}

		/*
		 * The record must be entirely enclosed.  Note that the
		 * record could be first_match or last_match, and will be
		 * deleted.
		 */
		KKASSERT(range->lf_start >= start && range->lf_end <= end);
		TAILQ_REMOVE(&lock->lf_range, range, lf_link);
		if (range->lf_flags & F_POSIX)
			--count;
		TAILQ_INSERT_TAIL(&deadlist, range, lf_link);
		if (range == last_match)
			break;
	}

	/*
	 * Attempt to merge locks adjacent to brange.  For example, we may
	 * have had to clip first_match and/or last_match, and they might
	 * be adjacent.  Or there might simply have been an adjacent lock
	 * already there.
	 *
	 * Don't get fancy, just check adjacent elements in the list if they
	 * happen to be owned by us.
	 *
	 * This case only gets hit if we have a situation where a shared
	 * and exclusive lock are adjacent, and the exclusive lock is 
	 * downgraded to shared or the shared lock is upgraded to exclusive.
	 */
	if (brange) {
		range = TAILQ_PREV(brange, lockf_range_list, lf_link);
		if (range &&
		    range->lf_owner == owner && 
		    range->lf_end == brange->lf_start - 1 &&
		    lf_match(range, type, flags)
		) {
			/*
			 * Extend range to cover brange and scrap brange.
			 */
			range->lf_end = brange->lf_end;
			range->lf_flags |= brange->lf_flags & F_NOEND;
			TAILQ_REMOVE(&lock->lf_range, brange, lf_link);
			if (brange->lf_flags & F_POSIX)
				--count;
			TAILQ_INSERT_TAIL(&deadlist, brange, lf_link);
			brange = range;
		}
		range = TAILQ_NEXT(brange, lf_link);
		if (range &&
		    range->lf_owner == owner &&
		    range->lf_start == brange->lf_end + 1 &&
		    lf_match(range, type, flags)
		) {
			/*
			 * Extend brange to cover range and scrap range.
			 */
			brange->lf_end = range->lf_end;
			brange->lf_flags |= range->lf_flags & F_NOEND;
			TAILQ_REMOVE(&lock->lf_range, range, lf_link);
			if (range->lf_flags & F_POSIX)
				--count;
			TAILQ_INSERT_TAIL(&deadlist, range, lf_link);
		}
	}

	/*
	 * Destroy deleted elements.  We didn't want to do it in the loop
	 * because the free() might have blocked.
	 *
	 * Adjust the count for any posix locks we thought we might create
	 * but didn't.
	 */
	while ((range = TAILQ_FIRST(&deadlist)) != NULL) {
		TAILQ_REMOVE(&deadlist, range, lf_link);
		lf_destroy_range(range);
	}

	KKASSERT(count <= 0);
	if (count < 0)
		lf_count_change(owner, count);
do_wakeup:
	lf_print_lock(lock);
	if (wakeup_needed)
		lf_wakeup(lock, start, end);
	error = 0;
do_cleanup:
	if (new_range1 != NULL)
		lf_destroy_range(new_range1);
	if (new_range2 != NULL)
		lf_destroy_range(new_range2);
	return(error);
}

/*
 * Check whether there is a blocking lock,
 * and if so return its process identifier.
 */
static int
lf_getlock(struct flock *fl, struct lockf *lock, struct proc *owner,
	   int type, int flags, off_t start, off_t end)
{
	struct lockf_range *range;

	TAILQ_FOREACH(range, &lock->lf_range, lf_link)
		if (range->lf_owner != owner &&
		    lf_overlap(range, start, end) &&
		    (type == F_WRLCK || range->lf_type == F_WRLCK))
			break;
	if (range == NULL) {
		fl->l_type = F_UNLCK;
		return(0);
	}
	fl->l_type = range->lf_type;
	fl->l_whence = SEEK_SET;
	fl->l_start = range->lf_start;
	if (range->lf_flags & F_NOEND)
		fl->l_len = 0;
	else
		fl->l_len = range->lf_end - range->lf_start + 1;
	if (range->lf_owner != NULL && (range->lf_flags & F_POSIX))
		fl->l_pid = range->lf_owner->p_pid;
	else
		fl->l_pid = -1;
	return(0);
}

/*
 * Wakeup pending lock attempts.  Theoretically we can stop as soon as
 * we encounter an exclusive request that covers the whole range (at least
 * insofar as the sleep code above calls lf_wakeup() if it would otherwise
 * exit instead of loop), but for now just wakeup all overlapping
 * requests.  XXX
 */
static void
lf_wakeup(struct lockf *lock, off_t start, off_t end)
{
	struct lockf_range *range, *nrange;

	TAILQ_FOREACH_MUTABLE(range, &lock->lf_blocked, lf_link, nrange) {
		if (lf_overlap(range, start, end) == 0)
			continue;
		TAILQ_REMOVE(&lock->lf_blocked, range, lf_link);
		range->lf_flags = 1;
		wakeup(range);
	}
}

/*
 * Allocate a range structure and initialize it sufficiently such that
 * lf_destroy_range() does not barf.
 *
 * Most use cases are temporary, implement a small 2-entry-per-cpu
 * cache.
 */
static struct lockf_range *
lf_alloc_range(void)
{
	struct lockf_range *range;
	struct lf_pcpu *lfpc;

	lfpc = &lf_pcpu_array[mycpuid];
	if ((range = lfpc->free1) != NULL) {
		lfpc->free1 = NULL;
		return range;
	}
	if ((range = lfpc->free2) != NULL) {
		lfpc->free2 = NULL;
		return range;
	}
	range = kmalloc(sizeof(struct lockf_range), M_LOCKF, M_WAITOK);
	range->lf_owner = NULL;

	return(range);
}

static void
lf_insert(struct lockf_range_list *list, struct lockf_range *elm,
	  struct lockf_range *insert_point)
{
	while (insert_point && insert_point->lf_start < elm->lf_start)
		insert_point = TAILQ_NEXT(insert_point, lf_link);
	if (insert_point != NULL)
		TAILQ_INSERT_BEFORE(insert_point, elm, lf_link);
	else
		TAILQ_INSERT_TAIL(list, elm, lf_link);
}

static void
lf_create_range(struct lockf_range *range, struct proc *owner, int type,
		int flags, off_t start, off_t end)
{
	KKASSERT(start <= end);
	range->lf_type = type;
	range->lf_flags = flags;
	range->lf_start = start;
	range->lf_end = end;
	range->lf_owner = owner;

	lf_printf("lf_create_range: %ju..%ju\n",
	    (uintmax_t)range->lf_start, (uintmax_t)range->lf_end);
}

static void
lf_destroy_range(struct lockf_range *range)
{
	struct lf_pcpu *lfpc;

	lf_printf("lf_destroy_range: %ju..%ju\n",
		  (uintmax_t)range->lf_start, (uintmax_t)range->lf_end);

	lfpc = &lf_pcpu_array[mycpuid];
	if (lfpc->free1 == NULL) {
		range->lf_owner = NULL;
		lfpc->free1 = range;
		return;
	}
	if (lfpc->free2 == NULL) {
		range->lf_owner = NULL;
		lfpc->free2 = range;
		return;
	}
	kfree(range, M_LOCKF);
}

#ifdef LOCKF_DEBUG

static void
_lf_printf(const char *ctl, ...)
{
	struct proc *p;
	__va_list va;

	if (lf_print_ranges) {
	    if ((p = curproc) != NULL)
		kprintf("pid %d (%s): ", p->p_pid, p->p_comm);
	}
	__va_start(va, ctl);
	kvprintf(ctl, va);
	__va_end(va);
}

static void
_lf_print_lock(const struct lockf *lock)
{
	struct lockf_range *range;

	if (lf_print_ranges == 0)
		return;

	if (TAILQ_EMPTY(&lock->lf_range)) {
		lf_printf("lockf %p: no ranges locked\n", lock);
	} else {
		lf_printf("lockf %p:\n", lock);
	}
	TAILQ_FOREACH(range, &lock->lf_range, lf_link)
		kprintf("\t%jd..%jd type %s owned by %d\n",
		       (uintmax_t)range->lf_start, (uintmax_t)range->lf_end,
		       range->lf_type == F_RDLCK ? "shared" : "exclusive",
		       range->lf_flags & F_POSIX ? range->lf_owner->p_pid : -1);
	if (TAILQ_EMPTY(&lock->lf_blocked))
		kprintf("no process waiting for range\n");
	else
		kprintf("blocked locks:");
	TAILQ_FOREACH(range, &lock->lf_blocked, lf_link)
		kprintf("\t%jd..%jd type %s waiting on %p\n",
		       (uintmax_t)range->lf_start, (uintmax_t)range->lf_end,
		       range->lf_type == F_RDLCK ? "shared" : "exclusive",
		       range);
}
#endif /* LOCKF_DEBUG */

static void
lf_init(void *dummy __unused)
{
	lf_pcpu_array = kmalloc(sizeof(*lf_pcpu_array) * ncpus,
				M_LOCKF, M_WAITOK | M_ZERO);
}

SYSINIT(lockf, SI_BOOT2_MACHDEP, SI_ORDER_ANY, lf_init, NULL);