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