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, unsigned long count, void *data)
45 ldlm_dump_all_namespaces();
49 int ldlm_proc_setup(void)
52 struct lprocfs_vars list[] = {
53 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
56 LASSERT(ldlm_ns_proc_dir == NULL);
58 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
61 if (IS_ERR(ldlm_type_proc_dir)) {
62 CERROR("LProcFS failed in ldlm-init\n");
63 rc = PTR_ERR(ldlm_type_proc_dir);
67 ldlm_ns_proc_dir = lprocfs_register("namespaces",
70 if (IS_ERR(ldlm_ns_proc_dir)) {
71 CERROR("LProcFS failed in ldlm-init\n");
72 rc = PTR_ERR(ldlm_ns_proc_dir);
76 ldlm_svc_proc_dir = lprocfs_register("services",
79 if (IS_ERR(ldlm_svc_proc_dir)) {
80 CERROR("LProcFS failed in ldlm-init\n");
81 rc = PTR_ERR(ldlm_svc_proc_dir);
85 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
90 lprocfs_remove(ldlm_ns_proc_dir);
92 lprocfs_remove(ldlm_type_proc_dir);
94 ldlm_type_proc_dir = NULL;
95 ldlm_ns_proc_dir = NULL;
96 ldlm_svc_proc_dir = NULL;
100 void ldlm_proc_cleanup(void)
102 if (ldlm_svc_proc_dir) {
103 lprocfs_remove(ldlm_svc_proc_dir);
104 ldlm_svc_proc_dir = NULL;
107 if (ldlm_ns_proc_dir) {
108 lprocfs_remove(ldlm_ns_proc_dir);
109 ldlm_ns_proc_dir = NULL;
112 if (ldlm_type_proc_dir) {
113 lprocfs_remove(ldlm_type_proc_dir);
114 ldlm_type_proc_dir = NULL;
118 static int lprocfs_uint_rd(char *page, char **start, off_t off,
119 int count, int *eof, void *data)
121 unsigned int *temp = (unsigned int *)data;
122 return snprintf(page, count, "%u\n", *temp);
125 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
126 int count, int *eof, void *data)
128 struct ldlm_namespace *ns = data;
129 return lprocfs_uint_rd(page, start, off, count, eof,
133 #define MAX_STRING_SIZE 128
134 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
135 unsigned long count, void *data)
137 struct ldlm_namespace *ns = data;
138 char dummy[MAX_STRING_SIZE + 1];
141 dummy[MAX_STRING_SIZE] = '\0';
142 copy_from_user(dummy, buffer, MAX_STRING_SIZE);
144 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
146 "dropping all unused locks from namespace %s\n",
148 tmp = ns->ns_max_unused;
149 ns->ns_max_unused = 0;
151 ns->ns_max_unused = tmp;
155 tmp = simple_strtoul(dummy, NULL, 0);
156 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
157 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
158 ns->ns_max_unused = (unsigned int)tmp;
165 void ldlm_proc_namespace(struct ldlm_namespace *ns)
167 struct lprocfs_vars lock_vars[2];
168 char lock_name[MAX_STRING_SIZE + 1];
171 LASSERT(ns->ns_name != NULL);
173 lock_name[MAX_STRING_SIZE] = '\0';
175 memset(lock_vars, 0, sizeof(lock_vars));
176 lock_vars[0].read_fptr = lprocfs_rd_u64;
177 lock_vars[0].name = lock_name;
179 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
180 lock_vars[0].data = &ns->ns_resources;
181 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
183 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
184 lock_vars[0].data = &ns->ns_locks;
185 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
188 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
190 lock_vars[0].data = &ns->ns_nr_unused;
191 lock_vars[0].read_fptr = lprocfs_uint_rd;
192 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
194 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
196 lock_vars[0].data = ns;
197 lock_vars[0].read_fptr = lprocfs_read_lru_size;
198 lock_vars[0].write_fptr = lprocfs_write_lru_size;
199 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
203 #undef MAX_STRING_SIZE
205 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
207 struct ldlm_namespace *ns = NULL;
208 struct list_head *bucket;
214 CERROR("ldlm_get_ref failed: %d\n", rc);
218 OBD_ALLOC(ns, sizeof(*ns));
222 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
226 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
228 GOTO(out_hash, NULL);
230 strcpy(ns->ns_name, name);
232 INIT_LIST_HEAD(&ns->ns_root_list);
233 l_lock_init(&ns->ns_lock);
235 ns->ns_client = client;
236 spin_lock_init(&ns->ns_counter_lock);
238 ns->ns_resources = 0;
240 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
242 INIT_LIST_HEAD(bucket);
244 INIT_LIST_HEAD(&ns->ns_unused_list);
245 ns->ns_nr_unused = 0;
246 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
248 spin_lock(&ldlm_namespace_lock);
249 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
250 spin_unlock(&ldlm_namespace_lock);
252 ldlm_proc_namespace(ns);
257 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
258 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
260 OBD_FREE(ns, sizeof(*ns));
266 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
268 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
269 * This is currently only used for recovery, and we make certain assumptions
270 * as a result--notably, that we shouldn't cancel locks with refs. -phil
272 * Called with the ns_lock held. */
273 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
276 struct list_head *tmp, *pos;
277 int rc = 0, client = res->lr_namespace->ns_client;
278 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
281 list_for_each_safe(tmp, pos, q) {
282 struct ldlm_lock *lock;
283 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
286 lock->l_flags |= LDLM_FL_CANCEL;
287 lock->l_flags |= flags;
289 if (local_only && (lock->l_readers || lock->l_writers)) {
290 /* This is a little bit gross, but much better than the
291 * alternative: pretend that we got a blocking AST from
292 * the server, so that when the lock is decref'd, it
293 * will go away ... */
294 lock->l_flags |= LDLM_FL_CBPENDING;
295 /* ... without sending a CANCEL message. */
296 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
297 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
298 if (lock->l_completion_ast)
299 lock->l_completion_ast(lock, 0, NULL);
305 struct lustre_handle lockh;
306 ldlm_lock2handle(lock, &lockh);
308 rc = ldlm_cli_cancel(&lockh);
310 CERROR("ldlm_cli_cancel: %d\n", rc);
312 /* Force local cleanup on errors, too. */
313 if (local_only || rc != ELDLM_OK)
314 ldlm_lock_cancel(lock);
316 LDLM_DEBUG(lock, "Freeing a lock still held by a "
319 ldlm_resource_unlink_lock(lock);
320 ldlm_lock_destroy(lock);
327 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
332 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
336 l_lock(&ns->ns_lock);
337 for (i = 0; i < RES_HASH_SIZE; i++) {
338 struct list_head *tmp, *pos;
339 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
340 struct ldlm_resource *res;
341 res = list_entry(tmp, struct ldlm_resource, lr_hash);
342 ldlm_resource_getref(res);
344 cleanup_resource(res, &res->lr_granted, flags);
345 cleanup_resource(res, &res->lr_converting, flags);
346 cleanup_resource(res, &res->lr_waiting, flags);
348 /* XXX what a mess: don't force cleanup if we're
349 * local_only (which is only used by recovery). In that
350 * case, we probably still have outstanding lock refs
351 * which reference these resources. -phil */
352 if (!ldlm_resource_putref(res) &&
353 !(flags & LDLM_FL_LOCAL_ONLY)) {
354 CERROR("Resource refcount nonzero (%d) after "
355 "lock cleanup; forcing cleanup.\n",
356 atomic_read(&res->lr_refcount));
357 ldlm_resource_dump(res);
358 atomic_set(&res->lr_refcount, 1);
359 ldlm_resource_putref(res);
363 l_unlock(&ns->ns_lock);
368 /* Cleanup, but also free, the namespace */
369 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
374 spin_lock(&ldlm_namespace_lock);
375 list_del(&ns->ns_list_chain);
377 spin_unlock(&ldlm_namespace_lock);
379 /* At shutdown time, don't call the cancellation callback */
380 ldlm_namespace_cleanup(ns, 0);
384 struct proc_dir_entry *dir;
385 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
387 CERROR("dlm namespace %s has no procfs dir?\n",
395 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
396 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
397 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
398 OBD_FREE(ns, sizeof(*ns));
405 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
410 for (i = 0; i < RES_NAME_SIZE; i++)
411 hash += name.name[i];
413 hash += (__u32)((unsigned long)parent >> 4);
415 return (hash & RES_HASH_MASK);
418 static struct ldlm_resource *ldlm_resource_new(void)
420 struct ldlm_resource *res;
422 OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_NOFS, sizeof *res);
427 memset(res, 0, sizeof(*res));
429 INIT_LIST_HEAD(&res->lr_children);
430 INIT_LIST_HEAD(&res->lr_childof);
431 INIT_LIST_HEAD(&res->lr_granted);
432 INIT_LIST_HEAD(&res->lr_converting);
433 INIT_LIST_HEAD(&res->lr_waiting);
435 atomic_set(&res->lr_refcount, 1);
440 /* Args: locked namespace
441 * Returns: newly-allocated, referenced, unlocked resource */
442 static struct ldlm_resource *
443 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
444 struct ldlm_res_id name, __u32 type)
446 struct list_head *bucket;
447 struct ldlm_resource *res;
450 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
455 res = ldlm_resource_new();
461 spin_lock(&ns->ns_counter_lock);
463 spin_unlock(&ns->ns_counter_lock);
465 l_lock(&ns->ns_lock);
466 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
467 res->lr_namespace = ns;
471 res->lr_most_restr = LCK_NL;
473 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
474 list_add(&res->lr_hash, bucket);
476 if (parent == NULL) {
477 list_add(&res->lr_childof, &ns->ns_root_list);
479 res->lr_parent = parent;
480 list_add(&res->lr_childof, &parent->lr_children);
482 l_unlock(&ns->ns_lock);
487 /* Args: unlocked namespace
488 * Locks: takes and releases ns->ns_lock and res->lr_lock
489 * Returns: referenced, unlocked ldlm_resource or NULL */
490 struct ldlm_resource *
491 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
492 struct ldlm_res_id name, __u32 type, int create)
494 struct list_head *bucket, *tmp;
495 struct ldlm_resource *res = NULL;
499 LASSERT(ns->ns_hash != NULL);
500 LASSERT(name.name[0] != 0);
502 l_lock(&ns->ns_lock);
503 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
505 list_for_each(tmp, bucket) {
506 res = list_entry(tmp, struct ldlm_resource, lr_hash);
508 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
509 ldlm_resource_getref(res);
510 l_unlock(&ns->ns_lock);
516 res = ldlm_resource_add(ns, parent, name, type);
520 l_unlock(&ns->ns_lock);
525 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
527 LASSERT(res != NULL);
528 LASSERT(res != (void *)0x5a5a5a5a);
529 atomic_inc(&res->lr_refcount);
530 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
531 atomic_read(&res->lr_refcount));
535 /* Returns 1 if the resource was freed, 0 if it remains. */
536 int ldlm_resource_putref(struct ldlm_resource *res)
541 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
542 atomic_read(&res->lr_refcount) - 1);
543 LASSERT(atomic_read(&res->lr_refcount) > 0);
544 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
546 if (atomic_dec_and_test(&res->lr_refcount)) {
547 struct ldlm_namespace *ns = res->lr_namespace;
550 l_lock(&ns->ns_lock);
552 if (atomic_read(&res->lr_refcount) != 0) {
553 /* We lost the race. */
554 l_unlock(&ns->ns_lock);
558 if (!list_empty(&res->lr_granted)) {
559 ldlm_resource_dump(res);
563 if (!list_empty(&res->lr_converting)) {
564 ldlm_resource_dump(res);
568 if (!list_empty(&res->lr_waiting)) {
569 ldlm_resource_dump(res);
573 if (!list_empty(&res->lr_children)) {
574 ldlm_resource_dump(res);
579 list_del_init(&res->lr_hash);
580 list_del_init(&res->lr_childof);
581 l_unlock(&ns->ns_lock);
583 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
585 spin_lock(&ns->ns_counter_lock);
587 spin_unlock(&ns->ns_counter_lock);
596 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
597 struct ldlm_lock *lock)
599 l_lock(&res->lr_namespace->ns_lock);
601 ldlm_resource_dump(res);
602 CDEBUG(D_OTHER, "About to add this lock:\n");
603 ldlm_lock_dump(D_OTHER, lock, 0);
605 if (lock->l_destroyed) {
606 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
610 LASSERT(list_empty(&lock->l_res_link));
612 list_add_tail(&lock->l_res_link, head);
614 l_unlock(&res->lr_namespace->ns_lock);
617 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
619 l_lock(&lock->l_resource->lr_namespace->ns_lock);
620 list_del_init(&lock->l_res_link);
621 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
624 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
626 desc->lr_type = res->lr_type;
627 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
628 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
631 void ldlm_dump_all_namespaces(void)
633 struct list_head *tmp;
635 spin_lock(&ldlm_namespace_lock);
637 list_for_each(tmp, &ldlm_namespace_list) {
638 struct ldlm_namespace *ns;
639 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
640 ldlm_namespace_dump(ns);
643 spin_unlock(&ldlm_namespace_lock);
646 void ldlm_namespace_dump(struct ldlm_namespace *ns)
648 struct list_head *tmp;
650 l_lock(&ns->ns_lock);
651 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
652 ns->ns_refcount, ns->ns_client);
654 list_for_each(tmp, &ns->ns_root_list) {
655 struct ldlm_resource *res;
656 res = list_entry(tmp, struct ldlm_resource, lr_childof);
658 /* Once we have resources with children, this should really dump
659 * them recursively. */
660 ldlm_resource_dump(res);
662 l_unlock(&ns->ns_lock);
665 void ldlm_resource_dump(struct ldlm_resource *res)
667 struct list_head *tmp;
670 if (RES_NAME_SIZE != 4)
673 CDEBUG(D_OTHER, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
674 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
675 res->lr_name.name[2], res->lr_name.name[3],
676 atomic_read(&res->lr_refcount));
678 if (!list_empty(&res->lr_granted)) {
680 CDEBUG(D_OTHER, "Granted locks:\n");
681 list_for_each(tmp, &res->lr_granted) {
682 struct ldlm_lock *lock;
683 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
684 ldlm_lock_dump(D_OTHER, lock, ++pos);
687 if (!list_empty(&res->lr_converting)) {
689 CDEBUG(D_OTHER, "Converting locks:\n");
690 list_for_each(tmp, &res->lr_converting) {
691 struct ldlm_lock *lock;
692 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
693 ldlm_lock_dump(D_OTHER, lock, ++pos);
696 if (!list_empty(&res->lr_waiting)) {
698 CDEBUG(D_OTHER, "Waiting locks:\n");
699 list_for_each(tmp, &res->lr_waiting) {
700 struct ldlm_lock *lock;
701 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
702 ldlm_lock_dump(D_OTHER, lock, ++pos);