4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
61 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
63 if (!lu_name_is_valid(ln))
65 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
71 /* Get FID from name and parent */
73 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
74 const struct lu_attr *pattr, const struct lu_name *lname,
75 struct lu_fid* fid, int mask)
77 const char *name = lname->ln_name;
78 const struct dt_key *key = (const struct dt_key *)name;
79 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
80 struct mdd_device *m = mdo2mdd(pobj);
81 struct dt_object *dir = mdd_object_child(mdd_obj);
85 if (unlikely(mdd_is_dead_obj(mdd_obj)))
88 if (mdd_object_remote(mdd_obj)) {
89 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
90 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
91 } else if (!mdd_object_exists(mdd_obj)) {
95 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
100 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101 dt_try_as_dir(env, dir))) {
103 rc = dir->do_index_ops->dio_lookup(env, dir,
104 (struct dt_rec *)fid, key,
105 mdd_object_capa(env, mdd_obj));
116 int mdd_lookup(const struct lu_env *env,
117 struct md_object *pobj, const struct lu_name *lname,
118 struct lu_fid *fid, struct md_op_spec *spec)
120 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
124 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
128 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
132 static inline int mdd_parent_fid(const struct lu_env *env,
133 struct mdd_object *obj,
134 const struct lu_attr *attr,
137 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
141 * For root fid use special function, which does not compare version component
142 * of fid. Version component is different for root fids on all MDTs.
144 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
146 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
147 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
151 * return 1: if lf is the fid of the ancestor of p1;
154 * return -EREMOTE: if remote object is found, in this
155 * case fid of remote object is saved to @pf;
157 * otherwise: values < 0, errors.
159 static int mdd_is_parent(const struct lu_env *env,
160 struct mdd_device *mdd,
161 struct mdd_object *p1,
162 const struct lu_attr *attr,
163 const struct lu_fid *lf,
166 struct mdd_object *parent = NULL;
171 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
172 pfid = &mdd_env_info(env)->mti_fid;
174 /* Check for root first. */
175 if (mdd_is_root(mdd, mdo2fid(p1)))
179 /* this is done recursively, bypass capa for each obj */
180 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
181 rc = mdd_parent_fid(env, p1, attr, pfid);
184 if (mdd_is_root(mdd, pfid))
186 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
188 if (lu_fid_eq(pfid, lf))
191 mdd_object_put(env, parent);
193 parent = mdd_object_find(env, mdd, pfid);
194 if (IS_ERR(parent)) {
195 GOTO(out, rc = PTR_ERR(parent));
196 } else if (mdd_object_remote(parent)) {
197 /*FIXME: Because of the restriction of rename in Phase I.
198 * If the parent is remote, we just assumed lf is not the
199 * parent of P1 for now */
206 if (parent && !IS_ERR(parent))
207 mdd_object_put(env, parent);
212 * No permission check is needed.
214 * returns 1: if fid is ancestor of @mo;
215 * returns 0: if fid is not a ancestor of @mo;
217 * returns EREMOTE if remote object is found, fid of remote object is saved to
220 * returns < 0: if error
222 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
223 const struct lu_fid *fid, struct lu_fid *sfid)
225 struct mdd_device *mdd = mdo2mdd(mo);
226 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
230 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
233 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
237 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
241 } else if (rc == 1) {
242 /* found @fid is parent */
250 * Check that @dir contains no entries except (possibly) dot and dotdot.
255 * -ENOTDIR not a directory object
256 * -ENOTEMPTY not empty
260 static int mdd_dir_is_empty(const struct lu_env *env,
261 struct mdd_object *dir)
264 struct dt_object *obj;
265 const struct dt_it_ops *iops;
269 obj = mdd_object_child(dir);
270 if (!dt_try_as_dir(env, obj))
273 iops = &obj->do_index_ops->dio_it;
274 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
276 result = iops->get(env, it, (const struct dt_key *)"");
279 for (result = 0, i = 0; result == 0 && i < 3; ++i)
280 result = iops->next(env, it);
283 else if (result == 1)
285 } else if (result == 0)
287 * Huh? Index contains no zero key?
294 result = PTR_ERR(it);
298 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
299 const struct lu_attr *la)
301 struct mdd_device *m = mdd_obj2mdd_dev(obj);
306 if (!S_ISDIR(la->la_mode))
310 * Subdir count limitation can be broken through.
312 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
319 * Check whether it may create the cobj under the pobj.
322 int mdd_may_create(const struct lu_env *env,
323 struct mdd_object *pobj, const struct lu_attr *pattr,
324 struct mdd_object *cobj, bool check_perm, bool check_nlink)
326 struct mdd_thread_info *info = mdd_env_info(env);
331 if (cobj && mdd_object_exists(cobj))
334 if (mdd_is_dead_obj(pobj))
337 /* If the parent is a sub-stripe, check whether it is dead */
338 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
339 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV,
340 mdd_object_capa(env, pobj));
341 if (unlikely(rc > 0)) {
342 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
344 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
345 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
351 rc = mdd_permission_internal_locked(env, pobj, pattr,
352 MAY_WRITE | MAY_EXEC,
354 if (!rc && check_nlink)
355 rc = __mdd_may_link(env, pobj, pattr);
361 * Check whether can unlink from the pobj in the case of "cobj == NULL".
363 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
364 const struct lu_attr *pattr, const struct lu_attr *attr)
369 if (mdd_is_dead_obj(pobj))
372 if ((attr->la_valid & LA_FLAGS) &&
373 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
376 rc = mdd_permission_internal_locked(env, pobj, pattr,
377 MAY_WRITE | MAY_EXEC,
382 if (mdd_is_append(pobj))
389 * pobj == NULL is remote ops case, under such case, pobj's
390 * VTX feature has been checked already, no need check again.
392 static inline int mdd_is_sticky(const struct lu_env *env,
393 struct mdd_object *pobj,
394 const struct lu_attr *pattr,
395 struct mdd_object *cobj,
396 const struct lu_attr *cattr)
398 struct lu_ucred *uc = lu_ucred_assert(env);
401 LASSERT(pattr != NULL);
402 if (!(pattr->la_mode & S_ISVTX) ||
403 (pattr->la_uid == uc->uc_fsuid))
407 LASSERT(cattr != NULL);
408 if (cattr->la_uid == uc->uc_fsuid)
411 return !md_capable(uc, CFS_CAP_FOWNER);
414 static int mdd_may_delete_entry(const struct lu_env *env,
415 struct mdd_object *pobj,
416 const struct lu_attr *pattr,
421 LASSERT(pobj != NULL);
422 if (!mdd_object_exists(pobj))
425 if (mdd_is_dead_obj(pobj))
430 rc = mdd_permission_internal_locked(env, pobj, pattr,
431 MAY_WRITE | MAY_EXEC,
437 if (mdd_is_append(pobj))
444 * Check whether it may delete the cobj from the pobj.
447 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
448 const struct lu_attr *tpattr, struct mdd_object *tobj,
449 const struct lu_attr *tattr, const struct lu_attr *cattr,
450 int check_perm, int check_empty)
456 LASSERT(tpattr != NULL);
457 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
465 if (!mdd_object_exists(tobj))
468 if (mdd_is_dead_obj(tobj))
471 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
474 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
477 if ((tattr->la_valid & LA_FLAGS) &&
478 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
481 /* additional check the rename case */
483 if (S_ISDIR(cattr->la_mode)) {
484 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
486 if (!S_ISDIR(tattr->la_mode))
489 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
491 } else if (S_ISDIR(tattr->la_mode))
495 if (S_ISDIR(tattr->la_mode) && check_empty)
496 rc = mdd_dir_is_empty(env, tobj);
503 * has mdd_write_lock on src already, but not on tgt yet
505 static int mdd_link_sanity_check(const struct lu_env *env,
506 struct mdd_object *tgt_obj,
507 const struct lu_attr *tattr,
508 const struct lu_name *lname,
509 struct mdd_object *src_obj,
510 const struct lu_attr *cattr)
512 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
516 if (!mdd_object_exists(src_obj))
519 if (mdd_is_dead_obj(src_obj))
522 /* Local ops, no lookup before link, check filename length here. */
523 rc = mdd_name_check(m, lname);
527 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
530 if (S_ISDIR(mdd_object_type(src_obj)))
533 LASSERT(src_obj != tgt_obj);
535 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true, false);
540 rc = __mdd_may_link(env, src_obj, cattr);
545 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
546 const char *name, struct thandle *handle,
547 struct lustre_capa *capa)
549 struct dt_object *next = mdd_object_child(pobj);
553 if (dt_try_as_dir(env, next)) {
554 rc = next->do_index_ops->dio_delete(env, next,
555 (struct dt_key *)name,
563 static int __mdd_index_insert_only(const struct lu_env *env,
564 struct mdd_object *pobj,
565 const struct lu_fid *lf, const char *name,
566 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 struct lu_ucred *uc = lu_ucred_check(env);
577 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
578 rc = next->do_index_ops->dio_insert(env, next,
580 (const struct dt_key *)name,
581 handle, capa, ignore_quota);
588 /* insert named index, add reference if isdir */
589 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
590 const struct lu_fid *lf, const char *name, int is_dir,
591 struct thandle *handle, struct lustre_capa *capa)
596 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
597 if (rc == 0 && is_dir) {
598 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
599 mdo_ref_add(env, pobj, handle);
600 mdd_write_unlock(env, pobj);
605 /* delete named index, drop reference if isdir */
606 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
607 const char *name, int is_dir, struct thandle *handle,
608 struct lustre_capa *capa)
613 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
614 if (rc == 0 && is_dir) {
615 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
616 mdo_ref_del(env, pobj, handle);
617 mdd_write_unlock(env, pobj);
623 int mdd_declare_changelog_store(const struct lu_env *env,
624 struct mdd_device *mdd,
625 const struct lu_name *fname,
626 struct thandle *handle)
628 struct obd_device *obd = mdd2obd_dev(mdd);
629 struct llog_ctxt *ctxt;
630 struct llog_changelog_rec *rec;
636 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
639 reclen = llog_data_len(sizeof(*rec) +
640 (fname != NULL ? fname->ln_namelen : 0));
641 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
642 if (buf->lb_buf == NULL)
646 rec->cr_hdr.lrh_len = reclen;
647 rec->cr_hdr.lrh_type = CHANGELOG_REC;
649 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
653 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
659 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
660 struct mdd_device *mdd,
661 const struct lu_name *tname,
662 const struct lu_name *sname,
663 struct thandle *handle)
665 struct obd_device *obd = mdd2obd_dev(mdd);
666 struct llog_ctxt *ctxt;
667 struct llog_changelog_ext_rec *rec;
673 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
676 reclen = llog_data_len(sizeof(*rec) +
677 (tname != NULL ? tname->ln_namelen : 0) +
678 (sname != NULL ? 1 + sname->ln_namelen : 0));
679 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
680 if (buf->lb_buf == NULL)
684 rec->cr_hdr.lrh_len = reclen;
685 rec->cr_hdr.lrh_type = CHANGELOG_REC;
687 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
691 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
697 /** Add a changelog entry \a rec to the changelog llog
700 * \param handle - currently ignored since llogs start their own transaction;
701 * this will hopefully be fixed in llog rewrite
704 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
705 struct llog_changelog_rec *rec, struct thandle *th)
707 struct obd_device *obd = mdd2obd_dev(mdd);
708 struct llog_ctxt *ctxt;
711 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
712 rec->cr_hdr.lrh_type = CHANGELOG_REC;
713 rec->cr.cr_time = cl_time();
715 spin_lock(&mdd->mdd_cl.mc_lock);
716 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
717 * but as long as the MDD transactions are ordered correctly for e.g.
718 * rename conflicts, I don't think this should matter. */
719 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
720 spin_unlock(&mdd->mdd_cl.mc_lock);
722 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
726 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
733 /** Add a changelog_ext entry \a rec to the changelog llog
736 * \param handle - currently ignored since llogs start their own transaction;
737 * this will hopefully be fixed in llog rewrite
741 mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
742 struct llog_changelog_ext_rec *rec, struct thandle *th)
744 struct obd_device *obd = mdd2obd_dev(mdd);
745 struct llog_ctxt *ctxt;
748 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
749 /* llog_lvfs_write_rec sets the llog tail len */
750 rec->cr_hdr.lrh_type = CHANGELOG_REC;
751 rec->cr.cr_time = cl_time();
753 spin_lock(&mdd->mdd_cl.mc_lock);
754 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
755 * but as long as the MDD transactions are ordered correctly for e.g.
756 * rename conflicts, I don't think this should matter. */
757 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
758 spin_unlock(&mdd->mdd_cl.mc_lock);
760 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
764 /* nested journal transaction */
765 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
773 /** Store a namespace change changelog record
774 * If this fails, we must fail the whole transaction; we don't
775 * want the change to commit without the log entry.
776 * \param target - mdd_object of change
777 * \param parent - parent dir/object
778 * \param tname - target name string
779 * \param handle - transacion handle
781 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
782 enum changelog_rec_type type, unsigned flags,
783 struct mdd_object *target, struct mdd_object *parent,
784 const struct lu_name *tname, struct thandle *handle)
786 struct llog_changelog_rec *rec;
793 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
795 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
798 LASSERT(target != NULL);
799 LASSERT(parent != NULL);
800 LASSERT(tname != NULL);
801 LASSERT(handle != NULL);
803 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
804 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
805 if (buf->lb_buf == NULL)
809 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
810 rec->cr.cr_type = (__u32)type;
811 rec->cr.cr_tfid = *mdo2fid(target);
812 rec->cr.cr_pfid = *mdo2fid(parent);
813 rec->cr.cr_namelen = tname->ln_namelen;
814 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
816 target->mod_cltime = cfs_time_current_64();
818 rc = mdd_changelog_store(env, mdd, rec, handle);
820 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
821 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
822 PFID(&rec->cr.cr_pfid));
830 /** Store a namespace change changelog record
831 * If this fails, we must fail the whole transaction; we don't
832 * want the change to commit without the log entry.
833 * \param target - mdd_object of change
834 * \param tpfid - target parent dir/object fid
835 * \param sfid - source object fid
836 * \param spfid - source parent fid
837 * \param tname - target name string
838 * \param sname - source name string
839 * \param handle - transacion handle
841 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
842 struct mdd_device *mdd,
843 enum changelog_rec_type type,
845 struct mdd_object *target,
846 const struct lu_fid *tpfid,
847 const struct lu_fid *sfid,
848 const struct lu_fid *spfid,
849 const struct lu_name *tname,
850 const struct lu_name *sname,
851 struct thandle *handle)
853 struct llog_changelog_ext_rec *rec;
860 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
862 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
865 LASSERT(sfid != NULL);
866 LASSERT(tpfid != NULL);
867 LASSERT(tname != NULL);
868 LASSERT(handle != NULL);
870 reclen = llog_data_len(sizeof(*rec) +
871 sname != NULL ? 1 + sname->ln_namelen : 0);
872 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
873 if (buf->lb_buf == NULL)
877 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
878 rec->cr.cr_type = (__u32)type;
879 rec->cr.cr_pfid = *tpfid;
880 rec->cr.cr_sfid = *sfid;
881 rec->cr.cr_spfid = *spfid;
882 rec->cr.cr_namelen = tname->ln_namelen;
883 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
885 rec->cr.cr_name[tname->ln_namelen] = '\0';
886 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
888 rec->cr.cr_namelen += 1 + sname->ln_namelen;
891 if (likely(target != NULL)) {
892 rec->cr.cr_tfid = *mdo2fid(target);
893 target->mod_cltime = cfs_time_current_64();
895 fid_zero(&rec->cr.cr_tfid);
898 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
900 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
901 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
908 static int __mdd_links_add(const struct lu_env *env,
909 struct mdd_object *mdd_obj,
910 struct linkea_data *ldata,
911 const struct lu_name *lname,
912 const struct lu_fid *pfid,
913 int first, int check)
917 if (ldata->ld_leh == NULL) {
918 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
922 rc = linkea_data_new(ldata,
923 &mdd_env_info(env)->mti_link_buf);
930 rc = linkea_links_find(ldata, lname, pfid);
931 if (rc && rc != -ENOENT)
937 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
938 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
942 linkea_add_buf(ldata, lname, tfid);
945 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
946 linkea_add_buf(ldata, lname, pfid);
948 return linkea_add_buf(ldata, lname, pfid);
951 static int __mdd_links_del(const struct lu_env *env,
952 struct mdd_object *mdd_obj,
953 struct linkea_data *ldata,
954 const struct lu_name *lname,
955 const struct lu_fid *pfid)
959 if (ldata->ld_leh == NULL) {
960 rc = mdd_links_read(env, mdd_obj, ldata);
965 rc = linkea_links_find(ldata, lname, pfid);
969 linkea_del_buf(ldata, lname);
973 static int mdd_linkea_prepare(const struct lu_env *env,
974 struct mdd_object *mdd_obj,
975 const struct lu_fid *oldpfid,
976 const struct lu_name *oldlname,
977 const struct lu_fid *newpfid,
978 const struct lu_name *newlname,
979 int first, int check,
980 struct linkea_data *ldata)
986 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
989 LASSERT(oldpfid != NULL || newpfid != NULL);
991 if (mdd_obj->mod_flags & DEAD_OBJ) {
992 /* Prevent linkea to be updated which is NOT necessary. */
993 ldata->ld_reclen = 0;
994 /* No more links, don't bother */
998 if (oldpfid != NULL) {
999 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1002 (rc != -ENODATA && rc != -ENOENT))
1004 /* No changes done. */
1009 /* If renaming, add the new record */
1010 if (newpfid != NULL) {
1011 /* even if the add fails, we still delete the out-of-date
1013 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1017 rc = rc != 0 ? rc : rc2;
1022 int mdd_links_rename(const struct lu_env *env,
1023 struct mdd_object *mdd_obj,
1024 const struct lu_fid *oldpfid,
1025 const struct lu_name *oldlname,
1026 const struct lu_fid *newpfid,
1027 const struct lu_name *newlname,
1028 struct thandle *handle,
1029 struct linkea_data *ldata,
1030 int first, int check)
1036 if (ldata == NULL) {
1037 ldata = &mdd_env_info(env)->mti_link_data;
1038 memset(ldata, 0, sizeof(*ldata));
1039 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1040 newpfid, newlname, first, check,
1046 if (ldata->ld_reclen != 0)
1047 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1054 if (rc == -EOVERFLOW || rc == -ENOSPC)
1056 if (oldpfid == NULL)
1057 CDEBUG(error ? D_ERROR : D_OTHER,
1058 "link_ea add '%.*s' failed %d "DFID"\n",
1059 newlname->ln_namelen, newlname->ln_name,
1060 rc, PFID(mdd_object_fid(mdd_obj)));
1061 else if (newpfid == NULL)
1062 CDEBUG(error ? D_ERROR : D_OTHER,
1063 "link_ea del '%.*s' failed %d "DFID"\n",
1064 oldlname->ln_namelen, oldlname->ln_name,
1065 rc, PFID(mdd_object_fid(mdd_obj)));
1067 CDEBUG(error ? D_ERROR : D_OTHER,
1068 "link_ea rename '%.*s'->'%.*s' failed %d "
1070 oldlname->ln_namelen, oldlname->ln_name,
1071 newlname->ln_namelen, newlname->ln_name,
1072 rc, PFID(mdd_object_fid(mdd_obj)));
1075 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1076 /* if we vmalloced a large buffer drop it */
1077 lu_buf_free(ldata->ld_buf);
1082 static inline int mdd_links_add(const struct lu_env *env,
1083 struct mdd_object *mdd_obj,
1084 const struct lu_fid *pfid,
1085 const struct lu_name *lname,
1086 struct thandle *handle,
1087 struct linkea_data *data, int first)
1089 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1090 pfid, lname, handle, data, first, 0);
1093 static inline int mdd_links_del(const struct lu_env *env,
1094 struct mdd_object *mdd_obj,
1095 const struct lu_fid *pfid,
1096 const struct lu_name *lname,
1097 struct thandle *handle)
1099 return mdd_links_rename(env, mdd_obj, pfid, lname,
1100 NULL, NULL, handle, NULL, 0, 0);
1103 /** Read the link EA into a temp buffer.
1104 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1105 * A pointer to the buffer is stored in \a ldata::ld_buf.
1107 * \retval 0 or error
1109 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1110 struct linkea_data *ldata)
1114 if (!mdd_object_exists(mdd_obj))
1117 /* First try a small buf */
1118 LASSERT(env != NULL);
1119 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1121 if (ldata->ld_buf->lb_buf == NULL)
1124 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1126 if (rc == -ERANGE) {
1127 /* Buf was too small, figure out what we need. */
1128 lu_buf_free(ldata->ld_buf);
1129 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1130 XATTR_NAME_LINK, BYPASS_CAPA);
1133 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1134 if (ldata->ld_buf->lb_buf == NULL)
1136 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1137 XATTR_NAME_LINK, BYPASS_CAPA);
1140 lu_buf_free(ldata->ld_buf);
1141 ldata->ld_buf = NULL;
1145 return linkea_init(ldata);
1148 /** Read the link EA into a temp buffer.
1149 * Uses the name_buf since it is generally large.
1150 * \retval IS_ERR err
1151 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1153 struct lu_buf *mdd_links_get(const struct lu_env *env,
1154 struct mdd_object *mdd_obj)
1156 struct linkea_data ldata = { 0 };
1159 rc = mdd_links_read(env, mdd_obj, &ldata);
1160 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1163 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1164 struct linkea_data *ldata, struct thandle *handle)
1166 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1167 ldata->ld_leh->leh_len);
1168 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1169 mdd_object_capa(env, mdd_obj));
1172 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1173 struct thandle *handle, struct linkea_data *ldata)
1179 if (ldata != NULL && ldata->ld_leh != NULL) {
1180 ea_len = ldata->ld_leh->leh_len;
1181 linkea = ldata->ld_buf->lb_buf;
1183 ea_len = DEFAULT_LINKEA_SIZE;
1187 /* XXX: max size? */
1188 rc = mdo_declare_xattr_set(env, mdd_obj,
1189 mdd_buf_get_const(env, linkea, ea_len),
1190 XATTR_NAME_LINK, 0, handle);
1194 static inline int mdd_declare_links_del(const struct lu_env *env,
1195 struct mdd_object *c,
1196 struct thandle *handle)
1200 /* For directory, the linkEA will be removed together
1201 * with the object. */
1202 if (!S_ISDIR(mdd_object_type(c)))
1203 rc = mdd_declare_links_add(env, c, handle, NULL);
1208 static int mdd_declare_link(const struct lu_env *env,
1209 struct mdd_device *mdd,
1210 struct mdd_object *p,
1211 struct mdd_object *c,
1212 const struct lu_name *name,
1213 struct thandle *handle,
1215 struct linkea_data *data)
1219 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1223 rc = mdo_declare_ref_add(env, c, handle);
1227 la->la_valid = LA_CTIME | LA_MTIME;
1228 rc = mdo_declare_attr_set(env, p, la, handle);
1232 la->la_valid = LA_CTIME;
1233 rc = mdo_declare_attr_set(env, c, la, handle);
1237 rc = mdd_declare_links_add(env, c, handle, data);
1241 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1246 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1247 struct md_object *src_obj, const struct lu_name *lname,
1250 const char *name = lname->ln_name;
1251 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1252 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1253 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1254 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1255 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1256 struct mdd_device *mdd = mdo2mdd(src_obj);
1257 struct thandle *handle;
1258 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1262 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1266 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1270 handle = mdd_trans_create(env, mdd);
1272 GOTO(out_pending, rc = PTR_ERR(handle));
1274 memset(ldata, 0, sizeof(*ldata));
1276 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1277 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1279 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1284 rc = mdd_trans_start(env, mdd, handle);
1288 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1289 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1292 GOTO(out_unlock, rc);
1294 rc = mdo_ref_add(env, mdd_sobj, handle);
1296 GOTO(out_unlock, rc);
1299 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1301 mdd_object_capa(env, mdd_tobj));
1303 mdo_ref_del(env, mdd_sobj, handle);
1304 GOTO(out_unlock, rc);
1307 la->la_valid = LA_CTIME | LA_MTIME;
1308 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1310 GOTO(out_unlock, rc);
1312 la->la_valid = LA_CTIME;
1313 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1315 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1316 mdo2fid(mdd_tobj), lname, 0, 0,
1319 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1320 lname, handle, ldata, 0);
1321 /* The failure of links_add should not cause the link
1322 * failure, reset rc here */
1327 mdd_write_unlock(env, mdd_sobj);
1329 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1330 mdd_tobj, lname, handle);
1332 mdd_trans_stop(env, mdd, rc, handle);
1334 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1335 /* if we vmalloced a large buffer drop it */
1336 lu_buf_free(ldata->ld_buf);
1341 static int mdd_mark_dead_object(const struct lu_env *env,
1342 struct mdd_object *obj, struct thandle *handle,
1345 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1349 obj->mod_flags |= DEAD_OBJ;
1351 if (!S_ISDIR(mdd_object_type(obj)))
1354 attr->la_valid = LA_FLAGS;
1355 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1358 rc = mdo_declare_attr_set(env, obj, attr, handle);
1360 rc = mdo_attr_set(env, obj, attr, handle,
1361 mdd_object_capa(env, obj));
1366 static int mdd_declare_finish_unlink(const struct lu_env *env,
1367 struct mdd_object *obj,
1368 struct thandle *handle)
1372 rc = mdd_mark_dead_object(env, obj, handle, true);
1376 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1380 rc = mdo_declare_destroy(env, obj, handle);
1384 return mdd_declare_links_del(env, obj, handle);
1387 /* caller should take a lock before calling */
1388 int mdd_finish_unlink(const struct lu_env *env,
1389 struct mdd_object *obj, struct md_attr *ma,
1390 const struct mdd_object *pobj,
1391 const struct lu_name *lname,
1395 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1398 LASSERT(mdd_write_locked(env, obj) != 0);
1400 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1401 rc = mdd_mark_dead_object(env, obj, th, false);
1405 /* add new orphan and the object
1406 * will be deleted during mdd_close() */
1407 if (obj->mod_count) {
1408 rc = __mdd_orphan_add(env, obj, th);
1410 CDEBUG(D_HA, "Object "DFID" is inserted into "
1411 "orphan list, open count = %d\n",
1412 PFID(mdd_object_fid(obj)),
1415 CERROR("Object "DFID" fail to be an orphan, "
1416 "open count = %d, maybe cause failed "
1418 PFID(mdd_object_fid(obj)),
1421 rc = mdo_destroy(env, obj, th);
1423 } else if (!is_dir) {
1424 /* old files may not have link ea; ignore errors */
1425 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1433 * has mdd_write_lock on cobj already, but not on pobj yet
1435 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1436 const struct lu_attr *pattr,
1437 struct mdd_object *cobj,
1438 const struct lu_attr *cattr)
1443 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1448 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1449 struct mdd_object *p, struct mdd_object *c,
1450 const struct lu_name *name, struct md_attr *ma,
1451 struct thandle *handle, int no_name)
1453 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1456 if (likely(no_name == 0)) {
1457 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1462 rc = mdo_declare_ref_del(env, p, handle);
1466 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1467 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1468 la->la_valid = LA_CTIME | LA_MTIME;
1469 rc = mdo_declare_attr_set(env, p, la, handle);
1474 rc = mdo_declare_ref_del(env, c, handle);
1478 rc = mdo_declare_ref_del(env, c, handle);
1482 la->la_valid = LA_CTIME;
1483 rc = mdo_declare_attr_set(env, c, la, handle);
1487 rc = mdd_declare_finish_unlink(env, c, handle);
1491 /* FIXME: need changelog for remove entry */
1492 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1499 * test if a file has an HSM archive
1500 * if HSM attributes are not found in ma update them from
1503 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1504 struct mdd_object *obj,
1509 if (!(ma->ma_valid & MA_HSM)) {
1510 /* no HSM MD provided, read xattr */
1511 struct lu_buf *hsm_buf;
1512 const size_t buflen = sizeof(struct hsm_attrs);
1515 hsm_buf = mdd_buf_get(env, NULL, 0);
1516 lu_buf_alloc(hsm_buf, buflen);
1517 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1518 mdd_object_capa(env, obj));
1519 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1520 lu_buf_free(hsm_buf);
1524 ma->ma_valid = MA_HSM;
1526 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1532 * Delete name entry and the object.
1533 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1534 * does not exist for this object, and it could only happen during resending
1535 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1536 * is also needed in this case(needed by changelog), so we have to add another
1537 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1538 * the ENOENT failure should be able to be fixed by redo mechanism.
1540 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1541 struct md_object *cobj, const struct lu_name *lname,
1542 struct md_attr *ma, int no_name)
1544 const char *name = lname->ln_name;
1545 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1546 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1547 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1548 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1549 struct mdd_object *mdd_cobj = NULL;
1550 struct mdd_device *mdd = mdo2mdd(pobj);
1551 struct thandle *handle;
1555 /* cobj == NULL means only delete name entry */
1556 if (likely(cobj != NULL)) {
1557 mdd_cobj = md2mdd_obj(cobj);
1558 if (mdd_object_exists(mdd_cobj) == 0)
1562 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1566 if (likely(mdd_cobj != NULL)) {
1568 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1572 is_dir = S_ISDIR(cattr->la_mode);
1575 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1579 handle = mdd_trans_create(env, mdd);
1581 RETURN(PTR_ERR(handle));
1583 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1584 lname, ma, handle, no_name);
1588 rc = mdd_trans_start(env, mdd, handle);
1592 if (likely(mdd_cobj != NULL))
1593 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1595 if (likely(no_name == 0)) {
1596 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1597 mdd_object_capa(env, mdd_pobj));
1602 if (likely(mdd_cobj != NULL)) {
1603 rc = mdo_ref_del(env, mdd_cobj, handle);
1605 __mdd_index_insert_only(env, mdd_pobj,
1608 mdd_object_capa(env, mdd_pobj));
1614 mdo_ref_del(env, mdd_cobj, handle);
1616 /* fetch updated nlink */
1617 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1622 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1623 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1625 la->la_valid = LA_CTIME | LA_MTIME;
1626 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1630 /* Enough for only unlink the entry */
1631 if (unlikely(mdd_cobj == NULL))
1634 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1635 /* update ctime of an unlinked file only if it is still
1636 * opened or a link still exists */
1637 la->la_valid = LA_CTIME;
1638 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1643 /* XXX: this transfer to ma will be removed with LOD/OSP */
1644 ma->ma_attr = *cattr;
1645 ma->ma_valid |= MA_INODE;
1646 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1648 /* fetch updated nlink */
1650 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1652 /* if object is removed then we can't get its attrs, use last get */
1653 if (cattr->la_nlink == 0) {
1654 ma->ma_attr = *cattr;
1655 ma->ma_valid |= MA_INODE;
1659 mdd_write_unlock(env, mdd_cobj);
1663 if (cattr->la_nlink == 0) {
1664 cl_flags |= CLF_UNLINK_LAST;
1665 /* search for an existing archive */
1666 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1667 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1670 rc = mdd_changelog_ns_store(env, mdd,
1671 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1672 mdd_cobj, mdd_pobj, lname, handle);
1676 mdd_trans_stop(env, mdd, rc, handle);
1682 * The permission has been checked when obj created, no need check again.
1684 static int mdd_cd_sanity_check(const struct lu_env *env,
1685 struct mdd_object *obj)
1690 if (!obj || mdd_is_dead_obj(obj))
1696 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1697 struct md_object *cobj, const struct md_op_spec *spec,
1700 struct mdd_device *mdd = mdo2mdd(cobj);
1701 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1702 struct mdd_object *son = md2mdd_obj(cobj);
1703 struct thandle *handle;
1704 const struct lu_buf *buf;
1705 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1706 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1710 rc = mdd_cd_sanity_check(env, son);
1714 if (!md_should_create(spec->sp_cr_flags))
1718 * there are following use cases for this function:
1719 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1720 * striping can be specified or not
1723 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1727 /* calling ->ah_make_hint() is used to transfer information from parent */
1728 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1730 handle = mdd_trans_create(env, mdd);
1732 GOTO(out_free, rc = PTR_ERR(handle));
1735 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1736 * Should this be fixed?
1738 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1739 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1740 spec->sp_cr_flags, spec->no_create);
1742 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1743 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1744 spec->u.sp_ea.eadatalen);
1749 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1750 XATTR_NAME_LOV, 0, handle);
1754 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1758 rc = mdd_trans_start(env, mdd, handle);
1762 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1763 0, handle, mdd_object_capa(env, son));
1768 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1771 mdd_trans_stop(env, mdd, rc, handle);
1776 static int mdd_declare_object_initialize(const struct lu_env *env,
1777 struct mdd_object *parent,
1778 struct mdd_object *child,
1779 struct lu_attr *attr,
1780 struct thandle *handle)
1786 * inode mode has been set in creation time, and it's based on umask,
1787 * la_mode and acl, don't set here again! (which will go wrong
1788 * because below function doesn't consider umask).
1789 * I'd suggest set all object attributes in creation time, see above.
1791 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1792 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1793 rc = mdo_declare_attr_set(env, child, attr, handle);
1794 attr->la_valid |= LA_MODE | LA_TYPE;
1795 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1796 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1799 rc = mdo_declare_ref_add(env, child, handle);
1801 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1808 static int mdd_object_initialize(const struct lu_env *env,
1809 const struct lu_fid *pfid,
1810 struct mdd_object *child,
1811 struct lu_attr *attr, struct thandle *handle,
1812 const struct md_op_spec *spec)
1818 * Update attributes for child.
1821 * (1) the valid bits should be converted between Lustre and Linux;
1822 * (2) maybe, the child attributes should be set in OSD when creation.
1825 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1826 /* arguments are supposed to stay the same */
1827 if (S_ISDIR(attr->la_mode)) {
1828 /* Add "." and ".." for newly created dir */
1829 mdo_ref_add(env, child, handle);
1830 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1831 dot, handle, BYPASS_CAPA);
1833 rc = __mdd_index_insert_only(env, child, pfid,
1837 mdo_ref_del(env, child, handle);
1843 /* has not lock on pobj yet */
1844 static int mdd_create_sanity_check(const struct lu_env *env,
1845 struct md_object *pobj,
1846 const struct lu_attr *pattr,
1847 const struct lu_name *lname,
1848 struct lu_attr *cattr,
1849 struct md_op_spec *spec)
1851 struct mdd_thread_info *info = mdd_env_info(env);
1852 struct lu_fid *fid = &info->mti_fid;
1853 struct mdd_object *obj = md2mdd_obj(pobj);
1854 struct mdd_device *m = mdo2mdd(pobj);
1859 if (mdd_is_dead_obj(obj))
1863 * In some cases this lookup is not needed - we know before if name
1864 * exists or not because MDT performs lookup for it.
1865 * name length check is done in lookup.
1867 if (spec->sp_cr_lookup) {
1869 * Check if the name already exist, though it will be checked in
1870 * _index_insert also, for avoiding rolling back if exists
1873 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1874 MAY_WRITE | MAY_EXEC);
1876 RETURN(rc ? : -EEXIST);
1878 rc = mdd_may_create(env, obj, pattr, NULL, true, false);
1884 if (pattr->la_mode & S_ISGID) {
1885 cattr->la_gid = pattr->la_gid;
1886 if (S_ISDIR(cattr->la_mode)) {
1887 cattr->la_mode |= S_ISGID;
1888 cattr->la_valid |= LA_MODE;
1892 rc = mdd_name_check(m, lname);
1896 switch (cattr->la_mode & S_IFMT) {
1898 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1900 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1901 RETURN(-ENAMETOOLONG);
1920 static int mdd_declare_object_create(const struct lu_env *env,
1921 struct mdd_device *mdd,
1922 struct mdd_object *p, struct mdd_object *c,
1923 struct lu_attr *attr,
1924 struct thandle *handle,
1925 const struct md_op_spec *spec,
1926 struct lu_buf *def_acl_buf,
1927 struct lu_buf *acl_buf,
1928 struct dt_allocation_hint *hint)
1932 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1937 #ifdef CONFIG_FS_POSIX_ACL
1938 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1939 /* if dir, then can inherit default ACl */
1940 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1941 XATTR_NAME_ACL_DEFAULT,
1947 if (acl_buf->lb_len > 0) {
1948 rc = mdo_declare_attr_set(env, c, attr, handle);
1952 rc = mdo_declare_xattr_set(env, c, acl_buf,
1953 XATTR_NAME_ACL_ACCESS, 0, handle);
1958 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1962 /* replay case, create LOV EA from client data */
1963 if (spec->no_create ||
1964 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
1965 const struct lu_buf *buf;
1967 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1968 spec->u.sp_ea.eadatalen);
1969 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
1975 if (S_ISLNK(attr->la_mode)) {
1976 const char *target_name = spec->u.sp_symname;
1977 int sym_len = strlen(target_name);
1978 const struct lu_buf *buf;
1980 buf = mdd_buf_get_const(env, target_name, sym_len);
1981 rc = dt_declare_record_write(env, mdd_object_child(c),
1990 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1991 struct mdd_object *p, struct mdd_object *c,
1992 const struct lu_name *name,
1993 struct lu_attr *attr,
1994 struct thandle *handle,
1995 const struct md_op_spec *spec,
1996 struct linkea_data *ldata,
1997 struct lu_buf *def_acl_buf,
1998 struct lu_buf *acl_buf,
1999 struct dt_allocation_hint *hint)
2003 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2004 def_acl_buf, acl_buf, hint);
2008 if (S_ISDIR(attr->la_mode)) {
2009 rc = mdo_declare_ref_add(env, p, handle);
2014 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE) {
2015 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2019 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2021 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,
2025 rc = mdd_declare_links_add(env, c, handle, ldata);
2030 la->la_valid = LA_CTIME | LA_MTIME;
2031 rc = mdo_declare_attr_set(env, p, la, handle);
2036 rc = mdd_declare_changelog_store(env, mdd, name, handle);
2040 /* XXX: For remote create, it should indicate the remote RPC
2041 * will be sent after local transaction is finished, which
2042 * is not very nice, but it will be removed once we fully support
2044 if (mdd_object_remote(p) && handle->th_update != NULL)
2045 handle->th_update->tu_sent_after_local_trans = 1;
2050 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2051 struct lu_attr *la, struct lu_buf *def_acl_buf,
2052 struct lu_buf *acl_buf)
2057 if (S_ISLNK(la->la_mode)) {
2058 acl_buf->lb_len = 0;
2059 def_acl_buf->lb_len = 0;
2063 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2064 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2065 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2066 mdd_read_unlock(env, pobj);
2068 /* If there are default ACL, fix mode/ACL by default ACL */
2069 def_acl_buf->lb_len = rc;
2070 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2071 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2072 acl_buf->lb_len = rc;
2073 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2076 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2077 /* If there are no default ACL, fix mode by mask */
2078 struct lu_ucred *uc = lu_ucred(env);
2080 /* The create triggered by MDT internal events, such as
2081 * LFSCK reset, will not contain valid "uc". */
2082 if (unlikely(uc != NULL))
2083 la->la_mode &= ~uc->uc_umask;
2085 acl_buf->lb_len = 0;
2086 def_acl_buf->lb_len = 0;
2093 * Create a metadata object and initialize it, set acl, xattr.
2095 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2096 struct mdd_object *son, struct lu_attr *attr,
2097 struct md_op_spec *spec, struct lu_buf *acl_buf,
2098 struct lu_buf *def_acl_buf,
2099 struct dt_allocation_hint *hint,
2100 struct thandle *handle)
2104 mdd_write_lock(env, son, MOR_TGT_CHILD);
2105 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2110 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2111 * created in declare phase, they also needs to be added to master
2112 * object as sub-directory entry. So it has to initialize the master
2113 * object, then set dir striped EA.(in mdo_xattr_set) */
2114 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2117 GOTO(err_destroy, rc);
2120 * in case of replay we just set LOVEA provided by the client
2121 * XXX: I think it would be interesting to try "old" way where
2122 * MDT calls this xattr_set(LOV) in a different transaction.
2123 * probably this way we code can be made better.
2126 /* During creation, there are only a few cases we need do xattr_set to
2128 * 1. regular file: see comments above.
2129 * 2. create striped directory with provided stripeEA.
2130 * 3. create striped directory because inherit default layout from the
2132 if (spec->no_create ||
2133 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2134 S_ISDIR(attr->la_mode)) {
2135 const struct lu_buf *buf;
2137 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2138 spec->u.sp_ea.eadatalen);
2139 rc = mdo_xattr_set(env, son, buf,
2140 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2142 handle, BYPASS_CAPA);
2144 GOTO(err_destroy, rc);
2147 #ifdef CONFIG_FS_POSIX_ACL
2148 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2149 S_ISDIR(attr->la_mode)) {
2150 /* set default acl */
2151 rc = mdo_xattr_set(env, son, def_acl_buf,
2152 XATTR_NAME_ACL_DEFAULT, 0,
2153 handle, BYPASS_CAPA);
2155 GOTO(err_destroy, rc);
2157 /* set its own acl */
2158 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2159 rc = mdo_xattr_set(env, son, acl_buf,
2160 XATTR_NAME_ACL_ACCESS,
2161 0, handle, BYPASS_CAPA);
2163 GOTO(err_destroy, rc);
2167 if (S_ISLNK(attr->la_mode)) {
2168 struct lu_ucred *uc = lu_ucred_assert(env);
2169 struct dt_object *dt = mdd_object_child(son);
2170 const char *target_name = spec->u.sp_symname;
2171 int sym_len = strlen(target_name);
2172 const struct lu_buf *buf;
2175 buf = mdd_buf_get_const(env, target_name, sym_len);
2176 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2177 mdd_object_capa(env, son),
2179 CFS_CAP_SYS_RESOURCE_MASK);
2184 GOTO(err_initlized, rc = -EFAULT);
2188 if (unlikely(rc != 0)) {
2190 if (S_ISDIR(attr->la_mode)) {
2191 /* Drop the reference, no need to delete "."/"..",
2192 * because the object to be destroied directly. */
2193 rc2 = mdo_ref_del(env, son, handle);
2197 rc2 = mdo_ref_del(env, son, handle);
2201 mdo_destroy(env, son, handle);
2204 mdd_write_unlock(env, son);
2209 * Create object and insert it into namespace.
2211 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2212 const struct lu_name *lname, struct md_object *child,
2213 struct md_op_spec *spec, struct md_attr* ma)
2215 struct mdd_thread_info *info = mdd_env_info(env);
2216 struct lu_attr *la = &info->mti_la_for_fix;
2217 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2218 struct mdd_object *son = md2mdd_obj(child);
2219 struct mdd_device *mdd = mdo2mdd(pobj);
2220 struct lu_attr *attr = &ma->ma_attr;
2221 struct thandle *handle;
2222 struct lu_attr *pattr = &info->mti_pattr;
2223 struct lu_buf acl_buf;
2224 struct lu_buf def_acl_buf;
2225 struct linkea_data *ldata = &info->mti_link_data;
2226 const char *name = lname->ln_name;
2227 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2233 * Two operations have to be performed:
2235 * - an allocation of a new object (->do_create()), and
2237 * - an insertion into a parent index (->dio_insert()).
2239 * Due to locking, operation order is not important, when both are
2240 * successful, *but* error handling cases are quite different:
2242 * - if insertion is done first, and following object creation fails,
2243 * insertion has to be rolled back, but this operation might fail
2244 * also leaving us with dangling index entry.
2246 * - if creation is done first, is has to be undone if insertion
2247 * fails, leaving us with leaked space, which is neither good, nor
2250 * It seems that creation-first is simplest solution, but it is
2251 * sub-optimal in the frequent
2256 * case, because second mkdir is bound to create object, only to
2257 * destroy it immediately.
2259 * To avoid this follow local file systems that do double lookup:
2261 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2263 * 1. create (mdd_object_create_internal())
2265 * 2. insert (__mdd_index_insert(), lookup again)
2268 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2272 /* Sanity checks before big job. */
2273 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2277 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2278 GOTO(out_free, rc = -EINPROGRESS);
2280 handle = mdd_trans_create(env, mdd);
2282 GOTO(out_free, rc = PTR_ERR(handle));
2284 acl_buf.lb_buf = info->mti_xattr_buf;
2285 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2286 def_acl_buf.lb_buf = info->mti_key;
2287 def_acl_buf.lb_len = sizeof(info->mti_key);
2288 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2292 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2294 memset(ldata, 0, sizeof(*ldata));
2295 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2296 mdd_object_fid(mdd_pobj),
2297 lname, 1, 0, ldata);
2299 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2300 handle, spec, ldata, &def_acl_buf, &acl_buf,
2305 rc = mdd_trans_start(env, mdd, handle);
2309 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2310 &def_acl_buf, hint, handle);
2314 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2315 mdd_write_lock(env, son, MOR_TGT_CHILD);
2316 rc = __mdd_orphan_add(env, son, handle);
2317 mdd_write_unlock(env, son);
2319 GOTO(err_created, rc);
2321 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2322 name, S_ISDIR(attr->la_mode), handle,
2323 mdd_object_capa(env, mdd_pobj));
2325 GOTO(err_created, rc);
2327 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2330 /* update parent directory mtime/ctime */
2332 la->la_valid = LA_CTIME | LA_MTIME;
2333 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2335 GOTO(err_insert, rc);
2343 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2344 rc2 = __mdd_orphan_del(env, son, handle);
2346 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2347 S_ISDIR(attr->la_mode),
2348 handle, BYPASS_CAPA);
2353 mdd_write_lock(env, son, MOR_TGT_CHILD);
2354 if (S_ISDIR(attr->la_mode)) {
2355 /* Drop the reference, no need to delete "."/"..",
2356 * because the object to be destroied directly. */
2357 rc2 = mdo_ref_del(env, son, handle);
2359 mdd_write_unlock(env, son);
2363 rc2 = mdo_ref_del(env, son, handle);
2365 mdd_write_unlock(env, son);
2369 mdo_destroy(env, son, handle);
2370 mdd_write_unlock(env, son);
2373 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2374 rc = mdd_changelog_ns_store(env, mdd,
2375 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2376 S_ISREG(attr->la_mode) ? CL_CREATE :
2377 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2378 0, son, mdd_pobj, lname, handle);
2380 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2384 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2385 /* if we vmalloced a large buffer drop it */
2386 lu_buf_free(ldata->ld_buf);
2388 /* The child object shouldn't be cached anymore */
2390 set_bit(LU_OBJECT_HEARD_BANSHEE,
2391 &child->mo_lu.lo_header->loh_flags);
2396 * Get locks on parents in proper order
2397 * RETURN: < 0 - error, rename_order if successful
2405 static int mdd_rename_order(const struct lu_env *env,
2406 struct mdd_device *mdd,
2407 struct mdd_object *src_pobj,
2408 const struct lu_attr *pattr,
2409 struct mdd_object *tgt_pobj)
2411 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2415 if (src_pobj == tgt_pobj)
2416 RETURN(MDD_RN_SAME);
2418 /* compared the parent child relationship of src_p&tgt_p */
2419 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2421 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2424 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2438 /* has not mdd_write{read}_lock on any obj yet. */
2439 static int mdd_rename_sanity_check(const struct lu_env *env,
2440 struct mdd_object *src_pobj,
2441 const struct lu_attr *pattr,
2442 struct mdd_object *tgt_pobj,
2443 const struct lu_attr *tpattr,
2444 struct mdd_object *sobj,
2445 const struct lu_attr *cattr,
2446 struct mdd_object *tobj,
2447 const struct lu_attr *tattr)
2452 /* XXX: when get here, sobj must NOT be NULL,
2453 * the other case has been processed in cld_rename
2454 * before mdd_rename and enable MDS_PERM_BYPASS. */
2457 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2461 /* XXX: when get here, "tobj == NULL" means tobj must
2462 * NOT exist (neither on remote MDS, such case has been
2463 * processed in cld_rename before mdd_rename and enable
2465 * So check may_create, but not check may_unlink. */
2467 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2468 (src_pobj != tgt_pobj), false);
2470 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2471 (src_pobj != tgt_pobj), 1);
2473 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2474 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2479 static int mdd_declare_rename(const struct lu_env *env,
2480 struct mdd_device *mdd,
2481 struct mdd_object *mdd_spobj,
2482 struct mdd_object *mdd_tpobj,
2483 struct mdd_object *mdd_sobj,
2484 struct mdd_object *mdd_tobj,
2485 const struct lu_name *tname,
2486 const struct lu_name *sname,
2488 struct linkea_data *ldata,
2489 struct thandle *handle)
2491 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2494 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2495 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2501 /* name from source dir */
2502 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2506 /* .. from source child */
2507 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2508 /* source child can be directory,
2509 * counted by source dir's nlink */
2510 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2513 if (mdd_spobj != mdd_tpobj) {
2514 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2519 rc = mdo_declare_index_insert(env, mdd_sobj,
2525 /* new target child can be directory,
2526 * counted by target dir's nlink */
2527 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2532 la->la_valid = LA_CTIME | LA_MTIME;
2533 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2537 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2541 la->la_valid = LA_CTIME;
2542 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2546 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2551 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2552 tname->ln_name, handle);
2556 /* name from target dir (old name), we declare it unconditionally
2557 * as mdd_rename() calls delete unconditionally as well. so just
2558 * to balance declarations vs calls to change ... */
2559 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2563 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2564 /* delete target child in target parent directory */
2565 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2569 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2570 /* target child can be directory,
2571 * delete "." reference in target child directory */
2572 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2576 /* delete ".." reference in target parent directory */
2577 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2582 la->la_valid = LA_CTIME;
2583 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2587 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2592 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2599 /* src object can be remote that is why we use only fid and type of object */
2600 static int mdd_rename(const struct lu_env *env,
2601 struct md_object *src_pobj, struct md_object *tgt_pobj,
2602 const struct lu_fid *lf, const struct lu_name *lsname,
2603 struct md_object *tobj, const struct lu_name *ltname,
2606 const char *sname = lsname->ln_name;
2607 const char *tname = ltname->ln_name;
2608 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2609 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2610 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2611 struct mdd_device *mdd = mdo2mdd(src_pobj);
2612 struct mdd_object *mdd_sobj = NULL; /* source object */
2613 struct mdd_object *mdd_tobj = NULL;
2614 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2615 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2616 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2617 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2618 struct thandle *handle;
2619 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2620 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2621 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2624 bool tobj_locked = 0;
2625 unsigned cl_flags = 0;
2630 mdd_tobj = md2mdd_obj(tobj);
2632 mdd_sobj = mdd_object_find(env, mdd, lf);
2634 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2636 GOTO(out_pending, rc);
2638 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2640 GOTO(out_pending, rc);
2643 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2645 GOTO(out_pending, rc);
2648 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2650 GOTO(out_pending, rc);
2652 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2653 mdd_sobj, cattr, mdd_tobj, tattr);
2655 GOTO(out_pending, rc);
2657 rc = mdd_name_check(mdd, ltname);
2659 GOTO(out_pending, rc);
2661 handle = mdd_trans_create(env, mdd);
2663 GOTO(out_pending, rc = PTR_ERR(handle));
2665 memset(ldata, 0, sizeof(*ldata));
2666 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2667 ltname, 1, 0, ldata);
2668 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2669 mdd_tobj, lsname, ltname, ma, ldata, handle);
2673 rc = mdd_trans_start(env, mdd, handle);
2677 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2678 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2680 GOTO(cleanup_unlocked, rc);
2682 is_dir = S_ISDIR(cattr->la_mode);
2684 /* Remove source name from source directory */
2685 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2686 mdd_object_capa(env, mdd_spobj));
2690 /* "mv dir1 dir2" needs "dir1/.." link update */
2691 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2692 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2693 mdd_object_capa(env, mdd_sobj));
2695 GOTO(fixup_spobj2, rc);
2697 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2698 handle, mdd_object_capa(env, mdd_sobj));
2700 GOTO(fixup_spobj, rc);
2703 /* Remove target name from target directory
2704 * Here tobj can be remote one, so we do index_delete unconditionally
2705 * and -ENOENT is allowed.
2707 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2708 mdd_object_capa(env, mdd_tpobj));
2711 /* tname might been renamed to something else */
2712 GOTO(fixup_spobj, rc);
2715 GOTO(fixup_spobj, rc);
2718 /* Insert new fid with target name into target dir */
2719 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2720 mdd_object_capa(env, mdd_tpobj));
2722 GOTO(fixup_tpobj, rc);
2724 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2725 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2727 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2729 la->la_valid = LA_CTIME;
2730 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2732 GOTO(fixup_tpobj, rc);
2735 /* Remove old target object
2736 * For tobj is remote case cmm layer has processed
2737 * and set tobj to NULL then. So when tobj is NOT NULL,
2738 * it must be local one.
2740 if (tobj && mdd_object_exists(mdd_tobj)) {
2741 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2743 if (mdd_is_dead_obj(mdd_tobj)) {
2744 /* shld not be dead, something is wrong */
2745 CERROR("tobj is dead, something is wrong\n");
2749 mdo_ref_del(env, mdd_tobj, handle);
2751 /* Remove dot reference. */
2752 if (S_ISDIR(tattr->la_mode))
2753 mdo_ref_del(env, mdd_tobj, handle);
2756 /* fetch updated nlink */
2757 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2759 CERROR("%s: Failed to get nlink for tobj "
2761 mdd2obd_dev(mdd)->obd_name,
2762 PFID(tpobj_fid), rc);
2763 GOTO(fixup_tpobj, rc);
2766 la->la_valid = LA_CTIME;
2767 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2769 CERROR("%s: Failed to set ctime for tobj "
2771 mdd2obd_dev(mdd)->obd_name,
2772 PFID(tpobj_fid), rc);
2773 GOTO(fixup_tpobj, rc);
2776 /* XXX: this transfer to ma will be removed with LOD/OSP */
2777 ma->ma_attr = *tattr;
2778 ma->ma_valid |= MA_INODE;
2779 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2782 CERROR("%s: Failed to unlink tobj "
2784 mdd2obd_dev(mdd)->obd_name,
2785 PFID(tpobj_fid), rc);
2786 GOTO(fixup_tpobj, rc);
2789 /* fetch updated nlink */
2790 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2792 CERROR("%s: Failed to get nlink for tobj "
2794 mdd2obd_dev(mdd)->obd_name,
2795 PFID(tpobj_fid), rc);
2796 GOTO(fixup_tpobj, rc);
2798 /* XXX: this transfer to ma will be removed with LOD/OSP */
2799 ma->ma_attr = *tattr;
2800 ma->ma_valid |= MA_INODE;
2802 if (tattr->la_nlink == 0) {
2803 cl_flags |= CLF_RENAME_LAST;
2804 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2805 cl_flags |= CLF_RENAME_LAST_EXISTS;
2809 la->la_valid = LA_CTIME | LA_MTIME;
2810 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2812 GOTO(fixup_tpobj, rc);
2814 if (mdd_spobj != mdd_tpobj) {
2815 la->la_valid = LA_CTIME | LA_MTIME;
2816 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2819 if (rc == 0 && mdd_sobj) {
2820 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2821 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2822 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2825 /* Old files might not have EA entry */
2826 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2827 lsname, handle, NULL, 0);
2828 mdd_write_unlock(env, mdd_sobj);
2829 /* We don't fail the transaction if the link ea can't be
2830 updated -- fid2path will use alternate lookup method. */
2838 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2841 CWARN("tp obj fix error %d\n",rc2);
2843 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2844 !mdd_is_dead_obj(mdd_tobj)) {
2846 mdo_ref_add(env, mdd_tobj, handle);
2848 mdo_ref_add(env, mdd_tobj, handle);
2851 rc2 = __mdd_index_insert(env, mdd_tpobj,
2852 mdo2fid(mdd_tobj), tname,
2857 CWARN("tp obj fix error %d\n",rc2);
2862 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2863 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2867 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2868 mdd2obd_dev(mdd)->obd_name, rc2);
2871 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2872 dotdot, handle, BYPASS_CAPA);
2874 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2875 mdd2obd_dev(mdd)->obd_name, rc2);
2880 rc2 = __mdd_index_insert(env, mdd_spobj,
2881 lf, sname, is_dir, handle, BYPASS_CAPA);
2883 CWARN("sp obj fix error %d\n",rc2);
2887 mdd_write_unlock(env, mdd_tobj);
2890 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2891 mdd_tobj, tpobj_fid, lf,
2892 spobj_fid, ltname, lsname,
2896 mdd_trans_stop(env, mdd, rc, handle);
2898 mdd_object_put(env, mdd_sobj);
2903 * During migration once the parent FID has been changed,
2904 * we need update the parent FID in linkea.
2906 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2907 struct mdd_object *parent,
2908 struct mdd_object *child,
2909 const char *name, int namelen,
2910 struct thandle *handle,
2913 struct mdd_thread_info *info = mdd_env_info(env);
2914 struct linkea_data ldata = {0};
2915 struct lu_buf *buf = &info->mti_link_buf;
2921 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2922 if (buf->lb_buf == NULL)
2926 rc = mdd_links_read(env, child, &ldata);
2928 if (rc == -ENOENT || rc == -ENODATA)
2933 LASSERT(ldata.ld_leh != NULL);
2934 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2935 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2936 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2937 struct lu_name lname;
2940 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2943 if (strncmp(lname.ln_name, name, namelen) != 0 ||
2944 lu_fid_eq(&fid, mdd_object_fid(parent))) {
2945 ldata.ld_lee = (struct link_ea_entry *)
2946 ((char *)ldata.ld_lee +
2951 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
2952 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
2953 lname.ln_namelen, lname.ln_name,
2954 PFID(mdd_object_fid(parent)));
2955 /* update to the new parent fid */
2956 linkea_entry_pack(ldata.ld_lee, &lname,
2957 mdd_object_fid(parent));
2959 rc = mdd_declare_links_add(env, child, handle, &ldata);
2961 rc = mdd_links_write(env, child, &ldata, handle);
2967 static int mdd_linkea_declare_update_child(const struct lu_env *env,
2968 struct mdd_object *parent,
2969 struct mdd_object *child,
2970 const char *name, int namelen,
2971 struct thandle *handle)
2973 return mdd_linkea_update_child_internal(env, parent, child, name,
2974 namelen, handle, true);
2977 static int mdd_linkea_update_child(const struct lu_env *env,
2978 struct mdd_object *parent,
2979 struct mdd_object *child,
2980 const char *name, int namelen,
2981 struct thandle *handle)
2983 return mdd_linkea_update_child_internal(env, parent, child, name,
2984 namelen, handle, false);
2987 static int mdd_update_linkea_internal(const struct lu_env *env,
2988 struct mdd_object *mdd_pobj,
2989 struct mdd_object *mdd_sobj,
2990 struct mdd_object *mdd_tobj,
2991 const struct lu_name *child_name,
2992 struct thandle *handle,
2995 struct mdd_thread_info *info = mdd_env_info(env);
2996 struct linkea_data *ldata = &info->mti_link_data;
3001 rc = mdd_links_read(env, mdd_sobj, ldata);
3003 if (rc == -ENOENT || rc == -ENODATA)
3009 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3011 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3016 /* If it is mulitple links file, we need update the name entry for
3018 LASSERT(ldata->ld_leh != NULL);
3019 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3020 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3021 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3022 struct mdd_object *pobj;
3023 struct lu_name lname;
3026 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3028 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3030 pobj = mdd_object_find(env, mdd, &fid);
3032 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3033 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3038 if (!mdd_object_exists(pobj)) {
3039 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3040 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3044 if (pobj == mdd_pobj &&
3045 lname.ln_namelen == child_name->ln_namelen &&
3046 strncmp(lname.ln_name, child_name->ln_name,
3047 lname.ln_namelen) == 0) {
3048 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3049 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3054 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3055 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3056 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3059 /* Remove source name from source directory */
3060 /* Insert new fid with target name into target dir */
3061 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3066 rc = mdo_declare_index_insert(env, pobj,
3067 mdd_object_fid(mdd_tobj),
3068 lname.ln_name, handle);
3072 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3076 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3080 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3082 mdd_object_capa(env, pobj));
3086 rc = __mdd_index_insert(env, pobj,
3087 mdd_object_fid(mdd_tobj),
3088 lname.ln_name, 0, handle,
3089 mdd_object_capa(env, pobj));
3093 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3094 rc = mdo_ref_add(env, mdd_tobj, handle);
3095 mdd_write_unlock(env, mdd_tobj);
3099 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3100 mdo_ref_del(env, mdd_sobj, handle);
3101 mdd_write_unlock(env, mdd_sobj);
3104 mdd_object_put(env, pobj);
3112 static int mdd_migrate_xattrs(const struct lu_env *env,
3113 struct mdd_object *mdd_sobj,
3114 struct mdd_object *mdd_tobj)
3116 struct mdd_thread_info *info = mdd_env_info(env);
3117 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3119 struct thandle *handle;
3125 struct lu_buf list_xbuf;
3128 /* retrieve xattr list from the old object */
3129 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3130 mdd_object_capa(env, mdd_sobj));
3131 if (list_xsize == -ENODATA)
3137 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3138 if (info->mti_big_buf.lb_buf == NULL)
3141 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3142 list_xbuf.lb_len = list_xsize;
3143 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3144 mdd_object_capa(env, mdd_sobj));
3149 xname = list_xbuf.lb_buf;
3151 xlen = strnlen(xname, rem - 1) + 1;
3152 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3153 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3154 strcmp(XATTR_NAME_LMV, xname) == 0)
3157 /* For directory, if there are default layout, migrate here */
3158 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3159 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3162 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3164 mdd_object_capa(env, mdd_sobj));
3165 if (xsize == -ENODATA)
3170 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3171 if (info->mti_link_buf.lb_buf == NULL)
3172 GOTO(out, rc = -ENOMEM);
3174 xbuf.lb_len = xsize;
3175 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3176 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3177 mdd_object_capa(env, mdd_sobj));
3183 handle = mdd_trans_create(env, mdd);
3185 GOTO(out, rc = PTR_ERR(handle));
3187 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3190 GOTO(stop_trans, rc);
3191 /* Note: this transaction is part of migration, and it is not
3192 * the last step of migration, so we set th_local = 1 to avoid
3193 * update last rcvd for this transaction */
3194 handle->th_local = 1;
3195 rc = mdd_trans_start(env, mdd, handle);
3197 GOTO(stop_trans, rc);
3199 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3200 mdd_object_capa(env, mdd_sobj));
3202 GOTO(stop_trans, rc = 0);
3205 GOTO(stop_trans, rc);
3207 mdd_trans_stop(env, mdd, rc, handle);
3212 memmove(xname, xname + xlen, rem);
3218 static int mdd_declare_migrate_create(const struct lu_env *env,
3219 struct mdd_object *mdd_pobj,
3220 struct mdd_object *mdd_sobj,
3221 struct mdd_object *mdd_tobj,
3222 struct md_op_spec *spec,
3224 union lmv_mds_md *mgr_ea,
3225 struct thandle *handle)
3227 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3228 const struct lu_buf *buf;
3232 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3233 handle, spec, NULL);
3237 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3242 if (S_ISLNK(la->la_mode)) {
3243 const char *target_name = spec->u.sp_symname;
3244 int sym_len = strlen(target_name);
3245 const struct lu_buf *buf;
3247 buf = mdd_buf_get_const(env, target_name, sym_len);
3248 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3254 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3255 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3256 spec->u.sp_ea.eadatalen);
3257 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3263 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3264 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3265 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3270 la_flag->la_valid = LA_FLAGS;
3271 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3272 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3273 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3278 static int mdd_migrate_create(const struct lu_env *env,
3279 struct mdd_object *mdd_pobj,
3280 struct mdd_object *mdd_sobj,
3281 struct mdd_object *mdd_tobj,
3284 struct mdd_thread_info *info = mdd_env_info(env);
3285 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3286 struct md_op_spec *spec = &info->mti_spec;
3287 struct lu_buf lmm_buf = { 0 };
3288 struct lu_buf link_buf = { 0 };
3289 const struct lu_buf *buf;
3290 struct thandle *handle;
3291 struct lmv_mds_md_v1 *mgr_ea;
3292 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3293 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3298 /* prepare spec for create */
3299 memset(spec, 0, sizeof(*spec));
3300 spec->sp_cr_lookup = 0;
3301 spec->sp_feat = &dt_directory_features;
3302 if (S_ISLNK(la->la_mode)) {
3303 buf = lu_buf_check_and_alloc(
3304 &mdd_env_info(env)->mti_big_buf,
3307 link_buf.lb_len = la->la_size + 1;
3308 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3310 rc = rc != 0 ? rc : -EFAULT;
3311 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3312 mdd2obd_dev(mdd)->obd_name,
3313 PFID(mdd_object_fid(mdd_sobj)), rc);
3316 spec->u.sp_symname = link_buf.lb_buf;
3317 } else if S_ISREG(la->la_mode) {
3318 /* retrieve lov of the old object */
3319 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3320 if (rc != 0 && rc != -ENODATA)
3322 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3323 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3324 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3325 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3329 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3330 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3331 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3332 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3333 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3334 fid_cpu_to_le(&mgr_ea->lmv_master_fid, mdd_object_fid(mdd_sobj));
3335 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3336 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3338 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3340 handle = mdd_trans_create(env, mdd);
3342 GOTO(out_free, rc = PTR_ERR(handle));
3344 /* Note: this transaction is part of migration, and it is not
3345 * the last step of migration, so we set th_local = 1 to avoid
3346 * update last rcvd for this transaction */
3347 handle->th_local = 1;
3348 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3350 (union lmv_mds_md *)info->mti_xattr_buf,
3353 GOTO(stop_trans, rc);
3355 rc = mdd_trans_start(env, mdd, handle);
3357 GOTO(stop_trans, rc);
3359 /* create the target object */
3360 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3363 GOTO(stop_trans, rc);
3365 /* Set MIGRATE EA on the source inode, so once the migration needs
3366 * to be re-done during failover, the re-do process can locate the
3367 * target object which is already being created. */
3368 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3369 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3370 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3371 handle, mdd_object_capa(env, mdd_sobj));
3373 GOTO(stop_trans, rc);
3375 /* Set immutable flag, so any modification is disabled until
3376 * the migration is done. Once the migration is interrupted,
3377 * if the resume process find the migrating object has both
3378 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3379 * flag and approve the migration */
3380 la_flag->la_valid = LA_FLAGS;
3381 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3382 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3383 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3384 mdd_object_capa(env, mdd_sobj));
3387 mdd_trans_stop(env, mdd, rc, handle);
3389 if (lmm_buf.lb_buf != NULL)
3390 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3394 static int mdd_migrate_entries(const struct lu_env *env,
3395 struct mdd_object *mdd_sobj,
3396 struct mdd_object *mdd_tobj)
3398 struct dt_object *next = mdd_object_child(mdd_sobj);
3399 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3400 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3401 struct thandle *handle;
3403 const struct dt_it_ops *iops;
3406 struct lu_dirent *ent;
3409 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3413 if (!dt_try_as_dir(env, next))
3414 GOTO(out_ent, rc = -ENOTDIR);
3416 * iterate directories
3418 iops = &next->do_index_ops->dio_it;
3419 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3420 mdd_object_capa(env, mdd_sobj));
3422 GOTO(out_ent, rc = PTR_ERR(it));
3424 rc = iops->load(env, it, 0);
3426 rc = iops->next(env, it);
3430 * At this point and across for-loop:
3432 * rc == 0 -> ok, proceed.
3433 * rc > 0 -> end of directory.
3437 struct mdd_object *child;
3438 char *name = mdd_env_info(env)->mti_key;
3442 bool target_exist = false;
3444 len = iops->key_size(env, it);
3448 result = iops->rec(env, it, (struct dt_rec *)ent,
3449 LUDA_FID | LUDA_TYPE);
3450 if (result == -ESTALE)
3457 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3458 recsize = le16_to_cpu(ent->lde_reclen);
3460 /* Insert new fid with target name into target dir */
3461 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3462 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3463 ent->lde_name[1] == '.'))
3466 child = mdd_object_find(env, mdd, &ent->lde_fid);
3468 GOTO(out, rc = PTR_ERR(child));
3470 is_dir = S_ISDIR(lu_object_attr(&child->mod_obj.mo_lu));
3472 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3474 /* Check whether the name has been inserted to the target */
3475 if (dt_try_as_dir(env, dt_tobj)) {
3476 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3478 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3479 (struct dt_rec *)fid,
3480 (struct dt_key *)name,
3481 mdd_object_capa(env, mdd_tobj));
3482 if (unlikely(rc == 0))
3483 target_exist = true;
3486 handle = mdd_trans_create(env, mdd);
3488 GOTO(out, rc = PTR_ERR(handle));
3490 /* Note: this transaction is part of migration, and it is not
3491 * the last step of migration, so we set th_local = 1 to avoid
3492 * updating last rcvd for this transaction */
3493 handle->th_local = 1;
3494 if (likely(!target_exist)) {
3495 rc = mdo_declare_index_insert(env, mdd_tobj,
3502 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3508 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3513 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3517 /* Update .. for child */
3518 rc = mdo_declare_index_delete(env, child, dotdot,
3523 rc = mdo_declare_index_insert(env, child,
3524 mdd_object_fid(mdd_tobj),
3530 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3537 rc = mdd_trans_start(env, mdd, handle);
3539 CERROR("%s: transaction start failed: rc = %d\n",
3540 mdd2obd_dev(mdd)->obd_name, rc);
3544 if (likely(!target_exist)) {
3545 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3546 name, is_dir, handle,
3547 mdd_object_capa(env, mdd_tobj));
3552 rc = mdo_ref_add(env, mdd_tobj, handle);
3559 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3560 mdd_object_capa(env, mdd_sobj));
3565 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3566 mdd_object_capa(env, child));
3570 rc = __mdd_index_insert_only(env, child,
3571 mdd_object_fid(mdd_tobj),
3573 mdd_object_capa(env, child));
3578 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3579 strlen(name), handle);
3582 mdd_object_put(env, child);
3583 mdd_trans_stop(env, mdd, rc, handle);
3587 result = iops->next(env, it);
3588 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3589 GOTO(out, rc = -EINTR);
3591 if (result == -ESTALE)
3593 } while (result == 0);
3596 iops->fini(env, it);
3598 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3602 static int mdd_declare_update_linkea(const struct lu_env *env,
3603 struct mdd_object *mdd_pobj,
3604 struct mdd_object *mdd_sobj,
3605 struct mdd_object *mdd_tobj,
3606 struct thandle *handle,
3607 const struct lu_name *child_name)
3609 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3610 child_name, handle, 1);
3613 static int mdd_update_linkea(const struct lu_env *env,
3614 struct mdd_object *mdd_pobj,
3615 struct mdd_object *mdd_sobj,
3616 struct mdd_object *mdd_tobj,
3617 struct thandle *handle,
3618 const struct lu_name *child_name)
3620 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3621 child_name, handle, 0);
3624 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3625 struct mdd_object *mdd_pobj,
3626 struct mdd_object *mdd_sobj,
3627 struct mdd_object *mdd_tobj,
3628 const struct lu_name *lname,
3630 struct lu_attr *parent_la,
3631 struct thandle *handle)
3633 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3636 /* Revert IMMUTABLE flag */
3637 la_flag->la_valid = LA_FLAGS;
3638 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3639 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3640 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3644 /* delete entry from source dir */
3645 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3649 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3650 mdd_tobj, handle, lname);
3654 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3655 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3661 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3662 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3668 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3669 lname->ln_name, handle);
3673 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3674 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3679 /* delete old object */
3680 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3684 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3685 /* delete old object */
3686 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3689 /* set nlink to 0 */
3690 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3695 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3699 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3704 static int mdd_migrate_update_name(const struct lu_env *env,
3705 struct mdd_object *mdd_pobj,
3706 struct mdd_object *mdd_sobj,
3707 struct mdd_object *mdd_tobj,
3708 const struct lu_name *lname,
3711 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3712 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3713 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3714 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3715 struct thandle *handle;
3716 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3717 const char *name = lname->ln_name;
3721 /* update time for parent */
3722 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3723 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3724 p_la->la_valid = LA_CTIME;
3726 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3730 handle = mdd_trans_create(env, mdd);
3732 RETURN(PTR_ERR(handle));
3734 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3735 lname, so_attr, p_la, handle);
3737 /* If the migration can not be fit in one transaction, just
3738 * leave it in the original MDT */
3740 GOTO(stop_trans, rc = 0);
3742 GOTO(stop_trans, rc);
3745 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3746 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3747 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3748 PFID(mdd_object_fid(mdd_tobj)));
3750 rc = mdd_trans_start(env, mdd, handle);
3752 GOTO(stop_trans, rc);
3754 /* Revert IMMUTABLE flag */
3755 la_flag->la_valid = LA_FLAGS;
3756 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3757 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3758 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3759 mdd_object_capa(env, mdd_pobj));
3761 GOTO(stop_trans, rc);
3763 /* Remove source name from source directory */
3764 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3765 mdd_object_capa(env, mdd_pobj));
3767 GOTO(stop_trans, rc);
3769 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3772 GOTO(stop_trans, rc);
3774 if (S_ISREG(so_attr->la_mode)) {
3775 if (so_attr->la_nlink == 1) {
3776 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3778 mdd_object_capa(env, mdd_sobj));
3779 if (rc != 0 && rc != -ENODATA)
3780 GOTO(stop_trans, rc);
3784 /* Insert new fid with target name into target dir */
3785 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj), name,
3786 is_dir, handle, mdd_object_capa(env, mdd_pobj));
3788 GOTO(stop_trans, rc);
3790 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3793 GOTO(stop_trans, rc);
3795 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3796 mdo_ref_del(env, mdd_sobj, handle);
3798 mdo_ref_del(env, mdd_sobj, handle);
3800 /* Get the attr again after ref_del */
3801 rc = mdd_la_get(env, mdd_sobj, so_attr,
3802 mdd_object_capa(env, mdd_sobj));
3804 GOTO(stop_trans, rc);
3805 ma->ma_attr = *so_attr;
3806 ma->ma_valid |= MA_INODE;
3807 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3809 GOTO(stop_trans, rc);
3811 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3813 GOTO(stop_trans, rc);
3815 mdd_write_unlock(env, mdd_sobj);
3818 mdd_trans_stop(env, mdd, rc, handle);
3824 * Check whether we should migrate the file/dir
3826 * < 0 permission check failed or other error.
3827 * = 0 the file can be migrated.
3828 * > 0 the file does not need to be migrated, mostly
3829 * for multiple link file
3831 static int mdd_migrate_sanity_check(const struct lu_env *env,
3832 struct mdd_object *pobj,
3833 const struct lu_attr *pattr,
3834 struct mdd_object *sobj,
3835 struct lu_attr *sattr)
3837 struct mdd_thread_info *info = mdd_env_info(env);
3838 struct linkea_data *ldata = &info->mti_link_data;
3840 struct lu_buf *mgr_buf;
3846 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3847 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3848 if (mgr_buf->lb_buf == NULL)
3851 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3852 mdd_object_capa(env, sobj));
3854 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3856 /* If the object has migrateEA, it means IMMUTE flag
3857 * is being set by previous migration process, so it
3858 * needs to override the IMMUTE flag, otherwise the
3859 * following sanity check will fail */
3860 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3861 LMV_HASH_FLAG_MIGRATION) {
3862 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3864 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3865 sobj->mod_flags &= ~IMMUTE_OBJ;
3866 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3867 mdd2obd_dev(mdd)->obd_name,
3868 PFID(mdd_object_fid(sobj)));
3872 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3873 sobj, sattr, NULL, NULL);
3877 /* Then it will check if the file should be migrated. If the file
3878 * has mulitple links, we only need migrate the file if all of its
3879 * entries has been migrated to the remote MDT */
3880 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3883 rc = mdd_links_read(env, sobj, ldata);
3885 /* For multiple links files, if there are no linkEA data at all,
3886 * means the file might be created before linkEA is enabled, and
3887 * all all of its links should not be migrated yet, otherwise
3888 * it should have some linkEA there */
3889 if (rc == -ENOENT || rc == -ENODATA)
3894 /* If it is mulitple links file, we need update the name entry for
3896 LASSERT(ldata->ld_leh != NULL);
3897 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3898 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3899 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3900 struct mdd_object *lpobj;
3901 struct lu_name lname;
3904 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3906 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3908 lpobj = mdd_object_find(env, mdd, &fid);
3909 if (IS_ERR(lpobj)) {
3910 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3911 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3916 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3917 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3918 PFID(&fid), lname.ln_namelen, lname.ln_name);
3919 mdd_object_put(env, lpobj);
3923 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3924 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3925 lname.ln_name, PFID(&fid));
3926 mdd_object_put(env, lpobj);
3934 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3935 struct md_object *sobj, const struct lu_name *lname,
3936 struct md_object *tobj, struct md_attr *ma)
3938 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3939 struct mdd_device *mdd = mdo2mdd(pobj);
3940 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
3941 struct mdd_object *mdd_tobj = NULL;
3942 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3943 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3947 /* If the file will being migrated, it will check whether
3948 * the file is being opened by someone else right now */
3949 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3950 if (mdd_sobj->mod_count >= 1) {
3951 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
3952 mdd2obd_dev(mdd)->obd_name,
3953 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3954 mdd_sobj->mod_count, -EBUSY);
3955 mdd_read_unlock(env, mdd_sobj);
3956 GOTO(put, rc = -EBUSY);
3958 mdd_read_unlock(env, mdd_sobj);
3960 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3964 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
3968 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
3975 /* Sigh, it is impossible to finish all of migration in a single
3976 * transaction, for example migrating big directory entries to the
3977 * new MDT, it needs insert all of name entries of children in the
3980 * So migration will be done in multiple steps and transactions.
3982 * 1. create an orphan object on the remote MDT in one transaction.
3983 * 2. migrate extend attributes to the new target file/directory.
3984 * 3. For directory, migrate the entries to the new MDT and update
3985 * linkEA of each children. Because we can not migrate all entries
3986 * in a single transaction, so the migrating directory will become
3987 * a striped directory during migration, so once the process is
3988 * interrupted, the directory is still accessible. (During lookup,
3989 * client will locate the name by searching both original and target
3991 * 4. Finally, update the name/FID to point to the new file/directory
3992 * in a separate transaction.
3995 /* step 1: Check whether the orphan object has been created, and create
3996 * orphan object on the remote MDT if needed */
3997 mdd_tobj = md2mdd_obj(tobj);
3998 if (!mdd_object_exists(mdd_tobj)) {
3999 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4005 /* step 2: migrate xattr */
4006 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4010 /* step 3: migrate name entries to the orphan object */
4011 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4012 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4015 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4016 OBD_FAIL_MDS_REINT_NET_REP)))
4020 /* step 4: update name entry to the new object */
4021 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4029 const struct md_dir_operations mdd_dir_ops = {
4030 .mdo_is_subdir = mdd_is_subdir,
4031 .mdo_lookup = mdd_lookup,
4032 .mdo_create = mdd_create,
4033 .mdo_rename = mdd_rename,
4034 .mdo_link = mdd_link,
4035 .mdo_unlink = mdd_unlink,
4036 .mdo_create_data = mdd_create_data,
4037 .mdo_migrate = mdd_migrate,