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