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.
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
44 # include <lustre_dlm.h>
46 # include <liblustre.h>
49 #include <lustre_fid.h>
50 #include <obd_class.h>
51 #include "ldlm_internal.h"
53 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
55 cfs_atomic_t ldlm_srv_namespace_nr = CFS_ATOMIC_INIT(0);
56 cfs_atomic_t ldlm_cli_namespace_nr = CFS_ATOMIC_INIT(0);
58 cfs_semaphore_t ldlm_srv_namespace_lock;
59 CFS_LIST_HEAD(ldlm_srv_namespace_list);
61 cfs_semaphore_t ldlm_cli_namespace_lock;
62 CFS_LIST_HEAD(ldlm_cli_namespace_list);
64 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
65 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
66 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
68 extern unsigned int ldlm_cancel_unused_locks_before_replay;
71 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
72 unsigned long count, void *data)
74 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
75 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
79 int ldlm_proc_setup(void)
82 struct lprocfs_vars list[] = {
83 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
84 { "cancel_unused_locks_before_replay",
85 lprocfs_rd_uint, lprocfs_wr_uint,
86 &ldlm_cancel_unused_locks_before_replay, NULL },
89 LASSERT(ldlm_ns_proc_dir == NULL);
91 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
94 if (IS_ERR(ldlm_type_proc_dir)) {
95 CERROR("LProcFS failed in ldlm-init\n");
96 rc = PTR_ERR(ldlm_type_proc_dir);
100 ldlm_ns_proc_dir = lprocfs_register("namespaces",
103 if (IS_ERR(ldlm_ns_proc_dir)) {
104 CERROR("LProcFS failed in ldlm-init\n");
105 rc = PTR_ERR(ldlm_ns_proc_dir);
109 ldlm_svc_proc_dir = lprocfs_register("services",
112 if (IS_ERR(ldlm_svc_proc_dir)) {
113 CERROR("LProcFS failed in ldlm-init\n");
114 rc = PTR_ERR(ldlm_svc_proc_dir);
118 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
123 lprocfs_remove(&ldlm_ns_proc_dir);
125 lprocfs_remove(&ldlm_type_proc_dir);
127 ldlm_svc_proc_dir = NULL;
131 void ldlm_proc_cleanup(void)
133 if (ldlm_svc_proc_dir)
134 lprocfs_remove(&ldlm_svc_proc_dir);
136 if (ldlm_ns_proc_dir)
137 lprocfs_remove(&ldlm_ns_proc_dir);
139 if (ldlm_type_proc_dir)
140 lprocfs_remove(&ldlm_type_proc_dir);
143 static int lprocfs_rd_ns_resources(char *page, char **start, off_t off,
144 int count, int *eof, void *data)
146 struct ldlm_namespace *ns = data;
151 /* result is not strictly consistant */
152 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, i)
153 res += cfs_hash_bd_count_get(&bd);
154 return lprocfs_rd_u64(page, start, off, count, eof, &res);
157 static int lprocfs_rd_ns_locks(char *page, char **start, off_t off,
158 int count, int *eof, void *data)
160 struct ldlm_namespace *ns = data;
163 locks = lprocfs_stats_collector(ns->ns_stats, LDLM_NSS_LOCKS,
164 LPROCFS_FIELDS_FLAGS_SUM);
165 return lprocfs_rd_u64(page, start, off, count, eof, &locks);
168 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
169 int count, int *eof, void *data)
171 struct ldlm_namespace *ns = data;
172 __u32 *nr = &ns->ns_max_unused;
174 if (ns_connect_lru_resize(ns))
175 nr = &ns->ns_nr_unused;
176 return lprocfs_rd_uint(page, start, off, count, eof, nr);
179 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
180 unsigned long count, void *data)
182 struct ldlm_namespace *ns = data;
183 char dummy[MAX_STRING_SIZE + 1], *end;
187 dummy[MAX_STRING_SIZE] = '\0';
188 if (cfs_copy_from_user(dummy, buffer, MAX_STRING_SIZE))
191 if (strncmp(dummy, "clear", 5) == 0) {
193 "dropping all unused locks from namespace %s\n",
195 if (ns_connect_lru_resize(ns)) {
196 int canceled, unused = ns->ns_nr_unused;
198 /* Try to cancel all @ns_nr_unused locks. */
199 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
201 if (canceled < unused) {
203 "not all requested locks are canceled, "
204 "requested: %d, canceled: %d\n", unused,
209 tmp = ns->ns_max_unused;
210 ns->ns_max_unused = 0;
211 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
212 ns->ns_max_unused = tmp;
217 tmp = simple_strtoul(dummy, &end, 0);
219 CERROR("invalid value written\n");
222 lru_resize = (tmp == 0);
224 if (ns_connect_lru_resize(ns)) {
226 ns->ns_max_unused = (unsigned int)tmp;
228 if (tmp > ns->ns_nr_unused)
229 tmp = ns->ns_nr_unused;
230 tmp = ns->ns_nr_unused - tmp;
233 "changing namespace %s unused locks from %u to %u\n",
234 ldlm_ns_name(ns), ns->ns_nr_unused,
236 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
240 "disable lru_resize for namespace %s\n",
242 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
246 "changing namespace %s max_unused from %u to %u\n",
247 ldlm_ns_name(ns), ns->ns_max_unused,
249 ns->ns_max_unused = (unsigned int)tmp;
250 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
252 /* Make sure that originally lru resize was supported before
253 * turning it on here. */
255 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
257 "enable lru_resize for namespace %s\n",
259 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
266 void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
268 struct proc_dir_entry *dir;
270 dir = lprocfs_srch(ldlm_ns_proc_dir, ldlm_ns_name(ns));
272 CERROR("dlm namespace %s has no procfs dir?\n",
275 lprocfs_remove(&dir);
278 if (ns->ns_stats != NULL)
279 lprocfs_free_stats(&ns->ns_stats);
282 int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
284 struct lprocfs_vars lock_vars[2];
285 char lock_name[MAX_STRING_SIZE + 1];
288 LASSERT(ns->ns_rs_hash != NULL);
290 ns->ns_stats = lprocfs_alloc_stats(LDLM_NSS_LAST, 0);
291 if (ns->ns_stats == NULL)
294 lprocfs_counter_init(ns->ns_stats, LDLM_NSS_LOCKS,
295 LPROCFS_CNTR_AVGMINMAX, "locks", "locks");
297 lock_name[MAX_STRING_SIZE] = '\0';
299 memset(lock_vars, 0, sizeof(lock_vars));
300 lock_vars[0].name = lock_name;
302 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count",
304 lock_vars[0].data = ns;
305 lock_vars[0].read_fptr = lprocfs_rd_ns_resources;
306 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
308 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count",
310 lock_vars[0].data = ns;
311 lock_vars[0].read_fptr = lprocfs_rd_ns_locks;
312 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
314 if (ns_is_client(ns)) {
315 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
317 lock_vars[0].data = &ns->ns_nr_unused;
318 lock_vars[0].read_fptr = lprocfs_rd_uint;
319 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
321 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
323 lock_vars[0].data = ns;
324 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
325 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
326 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
328 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
330 lock_vars[0].data = &ns->ns_max_age;
331 lock_vars[0].read_fptr = lprocfs_rd_uint;
332 lock_vars[0].write_fptr = lprocfs_wr_uint;
333 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
335 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
337 lock_vars[0].data = &ns->ns_ctime_age_limit;
338 lock_vars[0].read_fptr = lprocfs_rd_uint;
339 lock_vars[0].write_fptr = lprocfs_wr_uint;
340 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
342 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
344 lock_vars[0].data = &ns->ns_timeouts;
345 lock_vars[0].read_fptr = lprocfs_rd_uint;
346 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
348 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
350 lock_vars[0].data = &ns->ns_max_nolock_size;
351 lock_vars[0].read_fptr = lprocfs_rd_uint;
352 lock_vars[0].write_fptr = lprocfs_wr_uint;
353 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
355 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
357 lock_vars[0].data = &ns->ns_contention_time;
358 lock_vars[0].read_fptr = lprocfs_rd_uint;
359 lock_vars[0].write_fptr = lprocfs_wr_uint;
360 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
362 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
364 lock_vars[0].data = &ns->ns_contended_locks;
365 lock_vars[0].read_fptr = lprocfs_rd_uint;
366 lock_vars[0].write_fptr = lprocfs_wr_uint;
367 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
369 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_parallel_ast",
371 lock_vars[0].data = &ns->ns_max_parallel_ast;
372 lock_vars[0].read_fptr = lprocfs_rd_uint;
373 lock_vars[0].write_fptr = lprocfs_wr_uint;
374 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
378 #undef MAX_STRING_SIZE
381 #define ldlm_namespace_proc_unregister(ns) ({;})
382 #define ldlm_namespace_proc_register(ns) ({0;})
386 static unsigned ldlm_res_hop_hash(cfs_hash_t *hs,
387 const void *key, unsigned mask)
389 const struct ldlm_res_id *id = key;
393 for (i = 0; i < RES_NAME_SIZE; i++)
398 static unsigned ldlm_res_hop_fid_hash(cfs_hash_t *hs,
399 const void *key, unsigned mask)
401 const struct ldlm_res_id *id = key;
406 fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
407 fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_OID_OFF];
408 fid.f_ver = (__u32)id->name[LUSTRE_RES_ID_VER_OFF];
410 hash = fid_flatten32(&fid);
411 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
412 if (id->name[LUSTRE_RES_ID_HSH_OFF] != 0) {
413 val = id->name[LUSTRE_RES_ID_HSH_OFF];
414 hash += (val >> 5) + (val << 11);
418 hash = cfs_hash_long(hash, hs->hs_bkt_bits);
419 /* give me another random factor */
420 hash -= cfs_hash_long((unsigned long)hs, val % 11 + 3);
422 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
423 hash |= ldlm_res_hop_hash(hs, key, CFS_HASH_NBKT(hs) - 1);
428 static void *ldlm_res_hop_key(cfs_hlist_node_t *hnode)
430 struct ldlm_resource *res;
432 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
433 return &res->lr_name;
436 static int ldlm_res_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
438 struct ldlm_resource *res;
440 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
441 return ldlm_res_eq((const struct ldlm_res_id *)key,
442 (const struct ldlm_res_id *)&res->lr_name);
445 static void *ldlm_res_hop_object(cfs_hlist_node_t *hnode)
447 return cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
450 static void ldlm_res_hop_get_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
452 struct ldlm_resource *res;
454 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
455 ldlm_resource_getref(res);
456 LDLM_RESOURCE_ADDREF(res);
459 static void ldlm_res_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
461 struct ldlm_resource *res;
463 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
464 /* cfs_hash_for_each_nolock is the only chance we call it */
465 LDLM_RESOURCE_DELREF(res);
466 ldlm_resource_putref_locked(res);
469 static void ldlm_res_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
471 struct ldlm_resource *res;
473 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
474 LDLM_RESOURCE_DELREF(res);
475 ldlm_resource_putref(res);
478 cfs_hash_ops_t ldlm_ns_hash_ops = {
479 .hs_hash = ldlm_res_hop_hash,
480 .hs_key = ldlm_res_hop_key,
481 .hs_keycmp = ldlm_res_hop_keycmp,
483 .hs_object = ldlm_res_hop_object,
484 .hs_get = ldlm_res_hop_get_locked,
485 .hs_put_locked = ldlm_res_hop_put_locked,
486 .hs_put = ldlm_res_hop_put
489 cfs_hash_ops_t ldlm_ns_fid_hash_ops = {
490 .hs_hash = ldlm_res_hop_fid_hash,
491 .hs_key = ldlm_res_hop_key,
492 .hs_keycmp = ldlm_res_hop_keycmp,
494 .hs_object = ldlm_res_hop_object,
495 .hs_get = ldlm_res_hop_get_locked,
496 .hs_put_locked = ldlm_res_hop_put_locked,
497 .hs_put = ldlm_res_hop_put
501 ldlm_ns_type_t nsd_type;
502 /** hash bucket bits */
503 unsigned nsd_bkt_bits;
505 unsigned nsd_all_bits;
506 /** hash operations */
507 cfs_hash_ops_t *nsd_hops;
508 } ldlm_ns_hash_def_t;
510 ldlm_ns_hash_def_t ldlm_ns_hash_defs[] =
513 .nsd_type = LDLM_NS_TYPE_MDC,
516 .nsd_hops = &ldlm_ns_fid_hash_ops,
519 .nsd_type = LDLM_NS_TYPE_MDT,
522 .nsd_hops = &ldlm_ns_fid_hash_ops,
525 .nsd_type = LDLM_NS_TYPE_OSC,
528 .nsd_hops = &ldlm_ns_hash_ops,
531 .nsd_type = LDLM_NS_TYPE_OST,
534 .nsd_hops = &ldlm_ns_hash_ops,
537 .nsd_type = LDLM_NS_TYPE_MGC,
540 .nsd_hops = &ldlm_ns_hash_ops,
543 .nsd_type = LDLM_NS_TYPE_MGT,
546 .nsd_hops = &ldlm_ns_hash_ops,
549 .nsd_type = LDLM_NS_TYPE_UNKNOWN,
553 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
556 ldlm_ns_type_t ns_type)
558 struct ldlm_namespace *ns = NULL;
559 struct ldlm_ns_bucket *nsb;
560 ldlm_ns_hash_def_t *nsd;
566 LASSERT(obd != NULL);
570 CERROR("ldlm_get_ref failed: %d\n", rc);
574 for (idx = 0;;idx++) {
575 nsd = &ldlm_ns_hash_defs[idx];
576 if (nsd->nsd_type == LDLM_NS_TYPE_UNKNOWN) {
577 CERROR("Unknown type %d for ns %s\n", ns_type, name);
581 if (nsd->nsd_type == ns_type)
589 ns->ns_rs_hash = cfs_hash_create(name,
590 nsd->nsd_all_bits, nsd->nsd_all_bits,
591 nsd->nsd_bkt_bits, sizeof(*nsb),
597 CFS_HASH_SPIN_BKTLOCK |
598 CFS_HASH_NO_ITEMREF);
599 if (ns->ns_rs_hash == NULL)
602 cfs_hash_for_each_bucket(ns->ns_rs_hash, &bd, idx) {
603 nsb = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
604 at_init(&nsb->nsb_at_estimate, ldlm_enqueue_min, 0);
605 nsb->nsb_namespace = ns;
609 ns->ns_appetite = apt;
610 ns->ns_client = client;
612 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
613 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
614 cfs_spin_lock_init(&ns->ns_lock);
615 cfs_atomic_set(&ns->ns_bref, 0);
616 cfs_waitq_init(&ns->ns_waitq);
618 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
619 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
620 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
622 ns->ns_max_parallel_ast = LDLM_DEFAULT_PARALLEL_AST_LIMIT;
623 ns->ns_nr_unused = 0;
624 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
625 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
626 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
628 ns->ns_orig_connect_flags = 0;
629 ns->ns_connect_flags = 0;
631 rc = ldlm_namespace_proc_register(ns);
633 CERROR("Can't initialize ns proc, rc %d\n", rc);
637 idx = cfs_atomic_read(ldlm_namespace_nr(client));
638 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
640 CERROR("Can't initialize lock pool, rc %d\n", rc);
644 ldlm_namespace_register(ns, client);
647 ldlm_namespace_proc_unregister(ns);
648 ldlm_namespace_cleanup(ns, 0);
650 cfs_hash_putref(ns->ns_rs_hash);
658 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
660 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
661 * This is currently only used for recovery, and we make certain assumptions
662 * as a result--notably, that we shouldn't cancel locks with refs. -phil */
663 static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
667 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
668 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
671 struct ldlm_lock *lock = NULL;
673 /* first, we look for non-cleaned-yet lock
674 * all cleaned locks are marked by CLEANED flag */
676 cfs_list_for_each(tmp, q) {
677 lock = cfs_list_entry(tmp, struct ldlm_lock,
679 if (lock->l_flags & LDLM_FL_CLEANED) {
684 lock->l_flags |= LDLM_FL_CLEANED;
693 /* Set CBPENDING so nothing in the cancellation path
694 * can match this lock */
695 lock->l_flags |= LDLM_FL_CBPENDING;
696 lock->l_flags |= LDLM_FL_FAILED;
697 lock->l_flags |= flags;
699 /* ... without sending a CANCEL message for local_only. */
701 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
703 if (local_only && (lock->l_readers || lock->l_writers)) {
704 /* This is a little bit gross, but much better than the
705 * alternative: pretend that we got a blocking AST from
706 * the server, so that when the lock is decref'd, it
707 * will go away ... */
709 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
710 if (lock->l_completion_ast)
711 lock->l_completion_ast(lock, 0, NULL);
712 LDLM_LOCK_RELEASE(lock);
717 struct lustre_handle lockh;
720 ldlm_lock2handle(lock, &lockh);
721 rc = ldlm_cli_cancel(&lockh);
723 CERROR("ldlm_cli_cancel: %d\n", rc);
725 ldlm_resource_unlink_lock(lock);
727 LDLM_DEBUG(lock, "Freeing a lock still held by a "
729 ldlm_lock_destroy(lock);
731 LDLM_LOCK_RELEASE(lock);
735 static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
736 cfs_hlist_node_t *hnode, void *arg)
738 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
739 int flags = (int)(unsigned long)arg;
741 cleanup_resource(res, &res->lr_granted, flags);
742 cleanup_resource(res, &res->lr_converting, flags);
743 cleanup_resource(res, &res->lr_waiting, flags);
748 static int ldlm_resource_complain(cfs_hash_t *hs, cfs_hash_bd_t *bd,
749 cfs_hlist_node_t *hnode, void *arg)
751 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
753 CERROR("Namespace %s resource refcount nonzero "
754 "(%d) after lock cleanup; forcing "
756 ldlm_ns_name(ldlm_res_to_ns(res)),
757 cfs_atomic_read(&res->lr_refcount) - 1);
759 CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
760 LPU64") (rc: %d)\n", res,
761 res->lr_name.name[0], res->lr_name.name[1],
762 res->lr_name.name[2], res->lr_name.name[3],
763 cfs_atomic_read(&res->lr_refcount) - 1);
767 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
770 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
774 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean,
775 (void *)(unsigned long)flags);
776 cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
780 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
784 /* At shutdown time, don't call the cancellation callback */
785 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
787 if (cfs_atomic_read(&ns->ns_bref) > 0) {
788 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
791 "dlm namespace %s free waiting on refcount %d\n",
792 ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref));
795 lwi = LWI_TIMEOUT(obd_timeout * CFS_HZ / 4, NULL, NULL);
797 rc = l_wait_event(ns->ns_waitq,
798 cfs_atomic_read(&ns->ns_bref) == 0, &lwi);
800 /* Forced cleanups should be able to reclaim all references,
801 * so it's safe to wait forever... we can't leak locks... */
802 if (force && rc == -ETIMEDOUT) {
803 LCONSOLE_ERROR("Forced cleanup waiting for %s "
804 "namespace with %d resources in use, "
805 "(rc=%d)\n", ldlm_ns_name(ns),
806 cfs_atomic_read(&ns->ns_bref), rc);
807 GOTO(force_wait, rc);
810 if (cfs_atomic_read(&ns->ns_bref)) {
811 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
812 "with %d resources in use, (rc=%d)\n",
814 cfs_atomic_read(&ns->ns_bref), rc);
815 RETURN(ELDLM_NAMESPACE_EXISTS);
817 CDEBUG(D_DLMTRACE, "dlm namespace %s free done waiting\n",
825 * Performs various cleanups for passed \a ns to make it drop refc and be ready
826 * for freeing. Waits for refc == 0.
828 * The following is done:
829 * (0) Unregister \a ns from its list to make inaccessible for potential users
830 * like pools thread and others;
831 * (1) Clear all locks in \a ns.
833 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
834 struct obd_import *imp,
844 cfs_spin_lock(&ns->ns_lock);
846 cfs_spin_unlock(&ns->ns_lock);
849 * Can fail with -EINTR when force == 0 in which case try harder.
851 rc = __ldlm_namespace_free(ns, force);
852 if (rc != ELDLM_OK) {
854 ptlrpc_disconnect_import(imp, 0);
855 ptlrpc_invalidate_import(imp);
859 * With all requests dropped and the import inactive
860 * we are gaurenteed all reference will be dropped.
862 rc = __ldlm_namespace_free(ns, 1);
869 * Performs freeing memory structures related to \a ns. This is only done when
870 * ldlm_namespce_free_prior() successfully removed all resources referencing
871 * \a ns and its refc == 0.
873 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
883 * Make sure that nobody can find this ns in its list.
885 ldlm_namespace_unregister(ns, ns->ns_client);
887 * Fini pool _before_ parent proc dir is removed. This is important as
888 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
889 * it after @dir may cause oops.
891 ldlm_pool_fini(&ns->ns_pool);
893 ldlm_namespace_proc_unregister(ns);
894 cfs_hash_putref(ns->ns_rs_hash);
896 * Namespace \a ns should be not on list in this time, otherwise this
897 * will cause issues realted to using freed \a ns in pools thread.
899 LASSERT(cfs_list_empty(&ns->ns_list_chain));
906 /* Cleanup the resource, and free namespace.
909 * proc1: destroy import
910 * class_disconnect_export(grab cl_sem) ->
911 * -> ldlm_namespace_free ->
912 * -> lprocfs_remove(grab _lprocfs_lock).
913 * proc2: read proc info
914 * lprocfs_fops_read(grab _lprocfs_lock) ->
915 * -> osc_rd_active, etc(grab cl_sem).
917 * So that I have to split the ldlm_namespace_free into two parts - the first
918 * part ldlm_namespace_free_prior is used to cleanup the resource which is
919 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
920 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
923 void ldlm_namespace_free(struct ldlm_namespace *ns,
924 struct obd_import *imp,
927 ldlm_namespace_free_prior(ns, imp, force);
928 ldlm_namespace_free_post(ns);
931 void ldlm_namespace_get(struct ldlm_namespace *ns)
933 cfs_atomic_inc(&ns->ns_bref);
936 void ldlm_namespace_put(struct ldlm_namespace *ns)
938 if (cfs_atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
939 cfs_waitq_signal(&ns->ns_waitq);
940 cfs_spin_unlock(&ns->ns_lock);
944 /* Register @ns in the list of namespaces */
945 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
947 cfs_mutex_down(ldlm_namespace_lock(client));
948 LASSERT(cfs_list_empty(&ns->ns_list_chain));
949 cfs_list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
950 cfs_atomic_inc(ldlm_namespace_nr(client));
951 cfs_mutex_up(ldlm_namespace_lock(client));
954 /* Unregister @ns from the list of namespaces */
955 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
957 cfs_mutex_down(ldlm_namespace_lock(client));
958 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
960 * Some asserts and possibly other parts of code still using
961 * list_empty(&ns->ns_list_chain). This is why it is important
962 * to use list_del_init() here.
964 cfs_list_del_init(&ns->ns_list_chain);
965 cfs_atomic_dec(ldlm_namespace_nr(client));
966 cfs_mutex_up(ldlm_namespace_lock(client));
969 /* Should be called under ldlm_namespace_lock(client) taken */
970 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
972 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
973 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
974 cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
977 /* Should be called under ldlm_namespace_lock(client) taken */
978 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
980 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
981 LASSERT(!cfs_list_empty(ldlm_namespace_list(client)));
982 return container_of(ldlm_namespace_list(client)->next,
983 struct ldlm_namespace, ns_list_chain);
986 static struct ldlm_resource *ldlm_resource_new(void)
988 struct ldlm_resource *res;
991 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
995 CFS_INIT_LIST_HEAD(&res->lr_granted);
996 CFS_INIT_LIST_HEAD(&res->lr_converting);
997 CFS_INIT_LIST_HEAD(&res->lr_waiting);
999 /* initialize interval trees for each lock mode*/
1000 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1001 res->lr_itree[idx].lit_size = 0;
1002 res->lr_itree[idx].lit_mode = 1 << idx;
1003 res->lr_itree[idx].lit_root = NULL;
1006 cfs_atomic_set(&res->lr_refcount, 1);
1007 cfs_spin_lock_init(&res->lr_lock);
1008 lu_ref_init(&res->lr_reference);
1010 /* one who creates the resource must unlock
1011 * the semaphore after lvb initialization */
1012 cfs_init_mutex_locked(&res->lr_lvb_sem);
1017 /* Args: unlocked namespace
1018 * * Locks: takes and releases NS hash-lock and res->lr_lock
1019 * * Returns: referenced, unlocked ldlm_resource or NULL */
1020 struct ldlm_resource *
1021 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
1022 const struct ldlm_res_id *name, ldlm_type_t type, int create)
1024 cfs_hlist_node_t *hnode;
1025 struct ldlm_resource *res;
1029 LASSERT(ns != NULL);
1030 LASSERT(parent == NULL);
1031 LASSERT(ns->ns_rs_hash != NULL);
1032 LASSERT(name->name[0] != 0);
1034 cfs_hash_bd_get_and_lock(ns->ns_rs_hash, (void *)name, &bd, 0);
1035 hnode = cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1036 if (hnode != NULL) {
1037 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1038 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1039 /* synchronize WRT resource creation */
1040 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1041 cfs_down(&res->lr_lvb_sem);
1042 cfs_up(&res->lr_lvb_sem);
1047 version = cfs_hash_bd_version_get(&bd);
1048 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
1053 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
1054 "type: %d\n", type);
1055 res = ldlm_resource_new();
1059 res->lr_ns_bucket = cfs_hash_bd_extra_get(ns->ns_rs_hash, &bd);
1060 res->lr_name = *name;
1061 res->lr_type = type;
1062 res->lr_most_restr = LCK_NL;
1064 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1065 hnode = (version == cfs_hash_bd_version_get(&bd)) ? NULL :
1066 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
1068 if (hnode != NULL) {
1069 /* someone won the race and added the resource before */
1070 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1071 /* clean lu_ref for failed resource */
1072 lu_ref_fini(&res->lr_reference);
1073 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1075 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
1076 /* synchronize WRT resource creation */
1077 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1078 cfs_down(&res->lr_lvb_sem);
1079 cfs_up(&res->lr_lvb_sem);
1083 /* we won! let's add the resource */
1084 cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
1085 if (cfs_hash_bd_count_get(&bd) == 1)
1086 ldlm_namespace_get(ns);
1088 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1089 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
1092 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
1093 rc = ns->ns_lvbo->lvbo_init(res);
1095 CERROR("lvbo_init failed for resource "
1096 LPU64": rc %d\n", name->name[0], rc);
1097 /* we create resource with locked lr_lvb_sem */
1098 cfs_up(&res->lr_lvb_sem);
1104 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
1106 LASSERT(res != NULL);
1107 LASSERT(res != LP_POISON);
1108 cfs_atomic_inc(&res->lr_refcount);
1109 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
1110 cfs_atomic_read(&res->lr_refcount));
1114 static void __ldlm_resource_putref_final(cfs_hash_bd_t *bd,
1115 struct ldlm_resource *res)
1117 struct ldlm_ns_bucket *nsb = res->lr_ns_bucket;
1119 if (!cfs_list_empty(&res->lr_granted)) {
1120 ldlm_resource_dump(D_ERROR, res);
1124 if (!cfs_list_empty(&res->lr_converting)) {
1125 ldlm_resource_dump(D_ERROR, res);
1129 if (!cfs_list_empty(&res->lr_waiting)) {
1130 ldlm_resource_dump(D_ERROR, res);
1134 cfs_hash_bd_del_locked(nsb->nsb_namespace->ns_rs_hash,
1136 lu_ref_fini(&res->lr_reference);
1137 if (cfs_hash_bd_count_get(bd) == 0)
1138 ldlm_namespace_put(nsb->nsb_namespace);
1141 /* Returns 1 if the resource was freed, 0 if it remains. */
1142 int ldlm_resource_putref(struct ldlm_resource *res)
1144 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1147 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1148 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1149 res, cfs_atomic_read(&res->lr_refcount) - 1);
1151 cfs_hash_bd_get(ns->ns_rs_hash, &res->lr_name, &bd);
1152 if (cfs_hash_bd_dec_and_lock(ns->ns_rs_hash, &bd, &res->lr_refcount)) {
1153 __ldlm_resource_putref_final(&bd, res);
1154 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1155 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1156 ns->ns_lvbo->lvbo_free(res);
1157 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1163 /* Returns 1 if the resource was freed, 0 if it remains. */
1164 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1166 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1168 LASSERT_ATOMIC_GT_LT(&res->lr_refcount, 0, LI_POISON);
1169 CDEBUG(D_INFO, "putref res: %p count: %d\n",
1170 res, cfs_atomic_read(&res->lr_refcount) - 1);
1172 if (cfs_atomic_dec_and_test(&res->lr_refcount)) {
1175 cfs_hash_bd_get(ldlm_res_to_ns(res)->ns_rs_hash,
1176 &res->lr_name, &bd);
1177 __ldlm_resource_putref_final(&bd, res);
1178 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
1179 /* NB: ns_rs_hash is created with CFS_HASH_NO_ITEMREF,
1180 * so we should never be here while calling cfs_hash_del,
1181 * cfs_hash_for_each_nolock is the only case we can get
1182 * here, which is safe to release cfs_hash_bd_lock.
1184 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1185 ns->ns_lvbo->lvbo_free(res);
1186 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1188 cfs_hash_bd_lock(ns->ns_rs_hash, &bd, 1);
1194 void ldlm_resource_add_lock(struct ldlm_resource *res, cfs_list_t *head,
1195 struct ldlm_lock *lock)
1197 check_res_locked(res);
1199 CDEBUG(D_OTHER, "About to add this lock:\n");
1200 ldlm_lock_dump(D_OTHER, lock, 0);
1202 if (lock->l_destroyed) {
1203 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1207 LASSERT(cfs_list_empty(&lock->l_res_link));
1209 cfs_list_add_tail(&lock->l_res_link, head);
1212 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1213 struct ldlm_lock *new)
1215 struct ldlm_resource *res = original->l_resource;
1217 check_res_locked(res);
1219 ldlm_resource_dump(D_INFO, res);
1220 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1221 ldlm_lock_dump(D_OTHER, new, 0);
1223 if (new->l_destroyed) {
1224 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1228 LASSERT(cfs_list_empty(&new->l_res_link));
1230 cfs_list_add(&new->l_res_link, &original->l_res_link);
1234 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1236 int type = lock->l_resource->lr_type;
1238 check_res_locked(lock->l_resource);
1239 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1240 ldlm_unlink_lock_skiplist(lock);
1241 else if (type == LDLM_EXTENT)
1242 ldlm_extent_unlink_lock(lock);
1243 cfs_list_del_init(&lock->l_res_link);
1246 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1248 desc->lr_type = res->lr_type;
1249 desc->lr_name = res->lr_name;
1252 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1256 if (!((libcfs_debug | D_ERROR) & level))
1259 cfs_mutex_down(ldlm_namespace_lock(client));
1261 cfs_list_for_each(tmp, ldlm_namespace_list(client)) {
1262 struct ldlm_namespace *ns;
1263 ns = cfs_list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1264 ldlm_namespace_dump(level, ns);
1267 cfs_mutex_up(ldlm_namespace_lock(client));
1270 static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1271 cfs_hlist_node_t *hnode, void *arg)
1273 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1274 int level = (int)(unsigned long)arg;
1277 ldlm_resource_dump(level, res);
1283 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1285 if (!((libcfs_debug | D_ERROR) & level))
1288 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1289 ldlm_ns_name(ns), cfs_atomic_read(&ns->ns_bref),
1290 ns_is_client(ns) ? "client" : "server");
1292 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1295 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1297 (void *)(unsigned long)level);
1298 cfs_spin_lock(&ns->ns_lock);
1299 ns->ns_next_dump = cfs_time_shift(10);
1300 cfs_spin_unlock(&ns->ns_lock);
1303 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1308 CLASSERT(RES_NAME_SIZE == 4);
1310 if (!((libcfs_debug | D_ERROR) & level))
1313 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1314 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1315 res->lr_name.name[2], res->lr_name.name[3],
1316 cfs_atomic_read(&res->lr_refcount));
1318 if (!cfs_list_empty(&res->lr_granted)) {
1320 CDEBUG(level, "Granted locks:\n");
1321 cfs_list_for_each(tmp, &res->lr_granted) {
1322 struct ldlm_lock *lock;
1323 lock = cfs_list_entry(tmp, struct ldlm_lock,
1325 ldlm_lock_dump(level, lock, ++pos);
1328 if (!cfs_list_empty(&res->lr_converting)) {
1330 CDEBUG(level, "Converting locks:\n");
1331 cfs_list_for_each(tmp, &res->lr_converting) {
1332 struct ldlm_lock *lock;
1333 lock = cfs_list_entry(tmp, struct ldlm_lock,
1335 ldlm_lock_dump(level, lock, ++pos);
1338 if (!cfs_list_empty(&res->lr_waiting)) {
1340 CDEBUG(level, "Waiting locks:\n");
1341 cfs_list_for_each(tmp, &res->lr_waiting) {
1342 struct ldlm_lock *lock;
1343 lock = cfs_list_entry(tmp, struct ldlm_lock,
1345 ldlm_lock_dump(level, lock, ++pos);