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 * This is currently only used for recovery, and we make certain assumptions
139 * as a result--notably, that we shouldn't cancel locks with refs. -phil */
140 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
143 struct list_head *tmp, *pos;
144 int rc = 0, client = res->lr_namespace->ns_client;
147 list_for_each_safe(tmp, pos, q) {
148 struct ldlm_lock *lock;
149 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
152 if (local_only && (lock->l_readers || lock->l_writers)) {
153 /* This is a little bit gross, but much better than the
154 * alternative: pretend that we got a blocking AST from
155 * the server, so that when the lock is decref'd, it
156 * will go away ... */
157 lock->l_flags |= LDLM_FL_CBPENDING;
158 /* ... without sending a CANCEL message. */
159 lock->l_flags |= LDLM_FL_CANCELING;
164 /* At shutdown time, don't call the cancellation callback */
165 lock->l_flags |= LDLM_FL_CANCEL;
168 struct lustre_handle lockh;
169 ldlm_lock2handle(lock, &lockh);
171 rc = ldlm_cli_cancel(&lockh);
173 CERROR("ldlm_cli_cancel: %d\n", rc);
175 /* Force local cleanup on errors, too. */
176 if (local_only || rc != ELDLM_OK)
177 ldlm_lock_cancel(lock);
179 LDLM_DEBUG(lock, "Freeing a lock still held by a "
182 ldlm_resource_unlink_lock(lock);
183 ldlm_lock_destroy(lock);
190 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
195 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
199 l_lock(&ns->ns_lock);
200 for (i = 0; i < RES_HASH_SIZE; i++) {
201 struct list_head *tmp, *pos;
202 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
203 struct ldlm_resource *res;
204 res = list_entry(tmp, struct ldlm_resource, lr_hash);
205 ldlm_resource_getref(res);
207 cleanup_resource(res, &res->lr_granted, local_only);
208 cleanup_resource(res, &res->lr_converting, local_only);
209 cleanup_resource(res, &res->lr_waiting, local_only);
211 /* XXX what a mess: don't force cleanup if we're
212 * local_only (which is only used by recovery). In that
213 * case, we probably still have outstanding lock refs
214 * which reference these resources. -phil */
215 if (!ldlm_resource_putref(res) && !local_only) {
216 CERROR("Resource refcount nonzero (%d) after "
217 "lock cleanup; forcing cleanup.\n",
218 atomic_read(&res->lr_refcount));
219 ldlm_resource_dump(res);
220 atomic_set(&res->lr_refcount, 1);
221 ldlm_resource_putref(res);
225 l_unlock(&ns->ns_lock);
230 /* Cleanup, but also free, the namespace */
231 int ldlm_namespace_free(struct ldlm_namespace *ns)
236 spin_lock(&ldlm_namespace_lock);
237 list_del(&ns->ns_list_chain);
239 spin_unlock(&ldlm_namespace_lock);
241 ldlm_namespace_cleanup(ns, 0);
243 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
244 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
245 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
246 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
247 OBD_FREE(ns, sizeof(*ns));
252 int ldlm_client_free(struct obd_export *exp)
254 struct ldlm_export_data *led = &exp->exp_ldlm_data;
255 ptlrpc_cleanup_client(&led->led_import);
259 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, __u64 *name)
264 for (i = 0; i < RES_NAME_SIZE; i++)
267 hash += (__u32)((unsigned long)parent >> 4);
269 return (hash & RES_HASH_MASK);
272 static struct ldlm_resource *ldlm_resource_new(void)
274 struct ldlm_resource *res;
276 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
281 memset(res, 0, sizeof(*res));
283 INIT_LIST_HEAD(&res->lr_children);
284 INIT_LIST_HEAD(&res->lr_childof);
285 INIT_LIST_HEAD(&res->lr_granted);
286 INIT_LIST_HEAD(&res->lr_converting);
287 INIT_LIST_HEAD(&res->lr_waiting);
289 atomic_set(&res->lr_refcount, 1);
294 /* Args: locked namespace
295 * Returns: newly-allocated, referenced, unlocked resource */
296 static struct ldlm_resource *ldlm_resource_add(struct ldlm_namespace *ns,
297 struct ldlm_resource *parent,
298 __u64 *name, __u32 type)
300 struct list_head *bucket;
301 struct ldlm_resource *res;
304 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
309 res = ldlm_resource_new();
315 spin_lock(&ns->ns_counter_lock);
317 spin_unlock(&ns->ns_counter_lock);
319 l_lock(&ns->ns_lock);
320 memcpy(res->lr_name, name, sizeof(res->lr_name));
321 res->lr_namespace = ns;
325 res->lr_most_restr = LCK_NL;
327 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
328 list_add(&res->lr_hash, bucket);
330 if (parent == NULL) {
331 list_add(&res->lr_childof, &ns->ns_root_list);
333 res->lr_parent = parent;
334 list_add(&res->lr_childof, &parent->lr_children);
336 l_unlock(&ns->ns_lock);
341 /* Args: unlocked namespace
342 * Locks: takes and releases ns->ns_lock and res->lr_lock
343 * Returns: referenced, unlocked ldlm_resource or NULL */
344 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
345 struct ldlm_resource *parent,
346 __u64 *name, __u32 type, int create)
348 struct list_head *bucket, *tmp;
349 struct ldlm_resource *res = NULL;
353 LASSERT(ns->ns_hash != NULL);
355 l_lock(&ns->ns_lock);
356 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
358 list_for_each(tmp, bucket) {
359 res = list_entry(tmp, struct ldlm_resource, lr_hash);
361 if (memcmp(res->lr_name, name, sizeof(res->lr_name)) == 0) {
362 ldlm_resource_getref(res);
363 l_unlock(&ns->ns_lock);
369 res = ldlm_resource_add(ns, parent, name, type);
373 l_unlock(&ns->ns_lock);
378 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
380 atomic_inc(&res->lr_refcount);
381 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
382 atomic_read(&res->lr_refcount));
386 /* Returns 1 if the resource was freed, 0 if it remains. */
387 int ldlm_resource_putref(struct ldlm_resource *res)
392 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
393 atomic_read(&res->lr_refcount) - 1);
394 LASSERT(atomic_read(&res->lr_refcount) > 0);
395 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
397 if (atomic_dec_and_test(&res->lr_refcount)) {
398 struct ldlm_namespace *ns = res->lr_namespace;
401 l_lock(&ns->ns_lock);
403 if (atomic_read(&res->lr_refcount) != 0) {
404 /* We lost the race. */
405 l_unlock(&ns->ns_lock);
409 if (!list_empty(&res->lr_granted)) {
410 ldlm_resource_dump(res);
414 if (!list_empty(&res->lr_converting)) {
415 ldlm_resource_dump(res);
419 if (!list_empty(&res->lr_waiting)) {
420 ldlm_resource_dump(res);
424 if (!list_empty(&res->lr_children)) {
425 ldlm_resource_dump(res);
430 list_del_init(&res->lr_hash);
431 list_del_init(&res->lr_childof);
433 memset(res, 0x5a, sizeof(*res));
434 kmem_cache_free(ldlm_resource_slab, res);
435 l_unlock(&ns->ns_lock);
437 spin_lock(&ns->ns_counter_lock);
439 spin_unlock(&ns->ns_counter_lock);
448 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
449 struct ldlm_lock *lock)
451 l_lock(&res->lr_namespace->ns_lock);
453 ldlm_resource_dump(res);
454 CDEBUG(D_OTHER, "About to grant this lock:\n");
455 ldlm_lock_dump(D_OTHER, lock);
457 LASSERT(list_empty(&lock->l_res_link));
459 list_add(&lock->l_res_link, head);
460 l_unlock(&res->lr_namespace->ns_lock);
463 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
465 l_lock(&lock->l_resource->lr_namespace->ns_lock);
466 list_del_init(&lock->l_res_link);
467 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
470 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
472 desc->lr_type = res->lr_type;
473 memcpy(desc->lr_name, res->lr_name, sizeof(desc->lr_name));
474 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
477 void ldlm_dump_all_namespaces(void)
479 struct list_head *tmp;
481 spin_lock(&ldlm_namespace_lock);
483 list_for_each(tmp, &ldlm_namespace_list) {
484 struct ldlm_namespace *ns;
485 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
486 ldlm_namespace_dump(ns);
489 spin_unlock(&ldlm_namespace_lock);
492 void ldlm_namespace_dump(struct ldlm_namespace *ns)
494 struct list_head *tmp;
496 l_lock(&ns->ns_lock);
497 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
498 ns->ns_refcount, ns->ns_client);
500 list_for_each(tmp, &ns->ns_root_list) {
501 struct ldlm_resource *res;
502 res = list_entry(tmp, struct ldlm_resource, lr_childof);
504 /* Once we have resources with children, this should really dump
505 * them recursively. */
506 ldlm_resource_dump(res);
508 l_unlock(&ns->ns_lock);
511 void ldlm_resource_dump(struct ldlm_resource *res)
513 struct list_head *tmp;
516 if (RES_NAME_SIZE != 3)
519 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
520 (unsigned long long)res->lr_name[0],
521 (unsigned long long)res->lr_name[1],
522 (unsigned long long)res->lr_name[2]);
524 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
525 atomic_read(&res->lr_refcount));
526 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
527 res->lr_namespace->ns_name);
528 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
530 CDEBUG(D_OTHER, "Granted locks:\n");
531 list_for_each(tmp, &res->lr_granted) {
532 struct ldlm_lock *lock;
533 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
534 ldlm_lock_dump(D_OTHER, lock);
537 CDEBUG(D_OTHER, "Converting locks:\n");
538 list_for_each(tmp, &res->lr_converting) {
539 struct ldlm_lock *lock;
540 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
541 ldlm_lock_dump(D_OTHER, lock);
544 CDEBUG(D_OTHER, "Waiting locks:\n");
545 list_for_each(tmp, &res->lr_waiting) {
546 struct ldlm_lock *lock;
547 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
548 ldlm_lock_dump(D_OTHER, lock);