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.
12 #define DEBUG_SUBSYSTEM S_LDLM
14 #include <linux/lustre_dlm.h>
16 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
18 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
19 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
20 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
22 int ldlm_proc_setup(struct obd_device *obd)
26 if (obd->obd_proc_entry == NULL)
29 ldlm_ns_proc_dir = proc_mkdir("namespaces", obd->obd_proc_entry);
30 if (ldlm_ns_proc_dir == NULL) {
31 CERROR("Couldn't create /proc/lustre/ldlm/namespaces\n");
37 void ldlm_proc_cleanup(struct obd_device *obd)
39 proc_lustre_remove_obd_entry("namespaces", obd);
42 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
44 struct ldlm_namespace *ns = NULL;
45 struct list_head *bucket;
47 OBD_ALLOC(ns, sizeof(*ns));
53 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
58 obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
60 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
65 strcpy(ns->ns_name, name);
67 ptlrpc_init_client(NULL, NULL, LDLM_REQUEST_PORTAL, LDLM_REPLY_PORTAL,
70 INIT_LIST_HEAD(&ns->ns_root_list);
71 l_lock_init(&ns->ns_lock);
73 ns->ns_client = client;
75 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
77 INIT_LIST_HEAD(bucket);
79 spin_lock(&ldlm_namespace_lock);
80 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
81 ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
82 if (ns->ns_proc_dir == NULL)
83 CERROR("Unable to create proc directory for namespace.\n");
84 spin_unlock(&ldlm_namespace_lock);
89 if (ns && ns->ns_hash) {
91 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
93 if (ns && ns->ns_name)
94 OBD_FREE(ns->ns_name, strlen(name) + 1);
96 OBD_FREE(ns, sizeof(*ns));
100 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
102 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q)
104 struct list_head *tmp, *pos;
105 int rc = 0, client = res->lr_namespace->ns_client;
108 list_for_each_safe(tmp, pos, q) {
109 struct ldlm_lock *lock;
110 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
114 struct lustre_handle lockh;
115 ldlm_lock2handle(lock, &lockh);
116 /* can we get away without a connh here? */
117 rc = ldlm_cli_cancel(&lockh);
118 if (rc != ELDLM_OK) {
119 /* It failed remotely, but we'll force it to
120 * cleanup locally. */
121 CERROR("ldlm_cli_cancel: %d\n", rc);
122 ldlm_lock_cancel(lock);
125 LDLM_DEBUG(lock, "Freeing a lock still held by a "
128 ldlm_resource_unlink_lock(lock);
129 ldlm_lock_destroy(lock);
135 int ldlm_namespace_free(struct ldlm_namespace *ns)
137 struct list_head *tmp, *pos;
143 spin_lock(&ldlm_namespace_lock);
144 list_del(&ns->ns_list_chain);
145 remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
146 spin_unlock(&ldlm_namespace_lock);
148 l_lock(&ns->ns_lock);
150 for (i = 0; i < RES_HASH_SIZE; i++) {
151 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
152 struct ldlm_resource *res;
153 res = list_entry(tmp, struct ldlm_resource, lr_hash);
154 ldlm_resource_getref(res);
156 cleanup_resource(res, &res->lr_granted);
157 cleanup_resource(res, &res->lr_converting);
158 cleanup_resource(res, &res->lr_waiting);
160 if (!ldlm_resource_put(res)) {
161 CERROR("Resource refcount nonzero (%d) after "
162 "lock cleanup; forcing cleanup.\n",
163 atomic_read(&res->lr_refcount));
164 ldlm_resource_dump(res);
165 atomic_set(&res->lr_refcount, 1);
166 ldlm_resource_put(res);
171 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
172 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
173 ptlrpc_cleanup_client(&ns->ns_rpc_client);
174 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
175 OBD_FREE(ns, sizeof(*ns));
180 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
185 for (i = 0; i < RES_NAME_SIZE; i++)
188 hash += (__u32)((unsigned long)parent >> 4);
190 return (hash & RES_HASH_MASK);
193 static struct ldlm_resource *ldlm_resource_new(void)
195 struct ldlm_resource *res;
197 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
202 memset(res, 0, sizeof(*res));
204 INIT_LIST_HEAD(&res->lr_children);
205 INIT_LIST_HEAD(&res->lr_childof);
206 INIT_LIST_HEAD(&res->lr_granted);
207 INIT_LIST_HEAD(&res->lr_converting);
208 INIT_LIST_HEAD(&res->lr_waiting);
210 atomic_set(&res->lr_refcount, 1);
215 /* Args: locked namespace
216 * Returns: newly-allocated, referenced, unlocked resource */
217 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
218 struct ldlm_resource *parent,
219 __u64 *name, __u32 type)
221 struct list_head *bucket;
222 struct ldlm_resource *res;
225 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
230 res = ldlm_resource_new();
236 memcpy(res->lr_name, name, sizeof(res->lr_name));
237 res->lr_namespace = ns;
241 res->lr_most_restr = LCK_NL;
243 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
244 list_add(&res->lr_hash, bucket);
247 list_add(&res->lr_childof, &ns->ns_root_list);
249 res->lr_parent = parent;
250 list_add(&res->lr_childof, &parent->lr_children);
256 /* Args: unlocked namespace
257 * Locks: takes and releases ns->ns_lock and res->lr_lock
258 * Returns: referenced, unlocked ldlm_resource or NULL */
259 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
260 struct ldlm_resource *parent,
261 __u64 *name, __u32 type, int create)
263 struct list_head *bucket;
264 struct list_head *tmp = bucket;
265 struct ldlm_resource *res = NULL;
268 if (ns == NULL || ns->ns_hash == NULL) {
273 l_lock(&ns->ns_lock);
274 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
276 list_for_each(tmp, bucket) {
277 struct ldlm_resource *chk;
278 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
280 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
282 atomic_inc(&res->lr_refcount);
288 if (res == NULL && create)
289 res = ldlm_resource_add(ns, parent, name, type);
290 l_unlock(&ns->ns_lock);
295 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
297 atomic_inc(&res->lr_refcount);
301 /* Returns 1 if the resource was freed, 0 if it remains. */
302 int ldlm_resource_put(struct ldlm_resource *res)
306 if (atomic_dec_and_test(&res->lr_refcount)) {
307 struct ldlm_namespace *ns = res->lr_namespace;
310 l_lock(&ns->ns_lock);
312 if (atomic_read(&res->lr_refcount) != 0) {
313 /* We lost the race. */
314 l_unlock(&ns->ns_lock);
318 if (!list_empty(&res->lr_granted))
321 if (!list_empty(&res->lr_converting))
324 if (!list_empty(&res->lr_waiting))
327 if (!list_empty(&res->lr_children))
331 list_del(&res->lr_hash);
332 list_del(&res->lr_childof);
334 kmem_cache_free(ldlm_resource_slab, res);
335 l_unlock(&ns->ns_lock);
340 if (atomic_read(&res->lr_refcount) < 0)
347 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
348 struct ldlm_lock *lock)
350 l_lock(&res->lr_namespace->ns_lock);
352 ldlm_resource_dump(res);
353 ldlm_lock_dump(lock);
355 if (!list_empty(&lock->l_res_link))
358 list_add(&lock->l_res_link, head);
359 l_unlock(&res->lr_namespace->ns_lock);
362 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
364 l_lock(&lock->l_resource->lr_namespace->ns_lock);
365 list_del_init(&lock->l_res_link);
366 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
369 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
371 desc->lr_type = res->lr_type;
372 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
373 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
376 void ldlm_dump_all_namespaces(void)
378 struct list_head *tmp;
380 spin_lock(&ldlm_namespace_lock);
382 list_for_each(tmp, &ldlm_namespace_list) {
383 struct ldlm_namespace *ns;
384 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
385 ldlm_namespace_dump(ns);
388 spin_unlock(&ldlm_namespace_lock);
391 void ldlm_namespace_dump(struct ldlm_namespace *ns)
393 struct list_head *tmp;
395 l_lock(&ns->ns_lock);
396 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
397 ns->ns_refcount, ns->ns_client);
399 list_for_each(tmp, &ns->ns_root_list) {
400 struct ldlm_resource *res;
401 res = list_entry(tmp, struct ldlm_resource, lr_childof);
403 /* Once we have resources with children, this should really dump
404 * them recursively. */
405 ldlm_resource_dump(res);
407 l_unlock(&ns->ns_lock);
410 void ldlm_resource_dump(struct ldlm_resource *res)
412 struct list_head *tmp;
415 if (RES_NAME_SIZE != 3)
418 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
419 (unsigned long long)res->lr_name[0],
420 (unsigned long long)res->lr_name[1],
421 (unsigned long long)res->lr_name[2]);
423 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
424 atomic_read(&res->lr_refcount));
425 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
426 res->lr_namespace->ns_name);
427 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
429 CDEBUG(D_OTHER, "Granted locks:\n");
430 list_for_each(tmp, &res->lr_granted) {
431 struct ldlm_lock *lock;
432 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
433 ldlm_lock_dump(lock);
436 CDEBUG(D_OTHER, "Converting locks:\n");
437 list_for_each(tmp, &res->lr_converting) {
438 struct ldlm_lock *lock;
439 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
440 ldlm_lock_dump(lock);
443 CDEBUG(D_OTHER, "Waiting locks:\n");
444 list_for_each(tmp, &res->lr_waiting) {
445 struct ldlm_lock *lock;
446 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
447 ldlm_lock_dump(lock);