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 bool mdd_changelog_need_gc(const struct lu_env *env, struct mdd_device *mdd,
826 struct llog_handle *lgh)
828 unsigned long free_cat_entries = llog_cat_free_space(lgh);
829 struct mdd_changelog *mc = &mdd->mdd_cl;
831 return free_cat_entries <= mdd->mdd_changelog_min_free_cat_entries ||
832 mdd_changelog_is_too_idle(mdd, mc->mc_minrec, mc->mc_mintime) ||
833 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD);
836 /** Add a changelog entry \a rec to the changelog llog
839 * \param handle - currently ignored since llogs start their own transaction;
840 * this will hopefully be fixed in llog rewrite
843 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
844 struct llog_changelog_rec *rec, struct thandle *th)
846 struct obd_device *obd = mdd2obd_dev(mdd);
847 struct llog_ctxt *ctxt;
848 struct thandle *llog_th;
852 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
853 changelog_rec_varsize(&rec->cr));
855 /* llog_lvfs_write_rec sets the llog tail len */
856 rec->cr_hdr.lrh_type = CHANGELOG_REC;
857 rec->cr.cr_time = cl_time();
859 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
863 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
865 GOTO(out_put, rc = PTR_ERR(llog_th));
867 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
868 /* nested journal transaction */
869 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
871 /* time to recover some space ?? */
872 if (likely(!mdd->mdd_changelog_gc ||
873 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
874 mdd->mdd_changelog_min_gc_interval >=
875 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
876 /* save a spin_lock trip */
879 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_IDX_PUMP)) {
880 spin_lock(&mdd->mdd_cl.mc_lock);
881 mdd->mdd_cl.mc_index += cfs_fail_val;
882 spin_unlock(&mdd->mdd_cl.mc_lock);
885 need_gc = mdd_changelog_need_gc(env, mdd, ctxt->loc_handle);
886 spin_lock(&mdd->mdd_cl.mc_lock);
887 if (likely(mdd->mdd_changelog_gc &&
888 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
889 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
890 mdd->mdd_changelog_min_gc_interval)) {
891 if (unlikely(need_gc)) {
892 CWARN("%s:%s starting changelog garbage collection\n",
894 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
896 /* indicate further kthread run will occur outside
897 * right after current journal transaction filling has
900 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
902 /* next check in mdd_changelog_min_gc_interval anyway
904 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
906 spin_unlock(&mdd->mdd_cl.mc_lock);
914 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
915 const struct lu_fid *sfid,
916 const struct lu_fid *spfid,
917 const struct lu_name *sname)
919 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
922 LASSERT(sfid != NULL);
923 LASSERT(spfid != NULL);
924 LASSERT(sname != NULL);
926 extsize = sname->ln_namelen + 1;
928 rnm->cr_sfid = *sfid;
929 rnm->cr_spfid = *spfid;
931 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
932 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
933 rec->cr_namelen += extsize;
936 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
938 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
940 if (jobid == NULL || jobid[0] == '\0')
943 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
946 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
948 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
950 ef->cr_extra_flags = eflags;
953 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
954 __u64 uid, __u64 gid)
956 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
958 uidgid->cr_uid = uid;
959 uidgid->cr_gid = gid;
962 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
965 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
970 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
972 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
974 omd->cr_openflags = flags;
977 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
978 const char *xattr_name)
980 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
982 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
985 /** Store a namespace change changelog record
986 * If this fails, we must fail the whole transaction; we don't
987 * want the change to commit without the log entry.
988 * \param target - mdd_object of change
989 * \param tpfid - target parent dir/object fid
990 * \param sfid - source object fid
991 * \param spfid - source parent fid
992 * \param tname - target name string
993 * \param sname - source name string
994 * \param handle - transaction handle
996 int mdd_changelog_ns_store(const struct lu_env *env,
997 struct mdd_device *mdd,
998 enum changelog_rec_type type,
999 enum changelog_rec_flags clf_flags,
1000 struct mdd_object *target,
1001 const struct lu_fid *tpfid,
1002 const struct lu_fid *sfid,
1003 const struct lu_fid *spfid,
1004 const struct lu_name *tname,
1005 const struct lu_name *sname,
1006 struct thandle *handle)
1008 const struct lu_ucred *uc = lu_ucred(env);
1009 struct llog_changelog_rec *rec;
1012 __u64 xflags = CLFE_INVALID;
1016 if (!mdd_changelog_enabled(env, mdd, type))
1019 LASSERT(tpfid != NULL);
1020 LASSERT(tname != NULL);
1021 LASSERT(handle != NULL);
1023 reclen = mdd_llog_record_calc_size(env, tname, sname);
1024 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
1025 if (buf->lb_buf == NULL)
1029 clf_flags &= CLF_FLAGMASK;
1030 clf_flags |= CLF_EXTRA_FLAGS;
1033 if (uc->uc_jobid[0] != '\0')
1034 clf_flags |= CLF_JOBID;
1035 xflags |= CLFE_UIDGID;
1040 clf_flags |= CLF_RENAME;
1042 clf_flags |= CLF_VERSION;
1044 rec->cr.cr_flags = clf_flags;
1046 if (clf_flags & CLF_EXTRA_FLAGS) {
1047 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1048 if (xflags & CLFE_UIDGID)
1049 mdd_changelog_rec_extra_uidgid(&rec->cr,
1050 uc->uc_uid, uc->uc_gid);
1051 if (xflags & CLFE_NID)
1052 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1055 rec->cr.cr_type = (__u32)type;
1056 rec->cr.cr_pfid = *tpfid;
1057 rec->cr.cr_namelen = tname->ln_namelen;
1058 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1060 if (clf_flags & CLF_RENAME)
1061 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1063 if (clf_flags & CLF_JOBID)
1064 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1066 if (likely(target != NULL)) {
1067 rec->cr.cr_tfid = *mdd_object_fid(target);
1068 target->mod_cltime = ktime_get();
1070 fid_zero(&rec->cr.cr_tfid);
1073 rc = mdd_changelog_store(env, mdd, rec, handle);
1075 CERROR("%s: cannot store changelog record: type = %d, "
1076 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1077 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1078 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1085 static int __mdd_links_add(const struct lu_env *env,
1086 struct mdd_object *mdd_obj,
1087 struct linkea_data *ldata,
1088 const struct lu_name *lname,
1089 const struct lu_fid *pfid,
1090 int first, int check)
1092 /* cattr is set in mdd_link */
1093 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1096 if (ldata->ld_leh == NULL) {
1097 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1101 rc = linkea_data_new(ldata,
1102 &mdd_env_info(env)->mdi_link_buf);
1109 rc = linkea_links_find(ldata, lname, pfid);
1110 if (rc && rc != -ENOENT)
1116 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1117 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1121 linkea_add_buf(ldata, lname, tfid, false);
1124 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1125 linkea_add_buf(ldata, lname, pfid, false);
1127 /* For encrypted file, we want to limit number of hard links to what
1128 * linkEA can contain. So ask to return error in case of overflow.
1129 * Currently linkEA stores 4KiB of links, that is 14 NAME_MAX links,
1130 * or 119 16-byte names.
1132 return linkea_add_buf(ldata, lname, pfid,
1133 cattr->la_valid & LA_FLAGS &&
1134 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1137 static int __mdd_links_del(const struct lu_env *env,
1138 struct mdd_object *mdd_obj,
1139 struct linkea_data *ldata,
1140 const struct lu_name *lname,
1141 const struct lu_fid *pfid)
1143 /* cattr is set in mdd_link */
1144 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1147 if (ldata->ld_leh == NULL) {
1148 rc = mdd_links_read(env, mdd_obj, ldata);
1153 rc = linkea_links_find(ldata, lname, pfid);
1157 linkea_del_buf(ldata, lname,
1158 cattr->la_valid & LA_FLAGS &&
1159 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1163 static int mdd_linkea_prepare(const struct lu_env *env,
1164 struct mdd_object *mdd_obj,
1165 const struct lu_fid *oldpfid,
1166 const struct lu_name *oldlname,
1167 const struct lu_fid *newpfid,
1168 const struct lu_name *newlname,
1169 int first, int check,
1170 struct linkea_data *ldata)
1175 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1178 LASSERT(oldpfid != NULL || newpfid != NULL);
1180 if (mdd_obj->mod_flags & DEAD_OBJ)
1181 /* Unnecessary to update linkEA for dead object. */
1184 if (oldpfid != NULL) {
1185 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1187 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1190 /* No changes done. */
1195 /* If renaming, add the new record */
1196 if (newpfid != NULL)
1197 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1203 int mdd_links_rename(const struct lu_env *env,
1204 struct mdd_object *mdd_obj,
1205 const struct lu_fid *oldpfid,
1206 const struct lu_name *oldlname,
1207 const struct lu_fid *newpfid,
1208 const struct lu_name *newlname,
1209 struct thandle *handle,
1210 struct linkea_data *ldata,
1211 int first, int check)
1216 if (ldata == NULL) {
1217 ldata = &mdd_env_info(env)->mdi_link_data;
1218 memset(ldata, 0, sizeof(*ldata));
1219 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1220 newpfid, newlname, first, check, ldata);
1225 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1226 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1232 if (newlname == NULL)
1233 CERROR("link_ea add failed %d "DFID"\n",
1234 rc, PFID(mdd_object_fid(mdd_obj)));
1235 else if (oldpfid == NULL)
1236 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1237 newlname->ln_namelen, newlname->ln_name, rc,
1238 PFID(mdd_object_fid(mdd_obj)));
1239 else if (newpfid == NULL)
1240 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1241 oldlname->ln_namelen, oldlname->ln_name, rc,
1242 PFID(mdd_object_fid(mdd_obj)));
1244 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1245 "\n", oldlname->ln_namelen, oldlname->ln_name,
1246 newlname->ln_namelen, newlname->ln_name, rc,
1247 PFID(mdd_object_fid(mdd_obj)));
1250 if (is_vmalloc_addr(ldata->ld_buf))
1251 /* if we vmalloced a large buffer drop it */
1252 lu_buf_free(ldata->ld_buf);
1257 static inline int mdd_links_add(const struct lu_env *env,
1258 struct mdd_object *mdd_obj,
1259 const struct lu_fid *pfid,
1260 const struct lu_name *lname,
1261 struct thandle *handle,
1262 struct linkea_data *ldata, int first)
1264 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1265 pfid, lname, handle, ldata, first, 0);
1268 static inline int mdd_links_del(const struct lu_env *env,
1269 struct mdd_object *mdd_obj,
1270 const struct lu_fid *pfid,
1271 const struct lu_name *lname,
1272 struct thandle *handle)
1274 return mdd_links_rename(env, mdd_obj, pfid, lname,
1275 NULL, NULL, handle, NULL, 0, 0);
1278 /** Read the link EA into a temp buffer.
1279 * Uses the name_buf since it is generally large.
1280 * \retval IS_ERR err
1281 * \retval ptr to \a lu_buf (always \a mdi_link_buf)
1283 struct lu_buf *mdd_links_get(const struct lu_env *env,
1284 struct mdd_object *mdd_obj)
1286 struct linkea_data ldata = { NULL };
1289 rc = mdd_links_read(env, mdd_obj, &ldata);
1290 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1293 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1294 struct linkea_data *ldata, struct thandle *handle)
1296 const struct lu_buf *buf;
1299 if (ldata == NULL || ldata->ld_buf == NULL ||
1300 ldata->ld_leh == NULL)
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1307 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1308 ldata->ld_leh->leh_len);
1309 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1310 if (unlikely(rc == -ENOSPC)) {
1311 rc = linkea_overflow_shrink(ldata);
1319 static int mdd_declare_links_add(const struct lu_env *env,
1320 struct mdd_object *mdd_obj,
1321 struct thandle *handle,
1322 struct linkea_data *ldata)
1328 if (ldata != NULL && ldata->ld_leh != NULL) {
1329 ea_len = ldata->ld_leh->leh_len;
1330 linkea = ldata->ld_buf->lb_buf;
1332 ea_len = MAX_LINKEA_SIZE;
1336 rc = mdo_declare_xattr_set(env, mdd_obj,
1337 mdd_buf_get_const(env, linkea, ea_len),
1338 XATTR_NAME_LINK, 0, handle);
1343 static inline int mdd_declare_links_del(const struct lu_env *env,
1344 struct mdd_object *c,
1345 struct thandle *handle)
1349 /* For directory, the linkEA will be removed together
1350 * with the object. */
1351 if (!S_ISDIR(mdd_object_type(c)))
1352 rc = mdd_declare_links_add(env, c, handle, NULL);
1357 static int mdd_declare_link(const struct lu_env *env,
1358 struct mdd_device *mdd,
1359 struct mdd_object *p,
1360 struct mdd_object *c,
1361 const struct lu_name *name,
1362 struct thandle *handle,
1364 struct linkea_data *data)
1366 struct lu_fid tfid = *mdd_object_fid(c);
1369 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1370 tfid.f_oid = cfs_fail_val;
1372 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1373 name->ln_name, handle);
1377 rc = mdo_declare_ref_add(env, c, handle);
1381 la->la_valid = LA_CTIME | LA_MTIME;
1382 rc = mdo_declare_attr_set(env, p, la, handle);
1386 la->la_valid = LA_CTIME;
1387 rc = mdo_declare_attr_set(env, c, la, handle);
1391 rc = mdd_declare_links_add(env, c, handle, data);
1395 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1401 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1402 struct md_object *src_obj, const struct lu_name *lname,
1405 const char *name = lname->ln_name;
1406 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1407 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1408 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1409 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1410 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1411 struct mdd_device *mdd = mdo2mdd(src_obj);
1412 struct thandle *handle;
1413 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1414 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
1418 rc = mdd_la_get(env, mdd_sobj, cattr);
1422 rc = mdd_la_get(env, mdd_tobj, tattr);
1427 * If we are using project inheritance, we only allow hard link
1428 * creation in our tree when the project IDs are the same;
1429 * otherwise the tree quota mechanism could be circumvented.
1431 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1432 (tattr->la_projid != cattr->la_projid))
1435 handle = mdd_trans_create(env, mdd);
1437 GOTO(out_pending, rc = PTR_ERR(handle));
1439 memset(ldata, 0, sizeof(*ldata));
1441 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1442 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1444 /* Note: even this function will change ldata, but it comes from
1445 * thread_info, which is completely temporary and only seen in
1446 * this function, so we do not need reset ldata once it fails.*/
1447 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1448 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1452 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1457 rc = mdd_trans_start(env, mdd, handle);
1461 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1462 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1465 GOTO(out_unlock, rc);
1467 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1468 rc = mdo_ref_add(env, mdd_sobj, handle);
1470 GOTO(out_unlock, rc);
1473 *tfid = *mdd_object_fid(mdd_sobj);
1474 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1475 tfid->f_oid = cfs_fail_val;
1477 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1478 mdd_object_type(mdd_sobj), name, handle);
1480 mdo_ref_del(env, mdd_sobj, handle);
1481 GOTO(out_unlock, rc);
1484 la->la_valid = LA_CTIME | LA_MTIME;
1485 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1487 GOTO(out_unlock, rc);
1489 la->la_valid = LA_CTIME;
1490 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1492 /* Note: The failure of links_add should not cause the
1493 * link failure, so do not check return value. */
1494 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1495 lname, handle, ldata, 0);
1499 mdd_write_unlock(env, mdd_sobj);
1501 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1502 mdd_object_fid(mdd_tobj), NULL,
1503 NULL, lname, NULL, handle);
1505 rc = mdd_trans_stop(env, mdd, rc, handle);
1506 if (is_vmalloc_addr(ldata->ld_buf))
1507 /* if we vmalloced a large buffer drop it */
1508 lu_buf_free(ldata->ld_buf);
1513 static int mdd_mark_orphan_object(const struct lu_env *env,
1514 struct mdd_object *obj, struct thandle *handle,
1517 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1520 attr->la_valid = LA_FLAGS;
1521 attr->la_flags = LUSTRE_ORPHAN_FL;
1524 rc = mdo_declare_attr_set(env, obj, attr, handle);
1526 rc = mdo_attr_set(env, obj, attr, handle);
1531 static int mdd_declare_finish_unlink(const struct lu_env *env,
1532 struct mdd_object *obj,
1533 struct thandle *handle)
1537 /* Sigh, we do not know if the unlink object will become orphan in
1538 * declare phase, but fortunately the flags here does not matter
1539 * in current declare implementation */
1540 rc = mdd_mark_orphan_object(env, obj, handle, true);
1544 rc = mdo_declare_destroy(env, obj, handle);
1548 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1552 return mdd_declare_links_del(env, obj, handle);
1555 /* caller should take a lock before calling */
1556 int mdd_finish_unlink(const struct lu_env *env,
1557 struct mdd_object *obj, struct md_attr *ma,
1558 struct mdd_object *pobj,
1559 const struct lu_name *lname,
1563 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1566 LASSERT(mdd_write_locked(env, obj) != 0);
1568 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1569 /* add new orphan and the object
1570 * will be deleted during mdd_close() */
1571 obj->mod_flags |= DEAD_OBJ;
1572 if (obj->mod_count) {
1573 rc = mdd_orphan_insert(env, obj, th);
1575 CDEBUG(D_HA, "Object "DFID" is inserted into "
1576 "orphan list, open count = %d\n",
1577 PFID(mdd_object_fid(obj)),
1580 CERROR("Object "DFID" fail to be an orphan, "
1581 "open count = %d, maybe cause failed "
1583 PFID(mdd_object_fid(obj)),
1586 /* mark object as an orphan here, not
1587 * before mdd_orphan_insert() as racing
1588 * mdd_la_get() may propagate ORPHAN_OBJ
1589 * causing the asserition */
1590 rc = mdd_mark_orphan_object(env, obj, th, false);
1592 rc = mdo_destroy(env, obj, th);
1594 } else if (!is_dir) {
1595 /* old files may not have link ea; ignore errors */
1596 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1604 * has mdd_write_lock on cobj already, but not on pobj yet
1606 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1607 const struct lu_attr *pattr,
1608 struct mdd_object *cobj,
1609 const struct lu_attr *cattr)
1614 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1619 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1620 struct mdd_object *p, struct mdd_object *c,
1621 const struct lu_name *name, struct md_attr *ma,
1622 struct thandle *handle, int no_name, int is_dir)
1624 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1627 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1628 if (likely(no_name == 0)) {
1629 rc = mdo_declare_index_delete(env, p, name->ln_name,
1636 rc = mdo_declare_ref_del(env, p, handle);
1642 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1643 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1644 la->la_valid = LA_CTIME | LA_MTIME;
1645 rc = mdo_declare_attr_set(env, p, la, handle);
1650 rc = mdo_declare_ref_del(env, c, handle);
1654 rc = mdo_declare_ref_del(env, c, handle);
1658 la->la_valid = LA_CTIME;
1659 rc = mdo_declare_attr_set(env, c, la, handle);
1663 rc = mdd_declare_finish_unlink(env, c, handle);
1667 /* FIXME: need changelog for remove entry */
1668 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1676 * test if a file has an HSM archive
1677 * if HSM attributes are not found in ma update them from
1680 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1681 struct mdd_object *obj,
1686 if (!(ma->ma_valid & MA_HSM)) {
1687 /* no HSM MD provided, read xattr */
1688 struct lu_buf *hsm_buf;
1689 const size_t buflen = sizeof(struct hsm_attrs);
1692 hsm_buf = mdd_buf_get(env, NULL, 0);
1693 lu_buf_alloc(hsm_buf, buflen);
1694 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1695 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1696 lu_buf_free(hsm_buf);
1700 ma->ma_valid |= MA_HSM;
1702 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1708 * Delete name entry and the object.
1709 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1710 * does not exist for this object, and it could only happen during resending
1711 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1712 * is also needed in this case(needed by changelog), so we have to add another
1713 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1714 * the ENOENT failure should be able to be fixed by redo mechanism.
1716 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1717 struct md_object *cobj, const struct lu_name *lname,
1718 struct md_attr *ma, int no_name)
1720 const char *name = lname->ln_name;
1721 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1722 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1723 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1724 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1725 struct mdd_object *mdd_cobj = NULL;
1726 struct mdd_device *mdd = mdo2mdd(pobj);
1727 struct thandle *handle;
1728 int rc, is_dir = 0, cl_flags = 0;
1731 /* let shutdown to start */
1732 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1734 /* cobj == NULL means only delete name entry */
1735 if (likely(cobj != NULL)) {
1736 mdd_cobj = md2mdd_obj(cobj);
1737 if (mdd_object_exists(mdd_cobj) == 0)
1741 rc = mdd_la_get(env, mdd_pobj, pattr);
1745 if (likely(mdd_cobj != NULL)) {
1747 rc = mdd_la_get(env, mdd_cobj, cattr);
1751 is_dir = S_ISDIR(cattr->la_mode);
1752 /* search for an existing archive.
1753 * we should check ahead as the object
1754 * can be destroyed in this transaction */
1755 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1756 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1759 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1763 handle = mdd_trans_create(env, mdd);
1765 RETURN(PTR_ERR(handle));
1767 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1768 lname, ma, handle, no_name, is_dir);
1772 rc = mdd_trans_start(env, mdd, handle);
1776 if (likely(mdd_cobj != NULL))
1777 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1779 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1780 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1785 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1786 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1787 GOTO(cleanup, rc = 0);
1789 if (likely(mdd_cobj != NULL)) {
1790 rc = mdo_ref_del(env, mdd_cobj, handle);
1792 __mdd_index_insert_only(env, mdd_pobj,
1793 mdd_object_fid(mdd_cobj),
1794 mdd_object_type(mdd_cobj),
1801 mdo_ref_del(env, mdd_cobj, handle);
1803 /* fetch updated nlink */
1804 rc = mdd_la_get(env, mdd_cobj, cattr);
1809 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1810 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1812 la->la_valid = LA_CTIME | LA_MTIME;
1813 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1817 /* Enough for only unlink the entry */
1818 if (unlikely(mdd_cobj == NULL))
1821 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1822 /* update ctime of an unlinked file only if it is still
1823 * opened or a link still exists */
1824 la->la_valid = LA_CTIME;
1825 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1830 /* XXX: this transfer to ma will be removed with LOD/OSP */
1831 ma->ma_attr = *cattr;
1832 ma->ma_valid |= MA_INODE;
1833 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1837 /* fetch updated nlink */
1838 rc = mdd_la_get(env, mdd_cobj, cattr);
1839 /* if object is removed then we can't get its attrs,
1841 if (rc == -ENOENT) {
1842 cattr->la_nlink = 0;
1846 if (cattr->la_nlink == 0) {
1847 ma->ma_attr = *cattr;
1848 ma->ma_valid |= MA_INODE;
1853 if (likely(mdd_cobj != NULL))
1854 mdd_write_unlock(env, mdd_cobj);
1857 if (cattr->la_nlink == 0)
1858 cl_flags |= CLF_UNLINK_LAST;
1860 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1862 rc = mdd_changelog_ns_store(env, mdd,
1863 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1864 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1865 lname, NULL, handle);
1869 rc = mdd_trans_stop(env, mdd, rc, handle);
1875 * The permission has been checked when obj created, no need check again.
1877 static int mdd_cd_sanity_check(const struct lu_env *env,
1878 struct mdd_object *obj)
1883 if (!obj || mdd_is_dead_obj(obj))
1889 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1890 struct md_object *cobj,
1891 const struct md_op_spec *spec, struct md_attr *ma)
1893 struct mdd_device *mdd = mdo2mdd(cobj);
1894 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1895 struct mdd_object *son = md2mdd_obj(cobj);
1896 struct thandle *handle;
1897 const struct lu_buf *buf;
1898 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1899 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
1903 rc = mdd_cd_sanity_check(env, son);
1907 if (!md_should_create(spec->sp_cr_flags))
1911 * there are following use cases for this function:
1912 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1913 * striping can be specified or not
1916 rc = mdd_la_get(env, son, attr);
1920 /* calling ->ah_make_hint() is used to transfer information from parent */
1921 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1923 handle = mdd_trans_create(env, mdd);
1925 GOTO(out_free, rc = PTR_ERR(handle));
1928 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1929 * Should this be fixed?
1931 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1932 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1933 spec->sp_cr_flags, spec->no_create);
1935 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1936 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1937 spec->u.sp_ea.eadatalen);
1942 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1943 XATTR_NAME_LOV, 0, handle);
1947 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1952 rc = mdd_trans_start(env, mdd, handle);
1956 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1962 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1966 rc = mdd_trans_stop(env, mdd, rc, handle);
1972 static int mdd_declare_object_initialize(const struct lu_env *env,
1973 struct mdd_object *parent,
1974 struct mdd_object *child,
1975 const struct lu_attr *attr,
1976 struct thandle *handle)
1981 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1982 if (!S_ISDIR(attr->la_mode))
1985 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1986 S_IFDIR, dot, handle);
1990 rc = mdo_declare_ref_add(env, child, handle);
1994 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1995 S_IFDIR, dotdot, handle);
2000 static int mdd_object_initialize(const struct lu_env *env,
2001 const struct lu_fid *pfid,
2002 struct mdd_object *child,
2003 struct lu_attr *attr,
2004 struct thandle *handle)
2009 if (S_ISDIR(attr->la_mode)) {
2010 /* Add "." and ".." for newly created dir */
2011 mdo_ref_add(env, child, handle);
2012 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
2013 S_IFDIR, dot, handle);
2015 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2018 mdo_ref_del(env, child, handle);
2025 * This function checks whether it can create a file/dir under the
2026 * directory(@pobj). The directory(@pobj) is not being locked by
2029 * \param[in] env execution environment
2030 * \param[in] pobj the directory to create files
2031 * \param[in] pattr the attributes of the directory
2032 * \param[in] lname the name of the created file/dir
2033 * \param[in] cattr the attributes of the file/dir
2034 * \param[in] spec create specification
2036 * \retval = 0 it is allowed to create file/dir under
2038 * \retval negative error not allowed to create file/dir
2039 * under the directory
2041 static int mdd_create_sanity_check(const struct lu_env *env,
2042 struct md_object *pobj,
2043 const struct lu_attr *pattr,
2044 const struct lu_name *lname,
2045 struct lu_attr *cattr,
2046 struct md_op_spec *spec)
2048 struct mdd_thread_info *info = mdd_env_info(env);
2049 struct lu_fid *fid = &info->mdi_fid;
2050 struct mdd_object *obj = md2mdd_obj(pobj);
2051 struct mdd_device *m = mdo2mdd(pobj);
2052 bool check_perm = true;
2057 if (mdd_is_dead_obj(obj))
2061 * In some cases this lookup is not needed - we know before if name
2062 * exists or not because MDT performs lookup for it.
2063 * name length check is done in lookup.
2065 if (spec->sp_cr_lookup) {
2067 * Check if the name already exist, though it will be checked in
2068 * _index_insert also, for avoiding rolling back if exists
2071 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2072 MAY_WRITE | MAY_EXEC);
2074 RETURN(rc ? : -EEXIST);
2076 /* Permission is already being checked in mdd_lookup */
2080 if (S_ISDIR(cattr->la_mode) &&
2081 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2082 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2083 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2085 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2086 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2088 CERROR("%s: invalid lmv_user_md: magic = %x, "
2089 "stripe_offset = %d, stripe_count = %u: "
2090 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2091 le32_to_cpu(lum->lum_magic),
2092 (int)le32_to_cpu(lum->lum_stripe_offset),
2093 le32_to_cpu(lum->lum_stripe_count), rc);
2098 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2103 if (pattr->la_mode & S_ISGID) {
2104 struct lu_ucred *uc = lu_ucred(env);
2106 cattr->la_gid = pattr->la_gid;
2108 /* Directories are special, and always inherit S_ISGID */
2109 if (S_ISDIR(cattr->la_mode)) {
2110 cattr->la_mode |= S_ISGID;
2111 cattr->la_valid |= LA_MODE;
2112 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2113 == (S_ISGID | S_IXGRP) &&
2114 !lustre_in_group_p(uc,
2115 (cattr->la_valid & LA_GID) ?
2116 cattr->la_gid : pattr->la_gid) &&
2117 !md_capable(uc, CAP_FSETID)) {
2118 cattr->la_mode &= ~S_ISGID;
2119 cattr->la_valid |= LA_MODE;
2123 /* Inherit project ID from parent directory */
2124 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2125 cattr->la_projid = pattr->la_projid;
2126 if (S_ISDIR(cattr->la_mode)) {
2127 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2128 cattr->la_valid |= LA_FLAGS;
2130 cattr->la_valid |= LA_PROJID;
2133 rc = mdd_name_check(env, m, lname);
2137 switch (cattr->la_mode & S_IFMT) {
2139 unsigned int symlen = spec->u.sp_symname.ln_namelen + 1;
2141 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2142 RETURN(-ENAMETOOLONG);
2161 static int mdd_declare_create_object(const struct lu_env *env,
2162 struct mdd_device *mdd,
2163 struct mdd_object *p, struct mdd_object *c,
2164 struct lu_attr *attr,
2165 struct thandle *handle,
2166 const struct md_op_spec *spec,
2167 struct lu_buf *def_acl_buf,
2168 struct lu_buf *acl_buf,
2169 struct lu_buf *hsm_buf,
2170 struct dt_allocation_hint *hint)
2172 const struct lu_buf *buf;
2175 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2176 /* ldiskfs OSD needs this information for credit allocation */
2178 hint->dah_acl_len = def_acl_buf->lb_len;
2180 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2185 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2186 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2187 /* if dir, then can inherit default ACl */
2188 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2189 XATTR_NAME_ACL_DEFAULT,
2195 if (acl_buf && acl_buf->lb_len > 0) {
2196 rc = mdo_declare_attr_set(env, c, attr, handle);
2200 rc = mdo_declare_xattr_set(env, c, acl_buf,
2201 XATTR_NAME_ACL_ACCESS, 0, handle);
2206 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2210 /* replay case, create LOV EA from client data */
2211 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2212 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2213 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2214 spec->u.sp_ea.eadatalen);
2215 rc = mdo_declare_xattr_set(env, c, buf,
2216 S_ISDIR(attr->la_mode) ?
2217 XATTR_NAME_LMV : XATTR_NAME_LOV,
2218 LU_XATTR_CREATE, handle);
2222 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2223 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2231 if (S_ISLNK(attr->la_mode)) {
2232 const char *target_name = spec->u.sp_symname.ln_name;
2233 int sym_len = spec->u.sp_symname.ln_namelen;
2234 const struct lu_buf *buf;
2236 buf = mdd_buf_get_const(env, target_name, sym_len);
2237 rc = dt_declare_record_write(env, mdd_object_child(c),
2243 if (spec->sp_cr_file_secctx_name != NULL) {
2244 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2245 spec->sp_cr_file_secctx_size);
2246 rc = mdo_declare_xattr_set(env, c, buf,
2247 spec->sp_cr_file_secctx_name, 0,
2253 if (spec->sp_cr_file_encctx != NULL) {
2254 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2255 spec->sp_cr_file_encctx_size);
2256 rc = mdo_declare_xattr_set(env, c, buf,
2257 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2266 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2267 struct mdd_object *p, struct mdd_object *c,
2268 const struct lu_name *name,
2269 struct lu_attr *attr,
2270 struct thandle *handle,
2271 const struct md_op_spec *spec,
2272 struct linkea_data *ldata,
2273 struct lu_buf *def_acl_buf,
2274 struct lu_buf *acl_buf,
2275 struct lu_buf *hsm_buf,
2276 struct dt_allocation_hint *hint)
2280 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2281 def_acl_buf, acl_buf, hsm_buf, hint);
2285 if (S_ISDIR(attr->la_mode)) {
2286 rc = mdo_declare_ref_add(env, p, handle);
2291 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2292 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2296 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2297 enum changelog_rec_type type;
2299 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2300 attr->la_mode, name->ln_name,
2305 rc = mdd_declare_links_add(env, c, handle, ldata);
2310 la->la_valid = LA_CTIME | LA_MTIME;
2311 rc = mdo_declare_attr_set(env, p, la, handle);
2315 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2316 S_ISREG(attr->la_mode) ? CL_CREATE :
2317 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2319 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2328 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2329 struct lu_attr *la, struct lu_buf *def_acl_buf,
2330 struct lu_buf *acl_buf)
2336 if (S_ISLNK(la->la_mode)) {
2337 acl_buf->lb_len = 0;
2338 def_acl_buf->lb_len = 0;
2342 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2343 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2344 XATTR_NAME_ACL_DEFAULT);
2345 mdd_read_unlock(env, pobj);
2347 /* ACL buffer size is not enough, need realloc */
2348 if (rc > acl_buf->lb_len)
2351 /* If there are default ACL, fix mode/ACL by default ACL */
2352 def_acl_buf->lb_len = rc;
2353 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2354 acl_buf->lb_len = rc;
2355 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2358 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2359 /* If there are no default ACL, fix mode by mask */
2360 struct lu_ucred *uc = lu_ucred(env);
2362 /* The create triggered by MDT internal events, such as
2363 * LFSCK reset, will not contain valid "uc". */
2364 if (unlikely(uc != NULL))
2365 la->la_mode &= ~uc->uc_umask;
2367 acl_buf->lb_len = 0;
2368 def_acl_buf->lb_len = 0;
2375 * Create a metadata object and initialize it, set acl, xattr.
2377 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2378 struct mdd_object *son, struct lu_attr *attr,
2379 struct md_op_spec *spec, struct lu_buf *acl_buf,
2380 struct lu_buf *def_acl_buf,
2381 struct lu_buf *hsm_buf,
2382 struct dt_allocation_hint *hint,
2383 struct thandle *handle, bool initsecctx)
2385 const struct lu_buf *buf;
2388 mdd_write_lock(env, son, DT_TGT_CHILD);
2389 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2394 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2395 * created in declare phase, they also needs to be added to master
2396 * object as sub-directory entry. So it has to initialize the master
2397 * object, then set dir striped EA.(in mdo_xattr_set) */
2398 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2401 GOTO(err_destroy, rc);
2404 * in case of replay we just set LOVEA provided by the client
2405 * XXX: I think it would be interesting to try "old" way where
2406 * MDT calls this xattr_set(LOV) in a different transaction.
2407 * probably this way we code can be made better.
2410 /* During creation, there are only a few cases we need do xattr_set to
2412 * 1. regular file: see comments above.
2413 * 2. dir: inherit default striping or pool settings from parent.
2414 * 3. create striped directory with provided stripeEA.
2415 * 4. create striped directory because inherit default layout from the
2418 if (spec->no_create ||
2419 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2420 S_ISDIR(attr->la_mode)) {
2421 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2422 spec->u.sp_ea.eadatalen);
2423 rc = mdo_xattr_set(env, son, buf,
2424 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2426 LU_XATTR_CREATE, handle);
2428 GOTO(err_destroy, rc);
2431 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2434 memset(&mh, 0, sizeof(mh));
2435 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2436 mh.mh_arch_id = spec->sp_archive_id;
2437 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2438 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2441 GOTO(err_destroy, rc);
2444 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2445 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2446 S_ISDIR(attr->la_mode)) {
2447 /* set default acl */
2448 rc = mdo_xattr_set(env, son, def_acl_buf,
2449 XATTR_NAME_ACL_DEFAULT, 0,
2452 GOTO(err_destroy, rc);
2454 /* set its own acl */
2455 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2456 rc = mdo_xattr_set(env, son, acl_buf,
2457 XATTR_NAME_ACL_ACCESS,
2460 GOTO(err_destroy, rc);
2464 if (S_ISLNK(attr->la_mode)) {
2465 struct dt_object *dt = mdd_object_child(son);
2466 const char *target_name = spec->u.sp_symname.ln_name;
2467 int sym_len = spec->u.sp_symname.ln_namelen;
2470 buf = mdd_buf_get_const(env, target_name, sym_len);
2471 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2475 GOTO(err_initlized, rc = -EFAULT);
2478 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2479 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2480 spec->sp_cr_file_secctx_size);
2481 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2484 GOTO(err_initlized, rc);
2487 if (spec->sp_cr_file_encctx != NULL) {
2488 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2489 spec->sp_cr_file_encctx_size);
2490 rc = mdo_xattr_set(env, son, buf,
2491 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2494 GOTO(err_initlized, rc);
2498 if (unlikely(rc != 0)) {
2500 if (S_ISDIR(attr->la_mode)) {
2501 /* Drop the reference, no need to delete "."/"..",
2502 * because the object to be destroied directly. */
2503 rc2 = mdo_ref_del(env, son, handle);
2507 rc2 = mdo_ref_del(env, son, handle);
2511 mdo_destroy(env, son, handle);
2514 mdd_write_unlock(env, son);
2518 static int mdd_index_delete(const struct lu_env *env,
2519 struct mdd_object *mdd_pobj,
2520 struct lu_attr *cattr,
2521 const struct lu_name *lname)
2523 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2524 struct thandle *handle;
2528 handle = mdd_trans_create(env, mdd);
2530 RETURN(PTR_ERR(handle));
2532 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2537 if (S_ISDIR(cattr->la_mode)) {
2538 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2543 /* Since this will only be used in the error handler path,
2544 * Let's set the thandle to be local and not mess the transno */
2545 handle->th_local = 1;
2546 rc = mdd_trans_start(env, mdd, handle);
2550 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2551 S_ISDIR(cattr->la_mode), handle);
2555 rc = mdd_trans_stop(env, mdd, rc, handle);
2561 * Create object and insert it into namespace.
2563 * Two operations have to be performed:
2565 * - an allocation of a new object (->do_create()), and
2566 * - an insertion into a parent index (->dio_insert()).
2568 * Due to locking, operation order is not important, when both are
2569 * successful, *but* error handling cases are quite different:
2571 * - if insertion is done first, and following object creation fails,
2572 * insertion has to be rolled back, but this operation might fail
2573 * also leaving us with dangling index entry.
2575 * - if creation is done first, is has to be undone if insertion fails,
2576 * leaving us with leaked space, which is not good but not fatal.
2578 * It seems that creation-first is simplest solution, but it is sub-optimal
2584 * case, because second mkdir is bound to create object, only to
2585 * destroy it immediately.
2587 * To avoid this follow local file systems that do double lookup:
2589 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2590 * 1. create (mdd_create_object_internal())
2591 * 2. insert (__mdd_index_insert(), lookup again)
2593 * \param[in] pobj parent object
2594 * \param[in] lname name of child being created
2595 * \param[in,out] child child object being created
2596 * \param[in] spec additional create parameters
2597 * \param[in] ma attributes for new child object
2599 * \retval 0 on success
2600 * \retval negative errno on failure
2602 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2603 const struct lu_name *lname, struct md_object *child,
2604 struct md_op_spec *spec, struct md_attr *ma)
2606 struct mdd_thread_info *info = mdd_env_info(env);
2607 struct lu_attr *la = &info->mdi_la_for_fix;
2608 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2609 struct mdd_object *son = md2mdd_obj(child);
2610 struct mdd_device *mdd = mdo2mdd(pobj);
2611 struct lu_attr *attr = &ma->ma_attr;
2612 struct thandle *handle;
2613 struct lu_attr *pattr = &info->mdi_pattr;
2614 struct lu_buf acl_buf;
2615 struct lu_buf def_acl_buf;
2616 struct lu_buf hsm_buf;
2617 struct linkea_data *ldata = &info->mdi_link_data;
2618 const char *name = lname->ln_name;
2619 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2620 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2625 rc = mdd_la_get(env, mdd_pobj, pattr);
2629 /* Sanity checks before big job. */
2630 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2634 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2635 GOTO(out_free, rc = -EINPROGRESS);
2637 handle = mdd_trans_create(env, mdd);
2639 GOTO(out_free, rc = PTR_ERR(handle));
2642 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
2643 if (!acl_buf.lb_buf)
2644 GOTO(out_stop, rc = -ENOMEM);
2646 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
2647 if (!def_acl_buf.lb_buf)
2648 GOTO(out_stop, rc = -ENOMEM);
2650 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2651 if (unlikely(rc == -ERANGE &&
2652 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2653 /* use maximum-sized xattr buffer for too-big default ACL */
2654 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2656 goto use_bigger_buffer;
2661 if (S_ISDIR(attr->la_mode)) {
2662 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2665 * migrate may create 1-stripe directory, so lod_ah_init()
2666 * doesn't adjust stripe count from lmu.
2668 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2669 lmu->lum_stripe_count = 0;
2672 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2674 memset(ldata, 0, sizeof(*ldata));
2675 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2676 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2679 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2680 &tfid, lname, 1, 0, ldata);
2682 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2683 mdd_object_fid(mdd_pobj),
2684 lname, 1, 0, ldata);
2687 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2688 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2690 memset(&hsm_buf, 0, sizeof(hsm_buf));
2691 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2692 if (hsm_buf.lb_buf == NULL)
2693 GOTO(out_stop, rc = -ENOMEM);
2696 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2697 handle, spec, ldata, &def_acl_buf, &acl_buf,
2702 rc = mdd_trans_start(env, mdd, handle);
2706 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2707 &def_acl_buf, &hsm_buf, hint, handle, true);
2711 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2712 mdd_write_lock(env, son, DT_TGT_CHILD);
2713 son->mod_flags |= VOLATILE_OBJ;
2714 rc = mdd_orphan_insert(env, son, handle);
2715 GOTO(out_volatile, rc);
2717 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2718 attr->la_mode, name, handle);
2720 GOTO(err_created, rc);
2722 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2725 /* update parent directory mtime/ctime */
2727 la->la_valid = LA_CTIME | LA_MTIME;
2728 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2730 GOTO(err_insert, rc);
2736 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2737 rc2 = mdd_orphan_delete(env, son, handle);
2739 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2740 S_ISDIR(attr->la_mode),
2746 mdd_write_lock(env, son, DT_TGT_CHILD);
2747 if (S_ISDIR(attr->la_mode)) {
2748 /* Drop the reference, no need to delete "."/"..",
2749 * because the object is to be destroyed directly. */
2750 rc2 = mdo_ref_del(env, son, handle);
2752 mdd_write_unlock(env, son);
2757 /* For volatile files drop one link immediately, since there is
2758 * no filename in the namespace, and save any error returned. */
2759 rc2 = mdo_ref_del(env, son, handle);
2761 mdd_write_unlock(env, son);
2762 if (unlikely(rc == 0))
2767 /* Don't destroy the volatile object on success */
2768 if (likely(rc != 0))
2769 mdo_destroy(env, son, handle);
2770 mdd_write_unlock(env, son);
2773 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2774 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2775 rc = mdd_changelog_ns_store(env, mdd,
2776 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2777 S_ISREG(attr->la_mode) ? CL_CREATE :
2778 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2779 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2780 lname, NULL, handle);
2782 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2784 /* If creation fails, it is most likely due to the remote update
2785 * failure, because local transaction will mostly succeed at
2786 * this stage. There is no easy way to rollback all of previous
2787 * updates, so let's remove the object from namespace, and
2788 * LFSCK should handle the orphan object. */
2789 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2790 mdd_index_delete(env, mdd_pobj, attr, lname);
2794 if (is_vmalloc_addr(ldata->ld_buf))
2795 /* if we vmalloced a large buffer drop it */
2796 lu_buf_free(ldata->ld_buf);
2798 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2799 lu_buf_free(&hsm_buf);
2801 /* The child object shouldn't be cached anymore */
2803 set_bit(LU_OBJECT_HEARD_BANSHEE,
2804 &child->mo_lu.lo_header->loh_flags);
2808 /* has not mdd_write{read}_lock on any obj yet. */
2809 static int mdd_rename_sanity_check(const struct lu_env *env,
2810 struct mdd_object *src_pobj,
2811 const struct lu_attr *pattr,
2812 struct mdd_object *tgt_pobj,
2813 const struct lu_attr *tpattr,
2814 struct mdd_object *sobj,
2815 const struct lu_attr *cattr,
2816 struct mdd_object *tobj,
2817 const struct lu_attr *tattr)
2822 /* XXX: when get here, sobj must NOT be NULL,
2823 * the other case has been processed in cld_rename
2824 * before mdd_rename and enable MDS_PERM_BYPASS. */
2828 * If we are using project inheritance, we only allow renames
2829 * into our tree when the project IDs are the same; otherwise
2830 * tree quota mechanism would be circumvented.
2832 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2833 tpattr->la_projid != cattr->la_projid) ||
2834 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2835 (pattr->la_projid != tpattr->la_projid)))
2838 /* we prevent an encrypted file from being renamed
2839 * into an unencrypted dir
2841 if ((pattr->la_valid & LA_FLAGS &&
2842 pattr->la_flags & LUSTRE_ENCRYPT_FL) &&
2843 !(tpattr->la_valid & LA_FLAGS &&
2844 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
2847 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2851 /* XXX: when get here, "tobj == NULL" means tobj must
2852 * NOT exist (neither on remote MDS, such case has been
2853 * processed in cld_rename before mdd_rename and enable
2855 * So check may_create, but not check may_unlink. */
2857 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2858 (src_pobj != tgt_pobj));
2860 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2861 (src_pobj != tgt_pobj), 1);
2863 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2864 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2869 static int mdd_declare_rename(const struct lu_env *env,
2870 struct mdd_device *mdd,
2871 struct mdd_object *mdd_spobj,
2872 struct mdd_object *mdd_tpobj,
2873 struct mdd_object *mdd_sobj,
2874 struct mdd_object *mdd_tobj,
2875 const struct lu_name *sname,
2876 const struct lu_name *tname,
2878 struct linkea_data *ldata,
2879 struct thandle *handle)
2881 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2884 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2885 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2891 /* name from source dir */
2892 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2896 /* .. from source child */
2897 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2898 /* source child can be directory, count by source dir's nlink */
2899 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2902 if (mdd_spobj != mdd_tpobj) {
2903 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2908 rc = mdo_declare_index_insert(env, mdd_sobj,
2909 mdd_object_fid(mdd_tpobj),
2910 S_IFDIR, dotdot, handle);
2915 /* new target child can be directory,
2916 * counted by target dir's nlink */
2917 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2922 la->la_valid = LA_CTIME | LA_MTIME;
2923 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2927 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2931 la->la_valid = LA_CTIME;
2932 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2936 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2941 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2942 mdd_object_type(mdd_sobj),
2943 tname->ln_name, handle);
2947 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2948 /* delete target child in target parent directory */
2949 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2954 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2958 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2959 /* target child can be directory,
2960 * delete "." reference in target child directory */
2961 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2965 /* delete ".." reference in target parent directory */
2966 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2971 la->la_valid = LA_CTIME;
2972 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2976 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2981 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2989 static int mdd_migrate_object(const struct lu_env *env,
2990 struct mdd_object *spobj,
2991 struct mdd_object *tpobj,
2992 struct mdd_object *sobj,
2993 struct mdd_object *tobj,
2994 const struct lu_name *sname,
2995 const struct lu_name *tname,
2996 struct md_op_spec *spec,
2997 struct md_attr *ma);
2999 /* src object can be remote that is why we use only fid and type of object */
3000 static int mdd_rename(const struct lu_env *env,
3001 struct md_object *src_pobj, struct md_object *tgt_pobj,
3002 const struct lu_fid *lf, const struct lu_name *lsname,
3003 struct md_object *tobj, const struct lu_name *ltname,
3006 const char *sname = lsname->ln_name;
3007 const char *tname = ltname->ln_name;
3008 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3009 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3010 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
3011 struct mdd_device *mdd = mdo2mdd(src_pobj);
3012 struct mdd_object *mdd_sobj = NULL; /* source object */
3013 struct mdd_object *mdd_tobj = NULL;
3014 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
3015 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3016 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3017 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3018 struct thandle *handle;
3019 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3020 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3021 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3024 bool tobj_locked = 0;
3025 unsigned cl_flags = 0;
3029 /* let unlink to complete and commit */
3030 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3033 mdd_tobj = md2mdd_obj(tobj);
3035 mdd_sobj = mdd_object_find(env, mdd, lf);
3036 if (IS_ERR(mdd_sobj))
3037 RETURN(PTR_ERR(mdd_sobj));
3039 rc = mdd_la_get(env, mdd_sobj, cattr);
3041 GOTO(out_pending, rc);
3043 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3044 * hard links, and target doesn't exist.
3046 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3047 cattr->la_nlink == 1 && !tobj) {
3048 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3049 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3052 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3055 GOTO(out_pending, rc);
3057 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3058 if (IS_ERR(mdd_tobj))
3059 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3061 memset(spec, 0, sizeof(*spec));
3062 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3063 mdd_tobj, lsname, ltname, spec, ma);
3064 mdd_object_put(env, mdd_tobj);
3065 GOTO(out_pending, rc);
3068 rc = mdd_la_get(env, mdd_spobj, pattr);
3070 GOTO(out_pending, rc);
3073 rc = mdd_la_get(env, mdd_tobj, tattr);
3075 GOTO(out_pending, rc);
3076 /* search for an existing archive.
3077 * we should check ahead as the object
3078 * can be destroyed in this transaction */
3079 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3080 cl_flags |= CLF_RENAME_LAST_EXISTS;
3083 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3085 GOTO(out_pending, rc);
3087 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3088 mdd_sobj, cattr, mdd_tobj, tattr);
3090 GOTO(out_pending, rc);
3092 rc = mdd_name_check(env, mdd, ltname);
3094 GOTO(out_pending, rc);
3096 handle = mdd_trans_create(env, mdd);
3098 GOTO(out_pending, rc = PTR_ERR(handle));
3100 memset(ldata, 0, sizeof(*ldata));
3101 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3102 lsname, mdd_object_fid(mdd_tpobj), ltname,
3107 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3108 mdd_tobj, lsname, ltname, ma, ldata, handle);
3112 rc = mdd_trans_start(env, mdd, handle);
3116 is_dir = S_ISDIR(cattr->la_mode);
3118 /* Remove source name from source directory */
3119 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3123 /* "mv dir1 dir2" needs "dir1/.." link update */
3124 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3125 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3127 GOTO(fixup_spobj2, rc);
3129 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3132 GOTO(fixup_spobj, rc);
3135 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3136 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3138 /* tname might been renamed to something else */
3139 GOTO(fixup_spobj, rc);
3142 /* Insert new fid with target name into target dir */
3143 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3146 GOTO(fixup_tpobj, rc);
3148 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3149 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3151 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3152 la->la_valid = LA_CTIME;
3153 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3155 GOTO(fixup_tpobj, rc);
3157 /* Update the linkEA for the source object */
3158 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3159 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3160 lsname, mdd_object_fid(mdd_tpobj), ltname,
3161 handle, ldata, 0, 0);
3163 /* Old files might not have EA entry */
3164 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3165 lsname, handle, NULL, 0);
3166 mdd_write_unlock(env, mdd_sobj);
3167 /* We don't fail the transaction if the link ea can't be
3168 updated -- fid2path will use alternate lookup method. */
3171 /* Remove old target object
3172 * For tobj is remote case cmm layer has processed
3173 * and set tobj to NULL then. So when tobj is NOT NULL,
3174 * it must be local one.
3176 if (tobj && mdd_object_exists(mdd_tobj)) {
3177 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3179 if (mdd_is_dead_obj(mdd_tobj)) {
3180 /* shld not be dead, something is wrong */
3181 CERROR("tobj is dead, something is wrong\n");
3185 mdo_ref_del(env, mdd_tobj, handle);
3187 /* Remove dot reference. */
3188 if (S_ISDIR(tattr->la_mode))
3189 mdo_ref_del(env, mdd_tobj, handle);
3192 /* fetch updated nlink */
3193 rc = mdd_la_get(env, mdd_tobj, tattr);
3195 CERROR("%s: Failed to get nlink for tobj "
3197 mdd2obd_dev(mdd)->obd_name,
3198 PFID(tpobj_fid), rc);
3199 GOTO(fixup_tpobj, rc);
3202 la->la_valid = LA_CTIME;
3203 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3205 CERROR("%s: Failed to set ctime for tobj "
3207 mdd2obd_dev(mdd)->obd_name,
3208 PFID(tpobj_fid), rc);
3209 GOTO(fixup_tpobj, rc);
3212 /* XXX: this transfer to ma will be removed with LOD/OSP */
3213 ma->ma_attr = *tattr;
3214 ma->ma_valid |= MA_INODE;
3215 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3218 CERROR("%s: Failed to unlink tobj "
3220 mdd2obd_dev(mdd)->obd_name,
3221 PFID(tpobj_fid), rc);
3222 GOTO(fixup_tpobj, rc);
3225 /* fetch updated nlink */
3226 rc = mdd_la_get(env, mdd_tobj, tattr);
3227 if (rc == -ENOENT) {
3228 /* the object got removed, let's
3229 * return the latest known attributes */
3230 tattr->la_nlink = 0;
3232 } else if (rc != 0) {
3233 CERROR("%s: Failed to get nlink for tobj "
3235 mdd2obd_dev(mdd)->obd_name,
3236 PFID(tpobj_fid), rc);
3237 GOTO(fixup_tpobj, rc);
3239 /* XXX: this transfer to ma will be removed with LOD/OSP */
3240 ma->ma_attr = *tattr;
3241 ma->ma_valid |= MA_INODE;
3243 if (tattr->la_nlink == 0)
3244 cl_flags |= CLF_RENAME_LAST;
3246 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3249 la->la_valid = LA_CTIME | LA_MTIME;
3250 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3252 GOTO(fixup_tpobj, rc);
3254 if (mdd_spobj != mdd_tpobj) {
3255 la->la_valid = LA_CTIME | LA_MTIME;
3256 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3258 GOTO(fixup_tpobj, rc);
3265 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3267 CWARN("tp obj fix error %d\n",rc2);
3269 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3270 !mdd_is_dead_obj(mdd_tobj)) {
3272 mdo_ref_add(env, mdd_tobj, handle);
3274 mdo_ref_add(env, mdd_tobj, handle);
3277 rc2 = __mdd_index_insert(env, mdd_tpobj,
3278 mdd_object_fid(mdd_tobj),
3279 mdd_object_type(mdd_tobj),
3282 CWARN("tp obj fix error: rc = %d\n", rc2);
3287 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3288 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3290 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3291 mdd2obd_dev(mdd)->obd_name, rc2);
3294 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3297 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3298 mdd2obd_dev(mdd)->obd_name, rc2);
3303 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3304 mdd_object_type(mdd_sobj), sname,
3307 CWARN("sp obj fix error: rc = %d\n", rc2);
3312 mdd_write_unlock(env, mdd_tobj);
3315 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3316 mdd_tobj, tpobj_fid, lf, spobj_fid,
3317 ltname, lsname, handle);
3320 rc = mdd_trans_stop(env, mdd, rc, handle);
3323 mdd_object_put(env, mdd_sobj);
3328 * Check whether we should migrate the file/dir
3330 * < 0 permission check failed or other error.
3331 * = 0 the file can be migrated.
3333 static int mdd_migrate_sanity_check(const struct lu_env *env,
3334 struct mdd_device *mdd,
3335 struct mdd_object *spobj,
3336 struct mdd_object *tpobj,
3337 struct mdd_object *sobj,
3338 struct mdd_object *tobj,
3339 const struct lu_attr *spattr,
3340 const struct lu_attr *tpattr,
3341 const struct lu_attr *attr)
3347 if (!mdd_object_remote(sobj)) {
3348 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3349 if (sobj->mod_count > 0) {
3350 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3351 mdd_obj_dev_name(sobj),
3352 PFID(mdd_object_fid(sobj)),
3354 mdd_read_unlock(env, sobj);
3357 mdd_read_unlock(env, sobj);
3360 if (mdd_object_exists(tobj))
3363 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3367 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3372 struct mdd_xattr_entry {
3373 struct list_head mxe_linkage;
3375 struct lu_buf mxe_buf;
3379 struct lu_buf mx_namebuf;
3380 struct list_head mx_list;
3383 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3385 INIT_LIST_HEAD(&xattrs->mx_list);
3386 xattrs->mx_namebuf.lb_buf = NULL;
3387 xattrs->mx_namebuf.lb_len = 0;
3390 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3392 struct mdd_xattr_entry *entry;
3393 struct mdd_xattr_entry *tmp;
3395 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3396 lu_buf_free(&entry->mxe_buf);
3397 list_del(&entry->mxe_linkage);
3398 OBD_FREE_PTR(entry);
3401 lu_buf_free(&xattrs->mx_namebuf);
3404 /* read xattrs into buf, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is
3407 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3408 struct mdd_xattrs *xattrs,
3409 struct mdd_object *sobj,
3412 struct mdd_xattr_entry *entry;
3422 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3423 if (list_xsize == -ENODATA)
3429 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3430 if (xattrs->mx_namebuf.lb_buf == NULL)
3433 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3439 xname = xattrs->mx_namebuf.lb_buf;
3440 for (; rem > 0; xname += xlen, rem -= xlen) {
3441 xlen = strnlen(xname, rem - 1) + 1;
3442 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3443 strcmp(XATTR_NAME_LMV, xname) == 0)
3447 strcmp(XATTR_NAME_LINK, xname) == 0)
3450 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3451 if (xsize == -ENODATA)
3454 GOTO(fini, rc = xsize);
3456 OBD_ALLOC_PTR(entry);
3458 GOTO(fini, rc = -ENOMEM);
3460 lu_buf_alloc(&entry->mxe_buf, xsize);
3461 if (!entry->mxe_buf.lb_buf) {
3462 OBD_FREE_PTR(entry);
3463 GOTO(fini, rc = -ENOMEM);
3466 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3468 lu_buf_free(&entry->mxe_buf);
3469 OBD_FREE_PTR(entry);
3475 entry->mxe_name = xname;
3476 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3481 mdd_xattrs_fini(xattrs);
3485 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3486 struct mdd_object *obj,
3487 const struct lu_buf *buf,
3489 int fl, struct thandle *handle);
3491 static int mdd_foreach_xattr(const struct lu_env *env,
3492 struct mdd_object *tobj,
3493 struct mdd_xattrs *xattrs,
3494 struct thandle *handle,
3497 struct mdd_xattr_entry *entry;
3500 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
3501 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
3509 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3510 struct mdd_object *sobj,
3511 struct mdd_object *tobj,
3512 const struct lu_name *sname,
3513 const struct lu_fid *sfid,
3514 const struct lu_name *lname,
3515 const struct lu_fid *fid,
3517 struct thandle *handle);
3519 static int mdd_declare_update_link(const struct lu_env *env,
3520 struct mdd_object *sobj,
3521 struct mdd_object *tobj,
3522 const struct lu_name *tname,
3523 const struct lu_fid *tpfid,
3524 const struct lu_name *lname,
3525 const struct lu_fid *fid,
3527 struct thandle *handle)
3529 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3530 struct mdd_object *pobj;
3534 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3535 !strcmp(tname->ln_name, lname->ln_name))
3538 pobj = mdd_object_find(env, mdd, fid);
3540 return PTR_ERR(pobj);
3543 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3545 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3546 mdd_object_type(sobj),
3547 lname->ln_name, handle);
3548 mdd_object_put(env, pobj);
3552 rc = mdo_declare_ref_add(env, tobj, handle);
3556 rc = mdo_declare_ref_del(env, sobj, handle);
3560 static int mdd_update_link(const struct lu_env *env,
3561 struct mdd_object *sobj,
3562 struct mdd_object *tobj,
3563 const struct lu_name *tname,
3564 const struct lu_fid *tpfid,
3565 const struct lu_name *lname,
3566 const struct lu_fid *fid,
3568 struct thandle *handle)
3570 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3571 struct mdd_object *pobj;
3577 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3578 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3581 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3582 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3584 pobj = mdd_object_find(env, mdd, fid);
3586 CWARN("%s: cannot find obj "DFID": %ld\n",
3587 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3588 RETURN(PTR_ERR(pobj));
3591 if (!mdd_object_exists(pobj)) {
3592 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3593 mdd_object_put(env, pobj);
3597 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3598 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3600 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3601 mdd_object_type(sobj),
3602 lname->ln_name, handle);
3603 mdd_write_unlock(env, pobj);
3604 mdd_object_put(env, pobj);
3608 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3609 rc = mdo_ref_add(env, tobj, handle);
3610 mdd_write_unlock(env, tobj);
3614 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3615 rc = mdo_ref_del(env, sobj, handle);
3616 mdd_write_unlock(env, sobj);
3621 static inline int mdd_fld_lookup(const struct lu_env *env,
3622 struct mdd_device *mdd,
3623 const struct lu_fid *fid,
3626 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3627 struct seq_server_site *ss;
3630 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3632 range->lsr_flags = LU_SEQ_RANGE_MDT;
3633 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3637 *mdt_index = range->lsr_index;
3642 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3643 struct mdd_object *sobj,
3644 struct mdd_object *tobj,
3645 const struct lu_name *tname,
3646 const struct lu_fid *tpfid,
3647 const struct lu_name *lname,
3648 const struct lu_fid *fid,
3650 struct thandle *handle)
3652 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3653 __u32 source_mdt_index = *(__u32 *)opaque;
3654 __u32 link_mdt_index;
3660 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3661 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3664 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3668 RETURN(link_mdt_index == source_mdt_index);
3671 static int mdd_iterate_linkea(const struct lu_env *env,
3672 struct mdd_object *sobj,
3673 struct mdd_object *tobj,
3674 const struct lu_name *tname,
3675 const struct lu_fid *tpfid,
3676 struct linkea_data *ldata,
3678 struct thandle *handle,
3681 struct mdd_thread_info *info = mdd_env_info(env);
3682 char *filename = info->mdi_name;
3683 struct lu_name lname;
3690 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3691 linkea_next_entry(ldata)) {
3692 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3695 /* Note: lname might miss \0 at the end */
3696 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3697 lname.ln_namelen, lname.ln_name);
3698 lname.ln_name = filename;
3700 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3702 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3710 * Prepare linkea, and check whether file needs migrate: if source still has
3711 * link on source MDT, no need to migrate, just update namespace on source and
3714 * \retval 0 do migrate
3715 * \retval 1 don't migrate
3716 * \retval -errno on failure
3718 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3719 struct mdd_device *mdd,
3720 struct mdd_object *spobj,
3721 struct mdd_object *tpobj,
3722 struct mdd_object *sobj,
3723 const struct lu_name *sname,
3724 const struct lu_name *tname,
3725 const struct lu_attr *attr,
3726 struct linkea_data *ldata)
3728 __u32 source_mdt_index;
3733 memset(ldata, 0, sizeof(*ldata));
3734 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3735 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3740 * Then it will check if the file should be migrated. If the file has
3741 * mulitple links, we only need migrate the file if all of its entries
3742 * has been migrated to the remote MDT.
3744 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3747 /* If there are still links locally, don't migrate this file */
3748 LASSERT(ldata->ld_leh != NULL);
3751 * If linkEA is overflow, it means there are some unknown name entries
3752 * under unknown parents, which will prevent the migration.
3754 if (unlikely(ldata->ld_leh->leh_overflow_time))
3757 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3761 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3762 ldata, &source_mdt_index, NULL,
3763 mdd_is_link_on_source_mdt);
3767 static int mdd_declare_migrate_update(const struct lu_env *env,
3768 struct mdd_object *spobj,
3769 struct mdd_object *tpobj,
3770 struct mdd_object *obj,
3771 const struct lu_name *sname,
3772 const struct lu_name *tname,
3773 struct lu_attr *attr,
3774 struct lu_attr *spattr,
3775 struct lu_attr *tpattr,
3776 struct linkea_data *ldata,
3778 struct thandle *handle)
3780 struct mdd_thread_info *info = mdd_env_info(env);
3781 struct lu_attr *la = &info->mdi_la_for_fix;
3784 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3788 if (S_ISDIR(attr->la_mode)) {
3789 rc = mdo_declare_ref_del(env, spobj, handle);
3794 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3795 attr->la_mode & S_IFMT,
3796 tname->ln_name, handle);
3800 rc = mdd_declare_links_add(env, obj, handle, ldata);
3804 if (S_ISDIR(attr->la_mode)) {
3805 rc = mdo_declare_ref_add(env, tpobj, handle);
3810 la->la_valid = LA_CTIME | LA_MTIME;
3811 rc = mdo_declare_attr_set(env, spobj, la, handle);
3815 if (tpobj != spobj) {
3816 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3824 static int mdd_declare_migrate_create(const struct lu_env *env,
3825 struct mdd_object *spobj,
3826 struct mdd_object *tpobj,
3827 struct mdd_object *sobj,
3828 struct mdd_object *tobj,
3829 const struct lu_name *sname,
3830 const struct lu_name *tname,
3831 struct lu_attr *spattr,
3832 struct lu_attr *tpattr,
3833 struct lu_attr *attr,
3834 struct lu_buf *sbuf,
3835 struct linkea_data *ldata,
3836 struct mdd_xattrs *xattrs,
3838 struct md_op_spec *spec,
3839 struct dt_allocation_hint *hint,
3840 struct thandle *handle)
3842 struct mdd_thread_info *info = mdd_env_info(env);
3843 struct md_layout_change *mlc = &info->mdi_mlc;
3844 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3849 if (S_ISDIR(attr->la_mode)) {
3850 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3852 mlc->mlc_opc = MD_LAYOUT_DETACH;
3853 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3857 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3858 } else if (S_ISLNK(attr->la_mode)) {
3859 spec->u.sp_symname.ln_name = sbuf->lb_buf;
3860 /* don't count NUL */
3861 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
3862 } else if (S_ISREG(attr->la_mode)) {
3863 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3864 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3867 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3869 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3870 tname, attr, handle, spec, ldata, NULL, NULL,
3876 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3877 * createb yet, copy from sobj.
3879 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3880 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3881 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3883 if (S_ISDIR(attr->la_mode)) {
3885 /* if sobj is not striped, fake a 1-stripe LMV */
3886 LASSERT(sizeof(info->mdi_key) >
3887 lmv_mds_md_size(1, LMV_MAGIC_V1));
3888 lmv = (typeof(lmv))info->mdi_key;
3889 memset(lmv, 0, sizeof(*lmv));
3890 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3891 lmv->lmv_stripe_count = cpu_to_le32(1);
3892 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3893 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3894 mdd_object_fid(sobj));
3895 mlc->mlc_buf.lb_buf = lmv;
3896 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3898 mlc->mlc_buf = *sbuf;
3900 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3901 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3906 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
3907 mdo_declare_xattr_set);
3911 if (S_ISREG(attr->la_mode)) {
3912 struct lu_buf fid_buf;
3914 handle->th_complex = 1;
3916 /* target may be remote, update PFID via sobj. */
3917 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3918 fid_buf.lb_len = sizeof(struct lu_fid);
3919 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3924 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3929 if (!S_ISDIR(attr->la_mode)) {
3930 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3931 mdd_object_fid(tpobj), ldata, NULL,
3932 handle, mdd_declare_update_link);
3937 if (!S_ISDIR(attr->la_mode) || lmv) {
3938 rc = mdo_declare_ref_del(env, sobj, handle);
3942 if (S_ISDIR(attr->la_mode)) {
3943 rc = mdo_declare_ref_del(env, sobj, handle);
3948 rc = mdo_declare_destroy(env, sobj, handle);
3953 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3954 attr, spattr, tpattr, ldata, ma,
3960 * migrate dirent from \a spobj to \a tpobj.
3962 static int mdd_migrate_update(const struct lu_env *env,
3963 struct mdd_object *spobj,
3964 struct mdd_object *tpobj,
3965 struct mdd_object *obj,
3966 const struct lu_name *sname,
3967 const struct lu_name *tname,
3968 struct lu_attr *attr,
3969 struct lu_attr *spattr,
3970 struct lu_attr *tpattr,
3971 struct linkea_data *ldata,
3973 struct thandle *handle)
3975 struct mdd_thread_info *info = mdd_env_info(env);
3976 struct lu_attr *la = &info->mdi_la_for_fix;
3981 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3982 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3983 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3985 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3986 S_ISDIR(attr->la_mode), handle);
3990 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3991 attr->la_mode & S_IFMT,
3992 tname->ln_name, handle);
3996 rc = mdd_links_write(env, obj, ldata, handle);
4000 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4001 la->la_valid = LA_CTIME | LA_MTIME;
4002 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4003 rc = mdd_update_time(env, spobj, spattr, la, handle);
4004 mdd_write_unlock(env, spobj);
4008 if (tpobj != spobj) {
4009 la->la_valid = LA_CTIME | LA_MTIME;
4010 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4011 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4012 mdd_write_unlock(env, tpobj);
4021 * Migrate file/dir to target MDT.
4023 * Create target according to \a spec, and then migrate xattrs, if it's
4024 * directory, migrate source stripes to target.
4026 * \param[in] env execution environment
4027 * \param[in] spobj source parent object
4028 * \param[in] tpobj target parent object
4029 * \param[in] sobj source object
4030 * \param[in] tobj target object
4031 * \param[in] lname file name
4032 * \param[in] spattr source parent attributes
4033 * \param[in] tpattr target parent attributes
4034 * \param[in] attr source attributes
4035 * \param[in] sbuf source LMV buf
4036 * \param[in] spec migrate create spec
4037 * \param[in] hint target creation hint
4038 * \param[in] handle tranasction handle
4040 * \retval 0 on success
4041 * \retval -errno on failure
4043 static int mdd_migrate_create(const struct lu_env *env,
4044 struct mdd_object *spobj,
4045 struct mdd_object *tpobj,
4046 struct mdd_object *sobj,
4047 struct mdd_object *tobj,
4048 const struct lu_name *sname,
4049 const struct lu_name *tname,
4050 struct lu_attr *spattr,
4051 struct lu_attr *tpattr,
4052 struct lu_attr *attr,
4053 const struct lu_buf *sbuf,
4054 struct linkea_data *ldata,
4055 struct mdd_xattrs *xattrs,
4057 struct md_op_spec *spec,
4058 struct dt_allocation_hint *hint,
4059 struct thandle *handle)
4066 * migrate sobj stripes to tobj if it's directory:
4067 * 1. detach stripes from sobj.
4068 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4069 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4071 if (S_ISDIR(attr->la_mode)) {
4072 struct mdd_thread_info *info = mdd_env_info(env);
4073 struct md_layout_change *mlc = &info->mdi_mlc;
4075 mlc->mlc_opc = MD_LAYOUT_DETACH;
4077 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4078 rc = mdo_layout_change(env, sobj, mlc, handle);
4079 mdd_write_unlock(env, sobj);
4084 /* don't set nlink from sobj */
4085 attr->la_valid &= ~LA_NLINK;
4087 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4088 hint, handle, false);
4092 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4093 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4094 mdd_write_unlock(env, tobj);
4098 /* for regular file, update OST objects XATTR_NAME_FID */
4099 if (S_ISREG(attr->la_mode)) {
4100 struct lu_buf fid_buf;
4102 /* target may be remote, update PFID via sobj. */
4103 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4104 fid_buf.lb_len = sizeof(struct lu_fid);
4105 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4110 /* delete LOV to avoid deleting OST objs when destroying sobj */
4111 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4112 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4113 mdd_write_unlock(env, sobj);
4114 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4115 if (rc && rc != -ENODATA)
4120 /* update links FID */
4121 if (!S_ISDIR(attr->la_mode)) {
4122 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4123 mdd_object_fid(tpobj), ldata,
4124 NULL, handle, mdd_update_link);
4129 /* don't destroy sobj if it's plain directory */
4130 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4131 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4132 rc = mdo_ref_del(env, sobj, handle);
4134 if (S_ISDIR(attr->la_mode))
4135 rc = mdo_ref_del(env, sobj, handle);
4137 rc = mdo_destroy(env, sobj, handle);
4139 mdd_write_unlock(env, sobj);
4144 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4145 spattr, tpattr, ldata, ma, handle);
4150 /* NB: if user issued different migrate command, we can't adjust it silently
4151 * here, because this command will decide target MDT in subdir migration in
4154 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4155 const struct lmv_mds_md_v1 *lmv,
4156 const struct lmv_user_md_v1 *lum,
4157 const struct lu_name *lname)
4159 __u32 lum_stripe_count = lum->lum_stripe_count;
4160 __u32 lum_hash_type = lum->lum_hash_type &
4161 cpu_to_le32(LMV_HASH_TYPE_MASK);
4162 __u32 lmv_hash_type = lmv->lmv_hash_type &
4163 cpu_to_le32(LMV_HASH_TYPE_MASK);
4165 if (!lmv_is_sane(lmv))
4168 /* if stripe_count unspecified, set to 1 */
4169 if (!lum_stripe_count)
4170 lum_stripe_count = cpu_to_le32(1);
4172 /* TODO: check specific MDTs */
4173 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4174 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4175 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4176 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4177 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4178 le32_to_cpu(lmv->lmv_master_mdt_index),
4179 le32_to_cpu(lmv->lmv_migrate_offset),
4180 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4189 * Internal function to migrate directory or file between MDTs.
4191 * migrate source to target in following steps:
4192 * 1. create target, append source stripes after target's if it's directory,
4193 * migrate xattrs and update fid of source links.
4194 * 2. update namespace: migrate dirent from source parent to target parent,
4195 * update file linkea, and destroy source if it's not needed any more.
4197 * \param[in] env execution environment
4198 * \param[in] spobj source parent object
4199 * \param[in] tpobj target parent object
4200 * \param[in] sobj source object
4201 * \param[in] tobj target object
4202 * \param[in] sname source file name
4203 * \param[in] tname target file name
4204 * \param[in] spec target creation spec
4205 * \param[in] ma used to update \a pobj mtime and ctime
4207 * \retval 0 on success
4208 * \retval -errno on failure
4210 static int mdd_migrate_object(const struct lu_env *env,
4211 struct mdd_object *spobj,
4212 struct mdd_object *tpobj,
4213 struct mdd_object *sobj,
4214 struct mdd_object *tobj,
4215 const struct lu_name *sname,
4216 const struct lu_name *tname,
4217 struct md_op_spec *spec,
4220 struct mdd_thread_info *info = mdd_env_info(env);
4221 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4222 struct lu_attr *spattr = &info->mdi_pattr;
4223 struct lu_attr *tpattr = &info->mdi_tpattr;
4224 struct lu_attr *attr = &info->mdi_cattr;
4225 struct linkea_data *ldata = &info->mdi_link_data;
4226 struct dt_allocation_hint *hint = &info->mdi_hint;
4227 struct lu_buf sbuf = { NULL };
4228 struct mdd_xattrs xattrs;
4229 struct lmv_mds_md_v1 *lmv;
4230 struct thandle *handle;
4235 rc = mdd_la_get(env, sobj, attr);
4239 rc = mdd_la_get(env, spobj, spattr);
4243 rc = mdd_la_get(env, tpobj, tpattr);
4247 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4248 spattr, tpattr, attr);
4252 mdd_xattrs_init(&xattrs);
4254 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4255 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4259 /* if user use default value '0' for stripe_count, we need to
4260 * adjust it to '1' to create a 1-stripe directory.
4262 if (lum->lum_stripe_count == 0)
4263 lum->lum_stripe_count = cpu_to_le32(1);
4265 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4266 if (rc && rc != -ENODATA)
4271 if (!lmv_is_sane(lmv))
4272 GOTO(out, rc = -EBADF);
4273 if (lmv_is_migrating(lmv)) {
4274 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4279 } else if (!S_ISDIR(attr->la_mode)) {
4281 GOTO(out, rc = -EALREADY);
4283 /* update namespace only if @sobj is on MDT where @tpobj is. */
4284 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4285 spec->sp_migrate_nsonly = true;
4287 if (S_ISLNK(attr->la_mode)) {
4288 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4290 GOTO(out, rc = -ENOMEM);
4292 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4295 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4296 mdd2obd_dev(mdd)->obd_name,
4297 PFID(mdd_object_fid(sobj)), rc);
4303 /* linkea needs update upon FID or parent stripe change */
4304 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4305 tname, attr, ldata);
4307 /* update namespace only if @sobj has link on its MDT. */
4308 spec->sp_migrate_nsonly = true;
4312 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4313 * RPCs inside transaction.
4315 if (!spec->sp_migrate_nsonly) {
4316 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, false);
4321 handle = mdd_trans_create(env, mdd);
4323 GOTO(out, rc = PTR_ERR(handle));
4325 if (spec->sp_migrate_nsonly)
4326 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4327 tname, attr, spattr, tpattr,
4330 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4331 sname, tname, spattr, tpattr,
4332 attr, &sbuf, ldata, &xattrs, ma,
4333 spec, hint, handle);
4337 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4342 rc = mdd_trans_start(env, mdd, handle);
4346 if (spec->sp_migrate_nsonly)
4347 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4348 attr, spattr, tpattr, ldata, ma,
4351 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4352 tname, spattr, tpattr, attr, &sbuf,
4353 ldata, &xattrs, ma, spec, hint, handle);
4357 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4358 spec->sp_migrate_nsonly ? sobj : tobj,
4359 mdd_object_fid(spobj), mdd_object_fid(sobj),
4360 mdd_object_fid(tpobj), tname, sname,
4367 rc = mdd_trans_stop(env, mdd, rc, handle);
4369 mdd_xattrs_fini(&xattrs);
4376 * Migrate directory or file between MDTs.
4378 * \param[in] env execution environment
4379 * \param[in] md_pobj parent master object
4380 * \param[in] md_sobj source object
4381 * \param[in] lname file name
4382 * \param[in] md_tobj target object
4383 * \param[in] spec target creation spec
4384 * \param[in] ma used to update \a pobj mtime and ctime
4386 * \retval 0 on success
4387 * \retval -errno on failure
4389 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4390 struct md_object *md_sobj, const struct lu_name *lname,
4391 struct md_object *md_tobj, struct md_op_spec *spec,
4394 struct mdd_thread_info *info = mdd_env_info(env);
4395 struct mdd_device *mdd = mdo2mdd(md_pobj);
4396 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4397 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4398 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4399 struct mdd_object *spobj = NULL;
4400 struct mdd_object *tpobj = NULL;
4401 struct lu_buf pbuf = { NULL };
4402 struct lu_fid *fid = &info->mdi_fid2;
4403 struct lmv_mds_md_v1 *lmv;
4408 /* locate source and target stripe on pobj, which are the real parent */
4409 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4410 if (rc < 0 && rc != -ENODATA)
4417 if (!lmv_is_sane(lmv))
4418 GOTO(out, rc = -EBADF);
4420 /* locate target parent stripe */
4421 /* fail check here to make sure top dir migration succeed. */
4422 if (lmv_is_migrating(lmv) &&
4423 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4424 GOTO(out, rc = -EIO);
4426 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4429 GOTO(out, rc = index);
4431 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4432 tpobj = mdd_object_find(env, mdd, fid);
4434 GOTO(out, rc = PTR_ERR(tpobj));
4436 /* locate source parent stripe */
4437 if (lmv_is_layout_changing(lmv)) {
4438 index = lmv_name_to_stripe_index_old(lmv,
4442 GOTO(out, rc = index);
4444 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4445 spobj = mdd_object_find(env, mdd, fid);
4447 GOTO(out, rc = PTR_ERR(spobj));
4449 /* parent stripe unchanged */
4450 if (spobj == tpobj) {
4451 if (!lmv_is_restriping(lmv))
4452 GOTO(out, rc = -EINVAL);
4453 GOTO(out, rc = -EALREADY);
4457 mdd_object_get(spobj);
4462 mdd_object_get(tpobj);
4463 mdd_object_get(spobj);
4466 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4471 if (!IS_ERR_OR_NULL(spobj))
4472 mdd_object_put(env, spobj);
4473 if (!IS_ERR_OR_NULL(tpobj))
4474 mdd_object_put(env, tpobj);
4480 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4481 struct mdd_object *pobj,
4482 struct mdd_object *obj,
4483 struct mdd_object *stripe,
4484 struct lu_attr *attr,
4485 struct mdd_xattrs *xattrs,
4486 struct md_layout_change *mlc,
4487 struct lu_name *lname,
4488 struct thandle *handle)
4492 mlc->mlc_opc = MD_LAYOUT_DETACH;
4493 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4497 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4498 S_IFDIR, dotdot, handle);
4502 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
4503 mdo_declare_xattr_set);
4507 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4511 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4515 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4519 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4520 attr->la_mode, lname->ln_name, handle);
4524 rc = mdo_declare_ref_del(env, obj, handle);
4528 rc = mdo_declare_ref_del(env, obj, handle);
4532 rc = mdo_declare_destroy(env, obj, handle);
4539 /* transform one-stripe directory to a plain directory */
4540 static int mdd_1sd_collapse(const struct lu_env *env,
4541 struct mdd_object *pobj,
4542 struct mdd_object *obj,
4543 struct mdd_object *stripe,
4544 struct lu_attr *attr,
4545 struct mdd_xattrs *xattrs,
4546 struct md_layout_change *mlc,
4547 struct lu_name *lname,
4548 struct thandle *handle)
4554 /* replace 1-stripe directory with its stripe */
4555 mlc->mlc_opc = MD_LAYOUT_DETACH;
4557 mdd_write_lock(env, obj, DT_SRC_PARENT);
4558 rc = mdo_layout_change(env, obj, mlc, handle);
4559 mdd_write_unlock(env, obj);
4563 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4564 mdd_write_lock(env, obj, DT_SRC_CHILD);
4566 /* insert dotdot to stripe which points to parent */
4567 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4568 S_IFDIR, dotdot, handle);
4572 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
4577 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4581 /* don't set nlink from parent */
4582 attr->la_valid &= ~LA_NLINK;
4584 rc = mdo_attr_set(env, stripe, attr, handle);
4588 /* delete dir name from parent */
4589 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4593 /* insert stripe to parent with dir name */
4594 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4595 attr->la_mode, lname->ln_name, handle);
4599 /* destroy dir obj */
4600 rc = mdo_ref_del(env, obj, handle);
4604 rc = mdo_ref_del(env, obj, handle);
4608 rc = mdo_destroy(env, obj, handle);
4614 mdd_write_unlock(env, obj);
4615 mdd_write_unlock(env, pobj);
4621 * shrink directory stripes after migration/merge
4623 int mdd_dir_layout_shrink(const struct lu_env *env,
4624 struct md_object *md_obj,
4625 struct md_layout_change *mlc)
4627 struct mdd_device *mdd = mdo2mdd(md_obj);
4628 struct mdd_thread_info *info = mdd_env_info(env);
4629 struct mdd_object *obj = md2mdd_obj(md_obj);
4630 struct mdd_object *pobj = NULL;
4631 struct mdd_object *stripe = NULL;
4632 struct lu_attr *attr = &info->mdi_pattr;
4633 struct lu_fid *fid = &info->mdi_fid2;
4634 struct lu_name lname = { NULL };
4635 struct lu_buf lmv_buf = { NULL };
4636 struct mdd_xattrs xattrs;
4637 struct lmv_mds_md_v1 *lmv;
4638 struct lmv_user_md *lmu;
4639 struct thandle *handle;
4644 rc = mdd_la_get(env, obj, attr);
4648 if (!S_ISDIR(attr->la_mode))
4651 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4655 lmv = lmv_buf.lb_buf;
4656 if (!lmv_is_sane(lmv))
4659 lmu = mlc->mlc_buf.lb_buf;
4661 /* adjust the default value '0' to '1' */
4662 if (lmu->lum_stripe_count == 0)
4663 lmu->lum_stripe_count = cpu_to_le32(1);
4665 /* these were checked in MDT */
4666 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4667 le32_to_cpu(lmv->lmv_stripe_count));
4668 LASSERT(!lmv_is_splitting(lmv));
4669 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4671 mdd_xattrs_init(&xattrs);
4673 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4674 * to a plain dir, which will cause FID change and namespace update.
4676 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4677 struct linkea_data *ldata = &info->mdi_link_data;
4678 char *filename = info->mdi_name;
4680 rc = mdd_links_read(env, obj, ldata);
4684 if (ldata->ld_leh->leh_reccount > 1)
4685 GOTO(out, rc = -EINVAL);
4687 linkea_first_entry(ldata);
4689 GOTO(out, rc = -ENODATA);
4691 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4694 /* Note: lname might miss \0 at the end */
4695 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4696 lname.ln_namelen, lname.ln_name);
4697 lname.ln_name = filename;
4699 pobj = mdd_object_find(env, mdd, fid);
4706 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4708 stripe = mdd_object_find(env, mdd, fid);
4709 if (IS_ERR(stripe)) {
4710 mdd_object_put(env, pobj);
4712 GOTO(out, rc = PTR_ERR(stripe));
4715 if (!lmv_is_fixed(lmv))
4716 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true);
4719 handle = mdd_trans_create(env, mdd);
4721 GOTO(out, rc = PTR_ERR(handle));
4723 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4724 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4726 GOTO(stop_trans, rc);
4728 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4729 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
4730 &xattrs, mlc, &lname, handle);
4732 GOTO(stop_trans, rc);
4735 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4738 GOTO(stop_trans, rc);
4740 rc = mdd_trans_start(env, mdd, handle);
4742 GOTO(stop_trans, rc);
4744 mdd_write_lock(env, obj, DT_SRC_PARENT);
4745 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4746 rc = mdo_layout_change(env, obj, mlc, handle);
4747 mdd_write_unlock(env, obj);
4749 GOTO(stop_trans, rc);
4751 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4752 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
4753 mlc, &lname, handle);
4755 GOTO(stop_trans, rc);
4758 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4759 XATTR_NAME_LMV, handle);
4760 GOTO(stop_trans, rc);
4763 rc = mdd_trans_stop(env, mdd, rc, handle);
4765 mdd_xattrs_fini(&xattrs);
4767 mdd_object_put(env, stripe);
4768 mdd_object_put(env, pobj);
4770 lu_buf_free(&lmv_buf);
4774 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4775 struct mdd_device *mdd,
4776 struct mdd_object *pobj,
4777 struct mdd_object *obj,
4778 struct mdd_object *tobj,
4779 struct mdd_xattrs *xattrs,
4780 struct md_layout_change *mlc,
4781 struct dt_allocation_hint *hint,
4782 struct thandle *handle)
4784 struct mdd_thread_info *info = mdd_env_info(env);
4785 const struct lu_name *lname = mlc->mlc_name;
4786 struct lu_attr *la = &info->mdi_la_for_fix;
4787 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4788 struct linkea_data *ldata = &info->mdi_link_data;
4789 struct lmv_mds_md_v1 *lmv;
4793 mlc->mlc_opc = MD_LAYOUT_DETACH;
4794 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4798 memset(ldata, 0, sizeof(*ldata));
4799 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4800 lname, 1, 0, ldata);
4804 count = lum->lum_stripe_count;
4805 lum->lum_stripe_count = 0;
4806 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4808 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4809 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4810 ldata, NULL, NULL, NULL, hint);
4814 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4817 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4819 lmv = (typeof(lmv))info->mdi_key;
4820 memset(lmv, 0, sizeof(*lmv));
4821 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4822 lmv->lmv_stripe_count = cpu_to_le32(1);
4823 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4824 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4826 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4827 mlc->mlc_buf.lb_buf = lmv;
4828 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4829 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4833 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4834 mdo_declare_xattr_set);
4838 lum->lum_stripe_count = count;
4839 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4840 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4844 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4848 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4849 S_IFDIR, lname->ln_name, handle);
4853 la->la_valid = LA_CTIME | LA_MTIME;
4854 rc = mdo_declare_attr_set(env, obj, la, handle);
4858 rc = mdo_declare_attr_set(env, pobj, la, handle);
4862 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4868 * plain directory split:
4869 * 1. create \a tobj as plain directory.
4870 * 2. append \a obj as first stripe of \a tobj.
4871 * 3. migrate xattrs from \a obj to \a tobj.
4872 * 4. split \a tobj to specific stripe count.
4874 static int mdd_dir_split_plain(const struct lu_env *env,
4875 struct mdd_device *mdd,
4876 struct mdd_object *pobj,
4877 struct mdd_object *obj,
4878 struct mdd_object *tobj,
4879 struct mdd_xattrs *xattrs,
4880 struct md_layout_change *mlc,
4881 struct dt_allocation_hint *hint,
4882 struct thandle *handle)
4884 struct mdd_thread_info *info = mdd_env_info(env);
4885 struct lu_attr *pattr = &info->mdi_pattr;
4886 struct lu_attr *la = &info->mdi_la_for_fix;
4887 const struct lu_name *lname = mlc->mlc_name;
4888 struct linkea_data *ldata = &info->mdi_link_data;
4893 /* copy linkea out and set on target later */
4894 rc = mdd_links_read(env, obj, ldata);
4898 mlc->mlc_opc = MD_LAYOUT_DETACH;
4899 rc = mdo_layout_change(env, obj, mlc, handle);
4903 /* don't set nlink from obj */
4904 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4906 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4907 NULL, NULL, NULL, hint, handle, false);
4911 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4915 rc = mdd_links_write(env, tobj, ldata, handle);
4919 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4923 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4924 lname->ln_name, handle);
4928 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4929 la->la_valid = LA_CTIME | LA_MTIME;
4931 mdd_write_lock(env, obj, DT_SRC_CHILD);
4932 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4933 mdd_write_unlock(env, obj);
4937 rc = mdd_la_get(env, pobj, pattr);
4941 la->la_valid = LA_CTIME | LA_MTIME;
4943 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4944 rc = mdd_update_time(env, pobj, pattr, la, handle);
4945 mdd_write_unlock(env, pobj);
4949 /* FID changes, record it as CL_MIGRATE */
4950 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4951 mdd_object_fid(pobj), mdd_object_fid(obj),
4952 mdd_object_fid(pobj), lname, lname, handle);
4956 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4957 struct md_layout_change *mlc)
4959 struct mdd_thread_info *info = mdd_env_info(env);
4960 struct mdd_device *mdd = mdo2mdd(o);
4961 struct mdd_object *obj = md2mdd_obj(o);
4962 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4963 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4964 struct dt_allocation_hint *hint = &info->mdi_hint;
4965 bool is_plain = false;
4966 struct mdd_xattrs xattrs;
4967 struct thandle *handle;
4972 LASSERT(S_ISDIR(mdd_object_type(obj)));
4974 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4980 mdd_xattrs_init(&xattrs);
4982 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true);
4984 handle = mdd_trans_create(env, mdd);
4986 GOTO(out, rc = PTR_ERR(handle));
4989 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
4990 &xattrs, mlc, hint, handle);
4992 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4993 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4995 GOTO(stop_trans, rc);
4997 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5001 GOTO(stop_trans, rc);
5003 rc = mdd_trans_start(env, mdd, handle);
5005 GOTO(stop_trans, rc);
5008 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5011 mdd_write_lock(env, obj, DT_TGT_CHILD);
5012 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
5013 LU_XATTR_CREATE, handle);
5014 mdd_write_unlock(env, obj);
5016 GOTO(stop_trans, rc);
5018 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5019 XATTR_NAME_LMV, handle);
5022 GOTO(stop_trans, rc);
5027 rc = mdd_trans_stop(env, mdd, rc, handle);
5029 mdd_xattrs_fini(&xattrs);
5034 const struct md_dir_operations mdd_dir_ops = {
5035 .mdo_is_subdir = mdd_is_subdir,
5036 .mdo_lookup = mdd_lookup,
5037 .mdo_create = mdd_create,
5038 .mdo_rename = mdd_rename,
5039 .mdo_link = mdd_link,
5040 .mdo_unlink = mdd_unlink,
5041 .mdo_create_data = mdd_create_data,
5042 .mdo_migrate = mdd_migrate,