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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_resource.c
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Peter Braam <braam@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
43 #include "../include/lustre_dlm.h"
44 #include "../include/lustre_fid.h"
45 #include "../include/obd_class.h"
46 #include "ldlm_internal.h"
48 struct kmem_cache *ldlm_resource_slab, *ldlm_lock_slab;
50 int ldlm_srv_namespace_nr = 0;
51 int ldlm_cli_namespace_nr = 0;
53 struct mutex ldlm_srv_namespace_lock;
54 LIST_HEAD(ldlm_srv_namespace_list);
56 struct mutex ldlm_cli_namespace_lock;
57 /* Client Namespaces that have active resources in them.
58 * Once all resources go away, ldlm_poold moves such namespaces to the
60 LIST_HEAD(ldlm_cli_active_namespace_list);
61 /* Client namespaces that don't have any locks in them */
62 LIST_HEAD(ldlm_cli_inactive_namespace_list);
64 struct proc_dir_entry *ldlm_type_proc_dir = NULL;
65 struct proc_dir_entry *ldlm_ns_proc_dir = NULL;
66 struct proc_dir_entry *ldlm_svc_proc_dir = NULL;
68 extern unsigned int ldlm_cancel_unused_locks_before_replay;
70 /* during debug dump certain amount of granted locks for one resource to avoid
72 unsigned int ldlm_dump_granted_max = 256;
74 #if defined (CONFIG_PROC_FS)
75 static ssize_t lprocfs_wr_dump_ns(struct file *file, const char *buffer,
76 size_t count, loff_t *off)
78 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
79 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
82 LPROC_SEQ_FOPS_WR_ONLY(ldlm, dump_ns);
84 LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint);
85 LPROC_SEQ_FOPS_RO_TYPE(ldlm, uint);
87 int ldlm_proc_setup(void)
90 struct lprocfs_vars list[] = {
91 { "dump_namespaces", &ldlm_dump_ns_fops, NULL, 0222 },
92 { "dump_granted_max", &ldlm_rw_uint_fops,
93 &ldlm_dump_granted_max },
94 { "cancel_unused_locks_before_replay", &ldlm_rw_uint_fops,
95 &ldlm_cancel_unused_locks_before_replay },
97 LASSERT(ldlm_ns_proc_dir == NULL);
99 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
102 if (IS_ERR(ldlm_type_proc_dir)) {
103 CERROR("LProcFS failed in ldlm-init\n");
104 rc = PTR_ERR(ldlm_type_proc_dir);
108 ldlm_ns_proc_dir = lprocfs_register("namespaces",
111 if (IS_ERR(ldlm_ns_proc_dir)) {
112 CERROR("LProcFS failed in ldlm-init\n");
113 rc = PTR_ERR(ldlm_ns_proc_dir);
117 ldlm_svc_proc_dir = lprocfs_register("services",
120 if (IS_ERR(ldlm_svc_proc_dir)) {
121 CERROR("LProcFS failed in ldlm-init\n");
122 rc = PTR_ERR(ldlm_svc_proc_dir);
126 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
131 lprocfs_remove(&ldlm_ns_proc_dir);
133 lprocfs_remove(&ldlm_type_proc_dir);
135 ldlm_svc_proc_dir = NULL;
136 ldlm_type_proc_dir = NULL;
137 ldlm_ns_proc_dir = NULL;
141 void ldlm_proc_cleanup(void)
143 if (ldlm_svc_proc_dir)
144 lprocfs_remove(&ldlm_svc_proc_dir);
146 if (ldlm_ns_proc_dir)
147 lprocfs_remove(&ldlm_ns_proc_dir);
149 if (ldlm_type_proc_dir)
150 lprocfs_remove(&ldlm_type_proc_dir);
152 ldlm_svc_proc_dir = NULL;
153 ldlm_type_proc_dir = NULL;
154 ldlm_ns_proc_dir = NULL;
157 static int lprocfs_ns_resources_seq_show(struct seq_file *m, void *v)
159 struct ldlm_namespace *ns = m->private;
161 struct cfs_hash_bd bd;
164 /* result is not strictly consistent */
165 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
166 res += cfs_hash_bd_count_get(&bd);
167 return lprocfs_rd_u64(m, &res);
169 LPROC_SEQ_FOPS_RO(lprocfs_ns_resources);
171 static int lprocfs_ns_locks_seq_show(struct seq_file *m, void *v)
173 struct ldlm_namespace *ns = m->private;
176 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
177 LPROCFS_FIELDS_FLAGS_SUM);
178 return lprocfs_rd_u64(m, &locks);
180 LPROC_SEQ_FOPS_RO(lprocfs_ns_locks);
182 static int lprocfs_lru_size_seq_show(struct seq_file *m, void *v)
184 struct ldlm_namespace *ns = m->private;
185 __u32 *nr = &ns->ns_max_unused;
187 if (ns_connect_lru_resize(ns))
188 nr = &ns->ns_nr_unused;
189 return lprocfs_rd_uint(m, nr);
192 static ssize_t lprocfs_lru_size_seq_write(struct file *file,
193 const char __user *buffer,
194 size_t count, loff_t *off)
196 struct ldlm_namespace *ns = ((struct seq_file *)file->private_data)->private;
197 char dummy[MAX_STRING_SIZE + 1];
202 dummy[MAX_STRING_SIZE] = '\0';
203 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
206 if (strncmp(dummy, "clear", 5) == 0) {
208 "dropping all unused locks from namespace %s\n",
210 if (ns_connect_lru_resize(ns)) {
211 int canceled, unused = ns->ns_nr_unused;
213 /* Try to cancel all @ns_nr_unused locks. */
214 canceled = ldlm_cancel_lru(ns, unused, 0,
216 if (canceled < unused) {
218 "not all requested locks are canceled, "
219 "requested: %d, canceled: %d\n", unused,
224 tmp = ns->ns_max_unused;
225 ns->ns_max_unused = 0;
226 ldlm_cancel_lru(ns, 0, 0, LDLM_CANCEL_PASSED);
227 ns->ns_max_unused = tmp;
232 err = kstrtoul(dummy, 10, &tmp);
234 CERROR("invalid value written\n");
237 lru_resize = (tmp == 0);
239 if (ns_connect_lru_resize(ns)) {
241 ns->ns_max_unused = (unsigned int)tmp;
243 if (tmp > ns->ns_nr_unused)
244 tmp = ns->ns_nr_unused;
245 tmp = ns->ns_nr_unused - tmp;
248 "changing namespace %s unused locks from %u to %u\n",
249 ldlm_ns_name(ns), ns->ns_nr_unused,
251 ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_CANCEL_PASSED);
255 "disable lru_resize for namespace %s\n",
257 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
261 "changing namespace %s max_unused from %u to %u\n",
262 ldlm_ns_name(ns), ns->ns_max_unused,
264 ns->ns_max_unused = (unsigned int)tmp;
265 ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_CANCEL_PASSED);
267 /* Make sure that LRU resize was originally supported before
268 * turning it on here. */
270 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
272 "enable lru_resize for namespace %s\n",
274 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
280 LPROC_SEQ_FOPS(lprocfs_lru_size);
282 static int lprocfs_elc_seq_show(struct seq_file *m, void *v)
284 struct ldlm_namespace *ns = m->private;
285 unsigned int supp = ns_connect_cancelset(ns);
287 return lprocfs_rd_uint(m, &supp);
290 static ssize_t lprocfs_elc_seq_write(struct file *file, const char *buffer,
291 size_t count, loff_t *off)
293 struct ldlm_namespace *ns = ((struct seq_file *)file->private_data)->private;
294 unsigned int supp = -1;
297 rc = lprocfs_wr_uint(file, buffer, count, &supp);
302 ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
303 else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
304 ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
307 LPROC_SEQ_FOPS(lprocfs_elc);
309 void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
311 if (ns->ns_proc_dir_entry == NULL)
312 CERROR("dlm namespace %s has no procfs dir?\n",
315 lprocfs_remove(&ns->ns_proc_dir_entry);
317 if (ns->ns_stats != NULL)
318 lprocfs_free_stats(&ns->ns_stats);
321 #define LDLM_NS_ADD_VAR(name, var, ops) \
323 snprintf(lock_name, MAX_STRING_SIZE, name); \
324 lock_vars[0].data = var; \
325 lock_vars[0].fops = ops; \
326 lprocfs_add_vars(ns_pde, lock_vars, NULL); \
329 int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
331 struct lprocfs_vars lock_vars[2];
332 char lock_name[MAX_STRING_SIZE + 1];
333 struct proc_dir_entry *ns_pde;
336 LASSERT(ns->ns_rs_hash != NULL);
338 if (ns->ns_proc_dir_entry != NULL) {
339 ns_pde = ns->ns_proc_dir_entry;
341 ns_pde = proc_mkdir(ldlm_ns_name(ns), ldlm_ns_proc_dir);
344 ns->ns_proc_dir_entry = ns_pde;
347 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
348 if (ns->ns_stats == NULL)
351 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
352 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
354 lock_name[MAX_STRING_SIZE] = '\0';
356 memset(lock_vars, 0, sizeof(lock_vars));
357 lock_vars[0].name = lock_name;
359 LDLM_NS_ADD_VAR("resource_count", ns, &lprocfs_ns_resources_fops);
360 LDLM_NS_ADD_VAR("lock_count", ns, &lprocfs_ns_locks_fops);
362 if (ns_is_client(ns)) {
363 LDLM_NS_ADD_VAR("lock_unused_count", &ns->ns_nr_unused,
365 LDLM_NS_ADD_VAR("lru_size", ns, &lprocfs_lru_size_fops);
366 LDLM_NS_ADD_VAR("lru_max_age", &ns->ns_max_age,
368 LDLM_NS_ADD_VAR("early_lock_cancel", ns, &lprocfs_elc_fops);
370 LDLM_NS_ADD_VAR("ctime_age_limit", &ns->ns_ctime_age_limit,
372 LDLM_NS_ADD_VAR("lock_timeouts", &ns->ns_timeouts,
374 LDLM_NS_ADD_VAR("max_nolock_bytes", &ns->ns_max_nolock_size,
376 LDLM_NS_ADD_VAR("contention_seconds", &ns->ns_contention_time,
378 LDLM_NS_ADD_VAR("contended_locks", &ns->ns_contended_locks,
380 LDLM_NS_ADD_VAR("max_parallel_ast", &ns->ns_max_parallel_ast,
385 #undef MAX_STRING_SIZE
386 #else /* CONFIG_PROC_FS */
388 #define ldlm_namespace_proc_unregister(ns) ({;})
389 #define ldlm_namespace_proc_register(ns) ({0;})
391 #endif /* CONFIG_PROC_FS */
393 static unsigned ldlm_res_hop_hash(struct cfs_hash *hs,
394 const void *key, unsigned mask)
396 const struct ldlm_res_id *id = key;
400 for (i = 0; i < RES_NAME_SIZE; i++)
405 static unsigned ldlm_res_hop_fid_hash(struct cfs_hash *hs,
406 const void *key, unsigned mask)
408 const struct ldlm_res_id *id = key;
413 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
414 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
415 fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
417 hash = fid_flatten32(&fid);
418 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
419 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
420 val = id->name[LUSTRE_RES_ID_HSH_OFF];
421 hash += (val >> 5) + (val << 11);
425 hash = hash_long(hash, hs->hs_bkt_bits);
426 /* give me another random factor */
427 hash -= hash_long((unsigned long)hs, val % 11 + 3);
429 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
430 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
435 static void *ldlm_res_hop_key(struct hlist_node *hnode)
437 struct ldlm_resource *res;
439 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
440 return &res->lr_name;
443 static int ldlm_res_hop_keycmp(const void *key, struct hlist_node *hnode)
445 struct ldlm_resource *res;
447 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
448 return ldlm_res_eq((const struct ldlm_res_id *)key,
449 (const struct ldlm_res_id *)&res->lr_name);
452 static void *ldlm_res_hop_object(struct hlist_node *hnode)
454 return hlist_entry(hnode, struct ldlm_resource, lr_hash);
457 static void ldlm_res_hop_get_locked(struct cfs_hash *hs, struct hlist_node *hnode)
459 struct ldlm_resource *res;
461 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
462 ldlm_resource_getref(res);
465 static void ldlm_res_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
467 struct ldlm_resource *res;
469 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
470 /* cfs_hash_for_each_nolock is the only chance we call it */
471 ldlm_resource_putref_locked(res);
474 static void ldlm_res_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
476 struct ldlm_resource *res;
478 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
479 ldlm_resource_putref(res);
482 cfs_hash_ops_t ldlm_ns_hash_ops = {
483 .hs_hash = ldlm_res_hop_hash,
484 .hs_key = ldlm_res_hop_key,
485 .hs_keycmp = ldlm_res_hop_keycmp,
487 .hs_object = ldlm_res_hop_object,
488 .hs_get = ldlm_res_hop_get_locked,
489 .hs_put_locked = ldlm_res_hop_put_locked,
490 .hs_put = ldlm_res_hop_put
493 cfs_hash_ops_t ldlm_ns_fid_hash_ops = {
494 .hs_hash = ldlm_res_hop_fid_hash,
495 .hs_key = ldlm_res_hop_key,
496 .hs_keycmp = ldlm_res_hop_keycmp,
498 .hs_object = ldlm_res_hop_object,
499 .hs_get = ldlm_res_hop_get_locked,
500 .hs_put_locked = ldlm_res_hop_put_locked,
501 .hs_put = ldlm_res_hop_put
504 struct ldlm_ns_hash_def {
505 ldlm_ns_type_t nsd_type;
506 /** hash bucket bits */
507 unsigned nsd_bkt_bits;
509 unsigned nsd_all_bits;
510 /** hash operations */
511 cfs_hash_ops_t *nsd_hops;
514 struct ldlm_ns_hash_def ldlm_ns_hash_defs[] = {
516 .nsd_type = LDLM_NS_TYPE_MDC,
519 .nsd_hops = &ldlm_ns_fid_hash_ops,
522 .nsd_type = LDLM_NS_TYPE_MDT,
525 .nsd_hops = &ldlm_ns_fid_hash_ops,
528 .nsd_type = LDLM_NS_TYPE_OSC,
531 .nsd_hops = &ldlm_ns_hash_ops,
534 .nsd_type = LDLM_NS_TYPE_OST,
537 .nsd_hops = &ldlm_ns_hash_ops,
540 .nsd_type = LDLM_NS_TYPE_MGC,
543 .nsd_hops = &ldlm_ns_hash_ops,
546 .nsd_type = LDLM_NS_TYPE_MGT,
549 .nsd_hops = &ldlm_ns_hash_ops,
552 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
557 * Create and initialize new empty namespace.
559 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
562 ldlm_ns_type_t ns_type)
564 struct ldlm_namespace *ns = NULL;
565 struct ldlm_ns_bucket *nsb;
566 struct ldlm_ns_hash_def *nsd;
567 struct cfs_hash_bd bd;
571 LASSERT(obd != NULL);
575 CERROR("ldlm_get_ref failed: %d\n", rc);
579 for (idx = 0;;idx++) {
580 nsd = &ldlm_ns_hash_defs[idx];
581 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
582 CERROR("Unknown type %d for ns %s\n", ns_type, name);
586 if (nsd->nsd_type == ns_type)
594 ns->ns_rs_hash = cfs_hash_create(name,
595 nsd->nsd_all_bits, nsd->nsd_all_bits,
596 nsd->nsd_bkt_bits, sizeof(*nsb),
602 CFS_HASH_SPIN_BKTLOCK |
603 CFS_HASH_NO_ITEMREF);
604 if (ns->ns_rs_hash == NULL)
607 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
608 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
609 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
610 nsb->nsb_namespace = ns;
614 ns->ns_appetite = apt;
615 ns->ns_client = client;
617 INIT_LIST_HEAD(&ns->ns_list_chain);
618 INIT_LIST_HEAD(&ns->ns_unused_list);
619 spin_lock_init(&ns->ns_lock);
620 atomic_set(&ns->ns_bref, 0);
621 init_waitqueue_head(&ns->ns_waitq);
623 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
624 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
625 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
627 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
628 ns->ns_nr_unused = 0;
629 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
630 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
631 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
633 ns->ns_orig_connect_flags = 0;
634 ns->ns_connect_flags = 0;
636 rc = ldlm_namespace_proc_register(ns);
638 CERROR("Can't initialize ns proc, rc %d\n", rc);
642 idx = ldlm_namespace_nr_read(client);
643 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
645 CERROR("Can't initialize lock pool, rc %d\n", rc);
649 ldlm_namespace_register(ns, client);
652 ldlm_namespace_proc_unregister(ns);
653 ldlm_namespace_cleanup(ns, 0);
655 cfs_hash_putref(ns->ns_rs_hash);
662 EXPORT_SYMBOL(ldlm_namespace_new);
664 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
667 * Cancel and destroy all locks on a resource.
669 * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
670 * clean up. This is currently only used for recovery, and we make
671 * certain assumptions as a result--notably, that we shouldn't cancel
674 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
677 struct list_head *tmp;
678 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
679 bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
682 struct ldlm_lock *lock = NULL;
684 /* First, we look for non-cleaned-yet lock
685 * all cleaned locks are marked by CLEANED flag. */
687 list_for_each(tmp, q) {
688 lock = list_entry(tmp, struct ldlm_lock,
690 if (lock->l_flags & LDLM_FL_CLEANED) {
695 lock->l_flags |= LDLM_FL_CLEANED;
704 /* Set CBPENDING so nothing in the cancellation path
705 * can match this lock. */
706 lock->l_flags |= LDLM_FL_CBPENDING;
707 lock->l_flags |= LDLM_FL_FAILED;
708 lock->l_flags |= flags;
710 /* ... without sending a CANCEL message for local_only. */
712 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
714 if (local_only && (lock->l_readers || lock->l_writers)) {
715 /* This is a little bit gross, but much better than the
716 * alternative: pretend that we got a blocking AST from
717 * the server, so that when the lock is decref'd, it
718 * will go away ... */
720 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
721 if (lock->l_completion_ast)
722 lock->l_completion_ast(lock, 0, NULL);
723 LDLM_LOCK_RELEASE(lock);
728 struct lustre_handle lockh;
731 ldlm_lock2handle(lock, &lockh);
732 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
734 CERROR("ldlm_cli_cancel: %d\n", rc);
736 ldlm_resource_unlink_lock(lock);
738 LDLM_DEBUG(lock, "Freeing a lock still held by a "
740 ldlm_lock_destroy(lock);
742 LDLM_LOCK_RELEASE(lock);
746 static int ldlm_resource_clean(struct cfs_hash *hs, struct cfs_hash_bd *bd,
747 struct hlist_node *hnode, void *arg)
749 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
750 __u64 flags = *(__u64 *)arg;
752 cleanup_resource(res, &res->lr_granted, flags);
753 cleanup_resource(res, &res->lr_converting, flags);
754 cleanup_resource(res, &res->lr_waiting, flags);
759 static int ldlm_resource_complain(struct cfs_hash *hs, struct cfs_hash_bd *bd,
760 struct hlist_node *hnode, void *arg)
762 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
765 CERROR("%s: namespace resource "DLDLMRES
766 " (%p) refcount nonzero (%d) after lock cleanup; forcing cleanup.\n",
767 ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
768 atomic_read(&res->lr_refcount) - 1);
770 ldlm_resource_dump(D_ERROR, res);
776 * Cancel and destroy all locks in the namespace.
778 * Typically used during evictions when server notified client that it was
779 * evicted and all of its state needs to be destroyed.
780 * Also used during shutdown.
782 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
785 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
789 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean, &flags);
790 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
793 EXPORT_SYMBOL(ldlm_namespace_cleanup);
796 * Attempts to free namespace.
798 * Only used when namespace goes away, like during an unmount.
800 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
802 /* At shutdown time, don't call the cancellation callback */
803 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
805 if (atomic_read(&ns->ns_bref) > 0) {
806 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
809 "dlm namespace %s free waiting on refcount %d\n",
810 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
813 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
815 rc = l_wait_event(ns->ns_waitq,
816 atomic_read(&ns->ns_bref) == 0, &lwi);
818 /* Forced cleanups should be able to reclaim all references,
819 * so it's safe to wait forever... we can't leak locks... */
820 if (force && rc == -ETIMEDOUT) {
821 LCONSOLE_ERROR("Forced cleanup waiting for %s "
822 "namespace with %d resources in use, "
823 "(rc=%d)\n", ldlm_ns_name(ns),
824 atomic_read(&ns->ns_bref), rc);
828 if (atomic_read(&ns->ns_bref)) {
829 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
830 "with %d resources in use, (rc=%d)\n",
832 atomic_read(&ns->ns_bref), rc);
833 return ELDLM_NAMESPACE_EXISTS;
835 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
843 * Performs various cleanups for passed \a ns to make it drop refc and be
844 * ready for freeing. Waits for refc == 0.
846 * The following is done:
847 * (0) Unregister \a ns from its list to make inaccessible for potential
848 * users like pools thread and others;
849 * (1) Clear all locks in \a ns.
851 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
852 struct obd_import *imp,
860 spin_lock(&ns->ns_lock);
862 spin_unlock(&ns->ns_lock);
865 * Can fail with -EINTR when force == 0 in which case try harder.
867 rc = __ldlm_namespace_free(ns, force);
868 if (rc != ELDLM_OK) {
870 ptlrpc_disconnect_import(imp, 0);
871 ptlrpc_invalidate_import(imp);
875 * With all requests dropped and the import inactive
876 * we are guaranteed all reference will be dropped.
878 rc = __ldlm_namespace_free(ns, 1);
884 * Performs freeing memory structures related to \a ns. This is only done
885 * when ldlm_namespce_free_prior() successfully removed all resources
886 * referencing \a ns and its refc == 0.
888 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
893 /* Make sure that nobody can find this ns in its list. */
894 ldlm_namespace_unregister(ns, ns->ns_client);
895 /* Fini pool _before_ parent proc dir is removed. This is important as
896 * ldlm_pool_fini() removes own proc dir which is child to @dir.
897 * Removing it after @dir may cause oops. */
898 ldlm_pool_fini(&ns->ns_pool);
900 ldlm_namespace_proc_unregister(ns);
901 cfs_hash_putref(ns->ns_rs_hash);
902 /* Namespace \a ns should be not on list at this time, otherwise
903 * this will cause issues related to using freed \a ns in poold
905 LASSERT(list_empty(&ns->ns_list_chain));
911 * Cleanup the resource, and free namespace.
914 * proc1: destroy import
915 * class_disconnect_export(grab cl_sem) ->
916 * -> ldlm_namespace_free ->
917 * -> lprocfs_remove(grab _lprocfs_lock).
918 * proc2: read proc info
919 * lprocfs_fops_read(grab _lprocfs_lock) ->
920 * -> osc_rd_active, etc(grab cl_sem).
922 * So that I have to split the ldlm_namespace_free into two parts - the first
923 * part ldlm_namespace_free_prior is used to cleanup the resource which is
924 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
925 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
928 void ldlm_namespace_free(struct ldlm_namespace *ns,
929 struct obd_import *imp,
932 ldlm_namespace_free_prior(ns, imp, force);
933 ldlm_namespace_free_post(ns);
935 EXPORT_SYMBOL(ldlm_namespace_free);
937 void ldlm_namespace_get(struct ldlm_namespace *ns)
939 atomic_inc(&ns->ns_bref);
941 EXPORT_SYMBOL(ldlm_namespace_get);
943 /* This is only for callers that care about refcount */
944 int ldlm_namespace_get_return(struct ldlm_namespace *ns)
946 return atomic_inc_return(&ns->ns_bref);
949 void ldlm_namespace_put(struct ldlm_namespace *ns)
951 if (atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
952 wake_up(&ns->ns_waitq);
953 spin_unlock(&ns->ns_lock);
956 EXPORT_SYMBOL(ldlm_namespace_put);
958 /** Register \a ns in the list of namespaces */
959 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
961 mutex_lock(ldlm_namespace_lock(client));
962 LASSERT(list_empty(&ns->ns_list_chain));
963 list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
964 ldlm_namespace_nr_inc(client);
965 mutex_unlock(ldlm_namespace_lock(client));
968 /** Unregister \a ns from the list of namespaces. */
969 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
971 mutex_lock(ldlm_namespace_lock(client));
972 LASSERT(!list_empty(&ns->ns_list_chain));
973 /* Some asserts and possibly other parts of the code are still
974 * using list_empty(&ns->ns_list_chain). This is why it is
975 * important to use list_del_init() here. */
976 list_del_init(&ns->ns_list_chain);
977 ldlm_namespace_nr_dec(client);
978 mutex_unlock(ldlm_namespace_lock(client));
981 /** Should be called with ldlm_namespace_lock(client) taken. */
982 void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
985 LASSERT(!list_empty(&ns->ns_list_chain));
986 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
987 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
990 /** Should be called with ldlm_namespace_lock(client) taken. */
991 void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
994 LASSERT(!list_empty(&ns->ns_list_chain));
995 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
996 list_move_tail(&ns->ns_list_chain,
997 ldlm_namespace_inactive_list(client));
1000 /** Should be called with ldlm_namespace_lock(client) taken. */
1001 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
1003 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1004 LASSERT(!list_empty(ldlm_namespace_list(client)));
1005 return container_of(ldlm_namespace_list(client)->next,
1006 struct ldlm_namespace, ns_list_chain);
1009 /** Create and initialize new resource. */
1010 static struct ldlm_resource *ldlm_resource_new(void)
1012 struct ldlm_resource *res;
1015 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, GFP_NOFS);
1019 INIT_LIST_HEAD(&res->lr_granted);
1020 INIT_LIST_HEAD(&res->lr_converting);
1021 INIT_LIST_HEAD(&res->lr_waiting);
1023 /* Initialize interval trees for each lock mode. */
1024 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1025 res->lr_itree[idx].lit_size = 0;
1026 res->lr_itree[idx].lit_mode = 1 << idx;
1027 res->lr_itree[idx].lit_root = NULL;
1030 atomic_set(&res->lr_refcount, 1);
1031 spin_lock_init(&res->lr_lock);
1032 lu_ref_init(&res->lr_reference);
1034 /* The creator of the resource must unlock the mutex after LVB
1035 * initialization. */
1036 mutex_init(&res->lr_lvb_mutex);
1037 mutex_lock(&res->lr_lvb_mutex);
1043 * Return a reference to resource with given name, creating it if necessary.
1044 * Args: namespace with ns_lock unlocked
1045 * Locks: takes and releases NS hash-lock and res->lr_lock
1046 * Returns: referenced, unlocked ldlm_resource or NULL
1048 struct ldlm_resource *
1049 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1050 const struct ldlm_res_id *name, ldlm_type_t type, int create)
1052 struct hlist_node *hnode;
1053 struct ldlm_resource *res;
1054 struct cfs_hash_bd bd;
1056 int ns_refcount = 0;
1058 LASSERT(ns != NULL);
1059 LASSERT(parent == NULL);
1060 LASSERT(ns->ns_rs_hash != NULL);
1061 LASSERT(name->name[0] != 0);
1063 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1064 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1065 if (hnode != NULL) {
1066 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1067 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1068 /* Synchronize with regard to resource creation. */
1069 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1070 mutex_lock(&res->lr_lvb_mutex);
1071 mutex_unlock(&res->lr_lvb_mutex);
1074 if (unlikely(res->lr_lvb_len < 0)) {
1075 ldlm_resource_putref(res);
1081 version = cfs_hash_bd_version_get(&bd);
1082 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1087 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1088 "type: %d\n", type);
1089 res = ldlm_resource_new();
1093 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1094 res->lr_name = *name;
1095 res->lr_type = type;
1096 res->lr_most_restr = LCK_NL;
1098 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1099 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1100 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1102 if (hnode != NULL) {
1103 /* Someone won the race and already added the resource. */
1104 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1105 /* Clean lu_ref for failed resource. */
1106 lu_ref_fini(&res->lr_reference);
1107 /* We have taken lr_lvb_mutex. Drop it. */
1108 mutex_unlock(&res->lr_lvb_mutex);
1109 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
1111 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1112 /* Synchronize with regard to resource creation. */
1113 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1114 mutex_lock(&res->lr_lvb_mutex);
1115 mutex_unlock(&res->lr_lvb_mutex);
1118 if (unlikely(res->lr_lvb_len < 0)) {
1119 ldlm_resource_putref(res);
1124 /* We won! Let's add the resource. */
1125 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1126 if (cfs_hash_bd_count_get(&bd) == 1)
1127 ns_refcount = ldlm_namespace_get_return(ns);
1129 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1130 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1133 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1134 rc = ns->ns_lvbo->lvbo_init(res);
1136 CERROR("%s: lvbo_init failed for resource %#llx:%#llx: rc = %d\n",
1137 ns->ns_obd->obd_name, name->name[0],
1139 if (res->lr_lvb_data) {
1140 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1141 res->lr_lvb_data = NULL;
1143 res->lr_lvb_len = rc;
1144 mutex_unlock(&res->lr_lvb_mutex);
1145 ldlm_resource_putref(res);
1150 /* We create resource with locked lr_lvb_mutex. */
1151 mutex_unlock(&res->lr_lvb_mutex);
1153 /* Let's see if we happened to be the very first resource in this
1154 * namespace. If so, and this is a client namespace, we need to move
1155 * the namespace into the active namespaces list to be patrolled by
1156 * the ldlm_poold. */
1157 if (ns_is_client(ns) && ns_refcount == 1) {
1158 mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1159 ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
1160 mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1165 EXPORT_SYMBOL(ldlm_resource_get);
1167 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1169 LASSERT(res != NULL);
1170 LASSERT(res != LP_POISON);
1171 atomic_inc(&res->lr_refcount);
1172 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1173 atomic_read(&res->lr_refcount));
1177 static void __ldlm_resource_putref_final(struct cfs_hash_bd *bd,
1178 struct ldlm_resource *res)
1180 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1182 if (!list_empty(&res->lr_granted)) {
1183 ldlm_resource_dump(D_ERROR, res);
1187 if (!list_empty(&res->lr_converting)) {
1188 ldlm_resource_dump(D_ERROR, res);
1192 if (!list_empty(&res->lr_waiting)) {
1193 ldlm_resource_dump(D_ERROR, res);
1197 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1199 lu_ref_fini(&res->lr_reference);
1200 if (cfs_hash_bd_count_get(bd) == 0)
1201 ldlm_namespace_put(nsb->nsb_namespace);
1204 /* Returns 1 if the resource was freed, 0 if it remains. */
1205 int ldlm_resource_putref(struct ldlm_resource *res)
1207 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1208 struct cfs_hash_bd bd;
1210 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1211 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1212 res, atomic_read(&res->lr_refcount) - 1);
1214 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1215 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1216 __ldlm_resource_putref_final(&bd, res);
1217 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1218 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1219 ns->ns_lvbo->lvbo_free(res);
1220 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
1225 EXPORT_SYMBOL(ldlm_resource_putref);
1227 /* Returns 1 if the resource was freed, 0 if it remains. */
1228 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1230 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1232 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1233 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1234 res, atomic_read(&res->lr_refcount) - 1);
1236 if (atomic_dec_and_test(&res->lr_refcount)) {
1237 struct cfs_hash_bd bd;
1239 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1240 &res->lr_name, &bd);
1241 __ldlm_resource_putref_final(&bd, res);
1242 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1243 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1244 * so we should never be here while calling cfs_hash_del,
1245 * cfs_hash_for_each_nolock is the only case we can get
1246 * here, which is safe to release cfs_hash_bd_lock.
1248 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1249 ns->ns_lvbo->lvbo_free(res);
1250 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof(*res));
1252 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1259 * Add a lock into a given resource into specified lock list.
1261 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1262 struct ldlm_lock *lock)
1264 check_res_locked(res);
1266 LDLM_DEBUG(lock, "About to add this lock:\n");
1268 if (lock->l_flags & LDLM_FL_DESTROYED) {
1269 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1273 LASSERT(list_empty(&lock->l_res_link));
1275 list_add_tail(&lock->l_res_link, head);
1279 * Insert a lock into resource after specified lock.
1281 * Obtain resource description from the lock we are inserting after.
1283 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1284 struct ldlm_lock *new)
1286 struct ldlm_resource *res = original->l_resource;
1288 check_res_locked(res);
1290 ldlm_resource_dump(D_INFO, res);
1291 LDLM_DEBUG(new, "About to insert this lock after %p:\n", original);
1293 if (new->l_flags & LDLM_FL_DESTROYED) {
1294 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1298 LASSERT(list_empty(&new->l_res_link));
1300 list_add(&new->l_res_link, &original->l_res_link);
1304 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1306 int type = lock->l_resource->lr_type;
1308 check_res_locked(lock->l_resource);
1309 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1310 ldlm_unlink_lock_skiplist(lock);
1311 else if (type == LDLM_EXTENT)
1312 ldlm_extent_unlink_lock(lock);
1313 list_del_init(&lock->l_res_link);
1315 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1317 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1319 desc->lr_type = res->lr_type;
1320 desc->lr_name = res->lr_name;
1324 * Print information about all locks in all namespaces on this node to debug
1327 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1329 struct list_head *tmp;
1331 if (!((libcfs_debug | D_ERROR) & level))
1334 mutex_lock(ldlm_namespace_lock(client));
1336 list_for_each(tmp, ldlm_namespace_list(client)) {
1337 struct ldlm_namespace *ns;
1338 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1339 ldlm_namespace_dump(level, ns);
1342 mutex_unlock(ldlm_namespace_lock(client));
1344 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1346 static int ldlm_res_hash_dump(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1347 struct hlist_node *hnode, void *arg)
1349 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1350 int level = (int)(unsigned long)arg;
1353 ldlm_resource_dump(level, res);
1360 * Print information about all locks in this namespace on this node to debug
1363 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1365 if (!((libcfs_debug | D_ERROR) & level))
1368 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1369 ldlm_ns_name(ns), atomic_read(&ns->ns_bref),
1370 ns_is_client(ns) ? "client" : "server");
1372 if (time_before(cfs_time_current(), ns->ns_next_dump))
1375 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1377 (void *)(unsigned long)level);
1378 spin_lock(&ns->ns_lock);
1379 ns->ns_next_dump = cfs_time_shift(10);
1380 spin_unlock(&ns->ns_lock);
1382 EXPORT_SYMBOL(ldlm_namespace_dump);
1385 * Print information about all locks in this resource to debug log.
1387 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1389 struct ldlm_lock *lock;
1390 unsigned int granted = 0;
1392 CLASSERT(RES_NAME_SIZE == 4);
1394 if (!((libcfs_debug | D_ERROR) & level))
1397 CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
1398 PLDLMRES(res), res, atomic_read(&res->lr_refcount));
1400 if (!list_empty(&res->lr_granted)) {
1401 CDEBUG(level, "Granted locks (in reverse order):\n");
1402 list_for_each_entry_reverse(lock, &res->lr_granted,
1404 LDLM_DEBUG_LIMIT(level, lock, "###");
1405 if (!(level & D_CANTMASK) &&
1406 ++granted > ldlm_dump_granted_max) {
1407 CDEBUG(level, "only dump %d granted locks to "
1408 "avoid DDOS.\n", granted);
1413 if (!list_empty(&res->lr_converting)) {
1414 CDEBUG(level, "Converting locks:\n");
1415 list_for_each_entry(lock, &res->lr_converting, l_res_link)
1416 LDLM_DEBUG_LIMIT(level, lock, "###");
1418 if (!list_empty(&res->lr_waiting)) {
1419 CDEBUG(level, "Waiting locks:\n");
1420 list_for_each_entry(lock, &res->lr_waiting, l_res_link)
1421 LDLM_DEBUG_LIMIT(level, lock, "###");