Whamcloud - gitweb
LU-7318 out: dynamic reply size
[fs/lustre-release.git] / lustre / target / out_lib.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) 2014, 2015, Intel Corporation.
24  */
25 /*
26  * lustre/target/out_lib.c
27  *
28  * Author: Di Wang <di.wang@intel.com>
29  * Author: Fan, Yong <fan.yong@intel.com>
30  */
31
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <lu_target.h>
35 #include <md_object.h>
36 #include <lustre_update.h>
37 #include <obd.h>
38 #include <obd_class.h>
39 #include "tgt_internal.h"
40
41 const char *update_op_str(__u16 opc)
42 {
43         static const char *opc_str[] = {
44                 [OUT_START] = "start",
45                 [OUT_CREATE] = "create",
46                 [OUT_DESTROY] = "destroy",
47                 [OUT_REF_ADD] = "ref_add",
48                 [OUT_REF_DEL] = "ref_del" ,
49                 [OUT_ATTR_SET] = "attr_set",
50                 [OUT_ATTR_GET] = "attr_get",
51                 [OUT_XATTR_SET] = "xattr_set",
52                 [OUT_XATTR_GET] = "xattr_get",
53                 [OUT_INDEX_LOOKUP] = "lookup",
54                 [OUT_INDEX_INSERT] = "insert",
55                 [OUT_INDEX_DELETE] = "delete",
56                 [OUT_WRITE] = "write",
57                 [OUT_XATTR_DEL] = "xattr_del",
58                 [OUT_PUNCH] = "punch",
59                 [OUT_READ] = "read",
60                 [OUT_NOOP] = "noop",
61         };
62
63         if (opc < ARRAY_SIZE(opc_str) && opc_str[opc] != NULL)
64                 return opc_str[opc];
65         else
66                 return "unknown";
67 }
68 EXPORT_SYMBOL(update_op_str);
69
70 /**
71  * Fill object update header
72  *
73  * Only fill the object update header, and parameters will be filled later
74  * in other functions.
75  *
76  * \params[in] env              execution environment
77  * \params[in] update           object update to be filled
78  * \params[in,out] max_update_size      maximum object update size, if the
79  *                                      current update length equals or
80  *                                      exceeds the size, it will return -E2BIG.
81  * \params[in] update_op        update type
82  * \params[in] fid              object FID of the update
83  * \params[in] param_count      the count of the update parameters
84  * \params[in] param_sizes      the length of each parameters
85  *
86  * \retval                      0 if packing succeeds.
87  * \retval                      -E2BIG if packing exceeds the maximum length.
88  */
89 int out_update_header_pack(const struct lu_env *env,
90                            struct object_update *update,
91                            size_t *max_update_size,
92                            enum update_type update_op,
93                            const struct lu_fid *fid,
94                            unsigned int param_count,
95                            __u16 *param_sizes,
96                            __u32 reply_size)
97 {
98         struct object_update_param      *param;
99         unsigned int                    i;
100         size_t                          update_size;
101
102         if (((reply_size + 7) >> 3) >= 1ULL << 16)
103                 return -EINVAL;
104
105         /* Check whether the packing exceeding the maxima update length */
106         update_size = sizeof(*update);
107         for (i = 0; i < param_count; i++)
108                 update_size += cfs_size_round(sizeof(*param) + param_sizes[i]);
109
110         if (unlikely(update_size >= *max_update_size)) {
111                 *max_update_size = update_size;
112                 return -E2BIG;
113         }
114
115         update->ou_fid = *fid;
116         update->ou_type = update_op;
117         update->ou_params_count = param_count;
118         update->ou_result_size = reply_size;
119         param = &update->ou_params[0];
120         for (i = 0; i < param_count; i++) {
121                 param->oup_len = param_sizes[i];
122                 param = (struct object_update_param *)((char *)param +
123                          object_update_param_size(param));
124         }
125
126         return 0;
127 }
128
129 /**
130  * Packs one update into the update_buffer.
131  *
132  * \param[in] env       execution environment
133  * \param[in] update    update to be packed
134  * \param[in] max_update_size   *maximum size of \a update
135  * \param[in] op        update operation (enum update_type)
136  * \param[in] fid       object FID for this update
137  * \param[in] param_count       number of parameters for this update
138  * \param[in] param_sizes       array of parameters length of this update
139  * \param[in] param_bufs        parameter buffers
140  *
141  * \retval              = 0 if updates packing succeeds
142  * \retval              negative errno if updates packing fails
143  **/
144 int out_update_pack(const struct lu_env *env, struct object_update *update,
145                     size_t *max_update_size, enum update_type op,
146                     const struct lu_fid *fid, unsigned int param_count,
147                     __u16 *param_sizes, const void **param_bufs,
148                     __u32 reply_size)
149 {
150         struct object_update_param      *param;
151         unsigned int                    i;
152         int                             rc;
153         ENTRY;
154
155         rc = out_update_header_pack(env, update, max_update_size, op, fid,
156                                     param_count, param_sizes, reply_size);
157         if (rc != 0)
158                 RETURN(rc);
159
160         param = &update->ou_params[0];
161         for (i = 0; i < param_count; i++) {
162                 memcpy(&param->oup_buf[0], param_bufs[i], param_sizes[i]);
163                 param = (struct object_update_param *)((char *)param +
164                          object_update_param_size(param));
165         }
166
167         RETURN(0);
168 }
169 EXPORT_SYMBOL(out_update_pack);
170
171 /**
172  * Pack various updates into the update_buffer.
173  *
174  * The following functions pack different updates into the update_buffer
175  * So parameters of these API is basically same as its correspondent OSD/OSP
176  * API, for detail description of these parameters see osd_handler.c or
177  * osp_md_object.c.
178  *
179  * \param[in] env       execution environment
180  * \param[in] ubuf      update buffer
181  * \param[in] fid       fid of this object for the update
182  *
183  * \retval              0 if insertion succeeds.
184  * \retval              negative errno if insertion fails.
185  */
186 int out_create_pack(const struct lu_env *env, struct object_update *update,
187                     size_t *max_update_size, const struct lu_fid *fid,
188                     const struct lu_attr *attr, struct dt_allocation_hint *hint,
189                     struct dt_object_format *dof)
190 {
191         struct obdo             *obdo;
192         __u16                   sizes[2] = {sizeof(*obdo), 0};
193         int                     buf_count = 1;
194         const struct lu_fid     *parent_fid = NULL;
195         int                     rc;
196         ENTRY;
197
198         if (hint != NULL && hint->dah_parent) {
199                 parent_fid = lu_object_fid(&hint->dah_parent->do_lu);
200                 sizes[1] = sizeof(*parent_fid);
201                 buf_count++;
202         }
203
204         rc = out_update_header_pack(env, update, max_update_size, OUT_CREATE,
205                                     fid, buf_count, sizes, 0);
206         if (rc != 0)
207                 RETURN(rc);
208
209         obdo = object_update_param_get(update, 0, NULL);
210         LASSERT(obdo != NULL);
211         obdo->o_valid = 0;
212         obdo_from_la(obdo, attr, attr->la_valid);
213         lustre_set_wire_obdo(NULL, obdo, obdo);
214
215         if (parent_fid != NULL) {
216                 struct lu_fid *tmp;
217
218                 tmp = object_update_param_get(update, 1, NULL);
219                 LASSERT(tmp != NULL);
220                 fid_cpu_to_le(tmp, parent_fid);
221         }
222
223         RETURN(0);
224 }
225 EXPORT_SYMBOL(out_create_pack);
226
227 int out_ref_del_pack(const struct lu_env *env, struct object_update *update,
228                      size_t *max_update_size, const struct lu_fid *fid)
229 {
230         return out_update_pack(env, update, max_update_size, OUT_REF_DEL, fid,
231                                0, NULL, NULL, 0);
232 }
233 EXPORT_SYMBOL(out_ref_del_pack);
234
235 int out_ref_add_pack(const struct lu_env *env, struct object_update *update,
236                      size_t *max_update_size, const struct lu_fid *fid)
237 {
238         return out_update_pack(env, update, max_update_size, OUT_REF_ADD, fid,
239                                0, NULL, NULL, 0);
240 }
241 EXPORT_SYMBOL(out_ref_add_pack);
242
243 int out_attr_set_pack(const struct lu_env *env, struct object_update *update,
244                       size_t *max_update_size, const struct lu_fid *fid,
245                       const struct lu_attr *attr)
246 {
247         struct obdo             *obdo;
248         __u16                   size = sizeof(*obdo);
249         int                     rc;
250         ENTRY;
251
252         rc = out_update_header_pack(env, update, max_update_size,
253                                     OUT_ATTR_SET, fid, 1, &size, 0);
254         if (rc != 0)
255                 RETURN(rc);
256
257         obdo = object_update_param_get(update, 0, NULL);
258         LASSERT(obdo != NULL);
259         obdo->o_valid = 0;
260         obdo_from_la(obdo, attr, attr->la_valid);
261         lustre_set_wire_obdo(NULL, obdo, obdo);
262
263         RETURN(0);
264 }
265 EXPORT_SYMBOL(out_attr_set_pack);
266
267 int out_xattr_set_pack(const struct lu_env *env, struct object_update *update,
268                        size_t *max_update_size, const struct lu_fid *fid,
269                        const struct lu_buf *buf, const char *name, __u32 flag)
270 {
271         __u16   sizes[3] = {strlen(name) + 1, buf->lb_len, sizeof(flag)};
272         const void *bufs[3] = {(char *)name, (char *)buf->lb_buf,
273                                (char *)&flag};
274
275         return out_update_pack(env, update, max_update_size, OUT_XATTR_SET,
276                                fid, ARRAY_SIZE(sizes), sizes, bufs, 0);
277 }
278 EXPORT_SYMBOL(out_xattr_set_pack);
279
280 int out_xattr_del_pack(const struct lu_env *env, struct object_update *update,
281                        size_t *max_update_size, const struct lu_fid *fid,
282                        const char *name)
283 {
284         __u16   size = strlen(name) + 1;
285
286         return out_update_pack(env, update, max_update_size, OUT_XATTR_DEL,
287                                fid, 1, &size, (const void **)&name, 0);
288 }
289 EXPORT_SYMBOL(out_xattr_del_pack);
290
291 int out_index_insert_pack(const struct lu_env *env,
292                           struct object_update *update,
293                           size_t *max_update_size, const struct lu_fid *fid,
294                           const struct dt_rec *rec, const struct dt_key *key)
295 {
296         struct dt_insert_rec       *rec1 = (struct dt_insert_rec *)rec;
297         struct lu_fid              rec_fid;
298         __u32                       type = cpu_to_le32(rec1->rec_type);
299         __u16                       sizes[3] = { strlen((char *)key) + 1,
300                                                 sizeof(rec_fid),
301                                                 sizeof(type) };
302         const void                 *bufs[3] = { (char *)key,
303                                                 (char *)&rec_fid,
304                                                 (char *)&type };
305
306         fid_cpu_to_le(&rec_fid, rec1->rec_fid);
307
308         return out_update_pack(env, update, max_update_size, OUT_INDEX_INSERT,
309                                fid, ARRAY_SIZE(sizes), sizes, bufs, 0);
310 }
311 EXPORT_SYMBOL(out_index_insert_pack);
312
313 int out_index_delete_pack(const struct lu_env *env,
314                           struct object_update *update,
315                           size_t *max_update_size, const struct lu_fid *fid,
316                           const struct dt_key *key)
317 {
318         __u16   size = strlen((char *)key) + 1;
319         const void *buf = key;
320
321         return out_update_pack(env, update, max_update_size, OUT_INDEX_DELETE,
322                                fid, 1, &size, &buf, 0);
323 }
324 EXPORT_SYMBOL(out_index_delete_pack);
325
326 int out_object_destroy_pack(const struct lu_env *env,
327                             struct object_update *update,
328                             size_t *max_update_size, const struct lu_fid *fid)
329 {
330         return out_update_pack(env, update, max_update_size, OUT_DESTROY, fid,
331                                0, NULL, NULL, 0);
332 }
333 EXPORT_SYMBOL(out_object_destroy_pack);
334
335 int out_write_pack(const struct lu_env *env, struct object_update *update,
336                    size_t *max_update_size, const struct lu_fid *fid,
337                    const struct lu_buf *buf, __u64 pos)
338 {
339         __u16           sizes[2] = {buf->lb_len, sizeof(pos)};
340         const void      *bufs[2] = {(char *)buf->lb_buf, (char *)&pos};
341         int             rc;
342
343         pos = cpu_to_le64(pos);
344
345         rc = out_update_pack(env, update, max_update_size, OUT_WRITE, fid,
346                              ARRAY_SIZE(sizes), sizes, bufs, 0);
347         return rc;
348 }
349 EXPORT_SYMBOL(out_write_pack);
350
351 /**
352  * Pack various readonly updates into the update_buffer.
353  *
354  * The following update funcs are only used by read-only ops, lookup,
355  * getattr etc, so it does not need transaction here. Currently they
356  * are only used by OSP.
357  *
358  * \param[in] env       execution environment
359  * \param[in] fid       fid of this object for the update
360  * \param[in] ubuf      update buffer
361  *
362  * \retval              = 0 pack succeed.
363  *                      < 0 pack failed.
364  **/
365 int out_index_lookup_pack(const struct lu_env *env,
366                           struct object_update *update,
367                           size_t *max_update_size, const struct lu_fid *fid,
368                           struct dt_rec *rec, const struct dt_key *key)
369 {
370         const void      *name = key;
371         __u16           size = strlen((char *)name) + 1;
372
373         /* XXX: this shouldn't be hardcoded */
374         return out_update_pack(env, update, max_update_size, OUT_INDEX_LOOKUP,
375                                fid, 1, &size, &name, 256);
376 }
377 EXPORT_SYMBOL(out_index_lookup_pack);
378
379 int out_attr_get_pack(const struct lu_env *env, struct object_update *update,
380                       size_t *max_update_size, const struct lu_fid *fid)
381 {
382         return out_update_pack(env, update, max_update_size, OUT_ATTR_GET,
383                                fid, 0, NULL, NULL, sizeof(struct obdo));
384 }
385 EXPORT_SYMBOL(out_attr_get_pack);
386
387 int out_xattr_get_pack(const struct lu_env *env, struct object_update *update,
388                        size_t *max_update_size, const struct lu_fid *fid,
389                        const char *name, const int bufsize)
390 {
391         __u16 size;
392
393         LASSERT(name != NULL);
394         size = strlen(name) + 1;
395
396         return out_update_pack(env, update, max_update_size, OUT_XATTR_GET,
397                                fid, 1, &size, (const void **)&name, bufsize);
398 }
399 EXPORT_SYMBOL(out_xattr_get_pack);
400
401 int out_read_pack(const struct lu_env *env, struct object_update *update,
402                   size_t *max_update_size, const struct lu_fid *fid,
403                   size_t size, loff_t pos)
404 {
405         __u16           sizes[2] = {sizeof(size), sizeof(pos)};
406         const void      *bufs[2] = {&size, &pos};
407
408         LASSERT(size > 0);
409         size = cpu_to_le64(size);
410         pos = cpu_to_le64(pos);
411
412         return out_update_pack(env, update, max_update_size, OUT_READ, fid,
413                                ARRAY_SIZE(sizes), sizes, bufs, size);
414 }
415 EXPORT_SYMBOL(out_read_pack);
416
417 static int tx_extend_args(struct thandle_exec_args *ta, int new_alloc_ta)
418 {
419         struct tx_arg   **new_ta;
420         int             i;
421         int             rc = 0;
422
423         if (ta->ta_alloc_args >= new_alloc_ta)
424                 return 0;
425
426         OBD_ALLOC(new_ta, sizeof(*new_ta) * new_alloc_ta);
427         if (new_ta == NULL)
428                 return -ENOMEM;
429
430         for (i = 0; i < new_alloc_ta; i++) {
431                 if (i < ta->ta_alloc_args) {
432                         /* copy the old args to new one */
433                         new_ta[i] = ta->ta_args[i];
434                 } else {
435                         OBD_ALLOC_PTR(new_ta[i]);
436                         if (new_ta[i] == NULL)
437                                 GOTO(out, rc = -ENOMEM);
438                 }
439         }
440
441         /* free the old args */
442         if (ta->ta_args != NULL)
443                 OBD_FREE(ta->ta_args, sizeof(ta->ta_args[0]) *
444                                       ta->ta_alloc_args);
445
446         ta->ta_args = new_ta;
447         ta->ta_alloc_args = new_alloc_ta;
448 out:
449         if (rc != 0) {
450                 for (i = 0; i < new_alloc_ta; i++) {
451                         if (new_ta[i] != NULL)
452                                 OBD_FREE_PTR(new_ta[i]);
453                 }
454                 OBD_FREE(new_ta, sizeof(*new_ta) * new_alloc_ta);
455         }
456         return rc;
457 }
458
459 #define TX_ALLOC_STEP   8
460 struct tx_arg *tx_add_exec(struct thandle_exec_args *ta,
461                            tx_exec_func_t func, tx_exec_func_t undo,
462                            const char *file, int line)
463 {
464         int rc;
465         int i;
466
467         LASSERT(ta != NULL);
468         LASSERT(func != NULL);
469
470         if (ta->ta_argno + 1 >= ta->ta_alloc_args) {
471                 rc = tx_extend_args(ta, ta->ta_alloc_args + TX_ALLOC_STEP);
472                 if (rc != 0)
473                         return ERR_PTR(rc);
474         }
475
476         i = ta->ta_argno;
477
478         ta->ta_argno++;
479
480         ta->ta_args[i]->exec_fn = func;
481         ta->ta_args[i]->undo_fn = undo;
482         ta->ta_args[i]->file    = file;
483         ta->ta_args[i]->line    = line;
484
485         return ta->ta_args[i];
486 }
487
488 static int out_obj_destroy(const struct lu_env *env, struct dt_object *dt_obj,
489                            struct thandle *th)
490 {
491         int rc;
492
493         CDEBUG(D_INFO, "%s: destroy "DFID"\n", dt_obd_name(th->th_dev),
494                PFID(lu_object_fid(&dt_obj->do_lu)));
495
496         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
497         rc = dt_destroy(env, dt_obj, th);
498         dt_write_unlock(env, dt_obj);
499
500         return rc;
501 }
502
503 /**
504  * All of the xxx_undo will be used once execution failed,
505  * But because all of the required resource has been reserved in
506  * declare phase, i.e. if declare succeed, it should make sure
507  * the following executing phase succeed in anyway, so these undo
508  * should be useless for most of the time in Phase I
509  */
510 static int out_tx_create_undo(const struct lu_env *env, struct thandle *th,
511                               struct tx_arg *arg)
512 {
513         int rc;
514
515         rc = out_obj_destroy(env, arg->object, th);
516         if (rc != 0)
517                 CERROR("%s: undo failure, we are doomed!: rc = %d\n",
518                        dt_obd_name(th->th_dev), rc);
519         return rc;
520 }
521
522 int out_tx_create_exec(const struct lu_env *env, struct thandle *th,
523                        struct tx_arg *arg)
524 {
525         struct dt_object        *dt_obj = arg->object;
526         int                      rc;
527
528         CDEBUG(D_OTHER, "%s: create "DFID": dof %u, mode %o\n",
529                dt_obd_name(th->th_dev),
530                PFID(lu_object_fid(&arg->object->do_lu)),
531                arg->u.create.dof.dof_type,
532                arg->u.create.attr.la_mode & S_IFMT);
533
534         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
535         rc = dt_create(env, dt_obj, &arg->u.create.attr,
536                        &arg->u.create.hint, &arg->u.create.dof, th);
537
538         dt_write_unlock(env, dt_obj);
539
540         CDEBUG(D_INFO, "%s: insert create reply %p index %d: rc = %d\n",
541                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
542
543         if (arg->reply != NULL)
544                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
545                                             rc);
546
547         return rc;
548 }
549
550 /**
551  * Add create update to thandle
552  *
553  * Declare create updates and add the update to the thandle updates
554  * exec array.
555  *
556  * \param [in] env      execution environment
557  * \param [in] obj      object to be created
558  * \param [in] attr     attributes of the creation
559  * \param [in] parent_fid the fid of the parent
560  * \param [in] dof      dt object format of the creation
561  * \param [in] ta       thandle execuation args where all of updates
562  *                      of the transaction are stored
563  * \param [in] th       thandle for this update
564  * \param [in] reply    reply of the updates
565  * \param [in] index    index of the reply
566  * \param [in] file     the file name where the function is called,
567  *                      which is only for debugging purpose.
568  * \param [in] line     the line number where the funtion is called,
569  *                      which is only for debugging purpose.
570  *
571  * \retval              0 if updates is added successfully.
572  * \retval              negative errno if update adding fails.
573  */
574 int out_create_add_exec(const struct lu_env *env, struct dt_object *obj,
575                         struct lu_attr *attr, struct lu_fid *parent_fid,
576                         struct dt_object_format *dof,
577                         struct thandle_exec_args *ta,
578                         struct thandle  *th,
579                         struct object_update_reply *reply,
580                         int index, const char *file, int line)
581 {
582         struct tx_arg *arg;
583         int rc;
584
585         rc = dt_declare_create(env, obj, attr, NULL, dof, th);
586         if (rc != 0)
587                 return rc;
588
589         arg = tx_add_exec(ta, out_tx_create_exec, out_tx_create_undo, file,
590                           line);
591         if (IS_ERR(arg))
592                 return PTR_ERR(arg);
593
594         /* release the object in out_trans_stop */
595         lu_object_get(&obj->do_lu);
596         arg->object = obj;
597         arg->u.create.attr = *attr;
598         if (parent_fid != NULL)
599                 arg->u.create.fid = *parent_fid;
600         memset(&arg->u.create.hint, 0, sizeof(arg->u.create.hint));
601         arg->u.create.dof  = *dof;
602         arg->reply = reply;
603         arg->index = index;
604
605         return 0;
606 }
607
608 static int out_tx_attr_set_undo(const struct lu_env *env,
609                                 struct thandle *th, struct tx_arg *arg)
610 {
611         CERROR("%s: attr set undo "DFID" unimplemented yet!: rc = %d\n",
612                dt_obd_name(th->th_dev),
613                PFID(lu_object_fid(&arg->object->do_lu)), -ENOTSUPP);
614
615         return -ENOTSUPP;
616 }
617
618 static int out_tx_attr_set_exec(const struct lu_env *env, struct thandle *th,
619                                 struct tx_arg *arg)
620 {
621         struct dt_object        *dt_obj = arg->object;
622         int                     rc;
623
624         CDEBUG(D_OTHER, "%s: attr set "DFID"\n", dt_obd_name(th->th_dev),
625                PFID(lu_object_fid(&dt_obj->do_lu)));
626
627         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
628         rc = dt_attr_set(env, dt_obj, &arg->u.attr_set.attr, th);
629         dt_write_unlock(env, dt_obj);
630
631         CDEBUG(D_INFO, "%s: insert attr_set reply %p index %d: rc = %d\n",
632                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
633
634         if (arg->reply != NULL)
635                 object_update_result_insert(arg->reply, NULL, 0,
636                                             arg->index, rc);
637
638         return rc;
639 }
640
641 int out_attr_set_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
642                           const struct lu_attr *attr,
643                           struct thandle_exec_args *ta,
644                           struct thandle *th, struct object_update_reply *reply,
645                           int index, const char *file, int line)
646 {
647         struct tx_arg   *arg;
648         int             rc;
649
650         rc = dt_declare_attr_set(env, dt_obj, attr, th);
651         if (rc != 0)
652                 return rc;
653
654         arg = tx_add_exec(ta, out_tx_attr_set_exec, out_tx_attr_set_undo,
655                           file, line);
656         if (IS_ERR(arg))
657                 return PTR_ERR(arg);
658
659         lu_object_get(&dt_obj->do_lu);
660         arg->object = dt_obj;
661         arg->u.attr_set.attr = *attr;
662         arg->reply = reply;
663         arg->index = index;
664         return 0;
665 }
666
667 static int out_tx_write_exec(const struct lu_env *env, struct thandle *th,
668                              struct tx_arg *arg)
669 {
670         struct dt_object *dt_obj = arg->object;
671         int rc;
672
673         CDEBUG(D_INFO, "write "DFID" pos "LPU64" buf %p, len %lu\n",
674                PFID(lu_object_fid(&dt_obj->do_lu)), arg->u.write.pos,
675                arg->u.write.buf.lb_buf, (unsigned long)arg->u.write.buf.lb_len);
676
677         if (OBD_FAIL_CHECK(OBD_FAIL_OUT_ENOSPC)) {
678                 rc = -ENOSPC;
679         } else {
680                 dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
681                 rc = dt_record_write(env, dt_obj, &arg->u.write.buf,
682                                      &arg->u.write.pos, th);
683                 dt_write_unlock(env, dt_obj);
684
685                 if (rc == 0)
686                         rc = arg->u.write.buf.lb_len;
687         }
688
689         if (arg->reply != NULL)
690                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
691                                             rc);
692
693         return rc > 0 ? 0 : rc;
694 }
695
696 int out_write_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
697                        const struct lu_buf *buf, loff_t pos,
698                        struct thandle_exec_args *ta, struct thandle *th,
699                        struct object_update_reply *reply, int index,
700                        const char *file, int line)
701 {
702         struct tx_arg   *arg;
703         int             rc;
704
705         rc = dt_declare_record_write(env, dt_obj, buf, pos, th);
706         if (rc != 0)
707                 return rc;
708
709         arg = tx_add_exec(ta, out_tx_write_exec, NULL, file, line);
710         if (IS_ERR(arg))
711                 return PTR_ERR(arg);
712
713         lu_object_get(&dt_obj->do_lu);
714         arg->object = dt_obj;
715         arg->u.write.buf = *buf;
716         arg->u.write.pos = pos;
717         arg->reply = reply;
718         arg->index = index;
719         return 0;
720 }
721
722 static int out_tx_xattr_set_exec(const struct lu_env *env,
723                                  struct thandle *th,
724                                  struct tx_arg *arg)
725 {
726         struct dt_object *dt_obj = arg->object;
727         int rc;
728
729         CDEBUG(D_INFO, "%s: set xattr buf %p name %s flag %d\n",
730                dt_obd_name(th->th_dev), arg->u.xattr_set.buf.lb_buf,
731                arg->u.xattr_set.name, arg->u.xattr_set.flags);
732
733         if (!lu_object_exists(&dt_obj->do_lu))
734                 GOTO(out, rc = -ENOENT);
735
736         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
737         rc = dt_xattr_set(env, dt_obj, &arg->u.xattr_set.buf,
738                           arg->u.xattr_set.name, arg->u.xattr_set.flags,
739                           th);
740         /**
741          * Ignore errors if this is LINK EA
742          **/
743         if (unlikely(rc != 0 &&
744                      strcmp(arg->u.xattr_set.name, XATTR_NAME_LINK) == 0)) {
745                 /* XXX: If the linkEA is overflow, then we need to notify the
746                  *      namespace LFSCK to skip "nlink" attribute verification
747                  *      on this object to avoid the "nlink" to be shrinked by
748                  *      wrong. It may be not good an interaction with LFSCK
749                  *      like this. We will consider to replace it with other
750                  *      mechanism in future. LU-5802. */
751                 if (rc == -ENOSPC && arg->reply != NULL) {
752                         struct lfsck_request *lr = &tgt_th_info(env)->tti_lr;
753
754                         lfsck_pack_rfa(lr, lu_object_fid(&dt_obj->do_lu),
755                                        LE_SKIP_NLINK, LFSCK_TYPE_NAMESPACE);
756                         tgt_lfsck_in_notify(env,
757                                 tgt_ses_info(env)->tsi_tgt->lut_bottom, lr, th);
758                 }
759
760                 rc = 0;
761         }
762         dt_write_unlock(env, dt_obj);
763
764 out:
765         CDEBUG(D_INFO, "%s: insert xattr set reply %p index %d: rc = %d\n",
766                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
767
768         if (arg->reply != NULL)
769                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
770                                             rc);
771
772         return rc;
773 }
774
775 int out_xattr_set_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
776                            const struct lu_buf *buf, const char *name,
777                            int flags, struct thandle_exec_args *ta,
778                            struct thandle *th,
779                            struct object_update_reply *reply,
780                            int index, const char *file, int line)
781 {
782         struct tx_arg   *arg;
783         int             rc;
784
785         rc = dt_declare_xattr_set(env, dt_obj, buf, name, flags, th);
786         if (rc != 0)
787                 return rc;
788
789         if (strcmp(name, XATTR_NAME_LINK) == 0 && reply != NULL) {
790                 struct lfsck_request *lr = &tgt_th_info(env)->tti_lr;
791
792                 /* XXX: If the linkEA is overflow, then we need to notify the
793                  *      namespace LFSCK to skip "nlink" attribute verification
794                  *      on this object to avoid the "nlink" to be shrinked by
795                  *      wrong. It may be not good an interaction with LFSCK
796                  *      like this. We will consider to replace it with other
797                  *      mechanism in future. LU-5802. */
798                 lfsck_pack_rfa(lr, lu_object_fid(&dt_obj->do_lu),
799                                LE_SKIP_NLINK_DECLARE, LFSCK_TYPE_NAMESPACE);
800                 rc = tgt_lfsck_in_notify(env,
801                                          tgt_ses_info(env)->tsi_tgt->lut_bottom,
802                                          lr, ta->ta_handle);
803                 if (rc != 0)
804                         return rc;
805         }
806
807         arg = tx_add_exec(ta, out_tx_xattr_set_exec, NULL, file, line);
808         if (IS_ERR(arg))
809                 return PTR_ERR(arg);
810
811         lu_object_get(&dt_obj->do_lu);
812         arg->object = dt_obj;
813         arg->u.xattr_set.name = name;
814         arg->u.xattr_set.flags = flags;
815         arg->u.xattr_set.buf = *buf;
816         arg->reply = reply;
817         arg->index = index;
818         arg->u.xattr_set.csum = 0;
819         return 0;
820 }
821
822 static int out_tx_xattr_del_exec(const struct lu_env *env, struct thandle *th,
823                                  struct tx_arg *arg)
824 {
825         struct dt_object *dt_obj = arg->object;
826         int rc;
827
828         CDEBUG(D_INFO, "%s: del xattr name '%s' on "DFID"\n",
829                dt_obd_name(th->th_dev), arg->u.xattr_set.name,
830                PFID(lu_object_fid(&dt_obj->do_lu)));
831
832         if (!lu_object_exists(&dt_obj->do_lu))
833                 GOTO(out, rc = -ENOENT);
834
835         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
836         rc = dt_xattr_del(env, dt_obj, arg->u.xattr_set.name,
837                           th);
838         dt_write_unlock(env, dt_obj);
839 out:
840         CDEBUG(D_INFO, "%s: insert xattr del reply %p index %d: rc = %d\n",
841                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
842
843         if (arg->reply != NULL)
844                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
845                                             rc);
846
847         return rc;
848 }
849
850 int out_xattr_del_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
851                            const char *name, struct thandle_exec_args *ta,
852                            struct thandle *th,
853                            struct object_update_reply *reply, int index,
854                            const char *file, int line)
855 {
856         struct tx_arg   *arg;
857         int             rc;
858
859         rc = dt_declare_xattr_del(env, dt_obj, name, th);
860         if (rc != 0)
861                 return rc;
862
863         arg = tx_add_exec(ta, out_tx_xattr_del_exec, NULL, file, line);
864         if (IS_ERR(arg))
865                 return PTR_ERR(arg);
866
867         lu_object_get(&dt_obj->do_lu);
868         arg->object = dt_obj;
869         arg->u.xattr_set.name = name;
870         arg->reply = reply;
871         arg->index = index;
872         return 0;
873 }
874
875 static int out_obj_ref_add(const struct lu_env *env,
876                            struct dt_object *dt_obj,
877                            struct thandle *th)
878 {
879         int rc;
880
881         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
882         rc = dt_ref_add(env, dt_obj, th);
883         dt_write_unlock(env, dt_obj);
884
885         return rc;
886 }
887
888 static int out_obj_ref_del(const struct lu_env *env,
889                            struct dt_object *dt_obj,
890                            struct thandle *th)
891 {
892         int rc;
893
894         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
895         rc = dt_ref_del(env, dt_obj, th);
896         dt_write_unlock(env, dt_obj);
897
898         return rc;
899 }
900
901 static int out_tx_ref_add_exec(const struct lu_env *env, struct thandle *th,
902                                struct tx_arg *arg)
903 {
904         struct dt_object *dt_obj = arg->object;
905         int rc;
906
907         rc = out_obj_ref_add(env, dt_obj, th);
908
909         CDEBUG(D_INFO, "%s: insert ref_add reply %p index %d: rc = %d\n",
910                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
911
912         if (arg->reply != NULL)
913                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
914                                             rc);
915         return rc;
916 }
917
918 static int out_tx_ref_add_undo(const struct lu_env *env, struct thandle *th,
919                                struct tx_arg *arg)
920 {
921         return out_obj_ref_del(env, arg->object, th);
922 }
923
924 int out_ref_add_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
925                          struct thandle_exec_args *ta,
926                          struct thandle *th,
927                          struct object_update_reply *reply, int index,
928                          const char *file, int line)
929 {
930         struct tx_arg   *arg;
931         int             rc;
932
933         rc = dt_declare_ref_add(env, dt_obj, th);
934         if (rc != 0)
935                 return rc;
936
937         arg = tx_add_exec(ta, out_tx_ref_add_exec, out_tx_ref_add_undo, file,
938                           line);
939         if (IS_ERR(arg))
940                 return PTR_ERR(arg);
941
942         lu_object_get(&dt_obj->do_lu);
943         arg->object = dt_obj;
944         arg->reply = reply;
945         arg->index = index;
946         return 0;
947 }
948
949 static int out_tx_ref_del_exec(const struct lu_env *env, struct thandle *th,
950                                struct tx_arg *arg)
951 {
952         struct dt_object        *dt_obj = arg->object;
953         int                      rc;
954
955         rc = out_obj_ref_del(env, dt_obj, th);
956
957         CDEBUG(D_INFO, "%s: insert ref_del reply %p index %d: rc = %d\n",
958                dt_obd_name(th->th_dev), arg->reply, arg->index, 0);
959
960         if (arg->reply != NULL)
961                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
962                                             rc);
963
964         return rc;
965 }
966
967 static int out_tx_ref_del_undo(const struct lu_env *env, struct thandle *th,
968                                struct tx_arg *arg)
969 {
970         return out_obj_ref_add(env, arg->object, th);
971 }
972
973 int out_ref_del_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
974                          struct thandle_exec_args *ta,
975                          struct thandle *th,
976                          struct object_update_reply *reply, int index,
977                          const char *file, int line)
978 {
979         struct tx_arg   *arg;
980         int             rc;
981
982         rc = dt_declare_ref_del(env, dt_obj, th);
983         if (rc != 0)
984                 return rc;
985
986         arg = tx_add_exec(ta, out_tx_ref_del_exec, out_tx_ref_del_undo, file,
987                           line);
988         if (IS_ERR(arg))
989                 return PTR_ERR(arg);
990
991         lu_object_get(&dt_obj->do_lu);
992         arg->object = dt_obj;
993         arg->reply = reply;
994         arg->index = index;
995         return 0;
996 }
997
998 static int out_obj_index_insert(const struct lu_env *env,
999                                 struct dt_object *dt_obj,
1000                                 const struct dt_rec *rec,
1001                                 const struct dt_key *key,
1002                                 struct thandle *th)
1003 {
1004         int rc;
1005
1006         CDEBUG(D_INFO, "%s: index insert "DFID" name: %s fid "DFID", type %u\n",
1007                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
1008                (char *)key, PFID(((struct dt_insert_rec *)rec)->rec_fid),
1009                ((struct dt_insert_rec *)rec)->rec_type);
1010
1011         if (dt_try_as_dir(env, dt_obj) == 0)
1012                 return -ENOTDIR;
1013
1014         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
1015         rc = dt_insert(env, dt_obj, rec, key, th, 0);
1016         dt_write_unlock(env, dt_obj);
1017
1018         return rc;
1019 }
1020
1021 static int out_obj_index_delete(const struct lu_env *env,
1022                                 struct dt_object *dt_obj,
1023                                 const struct dt_key *key,
1024                                 struct thandle *th)
1025 {
1026         int rc;
1027
1028         CDEBUG(D_INFO, "%s: index delete "DFID" name: %s\n",
1029                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
1030                (char *)key);
1031
1032         if (dt_try_as_dir(env, dt_obj) == 0)
1033                 return -ENOTDIR;
1034
1035         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
1036         rc = dt_delete(env, dt_obj, key, th);
1037         dt_write_unlock(env, dt_obj);
1038
1039         return rc;
1040 }
1041
1042 static int out_tx_index_insert_exec(const struct lu_env *env,
1043                                     struct thandle *th, struct tx_arg *arg)
1044 {
1045         struct dt_object *dt_obj = arg->object;
1046         int rc;
1047
1048         if (unlikely(!dt_object_exists(dt_obj)))
1049                 RETURN(-ESTALE);
1050
1051         rc = out_obj_index_insert(env, dt_obj,
1052                                   (const struct dt_rec *)&arg->u.insert.rec,
1053                                   arg->u.insert.key, th);
1054
1055         CDEBUG(D_INFO, "%s: insert idx insert reply %p index %d: rc = %d\n",
1056                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1057
1058         if (arg->reply != NULL)
1059                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1060                                             rc);
1061         return rc;
1062 }
1063
1064 static int out_tx_index_insert_undo(const struct lu_env *env,
1065                                     struct thandle *th, struct tx_arg *arg)
1066 {
1067         return out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
1068 }
1069
1070 int out_index_insert_add_exec(const struct lu_env *env,
1071                               struct dt_object *dt_obj,
1072                               const struct dt_rec *rec,
1073                               const struct dt_key *key,
1074                               struct thandle_exec_args *ta,
1075                               struct thandle *th,
1076                               struct object_update_reply *reply,
1077                               int index, const char *file, int line)
1078 {
1079         struct tx_arg   *arg;
1080         int             rc;
1081
1082         if (dt_try_as_dir(env, dt_obj) == 0) {
1083                 rc = -ENOTDIR;
1084                 return rc;
1085         }
1086
1087         rc = dt_declare_insert(env, dt_obj, rec, key, th);
1088         if (rc != 0)
1089                 return rc;
1090
1091         arg = tx_add_exec(ta, out_tx_index_insert_exec,
1092                           out_tx_index_insert_undo, file, line);
1093         if (IS_ERR(arg))
1094                 return PTR_ERR(arg);
1095
1096         lu_object_get(&dt_obj->do_lu);
1097         arg->object = dt_obj;
1098         arg->reply = reply;
1099         arg->index = index;
1100         arg->u.insert.rec = *(const struct dt_insert_rec *)rec;
1101         arg->u.insert.key = key;
1102
1103         return 0;
1104 }
1105
1106 static int out_tx_index_delete_exec(const struct lu_env *env,
1107                                     struct thandle *th,
1108                                     struct tx_arg *arg)
1109 {
1110         int rc;
1111
1112         rc = out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
1113
1114         CDEBUG(D_INFO, "%s: delete idx insert reply %p index %d: rc = %d\n",
1115                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1116
1117         if (arg->reply != NULL)
1118                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1119                                             rc);
1120
1121         return rc;
1122 }
1123
1124 static int out_tx_index_delete_undo(const struct lu_env *env,
1125                                     struct thandle *th,
1126                                     struct tx_arg *arg)
1127 {
1128         CERROR("%s: Oops, can not rollback index_delete yet: rc = %d\n",
1129                dt_obd_name(th->th_dev), -ENOTSUPP);
1130         return -ENOTSUPP;
1131 }
1132
1133 int out_index_delete_add_exec(const struct lu_env *env,
1134                               struct dt_object *dt_obj,
1135                               const struct dt_key *key,
1136                               struct thandle_exec_args *ta,
1137                               struct thandle *th,
1138                               struct object_update_reply *reply,
1139                               int index, const char *file, int line)
1140 {
1141         struct tx_arg   *arg;
1142         int             rc;
1143
1144         if (dt_try_as_dir(env, dt_obj) == 0) {
1145                 rc = -ENOTDIR;
1146                 return rc;
1147         }
1148
1149         LASSERT(ta->ta_handle != NULL);
1150         rc = dt_declare_delete(env, dt_obj, key, th);
1151         if (rc != 0)
1152                 return rc;
1153
1154         arg = tx_add_exec(ta, out_tx_index_delete_exec,
1155                           out_tx_index_delete_undo, file, line);
1156         if (IS_ERR(arg))
1157                 return PTR_ERR(arg);
1158
1159         lu_object_get(&dt_obj->do_lu);
1160         arg->object = dt_obj;
1161         arg->reply = reply;
1162         arg->index = index;
1163         arg->u.insert.key = key;
1164         return 0;
1165 }
1166
1167 static int out_tx_destroy_exec(const struct lu_env *env, struct thandle *th,
1168                                struct tx_arg *arg)
1169 {
1170         struct dt_object *dt_obj = arg->object;
1171         int rc;
1172
1173         rc = out_obj_destroy(env, dt_obj, th);
1174
1175         CDEBUG(D_INFO, "%s: insert destroy reply %p index %d: rc = %d\n",
1176                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1177
1178         if (arg->reply != NULL)
1179                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1180                                             rc);
1181
1182         RETURN(rc);
1183 }
1184
1185 static int out_tx_destroy_undo(const struct lu_env *env, struct thandle *th,
1186                                struct tx_arg *arg)
1187 {
1188         CERROR("%s: not support destroy undo yet!: rc = %d\n",
1189                dt_obd_name(th->th_dev), -ENOTSUPP);
1190         return -ENOTSUPP;
1191 }
1192
1193 int out_destroy_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
1194                          struct thandle_exec_args *ta, struct thandle *th,
1195                          struct object_update_reply *reply,
1196                          int index, const char *file, int line)
1197 {
1198         struct tx_arg   *arg;
1199         int             rc;
1200
1201         rc = dt_declare_destroy(env, dt_obj, th);
1202         if (rc != 0)
1203                 return rc;
1204
1205         arg = tx_add_exec(ta, out_tx_destroy_exec, out_tx_destroy_undo,
1206                           file, line);
1207         if (IS_ERR(arg))
1208                 return PTR_ERR(arg);
1209
1210         lu_object_get(&dt_obj->do_lu);
1211         arg->object = dt_obj;
1212         arg->reply = reply;
1213         arg->index = index;
1214         return 0;
1215 }