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>
33 kmem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
35 spinlock_t ldlm_namespace_lock = SPIN_LOCK_UNLOCKED;
36 struct list_head ldlm_namespace_list = LIST_HEAD_INIT(ldlm_namespace_list);
37 static struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
39 int ldlm_proc_setup(struct obd_device *obd)
43 LASSERT(ldlm_ns_proc_dir == NULL);
45 rc = lprocfs_obd_attach(obd, 0);
47 CERROR("LProcFS failed in ldlm-init\n");
50 ldlm_ns_proc_dir = obd->obd_proc_entry;
54 void ldlm_proc_cleanup(struct obd_device *obd)
56 if (ldlm_ns_proc_dir) {
57 lprocfs_obd_detach(obd);
58 ldlm_ns_proc_dir = NULL;
63 static int lprocfs_uint_rd(char *page, char **start, off_t off,
64 int count, int *eof, void *data)
66 unsigned int *temp = (unsigned int *)data;
67 return snprintf(page, count, "%u\n", *temp);
71 #define MAX_STRING_SIZE 128
72 void ldlm_proc_namespace(struct ldlm_namespace *ns)
74 struct lprocfs_vars lock_vars[2];
75 char lock_name[MAX_STRING_SIZE + 1];
78 LASSERT(ns->ns_name != NULL);
80 lock_name[MAX_STRING_SIZE] = '\0';
82 memset(lock_vars, 0, sizeof(lock_vars));
83 lock_vars[0].read_fptr = lprocfs_rd_u64;
85 lock_vars[0].name = lock_name;
87 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
89 lock_vars[0].data = &ns->ns_resources;
90 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
92 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
94 lock_vars[0].data = &ns->ns_locks;
95 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
97 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
99 lock_vars[0].data = &ns->ns_nr_unused;
100 lock_vars[0].read_fptr = lprocfs_uint_rd;
101 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
104 #undef MAX_STRING_SIZE
106 #define LDLM_MAX_UNUSED 20
107 struct ldlm_namespace *ldlm_namespace_new(char *name, __u32 client)
109 struct ldlm_namespace *ns = NULL;
110 struct list_head *bucket;
113 OBD_ALLOC(ns, sizeof(*ns));
117 ns->ns_hash = vmalloc(sizeof(*ns->ns_hash) * RES_HASH_SIZE);
121 atomic_add(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
123 OBD_ALLOC(ns->ns_name, strlen(name) + 1);
125 GOTO(out_hash, NULL);
127 strcpy(ns->ns_name, name);
129 INIT_LIST_HEAD(&ns->ns_root_list);
130 l_lock_init(&ns->ns_lock);
132 ns->ns_client = client;
133 spin_lock_init(&ns->ns_counter_lock);
135 ns->ns_resources = 0;
137 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
139 INIT_LIST_HEAD(bucket);
141 INIT_LIST_HEAD(&ns->ns_unused_list);
142 ns->ns_nr_unused = 0;
143 ns->ns_max_unused = LDLM_MAX_UNUSED;
145 spin_lock(&ldlm_namespace_lock);
146 list_add(&ns->ns_list_chain, &ldlm_namespace_list);
147 spin_unlock(&ldlm_namespace_lock);
149 ldlm_proc_namespace(ns);
154 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
156 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
158 OBD_FREE(ns, sizeof(*ns));
162 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
164 /* If 'local_only' is true, don't try to tell the server, just cleanup.
165 * This is currently only used for recovery, and we make certain assumptions
166 * as a result--notably, that we shouldn't cancel locks with refs. -phil
168 * Called with the ns_lock held. */
169 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
172 struct list_head *tmp, *pos;
173 int rc = 0, client = res->lr_namespace->ns_client;
176 list_for_each_safe(tmp, pos, q) {
177 struct ldlm_lock *lock;
178 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
181 if (local_only && (lock->l_readers || lock->l_writers)) {
182 /* This is a little bit gross, but much better than the
183 * alternative: pretend that we got a blocking AST from
184 * the server, so that when the lock is decref'd, it
185 * will go away ... */
186 lock->l_flags |= LDLM_FL_CBPENDING;
187 /* ... without sending a CANCEL message. */
188 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
189 /* ... and without calling the cancellation callback */
190 lock->l_flags |= LDLM_FL_CANCEL;
195 /* At shutdown time, don't call the cancellation callback */
196 lock->l_flags |= LDLM_FL_CANCEL;
199 struct lustre_handle lockh;
200 ldlm_lock2handle(lock, &lockh);
202 rc = ldlm_cli_cancel(&lockh);
204 CERROR("ldlm_cli_cancel: %d\n", rc);
206 /* Force local cleanup on errors, too. */
207 if (local_only || rc != ELDLM_OK)
208 ldlm_lock_cancel(lock);
210 LDLM_DEBUG(lock, "Freeing a lock still held by a "
213 ldlm_resource_unlink_lock(lock);
214 ldlm_lock_destroy(lock);
221 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only)
226 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
230 l_lock(&ns->ns_lock);
231 for (i = 0; i < RES_HASH_SIZE; i++) {
232 struct list_head *tmp, *pos;
233 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
234 struct ldlm_resource *res;
235 res = list_entry(tmp, struct ldlm_resource, lr_hash);
236 ldlm_resource_getref(res);
238 cleanup_resource(res, &res->lr_granted, local_only);
239 cleanup_resource(res, &res->lr_converting, local_only);
240 cleanup_resource(res, &res->lr_waiting, local_only);
242 /* XXX what a mess: don't force cleanup if we're
243 * local_only (which is only used by recovery). In that
244 * case, we probably still have outstanding lock refs
245 * which reference these resources. -phil */
246 if (!ldlm_resource_putref(res) && !local_only) {
247 CERROR("Resource refcount nonzero (%d) after "
248 "lock cleanup; forcing cleanup.\n",
249 atomic_read(&res->lr_refcount));
250 ldlm_resource_dump(res);
251 atomic_set(&res->lr_refcount, 1);
252 ldlm_resource_putref(res);
256 l_unlock(&ns->ns_lock);
261 /* Cleanup, but also free, the namespace */
262 int ldlm_namespace_free(struct ldlm_namespace *ns)
267 spin_lock(&ldlm_namespace_lock);
268 list_del(&ns->ns_list_chain);
270 spin_unlock(&ldlm_namespace_lock);
272 ldlm_namespace_cleanup(ns, 0);
274 POISON(ns->ns_hash, 0x5a, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
275 vfree(ns->ns_hash /* , sizeof(*ns->ns_hash) * RES_HASH_SIZE */);
276 atomic_sub(sizeof(*ns->ns_hash) * RES_HASH_SIZE, &obd_memory);
277 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
278 OBD_FREE(ns, sizeof(*ns));
283 int ldlm_client_free(struct obd_export *exp)
285 struct ldlm_export_data *led = &exp->exp_ldlm_data;
286 ptlrpc_cleanup_client(&led->led_import);
290 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
295 for (i = 0; i < RES_NAME_SIZE; i++)
296 hash += name.name[i];
298 hash += (__u32)((unsigned long)parent >> 4);
300 return (hash & RES_HASH_MASK);
303 static struct ldlm_resource *ldlm_resource_new(void)
305 struct ldlm_resource *res;
307 res = kmem_cache_alloc(ldlm_resource_slab, SLAB_KERNEL);
312 memset(res, 0, sizeof(*res));
314 INIT_LIST_HEAD(&res->lr_children);
315 INIT_LIST_HEAD(&res->lr_childof);
316 INIT_LIST_HEAD(&res->lr_granted);
317 INIT_LIST_HEAD(&res->lr_converting);
318 INIT_LIST_HEAD(&res->lr_waiting);
320 atomic_set(&res->lr_refcount, 1);
325 /* Args: locked namespace
326 * Returns: newly-allocated, referenced, unlocked resource */
327 static struct ldlm_resource *
328 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
329 struct ldlm_res_id name, __u32 type)
331 struct list_head *bucket;
332 struct ldlm_resource *res;
335 if (type < LDLM_MIN_TYPE || type > LDLM_MAX_TYPE) {
340 res = ldlm_resource_new();
346 spin_lock(&ns->ns_counter_lock);
348 spin_unlock(&ns->ns_counter_lock);
350 l_lock(&ns->ns_lock);
351 memcpy(&res->lr_name, &name, sizeof(res->lr_name));
352 res->lr_namespace = ns;
356 res->lr_most_restr = LCK_NL;
358 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
359 list_add(&res->lr_hash, bucket);
361 if (parent == NULL) {
362 list_add(&res->lr_childof, &ns->ns_root_list);
364 res->lr_parent = parent;
365 list_add(&res->lr_childof, &parent->lr_children);
367 l_unlock(&ns->ns_lock);
372 /* Args: unlocked namespace
373 * Locks: takes and releases ns->ns_lock and res->lr_lock
374 * Returns: referenced, unlocked ldlm_resource or NULL */
375 struct ldlm_resource *
376 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
377 struct ldlm_res_id name, __u32 type, int create)
379 struct list_head *bucket, *tmp;
380 struct ldlm_resource *res = NULL;
384 LASSERT(ns->ns_hash != NULL);
386 l_lock(&ns->ns_lock);
387 bucket = ns->ns_hash + ldlm_hash_fn(parent, name);
389 list_for_each(tmp, bucket) {
390 res = list_entry(tmp, struct ldlm_resource, lr_hash);
392 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0) {
393 ldlm_resource_getref(res);
394 l_unlock(&ns->ns_lock);
400 res = ldlm_resource_add(ns, parent, name, type);
404 l_unlock(&ns->ns_lock);
409 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
411 atomic_inc(&res->lr_refcount);
412 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
413 atomic_read(&res->lr_refcount));
417 /* Returns 1 if the resource was freed, 0 if it remains. */
418 int ldlm_resource_putref(struct ldlm_resource *res)
423 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
424 atomic_read(&res->lr_refcount) - 1);
425 LASSERT(atomic_read(&res->lr_refcount) > 0);
426 LASSERT(atomic_read(&res->lr_refcount) < 0x5a5a5a5a);
428 if (atomic_dec_and_test(&res->lr_refcount)) {
429 struct ldlm_namespace *ns = res->lr_namespace;
432 l_lock(&ns->ns_lock);
434 if (atomic_read(&res->lr_refcount) != 0) {
435 /* We lost the race. */
436 l_unlock(&ns->ns_lock);
440 if (!list_empty(&res->lr_granted)) {
441 ldlm_resource_dump(res);
445 if (!list_empty(&res->lr_converting)) {
446 ldlm_resource_dump(res);
450 if (!list_empty(&res->lr_waiting)) {
451 ldlm_resource_dump(res);
455 if (!list_empty(&res->lr_children)) {
456 ldlm_resource_dump(res);
461 list_del_init(&res->lr_hash);
462 list_del_init(&res->lr_childof);
464 POISON(res, 0x5a, sizeof(*res));
465 kmem_cache_free(ldlm_resource_slab, res);
466 l_unlock(&ns->ns_lock);
468 spin_lock(&ns->ns_counter_lock);
470 spin_unlock(&ns->ns_counter_lock);
479 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
480 struct ldlm_lock *lock)
482 l_lock(&res->lr_namespace->ns_lock);
484 ldlm_resource_dump(res);
485 CDEBUG(D_OTHER, "About to add this lock:\n");
486 ldlm_lock_dump(D_OTHER, lock);
488 if (lock->l_destroyed) {
489 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
493 LASSERT(list_empty(&lock->l_res_link));
495 list_add_tail(&lock->l_res_link, head);
496 l_unlock(&res->lr_namespace->ns_lock);
499 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
501 l_lock(&lock->l_resource->lr_namespace->ns_lock);
502 list_del_init(&lock->l_res_link);
503 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
506 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
508 desc->lr_type = res->lr_type;
509 memcpy(&desc->lr_name, &res->lr_name, sizeof(desc->lr_name));
510 memcpy(desc->lr_version, res->lr_version, sizeof(desc->lr_version));
513 void ldlm_dump_all_namespaces(void)
515 struct list_head *tmp;
517 spin_lock(&ldlm_namespace_lock);
519 list_for_each(tmp, &ldlm_namespace_list) {
520 struct ldlm_namespace *ns;
521 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
522 ldlm_namespace_dump(ns);
525 spin_unlock(&ldlm_namespace_lock);
528 void ldlm_namespace_dump(struct ldlm_namespace *ns)
530 struct list_head *tmp;
532 l_lock(&ns->ns_lock);
533 CDEBUG(D_OTHER, "--- Namespace: %s (rc: %d, client: %d)\n", ns->ns_name,
534 ns->ns_refcount, ns->ns_client);
536 list_for_each(tmp, &ns->ns_root_list) {
537 struct ldlm_resource *res;
538 res = list_entry(tmp, struct ldlm_resource, lr_childof);
540 /* Once we have resources with children, this should really dump
541 * them recursively. */
542 ldlm_resource_dump(res);
544 l_unlock(&ns->ns_lock);
547 void ldlm_resource_dump(struct ldlm_resource *res)
549 struct list_head *tmp;
552 if (RES_NAME_SIZE != 3)
555 snprintf(name, sizeof(name), "%Lx %Lx %Lx",
556 (unsigned long long)res->lr_name.name[0],
557 (unsigned long long)res->lr_name.name[1],
558 (unsigned long long)res->lr_name.name[2]);
560 CDEBUG(D_OTHER, "--- Resource: %p (%s) (rc: %d)\n", res, name,
561 atomic_read(&res->lr_refcount));
562 CDEBUG(D_OTHER, "Namespace: %p (%s)\n", res->lr_namespace,
563 res->lr_namespace->ns_name);
564 CDEBUG(D_OTHER, "Parent: %p, root: %p\n", res->lr_parent, res->lr_root);
566 CDEBUG(D_OTHER, "Granted locks:\n");
567 list_for_each(tmp, &res->lr_granted) {
568 struct ldlm_lock *lock;
569 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
570 ldlm_lock_dump(D_OTHER, lock);
573 CDEBUG(D_OTHER, "Converting locks:\n");
574 list_for_each(tmp, &res->lr_converting) {
575 struct ldlm_lock *lock;
576 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
577 ldlm_lock_dump(D_OTHER, lock);
580 CDEBUG(D_OTHER, "Waiting locks:\n");
581 list_for_each(tmp, &res->lr_waiting) {
582 struct ldlm_lock *lock;
583 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
584 ldlm_lock_dump(D_OTHER, lock);