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 static int lprocfs_uint_rd(char *page, char **start, off_t off,
37 int count, int *eof, void *data)
39 unsigned int *temp = (unsigned int *)data;
41 len = snprintf(page, count, "%u\n", *temp);
45 #define MAX_STRING_SIZE 100
46 void ldlm_proc_namespace(struct ldlm_namespace *ns)
48 struct lprocfs_vars lock_vars[2];
49 char lock_names[MAX_STRING_SIZE + 1];
51 memset(lock_vars, 0, sizeof(lock_vars));
52 snprintf(lock_names, MAX_STRING_SIZE, "%s/resource_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_resources;
58 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
60 memset(lock_vars, 0, sizeof(lock_vars));
61 snprintf(lock_names, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
62 lock_names[MAX_STRING_SIZE] = '\0';
63 lock_vars[0].name = lock_names;
64 lock_vars[0].read_fptr = lprocfs_ll_rd;
65 lock_vars[0].write_fptr = NULL;
66 lock_vars[0].data = &ns->ns_locks;
67 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
69 memset(lock_vars, 0, sizeof(lock_vars));
70 snprintf(lock_names, MAX_STRING_SIZE, "%s/lock_unused_count",
72 lock_names[MAX_STRING_SIZE] = '\0';
73 lock_vars[0].name = lock_names;
74 lock_vars[0].read_fptr = lprocfs_uint_rd;
75 lock_vars[0].write_fptr = NULL;
76 lock_vars[0].data = &ns->ns_nr_unused;
77 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
79 #undef MAX_STRING_SIZE
81 #define LDLM_MAX_UNUSED 20
82 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
84 struct ldlm_namespace *ns = NULL;
85 struct list_head *bucket;
88 OBD_ALLOC(ns, sizeof(*ns));
92 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
96 atomic_add(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
98 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
100 GOTO(out_hash, NULL);
102 strcpy(ns->ns_name, name);
104 INIT_LIST_HEAD(&ns->ns_root_list);
105 l_lock_init(&ns->ns_lock);
107 ns->ns_client = client;
108 spin_lock_init(&ns->ns_counter_lock);
110 ns->ns_resources = 0;
112 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
114 INIT_LIST_HEAD(bucket);
116 INIT_LIST_HEAD(&ns->ns_unused_list);
117 ns->ns_nr_unused = 0;
118 ns->ns_max_unused = LDLM_MAX_UNUSED;
120 spin_lock(&ldlm_namespace_lock);
121 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
122 spin_unlock(&ldlm_namespace_lock);
123 ldlm_proc_namespace(ns);
127 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
129 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
131 OBD_FREE(ns, sizeof(*ns));
135 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
137 /* If 'local_only' is true, don't try to tell the server, just cleanup. */
138 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
141 struct list_head *tmp, *pos;
142 int rc = 0, client = res->lr_namespace->ns_client;
145 list_for_each_safe(tmp, pos, q) {
146 struct ldlm_lock *lock;
147 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
150 /* At shutdown time, don't call the cancellation callback */
151 lock->l_flags |= LDLM_FL_CANCEL;
154 struct lustre_handle lockh;
155 ldlm_lock2handle(lock, &lockh);
157 rc = ldlm_cli_cancel(&lockh);
159 CERROR("ldlm_cli_cancel: %d\n", rc);
161 /* Force local cleanup on errors, too. */
162 if (local_only || rc != ELDLM_OK)
163 ldlm_lock_cancel(lock);
165 LDLM_DEBUG(lock, "Freeing a lock still held by a "
168 ldlm_resource_unlink_lock(lock);
169 ldlm_lock_destroy(lock);
175 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
179 l_lock(&ns->ns_lock);
180 for (i = 0; i < RES_HASH_SIZE; i++) {
181 struct list_head *tmp, *pos;
182 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
183 struct ldlm_resource *res;
184 res = list_entry(tmp, struct ldlm_resource, lr_hash);
185 ldlm_resource_getref(res);
187 cleanup_resource(res, &res->lr_granted, local_only);
188 cleanup_resource(res, &res->lr_converting, local_only);
189 cleanup_resource(res, &res->lr_waiting, local_only);
191 /* XXX what a mess: don't force cleanup if we're
192 * local_only (which is only used by recovery). In that
193 * case, we probably still have outstanding lock refs
194 * which reference these resources. -phil */
195 if (!ldlm_resource_putref(res) && !local_only) {
196 CERROR("Resource refcount nonzero (%d) after "
197 "lock cleanup; forcing cleanup.\n",
198 atomic_read(&res->lr_refcount));
199 ldlm_resource_dump(res);
200 atomic_set(&res->lr_refcount, 1);
201 ldlm_resource_putref(res);
205 l_unlock(&ns->ns_lock);
210 /* Cleanup, but also free, the namespace */
211 int ldlm_namespace_free(struct ldlm_namespace *ns)
216 spin_lock(&ldlm_namespace_lock);
217 list_del(&ns->ns_list_chain);
219 spin_unlock(&ldlm_namespace_lock);
221 ldlm_namespace_cleanup(ns, 0);
223 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
224 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
225 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
226 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
227 OBD_FREE(ns, sizeof(*ns));
232 int ldlm_client_free(struct obd_export *exp)
234 struct ldlm_export_data *led = &exp->exp_ldlm_data;
235 ptlrpc_cleanup_client(&led->led_import);
239 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
244 for (i = 0; i < RES_NAME_SIZE; i++)
247 hash += (__u32)((unsigned long)parent >> 4);
249 return (hash & RES_HASH_MASK);
252 static struct ldlm_resource *ldlm_resource_new(void)
254 struct ldlm_resource *res;
256 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
261 memset(res, 0, sizeof(*res));
263 INIT_LIST_HEAD(&res->lr_children);
264 INIT_LIST_HEAD(&res->lr_childof);
265 INIT_LIST_HEAD(&res->lr_granted);
266 INIT_LIST_HEAD(&res->lr_converting);
267 INIT_LIST_HEAD(&res->lr_waiting);
269 atomic_set(&res->lr_refcount, 1);
274 /* Args: locked namespace
275 * Returns: newly-allocated, referenced, unlocked resource */
276 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
277 struct ldlm_resource *parent,
278 __u64 *name, __u32 type)
280 struct list_head *bucket;
281 struct ldlm_resource *res;
284 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
289 res = ldlm_resource_new();
295 spin_lock(&ns->ns_counter_lock);
297 spin_unlock(&ns->ns_counter_lock);
299 l_lock(&ns->ns_lock);
300 memcpy(res->lr_name, name, sizeof(res->lr_name));
301 res->lr_namespace = ns;
305 res->lr_most_restr = LCK_NL;
307 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
308 list_add(&res->lr_hash, bucket);
310 if (parent == NULL) {
311 list_add(&res->lr_childof, &ns->ns_root_list);
313 res->lr_parent = parent;
314 list_add(&res->lr_childof, &parent->lr_children);
316 l_unlock(&ns->ns_lock);
321 /* Args: unlocked namespace
322 * Locks: takes and releases ns->ns_lock and res->lr_lock
323 * Returns: referenced, unlocked ldlm_resource or NULL */
324 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
325 struct ldlm_resource *parent,
326 __u64 *name, __u32 type, int create)
328 struct list_head *bucket, *tmp;
329 struct ldlm_resource *res = NULL;
333 LASSERT(ns->ns_hash != NULL);
335 l_lock(&ns->ns_lock);
336 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
338 list_for_each(tmp, bucket) {
339 res = list_entry(tmp, struct ldlm_resource, lr_hash);
341 if (memcmp(res->lr_name, name, sizeof(res->lr_name)) == 0) {
342 ldlm_resource_getref(res);
343 l_unlock(&ns->ns_lock);
349 res = ldlm_resource_add(ns, parent, name, type);
353 l_unlock(&ns->ns_lock);
358 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
360 atomic_inc(&res->lr_refcount);
361 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
362 atomic_read(&res->lr_refcount));
366 /* Returns 1 if the resource was freed, 0 if it remains. */
367 int ldlm_resource_putref(struct ldlm_resource *res)
372 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
373 atomic_read(&res->lr_refcount) - 1);
374 LASSERT(atomic_read(&res->lr_refcount) > 0);
375 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
377 if (atomic_dec_and_test(&res->lr_refcount)) {
378 struct ldlm_namespace *ns = res->lr_namespace;
381 l_lock(&ns->ns_lock);
383 if (atomic_read(&res->lr_refcount) != 0) {
384 /* We lost the race. */
385 l_unlock(&ns->ns_lock);
389 if (!list_empty(&res->lr_granted)) {
390 ldlm_resource_dump(res);
394 if (!list_empty(&res->lr_converting)) {
395 ldlm_resource_dump(res);
399 if (!list_empty(&res->lr_waiting)) {
400 ldlm_resource_dump(res);
404 if (!list_empty(&res->lr_children)) {
405 ldlm_resource_dump(res);
410 list_del_init(&res->lr_hash);
411 list_del_init(&res->lr_childof);
413 memset(res, 0x5a, sizeof(*res));
414 kmem_cache_free(ldlm_resource_slab, res);
415 l_unlock(&ns->ns_lock);
417 spin_lock(&ns->ns_counter_lock);
419 spin_unlock(&ns->ns_counter_lock);
428 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
429 struct ldlm_lock *lock)
431 l_lock(&res->lr_namespace->ns_lock);
433 ldlm_resource_dump(res);
434 ldlm_lock_dump(lock);
436 LASSERT(list_empty(&lock->l_res_link));
438 list_add(&lock->l_res_link, head);
439 l_unlock(&res->lr_namespace->ns_lock);
442 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
444 l_lock(&lock->l_resource->lr_namespace->ns_lock);
445 list_del_init(&lock->l_res_link);
446 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
449 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
451 desc->lr_type = res->lr_type;
452 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
453 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
456 void ldlm_dump_all_namespaces(void)
458 struct list_head *tmp;
460 spin_lock(&ldlm_namespace_lock);
462 list_for_each(tmp, &ldlm_namespace_list) {
463 struct ldlm_namespace *ns;
464 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
465 ldlm_namespace_dump(ns);
468 spin_unlock(&ldlm_namespace_lock);
471 void ldlm_namespace_dump(struct ldlm_namespace *ns)
473 struct list_head *tmp;
475 l_lock(&ns->ns_lock);
476 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
477 ns->ns_refcount, ns->ns_client);
479 list_for_each(tmp, &ns->ns_root_list) {
480 struct ldlm_resource *res;
481 res = list_entry(tmp, struct ldlm_resource, lr_childof);
483 /* Once we have resources with children, this should really dump
484 * them recursively. */
485 ldlm_resource_dump(res);
487 l_unlock(&ns->ns_lock);
490 void ldlm_resource_dump(struct ldlm_resource *res)
492 struct list_head *tmp;
495 if (RES_NAME_SIZE != 3)
498 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
499 (unsigned long long)res->lr_name[0],
500 (unsigned long long)res->lr_name[1],
501 (unsigned long long)res->lr_name[2]);
503 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
504 atomic_read(&res->lr_refcount));
505 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
506 res->lr_namespace->ns_name);
507 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
509 CDEBUG(D_OTHER, "Granted locks:\n");
510 list_for_each(tmp, &res->lr_granted) {
511 struct ldlm_lock *lock;
512 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
513 ldlm_lock_dump(lock);
516 CDEBUG(D_OTHER, "Converting locks:\n");
517 list_for_each(tmp, &res->lr_converting) {
518 struct ldlm_lock *lock;
519 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
520 ldlm_lock_dump(lock);
523 CDEBUG(D_OTHER, "Waiting locks:\n");
524 list_for_each(tmp, &res->lr_waiting) {
525 struct ldlm_lock *lock;
526 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
527 ldlm_lock_dump(lock);