Whamcloud - gitweb
LU-5099 api: transfer object type via dt_insert API
[fs/lustre-release.git] / lustre / target / out_handler.c
index efc8b41..78e55f4 100644 (file)
@@ -22,7 +22,7 @@
 /*
  * Copyright (c) 2013, Intel Corporation.
  *
- * lustre/mdt/out_handler.c
+ * lustre/target/out_handler.c
  *
  * Object update handler between targets.
  *
 #include "tgt_internal.h"
 #include <lustre_update.h>
 
-struct tx_arg *tx_add_exec(struct thandle_exec_args *ta, tx_exec_func_t func,
-                          tx_exec_func_t undo, char *file, int line)
+static int tx_extend_args(struct thandle_exec_args *ta, int new_alloc_ta)
 {
+       struct tx_arg   **new_ta;
+       int             i;
+       int             rc = 0;
+
+       if (ta->ta_alloc_args >= new_alloc_ta)
+               return 0;
+
+       OBD_ALLOC(new_ta, sizeof(*new_ta) * new_alloc_ta);
+       if (new_ta == NULL)
+               return -ENOMEM;
+
+       for (i = 0; i < new_alloc_ta; i++) {
+               if (i < ta->ta_alloc_args) {
+                       /* copy the old args to new one */
+                       new_ta[i] = ta->ta_args[i];
+               } else {
+                       OBD_ALLOC_PTR(new_ta[i]);
+                       if (new_ta[i] == NULL)
+                               GOTO(out, rc = -ENOMEM);
+               }
+       }
+
+       /* free the old args */
+       if (ta->ta_args != NULL)
+               OBD_FREE(ta->ta_args, sizeof(ta->ta_args[0]) *
+                                     ta->ta_alloc_args);
+
+       ta->ta_args = new_ta;
+       ta->ta_alloc_args = new_alloc_ta;
+out:
+       if (rc != 0) {
+               for (i = 0; i < new_alloc_ta; i++) {
+                       if (new_ta[i] != NULL)
+                               OBD_FREE_PTR(new_ta[i]);
+               }
+               OBD_FREE(new_ta, sizeof(*new_ta) * new_alloc_ta);
+       }
+       return rc;
+}
+
+#define TX_ALLOC_STEP  8
+static struct tx_arg *tx_add_exec(struct thandle_exec_args *ta,
+                                 tx_exec_func_t func, tx_exec_func_t undo,
+                                 const char *file, int line)
+{
+       int rc;
        int i;
 
-       LASSERT(ta);
-       LASSERT(func);
+       LASSERT(ta != NULL);
+       LASSERT(func != NULL);
+
+       if (ta->ta_argno + 1 >= ta->ta_alloc_args) {
+               rc = tx_extend_args(ta, ta->ta_alloc_args + TX_ALLOC_STEP);
+               if (rc != 0)
+                       return ERR_PTR(rc);
+       }
 
-       LASSERTF(ta->ta_argno + 1 <= TX_MAX_OPS,
-                "Too many updates(%d) in one trans\n", ta->ta_argno);
        i = ta->ta_argno;
 
        ta->ta_argno++;
 
-       ta->ta_args[i].exec_fn = func;
-       ta->ta_args[i].undo_fn = undo;
-       ta->ta_args[i].file    = file;
-       ta->ta_args[i].line    = line;
+       ta->ta_args[i]->exec_fn = func;
+       ta->ta_args[i]->undo_fn = undo;
+       ta->ta_args[i]->file    = file;
+       ta->ta_args[i]->line    = line;
 
-       return &ta->ta_args[i];
+       return ta->ta_args[i];
 }
 
 static void out_reconstruct(const struct lu_env *env, struct dt_device *dt,
@@ -161,19 +210,21 @@ static int __out_tx_create(const struct lu_env *env, struct dt_object *obj,
                           struct dt_object_format *dof,
                           struct thandle_exec_args *ta,
                           struct object_update_reply *reply,
-                          int index, char *file, int line)
+                          int index, const char *file, int line)
 {
        struct tx_arg *arg;
+       int rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_create(env, obj, attr, NULL, dof,
+       rc = dt_declare_create(env, obj, attr, NULL, dof,
                                       ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_create_exec, out_tx_create_undo, file,
                          line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
 
        /* release the object in out_trans_stop */
        lu_object_get(&obj->do_lu);
@@ -211,7 +262,8 @@ static int out_create(struct tgt_session_info *tsi)
                RETURN(err_serious(-EPROTO));
        }
 
-       obdo_le_to_cpu(wobdo, wobdo);
+       if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
+               lustre_swab_obdo(wobdo);
        lustre_get_wire_obdo(NULL, lobdo, wobdo);
        la_from_obdo(attr, lobdo, lobdo->o_valid);
 
@@ -281,18 +333,21 @@ static int __out_tx_attr_set(const struct lu_env *env,
                             const struct lu_attr *attr,
                             struct thandle_exec_args *th,
                             struct object_update_reply *reply,
-                            int index, char *file, int line)
+                            int index, const char *file, int line)
 {
-       struct tx_arg           *arg;
+       struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(th->ta_handle != NULL);
-       th->ta_err = dt_declare_attr_set(env, dt_obj, attr, th->ta_handle);
-       if (th->ta_err != 0)
-               return th->ta_err;
+       rc = dt_declare_attr_set(env, dt_obj, attr, th->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(th, out_tx_attr_set_exec, out_tx_attr_set_undo,
                          file, line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->u.attr_set.attr = *attr;
@@ -323,7 +378,9 @@ static int out_attr_set(struct tgt_session_info *tsi)
 
        attr->la_valid = 0;
        attr->la_valid = 0;
-       obdo_le_to_cpu(wobdo, wobdo);
+
+       if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
+               lustre_swab_obdo(wobdo);
        lustre_get_wire_obdo(NULL, lobdo, wobdo);
        la_from_obdo(attr, lobdo, lobdo->o_valid);
 
@@ -360,46 +417,9 @@ static int out_attr_get(struct tgt_session_info *tsi)
        rc = dt_attr_get(env, obj, la, NULL);
        if (rc)
                GOTO(out_unlock, rc);
-       /*
-        * If it is a directory, we will also check whether the
-        * directory is empty.
-        * la_flags = 0 : Empty.
-        *          = 1 : Not empty.
-        */
-       la->la_flags = 0;
-       if (S_ISDIR(la->la_mode)) {
-               struct dt_it            *it;
-               const struct dt_it_ops  *iops;
-
-               if (!dt_try_as_dir(env, obj))
-                       GOTO(out_unlock, rc = -ENOTDIR);
-
-               iops = &obj->do_index_ops->dio_it;
-               it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
-               if (!IS_ERR(it)) {
-                       int  result;
-                       result = iops->get(env, it, (const void *)"");
-                       if (result > 0) {
-                               int i;
-                               for (result = 0, i = 0; result == 0 && i < 3;
-                                    ++i)
-                                       result = iops->next(env, it);
-                               if (result == 0)
-                                       la->la_flags = 1;
-                       } else if (result == 0)
-                               /*
-                                * Huh? Index contains no zero key?
-                                */
-                               rc = -EIO;
-
-                       iops->put(env, it);
-                       iops->fini(env, it);
-               }
-       }
 
        obdo->o_valid = 0;
        obdo_from_la(obdo, la, la->la_valid);
-       obdo_cpu_to_le(obdo, obdo);
        lustre_set_wire_obdo(NULL, obdo, obdo);
 
 out_unlock:
@@ -512,13 +532,13 @@ static int out_index_lookup(struct tgt_session_info *tsi)
        if (rc == 0)
                rc += 1;
 
+out_unlock:
+       dt_read_unlock(env, obj);
+
        CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
               PFID(lu_object_fid(&obj->do_lu)), name,
               PFID(&tti->tti_fid1), rc);
 
-out_unlock:
-       dt_read_unlock(env, obj);
-
        CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
               tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
               0, rc);
@@ -568,18 +588,20 @@ static int __out_tx_xattr_set(const struct lu_env *env,
                              const char *name, int flags,
                              struct thandle_exec_args *ta,
                              struct object_update_reply *reply,
-                             int index, char *file, int line)
+                             int index, const char *file, int line)
 {
-       struct tx_arg           *arg;
+       struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_xattr_set(env, dt_obj, buf, name,
-                                         flags, ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_xattr_set(env, dt_obj, buf, name, flags, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_xattr_set_exec, NULL, file, line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->u.xattr_set.name = name;
@@ -599,9 +621,10 @@ static int out_xattr_set(struct tgt_session_info *tsi)
        struct lu_buf           *lbuf = &tti->tti_buf;
        char                    *name;
        char                    *buf;
-       char                    *tmp;
+       __u32                   *tmp;
        int                      buf_len = 0;
        int                      flag;
+       int                      size = 0;
        int                      rc;
        ENTRY;
 
@@ -622,16 +645,16 @@ static int out_xattr_set(struct tgt_session_info *tsi)
        lbuf->lb_buf = buf;
        lbuf->lb_len = buf_len;
 
-       tmp = (char *)object_update_param_get(update, 2, NULL);
-       if (tmp == NULL) {
-               CERROR("%s: empty flag for xattr set: rc = %d\n",
-                      tgt_name(tsi->tsi_tgt), -EPROTO);
+       tmp = object_update_param_get(update, 2, &size);
+       if (tmp == NULL || size != sizeof(*tmp)) {
+               CERROR("%s: emptry or wrong size %d flag: rc = %d\n",
+                      tgt_name(tsi->tsi_tgt), size, -EPROTO);
                RETURN(err_serious(-EPROTO));
        }
 
        if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
-               __swab32s((__u32 *)tmp);
-       flag = *(int *)tmp;
+               __swab32s(tmp);
+       flag = *tmp;
 
        rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
                              &tti->tti_tea,
@@ -640,6 +663,79 @@ static int out_xattr_set(struct tgt_session_info *tsi)
        RETURN(rc);
 }
 
+static int out_tx_xattr_del_exec(const struct lu_env *env, struct thandle *th,
+                                struct tx_arg *arg)
+{
+       struct dt_object *dt_obj = arg->object;
+       int rc;
+
+       CDEBUG(D_INFO, "%s: del xattr name '%s' on "DFID"\n",
+              dt_obd_name(th->th_dev), arg->u.xattr_set.name,
+              PFID(lu_object_fid(&dt_obj->do_lu)));
+
+       if (!lu_object_exists(&dt_obj->do_lu))
+               GOTO(out, rc = -ENOENT);
+
+       dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
+       rc = dt_xattr_del(env, dt_obj, arg->u.xattr_set.name,
+                         th, NULL);
+       dt_write_unlock(env, dt_obj);
+out:
+       CDEBUG(D_INFO, "%s: insert xattr del reply %p index %d: rc = %d\n",
+              dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
+
+       object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
+
+       return rc;
+}
+
+static int __out_tx_xattr_del(const struct lu_env *env,
+                             struct dt_object *dt_obj, const char *name,
+                             struct thandle_exec_args *ta,
+                             struct object_update_reply *reply,
+                             int index, const char *file, int line)
+{
+       struct tx_arg   *arg;
+       int             rc;
+
+       rc = dt_declare_xattr_del(env, dt_obj, name, ta->ta_handle);
+       if (rc != 0)
+               return rc;
+
+       arg = tx_add_exec(ta, out_tx_xattr_del_exec, NULL, file, line);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
+       lu_object_get(&dt_obj->do_lu);
+       arg->object = dt_obj;
+       arg->u.xattr_set.name = name;
+       arg->reply = reply;
+       arg->index = index;
+       return 0;
+}
+
+static int out_xattr_del(struct tgt_session_info *tsi)
+{
+       struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
+       struct object_update    *update = tti->tti_u.update.tti_update;
+       struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
+       char                    *name;
+       int                      rc;
+       ENTRY;
+
+       name = object_update_param_get(update, 0, NULL);
+       if (name == NULL) {
+               CERROR("%s: empty name for xattr set: rc = %d\n",
+                      tgt_name(tsi->tsi_tgt), -EPROTO);
+               RETURN(err_serious(-EPROTO));
+       }
+
+       rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
+                             tti->tti_u.update.tti_update_reply,
+                             tti->tti_u.update.tti_update_reply_index);
+       RETURN(rc);
+}
+
 static int out_obj_ref_add(const struct lu_env *env,
                           struct dt_object *dt_obj,
                           struct thandle *th)
@@ -691,18 +787,21 @@ static int __out_tx_ref_add(const struct lu_env *env,
                            struct dt_object *dt_obj,
                            struct thandle_exec_args *ta,
                            struct object_update_reply *reply,
-                           int index, char *file, int line)
+                           int index, const char *file, int line)
 {
        struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_ref_add(env, dt_obj, ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_ref_add(env, dt_obj, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_ref_add_exec, out_tx_ref_add_undo, file,
                          line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->reply = reply;
@@ -753,18 +852,21 @@ static int __out_tx_ref_del(const struct lu_env *env,
                            struct dt_object *dt_obj,
                            struct thandle_exec_args *ta,
                            struct object_update_reply *reply,
-                           int index, char *file, int line)
+                           int index, const char *file, int line)
 {
        struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_ref_del(env, dt_obj, ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_ref_del(env, dt_obj, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_ref_del_exec, out_tx_ref_del_undo, file,
                          line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->reply = reply;
@@ -797,9 +899,10 @@ static int out_obj_index_insert(const struct lu_env *env,
 {
        int rc;
 
-       CDEBUG(D_INFO, "%s: index insert "DFID" name: %s fid "DFID"\n",
+       CDEBUG(D_INFO, "%s: index insert "DFID" name: %s fid "DFID", type %u\n",
               dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
-              (char *)key, PFID((struct lu_fid *)rec));
+              (char *)key, PFID(((struct dt_insert_rec *)rec)->rec_fid),
+              ((struct dt_insert_rec *)rec)->rec_type);
 
        if (dt_try_as_dir(env, dt_obj) == 0)
                return -ENOTDIR;
@@ -838,7 +941,8 @@ static int out_tx_index_insert_exec(const struct lu_env *env,
        struct dt_object *dt_obj = arg->object;
        int rc;
 
-       rc = out_obj_index_insert(env, dt_obj, arg->u.insert.rec,
+       rc = out_obj_index_insert(env, dt_obj,
+                                 (const struct dt_rec *)&arg->u.insert.rec,
                                  arg->u.insert.key, th);
 
        CDEBUG(D_INFO, "%s: insert idx insert reply %p index %d: rc = %d\n",
@@ -857,52 +961,51 @@ static int out_tx_index_insert_undo(const struct lu_env *env,
 
 static int __out_tx_index_insert(const struct lu_env *env,
                                 struct dt_object *dt_obj,
-                                char *name, struct lu_fid *fid,
+                                const struct dt_rec *rec,
+                                const struct dt_key *key,
                                 struct thandle_exec_args *ta,
                                 struct object_update_reply *reply,
-                                int index, char *file, int line)
+                                int index, const char *file, int line)
 {
-       struct tx_arg *arg;
+       struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-
        if (dt_try_as_dir(env, dt_obj) == 0) {
-               ta->ta_err = -ENOTDIR;
-               return ta->ta_err;
+               rc = -ENOTDIR;
+               return rc;
        }
 
-       ta->ta_err = dt_declare_insert(env, dt_obj,
-                                      (struct dt_rec *)fid,
-                                      (struct dt_key *)name,
-                                      ta->ta_handle);
-
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_insert(env, dt_obj, rec, key, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_index_insert_exec,
-                         out_tx_index_insert_undo, file,
-                         line);
-       LASSERT(arg);
+                         out_tx_index_insert_undo, file, line);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->reply = reply;
        arg->index = index;
-       arg->u.insert.rec = (struct dt_rec *)fid;
-       arg->u.insert.key = (struct dt_key *)name;
+       arg->u.insert.rec = *(const struct dt_insert_rec *)rec;
+       arg->u.insert.key = key;
 
        return 0;
 }
 
 static int out_index_insert(struct tgt_session_info *tsi)
 {
-       struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
-       struct object_update    *update = tti->tti_u.update.tti_update;
-       struct dt_object  *obj = tti->tti_u.update.tti_dt_object;
-       struct lu_fid     *fid;
-       char              *name;
-       int                rc = 0;
-       int                size;
-
+       struct tgt_thread_info  *tti    = tgt_th_info(tsi->tsi_env);
+       struct object_update    *update = tti->tti_u.update.tti_update;
+       struct dt_object        *obj    = tti->tti_u.update.tti_dt_object;
+       struct dt_insert_rec    *rec    = &tti->tti_rec;
+       struct lu_fid           *fid;
+       char                    *name;
+       __u32                   *ptype;
+       int                      rc     = 0;
+       int                      size;
        ENTRY;
 
        name = object_update_param_get(update, 0, NULL);
@@ -928,8 +1031,21 @@ static int out_index_insert(struct tgt_session_info *tsi)
                RETURN(err_serious(-EPROTO));
        }
 
-       rc = out_tx_index_insert(tsi->tsi_env, obj, name, fid,
-                                &tti->tti_tea,
+       ptype = object_update_param_get(update, 2, &size);
+       if (ptype == NULL || size != sizeof(*ptype)) {
+               CERROR("%s: invalid type for index insert: rc = %d\n",
+                      tgt_name(tsi->tsi_tgt), -EPROTO);
+               RETURN(err_serious(-EPROTO));
+       }
+
+       if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
+               __swab32s(ptype);
+
+       rec->rec_fid = fid;
+       rec->rec_type = *ptype;
+
+       rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
+                                (const struct dt_key *)name, &tti->tti_tea,
                                 tti->tti_u.update.tti_update_reply,
                                 tti->tti_u.update.tti_update_reply_index);
        RETURN(rc);
@@ -943,7 +1059,7 @@ static int out_tx_index_delete_exec(const struct lu_env *env,
 
        rc = out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
 
-       CDEBUG(D_INFO, "%s: insert idx insert reply %p index %d: rc = %d\n",
+       CDEBUG(D_INFO, "%s: delete idx insert reply %p index %d: rc = %d\n",
               dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
 
        object_update_result_insert(arg->reply, NULL, 0, arg->index, rc);
@@ -961,34 +1077,35 @@ static int out_tx_index_delete_undo(const struct lu_env *env,
 }
 
 static int __out_tx_index_delete(const struct lu_env *env,
-                                struct dt_object *dt_obj, char *name,
+                                struct dt_object *dt_obj,
+                                const struct dt_key *key,
                                 struct thandle_exec_args *ta,
                                 struct object_update_reply *reply,
-                                int index, char *file, int line)
+                                int index, const char *file, int line)
 {
-       struct tx_arg *arg;
+       struct tx_arg   *arg;
+       int             rc;
 
        if (dt_try_as_dir(env, dt_obj) == 0) {
-               ta->ta_err = -ENOTDIR;
-               return ta->ta_err;
+               rc = -ENOTDIR;
+               return rc;
        }
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_delete(env, dt_obj,
-                                      (struct dt_key *)name,
-                                      ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_delete(env, dt_obj, key, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_index_delete_exec,
-                         out_tx_index_delete_undo, file,
-                         line);
-       LASSERT(arg);
+                         out_tx_index_delete_undo, file, line);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->reply = reply;
        arg->index = index;
-       arg->u.insert.key = (struct dt_key *)name;
+       arg->u.insert.key = key;
        return 0;
 }
 
@@ -1010,7 +1127,8 @@ static int out_index_delete(struct tgt_session_info *tsi)
                RETURN(err_serious(-EPROTO));
        }
 
-       rc = out_tx_index_delete(tsi->tsi_env, obj, name, &tti->tti_tea,
+       rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
+                                &tti->tti_tea,
                                 tti->tti_u.update.tti_update_reply,
                                 tti->tti_u.update.tti_update_reply_index);
        RETURN(rc);
@@ -1043,18 +1161,21 @@ static int out_tx_destroy_undo(const struct lu_env *env, struct thandle *th,
 static int __out_tx_destroy(const struct lu_env *env, struct dt_object *dt_obj,
                             struct thandle_exec_args *ta,
                             struct object_update_reply *reply,
-                            int index, char *file, int line)
+                            int index, const char *file, int line)
 {
-       struct tx_arg *arg;
+       struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_destroy(env, dt_obj, ta->ta_handle);
-       if (ta->ta_err)
-               return ta->ta_err;
+       rc = dt_declare_destroy(env, dt_obj, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_destroy_exec, out_tx_destroy_undo,
                          file, line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->reply = reply;
@@ -1112,18 +1233,20 @@ static int __out_tx_write(const struct lu_env *env,
                          const struct lu_buf *buf,
                          loff_t pos, struct thandle_exec_args *ta,
                          struct object_update_reply *reply,
-                         int index, char *file, int line)
+                         int index, const char *file, int line)
 {
        struct tx_arg   *arg;
+       int             rc;
 
        LASSERT(ta->ta_handle != NULL);
-       ta->ta_err = dt_declare_record_write(env, dt_obj, buf, pos,
-                                            ta->ta_handle);
-       if (ta->ta_err != 0)
-               return ta->ta_err;
+       rc = dt_declare_record_write(env, dt_obj, buf, pos, ta->ta_handle);
+       if (rc != 0)
+               return rc;
 
        arg = tx_add_exec(ta, out_tx_write_exec, NULL, file, line);
-       LASSERT(arg);
+       if (IS_ERR(arg))
+               return PTR_ERR(arg);
+
        lu_object_get(&dt_obj->do_lu);
        arg->object = dt_obj;
        arg->u.write.buf = *buf;
@@ -1140,7 +1263,8 @@ static int out_write(struct tgt_session_info *tsi)
        struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
        struct lu_buf           *lbuf = &tti->tti_buf;
        char                    *buf;
-       char                    *tmp;
+       __u64                   *tmp;
+       int                     size = 0;
        int                     buf_len = 0;
        loff_t                  pos;
        int                      rc;
@@ -1155,16 +1279,16 @@ static int out_write(struct tgt_session_info *tsi)
        lbuf->lb_buf = buf;
        lbuf->lb_len = buf_len;
 
-       tmp = (char *)object_update_param_get(update, 1, NULL);
-       if (tmp == NULL) {
-               CERROR("%s: empty flag for xattr set: rc = %d\n",
-                      tgt_name(tsi->tsi_tgt), -EPROTO);
+       tmp = object_update_param_get(update, 1, &size);
+       if (tmp == NULL || size != sizeof(*tmp)) {
+               CERROR("%s: empty or wrong size %d pos: rc = %d\n",
+                      tgt_name(tsi->tsi_tgt), size, -EPROTO);
                RETURN(err_serious(-EPROTO));
        }
 
        if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
-               __swab64s((__u64 *)tmp);
-       pos = *(loff_t *)tmp;
+               __swab64s(tmp);
+       pos = *tmp;
 
        rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
                          &tti->tti_tea,
@@ -1184,7 +1308,6 @@ static int out_write(struct tgt_session_info *tsi)
        .th_version = 0,                                \
 }
 
-#define out_handler mdt_handler
 static struct tgt_handler out_update_ops[] = {
        DEF_OUT_HNDL(OUT_CREATE, "out_create", MUTABOR | HABEO_REFERO,
                     out_create),
@@ -1200,6 +1323,8 @@ static struct tgt_handler out_update_ops[] = {
                     out_attr_get),
        DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", MUTABOR | HABEO_REFERO,
                     out_xattr_set),
+       DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", MUTABOR | HABEO_REFERO,
+                    out_xattr_del),
        DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HABEO_REFERO,
                     out_xattr_get),
        DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HABEO_REFERO,
@@ -1229,18 +1354,17 @@ struct tgt_handler *out_handler_find(__u32 opc)
 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
                        struct thandle_exec_args *ta, struct obd_export *exp)
 {
-       memset(ta, 0, sizeof(*ta));
+       ta->ta_argno = 0;
        ta->ta_handle = dt_trans_create(env, dt);
        if (IS_ERR(ta->ta_handle)) {
                int rc;
 
                rc = PTR_ERR(ta->ta_handle);
                ta->ta_handle = NULL;
-               CERROR("%s: start handle error: rc = %d\n",
-                      dt_obd_name(dt), rc);
+               CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
+                      rc);
                return rc;
        }
-       ta->ta_dev = dt;
        if (exp->exp_need_sync)
                ta->ta_handle->th_sync = 1;
 
@@ -1250,7 +1374,7 @@ static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
 static int out_trans_start(const struct lu_env *env,
                           struct thandle_exec_args *ta)
 {
-       return dt_trans_start(env, ta->ta_dev, ta->ta_handle);
+       return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
 }
 
 static int out_trans_stop(const struct lu_env *env,
@@ -1260,73 +1384,82 @@ static int out_trans_stop(const struct lu_env *env,
        int rc;
 
        ta->ta_handle->th_result = err;
-       rc = dt_trans_stop(env, ta->ta_dev, ta->ta_handle);
+       rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
        for (i = 0; i < ta->ta_argno; i++) {
-               if (ta->ta_args[i].object != NULL) {
-                       struct dt_object *obj = ta->ta_args[i].object;
+               if (ta->ta_args[i]->object != NULL) {
+                       struct dt_object *obj = ta->ta_args[i]->object;
 
                        /* If the object is being created during this
                         * transaction, we need to remove them from the
                         * cache immediately, because a few layers are
                         * missing in OUT handler, i.e. the object might
                         * not be initialized in all layers */
-                       if (ta->ta_args[i].exec_fn == out_tx_create_exec)
+                       if (ta->ta_args[i]->exec_fn == out_tx_create_exec)
                                set_bit(LU_OBJECT_HEARD_BANSHEE,
                                        &obj->do_lu.lo_header->loh_flags);
-                       lu_object_put(env, &ta->ta_args[i].object->do_lu);
-                       ta->ta_args[i].object = NULL;
+                       lu_object_put(env, &ta->ta_args[i]->object->do_lu);
+                       ta->ta_args[i]->object = NULL;
                }
        }
+       ta->ta_handle = NULL;
+       ta->ta_argno = 0;
 
        return rc;
 }
 
-int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta)
+int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
+              int declare_ret)
 {
-       struct tgt_session_info *tsi = tgt_ses_info(env);
-       int i = 0, rc;
+       struct tgt_session_info *tsi = tgt_ses_info(env);
+       int                     i;
+       int                     rc;
+       int                     rc1;
+       ENTRY;
 
-       LASSERT(ta->ta_dev);
-       LASSERT(ta->ta_handle);
+       if (ta->ta_handle == NULL)
+               RETURN(0);
 
-       if (ta->ta_err != 0 || ta->ta_argno == 0)
-               GOTO(stop, rc = ta->ta_err);
+       if (declare_ret != 0 || ta->ta_argno == 0)
+               GOTO(stop, rc = declare_ret);
 
+       LASSERT(ta->ta_handle->th_dev != NULL);
        rc = out_trans_start(env, ta);
-       if (unlikely(rc))
+       if (unlikely(rc != 0))
                GOTO(stop, rc);
 
        for (i = 0; i < ta->ta_argno; i++) {
-               rc = ta->ta_args[i].exec_fn(env, ta->ta_handle,
-                                           &ta->ta_args[i]);
+               rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
+                                            ta->ta_args[i]);
                if (unlikely(rc != 0)) {
                        CDEBUG(D_INFO, "error during execution of #%u from"
-                              " %s:%d: rc = %d\n", i, ta->ta_args[i].file,
-                              ta->ta_args[i].line, rc);
+                              " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
+                              ta->ta_args[i]->line, rc);
                        while (--i >= 0) {
-                               if (ta->ta_args[i].undo_fn != NULL)
-                                       ta->ta_args[i].undo_fn(env,
+                               if (ta->ta_args[i]->undo_fn != NULL)
+                                       ta->ta_args[i]->undo_fn(env,
                                                               ta->ta_handle,
-                                                             &ta->ta_args[i]);
+                                                              ta->ta_args[i]);
                                else
                                        CERROR("%s: undo for %s:%d: rc = %d\n",
-                                              dt_obd_name(ta->ta_dev),
-                                              ta->ta_args[i].file,
-                                              ta->ta_args[i].line, -ENOTSUPP);
+                                            dt_obd_name(ta->ta_handle->th_dev),
+                                              ta->ta_args[i]->file,
+                                              ta->ta_args[i]->line, -ENOTSUPP);
                        }
                        break;
                }
+               CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
+                      dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
        }
 
        /* Only fail for real update */
        tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
 stop:
-       CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
-              dt_obd_name(ta->ta_dev), i, ta->ta_argno, rc);
-       out_trans_stop(env, ta, rc);
+       rc1 = out_trans_stop(env, ta, rc);
+       if (rc == 0)
+               rc = rc1;
+
        ta->ta_handle = NULL;
        ta->ta_argno = 0;
-       ta->ta_err = 0;
 
        RETURN(rc);
 }
@@ -1354,7 +1487,7 @@ int out_handle(struct tgt_session_info *tsi)
        struct object_update_reply      *reply;
        int                              bufsize;
        int                              count;
-       int                              old_batchid = -1;
+       int                              current_batchid = -1;
        int                              i;
        int                              rc = 0;
        int                              rc1 = 0;
@@ -1405,11 +1538,6 @@ int out_handle(struct tgt_session_info *tsi)
                RETURN(err_serious(-EPROTO));
        object_update_reply_init(reply, count);
        tti->tti_u.update.tti_update_reply = reply;
-
-       rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
-       if (rc != 0)
-               RETURN(rc);
-
        tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
 
        /* Walk through updates in the request to execute them synchronously */
@@ -1424,21 +1552,6 @@ int out_handle(struct tgt_session_info *tsi)
                if (ptlrpc_req_need_swab(pill->rc_req))
                        lustre_swab_object_update(update);
 
-               if (old_batchid == -1) {
-                       old_batchid = update->ou_batchid;
-               } else if (old_batchid != update->ou_batchid) {
-                       /* Stop the current update transaction,
-                        * create a new one */
-                       rc = out_tx_end(env, ta);
-                       if (rc < 0)
-                               RETURN(rc);
-
-                       rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
-                       if (rc != 0)
-                               RETURN(rc);
-                       old_batchid = update->ou_batchid;
-               }
-
                if (!fid_is_sane(&update->ou_fid)) {
                        CERROR("%s: invalid FID "DFID": rc = %d\n",
                               tgt_name(tsi->tsi_tgt), PFID(&update->ou_fid),
@@ -1461,33 +1574,61 @@ int out_handle(struct tgt_session_info *tsi)
                tti->tti_u.update.tti_update_reply_index = i;
 
                h = out_handler_find(update->ou_type);
-               if (likely(h != NULL)) {
-                       /* For real modification RPC, check if the update
-                        * has been executed */
-                       if (h->th_flags & MUTABOR) {
-                               struct ptlrpc_request *req = tgt_ses_req(tsi);
+               if (unlikely(h == NULL)) {
+                       CERROR("%s: unsupported opc: 0x%x\n",
+                              tgt_name(tsi->tsi_tgt), update->ou_type);
+                       GOTO(next, rc = -ENOTSUPP);
+               }
+
+               /* Check resend case only for modifying RPC */
+               if (h->th_flags & MUTABOR) {
+                       struct ptlrpc_request *req = tgt_ses_req(tsi);
+
+                       if (out_check_resent(env, dt, dt_obj, req,
+                                            out_reconstruct, reply, i))
+                               GOTO(next, rc = 0);
+               }
+
+               /* start transaction for modification RPC only */
+               if (h->th_flags & MUTABOR && current_batchid == -1) {
+                       current_batchid = update->ou_batchid;
+                       rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
+                       if (rc != 0)
+                               GOTO(next, rc);
+               }
 
-                               if (out_check_resent(env, dt, dt_obj, req,
-                                                    out_reconstruct, reply, i))
+               /* Stop the current update transaction, if the update has
+                * different batchid, or read-only update */
+               if (((current_batchid != update->ou_batchid) ||
+                    !(h->th_flags & MUTABOR)) && ta->ta_handle != NULL) {
+                       rc = out_tx_end(env, ta, rc);
+                       current_batchid = -1;
+                       if (rc != 0)
+                               GOTO(next, rc);
+
+                       /* start a new transaction if needed */
+                       if (h->th_flags & MUTABOR) {
+                               rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
+                               if (rc != 0)
                                        GOTO(next, rc);
-                       }
 
-                       rc = h->th_act(tsi);
-               } else {
-                       CERROR("%s: The unsupported opc: 0x%x\n",
-                              tgt_name(tsi->tsi_tgt), update->ou_type);
-                       lu_object_put(env, &dt_obj->do_lu);
-                       GOTO(out, rc = -ENOTSUPP);
+                               current_batchid = update->ou_batchid;
+                       }
                }
+
+               rc = h->th_act(tsi);
 next:
                lu_object_put(env, &dt_obj->do_lu);
                if (rc < 0)
                        GOTO(out, rc);
        }
 out:
-       rc1 = out_tx_end(env, ta);
-       if (rc == 0)
-               rc = rc1;
+       if (current_batchid != -1) {
+               rc1 = out_tx_end(env, ta, rc);
+               if (rc == 0)
+                       rc = rc1;
+       }
+
        RETURN(rc);
 }