#include <sys/cdefs.h>
#include "opt_debug_lockf.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/hash.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sbuf.h>
#include <sys/stat.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/user.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/fcntl.h>
#include <sys/lockf.h>
#include <sys/taskqueue.h>
#ifdef LOCKF_DEBUG
#include <sys/sysctl.h>
static int lockf_debug = 0;
SYSCTL_INT(_debug, OID_AUTO, lockf_debug, CTLFLAG_RW, &lockf_debug, 0, "");
#endif
static MALLOC_DEFINE(M_LOCKF, "lockf", "Byte-range locking structures");
struct owner_edge;
struct owner_vertex;
struct owner_vertex_list;
struct owner_graph;
#define NOLOCKF (struct lockf_entry *)0
#define SELF 0x1
#define OTHERS 0x2
static void lf_init(void *);
static int lf_hash_owner(caddr_t, struct vnode *, struct flock *, int);
static int lf_owner_matches(struct lock_owner *, caddr_t, struct flock *,
int);
static struct lockf_entry *
lf_alloc_lock(struct lock_owner *);
static int lf_free_lock(struct lockf_entry *);
static int lf_clearlock(struct lockf *, struct lockf_entry *);
static int lf_overlaps(struct lockf_entry *, struct lockf_entry *);
static int lf_blocks(struct lockf_entry *, struct lockf_entry *);
static void lf_free_edge(struct lockf_edge *);
static struct lockf_edge *
lf_alloc_edge(void);
static void lf_alloc_vertex(struct lockf_entry *);
static int lf_add_edge(struct lockf_entry *, struct lockf_entry *);
static void lf_remove_edge(struct lockf_edge *);
static void lf_remove_outgoing(struct lockf_entry *);
static void lf_remove_incoming(struct lockf_entry *);
static int lf_add_outgoing(struct lockf *, struct lockf_entry *);
static int lf_add_incoming(struct lockf *, struct lockf_entry *);
static int lf_findoverlap(struct lockf_entry **, struct lockf_entry *,
int);
static struct lockf_entry *
lf_getblock(struct lockf *, struct lockf_entry *);
static int lf_getlock(struct lockf *, struct lockf_entry *, struct flock *);
static void lf_insert_lock(struct lockf *, struct lockf_entry *);
static void lf_wakeup_lock(struct lockf *, struct lockf_entry *);
static void lf_update_dependancies(struct lockf *, struct lockf_entry *,
int all, struct lockf_entry_list *);
static void lf_set_start(struct lockf *, struct lockf_entry *, off_t,
struct lockf_entry_list*);
static void lf_set_end(struct lockf *, struct lockf_entry *, off_t,
struct lockf_entry_list*);
static int lf_setlock(struct lockf *, struct lockf_entry *,
struct vnode *, void **cookiep);
static int lf_cancel(struct lockf *, struct lockf_entry *, void *);
static void lf_split(struct lockf *, struct lockf_entry *,
struct lockf_entry *, struct lockf_entry_list *);
#ifdef LOCKF_DEBUG
static int graph_reaches(struct owner_vertex *x, struct owner_vertex *y,
struct owner_vertex_list *path);
static void graph_check(struct owner_graph *g, int checkorder);
static void graph_print_vertices(struct owner_vertex_list *set);
#endif
static int graph_delta_forward(struct owner_graph *g,
struct owner_vertex *x, struct owner_vertex *y,
struct owner_vertex_list *delta);
static int graph_delta_backward(struct owner_graph *g,
struct owner_vertex *x, struct owner_vertex *y,
struct owner_vertex_list *delta);
static int graph_add_indices(int *indices, int n,
struct owner_vertex_list *set);
static int graph_assign_indices(struct owner_graph *g, int *indices,
int nextunused, struct owner_vertex_list *set);
static int graph_add_edge(struct owner_graph *g,
struct owner_vertex *x, struct owner_vertex *y);
static void graph_remove_edge(struct owner_graph *g,
struct owner_vertex *x, struct owner_vertex *y);
static struct owner_vertex *graph_alloc_vertex(struct owner_graph *g,
struct lock_owner *lo);
static void graph_free_vertex(struct owner_graph *g,
struct owner_vertex *v);
static struct owner_graph * graph_init(struct owner_graph *g);
#ifdef LOCKF_DEBUG
static void lf_print(char *, struct lockf_entry *);
static void lf_printlist(char *, struct lockf_entry *);
static void lf_print_owner(struct lock_owner *);
#endif
#define LOCK_OWNER_HASH_SIZE 256
struct lock_owner {
LIST_ENTRY(lock_owner) lo_link;
int lo_refs;
int lo_flags;
caddr_t lo_id;
pid_t lo_pid;
int lo_sysid;
int lo_hash;
struct owner_vertex *lo_vertex;
};
LIST_HEAD(lock_owner_list, lock_owner);
struct lock_owner_chain {
struct sx lock;
struct lock_owner_list list;
};
static struct sx lf_lock_states_lock;
static struct lockf_list lf_lock_states;
static struct lock_owner_chain lf_lock_owners[LOCK_OWNER_HASH_SIZE];
struct owner_vertex;
struct owner_edge {
LIST_ENTRY(owner_edge) e_outlink;
LIST_ENTRY(owner_edge) e_inlink;
int e_refs;
struct owner_vertex *e_from;
struct owner_vertex *e_to;
};
LIST_HEAD(owner_edge_list, owner_edge);
struct owner_vertex {
TAILQ_ENTRY(owner_vertex) v_link;
uint32_t v_gen;
int v_order;
struct owner_edge_list v_outedges;
struct owner_edge_list v_inedges;
struct lock_owner *v_owner;
};
TAILQ_HEAD(owner_vertex_list, owner_vertex);
struct owner_graph {
struct owner_vertex** g_vertices;
int g_size;
int g_space;
int *g_indexbuf;
uint32_t g_gen;
};
static struct sx lf_owner_graph_lock;
static struct owner_graph lf_owner_graph;
static void
lf_init(void *dummy)
{
int i;
sx_init(&lf_lock_states_lock, "lock states lock");
LIST_INIT(&lf_lock_states);
for (i = 0; i < LOCK_OWNER_HASH_SIZE; i++) {
sx_init(&lf_lock_owners[i].lock, "lock owners lock");
LIST_INIT(&lf_lock_owners[i].list);
}
sx_init(&lf_owner_graph_lock, "owner graph lock");
graph_init(&lf_owner_graph);
}
SYSINIT(lf_init, SI_SUB_LOCK, SI_ORDER_FIRST, lf_init, NULL);
static int
lf_hash_owner(caddr_t id, struct vnode *vp, struct flock *fl, int flags)
{
uint32_t h;
if (flags & F_REMOTE) {
h = HASHSTEP(0, fl->l_pid);
h = HASHSTEP(h, fl->l_sysid);
} else if (flags & F_FLOCK) {
h = ((uintptr_t) id) >> 7;
} else {
h = ((uintptr_t) vp) >> 7;
}
return (h % LOCK_OWNER_HASH_SIZE);
}
static int
lf_owner_matches(struct lock_owner *lo, caddr_t id, struct flock *fl,
int flags)
{
if (flags & F_REMOTE) {
return lo->lo_pid == fl->l_pid
&& lo->lo_sysid == fl->l_sysid;
} else {
return lo->lo_id == id;
}
}
static struct lockf_entry *
lf_alloc_lock(struct lock_owner *lo)
{
struct lockf_entry *lf;
lf = malloc(sizeof(struct lockf_entry), M_LOCKF, M_WAITOK|M_ZERO);
#ifdef LOCKF_DEBUG
if (lockf_debug & 4)
printf("Allocated lock %p\n", lf);
#endif
if (lo) {
sx_xlock(&lf_lock_owners[lo->lo_hash].lock);
lo->lo_refs++;
sx_xunlock(&lf_lock_owners[lo->lo_hash].lock);
lf->lf_owner = lo;
}
return (lf);
}
static int
lf_free_lock(struct lockf_entry *lock)
{
struct sx *chainlock;
KASSERT(lock->lf_refs > 0, ("lockf_entry negative ref count %p", lock));
if (--lock->lf_refs > 0)
return (0);
struct lock_owner *lo = lock->lf_owner;
if (lo) {
KASSERT(LIST_EMPTY(&lock->lf_outedges),
("freeing lock with dependencies"));
KASSERT(LIST_EMPTY(&lock->lf_inedges),
("freeing lock with dependants"));
chainlock = &lf_lock_owners[lo->lo_hash].lock;
sx_xlock(chainlock);
KASSERT(lo->lo_refs > 0, ("lock owner refcount"));
lo->lo_refs--;
if (lo->lo_refs == 0) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
printf("lf_free_lock: freeing lock owner %p\n",
lo);
#endif
if (lo->lo_vertex) {
sx_xlock(&lf_owner_graph_lock);
graph_free_vertex(&lf_owner_graph,
lo->lo_vertex);
sx_xunlock(&lf_owner_graph_lock);
}
LIST_REMOVE(lo, lo_link);
free(lo, M_LOCKF);
#ifdef LOCKF_DEBUG
if (lockf_debug & 4)
printf("Freed lock owner %p\n", lo);
#endif
}
sx_unlock(chainlock);
}
if ((lock->lf_flags & F_REMOTE) && lock->lf_vnode) {
vrele(lock->lf_vnode);
lock->lf_vnode = NULL;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 4)
printf("Freed lock %p\n", lock);
#endif
free(lock, M_LOCKF);
return (1);
}
int
lf_advlockasync(struct vop_advlockasync_args *ap, struct lockf **statep,
u_quad_t size)
{
struct lockf *state;
struct flock *fl = ap->a_fl;
struct lockf_entry *lock;
struct vnode *vp = ap->a_vp;
caddr_t id = ap->a_id;
int flags = ap->a_flags;
int hash;
struct lock_owner *lo;
off_t start, end, oadd;
int error;
if (ap->a_op == F_UNLCKSYS) {
lf_clearremotesys(fl->l_sysid);
return (0);
}
switch (fl->l_whence) {
case SEEK_SET:
case SEEK_CUR:
start = fl->l_start;
break;
case SEEK_END:
if (size > OFF_MAX ||
(fl->l_start > 0 && size > OFF_MAX - fl->l_start))
return (EOVERFLOW);
start = size + fl->l_start;
break;
default:
return (EINVAL);
}
if (start < 0)
return (EINVAL);
if (fl->l_len < 0) {
if (start == 0)
return (EINVAL);
end = start - 1;
start += fl->l_len;
if (start < 0)
return (EINVAL);
} else if (fl->l_len == 0) {
end = OFF_MAX;
} else {
oadd = fl->l_len - 1;
if (oadd > OFF_MAX - start)
return (EOVERFLOW);
end = start + oadd;
}
retry_setlock:
if (ap->a_op != F_SETLK && (*statep) == NULL) {
VI_LOCK(vp);
if ((*statep) == NULL) {
fl->l_type = F_UNLCK;
VI_UNLOCK(vp);
return (0);
}
VI_UNLOCK(vp);
}
hash = lf_hash_owner(id, vp, fl, flags);
sx_xlock(&lf_lock_owners[hash].lock);
LIST_FOREACH(lo, &lf_lock_owners[hash].list, lo_link)
if (lf_owner_matches(lo, id, fl, flags))
break;
if (!lo) {
lo = malloc(sizeof(struct lock_owner), M_LOCKF,
M_WAITOK|M_ZERO);
#ifdef LOCKF_DEBUG
if (lockf_debug & 4)
printf("Allocated lock owner %p\n", lo);
#endif
lo->lo_refs = 1;
lo->lo_flags = flags;
lo->lo_id = id;
lo->lo_hash = hash;
if (flags & F_REMOTE) {
lo->lo_pid = fl->l_pid;
lo->lo_sysid = fl->l_sysid;
} else if (flags & F_FLOCK) {
lo->lo_pid = -1;
lo->lo_sysid = 0;
} else {
struct proc *p = (struct proc *) id;
lo->lo_pid = p->p_pid;
lo->lo_sysid = 0;
}
lo->lo_vertex = NULL;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
printf("lf_advlockasync: new lock owner %p ", lo);
lf_print_owner(lo);
printf("\n");
}
#endif
LIST_INSERT_HEAD(&lf_lock_owners[hash].list, lo, lo_link);
} else {
lo->lo_refs++;
}
sx_xunlock(&lf_lock_owners[hash].lock);
lock = lf_alloc_lock(NULL);
lock->lf_refs = 1;
lock->lf_start = start;
lock->lf_end = end;
lock->lf_owner = lo;
lock->lf_vnode = vp;
if (flags & F_REMOTE) {
vref(vp);
}
lock->lf_type = fl->l_type;
LIST_INIT(&lock->lf_outedges);
LIST_INIT(&lock->lf_inedges);
lock->lf_async_task = ap->a_task;
lock->lf_flags = ap->a_flags;
VI_LOCK(vp);
if (VN_IS_DOOMED(vp)) {
VI_UNLOCK(vp);
lf_free_lock(lock);
return (ENOENT);
}
state = *statep;
if (state == NULL) {
struct lockf *ls;
VI_UNLOCK(vp);
ls = malloc(sizeof(struct lockf), M_LOCKF, M_WAITOK|M_ZERO);
sx_init(&ls->ls_lock, "ls_lock");
LIST_INIT(&ls->ls_active);
LIST_INIT(&ls->ls_pending);
ls->ls_threads = 1;
sx_xlock(&lf_lock_states_lock);
LIST_INSERT_HEAD(&lf_lock_states, ls, ls_link);
sx_xunlock(&lf_lock_states_lock);
VI_LOCK(vp);
if (VN_IS_DOOMED(vp)) {
VI_UNLOCK(vp);
sx_xlock(&lf_lock_states_lock);
LIST_REMOVE(ls, ls_link);
sx_xunlock(&lf_lock_states_lock);
sx_destroy(&ls->ls_lock);
free(ls, M_LOCKF);
lf_free_lock(lock);
return (ENOENT);
}
if ((*statep) == NULL) {
state = *statep = ls;
VI_UNLOCK(vp);
} else {
state = *statep;
MPASS(state->ls_threads >= 0);
state->ls_threads++;
VI_UNLOCK(vp);
sx_xlock(&lf_lock_states_lock);
LIST_REMOVE(ls, ls_link);
sx_xunlock(&lf_lock_states_lock);
sx_destroy(&ls->ls_lock);
free(ls, M_LOCKF);
}
} else {
MPASS(state->ls_threads >= 0);
state->ls_threads++;
VI_UNLOCK(vp);
}
sx_xlock(&state->ls_lock);
if (VN_IS_DOOMED(vp)) {
VI_LOCK(vp);
MPASS(state->ls_threads > 0);
state->ls_threads--;
wakeup(state);
VI_UNLOCK(vp);
sx_xunlock(&state->ls_lock);
lf_free_lock(lock);
return (ENOENT);
}
switch (ap->a_op) {
case F_SETLK:
error = lf_setlock(state, lock, vp, ap->a_cookiep);
break;
case F_UNLCK:
error = lf_clearlock(state, lock);
lf_free_lock(lock);
break;
case F_GETLK:
error = lf_getlock(state, lock, fl);
lf_free_lock(lock);
break;
case F_CANCEL:
if (ap->a_cookiep)
error = lf_cancel(state, lock, *ap->a_cookiep);
else
error = EINVAL;
lf_free_lock(lock);
break;
default:
lf_free_lock(lock);
error = EINVAL;
break;
}
#ifdef DIAGNOSTIC
LIST_FOREACH(lock, &state->ls_active, lf_link) {
struct lockf_entry *lf;
if (LIST_NEXT(lock, lf_link))
KASSERT((lock->lf_start
<= LIST_NEXT(lock, lf_link)->lf_start),
("locks disordered"));
LIST_FOREACH(lf, &state->ls_active, lf_link) {
if (lock == lf)
break;
KASSERT(!lf_blocks(lock, lf),
("two conflicting active locks"));
if (lock->lf_owner == lf->lf_owner)
KASSERT(!lf_overlaps(lock, lf),
("two overlapping locks from same owner"));
}
}
LIST_FOREACH(lock, &state->ls_pending, lf_link) {
KASSERT(!LIST_EMPTY(&lock->lf_outedges),
("pending lock which should be active"));
}
#endif
sx_xunlock(&state->ls_lock);
VI_LOCK(vp);
MPASS(state->ls_threads > 0);
state->ls_threads--;
if (state->ls_threads != 0) {
wakeup(state);
}
VI_UNLOCK(vp);
if (error == EDOOFUS) {
KASSERT(ap->a_op == F_SETLK, ("EDOOFUS"));
goto retry_setlock;
}
return (error);
}
int
lf_advlock(struct vop_advlock_args *ap, struct lockf **statep, u_quad_t size)
{
struct vop_advlockasync_args a;
a.a_vp = ap->a_vp;
a.a_id = ap->a_id;
a.a_op = ap->a_op;
a.a_fl = ap->a_fl;
a.a_flags = ap->a_flags;
a.a_task = NULL;
a.a_cookiep = NULL;
return (lf_advlockasync(&a, statep, size));
}
void
lf_purgelocks(struct vnode *vp, struct lockf **statep)
{
struct lockf *state;
struct lockf_entry *lock, *nlock;
KASSERT(VN_IS_DOOMED(vp),
("lf_purgelocks: vp %p has not vgone yet", vp));
state = *statep;
if (state == NULL) {
return;
}
VI_LOCK(vp);
*statep = NULL;
if (LIST_EMPTY(&state->ls_active) && state->ls_threads == 0) {
KASSERT(LIST_EMPTY(&state->ls_pending),
("freeing state with pending locks"));
VI_UNLOCK(vp);
goto out_free;
}
MPASS(state->ls_threads >= 0);
state->ls_threads++;
VI_UNLOCK(vp);
sx_xlock(&state->ls_lock);
sx_xlock(&lf_owner_graph_lock);
LIST_FOREACH_SAFE(lock, &state->ls_pending, lf_link, nlock) {
LIST_REMOVE(lock, lf_link);
lf_remove_outgoing(lock);
lf_remove_incoming(lock);
if (lock->lf_async_task) {
lf_free_lock(lock);
} else {
lock->lf_flags |= F_INTR;
wakeup(lock);
}
}
sx_xunlock(&lf_owner_graph_lock);
sx_xunlock(&state->ls_lock);
VI_LOCK(vp);
while (state->ls_threads > 1)
msleep(state, VI_MTX(vp), 0, "purgelocks", 0);
VI_UNLOCK(vp);
KASSERT(LIST_EMPTY(&state->ls_pending),
("lock pending for %p", state));
LIST_FOREACH_SAFE(lock, &state->ls_active, lf_link, nlock) {
LIST_REMOVE(lock, lf_link);
lf_free_lock(lock);
}
out_free:
sx_xlock(&lf_lock_states_lock);
LIST_REMOVE(state, ls_link);
sx_xunlock(&lf_lock_states_lock);
sx_destroy(&state->ls_lock);
free(state, M_LOCKF);
}
static int
lf_overlaps(struct lockf_entry *x, struct lockf_entry *y)
{
return (x->lf_start <= y->lf_end && x->lf_end >= y->lf_start);
}
static int
lf_blocks(struct lockf_entry *x, struct lockf_entry *y)
{
return x->lf_owner != y->lf_owner
&& (x->lf_type == F_WRLCK || y->lf_type == F_WRLCK)
&& lf_overlaps(x, y);
}
static struct lockf_edge *
lf_alloc_edge(void)
{
return (malloc(sizeof(struct lockf_edge), M_LOCKF, M_WAITOK|M_ZERO));
}
static void
lf_free_edge(struct lockf_edge *e)
{
free(e, M_LOCKF);
}
static void
lf_alloc_vertex(struct lockf_entry *lock)
{
struct owner_graph *g = &lf_owner_graph;
if (!lock->lf_owner->lo_vertex)
lock->lf_owner->lo_vertex =
graph_alloc_vertex(g, lock->lf_owner);
}
static int
lf_add_edge(struct lockf_entry *x, struct lockf_entry *y)
{
struct owner_graph *g = &lf_owner_graph;
struct lockf_edge *e;
int error;
#ifdef DIAGNOSTIC
LIST_FOREACH(e, &x->lf_outedges, le_outlink)
KASSERT(e->le_to != y, ("adding lock edge twice"));
#endif
lf_alloc_vertex(x);
lf_alloc_vertex(y);
error = graph_add_edge(g, x->lf_owner->lo_vertex,
y->lf_owner->lo_vertex);
if (error)
return (error);
e = lf_alloc_edge();
LIST_INSERT_HEAD(&x->lf_outedges, e, le_outlink);
LIST_INSERT_HEAD(&y->lf_inedges, e, le_inlink);
e->le_from = x;
e->le_to = y;
return (0);
}
static void
lf_remove_edge(struct lockf_edge *e)
{
struct owner_graph *g = &lf_owner_graph;
struct lockf_entry *x = e->le_from;
struct lockf_entry *y = e->le_to;
graph_remove_edge(g, x->lf_owner->lo_vertex, y->lf_owner->lo_vertex);
LIST_REMOVE(e, le_outlink);
LIST_REMOVE(e, le_inlink);
e->le_from = NULL;
e->le_to = NULL;
lf_free_edge(e);
}
static void
lf_remove_outgoing(struct lockf_entry *x)
{
struct lockf_edge *e;
while ((e = LIST_FIRST(&x->lf_outedges)) != NULL) {
lf_remove_edge(e);
}
}
static void
lf_remove_incoming(struct lockf_entry *x)
{
struct lockf_edge *e;
while ((e = LIST_FIRST(&x->lf_inedges)) != NULL) {
lf_remove_edge(e);
}
}
static int
lf_add_outgoing(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry *overlap;
int error;
LIST_FOREACH(overlap, &state->ls_active, lf_link) {
if (overlap->lf_start > lock->lf_end)
break;
if (!lf_blocks(lock, overlap))
continue;
error = lf_add_edge(lock, overlap);
if (error) {
lf_remove_outgoing(lock);
return (error);
}
}
LIST_FOREACH(overlap, &state->ls_pending, lf_link) {
if (!lf_blocks(lock, overlap))
continue;
error = lf_add_edge(lock, overlap);
if (error) {
lf_remove_outgoing(lock);
return (error);
}
}
return (0);
}
static int
lf_add_incoming(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry *overlap;
int error;
sx_assert(&state->ls_lock, SX_XLOCKED);
if (LIST_EMPTY(&state->ls_pending))
return (0);
error = 0;
sx_xlock(&lf_owner_graph_lock);
LIST_FOREACH(overlap, &state->ls_pending, lf_link) {
if (!lf_blocks(lock, overlap))
continue;
error = lf_add_edge(overlap, lock);
if (error) {
lf_remove_incoming(lock);
break;
}
}
sx_xunlock(&lf_owner_graph_lock);
return (error);
}
static void
lf_insert_lock(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry *lf, *lfprev;
if (LIST_EMPTY(&state->ls_active)) {
LIST_INSERT_HEAD(&state->ls_active, lock, lf_link);
return;
}
lfprev = NULL;
LIST_FOREACH(lf, &state->ls_active, lf_link) {
if (lf->lf_start > lock->lf_start) {
LIST_INSERT_BEFORE(lf, lock, lf_link);
return;
}
lfprev = lf;
}
LIST_INSERT_AFTER(lfprev, lock, lf_link);
}
static void
lf_wakeup_lock(struct lockf *state, struct lockf_entry *wakelock)
{
LIST_REMOVE(wakelock, lf_link);
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_wakeup_lock: awakening", wakelock);
#endif
if (wakelock->lf_async_task) {
taskqueue_enqueue(taskqueue_thread, wakelock->lf_async_task);
} else {
wakeup(wakelock);
}
}
static void
lf_update_dependancies(struct lockf *state, struct lockf_entry *lock, int all,
struct lockf_entry_list *granted)
{
struct lockf_edge *e, *ne;
struct lockf_entry *deplock;
LIST_FOREACH_SAFE(e, &lock->lf_inedges, le_inlink, ne) {
deplock = e->le_from;
if (all || !lf_blocks(lock, deplock)) {
sx_xlock(&lf_owner_graph_lock);
lf_remove_edge(e);
sx_xunlock(&lf_owner_graph_lock);
if (LIST_EMPTY(&deplock->lf_outedges)) {
lf_wakeup_lock(state, deplock);
LIST_INSERT_HEAD(granted, deplock, lf_link);
}
}
}
}
static void
lf_set_start(struct lockf *state, struct lockf_entry *lock, off_t new_start,
struct lockf_entry_list *granted)
{
KASSERT(new_start >= lock->lf_start, ("can't increase lock"));
lock->lf_start = new_start;
LIST_REMOVE(lock, lf_link);
lf_insert_lock(state, lock);
lf_update_dependancies(state, lock, FALSE, granted);
}
static void
lf_set_end(struct lockf *state, struct lockf_entry *lock, off_t new_end,
struct lockf_entry_list *granted)
{
KASSERT(new_end <= lock->lf_end, ("can't increase lock"));
lock->lf_end = new_end;
lf_update_dependancies(state, lock, FALSE, granted);
}
static void
lf_activate_lock(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry *overlap, *lf;
struct lockf_entry_list granted;
int ovcase;
LIST_INIT(&granted);
LIST_INSERT_HEAD(&granted, lock, lf_link);
while (!LIST_EMPTY(&granted)) {
lock = LIST_FIRST(&granted);
LIST_REMOVE(lock, lf_link);
overlap = LIST_FIRST(&state->ls_active);
for (;;) {
ovcase = lf_findoverlap(&overlap, lock, SELF);
#ifdef LOCKF_DEBUG
if (ovcase && (lockf_debug & 2)) {
printf("lf_setlock: overlap %d", ovcase);
lf_print("", overlap);
}
#endif
switch (ovcase) {
case 0:
break;
case 1:
LIST_REMOVE(overlap, lf_link);
lf_update_dependancies(state, overlap, TRUE,
&granted);
lf_free_lock(overlap);
break;
case 2:
lf_split(state, overlap, lock, &granted);
break;
case 3:
lf = LIST_NEXT(overlap, lf_link);
LIST_REMOVE(overlap, lf_link);
lf_update_dependancies(state, overlap, TRUE,
&granted);
lf_free_lock(overlap);
overlap = lf;
continue;
case 4:
lf_set_end(state, overlap, lock->lf_start - 1,
&granted);
overlap = LIST_NEXT(overlap, lf_link);
continue;
case 5:
lf_set_start(state, overlap, lock->lf_end + 1,
&granted);
break;
}
break;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
if (lock->lf_type != F_UNLCK)
lf_print("lf_activate_lock: activated", lock);
else
lf_print("lf_activate_lock: unlocked", lock);
lf_printlist("lf_activate_lock", lock);
}
#endif
if (lock->lf_type != F_UNLCK)
lf_insert_lock(state, lock);
}
}
static void
lf_cancel_lock(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry_list granted;
LIST_REMOVE(lock, lf_link);
sx_xlock(&lf_owner_graph_lock);
lf_remove_outgoing(lock);
sx_xunlock(&lf_owner_graph_lock);
LIST_INIT(&granted);
lf_update_dependancies(state, lock, TRUE, &granted);
lf_free_lock(lock);
while (!LIST_EMPTY(&granted)) {
lock = LIST_FIRST(&granted);
LIST_REMOVE(lock, lf_link);
lf_activate_lock(state, lock);
}
}
static int
lf_setlock(struct lockf *state, struct lockf_entry *lock, struct vnode *vp,
void **cookiep)
{
static char lockstr[] = "lockf";
int error, priority, stops_deferred;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_setlock", lock);
#endif
priority = PLOCK;
if (lock->lf_type == F_WRLCK)
priority += 4;
if (!(lock->lf_flags & F_NOINTR))
priority |= PCATCH;
if (lf_getblock(state, lock)) {
if ((lock->lf_flags & F_WAIT) == 0
&& lock->lf_async_task == NULL) {
lf_free_lock(lock);
error = EAGAIN;
goto out;
}
if ((lock->lf_flags & F_FLOCK) &&
lock->lf_type == F_WRLCK) {
lock->lf_type = F_UNLCK;
lf_activate_lock(state, lock);
lock->lf_type = F_WRLCK;
}
sx_xlock(&lf_owner_graph_lock);
error = lf_add_outgoing(state, lock);
sx_xunlock(&lf_owner_graph_lock);
if (error) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_setlock: deadlock", lock);
#endif
lf_free_lock(lock);
goto out;
}
LIST_INSERT_HEAD(&state->ls_pending, lock, lf_link);
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
struct lockf_edge *e;
LIST_FOREACH(e, &lock->lf_outedges, le_outlink) {
lf_print("lf_setlock: blocking on", e->le_to);
lf_printlist("lf_setlock", e->le_to);
}
}
#endif
if ((lock->lf_flags & F_WAIT) == 0) {
*cookiep = (void *) lock;
error = EINPROGRESS;
goto out;
}
lock->lf_refs++;
stops_deferred = sigdeferstop(SIGDEFERSTOP_ERESTART);
error = sx_sleep(lock, &state->ls_lock, priority, lockstr, 0);
sigallowstop(stops_deferred);
if (lf_free_lock(lock)) {
error = EDOOFUS;
goto out;
}
if (lock->lf_flags & F_INTR) {
error = EINTR;
lf_free_lock(lock);
goto out;
}
if (LIST_EMPTY(&lock->lf_outedges)) {
error = 0;
} else {
lf_cancel_lock(state, lock);
goto out;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 1) {
lf_print("lf_setlock: granted", lock);
}
#endif
goto out;
}
error = lf_add_incoming(state, lock);
if (error) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_setlock: deadlock", lock);
#endif
lf_free_lock(lock);
goto out;
}
lf_activate_lock(state, lock);
error = 0;
out:
return (error);
}
static int
lf_clearlock(struct lockf *state, struct lockf_entry *unlock)
{
struct lockf_entry *overlap;
overlap = LIST_FIRST(&state->ls_active);
if (overlap == NOLOCKF)
return (0);
#ifdef LOCKF_DEBUG
if (unlock->lf_type != F_UNLCK)
panic("lf_clearlock: bad type");
if (lockf_debug & 1)
lf_print("lf_clearlock", unlock);
#endif
lf_activate_lock(state, unlock);
return (0);
}
static int
lf_getlock(struct lockf *state, struct lockf_entry *lock, struct flock *fl)
{
struct lockf_entry *block;
#ifdef LOCKF_DEBUG
if (lockf_debug & 1)
lf_print("lf_getlock", lock);
#endif
if ((block = lf_getblock(state, lock))) {
fl->l_type = block->lf_type;
fl->l_whence = SEEK_SET;
fl->l_start = block->lf_start;
if (block->lf_end == OFF_MAX)
fl->l_len = 0;
else
fl->l_len = block->lf_end - block->lf_start + 1;
fl->l_pid = block->lf_owner->lo_pid;
fl->l_sysid = block->lf_owner->lo_sysid;
} else {
fl->l_type = F_UNLCK;
}
return (0);
}
static int
lf_cancel(struct lockf *state, struct lockf_entry *lock, void *cookie)
{
struct lockf_entry *reallock;
LIST_FOREACH(reallock, &state->ls_pending, lf_link) {
if ((void *) reallock == cookie) {
if (!(reallock->lf_vnode == lock->lf_vnode
&& reallock->lf_start == lock->lf_start
&& reallock->lf_end == lock->lf_end)) {
return (ENOENT);
}
if (!reallock->lf_async_task) {
return (ENOENT);
}
lf_cancel_lock(state, reallock);
return (0);
}
}
return (ENOENT);
}
static struct lockf_entry *
lf_getblock(struct lockf *state, struct lockf_entry *lock)
{
struct lockf_entry *overlap;
LIST_FOREACH(overlap, &state->ls_active, lf_link) {
if (overlap->lf_start > lock->lf_end)
break;
if (!lf_blocks(lock, overlap))
continue;
return (overlap);
}
return (NOLOCKF);
}
static int
lf_findoverlap(struct lockf_entry **overlap, struct lockf_entry *lock, int type)
{
struct lockf_entry *lf;
off_t start, end;
int res;
if ((*overlap) == NOLOCKF) {
return (0);
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
lf_print("lf_findoverlap: looking for overlap in", lock);
#endif
start = lock->lf_start;
end = lock->lf_end;
res = 0;
while (*overlap) {
lf = *overlap;
if (lf->lf_start > end)
break;
if (((type & SELF) && lf->lf_owner != lock->lf_owner) ||
((type & OTHERS) && lf->lf_owner == lock->lf_owner)) {
*overlap = LIST_NEXT(lf, lf_link);
continue;
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
lf_print("\tchecking", lf);
#endif
if (start > lf->lf_end) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("no overlap\n");
#endif
*overlap = LIST_NEXT(lf, lf_link);
continue;
}
if (lf->lf_start == start && lf->lf_end == end) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap == lock\n");
#endif
res = 1;
break;
}
if (lf->lf_start <= start && lf->lf_end >= end) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap contains lock\n");
#endif
res = 2;
break;
}
if (start <= lf->lf_start && end >= lf->lf_end) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("lock contains overlap\n");
#endif
res = 3;
break;
}
if (lf->lf_start < start && lf->lf_end >= start) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap starts before lock\n");
#endif
res = 4;
break;
}
if (lf->lf_start > start && lf->lf_end > end) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 2)
printf("overlap ends after lock\n");
#endif
res = 5;
break;
}
panic("lf_findoverlap: default");
}
return (res);
}
static void
lf_split(struct lockf *state, struct lockf_entry *lock1,
struct lockf_entry *lock2, struct lockf_entry_list *granted)
{
struct lockf_entry *splitlock;
#ifdef LOCKF_DEBUG
if (lockf_debug & 2) {
lf_print("lf_split", lock1);
lf_print("splitting from", lock2);
}
#endif
if (lock1->lf_start == lock2->lf_start) {
lf_set_start(state, lock1, lock2->lf_end + 1, granted);
return;
}
if (lock1->lf_end == lock2->lf_end) {
lf_set_end(state, lock1, lock2->lf_start - 1, granted);
return;
}
splitlock = lf_alloc_lock(lock1->lf_owner);
memcpy(splitlock, lock1, sizeof *splitlock);
splitlock->lf_refs = 1;
if (splitlock->lf_flags & F_REMOTE)
vref(splitlock->lf_vnode);
splitlock->lf_start = lock2->lf_end + 1;
LIST_INIT(&splitlock->lf_outedges);
LIST_INIT(&splitlock->lf_inedges);
lf_add_incoming(state, splitlock);
lf_set_end(state, lock1, lock2->lf_start - 1, granted);
lf_insert_lock(state, splitlock);
}
struct lockdesc {
STAILQ_ENTRY(lockdesc) link;
struct vnode *vp;
struct flock fl;
};
STAILQ_HEAD(lockdesclist, lockdesc);
int
lf_iteratelocks_sysid(int sysid, lf_iterator *fn, void *arg)
{
struct lockf *ls;
struct lockf_entry *lf;
struct lockdesc *ldesc;
struct lockdesclist locks;
int error;
STAILQ_INIT(&locks);
sx_xlock(&lf_lock_states_lock);
LIST_FOREACH(ls, &lf_lock_states, ls_link) {
sx_xlock(&ls->ls_lock);
LIST_FOREACH(lf, &ls->ls_active, lf_link) {
if (lf->lf_owner->lo_sysid != sysid)
continue;
ldesc = malloc(sizeof(struct lockdesc), M_LOCKF,
M_WAITOK);
ldesc->vp = lf->lf_vnode;
vref(ldesc->vp);
ldesc->fl.l_start = lf->lf_start;
if (lf->lf_end == OFF_MAX)
ldesc->fl.l_len = 0;
else
ldesc->fl.l_len =
lf->lf_end - lf->lf_start + 1;
ldesc->fl.l_whence = SEEK_SET;
ldesc->fl.l_type = F_UNLCK;
ldesc->fl.l_pid = lf->lf_owner->lo_pid;
ldesc->fl.l_sysid = sysid;
STAILQ_INSERT_TAIL(&locks, ldesc, link);
}
sx_xunlock(&ls->ls_lock);
}
sx_xunlock(&lf_lock_states_lock);
error = 0;
while ((ldesc = STAILQ_FIRST(&locks)) != NULL) {
STAILQ_REMOVE_HEAD(&locks, link);
if (!error)
error = fn(ldesc->vp, &ldesc->fl, arg);
vrele(ldesc->vp);
free(ldesc, M_LOCKF);
}
return (error);
}
int
lf_iteratelocks_vnode(struct vnode *vp, lf_iterator *fn, void *arg)
{
struct lockf *ls;
struct lockf_entry *lf;
struct lockdesc *ldesc;
struct lockdesclist locks;
int error;
STAILQ_INIT(&locks);
VI_LOCK(vp);
ls = vp->v_lockf;
if (!ls) {
VI_UNLOCK(vp);
return (0);
}
MPASS(ls->ls_threads >= 0);
ls->ls_threads++;
VI_UNLOCK(vp);
sx_xlock(&ls->ls_lock);
LIST_FOREACH(lf, &ls->ls_active, lf_link) {
ldesc = malloc(sizeof(struct lockdesc), M_LOCKF,
M_WAITOK);
ldesc->vp = lf->lf_vnode;
vref(ldesc->vp);
ldesc->fl.l_start = lf->lf_start;
if (lf->lf_end == OFF_MAX)
ldesc->fl.l_len = 0;
else
ldesc->fl.l_len =
lf->lf_end - lf->lf_start + 1;
ldesc->fl.l_whence = SEEK_SET;
ldesc->fl.l_type = F_UNLCK;
ldesc->fl.l_pid = lf->lf_owner->lo_pid;
ldesc->fl.l_sysid = lf->lf_owner->lo_sysid;
STAILQ_INSERT_TAIL(&locks, ldesc, link);
}
sx_xunlock(&ls->ls_lock);
VI_LOCK(vp);
MPASS(ls->ls_threads > 0);
ls->ls_threads--;
wakeup(ls);
VI_UNLOCK(vp);
error = 0;
while ((ldesc = STAILQ_FIRST(&locks)) != NULL) {
STAILQ_REMOVE_HEAD(&locks, link);
if (!error)
error = fn(ldesc->vp, &ldesc->fl, arg);
vrele(ldesc->vp);
free(ldesc, M_LOCKF);
}
return (error);
}
static int
lf_clearremotesys_iterator(struct vnode *vp, struct flock *fl, void *arg)
{
VOP_ADVLOCK(vp, 0, F_UNLCK, fl, F_REMOTE);
return (0);
}
void
lf_clearremotesys(int sysid)
{
KASSERT(sysid != 0, ("Can't clear local locks with F_UNLCKSYS"));
lf_iteratelocks_sysid(sysid, lf_clearremotesys_iterator, NULL);
}
int
lf_countlocks(int sysid)
{
int i;
struct lock_owner *lo;
int count;
count = 0;
for (i = 0; i < LOCK_OWNER_HASH_SIZE; i++) {
sx_xlock(&lf_lock_owners[i].lock);
LIST_FOREACH(lo, &lf_lock_owners[i].list, lo_link)
if (lo->lo_sysid == sysid)
count += lo->lo_refs;
sx_xunlock(&lf_lock_owners[i].lock);
}
return (count);
}
#ifdef LOCKF_DEBUG
static int
graph_reaches(struct owner_vertex *x, struct owner_vertex *y,
struct owner_vertex_list *path)
{
struct owner_edge *e;
if (x == y) {
if (path)
TAILQ_INSERT_HEAD(path, x, v_link);
return 1;
}
LIST_FOREACH(e, &x->v_outedges, e_outlink) {
if (graph_reaches(e->e_to, y, path)) {
if (path)
TAILQ_INSERT_HEAD(path, x, v_link);
return 1;
}
}
return 0;
}
static void
graph_check(struct owner_graph *g, int checkorder)
{
int i, j;
for (i = 0; i < g->g_size; i++) {
if (!g->g_vertices[i]->v_owner)
continue;
KASSERT(g->g_vertices[i]->v_order == i,
("lock graph vertices disordered"));
if (checkorder) {
for (j = 0; j < i; j++) {
if (!g->g_vertices[j]->v_owner)
continue;
KASSERT(!graph_reaches(g->g_vertices[i],
g->g_vertices[j], NULL),
("lock graph vertices disordered"));
}
}
}
}
static void
graph_print_vertices(struct owner_vertex_list *set)
{
struct owner_vertex *v;
printf("{ ");
TAILQ_FOREACH(v, set, v_link) {
printf("%d:", v->v_order);
lf_print_owner(v->v_owner);
if (TAILQ_NEXT(v, v_link))
printf(", ");
}
printf(" }\n");
}
#endif
static int
graph_delta_forward(struct owner_graph *g, struct owner_vertex *x,
struct owner_vertex *y, struct owner_vertex_list *delta)
{
uint32_t gen;
struct owner_vertex *v;
struct owner_edge *e;
int n;
TAILQ_INIT(delta);
TAILQ_INSERT_TAIL(delta, y, v_link);
v = y;
n = 1;
gen = g->g_gen;
while (v) {
LIST_FOREACH(e, &v->v_outedges, e_outlink) {
if (e->e_to == x)
return -1;
if (e->e_to->v_order < x->v_order
&& e->e_to->v_gen != gen) {
e->e_to->v_gen = gen;
TAILQ_INSERT_TAIL(delta, e->e_to, v_link);
n++;
}
}
v = TAILQ_NEXT(v, v_link);
}
return (n);
}
static int
graph_delta_backward(struct owner_graph *g, struct owner_vertex *x,
struct owner_vertex *y, struct owner_vertex_list *delta)
{
uint32_t gen;
struct owner_vertex *v;
struct owner_edge *e;
int n;
TAILQ_INIT(delta);
TAILQ_INSERT_TAIL(delta, x, v_link);
v = x;
n = 1;
gen = g->g_gen;
while (v) {
LIST_FOREACH(e, &v->v_inedges, e_inlink) {
if (e->e_from->v_order > y->v_order
&& e->e_from->v_gen != gen) {
e->e_from->v_gen = gen;
TAILQ_INSERT_HEAD(delta, e->e_from, v_link);
n++;
}
}
v = TAILQ_PREV(v, owner_vertex_list, v_link);
}
return (n);
}
static int
graph_add_indices(int *indices, int n, struct owner_vertex_list *set)
{
struct owner_vertex *v;
int i, j;
TAILQ_FOREACH(v, set, v_link) {
for (i = n;
i > 0 && indices[i - 1] > v->v_order; i--)
;
for (j = n - 1; j >= i; j--)
indices[j + 1] = indices[j];
indices[i] = v->v_order;
n++;
}
return (n);
}
static int
graph_assign_indices(struct owner_graph *g, int *indices, int nextunused,
struct owner_vertex_list *set)
{
struct owner_vertex *v, *vlowest;
while (!TAILQ_EMPTY(set)) {
vlowest = NULL;
TAILQ_FOREACH(v, set, v_link) {
if (!vlowest || v->v_order < vlowest->v_order)
vlowest = v;
}
TAILQ_REMOVE(set, vlowest, v_link);
vlowest->v_order = indices[nextunused];
g->g_vertices[vlowest->v_order] = vlowest;
nextunused++;
}
return (nextunused);
}
static int
graph_add_edge(struct owner_graph *g, struct owner_vertex *x,
struct owner_vertex *y)
{
struct owner_edge *e;
struct owner_vertex_list deltaF, deltaB;
int nF, n, vi, i;
int *indices;
int nB __unused;
sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
LIST_FOREACH(e, &x->v_outedges, e_outlink) {
if (e->e_to == y) {
e->e_refs++;
return (0);
}
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
printf("adding edge %d:", x->v_order);
lf_print_owner(x->v_owner);
printf(" -> %d:", y->v_order);
lf_print_owner(y->v_owner);
printf("\n");
}
#endif
if (y->v_order < x->v_order) {
g->g_gen++;
if (g->g_gen == 0) {
for (vi = 0; vi < g->g_size; vi++) {
g->g_vertices[vi]->v_gen = 0;
}
g->g_gen++;
}
nF = graph_delta_forward(g, x, y, &deltaF);
if (nF < 0) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
struct owner_vertex_list path;
printf("deadlock: ");
TAILQ_INIT(&path);
graph_reaches(y, x, &path);
graph_print_vertices(&path);
}
#endif
return (EDEADLK);
}
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
printf("re-ordering graph vertices\n");
printf("deltaF = ");
graph_print_vertices(&deltaF);
}
#endif
nB = graph_delta_backward(g, x, y, &deltaB);
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
printf("deltaB = ");
graph_print_vertices(&deltaB);
}
#endif
indices = g->g_indexbuf;
n = graph_add_indices(indices, 0, &deltaF);
graph_add_indices(indices, n, &deltaB);
i = graph_assign_indices(g, indices, 0, &deltaB);
graph_assign_indices(g, indices, i, &deltaF);
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
struct owner_vertex_list set;
TAILQ_INIT(&set);
for (i = 0; i < nB + nF; i++)
TAILQ_INSERT_TAIL(&set,
g->g_vertices[indices[i]], v_link);
printf("new ordering = ");
graph_print_vertices(&set);
}
#endif
}
KASSERT(x->v_order < y->v_order, ("Failed to re-order graph"));
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
graph_check(g, TRUE);
}
#endif
e = malloc(sizeof(struct owner_edge), M_LOCKF, M_WAITOK);
LIST_INSERT_HEAD(&x->v_outedges, e, e_outlink);
LIST_INSERT_HEAD(&y->v_inedges, e, e_inlink);
e->e_refs = 1;
e->e_from = x;
e->e_to = y;
return (0);
}
static void
graph_remove_edge(struct owner_graph *g, struct owner_vertex *x,
struct owner_vertex *y)
{
struct owner_edge *e;
sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
LIST_FOREACH(e, &x->v_outedges, e_outlink) {
if (e->e_to == y)
break;
}
KASSERT(e, ("Removing non-existent edge from deadlock graph"));
e->e_refs--;
if (e->e_refs == 0) {
#ifdef LOCKF_DEBUG
if (lockf_debug & 8) {
printf("removing edge %d:", x->v_order);
lf_print_owner(x->v_owner);
printf(" -> %d:", y->v_order);
lf_print_owner(y->v_owner);
printf("\n");
}
#endif
LIST_REMOVE(e, e_outlink);
LIST_REMOVE(e, e_inlink);
free(e, M_LOCKF);
}
}
static struct owner_vertex *
graph_alloc_vertex(struct owner_graph *g, struct lock_owner *lo)
{
struct owner_vertex *v;
sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
v = malloc(sizeof(struct owner_vertex), M_LOCKF, M_WAITOK);
if (g->g_size == g->g_space) {
g->g_vertices = realloc(g->g_vertices,
2 * g->g_space * sizeof(struct owner_vertex *),
M_LOCKF, M_WAITOK);
free(g->g_indexbuf, M_LOCKF);
g->g_indexbuf = malloc(2 * g->g_space * sizeof(int),
M_LOCKF, M_WAITOK);
g->g_space = 2 * g->g_space;
}
v->v_order = g->g_size;
v->v_gen = g->g_gen;
g->g_vertices[g->g_size] = v;
g->g_size++;
LIST_INIT(&v->v_outedges);
LIST_INIT(&v->v_inedges);
v->v_owner = lo;
return (v);
}
static void
graph_free_vertex(struct owner_graph *g, struct owner_vertex *v)
{
struct owner_vertex *w;
int i;
sx_assert(&lf_owner_graph_lock, SX_XLOCKED);
KASSERT(LIST_EMPTY(&v->v_outedges), ("Freeing vertex with edges"));
KASSERT(LIST_EMPTY(&v->v_inedges), ("Freeing vertex with edges"));
for (i = v->v_order + 1; i < g->g_size; i++) {
w = g->g_vertices[i];
w->v_order--;
g->g_vertices[i - 1] = w;
}
g->g_size--;
free(v, M_LOCKF);
}
static struct owner_graph *
graph_init(struct owner_graph *g)
{
g->g_vertices = malloc(10 * sizeof(struct owner_vertex *),
M_LOCKF, M_WAITOK);
g->g_size = 0;
g->g_space = 10;
g->g_indexbuf = malloc(g->g_space * sizeof(int), M_LOCKF, M_WAITOK);
g->g_gen = 0;
return (g);
}
struct kinfo_lockf_linked {
struct kinfo_lockf kl;
struct vnode *vp;
STAILQ_ENTRY(kinfo_lockf_linked) link;
};
int
vfs_report_lockf(struct mount *mp, struct sbuf *sb)
{
struct lockf *ls;
struct lockf_entry *lf;
struct kinfo_lockf_linked *klf;
struct vnode *vp;
struct ucred *ucred;
char *fullpath, *freepath;
struct stat stt;
STAILQ_HEAD(, kinfo_lockf_linked) locks;
int error, gerror;
STAILQ_INIT(&locks);
sx_slock(&lf_lock_states_lock);
LIST_FOREACH(ls, &lf_lock_states, ls_link) {
sx_slock(&ls->ls_lock);
LIST_FOREACH(lf, &ls->ls_active, lf_link) {
vp = lf->lf_vnode;
if (VN_IS_DOOMED(vp) || vp->v_mount != mp)
continue;
vhold(vp);
klf = malloc(sizeof(struct kinfo_lockf_linked),
M_LOCKF, M_WAITOK | M_ZERO);
klf->vp = vp;
klf->kl.kl_structsize = sizeof(struct kinfo_lockf);
klf->kl.kl_start = lf->lf_start;
klf->kl.kl_len = lf->lf_end == OFF_MAX ? 0 :
lf->lf_end - lf->lf_start + 1;
klf->kl.kl_rw = lf->lf_type == F_RDLCK ?
KLOCKF_RW_READ : KLOCKF_RW_WRITE;
if (lf->lf_owner->lo_sysid != 0) {
klf->kl.kl_pid = lf->lf_owner->lo_pid;
klf->kl.kl_sysid = lf->lf_owner->lo_sysid;
klf->kl.kl_type = KLOCKF_TYPE_REMOTE;
} else if (lf->lf_owner->lo_pid == -1) {
klf->kl.kl_pid = -1;
klf->kl.kl_sysid = 0;
klf->kl.kl_type = KLOCKF_TYPE_FLOCK;
} else {
klf->kl.kl_pid = lf->lf_owner->lo_pid;
klf->kl.kl_sysid = 0;
klf->kl.kl_type = KLOCKF_TYPE_PID;
}
STAILQ_INSERT_TAIL(&locks, klf, link);
}
sx_sunlock(&ls->ls_lock);
}
sx_sunlock(&lf_lock_states_lock);
gerror = 0;
ucred = curthread->td_ucred;
while ((klf = STAILQ_FIRST(&locks)) != NULL) {
STAILQ_REMOVE_HEAD(&locks, link);
vp = klf->vp;
if (gerror == 0 && vn_lock(vp, LK_SHARED) == 0) {
error = prison_canseemount(ucred, vp->v_mount);
if (error == 0)
error = VOP_STAT(vp, &stt, ucred, NOCRED);
VOP_UNLOCK(vp);
if (error == 0) {
klf->kl.kl_file_fsid = stt.st_dev;
klf->kl.kl_file_rdev = stt.st_rdev;
klf->kl.kl_file_fileid = stt.st_ino;
freepath = NULL;
fullpath = "-";
error = vn_fullpath(vp, &fullpath, &freepath);
if (error == 0)
strlcpy(klf->kl.kl_path, fullpath,
sizeof(klf->kl.kl_path));
free(freepath, M_TEMP);
if (sbuf_bcat(sb, &klf->kl,
klf->kl.kl_structsize) != 0) {
gerror = sbuf_error(sb);
}
}
}
vdrop(vp);
free(klf, M_LOCKF);
}
return (gerror);
}
static int
sysctl_kern_lockf_run(struct sbuf *sb)
{
struct mount *mp;
int error;
error = 0;
mtx_lock(&mountlist_mtx);
TAILQ_FOREACH(mp, &mountlist, mnt_list) {
error = vfs_busy(mp, MBF_MNTLSTLOCK);
if (error != 0)
continue;
error = mp->mnt_op->vfs_report_lockf(mp, sb);
mtx_lock(&mountlist_mtx);
vfs_unbusy(mp);
if (error != 0)
break;
}
mtx_unlock(&mountlist_mtx);
return (error);
}
static int
sysctl_kern_lockf(SYSCTL_HANDLER_ARGS)
{
struct sbuf sb;
int error, error2;
sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_lockf) * 5, req);
sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
error = sysctl_kern_lockf_run(&sb);
error2 = sbuf_finish(&sb);
sbuf_delete(&sb);
return (error != 0 ? error : error2);
}
SYSCTL_PROC(_kern, KERN_LOCKF, lockf,
CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
0, 0, sysctl_kern_lockf, "S,lockf",
"Advisory locks table");
#ifdef LOCKF_DEBUG
static void
lf_print_owner(struct lock_owner *lo)
{
if (lo->lo_flags & F_REMOTE) {
printf("remote pid %d, system %d",
lo->lo_pid, lo->lo_sysid);
} else if (lo->lo_flags & F_FLOCK) {
printf("file %p", lo->lo_id);
} else {
printf("local pid %d", lo->lo_pid);
}
}
static void
lf_print(char *tag, struct lockf_entry *lock)
{
printf("%s: lock %p for ", tag, (void *)lock);
lf_print_owner(lock->lf_owner);
printf("\nvnode %p", lock->lf_vnode);
VOP_PRINT(lock->lf_vnode);
printf(" %s, start %jd, end ",
lock->lf_type == F_RDLCK ? "shared" :
lock->lf_type == F_WRLCK ? "exclusive" :
lock->lf_type == F_UNLCK ? "unlock" : "unknown",
(intmax_t)lock->lf_start);
if (lock->lf_end == OFF_MAX)
printf("EOF");
else
printf("%jd", (intmax_t)lock->lf_end);
if (!LIST_EMPTY(&lock->lf_outedges))
printf(" block %p\n",
(void *)LIST_FIRST(&lock->lf_outedges)->le_to);
else
printf("\n");
}
static void
lf_printlist(char *tag, struct lockf_entry *lock)
{
struct lockf_entry *lf, *blk;
struct lockf_edge *e;
printf("%s: Lock list for vnode %p:\n", tag, lock->lf_vnode);
LIST_FOREACH(lf, &lock->lf_vnode->v_lockf->ls_active, lf_link) {
printf("\tlock %p for ",(void *)lf);
lf_print_owner(lock->lf_owner);
printf(", %s, start %jd, end %jd",
lf->lf_type == F_RDLCK ? "shared" :
lf->lf_type == F_WRLCK ? "exclusive" :
lf->lf_type == F_UNLCK ? "unlock" :
"unknown", (intmax_t)lf->lf_start, (intmax_t)lf->lf_end);
LIST_FOREACH(e, &lf->lf_outedges, le_outlink) {
blk = e->le_to;
printf("\n\t\tlock request %p for ", (void *)blk);
lf_print_owner(blk->lf_owner);
printf(", %s, start %jd, end %jd",
blk->lf_type == F_RDLCK ? "shared" :
blk->lf_type == F_WRLCK ? "exclusive" :
blk->lf_type == F_UNLCK ? "unlock" :
"unknown", (intmax_t)blk->lf_start,
(intmax_t)blk->lf_end);
if (!LIST_EMPTY(&blk->lf_inedges))
panic("lf_printlist: bad list");
}
printf("\n");
}
}
#endif