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, const char *name,
592 struct thandle *handle,
593 struct lustre_capa *capa)
595 struct dt_object *next = mdd_object_child(pobj);
599 if (dt_try_as_dir(env, next)) {
600 struct lu_ucred *uc = lu_ucred_check(env);
603 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
604 rc = next->do_index_ops->dio_insert(env, next,
606 (const struct dt_key *)name,
607 handle, capa, ignore_quota);
614 /* insert named index, add reference if isdir */
615 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
616 const struct lu_fid *lf, const char *name, int is_dir,
617 struct thandle *handle, struct lustre_capa *capa)
622 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
623 if (rc == 0 && is_dir) {
624 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
625 mdo_ref_add(env, pobj, handle);
626 mdd_write_unlock(env, pobj);
631 /* delete named index, drop reference if isdir */
632 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
633 const char *name, int is_dir, struct thandle *handle,
634 struct lustre_capa *capa)
639 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
640 if (rc == 0 && is_dir) {
641 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
642 mdo_ref_del(env, pobj, handle);
643 mdd_write_unlock(env, pobj);
649 int mdd_declare_changelog_store(const struct lu_env *env,
650 struct mdd_device *mdd,
651 const struct lu_name *fname,
652 struct thandle *handle)
654 struct obd_device *obd = mdd2obd_dev(mdd);
655 struct llog_ctxt *ctxt;
656 struct llog_changelog_rec *rec;
662 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
665 reclen = llog_data_len(sizeof(*rec) +
666 (fname != NULL ? fname->ln_namelen : 0));
667 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
668 if (buf->lb_buf == NULL)
672 rec->cr_hdr.lrh_len = reclen;
673 rec->cr_hdr.lrh_type = CHANGELOG_REC;
675 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
679 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
685 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
686 struct mdd_device *mdd,
687 const struct lu_name *tname,
688 const struct lu_name *sname,
689 struct thandle *handle)
691 struct obd_device *obd = mdd2obd_dev(mdd);
692 struct llog_ctxt *ctxt;
693 struct llog_changelog_ext_rec *rec;
699 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
702 reclen = llog_data_len(sizeof(*rec) +
703 (tname != NULL ? tname->ln_namelen : 0) +
704 (sname != NULL ? 1 + sname->ln_namelen : 0));
705 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
706 if (buf->lb_buf == NULL)
710 rec->cr_hdr.lrh_len = reclen;
711 rec->cr_hdr.lrh_type = CHANGELOG_REC;
713 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
717 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
723 /** Add a changelog entry \a rec to the changelog llog
726 * \param handle - currently ignored since llogs start their own transaction;
727 * this will hopefully be fixed in llog rewrite
730 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
731 struct llog_changelog_rec *rec, struct thandle *th)
733 struct obd_device *obd = mdd2obd_dev(mdd);
734 struct llog_ctxt *ctxt;
737 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
738 rec->cr_hdr.lrh_type = CHANGELOG_REC;
739 rec->cr.cr_time = cl_time();
741 spin_lock(&mdd->mdd_cl.mc_lock);
742 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
743 * but as long as the MDD transactions are ordered correctly for e.g.
744 * rename conflicts, I don't think this should matter. */
745 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
746 spin_unlock(&mdd->mdd_cl.mc_lock);
748 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
752 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
759 /** Add a changelog_ext entry \a rec to the changelog llog
762 * \param handle - currently ignored since llogs start their own transaction;
763 * this will hopefully be fixed in llog rewrite
767 mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
768 struct llog_changelog_ext_rec *rec, struct thandle *th)
770 struct obd_device *obd = mdd2obd_dev(mdd);
771 struct llog_ctxt *ctxt;
774 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
775 /* llog_lvfs_write_rec sets the llog tail len */
776 rec->cr_hdr.lrh_type = CHANGELOG_REC;
777 rec->cr.cr_time = cl_time();
779 spin_lock(&mdd->mdd_cl.mc_lock);
780 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
781 * but as long as the MDD transactions are ordered correctly for e.g.
782 * rename conflicts, I don't think this should matter. */
783 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
784 spin_unlock(&mdd->mdd_cl.mc_lock);
786 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
790 /* nested journal transaction */
791 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
799 /** Store a namespace change changelog record
800 * If this fails, we must fail the whole transaction; we don't
801 * want the change to commit without the log entry.
802 * \param target - mdd_object of change
803 * \param parent - parent dir/object
804 * \param tname - target name string
805 * \param handle - transacion handle
807 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
808 enum changelog_rec_type type, unsigned flags,
809 struct mdd_object *target, struct mdd_object *parent,
810 const struct lu_name *tname, struct thandle *handle)
812 struct llog_changelog_rec *rec;
819 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
821 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
824 LASSERT(target != NULL);
825 LASSERT(parent != NULL);
826 LASSERT(tname != NULL);
827 LASSERT(handle != NULL);
829 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
830 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
831 if (buf->lb_buf == NULL)
835 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
836 rec->cr.cr_type = (__u32)type;
837 rec->cr.cr_tfid = *mdo2fid(target);
838 rec->cr.cr_pfid = *mdo2fid(parent);
839 rec->cr.cr_namelen = tname->ln_namelen;
840 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
842 target->mod_cltime = cfs_time_current_64();
844 rc = mdd_changelog_store(env, mdd, rec, handle);
846 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
847 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
848 PFID(&rec->cr.cr_pfid));
856 /** Store a namespace change changelog record
857 * If this fails, we must fail the whole transaction; we don't
858 * want the change to commit without the log entry.
859 * \param target - mdd_object of change
860 * \param tpfid - target parent dir/object fid
861 * \param sfid - source object fid
862 * \param spfid - source parent fid
863 * \param tname - target name string
864 * \param sname - source name string
865 * \param handle - transacion handle
867 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
868 struct mdd_device *mdd,
869 enum changelog_rec_type type,
871 struct mdd_object *target,
872 const struct lu_fid *tpfid,
873 const struct lu_fid *sfid,
874 const struct lu_fid *spfid,
875 const struct lu_name *tname,
876 const struct lu_name *sname,
877 struct thandle *handle)
879 struct llog_changelog_ext_rec *rec;
886 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
888 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
891 LASSERT(sfid != NULL);
892 LASSERT(tpfid != NULL);
893 LASSERT(tname != NULL);
894 LASSERT(handle != NULL);
896 reclen = llog_data_len(sizeof(*rec) +
897 sname != NULL ? 1 + sname->ln_namelen : 0);
898 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
899 if (buf->lb_buf == NULL)
903 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
904 rec->cr.cr_type = (__u32)type;
905 rec->cr.cr_pfid = *tpfid;
906 rec->cr.cr_sfid = *sfid;
907 rec->cr.cr_spfid = *spfid;
908 rec->cr.cr_namelen = tname->ln_namelen;
909 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
911 rec->cr.cr_name[tname->ln_namelen] = '\0';
912 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
914 rec->cr.cr_namelen += 1 + sname->ln_namelen;
917 if (likely(target != NULL)) {
918 rec->cr.cr_tfid = *mdo2fid(target);
919 target->mod_cltime = cfs_time_current_64();
921 fid_zero(&rec->cr.cr_tfid);
924 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
926 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
927 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
934 static int __mdd_links_add(const struct lu_env *env,
935 struct mdd_object *mdd_obj,
936 struct linkea_data *ldata,
937 const struct lu_name *lname,
938 const struct lu_fid *pfid,
939 int first, int check)
943 if (ldata->ld_leh == NULL) {
944 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
948 rc = linkea_data_new(ldata,
949 &mdd_env_info(env)->mti_link_buf);
956 rc = linkea_links_find(ldata, lname, pfid);
957 if (rc && rc != -ENOENT)
963 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
964 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
968 linkea_add_buf(ldata, lname, tfid);
971 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
972 linkea_add_buf(ldata, lname, pfid);
974 return linkea_add_buf(ldata, lname, pfid);
977 static int __mdd_links_del(const struct lu_env *env,
978 struct mdd_object *mdd_obj,
979 struct linkea_data *ldata,
980 const struct lu_name *lname,
981 const struct lu_fid *pfid)
985 if (ldata->ld_leh == NULL) {
986 rc = mdd_links_read(env, mdd_obj, ldata);
991 rc = linkea_links_find(ldata, lname, pfid);
995 linkea_del_buf(ldata, lname);
999 static int mdd_linkea_prepare(const struct lu_env *env,
1000 struct mdd_object *mdd_obj,
1001 const struct lu_fid *oldpfid,
1002 const struct lu_name *oldlname,
1003 const struct lu_fid *newpfid,
1004 const struct lu_name *newlname,
1005 int first, int check,
1006 struct linkea_data *ldata)
1012 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1015 LASSERT(oldpfid != NULL || newpfid != NULL);
1017 if (mdd_obj->mod_flags & DEAD_OBJ) {
1018 /* Prevent linkea to be updated which is NOT necessary. */
1019 ldata->ld_reclen = 0;
1020 /* No more links, don't bother */
1024 if (oldpfid != NULL) {
1025 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1028 (rc != -ENODATA && rc != -ENOENT))
1030 /* No changes done. */
1035 /* If renaming, add the new record */
1036 if (newpfid != NULL) {
1037 /* even if the add fails, we still delete the out-of-date
1039 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1043 rc = rc != 0 ? rc : rc2;
1048 int mdd_links_rename(const struct lu_env *env,
1049 struct mdd_object *mdd_obj,
1050 const struct lu_fid *oldpfid,
1051 const struct lu_name *oldlname,
1052 const struct lu_fid *newpfid,
1053 const struct lu_name *newlname,
1054 struct thandle *handle,
1055 struct linkea_data *ldata,
1056 int first, int check)
1061 if (ldata == NULL) {
1062 ldata = &mdd_env_info(env)->mti_link_data;
1063 memset(ldata, 0, sizeof(*ldata));
1064 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1065 newpfid, newlname, first, check,
1071 if (ldata->ld_reclen != 0)
1072 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1077 if (rc == -EOVERFLOW || rc == -ENOSPC)
1079 if (oldpfid == NULL)
1080 CDEBUG(error ? D_ERROR : D_OTHER,
1081 "link_ea add '%.*s' failed %d "DFID"\n",
1082 newlname->ln_namelen, newlname->ln_name,
1083 rc, PFID(mdd_object_fid(mdd_obj)));
1084 else if (newpfid == NULL)
1085 CDEBUG(error ? D_ERROR : D_OTHER,
1086 "link_ea del '%.*s' failed %d "DFID"\n",
1087 oldlname->ln_namelen, oldlname->ln_name,
1088 rc, PFID(mdd_object_fid(mdd_obj)));
1090 CDEBUG(error ? D_ERROR : D_OTHER,
1091 "link_ea rename '%.*s'->'%.*s' failed %d "
1093 oldlname->ln_namelen, oldlname->ln_name,
1094 newlname->ln_namelen, newlname->ln_name,
1095 rc, PFID(mdd_object_fid(mdd_obj)));
1098 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1099 /* if we vmalloced a large buffer drop it */
1100 lu_buf_free(ldata->ld_buf);
1105 static inline int mdd_links_add(const struct lu_env *env,
1106 struct mdd_object *mdd_obj,
1107 const struct lu_fid *pfid,
1108 const struct lu_name *lname,
1109 struct thandle *handle,
1110 struct linkea_data *ldata, int first)
1112 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1113 pfid, lname, handle, ldata, first, 0);
1116 static inline int mdd_links_del(const struct lu_env *env,
1117 struct mdd_object *mdd_obj,
1118 const struct lu_fid *pfid,
1119 const struct lu_name *lname,
1120 struct thandle *handle)
1122 return mdd_links_rename(env, mdd_obj, pfid, lname,
1123 NULL, NULL, handle, NULL, 0, 0);
1126 /** Read the link EA into a temp buffer.
1127 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1128 * A pointer to the buffer is stored in \a ldata::ld_buf.
1130 * \retval 0 or error
1132 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1133 struct linkea_data *ldata)
1137 if (!mdd_object_exists(mdd_obj))
1140 /* First try a small buf */
1141 LASSERT(env != NULL);
1142 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1144 if (ldata->ld_buf->lb_buf == NULL)
1147 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1149 if (rc == -ERANGE) {
1150 /* Buf was too small, figure out what we need. */
1151 lu_buf_free(ldata->ld_buf);
1152 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1153 XATTR_NAME_LINK, BYPASS_CAPA);
1156 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1157 if (ldata->ld_buf->lb_buf == NULL)
1159 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1160 XATTR_NAME_LINK, BYPASS_CAPA);
1163 lu_buf_free(ldata->ld_buf);
1164 ldata->ld_buf = NULL;
1168 return linkea_init(ldata);
1171 /** Read the link EA into a temp buffer.
1172 * Uses the name_buf since it is generally large.
1173 * \retval IS_ERR err
1174 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1176 struct lu_buf *mdd_links_get(const struct lu_env *env,
1177 struct mdd_object *mdd_obj)
1179 struct linkea_data ldata = { 0 };
1182 rc = mdd_links_read(env, mdd_obj, &ldata);
1183 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1186 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1187 struct linkea_data *ldata, struct thandle *handle)
1189 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1190 ldata->ld_leh->leh_len);
1191 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1192 mdd_object_capa(env, mdd_obj));
1195 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1196 struct thandle *handle, struct linkea_data *ldata)
1202 if (ldata != NULL && ldata->ld_leh != NULL) {
1203 ea_len = ldata->ld_leh->leh_len;
1204 linkea = ldata->ld_buf->lb_buf;
1206 ea_len = DEFAULT_LINKEA_SIZE;
1210 /* XXX: max size? */
1211 rc = mdo_declare_xattr_set(env, mdd_obj,
1212 mdd_buf_get_const(env, linkea, ea_len),
1213 XATTR_NAME_LINK, 0, handle);
1217 static inline int mdd_declare_links_del(const struct lu_env *env,
1218 struct mdd_object *c,
1219 struct thandle *handle)
1223 /* For directory, the linkEA will be removed together
1224 * with the object. */
1225 if (!S_ISDIR(mdd_object_type(c)))
1226 rc = mdd_declare_links_add(env, c, handle, NULL);
1231 static int mdd_declare_link(const struct lu_env *env,
1232 struct mdd_device *mdd,
1233 struct mdd_object *p,
1234 struct mdd_object *c,
1235 const struct lu_name *name,
1236 struct thandle *handle,
1238 struct linkea_data *data)
1242 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1246 rc = mdo_declare_ref_add(env, c, handle);
1250 la->la_valid = LA_CTIME | LA_MTIME;
1251 rc = mdo_declare_attr_set(env, p, la, handle);
1255 la->la_valid = LA_CTIME;
1256 rc = mdo_declare_attr_set(env, c, la, handle);
1260 rc = mdd_declare_links_add(env, c, handle, data);
1264 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1269 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1270 struct md_object *src_obj, const struct lu_name *lname,
1273 const char *name = lname->ln_name;
1274 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1275 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1276 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1277 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1278 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1279 struct mdd_device *mdd = mdo2mdd(src_obj);
1280 struct thandle *handle;
1281 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1285 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1289 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1293 handle = mdd_trans_create(env, mdd);
1295 GOTO(out_pending, rc = PTR_ERR(handle));
1297 memset(ldata, 0, sizeof(*ldata));
1299 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1300 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1302 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1307 rc = mdd_trans_start(env, mdd, handle);
1311 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1312 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1315 GOTO(out_unlock, rc);
1317 rc = mdo_ref_add(env, mdd_sobj, handle);
1319 GOTO(out_unlock, rc);
1322 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1324 mdd_object_capa(env, mdd_tobj));
1326 mdo_ref_del(env, mdd_sobj, handle);
1327 GOTO(out_unlock, rc);
1330 la->la_valid = LA_CTIME | LA_MTIME;
1331 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1333 GOTO(out_unlock, rc);
1335 la->la_valid = LA_CTIME;
1336 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1338 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1339 mdo2fid(mdd_tobj), lname, 0, 0,
1342 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1343 lname, handle, ldata, 0);
1344 /* The failure of links_add should not cause the link
1345 * failure, reset rc here */
1350 mdd_write_unlock(env, mdd_sobj);
1352 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1353 mdd_tobj, lname, handle);
1355 mdd_trans_stop(env, mdd, rc, handle);
1357 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1358 /* if we vmalloced a large buffer drop it */
1359 lu_buf_free(ldata->ld_buf);
1364 static int mdd_mark_dead_object(const struct lu_env *env,
1365 struct mdd_object *obj, struct thandle *handle,
1368 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1372 obj->mod_flags |= DEAD_OBJ;
1374 if (!S_ISDIR(mdd_object_type(obj)))
1377 attr->la_valid = LA_FLAGS;
1378 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1381 rc = mdo_declare_attr_set(env, obj, attr, handle);
1383 rc = mdo_attr_set(env, obj, attr, handle,
1384 mdd_object_capa(env, obj));
1389 static int mdd_declare_finish_unlink(const struct lu_env *env,
1390 struct mdd_object *obj,
1391 struct thandle *handle)
1395 rc = mdd_mark_dead_object(env, obj, handle, true);
1399 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1403 rc = mdo_declare_destroy(env, obj, handle);
1407 return mdd_declare_links_del(env, obj, handle);
1410 /* caller should take a lock before calling */
1411 int mdd_finish_unlink(const struct lu_env *env,
1412 struct mdd_object *obj, struct md_attr *ma,
1413 const struct mdd_object *pobj,
1414 const struct lu_name *lname,
1418 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1421 LASSERT(mdd_write_locked(env, obj) != 0);
1423 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1424 rc = mdd_mark_dead_object(env, obj, th, false);
1428 /* add new orphan and the object
1429 * will be deleted during mdd_close() */
1430 if (obj->mod_count) {
1431 rc = __mdd_orphan_add(env, obj, th);
1433 CDEBUG(D_HA, "Object "DFID" is inserted into "
1434 "orphan list, open count = %d\n",
1435 PFID(mdd_object_fid(obj)),
1438 CERROR("Object "DFID" fail to be an orphan, "
1439 "open count = %d, maybe cause failed "
1441 PFID(mdd_object_fid(obj)),
1444 rc = mdo_destroy(env, obj, th);
1446 } else if (!is_dir) {
1447 /* old files may not have link ea; ignore errors */
1448 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1456 * has mdd_write_lock on cobj already, but not on pobj yet
1458 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1459 const struct lu_attr *pattr,
1460 struct mdd_object *cobj,
1461 const struct lu_attr *cattr)
1466 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1471 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1472 struct mdd_object *p, struct mdd_object *c,
1473 const struct lu_name *name, struct md_attr *ma,
1474 struct thandle *handle, int no_name)
1476 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1479 if (likely(no_name == 0)) {
1480 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1485 rc = mdo_declare_ref_del(env, p, handle);
1489 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1490 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1491 la->la_valid = LA_CTIME | LA_MTIME;
1492 rc = mdo_declare_attr_set(env, p, la, handle);
1497 rc = mdo_declare_ref_del(env, c, handle);
1501 rc = mdo_declare_ref_del(env, c, handle);
1505 la->la_valid = LA_CTIME;
1506 rc = mdo_declare_attr_set(env, c, la, handle);
1510 rc = mdd_declare_finish_unlink(env, c, handle);
1514 /* FIXME: need changelog for remove entry */
1515 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1522 * test if a file has an HSM archive
1523 * if HSM attributes are not found in ma update them from
1526 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1527 struct mdd_object *obj,
1532 if (!(ma->ma_valid & MA_HSM)) {
1533 /* no HSM MD provided, read xattr */
1534 struct lu_buf *hsm_buf;
1535 const size_t buflen = sizeof(struct hsm_attrs);
1538 hsm_buf = mdd_buf_get(env, NULL, 0);
1539 lu_buf_alloc(hsm_buf, buflen);
1540 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1541 mdd_object_capa(env, obj));
1542 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1543 lu_buf_free(hsm_buf);
1547 ma->ma_valid = MA_HSM;
1549 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1555 * Delete name entry and the object.
1556 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1557 * does not exist for this object, and it could only happen during resending
1558 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1559 * is also needed in this case(needed by changelog), so we have to add another
1560 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1561 * the ENOENT failure should be able to be fixed by redo mechanism.
1563 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1564 struct md_object *cobj, const struct lu_name *lname,
1565 struct md_attr *ma, int no_name)
1567 const char *name = lname->ln_name;
1568 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1569 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1570 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1571 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1572 struct mdd_object *mdd_cobj = NULL;
1573 struct mdd_device *mdd = mdo2mdd(pobj);
1574 struct thandle *handle;
1578 /* cobj == NULL means only delete name entry */
1579 if (likely(cobj != NULL)) {
1580 mdd_cobj = md2mdd_obj(cobj);
1581 if (mdd_object_exists(mdd_cobj) == 0)
1585 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1589 if (likely(mdd_cobj != NULL)) {
1591 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1595 is_dir = S_ISDIR(cattr->la_mode);
1598 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1602 handle = mdd_trans_create(env, mdd);
1604 RETURN(PTR_ERR(handle));
1606 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1607 lname, ma, handle, no_name);
1611 rc = mdd_trans_start(env, mdd, handle);
1615 if (likely(mdd_cobj != NULL))
1616 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1618 if (likely(no_name == 0)) {
1619 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1620 mdd_object_capa(env, mdd_pobj));
1625 if (likely(mdd_cobj != NULL)) {
1626 rc = mdo_ref_del(env, mdd_cobj, handle);
1628 __mdd_index_insert_only(env, mdd_pobj,
1631 mdd_object_capa(env, mdd_pobj));
1637 mdo_ref_del(env, mdd_cobj, handle);
1639 /* fetch updated nlink */
1640 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1645 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1646 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1648 la->la_valid = LA_CTIME | LA_MTIME;
1649 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1653 /* Enough for only unlink the entry */
1654 if (unlikely(mdd_cobj == NULL))
1657 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1658 /* update ctime of an unlinked file only if it is still
1659 * opened or a link still exists */
1660 la->la_valid = LA_CTIME;
1661 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1666 /* XXX: this transfer to ma will be removed with LOD/OSP */
1667 ma->ma_attr = *cattr;
1668 ma->ma_valid |= MA_INODE;
1669 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1671 /* fetch updated nlink */
1673 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1675 /* if object is removed then we can't get its attrs, use last get */
1676 if (cattr->la_nlink == 0) {
1677 ma->ma_attr = *cattr;
1678 ma->ma_valid |= MA_INODE;
1682 mdd_write_unlock(env, mdd_cobj);
1686 if (cattr->la_nlink == 0) {
1687 cl_flags |= CLF_UNLINK_LAST;
1688 /* search for an existing archive */
1689 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1690 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1693 rc = mdd_changelog_ns_store(env, mdd,
1694 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1695 mdd_cobj, mdd_pobj, lname, handle);
1699 mdd_trans_stop(env, mdd, rc, handle);
1705 * The permission has been checked when obj created, no need check again.
1707 static int mdd_cd_sanity_check(const struct lu_env *env,
1708 struct mdd_object *obj)
1713 if (!obj || mdd_is_dead_obj(obj))
1719 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1720 struct md_object *cobj, const struct md_op_spec *spec,
1723 struct mdd_device *mdd = mdo2mdd(cobj);
1724 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1725 struct mdd_object *son = md2mdd_obj(cobj);
1726 struct thandle *handle;
1727 const struct lu_buf *buf;
1728 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1729 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1733 rc = mdd_cd_sanity_check(env, son);
1737 if (!md_should_create(spec->sp_cr_flags))
1741 * there are following use cases for this function:
1742 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1743 * striping can be specified or not
1746 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1750 /* calling ->ah_make_hint() is used to transfer information from parent */
1751 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1753 handle = mdd_trans_create(env, mdd);
1755 GOTO(out_free, rc = PTR_ERR(handle));
1758 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1759 * Should this be fixed?
1761 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1762 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1763 spec->sp_cr_flags, spec->no_create);
1765 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1766 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1767 spec->u.sp_ea.eadatalen);
1772 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1773 XATTR_NAME_LOV, 0, handle);
1777 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1781 rc = mdd_trans_start(env, mdd, handle);
1785 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1786 0, handle, mdd_object_capa(env, son));
1791 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1794 mdd_trans_stop(env, mdd, rc, handle);
1799 static int mdd_declare_object_initialize(const struct lu_env *env,
1800 struct mdd_object *parent,
1801 struct mdd_object *child,
1802 struct lu_attr *attr,
1803 struct thandle *handle)
1809 * inode mode has been set in creation time, and it's based on umask,
1810 * la_mode and acl, don't set here again! (which will go wrong
1811 * because below function doesn't consider umask).
1812 * I'd suggest set all object attributes in creation time, see above.
1814 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1815 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1816 rc = mdo_declare_attr_set(env, child, attr, handle);
1817 attr->la_valid |= LA_MODE | LA_TYPE;
1818 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1819 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1822 rc = mdo_declare_ref_add(env, child, handle);
1824 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1831 static int mdd_object_initialize(const struct lu_env *env,
1832 const struct lu_fid *pfid,
1833 struct mdd_object *child,
1834 struct lu_attr *attr, struct thandle *handle,
1835 const struct md_op_spec *spec)
1841 * Update attributes for child.
1844 * (1) the valid bits should be converted between Lustre and Linux;
1845 * (2) maybe, the child attributes should be set in OSD when creation.
1848 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1849 /* arguments are supposed to stay the same */
1850 if (S_ISDIR(attr->la_mode)) {
1851 /* Add "." and ".." for newly created dir */
1852 mdo_ref_add(env, child, handle);
1853 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1854 dot, handle, BYPASS_CAPA);
1856 rc = __mdd_index_insert_only(env, child, pfid,
1860 mdo_ref_del(env, child, handle);
1867 * This function checks whether it can create a file/dir under the
1868 * directory(@pobj). The directory(@pobj) is not being locked by
1871 * \param[in] env execution environment
1872 * \param[in] pobj the directory to create files
1873 * \param[in] pattr the attributes of the directory
1874 * \param[in] lname the name of the created file/dir
1875 * \param[in] cattr the attributes of the file/dir
1876 * \param[in] spec create specification
1878 * \retval = 0 it is allowed to create file/dir under
1880 * \retval negative error not allowed to create file/dir
1881 * under the directory
1883 static int mdd_create_sanity_check(const struct lu_env *env,
1884 struct md_object *pobj,
1885 const struct lu_attr *pattr,
1886 const struct lu_name *lname,
1887 struct lu_attr *cattr,
1888 struct md_op_spec *spec)
1890 struct mdd_thread_info *info = mdd_env_info(env);
1891 struct lu_fid *fid = &info->mti_fid;
1892 struct mdd_object *obj = md2mdd_obj(pobj);
1893 struct mdd_device *m = mdo2mdd(pobj);
1894 bool check_perm = true;
1899 if (mdd_is_dead_obj(obj))
1903 * In some cases this lookup is not needed - we know before if name
1904 * exists or not because MDT performs lookup for it.
1905 * name length check is done in lookup.
1907 if (spec->sp_cr_lookup) {
1909 * Check if the name already exist, though it will be checked in
1910 * _index_insert also, for avoiding rolling back if exists
1913 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1914 MAY_WRITE | MAY_EXEC);
1916 RETURN(rc ? : -EEXIST);
1918 /* Permission is already being checked in mdd_lookup */
1922 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1927 if (pattr->la_mode & S_ISGID) {
1928 cattr->la_gid = pattr->la_gid;
1929 if (S_ISDIR(cattr->la_mode)) {
1930 cattr->la_mode |= S_ISGID;
1931 cattr->la_valid |= LA_MODE;
1935 rc = mdd_name_check(m, lname);
1939 switch (cattr->la_mode & S_IFMT) {
1941 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1943 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1944 RETURN(-ENAMETOOLONG);
1963 static int mdd_declare_object_create(const struct lu_env *env,
1964 struct mdd_device *mdd,
1965 struct mdd_object *p, struct mdd_object *c,
1966 struct lu_attr *attr,
1967 struct thandle *handle,
1968 const struct md_op_spec *spec,
1969 struct lu_buf *def_acl_buf,
1970 struct lu_buf *acl_buf,
1971 struct dt_allocation_hint *hint)
1975 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1980 #ifdef CONFIG_FS_POSIX_ACL
1981 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1982 /* if dir, then can inherit default ACl */
1983 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1984 XATTR_NAME_ACL_DEFAULT,
1990 if (acl_buf->lb_len > 0) {
1991 rc = mdo_declare_attr_set(env, c, attr, handle);
1995 rc = mdo_declare_xattr_set(env, c, acl_buf,
1996 XATTR_NAME_ACL_ACCESS, 0, handle);
2001 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2005 /* replay case, create LOV EA from client data */
2006 if (spec->no_create ||
2007 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2008 const struct lu_buf *buf;
2010 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2011 spec->u.sp_ea.eadatalen);
2012 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2018 if (S_ISLNK(attr->la_mode)) {
2019 const char *target_name = spec->u.sp_symname;
2020 int sym_len = strlen(target_name);
2021 const struct lu_buf *buf;
2023 buf = mdd_buf_get_const(env, target_name, sym_len);
2024 rc = dt_declare_record_write(env, mdd_object_child(c),
2033 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2034 struct mdd_object *p, struct mdd_object *c,
2035 const struct lu_name *name,
2036 struct lu_attr *attr,
2037 struct thandle *handle,
2038 const struct md_op_spec *spec,
2039 struct linkea_data *ldata,
2040 struct lu_buf *def_acl_buf,
2041 struct lu_buf *acl_buf,
2042 struct dt_allocation_hint *hint)
2046 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2047 def_acl_buf, acl_buf, hint);
2051 if (S_ISDIR(attr->la_mode)) {
2052 rc = mdo_declare_ref_add(env, p, handle);
2057 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2058 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2062 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2064 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,
2068 rc = mdd_declare_links_add(env, c, handle, ldata);
2073 la->la_valid = LA_CTIME | LA_MTIME;
2074 rc = mdo_declare_attr_set(env, p, la, handle);
2078 rc = mdd_declare_changelog_store(env, mdd, name, handle);
2083 /* XXX: For remote create, it should indicate the remote RPC
2084 * will be sent after local transaction is finished, which
2085 * is not very nice, but it will be removed once we fully support
2087 if (mdd_object_remote(p) && handle->th_update != NULL)
2088 handle->th_update->tu_sent_after_local_trans = 1;
2093 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2094 struct lu_attr *la, struct lu_buf *def_acl_buf,
2095 struct lu_buf *acl_buf)
2100 if (S_ISLNK(la->la_mode)) {
2101 acl_buf->lb_len = 0;
2102 def_acl_buf->lb_len = 0;
2106 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2107 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2108 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2109 mdd_read_unlock(env, pobj);
2111 /* If there are default ACL, fix mode/ACL by default ACL */
2112 def_acl_buf->lb_len = rc;
2113 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2114 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2115 acl_buf->lb_len = rc;
2116 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2119 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2120 /* If there are no default ACL, fix mode by mask */
2121 struct lu_ucred *uc = lu_ucred(env);
2123 /* The create triggered by MDT internal events, such as
2124 * LFSCK reset, will not contain valid "uc". */
2125 if (unlikely(uc != NULL))
2126 la->la_mode &= ~uc->uc_umask;
2128 acl_buf->lb_len = 0;
2129 def_acl_buf->lb_len = 0;
2136 * Create a metadata object and initialize it, set acl, xattr.
2138 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2139 struct mdd_object *son, struct lu_attr *attr,
2140 struct md_op_spec *spec, struct lu_buf *acl_buf,
2141 struct lu_buf *def_acl_buf,
2142 struct dt_allocation_hint *hint,
2143 struct thandle *handle)
2147 mdd_write_lock(env, son, MOR_TGT_CHILD);
2148 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2153 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2154 * created in declare phase, they also needs to be added to master
2155 * object as sub-directory entry. So it has to initialize the master
2156 * object, then set dir striped EA.(in mdo_xattr_set) */
2157 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2160 GOTO(err_destroy, rc);
2163 * in case of replay we just set LOVEA provided by the client
2164 * XXX: I think it would be interesting to try "old" way where
2165 * MDT calls this xattr_set(LOV) in a different transaction.
2166 * probably this way we code can be made better.
2169 /* During creation, there are only a few cases we need do xattr_set to
2171 * 1. regular file: see comments above.
2172 * 2. create striped directory with provided stripeEA.
2173 * 3. create striped directory because inherit default layout from the
2175 if (spec->no_create ||
2176 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2177 S_ISDIR(attr->la_mode)) {
2178 const struct lu_buf *buf;
2180 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2181 spec->u.sp_ea.eadatalen);
2182 rc = mdo_xattr_set(env, son, buf,
2183 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2185 handle, BYPASS_CAPA);
2187 GOTO(err_destroy, rc);
2190 #ifdef CONFIG_FS_POSIX_ACL
2191 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2192 S_ISDIR(attr->la_mode)) {
2193 /* set default acl */
2194 rc = mdo_xattr_set(env, son, def_acl_buf,
2195 XATTR_NAME_ACL_DEFAULT, 0,
2196 handle, BYPASS_CAPA);
2198 GOTO(err_destroy, rc);
2200 /* set its own acl */
2201 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2202 rc = mdo_xattr_set(env, son, acl_buf,
2203 XATTR_NAME_ACL_ACCESS,
2204 0, handle, BYPASS_CAPA);
2206 GOTO(err_destroy, rc);
2210 if (S_ISLNK(attr->la_mode)) {
2211 struct lu_ucred *uc = lu_ucred_assert(env);
2212 struct dt_object *dt = mdd_object_child(son);
2213 const char *target_name = spec->u.sp_symname;
2214 int sym_len = strlen(target_name);
2215 const struct lu_buf *buf;
2218 buf = mdd_buf_get_const(env, target_name, sym_len);
2219 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2220 mdd_object_capa(env, son),
2222 CFS_CAP_SYS_RESOURCE_MASK);
2227 GOTO(err_initlized, rc = -EFAULT);
2231 if (unlikely(rc != 0)) {
2233 if (S_ISDIR(attr->la_mode)) {
2234 /* Drop the reference, no need to delete "."/"..",
2235 * because the object to be destroied directly. */
2236 rc2 = mdo_ref_del(env, son, handle);
2240 rc2 = mdo_ref_del(env, son, handle);
2244 mdo_destroy(env, son, handle);
2247 mdd_write_unlock(env, son);
2252 * Create object and insert it into namespace.
2254 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2255 const struct lu_name *lname, struct md_object *child,
2256 struct md_op_spec *spec, struct md_attr* ma)
2258 struct mdd_thread_info *info = mdd_env_info(env);
2259 struct lu_attr *la = &info->mti_la_for_fix;
2260 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2261 struct mdd_object *son = md2mdd_obj(child);
2262 struct mdd_device *mdd = mdo2mdd(pobj);
2263 struct lu_attr *attr = &ma->ma_attr;
2264 struct thandle *handle;
2265 struct lu_attr *pattr = &info->mti_pattr;
2266 struct lu_buf acl_buf;
2267 struct lu_buf def_acl_buf;
2268 struct linkea_data *ldata = &info->mti_link_data;
2269 const char *name = lname->ln_name;
2270 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2276 * Two operations have to be performed:
2278 * - an allocation of a new object (->do_create()), and
2280 * - an insertion into a parent index (->dio_insert()).
2282 * Due to locking, operation order is not important, when both are
2283 * successful, *but* error handling cases are quite different:
2285 * - if insertion is done first, and following object creation fails,
2286 * insertion has to be rolled back, but this operation might fail
2287 * also leaving us with dangling index entry.
2289 * - if creation is done first, is has to be undone if insertion
2290 * fails, leaving us with leaked space, which is neither good, nor
2293 * It seems that creation-first is simplest solution, but it is
2294 * sub-optimal in the frequent
2299 * case, because second mkdir is bound to create object, only to
2300 * destroy it immediately.
2302 * To avoid this follow local file systems that do double lookup:
2304 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2306 * 1. create (mdd_object_create_internal())
2308 * 2. insert (__mdd_index_insert(), lookup again)
2311 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2315 /* Sanity checks before big job. */
2316 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2320 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2321 GOTO(out_free, rc = -EINPROGRESS);
2323 handle = mdd_trans_create(env, mdd);
2325 GOTO(out_free, rc = PTR_ERR(handle));
2327 acl_buf.lb_buf = info->mti_xattr_buf;
2328 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2329 def_acl_buf.lb_buf = info->mti_key;
2330 def_acl_buf.lb_len = sizeof(info->mti_key);
2331 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2335 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2337 memset(ldata, 0, sizeof(*ldata));
2338 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2339 mdd_object_fid(mdd_pobj),
2340 lname, 1, 0, ldata);
2342 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2343 handle, spec, ldata, &def_acl_buf, &acl_buf,
2348 rc = mdd_trans_start(env, mdd, handle);
2352 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2353 &def_acl_buf, hint, handle);
2357 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2358 mdd_write_lock(env, son, MOR_TGT_CHILD);
2359 rc = __mdd_orphan_add(env, son, handle);
2360 GOTO(out_volatile, rc);
2362 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2363 name, S_ISDIR(attr->la_mode), handle,
2364 mdd_object_capa(env, mdd_pobj));
2366 GOTO(err_created, rc);
2368 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2371 /* update parent directory mtime/ctime */
2373 la->la_valid = LA_CTIME | LA_MTIME;
2374 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2376 GOTO(err_insert, rc);
2384 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2385 rc2 = __mdd_orphan_del(env, son, handle);
2387 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2388 S_ISDIR(attr->la_mode),
2389 handle, BYPASS_CAPA);
2394 mdd_write_lock(env, son, MOR_TGT_CHILD);
2395 if (S_ISDIR(attr->la_mode)) {
2396 /* Drop the reference, no need to delete "."/"..",
2397 * because the object is to be destroyed directly. */
2398 rc2 = mdo_ref_del(env, son, handle);
2400 mdd_write_unlock(env, son);
2405 /* For volatile files drop one link immediately, since there is
2406 * no filename in the namespace, and save any error returned. */
2407 rc2 = mdo_ref_del(env, son, handle);
2409 mdd_write_unlock(env, son);
2410 if (unlikely(rc == 0))
2415 /* Don't destroy the volatile object on success */
2416 if (likely(rc != 0))
2417 mdo_destroy(env, son, handle);
2418 mdd_write_unlock(env, son);
2421 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2422 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2423 rc = mdd_changelog_ns_store(env, mdd,
2424 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2425 S_ISREG(attr->la_mode) ? CL_CREATE :
2426 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2427 0, son, mdd_pobj, lname, handle);
2429 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2433 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2434 /* if we vmalloced a large buffer drop it */
2435 lu_buf_free(ldata->ld_buf);
2437 /* The child object shouldn't be cached anymore */
2439 set_bit(LU_OBJECT_HEARD_BANSHEE,
2440 &child->mo_lu.lo_header->loh_flags);
2445 * Get locks on parents in proper order
2446 * RETURN: < 0 - error, rename_order if successful
2454 static int mdd_rename_order(const struct lu_env *env,
2455 struct mdd_device *mdd,
2456 struct mdd_object *src_pobj,
2457 const struct lu_attr *pattr,
2458 struct mdd_object *tgt_pobj)
2460 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2464 if (src_pobj == tgt_pobj)
2465 RETURN(MDD_RN_SAME);
2467 /* compared the parent child relationship of src_p&tgt_p */
2468 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2470 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2473 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2487 /* has not mdd_write{read}_lock on any obj yet. */
2488 static int mdd_rename_sanity_check(const struct lu_env *env,
2489 struct mdd_object *src_pobj,
2490 const struct lu_attr *pattr,
2491 struct mdd_object *tgt_pobj,
2492 const struct lu_attr *tpattr,
2493 struct mdd_object *sobj,
2494 const struct lu_attr *cattr,
2495 struct mdd_object *tobj,
2496 const struct lu_attr *tattr)
2501 /* XXX: when get here, sobj must NOT be NULL,
2502 * the other case has been processed in cld_rename
2503 * before mdd_rename and enable MDS_PERM_BYPASS. */
2506 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2510 /* XXX: when get here, "tobj == NULL" means tobj must
2511 * NOT exist (neither on remote MDS, such case has been
2512 * processed in cld_rename before mdd_rename and enable
2514 * So check may_create, but not check may_unlink. */
2516 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2517 (src_pobj != tgt_pobj));
2519 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2520 (src_pobj != tgt_pobj), 1);
2522 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2523 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2528 static int mdd_declare_rename(const struct lu_env *env,
2529 struct mdd_device *mdd,
2530 struct mdd_object *mdd_spobj,
2531 struct mdd_object *mdd_tpobj,
2532 struct mdd_object *mdd_sobj,
2533 struct mdd_object *mdd_tobj,
2534 const struct lu_name *tname,
2535 const struct lu_name *sname,
2537 struct linkea_data *ldata,
2538 struct thandle *handle)
2540 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2543 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2544 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2550 /* name from source dir */
2551 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2555 /* .. from source child */
2556 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2557 /* source child can be directory,
2558 * counted by source dir's nlink */
2559 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2562 if (mdd_spobj != mdd_tpobj) {
2563 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2568 rc = mdo_declare_index_insert(env, mdd_sobj,
2574 /* new target child can be directory,
2575 * counted by target dir's nlink */
2576 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2581 la->la_valid = LA_CTIME | LA_MTIME;
2582 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2586 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2590 la->la_valid = LA_CTIME;
2591 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2595 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2600 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2601 tname->ln_name, handle);
2605 /* name from target dir (old name), we declare it unconditionally
2606 * as mdd_rename() calls delete unconditionally as well. so just
2607 * to balance declarations vs calls to change ... */
2608 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2612 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2613 /* delete target child in target parent directory */
2614 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2618 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2619 /* target child can be directory,
2620 * delete "." reference in target child directory */
2621 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2625 /* delete ".." reference in target parent directory */
2626 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2631 la->la_valid = LA_CTIME;
2632 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2636 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2641 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2648 /* src object can be remote that is why we use only fid and type of object */
2649 static int mdd_rename(const struct lu_env *env,
2650 struct md_object *src_pobj, struct md_object *tgt_pobj,
2651 const struct lu_fid *lf, const struct lu_name *lsname,
2652 struct md_object *tobj, const struct lu_name *ltname,
2655 const char *sname = lsname->ln_name;
2656 const char *tname = ltname->ln_name;
2657 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2658 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2659 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2660 struct mdd_device *mdd = mdo2mdd(src_pobj);
2661 struct mdd_object *mdd_sobj = NULL; /* source object */
2662 struct mdd_object *mdd_tobj = NULL;
2663 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2664 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2665 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2666 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2667 struct thandle *handle;
2668 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2669 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2670 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2673 bool tobj_locked = 0;
2674 unsigned cl_flags = 0;
2679 mdd_tobj = md2mdd_obj(tobj);
2681 mdd_sobj = mdd_object_find(env, mdd, lf);
2683 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2685 GOTO(out_pending, rc);
2687 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2689 GOTO(out_pending, rc);
2692 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2694 GOTO(out_pending, rc);
2697 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2699 GOTO(out_pending, rc);
2701 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2702 mdd_sobj, cattr, mdd_tobj, tattr);
2704 GOTO(out_pending, rc);
2706 rc = mdd_name_check(mdd, ltname);
2708 GOTO(out_pending, rc);
2710 handle = mdd_trans_create(env, mdd);
2712 GOTO(out_pending, rc = PTR_ERR(handle));
2714 memset(ldata, 0, sizeof(*ldata));
2715 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2716 ltname, 1, 0, ldata);
2717 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2718 mdd_tobj, lsname, ltname, ma, ldata, handle);
2722 rc = mdd_trans_start(env, mdd, handle);
2726 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2727 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2729 GOTO(cleanup_unlocked, rc);
2731 is_dir = S_ISDIR(cattr->la_mode);
2733 /* Remove source name from source directory */
2734 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2735 mdd_object_capa(env, mdd_spobj));
2739 /* "mv dir1 dir2" needs "dir1/.." link update */
2740 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2741 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2742 mdd_object_capa(env, mdd_sobj));
2744 GOTO(fixup_spobj2, rc);
2746 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2747 handle, mdd_object_capa(env, mdd_sobj));
2749 GOTO(fixup_spobj, rc);
2752 /* Remove target name from target directory
2753 * Here tobj can be remote one, so we do index_delete unconditionally
2754 * and -ENOENT is allowed.
2756 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2757 mdd_object_capa(env, mdd_tpobj));
2760 /* tname might been renamed to something else */
2761 GOTO(fixup_spobj, rc);
2764 GOTO(fixup_spobj, rc);
2767 /* Insert new fid with target name into target dir */
2768 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2769 mdd_object_capa(env, mdd_tpobj));
2771 GOTO(fixup_tpobj, rc);
2773 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2774 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2776 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2778 la->la_valid = LA_CTIME;
2779 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2781 GOTO(fixup_tpobj, rc);
2784 /* Remove old target object
2785 * For tobj is remote case cmm layer has processed
2786 * and set tobj to NULL then. So when tobj is NOT NULL,
2787 * it must be local one.
2789 if (tobj && mdd_object_exists(mdd_tobj)) {
2790 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2792 if (mdd_is_dead_obj(mdd_tobj)) {
2793 /* shld not be dead, something is wrong */
2794 CERROR("tobj is dead, something is wrong\n");
2798 mdo_ref_del(env, mdd_tobj, handle);
2800 /* Remove dot reference. */
2801 if (S_ISDIR(tattr->la_mode))
2802 mdo_ref_del(env, mdd_tobj, handle);
2805 /* fetch updated nlink */
2806 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2808 CERROR("%s: Failed to get nlink for tobj "
2810 mdd2obd_dev(mdd)->obd_name,
2811 PFID(tpobj_fid), rc);
2812 GOTO(fixup_tpobj, rc);
2815 la->la_valid = LA_CTIME;
2816 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2818 CERROR("%s: Failed to set ctime for tobj "
2820 mdd2obd_dev(mdd)->obd_name,
2821 PFID(tpobj_fid), rc);
2822 GOTO(fixup_tpobj, rc);
2825 /* XXX: this transfer to ma will be removed with LOD/OSP */
2826 ma->ma_attr = *tattr;
2827 ma->ma_valid |= MA_INODE;
2828 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2831 CERROR("%s: Failed to unlink tobj "
2833 mdd2obd_dev(mdd)->obd_name,
2834 PFID(tpobj_fid), rc);
2835 GOTO(fixup_tpobj, rc);
2838 /* fetch updated nlink */
2839 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2841 CERROR("%s: Failed to get nlink for tobj "
2843 mdd2obd_dev(mdd)->obd_name,
2844 PFID(tpobj_fid), rc);
2845 GOTO(fixup_tpobj, rc);
2847 /* XXX: this transfer to ma will be removed with LOD/OSP */
2848 ma->ma_attr = *tattr;
2849 ma->ma_valid |= MA_INODE;
2851 if (tattr->la_nlink == 0) {
2852 cl_flags |= CLF_RENAME_LAST;
2853 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2854 cl_flags |= CLF_RENAME_LAST_EXISTS;
2858 la->la_valid = LA_CTIME | LA_MTIME;
2859 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2861 GOTO(fixup_tpobj, rc);
2863 if (mdd_spobj != mdd_tpobj) {
2864 la->la_valid = LA_CTIME | LA_MTIME;
2865 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2868 if (rc == 0 && mdd_sobj) {
2869 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2870 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2871 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2874 /* Old files might not have EA entry */
2875 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2876 lsname, handle, NULL, 0);
2877 mdd_write_unlock(env, mdd_sobj);
2878 /* We don't fail the transaction if the link ea can't be
2879 updated -- fid2path will use alternate lookup method. */
2887 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2890 CWARN("tp obj fix error %d\n",rc2);
2892 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2893 !mdd_is_dead_obj(mdd_tobj)) {
2895 mdo_ref_add(env, mdd_tobj, handle);
2897 mdo_ref_add(env, mdd_tobj, handle);
2900 rc2 = __mdd_index_insert(env, mdd_tpobj,
2901 mdo2fid(mdd_tobj), tname,
2906 CWARN("tp obj fix error %d\n",rc2);
2911 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2912 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2916 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2917 mdd2obd_dev(mdd)->obd_name, rc2);
2920 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2921 dotdot, handle, BYPASS_CAPA);
2923 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2924 mdd2obd_dev(mdd)->obd_name, rc2);
2929 rc2 = __mdd_index_insert(env, mdd_spobj,
2930 lf, sname, is_dir, handle, BYPASS_CAPA);
2932 CWARN("sp obj fix error %d\n",rc2);
2936 mdd_write_unlock(env, mdd_tobj);
2939 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2940 mdd_tobj, tpobj_fid, lf,
2941 spobj_fid, ltname, lsname,
2945 mdd_trans_stop(env, mdd, rc, handle);
2947 mdd_object_put(env, mdd_sobj);
2952 * During migration once the parent FID has been changed,
2953 * we need update the parent FID in linkea.
2955 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2956 struct mdd_object *parent,
2957 struct mdd_object *child,
2958 const char *name, int namelen,
2959 struct thandle *handle,
2962 struct mdd_thread_info *info = mdd_env_info(env);
2963 struct linkea_data ldata = {0};
2964 struct lu_buf *buf = &info->mti_link_buf;
2970 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2971 if (buf->lb_buf == NULL)
2975 rc = mdd_links_read(env, child, &ldata);
2977 if (rc == -ENOENT || rc == -ENODATA)
2982 LASSERT(ldata.ld_leh != NULL);
2983 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2984 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2985 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2986 struct lu_name lname;
2989 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2992 if (strncmp(lname.ln_name, name, namelen) != 0 ||
2993 lu_fid_eq(&fid, mdd_object_fid(parent))) {
2994 ldata.ld_lee = (struct link_ea_entry *)
2995 ((char *)ldata.ld_lee +
3000 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3001 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3002 lname.ln_namelen, lname.ln_name,
3003 PFID(mdd_object_fid(parent)));
3004 /* update to the new parent fid */
3005 linkea_entry_pack(ldata.ld_lee, &lname,
3006 mdd_object_fid(parent));
3008 rc = mdd_declare_links_add(env, child, handle, &ldata);
3010 rc = mdd_links_write(env, child, &ldata, handle);
3016 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3017 struct mdd_object *parent,
3018 struct mdd_object *child,
3019 const char *name, int namelen,
3020 struct thandle *handle)
3022 return mdd_linkea_update_child_internal(env, parent, child, name,
3023 namelen, handle, true);
3026 static int mdd_linkea_update_child(const struct lu_env *env,
3027 struct mdd_object *parent,
3028 struct mdd_object *child,
3029 const char *name, int namelen,
3030 struct thandle *handle)
3032 return mdd_linkea_update_child_internal(env, parent, child, name,
3033 namelen, handle, false);
3036 static int mdd_update_linkea_internal(const struct lu_env *env,
3037 struct mdd_object *mdd_pobj,
3038 struct mdd_object *mdd_sobj,
3039 struct mdd_object *mdd_tobj,
3040 const struct lu_name *child_name,
3041 struct thandle *handle,
3044 struct mdd_thread_info *info = mdd_env_info(env);
3045 struct linkea_data *ldata = &info->mti_link_data;
3050 rc = mdd_links_read(env, mdd_sobj, ldata);
3052 if (rc == -ENOENT || rc == -ENODATA)
3058 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3060 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3065 /* If it is mulitple links file, we need update the name entry for
3067 LASSERT(ldata->ld_leh != NULL);
3068 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3069 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3070 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3071 struct mdd_object *pobj;
3072 struct lu_name lname;
3075 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3077 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3079 pobj = mdd_object_find(env, mdd, &fid);
3081 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3082 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3087 if (!mdd_object_exists(pobj)) {
3088 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3089 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3093 if (pobj == mdd_pobj &&
3094 lname.ln_namelen == child_name->ln_namelen &&
3095 strncmp(lname.ln_name, child_name->ln_name,
3096 lname.ln_namelen) == 0) {
3097 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3098 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3103 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3104 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3105 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3108 /* Remove source name from source directory */
3109 /* Insert new fid with target name into target dir */
3110 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3115 rc = mdo_declare_index_insert(env, pobj,
3116 mdd_object_fid(mdd_tobj),
3117 lname.ln_name, handle);
3121 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3125 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3129 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3131 mdd_object_capa(env, pobj));
3135 rc = __mdd_index_insert(env, pobj,
3136 mdd_object_fid(mdd_tobj),
3137 lname.ln_name, 0, handle,
3138 mdd_object_capa(env, pobj));
3142 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3143 rc = mdo_ref_add(env, mdd_tobj, handle);
3144 mdd_write_unlock(env, mdd_tobj);
3148 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3149 mdo_ref_del(env, mdd_sobj, handle);
3150 mdd_write_unlock(env, mdd_sobj);
3153 mdd_object_put(env, pobj);
3161 static int mdd_migrate_xattrs(const struct lu_env *env,
3162 struct mdd_object *mdd_sobj,
3163 struct mdd_object *mdd_tobj)
3165 struct mdd_thread_info *info = mdd_env_info(env);
3166 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3168 struct thandle *handle;
3174 struct lu_buf list_xbuf;
3177 /* retrieve xattr list from the old object */
3178 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3179 mdd_object_capa(env, mdd_sobj));
3180 if (list_xsize == -ENODATA)
3186 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3187 if (info->mti_big_buf.lb_buf == NULL)
3190 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3191 list_xbuf.lb_len = list_xsize;
3192 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3193 mdd_object_capa(env, mdd_sobj));
3198 xname = list_xbuf.lb_buf;
3200 xlen = strnlen(xname, rem - 1) + 1;
3201 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3202 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3203 strcmp(XATTR_NAME_LMV, xname) == 0)
3206 /* For directory, if there are default layout, migrate here */
3207 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3208 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3211 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3213 mdd_object_capa(env, mdd_sobj));
3214 if (xsize == -ENODATA)
3219 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3220 if (info->mti_link_buf.lb_buf == NULL)
3221 GOTO(out, rc = -ENOMEM);
3223 xbuf.lb_len = xsize;
3224 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3225 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3226 mdd_object_capa(env, mdd_sobj));
3232 handle = mdd_trans_create(env, mdd);
3234 GOTO(out, rc = PTR_ERR(handle));
3236 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3239 GOTO(stop_trans, rc);
3240 /* Note: this transaction is part of migration, and it is not
3241 * the last step of migration, so we set th_local = 1 to avoid
3242 * update last rcvd for this transaction */
3243 handle->th_local = 1;
3244 rc = mdd_trans_start(env, mdd, handle);
3246 GOTO(stop_trans, rc);
3248 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3249 mdd_object_capa(env, mdd_sobj));
3251 GOTO(stop_trans, rc = 0);
3254 GOTO(stop_trans, rc);
3256 mdd_trans_stop(env, mdd, rc, handle);
3261 memmove(xname, xname + xlen, rem);
3267 static int mdd_declare_migrate_create(const struct lu_env *env,
3268 struct mdd_object *mdd_pobj,
3269 struct mdd_object *mdd_sobj,
3270 struct mdd_object *mdd_tobj,
3271 struct md_op_spec *spec,
3273 union lmv_mds_md *mgr_ea,
3274 struct thandle *handle)
3276 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3277 const struct lu_buf *buf;
3281 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3282 handle, spec, NULL);
3286 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3291 if (S_ISLNK(la->la_mode)) {
3292 const char *target_name = spec->u.sp_symname;
3293 int sym_len = strlen(target_name);
3294 const struct lu_buf *buf;
3296 buf = mdd_buf_get_const(env, target_name, sym_len);
3297 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3303 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3304 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3305 spec->u.sp_ea.eadatalen);
3306 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3312 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3313 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3314 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3319 la_flag->la_valid = LA_FLAGS;
3320 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3321 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3322 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3327 static int mdd_migrate_create(const struct lu_env *env,
3328 struct mdd_object *mdd_pobj,
3329 struct mdd_object *mdd_sobj,
3330 struct mdd_object *mdd_tobj,
3333 struct mdd_thread_info *info = mdd_env_info(env);
3334 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3335 struct md_op_spec *spec = &info->mti_spec;
3336 struct lu_buf lmm_buf = { 0 };
3337 struct lu_buf link_buf = { 0 };
3338 const struct lu_buf *buf;
3339 struct thandle *handle;
3340 struct lmv_mds_md_v1 *mgr_ea;
3341 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3342 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3347 /* prepare spec for create */
3348 memset(spec, 0, sizeof(*spec));
3349 spec->sp_cr_lookup = 0;
3350 spec->sp_feat = &dt_directory_features;
3351 if (S_ISLNK(la->la_mode)) {
3352 buf = lu_buf_check_and_alloc(
3353 &mdd_env_info(env)->mti_big_buf,
3356 link_buf.lb_len = la->la_size + 1;
3357 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3359 rc = rc != 0 ? rc : -EFAULT;
3360 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3361 mdd2obd_dev(mdd)->obd_name,
3362 PFID(mdd_object_fid(mdd_sobj)), rc);
3365 spec->u.sp_symname = link_buf.lb_buf;
3366 } else if S_ISREG(la->la_mode) {
3367 /* retrieve lov of the old object */
3368 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3369 if (rc != 0 && rc != -ENODATA)
3371 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3372 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3373 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3374 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3378 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3379 memset(mgr_ea, 0, sizeof(mgr_ea));
3380 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3381 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3382 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3383 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3384 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3385 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3387 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3389 handle = mdd_trans_create(env, mdd);
3391 GOTO(out_free, rc = PTR_ERR(handle));
3393 /* Note: this transaction is part of migration, and it is not
3394 * the last step of migration, so we set th_local = 1 to avoid
3395 * update last rcvd for this transaction */
3396 handle->th_local = 1;
3397 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3399 (union lmv_mds_md *)info->mti_xattr_buf,
3402 GOTO(stop_trans, rc);
3404 rc = mdd_trans_start(env, mdd, handle);
3406 GOTO(stop_trans, rc);
3408 /* create the target object */
3409 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3412 GOTO(stop_trans, rc);
3414 /* Set MIGRATE EA on the source inode, so once the migration needs
3415 * to be re-done during failover, the re-do process can locate the
3416 * target object which is already being created. */
3417 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3418 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3419 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3420 handle, mdd_object_capa(env, mdd_sobj));
3422 GOTO(stop_trans, rc);
3424 /* Set immutable flag, so any modification is disabled until
3425 * the migration is done. Once the migration is interrupted,
3426 * if the resume process find the migrating object has both
3427 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3428 * flag and approve the migration */
3429 la_flag->la_valid = LA_FLAGS;
3430 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3431 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3432 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3433 mdd_object_capa(env, mdd_sobj));
3436 mdd_trans_stop(env, mdd, rc, handle);
3438 if (lmm_buf.lb_buf != NULL)
3439 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3443 static int mdd_migrate_entries(const struct lu_env *env,
3444 struct mdd_object *mdd_sobj,
3445 struct mdd_object *mdd_tobj)
3447 struct dt_object *next = mdd_object_child(mdd_sobj);
3448 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3449 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3450 struct thandle *handle;
3452 const struct dt_it_ops *iops;
3455 struct lu_dirent *ent;
3458 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3462 if (!dt_try_as_dir(env, next))
3463 GOTO(out_ent, rc = -ENOTDIR);
3465 * iterate directories
3467 iops = &next->do_index_ops->dio_it;
3468 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3469 mdd_object_capa(env, mdd_sobj));
3471 GOTO(out_ent, rc = PTR_ERR(it));
3473 rc = iops->load(env, it, 0);
3475 rc = iops->next(env, it);
3479 * At this point and across for-loop:
3481 * rc == 0 -> ok, proceed.
3482 * rc > 0 -> end of directory.
3486 struct mdd_object *child;
3487 char *name = mdd_env_info(env)->mti_key;
3491 bool target_exist = false;
3493 len = iops->key_size(env, it);
3497 result = iops->rec(env, it, (struct dt_rec *)ent,
3498 LUDA_FID | LUDA_TYPE);
3499 if (result == -ESTALE)
3506 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3507 recsize = le16_to_cpu(ent->lde_reclen);
3509 /* Insert new fid with target name into target dir */
3510 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3511 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3512 ent->lde_name[1] == '.'))
3515 child = mdd_object_find(env, mdd, &ent->lde_fid);
3517 GOTO(out, rc = PTR_ERR(child));
3519 is_dir = S_ISDIR(lu_object_attr(&child->mod_obj.mo_lu));
3521 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3523 /* Check whether the name has been inserted to the target */
3524 if (dt_try_as_dir(env, dt_tobj)) {
3525 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3527 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3528 (struct dt_rec *)fid,
3529 (struct dt_key *)name,
3530 mdd_object_capa(env, mdd_tobj));
3531 if (unlikely(rc == 0))
3532 target_exist = true;
3535 handle = mdd_trans_create(env, mdd);
3537 GOTO(out, rc = PTR_ERR(handle));
3539 /* Note: this transaction is part of migration, and it is not
3540 * the last step of migration, so we set th_local = 1 to avoid
3541 * updating last rcvd for this transaction */
3542 handle->th_local = 1;
3543 if (likely(!target_exist)) {
3544 rc = mdo_declare_index_insert(env, mdd_tobj,
3551 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3557 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3562 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3566 /* Update .. for child */
3567 rc = mdo_declare_index_delete(env, child, dotdot,
3572 rc = mdo_declare_index_insert(env, child,
3573 mdd_object_fid(mdd_tobj),
3579 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3586 rc = mdd_trans_start(env, mdd, handle);
3588 CERROR("%s: transaction start failed: rc = %d\n",
3589 mdd2obd_dev(mdd)->obd_name, rc);
3593 if (likely(!target_exist)) {
3594 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3595 name, is_dir, handle,
3596 mdd_object_capa(env, mdd_tobj));
3601 rc = mdo_ref_add(env, mdd_tobj, handle);
3608 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3609 mdd_object_capa(env, mdd_sobj));
3614 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3615 mdd_object_capa(env, child));
3619 rc = __mdd_index_insert_only(env, child,
3620 mdd_object_fid(mdd_tobj),
3622 mdd_object_capa(env, child));
3627 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3628 strlen(name), handle);
3631 mdd_object_put(env, child);
3632 mdd_trans_stop(env, mdd, rc, handle);
3636 result = iops->next(env, it);
3637 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3638 GOTO(out, rc = -EINTR);
3640 if (result == -ESTALE)
3642 } while (result == 0);
3645 iops->fini(env, it);
3647 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3651 static int mdd_declare_update_linkea(const struct lu_env *env,
3652 struct mdd_object *mdd_pobj,
3653 struct mdd_object *mdd_sobj,
3654 struct mdd_object *mdd_tobj,
3655 struct thandle *handle,
3656 const struct lu_name *child_name)
3658 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3659 child_name, handle, 1);
3662 static int mdd_update_linkea(const struct lu_env *env,
3663 struct mdd_object *mdd_pobj,
3664 struct mdd_object *mdd_sobj,
3665 struct mdd_object *mdd_tobj,
3666 struct thandle *handle,
3667 const struct lu_name *child_name)
3669 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3670 child_name, handle, 0);
3673 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3674 struct mdd_object *mdd_pobj,
3675 struct mdd_object *mdd_sobj,
3676 struct mdd_object *mdd_tobj,
3677 const struct lu_name *lname,
3679 struct lu_attr *parent_la,
3680 struct thandle *handle)
3682 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3685 /* Revert IMMUTABLE flag */
3686 la_flag->la_valid = LA_FLAGS;
3687 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3688 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3689 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3693 /* delete entry from source dir */
3694 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3698 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3699 mdd_tobj, handle, lname);
3703 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3704 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3710 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3711 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3717 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3718 lname->ln_name, handle);
3722 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3723 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3728 /* delete old object */
3729 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3733 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3734 /* delete old object */
3735 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3738 /* set nlink to 0 */
3739 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3744 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3748 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3753 static int mdd_migrate_update_name(const struct lu_env *env,
3754 struct mdd_object *mdd_pobj,
3755 struct mdd_object *mdd_sobj,
3756 struct mdd_object *mdd_tobj,
3757 const struct lu_name *lname,
3760 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3761 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3762 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3763 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3764 struct thandle *handle;
3765 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3766 const char *name = lname->ln_name;
3770 /* update time for parent */
3771 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3772 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3773 p_la->la_valid = LA_CTIME;
3775 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3779 handle = mdd_trans_create(env, mdd);
3781 RETURN(PTR_ERR(handle));
3783 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3784 lname, so_attr, p_la, handle);
3786 /* If the migration can not be fit in one transaction, just
3787 * leave it in the original MDT */
3789 GOTO(stop_trans, rc = 0);
3791 GOTO(stop_trans, rc);
3794 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3795 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3796 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3797 PFID(mdd_object_fid(mdd_tobj)));
3799 rc = mdd_trans_start(env, mdd, handle);
3801 GOTO(stop_trans, rc);
3803 /* Revert IMMUTABLE flag */
3804 la_flag->la_valid = LA_FLAGS;
3805 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3806 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3807 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3808 mdd_object_capa(env, mdd_pobj));
3810 GOTO(stop_trans, rc);
3812 /* Remove source name from source directory */
3813 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3814 mdd_object_capa(env, mdd_pobj));
3816 GOTO(stop_trans, rc);
3818 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3821 GOTO(stop_trans, rc);
3823 if (S_ISREG(so_attr->la_mode)) {
3824 if (so_attr->la_nlink == 1) {
3825 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3827 mdd_object_capa(env, mdd_sobj));
3828 if (rc != 0 && rc != -ENODATA)
3829 GOTO(stop_trans, rc);
3833 /* Insert new fid with target name into target dir */
3834 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj), name,
3835 is_dir, handle, mdd_object_capa(env, mdd_pobj));
3837 GOTO(stop_trans, rc);
3839 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3842 GOTO(stop_trans, rc);
3844 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3845 mdo_ref_del(env, mdd_sobj, handle);
3847 mdo_ref_del(env, mdd_sobj, handle);
3849 /* Get the attr again after ref_del */
3850 rc = mdd_la_get(env, mdd_sobj, so_attr,
3851 mdd_object_capa(env, mdd_sobj));
3853 GOTO(stop_trans, rc);
3854 ma->ma_attr = *so_attr;
3855 ma->ma_valid |= MA_INODE;
3856 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3858 GOTO(stop_trans, rc);
3860 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3862 GOTO(stop_trans, rc);
3864 mdd_write_unlock(env, mdd_sobj);
3867 mdd_trans_stop(env, mdd, rc, handle);
3873 * Check whether we should migrate the file/dir
3875 * < 0 permission check failed or other error.
3876 * = 0 the file can be migrated.
3877 * > 0 the file does not need to be migrated, mostly
3878 * for multiple link file
3880 static int mdd_migrate_sanity_check(const struct lu_env *env,
3881 struct mdd_object *pobj,
3882 const struct lu_attr *pattr,
3883 struct mdd_object *sobj,
3884 struct lu_attr *sattr)
3886 struct mdd_thread_info *info = mdd_env_info(env);
3887 struct linkea_data *ldata = &info->mti_link_data;
3889 struct lu_buf *mgr_buf;
3895 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3896 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3897 if (mgr_buf->lb_buf == NULL)
3900 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3901 mdd_object_capa(env, sobj));
3903 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3905 /* If the object has migrateEA, it means IMMUTE flag
3906 * is being set by previous migration process, so it
3907 * needs to override the IMMUTE flag, otherwise the
3908 * following sanity check will fail */
3909 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3910 LMV_HASH_FLAG_MIGRATION) {
3911 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3913 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3914 sobj->mod_flags &= ~IMMUTE_OBJ;
3915 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3916 mdd2obd_dev(mdd)->obd_name,
3917 PFID(mdd_object_fid(sobj)));
3921 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3922 sobj, sattr, NULL, NULL);
3926 /* Then it will check if the file should be migrated. If the file
3927 * has mulitple links, we only need migrate the file if all of its
3928 * entries has been migrated to the remote MDT */
3929 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3932 rc = mdd_links_read(env, sobj, ldata);
3934 /* For multiple links files, if there are no linkEA data at all,
3935 * means the file might be created before linkEA is enabled, and
3936 * all all of its links should not be migrated yet, otherwise
3937 * it should have some linkEA there */
3938 if (rc == -ENOENT || rc == -ENODATA)
3943 /* If it is mulitple links file, we need update the name entry for
3945 LASSERT(ldata->ld_leh != NULL);
3946 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3947 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3948 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3949 struct mdd_object *lpobj;
3950 struct lu_name lname;
3953 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3955 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3957 lpobj = mdd_object_find(env, mdd, &fid);
3958 if (IS_ERR(lpobj)) {
3959 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3960 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3965 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3966 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3967 PFID(&fid), lname.ln_namelen, lname.ln_name);
3968 mdd_object_put(env, lpobj);
3972 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3973 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3974 lname.ln_name, PFID(&fid));
3975 mdd_object_put(env, lpobj);
3983 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3984 struct md_object *sobj, const struct lu_name *lname,
3985 struct md_object *tobj, struct md_attr *ma)
3987 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3988 struct mdd_device *mdd = mdo2mdd(pobj);
3989 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
3990 struct mdd_object *mdd_tobj = NULL;
3991 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3992 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3996 /* If the file will being migrated, it will check whether
3997 * the file is being opened by someone else right now */
3998 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3999 if (mdd_sobj->mod_count >= 1) {
4000 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4001 mdd2obd_dev(mdd)->obd_name,
4002 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4003 mdd_sobj->mod_count, -EBUSY);
4004 mdd_read_unlock(env, mdd_sobj);
4005 GOTO(put, rc = -EBUSY);
4007 mdd_read_unlock(env, mdd_sobj);
4009 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
4013 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
4017 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4024 /* Sigh, it is impossible to finish all of migration in a single
4025 * transaction, for example migrating big directory entries to the
4026 * new MDT, it needs insert all of name entries of children in the
4029 * So migration will be done in multiple steps and transactions.
4031 * 1. create an orphan object on the remote MDT in one transaction.
4032 * 2. migrate extend attributes to the new target file/directory.
4033 * 3. For directory, migrate the entries to the new MDT and update
4034 * linkEA of each children. Because we can not migrate all entries
4035 * in a single transaction, so the migrating directory will become
4036 * a striped directory during migration, so once the process is
4037 * interrupted, the directory is still accessible. (During lookup,
4038 * client will locate the name by searching both original and target
4040 * 4. Finally, update the name/FID to point to the new file/directory
4041 * in a separate transaction.
4044 /* step 1: Check whether the orphan object has been created, and create
4045 * orphan object on the remote MDT if needed */
4046 mdd_tobj = md2mdd_obj(tobj);
4047 if (!mdd_object_exists(mdd_tobj)) {
4048 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4054 /* step 2: migrate xattr */
4055 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4059 /* step 3: migrate name entries to the orphan object */
4060 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4061 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4064 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4065 OBD_FAIL_MDS_REINT_NET_REP)))
4069 /* step 4: update name entry to the new object */
4070 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4078 const struct md_dir_operations mdd_dir_ops = {
4079 .mdo_is_subdir = mdd_is_subdir,
4080 .mdo_lookup = mdd_lookup,
4081 .mdo_create = mdd_create,
4082 .mdo_rename = mdd_rename,
4083 .mdo_link = mdd_link,
4084 .mdo_unlink = mdd_unlink,
4085 .mdo_create_data = mdd_create_data,
4086 .mdo_migrate = mdd_migrate,