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