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 = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1167 ldata->ld_leh->leh_len);
1170 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1173 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1174 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1175 mdd_object_remote(mdd_obj) == 0) {
1176 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1177 struct thandle *sub_th;
1179 /* XXX: If the linkEA is overflow, then we need to notify the
1180 * namespace LFSCK to skip "nlink" attribute verification
1181 * on this object to avoid the "nlink" to be shrinked by
1182 * wrong. It may be not good an interaction with LFSCK
1183 * like this. We will consider to replace it with other
1184 * mechanism in future. LU-5802. */
1185 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1186 LFSCK_TYPE_NAMESPACE);
1188 sub_th = thandle_get_sub_by_dt(env, handle,
1189 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1190 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1197 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1198 struct thandle *handle, struct linkea_data *ldata,
1199 enum mdd_links_add_overflow overflow)
1205 if (ldata != NULL && ldata->ld_leh != NULL) {
1206 ea_len = ldata->ld_leh->leh_len;
1207 linkea = ldata->ld_buf->lb_buf;
1209 ea_len = DEFAULT_LINKEA_SIZE;
1213 /* XXX: max size? */
1214 rc = mdo_declare_xattr_set(env, mdd_obj,
1215 mdd_buf_get_const(env, linkea, ea_len),
1216 XATTR_NAME_LINK, 0, handle);
1220 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1221 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1222 struct thandle *sub_th;
1224 /* XXX: If the linkEA is overflow, then we need to notify the
1225 * namespace LFSCK to skip "nlink" attribute verification
1226 * on this object to avoid the "nlink" to be shrinked by
1227 * wrong. It may be not good an interaction with LFSCK
1228 * like this. We will consider to replace it with other
1229 * mechanism in future. LU-5802. */
1230 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1231 LFSCK_TYPE_NAMESPACE);
1233 sub_th = thandle_get_sub_by_dt(env, handle,
1234 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1235 rc = lfsck_in_notify(env,
1236 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1243 static inline int mdd_declare_links_del(const struct lu_env *env,
1244 struct mdd_object *c,
1245 struct thandle *handle)
1249 /* For directory, the linkEA will be removed together
1250 * with the object. */
1251 if (!S_ISDIR(mdd_object_type(c)))
1252 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1257 static int mdd_declare_link(const struct lu_env *env,
1258 struct mdd_device *mdd,
1259 struct mdd_object *p,
1260 struct mdd_object *c,
1261 const struct lu_name *name,
1262 struct thandle *handle,
1264 struct linkea_data *data)
1268 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1269 name->ln_name, handle);
1273 rc = mdo_declare_ref_add(env, c, handle);
1277 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1278 rc = mdo_declare_ref_add(env, c, handle);
1283 la->la_valid = LA_CTIME | LA_MTIME;
1284 rc = mdo_declare_attr_set(env, p, la, handle);
1288 la->la_valid = LA_CTIME;
1289 rc = mdo_declare_attr_set(env, c, la, handle);
1293 rc = mdd_declare_links_add(env, c, handle, data,
1294 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1298 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1303 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1304 struct md_object *src_obj, const struct lu_name *lname,
1307 const char *name = lname->ln_name;
1308 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1309 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1310 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1311 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1312 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1313 struct mdd_device *mdd = mdo2mdd(src_obj);
1314 struct thandle *handle;
1315 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1319 rc = mdd_la_get(env, mdd_sobj, cattr);
1323 rc = mdd_la_get(env, mdd_tobj, tattr);
1327 handle = mdd_trans_create(env, mdd);
1329 GOTO(out_pending, rc = PTR_ERR(handle));
1331 memset(ldata, 0, sizeof(*ldata));
1333 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1334 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1336 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1341 rc = mdd_trans_start(env, mdd, handle);
1345 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1346 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1349 GOTO(out_unlock, rc);
1351 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1352 rc = mdo_ref_add(env, mdd_sobj, handle);
1354 GOTO(out_unlock, rc);
1357 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1358 rc = mdo_ref_add(env, mdd_sobj, handle);
1360 GOTO(out_unlock, rc);
1363 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1364 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1367 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1368 mdd_object_type(mdd_sobj),
1371 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1372 mdd_object_type(mdd_sobj),
1377 mdo_ref_del(env, mdd_sobj, handle);
1378 GOTO(out_unlock, rc);
1381 la->la_valid = LA_CTIME | LA_MTIME;
1382 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1384 GOTO(out_unlock, rc);
1386 la->la_valid = LA_CTIME;
1387 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1389 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1390 mdo2fid(mdd_tobj), lname, 0, 0,
1393 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1394 lname, handle, ldata, 0);
1395 /* The failure of links_add should not cause the link
1396 * failure, reset rc here */
1401 mdd_write_unlock(env, mdd_sobj);
1403 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1404 mdo2fid(mdd_tobj), NULL, NULL,
1405 lname, NULL, handle);
1407 mdd_trans_stop(env, mdd, rc, handle);
1409 if (is_vmalloc_addr(ldata->ld_buf))
1410 /* if we vmalloced a large buffer drop it */
1411 lu_buf_free(ldata->ld_buf);
1416 static int mdd_mark_dead_object(const struct lu_env *env,
1417 struct mdd_object *obj, struct thandle *handle,
1420 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1424 obj->mod_flags |= DEAD_OBJ;
1426 if (!S_ISDIR(mdd_object_type(obj)))
1429 attr->la_valid = LA_FLAGS;
1430 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1433 rc = mdo_declare_attr_set(env, obj, attr, handle);
1435 rc = mdo_attr_set(env, obj, attr, handle);
1440 static int mdd_declare_finish_unlink(const struct lu_env *env,
1441 struct mdd_object *obj,
1442 struct thandle *handle)
1446 rc = mdd_mark_dead_object(env, obj, handle, true);
1450 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1454 rc = mdo_declare_destroy(env, obj, handle);
1458 return mdd_declare_links_del(env, obj, handle);
1461 /* caller should take a lock before calling */
1462 int mdd_finish_unlink(const struct lu_env *env,
1463 struct mdd_object *obj, struct md_attr *ma,
1464 const struct mdd_object *pobj,
1465 const struct lu_name *lname,
1469 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1472 LASSERT(mdd_write_locked(env, obj) != 0);
1474 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1475 rc = mdd_mark_dead_object(env, obj, th, false);
1479 /* add new orphan and the object
1480 * will be deleted during mdd_close() */
1481 if (obj->mod_count) {
1482 rc = __mdd_orphan_add(env, obj, th);
1484 CDEBUG(D_HA, "Object "DFID" is inserted into "
1485 "orphan list, open count = %d\n",
1486 PFID(mdd_object_fid(obj)),
1489 CERROR("Object "DFID" fail to be an orphan, "
1490 "open count = %d, maybe cause failed "
1492 PFID(mdd_object_fid(obj)),
1495 rc = mdo_destroy(env, obj, th);
1497 } else if (!is_dir) {
1498 /* old files may not have link ea; ignore errors */
1499 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1507 * has mdd_write_lock on cobj already, but not on pobj yet
1509 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1510 const struct lu_attr *pattr,
1511 struct mdd_object *cobj,
1512 const struct lu_attr *cattr)
1517 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1522 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1523 struct mdd_object *p, struct mdd_object *c,
1524 const struct lu_name *name, struct md_attr *ma,
1525 struct thandle *handle, int no_name, int is_dir)
1527 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1530 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1531 if (likely(no_name == 0)) {
1532 rc = mdo_declare_index_delete(env, p, name->ln_name,
1539 rc = mdo_declare_ref_del(env, p, handle);
1545 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1546 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1547 la->la_valid = LA_CTIME | LA_MTIME;
1548 rc = mdo_declare_attr_set(env, p, la, handle);
1553 rc = mdo_declare_ref_del(env, c, handle);
1557 rc = mdo_declare_ref_del(env, c, handle);
1561 la->la_valid = LA_CTIME;
1562 rc = mdo_declare_attr_set(env, c, la, handle);
1566 rc = mdd_declare_finish_unlink(env, c, handle);
1570 /* FIXME: need changelog for remove entry */
1571 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1578 * test if a file has an HSM archive
1579 * if HSM attributes are not found in ma update them from
1582 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1583 struct mdd_object *obj,
1588 if (!(ma->ma_valid & MA_HSM)) {
1589 /* no HSM MD provided, read xattr */
1590 struct lu_buf *hsm_buf;
1591 const size_t buflen = sizeof(struct hsm_attrs);
1594 hsm_buf = mdd_buf_get(env, NULL, 0);
1595 lu_buf_alloc(hsm_buf, buflen);
1596 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1597 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1598 lu_buf_free(hsm_buf);
1602 ma->ma_valid = MA_HSM;
1604 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1610 * Delete name entry and the object.
1611 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1612 * does not exist for this object, and it could only happen during resending
1613 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1614 * is also needed in this case(needed by changelog), so we have to add another
1615 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1616 * the ENOENT failure should be able to be fixed by redo mechanism.
1618 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1619 struct md_object *cobj, const struct lu_name *lname,
1620 struct md_attr *ma, int no_name)
1622 const char *name = lname->ln_name;
1623 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1624 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1625 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1626 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1627 struct mdd_object *mdd_cobj = NULL;
1628 struct mdd_device *mdd = mdo2mdd(pobj);
1629 struct thandle *handle;
1633 /* cobj == NULL means only delete name entry */
1634 if (likely(cobj != NULL)) {
1635 mdd_cobj = md2mdd_obj(cobj);
1636 if (mdd_object_exists(mdd_cobj) == 0)
1640 rc = mdd_la_get(env, mdd_pobj, pattr);
1644 if (likely(mdd_cobj != NULL)) {
1646 rc = mdd_la_get(env, mdd_cobj, cattr);
1650 is_dir = S_ISDIR(cattr->la_mode);
1653 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1657 handle = mdd_trans_create(env, mdd);
1659 RETURN(PTR_ERR(handle));
1661 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1662 lname, ma, handle, no_name, is_dir);
1666 rc = mdd_trans_start(env, mdd, handle);
1670 if (likely(mdd_cobj != NULL))
1671 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1673 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1674 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1679 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1680 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1681 GOTO(cleanup, rc = 0);
1683 if (likely(mdd_cobj != NULL)) {
1684 rc = mdo_ref_del(env, mdd_cobj, handle);
1686 __mdd_index_insert_only(env, mdd_pobj,
1688 mdd_object_type(mdd_cobj),
1695 mdo_ref_del(env, mdd_cobj, handle);
1697 /* fetch updated nlink */
1698 rc = mdd_la_get(env, mdd_cobj, cattr);
1703 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1704 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1706 la->la_valid = LA_CTIME | LA_MTIME;
1707 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1711 /* Enough for only unlink the entry */
1712 if (unlikely(mdd_cobj == NULL))
1715 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1716 /* update ctime of an unlinked file only if it is still
1717 * opened or a link still exists */
1718 la->la_valid = LA_CTIME;
1719 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1724 /* XXX: this transfer to ma will be removed with LOD/OSP */
1725 ma->ma_attr = *cattr;
1726 ma->ma_valid |= MA_INODE;
1727 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1729 /* fetch updated nlink */
1731 rc = mdd_la_get(env, mdd_cobj, cattr);
1733 /* if object is removed then we can't get its attrs, use last get */
1734 if (cattr->la_nlink == 0) {
1735 ma->ma_attr = *cattr;
1736 ma->ma_valid |= MA_INODE;
1740 if (likely(mdd_cobj != NULL))
1741 mdd_write_unlock(env, mdd_cobj);
1746 if (cattr->la_nlink == 0) {
1747 cl_flags |= CLF_UNLINK_LAST;
1748 /* search for an existing archive */
1749 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1750 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1753 rc = mdd_changelog_ns_store(env, mdd,
1754 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1755 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1760 mdd_trans_stop(env, mdd, rc, handle);
1766 * The permission has been checked when obj created, no need check again.
1768 static int mdd_cd_sanity_check(const struct lu_env *env,
1769 struct mdd_object *obj)
1774 if (!obj || mdd_is_dead_obj(obj))
1780 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1781 struct md_object *cobj, const struct md_op_spec *spec,
1784 struct mdd_device *mdd = mdo2mdd(cobj);
1785 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1786 struct mdd_object *son = md2mdd_obj(cobj);
1787 struct thandle *handle;
1788 const struct lu_buf *buf;
1789 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1790 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1794 rc = mdd_cd_sanity_check(env, son);
1798 if (!md_should_create(spec->sp_cr_flags))
1802 * there are following use cases for this function:
1803 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1804 * striping can be specified or not
1807 rc = mdd_la_get(env, son, attr);
1811 /* calling ->ah_make_hint() is used to transfer information from parent */
1812 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1814 handle = mdd_trans_create(env, mdd);
1816 GOTO(out_free, rc = PTR_ERR(handle));
1819 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1820 * Should this be fixed?
1822 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1823 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1824 spec->sp_cr_flags, spec->no_create);
1826 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1827 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1828 spec->u.sp_ea.eadatalen);
1833 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1834 XATTR_NAME_LOV, 0, handle);
1838 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1842 rc = mdd_trans_start(env, mdd, handle);
1846 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1852 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1855 mdd_trans_stop(env, mdd, rc, handle);
1860 static int mdd_declare_object_initialize(const struct lu_env *env,
1861 struct mdd_object *parent,
1862 struct mdd_object *child,
1863 struct lu_attr *attr,
1864 struct thandle *handle)
1870 * inode mode has been set in creation time, and it's based on umask,
1871 * la_mode and acl, don't set here again! (which will go wrong
1872 * because below function doesn't consider umask).
1873 * I'd suggest set all object attributes in creation time, see above.
1875 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1876 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1877 rc = mdo_declare_attr_set(env, child, attr, handle);
1878 attr->la_valid |= LA_MODE | LA_TYPE;
1879 if (rc != 0 || !S_ISDIR(attr->la_mode))
1882 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1887 rc = mdo_declare_ref_add(env, child, handle);
1891 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1897 static int mdd_object_initialize(const struct lu_env *env,
1898 const struct lu_fid *pfid,
1899 struct mdd_object *child,
1900 struct lu_attr *attr, struct thandle *handle,
1901 const struct md_op_spec *spec)
1906 if (S_ISDIR(attr->la_mode)) {
1907 /* Add "." and ".." for newly created dir */
1908 mdo_ref_add(env, child, handle);
1909 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1910 S_IFDIR, dot, handle);
1912 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1915 mdo_ref_del(env, child, handle);
1922 * This function checks whether it can create a file/dir under the
1923 * directory(@pobj). The directory(@pobj) is not being locked by
1926 * \param[in] env execution environment
1927 * \param[in] pobj the directory to create files
1928 * \param[in] pattr the attributes of the directory
1929 * \param[in] lname the name of the created file/dir
1930 * \param[in] cattr the attributes of the file/dir
1931 * \param[in] spec create specification
1933 * \retval = 0 it is allowed to create file/dir under
1935 * \retval negative error not allowed to create file/dir
1936 * under the directory
1938 static int mdd_create_sanity_check(const struct lu_env *env,
1939 struct md_object *pobj,
1940 const struct lu_attr *pattr,
1941 const struct lu_name *lname,
1942 struct lu_attr *cattr,
1943 struct md_op_spec *spec)
1945 struct mdd_thread_info *info = mdd_env_info(env);
1946 struct lu_fid *fid = &info->mti_fid;
1947 struct mdd_object *obj = md2mdd_obj(pobj);
1948 struct mdd_device *m = mdo2mdd(pobj);
1949 bool check_perm = true;
1954 if (mdd_is_dead_obj(obj))
1958 * In some cases this lookup is not needed - we know before if name
1959 * exists or not because MDT performs lookup for it.
1960 * name length check is done in lookup.
1962 if (spec->sp_cr_lookup) {
1964 * Check if the name already exist, though it will be checked in
1965 * _index_insert also, for avoiding rolling back if exists
1968 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1969 MAY_WRITE | MAY_EXEC);
1971 RETURN(rc ? : -EEXIST);
1973 /* Permission is already being checked in mdd_lookup */
1977 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1982 if (pattr->la_mode & S_ISGID) {
1983 cattr->la_gid = pattr->la_gid;
1984 if (S_ISDIR(cattr->la_mode)) {
1985 cattr->la_mode |= S_ISGID;
1986 cattr->la_valid |= LA_MODE;
1990 rc = mdd_name_check(m, lname);
1994 switch (cattr->la_mode & S_IFMT) {
1996 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1998 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1999 RETURN(-ENAMETOOLONG);
2018 static int mdd_declare_object_create(const struct lu_env *env,
2019 struct mdd_device *mdd,
2020 struct mdd_object *p, struct mdd_object *c,
2021 struct lu_attr *attr,
2022 struct thandle *handle,
2023 const struct md_op_spec *spec,
2024 struct lu_buf *def_acl_buf,
2025 struct lu_buf *acl_buf,
2026 struct dt_allocation_hint *hint)
2030 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2035 #ifdef CONFIG_FS_POSIX_ACL
2036 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2037 /* if dir, then can inherit default ACl */
2038 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2039 XATTR_NAME_ACL_DEFAULT,
2045 if (acl_buf->lb_len > 0) {
2046 rc = mdo_declare_attr_set(env, c, attr, handle);
2050 rc = mdo_declare_xattr_set(env, c, acl_buf,
2051 XATTR_NAME_ACL_ACCESS, 0, handle);
2056 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2060 /* replay case, create LOV EA from client data */
2061 if (spec->no_create ||
2062 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2063 const struct lu_buf *buf;
2065 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2066 spec->u.sp_ea.eadatalen);
2067 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2073 if (S_ISLNK(attr->la_mode)) {
2074 const char *target_name = spec->u.sp_symname;
2075 int sym_len = strlen(target_name);
2076 const struct lu_buf *buf;
2078 buf = mdd_buf_get_const(env, target_name, sym_len);
2079 rc = dt_declare_record_write(env, mdd_object_child(c),
2088 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2089 struct mdd_object *p, struct mdd_object *c,
2090 const struct lu_name *name,
2091 struct lu_attr *attr,
2092 struct thandle *handle,
2093 const struct md_op_spec *spec,
2094 struct linkea_data *ldata,
2095 struct lu_buf *def_acl_buf,
2096 struct lu_buf *acl_buf,
2097 struct dt_allocation_hint *hint)
2101 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2102 def_acl_buf, acl_buf, hint);
2106 if (S_ISDIR(attr->la_mode)) {
2107 rc = mdo_declare_ref_add(env, p, handle);
2112 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2113 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2117 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2119 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2120 name->ln_name, handle);
2124 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2129 la->la_valid = LA_CTIME | LA_MTIME;
2130 rc = mdo_declare_attr_set(env, p, la, handle);
2134 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2142 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2143 struct lu_attr *la, struct lu_buf *def_acl_buf,
2144 struct lu_buf *acl_buf)
2149 if (S_ISLNK(la->la_mode)) {
2150 acl_buf->lb_len = 0;
2151 def_acl_buf->lb_len = 0;
2155 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2156 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2157 XATTR_NAME_ACL_DEFAULT);
2158 mdd_read_unlock(env, pobj);
2160 /* If there are default ACL, fix mode/ACL by default ACL */
2161 def_acl_buf->lb_len = rc;
2162 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2163 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2164 acl_buf->lb_len = rc;
2165 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2168 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2169 /* If there are no default ACL, fix mode by mask */
2170 struct lu_ucred *uc = lu_ucred(env);
2172 /* The create triggered by MDT internal events, such as
2173 * LFSCK reset, will not contain valid "uc". */
2174 if (unlikely(uc != NULL))
2175 la->la_mode &= ~uc->uc_umask;
2177 acl_buf->lb_len = 0;
2178 def_acl_buf->lb_len = 0;
2185 * Create a metadata object and initialize it, set acl, xattr.
2187 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2188 struct mdd_object *son, struct lu_attr *attr,
2189 struct md_op_spec *spec, struct lu_buf *acl_buf,
2190 struct lu_buf *def_acl_buf,
2191 struct dt_allocation_hint *hint,
2192 struct thandle *handle)
2196 mdd_write_lock(env, son, MOR_TGT_CHILD);
2197 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2202 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2203 * created in declare phase, they also needs to be added to master
2204 * object as sub-directory entry. So it has to initialize the master
2205 * object, then set dir striped EA.(in mdo_xattr_set) */
2206 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2209 GOTO(err_destroy, rc);
2212 * in case of replay we just set LOVEA provided by the client
2213 * XXX: I think it would be interesting to try "old" way where
2214 * MDT calls this xattr_set(LOV) in a different transaction.
2215 * probably this way we code can be made better.
2218 /* During creation, there are only a few cases we need do xattr_set to
2220 * 1. regular file: see comments above.
2221 * 2. dir: inherit default striping or pool settings from parent.
2222 * 3. create striped directory with provided stripeEA.
2223 * 4. create striped directory because inherit default layout from the
2226 if (spec->no_create ||
2227 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2228 S_ISDIR(attr->la_mode)) {
2229 const struct lu_buf *buf;
2231 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2232 spec->u.sp_ea.eadatalen);
2233 rc = mdo_xattr_set(env, son, buf,
2234 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2238 GOTO(err_destroy, rc);
2241 #ifdef CONFIG_FS_POSIX_ACL
2242 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2243 S_ISDIR(attr->la_mode)) {
2244 /* set default acl */
2245 rc = mdo_xattr_set(env, son, def_acl_buf,
2246 XATTR_NAME_ACL_DEFAULT, 0,
2249 GOTO(err_destroy, rc);
2251 /* set its own acl */
2252 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2253 rc = mdo_xattr_set(env, son, acl_buf,
2254 XATTR_NAME_ACL_ACCESS,
2257 GOTO(err_destroy, rc);
2261 if (S_ISLNK(attr->la_mode)) {
2262 struct lu_ucred *uc = lu_ucred_assert(env);
2263 struct dt_object *dt = mdd_object_child(son);
2264 const char *target_name = spec->u.sp_symname;
2265 int sym_len = strlen(target_name);
2266 const struct lu_buf *buf;
2269 buf = mdd_buf_get_const(env, target_name, sym_len);
2270 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2272 CFS_CAP_SYS_RESOURCE_MASK);
2277 GOTO(err_initlized, rc = -EFAULT);
2281 if (unlikely(rc != 0)) {
2283 if (S_ISDIR(attr->la_mode)) {
2284 /* Drop the reference, no need to delete "."/"..",
2285 * because the object to be destroied directly. */
2286 rc2 = mdo_ref_del(env, son, handle);
2290 rc2 = mdo_ref_del(env, son, handle);
2294 mdo_destroy(env, son, handle);
2297 mdd_write_unlock(env, son);
2301 static int mdd_index_delete(const struct lu_env *env,
2302 struct mdd_object *mdd_pobj,
2303 struct lu_attr *cattr,
2304 const struct lu_name *lname)
2306 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2307 struct thandle *handle;
2311 handle = mdd_trans_create(env, mdd);
2313 RETURN(PTR_ERR(handle));
2315 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2320 if (S_ISDIR(cattr->la_mode)) {
2321 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2326 /* Since this will only be used in the error handler path,
2327 * Let's set the thandle to be local and not mess the transno */
2328 handle->th_local = 1;
2329 rc = mdd_trans_start(env, mdd, handle);
2333 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2334 S_ISDIR(cattr->la_mode), handle);
2338 mdd_trans_stop(env, mdd, rc, handle);
2343 * Create object and insert it into namespace.
2345 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2346 const struct lu_name *lname, struct md_object *child,
2347 struct md_op_spec *spec, struct md_attr* ma)
2349 struct mdd_thread_info *info = mdd_env_info(env);
2350 struct lu_attr *la = &info->mti_la_for_fix;
2351 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2352 struct mdd_object *son = md2mdd_obj(child);
2353 struct mdd_device *mdd = mdo2mdd(pobj);
2354 struct lu_attr *attr = &ma->ma_attr;
2355 struct thandle *handle;
2356 struct lu_attr *pattr = &info->mti_pattr;
2357 struct lu_buf acl_buf;
2358 struct lu_buf def_acl_buf;
2359 struct linkea_data *ldata = &info->mti_link_data;
2360 const char *name = lname->ln_name;
2361 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2367 * Two operations have to be performed:
2369 * - an allocation of a new object (->do_create()), and
2371 * - an insertion into a parent index (->dio_insert()).
2373 * Due to locking, operation order is not important, when both are
2374 * successful, *but* error handling cases are quite different:
2376 * - if insertion is done first, and following object creation fails,
2377 * insertion has to be rolled back, but this operation might fail
2378 * also leaving us with dangling index entry.
2380 * - if creation is done first, is has to be undone if insertion
2381 * fails, leaving us with leaked space, which is neither good, nor
2384 * It seems that creation-first is simplest solution, but it is
2385 * sub-optimal in the frequent
2390 * case, because second mkdir is bound to create object, only to
2391 * destroy it immediately.
2393 * To avoid this follow local file systems that do double lookup:
2395 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2397 * 1. create (mdd_object_create_internal())
2399 * 2. insert (__mdd_index_insert(), lookup again)
2402 rc = mdd_la_get(env, mdd_pobj, pattr);
2406 /* Sanity checks before big job. */
2407 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2411 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2412 GOTO(out_free, rc = -EINPROGRESS);
2414 handle = mdd_trans_create(env, mdd);
2416 GOTO(out_free, rc = PTR_ERR(handle));
2418 acl_buf.lb_buf = info->mti_xattr_buf;
2419 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2420 def_acl_buf.lb_buf = info->mti_key;
2421 def_acl_buf.lb_len = sizeof(info->mti_key);
2422 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2426 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2428 memset(ldata, 0, sizeof(*ldata));
2429 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2430 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2433 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2434 &tfid, lname, 1, 0, ldata);
2436 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2437 mdd_object_fid(mdd_pobj),
2438 lname, 1, 0, ldata);
2441 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2442 handle, spec, ldata, &def_acl_buf, &acl_buf,
2447 rc = mdd_trans_start(env, mdd, handle);
2451 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2452 &def_acl_buf, hint, handle);
2456 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2457 mdd_write_lock(env, son, MOR_TGT_CHILD);
2458 rc = __mdd_orphan_add(env, son, handle);
2459 GOTO(out_volatile, rc);
2461 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2462 attr->la_mode, name, handle);
2464 GOTO(err_created, rc);
2466 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2469 /* update parent directory mtime/ctime */
2471 la->la_valid = LA_CTIME | LA_MTIME;
2472 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2474 GOTO(err_insert, rc);
2482 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2483 rc2 = __mdd_orphan_del(env, son, handle);
2485 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2486 S_ISDIR(attr->la_mode),
2492 mdd_write_lock(env, son, MOR_TGT_CHILD);
2493 if (S_ISDIR(attr->la_mode)) {
2494 /* Drop the reference, no need to delete "."/"..",
2495 * because the object is to be destroyed directly. */
2496 rc2 = mdo_ref_del(env, son, handle);
2498 mdd_write_unlock(env, son);
2503 /* For volatile files drop one link immediately, since there is
2504 * no filename in the namespace, and save any error returned. */
2505 rc2 = mdo_ref_del(env, son, handle);
2507 mdd_write_unlock(env, son);
2508 if (unlikely(rc == 0))
2513 /* Don't destroy the volatile object on success */
2514 if (likely(rc != 0))
2515 mdo_destroy(env, son, handle);
2516 mdd_write_unlock(env, son);
2519 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2520 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2521 rc = mdd_changelog_ns_store(env, mdd,
2522 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2523 S_ISREG(attr->la_mode) ? CL_CREATE :
2524 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2525 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2528 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2530 /* If creation fails, it is most likely due to the remote update
2531 * failure, because local transaction will mostly succeed at
2532 * this stage. There is no easy way to rollback all of previous
2533 * updates, so let's remove the object from namespace, and
2534 * LFSCK should handle the orphan object. */
2535 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2536 mdd_index_delete(env, mdd_pobj, attr, lname);
2540 if (is_vmalloc_addr(ldata->ld_buf))
2541 /* if we vmalloced a large buffer drop it */
2542 lu_buf_free(ldata->ld_buf);
2544 /* The child object shouldn't be cached anymore */
2546 set_bit(LU_OBJECT_HEARD_BANSHEE,
2547 &child->mo_lu.lo_header->loh_flags);
2552 * Get locks on parents in proper order
2553 * RETURN: < 0 - error, rename_order if successful
2561 static int mdd_rename_order(const struct lu_env *env,
2562 struct mdd_device *mdd,
2563 struct mdd_object *src_pobj,
2564 const struct lu_attr *pattr,
2565 struct mdd_object *tgt_pobj)
2567 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2571 if (src_pobj == tgt_pobj)
2572 RETURN(MDD_RN_SAME);
2574 /* compared the parent child relationship of src_p&tgt_p */
2575 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2577 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2580 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2594 /* has not mdd_write{read}_lock on any obj yet. */
2595 static int mdd_rename_sanity_check(const struct lu_env *env,
2596 struct mdd_object *src_pobj,
2597 const struct lu_attr *pattr,
2598 struct mdd_object *tgt_pobj,
2599 const struct lu_attr *tpattr,
2600 struct mdd_object *sobj,
2601 const struct lu_attr *cattr,
2602 struct mdd_object *tobj,
2603 const struct lu_attr *tattr)
2608 /* XXX: when get here, sobj must NOT be NULL,
2609 * the other case has been processed in cld_rename
2610 * before mdd_rename and enable MDS_PERM_BYPASS. */
2613 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2617 /* XXX: when get here, "tobj == NULL" means tobj must
2618 * NOT exist (neither on remote MDS, such case has been
2619 * processed in cld_rename before mdd_rename and enable
2621 * So check may_create, but not check may_unlink. */
2623 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2624 (src_pobj != tgt_pobj));
2626 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2627 (src_pobj != tgt_pobj), 1);
2629 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2630 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2635 static int mdd_declare_rename(const struct lu_env *env,
2636 struct mdd_device *mdd,
2637 struct mdd_object *mdd_spobj,
2638 struct mdd_object *mdd_tpobj,
2639 struct mdd_object *mdd_sobj,
2640 struct mdd_object *mdd_tobj,
2641 const struct lu_name *tname,
2642 const struct lu_name *sname,
2644 struct linkea_data *ldata,
2645 struct thandle *handle)
2647 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2650 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2651 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2657 /* name from source dir */
2658 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2662 /* .. from source child */
2663 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2664 /* source child can be directory,
2665 * counted by source dir's nlink */
2666 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2669 if (mdd_spobj != mdd_tpobj) {
2670 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2675 rc = mdo_declare_index_insert(env, mdd_sobj,
2677 S_IFDIR, dotdot, handle);
2682 /* new target child can be directory,
2683 * counted by target dir's nlink */
2684 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2689 la->la_valid = LA_CTIME | LA_MTIME;
2690 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2694 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2698 la->la_valid = LA_CTIME;
2699 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2703 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2704 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2709 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2710 mdd_object_type(mdd_sobj),
2711 tname->ln_name, handle);
2715 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2716 /* delete target child in target parent directory */
2717 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2722 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2726 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2727 /* target child can be directory,
2728 * delete "." reference in target child directory */
2729 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2733 /* delete ".." reference in target parent directory */
2734 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2739 la->la_valid = LA_CTIME;
2740 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2744 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2749 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2756 /* src object can be remote that is why we use only fid and type of object */
2757 static int mdd_rename(const struct lu_env *env,
2758 struct md_object *src_pobj, struct md_object *tgt_pobj,
2759 const struct lu_fid *lf, const struct lu_name *lsname,
2760 struct md_object *tobj, const struct lu_name *ltname,
2763 const char *sname = lsname->ln_name;
2764 const char *tname = ltname->ln_name;
2765 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2766 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2767 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2768 struct mdd_device *mdd = mdo2mdd(src_pobj);
2769 struct mdd_object *mdd_sobj = NULL; /* source object */
2770 struct mdd_object *mdd_tobj = NULL;
2771 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2772 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2773 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2774 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2775 struct thandle *handle;
2776 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2777 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2778 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2781 bool tobj_locked = 0;
2782 unsigned cl_flags = 0;
2787 mdd_tobj = md2mdd_obj(tobj);
2789 mdd_sobj = mdd_object_find(env, mdd, lf);
2790 if (IS_ERR(mdd_sobj))
2791 RETURN(PTR_ERR(mdd_sobj));
2793 rc = mdd_la_get(env, mdd_sobj, cattr);
2795 GOTO(out_pending, rc);
2797 rc = mdd_la_get(env, mdd_spobj, pattr);
2799 GOTO(out_pending, rc);
2802 rc = mdd_la_get(env, mdd_tobj, tattr);
2804 GOTO(out_pending, rc);
2807 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2809 GOTO(out_pending, rc);
2811 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2812 mdd_sobj, cattr, mdd_tobj, tattr);
2814 GOTO(out_pending, rc);
2816 rc = mdd_name_check(mdd, ltname);
2818 GOTO(out_pending, rc);
2820 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2821 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2823 GOTO(out_pending, rc);
2825 handle = mdd_trans_create(env, mdd);
2827 GOTO(out_pending, rc = PTR_ERR(handle));
2829 memset(ldata, 0, sizeof(*ldata));
2830 mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj), lsname,
2831 mdd_object_fid(mdd_tpobj), ltname, 1, 0, ldata);
2832 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2833 mdd_tobj, lsname, ltname, ma, ldata, handle);
2837 rc = mdd_trans_start(env, mdd, handle);
2841 is_dir = S_ISDIR(cattr->la_mode);
2843 /* Remove source name from source directory */
2844 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2848 /* "mv dir1 dir2" needs "dir1/.." link update */
2849 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2850 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2852 GOTO(fixup_spobj2, rc);
2854 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2857 GOTO(fixup_spobj, rc);
2860 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2861 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2863 /* tname might been renamed to something else */
2864 GOTO(fixup_spobj, rc);
2867 /* Insert new fid with target name into target dir */
2868 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2871 GOTO(fixup_tpobj, rc);
2873 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2874 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2876 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2877 la->la_valid = LA_CTIME;
2878 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2880 GOTO(fixup_tpobj, rc);
2882 /* Update the linkEA for the source object */
2883 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2884 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2885 mdo2fid(mdd_tpobj), ltname, handle, ldata,
2888 /* Old files might not have EA entry */
2889 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2890 lsname, handle, NULL, 0);
2891 mdd_write_unlock(env, mdd_sobj);
2892 /* We don't fail the transaction if the link ea can't be
2893 updated -- fid2path will use alternate lookup method. */
2896 /* Remove old target object
2897 * For tobj is remote case cmm layer has processed
2898 * and set tobj to NULL then. So when tobj is NOT NULL,
2899 * it must be local one.
2901 if (tobj && mdd_object_exists(mdd_tobj)) {
2902 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2904 if (mdd_is_dead_obj(mdd_tobj)) {
2905 /* shld not be dead, something is wrong */
2906 CERROR("tobj is dead, something is wrong\n");
2910 mdo_ref_del(env, mdd_tobj, handle);
2912 /* Remove dot reference. */
2913 if (S_ISDIR(tattr->la_mode))
2914 mdo_ref_del(env, mdd_tobj, handle);
2917 /* fetch updated nlink */
2918 rc = mdd_la_get(env, mdd_tobj, tattr);
2920 CERROR("%s: Failed to get nlink for tobj "
2922 mdd2obd_dev(mdd)->obd_name,
2923 PFID(tpobj_fid), rc);
2924 GOTO(fixup_tpobj, rc);
2927 la->la_valid = LA_CTIME;
2928 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2930 CERROR("%s: Failed to set ctime for tobj "
2932 mdd2obd_dev(mdd)->obd_name,
2933 PFID(tpobj_fid), rc);
2934 GOTO(fixup_tpobj, rc);
2937 /* XXX: this transfer to ma will be removed with LOD/OSP */
2938 ma->ma_attr = *tattr;
2939 ma->ma_valid |= MA_INODE;
2940 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2943 CERROR("%s: Failed to unlink tobj "
2945 mdd2obd_dev(mdd)->obd_name,
2946 PFID(tpobj_fid), rc);
2947 GOTO(fixup_tpobj, rc);
2950 /* fetch updated nlink */
2951 rc = mdd_la_get(env, mdd_tobj, tattr);
2953 CERROR("%s: Failed to get nlink for tobj "
2955 mdd2obd_dev(mdd)->obd_name,
2956 PFID(tpobj_fid), rc);
2957 GOTO(fixup_tpobj, rc);
2959 /* XXX: this transfer to ma will be removed with LOD/OSP */
2960 ma->ma_attr = *tattr;
2961 ma->ma_valid |= MA_INODE;
2963 if (tattr->la_nlink == 0) {
2964 cl_flags |= CLF_RENAME_LAST;
2965 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2966 cl_flags |= CLF_RENAME_LAST_EXISTS;
2970 la->la_valid = LA_CTIME | LA_MTIME;
2971 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2973 GOTO(fixup_tpobj, rc);
2975 if (mdd_spobj != mdd_tpobj) {
2976 la->la_valid = LA_CTIME | LA_MTIME;
2977 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2979 GOTO(fixup_tpobj, rc);
2986 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2988 CWARN("tp obj fix error %d\n",rc2);
2990 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2991 !mdd_is_dead_obj(mdd_tobj)) {
2993 mdo_ref_add(env, mdd_tobj, handle);
2995 mdo_ref_add(env, mdd_tobj, handle);
2998 rc2 = __mdd_index_insert(env, mdd_tpobj,
3000 mdd_object_type(mdd_tobj),
3003 CWARN("tp obj fix error: rc = %d\n", rc2);
3008 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3009 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3011 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3012 mdd2obd_dev(mdd)->obd_name, rc2);
3015 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3018 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3019 mdd2obd_dev(mdd)->obd_name, rc2);
3024 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3025 mdd_object_type(mdd_sobj), sname,
3028 CWARN("sp obj fix error: rc = %d\n", rc2);
3033 mdd_write_unlock(env, mdd_tobj);
3036 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3037 mdd_tobj, tpobj_fid, lf, spobj_fid,
3038 ltname, lsname, handle);
3041 mdd_trans_stop(env, mdd, rc, handle);
3044 mdd_object_put(env, mdd_sobj);
3049 * During migration once the parent FID has been changed,
3050 * we need update the parent FID in linkea.
3052 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3053 struct mdd_object *parent,
3054 struct mdd_object *child,
3055 const char *name, int namelen,
3056 struct thandle *handle,
3059 struct mdd_thread_info *info = mdd_env_info(env);
3060 struct linkea_data ldata = { NULL };
3061 struct lu_buf *buf = &info->mti_link_buf;
3067 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3068 if (buf->lb_buf == NULL)
3072 rc = mdd_links_read(env, child, &ldata);
3074 if (rc == -ENOENT || rc == -ENODATA)
3079 LASSERT(ldata.ld_leh != NULL);
3080 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3081 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3082 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3083 struct lu_name lname;
3086 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3089 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3090 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3091 ldata.ld_lee = (struct link_ea_entry *)
3092 ((char *)ldata.ld_lee +
3097 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3098 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3099 lname.ln_namelen, lname.ln_name,
3100 PFID(mdd_object_fid(parent)));
3101 /* update to the new parent fid */
3102 linkea_entry_pack(ldata.ld_lee, &lname,
3103 mdd_object_fid(parent));
3105 rc = mdd_declare_links_add(env, child, handle, &ldata,
3108 rc = mdd_links_write(env, child, &ldata, handle);
3114 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3115 struct mdd_object *parent,
3116 struct mdd_object *child,
3117 const char *name, int namelen,
3118 struct thandle *handle)
3120 return mdd_linkea_update_child_internal(env, parent, child, name,
3121 namelen, handle, true);
3124 static int mdd_linkea_update_child(const struct lu_env *env,
3125 struct mdd_object *parent,
3126 struct mdd_object *child,
3127 const char *name, int namelen,
3128 struct thandle *handle)
3130 return mdd_linkea_update_child_internal(env, parent, child, name,
3131 namelen, handle, false);
3134 static int mdd_update_linkea_internal(const struct lu_env *env,
3135 struct mdd_object *mdd_pobj,
3136 struct mdd_object *mdd_sobj,
3137 struct mdd_object *mdd_tobj,
3138 const struct lu_name *child_name,
3139 struct linkea_data *ldata,
3140 struct thandle *handle,
3143 struct mdd_thread_info *info = mdd_env_info(env);
3148 LASSERT(ldata->ld_buf != NULL);
3151 /* If it is mulitple links file, we need update the name entry for
3153 LASSERT(ldata->ld_leh != NULL);
3154 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3155 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3156 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3157 struct mdd_object *pobj;
3158 struct lu_name lname;
3161 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3163 pobj = mdd_object_find(env, mdd, &fid);
3165 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3166 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3168 linkea_del_buf(ldata, &lname);
3172 if (!mdd_object_exists(pobj)) {
3173 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3174 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3175 linkea_del_buf(ldata, &lname);
3176 mdd_object_put(env, pobj);
3180 if (pobj == mdd_pobj &&
3181 lname.ln_namelen == child_name->ln_namelen &&
3182 strncmp(lname.ln_name, child_name->ln_name,
3183 lname.ln_namelen) == 0) {
3184 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3185 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3187 linkea_del_buf(ldata, &lname);
3188 mdd_object_put(env, pobj);
3192 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3193 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3194 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3197 /* Remove source name from source directory */
3198 /* Insert new fid with target name into target dir */
3199 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3204 rc = mdo_declare_index_insert(env, pobj,
3205 mdd_object_fid(mdd_tobj),
3206 mdd_object_type(mdd_tobj),
3207 lname.ln_name, handle);
3211 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3215 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3219 char *tmp_name = info->mti_key;
3221 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3222 /* lnamelen is too big(> NAME_MAX + 16),
3223 * something wrong about this linkea, let's
3225 linkea_del_buf(ldata, &lname);
3226 mdd_object_put(env, pobj);
3230 /* Note: lname might be without \0 at the end, see
3231 * linkea_entry_unpack(), let's add extra \0 by
3233 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3234 lname.ln_namelen, lname.ln_name);
3235 lname.ln_name = tmp_name;
3237 /* Let's check if this linkEA still valid, before
3238 * it might be packed into the RPC buffer. */
3239 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3240 &info->mti_fid, NULL);
3242 !lu_fid_eq(&info->mti_fid,
3243 mdd_object_fid(mdd_sobj))) {
3244 /* skip invalid linkea entry */
3245 linkea_del_buf(ldata, &lname);
3246 mdd_object_put(env, pobj);
3250 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3254 rc = __mdd_index_insert(env, pobj,
3255 mdd_object_fid(mdd_tobj),
3256 mdd_object_type(mdd_tobj),
3261 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3262 rc = mdo_ref_add(env, mdd_tobj, handle);
3263 mdd_write_unlock(env, mdd_tobj);
3267 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3268 mdo_ref_del(env, mdd_sobj, handle);
3269 mdd_write_unlock(env, mdd_sobj);
3272 mdd_object_put(env, pobj);
3276 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3283 static int mdd_migrate_xattrs(const struct lu_env *env,
3284 struct mdd_object *mdd_sobj,
3285 struct mdd_object *mdd_tobj)
3287 struct mdd_thread_info *info = mdd_env_info(env);
3288 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3290 struct thandle *handle;
3296 struct lu_buf list_xbuf;
3300 /* retrieve xattr list from the old object */
3301 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3302 if (list_xsize == -ENODATA)
3308 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3309 if (info->mti_big_buf.lb_buf == NULL)
3312 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3313 list_xbuf.lb_len = list_xsize;
3314 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3319 xname = list_xbuf.lb_buf;
3321 xlen = strnlen(xname, rem - 1) + 1;
3322 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3323 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3324 strcmp(XATTR_NAME_LMV, xname) == 0)
3327 /* For directory, if there are default layout, migrate here */
3328 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3329 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3332 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3333 if (xsize == -ENODATA)
3338 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3339 if (info->mti_link_buf.lb_buf == NULL)
3340 GOTO(out, rc = -ENOMEM);
3342 xbuf.lb_len = xsize;
3343 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3344 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3350 handle = mdd_trans_create(env, mdd);
3352 GOTO(out, rc = PTR_ERR(handle));
3354 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3357 GOTO(stop_trans, rc);
3358 /* Note: this transaction is part of migration, and it is not
3359 * the last step of migration, so we set th_local = 1 to avoid
3360 * update last rcvd for this transaction */
3361 handle->th_local = 1;
3362 rc = mdd_trans_start(env, mdd, handle);
3364 GOTO(stop_trans, rc);
3366 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3368 GOTO(stop_trans, rc = 0);
3371 GOTO(stop_trans, rc);
3373 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3380 memmove(xname, xname + xlen, rem);
3386 static int mdd_declare_migrate_create(const struct lu_env *env,
3387 struct mdd_object *mdd_pobj,
3388 struct mdd_object *mdd_sobj,
3389 struct mdd_object *mdd_tobj,
3390 struct md_op_spec *spec,
3392 union lmv_mds_md *mgr_ea,
3393 struct linkea_data *ldata,
3394 struct thandle *handle)
3396 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3397 const struct lu_buf *buf;
3401 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3402 handle, spec, NULL);
3406 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3411 if (S_ISLNK(la->la_mode)) {
3412 const char *target_name = spec->u.sp_symname;
3413 int sym_len = strlen(target_name);
3414 const struct lu_buf *buf;
3416 buf = mdd_buf_get_const(env, target_name, sym_len);
3417 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3421 } else if (S_ISDIR(la->la_mode)) {
3422 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3428 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3429 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3430 spec->u.sp_ea.eadatalen);
3431 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3437 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3438 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3439 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3444 la_flag->la_valid = LA_FLAGS;
3445 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3446 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3451 static int mdd_migrate_create(const struct lu_env *env,
3452 struct mdd_object *mdd_pobj,
3453 struct mdd_object *mdd_sobj,
3454 struct mdd_object *mdd_tobj,
3455 const struct lu_name *lname,
3458 struct mdd_thread_info *info = mdd_env_info(env);
3459 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3460 struct md_op_spec *spec = &info->mti_spec;
3461 struct lu_buf lmm_buf = { NULL };
3462 struct lu_buf link_buf = { NULL };
3463 const struct lu_buf *buf;
3464 struct thandle *handle;
3465 struct lmv_mds_md_v1 *mgr_ea;
3466 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3467 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3469 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3473 /* prepare spec for create */
3474 memset(spec, 0, sizeof(*spec));
3475 spec->sp_cr_lookup = 0;
3476 spec->sp_feat = &dt_directory_features;
3477 if (S_ISLNK(la->la_mode)) {
3478 buf = lu_buf_check_and_alloc(
3479 &mdd_env_info(env)->mti_big_buf,
3482 link_buf.lb_len = la->la_size + 1;
3483 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3484 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3486 rc = rc != 0 ? rc : -EFAULT;
3487 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3488 mdd2obd_dev(mdd)->obd_name,
3489 PFID(mdd_object_fid(mdd_sobj)), rc);
3492 spec->u.sp_symname = link_buf.lb_buf;
3493 } else if (S_ISREG(la->la_mode)) {
3494 /* retrieve lov of the old object */
3495 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3496 if (rc != 0 && rc != -ENODATA)
3498 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3499 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3500 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3501 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3503 } else if (S_ISDIR(la->la_mode)) {
3504 rc = mdd_links_read(env, mdd_sobj, ldata);
3505 if (rc < 0 && rc != -ENODATA)
3509 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3510 memset(mgr_ea, 0, sizeof(*mgr_ea));
3511 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3512 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3513 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3514 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3515 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3516 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3518 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3520 handle = mdd_trans_create(env, mdd);
3522 GOTO(out_free, rc = PTR_ERR(handle));
3524 /* Note: this transaction is part of migration, and it is not
3525 * the last step of migration, so we set th_local = 1 to avoid
3526 * update last rcvd for this transaction */
3527 handle->th_local = 1;
3528 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3530 (union lmv_mds_md *)info->mti_xattr_buf,
3533 GOTO(stop_trans, rc);
3535 rc = mdd_trans_start(env, mdd, handle);
3537 GOTO(stop_trans, rc);
3539 /* create the target object */
3540 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3543 GOTO(stop_trans, rc);
3545 if (S_ISDIR(la->la_mode)) {
3546 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3548 GOTO(stop_trans, rc);
3551 /* Set MIGRATE EA on the source inode, so once the migration needs
3552 * to be re-done during failover, the re-do process can locate the
3553 * target object which is already being created. */
3554 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3555 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3556 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3558 GOTO(stop_trans, rc);
3560 /* Set immutable flag, so any modification is disabled until
3561 * the migration is done. Once the migration is interrupted,
3562 * if the resume process find the migrating object has both
3563 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3564 * flag and approve the migration */
3565 la_flag->la_valid = LA_FLAGS;
3566 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3567 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3569 if (handle != NULL) {
3572 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3577 if (lmm_buf.lb_buf != NULL)
3578 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3582 static int mdd_migrate_entries(const struct lu_env *env,
3583 struct mdd_object *mdd_sobj,
3584 struct mdd_object *mdd_tobj)
3586 struct dt_object *next = mdd_object_child(mdd_sobj);
3587 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3588 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3589 struct thandle *handle;
3591 const struct dt_it_ops *iops;
3594 struct lu_dirent *ent;
3597 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3601 if (!dt_try_as_dir(env, next))
3602 GOTO(out_ent, rc = -ENOTDIR);
3604 * iterate directories
3606 iops = &next->do_index_ops->dio_it;
3607 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3609 GOTO(out_ent, rc = PTR_ERR(it));
3611 rc = iops->load(env, it, 0);
3613 rc = iops->next(env, it);
3617 * At this point and across for-loop:
3619 * rc == 0 -> ok, proceed.
3620 * rc > 0 -> end of directory.
3624 struct mdd_object *child;
3625 char *name = mdd_env_info(env)->mti_key;
3629 bool target_exist = false;
3632 len = iops->key_size(env, it);
3636 result = iops->rec(env, it, (struct dt_rec *)ent,
3637 LUDA_FID | LUDA_TYPE);
3638 if (result == -ESTALE)
3645 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3646 recsize = le16_to_cpu(ent->lde_reclen);
3648 /* Insert new fid with target name into target dir */
3649 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3650 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3651 ent->lde_name[1] == '.'))
3654 child = mdd_object_find(env, mdd, &ent->lde_fid);
3656 GOTO(out, rc = PTR_ERR(child));
3658 mdd_write_lock(env, child, MOR_SRC_CHILD);
3659 is_dir = S_ISDIR(mdd_object_type(child));
3661 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3663 /* Check whether the name has been inserted to the target */
3664 if (dt_try_as_dir(env, dt_tobj)) {
3665 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3667 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3668 (struct dt_key *)name);
3669 if (unlikely(rc == 0))
3670 target_exist = true;
3673 handle = mdd_trans_create(env, mdd);
3675 GOTO(out_put, rc = PTR_ERR(handle));
3677 /* Note: this transaction is part of migration, and it is not
3678 * the last step of migration, so we set th_local = 1 to avoid
3679 * updating last rcvd for this transaction */
3680 handle->th_local = 1;
3681 if (likely(!target_exist)) {
3682 rc = mdo_declare_index_insert(env, mdd_tobj,
3684 mdd_object_type(child),
3690 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3696 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3701 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3705 /* Update .. for child */
3706 rc = mdo_declare_index_delete(env, child, dotdot,
3711 rc = mdo_declare_index_insert(env, child,
3712 mdd_object_fid(mdd_tobj),
3713 S_IFDIR, dotdot, handle);
3718 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3725 rc = mdd_trans_start(env, mdd, handle);
3727 CERROR("%s: transaction start failed: rc = %d\n",
3728 mdd2obd_dev(mdd)->obd_name, rc);
3732 if (likely(!target_exist)) {
3733 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3734 mdd_object_type(child),
3740 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3745 rc = __mdd_index_delete_only(env, child, dotdot,
3750 rc = __mdd_index_insert_only(env, child,
3751 mdd_object_fid(mdd_tobj), S_IFDIR,
3757 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3758 strlen(name), handle);
3761 mdd_write_unlock(env, child);
3762 mdd_object_put(env, child);
3763 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3770 result = iops->next(env, it);
3771 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3772 GOTO(out, rc = -EINTR);
3774 if (result == -ESTALE)
3776 } while (result == 0);
3779 iops->fini(env, it);
3781 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3785 static int mdd_declare_update_linkea(const struct lu_env *env,
3786 struct mdd_object *mdd_pobj,
3787 struct mdd_object *mdd_sobj,
3788 struct mdd_object *mdd_tobj,
3789 const struct lu_name *child_name,
3790 struct linkea_data *ldata,
3791 struct thandle *handle)
3793 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3794 child_name, ldata, handle, 1);
3797 static int mdd_update_linkea(const struct lu_env *env,
3798 struct mdd_object *mdd_pobj,
3799 struct mdd_object *mdd_sobj,
3800 struct mdd_object *mdd_tobj,
3801 const struct lu_name *child_name,
3802 struct linkea_data *ldata,
3803 struct thandle *handle)
3805 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3806 child_name, ldata, handle, 0);
3809 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3810 struct mdd_object *mdd_pobj,
3811 struct mdd_object *mdd_sobj,
3812 struct mdd_object *mdd_tobj,
3813 const struct lu_name *lname,
3815 struct lu_attr *parent_la,
3816 struct linkea_data *ldata,
3817 struct thandle *handle)
3819 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3820 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3823 /* Revert IMMUTABLE flag */
3824 la_flag->la_valid = LA_FLAGS;
3825 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3826 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3830 /* delete entry from source dir */
3831 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3835 if (ldata->ld_buf != NULL) {
3836 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3837 mdd_tobj, lname, ldata, handle);
3842 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3843 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3848 handle->th_complex = 1;
3849 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3851 LU_XATTR_REPLACE, handle);
3856 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3857 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3863 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3864 mdd_object_type(mdd_tobj),
3865 lname->ln_name, handle);
3869 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3873 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3874 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3879 /* delete old object */
3880 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3884 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3885 /* delete old object */
3886 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3889 /* set nlink to 0 */
3890 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3895 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3899 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3903 rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3908 static int mdd_migrate_update_name(const struct lu_env *env,
3909 struct mdd_object *mdd_pobj,
3910 struct mdd_object *mdd_sobj,
3911 struct mdd_object *mdd_tobj,
3912 const struct lu_name *lname,
3915 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3916 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3917 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3918 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3919 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3920 struct thandle *handle;
3921 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3922 const char *name = lname->ln_name;
3926 /* update time for parent */
3927 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3928 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3929 p_la->la_valid = LA_CTIME;
3931 rc = mdd_la_get(env, mdd_sobj, so_attr);
3935 ldata->ld_buf = NULL;
3936 rc = mdd_links_read(env, mdd_sobj, ldata);
3937 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3940 handle = mdd_trans_create(env, mdd);
3942 RETURN(PTR_ERR(handle));
3944 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3945 lname, so_attr, p_la, ldata,
3948 /* If the migration can not be fit in one transaction, just
3949 * leave it in the original MDT */
3951 GOTO(stop_trans, rc = 0);
3953 GOTO(stop_trans, rc);
3956 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3957 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3958 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3959 PFID(mdd_object_fid(mdd_tobj)));
3961 rc = mdd_trans_start(env, mdd, handle);
3963 GOTO(stop_trans, rc);
3965 /* Revert IMMUTABLE flag */
3966 la_flag->la_valid = LA_FLAGS;
3967 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3968 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3970 GOTO(stop_trans, rc);
3972 /* Remove source name from source directory */
3973 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3975 GOTO(stop_trans, rc);
3977 if (ldata->ld_buf != NULL) {
3978 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3979 lname, ldata, handle);
3981 GOTO(stop_trans, rc);
3983 /* linkea update might decrease the source object
3984 * nlink, let's get the attr again after ref_del */
3985 rc = mdd_la_get(env, mdd_sobj, so_attr);
3987 GOTO(stop_trans, rc);
3990 if (S_ISREG(so_attr->la_mode)) {
3991 if (so_attr->la_nlink == 1) {
3992 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3994 if (rc != 0 && rc != -ENODATA)
3995 GOTO(stop_trans, rc);
3997 rc = mdo_xattr_set(env, mdd_tobj, NULL,
3999 LU_XATTR_REPLACE, handle);
4001 GOTO(stop_trans, rc);
4005 /* Insert new fid with target name into target dir */
4006 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4007 mdd_object_type(mdd_tobj), name, handle);
4009 GOTO(stop_trans, rc);
4011 linkea_add_buf(ldata, lname, mdd_object_fid(mdd_pobj));
4012 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
4015 GOTO(stop_trans, rc);
4017 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4019 /* Increase mod_count to add the source object to the orphan list,
4020 * so if other clients still send RPC to the old object, then these
4021 * objects can help the request to find the new object, see
4022 * mdt_reint_open() */
4023 mdd_sobj->mod_count++;
4024 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
4025 mdd_sobj->mod_count--;
4027 GOTO(out_unlock, rc);
4029 mdo_ref_del(env, mdd_sobj, handle);
4031 mdo_ref_del(env, mdd_sobj, handle);
4033 /* Get the attr again after ref_del */
4034 rc = mdd_la_get(env, mdd_sobj, so_attr);
4036 GOTO(out_unlock, rc);
4038 ma->ma_attr = *so_attr;
4039 ma->ma_valid |= MA_INODE;
4041 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4043 GOTO(out_unlock, rc);
4045 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4046 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4047 mdo2fid(mdd_pobj), lname, lname, handle);
4049 CWARN("%s: changelog for migrate %s "DFID
4050 "under "DFID" failed: rc = %d\n",
4051 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4052 PFID(mdd_object_fid(mdd_sobj)),
4053 PFID(mdd_object_fid(mdd_pobj)), rc);
4054 /* Sigh, there are no easy way to migrate back the object, so
4055 * let's reset the result to 0 for now XXX */
4059 mdd_write_unlock(env, mdd_sobj);
4062 mdd_trans_stop(env, mdd, rc, handle);
4067 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4068 const struct lu_fid *fid, __u32 *mdt_index)
4070 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4071 struct seq_server_site *ss;
4074 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4076 range->lsr_flags = LU_SEQ_RANGE_MDT;
4077 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4081 *mdt_index = range->lsr_index;
4086 * Check whether we should migrate the file/dir
4088 * < 0 permission check failed or other error.
4089 * = 0 the file can be migrated.
4090 * > 0 the file does not need to be migrated, mostly
4091 * for multiple link file
4093 static int mdd_migrate_sanity_check(const struct lu_env *env,
4094 struct mdd_object *pobj,
4095 const struct lu_attr *pattr,
4096 struct mdd_object *sobj,
4097 struct lu_attr *sattr)
4099 struct mdd_thread_info *info = mdd_env_info(env);
4100 struct linkea_data *ldata = &info->mti_link_data;
4101 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4103 struct lu_buf *mgr_buf;
4109 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4110 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4111 if (mgr_buf->lb_buf == NULL)
4114 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4116 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4118 /* If the object has migrateEA, it means IMMUTE flag
4119 * is being set by previous migration process, so it
4120 * needs to override the IMMUTE flag, otherwise the
4121 * following sanity check will fail */
4122 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4123 LMV_HASH_FLAG_MIGRATION) {
4124 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4126 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4127 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4128 mdd2obd_dev(mdd)->obd_name,
4129 PFID(mdd_object_fid(sobj)));
4133 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4134 sobj, sattr, NULL, NULL);
4138 /* Then it will check if the file should be migrated. If the file
4139 * has mulitple links, we only need migrate the file if all of its
4140 * entries has been migrated to the remote MDT */
4141 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4144 rc = mdd_links_read(env, sobj, ldata);
4146 /* For multiple links files, if there are no linkEA data at all,
4147 * means the file might be created before linkEA is enabled, and
4148 * all of its links should not be migrated yet, otherwise it
4149 * should have some linkEA there */
4150 if (rc == -ENOENT || rc == -ENODATA)
4155 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4156 /* If there are still links locally, then the file will not be
4158 LASSERT(ldata->ld_leh != NULL);
4159 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4160 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4161 struct lu_name lname;
4163 __u32 parent_mdt_index;
4165 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4167 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4170 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4174 /* Migrate the object only if none of its parents are on the
4176 if (parent_mdt_index != mdt_index)
4179 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4180 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4181 lname.ln_name, PFID(&fid));
4189 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4190 struct md_object *sobj, const struct lu_name *lname,
4191 struct md_object *tobj, struct md_attr *ma)
4193 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4194 struct mdd_device *mdd = mdo2mdd(pobj);
4195 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4196 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4197 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4198 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4202 /* If the file will being migrated, it will check whether
4203 * the file is being opened by someone else right now */
4204 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4205 if (mdd_sobj->mod_count > 0) {
4206 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4207 mdd2obd_dev(mdd)->obd_name,
4208 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4209 mdd_sobj->mod_count, -EBUSY);
4210 mdd_read_unlock(env, mdd_sobj);
4211 GOTO(put, rc = -EBUSY);
4213 mdd_read_unlock(env, mdd_sobj);
4215 rc = mdd_la_get(env, mdd_sobj, so_attr);
4219 rc = mdd_la_get(env, mdd_pobj, pattr);
4223 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4230 /* Sigh, it is impossible to finish all of migration in a single
4231 * transaction, for example migrating big directory entries to the
4232 * new MDT, it needs insert all of name entries of children in the
4235 * So migration will be done in multiple steps and transactions.
4237 * 1. create an orphan object on the remote MDT in one transaction.
4238 * 2. migrate extend attributes to the new target file/directory.
4239 * 3. For directory, migrate the entries to the new MDT and update
4240 * linkEA of each children. Because we can not migrate all entries
4241 * in a single transaction, so the migrating directory will become
4242 * a striped directory during migration, so once the process is
4243 * interrupted, the directory is still accessible. (During lookup,
4244 * client will locate the name by searching both original and target
4246 * 4. Finally, update the name/FID to point to the new file/directory
4247 * in a separate transaction.
4250 /* step 1: Check whether the orphan object has been created, and create
4251 * orphan object on the remote MDT if needed */
4252 if (!mdd_object_exists(mdd_tobj)) {
4253 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4259 LASSERT(mdd_object_exists(mdd_tobj));
4260 /* step 2: migrate xattr */
4261 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4265 /* step 3: migrate name entries to the orphan object */
4266 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4267 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4270 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4271 OBD_FAIL_MDS_REINT_NET_REP)))
4274 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4277 LASSERT(mdd_object_exists(mdd_tobj));
4278 /* step 4: update name entry to the new object */
4279 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4287 const struct md_dir_operations mdd_dir_ops = {
4288 .mdo_is_subdir = mdd_is_subdir,
4289 .mdo_lookup = mdd_lookup,
4290 .mdo_create = mdd_create,
4291 .mdo_rename = mdd_rename,
4292 .mdo_link = mdd_link,
4293 .mdo_unlink = mdd_unlink,
4294 .mdo_create_data = mdd_create_data,
4295 .mdo_migrate = mdd_migrate,