Whamcloud - gitweb
LU-9019 mdd: migrate from jiffies64 to ktime
[fs/lustre-release.git] / lustre / mdd / mdd_dir.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_dir.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@intel.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_mds.h>
44 #include <lustre_fid.h>
45
46 #include "mdd_internal.h"
47
48 static const char dot[] = ".";
49 static const char dotdot[] = "..";
50
51 static struct lu_name lname_dotdot = {
52         .ln_name        = (char *) dotdot,
53         .ln_namelen     = sizeof(dotdot) - 1,
54 };
55
56 static inline int
57 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
58 {
59         if (!lu_name_is_valid(ln))
60                 return -EINVAL;
61         else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
62                 return -ENAMETOOLONG;
63         else
64                 return 0;
65 }
66
67 /* Get FID from name and parent */
68 static int
69 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
70              const struct lu_attr *pattr, const struct lu_name *lname,
71              struct lu_fid* fid, int mask)
72 {
73         const char *name                = lname->ln_name;
74         const struct dt_key *key        = (const struct dt_key *)name;
75         struct mdd_object *mdd_obj      = md2mdd_obj(pobj);
76         struct mdd_device *m            = mdo2mdd(pobj);
77         struct dt_object *dir           = mdd_object_child(mdd_obj);
78         int rc;
79         ENTRY;
80
81         if (unlikely(mdd_is_dead_obj(mdd_obj)))
82                 RETURN(-ESTALE);
83
84         if (!mdd_object_exists(mdd_obj))
85                 RETURN(-ESTALE);
86
87         if (mdd_object_remote(mdd_obj)) {
88                 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
89                        mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
90         }
91
92         rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
93                                             MOR_TGT_PARENT);
94         if (rc)
95                 RETURN(rc);
96
97         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
98                    dt_try_as_dir(env, dir)))
99                 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
100         else
101                 rc = -ENOTDIR;
102
103         RETURN(rc);
104 }
105
106 int mdd_lookup(const struct lu_env *env,
107                struct md_object *pobj, const struct lu_name *lname,
108                struct lu_fid *fid, struct md_op_spec *spec)
109 {
110         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
111         int rc;
112         ENTRY;
113
114         rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
115         if (rc != 0)
116                 RETURN(rc);
117
118         rc = __mdd_lookup(env, pobj, pattr, lname, fid,
119                           (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
120         RETURN(rc);
121 }
122
123 /** Read the link EA into a temp buffer.
124  * Uses the mdd_thread_info::mti_big_buf since it is generally large.
125  * A pointer to the buffer is stored in \a ldata::ld_buf.
126  *
127  * \retval 0 or error
128  */
129 static int __mdd_links_read(const struct lu_env *env,
130                             struct mdd_object *mdd_obj,
131                             struct linkea_data *ldata)
132 {
133         int rc;
134
135         if (!mdd_object_exists(mdd_obj))
136                 return -ENODATA;
137
138         /* First try a small buf */
139         LASSERT(env != NULL);
140         ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
141                                                PAGE_SIZE);
142         if (ldata->ld_buf->lb_buf == NULL)
143                 return -ENOMEM;
144
145         rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
146         if (rc == -ERANGE) {
147                 /* Buf was too small, figure out what we need. */
148                 lu_buf_free(ldata->ld_buf);
149                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
150                                    XATTR_NAME_LINK);
151                 if (rc < 0)
152                         return rc;
153                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
154                 if (ldata->ld_buf->lb_buf == NULL)
155                         return -ENOMEM;
156                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
157                                   XATTR_NAME_LINK);
158         }
159         if (rc < 0) {
160                 lu_buf_free(ldata->ld_buf);
161                 ldata->ld_buf = NULL;
162                 return rc;
163         }
164
165         return linkea_init(ldata);
166 }
167
168 static int mdd_links_read(const struct lu_env *env,
169                           struct mdd_object *mdd_obj,
170                           struct linkea_data *ldata)
171 {
172         int rc;
173
174         rc = __mdd_links_read(env, mdd_obj, ldata);
175         if (!rc)
176                 rc = linkea_init(ldata);
177
178         return rc;
179 }
180
181 static int mdd_links_read_with_rec(const struct lu_env *env,
182                                    struct mdd_object *mdd_obj,
183                                    struct linkea_data *ldata)
184 {
185         int rc;
186
187         rc = __mdd_links_read(env, mdd_obj, ldata);
188         if (!rc)
189                 rc = linkea_init_with_rec(ldata);
190
191         return rc;
192 }
193
194 /**
195  * Get parent FID of the directory
196  *
197  * Read parent FID from linkEA, if that fails, then do lookup
198  * dotdot to get the parent FID.
199  *
200  * \param[in] env       execution environment
201  * \param[in] obj       object from which to find the parent FID
202  * \param[in] attr      attribute of the object
203  * \param[out] fid      fid to get the parent FID
204  *
205  * \retval              0 if getting the parent FID succeeds.
206  * \retval              negative errno if getting the parent FID fails.
207  **/
208 static inline int mdd_parent_fid(const struct lu_env *env,
209                                  struct mdd_object *obj,
210                                  const struct lu_attr *attr,
211                                  struct lu_fid *fid)
212 {
213         struct mdd_thread_info  *info = mdd_env_info(env);
214         struct linkea_data      ldata = { NULL };
215         struct lu_buf           *buf = &info->mti_link_buf;
216         struct lu_name          lname;
217         int                     rc = 0;
218
219         ENTRY;
220
221         LASSERT(S_ISDIR(mdd_object_type(obj)));
222
223         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
224         if (buf->lb_buf == NULL)
225                 GOTO(lookup, rc = 0);
226
227         ldata.ld_buf = buf;
228         rc = mdd_links_read_with_rec(env, obj, &ldata);
229         if (rc != 0)
230                 GOTO(lookup, rc);
231
232         LASSERT(ldata.ld_leh != NULL);
233         /* Directory should only have 1 parent */
234         if (ldata.ld_leh->leh_reccount > 1)
235                 GOTO(lookup, rc);
236
237         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
238
239         linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
240         if (likely(fid_is_sane(fid)))
241                 RETURN(0);
242 lookup:
243         rc =  __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
244         RETURN(rc);
245 }
246
247 /*
248  * For root fid use special function, which does not compare version component
249  * of fid. Version component is different for root fids on all MDTs.
250  */
251 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
252 {
253         return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
254                 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
255 }
256
257 /*
258  * return 1: if lf is the fid of the ancestor of p1;
259  * return 0: if not;
260  *
261  * return -EREMOTE: if remote object is found, in this
262  * case fid of remote object is saved to @pf;
263  *
264  * otherwise: values < 0, errors.
265  */
266 static int mdd_is_parent(const struct lu_env *env,
267                         struct mdd_device *mdd,
268                         struct mdd_object *p1,
269                         const struct lu_attr *attr,
270                         const struct lu_fid *lf,
271                         struct lu_fid *pf)
272 {
273         struct mdd_object *parent = NULL;
274         struct lu_fid *pfid;
275         int rc;
276         ENTRY;
277
278         LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
279         pfid = &mdd_env_info(env)->mti_fid;
280
281         /* Check for root first. */
282         if (mdd_is_root(mdd, mdo2fid(p1)))
283                 RETURN(0);
284
285         for(;;) {
286                 /* this is done recursively */
287                 rc = mdd_parent_fid(env, p1, attr, pfid);
288                 if (rc)
289                         GOTO(out, rc);
290                 if (mdd_is_root(mdd, pfid))
291                         GOTO(out, rc = 0);
292                 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
293                         GOTO(out, rc = 0);
294                 if (lu_fid_eq(pfid, lf))
295                         GOTO(out, rc = 1);
296                 if (parent != NULL)
297                         mdd_object_put(env, parent);
298
299                 parent = mdd_object_find(env, mdd, pfid);
300                 if (IS_ERR(parent))
301                         GOTO(out, rc = PTR_ERR(parent));
302
303                 if (!mdd_object_exists(parent))
304                         GOTO(out, rc = -EINVAL);
305
306                 p1 = parent;
307         }
308         EXIT;
309 out:
310         if (parent && !IS_ERR(parent))
311                 mdd_object_put(env, parent);
312         return rc;
313 }
314
315 /*
316  * No permission check is needed.
317  *
318  * returns 1: if fid is ancestor of @mo;
319  * returns 0: if fid is not an ancestor of @mo;
320  *
321  * returns EREMOTE if remote object is found, fid of remote object is saved to
322  * @fid;
323  *
324  * returns < 0: if error
325  */
326 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
327                   const struct lu_fid *fid, struct lu_fid *sfid)
328 {
329         struct mdd_device *mdd = mdo2mdd(mo);
330         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
331         int rc;
332         ENTRY;
333
334         if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
335                 RETURN(0);
336
337         rc = mdd_la_get(env, md2mdd_obj(mo), attr);
338         if (rc != 0)
339                 RETURN(rc);
340
341         rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
342         if (rc == 0) {
343                 /* found root */
344                 fid_zero(sfid);
345         } else if (rc == 1) {
346                 /* found @fid is parent */
347                 *sfid = *fid;
348                 rc = 0;
349         }
350         RETURN(rc);
351 }
352
353 /*
354  * Check that @dir contains no entries except (possibly) dot and dotdot.
355  *
356  * Returns:
357  *
358  *             0        empty
359  *      -ENOTDIR        not a directory object
360  *    -ENOTEMPTY        not empty
361  *           -ve        other error
362  *
363  */
364 static int mdd_dir_is_empty(const struct lu_env *env,
365                             struct mdd_object *dir)
366 {
367         struct dt_it     *it;
368         struct dt_object *obj;
369         const struct dt_it_ops *iops;
370         int result;
371         ENTRY;
372
373         obj = mdd_object_child(dir);
374         if (!dt_try_as_dir(env, obj))
375                 RETURN(-ENOTDIR);
376
377         iops = &obj->do_index_ops->dio_it;
378         it = iops->init(env, obj, LUDA_64BITHASH);
379         if (!IS_ERR(it)) {
380                 result = iops->get(env, it, (const struct dt_key *)"");
381                 if (result > 0) {
382                         int i;
383                         for (result = 0, i = 0; result == 0 && i < 3; ++i)
384                                 result = iops->next(env, it);
385                         if (result == 0)
386                                 result = -ENOTEMPTY;
387                         else if (result == 1)
388                                 result = 0;
389                 } else if (result == 0)
390                         /*
391                          * Huh? Index contains no zero key?
392                          */
393                         result = -EIO;
394
395                 iops->put(env, it);
396                 iops->fini(env, it);
397         } else
398                 result = PTR_ERR(it);
399         RETURN(result);
400 }
401
402 /**
403  * Determine if the target object can be hard linked, and right now it only
404  * checks if the link count reach the maximum limit. Note: for ldiskfs, the
405  * directory nlink count might exceed the maximum link count(see
406  * osd_object_ref_add), so it only check nlink for non-directories.
407  *
408  * \param[in] env       thread environment
409  * \param[in] obj       object being linked to
410  * \param[in] la        attributes of \a obj
411  *
412  * \retval              0 if \a obj can be hard linked
413  * \retval              negative error if \a obj is a directory or has too
414  *                      many links
415  */
416 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
417                           const struct lu_attr *la)
418 {
419         struct mdd_device *m = mdd_obj2mdd_dev(obj);
420         ENTRY;
421
422         LASSERT(la != NULL);
423
424         /* Subdir count limitation can be broken through
425          * (see osd_object_ref_add), so only check non-directory here. */
426         if (!S_ISDIR(la->la_mode) &&
427             la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
428                 RETURN(-EMLINK);
429
430         RETURN(0);
431 }
432
433 /**
434  * Check whether it may create the cobj under the pobj.
435  *
436  * \param[in] env       execution environment
437  * \param[in] pobj      the parent directory
438  * \param[in] pattr     the attribute of the parent directory
439  * \param[in] cobj      the child to be created
440  * \param[in] check_perm        if check WRITE|EXEC permission for parent
441  *
442  * \retval              = 0 create the child under this dir is allowed
443  * \retval              negative errno create the child under this dir is
444  *                      not allowed
445  */
446 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
447                    const struct lu_attr *pattr, struct mdd_object *cobj,
448                    bool check_perm)
449 {
450         int rc = 0;
451         ENTRY;
452
453         if (cobj && mdd_object_exists(cobj))
454                 RETURN(-EEXIST);
455
456         if (mdd_is_dead_obj(pobj))
457                 RETURN(-ENOENT);
458
459         if (check_perm)
460                 rc = mdd_permission_internal_locked(env, pobj, pattr,
461                                                     MAY_WRITE | MAY_EXEC,
462                                                     MOR_TGT_PARENT);
463         RETURN(rc);
464 }
465
466 /*
467  * Check whether can unlink from the pobj in the case of "cobj == NULL".
468  */
469 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
470                    const struct lu_attr *pattr, const struct lu_attr *attr)
471 {
472         int rc;
473         ENTRY;
474
475         if (mdd_is_dead_obj(pobj))
476                 RETURN(-ENOENT);
477
478         if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
479                 RETURN(-EPERM);
480
481         rc = mdd_permission_internal_locked(env, pobj, pattr,
482                                             MAY_WRITE | MAY_EXEC,
483                                             MOR_TGT_PARENT);
484         if (rc != 0)
485                 RETURN(rc);
486
487         if (pattr->la_flags & LUSTRE_APPEND_FL)
488                 RETURN(-EPERM);
489
490         RETURN(rc);
491 }
492
493 /*
494  * pobj == NULL is remote ops case, under such case, pobj's
495  * VTX feature has been checked already, no need check again.
496  */
497 static inline int mdd_is_sticky(const struct lu_env *env,
498                                 struct mdd_object *pobj,
499                                 const struct lu_attr *pattr,
500                                 struct mdd_object *cobj,
501                                 const struct lu_attr *cattr)
502 {
503         struct lu_ucred *uc = lu_ucred_assert(env);
504
505         if (pobj != NULL) {
506                 LASSERT(pattr != NULL);
507                 if (!(pattr->la_mode & S_ISVTX) ||
508                     (pattr->la_uid == uc->uc_fsuid))
509                         return 0;
510         }
511
512         LASSERT(cattr != NULL);
513         if (cattr->la_uid == uc->uc_fsuid)
514                 return 0;
515
516         return !md_capable(uc, CFS_CAP_FOWNER);
517 }
518
519 static int mdd_may_delete_entry(const struct lu_env *env,
520                                 struct mdd_object *pobj,
521                                 const struct lu_attr *pattr,
522                                 int check_perm)
523 {
524         ENTRY;
525
526         LASSERT(pobj != NULL);
527         if (!mdd_object_exists(pobj))
528                 RETURN(-ENOENT);
529
530         if (mdd_is_dead_obj(pobj))
531                 RETURN(-ENOENT);
532
533         if (check_perm) {
534                 int rc;
535                 rc = mdd_permission_internal_locked(env, pobj, pattr,
536                                             MAY_WRITE | MAY_EXEC,
537                                             MOR_TGT_PARENT);
538                 if (rc)
539                         RETURN(rc);
540         }
541
542         if (pattr->la_flags & LUSTRE_APPEND_FL)
543                 RETURN(-EPERM);
544
545         RETURN(0);
546 }
547
548 /*
549  * Check whether it may delete the cobj from the pobj.
550  * pobj maybe NULL
551  */
552 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
553                    const struct lu_attr *tpattr, struct mdd_object *tobj,
554                    const struct lu_attr *tattr, const struct lu_attr *cattr,
555                    int check_perm, int check_empty)
556 {
557         int rc = 0;
558         ENTRY;
559
560         if (tpobj) {
561                 LASSERT(tpattr != NULL);
562                 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
563                 if (rc != 0)
564                         RETURN(rc);
565         }
566
567         if (tobj == NULL)
568                 RETURN(0);
569
570         if (!mdd_object_exists(tobj))
571                 RETURN(-ENOENT);
572
573         if (mdd_is_dead_obj(tobj))
574                 RETURN(-ESTALE);
575
576         if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
577                 RETURN(-EPERM);
578
579         if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
580                 RETURN(-EPERM);
581
582         /* additional check the rename case */
583         if (cattr) {
584                 if (S_ISDIR(cattr->la_mode)) {
585                         struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
586
587                         if (!S_ISDIR(tattr->la_mode))
588                                 RETURN(-ENOTDIR);
589
590                         if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
591                                 RETURN(-EBUSY);
592                 } else if (S_ISDIR(tattr->la_mode))
593                         RETURN(-EISDIR);
594         }
595
596         if (S_ISDIR(tattr->la_mode) && check_empty)
597                 rc = mdd_dir_is_empty(env, tobj);
598
599         RETURN(rc);
600 }
601
602 /**
603  * Check whether it can create the link file(linked to @src_obj) under
604  * the target directory(@tgt_obj), and src_obj has been locked by
605  * mdd_write_lock.
606  *
607  * \param[in] env       execution environment
608  * \param[in] tgt_obj   the target directory
609  * \param[in] tattr     attributes of target directory
610  * \param[in] lname     the link name
611  * \param[in] src_obj   source object for link
612  * \param[in] cattr     attributes for source object
613  *
614  * \retval              = 0 it is allowed to create the link file under tgt_obj
615  * \retval              negative error not allowed to create the link file
616  */
617 static int mdd_link_sanity_check(const struct lu_env *env,
618                                  struct mdd_object *tgt_obj,
619                                  const struct lu_attr *tattr,
620                                  const struct lu_name *lname,
621                                  struct mdd_object *src_obj,
622                                  const struct lu_attr *cattr)
623 {
624         struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
625         int rc = 0;
626         ENTRY;
627
628         if (!mdd_object_exists(src_obj))
629                 RETURN(-ENOENT);
630
631         if (mdd_is_dead_obj(src_obj))
632                 RETURN(-ESTALE);
633
634         /* Local ops, no lookup before link, check filename length here. */
635         rc = mdd_name_check(m, lname);
636         if (rc < 0)
637                 RETURN(rc);
638
639         if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
640                 RETURN(-EPERM);
641
642         if (S_ISDIR(mdd_object_type(src_obj)))
643                 RETURN(-EPERM);
644
645         LASSERT(src_obj != tgt_obj);
646         rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
647         if (rc != 0)
648                 RETURN(rc);
649
650         rc = __mdd_may_link(env, src_obj, cattr);
651
652         RETURN(rc);
653 }
654
655 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
656                                    const char *name, struct thandle *handle)
657 {
658         struct dt_object *next = mdd_object_child(pobj);
659         int rc;
660         ENTRY;
661
662         if (dt_try_as_dir(env, next))
663                 rc = dt_delete(env, next, (struct dt_key *)name, handle);
664         else
665                 rc = -ENOTDIR;
666
667         RETURN(rc);
668 }
669
670 static int __mdd_index_insert_only(const struct lu_env *env,
671                                    struct mdd_object *pobj,
672                                    const struct lu_fid *lf, __u32 type,
673                                    const char *name,
674                                    struct thandle *handle)
675 {
676         struct dt_object *next = mdd_object_child(pobj);
677         int               rc;
678         ENTRY;
679
680         if (dt_try_as_dir(env, next)) {
681                 struct dt_insert_rec    *rec = &mdd_env_info(env)->mti_dt_rec;
682                 struct lu_ucred         *uc  = lu_ucred_check(env);
683                 int                      ignore_quota;
684
685                 rec->rec_fid = lf;
686                 rec->rec_type = type;
687                 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
688                 rc = dt_insert(env, next, (const struct dt_rec *)rec,
689                                (const struct dt_key *)name, handle,
690                                ignore_quota);
691         } else {
692                 rc = -ENOTDIR;
693         }
694         RETURN(rc);
695 }
696
697 /* insert named index, add reference if isdir */
698 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
699                               const struct lu_fid *lf, __u32 type,
700                               const char *name, struct thandle *handle)
701 {
702         int rc;
703         ENTRY;
704
705         rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
706         if (rc == 0 && S_ISDIR(type)) {
707                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
708                 mdo_ref_add(env, pobj, handle);
709                 mdd_write_unlock(env, pobj);
710         }
711
712         RETURN(rc);
713 }
714
715 /* delete named index, drop reference if isdir */
716 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
717                               const char *name, int is_dir,
718                               struct thandle *handle)
719 {
720         int               rc;
721         ENTRY;
722
723         rc = __mdd_index_delete_only(env, pobj, name, handle);
724         if (rc == 0 && is_dir) {
725                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
726                 mdo_ref_del(env, pobj, handle);
727                 mdd_write_unlock(env, pobj);
728         }
729
730         RETURN(rc);
731 }
732
733 static int mdd_llog_record_calc_size(const struct lu_env *env,
734                                      const struct lu_name *tname,
735                                      const struct lu_name *sname)
736 {
737         const struct lu_ucred   *uc = lu_ucred(env);
738         enum changelog_rec_flags crf = 0;
739         size_t                   hdr_size = sizeof(struct llog_changelog_rec) -
740                                             sizeof(struct changelog_rec);
741
742         if (sname != NULL)
743                 crf |= CLF_RENAME;
744
745         if (uc != NULL && uc->uc_jobid[0] != '\0')
746                 crf |= CLF_JOBID;
747
748         return llog_data_len(hdr_size + changelog_rec_offset(crf) +
749                              (tname != NULL ? tname->ln_namelen : 0) +
750                              (sname != NULL ? 1 + sname->ln_namelen : 0));
751 }
752
753 int mdd_declare_changelog_store(const struct lu_env *env,
754                                 struct mdd_device *mdd,
755                                 const struct lu_name *tname,
756                                 const struct lu_name *sname,
757                                 struct thandle *handle)
758 {
759         struct obd_device               *obd = mdd2obd_dev(mdd);
760         struct llog_ctxt                *ctxt;
761         struct llog_changelog_rec       *rec;
762         struct lu_buf                   *buf;
763         struct thandle                  *llog_th;
764         int                              reclen;
765         int                              rc;
766
767         /* Not recording */
768         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
769                 return 0;
770
771         reclen = mdd_llog_record_calc_size(env, tname, sname);
772         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
773         if (buf->lb_buf == NULL)
774                 return -ENOMEM;
775
776         rec = buf->lb_buf;
777         rec->cr_hdr.lrh_len = reclen;
778         rec->cr_hdr.lrh_type = CHANGELOG_REC;
779
780         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
781         if (ctxt == NULL)
782                 return -ENXIO;
783
784         llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
785         if (IS_ERR(llog_th))
786                 GOTO(out_put, rc = PTR_ERR(llog_th));
787
788         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
789
790 out_put:
791         llog_ctxt_put(ctxt);
792
793         return rc;
794 }
795
796 /** Add a changelog entry \a rec to the changelog llog
797  * \param mdd
798  * \param rec
799  * \param handle - currently ignored since llogs start their own transaction;
800  *              this will hopefully be fixed in llog rewrite
801  * \retval 0 ok
802  */
803 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
804                         struct llog_changelog_rec *rec, struct thandle *th)
805 {
806         struct obd_device       *obd = mdd2obd_dev(mdd);
807         struct llog_ctxt        *ctxt;
808         struct thandle          *llog_th;
809         int                      rc;
810
811         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
812                                             changelog_rec_varsize(&rec->cr));
813
814         /* llog_lvfs_write_rec sets the llog tail len */
815         rec->cr_hdr.lrh_type = CHANGELOG_REC;
816         rec->cr.cr_time = cl_time();
817
818         spin_lock(&mdd->mdd_cl.mc_lock);
819         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
820          * but as long as the MDD transactions are ordered correctly for e.g.
821          * rename conflicts, I don't think this should matter. */
822         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
823         spin_unlock(&mdd->mdd_cl.mc_lock);
824
825         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
826         if (ctxt == NULL)
827                 return -ENXIO;
828
829         llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
830         if (IS_ERR(llog_th))
831                 GOTO(out_put, rc = PTR_ERR(llog_th));
832
833         /* nested journal transaction */
834         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
835
836 out_put:
837         llog_ctxt_put(ctxt);
838         if (rc > 0)
839                 rc = 0;
840         return rc;
841 }
842
843 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
844                                          const struct lu_fid *sfid,
845                                          const struct lu_fid *spfid,
846                                          const struct lu_name *sname)
847 {
848         struct changelog_ext_rename     *rnm = changelog_rec_rename(rec);
849         size_t                           extsize = sname->ln_namelen + 1;
850
851         LASSERT(sfid != NULL);
852         LASSERT(spfid != NULL);
853         LASSERT(sname != NULL);
854
855         rnm->cr_sfid = *sfid;
856         rnm->cr_spfid = *spfid;
857
858         changelog_rec_name(rec)[rec->cr_namelen] = '\0';
859         strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
860         rec->cr_namelen += extsize;
861 }
862
863 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
864 {
865         struct changelog_ext_jobid      *jid = changelog_rec_jobid(rec);
866
867         if (jobid == NULL || jobid[0] == '\0')
868                 return;
869
870         strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
871 }
872
873 /** Store a namespace change changelog record
874  * If this fails, we must fail the whole transaction; we don't
875  * want the change to commit without the log entry.
876  * \param target - mdd_object of change
877  * \param tpfid - target parent dir/object fid
878  * \param sfid - source object fid
879  * \param spfid - source parent fid
880  * \param tname - target name string
881  * \param sname - source name string
882  * \param handle - transaction handle
883  */
884 int mdd_changelog_ns_store(const struct lu_env *env,
885                            struct mdd_device *mdd,
886                            enum changelog_rec_type type,
887                            enum changelog_rec_flags crf,
888                            struct mdd_object *target,
889                            const struct lu_fid *tpfid,
890                            const struct lu_fid *sfid,
891                            const struct lu_fid *spfid,
892                            const struct lu_name *tname,
893                            const struct lu_name *sname,
894                            struct thandle *handle)
895 {
896         const struct lu_ucred           *uc = lu_ucred(env);
897         struct llog_changelog_rec       *rec;
898         struct lu_buf                   *buf;
899         int                              reclen;
900         int                              rc;
901         ENTRY;
902
903         /* Not recording */
904         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
905                 RETURN(0);
906
907         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
908                 RETURN(0);
909
910         LASSERT(tpfid != NULL);
911         LASSERT(tname != NULL);
912         LASSERT(handle != NULL);
913
914         reclen = mdd_llog_record_calc_size(env, tname, sname);
915         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
916         if (buf->lb_buf == NULL)
917                 RETURN(-ENOMEM);
918         rec = buf->lb_buf;
919
920         crf &= CLF_FLAGMASK;
921
922         if (uc != NULL && uc->uc_jobid[0] != '\0')
923                 crf |= CLF_JOBID;
924
925         if (sname != NULL)
926                 crf |= CLF_RENAME;
927         else
928                 crf |= CLF_VERSION;
929
930         rec->cr.cr_flags = crf;
931         rec->cr.cr_type = (__u32)type;
932         rec->cr.cr_pfid = *tpfid;
933         rec->cr.cr_namelen = tname->ln_namelen;
934         memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
935
936         if (crf & CLF_RENAME)
937                 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
938
939         if (crf & CLF_JOBID)
940                 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
941
942         if (likely(target != NULL)) {
943                 rec->cr.cr_tfid = *mdo2fid(target);
944                 target->mod_cltime = ktime_get();
945         } else {
946                 fid_zero(&rec->cr.cr_tfid);
947         }
948
949         rc = mdd_changelog_store(env, mdd, rec, handle);
950         if (rc < 0) {
951                 CERROR("%s: cannot store changelog record: type = %d, "
952                        "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
953                        mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
954                        PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
955                 return -EFAULT;
956         }
957
958         return 0;
959 }
960
961 static int __mdd_links_add(const struct lu_env *env,
962                            struct mdd_object *mdd_obj,
963                            struct linkea_data *ldata,
964                            const struct lu_name *lname,
965                            const struct lu_fid *pfid,
966                            int first, int check)
967 {
968         int rc;
969
970         if (ldata->ld_leh == NULL) {
971                 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
972                 if (rc) {
973                         if (rc != -ENODATA)
974                                 return rc;
975                         rc = linkea_data_new(ldata,
976                                              &mdd_env_info(env)->mti_link_buf);
977                         if (rc)
978                                 return rc;
979                 }
980         }
981
982         if (check) {
983                 rc = linkea_links_find(ldata, lname, pfid);
984                 if (rc && rc != -ENOENT)
985                         return rc;
986                 if (rc == 0)
987                         return -EEXIST;
988         }
989
990         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
991                 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
992
993                 *tfid = *pfid;
994                 tfid->f_ver = ~0;
995                 linkea_add_buf(ldata, lname, tfid);
996         }
997
998         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
999                 linkea_add_buf(ldata, lname, pfid);
1000
1001         return linkea_add_buf(ldata, lname, pfid);
1002 }
1003
1004 static int __mdd_links_del(const struct lu_env *env,
1005                            struct mdd_object *mdd_obj,
1006                            struct linkea_data *ldata,
1007                            const struct lu_name *lname,
1008                            const struct lu_fid *pfid)
1009 {
1010         int rc;
1011
1012         if (ldata->ld_leh == NULL) {
1013                 rc = mdd_links_read(env, mdd_obj, ldata);
1014                 if (rc)
1015                         return rc;
1016         }
1017
1018         rc = linkea_links_find(ldata, lname, pfid);
1019         if (rc)
1020                 return rc;
1021
1022         linkea_del_buf(ldata, lname);
1023         return 0;
1024 }
1025
1026 static int mdd_linkea_prepare(const struct lu_env *env,
1027                               struct mdd_object *mdd_obj,
1028                               const struct lu_fid *oldpfid,
1029                               const struct lu_name *oldlname,
1030                               const struct lu_fid *newpfid,
1031                               const struct lu_name *newlname,
1032                               int first, int check,
1033                               struct linkea_data *ldata)
1034 {
1035         int rc = 0;
1036         ENTRY;
1037
1038         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1039                 RETURN(0);
1040
1041         LASSERT(oldpfid != NULL || newpfid != NULL);
1042
1043         if (mdd_obj->mod_flags & DEAD_OBJ)
1044                 /* Unnecessary to update linkEA for dead object.  */
1045                 RETURN(0);
1046
1047         if (oldpfid != NULL) {
1048                 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1049                 if (rc) {
1050                         if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1051                                 RETURN(rc);
1052
1053                         /* No changes done. */
1054                         rc = 0;
1055                 }
1056         }
1057
1058         /* If renaming, add the new record */
1059         if (newpfid != NULL)
1060                 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1061                                      first, check);
1062
1063         RETURN(rc);
1064 }
1065
1066 int mdd_links_rename(const struct lu_env *env,
1067                      struct mdd_object *mdd_obj,
1068                      const struct lu_fid *oldpfid,
1069                      const struct lu_name *oldlname,
1070                      const struct lu_fid *newpfid,
1071                      const struct lu_name *newlname,
1072                      struct thandle *handle,
1073                      struct linkea_data *ldata,
1074                      int first, int check)
1075 {
1076         int rc = 0;
1077         ENTRY;
1078
1079         if (ldata == NULL) {
1080                 ldata = &mdd_env_info(env)->mti_link_data;
1081                 memset(ldata, 0, sizeof(*ldata));
1082                 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1083                                         newpfid, newlname, first, check, ldata);
1084                 if (rc)
1085                         GOTO(out, rc);
1086         }
1087
1088         if (!(mdd_obj->mod_flags & DEAD_OBJ))
1089                 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1090
1091         GOTO(out, rc);
1092
1093 out:
1094         if (rc != 0) {
1095                 if (newlname == NULL)
1096                         CERROR("link_ea add failed %d "DFID"\n",
1097                                rc, PFID(mdd_object_fid(mdd_obj)));
1098                 else if (oldpfid == NULL)
1099                         CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1100                                newlname->ln_namelen, newlname->ln_name, rc,
1101                                PFID(mdd_object_fid(mdd_obj)));
1102                 else if (newpfid == NULL)
1103                         CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1104                                oldlname->ln_namelen, oldlname->ln_name, rc,
1105                                PFID(mdd_object_fid(mdd_obj)));
1106                 else
1107                         CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1108                                "\n", oldlname->ln_namelen, oldlname->ln_name,
1109                                newlname->ln_namelen, newlname->ln_name, rc,
1110                                PFID(mdd_object_fid(mdd_obj)));
1111         }
1112
1113         if (is_vmalloc_addr(ldata->ld_buf))
1114                 /* if we vmalloced a large buffer drop it */
1115                 lu_buf_free(ldata->ld_buf);
1116
1117         return rc;
1118 }
1119
1120 static inline int mdd_links_add(const struct lu_env *env,
1121                                 struct mdd_object *mdd_obj,
1122                                 const struct lu_fid *pfid,
1123                                 const struct lu_name *lname,
1124                                 struct thandle *handle,
1125                                 struct linkea_data *ldata, int first)
1126 {
1127         return mdd_links_rename(env, mdd_obj, NULL, NULL,
1128                                 pfid, lname, handle, ldata, first, 0);
1129 }
1130
1131 static inline int mdd_links_del(const struct lu_env *env,
1132                                 struct mdd_object *mdd_obj,
1133                                 const struct lu_fid *pfid,
1134                                 const struct lu_name *lname,
1135                                 struct thandle *handle)
1136 {
1137         return mdd_links_rename(env, mdd_obj, pfid, lname,
1138                                 NULL, NULL, handle, NULL, 0, 0);
1139 }
1140
1141 /** Read the link EA into a temp buffer.
1142  * Uses the name_buf since it is generally large.
1143  * \retval IS_ERR err
1144  * \retval ptr to \a lu_buf (always \a mti_big_buf)
1145  */
1146 struct lu_buf *mdd_links_get(const struct lu_env *env,
1147                              struct mdd_object *mdd_obj)
1148 {
1149         struct linkea_data ldata = { NULL };
1150         int rc;
1151
1152         rc = mdd_links_read(env, mdd_obj, &ldata);
1153         return rc ? ERR_PTR(rc) : ldata.ld_buf;
1154 }
1155
1156 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1157                     struct linkea_data *ldata, struct thandle *handle)
1158 {
1159         const struct lu_buf *buf;
1160         int                 rc;
1161
1162         if (ldata == NULL || ldata->ld_buf == NULL ||
1163             ldata->ld_leh == NULL)
1164                 return 0;
1165
1166         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1167                 return 0;
1168
1169 again:
1170         buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1171                                 ldata->ld_leh->leh_len);
1172         rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1173         if (unlikely(rc == -ENOSPC)) {
1174                 rc = linkea_overflow_shrink(ldata);
1175                 if (likely(rc > 0))
1176                         goto again;
1177         }
1178
1179         return rc;
1180 }
1181
1182 static int mdd_declare_links_add(const struct lu_env *env,
1183                                  struct mdd_object *mdd_obj,
1184                                  struct thandle *handle,
1185                                  struct linkea_data *ldata)
1186 {
1187         int     rc;
1188         int     ea_len;
1189         void    *linkea;
1190
1191         if (ldata != NULL && ldata->ld_leh != NULL) {
1192                 ea_len = ldata->ld_leh->leh_len;
1193                 linkea = ldata->ld_buf->lb_buf;
1194         } else {
1195                 ea_len = MAX_LINKEA_SIZE;
1196                 linkea = NULL;
1197         }
1198
1199         rc = mdo_declare_xattr_set(env, mdd_obj,
1200                                    mdd_buf_get_const(env, linkea, ea_len),
1201                                    XATTR_NAME_LINK, 0, handle);
1202
1203         return rc;
1204 }
1205
1206 static inline int mdd_declare_links_del(const struct lu_env *env,
1207                                         struct mdd_object *c,
1208                                         struct thandle *handle)
1209 {
1210         int rc = 0;
1211
1212         /* For directory, the linkEA will be removed together
1213          * with the object. */
1214         if (!S_ISDIR(mdd_object_type(c)))
1215                 rc = mdd_declare_links_add(env, c, handle, NULL);
1216
1217         return rc;
1218 }
1219
1220 static int mdd_declare_link(const struct lu_env *env,
1221                             struct mdd_device *mdd,
1222                             struct mdd_object *p,
1223                             struct mdd_object *c,
1224                             const struct lu_name *name,
1225                             struct thandle *handle,
1226                             struct lu_attr *la,
1227                             struct linkea_data *data)
1228 {
1229         struct lu_fid tfid = *mdo2fid(c);
1230         int rc;
1231
1232         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1233                 tfid.f_oid = cfs_fail_val;
1234
1235         rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1236                                       name->ln_name, handle);
1237         if (rc != 0)
1238                 return rc;
1239
1240         rc = mdo_declare_ref_add(env, c, handle);
1241         if (rc != 0)
1242                 return rc;
1243
1244         la->la_valid = LA_CTIME | LA_MTIME;
1245         rc = mdo_declare_attr_set(env, p, la, handle);
1246         if (rc != 0)
1247                 return rc;
1248
1249         la->la_valid = LA_CTIME;
1250         rc = mdo_declare_attr_set(env, c, la, handle);
1251         if (rc != 0)
1252                 return rc;
1253
1254         rc = mdd_declare_links_add(env, c, handle, data);
1255         if (rc != 0)
1256                 return rc;
1257
1258         rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1259
1260         return rc;
1261 }
1262
1263 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1264                     struct md_object *src_obj, const struct lu_name *lname,
1265                     struct md_attr *ma)
1266 {
1267         const char *name = lname->ln_name;
1268         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1269         struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1270         struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1271         struct lu_attr    *cattr = MDD_ENV_VAR(env, cattr);
1272         struct lu_attr    *tattr = MDD_ENV_VAR(env, tattr);
1273         struct mdd_device *mdd = mdo2mdd(src_obj);
1274         struct thandle *handle;
1275         struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1276         struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1277         int rc;
1278         ENTRY;
1279
1280         rc = mdd_la_get(env, mdd_sobj, cattr);
1281         if (rc != 0)
1282                 RETURN(rc);
1283
1284         rc = mdd_la_get(env, mdd_tobj, tattr);
1285         if (rc != 0)
1286                 RETURN(rc);
1287
1288         /*
1289          * If we are using project inheritance, we only allow hard link
1290          * creation in our tree when the project IDs are the same;
1291          * otherwise the tree quota mechanism could be circumvented.
1292          */
1293         if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1294             (tattr->la_projid != cattr->la_projid))
1295                 RETURN(-EXDEV);
1296
1297         handle = mdd_trans_create(env, mdd);
1298         if (IS_ERR(handle))
1299                 GOTO(out_pending, rc = PTR_ERR(handle));
1300
1301         memset(ldata, 0, sizeof(*ldata));
1302
1303         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1304         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1305
1306         /* Note: even this function will change ldata, but it comes from
1307          * thread_info, which is completely temporary and only seen in
1308          * this function, so we do not need reset ldata once it fails.*/
1309         rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
1310                                 lname, 0, 0, ldata);
1311         if (rc != 0)
1312                 GOTO(stop, rc);
1313
1314         rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1315                               la, ldata);
1316         if (rc)
1317                 GOTO(stop, rc);
1318
1319         rc = mdd_trans_start(env, mdd, handle);
1320         if (rc)
1321                 GOTO(stop, rc);
1322
1323         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1324         rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1325                                    cattr);
1326         if (rc)
1327                 GOTO(out_unlock, rc);
1328
1329         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1330                 rc = mdo_ref_add(env, mdd_sobj, handle);
1331                 if (rc != 0)
1332                         GOTO(out_unlock, rc);
1333         }
1334
1335         *tfid = *mdo2fid(mdd_sobj);
1336         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1337                 tfid->f_oid = cfs_fail_val;
1338
1339         rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1340                                      mdd_object_type(mdd_sobj), name, handle);
1341         if (rc != 0) {
1342                 mdo_ref_del(env, mdd_sobj, handle);
1343                 GOTO(out_unlock, rc);
1344         }
1345
1346         la->la_valid = LA_CTIME | LA_MTIME;
1347         rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1348         if (rc)
1349                 GOTO(out_unlock, rc);
1350
1351         la->la_valid = LA_CTIME;
1352         rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1353         if (rc == 0)
1354                 /* Note: The failure of links_add should not cause the
1355                  * link failure, so do not check return value. */
1356                 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1357                               lname, handle, ldata, 0);
1358
1359         EXIT;
1360 out_unlock:
1361         mdd_write_unlock(env, mdd_sobj);
1362         if (rc == 0)
1363                 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1364                                             mdo2fid(mdd_tobj), NULL, NULL,
1365                                             lname, NULL, handle);
1366 stop:
1367         rc = mdd_trans_stop(env, mdd, rc, handle);
1368         if (is_vmalloc_addr(ldata->ld_buf))
1369                 /* if we vmalloced a large buffer drop it */
1370                 lu_buf_free(ldata->ld_buf);
1371 out_pending:
1372         return rc;
1373 }
1374
1375 static int mdd_mark_orphan_object(const struct lu_env *env,
1376                                 struct mdd_object *obj, struct thandle *handle,
1377                                 bool declare)
1378 {
1379         struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1380         int rc;
1381
1382         if (!S_ISDIR(mdd_object_type(obj)))
1383                 return 0;
1384
1385         attr->la_valid = LA_FLAGS;
1386         attr->la_flags = LUSTRE_ORPHAN_FL;
1387
1388         if (declare)
1389                 rc = mdo_declare_attr_set(env, obj, attr, handle);
1390         else
1391                 rc = mdo_attr_set(env, obj, attr, handle);
1392
1393         return rc;
1394 }
1395
1396 static int mdd_declare_finish_unlink(const struct lu_env *env,
1397                                      struct mdd_object *obj,
1398                                      struct thandle *handle)
1399 {
1400         int     rc;
1401
1402         /* Sigh, we do not know if the unlink object will become orphan in
1403          * declare phase, but fortunately the flags here does not matter
1404          * in current declare implementation */
1405         rc = mdd_mark_orphan_object(env, obj, handle, true);
1406         if (rc != 0)
1407                 return rc;
1408
1409         rc = mdo_declare_destroy(env, obj, handle);
1410         if (rc != 0)
1411                 return rc;
1412
1413         rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1414         if (rc != 0)
1415                 return rc;
1416
1417         return mdd_declare_links_del(env, obj, handle);
1418 }
1419
1420 /* caller should take a lock before calling */
1421 int mdd_finish_unlink(const struct lu_env *env,
1422                       struct mdd_object *obj, struct md_attr *ma,
1423                       const struct mdd_object *pobj,
1424                       const struct lu_name *lname,
1425                       struct thandle *th)
1426 {
1427         int rc = 0;
1428         int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1429         ENTRY;
1430
1431         LASSERT(mdd_write_locked(env, obj) != 0);
1432
1433         if (ma->ma_attr.la_nlink == 0 || is_dir) {
1434                 /* add new orphan and the object
1435                  * will be deleted during mdd_close() */
1436                 obj->mod_flags |= DEAD_OBJ;
1437                 if (obj->mod_count) {
1438                         rc = __mdd_orphan_add(env, obj, th);
1439                         if (rc == 0)
1440                                 CDEBUG(D_HA, "Object "DFID" is inserted into "
1441                                         "orphan list, open count = %d\n",
1442                                         PFID(mdd_object_fid(obj)),
1443                                         obj->mod_count);
1444                         else
1445                                 CERROR("Object "DFID" fail to be an orphan, "
1446                                        "open count = %d, maybe cause failed "
1447                                        "open replay\n",
1448                                         PFID(mdd_object_fid(obj)),
1449                                         obj->mod_count);
1450
1451                         /* mark object as an orphan here, not
1452                          * before __mdd_orphan_add() as racing
1453                          * mdd_la_get() may propagate ORPHAN_OBJ
1454                          * causing the asserition */
1455                         rc = mdd_mark_orphan_object(env, obj, th, false);
1456                 } else {
1457                         rc = mdo_destroy(env, obj, th);
1458                 }
1459         } else if (!is_dir) {
1460                 /* old files may not have link ea; ignore errors */
1461                 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1462         }
1463
1464         RETURN(rc);
1465 }
1466
1467 /*
1468  * pobj maybe NULL
1469  * has mdd_write_lock on cobj already, but not on pobj yet
1470  */
1471 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1472                             const struct lu_attr *pattr,
1473                             struct mdd_object *cobj,
1474                             const struct lu_attr *cattr)
1475 {
1476         int rc;
1477         ENTRY;
1478
1479         rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1480
1481         RETURN(rc);
1482 }
1483
1484 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1485                               struct mdd_object *p, struct mdd_object *c,
1486                               const struct lu_name *name, struct md_attr *ma,
1487                               struct thandle *handle, int no_name, int is_dir)
1488 {
1489         struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
1490         int              rc;
1491
1492         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1493                 if (likely(no_name == 0)) {
1494                         rc = mdo_declare_index_delete(env, p, name->ln_name,
1495                                                       handle);
1496                         if (rc != 0)
1497                                 return rc;
1498                 }
1499
1500                 if (is_dir != 0) {
1501                         rc = mdo_declare_ref_del(env, p, handle);
1502                         if (rc != 0)
1503                                 return rc;
1504                 }
1505         }
1506
1507         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1508         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1509         la->la_valid = LA_CTIME | LA_MTIME;
1510         rc = mdo_declare_attr_set(env, p, la, handle);
1511         if (rc)
1512                 return rc;
1513
1514         if (c != NULL) {
1515                 rc = mdo_declare_ref_del(env, c, handle);
1516                 if (rc)
1517                         return rc;
1518
1519                 rc = mdo_declare_ref_del(env, c, handle);
1520                 if (rc)
1521                         return rc;
1522
1523                 la->la_valid = LA_CTIME;
1524                 rc = mdo_declare_attr_set(env, c, la, handle);
1525                 if (rc)
1526                         return rc;
1527
1528                 rc = mdd_declare_finish_unlink(env, c, handle);
1529                 if (rc)
1530                         return rc;
1531
1532                 /* FIXME: need changelog for remove entry */
1533                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1534         }
1535
1536         return rc;
1537 }
1538
1539 /*
1540  * test if a file has an HSM archive
1541  * if HSM attributes are not found in ma update them from
1542  * HSM xattr
1543  */
1544 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1545                                    struct mdd_object *obj,
1546                                    struct md_attr *ma)
1547 {
1548         ENTRY;
1549
1550         if (!(ma->ma_valid & MA_HSM)) {
1551                 /* no HSM MD provided, read xattr */
1552                 struct lu_buf   *hsm_buf;
1553                 const size_t     buflen = sizeof(struct hsm_attrs);
1554                 int              rc;
1555
1556                 hsm_buf = mdd_buf_get(env, NULL, 0);
1557                 lu_buf_alloc(hsm_buf, buflen);
1558                 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1559                 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1560                 lu_buf_free(hsm_buf);
1561                 if (rc < 0)
1562                         RETURN(false);
1563
1564                 ma->ma_valid |= MA_HSM;
1565         }
1566         if (ma->ma_hsm.mh_flags & HS_EXISTS)
1567                 RETURN(true);
1568         RETURN(false);
1569 }
1570
1571 /**
1572  * Delete name entry and the object.
1573  * Note: no_name == 1 means it only destory the object, i.e. name_entry
1574  * does not exist for this object, and it could only happen during resending
1575  * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1576  * is also needed in this case(needed by changelog), so we have to add another
1577  * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1578  * the ENOENT failure should be able to be fixed by redo mechanism.
1579  */
1580 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1581                       struct md_object *cobj, const struct lu_name *lname,
1582                       struct md_attr *ma, int no_name)
1583 {
1584         const char *name = lname->ln_name;
1585         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1586         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1587         struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1588         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1589         struct mdd_object *mdd_cobj = NULL;
1590         struct mdd_device *mdd = mdo2mdd(pobj);
1591         struct thandle    *handle;
1592         int rc, is_dir = 0, cl_flags = 0;
1593         ENTRY;
1594
1595         /* cobj == NULL means only delete name entry */
1596         if (likely(cobj != NULL)) {
1597                 mdd_cobj = md2mdd_obj(cobj);
1598                 if (mdd_object_exists(mdd_cobj) == 0)
1599                         RETURN(-ENOENT);
1600         }
1601
1602         rc = mdd_la_get(env, mdd_pobj, pattr);
1603         if (rc)
1604                 RETURN(rc);
1605
1606         if (likely(mdd_cobj != NULL)) {
1607                 /* fetch cattr */
1608                 rc = mdd_la_get(env, mdd_cobj, cattr);
1609                 if (rc)
1610                         RETURN(rc);
1611
1612                 is_dir = S_ISDIR(cattr->la_mode);
1613                 /* search for an existing archive.
1614                  * we should check ahead as the object
1615                  * can be destroyed in this transaction */
1616                 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1617                         cl_flags |= CLF_UNLINK_HSM_EXISTS;
1618         }
1619
1620         rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1621         if (rc)
1622                 RETURN(rc);
1623
1624         handle = mdd_trans_create(env, mdd);
1625         if (IS_ERR(handle))
1626                 RETURN(PTR_ERR(handle));
1627
1628         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1629                                 lname, ma, handle, no_name, is_dir);
1630         if (rc)
1631                 GOTO(stop, rc);
1632
1633         rc = mdd_trans_start(env, mdd, handle);
1634         if (rc)
1635                 GOTO(stop, rc);
1636
1637         if (likely(mdd_cobj != NULL))
1638                 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1639
1640         if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1641                 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1642                 if (rc)
1643                         GOTO(cleanup, rc);
1644         }
1645
1646         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1647             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1648                 GOTO(cleanup, rc = 0);
1649
1650         if (likely(mdd_cobj != NULL)) {
1651                 rc = mdo_ref_del(env, mdd_cobj, handle);
1652                 if (rc != 0) {
1653                         __mdd_index_insert_only(env, mdd_pobj,
1654                                                 mdo2fid(mdd_cobj),
1655                                                 mdd_object_type(mdd_cobj),
1656                                                 name, handle);
1657                         GOTO(cleanup, rc);
1658                 }
1659
1660                 if (is_dir)
1661                         /* unlink dot */
1662                         mdo_ref_del(env, mdd_cobj, handle);
1663
1664                 /* fetch updated nlink */
1665                 rc = mdd_la_get(env, mdd_cobj, cattr);
1666                 if (rc)
1667                         GOTO(cleanup, rc);
1668         }
1669
1670         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1671         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1672
1673         la->la_valid = LA_CTIME | LA_MTIME;
1674         rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1675         if (rc)
1676                 GOTO(cleanup, rc);
1677
1678         /* Enough for only unlink the entry */
1679         if (unlikely(mdd_cobj == NULL))
1680                 GOTO(stop, rc);
1681
1682         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1683                 /* update ctime of an unlinked file only if it is still
1684                  * opened or a link still exists */
1685                 la->la_valid = LA_CTIME;
1686                 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1687                 if (rc)
1688                         GOTO(cleanup, rc);
1689         }
1690
1691         /* XXX: this transfer to ma will be removed with LOD/OSP */
1692         ma->ma_attr = *cattr;
1693         ma->ma_valid |= MA_INODE;
1694         rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1695         if (rc != 0)
1696                 GOTO(cleanup, rc);
1697
1698         /* fetch updated nlink */
1699         rc = mdd_la_get(env, mdd_cobj, cattr);
1700         /* if object is removed then we can't get its attrs,
1701          * use last get */
1702         if (rc == -ENOENT) {
1703                 cattr->la_nlink = 0;
1704                 rc = 0;
1705         }
1706
1707         if (cattr->la_nlink == 0) {
1708                 ma->ma_attr = *cattr;
1709                 ma->ma_valid |= MA_INODE;
1710         }
1711
1712         EXIT;
1713 cleanup:
1714         if (likely(mdd_cobj != NULL))
1715                 mdd_write_unlock(env, mdd_cobj);
1716
1717         if (rc == 0) {
1718                 if (cattr->la_nlink == 0)
1719                         cl_flags |= CLF_UNLINK_LAST;
1720                 else
1721                         cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1722
1723                 rc = mdd_changelog_ns_store(env, mdd,
1724                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1725                         mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1726                         handle);
1727         }
1728
1729 stop:
1730         rc = mdd_trans_stop(env, mdd, rc, handle);
1731
1732         return rc;
1733 }
1734
1735 /*
1736  * The permission has been checked when obj created, no need check again.
1737  */
1738 static int mdd_cd_sanity_check(const struct lu_env *env,
1739                                struct mdd_object *obj)
1740 {
1741         ENTRY;
1742
1743         /* EEXIST check */
1744         if (!obj || mdd_is_dead_obj(obj))
1745                 RETURN(-ENOENT);
1746
1747         RETURN(0);
1748 }
1749
1750 static int mdd_create_data(const struct lu_env *env,
1751                            struct md_object *pobj,
1752                            struct md_object *cobj,
1753                            const struct md_op_spec *spec,
1754                            struct md_attr *ma)
1755 {
1756         struct mdd_device *mdd = mdo2mdd(cobj);
1757         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1758         struct mdd_object *son = md2mdd_obj(cobj);
1759         struct thandle    *handle;
1760         const struct lu_buf *buf;
1761         struct lu_attr    *attr = MDD_ENV_VAR(env, cattr);
1762         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1763         int                rc;
1764         ENTRY;
1765
1766         rc = mdd_cd_sanity_check(env, son);
1767         if (rc)
1768                 RETURN(rc);
1769
1770         if (!md_should_create(spec->sp_cr_flags))
1771                 RETURN(0);
1772
1773         /*
1774          * there are following use cases for this function:
1775          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1776          *    striping can be specified or not
1777          * 2) CMD?
1778          */
1779         rc = mdd_la_get(env, son, attr);
1780         if (rc)
1781                 RETURN(rc);
1782
1783         /* calling ->ah_make_hint() is used to transfer information from parent */
1784         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1785
1786         handle = mdd_trans_create(env, mdd);
1787         if (IS_ERR(handle))
1788                 GOTO(out_free, rc = PTR_ERR(handle));
1789
1790         /*
1791          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1792          * Should this be fixed?
1793          */
1794         CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1795                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1796                spec->sp_cr_flags, spec->no_create);
1797
1798         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1799                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1800                                         spec->u.sp_ea.eadatalen);
1801         } else {
1802                 buf = &LU_BUF_NULL;
1803         }
1804
1805         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1806                                   XATTR_NAME_LOV, 0, handle);
1807         if (rc)
1808                 GOTO(stop, rc);
1809
1810         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1811         if (rc)
1812                 GOTO(stop, rc);
1813
1814         rc = mdd_trans_start(env, mdd, handle);
1815         if (rc)
1816                 GOTO(stop, rc);
1817
1818         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1819                           0, handle);
1820
1821         if (rc)
1822                 GOTO(stop, rc);
1823
1824         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1825
1826 stop:
1827         rc = mdd_trans_stop(env, mdd, rc, handle);
1828
1829 out_free:
1830         RETURN(rc);
1831 }
1832
1833 static int mdd_declare_object_initialize(const struct lu_env *env,
1834                                          struct mdd_object *parent,
1835                                          struct mdd_object *child,
1836                                          const struct lu_attr *attr,
1837                                          struct thandle *handle)
1838 {
1839         int rc;
1840         ENTRY;
1841
1842         LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1843         if (!S_ISDIR(attr->la_mode))
1844                 RETURN(0);
1845
1846         rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1847                                       dot, handle);
1848         if (rc != 0)
1849                 RETURN(rc);
1850
1851         rc = mdo_declare_ref_add(env, child, handle);
1852         if (rc != 0)
1853                 RETURN(rc);
1854
1855         rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1856                                       dotdot, handle);
1857
1858         RETURN(rc);
1859 }
1860
1861 static int mdd_object_initialize(const struct lu_env *env,
1862                                  const struct lu_fid *pfid,
1863                                  struct mdd_object *child,
1864                                  struct lu_attr *attr, struct thandle *handle,
1865                                  const struct md_op_spec *spec)
1866 {
1867         int rc = 0;
1868         ENTRY;
1869
1870         if (S_ISDIR(attr->la_mode)) {
1871                 /* Add "." and ".." for newly created dir */
1872                 mdo_ref_add(env, child, handle);
1873                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1874                                              S_IFDIR, dot, handle);
1875                 if (rc == 0)
1876                         rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1877                                                      dotdot, handle);
1878                 if (rc != 0)
1879                         mdo_ref_del(env, child, handle);
1880         }
1881
1882         RETURN(rc);
1883 }
1884
1885 /**
1886  * This function checks whether it can create a file/dir under the
1887  * directory(@pobj). The directory(@pobj) is not being locked by
1888  * mdd lock.
1889  *
1890  * \param[in] env       execution environment
1891  * \param[in] pobj      the directory to create files
1892  * \param[in] pattr     the attributes of the directory
1893  * \param[in] lname     the name of the created file/dir
1894  * \param[in] cattr     the attributes of the file/dir
1895  * \param[in] spec      create specification
1896  *
1897  * \retval              = 0 it is allowed to create file/dir under
1898  *                      the directory
1899  * \retval              negative error not allowed to create file/dir
1900  *                      under the directory
1901  */
1902 static int mdd_create_sanity_check(const struct lu_env *env,
1903                                    struct md_object *pobj,
1904                                    const struct lu_attr *pattr,
1905                                    const struct lu_name *lname,
1906                                    struct lu_attr *cattr,
1907                                    struct md_op_spec *spec)
1908 {
1909         struct mdd_thread_info *info = mdd_env_info(env);
1910         struct lu_fid     *fid       = &info->mti_fid;
1911         struct mdd_object *obj       = md2mdd_obj(pobj);
1912         struct mdd_device *m         = mdo2mdd(pobj);
1913         bool            check_perm = true;
1914         int rc;
1915         ENTRY;
1916
1917         /* EEXIST check */
1918         if (mdd_is_dead_obj(obj))
1919                 RETURN(-ENOENT);
1920
1921         /*
1922          * In some cases this lookup is not needed - we know before if name
1923          * exists or not because MDT performs lookup for it.
1924          * name length check is done in lookup.
1925          */
1926         if (spec->sp_cr_lookup) {
1927                 /*
1928                  * Check if the name already exist, though it will be checked in
1929                  * _index_insert also, for avoiding rolling back if exists
1930                  * _index_insert.
1931                  */
1932                 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1933                                   MAY_WRITE | MAY_EXEC);
1934                 if (rc != -ENOENT)
1935                         RETURN(rc ? : -EEXIST);
1936
1937                 /* Permission is already being checked in mdd_lookup */
1938                 check_perm = false;
1939         }
1940
1941         rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1942         if (rc != 0)
1943                 RETURN(rc);
1944
1945         /* sgid check */
1946         if (pattr->la_mode & S_ISGID) {
1947                 cattr->la_gid = pattr->la_gid;
1948                 if (S_ISDIR(cattr->la_mode)) {
1949                         cattr->la_mode |= S_ISGID;
1950                         cattr->la_valid |= LA_MODE;
1951                 }
1952         }
1953
1954         /* Inherit project ID from parent directory */
1955         if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
1956                 cattr->la_projid = pattr->la_projid;
1957                 if (S_ISDIR(cattr->la_mode)) {
1958                         cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
1959                         cattr->la_valid |= LA_FLAGS;
1960                 }
1961                 cattr->la_valid |= LA_PROJID;
1962         }
1963
1964         rc = mdd_name_check(m, lname);
1965         if (rc < 0)
1966                 RETURN(rc);
1967
1968         switch (cattr->la_mode & S_IFMT) {
1969         case S_IFLNK: {
1970                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1971
1972                 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
1973                         RETURN(-ENAMETOOLONG);
1974                 else
1975                         RETURN(0);
1976         }
1977         case S_IFDIR:
1978         case S_IFREG:
1979         case S_IFCHR:
1980         case S_IFBLK:
1981         case S_IFIFO:
1982         case S_IFSOCK:
1983                 rc = 0;
1984                 break;
1985         default:
1986                 rc = -EINVAL;
1987                 break;
1988         }
1989         RETURN(rc);
1990 }
1991
1992 static int mdd_declare_create_object(const struct lu_env *env,
1993                                      struct mdd_device *mdd,
1994                                      struct mdd_object *p, struct mdd_object *c,
1995                                      struct lu_attr *attr,
1996                                      struct thandle *handle,
1997                                      const struct md_op_spec *spec,
1998                                      struct lu_buf *def_acl_buf,
1999                                      struct lu_buf *acl_buf,
2000                                      struct dt_allocation_hint *hint)
2001 {
2002         const struct lu_buf *buf;
2003         int rc;
2004
2005         rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2006                                                 hint);
2007         if (rc)
2008                 GOTO(out, rc);
2009
2010 #ifdef CONFIG_FS_POSIX_ACL
2011         if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2012                 /* if dir, then can inherit default ACl */
2013                 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2014                                            XATTR_NAME_ACL_DEFAULT,
2015                                            0, handle);
2016                 if (rc)
2017                         GOTO(out, rc);
2018         }
2019
2020         if (acl_buf->lb_len > 0) {
2021                 rc = mdo_declare_attr_set(env, c, attr, handle);
2022                 if (rc)
2023                         GOTO(out, rc);
2024
2025                 rc = mdo_declare_xattr_set(env, c, acl_buf,
2026                                            XATTR_NAME_ACL_ACCESS, 0, handle);
2027                 if (rc)
2028                         GOTO(out, rc);
2029         }
2030 #endif
2031         rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2032         if (rc)
2033                 GOTO(out, rc);
2034
2035         /* replay case, create LOV EA from client data */
2036         if (spec->no_create ||
2037             (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2038                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2039                                         spec->u.sp_ea.eadatalen);
2040                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2041                                            handle);
2042                 if (rc)
2043                         GOTO(out, rc);
2044         }
2045
2046         if (S_ISLNK(attr->la_mode)) {
2047                 const char *target_name = spec->u.sp_symname;
2048                 int sym_len = strlen(target_name);
2049                 const struct lu_buf *buf;
2050
2051                 buf = mdd_buf_get_const(env, target_name, sym_len);
2052                 rc = dt_declare_record_write(env, mdd_object_child(c),
2053                                              buf, 0, handle);
2054                 if (rc)
2055                         GOTO(out, rc);
2056         }
2057
2058         if (spec->sp_cr_file_secctx_name != NULL) {
2059                 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2060                                         spec->sp_cr_file_secctx_size);
2061                 rc = mdo_declare_xattr_set(env, c, buf,
2062                                            spec->sp_cr_file_secctx_name, 0,
2063                                            handle);
2064                 if (rc < 0)
2065                         GOTO(out, rc);
2066         }
2067 out:
2068         return rc;
2069 }
2070
2071 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2072                               struct mdd_object *p, struct mdd_object *c,
2073                               const struct lu_name *name,
2074                               struct lu_attr *attr,
2075                               struct thandle *handle,
2076                               const struct md_op_spec *spec,
2077                               struct linkea_data *ldata,
2078                               struct lu_buf *def_acl_buf,
2079                               struct lu_buf *acl_buf,
2080                               struct dt_allocation_hint *hint)
2081 {
2082         int rc;
2083
2084         rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2085                                        def_acl_buf, acl_buf, hint);
2086         if (rc)
2087                 GOTO(out, rc);
2088
2089         if (S_ISDIR(attr->la_mode)) {
2090                 rc = mdo_declare_ref_add(env, p, handle);
2091                 if (rc)
2092                         GOTO(out, rc);
2093         }
2094
2095         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2096                 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2097                 if (rc)
2098                         GOTO(out, rc);
2099         } else {
2100                 struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
2101
2102                 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2103                                               name->ln_name, handle);
2104                 if (rc != 0)
2105                         return rc;
2106
2107                 rc = mdd_declare_links_add(env, c, handle, ldata);
2108                 if (rc)
2109                         return rc;
2110
2111                 *la = *attr;
2112                 la->la_valid = LA_CTIME | LA_MTIME;
2113                 rc = mdo_declare_attr_set(env, p, la, handle);
2114                 if (rc)
2115                         return rc;
2116
2117                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2118                 if (rc)
2119                         return rc;
2120         }
2121 out:
2122         return rc;
2123 }
2124
2125 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2126                         struct lu_attr *la, struct lu_buf *def_acl_buf,
2127                         struct lu_buf *acl_buf)
2128 {
2129         int     rc;
2130         ENTRY;
2131
2132         if (S_ISLNK(la->la_mode)) {
2133                 acl_buf->lb_len = 0;
2134                 def_acl_buf->lb_len = 0;
2135                 RETURN(0);
2136         }
2137
2138         mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2139         rc = mdo_xattr_get(env, pobj, def_acl_buf,
2140                            XATTR_NAME_ACL_DEFAULT);
2141         mdd_read_unlock(env, pobj);
2142         if (rc > 0) {
2143                 /* If there are default ACL, fix mode/ACL by default ACL */
2144                 def_acl_buf->lb_len = rc;
2145                 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2146                 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2147                 acl_buf->lb_len = rc;
2148                 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2149                 if (rc < 0)
2150                         RETURN(rc);
2151         } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2152                 /* If there are no default ACL, fix mode by mask */
2153                 struct lu_ucred *uc = lu_ucred(env);
2154
2155                 /* The create triggered by MDT internal events, such as
2156                  * LFSCK reset, will not contain valid "uc". */
2157                 if (unlikely(uc != NULL))
2158                         la->la_mode &= ~uc->uc_umask;
2159                 rc = 0;
2160                 acl_buf->lb_len = 0;
2161                 def_acl_buf->lb_len = 0;
2162         }
2163
2164         RETURN(rc);
2165 }
2166
2167 /**
2168  * Create a metadata object and initialize it, set acl, xattr.
2169  **/
2170 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2171                              struct mdd_object *son, struct lu_attr *attr,
2172                              struct md_op_spec *spec, struct lu_buf *acl_buf,
2173                              struct lu_buf *def_acl_buf,
2174                              struct dt_allocation_hint *hint,
2175                              struct thandle *handle)
2176 {
2177         const struct lu_buf *buf;
2178         int rc;
2179
2180         mdd_write_lock(env, son, MOR_TGT_CHILD);
2181         rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2182                                         hint);
2183         if (rc)
2184                 GOTO(unlock, rc);
2185
2186         /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2187          * created in declare phase, they also needs to be added to master
2188          * object as sub-directory entry. So it has to initialize the master
2189          * object, then set dir striped EA.(in mdo_xattr_set) */
2190         rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2191                                    spec);
2192         if (rc != 0)
2193                 GOTO(err_destroy, rc);
2194
2195         /*
2196          * in case of replay we just set LOVEA provided by the client
2197          * XXX: I think it would be interesting to try "old" way where
2198          *      MDT calls this xattr_set(LOV) in a different transaction.
2199          *      probably this way we code can be made better.
2200          */
2201
2202         /* During creation, there are only a few cases we need do xattr_set to
2203          * create stripes.
2204          * 1. regular file: see comments above.
2205          * 2. dir: inherit default striping or pool settings from parent.
2206          * 3. create striped directory with provided stripeEA.
2207          * 4. create striped directory because inherit default layout from the
2208          * parent.
2209          */
2210         if (spec->no_create ||
2211             (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2212             S_ISDIR(attr->la_mode)) {
2213                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2214                                         spec->u.sp_ea.eadatalen);
2215                 rc = mdo_xattr_set(env, son, buf,
2216                                    S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2217                                                             XATTR_NAME_LOV, 0,
2218                                    handle);
2219                 if (rc != 0)
2220                         GOTO(err_destroy, rc);
2221         }
2222
2223 #ifdef CONFIG_FS_POSIX_ACL
2224         if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2225             S_ISDIR(attr->la_mode)) {
2226                 /* set default acl */
2227                 rc = mdo_xattr_set(env, son, def_acl_buf,
2228                                    XATTR_NAME_ACL_DEFAULT, 0,
2229                                    handle);
2230                 if (rc)
2231                         GOTO(err_destroy, rc);
2232         }
2233         /* set its own acl */
2234         if (acl_buf != NULL && acl_buf->lb_len > 0) {
2235                 rc = mdo_xattr_set(env, son, acl_buf,
2236                                    XATTR_NAME_ACL_ACCESS,
2237                                    0, handle);
2238                 if (rc)
2239                         GOTO(err_destroy, rc);
2240         }
2241 #endif
2242
2243         if (S_ISLNK(attr->la_mode)) {
2244                 struct lu_ucred  *uc = lu_ucred_assert(env);
2245                 struct dt_object *dt = mdd_object_child(son);
2246                 const char *target_name = spec->u.sp_symname;
2247                 int sym_len = strlen(target_name);
2248                 loff_t pos = 0;
2249
2250                 buf = mdd_buf_get_const(env, target_name, sym_len);
2251                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2252                                                 uc->uc_cap &
2253                                                 CFS_CAP_SYS_RESOURCE_MASK);
2254
2255                 if (rc == sym_len)
2256                         rc = 0;
2257                 else
2258                         GOTO(err_initlized, rc = -EFAULT);
2259         }
2260
2261         if (spec->sp_cr_file_secctx_name != NULL) {
2262                 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2263                                         spec->sp_cr_file_secctx_size);
2264                 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2265                                    0, handle);
2266                 if (rc < 0)
2267                         GOTO(err_initlized, rc);
2268         }
2269
2270 err_initlized:
2271         if (unlikely(rc != 0)) {
2272                 int rc2;
2273                 if (S_ISDIR(attr->la_mode)) {
2274                         /* Drop the reference, no need to delete "."/"..",
2275                          * because the object to be destroied directly. */
2276                         rc2 = mdo_ref_del(env, son, handle);
2277                         if (rc2 != 0)
2278                                 GOTO(unlock, rc);
2279                 }
2280                 rc2 = mdo_ref_del(env, son, handle);
2281                 if (rc2 != 0)
2282                         GOTO(unlock, rc);
2283 err_destroy:
2284                 mdo_destroy(env, son, handle);
2285         }
2286 unlock:
2287         mdd_write_unlock(env, son);
2288         RETURN(rc);
2289 }
2290
2291 static int mdd_index_delete(const struct lu_env *env,
2292                             struct mdd_object *mdd_pobj,
2293                             struct lu_attr *cattr,
2294                             const struct lu_name *lname)
2295 {
2296         struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2297         struct thandle *handle;
2298         int rc;
2299         ENTRY;
2300
2301         handle = mdd_trans_create(env, mdd);
2302         if (IS_ERR(handle))
2303                 RETURN(PTR_ERR(handle));
2304
2305         rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2306                                       handle);
2307         if (rc != 0)
2308                 GOTO(stop, rc);
2309
2310         if (S_ISDIR(cattr->la_mode)) {
2311                 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2312                 if (rc != 0)
2313                         GOTO(stop, rc);
2314         }
2315
2316         /* Since this will only be used in the error handler path,
2317          * Let's set the thandle to be local and not mess the transno */
2318         handle->th_local = 1;
2319         rc = mdd_trans_start(env, mdd, handle);
2320         if (rc)
2321                 GOTO(stop, rc);
2322
2323         rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2324                                 S_ISDIR(cattr->la_mode), handle);
2325         if (rc)
2326                 GOTO(stop, rc);
2327 stop:
2328         rc = mdd_trans_stop(env, mdd, rc, handle);
2329
2330         RETURN(rc);
2331 }
2332
2333 /**
2334  * Create object and insert it into namespace.
2335  *
2336  * Two operations have to be performed:
2337  *
2338  *  - an allocation of a new object (->do_create()), and
2339  *  - an insertion into a parent index (->dio_insert()).
2340  *
2341  * Due to locking, operation order is not important, when both are
2342  * successful, *but* error handling cases are quite different:
2343  *
2344  *  - if insertion is done first, and following object creation fails,
2345  *  insertion has to be rolled back, but this operation might fail
2346  *  also leaving us with dangling index entry.
2347  *
2348  *  - if creation is done first, is has to be undone if insertion fails,
2349  *  leaving us with leaked space, which is not good but not fatal.
2350  *
2351  * It seems that creation-first is simplest solution, but it is sub-optimal
2352  * in the frequent
2353  *
2354  * $ mkdir foo
2355  * $ mkdir foo
2356  *
2357  * case, because second mkdir is bound to create object, only to
2358  * destroy it immediately.
2359  *
2360  * To avoid this follow local file systems that do double lookup:
2361  *
2362  * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2363  * 1. create            (mdd_create_object_internal())
2364  * 2. insert            (__mdd_index_insert(), lookup again)
2365  *
2366  * \param[in] pobj      parent object
2367  * \param[in] lname     name of child being created
2368  * \param[in,out] child child object being created
2369  * \param[in] spec      additional create parameters
2370  * \param[in] ma        attributes for new child object
2371  *
2372  * \retval              0 on success
2373  * \retval              negative errno on failure
2374  */
2375 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2376                       const struct lu_name *lname, struct md_object *child,
2377                       struct md_op_spec *spec, struct md_attr *ma)
2378 {
2379         struct mdd_thread_info  *info = mdd_env_info(env);
2380         struct lu_attr          *la = &info->mti_la_for_fix;
2381         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
2382         struct mdd_object       *son = md2mdd_obj(child);
2383         struct mdd_device       *mdd = mdo2mdd(pobj);
2384         struct lu_attr          *attr = &ma->ma_attr;
2385         struct thandle          *handle;
2386         struct lu_attr          *pattr = &info->mti_pattr;
2387         struct lu_buf           acl_buf;
2388         struct lu_buf           def_acl_buf;
2389         struct linkea_data      *ldata = &info->mti_link_data;
2390         const char              *name = lname->ln_name;
2391         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2392         int                      rc;
2393         int                      rc2;
2394         ENTRY;
2395
2396         rc = mdd_la_get(env, mdd_pobj, pattr);
2397         if (rc != 0)
2398                 RETURN(rc);
2399
2400         /* Sanity checks before big job. */
2401         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2402         if (rc)
2403                 RETURN(rc);
2404
2405         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2406                 GOTO(out_free, rc = -EINPROGRESS);
2407
2408         handle = mdd_trans_create(env, mdd);
2409         if (IS_ERR(handle))
2410                 GOTO(out_free, rc = PTR_ERR(handle));
2411
2412         lu_buf_check_and_alloc(&info->mti_xattr_buf,
2413                                mdd->mdd_dt_conf.ddp_max_ea_size);
2414         acl_buf = info->mti_xattr_buf;
2415         def_acl_buf.lb_buf = info->mti_key;
2416         def_acl_buf.lb_len = sizeof(info->mti_key);
2417         rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2418         if (rc < 0)
2419                 GOTO(out_stop, rc);
2420
2421         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2422
2423         memset(ldata, 0, sizeof(*ldata));
2424         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2425                 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2426
2427                 tfid.f_oid--;
2428                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2429                                         &tfid, lname, 1, 0, ldata);
2430         } else {
2431                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2432                                         mdd_object_fid(mdd_pobj),
2433                                         lname, 1, 0, ldata);
2434         }
2435
2436         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2437                                 handle, spec, ldata, &def_acl_buf, &acl_buf,
2438                                 hint);
2439         if (rc)
2440                 GOTO(out_stop, rc);
2441
2442         rc = mdd_trans_start(env, mdd, handle);
2443         if (rc)
2444                 GOTO(out_stop, rc);
2445
2446         rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2447                                &def_acl_buf, hint, handle);
2448         if (rc != 0)
2449                 GOTO(out_stop, rc);
2450
2451         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2452                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2453                 son->mod_flags |= VOLATILE_OBJ;
2454                 rc = __mdd_orphan_add(env, son, handle);
2455                 GOTO(out_volatile, rc);
2456         } else {
2457                 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2458                                         attr->la_mode, name, handle);
2459                 if (rc != 0)
2460                         GOTO(err_created, rc);
2461
2462                 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2463                               ldata, 1);
2464
2465                 /* update parent directory mtime/ctime */
2466                 *la = *attr;
2467                 la->la_valid = LA_CTIME | LA_MTIME;
2468                 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2469                 if (rc)
2470                         GOTO(err_insert, rc);
2471         }
2472
2473         EXIT;
2474 err_insert:
2475         if (rc != 0) {
2476                 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2477                         rc2 = __mdd_orphan_del(env, son, handle);
2478                 else
2479                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
2480                                                  S_ISDIR(attr->la_mode),
2481                                                  handle);
2482                 if (rc2 != 0)
2483                         goto out_stop;
2484
2485 err_created:
2486                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2487                 if (S_ISDIR(attr->la_mode)) {
2488                         /* Drop the reference, no need to delete "."/"..",
2489                          * because the object is to be destroyed directly. */
2490                         rc2 = mdo_ref_del(env, son, handle);
2491                         if (rc2 != 0) {
2492                                 mdd_write_unlock(env, son);
2493                                 goto out_stop;
2494                         }
2495                 }
2496 out_volatile:
2497                 /* For volatile files drop one link immediately, since there is
2498                  * no filename in the namespace, and save any error returned. */
2499                 rc2 = mdo_ref_del(env, son, handle);
2500                 if (rc2 != 0) {
2501                         mdd_write_unlock(env, son);
2502                         if (unlikely(rc == 0))
2503                                 rc = rc2;
2504                         goto out_stop;
2505                 }
2506
2507                 /* Don't destroy the volatile object on success */
2508                 if (likely(rc != 0))
2509                         mdo_destroy(env, son, handle);
2510                 mdd_write_unlock(env, son);
2511         }
2512
2513         if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2514             likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2515                 rc = mdd_changelog_ns_store(env, mdd,
2516                                 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2517                                 S_ISREG(attr->la_mode) ? CL_CREATE :
2518                                 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2519                                 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2520                                 NULL, handle);
2521 out_stop:
2522         rc2 = mdd_trans_stop(env, mdd, rc, handle);
2523         if (rc == 0) {
2524                 /* If creation fails, it is most likely due to the remote update
2525                  * failure, because local transaction will mostly succeed at
2526                  * this stage. There is no easy way to rollback all of previous
2527                  * updates, so let's remove the object from namespace, and
2528                  * LFSCK should handle the orphan object. */
2529                 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2530                         mdd_index_delete(env, mdd_pobj, attr, lname);
2531                 rc = rc2;
2532         }
2533 out_free:
2534         if (is_vmalloc_addr(ldata->ld_buf))
2535                 /* if we vmalloced a large buffer drop it */
2536                 lu_buf_free(ldata->ld_buf);
2537
2538         /* The child object shouldn't be cached anymore */
2539         if (rc)
2540                 set_bit(LU_OBJECT_HEARD_BANSHEE,
2541                         &child->mo_lu.lo_header->loh_flags);
2542         return rc;
2543 }
2544
2545 /*
2546  * Get locks on parents in proper order
2547  * RETURN: < 0 - error, rename_order if successful
2548  */
2549 enum rename_order {
2550         MDD_RN_SAME,
2551         MDD_RN_SRCTGT,
2552         MDD_RN_TGTSRC
2553 };
2554
2555 static int mdd_rename_order(const struct lu_env *env,
2556                             struct mdd_device *mdd,
2557                             struct mdd_object *src_pobj,
2558                             const struct lu_attr *pattr,
2559                             struct mdd_object *tgt_pobj)
2560 {
2561         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2562         int rc;
2563         ENTRY;
2564
2565         if (src_pobj == tgt_pobj)
2566                 RETURN(MDD_RN_SAME);
2567
2568         /* compared the parent child relationship of src_p&tgt_p */
2569         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2570                 rc = MDD_RN_SRCTGT;
2571         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2572                 rc = MDD_RN_TGTSRC;
2573         } else {
2574                 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2575                                    NULL);
2576                 if (rc == -EREMOTE)
2577                         rc = 0;
2578
2579                 if (rc == 1)
2580                         rc = MDD_RN_TGTSRC;
2581                 else if (rc == 0)
2582                         rc = MDD_RN_SRCTGT;
2583         }
2584
2585         RETURN(rc);
2586 }
2587
2588 /* has not mdd_write{read}_lock on any obj yet. */
2589 static int mdd_rename_sanity_check(const struct lu_env *env,
2590                                    struct mdd_object *src_pobj,
2591                                    const struct lu_attr *pattr,
2592                                    struct mdd_object *tgt_pobj,
2593                                    const struct lu_attr *tpattr,
2594                                    struct mdd_object *sobj,
2595                                    const struct lu_attr *cattr,
2596                                    struct mdd_object *tobj,
2597                                    const struct lu_attr *tattr)
2598 {
2599         int rc = 0;
2600         ENTRY;
2601
2602         /* XXX: when get here, sobj must NOT be NULL,
2603          * the other case has been processed in cld_rename
2604          * before mdd_rename and enable MDS_PERM_BYPASS. */
2605         LASSERT(sobj);
2606
2607         /*
2608          * If we are using project inheritance, we only allow renames
2609          * into our tree when the project IDs are the same; otherwise
2610          * tree quota mechanism would be circumvented.
2611          */
2612         if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2613             tpattr->la_projid != cattr->la_projid) ||
2614             ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2615             (pattr->la_projid != tpattr->la_projid)))
2616                 RETURN(-EXDEV);
2617
2618         rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2619         if (rc)
2620                 RETURN(rc);
2621
2622         /* XXX: when get here, "tobj == NULL" means tobj must
2623          * NOT exist (neither on remote MDS, such case has been
2624          * processed in cld_rename before mdd_rename and enable
2625          * MDS_PERM_BYPASS).
2626          * So check may_create, but not check may_unlink. */
2627         if (tobj == NULL)
2628                 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2629                                     (src_pobj != tgt_pobj));
2630         else
2631                 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2632                                     (src_pobj != tgt_pobj), 1);
2633
2634         if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2635                 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2636
2637         RETURN(rc);
2638 }
2639
2640 static int mdd_declare_rename(const struct lu_env *env,
2641                               struct mdd_device *mdd,
2642                               struct mdd_object *mdd_spobj,
2643                               struct mdd_object *mdd_tpobj,
2644                               struct mdd_object *mdd_sobj,
2645                               struct mdd_object *mdd_tobj,
2646                               const struct lu_name *sname,
2647                               const struct lu_name *tname,
2648                               struct md_attr *ma,
2649                               struct linkea_data *ldata,
2650                               struct thandle *handle)
2651 {
2652         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2653         int rc;
2654
2655         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2656         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2657
2658         LASSERT(mdd_spobj);
2659         LASSERT(mdd_tpobj);
2660         LASSERT(mdd_sobj);
2661
2662         /* name from source dir */
2663         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2664         if (rc)
2665                 return rc;
2666
2667         /* .. from source child */
2668         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2669                 /* source child can be directory,
2670                  * counted by source dir's nlink */
2671                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2672                 if (rc)
2673                         return rc;
2674                 if (mdd_spobj != mdd_tpobj) {
2675                         rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2676                                                       handle);
2677                         if (rc != 0)
2678                                 return rc;
2679
2680                         rc = mdo_declare_index_insert(env, mdd_sobj,
2681                                                       mdo2fid(mdd_tpobj),
2682                                                       S_IFDIR, dotdot, handle);
2683                         if (rc != 0)
2684                                 return rc;
2685                 }
2686
2687                 /* new target child can be directory,
2688                  * counted by target dir's nlink */
2689                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2690                 if (rc != 0)
2691                         return rc;
2692         }
2693
2694         la->la_valid = LA_CTIME | LA_MTIME;
2695         rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2696         if (rc != 0)
2697                 return rc;
2698
2699         rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2700         if (rc != 0)
2701                 return rc;
2702
2703         la->la_valid = LA_CTIME;
2704         rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2705         if (rc)
2706                 return rc;
2707
2708         rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2709         if (rc)
2710                 return rc;
2711
2712         /* new name */
2713         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2714                                       mdd_object_type(mdd_sobj),
2715                                       tname->ln_name, handle);
2716         if (rc != 0)
2717                 return rc;
2718
2719         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2720                 /* delete target child in target parent directory */
2721                 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2722                                               handle);
2723                 if (rc)
2724                         return rc;
2725
2726                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2727                 if (rc)
2728                         return rc;
2729
2730                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2731                         /* target child can be directory,
2732                          * delete "." reference in target child directory */
2733                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2734                         if (rc)
2735                                 return rc;
2736
2737                         /* delete ".." reference in target parent directory */
2738                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2739                         if (rc)
2740                                 return rc;
2741                 }
2742
2743                 la->la_valid = LA_CTIME;
2744                 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2745                 if (rc)
2746                         return rc;
2747
2748                 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2749                 if (rc)
2750                         return rc;
2751         }
2752
2753         rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2754         if (rc)
2755                 return rc;
2756
2757         return rc;
2758 }
2759
2760 /* src object can be remote that is why we use only fid and type of object */
2761 static int mdd_rename(const struct lu_env *env,
2762                       struct md_object *src_pobj, struct md_object *tgt_pobj,
2763                       const struct lu_fid *lf, const struct lu_name *lsname,
2764                       struct md_object *tobj, const struct lu_name *ltname,
2765                       struct md_attr *ma)
2766 {
2767         const char *sname = lsname->ln_name;
2768         const char *tname = ltname->ln_name;
2769         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2770         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2771         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2772         struct mdd_device *mdd = mdo2mdd(src_pobj);
2773         struct mdd_object *mdd_sobj = NULL;                  /* source object */
2774         struct mdd_object *mdd_tobj = NULL;
2775         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2776         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2777         struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2778         struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2779         struct thandle *handle;
2780         struct linkea_data  *ldata = &mdd_env_info(env)->mti_link_data;
2781         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2782         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2783         bool is_dir;
2784         bool tobj_ref = 0;
2785         bool tobj_locked = 0;
2786         unsigned cl_flags = 0;
2787         int rc, rc2;
2788         ENTRY;
2789
2790         if (tobj)
2791                 mdd_tobj = md2mdd_obj(tobj);
2792
2793         mdd_sobj = mdd_object_find(env, mdd, lf);
2794         if (IS_ERR(mdd_sobj))
2795                 RETURN(PTR_ERR(mdd_sobj));
2796
2797         rc = mdd_la_get(env, mdd_sobj, cattr);
2798         if (rc)
2799                 GOTO(out_pending, rc);
2800
2801         rc = mdd_la_get(env, mdd_spobj, pattr);
2802         if (rc)
2803                 GOTO(out_pending, rc);
2804
2805         if (mdd_tobj) {
2806                 rc = mdd_la_get(env, mdd_tobj, tattr);
2807                 if (rc)
2808                         GOTO(out_pending, rc);
2809                 /* search for an existing archive.
2810                  * we should check ahead as the object
2811                  * can be destroyed in this transaction */
2812                 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2813                         cl_flags |= CLF_RENAME_LAST_EXISTS;
2814         }
2815
2816         rc = mdd_la_get(env, mdd_tpobj, tpattr);
2817         if (rc)
2818                 GOTO(out_pending, rc);
2819
2820         rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2821                                      mdd_sobj, cattr, mdd_tobj, tattr);
2822         if (rc)
2823                 GOTO(out_pending, rc);
2824
2825         rc = mdd_name_check(mdd, ltname);
2826         if (rc < 0)
2827                 GOTO(out_pending, rc);
2828
2829         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2830         rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2831         if (rc < 0)
2832                 GOTO(out_pending, rc);
2833
2834         handle = mdd_trans_create(env, mdd);
2835         if (IS_ERR(handle))
2836                 GOTO(out_pending, rc = PTR_ERR(handle));
2837
2838         memset(ldata, 0, sizeof(*ldata));
2839         rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
2840                                 lsname, mdd_object_fid(mdd_tpobj), ltname,
2841                                 1, 0, ldata);
2842         if (rc)
2843                 GOTO(stop, rc);
2844
2845         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2846                                 mdd_tobj, lsname, ltname, ma, ldata, handle);
2847         if (rc)
2848                 GOTO(stop, rc);
2849
2850         rc = mdd_trans_start(env, mdd, handle);
2851         if (rc)
2852                 GOTO(stop, rc);
2853
2854         is_dir = S_ISDIR(cattr->la_mode);
2855
2856         /* Remove source name from source directory */
2857         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2858         if (rc != 0)
2859                 GOTO(stop, rc);
2860
2861         /* "mv dir1 dir2" needs "dir1/.." link update */
2862         if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2863                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2864                 if (rc != 0)
2865                         GOTO(fixup_spobj2, rc);
2866
2867                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2868                                              dotdot, handle);
2869                 if (rc != 0)
2870                         GOTO(fixup_spobj, rc);
2871         }
2872
2873         if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2874                 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2875                 if (rc != 0)
2876                         /* tname might been renamed to something else */
2877                         GOTO(fixup_spobj, rc);
2878         }
2879
2880         /* Insert new fid with target name into target dir */
2881         rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2882                                 tname, handle);
2883         if (rc != 0)
2884                 GOTO(fixup_tpobj, rc);
2885
2886         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2887         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2888
2889         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2890         la->la_valid = LA_CTIME;
2891         rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2892         if (rc)
2893                 GOTO(fixup_tpobj, rc);
2894
2895         /* Update the linkEA for the source object */
2896         mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2897         rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2898                               mdo2fid(mdd_tpobj), ltname, handle, ldata,
2899                               0, 0);
2900         if (rc == -ENOENT)
2901                 /* Old files might not have EA entry */
2902                 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2903                               lsname, handle, NULL, 0);
2904         mdd_write_unlock(env, mdd_sobj);
2905         /* We don't fail the transaction if the link ea can't be
2906            updated -- fid2path will use alternate lookup method. */
2907         rc = 0;
2908
2909         /* Remove old target object
2910          * For tobj is remote case cmm layer has processed
2911          * and set tobj to NULL then. So when tobj is NOT NULL,
2912          * it must be local one.
2913          */
2914         if (tobj && mdd_object_exists(mdd_tobj)) {
2915                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2916                 tobj_locked = 1;
2917                 if (mdd_is_dead_obj(mdd_tobj)) {
2918                         /* shld not be dead, something is wrong */
2919                         CERROR("tobj is dead, something is wrong\n");
2920                         rc = -EINVAL;
2921                         goto cleanup;
2922                 }
2923                 mdo_ref_del(env, mdd_tobj, handle);
2924
2925                 /* Remove dot reference. */
2926                 if (S_ISDIR(tattr->la_mode))
2927                         mdo_ref_del(env, mdd_tobj, handle);
2928                 tobj_ref = 1;
2929
2930                 /* fetch updated nlink */
2931                 rc = mdd_la_get(env, mdd_tobj, tattr);
2932                 if (rc != 0) {
2933                         CERROR("%s: Failed to get nlink for tobj "
2934                                 DFID": rc = %d\n",
2935                                 mdd2obd_dev(mdd)->obd_name,
2936                                 PFID(tpobj_fid), rc);
2937                         GOTO(fixup_tpobj, rc);
2938                 }
2939
2940                 la->la_valid = LA_CTIME;
2941                 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2942                 if (rc != 0) {
2943                         CERROR("%s: Failed to set ctime for tobj "
2944                                 DFID": rc = %d\n",
2945                                 mdd2obd_dev(mdd)->obd_name,
2946                                 PFID(tpobj_fid), rc);
2947                         GOTO(fixup_tpobj, rc);
2948                 }
2949
2950                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2951                 ma->ma_attr = *tattr;
2952                 ma->ma_valid |= MA_INODE;
2953                 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2954                                        handle);
2955                 if (rc != 0) {
2956                         CERROR("%s: Failed to unlink tobj "
2957                                 DFID": rc = %d\n",
2958                                 mdd2obd_dev(mdd)->obd_name,
2959                                 PFID(tpobj_fid), rc);
2960                         GOTO(fixup_tpobj, rc);
2961                 }
2962
2963                 /* fetch updated nlink */
2964                 rc = mdd_la_get(env, mdd_tobj, tattr);
2965                 if (rc == -ENOENT) {
2966                         /* the object got removed, let's
2967                          * return the latest known attributes */
2968                         tattr->la_nlink = 0;
2969                         rc = 0;
2970                 } else if (rc != 0) {
2971                         CERROR("%s: Failed to get nlink for tobj "
2972                                 DFID": rc = %d\n",
2973                                 mdd2obd_dev(mdd)->obd_name,
2974                                 PFID(tpobj_fid), rc);
2975                         GOTO(fixup_tpobj, rc);
2976                 }
2977                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2978                 ma->ma_attr = *tattr;
2979                 ma->ma_valid |= MA_INODE;
2980
2981                 if (tattr->la_nlink == 0)
2982                         cl_flags |= CLF_RENAME_LAST;
2983                 else
2984                         cl_flags &= ~CLF_RENAME_LAST_EXISTS;
2985         }
2986
2987         la->la_valid = LA_CTIME | LA_MTIME;
2988         rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2989         if (rc)
2990                 GOTO(fixup_tpobj, rc);
2991
2992         if (mdd_spobj != mdd_tpobj) {
2993                 la->la_valid = LA_CTIME | LA_MTIME;
2994                 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2995                 if (rc != 0)
2996                         GOTO(fixup_tpobj, rc);
2997         }
2998
2999         EXIT;
3000
3001 fixup_tpobj:
3002         if (rc) {
3003                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3004                 if (rc2)
3005                         CWARN("tp obj fix error %d\n",rc2);
3006
3007                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3008                     !mdd_is_dead_obj(mdd_tobj)) {
3009                         if (tobj_ref) {
3010                                 mdo_ref_add(env, mdd_tobj, handle);
3011                                 if (is_dir)
3012                                         mdo_ref_add(env, mdd_tobj, handle);
3013                         }
3014
3015                         rc2 = __mdd_index_insert(env, mdd_tpobj,
3016                                                   mdo2fid(mdd_tobj),
3017                                                   mdd_object_type(mdd_tobj),
3018                                                   tname, handle);
3019                         if (rc2 != 0)
3020                                 CWARN("tp obj fix error: rc = %d\n", rc2);
3021                 }
3022         }
3023
3024 fixup_spobj:
3025         if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3026                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3027                 if (rc2)
3028                         CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3029                                mdd2obd_dev(mdd)->obd_name, rc2);
3030
3031
3032                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3033                                               dotdot, handle);
3034                 if (rc2 != 0)
3035                         CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3036                               mdd2obd_dev(mdd)->obd_name, rc2);
3037         }
3038
3039 fixup_spobj2:
3040         if (rc != 0) {
3041                 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3042                                          mdd_object_type(mdd_sobj), sname,
3043                                          handle);
3044                 if (rc2 != 0)
3045                         CWARN("sp obj fix error: rc = %d\n", rc2);
3046         }
3047
3048 cleanup:
3049         if (tobj_locked)
3050                 mdd_write_unlock(env, mdd_tobj);
3051
3052         if (rc == 0)
3053                 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3054                                             mdd_tobj, tpobj_fid, lf, spobj_fid,
3055                                             ltname, lsname, handle);
3056
3057 stop:
3058         rc = mdd_trans_stop(env, mdd, rc, handle);
3059
3060 out_pending:
3061         mdd_object_put(env, mdd_sobj);
3062         return rc;
3063 }
3064
3065 /**
3066  * During migration once the parent FID has been changed,
3067  * we need update the parent FID in linkea.
3068  **/
3069 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3070                                             struct mdd_object *parent,
3071                                             struct mdd_object *newparent,
3072                                             struct mdd_object *child,
3073                                             const char *name, int namelen,
3074                                             struct thandle *handle,
3075                                             bool declare)
3076 {
3077         struct mdd_thread_info  *info = mdd_env_info(env);
3078         struct linkea_data      ldata = { NULL };
3079         struct lu_buf           *buf = &info->mti_link_buf;
3080         int                     count;
3081         int                     rc = 0;
3082
3083         ENTRY;
3084
3085         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3086         if (buf->lb_buf == NULL)
3087                 RETURN(-ENOMEM);
3088
3089         ldata.ld_buf = buf;
3090         rc = mdd_links_read(env, child, &ldata);
3091         if (rc != 0) {
3092                 if (rc == -ENOENT || rc == -ENODATA)
3093                         rc = 0;
3094                 RETURN(rc);
3095         }
3096
3097         LASSERT(ldata.ld_leh != NULL);
3098         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3099         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3100                 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3101                 struct lu_name lname;
3102                 struct lu_fid  fid;
3103
3104                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3105                                     &lname, &fid);
3106
3107                 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3108                     !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3109                         ldata.ld_lee = (struct link_ea_entry *)
3110                                        ((char *)ldata.ld_lee +
3111                                         ldata.ld_reclen);
3112                         continue;
3113                 }
3114
3115                 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3116                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3117                        lname.ln_namelen, lname.ln_name,
3118                        PFID(mdd_object_fid(newparent)));
3119                 /* update to the new parent fid */
3120                 linkea_entry_pack(ldata.ld_lee, &lname,
3121                                   mdd_object_fid(newparent));
3122                 if (declare)
3123                         rc = mdd_declare_links_add(env, child, handle, &ldata);
3124                 else
3125                         rc = mdd_links_write(env, child, &ldata, handle);
3126                 break;
3127         }
3128         RETURN(rc);
3129 }
3130
3131 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3132                                            struct mdd_object *parent,
3133                                            struct mdd_object *newparent,
3134                                            struct mdd_object *child,
3135                                            const char *name, int namelen,
3136                                            struct thandle *handle)
3137 {
3138         return mdd_linkea_update_child_internal(env, parent, newparent,
3139                                                 child, name,
3140                                                 namelen, handle, true);
3141 }
3142
3143 static int mdd_linkea_update_child(const struct lu_env *env,
3144                                    struct mdd_object *parent,
3145                                    struct mdd_object *newparent,
3146                                    struct mdd_object *child,
3147                                    const char *name, int namelen,
3148                                    struct thandle *handle)
3149 {
3150         return mdd_linkea_update_child_internal(env, parent, newparent,
3151                                                 child, name,
3152                                                 namelen, handle, false);
3153 }
3154
3155 static int mdd_update_linkea_internal(const struct lu_env *env,
3156                                       struct mdd_object *mdd_pobj,
3157                                       struct mdd_object *mdd_sobj,
3158                                       struct mdd_object *mdd_tobj,
3159                                       const struct lu_name *child_name,
3160                                       struct linkea_data *ldata,
3161                                       struct thandle *handle,
3162                                       int declare)
3163 {
3164         struct mdd_thread_info  *info = mdd_env_info(env);
3165         int                     count;
3166         int                     rc = 0;
3167         ENTRY;
3168
3169         LASSERT(ldata->ld_buf != NULL);
3170         LASSERT(ldata->ld_leh != NULL);
3171
3172         /* If it is mulitple links file, we need update the name entry for
3173          * all parent */
3174         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3175         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3176                 struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3177                 struct mdd_object       *pobj;
3178                 struct lu_name          lname;
3179                 struct lu_fid           fid;
3180
3181                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3182                                     &lname, &fid);
3183                 pobj = mdd_object_find(env, mdd, &fid);
3184                 if (IS_ERR(pobj)) {
3185                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3186                               mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3187                               PTR_ERR(pobj));
3188                         continue;
3189                 }
3190
3191                 if (!mdd_object_exists(pobj)) {
3192                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3193                               mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3194                         goto next_put;
3195                 }
3196
3197                 if (pobj == mdd_pobj &&
3198                     lname.ln_namelen == child_name->ln_namelen &&
3199                     strncmp(lname.ln_name, child_name->ln_name,
3200                             lname.ln_namelen) == 0) {
3201                         CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3202                               mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3203                               PFID(&fid));
3204                         goto next_put;
3205                 }
3206
3207                 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3208                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3209                        PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3210
3211                 if (declare) {
3212                         /* Remove source name from source directory */
3213                         /* Insert new fid with target name into target dir */
3214                         rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3215                                                       handle);
3216                         if (rc != 0)
3217                                 GOTO(next_put, rc);
3218
3219                         rc = mdo_declare_index_insert(env, pobj,
3220                                         mdd_object_fid(mdd_tobj),
3221                                         mdd_object_type(mdd_tobj),
3222                                         lname.ln_name, handle);
3223                         if (rc != 0)
3224                                 GOTO(next_put, rc);
3225
3226                         rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3227                         if (rc)
3228                                 GOTO(next_put, rc);
3229
3230                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3231                         if (rc)
3232                                 GOTO(next_put, rc);
3233                 } else {
3234                         char *tmp_name = info->mti_key;
3235
3236                         if (lname.ln_namelen >= sizeof(info->mti_key)) {
3237                                 /* lnamelen is too big(> NAME_MAX + 16),
3238                                  * something wrong about this linkea, let's
3239                                  * skip it */
3240                                 CWARN("%s: the name %.*s is too long under "
3241                                       DFID"\n", mdd2obd_dev(mdd)->obd_name,
3242                                       lname.ln_namelen, lname.ln_name,
3243                                       PFID(&fid));
3244                                 goto next_put;
3245                         }
3246
3247                         /* Note: lname might be without \0 at the end, see
3248                          * linkea_entry_unpack(), let's add extra \0 by
3249                          * snprintf */
3250                         snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3251                                  lname.ln_namelen, lname.ln_name);
3252                         lname.ln_name = tmp_name;
3253
3254                         /* Let's check if this linkEA still valid, before
3255                          * it might be packed into the RPC buffer. */
3256                         rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3257                                         &info->mti_fid, NULL);
3258                         if (rc < 0 || !lu_fid_eq(&info->mti_fid,
3259                                                  mdd_object_fid(mdd_sobj)))
3260                                 GOTO(next_put, rc == -ENOENT ? 0 : rc);
3261
3262                         rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3263                         if (rc != 0)
3264                                 GOTO(next_put, rc);
3265
3266                         rc = __mdd_index_insert(env, pobj,
3267                                         mdd_object_fid(mdd_tobj),
3268                                         mdd_object_type(mdd_tobj),
3269                                         tmp_name, handle);
3270                         if (rc != 0)
3271                                 GOTO(next_put, rc);
3272
3273                         mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3274                         rc = mdo_ref_add(env, mdd_tobj, handle);
3275                         mdd_write_unlock(env, mdd_tobj);
3276                         if (rc)
3277                                 GOTO(next_put, rc);
3278
3279                         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3280                         mdo_ref_del(env, mdd_sobj, handle);
3281                         mdd_write_unlock(env, mdd_sobj);
3282                 }
3283 next_put:
3284                 mdd_object_put(env, pobj);
3285                 if (rc != 0)
3286                         break;
3287
3288                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3289                                                          ldata->ld_reclen);
3290         }
3291
3292         RETURN(rc);
3293 }
3294
3295 static int mdd_migrate_xattrs(const struct lu_env *env,
3296                               struct mdd_object *mdd_sobj,
3297                               struct mdd_object *mdd_tobj)
3298 {
3299         struct mdd_thread_info  *info = mdd_env_info(env);
3300         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3301         char                    *xname;
3302         struct thandle          *handle;
3303         struct lu_buf           xbuf;
3304         int                     xlen;
3305         int                     rem;
3306         int                     xsize;
3307         int                     list_xsize;
3308         struct lu_buf           list_xbuf;
3309         int                     rc;
3310
3311         /* retrieve xattr list from the old object */
3312         list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3313         if (list_xsize == -ENODATA)
3314                 return 0;
3315
3316         if (list_xsize < 0)
3317                 return list_xsize;
3318
3319         lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3320         if (info->mti_big_buf.lb_buf == NULL)
3321                 return -ENOMEM;
3322
3323         list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3324         list_xbuf.lb_len = list_xsize;
3325         rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3326         if (rc < 0)
3327                 return rc;
3328         rc = 0;
3329         rem = list_xsize;
3330         xname = list_xbuf.lb_buf;
3331         while (rem > 0) {
3332                 xlen = strnlen(xname, rem - 1) + 1;
3333                 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3334                     strcmp(XATTR_NAME_LMV, xname) == 0)
3335                         goto next;
3336
3337                 /* For directory, if there are default layout, migrate here */
3338                 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3339                     !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3340                         goto next;
3341
3342                 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3343                 if (xsize == -ENODATA)
3344                         goto next;
3345                 if (xsize < 0)
3346                         GOTO(out, rc);
3347
3348                 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3349                 if (info->mti_link_buf.lb_buf == NULL)
3350                         GOTO(out, rc = -ENOMEM);
3351
3352                 xbuf.lb_len = xsize;
3353                 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3354                 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3355                 if (rc == -ENODATA)
3356                         goto next;
3357                 if (rc < 0)
3358                         GOTO(out, rc);
3359
3360                 handle = mdd_trans_create(env, mdd);
3361                 if (IS_ERR(handle))
3362                         GOTO(out, rc = PTR_ERR(handle));
3363
3364                 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3365                                            handle);
3366                 if (rc != 0)
3367                         GOTO(stop_trans, rc);
3368                 /* Note: this transaction is part of migration, and it is not
3369                  * the last step of migration, so we set th_local = 1 to avoid
3370                  * update last rcvd for this transaction */
3371                 handle->th_local = 1;
3372                 rc = mdd_trans_start(env, mdd, handle);
3373                 if (rc != 0)
3374                         GOTO(stop_trans, rc);
3375
3376 again:
3377                 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3378                 if (rc == -EEXIST)
3379                         GOTO(stop_trans, rc = 0);
3380
3381                 if (unlikely(rc == -ENOSPC &&
3382                              strcmp(xname, XATTR_NAME_LINK) == 0)) {
3383                         rc = linkea_overflow_shrink(
3384                                         (struct linkea_data *)(xbuf.lb_buf));
3385                         if (likely(rc > 0)) {
3386                                 xbuf.lb_len = rc;
3387                                 goto again;
3388                         }
3389                 }
3390
3391                 if (rc != 0)
3392                         GOTO(stop_trans, rc);
3393 stop_trans:
3394                 rc = mdd_trans_stop(env, mdd, rc, handle);
3395                 if (rc != 0)
3396                         GOTO(out, rc);
3397 next:
3398                 rem -= xlen;
3399                 memmove(xname, xname + xlen, rem);
3400         }
3401 out:
3402         return rc;
3403 }
3404
3405 static int mdd_declare_migrate_create(const struct lu_env *env,
3406                                       struct mdd_object *mdd_pobj,
3407                                       struct mdd_object *mdd_sobj,
3408                                       struct mdd_object *mdd_tobj,
3409                                       struct md_op_spec *spec,
3410                                       struct lu_attr *la,
3411                                       union lmv_mds_md *mgr_ea,
3412                                       struct linkea_data *ldata,
3413                                       struct thandle *handle)
3414 {
3415         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3416         const struct lu_buf     *buf;
3417         int                     rc;
3418         int                     mgr_easize;
3419
3420         rc = mdd_declare_create_object_internal(env, mdd_pobj, mdd_tobj, la,
3421                                                 handle, spec, NULL);
3422         if (rc != 0)
3423                 return rc;
3424
3425         rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3426                                            handle);
3427         if (rc != 0)
3428                 return rc;
3429
3430         if (S_ISLNK(la->la_mode)) {
3431                 const char *target_name = spec->u.sp_symname;
3432                 int sym_len = strlen(target_name);
3433                 const struct lu_buf *buf;
3434
3435                 buf = mdd_buf_get_const(env, target_name, sym_len);
3436                 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3437                                              buf, 0, handle);
3438                 if (rc != 0)
3439                         return rc;
3440         } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3441                 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3442                 if (rc != 0)
3443                         return rc;
3444         }
3445
3446         if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3447                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3448                                         spec->u.sp_ea.eadatalen);
3449                 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3450                                            0, handle);
3451                 if (rc)
3452                         return rc;
3453         }
3454
3455         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3456         buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3457         rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3458                                    0, handle);
3459         if (rc)
3460                 return rc;
3461
3462         la_flag->la_valid = LA_FLAGS;
3463         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3464         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3465
3466         return rc;
3467 }
3468
3469 static int mdd_migrate_create(const struct lu_env *env,
3470                               struct mdd_object *mdd_pobj,
3471                               struct mdd_object *mdd_sobj,
3472                               struct mdd_object *mdd_tobj,
3473                               const struct lu_name *lname,
3474                               struct lu_attr *la)
3475 {
3476         struct mdd_thread_info  *info = mdd_env_info(env);
3477         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3478         struct md_op_spec       *spec = &info->mti_spec;
3479         struct lu_buf           lmm_buf = { NULL };
3480         struct lu_buf           link_buf = { NULL };
3481         struct lu_buf            mgr_buf;
3482         struct thandle          *handle;
3483         struct lmv_mds_md_v1    *mgr_ea;
3484         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3485         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3486         int                     mgr_easize;
3487         struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
3488         int                     rc;
3489         ENTRY;
3490
3491         /* prepare spec for create */
3492         memset(spec, 0, sizeof(*spec));
3493         spec->sp_cr_lookup = 0;
3494         spec->sp_feat = &dt_directory_features;
3495         if (S_ISLNK(la->la_mode)) {
3496                 const struct lu_buf *buf;
3497
3498                 buf = lu_buf_check_and_alloc(
3499                                 &mdd_env_info(env)->mti_big_buf,
3500                                 la->la_size + 1);
3501                 link_buf = *buf;
3502                 link_buf.lb_len = la->la_size + 1;
3503                 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3504                 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3505                 if (rc <= 0) {
3506                         rc = rc != 0 ? rc : -EFAULT;
3507                         CERROR("%s: "DFID" readlink failed: rc = %d\n",
3508                                mdd2obd_dev(mdd)->obd_name,
3509                                PFID(mdd_object_fid(mdd_sobj)), rc);
3510                         RETURN(rc);
3511                 }
3512                 spec->u.sp_symname = link_buf.lb_buf;
3513         } else if (S_ISREG(la->la_mode)) {
3514                 /* retrieve lov of the old object */
3515                 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3516                 if (rc != 0 && rc != -ENODATA)
3517                         RETURN(rc);
3518                 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3519                         spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3520                         spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3521                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3522                 }
3523         } else if (S_ISDIR(la->la_mode)) {
3524                 rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
3525                 if (rc == -ENODATA) {
3526                         /* ignore the non-linkEA error */
3527                         ldata = NULL;
3528                         rc = 0;
3529                 }
3530                 if (rc < 0)
3531                         RETURN(rc);
3532         }
3533
3534         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3535         lu_buf_check_and_alloc(&info->mti_xattr_buf, mgr_easize);
3536         mgr_buf.lb_buf = info->mti_xattr_buf.lb_buf;
3537         mgr_buf.lb_len = mgr_easize;
3538         mgr_ea = mgr_buf.lb_buf;
3539         memset(mgr_ea, 0, sizeof(*mgr_ea));
3540         mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3541         mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3542         mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3543         mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3544         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3545         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3546
3547         mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3548
3549         handle = mdd_trans_create(env, mdd);
3550         if (IS_ERR(handle))
3551                 GOTO(out_free, rc = PTR_ERR(handle));
3552
3553         /* Note: this transaction is part of migration, and it is not
3554          * the last step of migration, so we set th_local = 1 to avoid
3555          * update last rcvd for this transaction */
3556         handle->th_local = 1;
3557         rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj, spec,
3558                                         la, mgr_buf.lb_buf, ldata, handle);
3559         if (rc != 0)
3560                 GOTO(stop_trans, rc);
3561
3562         rc = mdd_trans_start(env, mdd, handle);
3563         if (rc != 0)
3564                 GOTO(stop_trans, rc);
3565
3566         /* don't set nlink from the original object */
3567         la->la_valid &= ~LA_NLINK;
3568
3569         /* create the target object */
3570         rc = mdd_create_object(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3571                                hint, handle);
3572         if (rc != 0)
3573                 GOTO(stop_trans, rc);
3574
3575         if (S_ISDIR(la->la_mode) && ldata != NULL) {
3576                 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3577                 if (rc != 0)
3578                         GOTO(stop_trans, rc);
3579         }
3580
3581         /* Set MIGRATE EA on the source inode, so once the migration needs
3582          * to be re-done during failover, the re-do process can locate the
3583          * target object which is already being created. */
3584         rc = mdo_xattr_set(env, mdd_sobj, &mgr_buf, XATTR_NAME_LMV, 0, handle);
3585         if (rc != 0)
3586                 GOTO(stop_trans, rc);
3587
3588         /* Set immutable flag, so any modification is disabled until
3589          * the migration is done. Once the migration is interrupted,
3590          * if the resume process find the migrating object has both
3591          * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3592          * flag and approve the migration */
3593         la_flag->la_valid = LA_FLAGS;
3594         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3595         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3596 stop_trans:
3597         if (handle != NULL)
3598                 rc = mdd_trans_stop(env, mdd, rc, handle);
3599 out_free:
3600         if (lmm_buf.lb_buf != NULL)
3601                 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3602         RETURN(rc);
3603 }
3604
3605 static int mdd_migrate_entries(const struct lu_env *env,
3606                                struct mdd_object *mdd_sobj,
3607                                struct mdd_object *mdd_tobj)
3608 {
3609         struct dt_object        *next = mdd_object_child(mdd_sobj);
3610         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3611         struct dt_object        *dt_tobj = mdd_object_child(mdd_tobj);
3612         struct thandle          *handle;
3613         struct dt_it            *it;
3614         const struct dt_it_ops  *iops;
3615         int                      result;
3616         struct lu_dirent        *ent;
3617         int                      rc;
3618         ENTRY;
3619
3620         OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3621         if (ent == NULL)
3622                 RETURN(-ENOMEM);
3623
3624         if (!dt_try_as_dir(env, next))
3625                 GOTO(out_ent, rc = -ENOTDIR);
3626         /*
3627          * iterate directories
3628          */
3629         iops = &next->do_index_ops->dio_it;
3630         it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3631         if (IS_ERR(it))
3632                 GOTO(out_ent, rc = PTR_ERR(it));
3633
3634         rc = iops->load(env, it, 0);
3635         if (rc == 0)
3636                 rc = iops->next(env, it);
3637         else if (rc > 0)
3638                 rc = 0;
3639         /*
3640          * At this point and across for-loop:
3641          *
3642          *  rc == 0 -> ok, proceed.
3643          *  rc >  0 -> end of directory.
3644          *  rc <  0 -> error.
3645          */
3646         do {
3647                 struct mdd_object       *child;
3648                 char                    *name = mdd_env_info(env)->mti_key;
3649                 int                     len;
3650                 int                     is_dir;
3651                 bool                    target_exist = false;
3652
3653                 len = iops->key_size(env, it);
3654                 if (len == 0)
3655                         goto next;
3656
3657                 result = iops->rec(env, it, (struct dt_rec *)ent,
3658                                    LUDA_FID | LUDA_TYPE);
3659                 if (result == -ESTALE)
3660                         goto next;
3661                 if (result != 0) {
3662                         rc = result;
3663                         goto out;
3664                 }
3665
3666                 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3667
3668                 /* Insert new fid with target name into target dir */
3669                 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3670                     (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3671                      ent->lde_name[1] == '.'))
3672                         goto next;
3673
3674                 child = mdd_object_find(env, mdd, &ent->lde_fid);
3675                 if (IS_ERR(child))
3676                         GOTO(out, rc = PTR_ERR(child));
3677
3678                 mdd_write_lock(env, child, MOR_SRC_CHILD);
3679                 is_dir = S_ISDIR(mdd_object_type(child));
3680
3681                 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3682
3683                 /* Check whether the name has been inserted to the target */
3684                 if (dt_try_as_dir(env, dt_tobj)) {
3685                         struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3686
3687                         rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3688                                        (struct dt_key *)name);
3689                         if (unlikely(rc == 0))
3690                                 target_exist = true;
3691                 }
3692
3693                 handle = mdd_trans_create(env, mdd);
3694                 if (IS_ERR(handle))
3695                         GOTO(out_put, rc = PTR_ERR(handle));
3696
3697                 /* Note: this transaction is part of migration, and it is not
3698                  * the last step of migration, so we set th_local = 1 to avoid
3699                  * updating last rcvd for this transaction */
3700                 handle->th_local = 1;
3701                 if (likely(!target_exist)) {
3702                         rc = mdo_declare_index_insert(env, mdd_tobj,
3703                                                       &ent->lde_fid,
3704                                                       mdd_object_type(child),
3705                                                       name, handle);
3706                         if (rc != 0)
3707                                 GOTO(out_put, rc);
3708
3709                         if (is_dir) {
3710                                 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3711                                 if (rc != 0)
3712                                         GOTO(out_put, rc);
3713                         }
3714                 }
3715
3716                 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3717                 if (rc != 0)
3718                         GOTO(out_put, rc);
3719
3720                 if (is_dir) {
3721                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3722                         if (rc != 0)
3723                                 GOTO(out_put, rc);
3724
3725                         /* Update .. for child */
3726                         rc = mdo_declare_index_delete(env, child, dotdot,
3727                                                       handle);
3728                         if (rc != 0)
3729                                 GOTO(out_put, rc);
3730
3731                         rc = mdo_declare_index_insert(env, child,
3732                                                       mdd_object_fid(mdd_tobj),
3733                                                       S_IFDIR, dotdot, handle);
3734                         if (rc != 0)
3735                                 GOTO(out_put, rc);
3736                 }
3737
3738                 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3739                                                      child, name,
3740                                                      strlen(name),
3741                                                      handle);
3742                 if (rc != 0)
3743                         GOTO(out_put, rc);
3744
3745                 rc = mdd_trans_start(env, mdd, handle);
3746                 if (rc != 0) {
3747                         CERROR("%s: transaction start failed: rc = %d\n",
3748                                mdd2obd_dev(mdd)->obd_name, rc);
3749                         GOTO(out_put, rc);
3750                 }
3751
3752                 if (likely(!target_exist)) {
3753                         rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3754                                                 mdd_object_type(child),
3755                                                 name, handle);
3756                         if (rc != 0)
3757                                 GOTO(out_put, rc);
3758                 }
3759
3760                 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3761                 if (rc != 0)
3762                         GOTO(out_put, rc);
3763
3764                 if (is_dir) {
3765                         rc = __mdd_index_delete_only(env, child, dotdot,
3766                                                      handle);
3767                         if (rc != 0)
3768                                 GOTO(out_put, rc);
3769
3770                         rc = __mdd_index_insert_only(env, child,
3771                                          mdd_object_fid(mdd_tobj), S_IFDIR,
3772                                          dotdot, handle);
3773                         if (rc != 0)
3774                                 GOTO(out_put, rc);
3775                 }
3776
3777                 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3778                                              child, name,
3779                                              strlen(name), handle);
3780
3781 out_put:
3782                 mdd_write_unlock(env, child);
3783                 mdd_object_put(env, child);
3784                 rc = mdd_trans_stop(env, mdd, rc, handle);
3785                 if (rc != 0)
3786                         GOTO(out, rc);
3787 next:
3788                 result = iops->next(env, it);
3789                 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3790                         GOTO(out, rc = -EINTR);
3791
3792                 if (result == -ESTALE)
3793                         goto next;
3794         } while (result == 0);
3795 out:
3796         iops->put(env, it);
3797         iops->fini(env, it);
3798 out_ent:
3799         OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3800         RETURN(rc);
3801 }
3802
3803 static int mdd_declare_update_linkea(const struct lu_env *env,
3804                                      struct mdd_object *mdd_pobj,
3805                                      struct mdd_object *mdd_sobj,
3806                                      struct mdd_object *mdd_tobj,
3807                                      const struct lu_name *child_name,
3808                                      struct linkea_data *ldata,
3809                                      struct thandle *handle)
3810 {
3811         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3812                                           child_name, ldata, handle, 1);
3813 }
3814
3815 static int mdd_update_linkea(const struct lu_env *env,
3816                              struct mdd_object *mdd_pobj,
3817                              struct mdd_object *mdd_sobj,
3818                              struct mdd_object *mdd_tobj,
3819                              const struct lu_name *child_name,
3820                              struct linkea_data *ldata,
3821                              struct thandle *handle)
3822 {
3823         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3824                                           child_name, ldata, handle, 0);
3825 }
3826
3827 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3828                                            struct mdd_object *mdd_pobj,
3829                                            struct mdd_object *mdd_sobj,
3830                                            struct mdd_object *mdd_tobj,
3831                                            const struct lu_name *lname,
3832                                            struct lu_attr *la,
3833                                            struct lu_attr *parent_la,
3834                                            struct linkea_data *ldata,
3835                                            struct thandle *handle)
3836 {
3837         struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3838         struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3839         int rc;
3840
3841         /* Revert IMMUTABLE flag */
3842         la_flag->la_valid = LA_FLAGS;
3843         la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3844         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3845         if (rc != 0)
3846                 return rc;
3847
3848         /* delete entry from source dir */
3849         rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3850         if (rc != 0)
3851                 return rc;
3852
3853         if (ldata->ld_buf != NULL) {
3854                 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3855                                                mdd_tobj, lname, ldata, handle);
3856                 if (rc != 0)
3857                         return rc;
3858         }
3859
3860         if (S_ISREG(mdd_object_type(mdd_sobj))) {
3861                 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3862                                            handle);
3863                 if (rc != 0)
3864                         return rc;
3865
3866                 handle->th_complex = 1;
3867                 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3868                                            XATTR_NAME_FID,
3869                                            LU_XATTR_REPLACE, handle);
3870                 if (rc < 0)
3871                         return rc;
3872         }
3873
3874         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3875                 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3876                 if (rc != 0)
3877                         return rc;
3878         }
3879
3880         /* new name */
3881         rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3882                                       mdd_object_type(mdd_tobj),
3883                                       lname->ln_name, handle);
3884         if (rc != 0)
3885                 return rc;
3886
3887         rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
3888         if (rc != 0)
3889                 return rc;
3890
3891         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3892                 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3893                 if (rc != 0)
3894                         return rc;
3895         }
3896
3897         /* delete old object */
3898         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3899         if (rc != 0)
3900                 return rc;
3901
3902         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3903                 /* delete old object */
3904                 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3905                 if (rc != 0)
3906                         return rc;
3907                 /* set nlink to 0 */
3908                 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3909                 if (rc != 0)
3910                         return rc;
3911         }
3912
3913         rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3914         if (rc)
3915                 return rc;
3916
3917         rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3918         if (rc != 0)
3919                 return rc;
3920
3921         rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3922
3923         return rc;
3924 }
3925
3926 static int mdd_migrate_update_name(const struct lu_env *env,
3927                                    struct mdd_object *mdd_pobj,
3928                                    struct mdd_object *mdd_sobj,
3929                                    struct mdd_object *mdd_tobj,
3930                                    const struct lu_name *lname,
3931                                    struct md_attr *ma)
3932 {
3933         struct lu_attr          *p_la = MDD_ENV_VAR(env, la_for_fix);
3934         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
3935         struct lu_attr          *la_flag = MDD_ENV_VAR(env, tattr);
3936         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3937         struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
3938         struct thandle          *handle;
3939         int                     is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3940         const char              *name = lname->ln_name;
3941         int                     rc;
3942         ENTRY;
3943
3944         /* update time for parent */
3945         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3946         p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3947         p_la->la_valid = LA_CTIME;
3948
3949         rc = mdd_la_get(env, mdd_sobj, so_attr);
3950         if (rc != 0)
3951                 RETURN(rc);
3952
3953         ldata->ld_buf = NULL;
3954         rc = mdd_links_read(env, mdd_sobj, ldata);
3955         if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3956                 RETURN(rc);
3957
3958         handle = mdd_trans_create(env, mdd);
3959         if (IS_ERR(handle))
3960                 RETURN(PTR_ERR(handle));
3961
3962         rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3963                                              lname, so_attr, p_la, ldata,
3964                                              handle);
3965         if (rc != 0) {
3966                 /* If the migration can not be fit in one transaction, just
3967                  * leave it in the original MDT */
3968                 if (rc == -E2BIG)
3969                         GOTO(stop_trans, rc = 0);
3970                 else
3971                         GOTO(stop_trans, rc);
3972         }
3973
3974         CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3975                mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3976                PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3977                PFID(mdd_object_fid(mdd_tobj)));
3978
3979         rc = mdd_trans_start(env, mdd, handle);
3980         if (rc != 0)
3981                 GOTO(stop_trans, rc);
3982
3983         /* Revert IMMUTABLE flag */
3984         la_flag->la_valid = LA_FLAGS;
3985         la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3986         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3987         if (rc != 0)
3988                 GOTO(stop_trans, rc);
3989
3990         /* Remove source name from source directory */
3991         rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3992         if (rc != 0)
3993                 GOTO(stop_trans, rc);
3994
3995         if (ldata->ld_buf != NULL) {
3996                 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3997                                        lname, ldata, handle);
3998                 if (rc != 0)
3999                         GOTO(stop_trans, rc);
4000
4001                 /*  linkea update might decrease the source object
4002                  *  nlink, let's get the attr again after ref_del */
4003                 rc = mdd_la_get(env, mdd_sobj, so_attr);
4004                 if (rc != 0)
4005                         GOTO(stop_trans, rc);
4006         }
4007
4008         if (S_ISREG(so_attr->la_mode)) {
4009                 if (so_attr->la_nlink == 1) {
4010                         rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4011                                            handle);
4012                         if (rc != 0 && rc != -ENODATA)
4013                                 GOTO(stop_trans, rc);
4014
4015                         rc = mdo_xattr_set(env, mdd_tobj, NULL,
4016                                            XATTR_NAME_FID,
4017                                            LU_XATTR_REPLACE, handle);
4018                         if (rc < 0)
4019                                 GOTO(stop_trans, rc);
4020                 }
4021         }
4022
4023         /* Insert new fid with target name into target dir */
4024         rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4025                                 mdd_object_type(mdd_tobj), name, handle);
4026         if (rc != 0)
4027                 GOTO(stop_trans, rc);
4028
4029         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4030
4031         mdd_sobj->mod_flags |= DEAD_OBJ;
4032         rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4033         if (rc != 0)
4034                 GOTO(out_unlock, rc);
4035
4036         rc = __mdd_orphan_add(env, mdd_sobj, handle);
4037         if (rc != 0)
4038                 GOTO(out_unlock, rc);
4039
4040         mdo_ref_del(env, mdd_sobj, handle);
4041         if (is_dir)
4042                 mdo_ref_del(env, mdd_sobj, handle);
4043
4044         /* Get the attr again after ref_del */
4045         rc = mdd_la_get(env, mdd_sobj, so_attr);
4046         if (rc != 0)
4047                 GOTO(out_unlock, rc);
4048
4049         ma->ma_attr = *so_attr;
4050         ma->ma_valid |= MA_INODE;
4051
4052         rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4053         if (rc != 0)
4054                 GOTO(out_unlock, rc);
4055
4056         rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4057                                mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4058                                mdo2fid(mdd_pobj), lname, lname, handle);
4059         if (rc != 0) {
4060                 CWARN("%s: changelog for migrate %s "DFID
4061                       "under "DFID" failed: rc = %d\n",
4062                       mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4063                       PFID(mdd_object_fid(mdd_sobj)),
4064                       PFID(mdd_object_fid(mdd_pobj)), rc);
4065                 /* Sigh, there are no easy way to migrate back the object, so
4066                  * let's reset the result to 0 for now XXX */
4067                 rc = 0;
4068         }
4069 out_unlock:
4070         mdd_write_unlock(env, mdd_sobj);
4071
4072 stop_trans:
4073         rc = mdd_trans_stop(env, mdd, rc, handle);
4074
4075         RETURN(rc);
4076 }
4077
4078 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4079                           const struct lu_fid *fid, __u32 *mdt_index)
4080 {
4081         struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4082         struct seq_server_site *ss;
4083         int rc;
4084
4085         ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4086
4087         range->lsr_flags = LU_SEQ_RANGE_MDT;
4088         rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4089         if (rc != 0)
4090                 return rc;
4091
4092         *mdt_index = range->lsr_index;
4093
4094         return 0;
4095 }
4096 /**
4097  * Check whether we should migrate the file/dir
4098  * return val
4099  *      < 0  permission check failed or other error.
4100  *      = 0  the file can be migrated.
4101  *      > 0  the file does not need to be migrated, mostly
4102  *           for multiple link file
4103  **/
4104 static int mdd_migrate_sanity_check(const struct lu_env *env,
4105                                     struct mdd_object *pobj,
4106                                     const struct lu_attr *pattr,
4107                                     struct mdd_object *sobj,
4108                                     struct lu_attr *sattr)
4109 {
4110         struct mdd_thread_info  *info = mdd_env_info(env);
4111         struct linkea_data      *ldata = &info->mti_link_data;
4112         struct mdd_device       *mdd = mdo2mdd(&pobj->mod_obj);
4113         int                     mgr_easize;
4114         struct lu_buf           *mgr_buf;
4115         int                     count;
4116         int                     rc;
4117         __u64 mdt_index;
4118         ENTRY;
4119
4120         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4121         mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4122         if (mgr_buf->lb_buf == NULL)
4123                 RETURN(-ENOMEM);
4124
4125         rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4126         if (rc > 0) {
4127                 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4128
4129                 /* If the object has migrateEA, it means IMMUTE flag
4130                  * is being set by previous migration process, so it
4131                  * needs to override the IMMUTE flag, otherwise the
4132                  * following sanity check will fail */
4133                 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4134                                                 LMV_HASH_FLAG_MIGRATION) {
4135                         struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4136
4137                         sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4138                         CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4139                                mdd2obd_dev(mdd)->obd_name,
4140                                PFID(mdd_object_fid(sobj)));
4141                 }
4142         }
4143
4144         rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4145                                      sobj, sattr, NULL, NULL);
4146         if (rc != 0)
4147                 RETURN(rc);
4148
4149         /* Then it will check if the file should be migrated. If the file
4150          * has mulitple links, we only need migrate the file if all of its
4151          * entries has been migrated to the remote MDT */
4152         if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4153                 RETURN(0);
4154
4155         rc = mdd_links_read(env, sobj, ldata);
4156         if (rc != 0) {
4157                 /* For multiple links files, if there are no linkEA data at all,
4158                  * means the file might be created before linkEA is enabled, and
4159                  * all of its links should not be migrated yet, otherwise it
4160                  * should have some linkEA there */
4161                 if (rc == -ENOENT || rc == -ENODATA)
4162                         RETURN(1);
4163                 RETURN(rc);
4164         }
4165
4166         mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4167         /* If there are still links locally, then the file will not be
4168          * migrated. */
4169         LASSERT(ldata->ld_leh != NULL);
4170
4171         /* If the linkEA is overflow, then means there are some unknown name
4172          * entries under unknown parents, that will prevent the migration. */
4173         if (unlikely(ldata->ld_leh->leh_overflow_time))
4174                 RETURN(1);
4175
4176         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4177         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4178                 struct lu_name          lname;
4179                 struct lu_fid           fid;
4180                 __u32                   parent_mdt_index;
4181
4182                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4183                                     &lname, &fid);
4184                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4185                                                          ldata->ld_reclen);
4186
4187                 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4188                 if (rc != 0)
4189                         RETURN(rc);
4190
4191                 /* Migrate the object only if none of its parents are on the
4192                  * current MDT. */
4193                 if (parent_mdt_index != mdt_index)
4194                         continue;
4195
4196                 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4197                        PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4198                        lname.ln_name, PFID(&fid));
4199                 rc = 1;
4200                 break;
4201         }
4202
4203         RETURN(rc);
4204 }
4205
4206 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4207                        struct md_object *sobj, const struct lu_name *lname,
4208                        struct md_object *tobj, struct md_attr *ma)
4209 {
4210         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
4211         struct mdd_device       *mdd = mdo2mdd(pobj);
4212         struct mdd_object       *mdd_sobj = md2mdd_obj(sobj);
4213         struct mdd_object       *mdd_tobj = md2mdd_obj(tobj);
4214         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
4215         struct lu_attr          *pattr = MDD_ENV_VAR(env, pattr);
4216         bool                    created = false;
4217         int                     rc;
4218
4219         ENTRY;
4220         /* If the file will being migrated, it will check whether
4221          * the file is being opened by someone else right now */
4222         mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4223         if (mdd_sobj->mod_count > 0) {
4224                 CDEBUG(D_OTHER,
4225                        "%s: "DFID"%s is already opened count %d: rc = %d\n",
4226                        mdd2obd_dev(mdd)->obd_name,
4227                        PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4228                        mdd_sobj->mod_count, -EBUSY);
4229                 mdd_read_unlock(env, mdd_sobj);
4230                 GOTO(put, rc = -EBUSY);
4231         }
4232         mdd_read_unlock(env, mdd_sobj);
4233
4234         rc = mdd_la_get(env, mdd_sobj, so_attr);
4235         if (rc != 0)
4236                 GOTO(put, rc);
4237
4238         rc = mdd_la_get(env, mdd_pobj, pattr);
4239         if (rc != 0)
4240                 GOTO(put, rc);
4241
4242         rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4243         if (rc != 0) {
4244                 if (rc > 0)
4245                         rc = 0;
4246                 GOTO(put, rc);
4247         }
4248
4249         /* Sigh, it is impossible to finish all of migration in a single
4250          * transaction, for example migrating big directory entries to the
4251          * new MDT, it needs insert all of name entries of children in the
4252          * new directory.
4253          *
4254          * So migration will be done in multiple steps and transactions.
4255          *
4256          * 1. create an orphan object on the remote MDT in one transaction.
4257          * 2. migrate extend attributes to the new target file/directory.
4258          * 3. For directory, migrate the entries to the new MDT and update
4259          * linkEA of each children. Because we can not migrate all entries
4260          * in a single transaction, so the migrating directory will become
4261          * a striped directory during migration, so once the process is
4262          * interrupted, the directory is still accessible. (During lookup,
4263          * client will locate the name by searching both original and target
4264          * object).
4265          * 4. Finally, update the name/FID to point to the new file/directory
4266          * in a separate transaction.
4267          */
4268
4269         /* step 1: Check whether the orphan object has been created, and create
4270          * orphan object on the remote MDT if needed */
4271         if (!mdd_object_exists(mdd_tobj)) {
4272                 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4273                                         lname, so_attr);
4274                 if (rc != 0)
4275                         GOTO(put, rc);
4276                 created = true;
4277         }
4278
4279         LASSERT(mdd_object_exists(mdd_tobj));
4280         /* step 2: migrate xattr */
4281         rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4282         if (rc != 0)
4283                 GOTO(put, rc);
4284
4285         /* step 3: migrate name entries to the orphan object */
4286         if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4287                 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4288                 if (rc != 0)
4289                         GOTO(put, rc);
4290                 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4291                                                   OBD_FAIL_MDS_REINT_NET_REP)))
4292                         GOTO(put, rc = 0);
4293         } else {
4294                 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4295         }
4296
4297         LASSERT(mdd_object_exists(mdd_tobj));
4298         /* step 4: update name entry to the new object */
4299         rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4300                                      ma);
4301         if (rc != 0)
4302                 GOTO(put, rc);
4303
4304         /* newly created target was not locked, don't cache its attributes */
4305         if (created)
4306                 mdd_invalidate(env, tobj);
4307 put:
4308         RETURN(rc);
4309 }
4310
4311 const struct md_dir_operations mdd_dir_ops = {
4312         .mdo_is_subdir     = mdd_is_subdir,
4313         .mdo_lookup        = mdd_lookup,
4314         .mdo_create        = mdd_create,
4315         .mdo_rename        = mdd_rename,
4316         .mdo_link          = mdd_link,
4317         .mdo_unlink        = mdd_unlink,
4318         .mdo_create_data   = mdd_create_data,
4319         .mdo_migrate       = mdd_migrate,
4320 };