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, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
61 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
63 if (!lu_name_is_valid(ln))
65 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
71 /* Get FID from name and parent */
73 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
74 const struct lu_attr *pattr, const struct lu_name *lname,
75 struct lu_fid* fid, int mask)
77 const char *name = lname->ln_name;
78 const struct dt_key *key = (const struct dt_key *)name;
79 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
80 struct mdd_device *m = mdo2mdd(pobj);
81 struct dt_object *dir = mdd_object_child(mdd_obj);
85 if (unlikely(mdd_is_dead_obj(mdd_obj)))
88 if (!mdd_object_exists(mdd_obj))
91 if (mdd_object_remote(mdd_obj)) {
92 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
93 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
96 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
101 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
102 dt_try_as_dir(env, dir)))
103 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
110 int mdd_lookup(const struct lu_env *env,
111 struct md_object *pobj, const struct lu_name *lname,
112 struct lu_fid *fid, struct md_op_spec *spec)
114 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
118 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
122 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
123 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
128 * Get parent FID of the directory
130 * Read parent FID from linkEA, if that fails, then do lookup
131 * dotdot to get the parent FID.
133 * \param[in] env execution environment
134 * \param[in] obj object from which to find the parent FID
135 * \param[in] attr attribute of the object
136 * \param[out] fid fid to get the parent FID
138 * \retval 0 if getting the parent FID succeeds.
139 * \retval negative errno if getting the parent FID fails.
141 static inline int mdd_parent_fid(const struct lu_env *env,
142 struct mdd_object *obj,
143 const struct lu_attr *attr,
146 struct mdd_thread_info *info = mdd_env_info(env);
147 struct linkea_data ldata = { NULL };
148 struct lu_buf *buf = &info->mti_link_buf;
149 struct lu_name lname;
154 LASSERT(S_ISDIR(mdd_object_type(obj)));
156 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
157 if (buf->lb_buf == NULL)
158 GOTO(lookup, rc = 0);
161 rc = mdd_links_read(env, obj, &ldata);
165 LASSERT(ldata.ld_leh != NULL);
166 /* Directory should only have 1 parent */
167 if (ldata.ld_leh->leh_reccount > 1)
170 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
172 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
173 if (likely(fid_is_sane(fid)))
176 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
181 * For root fid use special function, which does not compare version component
182 * of fid. Version component is different for root fids on all MDTs.
184 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
186 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
187 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
191 * return 1: if lf is the fid of the ancestor of p1;
194 * return -EREMOTE: if remote object is found, in this
195 * case fid of remote object is saved to @pf;
197 * otherwise: values < 0, errors.
199 static int mdd_is_parent(const struct lu_env *env,
200 struct mdd_device *mdd,
201 struct mdd_object *p1,
202 const struct lu_attr *attr,
203 const struct lu_fid *lf,
206 struct mdd_object *parent = NULL;
211 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
212 pfid = &mdd_env_info(env)->mti_fid;
214 /* Check for root first. */
215 if (mdd_is_root(mdd, mdo2fid(p1)))
219 /* this is done recursively */
220 rc = mdd_parent_fid(env, p1, attr, pfid);
223 if (mdd_is_root(mdd, pfid))
225 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
227 if (lu_fid_eq(pfid, lf))
230 mdd_object_put(env, parent);
232 parent = mdd_object_find(env, mdd, pfid);
234 GOTO(out, rc = PTR_ERR(parent));
236 if (!mdd_object_exists(parent))
237 GOTO(out, rc = -EINVAL);
243 if (parent && !IS_ERR(parent))
244 mdd_object_put(env, parent);
249 * No permission check is needed.
251 * returns 1: if fid is ancestor of @mo;
252 * returns 0: if fid is not an ancestor of @mo;
254 * returns EREMOTE if remote object is found, fid of remote object is saved to
257 * returns < 0: if error
259 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
260 const struct lu_fid *fid, struct lu_fid *sfid)
262 struct mdd_device *mdd = mdo2mdd(mo);
263 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
267 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
270 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
274 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
278 } else if (rc == 1) {
279 /* found @fid is parent */
287 * Check that @dir contains no entries except (possibly) dot and dotdot.
292 * -ENOTDIR not a directory object
293 * -ENOTEMPTY not empty
297 static int mdd_dir_is_empty(const struct lu_env *env,
298 struct mdd_object *dir)
301 struct dt_object *obj;
302 const struct dt_it_ops *iops;
306 obj = mdd_object_child(dir);
307 if (!dt_try_as_dir(env, obj))
310 iops = &obj->do_index_ops->dio_it;
311 it = iops->init(env, obj, LUDA_64BITHASH);
313 result = iops->get(env, it, (const struct dt_key *)"");
316 for (result = 0, i = 0; result == 0 && i < 3; ++i)
317 result = iops->next(env, it);
320 else if (result == 1)
322 } else if (result == 0)
324 * Huh? Index contains no zero key?
331 result = PTR_ERR(it);
336 * Determine if the target object can be hard linked, and right now it only
337 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
338 * directory nlink count might exceed the maximum link count(see
339 * osd_object_ref_add), so it only check nlink for non-directories.
341 * \param[in] env thread environment
342 * \param[in] obj object being linked to
343 * \param[in] la attributes of \a obj
345 * \retval 0 if \a obj can be hard linked
346 * \retval negative error if \a obj is a directory or has too
349 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
350 const struct lu_attr *la)
352 struct mdd_device *m = mdd_obj2mdd_dev(obj);
357 /* Subdir count limitation can be broken through
358 * (see osd_object_ref_add), so only check non-directory here. */
359 if (!S_ISDIR(la->la_mode) &&
360 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
367 * Check whether it may create the cobj under the pobj.
369 * \param[in] env execution environment
370 * \param[in] pobj the parent directory
371 * \param[in] pattr the attribute of the parent directory
372 * \param[in] cobj the child to be created
373 * \param[in] check_perm if check WRITE|EXEC permission for parent
375 * \retval = 0 create the child under this dir is allowed
376 * \retval negative errno create the child under this dir is
379 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
380 const struct lu_attr *pattr, struct mdd_object *cobj,
383 struct mdd_thread_info *info = mdd_env_info(env);
388 if (cobj && mdd_object_exists(cobj))
391 if (mdd_is_dead_obj(pobj))
394 /* If the parent is a sub-stripe, check whether it is dead */
395 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
396 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV);
397 if (unlikely(rc > 0)) {
398 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
400 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
401 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
407 rc = mdd_permission_internal_locked(env, pobj, pattr,
408 MAY_WRITE | MAY_EXEC,
414 * Check whether can unlink from the pobj in the case of "cobj == NULL".
416 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
417 const struct lu_attr *pattr, const struct lu_attr *attr)
422 if (mdd_is_dead_obj(pobj))
425 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
428 rc = mdd_permission_internal_locked(env, pobj, pattr,
429 MAY_WRITE | MAY_EXEC,
434 if (pattr->la_flags & LUSTRE_APPEND_FL)
441 * pobj == NULL is remote ops case, under such case, pobj's
442 * VTX feature has been checked already, no need check again.
444 static inline int mdd_is_sticky(const struct lu_env *env,
445 struct mdd_object *pobj,
446 const struct lu_attr *pattr,
447 struct mdd_object *cobj,
448 const struct lu_attr *cattr)
450 struct lu_ucred *uc = lu_ucred_assert(env);
453 LASSERT(pattr != NULL);
454 if (!(pattr->la_mode & S_ISVTX) ||
455 (pattr->la_uid == uc->uc_fsuid))
459 LASSERT(cattr != NULL);
460 if (cattr->la_uid == uc->uc_fsuid)
463 return !md_capable(uc, CFS_CAP_FOWNER);
466 static int mdd_may_delete_entry(const struct lu_env *env,
467 struct mdd_object *pobj,
468 const struct lu_attr *pattr,
473 LASSERT(pobj != NULL);
474 if (!mdd_object_exists(pobj))
477 if (mdd_is_dead_obj(pobj))
482 rc = mdd_permission_internal_locked(env, pobj, pattr,
483 MAY_WRITE | MAY_EXEC,
489 if (pattr->la_flags & LUSTRE_APPEND_FL)
496 * Check whether it may delete the cobj from the pobj.
499 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
500 const struct lu_attr *tpattr, struct mdd_object *tobj,
501 const struct lu_attr *tattr, const struct lu_attr *cattr,
502 int check_perm, int check_empty)
508 LASSERT(tpattr != NULL);
509 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
517 if (!mdd_object_exists(tobj))
520 if (mdd_is_dead_obj(tobj))
523 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
526 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
529 /* additional check the rename case */
531 if (S_ISDIR(cattr->la_mode)) {
532 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
534 if (!S_ISDIR(tattr->la_mode))
537 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
539 } else if (S_ISDIR(tattr->la_mode))
543 if (S_ISDIR(tattr->la_mode) && check_empty)
544 rc = mdd_dir_is_empty(env, tobj);
550 * Check whether it can create the link file(linked to @src_obj) under
551 * the target directory(@tgt_obj), and src_obj has been locked by
554 * \param[in] env execution environment
555 * \param[in] tgt_obj the target directory
556 * \param[in] tattr attributes of target directory
557 * \param[in] lname the link name
558 * \param[in] src_obj source object for link
559 * \param[in] cattr attributes for source object
561 * \retval = 0 it is allowed to create the link file under tgt_obj
562 * \retval negative error not allowed to create the link file
564 static int mdd_link_sanity_check(const struct lu_env *env,
565 struct mdd_object *tgt_obj,
566 const struct lu_attr *tattr,
567 const struct lu_name *lname,
568 struct mdd_object *src_obj,
569 const struct lu_attr *cattr)
571 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
575 if (!mdd_object_exists(src_obj))
578 if (mdd_is_dead_obj(src_obj))
581 /* Local ops, no lookup before link, check filename length here. */
582 rc = mdd_name_check(m, lname);
586 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
589 if (S_ISDIR(mdd_object_type(src_obj)))
592 LASSERT(src_obj != tgt_obj);
593 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
597 rc = __mdd_may_link(env, src_obj, cattr);
602 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
603 const char *name, struct thandle *handle)
605 struct dt_object *next = mdd_object_child(pobj);
609 if (dt_try_as_dir(env, next))
610 rc = dt_delete(env, next, (struct dt_key *)name, handle);
617 static int __mdd_index_insert_only(const struct lu_env *env,
618 struct mdd_object *pobj,
619 const struct lu_fid *lf, __u32 type,
621 struct thandle *handle)
623 struct dt_object *next = mdd_object_child(pobj);
627 if (dt_try_as_dir(env, next)) {
628 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
629 struct lu_ucred *uc = lu_ucred_check(env);
633 rec->rec_type = type;
634 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
635 rc = dt_insert(env, next, (const struct dt_rec *)rec,
636 (const struct dt_key *)name, handle,
644 /* insert named index, add reference if isdir */
645 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
646 const struct lu_fid *lf, __u32 type,
647 const char *name, struct thandle *handle)
652 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
653 if (rc == 0 && S_ISDIR(type)) {
654 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
655 mdo_ref_add(env, pobj, handle);
656 mdd_write_unlock(env, pobj);
662 /* delete named index, drop reference if isdir */
663 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
664 const char *name, int is_dir,
665 struct thandle *handle)
670 rc = __mdd_index_delete_only(env, pobj, name, handle);
671 if (rc == 0 && is_dir) {
672 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
673 mdo_ref_del(env, pobj, handle);
674 mdd_write_unlock(env, pobj);
680 static int mdd_llog_record_calc_size(const struct lu_env *env,
681 const struct lu_name *tname,
682 const struct lu_name *sname)
684 const struct lu_ucred *uc = lu_ucred(env);
685 enum changelog_rec_flags crf = 0;
686 size_t hdr_size = sizeof(struct llog_changelog_rec) -
687 sizeof(struct changelog_rec);
692 if (uc != NULL && uc->uc_jobid[0] != '\0')
695 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
696 (tname != NULL ? tname->ln_namelen : 0) +
697 (sname != NULL ? 1 + sname->ln_namelen : 0));
700 int mdd_declare_changelog_store(const struct lu_env *env,
701 struct mdd_device *mdd,
702 const struct lu_name *tname,
703 const struct lu_name *sname,
704 struct thandle *handle)
706 struct obd_device *obd = mdd2obd_dev(mdd);
707 struct llog_ctxt *ctxt;
708 struct llog_changelog_rec *rec;
710 struct thandle *llog_th;
715 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
718 reclen = mdd_llog_record_calc_size(env, tname, sname);
719 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
720 if (buf->lb_buf == NULL)
724 rec->cr_hdr.lrh_len = reclen;
725 rec->cr_hdr.lrh_type = CHANGELOG_REC;
727 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
731 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
733 GOTO(out_put, rc = PTR_ERR(llog_th));
735 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
743 /** Add a changelog entry \a rec to the changelog llog
746 * \param handle - currently ignored since llogs start their own transaction;
747 * this will hopefully be fixed in llog rewrite
750 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
751 struct llog_changelog_rec *rec, struct thandle *th)
753 struct obd_device *obd = mdd2obd_dev(mdd);
754 struct llog_ctxt *ctxt;
755 struct thandle *llog_th;
758 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
759 changelog_rec_varsize(&rec->cr));
761 /* llog_lvfs_write_rec sets the llog tail len */
762 rec->cr_hdr.lrh_type = CHANGELOG_REC;
763 rec->cr.cr_time = cl_time();
765 spin_lock(&mdd->mdd_cl.mc_lock);
766 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
767 * but as long as the MDD transactions are ordered correctly for e.g.
768 * rename conflicts, I don't think this should matter. */
769 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
770 spin_unlock(&mdd->mdd_cl.mc_lock);
772 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
776 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
778 GOTO(out_put, rc = PTR_ERR(llog_th));
780 /* nested journal transaction */
781 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
790 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
791 const struct lu_fid *sfid,
792 const struct lu_fid *spfid,
793 const struct lu_name *sname)
795 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
796 size_t extsize = sname->ln_namelen + 1;
798 LASSERT(sfid != NULL);
799 LASSERT(spfid != NULL);
800 LASSERT(sname != NULL);
802 rnm->cr_sfid = *sfid;
803 rnm->cr_spfid = *spfid;
805 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
806 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
807 rec->cr_namelen += extsize;
810 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
812 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
814 if (jobid == NULL || jobid[0] == '\0')
817 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
820 /** Store a namespace change changelog record
821 * If this fails, we must fail the whole transaction; we don't
822 * want the change to commit without the log entry.
823 * \param target - mdd_object of change
824 * \param tpfid - target parent dir/object fid
825 * \param sfid - source object fid
826 * \param spfid - source parent fid
827 * \param tname - target name string
828 * \param sname - source name string
829 * \param handle - transaction handle
831 int mdd_changelog_ns_store(const struct lu_env *env,
832 struct mdd_device *mdd,
833 enum changelog_rec_type type,
834 enum changelog_rec_flags crf,
835 struct mdd_object *target,
836 const struct lu_fid *tpfid,
837 const struct lu_fid *sfid,
838 const struct lu_fid *spfid,
839 const struct lu_name *tname,
840 const struct lu_name *sname,
841 struct thandle *handle)
843 const struct lu_ucred *uc = lu_ucred(env);
844 struct llog_changelog_rec *rec;
851 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
854 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
857 LASSERT(tpfid != NULL);
858 LASSERT(tname != NULL);
859 LASSERT(handle != NULL);
861 reclen = mdd_llog_record_calc_size(env, tname, sname);
862 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
863 if (buf->lb_buf == NULL)
869 if (uc != NULL && uc->uc_jobid[0] != '\0')
877 rec->cr.cr_flags = crf;
878 rec->cr.cr_type = (__u32)type;
879 rec->cr.cr_pfid = *tpfid;
880 rec->cr.cr_namelen = tname->ln_namelen;
881 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
883 if (crf & CLF_RENAME)
884 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
887 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
889 if (likely(target != NULL)) {
890 rec->cr.cr_tfid = *mdo2fid(target);
891 target->mod_cltime = cfs_time_current_64();
893 fid_zero(&rec->cr.cr_tfid);
896 rc = mdd_changelog_store(env, mdd, rec, handle);
898 CERROR("%s: cannot store changelog record: type = %d, "
899 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
900 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
901 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
908 static int __mdd_links_add(const struct lu_env *env,
909 struct mdd_object *mdd_obj,
910 struct linkea_data *ldata,
911 const struct lu_name *lname,
912 const struct lu_fid *pfid,
913 int first, int check)
917 if (ldata->ld_leh == NULL) {
918 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
922 rc = linkea_data_new(ldata,
923 &mdd_env_info(env)->mti_link_buf);
930 rc = linkea_links_find(ldata, lname, pfid);
931 if (rc && rc != -ENOENT)
937 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
938 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
942 linkea_add_buf(ldata, lname, tfid);
945 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
946 linkea_add_buf(ldata, lname, pfid);
948 return linkea_add_buf(ldata, lname, pfid);
951 static int __mdd_links_del(const struct lu_env *env,
952 struct mdd_object *mdd_obj,
953 struct linkea_data *ldata,
954 const struct lu_name *lname,
955 const struct lu_fid *pfid)
959 if (ldata->ld_leh == NULL) {
960 rc = mdd_links_read(env, mdd_obj, ldata);
965 rc = linkea_links_find(ldata, lname, pfid);
969 linkea_del_buf(ldata, lname);
973 static int mdd_linkea_prepare(const struct lu_env *env,
974 struct mdd_object *mdd_obj,
975 const struct lu_fid *oldpfid,
976 const struct lu_name *oldlname,
977 const struct lu_fid *newpfid,
978 const struct lu_name *newlname,
979 int first, int check,
980 struct linkea_data *ldata)
986 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
989 LASSERT(oldpfid != NULL || newpfid != NULL);
991 if (mdd_obj->mod_flags & DEAD_OBJ) {
992 /* Prevent linkea to be updated which is NOT necessary. */
993 ldata->ld_reclen = 0;
994 /* No more links, don't bother */
998 if (oldpfid != NULL) {
999 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1002 (rc != -ENODATA && rc != -ENOENT))
1004 /* No changes done. */
1009 /* If renaming, add the new record */
1010 if (newpfid != NULL) {
1011 /* even if the add fails, we still delete the out-of-date
1013 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1017 rc = rc != 0 ? rc : rc2;
1022 int mdd_links_rename(const struct lu_env *env,
1023 struct mdd_object *mdd_obj,
1024 const struct lu_fid *oldpfid,
1025 const struct lu_name *oldlname,
1026 const struct lu_fid *newpfid,
1027 const struct lu_name *newlname,
1028 struct thandle *handle,
1029 struct linkea_data *ldata,
1030 int first, int check)
1035 if (ldata == NULL) {
1036 ldata = &mdd_env_info(env)->mti_link_data;
1037 memset(ldata, 0, sizeof(*ldata));
1038 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1039 newpfid, newlname, first, check,
1045 if (ldata->ld_reclen != 0)
1046 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1051 if (rc == -EOVERFLOW || rc == -ENOSPC)
1053 if (newlname == NULL)
1054 CDEBUG(error ? D_ERROR : D_OTHER,
1055 "link_ea add failed %d "DFID"\n",
1056 rc, PFID(mdd_object_fid(mdd_obj)));
1057 else if (oldpfid == NULL)
1058 CDEBUG(error ? D_ERROR : D_OTHER,
1059 "link_ea add '%.*s' failed %d "DFID"\n",
1060 newlname->ln_namelen, newlname->ln_name,
1061 rc, PFID(mdd_object_fid(mdd_obj)));
1062 else if (newpfid == NULL)
1063 CDEBUG(error ? D_ERROR : D_OTHER,
1064 "link_ea del '%.*s' failed %d "DFID"\n",
1065 oldlname->ln_namelen, oldlname->ln_name,
1066 rc, PFID(mdd_object_fid(mdd_obj)));
1068 CDEBUG(error ? D_ERROR : D_OTHER,
1069 "link_ea rename '%.*s'->'%.*s' failed %d "
1071 oldlname->ln_namelen, oldlname->ln_name,
1072 newlname->ln_namelen, newlname->ln_name,
1073 rc, PFID(mdd_object_fid(mdd_obj)));
1076 if (is_vmalloc_addr(ldata->ld_buf))
1077 /* if we vmalloced a large buffer drop it */
1078 lu_buf_free(ldata->ld_buf);
1083 static inline int mdd_links_add(const struct lu_env *env,
1084 struct mdd_object *mdd_obj,
1085 const struct lu_fid *pfid,
1086 const struct lu_name *lname,
1087 struct thandle *handle,
1088 struct linkea_data *ldata, int first)
1090 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1091 pfid, lname, handle, ldata, first, 0);
1094 static inline int mdd_links_del(const struct lu_env *env,
1095 struct mdd_object *mdd_obj,
1096 const struct lu_fid *pfid,
1097 const struct lu_name *lname,
1098 struct thandle *handle)
1100 return mdd_links_rename(env, mdd_obj, pfid, lname,
1101 NULL, NULL, handle, NULL, 0, 0);
1104 /** Read the link EA into a temp buffer.
1105 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1106 * A pointer to the buffer is stored in \a ldata::ld_buf.
1108 * \retval 0 or error
1110 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1111 struct linkea_data *ldata)
1115 if (!mdd_object_exists(mdd_obj))
1118 /* First try a small buf */
1119 LASSERT(env != NULL);
1120 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1122 if (ldata->ld_buf->lb_buf == NULL)
1125 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
1126 if (rc == -ERANGE) {
1127 /* Buf was too small, figure out what we need. */
1128 lu_buf_free(ldata->ld_buf);
1129 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1133 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1134 if (ldata->ld_buf->lb_buf == NULL)
1136 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1140 lu_buf_free(ldata->ld_buf);
1141 ldata->ld_buf = NULL;
1145 return linkea_init(ldata);
1148 /** Read the link EA into a temp buffer.
1149 * Uses the name_buf since it is generally large.
1150 * \retval IS_ERR err
1151 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1153 struct lu_buf *mdd_links_get(const struct lu_env *env,
1154 struct mdd_object *mdd_obj)
1156 struct linkea_data ldata = { NULL };
1159 rc = mdd_links_read(env, mdd_obj, &ldata);
1160 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1163 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1164 struct linkea_data *ldata, struct thandle *handle)
1166 const struct lu_buf *buf;
1169 if (ldata == NULL || ldata->ld_buf == NULL ||
1170 ldata->ld_leh == NULL)
1173 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1174 ldata->ld_leh->leh_len);
1175 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1178 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1179 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1180 mdd_object_remote(mdd_obj) == 0) {
1181 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1182 struct thandle *sub_th;
1184 /* XXX: If the linkEA is overflow, then we need to notify the
1185 * namespace LFSCK to skip "nlink" attribute verification
1186 * on this object to avoid the "nlink" to be shrinked by
1187 * wrong. It may be not good an interaction with LFSCK
1188 * like this. We will consider to replace it with other
1189 * mechanism in future. LU-5802. */
1190 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1191 LFSCK_TYPE_NAMESPACE);
1193 sub_th = thandle_get_sub_by_dt(env, handle,
1194 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1195 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1202 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1203 struct thandle *handle, struct linkea_data *ldata,
1204 enum mdd_links_add_overflow overflow)
1210 if (ldata != NULL && ldata->ld_leh != NULL) {
1211 ea_len = ldata->ld_leh->leh_len;
1212 linkea = ldata->ld_buf->lb_buf;
1214 ea_len = DEFAULT_LINKEA_SIZE;
1218 /* XXX: max size? */
1219 rc = mdo_declare_xattr_set(env, mdd_obj,
1220 mdd_buf_get_const(env, linkea, ea_len),
1221 XATTR_NAME_LINK, 0, handle);
1225 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1226 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1227 struct thandle *sub_th;
1229 /* XXX: If the linkEA is overflow, then we need to notify the
1230 * namespace LFSCK to skip "nlink" attribute verification
1231 * on this object to avoid the "nlink" to be shrinked by
1232 * wrong. It may be not good an interaction with LFSCK
1233 * like this. We will consider to replace it with other
1234 * mechanism in future. LU-5802. */
1235 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1236 LFSCK_TYPE_NAMESPACE);
1238 sub_th = thandle_get_sub_by_dt(env, handle,
1239 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1240 rc = lfsck_in_notify(env,
1241 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1248 static inline int mdd_declare_links_del(const struct lu_env *env,
1249 struct mdd_object *c,
1250 struct thandle *handle)
1254 /* For directory, the linkEA will be removed together
1255 * with the object. */
1256 if (!S_ISDIR(mdd_object_type(c)))
1257 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1262 static int mdd_declare_link(const struct lu_env *env,
1263 struct mdd_device *mdd,
1264 struct mdd_object *p,
1265 struct mdd_object *c,
1266 const struct lu_name *name,
1267 struct thandle *handle,
1269 struct linkea_data *data)
1273 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1274 name->ln_name, handle);
1278 rc = mdo_declare_ref_add(env, c, handle);
1282 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1283 rc = mdo_declare_ref_add(env, c, handle);
1288 la->la_valid = LA_CTIME | LA_MTIME;
1289 rc = mdo_declare_attr_set(env, p, la, handle);
1293 la->la_valid = LA_CTIME;
1294 rc = mdo_declare_attr_set(env, c, la, handle);
1298 rc = mdd_declare_links_add(env, c, handle, data,
1299 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1303 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1308 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1309 struct md_object *src_obj, const struct lu_name *lname,
1312 const char *name = lname->ln_name;
1313 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1314 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1315 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1316 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1317 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1318 struct mdd_device *mdd = mdo2mdd(src_obj);
1319 struct thandle *handle;
1320 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1324 rc = mdd_la_get(env, mdd_sobj, cattr);
1328 rc = mdd_la_get(env, mdd_tobj, tattr);
1332 handle = mdd_trans_create(env, mdd);
1334 GOTO(out_pending, rc = PTR_ERR(handle));
1336 memset(ldata, 0, sizeof(*ldata));
1338 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1339 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1341 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1346 rc = mdd_trans_start(env, mdd, handle);
1350 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1351 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1354 GOTO(out_unlock, rc);
1356 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1357 rc = mdo_ref_add(env, mdd_sobj, handle);
1359 GOTO(out_unlock, rc);
1362 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1363 rc = mdo_ref_add(env, mdd_sobj, handle);
1365 GOTO(out_unlock, rc);
1368 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1369 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1372 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1373 mdd_object_type(mdd_sobj),
1376 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1377 mdd_object_type(mdd_sobj),
1382 mdo_ref_del(env, mdd_sobj, handle);
1383 GOTO(out_unlock, rc);
1386 la->la_valid = LA_CTIME | LA_MTIME;
1387 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1389 GOTO(out_unlock, rc);
1391 la->la_valid = LA_CTIME;
1392 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1394 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1395 mdo2fid(mdd_tobj), lname, 0, 0,
1398 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1399 lname, handle, ldata, 0);
1400 /* The failure of links_add should not cause the link
1401 * failure, reset rc here */
1406 mdd_write_unlock(env, mdd_sobj);
1408 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1409 mdo2fid(mdd_tobj), NULL, NULL,
1410 lname, NULL, handle);
1412 mdd_trans_stop(env, mdd, rc, handle);
1414 if (is_vmalloc_addr(ldata->ld_buf))
1415 /* if we vmalloced a large buffer drop it */
1416 lu_buf_free(ldata->ld_buf);
1421 static int mdd_mark_dead_object(const struct lu_env *env,
1422 struct mdd_object *obj, struct thandle *handle,
1425 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1429 obj->mod_flags |= DEAD_OBJ;
1431 if (!S_ISDIR(mdd_object_type(obj)))
1434 attr->la_valid = LA_FLAGS;
1435 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1438 rc = mdo_declare_attr_set(env, obj, attr, handle);
1440 rc = mdo_attr_set(env, obj, attr, handle);
1445 static int mdd_declare_finish_unlink(const struct lu_env *env,
1446 struct mdd_object *obj,
1447 struct thandle *handle)
1451 rc = mdd_mark_dead_object(env, obj, handle, true);
1455 rc = mdo_declare_destroy(env, obj, handle);
1459 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1463 return mdd_declare_links_del(env, obj, handle);
1466 /* caller should take a lock before calling */
1467 int mdd_finish_unlink(const struct lu_env *env,
1468 struct mdd_object *obj, struct md_attr *ma,
1469 const struct mdd_object *pobj,
1470 const struct lu_name *lname,
1474 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1477 LASSERT(mdd_write_locked(env, obj) != 0);
1479 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1480 rc = mdd_mark_dead_object(env, obj, th, false);
1484 /* add new orphan and the object
1485 * will be deleted during mdd_close() */
1486 if (obj->mod_count) {
1487 rc = __mdd_orphan_add(env, obj, th);
1489 CDEBUG(D_HA, "Object "DFID" is inserted into "
1490 "orphan list, open count = %d\n",
1491 PFID(mdd_object_fid(obj)),
1494 CERROR("Object "DFID" fail to be an orphan, "
1495 "open count = %d, maybe cause failed "
1497 PFID(mdd_object_fid(obj)),
1500 rc = mdo_destroy(env, obj, th);
1502 } else if (!is_dir) {
1503 /* old files may not have link ea; ignore errors */
1504 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1512 * has mdd_write_lock on cobj already, but not on pobj yet
1514 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1515 const struct lu_attr *pattr,
1516 struct mdd_object *cobj,
1517 const struct lu_attr *cattr)
1522 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1527 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1528 struct mdd_object *p, struct mdd_object *c,
1529 const struct lu_name *name, struct md_attr *ma,
1530 struct thandle *handle, int no_name, int is_dir)
1532 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1535 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1536 if (likely(no_name == 0)) {
1537 rc = mdo_declare_index_delete(env, p, name->ln_name,
1544 rc = mdo_declare_ref_del(env, p, handle);
1550 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1551 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1552 la->la_valid = LA_CTIME | LA_MTIME;
1553 rc = mdo_declare_attr_set(env, p, la, handle);
1558 rc = mdo_declare_ref_del(env, c, handle);
1562 rc = mdo_declare_ref_del(env, c, handle);
1566 la->la_valid = LA_CTIME;
1567 rc = mdo_declare_attr_set(env, c, la, handle);
1571 rc = mdd_declare_finish_unlink(env, c, handle);
1575 /* FIXME: need changelog for remove entry */
1576 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1583 * test if a file has an HSM archive
1584 * if HSM attributes are not found in ma update them from
1587 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1588 struct mdd_object *obj,
1593 if (!(ma->ma_valid & MA_HSM)) {
1594 /* no HSM MD provided, read xattr */
1595 struct lu_buf *hsm_buf;
1596 const size_t buflen = sizeof(struct hsm_attrs);
1599 hsm_buf = mdd_buf_get(env, NULL, 0);
1600 lu_buf_alloc(hsm_buf, buflen);
1601 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1602 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1603 lu_buf_free(hsm_buf);
1607 ma->ma_valid = MA_HSM;
1609 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1615 * Delete name entry and the object.
1616 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1617 * does not exist for this object, and it could only happen during resending
1618 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1619 * is also needed in this case(needed by changelog), so we have to add another
1620 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1621 * the ENOENT failure should be able to be fixed by redo mechanism.
1623 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1624 struct md_object *cobj, const struct lu_name *lname,
1625 struct md_attr *ma, int no_name)
1627 const char *name = lname->ln_name;
1628 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1629 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1630 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1631 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1632 struct mdd_object *mdd_cobj = NULL;
1633 struct mdd_device *mdd = mdo2mdd(pobj);
1634 struct thandle *handle;
1638 /* cobj == NULL means only delete name entry */
1639 if (likely(cobj != NULL)) {
1640 mdd_cobj = md2mdd_obj(cobj);
1641 if (mdd_object_exists(mdd_cobj) == 0)
1645 rc = mdd_la_get(env, mdd_pobj, pattr);
1649 if (likely(mdd_cobj != NULL)) {
1651 rc = mdd_la_get(env, mdd_cobj, cattr);
1655 is_dir = S_ISDIR(cattr->la_mode);
1658 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1662 handle = mdd_trans_create(env, mdd);
1664 RETURN(PTR_ERR(handle));
1666 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1667 lname, ma, handle, no_name, is_dir);
1671 rc = mdd_trans_start(env, mdd, handle);
1675 if (likely(mdd_cobj != NULL))
1676 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1678 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1679 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1684 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1685 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1686 GOTO(cleanup, rc = 0);
1688 if (likely(mdd_cobj != NULL)) {
1689 rc = mdo_ref_del(env, mdd_cobj, handle);
1691 __mdd_index_insert_only(env, mdd_pobj,
1693 mdd_object_type(mdd_cobj),
1700 mdo_ref_del(env, mdd_cobj, handle);
1702 /* fetch updated nlink */
1703 rc = mdd_la_get(env, mdd_cobj, cattr);
1708 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1709 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1711 la->la_valid = LA_CTIME | LA_MTIME;
1712 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1716 /* Enough for only unlink the entry */
1717 if (unlikely(mdd_cobj == NULL))
1720 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1721 /* update ctime of an unlinked file only if it is still
1722 * opened or a link still exists */
1723 la->la_valid = LA_CTIME;
1724 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1729 /* XXX: this transfer to ma will be removed with LOD/OSP */
1730 ma->ma_attr = *cattr;
1731 ma->ma_valid |= MA_INODE;
1732 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1734 /* fetch updated nlink */
1736 rc = mdd_la_get(env, mdd_cobj, cattr);
1738 /* if object is removed then we can't get its attrs, use last get */
1739 if (cattr->la_nlink == 0) {
1740 ma->ma_attr = *cattr;
1741 ma->ma_valid |= MA_INODE;
1745 if (likely(mdd_cobj != NULL))
1746 mdd_write_unlock(env, mdd_cobj);
1751 if (cattr->la_nlink == 0) {
1752 cl_flags |= CLF_UNLINK_LAST;
1753 /* search for an existing archive */
1754 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1755 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1758 rc = mdd_changelog_ns_store(env, mdd,
1759 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1760 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1765 mdd_trans_stop(env, mdd, rc, handle);
1771 * The permission has been checked when obj created, no need check again.
1773 static int mdd_cd_sanity_check(const struct lu_env *env,
1774 struct mdd_object *obj)
1779 if (!obj || mdd_is_dead_obj(obj))
1785 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1786 struct md_object *cobj, const struct md_op_spec *spec,
1789 struct mdd_device *mdd = mdo2mdd(cobj);
1790 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1791 struct mdd_object *son = md2mdd_obj(cobj);
1792 struct thandle *handle;
1793 const struct lu_buf *buf;
1794 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1795 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1799 rc = mdd_cd_sanity_check(env, son);
1803 if (!md_should_create(spec->sp_cr_flags))
1807 * there are following use cases for this function:
1808 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1809 * striping can be specified or not
1812 rc = mdd_la_get(env, son, attr);
1816 /* calling ->ah_make_hint() is used to transfer information from parent */
1817 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1819 handle = mdd_trans_create(env, mdd);
1821 GOTO(out_free, rc = PTR_ERR(handle));
1824 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1825 * Should this be fixed?
1827 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1828 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1829 spec->sp_cr_flags, spec->no_create);
1831 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1832 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1833 spec->u.sp_ea.eadatalen);
1838 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1839 XATTR_NAME_LOV, 0, handle);
1843 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1847 rc = mdd_trans_start(env, mdd, handle);
1851 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1857 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1860 mdd_trans_stop(env, mdd, rc, handle);
1865 static int mdd_declare_object_initialize(const struct lu_env *env,
1866 struct mdd_object *parent,
1867 struct mdd_object *child,
1868 struct lu_attr *attr,
1869 struct thandle *handle)
1875 * inode mode has been set in creation time, and it's based on umask,
1876 * la_mode and acl, don't set here again! (which will go wrong
1877 * because below function doesn't consider umask).
1878 * I'd suggest set all object attributes in creation time, see above.
1880 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1881 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1882 rc = mdo_declare_attr_set(env, child, attr, handle);
1883 attr->la_valid |= LA_MODE | LA_TYPE;
1884 if (rc != 0 || !S_ISDIR(attr->la_mode))
1887 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1892 rc = mdo_declare_ref_add(env, child, handle);
1896 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1902 static int mdd_object_initialize(const struct lu_env *env,
1903 const struct lu_fid *pfid,
1904 struct mdd_object *child,
1905 struct lu_attr *attr, struct thandle *handle,
1906 const struct md_op_spec *spec)
1911 if (S_ISDIR(attr->la_mode)) {
1912 /* Add "." and ".." for newly created dir */
1913 mdo_ref_add(env, child, handle);
1914 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1915 S_IFDIR, dot, handle);
1917 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1920 mdo_ref_del(env, child, handle);
1927 * This function checks whether it can create a file/dir under the
1928 * directory(@pobj). The directory(@pobj) is not being locked by
1931 * \param[in] env execution environment
1932 * \param[in] pobj the directory to create files
1933 * \param[in] pattr the attributes of the directory
1934 * \param[in] lname the name of the created file/dir
1935 * \param[in] cattr the attributes of the file/dir
1936 * \param[in] spec create specification
1938 * \retval = 0 it is allowed to create file/dir under
1940 * \retval negative error not allowed to create file/dir
1941 * under the directory
1943 static int mdd_create_sanity_check(const struct lu_env *env,
1944 struct md_object *pobj,
1945 const struct lu_attr *pattr,
1946 const struct lu_name *lname,
1947 struct lu_attr *cattr,
1948 struct md_op_spec *spec)
1950 struct mdd_thread_info *info = mdd_env_info(env);
1951 struct lu_fid *fid = &info->mti_fid;
1952 struct mdd_object *obj = md2mdd_obj(pobj);
1953 struct mdd_device *m = mdo2mdd(pobj);
1954 bool check_perm = true;
1959 if (mdd_is_dead_obj(obj) ||
1960 pattr->la_flags & LUSTRE_SLAVE_DEAD_FL)
1964 * In some cases this lookup is not needed - we know before if name
1965 * exists or not because MDT performs lookup for it.
1966 * name length check is done in lookup.
1968 if (spec->sp_cr_lookup) {
1970 * Check if the name already exist, though it will be checked in
1971 * _index_insert also, for avoiding rolling back if exists
1974 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1975 MAY_WRITE | MAY_EXEC);
1977 RETURN(rc ? : -EEXIST);
1979 /* Permission is already being checked in mdd_lookup */
1983 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1988 if (pattr->la_mode & S_ISGID) {
1989 cattr->la_gid = pattr->la_gid;
1990 if (S_ISDIR(cattr->la_mode)) {
1991 cattr->la_mode |= S_ISGID;
1992 cattr->la_valid |= LA_MODE;
1996 rc = mdd_name_check(m, lname);
2000 switch (cattr->la_mode & S_IFMT) {
2002 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2004 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
2005 RETURN(-ENAMETOOLONG);
2024 static int mdd_declare_object_create(const struct lu_env *env,
2025 struct mdd_device *mdd,
2026 struct mdd_object *p, struct mdd_object *c,
2027 struct lu_attr *attr,
2028 struct thandle *handle,
2029 const struct md_op_spec *spec,
2030 struct lu_buf *def_acl_buf,
2031 struct lu_buf *acl_buf,
2032 struct dt_allocation_hint *hint)
2036 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2041 #ifdef CONFIG_FS_POSIX_ACL
2042 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2043 /* if dir, then can inherit default ACl */
2044 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2045 XATTR_NAME_ACL_DEFAULT,
2051 if (acl_buf->lb_len > 0) {
2052 rc = mdo_declare_attr_set(env, c, attr, handle);
2056 rc = mdo_declare_xattr_set(env, c, acl_buf,
2057 XATTR_NAME_ACL_ACCESS, 0, handle);
2062 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2066 /* replay case, create LOV EA from client data */
2067 if (spec->no_create ||
2068 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2069 const struct lu_buf *buf;
2071 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2072 spec->u.sp_ea.eadatalen);
2073 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2079 if (S_ISLNK(attr->la_mode)) {
2080 const char *target_name = spec->u.sp_symname;
2081 int sym_len = strlen(target_name);
2082 const struct lu_buf *buf;
2084 buf = mdd_buf_get_const(env, target_name, sym_len);
2085 rc = dt_declare_record_write(env, mdd_object_child(c),
2094 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2095 struct mdd_object *p, struct mdd_object *c,
2096 const struct lu_name *name,
2097 struct lu_attr *attr,
2098 struct thandle *handle,
2099 const struct md_op_spec *spec,
2100 struct linkea_data *ldata,
2101 struct lu_buf *def_acl_buf,
2102 struct lu_buf *acl_buf,
2103 struct dt_allocation_hint *hint)
2107 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2108 def_acl_buf, acl_buf, hint);
2112 if (S_ISDIR(attr->la_mode)) {
2113 rc = mdo_declare_ref_add(env, p, handle);
2118 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2119 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2123 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2125 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2126 name->ln_name, handle);
2130 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2135 la->la_valid = LA_CTIME | LA_MTIME;
2136 rc = mdo_declare_attr_set(env, p, la, handle);
2140 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2148 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2149 struct lu_attr *la, struct lu_buf *def_acl_buf,
2150 struct lu_buf *acl_buf)
2155 if (S_ISLNK(la->la_mode)) {
2156 acl_buf->lb_len = 0;
2157 def_acl_buf->lb_len = 0;
2161 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2162 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2163 XATTR_NAME_ACL_DEFAULT);
2164 mdd_read_unlock(env, pobj);
2166 /* If there are default ACL, fix mode/ACL by default ACL */
2167 def_acl_buf->lb_len = rc;
2168 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2169 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2170 acl_buf->lb_len = rc;
2171 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2174 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2175 /* If there are no default ACL, fix mode by mask */
2176 struct lu_ucred *uc = lu_ucred(env);
2178 /* The create triggered by MDT internal events, such as
2179 * LFSCK reset, will not contain valid "uc". */
2180 if (unlikely(uc != NULL))
2181 la->la_mode &= ~uc->uc_umask;
2183 acl_buf->lb_len = 0;
2184 def_acl_buf->lb_len = 0;
2191 * Create a metadata object and initialize it, set acl, xattr.
2193 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2194 struct mdd_object *son, struct lu_attr *attr,
2195 struct md_op_spec *spec, struct lu_buf *acl_buf,
2196 struct lu_buf *def_acl_buf,
2197 struct dt_allocation_hint *hint,
2198 struct thandle *handle)
2202 mdd_write_lock(env, son, MOR_TGT_CHILD);
2203 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2208 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2209 * created in declare phase, they also needs to be added to master
2210 * object as sub-directory entry. So it has to initialize the master
2211 * object, then set dir striped EA.(in mdo_xattr_set) */
2212 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2215 GOTO(err_destroy, rc);
2218 * in case of replay we just set LOVEA provided by the client
2219 * XXX: I think it would be interesting to try "old" way where
2220 * MDT calls this xattr_set(LOV) in a different transaction.
2221 * probably this way we code can be made better.
2224 /* During creation, there are only a few cases we need do xattr_set to
2226 * 1. regular file: see comments above.
2227 * 2. dir: inherit default striping or pool settings from parent.
2228 * 3. create striped directory with provided stripeEA.
2229 * 4. create striped directory because inherit default layout from the
2232 if (spec->no_create ||
2233 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2234 S_ISDIR(attr->la_mode)) {
2235 const struct lu_buf *buf;
2237 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2238 spec->u.sp_ea.eadatalen);
2239 rc = mdo_xattr_set(env, son, buf,
2240 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2244 GOTO(err_destroy, rc);
2247 #ifdef CONFIG_FS_POSIX_ACL
2248 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2249 S_ISDIR(attr->la_mode)) {
2250 /* set default acl */
2251 rc = mdo_xattr_set(env, son, def_acl_buf,
2252 XATTR_NAME_ACL_DEFAULT, 0,
2255 GOTO(err_destroy, rc);
2257 /* set its own acl */
2258 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2259 rc = mdo_xattr_set(env, son, acl_buf,
2260 XATTR_NAME_ACL_ACCESS,
2263 GOTO(err_destroy, rc);
2267 if (S_ISLNK(attr->la_mode)) {
2268 struct lu_ucred *uc = lu_ucred_assert(env);
2269 struct dt_object *dt = mdd_object_child(son);
2270 const char *target_name = spec->u.sp_symname;
2271 int sym_len = strlen(target_name);
2272 const struct lu_buf *buf;
2275 buf = mdd_buf_get_const(env, target_name, sym_len);
2276 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2278 CFS_CAP_SYS_RESOURCE_MASK);
2283 GOTO(err_initlized, rc = -EFAULT);
2287 if (unlikely(rc != 0)) {
2289 if (S_ISDIR(attr->la_mode)) {
2290 /* Drop the reference, no need to delete "."/"..",
2291 * because the object to be destroied directly. */
2292 rc2 = mdo_ref_del(env, son, handle);
2296 rc2 = mdo_ref_del(env, son, handle);
2300 mdo_destroy(env, son, handle);
2303 mdd_write_unlock(env, son);
2307 static int mdd_index_delete(const struct lu_env *env,
2308 struct mdd_object *mdd_pobj,
2309 struct lu_attr *cattr,
2310 const struct lu_name *lname)
2312 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2313 struct thandle *handle;
2317 handle = mdd_trans_create(env, mdd);
2319 RETURN(PTR_ERR(handle));
2321 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2326 if (S_ISDIR(cattr->la_mode)) {
2327 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2332 /* Since this will only be used in the error handler path,
2333 * Let's set the thandle to be local and not mess the transno */
2334 handle->th_local = 1;
2335 rc = mdd_trans_start(env, mdd, handle);
2339 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2340 S_ISDIR(cattr->la_mode), handle);
2344 mdd_trans_stop(env, mdd, rc, handle);
2349 * Create object and insert it into namespace.
2351 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2352 const struct lu_name *lname, struct md_object *child,
2353 struct md_op_spec *spec, struct md_attr* ma)
2355 struct mdd_thread_info *info = mdd_env_info(env);
2356 struct lu_attr *la = &info->mti_la_for_fix;
2357 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2358 struct mdd_object *son = md2mdd_obj(child);
2359 struct mdd_device *mdd = mdo2mdd(pobj);
2360 struct lu_attr *attr = &ma->ma_attr;
2361 struct thandle *handle;
2362 struct lu_attr *pattr = &info->mti_pattr;
2363 struct lu_buf acl_buf;
2364 struct lu_buf def_acl_buf;
2365 struct linkea_data *ldata = &info->mti_link_data;
2366 const char *name = lname->ln_name;
2367 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2373 * Two operations have to be performed:
2375 * - an allocation of a new object (->do_create()), and
2377 * - an insertion into a parent index (->dio_insert()).
2379 * Due to locking, operation order is not important, when both are
2380 * successful, *but* error handling cases are quite different:
2382 * - if insertion is done first, and following object creation fails,
2383 * insertion has to be rolled back, but this operation might fail
2384 * also leaving us with dangling index entry.
2386 * - if creation is done first, is has to be undone if insertion
2387 * fails, leaving us with leaked space, which is neither good, nor
2390 * It seems that creation-first is simplest solution, but it is
2391 * sub-optimal in the frequent
2396 * case, because second mkdir is bound to create object, only to
2397 * destroy it immediately.
2399 * To avoid this follow local file systems that do double lookup:
2401 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2403 * 1. create (mdd_object_create_internal())
2405 * 2. insert (__mdd_index_insert(), lookup again)
2408 rc = mdd_la_get(env, mdd_pobj, pattr);
2412 /* Sanity checks before big job. */
2413 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2417 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2418 GOTO(out_free, rc = -EINPROGRESS);
2420 handle = mdd_trans_create(env, mdd);
2422 GOTO(out_free, rc = PTR_ERR(handle));
2424 acl_buf.lb_buf = info->mti_xattr_buf;
2425 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2426 def_acl_buf.lb_buf = info->mti_key;
2427 def_acl_buf.lb_len = sizeof(info->mti_key);
2428 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2432 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2434 memset(ldata, 0, sizeof(*ldata));
2435 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2436 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2439 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2440 &tfid, lname, 1, 0, ldata);
2442 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2443 mdd_object_fid(mdd_pobj),
2444 lname, 1, 0, ldata);
2447 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2448 handle, spec, ldata, &def_acl_buf, &acl_buf,
2453 rc = mdd_trans_start(env, mdd, handle);
2457 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2458 &def_acl_buf, hint, handle);
2462 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2463 mdd_write_lock(env, son, MOR_TGT_CHILD);
2464 rc = __mdd_orphan_add(env, son, handle);
2465 GOTO(out_volatile, rc);
2467 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2468 attr->la_mode, name, handle);
2470 GOTO(err_created, rc);
2472 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2475 /* update parent directory mtime/ctime */
2477 la->la_valid = LA_CTIME | LA_MTIME;
2478 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2480 GOTO(err_insert, rc);
2488 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2489 rc2 = __mdd_orphan_del(env, son, handle);
2491 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2492 S_ISDIR(attr->la_mode),
2498 mdd_write_lock(env, son, MOR_TGT_CHILD);
2499 if (S_ISDIR(attr->la_mode)) {
2500 /* Drop the reference, no need to delete "."/"..",
2501 * because the object is to be destroyed directly. */
2502 rc2 = mdo_ref_del(env, son, handle);
2504 mdd_write_unlock(env, son);
2509 /* For volatile files drop one link immediately, since there is
2510 * no filename in the namespace, and save any error returned. */
2511 rc2 = mdo_ref_del(env, son, handle);
2513 mdd_write_unlock(env, son);
2514 if (unlikely(rc == 0))
2519 /* Don't destroy the volatile object on success */
2520 if (likely(rc != 0))
2521 mdo_destroy(env, son, handle);
2522 mdd_write_unlock(env, son);
2525 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2526 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2527 rc = mdd_changelog_ns_store(env, mdd,
2528 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2529 S_ISREG(attr->la_mode) ? CL_CREATE :
2530 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2531 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2534 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2536 /* If creation fails, it is most likely due to the remote update
2537 * failure, because local transaction will mostly succeed at
2538 * this stage. There is no easy way to rollback all of previous
2539 * updates, so let's remove the object from namespace, and
2540 * LFSCK should handle the orphan object. */
2541 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2542 mdd_index_delete(env, mdd_pobj, attr, lname);
2546 if (is_vmalloc_addr(ldata->ld_buf))
2547 /* if we vmalloced a large buffer drop it */
2548 lu_buf_free(ldata->ld_buf);
2550 /* The child object shouldn't be cached anymore */
2552 set_bit(LU_OBJECT_HEARD_BANSHEE,
2553 &child->mo_lu.lo_header->loh_flags);
2558 * Get locks on parents in proper order
2559 * RETURN: < 0 - error, rename_order if successful
2567 static int mdd_rename_order(const struct lu_env *env,
2568 struct mdd_device *mdd,
2569 struct mdd_object *src_pobj,
2570 const struct lu_attr *pattr,
2571 struct mdd_object *tgt_pobj)
2573 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2577 if (src_pobj == tgt_pobj)
2578 RETURN(MDD_RN_SAME);
2580 /* compared the parent child relationship of src_p&tgt_p */
2581 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2583 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2586 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2600 /* has not mdd_write{read}_lock on any obj yet. */
2601 static int mdd_rename_sanity_check(const struct lu_env *env,
2602 struct mdd_object *src_pobj,
2603 const struct lu_attr *pattr,
2604 struct mdd_object *tgt_pobj,
2605 const struct lu_attr *tpattr,
2606 struct mdd_object *sobj,
2607 const struct lu_attr *cattr,
2608 struct mdd_object *tobj,
2609 const struct lu_attr *tattr)
2614 /* XXX: when get here, sobj must NOT be NULL,
2615 * the other case has been processed in cld_rename
2616 * before mdd_rename and enable MDS_PERM_BYPASS. */
2619 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2623 /* XXX: when get here, "tobj == NULL" means tobj must
2624 * NOT exist (neither on remote MDS, such case has been
2625 * processed in cld_rename before mdd_rename and enable
2627 * So check may_create, but not check may_unlink. */
2629 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2630 (src_pobj != tgt_pobj));
2632 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2633 (src_pobj != tgt_pobj), 1);
2635 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2636 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2641 static int mdd_declare_rename(const struct lu_env *env,
2642 struct mdd_device *mdd,
2643 struct mdd_object *mdd_spobj,
2644 struct mdd_object *mdd_tpobj,
2645 struct mdd_object *mdd_sobj,
2646 struct mdd_object *mdd_tobj,
2647 const struct lu_name *tname,
2648 const struct lu_name *sname,
2650 struct linkea_data *ldata,
2651 struct thandle *handle)
2653 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2656 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2657 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2663 /* name from source dir */
2664 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2668 /* .. from source child */
2669 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2670 /* source child can be directory,
2671 * counted by source dir's nlink */
2672 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2675 if (mdd_spobj != mdd_tpobj) {
2676 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2681 rc = mdo_declare_index_insert(env, mdd_sobj,
2683 S_IFDIR, dotdot, handle);
2688 /* new target child can be directory,
2689 * counted by target dir's nlink */
2690 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2695 la->la_valid = LA_CTIME | LA_MTIME;
2696 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2700 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2704 la->la_valid = LA_CTIME;
2705 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2709 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2710 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2715 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2716 mdd_object_type(mdd_sobj),
2717 tname->ln_name, handle);
2721 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2722 /* delete target child in target parent directory */
2723 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2728 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2732 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2733 /* target child can be directory,
2734 * delete "." reference in target child directory */
2735 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2739 /* delete ".." reference in target parent directory */
2740 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2745 la->la_valid = LA_CTIME;
2746 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2750 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2755 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2762 /* src object can be remote that is why we use only fid and type of object */
2763 static int mdd_rename(const struct lu_env *env,
2764 struct md_object *src_pobj, struct md_object *tgt_pobj,
2765 const struct lu_fid *lf, const struct lu_name *lsname,
2766 struct md_object *tobj, const struct lu_name *ltname,
2769 const char *sname = lsname->ln_name;
2770 const char *tname = ltname->ln_name;
2771 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2772 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2773 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2774 struct mdd_device *mdd = mdo2mdd(src_pobj);
2775 struct mdd_object *mdd_sobj = NULL; /* source object */
2776 struct mdd_object *mdd_tobj = NULL;
2777 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2778 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2779 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2780 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2781 struct thandle *handle;
2782 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2783 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2784 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2787 bool tobj_locked = 0;
2788 unsigned cl_flags = 0;
2793 mdd_tobj = md2mdd_obj(tobj);
2795 mdd_sobj = mdd_object_find(env, mdd, lf);
2796 if (IS_ERR(mdd_sobj))
2797 RETURN(PTR_ERR(mdd_sobj));
2799 rc = mdd_la_get(env, mdd_sobj, cattr);
2801 GOTO(out_pending, rc);
2803 rc = mdd_la_get(env, mdd_spobj, pattr);
2805 GOTO(out_pending, rc);
2808 rc = mdd_la_get(env, mdd_tobj, tattr);
2810 GOTO(out_pending, rc);
2813 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2815 GOTO(out_pending, rc);
2817 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2818 mdd_sobj, cattr, mdd_tobj, tattr);
2820 GOTO(out_pending, rc);
2822 rc = mdd_name_check(mdd, ltname);
2824 GOTO(out_pending, rc);
2826 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2827 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2829 GOTO(out_pending, rc);
2831 handle = mdd_trans_create(env, mdd);
2833 GOTO(out_pending, rc = PTR_ERR(handle));
2835 memset(ldata, 0, sizeof(*ldata));
2836 mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj), lsname,
2837 mdd_object_fid(mdd_tpobj), ltname, 1, 0, ldata);
2838 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2839 mdd_tobj, lsname, ltname, ma, ldata, handle);
2843 rc = mdd_trans_start(env, mdd, handle);
2847 is_dir = S_ISDIR(cattr->la_mode);
2849 /* Remove source name from source directory */
2850 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2854 /* "mv dir1 dir2" needs "dir1/.." link update */
2855 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2856 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2858 GOTO(fixup_spobj2, rc);
2860 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2863 GOTO(fixup_spobj, rc);
2866 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2867 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2869 /* tname might been renamed to something else */
2870 GOTO(fixup_spobj, rc);
2873 /* Insert new fid with target name into target dir */
2874 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2877 GOTO(fixup_tpobj, rc);
2879 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2880 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2882 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2883 la->la_valid = LA_CTIME;
2884 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2886 GOTO(fixup_tpobj, rc);
2888 /* Update the linkEA for the source object */
2889 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2890 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2891 mdo2fid(mdd_tpobj), ltname, handle, ldata,
2894 /* Old files might not have EA entry */
2895 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2896 lsname, handle, NULL, 0);
2897 mdd_write_unlock(env, mdd_sobj);
2898 /* We don't fail the transaction if the link ea can't be
2899 updated -- fid2path will use alternate lookup method. */
2902 /* Remove old target object
2903 * For tobj is remote case cmm layer has processed
2904 * and set tobj to NULL then. So when tobj is NOT NULL,
2905 * it must be local one.
2907 if (tobj && mdd_object_exists(mdd_tobj)) {
2908 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2910 if (mdd_is_dead_obj(mdd_tobj)) {
2911 /* shld not be dead, something is wrong */
2912 CERROR("tobj is dead, something is wrong\n");
2916 mdo_ref_del(env, mdd_tobj, handle);
2918 /* Remove dot reference. */
2919 if (S_ISDIR(tattr->la_mode))
2920 mdo_ref_del(env, mdd_tobj, handle);
2923 /* fetch updated nlink */
2924 rc = mdd_la_get(env, mdd_tobj, tattr);
2926 CERROR("%s: Failed to get nlink for tobj "
2928 mdd2obd_dev(mdd)->obd_name,
2929 PFID(tpobj_fid), rc);
2930 GOTO(fixup_tpobj, rc);
2933 la->la_valid = LA_CTIME;
2934 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2936 CERROR("%s: Failed to set ctime for tobj "
2938 mdd2obd_dev(mdd)->obd_name,
2939 PFID(tpobj_fid), rc);
2940 GOTO(fixup_tpobj, rc);
2943 /* XXX: this transfer to ma will be removed with LOD/OSP */
2944 ma->ma_attr = *tattr;
2945 ma->ma_valid |= MA_INODE;
2946 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2949 CERROR("%s: Failed to unlink tobj "
2951 mdd2obd_dev(mdd)->obd_name,
2952 PFID(tpobj_fid), rc);
2953 GOTO(fixup_tpobj, rc);
2956 /* fetch updated nlink */
2957 rc = mdd_la_get(env, mdd_tobj, tattr);
2959 CERROR("%s: Failed to get nlink for tobj "
2961 mdd2obd_dev(mdd)->obd_name,
2962 PFID(tpobj_fid), rc);
2963 GOTO(fixup_tpobj, rc);
2965 /* XXX: this transfer to ma will be removed with LOD/OSP */
2966 ma->ma_attr = *tattr;
2967 ma->ma_valid |= MA_INODE;
2969 if (tattr->la_nlink == 0) {
2970 cl_flags |= CLF_RENAME_LAST;
2971 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2972 cl_flags |= CLF_RENAME_LAST_EXISTS;
2976 la->la_valid = LA_CTIME | LA_MTIME;
2977 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2979 GOTO(fixup_tpobj, rc);
2981 if (mdd_spobj != mdd_tpobj) {
2982 la->la_valid = LA_CTIME | LA_MTIME;
2983 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2985 GOTO(fixup_tpobj, rc);
2992 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2994 CWARN("tp obj fix error %d\n",rc2);
2996 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2997 !mdd_is_dead_obj(mdd_tobj)) {
2999 mdo_ref_add(env, mdd_tobj, handle);
3001 mdo_ref_add(env, mdd_tobj, handle);
3004 rc2 = __mdd_index_insert(env, mdd_tpobj,
3006 mdd_object_type(mdd_tobj),
3009 CWARN("tp obj fix error: rc = %d\n", rc2);
3014 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3015 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3017 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3018 mdd2obd_dev(mdd)->obd_name, rc2);
3021 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3024 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3025 mdd2obd_dev(mdd)->obd_name, rc2);
3030 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3031 mdd_object_type(mdd_sobj), sname,
3034 CWARN("sp obj fix error: rc = %d\n", rc2);
3039 mdd_write_unlock(env, mdd_tobj);
3042 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3043 mdd_tobj, tpobj_fid, lf, spobj_fid,
3044 ltname, lsname, handle);
3047 mdd_trans_stop(env, mdd, rc, handle);
3050 mdd_object_put(env, mdd_sobj);
3055 * During migration once the parent FID has been changed,
3056 * we need update the parent FID in linkea.
3058 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3059 struct mdd_object *parent,
3060 struct mdd_object *child,
3061 const char *name, int namelen,
3062 struct thandle *handle,
3065 struct mdd_thread_info *info = mdd_env_info(env);
3066 struct linkea_data ldata = { NULL };
3067 struct lu_buf *buf = &info->mti_link_buf;
3073 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3074 if (buf->lb_buf == NULL)
3078 rc = mdd_links_read(env, child, &ldata);
3080 if (rc == -ENOENT || rc == -ENODATA)
3085 LASSERT(ldata.ld_leh != NULL);
3086 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3087 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3088 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3089 struct lu_name lname;
3092 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3095 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3096 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3097 ldata.ld_lee = (struct link_ea_entry *)
3098 ((char *)ldata.ld_lee +
3103 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3104 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3105 lname.ln_namelen, lname.ln_name,
3106 PFID(mdd_object_fid(parent)));
3107 /* update to the new parent fid */
3108 linkea_entry_pack(ldata.ld_lee, &lname,
3109 mdd_object_fid(parent));
3111 rc = mdd_declare_links_add(env, child, handle, &ldata,
3114 rc = mdd_links_write(env, child, &ldata, handle);
3120 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3121 struct mdd_object *parent,
3122 struct mdd_object *child,
3123 const char *name, int namelen,
3124 struct thandle *handle)
3126 return mdd_linkea_update_child_internal(env, parent, child, name,
3127 namelen, handle, true);
3130 static int mdd_linkea_update_child(const struct lu_env *env,
3131 struct mdd_object *parent,
3132 struct mdd_object *child,
3133 const char *name, int namelen,
3134 struct thandle *handle)
3136 return mdd_linkea_update_child_internal(env, parent, child, name,
3137 namelen, handle, false);
3140 static int mdd_update_linkea_internal(const struct lu_env *env,
3141 struct mdd_object *mdd_pobj,
3142 struct mdd_object *mdd_sobj,
3143 struct mdd_object *mdd_tobj,
3144 const struct lu_name *child_name,
3145 struct linkea_data *ldata,
3146 struct thandle *handle,
3149 struct mdd_thread_info *info = mdd_env_info(env);
3154 LASSERT(ldata->ld_buf != NULL);
3157 /* If it is mulitple links file, we need update the name entry for
3159 LASSERT(ldata->ld_leh != NULL);
3160 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3161 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3162 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3163 struct mdd_object *pobj;
3164 struct lu_name lname;
3167 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3169 pobj = mdd_object_find(env, mdd, &fid);
3171 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3172 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3174 linkea_del_buf(ldata, &lname);
3178 if (!mdd_object_exists(pobj)) {
3179 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3180 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3181 linkea_del_buf(ldata, &lname);
3182 mdd_object_put(env, pobj);
3186 if (pobj == mdd_pobj &&
3187 lname.ln_namelen == child_name->ln_namelen &&
3188 strncmp(lname.ln_name, child_name->ln_name,
3189 lname.ln_namelen) == 0) {
3190 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3191 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3193 linkea_del_buf(ldata, &lname);
3194 mdd_object_put(env, pobj);
3198 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3199 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3200 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3203 /* Remove source name from source directory */
3204 /* Insert new fid with target name into target dir */
3205 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3210 rc = mdo_declare_index_insert(env, pobj,
3211 mdd_object_fid(mdd_tobj),
3212 mdd_object_type(mdd_tobj),
3213 lname.ln_name, handle);
3217 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3221 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3225 char *tmp_name = info->mti_key;
3227 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3228 /* lnamelen is too big(> NAME_MAX + 16),
3229 * something wrong about this linkea, let's
3231 linkea_del_buf(ldata, &lname);
3232 mdd_object_put(env, pobj);
3236 /* Note: lname might be without \0 at the end, see
3237 * linkea_entry_unpack(), let's add extra \0 by
3239 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3240 lname.ln_namelen, lname.ln_name);
3241 lname.ln_name = tmp_name;
3243 /* Let's check if this linkEA still valid, before
3244 * it might be packed into the RPC buffer. */
3245 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3246 &info->mti_fid, NULL);
3248 !lu_fid_eq(&info->mti_fid,
3249 mdd_object_fid(mdd_sobj))) {
3250 /* skip invalid linkea entry */
3251 linkea_del_buf(ldata, &lname);
3252 mdd_object_put(env, pobj);
3256 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3260 rc = __mdd_index_insert(env, pobj,
3261 mdd_object_fid(mdd_tobj),
3262 mdd_object_type(mdd_tobj),
3267 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3268 rc = mdo_ref_add(env, mdd_tobj, handle);
3269 mdd_write_unlock(env, mdd_tobj);
3273 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3274 mdo_ref_del(env, mdd_sobj, handle);
3275 mdd_write_unlock(env, mdd_sobj);
3278 mdd_object_put(env, pobj);
3282 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3289 static int mdd_migrate_xattrs(const struct lu_env *env,
3290 struct mdd_object *mdd_sobj,
3291 struct mdd_object *mdd_tobj)
3293 struct mdd_thread_info *info = mdd_env_info(env);
3294 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3296 struct thandle *handle;
3302 struct lu_buf list_xbuf;
3306 /* retrieve xattr list from the old object */
3307 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3308 if (list_xsize == -ENODATA)
3314 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3315 if (info->mti_big_buf.lb_buf == NULL)
3318 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3319 list_xbuf.lb_len = list_xsize;
3320 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3325 xname = list_xbuf.lb_buf;
3327 xlen = strnlen(xname, rem - 1) + 1;
3328 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3329 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3330 strcmp(XATTR_NAME_LMV, xname) == 0)
3333 /* For directory, if there are default layout, migrate here */
3334 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3335 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3338 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3339 if (xsize == -ENODATA)
3344 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3345 if (info->mti_link_buf.lb_buf == NULL)
3346 GOTO(out, rc = -ENOMEM);
3348 xbuf.lb_len = xsize;
3349 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3350 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3356 handle = mdd_trans_create(env, mdd);
3358 GOTO(out, rc = PTR_ERR(handle));
3360 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3363 GOTO(stop_trans, rc);
3364 /* Note: this transaction is part of migration, and it is not
3365 * the last step of migration, so we set th_local = 1 to avoid
3366 * update last rcvd for this transaction */
3367 handle->th_local = 1;
3368 rc = mdd_trans_start(env, mdd, handle);
3370 GOTO(stop_trans, rc);
3372 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3374 GOTO(stop_trans, rc = 0);
3377 GOTO(stop_trans, rc);
3379 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3386 memmove(xname, xname + xlen, rem);
3392 static int mdd_declare_migrate_create(const struct lu_env *env,
3393 struct mdd_object *mdd_pobj,
3394 struct mdd_object *mdd_sobj,
3395 struct mdd_object *mdd_tobj,
3396 struct md_op_spec *spec,
3398 union lmv_mds_md *mgr_ea,
3399 struct linkea_data *ldata,
3400 struct thandle *handle)
3402 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3403 const struct lu_buf *buf;
3407 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3408 handle, spec, NULL);
3412 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3417 if (S_ISLNK(la->la_mode)) {
3418 const char *target_name = spec->u.sp_symname;
3419 int sym_len = strlen(target_name);
3420 const struct lu_buf *buf;
3422 buf = mdd_buf_get_const(env, target_name, sym_len);
3423 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3427 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3428 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3434 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3435 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3436 spec->u.sp_ea.eadatalen);
3437 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3443 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3444 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3445 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3450 la_flag->la_valid = LA_FLAGS;
3451 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3452 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3457 static int mdd_migrate_create(const struct lu_env *env,
3458 struct mdd_object *mdd_pobj,
3459 struct mdd_object *mdd_sobj,
3460 struct mdd_object *mdd_tobj,
3461 const struct lu_name *lname,
3464 struct mdd_thread_info *info = mdd_env_info(env);
3465 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3466 struct md_op_spec *spec = &info->mti_spec;
3467 struct lu_buf lmm_buf = { NULL };
3468 struct lu_buf link_buf = { NULL };
3469 const struct lu_buf *buf;
3470 struct thandle *handle;
3471 struct lmv_mds_md_v1 *mgr_ea;
3472 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3473 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3475 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3479 /* prepare spec for create */
3480 memset(spec, 0, sizeof(*spec));
3481 spec->sp_cr_lookup = 0;
3482 spec->sp_feat = &dt_directory_features;
3483 if (S_ISLNK(la->la_mode)) {
3484 buf = lu_buf_check_and_alloc(
3485 &mdd_env_info(env)->mti_big_buf,
3488 link_buf.lb_len = la->la_size + 1;
3489 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3490 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3492 rc = rc != 0 ? rc : -EFAULT;
3493 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3494 mdd2obd_dev(mdd)->obd_name,
3495 PFID(mdd_object_fid(mdd_sobj)), rc);
3498 spec->u.sp_symname = link_buf.lb_buf;
3499 } else if (S_ISREG(la->la_mode)) {
3500 /* retrieve lov of the old object */
3501 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3502 if (rc != 0 && rc != -ENODATA)
3504 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3505 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3506 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3507 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3509 } else if (S_ISDIR(la->la_mode)) {
3510 rc = mdd_links_read(env, mdd_sobj, ldata);
3511 if (rc == -ENODATA) {
3512 /* ignore the non-linkEA error */
3520 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3521 memset(mgr_ea, 0, sizeof(*mgr_ea));
3522 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3523 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3524 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3525 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3526 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3527 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3529 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3531 handle = mdd_trans_create(env, mdd);
3533 GOTO(out_free, rc = PTR_ERR(handle));
3535 /* Note: this transaction is part of migration, and it is not
3536 * the last step of migration, so we set th_local = 1 to avoid
3537 * update last rcvd for this transaction */
3538 handle->th_local = 1;
3539 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3541 (union lmv_mds_md *)info->mti_xattr_buf,
3544 GOTO(stop_trans, rc);
3546 rc = mdd_trans_start(env, mdd, handle);
3548 GOTO(stop_trans, rc);
3550 /* don't set nlink from the original object */
3551 la->la_valid &= ~LA_NLINK;
3553 /* create the target object */
3554 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3557 GOTO(stop_trans, rc);
3559 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3560 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3562 GOTO(stop_trans, rc);
3565 /* Set MIGRATE EA on the source inode, so once the migration needs
3566 * to be re-done during failover, the re-do process can locate the
3567 * target object which is already being created. */
3568 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3569 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3570 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3572 GOTO(stop_trans, rc);
3574 /* Set immutable flag, so any modification is disabled until
3575 * the migration is done. Once the migration is interrupted,
3576 * if the resume process find the migrating object has both
3577 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3578 * flag and approve the migration */
3579 la_flag->la_valid = LA_FLAGS;
3580 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3581 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3583 if (handle != NULL) {
3586 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3591 if (lmm_buf.lb_buf != NULL)
3592 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3596 static int mdd_migrate_entries(const struct lu_env *env,
3597 struct mdd_object *mdd_sobj,
3598 struct mdd_object *mdd_tobj)
3600 struct dt_object *next = mdd_object_child(mdd_sobj);
3601 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3602 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3603 struct thandle *handle;
3605 const struct dt_it_ops *iops;
3608 struct lu_dirent *ent;
3611 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3615 if (!dt_try_as_dir(env, next))
3616 GOTO(out_ent, rc = -ENOTDIR);
3618 * iterate directories
3620 iops = &next->do_index_ops->dio_it;
3621 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3623 GOTO(out_ent, rc = PTR_ERR(it));
3625 rc = iops->load(env, it, 0);
3627 rc = iops->next(env, it);
3631 * At this point and across for-loop:
3633 * rc == 0 -> ok, proceed.
3634 * rc > 0 -> end of directory.
3638 struct mdd_object *child;
3639 char *name = mdd_env_info(env)->mti_key;
3643 bool target_exist = false;
3646 len = iops->key_size(env, it);
3650 result = iops->rec(env, it, (struct dt_rec *)ent,
3651 LUDA_FID | LUDA_TYPE);
3652 if (result == -ESTALE)
3659 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3660 recsize = le16_to_cpu(ent->lde_reclen);
3662 /* Insert new fid with target name into target dir */
3663 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3664 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3665 ent->lde_name[1] == '.'))
3668 child = mdd_object_find(env, mdd, &ent->lde_fid);
3670 GOTO(out, rc = PTR_ERR(child));
3672 mdd_write_lock(env, child, MOR_SRC_CHILD);
3673 is_dir = S_ISDIR(mdd_object_type(child));
3675 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3677 /* Check whether the name has been inserted to the target */
3678 if (dt_try_as_dir(env, dt_tobj)) {
3679 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3681 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3682 (struct dt_key *)name);
3683 if (unlikely(rc == 0))
3684 target_exist = true;
3687 handle = mdd_trans_create(env, mdd);
3689 GOTO(out_put, rc = PTR_ERR(handle));
3691 /* Note: this transaction is part of migration, and it is not
3692 * the last step of migration, so we set th_local = 1 to avoid
3693 * updating last rcvd for this transaction */
3694 handle->th_local = 1;
3695 if (likely(!target_exist)) {
3696 rc = mdo_declare_index_insert(env, mdd_tobj,
3698 mdd_object_type(child),
3704 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3710 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3715 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3719 /* Update .. for child */
3720 rc = mdo_declare_index_delete(env, child, dotdot,
3725 rc = mdo_declare_index_insert(env, child,
3726 mdd_object_fid(mdd_tobj),
3727 S_IFDIR, dotdot, handle);
3732 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3739 rc = mdd_trans_start(env, mdd, handle);
3741 CERROR("%s: transaction start failed: rc = %d\n",
3742 mdd2obd_dev(mdd)->obd_name, rc);
3746 if (likely(!target_exist)) {
3747 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3748 mdd_object_type(child),
3754 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3759 rc = __mdd_index_delete_only(env, child, dotdot,
3764 rc = __mdd_index_insert_only(env, child,
3765 mdd_object_fid(mdd_tobj), S_IFDIR,
3771 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3772 strlen(name), handle);
3775 mdd_write_unlock(env, child);
3776 mdd_object_put(env, child);
3777 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3784 result = iops->next(env, it);
3785 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3786 GOTO(out, rc = -EINTR);
3788 if (result == -ESTALE)
3790 } while (result == 0);
3793 iops->fini(env, it);
3795 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3799 static int mdd_declare_update_linkea(const struct lu_env *env,
3800 struct mdd_object *mdd_pobj,
3801 struct mdd_object *mdd_sobj,
3802 struct mdd_object *mdd_tobj,
3803 const struct lu_name *child_name,
3804 struct linkea_data *ldata,
3805 struct thandle *handle)
3807 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3808 child_name, ldata, handle, 1);
3811 static int mdd_update_linkea(const struct lu_env *env,
3812 struct mdd_object *mdd_pobj,
3813 struct mdd_object *mdd_sobj,
3814 struct mdd_object *mdd_tobj,
3815 const struct lu_name *child_name,
3816 struct linkea_data *ldata,
3817 struct thandle *handle)
3819 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3820 child_name, ldata, handle, 0);
3823 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3824 struct mdd_object *mdd_pobj,
3825 struct mdd_object *mdd_sobj,
3826 struct mdd_object *mdd_tobj,
3827 const struct lu_name *lname,
3829 struct lu_attr *parent_la,
3830 struct linkea_data *ldata,
3831 struct thandle *handle)
3833 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3834 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3837 /* Revert IMMUTABLE flag */
3838 la_flag->la_valid = LA_FLAGS;
3839 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3840 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3844 /* delete entry from source dir */
3845 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3849 if (ldata->ld_buf != NULL) {
3850 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3851 mdd_tobj, lname, ldata, handle);
3856 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3857 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3862 handle->th_complex = 1;
3863 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3865 LU_XATTR_REPLACE, handle);
3870 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3871 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3877 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3878 mdd_object_type(mdd_tobj),
3879 lname->ln_name, handle);
3883 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3887 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3888 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3893 /* delete old object */
3894 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3898 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3899 /* delete old object */
3900 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3903 /* set nlink to 0 */
3904 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3909 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3913 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3917 rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3922 static int mdd_migrate_update_name(const struct lu_env *env,
3923 struct mdd_object *mdd_pobj,
3924 struct mdd_object *mdd_sobj,
3925 struct mdd_object *mdd_tobj,
3926 const struct lu_name *lname,
3929 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3930 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3931 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3932 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3933 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3934 struct thandle *handle;
3935 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3936 const char *name = lname->ln_name;
3940 /* update time for parent */
3941 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3942 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3943 p_la->la_valid = LA_CTIME;
3945 rc = mdd_la_get(env, mdd_sobj, so_attr);
3949 ldata->ld_buf = NULL;
3950 rc = mdd_links_read(env, mdd_sobj, ldata);
3951 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3954 handle = mdd_trans_create(env, mdd);
3956 RETURN(PTR_ERR(handle));
3958 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3959 lname, so_attr, p_la, ldata,
3962 /* If the migration can not be fit in one transaction, just
3963 * leave it in the original MDT */
3965 GOTO(stop_trans, rc = 0);
3967 GOTO(stop_trans, rc);
3970 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3971 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3972 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3973 PFID(mdd_object_fid(mdd_tobj)));
3975 rc = mdd_trans_start(env, mdd, handle);
3977 GOTO(stop_trans, rc);
3979 /* Revert IMMUTABLE flag */
3980 la_flag->la_valid = LA_FLAGS;
3981 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3982 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3984 GOTO(stop_trans, rc);
3986 /* Remove source name from source directory */
3987 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3989 GOTO(stop_trans, rc);
3991 if (ldata->ld_buf != NULL) {
3992 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3993 lname, ldata, handle);
3995 GOTO(stop_trans, rc);
3997 /* linkea update might decrease the source object
3998 * nlink, let's get the attr again after ref_del */
3999 rc = mdd_la_get(env, mdd_sobj, so_attr);
4001 GOTO(stop_trans, rc);
4004 if (S_ISREG(so_attr->la_mode)) {
4005 if (so_attr->la_nlink == 1) {
4006 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4008 if (rc != 0 && rc != -ENODATA)
4009 GOTO(stop_trans, rc);
4011 rc = mdo_xattr_set(env, mdd_tobj, NULL,
4013 LU_XATTR_REPLACE, handle);
4015 GOTO(stop_trans, rc);
4019 /* Insert new fid with target name into target dir */
4020 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4021 mdd_object_type(mdd_tobj), name, handle);
4023 GOTO(stop_trans, rc);
4025 linkea_add_buf(ldata, lname, mdd_object_fid(mdd_pobj));
4026 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
4029 GOTO(stop_trans, rc);
4031 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4033 /* Increase mod_count to add the source object to the orphan list,
4034 * so if other clients still send RPC to the old object, then these
4035 * objects can help the request to find the new object, see
4036 * mdt_reint_open() */
4037 mdd_sobj->mod_count++;
4038 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
4039 mdd_sobj->mod_count--;
4041 GOTO(out_unlock, rc);
4043 mdo_ref_del(env, mdd_sobj, handle);
4045 mdo_ref_del(env, mdd_sobj, handle);
4047 /* Get the attr again after ref_del */
4048 rc = mdd_la_get(env, mdd_sobj, so_attr);
4050 GOTO(out_unlock, rc);
4052 ma->ma_attr = *so_attr;
4053 ma->ma_valid |= MA_INODE;
4055 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4057 GOTO(out_unlock, rc);
4059 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4060 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4061 mdo2fid(mdd_pobj), lname, lname, handle);
4063 CWARN("%s: changelog for migrate %s "DFID
4064 "under "DFID" failed: rc = %d\n",
4065 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4066 PFID(mdd_object_fid(mdd_sobj)),
4067 PFID(mdd_object_fid(mdd_pobj)), rc);
4068 /* Sigh, there are no easy way to migrate back the object, so
4069 * let's reset the result to 0 for now XXX */
4073 mdd_write_unlock(env, mdd_sobj);
4076 mdd_trans_stop(env, mdd, rc, handle);
4081 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4082 const struct lu_fid *fid, __u32 *mdt_index)
4084 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4085 struct seq_server_site *ss;
4088 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4090 range->lsr_flags = LU_SEQ_RANGE_MDT;
4091 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4095 *mdt_index = range->lsr_index;
4100 * Check whether we should migrate the file/dir
4102 * < 0 permission check failed or other error.
4103 * = 0 the file can be migrated.
4104 * > 0 the file does not need to be migrated, mostly
4105 * for multiple link file
4107 static int mdd_migrate_sanity_check(const struct lu_env *env,
4108 struct mdd_object *pobj,
4109 const struct lu_attr *pattr,
4110 struct mdd_object *sobj,
4111 struct lu_attr *sattr)
4113 struct mdd_thread_info *info = mdd_env_info(env);
4114 struct linkea_data *ldata = &info->mti_link_data;
4115 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4117 struct lu_buf *mgr_buf;
4123 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4124 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4125 if (mgr_buf->lb_buf == NULL)
4128 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4130 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4132 /* If the object has migrateEA, it means IMMUTE flag
4133 * is being set by previous migration process, so it
4134 * needs to override the IMMUTE flag, otherwise the
4135 * following sanity check will fail */
4136 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4137 LMV_HASH_FLAG_MIGRATION) {
4138 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4140 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4141 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4142 mdd2obd_dev(mdd)->obd_name,
4143 PFID(mdd_object_fid(sobj)));
4147 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4148 sobj, sattr, NULL, NULL);
4152 /* Then it will check if the file should be migrated. If the file
4153 * has mulitple links, we only need migrate the file if all of its
4154 * entries has been migrated to the remote MDT */
4155 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4158 rc = mdd_links_read(env, sobj, ldata);
4160 /* For multiple links files, if there are no linkEA data at all,
4161 * means the file might be created before linkEA is enabled, and
4162 * all of its links should not be migrated yet, otherwise it
4163 * should have some linkEA there */
4164 if (rc == -ENOENT || rc == -ENODATA)
4169 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4170 /* If there are still links locally, then the file will not be
4172 LASSERT(ldata->ld_leh != NULL);
4173 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4174 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4175 struct lu_name lname;
4177 __u32 parent_mdt_index;
4179 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4181 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4184 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4188 /* Migrate the object only if none of its parents are on the
4190 if (parent_mdt_index != mdt_index)
4193 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4194 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4195 lname.ln_name, PFID(&fid));
4203 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4204 struct md_object *sobj, const struct lu_name *lname,
4205 struct md_object *tobj, struct md_attr *ma)
4207 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4208 struct mdd_device *mdd = mdo2mdd(pobj);
4209 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4210 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4211 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4212 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4216 /* If the file will being migrated, it will check whether
4217 * the file is being opened by someone else right now */
4218 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4219 if (mdd_sobj->mod_count > 0) {
4220 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4221 mdd2obd_dev(mdd)->obd_name,
4222 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4223 mdd_sobj->mod_count, -EBUSY);
4224 mdd_read_unlock(env, mdd_sobj);
4225 GOTO(put, rc = -EBUSY);
4227 mdd_read_unlock(env, mdd_sobj);
4229 rc = mdd_la_get(env, mdd_sobj, so_attr);
4233 rc = mdd_la_get(env, mdd_pobj, pattr);
4237 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4244 /* Sigh, it is impossible to finish all of migration in a single
4245 * transaction, for example migrating big directory entries to the
4246 * new MDT, it needs insert all of name entries of children in the
4249 * So migration will be done in multiple steps and transactions.
4251 * 1. create an orphan object on the remote MDT in one transaction.
4252 * 2. migrate extend attributes to the new target file/directory.
4253 * 3. For directory, migrate the entries to the new MDT and update
4254 * linkEA of each children. Because we can not migrate all entries
4255 * in a single transaction, so the migrating directory will become
4256 * a striped directory during migration, so once the process is
4257 * interrupted, the directory is still accessible. (During lookup,
4258 * client will locate the name by searching both original and target
4260 * 4. Finally, update the name/FID to point to the new file/directory
4261 * in a separate transaction.
4264 /* step 1: Check whether the orphan object has been created, and create
4265 * orphan object on the remote MDT if needed */
4266 if (!mdd_object_exists(mdd_tobj)) {
4267 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4273 LASSERT(mdd_object_exists(mdd_tobj));
4274 /* step 2: migrate xattr */
4275 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4279 /* step 3: migrate name entries to the orphan object */
4280 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4281 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4284 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4285 OBD_FAIL_MDS_REINT_NET_REP)))
4288 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4291 LASSERT(mdd_object_exists(mdd_tobj));
4292 /* step 4: update name entry to the new object */
4293 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4301 const struct md_dir_operations mdd_dir_ops = {
4302 .mdo_is_subdir = mdd_is_subdir,
4303 .mdo_lookup = mdd_lookup,
4304 .mdo_create = mdd_create,
4305 .mdo_rename = mdd_rename,
4306 .mdo_link = mdd_link,
4307 .mdo_unlink = mdd_unlink,
4308 .mdo_create_data = mdd_create_data,
4309 .mdo_migrate = mdd_migrate,