2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Copyright (c) 1982, 1986, 1989, 1993
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software contributed to Berkeley by
32 * Scooter Morris at Genentech Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)ufs_lockf.c 8.3 (Berkeley) 1/6/94
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include "opt_debug_lockf.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/limits.h>
72 #include <sys/mount.h>
73 #include <sys/mutex.h>
76 #include <sys/unistd.h>
77 #include <sys/vnode.h>
78 #include <sys/malloc.h>
79 #include <sys/fcntl.h>
80 #include <sys/lockf.h>
81 #include <sys/taskqueue.h>
84 #include <sys/sysctl.h>
86 #include <ufs/ufs/quota.h>
87 #include <ufs/ufs/inode.h>
89 static int lockf_debug = 0; /* control debug output */
90 SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
93 MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
97 struct owner_vertex_list;
100 #define NOLOCKF (struct lockf_entry *)0
103 static void lf_init(void *);
104 static int lf_hash_owner(caddr_t, struct flock *, int);
105 static int lf_owner_matches(struct lock_owner *, caddr_t, struct flock *,
107 static struct lockf_entry *
108 lf_alloc_lock(struct lock_owner *);
109 static void lf_free_lock(struct lockf_entry *);
110 static int lf_clearlock(struct lockf *, struct lockf_entry *);
111 static int lf_overlaps(struct lockf_entry *, struct lockf_entry *);
112 static int lf_blocks(struct lockf_entry *, struct lockf_entry *);
113 static void lf_free_edge(struct lockf_edge *);
114 static struct lockf_edge *
116 static void lf_alloc_vertex(struct lockf_entry *);
117 static int lf_add_edge(struct lockf_entry *, struct lockf_entry *);
118 static void lf_remove_edge(struct lockf_edge *);
119 static void lf_remove_outgoing(struct lockf_entry *);
120 static void lf_remove_incoming(struct lockf_entry *);
121 static int lf_add_outgoing(struct lockf *, struct lockf_entry *);
122 static int lf_add_incoming(struct lockf *, struct lockf_entry *);
123 static int lf_findoverlap(struct lockf_entry **, struct lockf_entry *,
125 static struct lockf_entry *
126 lf_getblock(struct lockf *, struct lockf_entry *);
127 static int lf_getlock(struct lockf *, struct lockf_entry *, struct flock *);
128 static void lf_insert_lock(struct lockf *, struct lockf_entry *);
129 static void lf_wakeup_lock(struct lockf *, struct lockf_entry *);
130 static void lf_update_dependancies(struct lockf *, struct lockf_entry *,
131 int all, struct lockf_entry_list *);
132 static void lf_set_start(struct lockf *, struct lockf_entry *, off_t,
133 struct lockf_entry_list*);
134 static void lf_set_end(struct lockf *, struct lockf_entry *, off_t,
135 struct lockf_entry_list*);
136 static int lf_setlock(struct lockf *, struct lockf_entry *,
137 struct vnode *, void **cookiep);
138 static int lf_cancel(struct lockf *, struct lockf_entry *, void *);
139 static void lf_split(struct lockf *, struct lockf_entry *,
140 struct lockf_entry *, struct lockf_entry_list *);
142 static int graph_reaches(struct owner_vertex *x, struct owner_vertex *y,
143 struct owner_vertex_list *path);
144 static void graph_check(struct owner_graph *g, int checkorder);
145 static void graph_print_vertices(struct owner_vertex_list *set);
147 static int graph_delta_forward(struct owner_graph *g,
148 struct owner_vertex *x, struct owner_vertex *y,
149 struct owner_vertex_list *delta);
150 static int graph_delta_backward(struct owner_graph *g,
151 struct owner_vertex *x, struct owner_vertex *y,
152 struct owner_vertex_list *delta);
153 static int graph_add_indices(int *indices, int n,
154 struct owner_vertex_list *set);
155 static int graph_assign_indices(struct owner_graph *g, int *indices,
156 int nextunused, struct owner_vertex_list *set);
157 static int graph_add_edge(struct owner_graph *g,
158 struct owner_vertex *x, struct owner_vertex *y);
159 static void graph_remove_edge(struct owner_graph *g,
160 struct owner_vertex *x, struct owner_vertex *y);
161 static struct owner_vertex *graph_alloc_vertex(struct owner_graph *g,
162 struct lock_owner *lo);
163 static void graph_free_vertex(struct owner_graph *g,
164 struct owner_vertex *v);
165 static struct owner_graph * graph_init(struct owner_graph *g);
167 static void lf_print(char *, struct lockf_entry *);
168 static void lf_printlist(char *, struct lockf_entry *);
169 static void lf_print_owner(struct lock_owner *);
173 * This structure is used to keep track of both local and remote lock
174 * owners. The lf_owner field of the struct lockf_entry points back at
175 * the lock owner structure. Each possible lock owner (local proc for
176 * POSIX fcntl locks, local file for BSD flock locks or <pid,sysid>
177 * pair for remote locks) is represented by a unique instance of
180 * If a lock owner has a lock that blocks some other lock or a lock
181 * that is waiting for some other lock, it also has a vertex in the
185 * (s) locked by state->ls_lock
186 * (S) locked by lf_lock_states_lock
187 * (l) locked by lf_lock_owners_lock
188 * (g) locked by lf_owner_graph_lock
189 * (c) const until freeing
191 #define LOCK_OWNER_HASH_SIZE 256
194 LIST_ENTRY(lock_owner) lo_link; /* (l) hash chain */
195 int lo_refs; /* (l) Number of locks referring to this */
196 int lo_flags; /* (c) Flags passwd to lf_advlock */
197 caddr_t lo_id; /* (c) Id value passed to lf_advlock */
198 pid_t lo_pid; /* (c) Process Id of the lock owner */
199 int lo_sysid; /* (c) System Id of the lock owner */
200 struct owner_vertex *lo_vertex; /* (g) entry in deadlock graph */
203 LIST_HEAD(lock_owner_list, lock_owner);
205 static struct sx lf_lock_states_lock;
206 static struct lockf_list lf_lock_states; /* (S) */
207 static struct sx lf_lock_owners_lock;
208 static struct lock_owner_list lf_lock_owners[LOCK_OWNER_HASH_SIZE]; /* (l) */
211 * Structures for deadlock detection.
213 * We have two types of directed graph, the first is the set of locks,
214 * both active and pending on a vnode. Within this graph, active locks
215 * are terminal nodes in the graph (i.e. have no out-going
216 * edges). Pending locks have out-going edges to each blocking active
217 * lock that prevents the lock from being granted and also to each
218 * older pending lock that would block them if it was active. The
219 * graph for each vnode is naturally acyclic; new edges are only ever
220 * added to or from new nodes (either new pending locks which only add
221 * out-going edges or new active locks which only add in-coming edges)
222 * therefore they cannot create loops in the lock graph.
224 * The second graph is a global graph of lock owners. Each lock owner
225 * is a vertex in that graph and an edge is added to the graph
226 * whenever an edge is added to a vnode graph, with end points
227 * corresponding to owner of the new pending lock and the owner of the
228 * lock upon which it waits. In order to prevent deadlock, we only add
229 * an edge to this graph if the new edge would not create a cycle.
231 * The lock owner graph is topologically sorted, i.e. if a node has
232 * any outgoing edges, then it has an order strictly less than any
233 * node to which it has an outgoing edge. We preserve this ordering
234 * (and detect cycles) on edge insertion using Algorithm PK from the
235 * paper "A Dynamic Topological Sort Algorithm for Directed Acyclic
236 * Graphs" (ACM Journal of Experimental Algorithms, Vol 11, Article
242 LIST_ENTRY(owner_edge) e_outlink; /* (g) link from's out-edge list */
243 LIST_ENTRY(owner_edge) e_inlink; /* (g) link to's in-edge list */
244 int e_refs; /* (g) number of times added */
245 struct owner_vertex *e_from; /* (c) out-going from here */
246 struct owner_vertex *e_to; /* (c) in-coming to here */
248 LIST_HEAD(owner_edge_list, owner_edge);
250 struct owner_vertex {
251 TAILQ_ENTRY(owner_vertex) v_link; /* (g) workspace for edge insertion */
252 uint32_t v_gen; /* (g) workspace for edge insertion */
253 int v_order; /* (g) order of vertex in graph */
254 struct owner_edge_list v_outedges;/* (g) list of out-edges */
255 struct owner_edge_list v_inedges; /* (g) list of in-edges */
256 struct lock_owner *v_owner; /* (c) corresponding lock owner */
258 TAILQ_HEAD(owner_vertex_list, owner_vertex);
261 struct owner_vertex** g_vertices; /* (g) pointers to vertices */
262 int g_size; /* (g) number of vertices */
263 int g_space; /* (g) space allocated for vertices */
264 int *g_indexbuf; /* (g) workspace for loop detection */
265 uint32_t g_gen; /* (g) increment when re-ordering */
268 static struct sx lf_owner_graph_lock;
269 static struct owner_graph lf_owner_graph;
272 * Initialise various structures and locks.
279 sx_init(&lf_lock_states_lock, "lock states lock");
280 LIST_INIT(&lf_lock_states);
282 sx_init(&lf_lock_owners_lock, "lock owners lock");
283 for (i = 0; i < LOCK_OWNER_HASH_SIZE; i++)
284 LIST_INIT(&lf_lock_owners[i]);
286 sx_init(&lf_owner_graph_lock, "owner graph lock");
287 graph_init(&lf_owner_graph);
289 SYSINIT(lf_init, SI_SUB_LOCK, SI_ORDER_FIRST, lf_init, NULL);
292 * Generate a hash value for a lock owner.
295 lf_hash_owner(caddr_t id, struct flock *fl, int flags)
299 if (flags & F_REMOTE) {
300 h = HASHSTEP(0, fl->l_pid);
301 h = HASHSTEP(h, fl->l_sysid);
302 } else if (flags & F_FLOCK) {
303 h = ((uintptr_t) id) >> 7;
305 struct proc *p = (struct proc *) id;
306 h = HASHSTEP(0, p->p_pid);
310 return (h % LOCK_OWNER_HASH_SIZE);
314 * Return true if a lock owner matches the details passed to
318 lf_owner_matches(struct lock_owner *lo, caddr_t id, struct flock *fl,
321 if (flags & F_REMOTE) {
322 return lo->lo_pid == fl->l_pid
323 && lo->lo_sysid == fl->l_sysid;
325 return lo->lo_id == id;
329 static struct lockf_entry *
330 lf_alloc_lock(struct lock_owner *lo)
332 struct lockf_entry *lf;
334 lf = malloc(sizeof(struct lockf_entry), M_LOCKF, M_WAITOK|M_ZERO);
338 printf("Allocated lock %p\n", lf);
341 sx_xlock(&lf_lock_owners_lock);
343 sx_xunlock(&lf_lock_owners_lock);
351 lf_free_lock(struct lockf_entry *lock)
354 * Adjust the lock_owner reference count and
355 * reclaim the entry if this is the last lock
358 struct lock_owner *lo = lock->lf_owner;
360 KASSERT(LIST_EMPTY(&lock->lf_outedges),
361 ("freeing lock with dependancies"));
362 KASSERT(LIST_EMPTY(&lock->lf_inedges),
363 ("freeing lock with dependants"));
364 sx_xlock(&lf_lock_owners_lock);
365 KASSERT(lo->lo_refs > 0, ("lock owner refcount"));
367 if (lo->lo_refs == 0) {
370 printf("lf_free_lock: freeing lock owner %p\n",
374 sx_xlock(&lf_owner_graph_lock);
375 graph_free_vertex(&lf_owner_graph,
377 sx_xunlock(&lf_owner_graph_lock);
379 LIST_REMOVE(lo, lo_link);
383 printf("Freed lock owner %p\n", lo);
386 sx_unlock(&lf_lock_owners_lock);
388 if ((lock->lf_flags & F_REMOTE) && lock->lf_vnode) {
389 vrele(lock->lf_vnode);
390 lock->lf_vnode = NULL;
394 printf("Freed lock %p\n", lock);
400 * Advisory record locking support
403 lf_advlockasync(struct vop_advlockasync_args *ap, struct lockf **statep,
406 struct lockf *state, *freestate = NULL;
407 struct flock *fl = ap->a_fl;
408 struct lockf_entry *lock;
409 struct vnode *vp = ap->a_vp;
410 caddr_t id = ap->a_id;
411 int flags = ap->a_flags;
413 struct lock_owner *lo;
414 off_t start, end, oadd;
418 * Handle the F_UNLKSYS case first - no need to mess about
419 * creating a lock owner for this one.
421 if (ap->a_op == F_UNLCKSYS) {
422 lf_clearremotesys(fl->l_sysid);
427 * Convert the flock structure into a start and end.
429 switch (fl->l_whence) {
434 * Caller is responsible for adding any necessary offset
435 * when SEEK_CUR is used.
441 if (size > OFF_MAX ||
442 (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
444 start = size + fl->l_start;
459 } else if (fl->l_len == 0) {
462 oadd = fl->l_len - 1;
463 if (oadd > OFF_MAX - start)
468 * Avoid the common case of unlocking when inode has no locks.
471 if ((*statep) == NULL) {
472 if (ap->a_op != F_SETLK) {
473 fl->l_type = F_UNLCK;
481 * Map our arguments to an existing lock owner or create one
482 * if this is the first time we have seen this owner.
484 hash = lf_hash_owner(id, fl, flags);
485 sx_xlock(&lf_lock_owners_lock);
486 LIST_FOREACH(lo, &lf_lock_owners[hash], lo_link)
487 if (lf_owner_matches(lo, id, fl, flags))
491 * We initialise the lock with a reference
492 * count which matches the new lockf_entry
493 * structure created below.
495 lo = malloc(sizeof(struct lock_owner), M_LOCKF,
499 printf("Allocated lock owner %p\n", lo);
503 lo->lo_flags = flags;
505 if (flags & F_REMOTE) {
506 lo->lo_pid = fl->l_pid;
507 lo->lo_sysid = fl->l_sysid;
508 } else if (flags & F_FLOCK) {
512 struct proc *p = (struct proc *) id;
513 lo->lo_pid = p->p_pid;
516 lo->lo_vertex = NULL;
519 if (lockf_debug & 1) {
520 printf("lf_advlockasync: new lock owner %p ", lo);
526 LIST_INSERT_HEAD(&lf_lock_owners[hash], lo, lo_link);
529 * We have seen this lock owner before, increase its
530 * reference count to account for the new lockf_entry
531 * structure we create below.
535 sx_xunlock(&lf_lock_owners_lock);
538 * Create the lockf structure. We initialise the lf_owner
539 * field here instead of in lf_alloc_lock() to avoid paying
540 * the lf_lock_owners_lock tax twice.
542 lock = lf_alloc_lock(NULL);
543 lock->lf_start = start;
547 if (flags & F_REMOTE) {
549 * For remote locks, the caller may release its ref to
550 * the vnode at any time - we have to ref it here to
551 * prevent it from being recycled unexpectedly.
557 * XXX The problem is that VTOI is ufs specific, so it will
558 * break LOCKF_DEBUG for all other FS's other than UFS because
559 * it casts the vnode->data ptr to struct inode *.
561 /* lock->lf_inode = VTOI(ap->a_vp); */
562 lock->lf_inode = (struct inode *)0;
563 lock->lf_type = fl->l_type;
564 LIST_INIT(&lock->lf_outedges);
565 LIST_INIT(&lock->lf_inedges);
566 lock->lf_async_task = ap->a_task;
567 lock->lf_flags = ap->a_flags;
570 * Do the requested operation. First find our state structure
571 * and create a new one if necessary - the caller's *statep
572 * variable and the state's ls_threads count is protected by
573 * the vnode interlock.
576 if (vp->v_iflag & VI_DOOMED) {
583 * Allocate a state structure if necessary.
591 ls = malloc(sizeof(struct lockf), M_LOCKF, M_WAITOK|M_ZERO);
592 sx_init(&ls->ls_lock, "ls_lock");
593 LIST_INIT(&ls->ls_active);
594 LIST_INIT(&ls->ls_pending);
597 sx_xlock(&lf_lock_states_lock);
598 LIST_INSERT_HEAD(&lf_lock_states, ls, ls_link);
599 sx_xunlock(&lf_lock_states_lock);
602 * Cope if we lost a race with some other thread while
603 * trying to allocate memory.
606 if (vp->v_iflag & VI_DOOMED) {
608 sx_xlock(&lf_lock_states_lock);
609 LIST_REMOVE(ls, ls_link);
610 sx_xunlock(&lf_lock_states_lock);
611 sx_destroy(&ls->ls_lock);
616 if ((*statep) == NULL) {
617 state = *statep = ls;
624 sx_xlock(&lf_lock_states_lock);
625 LIST_REMOVE(ls, ls_link);
626 sx_xunlock(&lf_lock_states_lock);
627 sx_destroy(&ls->ls_lock);
635 sx_xlock(&state->ls_lock);
638 error = lf_setlock(state, lock, vp, ap->a_cookiep);
642 error = lf_clearlock(state, lock);
647 error = lf_getlock(state, lock, fl);
653 error = lf_cancel(state, lock, *ap->a_cookiep);
667 * Check for some can't happen stuff. In this case, the active
668 * lock list becoming disordered or containing mutually
669 * blocking locks. We also check the pending list for locks
670 * which should be active (i.e. have no out-going edges).
672 LIST_FOREACH(lock, &state->ls_active, lf_link) {
673 struct lockf_entry *lf;
674 if (LIST_NEXT(lock, lf_link))
675 KASSERT((lock->lf_start
676 <= LIST_NEXT(lock, lf_link)->lf_start),
677 ("locks disordered"));
678 LIST_FOREACH(lf, &state->ls_active, lf_link) {
681 KASSERT(!lf_blocks(lock, lf),
682 ("two conflicting active locks"));
683 if (lock->lf_owner == lf->lf_owner)
684 KASSERT(!lf_overlaps(lock, lf),
685 ("two overlapping locks from same owner"));
688 LIST_FOREACH(lock, &state->ls_pending, lf_link) {
689 KASSERT(!LIST_EMPTY(&lock->lf_outedges),
690 ("pending lock which should be active"));
693 sx_xunlock(&state->ls_lock);
696 * If we have removed the last active lock on the vnode and
697 * this is the last thread that was in-progress, we can free
698 * the state structure. We update the caller's pointer inside
699 * the vnode interlock but call free outside.
701 * XXX alternatively, keep the state structure around until
702 * the filesystem recycles - requires a callback from the
709 if (LIST_EMPTY(&state->ls_active) && state->ls_threads == 0) {
710 KASSERT(LIST_EMPTY(&state->ls_pending),
711 ("freeing state with pending locks"));
719 sx_xlock(&lf_lock_states_lock);
720 LIST_REMOVE(freestate, ls_link);
721 sx_xunlock(&lf_lock_states_lock);
722 sx_destroy(&freestate->ls_lock);
723 free(freestate, M_LOCKF);
729 lf_advlock(struct vop_advlock_args *ap, struct lockf **statep, u_quad_t size)
731 struct vop_advlockasync_args a;
737 a.a_flags = ap->a_flags;
741 return (lf_advlockasync(&a, statep, size));
745 lf_purgelocks(struct vnode *vp, struct lockf **statep)
748 struct lockf_entry *lock, *nlock;
751 * For this to work correctly, the caller must ensure that no
752 * other threads enter the locking system for this vnode,
753 * e.g. by checking VI_DOOMED. We wake up any threads that are
754 * sleeping waiting for locks on this vnode and then free all
755 * the remaining locks.
763 sx_xlock(&state->ls_lock);
764 sx_xlock(&lf_owner_graph_lock);
765 LIST_FOREACH_SAFE(lock, &state->ls_pending, lf_link, nlock) {
766 LIST_REMOVE(lock, lf_link);
767 lf_remove_outgoing(lock);
768 lf_remove_incoming(lock);
771 * If its an async lock, we can just free it
772 * here, otherwise we let the sleeping thread
775 if (lock->lf_async_task) {
778 lock->lf_flags |= F_INTR;
782 sx_xunlock(&lf_owner_graph_lock);
783 sx_xunlock(&state->ls_lock);
786 * Wait for all other threads, sleeping and otherwise
790 while (state->ls_threads > 1)
791 msleep(state, VI_MTX(vp), 0, "purgelocks", 0);
796 * We can just free all the active locks since they
797 * will have no dependancies (we removed them all
798 * above). We don't need to bother locking since we
799 * are the last thread using this state structure.
801 LIST_FOREACH_SAFE(lock, &state->ls_pending, lf_link, nlock) {
802 LIST_REMOVE(lock, lf_link);
805 sx_xlock(&lf_lock_states_lock);
806 LIST_REMOVE(state, ls_link);
807 sx_xunlock(&lf_lock_states_lock);
808 sx_destroy(&state->ls_lock);
809 free(state, M_LOCKF);
816 * Return non-zero if locks 'x' and 'y' overlap.
819 lf_overlaps(struct lockf_entry *x, struct lockf_entry *y)
822 return (x->lf_start <= y->lf_end && x->lf_end >= y->lf_start);
826 * Return non-zero if lock 'x' is blocked by lock 'y' (or vice versa).
829 lf_blocks(struct lockf_entry *x, struct lockf_entry *y)
832 return x->lf_owner != y->lf_owner
833 && (x->lf_type == F_WRLCK || y->lf_type == F_WRLCK)
834 && lf_overlaps(x, y);
838 * Allocate a lock edge from the free list
840 static struct lockf_edge *
844 return (malloc(sizeof(struct lockf_edge), M_LOCKF, M_WAITOK|M_ZERO));
851 lf_free_edge(struct lockf_edge *e)
859 * Ensure that the lock's owner has a corresponding vertex in the
863 lf_alloc_vertex(struct lockf_entry *lock)
865 struct owner_graph *g = &lf_owner_graph;
867 if (!lock->lf_owner->lo_vertex)
868 lock->lf_owner->lo_vertex =
869 graph_alloc_vertex(g, lock->lf_owner);
873 * Attempt to record an edge from lock x to lock y. Return EDEADLK if
874 * the new edge would cause a cycle in the owner graph.
877 lf_add_edge(struct lockf_entry *x, struct lockf_entry *y)
879 struct owner_graph *g = &lf_owner_graph;
880 struct lockf_edge *e;
884 LIST_FOREACH(e, &x->lf_outedges, le_outlink)
885 KASSERT(e->le_to != y, ("adding lock edge twice"));
889 * Make sure the two owners have entries in the owner graph.
894 error = graph_add_edge(g, x->lf_owner->lo_vertex,
895 y->lf_owner->lo_vertex);
900 LIST_INSERT_HEAD(&x->lf_outedges, e, le_outlink);
901 LIST_INSERT_HEAD(&y->lf_inedges, e, le_inlink);
909 * Remove an edge from the lock graph.
912 lf_remove_edge(struct lockf_edge *e)
914 struct owner_graph *g = &lf_owner_graph;
915 struct lockf_entry *x = e->le_from;
916 struct lockf_entry *y = e->le_to;
918 graph_remove_edge(g, x->lf_owner->lo_vertex, y->lf_owner->lo_vertex);
919 LIST_REMOVE(e, le_outlink);
920 LIST_REMOVE(e, le_inlink);
927 * Remove all out-going edges from lock x.
930 lf_remove_outgoing(struct lockf_entry *x)
932 struct lockf_edge *e;
934 while ((e = LIST_FIRST(&x->lf_outedges)) != NULL) {
940 * Remove all in-coming edges from lock x.
943 lf_remove_incoming(struct lockf_entry *x)
945 struct lockf_edge *e;
947 while ((e = LIST_FIRST(&x->lf_inedges)) != NULL) {
953 * Walk the list of locks for the file and create an out-going edge
954 * from lock to each blocking lock.
957 lf_add_outgoing(struct lockf *state, struct lockf_entry *lock)
959 struct lockf_entry *overlap;
962 LIST_FOREACH(overlap, &state->ls_active, lf_link) {
964 * We may assume that the active list is sorted by
967 if (overlap->lf_start > lock->lf_end)
969 if (!lf_blocks(lock, overlap))
973 * We've found a blocking lock. Add the corresponding
974 * edge to the graphs and see if it would cause a
977 error = lf_add_edge(lock, overlap);
980 * The only error that lf_add_edge returns is EDEADLK.
981 * Remove any edges we added and return the error.
984 lf_remove_outgoing(lock);
990 * We also need to add edges to sleeping locks that block
991 * us. This ensures that lf_wakeup_lock cannot grant two
992 * mutually blocking locks simultaneously and also enforces a
993 * 'first come, first served' fairness model. Note that this
994 * only happens if we are blocked by at least one active lock
995 * due to the call to lf_getblock in lf_setlock below.
997 LIST_FOREACH(overlap, &state->ls_pending, lf_link) {
998 if (!lf_blocks(lock, overlap))
1001 * We've found a blocking lock. Add the corresponding
1002 * edge to the graphs and see if it would cause a
1005 error = lf_add_edge(lock, overlap);
1008 * The only error that lf_add_edge returns is EDEADLK.
1009 * Remove any edges we added and return the error.
1012 lf_remove_outgoing(lock);
1021 * Walk the list of pending locks for the file and create an in-coming
1022 * edge from lock to each blocking lock.
1025 lf_add_incoming(struct lockf *state, struct lockf_entry *lock)
1027 struct lockf_entry *overlap;
1030 LIST_FOREACH(overlap, &state->ls_pending, lf_link) {
1031 if (!lf_blocks(lock, overlap))
1035 * We've found a blocking lock. Add the corresponding
1036 * edge to the graphs and see if it would cause a
1039 error = lf_add_edge(overlap, lock);
1042 * The only error that lf_add_edge returns is EDEADLK.
1043 * Remove any edges we added and return the error.
1046 lf_remove_incoming(lock);
1054 * Insert lock into the active list, keeping list entries ordered by
1055 * increasing values of lf_start.
1058 lf_insert_lock(struct lockf *state, struct lockf_entry *lock)
1060 struct lockf_entry *lf, *lfprev;
1062 if (LIST_EMPTY(&state->ls_active)) {
1063 LIST_INSERT_HEAD(&state->ls_active, lock, lf_link);
1068 LIST_FOREACH(lf, &state->ls_active, lf_link) {
1069 if (lf->lf_start > lock->lf_start) {
1070 LIST_INSERT_BEFORE(lf, lock, lf_link);
1075 LIST_INSERT_AFTER(lfprev, lock, lf_link);
1079 * Wake up a sleeping lock and remove it from the pending list now
1080 * that all its dependancies have been resolved. The caller should
1081 * arrange for the lock to be added to the active list, adjusting any
1082 * existing locks for the same owner as needed.
1085 lf_wakeup_lock(struct lockf *state, struct lockf_entry *wakelock)
1089 * Remove from ls_pending list and wake up the caller
1090 * or start the async notification, as appropriate.
1092 LIST_REMOVE(wakelock, lf_link);
1094 if (lockf_debug & 1)
1095 lf_print("lf_wakeup_lock: awakening", wakelock);
1096 #endif /* LOCKF_DEBUG */
1097 if (wakelock->lf_async_task) {
1098 taskqueue_enqueue(taskqueue_thread, wakelock->lf_async_task);
1105 * Re-check all dependant locks and remove edges to locks that we no
1106 * longer block. If 'all' is non-zero, the lock has been removed and
1107 * we must remove all the dependancies, otherwise it has simply been
1108 * reduced but remains active. Any pending locks which have been been
1109 * unblocked are added to 'granted'
1112 lf_update_dependancies(struct lockf *state, struct lockf_entry *lock, int all,
1113 struct lockf_entry_list *granted)
1115 struct lockf_edge *e, *ne;
1116 struct lockf_entry *deplock;
1118 LIST_FOREACH_SAFE(e, &lock->lf_inedges, le_inlink, ne) {
1119 deplock = e->le_from;
1120 if (all || !lf_blocks(lock, deplock)) {
1121 sx_xlock(&lf_owner_graph_lock);
1123 sx_xunlock(&lf_owner_graph_lock);
1124 if (LIST_EMPTY(&deplock->lf_outedges)) {
1125 lf_wakeup_lock(state, deplock);
1126 LIST_INSERT_HEAD(granted, deplock, lf_link);
1133 * Set the start of an existing active lock, updating dependancies and
1134 * adding any newly woken locks to 'granted'.
1137 lf_set_start(struct lockf *state, struct lockf_entry *lock, off_t new_start,
1138 struct lockf_entry_list *granted)
1141 KASSERT(new_start >= lock->lf_start, ("can't increase lock"));
1142 lock->lf_start = new_start;
1143 LIST_REMOVE(lock, lf_link);
1144 lf_insert_lock(state, lock);
1145 lf_update_dependancies(state, lock, FALSE, granted);
1149 * Set the end of an existing active lock, updating dependancies and
1150 * adding any newly woken locks to 'granted'.
1153 lf_set_end(struct lockf *state, struct lockf_entry *lock, off_t new_end,
1154 struct lockf_entry_list *granted)
1157 KASSERT(new_end <= lock->lf_end, ("can't increase lock"));
1158 lock->lf_end = new_end;
1159 lf_update_dependancies(state, lock, FALSE, granted);
1163 * Add a lock to the active list, updating or removing any current
1164 * locks owned by the same owner and processing any pending locks that
1165 * become unblocked as a result. This code is also used for unlock
1166 * since the logic for updating existing locks is identical.
1168 * As a result of processing the new lock, we may unblock existing
1169 * pending locks as a result of downgrading/unlocking. We simply
1170 * activate the newly granted locks by looping.
1172 * Since the new lock already has its dependancies set up, we always
1173 * add it to the list (unless its an unlock request). This may
1174 * fragment the lock list in some pathological cases but its probably
1175 * not a real problem.
1178 lf_activate_lock(struct lockf *state, struct lockf_entry *lock)
1180 struct lockf_entry *overlap, *lf;
1181 struct lockf_entry_list granted;
1184 LIST_INIT(&granted);
1185 LIST_INSERT_HEAD(&granted, lock, lf_link);
1187 while (!LIST_EMPTY(&granted)) {
1188 lock = LIST_FIRST(&granted);
1189 LIST_REMOVE(lock, lf_link);
1192 * Skip over locks owned by other processes. Handle
1193 * any locks that overlap and are owned by ourselves.
1195 overlap = LIST_FIRST(&state->ls_active);
1197 ovcase = lf_findoverlap(&overlap, lock, SELF);
1200 if (ovcase && (lockf_debug & 2)) {
1201 printf("lf_setlock: overlap %d", ovcase);
1202 lf_print("", overlap);
1208 * 1) overlap == lock
1209 * 2) overlap contains lock
1210 * 3) lock contains overlap
1211 * 4) overlap starts before lock
1212 * 5) overlap ends after lock
1215 case 0: /* no overlap */
1218 case 1: /* overlap == lock */
1220 * We have already setup the
1221 * dependants for the new lock, taking
1222 * into account a possible downgrade
1223 * or unlock. Remove the old lock.
1225 LIST_REMOVE(overlap, lf_link);
1226 lf_update_dependancies(state, overlap, TRUE,
1228 lf_free_lock(overlap);
1231 case 2: /* overlap contains lock */
1233 * Just split the existing lock.
1235 lf_split(state, overlap, lock, &granted);
1238 case 3: /* lock contains overlap */
1240 * Delete the overlap and advance to
1241 * the next entry in the list.
1243 lf = LIST_NEXT(overlap, lf_link);
1244 LIST_REMOVE(overlap, lf_link);
1245 lf_update_dependancies(state, overlap, TRUE,
1247 lf_free_lock(overlap);
1251 case 4: /* overlap starts before lock */
1253 * Just update the overlap end and
1256 lf_set_end(state, overlap, lock->lf_start - 1,
1258 overlap = LIST_NEXT(overlap, lf_link);
1261 case 5: /* overlap ends after lock */
1263 * Change the start of overlap and
1266 lf_set_start(state, overlap, lock->lf_end + 1,
1273 if (lockf_debug & 1) {
1274 if (lock->lf_type != F_UNLCK)
1275 lf_print("lf_activate_lock: activated", lock);
1277 lf_print("lf_activate_lock: unlocked", lock);
1278 lf_printlist("lf_activate_lock", lock);
1280 #endif /* LOCKF_DEBUG */
1281 if (lock->lf_type != F_UNLCK)
1282 lf_insert_lock(state, lock);
1287 * Cancel a pending lock request, either as a result of a signal or a
1288 * cancel request for an async lock.
1291 lf_cancel_lock(struct lockf *state, struct lockf_entry *lock)
1293 struct lockf_entry_list granted;
1296 * Note it is theoretically possible that cancelling this lock
1297 * may allow some other pending lock to become
1298 * active. Consider this case:
1300 * Owner Action Result Dependancies
1302 * A: lock [0..0] succeeds
1303 * B: lock [2..2] succeeds
1304 * C: lock [1..2] blocked C->B
1305 * D: lock [0..1] blocked C->B,D->A,D->C
1306 * A: unlock [0..0] C->B,D->C
1310 LIST_REMOVE(lock, lf_link);
1313 * Removing out-going edges is simple.
1315 sx_xlock(&lf_owner_graph_lock);
1316 lf_remove_outgoing(lock);
1317 sx_xunlock(&lf_owner_graph_lock);
1320 * Removing in-coming edges may allow some other lock to
1321 * become active - we use lf_update_dependancies to figure
1324 LIST_INIT(&granted);
1325 lf_update_dependancies(state, lock, TRUE, &granted);
1329 * Feed any newly active locks to lf_activate_lock.
1331 while (!LIST_EMPTY(&granted)) {
1332 lock = LIST_FIRST(&granted);
1333 LIST_REMOVE(lock, lf_link);
1334 lf_activate_lock(state, lock);
1339 * Set a byte-range lock.
1342 lf_setlock(struct lockf *state, struct lockf_entry *lock, struct vnode *vp,
1345 struct lockf_entry *block;
1346 static char lockstr[] = "lockf";
1347 int priority, error;
1350 if (lockf_debug & 1)
1351 lf_print("lf_setlock", lock);
1352 #endif /* LOCKF_DEBUG */
1358 if (lock->lf_type == F_WRLCK)
1360 if (!(lock->lf_flags & F_NOINTR))
1363 * Scan lock list for this file looking for locks that would block us.
1365 while ((block = lf_getblock(state, lock))) {
1367 * Free the structure and return if nonblocking.
1369 if ((lock->lf_flags & F_WAIT) == 0
1370 && lock->lf_async_task == NULL) {
1377 * For flock type locks, we must first remove
1378 * any shared locks that we hold before we sleep
1379 * waiting for an exclusive lock.
1381 if ((lock->lf_flags & F_FLOCK) &&
1382 lock->lf_type == F_WRLCK) {
1383 lock->lf_type = F_UNLCK;
1384 lf_activate_lock(state, lock);
1385 lock->lf_type = F_WRLCK;
1389 * We are blocked. Create edges to each blocking lock,
1390 * checking for deadlock using the owner graph. For
1391 * simplicity, we run deadlock detection for all
1392 * locks, posix and otherwise.
1394 sx_xlock(&lf_owner_graph_lock);
1395 error = lf_add_outgoing(state, lock);
1396 sx_xunlock(&lf_owner_graph_lock);
1400 if (lockf_debug & 1)
1401 lf_print("lf_setlock: deadlock", lock);
1408 * We have added edges to everything that blocks
1409 * us. Sleep until they all go away.
1411 LIST_INSERT_HEAD(&state->ls_pending, lock, lf_link);
1413 if (lockf_debug & 1) {
1414 struct lockf_edge *e;
1415 LIST_FOREACH(e, &lock->lf_outedges, le_outlink) {
1416 lf_print("lf_setlock: blocking on", e->le_to);
1417 lf_printlist("lf_setlock", e->le_to);
1420 #endif /* LOCKF_DEBUG */
1422 if ((lock->lf_flags & F_WAIT) == 0) {
1424 * The caller requested async notification -
1425 * this callback happens when the blocking
1426 * lock is released, allowing the caller to
1427 * make another attempt to take the lock.
1429 *cookiep = (void *) lock;
1430 error = EINPROGRESS;
1434 error = sx_sleep(lock, &state->ls_lock, priority, lockstr, 0);
1436 * We may have been awakened by a signal and/or by a
1437 * debugger continuing us (in which cases we must
1438 * remove our lock graph edges) and/or by another
1439 * process releasing a lock (in which case our edges
1440 * have already been removed and we have been moved to
1441 * the active list). We may also have been woken by
1442 * lf_purgelocks which we report to the caller as
1443 * EINTR. In that case, lf_purgelocks will have
1444 * removed our lock graph edges.
1446 * Note that it is possible to receive a signal after
1447 * we were successfully woken (and moved to the active
1448 * list) but before we resumed execution. In this
1449 * case, our lf_outedges list will be clear. We
1450 * pretend there was no error.
1452 * Note also, if we have been sleeping long enough, we
1453 * may now have incoming edges from some newer lock
1454 * which is waiting behind us in the queue.
1456 if (lock->lf_flags & F_INTR) {
1461 if (LIST_EMPTY(&lock->lf_outedges)) {
1464 lf_cancel_lock(state, lock);
1468 if (lockf_debug & 1) {
1469 lf_print("lf_setlock: granted", lock);
1475 * It looks like we are going to grant the lock. First add
1476 * edges from any currently pending lock that the new lock
1479 sx_xlock(&lf_owner_graph_lock);
1480 error = lf_add_incoming(state, lock);
1481 sx_xunlock(&lf_owner_graph_lock);
1484 if (lockf_debug & 1)
1485 lf_print("lf_setlock: deadlock", lock);
1492 * No blocks!! Add the lock. Note that we will
1493 * downgrade or upgrade any overlapping locks this
1494 * process already owns.
1496 lf_activate_lock(state, lock);
1503 * Remove a byte-range lock on an inode.
1505 * Generally, find the lock (or an overlap to that lock)
1506 * and remove it (or shrink it), then wakeup anyone we can.
1509 lf_clearlock(struct lockf *state, struct lockf_entry *unlock)
1511 struct lockf_entry *overlap;
1513 overlap = LIST_FIRST(&state->ls_active);
1515 if (overlap == NOLOCKF)
1518 if (unlock->lf_type != F_UNLCK)
1519 panic("lf_clearlock: bad type");
1520 if (lockf_debug & 1)
1521 lf_print("lf_clearlock", unlock);
1522 #endif /* LOCKF_DEBUG */
1524 lf_activate_lock(state, unlock);
1530 * Check whether there is a blocking lock, and if so return its
1534 lf_getlock(struct lockf *state, struct lockf_entry *lock, struct flock *fl)
1536 struct lockf_entry *block;
1539 if (lockf_debug & 1)
1540 lf_print("lf_getlock", lock);
1541 #endif /* LOCKF_DEBUG */
1543 if ((block = lf_getblock(state, lock))) {
1544 fl->l_type = block->lf_type;
1545 fl->l_whence = SEEK_SET;
1546 fl->l_start = block->lf_start;
1547 if (block->lf_end == OFF_MAX)
1550 fl->l_len = block->lf_end - block->lf_start + 1;
1551 fl->l_pid = block->lf_owner->lo_pid;
1552 fl->l_sysid = block->lf_owner->lo_sysid;
1554 fl->l_type = F_UNLCK;
1560 * Cancel an async lock request.
1563 lf_cancel(struct lockf *state, struct lockf_entry *lock, void *cookie)
1565 struct lockf_entry *reallock;
1568 * We need to match this request with an existing lock
1571 LIST_FOREACH(reallock, &state->ls_pending, lf_link) {
1572 if ((void *) reallock == cookie) {
1574 * Double-check that this lock looks right
1575 * (maybe use a rolling ID for the cancel
1578 if (!(reallock->lf_vnode == lock->lf_vnode
1579 && reallock->lf_start == lock->lf_start
1580 && reallock->lf_end == lock->lf_end)) {
1585 * Make sure this lock was async and then just
1586 * remove it from its wait lists.
1588 if (!reallock->lf_async_task) {
1593 * Note that since any other thread must take
1594 * state->ls_lock before it can possibly
1595 * trigger the async callback, we are safe
1596 * from a race with lf_wakeup_lock, i.e. we
1597 * can free the lock (actually our caller does
1600 lf_cancel_lock(state, reallock);
1606 * We didn't find a matching lock - not much we can do here.
1612 * Walk the list of locks for an inode and
1613 * return the first blocking lock.
1615 static struct lockf_entry *
1616 lf_getblock(struct lockf *state, struct lockf_entry *lock)
1618 struct lockf_entry *overlap;
1620 LIST_FOREACH(overlap, &state->ls_active, lf_link) {
1622 * We may assume that the active list is sorted by
1625 if (overlap->lf_start > lock->lf_end)
1627 if (!lf_blocks(lock, overlap))
1635 * Walk the list of locks for an inode to find an overlapping lock (if
1636 * any) and return a classification of that overlap.
1639 * *overlap The place in the lock list to start looking
1640 * lock The lock which is being tested
1641 * type Pass 'SELF' to test only locks with the same
1642 * owner as lock, or 'OTHER' to test only locks
1643 * with a different owner
1645 * Returns one of six values:
1647 * 1) overlap == lock
1648 * 2) overlap contains lock
1649 * 3) lock contains overlap
1650 * 4) overlap starts before lock
1651 * 5) overlap ends after lock
1653 * If there is an overlapping lock, '*overlap' is set to point at the
1656 * NOTE: this returns only the FIRST overlapping lock. There
1657 * may be more than one.
1660 lf_findoverlap(struct lockf_entry **overlap, struct lockf_entry *lock, int type)
1662 struct lockf_entry *lf;
1666 if ((*overlap) == NOLOCKF) {
1670 if (lockf_debug & 2)
1671 lf_print("lf_findoverlap: looking for overlap in", lock);
1672 #endif /* LOCKF_DEBUG */
1673 start = lock->lf_start;
1678 if (lf->lf_start > end)
1680 if (((type & SELF) && lf->lf_owner != lock->lf_owner) ||
1681 ((type & OTHERS) && lf->lf_owner == lock->lf_owner)) {
1682 *overlap = LIST_NEXT(lf, lf_link);
1686 if (lockf_debug & 2)
1687 lf_print("\tchecking", lf);
1688 #endif /* LOCKF_DEBUG */
1690 * OK, check for overlap
1694 * 1) overlap == lock
1695 * 2) overlap contains lock
1696 * 3) lock contains overlap
1697 * 4) overlap starts before lock
1698 * 5) overlap ends after lock
1700 if (start > lf->lf_end) {
1703 if (lockf_debug & 2)
1704 printf("no overlap\n");
1705 #endif /* LOCKF_DEBUG */
1706 *overlap = LIST_NEXT(lf, lf_link);
1709 if (lf->lf_start == start && lf->lf_end == end) {
1712 if (lockf_debug & 2)
1713 printf("overlap == lock\n");
1714 #endif /* LOCKF_DEBUG */
1718 if (lf->lf_start <= start && lf->lf_end >= end) {
1721 if (lockf_debug & 2)
1722 printf("overlap contains lock\n");
1723 #endif /* LOCKF_DEBUG */
1727 if (start <= lf->lf_start && end >= lf->lf_end) {
1730 if (lockf_debug & 2)
1731 printf("lock contains overlap\n");
1732 #endif /* LOCKF_DEBUG */
1736 if (lf->lf_start < start && lf->lf_end >= start) {
1739 if (lockf_debug & 2)
1740 printf("overlap starts before lock\n");
1741 #endif /* LOCKF_DEBUG */
1745 if (lf->lf_start > start && lf->lf_end > end) {
1748 if (lockf_debug & 2)
1749 printf("overlap ends after lock\n");
1750 #endif /* LOCKF_DEBUG */
1754 panic("lf_findoverlap: default");
1760 * Split an the existing 'lock1', based on the extent of the lock
1761 * described by 'lock2'. The existing lock should cover 'lock2'
1764 * Any pending locks which have been been unblocked are added to
1768 lf_split(struct lockf *state, struct lockf_entry *lock1,
1769 struct lockf_entry *lock2, struct lockf_entry_list *granted)
1771 struct lockf_entry *splitlock;
1774 if (lockf_debug & 2) {
1775 lf_print("lf_split", lock1);
1776 lf_print("splitting from", lock2);
1778 #endif /* LOCKF_DEBUG */
1780 * Check to see if we don't need to split at all.
1782 if (lock1->lf_start == lock2->lf_start) {
1783 lf_set_start(state, lock1, lock2->lf_end + 1, granted);
1786 if (lock1->lf_end == lock2->lf_end) {
1787 lf_set_end(state, lock1, lock2->lf_start - 1, granted);
1791 * Make a new lock consisting of the last part of
1792 * the encompassing lock.
1794 splitlock = lf_alloc_lock(lock1->lf_owner);
1795 memcpy(splitlock, lock1, sizeof *splitlock);
1796 if (splitlock->lf_flags & F_REMOTE)
1797 vref(splitlock->lf_vnode);
1800 * This cannot cause a deadlock since any edges we would add
1801 * to splitlock already exist in lock1. We must be sure to add
1802 * necessary dependancies to splitlock before we reduce lock1
1803 * otherwise we may accidentally grant a pending lock that
1804 * was blocked by the tail end of lock1.
1806 splitlock->lf_start = lock2->lf_end + 1;
1807 LIST_INIT(&splitlock->lf_outedges);
1808 LIST_INIT(&splitlock->lf_inedges);
1809 sx_xlock(&lf_owner_graph_lock);
1810 lf_add_incoming(state, splitlock);
1811 sx_xunlock(&lf_owner_graph_lock);
1813 lf_set_end(state, lock1, lock2->lf_start - 1, granted);
1816 * OK, now link it in
1818 lf_insert_lock(state, splitlock);
1822 STAILQ_ENTRY(lockdesc) link;
1826 STAILQ_HEAD(lockdesclist, lockdesc);
1829 lf_iteratelocks_sysid(int sysid, lf_iterator *fn, void *arg)
1832 struct lockf_entry *lf;
1833 struct lockdesc *ldesc;
1834 struct lockdesclist locks;
1838 * In order to keep the locking simple, we iterate over the
1839 * active lock lists to build a list of locks that need
1840 * releasing. We then call the iterator for each one in turn.
1842 * We take an extra reference to the vnode for the duration to
1843 * make sure it doesn't go away before we are finished.
1845 STAILQ_INIT(&locks);
1846 sx_xlock(&lf_lock_states_lock);
1847 LIST_FOREACH(ls, &lf_lock_states, ls_link) {
1848 sx_xlock(&ls->ls_lock);
1849 LIST_FOREACH(lf, &ls->ls_active, lf_link) {
1850 if (lf->lf_owner->lo_sysid != sysid)
1853 ldesc = malloc(sizeof(struct lockdesc), M_LOCKF,
1855 ldesc->vp = lf->lf_vnode;
1857 ldesc->fl.l_start = lf->lf_start;
1858 if (lf->lf_end == OFF_MAX)
1859 ldesc->fl.l_len = 0;
1862 lf->lf_end - lf->lf_start + 1;
1863 ldesc->fl.l_whence = SEEK_SET;
1864 ldesc->fl.l_type = F_UNLCK;
1865 ldesc->fl.l_pid = lf->lf_owner->lo_pid;
1866 ldesc->fl.l_sysid = sysid;
1867 STAILQ_INSERT_TAIL(&locks, ldesc, link);
1869 sx_xunlock(&ls->ls_lock);
1871 sx_xunlock(&lf_lock_states_lock);
1874 * Call the iterator function for each lock in turn. If the
1875 * iterator returns an error code, just free the rest of the
1876 * lockdesc structures.
1879 while ((ldesc = STAILQ_FIRST(&locks)) != NULL) {
1880 STAILQ_REMOVE_HEAD(&locks, link);
1882 error = fn(ldesc->vp, &ldesc->fl, arg);
1884 free(ldesc, M_LOCKF);
1891 lf_iteratelocks_vnode(struct vnode *vp, lf_iterator *fn, void *arg)
1894 struct lockf_entry *lf;
1895 struct lockdesc *ldesc;
1896 struct lockdesclist locks;
1900 * In order to keep the locking simple, we iterate over the
1901 * active lock lists to build a list of locks that need
1902 * releasing. We then call the iterator for each one in turn.
1904 * We take an extra reference to the vnode for the duration to
1905 * make sure it doesn't go away before we are finished.
1907 STAILQ_INIT(&locks);
1912 sx_xlock(&ls->ls_lock);
1913 LIST_FOREACH(lf, &ls->ls_active, lf_link) {
1914 ldesc = malloc(sizeof(struct lockdesc), M_LOCKF,
1916 ldesc->vp = lf->lf_vnode;
1918 ldesc->fl.l_start = lf->lf_start;
1919 if (lf->lf_end == OFF_MAX)
1920 ldesc->fl.l_len = 0;
1923 lf->lf_end - lf->lf_start + 1;
1924 ldesc->fl.l_whence = SEEK_SET;
1925 ldesc->fl.l_type = F_UNLCK;
1926 ldesc->fl.l_pid = lf->lf_owner->lo_pid;
1927 ldesc->fl.l_sysid = lf->lf_owner->lo_sysid;
1928 STAILQ_INSERT_TAIL(&locks, ldesc, link);
1930 sx_xunlock(&ls->ls_lock);
1933 * Call the iterator function for each lock in turn. If the
1934 * iterator returns an error code, just free the rest of the
1935 * lockdesc structures.
1938 while ((ldesc = STAILQ_FIRST(&locks)) != NULL) {
1939 STAILQ_REMOVE_HEAD(&locks, link);
1941 error = fn(ldesc->vp, &ldesc->fl, arg);
1943 free(ldesc, M_LOCKF);
1950 lf_clearremotesys_iterator(struct vnode *vp, struct flock *fl, void *arg)
1953 VOP_ADVLOCK(vp, 0, F_UNLCK, fl, F_REMOTE);
1958 lf_clearremotesys(int sysid)
1961 KASSERT(sysid != 0, ("Can't clear local locks with F_UNLCKSYS"));
1962 lf_iteratelocks_sysid(sysid, lf_clearremotesys_iterator, NULL);
1966 lf_countlocks(int sysid)
1969 struct lock_owner *lo;
1973 sx_xlock(&lf_lock_owners_lock);
1974 for (i = 0; i < LOCK_OWNER_HASH_SIZE; i++)
1975 LIST_FOREACH(lo, &lf_lock_owners[i], lo_link)
1976 if (lo->lo_sysid == sysid)
1977 count += lo->lo_refs;
1978 sx_xunlock(&lf_lock_owners_lock);
1986 * Return non-zero if y is reachable from x using a brute force
1987 * search. If reachable and path is non-null, return the route taken
1991 graph_reaches(struct owner_vertex *x, struct owner_vertex *y,
1992 struct owner_vertex_list *path)
1994 struct owner_edge *e;
1998 TAILQ_INSERT_HEAD(path, x, v_link);
2002 LIST_FOREACH(e, &x->v_outedges, e_outlink) {
2003 if (graph_reaches(e->e_to, y, path)) {
2005 TAILQ_INSERT_HEAD(path, x, v_link);
2013 * Perform consistency checks on the graph. Make sure the values of
2014 * v_order are correct. If checkorder is non-zero, check no vertex can
2015 * reach any other vertex with a smaller order.
2018 graph_check(struct owner_graph *g, int checkorder)
2022 for (i = 0; i < g->g_size; i++) {
2023 if (!g->g_vertices[i]->v_owner)
2025 KASSERT(g->g_vertices[i]->v_order == i,
2026 ("lock graph vertices disordered"));
2028 for (j = 0; j < i; j++) {
2029 if (!g->g_vertices[j]->v_owner)
2031 KASSERT(!graph_reaches(g->g_vertices[i],
2032 g->g_vertices[j], NULL),
2033 ("lock graph vertices disordered"));
2040 graph_print_vertices(struct owner_vertex_list *set)
2042 struct owner_vertex *v;
2045 TAILQ_FOREACH(v, set, v_link) {
2046 printf("%d:", v->v_order);
2047 lf_print_owner(v->v_owner);
2048 if (TAILQ_NEXT(v, v_link))
2057 * Calculate the sub-set of vertices v from the affected region [y..x]
2058 * where v is reachable from y. Return -1 if a loop was detected
2059 * (i.e. x is reachable from y, otherwise the number of vertices in
2063 graph_delta_forward(struct owner_graph *g, struct owner_vertex *x,
2064 struct owner_vertex *y, struct owner_vertex_list *delta)
2067 struct owner_vertex *v;
2068 struct owner_edge *e;
2072 * We start with a set containing just y. Then for each vertex
2073 * v in the set so far unprocessed, we add each vertex that v
2074 * has an out-edge to and that is within the affected region
2075 * [y..x]. If we see the vertex x on our travels, stop
2079 TAILQ_INSERT_TAIL(delta, y, v_link);
2084 LIST_FOREACH(e, &v->v_outedges, e_outlink) {
2087 if (e->e_to->v_order < x->v_order
2088 && e->e_to->v_gen != gen) {
2089 e->e_to->v_gen = gen;
2090 TAILQ_INSERT_TAIL(delta, e->e_to, v_link);
2094 v = TAILQ_NEXT(v, v_link);
2101 * Calculate the sub-set of vertices v from the affected region [y..x]
2102 * where v reaches x. Return the number of vertices in this subset.
2105 graph_delta_backward(struct owner_graph *g, struct owner_vertex *x,
2106 struct owner_vertex *y, struct owner_vertex_list *delta)
2109 struct owner_vertex *v;
2110 struct owner_edge *e;
2114 * We start with a set containing just x. Then for each vertex
2115 * v in the set so far unprocessed, we add each vertex that v
2116 * has an in-edge from and that is within the affected region
2120 TAILQ_INSERT_TAIL(delta, x, v_link);
2125 LIST_FOREACH(e, &v->v_inedges, e_inlink) {
2126 if (e->e_from->v_order > y->v_order
2127 && e->e_from->v_gen != gen) {
2128 e->e_from->v_gen = gen;
2129 TAILQ_INSERT_HEAD(delta, e->e_from, v_link);
2133 v = TAILQ_PREV(v, owner_vertex_list, v_link);
2140 graph_add_indices(int *indices, int n, struct owner_vertex_list *set)
2142 struct owner_vertex *v;
2145 TAILQ_FOREACH(v, set, v_link) {
2147 i > 0 && indices[i - 1] > v->v_order; i--)
2149 for (j = n - 1; j >= i; j--)
2150 indices[j + 1] = indices[j];
2151 indices[i] = v->v_order;
2159 graph_assign_indices(struct owner_graph *g, int *indices, int nextunused,
2160 struct owner_vertex_list *set)
2162 struct owner_vertex *v, *vlowest;
2164 while (!TAILQ_EMPTY(set)) {
2166 TAILQ_FOREACH(v, set, v_link) {
2167 if (!vlowest || v->v_order < vlowest->v_order)
2170 TAILQ_REMOVE(set, vlowest, v_link);
2171 vlowest->v_order = indices[nextunused];
2172 g->g_vertices[vlowest->v_order] = vlowest;
2176 return (nextunused);
2180 graph_add_edge(struct owner_graph *g, struct owner_vertex *x,
2181 struct owner_vertex *y)
2183 struct owner_edge *e;
2184 struct owner_vertex_list deltaF, deltaB;
2185 int nF, nB, n, vi, i;
2188 sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
2190 LIST_FOREACH(e, &x->v_outedges, e_outlink) {
2198 if (lockf_debug & 8) {
2199 printf("adding edge %d:", x->v_order);
2200 lf_print_owner(x->v_owner);
2201 printf(" -> %d:", y->v_order);
2202 lf_print_owner(y->v_owner);
2206 if (y->v_order < x->v_order) {
2208 * The new edge violates the order. First find the set
2209 * of affected vertices reachable from y (deltaF) and
2210 * the set of affect vertices affected that reach x
2211 * (deltaB), using the graph generation number to
2212 * detect whether we have visited a given vertex
2213 * already. We re-order the graph so that each vertex
2214 * in deltaB appears before each vertex in deltaF.
2216 * If x is a member of deltaF, then the new edge would
2217 * create a cycle. Otherwise, we may assume that
2218 * deltaF and deltaB are disjoint.
2221 if (g->g_gen == 0) {
2225 for (vi = 0; vi < g->g_size; vi++) {
2226 g->g_vertices[vi]->v_gen = 0;
2230 nF = graph_delta_forward(g, x, y, &deltaF);
2233 if (lockf_debug & 8) {
2234 struct owner_vertex_list path;
2235 printf("deadlock: ");
2237 graph_reaches(y, x, &path);
2238 graph_print_vertices(&path);
2245 if (lockf_debug & 8) {
2246 printf("re-ordering graph vertices\n");
2247 printf("deltaF = ");
2248 graph_print_vertices(&deltaF);
2252 nB = graph_delta_backward(g, x, y, &deltaB);
2255 if (lockf_debug & 8) {
2256 printf("deltaB = ");
2257 graph_print_vertices(&deltaB);
2262 * We first build a set of vertex indices (vertex
2263 * order values) that we may use, then we re-assign
2264 * orders first to those vertices in deltaB, then to
2265 * deltaF. Note that the contents of deltaF and deltaB
2266 * may be partially disordered - we perform an
2267 * insertion sort while building our index set.
2269 indices = g->g_indexbuf;
2270 n = graph_add_indices(indices, 0, &deltaF);
2271 graph_add_indices(indices, n, &deltaB);
2274 * We must also be sure to maintain the relative
2275 * ordering of deltaF and deltaB when re-assigning
2276 * vertices. We do this by iteratively removing the
2277 * lowest ordered element from the set and assigning
2278 * it the next value from our new ordering.
2280 i = graph_assign_indices(g, indices, 0, &deltaB);
2281 graph_assign_indices(g, indices, i, &deltaF);
2284 if (lockf_debug & 8) {
2285 struct owner_vertex_list set;
2287 for (i = 0; i < nB + nF; i++)
2288 TAILQ_INSERT_TAIL(&set,
2289 g->g_vertices[indices[i]], v_link);
2290 printf("new ordering = ");
2291 graph_print_vertices(&set);
2296 KASSERT(x->v_order < y->v_order, ("Failed to re-order graph"));
2299 if (lockf_debug & 8) {
2300 graph_check(g, TRUE);
2304 e = malloc(sizeof(struct owner_edge), M_LOCKF, M_WAITOK);
2306 LIST_INSERT_HEAD(&x->v_outedges, e, e_outlink);
2307 LIST_INSERT_HEAD(&y->v_inedges, e, e_inlink);
2316 * Remove an edge x->y from the graph.
2319 graph_remove_edge(struct owner_graph *g, struct owner_vertex *x,
2320 struct owner_vertex *y)
2322 struct owner_edge *e;
2324 sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
2326 LIST_FOREACH(e, &x->v_outedges, e_outlink) {
2330 KASSERT(e, ("Removing non-existent edge from deadlock graph"));
2333 if (e->e_refs == 0) {
2335 if (lockf_debug & 8) {
2336 printf("removing edge %d:", x->v_order);
2337 lf_print_owner(x->v_owner);
2338 printf(" -> %d:", y->v_order);
2339 lf_print_owner(y->v_owner);
2343 LIST_REMOVE(e, e_outlink);
2344 LIST_REMOVE(e, e_inlink);
2350 * Allocate a vertex from the free list. Return ENOMEM if there are
2353 static struct owner_vertex *
2354 graph_alloc_vertex(struct owner_graph *g, struct lock_owner *lo)
2356 struct owner_vertex *v;
2358 sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
2360 v = malloc(sizeof(struct owner_vertex), M_LOCKF, M_WAITOK);
2361 if (g->g_size == g->g_space) {
2362 g->g_vertices = realloc(g->g_vertices,
2363 2 * g->g_space * sizeof(struct owner_vertex *),
2365 free(g->g_indexbuf, M_LOCKF);
2366 g->g_indexbuf = malloc(2 * g->g_space * sizeof(int),
2368 g->g_space = 2 * g->g_space;
2370 v->v_order = g->g_size;
2371 v->v_gen = g->g_gen;
2372 g->g_vertices[g->g_size] = v;
2375 LIST_INIT(&v->v_outedges);
2376 LIST_INIT(&v->v_inedges);
2383 graph_free_vertex(struct owner_graph *g, struct owner_vertex *v)
2385 struct owner_vertex *w;
2388 sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
2390 KASSERT(LIST_EMPTY(&v->v_outedges), ("Freeing vertex with edges"));
2391 KASSERT(LIST_EMPTY(&v->v_inedges), ("Freeing vertex with edges"));
2394 * Remove from the graph's array and close up the gap,
2395 * renumbering the other vertices.
2397 for (i = v->v_order + 1; i < g->g_size; i++) {
2398 w = g->g_vertices[i];
2400 g->g_vertices[i - 1] = w;
2407 static struct owner_graph *
2408 graph_init(struct owner_graph *g)
2411 g->g_vertices = malloc(10 * sizeof(struct owner_vertex *),
2415 g->g_indexbuf = malloc(g->g_space * sizeof(int), M_LOCKF, M_WAITOK);
2423 * Print description of a lock owner
2426 lf_print_owner(struct lock_owner *lo)
2429 if (lo->lo_flags & F_REMOTE) {
2430 printf("remote pid %d, system %d",
2431 lo->lo_pid, lo->lo_sysid);
2432 } else if (lo->lo_flags & F_FLOCK) {
2433 printf("file %p", lo->lo_id);
2435 printf("local pid %d", lo->lo_pid);
2443 lf_print(char *tag, struct lockf_entry *lock)
2446 printf("%s: lock %p for ", tag, (void *)lock);
2447 lf_print_owner(lock->lf_owner);
2448 if (lock->lf_inode != (struct inode *)0)
2449 printf(" in ino %ju on dev <%s>,",
2450 (uintmax_t)lock->lf_inode->i_number,
2451 devtoname(lock->lf_inode->i_dev));
2452 printf(" %s, start %jd, end ",
2453 lock->lf_type == F_RDLCK ? "shared" :
2454 lock->lf_type == F_WRLCK ? "exclusive" :
2455 lock->lf_type == F_UNLCK ? "unlock" : "unknown",
2456 (intmax_t)lock->lf_start);
2457 if (lock->lf_end == OFF_MAX)
2460 printf("%jd", (intmax_t)lock->lf_end);
2461 if (!LIST_EMPTY(&lock->lf_outedges))
2462 printf(" block %p\n",
2463 (void *)LIST_FIRST(&lock->lf_outedges)->le_to);
2469 lf_printlist(char *tag, struct lockf_entry *lock)
2471 struct lockf_entry *lf, *blk;
2472 struct lockf_edge *e;
2474 if (lock->lf_inode == (struct inode *)0)
2477 printf("%s: Lock list for ino %ju on dev <%s>:\n",
2478 tag, (uintmax_t)lock->lf_inode->i_number,
2479 devtoname(lock->lf_inode->i_dev));
2480 LIST_FOREACH(lf, &lock->lf_vnode->v_lockf->ls_active, lf_link) {
2481 printf("\tlock %p for ",(void *)lf);
2482 lf_print_owner(lock->lf_owner);
2483 printf(", %s, start %jd, end %jd",
2484 lf->lf_type == F_RDLCK ? "shared" :
2485 lf->lf_type == F_WRLCK ? "exclusive" :
2486 lf->lf_type == F_UNLCK ? "unlock" :
2487 "unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
2488 LIST_FOREACH(e, &lf->lf_outedges, le_outlink) {
2490 printf("\n\t\tlock request %p for ", (void *)blk);
2491 lf_print_owner(blk->lf_owner);
2492 printf(", %s, start %jd, end %jd",
2493 blk->lf_type == F_RDLCK ? "shared" :
2494 blk->lf_type == F_WRLCK ? "exclusive" :
2495 blk->lf_type == F_UNLCK ? "unlock" :
2496 "unknown", (intmax_t)blk->lf_start,
2497 (intmax_t)blk->lf_end);
2498 if (!LIST_EMPTY(&blk->lf_inedges))
2499 panic("lf_printlist: bad list");
2504 #endif /* LOCKF_DEBUG */