vfs_lockf.c revision 1.61.6.3 1 /* $NetBSD: vfs_lockf.c,v 1.61.6.3 2008/09/28 10:40:54 mjf Exp $ */
2
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ufs_lockf.c 8.4 (Berkeley) 10/26/94
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: vfs_lockf.c,v 1.61.6.3 2008/09/28 10:40:54 mjf Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/pool.h>
47 #include <sys/fcntl.h>
48 #include <sys/lockf.h>
49 #include <sys/atomic.h>
50 #include <sys/kauth.h>
51
52 /*
53 * The lockf structure is a kernel structure which contains the information
54 * associated with a byte range lock. The lockf structures are linked into
55 * the vnode structure. Locks are sorted by the starting byte of the lock for
56 * efficiency.
57 *
58 * lf_next is used for two purposes, depending on whether the lock is
59 * being held, or is in conflict with an existing lock. If this lock
60 * is held, it indicates the next lock on the same vnode.
61 * For pending locks, if lock->lf_next is non-NULL, then lock->lf_block
62 * must be queued on the lf_blkhd TAILQ of lock->lf_next.
63 */
64
65 TAILQ_HEAD(locklist, lockf);
66
67 struct lockf {
68 kcondvar_t lf_cv; /* Signalling */
69 short lf_flags; /* Lock semantics: F_POSIX, F_FLOCK, F_WAIT */
70 short lf_type; /* Lock type: F_RDLCK, F_WRLCK */
71 off_t lf_start; /* The byte # of the start of the lock */
72 off_t lf_end; /* The byte # of the end of the lock (-1=EOF)*/
73 void *lf_id; /* process or file description holding lock */
74 struct lockf **lf_head; /* Back pointer to the head of lockf list */
75 struct lockf *lf_next; /* Next lock on this vnode, or blocking lock */
76 struct locklist lf_blkhd; /* List of requests blocked on this lock */
77 TAILQ_ENTRY(lockf) lf_block;/* A request waiting for a lock */
78 uid_t lf_uid; /* User ID responsible */
79 };
80
81 /* Maximum length of sleep chains to traverse to try and detect deadlock. */
82 #define MAXDEPTH 50
83
84 static pool_cache_t lockf_cache;
85 static kmutex_t *lockf_lock;
86 static char lockstr[] = "lockf";
87
88 /*
89 * This variable controls the maximum number of processes that will
90 * be checked in doing deadlock detection.
91 */
92 int maxlockdepth = MAXDEPTH;
93
94 #ifdef LOCKF_DEBUG
95 int lockf_debug = 0;
96 #endif
97
98 #define SELF 0x1
99 #define OTHERS 0x2
100
101 /*
102 * XXX TODO
103 * Misc cleanups: "void *id" should be visible in the API as a
104 * "struct proc *".
105 * (This requires rototilling all VFS's which support advisory locking).
106 */
107
108 /*
109 * If there's a lot of lock contention on a single vnode, locking
110 * schemes which allow for more paralleism would be needed. Given how
111 * infrequently byte-range locks are actually used in typical BSD
112 * code, a more complex approach probably isn't worth it.
113 */
114
115 /*
116 * We enforce a limit on locks by uid, so that a single user cannot
117 * run the kernel out of memory. For now, the limit is pretty coarse.
118 * There is no limit on root.
119 *
120 * Splitting a lock will always succeed, regardless of current allocations.
121 * If you're slightly above the limit, we still have to permit an allocation
122 * so that the unlock can succeed. If the unlocking causes too many splits,
123 * however, you're totally cutoff.
124 */
125 int maxlocksperuid = 1024;
126
127 #ifdef LOCKF_DEBUG
128 /*
129 * Print out a lock.
130 */
131 static void
132 lf_print(const char *tag, struct lockf *lock)
133 {
134
135 printf("%s: lock %p for ", tag, lock);
136 if (lock->lf_flags & F_POSIX)
137 printf("proc %d", ((struct proc *)lock->lf_id)->p_pid);
138 else
139 printf("file %p", (struct file *)lock->lf_id);
140 printf(" %s, start %qx, end %qx",
141 lock->lf_type == F_RDLCK ? "shared" :
142 lock->lf_type == F_WRLCK ? "exclusive" :
143 lock->lf_type == F_UNLCK ? "unlock" :
144 "unknown", lock->lf_start, lock->lf_end);
145 if (TAILQ_FIRST(&lock->lf_blkhd))
146 printf(" block %p\n", TAILQ_FIRST(&lock->lf_blkhd));
147 else
148 printf("\n");
149 }
150
151 static void
152 lf_printlist(const char *tag, struct lockf *lock)
153 {
154 struct lockf *lf, *blk;
155
156 printf("%s: Lock list:\n", tag);
157 for (lf = *lock->lf_head; lf; lf = lf->lf_next) {
158 printf("\tlock %p for ", lf);
159 if (lf->lf_flags & F_POSIX)
160 printf("proc %d", ((struct proc *)lf->lf_id)->p_pid);
161 else
162 printf("file %p", (struct file *)lf->lf_id);
163 printf(", %s, start %qx, end %qx",
164 lf->lf_type == F_RDLCK ? "shared" :
165 lf->lf_type == F_WRLCK ? "exclusive" :
166 lf->lf_type == F_UNLCK ? "unlock" :
167 "unknown", lf->lf_start, lf->lf_end);
168 TAILQ_FOREACH(blk, &lf->lf_blkhd, lf_block) {
169 if (blk->lf_flags & F_POSIX)
170 printf("; proc %d",
171 ((struct proc *)blk->lf_id)->p_pid);
172 else
173 printf("; file %p", (struct file *)blk->lf_id);
174 printf(", %s, start %qx, end %qx",
175 blk->lf_type == F_RDLCK ? "shared" :
176 blk->lf_type == F_WRLCK ? "exclusive" :
177 blk->lf_type == F_UNLCK ? "unlock" :
178 "unknown", blk->lf_start, blk->lf_end);
179 if (TAILQ_FIRST(&blk->lf_blkhd))
180 panic("lf_printlist: bad list");
181 }
182 printf("\n");
183 }
184 }
185 #endif /* LOCKF_DEBUG */
186
187 /*
188 * 3 options for allowfail.
189 * 0 - always allocate. 1 - cutoff at limit. 2 - cutoff at double limit.
190 */
191 static struct lockf *
192 lf_alloc(uid_t uid, int allowfail)
193 {
194 struct uidinfo *uip;
195 struct lockf *lock;
196 u_long lcnt;
197
198 uip = uid_find(uid);
199 lcnt = atomic_inc_ulong_nv(&uip->ui_lockcnt);
200 if (uid && allowfail && lcnt >
201 (allowfail == 1 ? maxlocksperuid : (maxlocksperuid * 2))) {
202 atomic_dec_ulong(&uip->ui_lockcnt);
203 return NULL;
204 }
205
206 lock = pool_cache_get(lockf_cache, PR_WAITOK);
207 lock->lf_uid = uid;
208 return lock;
209 }
210
211 static void
212 lf_free(struct lockf *lock)
213 {
214 struct uidinfo *uip;
215
216 uip = uid_find(lock->lf_uid);
217 atomic_dec_ulong(&uip->ui_lockcnt);
218 pool_cache_put(lockf_cache, lock);
219 }
220
221 static int
222 lf_ctor(void *arg, void *obj, int flag)
223 {
224 struct lockf *lock;
225
226 lock = obj;
227 cv_init(&lock->lf_cv, lockstr);
228
229 return 0;
230 }
231
232 static void
233 lf_dtor(void *arg, void *obj)
234 {
235 struct lockf *lock;
236
237 lock = obj;
238 cv_destroy(&lock->lf_cv);
239 }
240
241 /*
242 * Walk the list of locks for an inode to
243 * find an overlapping lock (if any).
244 *
245 * NOTE: this returns only the FIRST overlapping lock. There
246 * may be more than one.
247 */
248 static int
249 lf_findoverlap(struct lockf *lf, struct lockf *lock, int type,
250 struct lockf ***prev, struct lockf **overlap)
251 {
252 off_t start, end;
253
254 *overlap = lf;
255 if (lf == NULL)
256 return 0;
257 #ifdef LOCKF_DEBUG
258 if (lockf_debug & 2)
259 lf_print("lf_findoverlap: looking for overlap in", lock);
260 #endif /* LOCKF_DEBUG */
261 start = lock->lf_start;
262 end = lock->lf_end;
263 while (lf != NULL) {
264 if (((type == SELF) && lf->lf_id != lock->lf_id) ||
265 ((type == OTHERS) && lf->lf_id == lock->lf_id)) {
266 *prev = &lf->lf_next;
267 *overlap = lf = lf->lf_next;
268 continue;
269 }
270 #ifdef LOCKF_DEBUG
271 if (lockf_debug & 2)
272 lf_print("\tchecking", lf);
273 #endif /* LOCKF_DEBUG */
274 /*
275 * OK, check for overlap
276 *
277 * Six cases:
278 * 0) no overlap
279 * 1) overlap == lock
280 * 2) overlap contains lock
281 * 3) lock contains overlap
282 * 4) overlap starts before lock
283 * 5) overlap ends after lock
284 */
285 if ((lf->lf_end != -1 && start > lf->lf_end) ||
286 (end != -1 && lf->lf_start > end)) {
287 /* Case 0 */
288 #ifdef LOCKF_DEBUG
289 if (lockf_debug & 2)
290 printf("no overlap\n");
291 #endif /* LOCKF_DEBUG */
292 if ((type & SELF) && end != -1 && lf->lf_start > end)
293 return 0;
294 *prev = &lf->lf_next;
295 *overlap = lf = lf->lf_next;
296 continue;
297 }
298 if ((lf->lf_start == start) && (lf->lf_end == end)) {
299 /* Case 1 */
300 #ifdef LOCKF_DEBUG
301 if (lockf_debug & 2)
302 printf("overlap == lock\n");
303 #endif /* LOCKF_DEBUG */
304 return 1;
305 }
306 if ((lf->lf_start <= start) &&
307 (end != -1) &&
308 ((lf->lf_end >= end) || (lf->lf_end == -1))) {
309 /* Case 2 */
310 #ifdef LOCKF_DEBUG
311 if (lockf_debug & 2)
312 printf("overlap contains lock\n");
313 #endif /* LOCKF_DEBUG */
314 return 2;
315 }
316 if (start <= lf->lf_start &&
317 (end == -1 ||
318 (lf->lf_end != -1 && end >= lf->lf_end))) {
319 /* Case 3 */
320 #ifdef LOCKF_DEBUG
321 if (lockf_debug & 2)
322 printf("lock contains overlap\n");
323 #endif /* LOCKF_DEBUG */
324 return 3;
325 }
326 if ((lf->lf_start < start) &&
327 ((lf->lf_end >= start) || (lf->lf_end == -1))) {
328 /* Case 4 */
329 #ifdef LOCKF_DEBUG
330 if (lockf_debug & 2)
331 printf("overlap starts before lock\n");
332 #endif /* LOCKF_DEBUG */
333 return 4;
334 }
335 if ((lf->lf_start > start) &&
336 (end != -1) &&
337 ((lf->lf_end > end) || (lf->lf_end == -1))) {
338 /* Case 5 */
339 #ifdef LOCKF_DEBUG
340 if (lockf_debug & 2)
341 printf("overlap ends after lock\n");
342 #endif /* LOCKF_DEBUG */
343 return 5;
344 }
345 panic("lf_findoverlap: default");
346 }
347 return 0;
348 }
349
350 /*
351 * Split a lock and a contained region into
352 * two or three locks as necessary.
353 */
354 static void
355 lf_split(struct lockf *lock1, struct lockf *lock2, struct lockf **sparelock)
356 {
357 struct lockf *splitlock;
358
359 #ifdef LOCKF_DEBUG
360 if (lockf_debug & 2) {
361 lf_print("lf_split", lock1);
362 lf_print("splitting from", lock2);
363 }
364 #endif /* LOCKF_DEBUG */
365 /*
366 * Check to see if spliting into only two pieces.
367 */
368 if (lock1->lf_start == lock2->lf_start) {
369 lock1->lf_start = lock2->lf_end + 1;
370 lock2->lf_next = lock1;
371 return;
372 }
373 if (lock1->lf_end == lock2->lf_end) {
374 lock1->lf_end = lock2->lf_start - 1;
375 lock2->lf_next = lock1->lf_next;
376 lock1->lf_next = lock2;
377 return;
378 }
379 /*
380 * Make a new lock consisting of the last part of
381 * the encompassing lock
382 */
383 splitlock = *sparelock;
384 *sparelock = NULL;
385 memcpy(splitlock, lock1, sizeof(*splitlock));
386 cv_init(&splitlock->lf_cv, lockstr);
387
388 splitlock->lf_start = lock2->lf_end + 1;
389 TAILQ_INIT(&splitlock->lf_blkhd);
390 lock1->lf_end = lock2->lf_start - 1;
391 /*
392 * OK, now link it in
393 */
394 splitlock->lf_next = lock1->lf_next;
395 lock2->lf_next = splitlock;
396 lock1->lf_next = lock2;
397 }
398
399 /*
400 * Wakeup a blocklist
401 */
402 static void
403 lf_wakelock(struct lockf *listhead)
404 {
405 struct lockf *wakelock;
406
407 while ((wakelock = TAILQ_FIRST(&listhead->lf_blkhd))) {
408 KASSERT(wakelock->lf_next == listhead);
409 TAILQ_REMOVE(&listhead->lf_blkhd, wakelock, lf_block);
410 wakelock->lf_next = NULL;
411 #ifdef LOCKF_DEBUG
412 if (lockf_debug & 2)
413 lf_print("lf_wakelock: awakening", wakelock);
414 #endif
415 cv_broadcast(&wakelock->lf_cv);
416 }
417 }
418
419 /*
420 * Remove a byte-range lock on an inode.
421 *
422 * Generally, find the lock (or an overlap to that lock)
423 * and remove it (or shrink it), then wakeup anyone we can.
424 */
425 static int
426 lf_clearlock(struct lockf *unlock, struct lockf **sparelock)
427 {
428 struct lockf **head = unlock->lf_head;
429 struct lockf *lf = *head;
430 struct lockf *overlap, **prev;
431 int ovcase;
432
433 if (lf == NULL)
434 return 0;
435 #ifdef LOCKF_DEBUG
436 if (unlock->lf_type != F_UNLCK)
437 panic("lf_clearlock: bad type");
438 if (lockf_debug & 1)
439 lf_print("lf_clearlock", unlock);
440 #endif /* LOCKF_DEBUG */
441 prev = head;
442 while ((ovcase = lf_findoverlap(lf, unlock, SELF,
443 &prev, &overlap)) != 0) {
444 /*
445 * Wakeup the list of locks to be retried.
446 */
447 lf_wakelock(overlap);
448
449 switch (ovcase) {
450
451 case 1: /* overlap == lock */
452 *prev = overlap->lf_next;
453 lf_free(overlap);
454 break;
455
456 case 2: /* overlap contains lock: split it */
457 if (overlap->lf_start == unlock->lf_start) {
458 overlap->lf_start = unlock->lf_end + 1;
459 break;
460 }
461 lf_split(overlap, unlock, sparelock);
462 overlap->lf_next = unlock->lf_next;
463 break;
464
465 case 3: /* lock contains overlap */
466 *prev = overlap->lf_next;
467 lf = overlap->lf_next;
468 lf_free(overlap);
469 continue;
470
471 case 4: /* overlap starts before lock */
472 overlap->lf_end = unlock->lf_start - 1;
473 prev = &overlap->lf_next;
474 lf = overlap->lf_next;
475 continue;
476
477 case 5: /* overlap ends after lock */
478 overlap->lf_start = unlock->lf_end + 1;
479 break;
480 }
481 break;
482 }
483 #ifdef LOCKF_DEBUG
484 if (lockf_debug & 1)
485 lf_printlist("lf_clearlock", unlock);
486 #endif /* LOCKF_DEBUG */
487 return 0;
488 }
489
490 /*
491 * Walk the list of locks for an inode and
492 * return the first blocking lock.
493 */
494 static struct lockf *
495 lf_getblock(struct lockf *lock)
496 {
497 struct lockf **prev, *overlap, *lf = *(lock->lf_head);
498
499 prev = lock->lf_head;
500 while (lf_findoverlap(lf, lock, OTHERS, &prev, &overlap) != 0) {
501 /*
502 * We've found an overlap, see if it blocks us
503 */
504 if ((lock->lf_type == F_WRLCK || overlap->lf_type == F_WRLCK))
505 return overlap;
506 /*
507 * Nope, point to the next one on the list and
508 * see if it blocks us
509 */
510 lf = overlap->lf_next;
511 }
512 return NULL;
513 }
514
515 /*
516 * Set a byte-range lock.
517 */
518 static int
519 lf_setlock(struct lockf *lock, struct lockf **sparelock,
520 kmutex_t *interlock)
521 {
522 struct lockf *block;
523 struct lockf **head = lock->lf_head;
524 struct lockf **prev, *overlap, *ltmp;
525 int ovcase, needtolink, error;
526
527 #ifdef LOCKF_DEBUG
528 if (lockf_debug & 1)
529 lf_print("lf_setlock", lock);
530 #endif /* LOCKF_DEBUG */
531
532 /*
533 * Scan lock list for this file looking for locks that would block us.
534 */
535 while ((block = lf_getblock(lock)) != NULL) {
536 /*
537 * Free the structure and return if nonblocking.
538 */
539 if ((lock->lf_flags & F_WAIT) == 0) {
540 lf_free(lock);
541 return EAGAIN;
542 }
543 /*
544 * We are blocked. Since flock style locks cover
545 * the whole file, there is no chance for deadlock.
546 * For byte-range locks we must check for deadlock.
547 *
548 * Deadlock detection is done by looking through the
549 * wait channels to see if there are any cycles that
550 * involve us. MAXDEPTH is set just to make sure we
551 * do not go off into neverneverland.
552 */
553 if ((lock->lf_flags & F_POSIX) &&
554 (block->lf_flags & F_POSIX)) {
555 struct lwp *wlwp;
556 volatile const struct lockf *waitblock;
557 int i = 0;
558 struct proc *p;
559
560 p = (struct proc *)block->lf_id;
561 KASSERT(p != NULL);
562 while (i++ < maxlockdepth) {
563 mutex_enter(p->p_lock);
564 if (p->p_nlwps > 1) {
565 mutex_exit(p->p_lock);
566 break;
567 }
568 wlwp = LIST_FIRST(&p->p_lwps);
569 lwp_lock(wlwp);
570 if (wlwp->l_wchan == NULL ||
571 wlwp->l_wmesg != lockstr) {
572 lwp_unlock(wlwp);
573 mutex_exit(p->p_lock);
574 break;
575 }
576 waitblock = wlwp->l_wchan;
577 lwp_unlock(wlwp);
578 mutex_exit(p->p_lock);
579 /* Get the owner of the blocking lock */
580 waitblock = waitblock->lf_next;
581 if ((waitblock->lf_flags & F_POSIX) == 0)
582 break;
583 p = (struct proc *)waitblock->lf_id;
584 if (p == curproc) {
585 lf_free(lock);
586 return EDEADLK;
587 }
588 }
589 /*
590 * If we're still following a dependency chain
591 * after maxlockdepth iterations, assume we're in
592 * a cycle to be safe.
593 */
594 if (i >= maxlockdepth) {
595 lf_free(lock);
596 return EDEADLK;
597 }
598 }
599 /*
600 * For flock type locks, we must first remove
601 * any shared locks that we hold before we sleep
602 * waiting for an exclusive lock.
603 */
604 if ((lock->lf_flags & F_FLOCK) &&
605 lock->lf_type == F_WRLCK) {
606 lock->lf_type = F_UNLCK;
607 (void) lf_clearlock(lock, NULL);
608 lock->lf_type = F_WRLCK;
609 }
610 /*
611 * Add our lock to the blocked list and sleep until we're free.
612 * Remember who blocked us (for deadlock detection).
613 */
614 lock->lf_next = block;
615 TAILQ_INSERT_TAIL(&block->lf_blkhd, lock, lf_block);
616 #ifdef LOCKF_DEBUG
617 if (lockf_debug & 1) {
618 lf_print("lf_setlock: blocking on", block);
619 lf_printlist("lf_setlock", block);
620 }
621 #endif /* LOCKF_DEBUG */
622 error = cv_wait_sig(&lock->lf_cv, interlock);
623
624 /*
625 * We may have been awoken by a signal (in
626 * which case we must remove ourselves from the
627 * blocked list) and/or by another process
628 * releasing a lock (in which case we have already
629 * been removed from the blocked list and our
630 * lf_next field set to NULL).
631 */
632 if (lock->lf_next != NULL) {
633 TAILQ_REMOVE(&lock->lf_next->lf_blkhd, lock, lf_block);
634 lock->lf_next = NULL;
635 }
636 if (error) {
637 lf_free(lock);
638 return error;
639 }
640 }
641 /*
642 * No blocks!! Add the lock. Note that we will
643 * downgrade or upgrade any overlapping locks this
644 * process already owns.
645 *
646 * Skip over locks owned by other processes.
647 * Handle any locks that overlap and are owned by ourselves.
648 */
649 prev = head;
650 block = *head;
651 needtolink = 1;
652 for (;;) {
653 ovcase = lf_findoverlap(block, lock, SELF, &prev, &overlap);
654 if (ovcase)
655 block = overlap->lf_next;
656 /*
657 * Six cases:
658 * 0) no overlap
659 * 1) overlap == lock
660 * 2) overlap contains lock
661 * 3) lock contains overlap
662 * 4) overlap starts before lock
663 * 5) overlap ends after lock
664 */
665 switch (ovcase) {
666 case 0: /* no overlap */
667 if (needtolink) {
668 *prev = lock;
669 lock->lf_next = overlap;
670 }
671 break;
672
673 case 1: /* overlap == lock */
674 /*
675 * If downgrading lock, others may be
676 * able to acquire it.
677 */
678 if (lock->lf_type == F_RDLCK &&
679 overlap->lf_type == F_WRLCK)
680 lf_wakelock(overlap);
681 overlap->lf_type = lock->lf_type;
682 lf_free(lock);
683 lock = overlap; /* for debug output below */
684 break;
685
686 case 2: /* overlap contains lock */
687 /*
688 * Check for common starting point and different types.
689 */
690 if (overlap->lf_type == lock->lf_type) {
691 lf_free(lock);
692 lock = overlap; /* for debug output below */
693 break;
694 }
695 if (overlap->lf_start == lock->lf_start) {
696 *prev = lock;
697 lock->lf_next = overlap;
698 overlap->lf_start = lock->lf_end + 1;
699 } else
700 lf_split(overlap, lock, sparelock);
701 lf_wakelock(overlap);
702 break;
703
704 case 3: /* lock contains overlap */
705 /*
706 * If downgrading lock, others may be able to
707 * acquire it, otherwise take the list.
708 */
709 if (lock->lf_type == F_RDLCK &&
710 overlap->lf_type == F_WRLCK) {
711 lf_wakelock(overlap);
712 } else {
713 while ((ltmp = TAILQ_FIRST(&overlap->lf_blkhd))) {
714 KASSERT(ltmp->lf_next == overlap);
715 TAILQ_REMOVE(&overlap->lf_blkhd, ltmp,
716 lf_block);
717 ltmp->lf_next = lock;
718 TAILQ_INSERT_TAIL(&lock->lf_blkhd,
719 ltmp, lf_block);
720 }
721 }
722 /*
723 * Add the new lock if necessary and delete the overlap.
724 */
725 if (needtolink) {
726 *prev = lock;
727 lock->lf_next = overlap->lf_next;
728 prev = &lock->lf_next;
729 needtolink = 0;
730 } else
731 *prev = overlap->lf_next;
732 lf_free(overlap);
733 continue;
734
735 case 4: /* overlap starts before lock */
736 /*
737 * Add lock after overlap on the list.
738 */
739 lock->lf_next = overlap->lf_next;
740 overlap->lf_next = lock;
741 overlap->lf_end = lock->lf_start - 1;
742 prev = &lock->lf_next;
743 lf_wakelock(overlap);
744 needtolink = 0;
745 continue;
746
747 case 5: /* overlap ends after lock */
748 /*
749 * Add the new lock before overlap.
750 */
751 if (needtolink) {
752 *prev = lock;
753 lock->lf_next = overlap;
754 }
755 overlap->lf_start = lock->lf_end + 1;
756 lf_wakelock(overlap);
757 break;
758 }
759 break;
760 }
761 #ifdef LOCKF_DEBUG
762 if (lockf_debug & 1) {
763 lf_print("lf_setlock: got the lock", lock);
764 lf_printlist("lf_setlock", lock);
765 }
766 #endif /* LOCKF_DEBUG */
767 return 0;
768 }
769
770 /*
771 * Check whether there is a blocking lock,
772 * and if so return its process identifier.
773 */
774 static int
775 lf_getlock(struct lockf *lock, struct flock *fl)
776 {
777 struct lockf *block;
778
779 #ifdef LOCKF_DEBUG
780 if (lockf_debug & 1)
781 lf_print("lf_getlock", lock);
782 #endif /* LOCKF_DEBUG */
783
784 if ((block = lf_getblock(lock)) != NULL) {
785 fl->l_type = block->lf_type;
786 fl->l_whence = SEEK_SET;
787 fl->l_start = block->lf_start;
788 if (block->lf_end == -1)
789 fl->l_len = 0;
790 else
791 fl->l_len = block->lf_end - block->lf_start + 1;
792 if (block->lf_flags & F_POSIX)
793 fl->l_pid = ((struct proc *)block->lf_id)->p_pid;
794 else
795 fl->l_pid = -1;
796 } else {
797 fl->l_type = F_UNLCK;
798 }
799 return 0;
800 }
801
802 /*
803 * Do an advisory lock operation.
804 */
805 int
806 lf_advlock(struct vop_advlock_args *ap, struct lockf **head, off_t size)
807 {
808 struct lwp *l = curlwp;
809 struct flock *fl = ap->a_fl;
810 struct lockf *lock = NULL;
811 struct lockf *sparelock;
812 kmutex_t *interlock = lockf_lock;
813 off_t start, end;
814 int error = 0;
815
816 /*
817 * Convert the flock structure into a start and end.
818 */
819 switch (fl->l_whence) {
820 case SEEK_SET:
821 case SEEK_CUR:
822 /*
823 * Caller is responsible for adding any necessary offset
824 * when SEEK_CUR is used.
825 */
826 start = fl->l_start;
827 break;
828
829 case SEEK_END:
830 start = size + fl->l_start;
831 break;
832
833 default:
834 return EINVAL;
835 }
836 if (start < 0)
837 return EINVAL;
838
839 /*
840 * Allocate locks before acquiring the interlock. We need two
841 * locks in the worst case.
842 */
843 switch (ap->a_op) {
844 case F_SETLK:
845 case F_UNLCK:
846 /*
847 * XXX For F_UNLCK case, we can re-use the lock.
848 */
849 if ((ap->a_flags & F_FLOCK) == 0) {
850 /*
851 * Byte-range lock might need one more lock.
852 */
853 sparelock = lf_alloc(kauth_cred_geteuid(l->l_cred), 0);
854 if (sparelock == NULL) {
855 error = ENOMEM;
856 goto quit;
857 }
858 break;
859 }
860 /* FALLTHROUGH */
861
862 case F_GETLK:
863 sparelock = NULL;
864 break;
865
866 default:
867 return EINVAL;
868 }
869
870 lock = lf_alloc(kauth_cred_geteuid(l->l_cred),
871 ap->a_op != F_UNLCK ? 1 : 2);
872 if (lock == NULL) {
873 error = ENOMEM;
874 goto quit;
875 }
876
877 mutex_enter(interlock);
878
879 /*
880 * Avoid the common case of unlocking when inode has no locks.
881 */
882 if (*head == (struct lockf *)0) {
883 if (ap->a_op != F_SETLK) {
884 fl->l_type = F_UNLCK;
885 error = 0;
886 goto quit_unlock;
887 }
888 }
889
890 if (fl->l_len == 0)
891 end = -1;
892 else
893 end = start + fl->l_len - 1;
894 /*
895 * Create the lockf structure.
896 */
897 lock->lf_start = start;
898 lock->lf_end = end;
899 lock->lf_head = head;
900 lock->lf_type = fl->l_type;
901 lock->lf_next = (struct lockf *)0;
902 TAILQ_INIT(&lock->lf_blkhd);
903 lock->lf_flags = ap->a_flags;
904 if (lock->lf_flags & F_POSIX) {
905 KASSERT(curproc == (struct proc *)ap->a_id);
906 }
907 lock->lf_id = (struct proc *)ap->a_id;
908
909 /*
910 * Do the requested operation.
911 */
912 switch (ap->a_op) {
913
914 case F_SETLK:
915 error = lf_setlock(lock, &sparelock, interlock);
916 lock = NULL; /* lf_setlock freed it */
917 break;
918
919 case F_UNLCK:
920 error = lf_clearlock(lock, &sparelock);
921 break;
922
923 case F_GETLK:
924 error = lf_getlock(lock, fl);
925 break;
926
927 default:
928 break;
929 /* NOTREACHED */
930 }
931
932 quit_unlock:
933 mutex_exit(interlock);
934 quit:
935 if (lock)
936 lf_free(lock);
937 if (sparelock)
938 lf_free(sparelock);
939
940 return error;
941 }
942
943 /*
944 * Initialize subsystem. XXX We use a global lock. This could be the
945 * vnode interlock, but the deadlock detection code may need to inspect
946 * locks belonging to other files.
947 */
948 void
949 lf_init(void)
950 {
951
952 lockf_cache = pool_cache_init(sizeof(struct lockf), 0, 0, 0, "lockf",
953 NULL, IPL_NONE, lf_ctor, lf_dtor, NULL);
954 lockf_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
955 }
956