4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdd/mdd_dir.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@intel.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_mds.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_idmap.h>
47 #include "mdd_internal.h"
49 static const char dot[] = ".";
50 static const char dotdot[] = "..";
52 static struct lu_name lname_dotdot = {
53 .ln_name = (char *) dotdot,
54 .ln_namelen = sizeof(dotdot) - 1,
58 mdd_name_check(const struct lu_env *env, struct mdd_device *m,
59 const struct lu_name *ln)
61 struct mdd_thread_info *info = mdd_env_info(env);
62 bool enc = info->mdi_pattr.la_valid & LA_FLAGS &&
63 info->mdi_pattr.la_flags & LUSTRE_ENCRYPT_FL;
65 if (!lu_name_is_valid(ln))
67 else if (!enc && ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
73 /* Get FID from name and parent */
75 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
76 const struct lu_attr *pattr, const struct lu_name *lname,
77 struct lu_fid *fid, unsigned int may_mask)
79 const char *name = lname->ln_name;
80 const struct dt_key *key = (const struct dt_key *)name;
81 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
82 struct dt_object *dir = mdd_object_child(mdd_obj);
87 if (unlikely(mdd_is_dead_obj(mdd_obj)))
90 if (!mdd_object_exists(mdd_obj))
93 if (mdd_object_remote(mdd_obj)) {
94 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
95 mdd_obj_dev_name(mdd_obj),
96 PFID(mdd_object_fid(mdd_obj)));
99 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
104 if (likely(dt_try_as_dir(env, dir, true)))
105 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
112 int mdd_lookup(const struct lu_env *env,
113 struct md_object *pobj, const struct lu_name *lname,
114 struct lu_fid *fid, struct md_op_spec *spec)
116 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
120 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
124 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
125 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
129 /** Read the link EA into a temp buffer.
130 * Uses the mdd_thread_info::mdi_link_buf since it is generally large.
131 * A pointer to the buffer is stored in \a ldata::ld_buf.
135 static int __mdd_links_read(const struct lu_env *env,
136 struct mdd_object *mdd_obj,
137 struct linkea_data *ldata)
141 if (!mdd_object_exists(mdd_obj))
144 /* First try a small buf */
145 LASSERT(env != NULL);
146 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_link_buf,
148 if (ldata->ld_buf->lb_buf == NULL)
151 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
153 /* Buf was too small, figure out what we need. */
154 lu_buf_free(ldata->ld_buf);
155 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
159 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
160 if (ldata->ld_buf->lb_buf == NULL)
162 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
166 lu_buf_free(ldata->ld_buf);
167 ldata->ld_buf = NULL;
171 return linkea_init(ldata);
174 int mdd_links_read(const struct lu_env *env,
175 struct mdd_object *mdd_obj,
176 struct linkea_data *ldata)
180 rc = __mdd_links_read(env, mdd_obj, ldata);
182 rc = linkea_init(ldata);
187 static int mdd_links_read_with_rec(const struct lu_env *env,
188 struct mdd_object *mdd_obj,
189 struct linkea_data *ldata)
193 rc = __mdd_links_read(env, mdd_obj, ldata);
195 rc = linkea_init_with_rec(ldata);
201 * Get parent FID of the directory
203 * Read parent FID from linkEA, if that fails, then do lookup
204 * dotdot to get the parent FID.
206 * \param[in] env execution environment
207 * \param[in] obj object from which to find the parent FID
208 * \param[in] attr attribute of the object
209 * \param[out] fid fid to get the parent FID
211 * \retval 0 if getting the parent FID succeeds.
212 * \retval negative errno if getting the parent FID fails.
214 static inline int mdd_parent_fid(const struct lu_env *env,
215 struct mdd_object *obj,
216 const struct lu_attr *attr,
219 struct mdd_thread_info *info = mdd_env_info(env);
220 struct linkea_data ldata = { NULL };
221 struct lu_buf *buf = &info->mdi_link_buf;
222 struct lu_name lname;
227 LASSERTF(S_ISDIR(mdd_object_type(obj)),
228 "%s: FID "DFID" is not a directory type = %o\n",
229 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
230 mdd_object_type(obj));
232 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
233 if (buf->lb_buf == NULL)
234 GOTO(lookup, rc = 0);
237 rc = mdd_links_read_with_rec(env, obj, &ldata);
241 /* the obj is not locked, don't cache attributes */
242 mdd_invalidate(env, &obj->mod_obj);
244 LASSERT(ldata.ld_leh != NULL);
245 /* Directory should only have 1 parent */
246 if (ldata.ld_leh->leh_reccount > 1)
249 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
251 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
252 if (likely(fid_is_sane(fid)))
255 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
260 * For root fid use special function, which does not compare version component
261 * of fid. Version component is different for root fids on all MDTs.
263 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
265 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
266 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
270 * return 1: if \a tfid is the fid of the ancestor of \a mo;
272 * otherwise: values < 0, errors.
274 static int mdd_is_parent(const struct lu_env *env,
275 struct mdd_device *mdd,
276 struct mdd_object *mo,
277 const struct lu_attr *attr,
278 const struct lu_fid *tfid)
280 struct mdd_object *mp;
284 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
285 pfid = &mdd_env_info(env)->mdi_fid;
287 if (mdd_is_root(mdd, mdd_object_fid(mo)))
290 if (mdd_is_root(mdd, tfid))
293 rc = mdd_parent_fid(env, mo, attr, pfid);
298 if (lu_fid_eq(pfid, tfid))
301 if (mdd_is_root(mdd, pfid))
304 mp = mdd_object_find(env, mdd, pfid);
308 if (!mdd_object_exists(mp)) {
309 mdd_object_put(env, mp);
313 rc = mdd_parent_fid(env, mp, attr, pfid);
314 mdd_object_put(env, mp);
323 * No permission check is needed.
325 * returns 1: if fid is ancestor of @mo;
326 * returns 0: if fid is not an ancestor of @mo;
327 * returns < 0: if error
329 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
330 const struct lu_fid *fid)
332 struct mdd_device *mdd = mdo2mdd(mo);
333 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
337 if (!mdd_object_exists(md2mdd_obj(mo)))
340 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
343 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
347 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
352 * Check that @dir contains no entries except (possibly) dot and dotdot.
357 * -ENOTDIR not a directory object
358 * -ENOTEMPTY not empty
362 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
365 struct dt_object *obj;
366 const struct dt_it_ops *iops;
370 obj = mdd_object_child(dir);
371 if (!dt_try_as_dir(env, obj, true))
374 iops = &obj->do_index_ops->dio_it;
375 it = iops->init(env, obj, LUDA_64BITHASH);
377 result = iops->get(env, it, (const struct dt_key *)"");
380 for (result = 0, i = 0; result == 0 && i < 3; ++i)
381 result = iops->next(env, it);
384 else if (result == 1)
386 } else if (result == 0)
388 * Huh? Index contains no zero key?
395 result = PTR_ERR(it);
396 /* -ENODEV means no valid stripe */
397 if (result == -ENODEV)
404 * Determine if the target object can be hard linked, and right now it only
405 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
406 * directory nlink count might exceed the maximum link count(see
407 * osd_object_ref_add), so it only check nlink for non-directories.
409 * \param[in] env thread environment
410 * \param[in] obj object being linked to
411 * \param[in] la attributes of \a obj
413 * \retval 0 if \a obj can be hard linked
414 * \retval negative error if \a obj is a directory or has too
417 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
418 const struct lu_attr *la)
420 struct mdd_device *m = mdd_obj2mdd_dev(obj);
425 /* Subdir count limitation can be broken through
426 * (see osd_object_ref_add), so only check non-directory here. */
427 if (!S_ISDIR(la->la_mode) &&
428 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
435 * Check whether it may create the cobj under the pobj.
437 * \param[in] env execution environment
438 * \param[in] pobj the parent directory
439 * \param[in] pattr the attribute of the parent directory
440 * \param[in] cobj the child to be created
441 * \param[in] check_perm if check WRITE|EXEC permission for parent
443 * \retval = 0 create the child under this dir is allowed
444 * \retval negative errno create the child under this dir is
447 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
448 const struct lu_attr *pattr, struct mdd_object *cobj,
454 if (cobj && mdd_object_exists(cobj))
457 if (mdd_is_dead_obj(pobj))
461 rc = mdd_permission_internal_locked(env, pobj, pattr,
462 MAY_WRITE | MAY_EXEC,
468 * Check whether can unlink from the pobj in the case of "cobj == NULL".
470 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
471 const struct lu_attr *pattr, const struct lu_attr *attr)
476 if (mdd_is_dead_obj(pobj))
479 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
482 rc = mdd_permission_internal_locked(env, pobj, pattr,
483 MAY_WRITE | MAY_EXEC,
488 if (pattr->la_flags & LUSTRE_APPEND_FL)
495 * pobj == NULL is remote ops case, under such case, pobj's
496 * VTX feature has been checked already, no need check again.
498 static inline int mdd_is_sticky(const struct lu_env *env,
499 struct mdd_object *pobj,
500 const struct lu_attr *pattr,
501 struct mdd_object *cobj,
502 const struct lu_attr *cattr)
504 struct lu_ucred *uc = lu_ucred_assert(env);
507 LASSERT(pattr != NULL);
508 if (!(pattr->la_mode & S_ISVTX) ||
509 (pattr->la_uid == uc->uc_fsuid))
513 LASSERT(cattr != NULL);
514 if (cattr->la_uid == uc->uc_fsuid)
517 return !cap_raised(uc->uc_cap, CAP_FOWNER);
520 static int mdd_may_delete_entry(const struct lu_env *env,
521 struct mdd_object *pobj,
522 const struct lu_attr *pattr,
527 LASSERT(pobj != NULL);
528 if (!mdd_object_exists(pobj))
531 if (mdd_is_dead_obj(pobj))
536 rc = mdd_permission_internal_locked(env, pobj, pattr,
537 MAY_WRITE | MAY_EXEC,
543 if (pattr->la_flags & LUSTRE_APPEND_FL)
550 * Check whether it may delete the cobj from the pobj.
553 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
554 const struct lu_attr *tpattr, struct mdd_object *tobj,
555 const struct lu_attr *tattr, const struct lu_attr *cattr,
556 int check_perm, int check_empty)
562 LASSERT(tpattr != NULL);
563 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
571 if (!mdd_object_exists(tobj))
574 if (mdd_is_dead_obj(tobj))
577 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
580 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
583 /* additional check the rename case */
585 if (S_ISDIR(cattr->la_mode)) {
586 if (!S_ISDIR(tattr->la_mode))
589 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
590 mdd_object_fid(tobj)))
592 } else if (S_ISDIR(tattr->la_mode))
596 if (S_ISDIR(tattr->la_mode) && check_empty)
597 rc = mdd_dir_is_empty(env, tobj);
603 * Check whether it can create the link file(linked to @src_obj) under
604 * the target directory(@tgt_obj), and src_obj has been locked by
607 * \param[in] env execution environment
608 * \param[in] tgt_obj the target directory
609 * \param[in] tattr attributes of target directory
610 * \param[in] lname the link name
611 * \param[in] src_obj source object for link
612 * \param[in] cattr attributes for source object
614 * \retval = 0 it is allowed to create the link file under tgt_obj
615 * \retval negative error not allowed to create the link file
617 static int mdd_link_sanity_check(const struct lu_env *env,
618 struct mdd_object *tgt_obj,
619 const struct lu_attr *tattr,
620 const struct lu_name *lname,
621 struct mdd_object *src_obj,
622 const struct lu_attr *cattr)
624 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
628 if (!mdd_object_exists(src_obj))
631 if (mdd_is_dead_obj(src_obj))
634 /* Local ops, no lookup before link, check filename length here. */
635 rc = mdd_name_check(env, m, lname);
639 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
642 if (S_ISDIR(mdd_object_type(src_obj)))
645 LASSERT(src_obj != tgt_obj);
646 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
650 rc = __mdd_may_link(env, src_obj, cattr);
655 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
656 const char *name, struct thandle *handle)
658 struct dt_object *next = mdd_object_child(pobj);
662 if (dt_try_as_dir(env, next, true))
663 rc = dt_delete(env, next, (struct dt_key *)name, handle);
670 static int __mdd_index_insert_only(const struct lu_env *env,
671 struct mdd_object *pobj,
672 const struct lu_fid *lf, __u32 type,
673 const char *name, struct thandle *handle)
675 struct dt_object *next = mdd_object_child(pobj);
679 if (dt_try_as_dir(env, next, true)) {
680 struct dt_insert_rec *rec = &mdd_env_info(env)->mdi_dt_rec;
683 rec->rec_type = type;
684 rc = dt_insert(env, next, (const struct dt_rec *)rec,
685 (const struct dt_key *)name, handle);
692 /* insert named index, add reference if isdir */
693 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
694 const struct lu_fid *lf, __u32 type,
695 const char *name, struct thandle *handle)
700 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
701 if (rc == 0 && S_ISDIR(type)) {
702 mdd_write_lock(env, pobj, DT_TGT_PARENT);
703 mdo_ref_add(env, pobj, handle);
704 mdd_write_unlock(env, pobj);
710 /* delete named index, drop reference if isdir */
711 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
712 const char *name, int is_dir,
713 struct thandle *handle)
718 rc = __mdd_index_delete_only(env, pobj, name, handle);
719 if (rc == 0 && is_dir) {
720 mdd_write_lock(env, pobj, DT_TGT_PARENT);
721 mdo_ref_del(env, pobj, handle);
722 mdd_write_unlock(env, pobj);
728 static int mdd_llog_record_calc_size(const struct lu_env *env,
729 const struct lu_name *tname,
730 const struct lu_name *sname)
732 const struct lu_ucred *uc = lu_ucred(env);
733 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
734 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
737 clf_flags |= CLF_RENAME;
739 if (uc != NULL && uc->uc_jobid[0] != '\0')
740 clf_flags |= CLF_JOBID;
742 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
743 changelog_rec_offset(clf_flags, crfe) +
744 (tname != NULL ? tname->ln_namelen : 0) +
745 (sname != NULL ? 1 + sname->ln_namelen : 0));
748 int mdd_declare_changelog_store(const struct lu_env *env,
749 struct mdd_device *mdd,
750 enum changelog_rec_type type,
751 const struct lu_name *tname,
752 const struct lu_name *sname,
753 struct thandle *handle)
755 struct obd_device *obd = mdd2obd_dev(mdd);
756 struct llog_ctxt *ctxt;
757 struct llog_rec_hdr rec_hdr;
758 struct thandle *llog_th;
761 if (!mdd_changelog_enabled(env, mdd, type))
764 rec_hdr.lrh_len = mdd_llog_record_calc_size(env, tname, sname);
765 rec_hdr.lrh_type = CHANGELOG_REC;
767 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
771 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
773 GOTO(out_put, rc = PTR_ERR(llog_th));
775 rc = llog_declare_add(env, ctxt->loc_handle, &rec_hdr, llog_th);
783 int mdd_changelog_write_rec(const struct lu_env *env,
784 struct llog_handle *loghandle,
785 struct llog_rec_hdr *r,
786 struct llog_cookie *cookie,
787 int idx, struct thandle *th)
791 if (r->lrh_type == CHANGELOG_REC) {
792 struct mdd_device *mdd;
793 struct llog_changelog_rec *rec;
795 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
796 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
798 spin_lock(&mdd->mdd_cl.mc_lock);
799 rec->cr.cr_index = mdd->mdd_cl.mc_index + 1;
800 spin_unlock(&mdd->mdd_cl.mc_lock);
802 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
806 * if current llog is full, we will generate a new
807 * llog, and since it's actually not an error, let's
808 * avoid increasing index so that userspace apps
809 * should not see a gap in the changelog sequence
811 if (!(rc == -ENOSPC && llog_is_full(loghandle))) {
812 spin_lock(&mdd->mdd_cl.mc_lock);
813 ++mdd->mdd_cl.mc_index;
814 spin_unlock(&mdd->mdd_cl.mc_lock);
817 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
824 bool mdd_changelog_need_gc(const struct lu_env *env, struct mdd_device *mdd,
825 struct llog_handle *lgh)
827 unsigned long free_cat_entries = llog_cat_free_space(lgh);
828 struct mdd_changelog *mc = &mdd->mdd_cl;
830 return free_cat_entries <= mdd->mdd_changelog_min_free_cat_entries ||
831 mdd_changelog_is_too_idle(mdd, mc->mc_minrec, mc->mc_mintime) ||
832 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD);
835 /** Add a changelog entry \a rec to the changelog llog
838 * \param handle - currently ignored since llogs start their own transaction;
839 * this will hopefully be fixed in llog rewrite
842 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
843 struct llog_changelog_rec *rec, struct thandle *th)
845 struct obd_device *obd = mdd2obd_dev(mdd);
846 struct llog_ctxt *ctxt;
847 struct thandle *llog_th;
851 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
852 changelog_rec_varsize(&rec->cr));
854 /* llog_lvfs_write_rec sets the llog tail len */
855 rec->cr_hdr.lrh_type = CHANGELOG_REC;
856 rec->cr.cr_time = cl_time();
858 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
862 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
864 GOTO(out_put, rc = PTR_ERR(llog_th));
866 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
867 /* nested journal transaction */
868 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
870 /* time to recover some space ?? */
871 if (likely(!mdd->mdd_changelog_gc ||
872 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
873 mdd->mdd_changelog_min_gc_interval >=
874 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
875 /* save a spin_lock trip */
878 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_IDX_PUMP)) {
879 spin_lock(&mdd->mdd_cl.mc_lock);
880 mdd->mdd_cl.mc_index += cfs_fail_val;
881 spin_unlock(&mdd->mdd_cl.mc_lock);
884 need_gc = mdd_changelog_need_gc(env, mdd, ctxt->loc_handle);
885 spin_lock(&mdd->mdd_cl.mc_lock);
886 if (likely(mdd->mdd_changelog_gc &&
887 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
888 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
889 mdd->mdd_changelog_min_gc_interval)) {
890 if (unlikely(need_gc)) {
891 CWARN("%s:%s starting changelog garbage collection\n",
893 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
895 /* indicate further kthread run will occur outside
896 * right after current journal transaction filling has
899 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
901 /* next check in mdd_changelog_min_gc_interval anyway
903 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
905 spin_unlock(&mdd->mdd_cl.mc_lock);
913 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
914 const struct lu_fid *sfid,
915 const struct lu_fid *spfid,
916 const struct lu_name *sname)
918 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
921 LASSERT(sfid != NULL);
922 LASSERT(spfid != NULL);
923 LASSERT(sname != NULL);
925 extsize = sname->ln_namelen + 1;
927 rnm->cr_sfid = *sfid;
928 rnm->cr_spfid = *spfid;
930 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
931 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
932 rec->cr_namelen += extsize;
935 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
937 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
939 if (jobid == NULL || jobid[0] == '\0')
942 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
945 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
947 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
949 ef->cr_extra_flags = eflags;
952 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
953 __u64 uid, __u64 gid)
955 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
957 uidgid->cr_uid = uid;
958 uidgid->cr_gid = gid;
961 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
964 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
969 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
971 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
973 omd->cr_openflags = flags;
976 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
977 const char *xattr_name)
979 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
981 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
984 /** Store a namespace change changelog record
985 * If this fails, we must fail the whole transaction; we don't
986 * want the change to commit without the log entry.
987 * \param target - mdd_object of change
988 * \param tpfid - target parent dir/object fid
989 * \param sfid - source object fid
990 * \param spfid - source parent fid
991 * \param tname - target name string
992 * \param sname - source name string
993 * \param handle - transaction handle
995 int mdd_changelog_ns_store(const struct lu_env *env,
996 struct mdd_device *mdd,
997 enum changelog_rec_type type,
998 enum changelog_rec_flags clf_flags,
999 struct mdd_object *target,
1000 const struct lu_fid *tpfid,
1001 const struct lu_fid *sfid,
1002 const struct lu_fid *spfid,
1003 const struct lu_name *tname,
1004 const struct lu_name *sname,
1005 struct thandle *handle)
1007 const struct lu_ucred *uc = lu_ucred(env);
1008 struct llog_changelog_rec *rec;
1011 __u64 xflags = CLFE_INVALID;
1015 if (!mdd_changelog_enabled(env, mdd, type))
1018 LASSERT(tpfid != NULL);
1019 LASSERT(tname != NULL);
1020 LASSERT(handle != NULL);
1022 reclen = mdd_llog_record_calc_size(env, tname, sname);
1023 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
1024 if (buf->lb_buf == NULL)
1028 clf_flags &= CLF_FLAGMASK;
1029 clf_flags |= CLF_EXTRA_FLAGS;
1032 if (uc->uc_jobid[0] != '\0')
1033 clf_flags |= CLF_JOBID;
1034 xflags |= CLFE_UIDGID;
1039 clf_flags |= CLF_RENAME;
1041 clf_flags |= CLF_VERSION;
1043 rec->cr.cr_flags = clf_flags;
1045 if (clf_flags & CLF_EXTRA_FLAGS) {
1046 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1047 if (xflags & CLFE_UIDGID)
1048 mdd_changelog_rec_extra_uidgid(&rec->cr,
1049 uc->uc_uid, uc->uc_gid);
1050 if (xflags & CLFE_NID)
1051 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1054 rec->cr.cr_type = (__u32)type;
1055 rec->cr.cr_pfid = *tpfid;
1056 rec->cr.cr_namelen = tname->ln_namelen;
1057 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1059 if (clf_flags & CLF_RENAME)
1060 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1062 if (clf_flags & CLF_JOBID)
1063 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1065 if (likely(target != NULL)) {
1066 rec->cr.cr_tfid = *mdd_object_fid(target);
1067 target->mod_cltime = ktime_get();
1069 fid_zero(&rec->cr.cr_tfid);
1072 rc = mdd_changelog_store(env, mdd, rec, handle);
1074 CERROR("%s: cannot store changelog record: type = %d, "
1075 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1076 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1077 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1084 static int __mdd_links_add(const struct lu_env *env,
1085 struct mdd_object *mdd_obj,
1086 struct linkea_data *ldata,
1087 const struct lu_name *lname,
1088 const struct lu_fid *pfid,
1089 int first, int check)
1091 /* cattr is set in mdd_link */
1092 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1095 if (ldata->ld_leh == NULL) {
1096 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1100 rc = linkea_data_new(ldata,
1101 &mdd_env_info(env)->mdi_link_buf);
1108 rc = linkea_links_find(ldata, lname, pfid);
1109 if (rc && rc != -ENOENT)
1115 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1116 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1120 linkea_add_buf(ldata, lname, tfid, false);
1123 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1124 linkea_add_buf(ldata, lname, pfid, false);
1126 /* For encrypted file, we want to limit number of hard links to what
1127 * linkEA can contain. So ask to return error in case of overflow.
1128 * Currently linkEA stores 4KiB of links, that is 14 NAME_MAX links,
1129 * or 119 16-byte names.
1131 return linkea_add_buf(ldata, lname, pfid,
1132 cattr->la_valid & LA_FLAGS &&
1133 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1136 static int __mdd_links_del(const struct lu_env *env,
1137 struct mdd_object *mdd_obj,
1138 struct linkea_data *ldata,
1139 const struct lu_name *lname,
1140 const struct lu_fid *pfid)
1142 /* cattr is set in mdd_link */
1143 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1146 if (ldata->ld_leh == NULL) {
1147 rc = mdd_links_read(env, mdd_obj, ldata);
1152 rc = linkea_links_find(ldata, lname, pfid);
1156 linkea_del_buf(ldata, lname,
1157 cattr->la_valid & LA_FLAGS &&
1158 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1162 static int mdd_linkea_prepare(const struct lu_env *env,
1163 struct mdd_object *mdd_obj,
1164 const struct lu_fid *oldpfid,
1165 const struct lu_name *oldlname,
1166 const struct lu_fid *newpfid,
1167 const struct lu_name *newlname,
1168 int first, int check,
1169 struct linkea_data *ldata)
1174 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1177 LASSERT(oldpfid != NULL || newpfid != NULL);
1179 if (mdd_obj->mod_flags & DEAD_OBJ)
1180 /* Unnecessary to update linkEA for dead object. */
1183 if (oldpfid != NULL) {
1184 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1186 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1189 /* No changes done. */
1194 /* If renaming, add the new record */
1195 if (newpfid != NULL)
1196 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1202 int mdd_links_rename(const struct lu_env *env,
1203 struct mdd_object *mdd_obj,
1204 const struct lu_fid *oldpfid,
1205 const struct lu_name *oldlname,
1206 const struct lu_fid *newpfid,
1207 const struct lu_name *newlname,
1208 struct thandle *handle,
1209 struct linkea_data *ldata,
1210 int first, int check)
1215 if (ldata == NULL) {
1216 ldata = &mdd_env_info(env)->mdi_link_data;
1217 memset(ldata, 0, sizeof(*ldata));
1218 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1219 newpfid, newlname, first, check, ldata);
1224 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1225 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1231 if (newlname == NULL)
1232 CERROR("link_ea add failed %d "DFID"\n",
1233 rc, PFID(mdd_object_fid(mdd_obj)));
1234 else if (oldpfid == NULL)
1235 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1236 newlname->ln_namelen, newlname->ln_name, rc,
1237 PFID(mdd_object_fid(mdd_obj)));
1238 else if (newpfid == NULL)
1239 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1240 oldlname->ln_namelen, oldlname->ln_name, rc,
1241 PFID(mdd_object_fid(mdd_obj)));
1243 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1244 "\n", oldlname->ln_namelen, oldlname->ln_name,
1245 newlname->ln_namelen, newlname->ln_name, rc,
1246 PFID(mdd_object_fid(mdd_obj)));
1249 if (is_vmalloc_addr(ldata->ld_buf))
1250 /* if we vmalloced a large buffer drop it */
1251 lu_buf_free(ldata->ld_buf);
1256 static inline int mdd_links_add(const struct lu_env *env,
1257 struct mdd_object *mdd_obj,
1258 const struct lu_fid *pfid,
1259 const struct lu_name *lname,
1260 struct thandle *handle,
1261 struct linkea_data *ldata, int first)
1263 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1264 pfid, lname, handle, ldata, first, 0);
1267 static inline int mdd_links_del(const struct lu_env *env,
1268 struct mdd_object *mdd_obj,
1269 const struct lu_fid *pfid,
1270 const struct lu_name *lname,
1271 struct thandle *handle)
1273 return mdd_links_rename(env, mdd_obj, pfid, lname,
1274 NULL, NULL, handle, NULL, 0, 0);
1277 /** Read the link EA into a temp buffer.
1278 * Uses the name_buf since it is generally large.
1279 * \retval IS_ERR err
1280 * \retval ptr to \a lu_buf (always \a mdi_link_buf)
1282 struct lu_buf *mdd_links_get(const struct lu_env *env,
1283 struct mdd_object *mdd_obj)
1285 struct linkea_data ldata = { NULL };
1288 rc = mdd_links_read(env, mdd_obj, &ldata);
1289 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1292 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1293 struct linkea_data *ldata, struct thandle *handle)
1295 const struct lu_buf *buf;
1298 if (ldata == NULL || ldata->ld_buf == NULL ||
1299 ldata->ld_leh == NULL)
1302 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1306 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1307 ldata->ld_leh->leh_len);
1308 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1309 if (unlikely(rc == -ENOSPC)) {
1310 rc = linkea_overflow_shrink(ldata);
1318 static int mdd_declare_links_add(const struct lu_env *env,
1319 struct mdd_object *mdd_obj,
1320 struct thandle *handle,
1321 struct linkea_data *ldata)
1327 if (ldata != NULL && ldata->ld_leh != NULL) {
1328 ea_len = ldata->ld_leh->leh_len;
1329 linkea = ldata->ld_buf->lb_buf;
1331 ea_len = MAX_LINKEA_SIZE;
1335 rc = mdo_declare_xattr_set(env, mdd_obj,
1336 mdd_buf_get_const(env, linkea, ea_len),
1337 XATTR_NAME_LINK, 0, handle);
1342 static inline int mdd_declare_links_del(const struct lu_env *env,
1343 struct mdd_object *c,
1344 struct thandle *handle)
1348 /* For directory, the linkEA will be removed together
1349 * with the object. */
1350 if (!S_ISDIR(mdd_object_type(c)))
1351 rc = mdd_declare_links_add(env, c, handle, NULL);
1356 static int mdd_declare_link(const struct lu_env *env,
1357 struct mdd_device *mdd,
1358 struct mdd_object *p,
1359 struct mdd_object *c,
1360 const struct lu_name *name,
1361 struct thandle *handle,
1363 struct linkea_data *data)
1365 struct lu_fid tfid = *mdd_object_fid(c);
1368 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1369 tfid.f_oid = cfs_fail_val;
1371 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1372 name->ln_name, handle);
1376 rc = mdo_declare_ref_add(env, c, handle);
1380 la->la_valid = LA_CTIME | LA_MTIME;
1381 rc = mdo_declare_attr_set(env, p, la, handle);
1385 la->la_valid = LA_CTIME;
1386 rc = mdo_declare_attr_set(env, c, la, handle);
1390 rc = mdd_declare_links_add(env, c, handle, data);
1394 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1400 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1401 struct md_object *src_obj, const struct lu_name *lname,
1404 const char *name = lname->ln_name;
1405 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1406 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1407 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1408 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1409 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1410 struct mdd_device *mdd = mdo2mdd(src_obj);
1411 struct thandle *handle;
1412 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1413 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
1417 rc = mdd_la_get(env, mdd_sobj, cattr);
1421 rc = mdd_la_get(env, mdd_tobj, tattr);
1426 * If we are using project inheritance, we only allow hard link
1427 * creation in our tree when the project IDs are the same;
1428 * otherwise the tree quota mechanism could be circumvented.
1430 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1431 (tattr->la_projid != cattr->la_projid))
1434 handle = mdd_trans_create(env, mdd);
1436 GOTO(out_pending, rc = PTR_ERR(handle));
1438 memset(ldata, 0, sizeof(*ldata));
1440 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1441 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1443 /* Note: even this function will change ldata, but it comes from
1444 * thread_info, which is completely temporary and only seen in
1445 * this function, so we do not need reset ldata once it fails.*/
1446 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1447 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1451 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1456 rc = mdd_trans_start(env, mdd, handle);
1460 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1461 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1464 GOTO(out_unlock, rc);
1466 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1467 rc = mdo_ref_add(env, mdd_sobj, handle);
1469 GOTO(out_unlock, rc);
1472 *tfid = *mdd_object_fid(mdd_sobj);
1473 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1474 tfid->f_oid = cfs_fail_val;
1476 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1477 mdd_object_type(mdd_sobj), name, handle);
1479 mdo_ref_del(env, mdd_sobj, handle);
1480 GOTO(out_unlock, rc);
1483 la->la_valid = LA_CTIME | LA_MTIME;
1484 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1486 GOTO(out_unlock, rc);
1488 la->la_valid = LA_CTIME;
1489 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1491 /* Note: The failure of links_add should not cause the
1492 * link failure, so do not check return value. */
1493 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1494 lname, handle, ldata, 0);
1498 mdd_write_unlock(env, mdd_sobj);
1500 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1501 mdd_object_fid(mdd_tobj), NULL,
1502 NULL, lname, NULL, handle);
1504 rc = mdd_trans_stop(env, mdd, rc, handle);
1505 if (is_vmalloc_addr(ldata->ld_buf))
1506 /* if we vmalloced a large buffer drop it */
1507 lu_buf_free(ldata->ld_buf);
1512 static int mdd_mark_orphan_object(const struct lu_env *env,
1513 struct mdd_object *obj, struct thandle *handle,
1516 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1519 attr->la_valid = LA_FLAGS;
1520 attr->la_flags = LUSTRE_ORPHAN_FL;
1523 rc = mdo_declare_attr_set(env, obj, attr, handle);
1525 rc = mdo_attr_set(env, obj, attr, handle);
1530 static int mdd_declare_finish_unlink(const struct lu_env *env,
1531 struct mdd_object *obj,
1532 struct thandle *handle)
1536 /* Sigh, we do not know if the unlink object will become orphan in
1537 * declare phase, but fortunately the flags here does not matter
1538 * in current declare implementation */
1539 rc = mdd_mark_orphan_object(env, obj, handle, true);
1543 rc = mdo_declare_destroy(env, obj, handle);
1547 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1551 return mdd_declare_links_del(env, obj, handle);
1554 /* caller should take a lock before calling */
1555 int mdd_finish_unlink(const struct lu_env *env,
1556 struct mdd_object *obj, struct md_attr *ma,
1557 struct mdd_object *pobj,
1558 const struct lu_name *lname,
1562 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1565 LASSERT(mdd_write_locked(env, obj) != 0);
1567 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1568 /* add new orphan and the object
1569 * will be deleted during mdd_close() */
1570 obj->mod_flags |= DEAD_OBJ;
1571 if (obj->mod_count) {
1572 rc = mdd_orphan_insert(env, obj, th);
1574 CDEBUG(D_HA, "Object "DFID" is inserted into "
1575 "orphan list, open count = %d\n",
1576 PFID(mdd_object_fid(obj)),
1579 CERROR("Object "DFID" fail to be an orphan, "
1580 "open count = %d, maybe cause failed "
1582 PFID(mdd_object_fid(obj)),
1585 /* mark object as an orphan here, not
1586 * before mdd_orphan_insert() as racing
1587 * mdd_la_get() may propagate ORPHAN_OBJ
1588 * causing the asserition */
1589 rc = mdd_mark_orphan_object(env, obj, th, false);
1591 rc = mdo_destroy(env, obj, th);
1593 } else if (!is_dir) {
1594 /* old files may not have link ea; ignore errors */
1595 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1603 * has mdd_write_lock on cobj already, but not on pobj yet
1605 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1606 const struct lu_attr *pattr,
1607 struct mdd_object *cobj,
1608 const struct lu_attr *cattr)
1613 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1618 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1619 struct mdd_object *p, struct mdd_object *c,
1620 const struct lu_name *name, struct md_attr *ma,
1621 struct thandle *handle, int no_name, int is_dir)
1623 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1626 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1627 if (likely(no_name == 0)) {
1628 rc = mdo_declare_index_delete(env, p, name->ln_name,
1635 rc = mdo_declare_ref_del(env, p, handle);
1641 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1642 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1643 la->la_valid = LA_CTIME | LA_MTIME;
1644 rc = mdo_declare_attr_set(env, p, la, handle);
1649 rc = mdo_declare_ref_del(env, c, handle);
1653 rc = mdo_declare_ref_del(env, c, handle);
1657 la->la_valid = LA_CTIME;
1658 rc = mdo_declare_attr_set(env, c, la, handle);
1662 rc = mdd_declare_finish_unlink(env, c, handle);
1666 /* FIXME: need changelog for remove entry */
1667 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1675 * test if a file has an HSM archive
1676 * if HSM attributes are not found in ma update them from
1679 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1680 struct mdd_object *obj,
1685 if (!(ma->ma_valid & MA_HSM)) {
1686 /* no HSM MD provided, read xattr */
1687 struct lu_buf *hsm_buf;
1688 const size_t buflen = sizeof(struct hsm_attrs);
1691 hsm_buf = mdd_buf_get(env, NULL, 0);
1692 lu_buf_alloc(hsm_buf, buflen);
1693 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1694 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1695 lu_buf_free(hsm_buf);
1699 ma->ma_valid |= MA_HSM;
1701 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1707 * Delete name entry and the object.
1708 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1709 * does not exist for this object, and it could only happen during resending
1710 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1711 * is also needed in this case(needed by changelog), so we have to add another
1712 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1713 * the ENOENT failure should be able to be fixed by redo mechanism.
1715 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1716 struct md_object *cobj, const struct lu_name *lname,
1717 struct md_attr *ma, int no_name)
1719 char *name = (char *)lname->ln_name;
1720 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1721 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1722 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1723 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1724 struct mdd_object *mdd_cobj = NULL;
1725 struct mdd_device *mdd = mdo2mdd(pobj);
1726 struct thandle *handle;
1727 int rc, is_dir = 0, cl_flags = 0;
1730 /* let shutdown to start */
1731 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1733 /* cobj == NULL means only delete name entry */
1734 if (likely(cobj != NULL)) {
1735 mdd_cobj = md2mdd_obj(cobj);
1736 if (mdd_object_exists(mdd_cobj) == 0)
1740 rc = mdd_la_get(env, mdd_pobj, pattr);
1744 if (likely(mdd_cobj != NULL)) {
1746 rc = mdd_la_get(env, mdd_cobj, cattr);
1750 is_dir = S_ISDIR(cattr->la_mode);
1751 /* search for an existing archive.
1752 * we should check ahead as the object
1753 * can be destroyed in this transaction */
1754 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1755 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1758 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1762 handle = mdd_trans_create(env, mdd);
1764 RETURN(PTR_ERR(handle));
1766 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1767 lname, ma, handle, no_name, is_dir);
1771 rc = mdd_trans_start(env, mdd, handle);
1775 if (likely(mdd_cobj != NULL))
1776 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1778 if (lname->ln_name[lname->ln_namelen] != '\0') {
1779 /* lname->ln_name is not necessarily NUL terminated */
1780 name = kmalloc(lname->ln_namelen + 1, GFP_NOFS);
1782 GOTO(cleanup, rc = -ENOMEM);
1784 memcpy(name, lname->ln_name, lname->ln_namelen);
1785 name[lname->ln_namelen] = '\0';
1788 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1789 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1794 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1795 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1796 GOTO(cleanup, rc = 0);
1798 if (likely(mdd_cobj != NULL)) {
1799 rc = mdo_ref_del(env, mdd_cobj, handle);
1801 __mdd_index_insert_only(env, mdd_pobj,
1802 mdd_object_fid(mdd_cobj),
1803 mdd_object_type(mdd_cobj),
1810 mdo_ref_del(env, mdd_cobj, handle);
1812 /* fetch updated nlink */
1813 rc = mdd_la_get(env, mdd_cobj, cattr);
1818 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1819 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1821 la->la_valid = LA_CTIME | LA_MTIME;
1822 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1826 /* Enough for only unlink the entry */
1827 if (unlikely(mdd_cobj == NULL))
1830 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1831 /* update ctime of an unlinked file only if it is still
1832 * opened or a link still exists */
1833 la->la_valid = LA_CTIME;
1834 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1839 /* XXX: this transfer to ma will be removed with LOD/OSP */
1840 ma->ma_attr = *cattr;
1841 ma->ma_valid |= MA_INODE;
1842 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1846 /* fetch updated nlink */
1847 rc = mdd_la_get(env, mdd_cobj, cattr);
1848 /* if object is removed then we can't get its attrs,
1850 if (rc == -ENOENT) {
1851 cattr->la_nlink = 0;
1855 if (cattr->la_nlink == 0) {
1856 ma->ma_attr = *cattr;
1857 ma->ma_valid |= MA_INODE;
1862 if (name != lname->ln_name)
1865 if (likely(mdd_cobj != NULL))
1866 mdd_write_unlock(env, mdd_cobj);
1869 if (cattr->la_nlink == 0)
1870 cl_flags |= CLF_UNLINK_LAST;
1872 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1874 rc = mdd_changelog_ns_store(env, mdd,
1875 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1876 mdd_cobj, mdd_object_fid(mdd_pobj), NULL, NULL,
1877 lname, NULL, handle);
1881 rc = mdd_trans_stop(env, mdd, rc, handle);
1887 * The permission has been checked when obj created, no need check again.
1889 static int mdd_cd_sanity_check(const struct lu_env *env,
1890 struct mdd_object *obj)
1895 if (!obj || mdd_is_dead_obj(obj))
1901 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1902 struct md_object *cobj,
1903 const struct md_op_spec *spec, struct md_attr *ma)
1905 struct mdd_device *mdd = mdo2mdd(cobj);
1906 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1907 struct mdd_object *son = md2mdd_obj(cobj);
1908 struct thandle *handle;
1909 const struct lu_buf *buf;
1910 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1911 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
1915 rc = mdd_cd_sanity_check(env, son);
1919 if (!md_should_create(spec->sp_cr_flags))
1923 * there are following use cases for this function:
1924 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1925 * striping can be specified or not
1928 rc = mdd_la_get(env, son, attr);
1932 /* calling ->ah_make_hint() is used to transfer information from parent */
1933 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1935 handle = mdd_trans_create(env, mdd);
1937 GOTO(out_free, rc = PTR_ERR(handle));
1940 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1941 * Should this be fixed?
1943 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1944 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1945 spec->sp_cr_flags, spec->no_create);
1947 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1948 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1949 spec->u.sp_ea.eadatalen);
1954 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1955 XATTR_NAME_LOV, 0, handle);
1959 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1964 rc = mdd_trans_start(env, mdd, handle);
1968 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1974 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
1978 rc = mdd_trans_stop(env, mdd, rc, handle);
1984 static int mdd_declare_object_initialize(const struct lu_env *env,
1985 struct mdd_object *parent,
1986 struct mdd_object *child,
1987 const struct lu_attr *attr,
1988 struct thandle *handle)
1993 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1994 if (!S_ISDIR(attr->la_mode))
1997 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
1998 S_IFDIR, dot, handle);
2002 rc = mdo_declare_ref_add(env, child, handle);
2006 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
2007 S_IFDIR, dotdot, handle);
2012 static int mdd_object_initialize(const struct lu_env *env,
2013 const struct lu_fid *pfid,
2014 struct mdd_object *child,
2015 struct lu_attr *attr,
2016 struct thandle *handle)
2021 if (S_ISDIR(attr->la_mode)) {
2022 /* Add "." and ".." for newly created dir */
2023 mdo_ref_add(env, child, handle);
2024 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
2025 S_IFDIR, dot, handle);
2027 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2030 mdo_ref_del(env, child, handle);
2037 * This function checks whether it can create a file/dir under the
2038 * directory(@pobj). The directory(@pobj) is not being locked by
2041 * \param[in] env execution environment
2042 * \param[in] pobj the directory to create files
2043 * \param[in] pattr the attributes of the directory
2044 * \param[in] lname the name of the created file/dir
2045 * \param[in] cattr the attributes of the file/dir
2046 * \param[in] spec create specification
2048 * \retval = 0 it is allowed to create file/dir under
2050 * \retval negative error not allowed to create file/dir
2051 * under the directory
2053 static int mdd_create_sanity_check(const struct lu_env *env,
2054 struct md_object *pobj,
2055 const struct lu_attr *pattr,
2056 const struct lu_name *lname,
2057 struct lu_attr *cattr,
2058 struct md_op_spec *spec)
2060 struct mdd_thread_info *info = mdd_env_info(env);
2061 struct lu_fid *fid = &info->mdi_fid;
2062 struct mdd_object *obj = md2mdd_obj(pobj);
2063 struct mdd_device *m = mdo2mdd(pobj);
2064 bool check_perm = true;
2069 if (mdd_is_dead_obj(obj))
2073 * In some cases this lookup is not needed - we know before if name
2074 * exists or not because MDT performs lookup for it.
2075 * name length check is done in lookup.
2077 if (spec->sp_cr_lookup) {
2079 * Check if the name already exist, though it will be checked in
2080 * _index_insert also, for avoiding rolling back if exists
2083 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2084 MAY_WRITE | MAY_EXEC);
2086 RETURN(rc ? : -EEXIST);
2088 /* Permission is already being checked in mdd_lookup */
2092 if (S_ISDIR(cattr->la_mode) &&
2093 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2094 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2095 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2097 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2098 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2100 CERROR("%s: invalid lmv_user_md: magic=%x hash=%x stripe_offset=%d stripe_count=%u: rc = %d\n",
2101 mdd2obd_dev(m)->obd_name,
2102 le32_to_cpu(lum->lum_magic),
2103 le32_to_cpu(lum->lum_hash_type),
2104 (int)le32_to_cpu(lum->lum_stripe_offset),
2105 le32_to_cpu(lum->lum_stripe_count), rc);
2111 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2116 if (pattr->la_mode & S_ISGID) {
2117 struct lu_ucred *uc = lu_ucred(env);
2119 cattr->la_gid = pattr->la_gid;
2121 /* Directories are special, and always inherit S_ISGID */
2122 if (S_ISDIR(cattr->la_mode)) {
2123 cattr->la_mode |= S_ISGID;
2124 cattr->la_valid |= LA_MODE;
2125 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2126 == (S_ISGID | S_IXGRP) &&
2127 !lustre_in_group_p(uc,
2128 (cattr->la_valid & LA_GID) ?
2129 cattr->la_gid : pattr->la_gid) &&
2130 !cap_raised(uc->uc_cap, CAP_FSETID)) {
2131 cattr->la_mode &= ~S_ISGID;
2132 cattr->la_valid |= LA_MODE;
2136 /* Inherit project ID from parent directory */
2137 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2138 cattr->la_projid = pattr->la_projid;
2139 if (S_ISDIR(cattr->la_mode)) {
2140 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2141 cattr->la_valid |= LA_FLAGS;
2143 cattr->la_valid |= LA_PROJID;
2146 rc = mdd_name_check(env, m, lname);
2150 switch (cattr->la_mode & S_IFMT) {
2152 unsigned int symlen = spec->u.sp_symname.ln_namelen + 1;
2154 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2155 RETURN(-ENAMETOOLONG);
2174 static int mdd_declare_create_object(const struct lu_env *env,
2175 struct mdd_device *mdd,
2176 struct mdd_object *p, struct mdd_object *c,
2177 struct lu_attr *attr,
2178 struct thandle *handle,
2179 const struct md_op_spec *spec,
2180 struct lu_buf *def_acl_buf,
2181 struct lu_buf *acl_buf,
2182 struct lu_buf *hsm_buf,
2183 struct dt_allocation_hint *hint)
2185 const struct lu_buf *buf;
2188 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2189 /* ldiskfs OSD needs this information for credit allocation */
2191 hint->dah_acl_len = def_acl_buf->lb_len;
2193 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2198 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2199 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2200 /* if dir, then can inherit default ACl */
2201 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2202 XATTR_NAME_ACL_DEFAULT,
2208 if (acl_buf && acl_buf->lb_len > 0) {
2209 rc = mdo_declare_attr_set(env, c, attr, handle);
2213 rc = mdo_declare_xattr_set(env, c, acl_buf,
2214 XATTR_NAME_ACL_ACCESS, 0, handle);
2219 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2223 /* replay case, create LOV EA from client data */
2224 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2225 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2226 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2227 spec->u.sp_ea.eadatalen);
2228 rc = mdo_declare_xattr_set(env, c, buf,
2229 S_ISDIR(attr->la_mode) ?
2230 XATTR_NAME_LMV : XATTR_NAME_LOV,
2231 LU_XATTR_CREATE, handle);
2235 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2236 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2244 if (S_ISLNK(attr->la_mode)) {
2245 const char *target_name = spec->u.sp_symname.ln_name;
2246 int sym_len = spec->u.sp_symname.ln_namelen;
2247 const struct lu_buf *buf;
2249 buf = mdd_buf_get_const(env, target_name, sym_len);
2250 rc = dt_declare_record_write(env, mdd_object_child(c),
2256 if (spec->sp_cr_file_secctx_name != NULL) {
2257 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2258 spec->sp_cr_file_secctx_size);
2259 rc = mdo_declare_xattr_set(env, c, buf,
2260 spec->sp_cr_file_secctx_name, 0,
2266 if (spec->sp_cr_file_encctx != NULL) {
2267 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2268 spec->sp_cr_file_encctx_size);
2269 rc = mdo_declare_xattr_set(env, c, buf,
2270 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2279 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2280 struct mdd_object *p, struct mdd_object *c,
2281 const struct lu_name *name,
2282 struct lu_attr *attr,
2283 struct thandle *handle,
2284 const struct md_op_spec *spec,
2285 struct linkea_data *ldata,
2286 struct lu_buf *def_acl_buf,
2287 struct lu_buf *acl_buf,
2288 struct lu_buf *hsm_buf,
2289 struct dt_allocation_hint *hint)
2293 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2294 def_acl_buf, acl_buf, hsm_buf, hint);
2298 if (S_ISDIR(attr->la_mode)) {
2299 rc = mdo_declare_ref_add(env, p, handle);
2304 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2305 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2309 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2310 enum changelog_rec_type type;
2312 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2313 attr->la_mode, name->ln_name,
2318 rc = mdd_declare_links_add(env, c, handle, ldata);
2323 la->la_valid = LA_CTIME | LA_MTIME;
2324 rc = mdo_declare_attr_set(env, p, la, handle);
2328 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2329 S_ISREG(attr->la_mode) ? CL_CREATE :
2330 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2332 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2341 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2342 struct lu_attr *la, struct lu_buf *def_acl_buf,
2343 struct lu_buf *acl_buf)
2349 if (S_ISLNK(la->la_mode)) {
2350 acl_buf->lb_len = 0;
2351 def_acl_buf->lb_len = 0;
2355 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2356 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2357 XATTR_NAME_ACL_DEFAULT);
2358 mdd_read_unlock(env, pobj);
2360 /* ACL buffer size is not enough, need realloc */
2361 if (rc > acl_buf->lb_len)
2364 /* If there are default ACL, fix mode/ACL by default ACL */
2365 def_acl_buf->lb_len = rc;
2366 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2367 acl_buf->lb_len = rc;
2368 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2371 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2372 /* If there are no default ACL, fix mode by mask */
2373 struct lu_ucred *uc = lu_ucred(env);
2375 /* The create triggered by MDT internal events, such as
2376 * LFSCK reset, will not contain valid "uc". */
2377 if (unlikely(uc != NULL))
2378 la->la_mode &= ~uc->uc_umask;
2380 acl_buf->lb_len = 0;
2381 def_acl_buf->lb_len = 0;
2388 * Create a metadata object and initialize it, set acl, xattr.
2390 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2391 struct mdd_object *son, struct lu_attr *attr,
2392 struct md_op_spec *spec, struct lu_buf *acl_buf,
2393 struct lu_buf *def_acl_buf,
2394 struct lu_buf *hsm_buf,
2395 struct dt_allocation_hint *hint,
2396 struct thandle *handle, bool initsecctx)
2398 const struct lu_buf *buf;
2401 mdd_write_lock(env, son, DT_TGT_CHILD);
2402 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2407 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2408 * created in declare phase, they also needs to be added to master
2409 * object as sub-directory entry. So it has to initialize the master
2410 * object, then set dir striped EA.(in mdo_xattr_set) */
2411 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2414 GOTO(err_destroy, rc);
2417 * in case of replay we just set LOVEA provided by the client
2418 * XXX: I think it would be interesting to try "old" way where
2419 * MDT calls this xattr_set(LOV) in a different transaction.
2420 * probably this way we code can be made better.
2423 /* During creation, there are only a few cases we need do xattr_set to
2425 * 1. regular file: see comments above.
2426 * 2. dir: inherit default striping or pool settings from parent.
2427 * 3. create striped directory with provided stripeEA.
2428 * 4. create striped directory because inherit default layout from the
2431 if (spec->no_create ||
2432 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2433 S_ISDIR(attr->la_mode)) {
2434 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2435 spec->u.sp_ea.eadatalen);
2436 rc = mdo_xattr_set(env, son, buf,
2437 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2439 LU_XATTR_CREATE, handle);
2441 GOTO(err_destroy, rc);
2444 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2447 memset(&mh, 0, sizeof(mh));
2448 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2449 mh.mh_arch_id = spec->sp_archive_id;
2450 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2451 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2454 GOTO(err_destroy, rc);
2457 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2458 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2459 S_ISDIR(attr->la_mode)) {
2460 /* set default acl */
2461 rc = mdo_xattr_set(env, son, def_acl_buf,
2462 XATTR_NAME_ACL_DEFAULT, 0,
2465 GOTO(err_destroy, rc);
2467 /* set its own acl */
2468 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2469 rc = mdo_xattr_set(env, son, acl_buf,
2470 XATTR_NAME_ACL_ACCESS,
2473 GOTO(err_destroy, rc);
2477 if (S_ISLNK(attr->la_mode)) {
2478 struct dt_object *dt = mdd_object_child(son);
2479 const char *target_name = spec->u.sp_symname.ln_name;
2480 int sym_len = spec->u.sp_symname.ln_namelen;
2483 buf = mdd_buf_get_const(env, target_name, sym_len);
2484 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2488 GOTO(err_initlized, rc = -EFAULT);
2491 if (initsecctx && spec->sp_cr_file_secctx_name != NULL) {
2492 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2493 spec->sp_cr_file_secctx_size);
2494 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2497 GOTO(err_initlized, rc);
2500 if (spec->sp_cr_file_encctx != NULL) {
2501 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2502 spec->sp_cr_file_encctx_size);
2503 rc = mdo_xattr_set(env, son, buf,
2504 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2507 GOTO(err_initlized, rc);
2511 if (unlikely(rc != 0)) {
2513 if (S_ISDIR(attr->la_mode)) {
2514 /* Drop the reference, no need to delete "."/"..",
2515 * because the object to be destroied directly. */
2516 rc2 = mdo_ref_del(env, son, handle);
2520 rc2 = mdo_ref_del(env, son, handle);
2524 mdo_destroy(env, son, handle);
2527 mdd_write_unlock(env, son);
2531 static int mdd_index_delete(const struct lu_env *env,
2532 struct mdd_object *mdd_pobj,
2533 struct lu_attr *cattr,
2534 const struct lu_name *lname)
2536 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2537 struct thandle *handle;
2541 handle = mdd_trans_create(env, mdd);
2543 RETURN(PTR_ERR(handle));
2545 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2550 if (S_ISDIR(cattr->la_mode)) {
2551 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2556 /* Since this will only be used in the error handler path,
2557 * Let's set the thandle to be local and not mess the transno */
2558 handle->th_local = 1;
2559 rc = mdd_trans_start(env, mdd, handle);
2563 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2564 S_ISDIR(cattr->la_mode), handle);
2568 rc = mdd_trans_stop(env, mdd, rc, handle);
2574 * Create object and insert it into namespace.
2576 * Two operations have to be performed:
2578 * - an allocation of a new object (->do_create()), and
2579 * - an insertion into a parent index (->dio_insert()).
2581 * Due to locking, operation order is not important, when both are
2582 * successful, *but* error handling cases are quite different:
2584 * - if insertion is done first, and following object creation fails,
2585 * insertion has to be rolled back, but this operation might fail
2586 * also leaving us with dangling index entry.
2588 * - if creation is done first, is has to be undone if insertion fails,
2589 * leaving us with leaked space, which is not good but not fatal.
2591 * It seems that creation-first is simplest solution, but it is sub-optimal
2597 * case, because second mkdir is bound to create object, only to
2598 * destroy it immediately.
2600 * To avoid this follow local file systems that do double lookup:
2602 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2603 * 1. create (mdd_create_object_internal())
2604 * 2. insert (__mdd_index_insert(), lookup again)
2606 * \param[in] pobj parent object
2607 * \param[in] lname name of child being created
2608 * \param[in,out] child child object being created
2609 * \param[in] spec additional create parameters
2610 * \param[in] ma attributes for new child object
2612 * \retval 0 on success
2613 * \retval negative errno on failure
2615 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2616 const struct lu_name *lname, struct md_object *child,
2617 struct md_op_spec *spec, struct md_attr *ma)
2619 struct mdd_thread_info *info = mdd_env_info(env);
2620 struct lu_attr *la = &info->mdi_la_for_fix;
2621 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2622 struct mdd_object *son = md2mdd_obj(child);
2623 struct mdd_device *mdd = mdo2mdd(pobj);
2624 struct lu_attr *attr = &ma->ma_attr;
2625 struct thandle *handle;
2626 struct lu_attr *pattr = &info->mdi_pattr;
2627 struct lu_buf acl_buf;
2628 struct lu_buf def_acl_buf;
2629 struct lu_buf hsm_buf;
2630 struct linkea_data *ldata = &info->mdi_link_data;
2631 const char *name = lname->ln_name;
2632 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2633 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2638 rc = mdd_la_get(env, mdd_pobj, pattr);
2642 /* Sanity checks before big job. */
2643 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2647 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2648 GOTO(out_free, rc = -EINPROGRESS);
2650 handle = mdd_trans_create(env, mdd);
2652 GOTO(out_free, rc = PTR_ERR(handle));
2655 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
2656 if (!acl_buf.lb_buf)
2657 GOTO(out_stop, rc = -ENOMEM);
2659 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
2660 if (!def_acl_buf.lb_buf)
2661 GOTO(out_stop, rc = -ENOMEM);
2663 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2664 if (unlikely(rc == -ERANGE &&
2665 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2666 /* use maximum-sized xattr buffer for too-big default ACL */
2667 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2669 goto use_bigger_buffer;
2674 if (S_ISDIR(attr->la_mode)) {
2675 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2678 * migrate may create 1-stripe directory, so lod_ah_init()
2679 * doesn't adjust stripe count from lmu.
2681 if (lmu && lmu->lum_stripe_count == cpu_to_le32(1))
2682 lmu->lum_stripe_count = 0;
2685 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2687 memset(ldata, 0, sizeof(*ldata));
2688 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2689 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2692 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2693 &tfid, lname, 1, 0, ldata);
2695 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2696 mdd_object_fid(mdd_pobj),
2697 lname, 1, 0, ldata);
2700 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2701 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2703 memset(&hsm_buf, 0, sizeof(hsm_buf));
2704 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2705 if (hsm_buf.lb_buf == NULL)
2706 GOTO(out_stop, rc = -ENOMEM);
2709 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2710 handle, spec, ldata, &def_acl_buf, &acl_buf,
2715 rc = mdd_trans_start(env, mdd, handle);
2719 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2720 &def_acl_buf, &hsm_buf, hint, handle, true);
2724 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2725 mdd_write_lock(env, son, DT_TGT_CHILD);
2726 son->mod_flags |= VOLATILE_OBJ;
2727 rc = mdd_orphan_insert(env, son, handle);
2728 GOTO(out_volatile, rc);
2730 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(son),
2731 attr->la_mode, name, handle);
2733 GOTO(err_created, rc);
2735 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2738 /* update parent directory mtime/ctime */
2740 la->la_valid = LA_CTIME | LA_MTIME;
2741 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2743 GOTO(err_insert, rc);
2749 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2750 rc2 = mdd_orphan_delete(env, son, handle);
2752 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2753 S_ISDIR(attr->la_mode),
2759 mdd_write_lock(env, son, DT_TGT_CHILD);
2760 if (S_ISDIR(attr->la_mode)) {
2761 /* Drop the reference, no need to delete "."/"..",
2762 * because the object is to be destroyed directly. */
2763 rc2 = mdo_ref_del(env, son, handle);
2765 mdd_write_unlock(env, son);
2770 /* For volatile files drop one link immediately, since there is
2771 * no filename in the namespace, and save any error returned. */
2772 rc2 = mdo_ref_del(env, son, handle);
2774 mdd_write_unlock(env, son);
2775 if (unlikely(rc == 0))
2780 /* Don't destroy the volatile object on success */
2781 if (likely(rc != 0))
2782 mdo_destroy(env, son, handle);
2783 mdd_write_unlock(env, son);
2786 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
2787 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2788 rc = mdd_changelog_ns_store(env, mdd,
2789 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2790 S_ISREG(attr->la_mode) ? CL_CREATE :
2791 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2792 0, son, mdd_object_fid(mdd_pobj), NULL, NULL,
2793 lname, NULL, handle);
2795 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2797 /* If creation fails, it is most likely due to the remote update
2798 * failure, because local transaction will mostly succeed at
2799 * this stage. There is no easy way to rollback all of previous
2800 * updates, so let's remove the object from namespace, and
2801 * LFSCK should handle the orphan object. */
2802 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2803 mdd_index_delete(env, mdd_pobj, attr, lname);
2807 if (is_vmalloc_addr(ldata->ld_buf))
2808 /* if we vmalloced a large buffer drop it */
2809 lu_buf_free(ldata->ld_buf);
2811 if (spec->sp_cr_flags & MDS_OPEN_PCC)
2812 lu_buf_free(&hsm_buf);
2814 /* The child object shouldn't be cached anymore */
2816 set_bit(LU_OBJECT_HEARD_BANSHEE,
2817 &child->mo_lu.lo_header->loh_flags);
2821 /* has not mdd_write{read}_lock on any obj yet. */
2822 static int mdd_rename_sanity_check(const struct lu_env *env,
2823 struct mdd_object *src_pobj,
2824 const struct lu_attr *pattr,
2825 struct mdd_object *tgt_pobj,
2826 const struct lu_attr *tpattr,
2827 struct mdd_object *sobj,
2828 const struct lu_attr *cattr,
2829 struct mdd_object *tobj,
2830 const struct lu_attr *tattr)
2835 /* XXX: when get here, sobj must NOT be NULL,
2836 * the other case has been processed in cld_rename
2837 * before mdd_rename and enable MDS_PERM_BYPASS. */
2841 * If we are using project inheritance, we only allow renames
2842 * into our tree when the project IDs are the same; otherwise
2843 * tree quota mechanism would be circumvented.
2845 if ((((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2846 tpattr->la_projid != cattr->la_projid) ||
2847 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2848 (pattr->la_projid != tpattr->la_projid))) &&
2849 S_ISDIR(cattr->la_mode))
2852 /* we prevent an encrypted file from being renamed
2853 * into an unencrypted dir
2855 if ((pattr->la_valid & LA_FLAGS &&
2856 pattr->la_flags & LUSTRE_ENCRYPT_FL) &&
2857 !(tpattr->la_valid & LA_FLAGS &&
2858 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
2861 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2865 /* XXX: when get here, "tobj == NULL" means tobj must
2866 * NOT exist (neither on remote MDS, such case has been
2867 * processed in cld_rename before mdd_rename and enable
2869 * So check may_create, but not check may_unlink. */
2871 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2872 (src_pobj != tgt_pobj));
2874 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2875 (src_pobj != tgt_pobj), 1);
2877 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2878 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2883 static int mdd_declare_rename(const struct lu_env *env,
2884 struct mdd_device *mdd,
2885 struct mdd_object *mdd_spobj,
2886 struct mdd_object *mdd_tpobj,
2887 struct mdd_object *mdd_sobj,
2888 struct mdd_object *mdd_tobj,
2889 const struct lu_name *sname,
2890 const struct lu_name *tname,
2892 struct linkea_data *ldata, bool change_projid,
2893 struct thandle *handle)
2895 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2898 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2899 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2905 /* name from source dir */
2906 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2910 /* .. from source child */
2911 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2912 /* source child can be directory, count by source dir's nlink */
2913 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2916 if (mdd_spobj != mdd_tpobj) {
2917 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2922 rc = mdo_declare_index_insert(env, mdd_sobj,
2923 mdd_object_fid(mdd_tpobj),
2924 S_IFDIR, dotdot, handle);
2929 /* new target child can be directory,
2930 * counted by target dir's nlink */
2931 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2936 la->la_valid = LA_CTIME | LA_MTIME;
2937 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2941 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2945 la->la_valid = LA_CTIME;
2947 la->la_valid |= LA_PROJID;
2948 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2952 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2957 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
2958 mdd_object_type(mdd_sobj),
2959 tname->ln_name, handle);
2963 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2964 /* delete target child in target parent directory */
2965 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2970 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2974 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2975 /* target child can be directory,
2976 * delete "." reference in target child directory */
2977 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2981 /* delete ".." reference in target parent directory */
2982 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2987 la->la_valid = LA_CTIME;
2988 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2992 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2997 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
3005 static int mdd_migrate_object(const struct lu_env *env,
3006 struct mdd_object *spobj,
3007 struct mdd_object *tpobj,
3008 struct mdd_object *sobj,
3009 struct mdd_object *tobj,
3010 const struct lu_name *sname,
3011 const struct lu_name *tname,
3012 struct md_op_spec *spec,
3013 struct md_attr *ma);
3015 /* src object can be remote that is why we use only fid and type of object */
3016 static int mdd_rename(const struct lu_env *env,
3017 struct md_object *src_pobj, struct md_object *tgt_pobj,
3018 const struct lu_fid *lf, const struct lu_name *lsname,
3019 struct md_object *tobj, const struct lu_name *ltname,
3022 const char *sname = lsname->ln_name;
3023 const char *tname = ltname->ln_name;
3024 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3025 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3026 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
3027 struct mdd_device *mdd = mdo2mdd(src_pobj);
3028 struct mdd_object *mdd_sobj = NULL; /* source object */
3029 struct mdd_object *mdd_tobj = NULL;
3030 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
3031 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3032 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3033 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3034 struct thandle *handle;
3035 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3036 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3037 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3040 bool tobj_locked = 0;
3041 bool change_projid = false;
3042 unsigned cl_flags = 0;
3046 /* let unlink to complete and commit */
3047 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3050 mdd_tobj = md2mdd_obj(tobj);
3052 mdd_sobj = mdd_object_find(env, mdd, lf);
3053 if (IS_ERR(mdd_sobj))
3054 RETURN(PTR_ERR(mdd_sobj));
3056 rc = mdd_la_get(env, mdd_sobj, cattr);
3058 GOTO(out_pending, rc);
3060 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3061 * hard links, and target doesn't exist.
3063 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3064 cattr->la_nlink == 1 && !tobj) {
3065 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3066 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3069 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3072 GOTO(out_pending, rc);
3074 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3075 if (IS_ERR(mdd_tobj))
3076 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3078 memset(spec, 0, sizeof(*spec));
3079 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3080 mdd_tobj, lsname, ltname, spec, ma);
3081 mdd_object_put(env, mdd_tobj);
3082 GOTO(out_pending, rc);
3085 rc = mdd_la_get(env, mdd_spobj, pattr);
3087 GOTO(out_pending, rc);
3090 rc = mdd_la_get(env, mdd_tobj, tattr);
3092 GOTO(out_pending, rc);
3093 /* search for an existing archive.
3094 * we should check ahead as the object
3095 * can be destroyed in this transaction */
3096 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3097 cl_flags |= CLF_RENAME_LAST_EXISTS;
3100 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3102 GOTO(out_pending, rc);
3104 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3105 mdd_sobj, cattr, mdd_tobj, tattr);
3107 GOTO(out_pending, rc);
3109 rc = mdd_name_check(env, mdd, ltname);
3111 GOTO(out_pending, rc);
3113 handle = mdd_trans_create(env, mdd);
3115 GOTO(out_pending, rc = PTR_ERR(handle));
3117 memset(ldata, 0, sizeof(*ldata));
3118 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3119 lsname, mdd_object_fid(mdd_tpobj), ltname,
3124 if (tpattr->la_projid != cattr->la_projid &&
3125 tpattr->la_flags & LUSTRE_PROJINHERIT_FL)
3126 change_projid = true;
3128 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3129 mdd_tobj, lsname, ltname, ma, ldata,
3130 change_projid, handle);
3134 rc = mdd_trans_start(env, mdd, handle);
3138 is_dir = S_ISDIR(cattr->la_mode);
3140 /* Remove source name from source directory */
3141 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3145 /* "mv dir1 dir2" needs "dir1/.." link update */
3146 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3147 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3149 GOTO(fixup_spobj2, rc);
3151 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3154 GOTO(fixup_spobj, rc);
3157 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3158 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3160 /* tname might been renamed to something else */
3161 GOTO(fixup_spobj, rc);
3164 /* Insert new fid with target name into target dir */
3165 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3168 GOTO(fixup_tpobj, rc);
3170 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3171 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3173 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3174 la->la_valid = LA_CTIME;
3175 if (change_projid) {
3176 /* mdd_update_time honors other valid flags except TIME ones */
3177 la->la_valid |= LA_PROJID;
3178 la->la_projid = tpattr->la_projid;
3180 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3182 GOTO(fixup_tpobj, rc);
3184 /* Update the linkEA for the source object */
3185 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3186 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3187 lsname, mdd_object_fid(mdd_tpobj), ltname,
3188 handle, ldata, 0, 0);
3190 /* Old files might not have EA entry */
3191 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3192 lsname, handle, NULL, 0);
3193 mdd_write_unlock(env, mdd_sobj);
3194 /* We don't fail the transaction if the link ea can't be
3195 updated -- fid2path will use alternate lookup method. */
3198 /* Remove old target object
3199 * For tobj is remote case cmm layer has processed
3200 * and set tobj to NULL then. So when tobj is NOT NULL,
3201 * it must be local one.
3203 if (tobj && mdd_object_exists(mdd_tobj)) {
3204 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3206 if (mdd_is_dead_obj(mdd_tobj)) {
3207 /* shld not be dead, something is wrong */
3208 CERROR("tobj is dead, something is wrong\n");
3212 mdo_ref_del(env, mdd_tobj, handle);
3214 /* Remove dot reference. */
3215 if (S_ISDIR(tattr->la_mode))
3216 mdo_ref_del(env, mdd_tobj, handle);
3219 /* fetch updated nlink */
3220 rc = mdd_la_get(env, mdd_tobj, tattr);
3222 CERROR("%s: Failed to get nlink for tobj "
3224 mdd2obd_dev(mdd)->obd_name,
3225 PFID(tpobj_fid), rc);
3226 GOTO(fixup_tpobj, rc);
3229 la->la_valid = LA_CTIME;
3230 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3232 CERROR("%s: Failed to set ctime for tobj "
3234 mdd2obd_dev(mdd)->obd_name,
3235 PFID(tpobj_fid), rc);
3236 GOTO(fixup_tpobj, rc);
3239 /* XXX: this transfer to ma will be removed with LOD/OSP */
3240 ma->ma_attr = *tattr;
3241 ma->ma_valid |= MA_INODE;
3242 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3245 CERROR("%s: Failed to unlink tobj "
3247 mdd2obd_dev(mdd)->obd_name,
3248 PFID(tpobj_fid), rc);
3249 GOTO(fixup_tpobj, rc);
3252 /* fetch updated nlink */
3253 rc = mdd_la_get(env, mdd_tobj, tattr);
3254 if (rc == -ENOENT) {
3255 /* the object got removed, let's
3256 * return the latest known attributes */
3257 tattr->la_nlink = 0;
3259 } else if (rc != 0) {
3260 CERROR("%s: Failed to get nlink for tobj "
3262 mdd2obd_dev(mdd)->obd_name,
3263 PFID(tpobj_fid), rc);
3264 GOTO(fixup_tpobj, rc);
3266 /* XXX: this transfer to ma will be removed with LOD/OSP */
3267 ma->ma_attr = *tattr;
3268 ma->ma_valid |= MA_INODE;
3270 if (tattr->la_nlink == 0)
3271 cl_flags |= CLF_RENAME_LAST;
3273 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3276 la->la_valid = LA_CTIME | LA_MTIME;
3277 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3279 GOTO(fixup_tpobj, rc);
3281 if (mdd_spobj != mdd_tpobj) {
3282 la->la_valid = LA_CTIME | LA_MTIME;
3283 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3285 GOTO(fixup_tpobj, rc);
3292 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3294 CWARN("tp obj fix error %d\n",rc2);
3296 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3297 !mdd_is_dead_obj(mdd_tobj)) {
3299 mdo_ref_add(env, mdd_tobj, handle);
3301 mdo_ref_add(env, mdd_tobj, handle);
3304 rc2 = __mdd_index_insert(env, mdd_tpobj,
3305 mdd_object_fid(mdd_tobj),
3306 mdd_object_type(mdd_tobj),
3309 CWARN("tp obj fix error: rc = %d\n", rc2);
3314 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3315 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3317 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3318 mdd2obd_dev(mdd)->obd_name, rc2);
3321 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3324 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3325 mdd2obd_dev(mdd)->obd_name, rc2);
3330 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3331 mdd_object_type(mdd_sobj), sname,
3334 CWARN("sp obj fix error: rc = %d\n", rc2);
3339 mdd_write_unlock(env, mdd_tobj);
3342 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3343 mdd_tobj, tpobj_fid, lf, spobj_fid,
3344 ltname, lsname, handle);
3347 rc = mdd_trans_stop(env, mdd, rc, handle);
3350 mdd_object_put(env, mdd_sobj);
3355 * Check whether we should migrate the file/dir
3357 * < 0 permission check failed or other error.
3358 * = 0 the file can be migrated.
3360 static int mdd_migrate_sanity_check(const struct lu_env *env,
3361 struct mdd_device *mdd,
3362 struct mdd_object *spobj,
3363 struct mdd_object *tpobj,
3364 struct mdd_object *sobj,
3365 struct mdd_object *tobj,
3366 const struct lu_attr *spattr,
3367 const struct lu_attr *tpattr,
3368 const struct lu_attr *attr)
3374 if (!mdd_object_remote(sobj)) {
3375 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3376 if (sobj->mod_count > 0) {
3377 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3378 mdd_obj_dev_name(sobj),
3379 PFID(mdd_object_fid(sobj)),
3381 mdd_read_unlock(env, sobj);
3384 mdd_read_unlock(env, sobj);
3387 if (mdd_object_exists(tobj))
3390 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3394 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3399 struct mdd_xattr_entry {
3400 struct list_head mxe_linkage;
3402 struct lu_buf mxe_buf;
3406 struct lu_buf mx_namebuf;
3407 struct list_head mx_list;
3410 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3412 INIT_LIST_HEAD(&xattrs->mx_list);
3413 xattrs->mx_namebuf.lb_buf = NULL;
3414 xattrs->mx_namebuf.lb_len = 0;
3417 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3419 struct mdd_xattr_entry *entry;
3420 struct mdd_xattr_entry *tmp;
3422 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3423 lu_buf_free(&entry->mxe_buf);
3424 list_del(&entry->mxe_linkage);
3425 OBD_FREE_PTR(entry);
3428 lu_buf_free(&xattrs->mx_namebuf);
3431 /* read xattrs into buf, but skip LMA, LMV, LINKEA if 'skip_linkea' is
3432 * set, and DMV if 'skip_dmv" is set.
3434 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3435 struct mdd_xattrs *xattrs,
3436 struct mdd_object *sobj,
3440 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3441 struct mdd_xattr_entry *entry;
3442 bool needencxattr = false;
3443 bool encxattrfound = false;
3453 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3454 if (list_xsize == -ENODATA)
3460 if (attr->la_valid & LA_FLAGS &&
3461 attr->la_flags & LUSTRE_ENCRYPT_FL) {
3462 needencxattr = true;
3464 strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3467 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3468 if (xattrs->mx_namebuf.lb_buf == NULL)
3471 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3477 xname = xattrs->mx_namebuf.lb_buf;
3479 for (; rem > 0; xname += xlen, rem -= xlen) {
3481 strcmp(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3482 encxattrfound = true;
3483 xlen = strnlen(xname, rem - 1) + 1;
3484 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3485 strcmp(XATTR_NAME_LMV, xname) == 0)
3489 strcmp(XATTR_NAME_LINK, xname) == 0)
3493 strcmp(XATTR_NAME_DEFAULT_LMV, xname) == 0)
3496 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3497 if (xsize == -ENODATA)
3500 GOTO(fini, rc = xsize);
3502 OBD_ALLOC_PTR(entry);
3504 GOTO(fini, rc = -ENOMEM);
3506 lu_buf_alloc(&entry->mxe_buf, xsize);
3507 if (!entry->mxe_buf.lb_buf) {
3508 OBD_FREE_PTR(entry);
3509 GOTO(fini, rc = -ENOMEM);
3512 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3514 lu_buf_free(&entry->mxe_buf);
3515 OBD_FREE_PTR(entry);
3521 entry->mxe_name = xname;
3522 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3525 if (needencxattr && !encxattrfound) {
3526 xlen = strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3527 strncpy(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT, xlen);
3534 mdd_xattrs_fini(xattrs);
3538 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3539 struct mdd_object *obj,
3540 const struct lu_buf *buf,
3542 int fl, struct thandle *handle);
3544 static int mdd_foreach_xattr(const struct lu_env *env,
3545 struct mdd_object *tobj,
3546 struct mdd_xattrs *xattrs,
3547 struct thandle *handle,
3550 struct mdd_xattr_entry *entry;
3553 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
3554 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
3562 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3563 struct mdd_object *sobj,
3564 struct mdd_object *tobj,
3565 const struct lu_name *sname,
3566 const struct lu_fid *sfid,
3567 const struct lu_name *lname,
3568 const struct lu_fid *fid,
3570 struct thandle *handle);
3572 static int mdd_declare_update_link(const struct lu_env *env,
3573 struct mdd_object *sobj,
3574 struct mdd_object *tobj,
3575 const struct lu_name *tname,
3576 const struct lu_fid *tpfid,
3577 const struct lu_name *lname,
3578 const struct lu_fid *fid,
3580 struct thandle *handle)
3582 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3583 struct mdd_object *pobj;
3587 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3588 !strcmp(tname->ln_name, lname->ln_name))
3591 pobj = mdd_object_find(env, mdd, fid);
3593 return PTR_ERR(pobj);
3596 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3598 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3599 mdd_object_type(sobj),
3600 lname->ln_name, handle);
3601 mdd_object_put(env, pobj);
3605 rc = mdo_declare_ref_add(env, tobj, handle);
3609 rc = mdo_declare_ref_del(env, sobj, handle);
3613 static int mdd_update_link(const struct lu_env *env,
3614 struct mdd_object *sobj,
3615 struct mdd_object *tobj,
3616 const struct lu_name *tname,
3617 const struct lu_fid *tpfid,
3618 const struct lu_name *lname,
3619 const struct lu_fid *fid,
3621 struct thandle *handle)
3623 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3624 struct mdd_object *pobj;
3630 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3631 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3634 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3635 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3637 pobj = mdd_object_find(env, mdd, fid);
3639 CWARN("%s: cannot find obj "DFID": %ld\n",
3640 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3641 RETURN(PTR_ERR(pobj));
3644 if (!mdd_object_exists(pobj)) {
3645 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3646 mdd_object_put(env, pobj);
3650 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3651 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3653 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3654 mdd_object_type(sobj),
3655 lname->ln_name, handle);
3656 mdd_write_unlock(env, pobj);
3657 mdd_object_put(env, pobj);
3661 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3662 rc = mdo_ref_add(env, tobj, handle);
3663 mdd_write_unlock(env, tobj);
3667 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3668 rc = mdo_ref_del(env, sobj, handle);
3669 mdd_write_unlock(env, sobj);
3674 static inline int mdd_fld_lookup(const struct lu_env *env,
3675 struct mdd_device *mdd,
3676 const struct lu_fid *fid,
3679 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3680 struct seq_server_site *ss;
3683 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3685 range->lsr_flags = LU_SEQ_RANGE_MDT;
3686 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3690 *mdt_index = range->lsr_index;
3695 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3696 struct mdd_object *sobj,
3697 struct mdd_object *tobj,
3698 const struct lu_name *tname,
3699 const struct lu_fid *tpfid,
3700 const struct lu_name *lname,
3701 const struct lu_fid *fid,
3703 struct thandle *handle)
3705 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3706 __u32 source_mdt_index = *(__u32 *)opaque;
3707 __u32 link_mdt_index;
3713 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3714 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3717 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3721 RETURN(link_mdt_index == source_mdt_index);
3724 static int mdd_iterate_linkea(const struct lu_env *env,
3725 struct mdd_object *sobj,
3726 struct mdd_object *tobj,
3727 const struct lu_name *tname,
3728 const struct lu_fid *tpfid,
3729 struct linkea_data *ldata,
3731 struct thandle *handle,
3734 struct mdd_thread_info *info = mdd_env_info(env);
3735 char *filename = info->mdi_name;
3736 struct lu_name lname;
3743 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3744 linkea_next_entry(ldata)) {
3745 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3748 /* Note: lname might miss \0 at the end */
3749 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3750 lname.ln_namelen, lname.ln_name);
3751 lname.ln_name = filename;
3753 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3755 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3763 * Prepare linkea, and check whether file needs migrate: if source still has
3764 * link on source MDT, no need to migrate, just update namespace on source and
3767 * \retval 0 do migrate
3768 * \retval 1 don't migrate
3769 * \retval -errno on failure
3771 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3772 struct mdd_device *mdd,
3773 struct mdd_object *spobj,
3774 struct mdd_object *tpobj,
3775 struct mdd_object *sobj,
3776 const struct lu_name *sname,
3777 const struct lu_name *tname,
3778 const struct lu_attr *attr,
3779 struct linkea_data *ldata)
3781 __u32 source_mdt_index;
3786 memset(ldata, 0, sizeof(*ldata));
3787 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3788 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3793 * Then it will check if the file should be migrated. If the file has
3794 * mulitple links, we only need migrate the file if all of its entries
3795 * has been migrated to the remote MDT.
3797 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
3800 /* If there are still links locally, don't migrate this file */
3801 LASSERT(ldata->ld_leh != NULL);
3804 * If linkEA is overflow, it means there are some unknown name entries
3805 * under unknown parents, which will prevent the migration.
3807 if (unlikely(ldata->ld_leh->leh_overflow_time))
3810 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
3814 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
3815 ldata, &source_mdt_index, NULL,
3816 mdd_is_link_on_source_mdt);
3820 static int mdd_declare_migrate_update(const struct lu_env *env,
3821 struct mdd_object *spobj,
3822 struct mdd_object *tpobj,
3823 struct mdd_object *obj,
3824 const struct lu_name *sname,
3825 const struct lu_name *tname,
3826 struct lu_attr *attr,
3827 struct lu_attr *spattr,
3828 struct lu_attr *tpattr,
3829 struct linkea_data *ldata,
3831 struct thandle *handle)
3833 struct mdd_thread_info *info = mdd_env_info(env);
3834 struct lu_attr *la = &info->mdi_la_for_fix;
3837 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
3841 if (S_ISDIR(attr->la_mode)) {
3842 rc = mdo_declare_ref_del(env, spobj, handle);
3847 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
3848 attr->la_mode & S_IFMT,
3849 tname->ln_name, handle);
3853 rc = mdd_declare_links_add(env, obj, handle, ldata);
3857 if (S_ISDIR(attr->la_mode)) {
3858 rc = mdo_declare_ref_add(env, tpobj, handle);
3863 la->la_valid = LA_CTIME | LA_MTIME;
3864 rc = mdo_declare_attr_set(env, spobj, la, handle);
3868 if (tpobj != spobj) {
3869 rc = mdo_declare_attr_set(env, tpobj, la, handle);
3877 static int mdd_declare_migrate_create(const struct lu_env *env,
3878 struct mdd_object *spobj,
3879 struct mdd_object *tpobj,
3880 struct mdd_object *sobj,
3881 struct mdd_object *tobj,
3882 const struct lu_name *sname,
3883 const struct lu_name *tname,
3884 struct lu_attr *spattr,
3885 struct lu_attr *tpattr,
3886 struct lu_attr *attr,
3887 struct lu_buf *sbuf,
3888 struct linkea_data *ldata,
3889 struct mdd_xattrs *xattrs,
3891 struct md_op_spec *spec,
3892 struct dt_allocation_hint *hint,
3893 struct thandle *handle)
3895 struct mdd_thread_info *info = mdd_env_info(env);
3896 struct md_layout_change *mlc = &info->mdi_mlc;
3897 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
3902 if (S_ISDIR(attr->la_mode)) {
3903 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
3905 mlc->mlc_opc = MD_LAYOUT_DETACH;
3906 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
3910 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3911 } else if (S_ISLNK(attr->la_mode)) {
3912 spec->u.sp_symname.ln_name = sbuf->lb_buf;
3913 /* don't count NUL */
3914 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
3915 } else if (S_ISREG(attr->la_mode)) {
3916 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
3917 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
3920 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
3922 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
3923 tname, attr, handle, spec, ldata, NULL, NULL,
3929 * tobj mode will be used in mdo_declare_layout_change(), but it's not
3930 * createb yet, copy from sobj.
3932 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
3933 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
3934 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
3936 if (S_ISDIR(attr->la_mode)) {
3938 /* if sobj is not striped, fake a 1-stripe LMV */
3939 LASSERT(sizeof(info->mdi_key) >
3940 lmv_mds_md_size(1, LMV_MAGIC_V1));
3941 lmv = (typeof(lmv))info->mdi_key;
3942 memset(lmv, 0, sizeof(*lmv));
3943 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3944 lmv->lmv_stripe_count = cpu_to_le32(1);
3945 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
3946 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
3947 mdd_object_fid(sobj));
3948 mlc->mlc_buf.lb_buf = lmv;
3949 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
3951 mlc->mlc_buf = *sbuf;
3953 mlc->mlc_opc = MD_LAYOUT_ATTACH;
3954 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
3959 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
3960 mdo_declare_xattr_set);
3964 if (S_ISREG(attr->la_mode)) {
3965 struct lu_buf fid_buf;
3967 handle->th_complex = 1;
3969 /* target may be remote, update PFID via sobj. */
3970 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
3971 fid_buf.lb_len = sizeof(struct lu_fid);
3972 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
3977 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
3982 if (!S_ISDIR(attr->la_mode)) {
3983 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
3984 mdd_object_fid(tpobj), ldata, NULL,
3985 handle, mdd_declare_update_link);
3990 if (!S_ISDIR(attr->la_mode) || lmv) {
3991 rc = mdo_declare_ref_del(env, sobj, handle);
3995 if (S_ISDIR(attr->la_mode)) {
3996 rc = mdo_declare_ref_del(env, sobj, handle);
4001 rc = mdo_declare_destroy(env, sobj, handle);
4006 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
4007 attr, spattr, tpattr, ldata, ma,
4013 * migrate dirent from \a spobj to \a tpobj.
4015 static int mdd_migrate_update(const struct lu_env *env,
4016 struct mdd_object *spobj,
4017 struct mdd_object *tpobj,
4018 struct mdd_object *obj,
4019 const struct lu_name *sname,
4020 const struct lu_name *tname,
4021 struct lu_attr *attr,
4022 struct lu_attr *spattr,
4023 struct lu_attr *tpattr,
4024 struct linkea_data *ldata,
4026 struct thandle *handle)
4028 struct mdd_thread_info *info = mdd_env_info(env);
4029 struct lu_attr *la = &info->mdi_la_for_fix;
4034 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
4035 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
4036 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
4038 rc = __mdd_index_delete(env, spobj, sname->ln_name,
4039 S_ISDIR(attr->la_mode), handle);
4043 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
4044 attr->la_mode & S_IFMT,
4045 tname->ln_name, handle);
4049 rc = mdd_links_write(env, obj, ldata, handle);
4053 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4054 la->la_valid = LA_CTIME | LA_MTIME;
4055 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4056 rc = mdd_update_time(env, spobj, spattr, la, handle);
4057 mdd_write_unlock(env, spobj);
4061 if (tpobj != spobj) {
4062 la->la_valid = LA_CTIME | LA_MTIME;
4063 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4064 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4065 mdd_write_unlock(env, tpobj);
4074 * Migrate file/dir to target MDT.
4076 * Create target according to \a spec, and then migrate xattrs, if it's
4077 * directory, migrate source stripes to target.
4079 * \param[in] env execution environment
4080 * \param[in] spobj source parent object
4081 * \param[in] tpobj target parent object
4082 * \param[in] sobj source object
4083 * \param[in] tobj target object
4084 * \param[in] lname file name
4085 * \param[in] spattr source parent attributes
4086 * \param[in] tpattr target parent attributes
4087 * \param[in] attr source attributes
4088 * \param[in] sbuf source LMV buf
4089 * \param[in] spec migrate create spec
4090 * \param[in] hint target creation hint
4091 * \param[in] handle tranasction handle
4093 * \retval 0 on success
4094 * \retval -errno on failure
4096 static int mdd_migrate_create(const struct lu_env *env,
4097 struct mdd_object *spobj,
4098 struct mdd_object *tpobj,
4099 struct mdd_object *sobj,
4100 struct mdd_object *tobj,
4101 const struct lu_name *sname,
4102 const struct lu_name *tname,
4103 struct lu_attr *spattr,
4104 struct lu_attr *tpattr,
4105 struct lu_attr *attr,
4106 const struct lu_buf *sbuf,
4107 struct linkea_data *ldata,
4108 struct mdd_xattrs *xattrs,
4110 struct md_op_spec *spec,
4111 struct dt_allocation_hint *hint,
4112 struct thandle *handle)
4119 * migrate sobj stripes to tobj if it's directory:
4120 * 1. detach stripes from sobj.
4121 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4122 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4124 if (S_ISDIR(attr->la_mode)) {
4125 struct mdd_thread_info *info = mdd_env_info(env);
4126 struct md_layout_change *mlc = &info->mdi_mlc;
4128 mlc->mlc_opc = MD_LAYOUT_DETACH;
4130 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4131 rc = mdo_layout_change(env, sobj, mlc, handle);
4132 mdd_write_unlock(env, sobj);
4137 /* don't set nlink from sobj */
4138 attr->la_valid &= ~LA_NLINK;
4140 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4141 hint, handle, false);
4145 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4146 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4147 mdd_write_unlock(env, tobj);
4151 /* for regular file, update OST objects XATTR_NAME_FID */
4152 if (S_ISREG(attr->la_mode)) {
4153 struct lu_buf fid_buf;
4155 /* target may be remote, update PFID via sobj. */
4156 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4157 fid_buf.lb_len = sizeof(struct lu_fid);
4158 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4163 /* delete LOV to avoid deleting OST objs when destroying sobj */
4164 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4165 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4166 mdd_write_unlock(env, sobj);
4167 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4168 if (rc && rc != -ENODATA)
4173 /* update links FID */
4174 if (!S_ISDIR(attr->la_mode)) {
4175 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4176 mdd_object_fid(tpobj), ldata,
4177 NULL, handle, mdd_update_link);
4182 /* don't destroy sobj if it's plain directory */
4183 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4184 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4185 rc = mdo_ref_del(env, sobj, handle);
4187 if (S_ISDIR(attr->la_mode))
4188 rc = mdo_ref_del(env, sobj, handle);
4190 rc = mdo_destroy(env, sobj, handle);
4192 mdd_write_unlock(env, sobj);
4197 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4198 spattr, tpattr, ldata, ma, handle);
4203 /* NB: if user issued different migrate command, we can't adjust it silently
4204 * here, because this command will decide target MDT in subdir migration in
4207 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4208 const struct lmv_mds_md_v1 *lmv,
4209 const struct lmv_user_md_v1 *lum,
4210 const struct lu_name *lname)
4212 __u32 lum_stripe_count = lum->lum_stripe_count;
4213 __u32 lum_hash_type = lum->lum_hash_type &
4214 cpu_to_le32(LMV_HASH_TYPE_MASK);
4215 __u32 lmv_hash_type = lmv->lmv_hash_type &
4216 cpu_to_le32(LMV_HASH_TYPE_MASK);
4218 if (!lmv_is_sane(lmv))
4221 /* if stripe_count unspecified, set to 1 */
4222 if (!lum_stripe_count)
4223 lum_stripe_count = cpu_to_le32(1);
4225 /* TODO: check specific MDTs */
4226 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4227 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4228 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4229 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4230 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4231 le32_to_cpu(lmv->lmv_master_mdt_index),
4232 le32_to_cpu(lmv->lmv_migrate_offset),
4233 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4242 * Internal function to migrate directory or file between MDTs.
4244 * migrate source to target in following steps:
4245 * 1. create target, append source stripes after target's if it's directory,
4246 * migrate xattrs and update fid of source links.
4247 * 2. update namespace: migrate dirent from source parent to target parent,
4248 * update file linkea, and destroy source if it's not needed any more.
4250 * \param[in] env execution environment
4251 * \param[in] spobj source parent object
4252 * \param[in] tpobj target parent object
4253 * \param[in] sobj source object
4254 * \param[in] tobj target object
4255 * \param[in] sname source file name
4256 * \param[in] tname target file name
4257 * \param[in] spec target creation spec
4258 * \param[in] ma used to update \a pobj mtime and ctime
4260 * \retval 0 on success
4261 * \retval -errno on failure
4263 static int mdd_migrate_object(const struct lu_env *env,
4264 struct mdd_object *spobj,
4265 struct mdd_object *tpobj,
4266 struct mdd_object *sobj,
4267 struct mdd_object *tobj,
4268 const struct lu_name *sname,
4269 const struct lu_name *tname,
4270 struct md_op_spec *spec,
4273 struct mdd_thread_info *info = mdd_env_info(env);
4274 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4275 struct lu_attr *spattr = &info->mdi_pattr;
4276 struct lu_attr *tpattr = &info->mdi_tpattr;
4277 struct lu_attr *attr = &info->mdi_cattr;
4278 struct linkea_data *ldata = &info->mdi_link_data;
4279 struct dt_allocation_hint *hint = &info->mdi_hint;
4280 struct lu_buf sbuf = { NULL };
4281 struct mdd_xattrs xattrs;
4282 struct lmv_mds_md_v1 *lmv;
4283 struct thandle *handle;
4288 rc = mdd_la_get(env, sobj, attr);
4292 rc = mdd_la_get(env, spobj, spattr);
4296 rc = mdd_la_get(env, tpobj, tpattr);
4300 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4301 spattr, tpattr, attr);
4305 mdd_xattrs_init(&xattrs);
4307 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4308 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4312 /* if user use default value '0' for stripe_count, we need to
4313 * adjust it to '1' to create a 1-stripe directory.
4315 if (lum->lum_stripe_count == 0)
4316 lum->lum_stripe_count = cpu_to_le32(1);
4318 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4319 if (rc && rc != -ENODATA)
4324 if (!lmv_is_sane(lmv))
4325 GOTO(out, rc = -EBADF);
4326 if (lmv_is_migrating(lmv)) {
4327 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4332 } else if (!S_ISDIR(attr->la_mode)) {
4334 GOTO(out, rc = -EALREADY);
4336 /* update namespace only if @sobj is on MDT where @tpobj is. */
4337 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4338 spec->sp_migrate_nsonly = true;
4340 if (S_ISLNK(attr->la_mode)) {
4341 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4343 GOTO(out, rc = -ENOMEM);
4345 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4348 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4349 mdd2obd_dev(mdd)->obd_name,
4350 PFID(mdd_object_fid(sobj)), rc);
4356 /* linkea needs update upon FID or parent stripe change */
4357 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4358 tname, attr, ldata);
4360 /* update namespace only if @sobj has link on its MDT. */
4361 spec->sp_migrate_nsonly = true;
4365 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4366 * RPCs inside transaction.
4368 if (!spec->sp_migrate_nsonly) {
4369 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, true, true);
4374 handle = mdd_trans_create(env, mdd);
4376 GOTO(out, rc = PTR_ERR(handle));
4378 if (spec->sp_migrate_nsonly)
4379 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4380 tname, attr, spattr, tpattr,
4383 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4384 sname, tname, spattr, tpattr,
4385 attr, &sbuf, ldata, &xattrs, ma,
4386 spec, hint, handle);
4390 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4395 rc = mdd_trans_start(env, mdd, handle);
4399 if (spec->sp_migrate_nsonly)
4400 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4401 attr, spattr, tpattr, ldata, ma,
4404 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4405 tname, spattr, tpattr, attr, &sbuf,
4406 ldata, &xattrs, ma, spec, hint, handle);
4410 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4411 spec->sp_migrate_nsonly ? sobj : tobj,
4412 mdd_object_fid(spobj), mdd_object_fid(sobj),
4413 mdd_object_fid(tpobj), tname, sname,
4420 rc = mdd_trans_stop(env, mdd, rc, handle);
4422 mdd_xattrs_fini(&xattrs);
4429 * Migrate directory or file between MDTs.
4431 * \param[in] env execution environment
4432 * \param[in] md_pobj parent master object
4433 * \param[in] md_sobj source object
4434 * \param[in] lname file name
4435 * \param[in] md_tobj target object
4436 * \param[in] spec target creation spec
4437 * \param[in] ma used to update \a pobj mtime and ctime
4439 * \retval 0 on success
4440 * \retval -errno on failure
4442 static int mdd_migrate(const struct lu_env *env, struct md_object *md_pobj,
4443 struct md_object *md_sobj, const struct lu_name *lname,
4444 struct md_object *md_tobj, struct md_op_spec *spec,
4447 struct mdd_thread_info *info = mdd_env_info(env);
4448 struct mdd_device *mdd = mdo2mdd(md_pobj);
4449 struct mdd_object *pobj = md2mdd_obj(md_pobj);
4450 struct mdd_object *sobj = md2mdd_obj(md_sobj);
4451 struct mdd_object *tobj = md2mdd_obj(md_tobj);
4452 struct mdd_object *spobj = NULL;
4453 struct mdd_object *tpobj = NULL;
4454 struct lu_buf pbuf = { NULL };
4455 struct lu_fid *fid = &info->mdi_fid2;
4456 struct lmv_mds_md_v1 *lmv;
4461 /* locate source and target stripe on pobj, which are the real parent */
4462 rc = mdd_stripe_get(env, pobj, &pbuf, XATTR_NAME_LMV);
4463 if (rc < 0 && rc != -ENODATA)
4470 if (!lmv_is_sane(lmv))
4471 GOTO(out, rc = -EBADF);
4473 /* locate target parent stripe */
4474 /* fail check here to make sure top dir migration succeed. */
4475 if (lmv_is_migrating(lmv) &&
4476 OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_ENTRIES, 0))
4477 GOTO(out, rc = -EIO);
4479 index = lmv_name_to_stripe_index(lmv, lname->ln_name,
4482 GOTO(out, rc = index);
4484 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4485 tpobj = mdd_object_find(env, mdd, fid);
4487 GOTO(out, rc = PTR_ERR(tpobj));
4489 /* locate source parent stripe */
4490 if (lmv_is_layout_changing(lmv)) {
4491 index = lmv_name_to_stripe_index_old(lmv,
4495 GOTO(out, rc = index);
4497 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[index]);
4498 spobj = mdd_object_find(env, mdd, fid);
4500 GOTO(out, rc = PTR_ERR(spobj));
4502 /* parent stripe unchanged */
4503 if (spobj == tpobj) {
4504 if (!lmv_is_restriping(lmv))
4505 GOTO(out, rc = -EINVAL);
4506 GOTO(out, rc = -EALREADY);
4510 mdd_object_get(spobj);
4515 mdd_object_get(tpobj);
4516 mdd_object_get(spobj);
4519 rc = mdd_migrate_object(env, spobj, tpobj, sobj, tobj, lname, lname,
4524 if (!IS_ERR_OR_NULL(spobj))
4525 mdd_object_put(env, spobj);
4526 if (!IS_ERR_OR_NULL(tpobj))
4527 mdd_object_put(env, tpobj);
4533 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4534 struct mdd_object *pobj,
4535 struct mdd_object *obj,
4536 struct mdd_object *stripe,
4537 struct lu_attr *attr,
4538 struct mdd_xattrs *xattrs,
4539 struct md_layout_change *mlc,
4540 struct lu_name *lname,
4541 struct thandle *handle)
4545 mlc->mlc_opc = MD_LAYOUT_DETACH;
4546 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4550 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4551 S_IFDIR, dotdot, handle);
4555 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
4556 mdo_declare_xattr_set);
4560 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4564 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4568 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4572 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4573 attr->la_mode, lname->ln_name, handle);
4577 rc = mdo_declare_ref_del(env, obj, handle);
4581 rc = mdo_declare_ref_del(env, obj, handle);
4585 rc = mdo_declare_destroy(env, obj, handle);
4592 /* transform one-stripe directory to a plain directory */
4593 static int mdd_1sd_collapse(const struct lu_env *env,
4594 struct mdd_object *pobj,
4595 struct mdd_object *obj,
4596 struct mdd_object *stripe,
4597 struct lu_attr *attr,
4598 struct mdd_xattrs *xattrs,
4599 struct md_layout_change *mlc,
4600 struct lu_name *lname,
4601 struct thandle *handle)
4607 /* replace 1-stripe directory with its stripe */
4608 mlc->mlc_opc = MD_LAYOUT_DETACH;
4610 mdd_write_lock(env, obj, DT_SRC_PARENT);
4611 rc = mdo_layout_change(env, obj, mlc, handle);
4612 mdd_write_unlock(env, obj);
4616 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4617 mdd_write_lock(env, obj, DT_SRC_CHILD);
4619 /* insert dotdot to stripe which points to parent */
4620 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4621 S_IFDIR, dotdot, handle);
4625 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
4630 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4634 /* don't set nlink from parent */
4635 attr->la_valid &= ~LA_NLINK;
4637 rc = mdo_attr_set(env, stripe, attr, handle);
4641 /* delete dir name from parent */
4642 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4646 /* insert stripe to parent with dir name */
4647 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4648 attr->la_mode, lname->ln_name, handle);
4652 /* destroy dir obj */
4653 rc = mdo_ref_del(env, obj, handle);
4657 rc = mdo_ref_del(env, obj, handle);
4661 rc = mdo_destroy(env, obj, handle);
4667 mdd_write_unlock(env, obj);
4668 mdd_write_unlock(env, pobj);
4674 * shrink directory stripes after migration/merge
4676 int mdd_dir_layout_shrink(const struct lu_env *env,
4677 struct md_object *md_obj,
4678 struct md_layout_change *mlc)
4680 struct mdd_device *mdd = mdo2mdd(md_obj);
4681 struct mdd_thread_info *info = mdd_env_info(env);
4682 struct mdd_object *obj = md2mdd_obj(md_obj);
4683 struct mdd_object *pobj = NULL;
4684 struct mdd_object *stripe = NULL;
4685 struct lu_attr *attr = &info->mdi_pattr;
4686 struct lu_fid *fid = &info->mdi_fid2;
4687 struct lu_name lname = { NULL };
4688 struct lu_buf lmv_buf = { NULL };
4689 struct mdd_xattrs xattrs;
4690 struct lmv_mds_md_v1 *lmv;
4691 struct lmv_user_md *lmu;
4692 struct thandle *handle;
4697 rc = mdd_la_get(env, obj, attr);
4701 if (!S_ISDIR(attr->la_mode))
4704 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4708 lmv = lmv_buf.lb_buf;
4709 if (!lmv_is_sane(lmv))
4712 lmu = mlc->mlc_buf.lb_buf;
4714 /* adjust the default value '0' to '1' */
4715 if (lmu->lum_stripe_count == 0)
4716 lmu->lum_stripe_count = cpu_to_le32(1);
4718 /* these were checked in MDT */
4719 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4720 le32_to_cpu(lmv->lmv_stripe_count));
4721 LASSERT(!lmv_is_splitting(lmv));
4722 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4724 mdd_xattrs_init(&xattrs);
4726 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4727 * to a plain dir, which will cause FID change and namespace update.
4729 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4730 struct linkea_data *ldata = &info->mdi_link_data;
4731 char *filename = info->mdi_name;
4733 rc = mdd_links_read(env, obj, ldata);
4737 if (ldata->ld_leh->leh_reccount > 1)
4738 GOTO(out, rc = -EINVAL);
4740 linkea_first_entry(ldata);
4742 GOTO(out, rc = -ENODATA);
4744 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4747 /* Note: lname might miss \0 at the end */
4748 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4749 lname.ln_namelen, lname.ln_name);
4750 lname.ln_name = filename;
4752 pobj = mdd_object_find(env, mdd, fid);
4759 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4761 stripe = mdd_object_find(env, mdd, fid);
4762 if (IS_ERR(stripe)) {
4763 mdd_object_put(env, pobj);
4765 GOTO(out, rc = PTR_ERR(stripe));
4768 if (!lmv_is_fixed(lmv))
4769 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, false,
4773 handle = mdd_trans_create(env, mdd);
4775 GOTO(out, rc = PTR_ERR(handle));
4777 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4778 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4780 GOTO(stop_trans, rc);
4782 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4783 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
4784 &xattrs, mlc, &lname, handle);
4786 GOTO(stop_trans, rc);
4789 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4792 GOTO(stop_trans, rc);
4794 rc = mdd_trans_start(env, mdd, handle);
4796 GOTO(stop_trans, rc);
4798 mdd_write_lock(env, obj, DT_SRC_PARENT);
4799 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4800 rc = mdo_layout_change(env, obj, mlc, handle);
4801 mdd_write_unlock(env, obj);
4803 GOTO(stop_trans, rc);
4805 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4806 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
4807 mlc, &lname, handle);
4809 GOTO(stop_trans, rc);
4812 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4813 XATTR_NAME_LMV, handle);
4814 GOTO(stop_trans, rc);
4817 rc = mdd_trans_stop(env, mdd, rc, handle);
4819 mdd_xattrs_fini(&xattrs);
4821 mdd_object_put(env, stripe);
4822 mdd_object_put(env, pobj);
4824 lu_buf_free(&lmv_buf);
4828 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4829 struct mdd_device *mdd,
4830 struct mdd_object *pobj,
4831 struct mdd_object *obj,
4832 struct mdd_object *tobj,
4833 struct mdd_xattrs *xattrs,
4834 struct md_layout_change *mlc,
4835 struct dt_allocation_hint *hint,
4836 struct thandle *handle)
4838 struct mdd_thread_info *info = mdd_env_info(env);
4839 const struct lu_name *lname = mlc->mlc_name;
4840 struct lu_attr *la = &info->mdi_la_for_fix;
4841 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4842 struct linkea_data *ldata = &info->mdi_link_data;
4843 struct lmv_mds_md_v1 *lmv;
4847 mlc->mlc_opc = MD_LAYOUT_DETACH;
4848 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4852 memset(ldata, 0, sizeof(*ldata));
4853 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4854 lname, 1, 0, ldata);
4858 count = lum->lum_stripe_count;
4859 lum->lum_stripe_count = 0;
4860 /* don't set default LMV since it will become a striped dir */
4861 lum->lum_max_inherit = LMV_INHERIT_NONE;
4862 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4864 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
4865 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
4866 ldata, NULL, NULL, NULL, hint);
4870 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
4873 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
4875 lmv = (typeof(lmv))info->mdi_key;
4876 memset(lmv, 0, sizeof(*lmv));
4877 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4878 lmv->lmv_stripe_count = cpu_to_le32(1);
4879 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4880 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
4882 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4883 mlc->mlc_buf.lb_buf = lmv;
4884 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4885 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4889 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4890 mdo_declare_xattr_set);
4894 lum->lum_stripe_count = count;
4895 mlc->mlc_opc = MD_LAYOUT_SPLIT;
4896 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4900 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4904 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4905 S_IFDIR, lname->ln_name, handle);
4909 la->la_valid = LA_CTIME | LA_MTIME;
4910 rc = mdo_declare_attr_set(env, obj, la, handle);
4914 rc = mdo_declare_attr_set(env, pobj, la, handle);
4918 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4924 * plain directory split:
4925 * 1. create \a tobj as plain directory.
4926 * 2. append \a obj as first stripe of \a tobj.
4927 * 3. migrate xattrs from \a obj to \a tobj.
4928 * 4. split \a tobj to specific stripe count.
4930 static int mdd_dir_split_plain(const struct lu_env *env,
4931 struct mdd_device *mdd,
4932 struct mdd_object *pobj,
4933 struct mdd_object *obj,
4934 struct mdd_object *tobj,
4935 struct mdd_xattrs *xattrs,
4936 struct md_layout_change *mlc,
4937 struct dt_allocation_hint *hint,
4938 struct thandle *handle)
4940 struct mdd_thread_info *info = mdd_env_info(env);
4941 struct lu_attr *pattr = &info->mdi_pattr;
4942 struct lu_attr *la = &info->mdi_la_for_fix;
4943 const struct lu_name *lname = mlc->mlc_name;
4944 struct linkea_data *ldata = &info->mdi_link_data;
4949 /* copy linkea out and set on target later */
4950 rc = mdd_links_read(env, obj, ldata);
4954 mlc->mlc_opc = MD_LAYOUT_DETACH;
4955 rc = mdo_layout_change(env, obj, mlc, handle);
4959 /* don't set nlink from obj */
4960 mlc->mlc_attr->la_valid &= ~LA_NLINK;
4962 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4963 NULL, NULL, NULL, hint, handle, false);
4967 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4971 rc = mdd_links_write(env, tobj, ldata, handle);
4975 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
4979 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
4980 lname->ln_name, handle);
4984 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
4985 la->la_valid = LA_CTIME | LA_MTIME;
4987 mdd_write_lock(env, obj, DT_SRC_CHILD);
4988 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
4989 mdd_write_unlock(env, obj);
4993 rc = mdd_la_get(env, pobj, pattr);
4997 la->la_valid = LA_CTIME | LA_MTIME;
4999 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5000 rc = mdd_update_time(env, pobj, pattr, la, handle);
5001 mdd_write_unlock(env, pobj);
5005 /* FID changes, record it as CL_MIGRATE */
5006 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
5007 mdd_object_fid(pobj), mdd_object_fid(obj),
5008 mdd_object_fid(pobj), lname, lname, handle);
5012 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
5013 struct md_layout_change *mlc)
5015 struct mdd_thread_info *info = mdd_env_info(env);
5016 struct mdd_device *mdd = mdo2mdd(o);
5017 struct mdd_object *obj = md2mdd_obj(o);
5018 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
5019 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
5020 struct dt_allocation_hint *hint = &info->mdi_hint;
5021 bool is_plain = false;
5022 struct mdd_xattrs xattrs;
5023 struct thandle *handle;
5028 LASSERT(S_ISDIR(mdd_object_type(obj)));
5030 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
5036 mdd_xattrs_init(&xattrs);
5038 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true, true);
5040 handle = mdd_trans_create(env, mdd);
5042 GOTO(out, rc = PTR_ERR(handle));
5045 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
5046 &xattrs, mlc, hint, handle);
5048 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5049 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5051 GOTO(stop_trans, rc);
5053 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5057 GOTO(stop_trans, rc);
5059 rc = mdd_trans_start(env, mdd, handle);
5061 GOTO(stop_trans, rc);
5064 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5067 mdd_write_lock(env, obj, DT_TGT_CHILD);
5068 rc = mdo_xattr_set(env, obj, NULL, XATTR_NAME_LMV,
5069 LU_XATTR_CREATE, handle);
5070 mdd_write_unlock(env, obj);
5072 GOTO(stop_trans, rc);
5074 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5075 XATTR_NAME_LMV, handle);
5078 GOTO(stop_trans, rc);
5083 rc = mdd_trans_stop(env, mdd, rc, handle);
5085 mdd_xattrs_fini(&xattrs);
5090 const struct md_dir_operations mdd_dir_ops = {
5091 .mdo_is_subdir = mdd_is_subdir,
5092 .mdo_lookup = mdd_lookup,
5093 .mdo_create = mdd_create,
5094 .mdo_rename = mdd_rename,
5095 .mdo_link = mdd_link,
5096 .mdo_unlink = mdd_unlink,
5097 .mdo_create_data = mdd_create_data,
5098 .mdo_migrate = mdd_migrate,