Whamcloud - gitweb
Defensive check to avoid re-freeing export during force cleanup.
[fs/lustre-release.git] / lustre / obdclass / genops.c
index 12451d1..3bac4a3 100644 (file)
@@ -30,7 +30,7 @@
 #include <linux/random.h>
 #include <linux/slab.h>
 #include <linux/pagemap.h>
-#else 
+#else
 #include <liblustre.h>
 #include <linux/obd_class.h>
 #include <linux/obd.h>
@@ -91,8 +91,8 @@ void class_put_type(struct obd_type *type)
         module_put(type->typ_ops->o_owner);
 }
 
-int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
-                        char *name)
+int class_register_type(struct obd_ops *ops, struct md_ops *md_ops,
+                        struct lprocfs_vars *vars, char *name)
 {
         struct obd_type *type;
         int rc = 0;
@@ -112,10 +112,17 @@ int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
 
         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
         OBD_ALLOC(type->typ_name, strlen(name) + 1);
-        if (type->typ_ops == NULL || type->typ_name == NULL)
+        if (md_ops)
+                OBD_ALLOC(type->typ_md_ops, sizeof(*type->typ_md_ops));
+        if (type->typ_ops == NULL || type->typ_name == NULL ||
+                        (md_ops && type->typ_md_ops == NULL))
                 GOTO (failed, rc);
 
         *(type->typ_ops) = *ops;
+        if (md_ops)
+                *(type->typ_md_ops) = *md_ops;
+        else
+                type->typ_md_ops = NULL;
         strcpy(type->typ_name, name);
 
 #ifdef LPROCFS
@@ -139,6 +146,8 @@ int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
                 OBD_FREE(type->typ_name, strlen(name) + 1);
         if (type->typ_ops != NULL)
                 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
+        if (type->typ_md_ops != NULL)
+                OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
         OBD_FREE(type, sizeof(*type));
         RETURN(rc);
 }
@@ -172,6 +181,8 @@ int class_unregister_type(char *name)
         OBD_FREE(type->typ_name, strlen(name) + 1);
         if (type->typ_ops != NULL)
                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
+        if (type->typ_md_ops != NULL)
+                OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
         OBD_FREE(type, sizeof(*type));
         RETURN(0);
 } /* class_unregister_type */
@@ -240,8 +251,9 @@ struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
 
 /* Search for a client OBD connected to tgt_uuid.  If grp_uuid is
    specified, then only the client with that uuid is returned,
-   otherwise any client connected to the tgt is returned. */
-struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid, 
+   otherwise any client connected to the tgt is returned.
+   If tgt_uuid is NULL, the lov with grp_uuid is returned. */
+struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
                                           char * typ_name,
                                           struct obd_uuid *grp_uuid)
 {
@@ -251,13 +263,19 @@ struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
                 struct obd_device *obd = &obd_dev[i];
                 if (obd->obd_type == NULL)
                         continue;
-                if ((strncmp(obd->obd_type->typ_name, typ_name, 
+                if ((strncmp(obd->obd_type->typ_name, typ_name,
                              strlen(typ_name)) == 0)) {
                         struct client_obd *cli = &obd->u.cli;
                         struct obd_import *imp = cli->cl_import;
+                        if (tgt_uuid == NULL) {
+                                LASSERT(grp_uuid);
+                                if (obd_uuid_equals(grp_uuid, &obd->obd_uuid))
+                                        return obd;
+                                continue;
+                        }
                         if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
-                            ((grp_uuid)? obd_uuid_equals(grp_uuid, 
-                                                        &obd->obd_uuid) : 1)) {
+                            ((grp_uuid)? obd_uuid_equals(grp_uuid,
+                                                         &obd->obd_uuid) : 1)) {
                                 return obd;
                         }
                 }
@@ -266,6 +284,35 @@ struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
         return NULL;
 }
 
+/* Iterate the obd_device list looking devices have grp_uuid. Start
+   searching at *next, and if a device is found, the next index to look
+   it is saved in *next. If next is NULL, then the first matching device
+   will always be returned. */
+struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
+{
+        int i;
+        if (next == NULL) 
+                i = 0;
+        else if (*next >= 0 && *next < MAX_OBD_DEVICES)
+                i = *next;
+        else 
+                return NULL;
+                
+        for (; i < MAX_OBD_DEVICES; i++) {
+                struct obd_device *obd = &obd_dev[i];
+                if (obd->obd_type == NULL)
+                        continue;
+                if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
+                        if (next != NULL)
+                                *next = i+1;
+                        return obd;
+                }
+        }
+
+        return NULL;
+}
+
+
 void obd_cleanup_caches(void)
 {
         int rc;
@@ -364,7 +411,6 @@ struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
         return obd->u.cli.cl_import;
 }
 
-
 /* Export management functions */
 static void export_handle_addref(void *export)
 {
@@ -384,6 +430,7 @@ void __class_export_put(struct obd_export *exp)
                 if (exp->exp_connection)
                         ptlrpc_put_connection_superhack(exp->exp_connection);
 
+                LASSERT(list_empty(&exp->exp_outstanding_replies));
                 LASSERT(list_empty(&exp->exp_handle.h_link));
                 obd_destroy_export(exp);
 
@@ -391,6 +438,11 @@ void __class_export_put(struct obd_export *exp)
                 if (obd->obd_set_up) {
                         atomic_dec(&obd->obd_refcount);
                         wake_up(&obd->obd_refcount_waitq);
+                } else {
+                        CERROR("removing export %p from obd %s (%p) -- OBD "
+                               "not set up (refcount = %d)\n", exp,
+                               obd->obd_name, obd,
+                               atomic_read(&obd->obd_refcount));
                 }
         }
 }
@@ -410,7 +462,9 @@ struct obd_export *class_new_export(struct obd_device *obd)
 
         export->exp_conn_cnt = 0;
         atomic_set(&export->exp_refcount, 2);
+        atomic_set(&export->exp_rpc_count, 0);
         export->exp_obd = obd;
+        INIT_LIST_HEAD(&export->exp_outstanding_replies);
         /* XXX this should be in LDLM init */
         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
 
@@ -493,9 +547,11 @@ struct obd_import *class_new_import(void)
         imp->imp_max_transno = 0;
         imp->imp_peer_committed_transno = 0;
         imp->imp_state = LUSTRE_IMP_NEW;
-        sema_init(&imp->imp_recovery_sem, 1);
+        init_waitqueue_head(&imp->imp_recovery_waitq);
 
         atomic_set(&imp->imp_refcount, 2);
+        atomic_set(&imp->imp_inflight, 0);
+        atomic_set(&imp->imp_replay_inflight, 0);
         INIT_LIST_HEAD(&imp->imp_handle.h_link);
         class_handle_hash(&imp->imp_handle, import_handle_addref);
 
@@ -505,7 +561,7 @@ struct obd_import *class_new_import(void)
 void class_destroy_import(struct obd_import *import)
 {
         LASSERT(import != NULL);
-        LASSERT((unsigned long)import != 0x5a5a5a5a);
+        LASSERT(import != LP_POISON);
 
         class_handle_unhash(&import->imp_handle);
 
@@ -556,8 +612,7 @@ int class_disconnect(struct obd_export *export, int flags)
 
         if (export == NULL) {
                 fixme();
-                CDEBUG(D_IOCTL, "disconnect: attempting to free "
-                       "null export %p\n", export);
+                CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
                 RETURN(-EINVAL);
         }
 
@@ -567,11 +622,15 @@ int class_disconnect(struct obd_export *export, int flags)
         if (list_empty(&export->exp_handle.h_link))
                 RETURN(0);
 
-        CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", 
+        CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
                export->exp_handle.h_cookie);
 
-        class_unlink_export(export);
-        class_export_put(export);
+        if (export->exp_handle.h_cookie == 0x5a5a5a5a5a5a5a5a) {
+                CERROR("disconnecting freed export %p, ignoring\n", export);
+        } else {
+                class_unlink_export(export);
+                class_export_put(export);
+        }
         RETURN(0);
 }
 
@@ -589,17 +648,20 @@ void class_disconnect_exports(struct obd_device *obd, int flags)
         list_del_init(&obd->obd_exports);
         spin_unlock(&obd->obd_dev_lock);
 
-        CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
+        CDEBUG(D_HA, "OBD device %d (%p) has exports, "
                "disconnecting them\n", obd->obd_minor, obd);
         list_for_each_safe(tmp, n, &work_list) {
                 exp = list_entry(tmp, struct obd_export, exp_obd_chain);
                 class_export_get(exp);
-                
-                if (obd_uuid_equals(&exp->exp_client_uuid, 
+
+                if (obd_uuid_equals(&exp->exp_client_uuid,
                                     &exp->exp_obd->obd_uuid)) {
-                        CDEBUG(D_IOCTL, 
+                        CDEBUG(D_HA,
                                "exp %p export uuid == obd uuid, don't discon\n",
                                exp);
+                        /* Need to delete this now so we don't end up pointing
+                         * to work_list later when this export is cleaned up. */
+                        list_del_init(&exp->exp_obd_chain);
                         class_export_put(exp);
                         continue;
                 }
@@ -613,69 +675,134 @@ void class_disconnect_exports(struct obd_device *obd, int flags)
                 rc = obd_disconnect(fake_exp, flags);
                 class_export_put(exp);
                 if (rc) {
-                        CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
+                        CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
                                exp, rc);
                 } else {
-                        CDEBUG(D_IOCTL, "export %p disconnected\n", exp);
+                        CDEBUG(D_HA, "export %p disconnected\n", exp);
                 }
         }
         EXIT;
 }
 
-void osic_init(struct obd_sync_io_container *osic)
+int oig_init(struct obd_io_group **oig_out)
 {
-        spin_lock_init(&osic->osic_lock);
-        osic->osic_rc = 0;
-        osic->osic_pending = 0;
-        init_waitqueue_head(&osic->osic_waitq);
+        struct obd_io_group *oig;
+        ENTRY;
+
+        OBD_ALLOC(oig, sizeof(*oig));
+        if (oig == NULL)
+                RETURN(-ENOMEM);
+
+        spin_lock_init(&oig->oig_lock);
+        oig->oig_rc = 0;
+        oig->oig_pending = 0;
+        atomic_set(&oig->oig_refcount, 1);
+        init_waitqueue_head(&oig->oig_waitq);
+        INIT_LIST_HEAD(&oig->oig_occ_list);
+
+        *oig_out = oig;
+        RETURN(0);
 };
 
-void osic_add_one(struct obd_sync_io_container *osic)
+static inline void oig_grab(struct obd_io_group *oig)
+{
+        atomic_inc(&oig->oig_refcount);
+}
+void oig_release(struct obd_io_group *oig)
+{
+        if (atomic_dec_and_test(&oig->oig_refcount))
+                OBD_FREE(oig, sizeof(*oig));
+}
+
+void oig_add_one(struct obd_io_group *oig,
+                  struct oig_callback_context *occ)
 {
         unsigned long flags;
-        CDEBUG(D_CACHE, "osic %p ready to roll\n", osic);
-        spin_lock_irqsave(&osic->osic_lock, flags);
-        osic->osic_pending++;
-        spin_unlock_irqrestore(&osic->osic_lock, flags);
+        CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
+        spin_lock_irqsave(&oig->oig_lock, flags);
+        oig->oig_pending++;
+        if (occ != NULL)
+                list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
+        spin_unlock_irqrestore(&oig->oig_lock, flags);
+        oig_grab(oig);
 }
 
-void osic_complete_one(struct obd_sync_io_container *osic, int rc)
+void oig_complete_one(struct obd_io_group *oig,
+                      struct oig_callback_context *occ, int rc)
 {
         unsigned long flags;
-        wait_queue_head_t *wake = NULL; 
+        wait_queue_head_t *wake = NULL;
         int old_rc;
 
-        spin_lock_irqsave(&osic->osic_lock, flags);
-        old_rc = osic->osic_rc;
-        if (osic->osic_rc == 0 && rc != 0)
-                osic->osic_rc = rc;
-        if (--osic->osic_pending <= 0)
-                wake = &osic->osic_waitq;
-        spin_unlock_irqrestore(&osic->osic_lock, flags);
-        CDEBUG(D_CACHE, "osic %p completed, rc %d -> %d via %d, %d now "
-                        "pending (racey)\n", osic, old_rc, osic->osic_rc, rc, 
-                        osic->osic_pending);
+        spin_lock_irqsave(&oig->oig_lock, flags);
+
+        if (occ != NULL)
+                list_del_init(&occ->occ_oig_item);
+
+        old_rc = oig->oig_rc;
+        if (oig->oig_rc == 0 && rc != 0)
+                oig->oig_rc = rc;
+
+        if (--oig->oig_pending <= 0)
+                wake = &oig->oig_waitq;
+
+        spin_unlock_irqrestore(&oig->oig_lock, flags);
+
+        CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
+                        "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
+                        oig->oig_pending);
         if (wake)
                 wake_up(wake);
+        oig_release(oig);
 }
 
-static int osic_done(struct obd_sync_io_container *osic)
+static int oig_done(struct obd_io_group *oig)
 {
         unsigned long flags;
         int rc = 0;
-        spin_lock_irqsave(&osic->osic_lock, flags);
-        if (osic->osic_pending <= 0)
+        spin_lock_irqsave(&oig->oig_lock, flags);
+        if (oig->oig_pending <= 0)
                 rc = 1;
-        spin_unlock_irqrestore(&osic->osic_lock, flags);
+        spin_unlock_irqrestore(&oig->oig_lock, flags);
         return rc;
 }
 
-int osic_wait(struct obd_sync_io_container *osic)
+static void interrupted_oig(void *data)
+{
+        struct obd_io_group *oig = data;
+        struct list_head *pos;
+        struct oig_callback_context *occ;
+        unsigned long flags;
+
+        spin_lock_irqsave(&oig->oig_lock, flags);
+        list_for_each(pos, &oig->oig_occ_list) {
+                occ = list_entry(pos, struct oig_callback_context,
+                                 occ_oig_item);
+                occ->occ_interrupted(occ);
+        }
+        spin_unlock_irqrestore(&oig->oig_lock, flags);
+}
+
+int oig_wait(struct obd_io_group *oig)
 {
-        struct l_wait_info lwi = LWI_INTR(NULL, NULL);
+        struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
+        int rc;
+
+        CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
+
+        do {
+                rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
+                LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
+                /* we can't continue until the oig has emptied and stopped
+                 * referencing state that the caller will free upon return */
+                if (rc == -EINTR)
+                        lwi = (struct l_wait_info){ 0, };
+        } while (rc == -EINTR);
+
+        LASSERTF(oig->oig_pending == 0,
+                 "exiting oig_wait(oig = %p) with %d pending\n", oig,
+                 oig->oig_pending);
 
-        CDEBUG(D_CACHE, "waiting for osic %p\n", osic);
-        l_wait_event(osic->osic_waitq, osic_done(osic), &lwi);
-        CDEBUG(D_CACHE, "done waiting on osic %p\n", osic);
-        return osic->osic_rc;
+        CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
+        return oig->oig_rc;
 }