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 = {
60 /* Get FID from name and parent */
62 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
63 const struct lu_attr *pattr, const struct lu_name *lname,
64 struct lu_fid* fid, int mask)
66 const char *name = lname->ln_name;
67 const struct dt_key *key = (const struct dt_key *)name;
68 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
69 struct mdd_device *m = mdo2mdd(pobj);
70 struct dt_object *dir = mdd_object_child(mdd_obj);
74 if (unlikely(mdd_is_dead_obj(mdd_obj)))
77 if (mdd_object_remote(mdd_obj)) {
78 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
79 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
80 } else if (!mdd_object_exists(mdd_obj)) {
84 /* The common filename length check. */
85 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
86 RETURN(-ENAMETOOLONG);
88 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
93 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
94 dt_try_as_dir(env, dir))) {
96 rc = dir->do_index_ops->dio_lookup(env, dir,
97 (struct dt_rec *)fid, key,
98 mdd_object_capa(env, mdd_obj));
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
125 static inline int mdd_parent_fid(const struct lu_env *env,
126 struct mdd_object *obj,
127 const struct lu_attr *attr,
130 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
134 * For root fid use special function, which does not compare version component
135 * of fid. Version component is different for root fids on all MDTs.
137 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
139 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
140 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
144 * return 1: if lf is the fid of the ancestor of p1;
147 * return -EREMOTE: if remote object is found, in this
148 * case fid of remote object is saved to @pf;
150 * otherwise: values < 0, errors.
152 static int mdd_is_parent(const struct lu_env *env,
153 struct mdd_device *mdd,
154 struct mdd_object *p1,
155 const struct lu_attr *attr,
156 const struct lu_fid *lf,
159 struct mdd_object *parent = NULL;
164 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
165 pfid = &mdd_env_info(env)->mti_fid;
167 /* Check for root first. */
168 if (mdd_is_root(mdd, mdo2fid(p1)))
172 /* this is done recursively, bypass capa for each obj */
173 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
174 rc = mdd_parent_fid(env, p1, attr, pfid);
177 if (mdd_is_root(mdd, pfid))
179 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
181 if (lu_fid_eq(pfid, lf))
184 mdd_object_put(env, parent);
186 parent = mdd_object_find(env, mdd, pfid);
187 if (IS_ERR(parent)) {
188 GOTO(out, rc = PTR_ERR(parent));
189 } else if (mdd_object_remote(parent)) {
190 /*FIXME: Because of the restriction of rename in Phase I.
191 * If the parent is remote, we just assumed lf is not the
192 * parent of P1 for now */
199 if (parent && !IS_ERR(parent))
200 mdd_object_put(env, parent);
205 * No permission check is needed.
207 * returns 1: if fid is ancestor of @mo;
208 * returns 0: if fid is not a ancestor of @mo;
210 * returns EREMOTE if remote object is found, fid of remote object is saved to
213 * returns < 0: if error
215 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
216 const struct lu_fid *fid, struct lu_fid *sfid)
218 struct mdd_device *mdd = mdo2mdd(mo);
219 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
223 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
226 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
230 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
234 } else if (rc == 1) {
235 /* found @fid is parent */
243 * Check that @dir contains no entries except (possibly) dot and dotdot.
248 * -ENOTDIR not a directory object
249 * -ENOTEMPTY not empty
253 static int mdd_dir_is_empty(const struct lu_env *env,
254 struct mdd_object *dir)
257 struct dt_object *obj;
258 const struct dt_it_ops *iops;
262 obj = mdd_object_child(dir);
263 if (!dt_try_as_dir(env, obj))
266 iops = &obj->do_index_ops->dio_it;
267 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
269 result = iops->get(env, it, (const void *)"");
272 for (result = 0, i = 0; result == 0 && i < 3; ++i)
273 result = iops->next(env, it);
276 else if (result == +1)
278 } else if (result == 0)
280 * Huh? Index contains no zero key?
287 result = PTR_ERR(it);
291 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
292 const struct lu_attr *la)
294 struct mdd_device *m = mdd_obj2mdd_dev(obj);
299 if (!S_ISDIR(la->la_mode))
303 * Subdir count limitation can be broken through.
305 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
312 * Check whether it may create the cobj under the pobj.
315 int mdd_may_create(const struct lu_env *env,
316 struct mdd_object *pobj, const struct lu_attr *pattr,
317 struct mdd_object *cobj, int check_perm, int check_nlink)
322 if (cobj && mdd_object_exists(cobj))
325 if (mdd_is_dead_obj(pobj))
329 rc = mdd_permission_internal_locked(env, pobj, pattr,
330 MAY_WRITE | MAY_EXEC,
332 if (!rc && check_nlink)
333 rc = __mdd_may_link(env, pobj, pattr);
339 * Check whether can unlink from the pobj in the case of "cobj == NULL".
341 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
342 const struct lu_attr *pattr, const struct lu_attr *attr)
347 if (mdd_is_dead_obj(pobj))
350 if ((attr->la_valid & LA_FLAGS) &&
351 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
354 rc = mdd_permission_internal_locked(env, pobj, pattr,
355 MAY_WRITE | MAY_EXEC,
360 if (mdd_is_append(pobj))
367 * pobj == NULL is remote ops case, under such case, pobj's
368 * VTX feature has been checked already, no need check again.
370 static inline int mdd_is_sticky(const struct lu_env *env,
371 struct mdd_object *pobj,
372 const struct lu_attr *pattr,
373 struct mdd_object *cobj,
374 const struct lu_attr *cattr)
376 struct lu_ucred *uc = lu_ucred_assert(env);
379 LASSERT(pattr != NULL);
380 if (!(pattr->la_mode & S_ISVTX) ||
381 (pattr->la_uid == uc->uc_fsuid))
385 LASSERT(cattr != NULL);
386 if (cattr->la_uid == uc->uc_fsuid)
389 return !md_capable(uc, CFS_CAP_FOWNER);
392 static int mdd_may_delete_entry(const struct lu_env *env,
393 struct mdd_object *pobj,
394 const struct lu_attr *pattr,
399 LASSERT(pobj != NULL);
400 if (!mdd_object_exists(pobj))
403 if (mdd_is_dead_obj(pobj))
408 rc = mdd_permission_internal_locked(env, pobj, pattr,
409 MAY_WRITE | MAY_EXEC,
415 if (mdd_is_append(pobj))
422 * Check whether it may delete the cobj from the pobj.
425 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
426 const struct lu_attr *tpattr, struct mdd_object *tobj,
427 const struct lu_attr *tattr, const struct lu_attr *cattr,
428 int check_perm, int check_empty)
434 LASSERT(tpattr != NULL);
435 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
443 if (!mdd_object_exists(tobj))
446 if (mdd_is_dead_obj(tobj))
449 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
452 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
455 if ((tattr->la_valid & LA_FLAGS) &&
456 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
459 /* additional check the rename case */
461 if (S_ISDIR(cattr->la_mode)) {
462 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
464 if (!S_ISDIR(tattr->la_mode))
467 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
469 } else if (S_ISDIR(tattr->la_mode))
473 if (S_ISDIR(tattr->la_mode) && check_empty)
474 rc = mdd_dir_is_empty(env, tobj);
481 * has mdd_write_lock on src already, but not on tgt yet
483 static int mdd_link_sanity_check(const struct lu_env *env,
484 struct mdd_object *tgt_obj,
485 const struct lu_attr *tattr,
486 const struct lu_name *lname,
487 struct mdd_object *src_obj,
488 const struct lu_attr *cattr)
490 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
494 if (!mdd_object_exists(src_obj))
497 if (mdd_is_dead_obj(src_obj))
500 /* Local ops, no lookup before link, check filename length here. */
501 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
502 RETURN(-ENAMETOOLONG);
504 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
507 if (S_ISDIR(mdd_object_type(src_obj)))
510 LASSERT(src_obj != tgt_obj);
512 rc = mdd_may_create(env, tgt_obj, tattr, NULL, 1, 0);
517 rc = __mdd_may_link(env, src_obj, cattr);
522 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
523 const char *name, struct thandle *handle,
524 struct lustre_capa *capa)
526 struct dt_object *next = mdd_object_child(pobj);
530 if (dt_try_as_dir(env, next)) {
531 rc = next->do_index_ops->dio_delete(env, next,
532 (struct dt_key *)name,
540 static int __mdd_index_insert_only(const struct lu_env *env,
541 struct mdd_object *pobj,
542 const struct lu_fid *lf, const char *name,
543 struct thandle *handle,
544 struct lustre_capa *capa)
546 struct dt_object *next = mdd_object_child(pobj);
550 if (dt_try_as_dir(env, next)) {
551 struct lu_ucred *uc = lu_ucred_check(env);
554 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
555 rc = next->do_index_ops->dio_insert(env, next,
557 (const struct dt_key *)name,
558 handle, capa, ignore_quota);
565 /* insert named index, add reference if isdir */
566 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
567 const struct lu_fid *lf, const char *name, int is_dir,
568 struct thandle *handle, struct lustre_capa *capa)
573 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
574 if (rc == 0 && is_dir) {
575 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
576 mdo_ref_add(env, pobj, handle);
577 mdd_write_unlock(env, pobj);
582 /* delete named index, drop reference if isdir */
583 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
584 const char *name, int is_dir, struct thandle *handle,
585 struct lustre_capa *capa)
590 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
591 if (rc == 0 && is_dir) {
592 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
593 mdo_ref_del(env, pobj, handle);
594 mdd_write_unlock(env, pobj);
600 int mdd_declare_changelog_store(const struct lu_env *env,
601 struct mdd_device *mdd,
602 const struct lu_name *fname,
603 struct thandle *handle)
605 struct obd_device *obd = mdd2obd_dev(mdd);
606 struct llog_ctxt *ctxt;
607 struct llog_changelog_rec *rec;
613 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
616 reclen = llog_data_len(sizeof(*rec) +
617 (fname != NULL ? fname->ln_namelen : 0));
618 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
619 if (buf->lb_buf == NULL)
623 rec->cr_hdr.lrh_len = reclen;
624 rec->cr_hdr.lrh_type = CHANGELOG_REC;
626 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
630 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
636 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
637 struct mdd_device *mdd,
638 const struct lu_name *tname,
639 const struct lu_name *sname,
640 struct thandle *handle)
642 struct obd_device *obd = mdd2obd_dev(mdd);
643 struct llog_ctxt *ctxt;
644 struct llog_changelog_ext_rec *rec;
650 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
653 reclen = llog_data_len(sizeof(*rec) +
654 (tname != NULL ? tname->ln_namelen : 0) +
655 (sname != NULL ? 1 + sname->ln_namelen : 0));
656 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
657 if (buf->lb_buf == NULL)
661 rec->cr_hdr.lrh_len = reclen;
662 rec->cr_hdr.lrh_type = CHANGELOG_REC;
664 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
668 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
674 /** Add a changelog entry \a rec to the changelog llog
677 * \param handle - currently ignored since llogs start their own transaction;
678 * this will hopefully be fixed in llog rewrite
681 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
682 struct llog_changelog_rec *rec, struct thandle *th)
684 struct obd_device *obd = mdd2obd_dev(mdd);
685 struct llog_ctxt *ctxt;
688 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
689 rec->cr_hdr.lrh_type = CHANGELOG_REC;
690 rec->cr.cr_time = cl_time();
692 spin_lock(&mdd->mdd_cl.mc_lock);
693 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
694 * but as long as the MDD transactions are ordered correctly for e.g.
695 * rename conflicts, I don't think this should matter. */
696 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
697 spin_unlock(&mdd->mdd_cl.mc_lock);
699 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
703 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
710 /** Add a changelog_ext entry \a rec to the changelog llog
713 * \param handle - currently ignored since llogs start their own transaction;
714 * this will hopefully be fixed in llog rewrite
717 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
718 struct llog_changelog_ext_rec *rec,
721 struct obd_device *obd = mdd2obd_dev(mdd);
722 struct llog_ctxt *ctxt;
725 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
726 /* llog_lvfs_write_rec sets the llog tail len */
727 rec->cr_hdr.lrh_type = CHANGELOG_REC;
728 rec->cr.cr_time = cl_time();
730 spin_lock(&mdd->mdd_cl.mc_lock);
731 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
732 * but as long as the MDD transactions are ordered correctly for e.g.
733 * rename conflicts, I don't think this should matter. */
734 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
735 spin_unlock(&mdd->mdd_cl.mc_lock);
737 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
741 /* nested journal transaction */
742 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
750 /** Store a namespace change changelog record
751 * If this fails, we must fail the whole transaction; we don't
752 * want the change to commit without the log entry.
753 * \param target - mdd_object of change
754 * \param parent - parent dir/object
755 * \param tname - target name string
756 * \param handle - transacion handle
758 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
759 enum changelog_rec_type type, unsigned flags,
760 struct mdd_object *target, struct mdd_object *parent,
761 const struct lu_name *tname, struct thandle *handle)
763 struct llog_changelog_rec *rec;
770 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
772 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
775 LASSERT(target != NULL);
776 LASSERT(parent != NULL);
777 LASSERT(tname != NULL);
778 LASSERT(handle != NULL);
780 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
781 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
782 if (buf->lb_buf == NULL)
786 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
787 rec->cr.cr_type = (__u32)type;
788 rec->cr.cr_tfid = *mdo2fid(target);
789 rec->cr.cr_pfid = *mdo2fid(parent);
790 rec->cr.cr_namelen = tname->ln_namelen;
791 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
793 target->mod_cltime = cfs_time_current_64();
795 rc = mdd_changelog_store(env, mdd, rec, handle);
797 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
798 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
799 PFID(&rec->cr.cr_pfid));
807 /** Store a namespace change changelog record
808 * If this fails, we must fail the whole transaction; we don't
809 * want the change to commit without the log entry.
810 * \param target - mdd_object of change
811 * \param tpfid - target parent dir/object fid
812 * \param sfid - source object fid
813 * \param spfid - source parent fid
814 * \param tname - target name string
815 * \param sname - source name string
816 * \param handle - transacion handle
818 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
819 struct mdd_device *mdd,
820 enum changelog_rec_type type,
822 struct mdd_object *target,
823 const struct lu_fid *tpfid,
824 const struct lu_fid *sfid,
825 const struct lu_fid *spfid,
826 const struct lu_name *tname,
827 const struct lu_name *sname,
828 struct thandle *handle)
830 struct llog_changelog_ext_rec *rec;
837 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
839 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
842 LASSERT(sfid != NULL);
843 LASSERT(tpfid != NULL);
844 LASSERT(tname != NULL);
845 LASSERT(handle != NULL);
847 reclen = llog_data_len(sizeof(*rec) +
848 sname != NULL ? 1 + sname->ln_namelen : 0);
849 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
850 if (buf->lb_buf == NULL)
854 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
855 rec->cr.cr_type = (__u32)type;
856 rec->cr.cr_pfid = *tpfid;
857 rec->cr.cr_sfid = *sfid;
858 rec->cr.cr_spfid = *spfid;
859 rec->cr.cr_namelen = tname->ln_namelen;
860 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
862 rec->cr.cr_name[tname->ln_namelen] = '\0';
863 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
865 rec->cr.cr_namelen += 1 + sname->ln_namelen;
868 if (likely(target != NULL)) {
869 rec->cr.cr_tfid = *mdo2fid(target);
870 target->mod_cltime = cfs_time_current_64();
872 fid_zero(&rec->cr.cr_tfid);
875 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
877 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
878 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
885 static int __mdd_links_add(const struct lu_env *env,
886 struct mdd_object *mdd_obj,
887 struct linkea_data *ldata,
888 const struct lu_name *lname,
889 const struct lu_fid *pfid,
890 int first, int check)
894 if (ldata->ld_leh == NULL) {
895 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
899 rc = linkea_data_new(ldata,
900 &mdd_env_info(env)->mti_link_buf);
907 rc = linkea_links_find(ldata, lname, pfid);
908 if (rc && rc != -ENOENT)
914 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
915 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
919 linkea_add_buf(ldata, lname, tfid);
922 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
923 linkea_add_buf(ldata, lname, pfid);
925 return linkea_add_buf(ldata, lname, pfid);
928 static int __mdd_links_del(const struct lu_env *env,
929 struct mdd_object *mdd_obj,
930 struct linkea_data *ldata,
931 const struct lu_name *lname,
932 const struct lu_fid *pfid)
936 if (ldata->ld_leh == NULL) {
937 rc = mdd_links_read(env, mdd_obj, ldata);
942 rc = linkea_links_find(ldata, lname, pfid);
946 linkea_del_buf(ldata, lname);
950 static int mdd_linkea_prepare(const struct lu_env *env,
951 struct mdd_object *mdd_obj,
952 const struct lu_fid *oldpfid,
953 const struct lu_name *oldlname,
954 const struct lu_fid *newpfid,
955 const struct lu_name *newlname,
956 int first, int check,
957 struct linkea_data *ldata)
963 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
966 LASSERT(oldpfid != NULL || newpfid != NULL);
968 if (mdd_obj->mod_flags & DEAD_OBJ) {
969 /* Prevent linkea to be updated which is NOT necessary. */
970 ldata->ld_reclen = 0;
971 /* No more links, don't bother */
975 if (oldpfid != NULL) {
976 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
979 (rc != -ENODATA && rc != -ENOENT))
981 /* No changes done. */
986 /* If renaming, add the new record */
987 if (newpfid != NULL) {
988 /* even if the add fails, we still delete the out-of-date
990 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
994 rc = rc != 0 ? rc : rc2;
999 int mdd_links_rename(const struct lu_env *env,
1000 struct mdd_object *mdd_obj,
1001 const struct lu_fid *oldpfid,
1002 const struct lu_name *oldlname,
1003 const struct lu_fid *newpfid,
1004 const struct lu_name *newlname,
1005 struct thandle *handle,
1006 struct linkea_data *ldata,
1007 int first, int check)
1013 if (ldata == NULL) {
1014 ldata = &mdd_env_info(env)->mti_link_data;
1015 memset(ldata, 0, sizeof(*ldata));
1016 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1017 newpfid, newlname, first, check,
1023 if (ldata->ld_reclen != 0)
1024 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1031 if (rc == -EOVERFLOW || rc == -ENOSPC)
1033 if (oldpfid == NULL)
1034 CDEBUG(error ? D_ERROR : D_OTHER,
1035 "link_ea add '%.*s' failed %d "DFID"\n",
1036 newlname->ln_namelen, newlname->ln_name,
1037 rc, PFID(mdd_object_fid(mdd_obj)));
1038 else if (newpfid == NULL)
1039 CDEBUG(error ? D_ERROR : D_OTHER,
1040 "link_ea del '%.*s' failed %d "DFID"\n",
1041 oldlname->ln_namelen, oldlname->ln_name,
1042 rc, PFID(mdd_object_fid(mdd_obj)));
1044 CDEBUG(error ? D_ERROR : D_OTHER,
1045 "link_ea rename '%.*s'->'%.*s' failed %d "
1047 oldlname->ln_namelen, oldlname->ln_name,
1048 newlname->ln_namelen, newlname->ln_name,
1049 rc, PFID(mdd_object_fid(mdd_obj)));
1052 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1053 /* if we vmalloced a large buffer drop it */
1054 lu_buf_free(ldata->ld_buf);
1059 static inline int mdd_links_add(const struct lu_env *env,
1060 struct mdd_object *mdd_obj,
1061 const struct lu_fid *pfid,
1062 const struct lu_name *lname,
1063 struct thandle *handle,
1064 struct linkea_data *data, int first)
1066 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1067 pfid, lname, handle, data, first, 0);
1070 static inline int mdd_links_del(const struct lu_env *env,
1071 struct mdd_object *mdd_obj,
1072 const struct lu_fid *pfid,
1073 const struct lu_name *lname,
1074 struct thandle *handle)
1076 return mdd_links_rename(env, mdd_obj, pfid, lname,
1077 NULL, NULL, handle, NULL, 0, 0);
1080 /** Read the link EA into a temp buffer.
1081 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1082 * A pointer to the buffer is stored in \a ldata::ld_buf.
1084 * \retval 0 or error
1086 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1087 struct linkea_data *ldata)
1091 if (!mdd_object_exists(mdd_obj))
1094 /* First try a small buf */
1095 LASSERT(env != NULL);
1096 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1098 if (ldata->ld_buf->lb_buf == NULL)
1101 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1103 if (rc == -ERANGE) {
1104 /* Buf was too small, figure out what we need. */
1105 lu_buf_free(ldata->ld_buf);
1106 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1107 XATTR_NAME_LINK, BYPASS_CAPA);
1110 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1111 if (ldata->ld_buf->lb_buf == NULL)
1113 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1114 XATTR_NAME_LINK, BYPASS_CAPA);
1117 lu_buf_free(ldata->ld_buf);
1118 ldata->ld_buf = NULL;
1122 return linkea_init(ldata);
1125 /** Read the link EA into a temp buffer.
1126 * Uses the name_buf since it is generally large.
1127 * \retval IS_ERR err
1128 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1130 struct lu_buf *mdd_links_get(const struct lu_env *env,
1131 struct mdd_object *mdd_obj)
1133 struct linkea_data ldata = { 0 };
1136 rc = mdd_links_read(env, mdd_obj, &ldata);
1137 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1140 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1141 struct linkea_data *ldata, struct thandle *handle)
1143 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1144 ldata->ld_leh->leh_len);
1145 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1146 mdd_object_capa(env, mdd_obj));
1149 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1150 struct thandle *handle, struct linkea_data *ldata)
1156 if (ldata != NULL && ldata->ld_leh != NULL) {
1157 ea_len = ldata->ld_leh->leh_len;
1158 linkea = ldata->ld_buf->lb_buf;
1160 ea_len = DEFAULT_LINKEA_SIZE;
1164 /* XXX: max size? */
1165 rc = mdo_declare_xattr_set(env, mdd_obj,
1166 mdd_buf_get_const(env, linkea, ea_len),
1167 XATTR_NAME_LINK, 0, handle);
1171 static inline int mdd_declare_links_del(const struct lu_env *env,
1172 struct mdd_object *c,
1173 struct thandle *handle)
1177 /* For directory, the linkEA will be removed together
1178 * with the object. */
1179 if (!S_ISDIR(mdd_object_type(c)))
1180 rc = mdd_declare_links_add(env, c, handle, NULL);
1185 static int mdd_declare_link(const struct lu_env *env,
1186 struct mdd_device *mdd,
1187 struct mdd_object *p,
1188 struct mdd_object *c,
1189 const struct lu_name *name,
1190 struct thandle *handle,
1192 struct linkea_data *data)
1196 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1200 rc = mdo_declare_ref_add(env, c, handle);
1204 la->la_valid = LA_CTIME | LA_MTIME;
1205 rc = mdo_declare_attr_set(env, p, la, handle);
1209 la->la_valid = LA_CTIME;
1210 rc = mdo_declare_attr_set(env, c, la, handle);
1214 rc = mdd_declare_links_add(env, c, handle, data);
1218 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1223 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1224 struct md_object *src_obj, const struct lu_name *lname,
1227 const char *name = lname->ln_name;
1228 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1229 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1230 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1231 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1232 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1233 struct mdd_device *mdd = mdo2mdd(src_obj);
1234 struct thandle *handle;
1235 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1239 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1243 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1247 handle = mdd_trans_create(env, mdd);
1249 GOTO(out_pending, rc = PTR_ERR(handle));
1251 memset(ldata, 0, sizeof(*ldata));
1253 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1254 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1256 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1261 rc = mdd_trans_start(env, mdd, handle);
1265 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1266 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1269 GOTO(out_unlock, rc);
1271 rc = mdo_ref_add(env, mdd_sobj, handle);
1273 GOTO(out_unlock, rc);
1276 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1278 mdd_object_capa(env, mdd_tobj));
1280 mdo_ref_del(env, mdd_sobj, handle);
1281 GOTO(out_unlock, rc);
1284 la->la_valid = LA_CTIME | LA_MTIME;
1285 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1287 GOTO(out_unlock, rc);
1289 la->la_valid = LA_CTIME;
1290 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1292 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1293 mdo2fid(mdd_tobj), lname, 0, 0,
1296 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1297 lname, handle, ldata, 0);
1298 /* The failure of links_add should not cause the link
1299 * failure, reset rc here */
1304 mdd_write_unlock(env, mdd_sobj);
1306 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1307 mdd_tobj, lname, handle);
1309 mdd_trans_stop(env, mdd, rc, handle);
1311 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1312 /* if we vmalloced a large buffer drop it */
1313 lu_buf_free(ldata->ld_buf);
1318 static int mdd_declare_finish_unlink(const struct lu_env *env,
1319 struct mdd_object *obj,
1320 struct thandle *handle)
1324 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1328 return mdo_declare_destroy(env, obj, handle);
1331 /* caller should take a lock before calling */
1332 int mdd_finish_unlink(const struct lu_env *env,
1333 struct mdd_object *obj, struct md_attr *ma,
1334 const struct mdd_object *pobj,
1335 const struct lu_name *lname,
1339 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1342 LASSERT(mdd_write_locked(env, obj) != 0);
1344 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1345 obj->mod_flags |= DEAD_OBJ;
1347 /* add new orphan and the object
1348 * will be deleted during mdd_close() */
1349 if (obj->mod_count) {
1350 rc = __mdd_orphan_add(env, obj, th);
1352 CDEBUG(D_HA, "Object "DFID" is inserted into "
1353 "orphan list, open count = %d\n",
1354 PFID(mdd_object_fid(obj)),
1357 CERROR("Object "DFID" fail to be an orphan, "
1358 "open count = %d, maybe cause failed "
1360 PFID(mdd_object_fid(obj)),
1363 rc = mdo_destroy(env, obj, th);
1365 } else if (!is_dir) {
1366 /* old files may not have link ea; ignore errors */
1367 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1375 * has mdd_write_lock on cobj already, but not on pobj yet
1377 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1378 const struct lu_attr *pattr,
1379 struct mdd_object *cobj,
1380 const struct lu_attr *cattr)
1385 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1390 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1391 struct mdd_object *p, struct mdd_object *c,
1392 const struct lu_name *name, struct md_attr *ma,
1393 struct thandle *handle, int no_name)
1395 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1398 if (likely(no_name == 0)) {
1399 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1404 rc = mdo_declare_ref_del(env, p, handle);
1408 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1409 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1410 la->la_valid = LA_CTIME | LA_MTIME;
1411 rc = mdo_declare_attr_set(env, p, la, handle);
1416 rc = mdo_declare_ref_del(env, c, handle);
1420 rc = mdo_declare_ref_del(env, c, handle);
1424 la->la_valid = LA_CTIME;
1425 rc = mdo_declare_attr_set(env, c, la, handle);
1429 rc = mdd_declare_finish_unlink(env, c, handle);
1433 rc = mdd_declare_links_del(env, c, handle);
1437 /* FIXME: need changelog for remove entry */
1438 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1445 * test if a file has an HSM archive
1446 * if HSM attributes are not found in ma update them from
1449 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1450 struct mdd_object *obj,
1455 if (!(ma->ma_valid & MA_HSM)) {
1456 /* no HSM MD provided, read xattr */
1457 struct lu_buf *hsm_buf;
1458 const size_t buflen = sizeof(struct hsm_attrs);
1461 hsm_buf = mdd_buf_get(env, NULL, 0);
1462 lu_buf_alloc(hsm_buf, buflen);
1463 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1464 mdd_object_capa(env, obj));
1465 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1466 lu_buf_free(hsm_buf);
1470 ma->ma_valid = MA_HSM;
1472 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1478 * Delete name entry and the object.
1479 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1480 * does not exist for this object, and it could only happen during resending
1481 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1482 * is also needed in this case(needed by changelog), so we have to add another
1483 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1484 * the ENOENT failure should be able to be fixed by redo mechanism.
1486 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1487 struct md_object *cobj, const struct lu_name *lname,
1488 struct md_attr *ma, int no_name)
1490 const char *name = lname->ln_name;
1491 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1492 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1493 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1494 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1495 struct mdd_object *mdd_cobj = NULL;
1496 struct mdd_device *mdd = mdo2mdd(pobj);
1497 struct thandle *handle;
1501 /* cobj == NULL means only delete name entry */
1502 if (likely(cobj != NULL)) {
1503 mdd_cobj = md2mdd_obj(cobj);
1504 if (mdd_object_exists(mdd_cobj) == 0)
1508 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1512 if (likely(mdd_cobj != NULL)) {
1514 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1518 is_dir = S_ISDIR(cattr->la_mode);
1521 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1525 handle = mdd_trans_create(env, mdd);
1527 RETURN(PTR_ERR(handle));
1529 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1530 lname, ma, handle, no_name);
1534 rc = mdd_trans_start(env, mdd, handle);
1538 if (likely(mdd_cobj != NULL))
1539 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1541 if (likely(no_name == 0)) {
1542 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1543 mdd_object_capa(env, mdd_pobj));
1548 if (likely(mdd_cobj != NULL)) {
1549 rc = mdo_ref_del(env, mdd_cobj, handle);
1551 __mdd_index_insert_only(env, mdd_pobj,
1554 mdd_object_capa(env, mdd_pobj));
1560 mdo_ref_del(env, mdd_cobj, handle);
1562 /* fetch updated nlink */
1563 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1568 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1569 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1571 la->la_valid = LA_CTIME | LA_MTIME;
1572 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1576 /* Enough for only unlink the entry */
1577 if (unlikely(mdd_cobj == NULL))
1580 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1581 /* update ctime of an unlinked file only if it is still
1582 * opened or a link still exists */
1583 la->la_valid = LA_CTIME;
1584 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1589 /* XXX: this transfer to ma will be removed with LOD/OSP */
1590 ma->ma_attr = *cattr;
1591 ma->ma_valid |= MA_INODE;
1592 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1594 /* fetch updated nlink */
1596 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1598 /* if object is removed then we can't get its attrs, use last get */
1599 if (cattr->la_nlink == 0) {
1600 ma->ma_attr = *cattr;
1601 ma->ma_valid |= MA_INODE;
1605 mdd_write_unlock(env, mdd_cobj);
1609 if (cattr->la_nlink == 0) {
1610 cl_flags |= CLF_UNLINK_LAST;
1611 /* search for an existing archive */
1612 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1613 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1616 rc = mdd_changelog_ns_store(env, mdd,
1617 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1618 mdd_cobj, mdd_pobj, lname, handle);
1622 mdd_trans_stop(env, mdd, rc, handle);
1628 * The permission has been checked when obj created, no need check again.
1630 static int mdd_cd_sanity_check(const struct lu_env *env,
1631 struct mdd_object *obj)
1636 if (!obj || mdd_is_dead_obj(obj))
1642 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1643 struct md_object *cobj, const struct md_op_spec *spec,
1646 struct mdd_device *mdd = mdo2mdd(cobj);
1647 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1648 struct mdd_object *son = md2mdd_obj(cobj);
1649 struct thandle *handle;
1650 const struct lu_buf *buf;
1651 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1652 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1656 rc = mdd_cd_sanity_check(env, son);
1660 if (!md_should_create(spec->sp_cr_flags))
1664 * there are following use cases for this function:
1665 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1666 * striping can be specified or not
1669 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1673 /* calling ->ah_make_hint() is used to transfer information from parent */
1674 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1676 handle = mdd_trans_create(env, mdd);
1678 GOTO(out_free, rc = PTR_ERR(handle));
1681 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1682 * Should this be fixed?
1684 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1685 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1686 spec->sp_cr_flags, spec->no_create);
1688 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1689 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1690 spec->u.sp_ea.eadatalen);
1695 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1696 XATTR_NAME_LOV, 0, handle);
1700 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1704 rc = mdd_trans_start(env, mdd, handle);
1708 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1709 0, handle, mdd_object_capa(env, son));
1714 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1717 mdd_trans_stop(env, mdd, rc, handle);
1722 static int mdd_declare_object_initialize(const struct lu_env *env,
1723 struct mdd_object *parent,
1724 struct mdd_object *child,
1725 struct lu_attr *attr,
1726 struct thandle *handle)
1732 * inode mode has been set in creation time, and it's based on umask,
1733 * la_mode and acl, don't set here again! (which will go wrong
1734 * because below function doesn't consider umask).
1735 * I'd suggest set all object attributes in creation time, see above.
1737 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1738 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1739 rc = mdo_declare_attr_set(env, child, attr, handle);
1740 attr->la_valid |= LA_MODE | LA_TYPE;
1741 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1742 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1745 rc = mdo_declare_ref_add(env, child, handle);
1747 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1754 static int mdd_object_initialize(const struct lu_env *env,
1755 const struct lu_fid *pfid,
1756 struct mdd_object *child,
1757 struct lu_attr *attr, struct thandle *handle,
1758 const struct md_op_spec *spec)
1764 * Update attributes for child.
1767 * (1) the valid bits should be converted between Lustre and Linux;
1768 * (2) maybe, the child attributes should be set in OSD when creation.
1771 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1772 /* arguments are supposed to stay the same */
1773 if (S_ISDIR(attr->la_mode)) {
1774 /* Add "." and ".." for newly created dir */
1775 mdo_ref_add(env, child, handle);
1776 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1777 dot, handle, BYPASS_CAPA);
1779 rc = __mdd_index_insert_only(env, child, pfid,
1783 mdo_ref_del(env, child, handle);
1789 /* has not lock on pobj yet */
1790 static int mdd_create_sanity_check(const struct lu_env *env,
1791 struct md_object *pobj,
1792 const struct lu_attr *pattr,
1793 const struct lu_name *lname,
1794 struct lu_attr *cattr,
1795 struct md_op_spec *spec)
1797 struct mdd_thread_info *info = mdd_env_info(env);
1798 struct lu_fid *fid = &info->mti_fid;
1799 struct mdd_object *obj = md2mdd_obj(pobj);
1800 struct mdd_device *m = mdo2mdd(pobj);
1805 if (mdd_is_dead_obj(obj))
1809 * In some cases this lookup is not needed - we know before if name
1810 * exists or not because MDT performs lookup for it.
1811 * name length check is done in lookup.
1813 if (spec->sp_cr_lookup) {
1815 * Check if the name already exist, though it will be checked in
1816 * _index_insert also, for avoiding rolling back if exists
1819 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1820 MAY_WRITE | MAY_EXEC);
1822 RETURN(rc ? : -EEXIST);
1825 * Check WRITE permission for the parent.
1826 * EXEC permission have been checked
1827 * when lookup before create already.
1829 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1836 if (pattr->la_mode & S_ISGID) {
1837 cattr->la_gid = pattr->la_gid;
1838 if (S_ISDIR(cattr->la_mode)) {
1839 cattr->la_mode |= S_ISGID;
1840 cattr->la_valid |= LA_MODE;
1844 switch (cattr->la_mode & S_IFMT) {
1846 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1848 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1849 RETURN(-ENAMETOOLONG);
1868 static int mdd_declare_object_create(const struct lu_env *env,
1869 struct mdd_device *mdd,
1870 struct mdd_object *p, struct mdd_object *c,
1871 struct lu_attr *attr,
1872 struct thandle *handle,
1873 const struct md_op_spec *spec,
1874 struct lu_buf *def_acl_buf,
1875 struct lu_buf *acl_buf,
1876 struct dt_allocation_hint *hint)
1880 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1885 #ifdef CONFIG_FS_POSIX_ACL
1886 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1887 /* if dir, then can inherit default ACl */
1888 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1889 XATTR_NAME_ACL_DEFAULT,
1895 if (acl_buf->lb_len > 0) {
1896 rc = mdo_declare_attr_set(env, c, attr, handle);
1900 rc = mdo_declare_xattr_set(env, c, acl_buf,
1901 XATTR_NAME_ACL_ACCESS, 0, handle);
1906 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1910 /* replay case, create LOV EA from client data */
1911 if (spec->no_create ||
1912 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
1913 const struct lu_buf *buf;
1915 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1916 spec->u.sp_ea.eadatalen);
1917 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
1923 if (S_ISLNK(attr->la_mode)) {
1924 const char *target_name = spec->u.sp_symname;
1925 int sym_len = strlen(target_name);
1926 const struct lu_buf *buf;
1928 buf = mdd_buf_get_const(env, target_name, sym_len);
1929 rc = dt_declare_record_write(env, mdd_object_child(c),
1938 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1939 struct mdd_object *p, struct mdd_object *c,
1940 const struct lu_name *name,
1941 struct lu_attr *attr,
1942 struct thandle *handle,
1943 const struct md_op_spec *spec,
1944 struct linkea_data *ldata,
1945 struct lu_buf *def_acl_buf,
1946 struct lu_buf *acl_buf,
1947 struct dt_allocation_hint *hint)
1951 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
1952 def_acl_buf, acl_buf, hint);
1956 if (S_ISDIR(attr->la_mode)) {
1957 rc = mdo_declare_ref_add(env, p, handle);
1962 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE) {
1963 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1967 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1969 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,
1973 rc = mdd_declare_links_add(env, c, handle, ldata);
1978 la->la_valid = LA_CTIME | LA_MTIME;
1979 rc = mdo_declare_attr_set(env, p, la, handle);
1984 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1988 /* XXX: For remote create, it should indicate the remote RPC
1989 * will be sent after local transaction is finished, which
1990 * is not very nice, but it will be removed once we fully support
1992 if (mdd_object_remote(p) && handle->th_update != NULL)
1993 handle->th_update->tu_sent_after_local_trans = 1;
1998 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1999 struct lu_attr *la, struct lu_buf *def_acl_buf,
2000 struct lu_buf *acl_buf)
2005 if (S_ISLNK(la->la_mode)) {
2006 acl_buf->lb_len = 0;
2007 def_acl_buf->lb_len = 0;
2011 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2012 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2013 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2014 mdd_read_unlock(env, pobj);
2016 /* If there are default ACL, fix mode/ACL by default ACL */
2017 def_acl_buf->lb_len = rc;
2018 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2019 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2020 acl_buf->lb_len = rc;
2021 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2024 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2025 /* If there are no default ACL, fix mode by mask */
2026 struct lu_ucred *uc = lu_ucred(env);
2028 /* The create triggered by MDT internal events, such as
2029 * LFSCK reset, will not contain valid "uc". */
2030 if (unlikely(uc != NULL))
2031 la->la_mode &= ~uc->uc_umask;
2033 acl_buf->lb_len = 0;
2034 def_acl_buf->lb_len = 0;
2041 * Create a metadata object and initialize it, set acl, xattr.
2043 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2044 struct mdd_object *son, struct lu_attr *attr,
2045 struct md_op_spec *spec, struct lu_buf *acl_buf,
2046 struct lu_buf *def_acl_buf,
2047 struct dt_allocation_hint *hint,
2048 struct thandle *handle)
2052 mdd_write_lock(env, son, MOR_TGT_CHILD);
2053 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2058 #ifdef CONFIG_FS_POSIX_ACL
2059 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2060 S_ISDIR(attr->la_mode)) {
2061 /* set default acl */
2062 rc = mdo_xattr_set(env, son, def_acl_buf,
2063 XATTR_NAME_ACL_DEFAULT, 0,
2064 handle, BYPASS_CAPA);
2066 GOTO(err_destroy, rc);
2068 /* set its own acl */
2069 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2070 rc = mdo_xattr_set(env, son, acl_buf,
2071 XATTR_NAME_ACL_ACCESS,
2072 0, handle, BYPASS_CAPA);
2074 GOTO(err_destroy, rc);
2078 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2081 GOTO(err_destroy, rc);
2084 * in case of replay we just set LOVEA provided by the client
2085 * XXX: I think it would be interesting to try "old" way where
2086 * MDT calls this xattr_set(LOV) in a different transaction.
2087 * probably this way we code can be made better.
2089 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA &&
2090 S_ISREG(attr->la_mode))) {
2091 const struct lu_buf *buf;
2093 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2094 spec->u.sp_ea.eadatalen);
2095 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2098 GOTO(err_destroy, rc);
2101 if (S_ISLNK(attr->la_mode)) {
2102 struct lu_ucred *uc = lu_ucred_assert(env);
2103 struct dt_object *dt = mdd_object_child(son);
2104 const char *target_name = spec->u.sp_symname;
2105 int sym_len = strlen(target_name);
2106 const struct lu_buf *buf;
2109 buf = mdd_buf_get_const(env, target_name, sym_len);
2110 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2111 mdd_object_capa(env, son),
2113 CFS_CAP_SYS_RESOURCE_MASK);
2118 GOTO(err_initlized, rc = -EFAULT);
2122 if (unlikely(rc != 0)) {
2124 if (S_ISDIR(attr->la_mode)) {
2125 /* Drop the reference, no need to delete "."/"..",
2126 * because the object to be destroied directly. */
2127 rc2 = mdo_ref_del(env, son, handle);
2131 rc2 = mdo_ref_del(env, son, handle);
2135 mdo_destroy(env, son, handle);
2138 mdd_write_unlock(env, son);
2143 * Create object and insert it into namespace.
2145 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2146 const struct lu_name *lname, struct md_object *child,
2147 struct md_op_spec *spec, struct md_attr* ma)
2149 struct mdd_thread_info *info = mdd_env_info(env);
2150 struct lu_attr *la = &info->mti_la_for_fix;
2151 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2152 struct mdd_object *son = md2mdd_obj(child);
2153 struct mdd_device *mdd = mdo2mdd(pobj);
2154 struct lu_attr *attr = &ma->ma_attr;
2155 struct thandle *handle;
2156 struct lu_attr *pattr = &info->mti_pattr;
2157 struct lu_buf acl_buf;
2158 struct lu_buf def_acl_buf;
2159 struct linkea_data *ldata = &info->mti_link_data;
2160 const char *name = lname->ln_name;
2161 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2166 * Two operations have to be performed:
2168 * - an allocation of a new object (->do_create()), and
2170 * - an insertion into a parent index (->dio_insert()).
2172 * Due to locking, operation order is not important, when both are
2173 * successful, *but* error handling cases are quite different:
2175 * - if insertion is done first, and following object creation fails,
2176 * insertion has to be rolled back, but this operation might fail
2177 * also leaving us with dangling index entry.
2179 * - if creation is done first, is has to be undone if insertion
2180 * fails, leaving us with leaked space, which is neither good, nor
2183 * It seems that creation-first is simplest solution, but it is
2184 * sub-optimal in the frequent
2189 * case, because second mkdir is bound to create object, only to
2190 * destroy it immediately.
2192 * To avoid this follow local file systems that do double lookup:
2194 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2196 * 1. create (mdd_object_create_internal())
2198 * 2. insert (__mdd_index_insert(), lookup again)
2201 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2205 /* Sanity checks before big job. */
2206 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2210 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2211 GOTO(out_free, rc = -EINPROGRESS);
2213 handle = mdd_trans_create(env, mdd);
2215 GOTO(out_free, rc = PTR_ERR(handle));
2217 acl_buf.lb_buf = info->mti_xattr_buf;
2218 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2219 def_acl_buf.lb_buf = info->mti_key;
2220 def_acl_buf.lb_len = sizeof(info->mti_key);
2221 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2225 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2227 memset(ldata, 0, sizeof(*ldata));
2228 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2229 mdd_object_fid(mdd_pobj),
2230 lname, 1, 0, ldata);
2232 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2233 handle, spec, ldata, &def_acl_buf, &acl_buf,
2238 rc = mdd_trans_start(env, mdd, handle);
2242 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2243 &def_acl_buf, hint, handle);
2247 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2248 mdd_write_lock(env, son, MOR_TGT_CHILD);
2249 rc = __mdd_orphan_add(env, son, handle);
2250 mdd_write_unlock(env, son);
2252 GOTO(err_created, rc);
2254 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2255 name, S_ISDIR(attr->la_mode), handle,
2256 mdd_object_capa(env, mdd_pobj));
2258 GOTO(err_created, rc);
2260 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2263 /* update parent directory mtime/ctime */
2265 la->la_valid = LA_CTIME | LA_MTIME;
2266 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2268 GOTO(err_insert, rc);
2276 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2277 rc2 = __mdd_orphan_del(env, son, handle);
2279 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2280 S_ISDIR(attr->la_mode),
2281 handle, BYPASS_CAPA);
2286 mdd_write_lock(env, son, MOR_TGT_CHILD);
2287 if (S_ISDIR(attr->la_mode)) {
2288 /* Drop the reference, no need to delete "."/"..",
2289 * because the object to be destroied directly. */
2290 rc2 = mdo_ref_del(env, son, handle);
2292 mdd_write_unlock(env, son);
2296 rc2 = mdo_ref_del(env, son, handle);
2298 mdd_write_unlock(env, son);
2302 mdo_destroy(env, son, handle);
2303 mdd_write_unlock(env, son);
2306 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2307 rc = mdd_changelog_ns_store(env, mdd,
2308 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2309 S_ISREG(attr->la_mode) ? CL_CREATE :
2310 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2311 0, son, mdd_pobj, lname, handle);
2313 mdd_trans_stop(env, mdd, rc, handle);
2315 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2316 /* if we vmalloced a large buffer drop it */
2317 lu_buf_free(ldata->ld_buf);
2319 /* The child object shouldn't be cached anymore */
2321 set_bit(LU_OBJECT_HEARD_BANSHEE,
2322 &child->mo_lu.lo_header->loh_flags);
2327 * Get locks on parents in proper order
2328 * RETURN: < 0 - error, rename_order if successful
2336 static int mdd_rename_order(const struct lu_env *env,
2337 struct mdd_device *mdd,
2338 struct mdd_object *src_pobj,
2339 const struct lu_attr *pattr,
2340 struct mdd_object *tgt_pobj)
2342 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2346 if (src_pobj == tgt_pobj)
2347 RETURN(MDD_RN_SAME);
2349 /* compared the parent child relationship of src_p&tgt_p */
2350 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2352 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2355 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2369 /* has not mdd_write{read}_lock on any obj yet. */
2370 static int mdd_rename_sanity_check(const struct lu_env *env,
2371 struct mdd_object *src_pobj,
2372 const struct lu_attr *pattr,
2373 struct mdd_object *tgt_pobj,
2374 const struct lu_attr *tpattr,
2375 struct mdd_object *sobj,
2376 const struct lu_attr *cattr,
2377 struct mdd_object *tobj,
2378 const struct lu_attr *tattr)
2383 /* XXX: when get here, sobj must NOT be NULL,
2384 * the other case has been processed in cld_rename
2385 * before mdd_rename and enable MDS_PERM_BYPASS. */
2388 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2392 /* XXX: when get here, "tobj == NULL" means tobj must
2393 * NOT exist (neither on remote MDS, such case has been
2394 * processed in cld_rename before mdd_rename and enable
2396 * So check may_create, but not check may_unlink. */
2398 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2399 (src_pobj != tgt_pobj), 0);
2401 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2402 (src_pobj != tgt_pobj), 1);
2404 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2405 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2410 static int mdd_declare_rename(const struct lu_env *env,
2411 struct mdd_device *mdd,
2412 struct mdd_object *mdd_spobj,
2413 struct mdd_object *mdd_tpobj,
2414 struct mdd_object *mdd_sobj,
2415 struct mdd_object *mdd_tobj,
2416 const struct lu_name *tname,
2417 const struct lu_name *sname,
2419 struct linkea_data *ldata,
2420 struct thandle *handle)
2422 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2425 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2426 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2432 /* name from source dir */
2433 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2437 /* .. from source child */
2438 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2439 /* source child can be directory,
2440 * counted by source dir's nlink */
2441 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2444 if (mdd_spobj != mdd_tpobj) {
2445 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2450 rc = mdo_declare_index_insert(env, mdd_sobj,
2456 /* new target child can be directory,
2457 * counted by target dir's nlink */
2458 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2463 la->la_valid = LA_CTIME | LA_MTIME;
2464 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2468 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2472 la->la_valid = LA_CTIME;
2473 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2477 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2482 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2483 tname->ln_name, handle);
2487 /* name from target dir (old name), we declare it unconditionally
2488 * as mdd_rename() calls delete unconditionally as well. so just
2489 * to balance declarations vs calls to change ... */
2490 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2494 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2495 /* delete target child in target parent directory */
2496 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2500 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2501 /* target child can be directory,
2502 * delete "." reference in target child directory */
2503 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2507 /* delete ".." reference in target parent directory */
2508 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2513 la->la_valid = LA_CTIME;
2514 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2518 rc = mdd_declare_links_del(env, mdd_tobj, handle);
2522 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2527 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2534 /* src object can be remote that is why we use only fid and type of object */
2535 static int mdd_rename(const struct lu_env *env,
2536 struct md_object *src_pobj, struct md_object *tgt_pobj,
2537 const struct lu_fid *lf, const struct lu_name *lsname,
2538 struct md_object *tobj, const struct lu_name *ltname,
2541 const char *sname = lsname->ln_name;
2542 const char *tname = ltname->ln_name;
2543 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2544 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2545 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2546 struct mdd_device *mdd = mdo2mdd(src_pobj);
2547 struct mdd_object *mdd_sobj = NULL; /* source object */
2548 struct mdd_object *mdd_tobj = NULL;
2549 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2550 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2551 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2552 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2553 struct thandle *handle;
2554 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2555 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2556 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2559 bool tobj_locked = 0;
2560 unsigned cl_flags = 0;
2565 mdd_tobj = md2mdd_obj(tobj);
2567 mdd_sobj = mdd_object_find(env, mdd, lf);
2569 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2571 GOTO(out_pending, rc);
2573 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2575 GOTO(out_pending, rc);
2578 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2580 GOTO(out_pending, rc);
2583 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2585 GOTO(out_pending, rc);
2587 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2588 mdd_sobj, cattr, mdd_tobj, tattr);
2590 GOTO(out_pending, rc);
2592 handle = mdd_trans_create(env, mdd);
2594 GOTO(out_pending, rc = PTR_ERR(handle));
2596 memset(ldata, 0, sizeof(*ldata));
2597 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2598 ltname, 1, 0, ldata);
2599 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2600 mdd_tobj, lsname, ltname, ma, ldata, handle);
2604 rc = mdd_trans_start(env, mdd, handle);
2608 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2609 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2611 GOTO(cleanup_unlocked, rc);
2613 is_dir = S_ISDIR(cattr->la_mode);
2615 /* Remove source name from source directory */
2616 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2617 mdd_object_capa(env, mdd_spobj));
2621 /* "mv dir1 dir2" needs "dir1/.." link update */
2622 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2623 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2624 mdd_object_capa(env, mdd_sobj));
2626 GOTO(fixup_spobj2, rc);
2628 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2629 handle, mdd_object_capa(env, mdd_sobj));
2631 GOTO(fixup_spobj, rc);
2634 /* Remove target name from target directory
2635 * Here tobj can be remote one, so we do index_delete unconditionally
2636 * and -ENOENT is allowed.
2638 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2639 mdd_object_capa(env, mdd_tpobj));
2642 /* tname might been renamed to something else */
2643 GOTO(fixup_spobj, rc);
2646 GOTO(fixup_spobj, rc);
2649 /* Insert new fid with target name into target dir */
2650 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2651 mdd_object_capa(env, mdd_tpobj));
2653 GOTO(fixup_tpobj, rc);
2655 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2656 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2658 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2660 la->la_valid = LA_CTIME;
2661 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2663 GOTO(fixup_tpobj, rc);
2666 /* Remove old target object
2667 * For tobj is remote case cmm layer has processed
2668 * and set tobj to NULL then. So when tobj is NOT NULL,
2669 * it must be local one.
2671 if (tobj && mdd_object_exists(mdd_tobj)) {
2672 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2674 if (mdd_is_dead_obj(mdd_tobj)) {
2675 /* shld not be dead, something is wrong */
2676 CERROR("tobj is dead, something is wrong\n");
2680 mdo_ref_del(env, mdd_tobj, handle);
2682 /* Remove dot reference. */
2683 if (S_ISDIR(tattr->la_mode))
2684 mdo_ref_del(env, mdd_tobj, handle);
2687 /* fetch updated nlink */
2688 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2690 CERROR("%s: Failed to get nlink for tobj "
2692 mdd2obd_dev(mdd)->obd_name,
2693 PFID(tpobj_fid), rc);
2694 GOTO(fixup_tpobj, rc);
2697 la->la_valid = LA_CTIME;
2698 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2700 CERROR("%s: Failed to set ctime for tobj "
2702 mdd2obd_dev(mdd)->obd_name,
2703 PFID(tpobj_fid), rc);
2704 GOTO(fixup_tpobj, rc);
2707 /* XXX: this transfer to ma will be removed with LOD/OSP */
2708 ma->ma_attr = *tattr;
2709 ma->ma_valid |= MA_INODE;
2710 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2713 CERROR("%s: Failed to unlink tobj "
2715 mdd2obd_dev(mdd)->obd_name,
2716 PFID(tpobj_fid), rc);
2717 GOTO(fixup_tpobj, rc);
2720 /* fetch updated nlink */
2721 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2723 CERROR("%s: Failed to get nlink for tobj "
2725 mdd2obd_dev(mdd)->obd_name,
2726 PFID(tpobj_fid), rc);
2727 GOTO(fixup_tpobj, rc);
2729 /* XXX: this transfer to ma will be removed with LOD/OSP */
2730 ma->ma_attr = *tattr;
2731 ma->ma_valid |= MA_INODE;
2733 if (tattr->la_nlink == 0) {
2734 cl_flags |= CLF_RENAME_LAST;
2735 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2736 cl_flags |= CLF_RENAME_LAST_EXISTS;
2740 la->la_valid = LA_CTIME | LA_MTIME;
2741 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2743 GOTO(fixup_tpobj, rc);
2745 if (mdd_spobj != mdd_tpobj) {
2746 la->la_valid = LA_CTIME | LA_MTIME;
2747 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2750 if (rc == 0 && mdd_sobj) {
2751 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2752 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2753 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2756 /* Old files might not have EA entry */
2757 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2758 lsname, handle, NULL, 0);
2759 mdd_write_unlock(env, mdd_sobj);
2760 /* We don't fail the transaction if the link ea can't be
2761 updated -- fid2path will use alternate lookup method. */
2769 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2772 CWARN("tp obj fix error %d\n",rc2);
2774 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2775 !mdd_is_dead_obj(mdd_tobj)) {
2777 mdo_ref_add(env, mdd_tobj, handle);
2779 mdo_ref_add(env, mdd_tobj, handle);
2782 rc2 = __mdd_index_insert(env, mdd_tpobj,
2783 mdo2fid(mdd_tobj), tname,
2788 CWARN("tp obj fix error %d\n",rc2);
2793 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2794 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2798 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2799 mdd2obd_dev(mdd)->obd_name, rc2);
2802 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2803 dotdot, handle, BYPASS_CAPA);
2805 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2806 mdd2obd_dev(mdd)->obd_name, rc2);
2811 rc2 = __mdd_index_insert(env, mdd_spobj,
2812 lf, sname, is_dir, handle, BYPASS_CAPA);
2814 CWARN("sp obj fix error %d\n",rc2);
2818 mdd_write_unlock(env, mdd_tobj);
2821 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2822 mdd_tobj, tpobj_fid, lf,
2823 spobj_fid, ltname, lsname,
2827 mdd_trans_stop(env, mdd, rc, handle);
2829 mdd_object_put(env, mdd_sobj);
2834 * During migration once the parent FID has been changed,
2835 * we need update the parent FID in linkea.
2837 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2838 struct mdd_object *parent,
2839 struct mdd_object *child,
2840 const char *name, int namelen,
2841 struct thandle *handle,
2844 struct mdd_thread_info *info = mdd_env_info(env);
2845 struct linkea_data ldata = {0};
2846 struct lu_buf *buf = &info->mti_link_buf;
2852 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2853 if (buf->lb_buf == NULL)
2857 rc = mdd_links_read(env, child, &ldata);
2859 if (rc == -ENOENT || rc == -ENODATA)
2864 LASSERT(ldata.ld_leh != NULL);
2865 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2866 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2867 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2868 struct lu_name lname;
2871 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2874 if (strncmp(lname.ln_name, name, namelen) ||
2875 lu_fid_eq(&fid, mdd_object_fid(parent))) {
2876 ldata.ld_lee = (struct link_ea_entry *)
2877 ((char *)ldata.ld_lee +
2882 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
2883 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
2884 lname.ln_namelen, lname.ln_name,
2885 PFID(mdd_object_fid(parent)));
2886 /* update to the new parent fid */
2887 linkea_entry_pack(ldata.ld_lee, &lname,
2888 mdd_object_fid(parent));
2890 rc = mdd_declare_links_add(env, child, handle, &ldata);
2892 rc = mdd_links_write(env, child, &ldata, handle);
2898 static int mdd_linkea_declare_update_child(const struct lu_env *env,
2899 struct mdd_object *parent,
2900 struct mdd_object *child,
2901 const char *name, int namelen,
2902 struct thandle *handle)
2904 return mdd_linkea_update_child_internal(env, parent, child, name,
2905 namelen, handle, true);
2908 static int mdd_linkea_update_child(const struct lu_env *env,
2909 struct mdd_object *parent,
2910 struct mdd_object *child,
2911 const char *name, int namelen,
2912 struct thandle *handle)
2914 return mdd_linkea_update_child_internal(env, parent, child, name,
2915 namelen, handle, false);
2918 static int mdd_update_linkea_internal(const struct lu_env *env,
2919 struct mdd_object *mdd_pobj,
2920 struct mdd_object *mdd_sobj,
2921 struct mdd_object *mdd_tobj,
2922 const struct lu_name *child_name,
2923 struct thandle *handle,
2926 struct mdd_thread_info *info = mdd_env_info(env);
2927 struct linkea_data *ldata = &info->mti_link_data;
2932 rc = mdd_links_read(env, mdd_sobj, ldata);
2934 if (rc == -ENOENT || rc == -ENODATA)
2940 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
2942 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
2947 /* If it is mulitple links file, we need update the name entry for
2949 LASSERT(ldata->ld_leh != NULL);
2950 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
2951 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
2952 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
2953 struct mdd_object *pobj;
2954 struct lu_name lname;
2957 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
2959 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
2961 pobj = mdd_object_find(env, mdd, &fid);
2963 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
2964 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
2969 if (!mdd_object_exists(pobj)) {
2970 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
2971 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
2975 if (pobj == mdd_pobj &&
2976 lname.ln_namelen == child_name->ln_namelen &&
2977 strncmp(lname.ln_name, child_name->ln_name,
2978 lname.ln_namelen) == 0) {
2979 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
2980 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
2985 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
2986 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
2987 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
2990 /* Remove source name from source directory */
2991 /* Insert new fid with target name into target dir */
2992 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
2997 rc = mdo_declare_index_insert(env, pobj,
2998 mdd_object_fid(mdd_tobj),
2999 lname.ln_name, handle);
3003 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3007 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3011 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3013 mdd_object_capa(env, pobj));
3017 rc = __mdd_index_insert(env, pobj,
3018 mdd_object_fid(mdd_tobj),
3019 lname.ln_name, 0, handle,
3020 mdd_object_capa(env, pobj));
3024 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3025 rc = mdo_ref_add(env, mdd_tobj, handle);
3026 mdd_write_unlock(env, mdd_tobj);
3030 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3031 mdo_ref_del(env, mdd_sobj, handle);
3032 mdd_write_unlock(env, mdd_sobj);
3035 mdd_object_put(env, pobj);
3043 static int mdd_migrate_xattrs(const struct lu_env *env,
3044 struct mdd_object *mdd_sobj,
3045 struct mdd_object *mdd_tobj)
3047 struct mdd_thread_info *info = mdd_env_info(env);
3048 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3050 struct thandle *handle;
3056 struct lu_buf list_xbuf;
3059 /* retrieve xattr list from the old object */
3060 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3061 mdd_object_capa(env, mdd_sobj));
3062 if (list_xsize == -ENODATA)
3068 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3069 if (info->mti_big_buf.lb_buf == NULL)
3072 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3073 list_xbuf.lb_len = list_xsize;
3074 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3075 mdd_object_capa(env, mdd_sobj));
3080 xname = list_xbuf.lb_buf;
3082 xlen = strnlen(xname, rem - 1) + 1;
3083 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3084 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3085 strcmp(XATTR_NAME_LMV, xname) == 0)
3088 /* For directory, if there are default layout, migrate here */
3089 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3090 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3093 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3095 mdd_object_capa(env, mdd_sobj));
3096 if (xsize == -ENODATA)
3101 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3102 if (info->mti_link_buf.lb_buf == NULL)
3103 GOTO(out, rc = -ENOMEM);
3105 xbuf.lb_len = xsize;
3106 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3107 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3108 mdd_object_capa(env, mdd_sobj));
3114 handle = mdd_trans_create(env, mdd);
3116 GOTO(out, rc = PTR_ERR(handle));
3118 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3121 GOTO(stop_trans, rc);
3122 /* Note: this transaction is part of migration, and it is not
3123 * the last step of migration, so we set th_local = 1 to avoid
3124 * update last rcvd for this transaction */
3125 handle->th_local = 1;
3126 rc = mdd_trans_start(env, mdd, handle);
3128 GOTO(stop_trans, rc);
3130 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3131 mdd_object_capa(env, mdd_sobj));
3133 GOTO(stop_trans, rc = 0);
3136 GOTO(stop_trans, rc);
3138 mdd_trans_stop(env, mdd, rc, handle);
3143 memmove(xname, xname + xlen, rem);
3149 static int mdd_declare_migrate_create(const struct lu_env *env,
3150 struct mdd_object *mdd_pobj,
3151 struct mdd_object *mdd_sobj,
3152 struct mdd_object *mdd_tobj,
3153 struct md_op_spec *spec,
3155 union lmv_mds_md *mgr_ea,
3156 struct thandle *handle)
3158 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3159 const struct lu_buf *buf;
3163 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3164 handle, spec, NULL);
3168 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3173 if (S_ISLNK(la->la_mode)) {
3174 const char *target_name = spec->u.sp_symname;
3175 int sym_len = strlen(target_name);
3176 const struct lu_buf *buf;
3178 buf = mdd_buf_get_const(env, target_name, sym_len);
3179 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3185 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3186 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3187 spec->u.sp_ea.eadatalen);
3188 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3194 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3195 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3196 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3201 la_flag->la_valid = LA_FLAGS;
3202 la_flag->la_flags = LUSTRE_IMMUTABLE_FL;
3203 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3204 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3209 static int mdd_migrate_create(const struct lu_env *env,
3210 struct mdd_object *mdd_pobj,
3211 struct mdd_object *mdd_sobj,
3212 struct mdd_object *mdd_tobj,
3215 struct mdd_thread_info *info = mdd_env_info(env);
3216 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3217 struct md_op_spec *spec = &info->mti_spec;
3218 struct lu_buf lmm_buf = { 0 };
3219 struct lu_buf link_buf = { 0 };
3220 const struct lu_buf *buf;
3221 struct thandle *handle;
3222 struct lmv_mds_md_v1 *mgr_ea;
3223 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3228 /* prepare spec for create */
3229 memset(spec, 0, sizeof(*spec));
3230 spec->sp_cr_lookup = 0;
3231 spec->sp_feat = &dt_directory_features;
3232 if (S_ISLNK(la->la_mode)) {
3233 buf = lu_buf_check_and_alloc(
3234 &mdd_env_info(env)->mti_big_buf,
3237 link_buf.lb_len = la->la_size + 1;
3238 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3240 rc = rc != 0 ? rc : -EFAULT;
3241 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3242 mdd2obd_dev(mdd)->obd_name,
3243 PFID(mdd_object_fid(mdd_sobj)), rc);
3246 spec->u.sp_symname = link_buf.lb_buf;
3248 /* retrieve lov of the old object */
3249 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3250 if (rc != 0 && rc != -ENODATA)
3252 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3253 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3254 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3255 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3259 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3260 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_MIGRATE);
3261 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3262 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3263 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_MIGRATION);
3264 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3265 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3267 handle = mdd_trans_create(env, mdd);
3269 GOTO(out_free, rc = PTR_ERR(handle));
3271 /* Note: this transaction is part of migration, and it is not
3272 * the last step of migration, so we set th_local = 1 to avoid
3273 * update last rcvd for this transaction */
3274 handle->th_local = 1;
3275 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3277 (union lmv_mds_md *)info->mti_xattr_buf,
3280 GOTO(stop_trans, rc);
3282 rc = mdd_trans_start(env, mdd, handle);
3284 GOTO(stop_trans, rc);
3286 /* create the target object */
3287 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3290 GOTO(stop_trans, rc);
3292 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3293 buf = mdd_buf_get_const(env, lmm_buf.lb_buf, lmm_buf.lb_len);
3294 rc = mdo_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3295 0, handle, mdd_object_capa(env, mdd_sobj));
3297 GOTO(stop_trans, rc);
3300 /* Set MIGRATE EA on the source inode, so once the migration needs
3301 * to be re-done during failover, the re-do process can locate the
3302 * target object which is already being created. */
3303 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3304 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3305 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3306 handle, mdd_object_capa(env, mdd_sobj));
3308 GOTO(stop_trans, rc);
3310 /* Set immutable flag, so any modification is disabled until
3311 * the migration is done. Once the migration is interrupted,
3312 * if the resume process find the migrating object has both
3313 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3314 * flag and approve the migration */
3315 la_flag->la_valid = LA_FLAGS;
3316 la_flag->la_flags = LUSTRE_IMMUTABLE_FL;
3317 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3318 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3319 mdd_object_capa(env, mdd_sobj));
3322 mdd_trans_stop(env, mdd, rc, handle);
3324 if (lmm_buf.lb_buf != NULL)
3325 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3329 static int mdd_migrate_entries(const struct lu_env *env,
3330 struct mdd_object *mdd_sobj,
3331 struct mdd_object *mdd_tobj)
3333 struct dt_object *next = mdd_object_child(mdd_sobj);
3334 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3335 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3336 struct thandle *handle;
3338 const struct dt_it_ops *iops;
3341 struct lu_dirent *ent;
3344 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3348 if (!dt_try_as_dir(env, next))
3349 GOTO(out_ent, rc = -ENOTDIR);
3351 * iterate directories
3353 iops = &next->do_index_ops->dio_it;
3354 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3355 mdd_object_capa(env, mdd_sobj));
3357 GOTO(out_ent, rc = PTR_ERR(it));
3359 rc = iops->load(env, it, 0);
3361 rc = iops->next(env, it);
3365 * At this point and across for-loop:
3367 * rc == 0 -> ok, proceed.
3368 * rc > 0 -> end of directory.
3372 struct mdd_object *child;
3373 char *name = mdd_env_info(env)->mti_key;
3377 bool target_exist = false;
3379 len = iops->key_size(env, it);
3383 result = iops->rec(env, it, (struct dt_rec *)ent,
3384 LUDA_FID | LUDA_TYPE);
3385 if (result == -ESTALE)
3392 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3393 recsize = le16_to_cpu(ent->lde_reclen);
3395 /* Insert new fid with target name into target dir */
3396 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3397 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3398 ent->lde_name[1] == '.'))
3401 child = mdd_object_find(env, mdd, &ent->lde_fid);
3403 GOTO(out, rc = PTR_ERR(child));
3405 is_dir = S_ISDIR(lu_object_attr(&child->mod_obj.mo_lu));
3407 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3409 /* Check whether the name has been inserted to the target */
3410 if (dt_try_as_dir(env, dt_tobj)) {
3411 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3413 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3414 (struct dt_rec *)fid,
3415 (struct dt_key *)name,
3416 mdd_object_capa(env, mdd_tobj));
3417 if (unlikely(rc == 0))
3418 target_exist = true;
3421 handle = mdd_trans_create(env, mdd);
3423 GOTO(out, rc = PTR_ERR(handle));
3425 /* Note: this transaction is part of migration, and it is not
3426 * the last step of migration, so we set th_local = 1 to avoid
3427 * updating last rcvd for this transaction */
3428 handle->th_local = 1;
3429 if (likely(!target_exist)) {
3430 rc = mdo_declare_index_insert(env, mdd_tobj,
3437 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3443 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3448 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3452 /* Update .. for child */
3453 rc = mdo_declare_index_delete(env, child, dotdot,
3458 rc = mdo_declare_index_insert(env, child,
3459 mdd_object_fid(mdd_tobj),
3465 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3472 rc = mdd_trans_start(env, mdd, handle);
3474 CERROR("%s: transaction start failed: rc = %d\n",
3475 mdd2obd_dev(mdd)->obd_name, rc);
3479 if (likely(!target_exist)) {
3480 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3481 name, is_dir, handle,
3482 mdd_object_capa(env, mdd_tobj));
3487 rc = mdo_ref_add(env, mdd_tobj, handle);
3494 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3495 mdd_object_capa(env, mdd_sobj));
3500 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3501 mdd_object_capa(env, child));
3505 rc = __mdd_index_insert_only(env, child,
3506 mdd_object_fid(mdd_tobj),
3508 mdd_object_capa(env, child));
3513 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3514 strlen(name), handle);
3517 mdd_object_put(env, child);
3518 mdd_trans_stop(env, mdd, rc, handle);
3522 result = iops->next(env, it);
3523 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3524 GOTO(out, rc = -EINTR);
3526 if (result == -ESTALE)
3528 } while (result == 0);
3531 iops->fini(env, it);
3533 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3537 static int mdd_declare_update_linkea(const struct lu_env *env,
3538 struct mdd_object *mdd_pobj,
3539 struct mdd_object *mdd_sobj,
3540 struct mdd_object *mdd_tobj,
3541 struct thandle *handle,
3542 const struct lu_name *child_name)
3544 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3545 child_name, handle, 1);
3548 static int mdd_update_linkea(const struct lu_env *env,
3549 struct mdd_object *mdd_pobj,
3550 struct mdd_object *mdd_sobj,
3551 struct mdd_object *mdd_tobj,
3552 struct thandle *handle,
3553 const struct lu_name *child_name)
3555 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3556 child_name, handle, 0);
3559 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3560 struct mdd_object *mdd_pobj,
3561 struct mdd_object *mdd_sobj,
3562 struct mdd_object *mdd_tobj,
3563 const struct lu_name *lname,
3565 struct lu_attr *parent_la,
3566 struct thandle *handle)
3568 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3571 /* Revert IMMUTABLE flag */
3572 la_flag->la_valid = LA_FLAGS;
3573 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3574 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3575 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3579 /* delete entry from source dir */
3580 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3584 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3585 mdd_tobj, handle, lname);
3589 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3590 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3596 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3597 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3603 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3604 lname->ln_name, handle);
3608 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3609 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3614 /* delete old object */
3615 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3619 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3620 /* delete old object */
3621 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3624 /* set nlink to 0 */
3625 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3630 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3634 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3639 static int mdd_migrate_update_name(const struct lu_env *env,
3640 struct mdd_object *mdd_pobj,
3641 struct mdd_object *mdd_sobj,
3642 struct mdd_object *mdd_tobj,
3643 const struct lu_name *lname,
3646 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3647 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3648 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3649 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3650 struct thandle *handle;
3651 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3652 const char *name = lname->ln_name;
3656 /* update time for parent */
3657 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3658 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3659 p_la->la_valid = LA_CTIME;
3661 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3665 handle = mdd_trans_create(env, mdd);
3667 RETURN(PTR_ERR(handle));
3669 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3670 lname, so_attr, p_la, handle);
3672 /* If the migration can not be fit in one transaction, just
3673 * leave it in the original MDT */
3675 GOTO(stop_trans, rc = 0);
3677 GOTO(stop_trans, rc);
3680 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3681 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3682 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3683 PFID(mdd_object_fid(mdd_tobj)));
3685 rc = mdd_trans_start(env, mdd, handle);
3687 GOTO(stop_trans, rc);
3689 /* Revert IMMUTABLE flag */
3690 la_flag->la_valid = LA_FLAGS;
3691 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3692 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3693 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3694 mdd_object_capa(env, mdd_pobj));
3696 GOTO(stop_trans, rc);
3698 /* Remove source name from source directory */
3699 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3700 mdd_object_capa(env, mdd_pobj));
3702 GOTO(stop_trans, rc);
3704 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3707 GOTO(stop_trans, rc);
3709 if (S_ISREG(so_attr->la_mode)) {
3710 if (so_attr->la_nlink == 1) {
3711 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3713 mdd_object_capa(env, mdd_sobj));
3714 if (rc != 0 && rc != -ENODATA)
3715 GOTO(stop_trans, rc);
3719 /* Insert new fid with target name into target dir */
3720 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj), name,
3721 is_dir, handle, mdd_object_capa(env, mdd_pobj));
3723 GOTO(stop_trans, rc);
3725 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3728 GOTO(stop_trans, rc);
3730 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3731 mdo_ref_del(env, mdd_sobj, handle);
3733 mdo_ref_del(env, mdd_sobj, handle);
3735 ma->ma_attr = *so_attr;
3736 ma->ma_valid |= MA_INODE;
3737 rc = mdd_finish_unlink(env, mdd_sobj, ma, handle);
3739 GOTO(stop_trans, rc);
3741 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3743 GOTO(stop_trans, rc);
3745 mdd_write_unlock(env, mdd_sobj);
3748 mdd_trans_stop(env, mdd, rc, handle);
3754 * Check whether we should migrate the file/dir
3756 * < 0 permission check failed or other error.
3757 * = 0 the file can be migrated.
3758 * > 0 the file does not need to be migrated, mostly
3759 * for multiple link file
3761 static int mdd_migrate_sanity_check(const struct lu_env *env,
3762 struct mdd_object *pobj,
3763 const struct lu_attr *pattr,
3764 struct mdd_object *sobj,
3765 struct lu_attr *sattr)
3767 struct mdd_thread_info *info = mdd_env_info(env);
3768 struct linkea_data *ldata = &info->mti_link_data;
3770 struct lu_buf *mgr_buf;
3776 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3777 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3778 if (mgr_buf->lb_buf == NULL)
3781 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3782 mdd_object_capa(env, sobj));
3784 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3786 /* If the object has migrateEA, it means IMMUTE flag
3787 * is being set by previous migration process, so it
3788 * needs to override the IMMUTE flag, otherwise the
3789 * following sanity check will fail */
3790 if (le32_to_cpu(lmm->lmv_md_v1.lmv_magic) ==
3791 LMV_MAGIC_MIGRATE) {
3792 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3794 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3795 sobj->mod_flags &= ~IMMUTE_OBJ;
3796 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3797 mdd2obd_dev(mdd)->obd_name,
3798 PFID(mdd_object_fid(sobj)));
3802 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3803 sobj, sattr, NULL, NULL);
3807 /* Then it will check if the file should be migrated. If the file
3808 * has mulitple links, we only need migrate the file if all of its
3809 * entries has been migrated to the remote MDT */
3810 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3813 rc = mdd_links_read(env, sobj, ldata);
3815 /* For multiple links files, if there are no linkEA data at all,
3816 * means the file might be created before linkEA is enabled, and
3817 * all all of its links should not be migrated yet, otherwise
3818 * it should have some linkEA there */
3819 if (rc == -ENOENT || rc == -ENODATA)
3824 /* If it is mulitple links file, we need update the name entry for
3826 LASSERT(ldata->ld_leh != NULL);
3827 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3828 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3829 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3830 struct mdd_object *lpobj;
3831 struct lu_name lname;
3834 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3836 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3838 lpobj = mdd_object_find(env, mdd, &fid);
3839 if (IS_ERR(lpobj)) {
3840 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3841 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3846 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3847 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3848 PFID(&fid), lname.ln_namelen, lname.ln_name);
3849 mdd_object_put(env, lpobj);
3853 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3854 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3855 lname.ln_name, PFID(&fid));
3856 mdd_object_put(env, lpobj);
3864 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3865 const struct lu_fid *lf, const struct lu_name *lname,
3866 struct md_object *tobj, struct md_attr *ma)
3868 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3869 struct mdd_device *mdd = mdo2mdd(pobj);
3870 struct mdd_object *mdd_sobj = NULL;
3871 struct mdd_object *mdd_tobj = NULL;
3872 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3873 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3877 /* object has to be locked by mdt, so it must exist */
3878 mdd_sobj = mdd_object_find(env, mdd, lf);
3879 LASSERT(mdd_sobj != NULL);
3881 /* If the file will being migrated, it will check whether
3882 * the file is being opened by someone else right now */
3883 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3884 if (mdd_sobj->mod_count >= 1) {
3885 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
3886 mdd2obd_dev(mdd)->obd_name,
3887 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3888 mdd_sobj->mod_count, -EBUSY);
3889 mdd_read_unlock(env, mdd_sobj);
3890 GOTO(put, rc = -EBUSY);
3892 mdd_read_unlock(env, mdd_sobj);
3894 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3898 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
3902 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
3909 /* Sigh, it is impossible to finish all of migration in a single
3910 * transaction, for example migrating big directory entries to the
3911 * new MDT, it needs insert all of name entries of children in the
3914 * So migration will be done in multiple steps and transactions.
3916 * 1. create an orphan object on the remote MDT in one transaction.
3917 * 2. migrate extend attributes to the new target file/directory.
3918 * 3. For directory, migrate the entries to the new MDT and update
3919 * linkEA of each children. Because we can not migrate all entries
3920 * in a single transaction, so the migrating directory will become
3921 * a striped directory during migration, so once the process is
3922 * interrupted, the directory is still accessible. (During lookup,
3923 * client will locate the name by searching both original and target
3925 * 4. Finally, update the name/FID to point to the new file/directory
3926 * in a separate transaction.
3929 /* step 1: Check whether the orphan object has been created, and create
3930 * orphan object on the remote MDT if needed */
3931 mdd_tobj = md2mdd_obj(tobj);
3932 if (!mdd_object_exists(mdd_tobj)) {
3933 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3939 /* step 2: migrate xattr */
3940 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
3944 /* step 3: migrate name entries to the orphan object */
3945 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
3946 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
3949 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
3950 OBD_FAIL_MDS_REINT_NET_REP)))
3954 /* step 4: update name entry to the new object */
3955 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
3961 mdd_object_put(env, mdd_sobj);
3966 const struct md_dir_operations mdd_dir_ops = {
3967 .mdo_is_subdir = mdd_is_subdir,
3968 .mdo_lookup = mdd_lookup,
3969 .mdo_create = mdd_create,
3970 .mdo_rename = mdd_rename,
3971 .mdo_link = mdd_link,
3972 .mdo_unlink = mdd_unlink,
3973 .mdo_create_data = mdd_create_data,
3974 .mdo_migrate = mdd_migrate,