4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdd/mdd_dir.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@intel.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_mds.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_idmap.h>
47 #include "mdd_internal.h"
49 static const char dot[] = ".";
50 static const char dotdot[] = "..";
52 static struct lu_name lname_dotdot = {
53 .ln_name = (char *) dotdot,
54 .ln_namelen = sizeof(dotdot) - 1,
58 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
60 if (!lu_name_is_valid(ln))
62 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
68 /* Get FID from name and parent */
70 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
71 const struct lu_attr *pattr, const struct lu_name *lname,
72 struct lu_fid *fid, unsigned int may_mask)
74 const char *name = lname->ln_name;
75 const struct dt_key *key = (const struct dt_key *)name;
76 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
77 struct dt_object *dir = mdd_object_child(mdd_obj);
82 if (unlikely(mdd_is_dead_obj(mdd_obj)))
85 if (!mdd_object_exists(mdd_obj))
88 if (mdd_object_remote(mdd_obj)) {
89 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
90 mdd_obj_dev_name(mdd_obj),
91 PFID(mdd_object_fid(mdd_obj)));
94 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
99 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
100 dt_try_as_dir(env, dir)))
101 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
108 int mdd_lookup(const struct lu_env *env,
109 struct md_object *pobj, const struct lu_name *lname,
110 struct lu_fid *fid, struct md_op_spec *spec)
112 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
116 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
120 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
121 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
125 /** Read the link EA into a temp buffer.
126 * Uses the mdd_thread_info::mti_link_buf since it is generally large.
127 * A pointer to the buffer is stored in \a ldata::ld_buf.
131 static int __mdd_links_read(const struct lu_env *env,
132 struct mdd_object *mdd_obj,
133 struct linkea_data *ldata)
137 if (!mdd_object_exists(mdd_obj))
140 /* First try a small buf */
141 LASSERT(env != NULL);
142 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
144 if (ldata->ld_buf->lb_buf == NULL)
147 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
149 /* Buf was too small, figure out what we need. */
150 lu_buf_free(ldata->ld_buf);
151 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
155 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
156 if (ldata->ld_buf->lb_buf == NULL)
158 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
162 lu_buf_free(ldata->ld_buf);
163 ldata->ld_buf = NULL;
167 return linkea_init(ldata);
170 int mdd_links_read(const struct lu_env *env,
171 struct mdd_object *mdd_obj,
172 struct linkea_data *ldata)
176 rc = __mdd_links_read(env, mdd_obj, ldata);
178 rc = linkea_init(ldata);
183 static int mdd_links_read_with_rec(const struct lu_env *env,
184 struct mdd_object *mdd_obj,
185 struct linkea_data *ldata)
189 rc = __mdd_links_read(env, mdd_obj, ldata);
191 rc = linkea_init_with_rec(ldata);
197 * Get parent FID of the directory
199 * Read parent FID from linkEA, if that fails, then do lookup
200 * dotdot to get the parent FID.
202 * \param[in] env execution environment
203 * \param[in] obj object from which to find the parent FID
204 * \param[in] attr attribute of the object
205 * \param[out] fid fid to get the parent FID
207 * \retval 0 if getting the parent FID succeeds.
208 * \retval negative errno if getting the parent FID fails.
210 static inline int mdd_parent_fid(const struct lu_env *env,
211 struct mdd_object *obj,
212 const struct lu_attr *attr,
215 struct mdd_thread_info *info = mdd_env_info(env);
216 struct linkea_data ldata = { NULL };
217 struct lu_buf *buf = &info->mti_link_buf;
218 struct lu_name lname;
223 LASSERTF(S_ISDIR(mdd_object_type(obj)),
224 "%s: FID "DFID" is not a directory type = %o\n",
225 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
226 mdd_object_type(obj));
228 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
229 if (buf->lb_buf == NULL)
230 GOTO(lookup, rc = 0);
233 rc = mdd_links_read_with_rec(env, obj, &ldata);
237 /* the obj is not locked, don't cache attributes */
238 mdd_invalidate(env, &obj->mod_obj);
240 LASSERT(ldata.ld_leh != NULL);
241 /* Directory should only have 1 parent */
242 if (ldata.ld_leh->leh_reccount > 1)
245 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
247 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
248 if (likely(fid_is_sane(fid)))
251 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
256 * For root fid use special function, which does not compare version component
257 * of fid. Version component is different for root fids on all MDTs.
259 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
261 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
262 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
266 * return 1: if \a tfid is the fid of the ancestor of \a mo;
268 * otherwise: values < 0, errors.
270 static int mdd_is_parent(const struct lu_env *env,
271 struct mdd_device *mdd,
272 struct mdd_object *mo,
273 const struct lu_attr *attr,
274 const struct lu_fid *tfid)
276 struct mdd_object *mp;
280 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
281 pfid = &mdd_env_info(env)->mti_fid;
283 if (mdd_is_root(mdd, mdd_object_fid(mo)))
286 if (mdd_is_root(mdd, tfid))
289 rc = mdd_parent_fid(env, mo, attr, pfid);
294 if (lu_fid_eq(pfid, tfid))
297 if (mdd_is_root(mdd, pfid))
300 mp = mdd_object_find(env, mdd, pfid);
304 if (!mdd_object_exists(mp)) {
305 mdd_object_put(env, mp);
309 rc = mdd_parent_fid(env, mp, attr, pfid);
310 mdd_object_put(env, mp);
319 * No permission check is needed.
321 * returns 1: if fid is ancestor of @mo;
322 * returns 0: if fid is not an ancestor of @mo;
323 * returns < 0: if error
325 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
326 const struct lu_fid *fid)
328 struct mdd_device *mdd = mdo2mdd(mo);
329 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
333 if (!mdd_object_exists(md2mdd_obj(mo)))
336 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
339 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
343 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
348 * Check that @dir contains no entries except (possibly) dot and dotdot.
353 * -ENOTDIR not a directory object
354 * -ENOTEMPTY not empty
358 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
361 struct dt_object *obj;
362 const struct dt_it_ops *iops;
366 obj = mdd_object_child(dir);
367 if (!dt_try_as_dir(env, obj))
370 iops = &obj->do_index_ops->dio_it;
371 it = iops->init(env, obj, LUDA_64BITHASH);
373 result = iops->get(env, it, (const struct dt_key *)"");
376 for (result = 0, i = 0; result == 0 && i < 3; ++i)
377 result = iops->next(env, it);
380 else if (result == 1)
382 } else if (result == 0)
384 * Huh? Index contains no zero key?
391 result = PTR_ERR(it);
392 /* -ENODEV means no valid stripe */
393 if (result == -ENODEV)
400 * Determine if the target object can be hard linked, and right now it only
401 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
402 * directory nlink count might exceed the maximum link count(see
403 * osd_object_ref_add), so it only check nlink for non-directories.
405 * \param[in] env thread environment
406 * \param[in] obj object being linked to
407 * \param[in] la attributes of \a obj
409 * \retval 0 if \a obj can be hard linked
410 * \retval negative error if \a obj is a directory or has too
413 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
414 const struct lu_attr *la)
416 struct mdd_device *m = mdd_obj2mdd_dev(obj);
421 /* Subdir count limitation can be broken through
422 * (see osd_object_ref_add), so only check non-directory here. */
423 if (!S_ISDIR(la->la_mode) &&
424 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
431 * Check whether it may create the cobj under the pobj.
433 * \param[in] env execution environment
434 * \param[in] pobj the parent directory
435 * \param[in] pattr the attribute of the parent directory
436 * \param[in] cobj the child to be created
437 * \param[in] check_perm if check WRITE|EXEC permission for parent
439 * \retval = 0 create the child under this dir is allowed
440 * \retval negative errno create the child under this dir is
443 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
444 const struct lu_attr *pattr, struct mdd_object *cobj,
450 if (cobj && mdd_object_exists(cobj))
453 if (mdd_is_dead_obj(pobj))
457 rc = mdd_permission_internal_locked(env, pobj, pattr,
458 MAY_WRITE | MAY_EXEC,
464 * Check whether can unlink from the pobj in the case of "cobj == NULL".
466 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
467 const struct lu_attr *pattr, const struct lu_attr *attr)
472 if (mdd_is_dead_obj(pobj))
475 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
478 rc = mdd_permission_internal_locked(env, pobj, pattr,
479 MAY_WRITE | MAY_EXEC,
484 if (pattr->la_flags & LUSTRE_APPEND_FL)
491 * pobj == NULL is remote ops case, under such case, pobj's
492 * VTX feature has been checked already, no need check again.
494 static inline int mdd_is_sticky(const struct lu_env *env,
495 struct mdd_object *pobj,
496 const struct lu_attr *pattr,
497 struct mdd_object *cobj,
498 const struct lu_attr *cattr)
500 struct lu_ucred *uc = lu_ucred_assert(env);
503 LASSERT(pattr != NULL);
504 if (!(pattr->la_mode & S_ISVTX) ||
505 (pattr->la_uid == uc->uc_fsuid))
509 LASSERT(cattr != NULL);
510 if (cattr->la_uid == uc->uc_fsuid)
513 return !md_capable(uc, CAP_FOWNER);
516 static int mdd_may_delete_entry(const struct lu_env *env,
517 struct mdd_object *pobj,
518 const struct lu_attr *pattr,
523 LASSERT(pobj != NULL);
524 if (!mdd_object_exists(pobj))
527 if (mdd_is_dead_obj(pobj))
532 rc = mdd_permission_internal_locked(env, pobj, pattr,
533 MAY_WRITE | MAY_EXEC,
539 if (pattr->la_flags & LUSTRE_APPEND_FL)
546 * Check whether it may delete the cobj from the pobj.
549 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
550 const struct lu_attr *tpattr, struct mdd_object *tobj,
551 const struct lu_attr *tattr, const struct lu_attr *cattr,
552 int check_perm, int check_empty)
558 LASSERT(tpattr != NULL);
559 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
567 if (!mdd_object_exists(tobj))
570 if (mdd_is_dead_obj(tobj))
573 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
576 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
579 /* additional check the rename case */
581 if (S_ISDIR(cattr->la_mode)) {
582 if (!S_ISDIR(tattr->la_mode))
585 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
586 mdd_object_fid(tobj)))
588 } else if (S_ISDIR(tattr->la_mode))
592 if (S_ISDIR(tattr->la_mode) && check_empty)
593 rc = mdd_dir_is_empty(env, tobj);
599 * Check whether it can create the link file(linked to @src_obj) under
600 * the target directory(@tgt_obj), and src_obj has been locked by
603 * \param[in] env execution environment
604 * \param[in] tgt_obj the target directory
605 * \param[in] tattr attributes of target directory
606 * \param[in] lname the link name
607 * \param[in] src_obj source object for link
608 * \param[in] cattr attributes for source object
610 * \retval = 0 it is allowed to create the link file under tgt_obj
611 * \retval negative error not allowed to create the link file
613 static int mdd_link_sanity_check(const struct lu_env *env,
614 struct mdd_object *tgt_obj,
615 const struct lu_attr *tattr,
616 const struct lu_name *lname,
617 struct mdd_object *src_obj,
618 const struct lu_attr *cattr)
620 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
624 if (!mdd_object_exists(src_obj))
627 if (mdd_is_dead_obj(src_obj))
630 /* Local ops, no lookup before link, check filename length here. */
631 rc = mdd_name_check(m, lname);
635 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
638 if (S_ISDIR(mdd_object_type(src_obj)))
641 LASSERT(src_obj != tgt_obj);
642 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
646 rc = __mdd_may_link(env, src_obj, cattr);
651 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
652 const char *name, struct thandle *handle)
654 struct dt_object *next = mdd_object_child(pobj);
658 if (dt_try_as_dir(env, next))
659 rc = dt_delete(env, next, (struct dt_key *)name, handle);
666 static int __mdd_index_insert_only(const struct lu_env *env,
667 struct mdd_object *pobj,
668 const struct lu_fid *lf, __u32 type,
670 struct thandle *handle)
672 struct dt_object *next = mdd_object_child(pobj);
676 if (dt_try_as_dir(env, next)) {
677 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
680 rec->rec_type = type;
681 rc = dt_insert(env, next, (const struct dt_rec *)rec,
682 (const struct dt_key *)name, handle);
689 /* insert named index, add reference if isdir */
690 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
691 const struct lu_fid *lf, __u32 type,
692 const char *name, struct thandle *handle)
697 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
698 if (rc == 0 && S_ISDIR(type)) {
699 mdd_write_lock(env, pobj, DT_TGT_PARENT);
700 mdo_ref_add(env, pobj, handle);
701 mdd_write_unlock(env, pobj);
707 /* delete named index, drop reference if isdir */
708 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
709 const char *name, int is_dir,
710 struct thandle *handle)
715 rc = __mdd_index_delete_only(env, pobj, name, handle);
716 if (rc == 0 && is_dir) {
717 mdd_write_lock(env, pobj, DT_TGT_PARENT);
718 mdo_ref_del(env, pobj, handle);
719 mdd_write_unlock(env, pobj);
725 static int mdd_llog_record_calc_size(const struct lu_env *env,
726 const struct lu_name *tname,
727 const struct lu_name *sname)
729 const struct lu_ucred *uc = lu_ucred(env);
730 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
731 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
734 clf_flags |= CLF_RENAME;
736 if (uc != NULL && uc->uc_jobid[0] != '\0')
737 clf_flags |= CLF_JOBID;
739 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
740 changelog_rec_offset(clf_flags, crfe) +
741 (tname != NULL ? tname->ln_namelen : 0) +
742 (sname != NULL ? 1 + sname->ln_namelen : 0));
745 int mdd_declare_changelog_store(const struct lu_env *env,
746 struct mdd_device *mdd,
747 enum changelog_rec_type type,
748 const struct lu_name *tname,
749 const struct lu_name *sname,
750 struct thandle *handle)
752 struct obd_device *obd = mdd2obd_dev(mdd);
753 struct llog_ctxt *ctxt;
754 struct llog_rec_hdr rec_hdr;
755 struct thandle *llog_th;
758 if (!mdd_changelog_enabled(env, mdd, type))
761 rec_hdr.lrh_len = mdd_llog_record_calc_size(env, tname, sname);
762 rec_hdr.lrh_type = CHANGELOG_REC;
764 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
768 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
770 GOTO(out_put, rc = PTR_ERR(llog_th));
772 rc = llog_declare_add(env, ctxt->loc_handle, &rec_hdr, llog_th);
780 int mdd_changelog_write_rec(const struct lu_env *env,
781 struct llog_handle *loghandle,
782 struct llog_rec_hdr *r,
783 struct llog_cookie *cookie,
784 int idx, struct thandle *th)
788 if (r->lrh_type == CHANGELOG_REC) {
789 struct mdd_device *mdd;
790 struct llog_changelog_rec *rec;
792 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
793 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
795 spin_lock(&mdd->mdd_cl.mc_lock);
796 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
797 spin_unlock(&mdd->mdd_cl.mc_lock);
799 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
803 * if current llog is full, we will generate a new
804 * llog, and since it's actually not an error, let's
805 * avoid increasing index so that userspace apps
806 * should not see a gap in the changelog sequence
808 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
809 spin_lock(&mdd->mdd_cl.mc_lock);
810 ++mdd->mdd_cl.mc_index;
811 spin_unlock(&mdd->mdd_cl.mc_lock);
814 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
821 /** Add a changelog entry \a rec to the changelog llog
824 * \param handle - currently ignored since llogs start their own transaction;
825 * this will hopefully be fixed in llog rewrite
828 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
829 struct llog_changelog_rec *rec, struct thandle *th)
831 struct obd_device *obd = mdd2obd_dev(mdd);
832 struct llog_ctxt *ctxt;
833 struct thandle *llog_th;
836 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
837 changelog_rec_varsize(&rec->cr));
839 /* llog_lvfs_write_rec sets the llog tail len */
840 rec->cr_hdr.lrh_type = CHANGELOG_REC;
841 rec->cr.cr_time = cl_time();
843 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
847 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
849 GOTO(out_put, rc = PTR_ERR(llog_th));
851 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
852 /* nested journal transaction */
853 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
855 /* time to recover some space ?? */
856 if (likely(!mdd->mdd_changelog_gc ||
857 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
858 mdd->mdd_changelog_min_gc_interval >=
859 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
860 /* save a spin_lock trip */
862 spin_lock(&mdd->mdd_cl.mc_lock);
863 if (likely(mdd->mdd_changelog_gc &&
864 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
865 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
866 mdd->mdd_changelog_min_gc_interval)) {
867 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
868 mdd->mdd_changelog_min_free_cat_entries ||
869 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
870 CWARN("%s:%s low on changelog_catalog free entries, "
871 "starting ChangeLog garbage collection thread\n",
873 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
876 /* indicate further kthread run will occur outside
877 * right after current journal transaction filling has
880 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
882 /* next check in mdd_changelog_min_gc_interval anyway
884 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
886 spin_unlock(&mdd->mdd_cl.mc_lock);
894 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
895 const struct lu_fid *sfid,
896 const struct lu_fid *spfid,
897 const struct lu_name *sname)
899 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
902 LASSERT(sfid != NULL);
903 LASSERT(spfid != NULL);
904 LASSERT(sname != NULL);
906 extsize = sname->ln_namelen + 1;
908 rnm->cr_sfid = *sfid;
909 rnm->cr_spfid = *spfid;
911 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
912 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
913 rec->cr_namelen += extsize;
916 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
918 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
920 if (jobid == NULL || jobid[0] == '\0')
923 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
926 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
928 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
930 ef->cr_extra_flags = eflags;
933 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
934 __u64 uid, __u64 gid)
936 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
938 uidgid->cr_uid = uid;
939 uidgid->cr_gid = gid;
942 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
945 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
950 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
952 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
954 omd->cr_openflags = flags;
957 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
958 const char *xattr_name)
960 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
962 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
965 /** Store a namespace change changelog record
966 * If this fails, we must fail the whole transaction; we don't
967 * want the change to commit without the log entry.
968 * \param target - mdd_object of change
969 * \param tpfid - target parent dir/object fid
970 * \param sfid - source object fid
971 * \param spfid - source parent fid
972 * \param tname - target name string
973 * \param sname - source name string
974 * \param handle - transaction handle
976 int mdd_changelog_ns_store(const struct lu_env *env,
977 struct mdd_device *mdd,
978 enum changelog_rec_type type,
979 enum changelog_rec_flags clf_flags,
980 struct mdd_object *target,
981 const struct lu_fid *tpfid,
982 const struct lu_fid *sfid,
983 const struct lu_fid *spfid,
984 const struct lu_name *tname,
985 const struct lu_name *sname,
986 struct thandle *handle)
988 const struct lu_ucred *uc = lu_ucred(env);
989 struct llog_changelog_rec *rec;
992 __u64 xflags = CLFE_INVALID;
996 if (!mdd_changelog_enabled(env, mdd, type))
999 LASSERT(tpfid != NULL);
1000 LASSERT(tname != NULL);
1001 LASSERT(handle != NULL);
1003 reclen = mdd_llog_record_calc_size(env, tname, sname);
1004 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_chlg_buf, reclen);
1005 if (buf->lb_buf == NULL)
1009 clf_flags &= CLF_FLAGMASK;
1010 clf_flags |= CLF_EXTRA_FLAGS;
1013 if (uc->uc_jobid[0] != '\0')
1014 clf_flags |= CLF_JOBID;
1015 xflags |= CLFE_UIDGID;
1020 clf_flags |= CLF_RENAME;
1022 clf_flags |= CLF_VERSION;
1024 rec->cr.cr_flags = clf_flags;
1026 if (clf_flags & CLF_EXTRA_FLAGS) {
1027 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1028 if (xflags & CLFE_UIDGID)
1029 mdd_changelog_rec_extra_uidgid(&rec->cr,
1030 uc->uc_uid, uc->uc_gid);
1031 if (xflags & CLFE_NID)
1032 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1035 rec->cr.cr_type = (__u32)type;
1036 rec->cr.cr_pfid = *tpfid;
1037 rec->cr.cr_namelen = tname->ln_namelen;
1038 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1040 if (clf_flags & CLF_RENAME)
1041 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1043 if (clf_flags & CLF_JOBID)
1044 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1046 if (likely(target != NULL)) {
1047 rec->cr.cr_tfid = *mdd_object_fid(target);
1048 target->mod_cltime = ktime_get();
1050 fid_zero(&rec->cr.cr_tfid);
1053 rc = mdd_changelog_store(env, mdd, rec, handle);
1055 CERROR("%s: cannot store changelog record: type = %d, "
1056 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1057 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1058 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1065 static int __mdd_links_add(const struct lu_env *env,
1066 struct mdd_object *mdd_obj,
1067 struct linkea_data *ldata,
1068 const struct lu_name *lname,
1069 const struct lu_fid *pfid,
1070 int first, int check)
1074 if (ldata->ld_leh == NULL) {
1075 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1079 rc = linkea_data_new(ldata,
1080 &mdd_env_info(env)->mti_link_buf);
1087 rc = linkea_links_find(ldata, lname, pfid);
1088 if (rc && rc != -ENOENT)
1094 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1095 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1099 linkea_add_buf(ldata, lname, tfid);
1102 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1103 linkea_add_buf(ldata, lname, pfid);
1105 return linkea_add_buf(ldata, lname, pfid);
1108 static int __mdd_links_del(const struct lu_env *env,
1109 struct mdd_object *mdd_obj,
1110 struct linkea_data *ldata,
1111 const struct lu_name *lname,
1112 const struct lu_fid *pfid)
1116 if (ldata->ld_leh == NULL) {
1117 rc = mdd_links_read(env, mdd_obj, ldata);
1122 rc = linkea_links_find(ldata, lname, pfid);
1126 linkea_del_buf(ldata, lname);
1130 static int mdd_linkea_prepare(const struct lu_env *env,
1131 struct mdd_object *mdd_obj,
1132 const struct lu_fid *oldpfid,
1133 const struct lu_name *oldlname,
1134 const struct lu_fid *newpfid,
1135 const struct lu_name *newlname,
1136 int first, int check,
1137 struct linkea_data *ldata)
1142 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1145 LASSERT(oldpfid != NULL || newpfid != NULL);
1147 if (mdd_obj->mod_flags & DEAD_OBJ)
1148 /* Unnecessary to update linkEA for dead object. */
1151 if (oldpfid != NULL) {
1152 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1154 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1157 /* No changes done. */
1162 /* If renaming, add the new record */
1163 if (newpfid != NULL)
1164 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1170 int mdd_links_rename(const struct lu_env *env,
1171 struct mdd_object *mdd_obj,
1172 const struct lu_fid *oldpfid,
1173 const struct lu_name *oldlname,
1174 const struct lu_fid *newpfid,
1175 const struct lu_name *newlname,
1176 struct thandle *handle,
1177 struct linkea_data *ldata,
1178 int first, int check)
1183 if (ldata == NULL) {
1184 ldata = &mdd_env_info(env)->mti_link_data;
1185 memset(ldata, 0, sizeof(*ldata));
1186 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1187 newpfid, newlname, first, check, ldata);
1192 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1193 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1199 if (newlname == NULL)
1200 CERROR("link_ea add failed %d "DFID"\n",
1201 rc, PFID(mdd_object_fid(mdd_obj)));
1202 else if (oldpfid == NULL)
1203 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1204 newlname->ln_namelen, newlname->ln_name, rc,
1205 PFID(mdd_object_fid(mdd_obj)));
1206 else if (newpfid == NULL)
1207 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1208 oldlname->ln_namelen, oldlname->ln_name, rc,
1209 PFID(mdd_object_fid(mdd_obj)));
1211 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1212 "\n", oldlname->ln_namelen, oldlname->ln_name,
1213 newlname->ln_namelen, newlname->ln_name, rc,
1214 PFID(mdd_object_fid(mdd_obj)));
1217 if (is_vmalloc_addr(ldata->ld_buf))
1218 /* if we vmalloced a large buffer drop it */
1219 lu_buf_free(ldata->ld_buf);
1224 static inline int mdd_links_add(const struct lu_env *env,
1225 struct mdd_object *mdd_obj,
1226 const struct lu_fid *pfid,
1227 const struct lu_name *lname,
1228 struct thandle *handle,
1229 struct linkea_data *ldata, int first)
1231 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1232 pfid, lname, handle, ldata, first, 0);
1235 static inline int mdd_links_del(const struct lu_env *env,
1236 struct mdd_object *mdd_obj,
1237 const struct lu_fid *pfid,
1238 const struct lu_name *lname,
1239 struct thandle *handle)
1241 return mdd_links_rename(env, mdd_obj, pfid, lname,
1242 NULL, NULL, handle, NULL, 0, 0);
1245 /** Read the link EA into a temp buffer.
1246 * Uses the name_buf since it is generally large.
1247 * \retval IS_ERR err
1248 * \retval ptr to \a lu_buf (always \a mti_link_buf)
1250 struct lu_buf *mdd_links_get(const struct lu_env *env,
1251 struct mdd_object *mdd_obj)
1253 struct linkea_data ldata = { NULL };
1256 rc = mdd_links_read(env, mdd_obj, &ldata);
1257 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1260 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1261 struct linkea_data *ldata, struct thandle *handle)
1263 const struct lu_buf *buf;
1266 if (ldata == NULL || ldata->ld_buf == NULL ||
1267 ldata->ld_leh == NULL)
1270 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1274 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1275 ldata->ld_leh->leh_len);
1276 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1277 if (unlikely(rc == -ENOSPC)) {
1278 rc = linkea_overflow_shrink(ldata);
1286 static int mdd_declare_links_add(const struct lu_env *env,
1287 struct mdd_object *mdd_obj,
1288 struct thandle *handle,
1289 struct linkea_data *ldata)
1295 if (ldata != NULL && ldata->ld_leh != NULL) {
1296 ea_len = ldata->ld_leh->leh_len;
1297 linkea = ldata->ld_buf->lb_buf;
1299 ea_len = MAX_LINKEA_SIZE;
1303 rc = mdo_declare_xattr_set(env, mdd_obj,
1304 mdd_buf_get_const(env, linkea, ea_len),
1305 XATTR_NAME_LINK, 0, handle);
1310 static inline int mdd_declare_links_del(const struct lu_env *env,
1311 struct mdd_object *c,
1312 struct thandle *handle)
1316 /* For directory, the linkEA will be removed together
1317 * with the object. */
1318 if (!S_ISDIR(mdd_object_type(c)))
1319 rc = mdd_declare_links_add(env, c, handle, NULL);
1324 static int mdd_declare_link(const struct lu_env *env,
1325 struct mdd_device *mdd,
1326 struct mdd_object *p,
1327 struct mdd_object *c,
1328 const struct lu_name *name,
1329 struct thandle *handle,
1331 struct linkea_data *data)
1333 struct lu_fid tfid = *mdd_object_fid(c);
1336 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1337 tfid.f_oid = cfs_fail_val;
1339 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1340 name->ln_name, handle);
1344 rc = mdo_declare_ref_add(env, c, handle);
1348 la->la_valid = LA_CTIME | LA_MTIME;
1349 rc = mdo_declare_attr_set(env, p, la, handle);
1353 la->la_valid = LA_CTIME;
1354 rc = mdo_declare_attr_set(env, c, la, handle);
1358 rc = mdd_declare_links_add(env, c, handle, data);
1362 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1368 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1369 struct md_object *src_obj, const struct lu_name *lname,
1372 const char *name = lname->ln_name;
1373 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1374 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1375 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1376 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1377 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1378 struct mdd_device *mdd = mdo2mdd(src_obj);
1379 struct thandle *handle;
1380 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1381 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1385 rc = mdd_la_get(env, mdd_sobj, cattr);
1389 rc = mdd_la_get(env, mdd_tobj, tattr);
1394 * If we are using project inheritance, we only allow hard link
1395 * creation in our tree when the project IDs are the same;
1396 * otherwise the tree quota mechanism could be circumvented.
1398 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1399 (tattr->la_projid != cattr->la_projid))
1402 handle = mdd_trans_create(env, mdd);
1404 GOTO(out_pending, rc = PTR_ERR(handle));
1406 memset(ldata, 0, sizeof(*ldata));
1408 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1409 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1411 /* Note: even this function will change ldata, but it comes from
1412 * thread_info, which is completely temporary and only seen in
1413 * this function, so we do not need reset ldata once it fails.*/
1414 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1415 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1419 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1424 rc = mdd_trans_start(env, mdd, handle);
1428 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1429 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1432 GOTO(out_unlock, rc);
1434 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1435 rc = mdo_ref_add(env, mdd_sobj, handle);
1437 GOTO(out_unlock, rc);
1440 *tfid = *mdd_object_fid(mdd_sobj);
1441 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1442 tfid->f_oid = cfs_fail_val;
1444 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1445 mdd_object_type(mdd_sobj), name, handle);
1447 mdo_ref_del(env, mdd_sobj, handle);
1448 GOTO(out_unlock, rc);
1451 la->la_valid = LA_CTIME | LA_MTIME;
1452 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1454 GOTO(out_unlock, rc);
1456 la->la_valid = LA_CTIME;
1457 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1459 /* Note: The failure of links_add should not cause the
1460 * link failure, so do not check return value. */
1461 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1462 lname, handle, ldata, 0);
1466 mdd_write_unlock(env, mdd_sobj);
1468 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1469 mdd_object_fid(mdd_tobj), NULL,
1470 NULL, lname, NULL, handle);
1472 rc = mdd_trans_stop(env, mdd, rc, handle);
1473 if (is_vmalloc_addr(ldata->ld_buf))
1474 /* if we vmalloced a large buffer drop it */
1475 lu_buf_free(ldata->ld_buf);
1480 static int mdd_mark_orphan_object(const struct lu_env *env,
1481 struct mdd_object *obj, struct thandle *handle,
1484 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1487 attr->la_valid = LA_FLAGS;
1488 attr->la_flags = LUSTRE_ORPHAN_FL;
1491 rc = mdo_declare_attr_set(env, obj, attr, handle);
1493 rc = mdo_attr_set(env, obj, attr, handle);
1498 static int mdd_declare_finish_unlink(const struct lu_env *env,
1499 struct mdd_object *obj,
1500 struct thandle *handle)
1504 /* Sigh, we do not know if the unlink object will become orphan in
1505 * declare phase, but fortunately the flags here does not matter
1506 * in current declare implementation */
1507 rc = mdd_mark_orphan_object(env, obj, handle, true);
1511 rc = mdo_declare_destroy(env, obj, handle);
1515 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1519 return mdd_declare_links_del(env, obj, handle);
1522 /* caller should take a lock before calling */
1523 int mdd_finish_unlink(const struct lu_env *env,
1524 struct mdd_object *obj, struct md_attr *ma,
1525 struct mdd_object *pobj,
1526 const struct lu_name *lname,
1530 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1533 LASSERT(mdd_write_locked(env, obj) != 0);
1535 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1536 /* add new orphan and the object
1537 * will be deleted during mdd_close() */
1538 obj->mod_flags |= DEAD_OBJ;
1539 if (obj->mod_count) {
1540 rc = mdd_orphan_insert(env, obj, th);
1542 CDEBUG(D_HA, "Object "DFID" is inserted into "
1543 "orphan list, open count = %d\n",
1544 PFID(mdd_object_fid(obj)),
1547 CERROR("Object "DFID" fail to be an orphan, "
1548 "open count = %d, maybe cause failed "
1550 PFID(mdd_object_fid(obj)),
1553 /* mark object as an orphan here, not
1554 * before mdd_orphan_insert() as racing
1555 * mdd_la_get() may propagate ORPHAN_OBJ
1556 * causing the asserition */
1557 rc = mdd_mark_orphan_object(env, obj, th, false);
1559 rc = mdo_destroy(env, obj, th);
1561 } else if (!is_dir) {
1562 /* old files may not have link ea; ignore errors */
1563 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1571 * has mdd_write_lock on cobj already, but not on pobj yet
1573 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1574 const struct lu_attr *pattr,
1575 struct mdd_object *cobj,
1576 const struct lu_attr *cattr)
1581 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1586 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1587 struct mdd_object *p, struct mdd_object *c,
1588 const struct lu_name *name, struct md_attr *ma,
1589 struct thandle *handle, int no_name, int is_dir)
1591 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1594 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1595 if (likely(no_name == 0)) {
1596 rc = mdo_declare_index_delete(env, p, name->ln_name,
1603 rc = mdo_declare_ref_del(env, p, handle);
1609 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1610 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1611 la->la_valid = LA_CTIME | LA_MTIME;
1612 rc = mdo_declare_attr_set(env, p, la, handle);
1617 rc = mdo_declare_ref_del(env, c, handle);
1621 rc = mdo_declare_ref_del(env, c, handle);
1625 la->la_valid = LA_CTIME;
1626 rc = mdo_declare_attr_set(env, c, la, handle);
1630 rc = mdd_declare_finish_unlink(env, c, handle);
1634 /* FIXME: need changelog for remove entry */
1635 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1643 * test if a file has an HSM archive
1644 * if HSM attributes are not found in ma update them from
1647 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1648 struct mdd_object *obj,
1653 if (!(ma->ma_valid & MA_HSM)) {
1654 /* no HSM MD provided, read xattr */
1655 struct lu_buf *hsm_buf;
1656 const size_t buflen = sizeof(struct hsm_attrs);
1659 hsm_buf = mdd_buf_get(env, NULL, 0);
1660 lu_buf_alloc(hsm_buf, buflen);
1661 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1662 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1663 lu_buf_free(hsm_buf);
1667 ma->ma_valid |= MA_HSM;
1669 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1675 * Delete name entry and the object.
1676 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1677 * does not exist for this object, and it could only happen during resending
1678 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1679 * is also needed in this case(needed by changelog), so we have to add another
1680 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1681 * the ENOENT failure should be able to be fixed by redo mechanism.
1683 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1684 struct md_object *cobj, const struct lu_name *lname,
1685 struct md_attr *ma, int no_name)
1687 const char *name = lname->ln_name;
1688 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1689 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1690 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1691 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1692 struct mdd_object *mdd_cobj = NULL;
1693 struct mdd_device *mdd = mdo2mdd(pobj);
1694 struct thandle *handle;
1695 int rc, is_dir = 0, cl_flags = 0;
1698 /* let shutdown to start */
1699 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1701 /* cobj == NULL means only delete name entry */
1702 if (likely(cobj != NULL)) {
1703 mdd_cobj = md2mdd_obj(cobj);
1704 if (mdd_object_exists(mdd_cobj) == 0)
1708 rc = mdd_la_get(env, mdd_pobj, pattr);
1712 if (likely(mdd_cobj != NULL)) {
1714 rc = mdd_la_get(env, mdd_cobj, cattr);
1718 is_dir = S_ISDIR(cattr->la_mode);
1719 /* search for an existing archive.
1720 * we should check ahead as the object
1721 * can be destroyed in this transaction */
1722 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1723 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1726 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1730 handle = mdd_trans_create(env, mdd);
1732 RETURN(PTR_ERR(handle));
1734 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1735 lname, ma, handle, no_name, is_dir);
1739 rc = mdd_trans_start(env, mdd, handle);
1743 if (likely(mdd_cobj != NULL))
1744 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1746 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1747 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1752 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1753 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1754 GOTO(cleanup, rc = 0);
1756 if (likely(mdd_cobj != NULL)) {
1757 rc = mdo_ref_del(env, mdd_cobj, handle);
1759 __mdd_index_insert_only(env, mdd_pobj,
1760 mdd_object_fid(mdd_cobj),
1761 mdd_object_type(mdd_cobj),
1768 mdo_ref_del(env, mdd_cobj, handle);
1770 /* fetch updated nlink */
1771 rc = mdd_la_get(env, mdd_cobj, cattr);
1776 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1777 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1779 la->la_valid = LA_CTIME | LA_MTIME;
1780 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1784 /* Enough for only unlink the entry */
1785 if (unlikely(mdd_cobj == NULL))
1788 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1789 /* update ctime of an unlinked file only if it is still
1790 * opened or a link still exists */
1791 la->la_valid = LA_CTIME;
1792 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1797 /* XXX: this transfer to ma will be removed with LOD/OSP */
1798 ma->ma_attr = *cattr;
1799 ma->ma_valid |= MA_INODE;
1800 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1804 /* fetch updated nlink */
1805 rc = mdd_la_get(env, mdd_cobj, cattr);
1806 /* if object is removed then we can't get its attrs,
1808 if (rc == -ENOENT) {
1809 cattr->la_nlink = 0;
1813 if (cattr->la_nlink == 0) {
1814 ma->ma_attr = *cattr;
1815 ma->ma_valid |= MA_INODE;
1820 if (likely(mdd_cobj != NULL))
1821 mdd_write_unlock(env, mdd_cobj);
1824 if (cattr->la_nlink == 0)
1825 cl_flags |= CLF_UNLINK_LAST;
1827 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1829 rc = mdd_changelog_ns_store(env, mdd,
1830 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1831 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1832 lname, NULL, handle);
1836 rc = mdd_trans_stop(env, mdd, rc, handle);
1842 * The permission has been checked when obj created, no need check again.
1844 static int mdd_cd_sanity_check(const struct lu_env *env,
1845 struct mdd_object *obj)
1850 if (!obj || mdd_is_dead_obj(obj))
1856 static int mdd_create_data(const struct lu_env *env,
1857 struct md_object *pobj,
1858 struct md_object *cobj,
1859 const struct md_op_spec *spec,
1862 struct mdd_device *mdd = mdo2mdd(cobj);
1863 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1864 struct mdd_object *son = md2mdd_obj(cobj);
1865 struct thandle *handle;
1866 const struct lu_buf *buf;
1867 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1868 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1872 rc = mdd_cd_sanity_check(env, son);
1876 if (!md_should_create(spec->sp_cr_flags))
1880 * there are following use cases for this function:
1881 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1882 * striping can be specified or not
1885 rc = mdd_la_get(env, son, attr);
1889 /* calling ->ah_make_hint() is used to transfer information from parent */
1890 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1892 handle = mdd_trans_create(env, mdd);
1894 GOTO(out_free, rc = PTR_ERR(handle));
1897 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1898 * Should this be fixed?
1900 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1901 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1902 spec->sp_cr_flags, spec->no_create);
1904 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1905 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1906 spec->u.sp_ea.eadatalen);
1911 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1912 XATTR_NAME_LOV, 0, handle);
1916 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1921 rc = mdd_trans_start(env, mdd, handle);
1925 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1931 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1935 rc = mdd_trans_stop(env, mdd, rc, handle);
1941 static int mdd_declare_object_initialize(const struct lu_env *env,
1942 struct mdd_object *parent,
1943 struct mdd_object *child,
1944 const struct lu_attr *attr,
1945 struct thandle *handle)
1950 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1951 if (!S_ISDIR(attr->la_mode))
1954 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1955 S_IFDIR, dot, handle);
1959 rc = mdo_declare_ref_add(env, child, handle);
1963 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
1964 S_IFDIR, dotdot, handle);
1969 static int mdd_object_initialize(const struct lu_env *env,
1970 const struct lu_fid *pfid,
1971 struct mdd_object *child,
1972 struct lu_attr *attr,
1973 struct thandle *handle)
1978 if (S_ISDIR(attr->la_mode)) {
1979 /* Add "." and ".." for newly created dir */
1980 mdo_ref_add(env, child, handle);
1981 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
1982 S_IFDIR, dot, handle);
1984 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1987 mdo_ref_del(env, child, handle);
1994 * This function checks whether it can create a file/dir under the
1995 * directory(@pobj). The directory(@pobj) is not being locked by
1998 * \param[in] env execution environment
1999 * \param[in] pobj the directory to create files
2000 * \param[in] pattr the attributes of the directory
2001 * \param[in] lname the name of the created file/dir
2002 * \param[in] cattr the attributes of the file/dir
2003 * \param[in] spec create specification
2005 * \retval = 0 it is allowed to create file/dir under
2007 * \retval negative error not allowed to create file/dir
2008 * under the directory
2010 static int mdd_create_sanity_check(const struct lu_env *env,
2011 struct md_object *pobj,
2012 const struct lu_attr *pattr,
2013 const struct lu_name *lname,
2014 struct lu_attr *cattr,
2015 struct md_op_spec *spec)
2017 struct mdd_thread_info *info = mdd_env_info(env);
2018 struct lu_fid *fid = &info->mti_fid;
2019 struct mdd_object *obj = md2mdd_obj(pobj);
2020 struct mdd_device *m = mdo2mdd(pobj);
2021 bool check_perm = true;
2026 if (mdd_is_dead_obj(obj))
2030 * In some cases this lookup is not needed - we know before if name
2031 * exists or not because MDT performs lookup for it.
2032 * name length check is done in lookup.
2034 if (spec->sp_cr_lookup) {
2036 * Check if the name already exist, though it will be checked in
2037 * _index_insert also, for avoiding rolling back if exists
2040 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2041 MAY_WRITE | MAY_EXEC);
2043 RETURN(rc ? : -EEXIST);
2045 /* Permission is already being checked in mdd_lookup */
2049 if (S_ISDIR(cattr->la_mode) &&
2050 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2051 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2052 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2054 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2055 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2057 CERROR("%s: invalid lmv_user_md: magic = %x, "
2058 "stripe_offset = %d, stripe_count = %u: "
2059 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2060 le32_to_cpu(lum->lum_magic),
2061 (int)le32_to_cpu(lum->lum_stripe_offset),
2062 le32_to_cpu(lum->lum_stripe_count), rc);
2067 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2072 if (pattr->la_mode & S_ISGID) {
2073 struct lu_ucred *uc = lu_ucred(env);
2075 cattr->la_gid = pattr->la_gid;
2077 /* Directories are special, and always inherit S_ISGID */
2078 if (S_ISDIR(cattr->la_mode)) {
2079 cattr->la_mode |= S_ISGID;
2080 cattr->la_valid |= LA_MODE;
2081 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2082 == (S_ISGID | S_IXGRP) &&
2083 !lustre_in_group_p(uc,
2084 (cattr->la_valid & LA_GID) ?
2085 cattr->la_gid : pattr->la_gid) &&
2086 !md_capable(uc, CAP_FSETID)) {
2087 cattr->la_mode &= ~S_ISGID;
2088 cattr->la_valid |= LA_MODE;
2092 /* Inherit project ID from parent directory */
2093 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2094 cattr->la_projid = pattr->la_projid;
2095 if (S_ISDIR(cattr->la_mode)) {
2096 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2097 cattr->la_valid |= LA_FLAGS;
2099 cattr->la_valid |= LA_PROJID;
2102 rc = mdd_name_check(m, lname);
2106 switch (cattr->la_mode & S_IFMT) {
2108 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2110 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2111 RETURN(-ENAMETOOLONG);
2130 static int mdd_declare_create_object(const struct lu_env *env,
2131 struct mdd_device *mdd,
2132 struct mdd_object *p, struct mdd_object *c,
2133 struct lu_attr *attr,
2134 struct thandle *handle,
2135 const struct md_op_spec *spec,
2136 struct lu_buf *def_acl_buf,
2137 struct lu_buf *acl_buf,
2138 struct lu_buf *hsm_buf,
2139 struct dt_allocation_hint *hint)
2141 const struct lu_buf *buf;
2144 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2145 /* ldiskfs OSD needs this information for credit allocation */
2147 hint->dah_acl_len = def_acl_buf->lb_len;
2149 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2154 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2155 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2156 /* if dir, then can inherit default ACl */
2157 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2158 XATTR_NAME_ACL_DEFAULT,
2164 if (acl_buf && acl_buf->lb_len > 0) {
2165 rc = mdo_declare_attr_set(env, c, attr, handle);
2169 rc = mdo_declare_xattr_set(env, c, acl_buf,
2170 XATTR_NAME_ACL_ACCESS, 0, handle);
2175 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2179 /* replay case, create LOV EA from client data */
2180 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2181 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2182 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2183 spec->u.sp_ea.eadatalen);
2184 rc = mdo_declare_xattr_set(env, c, buf,
2185 S_ISDIR(attr->la_mode) ?
2186 XATTR_NAME_LMV : XATTR_NAME_LOV,
2187 LU_XATTR_CREATE, handle);
2191 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2192 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2200 if (S_ISLNK(attr->la_mode)) {
2201 const char *target_name = spec->u.sp_symname;
2202 int sym_len = strlen(target_name);
2203 const struct lu_buf *buf;
2205 buf = mdd_buf_get_const(env, target_name, sym_len);
2206 rc = dt_declare_record_write(env, mdd_object_child(c),
2212 if (spec->sp_cr_file_secctx_name != NULL) {
2213 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2214 spec->sp_cr_file_secctx_size);
2215 rc = mdo_declare_xattr_set(env, c, buf,
2216 spec->sp_cr_file_secctx_name, 0,
2222 if (spec->sp_cr_file_encctx != NULL) {
2223 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2224 spec->sp_cr_file_encctx_size);
2225 rc = mdo_declare_xattr_set(env, c, buf,
2226 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2235 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2236 struct mdd_object *p, struct mdd_object *c,
2237 const struct lu_name *name,
2238 struct lu_attr *attr,
2239 struct thandle *handle,
2240 const struct md_op_spec *spec,
2241 struct linkea_data *ldata,
2242 struct lu_buf *def_acl_buf,
2243 struct lu_buf *acl_buf,
2244 struct lu_buf *hsm_buf,
2245 struct dt_allocation_hint *hint)
2249 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2250 def_acl_buf, acl_buf, hsm_buf, hint);
2254 if (S_ISDIR(attr->la_mode)) {
2255 rc = mdo_declare_ref_add(env, p, handle);
2260 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2261 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2265 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2266 enum changelog_rec_type type;
2268 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2269 attr->la_mode, name->ln_name,
2274 rc = mdd_declare_links_add(env, c, handle, ldata);
2279 la->la_valid = LA_CTIME | LA_MTIME;
2280 rc = mdo_declare_attr_set(env, p, la, handle);
2284 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2285 S_ISREG(attr->la_mode) ? CL_CREATE :
2286 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2288 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2297 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2298 struct lu_attr *la, struct lu_buf *def_acl_buf,
2299 struct lu_buf *acl_buf)
2305 if (S_ISLNK(la->la_mode)) {
2306 acl_buf->lb_len = 0;
2307 def_acl_buf->lb_len = 0;
2311 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2312 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2313 XATTR_NAME_ACL_DEFAULT);
2314 mdd_read_unlock(env, pobj);
2316 /* ACL buffer size is not enough, need realloc */
2317 if (rc > acl_buf->lb_len)
2320 /* If there are default ACL, fix mode/ACL by default ACL */
2321 def_acl_buf->lb_len = rc;
2322 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2323 acl_buf->lb_len = rc;
2324 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2327 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2328 /* If there are no default ACL, fix mode by mask */
2329 struct lu_ucred *uc = lu_ucred(env);
2331 /* The create triggered by MDT internal events, such as
2332 * LFSCK reset, will not contain valid "uc". */
2333 if (unlikely(uc != NULL))
2334 la->la_mode &= ~uc->uc_umask;
2336 acl_buf->lb_len = 0;
2337 def_acl_buf->lb_len = 0;
2344 * Create a metadata object and initialize it, set acl, xattr.
2346 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2347 struct mdd_object *son, struct lu_attr *attr,
2348 struct md_op_spec *spec, struct lu_buf *acl_buf,
2349 struct lu_buf *def_acl_buf,
2350 struct lu_buf *hsm_buf,
2351 struct dt_allocation_hint *hint,
2352 struct thandle *handle, bool initsecctx)
2354 const struct lu_buf *buf;
2357 mdd_write_lock(env, son, DT_TGT_CHILD);
2358 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2363 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2364 * created in declare phase, they also needs to be added to master
2365 * object as sub-directory entry. So it has to initialize the master
2366 * object, then set dir striped EA.(in mdo_xattr_set) */
2367 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2370 GOTO(err_destroy, rc);
2373 * in case of replay we just set LOVEA provided by the client
2374 * XXX: I think it would be interesting to try "old" way where
2375 * MDT calls this xattr_set(LOV) in a different transaction.
2376 * probably this way we code can be made better.
2379 /* During creation, there are only a few cases we need do xattr_set to
2381 * 1. regular file: see comments above.
2382 * 2. dir: inherit default striping or pool settings from parent.
2383 * 3. create striped directory with provided stripeEA.
2384 * 4. create striped directory because inherit default layout from the
2387 if (spec->no_create ||
2388 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2389 S_ISDIR(attr->la_mode)) {
2390 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2391 spec->u.sp_ea.eadatalen);
2392 rc = mdo_xattr_set(env, son, buf,
2393 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2395 LU_XATTR_CREATE, handle);
2397 GOTO(err_destroy, rc);
2400 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2403 memset(&mh, 0, sizeof(mh));
2404 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2405 mh.mh_arch_id = spec->sp_archive_id;
2406 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2407 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2410 GOTO(err_destroy, rc);
2413 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2414 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2415 S_ISDIR(attr->la_mode)) {
2416 /* set default acl */
2417 rc = mdo_xattr_set(env, son, def_acl_buf,
2418 XATTR_NAME_ACL_DEFAULT, 0,
2421 GOTO(err_destroy, rc);
2423 /* set its own acl */
2424 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2425 rc = mdo_xattr_set(env, son, acl_buf,
2426 XATTR_NAME_ACL_ACCESS,
2429 GOTO(err_destroy, rc);
2433 if (S_ISLNK(attr->la_mode)) {
2434 struct dt_object *dt = mdd_object_child(son);
2435 const char *target_name = spec->u.sp_symname;
2436 int sym_len = strlen(target_name);
2439 buf = mdd_buf_get_const(env, target_name, sym_len);
2440 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2444 GOTO(err_initlized, rc = -EFAULT);
2447 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2448 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2449 spec->sp_cr_file_secctx_size);
2450 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2453 GOTO(err_initlized, rc);
2456 if (spec->sp_cr_file_encctx != NULL) {
2457 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2458 spec->sp_cr_file_encctx_size);
2459 rc = mdo_xattr_set(env, son, buf,
2460 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2463 GOTO(err_initlized, rc);
2467 if (unlikely(rc != 0)) {
2469 if (S_ISDIR(attr->la_mode)) {
2470 /* Drop the reference, no need to delete "."/"..",
2471 * because the object to be destroied directly. */
2472 rc2 = mdo_ref_del(env, son, handle);
2476 rc2 = mdo_ref_del(env, son, handle);
2480 mdo_destroy(env, son, handle);
2483 mdd_write_unlock(env, son);
2487 static int mdd_index_delete(const struct lu_env *env,
2488 struct mdd_object *mdd_pobj,
2489 struct lu_attr *cattr,
2490 const struct lu_name *lname)
2492 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2493 struct thandle *handle;
2497 handle = mdd_trans_create(env, mdd);
2499 RETURN(PTR_ERR(handle));
2501 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2506 if (S_ISDIR(cattr->la_mode)) {
2507 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2512 /* Since this will only be used in the error handler path,
2513 * Let's set the thandle to be local and not mess the transno */
2514 handle->th_local = 1;
2515 rc = mdd_trans_start(env, mdd, handle);
2519 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2520 S_ISDIR(cattr->la_mode), handle);
2524 rc = mdd_trans_stop(env, mdd, rc, handle);
2530 * Create object and insert it into namespace.
2532 * Two operations have to be performed:
2534 * - an allocation of a new object (->do_create()), and
2535 * - an insertion into a parent index (->dio_insert()).
2537 * Due to locking, operation order is not important, when both are
2538 * successful, *but* error handling cases are quite different:
2540 * - if insertion is done first, and following object creation fails,
2541 * insertion has to be rolled back, but this operation might fail
2542 * also leaving us with dangling index entry.
2544 * - if creation is done first, is has to be undone if insertion fails,
2545 * leaving us with leaked space, which is not good but not fatal.
2547 * It seems that creation-first is simplest solution, but it is sub-optimal
2553 * case, because second mkdir is bound to create object, only to
2554 * destroy it immediately.
2556 * To avoid this follow local file systems that do double lookup:
2558 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2559 * 1. create (mdd_create_object_internal())
2560 * 2. insert (__mdd_index_insert(), lookup again)
2562 * \param[in] pobj parent object
2563 * \param[in] lname name of child being created
2564 * \param[in,out] child child object being created
2565 * \param[in] spec additional create parameters
2566 * \param[in] ma attributes for new child object
2568 * \retval 0 on success
2569 * \retval negative errno on failure
2571 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2572 const struct lu_name *lname, struct md_object *child,
2573 struct md_op_spec *spec, struct md_attr *ma)
2575 struct mdd_thread_info *info = mdd_env_info(env);
2576 struct lu_attr *la = &info->mti_la_for_fix;
2577 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2578 struct mdd_object *son = md2mdd_obj(child);
2579 struct mdd_device *mdd = mdo2mdd(pobj);
2580 struct lu_attr *attr = &ma->ma_attr;
2581 struct thandle *handle;
2582 struct lu_attr *pattr = &info->mti_pattr;
2583 struct lu_buf acl_buf;
2584 struct lu_buf def_acl_buf;
2585 struct lu_buf hsm_buf;
2586 struct linkea_data *ldata = &info->mti_link_data;
2587 const char *name = lname->ln_name;
2588 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2589 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2594 rc = mdd_la_get(env, mdd_pobj, pattr);
2598 /* Sanity checks before big job. */
2599 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2603 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2604 GOTO(out_free, rc = -EINPROGRESS);
2606 handle = mdd_trans_create(env, mdd);
2608 GOTO(out_free, rc = PTR_ERR(handle));
2611 acl_buf = *lu_buf_check_and_alloc(&info->mti_xattr_buf, acl_size);
2612 if (!acl_buf.lb_buf)
2613 GOTO(out_stop, rc = -ENOMEM);
2615 def_acl_buf = *lu_buf_check_and_alloc(&info->mti_big_buf, acl_size);
2616 if (!def_acl_buf.lb_buf)
2617 GOTO(out_stop, rc = -ENOMEM);
2619 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2620 if (unlikely(rc == -ERANGE &&
2621 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2622 /* use maximum-sized xattr buffer for too-big default ACL */
2623 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2625 goto use_bigger_buffer;
2630 if (S_ISDIR(attr->la_mode)) {
2631 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2634 * migrate may create 1-stripe directory, so lod_ah_init()
2635 * doesn't adjust stripe count from lmu.
2637 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2638 lmu->lum_stripe_count = 0;
2641 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2643 memset(ldata, 0, sizeof(*ldata));
2644 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2645 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2648 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2649 &tfid, lname, 1, 0, ldata);
2651 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2652 mdd_object_fid(mdd_pobj),
2653 lname, 1, 0, ldata);
2656 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2657 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2659 memset(&hsm_buf, 0, sizeof(hsm_buf));
2660 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2661 if (hsm_buf.lb_buf == NULL)
2662 GOTO(out_stop, rc = -ENOMEM);
2665 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2666 handle, spec, ldata, &def_acl_buf, &acl_buf,
2671 rc = mdd_trans_start(env, mdd, handle);
2675 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2676 &def_acl_buf, &hsm_buf, hint, handle, true);
2680 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2681 mdd_write_lock(env, son, DT_TGT_CHILD);
2682 son->mod_flags |= VOLATILE_OBJ;
2683 rc = mdd_orphan_insert(env, son, handle);
2684 GOTO(out_volatile, rc);
2686 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2687 attr->la_mode, name, handle);
2689 GOTO(err_created, rc);
2691 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2694 /* update parent directory mtime/ctime */
2696 la->la_valid = LA_CTIME | LA_MTIME;
2697 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2699 GOTO(err_insert, rc);
2705 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2706 rc2 = mdd_orphan_delete(env, son, handle);
2708 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2709 S_ISDIR(attr->la_mode),
2715 mdd_write_lock(env, son, DT_TGT_CHILD);
2716 if (S_ISDIR(attr->la_mode)) {
2717 /* Drop the reference, no need to delete "."/"..",
2718 * because the object is to be destroyed directly. */
2719 rc2 = mdo_ref_del(env, son, handle);
2721 mdd_write_unlock(env, son);
2726 /* For volatile files drop one link immediately, since there is
2727 * no filename in the namespace, and save any error returned. */
2728 rc2 = mdo_ref_del(env, son, handle);
2730 mdd_write_unlock(env, son);
2731 if (unlikely(rc == 0))
2736 /* Don't destroy the volatile object on success */
2737 if (likely(rc != 0))
2738 mdo_destroy(env, son, handle);
2739 mdd_write_unlock(env, son);
2742 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2743 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2744 rc = mdd_changelog_ns_store(env, mdd,
2745 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2746 S_ISREG(attr->la_mode) ? CL_CREATE :
2747 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2748 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2749 lname, NULL, handle);
2751 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2753 /* If creation fails, it is most likely due to the remote update
2754 * failure, because local transaction will mostly succeed at
2755 * this stage. There is no easy way to rollback all of previous
2756 * updates, so let's remove the object from namespace, and
2757 * LFSCK should handle the orphan object. */
2758 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2759 mdd_index_delete(env, mdd_pobj, attr, lname);
2763 if (is_vmalloc_addr(ldata->ld_buf))
2764 /* if we vmalloced a large buffer drop it */
2765 lu_buf_free(ldata->ld_buf);
2767 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2768 lu_buf_free(&hsm_buf);
2770 /* The child object shouldn't be cached anymore */
2772 set_bit(LU_OBJECT_HEARD_BANSHEE,
2773 &child->mo_lu.lo_header->loh_flags);
2777 /* has not mdd_write{read}_lock on any obj yet. */
2778 static int mdd_rename_sanity_check(const struct lu_env *env,
2779 struct mdd_object *src_pobj,
2780 const struct lu_attr *pattr,
2781 struct mdd_object *tgt_pobj,
2782 const struct lu_attr *tpattr,
2783 struct mdd_object *sobj,
2784 const struct lu_attr *cattr,
2785 struct mdd_object *tobj,
2786 const struct lu_attr *tattr)
2791 /* XXX: when get here, sobj must NOT be NULL,
2792 * the other case has been processed in cld_rename
2793 * before mdd_rename and enable MDS_PERM_BYPASS. */
2797 * If we are using project inheritance, we only allow renames
2798 * into our tree when the project IDs are the same; otherwise
2799 * tree quota mechanism would be circumvented.
2801 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2802 tpattr->la_projid != cattr->la_projid) ||
2803 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2804 (pattr->la_projid != tpattr->la_projid)))
2807 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2811 /* XXX: when get here, "tobj == NULL" means tobj must
2812 * NOT exist (neither on remote MDS, such case has been
2813 * processed in cld_rename before mdd_rename and enable
2815 * So check may_create, but not check may_unlink. */
2817 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2818 (src_pobj != tgt_pobj));
2820 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2821 (src_pobj != tgt_pobj), 1);
2823 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2824 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2829 static int mdd_declare_rename(const struct lu_env *env,
2830 struct mdd_device *mdd,
2831 struct mdd_object *mdd_spobj,
2832 struct mdd_object *mdd_tpobj,
2833 struct mdd_object *mdd_sobj,
2834 struct mdd_object *mdd_tobj,
2835 const struct lu_name *sname,
2836 const struct lu_name *tname,
2838 struct linkea_data *ldata,
2839 struct thandle *handle)
2841 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2844 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2845 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2851 /* name from source dir */
2852 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2856 /* .. from source child */
2857 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2858 /* source child can be directory, count by source dir's nlink */
2859 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2862 if (mdd_spobj != mdd_tpobj) {
2863 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2868 rc = mdo_declare_index_insert(env, mdd_sobj,
2869 mdd_object_fid(mdd_tpobj),
2870 S_IFDIR, dotdot, handle);
2875 /* new target child can be directory,
2876 * counted by target dir's nlink */
2877 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2882 la->la_valid = LA_CTIME | LA_MTIME;
2883 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2887 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2891 la->la_valid = LA_CTIME;
2892 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2896 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2901 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2902 mdd_object_type(mdd_sobj),
2903 tname->ln_name, handle);
2907 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2908 /* delete target child in target parent directory */
2909 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2914 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2918 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2919 /* target child can be directory,
2920 * delete "." reference in target child directory */
2921 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2925 /* delete ".." reference in target parent directory */
2926 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2931 la->la_valid = LA_CTIME;
2932 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2936 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2941 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2949 static int mdd_migrate_object(const struct lu_env *env,
2950 struct mdd_object *spobj,
2951 struct mdd_object *tpobj,
2952 struct mdd_object *sobj,
2953 struct mdd_object *tobj,
2954 const struct lu_name *sname,
2955 const struct lu_name *tname,
2956 struct md_op_spec *spec,
2957 struct md_attr *ma);
2959 /* src object can be remote that is why we use only fid and type of object */
2960 static int mdd_rename(const struct lu_env *env,
2961 struct md_object *src_pobj, struct md_object *tgt_pobj,
2962 const struct lu_fid *lf, const struct lu_name *lsname,
2963 struct md_object *tobj, const struct lu_name *ltname,
2966 const char *sname = lsname->ln_name;
2967 const char *tname = ltname->ln_name;
2968 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2969 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2970 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2971 struct mdd_device *mdd = mdo2mdd(src_pobj);
2972 struct mdd_object *mdd_sobj = NULL; /* source object */
2973 struct mdd_object *mdd_tobj = NULL;
2974 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2975 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2976 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2977 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2978 struct thandle *handle;
2979 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2980 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
2981 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
2984 bool tobj_locked = 0;
2985 unsigned cl_flags = 0;
2989 /* let unlink to complete and commit */
2990 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
2993 mdd_tobj = md2mdd_obj(tobj);
2995 mdd_sobj = mdd_object_find(env, mdd, lf);
2996 if (IS_ERR(mdd_sobj))
2997 RETURN(PTR_ERR(mdd_sobj));
2999 rc = mdd_la_get(env, mdd_sobj, cattr);
3001 GOTO(out_pending, rc);
3003 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3004 * hard links, and target doesn't exist.
3006 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3007 cattr->la_nlink == 1 && !tobj) {
3008 struct md_op_spec *spec = &mdd_env_info(env)->mti_spec;
3009 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3012 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3015 GOTO(out_pending, rc);
3017 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3018 if (IS_ERR(mdd_tobj))
3019 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3021 memset(spec, 0, sizeof(*spec));
3022 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3023 mdd_tobj, lsname, ltname, spec, ma);
3024 mdd_object_put(env, mdd_tobj);
3025 GOTO(out_pending, rc);
3028 rc = mdd_la_get(env, mdd_spobj, pattr);
3030 GOTO(out_pending, rc);
3033 rc = mdd_la_get(env, mdd_tobj, tattr);
3035 GOTO(out_pending, rc);
3036 /* search for an existing archive.
3037 * we should check ahead as the object
3038 * can be destroyed in this transaction */
3039 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3040 cl_flags |= CLF_RENAME_LAST_EXISTS;
3043 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3045 GOTO(out_pending, rc);
3047 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3048 mdd_sobj, cattr, mdd_tobj, tattr);
3050 GOTO(out_pending, rc);
3052 rc = mdd_name_check(mdd, ltname);
3054 GOTO(out_pending, rc);
3056 handle = mdd_trans_create(env, mdd);
3058 GOTO(out_pending, rc = PTR_ERR(handle));
3060 memset(ldata, 0, sizeof(*ldata));
3061 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3062 lsname, mdd_object_fid(mdd_tpobj), ltname,
3067 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3068 mdd_tobj, lsname, ltname, ma, ldata, handle);
3072 rc = mdd_trans_start(env, mdd, handle);
3076 is_dir = S_ISDIR(cattr->la_mode);
3078 /* Remove source name from source directory */
3079 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3083 /* "mv dir1 dir2" needs "dir1/.." link update */
3084 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3085 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3087 GOTO(fixup_spobj2, rc);
3089 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3092 GOTO(fixup_spobj, rc);
3095 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3096 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3098 /* tname might been renamed to something else */
3099 GOTO(fixup_spobj, rc);
3102 /* Insert new fid with target name into target dir */
3103 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3106 GOTO(fixup_tpobj, rc);
3108 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3109 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3111 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3112 la->la_valid = LA_CTIME;
3113 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3115 GOTO(fixup_tpobj, rc);
3117 /* Update the linkEA for the source object */
3118 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3119 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3120 lsname, mdd_object_fid(mdd_tpobj), ltname,
3121 handle, ldata, 0, 0);
3123 /* Old files might not have EA entry */
3124 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3125 lsname, handle, NULL, 0);
3126 mdd_write_unlock(env, mdd_sobj);
3127 /* We don't fail the transaction if the link ea can't be
3128 updated -- fid2path will use alternate lookup method. */
3131 /* Remove old target object
3132 * For tobj is remote case cmm layer has processed
3133 * and set tobj to NULL then. So when tobj is NOT NULL,
3134 * it must be local one.
3136 if (tobj && mdd_object_exists(mdd_tobj)) {
3137 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3139 if (mdd_is_dead_obj(mdd_tobj)) {
3140 /* shld not be dead, something is wrong */
3141 CERROR("tobj is dead, something is wrong\n");
3145 mdo_ref_del(env, mdd_tobj, handle);
3147 /* Remove dot reference. */
3148 if (S_ISDIR(tattr->la_mode))
3149 mdo_ref_del(env, mdd_tobj, handle);
3152 /* fetch updated nlink */
3153 rc = mdd_la_get(env, mdd_tobj, tattr);
3155 CERROR("%s: Failed to get nlink for tobj "
3157 mdd2obd_dev(mdd)->obd_name,
3158 PFID(tpobj_fid), rc);
3159 GOTO(fixup_tpobj, rc);
3162 la->la_valid = LA_CTIME;
3163 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3165 CERROR("%s: Failed to set ctime for tobj "
3167 mdd2obd_dev(mdd)->obd_name,
3168 PFID(tpobj_fid), rc);
3169 GOTO(fixup_tpobj, rc);
3172 /* XXX: this transfer to ma will be removed with LOD/OSP */
3173 ma->ma_attr = *tattr;
3174 ma->ma_valid |= MA_INODE;
3175 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3178 CERROR("%s: Failed to unlink tobj "
3180 mdd2obd_dev(mdd)->obd_name,
3181 PFID(tpobj_fid), rc);
3182 GOTO(fixup_tpobj, rc);
3185 /* fetch updated nlink */
3186 rc = mdd_la_get(env, mdd_tobj, tattr);
3187 if (rc == -ENOENT) {
3188 /* the object got removed, let's
3189 * return the latest known attributes */
3190 tattr->la_nlink = 0;
3192 } else if (rc != 0) {
3193 CERROR("%s: Failed to get nlink for tobj "
3195 mdd2obd_dev(mdd)->obd_name,
3196 PFID(tpobj_fid), rc);
3197 GOTO(fixup_tpobj, rc);
3199 /* XXX: this transfer to ma will be removed with LOD/OSP */
3200 ma->ma_attr = *tattr;
3201 ma->ma_valid |= MA_INODE;
3203 if (tattr->la_nlink == 0)
3204 cl_flags |= CLF_RENAME_LAST;
3206 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3209 la->la_valid = LA_CTIME | LA_MTIME;
3210 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3212 GOTO(fixup_tpobj, rc);
3214 if (mdd_spobj != mdd_tpobj) {
3215 la->la_valid = LA_CTIME | LA_MTIME;
3216 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3218 GOTO(fixup_tpobj, rc);
3225 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3227 CWARN("tp obj fix error %d\n",rc2);
3229 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3230 !mdd_is_dead_obj(mdd_tobj)) {
3232 mdo_ref_add(env, mdd_tobj, handle);
3234 mdo_ref_add(env, mdd_tobj, handle);
3237 rc2 = __mdd_index_insert(env, mdd_tpobj,
3238 mdd_object_fid(mdd_tobj),
3239 mdd_object_type(mdd_tobj),
3242 CWARN("tp obj fix error: rc = %d\n", rc2);
3247 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3248 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3250 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3251 mdd2obd_dev(mdd)->obd_name, rc2);
3254 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3257 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3258 mdd2obd_dev(mdd)->obd_name, rc2);
3263 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3264 mdd_object_type(mdd_sobj), sname,
3267 CWARN("sp obj fix error: rc = %d\n", rc2);
3272 mdd_write_unlock(env, mdd_tobj);
3275 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3276 mdd_tobj, tpobj_fid, lf, spobj_fid,
3277 ltname, lsname, handle);
3280 rc = mdd_trans_stop(env, mdd, rc, handle);
3283 mdd_object_put(env, mdd_sobj);
3288 * Check whether we should migrate the file/dir
3290 * < 0 permission check failed or other error.
3291 * = 0 the file can be migrated.
3293 static int mdd_migrate_sanity_check(const struct lu_env *env,
3294 struct mdd_device *mdd,
3295 struct mdd_object *spobj,
3296 struct mdd_object *tpobj,
3297 struct mdd_object *sobj,
3298 struct mdd_object *tobj,
3299 const struct lu_attr *spattr,
3300 const struct lu_attr *tpattr,
3301 const struct lu_attr *attr)
3307 if (!mdd_object_remote(sobj)) {
3308 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3309 if (sobj->mod_count > 0) {
3310 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3311 mdd_obj_dev_name(sobj),
3312 PFID(mdd_object_fid(sobj)),
3314 mdd_read_unlock(env, sobj);
3317 mdd_read_unlock(env, sobj);
3320 if (mdd_object_exists(tobj))
3323 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3327 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3332 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3333 struct mdd_object *obj,
3334 const struct lu_buf *buf,
3336 int fl, struct thandle *handle);
3338 /* iterate xattrs, but ignore LMA, LMV, and LINKEA if 'skip_linkea' is set. */
3339 static int mdd_iterate_xattrs(const struct lu_env *env,
3340 struct mdd_object *sobj,
3341 struct mdd_object *tobj,
3343 struct thandle *handle,
3346 struct mdd_thread_info *info = mdd_env_info(env);
3348 struct lu_buf list_xbuf;
3349 struct lu_buf cbxbuf;
3350 struct lu_buf xbuf = { NULL };
3359 /* retrieve xattr list from the old object */
3360 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3361 if (list_xsize == -ENODATA)
3367 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3368 if (info->mti_big_buf.lb_buf == NULL)
3371 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3372 list_xbuf.lb_len = list_xsize;
3373 rc = mdo_xattr_list(env, sobj, &list_xbuf);
3379 xname = list_xbuf.lb_buf;
3381 xlen = strnlen(xname, rem - 1) + 1;
3382 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3383 strcmp(XATTR_NAME_LMV, xname) == 0)
3387 strcmp(XATTR_NAME_LINK, xname) == 0)
3390 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3391 if (xsize == -ENODATA)
3394 GOTO(out, rc = xsize);
3396 lu_buf_check_and_alloc(&xbuf, xsize);
3397 if (xbuf.lb_buf == NULL)
3398 GOTO(out, rc = -ENOMEM);
3400 rc = mdo_xattr_get(env, sobj, &xbuf, xname);
3407 cbxbuf.lb_len = xsize;
3409 rc = cb(env, tobj, &cbxbuf, xname, 0, handle);
3410 if (unlikely(rc == -ENOSPC &&
3411 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3412 rc = linkea_overflow_shrink(
3413 (struct linkea_data *)(cbxbuf.lb_buf));
3414 if (likely(rc > 0)) {
3432 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3433 struct mdd_object *sobj,
3434 struct mdd_object *tobj,
3435 const struct lu_name *sname,
3436 const struct lu_fid *sfid,
3437 const struct lu_name *lname,
3438 const struct lu_fid *fid,
3440 struct thandle *handle);
3442 static int mdd_declare_update_link(const struct lu_env *env,
3443 struct mdd_object *sobj,
3444 struct mdd_object *tobj,
3445 const struct lu_name *tname,
3446 const struct lu_fid *tpfid,
3447 const struct lu_name *lname,
3448 const struct lu_fid *fid,
3450 struct thandle *handle)
3452 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3453 struct mdd_object *pobj;
3457 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3458 !strcmp(tname->ln_name, lname->ln_name))
3461 pobj = mdd_object_find(env, mdd, fid);
3463 return PTR_ERR(pobj);
3466 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3468 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3469 mdd_object_type(sobj),
3470 lname->ln_name, handle);
3471 mdd_object_put(env, pobj);
3475 rc = mdo_declare_ref_add(env, tobj, handle);
3479 rc = mdo_declare_ref_del(env, sobj, handle);
3483 static int mdd_update_link(const struct lu_env *env,
3484 struct mdd_object *sobj,
3485 struct mdd_object *tobj,
3486 const struct lu_name *tname,
3487 const struct lu_fid *tpfid,
3488 const struct lu_name *lname,
3489 const struct lu_fid *fid,
3491 struct thandle *handle)
3493 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3494 struct mdd_object *pobj;
3499 LASSERT(lu_name_is_valid(lname));
3502 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3503 !strncmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3506 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3507 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3509 pobj = mdd_object_find(env, mdd, fid);
3511 CWARN("%s: cannot find obj "DFID": %ld\n",
3512 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3513 RETURN(PTR_ERR(pobj));
3516 if (!mdd_object_exists(pobj)) {
3517 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3518 mdd_object_put(env, pobj);
3522 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3523 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3525 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3526 mdd_object_type(sobj),
3527 lname->ln_name, handle);
3528 mdd_write_unlock(env, pobj);
3529 mdd_object_put(env, pobj);
3533 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3534 rc = mdo_ref_add(env, tobj, handle);
3535 mdd_write_unlock(env, tobj);
3539 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3540 rc = mdo_ref_del(env, sobj, handle);
3541 mdd_write_unlock(env, sobj);
3546 static inline int mdd_fld_lookup(const struct lu_env *env,
3547 struct mdd_device *mdd,
3548 const struct lu_fid *fid,
3551 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
3552 struct seq_server_site *ss;
3555 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3557 range->lsr_flags = LU_SEQ_RANGE_MDT;
3558 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3562 *mdt_index = range->lsr_index;
3567 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3568 struct mdd_object *sobj,
3569 struct mdd_object *tobj,
3570 const struct lu_name *tname,
3571 const struct lu_fid *tpfid,
3572 const struct lu_name *lname,
3573 const struct lu_fid *fid,
3575 struct thandle *handle)
3577 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3578 __u32 source_mdt_index = *(__u32 *)opaque;
3579 __u32 link_mdt_index;
3585 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3586 !strcmp(tname->ln_name, lname->ln_name))
3589 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3593 RETURN(link_mdt_index == source_mdt_index);
3596 static int mdd_iterate_linkea(const struct lu_env *env,
3597 struct mdd_object *sobj,
3598 struct mdd_object *tobj,
3599 const struct lu_name *tname,
3600 const struct lu_fid *tpfid,
3601 struct linkea_data *ldata,
3603 struct thandle *handle,
3606 struct mdd_thread_info *info = mdd_env_info(env);
3607 char *filename = info->mti_name;
3608 struct lu_name lname;
3615 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3616 linkea_next_entry(ldata)) {
3617 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3620 /* Note: lname might miss \0 at the end */
3621 snprintf(filename, sizeof(info->mti_name), "%.*s",
3622 lname.ln_namelen, lname.ln_name);
3623 lname.ln_name = filename;
3625 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3627 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3635 * Prepare linkea, and check whether file needs migrate: if source still has
3636 * link on source MDT, no need to migrate, just update namespace on source and
3639 * \retval 0 do migrate
3640 * \retval 1 don't migrate
3641 * \retval -errno on failure
3643 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3644 struct mdd_device *mdd,
3645 struct mdd_object *spobj,
3646 struct mdd_object *tpobj,
3647 struct mdd_object *sobj,
3648 const struct lu_name *sname,
3649 const struct lu_name *tname,
3650 const struct lu_attr *attr,
3651 struct linkea_data *ldata)
3653 __u32 source_mdt_index;
3658 memset(ldata, 0, sizeof(*ldata));
3659 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3660 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3665 * Then it will check if the file should be migrated. If the file has
3666 * mulitple links, we only need migrate the file if all of its entries
3667 * has been migrated to the remote MDT.
3669 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3672 /* If there are still links locally, don't migrate this file */
3673 LASSERT(ldata->ld_leh != NULL);
3676 * If linkEA is overflow, it means there are some unknown name entries
3677 * under unknown parents, which will prevent the migration.
3679 if (unlikely(ldata->ld_leh->leh_overflow_time))
3682 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3686 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3687 ldata, &source_mdt_index, NULL,
3688 mdd_is_link_on_source_mdt);
3692 static int mdd_declare_migrate_update(const struct lu_env *env,
3693 struct mdd_object *spobj,
3694 struct mdd_object *tpobj,
3695 struct mdd_object *obj,
3696 const struct lu_name *sname,
3697 const struct lu_name *tname,
3698 struct lu_attr *attr,
3699 struct lu_attr *spattr,
3700 struct lu_attr *tpattr,
3701 struct linkea_data *ldata,
3703 struct thandle *handle)
3705 struct mdd_thread_info *info = mdd_env_info(env);
3706 struct lu_attr *la = &info->mti_la_for_fix;
3709 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3713 if (S_ISDIR(attr->la_mode)) {
3714 rc = mdo_declare_ref_del(env, spobj, handle);
3719 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3720 attr->la_mode & S_IFMT,
3721 tname->ln_name, handle);
3725 rc = mdd_declare_links_add(env, obj, handle, ldata);
3729 if (S_ISDIR(attr->la_mode)) {
3730 rc = mdo_declare_ref_add(env, tpobj, handle);
3735 la->la_valid = LA_CTIME | LA_MTIME;
3736 rc = mdo_declare_attr_set(env, spobj, la, handle);
3740 if (tpobj != spobj) {
3741 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3749 static int mdd_declare_migrate_create(const struct lu_env *env,
3750 struct mdd_object *spobj,
3751 struct mdd_object *tpobj,
3752 struct mdd_object *sobj,
3753 struct mdd_object *tobj,
3754 const struct lu_name *sname,
3755 const struct lu_name *tname,
3756 struct lu_attr *spattr,
3757 struct lu_attr *tpattr,
3758 struct lu_attr *attr,
3759 struct lu_buf *sbuf,
3760 struct linkea_data *ldata,
3762 struct md_op_spec *spec,
3763 struct dt_allocation_hint *hint,
3764 struct thandle *handle)
3766 struct mdd_thread_info *info = mdd_env_info(env);
3767 struct md_layout_change *mlc = &info->mti_mlc;
3768 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3773 if (S_ISDIR(attr->la_mode)) {
3774 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3776 mlc->mlc_opc = MD_LAYOUT_DETACH;
3777 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3781 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3782 } else if (S_ISLNK(attr->la_mode)) {
3783 spec->u.sp_symname = sbuf->lb_buf;
3784 } else if (S_ISREG(attr->la_mode)) {
3785 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3786 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3789 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3791 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3792 tname, attr, handle, spec, ldata, NULL, NULL,
3798 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3799 * createb yet, copy from sobj.
3801 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3802 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3803 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3805 if (S_ISDIR(attr->la_mode)) {
3807 /* if sobj is not striped, fake a 1-stripe LMV */
3808 LASSERT(sizeof(info->mti_key) >
3809 lmv_mds_md_size(1, LMV_MAGIC_V1));
3810 lmv = (typeof(lmv))info->mti_key;
3811 memset(lmv, 0, sizeof(*lmv));
3812 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3813 lmv->lmv_stripe_count = cpu_to_le32(1);
3814 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3815 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3816 mdd_object_fid(sobj));
3817 mlc->mlc_buf.lb_buf = lmv;
3818 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3820 mlc->mlc_buf = *sbuf;
3822 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3823 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3828 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle,
3829 mdo_declare_xattr_set);
3833 if (S_ISREG(attr->la_mode)) {
3834 struct lu_buf fid_buf;
3836 handle->th_complex = 1;
3838 /* target may be remote, update PFID via sobj. */
3839 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3840 fid_buf.lb_len = sizeof(struct lu_fid);
3841 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3846 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3851 if (!S_ISDIR(attr->la_mode)) {
3852 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3853 mdd_object_fid(tpobj), ldata, NULL,
3854 handle, mdd_declare_update_link);
3859 if (!S_ISDIR(attr->la_mode) || lmv) {
3860 rc = mdo_declare_ref_del(env, sobj, handle);
3864 if (S_ISDIR(attr->la_mode)) {
3865 rc = mdo_declare_ref_del(env, sobj, handle);
3870 rc = mdo_declare_destroy(env, sobj, handle);
3875 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
3876 attr, spattr, tpattr, ldata, ma,
3882 * migrate dirent from \a spobj to \a tpobj.
3884 static int mdd_migrate_update(const struct lu_env *env,
3885 struct mdd_object *spobj,
3886 struct mdd_object *tpobj,
3887 struct mdd_object *obj,
3888 const struct lu_name *sname,
3889 const struct lu_name *tname,
3890 struct lu_attr *attr,
3891 struct lu_attr *spattr,
3892 struct lu_attr *tpattr,
3893 struct linkea_data *ldata,
3895 struct thandle *handle)
3897 struct mdd_thread_info *info = mdd_env_info(env);
3898 struct lu_attr *la = &info->mti_la_for_fix;
3903 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
3904 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
3905 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
3907 rc = __mdd_index_delete(env, spobj, sname->ln_name,
3908 S_ISDIR(attr->la_mode), handle);
3912 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
3913 attr->la_mode & S_IFMT,
3914 tname->ln_name, handle);
3918 rc = mdd_links_write(env, obj, ldata, handle);
3922 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3923 la->la_valid = LA_CTIME | LA_MTIME;
3924 mdd_write_lock(env, spobj, DT_SRC_PARENT);
3925 rc = mdd_update_time(env, spobj, spattr, la, handle);
3926 mdd_write_unlock(env, spobj);
3930 if (tpobj != spobj) {
3931 la->la_valid = LA_CTIME | LA_MTIME;
3932 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
3933 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
3934 mdd_write_unlock(env, tpobj);
3943 * Migrate file/dir to target MDT.
3945 * Create target according to \a spec, and then migrate xattrs, if it's
3946 * directory, migrate source stripes to target.
3948 * \param[in] env execution environment
3949 * \param[in] spobj source parent object
3950 * \param[in] tpobj target parent object
3951 * \param[in] sobj source object
3952 * \param[in] tobj target object
3953 * \param[in] lname file name
3954 * \param[in] spattr source parent attributes
3955 * \param[in] tpattr target parent attributes
3956 * \param[in] attr source attributes
3957 * \param[in] sbuf source LMV buf
3958 * \param[in] spec migrate create spec
3959 * \param[in] hint target creation hint
3960 * \param[in] handle tranasction handle
3962 * \retval 0 on success
3963 * \retval -errno on failure
3965 static int mdd_migrate_create(const struct lu_env *env,
3966 struct mdd_object *spobj,
3967 struct mdd_object *tpobj,
3968 struct mdd_object *sobj,
3969 struct mdd_object *tobj,
3970 const struct lu_name *sname,
3971 const struct lu_name *tname,
3972 struct lu_attr *spattr,
3973 struct lu_attr *tpattr,
3974 struct lu_attr *attr,
3975 const struct lu_buf *sbuf,
3976 struct linkea_data *ldata,
3978 struct md_op_spec *spec,
3979 struct dt_allocation_hint *hint,
3980 struct thandle *handle)
3987 * migrate sobj stripes to tobj if it's directory:
3988 * 1. detach stripes from sobj.
3989 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
3990 * 3. create stripes for tobj, see lod_xattr_set_lmv().
3992 if (S_ISDIR(attr->la_mode)) {
3993 struct mdd_thread_info *info = mdd_env_info(env);
3994 struct md_layout_change *mlc = &info->mti_mlc;
3996 mlc->mlc_opc = MD_LAYOUT_DETACH;
3998 mdd_write_lock(env, sobj, DT_SRC_PARENT);
3999 rc = mdo_layout_change(env, sobj, mlc, handle);
4000 mdd_write_unlock(env, sobj);
4005 /* don't set nlink from sobj */
4006 attr->la_valid &= ~LA_NLINK;
4008 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4009 hint, handle, false);
4013 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4014 rc = mdd_iterate_xattrs(env, sobj, tobj, true, handle, mdo_xattr_set);
4015 mdd_write_unlock(env, tobj);
4019 /* for regular file, update OST objects XATTR_NAME_FID */
4020 if (S_ISREG(attr->la_mode)) {
4021 struct lu_buf fid_buf;
4023 /* target may be remote, update PFID via sobj. */
4024 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4025 fid_buf.lb_len = sizeof(struct lu_fid);
4026 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4031 /* delete LOV to avoid deleting OST objs when destroying sobj */
4032 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4033 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4034 mdd_write_unlock(env, sobj);
4035 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4036 if (rc && rc != -ENODATA)
4041 /* update links FID */
4042 if (!S_ISDIR(attr->la_mode)) {
4043 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4044 mdd_object_fid(tpobj), ldata,
4045 NULL, handle, mdd_update_link);
4050 /* don't destroy sobj if it's plain directory */
4051 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4052 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4053 rc = mdo_ref_del(env, sobj, handle);
4055 if (S_ISDIR(attr->la_mode))
4056 rc = mdo_ref_del(env, sobj, handle);
4058 rc = mdo_destroy(env, sobj, handle);
4060 mdd_write_unlock(env, sobj);
4065 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4066 spattr, tpattr, ldata, ma, handle);
4071 /* NB: if user issued different migrate command, we can't adjust it silently
4072 * here, because this command will decide target MDT in subdir migration in
4075 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4076 const struct lmv_mds_md_v1 *lmv,
4077 const struct lmv_user_md_v1 *lum,
4078 const struct lu_name *lname)
4080 __u32 lum_stripe_count = lum->lum_stripe_count;
4081 __u32 lmv_hash_type = lmv->lmv_hash_type;
4083 if (!lmv_is_sane(lmv))
4086 /* if stripe_count unspecified, set to 1 */
4087 if (!lum_stripe_count)
4088 lum_stripe_count = cpu_to_le32(1);
4090 lmv_hash_type &= ~cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4092 /* TODO: check specific MDTs */
4093 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4094 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4095 (lum->lum_hash_type && lum->lum_hash_type != lmv_hash_type)) {
4096 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4097 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4098 le32_to_cpu(lmv->lmv_master_mdt_index),
4099 le32_to_cpu(lmv->lmv_migrate_offset),
4100 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4109 * Internal function to migrate directory or file between MDTs.
4111 * migrate source to target in following steps:
4112 * 1. create target, append source stripes after target's if it's directory,
4113 * migrate xattrs and update fid of source links.
4114 * 2. update namespace: migrate dirent from source parent to target parent,
4115 * update file linkea, and destroy source if it's not needed any more.
4117 * \param[in] env execution environment
4118 * \param[in] spobj source parent object
4119 * \param[in] tpobj target parent object
4120 * \param[in] sobj source object
4121 * \param[in] tobj target object
4122 * \param[in] sname source file name
4123 * \param[in] tname target file name
4124 * \param[in] spec target creation spec
4125 * \param[in] ma used to update \a pobj mtime and ctime
4127 * \retval 0 on success
4128 * \retval -errno on failure
4130 static int mdd_migrate_object(const struct lu_env *env,
4131 struct mdd_object *spobj,
4132 struct mdd_object *tpobj,
4133 struct mdd_object *sobj,
4134 struct mdd_object *tobj,
4135 const struct lu_name *sname,
4136 const struct lu_name *tname,
4137 struct md_op_spec *spec,
4140 struct mdd_thread_info *info = mdd_env_info(env);
4141 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4142 struct lu_attr *spattr = &info->mti_pattr;
4143 struct lu_attr *tpattr = &info->mti_tpattr;
4144 struct lu_attr *attr = &info->mti_cattr;
4145 struct linkea_data *ldata = &info->mti_link_data;
4146 struct dt_allocation_hint *hint = &info->mti_hint;
4147 struct lu_buf sbuf = { NULL };
4148 struct lmv_mds_md_v1 *lmv;
4149 struct thandle *handle;
4154 rc = mdd_la_get(env, sobj, attr);
4158 rc = mdd_la_get(env, spobj, spattr);
4162 rc = mdd_la_get(env, tpobj, tpattr);
4166 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4167 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4171 /* if user use default value '0' for stripe_count, we need to
4172 * adjust it to '1' to create a 1-stripe directory.
4174 if (lum->lum_stripe_count == 0)
4175 lum->lum_stripe_count = cpu_to_le32(1);
4177 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4178 if (rc && rc != -ENODATA)
4183 if (!lmv_is_sane(lmv))
4184 GOTO(out, rc = -EBADF);
4185 if (lmv_is_migrating(lmv)) {
4186 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4191 } else if (!S_ISDIR(attr->la_mode)) {
4193 GOTO(out, rc = -EALREADY);
4195 /* update namespace only if @sobj is on MDT where @tpobj is. */
4196 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4197 spec->sp_migrate_nsonly = true;
4199 if (S_ISLNK(attr->la_mode)) {
4200 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4202 GOTO(out, rc = -ENOMEM);
4204 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4207 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4208 mdd2obd_dev(mdd)->obd_name,
4209 PFID(mdd_object_fid(sobj)), rc);
4215 /* linkea needs update upon FID or parent stripe change */
4216 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4217 tname, attr, ldata);
4219 /* update namespace only if @sobj has link on its MDT. */
4220 spec->sp_migrate_nsonly = true;
4224 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4225 spattr, tpattr, attr);
4229 handle = mdd_trans_create(env, mdd);
4231 GOTO(out, rc = PTR_ERR(handle));
4233 if (spec->sp_migrate_nsonly)
4234 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4235 tname, attr, spattr, tpattr,
4238 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4239 sname, tname, spattr, tpattr,
4240 attr, &sbuf, ldata, ma, spec,
4245 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4250 rc = mdd_trans_start(env, mdd, handle);
4254 if (spec->sp_migrate_nsonly)
4255 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4256 attr, spattr, tpattr, ldata, ma,
4259 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4260 tname, spattr, tpattr, attr, &sbuf,
4261 ldata, ma, spec, hint, handle);
4265 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4266 spec->sp_migrate_nsonly ? sobj : tobj,
4267 mdd_object_fid(spobj), mdd_object_fid(sobj),
4268 mdd_object_fid(tpobj), tname, sname,
4275 rc = mdd_trans_stop(env, mdd, rc, handle);
4283 * Migrate directory or file between MDTs.
4285 * \param[in] env execution environment
4286 * \param[in] md_pobj parent master object
4287 * \param[in] md_sobj source object
4288 * \param[in] lname file name
4289 * \param[in] md_tobj target object
4290 * \param[in] spec target creation spec
4291 * \param[in] ma used to update \a pobj mtime and ctime
4293 * \retval 0 on success
4294 * \retval -errno on failure
4296 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4297 struct md_object *md_sobj, const struct lu_name *lname,
4298 struct md_object *md_tobj, struct md_op_spec *spec,
4301 struct mdd_thread_info *info = mdd_env_info(env);
4302 struct mdd_device *mdd = mdo2mdd(md_pobj);
4303 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4304 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4305 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4306 struct mdd_object *spobj = NULL;
4307 struct mdd_object *tpobj = NULL;
4308 struct lu_buf pbuf = { NULL };
4309 struct lu_fid *fid = &info->mti_fid2;
4310 struct lmv_mds_md_v1 *lmv;
4315 /* locate source and target stripe on pobj, which are the real parent */
4316 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4317 if (rc < 0 && rc != -ENODATA)
4324 if (!lmv_is_sane(lmv))
4325 GOTO(out, rc = -EBADF);
4327 /* locate target parent stripe */
4328 /* fail check here to make sure top dir migration succeed. */
4329 if (lmv_is_migrating(lmv) &&
4330 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4331 GOTO(out, rc = -EIO);
4333 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4336 GOTO(out, rc = index);
4338 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4339 tpobj = mdd_object_find(env, mdd, fid);
4341 GOTO(out, rc = PTR_ERR(tpobj));
4343 /* locate source parent stripe */
4344 if (lmv_is_layout_changing(lmv)) {
4345 index = lmv_name_to_stripe_index_old(lmv,
4349 GOTO(out, rc = index);
4351 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4352 spobj = mdd_object_find(env, mdd, fid);
4354 GOTO(out, rc = PTR_ERR(spobj));
4356 /* parent stripe unchanged */
4357 if (spobj == tpobj) {
4358 if (!lmv_is_restriping(lmv))
4359 GOTO(out, rc = -EINVAL);
4360 GOTO(out, rc = -EALREADY);
4364 mdd_object_get(spobj);
4369 mdd_object_get(tpobj);
4370 mdd_object_get(spobj);
4373 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4378 if (!IS_ERR_OR_NULL(spobj))
4379 mdd_object_put(env, spobj);
4380 if (!IS_ERR_OR_NULL(tpobj))
4381 mdd_object_put(env, tpobj);
4387 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4388 struct mdd_object *pobj,
4389 struct mdd_object *obj,
4390 struct mdd_object *stripe,
4391 struct lu_attr *attr,
4392 struct md_layout_change *mlc,
4393 struct lu_name *lname,
4394 struct thandle *handle)
4398 mlc->mlc_opc = MD_LAYOUT_DETACH;
4399 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4403 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4404 S_IFDIR, dotdot, handle);
4408 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle,
4409 mdo_declare_xattr_set);
4413 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4417 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4421 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4425 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4426 attr->la_mode, lname->ln_name, handle);
4430 rc = mdo_declare_ref_del(env, obj, handle);
4434 rc = mdo_declare_ref_del(env, obj, handle);
4438 rc = mdo_declare_destroy(env, obj, handle);
4445 /* transform one-stripe directory to a plain directory */
4446 static int mdd_1sd_collapse(const struct lu_env *env,
4447 struct mdd_object *pobj,
4448 struct mdd_object *obj,
4449 struct mdd_object *stripe,
4450 struct lu_attr *attr,
4451 struct md_layout_change *mlc,
4452 struct lu_name *lname,
4453 struct thandle *handle)
4459 /* replace 1-stripe directory with its stripe */
4460 mlc->mlc_opc = MD_LAYOUT_DETACH;
4462 mdd_write_lock(env, obj, DT_SRC_PARENT);
4463 rc = mdo_layout_change(env, obj, mlc, handle);
4464 mdd_write_unlock(env, obj);
4468 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4469 mdd_write_lock(env, obj, DT_SRC_CHILD);
4471 /* insert dotdot to stripe which points to parent */
4472 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4473 S_IFDIR, dotdot, handle);
4477 /* copy xattrs including linkea */
4478 rc = mdd_iterate_xattrs(env, obj, stripe, false, handle, mdo_xattr_set);
4483 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4487 /* don't set nlink from parent */
4488 attr->la_valid &= ~LA_NLINK;
4490 rc = mdo_attr_set(env, stripe, attr, handle);
4494 /* delete dir name from parent */
4495 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4499 /* insert stripe to parent with dir name */
4500 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4501 attr->la_mode, lname->ln_name, handle);
4505 /* destroy dir obj */
4506 rc = mdo_ref_del(env, obj, handle);
4510 rc = mdo_ref_del(env, obj, handle);
4514 rc = mdo_destroy(env, obj, handle);
4520 mdd_write_unlock(env, obj);
4521 mdd_write_unlock(env, pobj);
4527 * shrink directory stripes after migration/merge
4529 int mdd_dir_layout_shrink(const struct lu_env *env,
4530 struct md_object *md_obj,
4531 struct md_layout_change *mlc)
4533 struct mdd_device *mdd = mdo2mdd(md_obj);
4534 struct mdd_thread_info *info = mdd_env_info(env);
4535 struct mdd_object *obj = md2mdd_obj(md_obj);
4536 struct mdd_object *pobj = NULL;
4537 struct mdd_object *stripe = NULL;
4538 struct lu_attr *attr = &info->mti_pattr;
4539 struct lu_fid *fid = &info->mti_fid2;
4540 struct lu_name lname = { NULL };
4541 struct lu_buf lmv_buf = { NULL };
4542 struct lmv_mds_md_v1 *lmv;
4543 struct lmv_user_md *lmu;
4544 struct thandle *handle;
4549 rc = mdd_la_get(env, obj, attr);
4553 if (!S_ISDIR(attr->la_mode))
4556 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4560 lmv = lmv_buf.lb_buf;
4561 if (!lmv_is_sane(lmv))
4564 lmu = mlc->mlc_buf.lb_buf;
4566 /* adjust the default value '0' to '1' */
4567 if (lmu->lum_stripe_count == 0)
4568 lmu->lum_stripe_count = cpu_to_le32(1);
4570 /* these were checked in MDT */
4571 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4572 le32_to_cpu(lmv->lmv_stripe_count));
4573 LASSERT(!lmv_is_splitting(lmv));
4574 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4576 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4577 * to a plain dir, which will cause FID change and namespace update.
4579 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4580 struct linkea_data *ldata = &info->mti_link_data;
4581 char *filename = info->mti_name;
4583 rc = mdd_links_read(env, obj, ldata);
4587 if (ldata->ld_leh->leh_reccount > 1)
4588 GOTO(out, rc = -EINVAL);
4590 linkea_first_entry(ldata);
4592 GOTO(out, rc = -ENODATA);
4594 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4597 /* Note: lname might miss \0 at the end */
4598 snprintf(filename, sizeof(info->mti_name), "%.*s",
4599 lname.ln_namelen, lname.ln_name);
4600 lname.ln_name = filename;
4602 pobj = mdd_object_find(env, mdd, fid);
4609 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4611 stripe = mdd_object_find(env, mdd, fid);
4612 if (IS_ERR(stripe)) {
4613 mdd_object_put(env, pobj);
4615 GOTO(out, rc = PTR_ERR(stripe));
4619 handle = mdd_trans_create(env, mdd);
4621 GOTO(out, rc = PTR_ERR(handle));
4623 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4624 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4626 GOTO(stop_trans, rc);
4628 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4629 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr, mlc,
4632 GOTO(stop_trans, rc);
4635 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4638 GOTO(stop_trans, rc);
4640 rc = mdd_trans_start(env, mdd, handle);
4642 GOTO(stop_trans, rc);
4644 mdd_write_lock(env, obj, DT_SRC_PARENT);
4645 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4646 rc = mdo_layout_change(env, obj, mlc, handle);
4647 mdd_write_unlock(env, obj);
4649 GOTO(stop_trans, rc);
4651 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4652 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, mlc, &lname,
4655 GOTO(stop_trans, rc);
4658 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4659 XATTR_NAME_LMV, handle);
4660 GOTO(stop_trans, rc);
4663 rc = mdd_trans_stop(env, mdd, rc, handle);
4666 mdd_object_put(env, stripe);
4667 mdd_object_put(env, pobj);
4669 lu_buf_free(&lmv_buf);
4673 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4674 struct mdd_device *mdd,
4675 struct mdd_object *pobj,
4676 struct mdd_object *obj,
4677 struct mdd_object *tobj,
4678 struct md_layout_change *mlc,
4679 struct dt_allocation_hint *hint,
4680 struct thandle *handle)
4682 struct mdd_thread_info *info = mdd_env_info(env);
4683 const struct lu_name *lname = mlc->mlc_name;
4684 struct lu_attr *la = &info->mti_la_for_fix;
4685 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4686 struct linkea_data *ldata = &info->mti_link_data;
4687 struct lmv_mds_md_v1 *lmv;
4691 mlc->mlc_opc = MD_LAYOUT_DETACH;
4692 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4696 memset(ldata, 0, sizeof(*ldata));
4697 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4698 lname, 1, 0, ldata);
4702 count = lum->lum_stripe_count;
4703 lum->lum_stripe_count = 0;
4704 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4706 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4707 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4708 ldata, NULL, NULL, NULL, hint);
4712 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4715 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4717 lmv = (typeof(lmv))info->mti_key;
4718 memset(lmv, 0, sizeof(*lmv));
4719 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4720 lmv->lmv_stripe_count = cpu_to_le32(1);
4721 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4722 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4724 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4725 mlc->mlc_buf.lb_buf = lmv;
4726 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4727 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4731 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle,
4732 mdo_declare_xattr_set);
4736 lum->lum_stripe_count = count;
4737 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4738 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4742 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4746 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4747 S_IFDIR, lname->ln_name, handle);
4751 la->la_valid = LA_CTIME | LA_MTIME;
4752 rc = mdo_declare_attr_set(env, obj, la, handle);
4756 rc = mdo_declare_attr_set(env, pobj, la, handle);
4760 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4766 * plain directory split:
4767 * 1. create \a tobj as plain directory.
4768 * 2. append \a obj as first stripe of \a tobj.
4769 * 3. migrate xattrs from \a obj to \a tobj.
4770 * 4. split \a tobj to specific stripe count.
4772 static int mdd_dir_split_plain(const struct lu_env *env,
4773 struct mdd_device *mdd,
4774 struct mdd_object *pobj,
4775 struct mdd_object *obj,
4776 struct mdd_object *tobj,
4777 struct md_layout_change *mlc,
4778 struct dt_allocation_hint *hint,
4779 struct thandle *handle)
4781 struct mdd_thread_info *info = mdd_env_info(env);
4782 struct lu_attr *pattr = &info->mti_pattr;
4783 struct lu_attr *la = &info->mti_la_for_fix;
4784 const struct lu_name *lname = mlc->mlc_name;
4785 struct linkea_data *ldata = &info->mti_link_data;
4790 /* copy linkea out and set on target later */
4791 rc = mdd_links_read(env, obj, ldata);
4795 mlc->mlc_opc = MD_LAYOUT_DETACH;
4796 rc = mdo_layout_change(env, obj, mlc, handle);
4800 /* don't set nlink from obj */
4801 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4803 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4804 NULL, NULL, NULL, hint, handle, false);
4808 rc = mdd_iterate_xattrs(env, obj, tobj, true, handle, mdo_xattr_set);
4812 rc = mdd_links_write(env, tobj, ldata, handle);
4816 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4820 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4821 lname->ln_name, handle);
4825 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4826 la->la_valid = LA_CTIME | LA_MTIME;
4828 mdd_write_lock(env, obj, DT_SRC_CHILD);
4829 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4830 mdd_write_unlock(env, obj);
4834 rc = mdd_la_get(env, pobj, pattr);
4838 la->la_valid = LA_CTIME | LA_MTIME;
4840 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4841 rc = mdd_update_time(env, pobj, pattr, la, handle);
4842 mdd_write_unlock(env, pobj);
4846 /* FID changes, record it as CL_MIGRATE */
4847 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
4848 mdd_object_fid(pobj), mdd_object_fid(obj),
4849 mdd_object_fid(pobj), lname, lname, handle);
4853 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
4854 struct md_layout_change *mlc)
4856 struct mdd_thread_info *info = mdd_env_info(env);
4857 struct mdd_device *mdd = mdo2mdd(o);
4858 struct mdd_object *obj = md2mdd_obj(o);
4859 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
4860 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
4861 struct dt_allocation_hint *hint = &info->mti_hint;
4862 bool is_plain = false;
4863 struct thandle *handle;
4868 LASSERT(S_ISDIR(mdd_object_type(obj)));
4870 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
4876 handle = mdd_trans_create(env, mdd);
4878 RETURN(PTR_ERR(handle));
4881 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj, mlc,
4884 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4885 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4887 GOTO(stop_trans, rc);
4889 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
4893 GOTO(stop_trans, rc);
4895 rc = mdd_trans_start(env, mdd, handle);
4897 GOTO(stop_trans, rc);
4900 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, mlc, hint,
4903 mdd_write_lock(env, obj, DT_TGT_CHILD);
4904 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
4905 LU_XATTR_CREATE, handle);
4906 mdd_write_unlock(env, obj);
4908 GOTO(stop_trans, rc);
4910 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4911 XATTR_NAME_LMV, handle);
4914 GOTO(stop_trans, rc);
4919 rc = mdd_trans_stop(env, mdd, rc, handle);
4924 const struct md_dir_operations mdd_dir_ops = {
4925 .mdo_is_subdir = mdd_is_subdir,
4926 .mdo_lookup = mdd_lookup,
4927 .mdo_create = mdd_create,
4928 .mdo_rename = mdd_rename,
4929 .mdo_link = mdd_link,
4930 .mdo_unlink = mdd_unlink,
4931 .mdo_create_data = mdd_create_data,
4932 .mdo_migrate = mdd_migrate,