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 struct mdd_xattr_entry {
3357 struct list_head mxe_linkage;
3359 struct lu_buf mxe_buf;
3363 struct lu_buf mx_namebuf;
3364 struct list_head mx_list;
3367 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3369 INIT_LIST_HEAD(&xattrs->mx_list);
3370 xattrs->mx_namebuf.lb_buf = NULL;
3371 xattrs->mx_namebuf.lb_len = 0;
3374 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3376 struct mdd_xattr_entry *entry;
3377 struct mdd_xattr_entry *tmp;
3379 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3380 lu_buf_free(&entry->mxe_buf);
3381 list_del(&entry->mxe_linkage);
3382 OBD_FREE_PTR(entry);
3385 lu_buf_free(&xattrs->mx_namebuf);
3388 /* read xattrs into buf, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is
3391 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3392 struct mdd_xattrs *xattrs,
3393 struct mdd_object *sobj,
3396 struct mdd_xattr_entry *entry;
3406 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3407 if (list_xsize == -ENODATA)
3413 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3414 if (xattrs->mx_namebuf.lb_buf == NULL)
3417 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3423 xname = xattrs->mx_namebuf.lb_buf;
3424 for (; rem > 0; xname += xlen, rem -= xlen) {
3425 xlen = strnlen(xname, rem - 1) + 1;
3426 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3427 strcmp(XATTR_NAME_LMV, xname) == 0)
3431 strcmp(XATTR_NAME_LINK, xname) == 0)
3434 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3435 if (xsize == -ENODATA)
3438 GOTO(fini, rc = xsize);
3440 OBD_ALLOC_PTR(entry);
3442 GOTO(fini, rc = -ENOMEM);
3444 lu_buf_alloc(&entry->mxe_buf, xsize);
3445 if (!entry->mxe_buf.lb_buf) {
3446 OBD_FREE_PTR(entry);
3447 GOTO(fini, rc = -ENOMEM);
3450 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3452 lu_buf_free(&entry->mxe_buf);
3453 OBD_FREE_PTR(entry);
3459 entry->mxe_name = xname;
3460 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3465 mdd_xattrs_fini(xattrs);
3469 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3470 struct mdd_object *obj,
3471 const struct lu_buf *buf,
3473 int fl, struct thandle *handle);
3475 static int mdd_foreach_xattr(const struct lu_env *env,
3476 struct mdd_object *tobj,
3477 struct mdd_xattrs *xattrs,
3478 struct thandle *handle,
3481 struct mdd_xattr_entry *entry;
3484 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
3485 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
3493 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3494 struct mdd_object *sobj,
3495 struct mdd_object *tobj,
3496 const struct lu_name *sname,
3497 const struct lu_fid *sfid,
3498 const struct lu_name *lname,
3499 const struct lu_fid *fid,
3501 struct thandle *handle);
3503 static int mdd_declare_update_link(const struct lu_env *env,
3504 struct mdd_object *sobj,
3505 struct mdd_object *tobj,
3506 const struct lu_name *tname,
3507 const struct lu_fid *tpfid,
3508 const struct lu_name *lname,
3509 const struct lu_fid *fid,
3511 struct thandle *handle)
3513 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3514 struct mdd_object *pobj;
3518 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3519 !strcmp(tname->ln_name, lname->ln_name))
3522 pobj = mdd_object_find(env, mdd, fid);
3524 return PTR_ERR(pobj);
3527 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3529 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3530 mdd_object_type(sobj),
3531 lname->ln_name, handle);
3532 mdd_object_put(env, pobj);
3536 rc = mdo_declare_ref_add(env, tobj, handle);
3540 rc = mdo_declare_ref_del(env, sobj, handle);
3544 static int mdd_update_link(const struct lu_env *env,
3545 struct mdd_object *sobj,
3546 struct mdd_object *tobj,
3547 const struct lu_name *tname,
3548 const struct lu_fid *tpfid,
3549 const struct lu_name *lname,
3550 const struct lu_fid *fid,
3552 struct thandle *handle)
3554 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3555 struct mdd_object *pobj;
3561 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3562 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3565 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3566 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3568 pobj = mdd_object_find(env, mdd, fid);
3570 CWARN("%s: cannot find obj "DFID": %ld\n",
3571 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3572 RETURN(PTR_ERR(pobj));
3575 if (!mdd_object_exists(pobj)) {
3576 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3577 mdd_object_put(env, pobj);
3581 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3582 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3584 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3585 mdd_object_type(sobj),
3586 lname->ln_name, handle);
3587 mdd_write_unlock(env, pobj);
3588 mdd_object_put(env, pobj);
3592 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3593 rc = mdo_ref_add(env, tobj, handle);
3594 mdd_write_unlock(env, tobj);
3598 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3599 rc = mdo_ref_del(env, sobj, handle);
3600 mdd_write_unlock(env, sobj);
3605 static inline int mdd_fld_lookup(const struct lu_env *env,
3606 struct mdd_device *mdd,
3607 const struct lu_fid *fid,
3610 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3611 struct seq_server_site *ss;
3614 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3616 range->lsr_flags = LU_SEQ_RANGE_MDT;
3617 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3621 *mdt_index = range->lsr_index;
3626 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3627 struct mdd_object *sobj,
3628 struct mdd_object *tobj,
3629 const struct lu_name *tname,
3630 const struct lu_fid *tpfid,
3631 const struct lu_name *lname,
3632 const struct lu_fid *fid,
3634 struct thandle *handle)
3636 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3637 __u32 source_mdt_index = *(__u32 *)opaque;
3638 __u32 link_mdt_index;
3644 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3645 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3648 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3652 RETURN(link_mdt_index == source_mdt_index);
3655 static int mdd_iterate_linkea(const struct lu_env *env,
3656 struct mdd_object *sobj,
3657 struct mdd_object *tobj,
3658 const struct lu_name *tname,
3659 const struct lu_fid *tpfid,
3660 struct linkea_data *ldata,
3662 struct thandle *handle,
3665 struct mdd_thread_info *info = mdd_env_info(env);
3666 char *filename = info->mdi_name;
3667 struct lu_name lname;
3674 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3675 linkea_next_entry(ldata)) {
3676 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3679 /* Note: lname might miss \0 at the end */
3680 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3681 lname.ln_namelen, lname.ln_name);
3682 lname.ln_name = filename;
3684 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3686 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3694 * Prepare linkea, and check whether file needs migrate: if source still has
3695 * link on source MDT, no need to migrate, just update namespace on source and
3698 * \retval 0 do migrate
3699 * \retval 1 don't migrate
3700 * \retval -errno on failure
3702 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3703 struct mdd_device *mdd,
3704 struct mdd_object *spobj,
3705 struct mdd_object *tpobj,
3706 struct mdd_object *sobj,
3707 const struct lu_name *sname,
3708 const struct lu_name *tname,
3709 const struct lu_attr *attr,
3710 struct linkea_data *ldata)
3712 __u32 source_mdt_index;
3717 memset(ldata, 0, sizeof(*ldata));
3718 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3719 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3724 * Then it will check if the file should be migrated. If the file has
3725 * mulitple links, we only need migrate the file if all of its entries
3726 * has been migrated to the remote MDT.
3728 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3731 /* If there are still links locally, don't migrate this file */
3732 LASSERT(ldata->ld_leh != NULL);
3735 * If linkEA is overflow, it means there are some unknown name entries
3736 * under unknown parents, which will prevent the migration.
3738 if (unlikely(ldata->ld_leh->leh_overflow_time))
3741 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3745 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3746 ldata, &source_mdt_index, NULL,
3747 mdd_is_link_on_source_mdt);
3751 static int mdd_declare_migrate_update(const struct lu_env *env,
3752 struct mdd_object *spobj,
3753 struct mdd_object *tpobj,
3754 struct mdd_object *obj,
3755 const struct lu_name *sname,
3756 const struct lu_name *tname,
3757 struct lu_attr *attr,
3758 struct lu_attr *spattr,
3759 struct lu_attr *tpattr,
3760 struct linkea_data *ldata,
3762 struct thandle *handle)
3764 struct mdd_thread_info *info = mdd_env_info(env);
3765 struct lu_attr *la = &info->mdi_la_for_fix;
3768 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3772 if (S_ISDIR(attr->la_mode)) {
3773 rc = mdo_declare_ref_del(env, spobj, handle);
3778 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3779 attr->la_mode & S_IFMT,
3780 tname->ln_name, handle);
3784 rc = mdd_declare_links_add(env, obj, handle, ldata);
3788 if (S_ISDIR(attr->la_mode)) {
3789 rc = mdo_declare_ref_add(env, tpobj, handle);
3794 la->la_valid = LA_CTIME | LA_MTIME;
3795 rc = mdo_declare_attr_set(env, spobj, la, handle);
3799 if (tpobj != spobj) {
3800 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3808 static int mdd_declare_migrate_create(const struct lu_env *env,
3809 struct mdd_object *spobj,
3810 struct mdd_object *tpobj,
3811 struct mdd_object *sobj,
3812 struct mdd_object *tobj,
3813 const struct lu_name *sname,
3814 const struct lu_name *tname,
3815 struct lu_attr *spattr,
3816 struct lu_attr *tpattr,
3817 struct lu_attr *attr,
3818 struct lu_buf *sbuf,
3819 struct linkea_data *ldata,
3820 struct mdd_xattrs *xattrs,
3822 struct md_op_spec *spec,
3823 struct dt_allocation_hint *hint,
3824 struct thandle *handle)
3826 struct mdd_thread_info *info = mdd_env_info(env);
3827 struct md_layout_change *mlc = &info->mdi_mlc;
3828 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3833 if (S_ISDIR(attr->la_mode)) {
3834 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3836 mlc->mlc_opc = MD_LAYOUT_DETACH;
3837 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3841 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3842 } else if (S_ISLNK(attr->la_mode)) {
3843 spec->u.sp_symname.ln_name = sbuf->lb_buf;
3844 /* don't count NUL */
3845 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
3846 } else if (S_ISREG(attr->la_mode)) {
3847 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3848 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3851 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3853 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3854 tname, attr, handle, spec, ldata, NULL, NULL,
3860 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3861 * createb yet, copy from sobj.
3863 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3864 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3865 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3867 if (S_ISDIR(attr->la_mode)) {
3869 /* if sobj is not striped, fake a 1-stripe LMV */
3870 LASSERT(sizeof(info->mdi_key) >
3871 lmv_mds_md_size(1, LMV_MAGIC_V1));
3872 lmv = (typeof(lmv))info->mdi_key;
3873 memset(lmv, 0, sizeof(*lmv));
3874 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3875 lmv->lmv_stripe_count = cpu_to_le32(1);
3876 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3877 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3878 mdd_object_fid(sobj));
3879 mlc->mlc_buf.lb_buf = lmv;
3880 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3882 mlc->mlc_buf = *sbuf;
3884 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3885 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3890 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
3891 mdo_declare_xattr_set);
3895 if (S_ISREG(attr->la_mode)) {
3896 struct lu_buf fid_buf;
3898 handle->th_complex = 1;
3900 /* target may be remote, update PFID via sobj. */
3901 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3902 fid_buf.lb_len = sizeof(struct lu_fid);
3903 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3908 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3913 if (!S_ISDIR(attr->la_mode)) {
3914 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3915 mdd_object_fid(tpobj), ldata, NULL,
3916 handle, mdd_declare_update_link);
3921 if (!S_ISDIR(attr->la_mode) || lmv) {
3922 rc = mdo_declare_ref_del(env, sobj, handle);
3926 if (S_ISDIR(attr->la_mode)) {
3927 rc = mdo_declare_ref_del(env, sobj, handle);
3932 rc = mdo_declare_destroy(env, sobj, handle);
3937 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3938 attr, spattr, tpattr, ldata, ma,
3944 * migrate dirent from \a spobj to \a tpobj.
3946 static int mdd_migrate_update(const struct lu_env *env,
3947 struct mdd_object *spobj,
3948 struct mdd_object *tpobj,
3949 struct mdd_object *obj,
3950 const struct lu_name *sname,
3951 const struct lu_name *tname,
3952 struct lu_attr *attr,
3953 struct lu_attr *spattr,
3954 struct lu_attr *tpattr,
3955 struct linkea_data *ldata,
3957 struct thandle *handle)
3959 struct mdd_thread_info *info = mdd_env_info(env);
3960 struct lu_attr *la = &info->mdi_la_for_fix;
3965 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3966 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3967 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3969 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3970 S_ISDIR(attr->la_mode), handle);
3974 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3975 attr->la_mode & S_IFMT,
3976 tname->ln_name, handle);
3980 rc = mdd_links_write(env, obj, ldata, handle);
3984 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3985 la->la_valid = LA_CTIME | LA_MTIME;
3986 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3987 rc = mdd_update_time(env, spobj, spattr, la, handle);
3988 mdd_write_unlock(env, spobj);
3992 if (tpobj != spobj) {
3993 la->la_valid = LA_CTIME | LA_MTIME;
3994 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3995 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3996 mdd_write_unlock(env, tpobj);
4005 * Migrate file/dir to target MDT.
4007 * Create target according to \a spec, and then migrate xattrs, if it's
4008 * directory, migrate source stripes to target.
4010 * \param[in] env execution environment
4011 * \param[in] spobj source parent object
4012 * \param[in] tpobj target parent object
4013 * \param[in] sobj source object
4014 * \param[in] tobj target object
4015 * \param[in] lname file name
4016 * \param[in] spattr source parent attributes
4017 * \param[in] tpattr target parent attributes
4018 * \param[in] attr source attributes
4019 * \param[in] sbuf source LMV buf
4020 * \param[in] spec migrate create spec
4021 * \param[in] hint target creation hint
4022 * \param[in] handle tranasction handle
4024 * \retval 0 on success
4025 * \retval -errno on failure
4027 static int mdd_migrate_create(const struct lu_env *env,
4028 struct mdd_object *spobj,
4029 struct mdd_object *tpobj,
4030 struct mdd_object *sobj,
4031 struct mdd_object *tobj,
4032 const struct lu_name *sname,
4033 const struct lu_name *tname,
4034 struct lu_attr *spattr,
4035 struct lu_attr *tpattr,
4036 struct lu_attr *attr,
4037 const struct lu_buf *sbuf,
4038 struct linkea_data *ldata,
4039 struct mdd_xattrs *xattrs,
4041 struct md_op_spec *spec,
4042 struct dt_allocation_hint *hint,
4043 struct thandle *handle)
4050 * migrate sobj stripes to tobj if it's directory:
4051 * 1. detach stripes from sobj.
4052 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4053 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4055 if (S_ISDIR(attr->la_mode)) {
4056 struct mdd_thread_info *info = mdd_env_info(env);
4057 struct md_layout_change *mlc = &info->mdi_mlc;
4059 mlc->mlc_opc = MD_LAYOUT_DETACH;
4061 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4062 rc = mdo_layout_change(env, sobj, mlc, handle);
4063 mdd_write_unlock(env, sobj);
4068 /* don't set nlink from sobj */
4069 attr->la_valid &= ~LA_NLINK;
4071 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4072 hint, handle, false);
4076 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4077 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4078 mdd_write_unlock(env, tobj);
4082 /* for regular file, update OST objects XATTR_NAME_FID */
4083 if (S_ISREG(attr->la_mode)) {
4084 struct lu_buf fid_buf;
4086 /* target may be remote, update PFID via sobj. */
4087 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4088 fid_buf.lb_len = sizeof(struct lu_fid);
4089 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4094 /* delete LOV to avoid deleting OST objs when destroying sobj */
4095 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4096 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4097 mdd_write_unlock(env, sobj);
4098 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4099 if (rc && rc != -ENODATA)
4104 /* update links FID */
4105 if (!S_ISDIR(attr->la_mode)) {
4106 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4107 mdd_object_fid(tpobj), ldata,
4108 NULL, handle, mdd_update_link);
4113 /* don't destroy sobj if it's plain directory */
4114 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4115 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4116 rc = mdo_ref_del(env, sobj, handle);
4118 if (S_ISDIR(attr->la_mode))
4119 rc = mdo_ref_del(env, sobj, handle);
4121 rc = mdo_destroy(env, sobj, handle);
4123 mdd_write_unlock(env, sobj);
4128 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4129 spattr, tpattr, ldata, ma, handle);
4134 /* NB: if user issued different migrate command, we can't adjust it silently
4135 * here, because this command will decide target MDT in subdir migration in
4138 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4139 const struct lmv_mds_md_v1 *lmv,
4140 const struct lmv_user_md_v1 *lum,
4141 const struct lu_name *lname)
4143 __u32 lum_stripe_count = lum->lum_stripe_count;
4144 __u32 lum_hash_type = lum->lum_hash_type &
4145 cpu_to_le32(LMV_HASH_TYPE_MASK);
4146 __u32 lmv_hash_type = lmv->lmv_hash_type &
4147 cpu_to_le32(LMV_HASH_TYPE_MASK);
4149 if (!lmv_is_sane(lmv))
4152 /* if stripe_count unspecified, set to 1 */
4153 if (!lum_stripe_count)
4154 lum_stripe_count = cpu_to_le32(1);
4156 /* TODO: check specific MDTs */
4157 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4158 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4159 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4160 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4161 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4162 le32_to_cpu(lmv->lmv_master_mdt_index),
4163 le32_to_cpu(lmv->lmv_migrate_offset),
4164 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4173 * Internal function to migrate directory or file between MDTs.
4175 * migrate source to target in following steps:
4176 * 1. create target, append source stripes after target's if it's directory,
4177 * migrate xattrs and update fid of source links.
4178 * 2. update namespace: migrate dirent from source parent to target parent,
4179 * update file linkea, and destroy source if it's not needed any more.
4181 * \param[in] env execution environment
4182 * \param[in] spobj source parent object
4183 * \param[in] tpobj target parent object
4184 * \param[in] sobj source object
4185 * \param[in] tobj target object
4186 * \param[in] sname source file name
4187 * \param[in] tname target file name
4188 * \param[in] spec target creation spec
4189 * \param[in] ma used to update \a pobj mtime and ctime
4191 * \retval 0 on success
4192 * \retval -errno on failure
4194 static int mdd_migrate_object(const struct lu_env *env,
4195 struct mdd_object *spobj,
4196 struct mdd_object *tpobj,
4197 struct mdd_object *sobj,
4198 struct mdd_object *tobj,
4199 const struct lu_name *sname,
4200 const struct lu_name *tname,
4201 struct md_op_spec *spec,
4204 struct mdd_thread_info *info = mdd_env_info(env);
4205 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4206 struct lu_attr *spattr = &info->mdi_pattr;
4207 struct lu_attr *tpattr = &info->mdi_tpattr;
4208 struct lu_attr *attr = &info->mdi_cattr;
4209 struct linkea_data *ldata = &info->mdi_link_data;
4210 struct dt_allocation_hint *hint = &info->mdi_hint;
4211 struct lu_buf sbuf = { NULL };
4212 struct mdd_xattrs xattrs;
4213 struct lmv_mds_md_v1 *lmv;
4214 struct thandle *handle;
4219 rc = mdd_la_get(env, sobj, attr);
4223 rc = mdd_la_get(env, spobj, spattr);
4227 rc = mdd_la_get(env, tpobj, tpattr);
4231 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4232 spattr, tpattr, attr);
4236 mdd_xattrs_init(&xattrs);
4238 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4239 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4243 /* if user use default value '0' for stripe_count, we need to
4244 * adjust it to '1' to create a 1-stripe directory.
4246 if (lum->lum_stripe_count == 0)
4247 lum->lum_stripe_count = cpu_to_le32(1);
4249 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4250 if (rc && rc != -ENODATA)
4255 if (!lmv_is_sane(lmv))
4256 GOTO(out, rc = -EBADF);
4257 if (lmv_is_migrating(lmv)) {
4258 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4263 } else if (!S_ISDIR(attr->la_mode)) {
4265 GOTO(out, rc = -EALREADY);
4267 /* update namespace only if @sobj is on MDT where @tpobj is. */
4268 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4269 spec->sp_migrate_nsonly = true;
4271 if (S_ISLNK(attr->la_mode)) {
4272 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4274 GOTO(out, rc = -ENOMEM);
4276 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4279 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4280 mdd2obd_dev(mdd)->obd_name,
4281 PFID(mdd_object_fid(sobj)), rc);
4287 /* linkea needs update upon FID or parent stripe change */
4288 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4289 tname, attr, ldata);
4291 /* update namespace only if @sobj has link on its MDT. */
4292 spec->sp_migrate_nsonly = true;
4296 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4297 * RPCs inside transaction.
4299 if (!spec->sp_migrate_nsonly) {
4300 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, false);
4305 handle = mdd_trans_create(env, mdd);
4307 GOTO(out, rc = PTR_ERR(handle));
4309 if (spec->sp_migrate_nsonly)
4310 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4311 tname, attr, spattr, tpattr,
4314 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4315 sname, tname, spattr, tpattr,
4316 attr, &sbuf, ldata, &xattrs, ma,
4317 spec, hint, handle);
4321 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4326 rc = mdd_trans_start(env, mdd, handle);
4330 if (spec->sp_migrate_nsonly)
4331 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4332 attr, spattr, tpattr, ldata, ma,
4335 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4336 tname, spattr, tpattr, attr, &sbuf,
4337 ldata, &xattrs, ma, spec, hint, handle);
4341 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4342 spec->sp_migrate_nsonly ? sobj : tobj,
4343 mdd_object_fid(spobj), mdd_object_fid(sobj),
4344 mdd_object_fid(tpobj), tname, sname,
4351 rc = mdd_trans_stop(env, mdd, rc, handle);
4353 mdd_xattrs_fini(&xattrs);
4360 * Migrate directory or file between MDTs.
4362 * \param[in] env execution environment
4363 * \param[in] md_pobj parent master object
4364 * \param[in] md_sobj source object
4365 * \param[in] lname file name
4366 * \param[in] md_tobj target object
4367 * \param[in] spec target creation spec
4368 * \param[in] ma used to update \a pobj mtime and ctime
4370 * \retval 0 on success
4371 * \retval -errno on failure
4373 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4374 struct md_object *md_sobj, const struct lu_name *lname,
4375 struct md_object *md_tobj, struct md_op_spec *spec,
4378 struct mdd_thread_info *info = mdd_env_info(env);
4379 struct mdd_device *mdd = mdo2mdd(md_pobj);
4380 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4381 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4382 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4383 struct mdd_object *spobj = NULL;
4384 struct mdd_object *tpobj = NULL;
4385 struct lu_buf pbuf = { NULL };
4386 struct lu_fid *fid = &info->mdi_fid2;
4387 struct lmv_mds_md_v1 *lmv;
4392 /* locate source and target stripe on pobj, which are the real parent */
4393 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4394 if (rc < 0 && rc != -ENODATA)
4401 if (!lmv_is_sane(lmv))
4402 GOTO(out, rc = -EBADF);
4404 /* locate target parent stripe */
4405 /* fail check here to make sure top dir migration succeed. */
4406 if (lmv_is_migrating(lmv) &&
4407 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4408 GOTO(out, rc = -EIO);
4410 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4413 GOTO(out, rc = index);
4415 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4416 tpobj = mdd_object_find(env, mdd, fid);
4418 GOTO(out, rc = PTR_ERR(tpobj));
4420 /* locate source parent stripe */
4421 if (lmv_is_layout_changing(lmv)) {
4422 index = lmv_name_to_stripe_index_old(lmv,
4426 GOTO(out, rc = index);
4428 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4429 spobj = mdd_object_find(env, mdd, fid);
4431 GOTO(out, rc = PTR_ERR(spobj));
4433 /* parent stripe unchanged */
4434 if (spobj == tpobj) {
4435 if (!lmv_is_restriping(lmv))
4436 GOTO(out, rc = -EINVAL);
4437 GOTO(out, rc = -EALREADY);
4441 mdd_object_get(spobj);
4446 mdd_object_get(tpobj);
4447 mdd_object_get(spobj);
4450 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4455 if (!IS_ERR_OR_NULL(spobj))
4456 mdd_object_put(env, spobj);
4457 if (!IS_ERR_OR_NULL(tpobj))
4458 mdd_object_put(env, tpobj);
4464 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4465 struct mdd_object *pobj,
4466 struct mdd_object *obj,
4467 struct mdd_object *stripe,
4468 struct lu_attr *attr,
4469 struct mdd_xattrs *xattrs,
4470 struct md_layout_change *mlc,
4471 struct lu_name *lname,
4472 struct thandle *handle)
4476 mlc->mlc_opc = MD_LAYOUT_DETACH;
4477 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4481 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4482 S_IFDIR, dotdot, handle);
4486 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
4487 mdo_declare_xattr_set);
4491 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4495 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4499 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4503 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4504 attr->la_mode, lname->ln_name, handle);
4508 rc = mdo_declare_ref_del(env, obj, handle);
4512 rc = mdo_declare_ref_del(env, obj, handle);
4516 rc = mdo_declare_destroy(env, obj, handle);
4523 /* transform one-stripe directory to a plain directory */
4524 static int mdd_1sd_collapse(const struct lu_env *env,
4525 struct mdd_object *pobj,
4526 struct mdd_object *obj,
4527 struct mdd_object *stripe,
4528 struct lu_attr *attr,
4529 struct mdd_xattrs *xattrs,
4530 struct md_layout_change *mlc,
4531 struct lu_name *lname,
4532 struct thandle *handle)
4538 /* replace 1-stripe directory with its stripe */
4539 mlc->mlc_opc = MD_LAYOUT_DETACH;
4541 mdd_write_lock(env, obj, DT_SRC_PARENT);
4542 rc = mdo_layout_change(env, obj, mlc, handle);
4543 mdd_write_unlock(env, obj);
4547 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4548 mdd_write_lock(env, obj, DT_SRC_CHILD);
4550 /* insert dotdot to stripe which points to parent */
4551 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4552 S_IFDIR, dotdot, handle);
4556 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
4561 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4565 /* don't set nlink from parent */
4566 attr->la_valid &= ~LA_NLINK;
4568 rc = mdo_attr_set(env, stripe, attr, handle);
4572 /* delete dir name from parent */
4573 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4577 /* insert stripe to parent with dir name */
4578 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4579 attr->la_mode, lname->ln_name, handle);
4583 /* destroy dir obj */
4584 rc = mdo_ref_del(env, obj, handle);
4588 rc = mdo_ref_del(env, obj, handle);
4592 rc = mdo_destroy(env, obj, handle);
4598 mdd_write_unlock(env, obj);
4599 mdd_write_unlock(env, pobj);
4605 * shrink directory stripes after migration/merge
4607 int mdd_dir_layout_shrink(const struct lu_env *env,
4608 struct md_object *md_obj,
4609 struct md_layout_change *mlc)
4611 struct mdd_device *mdd = mdo2mdd(md_obj);
4612 struct mdd_thread_info *info = mdd_env_info(env);
4613 struct mdd_object *obj = md2mdd_obj(md_obj);
4614 struct mdd_object *pobj = NULL;
4615 struct mdd_object *stripe = NULL;
4616 struct lu_attr *attr = &info->mdi_pattr;
4617 struct lu_fid *fid = &info->mdi_fid2;
4618 struct lu_name lname = { NULL };
4619 struct lu_buf lmv_buf = { NULL };
4620 struct mdd_xattrs xattrs;
4621 struct lmv_mds_md_v1 *lmv;
4622 struct lmv_user_md *lmu;
4623 struct thandle *handle;
4628 rc = mdd_la_get(env, obj, attr);
4632 if (!S_ISDIR(attr->la_mode))
4635 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4639 lmv = lmv_buf.lb_buf;
4640 if (!lmv_is_sane(lmv))
4643 lmu = mlc->mlc_buf.lb_buf;
4645 /* adjust the default value '0' to '1' */
4646 if (lmu->lum_stripe_count == 0)
4647 lmu->lum_stripe_count = cpu_to_le32(1);
4649 /* these were checked in MDT */
4650 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4651 le32_to_cpu(lmv->lmv_stripe_count));
4652 LASSERT(!lmv_is_splitting(lmv));
4653 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4655 mdd_xattrs_init(&xattrs);
4657 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4658 * to a plain dir, which will cause FID change and namespace update.
4660 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4661 struct linkea_data *ldata = &info->mdi_link_data;
4662 char *filename = info->mdi_name;
4664 rc = mdd_links_read(env, obj, ldata);
4668 if (ldata->ld_leh->leh_reccount > 1)
4669 GOTO(out, rc = -EINVAL);
4671 linkea_first_entry(ldata);
4673 GOTO(out, rc = -ENODATA);
4675 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4678 /* Note: lname might miss \0 at the end */
4679 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4680 lname.ln_namelen, lname.ln_name);
4681 lname.ln_name = filename;
4683 pobj = mdd_object_find(env, mdd, fid);
4690 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4692 stripe = mdd_object_find(env, mdd, fid);
4693 if (IS_ERR(stripe)) {
4694 mdd_object_put(env, pobj);
4696 GOTO(out, rc = PTR_ERR(stripe));
4699 if (!lmv_is_fixed(lmv))
4700 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true);
4703 handle = mdd_trans_create(env, mdd);
4705 GOTO(out, rc = PTR_ERR(handle));
4707 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4708 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4710 GOTO(stop_trans, rc);
4712 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4713 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
4714 &xattrs, mlc, &lname, handle);
4716 GOTO(stop_trans, rc);
4719 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4722 GOTO(stop_trans, rc);
4724 rc = mdd_trans_start(env, mdd, handle);
4726 GOTO(stop_trans, rc);
4728 mdd_write_lock(env, obj, DT_SRC_PARENT);
4729 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4730 rc = mdo_layout_change(env, obj, mlc, handle);
4731 mdd_write_unlock(env, obj);
4733 GOTO(stop_trans, rc);
4735 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4736 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
4737 mlc, &lname, handle);
4739 GOTO(stop_trans, rc);
4742 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4743 XATTR_NAME_LMV, handle);
4744 GOTO(stop_trans, rc);
4747 rc = mdd_trans_stop(env, mdd, rc, handle);
4749 mdd_xattrs_fini(&xattrs);
4751 mdd_object_put(env, stripe);
4752 mdd_object_put(env, pobj);
4754 lu_buf_free(&lmv_buf);
4758 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4759 struct mdd_device *mdd,
4760 struct mdd_object *pobj,
4761 struct mdd_object *obj,
4762 struct mdd_object *tobj,
4763 struct mdd_xattrs *xattrs,
4764 struct md_layout_change *mlc,
4765 struct dt_allocation_hint *hint,
4766 struct thandle *handle)
4768 struct mdd_thread_info *info = mdd_env_info(env);
4769 const struct lu_name *lname = mlc->mlc_name;
4770 struct lu_attr *la = &info->mdi_la_for_fix;
4771 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4772 struct linkea_data *ldata = &info->mdi_link_data;
4773 struct lmv_mds_md_v1 *lmv;
4777 mlc->mlc_opc = MD_LAYOUT_DETACH;
4778 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4782 memset(ldata, 0, sizeof(*ldata));
4783 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4784 lname, 1, 0, ldata);
4788 count = lum->lum_stripe_count;
4789 lum->lum_stripe_count = 0;
4790 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4792 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4793 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4794 ldata, NULL, NULL, NULL, hint);
4798 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4801 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4803 lmv = (typeof(lmv))info->mdi_key;
4804 memset(lmv, 0, sizeof(*lmv));
4805 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4806 lmv->lmv_stripe_count = cpu_to_le32(1);
4807 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4808 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4810 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4811 mlc->mlc_buf.lb_buf = lmv;
4812 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4813 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4817 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4818 mdo_declare_xattr_set);
4822 lum->lum_stripe_count = count;
4823 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4824 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4828 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4832 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4833 S_IFDIR, lname->ln_name, handle);
4837 la->la_valid = LA_CTIME | LA_MTIME;
4838 rc = mdo_declare_attr_set(env, obj, la, handle);
4842 rc = mdo_declare_attr_set(env, pobj, la, handle);
4846 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4852 * plain directory split:
4853 * 1. create \a tobj as plain directory.
4854 * 2. append \a obj as first stripe of \a tobj.
4855 * 3. migrate xattrs from \a obj to \a tobj.
4856 * 4. split \a tobj to specific stripe count.
4858 static int mdd_dir_split_plain(const struct lu_env *env,
4859 struct mdd_device *mdd,
4860 struct mdd_object *pobj,
4861 struct mdd_object *obj,
4862 struct mdd_object *tobj,
4863 struct mdd_xattrs *xattrs,
4864 struct md_layout_change *mlc,
4865 struct dt_allocation_hint *hint,
4866 struct thandle *handle)
4868 struct mdd_thread_info *info = mdd_env_info(env);
4869 struct lu_attr *pattr = &info->mdi_pattr;
4870 struct lu_attr *la = &info->mdi_la_for_fix;
4871 const struct lu_name *lname = mlc->mlc_name;
4872 struct linkea_data *ldata = &info->mdi_link_data;
4877 /* copy linkea out and set on target later */
4878 rc = mdd_links_read(env, obj, ldata);
4882 mlc->mlc_opc = MD_LAYOUT_DETACH;
4883 rc = mdo_layout_change(env, obj, mlc, handle);
4887 /* don't set nlink from obj */
4888 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4890 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4891 NULL, NULL, NULL, hint, handle, false);
4895 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4899 rc = mdd_links_write(env, tobj, ldata, handle);
4903 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4907 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4908 lname->ln_name, handle);
4912 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4913 la->la_valid = LA_CTIME | LA_MTIME;
4915 mdd_write_lock(env, obj, DT_SRC_CHILD);
4916 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4917 mdd_write_unlock(env, obj);
4921 rc = mdd_la_get(env, pobj, pattr);
4925 la->la_valid = LA_CTIME | LA_MTIME;
4927 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4928 rc = mdd_update_time(env, pobj, pattr, la, handle);
4929 mdd_write_unlock(env, pobj);
4933 /* FID changes, record it as CL_MIGRATE */
4934 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4935 mdd_object_fid(pobj), mdd_object_fid(obj),
4936 mdd_object_fid(pobj), lname, lname, handle);
4940 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4941 struct md_layout_change *mlc)
4943 struct mdd_thread_info *info = mdd_env_info(env);
4944 struct mdd_device *mdd = mdo2mdd(o);
4945 struct mdd_object *obj = md2mdd_obj(o);
4946 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4947 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4948 struct dt_allocation_hint *hint = &info->mdi_hint;
4949 bool is_plain = false;
4950 struct mdd_xattrs xattrs;
4951 struct thandle *handle;
4956 LASSERT(S_ISDIR(mdd_object_type(obj)));
4958 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4964 mdd_xattrs_init(&xattrs);
4966 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true);
4968 handle = mdd_trans_create(env, mdd);
4970 GOTO(out, rc = PTR_ERR(handle));
4973 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
4974 &xattrs, mlc, hint, handle);
4976 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4977 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4979 GOTO(stop_trans, rc);
4981 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4985 GOTO(stop_trans, rc);
4987 rc = mdd_trans_start(env, mdd, handle);
4989 GOTO(stop_trans, rc);
4992 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
4995 mdd_write_lock(env, obj, DT_TGT_CHILD);
4996 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4997 LU_XATTR_CREATE, handle);
4998 mdd_write_unlock(env, obj);
5000 GOTO(stop_trans, rc);
5002 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5003 XATTR_NAME_LMV, handle);
5006 GOTO(stop_trans, rc);
5011 rc = mdd_trans_stop(env, mdd, rc, handle);
5013 mdd_xattrs_fini(&xattrs);
5018 const struct md_dir_operations mdd_dir_ops = {
5019 .mdo_is_subdir = mdd_is_subdir,
5020 .mdo_lookup = mdd_lookup,
5021 .mdo_create = mdd_create,
5022 .mdo_rename = mdd_rename,
5023 .mdo_link = mdd_link,
5024 .mdo_unlink = mdd_unlink,
5025 .mdo_create_data = mdd_create_data,
5026 .mdo_migrate = mdd_migrate,