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], *end;
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;
151 ldlm_cancel_lru(ns, LDLM_SYNC);
152 ns->ns_max_unused = tmp;
156 tmp = simple_strtoul(dummy, &end, 0);
157 if (tmp == 0 && *end) {
158 CERROR("invalid value written\n");
162 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
163 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
164 ns->ns_max_unused = (unsigned int)tmp;
166 ldlm_cancel_lru(ns, LDLM_ASYNC);
171 void ldlm_proc_namespace(struct ldlm_namespace *ns)
173 struct lprocfs_vars lock_vars[2];
174 char lock_name[MAX_STRING_SIZE + 1];
177 LASSERT(ns->ns_name != NULL);
179 lock_name[MAX_STRING_SIZE] = '\0';
181 memset(lock_vars, 0, sizeof(lock_vars));
182 lock_vars[0].read_fptr = lprocfs_rd_u64;
183 lock_vars[0].name = lock_name;
185 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
186 lock_vars[0].data = &ns->ns_resources;
187 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
189 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
190 lock_vars[0].data = &ns->ns_locks;
191 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
194 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
196 lock_vars[0].data = &ns->ns_nr_unused;
197 lock_vars[0].read_fptr = lprocfs_uint_rd;
198 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
200 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
202 lock_vars[0].data = ns;
203 lock_vars[0].read_fptr = lprocfs_read_lru_size;
204 lock_vars[0].write_fptr = lprocfs_write_lru_size;
205 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
209 #undef MAX_STRING_SIZE
211 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
213 struct ldlm_namespace *ns = NULL;
214 struct list_head *bucket;
220 CERROR("ldlm_get_ref failed: %d\n", rc);
224 OBD_ALLOC(ns, sizeof(*ns));
228 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
232 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
234 GOTO(out_hash, NULL);
236 strcpy(ns->ns_name, name);
238 INIT_LIST_HEAD(&ns->ns_root_list);
239 l_lock_init(&ns->ns_lock);
241 ns->ns_client = client;
242 spin_lock_init(&ns->ns_counter_lock);
244 ns->ns_resources = 0;
245 init_waitqueue_head(&ns->ns_waitq);
247 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
249 INIT_LIST_HEAD(bucket);
251 INIT_LIST_HEAD(&ns->ns_unused_list);
252 ns->ns_nr_unused = 0;
253 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
255 spin_lock(&ldlm_namespace_lock);
256 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
257 spin_unlock(&ldlm_namespace_lock);
259 ldlm_proc_namespace(ns);
264 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
265 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
267 OBD_FREE(ns, sizeof(*ns));
273 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
275 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
276 * This is currently only used for recovery, and we make certain assumptions
277 * as a result--notably, that we shouldn't cancel locks with refs. -phil
279 * Called with the ns_lock held. */
280 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
283 struct list_head *tmp, *pos;
284 int rc = 0, client = res->lr_namespace->ns_client;
285 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
288 list_for_each_safe(tmp, pos, q) {
289 struct ldlm_lock *lock;
290 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
293 /* Set CBPENDING so nothing in the cancellation path
294 * can match this lock */
295 lock->l_flags |= LDLM_FL_CBPENDING;
296 lock->l_flags |= LDLM_FL_FAILED;
297 lock->l_flags |= flags;
299 if (local_only && (lock->l_readers || lock->l_writers)) {
300 /* This is a little bit gross, but much better than the
301 * alternative: pretend that we got a blocking AST from
302 * the server, so that when the lock is decref'd, it
303 * will go away ... */
304 /* ... without sending a CANCEL message. */
305 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
306 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
307 if (lock->l_completion_ast)
308 lock->l_completion_ast(lock, 0, NULL);
314 struct lustre_handle lockh;
315 ldlm_lock2handle(lock, &lockh);
317 rc = ldlm_cli_cancel(&lockh);
319 CERROR("ldlm_cli_cancel: %d\n", rc);
321 /* Force local cleanup on errors, too. */
322 if (local_only || rc != ELDLM_OK)
323 ldlm_lock_cancel(lock);
325 LDLM_DEBUG(lock, "Freeing a lock still held by a "
328 ldlm_resource_unlink_lock(lock);
329 ldlm_lock_destroy(lock);
336 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
341 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
345 l_lock(&ns->ns_lock);
346 for (i = 0; i < RES_HASH_SIZE; i++) {
347 struct list_head *tmp, *pos;
348 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
349 struct ldlm_resource *res;
350 res = list_entry(tmp, struct ldlm_resource, lr_hash);
351 ldlm_resource_getref(res);
353 cleanup_resource(res, &res->lr_granted, flags);
354 cleanup_resource(res, &res->lr_converting, flags);
355 cleanup_resource(res, &res->lr_waiting, flags);
357 /* XXX what a mess: don't force cleanup if we're
358 * local_only (which is only used by recovery). In that
359 * case, we probably still have outstanding lock refs
360 * which reference these resources. -phil */
361 if (!ldlm_resource_putref(res) &&
362 !(flags & LDLM_FL_LOCAL_ONLY)) {
363 CERROR("Resource refcount nonzero (%d) after "
364 "lock cleanup; forcing cleanup.\n",
365 atomic_read(&res->lr_refcount));
366 ldlm_resource_dump(res);
367 atomic_set(&res->lr_refcount, 1);
368 ldlm_resource_putref(res);
372 l_unlock(&ns->ns_lock);
377 /* Cleanup, but also free, the namespace */
378 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
383 spin_lock(&ldlm_namespace_lock);
384 list_del(&ns->ns_list_chain);
386 spin_unlock(&ldlm_namespace_lock);
388 /* At shutdown time, don't call the cancellation callback */
389 ldlm_namespace_cleanup(ns, 0);
393 struct proc_dir_entry *dir;
394 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
396 CERROR("dlm namespace %s has no procfs dir?\n",
404 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
405 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
406 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
407 OBD_FREE(ns, sizeof(*ns));
414 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
419 for (i = 0; i < RES_NAME_SIZE; i++)
420 hash += name.name[i];
422 hash += (__u32)((unsigned long)parent >> 4);
424 return (hash & RES_HASH_MASK);
427 static struct ldlm_resource *ldlm_resource_new(void)
429 struct ldlm_resource *res;
431 OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
436 memset(res, 0, sizeof(*res));
438 INIT_LIST_HEAD(&res->lr_children);
439 INIT_LIST_HEAD(&res->lr_childof);
440 INIT_LIST_HEAD(&res->lr_granted);
441 INIT_LIST_HEAD(&res->lr_converting);
442 INIT_LIST_HEAD(&res->lr_waiting);
443 sema_init(&res->lr_lvb_sem, 1);
444 atomic_set(&res->lr_refcount, 1);
449 /* Args: locked namespace
450 * Returns: newly-allocated, referenced, unlocked resource */
451 static struct ldlm_resource *
452 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
453 struct ldlm_res_id name, __u32 type)
455 struct list_head *bucket;
456 struct ldlm_resource *res;
459 LASSERTF(type >= LDLM_MIN_TYPE && type <= LDLM_MAX_TYPE,
462 res = ldlm_resource_new();
468 spin_lock(&ns->ns_counter_lock);
470 spin_unlock(&ns->ns_counter_lock);
472 l_lock(&ns->ns_lock);
473 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
474 res->lr_namespace = ns;
478 res->lr_most_restr = LCK_NL;
480 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
481 list_add(&res->lr_hash, bucket);
483 if (parent == NULL) {
484 list_add(&res->lr_childof, &ns->ns_root_list);
486 res->lr_parent = parent;
487 list_add(&res->lr_childof, &parent->lr_children);
489 l_unlock(&ns->ns_lock);
495 /* Args: unlocked namespace
496 * Locks: takes and releases ns->ns_lock and res->lr_lock
497 * Returns: referenced, unlocked ldlm_resource or NULL */
498 struct ldlm_resource *
499 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
500 struct ldlm_res_id name, __u32 type, int create)
502 struct list_head *bucket, *tmp;
503 struct ldlm_resource *res = NULL;
507 LASSERT(ns->ns_hash != NULL);
508 LASSERT(name.name[0] != 0);
510 l_lock(&ns->ns_lock);
511 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
513 list_for_each(tmp, bucket) {
514 res = list_entry(tmp, struct ldlm_resource, lr_hash);
516 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
517 ldlm_resource_getref(res);
518 l_unlock(&ns->ns_lock);
524 res = ldlm_resource_add(ns, parent, name, type);
529 if (create && ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
532 /* Although this is technically a lock inversion risk (lvb_sem
533 * should be taken before DLM lock), this resource was just
534 * created, so nobody else can take the lvb_sem yet. -p */
535 down(&res->lr_lvb_sem);
536 /* Drop the dlm lock, because lvbo_init can touch the disk */
537 l_unlock(&ns->ns_lock);
538 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
539 rc = ns->ns_lvbo->lvbo_init(res);
540 up(&res->lr_lvb_sem);
542 CERROR("lvbo_init failed for resource "LPU64": rc %d\n",
545 l_unlock(&ns->ns_lock);
551 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
553 LASSERT(res != NULL);
554 LASSERT(res != LP_POISON);
555 atomic_inc(&res->lr_refcount);
556 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
557 atomic_read(&res->lr_refcount));
561 /* Returns 1 if the resource was freed, 0 if it remains. */
562 int ldlm_resource_putref(struct ldlm_resource *res)
567 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
568 atomic_read(&res->lr_refcount) - 1);
569 LASSERT(atomic_read(&res->lr_refcount) > 0);
570 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
572 if (atomic_dec_and_test(&res->lr_refcount)) {
573 struct ldlm_namespace *ns = res->lr_namespace;
576 l_lock(&ns->ns_lock);
578 if (atomic_read(&res->lr_refcount) != 0) {
579 /* We lost the race. */
580 l_unlock(&ns->ns_lock);
584 if (!list_empty(&res->lr_granted)) {
585 ldlm_resource_dump(res);
589 if (!list_empty(&res->lr_converting)) {
590 ldlm_resource_dump(res);
594 if (!list_empty(&res->lr_waiting)) {
595 ldlm_resource_dump(res);
599 if (!list_empty(&res->lr_children)) {
600 ldlm_resource_dump(res);
605 list_del_init(&res->lr_hash);
606 list_del_init(&res->lr_childof);
607 if (res->lr_lvb_data)
608 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
609 l_unlock(&ns->ns_lock);
611 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
613 spin_lock(&ns->ns_counter_lock);
615 if (ns->ns_resources == 0)
616 wake_up(&ns->ns_waitq);
617 spin_unlock(&ns->ns_counter_lock);
626 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
627 struct ldlm_lock *lock)
629 l_lock(&res->lr_namespace->ns_lock);
631 ldlm_resource_dump(res);
632 CDEBUG(D_OTHER, "About to add this lock:\n");
633 ldlm_lock_dump(D_OTHER, lock, 0);
635 if (lock->l_destroyed) {
636 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
640 LASSERT(list_empty(&lock->l_res_link));
642 list_add_tail(&lock->l_res_link, head);
644 l_unlock(&res->lr_namespace->ns_lock);
647 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
648 struct ldlm_lock *new)
650 struct ldlm_resource *res = original->l_resource;
652 l_lock(&res->lr_namespace->ns_lock);
654 ldlm_resource_dump(res);
655 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
656 ldlm_lock_dump(D_OTHER, new, 0);
658 if (new->l_destroyed) {
659 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
663 LASSERT(list_empty(&new->l_res_link));
665 list_add(&new->l_res_link, &original->l_res_link);
667 l_unlock(&res->lr_namespace->ns_lock);
670 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
672 l_lock(&lock->l_resource->lr_namespace->ns_lock);
673 list_del_init(&lock->l_res_link);
674 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
677 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
679 desc->lr_type = res->lr_type;
680 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
683 void ldlm_dump_all_namespaces(void)
685 struct list_head *tmp;
687 spin_lock(&ldlm_namespace_lock);
689 list_for_each(tmp, &ldlm_namespace_list) {
690 struct ldlm_namespace *ns;
691 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
692 ldlm_namespace_dump(ns);
695 spin_unlock(&ldlm_namespace_lock);
698 void ldlm_namespace_dump(struct ldlm_namespace *ns)
700 struct list_head *tmp;
701 unsigned int debug_save = portal_debug;
703 portal_debug |= D_OTHER;
704 l_lock(&ns->ns_lock);
705 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
706 ns->ns_refcount, ns->ns_client);
708 list_for_each(tmp, &ns->ns_root_list) {
709 struct ldlm_resource *res;
710 res = list_entry(tmp, struct ldlm_resource, lr_childof);
712 /* Once we have resources with children, this should really dump
713 * them recursively. */
714 ldlm_resource_dump(res);
716 l_unlock(&ns->ns_lock);
717 portal_debug = debug_save;
720 void ldlm_resource_dump(struct ldlm_resource *res)
722 struct list_head *tmp;
725 if (RES_NAME_SIZE != 4)
728 CDEBUG(D_OTHER, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
729 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
730 res->lr_name.name[2], res->lr_name.name[3],
731 atomic_read(&res->lr_refcount));
733 if (!list_empty(&res->lr_granted)) {
735 CDEBUG(D_OTHER, "Granted locks:\n");
736 list_for_each(tmp, &res->lr_granted) {
737 struct ldlm_lock *lock;
738 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
739 ldlm_lock_dump(D_OTHER, lock, ++pos);
742 if (!list_empty(&res->lr_converting)) {
744 CDEBUG(D_OTHER, "Converting locks:\n");
745 list_for_each(tmp, &res->lr_converting) {
746 struct ldlm_lock *lock;
747 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
748 ldlm_lock_dump(D_OTHER, lock, ++pos);
751 if (!list_empty(&res->lr_waiting)) {
753 CDEBUG(D_OTHER, "Waiting locks:\n");
754 list_for_each(tmp, &res->lr_waiting) {
755 struct ldlm_lock *lock;
756 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
757 ldlm_lock_dump(D_OTHER, lock, ++pos);