Whamcloud - gitweb
LU-395 Fix obd_zombie_barrier()
[fs/lustre-release.git] / lustre / obdclass / genops.c
index fd038cf..e0a9416 100644 (file)
@@ -26,7 +26,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
@@ -80,7 +80,6 @@ static struct obd_device *obd_device_alloc(void)
         }
         return obd;
 }
-EXPORT_SYMBOL(obd_device_alloc);
 
 static void obd_device_free(struct obd_device *obd)
 {
@@ -712,7 +711,7 @@ static void class_export_destroy(struct obd_export *exp)
         struct obd_device *obd = exp->exp_obd;
         ENTRY;
 
-        LASSERT (cfs_atomic_read(&exp->exp_refcount) == 0);
+        LASSERT_ATOMIC_ZERO(&exp->exp_refcount);
 
         CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
                exp->exp_client_uuid.uuid, obd->obd_name);
@@ -751,15 +750,18 @@ EXPORT_SYMBOL(class_export_get);
 void class_export_put(struct obd_export *exp)
 {
         LASSERT(exp != NULL);
+        LASSERT_ATOMIC_GT_LT(&exp->exp_refcount, 0, 0x5a5a5a);
         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
                cfs_atomic_read(&exp->exp_refcount) - 1);
-        LASSERT(cfs_atomic_read(&exp->exp_refcount) > 0);
-        LASSERT(cfs_atomic_read(&exp->exp_refcount) < 0x5a5a5a);
 
         if (cfs_atomic_dec_and_test(&exp->exp_refcount)) {
                 LASSERT(!cfs_list_empty(&exp->exp_obd_chain));
                 CDEBUG(D_IOCTL, "final put %p/%s\n",
                        exp, exp->exp_client_uuid.uuid);
+
+                /* release nid stat refererence */
+                lprocfs_exp_cleanup(exp);
+
                 obd_zombie_export_add(exp);
         }
 }
@@ -772,7 +774,7 @@ struct obd_export *class_new_export(struct obd_device *obd,
                                     struct obd_uuid *cluuid)
 {
         struct obd_export *export;
-        cfs_hash_t *hash;
+        cfs_hash_t *hash = NULL;
         int rc = 0;
         ENTRY;
 
@@ -801,6 +803,7 @@ struct obd_export *class_new_export(struct obd_device *obd,
         class_handle_hash(&export->exp_handle, export_handle_addref);
         export->exp_last_request_time = cfs_time_current_sec();
         cfs_spin_lock_init(&export->exp_lock);
+        cfs_spin_lock_init(&export->exp_rpc_lock);
         CFS_INIT_HLIST_NODE(&export->exp_uuid_hash);
         CFS_INIT_HLIST_NODE(&export->exp_nid_hash);
 
@@ -828,11 +831,11 @@ struct obd_export *class_new_export(struct obd_device *obd,
                 }
         }
 
-        cfs_hash_putref(hash);
-
         cfs_spin_lock(&obd->obd_dev_lock);
-        if (obd->obd_stopping)
+        if (obd->obd_stopping) {
+                cfs_hash_del(hash, cluuid, &export->exp_uuid_hash);
                 GOTO(exit_unlock, rc = -ENODEV);
+        }
 
         class_incref(obd, "export", export);
         cfs_list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
@@ -840,11 +843,14 @@ struct obd_export *class_new_export(struct obd_device *obd,
                           &export->exp_obd->obd_exports_timed);
         export->exp_obd->obd_num_exports++;
         cfs_spin_unlock(&obd->obd_dev_lock);
+        cfs_hash_putref(hash);
         RETURN(export);
 
 exit_unlock:
         cfs_spin_unlock(&obd->obd_dev_lock);
 exit_err:
+        if (hash)
+                cfs_hash_putref(hash);
         class_handle_unhash(&export->exp_handle);
         LASSERT(cfs_hlist_unhashed(&export->exp_uuid_hash));
         obd_destroy_export(export);
@@ -880,7 +886,7 @@ void class_import_destroy(struct obd_import *imp)
         CDEBUG(D_IOCTL, "destroying import %p for %s\n", imp,
                 imp->imp_obd->obd_name);
 
-        LASSERT(cfs_atomic_read(&imp->imp_refcount) == 0);
+        LASSERT_ATOMIC_ZERO(&imp->imp_refcount);
 
         ptlrpc_put_connection_superhack(imp->imp_connection);
 
@@ -907,8 +913,6 @@ static void import_handle_addref(void *import)
 
 struct obd_import *class_import_get(struct obd_import *import)
 {
-        LASSERT(cfs_atomic_read(&import->imp_refcount) >= 0);
-        LASSERT(cfs_atomic_read(&import->imp_refcount) < 0x5a5a5a);
         cfs_atomic_inc(&import->imp_refcount);
         CDEBUG(D_INFO, "import %p refcount=%d obd=%s\n", import,
                cfs_atomic_read(&import->imp_refcount),
@@ -921,9 +925,8 @@ void class_import_put(struct obd_import *imp)
 {
         ENTRY;
 
-        LASSERT(cfs_atomic_read(&imp->imp_refcount) > 0);
-        LASSERT(cfs_atomic_read(&imp->imp_refcount) < 0x5a5a5a);
         LASSERT(cfs_list_empty(&imp->imp_zombie_chain));
+        LASSERT_ATOMIC_GE_LT(&imp->imp_refcount, 0, 0x5a5a5a);
 
         CDEBUG(D_INFO, "import %p refcount=%d obd=%s\n", imp,
                cfs_atomic_read(&imp->imp_refcount) - 1,
@@ -1075,7 +1078,7 @@ void class_export_recovery_cleanup(struct obd_export *exp)
 {
         struct obd_device *obd = exp->exp_obd;
 
-        cfs_spin_lock_bh(&obd->obd_processing_task_lock);
+        cfs_spin_lock(&obd->obd_recovery_task_lock);
         if (exp->exp_delayed)
                 obd->obd_delayed_clients--;
         if (obd->obd_recovering && exp->exp_in_recovery) {
@@ -1085,6 +1088,7 @@ void class_export_recovery_cleanup(struct obd_export *exp)
                 LASSERT(obd->obd_connected_clients);
                 obd->obd_connected_clients--;
         }
+        cfs_spin_unlock(&obd->obd_recovery_task_lock);
         /** Cleanup req replay fields */
         if (exp->exp_req_replay_needed) {
                 cfs_spin_lock(&exp->exp_lock);
@@ -1101,7 +1105,6 @@ void class_export_recovery_cleanup(struct obd_export *exp)
                 LASSERT(cfs_atomic_read(&obd->obd_lock_replay_clients));
                 cfs_atomic_dec(&obd->obd_lock_replay_clients);
         }
-        cfs_spin_unlock_bh(&obd->obd_processing_task_lock);
 }
 
 /* This function removes 1-3 references from the export:
@@ -1461,6 +1464,9 @@ void obd_exports_barrier(struct obd_device *obd)
 }
 EXPORT_SYMBOL(obd_exports_barrier);
 
+/* Total amount of zombies to be destroyed */
+static int zombies_count = 0;
+
 /**
  * kill zombie imports and exports
  */
@@ -1491,11 +1497,19 @@ void obd_zombie_impexp_cull(void)
 
                 cfs_spin_unlock(&obd_zombie_impexp_lock);
 
-                if (import != NULL)
+                if (import != NULL) {
                         class_import_destroy(import);
+                        cfs_spin_lock(&obd_zombie_impexp_lock);
+                        zombies_count--;
+                        cfs_spin_unlock(&obd_zombie_impexp_lock);
+                }
 
-                if (export != NULL)
+                if (export != NULL) {
                         class_export_destroy(export);
+                        cfs_spin_lock(&obd_zombie_impexp_lock);
+                        zombies_count--;
+                        cfs_spin_unlock(&obd_zombie_impexp_lock);
+                }
 
                 cfs_cond_resched();
         } while (import != NULL || export != NULL);
@@ -1520,10 +1534,8 @@ static int obd_zombie_impexp_check(void *arg)
         int rc;
 
         cfs_spin_lock(&obd_zombie_impexp_lock);
-        rc = cfs_list_empty(&obd_zombie_imports) &&
-             cfs_list_empty(&obd_zombie_exports) &&
+        rc = (zombies_count == 0) &&
              !cfs_test_bit(OBD_ZOMBIE_STOP, &obd_zombie_flags);
-
         cfs_spin_unlock(&obd_zombie_impexp_lock);
 
         RETURN(rc);
@@ -1538,6 +1550,7 @@ static void obd_zombie_export_add(struct obd_export *exp) {
         cfs_list_del_init(&exp->exp_obd_chain);
         cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
         cfs_spin_lock(&obd_zombie_impexp_lock);
+        zombies_count++;
         cfs_list_add(&exp->exp_obd_chain, &obd_zombie_exports);
         cfs_spin_unlock(&obd_zombie_impexp_lock);
 
@@ -1552,6 +1565,7 @@ static void obd_zombie_import_add(struct obd_import *imp) {
         LASSERT(imp->imp_sec == NULL);
         cfs_spin_lock(&obd_zombie_impexp_lock);
         LASSERT(cfs_list_empty(&imp->imp_zombie_chain));
+        zombies_count++;
         cfs_list_add(&imp->imp_zombie_chain, &obd_zombie_imports);
         cfs_spin_unlock(&obd_zombie_impexp_lock);
 
@@ -1576,8 +1590,7 @@ static int obd_zombie_is_idle(void)
 
         LASSERT(!cfs_test_bit(OBD_ZOMBIE_STOP, &obd_zombie_flags));
         cfs_spin_lock(&obd_zombie_impexp_lock);
-        rc = cfs_list_empty(&obd_zombie_imports) &&
-             cfs_list_empty(&obd_zombie_exports);
+        rc = (zombies_count == 0);
         cfs_spin_unlock(&obd_zombie_impexp_lock);
         return rc;
 }
@@ -1702,3 +1715,70 @@ void obd_zombie_impexp_stop(void)
 #endif
 }
 
+/***** Kernel-userspace comm helpers *******/
+
+/* Get length of entire message, including header */
+int kuc_len(int payload_len)
+{
+        return sizeof(struct kuc_hdr) + payload_len;
+}
+EXPORT_SYMBOL(kuc_len);
+
+/* Get a pointer to kuc header, given a ptr to the payload
+ * @param p Pointer to payload area
+ * @returns Pointer to kuc header
+ */
+struct kuc_hdr * kuc_ptr(void *p)
+{
+        struct kuc_hdr *lh = ((struct kuc_hdr *)p) - 1;
+        LASSERT(lh->kuc_magic == KUC_MAGIC);
+        return lh;
+}
+EXPORT_SYMBOL(kuc_ptr);
+
+/* Test if payload is part of kuc message
+ * @param p Pointer to payload area
+ * @returns boolean
+ */
+int kuc_ispayload(void *p)
+{
+        struct kuc_hdr *kh = ((struct kuc_hdr *)p) - 1;
+
+        if (kh->kuc_magic == KUC_MAGIC)
+                return 1;
+        else
+                return 0;
+}
+EXPORT_SYMBOL(kuc_ispayload);
+
+/* Alloc space for a message, and fill in header
+ * @return Pointer to payload area
+ */
+void *kuc_alloc(int payload_len, int transport, int type)
+{
+        struct kuc_hdr *lh;
+        int len = kuc_len(payload_len);
+
+        OBD_ALLOC(lh, len);
+        if (lh == NULL)
+                return ERR_PTR(-ENOMEM);
+
+        lh->kuc_magic = KUC_MAGIC;
+        lh->kuc_transport = transport;
+        lh->kuc_msgtype = type;
+        lh->kuc_msglen = len;
+
+        return (void *)(lh + 1);
+}
+EXPORT_SYMBOL(kuc_alloc);
+
+/* Takes pointer to payload area */
+inline void kuc_free(void *p, int payload_len)
+{
+        struct kuc_hdr *lh = kuc_ptr(p);
+        OBD_FREE(lh, kuc_len(payload_len));
+}
+EXPORT_SYMBOL(kuc_free);
+
+
+