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>
28 # include <liblustre.h>
31 #include <linux/obd_class.h>
32 #include "ldlm_internal.h"
34 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
36 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
37 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
38 struct proc_dir_entry *ldlm_type_proc_dir = NULL;
39 struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
40 struct proc_dir_entry *ldlm_svc_proc_dir = NULL;
43 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
44 unsigned long count, void *data)
46 ldlm_dump_all_namespaces();
50 int ldlm_proc_setup(void)
53 struct lprocfs_vars list[] = {
54 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
57 LASSERT(ldlm_ns_proc_dir == NULL);
59 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
62 if (IS_ERR(ldlm_type_proc_dir)) {
63 CERROR("LProcFS failed in ldlm-init\n");
64 rc = PTR_ERR(ldlm_type_proc_dir);
68 ldlm_ns_proc_dir = lprocfs_register("namespaces",
71 if (IS_ERR(ldlm_ns_proc_dir)) {
72 CERROR("LProcFS failed in ldlm-init\n");
73 rc = PTR_ERR(ldlm_ns_proc_dir);
77 ldlm_svc_proc_dir = lprocfs_register("services",
80 if (IS_ERR(ldlm_svc_proc_dir)) {
81 CERROR("LProcFS failed in ldlm-init\n");
82 rc = PTR_ERR(ldlm_svc_proc_dir);
86 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
91 lprocfs_remove(ldlm_ns_proc_dir);
93 lprocfs_remove(ldlm_type_proc_dir);
95 ldlm_type_proc_dir = NULL;
96 ldlm_ns_proc_dir = NULL;
97 ldlm_svc_proc_dir = NULL;
101 void ldlm_proc_cleanup(void)
103 if (ldlm_svc_proc_dir) {
104 lprocfs_remove(ldlm_svc_proc_dir);
105 ldlm_svc_proc_dir = NULL;
108 if (ldlm_ns_proc_dir) {
109 lprocfs_remove(ldlm_ns_proc_dir);
110 ldlm_ns_proc_dir = NULL;
113 if (ldlm_type_proc_dir) {
114 lprocfs_remove(ldlm_type_proc_dir);
115 ldlm_type_proc_dir = NULL;
119 static int lprocfs_uint_rd(char *page, char **start, off_t off,
120 int count, int *eof, void *data)
122 unsigned int *temp = (unsigned int *)data;
123 return snprintf(page, count, "%u\n", *temp);
126 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
127 int count, int *eof, void *data)
129 struct ldlm_namespace *ns = data;
130 return lprocfs_uint_rd(page, start, off, count, eof,
134 #define MAX_STRING_SIZE 128
135 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
136 unsigned long count, void *data)
138 struct ldlm_namespace *ns = data;
139 char dummy[MAX_STRING_SIZE + 1];
142 dummy[MAX_STRING_SIZE] = '\0';
143 copy_from_user(dummy, buffer, MAX_STRING_SIZE);
145 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
147 "dropping all unused locks from namespace %s\n",
149 tmp = ns->ns_max_unused;
150 ns->ns_max_unused = 0;
152 ns->ns_max_unused = tmp;
156 tmp = simple_strtoul(dummy, NULL, 0);
157 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
158 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
159 ns->ns_max_unused = (unsigned int)tmp;
166 void ldlm_proc_namespace(struct ldlm_namespace *ns)
168 struct lprocfs_vars lock_vars[2];
169 char lock_name[MAX_STRING_SIZE + 1];
172 LASSERT(ns->ns_name != NULL);
174 lock_name[MAX_STRING_SIZE] = '\0';
176 memset(lock_vars, 0, sizeof(lock_vars));
177 lock_vars[0].read_fptr = lprocfs_rd_u64;
178 lock_vars[0].name = lock_name;
180 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
181 lock_vars[0].data = &ns->ns_resources;
182 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
184 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
185 lock_vars[0].data = &ns->ns_locks;
186 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
189 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
191 lock_vars[0].data = &ns->ns_nr_unused;
192 lock_vars[0].read_fptr = lprocfs_uint_rd;
193 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
195 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
197 lock_vars[0].data = ns;
198 lock_vars[0].read_fptr = lprocfs_read_lru_size;
199 lock_vars[0].write_fptr = lprocfs_write_lru_size;
200 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
204 #undef MAX_STRING_SIZE
206 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
208 struct ldlm_namespace *ns = NULL;
209 struct list_head *bucket;
215 CERROR("ldlm_get_ref failed: %d\n", rc);
219 OBD_ALLOC(ns, sizeof(*ns));
223 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
227 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
229 GOTO(out_hash, NULL);
231 strcpy(ns->ns_name, name);
233 INIT_LIST_HEAD(&ns->ns_root_list);
234 l_lock_init(&ns->ns_lock);
236 ns->ns_client = client;
237 spin_lock_init(&ns->ns_counter_lock);
239 ns->ns_resources = 0;
241 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
243 INIT_LIST_HEAD(bucket);
245 INIT_LIST_HEAD(&ns->ns_unused_list);
246 ns->ns_nr_unused = 0;
247 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
249 spin_lock(&ldlm_namespace_lock);
250 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
251 spin_unlock(&ldlm_namespace_lock);
253 ldlm_proc_namespace(ns);
258 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
259 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
261 OBD_FREE(ns, sizeof(*ns));
267 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
269 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
270 * This is currently only used for recovery, and we make certain assumptions
271 * as a result--notably, that we shouldn't cancel locks with refs. -phil
273 * Called with the ns_lock held. */
274 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
277 struct list_head *tmp, *pos;
278 int rc = 0, client = res->lr_namespace->ns_client;
279 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
282 list_for_each_safe(tmp, pos, q) {
283 struct ldlm_lock *lock;
284 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
287 lock->l_flags |= LDLM_FL_FAILED;
288 lock->l_flags |= flags;
290 if (local_only && (lock->l_readers || lock->l_writers)) {
291 /* This is a little bit gross, but much better than the
292 * alternative: pretend that we got a blocking AST from
293 * the server, so that when the lock is decref'd, it
294 * will go away ... */
295 lock->l_flags |= LDLM_FL_CBPENDING;
296 /* ... without sending a CANCEL message. */
297 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
298 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
299 if (lock->l_completion_ast)
300 lock->l_completion_ast(lock, 0, NULL);
306 struct lustre_handle lockh;
307 ldlm_lock2handle(lock, &lockh);
309 rc = ldlm_cli_cancel(&lockh);
311 CERROR("ldlm_cli_cancel: %d\n", rc);
313 /* Force local cleanup on errors, too. */
314 if (local_only || rc != ELDLM_OK)
315 ldlm_lock_cancel(lock);
317 LDLM_DEBUG(lock, "Freeing a lock still held by a "
320 ldlm_resource_unlink_lock(lock);
321 ldlm_lock_destroy(lock);
328 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
333 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
337 l_lock(&ns->ns_lock);
338 for (i = 0; i < RES_HASH_SIZE; i++) {
339 struct list_head *tmp, *pos;
340 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
341 struct ldlm_resource *res;
342 res = list_entry(tmp, struct ldlm_resource, lr_hash);
343 ldlm_resource_getref(res);
345 cleanup_resource(res, &res->lr_granted, flags);
346 cleanup_resource(res, &res->lr_converting, flags);
347 cleanup_resource(res, &res->lr_waiting, flags);
349 /* XXX what a mess: don't force cleanup if we're
350 * local_only (which is only used by recovery). In that
351 * case, we probably still have outstanding lock refs
352 * which reference these resources. -phil */
353 if (!ldlm_resource_putref(res) &&
354 !(flags & LDLM_FL_LOCAL_ONLY)) {
355 CERROR("Resource refcount nonzero (%d) after "
356 "lock cleanup; forcing cleanup.\n",
357 atomic_read(&res->lr_refcount));
358 ldlm_resource_dump(res);
359 atomic_set(&res->lr_refcount, 1);
360 ldlm_resource_putref(res);
364 l_unlock(&ns->ns_lock);
369 /* Cleanup, but also free, the namespace */
370 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
375 spin_lock(&ldlm_namespace_lock);
376 list_del(&ns->ns_list_chain);
378 spin_unlock(&ldlm_namespace_lock);
380 /* At shutdown time, don't call the cancellation callback */
381 ldlm_namespace_cleanup(ns, 0);
385 struct proc_dir_entry *dir;
386 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
388 CERROR("dlm namespace %s has no procfs dir?\n",
396 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
397 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
398 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
399 OBD_FREE(ns, sizeof(*ns));
406 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
411 for (i = 0; i < RES_NAME_SIZE; i++)
412 hash += name.name[i];
414 hash += (__u32)((unsigned long)parent >> 4);
416 return (hash & RES_HASH_MASK);
419 static struct ldlm_resource *ldlm_resource_new(void)
421 struct ldlm_resource *res;
423 OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
428 memset(res, 0, sizeof(*res));
430 INIT_LIST_HEAD(&res->lr_children);
431 INIT_LIST_HEAD(&res->lr_childof);
432 INIT_LIST_HEAD(&res->lr_granted);
433 INIT_LIST_HEAD(&res->lr_converting);
434 INIT_LIST_HEAD(&res->lr_waiting);
435 sema_init(&res->lr_lvb_sem, 1);
436 atomic_set(&res->lr_refcount, 1);
441 /* Args: locked namespace
442 * Returns: newly-allocated, referenced, unlocked resource */
443 static struct ldlm_resource *
444 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
445 struct ldlm_res_id name, __u32 type)
447 struct list_head *bucket;
448 struct ldlm_resource *res;
451 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
456 res = ldlm_resource_new();
462 spin_lock(&ns->ns_counter_lock);
464 spin_unlock(&ns->ns_counter_lock);
466 l_lock(&ns->ns_lock);
467 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
468 res->lr_namespace = ns;
472 res->lr_most_restr = LCK_NL;
474 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
475 list_add(&res->lr_hash, bucket);
477 if (parent == NULL) {
478 list_add(&res->lr_childof, &ns->ns_root_list);
480 res->lr_parent = parent;
481 list_add(&res->lr_childof, &parent->lr_children);
483 l_unlock(&ns->ns_lock);
489 /* Args: unlocked namespace
490 * Locks: takes and releases ns->ns_lock and res->lr_lock
491 * Returns: referenced, unlocked ldlm_resource or NULL */
492 struct ldlm_resource *
493 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
494 struct ldlm_res_id name, __u32 type, int create)
496 struct list_head *bucket, *tmp;
497 struct ldlm_resource *res = NULL;
501 LASSERT(ns->ns_hash != NULL);
502 LASSERT(name.name[0] != 0);
504 l_lock(&ns->ns_lock);
505 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
507 list_for_each(tmp, bucket) {
508 res = list_entry(tmp, struct ldlm_resource, lr_hash);
510 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
511 ldlm_resource_getref(res);
512 l_unlock(&ns->ns_lock);
518 res = ldlm_resource_add(ns, parent, name, type);
522 l_unlock(&ns->ns_lock);
524 if (create && ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
525 int rc = ns->ns_lvbo->lvbo_init(res);
527 CERROR("lvbo_init failed for resource "LPU64": rc %d\n",
534 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
536 LASSERT(res != NULL);
537 LASSERT(res != (void *)0x5a5a5a5a);
538 atomic_inc(&res->lr_refcount);
539 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
540 atomic_read(&res->lr_refcount));
544 /* Returns 1 if the resource was freed, 0 if it remains. */
545 int ldlm_resource_putref(struct ldlm_resource *res)
550 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
551 atomic_read(&res->lr_refcount) - 1);
552 LASSERT(atomic_read(&res->lr_refcount) > 0);
553 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
555 if (atomic_dec_and_test(&res->lr_refcount)) {
556 struct ldlm_namespace *ns = res->lr_namespace;
559 l_lock(&ns->ns_lock);
561 if (atomic_read(&res->lr_refcount) != 0) {
562 /* We lost the race. */
563 l_unlock(&ns->ns_lock);
567 if (!list_empty(&res->lr_granted)) {
568 ldlm_resource_dump(res);
572 if (!list_empty(&res->lr_converting)) {
573 ldlm_resource_dump(res);
577 if (!list_empty(&res->lr_waiting)) {
578 ldlm_resource_dump(res);
582 if (!list_empty(&res->lr_children)) {
583 ldlm_resource_dump(res);
588 list_del_init(&res->lr_hash);
589 list_del_init(&res->lr_childof);
590 if (res->lr_lvb_data)
591 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
592 l_unlock(&ns->ns_lock);
594 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
596 spin_lock(&ns->ns_counter_lock);
598 spin_unlock(&ns->ns_counter_lock);
607 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
608 struct ldlm_lock *lock)
610 l_lock(&res->lr_namespace->ns_lock);
612 ldlm_resource_dump(res);
613 CDEBUG(D_OTHER, "About to add this lock:\n");
614 ldlm_lock_dump(D_OTHER, lock, 0);
616 if (lock->l_destroyed) {
617 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
621 LASSERT(list_empty(&lock->l_res_link));
623 list_add_tail(&lock->l_res_link, head);
625 l_unlock(&res->lr_namespace->ns_lock);
628 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
629 struct ldlm_lock *new)
631 struct ldlm_resource *res = original->l_resource;
633 l_lock(&res->lr_namespace->ns_lock);
635 ldlm_resource_dump(res);
636 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
637 ldlm_lock_dump(D_OTHER, new, 0);
639 if (new->l_destroyed) {
640 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
644 LASSERT(list_empty(&new->l_res_link));
646 list_add(&new->l_res_link, &original->l_res_link);
648 l_unlock(&res->lr_namespace->ns_lock);
651 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
653 l_lock(&lock->l_resource->lr_namespace->ns_lock);
654 list_del_init(&lock->l_res_link);
655 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
658 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
660 desc->lr_type = res->lr_type;
661 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
664 void ldlm_dump_all_namespaces(void)
666 struct list_head *tmp;
668 spin_lock(&ldlm_namespace_lock);
670 list_for_each(tmp, &ldlm_namespace_list) {
671 struct ldlm_namespace *ns;
672 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
673 ldlm_namespace_dump(ns);
676 spin_unlock(&ldlm_namespace_lock);
679 void ldlm_namespace_dump(struct ldlm_namespace *ns)
681 struct list_head *tmp;
682 unsigned int debug_save = portal_debug;
684 portal_debug |= D_OTHER;
685 l_lock(&ns->ns_lock);
686 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
687 ns->ns_refcount, ns->ns_client);
689 list_for_each(tmp, &ns->ns_root_list) {
690 struct ldlm_resource *res;
691 res = list_entry(tmp, struct ldlm_resource, lr_childof);
693 /* Once we have resources with children, this should really dump
694 * them recursively. */
695 ldlm_resource_dump(res);
697 l_unlock(&ns->ns_lock);
698 portal_debug = debug_save;
701 void ldlm_resource_dump(struct ldlm_resource *res)
703 struct list_head *tmp;
706 if (RES_NAME_SIZE != 4)
709 CDEBUG(D_OTHER, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
710 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
711 res->lr_name.name[2], res->lr_name.name[3],
712 atomic_read(&res->lr_refcount));
714 if (!list_empty(&res->lr_granted)) {
716 CDEBUG(D_OTHER, "Granted locks:\n");
717 list_for_each(tmp, &res->lr_granted) {
718 struct ldlm_lock *lock;
719 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
720 ldlm_lock_dump(D_OTHER, lock, ++pos);
723 if (!list_empty(&res->lr_converting)) {
725 CDEBUG(D_OTHER, "Converting locks:\n");
726 list_for_each(tmp, &res->lr_converting) {
727 struct ldlm_lock *lock;
728 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
729 ldlm_lock_dump(D_OTHER, lock, ++pos);
732 if (!list_empty(&res->lr_waiting)) {
734 CDEBUG(D_OTHER, "Waiting locks:\n");
735 list_for_each(tmp, &res->lr_waiting) {
736 struct ldlm_lock *lock;
737 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
738 ldlm_lock_dump(D_OTHER, lock, ++pos);