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