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