1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
6 * Author: Peter Braam <braam@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LDLM
26 #include <linux/lustre_dlm.h>
27 #include <linux/obd_class.h>
29 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
31 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
32 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
33 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
35 int ldlm_proc_setup(struct obd_device *obd)
39 LASSERT(ldlm_ns_proc_dir == NULL);
40 rc = lprocfs_obd_attach(obd, 0);
42 CERROR("LProcFS failed in ldlm-init\n");
45 ldlm_ns_proc_dir = obd->obd_proc_entry;
49 void ldlm_proc_cleanup(struct obd_device *obd)
51 if (ldlm_ns_proc_dir) {
52 lprocfs_obd_detach(obd);
53 ldlm_ns_proc_dir = NULL;
57 static int lprocfs_uint_rd(char *page, char **start, off_t off,
58 int count, int *eof, void *data)
60 unsigned int *temp = (unsigned int *)data;
61 return snprintf(page, count, "%u\n", *temp);
64 #define MAX_STRING_SIZE 128
65 void ldlm_proc_namespace(struct ldlm_namespace *ns)
67 struct lprocfs_vars lock_vars[2];
68 char lock_name[MAX_STRING_SIZE + 1];
70 lock_name[MAX_STRING_SIZE] = '\0';
72 memset(lock_vars, 0, sizeof(lock_vars));
73 lock_vars[0].read_fptr = lprocfs_rd_u64;
75 lock_vars[0].name = lock_name;
77 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
79 lock_vars[0].data = &ns->ns_resources;
80 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
82 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
83 lock_vars[0].data = &ns->ns_locks;
84 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
86 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
88 lock_vars[0].data = &ns->ns_nr_unused;
89 lock_vars[0].read_fptr = lprocfs_uint_rd;
90 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
92 #undef MAX_STRING_SIZE
94 #define LDLM_MAX_UNUSED 20
95 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
97 struct ldlm_namespace *ns = NULL;
98 struct list_head *bucket;
101 OBD_ALLOC(ns, sizeof(*ns));
105 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
109 atomic_add(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
111 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
113 GOTO(out_hash, NULL);
115 strcpy(ns->ns_name, name);
117 INIT_LIST_HEAD(&ns->ns_root_list);
118 l_lock_init(&ns->ns_lock);
120 ns->ns_client = client;
121 spin_lock_init(&ns->ns_counter_lock);
123 ns->ns_resources = 0;
125 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
127 INIT_LIST_HEAD(bucket);
129 INIT_LIST_HEAD(&ns->ns_unused_list);
130 ns->ns_nr_unused = 0;
131 ns->ns_max_unused = LDLM_MAX_UNUSED;
133 spin_lock(&ldlm_namespace_lock);
134 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
135 spin_unlock(&ldlm_namespace_lock);
136 ldlm_proc_namespace(ns);
140 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
142 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
144 OBD_FREE(ns, sizeof(*ns));
148 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
150 /* If 'local_only' is true, don't try to tell the server, just cleanup.
151 * This is currently only used for recovery, and we make certain assumptions
152 * as a result--notably, that we shouldn't cancel locks with refs. -phil
154 * Called with the ns_lock held. */
155 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
158 struct list_head *tmp, *pos;
159 int rc = 0, client = res->lr_namespace->ns_client;
162 list_for_each_safe(tmp, pos, q) {
163 struct ldlm_lock *lock;
164 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
167 if (local_only && (lock->l_readers || lock->l_writers)) {
168 /* This is a little bit gross, but much better than the
169 * alternative: pretend that we got a blocking AST from
170 * the server, so that when the lock is decref'd, it
171 * will go away ... */
172 lock->l_flags |= LDLM_FL_CBPENDING;
173 /* ... without sending a CANCEL message. */
174 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
175 /* ... and without calling the cancellation callback */
176 lock->l_flags |= LDLM_FL_CANCEL;
181 /* At shutdown time, don't call the cancellation callback */
182 lock->l_flags |= LDLM_FL_CANCEL;
185 struct lustre_handle lockh;
186 ldlm_lock2handle(lock, &lockh);
188 rc = ldlm_cli_cancel(&lockh);
190 CERROR("ldlm_cli_cancel: %d\n", rc);
192 /* Force local cleanup on errors, too. */
193 if (local_only || rc != ELDLM_OK)
194 ldlm_lock_cancel(lock);
196 LDLM_DEBUG0(lock, "Freeing a lock still held by a "
199 ldlm_resource_unlink_lock(lock);
200 ldlm_lock_destroy(lock);
207 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
212 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
216 l_lock(&ns->ns_lock);
217 for (i = 0; i < RES_HASH_SIZE; i++) {
218 struct list_head *tmp, *pos;
219 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
220 struct ldlm_resource *res;
221 res = list_entry(tmp, struct ldlm_resource, lr_hash);
222 ldlm_resource_getref(res);
224 cleanup_resource(res, &res->lr_granted, local_only);
225 cleanup_resource(res, &res->lr_converting, local_only);
226 cleanup_resource(res, &res->lr_waiting, local_only);
228 /* XXX what a mess: don't force cleanup if we're
229 * local_only (which is only used by recovery). In that
230 * case, we probably still have outstanding lock refs
231 * which reference these resources. -phil */
232 if (!ldlm_resource_putref(res) && !local_only) {
233 CERROR("Resource refcount nonzero (%d) after "
234 "lock cleanup; forcing cleanup.\n",
235 atomic_read(&res->lr_refcount));
236 ldlm_resource_dump(res);
237 atomic_set(&res->lr_refcount, 1);
238 ldlm_resource_putref(res);
242 l_unlock(&ns->ns_lock);
247 /* Cleanup, but also free, the namespace */
248 int ldlm_namespace_free(struct ldlm_namespace *ns)
253 spin_lock(&ldlm_namespace_lock);
254 list_del(&ns->ns_list_chain);
256 spin_unlock(&ldlm_namespace_lock);
258 ldlm_namespace_cleanup(ns, 0);
260 memset(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
261 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
262 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
263 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
264 OBD_FREE(ns, sizeof(*ns));
269 int ldlm_client_free(struct obd_export *exp)
271 struct ldlm_export_data *led = &exp->exp_ldlm_data;
272 ptlrpc_cleanup_client(&led->led_import);
276 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
281 for (i = 0; i < RES_NAME_SIZE; i++)
282 hash += name.name[i];
284 hash += (__u32)((unsigned long)parent >> 4);
286 return (hash & RES_HASH_MASK);
289 static struct ldlm_resource *ldlm_resource_new(void)
291 struct ldlm_resource *res;
293 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
298 memset(res, 0, sizeof(*res));
300 INIT_LIST_HEAD(&res->lr_children);
301 INIT_LIST_HEAD(&res->lr_childof);
302 INIT_LIST_HEAD(&res->lr_granted);
303 INIT_LIST_HEAD(&res->lr_converting);
304 INIT_LIST_HEAD(&res->lr_waiting);
306 atomic_set(&res->lr_refcount, 1);
311 /* Args: locked namespace
312 * Returns: newly-allocated, referenced, unlocked resource */
313 static struct ldlm_resource *
314 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
315 struct ldlm_res_id name, __u32 type)
317 struct list_head *bucket;
318 struct ldlm_resource *res;
321 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
326 res = ldlm_resource_new();
332 spin_lock(&ns->ns_counter_lock);
334 spin_unlock(&ns->ns_counter_lock);
336 l_lock(&ns->ns_lock);
337 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
338 res->lr_namespace = ns;
342 res->lr_most_restr = LCK_NL;
344 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
345 list_add(&res->lr_hash, bucket);
347 if (parent == NULL) {
348 list_add(&res->lr_childof, &ns->ns_root_list);
350 res->lr_parent = parent;
351 list_add(&res->lr_childof, &parent->lr_children);
353 l_unlock(&ns->ns_lock);
358 /* Args: unlocked namespace
359 * Locks: takes and releases ns->ns_lock and res->lr_lock
360 * Returns: referenced, unlocked ldlm_resource or NULL */
361 struct ldlm_resource *
362 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
363 struct ldlm_res_id name, __u32 type, int create)
365 struct list_head *bucket, *tmp;
366 struct ldlm_resource *res = NULL;
370 LASSERT(ns->ns_hash != NULL);
372 l_lock(&ns->ns_lock);
373 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
375 list_for_each(tmp, bucket) {
376 res = list_entry(tmp, struct ldlm_resource, lr_hash);
378 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
379 ldlm_resource_getref(res);
380 l_unlock(&ns->ns_lock);
386 res = ldlm_resource_add(ns, parent, name, type);
390 l_unlock(&ns->ns_lock);
395 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
397 atomic_inc(&res->lr_refcount);
398 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
399 atomic_read(&res->lr_refcount));
403 /* Returns 1 if the resource was freed, 0 if it remains. */
404 int ldlm_resource_putref(struct ldlm_resource *res)
409 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
410 atomic_read(&res->lr_refcount) - 1);
411 LASSERT(atomic_read(&res->lr_refcount) > 0);
412 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
414 if (atomic_dec_and_test(&res->lr_refcount)) {
415 struct ldlm_namespace *ns = res->lr_namespace;
418 l_lock(&ns->ns_lock);
420 if (atomic_read(&res->lr_refcount) != 0) {
421 /* We lost the race. */
422 l_unlock(&ns->ns_lock);
426 if (!list_empty(&res->lr_granted)) {
427 ldlm_resource_dump(res);
431 if (!list_empty(&res->lr_converting)) {
432 ldlm_resource_dump(res);
436 if (!list_empty(&res->lr_waiting)) {
437 ldlm_resource_dump(res);
441 if (!list_empty(&res->lr_children)) {
442 ldlm_resource_dump(res);
447 list_del_init(&res->lr_hash);
448 list_del_init(&res->lr_childof);
450 memset(res, 0x5a, sizeof(*res));
451 kmem_cache_free(ldlm_resource_slab, res);
452 l_unlock(&ns->ns_lock);
454 spin_lock(&ns->ns_counter_lock);
456 spin_unlock(&ns->ns_counter_lock);
465 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
466 struct ldlm_lock *lock)
468 l_lock(&res->lr_namespace->ns_lock);
470 ldlm_resource_dump(res);
471 CDEBUG(D_OTHER, "About to add this lock:\n");
472 ldlm_lock_dump(D_OTHER, lock);
474 if (lock->l_destroyed) {
475 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
479 LASSERT(list_empty(&lock->l_res_link));
481 list_add_tail(&lock->l_res_link, head);
482 l_unlock(&res->lr_namespace->ns_lock);
485 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
487 l_lock(&lock->l_resource->lr_namespace->ns_lock);
488 list_del_init(&lock->l_res_link);
489 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
492 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
494 desc->lr_type = res->lr_type;
495 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
496 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
499 void ldlm_dump_all_namespaces(void)
501 struct list_head *tmp;
503 spin_lock(&ldlm_namespace_lock);
505 list_for_each(tmp, &ldlm_namespace_list) {
506 struct ldlm_namespace *ns;
507 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
508 ldlm_namespace_dump(ns);
511 spin_unlock(&ldlm_namespace_lock);
514 void ldlm_namespace_dump(struct ldlm_namespace *ns)
516 struct list_head *tmp;
518 l_lock(&ns->ns_lock);
519 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
520 ns->ns_refcount, ns->ns_client);
522 list_for_each(tmp, &ns->ns_root_list) {
523 struct ldlm_resource *res;
524 res = list_entry(tmp, struct ldlm_resource, lr_childof);
526 /* Once we have resources with children, this should really dump
527 * them recursively. */
528 ldlm_resource_dump(res);
530 l_unlock(&ns->ns_lock);
533 void ldlm_resource_dump(struct ldlm_resource *res)
535 struct list_head *tmp;
538 if (RES_NAME_SIZE != 3)
541 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
542 (unsigned long long)res->lr_name.name[0],
543 (unsigned long long)res->lr_name.name[1],
544 (unsigned long long)res->lr_name.name[2]);
546 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
547 atomic_read(&res->lr_refcount));
548 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
549 res->lr_namespace->ns_name);
550 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
552 CDEBUG(D_OTHER, "Granted locks:\n");
553 list_for_each(tmp, &res->lr_granted) {
554 struct ldlm_lock *lock;
555 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
556 ldlm_lock_dump(D_OTHER, lock);
559 CDEBUG(D_OTHER, "Converting locks:\n");
560 list_for_each(tmp, &res->lr_converting) {
561 struct ldlm_lock *lock;
562 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
563 ldlm_lock_dump(D_OTHER, lock);
566 CDEBUG(D_OTHER, "Waiting locks:\n");
567 list_for_each(tmp, &res->lr_waiting) {
568 struct ldlm_lock *lock;
569 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
570 ldlm_lock_dump(D_OTHER, lock);