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 2008 Sun Microsystems, Inc. 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 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
55 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
57 struct semaphore ldlm_srv_namespace_lock;
58 CFS_LIST_HEAD(ldlm_srv_namespace_list);
60 struct semaphore 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;
68 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
69 unsigned long count, void *data)
71 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
72 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
76 int ldlm_proc_setup(void)
79 struct lprocfs_vars list[] = {
80 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
83 LASSERT(ldlm_ns_proc_dir == NULL);
85 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
88 if (IS_ERR(ldlm_type_proc_dir)) {
89 CERROR("LProcFS failed in ldlm-init\n");
90 rc = PTR_ERR(ldlm_type_proc_dir);
94 ldlm_ns_proc_dir = lprocfs_register("namespaces",
97 if (IS_ERR(ldlm_ns_proc_dir)) {
98 CERROR("LProcFS failed in ldlm-init\n");
99 rc = PTR_ERR(ldlm_ns_proc_dir);
103 ldlm_svc_proc_dir = lprocfs_register("services",
106 if (IS_ERR(ldlm_svc_proc_dir)) {
107 CERROR("LProcFS failed in ldlm-init\n");
108 rc = PTR_ERR(ldlm_svc_proc_dir);
112 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
117 lprocfs_remove(&ldlm_ns_proc_dir);
119 lprocfs_remove(&ldlm_type_proc_dir);
121 ldlm_svc_proc_dir = NULL;
125 void ldlm_proc_cleanup(void)
127 if (ldlm_svc_proc_dir)
128 lprocfs_remove(&ldlm_svc_proc_dir);
130 if (ldlm_ns_proc_dir)
131 lprocfs_remove(&ldlm_ns_proc_dir);
133 if (ldlm_type_proc_dir)
134 lprocfs_remove(&ldlm_type_proc_dir);
137 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
138 int count, int *eof, void *data)
140 struct ldlm_namespace *ns = data;
141 __u32 *nr = &ns->ns_max_unused;
143 if (ns_connect_lru_resize(ns))
144 nr = &ns->ns_nr_unused;
145 return lprocfs_rd_uint(page, start, off, count, eof, nr);
148 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
149 unsigned long count, void *data)
151 struct ldlm_namespace *ns = data;
152 char dummy[MAX_STRING_SIZE + 1], *end;
156 dummy[MAX_STRING_SIZE] = '\0';
157 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
160 if (strncmp(dummy, "clear", 5) == 0) {
162 "dropping all unused locks from namespace %s\n",
164 if (ns_connect_lru_resize(ns)) {
165 int canceled, unused = ns->ns_nr_unused;
167 /* Try to cancel all @ns_nr_unused locks. */
168 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
170 if (canceled < unused) {
171 CERROR("not all requested locks are canceled, "
172 "requested: %d, canceled: %d\n", unused,
177 tmp = ns->ns_max_unused;
178 ns->ns_max_unused = 0;
179 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
180 ns->ns_max_unused = tmp;
185 tmp = simple_strtoul(dummy, &end, 0);
187 CERROR("invalid value written\n");
190 lru_resize = (tmp == 0);
192 if (ns_connect_lru_resize(ns)) {
194 ns->ns_max_unused = (unsigned int)tmp;
196 if (tmp > ns->ns_nr_unused)
197 tmp = ns->ns_nr_unused;
198 tmp = ns->ns_nr_unused - tmp;
201 "changing namespace %s unused locks from %u to %u\n",
202 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
203 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
207 "disable lru_resize for namespace %s\n",
209 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
213 "changing namespace %s max_unused from %u to %u\n",
214 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
215 ns->ns_max_unused = (unsigned int)tmp;
216 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
218 /* Make sure that originally lru resize was supported before
219 * turning it on here. */
221 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
223 "enable lru_resize for namespace %s\n",
225 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
232 void ldlm_proc_namespace(struct ldlm_namespace *ns)
234 struct lprocfs_vars lock_vars[2];
235 char lock_name[MAX_STRING_SIZE + 1];
238 LASSERT(ns->ns_name != NULL);
240 lock_name[MAX_STRING_SIZE] = '\0';
242 memset(lock_vars, 0, sizeof(lock_vars));
243 lock_vars[0].name = lock_name;
245 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
246 lock_vars[0].data = &ns->ns_refcount;
247 lock_vars[0].read_fptr = lprocfs_rd_atomic;
248 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
250 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
251 lock_vars[0].data = &ns->ns_locks;
252 lock_vars[0].read_fptr = lprocfs_rd_atomic;
253 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
255 if (ns_is_client(ns)) {
256 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
258 lock_vars[0].data = &ns->ns_nr_unused;
259 lock_vars[0].read_fptr = lprocfs_rd_uint;
260 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
262 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
264 lock_vars[0].data = ns;
265 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
266 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
267 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
269 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
271 lock_vars[0].data = &ns->ns_max_age;
272 lock_vars[0].read_fptr = lprocfs_rd_uint;
273 lock_vars[0].write_fptr = lprocfs_wr_uint;
274 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
276 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
278 lock_vars[0].data = &ns->ns_ctime_age_limit;
279 lock_vars[0].read_fptr = lprocfs_rd_uint;
280 lock_vars[0].write_fptr = lprocfs_wr_uint;
281 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
283 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_timeouts",
285 lock_vars[0].data = &ns->ns_timeouts;
286 lock_vars[0].read_fptr = lprocfs_rd_uint;
287 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
289 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
291 lock_vars[0].data = &ns->ns_max_nolock_size;
292 lock_vars[0].read_fptr = lprocfs_rd_uint;
293 lock_vars[0].write_fptr = lprocfs_wr_uint;
294 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
296 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
298 lock_vars[0].data = &ns->ns_contention_time;
299 lock_vars[0].read_fptr = lprocfs_rd_uint;
300 lock_vars[0].write_fptr = lprocfs_wr_uint;
301 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
303 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
305 lock_vars[0].data = &ns->ns_contended_locks;
306 lock_vars[0].read_fptr = lprocfs_rd_uint;
307 lock_vars[0].write_fptr = lprocfs_wr_uint;
308 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
311 #undef MAX_STRING_SIZE
313 #define ldlm_proc_namespace(ns) do {} while (0)
316 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
317 ldlm_side_t client, ldlm_appetite_t apt)
319 struct ldlm_namespace *ns = NULL;
320 struct list_head *bucket;
321 int rc, idx, namelen;
326 CERROR("ldlm_get_ref failed: %d\n", rc);
334 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
338 ns->ns_appetite = apt;
340 LASSERT(obd != NULL);
343 namelen = strlen(name);
344 OBD_ALLOC(ns->ns_name, namelen + 1);
346 GOTO(out_hash, NULL);
348 strcpy(ns->ns_name, name);
350 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
351 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
353 ns->ns_client = client;
354 spin_lock_init(&ns->ns_hash_lock);
355 atomic_set(&ns->ns_locks, 0);
356 ns->ns_resources = 0;
357 cfs_waitq_init(&ns->ns_waitq);
358 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
359 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
360 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
362 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
364 CFS_INIT_LIST_HEAD(bucket);
366 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
367 ns->ns_nr_unused = 0;
368 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
369 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
370 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
372 spin_lock_init(&ns->ns_unused_lock);
373 ns->ns_orig_connect_flags = 0;
374 ns->ns_connect_flags = 0;
375 ldlm_proc_namespace(ns);
377 idx = atomic_read(ldlm_namespace_nr(client));
378 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
380 CERROR("Can't initialize lock pool, rc %d\n", rc);
384 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
386 ldlm_namespace_register(ns, client);
389 ldlm_namespace_cleanup(ns, 0);
390 OBD_FREE(ns->ns_name, namelen + 1);
392 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
400 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
402 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
403 * This is currently only used for recovery, and we make certain assumptions
404 * as a result--notably, that we shouldn't cancel locks with refs. -phil
406 * Called with the ns_lock held. */
407 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
410 struct list_head *tmp;
411 int rc = 0, client = ns_is_client(res->lr_namespace);
412 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
417 struct ldlm_lock *lock = NULL;
419 /* first, we look for non-cleaned-yet lock
420 * all cleaned locks are marked by CLEANED flag */
422 list_for_each(tmp, q) {
423 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
424 if (lock->l_flags & LDLM_FL_CLEANED) {
429 lock->l_flags |= LDLM_FL_CLEANED;
438 /* Set CBPENDING so nothing in the cancellation path
439 * can match this lock */
440 lock->l_flags |= LDLM_FL_CBPENDING;
441 lock->l_flags |= LDLM_FL_FAILED;
442 lock->l_flags |= flags;
444 /* ... without sending a CANCEL message for local_only. */
446 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
448 if (local_only && (lock->l_readers || lock->l_writers)) {
449 /* This is a little bit gross, but much better than the
450 * alternative: pretend that we got a blocking AST from
451 * the server, so that when the lock is decref'd, it
452 * will go away ... */
454 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
455 if (lock->l_completion_ast)
456 lock->l_completion_ast(lock, 0, NULL);
457 LDLM_LOCK_RELEASE(lock);
462 struct lustre_handle lockh;
465 ldlm_lock2handle(lock, &lockh);
466 rc = ldlm_cli_cancel(&lockh);
468 CERROR("ldlm_cli_cancel: %d\n", rc);
470 ldlm_resource_unlink_lock(lock);
472 LDLM_DEBUG(lock, "Freeing a lock still held by a "
474 ldlm_lock_destroy(lock);
476 LDLM_LOCK_RELEASE(lock);
482 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
484 struct list_head *tmp;
488 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
492 for (i = 0; i < RES_HASH_SIZE; i++) {
493 spin_lock(&ns->ns_hash_lock);
494 tmp = ns->ns_hash[i].next;
495 while (tmp != &(ns->ns_hash[i])) {
496 struct ldlm_resource *res;
497 res = list_entry(tmp, struct ldlm_resource, lr_hash);
498 ldlm_resource_getref(res);
499 spin_unlock(&ns->ns_hash_lock);
500 LDLM_RESOURCE_ADDREF(res);
502 cleanup_resource(res, &res->lr_granted, flags);
503 cleanup_resource(res, &res->lr_converting, flags);
504 cleanup_resource(res, &res->lr_waiting, flags);
506 spin_lock(&ns->ns_hash_lock);
509 /* XXX: former stuff caused issues in case of race
510 * between ldlm_namespace_cleanup() and lockd() when
511 * client gets blocking ast when lock gets distracted by
512 * server. This is 1_4 branch solution, let's see how
514 LDLM_RESOURCE_DELREF(res);
515 if (!ldlm_resource_putref_locked(res))
517 "Namespace %s resource refcount nonzero "
518 "(%d) after lock cleanup; forcing cleanup.\n",
519 ns->ns_name, atomic_read(&res->lr_refcount));
521 spin_unlock(&ns->ns_hash_lock);
527 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
531 /* At shutdown time, don't call the cancellation callback */
532 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
534 if (ns->ns_refcount > 0) {
535 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
538 "dlm namespace %s free waiting on refcount %d\n",
539 ns->ns_name, ns->ns_refcount);
542 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
544 rc = l_wait_event(ns->ns_waitq,
545 ns->ns_refcount == 0, &lwi);
547 /* Forced cleanups should be able to reclaim all references,
548 * so it's safe to wait forever... we can't leak locks... */
549 if (force && rc == -ETIMEDOUT) {
550 LCONSOLE_ERROR("Forced cleanup waiting for %s "
551 "namespace with %d resources in use, "
552 "(rc=%d)\n", ns->ns_name,
553 ns->ns_refcount, rc);
554 GOTO(force_wait, rc);
557 if (ns->ns_refcount) {
558 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
559 "with %d resources in use, (rc=%d)\n",
561 ns->ns_refcount, rc);
562 RETURN(ELDLM_NAMESPACE_EXISTS);
565 "dlm namespace %s free done waiting\n", ns->ns_name);
572 * Performs various cleanups for passed \a ns to make it drop refc and be ready
573 * for freeing. Waits for refc == 0.
575 * The following is done:
576 * (0) Unregister \a ns from its list to make inaccessible for potential users
577 * like pools thread and others;
578 * (1) Clear all locks in \a ns.
580 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
581 struct obd_import *imp,
593 * Can fail with -EINTR when force == 0 in which case try harder.
595 rc = __ldlm_namespace_free(ns, force);
596 if (rc != ELDLM_OK) {
598 ptlrpc_disconnect_import(imp, 0);
599 ptlrpc_invalidate_import(imp);
603 * With all requests dropped and the import inactive
604 * we are gaurenteed all reference will be dropped.
606 rc = __ldlm_namespace_free(ns, 1);
613 * Performs freeing memory structures related to \a ns. This is only done when
614 * ldlm_namespce_free_prior() successfully removed all resources referencing
615 * \a ns and its refc == 0.
617 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
627 * Make sure that nobody can find this ns in its list.
629 ldlm_namespace_unregister(ns, ns->ns_client);
631 * Fini pool _before_ parent proc dir is removed. This is important as
632 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
633 * it after @dir may cause oops.
635 ldlm_pool_fini(&ns->ns_pool);
639 struct proc_dir_entry *dir;
640 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
642 CERROR("dlm namespace %s has no procfs dir?\n",
645 lprocfs_remove(&dir);
650 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
651 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
654 * Namespace \a ns should be not on list in this time, otherwise this
655 * will cause issues realted to using freed \a ns in pools thread.
657 LASSERT(list_empty(&ns->ns_list_chain));
664 /* Cleanup the resource, and free namespace.
667 * proc1: destroy import
668 * class_disconnect_export(grab cl_sem) ->
669 * -> ldlm_namespace_free ->
670 * -> lprocfs_remove(grab _lprocfs_lock).
671 * proc2: read proc info
672 * lprocfs_fops_read(grab _lprocfs_lock) ->
673 * -> osc_rd_active, etc(grab cl_sem).
675 * So that I have to split the ldlm_namespace_free into two parts - the first
676 * part ldlm_namespace_free_prior is used to cleanup the resource which is
677 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
678 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
681 void ldlm_namespace_free(struct ldlm_namespace *ns,
682 struct obd_import *imp,
685 ldlm_namespace_free_prior(ns, imp, force);
686 ldlm_namespace_free_post(ns);
690 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
695 void ldlm_namespace_get(struct ldlm_namespace *ns)
697 spin_lock(&ns->ns_hash_lock);
698 ldlm_namespace_get_locked(ns);
699 spin_unlock(&ns->ns_hash_lock);
702 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
704 LASSERT(ns->ns_refcount > 0);
706 if (ns->ns_refcount == 0 && wakeup)
707 wake_up(&ns->ns_waitq);
710 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
712 spin_lock(&ns->ns_hash_lock);
713 ldlm_namespace_put_locked(ns, wakeup);
714 spin_unlock(&ns->ns_hash_lock);
717 /* Register @ns in the list of namespaces */
718 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
720 mutex_down(ldlm_namespace_lock(client));
721 LASSERT(list_empty(&ns->ns_list_chain));
722 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
723 atomic_inc(ldlm_namespace_nr(client));
724 mutex_up(ldlm_namespace_lock(client));
727 /* Unregister @ns from the list of namespaces */
728 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
730 mutex_down(ldlm_namespace_lock(client));
731 LASSERT(!list_empty(&ns->ns_list_chain));
733 * Some asserts and possibly other parts of code still using
734 * list_empty(&ns->ns_list_chain). This is why it is important
735 * to use list_del_init() here.
737 list_del_init(&ns->ns_list_chain);
738 atomic_dec(ldlm_namespace_nr(client));
739 mutex_up(ldlm_namespace_lock(client));
742 /* Should be called under ldlm_namespace_lock(client) taken */
743 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
745 LASSERT(!list_empty(&ns->ns_list_chain));
746 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
747 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
750 /* Should be called under ldlm_namespace_lock(client) taken */
751 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
753 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
754 LASSERT(!list_empty(ldlm_namespace_list(client)));
755 return container_of(ldlm_namespace_list(client)->next,
756 struct ldlm_namespace, ns_list_chain);
758 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
759 const struct ldlm_res_id *name)
764 for (i = 0; i < RES_NAME_SIZE; i++)
765 hash += name->name[i];
767 hash += (__u32)((unsigned long)parent >> 4);
769 return (hash & RES_HASH_MASK);
772 static struct ldlm_resource *ldlm_resource_new(void)
774 struct ldlm_resource *res;
777 OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
781 memset(res, 0, sizeof(*res));
783 CFS_INIT_LIST_HEAD(&res->lr_children);
784 CFS_INIT_LIST_HEAD(&res->lr_childof);
785 CFS_INIT_LIST_HEAD(&res->lr_granted);
786 CFS_INIT_LIST_HEAD(&res->lr_converting);
787 CFS_INIT_LIST_HEAD(&res->lr_waiting);
789 /* initialize interval trees for each lock mode*/
790 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
791 res->lr_itree[idx].lit_size = 0;
792 res->lr_itree[idx].lit_mode = 1 << idx;
793 res->lr_itree[idx].lit_root = NULL;
796 atomic_set(&res->lr_refcount, 1);
797 spin_lock_init(&res->lr_lock);
798 lu_ref_init(&res->lr_reference);
800 /* one who creates the resource must unlock
801 * the semaphore after lvb initialization */
802 init_MUTEX_LOCKED(&res->lr_lvb_sem);
807 /* must be called with hash lock held */
808 static struct ldlm_resource *
809 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
812 struct list_head *bucket, *tmp;
813 struct ldlm_resource *res;
815 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
816 bucket = ns->ns_hash + hash;
818 list_for_each(tmp, bucket) {
819 res = list_entry(tmp, struct ldlm_resource, lr_hash);
820 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
827 /* Args: locked namespace
828 * Returns: newly-allocated, referenced, unlocked resource */
829 static struct ldlm_resource *
830 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
831 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
833 struct list_head *bucket;
834 struct ldlm_resource *res, *old_res;
837 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
840 res = ldlm_resource_new();
844 res->lr_name = *name;
845 res->lr_namespace = ns;
847 res->lr_most_restr = LCK_NL;
849 spin_lock(&ns->ns_hash_lock);
850 old_res = ldlm_resource_find(ns, name, hash);
852 /* someone won the race and added the resource before */
853 ldlm_resource_getref(old_res);
854 spin_unlock(&ns->ns_hash_lock);
855 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
856 /* synchronize WRT resource creation */
857 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
858 down(&old_res->lr_lvb_sem);
859 up(&old_res->lr_lvb_sem);
864 /* we won! let's add the resource */
865 bucket = ns->ns_hash + hash;
866 list_add(&res->lr_hash, bucket);
868 ldlm_namespace_get_locked(ns);
870 if (parent == NULL) {
871 list_add(&res->lr_childof, &ns->ns_root_list);
873 res->lr_parent = parent;
874 list_add(&res->lr_childof, &parent->lr_children);
876 spin_unlock(&ns->ns_hash_lock);
878 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
881 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
882 rc = ns->ns_lvbo->lvbo_init(res);
884 CERROR("lvbo_init failed for resource "
885 LPU64": rc %d\n", name->name[0], rc);
886 /* we create resource with locked lr_lvb_sem */
887 up(&res->lr_lvb_sem);
893 /* Args: unlocked namespace
894 * Locks: takes and releases ns->ns_lock and res->lr_lock
895 * Returns: referenced, unlocked ldlm_resource or NULL */
896 struct ldlm_resource *
897 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
898 const struct ldlm_res_id *name, ldlm_type_t type, int create)
900 __u32 hash = ldlm_hash_fn(parent, name);
901 struct ldlm_resource *res = NULL;
905 LASSERT(ns->ns_hash != NULL);
906 LASSERT(name->name[0] != 0);
908 spin_lock(&ns->ns_hash_lock);
909 res = ldlm_resource_find(ns, name, hash);
911 ldlm_resource_getref(res);
912 spin_unlock(&ns->ns_hash_lock);
913 /* synchronize WRT resource creation */
914 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
915 down(&res->lr_lvb_sem);
916 up(&res->lr_lvb_sem);
920 spin_unlock(&ns->ns_hash_lock);
925 res = ldlm_resource_add(ns, parent, name, hash, type);
929 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
931 LASSERT(res != NULL);
932 LASSERT(res != LP_POISON);
933 atomic_inc(&res->lr_refcount);
934 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
935 atomic_read(&res->lr_refcount));
939 void __ldlm_resource_putref_final(struct ldlm_resource *res)
941 struct ldlm_namespace *ns = res->lr_namespace;
943 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
945 if (!list_empty(&res->lr_granted)) {
946 ldlm_resource_dump(D_ERROR, res);
950 if (!list_empty(&res->lr_converting)) {
951 ldlm_resource_dump(D_ERROR, res);
955 if (!list_empty(&res->lr_waiting)) {
956 ldlm_resource_dump(D_ERROR, res);
960 if (!list_empty(&res->lr_children)) {
961 ldlm_resource_dump(D_ERROR, res);
965 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
966 * lines below when all children are freed. */
967 ldlm_namespace_put_locked(ns, 0);
968 list_del_init(&res->lr_hash);
969 list_del_init(&res->lr_childof);
970 lu_ref_fini(&res->lr_reference);
973 if (ns->ns_resources == 0)
974 wake_up(&ns->ns_waitq);
977 /* Returns 1 if the resource was freed, 0 if it remains. */
978 int ldlm_resource_putref(struct ldlm_resource *res)
980 struct ldlm_namespace *ns = res->lr_namespace;
984 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
985 atomic_read(&res->lr_refcount) - 1);
986 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
987 atomic_read(&res->lr_refcount));
988 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
989 atomic_read(&res->lr_refcount));
991 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
992 __ldlm_resource_putref_final(res);
993 spin_unlock(&ns->ns_hash_lock);
994 if (res->lr_lvb_data)
995 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
996 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1003 /* Returns 1 if the resource was freed, 0 if it remains. */
1004 int ldlm_resource_putref_locked(struct ldlm_resource *res)
1009 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
1010 atomic_read(&res->lr_refcount) - 1);
1011 LASSERT(atomic_read(&res->lr_refcount) > 0);
1012 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
1014 LASSERT(atomic_read(&res->lr_refcount) >= 0);
1015 if (atomic_dec_and_test(&res->lr_refcount)) {
1016 __ldlm_resource_putref_final(res);
1017 if (res->lr_lvb_data)
1018 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1019 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1026 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1027 struct ldlm_lock *lock)
1029 check_res_locked(res);
1031 ldlm_resource_dump(D_INFO, res);
1032 CDEBUG(D_OTHER, "About to add this lock:\n");
1033 ldlm_lock_dump(D_OTHER, lock, 0);
1035 if (lock->l_destroyed) {
1036 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1040 LASSERT(list_empty(&lock->l_res_link));
1042 list_add_tail(&lock->l_res_link, head);
1045 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1046 struct ldlm_lock *new)
1048 struct ldlm_resource *res = original->l_resource;
1050 check_res_locked(res);
1052 ldlm_resource_dump(D_INFO, res);
1053 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1054 ldlm_lock_dump(D_OTHER, new, 0);
1056 if (new->l_destroyed) {
1057 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1061 LASSERT(list_empty(&new->l_res_link));
1063 list_add(&new->l_res_link, &original->l_res_link);
1067 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1069 int type = lock->l_resource->lr_type;
1071 check_res_locked(lock->l_resource);
1072 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1073 ldlm_unlink_lock_skiplist(lock);
1074 else if (type == LDLM_EXTENT)
1075 ldlm_extent_unlink_lock(lock);
1076 list_del_init(&lock->l_res_link);
1079 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1081 desc->lr_type = res->lr_type;
1082 desc->lr_name = res->lr_name;
1085 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1087 struct list_head *tmp;
1089 if (!((libcfs_debug | D_ERROR) & level))
1092 mutex_down(ldlm_namespace_lock(client));
1094 list_for_each(tmp, ldlm_namespace_list(client)) {
1095 struct ldlm_namespace *ns;
1096 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1097 ldlm_namespace_dump(level, ns);
1100 mutex_up(ldlm_namespace_lock(client));
1103 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1105 struct list_head *tmp;
1107 if (!((libcfs_debug | D_ERROR) & level))
1110 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1111 ns->ns_name, ns->ns_refcount,
1112 ns_is_client(ns) ? "client" : "server");
1114 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1117 spin_lock(&ns->ns_hash_lock);
1118 tmp = ns->ns_root_list.next;
1119 while (tmp != &ns->ns_root_list) {
1120 struct ldlm_resource *res;
1121 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1123 ldlm_resource_getref(res);
1124 spin_unlock(&ns->ns_hash_lock);
1125 LDLM_RESOURCE_ADDREF(res);
1128 ldlm_resource_dump(level, res);
1131 LDLM_RESOURCE_DELREF(res);
1132 spin_lock(&ns->ns_hash_lock);
1134 ldlm_resource_putref_locked(res);
1136 ns->ns_next_dump = cfs_time_shift(10);
1137 spin_unlock(&ns->ns_hash_lock);
1140 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1142 struct list_head *tmp;
1145 CLASSERT(RES_NAME_SIZE == 4);
1147 if (!((libcfs_debug | D_ERROR) & level))
1150 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1151 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1152 res->lr_name.name[2], res->lr_name.name[3],
1153 atomic_read(&res->lr_refcount));
1155 if (!list_empty(&res->lr_granted)) {
1157 CDEBUG(level, "Granted locks:\n");
1158 list_for_each(tmp, &res->lr_granted) {
1159 struct ldlm_lock *lock;
1160 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1161 ldlm_lock_dump(level, lock, ++pos);
1164 if (!list_empty(&res->lr_converting)) {
1166 CDEBUG(level, "Converting locks:\n");
1167 list_for_each(tmp, &res->lr_converting) {
1168 struct ldlm_lock *lock;
1169 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1170 ldlm_lock_dump(level, lock, ++pos);
1173 if (!list_empty(&res->lr_waiting)) {
1175 CDEBUG(level, "Waiting locks:\n");
1176 list_for_each(tmp, &res->lr_waiting) {
1177 struct ldlm_lock *lock;
1178 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1179 ldlm_lock_dump(level, lock, ++pos);