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 /* FIXME: This can go away when we start to really use lprocfs */
44 static int lprocfs_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 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
57 struct ldlm_namespace *ns = NULL;
58 struct list_head *bucket;
59 struct proc_dir_entry *proc_entry;
61 OBD_ALLOC(ns, sizeof(*ns));
67 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
72 obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
74 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
79 strcpy(ns->ns_name, name);
81 INIT_LIST_HEAD(&ns->ns_root_list);
83 ns->ns_client = client;
84 spin_lock_init(&ns->ns_counter_lock);
88 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
90 INIT_LIST_HEAD(bucket);
92 spin_lock(&ldlm_namespace_lock);
93 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
94 spin_unlock(&ldlm_namespace_lock);
96 ns->ns_proc_dir = proc_mkdir(ns->ns_name, ldlm_ns_proc_dir);
97 if (ns->ns_proc_dir == NULL)
98 CERROR("Unable to create proc directory for namespace.\n");
99 proc_entry = create_proc_entry("resource_count", 0444, ns->ns_proc_dir);
100 proc_entry->read_proc = lprocfs_ll_rd;
101 proc_entry->data = &ns->ns_resources;
102 proc_entry = create_proc_entry("lock_count", 0444, ns->ns_proc_dir);
103 proc_entry->read_proc = lprocfs_ll_rd;
104 proc_entry->data = &ns->ns_locks;
109 if (ns && ns->ns_hash) {
111 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
113 if (ns && ns->ns_name)
114 OBD_FREE(ns->ns_name, strlen(name) + 1);
116 OBD_FREE(ns, sizeof(*ns));
120 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
122 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
123 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
126 struct list_head *tmp, *pos;
127 int rc = 0, client = res->lr_namespace->ns_client;
130 list_for_each_safe(tmp, pos, q) {
131 struct ldlm_lock *lock;
132 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
136 struct lustre_handle lockh;
137 ldlm_lock2handle(lock, &lockh);
139 rc = ldlm_cli_cancel(&lockh);
141 CERROR("ldlm_cli_cancel: %d\n", rc);
143 /* Force local cleanup on errors, too. */
144 if (local_only || rc != ELDLM_OK)
145 ldlm_lock_cancel(lock);
147 LDLM_DEBUG(lock, "Freeing a lock still held by a "
150 ldlm_resource_unlink_lock(lock);
151 ldlm_lock_destroy(lock);
157 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
161 l_lock(&ldlm_everything_lock);
162 for (i = 0; i < RES_HASH_SIZE; i++) {
163 struct list_head *tmp, *pos;
164 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
165 struct ldlm_resource *res;
166 res = list_entry(tmp, struct ldlm_resource, lr_hash);
167 ldlm_resource_getref(res);
169 cleanup_resource(res, &res->lr_granted, local_only);
170 cleanup_resource(res, &res->lr_converting, local_only);
171 cleanup_resource(res, &res->lr_waiting, local_only);
173 /* XXX this is a bit counter-intuitive and should
174 * probably be cleaner: don't force cleanup if we're
175 * local_only (which is only used by recovery). We
176 * probably still have outstanding lock refs which
177 * reference these resources. -phil */
178 if (!ldlm_resource_put(res) && !local_only) {
179 CERROR("Resource refcount nonzero (%d) after "
180 "lock cleanup; forcing cleanup.\n",
181 atomic_read(&res->lr_refcount));
182 ldlm_resource_dump(res);
183 atomic_set(&res->lr_refcount, 1);
184 ldlm_resource_put(res);
188 l_unlock(&ldlm_everything_lock);
193 /* Cleanup, but also free, the namespace */
194 int ldlm_namespace_free(struct ldlm_namespace *ns)
199 spin_lock(&ldlm_namespace_lock);
200 list_del(&ns->ns_list_chain);
201 remove_proc_entry("resource_count", ns->ns_proc_dir);
202 remove_proc_entry("lock_count", ns->ns_proc_dir);
203 remove_proc_entry(ns->ns_name, ldlm_ns_proc_dir);
204 spin_unlock(&ldlm_namespace_lock);
206 ldlm_namespace_cleanup(ns, 0);
208 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
209 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
210 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
211 OBD_FREE(ns, sizeof(*ns));
216 int ldlm_client_free(struct obd_export *exp)
218 struct ldlm_export_data *led = &exp->exp_ldlm_data;
219 ptlrpc_cleanup_client(&led->led_import);
223 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
228 for (i = 0; i < RES_NAME_SIZE; i++)
231 hash += (__u32)((unsigned long)parent >> 4);
233 return (hash & RES_HASH_MASK);
236 static struct ldlm_resource *ldlm_resource_new(void)
238 struct ldlm_resource *res;
240 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
245 memset(res, 0, sizeof(*res));
247 INIT_LIST_HEAD(&res->lr_children);
248 INIT_LIST_HEAD(&res->lr_childof);
249 INIT_LIST_HEAD(&res->lr_granted);
250 INIT_LIST_HEAD(&res->lr_converting);
251 INIT_LIST_HEAD(&res->lr_waiting);
253 atomic_set(&res->lr_refcount, 1);
258 /* Args: locked namespace
259 * Returns: newly-allocated, referenced, unlocked resource */
260 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
261 struct ldlm_resource *parent,
262 __u64 *name, __u32 type)
264 struct list_head *bucket;
265 struct ldlm_resource *res;
268 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
273 res = ldlm_resource_new();
279 spin_lock(&ns->ns_counter_lock);
281 spin_unlock(&ns->ns_counter_lock);
283 memcpy(res->lr_name, name, sizeof(res->lr_name));
284 res->lr_namespace = ns;
288 res->lr_most_restr = LCK_NL;
290 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
291 list_add(&res->lr_hash, bucket);
294 list_add(&res->lr_childof, &ns->ns_root_list);
296 res->lr_parent = parent;
297 list_add(&res->lr_childof, &parent->lr_children);
303 /* Args: unlocked namespace
304 * Locks: takes and releases ns->ns_lock and res->lr_lock
305 * Returns: referenced, unlocked ldlm_resource or NULL */
306 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
307 struct ldlm_resource *parent,
308 __u64 *name, __u32 type, int create)
310 struct list_head *bucket;
311 struct list_head *tmp = bucket;
312 struct ldlm_resource *res = NULL;
315 if (ns == NULL || ns->ns_hash == NULL) {
320 l_lock(&ldlm_everything_lock);
321 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
323 list_for_each(tmp, bucket) {
324 struct ldlm_resource *chk;
325 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
327 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
329 atomic_inc(&res->lr_refcount);
335 if (res == NULL && create)
336 res = ldlm_resource_add(ns, parent, name, type);
337 l_unlock(&ldlm_everything_lock);
342 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
344 atomic_inc(&res->lr_refcount);
348 /* Returns 1 if the resource was freed, 0 if it remains. */
349 int ldlm_resource_put(struct ldlm_resource *res)
353 if (atomic_dec_and_test(&res->lr_refcount)) {
354 struct ldlm_namespace *ns = res->lr_namespace;
357 l_lock(&ldlm_everything_lock);
359 if (atomic_read(&res->lr_refcount) != 0) {
360 /* We lost the race. */
361 l_unlock(&ldlm_everything_lock);
365 if (!list_empty(&res->lr_granted))
368 if (!list_empty(&res->lr_converting))
371 if (!list_empty(&res->lr_waiting))
374 if (!list_empty(&res->lr_children))
378 list_del(&res->lr_hash);
379 list_del(&res->lr_childof);
381 kmem_cache_free(ldlm_resource_slab, res);
382 l_unlock(&ldlm_everything_lock);
384 spin_lock(&ns->ns_counter_lock);
386 spin_unlock(&ns->ns_counter_lock);
392 if (atomic_read(&res->lr_refcount) < 0)
399 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
400 struct ldlm_lock *lock)
402 l_lock(&ldlm_everything_lock);
404 ldlm_resource_dump(res);
405 ldlm_lock_dump(lock);
407 if (!list_empty(&lock->l_res_link))
410 list_add(&lock->l_res_link, head);
411 l_unlock(&ldlm_everything_lock);
414 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
416 l_lock(&ldlm_everything_lock);
417 list_del_init(&lock->l_res_link);
418 l_unlock(&ldlm_everything_lock);
421 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
423 desc->lr_type = res->lr_type;
424 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
425 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
428 void ldlm_dump_all_namespaces(void)
430 struct list_head *tmp;
432 spin_lock(&ldlm_namespace_lock);
434 list_for_each(tmp, &ldlm_namespace_list) {
435 struct ldlm_namespace *ns;
436 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
437 ldlm_namespace_dump(ns);
440 spin_unlock(&ldlm_namespace_lock);
443 void ldlm_namespace_dump(struct ldlm_namespace *ns)
445 struct list_head *tmp;
447 l_lock(&ldlm_everything_lock);
448 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
449 ns->ns_refcount, ns->ns_client);
451 list_for_each(tmp, &ns->ns_root_list) {
452 struct ldlm_resource *res;
453 res = list_entry(tmp, struct ldlm_resource, lr_childof);
455 /* Once we have resources with children, this should really dump
456 * them recursively. */
457 ldlm_resource_dump(res);
459 l_unlock(&ldlm_everything_lock);
462 void ldlm_resource_dump(struct ldlm_resource *res)
464 struct list_head *tmp;
467 if (RES_NAME_SIZE != 3)
470 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
471 (unsigned long long)res->lr_name[0],
472 (unsigned long long)res->lr_name[1],
473 (unsigned long long)res->lr_name[2]);
475 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
476 atomic_read(&res->lr_refcount));
477 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
478 res->lr_namespace->ns_name);
479 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
481 CDEBUG(D_OTHER, "Granted locks:\n");
482 list_for_each(tmp, &res->lr_granted) {
483 struct ldlm_lock *lock;
484 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
485 ldlm_lock_dump(lock);
488 CDEBUG(D_OTHER, "Converting locks:\n");
489 list_for_each(tmp, &res->lr_converting) {
490 struct ldlm_lock *lock;
491 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
492 ldlm_lock_dump(lock);
495 CDEBUG(D_OTHER, "Waiting locks:\n");
496 list_for_each(tmp, &res->lr_waiting) {
497 struct ldlm_lock *lock;
498 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
499 ldlm_lock_dump(lock);