Whamcloud - gitweb
LU-2901 ldlm: fix resource/fid check, use DLDLMRES
[fs/lustre-release.git] / lustre / ldlm / ldlm_resource.c
index cbb0ac9..b86c66d 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -29,7 +27,7 @@
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2010, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <obd_class.h>
 #include "ldlm_internal.h"
 
-cfs_mem_cache_t *ldlm_resource_slab, *ldlm_lock_slab;
+struct kmem_cache *ldlm_resource_slab, *ldlm_lock_slab;
 
-cfs_atomic_t ldlm_srv_namespace_nr = CFS_ATOMIC_INIT(0);
-cfs_atomic_t ldlm_cli_namespace_nr = CFS_ATOMIC_INIT(0);
+int ldlm_srv_namespace_nr = 0;
+int ldlm_cli_namespace_nr = 0;
 
-cfs_semaphore_t ldlm_srv_namespace_lock;
+struct mutex ldlm_srv_namespace_lock;
 CFS_LIST_HEAD(ldlm_srv_namespace_list);
 
-cfs_semaphore_t ldlm_cli_namespace_lock;
-CFS_LIST_HEAD(ldlm_cli_namespace_list);
+struct mutex ldlm_cli_namespace_lock;
+/* Client Namespaces that have active resources in them.
+ * Once all resources go away, ldlm_poold moves such namespaces to the
+ * inactive list */
+CFS_LIST_HEAD(ldlm_cli_active_namespace_list);
+/* Client namespaces that don't have any locks in them */
+CFS_LIST_HEAD(ldlm_cli_inactive_namespace_list);
 
 cfs_proc_dir_entry_t *ldlm_type_proc_dir = NULL;
 cfs_proc_dir_entry_t *ldlm_ns_proc_dir = NULL;
@@ -69,6 +72,10 @@ cfs_proc_dir_entry_t *ldlm_svc_proc_dir = NULL;
 
 extern unsigned int ldlm_cancel_unused_locks_before_replay;
 
+/* during debug dump certain amount of granted locks for one resource to avoid
+ * DDOS. */
+unsigned int ldlm_dump_granted_max = 256;
+
 #ifdef LPROCFS
 static int ldlm_proc_dump_ns(struct file *file, const char *buffer,
                              unsigned long count, void *data)
@@ -83,6 +90,9 @@ int ldlm_proc_setup(void)
         int rc;
         struct lprocfs_vars list[] = {
                 { "dump_namespaces", NULL, ldlm_proc_dump_ns, NULL },
+                { "dump_granted_max",
+                  lprocfs_rd_uint, lprocfs_wr_uint,
+                  &ldlm_dump_granted_max, NULL },
                 { "cancel_unused_locks_before_replay",
                   lprocfs_rd_uint, lprocfs_wr_uint,
                   &ldlm_cancel_unused_locks_before_replay, NULL },
@@ -187,7 +197,7 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
         int lru_resize;
 
         dummy[MAX_STRING_SIZE] = '\0';
-        if (cfs_copy_from_user(dummy, buffer, MAX_STRING_SIZE))
+       if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
                 return -EFAULT;
 
         if (strncmp(dummy, "clear", 5) == 0) {
@@ -198,8 +208,8 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
                         int canceled, unused  = ns->ns_nr_unused;
 
                         /* Try to cancel all @ns_nr_unused locks. */
-                        canceled = ldlm_cancel_lru(ns, unused, LDLM_SYNC,
-                                                   LDLM_CANCEL_PASSED);
+                       canceled = ldlm_cancel_lru(ns, unused, 0,
+                                                  LDLM_CANCEL_PASSED);
                         if (canceled < unused) {
                                 CDEBUG(D_DLMTRACE,
                                        "not all requested locks are canceled, "
@@ -210,7 +220,7 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
                 } else {
                         tmp = ns->ns_max_unused;
                         ns->ns_max_unused = 0;
-                        ldlm_cancel_lru(ns, 0, LDLM_SYNC, LDLM_CANCEL_PASSED);
+                       ldlm_cancel_lru(ns, 0, 0, LDLM_CANCEL_PASSED);
                         ns->ns_max_unused = tmp;
                 }
                 return count;
@@ -235,7 +245,7 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
                        "changing namespace %s unused locks from %u to %u\n",
                        ldlm_ns_name(ns), ns->ns_nr_unused,
                        (unsigned int)tmp);
-                ldlm_cancel_lru(ns, tmp, LDLM_ASYNC, LDLM_CANCEL_PASSED);
+               ldlm_cancel_lru(ns, tmp, LCF_ASYNC, LDLM_CANCEL_PASSED);
 
                 if (!lru_resize) {
                         CDEBUG(D_DLMTRACE,
@@ -249,10 +259,10 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
                        ldlm_ns_name(ns), ns->ns_max_unused,
                        (unsigned int)tmp);
                 ns->ns_max_unused = (unsigned int)tmp;
-                ldlm_cancel_lru(ns, 0, LDLM_ASYNC, LDLM_CANCEL_PASSED);
+               ldlm_cancel_lru(ns, 0, LCF_ASYNC, LDLM_CANCEL_PASSED);
 
-                /* Make sure that originally lru resize was supported before
-                 * turning it on here. */
+               /* Make sure that LRU resize was originally supported before
+                * turning it on here. */
                 if (lru_resize &&
                     (ns->ns_orig_connect_flags & OBD_CONNECT_LRU_RESIZE)) {
                         CDEBUG(D_DLMTRACE,
@@ -265,6 +275,33 @@ static int lprocfs_wr_lru_size(struct file *file, const char *buffer,
         return count;
 }
 
+static int lprocfs_rd_elc(char *page, char **start, off_t off,
+                         int count, int *eof, void *data)
+{
+       struct ldlm_namespace *ns = data;
+       unsigned int supp = ns_connect_cancelset(ns);
+
+       return lprocfs_rd_uint(page, start, off, count, eof, &supp);
+}
+
+static int lprocfs_wr_elc(struct file *file, const char *buffer,
+                              unsigned long count, void *data)
+{
+       struct ldlm_namespace *ns = data;
+       unsigned int supp = -1;
+       int rc;
+
+       rc = lprocfs_wr_uint(file, buffer, count, &supp);
+       if (rc < 0)
+               return rc;
+
+       if (supp == 0)
+               ns->ns_connect_flags &= ~OBD_CONNECT_CANCELSET;
+       else if (ns->ns_orig_connect_flags & OBD_CONNECT_CANCELSET)
+               ns->ns_connect_flags |= OBD_CONNECT_CANCELSET;
+       return count;
+}
+
 void ldlm_namespace_proc_unregister(struct ldlm_namespace *ns)
 {
         struct proc_dir_entry *dir;
@@ -333,6 +370,13 @@ int ldlm_namespace_proc_register(struct ldlm_namespace *ns)
                 lock_vars[0].read_fptr = lprocfs_rd_uint;
                 lock_vars[0].write_fptr = lprocfs_wr_uint;
                 lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
+
+               snprintf(lock_name, MAX_STRING_SIZE, "%s/early_lock_cancel",
+                        ldlm_ns_name(ns));
+               lock_vars[0].data = ns;
+               lock_vars[0].read_fptr = lprocfs_rd_elc;
+               lock_vars[0].write_fptr = lprocfs_wr_elc;
+               lprocfs_add_vars(ldlm_ns_proc_dir, lock_vars, 0);
         } else {
                 snprintf(lock_name, MAX_STRING_SIZE, "%s/ctime_age_limit",
                          ldlm_ns_name(ns));
@@ -406,8 +450,8 @@ static unsigned ldlm_res_hop_fid_hash(cfs_hash_t *hs,
         __u32               val;
 
         fid.f_seq = id->name[LUSTRE_RES_ID_SEQ_OFF];
-        fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_OID_OFF];
-        fid.f_ver = (__u32)id->name[LUSTRE_RES_ID_VER_OFF];
+        fid.f_oid = (__u32)id->name[LUSTRE_RES_ID_VER_OID_OFF];
+        fid.f_ver = (__u32)(id->name[LUSTRE_RES_ID_VER_OID_OFF] >> 32);
 
         hash = fid_flatten32(&fid);
         hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
@@ -455,7 +499,6 @@ static void ldlm_res_hop_get_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
 
         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
         ldlm_resource_getref(res);
-        LDLM_RESOURCE_ADDREF(res);
 }
 
 static void ldlm_res_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
@@ -464,7 +507,6 @@ static void ldlm_res_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
 
         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
         /* cfs_hash_for_each_nolock is the only chance we call it */
-        LDLM_RESOURCE_DELREF(res);
         ldlm_resource_putref_locked(res);
 }
 
@@ -473,7 +515,6 @@ static void ldlm_res_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
         struct ldlm_resource *res;
 
         res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
-        LDLM_RESOURCE_DELREF(res);
         ldlm_resource_putref(res);
 }
 
@@ -552,6 +593,9 @@ ldlm_ns_hash_def_t ldlm_ns_hash_defs[] =
         },
 };
 
+/**
+ * Create and initialize new empty namespace.
+ */
 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
                                           ldlm_side_t client,
                                           ldlm_appetite_t apt,
@@ -613,7 +657,7 @@ struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
 
         CFS_INIT_LIST_HEAD(&ns->ns_list_chain);
         CFS_INIT_LIST_HEAD(&ns->ns_unused_list);
-        cfs_spin_lock_init(&ns->ns_lock);
+       spin_lock_init(&ns->ns_lock);
         cfs_atomic_set(&ns->ns_bref, 0);
         cfs_waitq_init(&ns->ns_waitq);
 
@@ -636,7 +680,7 @@ struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
                 GOTO(out_hash, rc);
         }
 
-        idx = cfs_atomic_read(ldlm_namespace_nr(client));
+        idx = ldlm_namespace_nr_read(client);
         rc = ldlm_pool_init(&ns->ns_pool, ns, idx, client);
         if (rc) {
                 CERROR("Can't initialize lock pool, rc %d\n", rc);
@@ -656,24 +700,30 @@ out_ref:
         ldlm_put_ref();
         RETURN(NULL);
 }
+EXPORT_SYMBOL(ldlm_namespace_new);
 
 extern struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
 
-/* If flags contains FL_LOCAL_ONLY, don't try to tell the server, just cleanup.
- * This is currently only used for recovery, and we make certain assumptions
- * as a result--notably, that we shouldn't cancel locks with refs. -phil */
+/**
+ * Cancel and destroy all locks on a resource.
+ *
+ * If flags contains FL_LOCAL_ONLY, don't try to tell the server, just
+ * clean up.  This is currently only used for recovery, and we make
+ * certain assumptions as a result--notably, that we shouldn't cancel
+ * locks with refs.
+ */
 static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
-                             int flags)
+                            __u64 flags)
 {
-        cfs_list_t *tmp;
-        int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
-        int local_only = (flags & LDLM_FL_LOCAL_ONLY);
+       cfs_list_t *tmp;
+       int rc = 0, client = ns_is_client(ldlm_res_to_ns(res));
+       bool local_only = !!(flags & LDLM_FL_LOCAL_ONLY);
 
         do {
                 struct ldlm_lock *lock = NULL;
 
-                /* first, we look for non-cleaned-yet lock
-                 * all cleaned locks are marked by CLEANED flag */
+               /* First, we look for non-cleaned-yet lock
+                * all cleaned locks are marked by CLEANED flag. */
                 lock_res(res);
                 cfs_list_for_each(tmp, q) {
                         lock = cfs_list_entry(tmp, struct ldlm_lock,
@@ -693,7 +743,7 @@ static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
                 }
 
                 /* Set CBPENDING so nothing in the cancellation path
-                 * can match this lock */
+                * can match this lock. */
                 lock->l_flags |= LDLM_FL_CBPENDING;
                 lock->l_flags |= LDLM_FL_FAILED;
                 lock->l_flags |= flags;
@@ -720,7 +770,7 @@ static void cleanup_resource(struct ldlm_resource *res, cfs_list_t *q,
 
                         unlock_res(res);
                         ldlm_lock2handle(lock, &lockh);
-                        rc = ldlm_cli_cancel(&lockh);
+                       rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
                         if (rc)
                                 CERROR("ldlm_cli_cancel: %d\n", rc);
                 } else {
@@ -738,7 +788,7 @@ static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
                                cfs_hlist_node_t *hnode, void *arg)
 {
         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
-        int    flags = (int)(unsigned long)arg;
+       __u64 flags = *(__u64 *)arg;
 
         cleanup_resource(res, &res->lr_granted, flags);
         cleanup_resource(res, &res->lr_converting, flags);
@@ -748,37 +798,46 @@ static int ldlm_resource_clean(cfs_hash_t *hs, cfs_hash_bd_t *bd,
 }
 
 static int ldlm_resource_complain(cfs_hash_t *hs, cfs_hash_bd_t *bd,
-                                  cfs_hlist_node_t *hnode, void *arg)
+                                 cfs_hlist_node_t *hnode, void *arg)
 {
-        struct ldlm_resource  *res = cfs_hash_object(hs, hnode);
-
-        CERROR("Namespace %s resource refcount nonzero "
-               "(%d) after lock cleanup; forcing "
-               "cleanup.\n",
-               ldlm_ns_name(ldlm_res_to_ns(res)),
-               cfs_atomic_read(&res->lr_refcount) - 1);
-
-        CERROR("Resource: %p ("LPU64"/"LPU64"/"LPU64"/"
-               LPU64") (rc: %d)\n", res,
-               res->lr_name.name[0], res->lr_name.name[1],
-               res->lr_name.name[2], res->lr_name.name[3],
-               cfs_atomic_read(&res->lr_refcount) - 1);
-        return 0;
+       struct ldlm_resource  *res = cfs_hash_object(hs, hnode);
+
+       lock_res(res);
+       CERROR("%s: namespace resource "DLDLMRES" (%p) refcount nonzero "
+              "(%d) after lock cleanup; forcing cleanup.\n",
+              ldlm_ns_name(ldlm_res_to_ns(res)), PLDLMRES(res), res,
+              cfs_atomic_read(&res->lr_refcount) - 1);
+
+       ldlm_resource_dump(D_ERROR, res);
+       unlock_res(res);
+       return 0;
 }
 
-int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags)
+/**
+ * Cancel and destroy all locks in the namespace.
+ *
+ * Typically used during evictions when server notified client that it was
+ * evicted and all of its state needs to be destroyed.
+ * Also used during shutdown.
+ */
+int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags)
 {
         if (ns == NULL) {
                 CDEBUG(D_INFO, "NULL ns, skipping cleanup\n");
                 return ELDLM_OK;
         }
 
-        cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean,
-                                 (void *)(unsigned long)flags);
+       cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_clean, &flags);
         cfs_hash_for_each_nolock(ns->ns_rs_hash, ldlm_resource_complain, NULL);
         return ELDLM_OK;
 }
+EXPORT_SYMBOL(ldlm_namespace_cleanup);
 
+/**
+ * Attempts to free namespace.
+ *
+ * Only used when namespace goes away, like during an unmount.
+ */
 static int __ldlm_namespace_free(struct ldlm_namespace *ns, int force)
 {
         ENTRY;
@@ -824,12 +883,12 @@ force_wait:
 }
 
 /**
- * Performs various cleanups for passed \a ns to make it drop refc and be ready
- * for freeing. Waits for refc == 0.
+ * Performs various cleanups for passed \a ns to make it drop refc and be
+ * ready for freeing. Waits for refc == 0.
  *
  * The following is done:
- * (0) Unregister \a ns from its list to make inaccessible for potential users
- * like pools thread and others;
+ * (0) Unregister \a ns from its list to make inaccessible for potential
+ * users like pools thread and others;
  * (1) Clear all locks in \a ns.
  */
 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
@@ -843,9 +902,9 @@ void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
                 return;
         }
 
-        cfs_spin_lock(&ns->ns_lock);
-        ns->ns_stopping = 1;
-        cfs_spin_unlock(&ns->ns_lock);
+       spin_lock(&ns->ns_lock);
+       ns->ns_stopping = 1;
+       spin_unlock(&ns->ns_lock);
 
         /*
          * Can fail with -EINTR when force == 0 in which case try harder.
@@ -868,9 +927,9 @@ void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
 }
 
 /**
- * Performs freeing memory structures related to \a ns. This is only done when
- * ldlm_namespce_free_prior() successfully removed all resources referencing
- * \a ns and its refc == 0.
+ * Performs freeing memory structures related to \a ns. This is only done
+ * when ldlm_namespce_free_prior() successfully removed all resources
+ * referencing \a ns and its refc == 0.
  */
 void ldlm_namespace_free_post(struct ldlm_namespace *ns)
 {
@@ -880,32 +939,26 @@ void ldlm_namespace_free_post(struct ldlm_namespace *ns)
                 return;
         }
 
-
-        /*
-         * Make sure that nobody can find this ns in its list.
-         */
-        ldlm_namespace_unregister(ns, ns->ns_client);
-        /*
-         * Fini pool _before_ parent proc dir is removed. This is important as
-         * ldlm_pool_fini() removes own proc dir which is child to @dir. Removing
-         * it after @dir may cause oops.
-         */
-        ldlm_pool_fini(&ns->ns_pool);
-
-        ldlm_namespace_proc_unregister(ns);
-        cfs_hash_putref(ns->ns_rs_hash);
-        /*
-         * Namespace \a ns should be not on list in this time, otherwise this
-         * will cause issues realted to using freed \a ns in pools thread.
-         */
-        LASSERT(cfs_list_empty(&ns->ns_list_chain));
-        OBD_FREE_PTR(ns);
-        ldlm_put_ref();
-        EXIT;
+       /* Make sure that nobody can find this ns in its list. */
+       ldlm_namespace_unregister(ns, ns->ns_client);
+       /* Fini pool _before_ parent proc dir is removed. This is important as
+        * ldlm_pool_fini() removes own proc dir which is child to @dir.
+        * Removing it after @dir may cause oops. */
+       ldlm_pool_fini(&ns->ns_pool);
+
+       ldlm_namespace_proc_unregister(ns);
+       cfs_hash_putref(ns->ns_rs_hash);
+       /* Namespace \a ns should be not on list at this time, otherwise
+        * this will cause issues related to using freed \a ns in poold
+        * thread. */
+       LASSERT(cfs_list_empty(&ns->ns_list_chain));
+       OBD_FREE_PTR(ns);
+       ldlm_put_ref();
+       EXIT;
 }
 
-
-/* Cleanup the resource, and free namespace.
+/**
+ * Cleanup the resource, and free namespace.
  * bug 12864:
  * Deadlock issue:
  * proc1: destroy import
@@ -929,68 +982,87 @@ void ldlm_namespace_free(struct ldlm_namespace *ns,
         ldlm_namespace_free_prior(ns, imp, force);
         ldlm_namespace_free_post(ns);
 }
+EXPORT_SYMBOL(ldlm_namespace_free);
 
 void ldlm_namespace_get(struct ldlm_namespace *ns)
 {
         cfs_atomic_inc(&ns->ns_bref);
 }
+EXPORT_SYMBOL(ldlm_namespace_get);
+
+/* This is only for callers that care about refcount */
+int ldlm_namespace_get_return(struct ldlm_namespace *ns)
+{
+        return cfs_atomic_inc_return(&ns->ns_bref);
+}
 
 void ldlm_namespace_put(struct ldlm_namespace *ns)
 {
-        if (cfs_atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
-                cfs_waitq_signal(&ns->ns_waitq);
-                cfs_spin_unlock(&ns->ns_lock);
-        }
+       if (cfs_atomic_dec_and_lock(&ns->ns_bref, &ns->ns_lock)) {
+               cfs_waitq_signal(&ns->ns_waitq);
+               spin_unlock(&ns->ns_lock);
+       }
 }
+EXPORT_SYMBOL(ldlm_namespace_put);
 
-/* Register @ns in the list of namespaces */
+/** Register \a ns in the list of namespaces */
 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
 {
-        cfs_mutex_down(ldlm_namespace_lock(client));
-        LASSERT(cfs_list_empty(&ns->ns_list_chain));
-        cfs_list_add(&ns->ns_list_chain, ldlm_namespace_list(client));
-        cfs_atomic_inc(ldlm_namespace_nr(client));
-        cfs_mutex_up(ldlm_namespace_lock(client));
+       mutex_lock(ldlm_namespace_lock(client));
+       LASSERT(cfs_list_empty(&ns->ns_list_chain));
+       cfs_list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
+       ldlm_namespace_nr_inc(client);
+       mutex_unlock(ldlm_namespace_lock(client));
 }
 
-/* Unregister @ns from the list of namespaces */
+/** Unregister \a ns from the list of namespaces. */
 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
 {
-        cfs_mutex_down(ldlm_namespace_lock(client));
-        LASSERT(!cfs_list_empty(&ns->ns_list_chain));
-        /*
-         * Some asserts and possibly other parts of code still using
-         * list_empty(&ns->ns_list_chain). This is why it is important
-         * to use list_del_init() here.
-         */
-        cfs_list_del_init(&ns->ns_list_chain);
-        cfs_atomic_dec(ldlm_namespace_nr(client));
-        cfs_mutex_up(ldlm_namespace_lock(client));
+       mutex_lock(ldlm_namespace_lock(client));
+       LASSERT(!cfs_list_empty(&ns->ns_list_chain));
+       /* Some asserts and possibly other parts of the code are still
+        * using list_empty(&ns->ns_list_chain). This is why it is
+        * important to use list_del_init() here. */
+       cfs_list_del_init(&ns->ns_list_chain);
+       ldlm_namespace_nr_dec(client);
+       mutex_unlock(ldlm_namespace_lock(client));
+}
+
+/** Should be called with ldlm_namespace_lock(client) taken. */
+void ldlm_namespace_move_to_active_locked(struct ldlm_namespace *ns,
+                                      ldlm_side_t client)
+{
+       LASSERT(!cfs_list_empty(&ns->ns_list_chain));
+       LASSERT_MUTEX_LOCKED(ldlm_namespace_lock(client));
+       cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
 }
 
-/* Should be called under ldlm_namespace_lock(client) taken */
-void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
+/** Should be called with ldlm_namespace_lock(client) taken. */
+void ldlm_namespace_move_to_inactive_locked(struct ldlm_namespace *ns,
+                                        ldlm_side_t client)
 {
-        LASSERT(!cfs_list_empty(&ns->ns_list_chain));
-        LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
-        cfs_list_move_tail(&ns->ns_list_chain, ldlm_namespace_list(client));
+       LASSERT(!cfs_list_empty(&ns->ns_list_chain));
+       LASSERT_MUTEX_LOCKED(ldlm_namespace_lock(client));
+       cfs_list_move_tail(&ns->ns_list_chain,
+                          ldlm_namespace_inactive_list(client));
 }
 
-/* Should be called under ldlm_namespace_lock(client) taken */
+/** Should be called with ldlm_namespace_lock(client) taken. */
 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client)
 {
-        LASSERT_SEM_LOCKED(ldlm_namespace_lock(client));
+        LASSERT_MUTEX_LOCKED(ldlm_namespace_lock(client));
         LASSERT(!cfs_list_empty(ldlm_namespace_list(client)));
         return container_of(ldlm_namespace_list(client)->next,
                 struct ldlm_namespace, ns_list_chain);
 }
 
+/** Create and initialize new resource. */
 static struct ldlm_resource *ldlm_resource_new(void)
 {
         struct ldlm_resource *res;
         int idx;
 
-        OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, CFS_ALLOC_IO);
+       OBD_SLAB_ALLOC_PTR_GFP(res, ldlm_resource_slab, __GFP_IO);
         if (res == NULL)
                 return NULL;
 
@@ -998,7 +1070,7 @@ static struct ldlm_resource *ldlm_resource_new(void)
         CFS_INIT_LIST_HEAD(&res->lr_converting);
         CFS_INIT_LIST_HEAD(&res->lr_waiting);
 
-        /* initialize interval trees for each lock mode*/
+       /* Initialize interval trees for each lock mode. */
         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
                 res->lr_itree[idx].lit_size = 0;
                 res->lr_itree[idx].lit_mode = 1 << idx;
@@ -1006,19 +1078,23 @@ static struct ldlm_resource *ldlm_resource_new(void)
         }
 
         cfs_atomic_set(&res->lr_refcount, 1);
-        cfs_spin_lock_init(&res->lr_lock);
-        lu_ref_init(&res->lr_reference);
+       spin_lock_init(&res->lr_lock);
+       lu_ref_init(&res->lr_reference);
 
-        /* one who creates the resource must unlock
-         * the semaphore after lvb initialization */
-        cfs_init_mutex_locked(&res->lr_lvb_sem);
+       /* The creator of the resource must unlock the mutex after LVB
+        * initialization. */
+       mutex_init(&res->lr_lvb_mutex);
+       mutex_lock(&res->lr_lvb_mutex);
 
-        return res;
+       return res;
 }
 
-/* Args: unlocked namespace
- *  * Locks: takes and releases NS hash-lock and res->lr_lock
- *   * Returns: referenced, unlocked ldlm_resource or NULL */
+/**
+ * Return a reference to resource with given name, creating it if necessary.
+ * Args: namespace with ns_lock unlocked
+ * Locks: takes and releases NS hash-lock and res->lr_lock
+ * Returns: referenced, unlocked ldlm_resource or NULL
+ */
 struct ldlm_resource *
 ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
                   const struct ldlm_res_id *name, ldlm_type_t type, int create)
@@ -1027,6 +1103,7 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
         struct ldlm_resource *res;
         cfs_hash_bd_t         bd;
         __u64                 version;
+       int                   ns_refcount = 0;
 
         LASSERT(ns != NULL);
         LASSERT(parent == NULL);
@@ -1038,11 +1115,16 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
         if (hnode != NULL) {
                 cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 0);
                 res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
-                /* synchronize WRT resource creation */
+               /* Synchronize with regard to resource creation. */
                 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
-                        cfs_down(&res->lr_lvb_sem);
-                        cfs_up(&res->lr_lvb_sem);
+                       mutex_lock(&res->lr_lvb_mutex);
+                       mutex_unlock(&res->lr_lvb_mutex);
                 }
+
+               if (unlikely(res->lr_lvb_len < 0)) {
+                       ldlm_resource_putref(res);
+                       res = NULL;
+               }
                 return res;
         }
 
@@ -1068,24 +1150,31 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
                 cfs_hash_bd_lookup_locked(ns->ns_rs_hash, &bd, (void *)name);
 
         if (hnode != NULL) {
-                /* someone won the race and added the resource before */
-                cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
-                /* clean lu_ref for failed resource */
-                lu_ref_fini(&res->lr_reference);
-                OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
-
-                res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
-                /* synchronize WRT resource creation */
-                if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
-                        cfs_down(&res->lr_lvb_sem);
-                        cfs_up(&res->lr_lvb_sem);
-                }
-                return res;
-        }
-        /* we won! let's add the resource */
+               /* Someone won the race and already added the resource. */
+               cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
+               /* Clean lu_ref for failed resource. */
+               lu_ref_fini(&res->lr_reference);
+               /* We have taken lr_lvb_mutex. Drop it. */
+               mutex_unlock(&res->lr_lvb_mutex);
+               OBD_SLAB_FREE(res, ldlm_resource_slab, sizeof *res);
+
+               res = cfs_hlist_entry(hnode, struct ldlm_resource, lr_hash);
+               /* Synchronize with regard to resource creation. */
+               if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
+                       mutex_lock(&res->lr_lvb_mutex);
+                       mutex_unlock(&res->lr_lvb_mutex);
+               }
+
+               if (unlikely(res->lr_lvb_len < 0)) {
+                       ldlm_resource_putref(res);
+                       res = NULL;
+               }
+               return res;
+       }
+       /* We won! Let's add the resource. */
         cfs_hash_bd_add_locked(ns->ns_rs_hash, &bd, &res->lr_hash);
-        if (cfs_hash_bd_count_get(&bd) == 1)
-                ldlm_namespace_get(ns);
+       if (cfs_hash_bd_count_get(&bd) == 1)
+               ns_refcount = ldlm_namespace_get_return(ns);
 
         cfs_hash_bd_unlock(ns->ns_rs_hash, &bd, 1);
         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_init) {
@@ -1093,15 +1182,37 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
 
                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_CREATE_RESOURCE, 2);
                 rc = ns->ns_lvbo->lvbo_init(res);
-                if (rc)
-                        CERROR("lvbo_init failed for resource "
-                               LPU64": rc %d\n", name->name[0], rc);
-                /* we create resource with locked lr_lvb_sem */
-                cfs_up(&res->lr_lvb_sem);
-        }
-
-        return res;
+               if (rc < 0) {
+                       CERROR("%s: lvbo_init failed for resource "LPX64":"
+                              LPX64": rc = %d\n", ns->ns_obd->obd_name,
+                              name->name[0], name->name[1], rc);
+                       if (res->lr_lvb_data) {
+                               OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
+                               res->lr_lvb_data = NULL;
+                       }
+                       res->lr_lvb_len = rc;
+                       mutex_unlock(&res->lr_lvb_mutex);
+                       ldlm_resource_putref(res);
+                       return NULL;
+               }
+       }
+
+       /* We create resource with locked lr_lvb_mutex. */
+       mutex_unlock(&res->lr_lvb_mutex);
+
+       /* Let's see if we happened to be the very first resource in this
+        * namespace. If so, and this is a client namespace, we need to move
+        * the namespace into the active namespaces list to be patrolled by
+        * the ldlm_poold. */
+       if (ns_is_client(ns) && ns_refcount == 1) {
+               mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
+               ldlm_namespace_move_to_active_locked(ns, LDLM_NAMESPACE_CLIENT);
+               mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
+       }
+
+       return res;
 }
+EXPORT_SYMBOL(ldlm_resource_get);
 
 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res)
 {
@@ -1161,6 +1272,7 @@ int ldlm_resource_putref(struct ldlm_resource *res)
         }
         return 0;
 }
+EXPORT_SYMBOL(ldlm_resource_putref);
 
 /* Returns 1 if the resource was freed, 0 if it remains. */
 int ldlm_resource_putref_locked(struct ldlm_resource *res)
@@ -1193,24 +1305,31 @@ int ldlm_resource_putref_locked(struct ldlm_resource *res)
         return 0;
 }
 
+/**
+ * Add a lock into a given resource into specified lock list.
+ */
 void ldlm_resource_add_lock(struct ldlm_resource *res, cfs_list_t *head,
                             struct ldlm_lock *lock)
 {
-        check_res_locked(res);
+       check_res_locked(res);
 
-        CDEBUG(D_OTHER, "About to add this lock:\n");
-        ldlm_lock_dump(D_OTHER, lock, 0);
+       LDLM_DEBUG(lock, "About to add this lock:\n");
 
-        if (lock->l_destroyed) {
-                CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
-                return;
-        }
+       if (lock->l_flags & LDLM_FL_DESTROYED) {
+               CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
+               return;
+       }
 
-        LASSERT(cfs_list_empty(&lock->l_res_link));
+       LASSERT(cfs_list_empty(&lock->l_res_link));
 
-        cfs_list_add_tail(&lock->l_res_link, head);
+       cfs_list_add_tail(&lock->l_res_link, head);
 }
 
+/**
+ * Insert a lock into resource after specified lock.
+ *
+ * Obtain resource description from the lock we are inserting after.
+ */
 void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
                                      struct ldlm_lock *new)
 {
@@ -1219,13 +1338,12 @@ void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
         check_res_locked(res);
 
         ldlm_resource_dump(D_INFO, res);
-        CDEBUG(D_OTHER, "About to insert this lock after %p:\n", original);
-        ldlm_lock_dump(D_OTHER, new, 0);
+        LDLM_DEBUG(new, "About to insert this lock after %p:\n", original);
 
-        if (new->l_destroyed) {
-                CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
-                goto out;
-        }
+       if (new->l_flags & LDLM_FL_DESTROYED) {
+               CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
+               goto out;
+       }
 
         LASSERT(cfs_list_empty(&new->l_res_link));
 
@@ -1244,6 +1362,7 @@ void ldlm_resource_unlink_lock(struct ldlm_lock *lock)
                 ldlm_extent_unlink_lock(lock);
         cfs_list_del_init(&lock->l_res_link);
 }
+EXPORT_SYMBOL(ldlm_resource_unlink_lock);
 
 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
 {
@@ -1251,6 +1370,10 @@ void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc)
         desc->lr_name = res->lr_name;
 }
 
+/**
+ * Print information about all locks in all namespaces on this node to debug
+ * log.
+ */
 void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
 {
         cfs_list_t *tmp;
@@ -1258,7 +1381,7 @@ void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
         if (!((libcfs_debug | D_ERROR) & level))
                 return;
 
-        cfs_mutex_down(ldlm_namespace_lock(client));
+       mutex_lock(ldlm_namespace_lock(client));
 
         cfs_list_for_each(tmp, ldlm_namespace_list(client)) {
                 struct ldlm_namespace *ns;
@@ -1266,8 +1389,9 @@ void ldlm_dump_all_namespaces(ldlm_side_t client, int level)
                 ldlm_namespace_dump(level, ns);
         }
 
-        cfs_mutex_up(ldlm_namespace_lock(client));
+       mutex_unlock(ldlm_namespace_lock(client));
 }
+EXPORT_SYMBOL(ldlm_dump_all_namespaces);
 
 static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
                               cfs_hlist_node_t *hnode, void *arg)
@@ -1282,6 +1406,10 @@ static int ldlm_res_hash_dump(cfs_hash_t *hs, cfs_hash_bd_t *bd,
         return 0;
 }
 
+/**
+ * Print information about all locks in this namespace on this node to debug
+ * log.
+ */
 void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
 {
         if (!((libcfs_debug | D_ERROR) & level))
@@ -1297,54 +1425,49 @@ void ldlm_namespace_dump(int level, struct ldlm_namespace *ns)
         cfs_hash_for_each_nolock(ns->ns_rs_hash,
                                  ldlm_res_hash_dump,
                                  (void *)(unsigned long)level);
-        cfs_spin_lock(&ns->ns_lock);
-        ns->ns_next_dump = cfs_time_shift(10);
-        cfs_spin_unlock(&ns->ns_lock);
+       spin_lock(&ns->ns_lock);
+       ns->ns_next_dump = cfs_time_shift(10);
+       spin_unlock(&ns->ns_lock);
 }
+EXPORT_SYMBOL(ldlm_namespace_dump);
 
+/**
+ * Print information about all locks in this resource to debug log.
+ */
 void ldlm_resource_dump(int level, struct ldlm_resource *res)
 {
-        cfs_list_t *tmp;
-        int pos;
+       struct ldlm_lock *lock;
+       unsigned int granted = 0;
 
-        CLASSERT(RES_NAME_SIZE == 4);
+       CLASSERT(RES_NAME_SIZE == 4);
 
-        if (!((libcfs_debug | D_ERROR) & level))
-                return;
+       if (!((libcfs_debug | D_ERROR) & level))
+               return;
 
-        CDEBUG(level, "--- Resource: %p ("LPU64"/"LPU64"/"LPU64"/"LPU64
-               ") (rc: %d)\n", res, res->lr_name.name[0], res->lr_name.name[1],
-               res->lr_name.name[2], res->lr_name.name[3],
-               cfs_atomic_read(&res->lr_refcount));
+       CDEBUG(level, "--- Resource: "DLDLMRES" (%p) refcount = %d\n",
+              PLDLMRES(res), res, cfs_atomic_read(&res->lr_refcount));
 
         if (!cfs_list_empty(&res->lr_granted)) {
-                pos = 0;
-                CDEBUG(level, "Granted locks:\n");
-                cfs_list_for_each(tmp, &res->lr_granted) {
-                        struct ldlm_lock *lock;
-                        lock = cfs_list_entry(tmp, struct ldlm_lock,
-                                              l_res_link);
-                        ldlm_lock_dump(level, lock, ++pos);
+                CDEBUG(level, "Granted locks (in reverse order):\n");
+                cfs_list_for_each_entry_reverse(lock, &res->lr_granted,
+                                                l_res_link) {
+                        LDLM_DEBUG_LIMIT(level, lock, "###");
+                        if (!(level & D_CANTMASK) &&
+                            ++granted > ldlm_dump_granted_max) {
+                                CDEBUG(level, "only dump %d granted locks to "
+                                       "avoid DDOS.\n", granted);
+                                break;
+                        }
                 }
         }
         if (!cfs_list_empty(&res->lr_converting)) {
-                pos = 0;
                 CDEBUG(level, "Converting locks:\n");
-                cfs_list_for_each(tmp, &res->lr_converting) {
-                        struct ldlm_lock *lock;
-                        lock = cfs_list_entry(tmp, struct ldlm_lock,
-                                              l_res_link);
-                        ldlm_lock_dump(level, lock, ++pos);
-                }
+                cfs_list_for_each_entry(lock, &res->lr_converting, l_res_link)
+                        LDLM_DEBUG_LIMIT(level, lock, "###");
         }
         if (!cfs_list_empty(&res->lr_waiting)) {
-                pos = 0;
                 CDEBUG(level, "Waiting locks:\n");
-                cfs_list_for_each(tmp, &res->lr_waiting) {
-                        struct ldlm_lock *lock;
-                        lock = cfs_list_entry(tmp, struct ldlm_lock,
-                                              l_res_link);
-                        ldlm_lock_dump(level, lock, ++pos);
-                }
+                cfs_list_for_each_entry(lock, &res->lr_waiting, l_res_link)
+                        LDLM_DEBUG_LIMIT(level, lock, "###");
         }
 }