Whamcloud - gitweb
LU-2901 ldlm: fix resource/fid check, use DLDLMRES
[fs/lustre-release.git] / lustre / ldlm / ldlm_resource.c
index 4345f1f..b86c66d 100644 (file)
@@ -27,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;
 
 struct mutex ldlm_srv_namespace_lock;
 CFS_LIST_HEAD(ldlm_srv_namespace_list);
 
 struct mutex ldlm_cli_namespace_lock;
-CFS_LIST_HEAD(ldlm_cli_namespace_list);
+/* 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;
@@ -192,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) {
@@ -203,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, "
@@ -215,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;
@@ -240,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,
@@ -254,7 +259,7 @@ 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 LRU resize was originally supported before
                 * turning it on here. */
@@ -675,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);
@@ -765,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 {
@@ -793,22 +798,19 @@ 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;
 }
 
 /**
@@ -988,6 +990,12 @@ void ldlm_namespace_get(struct ldlm_namespace *ns)
 }
 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)) {
@@ -1002,8 +1010,8 @@ void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client)
 {
        mutex_lock(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_list_add(&ns->ns_list_chain, ldlm_namespace_inactive_list(client));
+       ldlm_namespace_nr_inc(client);
        mutex_unlock(ldlm_namespace_lock(client));
 }
 
@@ -1016,16 +1024,27 @@ void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client)
         * 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));
+       ldlm_namespace_nr_dec(client);
        mutex_unlock(ldlm_namespace_lock(client));
 }
 
 /** Should be called with ldlm_namespace_lock(client) taken. */
-void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client)
+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));
+       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 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_MUTEX_LOCKED(ldlm_namespace_lock(client));
+       cfs_list_move_tail(&ns->ns_list_chain,
+                          ldlm_namespace_inactive_list(client));
 }
 
 /** Should be called with ldlm_namespace_lock(client) taken. */
@@ -1043,7 +1062,7 @@ 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;
 
@@ -1084,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);
@@ -1100,6 +1120,11 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
                        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;
         }
 
@@ -1139,12 +1164,17 @@ ldlm_resource_get(struct ldlm_namespace *ns, struct ldlm_resource *parent,
                        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) {
@@ -1152,14 +1182,34 @@ 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);
+               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);
@@ -1261,18 +1311,18 @@ int ldlm_resource_putref_locked(struct ldlm_resource *res)
 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);
 
-        LDLM_DEBUG(lock, "About to add this lock:\n");
+       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);
 }
 
 /**
@@ -1290,10 +1340,10 @@ void ldlm_resource_insert_lock_after(struct ldlm_lock *original,
         ldlm_resource_dump(D_INFO, res);
         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));
 
@@ -1386,18 +1436,16 @@ EXPORT_SYMBOL(ldlm_namespace_dump);
  */
 void ldlm_resource_dump(int level, struct ldlm_resource *res)
 {
-        struct ldlm_lock *lock;
-        unsigned int granted = 0;
+       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)) {
                 CDEBUG(level, "Granted locks (in reverse order):\n");