Whamcloud - gitweb
c3efb6fd8a7248935d78913413250d31310dde24
[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(struct lu_rdbuf) +
719                          nbufs * sizeof(rdbuf->rb_bufs[0]));
720         if (rdbuf == NULL)
721                 GOTO(out, rc = -ENOMEM);
722
723         rdbuf->rb_nbufs = 0;
724         total_size = 0;
725         for (i = 0; i < nbufs; i++) {
726                 __u32 read_size;
727
728                 read_size = size > OUT_BULK_BUFFER_SIZE ?
729                             OUT_BULK_BUFFER_SIZE : size;
730                 OBD_ALLOC(rdbuf->rb_bufs[i].lb_buf, read_size);
731                 if (rdbuf->rb_bufs[i].lb_buf == NULL)
732                         GOTO(out_free, rc = -ENOMEM);
733
734                 rdbuf->rb_bufs[i].lb_len = read_size;
735                 dt_read_lock(env, obj, DT_TGT_CHILD);
736                 rc = dt_read(env, obj, &rdbuf->rb_bufs[i], &pos);
737                 dt_read_unlock(env, obj);
738
739                 total_size += rc < 0 ? 0 : rc;
740                 if (rc <= 0)
741                         break;
742
743                 rdbuf->rb_nbufs++;
744                 size -= read_size;
745         }
746
747         /* send pages to client */
748         rc = tgt_send_buffer(tsi, rdbuf);
749         if (rc < 0)
750                 GOTO(out_free, rc);
751
752         orr->orr_size = total_size;
753         orr->orr_offset = pos;
754
755         orr_cpu_to_le(orr, orr);
756         update_result->our_datalen += orr->orr_size;
757 out_free:
758         for (i = 0; i < nbufs; i++) {
759                 if (rdbuf->rb_bufs[i].lb_buf != NULL) {
760                         OBD_FREE(rdbuf->rb_bufs[i].lb_buf,
761                                  rdbuf->rb_bufs[i].lb_len);
762                 }
763         }
764         OBD_FREE(rdbuf, sizeof(struct lu_rdbuf) +
765                         nbufs * sizeof(rdbuf->rb_bufs[0]));
766 out:
767         /* Insert read buffer */
768         update_result->our_rc = ptlrpc_status_hton(rc);
769         reply->ourp_lens[index] = cfs_size_round(update_result->our_datalen +
770                                                  sizeof(*update_result));
771         RETURN(rc);
772 }
773
774 static int out_noop(struct tgt_session_info *tsi)
775 {
776         return 0;
777 }
778
779 #define DEF_OUT_HNDL(opc, name, flags, fn)     \
780 [opc - OUT_CREATE] = {                                  \
781         .th_name    = name,                             \
782         .th_fail_id = 0,                                \
783         .th_opc     = opc,                              \
784         .th_flags   = flags,                            \
785         .th_act     = fn,                               \
786         .th_fmt     = NULL,                             \
787         .th_version = 0,                                \
788 }
789
790 static struct tgt_handler out_update_ops[] = {
791         DEF_OUT_HNDL(OUT_CREATE, "out_create", IS_MUTABLE | HAS_REPLY,
792                      out_create),
793         DEF_OUT_HNDL(OUT_DESTROY, "out_create", IS_MUTABLE | HAS_REPLY,
794                      out_destroy),
795         DEF_OUT_HNDL(OUT_REF_ADD, "out_ref_add", IS_MUTABLE | HAS_REPLY,
796                      out_ref_add),
797         DEF_OUT_HNDL(OUT_REF_DEL, "out_ref_del", IS_MUTABLE | HAS_REPLY,
798                      out_ref_del),
799         DEF_OUT_HNDL(OUT_ATTR_SET, "out_attr_set",  IS_MUTABLE | HAS_REPLY,
800                      out_attr_set),
801         DEF_OUT_HNDL(OUT_ATTR_GET, "out_attr_get",  HAS_REPLY,
802                      out_attr_get),
803         DEF_OUT_HNDL(OUT_XATTR_SET, "out_xattr_set", IS_MUTABLE | HAS_REPLY,
804                      out_xattr_set),
805         DEF_OUT_HNDL(OUT_XATTR_DEL, "out_xattr_del", IS_MUTABLE | HAS_REPLY,
806                      out_xattr_del),
807         DEF_OUT_HNDL(OUT_XATTR_GET, "out_xattr_get", HAS_REPLY,
808                      out_xattr_get),
809         DEF_OUT_HNDL(OUT_INDEX_LOOKUP, "out_index_lookup", HAS_REPLY,
810                      out_index_lookup),
811         DEF_OUT_HNDL(OUT_INDEX_INSERT, "out_index_insert",
812                      IS_MUTABLE | HAS_REPLY, out_index_insert),
813         DEF_OUT_HNDL(OUT_INDEX_DELETE, "out_index_delete",
814                      IS_MUTABLE | HAS_REPLY, out_index_delete),
815         DEF_OUT_HNDL(OUT_WRITE, "out_write", IS_MUTABLE | HAS_REPLY, out_write),
816         DEF_OUT_HNDL(OUT_READ, "out_read", HAS_REPLY, out_read),
817         DEF_OUT_HNDL(OUT_NOOP, "out_noop", HAS_REPLY, out_noop),
818         DEF_OUT_HNDL(OUT_XATTR_LIST, "out_xattr_list", HAS_REPLY,
819                      out_xattr_list),
820 };
821
822 static struct tgt_handler *out_handler_find(__u32 opc)
823 {
824         struct tgt_handler *h;
825
826         h = NULL;
827         if (OUT_CREATE <= opc && opc < OUT_LAST) {
828                 h = &out_update_ops[opc - OUT_CREATE];
829                 LASSERTF(h->th_opc == opc, "opcode mismatch %d != %d\n",
830                          h->th_opc, opc);
831         } else {
832                 h = NULL; /* unsupported opc */
833         }
834         return h;
835 }
836
837 static int out_tx_start(const struct lu_env *env, struct dt_device *dt,
838                         struct thandle_exec_args *ta, struct obd_export *exp)
839 {
840         ta->ta_argno = 0;
841         ta->ta_handle = dt_trans_create(env, dt);
842         if (IS_ERR(ta->ta_handle)) {
843                 int rc;
844
845                 rc = PTR_ERR(ta->ta_handle);
846                 ta->ta_handle = NULL;
847                 CERROR("%s: start handle error: rc = %d\n", dt_obd_name(dt),
848                        rc);
849                 return rc;
850         }
851         if (exp->exp_need_sync)
852                 ta->ta_handle->th_sync = 1;
853
854         return 0;
855 }
856
857 static int out_trans_start(const struct lu_env *env,
858                            struct thandle_exec_args *ta)
859 {
860         return dt_trans_start(env, ta->ta_handle->th_dev, ta->ta_handle);
861 }
862
863 static int out_trans_stop(const struct lu_env *env,
864                           struct thandle_exec_args *ta, int err)
865 {
866         int i;
867         int rc;
868
869         ta->ta_handle->th_result = err;
870         rc = dt_trans_stop(env, ta->ta_handle->th_dev, ta->ta_handle);
871         for (i = 0; i < ta->ta_argno; i++) {
872                 if (ta->ta_args[i]->object != NULL) {
873                         dt_object_put(env, ta->ta_args[i]->object);
874                         ta->ta_args[i]->object = NULL;
875                 }
876         }
877         ta->ta_handle = NULL;
878         ta->ta_argno = 0;
879
880         return rc;
881 }
882
883 static int out_tx_end(const struct lu_env *env, struct thandle_exec_args *ta,
884                       int declare_ret)
885 {
886         struct tgt_session_info *tsi = tgt_ses_info(env);
887         int                     i;
888         int                     rc;
889         int                     rc1;
890         ENTRY;
891
892         if (ta->ta_handle == NULL)
893                 RETURN(0);
894
895         if (declare_ret != 0 || ta->ta_argno == 0)
896                 GOTO(stop, rc = declare_ret);
897
898         LASSERT(ta->ta_handle->th_dev != NULL);
899         rc = out_trans_start(env, ta);
900         if (unlikely(rc != 0))
901                 GOTO(stop, rc);
902
903         for (i = 0; i < ta->ta_argno; i++) {
904                 rc = ta->ta_args[i]->exec_fn(env, ta->ta_handle,
905                                              ta->ta_args[i]);
906                 if (unlikely(rc != 0)) {
907                         CDEBUG(D_INFO, "error during execution of #%u from"
908                                " %s:%d: rc = %d\n", i, ta->ta_args[i]->file,
909                                ta->ta_args[i]->line, rc);
910                         while (--i >= 0) {
911                                 if (ta->ta_args[i]->undo_fn != NULL)
912                                         ta->ta_args[i]->undo_fn(env,
913                                                                ta->ta_handle,
914                                                                ta->ta_args[i]);
915                                 else
916                                         CERROR("%s: undo for %s:%d: rc = %d\n",
917                                              dt_obd_name(ta->ta_handle->th_dev),
918                                                ta->ta_args[i]->file,
919                                                ta->ta_args[i]->line, -ENOTSUPP);
920                         }
921                         break;
922                 }
923                 CDEBUG(D_INFO, "%s: executed %u/%u: rc = %d\n",
924                        dt_obd_name(ta->ta_handle->th_dev), i, ta->ta_argno, rc);
925         }
926
927         /* Only fail for real updates, XXX right now llog updates will be
928         * ignore, whose updates count is usually 1, so failover test
929         * case will spot this FAIL_UPDATE_NET_REP precisely, and it will
930         * be removed after async update patch is landed. */
931         if (ta->ta_argno > 1)
932                 tsi->tsi_reply_fail_id = OBD_FAIL_OUT_UPDATE_NET_REP;
933
934 stop:
935         rc1 = out_trans_stop(env, ta, rc);
936         if (rc == 0)
937                 rc = rc1;
938
939         ta->ta_handle = NULL;
940         ta->ta_argno = 0;
941
942         RETURN(rc);
943 }
944
945 /**
946  * Object updates between Targets. Because all the updates has been
947  * dis-assemblied into object updates at sender side, so OUT will
948  * call OSD API directly to execute these updates.
949  *
950  * In DNE phase I all of the updates in the request need to be executed
951  * in one transaction, and the transaction has to be synchronously.
952  *
953  * Please refer to lustre/include/lustre/lustre_idl.h for req/reply
954  * format.
955  */
956 int out_handle(struct tgt_session_info *tsi)
957 {
958         const struct lu_env             *env = tsi->tsi_env;
959         struct tgt_thread_info          *tti = tgt_th_info(env);
960         struct thandle_exec_args        *ta = &tti->tti_tea;
961         struct req_capsule              *pill = tsi->tsi_pill;
962         struct dt_device                *dt = tsi->tsi_tgt->lut_bottom;
963         struct out_update_header        *ouh;
964         struct out_update_buffer        *oub = NULL;
965         struct object_update            *update;
966         struct object_update_reply      *reply;
967         struct ptlrpc_bulk_desc         *desc = NULL;
968         void                            **update_bufs;
969         int                             current_batchid = -1;
970         __u32                           update_buf_count;
971         unsigned int                    i;
972         unsigned int                    reply_index = 0;
973         int                             rc = 0;
974         int                             rc1 = 0;
975         int                             ouh_size, reply_size;
976         int                             updates;
977         ENTRY;
978
979         req_capsule_set(pill, &RQF_OUT_UPDATE);
980         ouh_size = req_capsule_get_size(pill, &RMF_OUT_UPDATE_HEADER,
981                                         RCL_CLIENT);
982         if (ouh_size <= 0)
983                 RETURN(err_serious(-EPROTO));
984
985         ouh = req_capsule_client_get(pill, &RMF_OUT_UPDATE_HEADER);
986         if (ouh == NULL)
987                 RETURN(err_serious(-EPROTO));
988
989         if (ouh->ouh_magic != OUT_UPDATE_HEADER_MAGIC) {
990                 CERROR("%s: invalid update buffer magic %x expect %x: "
991                        "rc = %d\n", tgt_name(tsi->tsi_tgt), ouh->ouh_magic,
992                        UPDATE_REQUEST_MAGIC, -EPROTO);
993                 RETURN(err_serious(-EPROTO));
994         }
995
996         update_buf_count = ouh->ouh_count;
997         if (update_buf_count == 0)
998                 RETURN(err_serious(-EPROTO));
999
1000         OBD_ALLOC(update_bufs, sizeof(*update_bufs) * update_buf_count);
1001         if (update_bufs == NULL)
1002                 RETURN(err_serious(-ENOMEM));
1003
1004         if (ouh->ouh_inline_length > 0) {
1005                 update_bufs[0] = ouh->ouh_inline_data;
1006         } else {
1007                 struct out_update_buffer *tmp;
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                 desc = ptlrpc_prep_bulk_exp(pill->rc_req, update_buf_count,
1014                                             PTLRPC_BULK_OPS_COUNT,
1015                                             PTLRPC_BULK_GET_SINK |
1016                                             PTLRPC_BULK_BUF_KVEC,
1017                                             MDS_BULK_PORTAL,
1018                                             &ptlrpc_bulk_kvec_ops);
1019                 if (desc == NULL)
1020                         GOTO(out_free, rc = err_serious(-ENOMEM));
1021
1022                 tmp = oub;
1023                 for (i = 0; i < update_buf_count; i++, tmp++) {
1024                         if (tmp->oub_size >= OUT_MAXREQSIZE)
1025                                 GOTO(out_free, rc = err_serious(-EPROTO));
1026
1027                         OBD_ALLOC_LARGE(update_bufs[i], tmp->oub_size);
1028                         if (update_bufs[i] == NULL)
1029                                 GOTO(out_free, rc = err_serious(-ENOMEM));
1030
1031                         desc->bd_frag_ops->add_iov_frag(desc, update_bufs[i],
1032                                                         tmp->oub_size);
1033                 }
1034
1035                 pill->rc_req->rq_bulk_write = 1;
1036                 rc = sptlrpc_svc_prep_bulk(pill->rc_req, desc);
1037                 if (rc != 0)
1038                         GOTO(out_free, rc = err_serious(rc));
1039
1040                 rc = target_bulk_io(pill->rc_req->rq_export, desc);
1041                 if (rc < 0)
1042                         GOTO(out_free, rc = err_serious(rc));
1043         }
1044         /* validate the request and calculate the total update count and
1045          * set it to reply */
1046         reply_size = 0;
1047         updates = 0;
1048         for (i = 0; i < update_buf_count; i++) {
1049                 struct object_update_request    *our;
1050                 int                              j;
1051
1052                 our = update_bufs[i];
1053                 if (ptlrpc_req_need_swab(pill->rc_req))
1054                         lustre_swab_object_update_request(our);
1055
1056                 if (our->ourq_magic != UPDATE_REQUEST_MAGIC) {
1057                         CERROR("%s: invalid update buffer magic %x"
1058                                " expect %x: rc = %d\n",
1059                                tgt_name(tsi->tsi_tgt), our->ourq_magic,
1060                                UPDATE_REQUEST_MAGIC, -EPROTO);
1061                         GOTO(out_free, rc = err_serious(-EPROTO));
1062                 }
1063                 updates += our->ourq_count;
1064
1065                 /* need to calculate reply size */
1066                 for (j = 0; j < our->ourq_count; j++) {
1067                         update = object_update_request_get(our, j, NULL);
1068                         if (update == NULL)
1069                                 GOTO(out, rc = err_serious(-EPROTO));
1070                         if (ptlrpc_req_need_swab(pill->rc_req))
1071                                 lustre_swab_object_update(update);
1072
1073                         if (!fid_is_sane(&update->ou_fid)) {
1074                                 CERROR("%s: invalid FID "DFID": rc = %d\n",
1075                                        tgt_name(tsi->tsi_tgt),
1076                                        PFID(&update->ou_fid), -EPROTO);
1077                                 GOTO(out, rc = err_serious(-EPROTO));
1078                         }
1079
1080                         /* XXX: what ou_result_size can be considered safe? */
1081
1082                         reply_size += sizeof(reply->ourp_lens[0]);
1083                         reply_size += sizeof(struct object_update_result);
1084                         reply_size += update->ou_result_size;
1085                 }
1086         }
1087         reply_size += sizeof(*reply);
1088
1089         if (unlikely(reply_size > ouh->ouh_reply_size)) {
1090                 CERROR("%s: too small reply buf %u for %u, need %u at least\n",
1091                        tgt_name(tsi->tsi_tgt), ouh->ouh_reply_size,
1092                        updates, reply_size);
1093                 GOTO(out_free, rc = err_serious(-EPROTO));
1094         }
1095
1096         req_capsule_set_size(pill, &RMF_OUT_UPDATE_REPLY, RCL_SERVER,
1097                              ouh->ouh_reply_size);
1098         rc = req_capsule_server_pack(pill);
1099         if (rc != 0) {
1100                 CERROR("%s: Can't pack response: rc = %d\n",
1101                        tgt_name(tsi->tsi_tgt), rc);
1102                 GOTO(out_free, rc = err_serious(-EPROTO));
1103         }
1104
1105         /* Prepare the update reply buffer */
1106         reply = req_capsule_server_get(pill, &RMF_OUT_UPDATE_REPLY);
1107         if (reply == NULL)
1108                 GOTO(out_free, rc = -EPROTO);
1109         reply->ourp_magic = UPDATE_REPLY_MAGIC;
1110         reply->ourp_count = updates;
1111         tti->tti_u.update.tti_update_reply = reply;
1112         tti->tti_mult_trans = !req_is_replay(tgt_ses_req(tsi));
1113
1114         /* Walk through updates in the request to execute them */
1115         for (i = 0; i < update_buf_count; i++) {
1116                 struct tgt_handler      *h;
1117                 struct dt_object        *dt_obj;
1118                 int                     update_count;
1119                 struct object_update_request *our;
1120                 int                     j;
1121
1122                 our = update_bufs[i];
1123                 update_count = our->ourq_count;
1124                 for (j = 0; j < update_count; j++) {
1125                         struct lu_object_conf conf;
1126
1127                         update = object_update_request_get(our, j, NULL);
1128                         if (update->ou_type == OUT_CREATE)
1129                                 conf.loc_flags = LOC_F_NEW;
1130                         else
1131                                 conf.loc_flags = 0;
1132
1133                         dt_obj = dt_locate_at(env, dt, &update->ou_fid,
1134                                 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1135                         if (IS_ERR(dt_obj))
1136                                 GOTO(out, rc = PTR_ERR(dt_obj));
1137
1138                         if (dt->dd_record_fid_accessed) {
1139                                 struct lfsck_req_local *lrl = &tti->tti_lrl;
1140
1141                                 lfsck_pack_rfa(lrl,
1142                                                lu_object_fid(&dt_obj->do_lu),
1143                                                LEL_FID_ACCESSED,
1144                                                LFSCK_TYPE_LAYOUT);
1145                                 tgt_lfsck_in_notify_local(env, dt, lrl, NULL);
1146                         }
1147
1148                         tti->tti_u.update.tti_dt_object = dt_obj;
1149                         tti->tti_u.update.tti_update = update;
1150                         tti->tti_u.update.tti_update_reply_index = reply_index;
1151
1152                         h = out_handler_find(update->ou_type);
1153                         if (unlikely(h == NULL)) {
1154                                 CERROR("%s: unsupported opc: 0x%x\n",
1155                                        tgt_name(tsi->tsi_tgt), update->ou_type);
1156                                 GOTO(next, rc = -ENOTSUPP);
1157                         }
1158
1159                         /* Check resend case only for modifying RPC */
1160                         if (h->th_flags & IS_MUTABLE) {
1161                                 struct ptlrpc_request *req = tgt_ses_req(tsi);
1162
1163                                 if (out_check_resent(env, dt, dt_obj, req,
1164                                                      out_reconstruct, reply,
1165                                                      reply_index))
1166                                         GOTO(next, rc = 0);
1167
1168                                 if (dt->dd_rdonly)
1169                                         GOTO(next, rc = -EROFS);
1170                         }
1171
1172                         /* start transaction for modification RPC only */
1173                         if (h->th_flags & IS_MUTABLE && current_batchid == -1) {
1174                                 current_batchid = update->ou_batchid;
1175                                 rc = out_tx_start(env, dt, ta, tsi->tsi_exp);
1176                                 if (rc != 0)
1177                                         GOTO(next, rc);
1178
1179                                 if (update->ou_flags & UPDATE_FL_SYNC)
1180                                         ta->ta_handle->th_sync = 1;
1181                         }
1182
1183                         /* Stop the current update transaction, if the update
1184                          * has different batchid, or read-only update */
1185                         if (((current_batchid != update->ou_batchid) ||
1186                              !(h->th_flags & IS_MUTABLE)) &&
1187                              ta->ta_handle != NULL) {
1188                                 rc = out_tx_end(env, ta, rc);
1189                                 current_batchid = -1;
1190                                 if (rc != 0)
1191                                         GOTO(next, rc);
1192
1193                                 /* start a new transaction if needed */
1194                                 if (h->th_flags & IS_MUTABLE) {
1195                                         rc = out_tx_start(env, dt, ta,
1196                                                           tsi->tsi_exp);
1197                                         if (rc != 0)
1198                                                 GOTO(next, rc);
1199                                         if (update->ou_flags & UPDATE_FL_SYNC)
1200                                                 ta->ta_handle->th_sync = 1;
1201                                         current_batchid = update->ou_batchid;
1202                                 }
1203                         }
1204
1205                         rc = h->th_act(tsi);
1206 next:
1207                         reply_index++;
1208                         dt_object_put(env, dt_obj);
1209                         if (rc < 0)
1210                                 GOTO(out, rc);
1211                 }
1212         }
1213 out:
1214         if (current_batchid != -1) {
1215                 rc1 = out_tx_end(env, ta, rc);
1216                 if (rc == 0)
1217                         rc = rc1;
1218         }
1219
1220 out_free:
1221         if (update_bufs != NULL) {
1222                 if (oub != NULL) {
1223                         for (i = 0; i < update_buf_count; i++, oub++) {
1224                                 if (update_bufs[i] != NULL)
1225                                         OBD_FREE_LARGE(update_bufs[i],
1226                                                        oub->oub_size);
1227                         }
1228                 }
1229
1230                 OBD_FREE(update_bufs, sizeof(*update_bufs) * update_buf_count);
1231         }
1232
1233         if (desc != NULL)
1234                 ptlrpc_free_bulk(desc);
1235
1236         RETURN(rc);
1237 }
1238
1239 struct tgt_handler tgt_out_handlers[] = {
1240 TGT_UPDATE_HDL(IS_MUTABLE,      OUT_UPDATE,     out_handle),
1241 };
1242 EXPORT_SYMBOL(tgt_out_handlers);
1243