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 the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
29 # include <lustre_dlm.h>
31 # include <liblustre.h>
34 #include <obd_class.h>
35 #include "ldlm_internal.h"
37 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
39 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
40 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
42 struct semaphore ldlm_srv_namespace_lock;
43 struct list_head ldlm_srv_namespace_list =
44 CFS_LIST_HEAD_INIT(ldlm_srv_namespace_list);
46 struct semaphore ldlm_cli_namespace_lock;
47 struct list_head ldlm_cli_namespace_list =
48 CFS_LIST_HEAD_INIT(ldlm_cli_namespace_list);
50 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
51 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
52 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
55 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
56 unsigned long count, void *data)
58 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
59 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
63 int ldlm_proc_setup(void)
66 struct lprocfs_vars list[] = {
67 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
70 LASSERT(ldlm_ns_proc_dir == NULL);
72 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
75 if (IS_ERR(ldlm_type_proc_dir)) {
76 CERROR("LProcFS failed in ldlm-init\n");
77 rc = PTR_ERR(ldlm_type_proc_dir);
81 ldlm_ns_proc_dir = lprocfs_register("namespaces",
84 if (IS_ERR(ldlm_ns_proc_dir)) {
85 CERROR("LProcFS failed in ldlm-init\n");
86 rc = PTR_ERR(ldlm_ns_proc_dir);
90 ldlm_svc_proc_dir = lprocfs_register("services",
93 if (IS_ERR(ldlm_svc_proc_dir)) {
94 CERROR("LProcFS failed in ldlm-init\n");
95 rc = PTR_ERR(ldlm_svc_proc_dir);
99 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
104 lprocfs_remove(&ldlm_ns_proc_dir);
106 lprocfs_remove(&ldlm_type_proc_dir);
108 ldlm_svc_proc_dir = NULL;
112 void ldlm_proc_cleanup(void)
114 if (ldlm_svc_proc_dir)
115 lprocfs_remove(&ldlm_svc_proc_dir);
117 if (ldlm_ns_proc_dir)
118 lprocfs_remove(&ldlm_ns_proc_dir);
120 if (ldlm_type_proc_dir)
121 lprocfs_remove(&ldlm_type_proc_dir);
124 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
125 int count, int *eof, void *data)
127 struct ldlm_namespace *ns = data;
128 __u32 *nr = &ns->ns_max_unused;
130 if (ns_connect_lru_resize(ns))
131 nr = &ns->ns_nr_unused;
132 return lprocfs_rd_uint(page, start, off, count, eof, nr);
135 static int lprocfs_wr_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;
143 dummy[MAX_STRING_SIZE] = '\0';
144 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
147 if (count == 6 && memcmp(dummy, "clear", 5) == 0) {
149 "dropping all unused locks from namespace %s\n",
151 if (ns_connect_lru_resize(ns)) {
152 int canceled, unused = ns->ns_nr_unused;
154 /* Try to cancel all @ns_nr_unused locks. */
155 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
157 if (canceled < unused) {
158 CERROR("not all requested locks are canceled, "
159 "requested: %d, canceled: %d\n", unused,
164 tmp = ns->ns_max_unused;
165 ns->ns_max_unused = 0;
166 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
167 ns->ns_max_unused = tmp;
172 tmp = simple_strtoul(dummy, &end, 0);
174 CERROR("invalid value written\n");
177 lru_resize = (tmp == 0);
179 if (ns_connect_lru_resize(ns)) {
181 ns->ns_max_unused = (unsigned int)tmp;
183 if (tmp > ns->ns_nr_unused)
184 tmp = ns->ns_nr_unused;
185 tmp = ns->ns_nr_unused - tmp;
187 CDEBUG(D_DLMTRACE, "changing namespace %s unused locks from %u to %u\n",
188 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
189 ldlm_cancel_lru(ns, (unsigned int)tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
192 CDEBUG(D_DLMTRACE, "disable lru_resize for namespace %s\n",
194 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
197 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
198 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
199 ns->ns_max_unused = (unsigned int)tmp;
200 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
202 /* Make sure that originally lru resize was supported before
203 * turning it on here. */
205 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
206 CDEBUG(D_DLMTRACE, "enable lru_resize for namespace %s\n",
208 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
214 void ldlm_proc_namespace(struct ldlm_namespace *ns)
216 struct lprocfs_vars lock_vars[2];
217 char lock_name[MAX_STRING_SIZE + 1];
220 LASSERT(ns->ns_name != NULL);
222 lock_name[MAX_STRING_SIZE] = '\0';
224 memset(lock_vars, 0, sizeof(lock_vars));
225 lock_vars[0].name = lock_name;
227 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
228 lock_vars[0].data = &ns->ns_refcount;
229 lock_vars[0].read_fptr = lprocfs_rd_atomic;
230 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
232 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
233 lock_vars[0].data = &ns->ns_locks;
234 lock_vars[0].read_fptr = lprocfs_rd_atomic;
235 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
237 if (ns_is_client(ns)) {
238 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
240 lock_vars[0].data = &ns->ns_nr_unused;
241 lock_vars[0].read_fptr = lprocfs_rd_uint;
242 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
244 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
246 lock_vars[0].data = ns;
247 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
248 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
249 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
251 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
253 lock_vars[0].data = ns;
254 lock_vars[0].read_fptr = lprocfs_rd_uint;
255 lock_vars[0].write_fptr = lprocfs_wr_uint;
256 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
258 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
260 lock_vars[0].data = &ns->ns_max_age;
261 lock_vars[0].read_fptr = lprocfs_rd_uint;
262 lock_vars[0].write_fptr = lprocfs_wr_uint;
263 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
265 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
267 lock_vars[0].data = &ns->ns_max_nolock_size;
268 lock_vars[0].read_fptr = lprocfs_rd_uint;
269 lock_vars[0].write_fptr = lprocfs_wr_uint;
270 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
272 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
274 lock_vars[0].data = &ns->ns_contention_time;
275 lock_vars[0].read_fptr = lprocfs_rd_uint;
276 lock_vars[0].write_fptr = lprocfs_wr_uint;
277 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
279 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
281 lock_vars[0].data = &ns->ns_contended_locks;
282 lock_vars[0].read_fptr = lprocfs_rd_uint;
283 lock_vars[0].write_fptr = lprocfs_wr_uint;
284 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
287 #undef MAX_STRING_SIZE
289 #define ldlm_proc_namespace(ns) do {} while (0)
292 struct ldlm_namespace *ldlm_namespace_new(char *name, ldlm_side_t client,
295 struct ldlm_namespace *ns = NULL;
296 struct list_head *bucket;
297 int rc, idx, namelen;
302 CERROR("ldlm_get_ref failed: %d\n", rc);
310 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
314 namelen = strlen(name);
315 OBD_ALLOC(ns->ns_name, namelen + 1);
317 GOTO(out_hash, NULL);
319 ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
320 ns->ns_appetite = apt;
321 strcpy(ns->ns_name, name);
323 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
325 ns->ns_client = client;
326 spin_lock_init(&ns->ns_hash_lock);
327 atomic_set(&ns->ns_locks, 0);
328 ns->ns_resources = 0;
329 cfs_waitq_init(&ns->ns_waitq);
330 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
331 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
332 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
334 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
336 CFS_INIT_LIST_HEAD(bucket);
338 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
339 ns->ns_nr_unused = 0;
340 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
341 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
342 spin_lock_init(&ns->ns_unused_lock);
343 ns->ns_orig_connect_flags = 0;
344 ns->ns_connect_flags = 0;
346 ldlm_proc_namespace(ns);
348 idx = atomic_read(ldlm_namespace_nr(client));
350 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
352 CERROR("Can't initialize lock pool, rc %d\n", rc);
356 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
358 mutex_down(ldlm_namespace_lock(client));
359 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
360 atomic_inc(ldlm_namespace_nr(client));
361 mutex_up(ldlm_namespace_lock(client));
365 ldlm_namespace_cleanup(ns, 0);
366 OBD_FREE(ns->ns_name, namelen + 1);
368 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
376 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
378 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
379 * This is currently only used for recovery, and we make certain assumptions
380 * as a result--notably, that we shouldn't cancel locks with refs. -phil
382 * Called with the ns_lock held. */
383 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
386 struct list_head *tmp;
387 int rc = 0, client = ns_is_client(res->lr_namespace);
388 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
393 struct ldlm_lock *lock = NULL;
395 /* first, we look for non-cleaned-yet lock
396 * all cleaned locks are marked by CLEANED flag */
398 list_for_each(tmp, q) {
399 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
400 if (lock->l_flags & LDLM_FL_CLEANED) {
405 lock->l_flags |= LDLM_FL_CLEANED;
414 /* Set CBPENDING so nothing in the cancellation path
415 * can match this lock */
416 lock->l_flags |= LDLM_FL_CBPENDING;
417 lock->l_flags |= LDLM_FL_FAILED;
418 lock->l_flags |= flags;
420 /* ... without sending a CANCEL message for local_only. */
422 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
424 if (local_only && (lock->l_readers || lock->l_writers)) {
425 /* This is a little bit gross, but much better than the
426 * alternative: pretend that we got a blocking AST from
427 * the server, so that when the lock is decref'd, it
428 * will go away ... */
430 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
431 if (lock->l_completion_ast)
432 lock->l_completion_ast(lock, 0, NULL);
438 struct lustre_handle lockh;
441 ldlm_lock2handle(lock, &lockh);
442 rc = ldlm_cli_cancel(&lockh);
444 CERROR("ldlm_cli_cancel: %d\n", rc);
446 ldlm_resource_unlink_lock(lock);
448 LDLM_DEBUG(lock, "Freeing a lock still held by a "
450 ldlm_lock_destroy(lock);
458 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
460 struct list_head *tmp;
464 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
468 for (i = 0; i < RES_HASH_SIZE; i++) {
469 spin_lock(&ns->ns_hash_lock);
470 tmp = ns->ns_hash[i].next;
471 while (tmp != &(ns->ns_hash[i])) {
472 struct ldlm_resource *res;
473 res = list_entry(tmp, struct ldlm_resource, lr_hash);
474 ldlm_resource_getref(res);
475 spin_unlock(&ns->ns_hash_lock);
477 cleanup_resource(res, &res->lr_granted, flags);
478 cleanup_resource(res, &res->lr_converting, flags);
479 cleanup_resource(res, &res->lr_waiting, flags);
481 spin_lock(&ns->ns_hash_lock);
484 /* XXX: former stuff caused issues in case of race
485 * between ldlm_namespace_cleanup() and lockd() when
486 * client gets blocking ast when lock gets distracted by
487 * server. This is 1_4 branch solution, let's see how
489 if (!ldlm_resource_putref_locked(res))
491 "Namespace %s resource refcount nonzero "
492 "(%d) after lock cleanup; forcing cleanup.\n",
493 ns->ns_name, atomic_read(&res->lr_refcount));
495 spin_unlock(&ns->ns_hash_lock);
501 int ldlm_namespace_free_prior(struct ldlm_namespace *ns)
507 mutex_down(ldlm_namespace_lock(ns->ns_client));
508 list_del(&ns->ns_list_chain);
509 atomic_dec(ldlm_namespace_nr(ns->ns_client));
510 ldlm_pool_fini(&ns->ns_pool);
511 mutex_up(ldlm_namespace_lock(ns->ns_client));
513 /* At shutdown time, don't call the cancellation callback */
514 ldlm_namespace_cleanup(ns, 0);
516 if (ns->ns_refcount > 0) {
517 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
520 "dlm namespace %s free waiting on refcount %d\n",
521 ns->ns_name, ns->ns_refcount);
522 rc = l_wait_event(ns->ns_waitq,
523 ns->ns_refcount == 0, &lwi);
525 LCONSOLE_ERROR_MSG(0x139, "Lock manager: wait for %s "
526 "namespace cleanup aborted with %d "
527 "resources in use. (%d)\nI'm going "
528 "to try to clean up anyway, but I "
529 "might need a reboot of this node.\n",
530 ns->ns_name, (int) ns->ns_refcount,
533 "dlm namespace %s free done waiting\n", ns->ns_name);
539 int ldlm_namespace_free_post(struct ldlm_namespace *ns, int force)
547 struct proc_dir_entry *dir;
548 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
550 CERROR("dlm namespace %s has no procfs dir?\n",
553 lprocfs_remove(&dir);
557 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
558 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
564 /* Cleanup the resource, and free namespace.
567 * proc1: destroy import
568 * class_disconnect_export(grab cl_sem) ->
569 * -> ldlm_namespace_free ->
570 * -> lprocfs_remove(grab _lprocfs_lock).
571 * proc2: read proc info
572 * lprocfs_fops_read(grab _lprocfs_lock) ->
573 * -> osc_rd_active, etc(grab cl_sem).
575 * So that I have to split the ldlm_namespace_free into two parts - the first
576 * part ldlm_namespace_free_prior is used to cleanup the resource which is
577 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
578 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
581 int ldlm_namespace_free(struct ldlm_namespace *ns, int force)
583 ldlm_namespace_free_prior(ns);
584 ldlm_namespace_free_post(ns, force);
588 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
590 LASSERT(ns->ns_refcount >= 0);
594 void ldlm_namespace_get(struct ldlm_namespace *ns)
596 spin_lock(&ns->ns_hash_lock);
597 ldlm_namespace_get_nolock(ns);
598 spin_unlock(&ns->ns_hash_lock);
601 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
603 LASSERT(ns->ns_refcount > 0);
605 if (ns->ns_refcount == 0 && wakeup)
606 wake_up(&ns->ns_waitq);
609 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
611 spin_lock(&ns->ns_hash_lock);
612 ldlm_namespace_put_nolock(ns, wakeup);
613 spin_unlock(&ns->ns_hash_lock);
616 /* Should be called under ldlm_namespace_lock(client) taken */
617 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
619 LASSERT(!list_empty(&ns->ns_list_chain));
620 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
621 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
624 /* Should be called under ldlm_namespace_lock(client) taken */
625 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
627 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
628 LASSERT(!list_empty(ldlm_namespace_list(client)));
629 return container_of(ldlm_namespace_list(client)->next,
630 struct ldlm_namespace, ns_list_chain);
633 static __u32 ldlm_hash_fn(struct ldlm_resource *parent, struct ldlm_res_id name)
638 for (i = 0; i < RES_NAME_SIZE; i++)
639 hash += name.name[i];
641 hash += (__u32)((unsigned long)parent >> 4);
643 return (hash & RES_HASH_MASK);
646 static struct ldlm_resource *ldlm_resource_new(void)
648 struct ldlm_resource *res;
651 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
655 memset(res, 0, sizeof(*res));
657 CFS_INIT_LIST_HEAD(&res->lr_children);
658 CFS_INIT_LIST_HEAD(&res->lr_childof);
659 CFS_INIT_LIST_HEAD(&res->lr_granted);
660 CFS_INIT_LIST_HEAD(&res->lr_converting);
661 CFS_INIT_LIST_HEAD(&res->lr_waiting);
663 /* initialize interval trees for each lock mode*/
664 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
665 res->lr_itree[idx].lit_size = 0;
666 res->lr_itree[idx].lit_mode = 1 << idx;
667 res->lr_itree[idx].lit_root = NULL;
670 atomic_set(&res->lr_refcount, 1);
671 spin_lock_init(&res->lr_lock);
673 /* one who creates the resource must unlock
674 * the semaphore after lvb initialization */
675 init_MUTEX_LOCKED(&res->lr_lvb_sem);
680 /* must be called with hash lock held */
681 static struct ldlm_resource *
682 ldlm_resource_find(struct ldlm_namespace *ns, struct ldlm_res_id name, __u32 hash)
684 struct list_head *bucket, *tmp;
685 struct ldlm_resource *res;
687 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
688 bucket = ns->ns_hash + hash;
690 list_for_each(tmp, bucket) {
691 res = list_entry(tmp, struct ldlm_resource, lr_hash);
692 if (memcmp(&res->lr_name, &name, sizeof(res->lr_name)) == 0)
699 /* Args: locked namespace
700 * Returns: newly-allocated, referenced, unlocked resource */
701 static struct ldlm_resource *
702 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
703 struct ldlm_res_id name, __u32 hash, ldlm_type_t type)
705 struct list_head *bucket;
706 struct ldlm_resource *res, *old_res;
709 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
712 res = ldlm_resource_new();
717 res->lr_namespace = ns;
719 res->lr_most_restr = LCK_NL;
721 spin_lock(&ns->ns_hash_lock);
722 old_res = ldlm_resource_find(ns, name, hash);
724 /* someone won the race and added the resource before */
725 ldlm_resource_getref(old_res);
726 spin_unlock(&ns->ns_hash_lock);
727 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
728 /* synchronize WRT resource creation */
729 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
730 down(&old_res->lr_lvb_sem);
731 up(&old_res->lr_lvb_sem);
736 /* we won! let's add the resource */
737 bucket = ns->ns_hash + hash;
738 list_add(&res->lr_hash, bucket);
740 ldlm_namespace_get_nolock(ns);
742 if (parent == NULL) {
743 list_add(&res->lr_childof, &ns->ns_root_list);
745 res->lr_parent = parent;
746 list_add(&res->lr_childof, &parent->lr_children);
748 spin_unlock(&ns->ns_hash_lock);
750 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
753 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
754 rc = ns->ns_lvbo->lvbo_init(res);
756 CERROR("lvbo_init failed for resource "
757 LPU64": rc %d\n", name.name[0], rc);
758 /* we create resource with locked lr_lvb_sem */
759 up(&res->lr_lvb_sem);
765 /* Args: unlocked namespace
766 * Locks: takes and releases ns->ns_lock and res->lr_lock
767 * Returns: referenced, unlocked ldlm_resource or NULL */
768 struct ldlm_resource *
769 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
770 struct ldlm_res_id name, ldlm_type_t type, int create)
772 __u32 hash = ldlm_hash_fn(parent, name);
773 struct ldlm_resource *res = NULL;
777 LASSERT(ns->ns_hash != NULL);
778 LASSERT(name.name[0] != 0);
780 spin_lock(&ns->ns_hash_lock);
781 res = ldlm_resource_find(ns, name, hash);
783 ldlm_resource_getref(res);
784 spin_unlock(&ns->ns_hash_lock);
785 /* synchronize WRT resource creation */
786 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
787 down(&res->lr_lvb_sem);
788 up(&res->lr_lvb_sem);
792 spin_unlock(&ns->ns_hash_lock);
797 res = ldlm_resource_add(ns, parent, name, hash, type);
801 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
803 LASSERT(res != NULL);
804 LASSERT(res != LP_POISON);
805 atomic_inc(&res->lr_refcount);
806 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
807 atomic_read(&res->lr_refcount));
811 void __ldlm_resource_putref_final(struct ldlm_resource *res)
813 struct ldlm_namespace *ns = res->lr_namespace;
815 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
817 if (!list_empty(&res->lr_granted)) {
818 ldlm_resource_dump(D_ERROR, res);
822 if (!list_empty(&res->lr_converting)) {
823 ldlm_resource_dump(D_ERROR, res);
827 if (!list_empty(&res->lr_waiting)) {
828 ldlm_resource_dump(D_ERROR, res);
832 if (!list_empty(&res->lr_children)) {
833 ldlm_resource_dump(D_ERROR, res);
837 /* Pass 0 as second argument to not wake up ->ns_waitq yet, will do it
839 ldlm_namespace_put_nolock(ns, 0);
840 list_del_init(&res->lr_hash);
841 list_del_init(&res->lr_childof);
844 if (ns->ns_resources == 0)
845 wake_up(&ns->ns_waitq);
848 /* Returns 1 if the resource was freed, 0 if it remains. */
849 int ldlm_resource_putref(struct ldlm_resource *res)
851 struct ldlm_namespace *ns = res->lr_namespace;
855 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
856 atomic_read(&res->lr_refcount) - 1);
857 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
858 atomic_read(&res->lr_refcount));
859 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
860 atomic_read(&res->lr_refcount));
862 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
863 __ldlm_resource_putref_final(res);
864 spin_unlock(&ns->ns_hash_lock);
865 if (res->lr_lvb_data)
866 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
867 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
874 /* Returns 1 if the resource was freed, 0 if it remains. */
875 int ldlm_resource_putref_locked(struct ldlm_resource *res)
880 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
881 atomic_read(&res->lr_refcount) - 1);
882 LASSERT(atomic_read(&res->lr_refcount) > 0);
883 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
885 LASSERT(atomic_read(&res->lr_refcount) >= 0);
886 if (atomic_dec_and_test(&res->lr_refcount)) {
887 __ldlm_resource_putref_final(res);
888 if (res->lr_lvb_data)
889 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
890 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
897 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
898 struct ldlm_lock *lock)
900 check_res_locked(res);
902 ldlm_resource_dump(D_OTHER, res);
903 CDEBUG(D_OTHER, "About to add this lock:\n");
904 ldlm_lock_dump(D_OTHER, lock, 0);
906 if (lock->l_destroyed) {
907 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
911 LASSERT(list_empty(&lock->l_res_link));
913 list_add_tail(&lock->l_res_link, head);
916 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
917 struct ldlm_lock *new)
919 struct ldlm_resource *res = original->l_resource;
921 check_res_locked(res);
923 ldlm_resource_dump(D_OTHER, res);
924 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
925 ldlm_lock_dump(D_OTHER, new, 0);
927 if (new->l_destroyed) {
928 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
932 LASSERT(list_empty(&new->l_res_link));
934 list_add(&new->l_res_link, &original->l_res_link);
938 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
940 int type = lock->l_resource->lr_type;
942 check_res_locked(lock->l_resource);
943 if (type == LDLM_IBITS || type == LDLM_PLAIN)
944 ldlm_unlink_lock_skiplist(lock);
945 else if (type == LDLM_EXTENT)
946 ldlm_extent_unlink_lock(lock);
947 list_del_init(&lock->l_res_link);
950 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
952 desc->lr_type = res->lr_type;
953 desc->lr_name = res->lr_name;
956 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
958 struct list_head *tmp;
960 if (!((libcfs_debug | D_ERROR) & level))
963 mutex_down(ldlm_namespace_lock(client));
965 list_for_each(tmp, ldlm_namespace_list(client)) {
966 struct ldlm_namespace *ns;
967 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
968 ldlm_namespace_dump(level, ns);
971 mutex_up(ldlm_namespace_lock(client));
974 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
976 struct list_head *tmp;
978 if (!((libcfs_debug | D_ERROR) & level))
981 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
982 ns->ns_name, ns->ns_refcount,
983 ns_is_client(ns) ? "client" : "server");
985 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
988 spin_lock(&ns->ns_hash_lock);
989 tmp = ns->ns_root_list.next;
990 while (tmp != &ns->ns_root_list) {
991 struct ldlm_resource *res;
992 res = list_entry(tmp, struct ldlm_resource, lr_childof);
994 ldlm_resource_getref(res);
995 spin_unlock(&ns->ns_hash_lock);
998 ldlm_resource_dump(level, res);
1001 spin_lock(&ns->ns_hash_lock);
1003 ldlm_resource_putref_locked(res);
1005 ns->ns_next_dump = cfs_time_shift(10);
1006 spin_unlock(&ns->ns_hash_lock);
1009 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1011 struct list_head *tmp;
1014 CLASSERT(RES_NAME_SIZE == 4);
1016 if (!((libcfs_debug | D_ERROR) & level))
1019 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1020 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1021 res->lr_name.name[2], res->lr_name.name[3],
1022 atomic_read(&res->lr_refcount));
1024 if (!list_empty(&res->lr_granted)) {
1026 CDEBUG(level, "Granted locks:\n");
1027 list_for_each(tmp, &res->lr_granted) {
1028 struct ldlm_lock *lock;
1029 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1030 ldlm_lock_dump(level, lock, ++pos);
1033 if (!list_empty(&res->lr_converting)) {
1035 CDEBUG(level, "Converting locks:\n");
1036 list_for_each(tmp, &res->lr_converting) {
1037 struct ldlm_lock *lock;
1038 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1039 ldlm_lock_dump(level, lock, ++pos);
1042 if (!list_empty(&res->lr_waiting)) {
1044 CDEBUG(level, "Waiting locks:\n");
1045 list_for_each(tmp, &res->lr_waiting) {
1046 struct ldlm_lock *lock;
1047 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1048 ldlm_lock_dump(level, lock, ++pos);