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