4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
61 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
63 if (!lu_name_is_valid(ln))
65 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
71 /* Get FID from name and parent */
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)
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);
85 if (unlikely(mdd_is_dead_obj(mdd_obj)))
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)) {
95 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
100 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101 dt_try_as_dir(env, dir))) {
103 rc = dir->do_index_ops->dio_lookup(env, dir,
104 (struct dt_rec *)fid, key,
105 mdd_object_capa(env, mdd_obj));
116 int mdd_lookup(const struct lu_env *env,
117 struct md_object *pobj, const struct lu_name *lname,
118 struct lu_fid *fid, struct md_op_spec *spec)
120 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
124 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
128 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
132 static inline int mdd_parent_fid(const struct lu_env *env,
133 struct mdd_object *obj,
134 const struct lu_attr *attr,
137 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
141 * For root fid use special function, which does not compare version component
142 * of fid. Version component is different for root fids on all MDTs.
144 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
146 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
147 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
151 * return 1: if lf is the fid of the ancestor of p1;
154 * return -EREMOTE: if remote object is found, in this
155 * case fid of remote object is saved to @pf;
157 * otherwise: values < 0, errors.
159 static int mdd_is_parent(const struct lu_env *env,
160 struct mdd_device *mdd,
161 struct mdd_object *p1,
162 const struct lu_attr *attr,
163 const struct lu_fid *lf,
166 struct mdd_object *parent = NULL;
171 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
172 pfid = &mdd_env_info(env)->mti_fid;
174 /* Check for root first. */
175 if (mdd_is_root(mdd, mdo2fid(p1)))
179 /* this is done recursively, bypass capa for each obj */
180 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
181 rc = mdd_parent_fid(env, p1, attr, pfid);
184 if (mdd_is_root(mdd, pfid))
186 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
188 if (lu_fid_eq(pfid, lf))
191 mdd_object_put(env, parent);
193 parent = mdd_object_find(env, mdd, pfid);
194 if (IS_ERR(parent)) {
195 GOTO(out, rc = PTR_ERR(parent));
196 } else if (mdd_object_remote(parent)) {
197 /*FIXME: Because of the restriction of rename in Phase I.
198 * If the parent is remote, we just assumed lf is not the
199 * parent of P1 for now */
206 if (parent && !IS_ERR(parent))
207 mdd_object_put(env, parent);
212 * No permission check is needed.
214 * returns 1: if fid is ancestor of @mo;
215 * returns 0: if fid is not a ancestor of @mo;
217 * returns EREMOTE if remote object is found, fid of remote object is saved to
220 * returns < 0: if error
222 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
223 const struct lu_fid *fid, struct lu_fid *sfid)
225 struct mdd_device *mdd = mdo2mdd(mo);
226 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
230 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
233 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
237 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
241 } else if (rc == 1) {
242 /* found @fid is parent */
250 * Check that @dir contains no entries except (possibly) dot and dotdot.
255 * -ENOTDIR not a directory object
256 * -ENOTEMPTY not empty
260 static int mdd_dir_is_empty(const struct lu_env *env,
261 struct mdd_object *dir)
264 struct dt_object *obj;
265 const struct dt_it_ops *iops;
269 obj = mdd_object_child(dir);
270 if (!dt_try_as_dir(env, obj))
273 iops = &obj->do_index_ops->dio_it;
274 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
276 result = iops->get(env, it, (const struct dt_key *)"");
279 for (result = 0, i = 0; result == 0 && i < 3; ++i)
280 result = iops->next(env, it);
283 else if (result == 1)
285 } else if (result == 0)
287 * Huh? Index contains no zero key?
294 result = PTR_ERR(it);
299 * Determine if the target object can be hard linked, and right now it only
300 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
301 * directory nlink count might exceed the maximum link count(see
302 * osd_object_ref_add), so it only check nlink for non-directories.
304 * \param[in] env thread environment
305 * \param[in] obj object being linked to
306 * \param[in] la attributes of \a obj
308 * \retval 0 if \a obj can be hard linked
309 * \retval negative error if \a obj is a directory or has too
312 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
313 const struct lu_attr *la)
315 struct mdd_device *m = mdd_obj2mdd_dev(obj);
320 /* Subdir count limitation can be broken through
321 * (see osd_object_ref_add), so only check non-directory here. */
322 if (!S_ISDIR(la->la_mode) &&
323 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
330 * Check whether it may create the cobj under the pobj.
332 * \param[in] env execution environment
333 * \param[in] pobj the parent directory
334 * \param[in] pattr the attribute of the parent directory
335 * \param[in] cobj the child to be created
336 * \param[in] check_perm if check WRITE|EXEC permission for parent
338 * \retval = 0 create the child under this dir is allowed
339 * \retval negative errno create the child under this dir is
342 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
343 const struct lu_attr *pattr, struct mdd_object *cobj,
346 struct mdd_thread_info *info = mdd_env_info(env);
351 if (cobj && mdd_object_exists(cobj))
354 if (mdd_is_dead_obj(pobj))
357 /* If the parent is a sub-stripe, check whether it is dead */
358 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
359 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV,
360 mdd_object_capa(env, pobj));
361 if (unlikely(rc > 0)) {
362 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
364 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
365 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
371 rc = mdd_permission_internal_locked(env, pobj, pattr,
372 MAY_WRITE | MAY_EXEC,
378 * Check whether can unlink from the pobj in the case of "cobj == NULL".
380 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
381 const struct lu_attr *pattr, const struct lu_attr *attr)
386 if (mdd_is_dead_obj(pobj))
389 if ((attr->la_valid & LA_FLAGS) &&
390 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
393 rc = mdd_permission_internal_locked(env, pobj, pattr,
394 MAY_WRITE | MAY_EXEC,
399 if (mdd_is_append(pobj))
406 * pobj == NULL is remote ops case, under such case, pobj's
407 * VTX feature has been checked already, no need check again.
409 static inline int mdd_is_sticky(const struct lu_env *env,
410 struct mdd_object *pobj,
411 const struct lu_attr *pattr,
412 struct mdd_object *cobj,
413 const struct lu_attr *cattr)
415 struct lu_ucred *uc = lu_ucred_assert(env);
418 LASSERT(pattr != NULL);
419 if (!(pattr->la_mode & S_ISVTX) ||
420 (pattr->la_uid == uc->uc_fsuid))
424 LASSERT(cattr != NULL);
425 if (cattr->la_uid == uc->uc_fsuid)
428 return !md_capable(uc, CFS_CAP_FOWNER);
431 static int mdd_may_delete_entry(const struct lu_env *env,
432 struct mdd_object *pobj,
433 const struct lu_attr *pattr,
438 LASSERT(pobj != NULL);
439 if (!mdd_object_exists(pobj))
442 if (mdd_is_dead_obj(pobj))
447 rc = mdd_permission_internal_locked(env, pobj, pattr,
448 MAY_WRITE | MAY_EXEC,
454 if (mdd_is_append(pobj))
461 * Check whether it may delete the cobj from the pobj.
464 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
465 const struct lu_attr *tpattr, struct mdd_object *tobj,
466 const struct lu_attr *tattr, const struct lu_attr *cattr,
467 int check_perm, int check_empty)
473 LASSERT(tpattr != NULL);
474 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
482 if (!mdd_object_exists(tobj))
485 if (mdd_is_dead_obj(tobj))
488 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
491 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
494 if ((tattr->la_valid & LA_FLAGS) &&
495 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
498 /* additional check the rename case */
500 if (S_ISDIR(cattr->la_mode)) {
501 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
503 if (!S_ISDIR(tattr->la_mode))
506 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
508 } else if (S_ISDIR(tattr->la_mode))
512 if (S_ISDIR(tattr->la_mode) && check_empty)
513 rc = mdd_dir_is_empty(env, tobj);
519 * Check whether it can create the link file(linked to @src_obj) under
520 * the target directory(@tgt_obj), and src_obj has been locked by
523 * \param[in] env execution environment
524 * \param[in] tgt_obj the target directory
525 * \param[in] tattr attributes of target directory
526 * \param[in] lname the link name
527 * \param[in] src_obj source object for link
528 * \param[in] cattr attributes for source object
530 * \retval = 0 it is allowed to create the link file under tgt_obj
531 * \retval negative error not allowed to create the link file
533 static int mdd_link_sanity_check(const struct lu_env *env,
534 struct mdd_object *tgt_obj,
535 const struct lu_attr *tattr,
536 const struct lu_name *lname,
537 struct mdd_object *src_obj,
538 const struct lu_attr *cattr)
540 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
544 if (!mdd_object_exists(src_obj))
547 if (mdd_is_dead_obj(src_obj))
550 /* Local ops, no lookup before link, check filename length here. */
551 rc = mdd_name_check(m, lname);
555 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
558 if (S_ISDIR(mdd_object_type(src_obj)))
561 LASSERT(src_obj != tgt_obj);
562 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
566 rc = __mdd_may_link(env, src_obj, cattr);
571 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
572 const char *name, struct thandle *handle,
573 struct lustre_capa *capa)
575 struct dt_object *next = mdd_object_child(pobj);
579 if (dt_try_as_dir(env, next)) {
580 rc = next->do_index_ops->dio_delete(env, next,
581 (struct dt_key *)name,
589 static int __mdd_index_insert_only(const struct lu_env *env,
590 struct mdd_object *pobj,
591 const struct lu_fid *lf, __u32 type,
593 struct thandle *handle,
594 struct lustre_capa *capa)
596 struct dt_object *next = mdd_object_child(pobj);
600 if (dt_try_as_dir(env, next)) {
601 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
602 struct lu_ucred *uc = lu_ucred_check(env);
606 rec->rec_type = type;
607 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
608 rc = next->do_index_ops->dio_insert(env, next,
609 (const struct dt_rec *)rec,
610 (const struct dt_key *)name,
611 handle, capa, ignore_quota);
618 /* insert named index, add reference if isdir */
619 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
620 const struct lu_fid *lf, __u32 type,
621 const char *name, struct thandle *handle,
622 struct lustre_capa *capa)
627 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle, capa);
628 if (rc == 0 && S_ISDIR(type)) {
629 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
630 mdo_ref_add(env, pobj, handle);
631 mdd_write_unlock(env, pobj);
637 /* delete named index, drop reference if isdir */
638 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
639 const char *name, int is_dir, struct thandle *handle,
640 struct lustre_capa *capa)
645 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
646 if (rc == 0 && is_dir) {
647 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
648 mdo_ref_del(env, pobj, handle);
649 mdd_write_unlock(env, pobj);
655 int mdd_declare_changelog_store(const struct lu_env *env,
656 struct mdd_device *mdd,
657 const struct lu_name *fname,
658 struct thandle *handle)
660 struct obd_device *obd = mdd2obd_dev(mdd);
661 struct llog_ctxt *ctxt;
662 struct llog_changelog_rec *rec;
668 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
671 reclen = llog_data_len(sizeof(*rec) +
672 (fname != NULL ? fname->ln_namelen : 0));
673 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
674 if (buf->lb_buf == NULL)
678 rec->cr_hdr.lrh_len = reclen;
679 rec->cr_hdr.lrh_type = CHANGELOG_REC;
681 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
685 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
691 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
692 struct mdd_device *mdd,
693 const struct lu_name *tname,
694 const struct lu_name *sname,
695 struct thandle *handle)
697 struct obd_device *obd = mdd2obd_dev(mdd);
698 struct llog_ctxt *ctxt;
699 struct llog_changelog_ext_rec *rec;
705 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
708 reclen = llog_data_len(sizeof(*rec) +
709 (tname != NULL ? tname->ln_namelen : 0) +
710 (sname != NULL ? 1 + sname->ln_namelen : 0));
711 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
712 if (buf->lb_buf == NULL)
716 rec->cr_hdr.lrh_len = reclen;
717 rec->cr_hdr.lrh_type = CHANGELOG_REC;
719 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
723 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
729 /** Add a changelog entry \a rec to the changelog llog
732 * \param handle - currently ignored since llogs start their own transaction;
733 * this will hopefully be fixed in llog rewrite
736 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
737 struct llog_changelog_rec *rec, struct thandle *th)
739 struct obd_device *obd = mdd2obd_dev(mdd);
740 struct llog_ctxt *ctxt;
743 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
744 rec->cr_hdr.lrh_type = CHANGELOG_REC;
745 rec->cr.cr_time = cl_time();
747 spin_lock(&mdd->mdd_cl.mc_lock);
748 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
749 * but as long as the MDD transactions are ordered correctly for e.g.
750 * rename conflicts, I don't think this should matter. */
751 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
752 spin_unlock(&mdd->mdd_cl.mc_lock);
754 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
758 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
765 /** Add a changelog_ext entry \a rec to the changelog llog
768 * \param handle - currently ignored since llogs start their own transaction;
769 * this will hopefully be fixed in llog rewrite
773 mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
774 struct llog_changelog_ext_rec *rec, struct thandle *th)
776 struct obd_device *obd = mdd2obd_dev(mdd);
777 struct llog_ctxt *ctxt;
780 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
781 /* llog_lvfs_write_rec sets the llog tail len */
782 rec->cr_hdr.lrh_type = CHANGELOG_REC;
783 rec->cr.cr_time = cl_time();
785 spin_lock(&mdd->mdd_cl.mc_lock);
786 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
787 * but as long as the MDD transactions are ordered correctly for e.g.
788 * rename conflicts, I don't think this should matter. */
789 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
790 spin_unlock(&mdd->mdd_cl.mc_lock);
792 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
796 /* nested journal transaction */
797 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
805 /** Store a namespace change changelog record
806 * If this fails, we must fail the whole transaction; we don't
807 * want the change to commit without the log entry.
808 * \param target - mdd_object of change
809 * \param parent - parent dir/object
810 * \param tname - target name string
811 * \param handle - transacion handle
813 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
814 enum changelog_rec_type type, unsigned flags,
815 struct mdd_object *target, struct mdd_object *parent,
816 const struct lu_name *tname, struct thandle *handle)
818 struct llog_changelog_rec *rec;
825 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
827 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
830 LASSERT(target != NULL);
831 LASSERT(parent != NULL);
832 LASSERT(tname != NULL);
833 LASSERT(handle != NULL);
835 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
836 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
837 if (buf->lb_buf == NULL)
841 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
842 rec->cr.cr_type = (__u32)type;
843 rec->cr.cr_tfid = *mdo2fid(target);
844 rec->cr.cr_pfid = *mdo2fid(parent);
845 rec->cr.cr_namelen = tname->ln_namelen;
846 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
848 target->mod_cltime = cfs_time_current_64();
850 rc = mdd_changelog_store(env, mdd, rec, handle);
852 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
853 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
854 PFID(&rec->cr.cr_pfid));
862 /** Store a namespace change changelog record
863 * If this fails, we must fail the whole transaction; we don't
864 * want the change to commit without the log entry.
865 * \param target - mdd_object of change
866 * \param tpfid - target parent dir/object fid
867 * \param sfid - source object fid
868 * \param spfid - source parent fid
869 * \param tname - target name string
870 * \param sname - source name string
871 * \param handle - transacion handle
873 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
874 struct mdd_device *mdd,
875 enum changelog_rec_type type,
877 struct mdd_object *target,
878 const struct lu_fid *tpfid,
879 const struct lu_fid *sfid,
880 const struct lu_fid *spfid,
881 const struct lu_name *tname,
882 const struct lu_name *sname,
883 struct thandle *handle)
885 struct llog_changelog_ext_rec *rec;
892 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
894 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
897 LASSERT(sfid != NULL);
898 LASSERT(tpfid != NULL);
899 LASSERT(tname != NULL);
900 LASSERT(handle != NULL);
902 reclen = llog_data_len(sizeof(*rec) +
903 sname != NULL ? 1 + sname->ln_namelen : 0);
904 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
905 if (buf->lb_buf == NULL)
909 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
910 rec->cr.cr_type = (__u32)type;
911 rec->cr.cr_pfid = *tpfid;
912 rec->cr.cr_sfid = *sfid;
913 rec->cr.cr_spfid = *spfid;
914 rec->cr.cr_namelen = tname->ln_namelen;
915 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
917 rec->cr.cr_name[tname->ln_namelen] = '\0';
918 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
920 rec->cr.cr_namelen += 1 + sname->ln_namelen;
923 if (likely(target != NULL)) {
924 rec->cr.cr_tfid = *mdo2fid(target);
925 target->mod_cltime = cfs_time_current_64();
927 fid_zero(&rec->cr.cr_tfid);
930 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
932 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
933 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
940 static int __mdd_links_add(const struct lu_env *env,
941 struct mdd_object *mdd_obj,
942 struct linkea_data *ldata,
943 const struct lu_name *lname,
944 const struct lu_fid *pfid,
945 int first, int check)
949 if (ldata->ld_leh == NULL) {
950 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
954 rc = linkea_data_new(ldata,
955 &mdd_env_info(env)->mti_link_buf);
962 rc = linkea_links_find(ldata, lname, pfid);
963 if (rc && rc != -ENOENT)
969 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
970 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
974 linkea_add_buf(ldata, lname, tfid);
977 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
978 linkea_add_buf(ldata, lname, pfid);
980 return linkea_add_buf(ldata, lname, pfid);
983 static int __mdd_links_del(const struct lu_env *env,
984 struct mdd_object *mdd_obj,
985 struct linkea_data *ldata,
986 const struct lu_name *lname,
987 const struct lu_fid *pfid)
991 if (ldata->ld_leh == NULL) {
992 rc = mdd_links_read(env, mdd_obj, ldata);
997 rc = linkea_links_find(ldata, lname, pfid);
1001 linkea_del_buf(ldata, lname);
1005 static int mdd_linkea_prepare(const struct lu_env *env,
1006 struct mdd_object *mdd_obj,
1007 const struct lu_fid *oldpfid,
1008 const struct lu_name *oldlname,
1009 const struct lu_fid *newpfid,
1010 const struct lu_name *newlname,
1011 int first, int check,
1012 struct linkea_data *ldata)
1018 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1021 LASSERT(oldpfid != NULL || newpfid != NULL);
1023 if (mdd_obj->mod_flags & DEAD_OBJ) {
1024 /* Prevent linkea to be updated which is NOT necessary. */
1025 ldata->ld_reclen = 0;
1026 /* No more links, don't bother */
1030 if (oldpfid != NULL) {
1031 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1034 (rc != -ENODATA && rc != -ENOENT))
1036 /* No changes done. */
1041 /* If renaming, add the new record */
1042 if (newpfid != NULL) {
1043 /* even if the add fails, we still delete the out-of-date
1045 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1049 rc = rc != 0 ? rc : rc2;
1054 int mdd_links_rename(const struct lu_env *env,
1055 struct mdd_object *mdd_obj,
1056 const struct lu_fid *oldpfid,
1057 const struct lu_name *oldlname,
1058 const struct lu_fid *newpfid,
1059 const struct lu_name *newlname,
1060 struct thandle *handle,
1061 struct linkea_data *ldata,
1062 int first, int check)
1067 if (ldata == NULL) {
1068 ldata = &mdd_env_info(env)->mti_link_data;
1069 memset(ldata, 0, sizeof(*ldata));
1070 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1071 newpfid, newlname, first, check,
1077 if (ldata->ld_reclen != 0)
1078 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1083 if (rc == -EOVERFLOW || rc == -ENOSPC)
1085 if (oldpfid == NULL)
1086 CDEBUG(error ? D_ERROR : D_OTHER,
1087 "link_ea add '%.*s' failed %d "DFID"\n",
1088 newlname->ln_namelen, newlname->ln_name,
1089 rc, PFID(mdd_object_fid(mdd_obj)));
1090 else if (newpfid == NULL)
1091 CDEBUG(error ? D_ERROR : D_OTHER,
1092 "link_ea del '%.*s' failed %d "DFID"\n",
1093 oldlname->ln_namelen, oldlname->ln_name,
1094 rc, PFID(mdd_object_fid(mdd_obj)));
1096 CDEBUG(error ? D_ERROR : D_OTHER,
1097 "link_ea rename '%.*s'->'%.*s' failed %d "
1099 oldlname->ln_namelen, oldlname->ln_name,
1100 newlname->ln_namelen, newlname->ln_name,
1101 rc, PFID(mdd_object_fid(mdd_obj)));
1104 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1105 /* if we vmalloced a large buffer drop it */
1106 lu_buf_free(ldata->ld_buf);
1111 static inline int mdd_links_add(const struct lu_env *env,
1112 struct mdd_object *mdd_obj,
1113 const struct lu_fid *pfid,
1114 const struct lu_name *lname,
1115 struct thandle *handle,
1116 struct linkea_data *ldata, int first)
1118 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1119 pfid, lname, handle, ldata, first, 0);
1122 static inline int mdd_links_del(const struct lu_env *env,
1123 struct mdd_object *mdd_obj,
1124 const struct lu_fid *pfid,
1125 const struct lu_name *lname,
1126 struct thandle *handle)
1128 return mdd_links_rename(env, mdd_obj, pfid, lname,
1129 NULL, NULL, handle, NULL, 0, 0);
1132 /** Read the link EA into a temp buffer.
1133 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1134 * A pointer to the buffer is stored in \a ldata::ld_buf.
1136 * \retval 0 or error
1138 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1139 struct linkea_data *ldata)
1143 if (!mdd_object_exists(mdd_obj))
1146 /* First try a small buf */
1147 LASSERT(env != NULL);
1148 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1150 if (ldata->ld_buf->lb_buf == NULL)
1153 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1155 if (rc == -ERANGE) {
1156 /* Buf was too small, figure out what we need. */
1157 lu_buf_free(ldata->ld_buf);
1158 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1159 XATTR_NAME_LINK, BYPASS_CAPA);
1162 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1163 if (ldata->ld_buf->lb_buf == NULL)
1165 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1166 XATTR_NAME_LINK, BYPASS_CAPA);
1169 lu_buf_free(ldata->ld_buf);
1170 ldata->ld_buf = NULL;
1174 return linkea_init(ldata);
1177 /** Read the link EA into a temp buffer.
1178 * Uses the name_buf since it is generally large.
1179 * \retval IS_ERR err
1180 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1182 struct lu_buf *mdd_links_get(const struct lu_env *env,
1183 struct mdd_object *mdd_obj)
1185 struct linkea_data ldata = { 0 };
1188 rc = mdd_links_read(env, mdd_obj, &ldata);
1189 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1192 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1193 struct linkea_data *ldata, struct thandle *handle)
1195 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1196 ldata->ld_leh->leh_len);
1197 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1198 mdd_object_capa(env, mdd_obj));
1201 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1202 struct thandle *handle, struct linkea_data *ldata)
1208 if (ldata != NULL && ldata->ld_leh != NULL) {
1209 ea_len = ldata->ld_leh->leh_len;
1210 linkea = ldata->ld_buf->lb_buf;
1212 ea_len = DEFAULT_LINKEA_SIZE;
1216 /* XXX: max size? */
1217 rc = mdo_declare_xattr_set(env, mdd_obj,
1218 mdd_buf_get_const(env, linkea, ea_len),
1219 XATTR_NAME_LINK, 0, handle);
1223 static inline int mdd_declare_links_del(const struct lu_env *env,
1224 struct mdd_object *c,
1225 struct thandle *handle)
1229 /* For directory, the linkEA will be removed together
1230 * with the object. */
1231 if (!S_ISDIR(mdd_object_type(c)))
1232 rc = mdd_declare_links_add(env, c, handle, NULL);
1237 static int mdd_declare_link(const struct lu_env *env,
1238 struct mdd_device *mdd,
1239 struct mdd_object *p,
1240 struct mdd_object *c,
1241 const struct lu_name *name,
1242 struct thandle *handle,
1244 struct linkea_data *data)
1248 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1249 name->ln_name, handle);
1253 rc = mdo_declare_ref_add(env, c, handle);
1257 la->la_valid = LA_CTIME | LA_MTIME;
1258 rc = mdo_declare_attr_set(env, p, la, handle);
1262 la->la_valid = LA_CTIME;
1263 rc = mdo_declare_attr_set(env, c, la, handle);
1267 rc = mdd_declare_links_add(env, c, handle, data);
1271 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1276 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1277 struct md_object *src_obj, const struct lu_name *lname,
1280 const char *name = lname->ln_name;
1281 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1282 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1283 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1284 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1285 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1286 struct mdd_device *mdd = mdo2mdd(src_obj);
1287 struct thandle *handle;
1288 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1292 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1296 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1300 handle = mdd_trans_create(env, mdd);
1302 GOTO(out_pending, rc = PTR_ERR(handle));
1304 memset(ldata, 0, sizeof(*ldata));
1306 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1307 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1309 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1314 rc = mdd_trans_start(env, mdd, handle);
1318 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1319 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1322 GOTO(out_unlock, rc);
1324 rc = mdo_ref_add(env, mdd_sobj, handle);
1326 GOTO(out_unlock, rc);
1329 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1330 mdd_object_type(mdd_sobj), name, handle,
1331 mdd_object_capa(env, mdd_tobj));
1333 mdo_ref_del(env, mdd_sobj, handle);
1334 GOTO(out_unlock, rc);
1337 la->la_valid = LA_CTIME | LA_MTIME;
1338 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1340 GOTO(out_unlock, rc);
1342 la->la_valid = LA_CTIME;
1343 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1345 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1346 mdo2fid(mdd_tobj), lname, 0, 0,
1349 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1350 lname, handle, ldata, 0);
1351 /* The failure of links_add should not cause the link
1352 * failure, reset rc here */
1357 mdd_write_unlock(env, mdd_sobj);
1359 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1360 mdd_tobj, lname, handle);
1362 mdd_trans_stop(env, mdd, rc, handle);
1364 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1365 /* if we vmalloced a large buffer drop it */
1366 lu_buf_free(ldata->ld_buf);
1371 static int mdd_mark_dead_object(const struct lu_env *env,
1372 struct mdd_object *obj, struct thandle *handle,
1375 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1379 obj->mod_flags |= DEAD_OBJ;
1381 if (!S_ISDIR(mdd_object_type(obj)))
1384 attr->la_valid = LA_FLAGS;
1385 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1388 rc = mdo_declare_attr_set(env, obj, attr, handle);
1390 rc = mdo_attr_set(env, obj, attr, handle,
1391 mdd_object_capa(env, obj));
1396 static int mdd_declare_finish_unlink(const struct lu_env *env,
1397 struct mdd_object *obj,
1398 struct thandle *handle)
1402 rc = mdd_mark_dead_object(env, obj, handle, true);
1406 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1410 rc = mdo_declare_destroy(env, obj, handle);
1414 return mdd_declare_links_del(env, obj, handle);
1417 /* caller should take a lock before calling */
1418 int mdd_finish_unlink(const struct lu_env *env,
1419 struct mdd_object *obj, struct md_attr *ma,
1420 const struct mdd_object *pobj,
1421 const struct lu_name *lname,
1425 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1428 LASSERT(mdd_write_locked(env, obj) != 0);
1430 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1431 rc = mdd_mark_dead_object(env, obj, th, false);
1435 /* add new orphan and the object
1436 * will be deleted during mdd_close() */
1437 if (obj->mod_count) {
1438 rc = __mdd_orphan_add(env, obj, th);
1440 CDEBUG(D_HA, "Object "DFID" is inserted into "
1441 "orphan list, open count = %d\n",
1442 PFID(mdd_object_fid(obj)),
1445 CERROR("Object "DFID" fail to be an orphan, "
1446 "open count = %d, maybe cause failed "
1448 PFID(mdd_object_fid(obj)),
1451 rc = mdo_destroy(env, obj, th);
1453 } else if (!is_dir) {
1454 /* old files may not have link ea; ignore errors */
1455 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1463 * has mdd_write_lock on cobj already, but not on pobj yet
1465 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1466 const struct lu_attr *pattr,
1467 struct mdd_object *cobj,
1468 const struct lu_attr *cattr)
1473 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1478 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1479 struct mdd_object *p, struct mdd_object *c,
1480 const struct lu_name *name, struct md_attr *ma,
1481 struct thandle *handle, int no_name)
1483 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1486 if (likely(no_name == 0)) {
1487 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1492 rc = mdo_declare_ref_del(env, p, handle);
1496 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1497 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1498 la->la_valid = LA_CTIME | LA_MTIME;
1499 rc = mdo_declare_attr_set(env, p, la, handle);
1504 rc = mdo_declare_ref_del(env, c, handle);
1508 rc = mdo_declare_ref_del(env, c, handle);
1512 la->la_valid = LA_CTIME;
1513 rc = mdo_declare_attr_set(env, c, la, handle);
1517 rc = mdd_declare_finish_unlink(env, c, handle);
1521 /* FIXME: need changelog for remove entry */
1522 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1529 * test if a file has an HSM archive
1530 * if HSM attributes are not found in ma update them from
1533 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1534 struct mdd_object *obj,
1539 if (!(ma->ma_valid & MA_HSM)) {
1540 /* no HSM MD provided, read xattr */
1541 struct lu_buf *hsm_buf;
1542 const size_t buflen = sizeof(struct hsm_attrs);
1545 hsm_buf = mdd_buf_get(env, NULL, 0);
1546 lu_buf_alloc(hsm_buf, buflen);
1547 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1548 mdd_object_capa(env, obj));
1549 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1550 lu_buf_free(hsm_buf);
1554 ma->ma_valid = MA_HSM;
1556 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1562 * Delete name entry and the object.
1563 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1564 * does not exist for this object, and it could only happen during resending
1565 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1566 * is also needed in this case(needed by changelog), so we have to add another
1567 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1568 * the ENOENT failure should be able to be fixed by redo mechanism.
1570 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1571 struct md_object *cobj, const struct lu_name *lname,
1572 struct md_attr *ma, int no_name)
1574 const char *name = lname->ln_name;
1575 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1576 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1577 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1578 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1579 struct mdd_object *mdd_cobj = NULL;
1580 struct mdd_device *mdd = mdo2mdd(pobj);
1581 struct thandle *handle;
1585 /* cobj == NULL means only delete name entry */
1586 if (likely(cobj != NULL)) {
1587 mdd_cobj = md2mdd_obj(cobj);
1588 if (mdd_object_exists(mdd_cobj) == 0)
1592 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1596 if (likely(mdd_cobj != NULL)) {
1598 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1602 is_dir = S_ISDIR(cattr->la_mode);
1605 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1609 handle = mdd_trans_create(env, mdd);
1611 RETURN(PTR_ERR(handle));
1613 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1614 lname, ma, handle, no_name);
1618 rc = mdd_trans_start(env, mdd, handle);
1622 if (likely(mdd_cobj != NULL))
1623 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1625 if (likely(no_name == 0)) {
1626 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1627 mdd_object_capa(env, mdd_pobj));
1632 if (likely(mdd_cobj != NULL)) {
1633 rc = mdo_ref_del(env, mdd_cobj, handle);
1635 __mdd_index_insert_only(env, mdd_pobj,
1637 mdd_object_type(mdd_cobj),
1639 mdd_object_capa(env, mdd_pobj));
1645 mdo_ref_del(env, mdd_cobj, handle);
1647 /* fetch updated nlink */
1648 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1653 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1654 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1656 la->la_valid = LA_CTIME | LA_MTIME;
1657 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1661 /* Enough for only unlink the entry */
1662 if (unlikely(mdd_cobj == NULL))
1665 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1666 /* update ctime of an unlinked file only if it is still
1667 * opened or a link still exists */
1668 la->la_valid = LA_CTIME;
1669 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1674 /* XXX: this transfer to ma will be removed with LOD/OSP */
1675 ma->ma_attr = *cattr;
1676 ma->ma_valid |= MA_INODE;
1677 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1679 /* fetch updated nlink */
1681 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1683 /* if object is removed then we can't get its attrs, use last get */
1684 if (cattr->la_nlink == 0) {
1685 ma->ma_attr = *cattr;
1686 ma->ma_valid |= MA_INODE;
1690 mdd_write_unlock(env, mdd_cobj);
1694 if (cattr->la_nlink == 0) {
1695 cl_flags |= CLF_UNLINK_LAST;
1696 /* search for an existing archive */
1697 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1698 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1701 rc = mdd_changelog_ns_store(env, mdd,
1702 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1703 mdd_cobj, mdd_pobj, lname, handle);
1707 mdd_trans_stop(env, mdd, rc, handle);
1713 * The permission has been checked when obj created, no need check again.
1715 static int mdd_cd_sanity_check(const struct lu_env *env,
1716 struct mdd_object *obj)
1721 if (!obj || mdd_is_dead_obj(obj))
1727 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1728 struct md_object *cobj, const struct md_op_spec *spec,
1731 struct mdd_device *mdd = mdo2mdd(cobj);
1732 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1733 struct mdd_object *son = md2mdd_obj(cobj);
1734 struct thandle *handle;
1735 const struct lu_buf *buf;
1736 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1737 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1741 rc = mdd_cd_sanity_check(env, son);
1745 if (!md_should_create(spec->sp_cr_flags))
1749 * there are following use cases for this function:
1750 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1751 * striping can be specified or not
1754 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1758 /* calling ->ah_make_hint() is used to transfer information from parent */
1759 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1761 handle = mdd_trans_create(env, mdd);
1763 GOTO(out_free, rc = PTR_ERR(handle));
1766 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1767 * Should this be fixed?
1769 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1770 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1771 spec->sp_cr_flags, spec->no_create);
1773 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1774 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1775 spec->u.sp_ea.eadatalen);
1780 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1781 XATTR_NAME_LOV, 0, handle);
1785 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1789 rc = mdd_trans_start(env, mdd, handle);
1793 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1794 0, handle, mdd_object_capa(env, son));
1799 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1802 mdd_trans_stop(env, mdd, rc, handle);
1807 static int mdd_declare_object_initialize(const struct lu_env *env,
1808 struct mdd_object *parent,
1809 struct mdd_object *child,
1810 struct lu_attr *attr,
1811 struct thandle *handle)
1817 * inode mode has been set in creation time, and it's based on umask,
1818 * la_mode and acl, don't set here again! (which will go wrong
1819 * because below function doesn't consider umask).
1820 * I'd suggest set all object attributes in creation time, see above.
1822 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1823 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1824 rc = mdo_declare_attr_set(env, child, attr, handle);
1825 attr->la_valid |= LA_MODE | LA_TYPE;
1826 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1827 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1828 S_IFDIR, dot, handle);
1830 rc = mdo_declare_ref_add(env, child, handle);
1832 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1833 S_IFDIR, dotdot, handle);
1839 static int mdd_object_initialize(const struct lu_env *env,
1840 const struct lu_fid *pfid,
1841 struct mdd_object *child,
1842 struct lu_attr *attr, struct thandle *handle,
1843 const struct md_op_spec *spec)
1849 * Update attributes for child.
1852 * (1) the valid bits should be converted between Lustre and Linux;
1853 * (2) maybe, the child attributes should be set in OSD when creation.
1856 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1857 /* arguments are supposed to stay the same */
1858 if (S_ISDIR(attr->la_mode)) {
1859 /* Add "." and ".." for newly created dir */
1860 mdo_ref_add(env, child, handle);
1861 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1862 S_IFDIR, dot, handle, BYPASS_CAPA);
1864 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1868 mdo_ref_del(env, child, handle);
1875 * This function checks whether it can create a file/dir under the
1876 * directory(@pobj). The directory(@pobj) is not being locked by
1879 * \param[in] env execution environment
1880 * \param[in] pobj the directory to create files
1881 * \param[in] pattr the attributes of the directory
1882 * \param[in] lname the name of the created file/dir
1883 * \param[in] cattr the attributes of the file/dir
1884 * \param[in] spec create specification
1886 * \retval = 0 it is allowed to create file/dir under
1888 * \retval negative error not allowed to create file/dir
1889 * under the directory
1891 static int mdd_create_sanity_check(const struct lu_env *env,
1892 struct md_object *pobj,
1893 const struct lu_attr *pattr,
1894 const struct lu_name *lname,
1895 struct lu_attr *cattr,
1896 struct md_op_spec *spec)
1898 struct mdd_thread_info *info = mdd_env_info(env);
1899 struct lu_fid *fid = &info->mti_fid;
1900 struct mdd_object *obj = md2mdd_obj(pobj);
1901 struct mdd_device *m = mdo2mdd(pobj);
1902 bool check_perm = true;
1907 if (mdd_is_dead_obj(obj))
1911 * In some cases this lookup is not needed - we know before if name
1912 * exists or not because MDT performs lookup for it.
1913 * name length check is done in lookup.
1915 if (spec->sp_cr_lookup) {
1917 * Check if the name already exist, though it will be checked in
1918 * _index_insert also, for avoiding rolling back if exists
1921 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1922 MAY_WRITE | MAY_EXEC);
1924 RETURN(rc ? : -EEXIST);
1926 /* Permission is already being checked in mdd_lookup */
1930 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1935 if (pattr->la_mode & S_ISGID) {
1936 cattr->la_gid = pattr->la_gid;
1937 if (S_ISDIR(cattr->la_mode)) {
1938 cattr->la_mode |= S_ISGID;
1939 cattr->la_valid |= LA_MODE;
1943 rc = mdd_name_check(m, lname);
1947 switch (cattr->la_mode & S_IFMT) {
1949 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1951 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1952 RETURN(-ENAMETOOLONG);
1971 static int mdd_declare_object_create(const struct lu_env *env,
1972 struct mdd_device *mdd,
1973 struct mdd_object *p, struct mdd_object *c,
1974 struct lu_attr *attr,
1975 struct thandle *handle,
1976 const struct md_op_spec *spec,
1977 struct lu_buf *def_acl_buf,
1978 struct lu_buf *acl_buf,
1979 struct dt_allocation_hint *hint)
1983 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1988 #ifdef CONFIG_FS_POSIX_ACL
1989 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1990 /* if dir, then can inherit default ACl */
1991 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1992 XATTR_NAME_ACL_DEFAULT,
1998 if (acl_buf->lb_len > 0) {
1999 rc = mdo_declare_attr_set(env, c, attr, handle);
2003 rc = mdo_declare_xattr_set(env, c, acl_buf,
2004 XATTR_NAME_ACL_ACCESS, 0, handle);
2009 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2013 /* replay case, create LOV EA from client data */
2014 if (spec->no_create ||
2015 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2016 const struct lu_buf *buf;
2018 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2019 spec->u.sp_ea.eadatalen);
2020 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2026 if (S_ISLNK(attr->la_mode)) {
2027 const char *target_name = spec->u.sp_symname;
2028 int sym_len = strlen(target_name);
2029 const struct lu_buf *buf;
2031 buf = mdd_buf_get_const(env, target_name, sym_len);
2032 rc = dt_declare_record_write(env, mdd_object_child(c),
2041 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2042 struct mdd_object *p, struct mdd_object *c,
2043 const struct lu_name *name,
2044 struct lu_attr *attr,
2045 struct thandle *handle,
2046 const struct md_op_spec *spec,
2047 struct linkea_data *ldata,
2048 struct lu_buf *def_acl_buf,
2049 struct lu_buf *acl_buf,
2050 struct dt_allocation_hint *hint)
2054 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2055 def_acl_buf, acl_buf, hint);
2059 if (S_ISDIR(attr->la_mode)) {
2060 rc = mdo_declare_ref_add(env, p, handle);
2065 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2066 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2070 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2072 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2073 name->ln_name, handle);
2077 rc = mdd_declare_links_add(env, c, handle, ldata);
2082 la->la_valid = LA_CTIME | LA_MTIME;
2083 rc = mdo_declare_attr_set(env, p, la, handle);
2087 rc = mdd_declare_changelog_store(env, mdd, name, handle);
2092 /* XXX: For remote create, it should indicate the remote RPC
2093 * will be sent after local transaction is finished, which
2094 * is not very nice, but it will be removed once we fully support
2096 if (mdd_object_remote(p) && handle->th_update != NULL)
2097 handle->th_update->tu_sent_after_local_trans = 1;
2102 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2103 struct lu_attr *la, struct lu_buf *def_acl_buf,
2104 struct lu_buf *acl_buf)
2109 if (S_ISLNK(la->la_mode)) {
2110 acl_buf->lb_len = 0;
2111 def_acl_buf->lb_len = 0;
2115 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2116 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2117 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2118 mdd_read_unlock(env, pobj);
2120 /* If there are default ACL, fix mode/ACL by default ACL */
2121 def_acl_buf->lb_len = rc;
2122 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2123 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2124 acl_buf->lb_len = rc;
2125 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2128 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2129 /* If there are no default ACL, fix mode by mask */
2130 struct lu_ucred *uc = lu_ucred(env);
2132 /* The create triggered by MDT internal events, such as
2133 * LFSCK reset, will not contain valid "uc". */
2134 if (unlikely(uc != NULL))
2135 la->la_mode &= ~uc->uc_umask;
2137 acl_buf->lb_len = 0;
2138 def_acl_buf->lb_len = 0;
2145 * Create a metadata object and initialize it, set acl, xattr.
2147 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2148 struct mdd_object *son, struct lu_attr *attr,
2149 struct md_op_spec *spec, struct lu_buf *acl_buf,
2150 struct lu_buf *def_acl_buf,
2151 struct dt_allocation_hint *hint,
2152 struct thandle *handle)
2156 mdd_write_lock(env, son, MOR_TGT_CHILD);
2157 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2162 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2163 * created in declare phase, they also needs to be added to master
2164 * object as sub-directory entry. So it has to initialize the master
2165 * object, then set dir striped EA.(in mdo_xattr_set) */
2166 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2169 GOTO(err_destroy, rc);
2172 * in case of replay we just set LOVEA provided by the client
2173 * XXX: I think it would be interesting to try "old" way where
2174 * MDT calls this xattr_set(LOV) in a different transaction.
2175 * probably this way we code can be made better.
2178 /* During creation, there are only a few cases we need do xattr_set to
2180 * 1. regular file: see comments above.
2181 * 2. create striped directory with provided stripeEA.
2182 * 3. create striped directory because inherit default layout from the
2184 if (spec->no_create ||
2185 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2186 S_ISDIR(attr->la_mode)) {
2187 const struct lu_buf *buf;
2189 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2190 spec->u.sp_ea.eadatalen);
2191 rc = mdo_xattr_set(env, son, buf,
2192 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2194 handle, BYPASS_CAPA);
2196 GOTO(err_destroy, rc);
2199 #ifdef CONFIG_FS_POSIX_ACL
2200 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2201 S_ISDIR(attr->la_mode)) {
2202 /* set default acl */
2203 rc = mdo_xattr_set(env, son, def_acl_buf,
2204 XATTR_NAME_ACL_DEFAULT, 0,
2205 handle, BYPASS_CAPA);
2207 GOTO(err_destroy, rc);
2209 /* set its own acl */
2210 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2211 rc = mdo_xattr_set(env, son, acl_buf,
2212 XATTR_NAME_ACL_ACCESS,
2213 0, handle, BYPASS_CAPA);
2215 GOTO(err_destroy, rc);
2219 if (S_ISLNK(attr->la_mode)) {
2220 struct lu_ucred *uc = lu_ucred_assert(env);
2221 struct dt_object *dt = mdd_object_child(son);
2222 const char *target_name = spec->u.sp_symname;
2223 int sym_len = strlen(target_name);
2224 const struct lu_buf *buf;
2227 buf = mdd_buf_get_const(env, target_name, sym_len);
2228 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2229 mdd_object_capa(env, son),
2231 CFS_CAP_SYS_RESOURCE_MASK);
2236 GOTO(err_initlized, rc = -EFAULT);
2240 if (unlikely(rc != 0)) {
2242 if (S_ISDIR(attr->la_mode)) {
2243 /* Drop the reference, no need to delete "."/"..",
2244 * because the object to be destroied directly. */
2245 rc2 = mdo_ref_del(env, son, handle);
2249 rc2 = mdo_ref_del(env, son, handle);
2253 mdo_destroy(env, son, handle);
2256 mdd_write_unlock(env, son);
2261 * Create object and insert it into namespace.
2263 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2264 const struct lu_name *lname, struct md_object *child,
2265 struct md_op_spec *spec, struct md_attr* ma)
2267 struct mdd_thread_info *info = mdd_env_info(env);
2268 struct lu_attr *la = &info->mti_la_for_fix;
2269 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2270 struct mdd_object *son = md2mdd_obj(child);
2271 struct mdd_device *mdd = mdo2mdd(pobj);
2272 struct lu_attr *attr = &ma->ma_attr;
2273 struct thandle *handle;
2274 struct lu_attr *pattr = &info->mti_pattr;
2275 struct lu_buf acl_buf;
2276 struct lu_buf def_acl_buf;
2277 struct linkea_data *ldata = &info->mti_link_data;
2278 const char *name = lname->ln_name;
2279 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2285 * Two operations have to be performed:
2287 * - an allocation of a new object (->do_create()), and
2289 * - an insertion into a parent index (->dio_insert()).
2291 * Due to locking, operation order is not important, when both are
2292 * successful, *but* error handling cases are quite different:
2294 * - if insertion is done first, and following object creation fails,
2295 * insertion has to be rolled back, but this operation might fail
2296 * also leaving us with dangling index entry.
2298 * - if creation is done first, is has to be undone if insertion
2299 * fails, leaving us with leaked space, which is neither good, nor
2302 * It seems that creation-first is simplest solution, but it is
2303 * sub-optimal in the frequent
2308 * case, because second mkdir is bound to create object, only to
2309 * destroy it immediately.
2311 * To avoid this follow local file systems that do double lookup:
2313 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2315 * 1. create (mdd_object_create_internal())
2317 * 2. insert (__mdd_index_insert(), lookup again)
2320 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2324 /* Sanity checks before big job. */
2325 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2329 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2330 GOTO(out_free, rc = -EINPROGRESS);
2332 handle = mdd_trans_create(env, mdd);
2334 GOTO(out_free, rc = PTR_ERR(handle));
2336 acl_buf.lb_buf = info->mti_xattr_buf;
2337 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2338 def_acl_buf.lb_buf = info->mti_key;
2339 def_acl_buf.lb_len = sizeof(info->mti_key);
2340 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2344 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2346 memset(ldata, 0, sizeof(*ldata));
2347 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2348 mdd_object_fid(mdd_pobj),
2349 lname, 1, 0, ldata);
2351 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2352 handle, spec, ldata, &def_acl_buf, &acl_buf,
2357 rc = mdd_trans_start(env, mdd, handle);
2361 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2362 &def_acl_buf, hint, handle);
2366 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2367 mdd_write_lock(env, son, MOR_TGT_CHILD);
2368 rc = __mdd_orphan_add(env, son, handle);
2369 GOTO(out_volatile, rc);
2371 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2372 attr->la_mode, name, handle,
2373 mdd_object_capa(env, mdd_pobj));
2375 GOTO(err_created, rc);
2377 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2380 /* update parent directory mtime/ctime */
2382 la->la_valid = LA_CTIME | LA_MTIME;
2383 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2385 GOTO(err_insert, rc);
2393 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2394 rc2 = __mdd_orphan_del(env, son, handle);
2396 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2397 S_ISDIR(attr->la_mode),
2398 handle, BYPASS_CAPA);
2403 mdd_write_lock(env, son, MOR_TGT_CHILD);
2404 if (S_ISDIR(attr->la_mode)) {
2405 /* Drop the reference, no need to delete "."/"..",
2406 * because the object is to be destroyed directly. */
2407 rc2 = mdo_ref_del(env, son, handle);
2409 mdd_write_unlock(env, son);
2414 /* For volatile files drop one link immediately, since there is
2415 * no filename in the namespace, and save any error returned. */
2416 rc2 = mdo_ref_del(env, son, handle);
2418 mdd_write_unlock(env, son);
2419 if (unlikely(rc == 0))
2424 /* Don't destroy the volatile object on success */
2425 if (likely(rc != 0))
2426 mdo_destroy(env, son, handle);
2427 mdd_write_unlock(env, son);
2430 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2431 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2432 rc = mdd_changelog_ns_store(env, mdd,
2433 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2434 S_ISREG(attr->la_mode) ? CL_CREATE :
2435 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2436 0, son, mdd_pobj, lname, handle);
2438 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2442 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2443 /* if we vmalloced a large buffer drop it */
2444 lu_buf_free(ldata->ld_buf);
2446 /* The child object shouldn't be cached anymore */
2448 set_bit(LU_OBJECT_HEARD_BANSHEE,
2449 &child->mo_lu.lo_header->loh_flags);
2454 * Get locks on parents in proper order
2455 * RETURN: < 0 - error, rename_order if successful
2463 static int mdd_rename_order(const struct lu_env *env,
2464 struct mdd_device *mdd,
2465 struct mdd_object *src_pobj,
2466 const struct lu_attr *pattr,
2467 struct mdd_object *tgt_pobj)
2469 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2473 if (src_pobj == tgt_pobj)
2474 RETURN(MDD_RN_SAME);
2476 /* compared the parent child relationship of src_p&tgt_p */
2477 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2479 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2482 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2496 /* has not mdd_write{read}_lock on any obj yet. */
2497 static int mdd_rename_sanity_check(const struct lu_env *env,
2498 struct mdd_object *src_pobj,
2499 const struct lu_attr *pattr,
2500 struct mdd_object *tgt_pobj,
2501 const struct lu_attr *tpattr,
2502 struct mdd_object *sobj,
2503 const struct lu_attr *cattr,
2504 struct mdd_object *tobj,
2505 const struct lu_attr *tattr)
2510 /* XXX: when get here, sobj must NOT be NULL,
2511 * the other case has been processed in cld_rename
2512 * before mdd_rename and enable MDS_PERM_BYPASS. */
2515 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2519 /* XXX: when get here, "tobj == NULL" means tobj must
2520 * NOT exist (neither on remote MDS, such case has been
2521 * processed in cld_rename before mdd_rename and enable
2523 * So check may_create, but not check may_unlink. */
2525 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2526 (src_pobj != tgt_pobj));
2528 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2529 (src_pobj != tgt_pobj), 1);
2531 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2532 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2537 static int mdd_declare_rename(const struct lu_env *env,
2538 struct mdd_device *mdd,
2539 struct mdd_object *mdd_spobj,
2540 struct mdd_object *mdd_tpobj,
2541 struct mdd_object *mdd_sobj,
2542 struct mdd_object *mdd_tobj,
2543 const struct lu_name *tname,
2544 const struct lu_name *sname,
2546 struct linkea_data *ldata,
2547 struct thandle *handle)
2549 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2552 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2553 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2559 /* name from source dir */
2560 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2564 /* .. from source child */
2565 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2566 /* source child can be directory,
2567 * counted by source dir's nlink */
2568 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2571 if (mdd_spobj != mdd_tpobj) {
2572 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2577 rc = mdo_declare_index_insert(env, mdd_sobj,
2579 S_IFDIR, dotdot, handle);
2584 /* new target child can be directory,
2585 * counted by target dir's nlink */
2586 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2591 la->la_valid = LA_CTIME | LA_MTIME;
2592 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2596 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2600 la->la_valid = LA_CTIME;
2601 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2605 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2610 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2611 mdd_object_type(mdd_sobj),
2612 tname->ln_name, handle);
2616 /* name from target dir (old name), we declare it unconditionally
2617 * as mdd_rename() calls delete unconditionally as well. so just
2618 * to balance declarations vs calls to change ... */
2619 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2623 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2624 /* delete target child in target parent directory */
2625 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2629 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2630 /* target child can be directory,
2631 * delete "." reference in target child directory */
2632 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2636 /* delete ".." reference in target parent directory */
2637 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2642 la->la_valid = LA_CTIME;
2643 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2647 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2652 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2659 /* src object can be remote that is why we use only fid and type of object */
2660 static int mdd_rename(const struct lu_env *env,
2661 struct md_object *src_pobj, struct md_object *tgt_pobj,
2662 const struct lu_fid *lf, const struct lu_name *lsname,
2663 struct md_object *tobj, const struct lu_name *ltname,
2666 const char *sname = lsname->ln_name;
2667 const char *tname = ltname->ln_name;
2668 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2669 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2670 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2671 struct mdd_device *mdd = mdo2mdd(src_pobj);
2672 struct mdd_object *mdd_sobj = NULL; /* source object */
2673 struct mdd_object *mdd_tobj = NULL;
2674 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2675 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2676 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2677 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2678 struct thandle *handle;
2679 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2680 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2681 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2684 bool tobj_locked = 0;
2685 unsigned cl_flags = 0;
2690 mdd_tobj = md2mdd_obj(tobj);
2692 mdd_sobj = mdd_object_find(env, mdd, lf);
2694 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2696 GOTO(out_pending, rc);
2698 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2700 GOTO(out_pending, rc);
2703 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2705 GOTO(out_pending, rc);
2708 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2710 GOTO(out_pending, rc);
2712 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2713 mdd_sobj, cattr, mdd_tobj, tattr);
2715 GOTO(out_pending, rc);
2717 rc = mdd_name_check(mdd, ltname);
2719 GOTO(out_pending, rc);
2721 handle = mdd_trans_create(env, mdd);
2723 GOTO(out_pending, rc = PTR_ERR(handle));
2725 memset(ldata, 0, sizeof(*ldata));
2726 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2727 ltname, 1, 0, ldata);
2728 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2729 mdd_tobj, lsname, ltname, ma, ldata, handle);
2733 rc = mdd_trans_start(env, mdd, handle);
2737 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2738 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2740 GOTO(cleanup_unlocked, rc);
2742 is_dir = S_ISDIR(cattr->la_mode);
2744 /* Remove source name from source directory */
2745 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2746 mdd_object_capa(env, mdd_spobj));
2750 /* "mv dir1 dir2" needs "dir1/.." link update */
2751 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2752 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2753 mdd_object_capa(env, mdd_sobj));
2755 GOTO(fixup_spobj2, rc);
2757 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2759 mdd_object_capa(env, mdd_sobj));
2761 GOTO(fixup_spobj, rc);
2764 /* Remove target name from target directory
2765 * Here tobj can be remote one, so we do index_delete unconditionally
2766 * and -ENOENT is allowed.
2768 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2769 mdd_object_capa(env, mdd_tpobj));
2772 /* tname might been renamed to something else */
2773 GOTO(fixup_spobj, rc);
2776 GOTO(fixup_spobj, rc);
2779 /* Insert new fid with target name into target dir */
2780 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2781 tname, handle, mdd_object_capa(env, mdd_tpobj));
2783 GOTO(fixup_tpobj, rc);
2785 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2786 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2788 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2790 la->la_valid = LA_CTIME;
2791 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2793 GOTO(fixup_tpobj, rc);
2796 /* Remove old target object
2797 * For tobj is remote case cmm layer has processed
2798 * and set tobj to NULL then. So when tobj is NOT NULL,
2799 * it must be local one.
2801 if (tobj && mdd_object_exists(mdd_tobj)) {
2802 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2804 if (mdd_is_dead_obj(mdd_tobj)) {
2805 /* shld not be dead, something is wrong */
2806 CERROR("tobj is dead, something is wrong\n");
2810 mdo_ref_del(env, mdd_tobj, handle);
2812 /* Remove dot reference. */
2813 if (S_ISDIR(tattr->la_mode))
2814 mdo_ref_del(env, mdd_tobj, handle);
2817 /* fetch updated nlink */
2818 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2820 CERROR("%s: Failed to get nlink for tobj "
2822 mdd2obd_dev(mdd)->obd_name,
2823 PFID(tpobj_fid), rc);
2824 GOTO(fixup_tpobj, rc);
2827 la->la_valid = LA_CTIME;
2828 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2830 CERROR("%s: Failed to set ctime for tobj "
2832 mdd2obd_dev(mdd)->obd_name,
2833 PFID(tpobj_fid), rc);
2834 GOTO(fixup_tpobj, rc);
2837 /* XXX: this transfer to ma will be removed with LOD/OSP */
2838 ma->ma_attr = *tattr;
2839 ma->ma_valid |= MA_INODE;
2840 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2843 CERROR("%s: Failed to unlink tobj "
2845 mdd2obd_dev(mdd)->obd_name,
2846 PFID(tpobj_fid), rc);
2847 GOTO(fixup_tpobj, rc);
2850 /* fetch updated nlink */
2851 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2853 CERROR("%s: Failed to get nlink for tobj "
2855 mdd2obd_dev(mdd)->obd_name,
2856 PFID(tpobj_fid), rc);
2857 GOTO(fixup_tpobj, rc);
2859 /* XXX: this transfer to ma will be removed with LOD/OSP */
2860 ma->ma_attr = *tattr;
2861 ma->ma_valid |= MA_INODE;
2863 if (tattr->la_nlink == 0) {
2864 cl_flags |= CLF_RENAME_LAST;
2865 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2866 cl_flags |= CLF_RENAME_LAST_EXISTS;
2870 la->la_valid = LA_CTIME | LA_MTIME;
2871 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2873 GOTO(fixup_tpobj, rc);
2875 if (mdd_spobj != mdd_tpobj) {
2876 la->la_valid = LA_CTIME | LA_MTIME;
2877 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2880 if (rc == 0 && mdd_sobj) {
2881 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2882 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2883 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2886 /* Old files might not have EA entry */
2887 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2888 lsname, handle, NULL, 0);
2889 mdd_write_unlock(env, mdd_sobj);
2890 /* We don't fail the transaction if the link ea can't be
2891 updated -- fid2path will use alternate lookup method. */
2899 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2902 CWARN("tp obj fix error %d\n",rc2);
2904 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2905 !mdd_is_dead_obj(mdd_tobj)) {
2907 mdo_ref_add(env, mdd_tobj, handle);
2909 mdo_ref_add(env, mdd_tobj, handle);
2912 rc2 = __mdd_index_insert(env, mdd_tpobj,
2914 mdd_object_type(mdd_tobj),
2915 tname, handle, BYPASS_CAPA);
2917 CWARN("tp obj fix error: rc = %d\n", rc2);
2922 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2923 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2927 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2928 mdd2obd_dev(mdd)->obd_name, rc2);
2931 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2932 dotdot, handle, BYPASS_CAPA);
2934 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2935 mdd2obd_dev(mdd)->obd_name, rc2);
2940 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2941 mdd_object_type(mdd_sobj), sname,
2942 handle, BYPASS_CAPA);
2944 CWARN("sp obj fix error: rc = %d\n", rc2);
2949 mdd_write_unlock(env, mdd_tobj);
2953 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2954 mdd_tobj, tpobj_fid, lf,
2955 spobj_fid, ltname, lsname,
2959 mdd_trans_stop(env, mdd, rc, handle);
2962 mdd_object_put(env, mdd_sobj);
2967 * During migration once the parent FID has been changed,
2968 * we need update the parent FID in linkea.
2970 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2971 struct mdd_object *parent,
2972 struct mdd_object *child,
2973 const char *name, int namelen,
2974 struct thandle *handle,
2977 struct mdd_thread_info *info = mdd_env_info(env);
2978 struct linkea_data ldata = {0};
2979 struct lu_buf *buf = &info->mti_link_buf;
2985 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2986 if (buf->lb_buf == NULL)
2990 rc = mdd_links_read(env, child, &ldata);
2992 if (rc == -ENOENT || rc == -ENODATA)
2997 LASSERT(ldata.ld_leh != NULL);
2998 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2999 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3000 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3001 struct lu_name lname;
3004 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3007 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3008 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3009 ldata.ld_lee = (struct link_ea_entry *)
3010 ((char *)ldata.ld_lee +
3015 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3016 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3017 lname.ln_namelen, lname.ln_name,
3018 PFID(mdd_object_fid(parent)));
3019 /* update to the new parent fid */
3020 linkea_entry_pack(ldata.ld_lee, &lname,
3021 mdd_object_fid(parent));
3023 rc = mdd_declare_links_add(env, child, handle, &ldata);
3025 rc = mdd_links_write(env, child, &ldata, handle);
3031 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3032 struct mdd_object *parent,
3033 struct mdd_object *child,
3034 const char *name, int namelen,
3035 struct thandle *handle)
3037 return mdd_linkea_update_child_internal(env, parent, child, name,
3038 namelen, handle, true);
3041 static int mdd_linkea_update_child(const struct lu_env *env,
3042 struct mdd_object *parent,
3043 struct mdd_object *child,
3044 const char *name, int namelen,
3045 struct thandle *handle)
3047 return mdd_linkea_update_child_internal(env, parent, child, name,
3048 namelen, handle, false);
3051 static int mdd_update_linkea_internal(const struct lu_env *env,
3052 struct mdd_object *mdd_pobj,
3053 struct mdd_object *mdd_sobj,
3054 struct mdd_object *mdd_tobj,
3055 const struct lu_name *child_name,
3056 struct thandle *handle,
3059 struct mdd_thread_info *info = mdd_env_info(env);
3060 struct linkea_data *ldata = &info->mti_link_data;
3065 rc = mdd_links_read(env, mdd_sobj, ldata);
3067 if (rc == -ENOENT || rc == -ENODATA)
3073 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3075 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3080 /* If it is mulitple links file, we need update the name entry for
3082 LASSERT(ldata->ld_leh != NULL);
3083 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3084 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3085 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3086 struct mdd_object *pobj;
3087 struct lu_name lname;
3090 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3092 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3094 pobj = mdd_object_find(env, mdd, &fid);
3096 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3097 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3102 if (!mdd_object_exists(pobj)) {
3103 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3104 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3108 if (pobj == mdd_pobj &&
3109 lname.ln_namelen == child_name->ln_namelen &&
3110 strncmp(lname.ln_name, child_name->ln_name,
3111 lname.ln_namelen) == 0) {
3112 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3113 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3118 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3119 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3120 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3123 /* Remove source name from source directory */
3124 /* Insert new fid with target name into target dir */
3125 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3130 rc = mdo_declare_index_insert(env, pobj,
3131 mdd_object_fid(mdd_tobj),
3132 mdd_object_type(mdd_tobj),
3133 lname.ln_name, handle);
3137 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3141 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3145 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3147 mdd_object_capa(env, pobj));
3151 rc = __mdd_index_insert(env, pobj,
3152 mdd_object_fid(mdd_tobj),
3153 mdd_object_type(mdd_tobj),
3154 lname.ln_name, handle,
3155 mdd_object_capa(env, pobj));
3159 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3160 rc = mdo_ref_add(env, mdd_tobj, handle);
3161 mdd_write_unlock(env, mdd_tobj);
3165 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3166 mdo_ref_del(env, mdd_sobj, handle);
3167 mdd_write_unlock(env, mdd_sobj);
3170 mdd_object_put(env, pobj);
3178 static int mdd_migrate_xattrs(const struct lu_env *env,
3179 struct mdd_object *mdd_sobj,
3180 struct mdd_object *mdd_tobj)
3182 struct mdd_thread_info *info = mdd_env_info(env);
3183 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3185 struct thandle *handle;
3191 struct lu_buf list_xbuf;
3194 /* retrieve xattr list from the old object */
3195 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3196 mdd_object_capa(env, mdd_sobj));
3197 if (list_xsize == -ENODATA)
3203 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3204 if (info->mti_big_buf.lb_buf == NULL)
3207 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3208 list_xbuf.lb_len = list_xsize;
3209 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3210 mdd_object_capa(env, mdd_sobj));
3215 xname = list_xbuf.lb_buf;
3217 xlen = strnlen(xname, rem - 1) + 1;
3218 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3219 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3220 strcmp(XATTR_NAME_LMV, xname) == 0)
3223 /* For directory, if there are default layout, migrate here */
3224 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3225 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3228 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3230 mdd_object_capa(env, mdd_sobj));
3231 if (xsize == -ENODATA)
3236 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3237 if (info->mti_link_buf.lb_buf == NULL)
3238 GOTO(out, rc = -ENOMEM);
3240 xbuf.lb_len = xsize;
3241 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3242 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3243 mdd_object_capa(env, mdd_sobj));
3249 handle = mdd_trans_create(env, mdd);
3251 GOTO(out, rc = PTR_ERR(handle));
3253 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3256 GOTO(stop_trans, rc);
3257 /* Note: this transaction is part of migration, and it is not
3258 * the last step of migration, so we set th_local = 1 to avoid
3259 * update last rcvd for this transaction */
3260 handle->th_local = 1;
3261 rc = mdd_trans_start(env, mdd, handle);
3263 GOTO(stop_trans, rc);
3265 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3266 mdd_object_capa(env, mdd_sobj));
3268 GOTO(stop_trans, rc = 0);
3271 GOTO(stop_trans, rc);
3273 mdd_trans_stop(env, mdd, rc, handle);
3278 memmove(xname, xname + xlen, rem);
3284 static int mdd_declare_migrate_create(const struct lu_env *env,
3285 struct mdd_object *mdd_pobj,
3286 struct mdd_object *mdd_sobj,
3287 struct mdd_object *mdd_tobj,
3288 struct md_op_spec *spec,
3290 union lmv_mds_md *mgr_ea,
3291 struct thandle *handle)
3293 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3294 const struct lu_buf *buf;
3298 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3299 handle, spec, NULL);
3303 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3308 if (S_ISLNK(la->la_mode)) {
3309 const char *target_name = spec->u.sp_symname;
3310 int sym_len = strlen(target_name);
3311 const struct lu_buf *buf;
3313 buf = mdd_buf_get_const(env, target_name, sym_len);
3314 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3320 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3321 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3322 spec->u.sp_ea.eadatalen);
3323 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3329 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3330 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3331 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3336 la_flag->la_valid = LA_FLAGS;
3337 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3338 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3339 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3344 static int mdd_migrate_create(const struct lu_env *env,
3345 struct mdd_object *mdd_pobj,
3346 struct mdd_object *mdd_sobj,
3347 struct mdd_object *mdd_tobj,
3350 struct mdd_thread_info *info = mdd_env_info(env);
3351 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3352 struct md_op_spec *spec = &info->mti_spec;
3353 struct lu_buf lmm_buf = { 0 };
3354 struct lu_buf link_buf = { 0 };
3355 const struct lu_buf *buf;
3356 struct thandle *handle;
3357 struct lmv_mds_md_v1 *mgr_ea;
3358 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3359 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3364 /* prepare spec for create */
3365 memset(spec, 0, sizeof(*spec));
3366 spec->sp_cr_lookup = 0;
3367 spec->sp_feat = &dt_directory_features;
3368 if (S_ISLNK(la->la_mode)) {
3369 buf = lu_buf_check_and_alloc(
3370 &mdd_env_info(env)->mti_big_buf,
3373 link_buf.lb_len = la->la_size + 1;
3374 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3376 rc = rc != 0 ? rc : -EFAULT;
3377 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3378 mdd2obd_dev(mdd)->obd_name,
3379 PFID(mdd_object_fid(mdd_sobj)), rc);
3382 spec->u.sp_symname = link_buf.lb_buf;
3383 } else if S_ISREG(la->la_mode) {
3384 /* retrieve lov of the old object */
3385 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3386 if (rc != 0 && rc != -ENODATA)
3388 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3389 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3390 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3391 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3395 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3396 memset(mgr_ea, 0, sizeof(*mgr_ea));
3397 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3398 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3399 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3400 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3401 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3402 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3404 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3406 handle = mdd_trans_create(env, mdd);
3408 GOTO(out_free, rc = PTR_ERR(handle));
3410 /* Note: this transaction is part of migration, and it is not
3411 * the last step of migration, so we set th_local = 1 to avoid
3412 * update last rcvd for this transaction */
3413 handle->th_local = 1;
3414 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3416 (union lmv_mds_md *)info->mti_xattr_buf,
3419 GOTO(stop_trans, rc);
3421 rc = mdd_trans_start(env, mdd, handle);
3423 GOTO(stop_trans, rc);
3425 /* create the target object */
3426 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3429 GOTO(stop_trans, rc);
3431 /* Set MIGRATE EA on the source inode, so once the migration needs
3432 * to be re-done during failover, the re-do process can locate the
3433 * target object which is already being created. */
3434 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3435 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3436 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3437 handle, mdd_object_capa(env, mdd_sobj));
3439 GOTO(stop_trans, rc);
3441 /* Set immutable flag, so any modification is disabled until
3442 * the migration is done. Once the migration is interrupted,
3443 * if the resume process find the migrating object has both
3444 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3445 * flag and approve the migration */
3446 la_flag->la_valid = LA_FLAGS;
3447 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3448 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3449 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3450 mdd_object_capa(env, mdd_sobj));
3453 mdd_trans_stop(env, mdd, rc, handle);
3455 if (lmm_buf.lb_buf != NULL)
3456 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3460 static int mdd_migrate_entries(const struct lu_env *env,
3461 struct mdd_object *mdd_sobj,
3462 struct mdd_object *mdd_tobj)
3464 struct dt_object *next = mdd_object_child(mdd_sobj);
3465 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3466 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3467 struct thandle *handle;
3469 const struct dt_it_ops *iops;
3472 struct lu_dirent *ent;
3475 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3479 if (!dt_try_as_dir(env, next))
3480 GOTO(out_ent, rc = -ENOTDIR);
3482 * iterate directories
3484 iops = &next->do_index_ops->dio_it;
3485 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3486 mdd_object_capa(env, mdd_sobj));
3488 GOTO(out_ent, rc = PTR_ERR(it));
3490 rc = iops->load(env, it, 0);
3492 rc = iops->next(env, it);
3496 * At this point and across for-loop:
3498 * rc == 0 -> ok, proceed.
3499 * rc > 0 -> end of directory.
3503 struct mdd_object *child;
3504 char *name = mdd_env_info(env)->mti_key;
3508 bool target_exist = false;
3510 len = iops->key_size(env, it);
3514 result = iops->rec(env, it, (struct dt_rec *)ent,
3515 LUDA_FID | LUDA_TYPE);
3516 if (result == -ESTALE)
3523 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3524 recsize = le16_to_cpu(ent->lde_reclen);
3526 /* Insert new fid with target name into target dir */
3527 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3528 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3529 ent->lde_name[1] == '.'))
3532 child = mdd_object_find(env, mdd, &ent->lde_fid);
3534 GOTO(out, rc = PTR_ERR(child));
3536 is_dir = S_ISDIR(mdd_object_type(child));
3538 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3540 /* Check whether the name has been inserted to the target */
3541 if (dt_try_as_dir(env, dt_tobj)) {
3542 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3544 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3545 (struct dt_rec *)fid,
3546 (struct dt_key *)name,
3547 mdd_object_capa(env, mdd_tobj));
3548 if (unlikely(rc == 0))
3549 target_exist = true;
3552 handle = mdd_trans_create(env, mdd);
3554 GOTO(out, rc = PTR_ERR(handle));
3556 /* Note: this transaction is part of migration, and it is not
3557 * the last step of migration, so we set th_local = 1 to avoid
3558 * updating last rcvd for this transaction */
3559 handle->th_local = 1;
3560 if (likely(!target_exist)) {
3561 rc = mdo_declare_index_insert(env, mdd_tobj,
3563 mdd_object_type(child),
3569 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3575 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3580 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3584 /* Update .. for child */
3585 rc = mdo_declare_index_delete(env, child, dotdot,
3590 rc = mdo_declare_index_insert(env, child,
3591 mdd_object_fid(mdd_tobj),
3592 S_IFDIR, dotdot, handle);
3597 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3604 rc = mdd_trans_start(env, mdd, handle);
3606 CERROR("%s: transaction start failed: rc = %d\n",
3607 mdd2obd_dev(mdd)->obd_name, rc);
3611 if (likely(!target_exist)) {
3612 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3613 mdd_object_type(child),
3615 mdd_object_capa(env, mdd_tobj));
3620 rc = mdo_ref_add(env, mdd_tobj, handle);
3627 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3628 mdd_object_capa(env, mdd_sobj));
3633 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3634 mdd_object_capa(env, child));
3638 rc = __mdd_index_insert_only(env, child,
3639 mdd_object_fid(mdd_tobj), S_IFDIR,
3641 mdd_object_capa(env, child));
3646 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3647 strlen(name), handle);
3650 mdd_object_put(env, child);
3651 mdd_trans_stop(env, mdd, rc, handle);
3655 result = iops->next(env, it);
3656 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3657 GOTO(out, rc = -EINTR);
3659 if (result == -ESTALE)
3661 } while (result == 0);
3664 iops->fini(env, it);
3666 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3670 static int mdd_declare_update_linkea(const struct lu_env *env,
3671 struct mdd_object *mdd_pobj,
3672 struct mdd_object *mdd_sobj,
3673 struct mdd_object *mdd_tobj,
3674 struct thandle *handle,
3675 const struct lu_name *child_name)
3677 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3678 child_name, handle, 1);
3681 static int mdd_update_linkea(const struct lu_env *env,
3682 struct mdd_object *mdd_pobj,
3683 struct mdd_object *mdd_sobj,
3684 struct mdd_object *mdd_tobj,
3685 struct thandle *handle,
3686 const struct lu_name *child_name)
3688 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3689 child_name, handle, 0);
3692 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3693 struct mdd_object *mdd_pobj,
3694 struct mdd_object *mdd_sobj,
3695 struct mdd_object *mdd_tobj,
3696 const struct lu_name *lname,
3698 struct lu_attr *parent_la,
3699 struct thandle *handle)
3701 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3704 /* Revert IMMUTABLE flag */
3705 la_flag->la_valid = LA_FLAGS;
3706 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3707 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3708 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3712 /* delete entry from source dir */
3713 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3717 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3718 mdd_tobj, handle, lname);
3722 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3723 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3729 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3730 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3736 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3737 mdd_object_type(mdd_tobj),
3738 lname->ln_name, handle);
3742 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3743 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3748 /* delete old object */
3749 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3753 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3754 /* delete old object */
3755 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3758 /* set nlink to 0 */
3759 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3764 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3768 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3773 static int mdd_migrate_update_name(const struct lu_env *env,
3774 struct mdd_object *mdd_pobj,
3775 struct mdd_object *mdd_sobj,
3776 struct mdd_object *mdd_tobj,
3777 const struct lu_name *lname,
3780 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3781 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3782 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3783 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3784 struct thandle *handle;
3785 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3786 const char *name = lname->ln_name;
3790 /* update time for parent */
3791 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3792 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3793 p_la->la_valid = LA_CTIME;
3795 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3799 handle = mdd_trans_create(env, mdd);
3801 RETURN(PTR_ERR(handle));
3803 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3804 lname, so_attr, p_la, handle);
3806 /* If the migration can not be fit in one transaction, just
3807 * leave it in the original MDT */
3809 GOTO(stop_trans, rc = 0);
3811 GOTO(stop_trans, rc);
3814 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3815 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3816 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3817 PFID(mdd_object_fid(mdd_tobj)));
3819 rc = mdd_trans_start(env, mdd, handle);
3821 GOTO(stop_trans, rc);
3823 /* Revert IMMUTABLE flag */
3824 la_flag->la_valid = LA_FLAGS;
3825 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3826 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3827 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3828 mdd_object_capa(env, mdd_pobj));
3830 GOTO(stop_trans, rc);
3832 /* Remove source name from source directory */
3833 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3834 mdd_object_capa(env, mdd_pobj));
3836 GOTO(stop_trans, rc);
3838 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3841 GOTO(stop_trans, rc);
3843 if (S_ISREG(so_attr->la_mode)) {
3844 if (so_attr->la_nlink == 1) {
3845 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3847 mdd_object_capa(env, mdd_sobj));
3848 if (rc != 0 && rc != -ENODATA)
3849 GOTO(stop_trans, rc);
3853 /* Insert new fid with target name into target dir */
3854 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3855 mdd_object_type(mdd_tobj), name,
3856 handle, mdd_object_capa(env, mdd_pobj));
3858 GOTO(stop_trans, rc);
3860 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3863 GOTO(stop_trans, rc);
3865 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3866 mdo_ref_del(env, mdd_sobj, handle);
3868 mdo_ref_del(env, mdd_sobj, handle);
3870 /* Get the attr again after ref_del */
3871 rc = mdd_la_get(env, mdd_sobj, so_attr,
3872 mdd_object_capa(env, mdd_sobj));
3874 GOTO(stop_trans, rc);
3875 ma->ma_attr = *so_attr;
3876 ma->ma_valid |= MA_INODE;
3877 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3879 GOTO(stop_trans, rc);
3881 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3883 GOTO(stop_trans, rc);
3885 mdd_write_unlock(env, mdd_sobj);
3888 mdd_trans_stop(env, mdd, rc, handle);
3894 * Check whether we should migrate the file/dir
3896 * < 0 permission check failed or other error.
3897 * = 0 the file can be migrated.
3898 * > 0 the file does not need to be migrated, mostly
3899 * for multiple link file
3901 static int mdd_migrate_sanity_check(const struct lu_env *env,
3902 struct mdd_object *pobj,
3903 const struct lu_attr *pattr,
3904 struct mdd_object *sobj,
3905 struct lu_attr *sattr)
3907 struct mdd_thread_info *info = mdd_env_info(env);
3908 struct linkea_data *ldata = &info->mti_link_data;
3910 struct lu_buf *mgr_buf;
3916 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3917 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3918 if (mgr_buf->lb_buf == NULL)
3921 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3922 mdd_object_capa(env, sobj));
3924 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3926 /* If the object has migrateEA, it means IMMUTE flag
3927 * is being set by previous migration process, so it
3928 * needs to override the IMMUTE flag, otherwise the
3929 * following sanity check will fail */
3930 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3931 LMV_HASH_FLAG_MIGRATION) {
3932 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3934 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3935 sobj->mod_flags &= ~IMMUTE_OBJ;
3936 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3937 mdd2obd_dev(mdd)->obd_name,
3938 PFID(mdd_object_fid(sobj)));
3942 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3943 sobj, sattr, NULL, NULL);
3947 /* Then it will check if the file should be migrated. If the file
3948 * has mulitple links, we only need migrate the file if all of its
3949 * entries has been migrated to the remote MDT */
3950 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3953 rc = mdd_links_read(env, sobj, ldata);
3955 /* For multiple links files, if there are no linkEA data at all,
3956 * means the file might be created before linkEA is enabled, and
3957 * all all of its links should not be migrated yet, otherwise
3958 * it should have some linkEA there */
3959 if (rc == -ENOENT || rc == -ENODATA)
3964 /* If it is mulitple links file, we need update the name entry for
3966 LASSERT(ldata->ld_leh != NULL);
3967 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3968 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3969 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3970 struct mdd_object *lpobj;
3971 struct lu_name lname;
3974 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3976 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3978 lpobj = mdd_object_find(env, mdd, &fid);
3979 if (IS_ERR(lpobj)) {
3980 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3981 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3986 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3987 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3988 PFID(&fid), lname.ln_namelen, lname.ln_name);
3989 mdd_object_put(env, lpobj);
3993 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3994 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3995 lname.ln_name, PFID(&fid));
3996 mdd_object_put(env, lpobj);
4004 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4005 struct md_object *sobj, const struct lu_name *lname,
4006 struct md_object *tobj, struct md_attr *ma)
4008 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4009 struct mdd_device *mdd = mdo2mdd(pobj);
4010 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4011 struct mdd_object *mdd_tobj = NULL;
4012 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4013 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4017 /* If the file will being migrated, it will check whether
4018 * the file is being opened by someone else right now */
4019 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4020 if (mdd_sobj->mod_count >= 1) {
4021 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4022 mdd2obd_dev(mdd)->obd_name,
4023 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4024 mdd_sobj->mod_count, -EBUSY);
4025 mdd_read_unlock(env, mdd_sobj);
4026 GOTO(put, rc = -EBUSY);
4028 mdd_read_unlock(env, mdd_sobj);
4030 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
4034 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
4038 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4045 /* Sigh, it is impossible to finish all of migration in a single
4046 * transaction, for example migrating big directory entries to the
4047 * new MDT, it needs insert all of name entries of children in the
4050 * So migration will be done in multiple steps and transactions.
4052 * 1. create an orphan object on the remote MDT in one transaction.
4053 * 2. migrate extend attributes to the new target file/directory.
4054 * 3. For directory, migrate the entries to the new MDT and update
4055 * linkEA of each children. Because we can not migrate all entries
4056 * in a single transaction, so the migrating directory will become
4057 * a striped directory during migration, so once the process is
4058 * interrupted, the directory is still accessible. (During lookup,
4059 * client will locate the name by searching both original and target
4061 * 4. Finally, update the name/FID to point to the new file/directory
4062 * in a separate transaction.
4065 /* step 1: Check whether the orphan object has been created, and create
4066 * orphan object on the remote MDT if needed */
4067 mdd_tobj = md2mdd_obj(tobj);
4068 if (!mdd_object_exists(mdd_tobj)) {
4069 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4075 /* step 2: migrate xattr */
4076 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4080 /* step 3: migrate name entries to the orphan object */
4081 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4082 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4085 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4086 OBD_FAIL_MDS_REINT_NET_REP)))
4090 /* step 4: update name entry to the new object */
4091 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4099 const struct md_dir_operations mdd_dir_ops = {
4100 .mdo_is_subdir = mdd_is_subdir,
4101 .mdo_lookup = mdd_lookup,
4102 .mdo_create = mdd_create,
4103 .mdo_rename = mdd_rename,
4104 .mdo_link = mdd_link,
4105 .mdo_unlink = mdd_unlink,
4106 .mdo_create_data = mdd_create_data,
4107 .mdo_migrate = mdd_migrate,