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 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
40 int ldlm_proc_setup(struct obd_device *obd)
44 LASSERT(ldlm_ns_proc_dir == NULL);
46 rc = lprocfs_obd_attach(obd, 0);
48 CERROR("LProcFS failed in ldlm-init\n");
51 ldlm_ns_proc_dir = obd->obd_proc_entry;
55 void ldlm_proc_cleanup(struct obd_device *obd)
57 if (ldlm_ns_proc_dir) {
58 lprocfs_obd_detach(obd);
59 ldlm_ns_proc_dir = NULL;
64 static int lprocfs_uint_rd(char *page, char **start, off_t off,
65 int count, int *eof, void *data)
67 unsigned int *temp = (unsigned int *)data;
68 return snprintf(page, count, "%u\n", *temp);
71 static int lprocfs_read_lru_size(char *page, char **start, off_t off,
72 int count, int *eof, void *data)
74 struct ldlm_namespace *ns = data;
75 return lprocfs_uint_rd(page, start, off, count, eof,
79 #define MAX_STRING_SIZE 128
80 static int lprocfs_write_lru_size(struct file *file, const char *buffer,
81 unsigned long count, void *data)
83 struct ldlm_namespace *ns = data;
84 char dummy[MAX_STRING_SIZE + 1];
87 dummy[MAX_STRING_SIZE] = '\0';
88 copy_from_user(dummy, buffer, MAX_STRING_SIZE);
90 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
92 "dropping all unused locks from namespace %s\n",
94 tmp = ns->ns_max_unused;
95 ns->ns_max_unused = 0;
97 ns->ns_max_unused = tmp;
101 tmp = simple_strtoul(dummy, NULL, 0);
102 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
103 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
104 ns->ns_max_unused = (unsigned int)tmp;
111 void ldlm_proc_namespace(struct ldlm_namespace *ns)
113 struct lprocfs_vars lock_vars[2];
114 char lock_name[MAX_STRING_SIZE + 1];
117 LASSERT(ns->ns_name != NULL);
119 lock_name[MAX_STRING_SIZE] = '\0';
121 memset(lock_vars, 0, sizeof(lock_vars));
122 lock_vars[0].read_fptr = lprocfs_rd_u64;
123 lock_vars[0].name = lock_name;
125 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
126 lock_vars[0].data = &ns->ns_resources;
127 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
129 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
130 lock_vars[0].data = &ns->ns_locks;
131 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
134 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
136 lock_vars[0].data = &ns->ns_nr_unused;
137 lock_vars[0].read_fptr = lprocfs_uint_rd;
138 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
140 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
142 lock_vars[0].data = ns;
143 lock_vars[0].read_fptr = lprocfs_read_lru_size;
144 lock_vars[0].write_fptr = lprocfs_write_lru_size;
145 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
149 #undef MAX_STRING_SIZE
151 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
153 struct ldlm_namespace *ns = NULL;
154 struct list_head *bucket;
157 OBD_ALLOC(ns, sizeof(*ns));
161 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
165 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
167 GOTO(out_hash, NULL);
169 strcpy(ns->ns_name, name);
171 INIT_LIST_HEAD(&ns->ns_root_list);
172 l_lock_init(&ns->ns_lock);
174 ns->ns_client = client;
175 spin_lock_init(&ns->ns_counter_lock);
177 ns->ns_resources = 0;
179 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
181 INIT_LIST_HEAD(bucket);
183 INIT_LIST_HEAD(&ns->ns_unused_list);
184 ns->ns_nr_unused = 0;
185 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
187 spin_lock(&ldlm_namespace_lock);
188 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
189 spin_unlock(&ldlm_namespace_lock);
191 ldlm_proc_namespace(ns);
196 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
197 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
199 OBD_FREE(ns, sizeof(*ns));
203 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
205 /* If 'local_only' is true, don't try to tell the server, just cleanup.
206 * This is currently only used for recovery, and we make certain assumptions
207 * as a result--notably, that we shouldn't cancel locks with refs. -phil
209 * Called with the ns_lock held. */
210 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
213 struct list_head *tmp, *pos;
214 int rc = 0, client = res->lr_namespace->ns_client;
217 list_for_each_safe(tmp, pos, q) {
218 struct ldlm_lock *lock;
219 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
222 if (local_only && (lock->l_readers || lock->l_writers)) {
223 /* This is a little bit gross, but much better than the
224 * alternative: pretend that we got a blocking AST from
225 * the server, so that when the lock is decref'd, it
226 * will go away ... */
227 lock->l_flags |= LDLM_FL_CBPENDING;
228 /* ... without sending a CANCEL message. */
229 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
230 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
231 /* ... and without calling the cancellation callback */
232 lock->l_flags |= LDLM_FL_CANCEL;
237 /* At shutdown time, don't call the cancellation callback */
238 lock->l_flags |= LDLM_FL_CANCEL;
241 struct lustre_handle lockh;
242 ldlm_lock2handle(lock, &lockh);
244 rc = ldlm_cli_cancel(&lockh);
246 CERROR("ldlm_cli_cancel: %d\n", rc);
248 /* Force local cleanup on errors, too. */
249 if (local_only || rc != ELDLM_OK)
250 ldlm_lock_cancel(lock);
252 LDLM_DEBUG(lock, "Freeing a lock still held by a "
255 ldlm_resource_unlink_lock(lock);
256 ldlm_lock_destroy(lock);
263 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
268 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
272 l_lock(&ns->ns_lock);
273 for (i = 0; i < RES_HASH_SIZE; i++) {
274 struct list_head *tmp, *pos;
275 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
276 struct ldlm_resource *res;
277 res = list_entry(tmp, struct ldlm_resource, lr_hash);
278 ldlm_resource_getref(res);
280 cleanup_resource(res, &res->lr_granted, local_only);
281 cleanup_resource(res, &res->lr_converting, local_only);
282 cleanup_resource(res, &res->lr_waiting, local_only);
284 /* XXX what a mess: don't force cleanup if we're
285 * local_only (which is only used by recovery). In that
286 * case, we probably still have outstanding lock refs
287 * which reference these resources. -phil */
288 if (!ldlm_resource_putref(res) && !local_only) {
289 CERROR("Resource refcount nonzero (%d) after "
290 "lock cleanup; forcing cleanup.\n",
291 atomic_read(&res->lr_refcount));
292 ldlm_resource_dump(res);
293 atomic_set(&res->lr_refcount, 1);
294 ldlm_resource_putref(res);
298 l_unlock(&ns->ns_lock);
303 /* Cleanup, but also free, the namespace */
304 int ldlm_namespace_free(struct ldlm_namespace *ns)
309 spin_lock(&ldlm_namespace_lock);
310 list_del(&ns->ns_list_chain);
312 spin_unlock(&ldlm_namespace_lock);
314 ldlm_namespace_cleanup(ns, 0);
316 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
317 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
318 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
319 OBD_FREE(ns, sizeof(*ns));
324 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
329 for (i = 0; i < RES_NAME_SIZE; i++)
330 hash += name.name[i];
332 hash += (__u32)((unsigned long)parent >> 4);
334 return (hash & RES_HASH_MASK);
337 static struct ldlm_resource *ldlm_resource_new(void)
339 struct ldlm_resource *res;
341 OBD_SLAB_ALLOC(res, ldlm_resource_slab, SLAB_KERNEL, sizeof *res);
346 memset(res, 0, sizeof(*res));
348 INIT_LIST_HEAD(&res->lr_children);
349 INIT_LIST_HEAD(&res->lr_childof);
350 INIT_LIST_HEAD(&res->lr_granted);
351 INIT_LIST_HEAD(&res->lr_converting);
352 INIT_LIST_HEAD(&res->lr_waiting);
354 atomic_set(&res->lr_refcount, 1);
359 /* Args: locked namespace
360 * Returns: newly-allocated, referenced, unlocked resource */
361 static struct ldlm_resource *
362 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
363 struct ldlm_res_id name, __u32 type)
365 struct list_head *bucket;
366 struct ldlm_resource *res;
369 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
374 res = ldlm_resource_new();
380 spin_lock(&ns->ns_counter_lock);
382 spin_unlock(&ns->ns_counter_lock);
384 l_lock(&ns->ns_lock);
385 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
386 res->lr_namespace = ns;
390 res->lr_most_restr = LCK_NL;
392 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
393 list_add(&res->lr_hash, bucket);
395 if (parent == NULL) {
396 list_add(&res->lr_childof, &ns->ns_root_list);
398 res->lr_parent = parent;
399 list_add(&res->lr_childof, &parent->lr_children);
401 l_unlock(&ns->ns_lock);
406 /* Args: unlocked namespace
407 * Locks: takes and releases ns->ns_lock and res->lr_lock
408 * Returns: referenced, unlocked ldlm_resource or NULL */
409 struct ldlm_resource *
410 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
411 struct ldlm_res_id name, __u32 type, int create)
413 struct list_head *bucket, *tmp;
414 struct ldlm_resource *res = NULL;
418 LASSERT(ns->ns_hash != NULL);
420 l_lock(&ns->ns_lock);
421 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
423 list_for_each(tmp, bucket) {
424 res = list_entry(tmp, struct ldlm_resource, lr_hash);
426 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
427 ldlm_resource_getref(res);
428 l_unlock(&ns->ns_lock);
434 res = ldlm_resource_add(ns, parent, name, type);
438 l_unlock(&ns->ns_lock);
443 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
445 atomic_inc(&res->lr_refcount);
446 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
447 atomic_read(&res->lr_refcount));
451 /* Returns 1 if the resource was freed, 0 if it remains. */
452 int ldlm_resource_putref(struct ldlm_resource *res)
457 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
458 atomic_read(&res->lr_refcount) - 1);
459 LASSERT(atomic_read(&res->lr_refcount) > 0);
460 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
462 if (atomic_dec_and_test(&res->lr_refcount)) {
463 struct ldlm_namespace *ns = res->lr_namespace;
466 l_lock(&ns->ns_lock);
468 if (atomic_read(&res->lr_refcount) != 0) {
469 /* We lost the race. */
470 l_unlock(&ns->ns_lock);
474 if (!list_empty(&res->lr_granted)) {
475 ldlm_resource_dump(res);
479 if (!list_empty(&res->lr_converting)) {
480 ldlm_resource_dump(res);
484 if (!list_empty(&res->lr_waiting)) {
485 ldlm_resource_dump(res);
489 if (!list_empty(&res->lr_children)) {
490 ldlm_resource_dump(res);
495 list_del_init(&res->lr_hash);
496 list_del_init(&res->lr_childof);
498 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
499 l_unlock(&ns->ns_lock);
501 spin_lock(&ns->ns_counter_lock);
503 spin_unlock(&ns->ns_counter_lock);
512 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
513 struct ldlm_lock *lock)
515 l_lock(&res->lr_namespace->ns_lock);
517 ldlm_resource_dump(res);
518 CDEBUG(D_OTHER, "About to add this lock:\n");
519 ldlm_lock_dump(D_OTHER, lock);
521 if (lock->l_destroyed) {
522 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
526 LASSERT(list_empty(&lock->l_res_link));
528 list_add_tail(&lock->l_res_link, head);
529 l_unlock(&res->lr_namespace->ns_lock);
532 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
534 l_lock(&lock->l_resource->lr_namespace->ns_lock);
535 list_del_init(&lock->l_res_link);
536 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
539 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
541 desc->lr_type = res->lr_type;
542 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
543 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
546 void ldlm_dump_all_namespaces(void)
548 struct list_head *tmp;
550 spin_lock(&ldlm_namespace_lock);
552 list_for_each(tmp, &ldlm_namespace_list) {
553 struct ldlm_namespace *ns;
554 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
555 ldlm_namespace_dump(ns);
558 spin_unlock(&ldlm_namespace_lock);
561 void ldlm_namespace_dump(struct ldlm_namespace *ns)
563 struct list_head *tmp;
565 l_lock(&ns->ns_lock);
566 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
567 ns->ns_refcount, ns->ns_client);
569 list_for_each(tmp, &ns->ns_root_list) {
570 struct ldlm_resource *res;
571 res = list_entry(tmp, struct ldlm_resource, lr_childof);
573 /* Once we have resources with children, this should really dump
574 * them recursively. */
575 ldlm_resource_dump(res);
577 l_unlock(&ns->ns_lock);
580 void ldlm_resource_dump(struct ldlm_resource *res)
582 struct list_head *tmp;
585 if (RES_NAME_SIZE != 3)
588 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
589 (unsigned long long)res->lr_name.name[0],
590 (unsigned long long)res->lr_name.name[1],
591 (unsigned long long)res->lr_name.name[2]);
593 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
594 atomic_read(&res->lr_refcount));
595 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
596 res->lr_namespace->ns_name);
597 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
599 CDEBUG(D_OTHER, "Granted locks:\n");
600 list_for_each(tmp, &res->lr_granted) {
601 struct ldlm_lock *lock;
602 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
603 ldlm_lock_dump(D_OTHER, lock);
606 CDEBUG(D_OTHER, "Converting locks:\n");
607 list_for_each(tmp, &res->lr_converting) {
608 struct ldlm_lock *lock;
609 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
610 ldlm_lock_dump(D_OTHER, lock);
613 CDEBUG(D_OTHER, "Waiting locks:\n");
614 list_for_each(tmp, &res->lr_waiting) {
615 struct ldlm_lock *lock;
616 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
617 ldlm_lock_dump(D_OTHER, lock);