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