Whamcloud - gitweb
LU-7579 mdd: do not mark object as an orphan early
[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_orphan_add(env, obj, th);
1471                         if (rc == 0)
1472                                 CDEBUG(D_HA, "Object "DFID" is inserted into "
1473                                         "orphan list, open count = %d\n",
1474                                         PFID(mdd_object_fid(obj)),
1475                                         obj->mod_count);
1476                         else
1477                                 CERROR("Object "DFID" fail to be an orphan, "
1478                                        "open count = %d, maybe cause failed "
1479                                        "open replay\n",
1480                                         PFID(mdd_object_fid(obj)),
1481                                         obj->mod_count);
1482
1483                         /* mark object as an orphan here, not
1484                          * before __mdd_orphan_add() as racing
1485                          * mdd_la_get() may propagate ORPHAN_OBJ
1486                          * causing the asserition */
1487                         rc = mdd_mark_orphan_object(env, obj, th, false);
1488                 } else {
1489                         rc = mdo_destroy(env, obj, th);
1490                 }
1491         } else if (!is_dir) {
1492                 /* old files may not have link ea; ignore errors */
1493                 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1494         }
1495
1496         RETURN(rc);
1497 }
1498
1499 /*
1500  * pobj maybe NULL
1501  * has mdd_write_lock on cobj already, but not on pobj yet
1502  */
1503 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1504                             const struct lu_attr *pattr,
1505                             struct mdd_object *cobj,
1506                             const struct lu_attr *cattr)
1507 {
1508         int rc;
1509         ENTRY;
1510
1511         rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1512
1513         RETURN(rc);
1514 }
1515
1516 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1517                               struct mdd_object *p, struct mdd_object *c,
1518                               const struct lu_name *name, struct md_attr *ma,
1519                               struct thandle *handle, int no_name, int is_dir)
1520 {
1521         struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
1522         int              rc;
1523
1524         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1525                 if (likely(no_name == 0)) {
1526                         rc = mdo_declare_index_delete(env, p, name->ln_name,
1527                                                       handle);
1528                         if (rc != 0)
1529                                 return rc;
1530                 }
1531
1532                 if (is_dir != 0) {
1533                         rc = mdo_declare_ref_del(env, p, handle);
1534                         if (rc != 0)
1535                                 return rc;
1536                 }
1537         }
1538
1539         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1540         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1541         la->la_valid = LA_CTIME | LA_MTIME;
1542         rc = mdo_declare_attr_set(env, p, la, handle);
1543         if (rc)
1544                 return rc;
1545
1546         if (c != NULL) {
1547                 rc = mdo_declare_ref_del(env, c, handle);
1548                 if (rc)
1549                         return rc;
1550
1551                 rc = mdo_declare_ref_del(env, c, handle);
1552                 if (rc)
1553                         return rc;
1554
1555                 la->la_valid = LA_CTIME;
1556                 rc = mdo_declare_attr_set(env, c, la, handle);
1557                 if (rc)
1558                         return rc;
1559
1560                 rc = mdd_declare_finish_unlink(env, c, handle);
1561                 if (rc)
1562                         return rc;
1563
1564                 /* FIXME: need changelog for remove entry */
1565                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1566         }
1567
1568         return rc;
1569 }
1570
1571 /*
1572  * test if a file has an HSM archive
1573  * if HSM attributes are not found in ma update them from
1574  * HSM xattr
1575  */
1576 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1577                                    struct mdd_object *obj,
1578                                    struct md_attr *ma)
1579 {
1580         ENTRY;
1581
1582         if (!(ma->ma_valid & MA_HSM)) {
1583                 /* no HSM MD provided, read xattr */
1584                 struct lu_buf   *hsm_buf;
1585                 const size_t     buflen = sizeof(struct hsm_attrs);
1586                 int              rc;
1587
1588                 hsm_buf = mdd_buf_get(env, NULL, 0);
1589                 lu_buf_alloc(hsm_buf, buflen);
1590                 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1591                 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1592                 lu_buf_free(hsm_buf);
1593                 if (rc < 0)
1594                         RETURN(false);
1595
1596                 ma->ma_valid = MA_HSM;
1597         }
1598         if (ma->ma_hsm.mh_flags & HS_EXISTS)
1599                 RETURN(true);
1600         RETURN(false);
1601 }
1602
1603 /**
1604  * Delete name entry and the object.
1605  * Note: no_name == 1 means it only destory the object, i.e. name_entry
1606  * does not exist for this object, and it could only happen during resending
1607  * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1608  * is also needed in this case(needed by changelog), so we have to add another
1609  * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1610  * the ENOENT failure should be able to be fixed by redo mechanism.
1611  */
1612 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1613                       struct md_object *cobj, const struct lu_name *lname,
1614                       struct md_attr *ma, int no_name)
1615 {
1616         const char *name = lname->ln_name;
1617         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1618         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1619         struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1620         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1621         struct mdd_object *mdd_cobj = NULL;
1622         struct mdd_device *mdd = mdo2mdd(pobj);
1623         struct thandle    *handle;
1624         int rc, is_dir = 0;
1625         ENTRY;
1626
1627         /* cobj == NULL means only delete name entry */
1628         if (likely(cobj != NULL)) {
1629                 mdd_cobj = md2mdd_obj(cobj);
1630                 if (mdd_object_exists(mdd_cobj) == 0)
1631                         RETURN(-ENOENT);
1632         }
1633
1634         rc = mdd_la_get(env, mdd_pobj, pattr);
1635         if (rc)
1636                 RETURN(rc);
1637
1638         if (likely(mdd_cobj != NULL)) {
1639                 /* fetch cattr */
1640                 rc = mdd_la_get(env, mdd_cobj, cattr);
1641                 if (rc)
1642                         RETURN(rc);
1643
1644                 is_dir = S_ISDIR(cattr->la_mode);
1645         }
1646
1647         rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1648         if (rc)
1649                 RETURN(rc);
1650
1651         handle = mdd_trans_create(env, mdd);
1652         if (IS_ERR(handle))
1653                 RETURN(PTR_ERR(handle));
1654
1655         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1656                                 lname, ma, handle, no_name, is_dir);
1657         if (rc)
1658                 GOTO(stop, rc);
1659
1660         rc = mdd_trans_start(env, mdd, handle);
1661         if (rc)
1662                 GOTO(stop, rc);
1663
1664         if (likely(mdd_cobj != NULL))
1665                 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1666
1667         if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1668                 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1669                 if (rc)
1670                         GOTO(cleanup, rc);
1671         }
1672
1673         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1674             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1675                 GOTO(cleanup, rc = 0);
1676
1677         if (likely(mdd_cobj != NULL)) {
1678                 rc = mdo_ref_del(env, mdd_cobj, handle);
1679                 if (rc != 0) {
1680                         __mdd_index_insert_only(env, mdd_pobj,
1681                                                 mdo2fid(mdd_cobj),
1682                                                 mdd_object_type(mdd_cobj),
1683                                                 name, handle);
1684                         GOTO(cleanup, rc);
1685                 }
1686
1687                 if (is_dir)
1688                         /* unlink dot */
1689                         mdo_ref_del(env, mdd_cobj, handle);
1690
1691                 /* fetch updated nlink */
1692                 rc = mdd_la_get(env, mdd_cobj, cattr);
1693                 if (rc)
1694                         GOTO(cleanup, rc);
1695         }
1696
1697         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1698         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1699
1700         la->la_valid = LA_CTIME | LA_MTIME;
1701         rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1702         if (rc)
1703                 GOTO(cleanup, rc);
1704
1705         /* Enough for only unlink the entry */
1706         if (unlikely(mdd_cobj == NULL))
1707                 GOTO(stop, rc);
1708
1709         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1710                 /* update ctime of an unlinked file only if it is still
1711                  * opened or a link still exists */
1712                 la->la_valid = LA_CTIME;
1713                 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1714                 if (rc)
1715                         GOTO(cleanup, rc);
1716         }
1717
1718         /* XXX: this transfer to ma will be removed with LOD/OSP */
1719         ma->ma_attr = *cattr;
1720         ma->ma_valid |= MA_INODE;
1721         rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1722
1723         /* fetch updated nlink */
1724         if (rc != 0)
1725                 GOTO(cleanup, rc);
1726
1727         rc = mdd_la_get(env, mdd_cobj, cattr);
1728         /* if object is removed then we can't get its attrs,
1729          * use last get */
1730         if (rc == -ENOENT) {
1731                 cattr->la_nlink = 0;
1732                 rc = 0;
1733         }
1734
1735         if (cattr->la_nlink == 0) {
1736                 ma->ma_attr = *cattr;
1737                 ma->ma_valid |= MA_INODE;
1738         }
1739
1740         EXIT;
1741 cleanup:
1742         if (likely(mdd_cobj != NULL))
1743                 mdd_write_unlock(env, mdd_cobj);
1744
1745         if (rc == 0) {
1746                 int cl_flags = 0;
1747
1748                 if (cattr->la_nlink == 0) {
1749                         cl_flags |= CLF_UNLINK_LAST;
1750                         /* search for an existing archive */
1751                         if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1752                                 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1753                 }
1754
1755                 rc = mdd_changelog_ns_store(env, mdd,
1756                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1757                         mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1758                         handle);
1759         }
1760
1761 stop:
1762         mdd_trans_stop(env, mdd, rc, handle);
1763
1764         return rc;
1765 }
1766
1767 /*
1768  * The permission has been checked when obj created, no need check again.
1769  */
1770 static int mdd_cd_sanity_check(const struct lu_env *env,
1771                                struct mdd_object *obj)
1772 {
1773         ENTRY;
1774
1775         /* EEXIST check */
1776         if (!obj || mdd_is_dead_obj(obj))
1777                 RETURN(-ENOENT);
1778
1779         RETURN(0);
1780 }
1781
1782 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1783                            struct md_object *cobj, const struct md_op_spec *spec,
1784                            struct md_attr *ma)
1785 {
1786         struct mdd_device *mdd = mdo2mdd(cobj);
1787         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1788         struct mdd_object *son = md2mdd_obj(cobj);
1789         struct thandle    *handle;
1790         const struct lu_buf *buf;
1791         struct lu_attr    *attr = MDD_ENV_VAR(env, cattr);
1792         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1793         int                rc;
1794         ENTRY;
1795
1796         rc = mdd_cd_sanity_check(env, son);
1797         if (rc)
1798                 RETURN(rc);
1799
1800         if (!md_should_create(spec->sp_cr_flags))
1801                 RETURN(0);
1802
1803         /*
1804          * there are following use cases for this function:
1805          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1806          *    striping can be specified or not
1807          * 2) CMD?
1808          */
1809         rc = mdd_la_get(env, son, attr);
1810         if (rc)
1811                 RETURN(rc);
1812
1813         /* calling ->ah_make_hint() is used to transfer information from parent */
1814         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1815
1816         handle = mdd_trans_create(env, mdd);
1817         if (IS_ERR(handle))
1818                 GOTO(out_free, rc = PTR_ERR(handle));
1819
1820         /*
1821          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1822          * Should this be fixed?
1823          */
1824         CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1825                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1826                spec->sp_cr_flags, spec->no_create);
1827
1828         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1829                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1830                                         spec->u.sp_ea.eadatalen);
1831         } else {
1832                 buf = &LU_BUF_NULL;
1833         }
1834
1835         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1836                                   XATTR_NAME_LOV, 0, handle);
1837         if (rc)
1838                 GOTO(stop, rc);
1839
1840         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1841         if (rc)
1842                 GOTO(stop, rc);
1843
1844         rc = mdd_trans_start(env, mdd, handle);
1845         if (rc)
1846                 GOTO(stop, rc);
1847
1848         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1849                           0, handle);
1850
1851         if (rc)
1852                 GOTO(stop, rc);
1853
1854         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1855
1856 stop:
1857         mdd_trans_stop(env, mdd, rc, handle);
1858 out_free:
1859         RETURN(rc);
1860 }
1861
1862 static int mdd_declare_object_initialize(const struct lu_env *env,
1863                                          struct mdd_object *parent,
1864                                          struct mdd_object *child,
1865                                          struct lu_attr *attr,
1866                                          struct thandle *handle)
1867 {
1868         int rc;
1869         ENTRY;
1870
1871         /*
1872          * inode mode has been set in creation time, and it's based on umask,
1873          * la_mode and acl, don't set here again! (which will go wrong
1874          * because below function doesn't consider umask).
1875          * I'd suggest set all object attributes in creation time, see above.
1876          */
1877         LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1878         attr->la_valid &= ~(LA_MODE | LA_TYPE);
1879         rc = mdo_declare_attr_set(env, child, attr, handle);
1880         attr->la_valid |= LA_MODE | LA_TYPE;
1881         if (rc != 0 || !S_ISDIR(attr->la_mode))
1882                 RETURN(rc);
1883
1884         rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1885                                       dot, handle);
1886         if (rc != 0)
1887                 RETURN(rc);
1888
1889         rc = mdo_declare_ref_add(env, child, handle);
1890         if (rc != 0)
1891                 RETURN(rc);
1892
1893         rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1894                                       dotdot, handle);
1895
1896         RETURN(rc);
1897 }
1898
1899 static int mdd_object_initialize(const struct lu_env *env,
1900                                  const struct lu_fid *pfid,
1901                                  struct mdd_object *child,
1902                                  struct lu_attr *attr, struct thandle *handle,
1903                                  const struct md_op_spec *spec)
1904 {
1905         int rc = 0;
1906         ENTRY;
1907
1908         if (S_ISDIR(attr->la_mode)) {
1909                 /* Add "." and ".." for newly created dir */
1910                 mdo_ref_add(env, child, handle);
1911                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1912                                              S_IFDIR, dot, handle);
1913                 if (rc == 0)
1914                         rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1915                                                      dotdot, handle);
1916                 if (rc != 0)
1917                         mdo_ref_del(env, child, handle);
1918         }
1919
1920         RETURN(rc);
1921 }
1922
1923 /**
1924  * This function checks whether it can create a file/dir under the
1925  * directory(@pobj). The directory(@pobj) is not being locked by
1926  * mdd lock.
1927  *
1928  * \param[in] env       execution environment
1929  * \param[in] pobj      the directory to create files
1930  * \param[in] pattr     the attributes of the directory
1931  * \param[in] lname     the name of the created file/dir
1932  * \param[in] cattr     the attributes of the file/dir
1933  * \param[in] spec      create specification
1934  *
1935  * \retval              = 0 it is allowed to create file/dir under
1936  *                      the directory
1937  * \retval              negative error not allowed to create file/dir
1938  *                      under the directory
1939  */
1940 static int mdd_create_sanity_check(const struct lu_env *env,
1941                                    struct md_object *pobj,
1942                                    const struct lu_attr *pattr,
1943                                    const struct lu_name *lname,
1944                                    struct lu_attr *cattr,
1945                                    struct md_op_spec *spec)
1946 {
1947         struct mdd_thread_info *info = mdd_env_info(env);
1948         struct lu_fid     *fid       = &info->mti_fid;
1949         struct mdd_object *obj       = md2mdd_obj(pobj);
1950         struct mdd_device *m         = mdo2mdd(pobj);
1951         bool            check_perm = true;
1952         int rc;
1953         ENTRY;
1954
1955         /* EEXIST check */
1956         if (mdd_is_dead_obj(obj))
1957                 RETURN(-ENOENT);
1958
1959         /*
1960          * In some cases this lookup is not needed - we know before if name
1961          * exists or not because MDT performs lookup for it.
1962          * name length check is done in lookup.
1963          */
1964         if (spec->sp_cr_lookup) {
1965                 /*
1966                  * Check if the name already exist, though it will be checked in
1967                  * _index_insert also, for avoiding rolling back if exists
1968                  * _index_insert.
1969                  */
1970                 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1971                                   MAY_WRITE | MAY_EXEC);
1972                 if (rc != -ENOENT)
1973                         RETURN(rc ? : -EEXIST);
1974
1975                 /* Permission is already being checked in mdd_lookup */
1976                 check_perm = false;
1977         }
1978
1979         rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1980         if (rc != 0)
1981                 RETURN(rc);
1982
1983         /* sgid check */
1984         if (pattr->la_mode & S_ISGID) {
1985                 cattr->la_gid = pattr->la_gid;
1986                 if (S_ISDIR(cattr->la_mode)) {
1987                         cattr->la_mode |= S_ISGID;
1988                         cattr->la_valid |= LA_MODE;
1989                 }
1990         }
1991
1992         rc = mdd_name_check(m, lname);
1993         if (rc < 0)
1994                 RETURN(rc);
1995
1996         switch (cattr->la_mode & S_IFMT) {
1997         case S_IFLNK: {
1998                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1999
2000                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
2001                         RETURN(-ENAMETOOLONG);
2002                 else
2003                         RETURN(0);
2004         }
2005         case S_IFDIR:
2006         case S_IFREG:
2007         case S_IFCHR:
2008         case S_IFBLK:
2009         case S_IFIFO:
2010         case S_IFSOCK:
2011                 rc = 0;
2012                 break;
2013         default:
2014                 rc = -EINVAL;
2015                 break;
2016         }
2017         RETURN(rc);
2018 }
2019
2020 static int mdd_declare_object_create(const struct lu_env *env,
2021                                      struct mdd_device *mdd,
2022                                      struct mdd_object *p, struct mdd_object *c,
2023                                      struct lu_attr *attr,
2024                                      struct thandle *handle,
2025                                      const struct md_op_spec *spec,
2026                                      struct lu_buf *def_acl_buf,
2027                                      struct lu_buf *acl_buf,
2028                                      struct dt_allocation_hint *hint)
2029 {
2030         int rc;
2031
2032         rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2033                                                 hint);
2034         if (rc)
2035                 GOTO(out, rc);
2036
2037 #ifdef CONFIG_FS_POSIX_ACL
2038         if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2039                 /* if dir, then can inherit default ACl */
2040                 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2041                                            XATTR_NAME_ACL_DEFAULT,
2042                                            0, handle);
2043                 if (rc)
2044                         GOTO(out, rc);
2045         }
2046
2047         if (acl_buf->lb_len > 0) {
2048                 rc = mdo_declare_attr_set(env, c, attr, handle);
2049                 if (rc)
2050                         GOTO(out, rc);
2051
2052                 rc = mdo_declare_xattr_set(env, c, acl_buf,
2053                                            XATTR_NAME_ACL_ACCESS, 0, handle);
2054                 if (rc)
2055                         GOTO(out, rc);
2056         }
2057 #endif
2058         rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2059         if (rc)
2060                 GOTO(out, rc);
2061
2062         /* replay case, create LOV EA from client data */
2063         if (spec->no_create ||
2064             (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2065                 const struct lu_buf *buf;
2066
2067                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2068                                         spec->u.sp_ea.eadatalen);
2069                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2070                                            handle);
2071                 if (rc)
2072                         GOTO(out, rc);
2073         }
2074
2075         if (S_ISLNK(attr->la_mode)) {
2076                 const char *target_name = spec->u.sp_symname;
2077                 int sym_len = strlen(target_name);
2078                 const struct lu_buf *buf;
2079
2080                 buf = mdd_buf_get_const(env, target_name, sym_len);
2081                 rc = dt_declare_record_write(env, mdd_object_child(c),
2082                                              buf, 0, handle);
2083                 if (rc)
2084                         GOTO(out, rc);
2085         }
2086 out:
2087         return rc;
2088 }
2089
2090 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2091                               struct mdd_object *p, struct mdd_object *c,
2092                               const struct lu_name *name,
2093                               struct lu_attr *attr,
2094                               struct thandle *handle,
2095                               const struct md_op_spec *spec,
2096                               struct linkea_data *ldata,
2097                               struct lu_buf *def_acl_buf,
2098                               struct lu_buf *acl_buf,
2099                               struct dt_allocation_hint *hint)
2100 {
2101         int rc;
2102
2103         rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2104                                        def_acl_buf, acl_buf, hint);
2105         if (rc)
2106                 GOTO(out, rc);
2107
2108         if (S_ISDIR(attr->la_mode)) {
2109                 rc = mdo_declare_ref_add(env, p, handle);
2110                 if (rc)
2111                         GOTO(out, rc);
2112         }
2113
2114         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2115                 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2116                 if (rc)
2117                         GOTO(out, rc);
2118         } else {
2119                 struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
2120
2121                 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2122                                               name->ln_name, handle);
2123                 if (rc != 0)
2124                         return rc;
2125
2126                 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2127                 if (rc)
2128                         return rc;
2129
2130                 *la = *attr;
2131                 la->la_valid = LA_CTIME | LA_MTIME;
2132                 rc = mdo_declare_attr_set(env, p, la, handle);
2133                 if (rc)
2134                         return rc;
2135
2136                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2137                 if (rc)
2138                         return rc;
2139         }
2140 out:
2141         return rc;
2142 }
2143
2144 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2145                         struct lu_attr *la, struct lu_buf *def_acl_buf,
2146                         struct lu_buf *acl_buf)
2147 {
2148         int     rc;
2149         ENTRY;
2150
2151         if (S_ISLNK(la->la_mode)) {
2152                 acl_buf->lb_len = 0;
2153                 def_acl_buf->lb_len = 0;
2154                 RETURN(0);
2155         }
2156
2157         mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2158         rc = mdo_xattr_get(env, pobj, def_acl_buf,
2159                            XATTR_NAME_ACL_DEFAULT);
2160         mdd_read_unlock(env, pobj);
2161         if (rc > 0) {
2162                 /* If there are default ACL, fix mode/ACL by default ACL */
2163                 def_acl_buf->lb_len = rc;
2164                 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2165                 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2166                 acl_buf->lb_len = rc;
2167                 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2168                 if (rc < 0)
2169                         RETURN(rc);
2170         } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2171                 /* If there are no default ACL, fix mode by mask */
2172                 struct lu_ucred *uc = lu_ucred(env);
2173
2174                 /* The create triggered by MDT internal events, such as
2175                  * LFSCK reset, will not contain valid "uc". */
2176                 if (unlikely(uc != NULL))
2177                         la->la_mode &= ~uc->uc_umask;
2178                 rc = 0;
2179                 acl_buf->lb_len = 0;
2180                 def_acl_buf->lb_len = 0;
2181         }
2182
2183         RETURN(rc);
2184 }
2185
2186 /**
2187  * Create a metadata object and initialize it, set acl, xattr.
2188  **/
2189 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2190                              struct mdd_object *son, struct lu_attr *attr,
2191                              struct md_op_spec *spec, struct lu_buf *acl_buf,
2192                              struct lu_buf *def_acl_buf,
2193                              struct dt_allocation_hint *hint,
2194                              struct thandle *handle)
2195 {
2196         int                     rc;
2197
2198         mdd_write_lock(env, son, MOR_TGT_CHILD);
2199         rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2200                                         hint);
2201         if (rc)
2202                 GOTO(unlock, rc);
2203
2204         /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2205          * created in declare phase, they also needs to be added to master
2206          * object as sub-directory entry. So it has to initialize the master
2207          * object, then set dir striped EA.(in mdo_xattr_set) */
2208         rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2209                                    spec);
2210         if (rc != 0)
2211                 GOTO(err_destroy, rc);
2212
2213         /*
2214          * in case of replay we just set LOVEA provided by the client
2215          * XXX: I think it would be interesting to try "old" way where
2216          *      MDT calls this xattr_set(LOV) in a different transaction.
2217          *      probably this way we code can be made better.
2218          */
2219
2220         /* During creation, there are only a few cases we need do xattr_set to
2221          * create stripes.
2222          * 1. regular file: see comments above.
2223          * 2. dir: inherit default striping or pool settings from parent.
2224          * 3. create striped directory with provided stripeEA.
2225          * 4. create striped directory because inherit default layout from the
2226          * parent.
2227          */
2228         if (spec->no_create ||
2229             (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2230             S_ISDIR(attr->la_mode)) {
2231                 const struct lu_buf *buf;
2232
2233                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2234                                         spec->u.sp_ea.eadatalen);
2235                 rc = mdo_xattr_set(env, son, buf,
2236                                    S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2237                                                             XATTR_NAME_LOV, 0,
2238                                    handle);
2239                 if (rc != 0)
2240                         GOTO(err_destroy, rc);
2241         }
2242
2243 #ifdef CONFIG_FS_POSIX_ACL
2244         if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2245             S_ISDIR(attr->la_mode)) {
2246                 /* set default acl */
2247                 rc = mdo_xattr_set(env, son, def_acl_buf,
2248                                    XATTR_NAME_ACL_DEFAULT, 0,
2249                                    handle);
2250                 if (rc)
2251                         GOTO(err_destroy, rc);
2252         }
2253         /* set its own acl */
2254         if (acl_buf != NULL && acl_buf->lb_len > 0) {
2255                 rc = mdo_xattr_set(env, son, acl_buf,
2256                                    XATTR_NAME_ACL_ACCESS,
2257                                    0, handle);
2258                 if (rc)
2259                         GOTO(err_destroy, rc);
2260         }
2261 #endif
2262
2263         if (S_ISLNK(attr->la_mode)) {
2264                 struct lu_ucred  *uc = lu_ucred_assert(env);
2265                 struct dt_object *dt = mdd_object_child(son);
2266                 const char *target_name = spec->u.sp_symname;
2267                 int sym_len = strlen(target_name);
2268                 const struct lu_buf *buf;
2269                 loff_t pos = 0;
2270
2271                 buf = mdd_buf_get_const(env, target_name, sym_len);
2272                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2273                                                 uc->uc_cap &
2274                                                 CFS_CAP_SYS_RESOURCE_MASK);
2275
2276                 if (rc == sym_len)
2277                         rc = 0;
2278                 else
2279                         GOTO(err_initlized, rc = -EFAULT);
2280         }
2281
2282 err_initlized:
2283         if (unlikely(rc != 0)) {
2284                 int rc2;
2285                 if (S_ISDIR(attr->la_mode)) {
2286                         /* Drop the reference, no need to delete "."/"..",
2287                          * because the object to be destroied directly. */
2288                         rc2 = mdo_ref_del(env, son, handle);
2289                         if (rc2 != 0)
2290                                 GOTO(unlock, rc);
2291                 }
2292                 rc2 = mdo_ref_del(env, son, handle);
2293                 if (rc2 != 0)
2294                         GOTO(unlock, rc);
2295 err_destroy:
2296                 mdo_destroy(env, son, handle);
2297         }
2298 unlock:
2299         mdd_write_unlock(env, son);
2300         RETURN(rc);
2301 }
2302
2303 static int mdd_index_delete(const struct lu_env *env,
2304                             struct mdd_object *mdd_pobj,
2305                             struct lu_attr *cattr,
2306                             const struct lu_name *lname)
2307 {
2308         struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2309         struct thandle *handle;
2310         int rc;
2311         ENTRY;
2312
2313         handle = mdd_trans_create(env, mdd);
2314         if (IS_ERR(handle))
2315                 RETURN(PTR_ERR(handle));
2316
2317         rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2318                                       handle);
2319         if (rc != 0)
2320                 GOTO(stop, rc);
2321
2322         if (S_ISDIR(cattr->la_mode)) {
2323                 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2324                 if (rc != 0)
2325                         GOTO(stop, rc);
2326         }
2327
2328         /* Since this will only be used in the error handler path,
2329          * Let's set the thandle to be local and not mess the transno */
2330         handle->th_local = 1;
2331         rc = mdd_trans_start(env, mdd, handle);
2332         if (rc)
2333                 GOTO(stop, rc);
2334
2335         rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2336                                 S_ISDIR(cattr->la_mode), handle);
2337         if (rc)
2338                 GOTO(stop, rc);
2339 stop:
2340         mdd_trans_stop(env, mdd, rc, handle);
2341         RETURN(rc);
2342 }
2343
2344 /*
2345  * Create object and insert it into namespace.
2346  */
2347 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2348                       const struct lu_name *lname, struct md_object *child,
2349                       struct md_op_spec *spec, struct md_attr* ma)
2350 {
2351         struct mdd_thread_info  *info = mdd_env_info(env);
2352         struct lu_attr          *la = &info->mti_la_for_fix;
2353         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
2354         struct mdd_object       *son = md2mdd_obj(child);
2355         struct mdd_device       *mdd = mdo2mdd(pobj);
2356         struct lu_attr          *attr = &ma->ma_attr;
2357         struct thandle          *handle;
2358         struct lu_attr          *pattr = &info->mti_pattr;
2359         struct lu_buf           acl_buf;
2360         struct lu_buf           def_acl_buf;
2361         struct linkea_data      *ldata = &info->mti_link_data;
2362         const char              *name = lname->ln_name;
2363         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2364         int                      rc;
2365         int                      rc2;
2366         ENTRY;
2367
2368         /*
2369          * Two operations have to be performed:
2370          *
2371          *  - an allocation of a new object (->do_create()), and
2372          *
2373          *  - an insertion into a parent index (->dio_insert()).
2374          *
2375          * Due to locking, operation order is not important, when both are
2376          * successful, *but* error handling cases are quite different:
2377          *
2378          *  - if insertion is done first, and following object creation fails,
2379          *  insertion has to be rolled back, but this operation might fail
2380          *  also leaving us with dangling index entry.
2381          *
2382          *  - if creation is done first, is has to be undone if insertion
2383          *  fails, leaving us with leaked space, which is neither good, nor
2384          *  fatal.
2385          *
2386          * It seems that creation-first is simplest solution, but it is
2387          * sub-optimal in the frequent
2388          *
2389          *         $ mkdir foo
2390          *         $ mkdir foo
2391          *
2392          * case, because second mkdir is bound to create object, only to
2393          * destroy it immediately.
2394          *
2395          * To avoid this follow local file systems that do double lookup:
2396          *
2397          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
2398          *
2399          *     1. create            (mdd_object_create_internal())
2400          *
2401          *     2. insert            (__mdd_index_insert(), lookup again)
2402          */
2403
2404         rc = mdd_la_get(env, mdd_pobj, pattr);
2405         if (rc != 0)
2406                 RETURN(rc);
2407
2408         /* Sanity checks before big job. */
2409         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2410         if (rc)
2411                 RETURN(rc);
2412
2413         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2414                 GOTO(out_free, rc = -EINPROGRESS);
2415
2416         handle = mdd_trans_create(env, mdd);
2417         if (IS_ERR(handle))
2418                 GOTO(out_free, rc = PTR_ERR(handle));
2419
2420         acl_buf.lb_buf = info->mti_xattr_buf;
2421         acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2422         def_acl_buf.lb_buf = info->mti_key;
2423         def_acl_buf.lb_len = sizeof(info->mti_key);
2424         rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2425         if (rc < 0)
2426                 GOTO(out_stop, rc);
2427
2428         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2429
2430         memset(ldata, 0, sizeof(*ldata));
2431         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2432                 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2433
2434                 tfid.f_oid--;
2435                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2436                                         &tfid, lname, 1, 0, ldata);
2437         } else {
2438                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2439                                         mdd_object_fid(mdd_pobj),
2440                                         lname, 1, 0, ldata);
2441         }
2442
2443         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2444                                 handle, spec, ldata, &def_acl_buf, &acl_buf,
2445                                 hint);
2446         if (rc)
2447                 GOTO(out_stop, rc);
2448
2449         rc = mdd_trans_start(env, mdd, handle);
2450         if (rc)
2451                 GOTO(out_stop, rc);
2452
2453         rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2454                                &def_acl_buf, hint, handle);
2455         if (rc != 0)
2456                 GOTO(out_stop, rc);
2457
2458         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2459                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2460                 rc = __mdd_orphan_add(env, son, handle);
2461                 GOTO(out_volatile, rc);
2462         } else {
2463                 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2464                                         attr->la_mode, name, handle);
2465                 if (rc != 0)
2466                         GOTO(err_created, rc);
2467
2468                 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2469                               ldata, 1);
2470
2471                 /* update parent directory mtime/ctime */
2472                 *la = *attr;
2473                 la->la_valid = LA_CTIME | LA_MTIME;
2474                 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2475                 if (rc)
2476                         GOTO(err_insert, rc);
2477         }
2478
2479         EXIT;
2480 err_insert:
2481         if (rc != 0) {
2482                 int rc2;
2483
2484                 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2485                         rc2 = __mdd_orphan_del(env, son, handle);
2486                 else
2487                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
2488                                                  S_ISDIR(attr->la_mode),
2489                                                  handle);
2490                 if (rc2 != 0)
2491                         goto out_stop;
2492
2493 err_created:
2494                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2495                 if (S_ISDIR(attr->la_mode)) {
2496                         /* Drop the reference, no need to delete "."/"..",
2497                          * because the object is to be destroyed directly. */
2498                         rc2 = mdo_ref_del(env, son, handle);
2499                         if (rc2 != 0) {
2500                                 mdd_write_unlock(env, son);
2501                                 goto out_stop;
2502                         }
2503                 }
2504 out_volatile:
2505                 /* For volatile files drop one link immediately, since there is
2506                  * no filename in the namespace, and save any error returned. */
2507                 rc2 = mdo_ref_del(env, son, handle);
2508                 if (rc2 != 0) {
2509                         mdd_write_unlock(env, son);
2510                         if (unlikely(rc == 0))
2511                                 rc = rc2;
2512                         goto out_stop;
2513                 }
2514
2515                 /* Don't destroy the volatile object on success */
2516                 if (likely(rc != 0))
2517                         mdo_destroy(env, son, handle);
2518                 mdd_write_unlock(env, son);
2519         }
2520
2521         if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2522             likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2523                 rc = mdd_changelog_ns_store(env, mdd,
2524                                 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2525                                 S_ISREG(attr->la_mode) ? CL_CREATE :
2526                                 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2527                                 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2528                                 NULL, handle);
2529 out_stop:
2530         rc2 = mdd_trans_stop(env, mdd, rc, handle);
2531         if (rc == 0) {
2532                 /* If creation fails, it is most likely due to the remote update
2533                  * failure, because local transaction will mostly succeed at
2534                  * this stage. There is no easy way to rollback all of previous
2535                  * updates, so let's remove the object from namespace, and
2536                  * LFSCK should handle the orphan object. */
2537                 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2538                         mdd_index_delete(env, mdd_pobj, attr, lname);
2539                 rc = rc2;
2540         }
2541 out_free:
2542         if (is_vmalloc_addr(ldata->ld_buf))
2543                 /* if we vmalloced a large buffer drop it */
2544                 lu_buf_free(ldata->ld_buf);
2545
2546         /* The child object shouldn't be cached anymore */
2547         if (rc)
2548                 set_bit(LU_OBJECT_HEARD_BANSHEE,
2549                         &child->mo_lu.lo_header->loh_flags);
2550         return rc;
2551 }
2552
2553 /*
2554  * Get locks on parents in proper order
2555  * RETURN: < 0 - error, rename_order if successful
2556  */
2557 enum rename_order {
2558         MDD_RN_SAME,
2559         MDD_RN_SRCTGT,
2560         MDD_RN_TGTSRC
2561 };
2562
2563 static int mdd_rename_order(const struct lu_env *env,
2564                             struct mdd_device *mdd,
2565                             struct mdd_object *src_pobj,
2566                             const struct lu_attr *pattr,
2567                             struct mdd_object *tgt_pobj)
2568 {
2569         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2570         int rc;
2571         ENTRY;
2572
2573         if (src_pobj == tgt_pobj)
2574                 RETURN(MDD_RN_SAME);
2575
2576         /* compared the parent child relationship of src_p&tgt_p */
2577         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2578                 rc = MDD_RN_SRCTGT;
2579         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2580                 rc = MDD_RN_TGTSRC;
2581         } else {
2582                 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2583                                    NULL);
2584                 if (rc == -EREMOTE)
2585                         rc = 0;
2586
2587                 if (rc == 1)
2588                         rc = MDD_RN_TGTSRC;
2589                 else if (rc == 0)
2590                         rc = MDD_RN_SRCTGT;
2591         }
2592
2593         RETURN(rc);
2594 }
2595
2596 /* has not mdd_write{read}_lock on any obj yet. */
2597 static int mdd_rename_sanity_check(const struct lu_env *env,
2598                                    struct mdd_object *src_pobj,
2599                                    const struct lu_attr *pattr,
2600                                    struct mdd_object *tgt_pobj,
2601                                    const struct lu_attr *tpattr,
2602                                    struct mdd_object *sobj,
2603                                    const struct lu_attr *cattr,
2604                                    struct mdd_object *tobj,
2605                                    const struct lu_attr *tattr)
2606 {
2607         int rc = 0;
2608         ENTRY;
2609
2610         /* XXX: when get here, sobj must NOT be NULL,
2611          * the other case has been processed in cld_rename
2612          * before mdd_rename and enable MDS_PERM_BYPASS. */
2613         LASSERT(sobj);
2614
2615         rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2616         if (rc)
2617                 RETURN(rc);
2618
2619         /* XXX: when get here, "tobj == NULL" means tobj must
2620          * NOT exist (neither on remote MDS, such case has been
2621          * processed in cld_rename before mdd_rename and enable
2622          * MDS_PERM_BYPASS).
2623          * So check may_create, but not check may_unlink. */
2624         if (tobj == NULL)
2625                 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2626                                     (src_pobj != tgt_pobj));
2627         else
2628                 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2629                                     (src_pobj != tgt_pobj), 1);
2630
2631         if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2632                 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2633
2634         RETURN(rc);
2635 }
2636
2637 static int mdd_declare_rename(const struct lu_env *env,
2638                               struct mdd_device *mdd,
2639                               struct mdd_object *mdd_spobj,
2640                               struct mdd_object *mdd_tpobj,
2641                               struct mdd_object *mdd_sobj,
2642                               struct mdd_object *mdd_tobj,
2643                               const struct lu_name *tname,
2644                               const struct lu_name *sname,
2645                               struct md_attr *ma,
2646                               struct linkea_data *ldata,
2647                               struct thandle *handle)
2648 {
2649         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2650         int rc;
2651
2652         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2653         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2654
2655         LASSERT(mdd_spobj);
2656         LASSERT(mdd_tpobj);
2657         LASSERT(mdd_sobj);
2658
2659         /* name from source dir */
2660         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2661         if (rc)
2662                 return rc;
2663
2664         /* .. from source child */
2665         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2666                 /* source child can be directory,
2667                  * counted by source dir's nlink */
2668                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2669                 if (rc)
2670                         return rc;
2671                 if (mdd_spobj != mdd_tpobj) {
2672                         rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2673                                                       handle);
2674                         if (rc != 0)
2675                                 return rc;
2676
2677                         rc = mdo_declare_index_insert(env, mdd_sobj,
2678                                                       mdo2fid(mdd_tpobj),
2679                                                       S_IFDIR, dotdot, handle);
2680                         if (rc != 0)
2681                                 return rc;
2682                 }
2683
2684                 /* new target child can be directory,
2685                  * counted by target dir's nlink */
2686                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2687                 if (rc != 0)
2688                         return rc;
2689         }
2690
2691         la->la_valid = LA_CTIME | LA_MTIME;
2692         rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2693         if (rc != 0)
2694                 return rc;
2695
2696         rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2697         if (rc != 0)
2698                 return rc;
2699
2700         la->la_valid = LA_CTIME;
2701         rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2702         if (rc)
2703                 return rc;
2704
2705         rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2706                 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2707         if (rc)
2708                 return rc;
2709
2710         /* new name */
2711         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2712                                       mdd_object_type(mdd_sobj),
2713                                       tname->ln_name, handle);
2714         if (rc != 0)
2715                 return rc;
2716
2717         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2718                 /* delete target child in target parent directory */
2719                 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2720                                               handle);
2721                 if (rc)
2722                         return rc;
2723
2724                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2725                 if (rc)
2726                         return rc;
2727
2728                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2729                         /* target child can be directory,
2730                          * delete "." reference in target child directory */
2731                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2732                         if (rc)
2733                                 return rc;
2734
2735                         /* delete ".." reference in target parent directory */
2736                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2737                         if (rc)
2738                                 return rc;
2739                 }
2740
2741                 la->la_valid = LA_CTIME;
2742                 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2743                 if (rc)
2744                         return rc;
2745
2746                 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2747                 if (rc)
2748                         return rc;
2749         }
2750
2751         rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2752         if (rc)
2753                 return rc;
2754
2755         return rc;
2756 }
2757
2758 /* src object can be remote that is why we use only fid and type of object */
2759 static int mdd_rename(const struct lu_env *env,
2760                       struct md_object *src_pobj, struct md_object *tgt_pobj,
2761                       const struct lu_fid *lf, const struct lu_name *lsname,
2762                       struct md_object *tobj, const struct lu_name *ltname,
2763                       struct md_attr *ma)
2764 {
2765         const char *sname = lsname->ln_name;
2766         const char *tname = ltname->ln_name;
2767         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2768         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2769         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2770         struct mdd_device *mdd = mdo2mdd(src_pobj);
2771         struct mdd_object *mdd_sobj = NULL;                  /* source object */
2772         struct mdd_object *mdd_tobj = NULL;
2773         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2774         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2775         struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2776         struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2777         struct thandle *handle;
2778         struct linkea_data  *ldata = &mdd_env_info(env)->mti_link_data;
2779         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2780         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2781         bool is_dir;
2782         bool tobj_ref = 0;
2783         bool tobj_locked = 0;
2784         unsigned cl_flags = 0;
2785         int rc, rc2;
2786         ENTRY;
2787
2788         if (tobj)
2789                 mdd_tobj = md2mdd_obj(tobj);
2790
2791         mdd_sobj = mdd_object_find(env, mdd, lf);
2792         if (IS_ERR(mdd_sobj))
2793                 RETURN(PTR_ERR(mdd_sobj));
2794
2795         rc = mdd_la_get(env, mdd_sobj, cattr);
2796         if (rc)
2797                 GOTO(out_pending, rc);
2798
2799         rc = mdd_la_get(env, mdd_spobj, pattr);
2800         if (rc)
2801                 GOTO(out_pending, rc);
2802
2803         if (mdd_tobj) {
2804                 rc = mdd_la_get(env, mdd_tobj, tattr);
2805                 if (rc)
2806                         GOTO(out_pending, rc);
2807         }
2808
2809         rc = mdd_la_get(env, mdd_tpobj, tpattr);
2810         if (rc)
2811                 GOTO(out_pending, rc);
2812
2813         rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2814                                      mdd_sobj, cattr, mdd_tobj, tattr);
2815         if (rc)
2816                 GOTO(out_pending, rc);
2817
2818         rc = mdd_name_check(mdd, ltname);
2819         if (rc < 0)
2820                 GOTO(out_pending, rc);
2821
2822         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2823         rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2824         if (rc < 0)
2825                 GOTO(out_pending, rc);
2826
2827         handle = mdd_trans_create(env, mdd);
2828         if (IS_ERR(handle))
2829                 GOTO(out_pending, rc = PTR_ERR(handle));
2830
2831         memset(ldata, 0, sizeof(*ldata));
2832         mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj), lsname,
2833                            mdd_object_fid(mdd_tpobj), ltname, 1, 0, ldata);
2834         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2835                                 mdd_tobj, lsname, ltname, ma, ldata, handle);
2836         if (rc)
2837                 GOTO(stop, rc);
2838
2839         rc = mdd_trans_start(env, mdd, handle);
2840         if (rc)
2841                 GOTO(stop, rc);
2842
2843         is_dir = S_ISDIR(cattr->la_mode);
2844
2845         /* Remove source name from source directory */
2846         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2847         if (rc != 0)
2848                 GOTO(stop, rc);
2849
2850         /* "mv dir1 dir2" needs "dir1/.." link update */
2851         if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2852                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2853                 if (rc != 0)
2854                         GOTO(fixup_spobj2, rc);
2855
2856                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2857                                              dotdot, handle);
2858                 if (rc != 0)
2859                         GOTO(fixup_spobj, rc);
2860         }
2861
2862         if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2863                 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2864                 if (rc != 0)
2865                         /* tname might been renamed to something else */
2866                         GOTO(fixup_spobj, rc);
2867         }
2868
2869         /* Insert new fid with target name into target dir */
2870         rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2871                                 tname, handle);
2872         if (rc != 0)
2873                 GOTO(fixup_tpobj, rc);
2874
2875         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2876         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2877
2878         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2879         la->la_valid = LA_CTIME;
2880         rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2881         if (rc)
2882                 GOTO(fixup_tpobj, rc);
2883
2884         /* Update the linkEA for the source object */
2885         mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2886         rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2887                               mdo2fid(mdd_tpobj), ltname, handle, ldata,
2888                               0, 0);
2889         if (rc == -ENOENT)
2890                 /* Old files might not have EA entry */
2891                 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2892                               lsname, handle, NULL, 0);
2893         mdd_write_unlock(env, mdd_sobj);
2894         /* We don't fail the transaction if the link ea can't be
2895            updated -- fid2path will use alternate lookup method. */
2896         rc = 0;
2897
2898         /* Remove old target object
2899          * For tobj is remote case cmm layer has processed
2900          * and set tobj to NULL then. So when tobj is NOT NULL,
2901          * it must be local one.
2902          */
2903         if (tobj && mdd_object_exists(mdd_tobj)) {
2904                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2905                 tobj_locked = 1;
2906                 if (mdd_is_dead_obj(mdd_tobj)) {
2907                         /* shld not be dead, something is wrong */
2908                         CERROR("tobj is dead, something is wrong\n");
2909                         rc = -EINVAL;
2910                         goto cleanup;
2911                 }
2912                 mdo_ref_del(env, mdd_tobj, handle);
2913
2914                 /* Remove dot reference. */
2915                 if (S_ISDIR(tattr->la_mode))
2916                         mdo_ref_del(env, mdd_tobj, handle);
2917                 tobj_ref = 1;
2918
2919                 /* fetch updated nlink */
2920                 rc = mdd_la_get(env, mdd_tobj, tattr);
2921                 if (rc != 0) {
2922                         CERROR("%s: Failed to get nlink for tobj "
2923                                 DFID": rc = %d\n",
2924                                 mdd2obd_dev(mdd)->obd_name,
2925                                 PFID(tpobj_fid), rc);
2926                         GOTO(fixup_tpobj, rc);
2927                 }
2928
2929                 la->la_valid = LA_CTIME;
2930                 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2931                 if (rc != 0) {
2932                         CERROR("%s: Failed to set ctime for tobj "
2933                                 DFID": rc = %d\n",
2934                                 mdd2obd_dev(mdd)->obd_name,
2935                                 PFID(tpobj_fid), rc);
2936                         GOTO(fixup_tpobj, rc);
2937                 }
2938
2939                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2940                 ma->ma_attr = *tattr;
2941                 ma->ma_valid |= MA_INODE;
2942                 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2943                                        handle);
2944                 if (rc != 0) {
2945                         CERROR("%s: Failed to unlink tobj "
2946                                 DFID": rc = %d\n",
2947                                 mdd2obd_dev(mdd)->obd_name,
2948                                 PFID(tpobj_fid), rc);
2949                         GOTO(fixup_tpobj, rc);
2950                 }
2951
2952                 /* fetch updated nlink */
2953                 rc = mdd_la_get(env, mdd_tobj, tattr);
2954                 if (rc == -ENOENT) {
2955                         /* the object got removed, let's
2956                          * return the latest known attributes */
2957                         tattr->la_nlink = 0;
2958                         rc = 0;
2959                 } else if (rc != 0) {
2960                         CERROR("%s: Failed to get nlink for tobj "
2961                                 DFID": rc = %d\n",
2962                                 mdd2obd_dev(mdd)->obd_name,
2963                                 PFID(tpobj_fid), rc);
2964                         GOTO(fixup_tpobj, rc);
2965                 }
2966                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2967                 ma->ma_attr = *tattr;
2968                 ma->ma_valid |= MA_INODE;
2969
2970                 if (tattr->la_nlink == 0) {
2971                         cl_flags |= CLF_RENAME_LAST;
2972                         if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2973                                 cl_flags |= CLF_RENAME_LAST_EXISTS;
2974                 }
2975         }
2976
2977         la->la_valid = LA_CTIME | LA_MTIME;
2978         rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2979         if (rc)
2980                 GOTO(fixup_tpobj, rc);
2981
2982         if (mdd_spobj != mdd_tpobj) {
2983                 la->la_valid = LA_CTIME | LA_MTIME;
2984                 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2985                 if (rc != 0)
2986                         GOTO(fixup_tpobj, rc);
2987         }
2988
2989         EXIT;
2990
2991 fixup_tpobj:
2992         if (rc) {
2993                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2994                 if (rc2)
2995                         CWARN("tp obj fix error %d\n",rc2);
2996
2997                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2998                     !mdd_is_dead_obj(mdd_tobj)) {
2999                         if (tobj_ref) {
3000                                 mdo_ref_add(env, mdd_tobj, handle);
3001                                 if (is_dir)
3002                                         mdo_ref_add(env, mdd_tobj, handle);
3003                         }
3004
3005                         rc2 = __mdd_index_insert(env, mdd_tpobj,
3006                                                   mdo2fid(mdd_tobj),
3007                                                   mdd_object_type(mdd_tobj),
3008                                                   tname, handle);
3009                         if (rc2 != 0)
3010                                 CWARN("tp obj fix error: rc = %d\n", rc2);
3011                 }
3012         }
3013
3014 fixup_spobj:
3015         if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3016                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3017                 if (rc2)
3018                         CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3019                                mdd2obd_dev(mdd)->obd_name, rc2);
3020
3021
3022                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3023                                               dotdot, handle);
3024                 if (rc2 != 0)
3025                         CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3026                               mdd2obd_dev(mdd)->obd_name, rc2);
3027         }
3028
3029 fixup_spobj2:
3030         if (rc != 0) {
3031                 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3032                                          mdd_object_type(mdd_sobj), sname,
3033                                          handle);
3034                 if (rc2 != 0)
3035                         CWARN("sp obj fix error: rc = %d\n", rc2);
3036         }
3037
3038 cleanup:
3039         if (tobj_locked)
3040                 mdd_write_unlock(env, mdd_tobj);
3041
3042         if (rc == 0)
3043                 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3044                                             mdd_tobj, tpobj_fid, lf, spobj_fid,
3045                                             ltname, lsname, handle);
3046
3047 stop:
3048         mdd_trans_stop(env, mdd, rc, handle);
3049
3050 out_pending:
3051         mdd_object_put(env, mdd_sobj);
3052         return rc;
3053 }
3054
3055 /**
3056  * During migration once the parent FID has been changed,
3057  * we need update the parent FID in linkea.
3058  **/
3059 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3060                                             struct mdd_object *parent,
3061                                             struct mdd_object *newparent,
3062                                             struct mdd_object *child,
3063                                             const char *name, int namelen,
3064                                             struct thandle *handle,
3065                                             bool declare)
3066 {
3067         struct mdd_thread_info  *info = mdd_env_info(env);
3068         struct linkea_data      ldata = { NULL };
3069         struct lu_buf           *buf = &info->mti_link_buf;
3070         int                     count;
3071         int                     rc = 0;
3072
3073         ENTRY;
3074
3075         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3076         if (buf->lb_buf == NULL)
3077                 RETURN(-ENOMEM);
3078
3079         ldata.ld_buf = buf;
3080         rc = mdd_links_read(env, child, &ldata);
3081         if (rc != 0) {
3082                 if (rc == -ENOENT || rc == -ENODATA)
3083                         rc = 0;
3084                 RETURN(rc);
3085         }
3086
3087         LASSERT(ldata.ld_leh != NULL);
3088         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3089         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3090                 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3091                 struct lu_name lname;
3092                 struct lu_fid  fid;
3093
3094                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3095                                     &lname, &fid);
3096
3097                 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3098                     !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3099                         ldata.ld_lee = (struct link_ea_entry *)
3100                                        ((char *)ldata.ld_lee +
3101                                         ldata.ld_reclen);
3102                         continue;
3103                 }
3104
3105                 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3106                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3107                        lname.ln_namelen, lname.ln_name,
3108                        PFID(mdd_object_fid(newparent)));
3109                 /* update to the new parent fid */
3110                 linkea_entry_pack(ldata.ld_lee, &lname,
3111                                   mdd_object_fid(newparent));
3112                 if (declare)
3113                         rc = mdd_declare_links_add(env, child, handle, &ldata,
3114                                                    MLAO_IGNORE);
3115                 else
3116                         rc = mdd_links_write(env, child, &ldata, handle);
3117                 break;
3118         }
3119         RETURN(rc);
3120 }
3121
3122 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3123                                            struct mdd_object *parent,
3124                                            struct mdd_object *newparent,
3125                                            struct mdd_object *child,
3126                                            const char *name, int namelen,
3127                                            struct thandle *handle)
3128 {
3129         return mdd_linkea_update_child_internal(env, parent, newparent,
3130                                                 child, name,
3131                                                 namelen, handle, true);
3132 }
3133
3134 static int mdd_linkea_update_child(const struct lu_env *env,
3135                                    struct mdd_object *parent,
3136                                    struct mdd_object *newparent,
3137                                    struct mdd_object *child,
3138                                    const char *name, int namelen,
3139                                    struct thandle *handle)
3140 {
3141         return mdd_linkea_update_child_internal(env, parent, newparent,
3142                                                 child, name,
3143                                                 namelen, handle, false);
3144 }
3145
3146 static int mdd_update_linkea_internal(const struct lu_env *env,
3147                                       struct mdd_object *mdd_pobj,
3148                                       struct mdd_object *mdd_sobj,
3149                                       struct mdd_object *mdd_tobj,
3150                                       const struct lu_name *child_name,
3151                                       struct linkea_data *ldata,
3152                                       struct thandle *handle,
3153                                       int declare)
3154 {
3155         struct mdd_thread_info  *info = mdd_env_info(env);
3156         int                     count;
3157         int                     rc = 0;
3158         ENTRY;
3159
3160         LASSERT(ldata->ld_buf != NULL);
3161
3162 again:
3163         /* If it is mulitple links file, we need update the name entry for
3164          * all parent */
3165         LASSERT(ldata->ld_leh != NULL);
3166         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3167         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3168                 struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3169                 struct mdd_object       *pobj;
3170                 struct lu_name          lname;
3171                 struct lu_fid           fid;
3172
3173                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3174                                     &lname, &fid);
3175                 pobj = mdd_object_find(env, mdd, &fid);
3176                 if (IS_ERR(pobj)) {
3177                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3178                               mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3179                               PTR_ERR(pobj));
3180                         linkea_del_buf(ldata, &lname);
3181                         goto again;
3182                 }
3183
3184                 if (!mdd_object_exists(pobj)) {
3185                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3186                               mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3187                         linkea_del_buf(ldata, &lname);
3188                         mdd_object_put(env, pobj);
3189                         goto again;
3190                 }
3191
3192                 if (pobj == mdd_pobj &&
3193                     lname.ln_namelen == child_name->ln_namelen &&
3194                     strncmp(lname.ln_name, child_name->ln_name,
3195                             lname.ln_namelen) == 0) {
3196                         CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3197                               mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3198                               PFID(&fid));
3199                         linkea_del_buf(ldata, &lname);
3200                         mdd_object_put(env, pobj);
3201                         goto again;
3202                 }
3203
3204                 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3205                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3206                        PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3207
3208                 if (declare) {
3209                         /* Remove source name from source directory */
3210                         /* Insert new fid with target name into target dir */
3211                         rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3212                                                       handle);
3213                         if (rc != 0)
3214                                 GOTO(next_put, rc);
3215
3216                         rc = mdo_declare_index_insert(env, pobj,
3217                                         mdd_object_fid(mdd_tobj),
3218                                         mdd_object_type(mdd_tobj),
3219                                         lname.ln_name, handle);
3220                         if (rc != 0)
3221                                 GOTO(next_put, rc);
3222
3223                         rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3224                         if (rc)
3225                                 GOTO(next_put, rc);
3226
3227                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3228                         if (rc)
3229                                 GOTO(next_put, rc);
3230                 } else {
3231                         char *tmp_name = info->mti_key;
3232
3233                         if (lname.ln_namelen >= sizeof(info->mti_key)) {
3234                                 /* lnamelen is too big(> NAME_MAX + 16),
3235                                  * something wrong about this linkea, let's
3236                                  * skip it */
3237                                 linkea_del_buf(ldata, &lname);
3238                                 mdd_object_put(env, pobj);
3239                                 goto again;
3240                         }
3241
3242                         /* Note: lname might be without \0 at the end, see
3243                          * linkea_entry_unpack(), let's add extra \0 by
3244                          * snprintf */
3245                         snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3246                                  lname.ln_namelen, lname.ln_name);
3247                         lname.ln_name = tmp_name;
3248
3249                         /* Let's check if this linkEA still valid, before
3250                          * it might be packed into the RPC buffer. */
3251                         rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3252                                         &info->mti_fid, NULL);
3253                         if (rc < 0 ||
3254                             !lu_fid_eq(&info->mti_fid,
3255                                         mdd_object_fid(mdd_sobj))) {
3256                                 /* skip invalid linkea entry */
3257                                 linkea_del_buf(ldata, &lname);
3258                                 mdd_object_put(env, pobj);
3259                                 goto again;
3260                         }
3261
3262                         rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3263                         if (rc != 0)
3264                                 GOTO(next_put, rc);
3265
3266                         rc = __mdd_index_insert(env, pobj,
3267                                         mdd_object_fid(mdd_tobj),
3268                                         mdd_object_type(mdd_tobj),
3269                                         tmp_name, handle);
3270                         if (rc != 0)
3271                                 GOTO(next_put, rc);
3272
3273                         mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3274                         rc = mdo_ref_add(env, mdd_tobj, handle);
3275                         mdd_write_unlock(env, mdd_tobj);
3276                         if (rc)
3277                                 GOTO(next_put, rc);
3278
3279                         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3280                         mdo_ref_del(env, mdd_sobj, handle);
3281                         mdd_write_unlock(env, mdd_sobj);
3282                 }
3283 next_put:
3284                 mdd_object_put(env, pobj);
3285                 if (rc != 0)
3286                         break;
3287
3288                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3289                                                          ldata->ld_reclen);
3290         }
3291
3292         RETURN(rc);
3293 }
3294
3295 static int mdd_migrate_xattrs(const struct lu_env *env,
3296                               struct mdd_object *mdd_sobj,
3297                               struct mdd_object *mdd_tobj)
3298 {
3299         struct mdd_thread_info  *info = mdd_env_info(env);
3300         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3301         char                    *xname;
3302         struct thandle          *handle;
3303         struct lu_buf           xbuf;
3304         int                     xlen;
3305         int                     rem;
3306         int                     xsize;
3307         int                     list_xsize;
3308         struct lu_buf           list_xbuf;
3309         int                     rc;
3310         int                     rc1;
3311
3312         /* retrieve xattr list from the old object */
3313         list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3314         if (list_xsize == -ENODATA)
3315                 return 0;
3316
3317         if (list_xsize < 0)
3318                 return list_xsize;
3319
3320         lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3321         if (info->mti_big_buf.lb_buf == NULL)
3322                 return -ENOMEM;
3323
3324         list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3325         list_xbuf.lb_len = list_xsize;
3326         rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3327         if (rc < 0)
3328                 return rc;
3329         rc = 0;
3330         rem = list_xsize;
3331         xname = list_xbuf.lb_buf;
3332         while (rem > 0) {
3333                 xlen = strnlen(xname, rem - 1) + 1;
3334                 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3335                     strcmp(XATTR_NAME_LMA, xname) == 0 ||
3336                     strcmp(XATTR_NAME_LMV, xname) == 0)
3337                         goto next;
3338
3339                 /* For directory, if there are default layout, migrate here */
3340                 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3341                     !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3342                         goto next;
3343
3344                 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3345                 if (xsize == -ENODATA)
3346                         goto next;
3347                 if (xsize < 0)
3348                         GOTO(out, rc);
3349
3350                 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3351                 if (info->mti_link_buf.lb_buf == NULL)
3352                         GOTO(out, rc = -ENOMEM);
3353
3354                 xbuf.lb_len = xsize;
3355                 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3356                 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3357                 if (rc == -ENODATA)
3358                         goto next;
3359                 if (rc < 0)
3360                         GOTO(out, rc);
3361
3362                 handle = mdd_trans_create(env, mdd);
3363                 if (IS_ERR(handle))
3364                         GOTO(out, rc = PTR_ERR(handle));
3365
3366                 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3367                                            handle);
3368                 if (rc != 0)
3369                         GOTO(stop_trans, rc);
3370                 /* Note: this transaction is part of migration, and it is not
3371                  * the last step of migration, so we set th_local = 1 to avoid
3372                  * update last rcvd for this transaction */
3373                 handle->th_local = 1;
3374                 rc = mdd_trans_start(env, mdd, handle);
3375                 if (rc != 0)
3376                         GOTO(stop_trans, rc);
3377
3378                 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3379                 if (rc == -EEXIST)
3380                         GOTO(stop_trans, rc = 0);
3381
3382                 if (rc != 0)
3383                         GOTO(stop_trans, rc);
3384 stop_trans:
3385                 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3386                 if (rc == 0)
3387                         rc = rc1;
3388                 if (rc != 0)
3389                         GOTO(out, rc);
3390 next:
3391                 rem -= xlen;
3392                 memmove(xname, xname + xlen, rem);
3393         }
3394 out:
3395         return rc;
3396 }
3397
3398 static int mdd_declare_migrate_create(const struct lu_env *env,
3399                                       struct mdd_object *mdd_pobj,
3400                                       struct mdd_object *mdd_sobj,
3401                                       struct mdd_object *mdd_tobj,
3402                                       struct md_op_spec *spec,
3403                                       struct lu_attr *la,
3404                                       union lmv_mds_md *mgr_ea,
3405                                       struct linkea_data *ldata,
3406                                       struct thandle *handle)
3407 {
3408         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3409         const struct lu_buf     *buf;
3410         int                     rc;
3411         int                     mgr_easize;
3412
3413         rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3414                                                 handle, spec, NULL);
3415         if (rc != 0)
3416                 return rc;
3417
3418         rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3419                                            handle);
3420         if (rc != 0)
3421                 return rc;
3422
3423         if (S_ISLNK(la->la_mode)) {
3424                 const char *target_name = spec->u.sp_symname;
3425                 int sym_len = strlen(target_name);
3426                 const struct lu_buf *buf;
3427
3428                 buf = mdd_buf_get_const(env, target_name, sym_len);
3429                 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3430                                              buf, 0, handle);
3431                 if (rc != 0)
3432                         return rc;
3433         } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3434                 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3435                                            MLAO_IGNORE);
3436                 if (rc != 0)
3437                         return rc;
3438         }
3439
3440         if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3441                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3442                                         spec->u.sp_ea.eadatalen);
3443                 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3444                                            0, handle);
3445                 if (rc)
3446                         return rc;
3447         }
3448
3449         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3450         buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3451         rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3452                                    0, handle);
3453         if (rc)
3454                 return rc;
3455
3456         la_flag->la_valid = LA_FLAGS;
3457         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3458         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3459
3460         return rc;
3461 }
3462
3463 static int mdd_migrate_create(const struct lu_env *env,
3464                               struct mdd_object *mdd_pobj,
3465                               struct mdd_object *mdd_sobj,
3466                               struct mdd_object *mdd_tobj,
3467                               const struct lu_name *lname,
3468                               struct lu_attr *la)
3469 {
3470         struct mdd_thread_info  *info = mdd_env_info(env);
3471         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3472         struct md_op_spec       *spec = &info->mti_spec;
3473         struct lu_buf           lmm_buf = { NULL };
3474         struct lu_buf           link_buf = { NULL };
3475         const struct lu_buf     *buf;
3476         struct thandle          *handle;
3477         struct lmv_mds_md_v1    *mgr_ea;
3478         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3479         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3480         int                     mgr_easize;
3481         struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
3482         int                     rc;
3483         ENTRY;
3484
3485         /* prepare spec for create */
3486         memset(spec, 0, sizeof(*spec));
3487         spec->sp_cr_lookup = 0;
3488         spec->sp_feat = &dt_directory_features;
3489         if (S_ISLNK(la->la_mode)) {
3490                 buf = lu_buf_check_and_alloc(
3491                                 &mdd_env_info(env)->mti_big_buf,
3492                                 la->la_size + 1);
3493                 link_buf = *buf;
3494                 link_buf.lb_len = la->la_size + 1;
3495                 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3496                 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3497                 if (rc <= 0) {
3498                         rc = rc != 0 ? rc : -EFAULT;
3499                         CERROR("%s: "DFID" readlink failed: rc = %d\n",
3500                                mdd2obd_dev(mdd)->obd_name,
3501                                PFID(mdd_object_fid(mdd_sobj)), rc);
3502                         RETURN(rc);
3503                 }
3504                 spec->u.sp_symname = link_buf.lb_buf;
3505         } else if (S_ISREG(la->la_mode)) {
3506                 /* retrieve lov of the old object */
3507                 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3508                 if (rc != 0 && rc != -ENODATA)
3509                         RETURN(rc);
3510                 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3511                         spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3512                         spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3513                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3514                 }
3515         } else if (S_ISDIR(la->la_mode)) {
3516                 rc = mdd_links_read(env, mdd_sobj, ldata);
3517                 if (rc == -ENODATA) {
3518                         /* ignore the non-linkEA error */
3519                         ldata = NULL;
3520                         rc = 0;
3521                 }
3522                 if (rc < 0)
3523                         RETURN(rc);
3524         }
3525
3526         mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3527         memset(mgr_ea, 0, sizeof(*mgr_ea));
3528         mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3529         mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3530         mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3531         mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3532         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3533         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3534
3535         mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3536
3537         handle = mdd_trans_create(env, mdd);
3538         if (IS_ERR(handle))
3539                 GOTO(out_free, rc = PTR_ERR(handle));
3540
3541         /* Note: this transaction is part of migration, and it is not
3542          * the last step of migration, so we set th_local = 1 to avoid
3543          * update last rcvd for this transaction */
3544         handle->th_local = 1;
3545         rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3546                                         spec, la,
3547                                         (union lmv_mds_md *)info->mti_xattr_buf,
3548                                         ldata, handle);
3549         if (rc != 0)
3550                 GOTO(stop_trans, rc);
3551
3552         rc = mdd_trans_start(env, mdd, handle);
3553         if (rc != 0)
3554                 GOTO(stop_trans, rc);
3555
3556         /* don't set nlink from the original object */
3557         la->la_valid &= ~LA_NLINK;
3558
3559         /* create the target object */
3560         rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3561                                hint, handle);
3562         if (rc != 0)
3563                 GOTO(stop_trans, rc);
3564
3565         if (S_ISDIR(la->la_mode) && ldata != NULL) {
3566                 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3567                 if (rc != 0)
3568                         GOTO(stop_trans, rc);
3569         }
3570
3571         /* Set MIGRATE EA on the source inode, so once the migration needs
3572          * to be re-done during failover, the re-do process can locate the
3573          * target object which is already being created. */
3574         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3575         buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3576         rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3577         if (rc != 0)
3578                 GOTO(stop_trans, rc);
3579
3580         /* Set immutable flag, so any modification is disabled until
3581          * the migration is done. Once the migration is interrupted,
3582          * if the resume process find the migrating object has both
3583          * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3584          * flag and approve the migration */
3585         la_flag->la_valid = LA_FLAGS;
3586         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3587         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3588 stop_trans:
3589         if (handle != NULL) {
3590                 int rc1;
3591
3592                 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3593                 if (rc == 0)
3594                         rc = rc1;
3595         }
3596 out_free:
3597         if (lmm_buf.lb_buf != NULL)
3598                 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3599         RETURN(rc);
3600 }
3601
3602 static int mdd_migrate_entries(const struct lu_env *env,
3603                                struct mdd_object *mdd_sobj,
3604                                struct mdd_object *mdd_tobj)
3605 {
3606         struct dt_object        *next = mdd_object_child(mdd_sobj);
3607         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3608         struct dt_object        *dt_tobj = mdd_object_child(mdd_tobj);
3609         struct thandle          *handle;
3610         struct dt_it            *it;
3611         const struct dt_it_ops  *iops;
3612         int                      rc;
3613         int                      result;
3614         struct lu_dirent        *ent;
3615         ENTRY;
3616
3617         OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3618         if (ent == NULL)
3619                 RETURN(-ENOMEM);
3620
3621         if (!dt_try_as_dir(env, next))
3622                 GOTO(out_ent, rc = -ENOTDIR);
3623         /*
3624          * iterate directories
3625          */
3626         iops = &next->do_index_ops->dio_it;
3627         it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3628         if (IS_ERR(it))
3629                 GOTO(out_ent, rc = PTR_ERR(it));
3630
3631         rc = iops->load(env, it, 0);
3632         if (rc == 0)
3633                 rc = iops->next(env, it);
3634         else if (rc > 0)
3635                 rc = 0;
3636         /*
3637          * At this point and across for-loop:
3638          *
3639          *  rc == 0 -> ok, proceed.
3640          *  rc >  0 -> end of directory.
3641          *  rc <  0 -> error.
3642          */
3643         do {
3644                 struct mdd_object       *child;
3645                 char                    *name = mdd_env_info(env)->mti_key;
3646                 int                     len;
3647                 int                     recsize;
3648                 int                     is_dir;
3649                 bool                    target_exist = false;
3650                 int                     rc1;
3651
3652                 len = iops->key_size(env, it);
3653                 if (len == 0)
3654                         goto next;
3655
3656                 result = iops->rec(env, it, (struct dt_rec *)ent,
3657                                    LUDA_FID | LUDA_TYPE);
3658                 if (result == -ESTALE)
3659                         goto next;
3660                 if (result != 0) {
3661                         rc = result;
3662                         goto out;
3663                 }
3664
3665                 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3666                 recsize = le16_to_cpu(ent->lde_reclen);
3667
3668                 /* Insert new fid with target name into target dir */
3669                 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3670                     (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3671                      ent->lde_name[1] == '.'))
3672                         goto next;
3673
3674                 child = mdd_object_find(env, mdd, &ent->lde_fid);
3675                 if (IS_ERR(child))
3676                         GOTO(out, rc = PTR_ERR(child));
3677
3678                 mdd_write_lock(env, child, MOR_SRC_CHILD);
3679                 is_dir = S_ISDIR(mdd_object_type(child));
3680
3681                 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3682
3683                 /* Check whether the name has been inserted to the target */
3684                 if (dt_try_as_dir(env, dt_tobj)) {
3685                         struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3686
3687                         rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3688                                        (struct dt_key *)name);
3689                         if (unlikely(rc == 0))
3690                                 target_exist = true;
3691                 }
3692
3693                 handle = mdd_trans_create(env, mdd);
3694                 if (IS_ERR(handle))
3695                         GOTO(out_put, rc = PTR_ERR(handle));
3696
3697                 /* Note: this transaction is part of migration, and it is not
3698                  * the last step of migration, so we set th_local = 1 to avoid
3699                  * updating last rcvd for this transaction */
3700                 handle->th_local = 1;
3701                 if (likely(!target_exist)) {
3702                         rc = mdo_declare_index_insert(env, mdd_tobj,
3703                                                       &ent->lde_fid,
3704                                                       mdd_object_type(child),
3705                                                       name, handle);
3706                         if (rc != 0)
3707                                 GOTO(out_put, rc);
3708
3709                         if (is_dir) {
3710                                 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3711                                 if (rc != 0)
3712                                         GOTO(out_put, rc);
3713                         }
3714                 }
3715
3716                 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3717                 if (rc != 0)
3718                         GOTO(out_put, rc);
3719
3720                 if (is_dir) {
3721                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3722                         if (rc != 0)
3723                                 GOTO(out_put, rc);
3724
3725                         /* Update .. for child */
3726                         rc = mdo_declare_index_delete(env, child, dotdot,
3727                                                       handle);
3728                         if (rc != 0)
3729                                 GOTO(out_put, rc);
3730
3731                         rc = mdo_declare_index_insert(env, child,
3732                                                       mdd_object_fid(mdd_tobj),
3733                                                       S_IFDIR, dotdot, handle);
3734                         if (rc != 0)
3735                                 GOTO(out_put, rc);
3736                 }
3737
3738                 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3739                                                      child, name,
3740                                                      strlen(name),
3741                                                      handle);
3742                 if (rc != 0)
3743                         GOTO(out_put, rc);
3744
3745                 rc = mdd_trans_start(env, mdd, handle);
3746                 if (rc != 0) {
3747                         CERROR("%s: transaction start failed: rc = %d\n",
3748                                mdd2obd_dev(mdd)->obd_name, rc);
3749                         GOTO(out_put, rc);
3750                 }
3751
3752                 if (likely(!target_exist)) {
3753                         rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3754                                                 mdd_object_type(child),
3755                                                 name, handle);
3756                         if (rc != 0)
3757                                 GOTO(out_put, rc);
3758                 }
3759
3760                 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3761                 if (rc != 0)
3762                         GOTO(out_put, rc);
3763
3764                 if (is_dir) {
3765                         rc = __mdd_index_delete_only(env, child, dotdot,
3766                                                      handle);
3767                         if (rc != 0)
3768                                 GOTO(out_put, rc);
3769
3770                         rc = __mdd_index_insert_only(env, child,
3771                                          mdd_object_fid(mdd_tobj), S_IFDIR,
3772                                          dotdot, handle);
3773                         if (rc != 0)
3774                                 GOTO(out_put, rc);
3775                 }
3776
3777                 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3778                                              child, name,
3779                                              strlen(name), handle);
3780
3781 out_put:
3782                 mdd_write_unlock(env, child);
3783                 mdd_object_put(env, child);
3784                 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3785                 if (rc == 0)
3786                         rc = rc1;
3787
3788                 if (rc != 0)
3789                         GOTO(out, rc);
3790 next:
3791                 result = iops->next(env, it);
3792                 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3793                         GOTO(out, rc = -EINTR);
3794
3795                 if (result == -ESTALE)
3796                         goto next;
3797         } while (result == 0);
3798 out:
3799         iops->put(env, it);
3800         iops->fini(env, it);
3801 out_ent:
3802         OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3803         RETURN(rc);
3804 }
3805
3806 static int mdd_declare_update_linkea(const struct lu_env *env,
3807                                      struct mdd_object *mdd_pobj,
3808                                      struct mdd_object *mdd_sobj,
3809                                      struct mdd_object *mdd_tobj,
3810                                      const struct lu_name *child_name,
3811                                      struct linkea_data *ldata,
3812                                      struct thandle *handle)
3813 {
3814         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3815                                           child_name, ldata, handle, 1);
3816 }
3817
3818 static int mdd_update_linkea(const struct lu_env *env,
3819                              struct mdd_object *mdd_pobj,
3820                              struct mdd_object *mdd_sobj,
3821                              struct mdd_object *mdd_tobj,
3822                              const struct lu_name *child_name,
3823                              struct linkea_data *ldata,
3824                              struct thandle *handle)
3825 {
3826         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3827                                           child_name, ldata, handle, 0);
3828 }
3829
3830 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3831                                            struct mdd_object *mdd_pobj,
3832                                            struct mdd_object *mdd_sobj,
3833                                            struct mdd_object *mdd_tobj,
3834                                            const struct lu_name *lname,
3835                                            struct lu_attr *la,
3836                                            struct lu_attr *parent_la,
3837                                            struct linkea_data *ldata,
3838                                            struct thandle *handle)
3839 {
3840         struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3841         struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3842         int rc;
3843
3844         /* Revert IMMUTABLE flag */
3845         la_flag->la_valid = LA_FLAGS;
3846         la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3847         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3848         if (rc != 0)
3849                 return rc;
3850
3851         /* delete entry from source dir */
3852         rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3853         if (rc != 0)
3854                 return rc;
3855
3856         if (ldata->ld_buf != NULL) {
3857                 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3858                                                mdd_tobj, lname, ldata, handle);
3859                 if (rc != 0)
3860                         return rc;
3861         }
3862
3863         if (S_ISREG(mdd_object_type(mdd_sobj))) {
3864                 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3865                                            handle);
3866                 if (rc != 0)
3867                         return rc;
3868
3869                 handle->th_complex = 1;
3870                 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3871                                            XATTR_NAME_FID,
3872                                            LU_XATTR_REPLACE, handle);
3873                 if (rc < 0)
3874                         return rc;
3875         }
3876
3877         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3878                 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3879                 if (rc != 0)
3880                         return rc;
3881         }
3882
3883         /* new name */
3884         rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3885                                       mdd_object_type(mdd_tobj),
3886                                       lname->ln_name, handle);
3887         if (rc != 0)
3888                 return rc;
3889
3890         rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3891         if (rc != 0)
3892                 return rc;
3893
3894         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3895                 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3896                 if (rc != 0)
3897                         return rc;
3898         }
3899
3900         /* delete old object */
3901         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3902         if (rc != 0)
3903                 return rc;
3904
3905         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3906                 /* delete old object */
3907                 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3908                 if (rc != 0)
3909                         return rc;
3910                 /* set nlink to 0 */
3911                 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3912                 if (rc != 0)
3913                         return rc;
3914         }
3915
3916         rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3917         if (rc)
3918                 return rc;
3919
3920         rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3921         if (rc != 0)
3922                 return rc;
3923
3924         rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3925
3926         return rc;
3927 }
3928
3929 static int mdd_migrate_update_name(const struct lu_env *env,
3930                                    struct mdd_object *mdd_pobj,
3931                                    struct mdd_object *mdd_sobj,
3932                                    struct mdd_object *mdd_tobj,
3933                                    const struct lu_name *lname,
3934                                    struct md_attr *ma)
3935 {
3936         struct lu_attr          *p_la = MDD_ENV_VAR(env, la_for_fix);
3937         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
3938         struct lu_attr          *la_flag = MDD_ENV_VAR(env, tattr);
3939         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3940         struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
3941         struct thandle          *handle;
3942         int                     is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3943         const char              *name = lname->ln_name;
3944         int                     rc;
3945         ENTRY;
3946
3947         /* update time for parent */
3948         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3949         p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3950         p_la->la_valid = LA_CTIME;
3951
3952         rc = mdd_la_get(env, mdd_sobj, so_attr);
3953         if (rc != 0)
3954                 RETURN(rc);
3955
3956         ldata->ld_buf = NULL;
3957         rc = mdd_links_read(env, mdd_sobj, ldata);
3958         if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3959                 RETURN(rc);
3960
3961         handle = mdd_trans_create(env, mdd);
3962         if (IS_ERR(handle))
3963                 RETURN(PTR_ERR(handle));
3964
3965         rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3966                                              lname, so_attr, p_la, ldata,
3967                                              handle);
3968         if (rc != 0) {
3969                 /* If the migration can not be fit in one transaction, just
3970                  * leave it in the original MDT */
3971                 if (rc == -E2BIG)
3972                         GOTO(stop_trans, rc = 0);
3973                 else
3974                         GOTO(stop_trans, rc);
3975         }
3976
3977         CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3978                mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3979                PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3980                PFID(mdd_object_fid(mdd_tobj)));
3981
3982         rc = mdd_trans_start(env, mdd, handle);
3983         if (rc != 0)
3984                 GOTO(stop_trans, rc);
3985
3986         /* Revert IMMUTABLE flag */
3987         la_flag->la_valid = LA_FLAGS;
3988         la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3989         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3990         if (rc != 0)
3991                 GOTO(stop_trans, rc);
3992
3993         /* Remove source name from source directory */
3994         rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3995         if (rc != 0)
3996                 GOTO(stop_trans, rc);
3997
3998         if (ldata->ld_buf != NULL) {
3999                 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
4000                                        lname, ldata, handle);
4001                 if (rc != 0)
4002                         GOTO(stop_trans, rc);
4003
4004                 /*  linkea update might decrease the source object
4005                  *  nlink, let's get the attr again after ref_del */
4006                 rc = mdd_la_get(env, mdd_sobj, so_attr);
4007                 if (rc != 0)
4008                         GOTO(stop_trans, rc);
4009         }
4010
4011         if (S_ISREG(so_attr->la_mode)) {
4012                 if (so_attr->la_nlink == 1) {
4013                         rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4014                                            handle);
4015                         if (rc != 0 && rc != -ENODATA)
4016                                 GOTO(stop_trans, rc);
4017
4018                         rc = mdo_xattr_set(env, mdd_tobj, NULL,
4019                                            XATTR_NAME_FID,
4020                                            LU_XATTR_REPLACE, handle);
4021                         if (rc < 0)
4022                                 GOTO(stop_trans, rc);
4023                 }
4024         }
4025
4026         /* Insert new fid with target name into target dir */
4027         rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4028                                 mdd_object_type(mdd_tobj), name, handle);
4029         if (rc != 0)
4030                 GOTO(stop_trans, rc);
4031
4032         linkea_add_buf(ldata, lname, mdd_object_fid(mdd_pobj));
4033         rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
4034                            ldata, 1);
4035         if (rc != 0)
4036                 GOTO(stop_trans, rc);
4037
4038         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4039
4040         mdd_sobj->mod_flags |= DEAD_OBJ;
4041         rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4042         if (rc != 0)
4043                 GOTO(out_unlock, rc);
4044
4045         rc = __mdd_orphan_add(env, mdd_sobj, handle);
4046         if (rc != 0)
4047                 GOTO(out_unlock, rc);
4048
4049         mdo_ref_del(env, mdd_sobj, handle);
4050         if (is_dir)
4051                 mdo_ref_del(env, mdd_sobj, handle);
4052
4053         /* Get the attr again after ref_del */
4054         rc = mdd_la_get(env, mdd_sobj, so_attr);
4055         if (rc != 0)
4056                 GOTO(out_unlock, rc);
4057
4058         ma->ma_attr = *so_attr;
4059         ma->ma_valid |= MA_INODE;
4060
4061         rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4062         if (rc != 0)
4063                 GOTO(out_unlock, rc);
4064
4065         rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4066                                mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4067                                mdo2fid(mdd_pobj), lname, lname, handle);
4068         if (rc != 0) {
4069                 CWARN("%s: changelog for migrate %s "DFID
4070                       "under "DFID" failed: rc = %d\n",
4071                       mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4072                       PFID(mdd_object_fid(mdd_sobj)),
4073                       PFID(mdd_object_fid(mdd_pobj)), rc);
4074                 /* Sigh, there are no easy way to migrate back the object, so
4075                  * let's reset the result to 0 for now XXX */
4076                 rc = 0;
4077         }
4078 out_unlock:
4079         mdd_write_unlock(env, mdd_sobj);
4080
4081 stop_trans:
4082         mdd_trans_stop(env, mdd, rc, handle);
4083
4084         RETURN(rc);
4085 }
4086
4087 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4088                           const struct lu_fid *fid, __u32 *mdt_index)
4089 {
4090         struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4091         struct seq_server_site *ss;
4092         int rc;
4093
4094         ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4095
4096         range->lsr_flags = LU_SEQ_RANGE_MDT;
4097         rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4098         if (rc != 0)
4099                 return rc;
4100
4101         *mdt_index = range->lsr_index;
4102
4103         return 0;
4104 }
4105 /**
4106  * Check whether we should migrate the file/dir
4107  * return val
4108  *      < 0  permission check failed or other error.
4109  *      = 0  the file can be migrated.
4110  *      > 0  the file does not need to be migrated, mostly
4111  *           for multiple link file
4112  **/
4113 static int mdd_migrate_sanity_check(const struct lu_env *env,
4114                                     struct mdd_object *pobj,
4115                                     const struct lu_attr *pattr,
4116                                     struct mdd_object *sobj,
4117                                     struct lu_attr *sattr)
4118 {
4119         struct mdd_thread_info  *info = mdd_env_info(env);
4120         struct linkea_data      *ldata = &info->mti_link_data;
4121         struct mdd_device       *mdd = mdo2mdd(&pobj->mod_obj);
4122         int                     mgr_easize;
4123         struct lu_buf           *mgr_buf;
4124         int                     count;
4125         int                     rc;
4126         __u64 mdt_index;
4127         ENTRY;
4128
4129         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4130         mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4131         if (mgr_buf->lb_buf == NULL)
4132                 RETURN(-ENOMEM);
4133
4134         rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4135         if (rc > 0) {
4136                 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4137
4138                 /* If the object has migrateEA, it means IMMUTE flag
4139                  * is being set by previous migration process, so it
4140                  * needs to override the IMMUTE flag, otherwise the
4141                  * following sanity check will fail */
4142                 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4143                                                 LMV_HASH_FLAG_MIGRATION) {
4144                         struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4145
4146                         sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4147                         CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4148                                mdd2obd_dev(mdd)->obd_name,
4149                                PFID(mdd_object_fid(sobj)));
4150                 }
4151         }
4152
4153         rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4154                                      sobj, sattr, NULL, NULL);
4155         if (rc != 0)
4156                 RETURN(rc);
4157
4158         /* Then it will check if the file should be migrated. If the file
4159          * has mulitple links, we only need migrate the file if all of its
4160          * entries has been migrated to the remote MDT */
4161         if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4162                 RETURN(0);
4163
4164         rc = mdd_links_read(env, sobj, ldata);
4165         if (rc != 0) {
4166                 /* For multiple links files, if there are no linkEA data at all,
4167                  * means the file might be created before linkEA is enabled, and
4168                  * all of its links should not be migrated yet, otherwise it
4169                  * should have some linkEA there */
4170                 if (rc == -ENOENT || rc == -ENODATA)
4171                         RETURN(1);
4172                 RETURN(rc);
4173         }
4174
4175         mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4176         /* If there are still links locally, then the file will not be
4177          * migrated. */
4178         LASSERT(ldata->ld_leh != NULL);
4179         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4180         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4181                 struct lu_name          lname;
4182                 struct lu_fid           fid;
4183                 __u32                   parent_mdt_index;
4184
4185                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4186                                     &lname, &fid);
4187                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4188                                                          ldata->ld_reclen);
4189
4190                 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4191                 if (rc != 0)
4192                         RETURN(rc);
4193
4194                 /* Migrate the object only if none of its parents are on the
4195                  * current MDT. */
4196                 if (parent_mdt_index != mdt_index)
4197                         continue;
4198
4199                 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4200                        PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4201                        lname.ln_name, PFID(&fid));
4202                 rc = 1;
4203                 break;
4204         }
4205
4206         RETURN(rc);
4207 }
4208
4209 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4210                        struct md_object *sobj, const struct lu_name *lname,
4211                        struct md_object *tobj, struct md_attr *ma)
4212 {
4213         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
4214         struct mdd_device       *mdd = mdo2mdd(pobj);
4215         struct mdd_object       *mdd_sobj = md2mdd_obj(sobj);
4216         struct mdd_object       *mdd_tobj = md2mdd_obj(tobj);
4217         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
4218         struct lu_attr          *pattr = MDD_ENV_VAR(env, pattr);
4219         int                     rc;
4220
4221         ENTRY;
4222         /* If the file will being migrated, it will check whether
4223          * the file is being opened by someone else right now */
4224         mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4225         if (mdd_sobj->mod_count > 0) {
4226                 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4227                        mdd2obd_dev(mdd)->obd_name,
4228                        PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4229                        mdd_sobj->mod_count, -EBUSY);
4230                 mdd_read_unlock(env, mdd_sobj);
4231                 GOTO(put, rc = -EBUSY);
4232         }
4233         mdd_read_unlock(env, mdd_sobj);
4234
4235         rc = mdd_la_get(env, mdd_sobj, so_attr);
4236         if (rc != 0)
4237                 GOTO(put, rc);
4238
4239         rc = mdd_la_get(env, mdd_pobj, pattr);
4240         if (rc != 0)
4241                 GOTO(put, rc);
4242
4243         rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4244         if (rc != 0) {
4245                 if (rc > 0)
4246                         rc = 0;
4247                 GOTO(put, rc);
4248         }
4249
4250         /* Sigh, it is impossible to finish all of migration in a single
4251          * transaction, for example migrating big directory entries to the
4252          * new MDT, it needs insert all of name entries of children in the
4253          * new directory.
4254          *
4255          * So migration will be done in multiple steps and transactions.
4256          *
4257          * 1. create an orphan object on the remote MDT in one transaction.
4258          * 2. migrate extend attributes to the new target file/directory.
4259          * 3. For directory, migrate the entries to the new MDT and update
4260          * linkEA of each children. Because we can not migrate all entries
4261          * in a single transaction, so the migrating directory will become
4262          * a striped directory during migration, so once the process is
4263          * interrupted, the directory is still accessible. (During lookup,
4264          * client will locate the name by searching both original and target
4265          * object).
4266          * 4. Finally, update the name/FID to point to the new file/directory
4267          * in a separate transaction.
4268          */
4269
4270         /* step 1: Check whether the orphan object has been created, and create
4271          * orphan object on the remote MDT if needed */
4272         if (!mdd_object_exists(mdd_tobj)) {
4273                 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4274                                         lname, so_attr);
4275                 if (rc != 0)
4276                         GOTO(put, rc);
4277         }
4278
4279         LASSERT(mdd_object_exists(mdd_tobj));
4280         /* step 2: migrate xattr */
4281         rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4282         if (rc != 0)
4283                 GOTO(put, rc);
4284
4285         /* step 3: migrate name entries to the orphan object */
4286         if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4287                 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4288                 if (rc != 0)
4289                         GOTO(put, rc);
4290                 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4291                                                   OBD_FAIL_MDS_REINT_NET_REP)))
4292                         GOTO(put, rc = 0);
4293         } else {
4294                 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4295         }
4296
4297         LASSERT(mdd_object_exists(mdd_tobj));
4298         /* step 4: update name entry to the new object */
4299         rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4300                                      ma);
4301         if (rc != 0)
4302                 GOTO(put, rc);
4303 put:
4304         RETURN(rc);
4305 }
4306
4307 const struct md_dir_operations mdd_dir_ops = {
4308         .mdo_is_subdir     = mdd_is_subdir,
4309         .mdo_lookup        = mdd_lookup,
4310         .mdo_create        = mdd_create,
4311         .mdo_rename        = mdd_rename,
4312         .mdo_link          = mdd_link,
4313         .mdo_unlink        = mdd_unlink,
4314         .mdo_create_data   = mdd_create_data,
4315         .mdo_migrate       = mdd_migrate,
4316 };