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>
15 #include <linux/obd_class.h>
17 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
19 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
20 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
21 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
23 int ldlm_proc_setup(struct obd_device *obd)
27 if (obd->obd_proc_entry == NULL)
30 ldlm_ns_proc_dir = proc_mkdir("namespaces", obd->obd_proc_entry);
31 if (ldlm_ns_proc_dir == NULL) {
32 CERROR("Couldn't create /proc/lustre/ldlm/namespaces\n");
38 void ldlm_proc_cleanup(struct obd_device *obd)
40 proc_lustre_remove_obd_entry("namespaces", obd);
43 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
45 struct ldlm_namespace *ns = NULL;
46 struct list_head *bucket;
48 OBD_ALLOC(ns, sizeof(*ns));
54 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
59 obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
61 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
66 strcpy(ns->ns_name, name);
68 INIT_LIST_HEAD(&ns->ns_root_list);
69 l_lock_init(&ns->ns_lock);
71 ns->ns_client = client;
73 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
75 INIT_LIST_HEAD(bucket);
77 spin_lock(&ldlm_namespace_lock);
78 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
79 ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
80 if (ns->ns_proc_dir == NULL)
81 CERROR("Unable to create proc directory for namespace.\n");
82 spin_unlock(&ldlm_namespace_lock);
87 if (ns && ns->ns_hash) {
89 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
91 if (ns && ns->ns_name)
92 OBD_FREE(ns->ns_name, strlen(name) + 1);
94 OBD_FREE(ns, sizeof(*ns));
98 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
100 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q)
102 struct list_head *tmp, *pos;
103 int rc = 0, client = res->lr_namespace->ns_client;
106 list_for_each_safe(tmp, pos, q) {
107 struct ldlm_lock *lock;
108 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
112 struct lustre_handle lockh;
113 ldlm_lock2handle(lock, &lockh);
114 /* can we get away without a connh here? */
115 rc = ldlm_cli_cancel(&lockh);
116 if (rc != ELDLM_OK) {
117 /* It failed remotely, but we'll force it to
118 * cleanup locally. */
119 CERROR("ldlm_cli_cancel: %d\n", rc);
120 ldlm_lock_cancel(lock);
123 LDLM_DEBUG(lock, "Freeing a lock still held by a "
126 ldlm_resource_unlink_lock(lock);
127 ldlm_lock_destroy(lock);
133 int ldlm_namespace_free(struct ldlm_namespace *ns)
135 struct list_head *tmp, *pos;
141 spin_lock(&ldlm_namespace_lock);
142 list_del(&ns->ns_list_chain);
143 remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
144 spin_unlock(&ldlm_namespace_lock);
146 l_lock(&ns->ns_lock);
148 for (i = 0; i < RES_HASH_SIZE; i++) {
149 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
150 struct ldlm_resource *res;
151 res = list_entry(tmp, struct ldlm_resource, lr_hash);
152 ldlm_resource_getref(res);
154 cleanup_resource(res, &res->lr_granted);
155 cleanup_resource(res, &res->lr_converting);
156 cleanup_resource(res, &res->lr_waiting);
158 if (!ldlm_resource_put(res)) {
159 CERROR("Resource refcount nonzero (%d) after "
160 "lock cleanup; forcing cleanup.\n",
161 atomic_read(&res->lr_refcount));
162 ldlm_resource_dump(res);
163 atomic_set(&res->lr_refcount, 1);
164 ldlm_resource_put(res);
169 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
170 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
171 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
172 OBD_FREE(ns, sizeof(*ns));
177 int ldlm_client_free(struct obd_export *exp)
179 struct ldlm_export_data *led = &exp->exp_ldlm_data;
180 ptlrpc_cleanup_client(&led->led_import);
184 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
189 for (i = 0; i < RES_NAME_SIZE; i++)
192 hash += (__u32)((unsigned long)parent >> 4);
194 return (hash & RES_HASH_MASK);
197 static struct ldlm_resource *ldlm_resource_new(void)
199 struct ldlm_resource *res;
201 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
206 memset(res, 0, sizeof(*res));
208 INIT_LIST_HEAD(&res->lr_children);
209 INIT_LIST_HEAD(&res->lr_childof);
210 INIT_LIST_HEAD(&res->lr_granted);
211 INIT_LIST_HEAD(&res->lr_converting);
212 INIT_LIST_HEAD(&res->lr_waiting);
214 atomic_set(&res->lr_refcount, 1);
219 /* Args: locked namespace
220 * Returns: newly-allocated, referenced, unlocked resource */
221 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
222 struct ldlm_resource *parent,
223 __u64 *name, __u32 type)
225 struct list_head *bucket;
226 struct ldlm_resource *res;
229 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
234 res = ldlm_resource_new();
240 memcpy(res->lr_name, name, sizeof(res->lr_name));
241 res->lr_namespace = ns;
245 res->lr_most_restr = LCK_NL;
247 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
248 list_add(&res->lr_hash, bucket);
251 list_add(&res->lr_childof, &ns->ns_root_list);
253 res->lr_parent = parent;
254 list_add(&res->lr_childof, &parent->lr_children);
260 /* Args: unlocked namespace
261 * Locks: takes and releases ns->ns_lock and res->lr_lock
262 * Returns: referenced, unlocked ldlm_resource or NULL */
263 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
264 struct ldlm_resource *parent,
265 __u64 *name, __u32 type, int create)
267 struct list_head *bucket;
268 struct list_head *tmp = bucket;
269 struct ldlm_resource *res = NULL;
272 if (ns == NULL || ns->ns_hash == NULL) {
277 l_lock(&ns->ns_lock);
278 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
280 list_for_each(tmp, bucket) {
281 struct ldlm_resource *chk;
282 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
284 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
286 atomic_inc(&res->lr_refcount);
292 if (res == NULL && create)
293 res = ldlm_resource_add(ns, parent, name, type);
294 l_unlock(&ns->ns_lock);
299 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
301 atomic_inc(&res->lr_refcount);
305 /* Returns 1 if the resource was freed, 0 if it remains. */
306 int ldlm_resource_put(struct ldlm_resource *res)
310 if (atomic_dec_and_test(&res->lr_refcount)) {
311 struct ldlm_namespace *ns = res->lr_namespace;
314 l_lock(&ns->ns_lock);
316 if (atomic_read(&res->lr_refcount) != 0) {
317 /* We lost the race. */
318 l_unlock(&ns->ns_lock);
322 if (!list_empty(&res->lr_granted))
325 if (!list_empty(&res->lr_converting))
328 if (!list_empty(&res->lr_waiting))
331 if (!list_empty(&res->lr_children))
335 list_del(&res->lr_hash);
336 list_del(&res->lr_childof);
338 kmem_cache_free(ldlm_resource_slab, res);
339 l_unlock(&ns->ns_lock);
344 if (atomic_read(&res->lr_refcount) < 0)
351 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
352 struct ldlm_lock *lock)
354 l_lock(&res->lr_namespace->ns_lock);
356 ldlm_resource_dump(res);
357 ldlm_lock_dump(lock);
359 if (!list_empty(&lock->l_res_link))
362 list_add(&lock->l_res_link, head);
363 l_unlock(&res->lr_namespace->ns_lock);
366 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
368 l_lock(&lock->l_resource->lr_namespace->ns_lock);
369 list_del_init(&lock->l_res_link);
370 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
373 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
375 desc->lr_type = res->lr_type;
376 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
377 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
380 void ldlm_dump_all_namespaces(void)
382 struct list_head *tmp;
384 spin_lock(&ldlm_namespace_lock);
386 list_for_each(tmp, &ldlm_namespace_list) {
387 struct ldlm_namespace *ns;
388 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
389 ldlm_namespace_dump(ns);
392 spin_unlock(&ldlm_namespace_lock);
395 void ldlm_namespace_dump(struct ldlm_namespace *ns)
397 struct list_head *tmp;
399 l_lock(&ns->ns_lock);
400 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
401 ns->ns_refcount, ns->ns_client);
403 list_for_each(tmp, &ns->ns_root_list) {
404 struct ldlm_resource *res;
405 res = list_entry(tmp, struct ldlm_resource, lr_childof);
407 /* Once we have resources with children, this should really dump
408 * them recursively. */
409 ldlm_resource_dump(res);
411 l_unlock(&ns->ns_lock);
414 void ldlm_resource_dump(struct ldlm_resource *res)
416 struct list_head *tmp;
419 if (RES_NAME_SIZE != 3)
422 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
423 (unsigned long long)res->lr_name[0],
424 (unsigned long long)res->lr_name[1],
425 (unsigned long long)res->lr_name[2]);
427 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
428 atomic_read(&res->lr_refcount));
429 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
430 res->lr_namespace->ns_name);
431 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
433 CDEBUG(D_OTHER, "Granted locks:\n");
434 list_for_each(tmp, &res->lr_granted) {
435 struct ldlm_lock *lock;
436 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
437 ldlm_lock_dump(lock);
440 CDEBUG(D_OTHER, "Converting locks:\n");
441 list_for_each(tmp, &res->lr_converting) {
442 struct ldlm_lock *lock;
443 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
444 ldlm_lock_dump(lock);
447 CDEBUG(D_OTHER, "Waiting locks:\n");
448 list_for_each(tmp, &res->lr_waiting) {
449 struct ldlm_lock *lock;
450 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
451 ldlm_lock_dump(lock);