1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ldlm/ldlm_resource.c
40 * Author: Phil Schwan <phil@clusterfs.com>
41 * Author: Peter Braam <braam@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_LDLM
46 # include <lustre_dlm.h>
48 # include <liblustre.h>
51 #include <lustre_fid.h>
52 #include <obd_class.h>
53 #include "ldlm_internal.h"
55 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
57 cfs_atomic_t ldlm_srv_namespace_nr = CFS_ATOMIC_INIT(0);
58 cfs_atomic_t ldlm_cli_namespace_nr = CFS_ATOMIC_INIT(0);
60 cfs_semaphore_t ldlm_srv_namespace_lock;
61 CFS_LIST_HEAD(ldlm_srv_namespace_list);
63 cfs_semaphore_t ldlm_cli_namespace_lock;
64 CFS_LIST_HEAD(ldlm_cli_namespace_list);
66 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
67 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
68 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
70 extern unsigned int ldlm_cancel_unused_locks_before_replay;
73 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
74 unsigned long count, void *data)
76 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
77 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
81 int ldlm_proc_setup(void)
84 struct lprocfs_vars list[] = {
85 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
86 { "cancel_unused_locks_before_replay",
87 lprocfs_rd_uint, lprocfs_wr_uint,
88 &ldlm_cancel_unused_locks_before_replay, NULL },
91 LASSERT(ldlm_ns_proc_dir == NULL);
93 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
96 if (IS_ERR(ldlm_type_proc_dir)) {
97 CERROR("LProcFS failed in ldlm-init\n");
98 rc = PTR_ERR(ldlm_type_proc_dir);
102 ldlm_ns_proc_dir = lprocfs_register("namespaces",
105 if (IS_ERR(ldlm_ns_proc_dir)) {
106 CERROR("LProcFS failed in ldlm-init\n");
107 rc = PTR_ERR(ldlm_ns_proc_dir);
111 ldlm_svc_proc_dir = lprocfs_register("services",
114 if (IS_ERR(ldlm_svc_proc_dir)) {
115 CERROR("LProcFS failed in ldlm-init\n");
116 rc = PTR_ERR(ldlm_svc_proc_dir);
120 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
125 lprocfs_remove(&ldlm_ns_proc_dir);
127 lprocfs_remove(&ldlm_type_proc_dir);
129 ldlm_svc_proc_dir = NULL;
133 void ldlm_proc_cleanup(void)
135 if (ldlm_svc_proc_dir)
136 lprocfs_remove(&ldlm_svc_proc_dir);
138 if (ldlm_ns_proc_dir)
139 lprocfs_remove(&ldlm_ns_proc_dir);
141 if (ldlm_type_proc_dir)
142 lprocfs_remove(&ldlm_type_proc_dir);
145 static int lprocfs_rd_ns_resources(char *page, char **start, off_t off,
146 int count, int *eof, void *data)
148 struct ldlm_namespace *ns = data;
153 /* result is not strictly consistant */
154 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
155 res += cfs_hash_bd_count_get(&bd);
156 return lprocfs_rd_u64(page, start, off, count, eof, &res);
159 static int lprocfs_rd_ns_locks(char *page, char **start, off_t off,
160 int count, int *eof, void *data)
162 struct ldlm_namespace *ns = data;
165 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
166 LPROCFS_FIELDS_FLAGS_SUM);
167 return lprocfs_rd_u64(page, start, off, count, eof, &locks);
170 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
171 int count, int *eof, void *data)
173 struct ldlm_namespace *ns = data;
174 __u32 *nr = &ns->ns_max_unused;
176 if (ns_connect_lru_resize(ns))
177 nr = &ns->ns_nr_unused;
178 return lprocfs_rd_uint(page, start, off, count, eof, nr);
181 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
182 unsigned long count, void *data)
184 struct ldlm_namespace *ns = data;
185 char dummy[MAX_STRING_SIZE + 1], *end;
189 dummy[MAX_STRING_SIZE] = '\0';
190 if (cfs_copy_from_user(dummy, buffer, MAX_STRING_SIZE))
193 if (strncmp(dummy, "clear", 5) == 0) {
195 "dropping all unused locks from namespace %s\n",
197 if (ns_connect_lru_resize(ns)) {
198 int canceled, unused = ns->ns_nr_unused;
200 /* Try to cancel all @ns_nr_unused locks. */
201 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
203 if (canceled < unused) {
205 "not all requested locks are canceled, "
206 "requested: %d, canceled: %d\n", unused,
211 tmp = ns->ns_max_unused;
212 ns->ns_max_unused = 0;
213 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
214 ns->ns_max_unused = tmp;
219 tmp = simple_strtoul(dummy, &end, 0);
221 CERROR("invalid value written\n");
224 lru_resize = (tmp == 0);
226 if (ns_connect_lru_resize(ns)) {
228 ns->ns_max_unused = (unsigned int)tmp;
230 if (tmp > ns->ns_nr_unused)
231 tmp = ns->ns_nr_unused;
232 tmp = ns->ns_nr_unused - tmp;
235 "changing namespace %s unused locks from %u to %u\n",
236 ldlm_ns_name(ns), ns->ns_nr_unused,
238 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
242 "disable lru_resize for namespace %s\n",
244 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
248 "changing namespace %s max_unused from %u to %u\n",
249 ldlm_ns_name(ns), ns->ns_max_unused,
251 ns->ns_max_unused = (unsigned int)tmp;
252 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
254 /* Make sure that originally lru resize was supported before
255 * turning it on here. */
257 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
259 "enable lru_resize for namespace %s\n",
261 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
268 void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
270 struct proc_dir_entry *dir;
272 dir = lprocfs_srch(ldlm_ns_proc_dir, ldlm_ns_name(ns));
274 CERROR("dlm namespace %s has no procfs dir?\n",
277 lprocfs_remove(&dir);
280 if (ns->ns_stats != NULL)
281 lprocfs_free_stats(&ns->ns_stats);
284 int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
286 struct lprocfs_vars lock_vars[2];
287 char lock_name[MAX_STRING_SIZE + 1];
290 LASSERT(ns->ns_rs_hash != NULL);
292 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
293 if (ns->ns_stats == NULL)
296 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
297 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
299 lock_name[MAX_STRING_SIZE] = '\0';
301 memset(lock_vars, 0, sizeof(lock_vars));
302 lock_vars[0].name = lock_name;
304 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count",
306 lock_vars[0].data = ns;
307 lock_vars[0].read_fptr = lprocfs_rd_ns_resources;
308 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
310 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count",
312 lock_vars[0].data = ns;
313 lock_vars[0].read_fptr = lprocfs_rd_ns_locks;
314 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
316 if (ns_is_client(ns)) {
317 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
319 lock_vars[0].data = &ns->ns_nr_unused;
320 lock_vars[0].read_fptr = lprocfs_rd_uint;
321 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
323 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
325 lock_vars[0].data = ns;
326 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
327 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
328 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
330 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
332 lock_vars[0].data = &ns->ns_max_age;
333 lock_vars[0].read_fptr = lprocfs_rd_uint;
334 lock_vars[0].write_fptr = lprocfs_wr_uint;
335 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
337 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
339 lock_vars[0].data = &ns->ns_ctime_age_limit;
340 lock_vars[0].read_fptr = lprocfs_rd_uint;
341 lock_vars[0].write_fptr = lprocfs_wr_uint;
342 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
344 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
346 lock_vars[0].data = &ns->ns_timeouts;
347 lock_vars[0].read_fptr = lprocfs_rd_uint;
348 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
350 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
352 lock_vars[0].data = &ns->ns_max_nolock_size;
353 lock_vars[0].read_fptr = lprocfs_rd_uint;
354 lock_vars[0].write_fptr = lprocfs_wr_uint;
355 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
357 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
359 lock_vars[0].data = &ns->ns_contention_time;
360 lock_vars[0].read_fptr = lprocfs_rd_uint;
361 lock_vars[0].write_fptr = lprocfs_wr_uint;
362 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
364 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
366 lock_vars[0].data = &ns->ns_contended_locks;
367 lock_vars[0].read_fptr = lprocfs_rd_uint;
368 lock_vars[0].write_fptr = lprocfs_wr_uint;
369 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
371 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_parallel_ast",
373 lock_vars[0].data = &ns->ns_max_parallel_ast;
374 lock_vars[0].read_fptr = lprocfs_rd_uint;
375 lock_vars[0].write_fptr = lprocfs_wr_uint;
376 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
380 #undef MAX_STRING_SIZE
383 #define ldlm_namespace_proc_unregister(ns) ({;})
384 #define ldlm_namespace_proc_register(ns) ({0;})
388 static unsigned ldlm_res_hop_hash(cfs_hash_t *hs,
389 const void *key, unsigned mask)
391 const struct ldlm_res_id *id = key;
395 for (i = 0; i < RES_NAME_SIZE; i++)
400 static unsigned ldlm_res_hop_fid_hash(cfs_hash_t *hs,
401 const void *key, unsigned mask)
403 const struct ldlm_res_id *id = key;
408 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
409 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
410 fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
412 hash = fid_flatten32(&fid);
413 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
414 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
415 val = id->name[LUSTRE_RES_ID_HSH_OFF];
416 hash += (val >> 5) + (val << 11);
420 hash = cfs_hash_long(hash, hs->hs_bkt_bits);
421 /* give me another random factor */
422 hash -= cfs_hash_long((unsigned long)hs, val % 11 + 3);
424 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
425 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
430 static void *ldlm_res_hop_key(cfs_hlist_node_t *hnode)
432 struct ldlm_resource *res;
434 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
435 return &res->lr_name;
438 static int ldlm_res_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
440 struct ldlm_resource *res;
442 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
443 return ldlm_res_eq((const struct ldlm_res_id *)key,
444 (const struct ldlm_res_id *)&res->lr_name);
447 static void *ldlm_res_hop_object(cfs_hlist_node_t *hnode)
449 return cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
452 static void ldlm_res_hop_get_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
454 struct ldlm_resource *res;
456 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
457 ldlm_resource_getref(res);
458 LDLM_RESOURCE_ADDREF(res);
461 static void ldlm_res_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
463 struct ldlm_resource *res;
465 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
466 /* cfs_hash_for_each_nolock is the only chance we call it */
467 LDLM_RESOURCE_DELREF(res);
468 ldlm_resource_putref_locked(res);
471 static void ldlm_res_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
473 struct ldlm_resource *res;
475 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
476 LDLM_RESOURCE_DELREF(res);
477 ldlm_resource_putref(res);
480 cfs_hash_ops_t ldlm_ns_hash_ops = {
481 .hs_hash = ldlm_res_hop_hash,
482 .hs_key = ldlm_res_hop_key,
483 .hs_keycmp = ldlm_res_hop_keycmp,
485 .hs_object = ldlm_res_hop_object,
486 .hs_get = ldlm_res_hop_get_locked,
487 .hs_put_locked = ldlm_res_hop_put_locked,
488 .hs_put = ldlm_res_hop_put
491 cfs_hash_ops_t ldlm_ns_fid_hash_ops = {
492 .hs_hash = ldlm_res_hop_fid_hash,
493 .hs_key = ldlm_res_hop_key,
494 .hs_keycmp = ldlm_res_hop_keycmp,
496 .hs_object = ldlm_res_hop_object,
497 .hs_get = ldlm_res_hop_get_locked,
498 .hs_put_locked = ldlm_res_hop_put_locked,
499 .hs_put = ldlm_res_hop_put
503 ldlm_ns_type_t nsd_type;
504 /** hash bucket bits */
505 unsigned nsd_bkt_bits;
507 unsigned nsd_all_bits;
508 /** hash operations */
509 cfs_hash_ops_t *nsd_hops;
510 } ldlm_ns_hash_def_t;
512 ldlm_ns_hash_def_t ldlm_ns_hash_defs[] =
515 .nsd_type = LDLM_NS_TYPE_MDC,
518 .nsd_hops = &ldlm_ns_fid_hash_ops,
521 .nsd_type = LDLM_NS_TYPE_MDT,
524 .nsd_hops = &ldlm_ns_fid_hash_ops,
527 .nsd_type = LDLM_NS_TYPE_OSC,
530 .nsd_hops = &ldlm_ns_hash_ops,
533 .nsd_type = LDLM_NS_TYPE_OST,
536 .nsd_hops = &ldlm_ns_hash_ops,
539 .nsd_type = LDLM_NS_TYPE_MGC,
542 .nsd_hops = &ldlm_ns_hash_ops,
545 .nsd_type = LDLM_NS_TYPE_MGT,
548 .nsd_hops = &ldlm_ns_hash_ops,
551 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
555 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
558 ldlm_ns_type_t ns_type)
560 struct ldlm_namespace *ns = NULL;
561 struct ldlm_ns_bucket *nsb;
562 ldlm_ns_hash_def_t *nsd;
568 LASSERT(obd != NULL);
572 CERROR("ldlm_get_ref failed: %d\n", rc);
576 for (idx = 0;;idx++) {
577 nsd = &ldlm_ns_hash_defs[idx];
578 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
579 CERROR("Unknown type %d for ns %s\n", ns_type, name);
583 if (nsd->nsd_type == ns_type)
591 ns->ns_rs_hash = cfs_hash_create(name,
592 nsd->nsd_all_bits, nsd->nsd_all_bits,
593 nsd->nsd_bkt_bits, sizeof(*nsb),
599 CFS_HASH_SPIN_BKTLOCK |
600 CFS_HASH_NO_ITEMREF);
601 if (ns->ns_rs_hash == NULL)
604 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
605 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
606 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
607 nsb->nsb_namespace = ns;
611 ns->ns_appetite = apt;
612 ns->ns_client = client;
614 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
615 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
616 cfs_spin_lock_init(&ns->ns_lock);
617 cfs_atomic_set(&ns->ns_bref, 0);
618 cfs_waitq_init(&ns->ns_waitq);
620 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
621 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
622 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
624 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
625 ns->ns_nr_unused = 0;
626 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
627 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
628 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
630 ns->ns_orig_connect_flags = 0;
631 ns->ns_connect_flags = 0;
633 rc = ldlm_namespace_proc_register(ns);
635 CERROR("Can't initialize ns proc, rc %d\n", rc);
639 idx = cfs_atomic_read(ldlm_namespace_nr(client));
640 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
642 CERROR("Can't initialize lock pool, rc %d\n", rc);
646 ldlm_namespace_register(ns, client);
649 ldlm_namespace_proc_unregister(ns);
650 ldlm_namespace_cleanup(ns, 0);
652 cfs_hash_putref(ns->ns_rs_hash);
660 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
662 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
663 * This is currently only used for recovery, and we make certain assumptions
664 * as a result--notably, that we shouldn't cancel locks with refs. -phil */
665 static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
669 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
670 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
673 struct ldlm_lock *lock = NULL;
675 /* first, we look for non-cleaned-yet lock
676 * all cleaned locks are marked by CLEANED flag */
678 cfs_list_for_each(tmp, q) {
679 lock = cfs_list_entry(tmp, struct ldlm_lock,
681 if (lock->l_flags & LDLM_FL_CLEANED) {
686 lock->l_flags |= LDLM_FL_CLEANED;
695 /* Set CBPENDING so nothing in the cancellation path
696 * can match this lock */
697 lock->l_flags |= LDLM_FL_CBPENDING;
698 lock->l_flags |= LDLM_FL_FAILED;
699 lock->l_flags |= flags;
701 /* ... without sending a CANCEL message for local_only. */
703 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
705 if (local_only && (lock->l_readers || lock->l_writers)) {
706 /* This is a little bit gross, but much better than the
707 * alternative: pretend that we got a blocking AST from
708 * the server, so that when the lock is decref'd, it
709 * will go away ... */
711 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
712 if (lock->l_completion_ast)
713 lock->l_completion_ast(lock, 0, NULL);
714 LDLM_LOCK_RELEASE(lock);
719 struct lustre_handle lockh;
722 ldlm_lock2handle(lock, &lockh);
723 rc = ldlm_cli_cancel(&lockh);
725 CERROR("ldlm_cli_cancel: %d\n", rc);
727 ldlm_resource_unlink_lock(lock);
729 LDLM_DEBUG(lock, "Freeing a lock still held by a "
731 ldlm_lock_destroy(lock);
733 LDLM_LOCK_RELEASE(lock);
737 static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
738 cfs_hlist_node_t *hnode, void *arg)
740 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
741 int flags = (int)(unsigned long)arg;
743 cleanup_resource(res, &res->lr_granted, flags);
744 cleanup_resource(res, &res->lr_converting, flags);
745 cleanup_resource(res, &res->lr_waiting, flags);
750 static int ldlm_resource_complain(cfs_hash_t *hs, cfs_hash_bd_t *bd,
751 cfs_hlist_node_t *hnode, void *arg)
753 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
755 CERROR("Namespace %s resource refcount nonzero "
756 "(%d) after lock cleanup; forcing "
758 ldlm_ns_name(ldlm_res_to_ns(res)),
759 cfs_atomic_read(&res->lr_refcount) - 1);
761 CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
762 LPU64") (rc: %d)\n", res,
763 res->lr_name.name[0], res->lr_name.name[1],
764 res->lr_name.name[2], res->lr_name.name[3],
765 cfs_atomic_read(&res->lr_refcount) - 1);
769 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
772 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
776 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean,
777 (void *)(unsigned long)flags);
778 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
782 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
786 /* At shutdown time, don't call the cancellation callback */
787 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
789 if (cfs_atomic_read(&ns->ns_bref) > 0) {
790 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
793 "dlm namespace %s free waiting on refcount %d\n",
794 ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref));
797 lwi = LWI_TIMEOUT(obd_timeout * CFS_HZ / 4, NULL, NULL);
799 rc = l_wait_event(ns->ns_waitq,
800 cfs_atomic_read(&ns->ns_bref) == 0, &lwi);
802 /* Forced cleanups should be able to reclaim all references,
803 * so it's safe to wait forever... we can't leak locks... */
804 if (force && rc == -ETIMEDOUT) {
805 LCONSOLE_ERROR("Forced cleanup waiting for %s "
806 "namespace with %d resources in use, "
807 "(rc=%d)\n", ldlm_ns_name(ns),
808 cfs_atomic_read(&ns->ns_bref), rc);
809 GOTO(force_wait, rc);
812 if (cfs_atomic_read(&ns->ns_bref)) {
813 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
814 "with %d resources in use, (rc=%d)\n",
816 cfs_atomic_read(&ns->ns_bref), rc);
817 RETURN(ELDLM_NAMESPACE_EXISTS);
819 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
827 * Performs various cleanups for passed \a ns to make it drop refc and be ready
828 * for freeing. Waits for refc == 0.
830 * The following is done:
831 * (0) Unregister \a ns from its list to make inaccessible for potential users
832 * like pools thread and others;
833 * (1) Clear all locks in \a ns.
835 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
836 struct obd_import *imp,
846 cfs_spin_lock(&ns->ns_lock);
848 cfs_spin_unlock(&ns->ns_lock);
851 * Can fail with -EINTR when force == 0 in which case try harder.
853 rc = __ldlm_namespace_free(ns, force);
854 if (rc != ELDLM_OK) {
856 ptlrpc_disconnect_import(imp, 0);
857 ptlrpc_invalidate_import(imp);
861 * With all requests dropped and the import inactive
862 * we are gaurenteed all reference will be dropped.
864 rc = __ldlm_namespace_free(ns, 1);
871 * Performs freeing memory structures related to \a ns. This is only done when
872 * ldlm_namespce_free_prior() successfully removed all resources referencing
873 * \a ns and its refc == 0.
875 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
885 * Make sure that nobody can find this ns in its list.
887 ldlm_namespace_unregister(ns, ns->ns_client);
889 * Fini pool _before_ parent proc dir is removed. This is important as
890 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
891 * it after @dir may cause oops.
893 ldlm_pool_fini(&ns->ns_pool);
895 ldlm_namespace_proc_unregister(ns);
896 cfs_hash_putref(ns->ns_rs_hash);
898 * Namespace \a ns should be not on list in this time, otherwise this
899 * will cause issues realted to using freed \a ns in pools thread.
901 LASSERT(cfs_list_empty(&ns->ns_list_chain));
908 /* Cleanup the resource, and free namespace.
911 * proc1: destroy import
912 * class_disconnect_export(grab cl_sem) ->
913 * -> ldlm_namespace_free ->
914 * -> lprocfs_remove(grab _lprocfs_lock).
915 * proc2: read proc info
916 * lprocfs_fops_read(grab _lprocfs_lock) ->
917 * -> osc_rd_active, etc(grab cl_sem).
919 * So that I have to split the ldlm_namespace_free into two parts - the first
920 * part ldlm_namespace_free_prior is used to cleanup the resource which is
921 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
922 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
925 void ldlm_namespace_free(struct ldlm_namespace *ns,
926 struct obd_import *imp,
929 ldlm_namespace_free_prior(ns, imp, force);
930 ldlm_namespace_free_post(ns);
933 void ldlm_namespace_get(struct ldlm_namespace *ns)
935 cfs_atomic_inc(&ns->ns_bref);
938 void ldlm_namespace_put(struct ldlm_namespace *ns)
940 if (cfs_atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
941 cfs_waitq_signal(&ns->ns_waitq);
942 cfs_spin_unlock(&ns->ns_lock);
946 /* Register @ns in the list of namespaces */
947 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
949 cfs_mutex_down(ldlm_namespace_lock(client));
950 LASSERT(cfs_list_empty(&ns->ns_list_chain));
951 cfs_list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
952 cfs_atomic_inc(ldlm_namespace_nr(client));
953 cfs_mutex_up(ldlm_namespace_lock(client));
956 /* Unregister @ns from the list of namespaces */
957 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
959 cfs_mutex_down(ldlm_namespace_lock(client));
960 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
962 * Some asserts and possibly other parts of code still using
963 * list_empty(&ns->ns_list_chain). This is why it is important
964 * to use list_del_init() here.
966 cfs_list_del_init(&ns->ns_list_chain);
967 cfs_atomic_dec(ldlm_namespace_nr(client));
968 cfs_mutex_up(ldlm_namespace_lock(client));
971 /* Should be called under ldlm_namespace_lock(client) taken */
972 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
974 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
975 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
976 cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
979 /* Should be called under ldlm_namespace_lock(client) taken */
980 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
982 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
983 LASSERT(!cfs_list_empty(ldlm_namespace_list(client)));
984 return container_of(ldlm_namespace_list(client)->next,
985 struct ldlm_namespace, ns_list_chain);
988 static struct ldlm_resource *ldlm_resource_new(void)
990 struct ldlm_resource *res;
993 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
997 CFS_INIT_LIST_HEAD(&res->lr_granted);
998 CFS_INIT_LIST_HEAD(&res->lr_converting);
999 CFS_INIT_LIST_HEAD(&res->lr_waiting);
1001 /* initialize interval trees for each lock mode*/
1002 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1003 res->lr_itree[idx].lit_size = 0;
1004 res->lr_itree[idx].lit_mode = 1 << idx;
1005 res->lr_itree[idx].lit_root = NULL;
1008 cfs_atomic_set(&res->lr_refcount, 1);
1009 cfs_spin_lock_init(&res->lr_lock);
1010 lu_ref_init(&res->lr_reference);
1012 /* one who creates the resource must unlock
1013 * the semaphore after lvb initialization */
1014 cfs_init_mutex_locked(&res->lr_lvb_sem);
1019 /* Args: unlocked namespace
1020 * * Locks: takes and releases NS hash-lock and res->lr_lock
1021 * * Returns: referenced, unlocked ldlm_resource or NULL */
1022 struct ldlm_resource *
1023 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1024 const struct ldlm_res_id *name, ldlm_type_t type, int create)
1026 cfs_hlist_node_t *hnode;
1027 struct ldlm_resource *res;
1031 LASSERT(ns != NULL);
1032 LASSERT(parent == NULL);
1033 LASSERT(ns->ns_rs_hash != NULL);
1034 LASSERT(name->name[0] != 0);
1036 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1037 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1038 if (hnode != NULL) {
1039 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1040 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1041 /* synchronize WRT resource creation */
1042 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1043 cfs_down(&res->lr_lvb_sem);
1044 cfs_up(&res->lr_lvb_sem);
1049 version = cfs_hash_bd_version_get(&bd);
1050 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1055 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1056 "type: %d\n", type);
1057 res = ldlm_resource_new();
1061 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1062 res->lr_name = *name;
1063 res->lr_type = type;
1064 res->lr_most_restr = LCK_NL;
1066 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1067 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1068 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1070 if (hnode != NULL) {
1071 /* someone won the race and added the resource before */
1072 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1073 /* clean lu_ref for failed resource */
1074 lu_ref_fini(&res->lr_reference);
1075 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1077 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1078 /* synchronize WRT resource creation */
1079 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1080 cfs_down(&res->lr_lvb_sem);
1081 cfs_up(&res->lr_lvb_sem);
1085 /* we won! let's add the resource */
1086 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1087 if (cfs_hash_bd_count_get(&bd) == 1)
1088 ldlm_namespace_get(ns);
1090 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1091 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1094 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1095 rc = ns->ns_lvbo->lvbo_init(res);
1097 CERROR("lvbo_init failed for resource "
1098 LPU64": rc %d\n", name->name[0], rc);
1099 /* we create resource with locked lr_lvb_sem */
1100 cfs_up(&res->lr_lvb_sem);
1106 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1108 LASSERT(res != NULL);
1109 LASSERT(res != LP_POISON);
1110 cfs_atomic_inc(&res->lr_refcount);
1111 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1112 cfs_atomic_read(&res->lr_refcount));
1116 static void __ldlm_resource_putref_final(cfs_hash_bd_t *bd,
1117 struct ldlm_resource *res)
1119 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1121 if (!cfs_list_empty(&res->lr_granted)) {
1122 ldlm_resource_dump(D_ERROR, res);
1126 if (!cfs_list_empty(&res->lr_converting)) {
1127 ldlm_resource_dump(D_ERROR, res);
1131 if (!cfs_list_empty(&res->lr_waiting)) {
1132 ldlm_resource_dump(D_ERROR, res);
1136 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1138 lu_ref_fini(&res->lr_reference);
1139 if (cfs_hash_bd_count_get(bd) == 0)
1140 ldlm_namespace_put(nsb->nsb_namespace);
1143 /* Returns 1 if the resource was freed, 0 if it remains. */
1144 int ldlm_resource_putref(struct ldlm_resource *res)
1146 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1149 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1150 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1151 res, cfs_atomic_read(&res->lr_refcount) - 1);
1153 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1154 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1155 __ldlm_resource_putref_final(&bd, res);
1156 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1157 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1158 ns->ns_lvbo->lvbo_free(res);
1159 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1165 /* Returns 1 if the resource was freed, 0 if it remains. */
1166 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1168 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1170 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1171 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1172 res, cfs_atomic_read(&res->lr_refcount) - 1);
1174 if (cfs_atomic_dec_and_test(&res->lr_refcount)) {
1177 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1178 &res->lr_name, &bd);
1179 __ldlm_resource_putref_final(&bd, res);
1180 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1181 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1182 * so we should never be here while calling cfs_hash_del,
1183 * cfs_hash_for_each_nolock is the only case we can get
1184 * here, which is safe to release cfs_hash_bd_lock.
1186 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1187 ns->ns_lvbo->lvbo_free(res);
1188 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1190 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1196 void ldlm_resource_add_lock(struct ldlm_resource *res, cfs_list_t *head,
1197 struct ldlm_lock *lock)
1199 check_res_locked(res);
1201 CDEBUG(D_OTHER, "About to add this lock:\n");
1202 ldlm_lock_dump(D_OTHER, lock, 0);
1204 if (lock->l_destroyed) {
1205 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1209 LASSERT(cfs_list_empty(&lock->l_res_link));
1211 cfs_list_add_tail(&lock->l_res_link, head);
1214 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1215 struct ldlm_lock *new)
1217 struct ldlm_resource *res = original->l_resource;
1219 check_res_locked(res);
1221 ldlm_resource_dump(D_INFO, res);
1222 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1223 ldlm_lock_dump(D_OTHER, new, 0);
1225 if (new->l_destroyed) {
1226 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1230 LASSERT(cfs_list_empty(&new->l_res_link));
1232 cfs_list_add(&new->l_res_link, &original->l_res_link);
1236 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1238 int type = lock->l_resource->lr_type;
1240 check_res_locked(lock->l_resource);
1241 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1242 ldlm_unlink_lock_skiplist(lock);
1243 else if (type == LDLM_EXTENT)
1244 ldlm_extent_unlink_lock(lock);
1245 cfs_list_del_init(&lock->l_res_link);
1248 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1250 desc->lr_type = res->lr_type;
1251 desc->lr_name = res->lr_name;
1254 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1258 if (!((libcfs_debug | D_ERROR) & level))
1261 cfs_mutex_down(ldlm_namespace_lock(client));
1263 cfs_list_for_each(tmp, ldlm_namespace_list(client)) {
1264 struct ldlm_namespace *ns;
1265 ns = cfs_list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1266 ldlm_namespace_dump(level, ns);
1269 cfs_mutex_up(ldlm_namespace_lock(client));
1272 static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1273 cfs_hlist_node_t *hnode, void *arg)
1275 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1276 int level = (int)(unsigned long)arg;
1279 ldlm_resource_dump(level, res);
1285 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1287 if (!((libcfs_debug | D_ERROR) & level))
1290 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1291 ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref),
1292 ns_is_client(ns) ? "client" : "server");
1294 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1297 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1299 (void *)(unsigned long)level);
1300 cfs_spin_lock(&ns->ns_lock);
1301 ns->ns_next_dump = cfs_time_shift(10);
1302 cfs_spin_unlock(&ns->ns_lock);
1305 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1310 CLASSERT(RES_NAME_SIZE == 4);
1312 if (!((libcfs_debug | D_ERROR) & level))
1315 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1316 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1317 res->lr_name.name[2], res->lr_name.name[3],
1318 cfs_atomic_read(&res->lr_refcount));
1320 if (!cfs_list_empty(&res->lr_granted)) {
1322 CDEBUG(level, "Granted locks:\n");
1323 cfs_list_for_each(tmp, &res->lr_granted) {
1324 struct ldlm_lock *lock;
1325 lock = cfs_list_entry(tmp, struct ldlm_lock,
1327 ldlm_lock_dump(level, lock, ++pos);
1330 if (!cfs_list_empty(&res->lr_converting)) {
1332 CDEBUG(level, "Converting locks:\n");
1333 cfs_list_for_each(tmp, &res->lr_converting) {
1334 struct ldlm_lock *lock;
1335 lock = cfs_list_entry(tmp, struct ldlm_lock,
1337 ldlm_lock_dump(level, lock, ++pos);
1340 if (!cfs_list_empty(&res->lr_waiting)) {
1342 CDEBUG(level, "Waiting locks:\n");
1343 cfs_list_for_each(tmp, &res->lr_waiting) {
1344 struct ldlm_lock *lock;
1345 lock = cfs_list_entry(tmp, struct ldlm_lock,
1347 ldlm_lock_dump(level, lock, ++pos);