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/
31 * lustre/mdd/mdd_dir.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@intel.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_mds.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_idmap.h>
47 #include "mdd_internal.h"
49 static const char dot[] = ".";
50 static const char dotdot[] = "..";
52 static struct lu_name lname_dotdot = {
53 .ln_name = (char *) dotdot,
54 .ln_namelen = sizeof(dotdot) - 1,
58 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
60 if (!lu_name_is_valid(ln))
62 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
68 /* Get FID from name and parent */
70 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
71 const struct lu_attr *pattr, const struct lu_name *lname,
72 struct lu_fid *fid, unsigned int may_mask)
74 const char *name = lname->ln_name;
75 const struct dt_key *key = (const struct dt_key *)name;
76 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
77 struct dt_object *dir = mdd_object_child(mdd_obj);
82 if (unlikely(mdd_is_dead_obj(mdd_obj)))
85 if (!mdd_object_exists(mdd_obj))
88 if (mdd_object_remote(mdd_obj)) {
89 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
90 mdd_obj_dev_name(mdd_obj),
91 PFID(mdd_object_fid(mdd_obj)));
94 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
99 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
100 dt_try_as_dir(env, dir)))
101 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
108 int mdd_lookup(const struct lu_env *env,
109 struct md_object *pobj, const struct lu_name *lname,
110 struct lu_fid *fid, struct md_op_spec *spec)
112 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
116 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
120 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
121 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
125 /** Read the link EA into a temp buffer.
126 * Uses the mdd_thread_info::mti_link_buf since it is generally large.
127 * A pointer to the buffer is stored in \a ldata::ld_buf.
131 static int __mdd_links_read(const struct lu_env *env,
132 struct mdd_object *mdd_obj,
133 struct linkea_data *ldata)
137 if (!mdd_object_exists(mdd_obj))
140 /* First try a small buf */
141 LASSERT(env != NULL);
142 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
144 if (ldata->ld_buf->lb_buf == NULL)
147 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
149 /* Buf was too small, figure out what we need. */
150 lu_buf_free(ldata->ld_buf);
151 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
155 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
156 if (ldata->ld_buf->lb_buf == NULL)
158 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
162 lu_buf_free(ldata->ld_buf);
163 ldata->ld_buf = NULL;
167 return linkea_init(ldata);
170 int mdd_links_read(const struct lu_env *env,
171 struct mdd_object *mdd_obj,
172 struct linkea_data *ldata)
176 rc = __mdd_links_read(env, mdd_obj, ldata);
178 rc = linkea_init(ldata);
183 static int mdd_links_read_with_rec(const struct lu_env *env,
184 struct mdd_object *mdd_obj,
185 struct linkea_data *ldata)
189 rc = __mdd_links_read(env, mdd_obj, ldata);
191 rc = linkea_init_with_rec(ldata);
197 * Get parent FID of the directory
199 * Read parent FID from linkEA, if that fails, then do lookup
200 * dotdot to get the parent FID.
202 * \param[in] env execution environment
203 * \param[in] obj object from which to find the parent FID
204 * \param[in] attr attribute of the object
205 * \param[out] fid fid to get the parent FID
207 * \retval 0 if getting the parent FID succeeds.
208 * \retval negative errno if getting the parent FID fails.
210 static inline int mdd_parent_fid(const struct lu_env *env,
211 struct mdd_object *obj,
212 const struct lu_attr *attr,
215 struct mdd_thread_info *info = mdd_env_info(env);
216 struct linkea_data ldata = { NULL };
217 struct lu_buf *buf = &info->mti_link_buf;
218 struct lu_name lname;
223 LASSERTF(S_ISDIR(mdd_object_type(obj)),
224 "%s: FID "DFID" is not a directory type = %o\n",
225 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
226 mdd_object_type(obj));
228 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
229 if (buf->lb_buf == NULL)
230 GOTO(lookup, rc = 0);
233 rc = mdd_links_read_with_rec(env, obj, &ldata);
237 /* the obj is not locked, don't cache attributes */
238 mdd_invalidate(env, &obj->mod_obj);
240 LASSERT(ldata.ld_leh != NULL);
241 /* Directory should only have 1 parent */
242 if (ldata.ld_leh->leh_reccount > 1)
245 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
247 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
248 if (likely(fid_is_sane(fid)))
251 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
256 * For root fid use special function, which does not compare version component
257 * of fid. Version component is different for root fids on all MDTs.
259 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
261 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
262 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
266 * return 1: if \a tfid is the fid of the ancestor of \a mo;
268 * otherwise: values < 0, errors.
270 static int mdd_is_parent(const struct lu_env *env,
271 struct mdd_device *mdd,
272 struct mdd_object *mo,
273 const struct lu_attr *attr,
274 const struct lu_fid *tfid)
276 struct mdd_object *mp;
280 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
281 pfid = &mdd_env_info(env)->mti_fid;
283 if (mdd_is_root(mdd, mdd_object_fid(mo)))
286 if (mdd_is_root(mdd, tfid))
289 rc = mdd_parent_fid(env, mo, attr, pfid);
294 if (lu_fid_eq(pfid, tfid))
297 if (mdd_is_root(mdd, pfid))
300 mp = mdd_object_find(env, mdd, pfid);
304 if (!mdd_object_exists(mp)) {
305 mdd_object_put(env, mp);
309 rc = mdd_parent_fid(env, mp, attr, pfid);
310 mdd_object_put(env, mp);
319 * No permission check is needed.
321 * returns 1: if fid is ancestor of @mo;
322 * returns 0: if fid is not an ancestor of @mo;
323 * returns < 0: if error
325 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
326 const struct lu_fid *fid)
328 struct mdd_device *mdd = mdo2mdd(mo);
329 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
333 if (!mdd_object_exists(md2mdd_obj(mo)))
336 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
339 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
343 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
348 * Check that @dir contains no entries except (possibly) dot and dotdot.
353 * -ENOTDIR not a directory object
354 * -ENOTEMPTY not empty
358 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
361 struct dt_object *obj;
362 const struct dt_it_ops *iops;
366 obj = mdd_object_child(dir);
367 if (!dt_try_as_dir(env, obj))
370 iops = &obj->do_index_ops->dio_it;
371 it = iops->init(env, obj, LUDA_64BITHASH);
373 result = iops->get(env, it, (const struct dt_key *)"");
376 for (result = 0, i = 0; result == 0 && i < 3; ++i)
377 result = iops->next(env, it);
380 else if (result == 1)
382 } else if (result == 0)
384 * Huh? Index contains no zero key?
391 result = PTR_ERR(it);
392 /* -ENODEV means no valid stripe */
393 if (result == -ENODEV)
400 * Determine if the target object can be hard linked, and right now it only
401 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
402 * directory nlink count might exceed the maximum link count(see
403 * osd_object_ref_add), so it only check nlink for non-directories.
405 * \param[in] env thread environment
406 * \param[in] obj object being linked to
407 * \param[in] la attributes of \a obj
409 * \retval 0 if \a obj can be hard linked
410 * \retval negative error if \a obj is a directory or has too
413 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
414 const struct lu_attr *la)
416 struct mdd_device *m = mdd_obj2mdd_dev(obj);
421 /* Subdir count limitation can be broken through
422 * (see osd_object_ref_add), so only check non-directory here. */
423 if (!S_ISDIR(la->la_mode) &&
424 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
431 * Check whether it may create the cobj under the pobj.
433 * \param[in] env execution environment
434 * \param[in] pobj the parent directory
435 * \param[in] pattr the attribute of the parent directory
436 * \param[in] cobj the child to be created
437 * \param[in] check_perm if check WRITE|EXEC permission for parent
439 * \retval = 0 create the child under this dir is allowed
440 * \retval negative errno create the child under this dir is
443 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
444 const struct lu_attr *pattr, struct mdd_object *cobj,
450 if (cobj && mdd_object_exists(cobj))
453 if (mdd_is_dead_obj(pobj))
457 rc = mdd_permission_internal_locked(env, pobj, pattr,
458 MAY_WRITE | MAY_EXEC,
464 * Check whether can unlink from the pobj in the case of "cobj == NULL".
466 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
467 const struct lu_attr *pattr, const struct lu_attr *attr)
472 if (mdd_is_dead_obj(pobj))
475 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
478 rc = mdd_permission_internal_locked(env, pobj, pattr,
479 MAY_WRITE | MAY_EXEC,
484 if (pattr->la_flags & LUSTRE_APPEND_FL)
491 * pobj == NULL is remote ops case, under such case, pobj's
492 * VTX feature has been checked already, no need check again.
494 static inline int mdd_is_sticky(const struct lu_env *env,
495 struct mdd_object *pobj,
496 const struct lu_attr *pattr,
497 struct mdd_object *cobj,
498 const struct lu_attr *cattr)
500 struct lu_ucred *uc = lu_ucred_assert(env);
503 LASSERT(pattr != NULL);
504 if (!(pattr->la_mode & S_ISVTX) ||
505 (pattr->la_uid == uc->uc_fsuid))
509 LASSERT(cattr != NULL);
510 if (cattr->la_uid == uc->uc_fsuid)
513 return !md_capable(uc, CAP_FOWNER);
516 static int mdd_may_delete_entry(const struct lu_env *env,
517 struct mdd_object *pobj,
518 const struct lu_attr *pattr,
523 LASSERT(pobj != NULL);
524 if (!mdd_object_exists(pobj))
527 if (mdd_is_dead_obj(pobj))
532 rc = mdd_permission_internal_locked(env, pobj, pattr,
533 MAY_WRITE | MAY_EXEC,
539 if (pattr->la_flags & LUSTRE_APPEND_FL)
546 * Check whether it may delete the cobj from the pobj.
549 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
550 const struct lu_attr *tpattr, struct mdd_object *tobj,
551 const struct lu_attr *tattr, const struct lu_attr *cattr,
552 int check_perm, int check_empty)
558 LASSERT(tpattr != NULL);
559 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
567 if (!mdd_object_exists(tobj))
570 if (mdd_is_dead_obj(tobj))
573 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
576 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
579 /* additional check the rename case */
581 if (S_ISDIR(cattr->la_mode)) {
582 if (!S_ISDIR(tattr->la_mode))
585 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
586 mdd_object_fid(tobj)))
588 } else if (S_ISDIR(tattr->la_mode))
592 if (S_ISDIR(tattr->la_mode) && check_empty)
593 rc = mdd_dir_is_empty(env, tobj);
599 * Check whether it can create the link file(linked to @src_obj) under
600 * the target directory(@tgt_obj), and src_obj has been locked by
603 * \param[in] env execution environment
604 * \param[in] tgt_obj the target directory
605 * \param[in] tattr attributes of target directory
606 * \param[in] lname the link name
607 * \param[in] src_obj source object for link
608 * \param[in] cattr attributes for source object
610 * \retval = 0 it is allowed to create the link file under tgt_obj
611 * \retval negative error not allowed to create the link file
613 static int mdd_link_sanity_check(const struct lu_env *env,
614 struct mdd_object *tgt_obj,
615 const struct lu_attr *tattr,
616 const struct lu_name *lname,
617 struct mdd_object *src_obj,
618 const struct lu_attr *cattr)
620 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
624 if (!mdd_object_exists(src_obj))
627 if (mdd_is_dead_obj(src_obj))
630 /* Local ops, no lookup before link, check filename length here. */
631 rc = mdd_name_check(m, lname);
635 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
638 if (S_ISDIR(mdd_object_type(src_obj)))
641 LASSERT(src_obj != tgt_obj);
642 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
646 rc = __mdd_may_link(env, src_obj, cattr);
651 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
652 const char *name, struct thandle *handle)
654 struct dt_object *next = mdd_object_child(pobj);
658 if (dt_try_as_dir(env, next))
659 rc = dt_delete(env, next, (struct dt_key *)name, handle);
666 static int __mdd_index_insert_only(const struct lu_env *env,
667 struct mdd_object *pobj,
668 const struct lu_fid *lf, __u32 type,
670 struct thandle *handle)
672 struct dt_object *next = mdd_object_child(pobj);
676 if (dt_try_as_dir(env, next)) {
677 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
680 rec->rec_type = type;
681 rc = dt_insert(env, next, (const struct dt_rec *)rec,
682 (const struct dt_key *)name, handle);
689 /* insert named index, add reference if isdir */
690 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
691 const struct lu_fid *lf, __u32 type,
692 const char *name, struct thandle *handle)
697 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
698 if (rc == 0 && S_ISDIR(type)) {
699 mdd_write_lock(env, pobj, DT_TGT_PARENT);
700 mdo_ref_add(env, pobj, handle);
701 mdd_write_unlock(env, pobj);
707 /* delete named index, drop reference if isdir */
708 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
709 const char *name, int is_dir,
710 struct thandle *handle)
715 rc = __mdd_index_delete_only(env, pobj, name, handle);
716 if (rc == 0 && is_dir) {
717 mdd_write_lock(env, pobj, DT_TGT_PARENT);
718 mdo_ref_del(env, pobj, handle);
719 mdd_write_unlock(env, pobj);
725 static int mdd_llog_record_calc_size(const struct lu_env *env,
726 const struct lu_name *tname,
727 const struct lu_name *sname)
729 const struct lu_ucred *uc = lu_ucred(env);
730 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
731 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
734 clf_flags |= CLF_RENAME;
736 if (uc != NULL && uc->uc_jobid[0] != '\0')
737 clf_flags |= CLF_JOBID;
739 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
740 changelog_rec_offset(clf_flags, crfe) +
741 (tname != NULL ? tname->ln_namelen : 0) +
742 (sname != NULL ? 1 + sname->ln_namelen : 0));
745 int mdd_declare_changelog_store(const struct lu_env *env,
746 struct mdd_device *mdd,
747 enum changelog_rec_type type,
748 const struct lu_name *tname,
749 const struct lu_name *sname,
750 struct thandle *handle)
752 struct obd_device *obd = mdd2obd_dev(mdd);
753 struct llog_ctxt *ctxt;
754 struct llog_changelog_rec *rec;
756 struct thandle *llog_th;
760 if (!mdd_changelog_enabled(env, mdd, type))
763 reclen = mdd_llog_record_calc_size(env, tname, sname);
764 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
765 if (buf->lb_buf == NULL)
769 rec->cr_hdr.lrh_len = reclen;
770 rec->cr_hdr.lrh_type = CHANGELOG_REC;
772 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
776 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
778 GOTO(out_put, rc = PTR_ERR(llog_th));
780 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
788 int mdd_changelog_write_rec(const struct lu_env *env,
789 struct llog_handle *loghandle,
790 struct llog_rec_hdr *r,
791 struct llog_cookie *cookie,
792 int idx, struct thandle *th)
796 if (r->lrh_type == CHANGELOG_REC) {
797 struct mdd_device *mdd;
798 struct llog_changelog_rec *rec;
800 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
801 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
803 spin_lock(&mdd->mdd_cl.mc_lock);
804 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
805 spin_unlock(&mdd->mdd_cl.mc_lock);
807 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
811 * if current llog is full, we will generate a new
812 * llog, and since it's actually not an error, let's
813 * avoid increasing index so that userspace apps
814 * should not see a gap in the changelog sequence
816 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
817 spin_lock(&mdd->mdd_cl.mc_lock);
818 ++mdd->mdd_cl.mc_index;
819 spin_unlock(&mdd->mdd_cl.mc_lock);
822 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
829 /** Add a changelog entry \a rec to the changelog llog
832 * \param handle - currently ignored since llogs start their own transaction;
833 * this will hopefully be fixed in llog rewrite
836 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
837 struct llog_changelog_rec *rec, struct thandle *th)
839 struct obd_device *obd = mdd2obd_dev(mdd);
840 struct llog_ctxt *ctxt;
841 struct thandle *llog_th;
844 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
845 changelog_rec_varsize(&rec->cr));
847 /* llog_lvfs_write_rec sets the llog tail len */
848 rec->cr_hdr.lrh_type = CHANGELOG_REC;
849 rec->cr.cr_time = cl_time();
851 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
855 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
857 GOTO(out_put, rc = PTR_ERR(llog_th));
859 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
860 /* nested journal transaction */
861 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
863 /* time to recover some space ?? */
864 if (likely(!mdd->mdd_changelog_gc ||
865 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
866 mdd->mdd_changelog_min_gc_interval >=
867 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
868 /* save a spin_lock trip */
870 spin_lock(&mdd->mdd_cl.mc_lock);
871 if (likely(mdd->mdd_changelog_gc &&
872 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
873 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
874 mdd->mdd_changelog_min_gc_interval)) {
875 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
876 mdd->mdd_changelog_min_free_cat_entries ||
877 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
878 CWARN("%s:%s low on changelog_catalog free entries, "
879 "starting ChangeLog garbage collection thread\n",
881 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
884 /* indicate further kthread run will occur outside
885 * right after current journal transaction filling has
888 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
890 /* next check in mdd_changelog_min_gc_interval anyway
892 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
894 spin_unlock(&mdd->mdd_cl.mc_lock);
902 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
903 const struct lu_fid *sfid,
904 const struct lu_fid *spfid,
905 const struct lu_name *sname)
907 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
910 LASSERT(sfid != NULL);
911 LASSERT(spfid != NULL);
912 LASSERT(sname != NULL);
914 extsize = sname->ln_namelen + 1;
916 rnm->cr_sfid = *sfid;
917 rnm->cr_spfid = *spfid;
919 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
920 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
921 rec->cr_namelen += extsize;
924 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
926 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
928 if (jobid == NULL || jobid[0] == '\0')
931 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
934 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
936 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
938 ef->cr_extra_flags = eflags;
941 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
942 __u64 uid, __u64 gid)
944 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
946 uidgid->cr_uid = uid;
947 uidgid->cr_gid = gid;
950 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
953 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
958 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
960 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
962 omd->cr_openflags = flags;
965 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
966 const char *xattr_name)
968 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
970 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
973 /** Store a namespace change changelog record
974 * If this fails, we must fail the whole transaction; we don't
975 * want the change to commit without the log entry.
976 * \param target - mdd_object of change
977 * \param tpfid - target parent dir/object fid
978 * \param sfid - source object fid
979 * \param spfid - source parent fid
980 * \param tname - target name string
981 * \param sname - source name string
982 * \param handle - transaction handle
984 int mdd_changelog_ns_store(const struct lu_env *env,
985 struct mdd_device *mdd,
986 enum changelog_rec_type type,
987 enum changelog_rec_flags clf_flags,
988 struct mdd_object *target,
989 const struct lu_fid *tpfid,
990 const struct lu_fid *sfid,
991 const struct lu_fid *spfid,
992 const struct lu_name *tname,
993 const struct lu_name *sname,
994 struct thandle *handle)
996 const struct lu_ucred *uc = lu_ucred(env);
997 struct llog_changelog_rec *rec;
1000 __u64 xflags = CLFE_INVALID;
1004 if (!mdd_changelog_enabled(env, mdd, type))
1007 LASSERT(tpfid != NULL);
1008 LASSERT(tname != NULL);
1009 LASSERT(handle != NULL);
1011 reclen = mdd_llog_record_calc_size(env, tname, sname);
1012 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
1013 if (buf->lb_buf == NULL)
1017 clf_flags &= CLF_FLAGMASK;
1018 clf_flags |= CLF_EXTRA_FLAGS;
1021 if (uc->uc_jobid[0] != '\0')
1022 clf_flags |= CLF_JOBID;
1023 xflags |= CLFE_UIDGID;
1028 clf_flags |= CLF_RENAME;
1030 clf_flags |= CLF_VERSION;
1032 rec->cr.cr_flags = clf_flags;
1034 if (clf_flags & CLF_EXTRA_FLAGS) {
1035 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1036 if (xflags & CLFE_UIDGID)
1037 mdd_changelog_rec_extra_uidgid(&rec->cr,
1038 uc->uc_uid, uc->uc_gid);
1039 if (xflags & CLFE_NID)
1040 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1043 rec->cr.cr_type = (__u32)type;
1044 rec->cr.cr_pfid = *tpfid;
1045 rec->cr.cr_namelen = tname->ln_namelen;
1046 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1048 if (clf_flags & CLF_RENAME)
1049 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1051 if (clf_flags & CLF_JOBID)
1052 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1054 if (likely(target != NULL)) {
1055 rec->cr.cr_tfid = *mdd_object_fid(target);
1056 target->mod_cltime = ktime_get();
1058 fid_zero(&rec->cr.cr_tfid);
1061 rc = mdd_changelog_store(env, mdd, rec, handle);
1063 CERROR("%s: cannot store changelog record: type = %d, "
1064 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1065 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1066 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1073 static int __mdd_links_add(const struct lu_env *env,
1074 struct mdd_object *mdd_obj,
1075 struct linkea_data *ldata,
1076 const struct lu_name *lname,
1077 const struct lu_fid *pfid,
1078 int first, int check)
1082 if (ldata->ld_leh == NULL) {
1083 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1087 rc = linkea_data_new(ldata,
1088 &mdd_env_info(env)->mti_link_buf);
1095 rc = linkea_links_find(ldata, lname, pfid);
1096 if (rc && rc != -ENOENT)
1102 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1103 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1107 linkea_add_buf(ldata, lname, tfid);
1110 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1111 linkea_add_buf(ldata, lname, pfid);
1113 return linkea_add_buf(ldata, lname, pfid);
1116 static int __mdd_links_del(const struct lu_env *env,
1117 struct mdd_object *mdd_obj,
1118 struct linkea_data *ldata,
1119 const struct lu_name *lname,
1120 const struct lu_fid *pfid)
1124 if (ldata->ld_leh == NULL) {
1125 rc = mdd_links_read(env, mdd_obj, ldata);
1130 rc = linkea_links_find(ldata, lname, pfid);
1134 linkea_del_buf(ldata, lname);
1138 static int mdd_linkea_prepare(const struct lu_env *env,
1139 struct mdd_object *mdd_obj,
1140 const struct lu_fid *oldpfid,
1141 const struct lu_name *oldlname,
1142 const struct lu_fid *newpfid,
1143 const struct lu_name *newlname,
1144 int first, int check,
1145 struct linkea_data *ldata)
1150 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1153 LASSERT(oldpfid != NULL || newpfid != NULL);
1155 if (mdd_obj->mod_flags & DEAD_OBJ)
1156 /* Unnecessary to update linkEA for dead object. */
1159 if (oldpfid != NULL) {
1160 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1162 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1165 /* No changes done. */
1170 /* If renaming, add the new record */
1171 if (newpfid != NULL)
1172 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1178 int mdd_links_rename(const struct lu_env *env,
1179 struct mdd_object *mdd_obj,
1180 const struct lu_fid *oldpfid,
1181 const struct lu_name *oldlname,
1182 const struct lu_fid *newpfid,
1183 const struct lu_name *newlname,
1184 struct thandle *handle,
1185 struct linkea_data *ldata,
1186 int first, int check)
1191 if (ldata == NULL) {
1192 ldata = &mdd_env_info(env)->mti_link_data;
1193 memset(ldata, 0, sizeof(*ldata));
1194 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1195 newpfid, newlname, first, check, ldata);
1200 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1201 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1207 if (newlname == NULL)
1208 CERROR("link_ea add failed %d "DFID"\n",
1209 rc, PFID(mdd_object_fid(mdd_obj)));
1210 else if (oldpfid == NULL)
1211 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1212 newlname->ln_namelen, newlname->ln_name, rc,
1213 PFID(mdd_object_fid(mdd_obj)));
1214 else if (newpfid == NULL)
1215 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1216 oldlname->ln_namelen, oldlname->ln_name, rc,
1217 PFID(mdd_object_fid(mdd_obj)));
1219 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1220 "\n", oldlname->ln_namelen, oldlname->ln_name,
1221 newlname->ln_namelen, newlname->ln_name, rc,
1222 PFID(mdd_object_fid(mdd_obj)));
1225 if (is_vmalloc_addr(ldata->ld_buf))
1226 /* if we vmalloced a large buffer drop it */
1227 lu_buf_free(ldata->ld_buf);
1232 static inline int mdd_links_add(const struct lu_env *env,
1233 struct mdd_object *mdd_obj,
1234 const struct lu_fid *pfid,
1235 const struct lu_name *lname,
1236 struct thandle *handle,
1237 struct linkea_data *ldata, int first)
1239 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1240 pfid, lname, handle, ldata, first, 0);
1243 static inline int mdd_links_del(const struct lu_env *env,
1244 struct mdd_object *mdd_obj,
1245 const struct lu_fid *pfid,
1246 const struct lu_name *lname,
1247 struct thandle *handle)
1249 return mdd_links_rename(env, mdd_obj, pfid, lname,
1250 NULL, NULL, handle, NULL, 0, 0);
1253 /** Read the link EA into a temp buffer.
1254 * Uses the name_buf since it is generally large.
1255 * \retval IS_ERR err
1256 * \retval ptr to \a lu_buf (always \a mti_link_buf)
1258 struct lu_buf *mdd_links_get(const struct lu_env *env,
1259 struct mdd_object *mdd_obj)
1261 struct linkea_data ldata = { NULL };
1264 rc = mdd_links_read(env, mdd_obj, &ldata);
1265 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1268 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1269 struct linkea_data *ldata, struct thandle *handle)
1271 const struct lu_buf *buf;
1274 if (ldata == NULL || ldata->ld_buf == NULL ||
1275 ldata->ld_leh == NULL)
1278 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1282 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1283 ldata->ld_leh->leh_len);
1284 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1285 if (unlikely(rc == -ENOSPC)) {
1286 rc = linkea_overflow_shrink(ldata);
1294 static int mdd_declare_links_add(const struct lu_env *env,
1295 struct mdd_object *mdd_obj,
1296 struct thandle *handle,
1297 struct linkea_data *ldata)
1303 if (ldata != NULL && ldata->ld_leh != NULL) {
1304 ea_len = ldata->ld_leh->leh_len;
1305 linkea = ldata->ld_buf->lb_buf;
1307 ea_len = MAX_LINKEA_SIZE;
1311 rc = mdo_declare_xattr_set(env, mdd_obj,
1312 mdd_buf_get_const(env, linkea, ea_len),
1313 XATTR_NAME_LINK, 0, handle);
1318 static inline int mdd_declare_links_del(const struct lu_env *env,
1319 struct mdd_object *c,
1320 struct thandle *handle)
1324 /* For directory, the linkEA will be removed together
1325 * with the object. */
1326 if (!S_ISDIR(mdd_object_type(c)))
1327 rc = mdd_declare_links_add(env, c, handle, NULL);
1332 static int mdd_declare_link(const struct lu_env *env,
1333 struct mdd_device *mdd,
1334 struct mdd_object *p,
1335 struct mdd_object *c,
1336 const struct lu_name *name,
1337 struct thandle *handle,
1339 struct linkea_data *data)
1341 struct lu_fid tfid = *mdd_object_fid(c);
1344 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1345 tfid.f_oid = cfs_fail_val;
1347 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1348 name->ln_name, handle);
1352 rc = mdo_declare_ref_add(env, c, handle);
1356 la->la_valid = LA_CTIME | LA_MTIME;
1357 rc = mdo_declare_attr_set(env, p, la, handle);
1361 la->la_valid = LA_CTIME;
1362 rc = mdo_declare_attr_set(env, c, la, handle);
1366 rc = mdd_declare_links_add(env, c, handle, data);
1370 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1376 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1377 struct md_object *src_obj, const struct lu_name *lname,
1380 const char *name = lname->ln_name;
1381 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1382 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1383 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1384 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1385 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1386 struct mdd_device *mdd = mdo2mdd(src_obj);
1387 struct thandle *handle;
1388 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1389 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1393 rc = mdd_la_get(env, mdd_sobj, cattr);
1397 rc = mdd_la_get(env, mdd_tobj, tattr);
1402 * If we are using project inheritance, we only allow hard link
1403 * creation in our tree when the project IDs are the same;
1404 * otherwise the tree quota mechanism could be circumvented.
1406 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1407 (tattr->la_projid != cattr->la_projid))
1410 handle = mdd_trans_create(env, mdd);
1412 GOTO(out_pending, rc = PTR_ERR(handle));
1414 memset(ldata, 0, sizeof(*ldata));
1416 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1417 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1419 /* Note: even this function will change ldata, but it comes from
1420 * thread_info, which is completely temporary and only seen in
1421 * this function, so we do not need reset ldata once it fails.*/
1422 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1423 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1427 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1432 rc = mdd_trans_start(env, mdd, handle);
1436 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1437 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1440 GOTO(out_unlock, rc);
1442 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1443 rc = mdo_ref_add(env, mdd_sobj, handle);
1445 GOTO(out_unlock, rc);
1448 *tfid = *mdd_object_fid(mdd_sobj);
1449 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1450 tfid->f_oid = cfs_fail_val;
1452 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1453 mdd_object_type(mdd_sobj), name, handle);
1455 mdo_ref_del(env, mdd_sobj, handle);
1456 GOTO(out_unlock, rc);
1459 la->la_valid = LA_CTIME | LA_MTIME;
1460 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1462 GOTO(out_unlock, rc);
1464 la->la_valid = LA_CTIME;
1465 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1467 /* Note: The failure of links_add should not cause the
1468 * link failure, so do not check return value. */
1469 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1470 lname, handle, ldata, 0);
1474 mdd_write_unlock(env, mdd_sobj);
1476 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1477 mdd_object_fid(mdd_tobj), NULL,
1478 NULL, lname, NULL, handle);
1480 rc = mdd_trans_stop(env, mdd, rc, handle);
1481 if (is_vmalloc_addr(ldata->ld_buf))
1482 /* if we vmalloced a large buffer drop it */
1483 lu_buf_free(ldata->ld_buf);
1488 static int mdd_mark_orphan_object(const struct lu_env *env,
1489 struct mdd_object *obj, struct thandle *handle,
1492 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1495 attr->la_valid = LA_FLAGS;
1496 attr->la_flags = LUSTRE_ORPHAN_FL;
1499 rc = mdo_declare_attr_set(env, obj, attr, handle);
1501 rc = mdo_attr_set(env, obj, attr, handle);
1506 static int mdd_declare_finish_unlink(const struct lu_env *env,
1507 struct mdd_object *obj,
1508 struct thandle *handle)
1512 /* Sigh, we do not know if the unlink object will become orphan in
1513 * declare phase, but fortunately the flags here does not matter
1514 * in current declare implementation */
1515 rc = mdd_mark_orphan_object(env, obj, handle, true);
1519 rc = mdo_declare_destroy(env, obj, handle);
1523 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1527 return mdd_declare_links_del(env, obj, handle);
1530 /* caller should take a lock before calling */
1531 int mdd_finish_unlink(const struct lu_env *env,
1532 struct mdd_object *obj, struct md_attr *ma,
1533 struct mdd_object *pobj,
1534 const struct lu_name *lname,
1538 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1541 LASSERT(mdd_write_locked(env, obj) != 0);
1543 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1544 /* add new orphan and the object
1545 * will be deleted during mdd_close() */
1546 obj->mod_flags |= DEAD_OBJ;
1547 if (obj->mod_count) {
1548 rc = mdd_orphan_insert(env, obj, th);
1550 CDEBUG(D_HA, "Object "DFID" is inserted into "
1551 "orphan list, open count = %d\n",
1552 PFID(mdd_object_fid(obj)),
1555 CERROR("Object "DFID" fail to be an orphan, "
1556 "open count = %d, maybe cause failed "
1558 PFID(mdd_object_fid(obj)),
1561 /* mark object as an orphan here, not
1562 * before mdd_orphan_insert() as racing
1563 * mdd_la_get() may propagate ORPHAN_OBJ
1564 * causing the asserition */
1565 rc = mdd_mark_orphan_object(env, obj, th, false);
1567 rc = mdo_destroy(env, obj, th);
1569 } else if (!is_dir) {
1570 /* old files may not have link ea; ignore errors */
1571 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1579 * has mdd_write_lock on cobj already, but not on pobj yet
1581 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1582 const struct lu_attr *pattr,
1583 struct mdd_object *cobj,
1584 const struct lu_attr *cattr)
1589 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1594 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1595 struct mdd_object *p, struct mdd_object *c,
1596 const struct lu_name *name, struct md_attr *ma,
1597 struct thandle *handle, int no_name, int is_dir)
1599 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1602 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1603 if (likely(no_name == 0)) {
1604 rc = mdo_declare_index_delete(env, p, name->ln_name,
1611 rc = mdo_declare_ref_del(env, p, handle);
1617 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1618 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1619 la->la_valid = LA_CTIME | LA_MTIME;
1620 rc = mdo_declare_attr_set(env, p, la, handle);
1625 rc = mdo_declare_ref_del(env, c, handle);
1629 rc = mdo_declare_ref_del(env, c, handle);
1633 la->la_valid = LA_CTIME;
1634 rc = mdo_declare_attr_set(env, c, la, handle);
1638 rc = mdd_declare_finish_unlink(env, c, handle);
1642 /* FIXME: need changelog for remove entry */
1643 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1651 * test if a file has an HSM archive
1652 * if HSM attributes are not found in ma update them from
1655 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1656 struct mdd_object *obj,
1661 if (!(ma->ma_valid & MA_HSM)) {
1662 /* no HSM MD provided, read xattr */
1663 struct lu_buf *hsm_buf;
1664 const size_t buflen = sizeof(struct hsm_attrs);
1667 hsm_buf = mdd_buf_get(env, NULL, 0);
1668 lu_buf_alloc(hsm_buf, buflen);
1669 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1670 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1671 lu_buf_free(hsm_buf);
1675 ma->ma_valid |= MA_HSM;
1677 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1683 * Delete name entry and the object.
1684 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1685 * does not exist for this object, and it could only happen during resending
1686 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1687 * is also needed in this case(needed by changelog), so we have to add another
1688 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1689 * the ENOENT failure should be able to be fixed by redo mechanism.
1691 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1692 struct md_object *cobj, const struct lu_name *lname,
1693 struct md_attr *ma, int no_name)
1695 const char *name = lname->ln_name;
1696 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1697 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1698 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1699 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1700 struct mdd_object *mdd_cobj = NULL;
1701 struct mdd_device *mdd = mdo2mdd(pobj);
1702 struct thandle *handle;
1703 int rc, is_dir = 0, cl_flags = 0;
1706 /* let shutdown to start */
1707 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1709 /* cobj == NULL means only delete name entry */
1710 if (likely(cobj != NULL)) {
1711 mdd_cobj = md2mdd_obj(cobj);
1712 if (mdd_object_exists(mdd_cobj) == 0)
1716 rc = mdd_la_get(env, mdd_pobj, pattr);
1720 if (likely(mdd_cobj != NULL)) {
1722 rc = mdd_la_get(env, mdd_cobj, cattr);
1726 is_dir = S_ISDIR(cattr->la_mode);
1727 /* search for an existing archive.
1728 * we should check ahead as the object
1729 * can be destroyed in this transaction */
1730 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1731 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1734 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1738 handle = mdd_trans_create(env, mdd);
1740 RETURN(PTR_ERR(handle));
1742 if (likely(mdd_cobj != NULL))
1743 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1745 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1746 lname, ma, handle, no_name, is_dir);
1750 rc = mdd_trans_start(env, mdd, handle);
1754 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1755 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1760 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1761 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1762 GOTO(cleanup, rc = 0);
1764 if (likely(mdd_cobj != NULL)) {
1765 rc = mdo_ref_del(env, mdd_cobj, handle);
1767 __mdd_index_insert_only(env, mdd_pobj,
1768 mdd_object_fid(mdd_cobj),
1769 mdd_object_type(mdd_cobj),
1776 mdo_ref_del(env, mdd_cobj, handle);
1778 /* fetch updated nlink */
1779 rc = mdd_la_get(env, mdd_cobj, cattr);
1784 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1785 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1787 la->la_valid = LA_CTIME | LA_MTIME;
1788 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1792 /* Enough for only unlink the entry */
1793 if (unlikely(mdd_cobj == NULL))
1796 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1797 /* update ctime of an unlinked file only if it is still
1798 * opened or a link still exists */
1799 la->la_valid = LA_CTIME;
1800 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1805 /* XXX: this transfer to ma will be removed with LOD/OSP */
1806 ma->ma_attr = *cattr;
1807 ma->ma_valid |= MA_INODE;
1808 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1812 /* fetch updated nlink */
1813 rc = mdd_la_get(env, mdd_cobj, cattr);
1814 /* if object is removed then we can't get its attrs,
1816 if (rc == -ENOENT) {
1817 cattr->la_nlink = 0;
1821 if (cattr->la_nlink == 0) {
1822 ma->ma_attr = *cattr;
1823 ma->ma_valid |= MA_INODE;
1828 if (likely(mdd_cobj != NULL))
1829 mdd_write_unlock(env, mdd_cobj);
1832 if (cattr->la_nlink == 0)
1833 cl_flags |= CLF_UNLINK_LAST;
1835 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1837 rc = mdd_changelog_ns_store(env, mdd,
1838 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1839 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1840 lname, NULL, handle);
1843 rc = mdd_trans_stop(env, mdd, rc, handle);
1849 * The permission has been checked when obj created, no need check again.
1851 static int mdd_cd_sanity_check(const struct lu_env *env,
1852 struct mdd_object *obj)
1857 if (!obj || mdd_is_dead_obj(obj))
1863 static int mdd_create_data(const struct lu_env *env,
1864 struct md_object *pobj,
1865 struct md_object *cobj,
1866 const struct md_op_spec *spec,
1869 struct mdd_device *mdd = mdo2mdd(cobj);
1870 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1871 struct mdd_object *son = md2mdd_obj(cobj);
1872 struct thandle *handle;
1873 const struct lu_buf *buf;
1874 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1875 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1879 rc = mdd_cd_sanity_check(env, son);
1883 if (!md_should_create(spec->sp_cr_flags))
1887 * there are following use cases for this function:
1888 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1889 * striping can be specified or not
1892 rc = mdd_la_get(env, son, attr);
1896 /* calling ->ah_make_hint() is used to transfer information from parent */
1897 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1899 handle = mdd_trans_create(env, mdd);
1901 GOTO(out_free, rc = PTR_ERR(handle));
1904 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1905 * Should this be fixed?
1907 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1908 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1909 spec->sp_cr_flags, spec->no_create);
1911 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1912 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1913 spec->u.sp_ea.eadatalen);
1918 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1919 XATTR_NAME_LOV, 0, handle);
1923 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1928 rc = mdd_trans_start(env, mdd, handle);
1932 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1938 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1942 rc = mdd_trans_stop(env, mdd, rc, handle);
1948 static int mdd_declare_object_initialize(const struct lu_env *env,
1949 struct mdd_object *parent,
1950 struct mdd_object *child,
1951 const struct lu_attr *attr,
1952 struct thandle *handle)
1957 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1958 if (!S_ISDIR(attr->la_mode))
1961 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1962 S_IFDIR, dot, handle);
1966 rc = mdo_declare_ref_add(env, child, handle);
1970 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1971 S_IFDIR, dotdot, handle);
1976 static int mdd_object_initialize(const struct lu_env *env,
1977 const struct lu_fid *pfid,
1978 struct mdd_object *child,
1979 struct lu_attr *attr,
1980 struct thandle *handle)
1985 if (S_ISDIR(attr->la_mode)) {
1986 /* Add "." and ".." for newly created dir */
1987 mdo_ref_add(env, child, handle);
1988 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1989 S_IFDIR, dot, handle);
1991 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1994 mdo_ref_del(env, child, handle);
2001 * This function checks whether it can create a file/dir under the
2002 * directory(@pobj). The directory(@pobj) is not being locked by
2005 * \param[in] env execution environment
2006 * \param[in] pobj the directory to create files
2007 * \param[in] pattr the attributes of the directory
2008 * \param[in] lname the name of the created file/dir
2009 * \param[in] cattr the attributes of the file/dir
2010 * \param[in] spec create specification
2012 * \retval = 0 it is allowed to create file/dir under
2014 * \retval negative error not allowed to create file/dir
2015 * under the directory
2017 static int mdd_create_sanity_check(const struct lu_env *env,
2018 struct md_object *pobj,
2019 const struct lu_attr *pattr,
2020 const struct lu_name *lname,
2021 struct lu_attr *cattr,
2022 struct md_op_spec *spec)
2024 struct mdd_thread_info *info = mdd_env_info(env);
2025 struct lu_fid *fid = &info->mti_fid;
2026 struct mdd_object *obj = md2mdd_obj(pobj);
2027 struct mdd_device *m = mdo2mdd(pobj);
2028 bool check_perm = true;
2033 if (mdd_is_dead_obj(obj))
2037 * In some cases this lookup is not needed - we know before if name
2038 * exists or not because MDT performs lookup for it.
2039 * name length check is done in lookup.
2041 if (spec->sp_cr_lookup) {
2043 * Check if the name already exist, though it will be checked in
2044 * _index_insert also, for avoiding rolling back if exists
2047 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2048 MAY_WRITE | MAY_EXEC);
2050 RETURN(rc ? : -EEXIST);
2052 /* Permission is already being checked in mdd_lookup */
2056 if (S_ISDIR(cattr->la_mode) &&
2057 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2058 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2059 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2061 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2062 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2064 CERROR("%s: invalid lmv_user_md: magic = %x, "
2065 "stripe_offset = %d, stripe_count = %u: "
2066 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2067 le32_to_cpu(lum->lum_magic),
2068 (int)le32_to_cpu(lum->lum_stripe_offset),
2069 le32_to_cpu(lum->lum_stripe_count), rc);
2074 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2079 if (pattr->la_mode & S_ISGID) {
2080 struct lu_ucred *uc = lu_ucred(env);
2082 cattr->la_gid = pattr->la_gid;
2084 /* Directories are special, and always inherit S_ISGID */
2085 if (S_ISDIR(cattr->la_mode)) {
2086 cattr->la_mode |= S_ISGID;
2087 cattr->la_valid |= LA_MODE;
2088 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2089 == (S_ISGID | S_IXGRP) &&
2090 !lustre_in_group_p(uc,
2091 (cattr->la_valid & LA_GID) ?
2092 cattr->la_gid : pattr->la_gid) &&
2093 !md_capable(uc, CAP_FSETID)) {
2094 cattr->la_mode &= ~S_ISGID;
2095 cattr->la_valid |= LA_MODE;
2099 /* Inherit project ID from parent directory */
2100 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2101 cattr->la_projid = pattr->la_projid;
2102 if (S_ISDIR(cattr->la_mode)) {
2103 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2104 cattr->la_valid |= LA_FLAGS;
2106 cattr->la_valid |= LA_PROJID;
2109 rc = mdd_name_check(m, lname);
2113 switch (cattr->la_mode & S_IFMT) {
2115 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2117 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2118 RETURN(-ENAMETOOLONG);
2137 static int mdd_declare_create_object(const struct lu_env *env,
2138 struct mdd_device *mdd,
2139 struct mdd_object *p, struct mdd_object *c,
2140 struct lu_attr *attr,
2141 struct thandle *handle,
2142 const struct md_op_spec *spec,
2143 struct lu_buf *def_acl_buf,
2144 struct lu_buf *acl_buf,
2145 struct lu_buf *hsm_buf,
2146 struct dt_allocation_hint *hint)
2148 const struct lu_buf *buf;
2151 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2152 /* ldiskfs OSD needs this information for credit allocation */
2154 hint->dah_acl_len = def_acl_buf->lb_len;
2156 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2161 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2162 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2163 /* if dir, then can inherit default ACl */
2164 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2165 XATTR_NAME_ACL_DEFAULT,
2171 if (acl_buf && acl_buf->lb_len > 0) {
2172 rc = mdo_declare_attr_set(env, c, attr, handle);
2176 rc = mdo_declare_xattr_set(env, c, acl_buf,
2177 XATTR_NAME_ACL_ACCESS, 0, handle);
2182 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2186 /* replay case, create LOV EA from client data */
2187 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2188 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2189 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2190 spec->u.sp_ea.eadatalen);
2191 rc = mdo_declare_xattr_set(env, c, buf,
2192 S_ISDIR(attr->la_mode) ?
2193 XATTR_NAME_LMV : XATTR_NAME_LOV,
2194 LU_XATTR_CREATE, handle);
2198 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2199 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2207 if (S_ISLNK(attr->la_mode)) {
2208 const char *target_name = spec->u.sp_symname;
2209 int sym_len = strlen(target_name);
2210 const struct lu_buf *buf;
2212 buf = mdd_buf_get_const(env, target_name, sym_len);
2213 rc = dt_declare_record_write(env, mdd_object_child(c),
2219 if (spec->sp_cr_file_secctx_name != NULL) {
2220 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2221 spec->sp_cr_file_secctx_size);
2222 rc = mdo_declare_xattr_set(env, c, buf,
2223 spec->sp_cr_file_secctx_name, 0,
2229 if (spec->sp_cr_file_encctx != NULL) {
2230 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2231 spec->sp_cr_file_encctx_size);
2232 rc = mdo_declare_xattr_set(env, c, buf,
2233 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2242 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2243 struct mdd_object *p, struct mdd_object *c,
2244 const struct lu_name *name,
2245 struct lu_attr *attr,
2246 struct thandle *handle,
2247 const struct md_op_spec *spec,
2248 struct linkea_data *ldata,
2249 struct lu_buf *def_acl_buf,
2250 struct lu_buf *acl_buf,
2251 struct lu_buf *hsm_buf,
2252 struct dt_allocation_hint *hint)
2256 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2257 def_acl_buf, acl_buf, hsm_buf, hint);
2261 if (S_ISDIR(attr->la_mode)) {
2262 rc = mdo_declare_ref_add(env, p, handle);
2267 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2268 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2272 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2273 enum changelog_rec_type type;
2275 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2276 attr->la_mode, name->ln_name,
2281 rc = mdd_declare_links_add(env, c, handle, ldata);
2286 la->la_valid = LA_CTIME | LA_MTIME;
2287 rc = mdo_declare_attr_set(env, p, la, handle);
2291 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2292 S_ISREG(attr->la_mode) ? CL_CREATE :
2293 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2295 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2304 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2305 struct lu_attr *la, struct lu_buf *def_acl_buf,
2306 struct lu_buf *acl_buf)
2312 if (S_ISLNK(la->la_mode)) {
2313 acl_buf->lb_len = 0;
2314 def_acl_buf->lb_len = 0;
2318 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2319 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2320 XATTR_NAME_ACL_DEFAULT);
2321 mdd_read_unlock(env, pobj);
2323 /* ACL buffer size is not enough, need realloc */
2324 if (rc > acl_buf->lb_len)
2327 /* If there are default ACL, fix mode/ACL by default ACL */
2328 def_acl_buf->lb_len = rc;
2329 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2330 acl_buf->lb_len = rc;
2331 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2334 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2335 /* If there are no default ACL, fix mode by mask */
2336 struct lu_ucred *uc = lu_ucred(env);
2338 /* The create triggered by MDT internal events, such as
2339 * LFSCK reset, will not contain valid "uc". */
2340 if (unlikely(uc != NULL))
2341 la->la_mode &= ~uc->uc_umask;
2343 acl_buf->lb_len = 0;
2344 def_acl_buf->lb_len = 0;
2351 * Create a metadata object and initialize it, set acl, xattr.
2353 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2354 struct mdd_object *son, struct lu_attr *attr,
2355 struct md_op_spec *spec, struct lu_buf *acl_buf,
2356 struct lu_buf *def_acl_buf,
2357 struct lu_buf *hsm_buf,
2358 struct dt_allocation_hint *hint,
2359 struct thandle *handle, bool initsecctx)
2361 const struct lu_buf *buf;
2364 mdd_write_lock(env, son, DT_TGT_CHILD);
2365 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2370 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2371 * created in declare phase, they also needs to be added to master
2372 * object as sub-directory entry. So it has to initialize the master
2373 * object, then set dir striped EA.(in mdo_xattr_set) */
2374 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2377 GOTO(err_destroy, rc);
2380 * in case of replay we just set LOVEA provided by the client
2381 * XXX: I think it would be interesting to try "old" way where
2382 * MDT calls this xattr_set(LOV) in a different transaction.
2383 * probably this way we code can be made better.
2386 /* During creation, there are only a few cases we need do xattr_set to
2388 * 1. regular file: see comments above.
2389 * 2. dir: inherit default striping or pool settings from parent.
2390 * 3. create striped directory with provided stripeEA.
2391 * 4. create striped directory because inherit default layout from the
2394 if (spec->no_create ||
2395 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2396 S_ISDIR(attr->la_mode)) {
2397 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2398 spec->u.sp_ea.eadatalen);
2399 rc = mdo_xattr_set(env, son, buf,
2400 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2402 LU_XATTR_CREATE, handle);
2404 GOTO(err_destroy, rc);
2407 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2410 memset(&mh, 0, sizeof(mh));
2411 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2412 mh.mh_arch_id = spec->sp_archive_id;
2413 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2414 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2417 GOTO(err_destroy, rc);
2420 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2421 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2422 S_ISDIR(attr->la_mode)) {
2423 /* set default acl */
2424 rc = mdo_xattr_set(env, son, def_acl_buf,
2425 XATTR_NAME_ACL_DEFAULT, 0,
2428 GOTO(err_destroy, rc);
2430 /* set its own acl */
2431 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2432 rc = mdo_xattr_set(env, son, acl_buf,
2433 XATTR_NAME_ACL_ACCESS,
2436 GOTO(err_destroy, rc);
2440 if (S_ISLNK(attr->la_mode)) {
2441 struct dt_object *dt = mdd_object_child(son);
2442 const char *target_name = spec->u.sp_symname;
2443 int sym_len = strlen(target_name);
2446 buf = mdd_buf_get_const(env, target_name, sym_len);
2447 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2451 GOTO(err_initlized, rc = -EFAULT);
2454 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2455 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2456 spec->sp_cr_file_secctx_size);
2457 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2460 GOTO(err_initlized, rc);
2463 if (spec->sp_cr_file_encctx != NULL) {
2464 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2465 spec->sp_cr_file_encctx_size);
2466 rc = mdo_xattr_set(env, son, buf,
2467 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2470 GOTO(err_initlized, rc);
2474 if (unlikely(rc != 0)) {
2476 if (S_ISDIR(attr->la_mode)) {
2477 /* Drop the reference, no need to delete "."/"..",
2478 * because the object to be destroied directly. */
2479 rc2 = mdo_ref_del(env, son, handle);
2483 rc2 = mdo_ref_del(env, son, handle);
2487 mdo_destroy(env, son, handle);
2490 mdd_write_unlock(env, son);
2494 static int mdd_index_delete(const struct lu_env *env,
2495 struct mdd_object *mdd_pobj,
2496 struct lu_attr *cattr,
2497 const struct lu_name *lname)
2499 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2500 struct thandle *handle;
2504 handle = mdd_trans_create(env, mdd);
2506 RETURN(PTR_ERR(handle));
2508 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2513 if (S_ISDIR(cattr->la_mode)) {
2514 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2519 /* Since this will only be used in the error handler path,
2520 * Let's set the thandle to be local and not mess the transno */
2521 handle->th_local = 1;
2522 rc = mdd_trans_start(env, mdd, handle);
2526 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2527 S_ISDIR(cattr->la_mode), handle);
2531 rc = mdd_trans_stop(env, mdd, rc, handle);
2537 * Create object and insert it into namespace.
2539 * Two operations have to be performed:
2541 * - an allocation of a new object (->do_create()), and
2542 * - an insertion into a parent index (->dio_insert()).
2544 * Due to locking, operation order is not important, when both are
2545 * successful, *but* error handling cases are quite different:
2547 * - if insertion is done first, and following object creation fails,
2548 * insertion has to be rolled back, but this operation might fail
2549 * also leaving us with dangling index entry.
2551 * - if creation is done first, is has to be undone if insertion fails,
2552 * leaving us with leaked space, which is not good but not fatal.
2554 * It seems that creation-first is simplest solution, but it is sub-optimal
2560 * case, because second mkdir is bound to create object, only to
2561 * destroy it immediately.
2563 * To avoid this follow local file systems that do double lookup:
2565 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2566 * 1. create (mdd_create_object_internal())
2567 * 2. insert (__mdd_index_insert(), lookup again)
2569 * \param[in] pobj parent object
2570 * \param[in] lname name of child being created
2571 * \param[in,out] child child object being created
2572 * \param[in] spec additional create parameters
2573 * \param[in] ma attributes for new child object
2575 * \retval 0 on success
2576 * \retval negative errno on failure
2578 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2579 const struct lu_name *lname, struct md_object *child,
2580 struct md_op_spec *spec, struct md_attr *ma)
2582 struct mdd_thread_info *info = mdd_env_info(env);
2583 struct lu_attr *la = &info->mti_la_for_fix;
2584 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2585 struct mdd_object *son = md2mdd_obj(child);
2586 struct mdd_device *mdd = mdo2mdd(pobj);
2587 struct lu_attr *attr = &ma->ma_attr;
2588 struct thandle *handle;
2589 struct lu_attr *pattr = &info->mti_pattr;
2590 struct lu_buf acl_buf;
2591 struct lu_buf def_acl_buf;
2592 struct lu_buf hsm_buf;
2593 struct linkea_data *ldata = &info->mti_link_data;
2594 const char *name = lname->ln_name;
2595 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2596 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2601 rc = mdd_la_get(env, mdd_pobj, pattr);
2605 /* Sanity checks before big job. */
2606 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2610 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2611 GOTO(out_free, rc = -EINPROGRESS);
2613 handle = mdd_trans_create(env, mdd);
2615 GOTO(out_free, rc = PTR_ERR(handle));
2618 acl_buf = *lu_buf_check_and_alloc(&info->mti_xattr_buf, acl_size);
2619 if (!acl_buf.lb_buf)
2620 GOTO(out_stop, rc = -ENOMEM);
2621 /* mti_big_buf is also used down below in mdd_changelog_ns_store(),
2622 * but def_acl_buf is finished with it before then
2624 def_acl_buf = *lu_buf_check_and_alloc(&info->mti_big_buf, acl_size);
2625 if (!def_acl_buf.lb_buf)
2626 GOTO(out_stop, rc = -ENOMEM);
2628 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2629 if (unlikely(rc == -ERANGE &&
2630 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2631 /* use maximum-sized xattr buffer for too-big default ACL */
2632 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2634 goto use_bigger_buffer;
2639 if (S_ISDIR(attr->la_mode)) {
2640 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2643 * migrate may create 1-stripe directory, so lod_ah_init()
2644 * doesn't adjust stripe count from lmu.
2646 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2647 lmu->lum_stripe_count = 0;
2650 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2652 memset(ldata, 0, sizeof(*ldata));
2653 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2654 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2657 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2658 &tfid, lname, 1, 0, ldata);
2660 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2661 mdd_object_fid(mdd_pobj),
2662 lname, 1, 0, ldata);
2665 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2666 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2668 memset(&hsm_buf, 0, sizeof(hsm_buf));
2669 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2670 if (hsm_buf.lb_buf == NULL)
2671 GOTO(out_stop, rc = -ENOMEM);
2674 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2675 handle, spec, ldata, &def_acl_buf, &acl_buf,
2680 rc = mdd_trans_start(env, mdd, handle);
2684 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2685 &def_acl_buf, &hsm_buf, hint, handle, true);
2689 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2690 mdd_write_lock(env, son, DT_TGT_CHILD);
2691 son->mod_flags |= VOLATILE_OBJ;
2692 rc = mdd_orphan_insert(env, son, handle);
2693 GOTO(out_volatile, rc);
2695 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2696 attr->la_mode, name, handle);
2698 GOTO(err_created, rc);
2700 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2703 /* update parent directory mtime/ctime */
2705 la->la_valid = LA_CTIME | LA_MTIME;
2706 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2708 GOTO(err_insert, rc);
2714 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2715 rc2 = mdd_orphan_delete(env, son, handle);
2717 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2718 S_ISDIR(attr->la_mode),
2724 mdd_write_lock(env, son, DT_TGT_CHILD);
2725 if (S_ISDIR(attr->la_mode)) {
2726 /* Drop the reference, no need to delete "."/"..",
2727 * because the object is to be destroyed directly. */
2728 rc2 = mdo_ref_del(env, son, handle);
2730 mdd_write_unlock(env, son);
2735 /* For volatile files drop one link immediately, since there is
2736 * no filename in the namespace, and save any error returned. */
2737 rc2 = mdo_ref_del(env, son, handle);
2739 mdd_write_unlock(env, son);
2740 if (unlikely(rc == 0))
2745 /* Don't destroy the volatile object on success */
2746 if (likely(rc != 0))
2747 mdo_destroy(env, son, handle);
2748 mdd_write_unlock(env, son);
2751 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2752 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2753 rc = mdd_changelog_ns_store(env, mdd,
2754 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2755 S_ISREG(attr->la_mode) ? CL_CREATE :
2756 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2757 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2758 lname, NULL, handle);
2760 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2762 /* If creation fails, it is most likely due to the remote update
2763 * failure, because local transaction will mostly succeed at
2764 * this stage. There is no easy way to rollback all of previous
2765 * updates, so let's remove the object from namespace, and
2766 * LFSCK should handle the orphan object. */
2767 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2768 mdd_index_delete(env, mdd_pobj, attr, lname);
2772 if (is_vmalloc_addr(ldata->ld_buf))
2773 /* if we vmalloced a large buffer drop it */
2774 lu_buf_free(ldata->ld_buf);
2776 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2777 lu_buf_free(&hsm_buf);
2779 /* The child object shouldn't be cached anymore */
2781 set_bit(LU_OBJECT_HEARD_BANSHEE,
2782 &child->mo_lu.lo_header->loh_flags);
2786 /* has not mdd_write{read}_lock on any obj yet. */
2787 static int mdd_rename_sanity_check(const struct lu_env *env,
2788 struct mdd_object *src_pobj,
2789 const struct lu_attr *pattr,
2790 struct mdd_object *tgt_pobj,
2791 const struct lu_attr *tpattr,
2792 struct mdd_object *sobj,
2793 const struct lu_attr *cattr,
2794 struct mdd_object *tobj,
2795 const struct lu_attr *tattr)
2800 /* XXX: when get here, sobj must NOT be NULL,
2801 * the other case has been processed in cld_rename
2802 * before mdd_rename and enable MDS_PERM_BYPASS. */
2806 * If we are using project inheritance, we only allow renames
2807 * into our tree when the project IDs are the same; otherwise
2808 * tree quota mechanism would be circumvented.
2810 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2811 tpattr->la_projid != cattr->la_projid) ||
2812 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2813 (pattr->la_projid != tpattr->la_projid)))
2816 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2820 /* XXX: when get here, "tobj == NULL" means tobj must
2821 * NOT exist (neither on remote MDS, such case has been
2822 * processed in cld_rename before mdd_rename and enable
2824 * So check may_create, but not check may_unlink. */
2826 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2827 (src_pobj != tgt_pobj));
2829 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2830 (src_pobj != tgt_pobj), 1);
2832 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2833 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2838 static int mdd_declare_rename(const struct lu_env *env,
2839 struct mdd_device *mdd,
2840 struct mdd_object *mdd_spobj,
2841 struct mdd_object *mdd_tpobj,
2842 struct mdd_object *mdd_sobj,
2843 struct mdd_object *mdd_tobj,
2844 const struct lu_name *sname,
2845 const struct lu_name *tname,
2847 struct linkea_data *ldata,
2848 struct thandle *handle)
2850 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2853 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2854 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2860 /* name from source dir */
2861 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2865 /* .. from source child */
2866 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2867 /* source child can be directory, count by source dir's nlink */
2868 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2871 if (mdd_spobj != mdd_tpobj) {
2872 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2877 rc = mdo_declare_index_insert(env, mdd_sobj,
2878 mdd_object_fid(mdd_tpobj),
2879 S_IFDIR, dotdot, handle);
2884 /* new target child can be directory,
2885 * counted by target dir's nlink */
2886 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2891 la->la_valid = LA_CTIME | LA_MTIME;
2892 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2896 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2900 la->la_valid = LA_CTIME;
2901 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2905 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2910 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2911 mdd_object_type(mdd_sobj),
2912 tname->ln_name, handle);
2916 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2917 /* delete target child in target parent directory */
2918 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2923 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2927 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2928 /* target child can be directory,
2929 * delete "." reference in target child directory */
2930 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2934 /* delete ".." reference in target parent directory */
2935 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2940 la->la_valid = LA_CTIME;
2941 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2945 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2950 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2958 static int mdd_migrate_object(const struct lu_env *env,
2959 struct mdd_object *spobj,
2960 struct mdd_object *tpobj,
2961 struct mdd_object *sobj,
2962 struct mdd_object *tobj,
2963 const struct lu_name *sname,
2964 const struct lu_name *tname,
2965 struct md_op_spec *spec,
2966 struct md_attr *ma);
2968 /* src object can be remote that is why we use only fid and type of object */
2969 static int mdd_rename(const struct lu_env *env,
2970 struct md_object *src_pobj, struct md_object *tgt_pobj,
2971 const struct lu_fid *lf, const struct lu_name *lsname,
2972 struct md_object *tobj, const struct lu_name *ltname,
2975 const char *sname = lsname->ln_name;
2976 const char *tname = ltname->ln_name;
2977 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2978 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2979 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2980 struct mdd_device *mdd = mdo2mdd(src_pobj);
2981 struct mdd_object *mdd_sobj = NULL; /* source object */
2982 struct mdd_object *mdd_tobj = NULL;
2983 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2984 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2985 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2986 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2987 struct thandle *handle;
2988 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2989 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
2990 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
2993 bool tobj_locked = 0;
2994 unsigned cl_flags = 0;
2998 /* let unlink to complete and commit */
2999 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3002 mdd_tobj = md2mdd_obj(tobj);
3004 mdd_sobj = mdd_object_find(env, mdd, lf);
3005 if (IS_ERR(mdd_sobj))
3006 RETURN(PTR_ERR(mdd_sobj));
3008 rc = mdd_la_get(env, mdd_sobj, cattr);
3010 GOTO(out_pending, rc);
3012 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3013 * hard links, and target doesn't exist.
3015 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3016 cattr->la_nlink == 1 && !tobj) {
3017 struct md_op_spec *spec = &mdd_env_info(env)->mti_spec;
3018 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3021 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3024 GOTO(out_pending, rc);
3026 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3027 if (IS_ERR(mdd_tobj))
3028 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3030 memset(spec, 0, sizeof(*spec));
3031 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3032 mdd_tobj, lsname, ltname, spec, ma);
3033 mdd_object_put(env, mdd_tobj);
3034 GOTO(out_pending, rc);
3037 rc = mdd_la_get(env, mdd_spobj, pattr);
3039 GOTO(out_pending, rc);
3042 rc = mdd_la_get(env, mdd_tobj, tattr);
3044 GOTO(out_pending, rc);
3045 /* search for an existing archive.
3046 * we should check ahead as the object
3047 * can be destroyed in this transaction */
3048 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3049 cl_flags |= CLF_RENAME_LAST_EXISTS;
3052 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3054 GOTO(out_pending, rc);
3056 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3057 mdd_sobj, cattr, mdd_tobj, tattr);
3059 GOTO(out_pending, rc);
3061 rc = mdd_name_check(mdd, ltname);
3063 GOTO(out_pending, rc);
3065 handle = mdd_trans_create(env, mdd);
3067 GOTO(out_pending, rc = PTR_ERR(handle));
3069 memset(ldata, 0, sizeof(*ldata));
3070 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3071 lsname, mdd_object_fid(mdd_tpobj), ltname,
3076 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3077 mdd_tobj, lsname, ltname, ma, ldata, handle);
3081 rc = mdd_trans_start(env, mdd, handle);
3085 is_dir = S_ISDIR(cattr->la_mode);
3087 /* Remove source name from source directory */
3088 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3092 /* "mv dir1 dir2" needs "dir1/.." link update */
3093 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3094 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3096 GOTO(fixup_spobj2, rc);
3098 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3101 GOTO(fixup_spobj, rc);
3104 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3105 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3107 /* tname might been renamed to something else */
3108 GOTO(fixup_spobj, rc);
3111 /* Insert new fid with target name into target dir */
3112 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3115 GOTO(fixup_tpobj, rc);
3117 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3118 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3120 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3121 la->la_valid = LA_CTIME;
3122 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3124 GOTO(fixup_tpobj, rc);
3126 /* Update the linkEA for the source object */
3127 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3128 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3129 lsname, mdd_object_fid(mdd_tpobj), ltname,
3130 handle, ldata, 0, 0);
3132 /* Old files might not have EA entry */
3133 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3134 lsname, handle, NULL, 0);
3135 mdd_write_unlock(env, mdd_sobj);
3136 /* We don't fail the transaction if the link ea can't be
3137 updated -- fid2path will use alternate lookup method. */
3140 /* Remove old target object
3141 * For tobj is remote case cmm layer has processed
3142 * and set tobj to NULL then. So when tobj is NOT NULL,
3143 * it must be local one.
3145 if (tobj && mdd_object_exists(mdd_tobj)) {
3146 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3148 if (mdd_is_dead_obj(mdd_tobj)) {
3149 /* shld not be dead, something is wrong */
3150 CERROR("tobj is dead, something is wrong\n");
3154 mdo_ref_del(env, mdd_tobj, handle);
3156 /* Remove dot reference. */
3157 if (S_ISDIR(tattr->la_mode))
3158 mdo_ref_del(env, mdd_tobj, handle);
3161 /* fetch updated nlink */
3162 rc = mdd_la_get(env, mdd_tobj, tattr);
3164 CERROR("%s: Failed to get nlink for tobj "
3166 mdd2obd_dev(mdd)->obd_name,
3167 PFID(tpobj_fid), rc);
3168 GOTO(fixup_tpobj, rc);
3171 la->la_valid = LA_CTIME;
3172 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3174 CERROR("%s: Failed to set ctime for tobj "
3176 mdd2obd_dev(mdd)->obd_name,
3177 PFID(tpobj_fid), rc);
3178 GOTO(fixup_tpobj, rc);
3181 /* XXX: this transfer to ma will be removed with LOD/OSP */
3182 ma->ma_attr = *tattr;
3183 ma->ma_valid |= MA_INODE;
3184 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3187 CERROR("%s: Failed to unlink tobj "
3189 mdd2obd_dev(mdd)->obd_name,
3190 PFID(tpobj_fid), rc);
3191 GOTO(fixup_tpobj, rc);
3194 /* fetch updated nlink */
3195 rc = mdd_la_get(env, mdd_tobj, tattr);
3196 if (rc == -ENOENT) {
3197 /* the object got removed, let's
3198 * return the latest known attributes */
3199 tattr->la_nlink = 0;
3201 } else if (rc != 0) {
3202 CERROR("%s: Failed to get nlink for tobj "
3204 mdd2obd_dev(mdd)->obd_name,
3205 PFID(tpobj_fid), rc);
3206 GOTO(fixup_tpobj, rc);
3208 /* XXX: this transfer to ma will be removed with LOD/OSP */
3209 ma->ma_attr = *tattr;
3210 ma->ma_valid |= MA_INODE;
3212 if (tattr->la_nlink == 0)
3213 cl_flags |= CLF_RENAME_LAST;
3215 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3218 la->la_valid = LA_CTIME | LA_MTIME;
3219 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3221 GOTO(fixup_tpobj, rc);
3223 if (mdd_spobj != mdd_tpobj) {
3224 la->la_valid = LA_CTIME | LA_MTIME;
3225 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3227 GOTO(fixup_tpobj, rc);
3234 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3236 CWARN("tp obj fix error %d\n",rc2);
3238 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3239 !mdd_is_dead_obj(mdd_tobj)) {
3241 mdo_ref_add(env, mdd_tobj, handle);
3243 mdo_ref_add(env, mdd_tobj, handle);
3246 rc2 = __mdd_index_insert(env, mdd_tpobj,
3247 mdd_object_fid(mdd_tobj),
3248 mdd_object_type(mdd_tobj),
3251 CWARN("tp obj fix error: rc = %d\n", rc2);
3256 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3257 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3259 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3260 mdd2obd_dev(mdd)->obd_name, rc2);
3263 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3266 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3267 mdd2obd_dev(mdd)->obd_name, rc2);
3272 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3273 mdd_object_type(mdd_sobj), sname,
3276 CWARN("sp obj fix error: rc = %d\n", rc2);
3281 mdd_write_unlock(env, mdd_tobj);
3284 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3285 mdd_tobj, tpobj_fid, lf, spobj_fid,
3286 ltname, lsname, handle);
3289 rc = mdd_trans_stop(env, mdd, rc, handle);
3292 mdd_object_put(env, mdd_sobj);
3297 * Check whether we should migrate the file/dir
3299 * < 0 permission check failed or other error.
3300 * = 0 the file can be migrated.
3302 static int mdd_migrate_sanity_check(const struct lu_env *env,
3303 struct mdd_device *mdd,
3304 struct mdd_object *spobj,
3305 struct mdd_object *tpobj,
3306 struct mdd_object *sobj,
3307 struct mdd_object *tobj,
3308 const struct lu_attr *spattr,
3309 const struct lu_attr *tpattr,
3310 const struct lu_attr *attr)
3316 if (!mdd_object_remote(sobj)) {
3317 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3318 if (sobj->mod_count > 0) {
3319 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3320 mdd_obj_dev_name(sobj),
3321 PFID(mdd_object_fid(sobj)),
3323 mdd_read_unlock(env, sobj);
3326 mdd_read_unlock(env, sobj);
3329 if (mdd_object_exists(tobj))
3332 rc = mdd_rename_sanity_check(env, spobj, spattr, tpobj, tpattr, sobj,
3337 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3338 struct mdd_object *obj,
3339 const struct lu_buf *buf,
3341 int fl, struct thandle *handle);
3343 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3344 static int mdd_iterate_xattrs(const struct lu_env *env,
3345 struct mdd_object *sobj,
3346 struct mdd_object *tobj,
3348 struct thandle *handle,
3351 struct mdd_thread_info *info = mdd_env_info(env);
3353 struct lu_buf list_xbuf;
3354 struct lu_buf cbxbuf;
3355 struct lu_buf xbuf = { NULL };
3364 /* retrieve xattr list from the old object */
3365 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3366 if (list_xsize == -ENODATA)
3372 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3373 if (info->mti_big_buf.lb_buf == NULL)
3376 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3377 list_xbuf.lb_len = list_xsize;
3378 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3384 xname = list_xbuf.lb_buf;
3386 xlen = strnlen(xname, rem - 1) + 1;
3387 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3388 strcmp(XATTR_NAME_LMV, xname) == 0)
3392 strcmp(XATTR_NAME_LINK, xname) == 0)
3395 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3396 if (xsize == -ENODATA)
3399 GOTO(out, rc = xsize);
3401 lu_buf_check_and_alloc(&xbuf, xsize);
3402 if (xbuf.lb_buf == NULL)
3403 GOTO(out, rc = -ENOMEM);
3405 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3412 cbxbuf.lb_len = xsize;
3414 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3415 if (unlikely(rc == -ENOSPC &&
3416 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3417 rc = linkea_overflow_shrink(
3418 (struct linkea_data *)(cbxbuf.lb_buf));
3419 if (likely(rc > 0)) {
3437 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3438 struct mdd_object *sobj,
3439 struct mdd_object *tobj,
3440 const struct lu_name *sname,
3441 const struct lu_fid *sfid,
3442 const struct lu_name *lname,
3443 const struct lu_fid *fid,
3445 struct thandle *handle);
3447 static int mdd_declare_update_link(const struct lu_env *env,
3448 struct mdd_object *sobj,
3449 struct mdd_object *tobj,
3450 const struct lu_name *tname,
3451 const struct lu_fid *tpfid,
3452 const struct lu_name *lname,
3453 const struct lu_fid *fid,
3455 struct thandle *handle)
3457 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3458 struct mdd_object *pobj;
3462 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3463 !strcmp(tname->ln_name, lname->ln_name))
3466 pobj = mdd_object_find(env, mdd, fid);
3468 return PTR_ERR(pobj);
3471 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3473 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3474 mdd_object_type(sobj),
3475 lname->ln_name, handle);
3476 mdd_object_put(env, pobj);
3480 rc = mdo_declare_ref_add(env, tobj, handle);
3484 rc = mdo_declare_ref_del(env, sobj, handle);
3488 static int mdd_update_link(const struct lu_env *env,
3489 struct mdd_object *sobj,
3490 struct mdd_object *tobj,
3491 const struct lu_name *tname,
3492 const struct lu_fid *tpfid,
3493 const struct lu_name *lname,
3494 const struct lu_fid *fid,
3496 struct thandle *handle)
3498 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3499 struct mdd_object *pobj;
3504 LASSERT(lu_name_is_valid(lname));
3507 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3508 !strncmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3511 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3512 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3514 pobj = mdd_object_find(env, mdd, fid);
3516 CWARN("%s: cannot find obj "DFID": %ld\n",
3517 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3518 RETURN(PTR_ERR(pobj));
3521 if (!mdd_object_exists(pobj)) {
3522 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3523 mdd_object_put(env, pobj);
3527 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3528 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3530 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3531 mdd_object_type(sobj),
3532 lname->ln_name, handle);
3533 mdd_write_unlock(env, pobj);
3534 mdd_object_put(env, pobj);
3538 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3539 rc = mdo_ref_add(env, tobj, handle);
3540 mdd_write_unlock(env, tobj);
3544 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3545 rc = mdo_ref_del(env, sobj, handle);
3546 mdd_write_unlock(env, sobj);
3551 static inline int mdd_fld_lookup(const struct lu_env *env,
3552 struct mdd_device *mdd,
3553 const struct lu_fid *fid,
3556 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
3557 struct seq_server_site *ss;
3560 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3562 range->lsr_flags = LU_SEQ_RANGE_MDT;
3563 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3567 *mdt_index = range->lsr_index;
3572 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3573 struct mdd_object *sobj,
3574 struct mdd_object *tobj,
3575 const struct lu_name *tname,
3576 const struct lu_fid *tpfid,
3577 const struct lu_name *lname,
3578 const struct lu_fid *fid,
3580 struct thandle *handle)
3582 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3583 __u32 source_mdt_index = *(__u32 *)opaque;
3584 __u32 link_mdt_index;
3590 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3591 !strcmp(tname->ln_name, lname->ln_name))
3594 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3598 RETURN(link_mdt_index == source_mdt_index);
3601 static int mdd_iterate_linkea(const struct lu_env *env,
3602 struct mdd_object *sobj,
3603 struct mdd_object *tobj,
3604 const struct lu_name *tname,
3605 const struct lu_fid *tpfid,
3606 struct linkea_data *ldata,
3608 struct thandle *handle,
3611 struct mdd_thread_info *info = mdd_env_info(env);
3612 char *filename = info->mti_name;
3613 struct lu_name lname;
3620 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3621 linkea_next_entry(ldata)) {
3622 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3625 /* Note: lname might miss \0 at the end */
3626 snprintf(filename, sizeof(info->mti_name), "%.*s",
3627 lname.ln_namelen, lname.ln_name);
3628 lname.ln_name = filename;
3630 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3632 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3640 * Prepare linkea, and check whether file needs migrate: if source still has
3641 * link on source MDT, no need to migrate, just update namespace on source and
3644 * \retval 0 do migrate
3645 * \retval 1 don't migrate
3646 * \retval -errno on failure
3648 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3649 struct mdd_device *mdd,
3650 struct mdd_object *spobj,
3651 struct mdd_object *tpobj,
3652 struct mdd_object *sobj,
3653 const struct lu_name *sname,
3654 const struct lu_name *tname,
3655 const struct lu_attr *attr,
3656 struct linkea_data *ldata)
3658 __u32 source_mdt_index;
3663 memset(ldata, 0, sizeof(*ldata));
3664 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3665 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3670 * Then it will check if the file should be migrated. If the file has
3671 * mulitple links, we only need migrate the file if all of its entries
3672 * has been migrated to the remote MDT.
3674 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3677 /* If there are still links locally, don't migrate this file */
3678 LASSERT(ldata->ld_leh != NULL);
3681 * If linkEA is overflow, it means there are some unknown name entries
3682 * under unknown parents, which will prevent the migration.
3684 if (unlikely(ldata->ld_leh->leh_overflow_time))
3687 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3691 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3692 ldata, &source_mdt_index, NULL,
3693 mdd_is_link_on_source_mdt);
3697 static int mdd_declare_migrate_update(const struct lu_env *env,
3698 struct mdd_object *spobj,
3699 struct mdd_object *tpobj,
3700 struct mdd_object *obj,
3701 const struct lu_name *sname,
3702 const struct lu_name *tname,
3703 struct lu_attr *attr,
3704 struct lu_attr *spattr,
3705 struct lu_attr *tpattr,
3706 struct linkea_data *ldata,
3708 struct thandle *handle)
3710 struct mdd_thread_info *info = mdd_env_info(env);
3711 struct lu_attr *la = &info->mti_la_for_fix;
3714 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3718 if (S_ISDIR(attr->la_mode)) {
3719 rc = mdo_declare_ref_del(env, spobj, handle);
3724 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3725 attr->la_mode & S_IFMT,
3726 tname->ln_name, handle);
3730 rc = mdd_declare_links_add(env, obj, handle, ldata);
3734 if (S_ISDIR(attr->la_mode)) {
3735 rc = mdo_declare_ref_add(env, tpobj, handle);
3740 la->la_valid = LA_CTIME | LA_MTIME;
3741 rc = mdo_declare_attr_set(env, spobj, la, handle);
3745 if (tpobj != spobj) {
3746 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3754 static int mdd_declare_migrate_create(const struct lu_env *env,
3755 struct mdd_object *spobj,
3756 struct mdd_object *tpobj,
3757 struct mdd_object *sobj,
3758 struct mdd_object *tobj,
3759 const struct lu_name *sname,
3760 const struct lu_name *tname,
3761 struct lu_attr *spattr,
3762 struct lu_attr *tpattr,
3763 struct lu_attr *attr,
3764 struct lu_buf *sbuf,
3765 struct linkea_data *ldata,
3767 struct md_op_spec *spec,
3768 struct dt_allocation_hint *hint,
3769 struct thandle *handle)
3771 struct mdd_thread_info *info = mdd_env_info(env);
3772 struct md_layout_change *mlc = &info->mti_mlc;
3773 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3778 if (S_ISDIR(attr->la_mode)) {
3779 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3781 mlc->mlc_opc = MD_LAYOUT_DETACH;
3782 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3786 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3787 } else if (S_ISLNK(attr->la_mode)) {
3788 spec->u.sp_symname = sbuf->lb_buf;
3789 } else if (S_ISREG(attr->la_mode)) {
3790 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3791 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3794 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3796 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3797 tname, attr, handle, spec, ldata, NULL, NULL,
3803 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3804 * createb yet, copy from sobj.
3806 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3807 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3808 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3810 if (S_ISDIR(attr->la_mode)) {
3812 /* if sobj is not striped, fake a 1-stripe LMV */
3813 LASSERT(sizeof(info->mti_key) >
3814 lmv_mds_md_size(1, LMV_MAGIC_V1));
3815 lmv = (typeof(lmv))info->mti_key;
3816 memset(lmv, 0, sizeof(*lmv));
3817 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3818 lmv->lmv_stripe_count = cpu_to_le32(1);
3819 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3820 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3821 mdd_object_fid(sobj));
3822 mlc->mlc_buf.lb_buf = lmv;
3823 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3825 mlc->mlc_buf = *sbuf;
3827 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3828 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3833 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3834 mdo_declare_xattr_set);
3838 if (S_ISREG(attr->la_mode)) {
3839 struct lu_buf fid_buf;
3841 handle->th_complex = 1;
3843 /* target may be remote, update PFID via sobj. */
3844 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3845 fid_buf.lb_len = sizeof(struct lu_fid);
3846 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3851 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3856 if (!S_ISDIR(attr->la_mode)) {
3857 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3858 mdd_object_fid(tpobj), ldata, NULL,
3859 handle, mdd_declare_update_link);
3864 if (!S_ISDIR(attr->la_mode) || lmv) {
3865 rc = mdo_declare_ref_del(env, sobj, handle);
3869 if (S_ISDIR(attr->la_mode)) {
3870 rc = mdo_declare_ref_del(env, sobj, handle);
3875 rc = mdo_declare_destroy(env, sobj, handle);
3880 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3881 attr, spattr, tpattr, ldata, ma,
3887 * migrate dirent from \a spobj to \a tpobj.
3889 static int mdd_migrate_update(const struct lu_env *env,
3890 struct mdd_object *spobj,
3891 struct mdd_object *tpobj,
3892 struct mdd_object *obj,
3893 const struct lu_name *sname,
3894 const struct lu_name *tname,
3895 struct lu_attr *attr,
3896 struct lu_attr *spattr,
3897 struct lu_attr *tpattr,
3898 struct linkea_data *ldata,
3900 struct thandle *handle)
3902 struct mdd_thread_info *info = mdd_env_info(env);
3903 struct lu_attr *la = &info->mti_la_for_fix;
3908 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3909 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3910 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3912 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3913 S_ISDIR(attr->la_mode), handle);
3917 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3918 attr->la_mode & S_IFMT,
3919 tname->ln_name, handle);
3923 rc = mdd_links_write(env, obj, ldata, handle);
3927 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3928 la->la_valid = LA_CTIME | LA_MTIME;
3929 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3930 rc = mdd_update_time(env, spobj, spattr, la, handle);
3931 mdd_write_unlock(env, spobj);
3935 if (tpobj != spobj) {
3936 la->la_valid = LA_CTIME | LA_MTIME;
3937 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3938 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3939 mdd_write_unlock(env, tpobj);
3948 * Migrate file/dir to target MDT.
3950 * Create target according to \a spec, and then migrate xattrs, if it's
3951 * directory, migrate source stripes to target.
3953 * \param[in] env execution environment
3954 * \param[in] spobj source parent object
3955 * \param[in] tpobj target parent object
3956 * \param[in] sobj source object
3957 * \param[in] tobj target object
3958 * \param[in] lname file name
3959 * \param[in] spattr source parent attributes
3960 * \param[in] tpattr target parent attributes
3961 * \param[in] attr source attributes
3962 * \param[in] sbuf source LMV buf
3963 * \param[in] spec migrate create spec
3964 * \param[in] hint target creation hint
3965 * \param[in] handle tranasction handle
3967 * \retval 0 on success
3968 * \retval -errno on failure
3970 static int mdd_migrate_create(const struct lu_env *env,
3971 struct mdd_object *spobj,
3972 struct mdd_object *tpobj,
3973 struct mdd_object *sobj,
3974 struct mdd_object *tobj,
3975 const struct lu_name *sname,
3976 const struct lu_name *tname,
3977 struct lu_attr *spattr,
3978 struct lu_attr *tpattr,
3979 struct lu_attr *attr,
3980 const struct lu_buf *sbuf,
3981 struct linkea_data *ldata,
3983 struct md_op_spec *spec,
3984 struct dt_allocation_hint *hint,
3985 struct thandle *handle)
3992 * migrate sobj stripes to tobj if it's directory:
3993 * 1. detach stripes from sobj.
3994 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
3995 * 3. create stripes for tobj, see lod_xattr_set_lmv().
3997 if (S_ISDIR(attr->la_mode)) {
3998 struct mdd_thread_info *info = mdd_env_info(env);
3999 struct md_layout_change *mlc = &info->mti_mlc;
4001 mlc->mlc_opc = MD_LAYOUT_DETACH;
4003 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4004 rc = mdo_layout_change(env, sobj, mlc, handle);
4005 mdd_write_unlock(env, sobj);
4010 /* don't set nlink from sobj */
4011 attr->la_valid &= ~LA_NLINK;
4013 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4014 hint, handle, false);
4018 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4019 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4020 mdd_write_unlock(env, tobj);
4024 /* for regular file, update OST objects XATTR_NAME_FID */
4025 if (S_ISREG(attr->la_mode)) {
4026 struct lu_buf fid_buf;
4028 /* target may be remote, update PFID via sobj. */
4029 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4030 fid_buf.lb_len = sizeof(struct lu_fid);
4031 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4036 /* delete LOV to avoid deleting OST objs when destroying sobj */
4037 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4038 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4039 mdd_write_unlock(env, sobj);
4040 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4041 if (rc && rc != -ENODATA)
4046 /* update links FID */
4047 if (!S_ISDIR(attr->la_mode)) {
4048 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4049 mdd_object_fid(tpobj), ldata,
4050 NULL, handle, mdd_update_link);
4055 /* don't destroy sobj if it's plain directory */
4056 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4057 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4058 rc = mdo_ref_del(env, sobj, handle);
4060 if (S_ISDIR(attr->la_mode))
4061 rc = mdo_ref_del(env, sobj, handle);
4063 rc = mdo_destroy(env, sobj, handle);
4065 mdd_write_unlock(env, sobj);
4070 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4071 spattr, tpattr, ldata, ma, handle);
4076 /* NB: if user issued different migrate command, we can't adjust it silently
4077 * here, because this command will decide target MDT in subdir migration in
4080 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4081 const struct lmv_mds_md_v1 *lmv,
4082 const struct lmv_user_md_v1 *lum,
4083 const struct lu_name *lname)
4085 __u32 lum_stripe_count = lum->lum_stripe_count;
4086 __u32 lmv_hash_type = lmv->lmv_hash_type;
4088 if (!lmv_is_sane(lmv))
4091 /* if stripe_count unspecified, set to 1 */
4092 if (!lum_stripe_count)
4093 lum_stripe_count = cpu_to_le32(1);
4095 lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4097 /* TODO: check specific MDTs */
4098 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4099 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4100 (lum->lum_hash_type && lum->lum_hash_type != lmv_hash_type)) {
4101 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4102 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4103 le32_to_cpu(lmv->lmv_master_mdt_index),
4104 le32_to_cpu(lmv->lmv_migrate_offset),
4105 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4114 * Internal function to migrate directory or file between MDTs.
4116 * migrate source to target in following steps:
4117 * 1. create target, append source stripes after target's if it's directory,
4118 * migrate xattrs and update fid of source links.
4119 * 2. update namespace: migrate dirent from source parent to target parent,
4120 * update file linkea, and destroy source if it's not needed any more.
4122 * \param[in] env execution environment
4123 * \param[in] spobj source parent object
4124 * \param[in] tpobj target parent object
4125 * \param[in] sobj source object
4126 * \param[in] tobj target object
4127 * \param[in] sname source file name
4128 * \param[in] tname target file name
4129 * \param[in] spec target creation spec
4130 * \param[in] ma used to update \a pobj mtime and ctime
4132 * \retval 0 on success
4133 * \retval -errno on failure
4135 static int mdd_migrate_object(const struct lu_env *env,
4136 struct mdd_object *spobj,
4137 struct mdd_object *tpobj,
4138 struct mdd_object *sobj,
4139 struct mdd_object *tobj,
4140 const struct lu_name *sname,
4141 const struct lu_name *tname,
4142 struct md_op_spec *spec,
4145 struct mdd_thread_info *info = mdd_env_info(env);
4146 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4147 struct lu_attr *spattr = &info->mti_pattr;
4148 struct lu_attr *tpattr = &info->mti_tpattr;
4149 struct lu_attr *attr = &info->mti_cattr;
4150 struct linkea_data *ldata = &info->mti_link_data;
4151 struct dt_allocation_hint *hint = &info->mti_hint;
4152 struct lu_buf sbuf = { NULL };
4153 struct lmv_mds_md_v1 *lmv;
4154 struct thandle *handle;
4159 rc = mdd_la_get(env, sobj, attr);
4163 rc = mdd_la_get(env, spobj, spattr);
4167 rc = mdd_la_get(env, tpobj, tpattr);
4171 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4172 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4176 /* if user use default value '0' for stripe_count, we need to
4177 * adjust it to '1' to create a 1-stripe directory.
4179 if (lum->lum_stripe_count == 0)
4180 lum->lum_stripe_count = cpu_to_le32(1);
4182 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4183 if (rc && rc != -ENODATA)
4188 if (!lmv_is_sane(lmv))
4189 GOTO(out, rc = -EBADF);
4190 if (lmv_is_migrating(lmv)) {
4191 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4196 } else if (!S_ISDIR(attr->la_mode)) {
4198 GOTO(out, rc = -EALREADY);
4200 /* update namespace only if @sobj is on MDT where @tpobj is. */
4201 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4202 spec->sp_migrate_nsonly = true;
4204 if (S_ISLNK(attr->la_mode)) {
4205 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4207 GOTO(out, rc = -ENOMEM);
4209 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4212 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4213 mdd2obd_dev(mdd)->obd_name,
4214 PFID(mdd_object_fid(sobj)), rc);
4220 /* linkea needs update upon FID or parent stripe change */
4221 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4222 tname, attr, ldata);
4224 /* update namespace only if @sobj has link on its MDT. */
4225 spec->sp_migrate_nsonly = true;
4229 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4230 spattr, tpattr, attr);
4234 handle = mdd_trans_create(env, mdd);
4236 GOTO(out, rc = PTR_ERR(handle));
4238 if (spec->sp_migrate_nsonly)
4239 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4240 tname, attr, spattr, tpattr,
4243 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4244 sname, tname, spattr, tpattr,
4245 attr, &sbuf, ldata, ma, spec,
4250 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4255 rc = mdd_trans_start(env, mdd, handle);
4259 if (spec->sp_migrate_nsonly)
4260 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4261 attr, spattr, tpattr, ldata, ma,
4264 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4265 tname, spattr, tpattr, attr, &sbuf,
4266 ldata, ma, spec, hint, handle);
4270 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4271 spec->sp_migrate_nsonly ? sobj : tobj,
4272 mdd_object_fid(spobj), mdd_object_fid(sobj),
4273 mdd_object_fid(tpobj), tname, sname,
4280 rc = mdd_trans_stop(env, mdd, rc, handle);
4288 * Migrate directory or file between MDTs.
4290 * \param[in] env execution environment
4291 * \param[in] md_pobj parent master object
4292 * \param[in] md_sobj source object
4293 * \param[in] lname file name
4294 * \param[in] md_tobj target object
4295 * \param[in] spec target creation spec
4296 * \param[in] ma used to update \a pobj mtime and ctime
4298 * \retval 0 on success
4299 * \retval -errno on failure
4301 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4302 struct md_object *md_sobj, const struct lu_name *lname,
4303 struct md_object *md_tobj, struct md_op_spec *spec,
4306 struct mdd_thread_info *info = mdd_env_info(env);
4307 struct mdd_device *mdd = mdo2mdd(md_pobj);
4308 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4309 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4310 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4311 struct mdd_object *spobj = NULL;
4312 struct mdd_object *tpobj = NULL;
4313 struct lu_buf pbuf = { NULL };
4314 struct lu_fid *fid = &info->mti_fid2;
4315 struct lmv_mds_md_v1 *lmv;
4320 /* locate source and target stripe on pobj, which are the real parent */
4321 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4322 if (rc < 0 && rc != -ENODATA)
4329 if (!lmv_is_sane(lmv))
4330 GOTO(out, rc = -EBADF);
4332 /* locate target parent stripe */
4333 /* fail check here to make sure top dir migration succeed. */
4334 if (lmv_is_migrating(lmv) &&
4335 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4336 GOTO(out, rc = -EIO);
4338 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4341 GOTO(out, rc = index);
4343 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4344 tpobj = mdd_object_find(env, mdd, fid);
4346 GOTO(out, rc = PTR_ERR(tpobj));
4348 /* locate source parent stripe */
4349 if (lmv_is_layout_changing(lmv)) {
4350 index = lmv_name_to_stripe_index_old(lmv,
4354 GOTO(out, rc = index);
4356 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4357 spobj = mdd_object_find(env, mdd, fid);
4359 GOTO(out, rc = PTR_ERR(spobj));
4361 /* parent stripe unchanged */
4362 if (spobj == tpobj) {
4363 if (!lmv_is_restriping(lmv))
4364 GOTO(out, rc = -EINVAL);
4365 GOTO(out, rc = -EALREADY);
4369 mdd_object_get(spobj);
4374 mdd_object_get(tpobj);
4375 mdd_object_get(spobj);
4378 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4383 if (!IS_ERR_OR_NULL(spobj))
4384 mdd_object_put(env, spobj);
4385 if (!IS_ERR_OR_NULL(tpobj))
4386 mdd_object_put(env, tpobj);
4392 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4393 struct mdd_object *pobj,
4394 struct mdd_object *obj,
4395 struct mdd_object *stripe,
4396 struct lu_attr *attr,
4397 struct md_layout_change *mlc,
4398 struct lu_name *lname,
4399 struct thandle *handle)
4403 mlc->mlc_opc = MD_LAYOUT_DETACH;
4404 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4408 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4409 S_IFDIR, dotdot, handle);
4413 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4414 mdo_declare_xattr_set);
4418 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4422 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4426 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4430 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4431 attr->la_mode, lname->ln_name, handle);
4435 rc = mdo_declare_ref_del(env, obj, handle);
4439 rc = mdo_declare_ref_del(env, obj, handle);
4443 rc = mdo_declare_destroy(env, obj, handle);
4450 /* transform one-stripe directory to a plain directory */
4451 static int mdd_1sd_collapse(const struct lu_env *env,
4452 struct mdd_object *pobj,
4453 struct mdd_object *obj,
4454 struct mdd_object *stripe,
4455 struct lu_attr *attr,
4456 struct md_layout_change *mlc,
4457 struct lu_name *lname,
4458 struct thandle *handle)
4464 /* replace 1-stripe directory with its stripe */
4465 mlc->mlc_opc = MD_LAYOUT_DETACH;
4467 mdd_write_lock(env, obj, DT_SRC_PARENT);
4468 rc = mdo_layout_change(env, obj, mlc, handle);
4469 mdd_write_unlock(env, obj);
4473 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4474 mdd_write_lock(env, obj, DT_SRC_CHILD);
4476 /* insert dotdot to stripe which points to parent */
4477 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4478 S_IFDIR, dotdot, handle);
4482 /* copy xattrs including linkea */
4483 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4488 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4492 /* don't set nlink from parent */
4493 attr->la_valid &= ~LA_NLINK;
4495 rc = mdo_attr_set(env, stripe, attr, handle);
4499 /* delete dir name from parent */
4500 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4504 /* insert stripe to parent with dir name */
4505 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4506 attr->la_mode, lname->ln_name, handle);
4510 /* destroy dir obj */
4511 rc = mdo_ref_del(env, obj, handle);
4515 rc = mdo_ref_del(env, obj, handle);
4519 rc = mdo_destroy(env, obj, handle);
4525 mdd_write_unlock(env, obj);
4526 mdd_write_unlock(env, pobj);
4532 * shrink directory stripes after migration/merge
4534 int mdd_dir_layout_shrink(const struct lu_env *env,
4535 struct md_object *md_obj,
4536 struct md_layout_change *mlc)
4538 struct mdd_device *mdd = mdo2mdd(md_obj);
4539 struct mdd_thread_info *info = mdd_env_info(env);
4540 struct mdd_object *obj = md2mdd_obj(md_obj);
4541 struct mdd_object *pobj = NULL;
4542 struct mdd_object *stripe = NULL;
4543 struct lu_attr *attr = &info->mti_pattr;
4544 struct lu_fid *fid = &info->mti_fid2;
4545 struct lu_name lname = { NULL };
4546 struct lu_buf lmv_buf = { NULL };
4547 struct lmv_mds_md_v1 *lmv;
4548 struct lmv_user_md *lmu;
4549 struct thandle *handle;
4554 rc = mdd_la_get(env, obj, attr);
4558 if (!S_ISDIR(attr->la_mode))
4561 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4565 lmv = lmv_buf.lb_buf;
4566 if (!lmv_is_sane(lmv))
4569 lmu = mlc->mlc_buf.lb_buf;
4571 /* adjust the default value '0' to '1' */
4572 if (lmu->lum_stripe_count == 0)
4573 lmu->lum_stripe_count = cpu_to_le32(1);
4575 /* these were checked in MDT */
4576 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4577 le32_to_cpu(lmv->lmv_stripe_count));
4578 LASSERT(!lmv_is_splitting(lmv));
4579 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4581 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4582 * to a plain dir, which will cause FID change and namespace update.
4584 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4585 struct linkea_data *ldata = &info->mti_link_data;
4586 char *filename = info->mti_name;
4588 rc = mdd_links_read(env, obj, ldata);
4592 if (ldata->ld_leh->leh_reccount > 1)
4593 GOTO(out, rc = -EINVAL);
4595 linkea_first_entry(ldata);
4597 GOTO(out, rc = -ENODATA);
4599 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4602 /* Note: lname might miss \0 at the end */
4603 snprintf(filename, sizeof(info->mti_name), "%.*s",
4604 lname.ln_namelen, lname.ln_name);
4605 lname.ln_name = filename;
4607 pobj = mdd_object_find(env, mdd, fid);
4614 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4616 stripe = mdd_object_find(env, mdd, fid);
4617 if (IS_ERR(stripe)) {
4618 mdd_object_put(env, pobj);
4620 GOTO(out, rc = PTR_ERR(stripe));
4624 handle = mdd_trans_create(env, mdd);
4626 GOTO(out, rc = PTR_ERR(handle));
4628 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4629 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4631 GOTO(stop_trans, rc);
4633 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4634 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4637 GOTO(stop_trans, rc);
4640 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4643 GOTO(stop_trans, rc);
4645 rc = mdd_trans_start(env, mdd, handle);
4647 GOTO(stop_trans, rc);
4649 mdd_write_lock(env, obj, DT_SRC_PARENT);
4650 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4651 rc = mdo_layout_change(env, obj, mlc, handle);
4652 mdd_write_unlock(env, obj);
4654 GOTO(stop_trans, rc);
4656 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4657 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4660 GOTO(stop_trans, rc);
4663 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4664 XATTR_NAME_LMV, handle);
4665 GOTO(stop_trans, rc);
4668 rc = mdd_trans_stop(env, mdd, rc, handle);
4671 mdd_object_put(env, stripe);
4672 mdd_object_put(env, pobj);
4674 lu_buf_free(&lmv_buf);
4678 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4679 struct mdd_device *mdd,
4680 struct mdd_object *pobj,
4681 struct mdd_object *obj,
4682 struct mdd_object *tobj,
4683 struct md_layout_change *mlc,
4684 struct dt_allocation_hint *hint,
4685 struct thandle *handle)
4687 struct mdd_thread_info *info = mdd_env_info(env);
4688 const struct lu_name *lname = mlc->mlc_name;
4689 struct lu_attr *la = &info->mti_la_for_fix;
4690 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4691 struct linkea_data *ldata = &info->mti_link_data;
4692 struct lmv_mds_md_v1 *lmv;
4696 mlc->mlc_opc = MD_LAYOUT_DETACH;
4697 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4701 memset(ldata, 0, sizeof(*ldata));
4702 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4703 lname, 1, 0, ldata);
4707 count = lum->lum_stripe_count;
4708 lum->lum_stripe_count = 0;
4709 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4711 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4712 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4713 ldata, NULL, NULL, NULL, hint);
4717 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4720 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4722 lmv = (typeof(lmv))info->mti_key;
4723 memset(lmv, 0, sizeof(*lmv));
4724 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4725 lmv->lmv_stripe_count = cpu_to_le32(1);
4726 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4727 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4729 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4730 mlc->mlc_buf.lb_buf = lmv;
4731 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4732 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4736 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4737 mdo_declare_xattr_set);
4741 lum->lum_stripe_count = count;
4742 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4743 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4747 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4751 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4752 S_IFDIR, lname->ln_name, handle);
4756 la->la_valid = LA_CTIME | LA_MTIME;
4757 rc = mdo_declare_attr_set(env, obj, la, handle);
4761 rc = mdo_declare_attr_set(env, pobj, la, handle);
4765 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4771 * plain directory split:
4772 * 1. create \a tobj as plain directory.
4773 * 2. append \a obj as first stripe of \a tobj.
4774 * 3. migrate xattrs from \a obj to \a tobj.
4775 * 4. split \a tobj to specific stripe count.
4777 static int mdd_dir_split_plain(const struct lu_env *env,
4778 struct mdd_device *mdd,
4779 struct mdd_object *pobj,
4780 struct mdd_object *obj,
4781 struct mdd_object *tobj,
4782 struct md_layout_change *mlc,
4783 struct dt_allocation_hint *hint,
4784 struct thandle *handle)
4786 struct mdd_thread_info *info = mdd_env_info(env);
4787 struct lu_attr *pattr = &info->mti_pattr;
4788 struct lu_attr *la = &info->mti_la_for_fix;
4789 const struct lu_name *lname = mlc->mlc_name;
4790 struct linkea_data *ldata = &info->mti_link_data;
4795 /* copy linkea out and set on target later */
4796 rc = mdd_links_read(env, obj, ldata);
4800 mlc->mlc_opc = MD_LAYOUT_DETACH;
4801 rc = mdo_layout_change(env, obj, mlc, handle);
4805 /* don't set nlink from obj */
4806 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4808 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4809 NULL, NULL, NULL, hint, handle, false);
4813 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4817 rc = mdd_links_write(env, tobj, ldata, handle);
4821 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4825 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4826 lname->ln_name, handle);
4830 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4831 la->la_valid = LA_CTIME | LA_MTIME;
4833 mdd_write_lock(env, obj, DT_SRC_CHILD);
4834 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4835 mdd_write_unlock(env, obj);
4839 rc = mdd_la_get(env, pobj, pattr);
4843 la->la_valid = LA_CTIME | LA_MTIME;
4845 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4846 rc = mdd_update_time(env, pobj, pattr, la, handle);
4847 mdd_write_unlock(env, pobj);
4851 /* FID changes, record it as CL_MIGRATE */
4852 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4853 mdd_object_fid(pobj), mdd_object_fid(obj),
4854 mdd_object_fid(pobj), lname, lname, handle);
4858 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4859 struct md_layout_change *mlc)
4861 struct mdd_thread_info *info = mdd_env_info(env);
4862 struct mdd_device *mdd = mdo2mdd(o);
4863 struct mdd_object *obj = md2mdd_obj(o);
4864 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4865 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4866 struct dt_allocation_hint *hint = &info->mti_hint;
4867 bool is_plain = false;
4868 struct thandle *handle;
4873 LASSERT(S_ISDIR(mdd_object_type(obj)));
4875 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4881 handle = mdd_trans_create(env, mdd);
4883 RETURN(PTR_ERR(handle));
4886 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4889 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4890 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4892 GOTO(stop_trans, rc);
4894 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4898 GOTO(stop_trans, rc);
4900 rc = mdd_trans_start(env, mdd, handle);
4902 GOTO(stop_trans, rc);
4905 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4908 mdd_write_lock(env, obj, DT_TGT_CHILD);
4909 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4910 LU_XATTR_CREATE, handle);
4911 mdd_write_unlock(env, obj);
4913 GOTO(stop_trans, rc);
4915 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4916 XATTR_NAME_LMV, handle);
4919 GOTO(stop_trans, rc);
4924 rc = mdd_trans_stop(env, mdd, rc, handle);
4929 const struct md_dir_operations mdd_dir_ops = {
4930 .mdo_is_subdir = mdd_is_subdir,
4931 .mdo_lookup = mdd_lookup,
4932 .mdo_create = mdd_create,
4933 .mdo_rename = mdd_rename,
4934 .mdo_link = mdd_link,
4935 .mdo_unlink = mdd_unlink,
4936 .mdo_create_data = mdd_create_data,
4937 .mdo_migrate = mdd_migrate,