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