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, 2016, 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 <lustre_dlm.h>
40 #include <lustre_fid.h>
41 #include <obd_class.h>
42 #include "ldlm_internal.h"
44 struct kmem_cache *ldlm_resource_slab, *ldlm_lock_slab;
45 struct kmem_cache *ldlm_interval_tree_slab;
47 int ldlm_srv_namespace_nr = 0;
48 int ldlm_cli_namespace_nr = 0;
50 DEFINE_MUTEX(ldlm_srv_namespace_lock);
51 LIST_HEAD(ldlm_srv_namespace_list);
53 DEFINE_MUTEX(ldlm_cli_namespace_lock);
54 /* Client Namespaces that have active resources in them.
55 * 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 LIST_HEAD(ldlm_cli_inactive_namespace_list);
61 static struct proc_dir_entry *ldlm_type_proc_dir;
62 static struct proc_dir_entry *ldlm_ns_proc_dir;
63 struct proc_dir_entry *ldlm_svc_proc_dir;
65 /* during debug dump certain amount of granted locks for one resource to avoid
67 static unsigned int ldlm_dump_granted_max = 256;
71 lprocfs_dump_ns_seq_write(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);
78 LPROC_SEQ_FOPS_WR_ONLY(ldlm, dump_ns);
80 LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint);
82 #ifdef HAVE_SERVER_SUPPORT
84 static int seq_watermark_show(struct seq_file *m, void *data)
86 seq_printf(m, "%llu\n", *(__u64 *)m->private);
90 static ssize_t seq_watermark_write(struct file *file,
91 const char __user *buffer, size_t count,
96 __u64 *data = ((struct seq_file *)file->private_data)->private;
97 bool wm_low = (data == &ldlm_reclaim_threshold_mb) ? true : false;
100 rc = lprocfs_str_with_units_to_s64(buffer, count, &value, 'M');
102 CERROR("Failed to set %s, rc = %d.\n",
103 wm_low ? "lock_reclaim_threshold_mb" : "lock_limit_mb",
106 } else if (value != 0 && value < (1 << 20)) {
107 CERROR("%s should be greater than 1MB.\n",
108 wm_low ? "lock_reclaim_threshold_mb" : "lock_limit_mb");
111 watermark = value >> 20;
114 if (ldlm_lock_limit_mb != 0 && watermark > ldlm_lock_limit_mb) {
115 CERROR("lock_reclaim_threshold_mb must be smaller than "
121 if (watermark != 0) {
123 do_div(watermark, sizeof(struct ldlm_lock));
125 ldlm_reclaim_threshold = watermark;
127 if (ldlm_reclaim_threshold_mb != 0 &&
128 watermark < ldlm_reclaim_threshold_mb) {
129 CERROR("lock_limit_mb must be greater than "
130 "lock_reclaim_threshold_mb.\n");
135 if (watermark != 0) {
137 do_div(watermark, sizeof(struct ldlm_lock));
139 ldlm_lock_limit = watermark;
145 static int seq_watermark_open(struct inode *inode, struct file *file)
147 return single_open(file, seq_watermark_show, PDE_DATA(inode));
150 static const struct file_operations ldlm_watermark_fops = {
151 .owner = THIS_MODULE,
152 .open = seq_watermark_open,
154 .write = seq_watermark_write,
156 .release = lprocfs_single_release,
159 static int seq_granted_show(struct seq_file *m, void *data)
161 seq_printf(m, "%llu\n", percpu_counter_sum_positive(
162 (struct percpu_counter *)m->private));
166 static int seq_granted_open(struct inode *inode, struct file *file)
168 return single_open(file, seq_granted_show, PDE_DATA(inode));
171 static const struct file_operations ldlm_granted_fops = {
172 .owner = THIS_MODULE,
173 .open = seq_granted_open,
176 .release = seq_release,
179 #endif /* HAVE_SERVER_SUPPORT */
181 int ldlm_proc_setup(void)
184 struct lprocfs_vars list[] = {
185 { .name = "dump_namespaces",
186 .fops = &ldlm_dump_ns_fops,
188 { .name = "dump_granted_max",
189 .fops = &ldlm_rw_uint_fops,
190 .data = &ldlm_dump_granted_max },
191 #ifdef HAVE_SERVER_SUPPORT
192 { .name = "lock_reclaim_threshold_mb",
193 .fops = &ldlm_watermark_fops,
194 .data = &ldlm_reclaim_threshold_mb },
195 { .name = "lock_limit_mb",
196 .fops = &ldlm_watermark_fops,
197 .data = &ldlm_lock_limit_mb },
198 { .name = "lock_granted_count",
199 .fops = &ldlm_granted_fops,
200 .data = &ldlm_granted_total },
204 LASSERT(ldlm_ns_proc_dir == NULL);
206 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
209 if (IS_ERR(ldlm_type_proc_dir)) {
210 CERROR("LProcFS failed in ldlm-init\n");
211 rc = PTR_ERR(ldlm_type_proc_dir);
215 ldlm_ns_proc_dir = lprocfs_register("namespaces",
218 if (IS_ERR(ldlm_ns_proc_dir)) {
219 CERROR("LProcFS failed in ldlm-init\n");
220 rc = PTR_ERR(ldlm_ns_proc_dir);
224 ldlm_svc_proc_dir = lprocfs_register("services",
227 if (IS_ERR(ldlm_svc_proc_dir)) {
228 CERROR("LProcFS failed in ldlm-init\n");
229 rc = PTR_ERR(ldlm_svc_proc_dir);
233 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
235 CERROR("LProcFS failed in ldlm-init\n");
242 lprocfs_remove(&ldlm_svc_proc_dir);
244 lprocfs_remove(&ldlm_ns_proc_dir);
246 lprocfs_remove(&ldlm_type_proc_dir);
248 ldlm_svc_proc_dir = NULL;
252 void ldlm_proc_cleanup(void)
254 if (ldlm_svc_proc_dir)
255 lprocfs_remove(&ldlm_svc_proc_dir);
257 if (ldlm_ns_proc_dir)
258 lprocfs_remove(&ldlm_ns_proc_dir);
260 if (ldlm_type_proc_dir)
261 lprocfs_remove(&ldlm_type_proc_dir);
264 static ssize_t resource_count_show(struct kobject *kobj, struct attribute *attr,
267 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
270 struct cfs_hash_bd bd;
273 /* result is not strictly consistant */
274 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
275 res += cfs_hash_bd_count_get(&bd);
276 return sprintf(buf, "%lld\n", res);
278 LUSTRE_RO_ATTR(resource_count);
280 static ssize_t lock_count_show(struct kobject *kobj, struct attribute *attr,
283 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
287 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
288 LPROCFS_FIELDS_FLAGS_SUM);
289 return sprintf(buf, "%lld\n", locks);
291 LUSTRE_RO_ATTR(lock_count);
293 static ssize_t lock_unused_count_show(struct kobject *kobj,
294 struct attribute *attr,
297 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
300 return sprintf(buf, "%d\n", ns->ns_nr_unused);
302 LUSTRE_RO_ATTR(lock_unused_count);
304 static ssize_t lru_size_show(struct kobject *kobj, struct attribute *attr,
307 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
309 __u32 *nr = &ns->ns_max_unused;
311 if (ns_connect_lru_resize(ns))
312 nr = &ns->ns_nr_unused;
313 return sprintf(buf, "%u\n", *nr);
316 static ssize_t lru_size_store(struct kobject *kobj, struct attribute *attr,
317 const char *buffer, size_t count)
319 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
325 if (strncmp(buffer, "clear", 5) == 0) {
327 "dropping all unused locks from namespace %s\n",
329 if (ns_connect_lru_resize(ns)) {
330 int canceled, unused = ns->ns_nr_unused;
332 /* Try to cancel all @ns_nr_unused locks. */
333 canceled = ldlm_cancel_lru(ns, unused, 0,
334 LDLM_LRU_FLAG_PASSED |
335 LDLM_LRU_FLAG_CLEANUP);
336 if (canceled < unused) {
338 "not all requested locks are canceled, requested: %d, canceled: %d\n",
344 tmp = ns->ns_max_unused;
345 ns->ns_max_unused = 0;
346 ldlm_cancel_lru(ns, 0, 0, LDLM_LRU_FLAG_PASSED |
347 LDLM_LRU_FLAG_CLEANUP);
348 ns->ns_max_unused = tmp;
353 err = kstrtoul(buffer, 10, &tmp);
355 CERROR("lru_size: invalid value written\n");
358 lru_resize = (tmp == 0);
360 if (ns_connect_lru_resize(ns)) {
362 ns->ns_max_unused = (unsigned int)tmp;
364 if (tmp > ns->ns_nr_unused)
365 tmp = ns->ns_nr_unused;
366 tmp = ns->ns_nr_unused - tmp;
369 "changing namespace %s unused locks from %u to %u\n",
370 ldlm_ns_name(ns), ns->ns_nr_unused,
372 ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_LRU_FLAG_PASSED);
376 "disable lru_resize for namespace %s\n",
378 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
382 "changing namespace %s max_unused from %u to %u\n",
383 ldlm_ns_name(ns), ns->ns_max_unused,
385 ns->ns_max_unused = (unsigned int)tmp;
386 ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_LRU_FLAG_PASSED);
388 /* Make sure that LRU resize was originally supported before
389 * turning it on here.
392 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
394 "enable lru_resize for namespace %s\n",
396 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
402 LUSTRE_RW_ATTR(lru_size);
404 static ssize_t lru_max_age_show(struct kobject *kobj, struct attribute *attr,
407 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
410 return sprintf(buf, "%u\n", ns->ns_max_age);
413 static ssize_t lru_max_age_store(struct kobject *kobj, struct attribute *attr,
414 const char *buffer, size_t count)
416 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
421 err = kstrtoul(buffer, 10, &tmp);
425 ns->ns_max_age = tmp;
429 LUSTRE_RW_ATTR(lru_max_age);
431 static ssize_t early_lock_cancel_show(struct kobject *kobj,
432 struct attribute *attr,
435 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
438 return sprintf(buf, "%d\n", ns_connect_cancelset(ns));
441 static ssize_t early_lock_cancel_store(struct kobject *kobj,
442 struct attribute *attr,
446 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
448 unsigned long supp = -1;
451 rc = kstrtoul(buffer, 10, &supp);
456 ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
457 else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
458 ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
461 LUSTRE_RW_ATTR(early_lock_cancel);
463 #ifdef HAVE_SERVER_SUPPORT
464 static ssize_t ctime_age_limit_show(struct kobject *kobj,
465 struct attribute *attr, char *buf)
467 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
470 return sprintf(buf, "%u\n", ns->ns_ctime_age_limit);
473 static ssize_t ctime_age_limit_store(struct kobject *kobj,
474 struct attribute *attr,
475 const char *buffer, size_t count)
477 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
482 err = kstrtoul(buffer, 10, &tmp);
486 ns->ns_ctime_age_limit = tmp;
490 LUSTRE_RW_ATTR(ctime_age_limit);
492 static ssize_t lock_timeouts_show(struct kobject *kobj, struct attribute *attr,
495 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
498 return sprintf(buf, "%d\n", ns->ns_timeouts);
500 LUSTRE_RO_ATTR(lock_timeouts);
502 static ssize_t max_nolock_bytes_show(struct kobject *kobj,
503 struct attribute *attr, char *buf)
505 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
508 return sprintf(buf, "%u\n", ns->ns_max_nolock_size);
511 static ssize_t max_nolock_bytes_store(struct kobject *kobj,
512 struct attribute *attr,
513 const char *buffer, size_t count)
515 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
520 err = kstrtoul(buffer, 10, &tmp);
524 ns->ns_max_nolock_size = tmp;
528 LUSTRE_RW_ATTR(max_nolock_bytes);
530 static ssize_t contention_seconds_show(struct kobject *kobj,
531 struct attribute *attr, char *buf)
533 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
536 return sprintf(buf, "%u\n", ns->ns_contention_time);
539 static ssize_t contention_seconds_store(struct kobject *kobj,
540 struct attribute *attr,
541 const char *buffer, size_t count)
543 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
548 err = kstrtoul(buffer, 10, &tmp);
552 ns->ns_contention_time = tmp;
556 LUSTRE_RW_ATTR(contention_seconds);
558 static ssize_t contended_locks_show(struct kobject *kobj,
559 struct attribute *attr, char *buf)
561 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
564 return sprintf(buf, "%u\n", ns->ns_contended_locks);
567 static ssize_t contended_locks_store(struct kobject *kobj,
568 struct attribute *attr,
569 const char *buffer, size_t count)
571 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
576 err = kstrtoul(buffer, 10, &tmp);
580 ns->ns_contended_locks = tmp;
584 LUSTRE_RW_ATTR(contended_locks);
586 static ssize_t max_parallel_ast_show(struct kobject *kobj,
587 struct attribute *attr, char *buf)
589 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
592 return sprintf(buf, "%u\n", ns->ns_max_parallel_ast);
595 static ssize_t max_parallel_ast_store(struct kobject *kobj,
596 struct attribute *attr,
597 const char *buffer, size_t count)
599 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
604 err = kstrtoul(buffer, 10, &tmp);
608 ns->ns_max_parallel_ast = tmp;
612 LUSTRE_RW_ATTR(max_parallel_ast);
614 #endif /* HAVE_SERVER_SUPPORT */
616 /* These are for namespaces in /sys/fs/lustre/ldlm/namespaces/ */
617 static struct attribute *ldlm_ns_attrs[] = {
618 &lustre_attr_resource_count.attr,
619 &lustre_attr_lock_count.attr,
620 &lustre_attr_lock_unused_count.attr,
621 &lustre_attr_lru_size.attr,
622 &lustre_attr_lru_max_age.attr,
623 &lustre_attr_early_lock_cancel.attr,
624 #ifdef HAVE_SERVER_SUPPORT
625 &lustre_attr_ctime_age_limit.attr,
626 &lustre_attr_lock_timeouts.attr,
627 &lustre_attr_max_nolock_bytes.attr,
628 &lustre_attr_contention_seconds.attr,
629 &lustre_attr_contended_locks.attr,
630 &lustre_attr_max_parallel_ast.attr,
635 static void ldlm_ns_release(struct kobject *kobj)
637 struct ldlm_namespace *ns = container_of(kobj, struct ldlm_namespace,
639 complete(&ns->ns_kobj_unregister);
642 static struct kobj_type ldlm_ns_ktype = {
643 .default_attrs = ldlm_ns_attrs,
644 .sysfs_ops = &lustre_sysfs_ops,
645 .release = ldlm_ns_release,
648 static void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
650 if (ns->ns_proc_dir_entry == NULL)
651 CERROR("dlm namespace %s has no procfs dir?\n",
654 lprocfs_remove(&ns->ns_proc_dir_entry);
656 if (ns->ns_stats != NULL)
657 lprocfs_free_stats(&ns->ns_stats);
660 void ldlm_namespace_sysfs_unregister(struct ldlm_namespace *ns)
662 kobject_put(&ns->ns_kobj);
663 wait_for_completion(&ns->ns_kobj_unregister);
666 int ldlm_namespace_sysfs_register(struct ldlm_namespace *ns)
670 ns->ns_kobj.kset = ldlm_ns_kset;
671 init_completion(&ns->ns_kobj_unregister);
672 err = kobject_init_and_add(&ns->ns_kobj, &ldlm_ns_ktype, NULL,
673 "%s", ldlm_ns_name(ns));
675 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
677 kobject_put(&ns->ns_kobj);
681 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
682 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
687 static int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
689 struct proc_dir_entry *ns_pde;
692 LASSERT(ns->ns_rs_hash != NULL);
694 if (ns->ns_proc_dir_entry != NULL) {
695 ns_pde = ns->ns_proc_dir_entry;
697 ns_pde = proc_mkdir(ldlm_ns_name(ns), ldlm_ns_proc_dir);
700 ns->ns_proc_dir_entry = ns_pde;
705 #undef MAX_STRING_SIZE
706 #else /* CONFIG_PROC_FS */
708 #define ldlm_namespace_proc_unregister(ns) ({;})
709 #define ldlm_namespace_proc_register(ns) ({0;})
711 #endif /* CONFIG_PROC_FS */
713 static unsigned ldlm_res_hop_hash(struct cfs_hash *hs,
714 const void *key, unsigned mask)
716 const struct ldlm_res_id *id = key;
720 for (i = 0; i < RES_NAME_SIZE; i++)
725 static unsigned ldlm_res_hop_fid_hash(struct cfs_hash *hs,
726 const void *key, unsigned mask)
728 const struct ldlm_res_id *id = key;
733 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
734 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
735 fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
737 hash = fid_flatten32(&fid);
738 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
739 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
740 val = id->name[LUSTRE_RES_ID_HSH_OFF];
741 hash += (val >> 5) + (val << 11);
745 hash = hash_long(hash, hs->hs_bkt_bits);
746 /* give me another random factor */
747 hash -= hash_long((unsigned long)hs, val % 11 + 3);
749 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
750 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
755 static void *ldlm_res_hop_key(struct hlist_node *hnode)
757 struct ldlm_resource *res;
759 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
760 return &res->lr_name;
763 static int ldlm_res_hop_keycmp(const void *key, struct hlist_node *hnode)
765 struct ldlm_resource *res;
767 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
768 return ldlm_res_eq((const struct ldlm_res_id *)key,
769 (const struct ldlm_res_id *)&res->lr_name);
772 static void *ldlm_res_hop_object(struct hlist_node *hnode)
774 return hlist_entry(hnode, struct ldlm_resource, lr_hash);
778 ldlm_res_hop_get_locked(struct cfs_hash *hs, struct hlist_node *hnode)
780 struct ldlm_resource *res;
782 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
783 ldlm_resource_getref(res);
786 static void ldlm_res_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
788 struct ldlm_resource *res;
790 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
791 ldlm_resource_putref(res);
794 static struct cfs_hash_ops ldlm_ns_hash_ops = {
795 .hs_hash = ldlm_res_hop_hash,
796 .hs_key = ldlm_res_hop_key,
797 .hs_keycmp = ldlm_res_hop_keycmp,
799 .hs_object = ldlm_res_hop_object,
800 .hs_get = ldlm_res_hop_get_locked,
801 .hs_put = ldlm_res_hop_put
804 static struct cfs_hash_ops ldlm_ns_fid_hash_ops = {
805 .hs_hash = ldlm_res_hop_fid_hash,
806 .hs_key = ldlm_res_hop_key,
807 .hs_keycmp = ldlm_res_hop_keycmp,
809 .hs_object = ldlm_res_hop_object,
810 .hs_get = ldlm_res_hop_get_locked,
811 .hs_put = ldlm_res_hop_put
814 typedef struct ldlm_ns_hash_def {
815 enum ldlm_ns_type nsd_type;
816 /** hash bucket bits */
817 unsigned nsd_bkt_bits;
819 unsigned nsd_all_bits;
820 /** hash operations */
821 struct cfs_hash_ops *nsd_hops;
822 } ldlm_ns_hash_def_t;
824 static struct ldlm_ns_hash_def ldlm_ns_hash_defs[] =
827 .nsd_type = LDLM_NS_TYPE_MDC,
830 .nsd_hops = &ldlm_ns_fid_hash_ops,
833 .nsd_type = LDLM_NS_TYPE_MDT,
836 .nsd_hops = &ldlm_ns_fid_hash_ops,
839 .nsd_type = LDLM_NS_TYPE_OSC,
842 .nsd_hops = &ldlm_ns_hash_ops,
845 .nsd_type = LDLM_NS_TYPE_OST,
848 .nsd_hops = &ldlm_ns_hash_ops,
851 .nsd_type = LDLM_NS_TYPE_MGC,
854 .nsd_hops = &ldlm_ns_hash_ops,
857 .nsd_type = LDLM_NS_TYPE_MGT,
860 .nsd_hops = &ldlm_ns_hash_ops,
863 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
868 * Create and initialize new empty namespace.
870 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
871 enum ldlm_side client,
872 enum ldlm_appetite apt,
873 enum ldlm_ns_type ns_type)
875 struct ldlm_namespace *ns = NULL;
876 struct ldlm_ns_bucket *nsb;
877 struct ldlm_ns_hash_def *nsd;
878 struct cfs_hash_bd bd;
883 LASSERT(obd != NULL);
887 CERROR("ldlm_get_ref failed: %d\n", rc);
891 for (idx = 0;;idx++) {
892 nsd = &ldlm_ns_hash_defs[idx];
893 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
894 CERROR("Unknown type %d for ns %s\n", ns_type, name);
898 if (nsd->nsd_type == ns_type)
906 ns->ns_rs_hash = cfs_hash_create(name,
907 nsd->nsd_all_bits, nsd->nsd_all_bits,
908 nsd->nsd_bkt_bits, sizeof(*nsb),
914 CFS_HASH_SPIN_BKTLOCK |
915 CFS_HASH_NO_ITEMREF);
916 if (ns->ns_rs_hash == NULL)
919 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
920 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
921 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
922 nsb->nsb_namespace = ns;
923 nsb->nsb_reclaim_start = 0;
927 ns->ns_appetite = apt;
928 ns->ns_client = client;
930 INIT_LIST_HEAD(&ns->ns_list_chain);
931 INIT_LIST_HEAD(&ns->ns_unused_list);
932 spin_lock_init(&ns->ns_lock);
933 atomic_set(&ns->ns_bref, 0);
934 init_waitqueue_head(&ns->ns_waitq);
936 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
937 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
938 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
940 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
941 ns->ns_nr_unused = 0;
942 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
943 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
944 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
946 ns->ns_orig_connect_flags = 0;
947 ns->ns_connect_flags = 0;
949 ns->ns_reclaim_start = 0;
951 rc = ldlm_namespace_sysfs_register(ns);
953 CERROR("Can't initialize ns sysfs, rc %d\n", rc);
957 rc = ldlm_namespace_proc_register(ns);
959 CERROR("Can't initialize ns proc, rc %d\n", rc);
963 idx = ldlm_namespace_nr_read(client);
964 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
966 CERROR("Can't initialize lock pool, rc %d\n", rc);
970 ldlm_namespace_register(ns, client);
973 ldlm_namespace_proc_unregister(ns);
975 ldlm_namespace_sysfs_unregister(ns);
976 ldlm_namespace_cleanup(ns, 0);
978 cfs_hash_putref(ns->ns_rs_hash);
985 EXPORT_SYMBOL(ldlm_namespace_new);
987 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
990 * Cancel and destroy all locks on a resource.
992 * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
993 * clean up. This is currently only used for recovery, and we make
994 * certain assumptions as a result--notably, that we shouldn't cancel
997 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
1000 struct list_head *tmp;
1001 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
1002 bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
1005 struct ldlm_lock *lock = NULL;
1007 /* First, we look for non-cleaned-yet lock
1008 * all cleaned locks are marked by CLEANED flag. */
1010 list_for_each(tmp, q) {
1011 lock = list_entry(tmp, struct ldlm_lock,
1013 if (ldlm_is_cleaned(lock)) {
1017 LDLM_LOCK_GET(lock);
1018 ldlm_set_cleaned(lock);
1027 /* Set CBPENDING so nothing in the cancellation path
1028 * can match this lock. */
1029 ldlm_set_cbpending(lock);
1030 ldlm_set_failed(lock);
1031 lock->l_flags |= flags;
1033 /* ... without sending a CANCEL message for local_only. */
1035 ldlm_set_local_only(lock);
1037 if (local_only && (lock->l_readers || lock->l_writers)) {
1038 /* This is a little bit gross, but much better than the
1039 * alternative: pretend that we got a blocking AST from
1040 * the server, so that when the lock is decref'd, it
1041 * will go away ... */
1043 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
1044 if (lock->l_flags & LDLM_FL_FAIL_LOC) {
1045 set_current_state(TASK_UNINTERRUPTIBLE);
1046 schedule_timeout(cfs_time_seconds(4));
1047 set_current_state(TASK_RUNNING);
1049 if (lock->l_completion_ast)
1050 lock->l_completion_ast(lock,
1051 LDLM_FL_FAILED, NULL);
1052 LDLM_LOCK_RELEASE(lock);
1057 struct lustre_handle lockh;
1060 ldlm_lock2handle(lock, &lockh);
1061 rc = ldlm_cli_cancel(&lockh, LCF_LOCAL);
1063 CERROR("ldlm_cli_cancel: %d\n", rc);
1066 LDLM_DEBUG(lock, "Freeing a lock still held by a "
1068 ldlm_lock_cancel(lock);
1070 LDLM_LOCK_RELEASE(lock);
1074 static int ldlm_resource_clean(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1075 struct hlist_node *hnode, void *arg)
1077 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1078 __u64 flags = *(__u64 *)arg;
1080 cleanup_resource(res, &res->lr_granted, flags);
1081 cleanup_resource(res, &res->lr_converting, flags);
1082 cleanup_resource(res, &res->lr_waiting, flags);
1087 static int ldlm_resource_complain(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1088 struct hlist_node *hnode, void *arg)
1090 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1093 CERROR("%s: namespace resource "DLDLMRES" (%p) refcount nonzero "
1094 "(%d) after lock cleanup; forcing cleanup.\n",
1095 ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
1096 atomic_read(&res->lr_refcount) - 1);
1098 ldlm_resource_dump(D_ERROR, res);
1104 * Cancel and destroy all locks in the namespace.
1106 * Typically used during evictions when server notified client that it was
1107 * evicted and all of its state needs to be destroyed.
1108 * Also used during shutdown.
1110 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
1113 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
1117 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean,
1119 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain,
1123 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1126 * Attempts to free namespace.
1128 * Only used when namespace goes away, like during an unmount.
1130 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
1134 /* At shutdown time, don't call the cancellation callback */
1135 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
1137 if (atomic_read(&ns->ns_bref) > 0) {
1138 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1141 "dlm namespace %s free waiting on refcount %d\n",
1142 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
1145 lwi = LWI_TIMEOUT(msecs_to_jiffies(obd_timeout *
1146 MSEC_PER_SEC) / 4, NULL, NULL);
1148 rc = l_wait_event(ns->ns_waitq,
1149 atomic_read(&ns->ns_bref) == 0, &lwi);
1151 /* Forced cleanups should be able to reclaim all references,
1152 * so it's safe to wait forever... we can't leak locks... */
1153 if (force && rc == -ETIMEDOUT) {
1154 LCONSOLE_ERROR("Forced cleanup waiting for %s "
1155 "namespace with %d resources in use, "
1156 "(rc=%d)\n", ldlm_ns_name(ns),
1157 atomic_read(&ns->ns_bref), rc);
1158 GOTO(force_wait, rc);
1161 if (atomic_read(&ns->ns_bref)) {
1162 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
1163 "with %d resources in use, (rc=%d)\n",
1165 atomic_read(&ns->ns_bref), rc);
1166 RETURN(ELDLM_NAMESPACE_EXISTS);
1168 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
1176 * Performs various cleanups for passed \a ns to make it drop refc and be
1177 * ready for freeing. Waits for refc == 0.
1179 * The following is done:
1180 * (0) Unregister \a ns from its list to make inaccessible for potential
1181 * users like pools thread and others;
1182 * (1) Clear all locks in \a ns.
1184 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
1185 struct obd_import *imp,
1195 spin_lock(&ns->ns_lock);
1196 ns->ns_stopping = 1;
1197 spin_unlock(&ns->ns_lock);
1200 * Can fail with -EINTR when force == 0 in which case try harder.
1202 rc = __ldlm_namespace_free(ns, force);
1203 if (rc != ELDLM_OK) {
1205 ptlrpc_disconnect_import(imp, 0);
1206 ptlrpc_invalidate_import(imp);
1210 * With all requests dropped and the import inactive
1211 * we are gaurenteed all reference will be dropped.
1213 rc = __ldlm_namespace_free(ns, 1);
1218 EXPORT_SYMBOL(ldlm_namespace_free_prior);
1221 * Performs freeing memory structures related to \a ns. This is only done
1222 * when ldlm_namespce_free_prior() successfully removed all resources
1223 * referencing \a ns and its refc == 0.
1225 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
1233 /* Make sure that nobody can find this ns in its list. */
1234 ldlm_namespace_unregister(ns, ns->ns_client);
1235 /* Fini pool _before_ parent proc dir is removed. This is important as
1236 * ldlm_pool_fini() removes own proc dir which is child to @dir.
1237 * Removing it after @dir may cause oops. */
1238 ldlm_pool_fini(&ns->ns_pool);
1240 ldlm_namespace_proc_unregister(ns);
1241 ldlm_namespace_sysfs_unregister(ns);
1242 cfs_hash_putref(ns->ns_rs_hash);
1243 /* Namespace \a ns should be not on list at this time, otherwise
1244 * this will cause issues related to using freed \a ns in poold
1246 LASSERT(list_empty(&ns->ns_list_chain));
1251 EXPORT_SYMBOL(ldlm_namespace_free_post);
1254 * Cleanup the resource, and free namespace.
1257 * proc1: destroy import
1258 * class_disconnect_export(grab cl_sem) ->
1259 * -> ldlm_namespace_free ->
1260 * -> lprocfs_remove(grab _lprocfs_lock).
1261 * proc2: read proc info
1262 * lprocfs_fops_read(grab _lprocfs_lock) ->
1263 * -> osc_rd_active, etc(grab cl_sem).
1265 * So that I have to split the ldlm_namespace_free into two parts - the first
1266 * part ldlm_namespace_free_prior is used to cleanup the resource which is
1267 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
1268 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
1271 void ldlm_namespace_free(struct ldlm_namespace *ns,
1272 struct obd_import *imp,
1275 ldlm_namespace_free_prior(ns, imp, force);
1276 ldlm_namespace_free_post(ns);
1278 EXPORT_SYMBOL(ldlm_namespace_free);
1280 void ldlm_namespace_get(struct ldlm_namespace *ns)
1282 atomic_inc(&ns->ns_bref);
1285 /* This is only for callers that care about refcount */
1286 static int ldlm_namespace_get_return(struct ldlm_namespace *ns)
1288 return atomic_inc_return(&ns->ns_bref);
1291 void ldlm_namespace_put(struct ldlm_namespace *ns)
1293 if (atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
1294 wake_up(&ns->ns_waitq);
1295 spin_unlock(&ns->ns_lock);
1299 /** Register \a ns in the list of namespaces */
1300 void ldlm_namespace_register(struct ldlm_namespace *ns, enum ldlm_side client)
1302 mutex_lock(ldlm_namespace_lock(client));
1303 LASSERT(list_empty(&ns->ns_list_chain));
1304 list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
1305 ldlm_namespace_nr_inc(client);
1306 mutex_unlock(ldlm_namespace_lock(client));
1309 /** Unregister \a ns from the list of namespaces. */
1310 void ldlm_namespace_unregister(struct ldlm_namespace *ns, enum ldlm_side client)
1312 mutex_lock(ldlm_namespace_lock(client));
1313 LASSERT(!list_empty(&ns->ns_list_chain));
1314 /* Some asserts and possibly other parts of the code are still
1315 * using list_empty(&ns->ns_list_chain). This is why it is
1316 * important to use list_del_init() here. */
1317 list_del_init(&ns->ns_list_chain);
1318 ldlm_namespace_nr_dec(client);
1319 mutex_unlock(ldlm_namespace_lock(client));
1322 /** Should be called with ldlm_namespace_lock(client) taken. */
1323 void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
1324 enum ldlm_side client)
1326 LASSERT(!list_empty(&ns->ns_list_chain));
1327 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1328 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
1331 /** Should be called with ldlm_namespace_lock(client) taken. */
1332 void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
1333 enum ldlm_side client)
1335 LASSERT(!list_empty(&ns->ns_list_chain));
1336 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1337 list_move_tail(&ns->ns_list_chain,
1338 ldlm_namespace_inactive_list(client));
1341 /** Should be called with ldlm_namespace_lock(client) taken. */
1342 struct ldlm_namespace *ldlm_namespace_first_locked(enum ldlm_side client)
1344 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1345 LASSERT(!list_empty(ldlm_namespace_list(client)));
1346 return container_of(ldlm_namespace_list(client)->next,
1347 struct ldlm_namespace, ns_list_chain);
1350 /** Create and initialize new resource. */
1351 static struct ldlm_resource *ldlm_resource_new(enum ldlm_type ldlm_type)
1353 struct ldlm_resource *res;
1356 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, GFP_NOFS);
1360 if (ldlm_type == LDLM_EXTENT) {
1361 OBD_SLAB_ALLOC(res->lr_itree, ldlm_interval_tree_slab,
1362 sizeof(*res->lr_itree) * LCK_MODE_NUM);
1363 if (res->lr_itree == NULL) {
1364 OBD_SLAB_FREE_PTR(res, ldlm_resource_slab);
1367 /* Initialize interval trees for each lock mode. */
1368 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1369 res->lr_itree[idx].lit_size = 0;
1370 res->lr_itree[idx].lit_mode = 1 << idx;
1371 res->lr_itree[idx].lit_root = NULL;
1375 INIT_LIST_HEAD(&res->lr_granted);
1376 INIT_LIST_HEAD(&res->lr_converting);
1377 INIT_LIST_HEAD(&res->lr_waiting);
1379 atomic_set(&res->lr_refcount, 1);
1380 spin_lock_init(&res->lr_lock);
1381 lu_ref_init(&res->lr_reference);
1383 /* Since LVB init can be delayed now, there is no longer need to
1384 * immediatelly acquire mutex here. */
1385 mutex_init(&res->lr_lvb_mutex);
1386 res->lr_lvb_initialized = false;
1392 * Return a reference to resource with given name, creating it if necessary.
1393 * Args: namespace with ns_lock unlocked
1394 * Locks: takes and releases NS hash-lock and res->lr_lock
1395 * Returns: referenced, unlocked ldlm_resource or NULL
1397 struct ldlm_resource *
1398 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1399 const struct ldlm_res_id *name, enum ldlm_type type,
1402 struct hlist_node *hnode;
1403 struct ldlm_resource *res = NULL;
1404 struct cfs_hash_bd bd;
1406 int ns_refcount = 0;
1408 LASSERT(ns != NULL);
1409 LASSERT(parent == NULL);
1410 LASSERT(ns->ns_rs_hash != NULL);
1411 LASSERT(name->name[0] != 0);
1413 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1414 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1415 if (hnode != NULL) {
1416 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1420 version = cfs_hash_bd_version_get(&bd);
1421 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1424 return ERR_PTR(-ENOENT);
1426 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1427 "type: %d\n", type);
1428 res = ldlm_resource_new(type);
1430 return ERR_PTR(-ENOMEM);
1432 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1433 res->lr_name = *name;
1434 res->lr_type = type;
1436 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1437 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1438 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1440 if (hnode != NULL) {
1441 /* Someone won the race and already added the resource. */
1442 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1443 /* Clean lu_ref for failed resource. */
1444 lu_ref_fini(&res->lr_reference);
1445 if (res->lr_itree != NULL)
1446 OBD_SLAB_FREE(res->lr_itree, ldlm_interval_tree_slab,
1447 sizeof(*res->lr_itree) * LCK_MODE_NUM);
1448 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1450 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1453 /* We won! Let's add the resource. */
1454 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1455 if (cfs_hash_bd_count_get(&bd) == 1)
1456 ns_refcount = ldlm_namespace_get_return(ns);
1458 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1460 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1462 /* Let's see if we happened to be the very first resource in this
1463 * namespace. If so, and this is a client namespace, we need to move
1464 * the namespace into the active namespaces list to be patrolled by
1465 * the ldlm_poold. */
1466 if (ns_is_client(ns) && ns_refcount == 1) {
1467 mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1468 ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
1469 mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1474 EXPORT_SYMBOL(ldlm_resource_get);
1476 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1478 LASSERT(res != NULL);
1479 LASSERT(res != LP_POISON);
1480 atomic_inc(&res->lr_refcount);
1481 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1482 atomic_read(&res->lr_refcount));
1486 static void __ldlm_resource_putref_final(struct cfs_hash_bd *bd,
1487 struct ldlm_resource *res)
1489 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1491 if (!list_empty(&res->lr_granted)) {
1492 ldlm_resource_dump(D_ERROR, res);
1496 if (!list_empty(&res->lr_converting)) {
1497 ldlm_resource_dump(D_ERROR, res);
1501 if (!list_empty(&res->lr_waiting)) {
1502 ldlm_resource_dump(D_ERROR, res);
1506 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1508 lu_ref_fini(&res->lr_reference);
1509 if (cfs_hash_bd_count_get(bd) == 0)
1510 ldlm_namespace_put(nsb->nsb_namespace);
1513 /* Returns 1 if the resource was freed, 0 if it remains. */
1514 int ldlm_resource_putref(struct ldlm_resource *res)
1516 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1517 struct cfs_hash_bd bd;
1519 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1520 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1521 res, atomic_read(&res->lr_refcount) - 1);
1523 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1524 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1525 __ldlm_resource_putref_final(&bd, res);
1526 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1527 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1528 ns->ns_lvbo->lvbo_free(res);
1529 if (res->lr_itree != NULL)
1530 OBD_SLAB_FREE(res->lr_itree, ldlm_interval_tree_slab,
1531 sizeof(*res->lr_itree) * LCK_MODE_NUM);
1532 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1537 EXPORT_SYMBOL(ldlm_resource_putref);
1540 * Add a lock into a given resource into specified lock list.
1542 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1543 struct ldlm_lock *lock)
1545 check_res_locked(res);
1547 LDLM_DEBUG(lock, "About to add this lock");
1549 if (ldlm_is_destroyed(lock)) {
1550 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1554 LASSERT(list_empty(&lock->l_res_link));
1556 list_add_tail(&lock->l_res_link, head);
1560 * Insert a lock into resource after specified lock.
1562 * Obtain resource description from the lock we are inserting after.
1564 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1565 struct ldlm_lock *new)
1567 struct ldlm_resource *res = original->l_resource;
1569 check_res_locked(res);
1571 ldlm_resource_dump(D_INFO, res);
1572 LDLM_DEBUG(new, "About to insert this lock after %p: ", original);
1574 if (ldlm_is_destroyed(new)) {
1575 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1579 LASSERT(list_empty(&new->l_res_link));
1581 list_add(&new->l_res_link, &original->l_res_link);
1585 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1587 int type = lock->l_resource->lr_type;
1589 check_res_locked(lock->l_resource);
1590 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1591 ldlm_unlink_lock_skiplist(lock);
1592 else if (type == LDLM_EXTENT)
1593 ldlm_extent_unlink_lock(lock);
1594 list_del_init(&lock->l_res_link);
1596 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1598 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1600 desc->lr_type = res->lr_type;
1601 desc->lr_name = res->lr_name;
1605 * Print information about all locks in all namespaces on this node to debug
1608 void ldlm_dump_all_namespaces(enum ldlm_side client, int level)
1610 struct list_head *tmp;
1612 if (!((libcfs_debug | D_ERROR) & level))
1615 mutex_lock(ldlm_namespace_lock(client));
1617 list_for_each(tmp, ldlm_namespace_list(client)) {
1618 struct ldlm_namespace *ns;
1620 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1621 ldlm_namespace_dump(level, ns);
1624 mutex_unlock(ldlm_namespace_lock(client));
1627 static int ldlm_res_hash_dump(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1628 struct hlist_node *hnode, void *arg)
1630 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1631 int level = (int)(unsigned long)arg;
1634 ldlm_resource_dump(level, res);
1641 * Print information about all locks in this namespace on this node to debug
1644 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1646 if (!((libcfs_debug | D_ERROR) & level))
1649 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1650 ldlm_ns_name(ns), atomic_read(&ns->ns_bref),
1651 ns_is_client(ns) ? "client" : "server");
1653 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1656 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1658 (void *)(unsigned long)level, 0);
1659 spin_lock(&ns->ns_lock);
1660 ns->ns_next_dump = cfs_time_shift(10);
1661 spin_unlock(&ns->ns_lock);
1665 * Print information about all locks in this resource to debug log.
1667 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1669 struct ldlm_lock *lock;
1670 unsigned int granted = 0;
1672 CLASSERT(RES_NAME_SIZE == 4);
1674 if (!((libcfs_debug | D_ERROR) & level))
1677 CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
1678 PLDLMRES(res), res, atomic_read(&res->lr_refcount));
1680 if (!list_empty(&res->lr_granted)) {
1681 CDEBUG(level, "Granted locks (in reverse order):\n");
1682 list_for_each_entry_reverse(lock, &res->lr_granted,
1684 LDLM_DEBUG_LIMIT(level, lock, "###");
1685 if (!(level & D_CANTMASK) &&
1686 ++granted > ldlm_dump_granted_max) {
1687 CDEBUG(level, "only dump %d granted locks to "
1688 "avoid DDOS.\n", granted);
1693 if (!list_empty(&res->lr_converting)) {
1694 CDEBUG(level, "Converting locks:\n");
1695 list_for_each_entry(lock, &res->lr_converting, l_res_link)
1696 LDLM_DEBUG_LIMIT(level, lock, "###");
1698 if (!list_empty(&res->lr_waiting)) {
1699 CDEBUG(level, "Waiting locks:\n");
1700 list_for_each_entry(lock, &res->lr_waiting, l_res_link)
1701 LDLM_DEBUG_LIMIT(level, lock, "###");
1704 EXPORT_SYMBOL(ldlm_resource_dump);