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 (count == 6 && memcmp(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;
185 CDEBUG(D_DLMTRACE, "changing namespace %s unused locks from %u to %u\n",
186 ns->ns_name, ns->ns_nr_unused, (unsigned int)tmp);
187 ldlm_cancel_lru(ns, (unsigned int)tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
190 CDEBUG(D_DLMTRACE, "disable lru_resize for namespace %s\n",
192 ns->ns_connect_flags &= ~OBD_CONNECT_LRU_RESIZE;
195 CDEBUG(D_DLMTRACE, "changing namespace %s max_unused from %u to %u\n",
196 ns->ns_name, ns->ns_max_unused, (unsigned int)tmp);
197 ns->ns_max_unused = (unsigned int)tmp;
198 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
200 /* Make sure that originally lru resize was supported before
201 * turning it on here. */
203 (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
204 CDEBUG(D_DLMTRACE, "enable lru_resize for namespace %s\n",
206 ns->ns_connect_flags |= OBD_CONNECT_LRU_RESIZE;
213 void ldlm_proc_namespace(struct ldlm_namespace *ns)
215 struct lprocfs_vars lock_vars[2];
216 char lock_name[MAX_STRING_SIZE + 1];
219 LASSERT(ns->ns_name != NULL);
221 lock_name[MAX_STRING_SIZE] = '\0';
223 memset(lock_vars, 0, sizeof(lock_vars));
224 lock_vars[0].name = lock_name;
226 snprintf(lock_name, MAX_STRING_SIZE, "%s/resource_count", ns->ns_name);
227 lock_vars[0].data = &ns->ns_refcount;
228 lock_vars[0].read_fptr = lprocfs_rd_atomic;
229 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
231 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_count", ns->ns_name);
232 lock_vars[0].data = &ns->ns_locks;
233 lock_vars[0].read_fptr = lprocfs_rd_atomic;
234 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
236 if (ns_is_client(ns)) {
237 snprintf(lock_name, MAX_STRING_SIZE, "%s/lock_unused_count",
239 lock_vars[0].data = &ns->ns_nr_unused;
240 lock_vars[0].read_fptr = lprocfs_rd_uint;
241 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
243 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_size",
245 lock_vars[0].data = ns;
246 lock_vars[0].read_fptr = lprocfs_rd_lru_size;
247 lock_vars[0].write_fptr = lprocfs_wr_lru_size;
248 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
250 snprintf(lock_name, MAX_STRING_SIZE, "%s/shrink_thumb",
252 lock_vars[0].data = ns;
253 lock_vars[0].read_fptr = lprocfs_rd_uint;
254 lock_vars[0].write_fptr = lprocfs_wr_uint;
255 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
257 snprintf(lock_name, MAX_STRING_SIZE, "%s/lru_max_age",
259 lock_vars[0].data = &ns->ns_max_age;
260 lock_vars[0].read_fptr = lprocfs_rd_uint;
261 lock_vars[0].write_fptr = lprocfs_wr_uint;
262 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
264 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
266 lock_vars[0].data = &ns->ns_ctime_age_limit;
267 lock_vars[0].read_fptr = lprocfs_rd_uint;
268 lock_vars[0].write_fptr = lprocfs_wr_uint;
269 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
271 snprintf(lock_name, MAX_STRING_SIZE, "%s/max_nolock_bytes",
273 lock_vars[0].data = &ns->ns_max_nolock_size;
274 lock_vars[0].read_fptr = lprocfs_rd_uint;
275 lock_vars[0].write_fptr = lprocfs_wr_uint;
276 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
278 snprintf(lock_name, MAX_STRING_SIZE, "%s/contention_seconds",
280 lock_vars[0].data = &ns->ns_contention_time;
281 lock_vars[0].read_fptr = lprocfs_rd_uint;
282 lock_vars[0].write_fptr = lprocfs_wr_uint;
283 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
285 snprintf(lock_name, MAX_STRING_SIZE, "%s/contended_locks",
287 lock_vars[0].data = &ns->ns_contended_locks;
288 lock_vars[0].read_fptr = lprocfs_rd_uint;
289 lock_vars[0].write_fptr = lprocfs_wr_uint;
290 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
293 #undef MAX_STRING_SIZE
295 #define ldlm_proc_namespace(ns) do {} while (0)
298 struct ldlm_namespace *ldlm_namespace_new(char *name, ldlm_side_t client,
301 struct ldlm_namespace *ns = NULL;
302 struct list_head *bucket;
303 int rc, idx, namelen;
308 CERROR("ldlm_get_ref failed: %d\n", rc);
316 OBD_VMALLOC(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
320 ns->ns_shrink_thumb = LDLM_LOCK_SHRINK_THUMB;
321 ns->ns_appetite = apt;
322 namelen = strlen(name);
323 OBD_ALLOC(ns->ns_name, namelen + 1);
325 GOTO(out_hash, NULL);
327 strcpy(ns->ns_name, name);
329 CFS_INIT_LIST_HEAD(&ns->ns_root_list);
331 ns->ns_client = client;
332 spin_lock_init(&ns->ns_hash_lock);
333 atomic_set(&ns->ns_locks, 0);
334 ns->ns_resources = 0;
335 cfs_waitq_init(&ns->ns_waitq);
336 ns->ns_max_nolock_size = NS_DEFAULT_MAX_NOLOCK_BYTES;
337 ns->ns_contention_time = NS_DEFAULT_CONTENTION_SECONDS;
338 ns->ns_contended_locks = NS_DEFAULT_CONTENDED_LOCKS;
340 for (bucket = ns->ns_hash + RES_HASH_SIZE - 1; bucket >= ns->ns_hash;
342 CFS_INIT_LIST_HEAD(bucket);
344 CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
345 ns->ns_nr_unused = 0;
346 ns->ns_max_unused = LDLM_DEFAULT_LRU_SIZE;
347 ns->ns_max_age = LDLM_DEFAULT_MAX_ALIVE;
348 ns->ns_ctime_age_limit = LDLM_CTIME_AGE_LIMIT;
349 spin_lock_init(&ns->ns_unused_lock);
350 ns->ns_orig_connect_flags = 0;
351 ns->ns_connect_flags = 0;
352 ldlm_proc_namespace(ns);
354 idx = atomic_read(ldlm_namespace_nr(client));
355 rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
357 CERROR("Can't initialize lock pool, rc %d\n", rc);
361 mutex_down(ldlm_namespace_lock(client));
362 list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
363 atomic_inc(ldlm_namespace_nr(client));
364 mutex_up(ldlm_namespace_lock(client));
368 ldlm_namespace_cleanup(ns, 0);
369 OBD_FREE(ns->ns_name, namelen + 1);
371 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
379 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
381 /* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
382 * This is currently only used for recovery, and we make certain assumptions
383 * as a result--notably, that we shouldn't cancel locks with refs. -phil
385 * Called with the ns_lock held. */
386 static void cleanup_resource(struct ldlm_resource *res, struct list_head *q,
389 struct list_head *tmp;
390 int rc = 0, client = ns_is_client(res->lr_namespace);
391 int local_only = (flags & LDLM_FL_LOCAL_ONLY);
396 struct ldlm_lock *lock = NULL;
398 /* first, we look for non-cleaned-yet lock
399 * all cleaned locks are marked by CLEANED flag */
401 list_for_each(tmp, q) {
402 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
403 if (lock->l_flags & LDLM_FL_CLEANED) {
408 lock->l_flags |= LDLM_FL_CLEANED;
417 /* Set CBPENDING so nothing in the cancellation path
418 * can match this lock */
419 lock->l_flags |= LDLM_FL_CBPENDING;
420 lock->l_flags |= LDLM_FL_FAILED;
421 lock->l_flags |= flags;
423 /* ... without sending a CANCEL message for local_only. */
425 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
427 if (local_only && (lock->l_readers || lock->l_writers)) {
428 /* This is a little bit gross, but much better than the
429 * alternative: pretend that we got a blocking AST from
430 * the server, so that when the lock is decref'd, it
431 * will go away ... */
433 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
434 if (lock->l_completion_ast)
435 lock->l_completion_ast(lock, 0, NULL);
441 struct lustre_handle lockh;
444 ldlm_lock2handle(lock, &lockh);
445 rc = ldlm_cli_cancel(&lockh);
447 CERROR("ldlm_cli_cancel: %d\n", rc);
449 ldlm_resource_unlink_lock(lock);
451 LDLM_DEBUG(lock, "Freeing a lock still held by a "
453 ldlm_lock_destroy(lock);
461 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
463 struct list_head *tmp;
467 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
471 for (i = 0; i < RES_HASH_SIZE; i++) {
472 spin_lock(&ns->ns_hash_lock);
473 tmp = ns->ns_hash[i].next;
474 while (tmp != &(ns->ns_hash[i])) {
475 struct ldlm_resource *res;
476 res = list_entry(tmp, struct ldlm_resource, lr_hash);
477 ldlm_resource_getref(res);
478 spin_unlock(&ns->ns_hash_lock);
480 cleanup_resource(res, &res->lr_granted, flags);
481 cleanup_resource(res, &res->lr_converting, flags);
482 cleanup_resource(res, &res->lr_waiting, flags);
484 spin_lock(&ns->ns_hash_lock);
487 /* XXX: former stuff caused issues in case of race
488 * between ldlm_namespace_cleanup() and lockd() when
489 * client gets blocking ast when lock gets distracted by
490 * server. This is 1_4 branch solution, let's see how
492 if (!ldlm_resource_putref_locked(res))
494 "Namespace %s resource refcount nonzero "
495 "(%d) after lock cleanup; forcing cleanup.\n",
496 ns->ns_name, atomic_read(&res->lr_refcount));
498 spin_unlock(&ns->ns_hash_lock);
504 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
508 /* At shutdown time, don't call the cancellation callback */
509 ldlm_namespace_cleanup(ns, force ? LDLM_FL_LOCAL_ONLY : 0);
511 if (ns->ns_refcount > 0) {
512 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
515 "dlm namespace %s free waiting on refcount %d\n",
516 ns->ns_name, ns->ns_refcount);
519 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
521 rc = l_wait_event(ns->ns_waitq,
522 ns->ns_refcount == 0, &lwi);
524 /* Forced cleanups should be able to reclaim all references,
525 * so it's safe to wait forever... we can't leak locks... */
526 if (force && rc == -ETIMEDOUT) {
527 LCONSOLE_ERROR("Forced cleanup waiting for %s "
528 "namespace with %d resources in use, "
529 "(rc=%d)\n", ns->ns_name,
530 ns->ns_refcount, rc);
531 GOTO(force_wait, rc);
534 if (ns->ns_refcount) {
535 LCONSOLE_ERROR("Cleanup waiting for %s namespace "
536 "with %d resources in use, (rc=%d)\n",
538 ns->ns_refcount, rc);
539 RETURN(ELDLM_NAMESPACE_EXISTS);
542 "dlm namespace %s free done waiting\n", ns->ns_name);
548 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
549 struct obd_import *imp,
559 /* Can fail with -EINTR when force == 0 in which case try harder */
560 rc = __ldlm_namespace_free(ns, force);
561 if (rc != ELDLM_OK) {
563 ptlrpc_disconnect_import(imp, 0);
564 ptlrpc_invalidate_import(imp);
567 /* With all requests dropped and the import inactive
568 * we are gaurenteed all reference will be dropped. */
569 rc = __ldlm_namespace_free(ns, 1);
575 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
583 mutex_down(ldlm_namespace_lock(ns->ns_client));
585 * Some asserts and possibly other parts of code still using
586 * list_empty(&ns->ns_list_chain). This is why it is important
587 * to use list_del_init() here.
589 list_del_init(&ns->ns_list_chain);
590 atomic_dec(ldlm_namespace_nr(ns->ns_client));
591 ldlm_pool_fini(&ns->ns_pool);
592 mutex_up(ldlm_namespace_lock(ns->ns_client));
595 struct proc_dir_entry *dir;
596 dir = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
598 CERROR("dlm namespace %s has no procfs dir?\n",
601 lprocfs_remove(&dir);
606 OBD_VFREE(ns->ns_hash, sizeof(*ns->ns_hash) * RES_HASH_SIZE);
607 OBD_FREE(ns->ns_name, strlen(ns->ns_name) + 1);
609 * @ns should be not on list in this time, otherwise this will cause
610 * issues realted to using freed @ns in pools thread.
612 LASSERT(list_empty(&ns->ns_list_chain));
619 /* Cleanup the resource, and free namespace.
622 * proc1: destroy import
623 * class_disconnect_export(grab cl_sem) ->
624 * -> ldlm_namespace_free ->
625 * -> lprocfs_remove(grab _lprocfs_lock).
626 * proc2: read proc info
627 * lprocfs_fops_read(grab _lprocfs_lock) ->
628 * -> osc_rd_active, etc(grab cl_sem).
630 * So that I have to split the ldlm_namespace_free into two parts - the first
631 * part ldlm_namespace_free_prior is used to cleanup the resource which is
632 * being used; the 2nd part ldlm_namespace_free_post is used to unregister the
633 * lprocfs entries, and then free memory. It will be called w/o cli->cl_sem
636 void ldlm_namespace_free(struct ldlm_namespace *ns,
637 struct obd_import *imp,
640 ldlm_namespace_free_prior(ns, imp, force);
641 ldlm_namespace_free_post(ns);
645 void ldlm_namespace_get_nolock(struct ldlm_namespace *ns)
647 LASSERT(ns->ns_refcount >= 0);
651 void ldlm_namespace_get(struct ldlm_namespace *ns)
653 spin_lock(&ns->ns_hash_lock);
654 ldlm_namespace_get_nolock(ns);
655 spin_unlock(&ns->ns_hash_lock);
658 void ldlm_namespace_put_nolock(struct ldlm_namespace *ns, int wakeup)
660 LASSERT(ns->ns_refcount > 0);
662 if (ns->ns_refcount == 0 && wakeup)
663 wake_up(&ns->ns_waitq);
666 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup)
668 spin_lock(&ns->ns_hash_lock);
669 ldlm_namespace_put_nolock(ns, wakeup);
670 spin_unlock(&ns->ns_hash_lock);
673 /* Should be called under ldlm_namespace_lock(client) taken */
674 void ldlm_namespace_move(struct ldlm_namespace *ns, ldlm_side_t client)
676 LASSERT(!list_empty(&ns->ns_list_chain));
677 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
678 list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
681 /* Should be called under ldlm_namespace_lock(client) taken */
682 struct ldlm_namespace *ldlm_namespace_first(ldlm_side_t client)
684 LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
685 LASSERT(!list_empty(ldlm_namespace_list(client)));
686 return container_of(ldlm_namespace_list(client)->next,
687 struct ldlm_namespace, ns_list_chain);
689 static __u32 ldlm_hash_fn(struct ldlm_resource *parent,
690 const struct ldlm_res_id *name)
695 for (i = 0; i < RES_NAME_SIZE; i++)
696 hash += name->name[i];
698 hash += (__u32)((unsigned long)parent >> 4);
700 return (hash & RES_HASH_MASK);
703 static struct ldlm_resource *ldlm_resource_new(void)
705 struct ldlm_resource *res;
708 OBD_SLAB_ALLOC(res, ldlm_resource_slab, CFS_ALLOC_IO, sizeof *res);
712 memset(res, 0, sizeof(*res));
714 CFS_INIT_LIST_HEAD(&res->lr_children);
715 CFS_INIT_LIST_HEAD(&res->lr_childof);
716 CFS_INIT_LIST_HEAD(&res->lr_granted);
717 CFS_INIT_LIST_HEAD(&res->lr_converting);
718 CFS_INIT_LIST_HEAD(&res->lr_waiting);
720 /* initialize interval trees for each lock mode*/
721 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
722 res->lr_itree[idx].lit_size = 0;
723 res->lr_itree[idx].lit_mode = 1 << idx;
724 res->lr_itree[idx].lit_root = NULL;
727 atomic_set(&res->lr_refcount, 1);
728 spin_lock_init(&res->lr_lock);
730 /* one who creates the resource must unlock
731 * the semaphore after lvb initialization */
732 init_MUTEX_LOCKED(&res->lr_lvb_sem);
737 /* must be called with hash lock held */
738 static struct ldlm_resource *
739 ldlm_resource_find(struct ldlm_namespace *ns, const struct ldlm_res_id *name,
742 struct list_head *bucket, *tmp;
743 struct ldlm_resource *res;
745 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
746 bucket = ns->ns_hash + hash;
748 list_for_each(tmp, bucket) {
749 res = list_entry(tmp, struct ldlm_resource, lr_hash);
750 if (memcmp(&res->lr_name, name, sizeof(res->lr_name)) == 0)
757 /* Args: locked namespace
758 * Returns: newly-allocated, referenced, unlocked resource */
759 static struct ldlm_resource *
760 ldlm_resource_add(struct ldlm_namespace *ns, struct ldlm_resource *parent,
761 const struct ldlm_res_id *name, __u32 hash, ldlm_type_t type)
763 struct list_head *bucket;
764 struct ldlm_resource *res, *old_res;
767 LASSERTF(type >= LDLM_MIN_TYPE && type < LDLM_MAX_TYPE,
770 res = ldlm_resource_new();
774 res->lr_name = *name;
775 res->lr_namespace = ns;
777 res->lr_most_restr = LCK_NL;
779 spin_lock(&ns->ns_hash_lock);
780 old_res = ldlm_resource_find(ns, name, hash);
782 /* someone won the race and added the resource before */
783 ldlm_resource_getref(old_res);
784 spin_unlock(&ns->ns_hash_lock);
785 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
786 /* synchronize WRT resource creation */
787 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
788 down(&old_res->lr_lvb_sem);
789 up(&old_res->lr_lvb_sem);
794 /* we won! let's add the resource */
795 bucket = ns->ns_hash + hash;
796 list_add(&res->lr_hash, bucket);
798 ldlm_namespace_get_nolock(ns);
800 if (parent == NULL) {
801 list_add(&res->lr_childof, &ns->ns_root_list);
803 res->lr_parent = parent;
804 list_add(&res->lr_childof, &parent->lr_children);
806 spin_unlock(&ns->ns_hash_lock);
808 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
811 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
812 rc = ns->ns_lvbo->lvbo_init(res);
814 CERROR("lvbo_init failed for resource "
815 LPU64": rc %d\n", name->name[0], rc);
816 /* we create resource with locked lr_lvb_sem */
817 up(&res->lr_lvb_sem);
823 /* Args: unlocked namespace
824 * Locks: takes and releases ns->ns_lock and res->lr_lock
825 * Returns: referenced, unlocked ldlm_resource or NULL */
826 struct ldlm_resource *
827 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
828 const struct ldlm_res_id *name, ldlm_type_t type, int create)
830 __u32 hash = ldlm_hash_fn(parent, name);
831 struct ldlm_resource *res = NULL;
835 LASSERT(ns->ns_hash != NULL);
836 LASSERT(name->name[0] != 0);
838 spin_lock(&ns->ns_hash_lock);
839 res = ldlm_resource_find(ns, name, hash);
841 ldlm_resource_getref(res);
842 spin_unlock(&ns->ns_hash_lock);
843 /* synchronize WRT resource creation */
844 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
845 down(&res->lr_lvb_sem);
846 up(&res->lr_lvb_sem);
850 spin_unlock(&ns->ns_hash_lock);
855 res = ldlm_resource_add(ns, parent, name, hash, type);
859 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
861 LASSERT(res != NULL);
862 LASSERT(res != LP_POISON);
863 atomic_inc(&res->lr_refcount);
864 CDEBUG(D_INFO, "getref res: %p count: %d\n", res,
865 atomic_read(&res->lr_refcount));
869 void __ldlm_resource_putref_final(struct ldlm_resource *res)
871 struct ldlm_namespace *ns = res->lr_namespace;
873 LASSERT_SPIN_LOCKED(&ns->ns_hash_lock);
875 if (!list_empty(&res->lr_granted)) {
876 ldlm_resource_dump(D_ERROR, res);
880 if (!list_empty(&res->lr_converting)) {
881 ldlm_resource_dump(D_ERROR, res);
885 if (!list_empty(&res->lr_waiting)) {
886 ldlm_resource_dump(D_ERROR, res);
890 if (!list_empty(&res->lr_children)) {
891 ldlm_resource_dump(D_ERROR, res);
895 /* Pass 0 here to not wake ->ns_waitq up yet, we will do it few
896 * lines below when all children are freed. */
897 ldlm_namespace_put_nolock(ns, 0);
898 list_del_init(&res->lr_hash);
899 list_del_init(&res->lr_childof);
902 if (ns->ns_resources == 0)
903 wake_up(&ns->ns_waitq);
906 /* Returns 1 if the resource was freed, 0 if it remains. */
907 int ldlm_resource_putref(struct ldlm_resource *res)
909 struct ldlm_namespace *ns = res->lr_namespace;
913 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
914 atomic_read(&res->lr_refcount) - 1);
915 LASSERTF(atomic_read(&res->lr_refcount) > 0, "%d",
916 atomic_read(&res->lr_refcount));
917 LASSERTF(atomic_read(&res->lr_refcount) < LI_POISON, "%d",
918 atomic_read(&res->lr_refcount));
920 if (atomic_dec_and_lock(&res->lr_refcount, &ns->ns_hash_lock)) {
921 __ldlm_resource_putref_final(res);
922 spin_unlock(&ns->ns_hash_lock);
923 if (res->lr_lvb_data)
924 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
925 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
932 /* Returns 1 if the resource was freed, 0 if it remains. */
933 int ldlm_resource_putref_locked(struct ldlm_resource *res)
938 CDEBUG(D_INFO, "putref res: %p count: %d\n", res,
939 atomic_read(&res->lr_refcount) - 1);
940 LASSERT(atomic_read(&res->lr_refcount) > 0);
941 LASSERT(atomic_read(&res->lr_refcount) < LI_POISON);
943 LASSERT(atomic_read(&res->lr_refcount) >= 0);
944 if (atomic_dec_and_test(&res->lr_refcount)) {
945 __ldlm_resource_putref_final(res);
946 if (res->lr_lvb_data)
947 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
948 OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
955 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
956 struct ldlm_lock *lock)
958 check_res_locked(res);
960 ldlm_resource_dump(D_OTHER, res);
961 CDEBUG(D_OTHER, "About to add this lock:\n");
962 ldlm_lock_dump(D_OTHER, lock, 0);
964 if (lock->l_destroyed) {
965 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
969 LASSERT(list_empty(&lock->l_res_link));
971 list_add_tail(&lock->l_res_link, head);
974 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
975 struct ldlm_lock *new)
977 struct ldlm_resource *res = original->l_resource;
979 check_res_locked(res);
981 ldlm_resource_dump(D_OTHER, res);
982 CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
983 ldlm_lock_dump(D_OTHER, new, 0);
985 if (new->l_destroyed) {
986 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
990 LASSERT(list_empty(&new->l_res_link));
992 list_add(&new->l_res_link, &original->l_res_link);
996 void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
998 int type = lock->l_resource->lr_type;
1000 check_res_locked(lock->l_resource);
1001 if (type == LDLM_IBITS || type == LDLM_PLAIN)
1002 ldlm_unlink_lock_skiplist(lock);
1003 else if (type == LDLM_EXTENT)
1004 ldlm_extent_unlink_lock(lock);
1005 list_del_init(&lock->l_res_link);
1008 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
1010 desc->lr_type = res->lr_type;
1011 desc->lr_name = res->lr_name;
1014 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
1016 struct list_head *tmp;
1018 if (!((libcfs_debug | D_ERROR) & level))
1021 mutex_down(ldlm_namespace_lock(client));
1023 list_for_each(tmp, ldlm_namespace_list(client)) {
1024 struct ldlm_namespace *ns;
1025 ns = list_entry(tmp, struct ldlm_namespace, ns_list_chain);
1026 ldlm_namespace_dump(level, ns);
1029 mutex_up(ldlm_namespace_lock(client));
1032 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
1034 struct list_head *tmp;
1036 if (!((libcfs_debug | D_ERROR) & level))
1039 CDEBUG(level, "--- Namespace: %s (rc: %d, side: %s)\n",
1040 ns->ns_name, ns->ns_refcount,
1041 ns_is_client(ns) ? "client" : "server");
1043 if (cfs_time_before(cfs_time_current(), ns->ns_next_dump))
1046 spin_lock(&ns->ns_hash_lock);
1047 tmp = ns->ns_root_list.next;
1048 while (tmp != &ns->ns_root_list) {
1049 struct ldlm_resource *res;
1050 res = list_entry(tmp, struct ldlm_resource, lr_childof);
1052 ldlm_resource_getref(res);
1053 spin_unlock(&ns->ns_hash_lock);
1056 ldlm_resource_dump(level, res);
1059 spin_lock(&ns->ns_hash_lock);
1061 ldlm_resource_putref_locked(res);
1063 ns->ns_next_dump = cfs_time_shift(10);
1064 spin_unlock(&ns->ns_hash_lock);
1067 void ldlm_resource_dump(int level, struct ldlm_resource *res)
1069 struct list_head *tmp;
1072 CLASSERT(RES_NAME_SIZE == 4);
1074 if (!((libcfs_debug | D_ERROR) & level))
1077 CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
1078 ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
1079 res->lr_name.name[2], res->lr_name.name[3],
1080 atomic_read(&res->lr_refcount));
1082 if (!list_empty(&res->lr_granted)) {
1084 CDEBUG(level, "Granted locks:\n");
1085 list_for_each(tmp, &res->lr_granted) {
1086 struct ldlm_lock *lock;
1087 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1088 ldlm_lock_dump(level, lock, ++pos);
1091 if (!list_empty(&res->lr_converting)) {
1093 CDEBUG(level, "Converting locks:\n");
1094 list_for_each(tmp, &res->lr_converting) {
1095 struct ldlm_lock *lock;
1096 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1097 ldlm_lock_dump(level, lock, ++pos);
1100 if (!list_empty(&res->lr_waiting)) {
1102 CDEBUG(level, "Waiting locks:\n");
1103 list_for_each(tmp, &res->lr_waiting) {
1104 struct ldlm_lock *lock;
1105 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1106 ldlm_lock_dump(level, lock, ++pos);