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