Whamcloud - gitweb
b=22273 Don't remove lcd in case of client_add failure
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.c
index 0d794a1..8f18207 100644 (file)
@@ -49,8 +49,7 @@
 #include "mdt_internal.h"
 
 static int mdt_server_data_update(const struct lu_env *env,
-                                  struct mdt_device *mdt,
-                                  int need_sync);
+                                  struct mdt_device *mdt);
 
 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
 {
@@ -149,58 +148,40 @@ static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
         if (rc == 0)
                 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
 
-        CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
-                       "uuid = %s\n"
-                       "last_transno = "LPU64"\n",
-                        rc, mdt->mdt_lsd.lsd_uuid,
-                        mdt->mdt_lsd.lsd_last_transno);
+        CDEBUG(D_INFO, "read last_rcvd header rc = %d, uuid = %s, "
+               "last_transno = "LPU64"\n", rc, mdt->mdt_lsd.lsd_uuid,
+               mdt->mdt_lsd.lsd_last_transno);
         return rc;
 }
 
-static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
-                                             struct mdt_device *mdt,
-                                             int need_sync)
+static int mdt_last_rcvd_header_write(const struct lu_env *env,
+                                      struct mdt_device *mdt,
+                                      struct thandle *th)
 {
         struct mdt_thread_info *mti;
-        struct thandle *th;
         int rc;
         ENTRY;
 
         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
 
-        if (mti->mti_exp) {
-                spin_lock(&mti->mti_exp->exp_lock);
-                mti->mti_exp->exp_need_sync = need_sync;
-                spin_unlock(&mti->mti_exp->exp_lock);
-        }
-        mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
-        th = mdt_trans_start(env, mdt);
-        if (IS_ERR(th))
-                RETURN(PTR_ERR(th));
-
         mti->mti_off = 0;
         lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
 
-        if (need_sync && mti->mti_exp)
-                mdt_trans_add_cb(th, lut_cb_client, mti->mti_exp);
-
         rc = dt_record_write(env, mdt->mdt_last_rcvd,
                              mdt_buf_const(env, &mti->mti_lsd,
                                            sizeof(mti->mti_lsd)),
                              &mti->mti_off, th);
 
-        mdt_trans_stop(env, mdt, th);
-
-        CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
-               "uuid = %s\nlast_transno = "LPU64"\n",
-               rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
+        CDEBUG(D_INFO, "write last_rcvd header rc = %d, uuid = %s, "
+               "last_transno = "LPU64"\n", rc, mdt->mdt_lsd.lsd_uuid,
+               mdt->mdt_lsd.lsd_last_transno);
 
         RETURN(rc);
 }
 
-static int mdt_last_rcvd_read(const struct lu_env *env,
-                              struct mdt_device *mdt,
-                              struct lsd_client_data *lcd, loff_t *off)
+static int mdt_last_rcvd_read(const struct lu_env *env, struct mdt_device *mdt,
+                              struct lsd_client_data *lcd, loff_t *off, 
+                              int index)
 {
         struct mdt_thread_info *mti;
         struct lsd_client_data *tmp;
@@ -210,28 +191,19 @@ static int mdt_last_rcvd_read(const struct lu_env *env,
         tmp = &mti->mti_lcd;
         rc = dt_record_read(env, mdt->mdt_last_rcvd,
                             mdt_buf(env, tmp, sizeof(*tmp)), off);
-        if (rc == 0)
+        if (rc == 0) {
+                check_lcd(mdt2obd_dev(mdt)->obd_name, index, tmp);
                 lcd_le_to_cpu(tmp, lcd);
+        }
 
-        CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
-                       "uuid = %s\n"
-                       "last_transno = "LPU64"\n"
-                       "last_xid = "LPU64"\n"
-                       "last_result = %u\n"
-                       "last_data = %u\n"
-                       "last_close_transno = "LPU64"\n"
-                       "last_close_xid = "LPU64"\n"
-                       "last_close_result = %u\n",
-                        (int)(*off - sizeof(*tmp)),
-                        rc,
-                        lcd->lcd_uuid,
-                        lcd->lcd_last_transno,
-                        lcd->lcd_last_xid,
-                        lcd->lcd_last_result,
-                        lcd->lcd_last_data,
-                        lcd->lcd_last_close_transno,
-                        lcd->lcd_last_close_xid,
-                        lcd->lcd_last_close_result);
+        CDEBUG(D_INFO, "read lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
+               ", last_xid = "LPU64", last_result = %u, last_data = %u, "
+               "last_close_transno = "LPU64", last_close_xid = "LPU64", "
+               "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
+               rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
+               lcd->lcd_last_result, lcd->lcd_last_data,
+               lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
+               lcd->lcd_last_close_result);
         return rc;
 }
 
@@ -253,25 +225,14 @@ static int mdt_last_rcvd_write(const struct lu_env *env,
         rc = dt_record_write(env, mdt->mdt_last_rcvd,
                              mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
 
-        CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
-                       "uuid = %s\n"
-                       "last_transno = "LPU64"\n"
-                       "last_xid = "LPU64"\n"
-                       "last_result = %u\n"
-                       "last_data = %u\n"
-                       "last_close_transno = "LPU64"\n"
-                       "last_close_xid = "LPU64"\n"
-                       "last_close_result = %u\n",
-                        (int)(*off - sizeof(*tmp)),
-                        rc,
-                        lcd->lcd_uuid,
-                        lcd->lcd_last_transno,
-                        lcd->lcd_last_xid,
-                        lcd->lcd_last_result,
-                        lcd->lcd_last_data,
-                        lcd->lcd_last_close_transno,
-                        lcd->lcd_last_close_xid,
-                        lcd->lcd_last_close_result);
+        CDEBUG(D_INFO, "write lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
+               ", last_xid = "LPU64", last_result = %u, last_data = %u, "
+               "last_close_transno = "LPU64", last_close_xid = "LPU64" ,"
+               "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
+               rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
+               lcd->lcd_last_result, lcd->lcd_last_data,
+               lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
+               lcd->lcd_last_close_result);
         return rc;
 }
 
@@ -290,7 +251,7 @@ static int mdt_clients_data_init(const struct lu_env *env,
         /* When we do a clean MDS shutdown, we save the last_transno into
          * the header.  If we find clients with higher last_transno values
          * then those clients may need recovery done. */
-        LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
+        LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
         for (cl_idx = 0, off = lsd->lsd_client_start;
              off < last_size; cl_idx++) {
                 __u64 last_transno;
@@ -305,7 +266,7 @@ static int mdt_clients_data_init(const struct lu_env *env,
                 off = lsd->lsd_client_start +
                         cl_idx * lsd->lsd_client_size;
 
-                rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
+                rc = mdt_last_rcvd_read(env, mdt, lcd, &off, cl_idx);
                 if (rc) {
                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
                                LAST_RCVD, cl_idx, off, rc);
@@ -335,8 +296,9 @@ static int mdt_clients_data_init(const struct lu_env *env,
                         if (PTR_ERR(exp) == -EALREADY) {
                                 /* export already exists, zero out this one */
                                 lcd->lcd_uuid[0] = '\0';
-                        } else
+                        } else {
                                 GOTO(err_client, rc = PTR_ERR(exp));
+                        }
                 } else {
                         struct mdt_thread_info *mti;
                         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
@@ -349,10 +311,10 @@ static int mdt_clients_data_init(const struct lu_env *env,
                         /* VBR: set export last committed version */
                         exp->exp_last_committed = last_transno;
                         lcd = NULL;
-                        spin_lock(&exp->exp_lock);
+                        cfs_spin_lock(&exp->exp_lock);
                         exp->exp_connecting = 0;
                         exp->exp_in_recovery = 0;
-                        spin_unlock(&exp->exp_lock);
+                        cfs_spin_unlock(&exp->exp_lock);
                         obd->obd_max_recoverable_clients++;
                         class_export_put(exp);
                 }
@@ -360,10 +322,10 @@ static int mdt_clients_data_init(const struct lu_env *env,
                 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
                        cl_idx, last_transno);
                 /* protect __u64 value update */
-                spin_lock(&mdt->mdt_transno_lock);
+                cfs_spin_lock(&mdt->mdt_transno_lock);
                 mdt->mdt_last_transno = max(last_transno,
                                             mdt->mdt_last_transno);
-                spin_unlock(&mdt->mdt_transno_lock);
+                cfs_spin_unlock(&mdt->mdt_transno_lock);
         }
 
 err_client:
@@ -415,9 +377,10 @@ static int mdt_server_data_init(const struct lu_env *env,
                 lsd->lsd_server_size = LR_SERVER_SIZE;
                 lsd->lsd_client_start = LR_CLIENT_START;
                 lsd->lsd_client_size = LR_CLIENT_SIZE;
+                lsd->lsd_feature_compat = OBD_COMPAT_MDT;
                 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
                 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
-                                                       OBD_INCOMPAT_COMMON_LR;
+                                            OBD_INCOMPAT_COMMON_LR;
         } else {
                 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
                 rc = mdt_last_rcvd_header_read(env, mdt);
@@ -432,20 +395,47 @@ static int mdt_server_data_init(const struct lu_env *env,
                                            obd->obd_uuid.uuid, lsd->lsd_uuid);
                         GOTO(out, rc = -EINVAL);
                 }
+                lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
+                lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
+                                             OBD_INCOMPAT_COMMON_LR;
         }
         mount_count = lsd->lsd_mount_count;
 
         ldd = lsi->lsi_ldd;
 
+        if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
+                CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
+                       obd->obd_name,
+                       lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
+                GOTO(out, rc = -EINVAL);
+        }
+        if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
+                CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
+                       obd->obd_name,
+                       lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
+                /* XXX: Do something like remount filesystem read-only */
+                GOTO(out, rc = -EINVAL);
+        }
+        /** Interop: evict all clients at first boot with 1.8 last_rcvd */
+        if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
+                LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, remove all"
+                              " clients for interop needs\n", obd->obd_name);
+                simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
+                                lsi->lsi_srv_mnt, LAST_RCVD,
+                                lsd->lsd_client_start);
+                last_rcvd_size = lsd->lsd_client_start;
+                /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
+                lsd->lsd_feature_compat |= OBD_COMPAT_20;
+        }
+
         if (ldd->ldd_flags & LDD_F_IAM_DIR)
                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
 
-        lsd->lsd_feature_compat = OBD_COMPAT_MDT;
         lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
 
-        spin_lock(&mdt->mdt_transno_lock);
+        cfs_spin_lock(&mdt->mdt_transno_lock);
         mdt->mdt_last_transno = lsd->lsd_last_transno;
-        spin_unlock(&mdt->mdt_transno_lock);
+        cfs_spin_unlock(&mdt->mdt_transno_lock);
 
         CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
@@ -476,18 +466,17 @@ static int mdt_server_data_init(const struct lu_env *env,
         if (rc)
                 GOTO(err_client, rc);
 
-        spin_lock(&mdt->mdt_transno_lock);
+        cfs_spin_lock(&mdt->mdt_transno_lock);
         /* obd_last_committed is used for compatibility
          * with other lustre recovery code */
         obd->obd_last_committed = mdt->mdt_last_transno;
-        spin_unlock(&mdt->mdt_transno_lock);
+        cfs_spin_unlock(&mdt->mdt_transno_lock);
 
         mdt->mdt_mount_count = mount_count + 1;
         lsd->lsd_mount_count = mdt->mdt_mount_count;
 
         /* save it, so mount count and last_transno is current */
-        rc = mdt_server_data_update(env, mdt, (mti->mti_exp &&
-                                               mti->mti_exp->exp_need_sync));
+        rc = mdt_server_data_update(env, mdt);
         if (rc)
                 GOTO(err_client, rc);
 
@@ -500,28 +489,32 @@ out:
 }
 
 static int mdt_server_data_update(const struct lu_env *env,
-                                  struct mdt_device *mdt,
-                                  int need_sync)
+                                  struct mdt_device *mdt)
 {
-        int rc = 0;
-        ENTRY;
+        struct mdt_thread_info *mti;
+        struct thandle *th;
+        int rc;
+
+        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+
+        mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
+        th = mdt_trans_start(env, mdt);
+        if (IS_ERR(th))
+                RETURN(PTR_ERR(th));
 
         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
                mdt->mdt_mount_count, mdt->mdt_last_transno);
 
-        spin_lock(&mdt->mdt_transno_lock);
+        cfs_spin_lock(&mdt->mdt_transno_lock);
         mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
-        spin_unlock(&mdt->mdt_transno_lock);
+        cfs_spin_unlock(&mdt->mdt_transno_lock);
 
-        /*
-         * This may be called from difficult reply handler and
-         * mdt->mdt_last_rcvd may be NULL that time.
-         */
-        if (mdt->mdt_last_rcvd != NULL)
-                rc = mdt_last_rcvd_header_write(env, mdt, need_sync);
-        RETURN(rc);
+        rc = mdt_last_rcvd_header_write(env, mdt, th);
+        mdt_trans_stop(env, mdt, th);
+        return rc;
 }
 
+
 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
 {
         unsigned long *bitmap = mdt->mdt_client_bitmap;
@@ -549,17 +542,17 @@ int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
          * there's no need for extra complication here
          */
-        spin_lock(&mdt->mdt_client_bitmap_lock);
-        cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
+        cfs_spin_lock(&mdt->mdt_client_bitmap_lock);
+        cl_idx = cfs_find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
         if (cl_idx >= LR_MAX_CLIENTS ||
             OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
                 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
                        cl_idx);
-                spin_unlock(&mdt->mdt_client_bitmap_lock);
+                cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
                 RETURN(-EOVERFLOW);
         }
-        set_bit(cl_idx, bitmap);
-        spin_unlock(&mdt->mdt_client_bitmap_lock);
+        cfs_set_bit(cl_idx, bitmap);
+        cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
 
         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
                cl_idx, med->med_lcd->lcd_uuid);
@@ -567,12 +560,16 @@ int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
         med->med_lr_idx = cl_idx;
         med->med_lr_off = lsd->lsd_client_start +
                           (cl_idx * lsd->lsd_client_size);
-        init_mutex(&med->med_lcd_lock);
+        cfs_init_mutex(&med->med_lcd_lock);
 
         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
 
         /* Write new client data. */
         off = med->med_lr_off;
+
+        if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
+                RETURN(-ENOSPC);
+
         mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
 
         th = mdt_trans_start(env, mdt);
@@ -585,14 +582,14 @@ int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
          * transaction so that many connecting clients will not bring
          * server down with lots of sync writes.
          */
-        mdt_trans_add_cb(th, lut_cb_client, mti->mti_exp);
-        spin_lock(&mti->mti_exp->exp_lock);
+        mdt_trans_add_cb(th, lut_cb_client, class_export_cb_get(mti->mti_exp));
+        cfs_spin_lock(&mti->mti_exp->exp_lock);
         mti->mti_exp->exp_need_sync = 1;
-        spin_unlock(&mti->mti_exp->exp_lock);
+        cfs_spin_unlock(&mti->mti_exp->exp_lock);
 
         rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
-        CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
-               cl_idx, med->med_lr_off, sizeof(*lcd));
+        CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
+               cl_idx, med->med_lr_off, (int)sizeof(*lcd));
         mdt_trans_stop(env, mdt, th);
 
         RETURN(rc);
@@ -628,13 +625,13 @@ int mdt_client_add(const struct lu_env *env,
         if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
                 RETURN(0);
 
-        spin_lock(&mdt->mdt_client_bitmap_lock);
-        if (test_and_set_bit(cl_idx, bitmap)) {
+        cfs_spin_lock(&mdt->mdt_client_bitmap_lock);
+        if (cfs_test_and_set_bit(cl_idx, bitmap)) {
                 CERROR("MDS client %d: bit already set in bitmap!!\n",
                        cl_idx);
                 LBUG();
         }
-        spin_unlock(&mdt->mdt_client_bitmap_lock);
+        cfs_spin_unlock(&mdt->mdt_client_bitmap_lock);
 
         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
                cl_idx, med->med_lcd->lcd_uuid);
@@ -642,7 +639,7 @@ int mdt_client_add(const struct lu_env *env,
         med->med_lr_idx = cl_idx;
         med->med_lr_off = lsd->lsd_client_start +
                           (cl_idx * lsd->lsd_client_size);
-        init_mutex(&med->med_lcd_lock);
+        cfs_init_mutex(&med->med_lcd_lock);
 
         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
 
@@ -657,7 +654,6 @@ int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
         struct obd_device      *obd = mdt2obd_dev(mdt);
         struct obd_export      *exp;
         struct thandle         *th;
-        int                     need_sync;
         loff_t                  off;
         int                     rc = 0;
         ENTRY;
@@ -694,67 +690,32 @@ int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
          * Clear the bit _after_ zeroing out the client so we don't race with
          * mdt_client_add and zero out new clients.
          */
-        if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
+        if (!cfs_test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
                 CERROR("MDT client %u: bit already clear in bitmap!!\n",
                        med->med_lr_idx);
                 LBUG();
         }
 
-        /* Don't force sync on disconnect if aborting recovery,
-         * or it does num_clients * num_osts.  b=17194 */
-        need_sync = (!exp->exp_libclient || exp->exp_need_sync) &&
-                     !(exp->exp_flags & OBD_OPT_ABORT_RECOV);
+        /* Make sure the server's last_transno is up to date.
+         * This should be done before zeroing client slot so last_transno will
+         * be in server data or in client data in case of failure */
+        mdt_server_data_update(env, mdt);
 
-        /*
-         * This may be called from difficult reply handler path and
-         * mdt->mdt_last_rcvd may be NULL that time.
-         */
-        if (mdt->mdt_last_rcvd != NULL) {
-                mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
-
-                spin_lock(&exp->exp_lock);
-                exp->exp_need_sync = need_sync;
-                spin_unlock(&exp->exp_lock);
-
-                th = mdt_trans_start(env, mdt);
-                if (IS_ERR(th))
-                        GOTO(free, rc = PTR_ERR(th));
-
-                if (need_sync) {
-                        /*
-                         * Until this operations will be committed the sync
-                         * is needed for this export.
-                         */
-                        mdt_trans_add_cb(th, lut_cb_client, exp);
-                }
-
-                mutex_down(&med->med_lcd_lock);
-                memset(lcd, 0, sizeof *lcd);
+        mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
+        th = mdt_trans_start(env, mdt);
+        if (IS_ERR(th))
+                GOTO(free, rc = PTR_ERR(th));
 
-                rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
-                mutex_up(&med->med_lcd_lock);
-                mdt_trans_stop(env, mdt, th);
-        }
+        cfs_mutex_down(&med->med_lcd_lock);
+        memset(lcd->lcd_uuid, 0, sizeof lcd->lcd_uuid);
+        rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
+        cfs_mutex_up(&med->med_lcd_lock);
+        mdt_trans_stop(env, mdt, th);
 
         CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
-               "%s %ssync rc %d\n",  med->med_lr_idx, LAST_RCVD, 
-               need_sync ? "" : "a", rc);
-
-        spin_lock(&mdt->mdt_client_bitmap_lock);
-        clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
-        spin_unlock(&mdt->mdt_client_bitmap_lock);
-
-        /*
-         * Make sure the server's last_transno is up to date. Do this
-         * after the client is freed so we know all the client's
-         * transactions have been committed.
-         */
-        mdt_server_data_update(env, mdt, need_sync);
-
-        EXIT;
+               "%s, rc %d\n",  med->med_lr_idx, LAST_RCVD, rc);
+        RETURN(0);
 free:
-        OBD_FREE_PTR(lcd);
-        med->med_lcd = NULL;
         return 0;
 }
 
@@ -778,19 +739,21 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
         LASSERT(mdt);
         med = &req->rq_export->exp_mdt_data;
         LASSERT(med);
+
+        cfs_mutex_down(&med->med_lcd_lock);
         lcd = med->med_lcd;
-        /* if the export has already been failed, we have no last_rcvd slot */
-        if (req->rq_export->exp_failed) {
+        /* if the export has already been disconnected, we have no last_rcvd slot,
+         * update server data with latest transno then */
+        if (lcd == NULL) {
+                cfs_mutex_up(&med->med_lcd_lock);
                 CWARN("commit transaction for disconnected client %s: rc %d\n",
                       req->rq_export->exp_client_uuid.uuid, rc);
-                if (rc == 0)
-                        rc = -ENOTCONN;
-                RETURN(rc);
+                err = mdt_last_rcvd_header_write(mti->mti_env, mdt, th);
+                RETURN(err);
         }
 
         off = med->med_lr_off;
         LASSERT(ergo(mti->mti_transno == 0, rc != 0));
-        mutex_down(&med->med_lcd_lock);
         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
                 if (mti->mti_transno != 0)
@@ -822,7 +785,7 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
         } else {
                 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
         }
-        mutex_up(&med->med_lcd_lock);
+        cfs_mutex_up(&med->med_lcd_lock);
         RETURN(err);
 }
 
@@ -879,7 +842,7 @@ static int mdt_txn_stop_cb(const struct lu_env *env,
         }
 
         mti->mti_has_trans = 1;
-        spin_lock(&mdt->mdt_transno_lock);
+        cfs_spin_lock(&mdt->mdt_transno_lock);
         if (txn->th_result != 0) {
                 if (mti->mti_transno != 0) {
                         CERROR("Replay transno "LPU64" failed: rc %i\n",
@@ -892,7 +855,7 @@ static int mdt_txn_stop_cb(const struct lu_env *env,
                 if (mti->mti_transno > mdt->mdt_last_transno)
                         mdt->mdt_last_transno = mti->mti_transno;
         }
-        spin_unlock(&mdt->mdt_transno_lock);
+        cfs_spin_unlock(&mdt->mdt_transno_lock);
         /* sometimes the reply message has not been successfully packed */
         LASSERT(req != NULL && req->rq_repmsg != NULL);
 
@@ -906,15 +869,13 @@ static int mdt_txn_stop_cb(const struct lu_env *env,
 
         req->rq_transno = mti->mti_transno;
         lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
-        lustre_msg_set_last_xid(req->rq_repmsg,
-                         lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
         /* save transno for the commit callback */
         txi->txi_transno = mti->mti_transno;
 
         /* add separate commit callback for transaction handling because we need
          * export as parameter */
         mdt_trans_add_cb(txn, lut_cb_last_committed,
-                         class_export_get(mti->mti_exp));
+                         class_export_cb_get(mti->mti_exp));
 
         return mdt_last_rcvd_update(mti, txn);
 }
@@ -1000,36 +961,35 @@ void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
 {
         struct obd_export         *exp = req->rq_export;
-        struct list_head          *tmp;
+        cfs_list_t                *tmp;
         struct ptlrpc_reply_state *oldrep;
         struct ptlrpc_service     *svc;
         int                        i;
 
         /* CAVEAT EMPTOR: spinlock order */
-        spin_lock(&exp->exp_lock);
-        list_for_each (tmp, &exp->exp_outstanding_replies) {
-                oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
+        cfs_spin_lock(&exp->exp_lock);
+        cfs_list_for_each (tmp, &exp->exp_outstanding_replies) {
+                oldrep = cfs_list_entry(tmp, struct ptlrpc_reply_state,
+                                        rs_exp_list);
 
                 if (oldrep->rs_xid != req->rq_xid)
                         continue;
 
-                if (lustre_msg_get_opc(oldrep->rs_msg) !=
-                    lustre_msg_get_opc(req->rq_reqmsg))
+                if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
                         CERROR ("Resent req xid "LPU64" has mismatched opc: "
                                 "new %d old %d\n", req->rq_xid,
                                 lustre_msg_get_opc(req->rq_reqmsg),
-                                lustre_msg_get_opc(oldrep->rs_msg));
+                                oldrep->rs_opc);
 
                 svc = oldrep->rs_service;
-                spin_lock (&svc->srv_lock);
+                cfs_spin_lock (&svc->srv_lock);
 
-                list_del_init (&oldrep->rs_exp_list);
+                cfs_list_del_init (&oldrep->rs_exp_list);
 
                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
                       " o%d NID %s\n",
                       oldrep->rs_nlocks, oldrep,
-                      oldrep->rs_xid, oldrep->rs_transno,
-                      lustre_msg_get_opc(oldrep->rs_msg),
+                      oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
 
                 for (i = 0; i < oldrep->rs_nlocks; i++)
@@ -1038,14 +998,14 @@ static void mdt_steal_ack_locks(struct ptlrpc_request *req)
                 oldrep->rs_nlocks = 0;
 
                 DEBUG_REQ(D_HA, req, "stole locks for");
-                spin_lock(&oldrep->rs_lock);
+                cfs_spin_lock(&oldrep->rs_lock);
                 ptlrpc_schedule_difficult_reply (oldrep);
-                spin_unlock(&oldrep->rs_lock);
+                cfs_spin_unlock(&oldrep->rs_lock);
 
-                spin_unlock (&svc->srv_lock);
+                cfs_spin_unlock (&svc->srv_lock);
                 break;
         }
-        spin_unlock(&exp->exp_lock);
+        cfs_spin_unlock(&exp->exp_lock);
 }
 
 /**
@@ -1171,19 +1131,19 @@ static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
         mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
         mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
                            mdt_object_fid(obj));
-        if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
+        if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
                 struct mdt_file_data *mfd;
                 struct mdt_body *repbody;
 
                 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
                 repbody->ioepoch = obj->mot_ioepoch;
-                spin_lock(&med->med_open_lock);
-                list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
+                cfs_spin_lock(&med->med_open_lock);
+                cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
                         if (mfd->mfd_xid == req->rq_xid)
                                 break;
                 }
                 LASSERT(&mfd->mfd_list != &med->med_open_head);
-                spin_unlock(&med->med_open_lock);
+                cfs_spin_unlock(&med->med_open_lock);
                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
         }