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