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