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, unsigned int may_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, may_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_link_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, 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_link_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,
1944 rc = mdd_trans_stop(env, mdd, rc, handle);
1950 static int mdd_declare_object_initialize(const struct lu_env *env,
1951 struct mdd_object *parent,
1952 struct mdd_object *child,
1953 const struct lu_attr *attr,
1954 struct thandle *handle)
1959 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1960 if (!S_ISDIR(attr->la_mode))
1963 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1964 S_IFDIR, dot, handle);
1968 rc = mdo_declare_ref_add(env, child, handle);
1972 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1973 S_IFDIR, dotdot, handle);
1978 static int mdd_object_initialize(const struct lu_env *env,
1979 const struct lu_fid *pfid,
1980 struct mdd_object *child,
1981 struct lu_attr *attr,
1982 struct thandle *handle)
1987 if (S_ISDIR(attr->la_mode)) {
1988 /* Add "." and ".." for newly created dir */
1989 mdo_ref_add(env, child, handle);
1990 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1991 S_IFDIR, dot, handle);
1993 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1996 mdo_ref_del(env, child, handle);
2003 * This function checks whether it can create a file/dir under the
2004 * directory(@pobj). The directory(@pobj) is not being locked by
2007 * \param[in] env execution environment
2008 * \param[in] pobj the directory to create files
2009 * \param[in] pattr the attributes of the directory
2010 * \param[in] lname the name of the created file/dir
2011 * \param[in] cattr the attributes of the file/dir
2012 * \param[in] spec create specification
2014 * \retval = 0 it is allowed to create file/dir under
2016 * \retval negative error not allowed to create file/dir
2017 * under the directory
2019 static int mdd_create_sanity_check(const struct lu_env *env,
2020 struct md_object *pobj,
2021 const struct lu_attr *pattr,
2022 const struct lu_name *lname,
2023 struct lu_attr *cattr,
2024 struct md_op_spec *spec)
2026 struct mdd_thread_info *info = mdd_env_info(env);
2027 struct lu_fid *fid = &info->mti_fid;
2028 struct mdd_object *obj = md2mdd_obj(pobj);
2029 struct mdd_device *m = mdo2mdd(pobj);
2030 bool check_perm = true;
2035 if (mdd_is_dead_obj(obj))
2039 * In some cases this lookup is not needed - we know before if name
2040 * exists or not because MDT performs lookup for it.
2041 * name length check is done in lookup.
2043 if (spec->sp_cr_lookup) {
2045 * Check if the name already exist, though it will be checked in
2046 * _index_insert also, for avoiding rolling back if exists
2049 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2050 MAY_WRITE | MAY_EXEC);
2052 RETURN(rc ? : -EEXIST);
2054 /* Permission is already being checked in mdd_lookup */
2058 if (S_ISDIR(cattr->la_mode) &&
2059 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2060 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2061 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2063 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2064 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2066 CERROR("%s: invalid lmv_user_md: magic = %x, "
2067 "stripe_offset = %d, stripe_count = %u: "
2068 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2069 le32_to_cpu(lum->lum_magic),
2070 (int)le32_to_cpu(lum->lum_stripe_offset),
2071 le32_to_cpu(lum->lum_stripe_count), rc);
2076 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2081 if (pattr->la_mode & S_ISGID) {
2082 struct lu_ucred *uc = lu_ucred(env);
2084 cattr->la_gid = pattr->la_gid;
2086 /* Directories are special, and always inherit S_ISGID */
2087 if (S_ISDIR(cattr->la_mode)) {
2088 cattr->la_mode |= S_ISGID;
2089 cattr->la_valid |= LA_MODE;
2090 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2091 == (S_ISGID | S_IXGRP) &&
2092 !lustre_in_group_p(uc,
2093 (cattr->la_valid & LA_GID) ?
2094 cattr->la_gid : pattr->la_gid) &&
2095 !md_capable(uc, CAP_FSETID)) {
2096 cattr->la_mode &= ~S_ISGID;
2097 cattr->la_valid |= LA_MODE;
2101 /* Inherit project ID from parent directory */
2102 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2103 cattr->la_projid = pattr->la_projid;
2104 if (S_ISDIR(cattr->la_mode)) {
2105 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2106 cattr->la_valid |= LA_FLAGS;
2108 cattr->la_valid |= LA_PROJID;
2111 rc = mdd_name_check(m, lname);
2115 switch (cattr->la_mode & S_IFMT) {
2117 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2119 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2120 RETURN(-ENAMETOOLONG);
2139 static int mdd_declare_create_object(const struct lu_env *env,
2140 struct mdd_device *mdd,
2141 struct mdd_object *p, struct mdd_object *c,
2142 struct lu_attr *attr,
2143 struct thandle *handle,
2144 const struct md_op_spec *spec,
2145 struct lu_buf *def_acl_buf,
2146 struct lu_buf *acl_buf,
2147 struct lu_buf *hsm_buf,
2148 struct dt_allocation_hint *hint)
2150 const struct lu_buf *buf;
2153 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2158 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2159 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2160 /* if dir, then can inherit default ACl */
2161 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2162 XATTR_NAME_ACL_DEFAULT,
2168 if (acl_buf && acl_buf->lb_len > 0) {
2169 rc = mdo_declare_attr_set(env, c, attr, handle);
2173 rc = mdo_declare_xattr_set(env, c, acl_buf,
2174 XATTR_NAME_ACL_ACCESS, 0, handle);
2179 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2183 /* replay case, create LOV EA from client data */
2184 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2185 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2186 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2187 spec->u.sp_ea.eadatalen);
2188 rc = mdo_declare_xattr_set(env, c, buf,
2189 S_ISDIR(attr->la_mode) ?
2190 XATTR_NAME_LMV : XATTR_NAME_LOV,
2191 LU_XATTR_CREATE, handle);
2195 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2196 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2204 if (S_ISLNK(attr->la_mode)) {
2205 const char *target_name = spec->u.sp_symname;
2206 int sym_len = strlen(target_name);
2207 const struct lu_buf *buf;
2209 buf = mdd_buf_get_const(env, target_name, sym_len);
2210 rc = dt_declare_record_write(env, mdd_object_child(c),
2216 if (spec->sp_cr_file_secctx_name != NULL) {
2217 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2218 spec->sp_cr_file_secctx_size);
2219 rc = mdo_declare_xattr_set(env, c, buf,
2220 spec->sp_cr_file_secctx_name, 0,
2226 if (spec->sp_cr_file_encctx != NULL) {
2227 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2228 spec->sp_cr_file_encctx_size);
2229 rc = mdo_declare_xattr_set(env, c, buf,
2230 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2239 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2240 struct mdd_object *p, struct mdd_object *c,
2241 const struct lu_name *name,
2242 struct lu_attr *attr,
2243 struct thandle *handle,
2244 const struct md_op_spec *spec,
2245 struct linkea_data *ldata,
2246 struct lu_buf *def_acl_buf,
2247 struct lu_buf *acl_buf,
2248 struct lu_buf *hsm_buf,
2249 struct dt_allocation_hint *hint)
2253 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2254 def_acl_buf, acl_buf, hsm_buf, hint);
2258 if (S_ISDIR(attr->la_mode)) {
2259 rc = mdo_declare_ref_add(env, p, handle);
2264 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2265 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2269 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2270 enum changelog_rec_type type;
2272 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2273 attr->la_mode, name->ln_name,
2278 rc = mdd_declare_links_add(env, c, handle, ldata);
2283 la->la_valid = LA_CTIME | LA_MTIME;
2284 rc = mdo_declare_attr_set(env, p, la, handle);
2288 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2289 S_ISREG(attr->la_mode) ? CL_CREATE :
2290 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2292 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2301 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2302 struct lu_attr *la, struct lu_buf *def_acl_buf,
2303 struct lu_buf *acl_buf)
2309 if (S_ISLNK(la->la_mode)) {
2310 acl_buf->lb_len = 0;
2311 def_acl_buf->lb_len = 0;
2315 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2316 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2317 XATTR_NAME_ACL_DEFAULT);
2318 mdd_read_unlock(env, pobj);
2320 /* ACL buffer size is not enough, need realloc */
2321 if (rc > acl_buf->lb_len)
2324 /* If there are default ACL, fix mode/ACL by default ACL */
2325 def_acl_buf->lb_len = rc;
2326 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2327 acl_buf->lb_len = rc;
2328 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2331 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2332 /* If there are no default ACL, fix mode by mask */
2333 struct lu_ucred *uc = lu_ucred(env);
2335 /* The create triggered by MDT internal events, such as
2336 * LFSCK reset, will not contain valid "uc". */
2337 if (unlikely(uc != NULL))
2338 la->la_mode &= ~uc->uc_umask;
2340 acl_buf->lb_len = 0;
2341 def_acl_buf->lb_len = 0;
2348 * Create a metadata object and initialize it, set acl, xattr.
2350 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2351 struct mdd_object *son, struct lu_attr *attr,
2352 struct md_op_spec *spec, struct lu_buf *acl_buf,
2353 struct lu_buf *def_acl_buf,
2354 struct lu_buf *hsm_buf,
2355 struct dt_allocation_hint *hint,
2356 struct thandle *handle, bool initsecctx)
2358 const struct lu_buf *buf;
2361 mdd_write_lock(env, son, DT_TGT_CHILD);
2362 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2367 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2368 * created in declare phase, they also needs to be added to master
2369 * object as sub-directory entry. So it has to initialize the master
2370 * object, then set dir striped EA.(in mdo_xattr_set) */
2371 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2374 GOTO(err_destroy, rc);
2377 * in case of replay we just set LOVEA provided by the client
2378 * XXX: I think it would be interesting to try "old" way where
2379 * MDT calls this xattr_set(LOV) in a different transaction.
2380 * probably this way we code can be made better.
2383 /* During creation, there are only a few cases we need do xattr_set to
2385 * 1. regular file: see comments above.
2386 * 2. dir: inherit default striping or pool settings from parent.
2387 * 3. create striped directory with provided stripeEA.
2388 * 4. create striped directory because inherit default layout from the
2391 if (spec->no_create ||
2392 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2393 S_ISDIR(attr->la_mode)) {
2394 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2395 spec->u.sp_ea.eadatalen);
2396 rc = mdo_xattr_set(env, son, buf,
2397 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2399 LU_XATTR_CREATE, handle);
2401 GOTO(err_destroy, rc);
2404 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2407 memset(&mh, 0, sizeof(mh));
2408 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2409 mh.mh_arch_id = spec->sp_archive_id;
2410 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2411 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2414 GOTO(err_destroy, rc);
2417 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2418 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2419 S_ISDIR(attr->la_mode)) {
2420 /* set default acl */
2421 rc = mdo_xattr_set(env, son, def_acl_buf,
2422 XATTR_NAME_ACL_DEFAULT, 0,
2425 GOTO(err_destroy, rc);
2427 /* set its own acl */
2428 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2429 rc = mdo_xattr_set(env, son, acl_buf,
2430 XATTR_NAME_ACL_ACCESS,
2433 GOTO(err_destroy, rc);
2437 if (S_ISLNK(attr->la_mode)) {
2438 struct dt_object *dt = mdd_object_child(son);
2439 const char *target_name = spec->u.sp_symname;
2440 int sym_len = strlen(target_name);
2443 buf = mdd_buf_get_const(env, target_name, sym_len);
2444 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2448 GOTO(err_initlized, rc = -EFAULT);
2451 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2452 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2453 spec->sp_cr_file_secctx_size);
2454 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2457 GOTO(err_initlized, rc);
2460 if (spec->sp_cr_file_encctx != NULL) {
2461 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2462 spec->sp_cr_file_encctx_size);
2463 rc = mdo_xattr_set(env, son, buf,
2464 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2467 GOTO(err_initlized, rc);
2471 if (unlikely(rc != 0)) {
2473 if (S_ISDIR(attr->la_mode)) {
2474 /* Drop the reference, no need to delete "."/"..",
2475 * because the object to be destroied directly. */
2476 rc2 = mdo_ref_del(env, son, handle);
2480 rc2 = mdo_ref_del(env, son, handle);
2484 mdo_destroy(env, son, handle);
2487 mdd_write_unlock(env, son);
2491 static int mdd_index_delete(const struct lu_env *env,
2492 struct mdd_object *mdd_pobj,
2493 struct lu_attr *cattr,
2494 const struct lu_name *lname)
2496 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2497 struct thandle *handle;
2501 handle = mdd_trans_create(env, mdd);
2503 RETURN(PTR_ERR(handle));
2505 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2510 if (S_ISDIR(cattr->la_mode)) {
2511 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2516 /* Since this will only be used in the error handler path,
2517 * Let's set the thandle to be local and not mess the transno */
2518 handle->th_local = 1;
2519 rc = mdd_trans_start(env, mdd, handle);
2523 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2524 S_ISDIR(cattr->la_mode), handle);
2528 rc = mdd_trans_stop(env, mdd, rc, handle);
2534 * Create object and insert it into namespace.
2536 * Two operations have to be performed:
2538 * - an allocation of a new object (->do_create()), and
2539 * - an insertion into a parent index (->dio_insert()).
2541 * Due to locking, operation order is not important, when both are
2542 * successful, *but* error handling cases are quite different:
2544 * - if insertion is done first, and following object creation fails,
2545 * insertion has to be rolled back, but this operation might fail
2546 * also leaving us with dangling index entry.
2548 * - if creation is done first, is has to be undone if insertion fails,
2549 * leaving us with leaked space, which is not good but not fatal.
2551 * It seems that creation-first is simplest solution, but it is sub-optimal
2557 * case, because second mkdir is bound to create object, only to
2558 * destroy it immediately.
2560 * To avoid this follow local file systems that do double lookup:
2562 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2563 * 1. create (mdd_create_object_internal())
2564 * 2. insert (__mdd_index_insert(), lookup again)
2566 * \param[in] pobj parent object
2567 * \param[in] lname name of child being created
2568 * \param[in,out] child child object being created
2569 * \param[in] spec additional create parameters
2570 * \param[in] ma attributes for new child object
2572 * \retval 0 on success
2573 * \retval negative errno on failure
2575 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2576 const struct lu_name *lname, struct md_object *child,
2577 struct md_op_spec *spec, struct md_attr *ma)
2579 struct mdd_thread_info *info = mdd_env_info(env);
2580 struct lu_attr *la = &info->mti_la_for_fix;
2581 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2582 struct mdd_object *son = md2mdd_obj(child);
2583 struct mdd_device *mdd = mdo2mdd(pobj);
2584 struct lu_attr *attr = &ma->ma_attr;
2585 struct thandle *handle;
2586 struct lu_attr *pattr = &info->mti_pattr;
2587 struct lu_buf acl_buf;
2588 struct lu_buf def_acl_buf;
2589 struct lu_buf hsm_buf;
2590 struct linkea_data *ldata = &info->mti_link_data;
2591 const char *name = lname->ln_name;
2592 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2593 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2598 rc = mdd_la_get(env, mdd_pobj, pattr);
2602 /* Sanity checks before big job. */
2603 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2607 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2608 GOTO(out_free, rc = -EINPROGRESS);
2610 handle = mdd_trans_create(env, mdd);
2612 GOTO(out_free, rc = PTR_ERR(handle));
2615 acl_buf = *lu_buf_check_and_alloc(&info->mti_xattr_buf, acl_size);
2616 if (!acl_buf.lb_buf)
2617 GOTO(out_stop, rc = -ENOMEM);
2618 /* mti_big_buf is also used down below in mdd_changelog_ns_store(),
2619 * but def_acl_buf is finished with it before then
2621 def_acl_buf = *lu_buf_check_and_alloc(&info->mti_big_buf, acl_size);
2622 if (!def_acl_buf.lb_buf)
2623 GOTO(out_stop, rc = -ENOMEM);
2625 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2626 if (unlikely(rc == -ERANGE &&
2627 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2628 /* use maximum-sized xattr buffer for too-big default ACL */
2629 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2631 goto use_bigger_buffer;
2636 if (S_ISDIR(attr->la_mode)) {
2637 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2640 * migrate may create 1-stripe directory, so lod_ah_init()
2641 * doesn't adjust stripe count from lmu.
2643 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2644 lmu->lum_stripe_count = 0;
2647 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2649 memset(ldata, 0, sizeof(*ldata));
2650 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2651 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2654 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2655 &tfid, lname, 1, 0, ldata);
2657 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2658 mdd_object_fid(mdd_pobj),
2659 lname, 1, 0, ldata);
2662 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2663 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2665 memset(&hsm_buf, 0, sizeof(hsm_buf));
2666 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2667 if (hsm_buf.lb_buf == NULL)
2668 GOTO(out_stop, rc = -ENOMEM);
2671 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2672 handle, spec, ldata, &def_acl_buf, &acl_buf,
2677 rc = mdd_trans_start(env, mdd, handle);
2681 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2682 &def_acl_buf, &hsm_buf, hint, handle, true);
2686 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2687 mdd_write_lock(env, son, DT_TGT_CHILD);
2688 son->mod_flags |= VOLATILE_OBJ;
2689 rc = mdd_orphan_insert(env, son, handle);
2690 GOTO(out_volatile, rc);
2692 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2693 attr->la_mode, name, handle);
2695 GOTO(err_created, rc);
2697 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2700 /* update parent directory mtime/ctime */
2702 la->la_valid = LA_CTIME | LA_MTIME;
2703 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2705 GOTO(err_insert, rc);
2711 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2712 rc2 = mdd_orphan_delete(env, son, handle);
2714 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2715 S_ISDIR(attr->la_mode),
2721 mdd_write_lock(env, son, DT_TGT_CHILD);
2722 if (S_ISDIR(attr->la_mode)) {
2723 /* Drop the reference, no need to delete "."/"..",
2724 * because the object is to be destroyed directly. */
2725 rc2 = mdo_ref_del(env, son, handle);
2727 mdd_write_unlock(env, son);
2732 /* For volatile files drop one link immediately, since there is
2733 * no filename in the namespace, and save any error returned. */
2734 rc2 = mdo_ref_del(env, son, handle);
2736 mdd_write_unlock(env, son);
2737 if (unlikely(rc == 0))
2742 /* Don't destroy the volatile object on success */
2743 if (likely(rc != 0))
2744 mdo_destroy(env, son, handle);
2745 mdd_write_unlock(env, son);
2748 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2749 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2750 rc = mdd_changelog_ns_store(env, mdd,
2751 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2752 S_ISREG(attr->la_mode) ? CL_CREATE :
2753 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2754 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2755 lname, NULL, handle);
2757 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2759 /* If creation fails, it is most likely due to the remote update
2760 * failure, because local transaction will mostly succeed at
2761 * this stage. There is no easy way to rollback all of previous
2762 * updates, so let's remove the object from namespace, and
2763 * LFSCK should handle the orphan object. */
2764 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2765 mdd_index_delete(env, mdd_pobj, attr, lname);
2769 if (is_vmalloc_addr(ldata->ld_buf))
2770 /* if we vmalloced a large buffer drop it */
2771 lu_buf_free(ldata->ld_buf);
2773 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2774 lu_buf_free(&hsm_buf);
2776 /* The child object shouldn't be cached anymore */
2778 set_bit(LU_OBJECT_HEARD_BANSHEE,
2779 &child->mo_lu.lo_header->loh_flags);
2783 /* has not mdd_write{read}_lock on any obj yet. */
2784 static int mdd_rename_sanity_check(const struct lu_env *env,
2785 struct mdd_object *src_pobj,
2786 const struct lu_attr *pattr,
2787 struct mdd_object *tgt_pobj,
2788 const struct lu_attr *tpattr,
2789 struct mdd_object *sobj,
2790 const struct lu_attr *cattr,
2791 struct mdd_object *tobj,
2792 const struct lu_attr *tattr)
2797 /* XXX: when get here, sobj must NOT be NULL,
2798 * the other case has been processed in cld_rename
2799 * before mdd_rename and enable MDS_PERM_BYPASS. */
2803 * If we are using project inheritance, we only allow renames
2804 * into our tree when the project IDs are the same; otherwise
2805 * tree quota mechanism would be circumvented.
2807 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2808 tpattr->la_projid != cattr->la_projid) ||
2809 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2810 (pattr->la_projid != tpattr->la_projid)))
2813 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2817 /* XXX: when get here, "tobj == NULL" means tobj must
2818 * NOT exist (neither on remote MDS, such case has been
2819 * processed in cld_rename before mdd_rename and enable
2821 * So check may_create, but not check may_unlink. */
2823 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2824 (src_pobj != tgt_pobj));
2826 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2827 (src_pobj != tgt_pobj), 1);
2829 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2830 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2835 static int mdd_declare_rename(const struct lu_env *env,
2836 struct mdd_device *mdd,
2837 struct mdd_object *mdd_spobj,
2838 struct mdd_object *mdd_tpobj,
2839 struct mdd_object *mdd_sobj,
2840 struct mdd_object *mdd_tobj,
2841 const struct lu_name *sname,
2842 const struct lu_name *tname,
2844 struct linkea_data *ldata,
2845 struct thandle *handle)
2847 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2850 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2851 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2857 /* name from source dir */
2858 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2862 /* .. from source child */
2863 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2864 /* source child can be directory, count by source dir's nlink */
2865 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2868 if (mdd_spobj != mdd_tpobj) {
2869 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2874 rc = mdo_declare_index_insert(env, mdd_sobj,
2875 mdd_object_fid(mdd_tpobj),
2876 S_IFDIR, dotdot, handle);
2881 /* new target child can be directory,
2882 * counted by target dir's nlink */
2883 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2888 la->la_valid = LA_CTIME | LA_MTIME;
2889 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2893 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2897 la->la_valid = LA_CTIME;
2898 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2902 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2907 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2908 mdd_object_type(mdd_sobj),
2909 tname->ln_name, handle);
2913 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2914 /* delete target child in target parent directory */
2915 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2920 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2924 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2925 /* target child can be directory,
2926 * delete "." reference in target child directory */
2927 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2931 /* delete ".." reference in target parent directory */
2932 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2937 la->la_valid = LA_CTIME;
2938 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2942 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2947 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2955 static int mdd_migrate_object(const struct lu_env *env,
2956 struct mdd_object *spobj,
2957 struct mdd_object *tpobj,
2958 struct mdd_object *sobj,
2959 struct mdd_object *tobj,
2960 const struct lu_name *sname,
2961 const struct lu_name *tname,
2962 struct md_op_spec *spec,
2963 struct md_attr *ma);
2965 /* src object can be remote that is why we use only fid and type of object */
2966 static int mdd_rename(const struct lu_env *env,
2967 struct md_object *src_pobj, struct md_object *tgt_pobj,
2968 const struct lu_fid *lf, const struct lu_name *lsname,
2969 struct md_object *tobj, const struct lu_name *ltname,
2972 const char *sname = lsname->ln_name;
2973 const char *tname = ltname->ln_name;
2974 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2975 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2976 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2977 struct mdd_device *mdd = mdo2mdd(src_pobj);
2978 struct mdd_object *mdd_sobj = NULL; /* source object */
2979 struct mdd_object *mdd_tobj = NULL;
2980 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2981 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2982 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2983 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2984 struct thandle *handle;
2985 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2986 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
2987 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
2990 bool tobj_locked = 0;
2991 unsigned cl_flags = 0;
2995 /* let unlink to complete and commit */
2996 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
2999 mdd_tobj = md2mdd_obj(tobj);
3001 mdd_sobj = mdd_object_find(env, mdd, lf);
3002 if (IS_ERR(mdd_sobj))
3003 RETURN(PTR_ERR(mdd_sobj));
3005 rc = mdd_la_get(env, mdd_sobj, cattr);
3007 GOTO(out_pending, rc);
3009 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3010 * hard links, and target doesn't exist.
3012 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3013 cattr->la_nlink == 1 && !tobj) {
3014 struct md_op_spec *spec = &mdd_env_info(env)->mti_spec;
3015 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3018 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3021 GOTO(out_pending, rc);
3023 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3024 if (IS_ERR(mdd_tobj))
3025 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3027 memset(spec, 0, sizeof(*spec));
3028 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3029 mdd_tobj, lsname, ltname, spec, ma);
3030 mdd_object_put(env, mdd_tobj);
3031 GOTO(out_pending, rc);
3034 rc = mdd_la_get(env, mdd_spobj, pattr);
3036 GOTO(out_pending, rc);
3039 rc = mdd_la_get(env, mdd_tobj, tattr);
3041 GOTO(out_pending, rc);
3042 /* search for an existing archive.
3043 * we should check ahead as the object
3044 * can be destroyed in this transaction */
3045 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3046 cl_flags |= CLF_RENAME_LAST_EXISTS;
3049 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3051 GOTO(out_pending, rc);
3053 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3054 mdd_sobj, cattr, mdd_tobj, tattr);
3056 GOTO(out_pending, rc);
3058 rc = mdd_name_check(mdd, ltname);
3060 GOTO(out_pending, rc);
3062 handle = mdd_trans_create(env, mdd);
3064 GOTO(out_pending, rc = PTR_ERR(handle));
3066 memset(ldata, 0, sizeof(*ldata));
3067 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3068 lsname, mdd_object_fid(mdd_tpobj), ltname,
3073 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3074 mdd_tobj, lsname, ltname, ma, ldata, handle);
3078 rc = mdd_trans_start(env, mdd, handle);
3082 is_dir = S_ISDIR(cattr->la_mode);
3084 /* Remove source name from source directory */
3085 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3089 /* "mv dir1 dir2" needs "dir1/.." link update */
3090 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3091 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3093 GOTO(fixup_spobj2, rc);
3095 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3098 GOTO(fixup_spobj, rc);
3101 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3102 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3104 /* tname might been renamed to something else */
3105 GOTO(fixup_spobj, rc);
3108 /* Insert new fid with target name into target dir */
3109 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3112 GOTO(fixup_tpobj, rc);
3114 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3115 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3117 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3118 la->la_valid = LA_CTIME;
3119 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3121 GOTO(fixup_tpobj, rc);
3123 /* Update the linkEA for the source object */
3124 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3125 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3126 lsname, mdd_object_fid(mdd_tpobj), ltname,
3127 handle, ldata, 0, 0);
3129 /* Old files might not have EA entry */
3130 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3131 lsname, handle, NULL, 0);
3132 mdd_write_unlock(env, mdd_sobj);
3133 /* We don't fail the transaction if the link ea can't be
3134 updated -- fid2path will use alternate lookup method. */
3137 /* Remove old target object
3138 * For tobj is remote case cmm layer has processed
3139 * and set tobj to NULL then. So when tobj is NOT NULL,
3140 * it must be local one.
3142 if (tobj && mdd_object_exists(mdd_tobj)) {
3143 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3145 if (mdd_is_dead_obj(mdd_tobj)) {
3146 /* shld not be dead, something is wrong */
3147 CERROR("tobj is dead, something is wrong\n");
3151 mdo_ref_del(env, mdd_tobj, handle);
3153 /* Remove dot reference. */
3154 if (S_ISDIR(tattr->la_mode))
3155 mdo_ref_del(env, mdd_tobj, handle);
3158 /* fetch updated nlink */
3159 rc = mdd_la_get(env, mdd_tobj, tattr);
3161 CERROR("%s: Failed to get nlink for tobj "
3163 mdd2obd_dev(mdd)->obd_name,
3164 PFID(tpobj_fid), rc);
3165 GOTO(fixup_tpobj, rc);
3168 la->la_valid = LA_CTIME;
3169 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3171 CERROR("%s: Failed to set ctime for tobj "
3173 mdd2obd_dev(mdd)->obd_name,
3174 PFID(tpobj_fid), rc);
3175 GOTO(fixup_tpobj, rc);
3178 /* XXX: this transfer to ma will be removed with LOD/OSP */
3179 ma->ma_attr = *tattr;
3180 ma->ma_valid |= MA_INODE;
3181 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3184 CERROR("%s: Failed to unlink tobj "
3186 mdd2obd_dev(mdd)->obd_name,
3187 PFID(tpobj_fid), rc);
3188 GOTO(fixup_tpobj, rc);
3191 /* fetch updated nlink */
3192 rc = mdd_la_get(env, mdd_tobj, tattr);
3193 if (rc == -ENOENT) {
3194 /* the object got removed, let's
3195 * return the latest known attributes */
3196 tattr->la_nlink = 0;
3198 } else if (rc != 0) {
3199 CERROR("%s: Failed to get nlink for tobj "
3201 mdd2obd_dev(mdd)->obd_name,
3202 PFID(tpobj_fid), rc);
3203 GOTO(fixup_tpobj, rc);
3205 /* XXX: this transfer to ma will be removed with LOD/OSP */
3206 ma->ma_attr = *tattr;
3207 ma->ma_valid |= MA_INODE;
3209 if (tattr->la_nlink == 0)
3210 cl_flags |= CLF_RENAME_LAST;
3212 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3215 la->la_valid = LA_CTIME | LA_MTIME;
3216 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3218 GOTO(fixup_tpobj, rc);
3220 if (mdd_spobj != mdd_tpobj) {
3221 la->la_valid = LA_CTIME | LA_MTIME;
3222 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3224 GOTO(fixup_tpobj, rc);
3231 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3233 CWARN("tp obj fix error %d\n",rc2);
3235 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3236 !mdd_is_dead_obj(mdd_tobj)) {
3238 mdo_ref_add(env, mdd_tobj, handle);
3240 mdo_ref_add(env, mdd_tobj, handle);
3243 rc2 = __mdd_index_insert(env, mdd_tpobj,
3244 mdd_object_fid(mdd_tobj),
3245 mdd_object_type(mdd_tobj),
3248 CWARN("tp obj fix error: rc = %d\n", rc2);
3253 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3254 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3256 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3257 mdd2obd_dev(mdd)->obd_name, rc2);
3260 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3263 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3264 mdd2obd_dev(mdd)->obd_name, rc2);
3269 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3270 mdd_object_type(mdd_sobj), sname,
3273 CWARN("sp obj fix error: rc = %d\n", rc2);
3278 mdd_write_unlock(env, mdd_tobj);
3281 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3282 mdd_tobj, tpobj_fid, lf, spobj_fid,
3283 ltname, lsname, handle);
3286 rc = mdd_trans_stop(env, mdd, rc, handle);
3289 mdd_object_put(env, mdd_sobj);
3294 * Check whether we should migrate the file/dir
3296 * < 0 permission check failed or other error.
3297 * = 0 the file can be migrated.
3299 static int mdd_migrate_sanity_check(const struct lu_env *env,
3300 struct mdd_device *mdd,
3301 struct mdd_object *spobj,
3302 struct mdd_object *tpobj,
3303 struct mdd_object *sobj,
3304 struct mdd_object *tobj,
3305 const struct lu_attr *spattr,
3306 const struct lu_attr *tpattr,
3307 const struct lu_attr *attr)
3313 if (!mdd_object_remote(sobj)) {
3314 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3315 if (sobj->mod_count > 0) {
3316 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3317 mdd_obj_dev_name(sobj),
3318 PFID(mdd_object_fid(sobj)),
3320 mdd_read_unlock(env, sobj);
3323 mdd_read_unlock(env, sobj);
3326 if (mdd_object_exists(tobj))
3329 rc = mdd_rename_sanity_check(env, spobj, spattr, tpobj, tpattr, sobj,
3334 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3335 struct mdd_object *obj,
3336 const struct lu_buf *buf,
3338 int fl, struct thandle *handle);
3340 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3341 static int mdd_iterate_xattrs(const struct lu_env *env,
3342 struct mdd_object *sobj,
3343 struct mdd_object *tobj,
3345 struct thandle *handle,
3348 struct mdd_thread_info *info = mdd_env_info(env);
3350 struct lu_buf list_xbuf;
3351 struct lu_buf cbxbuf;
3352 struct lu_buf xbuf = { NULL };
3361 /* retrieve xattr list from the old object */
3362 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3363 if (list_xsize == -ENODATA)
3369 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3370 if (info->mti_big_buf.lb_buf == NULL)
3373 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3374 list_xbuf.lb_len = list_xsize;
3375 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3381 xname = list_xbuf.lb_buf;
3383 xlen = strnlen(xname, rem - 1) + 1;
3384 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3385 strcmp(XATTR_NAME_LMV, xname) == 0)
3389 strcmp(XATTR_NAME_LINK, xname) == 0)
3392 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3393 if (xsize == -ENODATA)
3396 GOTO(out, rc = xsize);
3398 lu_buf_check_and_alloc(&xbuf, xsize);
3399 if (xbuf.lb_buf == NULL)
3400 GOTO(out, rc = -ENOMEM);
3402 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3409 cbxbuf.lb_len = xsize;
3411 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3412 if (unlikely(rc == -ENOSPC &&
3413 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3414 rc = linkea_overflow_shrink(
3415 (struct linkea_data *)(cbxbuf.lb_buf));
3416 if (likely(rc > 0)) {
3434 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3435 struct mdd_object *sobj,
3436 struct mdd_object *tobj,
3437 const struct lu_name *sname,
3438 const struct lu_fid *sfid,
3439 const struct lu_name *lname,
3440 const struct lu_fid *fid,
3442 struct thandle *handle);
3444 static int mdd_declare_update_link(const struct lu_env *env,
3445 struct mdd_object *sobj,
3446 struct mdd_object *tobj,
3447 const struct lu_name *tname,
3448 const struct lu_fid *tpfid,
3449 const struct lu_name *lname,
3450 const struct lu_fid *fid,
3452 struct thandle *handle)
3454 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3455 struct mdd_object *pobj;
3459 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3460 !strcmp(tname->ln_name, lname->ln_name))
3463 pobj = mdd_object_find(env, mdd, fid);
3465 return PTR_ERR(pobj);
3468 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3470 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3471 mdd_object_type(sobj),
3472 lname->ln_name, handle);
3473 mdd_object_put(env, pobj);
3477 rc = mdo_declare_ref_add(env, tobj, handle);
3481 rc = mdo_declare_ref_del(env, sobj, handle);
3485 static int mdd_update_link(const struct lu_env *env,
3486 struct mdd_object *sobj,
3487 struct mdd_object *tobj,
3488 const struct lu_name *tname,
3489 const struct lu_fid *tpfid,
3490 const struct lu_name *lname,
3491 const struct lu_fid *fid,
3493 struct thandle *handle)
3495 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3496 struct mdd_object *pobj;
3501 LASSERT(lu_name_is_valid(lname));
3504 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3505 !strncmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3508 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3509 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3511 pobj = mdd_object_find(env, mdd, fid);
3513 CWARN("%s: cannot find obj "DFID": %ld\n",
3514 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3515 RETURN(PTR_ERR(pobj));
3518 if (!mdd_object_exists(pobj)) {
3519 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3520 mdd_object_put(env, pobj);
3524 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3525 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3527 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3528 mdd_object_type(sobj),
3529 lname->ln_name, handle);
3530 mdd_write_unlock(env, pobj);
3531 mdd_object_put(env, pobj);
3535 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3536 rc = mdo_ref_add(env, tobj, handle);
3537 mdd_write_unlock(env, tobj);
3541 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3542 rc = mdo_ref_del(env, sobj, handle);
3543 mdd_write_unlock(env, sobj);
3548 static inline int mdd_fld_lookup(const struct lu_env *env,
3549 struct mdd_device *mdd,
3550 const struct lu_fid *fid,
3553 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
3554 struct seq_server_site *ss;
3557 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3559 range->lsr_flags = LU_SEQ_RANGE_MDT;
3560 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3564 *mdt_index = range->lsr_index;
3569 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3570 struct mdd_object *sobj,
3571 struct mdd_object *tobj,
3572 const struct lu_name *tname,
3573 const struct lu_fid *tpfid,
3574 const struct lu_name *lname,
3575 const struct lu_fid *fid,
3577 struct thandle *handle)
3579 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3580 __u32 source_mdt_index = *(__u32 *)opaque;
3581 __u32 link_mdt_index;
3587 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3588 !strcmp(tname->ln_name, lname->ln_name))
3591 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3595 RETURN(link_mdt_index == source_mdt_index);
3598 static int mdd_iterate_linkea(const struct lu_env *env,
3599 struct mdd_object *sobj,
3600 struct mdd_object *tobj,
3601 const struct lu_name *tname,
3602 const struct lu_fid *tpfid,
3603 struct linkea_data *ldata,
3605 struct thandle *handle,
3608 struct mdd_thread_info *info = mdd_env_info(env);
3609 char *filename = info->mti_name;
3610 struct lu_name lname;
3617 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3618 linkea_next_entry(ldata)) {
3619 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3622 /* Note: lname might miss \0 at the end */
3623 snprintf(filename, sizeof(info->mti_name), "%.*s",
3624 lname.ln_namelen, lname.ln_name);
3625 lname.ln_name = filename;
3627 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3629 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3637 * Prepare linkea, and check whether file needs migrate: if source still has
3638 * link on source MDT, no need to migrate, just update namespace on source and
3641 * \retval 0 do migrate
3642 * \retval 1 don't migrate
3643 * \retval -errno on failure
3645 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3646 struct mdd_device *mdd,
3647 struct mdd_object *spobj,
3648 struct mdd_object *tpobj,
3649 struct mdd_object *sobj,
3650 const struct lu_name *sname,
3651 const struct lu_name *tname,
3652 const struct lu_attr *attr,
3653 struct linkea_data *ldata)
3655 __u32 source_mdt_index;
3660 memset(ldata, 0, sizeof(*ldata));
3661 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3662 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3667 * Then it will check if the file should be migrated. If the file has
3668 * mulitple links, we only need migrate the file if all of its entries
3669 * has been migrated to the remote MDT.
3671 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3674 /* If there are still links locally, don't migrate this file */
3675 LASSERT(ldata->ld_leh != NULL);
3678 * If linkEA is overflow, it means there are some unknown name entries
3679 * under unknown parents, which will prevent the migration.
3681 if (unlikely(ldata->ld_leh->leh_overflow_time))
3684 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3688 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3689 ldata, &source_mdt_index, NULL,
3690 mdd_is_link_on_source_mdt);
3694 static int mdd_declare_migrate_update(const struct lu_env *env,
3695 struct mdd_object *spobj,
3696 struct mdd_object *tpobj,
3697 struct mdd_object *obj,
3698 const struct lu_name *sname,
3699 const struct lu_name *tname,
3700 struct lu_attr *attr,
3701 struct lu_attr *spattr,
3702 struct lu_attr *tpattr,
3703 struct linkea_data *ldata,
3705 struct thandle *handle)
3707 struct mdd_thread_info *info = mdd_env_info(env);
3708 struct lu_attr *la = &info->mti_la_for_fix;
3711 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3715 if (S_ISDIR(attr->la_mode)) {
3716 rc = mdo_declare_ref_del(env, spobj, handle);
3721 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3722 attr->la_mode & S_IFMT,
3723 tname->ln_name, handle);
3727 rc = mdd_declare_links_add(env, obj, handle, ldata);
3731 if (S_ISDIR(attr->la_mode)) {
3732 rc = mdo_declare_ref_add(env, tpobj, handle);
3737 la->la_valid = LA_CTIME | LA_MTIME;
3738 rc = mdo_declare_attr_set(env, spobj, la, handle);
3742 if (tpobj != spobj) {
3743 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3751 static int mdd_declare_migrate_create(const struct lu_env *env,
3752 struct mdd_object *spobj,
3753 struct mdd_object *tpobj,
3754 struct mdd_object *sobj,
3755 struct mdd_object *tobj,
3756 const struct lu_name *sname,
3757 const struct lu_name *tname,
3758 struct lu_attr *spattr,
3759 struct lu_attr *tpattr,
3760 struct lu_attr *attr,
3761 struct lu_buf *sbuf,
3762 struct linkea_data *ldata,
3764 struct md_op_spec *spec,
3765 struct dt_allocation_hint *hint,
3766 struct thandle *handle)
3768 struct mdd_thread_info *info = mdd_env_info(env);
3769 struct md_layout_change *mlc = &info->mti_mlc;
3770 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3775 if (S_ISDIR(attr->la_mode)) {
3776 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3778 mlc->mlc_opc = MD_LAYOUT_DETACH;
3779 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3783 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3784 } else if (S_ISLNK(attr->la_mode)) {
3785 spec->u.sp_symname = sbuf->lb_buf;
3786 } else if (S_ISREG(attr->la_mode)) {
3787 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3788 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3791 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3793 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3794 tname, attr, handle, spec, ldata, NULL, NULL,
3800 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3801 * createb yet, copy from sobj.
3803 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3804 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3805 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3807 if (S_ISDIR(attr->la_mode)) {
3809 /* if sobj is not striped, fake a 1-stripe LMV */
3810 LASSERT(sizeof(info->mti_key) >
3811 lmv_mds_md_size(1, LMV_MAGIC_V1));
3812 lmv = (typeof(lmv))info->mti_key;
3813 memset(lmv, 0, sizeof(*lmv));
3814 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3815 lmv->lmv_stripe_count = cpu_to_le32(1);
3816 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3817 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3818 mdd_object_fid(sobj));
3819 mlc->mlc_buf.lb_buf = lmv;
3820 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3822 mlc->mlc_buf = *sbuf;
3824 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3825 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3830 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3831 mdo_declare_xattr_set);
3835 if (S_ISREG(attr->la_mode)) {
3836 struct lu_buf fid_buf;
3838 handle->th_complex = 1;
3840 /* target may be remote, update PFID via sobj. */
3841 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3842 fid_buf.lb_len = sizeof(struct lu_fid);
3843 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3848 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3853 if (!S_ISDIR(attr->la_mode)) {
3854 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3855 mdd_object_fid(tpobj), ldata, NULL,
3856 handle, mdd_declare_update_link);
3861 if (!S_ISDIR(attr->la_mode) || lmv) {
3862 rc = mdo_declare_ref_del(env, sobj, handle);
3866 if (S_ISDIR(attr->la_mode)) {
3867 rc = mdo_declare_ref_del(env, sobj, handle);
3872 rc = mdo_declare_destroy(env, sobj, handle);
3877 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3878 attr, spattr, tpattr, ldata, ma,
3884 * migrate dirent from \a spobj to \a tpobj.
3886 static int mdd_migrate_update(const struct lu_env *env,
3887 struct mdd_object *spobj,
3888 struct mdd_object *tpobj,
3889 struct mdd_object *obj,
3890 const struct lu_name *sname,
3891 const struct lu_name *tname,
3892 struct lu_attr *attr,
3893 struct lu_attr *spattr,
3894 struct lu_attr *tpattr,
3895 struct linkea_data *ldata,
3897 struct thandle *handle)
3899 struct mdd_thread_info *info = mdd_env_info(env);
3900 struct lu_attr *la = &info->mti_la_for_fix;
3905 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3906 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3907 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3909 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3910 S_ISDIR(attr->la_mode), handle);
3914 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3915 attr->la_mode & S_IFMT,
3916 tname->ln_name, handle);
3920 rc = mdd_links_write(env, obj, ldata, handle);
3924 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3925 la->la_valid = LA_CTIME | LA_MTIME;
3926 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3927 rc = mdd_update_time(env, spobj, spattr, la, handle);
3928 mdd_write_unlock(env, spobj);
3932 if (tpobj != spobj) {
3933 la->la_valid = LA_CTIME | LA_MTIME;
3934 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3935 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3936 mdd_write_unlock(env, tpobj);
3945 * Migrate file/dir to target MDT.
3947 * Create target according to \a spec, and then migrate xattrs, if it's
3948 * directory, migrate source stripes to target.
3950 * \param[in] env execution environment
3951 * \param[in] spobj source parent object
3952 * \param[in] tpobj target parent object
3953 * \param[in] sobj source object
3954 * \param[in] tobj target object
3955 * \param[in] lname file name
3956 * \param[in] spattr source parent attributes
3957 * \param[in] tpattr target parent attributes
3958 * \param[in] attr source attributes
3959 * \param[in] sbuf source LMV buf
3960 * \param[in] spec migrate create spec
3961 * \param[in] hint target creation hint
3962 * \param[in] handle tranasction handle
3964 * \retval 0 on success
3965 * \retval -errno on failure
3967 static int mdd_migrate_create(const struct lu_env *env,
3968 struct mdd_object *spobj,
3969 struct mdd_object *tpobj,
3970 struct mdd_object *sobj,
3971 struct mdd_object *tobj,
3972 const struct lu_name *sname,
3973 const struct lu_name *tname,
3974 struct lu_attr *spattr,
3975 struct lu_attr *tpattr,
3976 struct lu_attr *attr,
3977 const struct lu_buf *sbuf,
3978 struct linkea_data *ldata,
3980 struct md_op_spec *spec,
3981 struct dt_allocation_hint *hint,
3982 struct thandle *handle)
3989 * migrate sobj stripes to tobj if it's directory:
3990 * 1. detach stripes from sobj.
3991 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
3992 * 3. create stripes for tobj, see lod_xattr_set_lmv().
3994 if (S_ISDIR(attr->la_mode)) {
3995 struct mdd_thread_info *info = mdd_env_info(env);
3996 struct md_layout_change *mlc = &info->mti_mlc;
3998 mlc->mlc_opc = MD_LAYOUT_DETACH;
4000 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4001 rc = mdo_layout_change(env, sobj, mlc, handle);
4002 mdd_write_unlock(env, sobj);
4007 /* don't set nlink from sobj */
4008 attr->la_valid &= ~LA_NLINK;
4010 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4011 hint, handle, false);
4015 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4016 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4017 mdd_write_unlock(env, tobj);
4021 /* for regular file, update OST objects XATTR_NAME_FID */
4022 if (S_ISREG(attr->la_mode)) {
4023 struct lu_buf fid_buf;
4025 /* target may be remote, update PFID via sobj. */
4026 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4027 fid_buf.lb_len = sizeof(struct lu_fid);
4028 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4033 /* delete LOV to avoid deleting OST objs when destroying sobj */
4034 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4035 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4036 mdd_write_unlock(env, sobj);
4037 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4038 if (rc && rc != -ENODATA)
4043 /* update links FID */
4044 if (!S_ISDIR(attr->la_mode)) {
4045 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4046 mdd_object_fid(tpobj), ldata,
4047 NULL, handle, mdd_update_link);
4052 /* don't destroy sobj if it's plain directory */
4053 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4054 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4055 rc = mdo_ref_del(env, sobj, handle);
4057 if (S_ISDIR(attr->la_mode))
4058 rc = mdo_ref_del(env, sobj, handle);
4060 rc = mdo_destroy(env, sobj, handle);
4062 mdd_write_unlock(env, sobj);
4067 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4068 spattr, tpattr, ldata, ma, handle);
4073 /* NB: if user issued different migrate command, we can't adjust it silently
4074 * here, because this command will decide target MDT in subdir migration in
4077 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4078 const struct lmv_mds_md_v1 *lmv,
4079 const struct lmv_user_md_v1 *lum,
4080 const struct lu_name *lname)
4082 __u32 lum_stripe_count = lum->lum_stripe_count;
4083 __u32 lmv_hash_type = lmv->lmv_hash_type;
4084 char *mdt_hash_name[] = { "none",
4085 LMV_HASH_NAME_ALL_CHARS,
4086 LMV_HASH_NAME_FNV_1A_64,
4087 LMV_HASH_NAME_CRUSH,
4090 if (!lmv_is_sane(lmv))
4093 /* if stripe_count unspecified, set to 1 */
4094 if (!lum_stripe_count)
4095 lum_stripe_count = cpu_to_le32(1);
4097 lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4099 /* TODO: check specific MDTs */
4100 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4101 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4102 (lum->lum_hash_type && lum->lum_hash_type != lmv_hash_type)) {
4103 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4104 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4105 le32_to_cpu(lmv->lmv_master_mdt_index),
4106 le32_to_cpu(lmv->lmv_migrate_offset),
4107 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4116 * Internal function to migrate directory or file between MDTs.
4118 * migrate source to target in following steps:
4119 * 1. create target, append source stripes after target's if it's directory,
4120 * migrate xattrs and update fid of source links.
4121 * 2. update namespace: migrate dirent from source parent to target parent,
4122 * update file linkea, and destroy source if it's not needed any more.
4124 * \param[in] env execution environment
4125 * \param[in] spobj source parent object
4126 * \param[in] tpobj target parent object
4127 * \param[in] sobj source object
4128 * \param[in] tobj target object
4129 * \param[in] sname source file name
4130 * \param[in] tname target file name
4131 * \param[in] spec target creation spec
4132 * \param[in] ma used to update \a pobj mtime and ctime
4134 * \retval 0 on success
4135 * \retval -errno on failure
4137 static int mdd_migrate_object(const struct lu_env *env,
4138 struct mdd_object *spobj,
4139 struct mdd_object *tpobj,
4140 struct mdd_object *sobj,
4141 struct mdd_object *tobj,
4142 const struct lu_name *sname,
4143 const struct lu_name *tname,
4144 struct md_op_spec *spec,
4147 struct mdd_thread_info *info = mdd_env_info(env);
4148 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4149 struct lu_attr *spattr = &info->mti_pattr;
4150 struct lu_attr *tpattr = &info->mti_tpattr;
4151 struct lu_attr *attr = &info->mti_cattr;
4152 struct linkea_data *ldata = &info->mti_link_data;
4153 struct dt_allocation_hint *hint = &info->mti_hint;
4154 struct lu_buf sbuf = { NULL };
4155 struct lmv_mds_md_v1 *lmv;
4156 struct thandle *handle;
4161 rc = mdd_la_get(env, sobj, attr);
4165 rc = mdd_la_get(env, spobj, spattr);
4169 rc = mdd_la_get(env, tpobj, tpattr);
4173 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4174 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4178 /* if user use default value '0' for stripe_count, we need to
4179 * adjust it to '1' to create a 1-stripe directory.
4181 if (lum->lum_stripe_count == 0)
4182 lum->lum_stripe_count = cpu_to_le32(1);
4184 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4185 if (rc && rc != -ENODATA)
4190 if (!lmv_is_sane(lmv))
4191 GOTO(out, rc = -EBADF);
4192 if (lmv_is_migrating(lmv)) {
4193 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4198 } else if (!S_ISDIR(attr->la_mode)) {
4200 GOTO(out, rc = -EALREADY);
4202 /* update namespace only if @sobj is on MDT where @tpobj is. */
4203 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4204 spec->sp_migrate_nsonly = true;
4206 if (S_ISLNK(attr->la_mode)) {
4207 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4209 GOTO(out, rc = -ENOMEM);
4211 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4214 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4215 mdd2obd_dev(mdd)->obd_name,
4216 PFID(mdd_object_fid(sobj)), rc);
4222 /* linkea needs update upon FID or parent stripe change */
4223 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4224 tname, attr, ldata);
4226 /* update namespace only if @sobj has link on its MDT. */
4227 spec->sp_migrate_nsonly = true;
4231 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4232 spattr, tpattr, attr);
4236 handle = mdd_trans_create(env, mdd);
4238 GOTO(out, rc = PTR_ERR(handle));
4240 if (spec->sp_migrate_nsonly)
4241 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4242 tname, attr, spattr, tpattr,
4245 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4246 sname, tname, spattr, tpattr,
4247 attr, &sbuf, ldata, ma, spec,
4252 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4257 rc = mdd_trans_start(env, mdd, handle);
4261 if (spec->sp_migrate_nsonly)
4262 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4263 attr, spattr, tpattr, ldata, ma,
4266 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4267 tname, spattr, tpattr, attr, &sbuf,
4268 ldata, ma, spec, hint, handle);
4272 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4273 spec->sp_migrate_nsonly ? sobj : tobj,
4274 mdd_object_fid(spobj), mdd_object_fid(sobj),
4275 mdd_object_fid(tpobj), tname, sname,
4282 rc = mdd_trans_stop(env, mdd, rc, handle);
4290 * Migrate directory or file between MDTs.
4292 * \param[in] env execution environment
4293 * \param[in] md_pobj parent master object
4294 * \param[in] md_sobj source object
4295 * \param[in] lname file name
4296 * \param[in] md_tobj target object
4297 * \param[in] spec target creation spec
4298 * \param[in] ma used to update \a pobj mtime and ctime
4300 * \retval 0 on success
4301 * \retval -errno on failure
4303 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4304 struct md_object *md_sobj, const struct lu_name *lname,
4305 struct md_object *md_tobj, struct md_op_spec *spec,
4308 struct mdd_thread_info *info = mdd_env_info(env);
4309 struct mdd_device *mdd = mdo2mdd(md_pobj);
4310 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4311 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4312 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4313 struct mdd_object *spobj = NULL;
4314 struct mdd_object *tpobj = NULL;
4315 struct lu_buf pbuf = { NULL };
4316 struct lu_fid *fid = &info->mti_fid2;
4317 struct lmv_mds_md_v1 *lmv;
4322 /* locate source and target stripe on pobj, which are the real parent */
4323 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4324 if (rc < 0 && rc != -ENODATA)
4331 if (!lmv_is_sane(lmv))
4332 GOTO(out, rc = -EBADF);
4334 /* locate target parent stripe */
4335 /* fail check here to make sure top dir migration succeed. */
4336 if (lmv_is_migrating(lmv) &&
4337 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4338 GOTO(out, rc = -EIO);
4340 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4343 GOTO(out, rc = index);
4345 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4346 tpobj = mdd_object_find(env, mdd, fid);
4348 GOTO(out, rc = PTR_ERR(tpobj));
4350 /* locate source parent stripe */
4351 if (lmv_is_layout_changing(lmv)) {
4352 index = lmv_name_to_stripe_index_old(lmv,
4356 GOTO(out, rc = index);
4358 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4359 spobj = mdd_object_find(env, mdd, fid);
4361 GOTO(out, rc = PTR_ERR(spobj));
4363 /* parent stripe unchanged */
4364 if (spobj == tpobj) {
4365 if (!lmv_is_restriping(lmv))
4366 GOTO(out, rc = -EINVAL);
4367 GOTO(out, rc = -EALREADY);
4371 mdd_object_get(spobj);
4376 mdd_object_get(tpobj);
4377 mdd_object_get(spobj);
4380 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4385 if (!IS_ERR_OR_NULL(spobj))
4386 mdd_object_put(env, spobj);
4387 if (!IS_ERR_OR_NULL(tpobj))
4388 mdd_object_put(env, tpobj);
4394 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4395 struct mdd_object *pobj,
4396 struct mdd_object *obj,
4397 struct mdd_object *stripe,
4398 struct lu_attr *attr,
4399 struct md_layout_change *mlc,
4400 struct lu_name *lname,
4401 struct thandle *handle)
4405 mlc->mlc_opc = MD_LAYOUT_DETACH;
4406 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4410 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4411 S_IFDIR, dotdot, handle);
4415 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4416 mdo_declare_xattr_set);
4420 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4424 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4428 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4432 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4433 attr->la_mode, lname->ln_name, handle);
4437 rc = mdo_declare_ref_del(env, obj, handle);
4441 rc = mdo_declare_ref_del(env, obj, handle);
4445 rc = mdo_declare_destroy(env, obj, handle);
4452 /* transform one-stripe directory to a plain directory */
4453 static int mdd_1sd_collapse(const struct lu_env *env,
4454 struct mdd_object *pobj,
4455 struct mdd_object *obj,
4456 struct mdd_object *stripe,
4457 struct lu_attr *attr,
4458 struct md_layout_change *mlc,
4459 struct lu_name *lname,
4460 struct thandle *handle)
4466 /* replace 1-stripe directory with its stripe */
4467 mlc->mlc_opc = MD_LAYOUT_DETACH;
4469 mdd_write_lock(env, obj, DT_SRC_PARENT);
4470 rc = mdo_layout_change(env, obj, mlc, handle);
4471 mdd_write_unlock(env, obj);
4475 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4476 mdd_write_lock(env, obj, DT_SRC_CHILD);
4478 /* insert dotdot to stripe which points to parent */
4479 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4480 S_IFDIR, dotdot, handle);
4484 /* copy xattrs including linkea */
4485 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4490 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4494 /* don't set nlink from parent */
4495 attr->la_valid &= ~LA_NLINK;
4497 rc = mdo_attr_set(env, stripe, attr, handle);
4501 /* delete dir name from parent */
4502 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4506 /* insert stripe to parent with dir name */
4507 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4508 attr->la_mode, lname->ln_name, handle);
4512 /* destroy dir obj */
4513 rc = mdo_ref_del(env, obj, handle);
4517 rc = mdo_ref_del(env, obj, handle);
4521 rc = mdo_destroy(env, obj, handle);
4527 mdd_write_unlock(env, obj);
4528 mdd_write_unlock(env, pobj);
4534 * shrink directory stripes after migration/merge
4536 int mdd_dir_layout_shrink(const struct lu_env *env,
4537 struct md_object *md_obj,
4538 struct md_layout_change *mlc)
4540 struct mdd_device *mdd = mdo2mdd(md_obj);
4541 struct mdd_thread_info *info = mdd_env_info(env);
4542 struct mdd_object *obj = md2mdd_obj(md_obj);
4543 struct mdd_object *pobj = NULL;
4544 struct mdd_object *stripe = NULL;
4545 struct lu_attr *attr = &info->mti_pattr;
4546 struct lu_fid *fid = &info->mti_fid2;
4547 struct lu_name lname = { NULL };
4548 struct lu_buf lmv_buf = { NULL };
4549 struct lmv_mds_md_v1 *lmv;
4550 struct lmv_user_md *lmu;
4551 struct thandle *handle;
4556 rc = mdd_la_get(env, obj, attr);
4560 if (!S_ISDIR(attr->la_mode))
4563 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4567 lmv = lmv_buf.lb_buf;
4568 if (!lmv_is_sane(lmv))
4571 lmu = mlc->mlc_buf.lb_buf;
4573 /* adjust the default value '0' to '1' */
4574 if (lmu->lum_stripe_count == 0)
4575 lmu->lum_stripe_count = cpu_to_le32(1);
4577 /* these were checked in MDT */
4578 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4579 le32_to_cpu(lmv->lmv_stripe_count));
4580 LASSERT(!lmv_is_splitting(lmv));
4581 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4583 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4584 * to a plain dir, which will cause FID change and namespace update.
4586 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4587 struct linkea_data *ldata = &info->mti_link_data;
4588 char *filename = info->mti_name;
4590 rc = mdd_links_read(env, obj, ldata);
4594 if (ldata->ld_leh->leh_reccount > 1)
4595 GOTO(out, rc = -EINVAL);
4597 linkea_first_entry(ldata);
4599 GOTO(out, rc = -ENODATA);
4601 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4604 /* Note: lname might miss \0 at the end */
4605 snprintf(filename, sizeof(info->mti_name), "%.*s",
4606 lname.ln_namelen, lname.ln_name);
4607 lname.ln_name = filename;
4609 pobj = mdd_object_find(env, mdd, fid);
4616 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4618 stripe = mdd_object_find(env, mdd, fid);
4619 if (IS_ERR(stripe)) {
4620 mdd_object_put(env, pobj);
4622 GOTO(out, rc = PTR_ERR(stripe));
4626 handle = mdd_trans_create(env, mdd);
4628 GOTO(out, rc = PTR_ERR(handle));
4630 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4631 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4633 GOTO(stop_trans, rc);
4635 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4636 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4639 GOTO(stop_trans, rc);
4642 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4645 GOTO(stop_trans, rc);
4647 rc = mdd_trans_start(env, mdd, handle);
4649 GOTO(stop_trans, rc);
4651 mdd_write_lock(env, obj, DT_SRC_PARENT);
4652 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4653 rc = mdo_layout_change(env, obj, mlc, handle);
4654 mdd_write_unlock(env, obj);
4656 GOTO(stop_trans, rc);
4658 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4659 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4662 GOTO(stop_trans, rc);
4665 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4666 XATTR_NAME_LMV, handle);
4667 GOTO(stop_trans, rc);
4670 rc = mdd_trans_stop(env, mdd, rc, handle);
4673 mdd_object_put(env, stripe);
4674 mdd_object_put(env, pobj);
4676 lu_buf_free(&lmv_buf);
4680 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4681 struct mdd_device *mdd,
4682 struct mdd_object *pobj,
4683 struct mdd_object *obj,
4684 struct mdd_object *tobj,
4685 struct md_layout_change *mlc,
4686 struct dt_allocation_hint *hint,
4687 struct thandle *handle)
4689 struct mdd_thread_info *info = mdd_env_info(env);
4690 const struct lu_name *lname = mlc->mlc_name;
4691 struct lu_attr *la = &info->mti_la_for_fix;
4692 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4693 struct linkea_data *ldata = &info->mti_link_data;
4694 struct lmv_mds_md_v1 *lmv;
4698 mlc->mlc_opc = MD_LAYOUT_DETACH;
4699 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4703 memset(ldata, 0, sizeof(*ldata));
4704 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4705 lname, 1, 0, ldata);
4709 count = lum->lum_stripe_count;
4710 lum->lum_stripe_count = 0;
4711 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4713 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4714 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4715 ldata, NULL, NULL, NULL, hint);
4719 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4722 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4724 lmv = (typeof(lmv))info->mti_key;
4725 memset(lmv, 0, sizeof(*lmv));
4726 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4727 lmv->lmv_stripe_count = cpu_to_le32(1);
4728 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4729 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4731 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4732 mlc->mlc_buf.lb_buf = lmv;
4733 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4734 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4738 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4739 mdo_declare_xattr_set);
4743 lum->lum_stripe_count = count;
4744 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4745 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4749 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4753 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4754 S_IFDIR, lname->ln_name, handle);
4758 la->la_valid = LA_CTIME | LA_MTIME;
4759 rc = mdo_declare_attr_set(env, obj, la, handle);
4763 rc = mdo_declare_attr_set(env, pobj, la, handle);
4767 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4773 * plain directory split:
4774 * 1. create \a tobj as plain directory.
4775 * 2. append \a obj as first stripe of \a tobj.
4776 * 3. migrate xattrs from \a obj to \a tobj.
4777 * 4. split \a tobj to specific stripe count.
4779 static int mdd_dir_split_plain(const struct lu_env *env,
4780 struct mdd_device *mdd,
4781 struct mdd_object *pobj,
4782 struct mdd_object *obj,
4783 struct mdd_object *tobj,
4784 struct md_layout_change *mlc,
4785 struct dt_allocation_hint *hint,
4786 struct thandle *handle)
4788 struct mdd_thread_info *info = mdd_env_info(env);
4789 struct lu_attr *pattr = &info->mti_pattr;
4790 struct lu_attr *la = &info->mti_la_for_fix;
4791 const struct lu_name *lname = mlc->mlc_name;
4792 struct linkea_data *ldata = &info->mti_link_data;
4797 /* copy linkea out and set on target later */
4798 rc = mdd_links_read(env, obj, ldata);
4802 mlc->mlc_opc = MD_LAYOUT_DETACH;
4803 rc = mdo_layout_change(env, obj, mlc, handle);
4807 /* don't set nlink from obj */
4808 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4810 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4811 NULL, NULL, NULL, hint, handle, false);
4815 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4819 rc = mdd_links_write(env, tobj, ldata, handle);
4823 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4827 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4828 lname->ln_name, handle);
4832 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4833 la->la_valid = LA_CTIME | LA_MTIME;
4835 mdd_write_lock(env, obj, DT_SRC_CHILD);
4836 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4837 mdd_write_unlock(env, obj);
4841 rc = mdd_la_get(env, pobj, pattr);
4845 la->la_valid = LA_CTIME | LA_MTIME;
4847 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4848 rc = mdd_update_time(env, pobj, pattr, la, handle);
4849 mdd_write_unlock(env, pobj);
4853 /* FID changes, record it as CL_MIGRATE */
4854 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4855 mdd_object_fid(pobj), mdd_object_fid(obj),
4856 mdd_object_fid(pobj), lname, lname, handle);
4860 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4861 struct md_layout_change *mlc)
4863 struct mdd_thread_info *info = mdd_env_info(env);
4864 struct mdd_device *mdd = mdo2mdd(o);
4865 struct mdd_object *obj = md2mdd_obj(o);
4866 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4867 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4868 struct dt_allocation_hint *hint = &info->mti_hint;
4869 bool is_plain = false;
4870 struct thandle *handle;
4875 LASSERT(S_ISDIR(mdd_object_type(obj)));
4877 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4883 handle = mdd_trans_create(env, mdd);
4885 RETURN(PTR_ERR(handle));
4888 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4891 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4892 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4894 GOTO(stop_trans, rc);
4896 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4900 GOTO(stop_trans, rc);
4902 rc = mdd_trans_start(env, mdd, handle);
4904 GOTO(stop_trans, rc);
4907 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4910 mdd_write_lock(env, obj, DT_TGT_CHILD);
4911 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4912 LU_XATTR_CREATE, handle);
4913 mdd_write_unlock(env, obj);
4915 GOTO(stop_trans, rc);
4917 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4918 XATTR_NAME_LMV, handle);
4921 GOTO(stop_trans, rc);
4926 rc = mdd_trans_stop(env, mdd, rc, handle);
4931 const struct md_dir_operations mdd_dir_ops = {
4932 .mdo_is_subdir = mdd_is_subdir,
4933 .mdo_lookup = mdd_lookup,
4934 .mdo_create = mdd_create,
4935 .mdo_rename = mdd_rename,
4936 .mdo_link = mdd_link,
4937 .mdo_unlink = mdd_unlink,
4938 .mdo_create_data = mdd_create_data,
4939 .mdo_migrate = mdd_migrate,