Whamcloud - gitweb
LU-8589 osd: remove "object" from method function names
[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 <lustre_obdo.h>
36 #include <lustre_update.h>
37 #include <md_object.h>
38 #include <obd.h>
39 #include <obd_class.h>
40 #include <lustre_linkea.h>
41
42 #include "tgt_internal.h"
43
44 const char *update_op_str(__u16 opc)
45 {
46         static const char *opc_str[] = {
47                 [OUT_START] = "start",
48                 [OUT_CREATE] = "create",
49                 [OUT_DESTROY] = "destroy",
50                 [OUT_REF_ADD] = "ref_add",
51                 [OUT_REF_DEL] = "ref_del" ,
52                 [OUT_ATTR_SET] = "attr_set",
53                 [OUT_ATTR_GET] = "attr_get",
54                 [OUT_XATTR_SET] = "xattr_set",
55                 [OUT_XATTR_GET] = "xattr_get",
56                 [OUT_INDEX_LOOKUP] = "lookup",
57                 [OUT_INDEX_INSERT] = "insert",
58                 [OUT_INDEX_DELETE] = "delete",
59                 [OUT_WRITE] = "write",
60                 [OUT_XATTR_DEL] = "xattr_del",
61                 [OUT_PUNCH] = "punch",
62                 [OUT_READ] = "read",
63                 [OUT_NOOP] = "noop",
64         };
65
66         if (opc < ARRAY_SIZE(opc_str) && opc_str[opc] != NULL)
67                 return opc_str[opc];
68         else
69                 return "unknown";
70 }
71 EXPORT_SYMBOL(update_op_str);
72
73 /**
74  * Fill object update header
75  *
76  * Only fill the object update header, and parameters will be filled later
77  * in other functions.
78  *
79  * \params[in] env              execution environment
80  * \params[in] update           object update to be filled
81  * \params[in,out] max_update_size      maximum object update size, if the
82  *                                      current update length equals or
83  *                                      exceeds the size, it will return -E2BIG.
84  * \params[in] update_op        update type
85  * \params[in] fid              object FID of the update
86  * \params[in] param_count      the count of the update parameters
87  * \params[in] param_sizes      the length of each parameters
88  *
89  * \retval                      0 if packing succeeds.
90  * \retval                      -E2BIG if packing exceeds the maximum length.
91  */
92 int out_update_header_pack(const struct lu_env *env,
93                            struct object_update *update,
94                            size_t *max_update_size,
95                            enum update_type update_op,
96                            const struct lu_fid *fid,
97                            unsigned int param_count,
98                            __u16 *param_sizes,
99                            __u32 reply_size)
100 {
101         struct object_update_param      *param;
102         unsigned int                    i;
103         size_t                          update_size;
104
105         if (((reply_size + 7) >> 3) >= 1ULL << 16)
106                 return -EINVAL;
107
108         /* Check whether the packing exceeding the maxima update length */
109         update_size = sizeof(*update);
110         for (i = 0; i < param_count; i++)
111                 update_size += cfs_size_round(sizeof(*param) + param_sizes[i]);
112
113         if (unlikely(update_size >= *max_update_size)) {
114                 *max_update_size = update_size;
115                 return -E2BIG;
116         }
117
118         update->ou_fid = *fid;
119         update->ou_type = update_op;
120         update->ou_params_count = param_count;
121         update->ou_result_size = reply_size;
122         param = &update->ou_params[0];
123         for (i = 0; i < param_count; i++) {
124                 param->oup_len = param_sizes[i];
125                 param = (struct object_update_param *)((char *)param +
126                          object_update_param_size(param));
127         }
128
129         return 0;
130 }
131
132 /**
133  * Packs one update into the update_buffer.
134  *
135  * \param[in] env       execution environment
136  * \param[in] update    update to be packed
137  * \param[in] max_update_size   *maximum size of \a update
138  * \param[in] op        update operation (enum update_type)
139  * \param[in] fid       object FID for this update
140  * \param[in] param_count       number of parameters for this update
141  * \param[in] param_sizes       array of parameters length of this update
142  * \param[in] param_bufs        parameter buffers
143  *
144  * \retval              = 0 if updates packing succeeds
145  * \retval              negative errno if updates packing fails
146  **/
147 int out_update_pack(const struct lu_env *env, struct object_update *update,
148                     size_t *max_update_size, enum update_type op,
149                     const struct lu_fid *fid, unsigned int param_count,
150                     __u16 *param_sizes, const void **param_bufs,
151                     __u32 reply_size)
152 {
153         struct object_update_param      *param;
154         unsigned int                    i;
155         int                             rc;
156         ENTRY;
157
158         rc = out_update_header_pack(env, update, max_update_size, op, fid,
159                                     param_count, param_sizes, reply_size);
160         if (rc != 0)
161                 RETURN(rc);
162
163         param = &update->ou_params[0];
164         for (i = 0; i < param_count; i++) {
165                 memcpy(&param->oup_buf[0], param_bufs[i], param_sizes[i]);
166                 param = (struct object_update_param *)((char *)param +
167                          object_update_param_size(param));
168         }
169
170         RETURN(0);
171 }
172 EXPORT_SYMBOL(out_update_pack);
173
174 /**
175  * Pack various updates into the update_buffer.
176  *
177  * The following functions pack different updates into the update_buffer
178  * So parameters of these API is basically same as its correspondent OSD/OSP
179  * API, for detail description of these parameters see osd_handler.c or
180  * osp_md_object.c.
181  *
182  * \param[in] env       execution environment
183  * \param[in] ubuf      update buffer
184  * \param[in] fid       fid of this object for the update
185  *
186  * \retval              0 if insertion succeeds.
187  * \retval              negative errno if insertion fails.
188  */
189 int out_create_pack(const struct lu_env *env, struct object_update *update,
190                     size_t *max_update_size, const struct lu_fid *fid,
191                     const struct lu_attr *attr, struct dt_allocation_hint *hint,
192                     struct dt_object_format *dof)
193 {
194         struct obdo             *obdo;
195         __u16                   sizes[2] = {sizeof(*obdo), 0};
196         int                     buf_count = 1;
197         const struct lu_fid     *parent_fid = NULL;
198         int                     rc;
199         ENTRY;
200
201         if (hint != NULL && hint->dah_parent) {
202                 parent_fid = lu_object_fid(&hint->dah_parent->do_lu);
203                 sizes[1] = sizeof(*parent_fid);
204                 buf_count++;
205         }
206
207         rc = out_update_header_pack(env, update, max_update_size, OUT_CREATE,
208                                     fid, buf_count, sizes, 0);
209         if (rc != 0)
210                 RETURN(rc);
211
212         obdo = object_update_param_get(update, 0, NULL);
213         if (IS_ERR(obdo))
214                 RETURN(PTR_ERR(obdo));
215
216         obdo->o_valid = 0;
217         obdo_from_la(obdo, attr, attr->la_valid);
218
219         if (parent_fid != NULL) {
220                 struct lu_fid *tmp;
221
222                 tmp = object_update_param_get(update, 1, NULL);
223                 if (IS_ERR(tmp))
224                         RETURN(PTR_ERR(tmp));
225
226                 fid_cpu_to_le(tmp, parent_fid);
227         }
228
229         RETURN(0);
230 }
231 EXPORT_SYMBOL(out_create_pack);
232
233 int out_ref_del_pack(const struct lu_env *env, struct object_update *update,
234                      size_t *max_update_size, const struct lu_fid *fid)
235 {
236         return out_update_pack(env, update, max_update_size, OUT_REF_DEL, fid,
237                                0, NULL, NULL, 0);
238 }
239 EXPORT_SYMBOL(out_ref_del_pack);
240
241 int out_ref_add_pack(const struct lu_env *env, struct object_update *update,
242                      size_t *max_update_size, const struct lu_fid *fid)
243 {
244         return out_update_pack(env, update, max_update_size, OUT_REF_ADD, fid,
245                                0, NULL, NULL, 0);
246 }
247 EXPORT_SYMBOL(out_ref_add_pack);
248
249 int out_attr_set_pack(const struct lu_env *env, struct object_update *update,
250                       size_t *max_update_size, const struct lu_fid *fid,
251                       const struct lu_attr *attr)
252 {
253         struct obdo             *obdo;
254         __u16                   size = sizeof(*obdo);
255         int                     rc;
256         ENTRY;
257
258         rc = out_update_header_pack(env, update, max_update_size,
259                                     OUT_ATTR_SET, fid, 1, &size, 0);
260         if (rc != 0)
261                 RETURN(rc);
262
263         obdo = object_update_param_get(update, 0, NULL);
264         if (IS_ERR(obdo))
265                 RETURN(PTR_ERR(obdo));
266
267         obdo->o_valid = 0;
268         obdo_from_la(obdo, attr, attr->la_valid);
269
270         RETURN(0);
271 }
272 EXPORT_SYMBOL(out_attr_set_pack);
273
274 int out_xattr_set_pack(const struct lu_env *env, struct object_update *update,
275                        size_t *max_update_size, const struct lu_fid *fid,
276                        const struct lu_buf *buf, const char *name, __u32 flag)
277 {
278         __u16   sizes[3] = {strlen(name) + 1, buf->lb_len, sizeof(flag)};
279         const void *bufs[3] = {(char *)name, (char *)buf->lb_buf,
280                                (char *)&flag};
281
282         return out_update_pack(env, update, max_update_size, OUT_XATTR_SET,
283                                fid, ARRAY_SIZE(sizes), sizes, bufs, 0);
284 }
285 EXPORT_SYMBOL(out_xattr_set_pack);
286
287 int out_xattr_del_pack(const struct lu_env *env, struct object_update *update,
288                        size_t *max_update_size, const struct lu_fid *fid,
289                        const char *name)
290 {
291         __u16   size = strlen(name) + 1;
292
293         return out_update_pack(env, update, max_update_size, OUT_XATTR_DEL,
294                                fid, 1, &size, (const void **)&name, 0);
295 }
296 EXPORT_SYMBOL(out_xattr_del_pack);
297
298 int out_index_insert_pack(const struct lu_env *env,
299                           struct object_update *update,
300                           size_t *max_update_size, const struct lu_fid *fid,
301                           const struct dt_rec *rec, const struct dt_key *key)
302 {
303         struct dt_insert_rec       *rec1 = (struct dt_insert_rec *)rec;
304         struct lu_fid              rec_fid;
305         __u32                       type = cpu_to_le32(rec1->rec_type);
306         __u16                       sizes[3] = { strlen((char *)key) + 1,
307                                                 sizeof(rec_fid),
308                                                 sizeof(type) };
309         const void                 *bufs[3] = { (char *)key,
310                                                 (char *)&rec_fid,
311                                                 (char *)&type };
312
313         fid_cpu_to_le(&rec_fid, rec1->rec_fid);
314
315         return out_update_pack(env, update, max_update_size, OUT_INDEX_INSERT,
316                                fid, ARRAY_SIZE(sizes), sizes, bufs, 0);
317 }
318 EXPORT_SYMBOL(out_index_insert_pack);
319
320 int out_index_delete_pack(const struct lu_env *env,
321                           struct object_update *update,
322                           size_t *max_update_size, const struct lu_fid *fid,
323                           const struct dt_key *key)
324 {
325         __u16   size = strlen((char *)key) + 1;
326         const void *buf = key;
327
328         return out_update_pack(env, update, max_update_size, OUT_INDEX_DELETE,
329                                fid, 1, &size, &buf, 0);
330 }
331 EXPORT_SYMBOL(out_index_delete_pack);
332
333 int out_destroy_pack(const struct lu_env *env, 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_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 %llu 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         ENTRY;
735
736         CDEBUG(D_INFO, "%s: set xattr buf %p name %s flag %d\n",
737                dt_obd_name(th->th_dev), arg->u.xattr_set.buf.lb_buf,
738                arg->u.xattr_set.name, arg->u.xattr_set.flags);
739
740         if (!lu_object_exists(&dt_obj->do_lu)) {
741                 rc = -ENOENT;
742         } else {
743                 struct linkea_data ldata = { 0 };
744                 bool linkea;
745
746                 ldata.ld_buf = &arg->u.xattr_set.buf;
747                 if (strcmp(arg->u.xattr_set.name, XATTR_NAME_LINK) == 0) {
748                         struct link_ea_header *leh;
749
750                         linkea = true;
751                         rc = linkea_init(&ldata);
752                         if (unlikely(rc))
753                                 GOTO(out, rc == -ENODATA ? -EINVAL : rc);
754
755                         leh = ldata.ld_leh;
756                         LASSERT(leh != NULL);
757
758                         /* If the new linkEA contains overflow timestamp,
759                          * then two cases:
760                          *
761                          * 1. The old linkEA for the object has already
762                          *    overflowed before current setting, the new
763                          *    linkEA does not contains new link entry. So
764                          *    the linkEA overflow timestamp is unchanged.
765                          *
766                          * 2. There are new link entry in the new linkEA,
767                          *    so its overflow timestamp is differnt from
768                          *    the old one. Usually, the overstamp in the
769                          *    given linkEA is newer. But because of clock
770                          *    drift among MDTs, the timestamp may become
771                          *    older. So here, we convert the timestamp to
772                          *    the server local time. Then namespace LFSCK
773                          *    that uses local time can handle it easily. */
774                         if (unlikely(leh->leh_overflow_time)) {
775                                 struct lu_buf tbuf = { 0 };
776                                 bool update = false;
777
778                                 lu_buf_alloc(&tbuf, MAX_LINKEA_SIZE);
779                                 if (tbuf.lb_buf == NULL)
780                                         GOTO(unlock, rc = -ENOMEM);
781
782                                 rc = dt_xattr_get(env, dt_obj, &tbuf,
783                                                   XATTR_NAME_LINK);
784                                 if (rc > 0) {
785                                         struct linkea_data tdata = { 0 };
786
787                                         tdata.ld_buf = &tbuf;
788                                         rc = linkea_init(&tdata);
789                                         if (rc || leh->leh_overflow_time !=
790                                             tdata.ld_leh->leh_overflow_time)
791                                                 update = true;
792                                 } else {
793                                         /* Update the timestamp by force if
794                                          * fail to load the old linkEA. */
795                                         update = true;
796                                 }
797
798                                 lu_buf_free(&tbuf);
799                                 if (update) {
800                                         leh->leh_overflow_time =
801                                                         cfs_time_current_sec();
802                                         if (unlikely(!leh->leh_overflow_time))
803                                                 leh->leh_overflow_time++;
804                                 }
805                         }
806                 } else {
807                         linkea = false;
808                 }
809
810                 dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
811
812 again:
813                 rc = dt_xattr_set(env, dt_obj, ldata.ld_buf,
814                                   arg->u.xattr_set.name, arg->u.xattr_set.flags,
815                                   th);
816                 if (unlikely(rc == -ENOSPC && linkea)) {
817                         rc = linkea_overflow_shrink(&ldata);
818                         if (likely(rc > 0)) {
819                                 arg->u.xattr_set.buf.lb_len = rc;
820                                 goto again;
821                         }
822                 }
823
824 unlock:
825                 dt_write_unlock(env, dt_obj);
826         }
827
828         GOTO(out, rc);
829
830 out:
831         CDEBUG(D_INFO, "%s: insert xattr set 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_set_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
842                            const struct lu_buf *buf, const char *name,
843                            int flags, struct thandle_exec_args *ta,
844                            struct thandle *th,
845                            struct object_update_reply *reply,
846                            int index, const char *file, int line)
847 {
848         struct tx_arg   *arg;
849         int             rc;
850
851         rc = dt_declare_xattr_set(env, dt_obj, buf, name, flags, th);
852         if (rc != 0)
853                 return rc;
854
855         arg = tx_add_exec(ta, out_tx_xattr_set_exec, NULL, file, line);
856         if (IS_ERR(arg))
857                 return PTR_ERR(arg);
858
859         lu_object_get(&dt_obj->do_lu);
860         arg->object = dt_obj;
861         arg->u.xattr_set.name = name;
862         arg->u.xattr_set.flags = flags;
863         arg->u.xattr_set.buf = *buf;
864         arg->reply = reply;
865         arg->index = index;
866         arg->u.xattr_set.csum = 0;
867         return 0;
868 }
869
870 static int out_tx_xattr_del_exec(const struct lu_env *env, struct thandle *th,
871                                  struct tx_arg *arg)
872 {
873         struct dt_object *dt_obj = arg->object;
874         int rc;
875
876         CDEBUG(D_INFO, "%s: del xattr name '%s' on "DFID"\n",
877                dt_obd_name(th->th_dev), arg->u.xattr_set.name,
878                PFID(lu_object_fid(&dt_obj->do_lu)));
879
880         if (!lu_object_exists(&dt_obj->do_lu))
881                 GOTO(out, rc = -ENOENT);
882
883         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
884         rc = dt_xattr_del(env, dt_obj, arg->u.xattr_set.name,
885                           th);
886         dt_write_unlock(env, dt_obj);
887 out:
888         CDEBUG(D_INFO, "%s: insert xattr del reply %p index %d: rc = %d\n",
889                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
890
891         if (arg->reply != NULL)
892                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
893                                             rc);
894
895         return rc;
896 }
897
898 int out_xattr_del_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
899                            const char *name, struct thandle_exec_args *ta,
900                            struct thandle *th,
901                            struct object_update_reply *reply, int index,
902                            const char *file, int line)
903 {
904         struct tx_arg   *arg;
905         int             rc;
906
907         rc = dt_declare_xattr_del(env, dt_obj, name, th);
908         if (rc != 0)
909                 return rc;
910
911         arg = tx_add_exec(ta, out_tx_xattr_del_exec, NULL, file, line);
912         if (IS_ERR(arg))
913                 return PTR_ERR(arg);
914
915         lu_object_get(&dt_obj->do_lu);
916         arg->object = dt_obj;
917         arg->u.xattr_set.name = name;
918         arg->reply = reply;
919         arg->index = index;
920         return 0;
921 }
922
923 static int out_obj_ref_add(const struct lu_env *env,
924                            struct dt_object *dt_obj,
925                            struct thandle *th)
926 {
927         int rc;
928
929         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
930         rc = dt_ref_add(env, dt_obj, th);
931         dt_write_unlock(env, dt_obj);
932
933         return rc;
934 }
935
936 static int out_obj_ref_del(const struct lu_env *env,
937                            struct dt_object *dt_obj,
938                            struct thandle *th)
939 {
940         int rc;
941
942         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
943         rc = dt_ref_del(env, dt_obj, th);
944         dt_write_unlock(env, dt_obj);
945
946         return rc;
947 }
948
949 static int out_tx_ref_add_exec(const struct lu_env *env, struct thandle *th,
950                                struct tx_arg *arg)
951 {
952         struct dt_object *dt_obj = arg->object;
953         int rc;
954
955         rc = out_obj_ref_add(env, dt_obj, th);
956
957         CDEBUG(D_INFO, "%s: insert ref_add reply %p index %d: rc = %d\n",
958                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
959
960         if (arg->reply != NULL)
961                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
962                                             rc);
963         return rc;
964 }
965
966 static int out_tx_ref_add_undo(const struct lu_env *env, struct thandle *th,
967                                struct tx_arg *arg)
968 {
969         return out_obj_ref_del(env, arg->object, th);
970 }
971
972 int out_ref_add_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
973                          struct thandle_exec_args *ta,
974                          struct thandle *th,
975                          struct object_update_reply *reply, int index,
976                          const char *file, int line)
977 {
978         struct tx_arg   *arg;
979         int             rc;
980
981         rc = dt_declare_ref_add(env, dt_obj, th);
982         if (rc != 0)
983                 return rc;
984
985         arg = tx_add_exec(ta, out_tx_ref_add_exec, out_tx_ref_add_undo, file,
986                           line);
987         if (IS_ERR(arg))
988                 return PTR_ERR(arg);
989
990         lu_object_get(&dt_obj->do_lu);
991         arg->object = dt_obj;
992         arg->reply = reply;
993         arg->index = index;
994         return 0;
995 }
996
997 static int out_tx_ref_del_exec(const struct lu_env *env, struct thandle *th,
998                                struct tx_arg *arg)
999 {
1000         struct dt_object        *dt_obj = arg->object;
1001         int                      rc;
1002
1003         rc = out_obj_ref_del(env, dt_obj, th);
1004
1005         CDEBUG(D_INFO, "%s: insert ref_del reply %p index %d: rc = %d\n",
1006                dt_obd_name(th->th_dev), arg->reply, arg->index, 0);
1007
1008         if (arg->reply != NULL)
1009                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1010                                             rc);
1011
1012         return rc;
1013 }
1014
1015 static int out_tx_ref_del_undo(const struct lu_env *env, struct thandle *th,
1016                                struct tx_arg *arg)
1017 {
1018         return out_obj_ref_add(env, arg->object, th);
1019 }
1020
1021 int out_ref_del_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
1022                          struct thandle_exec_args *ta,
1023                          struct thandle *th,
1024                          struct object_update_reply *reply, int index,
1025                          const char *file, int line)
1026 {
1027         struct tx_arg   *arg;
1028         int             rc;
1029
1030         rc = dt_declare_ref_del(env, dt_obj, th);
1031         if (rc != 0)
1032                 return rc;
1033
1034         arg = tx_add_exec(ta, out_tx_ref_del_exec, out_tx_ref_del_undo, file,
1035                           line);
1036         if (IS_ERR(arg))
1037                 return PTR_ERR(arg);
1038
1039         lu_object_get(&dt_obj->do_lu);
1040         arg->object = dt_obj;
1041         arg->reply = reply;
1042         arg->index = index;
1043         return 0;
1044 }
1045
1046 static int out_obj_index_insert(const struct lu_env *env,
1047                                 struct dt_object *dt_obj,
1048                                 const struct dt_rec *rec,
1049                                 const struct dt_key *key,
1050                                 struct thandle *th)
1051 {
1052         int rc;
1053
1054         CDEBUG(D_INFO, "%s: index insert "DFID" name: %s fid "DFID", type %u\n",
1055                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
1056                (char *)key, PFID(((struct dt_insert_rec *)rec)->rec_fid),
1057                ((struct dt_insert_rec *)rec)->rec_type);
1058
1059         if (dt_try_as_dir(env, dt_obj) == 0)
1060                 return -ENOTDIR;
1061
1062         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
1063         rc = dt_insert(env, dt_obj, rec, key, th, 0);
1064         dt_write_unlock(env, dt_obj);
1065
1066         return rc;
1067 }
1068
1069 static int out_obj_index_delete(const struct lu_env *env,
1070                                 struct dt_object *dt_obj,
1071                                 const struct dt_key *key,
1072                                 struct thandle *th)
1073 {
1074         int rc;
1075
1076         CDEBUG(D_INFO, "%s: index delete "DFID" name: %s\n",
1077                dt_obd_name(th->th_dev), PFID(lu_object_fid(&dt_obj->do_lu)),
1078                (char *)key);
1079
1080         if (dt_try_as_dir(env, dt_obj) == 0)
1081                 return -ENOTDIR;
1082
1083         dt_write_lock(env, dt_obj, MOR_TGT_CHILD);
1084         rc = dt_delete(env, dt_obj, key, th);
1085         dt_write_unlock(env, dt_obj);
1086
1087         return rc;
1088 }
1089
1090 static int out_tx_index_insert_exec(const struct lu_env *env,
1091                                     struct thandle *th, struct tx_arg *arg)
1092 {
1093         struct dt_object *dt_obj = arg->object;
1094         int rc;
1095
1096         if (unlikely(!dt_object_exists(dt_obj)))
1097                 RETURN(-ESTALE);
1098
1099         rc = out_obj_index_insert(env, dt_obj,
1100                                   (const struct dt_rec *)&arg->u.insert.rec,
1101                                   arg->u.insert.key, th);
1102
1103         CDEBUG(D_INFO, "%s: insert idx insert reply %p index %d: rc = %d\n",
1104                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1105
1106         if (arg->reply != NULL)
1107                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1108                                             rc);
1109         return rc;
1110 }
1111
1112 static int out_tx_index_insert_undo(const struct lu_env *env,
1113                                     struct thandle *th, struct tx_arg *arg)
1114 {
1115         return out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
1116 }
1117
1118 int out_index_insert_add_exec(const struct lu_env *env,
1119                               struct dt_object *dt_obj,
1120                               const struct dt_rec *rec,
1121                               const struct dt_key *key,
1122                               struct thandle_exec_args *ta,
1123                               struct thandle *th,
1124                               struct object_update_reply *reply,
1125                               int index, const char *file, int line)
1126 {
1127         struct tx_arg   *arg;
1128         int             rc;
1129
1130         if (dt_try_as_dir(env, dt_obj) == 0) {
1131                 rc = -ENOTDIR;
1132                 return rc;
1133         }
1134
1135         rc = dt_declare_insert(env, dt_obj, rec, key, th);
1136         if (rc != 0)
1137                 return rc;
1138
1139         arg = tx_add_exec(ta, out_tx_index_insert_exec,
1140                           out_tx_index_insert_undo, file, line);
1141         if (IS_ERR(arg))
1142                 return PTR_ERR(arg);
1143
1144         lu_object_get(&dt_obj->do_lu);
1145         arg->object = dt_obj;
1146         arg->reply = reply;
1147         arg->index = index;
1148         arg->u.insert.rec = *(const struct dt_insert_rec *)rec;
1149         arg->u.insert.key = key;
1150
1151         return 0;
1152 }
1153
1154 static int out_tx_index_delete_exec(const struct lu_env *env,
1155                                     struct thandle *th,
1156                                     struct tx_arg *arg)
1157 {
1158         int rc;
1159
1160         rc = out_obj_index_delete(env, arg->object, arg->u.insert.key, th);
1161
1162         CDEBUG(D_INFO, "%s: delete idx insert reply %p index %d: rc = %d\n",
1163                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1164
1165         if (arg->reply != NULL)
1166                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1167                                             rc);
1168
1169         return rc;
1170 }
1171
1172 static int out_tx_index_delete_undo(const struct lu_env *env,
1173                                     struct thandle *th,
1174                                     struct tx_arg *arg)
1175 {
1176         CERROR("%s: Oops, can not rollback index_delete yet: rc = %d\n",
1177                dt_obd_name(th->th_dev), -ENOTSUPP);
1178         return -ENOTSUPP;
1179 }
1180
1181 int out_index_delete_add_exec(const struct lu_env *env,
1182                               struct dt_object *dt_obj,
1183                               const struct dt_key *key,
1184                               struct thandle_exec_args *ta,
1185                               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         if (dt_try_as_dir(env, dt_obj) == 0) {
1193                 rc = -ENOTDIR;
1194                 return rc;
1195         }
1196
1197         LASSERT(ta->ta_handle != NULL);
1198         rc = dt_declare_delete(env, dt_obj, key, th);
1199         if (rc != 0)
1200                 return rc;
1201
1202         arg = tx_add_exec(ta, out_tx_index_delete_exec,
1203                           out_tx_index_delete_undo, file, line);
1204         if (IS_ERR(arg))
1205                 return PTR_ERR(arg);
1206
1207         lu_object_get(&dt_obj->do_lu);
1208         arg->object = dt_obj;
1209         arg->reply = reply;
1210         arg->index = index;
1211         arg->u.insert.key = key;
1212         return 0;
1213 }
1214
1215 static int out_tx_destroy_exec(const struct lu_env *env, struct thandle *th,
1216                                struct tx_arg *arg)
1217 {
1218         struct dt_object *dt_obj = arg->object;
1219         int rc;
1220
1221         rc = out_obj_destroy(env, dt_obj, th);
1222
1223         CDEBUG(D_INFO, "%s: insert destroy reply %p index %d: rc = %d\n",
1224                dt_obd_name(th->th_dev), arg->reply, arg->index, rc);
1225
1226         if (arg->reply != NULL)
1227                 object_update_result_insert(arg->reply, NULL, 0, arg->index,
1228                                             rc);
1229
1230         RETURN(rc);
1231 }
1232
1233 static int out_tx_destroy_undo(const struct lu_env *env, struct thandle *th,
1234                                struct tx_arg *arg)
1235 {
1236         CERROR("%s: not support destroy undo yet!: rc = %d\n",
1237                dt_obd_name(th->th_dev), -ENOTSUPP);
1238         return -ENOTSUPP;
1239 }
1240
1241 int out_destroy_add_exec(const struct lu_env *env, struct dt_object *dt_obj,
1242                          struct thandle_exec_args *ta, struct thandle *th,
1243                          struct object_update_reply *reply,
1244                          int index, const char *file, int line)
1245 {
1246         struct tx_arg   *arg;
1247         int             rc;
1248
1249         rc = dt_declare_destroy(env, dt_obj, th);
1250         if (rc != 0)
1251                 return rc;
1252
1253         arg = tx_add_exec(ta, out_tx_destroy_exec, out_tx_destroy_undo,
1254                           file, line);
1255         if (IS_ERR(arg))
1256                 return PTR_ERR(arg);
1257
1258         lu_object_get(&dt_obj->do_lu);
1259         arg->object = dt_obj;
1260         arg->reply = reply;
1261         arg->index = index;
1262         return 0;
1263 }