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