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