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