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, 2014, 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 <lustre_dlm.h>
44 #include <lustre_fid.h>
45 #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 struct 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 struct list_head ldlm_cli_active_namespace_list;
61 /* Client namespaces that don't have any locks in them */
62 struct list_head ldlm_cli_inactive_namespace_list;
64 static struct proc_dir_entry *ldlm_type_proc_dir;
65 static struct proc_dir_entry *ldlm_ns_proc_dir;
66 struct proc_dir_entry *ldlm_svc_proc_dir;
68 /* during debug dump certain amount of granted locks for one resource to avoid
70 static unsigned int ldlm_dump_granted_max = 256;
74 lprocfs_dump_ns_seq_write(struct file *file, const char __user *buffer,
75 size_t count, loff_t *off)
77 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
78 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
81 LPROC_SEQ_FOPS_WO_TYPE(ldlm, dump_ns);
83 LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint);
84 LPROC_SEQ_FOPS_RO_TYPE(ldlm, uint);
86 int ldlm_proc_setup(void)
89 struct lprocfs_seq_vars list[] = {
90 { .name = "dump_namespaces",
91 .fops = &ldlm_dump_ns_fops,
93 { .name = "dump_granted_max",
94 .fops = &ldlm_rw_uint_fops,
95 .data = &ldlm_dump_granted_max },
96 { .name = "cancel_unused_locks_before_replay",
97 .fops = &ldlm_rw_uint_fops,
98 .data = &ldlm_cancel_unused_locks_before_replay },
101 LASSERT(ldlm_ns_proc_dir == NULL);
103 ldlm_type_proc_dir = lprocfs_seq_register(OBD_LDLM_DEVICENAME,
106 if (IS_ERR(ldlm_type_proc_dir)) {
107 CERROR("LProcFS failed in ldlm-init\n");
108 rc = PTR_ERR(ldlm_type_proc_dir);
112 ldlm_ns_proc_dir = lprocfs_seq_register("namespaces",
115 if (IS_ERR(ldlm_ns_proc_dir)) {
116 CERROR("LProcFS failed in ldlm-init\n");
117 rc = PTR_ERR(ldlm_ns_proc_dir);
121 ldlm_svc_proc_dir = lprocfs_seq_register("services",
124 if (IS_ERR(ldlm_svc_proc_dir)) {
125 CERROR("LProcFS failed in ldlm-init\n");
126 rc = PTR_ERR(ldlm_svc_proc_dir);
130 rc = lprocfs_seq_add_vars(ldlm_type_proc_dir, list, NULL);
132 CERROR("LProcFS failed in ldlm-init\n");
139 lprocfs_remove(&ldlm_svc_proc_dir);
141 lprocfs_remove(&ldlm_ns_proc_dir);
143 lprocfs_remove(&ldlm_type_proc_dir);
145 ldlm_svc_proc_dir = NULL;
149 void ldlm_proc_cleanup(void)
151 if (ldlm_svc_proc_dir)
152 lprocfs_remove(&ldlm_svc_proc_dir);
154 if (ldlm_ns_proc_dir)
155 lprocfs_remove(&ldlm_ns_proc_dir);
157 if (ldlm_type_proc_dir)
158 lprocfs_remove(&ldlm_type_proc_dir);
161 static int lprocfs_ns_resources_seq_show(struct seq_file *m, void *v)
163 struct ldlm_namespace *ns = m->private;
168 /* result is not strictly consistant */
169 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
170 res += cfs_hash_bd_count_get(&bd);
171 return lprocfs_u64_seq_show(m, &res);
173 LPROC_SEQ_FOPS_RO(lprocfs_ns_resources);
175 static int lprocfs_ns_locks_seq_show(struct seq_file *m, void *v)
177 struct ldlm_namespace *ns = m->private;
180 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
181 LPROCFS_FIELDS_FLAGS_SUM);
182 return lprocfs_u64_seq_show(m, &locks);
184 LPROC_SEQ_FOPS_RO(lprocfs_ns_locks);
186 static int lprocfs_lru_size_seq_show(struct seq_file *m, void *v)
188 struct ldlm_namespace *ns = m->private;
189 __u32 *nr = &ns->ns_max_unused;
191 if (ns_connect_lru_resize(ns))
192 nr = &ns->ns_nr_unused;
193 return lprocfs_uint_seq_show(m, nr);
196 static ssize_t lprocfs_lru_size_seq_write(struct file *file,
197 const char __user *buffer,
198 size_t count, loff_t *off)
200 struct ldlm_namespace *ns = ((struct seq_file *)file->private_data)->private;
201 char dummy[MAX_STRING_SIZE + 1], *end;
205 dummy[MAX_STRING_SIZE] = '\0';
206 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
209 if (strncmp(dummy, "clear", 5) == 0) {
211 "dropping all unused locks from namespace %s\n",
213 if (ns_connect_lru_resize(ns)) {
214 int canceled, unused = ns->ns_nr_unused;
216 /* Try to cancel all @ns_nr_unused locks. */
217 canceled = ldlm_cancel_lru(ns, unused, 0,
219 if (canceled < unused) {
221 "not all requested locks are canceled, "
222 "requested: %d, canceled: %d\n", unused,
227 tmp = ns->ns_max_unused;
228 ns->ns_max_unused = 0;
229 ldlm_cancel_lru(ns, 0, 0, LDLM_CANCEL_PASSED);
230 ns->ns_max_unused = tmp;
235 tmp = simple_strtoul(dummy, &end, 0);
237 CERROR("invalid value written\n");
240 lru_resize = (tmp == 0);
242 if (ns_connect_lru_resize(ns)) {
244 ns->ns_max_unused = (unsigned int)tmp;
246 if (tmp > ns->ns_nr_unused)
247 tmp = ns->ns_nr_unused;
248 tmp = ns->ns_nr_unused - tmp;
251 "changing namespace %s unused locks from %u to %u\n",
252 ldlm_ns_name(ns), ns->ns_nr_unused,
254 ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_CANCEL_PASSED);
258 "disable lru_resize for namespace %s\n",
260 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
264 "changing namespace %s max_unused from %u to %u\n",
265 ldlm_ns_name(ns), ns->ns_max_unused,
267 ns->ns_max_unused = (unsigned int)tmp;
268 ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_CANCEL_PASSED);
270 /* Make sure that LRU resize was originally supported before
271 * turning it on here. */
273 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
275 "enable lru_resize for namespace %s\n",
277 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
283 LPROC_SEQ_FOPS(lprocfs_lru_size);
285 static int lprocfs_elc_seq_show(struct seq_file *m, void *v)
287 struct ldlm_namespace *ns = m->private;
288 unsigned int supp = ns_connect_cancelset(ns);
290 return lprocfs_uint_seq_show(m, &supp);
293 static ssize_t lprocfs_elc_seq_write(struct file *file,
294 const char __user *buffer,
295 size_t count, loff_t *off)
297 struct ldlm_namespace *ns = ((struct seq_file *)file->private_data)->private;
298 unsigned int supp = -1;
301 rc = lprocfs_wr_uint(file, buffer, count, &supp);
306 ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
307 else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
308 ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
311 LPROC_SEQ_FOPS(lprocfs_elc);
313 static void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
315 if (ns->ns_proc_dir_entry == NULL)
316 CERROR("dlm namespace %s has no procfs dir?\n",
319 lprocfs_remove(&ns->ns_proc_dir_entry);
321 if (ns->ns_stats != NULL)
322 lprocfs_free_stats(&ns->ns_stats);
325 static int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
327 struct lprocfs_seq_vars lock_vars[2];
328 char lock_name[MAX_STRING_SIZE + 1];
329 struct proc_dir_entry *ns_pde;
332 LASSERT(ns->ns_rs_hash != NULL);
334 if (ns->ns_proc_dir_entry != NULL) {
335 ns_pde = ns->ns_proc_dir_entry;
337 ns_pde = proc_mkdir(ldlm_ns_name(ns), ldlm_ns_proc_dir);
340 ns->ns_proc_dir_entry = ns_pde;
343 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
344 if (ns->ns_stats == NULL)
347 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
348 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
350 lock_name[MAX_STRING_SIZE] = '\0';
352 memset(lock_vars, 0, sizeof(lock_vars));
353 lock_vars[0].name = lock_name;
355 ldlm_add_var(&lock_vars[0], ns_pde, "resource_count", ns,
356 &lprocfs_ns_resources_fops);
357 ldlm_add_var(&lock_vars[0], ns_pde, "lock_count", ns,
358 &lprocfs_ns_locks_fops);
360 if (ns_is_client(ns)) {
361 ldlm_add_var(&lock_vars[0], ns_pde, "lock_unused_count",
362 &ns->ns_nr_unused, &ldlm_uint_fops);
363 ldlm_add_var(&lock_vars[0], ns_pde, "lru_size", ns,
364 &lprocfs_lru_size_fops);
365 ldlm_add_var(&lock_vars[0], ns_pde, "lru_max_age",
366 &ns->ns_max_age, &ldlm_rw_uint_fops);
367 ldlm_add_var(&lock_vars[0], ns_pde, "early_lock_cancel",
368 ns, &lprocfs_elc_fops);
370 ldlm_add_var(&lock_vars[0], ns_pde, "ctime_age_limit",
371 &ns->ns_ctime_age_limit, &ldlm_rw_uint_fops);
372 ldlm_add_var(&lock_vars[0], ns_pde, "lock_timeouts",
373 &ns->ns_timeouts, &ldlm_uint_fops);
374 ldlm_add_var(&lock_vars[0], ns_pde, "max_nolock_bytes",
375 &ns->ns_max_nolock_size, &ldlm_rw_uint_fops);
376 ldlm_add_var(&lock_vars[0], ns_pde, "contention_seconds",
377 &ns->ns_contention_time, &ldlm_rw_uint_fops);
378 ldlm_add_var(&lock_vars[0], ns_pde, "contended_locks",
379 &ns->ns_contended_locks, &ldlm_rw_uint_fops);
380 ldlm_add_var(&lock_vars[0], ns_pde, "max_parallel_ast",
381 &ns->ns_max_parallel_ast, &ldlm_rw_uint_fops);
385 #undef MAX_STRING_SIZE
388 #define ldlm_namespace_proc_unregister(ns) ({;})
389 #define ldlm_namespace_proc_register(ns) ({0;})
393 static unsigned ldlm_res_hop_hash(cfs_hash_t *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(cfs_hash_t *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(cfs_hash_t *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(cfs_hash_t *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(cfs_hash_t *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 static 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 static 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
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;
512 } ldlm_ns_hash_def_t;
514 static ldlm_ns_hash_def_t ldlm_ns_hash_defs[] =
517 .nsd_type = LDLM_NS_TYPE_MDC,
520 .nsd_hops = &ldlm_ns_fid_hash_ops,
523 .nsd_type = LDLM_NS_TYPE_MDT,
526 .nsd_hops = &ldlm_ns_fid_hash_ops,
529 .nsd_type = LDLM_NS_TYPE_OSC,
532 .nsd_hops = &ldlm_ns_hash_ops,
535 .nsd_type = LDLM_NS_TYPE_OST,
538 .nsd_hops = &ldlm_ns_hash_ops,
541 .nsd_type = LDLM_NS_TYPE_MGC,
544 .nsd_hops = &ldlm_ns_hash_ops,
547 .nsd_type = LDLM_NS_TYPE_MGT,
550 .nsd_hops = &ldlm_ns_hash_ops,
553 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
558 * Create and initialize new empty namespace.
560 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
563 ldlm_ns_type_t ns_type)
565 struct ldlm_namespace *ns = NULL;
566 struct ldlm_ns_bucket *nsb;
567 ldlm_ns_hash_def_t *nsd;
573 LASSERT(obd != NULL);
577 CERROR("ldlm_get_ref failed: %d\n", rc);
581 for (idx = 0;;idx++) {
582 nsd = &ldlm_ns_hash_defs[idx];
583 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
584 CERROR("Unknown type %d for ns %s\n", ns_type, name);
588 if (nsd->nsd_type == ns_type)
596 ns->ns_rs_hash = cfs_hash_create(name,
597 nsd->nsd_all_bits, nsd->nsd_all_bits,
598 nsd->nsd_bkt_bits, sizeof(*nsb),
604 CFS_HASH_SPIN_BKTLOCK |
605 CFS_HASH_NO_ITEMREF);
606 if (ns->ns_rs_hash == NULL)
609 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
610 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
611 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
612 nsb->nsb_namespace = ns;
616 ns->ns_appetite = apt;
617 ns->ns_client = client;
619 INIT_LIST_HEAD(&ns->ns_list_chain);
620 INIT_LIST_HEAD(&ns->ns_unused_list);
621 spin_lock_init(&ns->ns_lock);
622 atomic_set(&ns->ns_bref, 0);
623 init_waitqueue_head(&ns->ns_waitq);
625 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
626 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
627 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
629 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
630 ns->ns_nr_unused = 0;
631 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
632 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
633 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
635 ns->ns_orig_connect_flags = 0;
636 ns->ns_connect_flags = 0;
638 rc = ldlm_namespace_proc_register(ns);
640 CERROR("Can't initialize ns proc, rc %d\n", rc);
644 idx = ldlm_namespace_nr_read(client);
645 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
647 CERROR("Can't initialize lock pool, rc %d\n", rc);
651 ldlm_namespace_register(ns, client);
654 ldlm_namespace_proc_unregister(ns);
655 ldlm_namespace_cleanup(ns, 0);
657 cfs_hash_putref(ns->ns_rs_hash);
664 EXPORT_SYMBOL(ldlm_namespace_new);
666 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
669 * Cancel and destroy all locks on a resource.
671 * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
672 * clean up. This is currently only used for recovery, and we make
673 * certain assumptions as a result--notably, that we shouldn't cancel
676 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
679 struct list_head *tmp;
680 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
681 bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
684 struct ldlm_lock *lock = NULL;
686 /* First, we look for non-cleaned-yet lock
687 * all cleaned locks are marked by CLEANED flag. */
689 list_for_each(tmp, q) {
690 lock = list_entry(tmp, struct ldlm_lock,
692 if (ldlm_is_cleaned(lock)) {
697 ldlm_set_cleaned(lock);
706 /* Set CBPENDING so nothing in the cancellation path
707 * can match this lock. */
708 ldlm_set_cbpending(lock);
709 ldlm_set_failed(lock);
710 lock->l_flags |= flags;
712 /* ... without sending a CANCEL message for local_only. */
714 ldlm_set_local_only(lock);
716 if (local_only && (lock->l_readers || lock->l_writers)) {
717 /* This is a little bit gross, but much better than the
718 * alternative: pretend that we got a blocking AST from
719 * the server, so that when the lock is decref'd, it
720 * will go away ... */
722 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
723 if (lock->l_flags & LDLM_FL_FAIL_LOC) {
724 schedule_timeout_and_set_state(
725 TASK_UNINTERRUPTIBLE,
726 cfs_time_seconds(4));
727 set_current_state(TASK_RUNNING);
729 if (lock->l_completion_ast)
730 lock->l_completion_ast(lock,
731 LDLM_FL_FAILED, NULL);
732 LDLM_LOCK_RELEASE(lock);
737 struct lustre_handle lockh;
740 ldlm_lock2handle(lock, &lockh);
741 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
743 CERROR("ldlm_cli_cancel: %d\n", rc);
745 ldlm_resource_unlink_lock(lock);
747 LDLM_DEBUG(lock, "Freeing a lock still held by a "
749 ldlm_lock_destroy(lock);
751 LDLM_LOCK_RELEASE(lock);
755 static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
756 struct hlist_node *hnode, void *arg)
758 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
759 __u64 flags = *(__u64 *)arg;
761 cleanup_resource(res, &res->lr_granted, flags);
762 cleanup_resource(res, &res->lr_converting, flags);
763 cleanup_resource(res, &res->lr_waiting, flags);
768 static int ldlm_resource_complain(cfs_hash_t *hs, cfs_hash_bd_t *bd,
769 struct hlist_node *hnode, void *arg)
771 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
774 CERROR("%s: namespace resource "DLDLMRES" (%p) refcount nonzero "
775 "(%d) after lock cleanup; forcing cleanup.\n",
776 ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
777 atomic_read(&res->lr_refcount) - 1);
779 ldlm_resource_dump(D_ERROR, res);
785 * Cancel and destroy all locks in the namespace.
787 * Typically used during evictions when server notified client that it was
788 * evicted and all of its state needs to be destroyed.
789 * Also used during shutdown.
791 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
794 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
798 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean, &flags);
799 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
802 EXPORT_SYMBOL(ldlm_namespace_cleanup);
805 * Attempts to free namespace.
807 * Only used when namespace goes away, like during an unmount.
809 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
813 /* At shutdown time, don't call the cancellation callback */
814 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
816 if (atomic_read(&ns->ns_bref) > 0) {
817 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
820 "dlm namespace %s free waiting on refcount %d\n",
821 ldlm_ns_name(ns), atomic_read(&ns->ns_bref));
824 lwi = LWI_TIMEOUT(msecs_to_jiffies(obd_timeout *
825 MSEC_PER_SEC) / 4, NULL, NULL);
827 rc = l_wait_event(ns->ns_waitq,
828 atomic_read(&ns->ns_bref) == 0, &lwi);
830 /* Forced cleanups should be able to reclaim all references,
831 * so it's safe to wait forever... we can't leak locks... */
832 if (force && rc == -ETIMEDOUT) {
833 LCONSOLE_ERROR("Forced cleanup waiting for %s "
834 "namespace with %d resources in use, "
835 "(rc=%d)\n", ldlm_ns_name(ns),
836 atomic_read(&ns->ns_bref), rc);
837 GOTO(force_wait, rc);
840 if (atomic_read(&ns->ns_bref)) {
841 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
842 "with %d resources in use, (rc=%d)\n",
844 atomic_read(&ns->ns_bref), rc);
845 RETURN(ELDLM_NAMESPACE_EXISTS);
847 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
855 * Performs various cleanups for passed \a ns to make it drop refc and be
856 * ready for freeing. Waits for refc == 0.
858 * The following is done:
859 * (0) Unregister \a ns from its list to make inaccessible for potential
860 * users like pools thread and others;
861 * (1) Clear all locks in \a ns.
863 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
864 struct obd_import *imp,
874 spin_lock(&ns->ns_lock);
876 spin_unlock(&ns->ns_lock);
879 * Can fail with -EINTR when force == 0 in which case try harder.
881 rc = __ldlm_namespace_free(ns, force);
882 if (rc != ELDLM_OK) {
884 ptlrpc_disconnect_import(imp, 0);
885 ptlrpc_invalidate_import(imp);
889 * With all requests dropped and the import inactive
890 * we are gaurenteed all reference will be dropped.
892 rc = __ldlm_namespace_free(ns, 1);
899 * Performs freeing memory structures related to \a ns. This is only done
900 * when ldlm_namespce_free_prior() successfully removed all resources
901 * referencing \a ns and its refc == 0.
903 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
911 /* Make sure that nobody can find this ns in its list. */
912 ldlm_namespace_unregister(ns, ns->ns_client);
913 /* Fini pool _before_ parent proc dir is removed. This is important as
914 * ldlm_pool_fini() removes own proc dir which is child to @dir.
915 * Removing it after @dir may cause oops. */
916 ldlm_pool_fini(&ns->ns_pool);
918 ldlm_namespace_proc_unregister(ns);
919 cfs_hash_putref(ns->ns_rs_hash);
920 /* Namespace \a ns should be not on list at this time, otherwise
921 * this will cause issues related to using freed \a ns in poold
923 LASSERT(list_empty(&ns->ns_list_chain));
930 * Cleanup the resource, and free namespace.
933 * proc1: destroy import
934 * class_disconnect_export(grab cl_sem) ->
935 * -> ldlm_namespace_free ->
936 * -> lprocfs_remove(grab _lprocfs_lock).
937 * proc2: read proc info
938 * lprocfs_fops_read(grab _lprocfs_lock) ->
939 * -> osc_rd_active, etc(grab cl_sem).
941 * So that I have to split the ldlm_namespace_free into two parts - the first
942 * part ldlm_namespace_free_prior is used to cleanup the resource which is
943 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
944 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
947 void ldlm_namespace_free(struct ldlm_namespace *ns,
948 struct obd_import *imp,
951 ldlm_namespace_free_prior(ns, imp, force);
952 ldlm_namespace_free_post(ns);
954 EXPORT_SYMBOL(ldlm_namespace_free);
956 void ldlm_namespace_get(struct ldlm_namespace *ns)
958 atomic_inc(&ns->ns_bref);
960 EXPORT_SYMBOL(ldlm_namespace_get);
962 /* This is only for callers that care about refcount */
963 static int ldlm_namespace_get_return(struct ldlm_namespace *ns)
965 return atomic_inc_return(&ns->ns_bref);
968 void ldlm_namespace_put(struct ldlm_namespace *ns)
970 if (atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
971 wake_up(&ns->ns_waitq);
972 spin_unlock(&ns->ns_lock);
975 EXPORT_SYMBOL(ldlm_namespace_put);
977 /** Register \a ns in the list of namespaces */
978 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
980 mutex_lock(ldlm_namespace_lock(client));
981 LASSERT(list_empty(&ns->ns_list_chain));
982 list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
983 ldlm_namespace_nr_inc(client);
984 mutex_unlock(ldlm_namespace_lock(client));
987 /** Unregister \a ns from the list of namespaces. */
988 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
990 mutex_lock(ldlm_namespace_lock(client));
991 LASSERT(!list_empty(&ns->ns_list_chain));
992 /* Some asserts and possibly other parts of the code are still
993 * using list_empty(&ns->ns_list_chain). This is why it is
994 * important to use list_del_init() here. */
995 list_del_init(&ns->ns_list_chain);
996 ldlm_namespace_nr_dec(client);
997 mutex_unlock(ldlm_namespace_lock(client));
1000 /** Should be called with ldlm_namespace_lock(client) taken. */
1001 void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
1004 LASSERT(!list_empty(&ns->ns_list_chain));
1005 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1006 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
1009 /** Should be called with ldlm_namespace_lock(client) taken. */
1010 void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
1013 LASSERT(!list_empty(&ns->ns_list_chain));
1014 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1015 list_move_tail(&ns->ns_list_chain,
1016 ldlm_namespace_inactive_list(client));
1019 /** Should be called with ldlm_namespace_lock(client) taken. */
1020 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
1022 LASSERT(mutex_is_locked(ldlm_namespace_lock(client)));
1023 LASSERT(!list_empty(ldlm_namespace_list(client)));
1024 return container_of(ldlm_namespace_list(client)->next,
1025 struct ldlm_namespace, ns_list_chain);
1028 /** Create and initialize new resource. */
1029 static struct ldlm_resource *ldlm_resource_new(void)
1031 struct ldlm_resource *res;
1034 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, GFP_NOFS);
1038 INIT_LIST_HEAD(&res->lr_granted);
1039 INIT_LIST_HEAD(&res->lr_converting);
1040 INIT_LIST_HEAD(&res->lr_waiting);
1042 /* Initialize interval trees for each lock mode. */
1043 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1044 res->lr_itree[idx].lit_size = 0;
1045 res->lr_itree[idx].lit_mode = 1 << idx;
1046 res->lr_itree[idx].lit_root = NULL;
1049 atomic_set(&res->lr_refcount, 1);
1050 spin_lock_init(&res->lr_lock);
1051 lu_ref_init(&res->lr_reference);
1053 /* Since LVB init can be delayed now, there is no longer need to
1054 * immediatelly acquire mutex here. */
1055 mutex_init(&res->lr_lvb_mutex);
1056 res->lr_lvb_initialized = false;
1062 * Return a reference to resource with given name, creating it if necessary.
1063 * Args: namespace with ns_lock unlocked
1064 * Locks: takes and releases NS hash-lock and res->lr_lock
1065 * Returns: referenced, unlocked ldlm_resource or NULL
1067 struct ldlm_resource *
1068 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1069 const struct ldlm_res_id *name, ldlm_type_t type, int create)
1071 struct hlist_node *hnode;
1072 struct ldlm_resource *res = NULL;
1075 int ns_refcount = 0;
1077 LASSERT(ns != NULL);
1078 LASSERT(parent == NULL);
1079 LASSERT(ns->ns_rs_hash != NULL);
1080 LASSERT(name->name[0] != 0);
1082 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1083 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1084 if (hnode != NULL) {
1085 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1089 version = cfs_hash_bd_version_get(&bd);
1090 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1093 return ERR_PTR(-ENOENT);
1095 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1096 "type: %d\n", type);
1097 res = ldlm_resource_new();
1099 return ERR_PTR(-ENOMEM);
1101 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1102 res->lr_name = *name;
1103 res->lr_type = type;
1104 res->lr_most_restr = LCK_NL;
1106 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1107 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1108 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1110 if (hnode != NULL) {
1111 /* Someone won the race and already added the resource. */
1112 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1113 /* Clean lu_ref for failed resource. */
1114 lu_ref_fini(&res->lr_reference);
1115 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1117 res = hlist_entry(hnode, struct ldlm_resource, lr_hash);
1120 /* We won! Let's add the resource. */
1121 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1122 if (cfs_hash_bd_count_get(&bd) == 1)
1123 ns_refcount = ldlm_namespace_get_return(ns);
1125 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1127 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1129 /* Let's see if we happened to be the very first resource in this
1130 * namespace. If so, and this is a client namespace, we need to move
1131 * the namespace into the active namespaces list to be patrolled by
1132 * the ldlm_poold. */
1133 if (ns_is_client(ns) && ns_refcount == 1) {
1134 mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1135 ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
1136 mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1141 EXPORT_SYMBOL(ldlm_resource_get);
1143 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1145 LASSERT(res != NULL);
1146 LASSERT(res != LP_POISON);
1147 atomic_inc(&res->lr_refcount);
1148 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1149 atomic_read(&res->lr_refcount));
1153 static void __ldlm_resource_putref_final(cfs_hash_bd_t *bd,
1154 struct ldlm_resource *res)
1156 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1158 if (!list_empty(&res->lr_granted)) {
1159 ldlm_resource_dump(D_ERROR, res);
1163 if (!list_empty(&res->lr_converting)) {
1164 ldlm_resource_dump(D_ERROR, res);
1168 if (!list_empty(&res->lr_waiting)) {
1169 ldlm_resource_dump(D_ERROR, res);
1173 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1175 lu_ref_fini(&res->lr_reference);
1176 if (cfs_hash_bd_count_get(bd) == 0)
1177 ldlm_namespace_put(nsb->nsb_namespace);
1180 /* Returns 1 if the resource was freed, 0 if it remains. */
1181 int ldlm_resource_putref(struct ldlm_resource *res)
1183 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1186 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1187 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1188 res, atomic_read(&res->lr_refcount) - 1);
1190 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1191 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1192 __ldlm_resource_putref_final(&bd, res);
1193 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1194 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1195 ns->ns_lvbo->lvbo_free(res);
1196 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1201 EXPORT_SYMBOL(ldlm_resource_putref);
1203 /* Returns 1 if the resource was freed, 0 if it remains. */
1204 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1206 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1208 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1209 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1210 res, atomic_read(&res->lr_refcount) - 1);
1212 if (atomic_dec_and_test(&res->lr_refcount)) {
1215 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1216 &res->lr_name, &bd);
1217 __ldlm_resource_putref_final(&bd, res);
1218 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1219 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1220 * so we should never be here while calling cfs_hash_del,
1221 * cfs_hash_for_each_nolock is the only case we can get
1222 * here, which is safe to release cfs_hash_bd_lock.
1224 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1225 ns->ns_lvbo->lvbo_free(res);
1226 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1228 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1235 * Add a lock into a given resource into specified lock list.
1237 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1238 struct ldlm_lock *lock)
1240 check_res_locked(res);
1242 LDLM_DEBUG(lock, "About to add this lock:\n");
1244 if (ldlm_is_destroyed(lock)) {
1245 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1249 LASSERT(list_empty(&lock->l_res_link));
1251 list_add_tail(&lock->l_res_link, head);
1255 * Insert a lock into resource after specified lock.
1257 * Obtain resource description from the lock we are inserting after.
1259 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1260 struct ldlm_lock *new)
1262 struct ldlm_resource *res = original->l_resource;
1264 check_res_locked(res);
1266 ldlm_resource_dump(D_INFO, res);
1267 LDLM_DEBUG(new, "About to insert this lock after %p:\n", original);
1269 if (ldlm_is_destroyed(new)) {
1270 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1274 LASSERT(list_empty(&new->l_res_link));
1276 list_add(&new->l_res_link, &original->l_res_link);
1280 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1282 int type = lock->l_resource->lr_type;
1284 check_res_locked(lock->l_resource);
1285 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1286 ldlm_unlink_lock_skiplist(lock);
1287 else if (type == LDLM_EXTENT)
1288 ldlm_extent_unlink_lock(lock);
1289 list_del_init(&lock->l_res_link);
1291 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
1293 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1295 desc->lr_type = res->lr_type;
1296 desc->lr_name = res->lr_name;
1300 * Print information about all locks in all namespaces on this node to debug
1303 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1305 struct list_head *tmp;
1307 if (!((libcfs_debug | D_ERROR) & level))
1310 mutex_lock(ldlm_namespace_lock(client));
1312 list_for_each(tmp, ldlm_namespace_list(client)) {
1313 struct ldlm_namespace *ns;
1314 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1315 ldlm_namespace_dump(level, ns);
1318 mutex_unlock(ldlm_namespace_lock(client));
1320 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1322 static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1323 struct hlist_node *hnode, void *arg)
1325 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1326 int level = (int)(unsigned long)arg;
1329 ldlm_resource_dump(level, res);
1336 * Print information about all locks in this namespace on this node to debug
1339 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1341 if (!((libcfs_debug | D_ERROR) & level))
1344 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1345 ldlm_ns_name(ns), atomic_read(&ns->ns_bref),
1346 ns_is_client(ns) ? "client" : "server");
1348 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1351 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1353 (void *)(unsigned long)level);
1354 spin_lock(&ns->ns_lock);
1355 ns->ns_next_dump = cfs_time_shift(10);
1356 spin_unlock(&ns->ns_lock);
1358 EXPORT_SYMBOL(ldlm_namespace_dump);
1361 * Print information about all locks in this resource to debug log.
1363 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1365 struct ldlm_lock *lock;
1366 unsigned int granted = 0;
1368 CLASSERT(RES_NAME_SIZE == 4);
1370 if (!((libcfs_debug | D_ERROR) & level))
1373 CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
1374 PLDLMRES(res), res, atomic_read(&res->lr_refcount));
1376 if (!list_empty(&res->lr_granted)) {
1377 CDEBUG(level, "Granted locks (in reverse order):\n");
1378 list_for_each_entry_reverse(lock, &res->lr_granted,
1380 LDLM_DEBUG_LIMIT(level, lock, "###");
1381 if (!(level & D_CANTMASK) &&
1382 ++granted > ldlm_dump_granted_max) {
1383 CDEBUG(level, "only dump %d granted locks to "
1384 "avoid DDOS.\n", granted);
1389 if (!list_empty(&res->lr_converting)) {
1390 CDEBUG(level, "Converting locks:\n");
1391 list_for_each_entry(lock, &res->lr_converting, l_res_link)
1392 LDLM_DEBUG_LIMIT(level, lock, "###");
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);