4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ldlm/ldlm_resource.c
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Peter Braam <braam@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LDLM
39 #include "../include/lustre_dlm.h"
40 #include "../include/lustre_fid.h"
41 #include "../include/obd_class.h"
42 #include "ldlm_internal.h"
44 struct kmem_cache *ldlm_resource_slab, *ldlm_lock_slab;
46 int ldlm_srv_namespace_nr;
47 int ldlm_cli_namespace_nr;
49 struct mutex ldlm_srv_namespace_lock;
50 LIST_HEAD(ldlm_srv_namespace_list);
52 struct mutex ldlm_cli_namespace_lock;
53 /* Client Namespaces that have active resources in them.
54 * Once all resources go away, ldlm_poold moves such namespaces to the
57 LIST_HEAD(ldlm_cli_active_namespace_list);
58 /* Client namespaces that don't have any locks in them */
59 static LIST_HEAD(ldlm_cli_inactive_namespace_list);
61 static struct dentry *ldlm_debugfs_dir;
62 static struct dentry *ldlm_ns_debugfs_dir;
63 struct dentry *ldlm_svc_debugfs_dir;
65 /* during debug dump certain amount of granted locks for one resource to avoid
68 static unsigned int ldlm_dump_granted_max = 256;
71 lprocfs_wr_dump_ns(struct file *file, const char __user *buffer,
72 size_t count, loff_t *off)
74 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
75 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
79 LPROC_SEQ_FOPS_WR_ONLY(ldlm, dump_ns);
81 LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint);
83 static struct lprocfs_vars ldlm_debugfs_list[] = {
84 { "dump_namespaces", &ldlm_dump_ns_fops, NULL, 0222 },
85 { "dump_granted_max", &ldlm_rw_uint_fops, &ldlm_dump_granted_max },
89 int ldlm_debugfs_setup(void)
93 ldlm_debugfs_dir = ldebugfs_register(OBD_LDLM_DEVICENAME,
96 if (IS_ERR_OR_NULL(ldlm_debugfs_dir)) {
97 CERROR("LProcFS failed in ldlm-init\n");
98 rc = ldlm_debugfs_dir ? PTR_ERR(ldlm_debugfs_dir) : -ENOMEM;
102 ldlm_ns_debugfs_dir = ldebugfs_register("namespaces",
105 if (IS_ERR_OR_NULL(ldlm_ns_debugfs_dir)) {
106 CERROR("LProcFS failed in ldlm-init\n");
107 rc = ldlm_ns_debugfs_dir ? PTR_ERR(ldlm_ns_debugfs_dir)
112 ldlm_svc_debugfs_dir = ldebugfs_register("services",
115 if (IS_ERR_OR_NULL(ldlm_svc_debugfs_dir)) {
116 CERROR("LProcFS failed in ldlm-init\n");
117 rc = ldlm_svc_debugfs_dir ? PTR_ERR(ldlm_svc_debugfs_dir)
122 rc = ldebugfs_add_vars(ldlm_debugfs_dir, ldlm_debugfs_list, NULL);
124 CERROR("LProcFS failed in ldlm-init\n");
131 ldebugfs_remove(&ldlm_svc_debugfs_dir);
133 ldebugfs_remove(&ldlm_ns_debugfs_dir);
135 ldebugfs_remove(&ldlm_debugfs_dir);
137 ldlm_svc_debugfs_dir = NULL;
138 ldlm_ns_debugfs_dir = NULL;
139 ldlm_debugfs_dir = NULL;
143 void ldlm_debugfs_cleanup(void)
145 if (!IS_ERR_OR_NULL(ldlm_svc_debugfs_dir))
146 ldebugfs_remove(&ldlm_svc_debugfs_dir);
148 if (!IS_ERR_OR_NULL(ldlm_ns_debugfs_dir))
149 ldebugfs_remove(&ldlm_ns_debugfs_dir);
151 if (!IS_ERR_OR_NULL(ldlm_debugfs_dir))
152 ldebugfs_remove(&ldlm_debugfs_dir);
154 ldlm_svc_debugfs_dir = NULL;
155 ldlm_ns_debugfs_dir = NULL;
156 ldlm_debugfs_dir = NULL;
159 static ssize_t resource_count_show(struct kobject *kobj, struct attribute *attr,
162 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
165 struct cfs_hash_bd bd;
168 /* result is not strictly consistent */
169 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
170 res += cfs_hash_bd_count_get(&bd);
171 return sprintf(buf, "%lld\n", res);
173 LUSTRE_RO_ATTR(resource_count);
175 static ssize_t lock_count_show(struct kobject *kobj, struct attribute *attr,
178 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
182 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
183 LPROCFS_FIELDS_FLAGS_SUM);
184 return sprintf(buf, "%lld\n", locks);
186 LUSTRE_RO_ATTR(lock_count);
188 static ssize_t lock_unused_count_show(struct kobject *kobj,
189 struct attribute *attr,
192 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
195 return sprintf(buf, "%d\n", ns->ns_nr_unused);
197 LUSTRE_RO_ATTR(lock_unused_count);
199 static ssize_t lru_size_show(struct kobject *kobj, struct attribute *attr,
202 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
204 __u32 *nr = &ns->ns_max_unused;
206 if (ns_connect_lru_resize(ns))
207 nr = &ns->ns_nr_unused;
208 return sprintf(buf, "%u", *nr);
211 static ssize_t lru_size_store(struct kobject *kobj, struct attribute *attr,
212 const char *buffer, size_t count)
214 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
220 if (strncmp(buffer, "clear", 5) == 0) {
222 "dropping all unused locks from namespace %s\n",
224 if (ns_connect_lru_resize(ns)) {
225 int canceled, unused = ns->ns_nr_unused;
227 /* Try to cancel all @ns_nr_unused locks. */
228 canceled = ldlm_cancel_lru(ns, unused, 0,
230 if (canceled < unused) {
232 "not all requested locks are canceled, requested: %d, canceled: %d\n",
238 tmp = ns->ns_max_unused;
239 ns->ns_max_unused = 0;
240 ldlm_cancel_lru(ns, 0, 0, LDLM_CANCEL_PASSED);
241 ns->ns_max_unused = tmp;
246 err = kstrtoul(buffer, 10, &tmp);
248 CERROR("lru_size: invalid value written\n");
251 lru_resize = (tmp == 0);
253 if (ns_connect_lru_resize(ns)) {
255 ns->ns_max_unused = (unsigned int)tmp;
257 if (tmp > ns->ns_nr_unused)
258 tmp = ns->ns_nr_unused;
259 tmp = ns->ns_nr_unused - tmp;
262 "changing namespace %s unused locks from %u to %u\n",
263 ldlm_ns_name(ns), ns->ns_nr_unused,
265 ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_CANCEL_PASSED);
269 "disable lru_resize for namespace %s\n",
271 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
275 "changing namespace %s max_unused from %u to %u\n",
276 ldlm_ns_name(ns), ns->ns_max_unused,
278 ns->ns_max_unused = (unsigned int)tmp;
279 ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_CANCEL_PASSED);
281 /* Make sure that LRU resize was originally supported before
282 * turning it on here.
285 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
287 "enable lru_resize for namespace %s\n",
289 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
295 LUSTRE_RW_ATTR(lru_size);
297 static ssize_t lru_max_age_show(struct kobject *kobj, struct attribute *attr,
300 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
303 return sprintf(buf, "%u", ns->ns_max_age);
306 static ssize_t lru_max_age_store(struct kobject *kobj, struct attribute *attr,
307 const char *buffer, size_t count)
309 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
314 err = kstrtoul(buffer, 10, &tmp);
318 ns->ns_max_age = tmp;
322 LUSTRE_RW_ATTR(lru_max_age);
324 static ssize_t early_lock_cancel_show(struct kobject *kobj,
325 struct attribute *attr,
328 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
331 return sprintf(buf, "%d\n", ns_connect_cancelset(ns));
334 static ssize_t early_lock_cancel_store(struct kobject *kobj,
335 struct attribute *attr,
339 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
341 unsigned long supp = -1;
344 rc = kstrtoul(buffer, 10, &supp);
349 ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
350 else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
351 ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
354 LUSTRE_RW_ATTR(early_lock_cancel);
356 /* These are for namespaces in /sys/fs/lustre/ldlm/namespaces/ */
357 static struct attribute *ldlm_ns_attrs[] = {
358 &lustre_attr_resource_count.attr,
359 &lustre_attr_lock_count.attr,
360 &lustre_attr_lock_unused_count.attr,
361 &lustre_attr_lru_size.attr,
362 &lustre_attr_lru_max_age.attr,
363 &lustre_attr_early_lock_cancel.attr,
367 static void ldlm_ns_release(struct kobject *kobj)
369 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
371 complete(&ns->ns_kobj_unregister);
374 static struct kobj_type ldlm_ns_ktype = {
375 .default_attrs = ldlm_ns_attrs,
376 .sysfs_ops = &lustre_sysfs_ops,
377 .release = ldlm_ns_release,
380 static void ldlm_namespace_debugfs_unregister(struct ldlm_namespace *ns)
382 if (IS_ERR_OR_NULL(ns->ns_debugfs_entry))
383 CERROR("dlm namespace %s has no procfs dir?\n",
386 ldebugfs_remove(&ns->ns_debugfs_entry);
389 lprocfs_free_stats(&ns->ns_stats);
392 static void ldlm_namespace_sysfs_unregister(struct ldlm_namespace *ns)
394 kobject_put(&ns->ns_kobj);
395 wait_for_completion(&ns->ns_kobj_unregister);
398 static int ldlm_namespace_sysfs_register(struct ldlm_namespace *ns)
402 ns->ns_kobj.kset = ldlm_ns_kset;
403 init_completion(&ns->ns_kobj_unregister);
404 err = kobject_init_and_add(&ns->ns_kobj, &ldlm_ns_ktype, NULL,
405 "%s", ldlm_ns_name(ns));
407 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
409 kobject_put(&ns->ns_kobj);
413 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
414 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
419 static int ldlm_namespace_debugfs_register(struct ldlm_namespace *ns)
421 struct dentry *ns_entry;
423 if (!IS_ERR_OR_NULL(ns->ns_debugfs_entry)) {
424 ns_entry = ns->ns_debugfs_entry;
426 ns_entry = debugfs_create_dir(ldlm_ns_name(ns),
427 ldlm_ns_debugfs_dir);
430 ns->ns_debugfs_entry = ns_entry;
436 #undef MAX_STRING_SIZE
438 static struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
441 LASSERT(res != LP_POISON);
442 atomic_inc(&res->lr_refcount);
443 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
444 atomic_read(&res->lr_refcount));
448 static unsigned ldlm_res_hop_hash(struct cfs_hash *hs,
449 const void *key, unsigned mask)
451 const struct ldlm_res_id *id = key;
455 for (i = 0; i < RES_NAME_SIZE; i++)
460 static unsigned ldlm_res_hop_fid_hash(struct cfs_hash *hs,
461 const void *key, unsigned mask)
463 const struct ldlm_res_id *id = key;
468 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
469 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
470 fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
472 hash = fid_flatten32(&fid);
473 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
474 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
475 val = id->name[LUSTRE_RES_ID_HSH_OFF];
476 hash += (val >> 5) + (val << 11);
480 hash = hash_long(hash, hs->hs_bkt_bits);
481 /* give me another random factor */
482 hash -= hash_long((unsigned long)hs, val % 11 + 3);
484 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
485 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
490 static void *ldlm_res_hop_key(struct hlist_node *hnode)
492 struct ldlm_resource *res;
494 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
495 return &res->lr_name;
498 static int ldlm_res_hop_keycmp(const void *key, struct hlist_node *hnode)
500 struct ldlm_resource *res;
502 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
503 return ldlm_res_eq((const struct ldlm_res_id *)key,
504 (const struct ldlm_res_id *)&res->lr_name);
507 static void *ldlm_res_hop_object(struct hlist_node *hnode)
509 return hlist_entry(hnode, struct ldlm_resource, lr_hash);
512 static void ldlm_res_hop_get_locked(struct cfs_hash *hs,
513 struct hlist_node *hnode)
515 struct ldlm_resource *res;
517 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
518 ldlm_resource_getref(res);
521 static void ldlm_res_hop_put_locked(struct cfs_hash *hs,
522 struct hlist_node *hnode)
524 struct ldlm_resource *res;
526 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
527 /* cfs_hash_for_each_nolock is the only chance we call it */
528 ldlm_resource_putref_locked(res);
531 static void ldlm_res_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
533 struct ldlm_resource *res;
535 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
536 ldlm_resource_putref(res);
539 static struct cfs_hash_ops ldlm_ns_hash_ops = {
540 .hs_hash = ldlm_res_hop_hash,
541 .hs_key = ldlm_res_hop_key,
542 .hs_keycmp = ldlm_res_hop_keycmp,
544 .hs_object = ldlm_res_hop_object,
545 .hs_get = ldlm_res_hop_get_locked,
546 .hs_put_locked = ldlm_res_hop_put_locked,
547 .hs_put = ldlm_res_hop_put
550 static struct cfs_hash_ops ldlm_ns_fid_hash_ops = {
551 .hs_hash = ldlm_res_hop_fid_hash,
552 .hs_key = ldlm_res_hop_key,
553 .hs_keycmp = ldlm_res_hop_keycmp,
555 .hs_object = ldlm_res_hop_object,
556 .hs_get = ldlm_res_hop_get_locked,
557 .hs_put_locked = ldlm_res_hop_put_locked,
558 .hs_put = ldlm_res_hop_put
561 struct ldlm_ns_hash_def {
562 enum ldlm_ns_type nsd_type;
563 /** hash bucket bits */
564 unsigned nsd_bkt_bits;
566 unsigned nsd_all_bits;
567 /** hash operations */
568 struct cfs_hash_ops *nsd_hops;
571 static struct ldlm_ns_hash_def ldlm_ns_hash_defs[] = {
573 .nsd_type = LDLM_NS_TYPE_MDC,
576 .nsd_hops = &ldlm_ns_fid_hash_ops,
579 .nsd_type = LDLM_NS_TYPE_MDT,
582 .nsd_hops = &ldlm_ns_fid_hash_ops,
585 .nsd_type = LDLM_NS_TYPE_OSC,
588 .nsd_hops = &ldlm_ns_hash_ops,
591 .nsd_type = LDLM_NS_TYPE_OST,
594 .nsd_hops = &ldlm_ns_hash_ops,
597 .nsd_type = LDLM_NS_TYPE_MGC,
600 .nsd_hops = &ldlm_ns_hash_ops,
603 .nsd_type = LDLM_NS_TYPE_MGT,
606 .nsd_hops = &ldlm_ns_hash_ops,
609 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
613 /** Register \a ns in the list of namespaces */
614 static void ldlm_namespace_register(struct ldlm_namespace *ns,
617 mutex_lock(ldlm_namespace_lock(client));
618 LASSERT(list_empty(&ns->ns_list_chain));
619 list_add(&ns->ns_list_chain, &ldlm_cli_inactive_namespace_list);
620 ldlm_namespace_nr_inc(client);
621 mutex_unlock(ldlm_namespace_lock(client));
625 * Create and initialize new empty namespace.
627 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
629 enum ldlm_appetite apt,
630 enum ldlm_ns_type ns_type)
632 struct ldlm_namespace *ns = NULL;
633 struct ldlm_ns_bucket *nsb;
634 struct ldlm_ns_hash_def *nsd;
635 struct cfs_hash_bd bd;
643 CERROR("ldlm_get_ref failed: %d\n", rc);
647 for (idx = 0;; idx++) {
648 nsd = &ldlm_ns_hash_defs[idx];
649 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
650 CERROR("Unknown type %d for ns %s\n", ns_type, name);
654 if (nsd->nsd_type == ns_type)
658 ns = kzalloc(sizeof(*ns), GFP_NOFS);
662 ns->ns_rs_hash = cfs_hash_create(name,
663 nsd->nsd_all_bits, nsd->nsd_all_bits,
664 nsd->nsd_bkt_bits, sizeof(*nsb),
670 CFS_HASH_SPIN_BKTLOCK |
671 CFS_HASH_NO_ITEMREF);
675 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
676 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
677 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
678 nsb->nsb_namespace = ns;
682 ns->ns_appetite = apt;
683 ns->ns_client = client;
685 INIT_LIST_HEAD(&ns->ns_list_chain);
686 INIT_LIST_HEAD(&ns->ns_unused_list);
687 spin_lock_init(&ns->ns_lock);
688 atomic_set(&ns->ns_bref, 0);
689 init_waitqueue_head(&ns->ns_waitq);
691 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
692 ns->ns_nr_unused = 0;
693 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
694 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
695 ns->ns_orig_connect_flags = 0;
696 ns->ns_connect_flags = 0;
699 rc = ldlm_namespace_sysfs_register(ns);
701 CERROR("Can't initialize ns sysfs, rc %d\n", rc);
705 rc = ldlm_namespace_debugfs_register(ns);
707 CERROR("Can't initialize ns proc, rc %d\n", rc);
711 idx = ldlm_namespace_nr_read(client);
712 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
714 CERROR("Can't initialize lock pool, rc %d\n", rc);
718 ldlm_namespace_register(ns, client);
721 ldlm_namespace_debugfs_unregister(ns);
723 ldlm_namespace_sysfs_unregister(ns);
724 ldlm_namespace_cleanup(ns, 0);
726 cfs_hash_putref(ns->ns_rs_hash);
733 EXPORT_SYMBOL(ldlm_namespace_new);
735 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
738 * Cancel and destroy all locks on a resource.
740 * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
741 * clean up. This is currently only used for recovery, and we make
742 * certain assumptions as a result--notably, that we shouldn't cancel
745 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
748 struct list_head *tmp;
750 bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
753 struct ldlm_lock *lock = NULL;
754 struct lustre_handle lockh;
756 /* First, we look for non-cleaned-yet lock
757 * all cleaned locks are marked by CLEANED flag.
760 list_for_each(tmp, q) {
761 lock = list_entry(tmp, struct ldlm_lock,
763 if (ldlm_is_cleaned(lock)) {
768 ldlm_set_cleaned(lock);
777 /* Set CBPENDING so nothing in the cancellation path
778 * can match this lock.
780 ldlm_set_cbpending(lock);
781 ldlm_set_failed(lock);
782 lock->l_flags |= flags;
784 /* ... without sending a CANCEL message for local_only. */
786 ldlm_set_local_only(lock);
788 if (local_only && (lock->l_readers || lock->l_writers)) {
789 /* This is a little bit gross, but much better than the
790 * alternative: pretend that we got a blocking AST from
791 * the server, so that when the lock is decref'd, it
795 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
796 if (lock->l_flags & LDLM_FL_FAIL_LOC) {
797 set_current_state(TASK_UNINTERRUPTIBLE);
798 schedule_timeout(cfs_time_seconds(4));
799 set_current_state(TASK_RUNNING);
801 if (lock->l_completion_ast)
802 lock->l_completion_ast(lock, LDLM_FL_FAILED,
804 LDLM_LOCK_RELEASE(lock);
809 ldlm_lock2handle(lock, &lockh);
810 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
812 CERROR("ldlm_cli_cancel: %d\n", rc);
813 LDLM_LOCK_RELEASE(lock);
817 static int ldlm_resource_clean(struct cfs_hash *hs, struct cfs_hash_bd *bd,
818 struct hlist_node *hnode, void *arg)
820 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
821 __u64 flags = *(__u64 *)arg;
823 cleanup_resource(res, &res->lr_granted, flags);
824 cleanup_resource(res, &res->lr_waiting, flags);
829 static int ldlm_resource_complain(struct cfs_hash *hs, struct cfs_hash_bd *bd,
830 struct hlist_node *hnode, void *arg)
832 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
835 CERROR("%s: namespace resource "DLDLMRES
836 " (%p) refcount nonzero (%d) after lock cleanup; forcing cleanup.\n",
837 ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
838 atomic_read(&res->lr_refcount) - 1);
840 ldlm_resource_dump(D_ERROR, res);
846 * Cancel and destroy all locks in the namespace.
848 * Typically used during evictions when server notified client that it was
849 * evicted and all of its state needs to be destroyed.
850 * Also used during shutdown.
852 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
855 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
859 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean, &flags);
860 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
863 EXPORT_SYMBOL(ldlm_namespace_cleanup);
866 * Attempts to free namespace.
868 * Only used when namespace goes away, like during an unmount.
870 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
872 /* At shutdown time, don't call the cancellation callback */
873 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
875 if (atomic_read(&ns->ns_bref) > 0) {
876 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
880 "dlm namespace %s free waiting on refcount %d\n",
881 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
884 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
886 rc = l_wait_event(ns->ns_waitq,
887 atomic_read(&ns->ns_bref) == 0, &lwi);
889 /* Forced cleanups should be able to reclaim all references,
890 * so it's safe to wait forever... we can't leak locks...
892 if (force && rc == -ETIMEDOUT) {
893 LCONSOLE_ERROR("Forced cleanup waiting for %s namespace with %d resources in use, (rc=%d)\n",
895 atomic_read(&ns->ns_bref), rc);
899 if (atomic_read(&ns->ns_bref)) {
900 LCONSOLE_ERROR("Cleanup waiting for %s namespace with %d resources in use, (rc=%d)\n",
902 atomic_read(&ns->ns_bref), rc);
903 return ELDLM_NAMESPACE_EXISTS;
905 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
913 * Performs various cleanups for passed \a ns to make it drop refc and be
914 * ready for freeing. Waits for refc == 0.
916 * The following is done:
917 * (0) Unregister \a ns from its list to make inaccessible for potential
918 * users like pools thread and others;
919 * (1) Clear all locks in \a ns.
921 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
922 struct obd_import *imp,
930 spin_lock(&ns->ns_lock);
932 spin_unlock(&ns->ns_lock);
935 * Can fail with -EINTR when force == 0 in which case try harder.
937 rc = __ldlm_namespace_free(ns, force);
938 if (rc != ELDLM_OK) {
940 ptlrpc_disconnect_import(imp, 0);
941 ptlrpc_invalidate_import(imp);
945 * With all requests dropped and the import inactive
946 * we are guaranteed all reference will be dropped.
948 rc = __ldlm_namespace_free(ns, 1);
953 /** Unregister \a ns from the list of namespaces. */
954 static void ldlm_namespace_unregister(struct ldlm_namespace *ns,
957 mutex_lock(ldlm_namespace_lock(client));
958 LASSERT(!list_empty(&ns->ns_list_chain));
959 /* Some asserts and possibly other parts of the code are still
960 * using list_empty(&ns->ns_list_chain). This is why it is
961 * important to use list_del_init() here.
963 list_del_init(&ns->ns_list_chain);
964 ldlm_namespace_nr_dec(client);
965 mutex_unlock(ldlm_namespace_lock(client));
969 * Performs freeing memory structures related to \a ns. This is only done
970 * when ldlm_namespce_free_prior() successfully removed all resources
971 * referencing \a ns and its refc == 0.
973 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
978 /* Make sure that nobody can find this ns in its list. */
979 ldlm_namespace_unregister(ns, ns->ns_client);
980 /* Fini pool _before_ parent proc dir is removed. This is important as
981 * ldlm_pool_fini() removes own proc dir which is child to @dir.
982 * Removing it after @dir may cause oops.
984 ldlm_pool_fini(&ns->ns_pool);
986 ldlm_namespace_debugfs_unregister(ns);
987 ldlm_namespace_sysfs_unregister(ns);
988 cfs_hash_putref(ns->ns_rs_hash);
989 /* Namespace \a ns should be not on list at this time, otherwise
990 * this will cause issues related to using freed \a ns in poold
993 LASSERT(list_empty(&ns->ns_list_chain));
998 void ldlm_namespace_get(struct ldlm_namespace *ns)
1000 atomic_inc(&ns->ns_bref);
1002 EXPORT_SYMBOL(ldlm_namespace_get);
1004 /* This is only for callers that care about refcount */
1005 static int ldlm_namespace_get_return(struct ldlm_namespace *ns)
1007 return atomic_inc_return(&ns->ns_bref);
1010 void ldlm_namespace_put(struct ldlm_namespace *ns)
1012 if (atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
1013 wake_up(&ns->ns_waitq);
1014 spin_unlock(&ns->ns_lock);
1017 EXPORT_SYMBOL(ldlm_namespace_put);
1019 /** Should be called with ldlm_namespace_lock(client) taken. */
1020 void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
1023 LASSERT(!list_empty(&ns->ns_list_chain));
1024 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1025 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
1028 /** Should be called with ldlm_namespace_lock(client) taken. */
1029 void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
1032 LASSERT(!list_empty(&ns->ns_list_chain));
1033 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1034 list_move_tail(&ns->ns_list_chain, &ldlm_cli_inactive_namespace_list);
1037 /** Should be called with ldlm_namespace_lock(client) taken. */
1038 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
1040 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1041 LASSERT(!list_empty(ldlm_namespace_list(client)));
1042 return container_of(ldlm_namespace_list(client)->next,
1043 struct ldlm_namespace, ns_list_chain);
1046 /** Create and initialize new resource. */
1047 static struct ldlm_resource *ldlm_resource_new(void)
1049 struct ldlm_resource *res;
1052 res = kmem_cache_zalloc(ldlm_resource_slab, GFP_NOFS);
1056 INIT_LIST_HEAD(&res->lr_granted);
1057 INIT_LIST_HEAD(&res->lr_waiting);
1059 /* Initialize interval trees for each lock mode. */
1060 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1061 res->lr_itree[idx].lit_size = 0;
1062 res->lr_itree[idx].lit_mode = 1 << idx;
1063 res->lr_itree[idx].lit_root = NULL;
1066 atomic_set(&res->lr_refcount, 1);
1067 spin_lock_init(&res->lr_lock);
1068 lu_ref_init(&res->lr_reference);
1070 /* The creator of the resource must unlock the mutex after LVB
1073 mutex_init(&res->lr_lvb_mutex);
1074 mutex_lock(&res->lr_lvb_mutex);
1080 * Return a reference to resource with given name, creating it if necessary.
1081 * Args: namespace with ns_lock unlocked
1082 * Locks: takes and releases NS hash-lock and res->lr_lock
1083 * Returns: referenced, unlocked ldlm_resource or NULL
1085 struct ldlm_resource *
1086 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1087 const struct ldlm_res_id *name, enum ldlm_type type,
1090 struct hlist_node *hnode;
1091 struct ldlm_resource *res = NULL;
1092 struct cfs_hash_bd bd;
1094 int ns_refcount = 0;
1097 LASSERT(ns->ns_rs_hash);
1098 LASSERT(name->name[0] != 0);
1100 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1101 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1103 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1107 version = cfs_hash_bd_version_get(&bd);
1108 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1111 return ERR_PTR(-ENOENT);
1113 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1114 "type: %d\n", type);
1115 res = ldlm_resource_new();
1117 return ERR_PTR(-ENOMEM);
1119 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1120 res->lr_name = *name;
1121 res->lr_type = type;
1123 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1124 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1125 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1128 /* Someone won the race and already added the resource. */
1129 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1130 /* Clean lu_ref for failed resource. */
1131 lu_ref_fini(&res->lr_reference);
1132 /* We have taken lr_lvb_mutex. Drop it. */
1133 mutex_unlock(&res->lr_lvb_mutex);
1134 kmem_cache_free(ldlm_resource_slab, res);
1136 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1137 /* Synchronize with regard to resource creation. */
1138 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1139 mutex_lock(&res->lr_lvb_mutex);
1140 mutex_unlock(&res->lr_lvb_mutex);
1143 if (unlikely(res->lr_lvb_len < 0)) {
1144 ldlm_resource_putref(res);
1145 res = ERR_PTR(res->lr_lvb_len);
1149 /* We won! Let's add the resource. */
1150 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1151 if (cfs_hash_bd_count_get(&bd) == 1)
1152 ns_refcount = ldlm_namespace_get_return(ns);
1154 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1155 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1158 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1159 rc = ns->ns_lvbo->lvbo_init(res);
1161 CERROR("%s: lvbo_init failed for resource %#llx:%#llx: rc = %d\n",
1162 ns->ns_obd->obd_name, name->name[0],
1164 res->lr_lvb_len = rc;
1165 mutex_unlock(&res->lr_lvb_mutex);
1166 ldlm_resource_putref(res);
1171 /* We create resource with locked lr_lvb_mutex. */
1172 mutex_unlock(&res->lr_lvb_mutex);
1174 /* Let's see if we happened to be the very first resource in this
1175 * namespace. If so, and this is a client namespace, we need to move
1176 * the namespace into the active namespaces list to be patrolled by
1179 if (ns_refcount == 1) {
1180 mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1181 ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
1182 mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1187 EXPORT_SYMBOL(ldlm_resource_get);
1189 static void __ldlm_resource_putref_final(struct cfs_hash_bd *bd,
1190 struct ldlm_resource *res)
1192 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1194 if (!list_empty(&res->lr_granted)) {
1195 ldlm_resource_dump(D_ERROR, res);
1199 if (!list_empty(&res->lr_waiting)) {
1200 ldlm_resource_dump(D_ERROR, res);
1204 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1206 lu_ref_fini(&res->lr_reference);
1207 if (cfs_hash_bd_count_get(bd) == 0)
1208 ldlm_namespace_put(nsb->nsb_namespace);
1211 /* Returns 1 if the resource was freed, 0 if it remains. */
1212 int ldlm_resource_putref(struct ldlm_resource *res)
1214 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1215 struct cfs_hash_bd bd;
1217 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1218 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1219 res, atomic_read(&res->lr_refcount) - 1);
1221 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1222 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1223 __ldlm_resource_putref_final(&bd, res);
1224 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1225 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1226 ns->ns_lvbo->lvbo_free(res);
1227 kmem_cache_free(ldlm_resource_slab, res);
1232 EXPORT_SYMBOL(ldlm_resource_putref);
1234 /* Returns 1 if the resource was freed, 0 if it remains. */
1235 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1237 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1239 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1240 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1241 res, atomic_read(&res->lr_refcount) - 1);
1243 if (atomic_dec_and_test(&res->lr_refcount)) {
1244 struct cfs_hash_bd bd;
1246 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1247 &res->lr_name, &bd);
1248 __ldlm_resource_putref_final(&bd, res);
1249 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1250 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1251 * so we should never be here while calling cfs_hash_del,
1252 * cfs_hash_for_each_nolock is the only case we can get
1253 * here, which is safe to release cfs_hash_bd_lock.
1255 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1256 ns->ns_lvbo->lvbo_free(res);
1257 kmem_cache_free(ldlm_resource_slab, res);
1259 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1266 * Add a lock into a given resource into specified lock list.
1268 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1269 struct ldlm_lock *lock)
1271 check_res_locked(res);
1273 LDLM_DEBUG(lock, "About to add this lock:");
1275 if (ldlm_is_destroyed(lock)) {
1276 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1280 LASSERT(list_empty(&lock->l_res_link));
1282 list_add_tail(&lock->l_res_link, head);
1285 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1287 int type = lock->l_resource->lr_type;
1289 check_res_locked(lock->l_resource);
1290 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1291 ldlm_unlink_lock_skiplist(lock);
1292 else if (type == LDLM_EXTENT)
1293 ldlm_extent_unlink_lock(lock);
1294 list_del_init(&lock->l_res_link);
1296 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1298 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1300 desc->lr_type = res->lr_type;
1301 desc->lr_name = res->lr_name;
1305 * Print information about all locks in all namespaces on this node to debug
1308 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1310 struct list_head *tmp;
1312 if (!((libcfs_debug | D_ERROR) & level))
1315 mutex_lock(ldlm_namespace_lock(client));
1317 list_for_each(tmp, ldlm_namespace_list(client)) {
1318 struct ldlm_namespace *ns;
1320 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1321 ldlm_namespace_dump(level, ns);
1324 mutex_unlock(ldlm_namespace_lock(client));
1326 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1328 static int ldlm_res_hash_dump(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1329 struct hlist_node *hnode, void *arg)
1331 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1332 int level = (int)(unsigned long)arg;
1335 ldlm_resource_dump(level, res);
1342 * Print information about all locks in this namespace on this node to debug
1345 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1347 if (!((libcfs_debug | D_ERROR) & level))
1350 CDEBUG(level, "--- Namespace: %s (rc: %d, side: client)\n",
1351 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
1353 if (time_before(cfs_time_current(), ns->ns_next_dump))
1356 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1358 (void *)(unsigned long)level);
1359 spin_lock(&ns->ns_lock);
1360 ns->ns_next_dump = cfs_time_shift(10);
1361 spin_unlock(&ns->ns_lock);
1363 EXPORT_SYMBOL(ldlm_namespace_dump);
1366 * Print information about all locks in this resource to debug log.
1368 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1370 struct ldlm_lock *lock;
1371 unsigned int granted = 0;
1373 CLASSERT(RES_NAME_SIZE == 4);
1375 if (!((libcfs_debug | D_ERROR) & level))
1378 CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
1379 PLDLMRES(res), res, atomic_read(&res->lr_refcount));
1381 if (!list_empty(&res->lr_granted)) {
1382 CDEBUG(level, "Granted locks (in reverse order):\n");
1383 list_for_each_entry_reverse(lock, &res->lr_granted,
1385 LDLM_DEBUG_LIMIT(level, lock, "###");
1386 if (!(level & D_CANTMASK) &&
1387 ++granted > ldlm_dump_granted_max) {
1388 CDEBUG(level, "only dump %d granted locks to avoid DDOS.\n",
1394 if (!list_empty(&res->lr_waiting)) {
1395 CDEBUG(level, "Waiting locks:\n");
1396 list_for_each_entry(lock, &res->lr_waiting, l_res_link)
1397 LDLM_DEBUG_LIMIT(level, lock, "###");
1400 EXPORT_SYMBOL(ldlm_resource_dump);