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)))
102 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key,
103 mdd_object_capa(env, mdd_obj));
110 int mdd_lookup(const struct lu_env *env,
111 struct md_object *pobj, const struct lu_name *lname,
112 struct lu_fid *fid, struct md_op_spec *spec)
114 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
118 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
122 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
126 static inline int mdd_parent_fid(const struct lu_env *env,
127 struct mdd_object *obj,
128 const struct lu_attr *attr,
131 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
135 * For root fid use special function, which does not compare version component
136 * of fid. Version component is different for root fids on all MDTs.
138 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
140 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
141 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
145 * return 1: if lf is the fid of the ancestor of p1;
148 * return -EREMOTE: if remote object is found, in this
149 * case fid of remote object is saved to @pf;
151 * otherwise: values < 0, errors.
153 static int mdd_is_parent(const struct lu_env *env,
154 struct mdd_device *mdd,
155 struct mdd_object *p1,
156 const struct lu_attr *attr,
157 const struct lu_fid *lf,
160 struct mdd_object *parent = NULL;
165 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
166 pfid = &mdd_env_info(env)->mti_fid;
168 /* Check for root first. */
169 if (mdd_is_root(mdd, mdo2fid(p1)))
173 /* this is done recursively, bypass capa for each obj */
174 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
175 rc = mdd_parent_fid(env, p1, attr, pfid);
178 if (mdd_is_root(mdd, pfid))
180 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
182 if (lu_fid_eq(pfid, lf))
185 mdd_object_put(env, parent);
187 parent = mdd_object_find(env, mdd, pfid);
188 if (IS_ERR(parent)) {
189 GOTO(out, rc = PTR_ERR(parent));
190 } else if (mdd_object_remote(parent)) {
191 /*FIXME: Because of the restriction of rename in Phase I.
192 * If the parent is remote, we just assumed lf is not the
193 * parent of P1 for now */
200 if (parent && !IS_ERR(parent))
201 mdd_object_put(env, parent);
206 * No permission check is needed.
208 * returns 1: if fid is ancestor of @mo;
209 * returns 0: if fid is not a ancestor of @mo;
211 * returns EREMOTE if remote object is found, fid of remote object is saved to
214 * returns < 0: if error
216 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
217 const struct lu_fid *fid, struct lu_fid *sfid)
219 struct mdd_device *mdd = mdo2mdd(mo);
220 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
224 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
227 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
231 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
235 } else if (rc == 1) {
236 /* found @fid is parent */
244 * Check that @dir contains no entries except (possibly) dot and dotdot.
249 * -ENOTDIR not a directory object
250 * -ENOTEMPTY not empty
254 static int mdd_dir_is_empty(const struct lu_env *env,
255 struct mdd_object *dir)
258 struct dt_object *obj;
259 const struct dt_it_ops *iops;
263 obj = mdd_object_child(dir);
264 if (!dt_try_as_dir(env, obj))
267 iops = &obj->do_index_ops->dio_it;
268 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
270 result = iops->get(env, it, (const struct dt_key *)"");
273 for (result = 0, i = 0; result == 0 && i < 3; ++i)
274 result = iops->next(env, it);
277 else if (result == 1)
279 } else if (result == 0)
281 * Huh? Index contains no zero key?
288 result = PTR_ERR(it);
293 * Determine if the target object can be hard linked, and right now it only
294 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
295 * directory nlink count might exceed the maximum link count(see
296 * osd_object_ref_add), so it only check nlink for non-directories.
298 * \param[in] env thread environment
299 * \param[in] obj object being linked to
300 * \param[in] la attributes of \a obj
302 * \retval 0 if \a obj can be hard linked
303 * \retval negative error if \a obj is a directory or has too
306 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
307 const struct lu_attr *la)
309 struct mdd_device *m = mdd_obj2mdd_dev(obj);
314 /* Subdir count limitation can be broken through
315 * (see osd_object_ref_add), so only check non-directory here. */
316 if (!S_ISDIR(la->la_mode) &&
317 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
324 * Check whether it may create the cobj under the pobj.
326 * \param[in] env execution environment
327 * \param[in] pobj the parent directory
328 * \param[in] pattr the attribute of the parent directory
329 * \param[in] cobj the child to be created
330 * \param[in] check_perm if check WRITE|EXEC permission for parent
332 * \retval = 0 create the child under this dir is allowed
333 * \retval negative errno create the child under this dir is
336 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
337 const struct lu_attr *pattr, struct mdd_object *cobj,
340 struct mdd_thread_info *info = mdd_env_info(env);
345 if (cobj && mdd_object_exists(cobj))
348 if (mdd_is_dead_obj(pobj))
351 /* If the parent is a sub-stripe, check whether it is dead */
352 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
353 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV,
354 mdd_object_capa(env, pobj));
355 if (unlikely(rc > 0)) {
356 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
358 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
359 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
365 rc = mdd_permission_internal_locked(env, pobj, pattr,
366 MAY_WRITE | MAY_EXEC,
372 * Check whether can unlink from the pobj in the case of "cobj == NULL".
374 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
375 const struct lu_attr *pattr, const struct lu_attr *attr)
380 if (mdd_is_dead_obj(pobj))
383 if ((attr->la_valid & LA_FLAGS) &&
384 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
387 rc = mdd_permission_internal_locked(env, pobj, pattr,
388 MAY_WRITE | MAY_EXEC,
393 if (mdd_is_append(pobj))
400 * pobj == NULL is remote ops case, under such case, pobj's
401 * VTX feature has been checked already, no need check again.
403 static inline int mdd_is_sticky(const struct lu_env *env,
404 struct mdd_object *pobj,
405 const struct lu_attr *pattr,
406 struct mdd_object *cobj,
407 const struct lu_attr *cattr)
409 struct lu_ucred *uc = lu_ucred_assert(env);
412 LASSERT(pattr != NULL);
413 if (!(pattr->la_mode & S_ISVTX) ||
414 (pattr->la_uid == uc->uc_fsuid))
418 LASSERT(cattr != NULL);
419 if (cattr->la_uid == uc->uc_fsuid)
422 return !md_capable(uc, CFS_CAP_FOWNER);
425 static int mdd_may_delete_entry(const struct lu_env *env,
426 struct mdd_object *pobj,
427 const struct lu_attr *pattr,
432 LASSERT(pobj != NULL);
433 if (!mdd_object_exists(pobj))
436 if (mdd_is_dead_obj(pobj))
441 rc = mdd_permission_internal_locked(env, pobj, pattr,
442 MAY_WRITE | MAY_EXEC,
448 if (mdd_is_append(pobj))
455 * Check whether it may delete the cobj from the pobj.
458 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
459 const struct lu_attr *tpattr, struct mdd_object *tobj,
460 const struct lu_attr *tattr, const struct lu_attr *cattr,
461 int check_perm, int check_empty)
467 LASSERT(tpattr != NULL);
468 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
476 if (!mdd_object_exists(tobj))
479 if (mdd_is_dead_obj(tobj))
482 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
485 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
488 if ((tattr->la_valid & LA_FLAGS) &&
489 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
492 /* additional check the rename case */
494 if (S_ISDIR(cattr->la_mode)) {
495 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
497 if (!S_ISDIR(tattr->la_mode))
500 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
502 } else if (S_ISDIR(tattr->la_mode))
506 if (S_ISDIR(tattr->la_mode) && check_empty)
507 rc = mdd_dir_is_empty(env, tobj);
513 * Check whether it can create the link file(linked to @src_obj) under
514 * the target directory(@tgt_obj), and src_obj has been locked by
517 * \param[in] env execution environment
518 * \param[in] tgt_obj the target directory
519 * \param[in] tattr attributes of target directory
520 * \param[in] lname the link name
521 * \param[in] src_obj source object for link
522 * \param[in] cattr attributes for source object
524 * \retval = 0 it is allowed to create the link file under tgt_obj
525 * \retval negative error not allowed to create the link file
527 static int mdd_link_sanity_check(const struct lu_env *env,
528 struct mdd_object *tgt_obj,
529 const struct lu_attr *tattr,
530 const struct lu_name *lname,
531 struct mdd_object *src_obj,
532 const struct lu_attr *cattr)
534 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
538 if (!mdd_object_exists(src_obj))
541 if (mdd_is_dead_obj(src_obj))
544 /* Local ops, no lookup before link, check filename length here. */
545 rc = mdd_name_check(m, lname);
549 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
552 if (S_ISDIR(mdd_object_type(src_obj)))
555 LASSERT(src_obj != tgt_obj);
556 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
560 rc = __mdd_may_link(env, src_obj, cattr);
565 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
566 const char *name, struct thandle *handle,
567 struct lustre_capa *capa)
569 struct dt_object *next = mdd_object_child(pobj);
573 if (dt_try_as_dir(env, next))
574 rc = dt_delete(env, next, (struct dt_key *)name, handle, capa);
581 static int __mdd_index_insert_only(const struct lu_env *env,
582 struct mdd_object *pobj,
583 const struct lu_fid *lf, __u32 type,
585 struct thandle *handle,
586 struct lustre_capa *capa)
588 struct dt_object *next = mdd_object_child(pobj);
592 if (dt_try_as_dir(env, next)) {
593 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
594 struct lu_ucred *uc = lu_ucred_check(env);
598 rec->rec_type = type;
599 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
600 rc = dt_insert(env, next, (const struct dt_rec *)rec,
601 (const struct dt_key *)name, handle, capa,
609 /* insert named index, add reference if isdir */
610 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
611 const struct lu_fid *lf, __u32 type,
612 const char *name, struct thandle *handle,
613 struct lustre_capa *capa)
618 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle, capa);
619 if (rc == 0 && S_ISDIR(type)) {
620 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
621 mdo_ref_add(env, pobj, handle);
622 mdd_write_unlock(env, pobj);
628 /* delete named index, drop reference if isdir */
629 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
630 const char *name, int is_dir, struct thandle *handle,
631 struct lustre_capa *capa)
636 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
637 if (rc == 0 && is_dir) {
638 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
639 mdo_ref_del(env, pobj, handle);
640 mdd_write_unlock(env, pobj);
646 int mdd_declare_changelog_store(const struct lu_env *env,
647 struct mdd_device *mdd,
648 const struct lu_name *fname,
649 struct thandle *handle)
651 struct obd_device *obd = mdd2obd_dev(mdd);
652 struct llog_ctxt *ctxt;
653 struct llog_changelog_rec *rec;
659 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
662 reclen = llog_data_len(sizeof(*rec) +
663 (fname != NULL ? fname->ln_namelen : 0));
664 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
665 if (buf->lb_buf == NULL)
669 rec->cr_hdr.lrh_len = reclen;
670 rec->cr_hdr.lrh_type = CHANGELOG_REC;
672 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
676 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
682 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
683 struct mdd_device *mdd,
684 const struct lu_name *tname,
685 const struct lu_name *sname,
686 struct thandle *handle)
688 struct obd_device *obd = mdd2obd_dev(mdd);
689 struct llog_ctxt *ctxt;
690 struct llog_changelog_ext_rec *rec;
696 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
699 reclen = llog_data_len(sizeof(*rec) +
700 (tname != NULL ? tname->ln_namelen : 0) +
701 (sname != NULL ? 1 + sname->ln_namelen : 0));
702 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
703 if (buf->lb_buf == NULL)
707 rec->cr_hdr.lrh_len = reclen;
708 rec->cr_hdr.lrh_type = CHANGELOG_REC;
710 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
714 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
720 /** Add a changelog entry \a rec to the changelog llog
723 * \param handle - currently ignored since llogs start their own transaction;
724 * this will hopefully be fixed in llog rewrite
727 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
728 struct llog_changelog_rec *rec, struct thandle *th)
730 struct obd_device *obd = mdd2obd_dev(mdd);
731 struct llog_ctxt *ctxt;
734 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
735 rec->cr_hdr.lrh_type = CHANGELOG_REC;
736 rec->cr.cr_time = cl_time();
738 spin_lock(&mdd->mdd_cl.mc_lock);
739 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
740 * but as long as the MDD transactions are ordered correctly for e.g.
741 * rename conflicts, I don't think this should matter. */
742 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
743 spin_unlock(&mdd->mdd_cl.mc_lock);
745 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
749 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
756 /** Add a changelog_ext entry \a rec to the changelog llog
759 * \param handle - currently ignored since llogs start their own transaction;
760 * this will hopefully be fixed in llog rewrite
764 mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
765 struct llog_changelog_ext_rec *rec, struct thandle *th)
767 struct obd_device *obd = mdd2obd_dev(mdd);
768 struct llog_ctxt *ctxt;
771 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
772 /* llog_lvfs_write_rec sets the llog tail len */
773 rec->cr_hdr.lrh_type = CHANGELOG_REC;
774 rec->cr.cr_time = cl_time();
776 spin_lock(&mdd->mdd_cl.mc_lock);
777 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
778 * but as long as the MDD transactions are ordered correctly for e.g.
779 * rename conflicts, I don't think this should matter. */
780 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
781 spin_unlock(&mdd->mdd_cl.mc_lock);
783 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
787 /* nested journal transaction */
788 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
796 /** Store a namespace change changelog record
797 * If this fails, we must fail the whole transaction; we don't
798 * want the change to commit without the log entry.
799 * \param target - mdd_object of change
800 * \param parent - parent dir/object
801 * \param tname - target name string
802 * \param handle - transacion handle
804 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
805 enum changelog_rec_type type, unsigned flags,
806 struct mdd_object *target, struct mdd_object *parent,
807 const struct lu_name *tname, struct thandle *handle)
809 struct llog_changelog_rec *rec;
816 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
818 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
821 LASSERT(target != NULL);
822 LASSERT(parent != NULL);
823 LASSERT(tname != NULL);
824 LASSERT(handle != NULL);
826 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
827 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
828 if (buf->lb_buf == NULL)
832 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
833 rec->cr.cr_type = (__u32)type;
834 rec->cr.cr_tfid = *mdo2fid(target);
835 rec->cr.cr_pfid = *mdo2fid(parent);
836 rec->cr.cr_namelen = tname->ln_namelen;
837 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
839 target->mod_cltime = cfs_time_current_64();
841 rc = mdd_changelog_store(env, mdd, rec, handle);
843 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
844 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
845 PFID(&rec->cr.cr_pfid));
853 /** Store a namespace change changelog record
854 * If this fails, we must fail the whole transaction; we don't
855 * want the change to commit without the log entry.
856 * \param target - mdd_object of change
857 * \param tpfid - target parent dir/object fid
858 * \param sfid - source object fid
859 * \param spfid - source parent fid
860 * \param tname - target name string
861 * \param sname - source name string
862 * \param handle - transacion handle
864 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
865 struct mdd_device *mdd,
866 enum changelog_rec_type type,
868 struct mdd_object *target,
869 const struct lu_fid *tpfid,
870 const struct lu_fid *sfid,
871 const struct lu_fid *spfid,
872 const struct lu_name *tname,
873 const struct lu_name *sname,
874 struct thandle *handle)
876 struct llog_changelog_ext_rec *rec;
883 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
885 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
888 LASSERT(sfid != NULL);
889 LASSERT(tpfid != NULL);
890 LASSERT(tname != NULL);
891 LASSERT(handle != NULL);
893 reclen = llog_data_len(sizeof(*rec) +
894 sname != NULL ? 1 + sname->ln_namelen : 0);
895 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
896 if (buf->lb_buf == NULL)
900 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
901 rec->cr.cr_type = (__u32)type;
902 rec->cr.cr_pfid = *tpfid;
903 rec->cr.cr_sfid = *sfid;
904 rec->cr.cr_spfid = *spfid;
905 rec->cr.cr_namelen = tname->ln_namelen;
906 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
908 rec->cr.cr_name[tname->ln_namelen] = '\0';
909 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
911 rec->cr.cr_namelen += 1 + sname->ln_namelen;
914 if (likely(target != NULL)) {
915 rec->cr.cr_tfid = *mdo2fid(target);
916 target->mod_cltime = cfs_time_current_64();
918 fid_zero(&rec->cr.cr_tfid);
921 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
923 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
924 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
931 static int __mdd_links_add(const struct lu_env *env,
932 struct mdd_object *mdd_obj,
933 struct linkea_data *ldata,
934 const struct lu_name *lname,
935 const struct lu_fid *pfid,
936 int first, int check)
940 if (ldata->ld_leh == NULL) {
941 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
945 rc = linkea_data_new(ldata,
946 &mdd_env_info(env)->mti_link_buf);
953 rc = linkea_links_find(ldata, lname, pfid);
954 if (rc && rc != -ENOENT)
960 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
961 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
965 linkea_add_buf(ldata, lname, tfid);
968 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
969 linkea_add_buf(ldata, lname, pfid);
971 return linkea_add_buf(ldata, lname, pfid);
974 static int __mdd_links_del(const struct lu_env *env,
975 struct mdd_object *mdd_obj,
976 struct linkea_data *ldata,
977 const struct lu_name *lname,
978 const struct lu_fid *pfid)
982 if (ldata->ld_leh == NULL) {
983 rc = mdd_links_read(env, mdd_obj, ldata);
988 rc = linkea_links_find(ldata, lname, pfid);
992 linkea_del_buf(ldata, lname);
996 static int mdd_linkea_prepare(const struct lu_env *env,
997 struct mdd_object *mdd_obj,
998 const struct lu_fid *oldpfid,
999 const struct lu_name *oldlname,
1000 const struct lu_fid *newpfid,
1001 const struct lu_name *newlname,
1002 int first, int check,
1003 struct linkea_data *ldata)
1009 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1012 LASSERT(oldpfid != NULL || newpfid != NULL);
1014 if (mdd_obj->mod_flags & DEAD_OBJ) {
1015 /* Prevent linkea to be updated which is NOT necessary. */
1016 ldata->ld_reclen = 0;
1017 /* No more links, don't bother */
1021 if (oldpfid != NULL) {
1022 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1025 (rc != -ENODATA && rc != -ENOENT))
1027 /* No changes done. */
1032 /* If renaming, add the new record */
1033 if (newpfid != NULL) {
1034 /* even if the add fails, we still delete the out-of-date
1036 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1040 rc = rc != 0 ? rc : rc2;
1045 int mdd_links_rename(const struct lu_env *env,
1046 struct mdd_object *mdd_obj,
1047 const struct lu_fid *oldpfid,
1048 const struct lu_name *oldlname,
1049 const struct lu_fid *newpfid,
1050 const struct lu_name *newlname,
1051 struct thandle *handle,
1052 struct linkea_data *ldata,
1053 int first, int check)
1058 if (ldata == NULL) {
1059 ldata = &mdd_env_info(env)->mti_link_data;
1060 memset(ldata, 0, sizeof(*ldata));
1061 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1062 newpfid, newlname, first, check,
1068 if (ldata->ld_reclen != 0)
1069 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1074 if (rc == -EOVERFLOW || rc == -ENOSPC)
1076 if (oldpfid == NULL)
1077 CDEBUG(error ? D_ERROR : D_OTHER,
1078 "link_ea add '%.*s' failed %d "DFID"\n",
1079 newlname->ln_namelen, newlname->ln_name,
1080 rc, PFID(mdd_object_fid(mdd_obj)));
1081 else if (newpfid == NULL)
1082 CDEBUG(error ? D_ERROR : D_OTHER,
1083 "link_ea del '%.*s' failed %d "DFID"\n",
1084 oldlname->ln_namelen, oldlname->ln_name,
1085 rc, PFID(mdd_object_fid(mdd_obj)));
1087 CDEBUG(error ? D_ERROR : D_OTHER,
1088 "link_ea rename '%.*s'->'%.*s' failed %d "
1090 oldlname->ln_namelen, oldlname->ln_name,
1091 newlname->ln_namelen, newlname->ln_name,
1092 rc, PFID(mdd_object_fid(mdd_obj)));
1095 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1096 /* if we vmalloced a large buffer drop it */
1097 lu_buf_free(ldata->ld_buf);
1102 static inline int mdd_links_add(const struct lu_env *env,
1103 struct mdd_object *mdd_obj,
1104 const struct lu_fid *pfid,
1105 const struct lu_name *lname,
1106 struct thandle *handle,
1107 struct linkea_data *ldata, int first)
1109 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1110 pfid, lname, handle, ldata, first, 0);
1113 static inline int mdd_links_del(const struct lu_env *env,
1114 struct mdd_object *mdd_obj,
1115 const struct lu_fid *pfid,
1116 const struct lu_name *lname,
1117 struct thandle *handle)
1119 return mdd_links_rename(env, mdd_obj, pfid, lname,
1120 NULL, NULL, handle, NULL, 0, 0);
1123 /** Read the link EA into a temp buffer.
1124 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1125 * A pointer to the buffer is stored in \a ldata::ld_buf.
1127 * \retval 0 or error
1129 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1130 struct linkea_data *ldata)
1134 if (!mdd_object_exists(mdd_obj))
1137 /* First try a small buf */
1138 LASSERT(env != NULL);
1139 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1141 if (ldata->ld_buf->lb_buf == NULL)
1144 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1146 if (rc == -ERANGE) {
1147 /* Buf was too small, figure out what we need. */
1148 lu_buf_free(ldata->ld_buf);
1149 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1150 XATTR_NAME_LINK, BYPASS_CAPA);
1153 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1154 if (ldata->ld_buf->lb_buf == NULL)
1156 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1157 XATTR_NAME_LINK, BYPASS_CAPA);
1160 lu_buf_free(ldata->ld_buf);
1161 ldata->ld_buf = NULL;
1165 return linkea_init(ldata);
1168 /** Read the link EA into a temp buffer.
1169 * Uses the name_buf since it is generally large.
1170 * \retval IS_ERR err
1171 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1173 struct lu_buf *mdd_links_get(const struct lu_env *env,
1174 struct mdd_object *mdd_obj)
1176 struct linkea_data ldata = { 0 };
1179 rc = mdd_links_read(env, mdd_obj, &ldata);
1180 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1183 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1184 struct linkea_data *ldata, struct thandle *handle)
1186 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1187 ldata->ld_leh->leh_len);
1189 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1192 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1193 mdd_object_capa(env, mdd_obj));
1196 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1197 struct thandle *handle, struct linkea_data *ldata)
1203 if (ldata != NULL && ldata->ld_leh != NULL) {
1204 ea_len = ldata->ld_leh->leh_len;
1205 linkea = ldata->ld_buf->lb_buf;
1207 ea_len = DEFAULT_LINKEA_SIZE;
1211 /* XXX: max size? */
1212 rc = mdo_declare_xattr_set(env, mdd_obj,
1213 mdd_buf_get_const(env, linkea, ea_len),
1214 XATTR_NAME_LINK, 0, handle);
1218 static inline int mdd_declare_links_del(const struct lu_env *env,
1219 struct mdd_object *c,
1220 struct thandle *handle)
1224 /* For directory, the linkEA will be removed together
1225 * with the object. */
1226 if (!S_ISDIR(mdd_object_type(c)))
1227 rc = mdd_declare_links_add(env, c, handle, NULL);
1232 static int mdd_declare_link(const struct lu_env *env,
1233 struct mdd_device *mdd,
1234 struct mdd_object *p,
1235 struct mdd_object *c,
1236 const struct lu_name *name,
1237 struct thandle *handle,
1239 struct linkea_data *data)
1243 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1244 name->ln_name, handle);
1248 rc = mdo_declare_ref_add(env, c, handle);
1252 la->la_valid = LA_CTIME | LA_MTIME;
1253 rc = mdo_declare_attr_set(env, p, la, handle);
1257 la->la_valid = LA_CTIME;
1258 rc = mdo_declare_attr_set(env, c, la, handle);
1262 rc = mdd_declare_links_add(env, c, handle, data);
1266 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1271 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1272 struct md_object *src_obj, const struct lu_name *lname,
1275 const char *name = lname->ln_name;
1276 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1277 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1278 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1279 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1280 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1281 struct mdd_device *mdd = mdo2mdd(src_obj);
1282 struct thandle *handle;
1283 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1287 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1291 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1295 handle = mdd_trans_create(env, mdd);
1297 GOTO(out_pending, rc = PTR_ERR(handle));
1299 memset(ldata, 0, sizeof(*ldata));
1301 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1302 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1304 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1309 rc = mdd_trans_start(env, mdd, handle);
1313 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1314 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1317 GOTO(out_unlock, rc);
1319 rc = mdo_ref_add(env, mdd_sobj, handle);
1321 GOTO(out_unlock, rc);
1324 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1325 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1328 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1329 mdd_object_type(mdd_sobj),
1331 mdd_object_capa(env, mdd_tobj));
1333 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1334 mdd_object_type(mdd_sobj),
1336 mdd_object_capa(env, mdd_tobj));
1340 mdo_ref_del(env, mdd_sobj, handle);
1341 GOTO(out_unlock, rc);
1344 la->la_valid = LA_CTIME | LA_MTIME;
1345 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1347 GOTO(out_unlock, rc);
1349 la->la_valid = LA_CTIME;
1350 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1352 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1353 mdo2fid(mdd_tobj), lname, 0, 0,
1356 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1357 lname, handle, ldata, 0);
1358 /* The failure of links_add should not cause the link
1359 * failure, reset rc here */
1364 mdd_write_unlock(env, mdd_sobj);
1366 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1367 mdd_tobj, lname, handle);
1369 mdd_trans_stop(env, mdd, rc, handle);
1371 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1372 /* if we vmalloced a large buffer drop it */
1373 lu_buf_free(ldata->ld_buf);
1378 static int mdd_mark_dead_object(const struct lu_env *env,
1379 struct mdd_object *obj, struct thandle *handle,
1382 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1386 obj->mod_flags |= DEAD_OBJ;
1388 if (!S_ISDIR(mdd_object_type(obj)))
1391 attr->la_valid = LA_FLAGS;
1392 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1395 rc = mdo_declare_attr_set(env, obj, attr, handle);
1397 rc = mdo_attr_set(env, obj, attr, handle,
1398 mdd_object_capa(env, obj));
1403 static int mdd_declare_finish_unlink(const struct lu_env *env,
1404 struct mdd_object *obj,
1405 struct thandle *handle)
1409 rc = mdd_mark_dead_object(env, obj, handle, true);
1413 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1417 rc = mdo_declare_destroy(env, obj, handle);
1421 return mdd_declare_links_del(env, obj, handle);
1424 /* caller should take a lock before calling */
1425 int mdd_finish_unlink(const struct lu_env *env,
1426 struct mdd_object *obj, struct md_attr *ma,
1427 const struct mdd_object *pobj,
1428 const struct lu_name *lname,
1432 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1435 LASSERT(mdd_write_locked(env, obj) != 0);
1437 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1438 rc = mdd_mark_dead_object(env, obj, th, false);
1442 /* add new orphan and the object
1443 * will be deleted during mdd_close() */
1444 if (obj->mod_count) {
1445 rc = __mdd_orphan_add(env, obj, th);
1447 CDEBUG(D_HA, "Object "DFID" is inserted into "
1448 "orphan list, open count = %d\n",
1449 PFID(mdd_object_fid(obj)),
1452 CERROR("Object "DFID" fail to be an orphan, "
1453 "open count = %d, maybe cause failed "
1455 PFID(mdd_object_fid(obj)),
1458 rc = mdo_destroy(env, obj, th);
1460 } else if (!is_dir) {
1461 /* old files may not have link ea; ignore errors */
1462 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1470 * has mdd_write_lock on cobj already, but not on pobj yet
1472 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1473 const struct lu_attr *pattr,
1474 struct mdd_object *cobj,
1475 const struct lu_attr *cattr)
1480 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1485 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1486 struct mdd_object *p, struct mdd_object *c,
1487 const struct lu_name *name, struct md_attr *ma,
1488 struct thandle *handle, int no_name, int is_dir)
1490 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1493 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1494 if (likely(no_name == 0)) {
1495 rc = mdo_declare_index_delete(env, p, name->ln_name,
1502 rc = mdo_declare_ref_del(env, p, handle);
1508 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1509 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1510 la->la_valid = LA_CTIME | LA_MTIME;
1511 rc = mdo_declare_attr_set(env, p, la, handle);
1516 rc = mdo_declare_ref_del(env, c, handle);
1520 rc = mdo_declare_ref_del(env, c, handle);
1524 la->la_valid = LA_CTIME;
1525 rc = mdo_declare_attr_set(env, c, la, handle);
1529 rc = mdd_declare_finish_unlink(env, c, handle);
1533 /* FIXME: need changelog for remove entry */
1534 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1541 * test if a file has an HSM archive
1542 * if HSM attributes are not found in ma update them from
1545 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1546 struct mdd_object *obj,
1551 if (!(ma->ma_valid & MA_HSM)) {
1552 /* no HSM MD provided, read xattr */
1553 struct lu_buf *hsm_buf;
1554 const size_t buflen = sizeof(struct hsm_attrs);
1557 hsm_buf = mdd_buf_get(env, NULL, 0);
1558 lu_buf_alloc(hsm_buf, buflen);
1559 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1560 mdd_object_capa(env, obj));
1561 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1562 lu_buf_free(hsm_buf);
1566 ma->ma_valid = MA_HSM;
1568 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1574 * Delete name entry and the object.
1575 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1576 * does not exist for this object, and it could only happen during resending
1577 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1578 * is also needed in this case(needed by changelog), so we have to add another
1579 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1580 * the ENOENT failure should be able to be fixed by redo mechanism.
1582 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1583 struct md_object *cobj, const struct lu_name *lname,
1584 struct md_attr *ma, int no_name)
1586 const char *name = lname->ln_name;
1587 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1588 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1589 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1590 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1591 struct mdd_object *mdd_cobj = NULL;
1592 struct mdd_device *mdd = mdo2mdd(pobj);
1593 struct thandle *handle;
1597 /* cobj == NULL means only delete name entry */
1598 if (likely(cobj != NULL)) {
1599 mdd_cobj = md2mdd_obj(cobj);
1600 if (mdd_object_exists(mdd_cobj) == 0)
1604 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1608 if (likely(mdd_cobj != NULL)) {
1610 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1614 is_dir = S_ISDIR(cattr->la_mode);
1617 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1621 handle = mdd_trans_create(env, mdd);
1623 RETURN(PTR_ERR(handle));
1625 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1626 lname, ma, handle, no_name, is_dir);
1630 rc = mdd_trans_start(env, mdd, handle);
1634 if (likely(mdd_cobj != NULL))
1635 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1637 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1638 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1639 mdd_object_capa(env, mdd_pobj));
1644 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1645 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1646 GOTO(cleanup, rc = 0);
1648 if (likely(mdd_cobj != NULL)) {
1649 rc = mdo_ref_del(env, mdd_cobj, handle);
1651 __mdd_index_insert_only(env, mdd_pobj,
1653 mdd_object_type(mdd_cobj),
1655 mdd_object_capa(env, mdd_pobj));
1661 mdo_ref_del(env, mdd_cobj, handle);
1663 /* fetch updated nlink */
1664 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1669 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1670 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1672 la->la_valid = LA_CTIME | LA_MTIME;
1673 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1677 /* Enough for only unlink the entry */
1678 if (unlikely(mdd_cobj == NULL))
1681 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1682 /* update ctime of an unlinked file only if it is still
1683 * opened or a link still exists */
1684 la->la_valid = LA_CTIME;
1685 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1690 /* XXX: this transfer to ma will be removed with LOD/OSP */
1691 ma->ma_attr = *cattr;
1692 ma->ma_valid |= MA_INODE;
1693 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1695 /* fetch updated nlink */
1697 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1699 /* if object is removed then we can't get its attrs, use last get */
1700 if (cattr->la_nlink == 0) {
1701 ma->ma_attr = *cattr;
1702 ma->ma_valid |= MA_INODE;
1706 if (likely(mdd_cobj != NULL))
1707 mdd_write_unlock(env, mdd_cobj);
1712 if (cattr->la_nlink == 0) {
1713 cl_flags |= CLF_UNLINK_LAST;
1714 /* search for an existing archive */
1715 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1716 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1719 rc = mdd_changelog_ns_store(env, mdd,
1720 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1721 mdd_cobj, mdd_pobj, lname, handle);
1725 mdd_trans_stop(env, mdd, rc, handle);
1731 * The permission has been checked when obj created, no need check again.
1733 static int mdd_cd_sanity_check(const struct lu_env *env,
1734 struct mdd_object *obj)
1739 if (!obj || mdd_is_dead_obj(obj))
1745 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1746 struct md_object *cobj, const struct md_op_spec *spec,
1749 struct mdd_device *mdd = mdo2mdd(cobj);
1750 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1751 struct mdd_object *son = md2mdd_obj(cobj);
1752 struct thandle *handle;
1753 const struct lu_buf *buf;
1754 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1755 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1759 rc = mdd_cd_sanity_check(env, son);
1763 if (!md_should_create(spec->sp_cr_flags))
1767 * there are following use cases for this function:
1768 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1769 * striping can be specified or not
1772 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1776 /* calling ->ah_make_hint() is used to transfer information from parent */
1777 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1779 handle = mdd_trans_create(env, mdd);
1781 GOTO(out_free, rc = PTR_ERR(handle));
1784 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1785 * Should this be fixed?
1787 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1788 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1789 spec->sp_cr_flags, spec->no_create);
1791 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1792 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1793 spec->u.sp_ea.eadatalen);
1798 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1799 XATTR_NAME_LOV, 0, handle);
1803 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1807 rc = mdd_trans_start(env, mdd, handle);
1811 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1812 0, handle, mdd_object_capa(env, son));
1817 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1820 mdd_trans_stop(env, mdd, rc, handle);
1825 static int mdd_declare_object_initialize(const struct lu_env *env,
1826 struct mdd_object *parent,
1827 struct mdd_object *child,
1828 struct lu_attr *attr,
1829 struct thandle *handle)
1835 * inode mode has been set in creation time, and it's based on umask,
1836 * la_mode and acl, don't set here again! (which will go wrong
1837 * because below function doesn't consider umask).
1838 * I'd suggest set all object attributes in creation time, see above.
1840 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1841 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1842 rc = mdo_declare_attr_set(env, child, attr, handle);
1843 attr->la_valid |= LA_MODE | LA_TYPE;
1844 if (rc != 0 || !S_ISDIR(attr->la_mode))
1847 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1852 rc = mdo_declare_ref_add(env, child, handle);
1856 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1862 static int mdd_object_initialize(const struct lu_env *env,
1863 const struct lu_fid *pfid,
1864 struct mdd_object *child,
1865 struct lu_attr *attr, struct thandle *handle,
1866 const struct md_op_spec *spec)
1872 * Update attributes for child.
1875 * (1) the valid bits should be converted between Lustre and Linux;
1876 * (2) maybe, the child attributes should be set in OSD when creation.
1879 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1880 /* arguments are supposed to stay the same */
1881 if (S_ISDIR(attr->la_mode)) {
1882 /* Add "." and ".." for newly created dir */
1883 mdo_ref_add(env, child, handle);
1884 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1885 S_IFDIR, dot, handle, BYPASS_CAPA);
1887 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1891 mdo_ref_del(env, child, handle);
1898 * This function checks whether it can create a file/dir under the
1899 * directory(@pobj). The directory(@pobj) is not being locked by
1902 * \param[in] env execution environment
1903 * \param[in] pobj the directory to create files
1904 * \param[in] pattr the attributes of the directory
1905 * \param[in] lname the name of the created file/dir
1906 * \param[in] cattr the attributes of the file/dir
1907 * \param[in] spec create specification
1909 * \retval = 0 it is allowed to create file/dir under
1911 * \retval negative error not allowed to create file/dir
1912 * under the directory
1914 static int mdd_create_sanity_check(const struct lu_env *env,
1915 struct md_object *pobj,
1916 const struct lu_attr *pattr,
1917 const struct lu_name *lname,
1918 struct lu_attr *cattr,
1919 struct md_op_spec *spec)
1921 struct mdd_thread_info *info = mdd_env_info(env);
1922 struct lu_fid *fid = &info->mti_fid;
1923 struct mdd_object *obj = md2mdd_obj(pobj);
1924 struct mdd_device *m = mdo2mdd(pobj);
1925 bool check_perm = true;
1930 if (mdd_is_dead_obj(obj))
1934 * In some cases this lookup is not needed - we know before if name
1935 * exists or not because MDT performs lookup for it.
1936 * name length check is done in lookup.
1938 if (spec->sp_cr_lookup) {
1940 * Check if the name already exist, though it will be checked in
1941 * _index_insert also, for avoiding rolling back if exists
1944 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1945 MAY_WRITE | MAY_EXEC);
1947 RETURN(rc ? : -EEXIST);
1949 /* Permission is already being checked in mdd_lookup */
1953 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1958 if (pattr->la_mode & S_ISGID) {
1959 cattr->la_gid = pattr->la_gid;
1960 if (S_ISDIR(cattr->la_mode)) {
1961 cattr->la_mode |= S_ISGID;
1962 cattr->la_valid |= LA_MODE;
1966 rc = mdd_name_check(m, lname);
1970 switch (cattr->la_mode & S_IFMT) {
1972 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1974 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1975 RETURN(-ENAMETOOLONG);
1994 static int mdd_declare_object_create(const struct lu_env *env,
1995 struct mdd_device *mdd,
1996 struct mdd_object *p, struct mdd_object *c,
1997 struct lu_attr *attr,
1998 struct thandle *handle,
1999 const struct md_op_spec *spec,
2000 struct lu_buf *def_acl_buf,
2001 struct lu_buf *acl_buf,
2002 struct dt_allocation_hint *hint)
2006 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2011 #ifdef CONFIG_FS_POSIX_ACL
2012 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2013 /* if dir, then can inherit default ACl */
2014 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2015 XATTR_NAME_ACL_DEFAULT,
2021 if (acl_buf->lb_len > 0) {
2022 rc = mdo_declare_attr_set(env, c, attr, handle);
2026 rc = mdo_declare_xattr_set(env, c, acl_buf,
2027 XATTR_NAME_ACL_ACCESS, 0, handle);
2032 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2036 /* replay case, create LOV EA from client data */
2037 if (spec->no_create ||
2038 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2039 const struct lu_buf *buf;
2041 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2042 spec->u.sp_ea.eadatalen);
2043 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2049 if (S_ISLNK(attr->la_mode)) {
2050 const char *target_name = spec->u.sp_symname;
2051 int sym_len = strlen(target_name);
2052 const struct lu_buf *buf;
2054 buf = mdd_buf_get_const(env, target_name, sym_len);
2055 rc = dt_declare_record_write(env, mdd_object_child(c),
2064 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2065 struct mdd_object *p, struct mdd_object *c,
2066 const struct lu_name *name,
2067 struct lu_attr *attr,
2068 struct thandle *handle,
2069 const struct md_op_spec *spec,
2070 struct linkea_data *ldata,
2071 struct lu_buf *def_acl_buf,
2072 struct lu_buf *acl_buf,
2073 struct dt_allocation_hint *hint)
2077 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2078 def_acl_buf, acl_buf, hint);
2082 if (S_ISDIR(attr->la_mode)) {
2083 rc = mdo_declare_ref_add(env, p, handle);
2088 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2089 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2093 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2095 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2096 name->ln_name, handle);
2100 rc = mdd_declare_links_add(env, c, handle, ldata);
2105 la->la_valid = LA_CTIME | LA_MTIME;
2106 rc = mdo_declare_attr_set(env, p, la, handle);
2110 rc = mdd_declare_changelog_store(env, mdd, name, handle);
2115 /* XXX: For remote create, it should indicate the remote RPC
2116 * will be sent after local transaction is finished, which
2117 * is not very nice, but it will be removed once we fully support
2119 if (mdd_object_remote(p) && handle->th_update != NULL)
2120 handle->th_update->tu_sent_after_local_trans = 1;
2125 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2126 struct lu_attr *la, struct lu_buf *def_acl_buf,
2127 struct lu_buf *acl_buf)
2132 if (S_ISLNK(la->la_mode)) {
2133 acl_buf->lb_len = 0;
2134 def_acl_buf->lb_len = 0;
2138 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2139 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2140 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2141 mdd_read_unlock(env, pobj);
2143 /* If there are default ACL, fix mode/ACL by default ACL */
2144 def_acl_buf->lb_len = rc;
2145 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2146 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2147 acl_buf->lb_len = rc;
2148 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2151 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2152 /* If there are no default ACL, fix mode by mask */
2153 struct lu_ucred *uc = lu_ucred(env);
2155 /* The create triggered by MDT internal events, such as
2156 * LFSCK reset, will not contain valid "uc". */
2157 if (unlikely(uc != NULL))
2158 la->la_mode &= ~uc->uc_umask;
2160 acl_buf->lb_len = 0;
2161 def_acl_buf->lb_len = 0;
2168 * Create a metadata object and initialize it, set acl, xattr.
2170 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2171 struct mdd_object *son, struct lu_attr *attr,
2172 struct md_op_spec *spec, struct lu_buf *acl_buf,
2173 struct lu_buf *def_acl_buf,
2174 struct dt_allocation_hint *hint,
2175 struct thandle *handle)
2179 mdd_write_lock(env, son, MOR_TGT_CHILD);
2180 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2185 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2186 * created in declare phase, they also needs to be added to master
2187 * object as sub-directory entry. So it has to initialize the master
2188 * object, then set dir striped EA.(in mdo_xattr_set) */
2189 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2192 GOTO(err_destroy, rc);
2195 * in case of replay we just set LOVEA provided by the client
2196 * XXX: I think it would be interesting to try "old" way where
2197 * MDT calls this xattr_set(LOV) in a different transaction.
2198 * probably this way we code can be made better.
2201 /* During creation, there are only a few cases we need do xattr_set to
2203 * 1. regular file: see comments above.
2204 * 2. create striped directory with provided stripeEA.
2205 * 3. create striped directory because inherit default layout from the
2207 if (spec->no_create ||
2208 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2209 S_ISDIR(attr->la_mode)) {
2210 const struct lu_buf *buf;
2212 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2213 spec->u.sp_ea.eadatalen);
2214 rc = mdo_xattr_set(env, son, buf,
2215 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2217 handle, BYPASS_CAPA);
2219 GOTO(err_destroy, rc);
2222 #ifdef CONFIG_FS_POSIX_ACL
2223 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2224 S_ISDIR(attr->la_mode)) {
2225 /* set default acl */
2226 rc = mdo_xattr_set(env, son, def_acl_buf,
2227 XATTR_NAME_ACL_DEFAULT, 0,
2228 handle, BYPASS_CAPA);
2230 GOTO(err_destroy, rc);
2232 /* set its own acl */
2233 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2234 rc = mdo_xattr_set(env, son, acl_buf,
2235 XATTR_NAME_ACL_ACCESS,
2236 0, handle, BYPASS_CAPA);
2238 GOTO(err_destroy, rc);
2242 if (S_ISLNK(attr->la_mode)) {
2243 struct lu_ucred *uc = lu_ucred_assert(env);
2244 struct dt_object *dt = mdd_object_child(son);
2245 const char *target_name = spec->u.sp_symname;
2246 int sym_len = strlen(target_name);
2247 const struct lu_buf *buf;
2250 buf = mdd_buf_get_const(env, target_name, sym_len);
2251 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2252 mdd_object_capa(env, son),
2254 CFS_CAP_SYS_RESOURCE_MASK);
2259 GOTO(err_initlized, rc = -EFAULT);
2263 if (unlikely(rc != 0)) {
2265 if (S_ISDIR(attr->la_mode)) {
2266 /* Drop the reference, no need to delete "."/"..",
2267 * because the object to be destroied directly. */
2268 rc2 = mdo_ref_del(env, son, handle);
2272 rc2 = mdo_ref_del(env, son, handle);
2276 mdo_destroy(env, son, handle);
2279 mdd_write_unlock(env, son);
2284 * Create object and insert it into namespace.
2286 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2287 const struct lu_name *lname, struct md_object *child,
2288 struct md_op_spec *spec, struct md_attr* ma)
2290 struct mdd_thread_info *info = mdd_env_info(env);
2291 struct lu_attr *la = &info->mti_la_for_fix;
2292 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2293 struct mdd_object *son = md2mdd_obj(child);
2294 struct mdd_device *mdd = mdo2mdd(pobj);
2295 struct lu_attr *attr = &ma->ma_attr;
2296 struct thandle *handle;
2297 struct lu_attr *pattr = &info->mti_pattr;
2298 struct lu_buf acl_buf;
2299 struct lu_buf def_acl_buf;
2300 struct linkea_data *ldata = &info->mti_link_data;
2301 const char *name = lname->ln_name;
2302 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2308 * Two operations have to be performed:
2310 * - an allocation of a new object (->do_create()), and
2312 * - an insertion into a parent index (->dio_insert()).
2314 * Due to locking, operation order is not important, when both are
2315 * successful, *but* error handling cases are quite different:
2317 * - if insertion is done first, and following object creation fails,
2318 * insertion has to be rolled back, but this operation might fail
2319 * also leaving us with dangling index entry.
2321 * - if creation is done first, is has to be undone if insertion
2322 * fails, leaving us with leaked space, which is neither good, nor
2325 * It seems that creation-first is simplest solution, but it is
2326 * sub-optimal in the frequent
2331 * case, because second mkdir is bound to create object, only to
2332 * destroy it immediately.
2334 * To avoid this follow local file systems that do double lookup:
2336 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2338 * 1. create (mdd_object_create_internal())
2340 * 2. insert (__mdd_index_insert(), lookup again)
2343 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2347 /* Sanity checks before big job. */
2348 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2352 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2353 GOTO(out_free, rc = -EINPROGRESS);
2355 handle = mdd_trans_create(env, mdd);
2357 GOTO(out_free, rc = PTR_ERR(handle));
2359 acl_buf.lb_buf = info->mti_xattr_buf;
2360 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2361 def_acl_buf.lb_buf = info->mti_key;
2362 def_acl_buf.lb_len = sizeof(info->mti_key);
2363 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2367 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2369 memset(ldata, 0, sizeof(*ldata));
2370 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT2)) {
2371 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2374 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2375 &tfid, lname, 1, 0, ldata);
2377 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2378 mdd_object_fid(mdd_pobj),
2379 lname, 1, 0, ldata);
2382 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2383 handle, spec, ldata, &def_acl_buf, &acl_buf,
2388 rc = mdd_trans_start(env, mdd, handle);
2392 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2393 &def_acl_buf, hint, handle);
2397 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2398 mdd_write_lock(env, son, MOR_TGT_CHILD);
2399 rc = __mdd_orphan_add(env, son, handle);
2400 GOTO(out_volatile, rc);
2402 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2403 attr->la_mode, name, handle,
2404 mdd_object_capa(env, mdd_pobj));
2406 GOTO(err_created, rc);
2408 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2411 /* update parent directory mtime/ctime */
2413 la->la_valid = LA_CTIME | LA_MTIME;
2414 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2416 GOTO(err_insert, rc);
2424 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2425 rc2 = __mdd_orphan_del(env, son, handle);
2427 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2428 S_ISDIR(attr->la_mode),
2429 handle, BYPASS_CAPA);
2434 mdd_write_lock(env, son, MOR_TGT_CHILD);
2435 if (S_ISDIR(attr->la_mode)) {
2436 /* Drop the reference, no need to delete "."/"..",
2437 * because the object is to be destroyed directly. */
2438 rc2 = mdo_ref_del(env, son, handle);
2440 mdd_write_unlock(env, son);
2445 /* For volatile files drop one link immediately, since there is
2446 * no filename in the namespace, and save any error returned. */
2447 rc2 = mdo_ref_del(env, son, handle);
2449 mdd_write_unlock(env, son);
2450 if (unlikely(rc == 0))
2455 /* Don't destroy the volatile object on success */
2456 if (likely(rc != 0))
2457 mdo_destroy(env, son, handle);
2458 mdd_write_unlock(env, son);
2461 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2462 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2463 rc = mdd_changelog_ns_store(env, mdd,
2464 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2465 S_ISREG(attr->la_mode) ? CL_CREATE :
2466 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2467 0, son, mdd_pobj, lname, handle);
2469 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2473 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2474 /* if we vmalloced a large buffer drop it */
2475 lu_buf_free(ldata->ld_buf);
2477 /* The child object shouldn't be cached anymore */
2479 set_bit(LU_OBJECT_HEARD_BANSHEE,
2480 &child->mo_lu.lo_header->loh_flags);
2485 * Get locks on parents in proper order
2486 * RETURN: < 0 - error, rename_order if successful
2494 static int mdd_rename_order(const struct lu_env *env,
2495 struct mdd_device *mdd,
2496 struct mdd_object *src_pobj,
2497 const struct lu_attr *pattr,
2498 struct mdd_object *tgt_pobj)
2500 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2504 if (src_pobj == tgt_pobj)
2505 RETURN(MDD_RN_SAME);
2507 /* compared the parent child relationship of src_p&tgt_p */
2508 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2510 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2513 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2527 /* has not mdd_write{read}_lock on any obj yet. */
2528 static int mdd_rename_sanity_check(const struct lu_env *env,
2529 struct mdd_object *src_pobj,
2530 const struct lu_attr *pattr,
2531 struct mdd_object *tgt_pobj,
2532 const struct lu_attr *tpattr,
2533 struct mdd_object *sobj,
2534 const struct lu_attr *cattr,
2535 struct mdd_object *tobj,
2536 const struct lu_attr *tattr)
2541 /* XXX: when get here, sobj must NOT be NULL,
2542 * the other case has been processed in cld_rename
2543 * before mdd_rename and enable MDS_PERM_BYPASS. */
2546 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2550 /* XXX: when get here, "tobj == NULL" means tobj must
2551 * NOT exist (neither on remote MDS, such case has been
2552 * processed in cld_rename before mdd_rename and enable
2554 * So check may_create, but not check may_unlink. */
2556 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2557 (src_pobj != tgt_pobj));
2559 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2560 (src_pobj != tgt_pobj), 1);
2562 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2563 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2568 static int mdd_declare_rename(const struct lu_env *env,
2569 struct mdd_device *mdd,
2570 struct mdd_object *mdd_spobj,
2571 struct mdd_object *mdd_tpobj,
2572 struct mdd_object *mdd_sobj,
2573 struct mdd_object *mdd_tobj,
2574 const struct lu_name *tname,
2575 const struct lu_name *sname,
2577 struct linkea_data *ldata,
2578 struct thandle *handle)
2580 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2583 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2584 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2590 /* name from source dir */
2591 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2595 /* .. from source child */
2596 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2597 /* source child can be directory,
2598 * counted by source dir's nlink */
2599 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2602 if (mdd_spobj != mdd_tpobj) {
2603 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2608 rc = mdo_declare_index_insert(env, mdd_sobj,
2610 S_IFDIR, dotdot, handle);
2615 /* new target child can be directory,
2616 * counted by target dir's nlink */
2617 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2622 la->la_valid = LA_CTIME | LA_MTIME;
2623 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2627 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2631 la->la_valid = LA_CTIME;
2632 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2636 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2641 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2642 mdd_object_type(mdd_sobj),
2643 tname->ln_name, handle);
2647 /* name from target dir (old name), we declare it unconditionally
2648 * as mdd_rename() calls delete unconditionally as well. so just
2649 * to balance declarations vs calls to change ... */
2650 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2654 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2655 /* delete target child in target parent directory */
2656 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2660 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2661 /* target child can be directory,
2662 * delete "." reference in target child directory */
2663 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2667 /* delete ".." reference in target parent directory */
2668 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2673 la->la_valid = LA_CTIME;
2674 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2678 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2683 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2690 /* src object can be remote that is why we use only fid and type of object */
2691 static int mdd_rename(const struct lu_env *env,
2692 struct md_object *src_pobj, struct md_object *tgt_pobj,
2693 const struct lu_fid *lf, const struct lu_name *lsname,
2694 struct md_object *tobj, const struct lu_name *ltname,
2697 const char *sname = lsname->ln_name;
2698 const char *tname = ltname->ln_name;
2699 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2700 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2701 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2702 struct mdd_device *mdd = mdo2mdd(src_pobj);
2703 struct mdd_object *mdd_sobj = NULL; /* source object */
2704 struct mdd_object *mdd_tobj = NULL;
2705 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2706 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2707 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2708 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2709 struct thandle *handle;
2710 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2711 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2712 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2715 bool tobj_locked = 0;
2716 unsigned cl_flags = 0;
2721 mdd_tobj = md2mdd_obj(tobj);
2723 mdd_sobj = mdd_object_find(env, mdd, lf);
2725 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2727 GOTO(out_pending, rc);
2729 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2731 GOTO(out_pending, rc);
2734 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2736 GOTO(out_pending, rc);
2739 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2741 GOTO(out_pending, rc);
2743 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2744 mdd_sobj, cattr, mdd_tobj, tattr);
2746 GOTO(out_pending, rc);
2748 rc = mdd_name_check(mdd, ltname);
2750 GOTO(out_pending, rc);
2752 handle = mdd_trans_create(env, mdd);
2754 GOTO(out_pending, rc = PTR_ERR(handle));
2756 memset(ldata, 0, sizeof(*ldata));
2757 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2758 ltname, 1, 0, ldata);
2759 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2760 mdd_tobj, lsname, ltname, ma, ldata, handle);
2764 rc = mdd_trans_start(env, mdd, handle);
2768 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2769 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2771 GOTO(cleanup_unlocked, rc);
2773 is_dir = S_ISDIR(cattr->la_mode);
2775 /* Remove source name from source directory */
2776 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2777 mdd_object_capa(env, mdd_spobj));
2781 /* "mv dir1 dir2" needs "dir1/.." link update */
2782 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2783 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2784 mdd_object_capa(env, mdd_sobj));
2786 GOTO(fixup_spobj2, rc);
2788 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2790 mdd_object_capa(env, mdd_sobj));
2792 GOTO(fixup_spobj, rc);
2795 /* Remove target name from target directory
2796 * Here tobj can be remote one, so we do index_delete unconditionally
2797 * and -ENOENT is allowed.
2799 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2800 mdd_object_capa(env, mdd_tpobj));
2803 /* tname might been renamed to something else */
2804 GOTO(fixup_spobj, rc);
2807 GOTO(fixup_spobj, rc);
2810 /* Insert new fid with target name into target dir */
2811 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2812 tname, handle, mdd_object_capa(env, mdd_tpobj));
2814 GOTO(fixup_tpobj, rc);
2816 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2817 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2819 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2821 la->la_valid = LA_CTIME;
2822 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2824 GOTO(fixup_tpobj, rc);
2827 /* Remove old target object
2828 * For tobj is remote case cmm layer has processed
2829 * and set tobj to NULL then. So when tobj is NOT NULL,
2830 * it must be local one.
2832 if (tobj && mdd_object_exists(mdd_tobj)) {
2833 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2835 if (mdd_is_dead_obj(mdd_tobj)) {
2836 /* shld not be dead, something is wrong */
2837 CERROR("tobj is dead, something is wrong\n");
2841 mdo_ref_del(env, mdd_tobj, handle);
2843 /* Remove dot reference. */
2844 if (S_ISDIR(tattr->la_mode))
2845 mdo_ref_del(env, mdd_tobj, handle);
2848 /* fetch updated nlink */
2849 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2851 CERROR("%s: Failed to get nlink for tobj "
2853 mdd2obd_dev(mdd)->obd_name,
2854 PFID(tpobj_fid), rc);
2855 GOTO(fixup_tpobj, rc);
2858 la->la_valid = LA_CTIME;
2859 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2861 CERROR("%s: Failed to set ctime for tobj "
2863 mdd2obd_dev(mdd)->obd_name,
2864 PFID(tpobj_fid), rc);
2865 GOTO(fixup_tpobj, rc);
2868 /* XXX: this transfer to ma will be removed with LOD/OSP */
2869 ma->ma_attr = *tattr;
2870 ma->ma_valid |= MA_INODE;
2871 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2874 CERROR("%s: Failed to unlink tobj "
2876 mdd2obd_dev(mdd)->obd_name,
2877 PFID(tpobj_fid), rc);
2878 GOTO(fixup_tpobj, rc);
2881 /* fetch updated nlink */
2882 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2884 CERROR("%s: Failed to get nlink for tobj "
2886 mdd2obd_dev(mdd)->obd_name,
2887 PFID(tpobj_fid), rc);
2888 GOTO(fixup_tpobj, rc);
2890 /* XXX: this transfer to ma will be removed with LOD/OSP */
2891 ma->ma_attr = *tattr;
2892 ma->ma_valid |= MA_INODE;
2894 if (tattr->la_nlink == 0) {
2895 cl_flags |= CLF_RENAME_LAST;
2896 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2897 cl_flags |= CLF_RENAME_LAST_EXISTS;
2901 la->la_valid = LA_CTIME | LA_MTIME;
2902 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2904 GOTO(fixup_tpobj, rc);
2906 if (mdd_spobj != mdd_tpobj) {
2907 la->la_valid = LA_CTIME | LA_MTIME;
2908 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2911 if (rc == 0 && mdd_sobj) {
2912 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2913 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2914 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2917 /* Old files might not have EA entry */
2918 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2919 lsname, handle, NULL, 0);
2920 mdd_write_unlock(env, mdd_sobj);
2921 /* We don't fail the transaction if the link ea can't be
2922 updated -- fid2path will use alternate lookup method. */
2930 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2933 CWARN("tp obj fix error %d\n",rc2);
2935 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2936 !mdd_is_dead_obj(mdd_tobj)) {
2938 mdo_ref_add(env, mdd_tobj, handle);
2940 mdo_ref_add(env, mdd_tobj, handle);
2943 rc2 = __mdd_index_insert(env, mdd_tpobj,
2945 mdd_object_type(mdd_tobj),
2946 tname, handle, BYPASS_CAPA);
2948 CWARN("tp obj fix error: rc = %d\n", rc2);
2953 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2954 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2958 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2959 mdd2obd_dev(mdd)->obd_name, rc2);
2962 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2963 dotdot, handle, BYPASS_CAPA);
2965 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2966 mdd2obd_dev(mdd)->obd_name, rc2);
2971 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2972 mdd_object_type(mdd_sobj), sname,
2973 handle, BYPASS_CAPA);
2975 CWARN("sp obj fix error: rc = %d\n", rc2);
2980 mdd_write_unlock(env, mdd_tobj);
2984 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2985 mdd_tobj, tpobj_fid, lf,
2986 spobj_fid, ltname, lsname,
2990 mdd_trans_stop(env, mdd, rc, handle);
2993 mdd_object_put(env, mdd_sobj);
2998 * During migration once the parent FID has been changed,
2999 * we need update the parent FID in linkea.
3001 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3002 struct mdd_object *parent,
3003 struct mdd_object *child,
3004 const char *name, int namelen,
3005 struct thandle *handle,
3008 struct mdd_thread_info *info = mdd_env_info(env);
3009 struct linkea_data ldata = {0};
3010 struct lu_buf *buf = &info->mti_link_buf;
3016 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3017 if (buf->lb_buf == NULL)
3021 rc = mdd_links_read(env, child, &ldata);
3023 if (rc == -ENOENT || rc == -ENODATA)
3028 LASSERT(ldata.ld_leh != NULL);
3029 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3030 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3031 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3032 struct lu_name lname;
3035 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3038 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3039 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3040 ldata.ld_lee = (struct link_ea_entry *)
3041 ((char *)ldata.ld_lee +
3046 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3047 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3048 lname.ln_namelen, lname.ln_name,
3049 PFID(mdd_object_fid(parent)));
3050 /* update to the new parent fid */
3051 linkea_entry_pack(ldata.ld_lee, &lname,
3052 mdd_object_fid(parent));
3054 rc = mdd_declare_links_add(env, child, handle, &ldata);
3056 rc = mdd_links_write(env, child, &ldata, handle);
3062 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3063 struct mdd_object *parent,
3064 struct mdd_object *child,
3065 const char *name, int namelen,
3066 struct thandle *handle)
3068 return mdd_linkea_update_child_internal(env, parent, child, name,
3069 namelen, handle, true);
3072 static int mdd_linkea_update_child(const struct lu_env *env,
3073 struct mdd_object *parent,
3074 struct mdd_object *child,
3075 const char *name, int namelen,
3076 struct thandle *handle)
3078 return mdd_linkea_update_child_internal(env, parent, child, name,
3079 namelen, handle, false);
3082 static int mdd_update_linkea_internal(const struct lu_env *env,
3083 struct mdd_object *mdd_pobj,
3084 struct mdd_object *mdd_sobj,
3085 struct mdd_object *mdd_tobj,
3086 const struct lu_name *child_name,
3087 struct thandle *handle,
3090 struct mdd_thread_info *info = mdd_env_info(env);
3091 struct linkea_data *ldata = &info->mti_link_data;
3096 rc = mdd_links_read(env, mdd_sobj, ldata);
3098 if (rc == -ENOENT || rc == -ENODATA)
3104 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3106 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3111 /* If it is mulitple links file, we need update the name entry for
3113 LASSERT(ldata->ld_leh != NULL);
3114 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3115 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3116 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3117 struct mdd_object *pobj;
3118 struct lu_name lname;
3121 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3123 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3125 pobj = mdd_object_find(env, mdd, &fid);
3127 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3128 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3133 if (!mdd_object_exists(pobj)) {
3134 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3135 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3139 if (pobj == mdd_pobj &&
3140 lname.ln_namelen == child_name->ln_namelen &&
3141 strncmp(lname.ln_name, child_name->ln_name,
3142 lname.ln_namelen) == 0) {
3143 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3144 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3149 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3150 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3151 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3154 /* Remove source name from source directory */
3155 /* Insert new fid with target name into target dir */
3156 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3161 rc = mdo_declare_index_insert(env, pobj,
3162 mdd_object_fid(mdd_tobj),
3163 mdd_object_type(mdd_tobj),
3164 lname.ln_name, handle);
3168 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3172 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3176 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3178 mdd_object_capa(env, pobj));
3182 rc = __mdd_index_insert(env, pobj,
3183 mdd_object_fid(mdd_tobj),
3184 mdd_object_type(mdd_tobj),
3185 lname.ln_name, handle,
3186 mdd_object_capa(env, pobj));
3190 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3191 rc = mdo_ref_add(env, mdd_tobj, handle);
3192 mdd_write_unlock(env, mdd_tobj);
3196 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3197 mdo_ref_del(env, mdd_sobj, handle);
3198 mdd_write_unlock(env, mdd_sobj);
3201 mdd_object_put(env, pobj);
3209 static int mdd_migrate_xattrs(const struct lu_env *env,
3210 struct mdd_object *mdd_sobj,
3211 struct mdd_object *mdd_tobj)
3213 struct mdd_thread_info *info = mdd_env_info(env);
3214 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3216 struct thandle *handle;
3222 struct lu_buf list_xbuf;
3225 /* retrieve xattr list from the old object */
3226 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3227 mdd_object_capa(env, mdd_sobj));
3228 if (list_xsize == -ENODATA)
3234 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3235 if (info->mti_big_buf.lb_buf == NULL)
3238 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3239 list_xbuf.lb_len = list_xsize;
3240 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3241 mdd_object_capa(env, mdd_sobj));
3246 xname = list_xbuf.lb_buf;
3248 xlen = strnlen(xname, rem - 1) + 1;
3249 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3250 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3251 strcmp(XATTR_NAME_LMV, xname) == 0)
3254 /* For directory, if there are default layout, migrate here */
3255 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3256 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3259 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3261 mdd_object_capa(env, mdd_sobj));
3262 if (xsize == -ENODATA)
3267 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3268 if (info->mti_link_buf.lb_buf == NULL)
3269 GOTO(out, rc = -ENOMEM);
3271 xbuf.lb_len = xsize;
3272 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3273 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3274 mdd_object_capa(env, mdd_sobj));
3280 handle = mdd_trans_create(env, mdd);
3282 GOTO(out, rc = PTR_ERR(handle));
3284 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3287 GOTO(stop_trans, rc);
3288 /* Note: this transaction is part of migration, and it is not
3289 * the last step of migration, so we set th_local = 1 to avoid
3290 * update last rcvd for this transaction */
3291 handle->th_local = 1;
3292 rc = mdd_trans_start(env, mdd, handle);
3294 GOTO(stop_trans, rc);
3296 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3297 mdd_object_capa(env, mdd_sobj));
3299 GOTO(stop_trans, rc = 0);
3302 GOTO(stop_trans, rc);
3304 mdd_trans_stop(env, mdd, rc, handle);
3309 memmove(xname, xname + xlen, rem);
3315 static int mdd_declare_migrate_create(const struct lu_env *env,
3316 struct mdd_object *mdd_pobj,
3317 struct mdd_object *mdd_sobj,
3318 struct mdd_object *mdd_tobj,
3319 struct md_op_spec *spec,
3321 union lmv_mds_md *mgr_ea,
3322 struct thandle *handle)
3324 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3325 const struct lu_buf *buf;
3329 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3330 handle, spec, NULL);
3334 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3339 if (S_ISLNK(la->la_mode)) {
3340 const char *target_name = spec->u.sp_symname;
3341 int sym_len = strlen(target_name);
3342 const struct lu_buf *buf;
3344 buf = mdd_buf_get_const(env, target_name, sym_len);
3345 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3351 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3352 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3353 spec->u.sp_ea.eadatalen);
3354 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3360 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3361 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3362 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3367 la_flag->la_valid = LA_FLAGS;
3368 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3369 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3370 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3375 static int mdd_migrate_create(const struct lu_env *env,
3376 struct mdd_object *mdd_pobj,
3377 struct mdd_object *mdd_sobj,
3378 struct mdd_object *mdd_tobj,
3381 struct mdd_thread_info *info = mdd_env_info(env);
3382 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3383 struct md_op_spec *spec = &info->mti_spec;
3384 struct lu_buf lmm_buf = { 0 };
3385 struct lu_buf link_buf = { 0 };
3386 const struct lu_buf *buf;
3387 struct thandle *handle;
3388 struct lmv_mds_md_v1 *mgr_ea;
3389 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3390 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3395 /* prepare spec for create */
3396 memset(spec, 0, sizeof(*spec));
3397 spec->sp_cr_lookup = 0;
3398 spec->sp_feat = &dt_directory_features;
3399 if (S_ISLNK(la->la_mode)) {
3400 buf = lu_buf_check_and_alloc(
3401 &mdd_env_info(env)->mti_big_buf,
3404 link_buf.lb_len = la->la_size + 1;
3405 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3407 rc = rc != 0 ? rc : -EFAULT;
3408 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3409 mdd2obd_dev(mdd)->obd_name,
3410 PFID(mdd_object_fid(mdd_sobj)), rc);
3413 spec->u.sp_symname = link_buf.lb_buf;
3414 } else if S_ISREG(la->la_mode) {
3415 /* retrieve lov of the old object */
3416 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3417 if (rc != 0 && rc != -ENODATA)
3419 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3420 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3421 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3422 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3426 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3427 memset(mgr_ea, 0, sizeof(*mgr_ea));
3428 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3429 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3430 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3431 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3432 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3433 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3435 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3437 handle = mdd_trans_create(env, mdd);
3439 GOTO(out_free, rc = PTR_ERR(handle));
3441 /* Note: this transaction is part of migration, and it is not
3442 * the last step of migration, so we set th_local = 1 to avoid
3443 * update last rcvd for this transaction */
3444 handle->th_local = 1;
3445 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3447 (union lmv_mds_md *)info->mti_xattr_buf,
3450 GOTO(stop_trans, rc);
3452 rc = mdd_trans_start(env, mdd, handle);
3454 GOTO(stop_trans, rc);
3456 /* create the target object */
3457 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3460 GOTO(stop_trans, rc);
3462 /* Set MIGRATE EA on the source inode, so once the migration needs
3463 * to be re-done during failover, the re-do process can locate the
3464 * target object which is already being created. */
3465 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3466 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3467 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3468 handle, mdd_object_capa(env, mdd_sobj));
3470 GOTO(stop_trans, rc);
3472 /* Set immutable flag, so any modification is disabled until
3473 * the migration is done. Once the migration is interrupted,
3474 * if the resume process find the migrating object has both
3475 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3476 * flag and approve the migration */
3477 la_flag->la_valid = LA_FLAGS;
3478 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3479 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3480 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3481 mdd_object_capa(env, mdd_sobj));
3484 mdd_trans_stop(env, mdd, rc, handle);
3486 if (lmm_buf.lb_buf != NULL)
3487 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3491 static int mdd_migrate_entries(const struct lu_env *env,
3492 struct mdd_object *mdd_sobj,
3493 struct mdd_object *mdd_tobj)
3495 struct dt_object *next = mdd_object_child(mdd_sobj);
3496 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3497 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3498 struct thandle *handle;
3500 const struct dt_it_ops *iops;
3503 struct lu_dirent *ent;
3506 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3510 if (!dt_try_as_dir(env, next))
3511 GOTO(out_ent, rc = -ENOTDIR);
3513 * iterate directories
3515 iops = &next->do_index_ops->dio_it;
3516 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3517 mdd_object_capa(env, mdd_sobj));
3519 GOTO(out_ent, rc = PTR_ERR(it));
3521 rc = iops->load(env, it, 0);
3523 rc = iops->next(env, it);
3527 * At this point and across for-loop:
3529 * rc == 0 -> ok, proceed.
3530 * rc > 0 -> end of directory.
3534 struct mdd_object *child;
3535 char *name = mdd_env_info(env)->mti_key;
3539 bool target_exist = false;
3541 len = iops->key_size(env, it);
3545 result = iops->rec(env, it, (struct dt_rec *)ent,
3546 LUDA_FID | LUDA_TYPE);
3547 if (result == -ESTALE)
3554 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3555 recsize = le16_to_cpu(ent->lde_reclen);
3557 /* Insert new fid with target name into target dir */
3558 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3559 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3560 ent->lde_name[1] == '.'))
3563 child = mdd_object_find(env, mdd, &ent->lde_fid);
3565 GOTO(out, rc = PTR_ERR(child));
3567 is_dir = S_ISDIR(mdd_object_type(child));
3569 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3571 /* Check whether the name has been inserted to the target */
3572 if (dt_try_as_dir(env, dt_tobj)) {
3573 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3575 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3576 (struct dt_key *)name,
3577 mdd_object_capa(env, mdd_tobj));
3578 if (unlikely(rc == 0))
3579 target_exist = true;
3582 handle = mdd_trans_create(env, mdd);
3584 GOTO(out, rc = PTR_ERR(handle));
3586 /* Note: this transaction is part of migration, and it is not
3587 * the last step of migration, so we set th_local = 1 to avoid
3588 * updating last rcvd for this transaction */
3589 handle->th_local = 1;
3590 if (likely(!target_exist)) {
3591 rc = mdo_declare_index_insert(env, mdd_tobj,
3593 mdd_object_type(child),
3599 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3605 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3610 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3614 /* Update .. for child */
3615 rc = mdo_declare_index_delete(env, child, dotdot,
3620 rc = mdo_declare_index_insert(env, child,
3621 mdd_object_fid(mdd_tobj),
3622 S_IFDIR, dotdot, handle);
3627 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3634 rc = mdd_trans_start(env, mdd, handle);
3636 CERROR("%s: transaction start failed: rc = %d\n",
3637 mdd2obd_dev(mdd)->obd_name, rc);
3641 if (likely(!target_exist)) {
3642 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3643 mdd_object_type(child),
3645 mdd_object_capa(env, mdd_tobj));
3650 rc = mdo_ref_add(env, mdd_tobj, handle);
3657 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3658 mdd_object_capa(env, mdd_sobj));
3663 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3664 mdd_object_capa(env, child));
3668 rc = __mdd_index_insert_only(env, child,
3669 mdd_object_fid(mdd_tobj), S_IFDIR,
3671 mdd_object_capa(env, child));
3676 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3677 strlen(name), handle);
3680 mdd_object_put(env, child);
3681 mdd_trans_stop(env, mdd, rc, handle);
3685 result = iops->next(env, it);
3686 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3687 GOTO(out, rc = -EINTR);
3689 if (result == -ESTALE)
3691 } while (result == 0);
3694 iops->fini(env, it);
3696 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3700 static int mdd_declare_update_linkea(const struct lu_env *env,
3701 struct mdd_object *mdd_pobj,
3702 struct mdd_object *mdd_sobj,
3703 struct mdd_object *mdd_tobj,
3704 struct thandle *handle,
3705 const struct lu_name *child_name)
3707 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3708 child_name, handle, 1);
3711 static int mdd_update_linkea(const struct lu_env *env,
3712 struct mdd_object *mdd_pobj,
3713 struct mdd_object *mdd_sobj,
3714 struct mdd_object *mdd_tobj,
3715 struct thandle *handle,
3716 const struct lu_name *child_name)
3718 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3719 child_name, handle, 0);
3722 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3723 struct mdd_object *mdd_pobj,
3724 struct mdd_object *mdd_sobj,
3725 struct mdd_object *mdd_tobj,
3726 const struct lu_name *lname,
3728 struct lu_attr *parent_la,
3729 struct thandle *handle)
3731 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3734 /* Revert IMMUTABLE flag */
3735 la_flag->la_valid = LA_FLAGS;
3736 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3737 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3738 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3742 /* delete entry from source dir */
3743 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3747 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3748 mdd_tobj, handle, lname);
3752 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3753 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3759 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3760 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3766 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3767 mdd_object_type(mdd_tobj),
3768 lname->ln_name, handle);
3772 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3773 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3778 /* delete old object */
3779 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3783 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3784 /* delete old object */
3785 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3788 /* set nlink to 0 */
3789 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3794 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3798 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3803 static int mdd_migrate_update_name(const struct lu_env *env,
3804 struct mdd_object *mdd_pobj,
3805 struct mdd_object *mdd_sobj,
3806 struct mdd_object *mdd_tobj,
3807 const struct lu_name *lname,
3810 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3811 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3812 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3813 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3814 struct thandle *handle;
3815 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3816 const char *name = lname->ln_name;
3820 /* update time for parent */
3821 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3822 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3823 p_la->la_valid = LA_CTIME;
3825 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3829 handle = mdd_trans_create(env, mdd);
3831 RETURN(PTR_ERR(handle));
3833 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3834 lname, so_attr, p_la, handle);
3836 /* If the migration can not be fit in one transaction, just
3837 * leave it in the original MDT */
3839 GOTO(stop_trans, rc = 0);
3841 GOTO(stop_trans, rc);
3844 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3845 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3846 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3847 PFID(mdd_object_fid(mdd_tobj)));
3849 rc = mdd_trans_start(env, mdd, handle);
3851 GOTO(stop_trans, rc);
3853 /* Revert IMMUTABLE flag */
3854 la_flag->la_valid = LA_FLAGS;
3855 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3856 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3857 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3858 mdd_object_capa(env, mdd_pobj));
3860 GOTO(stop_trans, rc);
3862 /* Remove source name from source directory */
3863 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3864 mdd_object_capa(env, mdd_pobj));
3866 GOTO(stop_trans, rc);
3868 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3871 GOTO(stop_trans, rc);
3873 if (S_ISREG(so_attr->la_mode)) {
3874 if (so_attr->la_nlink == 1) {
3875 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3877 mdd_object_capa(env, mdd_sobj));
3878 if (rc != 0 && rc != -ENODATA)
3879 GOTO(stop_trans, rc);
3883 /* Insert new fid with target name into target dir */
3884 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3885 mdd_object_type(mdd_tobj), name,
3886 handle, mdd_object_capa(env, mdd_pobj));
3888 GOTO(stop_trans, rc);
3890 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3893 GOTO(stop_trans, rc);
3895 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3896 mdo_ref_del(env, mdd_sobj, handle);
3898 mdo_ref_del(env, mdd_sobj, handle);
3900 /* Get the attr again after ref_del */
3901 rc = mdd_la_get(env, mdd_sobj, so_attr,
3902 mdd_object_capa(env, mdd_sobj));
3904 GOTO(out_unlock, rc);
3906 ma->ma_attr = *so_attr;
3907 ma->ma_valid |= MA_INODE;
3908 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3910 GOTO(out_unlock, rc);
3912 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3914 GOTO(out_unlock, rc);
3917 mdd_write_unlock(env, mdd_sobj);
3920 mdd_trans_stop(env, mdd, rc, handle);
3926 * Check whether we should migrate the file/dir
3928 * < 0 permission check failed or other error.
3929 * = 0 the file can be migrated.
3930 * > 0 the file does not need to be migrated, mostly
3931 * for multiple link file
3933 static int mdd_migrate_sanity_check(const struct lu_env *env,
3934 struct mdd_object *pobj,
3935 const struct lu_attr *pattr,
3936 struct mdd_object *sobj,
3937 struct lu_attr *sattr)
3939 struct mdd_thread_info *info = mdd_env_info(env);
3940 struct linkea_data *ldata = &info->mti_link_data;
3942 struct lu_buf *mgr_buf;
3948 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3949 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3950 if (mgr_buf->lb_buf == NULL)
3953 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3954 mdd_object_capa(env, sobj));
3956 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3958 /* If the object has migrateEA, it means IMMUTE flag
3959 * is being set by previous migration process, so it
3960 * needs to override the IMMUTE flag, otherwise the
3961 * following sanity check will fail */
3962 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3963 LMV_HASH_FLAG_MIGRATION) {
3964 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3966 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3967 sobj->mod_flags &= ~IMMUTE_OBJ;
3968 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3969 mdd2obd_dev(mdd)->obd_name,
3970 PFID(mdd_object_fid(sobj)));
3974 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3975 sobj, sattr, NULL, NULL);
3979 /* Then it will check if the file should be migrated. If the file
3980 * has mulitple links, we only need migrate the file if all of its
3981 * entries has been migrated to the remote MDT */
3982 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3985 rc = mdd_links_read(env, sobj, ldata);
3987 /* For multiple links files, if there are no linkEA data at all,
3988 * means the file might be created before linkEA is enabled, and
3989 * all all of its links should not be migrated yet, otherwise
3990 * it should have some linkEA there */
3991 if (rc == -ENOENT || rc == -ENODATA)
3996 /* If it is mulitple links file, we need update the name entry for
3998 LASSERT(ldata->ld_leh != NULL);
3999 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4000 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4001 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4002 struct mdd_object *lpobj;
4003 struct lu_name lname;
4006 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4008 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4010 lpobj = mdd_object_find(env, mdd, &fid);
4011 if (IS_ERR(lpobj)) {
4012 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
4013 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
4018 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
4019 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
4020 PFID(&fid), lname.ln_namelen, lname.ln_name);
4021 mdd_object_put(env, lpobj);
4025 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4026 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4027 lname.ln_name, PFID(&fid));
4028 mdd_object_put(env, lpobj);
4036 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4037 struct md_object *sobj, const struct lu_name *lname,
4038 struct md_object *tobj, struct md_attr *ma)
4040 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4041 struct mdd_device *mdd = mdo2mdd(pobj);
4042 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4043 struct mdd_object *mdd_tobj = NULL;
4044 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4045 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4049 /* If the file will being migrated, it will check whether
4050 * the file is being opened by someone else right now */
4051 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4052 if (mdd_sobj->mod_count >= 1) {
4053 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4054 mdd2obd_dev(mdd)->obd_name,
4055 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4056 mdd_sobj->mod_count, -EBUSY);
4057 mdd_read_unlock(env, mdd_sobj);
4058 GOTO(put, rc = -EBUSY);
4060 mdd_read_unlock(env, mdd_sobj);
4062 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
4066 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
4070 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4077 /* Sigh, it is impossible to finish all of migration in a single
4078 * transaction, for example migrating big directory entries to the
4079 * new MDT, it needs insert all of name entries of children in the
4082 * So migration will be done in multiple steps and transactions.
4084 * 1. create an orphan object on the remote MDT in one transaction.
4085 * 2. migrate extend attributes to the new target file/directory.
4086 * 3. For directory, migrate the entries to the new MDT and update
4087 * linkEA of each children. Because we can not migrate all entries
4088 * in a single transaction, so the migrating directory will become
4089 * a striped directory during migration, so once the process is
4090 * interrupted, the directory is still accessible. (During lookup,
4091 * client will locate the name by searching both original and target
4093 * 4. Finally, update the name/FID to point to the new file/directory
4094 * in a separate transaction.
4097 /* step 1: Check whether the orphan object has been created, and create
4098 * orphan object on the remote MDT if needed */
4099 mdd_tobj = md2mdd_obj(tobj);
4100 if (!mdd_object_exists(mdd_tobj)) {
4101 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4107 /* step 2: migrate xattr */
4108 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4112 /* step 3: migrate name entries to the orphan object */
4113 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4114 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4117 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4118 OBD_FAIL_MDS_REINT_NET_REP)))
4122 /* step 4: update name entry to the new object */
4123 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4131 const struct md_dir_operations mdd_dir_ops = {
4132 .mdo_is_subdir = mdd_is_subdir,
4133 .mdo_lookup = mdd_lookup,
4134 .mdo_create = mdd_create,
4135 .mdo_rename = mdd_rename,
4136 .mdo_link = mdd_link,
4137 .mdo_unlink = mdd_unlink,
4138 .mdo_create_data = mdd_create_data,
4139 .mdo_migrate = mdd_migrate,