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 <obd_class.h>
50 #include "ldlm_internal.h"
52 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
54 cfs_atomic_t ldlm_srv_namespace_nr = CFS_ATOMIC_INIT(0);
55 cfs_atomic_t ldlm_cli_namespace_nr = CFS_ATOMIC_INIT(0);
57 cfs_semaphore_t ldlm_srv_namespace_lock;
58 CFS_LIST_HEAD(ldlm_srv_namespace_list);
60 cfs_semaphore_t ldlm_cli_namespace_lock;
61 CFS_LIST_HEAD(ldlm_cli_namespace_list);
63 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
64 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
65 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
67 extern unsigned int ldlm_cancel_unused_locks_before_replay;
70 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
71 unsigned long count, void *data)
73 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
74 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
78 int ldlm_proc_setup(void)
81 struct lprocfs_vars list[] = {
82 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
83 { "cancel_unused_locks_before_replay",
84 lprocfs_rd_uint, lprocfs_wr_uint,
85 &ldlm_cancel_unused_locks_before_replay, NULL },
88 LASSERT(ldlm_ns_proc_dir == NULL);
90 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
93 if (IS_ERR(ldlm_type_proc_dir)) {
94 CERROR("LProcFS failed in ldlm-init\n");
95 rc = PTR_ERR(ldlm_type_proc_dir);
99 ldlm_ns_proc_dir = lprocfs_register("namespaces",
102 if (IS_ERR(ldlm_ns_proc_dir)) {
103 CERROR("LProcFS failed in ldlm-init\n");
104 rc = PTR_ERR(ldlm_ns_proc_dir);
108 ldlm_svc_proc_dir = lprocfs_register("services",
111 if (IS_ERR(ldlm_svc_proc_dir)) {
112 CERROR("LProcFS failed in ldlm-init\n");
113 rc = PTR_ERR(ldlm_svc_proc_dir);
117 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
122 lprocfs_remove(&ldlm_ns_proc_dir);
124 lprocfs_remove(&ldlm_type_proc_dir);
126 ldlm_svc_proc_dir = NULL;
130 void ldlm_proc_cleanup(void)
132 if (ldlm_svc_proc_dir)
133 lprocfs_remove(&ldlm_svc_proc_dir);
135 if (ldlm_ns_proc_dir)
136 lprocfs_remove(&ldlm_ns_proc_dir);
138 if (ldlm_type_proc_dir)
139 lprocfs_remove(&ldlm_type_proc_dir);
142 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
143 int count, int *eof, void *data)
145 struct ldlm_namespace *ns = data;
146 __u32 *nr = &ns->ns_max_unused;
148 if (ns_connect_lru_resize(ns))
149 nr = &ns->ns_nr_unused;
150 return lprocfs_rd_uint(page, start, off, count, eof, nr);
153 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
154 unsigned long count, void *data)
156 struct ldlm_namespace *ns = data;
157 char dummy[MAX_STRING_SIZE + 1], *end;
161 dummy[MAX_STRING_SIZE] = '\0';
162 if (cfs_copy_from_user(dummy, buffer, MAX_STRING_SIZE))
165 if (strncmp(dummy, "clear", 5) == 0) {
167 "dropping all unused locks from namespace %s\n",
169 if (ns_connect_lru_resize(ns)) {
170 int canceled, unused = ns->ns_nr_unused;
172 /* Try to cancel all @ns_nr_unused locks. */
173 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
175 if (canceled < unused) {
177 "not all requested locks are canceled, "
178 "requested: %d, canceled: %d\n", unused,
183 tmp = ns->ns_max_unused;
184 ns->ns_max_unused = 0;
185 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
186 ns->ns_max_unused = tmp;
191 tmp = simple_strtoul(dummy, &end, 0);
193 CERROR("invalid value written\n");
196 lru_resize = (tmp == 0);
198 if (ns_connect_lru_resize(ns)) {
200 ns->ns_max_unused = (unsigned int)tmp;
202 if (tmp > ns->ns_nr_unused)
203 tmp = ns->ns_nr_unused;
204 tmp = ns->ns_nr_unused - tmp;
207 "changing namespace %s unused locks from %u to %u\n",
208 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
209 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
213 "disable lru_resize for namespace %s\n",
215 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
219 "changing namespace %s max_unused from %u to %u\n",
220 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
221 ns->ns_max_unused = (unsigned int)tmp;
222 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
224 /* Make sure that originally lru resize was supported before
225 * turning it on here. */
227 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
229 "enable lru_resize for namespace %s\n",
231 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
238 void ldlm_proc_namespace(struct ldlm_namespace *ns)
240 struct lprocfs_vars lock_vars[2];
241 char lock_name[MAX_STRING_SIZE + 1];
244 LASSERT(ns->ns_name != NULL);
246 lock_name[MAX_STRING_SIZE] = '\0';
248 memset(lock_vars, 0, sizeof(lock_vars));
249 lock_vars[0].name = lock_name;
251 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
252 lock_vars[0].data = &ns->ns_refcount;
253 lock_vars[0].read_fptr = lprocfs_rd_atomic;
254 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
256 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
257 lock_vars[0].data = &ns->ns_locks;
258 lock_vars[0].read_fptr = lprocfs_rd_atomic;
259 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
261 if (ns_is_client(ns)) {
262 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
264 lock_vars[0].data = &ns->ns_nr_unused;
265 lock_vars[0].read_fptr = lprocfs_rd_uint;
266 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
268 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
270 lock_vars[0].data = ns;
271 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
272 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
273 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
275 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
277 lock_vars[0].data = &ns->ns_max_age;
278 lock_vars[0].read_fptr = lprocfs_rd_uint;
279 lock_vars[0].write_fptr = lprocfs_wr_uint;
280 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
282 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
284 lock_vars[0].data = &ns->ns_ctime_age_limit;
285 lock_vars[0].read_fptr = lprocfs_rd_uint;
286 lock_vars[0].write_fptr = lprocfs_wr_uint;
287 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
289 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
291 lock_vars[0].data = &ns->ns_timeouts;
292 lock_vars[0].read_fptr = lprocfs_rd_uint;
293 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
295 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
297 lock_vars[0].data = &ns->ns_max_nolock_size;
298 lock_vars[0].read_fptr = lprocfs_rd_uint;
299 lock_vars[0].write_fptr = lprocfs_wr_uint;
300 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
302 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
304 lock_vars[0].data = &ns->ns_contention_time;
305 lock_vars[0].read_fptr = lprocfs_rd_uint;
306 lock_vars[0].write_fptr = lprocfs_wr_uint;
307 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
309 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
311 lock_vars[0].data = &ns->ns_contended_locks;
312 lock_vars[0].read_fptr = lprocfs_rd_uint;
313 lock_vars[0].write_fptr = lprocfs_wr_uint;
314 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
317 #undef MAX_STRING_SIZE
319 #define ldlm_proc_namespace(ns) do {} while (0)
322 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
323 ldlm_side_t client, ldlm_appetite_t apt)
325 struct ldlm_namespace *ns = NULL;
327 int rc, idx, namelen;
332 CERROR("ldlm_get_ref failed: %d\n", rc);
340 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
344 ns->ns_appetite = apt;
346 LASSERT(obd != NULL);
349 namelen = strlen(name);
350 OBD_ALLOC(ns->ns_name, namelen + 1);
352 GOTO(out_hash, NULL);
354 strcpy(ns->ns_name, name);
356 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
357 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
359 ns->ns_client = client;
360 cfs_spin_lock_init(&ns->ns_hash_lock);
361 cfs_atomic_set(&ns->ns_locks, 0);
362 ns->ns_resources = 0;
363 cfs_waitq_init(&ns->ns_waitq);
364 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
365 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
366 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
368 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
370 CFS_INIT_LIST_HEAD(bucket);
372 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
373 ns->ns_nr_unused = 0;
374 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
375 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
376 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
378 cfs_spin_lock_init(&ns->ns_unused_lock);
379 ns->ns_orig_connect_flags = 0;
380 ns->ns_connect_flags = 0;
381 ldlm_proc_namespace(ns);
383 idx = cfs_atomic_read(ldlm_namespace_nr(client));
384 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
386 CERROR("Can't initialize lock pool, rc %d\n", rc);
390 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
392 ldlm_namespace_register(ns, client);
395 ldlm_namespace_cleanup(ns, 0);
396 OBD_FREE(ns->ns_name, namelen + 1);
398 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
406 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
408 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
409 * This is currently only used for recovery, and we make certain assumptions
410 * as a result--notably, that we shouldn't cancel locks with refs. -phil
412 * Called with the ns_lock held. */
413 static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
417 int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
418 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
423 struct ldlm_lock *lock = NULL;
425 /* first, we look for non-cleaned-yet lock
426 * all cleaned locks are marked by CLEANED flag */
428 cfs_list_for_each(tmp, q) {
429 lock = cfs_list_entry(tmp, struct ldlm_lock,
431 if (lock->l_flags & LDLM_FL_CLEANED) {
436 lock->l_flags |= LDLM_FL_CLEANED;
445 /* Set CBPENDING so nothing in the cancellation path
446 * can match this lock */
447 lock->l_flags |= LDLM_FL_CBPENDING;
448 lock->l_flags |= LDLM_FL_FAILED;
449 lock->l_flags |= flags;
451 /* ... without sending a CANCEL message for local_only. */
453 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
455 if (local_only && (lock->l_readers || lock->l_writers)) {
456 /* This is a little bit gross, but much better than the
457 * alternative: pretend that we got a blocking AST from
458 * the server, so that when the lock is decref'd, it
459 * will go away ... */
461 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
462 if (lock->l_completion_ast)
463 lock->l_completion_ast(lock, 0, NULL);
464 LDLM_LOCK_RELEASE(lock);
469 struct lustre_handle lockh;
472 ldlm_lock2handle(lock, &lockh);
473 rc = ldlm_cli_cancel(&lockh);
475 CERROR("ldlm_cli_cancel: %d\n", rc);
477 ldlm_resource_unlink_lock(lock);
479 LDLM_DEBUG(lock, "Freeing a lock still held by a "
481 ldlm_lock_destroy(lock);
483 LDLM_LOCK_RELEASE(lock);
489 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
495 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
499 for (i = 0; i < RES_HASH_SIZE; i++) {
500 cfs_spin_lock(&ns->ns_hash_lock);
501 tmp = ns->ns_hash[i].next;
502 while (tmp != &(ns->ns_hash[i])) {
503 struct ldlm_resource *res;
504 res = cfs_list_entry(tmp, struct ldlm_resource,
506 ldlm_resource_getref(res);
507 cfs_spin_unlock(&ns->ns_hash_lock);
508 LDLM_RESOURCE_ADDREF(res);
510 cleanup_resource(res, &res->lr_granted, flags);
511 cleanup_resource(res, &res->lr_converting, flags);
512 cleanup_resource(res, &res->lr_waiting, flags);
514 cfs_spin_lock(&ns->ns_hash_lock);
517 /* XXX: former stuff caused issues in case of race
518 * between ldlm_namespace_cleanup() and lockd() when
519 * client gets blocking ast when lock gets distracted by
520 * server. This is 1_4 branch solution, let's see how
522 LDLM_RESOURCE_DELREF(res);
523 if (!ldlm_resource_putref_locked(res)) {
524 CERROR("Namespace %s resource refcount nonzero "
525 "(%d) after lock cleanup; forcing "
528 cfs_atomic_read(&res->lr_refcount));
529 CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
530 LPU64") (rc: %d)\n", res,
531 res->lr_name.name[0],
532 res->lr_name.name[1],
533 res->lr_name.name[2],
534 res->lr_name.name[3],
535 cfs_atomic_read(&res->lr_refcount));
538 cfs_spin_unlock(&ns->ns_hash_lock);
544 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
548 /* At shutdown time, don't call the cancellation callback */
549 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
551 if (ns->ns_refcount > 0) {
552 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
555 "dlm namespace %s free waiting on refcount %d\n",
556 ns->ns_name, ns->ns_refcount);
559 lwi = LWI_TIMEOUT(obd_timeout * CFS_HZ / 4, NULL, NULL);
561 rc = l_wait_event(ns->ns_waitq,
562 ns->ns_refcount == 0, &lwi);
564 /* Forced cleanups should be able to reclaim all references,
565 * so it's safe to wait forever... we can't leak locks... */
566 if (force && rc == -ETIMEDOUT) {
567 LCONSOLE_ERROR("Forced cleanup waiting for %s "
568 "namespace with %d resources in use, "
569 "(rc=%d)\n", ns->ns_name,
570 ns->ns_refcount, rc);
571 GOTO(force_wait, rc);
574 if (ns->ns_refcount) {
575 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
576 "with %d resources in use, (rc=%d)\n",
578 ns->ns_refcount, rc);
579 RETURN(ELDLM_NAMESPACE_EXISTS);
582 "dlm namespace %s free done waiting\n", ns->ns_name);
589 * Performs various cleanups for passed \a ns to make it drop refc and be ready
590 * for freeing. Waits for refc == 0.
592 * The following is done:
593 * (0) Unregister \a ns from its list to make inaccessible for potential users
594 * like pools thread and others;
595 * (1) Clear all locks in \a ns.
597 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
598 struct obd_import *imp,
610 * Can fail with -EINTR when force == 0 in which case try harder.
612 rc = __ldlm_namespace_free(ns, force);
613 if (rc != ELDLM_OK) {
615 ptlrpc_disconnect_import(imp, 0);
616 ptlrpc_invalidate_import(imp);
620 * With all requests dropped and the import inactive
621 * we are gaurenteed all reference will be dropped.
623 rc = __ldlm_namespace_free(ns, 1);
630 * Performs freeing memory structures related to \a ns. This is only done when
631 * ldlm_namespce_free_prior() successfully removed all resources referencing
632 * \a ns and its refc == 0.
634 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
644 * Make sure that nobody can find this ns in its list.
646 ldlm_namespace_unregister(ns, ns->ns_client);
648 * Fini pool _before_ parent proc dir is removed. This is important as
649 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
650 * it after @dir may cause oops.
652 ldlm_pool_fini(&ns->ns_pool);
656 struct proc_dir_entry *dir;
657 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
659 CERROR("dlm namespace %s has no procfs dir?\n",
662 lprocfs_remove(&dir);
667 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
668 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
671 * Namespace \a ns should be not on list in this time, otherwise this
672 * will cause issues realted to using freed \a ns in pools thread.
674 LASSERT(cfs_list_empty(&ns->ns_list_chain));
681 /* Cleanup the resource, and free namespace.
684 * proc1: destroy import
685 * class_disconnect_export(grab cl_sem) ->
686 * -> ldlm_namespace_free ->
687 * -> lprocfs_remove(grab _lprocfs_lock).
688 * proc2: read proc info
689 * lprocfs_fops_read(grab _lprocfs_lock) ->
690 * -> osc_rd_active, etc(grab cl_sem).
692 * So that I have to split the ldlm_namespace_free into two parts - the first
693 * part ldlm_namespace_free_prior is used to cleanup the resource which is
694 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
695 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
698 void ldlm_namespace_free(struct ldlm_namespace *ns,
699 struct obd_import *imp,
702 ldlm_namespace_free_prior(ns, imp, force);
703 ldlm_namespace_free_post(ns);
707 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
712 void ldlm_namespace_get(struct ldlm_namespace *ns)
714 cfs_spin_lock(&ns->ns_hash_lock);
715 ldlm_namespace_get_locked(ns);
716 cfs_spin_unlock(&ns->ns_hash_lock);
719 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
721 LASSERT(ns->ns_refcount > 0);
723 if (ns->ns_refcount == 0 && wakeup)
724 cfs_waitq_signal(&ns->ns_waitq);
727 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
729 cfs_spin_lock(&ns->ns_hash_lock);
730 ldlm_namespace_put_locked(ns, wakeup);
731 cfs_spin_unlock(&ns->ns_hash_lock);
734 /* Register @ns in the list of namespaces */
735 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
737 cfs_mutex_down(ldlm_namespace_lock(client));
738 LASSERT(cfs_list_empty(&ns->ns_list_chain));
739 cfs_list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
740 cfs_atomic_inc(ldlm_namespace_nr(client));
741 cfs_mutex_up(ldlm_namespace_lock(client));
744 /* Unregister @ns from the list of namespaces */
745 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
747 cfs_mutex_down(ldlm_namespace_lock(client));
748 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
750 * Some asserts and possibly other parts of code still using
751 * list_empty(&ns->ns_list_chain). This is why it is important
752 * to use list_del_init() here.
754 cfs_list_del_init(&ns->ns_list_chain);
755 cfs_atomic_dec(ldlm_namespace_nr(client));
756 cfs_mutex_up(ldlm_namespace_lock(client));
759 /* Should be called under ldlm_namespace_lock(client) taken */
760 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
762 LASSERT(!cfs_list_empty(&ns->ns_list_chain));
763 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
764 cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
767 /* Should be called under ldlm_namespace_lock(client) taken */
768 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
770 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
771 LASSERT(!cfs_list_empty(ldlm_namespace_list(client)));
772 return container_of(ldlm_namespace_list(client)->next,
773 struct ldlm_namespace, ns_list_chain);
775 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
776 const struct ldlm_res_id *name)
781 for (i = 0; i < RES_NAME_SIZE; i++)
782 hash += name->name[i];
784 hash += (__u32)((unsigned long)parent >> 4);
786 return (hash & RES_HASH_MASK);
789 static struct ldlm_resource *ldlm_resource_new(void)
791 struct ldlm_resource *res;
794 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
798 memset(res, 0, sizeof(*res));
800 CFS_INIT_LIST_HEAD(&res->lr_childof);
801 CFS_INIT_LIST_HEAD(&res->lr_granted);
802 CFS_INIT_LIST_HEAD(&res->lr_converting);
803 CFS_INIT_LIST_HEAD(&res->lr_waiting);
805 /* initialize interval trees for each lock mode*/
806 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
807 res->lr_itree[idx].lit_size = 0;
808 res->lr_itree[idx].lit_mode = 1 << idx;
809 res->lr_itree[idx].lit_root = NULL;
812 cfs_atomic_set(&res->lr_refcount, 1);
813 cfs_spin_lock_init(&res->lr_lock);
814 lu_ref_init(&res->lr_reference);
816 /* one who creates the resource must unlock
817 * the semaphore after lvb initialization */
818 cfs_init_mutex_locked(&res->lr_lvb_sem);
823 /* must be called with hash lock held */
824 static struct ldlm_resource *
825 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
828 cfs_list_t *bucket, *tmp;
829 struct ldlm_resource *res;
831 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
832 bucket = ns->ns_hash + hash;
834 cfs_list_for_each(tmp, bucket) {
835 res = cfs_list_entry(tmp, struct ldlm_resource, lr_hash);
836 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
843 /* Args: locked namespace
844 * Returns: newly-allocated, referenced, unlocked resource */
845 static struct ldlm_resource *
846 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
847 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
850 struct ldlm_resource *res, *old_res;
853 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
856 res = ldlm_resource_new();
860 res->lr_name = *name;
861 res->lr_namespace = ns;
863 res->lr_most_restr = LCK_NL;
865 cfs_spin_lock(&ns->ns_hash_lock);
866 old_res = ldlm_resource_find(ns, name, hash);
868 /* someone won the race and added the resource before */
869 ldlm_resource_getref(old_res);
870 cfs_spin_unlock(&ns->ns_hash_lock);
871 /* clean lu_ref for failed resource */
872 lu_ref_fini(&res->lr_reference);
873 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
874 /* synchronize WRT resource creation */
875 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
876 cfs_down(&old_res->lr_lvb_sem);
877 cfs_up(&old_res->lr_lvb_sem);
882 /* we won! let's add the resource */
883 bucket = ns->ns_hash + hash;
884 cfs_list_add(&res->lr_hash, bucket);
886 ldlm_namespace_get_locked(ns);
888 LASSERT(parent == NULL); /* legacy... */
889 cfs_list_add(&res->lr_childof, &ns->ns_root_list);
890 cfs_spin_unlock(&ns->ns_hash_lock);
892 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
895 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
896 rc = ns->ns_lvbo->lvbo_init(res);
898 CERROR("lvbo_init failed for resource "
899 LPU64": rc %d\n", name->name[0], rc);
900 /* we create resource with locked lr_lvb_sem */
901 cfs_up(&res->lr_lvb_sem);
907 /* Args: unlocked namespace
908 * Locks: takes and releases ns->ns_lock and res->lr_lock
909 * Returns: referenced, unlocked ldlm_resource or NULL */
910 struct ldlm_resource *
911 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
912 const struct ldlm_res_id *name, ldlm_type_t type, int create)
914 __u32 hash = ldlm_hash_fn(parent, name);
915 struct ldlm_resource *res = NULL;
919 LASSERT(ns->ns_hash != NULL);
920 LASSERT(name->name[0] != 0);
922 cfs_spin_lock(&ns->ns_hash_lock);
923 res = ldlm_resource_find(ns, name, hash);
925 ldlm_resource_getref(res);
926 cfs_spin_unlock(&ns->ns_hash_lock);
927 /* synchronize WRT resource creation */
928 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
929 cfs_down(&res->lr_lvb_sem);
930 cfs_up(&res->lr_lvb_sem);
934 cfs_spin_unlock(&ns->ns_hash_lock);
939 res = ldlm_resource_add(ns, parent, name, hash, type);
943 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
945 LASSERT(res != NULL);
946 LASSERT(res != LP_POISON);
947 cfs_atomic_inc(&res->lr_refcount);
948 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
949 cfs_atomic_read(&res->lr_refcount));
953 void __ldlm_resource_putref_final(struct ldlm_resource *res)
955 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
957 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
959 if (!cfs_list_empty(&res->lr_granted)) {
960 ldlm_resource_dump(D_ERROR, res);
964 if (!cfs_list_empty(&res->lr_converting)) {
965 ldlm_resource_dump(D_ERROR, res);
969 if (!cfs_list_empty(&res->lr_waiting)) {
970 ldlm_resource_dump(D_ERROR, res);
974 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
975 * lines below when all children are freed. */
976 ldlm_namespace_put_locked(ns, 0);
977 cfs_list_del_init(&res->lr_hash);
978 cfs_list_del_init(&res->lr_childof);
979 lu_ref_fini(&res->lr_reference);
982 if (ns->ns_resources == 0)
983 cfs_waitq_signal(&ns->ns_waitq);
986 int ldlm_resource_putref_internal(struct ldlm_resource *res, int locked)
988 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
991 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
992 cfs_atomic_read(&res->lr_refcount) - 1);
993 LASSERTF(cfs_atomic_read(&res->lr_refcount) > 0, "%d",
994 cfs_atomic_read(&res->lr_refcount));
995 LASSERTF(cfs_atomic_read(&res->lr_refcount) < LI_POISON, "%d",
996 cfs_atomic_read(&res->lr_refcount));
998 if (locked && !cfs_atomic_dec_and_test(&res->lr_refcount))
1000 if (!locked && !cfs_atomic_dec_and_lock(&res->lr_refcount,
1004 __ldlm_resource_putref_final(res);
1007 cfs_spin_unlock(&ns->ns_hash_lock);
1009 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_free)
1010 ns->ns_lvbo->lvbo_free(res);
1012 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1017 int ldlm_resource_putref(struct ldlm_resource *res)
1019 return ldlm_resource_putref_internal(res, 0);
1022 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1024 return ldlm_resource_putref_internal(res, 1);
1027 void ldlm_resource_add_lock(struct ldlm_resource *res, cfs_list_t *head,
1028 struct ldlm_lock *lock)
1030 check_res_locked(res);
1032 ldlm_resource_dump(D_INFO, res);
1033 CDEBUG(D_OTHER, "About to add this lock:\n");
1034 ldlm_lock_dump(D_OTHER, lock, 0);
1036 if (lock->l_destroyed) {
1037 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1041 LASSERT(cfs_list_empty(&lock->l_res_link));
1043 cfs_list_add_tail(&lock->l_res_link, head);
1046 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1047 struct ldlm_lock *new)
1049 struct ldlm_resource *res = original->l_resource;
1051 check_res_locked(res);
1053 ldlm_resource_dump(D_INFO, res);
1054 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1055 ldlm_lock_dump(D_OTHER, new, 0);
1057 if (new->l_destroyed) {
1058 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1062 LASSERT(cfs_list_empty(&new->l_res_link));
1064 cfs_list_add(&new->l_res_link, &original->l_res_link);
1068 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1070 int type = lock->l_resource->lr_type;
1072 check_res_locked(lock->l_resource);
1073 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1074 ldlm_unlink_lock_skiplist(lock);
1075 else if (type == LDLM_EXTENT)
1076 ldlm_extent_unlink_lock(lock);
1077 cfs_list_del_init(&lock->l_res_link);
1080 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1082 desc->lr_type = res->lr_type;
1083 desc->lr_name = res->lr_name;
1086 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1090 if (!((libcfs_debug | D_ERROR) & level))
1093 cfs_mutex_down(ldlm_namespace_lock(client));
1095 cfs_list_for_each(tmp, ldlm_namespace_list(client)) {
1096 struct ldlm_namespace *ns;
1097 ns = cfs_list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1098 ldlm_namespace_dump(level, ns);
1101 cfs_mutex_up(ldlm_namespace_lock(client));
1104 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1108 if (!((libcfs_debug | D_ERROR) & level))
1111 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1112 ns->ns_name, ns->ns_refcount,
1113 ns_is_client(ns) ? "client" : "server");
1115 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1118 cfs_spin_lock(&ns->ns_hash_lock);
1119 tmp = ns->ns_root_list.next;
1120 while (tmp != &ns->ns_root_list) {
1121 struct ldlm_resource *res;
1122 res = cfs_list_entry(tmp, struct ldlm_resource, lr_childof);
1124 ldlm_resource_getref(res);
1125 cfs_spin_unlock(&ns->ns_hash_lock);
1126 LDLM_RESOURCE_ADDREF(res);
1129 ldlm_resource_dump(level, res);
1132 LDLM_RESOURCE_DELREF(res);
1133 cfs_spin_lock(&ns->ns_hash_lock);
1135 ldlm_resource_putref_locked(res);
1137 ns->ns_next_dump = cfs_time_shift(10);
1138 cfs_spin_unlock(&ns->ns_hash_lock);
1141 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1146 CLASSERT(RES_NAME_SIZE == 4);
1148 if (!((libcfs_debug | D_ERROR) & level))
1151 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1152 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1153 res->lr_name.name[2], res->lr_name.name[3],
1154 cfs_atomic_read(&res->lr_refcount));
1156 if (!cfs_list_empty(&res->lr_granted)) {
1158 CDEBUG(level, "Granted locks:\n");
1159 cfs_list_for_each(tmp, &res->lr_granted) {
1160 struct ldlm_lock *lock;
1161 lock = cfs_list_entry(tmp, struct ldlm_lock,
1163 ldlm_lock_dump(level, lock, ++pos);
1166 if (!cfs_list_empty(&res->lr_converting)) {
1168 CDEBUG(level, "Converting locks:\n");
1169 cfs_list_for_each(tmp, &res->lr_converting) {
1170 struct ldlm_lock *lock;
1171 lock = cfs_list_entry(tmp, struct ldlm_lock,
1173 ldlm_lock_dump(level, lock, ++pos);
1176 if (!cfs_list_empty(&res->lr_waiting)) {
1178 CDEBUG(level, "Waiting locks:\n");
1179 cfs_list_for_each(tmp, &res->lr_waiting) {
1180 struct ldlm_lock *lock;
1181 lock = cfs_list_entry(tmp, struct ldlm_lock,
1183 ldlm_lock_dump(level, lock, ++pos);