1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * by Cluster File Systems, Inc.
13 #define DEBUG_SUBSYSTEM S_LDLM
15 #include <linux/slab.h>
16 #include <linux/lustre_dlm.h>
18 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
20 LIST_HEAD(ldlm_namespaces);
21 spinlock_t ldlm_spinlock;
23 struct ldlm_namespace *ldlm_namespace_find(__u32 id)
25 struct list_head *tmp;
26 struct ldlm_namespace *res;
29 list_for_each(tmp, &ldlm_namespaces) {
30 struct ldlm_namespace *chk;
31 chk = list_entry(tmp, struct ldlm_namespace, ns_link);
33 if ( chk->ns_id == id ) {
42 /* this must be called with ldlm_lock() held */
43 static void res_hash_init(struct ldlm_namespace *ns)
45 struct list_head *res_hash;
46 struct list_head *bucket;
48 if (ns->ns_hash != NULL)
51 /* FIXME: this memory appears to be leaked */
52 OBD_ALLOC(res_hash, sizeof(*res_hash) * RES_HASH_SIZE);
56 for (bucket = res_hash + RES_HASH_SIZE - 1; bucket >= res_hash;
58 INIT_LIST_HEAD(bucket);
60 ns->ns_hash = res_hash;
63 ldlm_error_t ldlm_namespace_new(struct obd_device *obddev, __u32 id,
64 struct ldlm_namespace **ns_out)
66 struct ldlm_namespace *ns;
68 if (ldlm_namespace_find(id))
69 return -ELDLM_NAMESPACE_EXISTS;
71 OBD_ALLOC(ns, sizeof(*ns));
76 ns->ns_obddev = obddev;
77 INIT_LIST_HEAD(&ns->ns_root_list);
79 list_add(&ns->ns_link, &ldlm_namespaces);
82 atomic_set(&ns->ns_refcount, 0);
88 int ldlm_namespace_free(struct ldlm_namespace *ns)
90 if (atomic_read(&ns->ns_refcount))
93 list_del(&ns->ns_link);
94 OBD_FREE(ns->ns_hash, sizeof(struct list_head) * RES_HASH_SIZE);
95 OBD_FREE(ns, sizeof(*ns));
100 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
105 for (i = 0; i < RES_NAME_SIZE; i++)
108 hash += (__u32)((unsigned long)parent >> 4);
110 return (hash & RES_HASH_MASK);
113 static struct ldlm_resource *ldlm_resource_new(void)
115 struct ldlm_resource *res;
117 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
120 memset(res, 0, sizeof(*res));
122 INIT_LIST_HEAD(&res->lr_children);
123 INIT_LIST_HEAD(&res->lr_childof);
124 INIT_LIST_HEAD(&res->lr_granted);
125 INIT_LIST_HEAD(&res->lr_converting);
126 INIT_LIST_HEAD(&res->lr_waiting);
128 res->lr_lock = SPIN_LOCK_UNLOCKED;
130 atomic_set(&res->lr_refcount, 1);
135 /* ldlm_lock() must be taken before calling resource_add */
136 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
137 struct ldlm_resource *parent,
138 __u64 *name, __u32 type)
140 struct list_head *bucket;
141 struct ldlm_resource *res;
143 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
145 res = ldlm_resource_new();
149 memcpy(res->lr_name, name, sizeof(res->lr_name));
150 res->lr_namespace = ns;
151 if (type < 0 || type > LDLM_MAX_TYPE)
155 res->lr_most_restr = LCK_NL;
156 list_add(&res->lr_hash, bucket);
157 atomic_inc(&ns->ns_refcount);
158 if (parent == NULL) {
159 res->lr_parent = res;
160 list_add(&res->lr_rootlink, &ns->ns_root_list);
162 res->lr_parent = parent;
163 list_add(&res->lr_childof, &parent->lr_children);
169 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
170 struct ldlm_resource *parent,
171 __u64 *name, __u32 type, int create)
173 struct list_head *bucket;
174 struct list_head *tmp = bucket;
175 struct ldlm_resource *res;
179 if (ns->ns_hash == NULL)
181 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
184 list_for_each(tmp, bucket) {
185 struct ldlm_resource *chk;
186 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
188 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
190 atomic_inc(&res->lr_refcount);
195 if (res == NULL && create)
196 res = ldlm_resource_add(ns, parent, name, type);
201 int ldlm_resource_put(struct ldlm_resource *res)
205 if (atomic_read(&res->lr_refcount) <= 0)
208 if (atomic_dec_and_test(&res->lr_refcount)) {
209 if (!list_empty(&res->lr_granted))
212 if (!list_empty(&res->lr_converting))
215 if (!list_empty(&res->lr_waiting))
218 atomic_dec(&res->lr_namespace->ns_refcount);
219 list_del(&res->lr_hash);
220 list_del(&res->lr_rootlink);
221 list_del(&res->lr_childof);
223 kmem_cache_free(ldlm_resource_slab, res);
230 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
231 struct ldlm_lock *lock)
233 list_add(&lock->l_res_link, head);
234 atomic_inc(&res->lr_refcount);
237 void ldlm_resource_del_lock(struct ldlm_lock *lock)
239 list_del(&lock->l_res_link);
240 atomic_dec(&lock->l_resource->lr_refcount);
243 int ldlm_get_resource_handle(struct ldlm_resource *res, struct ldlm_handle *h)
249 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
251 desc->lr_ns_id = res->lr_namespace->ns_id;
252 desc->lr_type = res->lr_type;
253 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
254 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
257 void ldlm_resource_dump(struct ldlm_resource *res)
259 struct list_head *tmp;
262 if (RES_NAME_SIZE != 3)
265 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
266 res->lr_name[0], res->lr_name[1], res->lr_name[2]);
268 CDEBUG(D_OTHER, "--- Resource: %p (%s)\n", res, name);
269 CDEBUG(D_OTHER, "Namespace: %p (%u)\n", res->lr_namespace,
270 res->lr_namespace->ns_id);
271 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
273 CDEBUG(D_OTHER, "Granted locks:\n");
274 list_for_each(tmp, &res->lr_granted) {
275 struct ldlm_lock *lock;
276 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
277 ldlm_lock_dump(lock);
280 CDEBUG(D_OTHER, "Converting locks:\n");
281 list_for_each(tmp, &res->lr_converting) {
282 struct ldlm_lock *lock;
283 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
284 ldlm_lock_dump(lock);
287 CDEBUG(D_OTHER, "Waiting locks:\n");
288 list_for_each(tmp, &res->lr_waiting) {
289 struct ldlm_lock *lock;
290 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
291 ldlm_lock_dump(lock);