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