4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdd/mdd_dir.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@intel.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_mds.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_idmap.h>
47 #include "mdd_internal.h"
49 static const char dot[] = ".";
50 static const char dotdot[] = "..";
52 static struct lu_name lname_dotdot = {
53 .ln_name = (char *) dotdot,
54 .ln_namelen = sizeof(dotdot) - 1,
58 mdd_name_check(const struct lu_env *env, struct mdd_device *m,
59 const struct lu_name *ln)
61 struct mdd_thread_info *info = mdd_env_info(env);
62 bool enc = info->mdi_pattr.la_valid & LA_FLAGS &&
63 info->mdi_pattr.la_flags & LUSTRE_ENCRYPT_FL;
65 if (!lu_name_is_valid(ln))
67 else if (!enc && ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
73 /* Get FID from name and parent */
75 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
76 const struct lu_attr *pattr, const struct lu_name *lname,
77 struct lu_fid *fid, unsigned int may_mask)
79 const char *name = lname->ln_name;
80 const struct dt_key *key = (const struct dt_key *)name;
81 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
82 struct dt_object *dir = mdd_object_child(mdd_obj);
87 if (unlikely(mdd_is_dead_obj(mdd_obj)))
90 if (!mdd_object_exists(mdd_obj))
93 if (mdd_object_remote(mdd_obj)) {
94 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
95 mdd_obj_dev_name(mdd_obj),
96 PFID(mdd_object_fid(mdd_obj)));
99 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
104 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
105 dt_try_as_dir(env, dir)))
106 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
113 int mdd_lookup(const struct lu_env *env,
114 struct md_object *pobj, const struct lu_name *lname,
115 struct lu_fid *fid, struct md_op_spec *spec)
117 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
121 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
125 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
126 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
130 /** Read the link EA into a temp buffer.
131 * Uses the mdd_thread_info::mdi_link_buf since it is generally large.
132 * A pointer to the buffer is stored in \a ldata::ld_buf.
136 static int __mdd_links_read(const struct lu_env *env,
137 struct mdd_object *mdd_obj,
138 struct linkea_data *ldata)
142 if (!mdd_object_exists(mdd_obj))
145 /* First try a small buf */
146 LASSERT(env != NULL);
147 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_link_buf,
149 if (ldata->ld_buf->lb_buf == NULL)
152 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
154 /* Buf was too small, figure out what we need. */
155 lu_buf_free(ldata->ld_buf);
156 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
160 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
161 if (ldata->ld_buf->lb_buf == NULL)
163 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
167 lu_buf_free(ldata->ld_buf);
168 ldata->ld_buf = NULL;
172 return linkea_init(ldata);
175 int mdd_links_read(const struct lu_env *env,
176 struct mdd_object *mdd_obj,
177 struct linkea_data *ldata)
181 rc = __mdd_links_read(env, mdd_obj, ldata);
183 rc = linkea_init(ldata);
188 static int mdd_links_read_with_rec(const struct lu_env *env,
189 struct mdd_object *mdd_obj,
190 struct linkea_data *ldata)
194 rc = __mdd_links_read(env, mdd_obj, ldata);
196 rc = linkea_init_with_rec(ldata);
202 * Get parent FID of the directory
204 * Read parent FID from linkEA, if that fails, then do lookup
205 * dotdot to get the parent FID.
207 * \param[in] env execution environment
208 * \param[in] obj object from which to find the parent FID
209 * \param[in] attr attribute of the object
210 * \param[out] fid fid to get the parent FID
212 * \retval 0 if getting the parent FID succeeds.
213 * \retval negative errno if getting the parent FID fails.
215 static inline int mdd_parent_fid(const struct lu_env *env,
216 struct mdd_object *obj,
217 const struct lu_attr *attr,
220 struct mdd_thread_info *info = mdd_env_info(env);
221 struct linkea_data ldata = { NULL };
222 struct lu_buf *buf = &info->mdi_link_buf;
223 struct lu_name lname;
228 LASSERTF(S_ISDIR(mdd_object_type(obj)),
229 "%s: FID "DFID" is not a directory type = %o\n",
230 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
231 mdd_object_type(obj));
233 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
234 if (buf->lb_buf == NULL)
235 GOTO(lookup, rc = 0);
238 rc = mdd_links_read_with_rec(env, obj, &ldata);
242 /* the obj is not locked, don't cache attributes */
243 mdd_invalidate(env, &obj->mod_obj);
245 LASSERT(ldata.ld_leh != NULL);
246 /* Directory should only have 1 parent */
247 if (ldata.ld_leh->leh_reccount > 1)
250 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
252 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
253 if (likely(fid_is_sane(fid)))
256 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
261 * For root fid use special function, which does not compare version component
262 * of fid. Version component is different for root fids on all MDTs.
264 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
266 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
267 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
271 * return 1: if \a tfid is the fid of the ancestor of \a mo;
273 * otherwise: values < 0, errors.
275 static int mdd_is_parent(const struct lu_env *env,
276 struct mdd_device *mdd,
277 struct mdd_object *mo,
278 const struct lu_attr *attr,
279 const struct lu_fid *tfid)
281 struct mdd_object *mp;
285 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
286 pfid = &mdd_env_info(env)->mdi_fid;
288 if (mdd_is_root(mdd, mdd_object_fid(mo)))
291 if (mdd_is_root(mdd, tfid))
294 rc = mdd_parent_fid(env, mo, attr, pfid);
299 if (lu_fid_eq(pfid, tfid))
302 if (mdd_is_root(mdd, pfid))
305 mp = mdd_object_find(env, mdd, pfid);
309 if (!mdd_object_exists(mp)) {
310 mdd_object_put(env, mp);
314 rc = mdd_parent_fid(env, mp, attr, pfid);
315 mdd_object_put(env, mp);
324 * No permission check is needed.
326 * returns 1: if fid is ancestor of @mo;
327 * returns 0: if fid is not an ancestor of @mo;
328 * returns < 0: if error
330 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
331 const struct lu_fid *fid)
333 struct mdd_device *mdd = mdo2mdd(mo);
334 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
338 if (!mdd_object_exists(md2mdd_obj(mo)))
341 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
344 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
348 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
353 * Check that @dir contains no entries except (possibly) dot and dotdot.
358 * -ENOTDIR not a directory object
359 * -ENOTEMPTY not empty
363 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
366 struct dt_object *obj;
367 const struct dt_it_ops *iops;
371 obj = mdd_object_child(dir);
372 if (!dt_try_as_dir(env, obj))
375 iops = &obj->do_index_ops->dio_it;
376 it = iops->init(env, obj, LUDA_64BITHASH);
378 result = iops->get(env, it, (const struct dt_key *)"");
381 for (result = 0, i = 0; result == 0 && i < 3; ++i)
382 result = iops->next(env, it);
385 else if (result == 1)
387 } else if (result == 0)
389 * Huh? Index contains no zero key?
396 result = PTR_ERR(it);
397 /* -ENODEV means no valid stripe */
398 if (result == -ENODEV)
405 * Determine if the target object can be hard linked, and right now it only
406 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
407 * directory nlink count might exceed the maximum link count(see
408 * osd_object_ref_add), so it only check nlink for non-directories.
410 * \param[in] env thread environment
411 * \param[in] obj object being linked to
412 * \param[in] la attributes of \a obj
414 * \retval 0 if \a obj can be hard linked
415 * \retval negative error if \a obj is a directory or has too
418 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
419 const struct lu_attr *la)
421 struct mdd_device *m = mdd_obj2mdd_dev(obj);
426 /* Subdir count limitation can be broken through
427 * (see osd_object_ref_add), so only check non-directory here. */
428 if (!S_ISDIR(la->la_mode) &&
429 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
436 * Check whether it may create the cobj under the pobj.
438 * \param[in] env execution environment
439 * \param[in] pobj the parent directory
440 * \param[in] pattr the attribute of the parent directory
441 * \param[in] cobj the child to be created
442 * \param[in] check_perm if check WRITE|EXEC permission for parent
444 * \retval = 0 create the child under this dir is allowed
445 * \retval negative errno create the child under this dir is
448 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
449 const struct lu_attr *pattr, struct mdd_object *cobj,
455 if (cobj && mdd_object_exists(cobj))
458 if (mdd_is_dead_obj(pobj))
462 rc = mdd_permission_internal_locked(env, pobj, pattr,
463 MAY_WRITE | MAY_EXEC,
469 * Check whether can unlink from the pobj in the case of "cobj == NULL".
471 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
472 const struct lu_attr *pattr, const struct lu_attr *attr)
477 if (mdd_is_dead_obj(pobj))
480 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
483 rc = mdd_permission_internal_locked(env, pobj, pattr,
484 MAY_WRITE | MAY_EXEC,
489 if (pattr->la_flags & LUSTRE_APPEND_FL)
496 * pobj == NULL is remote ops case, under such case, pobj's
497 * VTX feature has been checked already, no need check again.
499 static inline int mdd_is_sticky(const struct lu_env *env,
500 struct mdd_object *pobj,
501 const struct lu_attr *pattr,
502 struct mdd_object *cobj,
503 const struct lu_attr *cattr)
505 struct lu_ucred *uc = lu_ucred_assert(env);
508 LASSERT(pattr != NULL);
509 if (!(pattr->la_mode & S_ISVTX) ||
510 (pattr->la_uid == uc->uc_fsuid))
514 LASSERT(cattr != NULL);
515 if (cattr->la_uid == uc->uc_fsuid)
518 return !md_capable(uc, CAP_FOWNER);
521 static int mdd_may_delete_entry(const struct lu_env *env,
522 struct mdd_object *pobj,
523 const struct lu_attr *pattr,
528 LASSERT(pobj != NULL);
529 if (!mdd_object_exists(pobj))
532 if (mdd_is_dead_obj(pobj))
537 rc = mdd_permission_internal_locked(env, pobj, pattr,
538 MAY_WRITE | MAY_EXEC,
544 if (pattr->la_flags & LUSTRE_APPEND_FL)
551 * Check whether it may delete the cobj from the pobj.
554 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
555 const struct lu_attr *tpattr, struct mdd_object *tobj,
556 const struct lu_attr *tattr, const struct lu_attr *cattr,
557 int check_perm, int check_empty)
563 LASSERT(tpattr != NULL);
564 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
572 if (!mdd_object_exists(tobj))
575 if (mdd_is_dead_obj(tobj))
578 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
581 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
584 /* additional check the rename case */
586 if (S_ISDIR(cattr->la_mode)) {
587 if (!S_ISDIR(tattr->la_mode))
590 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
591 mdd_object_fid(tobj)))
593 } else if (S_ISDIR(tattr->la_mode))
597 if (S_ISDIR(tattr->la_mode) && check_empty)
598 rc = mdd_dir_is_empty(env, tobj);
604 * Check whether it can create the link file(linked to @src_obj) under
605 * the target directory(@tgt_obj), and src_obj has been locked by
608 * \param[in] env execution environment
609 * \param[in] tgt_obj the target directory
610 * \param[in] tattr attributes of target directory
611 * \param[in] lname the link name
612 * \param[in] src_obj source object for link
613 * \param[in] cattr attributes for source object
615 * \retval = 0 it is allowed to create the link file under tgt_obj
616 * \retval negative error not allowed to create the link file
618 static int mdd_link_sanity_check(const struct lu_env *env,
619 struct mdd_object *tgt_obj,
620 const struct lu_attr *tattr,
621 const struct lu_name *lname,
622 struct mdd_object *src_obj,
623 const struct lu_attr *cattr)
625 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
629 if (!mdd_object_exists(src_obj))
632 if (mdd_is_dead_obj(src_obj))
635 /* Local ops, no lookup before link, check filename length here. */
636 rc = mdd_name_check(env, m, lname);
640 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
643 if (S_ISDIR(mdd_object_type(src_obj)))
646 LASSERT(src_obj != tgt_obj);
647 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
651 rc = __mdd_may_link(env, src_obj, cattr);
656 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
657 const char *name, struct thandle *handle)
659 struct dt_object *next = mdd_object_child(pobj);
663 if (dt_try_as_dir(env, next))
664 rc = dt_delete(env, next, (struct dt_key *)name, handle);
671 static int __mdd_index_insert_only(const struct lu_env *env,
672 struct mdd_object *pobj,
673 const struct lu_fid *lf, __u32 type,
674 const char *name, struct thandle *handle)
676 struct dt_object *next = mdd_object_child(pobj);
680 if (dt_try_as_dir(env, next)) {
681 struct dt_insert_rec *rec = &mdd_env_info(env)->mdi_dt_rec;
684 rec->rec_type = type;
685 rc = dt_insert(env, next, (const struct dt_rec *)rec,
686 (const struct dt_key *)name, handle);
693 /* insert named index, add reference if isdir */
694 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
695 const struct lu_fid *lf, __u32 type,
696 const char *name, struct thandle *handle)
701 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
702 if (rc == 0 && S_ISDIR(type)) {
703 mdd_write_lock(env, pobj, DT_TGT_PARENT);
704 mdo_ref_add(env, pobj, handle);
705 mdd_write_unlock(env, pobj);
711 /* delete named index, drop reference if isdir */
712 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
713 const char *name, int is_dir,
714 struct thandle *handle)
719 rc = __mdd_index_delete_only(env, pobj, name, handle);
720 if (rc == 0 && is_dir) {
721 mdd_write_lock(env, pobj, DT_TGT_PARENT);
722 mdo_ref_del(env, pobj, handle);
723 mdd_write_unlock(env, pobj);
729 static int mdd_llog_record_calc_size(const struct lu_env *env,
730 const struct lu_name *tname,
731 const struct lu_name *sname)
733 const struct lu_ucred *uc = lu_ucred(env);
734 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
735 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
738 clf_flags |= CLF_RENAME;
740 if (uc != NULL && uc->uc_jobid[0] != '\0')
741 clf_flags |= CLF_JOBID;
743 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
744 changelog_rec_offset(clf_flags, crfe) +
745 (tname != NULL ? tname->ln_namelen : 0) +
746 (sname != NULL ? 1 + sname->ln_namelen : 0));
749 int mdd_declare_changelog_store(const struct lu_env *env,
750 struct mdd_device *mdd,
751 enum changelog_rec_type type,
752 const struct lu_name *tname,
753 const struct lu_name *sname,
754 struct thandle *handle)
756 struct obd_device *obd = mdd2obd_dev(mdd);
757 struct llog_ctxt *ctxt;
758 struct llog_rec_hdr rec_hdr;
759 struct thandle *llog_th;
762 if (!mdd_changelog_enabled(env, mdd, type))
765 rec_hdr.lrh_len = mdd_llog_record_calc_size(env, tname, sname);
766 rec_hdr.lrh_type = CHANGELOG_REC;
768 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
772 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
774 GOTO(out_put, rc = PTR_ERR(llog_th));
776 rc = llog_declare_add(env, ctxt->loc_handle, &rec_hdr, llog_th);
784 int mdd_changelog_write_rec(const struct lu_env *env,
785 struct llog_handle *loghandle,
786 struct llog_rec_hdr *r,
787 struct llog_cookie *cookie,
788 int idx, struct thandle *th)
792 if (r->lrh_type == CHANGELOG_REC) {
793 struct mdd_device *mdd;
794 struct llog_changelog_rec *rec;
796 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
797 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
799 spin_lock(&mdd->mdd_cl.mc_lock);
800 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
801 spin_unlock(&mdd->mdd_cl.mc_lock);
803 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
807 * if current llog is full, we will generate a new
808 * llog, and since it's actually not an error, let's
809 * avoid increasing index so that userspace apps
810 * should not see a gap in the changelog sequence
812 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
813 spin_lock(&mdd->mdd_cl.mc_lock);
814 ++mdd->mdd_cl.mc_index;
815 spin_unlock(&mdd->mdd_cl.mc_lock);
818 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
825 /** Add a changelog entry \a rec to the changelog llog
828 * \param handle - currently ignored since llogs start their own transaction;
829 * this will hopefully be fixed in llog rewrite
832 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
833 struct llog_changelog_rec *rec, struct thandle *th)
835 struct obd_device *obd = mdd2obd_dev(mdd);
836 struct llog_ctxt *ctxt;
837 struct thandle *llog_th;
840 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
841 changelog_rec_varsize(&rec->cr));
843 /* llog_lvfs_write_rec sets the llog tail len */
844 rec->cr_hdr.lrh_type = CHANGELOG_REC;
845 rec->cr.cr_time = cl_time();
847 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
851 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
853 GOTO(out_put, rc = PTR_ERR(llog_th));
855 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
856 /* nested journal transaction */
857 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
859 /* time to recover some space ?? */
860 if (likely(!mdd->mdd_changelog_gc ||
861 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
862 mdd->mdd_changelog_min_gc_interval >=
863 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
864 /* save a spin_lock trip */
866 spin_lock(&mdd->mdd_cl.mc_lock);
867 if (likely(mdd->mdd_changelog_gc &&
868 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
869 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
870 mdd->mdd_changelog_min_gc_interval)) {
871 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
872 mdd->mdd_changelog_min_free_cat_entries ||
873 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
874 CWARN("%s:%s low on changelog_catalog free entries, "
875 "starting ChangeLog garbage collection thread\n",
877 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
880 /* indicate further kthread run will occur outside
881 * right after current journal transaction filling has
884 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
886 /* next check in mdd_changelog_min_gc_interval anyway
888 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
890 spin_unlock(&mdd->mdd_cl.mc_lock);
898 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
899 const struct lu_fid *sfid,
900 const struct lu_fid *spfid,
901 const struct lu_name *sname)
903 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
906 LASSERT(sfid != NULL);
907 LASSERT(spfid != NULL);
908 LASSERT(sname != NULL);
910 extsize = sname->ln_namelen + 1;
912 rnm->cr_sfid = *sfid;
913 rnm->cr_spfid = *spfid;
915 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
916 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
917 rec->cr_namelen += extsize;
920 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
922 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
924 if (jobid == NULL || jobid[0] == '\0')
927 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
930 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
932 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
934 ef->cr_extra_flags = eflags;
937 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
938 __u64 uid, __u64 gid)
940 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
942 uidgid->cr_uid = uid;
943 uidgid->cr_gid = gid;
946 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
949 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
954 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
956 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
958 omd->cr_openflags = flags;
961 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
962 const char *xattr_name)
964 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
966 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
969 /** Store a namespace change changelog record
970 * If this fails, we must fail the whole transaction; we don't
971 * want the change to commit without the log entry.
972 * \param target - mdd_object of change
973 * \param tpfid - target parent dir/object fid
974 * \param sfid - source object fid
975 * \param spfid - source parent fid
976 * \param tname - target name string
977 * \param sname - source name string
978 * \param handle - transaction handle
980 int mdd_changelog_ns_store(const struct lu_env *env,
981 struct mdd_device *mdd,
982 enum changelog_rec_type type,
983 enum changelog_rec_flags clf_flags,
984 struct mdd_object *target,
985 const struct lu_fid *tpfid,
986 const struct lu_fid *sfid,
987 const struct lu_fid *spfid,
988 const struct lu_name *tname,
989 const struct lu_name *sname,
990 struct thandle *handle)
992 const struct lu_ucred *uc = lu_ucred(env);
993 struct llog_changelog_rec *rec;
996 __u64 xflags = CLFE_INVALID;
1000 if (!mdd_changelog_enabled(env, mdd, type))
1003 LASSERT(tpfid != NULL);
1004 LASSERT(tname != NULL);
1005 LASSERT(handle != NULL);
1007 reclen = mdd_llog_record_calc_size(env, tname, sname);
1008 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
1009 if (buf->lb_buf == NULL)
1013 clf_flags &= CLF_FLAGMASK;
1014 clf_flags |= CLF_EXTRA_FLAGS;
1017 if (uc->uc_jobid[0] != '\0')
1018 clf_flags |= CLF_JOBID;
1019 xflags |= CLFE_UIDGID;
1024 clf_flags |= CLF_RENAME;
1026 clf_flags |= CLF_VERSION;
1028 rec->cr.cr_flags = clf_flags;
1030 if (clf_flags & CLF_EXTRA_FLAGS) {
1031 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1032 if (xflags & CLFE_UIDGID)
1033 mdd_changelog_rec_extra_uidgid(&rec->cr,
1034 uc->uc_uid, uc->uc_gid);
1035 if (xflags & CLFE_NID)
1036 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1039 rec->cr.cr_type = (__u32)type;
1040 rec->cr.cr_pfid = *tpfid;
1041 rec->cr.cr_namelen = tname->ln_namelen;
1042 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1044 if (clf_flags & CLF_RENAME)
1045 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1047 if (clf_flags & CLF_JOBID)
1048 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1050 if (likely(target != NULL)) {
1051 rec->cr.cr_tfid = *mdd_object_fid(target);
1052 target->mod_cltime = ktime_get();
1054 fid_zero(&rec->cr.cr_tfid);
1057 rc = mdd_changelog_store(env, mdd, rec, handle);
1059 CERROR("%s: cannot store changelog record: type = %d, "
1060 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1061 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1062 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1069 static int __mdd_links_add(const struct lu_env *env,
1070 struct mdd_object *mdd_obj,
1071 struct linkea_data *ldata,
1072 const struct lu_name *lname,
1073 const struct lu_fid *pfid,
1074 int first, int check)
1076 /* cattr is set in mdd_link */
1077 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1080 if (ldata->ld_leh == NULL) {
1081 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1085 rc = linkea_data_new(ldata,
1086 &mdd_env_info(env)->mdi_link_buf);
1093 rc = linkea_links_find(ldata, lname, pfid);
1094 if (rc && rc != -ENOENT)
1100 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1101 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1105 linkea_add_buf(ldata, lname, tfid, false);
1108 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1109 linkea_add_buf(ldata, lname, pfid, false);
1111 /* For encrypted file, we want to limit number of hard links to what
1112 * linkEA can contain. So ask to return error in case of overflow.
1113 * Currently linkEA stores 4KiB of links, that is 14 NAME_MAX links,
1114 * or 119 16-byte names.
1116 return linkea_add_buf(ldata, lname, pfid,
1117 cattr->la_valid & LA_FLAGS &&
1118 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1121 static int __mdd_links_del(const struct lu_env *env,
1122 struct mdd_object *mdd_obj,
1123 struct linkea_data *ldata,
1124 const struct lu_name *lname,
1125 const struct lu_fid *pfid)
1127 /* cattr is set in mdd_link */
1128 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1131 if (ldata->ld_leh == NULL) {
1132 rc = mdd_links_read(env, mdd_obj, ldata);
1137 rc = linkea_links_find(ldata, lname, pfid);
1141 linkea_del_buf(ldata, lname,
1142 cattr->la_valid & LA_FLAGS &&
1143 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1147 static int mdd_linkea_prepare(const struct lu_env *env,
1148 struct mdd_object *mdd_obj,
1149 const struct lu_fid *oldpfid,
1150 const struct lu_name *oldlname,
1151 const struct lu_fid *newpfid,
1152 const struct lu_name *newlname,
1153 int first, int check,
1154 struct linkea_data *ldata)
1159 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1162 LASSERT(oldpfid != NULL || newpfid != NULL);
1164 if (mdd_obj->mod_flags & DEAD_OBJ)
1165 /* Unnecessary to update linkEA for dead object. */
1168 if (oldpfid != NULL) {
1169 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1171 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1174 /* No changes done. */
1179 /* If renaming, add the new record */
1180 if (newpfid != NULL)
1181 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1187 int mdd_links_rename(const struct lu_env *env,
1188 struct mdd_object *mdd_obj,
1189 const struct lu_fid *oldpfid,
1190 const struct lu_name *oldlname,
1191 const struct lu_fid *newpfid,
1192 const struct lu_name *newlname,
1193 struct thandle *handle,
1194 struct linkea_data *ldata,
1195 int first, int check)
1200 if (ldata == NULL) {
1201 ldata = &mdd_env_info(env)->mdi_link_data;
1202 memset(ldata, 0, sizeof(*ldata));
1203 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1204 newpfid, newlname, first, check, ldata);
1209 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1210 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1216 if (newlname == NULL)
1217 CERROR("link_ea add failed %d "DFID"\n",
1218 rc, PFID(mdd_object_fid(mdd_obj)));
1219 else if (oldpfid == NULL)
1220 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1221 newlname->ln_namelen, newlname->ln_name, rc,
1222 PFID(mdd_object_fid(mdd_obj)));
1223 else if (newpfid == NULL)
1224 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1225 oldlname->ln_namelen, oldlname->ln_name, rc,
1226 PFID(mdd_object_fid(mdd_obj)));
1228 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1229 "\n", oldlname->ln_namelen, oldlname->ln_name,
1230 newlname->ln_namelen, newlname->ln_name, rc,
1231 PFID(mdd_object_fid(mdd_obj)));
1234 if (is_vmalloc_addr(ldata->ld_buf))
1235 /* if we vmalloced a large buffer drop it */
1236 lu_buf_free(ldata->ld_buf);
1241 static inline int mdd_links_add(const struct lu_env *env,
1242 struct mdd_object *mdd_obj,
1243 const struct lu_fid *pfid,
1244 const struct lu_name *lname,
1245 struct thandle *handle,
1246 struct linkea_data *ldata, int first)
1248 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1249 pfid, lname, handle, ldata, first, 0);
1252 static inline int mdd_links_del(const struct lu_env *env,
1253 struct mdd_object *mdd_obj,
1254 const struct lu_fid *pfid,
1255 const struct lu_name *lname,
1256 struct thandle *handle)
1258 return mdd_links_rename(env, mdd_obj, pfid, lname,
1259 NULL, NULL, handle, NULL, 0, 0);
1262 /** Read the link EA into a temp buffer.
1263 * Uses the name_buf since it is generally large.
1264 * \retval IS_ERR err
1265 * \retval ptr to \a lu_buf (always \a mdi_link_buf)
1267 struct lu_buf *mdd_links_get(const struct lu_env *env,
1268 struct mdd_object *mdd_obj)
1270 struct linkea_data ldata = { NULL };
1273 rc = mdd_links_read(env, mdd_obj, &ldata);
1274 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1277 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1278 struct linkea_data *ldata, struct thandle *handle)
1280 const struct lu_buf *buf;
1283 if (ldata == NULL || ldata->ld_buf == NULL ||
1284 ldata->ld_leh == NULL)
1287 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1291 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1292 ldata->ld_leh->leh_len);
1293 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1294 if (unlikely(rc == -ENOSPC)) {
1295 rc = linkea_overflow_shrink(ldata);
1303 static int mdd_declare_links_add(const struct lu_env *env,
1304 struct mdd_object *mdd_obj,
1305 struct thandle *handle,
1306 struct linkea_data *ldata)
1312 if (ldata != NULL && ldata->ld_leh != NULL) {
1313 ea_len = ldata->ld_leh->leh_len;
1314 linkea = ldata->ld_buf->lb_buf;
1316 ea_len = MAX_LINKEA_SIZE;
1320 rc = mdo_declare_xattr_set(env, mdd_obj,
1321 mdd_buf_get_const(env, linkea, ea_len),
1322 XATTR_NAME_LINK, 0, handle);
1327 static inline int mdd_declare_links_del(const struct lu_env *env,
1328 struct mdd_object *c,
1329 struct thandle *handle)
1333 /* For directory, the linkEA will be removed together
1334 * with the object. */
1335 if (!S_ISDIR(mdd_object_type(c)))
1336 rc = mdd_declare_links_add(env, c, handle, NULL);
1341 static int mdd_declare_link(const struct lu_env *env,
1342 struct mdd_device *mdd,
1343 struct mdd_object *p,
1344 struct mdd_object *c,
1345 const struct lu_name *name,
1346 struct thandle *handle,
1348 struct linkea_data *data)
1350 struct lu_fid tfid = *mdd_object_fid(c);
1353 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1354 tfid.f_oid = cfs_fail_val;
1356 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1357 name->ln_name, handle);
1361 rc = mdo_declare_ref_add(env, c, handle);
1365 la->la_valid = LA_CTIME | LA_MTIME;
1366 rc = mdo_declare_attr_set(env, p, la, handle);
1370 la->la_valid = LA_CTIME;
1371 rc = mdo_declare_attr_set(env, c, la, handle);
1375 rc = mdd_declare_links_add(env, c, handle, data);
1379 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1385 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1386 struct md_object *src_obj, const struct lu_name *lname,
1389 const char *name = lname->ln_name;
1390 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1391 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1392 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1393 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1394 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1395 struct mdd_device *mdd = mdo2mdd(src_obj);
1396 struct thandle *handle;
1397 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1398 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
1402 rc = mdd_la_get(env, mdd_sobj, cattr);
1406 rc = mdd_la_get(env, mdd_tobj, tattr);
1411 * If we are using project inheritance, we only allow hard link
1412 * creation in our tree when the project IDs are the same;
1413 * otherwise the tree quota mechanism could be circumvented.
1415 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1416 (tattr->la_projid != cattr->la_projid))
1419 handle = mdd_trans_create(env, mdd);
1421 GOTO(out_pending, rc = PTR_ERR(handle));
1423 memset(ldata, 0, sizeof(*ldata));
1425 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1426 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1428 /* Note: even this function will change ldata, but it comes from
1429 * thread_info, which is completely temporary and only seen in
1430 * this function, so we do not need reset ldata once it fails.*/
1431 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1432 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1436 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1441 rc = mdd_trans_start(env, mdd, handle);
1445 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1446 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1449 GOTO(out_unlock, rc);
1451 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1452 rc = mdo_ref_add(env, mdd_sobj, handle);
1454 GOTO(out_unlock, rc);
1457 *tfid = *mdd_object_fid(mdd_sobj);
1458 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1459 tfid->f_oid = cfs_fail_val;
1461 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1462 mdd_object_type(mdd_sobj), name, handle);
1464 mdo_ref_del(env, mdd_sobj, handle);
1465 GOTO(out_unlock, rc);
1468 la->la_valid = LA_CTIME | LA_MTIME;
1469 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1471 GOTO(out_unlock, rc);
1473 la->la_valid = LA_CTIME;
1474 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1476 /* Note: The failure of links_add should not cause the
1477 * link failure, so do not check return value. */
1478 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1479 lname, handle, ldata, 0);
1483 mdd_write_unlock(env, mdd_sobj);
1485 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1486 mdd_object_fid(mdd_tobj), NULL,
1487 NULL, lname, NULL, handle);
1489 rc = mdd_trans_stop(env, mdd, rc, handle);
1490 if (is_vmalloc_addr(ldata->ld_buf))
1491 /* if we vmalloced a large buffer drop it */
1492 lu_buf_free(ldata->ld_buf);
1497 static int mdd_mark_orphan_object(const struct lu_env *env,
1498 struct mdd_object *obj, struct thandle *handle,
1501 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1504 attr->la_valid = LA_FLAGS;
1505 attr->la_flags = LUSTRE_ORPHAN_FL;
1508 rc = mdo_declare_attr_set(env, obj, attr, handle);
1510 rc = mdo_attr_set(env, obj, attr, handle);
1515 static int mdd_declare_finish_unlink(const struct lu_env *env,
1516 struct mdd_object *obj,
1517 struct thandle *handle)
1521 /* Sigh, we do not know if the unlink object will become orphan in
1522 * declare phase, but fortunately the flags here does not matter
1523 * in current declare implementation */
1524 rc = mdd_mark_orphan_object(env, obj, handle, true);
1528 rc = mdo_declare_destroy(env, obj, handle);
1532 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1536 return mdd_declare_links_del(env, obj, handle);
1539 /* caller should take a lock before calling */
1540 int mdd_finish_unlink(const struct lu_env *env,
1541 struct mdd_object *obj, struct md_attr *ma,
1542 struct mdd_object *pobj,
1543 const struct lu_name *lname,
1547 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1550 LASSERT(mdd_write_locked(env, obj) != 0);
1552 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1553 /* add new orphan and the object
1554 * will be deleted during mdd_close() */
1555 obj->mod_flags |= DEAD_OBJ;
1556 if (obj->mod_count) {
1557 rc = mdd_orphan_insert(env, obj, th);
1559 CDEBUG(D_HA, "Object "DFID" is inserted into "
1560 "orphan list, open count = %d\n",
1561 PFID(mdd_object_fid(obj)),
1564 CERROR("Object "DFID" fail to be an orphan, "
1565 "open count = %d, maybe cause failed "
1567 PFID(mdd_object_fid(obj)),
1570 /* mark object as an orphan here, not
1571 * before mdd_orphan_insert() as racing
1572 * mdd_la_get() may propagate ORPHAN_OBJ
1573 * causing the asserition */
1574 rc = mdd_mark_orphan_object(env, obj, th, false);
1576 rc = mdo_destroy(env, obj, th);
1578 } else if (!is_dir) {
1579 /* old files may not have link ea; ignore errors */
1580 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1588 * has mdd_write_lock on cobj already, but not on pobj yet
1590 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1591 const struct lu_attr *pattr,
1592 struct mdd_object *cobj,
1593 const struct lu_attr *cattr)
1598 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1603 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1604 struct mdd_object *p, struct mdd_object *c,
1605 const struct lu_name *name, struct md_attr *ma,
1606 struct thandle *handle, int no_name, int is_dir)
1608 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1611 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1612 if (likely(no_name == 0)) {
1613 rc = mdo_declare_index_delete(env, p, name->ln_name,
1620 rc = mdo_declare_ref_del(env, p, handle);
1626 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1627 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1628 la->la_valid = LA_CTIME | LA_MTIME;
1629 rc = mdo_declare_attr_set(env, p, la, handle);
1634 rc = mdo_declare_ref_del(env, c, handle);
1638 rc = mdo_declare_ref_del(env, c, handle);
1642 la->la_valid = LA_CTIME;
1643 rc = mdo_declare_attr_set(env, c, la, handle);
1647 rc = mdd_declare_finish_unlink(env, c, handle);
1651 /* FIXME: need changelog for remove entry */
1652 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1660 * test if a file has an HSM archive
1661 * if HSM attributes are not found in ma update them from
1664 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1665 struct mdd_object *obj,
1670 if (!(ma->ma_valid & MA_HSM)) {
1671 /* no HSM MD provided, read xattr */
1672 struct lu_buf *hsm_buf;
1673 const size_t buflen = sizeof(struct hsm_attrs);
1676 hsm_buf = mdd_buf_get(env, NULL, 0);
1677 lu_buf_alloc(hsm_buf, buflen);
1678 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1679 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1680 lu_buf_free(hsm_buf);
1684 ma->ma_valid |= MA_HSM;
1686 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1692 * Delete name entry and the object.
1693 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1694 * does not exist for this object, and it could only happen during resending
1695 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1696 * is also needed in this case(needed by changelog), so we have to add another
1697 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1698 * the ENOENT failure should be able to be fixed by redo mechanism.
1700 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1701 struct md_object *cobj, const struct lu_name *lname,
1702 struct md_attr *ma, int no_name)
1704 const char *name = lname->ln_name;
1705 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1706 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1707 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1708 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1709 struct mdd_object *mdd_cobj = NULL;
1710 struct mdd_device *mdd = mdo2mdd(pobj);
1711 struct thandle *handle;
1712 int rc, is_dir = 0, cl_flags = 0;
1715 /* let shutdown to start */
1716 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1718 /* cobj == NULL means only delete name entry */
1719 if (likely(cobj != NULL)) {
1720 mdd_cobj = md2mdd_obj(cobj);
1721 if (mdd_object_exists(mdd_cobj) == 0)
1725 rc = mdd_la_get(env, mdd_pobj, pattr);
1729 if (likely(mdd_cobj != NULL)) {
1731 rc = mdd_la_get(env, mdd_cobj, cattr);
1735 is_dir = S_ISDIR(cattr->la_mode);
1736 /* search for an existing archive.
1737 * we should check ahead as the object
1738 * can be destroyed in this transaction */
1739 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1740 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1743 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1747 handle = mdd_trans_create(env, mdd);
1749 RETURN(PTR_ERR(handle));
1751 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1752 lname, ma, handle, no_name, is_dir);
1756 rc = mdd_trans_start(env, mdd, handle);
1760 if (likely(mdd_cobj != NULL))
1761 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1763 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1764 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1769 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1770 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1771 GOTO(cleanup, rc = 0);
1773 if (likely(mdd_cobj != NULL)) {
1774 rc = mdo_ref_del(env, mdd_cobj, handle);
1776 __mdd_index_insert_only(env, mdd_pobj,
1777 mdd_object_fid(mdd_cobj),
1778 mdd_object_type(mdd_cobj),
1785 mdo_ref_del(env, mdd_cobj, handle);
1787 /* fetch updated nlink */
1788 rc = mdd_la_get(env, mdd_cobj, cattr);
1793 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1794 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1796 la->la_valid = LA_CTIME | LA_MTIME;
1797 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1801 /* Enough for only unlink the entry */
1802 if (unlikely(mdd_cobj == NULL))
1805 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1806 /* update ctime of an unlinked file only if it is still
1807 * opened or a link still exists */
1808 la->la_valid = LA_CTIME;
1809 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1814 /* XXX: this transfer to ma will be removed with LOD/OSP */
1815 ma->ma_attr = *cattr;
1816 ma->ma_valid |= MA_INODE;
1817 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1821 /* fetch updated nlink */
1822 rc = mdd_la_get(env, mdd_cobj, cattr);
1823 /* if object is removed then we can't get its attrs,
1825 if (rc == -ENOENT) {
1826 cattr->la_nlink = 0;
1830 if (cattr->la_nlink == 0) {
1831 ma->ma_attr = *cattr;
1832 ma->ma_valid |= MA_INODE;
1837 if (likely(mdd_cobj != NULL))
1838 mdd_write_unlock(env, mdd_cobj);
1841 if (cattr->la_nlink == 0)
1842 cl_flags |= CLF_UNLINK_LAST;
1844 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1846 rc = mdd_changelog_ns_store(env, mdd,
1847 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1848 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1849 lname, NULL, handle);
1853 rc = mdd_trans_stop(env, mdd, rc, handle);
1859 * The permission has been checked when obj created, no need check again.
1861 static int mdd_cd_sanity_check(const struct lu_env *env,
1862 struct mdd_object *obj)
1867 if (!obj || mdd_is_dead_obj(obj))
1873 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1874 struct md_object *cobj,
1875 const struct md_op_spec *spec, struct md_attr *ma)
1877 struct mdd_device *mdd = mdo2mdd(cobj);
1878 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1879 struct mdd_object *son = md2mdd_obj(cobj);
1880 struct thandle *handle;
1881 const struct lu_buf *buf;
1882 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1883 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
1887 rc = mdd_cd_sanity_check(env, son);
1891 if (!md_should_create(spec->sp_cr_flags))
1895 * there are following use cases for this function:
1896 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1897 * striping can be specified or not
1900 rc = mdd_la_get(env, son, attr);
1904 /* calling ->ah_make_hint() is used to transfer information from parent */
1905 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1907 handle = mdd_trans_create(env, mdd);
1909 GOTO(out_free, rc = PTR_ERR(handle));
1912 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1913 * Should this be fixed?
1915 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1916 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1917 spec->sp_cr_flags, spec->no_create);
1919 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1920 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1921 spec->u.sp_ea.eadatalen);
1926 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1927 XATTR_NAME_LOV, 0, handle);
1931 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1936 rc = mdd_trans_start(env, mdd, handle);
1940 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1946 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1950 rc = mdd_trans_stop(env, mdd, rc, handle);
1956 static int mdd_declare_object_initialize(const struct lu_env *env,
1957 struct mdd_object *parent,
1958 struct mdd_object *child,
1959 const struct lu_attr *attr,
1960 struct thandle *handle)
1965 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1966 if (!S_ISDIR(attr->la_mode))
1969 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1970 S_IFDIR, dot, handle);
1974 rc = mdo_declare_ref_add(env, child, handle);
1978 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1979 S_IFDIR, dotdot, handle);
1984 static int mdd_object_initialize(const struct lu_env *env,
1985 const struct lu_fid *pfid,
1986 struct mdd_object *child,
1987 struct lu_attr *attr,
1988 struct thandle *handle)
1993 if (S_ISDIR(attr->la_mode)) {
1994 /* Add "." and ".." for newly created dir */
1995 mdo_ref_add(env, child, handle);
1996 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1997 S_IFDIR, dot, handle);
1999 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2002 mdo_ref_del(env, child, handle);
2009 * This function checks whether it can create a file/dir under the
2010 * directory(@pobj). The directory(@pobj) is not being locked by
2013 * \param[in] env execution environment
2014 * \param[in] pobj the directory to create files
2015 * \param[in] pattr the attributes of the directory
2016 * \param[in] lname the name of the created file/dir
2017 * \param[in] cattr the attributes of the file/dir
2018 * \param[in] spec create specification
2020 * \retval = 0 it is allowed to create file/dir under
2022 * \retval negative error not allowed to create file/dir
2023 * under the directory
2025 static int mdd_create_sanity_check(const struct lu_env *env,
2026 struct md_object *pobj,
2027 const struct lu_attr *pattr,
2028 const struct lu_name *lname,
2029 struct lu_attr *cattr,
2030 struct md_op_spec *spec)
2032 struct mdd_thread_info *info = mdd_env_info(env);
2033 struct lu_fid *fid = &info->mdi_fid;
2034 struct mdd_object *obj = md2mdd_obj(pobj);
2035 struct mdd_device *m = mdo2mdd(pobj);
2036 bool check_perm = true;
2041 if (mdd_is_dead_obj(obj))
2045 * In some cases this lookup is not needed - we know before if name
2046 * exists or not because MDT performs lookup for it.
2047 * name length check is done in lookup.
2049 if (spec->sp_cr_lookup) {
2051 * Check if the name already exist, though it will be checked in
2052 * _index_insert also, for avoiding rolling back if exists
2055 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2056 MAY_WRITE | MAY_EXEC);
2058 RETURN(rc ? : -EEXIST);
2060 /* Permission is already being checked in mdd_lookup */
2064 if (S_ISDIR(cattr->la_mode) &&
2065 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2066 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2067 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2069 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2070 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2072 CERROR("%s: invalid lmv_user_md: magic = %x, "
2073 "stripe_offset = %d, stripe_count = %u: "
2074 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2075 le32_to_cpu(lum->lum_magic),
2076 (int)le32_to_cpu(lum->lum_stripe_offset),
2077 le32_to_cpu(lum->lum_stripe_count), rc);
2082 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2087 if (pattr->la_mode & S_ISGID) {
2088 struct lu_ucred *uc = lu_ucred(env);
2090 cattr->la_gid = pattr->la_gid;
2092 /* Directories are special, and always inherit S_ISGID */
2093 if (S_ISDIR(cattr->la_mode)) {
2094 cattr->la_mode |= S_ISGID;
2095 cattr->la_valid |= LA_MODE;
2096 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2097 == (S_ISGID | S_IXGRP) &&
2098 !lustre_in_group_p(uc,
2099 (cattr->la_valid & LA_GID) ?
2100 cattr->la_gid : pattr->la_gid) &&
2101 !md_capable(uc, CAP_FSETID)) {
2102 cattr->la_mode &= ~S_ISGID;
2103 cattr->la_valid |= LA_MODE;
2107 /* Inherit project ID from parent directory */
2108 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2109 cattr->la_projid = pattr->la_projid;
2110 if (S_ISDIR(cattr->la_mode)) {
2111 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2112 cattr->la_valid |= LA_FLAGS;
2114 cattr->la_valid |= LA_PROJID;
2117 rc = mdd_name_check(env, m, lname);
2121 switch (cattr->la_mode & S_IFMT) {
2123 unsigned int symlen = spec->u.sp_symname.ln_namelen + 1;
2125 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2126 RETURN(-ENAMETOOLONG);
2145 static int mdd_declare_create_object(const struct lu_env *env,
2146 struct mdd_device *mdd,
2147 struct mdd_object *p, struct mdd_object *c,
2148 struct lu_attr *attr,
2149 struct thandle *handle,
2150 const struct md_op_spec *spec,
2151 struct lu_buf *def_acl_buf,
2152 struct lu_buf *acl_buf,
2153 struct lu_buf *hsm_buf,
2154 struct dt_allocation_hint *hint)
2156 const struct lu_buf *buf;
2159 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2160 /* ldiskfs OSD needs this information for credit allocation */
2162 hint->dah_acl_len = def_acl_buf->lb_len;
2164 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2169 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2170 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2171 /* if dir, then can inherit default ACl */
2172 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2173 XATTR_NAME_ACL_DEFAULT,
2179 if (acl_buf && acl_buf->lb_len > 0) {
2180 rc = mdo_declare_attr_set(env, c, attr, handle);
2184 rc = mdo_declare_xattr_set(env, c, acl_buf,
2185 XATTR_NAME_ACL_ACCESS, 0, handle);
2190 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2194 /* replay case, create LOV EA from client data */
2195 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2196 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2197 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2198 spec->u.sp_ea.eadatalen);
2199 rc = mdo_declare_xattr_set(env, c, buf,
2200 S_ISDIR(attr->la_mode) ?
2201 XATTR_NAME_LMV : XATTR_NAME_LOV,
2202 LU_XATTR_CREATE, handle);
2206 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2207 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2215 if (S_ISLNK(attr->la_mode)) {
2216 const char *target_name = spec->u.sp_symname.ln_name;
2217 int sym_len = spec->u.sp_symname.ln_namelen;
2218 const struct lu_buf *buf;
2220 buf = mdd_buf_get_const(env, target_name, sym_len);
2221 rc = dt_declare_record_write(env, mdd_object_child(c),
2227 if (spec->sp_cr_file_secctx_name != NULL) {
2228 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2229 spec->sp_cr_file_secctx_size);
2230 rc = mdo_declare_xattr_set(env, c, buf,
2231 spec->sp_cr_file_secctx_name, 0,
2237 if (spec->sp_cr_file_encctx != NULL) {
2238 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2239 spec->sp_cr_file_encctx_size);
2240 rc = mdo_declare_xattr_set(env, c, buf,
2241 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2250 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2251 struct mdd_object *p, struct mdd_object *c,
2252 const struct lu_name *name,
2253 struct lu_attr *attr,
2254 struct thandle *handle,
2255 const struct md_op_spec *spec,
2256 struct linkea_data *ldata,
2257 struct lu_buf *def_acl_buf,
2258 struct lu_buf *acl_buf,
2259 struct lu_buf *hsm_buf,
2260 struct dt_allocation_hint *hint)
2264 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2265 def_acl_buf, acl_buf, hsm_buf, hint);
2269 if (S_ISDIR(attr->la_mode)) {
2270 rc = mdo_declare_ref_add(env, p, handle);
2275 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2276 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2280 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2281 enum changelog_rec_type type;
2283 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2284 attr->la_mode, name->ln_name,
2289 rc = mdd_declare_links_add(env, c, handle, ldata);
2294 la->la_valid = LA_CTIME | LA_MTIME;
2295 rc = mdo_declare_attr_set(env, p, la, handle);
2299 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2300 S_ISREG(attr->la_mode) ? CL_CREATE :
2301 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2303 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2312 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2313 struct lu_attr *la, struct lu_buf *def_acl_buf,
2314 struct lu_buf *acl_buf)
2320 if (S_ISLNK(la->la_mode)) {
2321 acl_buf->lb_len = 0;
2322 def_acl_buf->lb_len = 0;
2326 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2327 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2328 XATTR_NAME_ACL_DEFAULT);
2329 mdd_read_unlock(env, pobj);
2331 /* ACL buffer size is not enough, need realloc */
2332 if (rc > acl_buf->lb_len)
2335 /* If there are default ACL, fix mode/ACL by default ACL */
2336 def_acl_buf->lb_len = rc;
2337 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2338 acl_buf->lb_len = rc;
2339 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2342 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2343 /* If there are no default ACL, fix mode by mask */
2344 struct lu_ucred *uc = lu_ucred(env);
2346 /* The create triggered by MDT internal events, such as
2347 * LFSCK reset, will not contain valid "uc". */
2348 if (unlikely(uc != NULL))
2349 la->la_mode &= ~uc->uc_umask;
2351 acl_buf->lb_len = 0;
2352 def_acl_buf->lb_len = 0;
2359 * Create a metadata object and initialize it, set acl, xattr.
2361 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2362 struct mdd_object *son, struct lu_attr *attr,
2363 struct md_op_spec *spec, struct lu_buf *acl_buf,
2364 struct lu_buf *def_acl_buf,
2365 struct lu_buf *hsm_buf,
2366 struct dt_allocation_hint *hint,
2367 struct thandle *handle, bool initsecctx)
2369 const struct lu_buf *buf;
2372 mdd_write_lock(env, son, DT_TGT_CHILD);
2373 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2378 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2379 * created in declare phase, they also needs to be added to master
2380 * object as sub-directory entry. So it has to initialize the master
2381 * object, then set dir striped EA.(in mdo_xattr_set) */
2382 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2385 GOTO(err_destroy, rc);
2388 * in case of replay we just set LOVEA provided by the client
2389 * XXX: I think it would be interesting to try "old" way where
2390 * MDT calls this xattr_set(LOV) in a different transaction.
2391 * probably this way we code can be made better.
2394 /* During creation, there are only a few cases we need do xattr_set to
2396 * 1. regular file: see comments above.
2397 * 2. dir: inherit default striping or pool settings from parent.
2398 * 3. create striped directory with provided stripeEA.
2399 * 4. create striped directory because inherit default layout from the
2402 if (spec->no_create ||
2403 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2404 S_ISDIR(attr->la_mode)) {
2405 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2406 spec->u.sp_ea.eadatalen);
2407 rc = mdo_xattr_set(env, son, buf,
2408 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2410 LU_XATTR_CREATE, handle);
2412 GOTO(err_destroy, rc);
2415 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2418 memset(&mh, 0, sizeof(mh));
2419 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2420 mh.mh_arch_id = spec->sp_archive_id;
2421 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2422 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2425 GOTO(err_destroy, rc);
2428 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2429 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2430 S_ISDIR(attr->la_mode)) {
2431 /* set default acl */
2432 rc = mdo_xattr_set(env, son, def_acl_buf,
2433 XATTR_NAME_ACL_DEFAULT, 0,
2436 GOTO(err_destroy, rc);
2438 /* set its own acl */
2439 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2440 rc = mdo_xattr_set(env, son, acl_buf,
2441 XATTR_NAME_ACL_ACCESS,
2444 GOTO(err_destroy, rc);
2448 if (S_ISLNK(attr->la_mode)) {
2449 struct dt_object *dt = mdd_object_child(son);
2450 const char *target_name = spec->u.sp_symname.ln_name;
2451 int sym_len = spec->u.sp_symname.ln_namelen;
2454 buf = mdd_buf_get_const(env, target_name, sym_len);
2455 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2459 GOTO(err_initlized, rc = -EFAULT);
2462 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2463 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2464 spec->sp_cr_file_secctx_size);
2465 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2468 GOTO(err_initlized, rc);
2471 if (spec->sp_cr_file_encctx != NULL) {
2472 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2473 spec->sp_cr_file_encctx_size);
2474 rc = mdo_xattr_set(env, son, buf,
2475 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2478 GOTO(err_initlized, rc);
2482 if (unlikely(rc != 0)) {
2484 if (S_ISDIR(attr->la_mode)) {
2485 /* Drop the reference, no need to delete "."/"..",
2486 * because the object to be destroied directly. */
2487 rc2 = mdo_ref_del(env, son, handle);
2491 rc2 = mdo_ref_del(env, son, handle);
2495 mdo_destroy(env, son, handle);
2498 mdd_write_unlock(env, son);
2502 static int mdd_index_delete(const struct lu_env *env,
2503 struct mdd_object *mdd_pobj,
2504 struct lu_attr *cattr,
2505 const struct lu_name *lname)
2507 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2508 struct thandle *handle;
2512 handle = mdd_trans_create(env, mdd);
2514 RETURN(PTR_ERR(handle));
2516 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2521 if (S_ISDIR(cattr->la_mode)) {
2522 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2527 /* Since this will only be used in the error handler path,
2528 * Let's set the thandle to be local and not mess the transno */
2529 handle->th_local = 1;
2530 rc = mdd_trans_start(env, mdd, handle);
2534 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2535 S_ISDIR(cattr->la_mode), handle);
2539 rc = mdd_trans_stop(env, mdd, rc, handle);
2545 * Create object and insert it into namespace.
2547 * Two operations have to be performed:
2549 * - an allocation of a new object (->do_create()), and
2550 * - an insertion into a parent index (->dio_insert()).
2552 * Due to locking, operation order is not important, when both are
2553 * successful, *but* error handling cases are quite different:
2555 * - if insertion is done first, and following object creation fails,
2556 * insertion has to be rolled back, but this operation might fail
2557 * also leaving us with dangling index entry.
2559 * - if creation is done first, is has to be undone if insertion fails,
2560 * leaving us with leaked space, which is not good but not fatal.
2562 * It seems that creation-first is simplest solution, but it is sub-optimal
2568 * case, because second mkdir is bound to create object, only to
2569 * destroy it immediately.
2571 * To avoid this follow local file systems that do double lookup:
2573 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2574 * 1. create (mdd_create_object_internal())
2575 * 2. insert (__mdd_index_insert(), lookup again)
2577 * \param[in] pobj parent object
2578 * \param[in] lname name of child being created
2579 * \param[in,out] child child object being created
2580 * \param[in] spec additional create parameters
2581 * \param[in] ma attributes for new child object
2583 * \retval 0 on success
2584 * \retval negative errno on failure
2586 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2587 const struct lu_name *lname, struct md_object *child,
2588 struct md_op_spec *spec, struct md_attr *ma)
2590 struct mdd_thread_info *info = mdd_env_info(env);
2591 struct lu_attr *la = &info->mdi_la_for_fix;
2592 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2593 struct mdd_object *son = md2mdd_obj(child);
2594 struct mdd_device *mdd = mdo2mdd(pobj);
2595 struct lu_attr *attr = &ma->ma_attr;
2596 struct thandle *handle;
2597 struct lu_attr *pattr = &info->mdi_pattr;
2598 struct lu_buf acl_buf;
2599 struct lu_buf def_acl_buf;
2600 struct lu_buf hsm_buf;
2601 struct linkea_data *ldata = &info->mdi_link_data;
2602 const char *name = lname->ln_name;
2603 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2604 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2609 rc = mdd_la_get(env, mdd_pobj, pattr);
2613 /* Sanity checks before big job. */
2614 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2618 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2619 GOTO(out_free, rc = -EINPROGRESS);
2621 handle = mdd_trans_create(env, mdd);
2623 GOTO(out_free, rc = PTR_ERR(handle));
2626 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
2627 if (!acl_buf.lb_buf)
2628 GOTO(out_stop, rc = -ENOMEM);
2630 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
2631 if (!def_acl_buf.lb_buf)
2632 GOTO(out_stop, rc = -ENOMEM);
2634 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2635 if (unlikely(rc == -ERANGE &&
2636 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2637 /* use maximum-sized xattr buffer for too-big default ACL */
2638 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2640 goto use_bigger_buffer;
2645 if (S_ISDIR(attr->la_mode)) {
2646 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2649 * migrate may create 1-stripe directory, so lod_ah_init()
2650 * doesn't adjust stripe count from lmu.
2652 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2653 lmu->lum_stripe_count = 0;
2656 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2658 memset(ldata, 0, sizeof(*ldata));
2659 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2660 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2663 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2664 &tfid, lname, 1, 0, ldata);
2666 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2667 mdd_object_fid(mdd_pobj),
2668 lname, 1, 0, ldata);
2671 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2672 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2674 memset(&hsm_buf, 0, sizeof(hsm_buf));
2675 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2676 if (hsm_buf.lb_buf == NULL)
2677 GOTO(out_stop, rc = -ENOMEM);
2680 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2681 handle, spec, ldata, &def_acl_buf, &acl_buf,
2686 rc = mdd_trans_start(env, mdd, handle);
2690 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2691 &def_acl_buf, &hsm_buf, hint, handle, true);
2695 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2696 mdd_write_lock(env, son, DT_TGT_CHILD);
2697 son->mod_flags |= VOLATILE_OBJ;
2698 rc = mdd_orphan_insert(env, son, handle);
2699 GOTO(out_volatile, rc);
2701 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2702 attr->la_mode, name, handle);
2704 GOTO(err_created, rc);
2706 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2709 /* update parent directory mtime/ctime */
2711 la->la_valid = LA_CTIME | LA_MTIME;
2712 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2714 GOTO(err_insert, rc);
2720 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2721 rc2 = mdd_orphan_delete(env, son, handle);
2723 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2724 S_ISDIR(attr->la_mode),
2730 mdd_write_lock(env, son, DT_TGT_CHILD);
2731 if (S_ISDIR(attr->la_mode)) {
2732 /* Drop the reference, no need to delete "."/"..",
2733 * because the object is to be destroyed directly. */
2734 rc2 = mdo_ref_del(env, son, handle);
2736 mdd_write_unlock(env, son);
2741 /* For volatile files drop one link immediately, since there is
2742 * no filename in the namespace, and save any error returned. */
2743 rc2 = mdo_ref_del(env, son, handle);
2745 mdd_write_unlock(env, son);
2746 if (unlikely(rc == 0))
2751 /* Don't destroy the volatile object on success */
2752 if (likely(rc != 0))
2753 mdo_destroy(env, son, handle);
2754 mdd_write_unlock(env, son);
2757 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2758 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2759 rc = mdd_changelog_ns_store(env, mdd,
2760 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2761 S_ISREG(attr->la_mode) ? CL_CREATE :
2762 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2763 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2764 lname, NULL, handle);
2766 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2768 /* If creation fails, it is most likely due to the remote update
2769 * failure, because local transaction will mostly succeed at
2770 * this stage. There is no easy way to rollback all of previous
2771 * updates, so let's remove the object from namespace, and
2772 * LFSCK should handle the orphan object. */
2773 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2774 mdd_index_delete(env, mdd_pobj, attr, lname);
2778 if (is_vmalloc_addr(ldata->ld_buf))
2779 /* if we vmalloced a large buffer drop it */
2780 lu_buf_free(ldata->ld_buf);
2782 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2783 lu_buf_free(&hsm_buf);
2785 /* The child object shouldn't be cached anymore */
2787 set_bit(LU_OBJECT_HEARD_BANSHEE,
2788 &child->mo_lu.lo_header->loh_flags);
2792 /* has not mdd_write{read}_lock on any obj yet. */
2793 static int mdd_rename_sanity_check(const struct lu_env *env,
2794 struct mdd_object *src_pobj,
2795 const struct lu_attr *pattr,
2796 struct mdd_object *tgt_pobj,
2797 const struct lu_attr *tpattr,
2798 struct mdd_object *sobj,
2799 const struct lu_attr *cattr,
2800 struct mdd_object *tobj,
2801 const struct lu_attr *tattr)
2806 /* XXX: when get here, sobj must NOT be NULL,
2807 * the other case has been processed in cld_rename
2808 * before mdd_rename and enable MDS_PERM_BYPASS. */
2812 * If we are using project inheritance, we only allow renames
2813 * into our tree when the project IDs are the same; otherwise
2814 * tree quota mechanism would be circumvented.
2816 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2817 tpattr->la_projid != cattr->la_projid) ||
2818 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2819 (pattr->la_projid != tpattr->la_projid)))
2822 /* we prevent an encrypted file from being renamed
2823 * into an unencrypted dir
2825 if ((pattr->la_valid & LA_FLAGS &&
2826 pattr->la_flags & LUSTRE_ENCRYPT_FL) &&
2827 !(tpattr->la_valid & LA_FLAGS &&
2828 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
2831 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2835 /* XXX: when get here, "tobj == NULL" means tobj must
2836 * NOT exist (neither on remote MDS, such case has been
2837 * processed in cld_rename before mdd_rename and enable
2839 * So check may_create, but not check may_unlink. */
2841 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2842 (src_pobj != tgt_pobj));
2844 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2845 (src_pobj != tgt_pobj), 1);
2847 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2848 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2853 static int mdd_declare_rename(const struct lu_env *env,
2854 struct mdd_device *mdd,
2855 struct mdd_object *mdd_spobj,
2856 struct mdd_object *mdd_tpobj,
2857 struct mdd_object *mdd_sobj,
2858 struct mdd_object *mdd_tobj,
2859 const struct lu_name *sname,
2860 const struct lu_name *tname,
2862 struct linkea_data *ldata,
2863 struct thandle *handle)
2865 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2868 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2869 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2875 /* name from source dir */
2876 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2880 /* .. from source child */
2881 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2882 /* source child can be directory, count by source dir's nlink */
2883 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2886 if (mdd_spobj != mdd_tpobj) {
2887 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2892 rc = mdo_declare_index_insert(env, mdd_sobj,
2893 mdd_object_fid(mdd_tpobj),
2894 S_IFDIR, dotdot, handle);
2899 /* new target child can be directory,
2900 * counted by target dir's nlink */
2901 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2906 la->la_valid = LA_CTIME | LA_MTIME;
2907 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2911 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2915 la->la_valid = LA_CTIME;
2916 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2920 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2925 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2926 mdd_object_type(mdd_sobj),
2927 tname->ln_name, handle);
2931 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2932 /* delete target child in target parent directory */
2933 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2938 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2942 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2943 /* target child can be directory,
2944 * delete "." reference in target child directory */
2945 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2949 /* delete ".." reference in target parent directory */
2950 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2955 la->la_valid = LA_CTIME;
2956 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2960 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2965 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2973 static int mdd_migrate_object(const struct lu_env *env,
2974 struct mdd_object *spobj,
2975 struct mdd_object *tpobj,
2976 struct mdd_object *sobj,
2977 struct mdd_object *tobj,
2978 const struct lu_name *sname,
2979 const struct lu_name *tname,
2980 struct md_op_spec *spec,
2981 struct md_attr *ma);
2983 /* src object can be remote that is why we use only fid and type of object */
2984 static int mdd_rename(const struct lu_env *env,
2985 struct md_object *src_pobj, struct md_object *tgt_pobj,
2986 const struct lu_fid *lf, const struct lu_name *lsname,
2987 struct md_object *tobj, const struct lu_name *ltname,
2990 const char *sname = lsname->ln_name;
2991 const char *tname = ltname->ln_name;
2992 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2993 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2994 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2995 struct mdd_device *mdd = mdo2mdd(src_pobj);
2996 struct mdd_object *mdd_sobj = NULL; /* source object */
2997 struct mdd_object *mdd_tobj = NULL;
2998 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2999 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3000 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3001 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3002 struct thandle *handle;
3003 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3004 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3005 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3008 bool tobj_locked = 0;
3009 unsigned cl_flags = 0;
3013 /* let unlink to complete and commit */
3014 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3017 mdd_tobj = md2mdd_obj(tobj);
3019 mdd_sobj = mdd_object_find(env, mdd, lf);
3020 if (IS_ERR(mdd_sobj))
3021 RETURN(PTR_ERR(mdd_sobj));
3023 rc = mdd_la_get(env, mdd_sobj, cattr);
3025 GOTO(out_pending, rc);
3027 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3028 * hard links, and target doesn't exist.
3030 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3031 cattr->la_nlink == 1 && !tobj) {
3032 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3033 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3036 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3039 GOTO(out_pending, rc);
3041 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3042 if (IS_ERR(mdd_tobj))
3043 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3045 memset(spec, 0, sizeof(*spec));
3046 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3047 mdd_tobj, lsname, ltname, spec, ma);
3048 mdd_object_put(env, mdd_tobj);
3049 GOTO(out_pending, rc);
3052 rc = mdd_la_get(env, mdd_spobj, pattr);
3054 GOTO(out_pending, rc);
3057 rc = mdd_la_get(env, mdd_tobj, tattr);
3059 GOTO(out_pending, rc);
3060 /* search for an existing archive.
3061 * we should check ahead as the object
3062 * can be destroyed in this transaction */
3063 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3064 cl_flags |= CLF_RENAME_LAST_EXISTS;
3067 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3069 GOTO(out_pending, rc);
3071 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3072 mdd_sobj, cattr, mdd_tobj, tattr);
3074 GOTO(out_pending, rc);
3076 rc = mdd_name_check(env, mdd, ltname);
3078 GOTO(out_pending, rc);
3080 handle = mdd_trans_create(env, mdd);
3082 GOTO(out_pending, rc = PTR_ERR(handle));
3084 memset(ldata, 0, sizeof(*ldata));
3085 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3086 lsname, mdd_object_fid(mdd_tpobj), ltname,
3091 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3092 mdd_tobj, lsname, ltname, ma, ldata, handle);
3096 rc = mdd_trans_start(env, mdd, handle);
3100 is_dir = S_ISDIR(cattr->la_mode);
3102 /* Remove source name from source directory */
3103 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3107 /* "mv dir1 dir2" needs "dir1/.." link update */
3108 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3109 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3111 GOTO(fixup_spobj2, rc);
3113 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3116 GOTO(fixup_spobj, rc);
3119 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3120 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3122 /* tname might been renamed to something else */
3123 GOTO(fixup_spobj, rc);
3126 /* Insert new fid with target name into target dir */
3127 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3130 GOTO(fixup_tpobj, rc);
3132 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3133 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3135 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3136 la->la_valid = LA_CTIME;
3137 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3139 GOTO(fixup_tpobj, rc);
3141 /* Update the linkEA for the source object */
3142 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3143 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3144 lsname, mdd_object_fid(mdd_tpobj), ltname,
3145 handle, ldata, 0, 0);
3147 /* Old files might not have EA entry */
3148 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3149 lsname, handle, NULL, 0);
3150 mdd_write_unlock(env, mdd_sobj);
3151 /* We don't fail the transaction if the link ea can't be
3152 updated -- fid2path will use alternate lookup method. */
3155 /* Remove old target object
3156 * For tobj is remote case cmm layer has processed
3157 * and set tobj to NULL then. So when tobj is NOT NULL,
3158 * it must be local one.
3160 if (tobj && mdd_object_exists(mdd_tobj)) {
3161 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3163 if (mdd_is_dead_obj(mdd_tobj)) {
3164 /* shld not be dead, something is wrong */
3165 CERROR("tobj is dead, something is wrong\n");
3169 mdo_ref_del(env, mdd_tobj, handle);
3171 /* Remove dot reference. */
3172 if (S_ISDIR(tattr->la_mode))
3173 mdo_ref_del(env, mdd_tobj, handle);
3176 /* fetch updated nlink */
3177 rc = mdd_la_get(env, mdd_tobj, tattr);
3179 CERROR("%s: Failed to get nlink for tobj "
3181 mdd2obd_dev(mdd)->obd_name,
3182 PFID(tpobj_fid), rc);
3183 GOTO(fixup_tpobj, rc);
3186 la->la_valid = LA_CTIME;
3187 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3189 CERROR("%s: Failed to set ctime for tobj "
3191 mdd2obd_dev(mdd)->obd_name,
3192 PFID(tpobj_fid), rc);
3193 GOTO(fixup_tpobj, rc);
3196 /* XXX: this transfer to ma will be removed with LOD/OSP */
3197 ma->ma_attr = *tattr;
3198 ma->ma_valid |= MA_INODE;
3199 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3202 CERROR("%s: Failed to unlink tobj "
3204 mdd2obd_dev(mdd)->obd_name,
3205 PFID(tpobj_fid), rc);
3206 GOTO(fixup_tpobj, rc);
3209 /* fetch updated nlink */
3210 rc = mdd_la_get(env, mdd_tobj, tattr);
3211 if (rc == -ENOENT) {
3212 /* the object got removed, let's
3213 * return the latest known attributes */
3214 tattr->la_nlink = 0;
3216 } else if (rc != 0) {
3217 CERROR("%s: Failed to get nlink for tobj "
3219 mdd2obd_dev(mdd)->obd_name,
3220 PFID(tpobj_fid), rc);
3221 GOTO(fixup_tpobj, rc);
3223 /* XXX: this transfer to ma will be removed with LOD/OSP */
3224 ma->ma_attr = *tattr;
3225 ma->ma_valid |= MA_INODE;
3227 if (tattr->la_nlink == 0)
3228 cl_flags |= CLF_RENAME_LAST;
3230 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3233 la->la_valid = LA_CTIME | LA_MTIME;
3234 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3236 GOTO(fixup_tpobj, rc);
3238 if (mdd_spobj != mdd_tpobj) {
3239 la->la_valid = LA_CTIME | LA_MTIME;
3240 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3242 GOTO(fixup_tpobj, rc);
3249 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3251 CWARN("tp obj fix error %d\n",rc2);
3253 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3254 !mdd_is_dead_obj(mdd_tobj)) {
3256 mdo_ref_add(env, mdd_tobj, handle);
3258 mdo_ref_add(env, mdd_tobj, handle);
3261 rc2 = __mdd_index_insert(env, mdd_tpobj,
3262 mdd_object_fid(mdd_tobj),
3263 mdd_object_type(mdd_tobj),
3266 CWARN("tp obj fix error: rc = %d\n", rc2);
3271 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3272 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3274 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3275 mdd2obd_dev(mdd)->obd_name, rc2);
3278 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3281 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3282 mdd2obd_dev(mdd)->obd_name, rc2);
3287 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3288 mdd_object_type(mdd_sobj), sname,
3291 CWARN("sp obj fix error: rc = %d\n", rc2);
3296 mdd_write_unlock(env, mdd_tobj);
3299 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3300 mdd_tobj, tpobj_fid, lf, spobj_fid,
3301 ltname, lsname, handle);
3304 rc = mdd_trans_stop(env, mdd, rc, handle);
3307 mdd_object_put(env, mdd_sobj);
3312 * Check whether we should migrate the file/dir
3314 * < 0 permission check failed or other error.
3315 * = 0 the file can be migrated.
3317 static int mdd_migrate_sanity_check(const struct lu_env *env,
3318 struct mdd_device *mdd,
3319 struct mdd_object *spobj,
3320 struct mdd_object *tpobj,
3321 struct mdd_object *sobj,
3322 struct mdd_object *tobj,
3323 const struct lu_attr *spattr,
3324 const struct lu_attr *tpattr,
3325 const struct lu_attr *attr)
3331 if (!mdd_object_remote(sobj)) {
3332 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3333 if (sobj->mod_count > 0) {
3334 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3335 mdd_obj_dev_name(sobj),
3336 PFID(mdd_object_fid(sobj)),
3338 mdd_read_unlock(env, sobj);
3341 mdd_read_unlock(env, sobj);
3344 if (mdd_object_exists(tobj))
3347 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3351 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3356 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3357 struct mdd_object *obj,
3358 const struct lu_buf *buf,
3360 int fl, struct thandle *handle);
3362 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3363 static int mdd_iterate_xattrs(const struct lu_env *env,
3364 struct mdd_object *sobj,
3365 struct mdd_object *tobj,
3367 struct thandle *handle,
3370 struct mdd_thread_info *info = mdd_env_info(env);
3372 struct lu_buf list_xbuf;
3373 struct lu_buf cbxbuf;
3374 struct lu_buf xbuf = { NULL };
3383 /* retrieve xattr list from the old object */
3384 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3385 if (list_xsize == -ENODATA)
3391 lu_buf_check_and_alloc(&info->mdi_big_buf, list_xsize);
3392 if (info->mdi_big_buf.lb_buf == NULL)
3395 list_xbuf.lb_buf = info->mdi_big_buf.lb_buf;
3396 list_xbuf.lb_len = list_xsize;
3397 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3403 xname = list_xbuf.lb_buf;
3405 xlen = strnlen(xname, rem - 1) + 1;
3406 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3407 strcmp(XATTR_NAME_LMV, xname) == 0)
3411 strcmp(XATTR_NAME_LINK, xname) == 0)
3414 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3415 if (xsize == -ENODATA)
3418 GOTO(out, rc = xsize);
3420 lu_buf_check_and_alloc(&xbuf, xsize);
3421 if (xbuf.lb_buf == NULL)
3422 GOTO(out, rc = -ENOMEM);
3424 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3431 cbxbuf.lb_len = xsize;
3433 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3434 if (unlikely(rc == -ENOSPC &&
3435 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3436 rc = linkea_overflow_shrink(
3437 (struct linkea_data *)(cbxbuf.lb_buf));
3438 if (likely(rc > 0)) {
3456 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3457 struct mdd_object *sobj,
3458 struct mdd_object *tobj,
3459 const struct lu_name *sname,
3460 const struct lu_fid *sfid,
3461 const struct lu_name *lname,
3462 const struct lu_fid *fid,
3464 struct thandle *handle);
3466 static int mdd_declare_update_link(const struct lu_env *env,
3467 struct mdd_object *sobj,
3468 struct mdd_object *tobj,
3469 const struct lu_name *tname,
3470 const struct lu_fid *tpfid,
3471 const struct lu_name *lname,
3472 const struct lu_fid *fid,
3474 struct thandle *handle)
3476 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3477 struct mdd_object *pobj;
3481 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3482 !strcmp(tname->ln_name, lname->ln_name))
3485 pobj = mdd_object_find(env, mdd, fid);
3487 return PTR_ERR(pobj);
3490 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3492 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3493 mdd_object_type(sobj),
3494 lname->ln_name, handle);
3495 mdd_object_put(env, pobj);
3499 rc = mdo_declare_ref_add(env, tobj, handle);
3503 rc = mdo_declare_ref_del(env, sobj, handle);
3507 static int mdd_update_link(const struct lu_env *env,
3508 struct mdd_object *sobj,
3509 struct mdd_object *tobj,
3510 const struct lu_name *tname,
3511 const struct lu_fid *tpfid,
3512 const struct lu_name *lname,
3513 const struct lu_fid *fid,
3515 struct thandle *handle)
3517 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3518 struct mdd_object *pobj;
3524 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3525 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3528 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3529 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3531 pobj = mdd_object_find(env, mdd, fid);
3533 CWARN("%s: cannot find obj "DFID": %ld\n",
3534 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3535 RETURN(PTR_ERR(pobj));
3538 if (!mdd_object_exists(pobj)) {
3539 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3540 mdd_object_put(env, pobj);
3544 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3545 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3547 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3548 mdd_object_type(sobj),
3549 lname->ln_name, handle);
3550 mdd_write_unlock(env, pobj);
3551 mdd_object_put(env, pobj);
3555 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3556 rc = mdo_ref_add(env, tobj, handle);
3557 mdd_write_unlock(env, tobj);
3561 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3562 rc = mdo_ref_del(env, sobj, handle);
3563 mdd_write_unlock(env, sobj);
3568 static inline int mdd_fld_lookup(const struct lu_env *env,
3569 struct mdd_device *mdd,
3570 const struct lu_fid *fid,
3573 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3574 struct seq_server_site *ss;
3577 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3579 range->lsr_flags = LU_SEQ_RANGE_MDT;
3580 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3584 *mdt_index = range->lsr_index;
3589 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3590 struct mdd_object *sobj,
3591 struct mdd_object *tobj,
3592 const struct lu_name *tname,
3593 const struct lu_fid *tpfid,
3594 const struct lu_name *lname,
3595 const struct lu_fid *fid,
3597 struct thandle *handle)
3599 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3600 __u32 source_mdt_index = *(__u32 *)opaque;
3601 __u32 link_mdt_index;
3607 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3608 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3611 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3615 RETURN(link_mdt_index == source_mdt_index);
3618 static int mdd_iterate_linkea(const struct lu_env *env,
3619 struct mdd_object *sobj,
3620 struct mdd_object *tobj,
3621 const struct lu_name *tname,
3622 const struct lu_fid *tpfid,
3623 struct linkea_data *ldata,
3625 struct thandle *handle,
3628 struct mdd_thread_info *info = mdd_env_info(env);
3629 char *filename = info->mdi_name;
3630 struct lu_name lname;
3637 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3638 linkea_next_entry(ldata)) {
3639 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3642 /* Note: lname might miss \0 at the end */
3643 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3644 lname.ln_namelen, lname.ln_name);
3645 lname.ln_name = filename;
3647 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3649 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3657 * Prepare linkea, and check whether file needs migrate: if source still has
3658 * link on source MDT, no need to migrate, just update namespace on source and
3661 * \retval 0 do migrate
3662 * \retval 1 don't migrate
3663 * \retval -errno on failure
3665 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3666 struct mdd_device *mdd,
3667 struct mdd_object *spobj,
3668 struct mdd_object *tpobj,
3669 struct mdd_object *sobj,
3670 const struct lu_name *sname,
3671 const struct lu_name *tname,
3672 const struct lu_attr *attr,
3673 struct linkea_data *ldata)
3675 __u32 source_mdt_index;
3680 memset(ldata, 0, sizeof(*ldata));
3681 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3682 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3687 * Then it will check if the file should be migrated. If the file has
3688 * mulitple links, we only need migrate the file if all of its entries
3689 * has been migrated to the remote MDT.
3691 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3694 /* If there are still links locally, don't migrate this file */
3695 LASSERT(ldata->ld_leh != NULL);
3698 * If linkEA is overflow, it means there are some unknown name entries
3699 * under unknown parents, which will prevent the migration.
3701 if (unlikely(ldata->ld_leh->leh_overflow_time))
3704 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3708 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3709 ldata, &source_mdt_index, NULL,
3710 mdd_is_link_on_source_mdt);
3714 static int mdd_declare_migrate_update(const struct lu_env *env,
3715 struct mdd_object *spobj,
3716 struct mdd_object *tpobj,
3717 struct mdd_object *obj,
3718 const struct lu_name *sname,
3719 const struct lu_name *tname,
3720 struct lu_attr *attr,
3721 struct lu_attr *spattr,
3722 struct lu_attr *tpattr,
3723 struct linkea_data *ldata,
3725 struct thandle *handle)
3727 struct mdd_thread_info *info = mdd_env_info(env);
3728 struct lu_attr *la = &info->mdi_la_for_fix;
3731 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3735 if (S_ISDIR(attr->la_mode)) {
3736 rc = mdo_declare_ref_del(env, spobj, handle);
3741 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3742 attr->la_mode & S_IFMT,
3743 tname->ln_name, handle);
3747 rc = mdd_declare_links_add(env, obj, handle, ldata);
3751 if (S_ISDIR(attr->la_mode)) {
3752 rc = mdo_declare_ref_add(env, tpobj, handle);
3757 la->la_valid = LA_CTIME | LA_MTIME;
3758 rc = mdo_declare_attr_set(env, spobj, la, handle);
3762 if (tpobj != spobj) {
3763 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3771 static int mdd_declare_migrate_create(const struct lu_env *env,
3772 struct mdd_object *spobj,
3773 struct mdd_object *tpobj,
3774 struct mdd_object *sobj,
3775 struct mdd_object *tobj,
3776 const struct lu_name *sname,
3777 const struct lu_name *tname,
3778 struct lu_attr *spattr,
3779 struct lu_attr *tpattr,
3780 struct lu_attr *attr,
3781 struct lu_buf *sbuf,
3782 struct linkea_data *ldata,
3784 struct md_op_spec *spec,
3785 struct dt_allocation_hint *hint,
3786 struct thandle *handle)
3788 struct mdd_thread_info *info = mdd_env_info(env);
3789 struct md_layout_change *mlc = &info->mdi_mlc;
3790 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3795 if (S_ISDIR(attr->la_mode)) {
3796 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3798 mlc->mlc_opc = MD_LAYOUT_DETACH;
3799 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3803 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3804 } else if (S_ISLNK(attr->la_mode)) {
3805 spec->u.sp_symname.ln_name = sbuf->lb_buf;
3806 /* don't count NUL */
3807 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
3808 } else if (S_ISREG(attr->la_mode)) {
3809 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3810 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3813 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3815 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3816 tname, attr, handle, spec, ldata, NULL, NULL,
3822 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3823 * createb yet, copy from sobj.
3825 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3826 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3827 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3829 if (S_ISDIR(attr->la_mode)) {
3831 /* if sobj is not striped, fake a 1-stripe LMV */
3832 LASSERT(sizeof(info->mdi_key) >
3833 lmv_mds_md_size(1, LMV_MAGIC_V1));
3834 lmv = (typeof(lmv))info->mdi_key;
3835 memset(lmv, 0, sizeof(*lmv));
3836 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3837 lmv->lmv_stripe_count = cpu_to_le32(1);
3838 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3839 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3840 mdd_object_fid(sobj));
3841 mlc->mlc_buf.lb_buf = lmv;
3842 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3844 mlc->mlc_buf = *sbuf;
3846 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3847 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3852 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3853 mdo_declare_xattr_set);
3857 if (S_ISREG(attr->la_mode)) {
3858 struct lu_buf fid_buf;
3860 handle->th_complex = 1;
3862 /* target may be remote, update PFID via sobj. */
3863 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3864 fid_buf.lb_len = sizeof(struct lu_fid);
3865 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3870 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3875 if (!S_ISDIR(attr->la_mode)) {
3876 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3877 mdd_object_fid(tpobj), ldata, NULL,
3878 handle, mdd_declare_update_link);
3883 if (!S_ISDIR(attr->la_mode) || lmv) {
3884 rc = mdo_declare_ref_del(env, sobj, handle);
3888 if (S_ISDIR(attr->la_mode)) {
3889 rc = mdo_declare_ref_del(env, sobj, handle);
3894 rc = mdo_declare_destroy(env, sobj, handle);
3899 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3900 attr, spattr, tpattr, ldata, ma,
3906 * migrate dirent from \a spobj to \a tpobj.
3908 static int mdd_migrate_update(const struct lu_env *env,
3909 struct mdd_object *spobj,
3910 struct mdd_object *tpobj,
3911 struct mdd_object *obj,
3912 const struct lu_name *sname,
3913 const struct lu_name *tname,
3914 struct lu_attr *attr,
3915 struct lu_attr *spattr,
3916 struct lu_attr *tpattr,
3917 struct linkea_data *ldata,
3919 struct thandle *handle)
3921 struct mdd_thread_info *info = mdd_env_info(env);
3922 struct lu_attr *la = &info->mdi_la_for_fix;
3927 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3928 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3929 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3931 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3932 S_ISDIR(attr->la_mode), handle);
3936 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3937 attr->la_mode & S_IFMT,
3938 tname->ln_name, handle);
3942 rc = mdd_links_write(env, obj, ldata, handle);
3946 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3947 la->la_valid = LA_CTIME | LA_MTIME;
3948 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3949 rc = mdd_update_time(env, spobj, spattr, la, handle);
3950 mdd_write_unlock(env, spobj);
3954 if (tpobj != spobj) {
3955 la->la_valid = LA_CTIME | LA_MTIME;
3956 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3957 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3958 mdd_write_unlock(env, tpobj);
3967 * Migrate file/dir to target MDT.
3969 * Create target according to \a spec, and then migrate xattrs, if it's
3970 * directory, migrate source stripes to target.
3972 * \param[in] env execution environment
3973 * \param[in] spobj source parent object
3974 * \param[in] tpobj target parent object
3975 * \param[in] sobj source object
3976 * \param[in] tobj target object
3977 * \param[in] lname file name
3978 * \param[in] spattr source parent attributes
3979 * \param[in] tpattr target parent attributes
3980 * \param[in] attr source attributes
3981 * \param[in] sbuf source LMV buf
3982 * \param[in] spec migrate create spec
3983 * \param[in] hint target creation hint
3984 * \param[in] handle tranasction handle
3986 * \retval 0 on success
3987 * \retval -errno on failure
3989 static int mdd_migrate_create(const struct lu_env *env,
3990 struct mdd_object *spobj,
3991 struct mdd_object *tpobj,
3992 struct mdd_object *sobj,
3993 struct mdd_object *tobj,
3994 const struct lu_name *sname,
3995 const struct lu_name *tname,
3996 struct lu_attr *spattr,
3997 struct lu_attr *tpattr,
3998 struct lu_attr *attr,
3999 const struct lu_buf *sbuf,
4000 struct linkea_data *ldata,
4002 struct md_op_spec *spec,
4003 struct dt_allocation_hint *hint,
4004 struct thandle *handle)
4011 * migrate sobj stripes to tobj if it's directory:
4012 * 1. detach stripes from sobj.
4013 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4014 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4016 if (S_ISDIR(attr->la_mode)) {
4017 struct mdd_thread_info *info = mdd_env_info(env);
4018 struct md_layout_change *mlc = &info->mdi_mlc;
4020 mlc->mlc_opc = MD_LAYOUT_DETACH;
4022 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4023 rc = mdo_layout_change(env, sobj, mlc, handle);
4024 mdd_write_unlock(env, sobj);
4029 /* don't set nlink from sobj */
4030 attr->la_valid &= ~LA_NLINK;
4032 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4033 hint, handle, false);
4037 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4038 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4039 mdd_write_unlock(env, tobj);
4043 /* for regular file, update OST objects XATTR_NAME_FID */
4044 if (S_ISREG(attr->la_mode)) {
4045 struct lu_buf fid_buf;
4047 /* target may be remote, update PFID via sobj. */
4048 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4049 fid_buf.lb_len = sizeof(struct lu_fid);
4050 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4055 /* delete LOV to avoid deleting OST objs when destroying sobj */
4056 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4057 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4058 mdd_write_unlock(env, sobj);
4059 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4060 if (rc && rc != -ENODATA)
4065 /* update links FID */
4066 if (!S_ISDIR(attr->la_mode)) {
4067 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4068 mdd_object_fid(tpobj), ldata,
4069 NULL, handle, mdd_update_link);
4074 /* don't destroy sobj if it's plain directory */
4075 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4076 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4077 rc = mdo_ref_del(env, sobj, handle);
4079 if (S_ISDIR(attr->la_mode))
4080 rc = mdo_ref_del(env, sobj, handle);
4082 rc = mdo_destroy(env, sobj, handle);
4084 mdd_write_unlock(env, sobj);
4089 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4090 spattr, tpattr, ldata, ma, handle);
4095 /* NB: if user issued different migrate command, we can't adjust it silently
4096 * here, because this command will decide target MDT in subdir migration in
4099 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4100 const struct lmv_mds_md_v1 *lmv,
4101 const struct lmv_user_md_v1 *lum,
4102 const struct lu_name *lname)
4104 __u32 lum_stripe_count = lum->lum_stripe_count;
4105 __u32 lum_hash_type = lum->lum_hash_type &
4106 cpu_to_le32(LMV_HASH_TYPE_MASK);
4107 __u32 lmv_hash_type = lmv->lmv_hash_type &
4108 cpu_to_le32(LMV_HASH_TYPE_MASK);
4110 if (!lmv_is_sane(lmv))
4113 /* if stripe_count unspecified, set to 1 */
4114 if (!lum_stripe_count)
4115 lum_stripe_count = cpu_to_le32(1);
4117 /* TODO: check specific MDTs */
4118 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4119 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4120 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4121 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4122 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4123 le32_to_cpu(lmv->lmv_master_mdt_index),
4124 le32_to_cpu(lmv->lmv_migrate_offset),
4125 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4134 * Internal function to migrate directory or file between MDTs.
4136 * migrate source to target in following steps:
4137 * 1. create target, append source stripes after target's if it's directory,
4138 * migrate xattrs and update fid of source links.
4139 * 2. update namespace: migrate dirent from source parent to target parent,
4140 * update file linkea, and destroy source if it's not needed any more.
4142 * \param[in] env execution environment
4143 * \param[in] spobj source parent object
4144 * \param[in] tpobj target parent object
4145 * \param[in] sobj source object
4146 * \param[in] tobj target object
4147 * \param[in] sname source file name
4148 * \param[in] tname target file name
4149 * \param[in] spec target creation spec
4150 * \param[in] ma used to update \a pobj mtime and ctime
4152 * \retval 0 on success
4153 * \retval -errno on failure
4155 static int mdd_migrate_object(const struct lu_env *env,
4156 struct mdd_object *spobj,
4157 struct mdd_object *tpobj,
4158 struct mdd_object *sobj,
4159 struct mdd_object *tobj,
4160 const struct lu_name *sname,
4161 const struct lu_name *tname,
4162 struct md_op_spec *spec,
4165 struct mdd_thread_info *info = mdd_env_info(env);
4166 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4167 struct lu_attr *spattr = &info->mdi_pattr;
4168 struct lu_attr *tpattr = &info->mdi_tpattr;
4169 struct lu_attr *attr = &info->mdi_cattr;
4170 struct linkea_data *ldata = &info->mdi_link_data;
4171 struct dt_allocation_hint *hint = &info->mdi_hint;
4172 struct lu_buf sbuf = { NULL };
4173 struct lmv_mds_md_v1 *lmv;
4174 struct thandle *handle;
4179 rc = mdd_la_get(env, sobj, attr);
4183 rc = mdd_la_get(env, spobj, spattr);
4187 rc = mdd_la_get(env, tpobj, tpattr);
4191 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4192 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4196 /* if user use default value '0' for stripe_count, we need to
4197 * adjust it to '1' to create a 1-stripe directory.
4199 if (lum->lum_stripe_count == 0)
4200 lum->lum_stripe_count = cpu_to_le32(1);
4202 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4203 if (rc && rc != -ENODATA)
4208 if (!lmv_is_sane(lmv))
4209 GOTO(out, rc = -EBADF);
4210 if (lmv_is_migrating(lmv)) {
4211 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4216 } else if (!S_ISDIR(attr->la_mode)) {
4218 GOTO(out, rc = -EALREADY);
4220 /* update namespace only if @sobj is on MDT where @tpobj is. */
4221 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4222 spec->sp_migrate_nsonly = true;
4224 if (S_ISLNK(attr->la_mode)) {
4225 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4227 GOTO(out, rc = -ENOMEM);
4229 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4232 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4233 mdd2obd_dev(mdd)->obd_name,
4234 PFID(mdd_object_fid(sobj)), rc);
4240 /* linkea needs update upon FID or parent stripe change */
4241 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4242 tname, attr, ldata);
4244 /* update namespace only if @sobj has link on its MDT. */
4245 spec->sp_migrate_nsonly = true;
4249 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4250 spattr, tpattr, attr);
4254 handle = mdd_trans_create(env, mdd);
4256 GOTO(out, rc = PTR_ERR(handle));
4258 if (spec->sp_migrate_nsonly)
4259 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4260 tname, attr, spattr, tpattr,
4263 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4264 sname, tname, spattr, tpattr,
4265 attr, &sbuf, ldata, ma, spec,
4270 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4275 rc = mdd_trans_start(env, mdd, handle);
4279 if (spec->sp_migrate_nsonly)
4280 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4281 attr, spattr, tpattr, ldata, ma,
4284 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4285 tname, spattr, tpattr, attr, &sbuf,
4286 ldata, ma, spec, hint, handle);
4290 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4291 spec->sp_migrate_nsonly ? sobj : tobj,
4292 mdd_object_fid(spobj), mdd_object_fid(sobj),
4293 mdd_object_fid(tpobj), tname, sname,
4300 rc = mdd_trans_stop(env, mdd, rc, handle);
4308 * Migrate directory or file between MDTs.
4310 * \param[in] env execution environment
4311 * \param[in] md_pobj parent master object
4312 * \param[in] md_sobj source object
4313 * \param[in] lname file name
4314 * \param[in] md_tobj target object
4315 * \param[in] spec target creation spec
4316 * \param[in] ma used to update \a pobj mtime and ctime
4318 * \retval 0 on success
4319 * \retval -errno on failure
4321 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4322 struct md_object *md_sobj, const struct lu_name *lname,
4323 struct md_object *md_tobj, struct md_op_spec *spec,
4326 struct mdd_thread_info *info = mdd_env_info(env);
4327 struct mdd_device *mdd = mdo2mdd(md_pobj);
4328 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4329 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4330 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4331 struct mdd_object *spobj = NULL;
4332 struct mdd_object *tpobj = NULL;
4333 struct lu_buf pbuf = { NULL };
4334 struct lu_fid *fid = &info->mdi_fid2;
4335 struct lmv_mds_md_v1 *lmv;
4340 /* locate source and target stripe on pobj, which are the real parent */
4341 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4342 if (rc < 0 && rc != -ENODATA)
4349 if (!lmv_is_sane(lmv))
4350 GOTO(out, rc = -EBADF);
4352 /* locate target parent stripe */
4353 /* fail check here to make sure top dir migration succeed. */
4354 if (lmv_is_migrating(lmv) &&
4355 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4356 GOTO(out, rc = -EIO);
4358 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4361 GOTO(out, rc = index);
4363 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4364 tpobj = mdd_object_find(env, mdd, fid);
4366 GOTO(out, rc = PTR_ERR(tpobj));
4368 /* locate source parent stripe */
4369 if (lmv_is_layout_changing(lmv)) {
4370 index = lmv_name_to_stripe_index_old(lmv,
4374 GOTO(out, rc = index);
4376 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4377 spobj = mdd_object_find(env, mdd, fid);
4379 GOTO(out, rc = PTR_ERR(spobj));
4381 /* parent stripe unchanged */
4382 if (spobj == tpobj) {
4383 if (!lmv_is_restriping(lmv))
4384 GOTO(out, rc = -EINVAL);
4385 GOTO(out, rc = -EALREADY);
4389 mdd_object_get(spobj);
4394 mdd_object_get(tpobj);
4395 mdd_object_get(spobj);
4398 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4403 if (!IS_ERR_OR_NULL(spobj))
4404 mdd_object_put(env, spobj);
4405 if (!IS_ERR_OR_NULL(tpobj))
4406 mdd_object_put(env, tpobj);
4412 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4413 struct mdd_object *pobj,
4414 struct mdd_object *obj,
4415 struct mdd_object *stripe,
4416 struct lu_attr *attr,
4417 struct md_layout_change *mlc,
4418 struct lu_name *lname,
4419 struct thandle *handle)
4423 mlc->mlc_opc = MD_LAYOUT_DETACH;
4424 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4428 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4429 S_IFDIR, dotdot, handle);
4433 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4434 mdo_declare_xattr_set);
4438 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4442 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4446 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4450 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4451 attr->la_mode, lname->ln_name, handle);
4455 rc = mdo_declare_ref_del(env, obj, handle);
4459 rc = mdo_declare_ref_del(env, obj, handle);
4463 rc = mdo_declare_destroy(env, obj, handle);
4470 /* transform one-stripe directory to a plain directory */
4471 static int mdd_1sd_collapse(const struct lu_env *env,
4472 struct mdd_object *pobj,
4473 struct mdd_object *obj,
4474 struct mdd_object *stripe,
4475 struct lu_attr *attr,
4476 struct md_layout_change *mlc,
4477 struct lu_name *lname,
4478 struct thandle *handle)
4484 /* replace 1-stripe directory with its stripe */
4485 mlc->mlc_opc = MD_LAYOUT_DETACH;
4487 mdd_write_lock(env, obj, DT_SRC_PARENT);
4488 rc = mdo_layout_change(env, obj, mlc, handle);
4489 mdd_write_unlock(env, obj);
4493 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4494 mdd_write_lock(env, obj, DT_SRC_CHILD);
4496 /* insert dotdot to stripe which points to parent */
4497 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4498 S_IFDIR, dotdot, handle);
4502 /* copy xattrs including linkea */
4503 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4508 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4512 /* don't set nlink from parent */
4513 attr->la_valid &= ~LA_NLINK;
4515 rc = mdo_attr_set(env, stripe, attr, handle);
4519 /* delete dir name from parent */
4520 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4524 /* insert stripe to parent with dir name */
4525 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4526 attr->la_mode, lname->ln_name, handle);
4530 /* destroy dir obj */
4531 rc = mdo_ref_del(env, obj, handle);
4535 rc = mdo_ref_del(env, obj, handle);
4539 rc = mdo_destroy(env, obj, handle);
4545 mdd_write_unlock(env, obj);
4546 mdd_write_unlock(env, pobj);
4552 * shrink directory stripes after migration/merge
4554 int mdd_dir_layout_shrink(const struct lu_env *env,
4555 struct md_object *md_obj,
4556 struct md_layout_change *mlc)
4558 struct mdd_device *mdd = mdo2mdd(md_obj);
4559 struct mdd_thread_info *info = mdd_env_info(env);
4560 struct mdd_object *obj = md2mdd_obj(md_obj);
4561 struct mdd_object *pobj = NULL;
4562 struct mdd_object *stripe = NULL;
4563 struct lu_attr *attr = &info->mdi_pattr;
4564 struct lu_fid *fid = &info->mdi_fid2;
4565 struct lu_name lname = { NULL };
4566 struct lu_buf lmv_buf = { NULL };
4567 struct lmv_mds_md_v1 *lmv;
4568 struct lmv_user_md *lmu;
4569 struct thandle *handle;
4574 rc = mdd_la_get(env, obj, attr);
4578 if (!S_ISDIR(attr->la_mode))
4581 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4585 lmv = lmv_buf.lb_buf;
4586 if (!lmv_is_sane(lmv))
4589 lmu = mlc->mlc_buf.lb_buf;
4591 /* adjust the default value '0' to '1' */
4592 if (lmu->lum_stripe_count == 0)
4593 lmu->lum_stripe_count = cpu_to_le32(1);
4595 /* these were checked in MDT */
4596 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4597 le32_to_cpu(lmv->lmv_stripe_count));
4598 LASSERT(!lmv_is_splitting(lmv));
4599 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4601 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4602 * to a plain dir, which will cause FID change and namespace update.
4604 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4605 struct linkea_data *ldata = &info->mdi_link_data;
4606 char *filename = info->mdi_name;
4608 rc = mdd_links_read(env, obj, ldata);
4612 if (ldata->ld_leh->leh_reccount > 1)
4613 GOTO(out, rc = -EINVAL);
4615 linkea_first_entry(ldata);
4617 GOTO(out, rc = -ENODATA);
4619 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4622 /* Note: lname might miss \0 at the end */
4623 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4624 lname.ln_namelen, lname.ln_name);
4625 lname.ln_name = filename;
4627 pobj = mdd_object_find(env, mdd, fid);
4634 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4636 stripe = mdd_object_find(env, mdd, fid);
4637 if (IS_ERR(stripe)) {
4638 mdd_object_put(env, pobj);
4640 GOTO(out, rc = PTR_ERR(stripe));
4644 handle = mdd_trans_create(env, mdd);
4646 GOTO(out, rc = PTR_ERR(handle));
4648 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4649 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4651 GOTO(stop_trans, rc);
4653 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4654 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4657 GOTO(stop_trans, rc);
4660 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4663 GOTO(stop_trans, rc);
4665 rc = mdd_trans_start(env, mdd, handle);
4667 GOTO(stop_trans, rc);
4669 mdd_write_lock(env, obj, DT_SRC_PARENT);
4670 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4671 rc = mdo_layout_change(env, obj, mlc, handle);
4672 mdd_write_unlock(env, obj);
4674 GOTO(stop_trans, rc);
4676 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4677 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4680 GOTO(stop_trans, rc);
4683 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4684 XATTR_NAME_LMV, handle);
4685 GOTO(stop_trans, rc);
4688 rc = mdd_trans_stop(env, mdd, rc, handle);
4691 mdd_object_put(env, stripe);
4692 mdd_object_put(env, pobj);
4694 lu_buf_free(&lmv_buf);
4698 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4699 struct mdd_device *mdd,
4700 struct mdd_object *pobj,
4701 struct mdd_object *obj,
4702 struct mdd_object *tobj,
4703 struct md_layout_change *mlc,
4704 struct dt_allocation_hint *hint,
4705 struct thandle *handle)
4707 struct mdd_thread_info *info = mdd_env_info(env);
4708 const struct lu_name *lname = mlc->mlc_name;
4709 struct lu_attr *la = &info->mdi_la_for_fix;
4710 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4711 struct linkea_data *ldata = &info->mdi_link_data;
4712 struct lmv_mds_md_v1 *lmv;
4716 mlc->mlc_opc = MD_LAYOUT_DETACH;
4717 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4721 memset(ldata, 0, sizeof(*ldata));
4722 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4723 lname, 1, 0, ldata);
4727 count = lum->lum_stripe_count;
4728 lum->lum_stripe_count = 0;
4729 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4731 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4732 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4733 ldata, NULL, NULL, NULL, hint);
4737 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4740 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4742 lmv = (typeof(lmv))info->mdi_key;
4743 memset(lmv, 0, sizeof(*lmv));
4744 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4745 lmv->lmv_stripe_count = cpu_to_le32(1);
4746 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4747 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4749 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4750 mlc->mlc_buf.lb_buf = lmv;
4751 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4752 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4756 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4757 mdo_declare_xattr_set);
4761 lum->lum_stripe_count = count;
4762 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4763 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4767 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4771 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4772 S_IFDIR, lname->ln_name, handle);
4776 la->la_valid = LA_CTIME | LA_MTIME;
4777 rc = mdo_declare_attr_set(env, obj, la, handle);
4781 rc = mdo_declare_attr_set(env, pobj, la, handle);
4785 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4791 * plain directory split:
4792 * 1. create \a tobj as plain directory.
4793 * 2. append \a obj as first stripe of \a tobj.
4794 * 3. migrate xattrs from \a obj to \a tobj.
4795 * 4. split \a tobj to specific stripe count.
4797 static int mdd_dir_split_plain(const struct lu_env *env,
4798 struct mdd_device *mdd,
4799 struct mdd_object *pobj,
4800 struct mdd_object *obj,
4801 struct mdd_object *tobj,
4802 struct md_layout_change *mlc,
4803 struct dt_allocation_hint *hint,
4804 struct thandle *handle)
4806 struct mdd_thread_info *info = mdd_env_info(env);
4807 struct lu_attr *pattr = &info->mdi_pattr;
4808 struct lu_attr *la = &info->mdi_la_for_fix;
4809 const struct lu_name *lname = mlc->mlc_name;
4810 struct linkea_data *ldata = &info->mdi_link_data;
4815 /* copy linkea out and set on target later */
4816 rc = mdd_links_read(env, obj, ldata);
4820 mlc->mlc_opc = MD_LAYOUT_DETACH;
4821 rc = mdo_layout_change(env, obj, mlc, handle);
4825 /* don't set nlink from obj */
4826 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4828 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4829 NULL, NULL, NULL, hint, handle, false);
4833 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4837 rc = mdd_links_write(env, tobj, ldata, handle);
4841 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4845 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4846 lname->ln_name, handle);
4850 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4851 la->la_valid = LA_CTIME | LA_MTIME;
4853 mdd_write_lock(env, obj, DT_SRC_CHILD);
4854 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4855 mdd_write_unlock(env, obj);
4859 rc = mdd_la_get(env, pobj, pattr);
4863 la->la_valid = LA_CTIME | LA_MTIME;
4865 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4866 rc = mdd_update_time(env, pobj, pattr, la, handle);
4867 mdd_write_unlock(env, pobj);
4871 /* FID changes, record it as CL_MIGRATE */
4872 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4873 mdd_object_fid(pobj), mdd_object_fid(obj),
4874 mdd_object_fid(pobj), lname, lname, handle);
4878 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4879 struct md_layout_change *mlc)
4881 struct mdd_thread_info *info = mdd_env_info(env);
4882 struct mdd_device *mdd = mdo2mdd(o);
4883 struct mdd_object *obj = md2mdd_obj(o);
4884 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4885 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4886 struct dt_allocation_hint *hint = &info->mdi_hint;
4887 bool is_plain = false;
4888 struct thandle *handle;
4893 LASSERT(S_ISDIR(mdd_object_type(obj)));
4895 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4901 handle = mdd_trans_create(env, mdd);
4903 RETURN(PTR_ERR(handle));
4906 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4909 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4910 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4912 GOTO(stop_trans, rc);
4914 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4918 GOTO(stop_trans, rc);
4920 rc = mdd_trans_start(env, mdd, handle);
4922 GOTO(stop_trans, rc);
4925 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4928 mdd_write_lock(env, obj, DT_TGT_CHILD);
4929 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4930 LU_XATTR_CREATE, handle);
4931 mdd_write_unlock(env, obj);
4933 GOTO(stop_trans, rc);
4935 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4936 XATTR_NAME_LMV, handle);
4939 GOTO(stop_trans, rc);
4944 rc = mdd_trans_stop(env, mdd, rc, handle);
4949 const struct md_dir_operations mdd_dir_ops = {
4950 .mdo_is_subdir = mdd_is_subdir,
4951 .mdo_lookup = mdd_lookup,
4952 .mdo_create = mdd_create,
4953 .mdo_rename = mdd_rename,
4954 .mdo_link = mdd_link,
4955 .mdo_unlink = mdd_unlink,
4956 .mdo_create_data = mdd_create_data,
4957 .mdo_migrate = mdd_migrate,