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)
26 LASSERT(ldlm_ns_proc_dir == NULL);
27 ldlm_ns_proc_dir=obd->obd_type->typ_procroot;
31 void ldlm_proc_cleanup(struct obd_device *obd)
33 ldlm_ns_proc_dir = NULL;
36 #define MAX_STRING_SIZE 100
37 void ldlm_proc_namespace(struct ldlm_namespace *ns)
39 struct lprocfs_vars lock_vars[2];
40 char lock_names[MAX_STRING_SIZE+1];
42 memset(lock_vars, 0, sizeof(lock_vars));
43 snprintf(lock_names, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
44 lock_names[MAX_STRING_SIZE] = '\0';
45 lock_vars[0].name = lock_names;
46 lock_vars[0].read_fptr = lprocfs_ll_rd;
47 lock_vars[0].write_fptr = NULL;
48 lock_vars[0].data = &ns->ns_resources;
49 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
51 memset(lock_vars, 0, sizeof(lock_vars));
52 snprintf(lock_names, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
53 lock_names[MAX_STRING_SIZE] = '\0';
54 lock_vars[0].name = lock_names;
55 lock_vars[0].read_fptr = lprocfs_ll_rd;
56 lock_vars[0].write_fptr = NULL;
57 lock_vars[0].data = &ns->ns_locks;
58 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
61 #undef MAX_STRING_SIZE
63 #define LDLM_MAX_UNUSED 20
64 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
66 struct ldlm_namespace *ns = NULL;
67 struct list_head *bucket;
70 OBD_ALLOC(ns, sizeof(*ns));
76 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
81 obd_memory += sizeof(*ns->ns_hash) * RES_HASH_SIZE;
83 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
88 strcpy(ns->ns_name, name);
90 INIT_LIST_HEAD(&ns->ns_root_list);
91 l_lock_init(&ns->ns_lock);
93 ns->ns_client = client;
94 spin_lock_init(&ns->ns_counter_lock);
98 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
100 INIT_LIST_HEAD(bucket);
102 INIT_LIST_HEAD(&ns->ns_unused_list);
103 ns->ns_nr_unused = 0;
104 ns->ns_max_unused = LDLM_MAX_UNUSED;
106 spin_lock(&ldlm_namespace_lock);
107 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
108 spin_unlock(&ldlm_namespace_lock);
109 ldlm_proc_namespace(ns);
113 if (ns && ns->ns_hash) {
114 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
116 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
118 if (ns && ns->ns_name)
119 OBD_FREE(ns->ns_name, strlen(name) + 1);
121 OBD_FREE(ns, sizeof(*ns));
125 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
127 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
128 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
131 struct list_head *tmp, *pos;
132 int rc = 0, client = res->lr_namespace->ns_client;
135 list_for_each_safe(tmp, pos, q) {
136 struct ldlm_lock *lock;
137 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
140 /* At shutdown time, don't call the cancellation callback */
141 lock->l_flags |= LDLM_FL_CANCEL;
144 struct lustre_handle lockh;
145 ldlm_lock2handle(lock, &lockh);
147 rc = ldlm_cli_cancel(&lockh);
149 CERROR("ldlm_cli_cancel: %d\n", rc);
151 /* Force local cleanup on errors, too. */
152 if (local_only || rc != ELDLM_OK)
153 ldlm_lock_cancel(lock);
155 LDLM_DEBUG(lock, "Freeing a lock still held by a "
158 ldlm_resource_unlink_lock(lock);
159 ldlm_lock_destroy(lock);
165 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
169 l_lock(&ns->ns_lock);
170 for (i = 0; i < RES_HASH_SIZE; i++) {
171 struct list_head *tmp, *pos;
172 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
173 struct ldlm_resource *res;
174 res = list_entry(tmp, struct ldlm_resource, lr_hash);
175 ldlm_resource_getref(res);
177 cleanup_resource(res, &res->lr_granted, local_only);
178 cleanup_resource(res, &res->lr_converting, local_only);
179 cleanup_resource(res, &res->lr_waiting, local_only);
181 /* XXX what a mess: don't force cleanup if we're
182 * local_only (which is only used by recovery). In that
183 * case, we probably still have outstanding lock refs
184 * which reference these resources. -phil */
185 if (!ldlm_resource_put(res) && !local_only) {
186 CERROR("Resource refcount nonzero (%d) after "
187 "lock cleanup; forcing cleanup.\n",
188 atomic_read(&res->lr_refcount));
189 ldlm_resource_dump(res);
190 atomic_set(&res->lr_refcount, 1);
191 ldlm_resource_put(res);
195 l_unlock(&ns->ns_lock);
200 /* Cleanup, but also free, the namespace */
201 int ldlm_namespace_free(struct ldlm_namespace *ns)
206 spin_lock(&ldlm_namespace_lock);
207 list_del(&ns->ns_list_chain);
209 spin_unlock(&ldlm_namespace_lock);
211 ldlm_namespace_cleanup(ns, 0);
213 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
214 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
215 obd_memory -= sizeof(*ns->ns_hash) * RES_HASH_SIZE;
216 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
217 OBD_FREE(ns, sizeof(*ns));
222 int ldlm_client_free(struct obd_export *exp)
224 struct ldlm_export_data *led = &exp->exp_ldlm_data;
225 ptlrpc_cleanup_client(&led->led_import);
229 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
234 for (i = 0; i < RES_NAME_SIZE; i++)
237 hash += (__u32)((unsigned long)parent >> 4);
239 return (hash & RES_HASH_MASK);
242 static struct ldlm_resource *ldlm_resource_new(void)
244 struct ldlm_resource *res;
246 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
251 memset(res, 0, sizeof(*res));
253 INIT_LIST_HEAD(&res->lr_children);
254 INIT_LIST_HEAD(&res->lr_childof);
255 INIT_LIST_HEAD(&res->lr_granted);
256 INIT_LIST_HEAD(&res->lr_converting);
257 INIT_LIST_HEAD(&res->lr_waiting);
259 atomic_set(&res->lr_refcount, 1);
264 /* Args: locked namespace
265 * Returns: newly-allocated, referenced, unlocked resource */
266 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
267 struct ldlm_resource *parent,
268 __u64 *name, __u32 type)
270 struct list_head *bucket;
271 struct ldlm_resource *res;
274 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
279 res = ldlm_resource_new();
285 spin_lock(&ns->ns_counter_lock);
287 spin_unlock(&ns->ns_counter_lock);
289 memcpy(res->lr_name, name, sizeof(res->lr_name));
290 res->lr_namespace = ns;
294 res->lr_most_restr = LCK_NL;
296 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
297 list_add(&res->lr_hash, bucket);
300 list_add(&res->lr_childof, &ns->ns_root_list);
302 res->lr_parent = parent;
303 list_add(&res->lr_childof, &parent->lr_children);
309 /* Args: unlocked namespace
310 * Locks: takes and releases ns->ns_lock and res->lr_lock
311 * Returns: referenced, unlocked ldlm_resource or NULL */
312 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
313 struct ldlm_resource *parent,
314 __u64 *name, __u32 type, int create)
316 struct list_head *bucket;
317 struct list_head *tmp = bucket;
318 struct ldlm_resource *res = NULL;
321 if (ns == NULL || ns->ns_hash == NULL) {
326 l_lock(&ns->ns_lock);
327 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
329 list_for_each(tmp, bucket) {
330 struct ldlm_resource *chk;
331 chk = list_entry(tmp, struct ldlm_resource, lr_hash);
333 if (memcmp(chk->lr_name, name, sizeof(chk->lr_name)) == 0) {
335 atomic_inc(&res->lr_refcount);
336 l_unlock(&ns->ns_lock);
342 res = ldlm_resource_add(ns, parent, name, type);
343 l_unlock(&ns->ns_lock);
348 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
350 atomic_inc(&res->lr_refcount);
354 /* Returns 1 if the resource was freed, 0 if it remains. */
355 int ldlm_resource_put(struct ldlm_resource *res)
359 if (atomic_dec_and_test(&res->lr_refcount)) {
360 struct ldlm_namespace *ns = res->lr_namespace;
363 l_lock(&ns->ns_lock);
365 if (atomic_read(&res->lr_refcount) != 0) {
366 /* We lost the race. */
367 l_unlock(&ns->ns_lock);
371 if (!list_empty(&res->lr_granted))
374 if (!list_empty(&res->lr_converting))
377 if (!list_empty(&res->lr_waiting))
380 if (!list_empty(&res->lr_children))
384 list_del(&res->lr_hash);
385 list_del(&res->lr_childof);
387 memset(res, 0x5a, sizeof(*res));
388 kmem_cache_free(ldlm_resource_slab, res);
389 l_unlock(&ns->ns_lock);
391 spin_lock(&ns->ns_counter_lock);
393 spin_unlock(&ns->ns_counter_lock);
399 if (atomic_read(&res->lr_refcount) < 0)
406 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
407 struct ldlm_lock *lock)
409 l_lock(&res->lr_namespace->ns_lock);
411 ldlm_resource_dump(res);
412 ldlm_lock_dump(lock);
414 if (!list_empty(&lock->l_res_link))
417 list_add(&lock->l_res_link, head);
418 l_unlock(&res->lr_namespace->ns_lock);
421 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
423 l_lock(&lock->l_resource->lr_namespace->ns_lock);
424 list_del_init(&lock->l_res_link);
425 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
428 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
430 desc->lr_type = res->lr_type;
431 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
432 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
435 void ldlm_dump_all_namespaces(void)
437 struct list_head *tmp;
439 spin_lock(&ldlm_namespace_lock);
441 list_for_each(tmp, &ldlm_namespace_list) {
442 struct ldlm_namespace *ns;
443 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
444 ldlm_namespace_dump(ns);
447 spin_unlock(&ldlm_namespace_lock);
450 void ldlm_namespace_dump(struct ldlm_namespace *ns)
452 struct list_head *tmp;
454 l_lock(&ns->ns_lock);
455 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
456 ns->ns_refcount, ns->ns_client);
458 list_for_each(tmp, &ns->ns_root_list) {
459 struct ldlm_resource *res;
460 res = list_entry(tmp, struct ldlm_resource, lr_childof);
462 /* Once we have resources with children, this should really dump
463 * them recursively. */
464 ldlm_resource_dump(res);
466 l_unlock(&ns->ns_lock);
469 void ldlm_resource_dump(struct ldlm_resource *res)
471 struct list_head *tmp;
474 if (RES_NAME_SIZE != 3)
477 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
478 (unsigned long long)res->lr_name[0],
479 (unsigned long long)res->lr_name[1],
480 (unsigned long long)res->lr_name[2]);
482 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
483 atomic_read(&res->lr_refcount));
484 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
485 res->lr_namespace->ns_name);
486 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
488 CDEBUG(D_OTHER, "Granted locks:\n");
489 list_for_each(tmp, &res->lr_granted) {
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, "Converting locks:\n");
496 list_for_each(tmp, &res->lr_converting) {
497 struct ldlm_lock *lock;
498 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
499 ldlm_lock_dump(lock);
502 CDEBUG(D_OTHER, "Waiting locks:\n");
503 list_for_each(tmp, &res->lr_waiting) {
504 struct ldlm_lock *lock;
505 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
506 ldlm_lock_dump(lock);