Whamcloud - gitweb
LU-12811 ptlrpc: pass buflen to lustre_swab_object_update_*()
[fs/lustre-release.git] / lustre / target / out_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2017, Intel Corporation.
24  *
25  * lustre/target/out_handler.c
26  *
27  * Object update handler between targets.
28  *
29  * Author: di.wang <di.wang@intel.com>
30  */
31
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <llog_swab.h>
35 #include <lustre_obdo.h>
36 #include <lustre_swab.h>
37 #include <lustre_update.h>
38 #include <md_object.h>
39 #include <obd_class.h>
40 #include "tgt_internal.h"
41
42 static inline void orr_cpu_to_le(struct out_read_reply *orr_dst,
43                                  const struct out_read_reply *orr_src)
44 {
45         orr_dst->orr_size = cpu_to_le32(orr_src->orr_size);
46         orr_dst->orr_padding = cpu_to_le32(orr_src->orr_padding);
47         orr_dst->orr_offset = cpu_to_le64(orr_dst->orr_offset);
48 }
49
50 static void out_reconstruct(const struct lu_env *env, struct dt_device *dt,
51                             struct dt_object *obj,
52                             struct object_update_reply *reply,
53                             int index)
54 {
55         CDEBUG(D_INFO, "%s: fork reply reply %p index %d: rc = %d\n",
56                dt_obd_name(dt), reply, index, 0);
57
58         object_update_result_insert(reply, NULL, 0, index, 0);
59 }
60
61 typedef void (*out_reconstruct_t)(const struct lu_env *env,
62                                   struct dt_device *dt,
63                                   struct dt_object *obj,
64                                   struct object_update_reply *reply,
65                                   int index);
66
67 static inline int out_check_resent(const struct lu_env *env,
68                                    struct dt_device *dt,
69                                    struct dt_object *obj,
70                                    struct ptlrpc_request *req,
71                                    out_reconstruct_t reconstruct,
72                                    struct object_update_reply *reply,
73                                    int index)
74 {
75         if (likely(!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
76                 return 0;
77
78         if (req_xid_is_last(req)) {
79                 struct lsd_client_data *lcd;
80
81                 /* XXX this does not support mulitple transactions yet, i.e.
82                  * only 1 update RPC each time betwee MDTs */
83                 lcd = req->rq_export->exp_target_data.ted_lcd;
84
85                 req->rq_transno = lcd->lcd_last_transno;
86                 req->rq_status = lcd->lcd_last_result;
87                 if (req->rq_status != 0)
88                         req->rq_transno = 0;
89                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
90                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
91
92                 DEBUG_REQ(D_RPCTRACE, req, "restoring resent RPC");
93
94                 reconstruct(env, dt, obj, reply, index);
95                 return 1;
96         }
97         DEBUG_REQ(D_HA, req, "no reply for RESENT req (have %lld)",
98                  req->rq_export->exp_target_data.ted_lcd->lcd_last_xid);
99         return 0;
100 }
101
102 static int out_create(struct tgt_session_info *tsi)
103 {
104         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
105         struct object_update    *update = tti->tti_u.update.tti_update;
106         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
107         struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
108         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
109         struct lu_attr          *attr = &tti->tti_attr;
110         struct lu_fid           *fid = NULL;
111         struct obdo             *wobdo;
112         size_t                  size;
113         int                     rc;
114
115         ENTRY;
116
117         wobdo = object_update_param_get(update, 0, &size);
118         if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
119                 CERROR("%s: obdo is NULL, invalid RPC: rc = %ld\n",
120                        tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
121                 RETURN(PTR_ERR(wobdo));
122         }
123
124         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
125                 lustre_swab_obdo(wobdo);
126         lustre_get_wire_obdo(NULL, lobdo, wobdo);
127         la_from_obdo(attr, lobdo, lobdo->o_valid);
128
129         dof->dof_type = dt_mode_to_dft(attr->la_mode);
130         if (update->ou_params_count > 1) {
131                 fid = object_update_param_get(update, 1, &size);
132                 if (IS_ERR(fid) || size != sizeof(*fid)) {
133                         CERROR("%s: invalid fid: rc = %ld\n",
134                                tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
135                         RETURN(PTR_ERR(fid));
136                 }
137                 if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
138                         lustre_swab_lu_fid(fid);
139                 if (!fid_is_sane(fid)) {
140                         CERROR("%s: invalid fid "DFID": rc = %d\n",
141                                tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
142                         RETURN(-EPROTO);
143                 }
144         }
145
146         if (lu_object_exists(&obj->do_lu))
147                 RETURN(-EEXIST);
148
149         rc = out_tx_create(tsi->tsi_env, obj, attr, fid, dof,
150                            &tti->tti_tea, tti->tti_tea.ta_handle,
151                            tti->tti_u.update.tti_update_reply,
152                            tti->tti_u.update.tti_update_reply_index);
153
154         RETURN(rc);
155 }
156
157 static int out_attr_set(struct tgt_session_info *tsi)
158 {
159         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
160         struct object_update    *update = tti->tti_u.update.tti_update;
161         struct lu_attr          *attr = &tti->tti_attr;
162         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
163         struct obdo             *lobdo = &tti->tti_u.update.tti_obdo;
164         struct obdo             *wobdo;
165         size_t                   size;
166         int                      rc;
167
168         ENTRY;
169
170         wobdo = object_update_param_get(update, 0, &size);
171         if (IS_ERR(wobdo) || size != sizeof(*wobdo)) {
172                 CERROR("%s: empty obdo in the update: rc = %ld\n",
173                        tgt_name(tsi->tsi_tgt), PTR_ERR(wobdo));
174                 RETURN(PTR_ERR(wobdo));
175         }
176
177         attr->la_valid = 0;
178         attr->la_valid = 0;
179
180         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
181                 lustre_swab_obdo(wobdo);
182         lustre_get_wire_obdo(NULL, lobdo, wobdo);
183         la_from_obdo(attr, lobdo, lobdo->o_valid);
184
185         rc = out_tx_attr_set(tsi->tsi_env, obj, attr, &tti->tti_tea,
186                              tti->tti_tea.ta_handle,
187                              tti->tti_u.update.tti_update_reply,
188                              tti->tti_u.update.tti_update_reply_index);
189
190         RETURN(rc);
191 }
192
193 static int out_attr_get(struct tgt_session_info *tsi)
194 {
195         const struct lu_env     *env = tsi->tsi_env;
196         struct tgt_thread_info  *tti = tgt_th_info(env);
197         struct object_update    *update = tti->tti_u.update.tti_update;
198         struct obdo             *obdo = &tti->tti_u.update.tti_obdo;
199         struct lu_attr          *la = &tti->tti_attr;
200         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
201         int                     idx = tti->tti_u.update.tti_update_reply_index;
202         int                     rc;
203
204         ENTRY;
205
206         if (unlikely(update->ou_result_size < sizeof(*obdo)))
207                 return -EPROTO;
208
209         if (!lu_object_exists(&obj->do_lu)) {
210                 /* Usually, this will be called when the master MDT try
211                  * to init a remote object(see osp_object_init), so if
212                  * the object does not exist on slave, we need set BANSHEE flag,
213                  * so the object can be removed from the cache immediately */
214                 set_bit(LU_OBJECT_HEARD_BANSHEE,
215                         &obj->do_lu.lo_header->loh_flags);
216                 RETURN(-ENOENT);
217         }
218
219         dt_read_lock(env, obj, DT_TGT_CHILD);
220         rc = dt_attr_get(env, obj, la);
221         if (rc)
222                 GOTO(out_unlock, rc);
223
224         obdo->o_valid = 0;
225         obdo_from_la(obdo, la, la->la_valid);
226
227 out_unlock:
228         dt_read_unlock(env, obj);
229
230         CDEBUG(D_INFO, "%s: insert attr get reply %p index %d: rc = %d\n",
231                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
232                0, rc);
233
234         object_update_result_insert(tti->tti_u.update.tti_update_reply, obdo,
235                                     sizeof(*obdo), idx, rc);
236
237         RETURN(rc);
238 }
239
240 static int out_xattr_get(struct tgt_session_info *tsi)
241 {
242         const struct lu_env        *env = tsi->tsi_env;
243         struct tgt_thread_info     *tti = tgt_th_info(env);
244         struct object_update       *update = tti->tti_u.update.tti_update;
245         struct lu_buf              *lbuf = &tti->tti_buf;
246         struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
247         struct dt_object           *obj = tti->tti_u.update.tti_dt_object;
248         char                       *name;
249         struct object_update_result *update_result;
250         int                     idx = tti->tti_u.update.tti_update_reply_index;
251         int                        rc;
252
253         ENTRY;
254
255         if (!lu_object_exists(&obj->do_lu)) {
256                 set_bit(LU_OBJECT_HEARD_BANSHEE,
257                         &obj->do_lu.lo_header->loh_flags);
258                 RETURN(-ENOENT);
259         }
260
261         name = object_update_param_get(update, 0, NULL);
262         if (IS_ERR(name)) {
263                 CERROR("%s: empty name for xattr get: rc = %ld\n",
264                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
265                 RETURN(PTR_ERR(name));
266         }
267
268         update_result = object_update_result_get(reply, idx, NULL);
269         if (update_result == NULL) {
270                 CERROR("%s: empty name for xattr get: rc = %d\n",
271                        tgt_name(tsi->tsi_tgt), -EPROTO);
272                 RETURN(-EPROTO);
273         }
274
275         lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
276         if (lbuf->lb_len == 0)
277                 lbuf->lb_buf = NULL;
278         else
279                 lbuf->lb_buf = update_result->our_data;
280
281         dt_read_lock(env, obj, DT_TGT_CHILD);
282         rc = dt_xattr_get(env, obj, lbuf, name);
283         dt_read_unlock(env, obj);
284         if (rc <= 0) {
285                 lbuf->lb_len = 0;
286                 if (unlikely(!rc))
287                         rc = -ENODATA;
288         } else if (lbuf->lb_buf) {
289                 lbuf->lb_len = rc;
290         }
291         CDEBUG(D_INFO, "%s: "DFID" get xattr %s len %d\n",
292                tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
293                name, rc);
294
295         GOTO(out, rc);
296
297 out:
298         object_update_result_insert(reply, lbuf->lb_buf, lbuf->lb_len, idx, rc);
299         RETURN(0);
300 }
301
302 static int out_xattr_list(struct tgt_session_info *tsi)
303 {
304         const struct lu_env *env = tsi->tsi_env;
305         struct tgt_thread_info *tti = tgt_th_info(env);
306         struct lu_buf *lbuf = &tti->tti_buf;
307         struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
308         struct dt_object *obj = tti->tti_u.update.tti_dt_object;
309         struct object_update_result *update_result;
310         int idx = tti->tti_u.update.tti_update_reply_index;
311         int rc;
312
313         ENTRY;
314
315         if (!lu_object_exists(&obj->do_lu)) {
316                 set_bit(LU_OBJECT_HEARD_BANSHEE,
317                         &obj->do_lu.lo_header->loh_flags);
318                 RETURN(-ENOENT);
319         }
320
321         update_result = object_update_result_get(reply, 0, NULL);
322         if (!update_result) {
323                 rc = -EPROTO;
324                 CERROR("%s: empty buf for xattr list: rc = %d\n",
325                        tgt_name(tsi->tsi_tgt), rc);
326                 RETURN(rc);
327         }
328
329         lbuf->lb_len = (int)tti->tti_u.update.tti_update->ou_result_size;
330         lbuf->lb_buf = update_result->our_data;
331         if (lbuf->lb_len == 0)
332                 lbuf->lb_buf = 0;
333
334         dt_read_lock(env, obj, DT_TGT_CHILD);
335         rc = dt_xattr_list(env, obj, lbuf);
336         dt_read_unlock(env, obj);
337         if (rc <= 0) {
338                 lbuf->lb_len = 0;
339                 if (unlikely(!rc))
340                         rc = -ENODATA;
341         } else if (lbuf->lb_buf) {
342                 lbuf->lb_len = rc;
343         }
344
345         CDEBUG(D_INFO, "%s: "DFID" list xattr len %d\n",
346                tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)), rc);
347
348         /* Since we directly use update_result->our_data as the lbuf->lb_buf,
349          * then use NULL for result_insert to avoid unnecessary memory copy. */
350         object_update_result_insert(reply, NULL, lbuf->lb_len, idx, rc);
351
352         RETURN(0);
353 }
354
355 static int out_index_lookup(struct tgt_session_info *tsi)
356 {
357         const struct lu_env     *env = tsi->tsi_env;
358         struct tgt_thread_info  *tti = tgt_th_info(env);
359         struct object_update    *update = tti->tti_u.update.tti_update;
360         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
361         char                    *name;
362         int                      rc;
363
364         ENTRY;
365
366         if (unlikely(update->ou_result_size < sizeof(tti->tti_fid1)))
367                 return -EPROTO;
368
369         if (!lu_object_exists(&obj->do_lu))
370                 RETURN(-ENOENT);
371
372         name = object_update_param_get(update, 0, NULL);
373         if (IS_ERR(name)) {
374                 CERROR("%s: empty name for lookup: rc = %ld\n",
375                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
376                 RETURN(PTR_ERR(name));
377         }
378
379         dt_read_lock(env, obj, DT_TGT_CHILD);
380         if (!dt_try_as_dir(env, obj))
381                 GOTO(out_unlock, rc = -ENOTDIR);
382
383         rc = dt_lookup(env, obj, (struct dt_rec *)&tti->tti_fid1,
384                        (struct dt_key *)name);
385
386         if (rc < 0)
387                 GOTO(out_unlock, rc);
388
389         if (rc == 0)
390                 rc += 1;
391
392 out_unlock:
393         dt_read_unlock(env, obj);
394
395         CDEBUG(D_INFO, "lookup "DFID" %s get "DFID" rc %d\n",
396                PFID(lu_object_fid(&obj->do_lu)), name,
397                PFID(&tti->tti_fid1), rc);
398
399         CDEBUG(D_INFO, "%s: insert lookup reply %p index %d: rc = %d\n",
400                tgt_name(tsi->tsi_tgt), tti->tti_u.update.tti_update_reply,
401                0, rc);
402
403         object_update_result_insert(tti->tti_u.update.tti_update_reply,
404                             &tti->tti_fid1, sizeof(tti->tti_fid1),
405                             tti->tti_u.update.tti_update_reply_index, rc);
406         RETURN(rc);
407 }
408
409 static int out_xattr_set(struct tgt_session_info *tsi)
410 {
411         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
412         struct object_update    *update = tti->tti_u.update.tti_update;
413         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
414         struct lu_buf           *lbuf = &tti->tti_buf;
415         char                    *name;
416         char                    *buf;
417         __u32                   *tmp;
418         size_t                   buf_len = 0;
419         int                      flag;
420         size_t                   size = 0;
421         int                      rc;
422         ENTRY;
423
424         name = object_update_param_get(update, 0, NULL);
425         if (IS_ERR(name)) {
426                 CERROR("%s: empty name for xattr set: rc = %ld\n",
427                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
428                 RETURN(PTR_ERR(name));
429         }
430
431         /* If buffer == NULL (-ENODATA), then it might mean delete xattr */
432         buf = object_update_param_get(update, 1, &buf_len);
433         if (IS_ERR(buf) && PTR_ERR(buf) != -ENODATA)
434                 RETURN(PTR_ERR(buf));
435
436         lbuf->lb_buf = buf;
437         lbuf->lb_len = buf_len;
438
439         tmp = object_update_param_get(update, 2, &size);
440         if (IS_ERR(tmp) || size != sizeof(*tmp)) {
441                 CERROR("%s: emptry or wrong size %zu flag: rc = %ld\n",
442                        tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
443                 RETURN(PTR_ERR(tmp));
444         }
445
446         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
447                 __swab32s(tmp);
448         flag = *tmp;
449
450         rc = out_tx_xattr_set(tsi->tsi_env, obj, lbuf, name, flag,
451                               &tti->tti_tea, tti->tti_tea.ta_handle,
452                               tti->tti_u.update.tti_update_reply,
453                               tti->tti_u.update.tti_update_reply_index);
454         RETURN(rc);
455 }
456
457 static int out_xattr_del(struct tgt_session_info *tsi)
458 {
459         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
460         struct object_update    *update = tti->tti_u.update.tti_update;
461         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
462         char                    *name;
463         int                      rc;
464         ENTRY;
465
466         name = object_update_param_get(update, 0, NULL);
467         if (IS_ERR(name)) {
468                 CERROR("%s: empty name for xattr set: rc = %ld\n",
469                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
470                 RETURN(PTR_ERR(name));
471         }
472
473         rc = out_tx_xattr_del(tsi->tsi_env, obj, name, &tti->tti_tea,
474                               tti->tti_tea.ta_handle,
475                               tti->tti_u.update.tti_update_reply,
476                               tti->tti_u.update.tti_update_reply_index);
477         RETURN(rc);
478 }
479
480 /**
481  * increase ref of the object
482  **/
483 static int out_ref_add(struct tgt_session_info *tsi)
484 {
485         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
486         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
487         int                      rc;
488
489         ENTRY;
490
491         rc = out_tx_ref_add(tsi->tsi_env, obj, &tti->tti_tea,
492                             tti->tti_tea.ta_handle,
493                             tti->tti_u.update.tti_update_reply,
494                             tti->tti_u.update.tti_update_reply_index);
495         RETURN(rc);
496 }
497
498 static int out_ref_del(struct tgt_session_info *tsi)
499 {
500         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
501         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
502         int                      rc;
503
504         ENTRY;
505
506         if (!lu_object_exists(&obj->do_lu))
507                 RETURN(-ENOENT);
508
509         rc = out_tx_ref_del(tsi->tsi_env, obj, &tti->tti_tea,
510                             tti->tti_tea.ta_handle,
511                             tti->tti_u.update.tti_update_reply,
512                             tti->tti_u.update.tti_update_reply_index);
513         RETURN(rc);
514 }
515
516 static int out_index_insert(struct tgt_session_info *tsi)
517 {
518         struct tgt_thread_info  *tti    = tgt_th_info(tsi->tsi_env);
519         struct object_update    *update = tti->tti_u.update.tti_update;
520         struct dt_object        *obj    = tti->tti_u.update.tti_dt_object;
521         struct dt_insert_rec    *rec    = &tti->tti_rec;
522         struct lu_fid           *fid;
523         char                    *name;
524         __u32                   *ptype;
525         int                      rc     = 0;
526         size_t                   size;
527         ENTRY;
528
529         name = object_update_param_get(update, 0, NULL);
530         if (IS_ERR(name)) {
531                 CERROR("%s: empty name for index insert: rc = %ld\n",
532                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
533                 RETURN(PTR_ERR(name));
534         }
535
536         fid = object_update_param_get(update, 1, &size);
537         if (IS_ERR(fid) || size != sizeof(*fid)) {
538                 CERROR("%s: invalid fid: rc = %ld\n",
539                        tgt_name(tsi->tsi_tgt), PTR_ERR(fid));
540                 RETURN(PTR_ERR(fid));
541         }
542
543         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
544                 lustre_swab_lu_fid(fid);
545
546         if (!fid_is_sane(fid)) {
547                 CERROR("%s: invalid FID "DFID": rc = %d\n",
548                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
549                 RETURN(-EPROTO);
550         }
551
552         ptype = object_update_param_get(update, 2, &size);
553         if (IS_ERR(ptype) || size != sizeof(*ptype)) {
554                 CERROR("%s: invalid type for index insert: rc = %ld\n",
555                        tgt_name(tsi->tsi_tgt), PTR_ERR(ptype));
556                 RETURN(PTR_ERR(ptype));
557         }
558
559         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
560                 __swab32s(ptype);
561
562         rec->rec_fid = fid;
563         rec->rec_type = *ptype;
564
565         rc = out_tx_index_insert(tsi->tsi_env, obj, (const struct dt_rec *)rec,
566                                  (const struct dt_key *)name, &tti->tti_tea,
567                                  tti->tti_tea.ta_handle,
568                                  tti->tti_u.update.tti_update_reply,
569                                  tti->tti_u.update.tti_update_reply_index);
570
571         CDEBUG(D_INFO, "%s: "DFID" index insert %s: rc = %d\n",
572                tgt_name(tsi->tsi_tgt), PFID(lu_object_fid(&obj->do_lu)),
573                name, rc);
574
575         RETURN(rc);
576 }
577
578 static int out_index_delete(struct tgt_session_info *tsi)
579 {
580         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
581         struct object_update    *update = tti->tti_u.update.tti_update;
582         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
583         char                    *name;
584         int                      rc = 0;
585
586         if (!lu_object_exists(&obj->do_lu))
587                 RETURN(-ENOENT);
588
589         name = object_update_param_get(update, 0, NULL);
590         if (IS_ERR(name)) {
591                 CERROR("%s: empty name for index delete: rc = %ld\n",
592                        tgt_name(tsi->tsi_tgt), PTR_ERR(name));
593                 RETURN(PTR_ERR(name));
594         }
595
596         rc = out_tx_index_delete(tsi->tsi_env, obj, (const struct dt_key *)name,
597                                  &tti->tti_tea, tti->tti_tea.ta_handle,
598                                  tti->tti_u.update.tti_update_reply,
599                                  tti->tti_u.update.tti_update_reply_index);
600         RETURN(rc);
601 }
602
603 static int out_destroy(struct tgt_session_info *tsi)
604 {
605         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
606         struct object_update    *update = tti->tti_u.update.tti_update;
607         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
608         struct lu_fid           *fid;
609         int                      rc;
610         ENTRY;
611
612         fid = &update->ou_fid;
613         if (!fid_is_sane(fid)) {
614                 CERROR("%s: invalid FID "DFID": rc = %d\n",
615                        tgt_name(tsi->tsi_tgt), PFID(fid), -EPROTO);
616                 RETURN(-EPROTO);
617         }
618
619         if (!lu_object_exists(&obj->do_lu))
620                 RETURN(-ENOENT);
621
622         rc = out_tx_destroy(tsi->tsi_env, obj, &tti->tti_tea,
623                             tti->tti_tea.ta_handle,
624                             tti->tti_u.update.tti_update_reply,
625                             tti->tti_u.update.tti_update_reply_index);
626
627         RETURN(rc);
628 }
629
630 static int out_write(struct tgt_session_info *tsi)
631 {
632         struct tgt_thread_info  *tti = tgt_th_info(tsi->tsi_env);
633         struct object_update    *update = tti->tti_u.update.tti_update;
634         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
635         struct lu_buf           *lbuf = &tti->tti_buf;
636         char                    *buf;
637         __u64                   *tmp;
638         size_t                  size = 0;
639         size_t                  buf_len = 0;
640         loff_t                  pos;
641         int                      rc;
642         ENTRY;
643
644         buf = object_update_param_get(update, 0, &buf_len);
645         if (IS_ERR(buf) || buf_len == 0) {
646                 CERROR("%s: empty buf for xattr set: rc = %ld\n",
647                        tgt_name(tsi->tsi_tgt), PTR_ERR(buf));
648                 RETURN(PTR_ERR(buf));
649         }
650         lbuf->lb_buf = buf;
651         lbuf->lb_len = buf_len;
652
653         tmp = object_update_param_get(update, 1, &size);
654         if (IS_ERR(tmp) || size != sizeof(*tmp)) {
655                 CERROR("%s: empty or wrong size %zu pos: rc = %ld\n",
656                        tgt_name(tsi->tsi_tgt), size, PTR_ERR(tmp));
657                 RETURN(PTR_ERR(tmp));
658         }
659
660         if (ptlrpc_req_need_swab(tsi->tsi_pill->rc_req))
661                 __swab64s(tmp);
662         pos = *tmp;
663
664         rc = out_tx_write(tsi->tsi_env, obj, lbuf, pos,
665                           &tti->tti_tea, tti->tti_tea.ta_handle,
666                           tti->tti_u.update.tti_update_reply,
667                           tti->tti_u.update.tti_update_reply_index);
668         RETURN(rc);
669 }
670
671 static int out_read(struct tgt_session_info *tsi)
672 {
673         const struct lu_env     *env = tsi->tsi_env;
674         struct tgt_thread_info  *tti = tgt_th_info(env);
675         struct object_update    *update = tti->tti_u.update.tti_update;
676         struct dt_object        *obj = tti->tti_u.update.tti_dt_object;
677         struct object_update_reply *reply = tti->tti_u.update.tti_update_reply;
678         int index = tti->tti_u.update.tti_update_reply_index;
679         struct lu_rdbuf *rdbuf;
680         struct object_update_result *update_result;
681         struct out_read_reply   *orr;
682         void *tmp;
683         size_t size;
684         size_t total_size = 0;
685         __u64 pos;
686         unsigned int i;
687         unsigned int nbufs;
688         int rc = 0;
689         ENTRY;
690
691         update_result = object_update_result_get(reply, index, NULL);
692         LASSERT(update_result != NULL);
693         update_result->our_datalen = sizeof(*orr);
694
695         if (!lu_object_exists(&obj->do_lu))
696                 GOTO(out, rc = -ENOENT);
697
698         tmp = object_update_param_get(update, 0, NULL);
699         if (IS_ERR(tmp)) {
700                 CERROR("%s: empty size for read: rc = %ld\n",
701                        tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
702                 GOTO(out, rc = PTR_ERR(tmp));
703         }
704         size = le64_to_cpu(*(size_t *)(tmp));
705
706         tmp = object_update_param_get(update, 1, NULL);
707         if (IS_ERR(tmp)) {
708                 CERROR("%s: empty pos for read: rc = %ld\n",
709                        tgt_name(tsi->tsi_tgt), PTR_ERR(tmp));
710                 GOTO(out, rc = PTR_ERR(tmp));
711         }
712         pos = le64_to_cpu(*(__u64 *)(tmp));
713
714         /* Put the offset into the begining of the buffer in reply */
715         orr = (struct out_read_reply *)update_result->our_data;
716
717         nbufs = (size + OUT_BULK_BUFFER_SIZE - 1) / OUT_BULK_BUFFER_SIZE;
718         OBD_ALLOC(rdbuf, sizeof(*rdbuf) + nbufs * sizeof(rdbuf->rb_bufs[0]));
719         if (rdbuf == NULL)
720                 GOTO(out, rc = -ENOMEM);
721
722         rdbuf->rb_nbufs = 0;
723         total_size = 0;
724         for (i = 0; i < nbufs; i++) {
725                 __u32 read_size;
726
727                 read_size = size > OUT_BULK_BUFFER_SIZE ?
728                             OUT_BULK_BUFFER_SIZE : size;
729                 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
730                 if (rdbuf->rb_bufs[i].lb_buf == NULL)
731                         GOTO(out_free, rc = -ENOMEM);
732
733                 rdbuf->rb_bufs[i].lb_len = read_size;
734                 dt_read_lock(env, obj, DT_TGT_CHILD);
735                 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
736                 dt_read_unlock(env, obj);
737
738                 total_size += rc < 0 ? 0 : rc;
739                 if (rc <= 0)
740                         break;
741
742                 rdbuf->rb_nbufs++;
743                 size -= read_size;
744         }
745
746         /* send pages to client */
747         rc = tgt_send_buffer(tsi, rdbuf);
748         if (rc < 0)
749                 GOTO(out_free, rc);
750
751         orr->orr_size = total_size;
752         orr->orr_offset = pos;
753
754         orr_cpu_to_le(orr, orr);
755         update_result->our_datalen += orr->orr_size;
756 out_free:
757         for (i = 0; i < nbufs; i++) {
758                 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
759                         OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
760                                  rdbuf->rb_bufs[i].lb_len);
761                 }
762         }
763         OBD_FREE(rdbuf, sizeof(*rdbuf) +
764                         nbufs * sizeof(rdbuf->rb_bufs[0]));
765 out:
766         /* Insert read buffer */
767         update_result->our_rc = ptlrpc_status_hton(rc);
768         reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
769                                                  sizeof(*update_result));
770         RETURN(rc);
771 }
772
773 static int out_noop(struct tgt_session_info *tsi)
774 {
775         return 0;
776 }
777
778 #define DEF_OUT_HNDL(opc, name, flags, fn)     \
779 [opc - OUT_CREATE] = {                                  \
780         .th_name    = name,                             \
781         .th_fail_id = 0,                                \
782         .th_opc     = opc,                              \
783         .th_flags   = flags,                            \
784         .th_act     = fn,                               \
785         .th_fmt     = NULL,                             \
786         .th_version = 0,                                \
787 }
788
789 static struct tgt_handler out_update_ops[] = {
790         DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
791                      out_create),
792         DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
793                      out_destroy),
794         DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
795                      out_ref_add),
796         DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
797                      out_ref_del),
798         DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set",  IS_MUTABLE | HAS_REPLY,
799                      out_attr_set),
800         DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get",  HAS_REPLY,
801                      out_attr_get),
802         DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
803                      out_xattr_set),
804         DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
805                      out_xattr_del),
806         DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
807                      out_xattr_get),
808         DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
809                      out_index_lookup),
810         DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
811                      IS_MUTABLE | HAS_REPLY, out_index_insert),
812         DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
813                      IS_MUTABLE | HAS_REPLY, out_index_delete),
814         DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
815         DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
816         DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
817         DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
818                      out_xattr_list),
819 };
820
821 static struct tgt_handler *out_handler_find(__u32 opc)
822 {
823         struct tgt_handler *h;
824
825         h = NULL;
826         if (OUT_CREATE <= opc && opc < OUT_LAST) {
827                 h = &out_update_ops[opc - OUT_CREATE];
828                 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
829                          h->th_opc, opc);
830         } else {
831                 h = NULL; /* unsupported opc */
832         }
833         return h;
834 }
835
836 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
837                         struct thandle_exec_args *ta, struct obd_export *exp)
838 {
839         ta->ta_argno = 0;
840         ta->ta_handle = dt_trans_create(env, dt);
841         if (IS_ERR(ta->ta_handle)) {
842                 int rc;
843
844                 rc = PTR_ERR(ta->ta_handle);
845                 ta->ta_handle = NULL;
846                 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
847                        rc);
848                 return rc;
849         }
850         if (exp->exp_need_sync)
851                 ta->ta_handle->th_sync = 1;
852
853         return 0;
854 }
855
856 static int out_trans_start(const struct lu_env *env,
857                            struct thandle_exec_args *ta)
858 {
859         return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
860 }
861
862 static int out_trans_stop(const struct lu_env *env,
863                           struct thandle_exec_args *ta, int err)
864 {
865         int i;
866         int rc;
867
868         ta->ta_handle->th_result = err;
869         rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
870         for (i = 0; i < ta->ta_argno; i++) {
871                 if (ta->ta_args[i]->object != NULL) {
872                         dt_object_put(env, ta->ta_args[i]->object);
873                         ta->ta_args[i]->object = NULL;
874                 }
875         }
876         ta->ta_handle = NULL;
877         ta->ta_argno = 0;
878
879         return rc;
880 }
881
882 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
883                       int declare_ret)
884 {
885         struct tgt_session_info *tsi = tgt_ses_info(env);
886         int                     i;
887         int                     rc;
888         int                     rc1;
889         ENTRY;
890
891         if (ta->ta_handle == NULL)
892                 RETURN(0);
893
894         if (declare_ret != 0 || ta->ta_argno == 0)
895                 GOTO(stop, rc = declare_ret);
896
897         LASSERT(ta->ta_handle->th_dev != NULL);
898         rc = out_trans_start(env, ta);
899         if (unlikely(rc != 0))
900                 GOTO(stop, rc);
901
902         for (i = 0; i < ta->ta_argno; i++) {
903                 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
904                                              ta->ta_args[i]);
905                 if (unlikely(rc != 0)) {
906                         CDEBUG(D_INFO, "error during execution of #%u from"
907                                " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
908                                ta->ta_args[i]->line, rc);
909                         while (--i >= 0) {
910                                 if (ta->ta_args[i]->undo_fn != NULL)
911                                         ta->ta_args[i]->undo_fn(env,
912                                                                ta->ta_handle,
913                                                                ta->ta_args[i]);
914                                 else
915                                         CERROR("%s: undo for %s:%d: rc = %d\n",
916                                              dt_obd_name(ta->ta_handle->th_dev),
917                                                ta->ta_args[i]->file,
918                                                ta->ta_args[i]->line, -ENOTSUPP);
919                         }
920                         break;
921                 }
922                 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
923                        dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
924         }
925
926         /* Only fail for real updates, XXX right now llog updates will be
927         * ignore, whose updates count is usually 1, so failover test
928         * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
929         * be removed after async update patch is landed. */
930         if (ta->ta_argno > 1)
931                 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
932
933 stop:
934         rc1 = out_trans_stop(env, ta, rc);
935         if (rc == 0)
936                 rc = rc1;
937
938         ta->ta_handle = NULL;
939         ta->ta_argno = 0;
940
941         RETURN(rc);
942 }
943
944 /**
945  * Object updates between Targets. Because all the updates has been
946  * dis-assemblied into object updates at sender side, so OUT will
947  * call OSD API directly to execute these updates.
948  *
949  * In DNE phase I all of the updates in the request need to be executed
950  * in one transaction, and the transaction has to be synchronously.
951  *
952  * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
953  * format.
954  */
955 int out_handle(struct tgt_session_info *tsi)
956 {
957         const struct lu_env             *env = tsi->tsi_env;
958         struct tgt_thread_info          *tti = tgt_th_info(env);
959         struct thandle_exec_args        *ta = &tti->tti_tea;
960         struct req_capsule              *pill = tsi->tsi_pill;
961         struct dt_device                *dt = tsi->tsi_tgt->lut_bottom;
962         struct out_update_header        *ouh;
963         struct out_update_buffer        *oub = NULL;
964         struct object_update            *update;
965         struct object_update_reply      *reply;
966         struct ptlrpc_bulk_desc         *desc = NULL;
967         void                            **update_bufs;
968         int                             current_batchid = -1;
969         __u32                           update_buf_count;
970         unsigned int                    i;
971         unsigned int                    reply_index = 0;
972         int                             rc = 0;
973         int                             rc1 = 0;
974         int                             ouh_size, reply_size;
975         int                             updates;
976         ENTRY;
977
978         req_capsule_set(pill, &RQF_OUT_UPDATE);
979         ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
980                                         RCL_CLIENT);
981         if (ouh_size <= 0)
982                 RETURN(err_serious(-EPROTO));
983
984         ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
985         if (ouh == NULL)
986                 RETURN(err_serious(-EPROTO));
987
988         if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
989                 CERROR("%s: invalid update buffer magic %x expect %x: "
990                        "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
991                        UPDATE_REQUEST_MAGIC, -EPROTO);
992                 RETURN(err_serious(-EPROTO));
993         }
994
995         update_buf_count = ouh->ouh_count;
996         if (update_buf_count == 0)
997                 RETURN(err_serious(-EPROTO));
998
999         OBD_ALLOC(update_bufs, sizeof(*update_bufs) * update_buf_count);
1000         if (update_bufs == NULL)
1001                 RETURN(err_serious(-ENOMEM));
1002
1003         if (ouh->ouh_inline_length > 0) {
1004                 update_bufs[0] = ouh->ouh_inline_data;
1005         } else {
1006                 struct out_update_buffer *tmp;
1007                 int page_count = 0;
1008
1009                 oub = req_capsule_client_get(pill, &RMF_OUT_UPDATE_BUF);
1010                 if (oub == NULL)
1011                         GOTO(out_free, rc = err_serious(-EPROTO));
1012
1013                 for (i = 0; i < update_buf_count; i++)
1014                         /* First *and* last might be partial pages, hence +1 */
1015                         page_count += DIV_ROUND_UP(oub[i].oub_size,
1016                                                    PAGE_SIZE) + 1;
1017
1018                 desc = ptlrpc_prep_bulk_exp(pill->rc_req, page_count,
1019                                             PTLRPC_BULK_OPS_COUNT,
1020                                             PTLRPC_BULK_GET_SINK,
1021                                             MDS_BULK_PORTAL,
1022                                             &ptlrpc_bulk_kiov_nopin_ops);
1023                 if (desc == NULL)
1024                         GOTO(out_free, rc = err_serious(-ENOMEM));
1025
1026                 tmp = oub;
1027                 for (i = 0; i < update_buf_count; i++, tmp++) {
1028                         if (tmp->oub_size >= OUT_MAXREQSIZE)
1029                                 GOTO(out_free, rc = err_serious(-EPROTO));
1030
1031                         OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1032                         if (update_bufs[i] == NULL)
1033                                 GOTO(out_free, rc = err_serious(-ENOMEM));
1034
1035                         desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1036                                                         tmp->oub_size);
1037                 }
1038
1039                 pill->rc_req->rq_bulk_write = 1;
1040                 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1041                 if (rc != 0)
1042                         GOTO(out_free, rc = err_serious(rc));
1043
1044                 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1045                 if (rc < 0)
1046                         GOTO(out_free, rc = err_serious(rc));
1047         }
1048         /* validate the request and calculate the total update count and
1049          * set it to reply */
1050         reply_size = 0;
1051         updates = 0;
1052         for (i = 0; i < update_buf_count; i++) {
1053                 struct object_update_request    *our;
1054                 int                              j;
1055
1056                 our = update_bufs[i];
1057                 if (ptlrpc_req_need_swab(pill->rc_req))
1058                         lustre_swab_object_update_request(our, 0);
1059
1060                 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1061                         CERROR("%s: invalid update buffer magic %x"
1062                                " expect %x: rc = %d\n",
1063                                tgt_name(tsi->tsi_tgt), our->ourq_magic,
1064                                UPDATE_REQUEST_MAGIC, -EPROTO);
1065                         GOTO(out_free, rc = err_serious(-EPROTO));
1066                 }
1067                 updates += our->ourq_count;
1068
1069                 /* need to calculate reply size */
1070                 for (j = 0; j < our->ourq_count; j++) {
1071                         update = object_update_request_get(our, j, NULL);
1072                         if (update == NULL)
1073                                 GOTO(out, rc = err_serious(-EPROTO));
1074                         if (ptlrpc_req_need_swab(pill->rc_req))
1075                                 lustre_swab_object_update(update);
1076
1077                         if (!fid_is_sane(&update->ou_fid)) {
1078                                 CERROR("%s: invalid FID "DFID": rc = %d\n",
1079                                        tgt_name(tsi->tsi_tgt),
1080                                        PFID(&update->ou_fid), -EPROTO);
1081                                 GOTO(out, rc = err_serious(-EPROTO));
1082                         }
1083
1084                         /* XXX: what ou_result_size can be considered safe? */
1085
1086                         reply_size += sizeof(reply->ourp_lens[0]);
1087                         reply_size += sizeof(struct object_update_result);
1088                         reply_size += update->ou_result_size;
1089                 }
1090         }
1091         reply_size += sizeof(*reply);
1092
1093         if (unlikely(reply_size > ouh->ouh_reply_size)) {
1094                 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1095                        tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1096                        updates, reply_size);
1097                 GOTO(out_free, rc = err_serious(-EPROTO));
1098         }
1099
1100         req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1101                              ouh->ouh_reply_size);
1102         rc = req_capsule_server_pack(pill);
1103         if (rc != 0) {
1104                 CERROR("%s: Can't pack response: rc = %d\n",
1105                        tgt_name(tsi->tsi_tgt), rc);
1106                 GOTO(out_free, rc = err_serious(-EPROTO));
1107         }
1108
1109         /* Prepare the update reply buffer */
1110         reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1111         if (reply == NULL)
1112                 GOTO(out_free, rc = -EPROTO);
1113         reply->ourp_magic = UPDATE_REPLY_MAGIC;
1114         reply->ourp_count = updates;
1115         tti->tti_u.update.tti_update_reply = reply;
1116         tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1117
1118         /* Walk through updates in the request to execute them */
1119         for (i = 0; i < update_buf_count; i++) {
1120                 struct tgt_handler      *h;
1121                 struct dt_object        *dt_obj;
1122                 int                     update_count;
1123                 struct object_update_request *our;
1124                 int                     j;
1125
1126                 our = update_bufs[i];
1127                 update_count = our->ourq_count;
1128                 for (j = 0; j < update_count; j++) {
1129                         struct lu_object_conf conf;
1130
1131                         update = object_update_request_get(our, j, NULL);
1132                         if (update->ou_type == OUT_CREATE)
1133                                 conf.loc_flags = LOC_F_NEW;
1134                         else
1135                                 conf.loc_flags = 0;
1136
1137                         dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1138                                 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1139                         if (IS_ERR(dt_obj))
1140                                 GOTO(out, rc = PTR_ERR(dt_obj));
1141
1142                         if (dt->dd_record_fid_accessed) {
1143                                 struct lfsck_req_local *lrl = &tti->tti_lrl;
1144
1145                                 lfsck_pack_rfa(lrl,
1146                                                lu_object_fid(&dt_obj->do_lu),
1147                                                LEL_FID_ACCESSED,
1148                                                LFSCK_TYPE_LAYOUT);
1149                                 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1150                         }
1151
1152                         tti->tti_u.update.tti_dt_object = dt_obj;
1153                         tti->tti_u.update.tti_update = update;
1154                         tti->tti_u.update.tti_update_reply_index = reply_index;
1155
1156                         h = out_handler_find(update->ou_type);
1157                         if (unlikely(h == NULL)) {
1158                                 CERROR("%s: unsupported opc: 0x%x\n",
1159                                        tgt_name(tsi->tsi_tgt), update->ou_type);
1160                                 GOTO(next, rc = -ENOTSUPP);
1161                         }
1162
1163                         /* Check resend case only for modifying RPC */
1164                         if (h->th_flags & IS_MUTABLE) {
1165                                 struct ptlrpc_request *req = tgt_ses_req(tsi);
1166
1167                                 if (out_check_resent(env, dt, dt_obj, req,
1168                                                      out_reconstruct, reply,
1169                                                      reply_index))
1170                                         GOTO(next, rc = 0);
1171
1172                                 if (dt->dd_rdonly)
1173                                         GOTO(next, rc = -EROFS);
1174                         }
1175
1176                         /* start transaction for modification RPC only */
1177                         if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1178                                 current_batchid = update->ou_batchid;
1179                                 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1180                                 if (rc != 0)
1181                                         GOTO(next, rc);
1182
1183                                 if (update->ou_flags & UPDATE_FL_SYNC)
1184                                         ta->ta_handle->th_sync = 1;
1185                         }
1186
1187                         /* Stop the current update transaction, if the update
1188                          * has different batchid, or read-only update */
1189                         if (((current_batchid != update->ou_batchid) ||
1190                              !(h->th_flags & IS_MUTABLE)) &&
1191                              ta->ta_handle != NULL) {
1192                                 rc = out_tx_end(env, ta, rc);
1193                                 current_batchid = -1;
1194                                 if (rc != 0)
1195                                         GOTO(next, rc);
1196
1197                                 /* start a new transaction if needed */
1198                                 if (h->th_flags & IS_MUTABLE) {
1199                                         rc = out_tx_start(env, dt, ta,
1200                                                           tsi->tsi_exp);
1201                                         if (rc != 0)
1202                                                 GOTO(next, rc);
1203                                         if (update->ou_flags & UPDATE_FL_SYNC)
1204                                                 ta->ta_handle->th_sync = 1;
1205                                         current_batchid = update->ou_batchid;
1206                                 }
1207                         }
1208
1209                         rc = h->th_act(tsi);
1210 next:
1211                         reply_index++;
1212                         dt_object_put(env, dt_obj);
1213                         if (rc < 0)
1214                                 GOTO(out, rc);
1215                 }
1216         }
1217 out:
1218         if (current_batchid != -1) {
1219                 rc1 = out_tx_end(env, ta, rc);
1220                 if (rc == 0)
1221                         rc = rc1;
1222         }
1223
1224 out_free:
1225         if (update_bufs != NULL) {
1226                 if (oub != NULL) {
1227                         for (i = 0; i < update_buf_count; i++, oub++) {
1228                                 if (update_bufs[i] != NULL)
1229                                         OBD_FREE_LARGE(update_bufs[i],
1230                                                        oub->oub_size);
1231                         }
1232                 }
1233
1234                 OBD_FREE(update_bufs, sizeof(*update_bufs) * update_buf_count);
1235         }
1236
1237         if (desc != NULL)
1238                 ptlrpc_free_bulk(desc);
1239
1240         RETURN(rc);
1241 }
1242
1243 struct tgt_handler tgt_out_handlers[] = {
1244 TGT_UPDATE_HDL(IS_MUTABLE,      OUT_UPDATE,     out_handle),
1245 };
1246 EXPORT_SYMBOL(tgt_out_handlers);
1247