Whamcloud - gitweb
LU-911 lut: few more methods from orion are taken
[fs/lustre-release.git] / lustre / mdt / mdt_recovery.c
index e125d5b..b65f73d 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -28,6 +26,8 @@
 /*
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -48,9 +48,6 @@
 
 #include "mdt_internal.h"
 
-static int mdt_server_data_update(const struct lu_env *env,
-                                  struct mdt_device *mdt);
-
 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
 {
         struct lu_buf *buf;
@@ -77,163 +74,10 @@ const struct lu_buf *mdt_buf_const(const struct lu_env *env,
         return buf;
 }
 
-static inline int mdt_trans_credit_get(const struct lu_env *env,
-                                       struct mdt_device *mdt,
-                                       enum mdt_txn_op op)
-{
-        struct dt_device *dev = mdt->mdt_bottom;
-        int cr;
-        switch (op) {
-                case MDT_TXN_CAPA_KEYS_WRITE_OP:
-                case MDT_TXN_LAST_RCVD_WRITE_OP:
-                        cr = dev->dd_ops->dt_credit_get(env,
-                                                        dev,
-                                                        DTO_WRITE_BLOCK);
-                break;
-                default:
-                        LBUG();
-        }
-        return cr;
-}
-
-void mdt_trans_credit_init(const struct lu_env *env,
-                           struct mdt_device *mdt,
-                           enum mdt_txn_op op)
-{
-        struct mdt_thread_info *mti;
-        struct txn_param *p;
-        int cr;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        p = &mti->mti_txn_param;
-
-        cr = mdt_trans_credit_get(env, mdt, op);
-        txn_param_init(p, cr);
-}
-
-struct thandle* mdt_trans_start(const struct lu_env *env,
-                                struct mdt_device *mdt)
-{
-        struct mdt_thread_info *mti;
-        struct txn_param *p;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        p = &mti->mti_txn_param;
-
-        /* export can require sync operations */
-        if (mti->mti_exp != NULL)
-                p->tp_sync = mti->mti_exp->exp_need_sync;
-
-        return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
-}
-
 void mdt_trans_stop(const struct lu_env *env,
                     struct mdt_device *mdt, struct thandle *th)
 {
-        mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
-}
-
-static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
-                                            struct mdt_device *mdt)
-{
-        struct mdt_thread_info *mti;
-        int rc;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-
-        mti->mti_off = 0;
-        rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
-                            mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
-                            &mti->mti_off);
-        if (rc == 0)
-                lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lut.lut_lsd);
-
-        CDEBUG(D_INFO, "read last_rcvd header rc = %d, uuid = %s, "
-               "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
-               mdt->mdt_lut.lut_lsd.lsd_last_transno);
-        return rc;
-}
-
-static int mdt_last_rcvd_header_write(const struct lu_env *env,
-                                      struct mdt_device *mdt,
-                                      struct thandle *th)
-{
-        struct mdt_thread_info *mti;
-        int rc;
-        ENTRY;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-
-        mti->mti_off = 0;
-        lsd_cpu_to_le(&mdt->mdt_lut.lut_lsd, &mti->mti_lsd);
-
-        rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
-                             mdt_buf_const(env, &mti->mti_lsd,
-                                           sizeof(mti->mti_lsd)),
-                             &mti->mti_off, th);
-
-        CDEBUG(D_INFO, "write last_rcvd header rc = %d, uuid = %s, "
-               "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
-               mdt->mdt_lut.lut_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, 
-                              int index)
-{
-        struct mdt_thread_info *mti;
-        struct lsd_client_data *tmp;
-        int rc;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        tmp = &mti->mti_lcd;
-        rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
-                            mdt_buf(env, tmp, sizeof(*tmp)), off);
-        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, 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;
-}
-
-static int mdt_last_rcvd_write(const struct lu_env *env,
-                               struct mdt_device *mdt,
-                               struct lsd_client_data *lcd,
-                               loff_t *off, struct thandle *th)
-{
-        struct mdt_thread_info *mti;
-        struct lsd_client_data *tmp;
-        int rc;
-
-        LASSERT(th != NULL);
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        tmp = &mti->mti_lcd;
-
-        lcd_cpu_to_le(lcd, tmp);
-
-        rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
-                             mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
-
-        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;
+        dt_trans_stop(env, mdt->mdt_bottom, th);
 }
 
 static int mdt_clients_data_init(const struct lu_env *env,
@@ -265,7 +109,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, cl_idx);
+               rc = lut_client_data_read(env, &mdt->mdt_lut, lcd, &off, cl_idx);
                 if (rc) {
                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
                                LAST_RCVD, cl_idx, off, rc);
@@ -305,7 +149,7 @@ static int mdt_clients_data_init(const struct lu_env *env,
                 mti->mti_exp = exp;
                 /* copy on-disk lcd to the export */
                 *exp->exp_target_data.ted_lcd = *lcd;
-                rc = mdt_client_add(env, mdt, cl_idx);
+               rc = lut_client_add(env, exp, cl_idx);
                 /* can't fail existing */
                 LASSERTF(rc == 0, "rc = %d\n", rc);
                 /* VBR: set export last committed version */
@@ -341,7 +185,6 @@ static int mdt_server_data_init(const struct lu_env *env,
         struct mdt_thread_info *mti;
         struct dt_object       *obj;
         struct lu_attr         *la;
-        struct lustre_disk_data  *ldd;
         unsigned long last_rcvd_size;
         __u64 mount_count;
         int rc;
@@ -358,7 +201,7 @@ static int mdt_server_data_init(const struct lu_env *env,
         la = &mti->mti_attr.ma_attr;
 
         obj = mdt->mdt_lut.lut_last_rcvd;
-        rc = obj->do_ops->do_attr_get(env, obj, la, BYPASS_CAPA);
+       rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
         if (rc)
                 RETURN(rc);
 
@@ -377,10 +220,11 @@ static int mdt_server_data_init(const struct lu_env *env,
                 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 |
+                                            OBD_INCOMPAT_MULTI_OI;
         } else {
                 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
-                rc = mdt_last_rcvd_header_read(env, mdt);
+               rc = lut_server_data_read(env, &mdt->mdt_lut);
                 if (rc) {
                         CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
                         GOTO(out, rc);
@@ -398,8 +242,6 @@ static int mdt_server_data_init(const struct lu_env *env,
         }
         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,
@@ -419,16 +261,17 @@ static int mdt_server_data_init(const struct lu_env *env,
                         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);
+                       rc = lut_truncate_last_rcvd(env, &mdt->mdt_lut,
+                                                   lsd->lsd_client_start);
+                       if (rc)
+                               GOTO(out, rc);
                         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)
+       if (lsi->lsi_ldd->ldd_flags & LDD_F_IAM_DIR)
                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
 
         lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
@@ -473,10 +316,11 @@ static int mdt_server_data_init(const struct lu_env *env,
         cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
 
         obd->u.obt.obt_mount_count = mount_count + 1;
+        obd->u.obt.obt_instance = (__u32)obd->u.obt.obt_mount_count;
         lsd->lsd_mount_count = obd->u.obt.obt_mount_count;
 
         /* save it, so mount count and last_transno is current */
-        rc = mdt_server_data_update(env, mdt);
+       rc = lut_server_data_update(env, &mdt->mdt_lut, 0);
         if (rc)
                 GOTO(err_client, rc);
 
@@ -488,234 +332,6 @@ out:
         return rc;
 }
 
-static int mdt_server_data_update(const struct lu_env *env,
-                                  struct mdt_device *mdt)
-{
-        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_lut.lut_obd->u.obt.obt_mount_count,
-               mdt->mdt_lut.lut_last_transno);
-
-        cfs_spin_lock(&mdt->mdt_lut.lut_translock);
-        mdt->mdt_lut.lut_lsd.lsd_last_transno = mdt->mdt_lut.lut_last_transno;
-        cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
-
-        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_lut.lut_client_bitmap;
-        struct mdt_thread_info *mti;
-        struct tg_export_data *ted;
-        struct lr_server_data  *lsd = &mdt->mdt_lut.lut_lsd;
-        struct obd_device *obd = mdt2obd_dev(mdt);
-        struct thandle *th;
-        loff_t off;
-        int rc;
-        int cl_idx;
-        ENTRY;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        LASSERT(mti != NULL);
-
-        ted = &mti->mti_exp->exp_target_data;
-
-        LASSERT(bitmap != NULL);
-        if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
-                RETURN(0);
-
-        /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
-         * there's no need for extra complication here
-         */
-        cfs_spin_lock(&mdt->mdt_lut.lut_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);
-                cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
-                RETURN(-EOVERFLOW);
-        }
-        cfs_set_bit(cl_idx, bitmap);
-        cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
-
-        CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
-               cl_idx, ted->ted_lcd->lcd_uuid);
-
-        ted->ted_lr_idx = cl_idx;
-        ted->ted_lr_off = lsd->lsd_client_start +
-                          (cl_idx * lsd->lsd_client_size);
-        cfs_init_mutex(&ted->ted_lcd_lock);
-
-        LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
-
-        /* Write new client data. */
-        off = ted->ted_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);
-        if (IS_ERR(th))
-                RETURN(PTR_ERR(th));
-
-        /*
-         * Until this operations will be committed the sync is needed
-         * for this export. This should be done _after_ starting the
-         * transaction so that many connecting clients will not bring
-         * server down with lots of sync writes.
-         */
-        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;
-        cfs_spin_unlock(&mti->mti_exp->exp_lock);
-
-        rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
-        CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
-               cl_idx, ted->ted_lr_off, (int)sizeof(*(ted->ted_lcd)));
-        mdt_trans_stop(env, mdt, th);
-
-        RETURN(rc);
-}
-
-/* Add client data to the MDS.  We use a bitmap to locate a free space
- * in the last_rcvd file if cl_off is -1 (i.e. a new client).
- * Otherwise, we just have to read the data from the last_rcvd file and
- * we know its offset.
- *
- * It should not be possible to fail adding an existing client - otherwise
- * mdt_init_server_data() callsite needs to be fixed.
- */
-int mdt_client_add(const struct lu_env *env,
-                   struct mdt_device *mdt, int cl_idx)
-{
-        struct mdt_thread_info *mti;
-        struct tg_export_data  *ted;
-        unsigned long *bitmap = mdt->mdt_lut.lut_client_bitmap;
-        struct obd_device *obd = mdt2obd_dev(mdt);
-        struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
-        int rc = 0;
-        ENTRY;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        LASSERT(mti != NULL);
-
-        ted = &mti->mti_exp->exp_target_data;
-
-        LASSERT(bitmap != NULL);
-        LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
-
-        if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
-                RETURN(0);
-
-        cfs_spin_lock(&mdt->mdt_lut.lut_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();
-        }
-        cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
-
-        CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
-               cl_idx, ted->ted_lcd->lcd_uuid);
-
-        ted->ted_lr_idx = cl_idx;
-        ted->ted_lr_off = lsd->lsd_client_start +
-                          (cl_idx * lsd->lsd_client_size);
-        cfs_init_mutex(&ted->ted_lcd_lock);
-
-        LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
-
-        RETURN(rc);
-}
-
-int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
-{
-        struct mdt_thread_info *mti;
-        struct tg_export_data  *ted;
-        struct obd_device      *obd = mdt2obd_dev(mdt);
-        struct obd_export      *exp;
-        struct thandle         *th;
-        loff_t                  off;
-        int                     rc = 0;
-        ENTRY;
-
-        mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
-        LASSERT(mti != NULL);
-
-        exp = mti->mti_exp;
-        ted = &exp->exp_target_data;
-        if (!ted->ted_lcd)
-                RETURN(0);
-
-        /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
-        if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
-                GOTO(free, 0);
-
-        CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
-               ted->ted_lr_idx, ted->ted_lr_off);
-
-        off = ted->ted_lr_off;
-
-        /*
-         * Don't clear ted_lr_idx here as it is likely also unset.  At worst we
-         * leak a client slot that will be cleaned on the next recovery.
-         */
-        if (off <= 0) {
-                CERROR("client idx %d has offset %lld\n",
-                        ted->ted_lr_idx, off);
-                GOTO(free, rc = -EINVAL);
-        }
-
-        /*
-         * Clear the bit _after_ zeroing out the client so we don't race with
-         * mdt_client_add and zero out new clients.
-         */
-        if (!cfs_test_bit(ted->ted_lr_idx, mdt->mdt_lut.lut_client_bitmap)) {
-                CERROR("MDT client %u: bit already clear in bitmap!!\n",
-                       ted->ted_lr_idx);
-                LBUG();
-        }
-
-        /* 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);
-
-        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));
-
-        cfs_mutex_down(&ted->ted_lcd_lock);
-        memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
-        rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
-        cfs_mutex_up(&ted->ted_lcd_lock);
-        mdt_trans_stop(env, mdt, th);
-
-        CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
-               "%s, rc %d\n",  ted->ted_lr_idx, LAST_RCVD, rc);
-        RETURN(0);
-free:
-        return 0;
-}
-
 /*
  * last_rcvd & last_committed update callbacks
  */
@@ -737,15 +353,15 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
         ted = &req->rq_export->exp_target_data;
         LASSERT(ted);
 
-        cfs_mutex_down(&ted->ted_lcd_lock);
+        cfs_mutex_lock(&ted->ted_lcd_lock);
         lcd = ted->ted_lcd;
         /* 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(&ted->ted_lcd_lock);
+                cfs_mutex_unlock(&ted->ted_lcd_lock);
                 CWARN("commit transaction for disconnected client %s: rc %d\n",
                       req->rq_export->exp_client_uuid.uuid, rc);
-                err = mdt_last_rcvd_header_write(mti->mti_env, mdt, th);
+               err = lut_server_data_write(mti->mti_env, &mdt->mdt_lut, th);
                 RETURN(err);
         }
 
@@ -753,8 +369,23 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
         LASSERT(ergo(mti->mti_transno == 0, rc != 0));
         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)
+                if (mti->mti_transno != 0) {
+                        if (lcd->lcd_last_close_transno > mti->mti_transno) {
+                                CERROR("Trying to overwrite bigger transno:"
+                                       "on-disk: "LPU64", new: "LPU64" "
+                                       "replay: %d. see LU-617.\n",
+                                       lcd->lcd_last_close_transno,
+                                       mti->mti_transno, req_is_replay(req));
+                                if (req_is_replay(req)) {
+                                        cfs_spin_lock(&req->rq_export->exp_lock);
+                                        req->rq_export->exp_vbr_failed = 1;
+                                        cfs_spin_unlock(&req->rq_export->exp_lock);
+                                }
+                                cfs_mutex_unlock(&ted->ted_lcd_lock);
+                                RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
+                        }
                         lcd->lcd_last_close_transno = mti->mti_transno;
+                }
                 lcd->lcd_last_close_xid = req->rq_xid;
                 lcd->lcd_last_close_result = rc;
         } else {
@@ -766,8 +397,23 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
                         lcd->lcd_pre_versions[2] = pre_versions[2];
                         lcd->lcd_pre_versions[3] = pre_versions[3];
                 }
-                if (mti->mti_transno != 0)
+                if (mti->mti_transno != 0) {
+                        if (lcd->lcd_last_transno > mti->mti_transno) {
+                                CERROR("Trying to overwrite bigger transno:"
+                                       "on-disk: "LPU64", new: "LPU64" "
+                                       "replay: %d. see LU-617.\n",
+                                       lcd->lcd_last_transno,
+                                       mti->mti_transno, req_is_replay(req));
+                                if (req_is_replay(req)) {
+                                        cfs_spin_lock(&req->rq_export->exp_lock);
+                                        req->rq_export->exp_vbr_failed = 1;
+                                        cfs_spin_unlock(&req->rq_export->exp_lock);
+                                }
+                                cfs_mutex_unlock(&ted->ted_lcd_lock);
+                                RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
+                        }
                         lcd->lcd_last_transno = mti->mti_transno;
+                }
                 lcd->lcd_last_xid = req->rq_xid;
                 lcd->lcd_last_result = rc;
                 /*XXX: save intent_disposition in mdt_thread_info?
@@ -780,9 +426,10 @@ static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
                 CERROR("client idx %d has offset %lld\n", ted->ted_lr_idx, off);
                 err = -EINVAL;
         } else {
-                err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
+               err = lut_client_data_write(mti->mti_env, &mdt->mdt_lut, lcd,
+                                           &off, th);
         }
-        cfs_mutex_up(&ted->ted_lcd_lock);
+        cfs_mutex_unlock(&ted->ted_lcd_lock);
         RETURN(err);
 }
 
@@ -790,23 +437,35 @@ extern struct lu_context_key mdt_thread_key;
 
 /* add credits for last_rcvd update */
 static int mdt_txn_start_cb(const struct lu_env *env,
-                            struct txn_param *param, void *cookie)
+                           struct thandle *th, void *cookie)
 {
-        struct mdt_device *mdt = cookie;
+       struct mdt_device *mdt = cookie;
+       struct mdt_thread_info *mti;
+       int rc;
+       ENTRY;
 
-        param->tp_credits += mdt_trans_credit_get(env, mdt,
-                                                  MDT_TXN_LAST_RCVD_WRITE_OP);
-        return 0;
-}
+       mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
 
-/* Set new object versions */
-static void mdt_version_set(struct mdt_thread_info *info)
-{
-        if (info->mti_mos != NULL) {
-                mo_version_set(info->mti_env, mdt_object_child(info->mti_mos),
-                               info->mti_transno);
-                info->mti_mos = NULL;
-        }
+       LASSERT(mdt->mdt_lut.lut_last_rcvd);
+       if (mti->mti_exp == NULL)
+               RETURN(0);
+
+       rc = dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
+                                    sizeof(struct lsd_client_data),
+                                    mti->mti_exp->exp_target_data.ted_lr_off,
+                                    th);
+       if (rc)
+               return rc;
+
+       rc = dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
+                                    sizeof(struct lr_server_data), 0, th);
+       if (rc)
+               return rc;
+
+       if (mti->mti_mos != NULL)
+               rc = dt_declare_version_set(env, mdt_obj2dt(mti->mti_mos), th);
+
+       return rc;
 }
 
 /* Update last_rcvd records with latests transaction data */
@@ -814,17 +473,13 @@ static int mdt_txn_stop_cb(const struct lu_env *env,
                            struct thandle *txn, void *cookie)
 {
         struct mdt_device *mdt = cookie;
-        struct mdt_txn_info *txi;
         struct mdt_thread_info *mti;
         struct ptlrpc_request *req;
 
-        /* transno in two contexts - for commit_cb and for thread */
-        txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
         req = mdt_info_req(mti);
 
         if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
-                txi->txi_transno = 0;
                 mti->mti_no_need_trans = 0;
                 return 0;
         }
@@ -856,8 +511,11 @@ static int mdt_txn_stop_cb(const struct lu_env *env,
         LASSERT(req != NULL && req->rq_repmsg != NULL);
 
         /** VBR: set new versions */
-        if (txn->th_result == 0)
-                mdt_version_set(mti);
+        if (txn->th_result == 0 && mti->mti_mos != NULL) {
+                dt_version_set(env, mdt_obj2dt(mti->mti_mos),
+                               mti->mti_transno, txn);
+                mti->mti_mos = NULL;
+        }
 
         /* filling reply data */
         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
@@ -865,35 +523,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);
-        /* 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_cb_get(mti->mti_exp));
-
+        /* if can't add callback, do sync write */
+        txn->th_sync |= !!lut_last_commit_cb_add(txn, &mdt->mdt_lut,
+                                                 mti->mti_exp,
+                                                 mti->mti_transno);
         return mdt_last_rcvd_update(mti, txn);
 }
 
-/* commit callback, need to update last_committed value */
-static int mdt_txn_commit_cb(const struct lu_env *env,
-                             struct thandle *txn, void *cookie)
-{
-        struct mdt_device *mdt = cookie;
-        struct mdt_txn_info *txi;
-        int i;
-
-        txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
-
-        /* iterate through all additional callbacks */
-        for (i = 0; i < txi->txi_cb_count; i++) {
-                txi->txi_cb[i].lut_cb_func(&mdt->mdt_lut, txi->txi_transno,
-                                           txi->txi_cb[i].lut_cb_data, 0);
-        }
-        return 0;
-}
-
 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
                  struct obd_device *obd,
                  struct lustre_sb_info *lsi)
@@ -909,7 +545,7 @@ int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
         /* prepare transactions callbacks */
         mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
         mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
-        mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
+        mdt->mdt_txn_cb.dtc_txn_commit = NULL;
         mdt->mdt_txn_cb.dtc_cookie = mdt;
         mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
         CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);