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)
1035 if (ldata == NULL) {
1036 ldata = &mdd_env_info(env)->mti_link_data;
1037 memset(ldata, 0, sizeof(*ldata));
1038 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1039 newpfid, newlname, first, check,
1045 if (ldata->ld_reclen != 0)
1046 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1051 if (rc == -EOVERFLOW || rc == -ENOSPC)
1053 if (oldpfid == NULL)
1054 CDEBUG(error ? D_ERROR : D_OTHER,
1055 "link_ea add '%.*s' failed %d "DFID"\n",
1056 newlname->ln_namelen, newlname->ln_name,
1057 rc, PFID(mdd_object_fid(mdd_obj)));
1058 else if (newpfid == NULL)
1059 CDEBUG(error ? D_ERROR : D_OTHER,
1060 "link_ea del '%.*s' failed %d "DFID"\n",
1061 oldlname->ln_namelen, oldlname->ln_name,
1062 rc, PFID(mdd_object_fid(mdd_obj)));
1064 CDEBUG(error ? D_ERROR : D_OTHER,
1065 "link_ea rename '%.*s'->'%.*s' failed %d "
1067 oldlname->ln_namelen, oldlname->ln_name,
1068 newlname->ln_namelen, newlname->ln_name,
1069 rc, PFID(mdd_object_fid(mdd_obj)));
1072 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1073 /* if we vmalloced a large buffer drop it */
1074 lu_buf_free(ldata->ld_buf);
1079 static inline int mdd_links_add(const struct lu_env *env,
1080 struct mdd_object *mdd_obj,
1081 const struct lu_fid *pfid,
1082 const struct lu_name *lname,
1083 struct thandle *handle,
1084 struct linkea_data *ldata, int first)
1086 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1087 pfid, lname, handle, ldata, first, 0);
1090 static inline int mdd_links_del(const struct lu_env *env,
1091 struct mdd_object *mdd_obj,
1092 const struct lu_fid *pfid,
1093 const struct lu_name *lname,
1094 struct thandle *handle)
1096 return mdd_links_rename(env, mdd_obj, pfid, lname,
1097 NULL, NULL, handle, NULL, 0, 0);
1100 /** Read the link EA into a temp buffer.
1101 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1102 * A pointer to the buffer is stored in \a ldata::ld_buf.
1104 * \retval 0 or error
1106 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1107 struct linkea_data *ldata)
1111 if (!mdd_object_exists(mdd_obj))
1114 /* First try a small buf */
1115 LASSERT(env != NULL);
1116 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1118 if (ldata->ld_buf->lb_buf == NULL)
1121 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1123 if (rc == -ERANGE) {
1124 /* Buf was too small, figure out what we need. */
1125 lu_buf_free(ldata->ld_buf);
1126 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1127 XATTR_NAME_LINK, BYPASS_CAPA);
1130 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1131 if (ldata->ld_buf->lb_buf == NULL)
1133 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1134 XATTR_NAME_LINK, BYPASS_CAPA);
1137 lu_buf_free(ldata->ld_buf);
1138 ldata->ld_buf = NULL;
1142 return linkea_init(ldata);
1145 /** Read the link EA into a temp buffer.
1146 * Uses the name_buf since it is generally large.
1147 * \retval IS_ERR err
1148 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1150 struct lu_buf *mdd_links_get(const struct lu_env *env,
1151 struct mdd_object *mdd_obj)
1153 struct linkea_data ldata = { 0 };
1156 rc = mdd_links_read(env, mdd_obj, &ldata);
1157 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1160 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1161 struct linkea_data *ldata, struct thandle *handle)
1163 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1164 ldata->ld_leh->leh_len);
1165 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1166 mdd_object_capa(env, mdd_obj));
1169 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1170 struct thandle *handle, struct linkea_data *ldata)
1176 if (ldata != NULL && ldata->ld_leh != NULL) {
1177 ea_len = ldata->ld_leh->leh_len;
1178 linkea = ldata->ld_buf->lb_buf;
1180 ea_len = DEFAULT_LINKEA_SIZE;
1184 /* XXX: max size? */
1185 rc = mdo_declare_xattr_set(env, mdd_obj,
1186 mdd_buf_get_const(env, linkea, ea_len),
1187 XATTR_NAME_LINK, 0, handle);
1191 static inline int mdd_declare_links_del(const struct lu_env *env,
1192 struct mdd_object *c,
1193 struct thandle *handle)
1197 /* For directory, the linkEA will be removed together
1198 * with the object. */
1199 if (!S_ISDIR(mdd_object_type(c)))
1200 rc = mdd_declare_links_add(env, c, handle, NULL);
1205 static int mdd_declare_link(const struct lu_env *env,
1206 struct mdd_device *mdd,
1207 struct mdd_object *p,
1208 struct mdd_object *c,
1209 const struct lu_name *name,
1210 struct thandle *handle,
1212 struct linkea_data *data)
1216 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1220 rc = mdo_declare_ref_add(env, c, handle);
1224 la->la_valid = LA_CTIME | LA_MTIME;
1225 rc = mdo_declare_attr_set(env, p, la, handle);
1229 la->la_valid = LA_CTIME;
1230 rc = mdo_declare_attr_set(env, c, la, handle);
1234 rc = mdd_declare_links_add(env, c, handle, data);
1238 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1243 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1244 struct md_object *src_obj, const struct lu_name *lname,
1247 const char *name = lname->ln_name;
1248 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1249 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1250 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1251 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1252 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1253 struct mdd_device *mdd = mdo2mdd(src_obj);
1254 struct thandle *handle;
1255 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1259 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1263 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1267 handle = mdd_trans_create(env, mdd);
1269 GOTO(out_pending, rc = PTR_ERR(handle));
1271 memset(ldata, 0, sizeof(*ldata));
1273 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1274 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1276 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1281 rc = mdd_trans_start(env, mdd, handle);
1285 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1286 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1289 GOTO(out_unlock, rc);
1291 rc = mdo_ref_add(env, mdd_sobj, handle);
1293 GOTO(out_unlock, rc);
1296 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1298 mdd_object_capa(env, mdd_tobj));
1300 mdo_ref_del(env, mdd_sobj, handle);
1301 GOTO(out_unlock, rc);
1304 la->la_valid = LA_CTIME | LA_MTIME;
1305 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1307 GOTO(out_unlock, rc);
1309 la->la_valid = LA_CTIME;
1310 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1312 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1313 mdo2fid(mdd_tobj), lname, 0, 0,
1316 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1317 lname, handle, ldata, 0);
1318 /* The failure of links_add should not cause the link
1319 * failure, reset rc here */
1324 mdd_write_unlock(env, mdd_sobj);
1326 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1327 mdd_tobj, lname, handle);
1329 mdd_trans_stop(env, mdd, rc, handle);
1331 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1332 /* if we vmalloced a large buffer drop it */
1333 lu_buf_free(ldata->ld_buf);
1338 static int mdd_mark_dead_object(const struct lu_env *env,
1339 struct mdd_object *obj, struct thandle *handle,
1342 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1346 obj->mod_flags |= DEAD_OBJ;
1348 if (!S_ISDIR(mdd_object_type(obj)))
1351 attr->la_valid = LA_FLAGS;
1352 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1355 rc = mdo_declare_attr_set(env, obj, attr, handle);
1357 rc = mdo_attr_set(env, obj, attr, handle,
1358 mdd_object_capa(env, obj));
1363 static int mdd_declare_finish_unlink(const struct lu_env *env,
1364 struct mdd_object *obj,
1365 struct thandle *handle)
1369 rc = mdd_mark_dead_object(env, obj, handle, true);
1373 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1377 rc = mdo_declare_destroy(env, obj, handle);
1381 return mdd_declare_links_del(env, obj, handle);
1384 /* caller should take a lock before calling */
1385 int mdd_finish_unlink(const struct lu_env *env,
1386 struct mdd_object *obj, struct md_attr *ma,
1387 const struct mdd_object *pobj,
1388 const struct lu_name *lname,
1392 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1395 LASSERT(mdd_write_locked(env, obj) != 0);
1397 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1398 rc = mdd_mark_dead_object(env, obj, th, false);
1402 /* add new orphan and the object
1403 * will be deleted during mdd_close() */
1404 if (obj->mod_count) {
1405 rc = __mdd_orphan_add(env, obj, th);
1407 CDEBUG(D_HA, "Object "DFID" is inserted into "
1408 "orphan list, open count = %d\n",
1409 PFID(mdd_object_fid(obj)),
1412 CERROR("Object "DFID" fail to be an orphan, "
1413 "open count = %d, maybe cause failed "
1415 PFID(mdd_object_fid(obj)),
1418 rc = mdo_destroy(env, obj, th);
1420 } else if (!is_dir) {
1421 /* old files may not have link ea; ignore errors */
1422 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1430 * has mdd_write_lock on cobj already, but not on pobj yet
1432 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1433 const struct lu_attr *pattr,
1434 struct mdd_object *cobj,
1435 const struct lu_attr *cattr)
1440 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1445 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1446 struct mdd_object *p, struct mdd_object *c,
1447 const struct lu_name *name, struct md_attr *ma,
1448 struct thandle *handle, int no_name)
1450 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1453 if (likely(no_name == 0)) {
1454 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1459 rc = mdo_declare_ref_del(env, p, handle);
1463 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1464 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1465 la->la_valid = LA_CTIME | LA_MTIME;
1466 rc = mdo_declare_attr_set(env, p, la, handle);
1471 rc = mdo_declare_ref_del(env, c, handle);
1475 rc = mdo_declare_ref_del(env, c, handle);
1479 la->la_valid = LA_CTIME;
1480 rc = mdo_declare_attr_set(env, c, la, handle);
1484 rc = mdd_declare_finish_unlink(env, c, handle);
1488 /* FIXME: need changelog for remove entry */
1489 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1496 * test if a file has an HSM archive
1497 * if HSM attributes are not found in ma update them from
1500 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1501 struct mdd_object *obj,
1506 if (!(ma->ma_valid & MA_HSM)) {
1507 /* no HSM MD provided, read xattr */
1508 struct lu_buf *hsm_buf;
1509 const size_t buflen = sizeof(struct hsm_attrs);
1512 hsm_buf = mdd_buf_get(env, NULL, 0);
1513 lu_buf_alloc(hsm_buf, buflen);
1514 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1515 mdd_object_capa(env, obj));
1516 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1517 lu_buf_free(hsm_buf);
1521 ma->ma_valid = MA_HSM;
1523 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1529 * Delete name entry and the object.
1530 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1531 * does not exist for this object, and it could only happen during resending
1532 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1533 * is also needed in this case(needed by changelog), so we have to add another
1534 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1535 * the ENOENT failure should be able to be fixed by redo mechanism.
1537 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1538 struct md_object *cobj, const struct lu_name *lname,
1539 struct md_attr *ma, int no_name)
1541 const char *name = lname->ln_name;
1542 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1543 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1544 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1545 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1546 struct mdd_object *mdd_cobj = NULL;
1547 struct mdd_device *mdd = mdo2mdd(pobj);
1548 struct thandle *handle;
1552 /* cobj == NULL means only delete name entry */
1553 if (likely(cobj != NULL)) {
1554 mdd_cobj = md2mdd_obj(cobj);
1555 if (mdd_object_exists(mdd_cobj) == 0)
1559 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1563 if (likely(mdd_cobj != NULL)) {
1565 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1569 is_dir = S_ISDIR(cattr->la_mode);
1572 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1576 handle = mdd_trans_create(env, mdd);
1578 RETURN(PTR_ERR(handle));
1580 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1581 lname, ma, handle, no_name);
1585 rc = mdd_trans_start(env, mdd, handle);
1589 if (likely(mdd_cobj != NULL))
1590 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1592 if (likely(no_name == 0)) {
1593 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1594 mdd_object_capa(env, mdd_pobj));
1599 if (likely(mdd_cobj != NULL)) {
1600 rc = mdo_ref_del(env, mdd_cobj, handle);
1602 __mdd_index_insert_only(env, mdd_pobj,
1605 mdd_object_capa(env, mdd_pobj));
1611 mdo_ref_del(env, mdd_cobj, handle);
1613 /* fetch updated nlink */
1614 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1619 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1620 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1622 la->la_valid = LA_CTIME | LA_MTIME;
1623 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1627 /* Enough for only unlink the entry */
1628 if (unlikely(mdd_cobj == NULL))
1631 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1632 /* update ctime of an unlinked file only if it is still
1633 * opened or a link still exists */
1634 la->la_valid = LA_CTIME;
1635 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1640 /* XXX: this transfer to ma will be removed with LOD/OSP */
1641 ma->ma_attr = *cattr;
1642 ma->ma_valid |= MA_INODE;
1643 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1645 /* fetch updated nlink */
1647 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1649 /* if object is removed then we can't get its attrs, use last get */
1650 if (cattr->la_nlink == 0) {
1651 ma->ma_attr = *cattr;
1652 ma->ma_valid |= MA_INODE;
1656 mdd_write_unlock(env, mdd_cobj);
1660 if (cattr->la_nlink == 0) {
1661 cl_flags |= CLF_UNLINK_LAST;
1662 /* search for an existing archive */
1663 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1664 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1667 rc = mdd_changelog_ns_store(env, mdd,
1668 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1669 mdd_cobj, mdd_pobj, lname, handle);
1673 mdd_trans_stop(env, mdd, rc, handle);
1679 * The permission has been checked when obj created, no need check again.
1681 static int mdd_cd_sanity_check(const struct lu_env *env,
1682 struct mdd_object *obj)
1687 if (!obj || mdd_is_dead_obj(obj))
1693 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1694 struct md_object *cobj, const struct md_op_spec *spec,
1697 struct mdd_device *mdd = mdo2mdd(cobj);
1698 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1699 struct mdd_object *son = md2mdd_obj(cobj);
1700 struct thandle *handle;
1701 const struct lu_buf *buf;
1702 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1703 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1707 rc = mdd_cd_sanity_check(env, son);
1711 if (!md_should_create(spec->sp_cr_flags))
1715 * there are following use cases for this function:
1716 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1717 * striping can be specified or not
1720 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1724 /* calling ->ah_make_hint() is used to transfer information from parent */
1725 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1727 handle = mdd_trans_create(env, mdd);
1729 GOTO(out_free, rc = PTR_ERR(handle));
1732 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1733 * Should this be fixed?
1735 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1736 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1737 spec->sp_cr_flags, spec->no_create);
1739 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1740 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1741 spec->u.sp_ea.eadatalen);
1746 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1747 XATTR_NAME_LOV, 0, handle);
1751 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1755 rc = mdd_trans_start(env, mdd, handle);
1759 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1760 0, handle, mdd_object_capa(env, son));
1765 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1768 mdd_trans_stop(env, mdd, rc, handle);
1773 static int mdd_declare_object_initialize(const struct lu_env *env,
1774 struct mdd_object *parent,
1775 struct mdd_object *child,
1776 struct lu_attr *attr,
1777 struct thandle *handle)
1783 * inode mode has been set in creation time, and it's based on umask,
1784 * la_mode and acl, don't set here again! (which will go wrong
1785 * because below function doesn't consider umask).
1786 * I'd suggest set all object attributes in creation time, see above.
1788 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1789 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1790 rc = mdo_declare_attr_set(env, child, attr, handle);
1791 attr->la_valid |= LA_MODE | LA_TYPE;
1792 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1793 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1796 rc = mdo_declare_ref_add(env, child, handle);
1798 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1805 static int mdd_object_initialize(const struct lu_env *env,
1806 const struct lu_fid *pfid,
1807 struct mdd_object *child,
1808 struct lu_attr *attr, struct thandle *handle,
1809 const struct md_op_spec *spec)
1815 * Update attributes for child.
1818 * (1) the valid bits should be converted between Lustre and Linux;
1819 * (2) maybe, the child attributes should be set in OSD when creation.
1822 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1823 /* arguments are supposed to stay the same */
1824 if (S_ISDIR(attr->la_mode)) {
1825 /* Add "." and ".." for newly created dir */
1826 mdo_ref_add(env, child, handle);
1827 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1828 dot, handle, BYPASS_CAPA);
1830 rc = __mdd_index_insert_only(env, child, pfid,
1834 mdo_ref_del(env, child, handle);
1840 /* has not lock on pobj yet */
1841 static int mdd_create_sanity_check(const struct lu_env *env,
1842 struct md_object *pobj,
1843 const struct lu_attr *pattr,
1844 const struct lu_name *lname,
1845 struct lu_attr *cattr,
1846 struct md_op_spec *spec)
1848 struct mdd_thread_info *info = mdd_env_info(env);
1849 struct lu_fid *fid = &info->mti_fid;
1850 struct mdd_object *obj = md2mdd_obj(pobj);
1851 struct mdd_device *m = mdo2mdd(pobj);
1856 if (mdd_is_dead_obj(obj))
1860 * In some cases this lookup is not needed - we know before if name
1861 * exists or not because MDT performs lookup for it.
1862 * name length check is done in lookup.
1864 if (spec->sp_cr_lookup) {
1866 * Check if the name already exist, though it will be checked in
1867 * _index_insert also, for avoiding rolling back if exists
1870 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1871 MAY_WRITE | MAY_EXEC);
1873 RETURN(rc ? : -EEXIST);
1875 rc = mdd_may_create(env, obj, pattr, NULL, true, false);
1881 if (pattr->la_mode & S_ISGID) {
1882 cattr->la_gid = pattr->la_gid;
1883 if (S_ISDIR(cattr->la_mode)) {
1884 cattr->la_mode |= S_ISGID;
1885 cattr->la_valid |= LA_MODE;
1889 rc = mdd_name_check(m, lname);
1893 switch (cattr->la_mode & S_IFMT) {
1895 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1897 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1898 RETURN(-ENAMETOOLONG);
1917 static int mdd_declare_object_create(const struct lu_env *env,
1918 struct mdd_device *mdd,
1919 struct mdd_object *p, struct mdd_object *c,
1920 struct lu_attr *attr,
1921 struct thandle *handle,
1922 const struct md_op_spec *spec,
1923 struct lu_buf *def_acl_buf,
1924 struct lu_buf *acl_buf,
1925 struct dt_allocation_hint *hint)
1929 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1934 #ifdef CONFIG_FS_POSIX_ACL
1935 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1936 /* if dir, then can inherit default ACl */
1937 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1938 XATTR_NAME_ACL_DEFAULT,
1944 if (acl_buf->lb_len > 0) {
1945 rc = mdo_declare_attr_set(env, c, attr, handle);
1949 rc = mdo_declare_xattr_set(env, c, acl_buf,
1950 XATTR_NAME_ACL_ACCESS, 0, handle);
1955 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1959 /* replay case, create LOV EA from client data */
1960 if (spec->no_create ||
1961 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
1962 const struct lu_buf *buf;
1964 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1965 spec->u.sp_ea.eadatalen);
1966 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
1972 if (S_ISLNK(attr->la_mode)) {
1973 const char *target_name = spec->u.sp_symname;
1974 int sym_len = strlen(target_name);
1975 const struct lu_buf *buf;
1977 buf = mdd_buf_get_const(env, target_name, sym_len);
1978 rc = dt_declare_record_write(env, mdd_object_child(c),
1987 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1988 struct mdd_object *p, struct mdd_object *c,
1989 const struct lu_name *name,
1990 struct lu_attr *attr,
1991 struct thandle *handle,
1992 const struct md_op_spec *spec,
1993 struct linkea_data *ldata,
1994 struct lu_buf *def_acl_buf,
1995 struct lu_buf *acl_buf,
1996 struct dt_allocation_hint *hint)
2000 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2001 def_acl_buf, acl_buf, hint);
2005 if (S_ISDIR(attr->la_mode)) {
2006 rc = mdo_declare_ref_add(env, p, handle);
2011 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2012 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2016 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2018 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,
2022 rc = mdd_declare_links_add(env, c, handle, ldata);
2027 la->la_valid = LA_CTIME | LA_MTIME;
2028 rc = mdo_declare_attr_set(env, p, la, handle);
2032 rc = mdd_declare_changelog_store(env, mdd, name, handle);
2037 /* XXX: For remote create, it should indicate the remote RPC
2038 * will be sent after local transaction is finished, which
2039 * is not very nice, but it will be removed once we fully support
2041 if (mdd_object_remote(p) && handle->th_update != NULL)
2042 handle->th_update->tu_sent_after_local_trans = 1;
2047 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2048 struct lu_attr *la, struct lu_buf *def_acl_buf,
2049 struct lu_buf *acl_buf)
2054 if (S_ISLNK(la->la_mode)) {
2055 acl_buf->lb_len = 0;
2056 def_acl_buf->lb_len = 0;
2060 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2061 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2062 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2063 mdd_read_unlock(env, pobj);
2065 /* If there are default ACL, fix mode/ACL by default ACL */
2066 def_acl_buf->lb_len = rc;
2067 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2068 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2069 acl_buf->lb_len = rc;
2070 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2073 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2074 /* If there are no default ACL, fix mode by mask */
2075 struct lu_ucred *uc = lu_ucred(env);
2077 /* The create triggered by MDT internal events, such as
2078 * LFSCK reset, will not contain valid "uc". */
2079 if (unlikely(uc != NULL))
2080 la->la_mode &= ~uc->uc_umask;
2082 acl_buf->lb_len = 0;
2083 def_acl_buf->lb_len = 0;
2090 * Create a metadata object and initialize it, set acl, xattr.
2092 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2093 struct mdd_object *son, struct lu_attr *attr,
2094 struct md_op_spec *spec, struct lu_buf *acl_buf,
2095 struct lu_buf *def_acl_buf,
2096 struct dt_allocation_hint *hint,
2097 struct thandle *handle)
2101 mdd_write_lock(env, son, MOR_TGT_CHILD);
2102 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2107 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2108 * created in declare phase, they also needs to be added to master
2109 * object as sub-directory entry. So it has to initialize the master
2110 * object, then set dir striped EA.(in mdo_xattr_set) */
2111 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2114 GOTO(err_destroy, rc);
2117 * in case of replay we just set LOVEA provided by the client
2118 * XXX: I think it would be interesting to try "old" way where
2119 * MDT calls this xattr_set(LOV) in a different transaction.
2120 * probably this way we code can be made better.
2123 /* During creation, there are only a few cases we need do xattr_set to
2125 * 1. regular file: see comments above.
2126 * 2. create striped directory with provided stripeEA.
2127 * 3. create striped directory because inherit default layout from the
2129 if (spec->no_create ||
2130 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2131 S_ISDIR(attr->la_mode)) {
2132 const struct lu_buf *buf;
2134 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2135 spec->u.sp_ea.eadatalen);
2136 rc = mdo_xattr_set(env, son, buf,
2137 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2139 handle, BYPASS_CAPA);
2141 GOTO(err_destroy, rc);
2144 #ifdef CONFIG_FS_POSIX_ACL
2145 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2146 S_ISDIR(attr->la_mode)) {
2147 /* set default acl */
2148 rc = mdo_xattr_set(env, son, def_acl_buf,
2149 XATTR_NAME_ACL_DEFAULT, 0,
2150 handle, BYPASS_CAPA);
2152 GOTO(err_destroy, rc);
2154 /* set its own acl */
2155 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2156 rc = mdo_xattr_set(env, son, acl_buf,
2157 XATTR_NAME_ACL_ACCESS,
2158 0, handle, BYPASS_CAPA);
2160 GOTO(err_destroy, rc);
2164 if (S_ISLNK(attr->la_mode)) {
2165 struct lu_ucred *uc = lu_ucred_assert(env);
2166 struct dt_object *dt = mdd_object_child(son);
2167 const char *target_name = spec->u.sp_symname;
2168 int sym_len = strlen(target_name);
2169 const struct lu_buf *buf;
2172 buf = mdd_buf_get_const(env, target_name, sym_len);
2173 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2174 mdd_object_capa(env, son),
2176 CFS_CAP_SYS_RESOURCE_MASK);
2181 GOTO(err_initlized, rc = -EFAULT);
2185 if (unlikely(rc != 0)) {
2187 if (S_ISDIR(attr->la_mode)) {
2188 /* Drop the reference, no need to delete "."/"..",
2189 * because the object to be destroied directly. */
2190 rc2 = mdo_ref_del(env, son, handle);
2194 rc2 = mdo_ref_del(env, son, handle);
2198 mdo_destroy(env, son, handle);
2201 mdd_write_unlock(env, son);
2206 * Create object and insert it into namespace.
2208 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2209 const struct lu_name *lname, struct md_object *child,
2210 struct md_op_spec *spec, struct md_attr* ma)
2212 struct mdd_thread_info *info = mdd_env_info(env);
2213 struct lu_attr *la = &info->mti_la_for_fix;
2214 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2215 struct mdd_object *son = md2mdd_obj(child);
2216 struct mdd_device *mdd = mdo2mdd(pobj);
2217 struct lu_attr *attr = &ma->ma_attr;
2218 struct thandle *handle;
2219 struct lu_attr *pattr = &info->mti_pattr;
2220 struct lu_buf acl_buf;
2221 struct lu_buf def_acl_buf;
2222 struct linkea_data *ldata = &info->mti_link_data;
2223 const char *name = lname->ln_name;
2224 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2230 * Two operations have to be performed:
2232 * - an allocation of a new object (->do_create()), and
2234 * - an insertion into a parent index (->dio_insert()).
2236 * Due to locking, operation order is not important, when both are
2237 * successful, *but* error handling cases are quite different:
2239 * - if insertion is done first, and following object creation fails,
2240 * insertion has to be rolled back, but this operation might fail
2241 * also leaving us with dangling index entry.
2243 * - if creation is done first, is has to be undone if insertion
2244 * fails, leaving us with leaked space, which is neither good, nor
2247 * It seems that creation-first is simplest solution, but it is
2248 * sub-optimal in the frequent
2253 * case, because second mkdir is bound to create object, only to
2254 * destroy it immediately.
2256 * To avoid this follow local file systems that do double lookup:
2258 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2260 * 1. create (mdd_object_create_internal())
2262 * 2. insert (__mdd_index_insert(), lookup again)
2265 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2269 /* Sanity checks before big job. */
2270 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2274 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2275 GOTO(out_free, rc = -EINPROGRESS);
2277 handle = mdd_trans_create(env, mdd);
2279 GOTO(out_free, rc = PTR_ERR(handle));
2281 acl_buf.lb_buf = info->mti_xattr_buf;
2282 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2283 def_acl_buf.lb_buf = info->mti_key;
2284 def_acl_buf.lb_len = sizeof(info->mti_key);
2285 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2289 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2291 memset(ldata, 0, sizeof(*ldata));
2292 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2293 mdd_object_fid(mdd_pobj),
2294 lname, 1, 0, ldata);
2296 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2297 handle, spec, ldata, &def_acl_buf, &acl_buf,
2302 rc = mdd_trans_start(env, mdd, handle);
2306 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2307 &def_acl_buf, hint, handle);
2311 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2312 mdd_write_lock(env, son, MOR_TGT_CHILD);
2313 rc = __mdd_orphan_add(env, son, handle);
2314 GOTO(out_volatile, rc);
2316 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2317 name, S_ISDIR(attr->la_mode), handle,
2318 mdd_object_capa(env, mdd_pobj));
2320 GOTO(err_created, rc);
2322 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2325 /* update parent directory mtime/ctime */
2327 la->la_valid = LA_CTIME | LA_MTIME;
2328 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2330 GOTO(err_insert, rc);
2338 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2339 rc2 = __mdd_orphan_del(env, son, handle);
2341 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2342 S_ISDIR(attr->la_mode),
2343 handle, BYPASS_CAPA);
2348 mdd_write_lock(env, son, MOR_TGT_CHILD);
2349 if (S_ISDIR(attr->la_mode)) {
2350 /* Drop the reference, no need to delete "."/"..",
2351 * because the object is to be destroyed directly. */
2352 rc2 = mdo_ref_del(env, son, handle);
2354 mdd_write_unlock(env, son);
2359 /* For volatile files drop one link immediately, since there is
2360 * no filename in the namespace, and save any error returned. */
2361 rc2 = mdo_ref_del(env, son, handle);
2363 mdd_write_unlock(env, son);
2364 if (unlikely(rc == 0))
2369 /* Don't destroy the volatile object on success */
2370 if (likely(rc != 0))
2371 mdo_destroy(env, son, handle);
2372 mdd_write_unlock(env, son);
2375 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2376 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2377 rc = mdd_changelog_ns_store(env, mdd,
2378 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2379 S_ISREG(attr->la_mode) ? CL_CREATE :
2380 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2381 0, son, mdd_pobj, lname, handle);
2383 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2387 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2388 /* if we vmalloced a large buffer drop it */
2389 lu_buf_free(ldata->ld_buf);
2391 /* The child object shouldn't be cached anymore */
2393 set_bit(LU_OBJECT_HEARD_BANSHEE,
2394 &child->mo_lu.lo_header->loh_flags);
2399 * Get locks on parents in proper order
2400 * RETURN: < 0 - error, rename_order if successful
2408 static int mdd_rename_order(const struct lu_env *env,
2409 struct mdd_device *mdd,
2410 struct mdd_object *src_pobj,
2411 const struct lu_attr *pattr,
2412 struct mdd_object *tgt_pobj)
2414 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2418 if (src_pobj == tgt_pobj)
2419 RETURN(MDD_RN_SAME);
2421 /* compared the parent child relationship of src_p&tgt_p */
2422 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2424 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2427 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2441 /* has not mdd_write{read}_lock on any obj yet. */
2442 static int mdd_rename_sanity_check(const struct lu_env *env,
2443 struct mdd_object *src_pobj,
2444 const struct lu_attr *pattr,
2445 struct mdd_object *tgt_pobj,
2446 const struct lu_attr *tpattr,
2447 struct mdd_object *sobj,
2448 const struct lu_attr *cattr,
2449 struct mdd_object *tobj,
2450 const struct lu_attr *tattr)
2455 /* XXX: when get here, sobj must NOT be NULL,
2456 * the other case has been processed in cld_rename
2457 * before mdd_rename and enable MDS_PERM_BYPASS. */
2460 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2464 /* XXX: when get here, "tobj == NULL" means tobj must
2465 * NOT exist (neither on remote MDS, such case has been
2466 * processed in cld_rename before mdd_rename and enable
2468 * So check may_create, but not check may_unlink. */
2470 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2471 (src_pobj != tgt_pobj), false);
2473 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2474 (src_pobj != tgt_pobj), 1);
2476 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2477 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2482 static int mdd_declare_rename(const struct lu_env *env,
2483 struct mdd_device *mdd,
2484 struct mdd_object *mdd_spobj,
2485 struct mdd_object *mdd_tpobj,
2486 struct mdd_object *mdd_sobj,
2487 struct mdd_object *mdd_tobj,
2488 const struct lu_name *tname,
2489 const struct lu_name *sname,
2491 struct linkea_data *ldata,
2492 struct thandle *handle)
2494 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2497 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2498 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2504 /* name from source dir */
2505 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2509 /* .. from source child */
2510 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2511 /* source child can be directory,
2512 * counted by source dir's nlink */
2513 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2516 if (mdd_spobj != mdd_tpobj) {
2517 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2522 rc = mdo_declare_index_insert(env, mdd_sobj,
2528 /* new target child can be directory,
2529 * counted by target dir's nlink */
2530 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2535 la->la_valid = LA_CTIME | LA_MTIME;
2536 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2540 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2544 la->la_valid = LA_CTIME;
2545 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2549 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2554 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2555 tname->ln_name, handle);
2559 /* name from target dir (old name), we declare it unconditionally
2560 * as mdd_rename() calls delete unconditionally as well. so just
2561 * to balance declarations vs calls to change ... */
2562 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2566 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2567 /* delete target child in target parent directory */
2568 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2572 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2573 /* target child can be directory,
2574 * delete "." reference in target child directory */
2575 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2579 /* delete ".." reference in target parent directory */
2580 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2585 la->la_valid = LA_CTIME;
2586 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2590 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2595 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2602 /* src object can be remote that is why we use only fid and type of object */
2603 static int mdd_rename(const struct lu_env *env,
2604 struct md_object *src_pobj, struct md_object *tgt_pobj,
2605 const struct lu_fid *lf, const struct lu_name *lsname,
2606 struct md_object *tobj, const struct lu_name *ltname,
2609 const char *sname = lsname->ln_name;
2610 const char *tname = ltname->ln_name;
2611 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2612 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2613 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2614 struct mdd_device *mdd = mdo2mdd(src_pobj);
2615 struct mdd_object *mdd_sobj = NULL; /* source object */
2616 struct mdd_object *mdd_tobj = NULL;
2617 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2618 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2619 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2620 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2621 struct thandle *handle;
2622 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2623 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2624 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2627 bool tobj_locked = 0;
2628 unsigned cl_flags = 0;
2633 mdd_tobj = md2mdd_obj(tobj);
2635 mdd_sobj = mdd_object_find(env, mdd, lf);
2637 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2639 GOTO(out_pending, rc);
2641 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2643 GOTO(out_pending, rc);
2646 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2648 GOTO(out_pending, rc);
2651 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2653 GOTO(out_pending, rc);
2655 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2656 mdd_sobj, cattr, mdd_tobj, tattr);
2658 GOTO(out_pending, rc);
2660 rc = mdd_name_check(mdd, ltname);
2662 GOTO(out_pending, rc);
2664 handle = mdd_trans_create(env, mdd);
2666 GOTO(out_pending, rc = PTR_ERR(handle));
2668 memset(ldata, 0, sizeof(*ldata));
2669 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2670 ltname, 1, 0, ldata);
2671 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2672 mdd_tobj, lsname, ltname, ma, ldata, handle);
2676 rc = mdd_trans_start(env, mdd, handle);
2680 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2681 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2683 GOTO(cleanup_unlocked, rc);
2685 is_dir = S_ISDIR(cattr->la_mode);
2687 /* Remove source name from source directory */
2688 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2689 mdd_object_capa(env, mdd_spobj));
2693 /* "mv dir1 dir2" needs "dir1/.." link update */
2694 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2695 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2696 mdd_object_capa(env, mdd_sobj));
2698 GOTO(fixup_spobj2, rc);
2700 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2701 handle, mdd_object_capa(env, mdd_sobj));
2703 GOTO(fixup_spobj, rc);
2706 /* Remove target name from target directory
2707 * Here tobj can be remote one, so we do index_delete unconditionally
2708 * and -ENOENT is allowed.
2710 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2711 mdd_object_capa(env, mdd_tpobj));
2714 /* tname might been renamed to something else */
2715 GOTO(fixup_spobj, rc);
2718 GOTO(fixup_spobj, rc);
2721 /* Insert new fid with target name into target dir */
2722 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2723 mdd_object_capa(env, mdd_tpobj));
2725 GOTO(fixup_tpobj, rc);
2727 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2728 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2730 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2732 la->la_valid = LA_CTIME;
2733 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2735 GOTO(fixup_tpobj, rc);
2738 /* Remove old target object
2739 * For tobj is remote case cmm layer has processed
2740 * and set tobj to NULL then. So when tobj is NOT NULL,
2741 * it must be local one.
2743 if (tobj && mdd_object_exists(mdd_tobj)) {
2744 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2746 if (mdd_is_dead_obj(mdd_tobj)) {
2747 /* shld not be dead, something is wrong */
2748 CERROR("tobj is dead, something is wrong\n");
2752 mdo_ref_del(env, mdd_tobj, handle);
2754 /* Remove dot reference. */
2755 if (S_ISDIR(tattr->la_mode))
2756 mdo_ref_del(env, mdd_tobj, handle);
2759 /* fetch updated nlink */
2760 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2762 CERROR("%s: Failed to get nlink for tobj "
2764 mdd2obd_dev(mdd)->obd_name,
2765 PFID(tpobj_fid), rc);
2766 GOTO(fixup_tpobj, rc);
2769 la->la_valid = LA_CTIME;
2770 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2772 CERROR("%s: Failed to set ctime for tobj "
2774 mdd2obd_dev(mdd)->obd_name,
2775 PFID(tpobj_fid), rc);
2776 GOTO(fixup_tpobj, rc);
2779 /* XXX: this transfer to ma will be removed with LOD/OSP */
2780 ma->ma_attr = *tattr;
2781 ma->ma_valid |= MA_INODE;
2782 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2785 CERROR("%s: Failed to unlink tobj "
2787 mdd2obd_dev(mdd)->obd_name,
2788 PFID(tpobj_fid), rc);
2789 GOTO(fixup_tpobj, rc);
2792 /* fetch updated nlink */
2793 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2795 CERROR("%s: Failed to get nlink for tobj "
2797 mdd2obd_dev(mdd)->obd_name,
2798 PFID(tpobj_fid), rc);
2799 GOTO(fixup_tpobj, rc);
2801 /* XXX: this transfer to ma will be removed with LOD/OSP */
2802 ma->ma_attr = *tattr;
2803 ma->ma_valid |= MA_INODE;
2805 if (tattr->la_nlink == 0) {
2806 cl_flags |= CLF_RENAME_LAST;
2807 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2808 cl_flags |= CLF_RENAME_LAST_EXISTS;
2812 la->la_valid = LA_CTIME | LA_MTIME;
2813 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2815 GOTO(fixup_tpobj, rc);
2817 if (mdd_spobj != mdd_tpobj) {
2818 la->la_valid = LA_CTIME | LA_MTIME;
2819 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2822 if (rc == 0 && mdd_sobj) {
2823 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2824 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2825 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2828 /* Old files might not have EA entry */
2829 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2830 lsname, handle, NULL, 0);
2831 mdd_write_unlock(env, mdd_sobj);
2832 /* We don't fail the transaction if the link ea can't be
2833 updated -- fid2path will use alternate lookup method. */
2841 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2844 CWARN("tp obj fix error %d\n",rc2);
2846 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2847 !mdd_is_dead_obj(mdd_tobj)) {
2849 mdo_ref_add(env, mdd_tobj, handle);
2851 mdo_ref_add(env, mdd_tobj, handle);
2854 rc2 = __mdd_index_insert(env, mdd_tpobj,
2855 mdo2fid(mdd_tobj), tname,
2860 CWARN("tp obj fix error %d\n",rc2);
2865 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2866 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2870 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2871 mdd2obd_dev(mdd)->obd_name, rc2);
2874 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2875 dotdot, handle, BYPASS_CAPA);
2877 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2878 mdd2obd_dev(mdd)->obd_name, rc2);
2883 rc2 = __mdd_index_insert(env, mdd_spobj,
2884 lf, sname, is_dir, handle, BYPASS_CAPA);
2886 CWARN("sp obj fix error %d\n",rc2);
2890 mdd_write_unlock(env, mdd_tobj);
2893 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2894 mdd_tobj, tpobj_fid, lf,
2895 spobj_fid, ltname, lsname,
2899 mdd_trans_stop(env, mdd, rc, handle);
2901 mdd_object_put(env, mdd_sobj);
2906 * During migration once the parent FID has been changed,
2907 * we need update the parent FID in linkea.
2909 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2910 struct mdd_object *parent,
2911 struct mdd_object *child,
2912 const char *name, int namelen,
2913 struct thandle *handle,
2916 struct mdd_thread_info *info = mdd_env_info(env);
2917 struct linkea_data ldata = {0};
2918 struct lu_buf *buf = &info->mti_link_buf;
2924 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2925 if (buf->lb_buf == NULL)
2929 rc = mdd_links_read(env, child, &ldata);
2931 if (rc == -ENOENT || rc == -ENODATA)
2936 LASSERT(ldata.ld_leh != NULL);
2937 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2938 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2939 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2940 struct lu_name lname;
2943 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2946 if (strncmp(lname.ln_name, name, namelen) != 0 ||
2947 lu_fid_eq(&fid, mdd_object_fid(parent))) {
2948 ldata.ld_lee = (struct link_ea_entry *)
2949 ((char *)ldata.ld_lee +
2954 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
2955 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
2956 lname.ln_namelen, lname.ln_name,
2957 PFID(mdd_object_fid(parent)));
2958 /* update to the new parent fid */
2959 linkea_entry_pack(ldata.ld_lee, &lname,
2960 mdd_object_fid(parent));
2962 rc = mdd_declare_links_add(env, child, handle, &ldata);
2964 rc = mdd_links_write(env, child, &ldata, handle);
2970 static int mdd_linkea_declare_update_child(const struct lu_env *env,
2971 struct mdd_object *parent,
2972 struct mdd_object *child,
2973 const char *name, int namelen,
2974 struct thandle *handle)
2976 return mdd_linkea_update_child_internal(env, parent, child, name,
2977 namelen, handle, true);
2980 static int mdd_linkea_update_child(const struct lu_env *env,
2981 struct mdd_object *parent,
2982 struct mdd_object *child,
2983 const char *name, int namelen,
2984 struct thandle *handle)
2986 return mdd_linkea_update_child_internal(env, parent, child, name,
2987 namelen, handle, false);
2990 static int mdd_update_linkea_internal(const struct lu_env *env,
2991 struct mdd_object *mdd_pobj,
2992 struct mdd_object *mdd_sobj,
2993 struct mdd_object *mdd_tobj,
2994 const struct lu_name *child_name,
2995 struct thandle *handle,
2998 struct mdd_thread_info *info = mdd_env_info(env);
2999 struct linkea_data *ldata = &info->mti_link_data;
3004 rc = mdd_links_read(env, mdd_sobj, ldata);
3006 if (rc == -ENOENT || rc == -ENODATA)
3012 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3014 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3019 /* If it is mulitple links file, we need update the name entry for
3021 LASSERT(ldata->ld_leh != NULL);
3022 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3023 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3024 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3025 struct mdd_object *pobj;
3026 struct lu_name lname;
3029 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3031 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3033 pobj = mdd_object_find(env, mdd, &fid);
3035 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3036 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3041 if (!mdd_object_exists(pobj)) {
3042 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3043 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3047 if (pobj == mdd_pobj &&
3048 lname.ln_namelen == child_name->ln_namelen &&
3049 strncmp(lname.ln_name, child_name->ln_name,
3050 lname.ln_namelen) == 0) {
3051 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3052 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3057 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3058 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3059 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3062 /* Remove source name from source directory */
3063 /* Insert new fid with target name into target dir */
3064 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3069 rc = mdo_declare_index_insert(env, pobj,
3070 mdd_object_fid(mdd_tobj),
3071 lname.ln_name, handle);
3075 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3079 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3083 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3085 mdd_object_capa(env, pobj));
3089 rc = __mdd_index_insert(env, pobj,
3090 mdd_object_fid(mdd_tobj),
3091 lname.ln_name, 0, handle,
3092 mdd_object_capa(env, pobj));
3096 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3097 rc = mdo_ref_add(env, mdd_tobj, handle);
3098 mdd_write_unlock(env, mdd_tobj);
3102 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3103 mdo_ref_del(env, mdd_sobj, handle);
3104 mdd_write_unlock(env, mdd_sobj);
3107 mdd_object_put(env, pobj);
3115 static int mdd_migrate_xattrs(const struct lu_env *env,
3116 struct mdd_object *mdd_sobj,
3117 struct mdd_object *mdd_tobj)
3119 struct mdd_thread_info *info = mdd_env_info(env);
3120 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3122 struct thandle *handle;
3128 struct lu_buf list_xbuf;
3131 /* retrieve xattr list from the old object */
3132 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3133 mdd_object_capa(env, mdd_sobj));
3134 if (list_xsize == -ENODATA)
3140 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3141 if (info->mti_big_buf.lb_buf == NULL)
3144 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3145 list_xbuf.lb_len = list_xsize;
3146 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3147 mdd_object_capa(env, mdd_sobj));
3152 xname = list_xbuf.lb_buf;
3154 xlen = strnlen(xname, rem - 1) + 1;
3155 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3156 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3157 strcmp(XATTR_NAME_LMV, xname) == 0)
3160 /* For directory, if there are default layout, migrate here */
3161 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3162 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3165 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3167 mdd_object_capa(env, mdd_sobj));
3168 if (xsize == -ENODATA)
3173 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3174 if (info->mti_link_buf.lb_buf == NULL)
3175 GOTO(out, rc = -ENOMEM);
3177 xbuf.lb_len = xsize;
3178 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3179 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3180 mdd_object_capa(env, mdd_sobj));
3186 handle = mdd_trans_create(env, mdd);
3188 GOTO(out, rc = PTR_ERR(handle));
3190 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3193 GOTO(stop_trans, rc);
3194 /* Note: this transaction is part of migration, and it is not
3195 * the last step of migration, so we set th_local = 1 to avoid
3196 * update last rcvd for this transaction */
3197 handle->th_local = 1;
3198 rc = mdd_trans_start(env, mdd, handle);
3200 GOTO(stop_trans, rc);
3202 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3203 mdd_object_capa(env, mdd_sobj));
3205 GOTO(stop_trans, rc = 0);
3208 GOTO(stop_trans, rc);
3210 mdd_trans_stop(env, mdd, rc, handle);
3215 memmove(xname, xname + xlen, rem);
3221 static int mdd_declare_migrate_create(const struct lu_env *env,
3222 struct mdd_object *mdd_pobj,
3223 struct mdd_object *mdd_sobj,
3224 struct mdd_object *mdd_tobj,
3225 struct md_op_spec *spec,
3227 union lmv_mds_md *mgr_ea,
3228 struct thandle *handle)
3230 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3231 const struct lu_buf *buf;
3235 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3236 handle, spec, NULL);
3240 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3245 if (S_ISLNK(la->la_mode)) {
3246 const char *target_name = spec->u.sp_symname;
3247 int sym_len = strlen(target_name);
3248 const struct lu_buf *buf;
3250 buf = mdd_buf_get_const(env, target_name, sym_len);
3251 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3257 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3258 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3259 spec->u.sp_ea.eadatalen);
3260 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3266 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3267 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3268 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3273 la_flag->la_valid = LA_FLAGS;
3274 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3275 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3276 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3281 static int mdd_migrate_create(const struct lu_env *env,
3282 struct mdd_object *mdd_pobj,
3283 struct mdd_object *mdd_sobj,
3284 struct mdd_object *mdd_tobj,
3287 struct mdd_thread_info *info = mdd_env_info(env);
3288 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3289 struct md_op_spec *spec = &info->mti_spec;
3290 struct lu_buf lmm_buf = { 0 };
3291 struct lu_buf link_buf = { 0 };
3292 const struct lu_buf *buf;
3293 struct thandle *handle;
3294 struct lmv_mds_md_v1 *mgr_ea;
3295 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3296 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3301 /* prepare spec for create */
3302 memset(spec, 0, sizeof(*spec));
3303 spec->sp_cr_lookup = 0;
3304 spec->sp_feat = &dt_directory_features;
3305 if (S_ISLNK(la->la_mode)) {
3306 buf = lu_buf_check_and_alloc(
3307 &mdd_env_info(env)->mti_big_buf,
3310 link_buf.lb_len = la->la_size + 1;
3311 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3313 rc = rc != 0 ? rc : -EFAULT;
3314 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3315 mdd2obd_dev(mdd)->obd_name,
3316 PFID(mdd_object_fid(mdd_sobj)), rc);
3319 spec->u.sp_symname = link_buf.lb_buf;
3320 } else if S_ISREG(la->la_mode) {
3321 /* retrieve lov of the old object */
3322 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3323 if (rc != 0 && rc != -ENODATA)
3325 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3326 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3327 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3328 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3332 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3333 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3334 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3335 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3336 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3337 fid_cpu_to_le(&mgr_ea->lmv_master_fid, mdd_object_fid(mdd_sobj));
3338 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3339 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3341 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3343 handle = mdd_trans_create(env, mdd);
3345 GOTO(out_free, rc = PTR_ERR(handle));
3347 /* Note: this transaction is part of migration, and it is not
3348 * the last step of migration, so we set th_local = 1 to avoid
3349 * update last rcvd for this transaction */
3350 handle->th_local = 1;
3351 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3353 (union lmv_mds_md *)info->mti_xattr_buf,
3356 GOTO(stop_trans, rc);
3358 rc = mdd_trans_start(env, mdd, handle);
3360 GOTO(stop_trans, rc);
3362 /* create the target object */
3363 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3366 GOTO(stop_trans, rc);
3368 /* Set MIGRATE EA on the source inode, so once the migration needs
3369 * to be re-done during failover, the re-do process can locate the
3370 * target object which is already being created. */
3371 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3372 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3373 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3374 handle, mdd_object_capa(env, mdd_sobj));
3376 GOTO(stop_trans, rc);
3378 /* Set immutable flag, so any modification is disabled until
3379 * the migration is done. Once the migration is interrupted,
3380 * if the resume process find the migrating object has both
3381 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3382 * flag and approve the migration */
3383 la_flag->la_valid = LA_FLAGS;
3384 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3385 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3386 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3387 mdd_object_capa(env, mdd_sobj));
3390 mdd_trans_stop(env, mdd, rc, handle);
3392 if (lmm_buf.lb_buf != NULL)
3393 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3397 static int mdd_migrate_entries(const struct lu_env *env,
3398 struct mdd_object *mdd_sobj,
3399 struct mdd_object *mdd_tobj)
3401 struct dt_object *next = mdd_object_child(mdd_sobj);
3402 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3403 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3404 struct thandle *handle;
3406 const struct dt_it_ops *iops;
3409 struct lu_dirent *ent;
3412 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3416 if (!dt_try_as_dir(env, next))
3417 GOTO(out_ent, rc = -ENOTDIR);
3419 * iterate directories
3421 iops = &next->do_index_ops->dio_it;
3422 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3423 mdd_object_capa(env, mdd_sobj));
3425 GOTO(out_ent, rc = PTR_ERR(it));
3427 rc = iops->load(env, it, 0);
3429 rc = iops->next(env, it);
3433 * At this point and across for-loop:
3435 * rc == 0 -> ok, proceed.
3436 * rc > 0 -> end of directory.
3440 struct mdd_object *child;
3441 char *name = mdd_env_info(env)->mti_key;
3445 bool target_exist = false;
3447 len = iops->key_size(env, it);
3451 result = iops->rec(env, it, (struct dt_rec *)ent,
3452 LUDA_FID | LUDA_TYPE);
3453 if (result == -ESTALE)
3460 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3461 recsize = le16_to_cpu(ent->lde_reclen);
3463 /* Insert new fid with target name into target dir */
3464 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3465 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3466 ent->lde_name[1] == '.'))
3469 child = mdd_object_find(env, mdd, &ent->lde_fid);
3471 GOTO(out, rc = PTR_ERR(child));
3473 is_dir = S_ISDIR(lu_object_attr(&child->mod_obj.mo_lu));
3475 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3477 /* Check whether the name has been inserted to the target */
3478 if (dt_try_as_dir(env, dt_tobj)) {
3479 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3481 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3482 (struct dt_rec *)fid,
3483 (struct dt_key *)name,
3484 mdd_object_capa(env, mdd_tobj));
3485 if (unlikely(rc == 0))
3486 target_exist = true;
3489 handle = mdd_trans_create(env, mdd);
3491 GOTO(out, rc = PTR_ERR(handle));
3493 /* Note: this transaction is part of migration, and it is not
3494 * the last step of migration, so we set th_local = 1 to avoid
3495 * updating last rcvd for this transaction */
3496 handle->th_local = 1;
3497 if (likely(!target_exist)) {
3498 rc = mdo_declare_index_insert(env, mdd_tobj,
3505 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3511 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3516 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3520 /* Update .. for child */
3521 rc = mdo_declare_index_delete(env, child, dotdot,
3526 rc = mdo_declare_index_insert(env, child,
3527 mdd_object_fid(mdd_tobj),
3533 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3540 rc = mdd_trans_start(env, mdd, handle);
3542 CERROR("%s: transaction start failed: rc = %d\n",
3543 mdd2obd_dev(mdd)->obd_name, rc);
3547 if (likely(!target_exist)) {
3548 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3549 name, is_dir, handle,
3550 mdd_object_capa(env, mdd_tobj));
3555 rc = mdo_ref_add(env, mdd_tobj, handle);
3562 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3563 mdd_object_capa(env, mdd_sobj));
3568 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3569 mdd_object_capa(env, child));
3573 rc = __mdd_index_insert_only(env, child,
3574 mdd_object_fid(mdd_tobj),
3576 mdd_object_capa(env, child));
3581 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3582 strlen(name), handle);
3585 mdd_object_put(env, child);
3586 mdd_trans_stop(env, mdd, rc, handle);
3590 result = iops->next(env, it);
3591 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3592 GOTO(out, rc = -EINTR);
3594 if (result == -ESTALE)
3596 } while (result == 0);
3599 iops->fini(env, it);
3601 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3605 static int mdd_declare_update_linkea(const struct lu_env *env,
3606 struct mdd_object *mdd_pobj,
3607 struct mdd_object *mdd_sobj,
3608 struct mdd_object *mdd_tobj,
3609 struct thandle *handle,
3610 const struct lu_name *child_name)
3612 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3613 child_name, handle, 1);
3616 static int mdd_update_linkea(const struct lu_env *env,
3617 struct mdd_object *mdd_pobj,
3618 struct mdd_object *mdd_sobj,
3619 struct mdd_object *mdd_tobj,
3620 struct thandle *handle,
3621 const struct lu_name *child_name)
3623 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3624 child_name, handle, 0);
3627 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3628 struct mdd_object *mdd_pobj,
3629 struct mdd_object *mdd_sobj,
3630 struct mdd_object *mdd_tobj,
3631 const struct lu_name *lname,
3633 struct lu_attr *parent_la,
3634 struct thandle *handle)
3636 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3639 /* Revert IMMUTABLE flag */
3640 la_flag->la_valid = LA_FLAGS;
3641 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3642 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3643 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3647 /* delete entry from source dir */
3648 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3652 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3653 mdd_tobj, handle, lname);
3657 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3658 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3664 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3665 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3671 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3672 lname->ln_name, handle);
3676 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3677 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3682 /* delete old object */
3683 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3687 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3688 /* delete old object */
3689 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3692 /* set nlink to 0 */
3693 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3698 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3702 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3707 static int mdd_migrate_update_name(const struct lu_env *env,
3708 struct mdd_object *mdd_pobj,
3709 struct mdd_object *mdd_sobj,
3710 struct mdd_object *mdd_tobj,
3711 const struct lu_name *lname,
3714 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3715 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3716 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3717 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3718 struct thandle *handle;
3719 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3720 const char *name = lname->ln_name;
3724 /* update time for parent */
3725 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3726 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3727 p_la->la_valid = LA_CTIME;
3729 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3733 handle = mdd_trans_create(env, mdd);
3735 RETURN(PTR_ERR(handle));
3737 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3738 lname, so_attr, p_la, handle);
3740 /* If the migration can not be fit in one transaction, just
3741 * leave it in the original MDT */
3743 GOTO(stop_trans, rc = 0);
3745 GOTO(stop_trans, rc);
3748 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3749 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3750 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3751 PFID(mdd_object_fid(mdd_tobj)));
3753 rc = mdd_trans_start(env, mdd, handle);
3755 GOTO(stop_trans, rc);
3757 /* Revert IMMUTABLE flag */
3758 la_flag->la_valid = LA_FLAGS;
3759 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3760 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3761 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3762 mdd_object_capa(env, mdd_pobj));
3764 GOTO(stop_trans, rc);
3766 /* Remove source name from source directory */
3767 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3768 mdd_object_capa(env, mdd_pobj));
3770 GOTO(stop_trans, rc);
3772 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3775 GOTO(stop_trans, rc);
3777 if (S_ISREG(so_attr->la_mode)) {
3778 if (so_attr->la_nlink == 1) {
3779 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3781 mdd_object_capa(env, mdd_sobj));
3782 if (rc != 0 && rc != -ENODATA)
3783 GOTO(stop_trans, rc);
3787 /* Insert new fid with target name into target dir */
3788 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj), name,
3789 is_dir, handle, mdd_object_capa(env, mdd_pobj));
3791 GOTO(stop_trans, rc);
3793 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3796 GOTO(stop_trans, rc);
3798 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3799 mdo_ref_del(env, mdd_sobj, handle);
3801 mdo_ref_del(env, mdd_sobj, handle);
3803 /* Get the attr again after ref_del */
3804 rc = mdd_la_get(env, mdd_sobj, so_attr,
3805 mdd_object_capa(env, mdd_sobj));
3807 GOTO(stop_trans, rc);
3808 ma->ma_attr = *so_attr;
3809 ma->ma_valid |= MA_INODE;
3810 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3812 GOTO(stop_trans, rc);
3814 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3816 GOTO(stop_trans, rc);
3818 mdd_write_unlock(env, mdd_sobj);
3821 mdd_trans_stop(env, mdd, rc, handle);
3827 * Check whether we should migrate the file/dir
3829 * < 0 permission check failed or other error.
3830 * = 0 the file can be migrated.
3831 * > 0 the file does not need to be migrated, mostly
3832 * for multiple link file
3834 static int mdd_migrate_sanity_check(const struct lu_env *env,
3835 struct mdd_object *pobj,
3836 const struct lu_attr *pattr,
3837 struct mdd_object *sobj,
3838 struct lu_attr *sattr)
3840 struct mdd_thread_info *info = mdd_env_info(env);
3841 struct linkea_data *ldata = &info->mti_link_data;
3843 struct lu_buf *mgr_buf;
3849 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3850 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3851 if (mgr_buf->lb_buf == NULL)
3854 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3855 mdd_object_capa(env, sobj));
3857 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3859 /* If the object has migrateEA, it means IMMUTE flag
3860 * is being set by previous migration process, so it
3861 * needs to override the IMMUTE flag, otherwise the
3862 * following sanity check will fail */
3863 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3864 LMV_HASH_FLAG_MIGRATION) {
3865 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3867 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3868 sobj->mod_flags &= ~IMMUTE_OBJ;
3869 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3870 mdd2obd_dev(mdd)->obd_name,
3871 PFID(mdd_object_fid(sobj)));
3875 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3876 sobj, sattr, NULL, NULL);
3880 /* Then it will check if the file should be migrated. If the file
3881 * has mulitple links, we only need migrate the file if all of its
3882 * entries has been migrated to the remote MDT */
3883 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3886 rc = mdd_links_read(env, sobj, ldata);
3888 /* For multiple links files, if there are no linkEA data at all,
3889 * means the file might be created before linkEA is enabled, and
3890 * all all of its links should not be migrated yet, otherwise
3891 * it should have some linkEA there */
3892 if (rc == -ENOENT || rc == -ENODATA)
3897 /* If it is mulitple links file, we need update the name entry for
3899 LASSERT(ldata->ld_leh != NULL);
3900 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3901 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3902 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3903 struct mdd_object *lpobj;
3904 struct lu_name lname;
3907 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3909 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3911 lpobj = mdd_object_find(env, mdd, &fid);
3912 if (IS_ERR(lpobj)) {
3913 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3914 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3919 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3920 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3921 PFID(&fid), lname.ln_namelen, lname.ln_name);
3922 mdd_object_put(env, lpobj);
3926 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3927 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3928 lname.ln_name, PFID(&fid));
3929 mdd_object_put(env, lpobj);
3937 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3938 struct md_object *sobj, const struct lu_name *lname,
3939 struct md_object *tobj, struct md_attr *ma)
3941 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3942 struct mdd_device *mdd = mdo2mdd(pobj);
3943 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
3944 struct mdd_object *mdd_tobj = NULL;
3945 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3946 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3950 /* If the file will being migrated, it will check whether
3951 * the file is being opened by someone else right now */
3952 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3953 if (mdd_sobj->mod_count >= 1) {
3954 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
3955 mdd2obd_dev(mdd)->obd_name,
3956 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3957 mdd_sobj->mod_count, -EBUSY);
3958 mdd_read_unlock(env, mdd_sobj);
3959 GOTO(put, rc = -EBUSY);
3961 mdd_read_unlock(env, mdd_sobj);
3963 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3967 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
3971 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
3978 /* Sigh, it is impossible to finish all of migration in a single
3979 * transaction, for example migrating big directory entries to the
3980 * new MDT, it needs insert all of name entries of children in the
3983 * So migration will be done in multiple steps and transactions.
3985 * 1. create an orphan object on the remote MDT in one transaction.
3986 * 2. migrate extend attributes to the new target file/directory.
3987 * 3. For directory, migrate the entries to the new MDT and update
3988 * linkEA of each children. Because we can not migrate all entries
3989 * in a single transaction, so the migrating directory will become
3990 * a striped directory during migration, so once the process is
3991 * interrupted, the directory is still accessible. (During lookup,
3992 * client will locate the name by searching both original and target
3994 * 4. Finally, update the name/FID to point to the new file/directory
3995 * in a separate transaction.
3998 /* step 1: Check whether the orphan object has been created, and create
3999 * orphan object on the remote MDT if needed */
4000 mdd_tobj = md2mdd_obj(tobj);
4001 if (!mdd_object_exists(mdd_tobj)) {
4002 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4008 /* step 2: migrate xattr */
4009 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4013 /* step 3: migrate name entries to the orphan object */
4014 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4015 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4018 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4019 OBD_FAIL_MDS_REINT_NET_REP)))
4023 /* step 4: update name entry to the new object */
4024 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4032 const struct md_dir_operations mdd_dir_ops = {
4033 .mdo_is_subdir = mdd_is_subdir,
4034 .mdo_lookup = mdd_lookup,
4035 .mdo_create = mdd_create,
4036 .mdo_rename = mdd_rename,
4037 .mdo_link = mdd_link,
4038 .mdo_unlink = mdd_unlink,
4039 .mdo_create_data = mdd_create_data,
4040 .mdo_migrate = mdd_migrate,