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