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