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