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