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