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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_dir.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@intel.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_mds.h>
44 #include <lustre_fid.h>
45 #include <lustre_lmv.h>
46 #include <lustre_idmap.h>
48 #include "mdd_internal.h"
50 static const char dot[] = ".";
51 static const char dotdot[] = "..";
53 static struct lu_name lname_dotdot = {
54 .ln_name = (char *) dotdot,
55 .ln_namelen = sizeof(dotdot) - 1,
59 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
61 if (!lu_name_is_valid(ln))
63 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
69 /* Get FID from name and parent */
71 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_attr *pattr, const struct lu_name *lname,
73 struct lu_fid* fid, int mask)
75 const char *name = lname->ln_name;
76 const struct dt_key *key = (const struct dt_key *)name;
77 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
78 struct dt_object *dir = mdd_object_child(mdd_obj);
83 if (unlikely(mdd_is_dead_obj(mdd_obj)))
86 if (!mdd_object_exists(mdd_obj))
89 if (mdd_object_remote(mdd_obj)) {
90 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
91 mdd_obj_dev_name(mdd_obj),
92 PFID(mdd_object_fid(mdd_obj)));
95 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
100 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101 dt_try_as_dir(env, dir)))
102 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
122 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
126 /** Read the link EA into a temp buffer.
127 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
128 * A pointer to the buffer is stored in \a ldata::ld_buf.
132 static int __mdd_links_read(const struct lu_env *env,
133 struct mdd_object *mdd_obj,
134 struct linkea_data *ldata)
138 if (!mdd_object_exists(mdd_obj))
141 /* First try a small buf */
142 LASSERT(env != NULL);
143 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
145 if (ldata->ld_buf->lb_buf == NULL)
148 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
150 /* Buf was too small, figure out what we need. */
151 lu_buf_free(ldata->ld_buf);
152 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
156 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
157 if (ldata->ld_buf->lb_buf == NULL)
159 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
163 lu_buf_free(ldata->ld_buf);
164 ldata->ld_buf = NULL;
168 return linkea_init(ldata);
171 int mdd_links_read(const struct lu_env *env,
172 struct mdd_object *mdd_obj,
173 struct linkea_data *ldata)
177 rc = __mdd_links_read(env, mdd_obj, ldata);
179 rc = linkea_init(ldata);
184 static int mdd_links_read_with_rec(const struct lu_env *env,
185 struct mdd_object *mdd_obj,
186 struct linkea_data *ldata)
190 rc = __mdd_links_read(env, mdd_obj, ldata);
192 rc = linkea_init_with_rec(ldata);
198 * Get parent FID of the directory
200 * Read parent FID from linkEA, if that fails, then do lookup
201 * dotdot to get the parent FID.
203 * \param[in] env execution environment
204 * \param[in] obj object from which to find the parent FID
205 * \param[in] attr attribute of the object
206 * \param[out] fid fid to get the parent FID
208 * \retval 0 if getting the parent FID succeeds.
209 * \retval negative errno if getting the parent FID fails.
211 static inline int mdd_parent_fid(const struct lu_env *env,
212 struct mdd_object *obj,
213 const struct lu_attr *attr,
216 struct mdd_thread_info *info = mdd_env_info(env);
217 struct linkea_data ldata = { NULL };
218 struct lu_buf *buf = &info->mti_link_buf;
219 struct lu_name lname;
224 LASSERTF(S_ISDIR(mdd_object_type(obj)),
225 "%s: FID "DFID" is not a directory type = %o\n",
226 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
227 mdd_object_type(obj));
229 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
230 if (buf->lb_buf == NULL)
231 GOTO(lookup, rc = 0);
234 rc = mdd_links_read_with_rec(env, obj, &ldata);
238 /* the obj is not locked, don't cache attributes */
239 mdd_invalidate(env, &obj->mod_obj);
241 LASSERT(ldata.ld_leh != NULL);
242 /* Directory should only have 1 parent */
243 if (ldata.ld_leh->leh_reccount > 1)
246 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
248 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
249 if (likely(fid_is_sane(fid)))
252 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
257 * For root fid use special function, which does not compare version component
258 * of fid. Version component is different for root fids on all MDTs.
260 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
262 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
263 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
267 * return 1: if \a tfid is the fid of the ancestor of \a mo;
269 * otherwise: values < 0, errors.
271 static int mdd_is_parent(const struct lu_env *env,
272 struct mdd_device *mdd,
273 struct mdd_object *mo,
274 const struct lu_attr *attr,
275 const struct lu_fid *tfid)
277 struct mdd_object *mp;
281 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
282 pfid = &mdd_env_info(env)->mti_fid;
284 if (mdd_is_root(mdd, mdd_object_fid(mo)))
287 if (mdd_is_root(mdd, tfid))
290 rc = mdd_parent_fid(env, mo, attr, pfid);
295 if (lu_fid_eq(pfid, tfid))
298 if (mdd_is_root(mdd, pfid))
301 mp = mdd_object_find(env, mdd, pfid);
305 if (!mdd_object_exists(mp)) {
306 mdd_object_put(env, mp);
310 rc = mdd_parent_fid(env, mp, attr, pfid);
311 mdd_object_put(env, mp);
320 * No permission check is needed.
322 * returns 1: if fid is ancestor of @mo;
323 * returns 0: if fid is not an ancestor of @mo;
324 * returns < 0: if error
326 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
327 const struct lu_fid *fid)
329 struct mdd_device *mdd = mdo2mdd(mo);
330 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
334 if (!mdd_object_exists(md2mdd_obj(mo)))
337 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
340 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
344 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
349 * Check that @dir contains no entries except (possibly) dot and dotdot.
354 * -ENOTDIR not a directory object
355 * -ENOTEMPTY not empty
359 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
362 struct dt_object *obj;
363 const struct dt_it_ops *iops;
367 obj = mdd_object_child(dir);
368 if (!dt_try_as_dir(env, obj))
371 iops = &obj->do_index_ops->dio_it;
372 it = iops->init(env, obj, LUDA_64BITHASH);
374 result = iops->get(env, it, (const struct dt_key *)"");
377 for (result = 0, i = 0; result == 0 && i < 3; ++i)
378 result = iops->next(env, it);
381 else if (result == 1)
383 } else if (result == 0)
385 * Huh? Index contains no zero key?
392 result = PTR_ERR(it);
393 /* -ENODEV means no valid stripe */
394 if (result == -ENODEV)
401 * Determine if the target object can be hard linked, and right now it only
402 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
403 * directory nlink count might exceed the maximum link count(see
404 * osd_object_ref_add), so it only check nlink for non-directories.
406 * \param[in] env thread environment
407 * \param[in] obj object being linked to
408 * \param[in] la attributes of \a obj
410 * \retval 0 if \a obj can be hard linked
411 * \retval negative error if \a obj is a directory or has too
414 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
415 const struct lu_attr *la)
417 struct mdd_device *m = mdd_obj2mdd_dev(obj);
422 /* Subdir count limitation can be broken through
423 * (see osd_object_ref_add), so only check non-directory here. */
424 if (!S_ISDIR(la->la_mode) &&
425 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
432 * Check whether it may create the cobj under the pobj.
434 * \param[in] env execution environment
435 * \param[in] pobj the parent directory
436 * \param[in] pattr the attribute of the parent directory
437 * \param[in] cobj the child to be created
438 * \param[in] check_perm if check WRITE|EXEC permission for parent
440 * \retval = 0 create the child under this dir is allowed
441 * \retval negative errno create the child under this dir is
444 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
445 const struct lu_attr *pattr, struct mdd_object *cobj,
451 if (cobj && mdd_object_exists(cobj))
454 if (mdd_is_dead_obj(pobj))
458 rc = mdd_permission_internal_locked(env, pobj, pattr,
459 MAY_WRITE | MAY_EXEC,
465 * Check whether can unlink from the pobj in the case of "cobj == NULL".
467 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
468 const struct lu_attr *pattr, const struct lu_attr *attr)
473 if (mdd_is_dead_obj(pobj))
476 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
479 rc = mdd_permission_internal_locked(env, pobj, pattr,
480 MAY_WRITE | MAY_EXEC,
485 if (pattr->la_flags & LUSTRE_APPEND_FL)
492 * pobj == NULL is remote ops case, under such case, pobj's
493 * VTX feature has been checked already, no need check again.
495 static inline int mdd_is_sticky(const struct lu_env *env,
496 struct mdd_object *pobj,
497 const struct lu_attr *pattr,
498 struct mdd_object *cobj,
499 const struct lu_attr *cattr)
501 struct lu_ucred *uc = lu_ucred_assert(env);
504 LASSERT(pattr != NULL);
505 if (!(pattr->la_mode & S_ISVTX) ||
506 (pattr->la_uid == uc->uc_fsuid))
510 LASSERT(cattr != NULL);
511 if (cattr->la_uid == uc->uc_fsuid)
514 return !md_capable(uc, CFS_CAP_FOWNER);
517 static int mdd_may_delete_entry(const struct lu_env *env,
518 struct mdd_object *pobj,
519 const struct lu_attr *pattr,
524 LASSERT(pobj != NULL);
525 if (!mdd_object_exists(pobj))
528 if (mdd_is_dead_obj(pobj))
533 rc = mdd_permission_internal_locked(env, pobj, pattr,
534 MAY_WRITE | MAY_EXEC,
540 if (pattr->la_flags & LUSTRE_APPEND_FL)
547 * Check whether it may delete the cobj from the pobj.
550 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
551 const struct lu_attr *tpattr, struct mdd_object *tobj,
552 const struct lu_attr *tattr, const struct lu_attr *cattr,
553 int check_perm, int check_empty)
559 LASSERT(tpattr != NULL);
560 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
568 if (!mdd_object_exists(tobj))
571 if (mdd_is_dead_obj(tobj))
574 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
577 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
580 /* additional check the rename case */
582 if (S_ISDIR(cattr->la_mode)) {
583 if (!S_ISDIR(tattr->la_mode))
586 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
587 mdd_object_fid(tobj)))
589 } else if (S_ISDIR(tattr->la_mode))
593 if (S_ISDIR(tattr->la_mode) && check_empty)
594 rc = mdd_dir_is_empty(env, tobj);
600 * Check whether it can create the link file(linked to @src_obj) under
601 * the target directory(@tgt_obj), and src_obj has been locked by
604 * \param[in] env execution environment
605 * \param[in] tgt_obj the target directory
606 * \param[in] tattr attributes of target directory
607 * \param[in] lname the link name
608 * \param[in] src_obj source object for link
609 * \param[in] cattr attributes for source object
611 * \retval = 0 it is allowed to create the link file under tgt_obj
612 * \retval negative error not allowed to create the link file
614 static int mdd_link_sanity_check(const struct lu_env *env,
615 struct mdd_object *tgt_obj,
616 const struct lu_attr *tattr,
617 const struct lu_name *lname,
618 struct mdd_object *src_obj,
619 const struct lu_attr *cattr)
621 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
625 if (!mdd_object_exists(src_obj))
628 if (mdd_is_dead_obj(src_obj))
631 /* Local ops, no lookup before link, check filename length here. */
632 rc = mdd_name_check(m, lname);
636 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
639 if (S_ISDIR(mdd_object_type(src_obj)))
642 LASSERT(src_obj != tgt_obj);
643 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
647 rc = __mdd_may_link(env, src_obj, cattr);
652 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
653 const char *name, struct thandle *handle)
655 struct dt_object *next = mdd_object_child(pobj);
659 if (dt_try_as_dir(env, next))
660 rc = dt_delete(env, next, (struct dt_key *)name, handle);
667 static int __mdd_index_insert_only(const struct lu_env *env,
668 struct mdd_object *pobj,
669 const struct lu_fid *lf, __u32 type,
671 struct thandle *handle)
673 struct dt_object *next = mdd_object_child(pobj);
677 if (dt_try_as_dir(env, next)) {
678 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
681 rec->rec_type = type;
682 rc = dt_insert(env, next, (const struct dt_rec *)rec,
683 (const struct dt_key *)name, handle);
690 /* insert named index, add reference if isdir */
691 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
692 const struct lu_fid *lf, __u32 type,
693 const char *name, struct thandle *handle)
698 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
699 if (rc == 0 && S_ISDIR(type)) {
700 mdd_write_lock(env, pobj, DT_TGT_PARENT);
701 mdo_ref_add(env, pobj, handle);
702 mdd_write_unlock(env, pobj);
708 /* delete named index, drop reference if isdir */
709 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
710 const char *name, int is_dir,
711 struct thandle *handle)
716 rc = __mdd_index_delete_only(env, pobj, name, handle);
717 if (rc == 0 && is_dir) {
718 mdd_write_lock(env, pobj, DT_TGT_PARENT);
719 mdo_ref_del(env, pobj, handle);
720 mdd_write_unlock(env, pobj);
726 static int mdd_llog_record_calc_size(const struct lu_env *env,
727 const struct lu_name *tname,
728 const struct lu_name *sname)
730 const struct lu_ucred *uc = lu_ucred(env);
731 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
732 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
735 clf_flags |= CLF_RENAME;
737 if (uc != NULL && uc->uc_jobid[0] != '\0')
738 clf_flags |= CLF_JOBID;
740 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
741 changelog_rec_offset(clf_flags, crfe) +
742 (tname != NULL ? tname->ln_namelen : 0) +
743 (sname != NULL ? 1 + sname->ln_namelen : 0));
746 int mdd_declare_changelog_store(const struct lu_env *env,
747 struct mdd_device *mdd,
748 enum changelog_rec_type type,
749 const struct lu_name *tname,
750 const struct lu_name *sname,
751 struct thandle *handle)
753 struct obd_device *obd = mdd2obd_dev(mdd);
754 struct llog_ctxt *ctxt;
755 struct llog_changelog_rec *rec;
757 struct thandle *llog_th;
761 if (!mdd_changelog_enabled(env, mdd, type))
764 reclen = mdd_llog_record_calc_size(env, tname, sname);
765 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
766 if (buf->lb_buf == NULL)
770 rec->cr_hdr.lrh_len = reclen;
771 rec->cr_hdr.lrh_type = CHANGELOG_REC;
773 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
777 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
779 GOTO(out_put, rc = PTR_ERR(llog_th));
781 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
789 int mdd_changelog_write_rec(const struct lu_env *env,
790 struct llog_handle *loghandle,
791 struct llog_rec_hdr *r,
792 struct llog_cookie *cookie,
793 int idx, struct thandle *th)
797 if (r->lrh_type == CHANGELOG_REC) {
798 struct mdd_device *mdd;
799 struct llog_changelog_rec *rec;
801 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
802 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
804 spin_lock(&mdd->mdd_cl.mc_lock);
805 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
806 spin_unlock(&mdd->mdd_cl.mc_lock);
808 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
812 * if current llog is full, we will generate a new
813 * llog, and since it's actually not an error, let's
814 * avoid increasing index so that userspace apps
815 * should not see a gap in the changelog sequence
817 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
818 spin_lock(&mdd->mdd_cl.mc_lock);
819 ++mdd->mdd_cl.mc_index;
820 spin_unlock(&mdd->mdd_cl.mc_lock);
823 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
830 /** Add a changelog entry \a rec to the changelog llog
833 * \param handle - currently ignored since llogs start their own transaction;
834 * this will hopefully be fixed in llog rewrite
837 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
838 struct llog_changelog_rec *rec, struct thandle *th)
840 struct obd_device *obd = mdd2obd_dev(mdd);
841 struct llog_ctxt *ctxt;
842 struct thandle *llog_th;
845 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
846 changelog_rec_varsize(&rec->cr));
848 /* llog_lvfs_write_rec sets the llog tail len */
849 rec->cr_hdr.lrh_type = CHANGELOG_REC;
850 rec->cr.cr_time = cl_time();
852 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
856 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
858 GOTO(out_put, rc = PTR_ERR(llog_th));
860 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
861 /* nested journal transaction */
862 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
864 /* time to recover some space ?? */
865 if (likely(!mdd->mdd_changelog_gc ||
866 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
867 mdd->mdd_changelog_min_gc_interval >=
868 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
869 /* save a spin_lock trip */
871 spin_lock(&mdd->mdd_cl.mc_lock);
872 if (likely(mdd->mdd_changelog_gc &&
873 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
874 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
875 mdd->mdd_changelog_min_gc_interval)) {
876 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
877 mdd->mdd_changelog_min_free_cat_entries ||
878 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
879 CWARN("%s:%s low on changelog_catalog free entries, "
880 "starting ChangeLog garbage collection thread\n",
882 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
885 /* indicate further kthread run will occur outside
886 * right after current journal transaction filling has
889 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
891 /* next check in mdd_changelog_min_gc_interval anyway
893 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
895 spin_unlock(&mdd->mdd_cl.mc_lock);
903 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
904 const struct lu_fid *sfid,
905 const struct lu_fid *spfid,
906 const struct lu_name *sname)
908 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
911 LASSERT(sfid != NULL);
912 LASSERT(spfid != NULL);
913 LASSERT(sname != NULL);
915 extsize = sname->ln_namelen + 1;
917 rnm->cr_sfid = *sfid;
918 rnm->cr_spfid = *spfid;
920 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
921 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
922 rec->cr_namelen += extsize;
925 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
927 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
929 if (jobid == NULL || jobid[0] == '\0')
932 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
935 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
937 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
939 ef->cr_extra_flags = eflags;
942 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
943 __u64 uid, __u64 gid)
945 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
947 uidgid->cr_uid = uid;
948 uidgid->cr_gid = gid;
951 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
954 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
959 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
961 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
963 omd->cr_openflags = flags;
966 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
967 const char *xattr_name)
969 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
971 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
974 /** Store a namespace change changelog record
975 * If this fails, we must fail the whole transaction; we don't
976 * want the change to commit without the log entry.
977 * \param target - mdd_object of change
978 * \param tpfid - target parent dir/object fid
979 * \param sfid - source object fid
980 * \param spfid - source parent fid
981 * \param tname - target name string
982 * \param sname - source name string
983 * \param handle - transaction handle
985 int mdd_changelog_ns_store(const struct lu_env *env,
986 struct mdd_device *mdd,
987 enum changelog_rec_type type,
988 enum changelog_rec_flags clf_flags,
989 struct mdd_object *target,
990 const struct lu_fid *tpfid,
991 const struct lu_fid *sfid,
992 const struct lu_fid *spfid,
993 const struct lu_name *tname,
994 const struct lu_name *sname,
995 struct thandle *handle)
997 const struct lu_ucred *uc = lu_ucred(env);
998 struct llog_changelog_rec *rec;
1001 __u64 xflags = CLFE_INVALID;
1005 if (!mdd_changelog_enabled(env, mdd, type))
1008 LASSERT(tpfid != NULL);
1009 LASSERT(tname != NULL);
1010 LASSERT(handle != NULL);
1012 reclen = mdd_llog_record_calc_size(env, tname, sname);
1013 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
1014 if (buf->lb_buf == NULL)
1018 clf_flags &= CLF_FLAGMASK;
1019 clf_flags |= CLF_EXTRA_FLAGS;
1022 if (uc->uc_jobid[0] != '\0')
1023 clf_flags |= CLF_JOBID;
1024 xflags |= CLFE_UIDGID;
1029 clf_flags |= CLF_RENAME;
1031 clf_flags |= CLF_VERSION;
1033 rec->cr.cr_flags = clf_flags;
1035 if (clf_flags & CLF_EXTRA_FLAGS) {
1036 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1037 if (xflags & CLFE_UIDGID)
1038 mdd_changelog_rec_extra_uidgid(&rec->cr,
1039 uc->uc_uid, uc->uc_gid);
1040 if (xflags & CLFE_NID)
1041 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1044 rec->cr.cr_type = (__u32)type;
1045 rec->cr.cr_pfid = *tpfid;
1046 rec->cr.cr_namelen = tname->ln_namelen;
1047 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1049 if (clf_flags & CLF_RENAME)
1050 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1052 if (clf_flags & CLF_JOBID)
1053 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1055 if (likely(target != NULL)) {
1056 rec->cr.cr_tfid = *mdd_object_fid(target);
1057 target->mod_cltime = ktime_get();
1059 fid_zero(&rec->cr.cr_tfid);
1062 rc = mdd_changelog_store(env, mdd, rec, handle);
1064 CERROR("%s: cannot store changelog record: type = %d, "
1065 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1066 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1067 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1074 static int __mdd_links_add(const struct lu_env *env,
1075 struct mdd_object *mdd_obj,
1076 struct linkea_data *ldata,
1077 const struct lu_name *lname,
1078 const struct lu_fid *pfid,
1079 int first, int check)
1083 if (ldata->ld_leh == NULL) {
1084 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1088 rc = linkea_data_new(ldata,
1089 &mdd_env_info(env)->mti_link_buf);
1096 rc = linkea_links_find(ldata, lname, pfid);
1097 if (rc && rc != -ENOENT)
1103 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1104 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1108 linkea_add_buf(ldata, lname, tfid);
1111 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1112 linkea_add_buf(ldata, lname, pfid);
1114 return linkea_add_buf(ldata, lname, pfid);
1117 static int __mdd_links_del(const struct lu_env *env,
1118 struct mdd_object *mdd_obj,
1119 struct linkea_data *ldata,
1120 const struct lu_name *lname,
1121 const struct lu_fid *pfid)
1125 if (ldata->ld_leh == NULL) {
1126 rc = mdd_links_read(env, mdd_obj, ldata);
1131 rc = linkea_links_find(ldata, lname, pfid);
1135 linkea_del_buf(ldata, lname);
1139 static int mdd_linkea_prepare(const struct lu_env *env,
1140 struct mdd_object *mdd_obj,
1141 const struct lu_fid *oldpfid,
1142 const struct lu_name *oldlname,
1143 const struct lu_fid *newpfid,
1144 const struct lu_name *newlname,
1145 int first, int check,
1146 struct linkea_data *ldata)
1151 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1154 LASSERT(oldpfid != NULL || newpfid != NULL);
1156 if (mdd_obj->mod_flags & DEAD_OBJ)
1157 /* Unnecessary to update linkEA for dead object. */
1160 if (oldpfid != NULL) {
1161 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1163 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1166 /* No changes done. */
1171 /* If renaming, add the new record */
1172 if (newpfid != NULL)
1173 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1179 int mdd_links_rename(const struct lu_env *env,
1180 struct mdd_object *mdd_obj,
1181 const struct lu_fid *oldpfid,
1182 const struct lu_name *oldlname,
1183 const struct lu_fid *newpfid,
1184 const struct lu_name *newlname,
1185 struct thandle *handle,
1186 struct linkea_data *ldata,
1187 int first, int check)
1192 if (ldata == NULL) {
1193 ldata = &mdd_env_info(env)->mti_link_data;
1194 memset(ldata, 0, sizeof(*ldata));
1195 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1196 newpfid, newlname, first, check, ldata);
1201 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1202 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1208 if (newlname == NULL)
1209 CERROR("link_ea add failed %d "DFID"\n",
1210 rc, PFID(mdd_object_fid(mdd_obj)));
1211 else if (oldpfid == NULL)
1212 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1213 newlname->ln_namelen, newlname->ln_name, rc,
1214 PFID(mdd_object_fid(mdd_obj)));
1215 else if (newpfid == NULL)
1216 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1217 oldlname->ln_namelen, oldlname->ln_name, rc,
1218 PFID(mdd_object_fid(mdd_obj)));
1220 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1221 "\n", oldlname->ln_namelen, oldlname->ln_name,
1222 newlname->ln_namelen, newlname->ln_name, rc,
1223 PFID(mdd_object_fid(mdd_obj)));
1226 if (is_vmalloc_addr(ldata->ld_buf))
1227 /* if we vmalloced a large buffer drop it */
1228 lu_buf_free(ldata->ld_buf);
1233 static inline int mdd_links_add(const struct lu_env *env,
1234 struct mdd_object *mdd_obj,
1235 const struct lu_fid *pfid,
1236 const struct lu_name *lname,
1237 struct thandle *handle,
1238 struct linkea_data *ldata, int first)
1240 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1241 pfid, lname, handle, ldata, first, 0);
1244 static inline int mdd_links_del(const struct lu_env *env,
1245 struct mdd_object *mdd_obj,
1246 const struct lu_fid *pfid,
1247 const struct lu_name *lname,
1248 struct thandle *handle)
1250 return mdd_links_rename(env, mdd_obj, pfid, lname,
1251 NULL, NULL, handle, NULL, 0, 0);
1254 /** Read the link EA into a temp buffer.
1255 * Uses the name_buf since it is generally large.
1256 * \retval IS_ERR err
1257 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1259 struct lu_buf *mdd_links_get(const struct lu_env *env,
1260 struct mdd_object *mdd_obj)
1262 struct linkea_data ldata = { NULL };
1265 rc = mdd_links_read(env, mdd_obj, &ldata);
1266 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1269 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1270 struct linkea_data *ldata, struct thandle *handle)
1272 const struct lu_buf *buf;
1275 if (ldata == NULL || ldata->ld_buf == NULL ||
1276 ldata->ld_leh == NULL)
1279 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1283 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1284 ldata->ld_leh->leh_len);
1285 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1286 if (unlikely(rc == -ENOSPC)) {
1287 rc = linkea_overflow_shrink(ldata);
1295 static int mdd_declare_links_add(const struct lu_env *env,
1296 struct mdd_object *mdd_obj,
1297 struct thandle *handle,
1298 struct linkea_data *ldata)
1304 if (ldata != NULL && ldata->ld_leh != NULL) {
1305 ea_len = ldata->ld_leh->leh_len;
1306 linkea = ldata->ld_buf->lb_buf;
1308 ea_len = MAX_LINKEA_SIZE;
1312 rc = mdo_declare_xattr_set(env, mdd_obj,
1313 mdd_buf_get_const(env, linkea, ea_len),
1314 XATTR_NAME_LINK, 0, handle);
1319 static inline int mdd_declare_links_del(const struct lu_env *env,
1320 struct mdd_object *c,
1321 struct thandle *handle)
1325 /* For directory, the linkEA will be removed together
1326 * with the object. */
1327 if (!S_ISDIR(mdd_object_type(c)))
1328 rc = mdd_declare_links_add(env, c, handle, NULL);
1333 static int mdd_declare_link(const struct lu_env *env,
1334 struct mdd_device *mdd,
1335 struct mdd_object *p,
1336 struct mdd_object *c,
1337 const struct lu_name *name,
1338 struct thandle *handle,
1340 struct linkea_data *data)
1342 struct lu_fid tfid = *mdd_object_fid(c);
1345 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1346 tfid.f_oid = cfs_fail_val;
1348 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1349 name->ln_name, handle);
1353 rc = mdo_declare_ref_add(env, c, handle);
1357 la->la_valid = LA_CTIME | LA_MTIME;
1358 rc = mdo_declare_attr_set(env, p, la, handle);
1362 la->la_valid = LA_CTIME;
1363 rc = mdo_declare_attr_set(env, c, la, handle);
1367 rc = mdd_declare_links_add(env, c, handle, data);
1371 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1377 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1378 struct md_object *src_obj, const struct lu_name *lname,
1381 const char *name = lname->ln_name;
1382 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1383 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1384 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1385 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1386 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1387 struct mdd_device *mdd = mdo2mdd(src_obj);
1388 struct thandle *handle;
1389 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1390 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1394 rc = mdd_la_get(env, mdd_sobj, cattr);
1398 rc = mdd_la_get(env, mdd_tobj, tattr);
1403 * If we are using project inheritance, we only allow hard link
1404 * creation in our tree when the project IDs are the same;
1405 * otherwise the tree quota mechanism could be circumvented.
1407 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1408 (tattr->la_projid != cattr->la_projid))
1411 handle = mdd_trans_create(env, mdd);
1413 GOTO(out_pending, rc = PTR_ERR(handle));
1415 memset(ldata, 0, sizeof(*ldata));
1417 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1418 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1420 /* Note: even this function will change ldata, but it comes from
1421 * thread_info, which is completely temporary and only seen in
1422 * this function, so we do not need reset ldata once it fails.*/
1423 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1424 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1428 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1433 rc = mdd_trans_start(env, mdd, handle);
1437 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1438 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1441 GOTO(out_unlock, rc);
1443 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1444 rc = mdo_ref_add(env, mdd_sobj, handle);
1446 GOTO(out_unlock, rc);
1449 *tfid = *mdd_object_fid(mdd_sobj);
1450 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1451 tfid->f_oid = cfs_fail_val;
1453 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1454 mdd_object_type(mdd_sobj), name, handle);
1456 mdo_ref_del(env, mdd_sobj, handle);
1457 GOTO(out_unlock, rc);
1460 la->la_valid = LA_CTIME | LA_MTIME;
1461 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1463 GOTO(out_unlock, rc);
1465 la->la_valid = LA_CTIME;
1466 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1468 /* Note: The failure of links_add should not cause the
1469 * link failure, so do not check return value. */
1470 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1471 lname, handle, ldata, 0);
1475 mdd_write_unlock(env, mdd_sobj);
1477 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1478 mdd_object_fid(mdd_tobj), NULL,
1479 NULL, lname, NULL, handle);
1481 rc = mdd_trans_stop(env, mdd, rc, handle);
1482 if (is_vmalloc_addr(ldata->ld_buf))
1483 /* if we vmalloced a large buffer drop it */
1484 lu_buf_free(ldata->ld_buf);
1489 static int mdd_mark_orphan_object(const struct lu_env *env,
1490 struct mdd_object *obj, struct thandle *handle,
1493 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1496 attr->la_valid = LA_FLAGS;
1497 attr->la_flags = LUSTRE_ORPHAN_FL;
1500 rc = mdo_declare_attr_set(env, obj, attr, handle);
1502 rc = mdo_attr_set(env, obj, attr, handle);
1507 static int mdd_declare_finish_unlink(const struct lu_env *env,
1508 struct mdd_object *obj,
1509 struct thandle *handle)
1513 /* Sigh, we do not know if the unlink object will become orphan in
1514 * declare phase, but fortunately the flags here does not matter
1515 * in current declare implementation */
1516 rc = mdd_mark_orphan_object(env, obj, handle, true);
1520 rc = mdo_declare_destroy(env, obj, handle);
1524 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1528 return mdd_declare_links_del(env, obj, handle);
1531 /* caller should take a lock before calling */
1532 int mdd_finish_unlink(const struct lu_env *env,
1533 struct mdd_object *obj, struct md_attr *ma,
1534 struct mdd_object *pobj,
1535 const struct lu_name *lname,
1539 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1542 LASSERT(mdd_write_locked(env, obj) != 0);
1544 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1545 /* add new orphan and the object
1546 * will be deleted during mdd_close() */
1547 obj->mod_flags |= DEAD_OBJ;
1548 if (obj->mod_count) {
1549 rc = mdd_orphan_insert(env, obj, th);
1551 CDEBUG(D_HA, "Object "DFID" is inserted into "
1552 "orphan list, open count = %d\n",
1553 PFID(mdd_object_fid(obj)),
1556 CERROR("Object "DFID" fail to be an orphan, "
1557 "open count = %d, maybe cause failed "
1559 PFID(mdd_object_fid(obj)),
1562 /* mark object as an orphan here, not
1563 * before mdd_orphan_insert() as racing
1564 * mdd_la_get() may propagate ORPHAN_OBJ
1565 * causing the asserition */
1566 rc = mdd_mark_orphan_object(env, obj, th, false);
1568 rc = mdo_destroy(env, obj, th);
1570 } else if (!is_dir) {
1571 /* old files may not have link ea; ignore errors */
1572 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1580 * has mdd_write_lock on cobj already, but not on pobj yet
1582 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1583 const struct lu_attr *pattr,
1584 struct mdd_object *cobj,
1585 const struct lu_attr *cattr)
1590 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1595 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1596 struct mdd_object *p, struct mdd_object *c,
1597 const struct lu_name *name, struct md_attr *ma,
1598 struct thandle *handle, int no_name, int is_dir)
1600 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1603 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1604 if (likely(no_name == 0)) {
1605 rc = mdo_declare_index_delete(env, p, name->ln_name,
1612 rc = mdo_declare_ref_del(env, p, handle);
1618 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1619 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1620 la->la_valid = LA_CTIME | LA_MTIME;
1621 rc = mdo_declare_attr_set(env, p, la, handle);
1626 rc = mdo_declare_ref_del(env, c, handle);
1630 rc = mdo_declare_ref_del(env, c, handle);
1634 la->la_valid = LA_CTIME;
1635 rc = mdo_declare_attr_set(env, c, la, handle);
1639 rc = mdd_declare_finish_unlink(env, c, handle);
1643 /* FIXME: need changelog for remove entry */
1644 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1652 * test if a file has an HSM archive
1653 * if HSM attributes are not found in ma update them from
1656 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1657 struct mdd_object *obj,
1662 if (!(ma->ma_valid & MA_HSM)) {
1663 /* no HSM MD provided, read xattr */
1664 struct lu_buf *hsm_buf;
1665 const size_t buflen = sizeof(struct hsm_attrs);
1668 hsm_buf = mdd_buf_get(env, NULL, 0);
1669 lu_buf_alloc(hsm_buf, buflen);
1670 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1671 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1672 lu_buf_free(hsm_buf);
1676 ma->ma_valid |= MA_HSM;
1678 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1684 * Delete name entry and the object.
1685 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1686 * does not exist for this object, and it could only happen during resending
1687 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1688 * is also needed in this case(needed by changelog), so we have to add another
1689 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1690 * the ENOENT failure should be able to be fixed by redo mechanism.
1692 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1693 struct md_object *cobj, const struct lu_name *lname,
1694 struct md_attr *ma, int no_name)
1696 const char *name = lname->ln_name;
1697 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1698 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1699 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1700 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1701 struct mdd_object *mdd_cobj = NULL;
1702 struct mdd_device *mdd = mdo2mdd(pobj);
1703 struct thandle *handle;
1704 int rc, is_dir = 0, cl_flags = 0;
1707 /* let shutdown to start */
1708 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1710 /* cobj == NULL means only delete name entry */
1711 if (likely(cobj != NULL)) {
1712 mdd_cobj = md2mdd_obj(cobj);
1713 if (mdd_object_exists(mdd_cobj) == 0)
1717 rc = mdd_la_get(env, mdd_pobj, pattr);
1721 if (likely(mdd_cobj != NULL)) {
1723 rc = mdd_la_get(env, mdd_cobj, cattr);
1727 is_dir = S_ISDIR(cattr->la_mode);
1728 /* search for an existing archive.
1729 * we should check ahead as the object
1730 * can be destroyed in this transaction */
1731 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1732 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1735 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1739 handle = mdd_trans_create(env, mdd);
1741 RETURN(PTR_ERR(handle));
1743 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1744 lname, ma, handle, no_name, is_dir);
1748 rc = mdd_trans_start(env, mdd, handle);
1752 if (likely(mdd_cobj != NULL))
1753 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1755 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1756 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1761 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1762 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1763 GOTO(cleanup, rc = 0);
1765 if (likely(mdd_cobj != NULL)) {
1766 rc = mdo_ref_del(env, mdd_cobj, handle);
1768 __mdd_index_insert_only(env, mdd_pobj,
1769 mdd_object_fid(mdd_cobj),
1770 mdd_object_type(mdd_cobj),
1777 mdo_ref_del(env, mdd_cobj, handle);
1779 /* fetch updated nlink */
1780 rc = mdd_la_get(env, mdd_cobj, cattr);
1785 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1786 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1788 la->la_valid = LA_CTIME | LA_MTIME;
1789 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1793 /* Enough for only unlink the entry */
1794 if (unlikely(mdd_cobj == NULL))
1797 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1798 /* update ctime of an unlinked file only if it is still
1799 * opened or a link still exists */
1800 la->la_valid = LA_CTIME;
1801 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1806 /* XXX: this transfer to ma will be removed with LOD/OSP */
1807 ma->ma_attr = *cattr;
1808 ma->ma_valid |= MA_INODE;
1809 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1813 /* fetch updated nlink */
1814 rc = mdd_la_get(env, mdd_cobj, cattr);
1815 /* if object is removed then we can't get its attrs,
1817 if (rc == -ENOENT) {
1818 cattr->la_nlink = 0;
1822 if (cattr->la_nlink == 0) {
1823 ma->ma_attr = *cattr;
1824 ma->ma_valid |= MA_INODE;
1829 if (likely(mdd_cobj != NULL))
1830 mdd_write_unlock(env, mdd_cobj);
1833 if (cattr->la_nlink == 0)
1834 cl_flags |= CLF_UNLINK_LAST;
1836 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1838 rc = mdd_changelog_ns_store(env, mdd,
1839 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1840 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1841 lname, NULL, handle);
1845 rc = mdd_trans_stop(env, mdd, rc, handle);
1851 * The permission has been checked when obj created, no need check again.
1853 static int mdd_cd_sanity_check(const struct lu_env *env,
1854 struct mdd_object *obj)
1859 if (!obj || mdd_is_dead_obj(obj))
1865 static int mdd_create_data(const struct lu_env *env,
1866 struct md_object *pobj,
1867 struct md_object *cobj,
1868 const struct md_op_spec *spec,
1871 struct mdd_device *mdd = mdo2mdd(cobj);
1872 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1873 struct mdd_object *son = md2mdd_obj(cobj);
1874 struct thandle *handle;
1875 const struct lu_buf *buf;
1876 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1877 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1881 rc = mdd_cd_sanity_check(env, son);
1885 if (!md_should_create(spec->sp_cr_flags))
1889 * there are following use cases for this function:
1890 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1891 * striping can be specified or not
1894 rc = mdd_la_get(env, son, attr);
1898 /* calling ->ah_make_hint() is used to transfer information from parent */
1899 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1901 handle = mdd_trans_create(env, mdd);
1903 GOTO(out_free, rc = PTR_ERR(handle));
1906 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1907 * Should this be fixed?
1909 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1910 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1911 spec->sp_cr_flags, spec->no_create);
1913 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1914 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1915 spec->u.sp_ea.eadatalen);
1920 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1921 XATTR_NAME_LOV, 0, handle);
1925 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1930 rc = mdd_trans_start(env, mdd, handle);
1934 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1940 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1943 rc = mdd_trans_stop(env, mdd, rc, handle);
1949 static int mdd_declare_object_initialize(const struct lu_env *env,
1950 struct mdd_object *parent,
1951 struct mdd_object *child,
1952 const struct lu_attr *attr,
1953 struct thandle *handle)
1958 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1959 if (!S_ISDIR(attr->la_mode))
1962 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1963 S_IFDIR, dot, handle);
1967 rc = mdo_declare_ref_add(env, child, handle);
1971 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1972 S_IFDIR, dotdot, handle);
1977 static int mdd_object_initialize(const struct lu_env *env,
1978 const struct lu_fid *pfid,
1979 struct mdd_object *child,
1980 struct lu_attr *attr,
1981 struct thandle *handle)
1986 if (S_ISDIR(attr->la_mode)) {
1987 /* Add "." and ".." for newly created dir */
1988 mdo_ref_add(env, child, handle);
1989 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1990 S_IFDIR, dot, handle);
1992 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1995 mdo_ref_del(env, child, handle);
2002 * This function checks whether it can create a file/dir under the
2003 * directory(@pobj). The directory(@pobj) is not being locked by
2006 * \param[in] env execution environment
2007 * \param[in] pobj the directory to create files
2008 * \param[in] pattr the attributes of the directory
2009 * \param[in] lname the name of the created file/dir
2010 * \param[in] cattr the attributes of the file/dir
2011 * \param[in] spec create specification
2013 * \retval = 0 it is allowed to create file/dir under
2015 * \retval negative error not allowed to create file/dir
2016 * under the directory
2018 static int mdd_create_sanity_check(const struct lu_env *env,
2019 struct md_object *pobj,
2020 const struct lu_attr *pattr,
2021 const struct lu_name *lname,
2022 struct lu_attr *cattr,
2023 struct md_op_spec *spec)
2025 struct mdd_thread_info *info = mdd_env_info(env);
2026 struct lu_fid *fid = &info->mti_fid;
2027 struct mdd_object *obj = md2mdd_obj(pobj);
2028 struct mdd_device *m = mdo2mdd(pobj);
2029 bool check_perm = true;
2034 if (mdd_is_dead_obj(obj))
2038 * In some cases this lookup is not needed - we know before if name
2039 * exists or not because MDT performs lookup for it.
2040 * name length check is done in lookup.
2042 if (spec->sp_cr_lookup) {
2044 * Check if the name already exist, though it will be checked in
2045 * _index_insert also, for avoiding rolling back if exists
2048 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2049 MAY_WRITE | MAY_EXEC);
2051 RETURN(rc ? : -EEXIST);
2053 /* Permission is already being checked in mdd_lookup */
2057 if (S_ISDIR(cattr->la_mode) &&
2058 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2059 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2060 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2062 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2063 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2065 CERROR("%s: invalid lmv_user_md: magic = %x, "
2066 "stripe_offset = %d, stripe_count = %u: "
2067 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2068 le32_to_cpu(lum->lum_magic),
2069 (int)le32_to_cpu(lum->lum_stripe_offset),
2070 le32_to_cpu(lum->lum_stripe_count), rc);
2075 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2080 if (pattr->la_mode & S_ISGID) {
2081 struct lu_ucred *uc = lu_ucred(env);
2083 cattr->la_gid = pattr->la_gid;
2085 /* Directories are special, and always inherit S_ISGID */
2086 if (S_ISDIR(cattr->la_mode)) {
2087 cattr->la_mode |= S_ISGID;
2088 cattr->la_valid |= LA_MODE;
2089 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2090 == (S_ISGID | S_IXGRP) &&
2091 !lustre_in_group_p(uc,
2092 (cattr->la_valid & LA_GID) ?
2093 cattr->la_gid : pattr->la_gid) &&
2094 !md_capable(uc, CFS_CAP_FSETID)) {
2095 cattr->la_mode &= ~S_ISGID;
2096 cattr->la_valid |= LA_MODE;
2100 /* Inherit project ID from parent directory */
2101 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2102 cattr->la_projid = pattr->la_projid;
2103 if (S_ISDIR(cattr->la_mode)) {
2104 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2105 cattr->la_valid |= LA_FLAGS;
2107 cattr->la_valid |= LA_PROJID;
2110 rc = mdd_name_check(m, lname);
2114 switch (cattr->la_mode & S_IFMT) {
2116 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2118 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2119 RETURN(-ENAMETOOLONG);
2138 static int mdd_declare_create_object(const struct lu_env *env,
2139 struct mdd_device *mdd,
2140 struct mdd_object *p, struct mdd_object *c,
2141 struct lu_attr *attr,
2142 struct thandle *handle,
2143 const struct md_op_spec *spec,
2144 struct lu_buf *def_acl_buf,
2145 struct lu_buf *acl_buf,
2146 struct lu_buf *hsm_buf,
2147 struct dt_allocation_hint *hint)
2149 const struct lu_buf *buf;
2152 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2157 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2158 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2159 /* if dir, then can inherit default ACl */
2160 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2161 XATTR_NAME_ACL_DEFAULT,
2167 if (acl_buf && acl_buf->lb_len > 0) {
2168 rc = mdo_declare_attr_set(env, c, attr, handle);
2172 rc = mdo_declare_xattr_set(env, c, acl_buf,
2173 XATTR_NAME_ACL_ACCESS, 0, handle);
2178 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2182 /* replay case, create LOV EA from client data */
2183 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2184 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2185 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2186 spec->u.sp_ea.eadatalen);
2187 rc = mdo_declare_xattr_set(env, c, buf,
2188 S_ISDIR(attr->la_mode) ?
2189 XATTR_NAME_LMV : XATTR_NAME_LOV,
2190 LU_XATTR_CREATE, handle);
2194 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2195 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2203 if (S_ISLNK(attr->la_mode)) {
2204 const char *target_name = spec->u.sp_symname;
2205 int sym_len = strlen(target_name);
2206 const struct lu_buf *buf;
2208 buf = mdd_buf_get_const(env, target_name, sym_len);
2209 rc = dt_declare_record_write(env, mdd_object_child(c),
2215 if (spec->sp_cr_file_secctx_name != NULL) {
2216 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2217 spec->sp_cr_file_secctx_size);
2218 rc = mdo_declare_xattr_set(env, c, buf,
2219 spec->sp_cr_file_secctx_name, 0,
2225 if (spec->sp_cr_file_encctx != NULL) {
2226 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2227 spec->sp_cr_file_encctx_size);
2228 rc = mdo_declare_xattr_set(env, c, buf,
2229 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2238 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2239 struct mdd_object *p, struct mdd_object *c,
2240 const struct lu_name *name,
2241 struct lu_attr *attr,
2242 struct thandle *handle,
2243 const struct md_op_spec *spec,
2244 struct linkea_data *ldata,
2245 struct lu_buf *def_acl_buf,
2246 struct lu_buf *acl_buf,
2247 struct lu_buf *hsm_buf,
2248 struct dt_allocation_hint *hint)
2252 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2253 def_acl_buf, acl_buf, hsm_buf, hint);
2257 if (S_ISDIR(attr->la_mode)) {
2258 rc = mdo_declare_ref_add(env, p, handle);
2263 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2264 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2268 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2269 enum changelog_rec_type type;
2271 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2272 attr->la_mode, name->ln_name,
2277 rc = mdd_declare_links_add(env, c, handle, ldata);
2282 la->la_valid = LA_CTIME | LA_MTIME;
2283 rc = mdo_declare_attr_set(env, p, la, handle);
2287 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2288 S_ISREG(attr->la_mode) ? CL_CREATE :
2289 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2291 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2300 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2301 struct lu_attr *la, struct lu_buf *def_acl_buf,
2302 struct lu_buf *acl_buf)
2307 if (S_ISLNK(la->la_mode)) {
2308 acl_buf->lb_len = 0;
2309 def_acl_buf->lb_len = 0;
2313 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2314 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2315 XATTR_NAME_ACL_DEFAULT);
2316 mdd_read_unlock(env, pobj);
2318 /* If there are default ACL, fix mode/ACL by default ACL */
2319 def_acl_buf->lb_len = rc;
2320 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2321 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2322 acl_buf->lb_len = rc;
2323 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2326 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2327 /* If there are no default ACL, fix mode by mask */
2328 struct lu_ucred *uc = lu_ucred(env);
2330 /* The create triggered by MDT internal events, such as
2331 * LFSCK reset, will not contain valid "uc". */
2332 if (unlikely(uc != NULL))
2333 la->la_mode &= ~uc->uc_umask;
2335 acl_buf->lb_len = 0;
2336 def_acl_buf->lb_len = 0;
2343 * Create a metadata object and initialize it, set acl, xattr.
2345 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2346 struct mdd_object *son, struct lu_attr *attr,
2347 struct md_op_spec *spec, struct lu_buf *acl_buf,
2348 struct lu_buf *def_acl_buf,
2349 struct lu_buf *hsm_buf,
2350 struct dt_allocation_hint *hint,
2351 struct thandle *handle, bool initsecctx)
2353 const struct lu_buf *buf;
2356 mdd_write_lock(env, son, DT_TGT_CHILD);
2357 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2362 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2363 * created in declare phase, they also needs to be added to master
2364 * object as sub-directory entry. So it has to initialize the master
2365 * object, then set dir striped EA.(in mdo_xattr_set) */
2366 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2369 GOTO(err_destroy, rc);
2372 * in case of replay we just set LOVEA provided by the client
2373 * XXX: I think it would be interesting to try "old" way where
2374 * MDT calls this xattr_set(LOV) in a different transaction.
2375 * probably this way we code can be made better.
2378 /* During creation, there are only a few cases we need do xattr_set to
2380 * 1. regular file: see comments above.
2381 * 2. dir: inherit default striping or pool settings from parent.
2382 * 3. create striped directory with provided stripeEA.
2383 * 4. create striped directory because inherit default layout from the
2386 if (spec->no_create ||
2387 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2388 S_ISDIR(attr->la_mode)) {
2389 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2390 spec->u.sp_ea.eadatalen);
2391 rc = mdo_xattr_set(env, son, buf,
2392 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2394 LU_XATTR_CREATE, handle);
2396 GOTO(err_destroy, rc);
2399 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2402 memset(&mh, 0, sizeof(mh));
2403 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2404 mh.mh_arch_id = spec->sp_archive_id;
2405 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2406 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2409 GOTO(err_destroy, rc);
2412 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2413 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2414 S_ISDIR(attr->la_mode)) {
2415 /* set default acl */
2416 rc = mdo_xattr_set(env, son, def_acl_buf,
2417 XATTR_NAME_ACL_DEFAULT, 0,
2420 GOTO(err_destroy, rc);
2422 /* set its own acl */
2423 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2424 rc = mdo_xattr_set(env, son, acl_buf,
2425 XATTR_NAME_ACL_ACCESS,
2428 GOTO(err_destroy, rc);
2432 if (S_ISLNK(attr->la_mode)) {
2433 struct dt_object *dt = mdd_object_child(son);
2434 const char *target_name = spec->u.sp_symname;
2435 int sym_len = strlen(target_name);
2438 buf = mdd_buf_get_const(env, target_name, sym_len);
2439 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2443 GOTO(err_initlized, rc = -EFAULT);
2446 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2447 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2448 spec->sp_cr_file_secctx_size);
2449 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2452 GOTO(err_initlized, rc);
2455 if (spec->sp_cr_file_encctx != NULL) {
2456 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2457 spec->sp_cr_file_encctx_size);
2458 rc = mdo_xattr_set(env, son, buf,
2459 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2462 GOTO(err_initlized, rc);
2466 if (unlikely(rc != 0)) {
2468 if (S_ISDIR(attr->la_mode)) {
2469 /* Drop the reference, no need to delete "."/"..",
2470 * because the object to be destroied directly. */
2471 rc2 = mdo_ref_del(env, son, handle);
2475 rc2 = mdo_ref_del(env, son, handle);
2479 mdo_destroy(env, son, handle);
2482 mdd_write_unlock(env, son);
2486 static int mdd_index_delete(const struct lu_env *env,
2487 struct mdd_object *mdd_pobj,
2488 struct lu_attr *cattr,
2489 const struct lu_name *lname)
2491 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2492 struct thandle *handle;
2496 handle = mdd_trans_create(env, mdd);
2498 RETURN(PTR_ERR(handle));
2500 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2505 if (S_ISDIR(cattr->la_mode)) {
2506 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2511 /* Since this will only be used in the error handler path,
2512 * Let's set the thandle to be local and not mess the transno */
2513 handle->th_local = 1;
2514 rc = mdd_trans_start(env, mdd, handle);
2518 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2519 S_ISDIR(cattr->la_mode), handle);
2523 rc = mdd_trans_stop(env, mdd, rc, handle);
2529 * Create object and insert it into namespace.
2531 * Two operations have to be performed:
2533 * - an allocation of a new object (->do_create()), and
2534 * - an insertion into a parent index (->dio_insert()).
2536 * Due to locking, operation order is not important, when both are
2537 * successful, *but* error handling cases are quite different:
2539 * - if insertion is done first, and following object creation fails,
2540 * insertion has to be rolled back, but this operation might fail
2541 * also leaving us with dangling index entry.
2543 * - if creation is done first, is has to be undone if insertion fails,
2544 * leaving us with leaked space, which is not good but not fatal.
2546 * It seems that creation-first is simplest solution, but it is sub-optimal
2552 * case, because second mkdir is bound to create object, only to
2553 * destroy it immediately.
2555 * To avoid this follow local file systems that do double lookup:
2557 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2558 * 1. create (mdd_create_object_internal())
2559 * 2. insert (__mdd_index_insert(), lookup again)
2561 * \param[in] pobj parent object
2562 * \param[in] lname name of child being created
2563 * \param[in,out] child child object being created
2564 * \param[in] spec additional create parameters
2565 * \param[in] ma attributes for new child object
2567 * \retval 0 on success
2568 * \retval negative errno on failure
2570 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2571 const struct lu_name *lname, struct md_object *child,
2572 struct md_op_spec *spec, struct md_attr *ma)
2574 struct mdd_thread_info *info = mdd_env_info(env);
2575 struct lu_attr *la = &info->mti_la_for_fix;
2576 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2577 struct mdd_object *son = md2mdd_obj(child);
2578 struct mdd_device *mdd = mdo2mdd(pobj);
2579 struct lu_attr *attr = &ma->ma_attr;
2580 struct thandle *handle;
2581 struct lu_attr *pattr = &info->mti_pattr;
2582 struct lu_buf acl_buf;
2583 struct lu_buf def_acl_buf;
2584 struct lu_buf hsm_buf;
2585 struct linkea_data *ldata = &info->mti_link_data;
2586 const char *name = lname->ln_name;
2587 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2592 rc = mdd_la_get(env, mdd_pobj, pattr);
2596 /* Sanity checks before big job. */
2597 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2601 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2602 GOTO(out_free, rc = -EINPROGRESS);
2604 handle = mdd_trans_create(env, mdd);
2606 GOTO(out_free, rc = PTR_ERR(handle));
2608 lu_buf_check_and_alloc(&info->mti_xattr_buf,
2609 min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2611 acl_buf = info->mti_xattr_buf;
2612 def_acl_buf.lb_buf = info->mti_key;
2613 def_acl_buf.lb_len = sizeof(info->mti_key);
2614 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2618 if (S_ISDIR(attr->la_mode)) {
2619 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2622 * migrate may create 1-stripe directory, so lod_ah_init()
2623 * doesn't adjust stripe count from lmu.
2625 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2626 lmu->lum_stripe_count = 0;
2629 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2631 memset(ldata, 0, sizeof(*ldata));
2632 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2633 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2636 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2637 &tfid, lname, 1, 0, ldata);
2639 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2640 mdd_object_fid(mdd_pobj),
2641 lname, 1, 0, ldata);
2644 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2645 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2647 memset(&hsm_buf, 0, sizeof(hsm_buf));
2648 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2649 if (hsm_buf.lb_buf == NULL)
2650 GOTO(out_stop, rc = -ENOMEM);
2653 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2654 handle, spec, ldata, &def_acl_buf, &acl_buf,
2659 rc = mdd_trans_start(env, mdd, handle);
2663 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2664 &def_acl_buf, &hsm_buf, hint, handle, true);
2668 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2669 mdd_write_lock(env, son, DT_TGT_CHILD);
2670 son->mod_flags |= VOLATILE_OBJ;
2671 rc = mdd_orphan_insert(env, son, handle);
2672 GOTO(out_volatile, rc);
2674 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2675 attr->la_mode, name, handle);
2677 GOTO(err_created, rc);
2679 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2682 /* update parent directory mtime/ctime */
2684 la->la_valid = LA_CTIME | LA_MTIME;
2685 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2687 GOTO(err_insert, rc);
2693 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2694 rc2 = mdd_orphan_delete(env, son, handle);
2696 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2697 S_ISDIR(attr->la_mode),
2703 mdd_write_lock(env, son, DT_TGT_CHILD);
2704 if (S_ISDIR(attr->la_mode)) {
2705 /* Drop the reference, no need to delete "."/"..",
2706 * because the object is to be destroyed directly. */
2707 rc2 = mdo_ref_del(env, son, handle);
2709 mdd_write_unlock(env, son);
2714 /* For volatile files drop one link immediately, since there is
2715 * no filename in the namespace, and save any error returned. */
2716 rc2 = mdo_ref_del(env, son, handle);
2718 mdd_write_unlock(env, son);
2719 if (unlikely(rc == 0))
2724 /* Don't destroy the volatile object on success */
2725 if (likely(rc != 0))
2726 mdo_destroy(env, son, handle);
2727 mdd_write_unlock(env, son);
2730 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2731 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2732 rc = mdd_changelog_ns_store(env, mdd,
2733 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2734 S_ISREG(attr->la_mode) ? CL_CREATE :
2735 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2736 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2737 lname, NULL, handle);
2739 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2741 /* If creation fails, it is most likely due to the remote update
2742 * failure, because local transaction will mostly succeed at
2743 * this stage. There is no easy way to rollback all of previous
2744 * updates, so let's remove the object from namespace, and
2745 * LFSCK should handle the orphan object. */
2746 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2747 mdd_index_delete(env, mdd_pobj, attr, lname);
2751 if (is_vmalloc_addr(ldata->ld_buf))
2752 /* if we vmalloced a large buffer drop it */
2753 lu_buf_free(ldata->ld_buf);
2755 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2756 lu_buf_free(&hsm_buf);
2758 /* The child object shouldn't be cached anymore */
2760 set_bit(LU_OBJECT_HEARD_BANSHEE,
2761 &child->mo_lu.lo_header->loh_flags);
2766 * Get locks on parents in proper order
2767 * RETURN: < 0 - error, rename_order if successful
2775 static int mdd_rename_order(const struct lu_env *env,
2776 struct mdd_device *mdd,
2777 struct mdd_object *src_pobj,
2778 const struct lu_attr *pattr,
2779 struct mdd_object *tgt_pobj)
2781 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2785 if (src_pobj == tgt_pobj)
2786 RETURN(MDD_RN_SAME);
2788 /* compared the parent child relationship of src_p & tgt_p */
2789 if (lu_fid_eq(&mdd->mdd_root_fid, mdd_object_fid(src_pobj))) {
2791 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdd_object_fid(tgt_pobj))) {
2794 rc = mdd_is_parent(env, mdd, src_pobj, pattr,
2795 mdd_object_fid(tgt_pobj));
2808 /* has not mdd_write{read}_lock on any obj yet. */
2809 static int mdd_rename_sanity_check(const struct lu_env *env,
2810 struct mdd_object *src_pobj,
2811 const struct lu_attr *pattr,
2812 struct mdd_object *tgt_pobj,
2813 const struct lu_attr *tpattr,
2814 struct mdd_object *sobj,
2815 const struct lu_attr *cattr,
2816 struct mdd_object *tobj,
2817 const struct lu_attr *tattr)
2822 /* XXX: when get here, sobj must NOT be NULL,
2823 * the other case has been processed in cld_rename
2824 * before mdd_rename and enable MDS_PERM_BYPASS. */
2828 * If we are using project inheritance, we only allow renames
2829 * into our tree when the project IDs are the same; otherwise
2830 * tree quota mechanism would be circumvented.
2832 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2833 tpattr->la_projid != cattr->la_projid) ||
2834 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2835 (pattr->la_projid != tpattr->la_projid)))
2838 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2842 /* XXX: when get here, "tobj == NULL" means tobj must
2843 * NOT exist (neither on remote MDS, such case has been
2844 * processed in cld_rename before mdd_rename and enable
2846 * So check may_create, but not check may_unlink. */
2848 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2849 (src_pobj != tgt_pobj));
2851 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2852 (src_pobj != tgt_pobj), 1);
2854 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2855 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2860 static int mdd_declare_rename(const struct lu_env *env,
2861 struct mdd_device *mdd,
2862 struct mdd_object *mdd_spobj,
2863 struct mdd_object *mdd_tpobj,
2864 struct mdd_object *mdd_sobj,
2865 struct mdd_object *mdd_tobj,
2866 const struct lu_name *sname,
2867 const struct lu_name *tname,
2869 struct linkea_data *ldata,
2870 struct thandle *handle)
2872 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2875 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2876 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2882 /* name from source dir */
2883 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2887 /* .. from source child */
2888 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2889 /* source child can be directory, count by source dir's nlink */
2890 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2893 if (mdd_spobj != mdd_tpobj) {
2894 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2899 rc = mdo_declare_index_insert(env, mdd_sobj,
2900 mdd_object_fid(mdd_tpobj),
2901 S_IFDIR, dotdot, handle);
2906 /* new target child can be directory,
2907 * counted by target dir's nlink */
2908 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2913 la->la_valid = LA_CTIME | LA_MTIME;
2914 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2918 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2922 la->la_valid = LA_CTIME;
2923 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2927 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2932 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2933 mdd_object_type(mdd_sobj),
2934 tname->ln_name, handle);
2938 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2939 /* delete target child in target parent directory */
2940 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2945 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2949 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2950 /* target child can be directory,
2951 * delete "." reference in target child directory */
2952 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2956 /* delete ".." reference in target parent directory */
2957 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2962 la->la_valid = LA_CTIME;
2963 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2967 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2972 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2980 /* src object can be remote that is why we use only fid and type of object */
2981 static int mdd_rename(const struct lu_env *env,
2982 struct md_object *src_pobj, struct md_object *tgt_pobj,
2983 const struct lu_fid *lf, const struct lu_name *lsname,
2984 struct md_object *tobj, const struct lu_name *ltname,
2987 const char *sname = lsname->ln_name;
2988 const char *tname = ltname->ln_name;
2989 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2990 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2991 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2992 struct mdd_device *mdd = mdo2mdd(src_pobj);
2993 struct mdd_object *mdd_sobj = NULL; /* source object */
2994 struct mdd_object *mdd_tobj = NULL;
2995 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2996 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2997 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2998 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2999 struct thandle *handle;
3000 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3001 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3002 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3005 bool tobj_locked = 0;
3006 unsigned cl_flags = 0;
3010 /* let unlink to complete and commit */
3011 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3014 mdd_tobj = md2mdd_obj(tobj);
3016 mdd_sobj = mdd_object_find(env, mdd, lf);
3017 if (IS_ERR(mdd_sobj))
3018 RETURN(PTR_ERR(mdd_sobj));
3020 rc = mdd_la_get(env, mdd_sobj, cattr);
3022 GOTO(out_pending, rc);
3024 rc = mdd_la_get(env, mdd_spobj, pattr);
3026 GOTO(out_pending, rc);
3029 rc = mdd_la_get(env, mdd_tobj, tattr);
3031 GOTO(out_pending, rc);
3032 /* search for an existing archive.
3033 * we should check ahead as the object
3034 * can be destroyed in this transaction */
3035 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3036 cl_flags |= CLF_RENAME_LAST_EXISTS;
3039 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3041 GOTO(out_pending, rc);
3043 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3044 mdd_sobj, cattr, mdd_tobj, tattr);
3046 GOTO(out_pending, rc);
3048 rc = mdd_name_check(mdd, ltname);
3050 GOTO(out_pending, rc);
3052 /* FIXME: Should consider tobj and sobj too in rename_lock. */
3053 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
3055 GOTO(out_pending, rc);
3057 handle = mdd_trans_create(env, mdd);
3059 GOTO(out_pending, rc = PTR_ERR(handle));
3061 memset(ldata, 0, sizeof(*ldata));
3062 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3063 lsname, mdd_object_fid(mdd_tpobj), ltname,
3068 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3069 mdd_tobj, lsname, ltname, ma, ldata, handle);
3073 rc = mdd_trans_start(env, mdd, handle);
3077 is_dir = S_ISDIR(cattr->la_mode);
3079 /* Remove source name from source directory */
3080 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3084 /* "mv dir1 dir2" needs "dir1/.." link update */
3085 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3086 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3088 GOTO(fixup_spobj2, rc);
3090 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3093 GOTO(fixup_spobj, rc);
3096 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3097 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3099 /* tname might been renamed to something else */
3100 GOTO(fixup_spobj, rc);
3103 /* Insert new fid with target name into target dir */
3104 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3107 GOTO(fixup_tpobj, rc);
3109 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3110 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3112 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3113 la->la_valid = LA_CTIME;
3114 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3116 GOTO(fixup_tpobj, rc);
3118 /* Update the linkEA for the source object */
3119 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3120 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3121 lsname, mdd_object_fid(mdd_tpobj), ltname,
3122 handle, ldata, 0, 0);
3124 /* Old files might not have EA entry */
3125 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3126 lsname, handle, NULL, 0);
3127 mdd_write_unlock(env, mdd_sobj);
3128 /* We don't fail the transaction if the link ea can't be
3129 updated -- fid2path will use alternate lookup method. */
3132 /* Remove old target object
3133 * For tobj is remote case cmm layer has processed
3134 * and set tobj to NULL then. So when tobj is NOT NULL,
3135 * it must be local one.
3137 if (tobj && mdd_object_exists(mdd_tobj)) {
3138 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3140 if (mdd_is_dead_obj(mdd_tobj)) {
3141 /* shld not be dead, something is wrong */
3142 CERROR("tobj is dead, something is wrong\n");
3146 mdo_ref_del(env, mdd_tobj, handle);
3148 /* Remove dot reference. */
3149 if (S_ISDIR(tattr->la_mode))
3150 mdo_ref_del(env, mdd_tobj, handle);
3153 /* fetch updated nlink */
3154 rc = mdd_la_get(env, mdd_tobj, tattr);
3156 CERROR("%s: Failed to get nlink for tobj "
3158 mdd2obd_dev(mdd)->obd_name,
3159 PFID(tpobj_fid), rc);
3160 GOTO(fixup_tpobj, rc);
3163 la->la_valid = LA_CTIME;
3164 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3166 CERROR("%s: Failed to set ctime for tobj "
3168 mdd2obd_dev(mdd)->obd_name,
3169 PFID(tpobj_fid), rc);
3170 GOTO(fixup_tpobj, rc);
3173 /* XXX: this transfer to ma will be removed with LOD/OSP */
3174 ma->ma_attr = *tattr;
3175 ma->ma_valid |= MA_INODE;
3176 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3179 CERROR("%s: Failed to unlink tobj "
3181 mdd2obd_dev(mdd)->obd_name,
3182 PFID(tpobj_fid), rc);
3183 GOTO(fixup_tpobj, rc);
3186 /* fetch updated nlink */
3187 rc = mdd_la_get(env, mdd_tobj, tattr);
3188 if (rc == -ENOENT) {
3189 /* the object got removed, let's
3190 * return the latest known attributes */
3191 tattr->la_nlink = 0;
3193 } else if (rc != 0) {
3194 CERROR("%s: Failed to get nlink for tobj "
3196 mdd2obd_dev(mdd)->obd_name,
3197 PFID(tpobj_fid), rc);
3198 GOTO(fixup_tpobj, rc);
3200 /* XXX: this transfer to ma will be removed with LOD/OSP */
3201 ma->ma_attr = *tattr;
3202 ma->ma_valid |= MA_INODE;
3204 if (tattr->la_nlink == 0)
3205 cl_flags |= CLF_RENAME_LAST;
3207 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3210 la->la_valid = LA_CTIME | LA_MTIME;
3211 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3213 GOTO(fixup_tpobj, rc);
3215 if (mdd_spobj != mdd_tpobj) {
3216 la->la_valid = LA_CTIME | LA_MTIME;
3217 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3219 GOTO(fixup_tpobj, rc);
3226 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3228 CWARN("tp obj fix error %d\n",rc2);
3230 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3231 !mdd_is_dead_obj(mdd_tobj)) {
3233 mdo_ref_add(env, mdd_tobj, handle);
3235 mdo_ref_add(env, mdd_tobj, handle);
3238 rc2 = __mdd_index_insert(env, mdd_tpobj,
3239 mdd_object_fid(mdd_tobj),
3240 mdd_object_type(mdd_tobj),
3243 CWARN("tp obj fix error: rc = %d\n", rc2);
3248 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3249 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3251 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3252 mdd2obd_dev(mdd)->obd_name, rc2);
3255 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3258 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3259 mdd2obd_dev(mdd)->obd_name, rc2);
3264 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3265 mdd_object_type(mdd_sobj), sname,
3268 CWARN("sp obj fix error: rc = %d\n", rc2);
3273 mdd_write_unlock(env, mdd_tobj);
3276 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3277 mdd_tobj, tpobj_fid, lf, spobj_fid,
3278 ltname, lsname, handle);
3281 rc = mdd_trans_stop(env, mdd, rc, handle);
3284 mdd_object_put(env, mdd_sobj);
3289 * Check whether we should migrate the file/dir
3291 * < 0 permission check failed or other error.
3292 * = 0 the file can be migrated.
3294 static int mdd_migrate_sanity_check(const struct lu_env *env,
3295 struct mdd_device *mdd,
3296 struct mdd_object *spobj,
3297 struct mdd_object *tpobj,
3298 struct mdd_object *sobj,
3299 struct mdd_object *tobj,
3300 const struct lu_attr *spattr,
3301 const struct lu_attr *tpattr,
3302 const struct lu_attr *attr)
3308 if (!mdd_object_remote(sobj)) {
3309 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3310 if (sobj->mod_count > 0) {
3311 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3312 mdd_obj_dev_name(sobj),
3313 PFID(mdd_object_fid(sobj)),
3315 mdd_read_unlock(env, sobj);
3318 mdd_read_unlock(env, sobj);
3321 if (mdd_object_exists(tobj))
3324 rc = mdd_rename_sanity_check(env, spobj, spattr, tpobj, tpattr, sobj,
3329 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3330 struct mdd_object *obj,
3331 const struct lu_buf *buf,
3333 int fl, struct thandle *handle);
3335 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3336 static int mdd_iterate_xattrs(const struct lu_env *env,
3337 struct mdd_object *sobj,
3338 struct mdd_object *tobj,
3340 struct thandle *handle,
3343 struct mdd_thread_info *info = mdd_env_info(env);
3345 struct lu_buf list_xbuf;
3346 struct lu_buf cbxbuf;
3347 struct lu_buf xbuf = { NULL };
3356 /* retrieve xattr list from the old object */
3357 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3358 if (list_xsize == -ENODATA)
3364 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3365 if (info->mti_big_buf.lb_buf == NULL)
3368 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3369 list_xbuf.lb_len = list_xsize;
3370 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3376 xname = list_xbuf.lb_buf;
3378 xlen = strnlen(xname, rem - 1) + 1;
3379 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3380 strcmp(XATTR_NAME_LMV, xname) == 0)
3384 strcmp(XATTR_NAME_LINK, xname) == 0)
3387 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3388 if (xsize == -ENODATA)
3391 GOTO(out, rc = xsize);
3393 lu_buf_check_and_alloc(&xbuf, xsize);
3394 if (xbuf.lb_buf == NULL)
3395 GOTO(out, rc = -ENOMEM);
3397 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3404 cbxbuf.lb_len = xsize;
3406 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3407 if (unlikely(rc == -ENOSPC &&
3408 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3409 rc = linkea_overflow_shrink(
3410 (struct linkea_data *)(cbxbuf.lb_buf));
3411 if (likely(rc > 0)) {
3429 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3430 struct mdd_object *sobj,
3431 struct mdd_object *tobj,
3432 const struct lu_name *sname,
3433 const struct lu_fid *sfid,
3434 const struct lu_name *lname,
3435 const struct lu_fid *fid,
3437 struct thandle *handle);
3439 static int mdd_declare_update_link(const struct lu_env *env,
3440 struct mdd_object *sobj,
3441 struct mdd_object *tobj,
3442 const struct lu_name *tname,
3443 const struct lu_fid *tpfid,
3444 const struct lu_name *lname,
3445 const struct lu_fid *fid,
3447 struct thandle *handle)
3449 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3450 struct mdd_object *pobj;
3454 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3455 !strcmp(tname->ln_name, lname->ln_name))
3458 pobj = mdd_object_find(env, mdd, fid);
3460 return PTR_ERR(pobj);
3463 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3465 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3466 mdd_object_type(sobj),
3467 lname->ln_name, handle);
3468 mdd_object_put(env, pobj);
3472 rc = mdo_declare_ref_add(env, tobj, handle);
3476 rc = mdo_declare_ref_del(env, sobj, handle);
3480 static int mdd_update_link(const struct lu_env *env,
3481 struct mdd_object *sobj,
3482 struct mdd_object *tobj,
3483 const struct lu_name *tname,
3484 const struct lu_fid *tpfid,
3485 const struct lu_name *lname,
3486 const struct lu_fid *fid,
3488 struct thandle *handle)
3490 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3491 struct mdd_object *pobj;
3496 LASSERT(lu_name_is_valid(lname));
3499 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3500 !strncmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3503 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3504 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3506 pobj = mdd_object_find(env, mdd, fid);
3508 CWARN("%s: cannot find obj "DFID": %ld\n",
3509 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3510 RETURN(PTR_ERR(pobj));
3513 if (!mdd_object_exists(pobj)) {
3514 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3515 mdd_object_put(env, pobj);
3519 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3520 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3522 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3523 mdd_object_type(sobj),
3524 lname->ln_name, handle);
3525 mdd_write_unlock(env, pobj);
3526 mdd_object_put(env, pobj);
3530 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3531 rc = mdo_ref_add(env, tobj, handle);
3532 mdd_write_unlock(env, tobj);
3536 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3537 rc = mdo_ref_del(env, sobj, handle);
3538 mdd_write_unlock(env, sobj);
3543 static inline int mdd_fld_lookup(const struct lu_env *env,
3544 struct mdd_device *mdd,
3545 const struct lu_fid *fid,
3548 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
3549 struct seq_server_site *ss;
3552 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3554 range->lsr_flags = LU_SEQ_RANGE_MDT;
3555 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3559 *mdt_index = range->lsr_index;
3564 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3565 struct mdd_object *sobj,
3566 struct mdd_object *tobj,
3567 const struct lu_name *tname,
3568 const struct lu_fid *tpfid,
3569 const struct lu_name *lname,
3570 const struct lu_fid *fid,
3572 struct thandle *handle)
3574 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3575 __u32 source_mdt_index = *(__u32 *)opaque;
3576 __u32 link_mdt_index;
3582 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3583 !strcmp(tname->ln_name, lname->ln_name))
3586 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3590 RETURN(link_mdt_index == source_mdt_index);
3593 static int mdd_iterate_linkea(const struct lu_env *env,
3594 struct mdd_object *sobj,
3595 struct mdd_object *tobj,
3596 const struct lu_name *tname,
3597 const struct lu_fid *tpfid,
3598 struct linkea_data *ldata,
3600 struct thandle *handle,
3603 struct mdd_thread_info *info = mdd_env_info(env);
3604 char *filename = info->mti_name;
3605 struct lu_name lname;
3612 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3613 linkea_next_entry(ldata)) {
3614 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3617 /* Note: lname might miss \0 at the end */
3618 snprintf(filename, sizeof(info->mti_name), "%.*s",
3619 lname.ln_namelen, lname.ln_name);
3620 lname.ln_name = filename;
3622 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3624 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3632 * Prepare linkea, and check whether file needs migrate: if source still has
3633 * link on source MDT, no need to migrate, just update namespace on source and
3636 * \retval 0 do migrate
3637 * \retval 1 don't migrate
3638 * \retval -errno on failure
3640 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3641 struct mdd_device *mdd,
3642 struct mdd_object *spobj,
3643 struct mdd_object *tpobj,
3644 struct mdd_object *sobj,
3645 const struct lu_name *lname,
3646 const struct lu_attr *attr,
3647 struct linkea_data *ldata)
3649 __u32 source_mdt_index;
3654 memset(ldata, 0, sizeof(*ldata));
3655 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), lname,
3656 mdd_object_fid(tpobj), lname, 1, 0, ldata);
3661 * Then it will check if the file should be migrated. If the file has
3662 * mulitple links, we only need migrate the file if all of its entries
3663 * has been migrated to the remote MDT.
3665 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3668 /* If there are still links locally, don't migrate this file */
3669 LASSERT(ldata->ld_leh != NULL);
3672 * If linkEA is overflow, it means there are some unknown name entries
3673 * under unknown parents, which will prevent the migration.
3675 if (unlikely(ldata->ld_leh->leh_overflow_time))
3678 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3682 rc = mdd_iterate_linkea(env, sobj, NULL, lname, mdd_object_fid(tpobj),
3683 ldata, &source_mdt_index, NULL,
3684 mdd_is_link_on_source_mdt);
3688 static int mdd_declare_migrate_update(const struct lu_env *env,
3689 struct mdd_object *spobj,
3690 struct mdd_object *tpobj,
3691 struct mdd_object *obj,
3692 const struct lu_name *lname,
3693 struct lu_attr *attr,
3694 struct lu_attr *spattr,
3695 struct lu_attr *tpattr,
3696 struct linkea_data *ldata,
3698 struct thandle *handle)
3700 struct mdd_thread_info *info = mdd_env_info(env);
3701 struct lu_attr *la = &info->mti_la_for_fix;
3704 rc = mdo_declare_index_delete(env, spobj, lname->ln_name, handle);
3708 if (S_ISDIR(attr->la_mode)) {
3709 rc = mdo_declare_ref_del(env, spobj, handle);
3714 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3715 attr->la_mode & S_IFMT,
3716 lname->ln_name, handle);
3720 rc = mdd_declare_links_add(env, obj, handle, ldata);
3724 if (S_ISDIR(attr->la_mode)) {
3725 rc = mdo_declare_ref_add(env, tpobj, handle);
3730 la->la_valid = LA_CTIME | LA_MTIME;
3731 rc = mdo_declare_attr_set(env, spobj, la, handle);
3735 if (tpobj != spobj) {
3736 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3744 static int mdd_declare_migrate_create(const struct lu_env *env,
3745 struct mdd_object *spobj,
3746 struct mdd_object *tpobj,
3747 struct mdd_object *sobj,
3748 struct mdd_object *tobj,
3749 const struct lu_name *lname,
3750 struct lu_attr *spattr,
3751 struct lu_attr *tpattr,
3752 struct lu_attr *attr,
3753 struct lu_buf *sbuf,
3754 struct linkea_data *ldata,
3756 struct md_op_spec *spec,
3757 struct dt_allocation_hint *hint,
3758 struct thandle *handle)
3760 struct mdd_thread_info *info = mdd_env_info(env);
3761 struct md_layout_change *mlc = &info->mti_mlc;
3762 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3767 if (S_ISDIR(attr->la_mode)) {
3768 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3770 mlc->mlc_opc = MD_LAYOUT_DETACH;
3771 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3775 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3776 } else if (S_ISLNK(attr->la_mode)) {
3777 spec->u.sp_symname = sbuf->lb_buf;
3778 } else if (S_ISREG(attr->la_mode)) {
3779 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3780 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3783 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3785 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3786 lname, attr, handle, spec, ldata, NULL, NULL,
3792 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3793 * createb yet, copy from sobj.
3795 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3796 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3797 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3799 if (S_ISDIR(attr->la_mode)) {
3801 /* if sobj is not striped, fake a 1-stripe LMV */
3802 LASSERT(sizeof(info->mti_key) >
3803 lmv_mds_md_size(1, LMV_MAGIC_V1));
3804 lmv = (typeof(lmv))info->mti_key;
3805 memset(lmv, 0, sizeof(*lmv));
3806 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3807 lmv->lmv_stripe_count = cpu_to_le32(1);
3808 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3809 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3810 mdd_object_fid(sobj));
3811 mlc->mlc_buf.lb_buf = lmv;
3812 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3814 mlc->mlc_buf = *sbuf;
3816 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3817 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3822 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3823 mdo_declare_xattr_set);
3827 if (S_ISREG(attr->la_mode)) {
3828 struct lu_buf fid_buf;
3830 handle->th_complex = 1;
3832 /* target may be remote, update PFID via sobj. */
3833 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3834 fid_buf.lb_len = sizeof(struct lu_fid);
3835 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3840 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3845 if (!S_ISDIR(attr->la_mode)) {
3846 rc = mdd_iterate_linkea(env, sobj, tobj, lname,
3847 mdd_object_fid(tpobj), ldata, NULL,
3848 handle, mdd_declare_update_link);
3853 if (!S_ISDIR(attr->la_mode) || lmv) {
3854 rc = mdo_declare_ref_del(env, sobj, handle);
3858 if (S_ISDIR(attr->la_mode)) {
3859 rc = mdo_declare_ref_del(env, sobj, handle);
3864 rc = mdo_declare_destroy(env, sobj, handle);
3869 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, lname, attr,
3870 spattr, tpattr, ldata, ma, handle);
3875 * migrate dirent from \a spobj to \a tpobj.
3877 static int mdd_migrate_update(const struct lu_env *env,
3878 struct mdd_object *spobj,
3879 struct mdd_object *tpobj,
3880 struct mdd_object *obj,
3881 const struct lu_name *lname,
3882 struct lu_attr *attr,
3883 struct lu_attr *spattr,
3884 struct lu_attr *tpattr,
3885 struct linkea_data *ldata,
3887 struct thandle *handle)
3889 struct mdd_thread_info *info = mdd_env_info(env);
3890 struct lu_attr *la = &info->mti_la_for_fix;
3895 CDEBUG(D_INFO, "update "DFID"/%s to "DFID"/"DFID"\n",
3896 PFID(mdd_object_fid(spobj)), lname->ln_name,
3897 PFID(mdd_object_fid(tpobj)), PFID(mdd_object_fid(obj)));
3899 rc = __mdd_index_delete(env, spobj, lname->ln_name,
3900 S_ISDIR(attr->la_mode), handle);
3904 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3905 attr->la_mode & S_IFMT,
3906 lname->ln_name, handle);
3910 rc = mdd_links_write(env, obj, ldata, handle);
3914 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3915 la->la_valid = LA_CTIME | LA_MTIME;
3916 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3917 rc = mdd_update_time(env, spobj, spattr, la, handle);
3918 mdd_write_unlock(env, spobj);
3922 if (tpobj != spobj) {
3923 la->la_valid = LA_CTIME | LA_MTIME;
3924 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3925 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3926 mdd_write_unlock(env, tpobj);
3935 * Migrate file/dir to target MDT.
3937 * Create target according to \a spec, and then migrate xattrs, if it's
3938 * directory, migrate source stripes to target.
3940 * \param[in] env execution environment
3941 * \param[in] spobj source parent object
3942 * \param[in] tpobj target parent object
3943 * \param[in] sobj source object
3944 * \param[in] tobj target object
3945 * \param[in] lname file name
3946 * \param[in] spattr source parent attributes
3947 * \param[in] tpattr target parent attributes
3948 * \param[in] attr source attributes
3949 * \param[in] sbuf source LMV buf
3950 * \param[in] spec migrate create spec
3951 * \param[in] hint target creation hint
3952 * \param[in] handle tranasction handle
3954 * \retval 0 on success
3955 * \retval -errno on failure
3957 static int mdd_migrate_create(const struct lu_env *env,
3958 struct mdd_object *spobj,
3959 struct mdd_object *tpobj,
3960 struct mdd_object *sobj,
3961 struct mdd_object *tobj,
3962 const struct lu_name *lname,
3963 struct lu_attr *spattr,
3964 struct lu_attr *tpattr,
3965 struct lu_attr *attr,
3966 const struct lu_buf *sbuf,
3967 struct linkea_data *ldata,
3969 struct md_op_spec *spec,
3970 struct dt_allocation_hint *hint,
3971 struct thandle *handle)
3978 * migrate sobj stripes to tobj if it's directory:
3979 * 1. detach stripes from sobj.
3980 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
3981 * 3. create stripes for tobj, see lod_xattr_set_lmv().
3983 if (S_ISDIR(attr->la_mode)) {
3984 struct mdd_thread_info *info = mdd_env_info(env);
3985 struct md_layout_change *mlc = &info->mti_mlc;
3987 mlc->mlc_opc = MD_LAYOUT_DETACH;
3989 mdd_write_lock(env, sobj, DT_SRC_PARENT);
3990 rc = mdo_layout_change(env, sobj, mlc, handle);
3991 mdd_write_unlock(env, sobj);
3996 /* don't set nlink from sobj */
3997 attr->la_valid &= ~LA_NLINK;
3999 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4000 hint, handle, false);
4004 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4005 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4006 mdd_write_unlock(env, tobj);
4010 /* for regular file, update OST objects XATTR_NAME_FID */
4011 if (S_ISREG(attr->la_mode)) {
4012 struct lu_buf fid_buf;
4014 /* target may be remote, update PFID via sobj. */
4015 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4016 fid_buf.lb_len = sizeof(struct lu_fid);
4017 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4022 /* delete LOV to avoid deleting OST objs when destroying sobj */
4023 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4024 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4025 mdd_write_unlock(env, sobj);
4026 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4027 if (rc && rc != -ENODATA)
4032 /* update links FID */
4033 if (!S_ISDIR(attr->la_mode)) {
4034 rc = mdd_iterate_linkea(env, sobj, tobj, lname,
4035 mdd_object_fid(tpobj), ldata,
4036 NULL, handle, mdd_update_link);
4041 /* don't destroy sobj if it's plain directory */
4042 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4043 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4044 rc = mdo_ref_del(env, sobj, handle);
4046 if (S_ISDIR(attr->la_mode))
4047 rc = mdo_ref_del(env, sobj, handle);
4049 rc = mdo_destroy(env, sobj, handle);
4051 mdd_write_unlock(env, sobj);
4056 rc = mdd_migrate_update(env, spobj, tpobj, tobj, lname, attr,
4057 spattr, tpattr, ldata, ma, handle);
4062 /* NB: if user issued different migrate command, we can't adjust it silently
4063 * here, because this command will decide target MDT in subdir migration in
4066 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4067 const struct lmv_mds_md_v1 *lmv,
4068 const struct lmv_user_md_v1 *lum,
4069 const struct lu_name *lname)
4071 __u32 lum_stripe_count = lum->lum_stripe_count;
4072 __u32 lmv_hash_type = lmv->lmv_hash_type;
4073 char *mdt_hash_name[] = { "none",
4074 LMV_HASH_NAME_ALL_CHARS,
4075 LMV_HASH_NAME_FNV_1A_64,
4076 LMV_HASH_NAME_CRUSH,
4079 if (!lmv_is_sane(lmv))
4082 /* if stripe_count unspecified, set to 1 */
4083 if (!lum_stripe_count)
4084 lum_stripe_count = cpu_to_le32(1);
4086 lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4088 /* TODO: check specific MDTs */
4089 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4090 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4091 (lum->lum_hash_type && lum->lum_hash_type != lmv_hash_type)) {
4092 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4093 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4094 le32_to_cpu(lmv->lmv_master_mdt_index),
4095 le32_to_cpu(lmv->lmv_migrate_offset),
4096 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4105 * Migrate directory or file.
4107 * migrate source to target in following steps:
4108 * 1. create target, append source stripes after target's if it's directory,
4109 * migrate xattrs and update fid of source links.
4110 * 2. update namespace: migrate dirent from source parent to target parent,
4111 * update file linkea, and destroy source if it's not needed any more.
4113 * \param[in] env execution environment
4114 * \param[in] md_pobj parent master object
4115 * \param[in] md_sobj source object
4116 * \param[in] lname file name
4117 * \param[in] md_tobj target object
4118 * \param[in] spec target creation spec
4119 * \param[in] ma used to update \a pobj mtime and ctime
4121 * \retval 0 on success
4122 * \retval -errno on failure
4124 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4125 struct md_object *md_sobj, const struct lu_name *lname,
4126 struct md_object *md_tobj, struct md_op_spec *spec,
4129 struct mdd_device *mdd = mdo2mdd(md_pobj);
4130 struct mdd_thread_info *info = mdd_env_info(env);
4131 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4132 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4133 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4134 struct mdd_object *spobj = NULL;
4135 struct mdd_object *tpobj = NULL;
4136 struct lu_attr *spattr = &info->mti_pattr;
4137 struct lu_attr *tpattr = &info->mti_tpattr;
4138 struct lu_attr *attr = &info->mti_cattr;
4139 struct linkea_data *ldata = &info->mti_link_data;
4140 struct dt_allocation_hint *hint = &info->mti_hint;
4141 struct lu_fid *fid = &info->mti_fid2;
4142 struct lu_buf pbuf = { NULL };
4143 struct lu_buf sbuf = { NULL };
4144 struct lmv_mds_md_v1 *lmv;
4145 struct thandle *handle;
4146 bool nsonly = false;
4151 rc = mdd_la_get(env, sobj, attr);
4155 /* locate source and target stripe on pobj, which are the real parent */
4156 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4157 if (rc < 0 && rc != -ENODATA)
4164 if (!lmv_is_sane(lmv))
4165 GOTO(out, rc = -EBADF);
4167 /* locate target parent stripe */
4168 /* fail check here to make sure top dir migration succeed. */
4169 if (lmv_is_migrating(lmv) &&
4170 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4171 GOTO(out, rc = -EIO);
4173 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4176 GOTO(out, rc = index);
4178 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4179 tpobj = mdd_object_find(env, mdd, fid);
4181 GOTO(out, rc = PTR_ERR(tpobj));
4183 /* locate source parent stripe */
4184 if (lmv_is_layout_changing(lmv)) {
4185 index = lmv_name_to_stripe_index_old(lmv,
4189 GOTO(out, rc = index);
4191 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4192 spobj = mdd_object_find(env, mdd, fid);
4194 GOTO(out, rc = PTR_ERR(spobj));
4196 /* parent stripe unchanged */
4197 if (spobj == tpobj) {
4198 if (!lmv_is_restriping(lmv))
4199 GOTO(out, rc = -EINVAL);
4200 GOTO(out, rc = -EALREADY);
4202 nsonly = spec->sp_migrate_nsonly;
4205 mdd_object_get(spobj);
4210 mdd_object_get(tpobj);
4211 mdd_object_get(spobj);
4214 rc = mdd_la_get(env, spobj, spattr);
4218 rc = mdd_la_get(env, tpobj, tpattr);
4222 if (S_ISDIR(attr->la_mode) && !nsonly) {
4223 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4227 /* if user use default value '0' for stripe_count, we need to
4228 * adjust it to '1' to create a 1-stripe directory.
4230 if (lum->lum_stripe_count == 0)
4231 lum->lum_stripe_count = cpu_to_le32(1);
4233 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4234 if (rc && rc != -ENODATA)
4239 if (!lmv_is_sane(lmv))
4240 GOTO(out, rc = -EBADF);
4241 if (lmv_is_migrating(lmv)) {
4242 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4247 } else if (!S_ISDIR(attr->la_mode)) {
4249 GOTO(out, rc = -EALREADY);
4251 /* update namespace only if @sobj is on MDT where @tpobj is. */
4252 if (!mdd_object_remote(tpobj))
4255 if (S_ISLNK(attr->la_mode)) {
4256 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4258 GOTO(out, rc = -ENOMEM);
4260 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4263 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4264 mdd2obd_dev(mdd)->obd_name,
4265 PFID(mdd_object_fid(sobj)), rc);
4271 /* linkea needs update upon FID or parent stripe change */
4272 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, lname,
4275 /* update namespace only if @sobj has link on its MDT. */
4280 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4281 spattr, tpattr, attr);
4285 handle = mdd_trans_create(env, mdd);
4287 GOTO(out, rc = PTR_ERR(handle));
4290 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, lname,
4291 attr, spattr, tpattr, ldata, ma,
4294 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4295 lname, spattr, tpattr, attr,
4296 &sbuf, ldata, ma, spec, hint,
4299 GOTO(stop_trans, rc);
4301 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4304 GOTO(stop_trans, rc);
4306 rc = mdd_trans_start(env, mdd, handle);
4308 GOTO(stop_trans, rc);
4311 rc = mdd_migrate_update(env, spobj, tpobj, sobj, lname, attr,
4312 spattr, tpattr, ldata, ma, handle);
4314 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, lname,
4315 spattr, tpattr, attr, &sbuf, ldata, ma,
4316 spec, hint, handle);
4318 GOTO(stop_trans, rc);
4320 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4321 nsonly ? sobj : tobj, mdd_object_fid(spobj),
4322 mdd_object_fid(sobj), mdd_object_fid(tpobj),
4323 lname, lname, handle);
4325 GOTO(stop_trans, rc);
4329 rc = mdd_trans_stop(env, mdd, rc, handle);
4331 if (!IS_ERR_OR_NULL(spobj))
4332 mdd_object_put(env, spobj);
4333 if (!IS_ERR_OR_NULL(tpobj))
4334 mdd_object_put(env, tpobj);
4341 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4342 struct mdd_object *pobj,
4343 struct mdd_object *obj,
4344 struct mdd_object *stripe,
4345 struct lu_attr *attr,
4346 struct md_layout_change *mlc,
4347 struct lu_name *lname,
4348 struct thandle *handle)
4352 mlc->mlc_opc = MD_LAYOUT_DETACH;
4353 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4357 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4358 S_IFDIR, dotdot, handle);
4362 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4363 mdo_declare_xattr_set);
4367 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4371 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4375 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4379 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4380 attr->la_mode, lname->ln_name, handle);
4384 rc = mdo_declare_ref_del(env, obj, handle);
4388 rc = mdo_declare_ref_del(env, obj, handle);
4392 rc = mdo_declare_destroy(env, obj, handle);
4399 /* transform one-stripe directory to a plain directory */
4400 static int mdd_1sd_collapse(const struct lu_env *env,
4401 struct mdd_object *pobj,
4402 struct mdd_object *obj,
4403 struct mdd_object *stripe,
4404 struct lu_attr *attr,
4405 struct md_layout_change *mlc,
4406 struct lu_name *lname,
4407 struct thandle *handle)
4413 /* replace 1-stripe directory with its stripe */
4414 mlc->mlc_opc = MD_LAYOUT_DETACH;
4416 mdd_write_lock(env, obj, DT_SRC_PARENT);
4417 rc = mdo_layout_change(env, obj, mlc, handle);
4418 mdd_write_unlock(env, obj);
4422 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4423 mdd_write_lock(env, obj, DT_SRC_CHILD);
4425 /* insert dotdot to stripe which points to parent */
4426 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4427 S_IFDIR, dotdot, handle);
4431 /* copy xattrs including linkea */
4432 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4437 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4441 /* don't set nlink from parent */
4442 attr->la_valid &= ~LA_NLINK;
4444 rc = mdo_attr_set(env, stripe, attr, handle);
4448 /* delete dir name from parent */
4449 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4453 /* insert stripe to parent with dir name */
4454 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4455 attr->la_mode, lname->ln_name, handle);
4459 /* destroy dir obj */
4460 rc = mdo_ref_del(env, obj, handle);
4464 rc = mdo_ref_del(env, obj, handle);
4468 rc = mdo_destroy(env, obj, handle);
4474 mdd_write_unlock(env, obj);
4475 mdd_write_unlock(env, pobj);
4481 * shrink directory stripes after migration/merge
4483 int mdd_dir_layout_shrink(const struct lu_env *env,
4484 struct md_object *md_obj,
4485 struct md_layout_change *mlc)
4487 struct mdd_device *mdd = mdo2mdd(md_obj);
4488 struct mdd_thread_info *info = mdd_env_info(env);
4489 struct mdd_object *obj = md2mdd_obj(md_obj);
4490 struct mdd_object *pobj = NULL;
4491 struct mdd_object *stripe = NULL;
4492 struct lu_attr *attr = &info->mti_pattr;
4493 struct lu_fid *fid = &info->mti_fid2;
4494 struct lu_name lname = { NULL };
4495 struct lu_buf lmv_buf = { NULL };
4496 struct lmv_mds_md_v1 *lmv;
4497 struct lmv_user_md *lmu;
4498 struct thandle *handle;
4503 rc = mdd_la_get(env, obj, attr);
4507 if (!S_ISDIR(attr->la_mode))
4510 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4514 lmv = lmv_buf.lb_buf;
4515 if (!lmv_is_sane(lmv))
4518 lmu = mlc->mlc_buf.lb_buf;
4520 /* adjust the default value '0' to '1' */
4521 if (lmu->lum_stripe_count == 0)
4522 lmu->lum_stripe_count = cpu_to_le32(1);
4524 /* these were checked in MDT */
4525 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4526 le32_to_cpu(lmv->lmv_stripe_count));
4527 LASSERT(!lmv_is_splitting(lmv));
4528 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4530 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4531 * to a plain dir, which will cause FID change and namespace update.
4533 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4534 struct linkea_data *ldata = &info->mti_link_data;
4535 char *filename = info->mti_name;
4537 rc = mdd_links_read(env, obj, ldata);
4541 if (ldata->ld_leh->leh_reccount > 1)
4542 GOTO(out, rc = -EINVAL);
4544 linkea_first_entry(ldata);
4546 GOTO(out, rc = -ENODATA);
4548 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4551 /* Note: lname might miss \0 at the end */
4552 snprintf(filename, sizeof(info->mti_name), "%.*s",
4553 lname.ln_namelen, lname.ln_name);
4554 lname.ln_name = filename;
4556 pobj = mdd_object_find(env, mdd, fid);
4563 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4565 stripe = mdd_object_find(env, mdd, fid);
4566 if (IS_ERR(stripe)) {
4567 mdd_object_put(env, pobj);
4569 GOTO(out, rc = PTR_ERR(stripe));
4573 handle = mdd_trans_create(env, mdd);
4575 GOTO(out, rc = PTR_ERR(handle));
4577 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4578 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4580 GOTO(stop_trans, rc);
4582 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4583 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4586 GOTO(stop_trans, rc);
4589 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4592 GOTO(stop_trans, rc);
4594 rc = mdd_trans_start(env, mdd, handle);
4596 GOTO(stop_trans, rc);
4598 mdd_write_lock(env, obj, DT_SRC_PARENT);
4599 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4600 rc = mdo_layout_change(env, obj, mlc, handle);
4601 mdd_write_unlock(env, obj);
4603 GOTO(stop_trans, rc);
4605 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4606 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4609 GOTO(stop_trans, rc);
4612 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4613 XATTR_NAME_LMV, handle);
4614 GOTO(stop_trans, rc);
4617 rc = mdd_trans_stop(env, mdd, rc, handle);
4620 mdd_object_put(env, stripe);
4621 mdd_object_put(env, pobj);
4623 lu_buf_free(&lmv_buf);
4627 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4628 struct mdd_device *mdd,
4629 struct mdd_object *pobj,
4630 struct mdd_object *obj,
4631 struct mdd_object *tobj,
4632 struct md_layout_change *mlc,
4633 struct dt_allocation_hint *hint,
4634 struct thandle *handle)
4636 struct mdd_thread_info *info = mdd_env_info(env);
4637 const struct lu_name *lname = mlc->mlc_name;
4638 struct lu_attr *la = &info->mti_la_for_fix;
4639 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4640 struct linkea_data *ldata = &info->mti_link_data;
4641 struct lmv_mds_md_v1 *lmv;
4645 mlc->mlc_opc = MD_LAYOUT_DETACH;
4646 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4650 memset(ldata, 0, sizeof(*ldata));
4651 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4652 lname, 1, 0, ldata);
4656 count = lum->lum_stripe_count;
4657 lum->lum_stripe_count = 0;
4658 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4660 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4661 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4662 ldata, NULL, NULL, NULL, hint);
4666 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4669 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4671 lmv = (typeof(lmv))info->mti_key;
4672 memset(lmv, 0, sizeof(*lmv));
4673 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4674 lmv->lmv_stripe_count = cpu_to_le32(1);
4675 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4676 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4678 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4679 mlc->mlc_buf.lb_buf = lmv;
4680 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4681 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4685 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4686 mdo_declare_xattr_set);
4690 lum->lum_stripe_count = count;
4691 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4692 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4696 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4700 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4701 S_IFDIR, lname->ln_name, handle);
4705 la->la_valid = LA_CTIME | LA_MTIME;
4706 rc = mdo_declare_attr_set(env, obj, la, handle);
4710 rc = mdo_declare_attr_set(env, pobj, la, handle);
4714 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4720 * plain directory split:
4721 * 1. create \a tobj as plain directory.
4722 * 2. append \a obj as first stripe of \a tobj.
4723 * 3. migrate xattrs from \a obj to \a tobj.
4724 * 4. split \a tobj to specific stripe count.
4726 static int mdd_dir_split_plain(const struct lu_env *env,
4727 struct mdd_device *mdd,
4728 struct mdd_object *pobj,
4729 struct mdd_object *obj,
4730 struct mdd_object *tobj,
4731 struct md_layout_change *mlc,
4732 struct dt_allocation_hint *hint,
4733 struct thandle *handle)
4735 struct mdd_thread_info *info = mdd_env_info(env);
4736 struct lu_attr *pattr = &info->mti_pattr;
4737 struct lu_attr *la = &info->mti_la_for_fix;
4738 const struct lu_name *lname = mlc->mlc_name;
4739 struct linkea_data *ldata = &info->mti_link_data;
4744 /* copy linkea out and set on target later */
4745 rc = mdd_links_read(env, obj, ldata);
4749 mlc->mlc_opc = MD_LAYOUT_DETACH;
4750 rc = mdo_layout_change(env, obj, mlc, handle);
4754 /* don't set nlink from obj */
4755 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4757 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4758 NULL, NULL, NULL, hint, handle, false);
4762 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4766 rc = mdd_links_write(env, tobj, ldata, handle);
4770 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4774 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4775 lname->ln_name, handle);
4779 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4780 la->la_valid = LA_CTIME | LA_MTIME;
4782 mdd_write_lock(env, obj, DT_SRC_CHILD);
4783 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4784 mdd_write_unlock(env, obj);
4788 rc = mdd_la_get(env, pobj, pattr);
4792 la->la_valid = LA_CTIME | LA_MTIME;
4794 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4795 rc = mdd_update_time(env, pobj, pattr, la, handle);
4796 mdd_write_unlock(env, pobj);
4800 /* FID changes, record it as CL_MIGRATE */
4801 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4802 mdd_object_fid(pobj), mdd_object_fid(obj),
4803 mdd_object_fid(pobj), lname, lname, handle);
4807 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4808 struct md_layout_change *mlc)
4810 struct mdd_thread_info *info = mdd_env_info(env);
4811 struct mdd_device *mdd = mdo2mdd(o);
4812 struct mdd_object *obj = md2mdd_obj(o);
4813 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4814 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4815 struct dt_allocation_hint *hint = &info->mti_hint;
4816 bool is_plain = false;
4817 struct thandle *handle;
4822 LASSERT(S_ISDIR(mdd_object_type(obj)));
4824 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4830 handle = mdd_trans_create(env, mdd);
4832 RETURN(PTR_ERR(handle));
4835 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4838 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4839 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4841 GOTO(stop_trans, rc);
4843 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4847 GOTO(stop_trans, rc);
4849 rc = mdd_trans_start(env, mdd, handle);
4851 GOTO(stop_trans, rc);
4854 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4857 mdd_write_lock(env, obj, DT_TGT_CHILD);
4858 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4859 LU_XATTR_CREATE, handle);
4860 mdd_write_unlock(env, obj);
4862 GOTO(stop_trans, rc);
4864 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4865 XATTR_NAME_LMV, handle);
4868 GOTO(stop_trans, rc);
4873 rc = mdd_trans_stop(env, mdd, rc, handle);
4878 const struct md_dir_operations mdd_dir_ops = {
4879 .mdo_is_subdir = mdd_is_subdir,
4880 .mdo_lookup = mdd_lookup,
4881 .mdo_create = mdd_create,
4882 .mdo_rename = mdd_rename,
4883 .mdo_link = mdd_link,
4884 .mdo_unlink = mdd_unlink,
4885 .mdo_create_data = mdd_create_data,
4886 .mdo_migrate = mdd_migrate,