1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
6 * Author: Peter Braam <braam@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
29 # include <lustre_dlm.h>
31 # include <liblustre.h>
34 #include <obd_class.h>
35 #include "ldlm_internal.h"
37 cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
39 atomic_t ldlm_srv_namespace_nr = ATOMIC_INIT(0);
40 atomic_t ldlm_cli_namespace_nr = ATOMIC_INIT(0);
42 struct semaphore ldlm_srv_namespace_lock;
43 CFS_LIST_HEAD(ldlm_srv_namespace_list);
45 struct semaphore ldlm_cli_namespace_lock;
46 CFS_LIST_HEAD(ldlm_cli_namespace_list);
48 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
49 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
50 cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
53 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
54 unsigned long count, void *data)
56 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
57 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
61 int ldlm_proc_setup(void)
64 struct lprocfs_vars list[] = {
65 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
68 LASSERT(ldlm_ns_proc_dir == NULL);
70 ldlm_type_proc_dir = lprocfs_register(OBD_LDLM_DEVICENAME,
73 if (IS_ERR(ldlm_type_proc_dir)) {
74 CERROR("LProcFS failed in ldlm-init\n");
75 rc = PTR_ERR(ldlm_type_proc_dir);
79 ldlm_ns_proc_dir = lprocfs_register("namespaces",
82 if (IS_ERR(ldlm_ns_proc_dir)) {
83 CERROR("LProcFS failed in ldlm-init\n");
84 rc = PTR_ERR(ldlm_ns_proc_dir);
88 ldlm_svc_proc_dir = lprocfs_register("services",
91 if (IS_ERR(ldlm_svc_proc_dir)) {
92 CERROR("LProcFS failed in ldlm-init\n");
93 rc = PTR_ERR(ldlm_svc_proc_dir);
97 rc = lprocfs_add_vars(ldlm_type_proc_dir, list, NULL);
102 lprocfs_remove(&ldlm_ns_proc_dir);
104 lprocfs_remove(&ldlm_type_proc_dir);
106 ldlm_svc_proc_dir = NULL;
110 void ldlm_proc_cleanup(void)
112 if (ldlm_svc_proc_dir)
113 lprocfs_remove(&ldlm_svc_proc_dir);
115 if (ldlm_ns_proc_dir)
116 lprocfs_remove(&ldlm_ns_proc_dir);
118 if (ldlm_type_proc_dir)
119 lprocfs_remove(&ldlm_type_proc_dir);
122 static int lprocfs_rd_lru_size(char *page, char **start, off_t off,
123 int count, int *eof, void *data)
125 struct ldlm_namespace *ns = data;
126 __u32 *nr = &ns->ns_max_unused;
128 if (ns_connect_lru_resize(ns))
129 nr = &ns->ns_nr_unused;
130 return lprocfs_rd_uint(page, start, off, count, eof, nr);
133 static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
134 unsigned long count, void *data)
136 struct ldlm_namespace *ns = data;
137 char dummy[MAX_STRING_SIZE + 1], *end;
141 dummy[MAX_STRING_SIZE] = '\0';
142 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
145 if (strncmp(dummy, "clear", 5) == 0) {
147 "dropping all unused locks from namespace %s\n",
149 if (ns_connect_lru_resize(ns)) {
150 int canceled, unused = ns->ns_nr_unused;
152 /* Try to cancel all @ns_nr_unused locks. */
153 canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
155 if (canceled < unused) {
156 CERROR("not all requested locks are canceled, "
157 "requested: %d, canceled: %d\n", unused,
162 tmp = ns->ns_max_unused;
163 ns->ns_max_unused = 0;
164 ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
165 ns->ns_max_unused = tmp;
170 tmp = simple_strtoul(dummy, &end, 0);
172 CERROR("invalid value written\n");
175 lru_resize = (tmp == 0);
177 if (ns_connect_lru_resize(ns)) {
179 ns->ns_max_unused = (unsigned int)tmp;
181 if (tmp > ns->ns_nr_unused)
182 tmp = ns->ns_nr_unused;
183 tmp = ns->ns_nr_unused - tmp;
186 "changing namespace %s unused locks from %u to %u\n",
187 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
188 ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
192 "disable lru_resize for namespace %s\n",
194 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
198 "changing namespace %s max_unused from %u to %u\n",
199 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
200 ns->ns_max_unused = (unsigned int)tmp;
201 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
203 /* Make sure that originally lru resize was supported before
204 * turning it on here. */
206 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
208 "enable lru_resize for namespace %s\n",
210 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
217 void ldlm_proc_namespace(struct ldlm_namespace *ns)
219 struct lprocfs_vars lock_vars[2];
220 char lock_name[MAX_STRING_SIZE + 1];
223 LASSERT(ns->ns_name != NULL);
225 lock_name[MAX_STRING_SIZE] = '\0';
227 memset(lock_vars, 0, sizeof(lock_vars));
228 lock_vars[0].name = lock_name;
230 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
231 lock_vars[0].data = &ns->ns_refcount;
232 lock_vars[0].read_fptr = lprocfs_rd_atomic;
233 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
235 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
236 lock_vars[0].data = &ns->ns_locks;
237 lock_vars[0].read_fptr = lprocfs_rd_atomic;
238 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
240 if (ns_is_client(ns)) {
241 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
243 lock_vars[0].data = &ns->ns_nr_unused;
244 lock_vars[0].read_fptr = lprocfs_rd_uint;
245 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
247 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
249 lock_vars[0].data = ns;
250 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
251 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
252 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
254 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
256 lock_vars[0].data = ns;
257 lock_vars[0].read_fptr = lprocfs_rd_uint;
258 lock_vars[0].write_fptr = lprocfs_wr_uint;
259 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
261 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
263 lock_vars[0].data = &ns->ns_max_age;
264 lock_vars[0].read_fptr = lprocfs_rd_uint;
265 lock_vars[0].write_fptr = lprocfs_wr_uint;
266 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
268 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
270 lock_vars[0].data = &ns->ns_ctime_age_limit;
271 lock_vars[0].read_fptr = lprocfs_rd_uint;
272 lock_vars[0].write_fptr = lprocfs_wr_uint;
273 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
275 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
277 lock_vars[0].data = &ns->ns_max_nolock_size;
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/contention_seconds",
284 lock_vars[0].data = &ns->ns_contention_time;
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/contended_locks",
291 lock_vars[0].data = &ns->ns_contended_locks;
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);
297 #undef MAX_STRING_SIZE
299 #define ldlm_proc_namespace(ns) do {} while (0)
302 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
303 ldlm_side_t client, ldlm_appetite_t apt)
305 struct ldlm_namespace *ns = NULL;
306 struct list_head *bucket;
307 int rc, idx, namelen;
312 CERROR("ldlm_get_ref failed: %d\n", rc);
320 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
324 ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
325 ns->ns_appetite = apt;
327 LASSERT(obd != NULL);
330 namelen = strlen(name);
331 OBD_ALLOC(ns->ns_name, namelen + 1);
333 GOTO(out_hash, NULL);
335 strcpy(ns->ns_name, name);
337 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
338 CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
340 ns->ns_client = client;
341 spin_lock_init(&ns->ns_hash_lock);
342 atomic_set(&ns->ns_locks, 0);
343 ns->ns_resources = 0;
344 cfs_waitq_init(&ns->ns_waitq);
345 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
346 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
347 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
349 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
351 CFS_INIT_LIST_HEAD(bucket);
353 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
354 ns->ns_nr_unused = 0;
355 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
356 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
357 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
358 spin_lock_init(&ns->ns_unused_lock);
359 ns->ns_orig_connect_flags = 0;
360 ns->ns_connect_flags = 0;
361 ldlm_proc_namespace(ns);
363 idx = atomic_read(ldlm_namespace_nr(client));
364 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
366 CERROR("Can't initialize lock pool, rc %d\n", rc);
370 at_init(&ns->ns_at_estimate, ldlm_enqueue_min, 0);
372 ldlm_namespace_register(ns, client);
375 ldlm_namespace_cleanup(ns, 0);
376 OBD_FREE(ns->ns_name, namelen + 1);
378 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
386 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
388 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
389 * This is currently only used for recovery, and we make certain assumptions
390 * as a result--notably, that we shouldn't cancel locks with refs. -phil
392 * Called with the ns_lock held. */
393 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
396 struct list_head *tmp;
397 int rc = 0, client = ns_is_client(res->lr_namespace);
398 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
403 struct ldlm_lock *lock = NULL;
405 /* first, we look for non-cleaned-yet lock
406 * all cleaned locks are marked by CLEANED flag */
408 list_for_each(tmp, q) {
409 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
410 if (lock->l_flags & LDLM_FL_CLEANED) {
415 lock->l_flags |= LDLM_FL_CLEANED;
424 /* Set CBPENDING so nothing in the cancellation path
425 * can match this lock */
426 lock->l_flags |= LDLM_FL_CBPENDING;
427 lock->l_flags |= LDLM_FL_FAILED;
428 lock->l_flags |= flags;
430 /* ... without sending a CANCEL message for local_only. */
432 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
434 if (local_only && (lock->l_readers || lock->l_writers)) {
435 /* This is a little bit gross, but much better than the
436 * alternative: pretend that we got a blocking AST from
437 * the server, so that when the lock is decref'd, it
438 * will go away ... */
440 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
441 if (lock->l_completion_ast)
442 lock->l_completion_ast(lock, 0, NULL);
448 struct lustre_handle lockh;
451 ldlm_lock2handle(lock, &lockh);
452 rc = ldlm_cli_cancel(&lockh);
454 CERROR("ldlm_cli_cancel: %d\n", rc);
456 ldlm_resource_unlink_lock(lock);
458 LDLM_DEBUG(lock, "Freeing a lock still held by a "
460 ldlm_lock_destroy(lock);
468 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
470 struct list_head *tmp;
474 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
478 for (i = 0; i < RES_HASH_SIZE; i++) {
479 spin_lock(&ns->ns_hash_lock);
480 tmp = ns->ns_hash[i].next;
481 while (tmp != &(ns->ns_hash[i])) {
482 struct ldlm_resource *res;
483 res = list_entry(tmp, struct ldlm_resource, lr_hash);
484 ldlm_resource_getref(res);
485 spin_unlock(&ns->ns_hash_lock);
487 cleanup_resource(res, &res->lr_granted, flags);
488 cleanup_resource(res, &res->lr_converting, flags);
489 cleanup_resource(res, &res->lr_waiting, flags);
491 spin_lock(&ns->ns_hash_lock);
494 /* XXX: former stuff caused issues in case of race
495 * between ldlm_namespace_cleanup() and lockd() when
496 * client gets blocking ast when lock gets distracted by
497 * server. This is 1_4 branch solution, let's see how
499 if (!ldlm_resource_putref_locked(res))
501 "Namespace %s resource refcount nonzero "
502 "(%d) after lock cleanup; forcing cleanup.\n",
503 ns->ns_name, atomic_read(&res->lr_refcount));
505 spin_unlock(&ns->ns_hash_lock);
511 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
515 /* At shutdown time, don't call the cancellation callback */
516 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
518 if (ns->ns_refcount > 0) {
519 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
522 "dlm namespace %s free waiting on refcount %d\n",
523 ns->ns_name, ns->ns_refcount);
526 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
528 rc = l_wait_event(ns->ns_waitq,
529 ns->ns_refcount == 0, &lwi);
531 /* Forced cleanups should be able to reclaim all references,
532 * so it's safe to wait forever... we can't leak locks... */
533 if (force && rc == -ETIMEDOUT) {
534 LCONSOLE_ERROR("Forced cleanup waiting for %s "
535 "namespace with %d resources in use, "
536 "(rc=%d)\n", ns->ns_name,
537 ns->ns_refcount, rc);
538 GOTO(force_wait, rc);
541 if (ns->ns_refcount) {
542 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
543 "with %d resources in use, (rc=%d)\n",
545 ns->ns_refcount, rc);
546 RETURN(ELDLM_NAMESPACE_EXISTS);
549 "dlm namespace %s free done waiting\n", ns->ns_name);
556 * Performs various cleanups for passed \a ns to make it drop refc and be ready
557 * for freeing. Waits for refc == 0.
559 * The following is done:
560 * (0) Unregister \a ns from its list to make inaccessible for potential users
561 * like pools thread and others;
562 * (1) Clear all locks in \a ns.
564 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
565 struct obd_import *imp,
576 * Make sure that nobody can find this ns in its list.
578 ldlm_namespace_unregister(ns, ns->ns_client);
581 * Can fail with -EINTR when force == 0 in which case try harder.
583 rc = __ldlm_namespace_free(ns, force);
584 if (rc != ELDLM_OK) {
586 ptlrpc_disconnect_import(imp, 0);
587 ptlrpc_invalidate_import(imp);
591 * With all requests dropped and the import inactive
592 * we are gaurenteed all reference will be dropped.
594 rc = __ldlm_namespace_free(ns, 1);
601 * Performs freeing memory structures related to \a ns. This is only done when
602 * ldlm_namespce_free_prior() successfully removed all resources referencing
603 * \a ns and its refc == 0.
605 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
614 * Fini pool _before_ parent proc dir is removed. This is important as
615 * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
616 * it after @dir may cause oops.
618 ldlm_pool_fini(&ns->ns_pool);
622 struct proc_dir_entry *dir;
623 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
625 CERROR("dlm namespace %s has no procfs dir?\n",
628 lprocfs_remove(&dir);
633 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
634 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
637 * Namespace \a ns should be not on list in this time, otherwise this
638 * will cause issues realted to using freed \a ns in pools thread.
640 LASSERT(list_empty(&ns->ns_list_chain));
647 /* Cleanup the resource, and free namespace.
650 * proc1: destroy import
651 * class_disconnect_export(grab cl_sem) ->
652 * -> ldlm_namespace_free ->
653 * -> lprocfs_remove(grab _lprocfs_lock).
654 * proc2: read proc info
655 * lprocfs_fops_read(grab _lprocfs_lock) ->
656 * -> osc_rd_active, etc(grab cl_sem).
658 * So that I have to split the ldlm_namespace_free into two parts - the first
659 * part ldlm_namespace_free_prior is used to cleanup the resource which is
660 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
661 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
664 void ldlm_namespace_free(struct ldlm_namespace *ns,
665 struct obd_import *imp,
668 ldlm_namespace_free_prior(ns, imp, force);
669 ldlm_namespace_free_post(ns);
673 void ldlm_namespace_get_locked(struct ldlm_namespace *ns)
675 LASSERT(ns->ns_refcount >= 0);
679 void ldlm_namespace_get(struct ldlm_namespace *ns)
681 spin_lock(&ns->ns_hash_lock);
682 ldlm_namespace_get_locked(ns);
683 spin_unlock(&ns->ns_hash_lock);
686 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup)
688 LASSERT(ns->ns_refcount > 0);
690 if (ns->ns_refcount == 0 && wakeup)
691 wake_up(&ns->ns_waitq);
694 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
696 spin_lock(&ns->ns_hash_lock);
697 ldlm_namespace_put_locked(ns, wakeup);
698 spin_unlock(&ns->ns_hash_lock);
701 /* Register @ns in the list of namespaces */
702 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
704 mutex_down(ldlm_namespace_lock(client));
705 LASSERT(list_empty(&ns->ns_list_chain));
706 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
707 atomic_inc(ldlm_namespace_nr(client));
708 mutex_up(ldlm_namespace_lock(client));
711 /* Unregister @ns from the list of namespaces */
712 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
714 mutex_down(ldlm_namespace_lock(client));
715 LASSERT(!list_empty(&ns->ns_list_chain));
717 * Some asserts and possibly other parts of code still using
718 * list_empty(&ns->ns_list_chain). This is why it is important
719 * to use list_del_init() here.
721 list_del_init(&ns->ns_list_chain);
722 atomic_dec(ldlm_namespace_nr(client));
723 mutex_up(ldlm_namespace_lock(client));
726 /* Should be called under ldlm_namespace_lock(client) taken */
727 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
729 LASSERT(!list_empty(&ns->ns_list_chain));
730 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
731 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
734 /* Should be called under ldlm_namespace_lock(client) taken */
735 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
737 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
738 LASSERT(!list_empty(ldlm_namespace_list(client)));
739 return container_of(ldlm_namespace_list(client)->next,
740 struct ldlm_namespace, ns_list_chain);
742 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
743 const struct ldlm_res_id *name)
748 for (i = 0; i < RES_NAME_SIZE; i++)
749 hash += name->name[i];
751 hash += (__u32)((unsigned long)parent >> 4);
753 return (hash & RES_HASH_MASK);
756 static struct ldlm_resource *ldlm_resource_new(void)
758 struct ldlm_resource *res;
761 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
765 memset(res, 0, sizeof(*res));
767 CFS_INIT_LIST_HEAD(&res->lr_children);
768 CFS_INIT_LIST_HEAD(&res->lr_childof);
769 CFS_INIT_LIST_HEAD(&res->lr_granted);
770 CFS_INIT_LIST_HEAD(&res->lr_converting);
771 CFS_INIT_LIST_HEAD(&res->lr_waiting);
773 /* initialize interval trees for each lock mode*/
774 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
775 res->lr_itree[idx].lit_size = 0;
776 res->lr_itree[idx].lit_mode = 1 << idx;
777 res->lr_itree[idx].lit_root = NULL;
780 atomic_set(&res->lr_refcount, 1);
781 spin_lock_init(&res->lr_lock);
783 /* one who creates the resource must unlock
784 * the semaphore after lvb initialization */
785 init_MUTEX_LOCKED(&res->lr_lvb_sem);
790 /* must be called with hash lock held */
791 static struct ldlm_resource *
792 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
795 struct list_head *bucket, *tmp;
796 struct ldlm_resource *res;
798 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
799 bucket = ns->ns_hash + hash;
801 list_for_each(tmp, bucket) {
802 res = list_entry(tmp, struct ldlm_resource, lr_hash);
803 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
810 /* Args: locked namespace
811 * Returns: newly-allocated, referenced, unlocked resource */
812 static struct ldlm_resource *
813 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
814 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
816 struct list_head *bucket;
817 struct ldlm_resource *res, *old_res;
820 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
823 res = ldlm_resource_new();
827 res->lr_name = *name;
828 res->lr_namespace = ns;
830 res->lr_most_restr = LCK_NL;
832 spin_lock(&ns->ns_hash_lock);
833 old_res = ldlm_resource_find(ns, name, hash);
835 /* someone won the race and added the resource before */
836 ldlm_resource_getref(old_res);
837 spin_unlock(&ns->ns_hash_lock);
838 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
839 /* synchronize WRT resource creation */
840 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
841 down(&old_res->lr_lvb_sem);
842 up(&old_res->lr_lvb_sem);
847 /* we won! let's add the resource */
848 bucket = ns->ns_hash + hash;
849 list_add(&res->lr_hash, bucket);
851 ldlm_namespace_get_locked(ns);
853 if (parent == NULL) {
854 list_add(&res->lr_childof, &ns->ns_root_list);
856 res->lr_parent = parent;
857 list_add(&res->lr_childof, &parent->lr_children);
859 spin_unlock(&ns->ns_hash_lock);
861 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
864 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
865 rc = ns->ns_lvbo->lvbo_init(res);
867 CERROR("lvbo_init failed for resource "
868 LPU64": rc %d\n", name->name[0], rc);
869 /* we create resource with locked lr_lvb_sem */
870 up(&res->lr_lvb_sem);
876 /* Args: unlocked namespace
877 * Locks: takes and releases ns->ns_lock and res->lr_lock
878 * Returns: referenced, unlocked ldlm_resource or NULL */
879 struct ldlm_resource *
880 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
881 const struct ldlm_res_id *name, ldlm_type_t type, int create)
883 __u32 hash = ldlm_hash_fn(parent, name);
884 struct ldlm_resource *res = NULL;
888 LASSERT(ns->ns_hash != NULL);
889 LASSERT(name->name[0] != 0);
891 spin_lock(&ns->ns_hash_lock);
892 res = ldlm_resource_find(ns, name, hash);
894 ldlm_resource_getref(res);
895 spin_unlock(&ns->ns_hash_lock);
896 /* synchronize WRT resource creation */
897 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
898 down(&res->lr_lvb_sem);
899 up(&res->lr_lvb_sem);
903 spin_unlock(&ns->ns_hash_lock);
908 res = ldlm_resource_add(ns, parent, name, hash, type);
912 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
914 LASSERT(res != NULL);
915 LASSERT(res != LP_POISON);
916 atomic_inc(&res->lr_refcount);
917 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
918 atomic_read(&res->lr_refcount));
922 void __ldlm_resource_putref_final(struct ldlm_resource *res)
924 struct ldlm_namespace *ns = res->lr_namespace;
926 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
928 if (!list_empty(&res->lr_granted)) {
929 ldlm_resource_dump(D_ERROR, res);
933 if (!list_empty(&res->lr_converting)) {
934 ldlm_resource_dump(D_ERROR, res);
938 if (!list_empty(&res->lr_waiting)) {
939 ldlm_resource_dump(D_ERROR, res);
943 if (!list_empty(&res->lr_children)) {
944 ldlm_resource_dump(D_ERROR, res);
948 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
949 * lines below when all children are freed. */
950 ldlm_namespace_put_locked(ns, 0);
951 list_del_init(&res->lr_hash);
952 list_del_init(&res->lr_childof);
955 if (ns->ns_resources == 0)
956 wake_up(&ns->ns_waitq);
959 /* Returns 1 if the resource was freed, 0 if it remains. */
960 int ldlm_resource_putref(struct ldlm_resource *res)
962 struct ldlm_namespace *ns = res->lr_namespace;
966 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
967 atomic_read(&res->lr_refcount) - 1);
968 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
969 atomic_read(&res->lr_refcount));
970 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
971 atomic_read(&res->lr_refcount));
973 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
974 __ldlm_resource_putref_final(res);
975 spin_unlock(&ns->ns_hash_lock);
976 if (res->lr_lvb_data)
977 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
978 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
985 /* Returns 1 if the resource was freed, 0 if it remains. */
986 int ldlm_resource_putref_locked(struct ldlm_resource *res)
991 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
992 atomic_read(&res->lr_refcount) - 1);
993 LASSERT(atomic_read(&res->lr_refcount) > 0);
994 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
996 LASSERT(atomic_read(&res->lr_refcount) >= 0);
997 if (atomic_dec_and_test(&res->lr_refcount)) {
998 __ldlm_resource_putref_final(res);
999 if (res->lr_lvb_data)
1000 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1001 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
1008 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
1009 struct ldlm_lock *lock)
1011 check_res_locked(res);
1013 ldlm_resource_dump(D_INFO, res);
1014 CDEBUG(D_INFO, "About to add this lock:\n");
1015 ldlm_lock_dump(D_INFO, lock, 0);
1017 if (lock->l_destroyed) {
1018 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1022 LASSERT(list_empty(&lock->l_res_link));
1024 list_add_tail(&lock->l_res_link, head);
1027 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
1028 struct ldlm_lock *new)
1030 struct ldlm_resource *res = original->l_resource;
1032 check_res_locked(res);
1034 ldlm_resource_dump(D_OTHER, res);
1035 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
1036 ldlm_lock_dump(D_OTHER, new, 0);
1038 if (new->l_destroyed) {
1039 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
1043 LASSERT(list_empty(&new->l_res_link));
1045 list_add(&new->l_res_link, &original->l_res_link);
1049 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
1051 int type = lock->l_resource->lr_type;
1053 check_res_locked(lock->l_resource);
1054 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1055 ldlm_unlink_lock_skiplist(lock);
1056 else if (type == LDLM_EXTENT)
1057 ldlm_extent_unlink_lock(lock);
1058 list_del_init(&lock->l_res_link);
1061 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1063 desc->lr_type = res->lr_type;
1064 desc->lr_name = res->lr_name;
1067 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1069 struct list_head *tmp;
1071 if (!((libcfs_debug | D_ERROR) & level))
1074 mutex_down(ldlm_namespace_lock(client));
1076 list_for_each(tmp, ldlm_namespace_list(client)) {
1077 struct ldlm_namespace *ns;
1078 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1079 ldlm_namespace_dump(level, ns);
1082 mutex_up(ldlm_namespace_lock(client));
1085 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1087 struct list_head *tmp;
1089 if (!((libcfs_debug | D_ERROR) & level))
1092 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1093 ns->ns_name, ns->ns_refcount,
1094 ns_is_client(ns) ? "client" : "server");
1096 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1099 spin_lock(&ns->ns_hash_lock);
1100 tmp = ns->ns_root_list.next;
1101 while (tmp != &ns->ns_root_list) {
1102 struct ldlm_resource *res;
1103 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1105 ldlm_resource_getref(res);
1106 spin_unlock(&ns->ns_hash_lock);
1109 ldlm_resource_dump(level, res);
1112 spin_lock(&ns->ns_hash_lock);
1114 ldlm_resource_putref_locked(res);
1116 ns->ns_next_dump = cfs_time_shift(10);
1117 spin_unlock(&ns->ns_hash_lock);
1120 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1122 struct list_head *tmp;
1125 CLASSERT(RES_NAME_SIZE == 4);
1127 if (!((libcfs_debug | D_ERROR) & level))
1130 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1131 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1132 res->lr_name.name[2], res->lr_name.name[3],
1133 atomic_read(&res->lr_refcount));
1135 if (!list_empty(&res->lr_granted)) {
1137 CDEBUG(level, "Granted locks:\n");
1138 list_for_each(tmp, &res->lr_granted) {
1139 struct ldlm_lock *lock;
1140 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1141 ldlm_lock_dump(level, lock, ++pos);
1144 if (!list_empty(&res->lr_converting)) {
1146 CDEBUG(level, "Converting locks:\n");
1147 list_for_each(tmp, &res->lr_converting) {
1148 struct ldlm_lock *lock;
1149 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1150 ldlm_lock_dump(level, lock, ++pos);
1153 if (!list_empty(&res->lr_waiting)) {
1155 CDEBUG(level, "Waiting locks:\n");
1156 list_for_each(tmp, &res->lr_waiting) {
1157 struct ldlm_lock *lock;
1158 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1159 ldlm_lock_dump(level, lock, ++pos);