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;
22 extern struct proc_dir_entry proc_root;
24 int ldlm_proc_setup(struct obd_device *obd)
28 LASSERT(ldlm_ns_proc_dir == NULL);
30 ldlm_ns_proc_dir = proc_mkdir("ldlm", &proc_root);
31 if (ldlm_ns_proc_dir == NULL) {
32 CERROR("Couldn't create /proc/ldlm\n");
38 void ldlm_proc_cleanup(struct obd_device *obd)
40 remove_proc_entry("ldlm", &proc_root);
43 /* FIXME: This can go away when we start to really use lprocfs */
44 static int ldlm_proc_ll_rd(char *page, char **start, off_t off,
45 int count, int *eof, void *data)
48 __u64 *temp = (__u64 *)data;
50 len = snprintf(page, count, "%Lu\n", *temp);
55 #define LDLM_MAX_UNUSED 20
56 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
58 struct ldlm_namespace *ns = NULL;
59 struct list_head *bucket;
60 struct proc_dir_entry *proc_entry;
62 OBD_ALLOC(ns, sizeof(*ns));
68 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
73 obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
75 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
80 strcpy(ns->ns_name, name);
82 INIT_LIST_HEAD(&ns->ns_root_list);
83 l_lock_init(&ns->ns_lock);
85 ns->ns_client = client;
86 spin_lock_init(&ns->ns_counter_lock);
90 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
92 INIT_LIST_HEAD(bucket);
94 INIT_LIST_HEAD(&ns->ns_unused_list);
96 ns->ns_max_unused = LDLM_MAX_UNUSED;
98 spin_lock(&ldlm_namespace_lock);
99 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
100 spin_unlock(&ldlm_namespace_lock);
102 ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
103 if (ns->ns_proc_dir == NULL)
104 CERROR("Unable to create proc directory for namespace.\n");
105 proc_entry = create_proc_entry("resource_count", 0444, ns->ns_proc_dir);
106 proc_entry->read_proc = ldlm_proc_ll_rd;
107 proc_entry->data = &ns->ns_resources;
108 proc_entry = create_proc_entry("lock_count", 0444, ns->ns_proc_dir);
109 proc_entry->read_proc = ldlm_proc_ll_rd;
110 proc_entry->data = &ns->ns_locks;
115 if (ns && ns->ns_hash) {
117 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
119 if (ns && ns->ns_name)
120 OBD_FREE(ns->ns_name, strlen(name) + 1);
122 OBD_FREE(ns, sizeof(*ns));
126 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
128 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
129 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
132 struct list_head *tmp, *pos;
133 int rc = 0, client = res->lr_namespace->ns_client;
136 list_for_each_safe(tmp, pos, q) {
137 struct ldlm_lock *lock;
138 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
141 /* At shutdown time, don't call the cancellation callback */
142 lock->l_flags |= LDLM_FL_CANCEL;
145 struct lustre_handle lockh;
146 ldlm_lock2handle(lock, &lockh);
148 rc = ldlm_cli_cancel(&lockh);
150 CERROR("ldlm_cli_cancel: %d\n", rc);
152 /* Force local cleanup on errors, too. */
153 if (local_only || rc != ELDLM_OK)
154 ldlm_lock_cancel(lock);
156 LDLM_DEBUG(lock, "Freeing a lock still held by a "
159 ldlm_resource_unlink_lock(lock);
160 ldlm_lock_destroy(lock);
166 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
170 l_lock(&ns->ns_lock);
171 for (i = 0; i < RES_HASH_SIZE; i++) {
172 struct list_head *tmp, *pos;
173 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
174 struct ldlm_resource *res;
175 res = list_entry(tmp, struct ldlm_resource, lr_hash);
176 ldlm_resource_getref(res);
178 cleanup_resource(res, &res->lr_granted, local_only);
179 cleanup_resource(res, &res->lr_converting, local_only);
180 cleanup_resource(res, &res->lr_waiting, local_only);
182 /* XXX this is a bit counter-intuitive and should
183 * probably be cleaner: don't force cleanup if we're
184 * local_only (which is only used by recovery). We
185 * probably still have outstanding lock refs which
186 * reference these resources. -phil */
187 if (!ldlm_resource_put(res) && !local_only) {
188 CERROR("Resource refcount nonzero (%d) after "
189 "lock cleanup; forcing cleanup.\n",
190 atomic_read(&res->lr_refcount));
191 ldlm_resource_dump(res);
192 atomic_set(&res->lr_refcount, 1);
193 ldlm_resource_put(res);
197 l_unlock(&ns->ns_lock);
202 /* Cleanup, but also free, the namespace */
203 int ldlm_namespace_free(struct ldlm_namespace *ns)
208 spin_lock(&ldlm_namespace_lock);
209 list_del(&ns->ns_list_chain);
210 remove_proc_entry("resource_count", ns->ns_proc_dir);
211 remove_proc_entry("lock_count", ns->ns_proc_dir);
212 remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
213 spin_unlock(&ldlm_namespace_lock);
215 ldlm_namespace_cleanup(ns, 0);
217 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
218 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
219 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
220 OBD_FREE(ns, sizeof(*ns));
225 int ldlm_client_free(struct obd_export *exp)
227 struct ldlm_export_data *led = &exp->exp_ldlm_data;
228 ptlrpc_cleanup_client(&led->led_import);
232 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
237 for (i = 0; i < RES_NAME_SIZE; i++)
240 hash += (__u32)((unsigned long)parent >> 4);
242 return (hash & RES_HASH_MASK);
245 static struct ldlm_resource *ldlm_resource_new(void)
247 struct ldlm_resource *res;
249 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
254 memset(res, 0, sizeof(*res));
256 INIT_LIST_HEAD(&res->lr_children);
257 INIT_LIST_HEAD(&res->lr_childof);
258 INIT_LIST_HEAD(&res->lr_granted);
259 INIT_LIST_HEAD(&res->lr_converting);
260 INIT_LIST_HEAD(&res->lr_waiting);
262 atomic_set(&res->lr_refcount, 1);
267 /* Args: locked namespace
268 * Returns: newly-allocated, referenced, unlocked resource */
269 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
270 struct ldlm_resource *parent,
271 __u64 *name, __u32 type)
273 struct list_head *bucket;
274 struct ldlm_resource *res;
277 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
282 res = ldlm_resource_new();
288 spin_lock(&ns->ns_counter_lock);
290 spin_unlock(&ns->ns_counter_lock);
292 memcpy(res->lr_name, name, sizeof(res->lr_name));
293 res->lr_namespace = ns;
297 res->lr_most_restr = LCK_NL;
299 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
300 list_add(&res->lr_hash, bucket);
303 list_add(&res->lr_childof, &ns->ns_root_list);
305 res->lr_parent = parent;
306 list_add(&res->lr_childof, &parent->lr_children);
312 /* Args: unlocked namespace
313 * Locks: takes and releases ns->ns_lock and res->lr_lock
314 * Returns: referenced, unlocked ldlm_resource or NULL */
315 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
316 struct ldlm_resource *parent,
317 __u64 *name, __u32 type, int create)
319 struct list_head *bucket;
320 struct list_head *tmp = bucket;
321 struct ldlm_resource *res = NULL;
324 if (ns == NULL || ns->ns_hash == NULL) {
329 l_lock(&ns->ns_lock);
330 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
332 list_for_each(tmp, bucket) {
333 struct ldlm_resource *chk;
334 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
336 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
338 atomic_inc(&res->lr_refcount);
344 if (res == NULL && create)
345 res = ldlm_resource_add(ns, parent, name, type);
346 l_unlock(&ns->ns_lock);
351 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
353 atomic_inc(&res->lr_refcount);
357 /* Returns 1 if the resource was freed, 0 if it remains. */
358 int ldlm_resource_put(struct ldlm_resource *res)
362 if (atomic_dec_and_test(&res->lr_refcount)) {
363 struct ldlm_namespace *ns = res->lr_namespace;
366 l_lock(&ns->ns_lock);
368 if (atomic_read(&res->lr_refcount) != 0) {
369 /* We lost the race. */
370 l_unlock(&ns->ns_lock);
374 if (!list_empty(&res->lr_granted))
377 if (!list_empty(&res->lr_converting))
380 if (!list_empty(&res->lr_waiting))
383 if (!list_empty(&res->lr_children))
387 list_del(&res->lr_hash);
388 list_del(&res->lr_childof);
390 kmem_cache_free(ldlm_resource_slab, res);
391 l_unlock(&ns->ns_lock);
393 spin_lock(&ns->ns_counter_lock);
395 spin_unlock(&ns->ns_counter_lock);
401 if (atomic_read(&res->lr_refcount) < 0)
408 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
409 struct ldlm_lock *lock)
411 l_lock(&res->lr_namespace->ns_lock);
413 ldlm_resource_dump(res);
414 ldlm_lock_dump(lock);
416 if (!list_empty(&lock->l_res_link))
419 list_add(&lock->l_res_link, head);
420 l_unlock(&res->lr_namespace->ns_lock);
423 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
425 l_lock(&lock->l_resource->lr_namespace->ns_lock);
426 list_del_init(&lock->l_res_link);
427 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
430 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
432 desc->lr_type = res->lr_type;
433 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
434 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
437 void ldlm_dump_all_namespaces(void)
439 struct list_head *tmp;
441 spin_lock(&ldlm_namespace_lock);
443 list_for_each(tmp, &ldlm_namespace_list) {
444 struct ldlm_namespace *ns;
445 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
446 ldlm_namespace_dump(ns);
449 spin_unlock(&ldlm_namespace_lock);
452 void ldlm_namespace_dump(struct ldlm_namespace *ns)
454 struct list_head *tmp;
456 l_lock(&ns->ns_lock);
457 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
458 ns->ns_refcount, ns->ns_client);
460 list_for_each(tmp, &ns->ns_root_list) {
461 struct ldlm_resource *res;
462 res = list_entry(tmp, struct ldlm_resource, lr_childof);
464 /* Once we have resources with children, this should really dump
465 * them recursively. */
466 ldlm_resource_dump(res);
468 l_unlock(&ns->ns_lock);
471 void ldlm_resource_dump(struct ldlm_resource *res)
473 struct list_head *tmp;
476 if (RES_NAME_SIZE != 3)
479 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
480 (unsigned long long)res->lr_name[0],
481 (unsigned long long)res->lr_name[1],
482 (unsigned long long)res->lr_name[2]);
484 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
485 atomic_read(&res->lr_refcount));
486 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
487 res->lr_namespace->ns_name);
488 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
490 CDEBUG(D_OTHER, "Granted locks:\n");
491 list_for_each(tmp, &res->lr_granted) {
492 struct ldlm_lock *lock;
493 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
494 ldlm_lock_dump(lock);
497 CDEBUG(D_OTHER, "Converting locks:\n");
498 list_for_each(tmp, &res->lr_converting) {
499 struct ldlm_lock *lock;
500 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
501 ldlm_lock_dump(lock);
504 CDEBUG(D_OTHER, "Waiting locks:\n");
505 list_for_each(tmp, &res->lr_waiting) {
506 struct ldlm_lock *lock;
507 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
508 ldlm_lock_dump(lock);