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,
1337 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1340 LASSERT(mdd_write_locked(env, obj) != 0);
1342 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1343 obj->mod_flags |= DEAD_OBJ;
1345 /* add new orphan and the object
1346 * will be deleted during mdd_close() */
1347 if (obj->mod_count) {
1348 rc = __mdd_orphan_add(env, obj, th);
1350 CDEBUG(D_HA, "Object "DFID" is inserted into "
1351 "orphan list, open count = %d\n",
1352 PFID(mdd_object_fid(obj)),
1355 CERROR("Object "DFID" fail to be an orphan, "
1356 "open count = %d, maybe cause failed "
1358 PFID(mdd_object_fid(obj)),
1361 rc = mdo_destroy(env, obj, th);
1370 * has mdd_write_lock on cobj already, but not on pobj yet
1372 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1373 const struct lu_attr *pattr,
1374 struct mdd_object *cobj,
1375 const struct lu_attr *cattr)
1380 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1385 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1386 struct mdd_object *p, struct mdd_object *c,
1387 const struct lu_name *name, struct md_attr *ma,
1388 struct thandle *handle, int no_name)
1390 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1393 if (likely(no_name == 0)) {
1394 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1399 rc = mdo_declare_ref_del(env, p, handle);
1403 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1404 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1405 la->la_valid = LA_CTIME | LA_MTIME;
1406 rc = mdo_declare_attr_set(env, p, la, handle);
1411 rc = mdo_declare_ref_del(env, c, handle);
1415 rc = mdo_declare_ref_del(env, c, handle);
1419 la->la_valid = LA_CTIME;
1420 rc = mdo_declare_attr_set(env, c, la, handle);
1424 rc = mdd_declare_finish_unlink(env, c, handle);
1428 rc = mdd_declare_links_del(env, c, handle);
1432 /* FIXME: need changelog for remove entry */
1433 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1440 * test if a file has an HSM archive
1441 * if HSM attributes are not found in ma update them from
1444 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1445 struct mdd_object *obj,
1450 if (!(ma->ma_valid & MA_HSM)) {
1451 /* no HSM MD provided, read xattr */
1452 struct lu_buf *hsm_buf;
1453 const size_t buflen = sizeof(struct hsm_attrs);
1456 hsm_buf = mdd_buf_get(env, NULL, 0);
1457 lu_buf_alloc(hsm_buf, buflen);
1458 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1459 mdd_object_capa(env, obj));
1460 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1461 lu_buf_free(hsm_buf);
1465 ma->ma_valid = MA_HSM;
1467 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1473 * Delete name entry and the object.
1474 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1475 * does not exist for this object, and it could only happen during resending
1476 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1477 * is also needed in this case(needed by changelog), so we have to add another
1478 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1479 * the ENOENT failure should be able to be fixed by redo mechanism.
1481 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1482 struct md_object *cobj, const struct lu_name *lname,
1483 struct md_attr *ma, int no_name)
1485 const char *name = lname->ln_name;
1486 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1487 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1488 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1489 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1490 struct mdd_object *mdd_cobj = NULL;
1491 struct mdd_device *mdd = mdo2mdd(pobj);
1492 struct thandle *handle;
1496 /* cobj == NULL means only delete name entry */
1497 if (likely(cobj != NULL)) {
1498 mdd_cobj = md2mdd_obj(cobj);
1499 if (mdd_object_exists(mdd_cobj) == 0)
1503 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1507 if (likely(mdd_cobj != NULL)) {
1509 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1513 is_dir = S_ISDIR(cattr->la_mode);
1516 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1520 handle = mdd_trans_create(env, mdd);
1522 RETURN(PTR_ERR(handle));
1524 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1525 lname, ma, handle, no_name);
1529 rc = mdd_trans_start(env, mdd, handle);
1533 if (likely(mdd_cobj != NULL))
1534 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1536 if (likely(no_name == 0)) {
1537 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1538 mdd_object_capa(env, mdd_pobj));
1543 if (likely(mdd_cobj != NULL)) {
1544 rc = mdo_ref_del(env, mdd_cobj, handle);
1546 __mdd_index_insert_only(env, mdd_pobj,
1549 mdd_object_capa(env, mdd_pobj));
1555 mdo_ref_del(env, mdd_cobj, handle);
1557 /* fetch updated nlink */
1558 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1563 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1564 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1566 la->la_valid = LA_CTIME | LA_MTIME;
1567 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1571 /* Enough for only unlink the entry */
1572 if (unlikely(mdd_cobj == NULL))
1575 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1576 /* update ctime of an unlinked file only if it is still
1577 * opened or a link still exists */
1578 la->la_valid = LA_CTIME;
1579 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1584 /* XXX: this transfer to ma will be removed with LOD/OSP */
1585 ma->ma_attr = *cattr;
1586 ma->ma_valid |= MA_INODE;
1587 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1589 /* fetch updated nlink */
1591 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1594 /* old files may not have link ea; ignore errors */
1595 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1597 /* if object is removed then we can't get its attrs, use last get */
1598 if (cattr->la_nlink == 0) {
1599 ma->ma_attr = *cattr;
1600 ma->ma_valid |= MA_INODE;
1604 mdd_write_unlock(env, mdd_cobj);
1608 if (cattr->la_nlink == 0) {
1609 cl_flags |= CLF_UNLINK_LAST;
1610 /* search for an existing archive */
1611 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1612 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1615 rc = mdd_changelog_ns_store(env, mdd,
1616 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1617 mdd_cobj, mdd_pobj, lname, handle);
1621 mdd_trans_stop(env, mdd, rc, handle);
1627 * The permission has been checked when obj created, no need check again.
1629 static int mdd_cd_sanity_check(const struct lu_env *env,
1630 struct mdd_object *obj)
1635 if (!obj || mdd_is_dead_obj(obj))
1641 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1642 struct md_object *cobj, const struct md_op_spec *spec,
1645 struct mdd_device *mdd = mdo2mdd(cobj);
1646 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1647 struct mdd_object *son = md2mdd_obj(cobj);
1648 struct thandle *handle;
1649 const struct lu_buf *buf;
1650 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1651 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1655 rc = mdd_cd_sanity_check(env, son);
1659 if (!md_should_create(spec->sp_cr_flags))
1663 * there are following use cases for this function:
1664 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1665 * striping can be specified or not
1668 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1672 /* calling ->ah_make_hint() is used to transfer information from parent */
1673 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1675 handle = mdd_trans_create(env, mdd);
1677 GOTO(out_free, rc = PTR_ERR(handle));
1680 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1681 * Should this be fixed?
1683 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1684 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1685 spec->sp_cr_flags, spec->no_create);
1687 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1688 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1689 spec->u.sp_ea.eadatalen);
1694 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1695 XATTR_NAME_LOV, 0, handle);
1699 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1703 rc = mdd_trans_start(env, mdd, handle);
1707 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1708 0, handle, mdd_object_capa(env, son));
1713 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1716 mdd_trans_stop(env, mdd, rc, handle);
1721 static int mdd_declare_object_initialize(const struct lu_env *env,
1722 struct mdd_object *parent,
1723 struct mdd_object *child,
1724 struct lu_attr *attr,
1725 struct thandle *handle)
1731 * inode mode has been set in creation time, and it's based on umask,
1732 * la_mode and acl, don't set here again! (which will go wrong
1733 * because below function doesn't consider umask).
1734 * I'd suggest set all object attributes in creation time, see above.
1736 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1737 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1738 rc = mdo_declare_attr_set(env, child, attr, handle);
1739 attr->la_valid |= LA_MODE | LA_TYPE;
1740 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1741 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1744 rc = mdo_declare_ref_add(env, child, handle);
1746 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1753 static int mdd_object_initialize(const struct lu_env *env,
1754 const struct lu_fid *pfid,
1755 struct mdd_object *child,
1756 struct lu_attr *attr, struct thandle *handle,
1757 const struct md_op_spec *spec)
1763 * Update attributes for child.
1766 * (1) the valid bits should be converted between Lustre and Linux;
1767 * (2) maybe, the child attributes should be set in OSD when creation.
1770 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1771 /* arguments are supposed to stay the same */
1772 if (S_ISDIR(attr->la_mode)) {
1773 /* Add "." and ".." for newly created dir */
1774 mdo_ref_add(env, child, handle);
1775 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1776 dot, handle, BYPASS_CAPA);
1778 rc = __mdd_index_insert_only(env, child, pfid,
1782 mdo_ref_del(env, child, handle);
1788 /* has not lock on pobj yet */
1789 static int mdd_create_sanity_check(const struct lu_env *env,
1790 struct md_object *pobj,
1791 const struct lu_attr *pattr,
1792 const struct lu_name *lname,
1793 struct lu_attr *cattr,
1794 struct md_op_spec *spec)
1796 struct mdd_thread_info *info = mdd_env_info(env);
1797 struct lu_fid *fid = &info->mti_fid;
1798 struct mdd_object *obj = md2mdd_obj(pobj);
1799 struct mdd_device *m = mdo2mdd(pobj);
1804 if (mdd_is_dead_obj(obj))
1808 * In some cases this lookup is not needed - we know before if name
1809 * exists or not because MDT performs lookup for it.
1810 * name length check is done in lookup.
1812 if (spec->sp_cr_lookup) {
1814 * Check if the name already exist, though it will be checked in
1815 * _index_insert also, for avoiding rolling back if exists
1818 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1819 MAY_WRITE | MAY_EXEC);
1821 RETURN(rc ? : -EEXIST);
1824 * Check WRITE permission for the parent.
1825 * EXEC permission have been checked
1826 * when lookup before create already.
1828 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1835 if (pattr->la_mode & S_ISGID) {
1836 cattr->la_gid = pattr->la_gid;
1837 if (S_ISDIR(cattr->la_mode)) {
1838 cattr->la_mode |= S_ISGID;
1839 cattr->la_valid |= LA_MODE;
1843 switch (cattr->la_mode & S_IFMT) {
1845 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1847 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1848 RETURN(-ENAMETOOLONG);
1867 static int mdd_declare_object_create(const struct lu_env *env,
1868 struct mdd_device *mdd,
1869 struct mdd_object *p, struct mdd_object *c,
1870 struct lu_attr *attr,
1871 struct thandle *handle,
1872 const struct md_op_spec *spec,
1873 struct lu_buf *def_acl_buf,
1874 struct lu_buf *acl_buf,
1875 struct dt_allocation_hint *hint)
1879 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1884 #ifdef CONFIG_FS_POSIX_ACL
1885 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1886 /* if dir, then can inherit default ACl */
1887 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1888 XATTR_NAME_ACL_DEFAULT,
1894 if (acl_buf->lb_len > 0) {
1895 rc = mdo_declare_attr_set(env, c, attr, handle);
1899 rc = mdo_declare_xattr_set(env, c, acl_buf,
1900 XATTR_NAME_ACL_ACCESS, 0, handle);
1905 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1909 /* replay case, create LOV EA from client data */
1910 if (spec->no_create ||
1911 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
1912 const struct lu_buf *buf;
1914 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1915 spec->u.sp_ea.eadatalen);
1916 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
1922 if (S_ISLNK(attr->la_mode)) {
1923 const char *target_name = spec->u.sp_symname;
1924 int sym_len = strlen(target_name);
1925 const struct lu_buf *buf;
1927 buf = mdd_buf_get_const(env, target_name, sym_len);
1928 rc = dt_declare_record_write(env, mdd_object_child(c),
1937 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1938 struct mdd_object *p, struct mdd_object *c,
1939 const struct lu_name *name,
1940 struct lu_attr *attr,
1941 struct thandle *handle,
1942 const struct md_op_spec *spec,
1943 struct linkea_data *ldata,
1944 struct lu_buf *def_acl_buf,
1945 struct lu_buf *acl_buf,
1946 struct dt_allocation_hint *hint)
1950 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
1951 def_acl_buf, acl_buf, hint);
1955 if (S_ISDIR(attr->la_mode)) {
1956 rc = mdo_declare_ref_add(env, p, handle);
1961 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE) {
1962 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1966 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1968 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,
1972 rc = mdd_declare_links_add(env, c, handle, ldata);
1977 la->la_valid = LA_CTIME | LA_MTIME;
1978 rc = mdo_declare_attr_set(env, p, la, handle);
1983 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1987 /* XXX: For remote create, it should indicate the remote RPC
1988 * will be sent after local transaction is finished, which
1989 * is not very nice, but it will be removed once we fully support
1991 if (mdd_object_remote(p) && handle->th_update != NULL)
1992 handle->th_update->tu_sent_after_local_trans = 1;
1997 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1998 struct lu_attr *la, struct lu_buf *def_acl_buf,
1999 struct lu_buf *acl_buf)
2004 if (S_ISLNK(la->la_mode)) {
2005 acl_buf->lb_len = 0;
2006 def_acl_buf->lb_len = 0;
2010 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2011 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2012 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2013 mdd_read_unlock(env, pobj);
2015 /* If there are default ACL, fix mode/ACL by default ACL */
2016 def_acl_buf->lb_len = rc;
2017 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2018 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2019 acl_buf->lb_len = rc;
2020 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2023 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2024 /* If there are no default ACL, fix mode by mask */
2025 struct lu_ucred *uc = lu_ucred(env);
2027 /* The create triggered by MDT internal events, such as
2028 * LFSCK reset, will not contain valid "uc". */
2029 if (unlikely(uc != NULL))
2030 la->la_mode &= ~uc->uc_umask;
2032 acl_buf->lb_len = 0;
2033 def_acl_buf->lb_len = 0;
2040 * Create a metadata object and initialize it, set acl, xattr.
2042 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2043 struct mdd_object *son, struct lu_attr *attr,
2044 struct md_op_spec *spec, struct lu_buf *acl_buf,
2045 struct lu_buf *def_acl_buf,
2046 struct dt_allocation_hint *hint,
2047 struct thandle *handle)
2051 mdd_write_lock(env, son, MOR_TGT_CHILD);
2052 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2057 #ifdef CONFIG_FS_POSIX_ACL
2058 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2059 S_ISDIR(attr->la_mode)) {
2060 /* set default acl */
2061 rc = mdo_xattr_set(env, son, def_acl_buf,
2062 XATTR_NAME_ACL_DEFAULT, 0,
2063 handle, BYPASS_CAPA);
2065 GOTO(err_destroy, rc);
2067 /* set its own acl */
2068 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2069 rc = mdo_xattr_set(env, son, acl_buf,
2070 XATTR_NAME_ACL_ACCESS,
2071 0, handle, BYPASS_CAPA);
2073 GOTO(err_destroy, rc);
2077 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2080 GOTO(err_destroy, rc);
2083 * in case of replay we just set LOVEA provided by the client
2084 * XXX: I think it would be interesting to try "old" way where
2085 * MDT calls this xattr_set(LOV) in a different transaction.
2086 * probably this way we code can be made better.
2088 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA &&
2089 S_ISREG(attr->la_mode))) {
2090 const struct lu_buf *buf;
2092 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2093 spec->u.sp_ea.eadatalen);
2094 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2097 GOTO(err_destroy, rc);
2100 if (S_ISLNK(attr->la_mode)) {
2101 struct lu_ucred *uc = lu_ucred_assert(env);
2102 struct dt_object *dt = mdd_object_child(son);
2103 const char *target_name = spec->u.sp_symname;
2104 int sym_len = strlen(target_name);
2105 const struct lu_buf *buf;
2108 buf = mdd_buf_get_const(env, target_name, sym_len);
2109 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2110 mdd_object_capa(env, son),
2112 CFS_CAP_SYS_RESOURCE_MASK);
2117 GOTO(err_initlized, rc = -EFAULT);
2121 if (unlikely(rc != 0)) {
2123 if (S_ISDIR(attr->la_mode)) {
2124 /* Drop the reference, no need to delete "."/"..",
2125 * because the object to be destroied directly. */
2126 rc2 = mdo_ref_del(env, son, handle);
2130 rc2 = mdo_ref_del(env, son, handle);
2134 mdo_destroy(env, son, handle);
2137 mdd_write_unlock(env, son);
2142 * Create object and insert it into namespace.
2144 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2145 const struct lu_name *lname, struct md_object *child,
2146 struct md_op_spec *spec, struct md_attr* ma)
2148 struct mdd_thread_info *info = mdd_env_info(env);
2149 struct lu_attr *la = &info->mti_la_for_fix;
2150 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2151 struct mdd_object *son = md2mdd_obj(child);
2152 struct mdd_device *mdd = mdo2mdd(pobj);
2153 struct lu_attr *attr = &ma->ma_attr;
2154 struct thandle *handle;
2155 struct lu_attr *pattr = &info->mti_pattr;
2156 struct lu_buf acl_buf;
2157 struct lu_buf def_acl_buf;
2158 struct linkea_data *ldata = &info->mti_link_data;
2159 const char *name = lname->ln_name;
2160 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 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2317 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2318 /* if we vmalloced a large buffer drop it */
2319 lu_buf_free(ldata->ld_buf);
2321 /* The child object shouldn't be cached anymore */
2323 set_bit(LU_OBJECT_HEARD_BANSHEE,
2324 &child->mo_lu.lo_header->loh_flags);
2329 * Get locks on parents in proper order
2330 * RETURN: < 0 - error, rename_order if successful
2338 static int mdd_rename_order(const struct lu_env *env,
2339 struct mdd_device *mdd,
2340 struct mdd_object *src_pobj,
2341 const struct lu_attr *pattr,
2342 struct mdd_object *tgt_pobj)
2344 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2348 if (src_pobj == tgt_pobj)
2349 RETURN(MDD_RN_SAME);
2351 /* compared the parent child relationship of src_p&tgt_p */
2352 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2354 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2357 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2371 /* has not mdd_write{read}_lock on any obj yet. */
2372 static int mdd_rename_sanity_check(const struct lu_env *env,
2373 struct mdd_object *src_pobj,
2374 const struct lu_attr *pattr,
2375 struct mdd_object *tgt_pobj,
2376 const struct lu_attr *tpattr,
2377 struct mdd_object *sobj,
2378 const struct lu_attr *cattr,
2379 struct mdd_object *tobj,
2380 const struct lu_attr *tattr)
2385 /* XXX: when get here, sobj must NOT be NULL,
2386 * the other case has been processed in cld_rename
2387 * before mdd_rename and enable MDS_PERM_BYPASS. */
2390 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2394 /* XXX: when get here, "tobj == NULL" means tobj must
2395 * NOT exist (neither on remote MDS, such case has been
2396 * processed in cld_rename before mdd_rename and enable
2398 * So check may_create, but not check may_unlink. */
2400 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2401 (src_pobj != tgt_pobj), 0);
2403 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2404 (src_pobj != tgt_pobj), 1);
2406 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2407 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2412 static int mdd_declare_rename(const struct lu_env *env,
2413 struct mdd_device *mdd,
2414 struct mdd_object *mdd_spobj,
2415 struct mdd_object *mdd_tpobj,
2416 struct mdd_object *mdd_sobj,
2417 struct mdd_object *mdd_tobj,
2418 const struct lu_name *tname,
2419 const struct lu_name *sname,
2421 struct linkea_data *ldata,
2422 struct thandle *handle)
2424 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2427 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2428 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2434 /* name from source dir */
2435 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2439 /* .. from source child */
2440 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2441 /* source child can be directory,
2442 * counted by source dir's nlink */
2443 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2446 if (mdd_spobj != mdd_tpobj) {
2447 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2452 rc = mdo_declare_index_insert(env, mdd_sobj,
2458 /* new target child can be directory,
2459 * counted by target dir's nlink */
2460 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2465 la->la_valid = LA_CTIME | LA_MTIME;
2466 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2470 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2474 la->la_valid = LA_CTIME;
2475 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2479 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2484 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2485 tname->ln_name, handle);
2489 /* name from target dir (old name), we declare it unconditionally
2490 * as mdd_rename() calls delete unconditionally as well. so just
2491 * to balance declarations vs calls to change ... */
2492 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2496 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2497 /* delete target child in target parent directory */
2498 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2502 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2503 /* target child can be directory,
2504 * delete "." reference in target child directory */
2505 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2509 /* delete ".." reference in target parent directory */
2510 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2515 la->la_valid = LA_CTIME;
2516 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2520 rc = mdd_declare_links_del(env, mdd_tobj, handle);
2524 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2529 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2536 /* src object can be remote that is why we use only fid and type of object */
2537 static int mdd_rename(const struct lu_env *env,
2538 struct md_object *src_pobj, struct md_object *tgt_pobj,
2539 const struct lu_fid *lf, const struct lu_name *lsname,
2540 struct md_object *tobj, const struct lu_name *ltname,
2543 const char *sname = lsname->ln_name;
2544 const char *tname = ltname->ln_name;
2545 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2546 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2547 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2548 struct mdd_device *mdd = mdo2mdd(src_pobj);
2549 struct mdd_object *mdd_sobj = NULL; /* source object */
2550 struct mdd_object *mdd_tobj = NULL;
2551 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2552 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2553 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2554 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2555 struct thandle *handle;
2556 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2557 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2558 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2561 bool tobj_locked = 0;
2562 unsigned cl_flags = 0;
2567 mdd_tobj = md2mdd_obj(tobj);
2569 mdd_sobj = mdd_object_find(env, mdd, lf);
2571 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2573 GOTO(out_pending, rc);
2575 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2577 GOTO(out_pending, rc);
2580 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2582 GOTO(out_pending, rc);
2585 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2587 GOTO(out_pending, rc);
2589 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2590 mdd_sobj, cattr, mdd_tobj, tattr);
2592 GOTO(out_pending, rc);
2594 handle = mdd_trans_create(env, mdd);
2596 GOTO(out_pending, rc = PTR_ERR(handle));
2598 memset(ldata, 0, sizeof(*ldata));
2599 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2600 ltname, 1, 0, ldata);
2601 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2602 mdd_tobj, lsname, ltname, ma, ldata, handle);
2606 rc = mdd_trans_start(env, mdd, handle);
2610 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2611 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2613 GOTO(cleanup_unlocked, rc);
2615 is_dir = S_ISDIR(cattr->la_mode);
2617 /* Remove source name from source directory */
2618 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2619 mdd_object_capa(env, mdd_spobj));
2623 /* "mv dir1 dir2" needs "dir1/.." link update */
2624 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2625 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2626 mdd_object_capa(env, mdd_sobj));
2628 GOTO(fixup_spobj2, rc);
2630 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2631 handle, mdd_object_capa(env, mdd_sobj));
2633 GOTO(fixup_spobj, rc);
2636 /* Remove target name from target directory
2637 * Here tobj can be remote one, so we do index_delete unconditionally
2638 * and -ENOENT is allowed.
2640 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2641 mdd_object_capa(env, mdd_tpobj));
2644 /* tname might been renamed to something else */
2645 GOTO(fixup_spobj, rc);
2648 GOTO(fixup_spobj, rc);
2651 /* Insert new fid with target name into target dir */
2652 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2653 mdd_object_capa(env, mdd_tpobj));
2655 GOTO(fixup_tpobj, rc);
2657 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2658 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2660 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2662 la->la_valid = LA_CTIME;
2663 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2665 GOTO(fixup_tpobj, rc);
2668 /* Remove old target object
2669 * For tobj is remote case cmm layer has processed
2670 * and set tobj to NULL then. So when tobj is NOT NULL,
2671 * it must be local one.
2673 if (tobj && mdd_object_exists(mdd_tobj)) {
2674 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2676 if (mdd_is_dead_obj(mdd_tobj)) {
2677 /* shld not be dead, something is wrong */
2678 CERROR("tobj is dead, something is wrong\n");
2682 mdo_ref_del(env, mdd_tobj, handle);
2684 /* Remove dot reference. */
2685 if (S_ISDIR(tattr->la_mode))
2686 mdo_ref_del(env, mdd_tobj, handle);
2689 /* fetch updated nlink */
2690 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2692 CERROR("%s: Failed to get nlink for tobj "
2694 mdd2obd_dev(mdd)->obd_name,
2695 PFID(tpobj_fid), rc);
2696 GOTO(fixup_tpobj, rc);
2699 la->la_valid = LA_CTIME;
2700 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2702 CERROR("%s: Failed to set ctime for tobj "
2704 mdd2obd_dev(mdd)->obd_name,
2705 PFID(tpobj_fid), rc);
2706 GOTO(fixup_tpobj, rc);
2709 /* XXX: this transfer to ma will be removed with LOD/OSP */
2710 ma->ma_attr = *tattr;
2711 ma->ma_valid |= MA_INODE;
2712 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2714 CERROR("%s: Failed to unlink tobj "
2716 mdd2obd_dev(mdd)->obd_name,
2717 PFID(tpobj_fid), rc);
2718 GOTO(fixup_tpobj, rc);
2721 /* fetch updated nlink */
2722 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2724 CERROR("%s: Failed to get nlink for tobj "
2726 mdd2obd_dev(mdd)->obd_name,
2727 PFID(tpobj_fid), rc);
2728 GOTO(fixup_tpobj, rc);
2730 /* XXX: this transfer to ma will be removed with LOD/OSP */
2731 ma->ma_attr = *tattr;
2732 ma->ma_valid |= MA_INODE;
2734 if (tattr->la_nlink == 0) {
2735 cl_flags |= CLF_RENAME_LAST;
2736 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2737 cl_flags |= CLF_RENAME_LAST_EXISTS;
2741 la->la_valid = LA_CTIME | LA_MTIME;
2742 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2744 GOTO(fixup_tpobj, rc);
2746 if (mdd_spobj != mdd_tpobj) {
2747 la->la_valid = LA_CTIME | LA_MTIME;
2748 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2751 if (rc == 0 && mdd_sobj) {
2752 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2753 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2754 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2757 /* Old files might not have EA entry */
2758 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2759 lsname, handle, NULL, 0);
2760 mdd_write_unlock(env, mdd_sobj);
2761 /* We don't fail the transaction if the link ea can't be
2762 updated -- fid2path will use alternate lookup method. */
2770 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2773 CWARN("tp obj fix error %d\n",rc2);
2775 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2776 !mdd_is_dead_obj(mdd_tobj)) {
2778 mdo_ref_add(env, mdd_tobj, handle);
2780 mdo_ref_add(env, mdd_tobj, handle);
2783 rc2 = __mdd_index_insert(env, mdd_tpobj,
2784 mdo2fid(mdd_tobj), tname,
2789 CWARN("tp obj fix error %d\n",rc2);
2794 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2795 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2799 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2800 mdd2obd_dev(mdd)->obd_name, rc2);
2803 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2804 dotdot, handle, BYPASS_CAPA);
2806 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2807 mdd2obd_dev(mdd)->obd_name, rc2);
2812 rc2 = __mdd_index_insert(env, mdd_spobj,
2813 lf, sname, is_dir, handle, BYPASS_CAPA);
2815 CWARN("sp obj fix error %d\n",rc2);
2819 mdd_write_unlock(env, mdd_tobj);
2822 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2823 mdd_tobj, tpobj_fid, lf,
2824 spobj_fid, ltname, lsname,
2828 mdd_trans_stop(env, mdd, rc, handle);
2830 mdd_object_put(env, mdd_sobj);
2835 * During migration once the parent FID has been changed,
2836 * we need update the parent FID in linkea.
2838 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2839 struct mdd_object *parent,
2840 struct mdd_object *child,
2841 const char *name, int namelen,
2842 struct thandle *handle,
2845 struct mdd_thread_info *info = mdd_env_info(env);
2846 struct linkea_data ldata = {0};
2847 struct lu_buf *buf = &info->mti_link_buf;
2853 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2854 if (buf->lb_buf == NULL)
2858 rc = mdd_links_read(env, child, &ldata);
2860 if (rc == -ENOENT || rc == -ENODATA)
2865 LASSERT(ldata.ld_leh != NULL);
2866 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2867 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2868 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2869 struct lu_name lname;
2872 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2875 if (strncmp(lname.ln_name, name, namelen) != 0 ||
2876 lu_fid_eq(&fid, mdd_object_fid(parent))) {
2877 ldata.ld_lee = (struct link_ea_entry *)
2878 ((char *)ldata.ld_lee +
2883 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
2884 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
2885 lname.ln_namelen, lname.ln_name,
2886 PFID(mdd_object_fid(parent)));
2887 /* update to the new parent fid */
2888 linkea_entry_pack(ldata.ld_lee, &lname,
2889 mdd_object_fid(parent));
2891 rc = mdd_declare_links_add(env, child, handle, &ldata);
2893 rc = mdd_links_write(env, child, &ldata, handle);
2899 static int mdd_linkea_declare_update_child(const struct lu_env *env,
2900 struct mdd_object *parent,
2901 struct mdd_object *child,
2902 const char *name, int namelen,
2903 struct thandle *handle)
2905 return mdd_linkea_update_child_internal(env, parent, child, name,
2906 namelen, handle, true);
2909 static int mdd_linkea_update_child(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)
2915 return mdd_linkea_update_child_internal(env, parent, child, name,
2916 namelen, handle, false);
2919 static int mdd_update_linkea_internal(const struct lu_env *env,
2920 struct mdd_object *mdd_pobj,
2921 struct mdd_object *mdd_sobj,
2922 struct mdd_object *mdd_tobj,
2923 const struct lu_name *child_name,
2924 struct thandle *handle,
2927 struct mdd_thread_info *info = mdd_env_info(env);
2928 struct linkea_data *ldata = &info->mti_link_data;
2933 rc = mdd_links_read(env, mdd_sobj, ldata);
2935 if (rc == -ENOENT || rc == -ENODATA)
2941 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
2943 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
2948 /* If it is mulitple links file, we need update the name entry for
2950 LASSERT(ldata->ld_leh != NULL);
2951 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
2952 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
2953 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
2954 struct mdd_object *pobj;
2955 struct lu_name lname;
2958 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
2960 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
2962 pobj = mdd_object_find(env, mdd, &fid);
2964 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
2965 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
2970 if (!mdd_object_exists(pobj)) {
2971 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
2972 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
2976 if (pobj == mdd_pobj &&
2977 lname.ln_namelen == child_name->ln_namelen &&
2978 strncmp(lname.ln_name, child_name->ln_name,
2979 lname.ln_namelen) == 0) {
2980 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
2981 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
2986 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
2987 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
2988 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
2991 /* Remove source name from source directory */
2992 /* Insert new fid with target name into target dir */
2993 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
2998 rc = mdo_declare_index_insert(env, pobj,
2999 mdd_object_fid(mdd_tobj),
3000 lname.ln_name, handle);
3004 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3008 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3012 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3014 mdd_object_capa(env, pobj));
3018 rc = __mdd_index_insert(env, pobj,
3019 mdd_object_fid(mdd_tobj),
3020 lname.ln_name, 0, handle,
3021 mdd_object_capa(env, pobj));
3025 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3026 rc = mdo_ref_add(env, mdd_tobj, handle);
3027 mdd_write_unlock(env, mdd_tobj);
3031 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3032 mdo_ref_del(env, mdd_sobj, handle);
3033 mdd_write_unlock(env, mdd_sobj);
3036 mdd_object_put(env, pobj);
3044 static int mdd_migrate_xattrs(const struct lu_env *env,
3045 struct mdd_object *mdd_sobj,
3046 struct mdd_object *mdd_tobj)
3048 struct mdd_thread_info *info = mdd_env_info(env);
3049 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3051 struct thandle *handle;
3057 struct lu_buf list_xbuf;
3060 /* retrieve xattr list from the old object */
3061 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3062 mdd_object_capa(env, mdd_sobj));
3063 if (list_xsize == -ENODATA)
3069 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3070 if (info->mti_big_buf.lb_buf == NULL)
3073 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3074 list_xbuf.lb_len = list_xsize;
3075 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3076 mdd_object_capa(env, mdd_sobj));
3081 xname = list_xbuf.lb_buf;
3083 xlen = strnlen(xname, rem - 1) + 1;
3084 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3085 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3086 strcmp(XATTR_NAME_LMV, xname) == 0)
3089 /* For directory, if there are default layout, migrate here */
3090 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3091 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3094 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3096 mdd_object_capa(env, mdd_sobj));
3097 if (xsize == -ENODATA)
3102 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3103 if (info->mti_link_buf.lb_buf == NULL)
3104 GOTO(out, rc = -ENOMEM);
3106 xbuf.lb_len = xsize;
3107 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3108 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3109 mdd_object_capa(env, mdd_sobj));
3115 handle = mdd_trans_create(env, mdd);
3117 GOTO(out, rc = PTR_ERR(handle));
3119 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3122 GOTO(stop_trans, rc);
3123 /* Note: this transaction is part of migration, and it is not
3124 * the last step of migration, so we set th_local = 1 to avoid
3125 * update last rcvd for this transaction */
3126 handle->th_local = 1;
3127 rc = mdd_trans_start(env, mdd, handle);
3129 GOTO(stop_trans, rc);
3131 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3132 mdd_object_capa(env, mdd_sobj));
3134 GOTO(stop_trans, rc = 0);
3137 GOTO(stop_trans, rc);
3139 mdd_trans_stop(env, mdd, rc, handle);
3144 memmove(xname, xname + xlen, rem);
3150 static int mdd_declare_migrate_create(const struct lu_env *env,
3151 struct mdd_object *mdd_pobj,
3152 struct mdd_object *mdd_sobj,
3153 struct mdd_object *mdd_tobj,
3154 struct md_op_spec *spec,
3156 union lmv_mds_md *mgr_ea,
3157 struct thandle *handle)
3159 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3160 const struct lu_buf *buf;
3164 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3165 handle, spec, NULL);
3169 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3174 if (S_ISLNK(la->la_mode)) {
3175 const char *target_name = spec->u.sp_symname;
3176 int sym_len = strlen(target_name);
3177 const struct lu_buf *buf;
3179 buf = mdd_buf_get_const(env, target_name, sym_len);
3180 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3186 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3187 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3188 spec->u.sp_ea.eadatalen);
3189 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3195 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3196 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3197 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3202 la_flag->la_valid = LA_FLAGS;
3203 la_flag->la_flags = LUSTRE_IMMUTABLE_FL;
3204 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3205 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3210 static int mdd_migrate_create(const struct lu_env *env,
3211 struct mdd_object *mdd_pobj,
3212 struct mdd_object *mdd_sobj,
3213 struct mdd_object *mdd_tobj,
3216 struct mdd_thread_info *info = mdd_env_info(env);
3217 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3218 struct md_op_spec *spec = &info->mti_spec;
3219 struct lu_buf lmm_buf = { 0 };
3220 struct lu_buf link_buf = { 0 };
3221 const struct lu_buf *buf;
3222 struct thandle *handle;
3223 struct lmv_mds_md_v1 *mgr_ea;
3224 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3229 /* prepare spec for create */
3230 memset(spec, 0, sizeof(*spec));
3231 spec->sp_cr_lookup = 0;
3232 spec->sp_feat = &dt_directory_features;
3233 if (S_ISLNK(la->la_mode)) {
3234 buf = lu_buf_check_and_alloc(
3235 &mdd_env_info(env)->mti_big_buf,
3238 link_buf.lb_len = la->la_size + 1;
3239 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3241 rc = rc != 0 ? rc : -EFAULT;
3242 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3243 mdd2obd_dev(mdd)->obd_name,
3244 PFID(mdd_object_fid(mdd_sobj)), rc);
3247 spec->u.sp_symname = link_buf.lb_buf;
3249 /* retrieve lov of the old object */
3250 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3251 if (rc != 0 && rc != -ENODATA)
3253 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3254 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3255 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3256 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3260 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3261 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_MIGRATE);
3262 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3263 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3264 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_MIGRATION);
3265 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3266 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3268 handle = mdd_trans_create(env, mdd);
3270 GOTO(out_free, rc = PTR_ERR(handle));
3272 /* Note: this transaction is part of migration, and it is not
3273 * the last step of migration, so we set th_local = 1 to avoid
3274 * update last rcvd for this transaction */
3275 handle->th_local = 1;
3276 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3278 (union lmv_mds_md *)info->mti_xattr_buf,
3281 GOTO(stop_trans, rc);
3283 rc = mdd_trans_start(env, mdd, handle);
3285 GOTO(stop_trans, rc);
3287 /* create the target object */
3288 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3291 GOTO(stop_trans, rc);
3293 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3294 buf = mdd_buf_get_const(env, lmm_buf.lb_buf, lmm_buf.lb_len);
3295 rc = mdo_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3296 0, handle, mdd_object_capa(env, mdd_sobj));
3298 GOTO(stop_trans, rc);
3301 /* Set MIGRATE EA on the source inode, so once the migration needs
3302 * to be re-done during failover, the re-do process can locate the
3303 * target object which is already being created. */
3304 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3305 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3306 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3307 handle, mdd_object_capa(env, mdd_sobj));
3309 GOTO(stop_trans, rc);
3311 /* Set immutable flag, so any modification is disabled until
3312 * the migration is done. Once the migration is interrupted,
3313 * if the resume process find the migrating object has both
3314 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3315 * flag and approve the migration */
3316 la_flag->la_valid = LA_FLAGS;
3317 la_flag->la_flags = LUSTRE_IMMUTABLE_FL;
3318 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3319 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3320 mdd_object_capa(env, mdd_sobj));
3323 mdd_trans_stop(env, mdd, rc, handle);
3325 if (lmm_buf.lb_buf != NULL)
3326 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3330 static int mdd_migrate_entries(const struct lu_env *env,
3331 struct mdd_object *mdd_sobj,
3332 struct mdd_object *mdd_tobj)
3334 struct dt_object *next = mdd_object_child(mdd_sobj);
3335 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3336 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3337 struct thandle *handle;
3339 const struct dt_it_ops *iops;
3342 struct lu_dirent *ent;
3345 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3349 if (!dt_try_as_dir(env, next))
3350 GOTO(out_ent, rc = -ENOTDIR);
3352 * iterate directories
3354 iops = &next->do_index_ops->dio_it;
3355 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3356 mdd_object_capa(env, mdd_sobj));
3358 GOTO(out_ent, rc = PTR_ERR(it));
3360 rc = iops->load(env, it, 0);
3362 rc = iops->next(env, it);
3366 * At this point and across for-loop:
3368 * rc == 0 -> ok, proceed.
3369 * rc > 0 -> end of directory.
3373 struct mdd_object *child;
3374 char *name = mdd_env_info(env)->mti_key;
3378 bool target_exist = false;
3380 len = iops->key_size(env, it);
3384 result = iops->rec(env, it, (struct dt_rec *)ent,
3385 LUDA_FID | LUDA_TYPE);
3386 if (result == -ESTALE)
3393 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3394 recsize = le16_to_cpu(ent->lde_reclen);
3396 /* Insert new fid with target name into target dir */
3397 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3398 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3399 ent->lde_name[1] == '.'))
3402 child = mdd_object_find(env, mdd, &ent->lde_fid);
3404 GOTO(out, rc = PTR_ERR(child));
3406 is_dir = S_ISDIR(lu_object_attr(&child->mod_obj.mo_lu));
3408 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3410 /* Check whether the name has been inserted to the target */
3411 if (dt_try_as_dir(env, dt_tobj)) {
3412 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3414 rc = dt_tobj->do_index_ops->dio_lookup(env, dt_tobj,
3415 (struct dt_rec *)fid,
3416 (struct dt_key *)name,
3417 mdd_object_capa(env, mdd_tobj));
3418 if (unlikely(rc == 0))
3419 target_exist = true;
3422 handle = mdd_trans_create(env, mdd);
3424 GOTO(out, rc = PTR_ERR(handle));
3426 /* Note: this transaction is part of migration, and it is not
3427 * the last step of migration, so we set th_local = 1 to avoid
3428 * updating last rcvd for this transaction */
3429 handle->th_local = 1;
3430 if (likely(!target_exist)) {
3431 rc = mdo_declare_index_insert(env, mdd_tobj,
3438 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3444 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3449 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3453 /* Update .. for child */
3454 rc = mdo_declare_index_delete(env, child, dotdot,
3459 rc = mdo_declare_index_insert(env, child,
3460 mdd_object_fid(mdd_tobj),
3466 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3473 rc = mdd_trans_start(env, mdd, handle);
3475 CERROR("%s: transaction start failed: rc = %d\n",
3476 mdd2obd_dev(mdd)->obd_name, rc);
3480 if (likely(!target_exist)) {
3481 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3482 name, is_dir, handle,
3483 mdd_object_capa(env, mdd_tobj));
3488 rc = mdo_ref_add(env, mdd_tobj, handle);
3495 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3496 mdd_object_capa(env, mdd_sobj));
3501 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3502 mdd_object_capa(env, child));
3506 rc = __mdd_index_insert_only(env, child,
3507 mdd_object_fid(mdd_tobj),
3509 mdd_object_capa(env, child));
3514 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3515 strlen(name), handle);
3518 mdd_object_put(env, child);
3519 mdd_trans_stop(env, mdd, rc, handle);
3523 result = iops->next(env, it);
3524 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3525 GOTO(out, rc = -EINTR);
3527 if (result == -ESTALE)
3529 } while (result == 0);
3532 iops->fini(env, it);
3534 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3538 static int mdd_declare_update_linkea(const struct lu_env *env,
3539 struct mdd_object *mdd_pobj,
3540 struct mdd_object *mdd_sobj,
3541 struct mdd_object *mdd_tobj,
3542 struct thandle *handle,
3543 const struct lu_name *child_name)
3545 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3546 child_name, handle, 1);
3549 static int mdd_update_linkea(const struct lu_env *env,
3550 struct mdd_object *mdd_pobj,
3551 struct mdd_object *mdd_sobj,
3552 struct mdd_object *mdd_tobj,
3553 struct thandle *handle,
3554 const struct lu_name *child_name)
3556 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3557 child_name, handle, 0);
3560 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3561 struct mdd_object *mdd_pobj,
3562 struct mdd_object *mdd_sobj,
3563 struct mdd_object *mdd_tobj,
3564 const struct lu_name *lname,
3566 struct lu_attr *parent_la,
3567 struct thandle *handle)
3569 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3572 /* Revert IMMUTABLE flag */
3573 la_flag->la_valid = LA_FLAGS;
3574 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3575 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3576 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3580 /* delete entry from source dir */
3581 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3585 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3586 mdd_tobj, handle, lname);
3590 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3591 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3597 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3598 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3604 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3605 lname->ln_name, handle);
3609 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3610 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3615 /* delete old object */
3616 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3620 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3621 /* delete old object */
3622 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3625 /* set nlink to 0 */
3626 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3631 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3635 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3640 static int mdd_migrate_update_name(const struct lu_env *env,
3641 struct mdd_object *mdd_pobj,
3642 struct mdd_object *mdd_sobj,
3643 struct mdd_object *mdd_tobj,
3644 const struct lu_name *lname,
3647 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3648 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3649 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3650 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3651 struct thandle *handle;
3652 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3653 const char *name = lname->ln_name;
3657 /* update time for parent */
3658 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3659 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3660 p_la->la_valid = LA_CTIME;
3662 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3666 handle = mdd_trans_create(env, mdd);
3668 RETURN(PTR_ERR(handle));
3670 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3671 lname, so_attr, p_la, handle);
3673 /* If the migration can not be fit in one transaction, just
3674 * leave it in the original MDT */
3676 GOTO(stop_trans, rc = 0);
3678 GOTO(stop_trans, rc);
3681 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3682 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3683 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3684 PFID(mdd_object_fid(mdd_tobj)));
3686 rc = mdd_trans_start(env, mdd, handle);
3688 GOTO(stop_trans, rc);
3690 /* Revert IMMUTABLE flag */
3691 la_flag->la_valid = LA_FLAGS;
3692 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3693 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3694 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3695 mdd_object_capa(env, mdd_pobj));
3697 GOTO(stop_trans, rc);
3699 /* Remove source name from source directory */
3700 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3701 mdd_object_capa(env, mdd_pobj));
3703 GOTO(stop_trans, rc);
3705 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3708 GOTO(stop_trans, rc);
3710 if (S_ISREG(so_attr->la_mode)) {
3711 if (so_attr->la_nlink == 1) {
3712 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3714 mdd_object_capa(env, mdd_sobj));
3715 if (rc != 0 && rc != -ENODATA)
3716 GOTO(stop_trans, rc);
3720 /* Insert new fid with target name into target dir */
3721 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj), name,
3722 is_dir, handle, mdd_object_capa(env, mdd_pobj));
3724 GOTO(stop_trans, rc);
3726 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3729 GOTO(stop_trans, rc);
3731 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3732 mdo_ref_del(env, mdd_sobj, handle);
3734 mdo_ref_del(env, mdd_sobj, handle);
3736 /* Get the attr again after ref_del */
3737 rc = mdd_la_get(env, mdd_sobj, so_attr,
3738 mdd_object_capa(env, mdd_sobj));
3740 GOTO(stop_trans, rc);
3741 ma->ma_attr = *so_attr;
3742 ma->ma_valid |= MA_INODE;
3743 rc = mdd_finish_unlink(env, mdd_sobj, ma, handle);
3745 GOTO(stop_trans, rc);
3747 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3749 GOTO(stop_trans, rc);
3751 mdd_write_unlock(env, mdd_sobj);
3754 mdd_trans_stop(env, mdd, rc, handle);
3760 * Check whether we should migrate the file/dir
3762 * < 0 permission check failed or other error.
3763 * = 0 the file can be migrated.
3764 * > 0 the file does not need to be migrated, mostly
3765 * for multiple link file
3767 static int mdd_migrate_sanity_check(const struct lu_env *env,
3768 struct mdd_object *pobj,
3769 const struct lu_attr *pattr,
3770 struct mdd_object *sobj,
3771 struct lu_attr *sattr)
3773 struct mdd_thread_info *info = mdd_env_info(env);
3774 struct linkea_data *ldata = &info->mti_link_data;
3776 struct lu_buf *mgr_buf;
3782 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_MIGRATE);
3783 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3784 if (mgr_buf->lb_buf == NULL)
3787 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3788 mdd_object_capa(env, sobj));
3790 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3792 /* If the object has migrateEA, it means IMMUTE flag
3793 * is being set by previous migration process, so it
3794 * needs to override the IMMUTE flag, otherwise the
3795 * following sanity check will fail */
3796 if (le32_to_cpu(lmm->lmv_md_v1.lmv_magic) ==
3797 LMV_MAGIC_MIGRATE) {
3798 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3800 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3801 sobj->mod_flags &= ~IMMUTE_OBJ;
3802 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3803 mdd2obd_dev(mdd)->obd_name,
3804 PFID(mdd_object_fid(sobj)));
3808 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3809 sobj, sattr, NULL, NULL);
3813 /* Then it will check if the file should be migrated. If the file
3814 * has mulitple links, we only need migrate the file if all of its
3815 * entries has been migrated to the remote MDT */
3816 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3819 rc = mdd_links_read(env, sobj, ldata);
3821 /* For multiple links files, if there are no linkEA data at all,
3822 * means the file might be created before linkEA is enabled, and
3823 * all all of its links should not be migrated yet, otherwise
3824 * it should have some linkEA there */
3825 if (rc == -ENOENT || rc == -ENODATA)
3830 /* If it is mulitple links file, we need update the name entry for
3832 LASSERT(ldata->ld_leh != NULL);
3833 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3834 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3835 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3836 struct mdd_object *lpobj;
3837 struct lu_name lname;
3840 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3842 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3844 lpobj = mdd_object_find(env, mdd, &fid);
3845 if (IS_ERR(lpobj)) {
3846 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3847 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3852 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3853 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3854 PFID(&fid), lname.ln_namelen, lname.ln_name);
3855 mdd_object_put(env, lpobj);
3859 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3860 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3861 lname.ln_name, PFID(&fid));
3862 mdd_object_put(env, lpobj);
3870 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3871 struct md_object *sobj, const struct lu_name *lname,
3872 struct md_object *tobj, struct md_attr *ma)
3874 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3875 struct mdd_device *mdd = mdo2mdd(pobj);
3876 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
3877 struct mdd_object *mdd_tobj = NULL;
3878 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3879 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3883 /* If the file will being migrated, it will check whether
3884 * the file is being opened by someone else right now */
3885 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3886 if (mdd_sobj->mod_count >= 1) {
3887 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
3888 mdd2obd_dev(mdd)->obd_name,
3889 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3890 mdd_sobj->mod_count, -EBUSY);
3891 mdd_read_unlock(env, mdd_sobj);
3892 GOTO(put, rc = -EBUSY);
3894 mdd_read_unlock(env, mdd_sobj);
3896 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3900 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
3904 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
3911 /* Sigh, it is impossible to finish all of migration in a single
3912 * transaction, for example migrating big directory entries to the
3913 * new MDT, it needs insert all of name entries of children in the
3916 * So migration will be done in multiple steps and transactions.
3918 * 1. create an orphan object on the remote MDT in one transaction.
3919 * 2. migrate extend attributes to the new target file/directory.
3920 * 3. For directory, migrate the entries to the new MDT and update
3921 * linkEA of each children. Because we can not migrate all entries
3922 * in a single transaction, so the migrating directory will become
3923 * a striped directory during migration, so once the process is
3924 * interrupted, the directory is still accessible. (During lookup,
3925 * client will locate the name by searching both original and target
3927 * 4. Finally, update the name/FID to point to the new file/directory
3928 * in a separate transaction.
3931 /* step 1: Check whether the orphan object has been created, and create
3932 * orphan object on the remote MDT if needed */
3933 mdd_tobj = md2mdd_obj(tobj);
3934 if (!mdd_object_exists(mdd_tobj)) {
3935 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3941 /* step 2: migrate xattr */
3942 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
3946 /* step 3: migrate name entries to the orphan object */
3947 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
3948 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
3951 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
3952 OBD_FAIL_MDS_REINT_NET_REP)))
3956 /* step 4: update name entry to the new object */
3957 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
3965 const struct md_dir_operations mdd_dir_ops = {
3966 .mdo_is_subdir = mdd_is_subdir,
3967 .mdo_lookup = mdd_lookup,
3968 .mdo_create = mdd_create,
3969 .mdo_rename = mdd_rename,
3970 .mdo_link = mdd_link,
3971 .mdo_unlink = mdd_unlink,
3972 .mdo_create_data = mdd_create_data,
3973 .mdo_migrate = mdd_migrate,