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