Whamcloud - gitweb
land b_smallfix 20040407_1414:
[fs/lustre-release.git] / lustre / obdclass / obd_config.c
index 41f2258..6359a3c 100644 (file)
  */
 int class_attach(struct lustre_cfg *lcfg)
 {
-        int minor;
         struct obd_type *type;
-        int err = 0;
-        int len;
-        char *typename;
-        char *name;
-        char *uuid;
         struct obd_device *obd;
-        int dev;
+        char *typename, *name, *uuid;
+        int minor, rc, len, dev, stage = 0;
 
         if (!lcfg->lcfg_inllen1 || !lcfg->lcfg_inlbuf1) {
                 CERROR("No type passed!\n");
@@ -95,22 +90,23 @@ int class_attach(struct lustre_cfg *lcfg)
                 CERROR("OBD: unknown type: %s\n", typename);
                 RETURN(-EINVAL);
         }
+        stage = 1;
 
         obd = class_name2obd(name);
         if (obd != NULL) {
                 CERROR("obd %s already attached\n", name);
-                RETURN(-EEXIST);
+                GOTO(out, rc = -EEXIST);
         }
 
         obd = class_newdev(&dev);
         if (obd == NULL)
-                RETURN(-EINVAL);
+                GOTO(out, rc = -EINVAL);
 
         /* have we attached a type to this device */
         if (obd->obd_attached || obd->obd_type) {
                 CERROR("OBD: Device %d already typed as %s.\n",
                        obd->obd_minor, MKSTR(obd->obd_type->typ_name));
-                RETURN(-EBUSY);
+                GOTO(out, rc = -EBUSY);
         }
 
         LASSERT(obd == (obd_dev + obd->obd_minor));
@@ -133,44 +129,45 @@ int class_attach(struct lustre_cfg *lcfg)
         INIT_LIST_HEAD(&obd->obd_recovery_queue);
         INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
 
-        spin_lock_init (&obd->obd_uncommitted_replies_lock);
-        INIT_LIST_HEAD (&obd->obd_uncommitted_replies);
+        spin_lock_init(&obd->obd_uncommitted_replies_lock);
+        INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
 
         len = strlen(name) + 1;
         OBD_ALLOC(obd->obd_name, len);
-        if (!obd->obd_name) {
-                class_put_type(obd->obd_type);
-                obd->obd_type = NULL;
-                RETURN(-ENOMEM);
-        }
+        if (!obd->obd_name)
+                GOTO(out, rc = -ENOMEM);
         memcpy(obd->obd_name, name, len);
+        stage = 2;
 
         len = strlen(uuid);
         if (len >= sizeof(obd->obd_uuid)) {
                 CERROR("uuid must be < "LPSZ" bytes long\n",
                        sizeof(obd->obd_uuid));
-                OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
-                class_put_type(obd->obd_type);
-                obd->obd_type = NULL;
-                RETURN(-EINVAL);
+                GOTO(out, rc = -EINVAL);
         }
         memcpy(obd->obd_uuid.uuid, uuid, len);
 
         /* do the attach */
-        if (OBP(obd, attach))
-                err = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
+        if (OBP(obd, attach)) {
+                rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
+                if (rc)
+                        GOTO(out, rc = -EINVAL);
+        }
 
-        if (err) {
+        obd->obd_attached = 1;
+        type->typ_refcnt++;
+        CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
+               obd->obd_minor, typename);
+        RETURN(0);
+ out:
+        switch (stage) {
+        case 2:
                 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
+        case 1:
                 class_put_type(obd->obd_type);
                 obd->obd_type = NULL;
-        } else {
-                obd->obd_attached = 1;
-                type->typ_refcnt++;
-                CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
-                       obd->obd_minor, typename);
         }
-        RETURN(err);
+        return rc;
 }
 
 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)