4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_dir.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@intel.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_mds.h>
44 #include <lustre_fid.h>
45 #include <lustre_lmv.h>
46 #include <lustre_idmap.h>
48 #include "mdd_internal.h"
50 static const char dot[] = ".";
51 static const char dotdot[] = "..";
53 static struct lu_name lname_dotdot = {
54 .ln_name = (char *) dotdot,
55 .ln_namelen = sizeof(dotdot) - 1,
59 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
61 if (!lu_name_is_valid(ln))
63 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
69 /* Get FID from name and parent */
71 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_attr *pattr, const struct lu_name *lname,
73 struct lu_fid *fid, unsigned int may_mask)
75 const char *name = lname->ln_name;
76 const struct dt_key *key = (const struct dt_key *)name;
77 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
78 struct dt_object *dir = mdd_object_child(mdd_obj);
83 if (unlikely(mdd_is_dead_obj(mdd_obj)))
86 if (!mdd_object_exists(mdd_obj))
89 if (mdd_object_remote(mdd_obj)) {
90 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
91 mdd_obj_dev_name(mdd_obj),
92 PFID(mdd_object_fid(mdd_obj)));
95 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
100 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101 dt_try_as_dir(env, dir)))
102 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
122 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
126 /** Read the link EA into a temp buffer.
127 * Uses the mdd_thread_info::mti_link_buf since it is generally large.
128 * A pointer to the buffer is stored in \a ldata::ld_buf.
132 static int __mdd_links_read(const struct lu_env *env,
133 struct mdd_object *mdd_obj,
134 struct linkea_data *ldata)
138 if (!mdd_object_exists(mdd_obj))
141 /* First try a small buf */
142 LASSERT(env != NULL);
143 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
145 if (ldata->ld_buf->lb_buf == NULL)
148 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
150 /* Buf was too small, figure out what we need. */
151 lu_buf_free(ldata->ld_buf);
152 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
156 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
157 if (ldata->ld_buf->lb_buf == NULL)
159 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
163 lu_buf_free(ldata->ld_buf);
164 ldata->ld_buf = NULL;
168 return linkea_init(ldata);
171 int mdd_links_read(const struct lu_env *env,
172 struct mdd_object *mdd_obj,
173 struct linkea_data *ldata)
177 rc = __mdd_links_read(env, mdd_obj, ldata);
179 rc = linkea_init(ldata);
184 static int mdd_links_read_with_rec(const struct lu_env *env,
185 struct mdd_object *mdd_obj,
186 struct linkea_data *ldata)
190 rc = __mdd_links_read(env, mdd_obj, ldata);
192 rc = linkea_init_with_rec(ldata);
198 * Get parent FID of the directory
200 * Read parent FID from linkEA, if that fails, then do lookup
201 * dotdot to get the parent FID.
203 * \param[in] env execution environment
204 * \param[in] obj object from which to find the parent FID
205 * \param[in] attr attribute of the object
206 * \param[out] fid fid to get the parent FID
208 * \retval 0 if getting the parent FID succeeds.
209 * \retval negative errno if getting the parent FID fails.
211 static inline int mdd_parent_fid(const struct lu_env *env,
212 struct mdd_object *obj,
213 const struct lu_attr *attr,
216 struct mdd_thread_info *info = mdd_env_info(env);
217 struct linkea_data ldata = { NULL };
218 struct lu_buf *buf = &info->mti_link_buf;
219 struct lu_name lname;
224 LASSERTF(S_ISDIR(mdd_object_type(obj)),
225 "%s: FID "DFID" is not a directory type = %o\n",
226 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
227 mdd_object_type(obj));
229 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
230 if (buf->lb_buf == NULL)
231 GOTO(lookup, rc = 0);
234 rc = mdd_links_read_with_rec(env, obj, &ldata);
238 /* the obj is not locked, don't cache attributes */
239 mdd_invalidate(env, &obj->mod_obj);
241 LASSERT(ldata.ld_leh != NULL);
242 /* Directory should only have 1 parent */
243 if (ldata.ld_leh->leh_reccount > 1)
246 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
248 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
249 if (likely(fid_is_sane(fid)))
252 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
257 * For root fid use special function, which does not compare version component
258 * of fid. Version component is different for root fids on all MDTs.
260 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
262 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
263 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
267 * return 1: if \a tfid is the fid of the ancestor of \a mo;
269 * otherwise: values < 0, errors.
271 static int mdd_is_parent(const struct lu_env *env,
272 struct mdd_device *mdd,
273 struct mdd_object *mo,
274 const struct lu_attr *attr,
275 const struct lu_fid *tfid)
277 struct mdd_object *mp;
281 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
282 pfid = &mdd_env_info(env)->mti_fid;
284 if (mdd_is_root(mdd, mdd_object_fid(mo)))
287 if (mdd_is_root(mdd, tfid))
290 rc = mdd_parent_fid(env, mo, attr, pfid);
295 if (lu_fid_eq(pfid, tfid))
298 if (mdd_is_root(mdd, pfid))
301 mp = mdd_object_find(env, mdd, pfid);
305 if (!mdd_object_exists(mp)) {
306 mdd_object_put(env, mp);
310 rc = mdd_parent_fid(env, mp, attr, pfid);
311 mdd_object_put(env, mp);
320 * No permission check is needed.
322 * returns 1: if fid is ancestor of @mo;
323 * returns 0: if fid is not an ancestor of @mo;
324 * returns < 0: if error
326 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
327 const struct lu_fid *fid)
329 struct mdd_device *mdd = mdo2mdd(mo);
330 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
334 if (!mdd_object_exists(md2mdd_obj(mo)))
337 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
340 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
344 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
349 * Check that @dir contains no entries except (possibly) dot and dotdot.
354 * -ENOTDIR not a directory object
355 * -ENOTEMPTY not empty
359 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
362 struct dt_object *obj;
363 const struct dt_it_ops *iops;
367 obj = mdd_object_child(dir);
368 if (!dt_try_as_dir(env, obj))
371 iops = &obj->do_index_ops->dio_it;
372 it = iops->init(env, obj, LUDA_64BITHASH);
374 result = iops->get(env, it, (const struct dt_key *)"");
377 for (result = 0, i = 0; result == 0 && i < 3; ++i)
378 result = iops->next(env, it);
381 else if (result == 1)
383 } else if (result == 0)
385 * Huh? Index contains no zero key?
392 result = PTR_ERR(it);
393 /* -ENODEV means no valid stripe */
394 if (result == -ENODEV)
401 * Determine if the target object can be hard linked, and right now it only
402 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
403 * directory nlink count might exceed the maximum link count(see
404 * osd_object_ref_add), so it only check nlink for non-directories.
406 * \param[in] env thread environment
407 * \param[in] obj object being linked to
408 * \param[in] la attributes of \a obj
410 * \retval 0 if \a obj can be hard linked
411 * \retval negative error if \a obj is a directory or has too
414 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
415 const struct lu_attr *la)
417 struct mdd_device *m = mdd_obj2mdd_dev(obj);
422 /* Subdir count limitation can be broken through
423 * (see osd_object_ref_add), so only check non-directory here. */
424 if (!S_ISDIR(la->la_mode) &&
425 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
432 * Check whether it may create the cobj under the pobj.
434 * \param[in] env execution environment
435 * \param[in] pobj the parent directory
436 * \param[in] pattr the attribute of the parent directory
437 * \param[in] cobj the child to be created
438 * \param[in] check_perm if check WRITE|EXEC permission for parent
440 * \retval = 0 create the child under this dir is allowed
441 * \retval negative errno create the child under this dir is
444 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
445 const struct lu_attr *pattr, struct mdd_object *cobj,
451 if (cobj && mdd_object_exists(cobj))
454 if (mdd_is_dead_obj(pobj))
458 rc = mdd_permission_internal_locked(env, pobj, pattr,
459 MAY_WRITE | MAY_EXEC,
465 * Check whether can unlink from the pobj in the case of "cobj == NULL".
467 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
468 const struct lu_attr *pattr, const struct lu_attr *attr)
473 if (mdd_is_dead_obj(pobj))
476 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
479 rc = mdd_permission_internal_locked(env, pobj, pattr,
480 MAY_WRITE | MAY_EXEC,
485 if (pattr->la_flags & LUSTRE_APPEND_FL)
492 * pobj == NULL is remote ops case, under such case, pobj's
493 * VTX feature has been checked already, no need check again.
495 static inline int mdd_is_sticky(const struct lu_env *env,
496 struct mdd_object *pobj,
497 const struct lu_attr *pattr,
498 struct mdd_object *cobj,
499 const struct lu_attr *cattr)
501 struct lu_ucred *uc = lu_ucred_assert(env);
504 LASSERT(pattr != NULL);
505 if (!(pattr->la_mode & S_ISVTX) ||
506 (pattr->la_uid == uc->uc_fsuid))
510 LASSERT(cattr != NULL);
511 if (cattr->la_uid == uc->uc_fsuid)
514 return !md_capable(uc, CAP_FOWNER);
517 static int mdd_may_delete_entry(const struct lu_env *env,
518 struct mdd_object *pobj,
519 const struct lu_attr *pattr,
524 LASSERT(pobj != NULL);
525 if (!mdd_object_exists(pobj))
528 if (mdd_is_dead_obj(pobj))
533 rc = mdd_permission_internal_locked(env, pobj, pattr,
534 MAY_WRITE | MAY_EXEC,
540 if (pattr->la_flags & LUSTRE_APPEND_FL)
547 * Check whether it may delete the cobj from the pobj.
550 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
551 const struct lu_attr *tpattr, struct mdd_object *tobj,
552 const struct lu_attr *tattr, const struct lu_attr *cattr,
553 int check_perm, int check_empty)
559 LASSERT(tpattr != NULL);
560 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
568 if (!mdd_object_exists(tobj))
571 if (mdd_is_dead_obj(tobj))
574 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
577 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
580 /* additional check the rename case */
582 if (S_ISDIR(cattr->la_mode)) {
583 if (!S_ISDIR(tattr->la_mode))
586 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
587 mdd_object_fid(tobj)))
589 } else if (S_ISDIR(tattr->la_mode))
593 if (S_ISDIR(tattr->la_mode) && check_empty)
594 rc = mdd_dir_is_empty(env, tobj);
600 * Check whether it can create the link file(linked to @src_obj) under
601 * the target directory(@tgt_obj), and src_obj has been locked by
604 * \param[in] env execution environment
605 * \param[in] tgt_obj the target directory
606 * \param[in] tattr attributes of target directory
607 * \param[in] lname the link name
608 * \param[in] src_obj source object for link
609 * \param[in] cattr attributes for source object
611 * \retval = 0 it is allowed to create the link file under tgt_obj
612 * \retval negative error not allowed to create the link file
614 static int mdd_link_sanity_check(const struct lu_env *env,
615 struct mdd_object *tgt_obj,
616 const struct lu_attr *tattr,
617 const struct lu_name *lname,
618 struct mdd_object *src_obj,
619 const struct lu_attr *cattr)
621 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
625 if (!mdd_object_exists(src_obj))
628 if (mdd_is_dead_obj(src_obj))
631 /* Local ops, no lookup before link, check filename length here. */
632 rc = mdd_name_check(m, lname);
636 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
639 if (S_ISDIR(mdd_object_type(src_obj)))
642 LASSERT(src_obj != tgt_obj);
643 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
647 rc = __mdd_may_link(env, src_obj, cattr);
652 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
653 const char *name, struct thandle *handle)
655 struct dt_object *next = mdd_object_child(pobj);
659 if (dt_try_as_dir(env, next))
660 rc = dt_delete(env, next, (struct dt_key *)name, handle);
667 static int __mdd_index_insert_only(const struct lu_env *env,
668 struct mdd_object *pobj,
669 const struct lu_fid *lf, __u32 type,
671 struct thandle *handle)
673 struct dt_object *next = mdd_object_child(pobj);
677 if (dt_try_as_dir(env, next)) {
678 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
681 rec->rec_type = type;
682 rc = dt_insert(env, next, (const struct dt_rec *)rec,
683 (const struct dt_key *)name, handle);
690 /* insert named index, add reference if isdir */
691 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
692 const struct lu_fid *lf, __u32 type,
693 const char *name, struct thandle *handle)
698 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
699 if (rc == 0 && S_ISDIR(type)) {
700 mdd_write_lock(env, pobj, DT_TGT_PARENT);
701 mdo_ref_add(env, pobj, handle);
702 mdd_write_unlock(env, pobj);
708 /* delete named index, drop reference if isdir */
709 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
710 const char *name, int is_dir,
711 struct thandle *handle)
716 rc = __mdd_index_delete_only(env, pobj, name, handle);
717 if (rc == 0 && is_dir) {
718 mdd_write_lock(env, pobj, DT_TGT_PARENT);
719 mdo_ref_del(env, pobj, handle);
720 mdd_write_unlock(env, pobj);
726 static int mdd_llog_record_calc_size(const struct lu_env *env,
727 const struct lu_name *tname,
728 const struct lu_name *sname)
730 const struct lu_ucred *uc = lu_ucred(env);
731 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
732 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
735 clf_flags |= CLF_RENAME;
737 if (uc != NULL && uc->uc_jobid[0] != '\0')
738 clf_flags |= CLF_JOBID;
740 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
741 changelog_rec_offset(clf_flags, crfe) +
742 (tname != NULL ? tname->ln_namelen : 0) +
743 (sname != NULL ? 1 + sname->ln_namelen : 0));
746 int mdd_declare_changelog_store(const struct lu_env *env,
747 struct mdd_device *mdd,
748 enum changelog_rec_type type,
749 const struct lu_name *tname,
750 const struct lu_name *sname,
751 struct thandle *handle)
753 struct obd_device *obd = mdd2obd_dev(mdd);
754 struct llog_ctxt *ctxt;
755 struct llog_changelog_rec *rec;
757 struct thandle *llog_th;
761 if (!mdd_changelog_enabled(env, mdd, type))
764 reclen = mdd_llog_record_calc_size(env, tname, sname);
765 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
766 if (buf->lb_buf == NULL)
770 rec->cr_hdr.lrh_len = reclen;
771 rec->cr_hdr.lrh_type = CHANGELOG_REC;
773 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
777 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
779 GOTO(out_put, rc = PTR_ERR(llog_th));
781 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
789 int mdd_changelog_write_rec(const struct lu_env *env,
790 struct llog_handle *loghandle,
791 struct llog_rec_hdr *r,
792 struct llog_cookie *cookie,
793 int idx, struct thandle *th)
797 if (r->lrh_type == CHANGELOG_REC) {
798 struct mdd_device *mdd;
799 struct llog_changelog_rec *rec;
801 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
802 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
804 spin_lock(&mdd->mdd_cl.mc_lock);
805 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
806 spin_unlock(&mdd->mdd_cl.mc_lock);
808 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
812 * if current llog is full, we will generate a new
813 * llog, and since it's actually not an error, let's
814 * avoid increasing index so that userspace apps
815 * should not see a gap in the changelog sequence
817 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
818 spin_lock(&mdd->mdd_cl.mc_lock);
819 ++mdd->mdd_cl.mc_index;
820 spin_unlock(&mdd->mdd_cl.mc_lock);
823 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
830 /** Add a changelog entry \a rec to the changelog llog
833 * \param handle - currently ignored since llogs start their own transaction;
834 * this will hopefully be fixed in llog rewrite
837 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
838 struct llog_changelog_rec *rec, struct thandle *th)
840 struct obd_device *obd = mdd2obd_dev(mdd);
841 struct llog_ctxt *ctxt;
842 struct thandle *llog_th;
845 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
846 changelog_rec_varsize(&rec->cr));
848 /* llog_lvfs_write_rec sets the llog tail len */
849 rec->cr_hdr.lrh_type = CHANGELOG_REC;
850 rec->cr.cr_time = cl_time();
852 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
856 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
858 GOTO(out_put, rc = PTR_ERR(llog_th));
860 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
861 /* nested journal transaction */
862 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
864 /* time to recover some space ?? */
865 if (likely(!mdd->mdd_changelog_gc ||
866 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
867 mdd->mdd_changelog_min_gc_interval >=
868 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
869 /* save a spin_lock trip */
871 spin_lock(&mdd->mdd_cl.mc_lock);
872 if (likely(mdd->mdd_changelog_gc &&
873 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
874 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
875 mdd->mdd_changelog_min_gc_interval)) {
876 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
877 mdd->mdd_changelog_min_free_cat_entries ||
878 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
879 CWARN("%s:%s low on changelog_catalog free entries, "
880 "starting ChangeLog garbage collection thread\n",
882 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
885 /* indicate further kthread run will occur outside
886 * right after current journal transaction filling has
889 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
891 /* next check in mdd_changelog_min_gc_interval anyway
893 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
895 spin_unlock(&mdd->mdd_cl.mc_lock);
903 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
904 const struct lu_fid *sfid,
905 const struct lu_fid *spfid,
906 const struct lu_name *sname)
908 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
911 LASSERT(sfid != NULL);
912 LASSERT(spfid != NULL);
913 LASSERT(sname != NULL);
915 extsize = sname->ln_namelen + 1;
917 rnm->cr_sfid = *sfid;
918 rnm->cr_spfid = *spfid;
920 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
921 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
922 rec->cr_namelen += extsize;
925 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
927 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
929 if (jobid == NULL || jobid[0] == '\0')
932 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
935 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
937 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
939 ef->cr_extra_flags = eflags;
942 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
943 __u64 uid, __u64 gid)
945 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
947 uidgid->cr_uid = uid;
948 uidgid->cr_gid = gid;
951 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
954 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
959 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
961 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
963 omd->cr_openflags = flags;
966 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
967 const char *xattr_name)
969 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
971 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
974 /** Store a namespace change changelog record
975 * If this fails, we must fail the whole transaction; we don't
976 * want the change to commit without the log entry.
977 * \param target - mdd_object of change
978 * \param tpfid - target parent dir/object fid
979 * \param sfid - source object fid
980 * \param spfid - source parent fid
981 * \param tname - target name string
982 * \param sname - source name string
983 * \param handle - transaction handle
985 int mdd_changelog_ns_store(const struct lu_env *env,
986 struct mdd_device *mdd,
987 enum changelog_rec_type type,
988 enum changelog_rec_flags clf_flags,
989 struct mdd_object *target,
990 const struct lu_fid *tpfid,
991 const struct lu_fid *sfid,
992 const struct lu_fid *spfid,
993 const struct lu_name *tname,
994 const struct lu_name *sname,
995 struct thandle *handle)
997 const struct lu_ucred *uc = lu_ucred(env);
998 struct llog_changelog_rec *rec;
1001 __u64 xflags = CLFE_INVALID;
1005 if (!mdd_changelog_enabled(env, mdd, type))
1008 LASSERT(tpfid != NULL);
1009 LASSERT(tname != NULL);
1010 LASSERT(handle != NULL);
1012 reclen = mdd_llog_record_calc_size(env, tname, sname);
1013 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
1014 if (buf->lb_buf == NULL)
1018 clf_flags &= CLF_FLAGMASK;
1019 clf_flags |= CLF_EXTRA_FLAGS;
1022 if (uc->uc_jobid[0] != '\0')
1023 clf_flags |= CLF_JOBID;
1024 xflags |= CLFE_UIDGID;
1029 clf_flags |= CLF_RENAME;
1031 clf_flags |= CLF_VERSION;
1033 rec->cr.cr_flags = clf_flags;
1035 if (clf_flags & CLF_EXTRA_FLAGS) {
1036 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1037 if (xflags & CLFE_UIDGID)
1038 mdd_changelog_rec_extra_uidgid(&rec->cr,
1039 uc->uc_uid, uc->uc_gid);
1040 if (xflags & CLFE_NID)
1041 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1044 rec->cr.cr_type = (__u32)type;
1045 rec->cr.cr_pfid = *tpfid;
1046 rec->cr.cr_namelen = tname->ln_namelen;
1047 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1049 if (clf_flags & CLF_RENAME)
1050 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1052 if (clf_flags & CLF_JOBID)
1053 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1055 if (likely(target != NULL)) {
1056 rec->cr.cr_tfid = *mdd_object_fid(target);
1057 target->mod_cltime = ktime_get();
1059 fid_zero(&rec->cr.cr_tfid);
1062 rc = mdd_changelog_store(env, mdd, rec, handle);
1064 CERROR("%s: cannot store changelog record: type = %d, "
1065 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1066 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1067 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1074 static int __mdd_links_add(const struct lu_env *env,
1075 struct mdd_object *mdd_obj,
1076 struct linkea_data *ldata,
1077 const struct lu_name *lname,
1078 const struct lu_fid *pfid,
1079 int first, int check)
1083 if (ldata->ld_leh == NULL) {
1084 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1088 rc = linkea_data_new(ldata,
1089 &mdd_env_info(env)->mti_link_buf);
1096 rc = linkea_links_find(ldata, lname, pfid);
1097 if (rc && rc != -ENOENT)
1103 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1104 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1108 linkea_add_buf(ldata, lname, tfid);
1111 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1112 linkea_add_buf(ldata, lname, pfid);
1114 return linkea_add_buf(ldata, lname, pfid);
1117 static int __mdd_links_del(const struct lu_env *env,
1118 struct mdd_object *mdd_obj,
1119 struct linkea_data *ldata,
1120 const struct lu_name *lname,
1121 const struct lu_fid *pfid)
1125 if (ldata->ld_leh == NULL) {
1126 rc = mdd_links_read(env, mdd_obj, ldata);
1131 rc = linkea_links_find(ldata, lname, pfid);
1135 linkea_del_buf(ldata, lname);
1139 static int mdd_linkea_prepare(const struct lu_env *env,
1140 struct mdd_object *mdd_obj,
1141 const struct lu_fid *oldpfid,
1142 const struct lu_name *oldlname,
1143 const struct lu_fid *newpfid,
1144 const struct lu_name *newlname,
1145 int first, int check,
1146 struct linkea_data *ldata)
1151 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1154 LASSERT(oldpfid != NULL || newpfid != NULL);
1156 if (mdd_obj->mod_flags & DEAD_OBJ)
1157 /* Unnecessary to update linkEA for dead object. */
1160 if (oldpfid != NULL) {
1161 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1163 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1166 /* No changes done. */
1171 /* If renaming, add the new record */
1172 if (newpfid != NULL)
1173 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1179 int mdd_links_rename(const struct lu_env *env,
1180 struct mdd_object *mdd_obj,
1181 const struct lu_fid *oldpfid,
1182 const struct lu_name *oldlname,
1183 const struct lu_fid *newpfid,
1184 const struct lu_name *newlname,
1185 struct thandle *handle,
1186 struct linkea_data *ldata,
1187 int first, int check)
1192 if (ldata == NULL) {
1193 ldata = &mdd_env_info(env)->mti_link_data;
1194 memset(ldata, 0, sizeof(*ldata));
1195 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1196 newpfid, newlname, first, check, ldata);
1201 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1202 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1208 if (newlname == NULL)
1209 CERROR("link_ea add failed %d "DFID"\n",
1210 rc, PFID(mdd_object_fid(mdd_obj)));
1211 else if (oldpfid == NULL)
1212 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1213 newlname->ln_namelen, newlname->ln_name, rc,
1214 PFID(mdd_object_fid(mdd_obj)));
1215 else if (newpfid == NULL)
1216 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1217 oldlname->ln_namelen, oldlname->ln_name, rc,
1218 PFID(mdd_object_fid(mdd_obj)));
1220 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1221 "\n", oldlname->ln_namelen, oldlname->ln_name,
1222 newlname->ln_namelen, newlname->ln_name, rc,
1223 PFID(mdd_object_fid(mdd_obj)));
1226 if (is_vmalloc_addr(ldata->ld_buf))
1227 /* if we vmalloced a large buffer drop it */
1228 lu_buf_free(ldata->ld_buf);
1233 static inline int mdd_links_add(const struct lu_env *env,
1234 struct mdd_object *mdd_obj,
1235 const struct lu_fid *pfid,
1236 const struct lu_name *lname,
1237 struct thandle *handle,
1238 struct linkea_data *ldata, int first)
1240 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1241 pfid, lname, handle, ldata, first, 0);
1244 static inline int mdd_links_del(const struct lu_env *env,
1245 struct mdd_object *mdd_obj,
1246 const struct lu_fid *pfid,
1247 const struct lu_name *lname,
1248 struct thandle *handle)
1250 return mdd_links_rename(env, mdd_obj, pfid, lname,
1251 NULL, NULL, handle, NULL, 0, 0);
1254 /** Read the link EA into a temp buffer.
1255 * Uses the name_buf since it is generally large.
1256 * \retval IS_ERR err
1257 * \retval ptr to \a lu_buf (always \a mti_link_buf)
1259 struct lu_buf *mdd_links_get(const struct lu_env *env,
1260 struct mdd_object *mdd_obj)
1262 struct linkea_data ldata = { NULL };
1265 rc = mdd_links_read(env, mdd_obj, &ldata);
1266 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1269 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1270 struct linkea_data *ldata, struct thandle *handle)
1272 const struct lu_buf *buf;
1275 if (ldata == NULL || ldata->ld_buf == NULL ||
1276 ldata->ld_leh == NULL)
1279 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1283 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1284 ldata->ld_leh->leh_len);
1285 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1286 if (unlikely(rc == -ENOSPC)) {
1287 rc = linkea_overflow_shrink(ldata);
1295 static int mdd_declare_links_add(const struct lu_env *env,
1296 struct mdd_object *mdd_obj,
1297 struct thandle *handle,
1298 struct linkea_data *ldata)
1304 if (ldata != NULL && ldata->ld_leh != NULL) {
1305 ea_len = ldata->ld_leh->leh_len;
1306 linkea = ldata->ld_buf->lb_buf;
1308 ea_len = MAX_LINKEA_SIZE;
1312 rc = mdo_declare_xattr_set(env, mdd_obj,
1313 mdd_buf_get_const(env, linkea, ea_len),
1314 XATTR_NAME_LINK, 0, handle);
1319 static inline int mdd_declare_links_del(const struct lu_env *env,
1320 struct mdd_object *c,
1321 struct thandle *handle)
1325 /* For directory, the linkEA will be removed together
1326 * with the object. */
1327 if (!S_ISDIR(mdd_object_type(c)))
1328 rc = mdd_declare_links_add(env, c, handle, NULL);
1333 static int mdd_declare_link(const struct lu_env *env,
1334 struct mdd_device *mdd,
1335 struct mdd_object *p,
1336 struct mdd_object *c,
1337 const struct lu_name *name,
1338 struct thandle *handle,
1340 struct linkea_data *data)
1342 struct lu_fid tfid = *mdd_object_fid(c);
1345 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1346 tfid.f_oid = cfs_fail_val;
1348 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1349 name->ln_name, handle);
1353 rc = mdo_declare_ref_add(env, c, handle);
1357 la->la_valid = LA_CTIME | LA_MTIME;
1358 rc = mdo_declare_attr_set(env, p, la, handle);
1362 la->la_valid = LA_CTIME;
1363 rc = mdo_declare_attr_set(env, c, la, handle);
1367 rc = mdd_declare_links_add(env, c, handle, data);
1371 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1377 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1378 struct md_object *src_obj, const struct lu_name *lname,
1381 const char *name = lname->ln_name;
1382 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1383 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1384 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1385 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1386 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1387 struct mdd_device *mdd = mdo2mdd(src_obj);
1388 struct thandle *handle;
1389 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1390 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1394 rc = mdd_la_get(env, mdd_sobj, cattr);
1398 rc = mdd_la_get(env, mdd_tobj, tattr);
1403 * If we are using project inheritance, we only allow hard link
1404 * creation in our tree when the project IDs are the same;
1405 * otherwise the tree quota mechanism could be circumvented.
1407 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1408 (tattr->la_projid != cattr->la_projid))
1411 handle = mdd_trans_create(env, mdd);
1413 GOTO(out_pending, rc = PTR_ERR(handle));
1415 memset(ldata, 0, sizeof(*ldata));
1417 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1418 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1420 /* Note: even this function will change ldata, but it comes from
1421 * thread_info, which is completely temporary and only seen in
1422 * this function, so we do not need reset ldata once it fails.*/
1423 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1424 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1428 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1433 rc = mdd_trans_start(env, mdd, handle);
1437 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1438 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1441 GOTO(out_unlock, rc);
1443 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1444 rc = mdo_ref_add(env, mdd_sobj, handle);
1446 GOTO(out_unlock, rc);
1449 *tfid = *mdd_object_fid(mdd_sobj);
1450 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1451 tfid->f_oid = cfs_fail_val;
1453 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1454 mdd_object_type(mdd_sobj), name, handle);
1456 mdo_ref_del(env, mdd_sobj, handle);
1457 GOTO(out_unlock, rc);
1460 la->la_valid = LA_CTIME | LA_MTIME;
1461 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1463 GOTO(out_unlock, rc);
1465 la->la_valid = LA_CTIME;
1466 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1468 /* Note: The failure of links_add should not cause the
1469 * link failure, so do not check return value. */
1470 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1471 lname, handle, ldata, 0);
1475 mdd_write_unlock(env, mdd_sobj);
1477 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1478 mdd_object_fid(mdd_tobj), NULL,
1479 NULL, lname, NULL, handle);
1481 rc = mdd_trans_stop(env, mdd, rc, handle);
1482 if (is_vmalloc_addr(ldata->ld_buf))
1483 /* if we vmalloced a large buffer drop it */
1484 lu_buf_free(ldata->ld_buf);
1489 static int mdd_mark_orphan_object(const struct lu_env *env,
1490 struct mdd_object *obj, struct thandle *handle,
1493 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1496 attr->la_valid = LA_FLAGS;
1497 attr->la_flags = LUSTRE_ORPHAN_FL;
1500 rc = mdo_declare_attr_set(env, obj, attr, handle);
1502 rc = mdo_attr_set(env, obj, attr, handle);
1507 static int mdd_declare_finish_unlink(const struct lu_env *env,
1508 struct mdd_object *obj,
1509 struct thandle *handle)
1513 /* Sigh, we do not know if the unlink object will become orphan in
1514 * declare phase, but fortunately the flags here does not matter
1515 * in current declare implementation */
1516 rc = mdd_mark_orphan_object(env, obj, handle, true);
1520 rc = mdo_declare_destroy(env, obj, handle);
1524 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1528 return mdd_declare_links_del(env, obj, handle);
1531 /* caller should take a lock before calling */
1532 int mdd_finish_unlink(const struct lu_env *env,
1533 struct mdd_object *obj, struct md_attr *ma,
1534 struct mdd_object *pobj,
1535 const struct lu_name *lname,
1539 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1542 LASSERT(mdd_write_locked(env, obj) != 0);
1544 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1545 /* add new orphan and the object
1546 * will be deleted during mdd_close() */
1547 obj->mod_flags |= DEAD_OBJ;
1548 if (obj->mod_count) {
1549 rc = mdd_orphan_insert(env, obj, th);
1551 CDEBUG(D_HA, "Object "DFID" is inserted into "
1552 "orphan list, open count = %d\n",
1553 PFID(mdd_object_fid(obj)),
1556 CERROR("Object "DFID" fail to be an orphan, "
1557 "open count = %d, maybe cause failed "
1559 PFID(mdd_object_fid(obj)),
1562 /* mark object as an orphan here, not
1563 * before mdd_orphan_insert() as racing
1564 * mdd_la_get() may propagate ORPHAN_OBJ
1565 * causing the asserition */
1566 rc = mdd_mark_orphan_object(env, obj, th, false);
1568 rc = mdo_destroy(env, obj, th);
1570 } else if (!is_dir) {
1571 /* old files may not have link ea; ignore errors */
1572 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1580 * has mdd_write_lock on cobj already, but not on pobj yet
1582 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1583 const struct lu_attr *pattr,
1584 struct mdd_object *cobj,
1585 const struct lu_attr *cattr)
1590 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1595 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1596 struct mdd_object *p, struct mdd_object *c,
1597 const struct lu_name *name, struct md_attr *ma,
1598 struct thandle *handle, int no_name, int is_dir)
1600 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1603 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1604 if (likely(no_name == 0)) {
1605 rc = mdo_declare_index_delete(env, p, name->ln_name,
1612 rc = mdo_declare_ref_del(env, p, handle);
1618 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1619 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1620 la->la_valid = LA_CTIME | LA_MTIME;
1621 rc = mdo_declare_attr_set(env, p, la, handle);
1626 rc = mdo_declare_ref_del(env, c, handle);
1630 rc = mdo_declare_ref_del(env, c, handle);
1634 la->la_valid = LA_CTIME;
1635 rc = mdo_declare_attr_set(env, c, la, handle);
1639 rc = mdd_declare_finish_unlink(env, c, handle);
1643 /* FIXME: need changelog for remove entry */
1644 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1652 * test if a file has an HSM archive
1653 * if HSM attributes are not found in ma update them from
1656 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1657 struct mdd_object *obj,
1662 if (!(ma->ma_valid & MA_HSM)) {
1663 /* no HSM MD provided, read xattr */
1664 struct lu_buf *hsm_buf;
1665 const size_t buflen = sizeof(struct hsm_attrs);
1668 hsm_buf = mdd_buf_get(env, NULL, 0);
1669 lu_buf_alloc(hsm_buf, buflen);
1670 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1671 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1672 lu_buf_free(hsm_buf);
1676 ma->ma_valid |= MA_HSM;
1678 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1684 * Delete name entry and the object.
1685 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1686 * does not exist for this object, and it could only happen during resending
1687 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1688 * is also needed in this case(needed by changelog), so we have to add another
1689 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1690 * the ENOENT failure should be able to be fixed by redo mechanism.
1692 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1693 struct md_object *cobj, const struct lu_name *lname,
1694 struct md_attr *ma, int no_name)
1696 const char *name = lname->ln_name;
1697 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1698 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1699 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1700 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1701 struct mdd_object *mdd_cobj = NULL;
1702 struct mdd_device *mdd = mdo2mdd(pobj);
1703 struct thandle *handle;
1704 int rc, is_dir = 0, cl_flags = 0;
1707 /* let shutdown to start */
1708 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1710 /* cobj == NULL means only delete name entry */
1711 if (likely(cobj != NULL)) {
1712 mdd_cobj = md2mdd_obj(cobj);
1713 if (mdd_object_exists(mdd_cobj) == 0)
1717 rc = mdd_la_get(env, mdd_pobj, pattr);
1721 if (likely(mdd_cobj != NULL)) {
1723 rc = mdd_la_get(env, mdd_cobj, cattr);
1727 is_dir = S_ISDIR(cattr->la_mode);
1728 /* search for an existing archive.
1729 * we should check ahead as the object
1730 * can be destroyed in this transaction */
1731 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1732 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1735 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1739 handle = mdd_trans_create(env, mdd);
1741 RETURN(PTR_ERR(handle));
1743 if (likely(mdd_cobj != NULL))
1744 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1746 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1747 lname, ma, handle, no_name, is_dir);
1751 rc = mdd_trans_start(env, mdd, handle);
1755 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1756 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1761 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1762 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1763 GOTO(cleanup, rc = 0);
1765 if (likely(mdd_cobj != NULL)) {
1766 rc = mdo_ref_del(env, mdd_cobj, handle);
1768 __mdd_index_insert_only(env, mdd_pobj,
1769 mdd_object_fid(mdd_cobj),
1770 mdd_object_type(mdd_cobj),
1777 mdo_ref_del(env, mdd_cobj, handle);
1779 /* fetch updated nlink */
1780 rc = mdd_la_get(env, mdd_cobj, cattr);
1785 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1786 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1788 la->la_valid = LA_CTIME | LA_MTIME;
1789 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1793 /* Enough for only unlink the entry */
1794 if (unlikely(mdd_cobj == NULL))
1797 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1798 /* update ctime of an unlinked file only if it is still
1799 * opened or a link still exists */
1800 la->la_valid = LA_CTIME;
1801 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1806 /* XXX: this transfer to ma will be removed with LOD/OSP */
1807 ma->ma_attr = *cattr;
1808 ma->ma_valid |= MA_INODE;
1809 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1813 /* fetch updated nlink */
1814 rc = mdd_la_get(env, mdd_cobj, cattr);
1815 /* if object is removed then we can't get its attrs,
1817 if (rc == -ENOENT) {
1818 cattr->la_nlink = 0;
1822 if (cattr->la_nlink == 0) {
1823 ma->ma_attr = *cattr;
1824 ma->ma_valid |= MA_INODE;
1829 if (likely(mdd_cobj != NULL))
1830 mdd_write_unlock(env, mdd_cobj);
1833 if (cattr->la_nlink == 0)
1834 cl_flags |= CLF_UNLINK_LAST;
1836 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1838 rc = mdd_changelog_ns_store(env, mdd,
1839 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1840 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1841 lname, NULL, handle);
1844 rc = mdd_trans_stop(env, mdd, rc, handle);
1850 * The permission has been checked when obj created, no need check again.
1852 static int mdd_cd_sanity_check(const struct lu_env *env,
1853 struct mdd_object *obj)
1858 if (!obj || mdd_is_dead_obj(obj))
1864 static int mdd_create_data(const struct lu_env *env,
1865 struct md_object *pobj,
1866 struct md_object *cobj,
1867 const struct md_op_spec *spec,
1870 struct mdd_device *mdd = mdo2mdd(cobj);
1871 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1872 struct mdd_object *son = md2mdd_obj(cobj);
1873 struct thandle *handle;
1874 const struct lu_buf *buf;
1875 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1876 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1880 rc = mdd_cd_sanity_check(env, son);
1884 if (!md_should_create(spec->sp_cr_flags))
1888 * there are following use cases for this function:
1889 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1890 * striping can be specified or not
1893 rc = mdd_la_get(env, son, attr);
1897 /* calling ->ah_make_hint() is used to transfer information from parent */
1898 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1900 handle = mdd_trans_create(env, mdd);
1902 GOTO(out_free, rc = PTR_ERR(handle));
1905 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1906 * Should this be fixed?
1908 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1909 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1910 spec->sp_cr_flags, spec->no_create);
1912 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1913 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1914 spec->u.sp_ea.eadatalen);
1919 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1920 XATTR_NAME_LOV, 0, handle);
1924 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1929 rc = mdd_trans_start(env, mdd, handle);
1933 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1939 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1943 rc = mdd_trans_stop(env, mdd, rc, handle);
1949 static int mdd_declare_object_initialize(const struct lu_env *env,
1950 struct mdd_object *parent,
1951 struct mdd_object *child,
1952 const struct lu_attr *attr,
1953 struct thandle *handle)
1958 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1959 if (!S_ISDIR(attr->la_mode))
1962 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1963 S_IFDIR, dot, handle);
1967 rc = mdo_declare_ref_add(env, child, handle);
1971 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1972 S_IFDIR, dotdot, handle);
1977 static int mdd_object_initialize(const struct lu_env *env,
1978 const struct lu_fid *pfid,
1979 struct mdd_object *child,
1980 struct lu_attr *attr,
1981 struct thandle *handle)
1986 if (S_ISDIR(attr->la_mode)) {
1987 /* Add "." and ".." for newly created dir */
1988 mdo_ref_add(env, child, handle);
1989 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1990 S_IFDIR, dot, handle);
1992 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1995 mdo_ref_del(env, child, handle);
2002 * This function checks whether it can create a file/dir under the
2003 * directory(@pobj). The directory(@pobj) is not being locked by
2006 * \param[in] env execution environment
2007 * \param[in] pobj the directory to create files
2008 * \param[in] pattr the attributes of the directory
2009 * \param[in] lname the name of the created file/dir
2010 * \param[in] cattr the attributes of the file/dir
2011 * \param[in] spec create specification
2013 * \retval = 0 it is allowed to create file/dir under
2015 * \retval negative error not allowed to create file/dir
2016 * under the directory
2018 static int mdd_create_sanity_check(const struct lu_env *env,
2019 struct md_object *pobj,
2020 const struct lu_attr *pattr,
2021 const struct lu_name *lname,
2022 struct lu_attr *cattr,
2023 struct md_op_spec *spec)
2025 struct mdd_thread_info *info = mdd_env_info(env);
2026 struct lu_fid *fid = &info->mti_fid;
2027 struct mdd_object *obj = md2mdd_obj(pobj);
2028 struct mdd_device *m = mdo2mdd(pobj);
2029 bool check_perm = true;
2034 if (mdd_is_dead_obj(obj))
2038 * In some cases this lookup is not needed - we know before if name
2039 * exists or not because MDT performs lookup for it.
2040 * name length check is done in lookup.
2042 if (spec->sp_cr_lookup) {
2044 * Check if the name already exist, though it will be checked in
2045 * _index_insert also, for avoiding rolling back if exists
2048 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2049 MAY_WRITE | MAY_EXEC);
2051 RETURN(rc ? : -EEXIST);
2053 /* Permission is already being checked in mdd_lookup */
2057 if (S_ISDIR(cattr->la_mode) &&
2058 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2059 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2060 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2062 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2063 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2065 CERROR("%s: invalid lmv_user_md: magic = %x, "
2066 "stripe_offset = %d, stripe_count = %u: "
2067 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2068 le32_to_cpu(lum->lum_magic),
2069 (int)le32_to_cpu(lum->lum_stripe_offset),
2070 le32_to_cpu(lum->lum_stripe_count), rc);
2075 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2080 if (pattr->la_mode & S_ISGID) {
2081 struct lu_ucred *uc = lu_ucred(env);
2083 cattr->la_gid = pattr->la_gid;
2085 /* Directories are special, and always inherit S_ISGID */
2086 if (S_ISDIR(cattr->la_mode)) {
2087 cattr->la_mode |= S_ISGID;
2088 cattr->la_valid |= LA_MODE;
2089 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2090 == (S_ISGID | S_IXGRP) &&
2091 !lustre_in_group_p(uc,
2092 (cattr->la_valid & LA_GID) ?
2093 cattr->la_gid : pattr->la_gid) &&
2094 !md_capable(uc, CAP_FSETID)) {
2095 cattr->la_mode &= ~S_ISGID;
2096 cattr->la_valid |= LA_MODE;
2100 /* Inherit project ID from parent directory */
2101 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2102 cattr->la_projid = pattr->la_projid;
2103 if (S_ISDIR(cattr->la_mode)) {
2104 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2105 cattr->la_valid |= LA_FLAGS;
2107 cattr->la_valid |= LA_PROJID;
2110 rc = mdd_name_check(m, lname);
2114 switch (cattr->la_mode & S_IFMT) {
2116 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2118 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2119 RETURN(-ENAMETOOLONG);
2138 static int mdd_declare_create_object(const struct lu_env *env,
2139 struct mdd_device *mdd,
2140 struct mdd_object *p, struct mdd_object *c,
2141 struct lu_attr *attr,
2142 struct thandle *handle,
2143 const struct md_op_spec *spec,
2144 struct lu_buf *def_acl_buf,
2145 struct lu_buf *acl_buf,
2146 struct lu_buf *hsm_buf,
2147 struct dt_allocation_hint *hint)
2149 const struct lu_buf *buf;
2152 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2157 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2158 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2159 /* if dir, then can inherit default ACl */
2160 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2161 XATTR_NAME_ACL_DEFAULT,
2167 if (acl_buf && acl_buf->lb_len > 0) {
2168 rc = mdo_declare_attr_set(env, c, attr, handle);
2172 rc = mdo_declare_xattr_set(env, c, acl_buf,
2173 XATTR_NAME_ACL_ACCESS, 0, handle);
2178 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2182 /* replay case, create LOV EA from client data */
2183 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2184 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2185 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2186 spec->u.sp_ea.eadatalen);
2187 rc = mdo_declare_xattr_set(env, c, buf,
2188 S_ISDIR(attr->la_mode) ?
2189 XATTR_NAME_LMV : XATTR_NAME_LOV,
2190 LU_XATTR_CREATE, handle);
2194 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2195 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2203 if (S_ISLNK(attr->la_mode)) {
2204 const char *target_name = spec->u.sp_symname;
2205 int sym_len = strlen(target_name);
2206 const struct lu_buf *buf;
2208 buf = mdd_buf_get_const(env, target_name, sym_len);
2209 rc = dt_declare_record_write(env, mdd_object_child(c),
2215 if (spec->sp_cr_file_secctx_name != NULL) {
2216 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2217 spec->sp_cr_file_secctx_size);
2218 rc = mdo_declare_xattr_set(env, c, buf,
2219 spec->sp_cr_file_secctx_name, 0,
2225 if (spec->sp_cr_file_encctx != NULL) {
2226 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2227 spec->sp_cr_file_encctx_size);
2228 rc = mdo_declare_xattr_set(env, c, buf,
2229 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2238 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2239 struct mdd_object *p, struct mdd_object *c,
2240 const struct lu_name *name,
2241 struct lu_attr *attr,
2242 struct thandle *handle,
2243 const struct md_op_spec *spec,
2244 struct linkea_data *ldata,
2245 struct lu_buf *def_acl_buf,
2246 struct lu_buf *acl_buf,
2247 struct lu_buf *hsm_buf,
2248 struct dt_allocation_hint *hint)
2252 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2253 def_acl_buf, acl_buf, hsm_buf, hint);
2257 if (S_ISDIR(attr->la_mode)) {
2258 rc = mdo_declare_ref_add(env, p, handle);
2263 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2264 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2268 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2269 enum changelog_rec_type type;
2271 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2272 attr->la_mode, name->ln_name,
2277 rc = mdd_declare_links_add(env, c, handle, ldata);
2282 la->la_valid = LA_CTIME | LA_MTIME;
2283 rc = mdo_declare_attr_set(env, p, la, handle);
2287 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2288 S_ISREG(attr->la_mode) ? CL_CREATE :
2289 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2291 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2300 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2301 struct lu_attr *la, struct lu_buf *def_acl_buf,
2302 struct lu_buf *acl_buf)
2308 if (S_ISLNK(la->la_mode)) {
2309 acl_buf->lb_len = 0;
2310 def_acl_buf->lb_len = 0;
2314 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2315 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2316 XATTR_NAME_ACL_DEFAULT);
2317 mdd_read_unlock(env, pobj);
2319 /* ACL buffer size is not enough, need realloc */
2320 if (rc > acl_buf->lb_len)
2323 /* If there are default ACL, fix mode/ACL by default ACL */
2324 def_acl_buf->lb_len = rc;
2325 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2326 acl_buf->lb_len = rc;
2327 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2330 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2331 /* If there are no default ACL, fix mode by mask */
2332 struct lu_ucred *uc = lu_ucred(env);
2334 /* The create triggered by MDT internal events, such as
2335 * LFSCK reset, will not contain valid "uc". */
2336 if (unlikely(uc != NULL))
2337 la->la_mode &= ~uc->uc_umask;
2339 acl_buf->lb_len = 0;
2340 def_acl_buf->lb_len = 0;
2347 * Create a metadata object and initialize it, set acl, xattr.
2349 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2350 struct mdd_object *son, struct lu_attr *attr,
2351 struct md_op_spec *spec, struct lu_buf *acl_buf,
2352 struct lu_buf *def_acl_buf,
2353 struct lu_buf *hsm_buf,
2354 struct dt_allocation_hint *hint,
2355 struct thandle *handle, bool initsecctx)
2357 const struct lu_buf *buf;
2360 mdd_write_lock(env, son, DT_TGT_CHILD);
2361 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2366 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2367 * created in declare phase, they also needs to be added to master
2368 * object as sub-directory entry. So it has to initialize the master
2369 * object, then set dir striped EA.(in mdo_xattr_set) */
2370 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2373 GOTO(err_destroy, rc);
2376 * in case of replay we just set LOVEA provided by the client
2377 * XXX: I think it would be interesting to try "old" way where
2378 * MDT calls this xattr_set(LOV) in a different transaction.
2379 * probably this way we code can be made better.
2382 /* During creation, there are only a few cases we need do xattr_set to
2384 * 1. regular file: see comments above.
2385 * 2. dir: inherit default striping or pool settings from parent.
2386 * 3. create striped directory with provided stripeEA.
2387 * 4. create striped directory because inherit default layout from the
2390 if (spec->no_create ||
2391 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2392 S_ISDIR(attr->la_mode)) {
2393 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2394 spec->u.sp_ea.eadatalen);
2395 rc = mdo_xattr_set(env, son, buf,
2396 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2398 LU_XATTR_CREATE, handle);
2400 GOTO(err_destroy, rc);
2403 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2406 memset(&mh, 0, sizeof(mh));
2407 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2408 mh.mh_arch_id = spec->sp_archive_id;
2409 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2410 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2413 GOTO(err_destroy, rc);
2416 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2417 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2418 S_ISDIR(attr->la_mode)) {
2419 /* set default acl */
2420 rc = mdo_xattr_set(env, son, def_acl_buf,
2421 XATTR_NAME_ACL_DEFAULT, 0,
2424 GOTO(err_destroy, rc);
2426 /* set its own acl */
2427 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2428 rc = mdo_xattr_set(env, son, acl_buf,
2429 XATTR_NAME_ACL_ACCESS,
2432 GOTO(err_destroy, rc);
2436 if (S_ISLNK(attr->la_mode)) {
2437 struct dt_object *dt = mdd_object_child(son);
2438 const char *target_name = spec->u.sp_symname;
2439 int sym_len = strlen(target_name);
2442 buf = mdd_buf_get_const(env, target_name, sym_len);
2443 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2447 GOTO(err_initlized, rc = -EFAULT);
2450 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2451 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2452 spec->sp_cr_file_secctx_size);
2453 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2456 GOTO(err_initlized, rc);
2459 if (spec->sp_cr_file_encctx != NULL) {
2460 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2461 spec->sp_cr_file_encctx_size);
2462 rc = mdo_xattr_set(env, son, buf,
2463 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2466 GOTO(err_initlized, rc);
2470 if (unlikely(rc != 0)) {
2472 if (S_ISDIR(attr->la_mode)) {
2473 /* Drop the reference, no need to delete "."/"..",
2474 * because the object to be destroied directly. */
2475 rc2 = mdo_ref_del(env, son, handle);
2479 rc2 = mdo_ref_del(env, son, handle);
2483 mdo_destroy(env, son, handle);
2486 mdd_write_unlock(env, son);
2490 static int mdd_index_delete(const struct lu_env *env,
2491 struct mdd_object *mdd_pobj,
2492 struct lu_attr *cattr,
2493 const struct lu_name *lname)
2495 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2496 struct thandle *handle;
2500 handle = mdd_trans_create(env, mdd);
2502 RETURN(PTR_ERR(handle));
2504 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2509 if (S_ISDIR(cattr->la_mode)) {
2510 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2515 /* Since this will only be used in the error handler path,
2516 * Let's set the thandle to be local and not mess the transno */
2517 handle->th_local = 1;
2518 rc = mdd_trans_start(env, mdd, handle);
2522 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2523 S_ISDIR(cattr->la_mode), handle);
2527 rc = mdd_trans_stop(env, mdd, rc, handle);
2533 * Create object and insert it into namespace.
2535 * Two operations have to be performed:
2537 * - an allocation of a new object (->do_create()), and
2538 * - an insertion into a parent index (->dio_insert()).
2540 * Due to locking, operation order is not important, when both are
2541 * successful, *but* error handling cases are quite different:
2543 * - if insertion is done first, and following object creation fails,
2544 * insertion has to be rolled back, but this operation might fail
2545 * also leaving us with dangling index entry.
2547 * - if creation is done first, is has to be undone if insertion fails,
2548 * leaving us with leaked space, which is not good but not fatal.
2550 * It seems that creation-first is simplest solution, but it is sub-optimal
2556 * case, because second mkdir is bound to create object, only to
2557 * destroy it immediately.
2559 * To avoid this follow local file systems that do double lookup:
2561 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2562 * 1. create (mdd_create_object_internal())
2563 * 2. insert (__mdd_index_insert(), lookup again)
2565 * \param[in] pobj parent object
2566 * \param[in] lname name of child being created
2567 * \param[in,out] child child object being created
2568 * \param[in] spec additional create parameters
2569 * \param[in] ma attributes for new child object
2571 * \retval 0 on success
2572 * \retval negative errno on failure
2574 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2575 const struct lu_name *lname, struct md_object *child,
2576 struct md_op_spec *spec, struct md_attr *ma)
2578 struct mdd_thread_info *info = mdd_env_info(env);
2579 struct lu_attr *la = &info->mti_la_for_fix;
2580 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2581 struct mdd_object *son = md2mdd_obj(child);
2582 struct mdd_device *mdd = mdo2mdd(pobj);
2583 struct lu_attr *attr = &ma->ma_attr;
2584 struct thandle *handle;
2585 struct lu_attr *pattr = &info->mti_pattr;
2586 struct lu_buf acl_buf;
2587 struct lu_buf def_acl_buf;
2588 struct lu_buf hsm_buf;
2589 struct linkea_data *ldata = &info->mti_link_data;
2590 const char *name = lname->ln_name;
2591 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2592 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2597 rc = mdd_la_get(env, mdd_pobj, pattr);
2601 /* Sanity checks before big job. */
2602 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2606 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2607 GOTO(out_free, rc = -EINPROGRESS);
2609 handle = mdd_trans_create(env, mdd);
2611 GOTO(out_free, rc = PTR_ERR(handle));
2614 acl_buf = *lu_buf_check_and_alloc(&info->mti_xattr_buf, acl_size);
2615 if (!acl_buf.lb_buf)
2616 GOTO(out_stop, rc = -ENOMEM);
2617 /* mti_big_buf is also used down below in mdd_changelog_ns_store(),
2618 * but def_acl_buf is finished with it before then
2620 def_acl_buf = *lu_buf_check_and_alloc(&info->mti_big_buf, acl_size);
2621 if (!def_acl_buf.lb_buf)
2622 GOTO(out_stop, rc = -ENOMEM);
2624 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2625 if (unlikely(rc == -ERANGE &&
2626 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2627 /* use maximum-sized xattr buffer for too-big default ACL */
2628 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2630 goto use_bigger_buffer;
2635 if (S_ISDIR(attr->la_mode)) {
2636 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2639 * migrate may create 1-stripe directory, so lod_ah_init()
2640 * doesn't adjust stripe count from lmu.
2642 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2643 lmu->lum_stripe_count = 0;
2646 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2648 memset(ldata, 0, sizeof(*ldata));
2649 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2650 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2653 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2654 &tfid, lname, 1, 0, ldata);
2656 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2657 mdd_object_fid(mdd_pobj),
2658 lname, 1, 0, ldata);
2661 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2662 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2664 memset(&hsm_buf, 0, sizeof(hsm_buf));
2665 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2666 if (hsm_buf.lb_buf == NULL)
2667 GOTO(out_stop, rc = -ENOMEM);
2670 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2671 handle, spec, ldata, &def_acl_buf, &acl_buf,
2676 rc = mdd_trans_start(env, mdd, handle);
2680 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2681 &def_acl_buf, &hsm_buf, hint, handle, true);
2685 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2686 mdd_write_lock(env, son, DT_TGT_CHILD);
2687 son->mod_flags |= VOLATILE_OBJ;
2688 rc = mdd_orphan_insert(env, son, handle);
2689 GOTO(out_volatile, rc);
2691 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2692 attr->la_mode, name, handle);
2694 GOTO(err_created, rc);
2696 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2699 /* update parent directory mtime/ctime */
2701 la->la_valid = LA_CTIME | LA_MTIME;
2702 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2704 GOTO(err_insert, rc);
2710 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2711 rc2 = mdd_orphan_delete(env, son, handle);
2713 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2714 S_ISDIR(attr->la_mode),
2720 mdd_write_lock(env, son, DT_TGT_CHILD);
2721 if (S_ISDIR(attr->la_mode)) {
2722 /* Drop the reference, no need to delete "."/"..",
2723 * because the object is to be destroyed directly. */
2724 rc2 = mdo_ref_del(env, son, handle);
2726 mdd_write_unlock(env, son);
2731 /* For volatile files drop one link immediately, since there is
2732 * no filename in the namespace, and save any error returned. */
2733 rc2 = mdo_ref_del(env, son, handle);
2735 mdd_write_unlock(env, son);
2736 if (unlikely(rc == 0))
2741 /* Don't destroy the volatile object on success */
2742 if (likely(rc != 0))
2743 mdo_destroy(env, son, handle);
2744 mdd_write_unlock(env, son);
2747 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2748 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2749 rc = mdd_changelog_ns_store(env, mdd,
2750 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2751 S_ISREG(attr->la_mode) ? CL_CREATE :
2752 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2753 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2754 lname, NULL, handle);
2756 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2758 /* If creation fails, it is most likely due to the remote update
2759 * failure, because local transaction will mostly succeed at
2760 * this stage. There is no easy way to rollback all of previous
2761 * updates, so let's remove the object from namespace, and
2762 * LFSCK should handle the orphan object. */
2763 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2764 mdd_index_delete(env, mdd_pobj, attr, lname);
2768 if (is_vmalloc_addr(ldata->ld_buf))
2769 /* if we vmalloced a large buffer drop it */
2770 lu_buf_free(ldata->ld_buf);
2772 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2773 lu_buf_free(&hsm_buf);
2775 /* The child object shouldn't be cached anymore */
2777 set_bit(LU_OBJECT_HEARD_BANSHEE,
2778 &child->mo_lu.lo_header->loh_flags);
2782 /* has not mdd_write{read}_lock on any obj yet. */
2783 static int mdd_rename_sanity_check(const struct lu_env *env,
2784 struct mdd_object *src_pobj,
2785 const struct lu_attr *pattr,
2786 struct mdd_object *tgt_pobj,
2787 const struct lu_attr *tpattr,
2788 struct mdd_object *sobj,
2789 const struct lu_attr *cattr,
2790 struct mdd_object *tobj,
2791 const struct lu_attr *tattr)
2796 /* XXX: when get here, sobj must NOT be NULL,
2797 * the other case has been processed in cld_rename
2798 * before mdd_rename and enable MDS_PERM_BYPASS. */
2802 * If we are using project inheritance, we only allow renames
2803 * into our tree when the project IDs are the same; otherwise
2804 * tree quota mechanism would be circumvented.
2806 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2807 tpattr->la_projid != cattr->la_projid) ||
2808 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2809 (pattr->la_projid != tpattr->la_projid)))
2812 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2816 /* XXX: when get here, "tobj == NULL" means tobj must
2817 * NOT exist (neither on remote MDS, such case has been
2818 * processed in cld_rename before mdd_rename and enable
2820 * So check may_create, but not check may_unlink. */
2822 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2823 (src_pobj != tgt_pobj));
2825 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2826 (src_pobj != tgt_pobj), 1);
2828 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2829 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2834 static int mdd_declare_rename(const struct lu_env *env,
2835 struct mdd_device *mdd,
2836 struct mdd_object *mdd_spobj,
2837 struct mdd_object *mdd_tpobj,
2838 struct mdd_object *mdd_sobj,
2839 struct mdd_object *mdd_tobj,
2840 const struct lu_name *sname,
2841 const struct lu_name *tname,
2843 struct linkea_data *ldata,
2844 struct thandle *handle)
2846 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2849 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2850 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2856 /* name from source dir */
2857 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2861 /* .. from source child */
2862 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2863 /* source child can be directory, count by source dir's nlink */
2864 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2867 if (mdd_spobj != mdd_tpobj) {
2868 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2873 rc = mdo_declare_index_insert(env, mdd_sobj,
2874 mdd_object_fid(mdd_tpobj),
2875 S_IFDIR, dotdot, handle);
2880 /* new target child can be directory,
2881 * counted by target dir's nlink */
2882 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2887 la->la_valid = LA_CTIME | LA_MTIME;
2888 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2892 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2896 la->la_valid = LA_CTIME;
2897 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2901 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2906 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2907 mdd_object_type(mdd_sobj),
2908 tname->ln_name, handle);
2912 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2913 /* delete target child in target parent directory */
2914 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2919 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2923 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2924 /* target child can be directory,
2925 * delete "." reference in target child directory */
2926 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2930 /* delete ".." reference in target parent directory */
2931 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2936 la->la_valid = LA_CTIME;
2937 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2941 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2946 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2954 static int mdd_migrate_object(const struct lu_env *env,
2955 struct mdd_object *spobj,
2956 struct mdd_object *tpobj,
2957 struct mdd_object *sobj,
2958 struct mdd_object *tobj,
2959 const struct lu_name *sname,
2960 const struct lu_name *tname,
2961 struct md_op_spec *spec,
2962 struct md_attr *ma);
2964 /* src object can be remote that is why we use only fid and type of object */
2965 static int mdd_rename(const struct lu_env *env,
2966 struct md_object *src_pobj, struct md_object *tgt_pobj,
2967 const struct lu_fid *lf, const struct lu_name *lsname,
2968 struct md_object *tobj, const struct lu_name *ltname,
2971 const char *sname = lsname->ln_name;
2972 const char *tname = ltname->ln_name;
2973 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2974 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2975 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2976 struct mdd_device *mdd = mdo2mdd(src_pobj);
2977 struct mdd_object *mdd_sobj = NULL; /* source object */
2978 struct mdd_object *mdd_tobj = NULL;
2979 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2980 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2981 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2982 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2983 struct thandle *handle;
2984 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2985 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
2986 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
2989 bool tobj_locked = 0;
2990 unsigned cl_flags = 0;
2994 /* let unlink to complete and commit */
2995 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
2998 mdd_tobj = md2mdd_obj(tobj);
3000 mdd_sobj = mdd_object_find(env, mdd, lf);
3001 if (IS_ERR(mdd_sobj))
3002 RETURN(PTR_ERR(mdd_sobj));
3004 rc = mdd_la_get(env, mdd_sobj, cattr);
3006 GOTO(out_pending, rc);
3008 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3009 * hard links, and target doesn't exist.
3011 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3012 cattr->la_nlink == 1 && !tobj) {
3013 struct md_op_spec *spec = &mdd_env_info(env)->mti_spec;
3014 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3017 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3020 GOTO(out_pending, rc);
3022 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3023 if (IS_ERR(mdd_tobj))
3024 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3026 memset(spec, 0, sizeof(*spec));
3027 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3028 mdd_tobj, lsname, ltname, spec, ma);
3029 mdd_object_put(env, mdd_tobj);
3030 GOTO(out_pending, rc);
3033 rc = mdd_la_get(env, mdd_spobj, pattr);
3035 GOTO(out_pending, rc);
3038 rc = mdd_la_get(env, mdd_tobj, tattr);
3040 GOTO(out_pending, rc);
3041 /* search for an existing archive.
3042 * we should check ahead as the object
3043 * can be destroyed in this transaction */
3044 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3045 cl_flags |= CLF_RENAME_LAST_EXISTS;
3048 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3050 GOTO(out_pending, rc);
3052 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3053 mdd_sobj, cattr, mdd_tobj, tattr);
3055 GOTO(out_pending, rc);
3057 rc = mdd_name_check(mdd, ltname);
3059 GOTO(out_pending, rc);
3061 handle = mdd_trans_create(env, mdd);
3063 GOTO(out_pending, rc = PTR_ERR(handle));
3065 memset(ldata, 0, sizeof(*ldata));
3066 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3067 lsname, mdd_object_fid(mdd_tpobj), ltname,
3072 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3073 mdd_tobj, lsname, ltname, ma, ldata, handle);
3077 rc = mdd_trans_start(env, mdd, handle);
3081 is_dir = S_ISDIR(cattr->la_mode);
3083 /* Remove source name from source directory */
3084 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3088 /* "mv dir1 dir2" needs "dir1/.." link update */
3089 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3090 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3092 GOTO(fixup_spobj2, rc);
3094 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3097 GOTO(fixup_spobj, rc);
3100 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3101 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3103 /* tname might been renamed to something else */
3104 GOTO(fixup_spobj, rc);
3107 /* Insert new fid with target name into target dir */
3108 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3111 GOTO(fixup_tpobj, rc);
3113 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3114 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3116 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3117 la->la_valid = LA_CTIME;
3118 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3120 GOTO(fixup_tpobj, rc);
3122 /* Update the linkEA for the source object */
3123 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3124 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3125 lsname, mdd_object_fid(mdd_tpobj), ltname,
3126 handle, ldata, 0, 0);
3128 /* Old files might not have EA entry */
3129 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3130 lsname, handle, NULL, 0);
3131 mdd_write_unlock(env, mdd_sobj);
3132 /* We don't fail the transaction if the link ea can't be
3133 updated -- fid2path will use alternate lookup method. */
3136 /* Remove old target object
3137 * For tobj is remote case cmm layer has processed
3138 * and set tobj to NULL then. So when tobj is NOT NULL,
3139 * it must be local one.
3141 if (tobj && mdd_object_exists(mdd_tobj)) {
3142 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3144 if (mdd_is_dead_obj(mdd_tobj)) {
3145 /* shld not be dead, something is wrong */
3146 CERROR("tobj is dead, something is wrong\n");
3150 mdo_ref_del(env, mdd_tobj, handle);
3152 /* Remove dot reference. */
3153 if (S_ISDIR(tattr->la_mode))
3154 mdo_ref_del(env, mdd_tobj, handle);
3157 /* fetch updated nlink */
3158 rc = mdd_la_get(env, mdd_tobj, tattr);
3160 CERROR("%s: Failed to get nlink for tobj "
3162 mdd2obd_dev(mdd)->obd_name,
3163 PFID(tpobj_fid), rc);
3164 GOTO(fixup_tpobj, rc);
3167 la->la_valid = LA_CTIME;
3168 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3170 CERROR("%s: Failed to set ctime for tobj "
3172 mdd2obd_dev(mdd)->obd_name,
3173 PFID(tpobj_fid), rc);
3174 GOTO(fixup_tpobj, rc);
3177 /* XXX: this transfer to ma will be removed with LOD/OSP */
3178 ma->ma_attr = *tattr;
3179 ma->ma_valid |= MA_INODE;
3180 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3183 CERROR("%s: Failed to unlink tobj "
3185 mdd2obd_dev(mdd)->obd_name,
3186 PFID(tpobj_fid), rc);
3187 GOTO(fixup_tpobj, rc);
3190 /* fetch updated nlink */
3191 rc = mdd_la_get(env, mdd_tobj, tattr);
3192 if (rc == -ENOENT) {
3193 /* the object got removed, let's
3194 * return the latest known attributes */
3195 tattr->la_nlink = 0;
3197 } else if (rc != 0) {
3198 CERROR("%s: Failed to get nlink for tobj "
3200 mdd2obd_dev(mdd)->obd_name,
3201 PFID(tpobj_fid), rc);
3202 GOTO(fixup_tpobj, rc);
3204 /* XXX: this transfer to ma will be removed with LOD/OSP */
3205 ma->ma_attr = *tattr;
3206 ma->ma_valid |= MA_INODE;
3208 if (tattr->la_nlink == 0)
3209 cl_flags |= CLF_RENAME_LAST;
3211 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3214 la->la_valid = LA_CTIME | LA_MTIME;
3215 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3217 GOTO(fixup_tpobj, rc);
3219 if (mdd_spobj != mdd_tpobj) {
3220 la->la_valid = LA_CTIME | LA_MTIME;
3221 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3223 GOTO(fixup_tpobj, rc);
3230 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3232 CWARN("tp obj fix error %d\n",rc2);
3234 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3235 !mdd_is_dead_obj(mdd_tobj)) {
3237 mdo_ref_add(env, mdd_tobj, handle);
3239 mdo_ref_add(env, mdd_tobj, handle);
3242 rc2 = __mdd_index_insert(env, mdd_tpobj,
3243 mdd_object_fid(mdd_tobj),
3244 mdd_object_type(mdd_tobj),
3247 CWARN("tp obj fix error: rc = %d\n", rc2);
3252 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3253 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3255 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3256 mdd2obd_dev(mdd)->obd_name, rc2);
3259 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3262 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3263 mdd2obd_dev(mdd)->obd_name, rc2);
3268 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3269 mdd_object_type(mdd_sobj), sname,
3272 CWARN("sp obj fix error: rc = %d\n", rc2);
3277 mdd_write_unlock(env, mdd_tobj);
3280 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3281 mdd_tobj, tpobj_fid, lf, spobj_fid,
3282 ltname, lsname, handle);
3285 rc = mdd_trans_stop(env, mdd, rc, handle);
3288 mdd_object_put(env, mdd_sobj);
3293 * Check whether we should migrate the file/dir
3295 * < 0 permission check failed or other error.
3296 * = 0 the file can be migrated.
3298 static int mdd_migrate_sanity_check(const struct lu_env *env,
3299 struct mdd_device *mdd,
3300 struct mdd_object *spobj,
3301 struct mdd_object *tpobj,
3302 struct mdd_object *sobj,
3303 struct mdd_object *tobj,
3304 const struct lu_attr *spattr,
3305 const struct lu_attr *tpattr,
3306 const struct lu_attr *attr)
3312 if (!mdd_object_remote(sobj)) {
3313 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3314 if (sobj->mod_count > 0) {
3315 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3316 mdd_obj_dev_name(sobj),
3317 PFID(mdd_object_fid(sobj)),
3319 mdd_read_unlock(env, sobj);
3322 mdd_read_unlock(env, sobj);
3325 if (mdd_object_exists(tobj))
3328 rc = mdd_rename_sanity_check(env, spobj, spattr, tpobj, tpattr, sobj,
3333 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3334 struct mdd_object *obj,
3335 const struct lu_buf *buf,
3337 int fl, struct thandle *handle);
3339 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3340 static int mdd_iterate_xattrs(const struct lu_env *env,
3341 struct mdd_object *sobj,
3342 struct mdd_object *tobj,
3344 struct thandle *handle,
3347 struct mdd_thread_info *info = mdd_env_info(env);
3349 struct lu_buf list_xbuf;
3350 struct lu_buf cbxbuf;
3351 struct lu_buf xbuf = { NULL };
3360 /* retrieve xattr list from the old object */
3361 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3362 if (list_xsize == -ENODATA)
3368 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3369 if (info->mti_big_buf.lb_buf == NULL)
3372 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3373 list_xbuf.lb_len = list_xsize;
3374 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3380 xname = list_xbuf.lb_buf;
3382 xlen = strnlen(xname, rem - 1) + 1;
3383 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3384 strcmp(XATTR_NAME_LMV, xname) == 0)
3388 strcmp(XATTR_NAME_LINK, xname) == 0)
3391 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3392 if (xsize == -ENODATA)
3395 GOTO(out, rc = xsize);
3397 lu_buf_check_and_alloc(&xbuf, xsize);
3398 if (xbuf.lb_buf == NULL)
3399 GOTO(out, rc = -ENOMEM);
3401 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3408 cbxbuf.lb_len = xsize;
3410 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3411 if (unlikely(rc == -ENOSPC &&
3412 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3413 rc = linkea_overflow_shrink(
3414 (struct linkea_data *)(cbxbuf.lb_buf));
3415 if (likely(rc > 0)) {
3433 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3434 struct mdd_object *sobj,
3435 struct mdd_object *tobj,
3436 const struct lu_name *sname,
3437 const struct lu_fid *sfid,
3438 const struct lu_name *lname,
3439 const struct lu_fid *fid,
3441 struct thandle *handle);
3443 static int mdd_declare_update_link(const struct lu_env *env,
3444 struct mdd_object *sobj,
3445 struct mdd_object *tobj,
3446 const struct lu_name *tname,
3447 const struct lu_fid *tpfid,
3448 const struct lu_name *lname,
3449 const struct lu_fid *fid,
3451 struct thandle *handle)
3453 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3454 struct mdd_object *pobj;
3458 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3459 !strcmp(tname->ln_name, lname->ln_name))
3462 pobj = mdd_object_find(env, mdd, fid);
3464 return PTR_ERR(pobj);
3467 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3469 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3470 mdd_object_type(sobj),
3471 lname->ln_name, handle);
3472 mdd_object_put(env, pobj);
3476 rc = mdo_declare_ref_add(env, tobj, handle);
3480 rc = mdo_declare_ref_del(env, sobj, handle);
3484 static int mdd_update_link(const struct lu_env *env,
3485 struct mdd_object *sobj,
3486 struct mdd_object *tobj,
3487 const struct lu_name *tname,
3488 const struct lu_fid *tpfid,
3489 const struct lu_name *lname,
3490 const struct lu_fid *fid,
3492 struct thandle *handle)
3494 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3495 struct mdd_object *pobj;
3500 LASSERT(lu_name_is_valid(lname));
3503 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3504 !strncmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3507 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3508 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3510 pobj = mdd_object_find(env, mdd, fid);
3512 CWARN("%s: cannot find obj "DFID": %ld\n",
3513 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3514 RETURN(PTR_ERR(pobj));
3517 if (!mdd_object_exists(pobj)) {
3518 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3519 mdd_object_put(env, pobj);
3523 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3524 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3526 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3527 mdd_object_type(sobj),
3528 lname->ln_name, handle);
3529 mdd_write_unlock(env, pobj);
3530 mdd_object_put(env, pobj);
3534 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3535 rc = mdo_ref_add(env, tobj, handle);
3536 mdd_write_unlock(env, tobj);
3540 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3541 rc = mdo_ref_del(env, sobj, handle);
3542 mdd_write_unlock(env, sobj);
3547 static inline int mdd_fld_lookup(const struct lu_env *env,
3548 struct mdd_device *mdd,
3549 const struct lu_fid *fid,
3552 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
3553 struct seq_server_site *ss;
3556 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3558 range->lsr_flags = LU_SEQ_RANGE_MDT;
3559 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3563 *mdt_index = range->lsr_index;
3568 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3569 struct mdd_object *sobj,
3570 struct mdd_object *tobj,
3571 const struct lu_name *tname,
3572 const struct lu_fid *tpfid,
3573 const struct lu_name *lname,
3574 const struct lu_fid *fid,
3576 struct thandle *handle)
3578 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3579 __u32 source_mdt_index = *(__u32 *)opaque;
3580 __u32 link_mdt_index;
3586 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3587 !strcmp(tname->ln_name, lname->ln_name))
3590 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3594 RETURN(link_mdt_index == source_mdt_index);
3597 static int mdd_iterate_linkea(const struct lu_env *env,
3598 struct mdd_object *sobj,
3599 struct mdd_object *tobj,
3600 const struct lu_name *tname,
3601 const struct lu_fid *tpfid,
3602 struct linkea_data *ldata,
3604 struct thandle *handle,
3607 struct mdd_thread_info *info = mdd_env_info(env);
3608 char *filename = info->mti_name;
3609 struct lu_name lname;
3616 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3617 linkea_next_entry(ldata)) {
3618 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3621 /* Note: lname might miss \0 at the end */
3622 snprintf(filename, sizeof(info->mti_name), "%.*s",
3623 lname.ln_namelen, lname.ln_name);
3624 lname.ln_name = filename;
3626 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3628 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3636 * Prepare linkea, and check whether file needs migrate: if source still has
3637 * link on source MDT, no need to migrate, just update namespace on source and
3640 * \retval 0 do migrate
3641 * \retval 1 don't migrate
3642 * \retval -errno on failure
3644 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3645 struct mdd_device *mdd,
3646 struct mdd_object *spobj,
3647 struct mdd_object *tpobj,
3648 struct mdd_object *sobj,
3649 const struct lu_name *sname,
3650 const struct lu_name *tname,
3651 const struct lu_attr *attr,
3652 struct linkea_data *ldata)
3654 __u32 source_mdt_index;
3659 memset(ldata, 0, sizeof(*ldata));
3660 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3661 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3666 * Then it will check if the file should be migrated. If the file has
3667 * mulitple links, we only need migrate the file if all of its entries
3668 * has been migrated to the remote MDT.
3670 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3673 /* If there are still links locally, don't migrate this file */
3674 LASSERT(ldata->ld_leh != NULL);
3677 * If linkEA is overflow, it means there are some unknown name entries
3678 * under unknown parents, which will prevent the migration.
3680 if (unlikely(ldata->ld_leh->leh_overflow_time))
3683 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3687 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3688 ldata, &source_mdt_index, NULL,
3689 mdd_is_link_on_source_mdt);
3693 static int mdd_declare_migrate_update(const struct lu_env *env,
3694 struct mdd_object *spobj,
3695 struct mdd_object *tpobj,
3696 struct mdd_object *obj,
3697 const struct lu_name *sname,
3698 const struct lu_name *tname,
3699 struct lu_attr *attr,
3700 struct lu_attr *spattr,
3701 struct lu_attr *tpattr,
3702 struct linkea_data *ldata,
3704 struct thandle *handle)
3706 struct mdd_thread_info *info = mdd_env_info(env);
3707 struct lu_attr *la = &info->mti_la_for_fix;
3710 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3714 if (S_ISDIR(attr->la_mode)) {
3715 rc = mdo_declare_ref_del(env, spobj, handle);
3720 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3721 attr->la_mode & S_IFMT,
3722 tname->ln_name, handle);
3726 rc = mdd_declare_links_add(env, obj, handle, ldata);
3730 if (S_ISDIR(attr->la_mode)) {
3731 rc = mdo_declare_ref_add(env, tpobj, handle);
3736 la->la_valid = LA_CTIME | LA_MTIME;
3737 rc = mdo_declare_attr_set(env, spobj, la, handle);
3741 if (tpobj != spobj) {
3742 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3750 static int mdd_declare_migrate_create(const struct lu_env *env,
3751 struct mdd_object *spobj,
3752 struct mdd_object *tpobj,
3753 struct mdd_object *sobj,
3754 struct mdd_object *tobj,
3755 const struct lu_name *sname,
3756 const struct lu_name *tname,
3757 struct lu_attr *spattr,
3758 struct lu_attr *tpattr,
3759 struct lu_attr *attr,
3760 struct lu_buf *sbuf,
3761 struct linkea_data *ldata,
3763 struct md_op_spec *spec,
3764 struct dt_allocation_hint *hint,
3765 struct thandle *handle)
3767 struct mdd_thread_info *info = mdd_env_info(env);
3768 struct md_layout_change *mlc = &info->mti_mlc;
3769 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3774 if (S_ISDIR(attr->la_mode)) {
3775 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3777 mlc->mlc_opc = MD_LAYOUT_DETACH;
3778 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3782 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3783 } else if (S_ISLNK(attr->la_mode)) {
3784 spec->u.sp_symname = sbuf->lb_buf;
3785 } else if (S_ISREG(attr->la_mode)) {
3786 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3787 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3790 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3792 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3793 tname, attr, handle, spec, ldata, NULL, NULL,
3799 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3800 * createb yet, copy from sobj.
3802 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3803 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3804 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3806 if (S_ISDIR(attr->la_mode)) {
3808 /* if sobj is not striped, fake a 1-stripe LMV */
3809 LASSERT(sizeof(info->mti_key) >
3810 lmv_mds_md_size(1, LMV_MAGIC_V1));
3811 lmv = (typeof(lmv))info->mti_key;
3812 memset(lmv, 0, sizeof(*lmv));
3813 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3814 lmv->lmv_stripe_count = cpu_to_le32(1);
3815 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3816 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3817 mdd_object_fid(sobj));
3818 mlc->mlc_buf.lb_buf = lmv;
3819 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3821 mlc->mlc_buf = *sbuf;
3823 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3824 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3829 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3830 mdo_declare_xattr_set);
3834 if (S_ISREG(attr->la_mode)) {
3835 struct lu_buf fid_buf;
3837 handle->th_complex = 1;
3839 /* target may be remote, update PFID via sobj. */
3840 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3841 fid_buf.lb_len = sizeof(struct lu_fid);
3842 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3847 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3852 if (!S_ISDIR(attr->la_mode)) {
3853 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3854 mdd_object_fid(tpobj), ldata, NULL,
3855 handle, mdd_declare_update_link);
3860 if (!S_ISDIR(attr->la_mode) || lmv) {
3861 rc = mdo_declare_ref_del(env, sobj, handle);
3865 if (S_ISDIR(attr->la_mode)) {
3866 rc = mdo_declare_ref_del(env, sobj, handle);
3871 rc = mdo_declare_destroy(env, sobj, handle);
3876 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3877 attr, spattr, tpattr, ldata, ma,
3883 * migrate dirent from \a spobj to \a tpobj.
3885 static int mdd_migrate_update(const struct lu_env *env,
3886 struct mdd_object *spobj,
3887 struct mdd_object *tpobj,
3888 struct mdd_object *obj,
3889 const struct lu_name *sname,
3890 const struct lu_name *tname,
3891 struct lu_attr *attr,
3892 struct lu_attr *spattr,
3893 struct lu_attr *tpattr,
3894 struct linkea_data *ldata,
3896 struct thandle *handle)
3898 struct mdd_thread_info *info = mdd_env_info(env);
3899 struct lu_attr *la = &info->mti_la_for_fix;
3904 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3905 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3906 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3908 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3909 S_ISDIR(attr->la_mode), handle);
3913 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3914 attr->la_mode & S_IFMT,
3915 tname->ln_name, handle);
3919 rc = mdd_links_write(env, obj, ldata, handle);
3923 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3924 la->la_valid = LA_CTIME | LA_MTIME;
3925 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3926 rc = mdd_update_time(env, spobj, spattr, la, handle);
3927 mdd_write_unlock(env, spobj);
3931 if (tpobj != spobj) {
3932 la->la_valid = LA_CTIME | LA_MTIME;
3933 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3934 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3935 mdd_write_unlock(env, tpobj);
3944 * Migrate file/dir to target MDT.
3946 * Create target according to \a spec, and then migrate xattrs, if it's
3947 * directory, migrate source stripes to target.
3949 * \param[in] env execution environment
3950 * \param[in] spobj source parent object
3951 * \param[in] tpobj target parent object
3952 * \param[in] sobj source object
3953 * \param[in] tobj target object
3954 * \param[in] lname file name
3955 * \param[in] spattr source parent attributes
3956 * \param[in] tpattr target parent attributes
3957 * \param[in] attr source attributes
3958 * \param[in] sbuf source LMV buf
3959 * \param[in] spec migrate create spec
3960 * \param[in] hint target creation hint
3961 * \param[in] handle tranasction handle
3963 * \retval 0 on success
3964 * \retval -errno on failure
3966 static int mdd_migrate_create(const struct lu_env *env,
3967 struct mdd_object *spobj,
3968 struct mdd_object *tpobj,
3969 struct mdd_object *sobj,
3970 struct mdd_object *tobj,
3971 const struct lu_name *sname,
3972 const struct lu_name *tname,
3973 struct lu_attr *spattr,
3974 struct lu_attr *tpattr,
3975 struct lu_attr *attr,
3976 const struct lu_buf *sbuf,
3977 struct linkea_data *ldata,
3979 struct md_op_spec *spec,
3980 struct dt_allocation_hint *hint,
3981 struct thandle *handle)
3988 * migrate sobj stripes to tobj if it's directory:
3989 * 1. detach stripes from sobj.
3990 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
3991 * 3. create stripes for tobj, see lod_xattr_set_lmv().
3993 if (S_ISDIR(attr->la_mode)) {
3994 struct mdd_thread_info *info = mdd_env_info(env);
3995 struct md_layout_change *mlc = &info->mti_mlc;
3997 mlc->mlc_opc = MD_LAYOUT_DETACH;
3999 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4000 rc = mdo_layout_change(env, sobj, mlc, handle);
4001 mdd_write_unlock(env, sobj);
4006 /* don't set nlink from sobj */
4007 attr->la_valid &= ~LA_NLINK;
4009 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4010 hint, handle, false);
4014 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4015 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4016 mdd_write_unlock(env, tobj);
4020 /* for regular file, update OST objects XATTR_NAME_FID */
4021 if (S_ISREG(attr->la_mode)) {
4022 struct lu_buf fid_buf;
4024 /* target may be remote, update PFID via sobj. */
4025 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4026 fid_buf.lb_len = sizeof(struct lu_fid);
4027 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4032 /* delete LOV to avoid deleting OST objs when destroying sobj */
4033 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4034 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4035 mdd_write_unlock(env, sobj);
4036 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4037 if (rc && rc != -ENODATA)
4042 /* update links FID */
4043 if (!S_ISDIR(attr->la_mode)) {
4044 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4045 mdd_object_fid(tpobj), ldata,
4046 NULL, handle, mdd_update_link);
4051 /* don't destroy sobj if it's plain directory */
4052 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4053 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4054 rc = mdo_ref_del(env, sobj, handle);
4056 if (S_ISDIR(attr->la_mode))
4057 rc = mdo_ref_del(env, sobj, handle);
4059 rc = mdo_destroy(env, sobj, handle);
4061 mdd_write_unlock(env, sobj);
4066 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4067 spattr, tpattr, ldata, ma, handle);
4072 /* NB: if user issued different migrate command, we can't adjust it silently
4073 * here, because this command will decide target MDT in subdir migration in
4076 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4077 const struct lmv_mds_md_v1 *lmv,
4078 const struct lmv_user_md_v1 *lum,
4079 const struct lu_name *lname)
4081 __u32 lum_stripe_count = lum->lum_stripe_count;
4082 __u32 lmv_hash_type = lmv->lmv_hash_type;
4084 if (!lmv_is_sane(lmv))
4087 /* if stripe_count unspecified, set to 1 */
4088 if (!lum_stripe_count)
4089 lum_stripe_count = cpu_to_le32(1);
4091 lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4093 /* TODO: check specific MDTs */
4094 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4095 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4096 (lum->lum_hash_type && lum->lum_hash_type != lmv_hash_type)) {
4097 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4098 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4099 le32_to_cpu(lmv->lmv_master_mdt_index),
4100 le32_to_cpu(lmv->lmv_migrate_offset),
4101 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4110 * Internal function to migrate directory or file between MDTs.
4112 * migrate source to target in following steps:
4113 * 1. create target, append source stripes after target's if it's directory,
4114 * migrate xattrs and update fid of source links.
4115 * 2. update namespace: migrate dirent from source parent to target parent,
4116 * update file linkea, and destroy source if it's not needed any more.
4118 * \param[in] env execution environment
4119 * \param[in] spobj source parent object
4120 * \param[in] tpobj target parent object
4121 * \param[in] sobj source object
4122 * \param[in] tobj target object
4123 * \param[in] sname source file name
4124 * \param[in] tname target file name
4125 * \param[in] spec target creation spec
4126 * \param[in] ma used to update \a pobj mtime and ctime
4128 * \retval 0 on success
4129 * \retval -errno on failure
4131 static int mdd_migrate_object(const struct lu_env *env,
4132 struct mdd_object *spobj,
4133 struct mdd_object *tpobj,
4134 struct mdd_object *sobj,
4135 struct mdd_object *tobj,
4136 const struct lu_name *sname,
4137 const struct lu_name *tname,
4138 struct md_op_spec *spec,
4141 struct mdd_thread_info *info = mdd_env_info(env);
4142 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4143 struct lu_attr *spattr = &info->mti_pattr;
4144 struct lu_attr *tpattr = &info->mti_tpattr;
4145 struct lu_attr *attr = &info->mti_cattr;
4146 struct linkea_data *ldata = &info->mti_link_data;
4147 struct dt_allocation_hint *hint = &info->mti_hint;
4148 struct lu_buf sbuf = { NULL };
4149 struct lmv_mds_md_v1 *lmv;
4150 struct thandle *handle;
4155 rc = mdd_la_get(env, sobj, attr);
4159 rc = mdd_la_get(env, spobj, spattr);
4163 rc = mdd_la_get(env, tpobj, tpattr);
4167 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4168 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4172 /* if user use default value '0' for stripe_count, we need to
4173 * adjust it to '1' to create a 1-stripe directory.
4175 if (lum->lum_stripe_count == 0)
4176 lum->lum_stripe_count = cpu_to_le32(1);
4178 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4179 if (rc && rc != -ENODATA)
4184 if (!lmv_is_sane(lmv))
4185 GOTO(out, rc = -EBADF);
4186 if (lmv_is_migrating(lmv)) {
4187 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4192 } else if (!S_ISDIR(attr->la_mode)) {
4194 GOTO(out, rc = -EALREADY);
4196 /* update namespace only if @sobj is on MDT where @tpobj is. */
4197 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4198 spec->sp_migrate_nsonly = true;
4200 if (S_ISLNK(attr->la_mode)) {
4201 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4203 GOTO(out, rc = -ENOMEM);
4205 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4208 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4209 mdd2obd_dev(mdd)->obd_name,
4210 PFID(mdd_object_fid(sobj)), rc);
4216 /* linkea needs update upon FID or parent stripe change */
4217 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4218 tname, attr, ldata);
4220 /* update namespace only if @sobj has link on its MDT. */
4221 spec->sp_migrate_nsonly = true;
4225 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4226 spattr, tpattr, attr);
4230 handle = mdd_trans_create(env, mdd);
4232 GOTO(out, rc = PTR_ERR(handle));
4234 if (spec->sp_migrate_nsonly)
4235 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4236 tname, attr, spattr, tpattr,
4239 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4240 sname, tname, spattr, tpattr,
4241 attr, &sbuf, ldata, ma, spec,
4246 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4251 rc = mdd_trans_start(env, mdd, handle);
4255 if (spec->sp_migrate_nsonly)
4256 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4257 attr, spattr, tpattr, ldata, ma,
4260 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4261 tname, spattr, tpattr, attr, &sbuf,
4262 ldata, ma, spec, hint, handle);
4266 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4267 spec->sp_migrate_nsonly ? sobj : tobj,
4268 mdd_object_fid(spobj), mdd_object_fid(sobj),
4269 mdd_object_fid(tpobj), tname, sname,
4276 rc = mdd_trans_stop(env, mdd, rc, handle);
4284 * Migrate directory or file between MDTs.
4286 * \param[in] env execution environment
4287 * \param[in] md_pobj parent master object
4288 * \param[in] md_sobj source object
4289 * \param[in] lname file name
4290 * \param[in] md_tobj target object
4291 * \param[in] spec target creation spec
4292 * \param[in] ma used to update \a pobj mtime and ctime
4294 * \retval 0 on success
4295 * \retval -errno on failure
4297 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4298 struct md_object *md_sobj, const struct lu_name *lname,
4299 struct md_object *md_tobj, struct md_op_spec *spec,
4302 struct mdd_thread_info *info = mdd_env_info(env);
4303 struct mdd_device *mdd = mdo2mdd(md_pobj);
4304 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4305 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4306 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4307 struct mdd_object *spobj = NULL;
4308 struct mdd_object *tpobj = NULL;
4309 struct lu_buf pbuf = { NULL };
4310 struct lu_fid *fid = &info->mti_fid2;
4311 struct lmv_mds_md_v1 *lmv;
4316 /* locate source and target stripe on pobj, which are the real parent */
4317 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4318 if (rc < 0 && rc != -ENODATA)
4325 if (!lmv_is_sane(lmv))
4326 GOTO(out, rc = -EBADF);
4328 /* locate target parent stripe */
4329 /* fail check here to make sure top dir migration succeed. */
4330 if (lmv_is_migrating(lmv) &&
4331 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4332 GOTO(out, rc = -EIO);
4334 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4337 GOTO(out, rc = index);
4339 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4340 tpobj = mdd_object_find(env, mdd, fid);
4342 GOTO(out, rc = PTR_ERR(tpobj));
4344 /* locate source parent stripe */
4345 if (lmv_is_layout_changing(lmv)) {
4346 index = lmv_name_to_stripe_index_old(lmv,
4350 GOTO(out, rc = index);
4352 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4353 spobj = mdd_object_find(env, mdd, fid);
4355 GOTO(out, rc = PTR_ERR(spobj));
4357 /* parent stripe unchanged */
4358 if (spobj == tpobj) {
4359 if (!lmv_is_restriping(lmv))
4360 GOTO(out, rc = -EINVAL);
4361 GOTO(out, rc = -EALREADY);
4365 mdd_object_get(spobj);
4370 mdd_object_get(tpobj);
4371 mdd_object_get(spobj);
4374 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4379 if (!IS_ERR_OR_NULL(spobj))
4380 mdd_object_put(env, spobj);
4381 if (!IS_ERR_OR_NULL(tpobj))
4382 mdd_object_put(env, tpobj);
4388 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4389 struct mdd_object *pobj,
4390 struct mdd_object *obj,
4391 struct mdd_object *stripe,
4392 struct lu_attr *attr,
4393 struct md_layout_change *mlc,
4394 struct lu_name *lname,
4395 struct thandle *handle)
4399 mlc->mlc_opc = MD_LAYOUT_DETACH;
4400 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4404 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4405 S_IFDIR, dotdot, handle);
4409 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4410 mdo_declare_xattr_set);
4414 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4418 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4422 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4426 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4427 attr->la_mode, lname->ln_name, handle);
4431 rc = mdo_declare_ref_del(env, obj, handle);
4435 rc = mdo_declare_ref_del(env, obj, handle);
4439 rc = mdo_declare_destroy(env, obj, handle);
4446 /* transform one-stripe directory to a plain directory */
4447 static int mdd_1sd_collapse(const struct lu_env *env,
4448 struct mdd_object *pobj,
4449 struct mdd_object *obj,
4450 struct mdd_object *stripe,
4451 struct lu_attr *attr,
4452 struct md_layout_change *mlc,
4453 struct lu_name *lname,
4454 struct thandle *handle)
4460 /* replace 1-stripe directory with its stripe */
4461 mlc->mlc_opc = MD_LAYOUT_DETACH;
4463 mdd_write_lock(env, obj, DT_SRC_PARENT);
4464 rc = mdo_layout_change(env, obj, mlc, handle);
4465 mdd_write_unlock(env, obj);
4469 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4470 mdd_write_lock(env, obj, DT_SRC_CHILD);
4472 /* insert dotdot to stripe which points to parent */
4473 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4474 S_IFDIR, dotdot, handle);
4478 /* copy xattrs including linkea */
4479 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4484 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4488 /* don't set nlink from parent */
4489 attr->la_valid &= ~LA_NLINK;
4491 rc = mdo_attr_set(env, stripe, attr, handle);
4495 /* delete dir name from parent */
4496 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4500 /* insert stripe to parent with dir name */
4501 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4502 attr->la_mode, lname->ln_name, handle);
4506 /* destroy dir obj */
4507 rc = mdo_ref_del(env, obj, handle);
4511 rc = mdo_ref_del(env, obj, handle);
4515 rc = mdo_destroy(env, obj, handle);
4521 mdd_write_unlock(env, obj);
4522 mdd_write_unlock(env, pobj);
4528 * shrink directory stripes after migration/merge
4530 int mdd_dir_layout_shrink(const struct lu_env *env,
4531 struct md_object *md_obj,
4532 struct md_layout_change *mlc)
4534 struct mdd_device *mdd = mdo2mdd(md_obj);
4535 struct mdd_thread_info *info = mdd_env_info(env);
4536 struct mdd_object *obj = md2mdd_obj(md_obj);
4537 struct mdd_object *pobj = NULL;
4538 struct mdd_object *stripe = NULL;
4539 struct lu_attr *attr = &info->mti_pattr;
4540 struct lu_fid *fid = &info->mti_fid2;
4541 struct lu_name lname = { NULL };
4542 struct lu_buf lmv_buf = { NULL };
4543 struct lmv_mds_md_v1 *lmv;
4544 struct lmv_user_md *lmu;
4545 struct thandle *handle;
4550 rc = mdd_la_get(env, obj, attr);
4554 if (!S_ISDIR(attr->la_mode))
4557 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4561 lmv = lmv_buf.lb_buf;
4562 if (!lmv_is_sane(lmv))
4565 lmu = mlc->mlc_buf.lb_buf;
4567 /* adjust the default value '0' to '1' */
4568 if (lmu->lum_stripe_count == 0)
4569 lmu->lum_stripe_count = cpu_to_le32(1);
4571 /* these were checked in MDT */
4572 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4573 le32_to_cpu(lmv->lmv_stripe_count));
4574 LASSERT(!lmv_is_splitting(lmv));
4575 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4577 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4578 * to a plain dir, which will cause FID change and namespace update.
4580 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4581 struct linkea_data *ldata = &info->mti_link_data;
4582 char *filename = info->mti_name;
4584 rc = mdd_links_read(env, obj, ldata);
4588 if (ldata->ld_leh->leh_reccount > 1)
4589 GOTO(out, rc = -EINVAL);
4591 linkea_first_entry(ldata);
4593 GOTO(out, rc = -ENODATA);
4595 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4598 /* Note: lname might miss \0 at the end */
4599 snprintf(filename, sizeof(info->mti_name), "%.*s",
4600 lname.ln_namelen, lname.ln_name);
4601 lname.ln_name = filename;
4603 pobj = mdd_object_find(env, mdd, fid);
4610 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4612 stripe = mdd_object_find(env, mdd, fid);
4613 if (IS_ERR(stripe)) {
4614 mdd_object_put(env, pobj);
4616 GOTO(out, rc = PTR_ERR(stripe));
4620 handle = mdd_trans_create(env, mdd);
4622 GOTO(out, rc = PTR_ERR(handle));
4624 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4625 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4627 GOTO(stop_trans, rc);
4629 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4630 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4633 GOTO(stop_trans, rc);
4636 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4639 GOTO(stop_trans, rc);
4641 rc = mdd_trans_start(env, mdd, handle);
4643 GOTO(stop_trans, rc);
4645 mdd_write_lock(env, obj, DT_SRC_PARENT);
4646 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4647 rc = mdo_layout_change(env, obj, mlc, handle);
4648 mdd_write_unlock(env, obj);
4650 GOTO(stop_trans, rc);
4652 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4653 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4656 GOTO(stop_trans, rc);
4659 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4660 XATTR_NAME_LMV, handle);
4661 GOTO(stop_trans, rc);
4664 rc = mdd_trans_stop(env, mdd, rc, handle);
4667 mdd_object_put(env, stripe);
4668 mdd_object_put(env, pobj);
4670 lu_buf_free(&lmv_buf);
4674 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4675 struct mdd_device *mdd,
4676 struct mdd_object *pobj,
4677 struct mdd_object *obj,
4678 struct mdd_object *tobj,
4679 struct md_layout_change *mlc,
4680 struct dt_allocation_hint *hint,
4681 struct thandle *handle)
4683 struct mdd_thread_info *info = mdd_env_info(env);
4684 const struct lu_name *lname = mlc->mlc_name;
4685 struct lu_attr *la = &info->mti_la_for_fix;
4686 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4687 struct linkea_data *ldata = &info->mti_link_data;
4688 struct lmv_mds_md_v1 *lmv;
4692 mlc->mlc_opc = MD_LAYOUT_DETACH;
4693 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4697 memset(ldata, 0, sizeof(*ldata));
4698 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4699 lname, 1, 0, ldata);
4703 count = lum->lum_stripe_count;
4704 lum->lum_stripe_count = 0;
4705 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4707 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4708 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4709 ldata, NULL, NULL, NULL, hint);
4713 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4716 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4718 lmv = (typeof(lmv))info->mti_key;
4719 memset(lmv, 0, sizeof(*lmv));
4720 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4721 lmv->lmv_stripe_count = cpu_to_le32(1);
4722 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4723 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4725 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4726 mlc->mlc_buf.lb_buf = lmv;
4727 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4728 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4732 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4733 mdo_declare_xattr_set);
4737 lum->lum_stripe_count = count;
4738 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4739 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4743 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4747 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4748 S_IFDIR, lname->ln_name, handle);
4752 la->la_valid = LA_CTIME | LA_MTIME;
4753 rc = mdo_declare_attr_set(env, obj, la, handle);
4757 rc = mdo_declare_attr_set(env, pobj, la, handle);
4761 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4767 * plain directory split:
4768 * 1. create \a tobj as plain directory.
4769 * 2. append \a obj as first stripe of \a tobj.
4770 * 3. migrate xattrs from \a obj to \a tobj.
4771 * 4. split \a tobj to specific stripe count.
4773 static int mdd_dir_split_plain(const struct lu_env *env,
4774 struct mdd_device *mdd,
4775 struct mdd_object *pobj,
4776 struct mdd_object *obj,
4777 struct mdd_object *tobj,
4778 struct md_layout_change *mlc,
4779 struct dt_allocation_hint *hint,
4780 struct thandle *handle)
4782 struct mdd_thread_info *info = mdd_env_info(env);
4783 struct lu_attr *pattr = &info->mti_pattr;
4784 struct lu_attr *la = &info->mti_la_for_fix;
4785 const struct lu_name *lname = mlc->mlc_name;
4786 struct linkea_data *ldata = &info->mti_link_data;
4791 /* copy linkea out and set on target later */
4792 rc = mdd_links_read(env, obj, ldata);
4796 mlc->mlc_opc = MD_LAYOUT_DETACH;
4797 rc = mdo_layout_change(env, obj, mlc, handle);
4801 /* don't set nlink from obj */
4802 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4804 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4805 NULL, NULL, NULL, hint, handle, false);
4809 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4813 rc = mdd_links_write(env, tobj, ldata, handle);
4817 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4821 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4822 lname->ln_name, handle);
4826 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4827 la->la_valid = LA_CTIME | LA_MTIME;
4829 mdd_write_lock(env, obj, DT_SRC_CHILD);
4830 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4831 mdd_write_unlock(env, obj);
4835 rc = mdd_la_get(env, pobj, pattr);
4839 la->la_valid = LA_CTIME | LA_MTIME;
4841 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4842 rc = mdd_update_time(env, pobj, pattr, la, handle);
4843 mdd_write_unlock(env, pobj);
4847 /* FID changes, record it as CL_MIGRATE */
4848 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4849 mdd_object_fid(pobj), mdd_object_fid(obj),
4850 mdd_object_fid(pobj), lname, lname, handle);
4854 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4855 struct md_layout_change *mlc)
4857 struct mdd_thread_info *info = mdd_env_info(env);
4858 struct mdd_device *mdd = mdo2mdd(o);
4859 struct mdd_object *obj = md2mdd_obj(o);
4860 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4861 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4862 struct dt_allocation_hint *hint = &info->mti_hint;
4863 bool is_plain = false;
4864 struct thandle *handle;
4869 LASSERT(S_ISDIR(mdd_object_type(obj)));
4871 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4877 handle = mdd_trans_create(env, mdd);
4879 RETURN(PTR_ERR(handle));
4882 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4885 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4886 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4888 GOTO(stop_trans, rc);
4890 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4894 GOTO(stop_trans, rc);
4896 rc = mdd_trans_start(env, mdd, handle);
4898 GOTO(stop_trans, rc);
4901 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4904 mdd_write_lock(env, obj, DT_TGT_CHILD);
4905 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4906 LU_XATTR_CREATE, handle);
4907 mdd_write_unlock(env, obj);
4909 GOTO(stop_trans, rc);
4911 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4912 XATTR_NAME_LMV, handle);
4915 GOTO(stop_trans, rc);
4920 rc = mdd_trans_stop(env, mdd, rc, handle);
4925 const struct md_dir_operations mdd_dir_ops = {
4926 .mdo_is_subdir = mdd_is_subdir,
4927 .mdo_lookup = mdd_lookup,
4928 .mdo_create = mdd_create,
4929 .mdo_rename = mdd_rename,
4930 .mdo_link = mdd_link,
4931 .mdo_unlink = mdd_unlink,
4932 .mdo_create_data = mdd_create_data,
4933 .mdo_migrate = mdd_migrate,