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 static 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,
825 * Checks that changelog consumes safe amount of space comparing
828 * \param env - current lu_env
829 * \param mdd - current MDD device
830 * \param lgh - changelog catalog llog handle
831 * \param estimate - get exact llog size or estimate it.
835 bool mdd_changelog_is_space_safe(const struct lu_env *env,
836 struct mdd_device *mdd,
837 struct llog_handle *lgh,
840 struct obd_statfs sfs;
841 unsigned long long free_space_limit;
842 unsigned long long llog_size;
845 rc = dt_statfs(env, mdd->mdd_bottom, &sfs);
847 /* check is ignored if OSD is not healthy for any reason */
851 * if changelog consumes more than 1/4 of available space then start
854 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_ENOSPC))
855 free_space_limit = cfs_fail_val;
857 free_space_limit = (sfs.os_bfree * sfs.os_bsize) >> 2;
859 /* if \estimate parameter is used then calculate llog size from
860 * number of used catalog entries and plain llog maximum size.
861 * Plain llog maximum size if set as 1/64 of FS free space limited
862 * by 128MB as maximum and 2MB as minimum, see llog_cat_new_log()
863 * Estimation helps to avoid full llog processing to get exact size
864 * by llog_cat_size().
867 /* use 1/64 of FS size but keep it between 2MB and 128MB */
868 llog_size = clamp_t(unsigned long long,
869 (sfs.os_blocks * sfs.os_bsize) >> 6,
872 * llog_cat_free_space() gives free slots, we need occupied,
873 * so subtruct free from total slots minus one for header
875 llog_size *= LLOG_HDR_BITMAP_SIZE(lgh->lgh_hdr) - 1 -
876 llog_cat_free_space(lgh);
878 /* get exact llog size */
879 llog_size = llog_cat_size(env, lgh);
881 CDEBUG(D_HA, "%s:%s changelog size is %lluMB, space limit is %lluMB\n",
882 mdd2obd_dev(mdd)->obd_name, estimate ? " estimated" : "",
883 llog_size >> 20, free_space_limit >> 20);
885 if (llog_size > free_space_limit) {
886 CWARN("%s: changelog uses %lluMB with %lluMB space limit\n",
887 mdd2obd_dev(mdd)->obd_name, llog_size >> 20,
888 free_space_limit >> 20);
896 * Checks if there is enough space in changelog itself and in FS and force
897 * emergency changelog cleanup if needed. It will purge users one by one
898 * from the oldest one while emergency conditions are true.
900 * \param env - current lu_env
901 * \param mdd - current MDD device
902 * \param lgh - changelog catalog llog handle
904 * \retval true if emergency cleanup is needed for changelog
906 static bool mdd_changelog_emrg_cleanup(const struct lu_env *env,
907 struct mdd_device *mdd,
908 struct llog_handle *lgh)
910 unsigned long free_entries = llog_cat_free_space(lgh);
912 /* free space GC is disabled or is in progress already */
913 if (!mdd->mdd_changelog_free_space_gc || mdd->mdd_changelog_emrg_gc)
916 if (free_entries <= mdd->mdd_changelog_min_free_cat_entries) {
917 CWARN("%s: changelog has only %lu free catalog entries\n",
918 mdd2obd_dev(mdd)->obd_name, free_entries);
919 mdd->mdd_changelog_emrg_gc = true;
923 if (!mdd_changelog_is_space_safe(env, mdd, lgh, true)) {
924 mdd->mdd_changelog_emrg_gc = true;
931 static bool mdd_changelog_need_gc(const struct lu_env *env,
932 struct mdd_device *mdd,
933 struct llog_handle *lgh)
935 struct mdd_changelog *mc = &mdd->mdd_cl;
937 return mdd_changelog_emrg_cleanup(env, mdd, lgh) ||
938 mdd_changelog_is_too_idle(mdd, mc->mc_minrec, mc->mc_mintime) ||
939 CFS_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD);
942 /** Add a changelog entry \a rec to the changelog llog
945 * \param handle - currently ignored since llogs start their own transaction;
946 * this will hopefully be fixed in llog rewrite
949 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
950 struct llog_changelog_rec *rec, struct thandle *th)
952 struct obd_device *obd = mdd2obd_dev(mdd);
953 struct llog_ctxt *ctxt;
954 struct thandle *llog_th;
958 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
959 changelog_rec_varsize(&rec->cr));
961 /* llog_lvfs_write_rec sets the llog tail len */
962 rec->cr_hdr.lrh_type = CHANGELOG_REC;
963 rec->cr.cr_time = cl_time();
965 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
969 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
971 GOTO(out_put, rc = PTR_ERR(llog_th));
973 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
974 /* nested journal transaction */
975 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
977 /* time to recover some space ?? */
978 if (likely(!mdd->mdd_changelog_gc ||
979 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
980 mdd->mdd_changelog_min_gc_interval >=
981 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
982 /* save a spin_lock trip */
985 if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_IDX_PUMP)) {
986 spin_lock(&mdd->mdd_cl.mc_lock);
987 mdd->mdd_cl.mc_index += cfs_fail_val;
988 spin_unlock(&mdd->mdd_cl.mc_lock);
991 need_gc = mdd_changelog_need_gc(env, mdd, ctxt->loc_handle);
992 spin_lock(&mdd->mdd_cl.mc_lock);
993 if (likely(mdd->mdd_changelog_gc &&
994 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
995 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
996 mdd->mdd_changelog_min_gc_interval)) {
997 if (unlikely(need_gc)) {
998 CWARN("%s:%s starting changelog garbage collection\n",
1000 CFS_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
1002 /* indicate further kthread run will occur outside
1003 * right after current journal transaction filling has
1006 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
1008 /* next check in mdd_changelog_min_gc_interval anyway
1010 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
1012 spin_unlock(&mdd->mdd_cl.mc_lock);
1014 llog_ctxt_put(ctxt);
1020 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
1021 const struct lu_fid *sfid,
1022 const struct lu_fid *spfid,
1023 const struct lu_name *sname)
1025 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
1028 LASSERT(sfid != NULL);
1029 LASSERT(spfid != NULL);
1030 LASSERT(sname != NULL);
1032 extsize = sname->ln_namelen + 1;
1034 rnm->cr_sfid = *sfid;
1035 rnm->cr_spfid = *spfid;
1037 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
1038 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
1039 rec->cr_namelen += extsize;
1042 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
1044 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
1046 if (jobid == NULL || jobid[0] == '\0')
1049 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
1052 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
1054 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
1056 ef->cr_extra_flags = eflags;
1059 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
1060 __u64 uid, __u64 gid)
1062 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
1064 uidgid->cr_uid = uid;
1065 uidgid->cr_gid = gid;
1068 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
1071 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
1073 clnid->cr_nid = nid;
1076 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
1078 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
1080 omd->cr_openflags = flags;
1083 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
1084 const char *xattr_name)
1086 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
1088 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
1092 * Set the parent FID at \a pfid for a namespace change changelog record, using
1093 * XATTR_NAME_LMV and linkEA from the remote object to obtain the correct
1094 * parent FID for striped directories
1096 * \param[in] env - environment
1097 * \param[in] mdd - mdd device
1098 * \param[in] parent - parent object
1099 * \param[in] pattr - parent attribute
1100 * \param[out] pfid - parent fid
1103 * \retval -errno failure
1105 int mdd_changelog_ns_pfid_set(const struct lu_env *env, struct mdd_device *mdd,
1106 struct mdd_object *parent,
1107 const struct lu_attr *pattr, struct lu_fid *pfid)
1111 /* Certain userspace tools might rely on the previous behavior of
1112 * displaying the shard's parent FID, on some changelog records related
1113 * to striped directories, so use that for compatibility if needed
1115 if (mdd->mdd_cl.mc_enable_shard_pfid) {
1116 *pfid = *mdd_object_fid(parent);
1120 if (!fid_is_zero(&parent->mod_striped_pfid)) {
1121 *pfid = parent->mod_striped_pfid;
1125 /* is the parent dir striped? */
1126 rc = mdo_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LMV);
1127 if (rc == -ENODATA) {
1128 *pfid = *mdd_object_fid(parent);
1129 parent->mod_striped_pfid = *pfid;
1136 LASSERT(!mdd_is_root(mdo2mdd(&parent->mod_obj),
1137 mdd_object_fid(parent)));
1139 /* hide shard FID */
1140 rc = mdd_parent_fid(env, parent, pattr, pfid);
1142 parent->mod_striped_pfid = *pfid;
1147 /** Store a namespace change changelog record
1148 * If this fails, we must fail the whole transaction; we don't
1149 * want the change to commit without the log entry.
1150 * \param target - mdd_object of change
1151 * \param parent - target parent object
1152 * \param pattr - target parent attribute
1153 * \param sfid - source object fid
1154 * \param sparent - source parent object
1155 * \param spattr - source parent attribute
1156 * \param tname - target name string
1157 * \param sname - source name string
1158 * \param handle - transaction handle
1160 int mdd_changelog_ns_store(const struct lu_env *env,
1161 struct mdd_device *mdd,
1162 enum changelog_rec_type type,
1163 enum changelog_rec_flags clf_flags,
1164 struct mdd_object *target,
1165 struct mdd_object *parent,
1166 const struct lu_attr *pattr,
1167 const struct lu_fid *sfid,
1168 struct mdd_object *sparent,
1169 const struct lu_attr *spattr,
1170 const struct lu_name *tname,
1171 const struct lu_name *sname,
1172 struct thandle *handle)
1174 const struct lu_ucred *uc = lu_ucred(env);
1175 struct llog_changelog_rec *rec;
1178 __u64 xflags = CLFE_INVALID;
1182 if (!mdd_changelog_enabled(env, mdd, type))
1185 LASSERT(S_ISDIR(mdd_object_type(parent)));
1186 LASSERT(tname != NULL);
1187 LASSERT(handle != NULL);
1189 reclen = mdd_llog_record_calc_size(env, tname, sname);
1190 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
1191 if (buf->lb_buf == NULL)
1195 clf_flags &= CLF_FLAGMASK;
1196 clf_flags |= CLF_EXTRA_FLAGS;
1199 if (uc->uc_jobid[0] != '\0')
1200 clf_flags |= CLF_JOBID;
1201 xflags |= CLFE_UIDGID;
1206 clf_flags |= CLF_RENAME;
1208 clf_flags |= CLF_VERSION;
1210 rec->cr.cr_flags = clf_flags;
1212 if (clf_flags & CLF_EXTRA_FLAGS) {
1213 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1214 if (xflags & CLFE_UIDGID)
1215 mdd_changelog_rec_extra_uidgid(&rec->cr,
1216 uc->uc_uid, uc->uc_gid);
1217 if (xflags & CLFE_NID)
1218 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1221 rec->cr.cr_type = (__u32)type;
1223 rc = mdd_changelog_ns_pfid_set(env, mdd, parent, pattr,
1228 rec->cr.cr_namelen = tname->ln_namelen;
1229 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1231 if (clf_flags & CLF_RENAME) {
1232 struct lu_fid spfid;
1234 rc = mdd_changelog_ns_pfid_set(env, mdd, sparent, spattr,
1239 mdd_changelog_rec_ext_rename(&rec->cr, sfid, &spfid, sname);
1242 if (clf_flags & CLF_JOBID)
1243 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1245 if (likely(target != NULL)) {
1246 rec->cr.cr_tfid = *mdd_object_fid(target);
1247 target->mod_cltime = ktime_get();
1249 fid_zero(&rec->cr.cr_tfid);
1252 rc = mdd_changelog_store(env, mdd, rec, handle);
1254 CERROR("%s: cannot store changelog record: type = %d, "
1255 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1256 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1257 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1264 static int __mdd_links_add(const struct lu_env *env,
1265 struct mdd_object *mdd_obj,
1266 struct linkea_data *ldata,
1267 const struct lu_name *lname,
1268 const struct lu_fid *pfid,
1269 int first, int check)
1271 /* cattr is set in mdd_link */
1272 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1275 if (ldata->ld_leh == NULL) {
1276 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1280 rc = linkea_data_new(ldata,
1281 &mdd_env_info(env)->mdi_link_buf);
1288 rc = linkea_links_find(ldata, lname, pfid);
1289 if (rc && rc != -ENOENT)
1295 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1296 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1300 linkea_add_buf(ldata, lname, tfid, false);
1303 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1304 linkea_add_buf(ldata, lname, pfid, false);
1306 /* For encrypted file, we want to limit number of hard links to what
1307 * linkEA can contain. So ask to return error in case of overflow.
1308 * Currently linkEA stores 4KiB of links, that is 14 NAME_MAX links,
1309 * or 119 16-byte names.
1311 return linkea_add_buf(ldata, lname, pfid,
1312 cattr->la_valid & LA_FLAGS &&
1313 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1316 static int __mdd_links_del(const struct lu_env *env,
1317 struct mdd_object *mdd_obj,
1318 struct linkea_data *ldata,
1319 const struct lu_name *lname,
1320 const struct lu_fid *pfid)
1322 /* cattr is set in mdd_link */
1323 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1326 if (ldata->ld_leh == NULL) {
1327 rc = mdd_links_read(env, mdd_obj, ldata);
1332 rc = linkea_links_find(ldata, lname, pfid);
1336 linkea_del_buf(ldata, lname,
1337 cattr->la_valid & LA_FLAGS &&
1338 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1342 static int mdd_linkea_prepare(const struct lu_env *env,
1343 struct mdd_object *mdd_obj,
1344 const struct lu_fid *oldpfid,
1345 const struct lu_name *oldlname,
1346 const struct lu_fid *newpfid,
1347 const struct lu_name *newlname,
1348 int first, int check,
1349 struct linkea_data *ldata)
1354 if (CFS_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1357 LASSERT(oldpfid != NULL || newpfid != NULL);
1359 if (mdd_obj->mod_flags & DEAD_OBJ)
1360 /* Unnecessary to update linkEA for dead object. */
1363 if (oldpfid != NULL) {
1364 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1366 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1369 /* No changes done. */
1374 /* If renaming, add the new record */
1375 if (newpfid != NULL)
1376 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1382 int mdd_links_rename(const struct lu_env *env,
1383 struct mdd_object *mdd_obj,
1384 const struct lu_fid *oldpfid,
1385 const struct lu_name *oldlname,
1386 const struct lu_fid *newpfid,
1387 const struct lu_name *newlname,
1388 struct thandle *handle,
1389 struct linkea_data *ldata,
1390 int first, int check)
1395 if (ldata == NULL) {
1396 ldata = &mdd_env_info(env)->mdi_link_data;
1397 memset(ldata, 0, sizeof(*ldata));
1398 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1399 newpfid, newlname, first, check, ldata);
1404 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1405 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1411 if (newlname == NULL)
1412 CERROR("link_ea add failed %d "DFID"\n",
1413 rc, PFID(mdd_object_fid(mdd_obj)));
1414 else if (oldpfid == NULL)
1415 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1416 newlname->ln_namelen, newlname->ln_name, rc,
1417 PFID(mdd_object_fid(mdd_obj)));
1418 else if (newpfid == NULL)
1419 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1420 oldlname->ln_namelen, oldlname->ln_name, rc,
1421 PFID(mdd_object_fid(mdd_obj)));
1423 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1424 "\n", oldlname->ln_namelen, oldlname->ln_name,
1425 newlname->ln_namelen, newlname->ln_name, rc,
1426 PFID(mdd_object_fid(mdd_obj)));
1429 if (is_vmalloc_addr(ldata->ld_buf))
1430 /* if we vmalloced a large buffer drop it */
1431 lu_buf_free(ldata->ld_buf);
1436 static inline int mdd_links_add(const struct lu_env *env,
1437 struct mdd_object *mdd_obj,
1438 const struct lu_fid *pfid,
1439 const struct lu_name *lname,
1440 struct thandle *handle,
1441 struct linkea_data *ldata, int first)
1443 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1444 pfid, lname, handle, ldata, first, 0);
1447 static inline int mdd_links_del(const struct lu_env *env,
1448 struct mdd_object *mdd_obj,
1449 const struct lu_fid *pfid,
1450 const struct lu_name *lname,
1451 struct thandle *handle)
1453 return mdd_links_rename(env, mdd_obj, pfid, lname,
1454 NULL, NULL, handle, NULL, 0, 0);
1457 /** Read the link EA into a temp buffer.
1458 * Uses the name_buf since it is generally large.
1459 * \retval IS_ERR err
1460 * \retval ptr to \a lu_buf (always \a mdi_link_buf)
1462 struct lu_buf *mdd_links_get(const struct lu_env *env,
1463 struct mdd_object *mdd_obj)
1465 struct linkea_data ldata = { NULL };
1468 rc = mdd_links_read(env, mdd_obj, &ldata);
1469 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1472 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1473 struct linkea_data *ldata, struct thandle *handle)
1475 const struct lu_buf *buf;
1478 if (ldata == NULL || ldata->ld_buf == NULL ||
1479 ldata->ld_leh == NULL)
1482 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1486 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1487 ldata->ld_leh->leh_len);
1488 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1489 if (unlikely(rc == -ENOSPC)) {
1490 rc = linkea_overflow_shrink(ldata);
1498 static int mdd_declare_links_add(const struct lu_env *env,
1499 struct mdd_object *mdd_obj,
1500 struct thandle *handle,
1501 struct linkea_data *ldata)
1507 if (ldata != NULL && ldata->ld_leh != NULL) {
1508 ea_len = ldata->ld_leh->leh_len;
1509 linkea = ldata->ld_buf->lb_buf;
1511 ea_len = MAX_LINKEA_SIZE;
1515 rc = mdo_declare_xattr_set(env, mdd_obj,
1516 mdd_buf_get_const(env, linkea, ea_len),
1517 XATTR_NAME_LINK, 0, handle);
1522 static inline int mdd_declare_links_del(const struct lu_env *env,
1523 struct mdd_object *c,
1524 struct thandle *handle)
1528 /* For directory, the linkEA will be removed together
1529 * with the object. */
1530 if (!S_ISDIR(mdd_object_type(c)))
1531 rc = mdd_declare_links_add(env, c, handle, NULL);
1536 static int mdd_declare_link(const struct lu_env *env,
1537 struct mdd_device *mdd,
1538 struct mdd_object *p,
1539 struct mdd_object *c,
1540 const struct lu_name *name,
1541 struct thandle *handle,
1543 struct linkea_data *data)
1545 struct lu_fid tfid = *mdd_object_fid(c);
1548 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1549 tfid.f_oid = cfs_fail_val;
1551 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1552 name->ln_name, handle);
1556 rc = mdo_declare_ref_add(env, c, handle);
1560 la->la_valid = LA_CTIME | LA_MTIME;
1561 rc = mdo_declare_attr_set(env, p, la, handle);
1565 la->la_valid = LA_CTIME;
1566 rc = mdo_declare_attr_set(env, c, la, handle);
1570 rc = mdd_declare_links_add(env, c, handle, data);
1574 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1580 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1581 struct md_object *src_obj, const struct lu_name *lname,
1584 const char *name = lname->ln_name;
1585 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1586 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1587 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1588 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1589 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1590 struct mdd_device *mdd = mdo2mdd(src_obj);
1591 struct thandle *handle;
1592 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1593 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
1597 rc = mdd_la_get(env, mdd_sobj, cattr);
1601 rc = mdd_la_get(env, mdd_tobj, tattr);
1606 * If we are using project inheritance, we only allow hard link
1607 * creation in our tree when the project IDs are the same;
1608 * otherwise the tree quota mechanism could be circumvented.
1610 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1611 (tattr->la_projid != cattr->la_projid))
1614 handle = mdd_trans_create(env, mdd);
1616 GOTO(out_pending, rc = PTR_ERR(handle));
1618 memset(ldata, 0, sizeof(*ldata));
1620 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1621 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1623 /* Note: even this function will change ldata, but it comes from
1624 * thread_info, which is completely temporary and only seen in
1625 * this function, so we do not need reset ldata once it fails.*/
1626 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1627 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1631 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1636 rc = mdd_trans_start(env, mdd, handle);
1640 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1641 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1644 GOTO(out_unlock, rc);
1646 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1647 rc = mdo_ref_add(env, mdd_sobj, handle);
1649 GOTO(out_unlock, rc);
1652 *tfid = *mdd_object_fid(mdd_sobj);
1653 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1654 tfid->f_oid = cfs_fail_val;
1656 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1657 mdd_object_type(mdd_sobj), name, handle);
1659 mdo_ref_del(env, mdd_sobj, handle);
1660 GOTO(out_unlock, rc);
1663 la->la_valid = LA_CTIME | LA_MTIME;
1664 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1666 GOTO(out_unlock, rc);
1668 la->la_valid = LA_CTIME;
1669 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1671 /* Note: The failure of links_add should not cause the
1672 * link failure, so do not check return value. */
1673 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1674 lname, handle, ldata, 0);
1678 mdd_write_unlock(env, mdd_sobj);
1680 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1681 mdd_tobj, tattr, NULL,
1682 NULL, NULL, lname, NULL, handle);
1684 rc = mdd_trans_stop(env, mdd, rc, handle);
1685 if (is_vmalloc_addr(ldata->ld_buf))
1686 /* if we vmalloced a large buffer drop it */
1687 lu_buf_free(ldata->ld_buf);
1692 static int mdd_mark_orphan_object(const struct lu_env *env,
1693 struct mdd_object *obj, struct thandle *handle,
1696 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1699 attr->la_valid = LA_FLAGS;
1700 attr->la_flags = LUSTRE_ORPHAN_FL;
1703 rc = mdo_declare_attr_set(env, obj, attr, handle);
1705 rc = mdo_attr_set(env, obj, attr, handle);
1710 static int mdd_declare_finish_unlink(const struct lu_env *env,
1711 struct mdd_object *obj,
1712 struct thandle *handle)
1716 /* Sigh, we do not know if the unlink object will become orphan in
1717 * declare phase, but fortunately the flags here does not matter
1718 * in current declare implementation */
1719 rc = mdd_mark_orphan_object(env, obj, handle, true);
1723 rc = mdo_declare_destroy(env, obj, handle);
1727 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1731 return mdd_declare_links_del(env, obj, handle);
1734 /* caller should take a lock before calling */
1735 int mdd_finish_unlink(const struct lu_env *env,
1736 struct mdd_object *obj, struct md_attr *ma,
1737 struct mdd_object *pobj,
1738 const struct lu_name *lname,
1742 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1745 LASSERT(mdd_write_locked(env, obj) != 0);
1747 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1748 /* add new orphan and the object
1749 * will be deleted during mdd_close() */
1750 obj->mod_flags |= DEAD_OBJ;
1751 if (obj->mod_count) {
1752 rc = mdd_orphan_insert(env, obj, th);
1754 CDEBUG(D_HA, "Object "DFID" is inserted into "
1755 "orphan list, open count = %d\n",
1756 PFID(mdd_object_fid(obj)),
1759 CERROR("Object "DFID" fail to be an orphan, "
1760 "open count = %d, maybe cause failed "
1762 PFID(mdd_object_fid(obj)),
1765 /* mark object as an orphan here, not
1766 * before mdd_orphan_insert() as racing
1767 * mdd_la_get() may propagate ORPHAN_OBJ
1768 * causing the asserition */
1769 rc = mdd_mark_orphan_object(env, obj, th, false);
1771 rc = mdo_destroy(env, obj, th);
1773 } else if (!is_dir) {
1774 /* old files may not have link ea; ignore errors */
1775 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
1783 * has mdd_write_lock on cobj already, but not on pobj yet
1785 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1786 const struct lu_attr *pattr,
1787 struct mdd_object *cobj,
1788 const struct lu_attr *cattr)
1793 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1798 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1799 struct mdd_object *p, struct mdd_object *c,
1800 const struct lu_name *name, struct md_attr *ma,
1801 struct thandle *handle, int no_name, int is_dir)
1803 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1806 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1807 if (likely(no_name == 0)) {
1808 rc = mdo_declare_index_delete(env, p, name->ln_name,
1815 rc = mdo_declare_ref_del(env, p, handle);
1821 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1822 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1823 la->la_valid = LA_CTIME | LA_MTIME;
1824 rc = mdo_declare_attr_set(env, p, la, handle);
1829 rc = mdo_declare_ref_del(env, c, handle);
1833 rc = mdo_declare_ref_del(env, c, handle);
1837 la->la_valid = LA_CTIME;
1838 rc = mdo_declare_attr_set(env, c, la, handle);
1842 rc = mdd_declare_finish_unlink(env, c, handle);
1846 /* FIXME: need changelog for remove entry */
1847 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1855 * test if a file has an HSM archive
1856 * if HSM attributes are not found in ma update them from
1859 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1860 struct mdd_object *obj,
1865 if (!(ma->ma_valid & MA_HSM)) {
1866 /* no HSM MD provided, read xattr */
1867 struct lu_buf *hsm_buf;
1868 const size_t buflen = sizeof(struct hsm_attrs);
1871 hsm_buf = mdd_buf_get(env, NULL, 0);
1872 lu_buf_alloc(hsm_buf, buflen);
1873 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1874 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1875 lu_buf_free(hsm_buf);
1879 ma->ma_valid |= MA_HSM;
1881 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1887 * Delete name entry and the object.
1888 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1889 * does not exist for this object, and it could only happen during resending
1890 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1891 * is also needed in this case(needed by changelog), so we have to add another
1892 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1893 * the ENOENT failure should be able to be fixed by redo mechanism.
1895 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1896 struct md_object *cobj, const struct lu_name *lname,
1897 struct md_attr *ma, int no_name)
1899 char *name = (char *)lname->ln_name;
1900 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1901 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1902 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1903 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1904 struct mdd_object *mdd_cobj = NULL;
1905 struct mdd_device *mdd = mdo2mdd(pobj);
1906 struct thandle *handle;
1907 int rc, is_dir = 0, cl_flags = 0;
1910 /* let shutdown to start */
1911 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1913 /* cobj == NULL means only delete name entry */
1914 if (likely(cobj != NULL)) {
1915 mdd_cobj = md2mdd_obj(cobj);
1916 if (mdd_object_exists(mdd_cobj) == 0)
1920 rc = mdd_la_get(env, mdd_pobj, pattr);
1924 if (likely(mdd_cobj != NULL)) {
1926 rc = mdd_la_get(env, mdd_cobj, cattr);
1930 is_dir = S_ISDIR(cattr->la_mode);
1931 /* search for an existing archive.
1932 * we should check ahead as the object
1933 * can be destroyed in this transaction */
1934 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1935 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1938 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1942 handle = mdd_trans_create(env, mdd);
1944 RETURN(PTR_ERR(handle));
1946 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1947 lname, ma, handle, no_name, is_dir);
1951 rc = mdd_trans_start(env, mdd, handle);
1955 if (likely(mdd_cobj != NULL))
1956 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
1958 if (lname->ln_name[lname->ln_namelen] != '\0') {
1959 /* lname->ln_name is not necessarily NUL terminated */
1960 name = kmalloc(lname->ln_namelen + 1, GFP_NOFS);
1962 GOTO(cleanup, rc = -ENOMEM);
1964 memcpy(name, lname->ln_name, lname->ln_namelen);
1965 name[lname->ln_namelen] = '\0';
1968 if (likely(no_name == 0) && !CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1969 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1974 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1975 CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1976 GOTO(cleanup, rc = 0);
1978 if (likely(mdd_cobj != NULL)) {
1979 rc = mdo_ref_del(env, mdd_cobj, handle);
1981 __mdd_index_insert_only(env, mdd_pobj,
1982 mdd_object_fid(mdd_cobj),
1983 mdd_object_type(mdd_cobj),
1990 mdo_ref_del(env, mdd_cobj, handle);
1992 /* fetch updated nlink */
1993 rc = mdd_la_get(env, mdd_cobj, cattr);
1998 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1999 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2001 la->la_valid = LA_CTIME | LA_MTIME;
2002 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2006 /* Enough for only unlink the entry */
2007 if (unlikely(mdd_cobj == NULL))
2010 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
2011 /* update ctime of an unlinked file only if it is still
2012 * opened or a link still exists */
2013 la->la_valid = LA_CTIME;
2014 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
2019 /* XXX: this transfer to ma will be removed with LOD/OSP */
2020 ma->ma_attr = *cattr;
2021 ma->ma_valid |= MA_INODE;
2022 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
2026 /* fetch updated nlink */
2027 rc = mdd_la_get(env, mdd_cobj, cattr);
2028 /* if object is removed then we can't get its attrs,
2030 if (rc == -ENOENT) {
2031 cattr->la_nlink = 0;
2035 if (cattr->la_nlink == 0) {
2036 ma->ma_attr = *cattr;
2037 ma->ma_valid |= MA_INODE;
2042 if (name != lname->ln_name)
2045 if (likely(mdd_cobj != NULL))
2046 mdd_write_unlock(env, mdd_cobj);
2049 if (cattr->la_nlink == 0)
2050 cl_flags |= CLF_UNLINK_LAST;
2052 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
2054 rc = mdd_changelog_ns_store(env, mdd,
2055 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
2056 mdd_cobj, mdd_pobj, pattr, NULL,
2057 NULL, NULL, lname, NULL, handle);
2061 rc = mdd_trans_stop(env, mdd, rc, handle);
2067 * The permission has been checked when obj created, no need check again.
2069 static int mdd_cd_sanity_check(const struct lu_env *env,
2070 struct mdd_object *obj)
2075 if (!obj || mdd_is_dead_obj(obj))
2081 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
2082 struct md_object *cobj,
2083 const struct md_op_spec *spec, struct md_attr *ma)
2085 struct mdd_device *mdd = mdo2mdd(cobj);
2086 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2087 struct mdd_object *son = md2mdd_obj(cobj);
2088 struct thandle *handle;
2089 const struct lu_buf *buf;
2090 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
2091 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2095 rc = mdd_cd_sanity_check(env, son);
2099 if (!md_should_create(spec->sp_cr_flags))
2103 * there are following use cases for this function:
2104 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
2105 * striping can be specified or not
2108 rc = mdd_la_get(env, son, attr);
2112 /* calling ->ah_make_hint() is used to transfer information from parent */
2113 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2115 handle = mdd_trans_create(env, mdd);
2117 GOTO(out_free, rc = PTR_ERR(handle));
2120 * XXX: Setting the lov ea is not locked but setting the attr is locked?
2121 * Should this be fixed?
2123 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
2124 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
2125 spec->sp_cr_flags, spec->no_create);
2127 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2128 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2129 spec->u.sp_ea.eadatalen);
2134 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
2135 XATTR_NAME_LOV, 0, handle);
2139 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2144 rc = mdd_trans_start(env, mdd, handle);
2148 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
2154 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
2158 rc = mdd_trans_stop(env, mdd, rc, handle);
2164 static int mdd_declare_object_initialize(const struct lu_env *env,
2165 struct mdd_object *parent,
2166 struct mdd_object *child,
2167 const struct lu_attr *attr,
2168 struct thandle *handle)
2173 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
2174 if (!S_ISDIR(attr->la_mode))
2177 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
2178 S_IFDIR, dot, handle);
2182 rc = mdo_declare_ref_add(env, child, handle);
2186 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
2187 S_IFDIR, dotdot, handle);
2192 static int mdd_object_initialize(const struct lu_env *env,
2193 const struct lu_fid *pfid,
2194 struct mdd_object *child,
2195 struct lu_attr *attr,
2196 struct thandle *handle)
2201 if (S_ISDIR(attr->la_mode)) {
2202 /* Add "." and ".." for newly created dir */
2203 mdo_ref_add(env, child, handle);
2204 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
2205 S_IFDIR, dot, handle);
2207 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2210 mdo_ref_del(env, child, handle);
2217 * This function checks whether it can create a file/dir under the
2218 * directory(@pobj). The directory(@pobj) is not being locked by
2221 * \param[in] env execution environment
2222 * \param[in] pobj the directory to create files
2223 * \param[in] pattr the attributes of the directory
2224 * \param[in] lname the name of the created file/dir
2225 * \param[in] cattr the attributes of the file/dir
2226 * \param[in] spec create specification
2228 * \retval = 0 it is allowed to create file/dir under
2230 * \retval negative error not allowed to create file/dir
2231 * under the directory
2233 static int mdd_create_sanity_check(const struct lu_env *env,
2234 struct md_object *pobj,
2235 const struct lu_attr *pattr,
2236 const struct lu_name *lname,
2237 struct lu_attr *cattr,
2238 struct md_op_spec *spec)
2240 struct mdd_thread_info *info = mdd_env_info(env);
2241 struct lu_fid *fid = &info->mdi_fid;
2242 struct mdd_object *obj = md2mdd_obj(pobj);
2243 struct mdd_device *m = mdo2mdd(pobj);
2244 bool check_perm = true;
2249 if (mdd_is_dead_obj(obj))
2253 * In some cases this lookup is not needed - we know before if name
2254 * exists or not because MDT performs lookup for it.
2255 * name length check is done in lookup.
2257 if (spec->sp_cr_lookup) {
2259 * Check if the name already exist, though it will be checked in
2260 * _index_insert also, for avoiding rolling back if exists
2263 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2264 MAY_WRITE | MAY_EXEC);
2266 RETURN(rc ? : -EEXIST);
2268 /* Permission is already being checked in mdd_lookup */
2272 if (S_ISDIR(cattr->la_mode) &&
2273 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2274 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2275 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2277 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2278 !(spec->sp_replay &&
2279 lum->lum_magic == cpu_to_le32(LMV_MAGIC_V1))) {
2281 CERROR("%s: invalid lmv_user_md: magic=%x hash=%x stripe_offset=%d stripe_count=%u: rc = %d\n",
2282 mdd2obd_dev(m)->obd_name,
2283 le32_to_cpu(lum->lum_magic),
2284 le32_to_cpu(lum->lum_hash_type),
2285 (int)le32_to_cpu(lum->lum_stripe_offset),
2286 le32_to_cpu(lum->lum_stripe_count), rc);
2291 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2296 if (pattr->la_mode & S_ISGID) {
2297 struct lu_ucred *uc = lu_ucred(env);
2299 cattr->la_gid = pattr->la_gid;
2301 /* Directories are special, and always inherit S_ISGID */
2302 if (S_ISDIR(cattr->la_mode)) {
2303 cattr->la_mode |= S_ISGID;
2304 cattr->la_valid |= LA_MODE;
2305 } else if ((cattr->la_mode & (S_ISGID | S_IXGRP))
2306 == (S_ISGID | S_IXGRP) &&
2307 !lustre_in_group_p(uc,
2308 (cattr->la_valid & LA_GID) ?
2309 cattr->la_gid : pattr->la_gid) &&
2310 !cap_raised(uc->uc_cap, CAP_FSETID)) {
2311 cattr->la_mode &= ~S_ISGID;
2312 cattr->la_valid |= LA_MODE;
2316 /* Inherit project ID from parent directory */
2317 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2318 cattr->la_projid = pattr->la_projid;
2319 if (S_ISDIR(cattr->la_mode)) {
2320 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2321 cattr->la_valid |= LA_FLAGS;
2323 cattr->la_valid |= LA_PROJID;
2326 rc = mdd_name_check(env, m, lname);
2330 switch (cattr->la_mode & S_IFMT) {
2332 unsigned int symlen = spec->u.sp_symname.ln_namelen + 1;
2334 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2335 RETURN(-ENAMETOOLONG);
2354 static int mdd_declare_create_object(const struct lu_env *env,
2355 struct mdd_device *mdd,
2356 struct mdd_object *p, struct mdd_object *c,
2357 struct lu_attr *attr,
2358 struct thandle *handle,
2359 const struct md_op_spec *spec,
2360 struct lu_buf *def_acl_buf,
2361 struct lu_buf *acl_buf,
2362 struct lu_buf *hsm_buf,
2363 struct dt_allocation_hint *hint)
2365 const struct lu_buf *buf;
2368 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2369 /* ldiskfs OSD needs this information for credit allocation */
2371 hint->dah_acl_len = def_acl_buf->lb_len;
2373 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2378 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2379 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2380 /* if dir, then can inherit default ACl */
2381 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2382 XATTR_NAME_ACL_DEFAULT,
2388 if (acl_buf && acl_buf->lb_len > 0) {
2389 rc = mdo_declare_attr_set(env, c, attr, handle);
2393 rc = mdo_declare_xattr_set(env, c, acl_buf,
2394 XATTR_NAME_ACL_ACCESS, 0, handle);
2399 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2403 /* replay case, create LOV EA from client data */
2404 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2405 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2406 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2407 spec->u.sp_ea.eadatalen);
2408 rc = mdo_declare_xattr_set(env, c, buf,
2409 S_ISDIR(attr->la_mode) ?
2410 XATTR_NAME_LMV : XATTR_NAME_LOV,
2411 LU_XATTR_CREATE, handle);
2415 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2416 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2424 if (S_ISLNK(attr->la_mode)) {
2425 const char *target_name = spec->u.sp_symname.ln_name;
2426 int sym_len = spec->u.sp_symname.ln_namelen;
2427 const struct lu_buf *buf;
2429 buf = mdd_buf_get_const(env, target_name, sym_len);
2430 rc = dt_declare_record_write(env, mdd_object_child(c),
2436 if (spec->sp_cr_file_secctx_name != NULL) {
2437 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2438 spec->sp_cr_file_secctx_size);
2439 rc = mdo_declare_xattr_set(env, c, buf,
2440 spec->sp_cr_file_secctx_name, 0,
2446 if (spec->sp_cr_file_encctx != NULL) {
2447 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2448 spec->sp_cr_file_encctx_size);
2449 rc = mdo_declare_xattr_set(env, c, buf,
2450 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2459 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2460 struct mdd_object *p, struct mdd_object *c,
2461 const struct lu_name *name,
2462 struct lu_attr *attr,
2463 struct thandle *handle,
2464 const struct md_op_spec *spec,
2465 struct linkea_data *ldata,
2466 struct lu_buf *def_acl_buf,
2467 struct lu_buf *acl_buf,
2468 struct lu_buf *hsm_buf,
2469 struct dt_allocation_hint *hint)
2473 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2474 def_acl_buf, acl_buf, hsm_buf, hint);
2478 if (S_ISDIR(attr->la_mode)) {
2479 rc = mdo_declare_ref_add(env, p, handle);
2484 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2485 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2489 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2490 enum changelog_rec_type type;
2492 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2493 attr->la_mode, name->ln_name,
2498 rc = mdd_declare_links_add(env, c, handle, ldata);
2503 la->la_valid = LA_CTIME | LA_MTIME;
2504 rc = mdo_declare_attr_set(env, p, la, handle);
2508 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2509 S_ISREG(attr->la_mode) ? CL_CREATE :
2510 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2512 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2521 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2522 struct lu_attr *la, struct lu_buf *def_acl_buf,
2523 struct lu_buf *acl_buf)
2529 if (S_ISLNK(la->la_mode)) {
2530 acl_buf->lb_len = 0;
2531 def_acl_buf->lb_len = 0;
2535 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2536 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2537 XATTR_NAME_ACL_DEFAULT);
2538 mdd_read_unlock(env, pobj);
2540 /* ACL buffer size is not enough, need realloc */
2541 if (rc > acl_buf->lb_len)
2544 /* If there are default ACL, fix mode/ACL by default ACL */
2545 def_acl_buf->lb_len = rc;
2546 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2547 acl_buf->lb_len = rc;
2548 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2551 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2552 /* If there are no default ACL, fix mode by mask */
2553 struct lu_ucred *uc = lu_ucred(env);
2555 /* The create triggered by MDT internal events, such as
2556 * LFSCK reset, will not contain valid "uc". */
2557 if (unlikely(uc != NULL))
2558 la->la_mode &= ~uc->uc_umask;
2560 acl_buf->lb_len = 0;
2561 def_acl_buf->lb_len = 0;
2568 * Create a metadata object and initialize it, set acl, xattr.
2570 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2571 struct mdd_object *son, struct lu_attr *attr,
2572 struct md_op_spec *spec, struct lu_buf *acl_buf,
2573 struct lu_buf *def_acl_buf,
2574 struct lu_buf *hsm_buf,
2575 struct dt_allocation_hint *hint,
2576 struct thandle *handle, bool initial_create)
2578 const struct lu_fid *son_fid = mdd_object_fid(son);
2579 const struct lu_ucred *uc = lu_ucred(env);
2580 const char *jobid = uc->uc_jobid;
2581 const struct lu_buf *buf;
2585 mdd_write_lock(env, son, DT_TGT_CHILD);
2586 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2591 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2592 * created in declare phase, they also needs to be added to master
2593 * object as sub-directory entry. So it has to initialize the master
2594 * object, then set dir striped EA.(in mdo_xattr_set) */
2595 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2598 GOTO(err_destroy, rc);
2601 * in case of replay we just set LOVEA provided by the client
2602 * XXX: I think it would be interesting to try "old" way where
2603 * MDT calls this xattr_set(LOV) in a different transaction.
2604 * probably this way we code can be made better.
2607 /* During creation, there are only a few cases we need do xattr_set to
2609 * 1. regular file: see comments above.
2610 * 2. dir: inherit default striping or pool settings from parent.
2611 * 3. create striped directory with provided stripeEA.
2612 * 4. create striped directory because inherit default layout from the
2615 if (spec->no_create ||
2616 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2617 S_ISDIR(attr->la_mode)) {
2618 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2619 spec->u.sp_ea.eadatalen);
2620 rc = mdo_xattr_set(env, son, buf,
2621 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2623 LU_XATTR_CREATE, handle);
2625 GOTO(err_destroy, rc);
2628 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2631 memset(&mh, 0, sizeof(mh));
2632 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2633 mh.mh_arch_id = spec->sp_archive_id;
2634 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2635 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2638 GOTO(err_destroy, rc);
2641 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2642 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2643 S_ISDIR(attr->la_mode)) {
2644 /* set default acl */
2645 rc = mdo_xattr_set(env, son, def_acl_buf,
2646 XATTR_NAME_ACL_DEFAULT, 0,
2649 GOTO(err_destroy, rc);
2651 /* set its own acl */
2652 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2653 rc = mdo_xattr_set(env, son, acl_buf,
2654 XATTR_NAME_ACL_ACCESS,
2657 GOTO(err_destroy, rc);
2661 if (S_ISLNK(attr->la_mode)) {
2662 struct dt_object *dt = mdd_object_child(son);
2663 const char *target_name = spec->u.sp_symname.ln_name;
2664 int sym_len = spec->u.sp_symname.ln_namelen;
2667 buf = mdd_buf_get_const(env, target_name, sym_len);
2668 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2672 GOTO(err_initlized, rc = -EFAULT);
2675 if (initial_create && spec->sp_cr_file_secctx_name != NULL) {
2676 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2677 spec->sp_cr_file_secctx_size);
2678 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2681 GOTO(err_initlized, rc);
2684 if (spec->sp_cr_file_encctx != NULL) {
2685 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2686 spec->sp_cr_file_encctx_size);
2687 rc = mdo_xattr_set(env, son, buf,
2688 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2691 GOTO(err_initlized, rc);
2694 if (initial_create &&
2695 spec->sp_cr_job_xattr[0] != '\0' &&
2697 (S_ISREG(attr->la_mode) || S_ISDIR(attr->la_mode))) {
2698 jobid_len = strnlen(jobid, LUSTRE_JOBID_SIZE);
2699 buf = mdd_buf_get_const(env, jobid, jobid_len);
2701 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_job_xattr, 0,
2703 /* this xattr is nonessential, so ignore errors. */
2706 DFID" failed to set xattr '%s': rc = %d\n",
2707 PFID(son_fid), spec->sp_cr_job_xattr, rc);
2713 if (unlikely(rc != 0)) {
2715 if (S_ISDIR(attr->la_mode)) {
2716 /* Drop the reference, no need to delete "."/"..",
2717 * because the object to be destroyed directly. */
2718 rc2 = mdo_ref_del(env, son, handle);
2722 rc2 = mdo_ref_del(env, son, handle);
2726 mdo_destroy(env, son, handle);
2729 mdd_write_unlock(env, son);
2733 static int mdd_index_delete(const struct lu_env *env,
2734 struct mdd_object *mdd_pobj,
2735 struct lu_attr *cattr,
2736 const struct lu_name *lname)
2738 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2739 struct thandle *handle;
2743 handle = mdd_trans_create(env, mdd);
2745 RETURN(PTR_ERR(handle));
2747 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2752 if (S_ISDIR(cattr->la_mode)) {
2753 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2758 /* Since this will only be used in the error handler path,
2759 * Let's set the thandle to be local and not mess the transno */
2760 handle->th_local = 1;
2761 rc = mdd_trans_start(env, mdd, handle);
2765 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2766 S_ISDIR(cattr->la_mode), handle);
2770 rc = mdd_trans_stop(env, mdd, rc, handle);
2776 * Create object and insert it into namespace.
2778 * Two operations have to be performed:
2780 * - an allocation of a new object (->do_create()), and
2781 * - an insertion into a parent index (->dio_insert()).
2783 * Due to locking, operation order is not important, when both are
2784 * successful, *but* error handling cases are quite different:
2786 * - if insertion is done first, and following object creation fails,
2787 * insertion has to be rolled back, but this operation might fail
2788 * also leaving us with dangling index entry.
2790 * - if creation is done first, is has to be undone if insertion fails,
2791 * leaving us with leaked space, which is not good but not fatal.
2793 * It seems that creation-first is simplest solution, but it is sub-optimal
2799 * case, because second mkdir is bound to create object, only to
2800 * destroy it immediately.
2802 * To avoid this follow local file systems that do double lookup:
2804 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2805 * 1. create (mdd_create_object_internal())
2806 * 2. insert (__mdd_index_insert(), lookup again)
2808 * \param[in] pobj parent object
2809 * \param[in] lname name of child being created
2810 * \param[in,out] child child object being created
2811 * \param[in] spec additional create parameters
2812 * \param[in] ma attributes for new child object
2814 * \retval 0 on success
2815 * \retval negative errno on failure
2817 int mdd_create(const struct lu_env *env, struct md_object *pobj,
2818 const struct lu_name *lname, struct md_object *child,
2819 struct md_op_spec *spec, struct md_attr *ma)
2821 struct mdd_thread_info *info = mdd_env_info(env);
2822 struct lu_attr *la = &info->mdi_la_for_fix;
2823 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2824 struct mdd_object *son = md2mdd_obj(child);
2825 struct mdd_device *mdd = mdo2mdd(pobj);
2826 struct lu_attr *attr = &ma->ma_attr;
2827 struct thandle *handle;
2828 struct lu_attr *pattr = &info->mdi_pattr;
2829 struct lu_buf acl_buf;
2830 struct lu_buf def_acl_buf;
2831 struct lu_buf hsm_buf;
2832 struct linkea_data *ldata = &info->mdi_link_data;
2833 const char *name = lname->ln_name;
2834 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2835 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
2836 bool name_inserted = false;
2841 rc = mdd_la_get(env, mdd_pobj, pattr);
2845 /* Sanity checks before big job. */
2846 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2847 if (unlikely(rc == -EEXIST && S_ISDIR(attr->la_mode) &&
2848 spec->sp_replay && mdd_object_remote(mdd_pobj)))
2849 /* if it's replay by client request, and name is found in
2850 * parent directory on remote MDT, it means mkdir was partially
2851 * executed: name was successfully added, but target not.
2853 name_inserted = true;
2857 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2858 GOTO(out_free, rc = -EINPROGRESS);
2860 handle = mdd_trans_create(env, mdd);
2862 GOTO(out_free, rc = PTR_ERR(handle));
2865 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
2866 if (!acl_buf.lb_buf)
2867 GOTO(out_stop, rc = -ENOMEM);
2869 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
2870 if (!def_acl_buf.lb_buf)
2871 GOTO(out_stop, rc = -ENOMEM);
2873 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2874 if (unlikely(rc == -ERANGE &&
2875 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
2876 /* use maximum-sized xattr buffer for too-big default ACL */
2877 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
2879 goto use_bigger_buffer;
2884 /* adjust stripe count to 0 for 'lfs mkdir -c 1 ...' to avoid creating
2885 * 1-stripe directory, MDS_OPEN_DEFAULT_LMV means ea is default LMV.
2887 if (unlikely(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata &&
2888 !(spec->sp_cr_flags & MDS_OPEN_DEFAULT_LMV))) {
2889 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
2891 /* migrate may create 1-stripe directory, adjust stripe count
2892 * before lod_ah_init().
2894 if (lmu && lmu->lum_magic == cpu_to_le32(LMV_USER_MAGIC) &&
2895 lmu->lum_stripe_count == cpu_to_le32(1))
2896 lmu->lum_stripe_count = 0;
2899 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2901 memset(ldata, 0, sizeof(*ldata));
2902 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2903 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2906 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2907 &tfid, lname, 1, 0, ldata);
2909 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2910 mdd_object_fid(mdd_pobj),
2911 lname, 1, 0, ldata);
2914 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2915 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
2917 memset(&hsm_buf, 0, sizeof(hsm_buf));
2918 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
2919 if (hsm_buf.lb_buf == NULL)
2920 GOTO(out_stop, rc = -ENOMEM);
2923 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2924 handle, spec, ldata, &def_acl_buf, &acl_buf,
2929 rc = mdd_trans_start(env, mdd, handle);
2933 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2934 &def_acl_buf, &hsm_buf, hint, handle, true);
2938 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2939 mdd_write_lock(env, son, DT_TGT_CHILD);
2940 son->mod_flags |= VOLATILE_OBJ;
2941 rc = mdd_orphan_insert(env, son, handle);
2942 GOTO(out_volatile, rc);
2944 if (likely(!name_inserted)) {
2945 rc = __mdd_index_insert(env, mdd_pobj,
2946 mdd_object_fid(son),
2947 attr->la_mode, name, handle);
2949 GOTO(err_created, rc);
2952 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
2955 /* update parent directory mtime/ctime */
2957 la->la_valid = LA_CTIME | LA_MTIME;
2958 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2960 GOTO(err_insert, rc);
2966 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2967 rc2 = mdd_orphan_delete(env, son, handle);
2969 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2970 S_ISDIR(attr->la_mode),
2976 mdd_write_lock(env, son, DT_TGT_CHILD);
2977 if (S_ISDIR(attr->la_mode)) {
2978 /* Drop the reference, no need to delete "."/"..",
2979 * because the object is to be destroyed directly. */
2980 rc2 = mdo_ref_del(env, son, handle);
2982 mdd_write_unlock(env, son);
2987 /* For volatile files drop one link immediately, since there is
2988 * no filename in the namespace, and save any error returned. */
2989 rc2 = mdo_ref_del(env, son, handle);
2991 mdd_write_unlock(env, son);
2992 if (unlikely(rc == 0))
2997 /* Don't destroy the volatile object on success */
2998 if (likely(rc != 0))
2999 mdo_destroy(env, son, handle);
3000 mdd_write_unlock(env, son);
3003 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
3004 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
3005 rc = mdd_changelog_ns_store(env, mdd,
3006 S_ISDIR(attr->la_mode) ? CL_MKDIR :
3007 S_ISREG(attr->la_mode) ? CL_CREATE :
3008 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
3009 0, son, mdd_pobj, pattr, NULL, NULL, NULL,
3010 lname, NULL, handle);
3012 rc2 = mdd_trans_stop(env, mdd, rc, handle);
3014 /* If creation fails, it is most likely due to the remote update
3015 * failure, because local transaction will mostly succeed at
3016 * this stage. There is no easy way to rollback all of previous
3017 * updates, so let's remove the object from namespace, and
3018 * LFSCK should handle the orphan object. */
3019 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
3020 mdd_index_delete(env, mdd_pobj, attr, lname);
3024 if (is_vmalloc_addr(ldata->ld_buf))
3025 /* if we vmalloced a large buffer drop it */
3026 lu_buf_free(ldata->ld_buf);
3028 if (spec->sp_cr_flags & MDS_OPEN_PCC)
3029 lu_buf_free(&hsm_buf);
3031 /* The child object shouldn't be cached anymore */
3033 set_bit(LU_OBJECT_HEARD_BANSHEE,
3034 &child->mo_lu.lo_header->loh_flags);
3038 /* has not mdd_write{read}_lock on any obj yet. */
3039 static int mdd_rename_sanity_check(const struct lu_env *env,
3040 struct mdd_object *src_pobj,
3041 const struct lu_attr *spattr,
3042 struct mdd_object *tgt_pobj,
3043 const struct lu_attr *tpattr,
3044 struct mdd_object *sobj,
3045 const struct lu_attr *sattr,
3046 struct mdd_object *tobj,
3047 const struct lu_attr *tattr)
3052 /* XXX: when get here, sobj must NOT be NULL,
3053 * the other case has been processed in cld_rename
3054 * before mdd_rename and enable MDS_PERM_BYPASS. */
3058 * If we are using project inheritance, we only allow renames
3059 * into our tree when the project IDs are the same; otherwise
3060 * tree quota mechanism would be circumvented.
3062 if ((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
3063 tpattr->la_projid != sattr->la_projid && S_ISDIR(sattr->la_mode))
3066 /* we prevent an encrypted file from being renamed
3067 * into an unencrypted dir
3069 if ((spattr->la_valid & LA_FLAGS &&
3070 spattr->la_flags & LUSTRE_ENCRYPT_FL) &&
3071 !(tpattr->la_valid & LA_FLAGS &&
3072 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
3075 rc = mdd_may_delete(env, src_pobj, spattr, sobj, sattr, NULL, 1, 0);
3079 /* XXX: when get here, "tobj == NULL" means tobj must
3080 * NOT exist (neither on remote MDS, such case has been
3081 * processed in cld_rename before mdd_rename and enable
3083 * So check may_create, but not check may_unlink. */
3085 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
3086 (src_pobj != tgt_pobj));
3088 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, sattr,
3089 (src_pobj != tgt_pobj), 1);
3091 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(sattr->la_mode))
3092 rc = __mdd_may_link(env, tgt_pobj, tpattr);
3098 int mdd_declare_rename(const struct lu_env *env, struct mdd_device *mdd,
3099 struct mdd_object *mdd_spobj, struct mdd_object *mdd_tpobj,
3100 struct mdd_object *mdd_sobj, struct mdd_object *mdd_tobj,
3101 const struct lu_name *sname, const struct lu_name *tname,
3102 struct md_attr *ma, struct linkea_data *ldata,
3103 bool change_projid, struct thandle *handle)
3105 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3108 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3109 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3115 /* name from source dir */
3116 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
3120 /* .. from source child */
3121 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3122 /* source child can be directory, count by source dir's nlink */
3123 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
3126 if (mdd_spobj != mdd_tpobj) {
3127 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
3132 rc = mdo_declare_index_insert(env, mdd_sobj,
3133 mdd_object_fid(mdd_tpobj),
3134 S_IFDIR, dotdot, handle);
3139 /* new target child can be directory,
3140 * counted by target dir's nlink */
3141 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
3146 la->la_valid = LA_CTIME | LA_MTIME;
3147 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
3151 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
3155 la->la_valid = LA_CTIME;
3157 la->la_valid |= LA_PROJID;
3158 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3162 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
3167 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
3168 mdd_object_type(mdd_sobj),
3169 tname->ln_name, handle);
3173 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
3174 /* delete target child in target parent directory */
3175 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
3180 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3184 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
3185 /* target child can be directory,
3186 * delete "." reference in target child directory
3188 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3192 /* delete ".." reference in target parent directory */
3193 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
3198 la->la_valid = LA_CTIME;
3199 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
3203 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
3208 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
3217 int mdd_migrate_object(const struct lu_env *env, struct mdd_object *spobj,
3218 struct mdd_object *tpobj, struct mdd_object *sobj,
3219 struct mdd_object *tobj, const struct lu_name *sname,
3220 const struct lu_name *tname, struct md_op_spec *spec,
3221 struct md_attr *ma);
3223 /* src object can be remote that is why we use only fid and type of object */
3224 static int mdd_rename(const struct lu_env *env, struct md_object *src_pobj,
3225 struct md_object *tgt_pobj, const struct lu_fid *lf,
3226 const struct lu_name *lsname, struct md_object *tobj,
3227 const struct lu_name *ltname, struct md_attr *ma)
3229 const char *sname = lsname->ln_name;
3230 const char *tname = ltname->ln_name;
3231 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3232 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3233 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj); /* target parent */
3234 struct mdd_object *mdd_sobj = NULL; /* source object */
3235 struct mdd_object *mdd_tobj = NULL; /* (possible) target object */
3236 struct lu_attr *sattr = MDD_ENV_VAR(env, cattr);
3237 struct lu_attr *spattr = MDD_ENV_VAR(env, pattr);
3238 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3239 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3240 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3241 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3242 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3243 struct mdd_device *mdd = mdo2mdd(src_pobj);
3244 struct thandle *handle;
3247 bool tobj_locked = 0;
3248 bool change_projid = false;
3249 unsigned cl_flags = 0;
3253 /* let unlink to complete and commit */
3254 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3257 mdd_tobj = md2mdd_obj(tobj);
3259 mdd_sobj = mdd_object_find(env, mdd, lf);
3260 if (IS_ERR(mdd_sobj))
3261 RETURN(PTR_ERR(mdd_sobj));
3263 rc = mdd_la_get(env, mdd_sobj, sattr);
3265 GOTO(out_pending, rc);
3267 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3268 * hard links, and target doesn't exist.
3270 if (mdd_object_remote(mdd_sobj) && S_ISLNK(sattr->la_mode) &&
3271 sattr->la_nlink == 1 && !tobj) {
3272 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3273 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3276 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3279 GOTO(out_pending, rc);
3281 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3282 if (IS_ERR(mdd_tobj))
3283 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3285 memset(spec, 0, sizeof(*spec));
3286 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3287 mdd_tobj, lsname, ltname, spec, ma);
3288 mdd_object_put(env, mdd_tobj);
3289 GOTO(out_pending, rc);
3292 rc = mdd_la_get(env, mdd_spobj, spattr);
3294 GOTO(out_pending, rc);
3297 rc = mdd_la_get(env, mdd_tobj, tattr);
3299 GOTO(out_pending, rc);
3300 /* search for an existing archive.
3301 * we should check ahead as the object
3302 * can be destroyed in this transaction */
3303 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3304 cl_flags |= CLF_RENAME_LAST_EXISTS;
3307 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3309 GOTO(out_pending, rc);
3311 rc = mdd_rename_sanity_check(env, mdd_spobj, spattr, mdd_tpobj, tpattr,
3312 mdd_sobj, sattr, mdd_tobj, tattr);
3314 GOTO(out_pending, rc);
3316 rc = mdd_name_check(env, mdd, ltname);
3318 GOTO(out_pending, rc);
3320 handle = mdd_trans_create(env, mdd);
3322 GOTO(out_pending, rc = PTR_ERR(handle));
3324 memset(ldata, 0, sizeof(*ldata));
3325 rc = mdd_linkea_prepare(env, mdd_sobj, spobj_fid, lsname, tpobj_fid,
3326 ltname, 1, 0, ldata);
3330 if (tpattr->la_projid != sattr->la_projid &&
3331 tpattr->la_flags & LUSTRE_PROJINHERIT_FL)
3332 change_projid = true;
3334 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3335 mdd_tobj, lsname, ltname, ma, ldata,
3336 change_projid, handle);
3340 rc = mdd_trans_start(env, mdd, handle);
3344 is_dir = S_ISDIR(sattr->la_mode);
3346 /* Remove source name from source directory */
3347 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3351 /* "mv dir1 dir2" needs "dir1/.." link update */
3352 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3353 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3355 GOTO(fixup_spobj2, rc);
3357 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3360 GOTO(fixup_spobj, rc);
3363 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3364 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3366 /* tname might been renamed to something else */
3367 GOTO(fixup_spobj, rc);
3370 /* Insert new fid with target name into target dir */
3371 rc = __mdd_index_insert(env, mdd_tpobj, lf, sattr->la_mode,
3374 GOTO(fixup_tpobj, rc);
3376 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3377 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3379 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3380 la->la_valid = LA_CTIME;
3381 if (change_projid) {
3382 /* mdd_update_time honors other valid flags except TIME ones */
3383 la->la_valid |= LA_PROJID;
3384 la->la_projid = tpattr->la_projid;
3386 rc = mdd_update_time(env, mdd_sobj, sattr, la, handle);
3388 GOTO(fixup_tpobj, rc);
3390 /* Update the linkEA for the source object */
3391 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3392 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3393 lsname, mdd_object_fid(mdd_tpobj), ltname,
3394 handle, ldata, 0, 0);
3396 /* Old files might not have EA entry */
3397 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3398 lsname, handle, NULL, 0);
3399 mdd_write_unlock(env, mdd_sobj);
3400 /* We don't fail the transaction if the link ea can't be
3401 updated -- fid2path will use alternate lookup method. */
3404 /* Remove old target object
3405 * For tobj is remote case cmm layer has processed
3406 * and set tobj to NULL then. So when tobj is NOT NULL,
3407 * it must be local one.
3409 if (tobj && mdd_object_exists(mdd_tobj)) {
3410 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3412 if (mdd_is_dead_obj(mdd_tobj)) {
3413 /* should not be dead, something is wrong */
3415 CERROR("%s: something bad, dead tobj "DFID": rc = %d\n",
3416 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3419 mdo_ref_del(env, mdd_tobj, handle);
3421 /* Remove dot reference. */
3422 if (S_ISDIR(tattr->la_mode))
3423 mdo_ref_del(env, mdd_tobj, handle);
3426 /* fetch updated nlink */
3427 rc = mdd_la_get(env, mdd_tobj, tattr);
3429 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3430 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3431 GOTO(fixup_tpobj, rc);
3434 la->la_valid = LA_CTIME;
3435 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3437 CERROR("%s: failed set ctime of tobj "DFID": rc = %d\n",
3438 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3439 GOTO(fixup_tpobj, rc);
3442 /* XXX: this transfer to ma will be removed with LOD/OSP */
3443 ma->ma_attr = *tattr;
3444 ma->ma_valid |= MA_INODE;
3445 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3448 CERROR("%s: failed to unlink tobj "DFID": rc = %d\n",
3449 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3450 GOTO(fixup_tpobj, rc);
3453 /* fetch updated nlink */
3454 rc = mdd_la_get(env, mdd_tobj, tattr);
3455 if (rc == -ENOENT) {
3456 /* the object got removed, let's
3457 * return the latest known attributes */
3458 tattr->la_nlink = 0;
3461 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3462 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3463 GOTO(fixup_tpobj, rc);
3465 /* XXX: this transfer to ma will be removed with LOD/OSP */
3466 ma->ma_attr = *tattr;
3467 ma->ma_valid |= MA_INODE;
3469 if (tattr->la_nlink == 0)
3470 cl_flags |= CLF_RENAME_LAST;
3472 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3475 la->la_valid = LA_CTIME | LA_MTIME;
3476 rc = mdd_update_time(env, mdd_spobj, spattr, la, handle);
3478 GOTO(fixup_tpobj, rc);
3480 if (mdd_spobj != mdd_tpobj) {
3481 la->la_valid = LA_CTIME | LA_MTIME;
3482 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3484 GOTO(fixup_tpobj, rc);
3491 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3493 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3494 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc2);
3496 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3497 !mdd_is_dead_obj(mdd_tobj)) {
3499 mdo_ref_add(env, mdd_tobj, handle);
3501 mdo_ref_add(env, mdd_tobj, handle);
3504 rc2 = __mdd_index_insert(env, mdd_tpobj, tpobj_fid,
3505 mdd_object_type(mdd_tobj),
3508 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3509 mdd2obd_dev(mdd)->obd_name,
3510 PFID(tpobj_fid), rc2);
3515 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3516 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3518 CWARN("%s: spobj "DFID" dotdot delete error: rc = %d\n",
3519 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3522 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3525 CWARN("%s: spobj "DFID" dotdot insert error: rc = %d\n",
3526 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3531 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3532 mdd_object_type(mdd_sobj), sname,
3535 CWARN("%s: spobj "DFID" fix error: rc = %d\n",
3536 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3541 mdd_write_unlock(env, mdd_tobj);
3544 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3545 mdd_tobj, mdd_tpobj, tpattr, lf,
3546 mdd_spobj, spattr, ltname, lsname,
3550 rc = mdd_trans_stop(env, mdd, rc, handle);
3553 mdd_object_put(env, mdd_sobj);
3559 * Check whether we should migrate the file/dir
3561 * < 0 permission check failed or other error.
3562 * = 0 the file can be migrated.
3564 static int mdd_migrate_sanity_check(const struct lu_env *env,
3565 struct mdd_device *mdd,
3566 struct mdd_object *spobj,
3567 struct mdd_object *tpobj,
3568 struct mdd_object *sobj,
3569 struct mdd_object *tobj,
3570 const struct lu_attr *spattr,
3571 const struct lu_attr *tpattr,
3572 const struct lu_attr *attr)
3578 if (!mdd_object_remote(sobj)) {
3579 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3580 if (sobj->mod_count > 0) {
3581 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3582 mdd_obj_dev_name(sobj),
3583 PFID(mdd_object_fid(sobj)),
3585 mdd_read_unlock(env, sobj);
3588 mdd_read_unlock(env, sobj);
3591 if (mdd_object_exists(tobj))
3594 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3598 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3603 struct mdd_xattr_entry {
3604 struct list_head mxe_linkage;
3606 struct lu_buf mxe_buf;
3610 struct lu_buf mx_namebuf;
3611 struct list_head mx_list;
3614 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3616 INIT_LIST_HEAD(&xattrs->mx_list);
3617 xattrs->mx_namebuf.lb_buf = NULL;
3618 xattrs->mx_namebuf.lb_len = 0;
3621 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3623 struct mdd_xattr_entry *entry;
3624 struct mdd_xattr_entry *tmp;
3626 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3627 lu_buf_free(&entry->mxe_buf);
3628 list_del(&entry->mxe_linkage);
3629 OBD_FREE_PTR(entry);
3632 lu_buf_free(&xattrs->mx_namebuf);
3635 /* read xattrs into buf, but skip LMA, LMV, LINKEA if 'skip_linkea' is
3636 * set, and DMV if 'skip_dmv" is set.
3638 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3639 struct mdd_xattrs *xattrs,
3640 struct mdd_object *sobj,
3644 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3645 struct mdd_xattr_entry *entry;
3646 bool needencxattr = false;
3647 bool encxattrfound = false;
3657 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3658 if (list_xsize == -ENODATA)
3664 if (attr->la_valid & LA_FLAGS &&
3665 attr->la_flags & LUSTRE_ENCRYPT_FL) {
3666 needencxattr = true;
3668 strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3671 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3672 if (xattrs->mx_namebuf.lb_buf == NULL)
3675 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3681 xname = xattrs->mx_namebuf.lb_buf;
3683 for (; rem > 0; xname += xlen, rem -= xlen) {
3685 strcmp(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3686 encxattrfound = true;
3687 xlen = strnlen(xname, rem - 1) + 1;
3688 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3689 strcmp(XATTR_NAME_LMV, xname) == 0)
3693 strcmp(XATTR_NAME_LINK, xname) == 0)
3697 strcmp(XATTR_NAME_DEFAULT_LMV, xname) == 0)
3700 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3701 if (xsize == -ENODATA)
3704 GOTO(fini, rc = xsize);
3706 OBD_ALLOC_PTR(entry);
3708 GOTO(fini, rc = -ENOMEM);
3710 lu_buf_alloc(&entry->mxe_buf, xsize);
3711 if (!entry->mxe_buf.lb_buf) {
3712 OBD_FREE_PTR(entry);
3713 GOTO(fini, rc = -ENOMEM);
3716 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3718 lu_buf_free(&entry->mxe_buf);
3719 OBD_FREE_PTR(entry);
3725 entry->mxe_name = xname;
3726 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3729 if (needencxattr && !encxattrfound) {
3730 xlen = strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3731 strncpy(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT, xlen);
3738 mdd_xattrs_fini(xattrs);
3742 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3743 struct mdd_object *obj,
3744 const struct lu_buf *buf,
3746 int fl, struct thandle *handle);
3748 static int mdd_foreach_xattr(const struct lu_env *env,
3749 struct mdd_object *tobj,
3750 struct mdd_xattrs *xattrs,
3751 struct thandle *handle,
3754 struct mdd_xattr_entry *entry;
3757 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
3758 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
3766 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3767 struct mdd_object *sobj,
3768 struct mdd_object *tobj,
3769 const struct lu_name *sname,
3770 const struct lu_fid *sfid,
3771 const struct lu_name *lname,
3772 const struct lu_fid *fid,
3774 struct thandle *handle);
3776 static int mdd_declare_update_link(const struct lu_env *env,
3777 struct mdd_object *sobj,
3778 struct mdd_object *tobj,
3779 const struct lu_name *tname,
3780 const struct lu_fid *tpfid,
3781 const struct lu_name *lname,
3782 const struct lu_fid *fid,
3784 struct thandle *handle)
3786 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3787 struct mdd_object *pobj;
3791 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3792 !strcmp(tname->ln_name, lname->ln_name))
3795 pobj = mdd_object_find(env, mdd, fid);
3797 return PTR_ERR(pobj);
3800 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3802 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3803 mdd_object_type(sobj),
3804 lname->ln_name, handle);
3805 mdd_object_put(env, pobj);
3809 rc = mdo_declare_ref_add(env, tobj, handle);
3813 rc = mdo_declare_ref_del(env, sobj, handle);
3817 static int mdd_update_link(const struct lu_env *env,
3818 struct mdd_object *sobj,
3819 struct mdd_object *tobj,
3820 const struct lu_name *tname,
3821 const struct lu_fid *tpfid,
3822 const struct lu_name *lname,
3823 const struct lu_fid *fid,
3825 struct thandle *handle)
3827 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3828 struct mdd_object *pobj;
3834 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3835 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3838 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3839 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3841 pobj = mdd_object_find(env, mdd, fid);
3843 CWARN("%s: cannot find obj "DFID": %ld\n",
3844 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3845 RETURN(PTR_ERR(pobj));
3848 if (!mdd_object_exists(pobj)) {
3849 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3850 mdd_object_put(env, pobj);
3854 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3855 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3857 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3858 mdd_object_type(sobj),
3859 lname->ln_name, handle);
3860 mdd_write_unlock(env, pobj);
3861 mdd_object_put(env, pobj);
3865 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3866 rc = mdo_ref_add(env, tobj, handle);
3867 mdd_write_unlock(env, tobj);
3871 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3872 rc = mdo_ref_del(env, sobj, handle);
3873 mdd_write_unlock(env, sobj);
3878 static inline int mdd_fld_lookup(const struct lu_env *env,
3879 struct mdd_device *mdd,
3880 const struct lu_fid *fid,
3883 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3884 struct seq_server_site *ss;
3887 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3889 range->lsr_flags = LU_SEQ_RANGE_MDT;
3890 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3894 *mdt_index = range->lsr_index;
3899 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3900 struct mdd_object *sobj,
3901 struct mdd_object *tobj,
3902 const struct lu_name *tname,
3903 const struct lu_fid *tpfid,
3904 const struct lu_name *lname,
3905 const struct lu_fid *fid,
3907 struct thandle *handle)
3909 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3910 __u32 source_mdt_index = *(__u32 *)opaque;
3911 __u32 link_mdt_index;
3917 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3918 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3921 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3925 RETURN(link_mdt_index == source_mdt_index);
3928 static int mdd_iterate_linkea(const struct lu_env *env,
3929 struct mdd_object *sobj,
3930 struct mdd_object *tobj,
3931 const struct lu_name *tname,
3932 const struct lu_fid *tpfid,
3933 struct linkea_data *ldata,
3935 struct thandle *handle,
3938 struct mdd_thread_info *info = mdd_env_info(env);
3939 char *filename = info->mdi_name;
3940 struct lu_name lname;
3947 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3948 linkea_next_entry(ldata)) {
3949 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3952 /* Note: lname might miss \0 at the end */
3953 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3954 lname.ln_namelen, lname.ln_name);
3955 lname.ln_name = filename;
3957 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3959 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3967 * Prepare linkea, and check whether file needs migrate: if source still has
3968 * link on source MDT, no need to migrate, just update namespace on source and
3971 * \retval 0 do migrate
3972 * \retval 1 don't migrate
3973 * \retval -errno on failure
3975 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3976 struct mdd_device *mdd,
3977 struct mdd_object *spobj,
3978 struct mdd_object *tpobj,
3979 struct mdd_object *sobj,
3980 const struct lu_name *sname,
3981 const struct lu_name *tname,
3982 const struct lu_attr *attr,
3983 struct linkea_data *ldata)
3985 __u32 source_mdt_index;
3990 memset(ldata, 0, sizeof(*ldata));
3991 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
3992 mdd_object_fid(tpobj), tname, 1, 0, ldata);
3997 * Then it will check if the file should be migrated. If the file has
3998 * mulitple links, we only need migrate the file if all of its entries
3999 * has been migrated to the remote MDT.
4001 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
4004 /* If there are still links locally, don't migrate this file */
4005 LASSERT(ldata->ld_leh != NULL);
4008 * If linkEA is overflow, it means there are some unknown name entries
4009 * under unknown parents, which will prevent the migration.
4011 if (unlikely(ldata->ld_leh->leh_overflow_time))
4014 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
4018 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
4019 ldata, &source_mdt_index, NULL,
4020 mdd_is_link_on_source_mdt);
4024 static int mdd_declare_migrate_update(const struct lu_env *env,
4025 struct mdd_object *spobj,
4026 struct mdd_object *tpobj,
4027 struct mdd_object *obj,
4028 const struct lu_name *sname,
4029 const struct lu_name *tname,
4030 struct lu_attr *attr,
4031 struct lu_attr *spattr,
4032 struct lu_attr *tpattr,
4033 struct linkea_data *ldata,
4035 struct thandle *handle)
4037 struct mdd_thread_info *info = mdd_env_info(env);
4038 struct lu_attr *la = &info->mdi_la_for_fix;
4041 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
4045 if (S_ISDIR(attr->la_mode)) {
4046 rc = mdo_declare_ref_del(env, spobj, handle);
4051 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
4052 attr->la_mode & S_IFMT,
4053 tname->ln_name, handle);
4057 rc = mdd_declare_links_add(env, obj, handle, ldata);
4061 if (S_ISDIR(attr->la_mode)) {
4062 rc = mdo_declare_ref_add(env, tpobj, handle);
4067 la->la_valid = LA_CTIME | LA_MTIME;
4068 rc = mdo_declare_attr_set(env, spobj, la, handle);
4072 if (tpobj != spobj) {
4073 rc = mdo_declare_attr_set(env, tpobj, la, handle);
4081 static int mdd_declare_migrate_create(const struct lu_env *env,
4082 struct mdd_object *spobj,
4083 struct mdd_object *tpobj,
4084 struct mdd_object *sobj,
4085 struct mdd_object *tobj,
4086 const struct lu_name *sname,
4087 const struct lu_name *tname,
4088 struct lu_attr *spattr,
4089 struct lu_attr *tpattr,
4090 struct lu_attr *attr,
4091 struct lu_buf *sbuf,
4092 struct linkea_data *ldata,
4093 struct mdd_xattrs *xattrs,
4095 struct md_op_spec *spec,
4096 struct dt_allocation_hint *hint,
4097 struct thandle *handle)
4099 struct mdd_thread_info *info = mdd_env_info(env);
4100 struct md_layout_change *mlc = &info->mdi_mlc;
4101 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
4106 if (S_ISDIR(attr->la_mode)) {
4107 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
4109 mlc->mlc_opc = MD_LAYOUT_DETACH;
4110 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
4114 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4115 } else if (S_ISLNK(attr->la_mode)) {
4116 spec->u.sp_symname.ln_name = sbuf->lb_buf;
4117 /* don't count NUL */
4118 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
4119 } else if (S_ISREG(attr->la_mode)) {
4120 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
4121 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
4124 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
4126 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
4127 tname, attr, handle, spec, ldata, NULL, NULL,
4133 * tobj mode will be used in mdo_declare_layout_change(), but it's not
4134 * createb yet, copy from sobj.
4136 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
4137 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
4138 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
4140 if (S_ISDIR(attr->la_mode)) {
4142 /* if sobj is not striped, fake a 1-stripe LMV */
4143 LASSERT(sizeof(info->mdi_key) >
4144 lmv_mds_md_size(1, LMV_MAGIC_V1));
4145 lmv = (typeof(lmv))info->mdi_key;
4146 memset(lmv, 0, sizeof(*lmv));
4147 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4148 lmv->lmv_stripe_count = cpu_to_le32(1);
4149 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4150 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
4151 mdd_object_fid(sobj));
4152 mlc->mlc_buf.lb_buf = lmv;
4153 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4155 mlc->mlc_buf = *sbuf;
4157 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4158 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4163 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4164 mdo_declare_xattr_set);
4168 if (S_ISREG(attr->la_mode)) {
4169 struct lu_buf fid_buf;
4171 handle->th_complex = 1;
4173 /* target may be remote, update PFID via sobj. */
4174 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4175 fid_buf.lb_len = sizeof(struct lu_fid);
4176 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
4181 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4186 if (!S_ISDIR(attr->la_mode)) {
4187 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4188 mdd_object_fid(tpobj), ldata, NULL,
4189 handle, mdd_declare_update_link);
4194 if (!S_ISDIR(attr->la_mode) || lmv) {
4195 rc = mdo_declare_ref_del(env, sobj, handle);
4199 if (S_ISDIR(attr->la_mode)) {
4200 rc = mdo_declare_ref_del(env, sobj, handle);
4205 rc = mdo_declare_destroy(env, sobj, handle);
4210 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
4211 attr, spattr, tpattr, ldata, ma,
4217 * migrate dirent from \a spobj to \a tpobj.
4219 static int mdd_migrate_update(const struct lu_env *env,
4220 struct mdd_object *spobj,
4221 struct mdd_object *tpobj,
4222 struct mdd_object *obj,
4223 const struct lu_name *sname,
4224 const struct lu_name *tname,
4225 struct lu_attr *attr,
4226 struct lu_attr *spattr,
4227 struct lu_attr *tpattr,
4228 struct linkea_data *ldata,
4230 struct thandle *handle)
4232 struct mdd_thread_info *info = mdd_env_info(env);
4233 struct lu_attr *la = &info->mdi_la_for_fix;
4238 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
4239 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
4240 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
4242 rc = __mdd_index_delete(env, spobj, sname->ln_name,
4243 S_ISDIR(attr->la_mode), handle);
4247 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
4248 attr->la_mode & S_IFMT,
4249 tname->ln_name, handle);
4253 rc = mdd_links_write(env, obj, ldata, handle);
4257 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4258 la->la_valid = LA_CTIME | LA_MTIME;
4259 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4260 rc = mdd_update_time(env, spobj, spattr, la, handle);
4261 mdd_write_unlock(env, spobj);
4265 if (tpobj != spobj) {
4266 la->la_valid = LA_CTIME | LA_MTIME;
4267 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4268 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4269 mdd_write_unlock(env, tpobj);
4278 * Migrate file/dir to target MDT.
4280 * Create target according to \a spec, and then migrate xattrs, if it's
4281 * directory, migrate source stripes to target.
4283 * \param[in] env execution environment
4284 * \param[in] spobj source parent object
4285 * \param[in] tpobj target parent object
4286 * \param[in] sobj source object
4287 * \param[in] tobj target object
4288 * \param[in] lname file name
4289 * \param[in] spattr source parent attributes
4290 * \param[in] tpattr target parent attributes
4291 * \param[in] attr source attributes
4292 * \param[in] sbuf source LMV buf
4293 * \param[in] spec migrate create spec
4294 * \param[in] hint target creation hint
4295 * \param[in] handle tranasction handle
4297 * \retval 0 on success
4298 * \retval -errno on failure
4300 static int mdd_migrate_create(const struct lu_env *env,
4301 struct mdd_object *spobj,
4302 struct mdd_object *tpobj,
4303 struct mdd_object *sobj,
4304 struct mdd_object *tobj,
4305 const struct lu_name *sname,
4306 const struct lu_name *tname,
4307 struct lu_attr *spattr,
4308 struct lu_attr *tpattr,
4309 struct lu_attr *attr,
4310 const struct lu_buf *sbuf,
4311 struct linkea_data *ldata,
4312 struct mdd_xattrs *xattrs,
4314 struct md_op_spec *spec,
4315 struct dt_allocation_hint *hint,
4316 struct thandle *handle)
4323 * migrate sobj stripes to tobj if it's directory:
4324 * 1. detach stripes from sobj.
4325 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4326 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4328 if (S_ISDIR(attr->la_mode)) {
4329 struct mdd_thread_info *info = mdd_env_info(env);
4330 struct md_layout_change *mlc = &info->mdi_mlc;
4332 mlc->mlc_opc = MD_LAYOUT_DETACH;
4334 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4335 rc = mdo_layout_change(env, sobj, mlc, handle);
4336 mdd_write_unlock(env, sobj);
4341 /* don't set nlink from sobj */
4342 attr->la_valid &= ~LA_NLINK;
4344 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4345 hint, handle, false);
4349 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4350 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4351 mdd_write_unlock(env, tobj);
4355 /* for regular file, update OST objects XATTR_NAME_FID */
4356 if (S_ISREG(attr->la_mode)) {
4357 struct lu_buf fid_buf;
4359 /* target may be remote, update PFID via sobj. */
4360 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4361 fid_buf.lb_len = sizeof(struct lu_fid);
4362 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4367 /* delete LOV to avoid deleting OST objs when destroying sobj */
4368 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4369 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4370 mdd_write_unlock(env, sobj);
4371 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4372 if (rc && rc != -ENODATA)
4377 /* update links FID */
4378 if (!S_ISDIR(attr->la_mode)) {
4379 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4380 mdd_object_fid(tpobj), ldata,
4381 NULL, handle, mdd_update_link);
4386 /* don't destroy sobj if it's plain directory */
4387 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4388 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4389 rc = mdo_ref_del(env, sobj, handle);
4391 if (S_ISDIR(attr->la_mode))
4392 rc = mdo_ref_del(env, sobj, handle);
4394 rc = mdo_destroy(env, sobj, handle);
4396 mdd_write_unlock(env, sobj);
4401 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4402 spattr, tpattr, ldata, ma, handle);
4407 /* NB: if user issued different migrate command, we can't adjust it silently
4408 * here, because this command will decide target MDT in subdir migration in
4411 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4412 const struct lmv_mds_md_v1 *lmv,
4413 const struct lmv_user_md_v1 *lum,
4414 const struct lu_name *lname)
4416 __u32 lum_stripe_count = lum->lum_stripe_count;
4417 __u32 lum_hash_type = lum->lum_hash_type &
4418 cpu_to_le32(LMV_HASH_TYPE_MASK);
4419 __u32 lmv_hash_type = lmv->lmv_hash_type &
4420 cpu_to_le32(LMV_HASH_TYPE_MASK);
4422 if (!lmv_is_sane(lmv))
4425 /* if stripe_count unspecified, set to 1 */
4426 if (!lum_stripe_count)
4427 lum_stripe_count = cpu_to_le32(1);
4429 /* TODO: check specific MDTs */
4430 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4431 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4432 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4433 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4434 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4435 le32_to_cpu(lmv->lmv_master_mdt_index),
4436 le32_to_cpu(lmv->lmv_migrate_offset),
4437 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4446 * Internal function to migrate directory or file between MDTs.
4448 * migrate source to target in following steps:
4449 * 1. create target, append source stripes after target's if it's directory,
4450 * migrate xattrs and update fid of source links.
4451 * 2. update namespace: migrate dirent from source parent to target parent,
4452 * update file linkea, and destroy source if it's not needed any more.
4454 * \param[in] env execution environment
4455 * \param[in] spobj source parent object
4456 * \param[in] tpobj target parent object
4457 * \param[in] sobj source object
4458 * \param[in] tobj target object
4459 * \param[in] sname source file name
4460 * \param[in] tname target file name
4461 * \param[in] spec target creation spec
4462 * \param[in] ma used to update \a pobj mtime and ctime
4464 * \retval 0 on success
4465 * \retval -errno on failure
4467 static int mdd_migrate_object(const struct lu_env *env,
4468 struct mdd_object *spobj,
4469 struct mdd_object *tpobj,
4470 struct mdd_object *sobj,
4471 struct mdd_object *tobj,
4472 const struct lu_name *sname,
4473 const struct lu_name *tname,
4474 struct md_op_spec *spec,
4477 struct mdd_thread_info *info = mdd_env_info(env);
4478 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4479 struct lu_attr *spattr = &info->mdi_pattr;
4480 struct lu_attr *tpattr = &info->mdi_tpattr;
4481 struct lu_attr *attr = &info->mdi_cattr;
4482 struct linkea_data *ldata = &info->mdi_link_data;
4483 struct dt_allocation_hint *hint = &info->mdi_hint;
4484 struct lu_buf sbuf = { NULL };
4485 struct mdd_xattrs xattrs;
4486 struct lmv_mds_md_v1 *lmv;
4487 struct thandle *handle;
4493 CDEBUG(D_INFO, "migrate %s from "DFID"/"DFID" to "DFID"/"DFID"\n",
4494 sname->ln_name, PFID(mdd_object_fid(spobj)),
4495 PFID(mdd_object_fid(sobj)), PFID(mdd_object_fid(tpobj)),
4496 PFID(mdd_object_fid(tobj)));
4499 rc = mdd_la_get(env, sobj, attr);
4503 rc = mdd_la_get(env, spobj, spattr);
4507 rc = mdd_la_get(env, tpobj, tpattr);
4511 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4512 spattr, tpattr, attr);
4516 mdd_xattrs_init(&xattrs);
4518 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4519 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4523 /* if user use default value '0' for stripe_count, we need to
4524 * adjust it to '1' to create a 1-stripe directory.
4526 if (lum->lum_stripe_count == 0)
4527 lum->lum_stripe_count = cpu_to_le32(1);
4529 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4530 if (rc && rc != -ENODATA)
4535 if (!lmv_is_sane(lmv))
4536 GOTO(out, rc = -EBADF);
4537 if (lmv_is_migrating(lmv)) {
4538 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4543 } else if (!S_ISDIR(attr->la_mode)) {
4545 GOTO(out, rc = -EALREADY);
4547 /* update namespace only if @sobj is on MDT where @tpobj is. */
4548 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4549 spec->sp_migrate_nsonly = true;
4551 if (S_ISLNK(attr->la_mode)) {
4552 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4554 GOTO(out, rc = -ENOMEM);
4556 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4559 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4560 mdd2obd_dev(mdd)->obd_name,
4561 PFID(mdd_object_fid(sobj)), rc);
4567 /* linkea needs update upon FID or parent stripe change */
4568 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4569 tname, attr, ldata);
4571 /* update namespace only if @sobj has link on its MDT. */
4572 spec->sp_migrate_nsonly = true;
4576 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4577 * RPCs inside transaction.
4579 if (!spec->sp_migrate_nsonly) {
4580 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, true, true);
4585 handle = mdd_trans_create(env, mdd);
4587 GOTO(out, rc = PTR_ERR(handle));
4589 if (spec->sp_migrate_nsonly)
4590 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4591 tname, attr, spattr, tpattr,
4594 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4595 sname, tname, spattr, tpattr,
4596 attr, &sbuf, ldata, &xattrs, ma,
4597 spec, hint, handle);
4601 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4606 rc = mdd_trans_start(env, mdd, handle);
4610 if (spec->sp_migrate_nsonly)
4611 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4612 attr, spattr, tpattr, ldata, ma,
4615 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4616 tname, spattr, tpattr, attr, &sbuf,
4617 ldata, &xattrs, ma, spec, hint, handle);
4621 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4622 spec->sp_migrate_nsonly ? sobj : tobj,
4623 spobj, spattr, mdd_object_fid(sobj),
4624 tpobj, tpattr, tname, sname,
4628 rc = mdd_trans_stop(env, mdd, rc, handle);
4630 mdd_xattrs_fini(&xattrs);
4634 * -EAGAIN means transaction execution phase detect the layout
4635 * has been changed by others.
4637 if (rc == -EAGAIN && retried++ < MAX_TRANS_RETRIED)
4638 GOTO(retry, retried);
4644 * Migrate directory or file between MDTs.
4646 * \param[in] env execution environment
4647 * \param[in] md_spobj source parent object
4648 * \param[in] md_tpobj target parent object
4649 * \param[in] md_sobj source object
4650 * \param[in] lname file name
4651 * \param[in] md_tobj target object
4652 * \param[in] spec target creation spec
4653 * \param[in] ma used to update \a pobj mtime and ctime
4655 * \retval 0 on success
4656 * \retval -errno on failure
4658 static int mdd_migrate(const struct lu_env *env, struct md_object *md_spobj,
4659 struct md_object *md_tpobj, struct md_object *md_sobj,
4660 struct md_object *md_tobj, const struct lu_name *lname,
4661 struct md_op_spec *spec, struct md_attr *ma)
4663 return mdd_migrate_object(env, md2mdd_obj(md_spobj),
4664 md2mdd_obj(md_tpobj), md2mdd_obj(md_sobj),
4665 md2mdd_obj(md_tobj), lname, lname, spec, ma);
4668 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4669 struct mdd_object *pobj,
4670 struct mdd_object *obj,
4671 struct mdd_object *stripe,
4672 struct lu_attr *attr,
4673 struct mdd_xattrs *xattrs,
4674 struct md_layout_change *mlc,
4675 struct lu_name *lname,
4676 struct thandle *handle)
4680 mlc->mlc_opc = MD_LAYOUT_DETACH;
4681 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4685 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4686 S_IFDIR, dotdot, handle);
4690 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
4691 mdo_declare_xattr_set);
4695 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4699 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4703 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4707 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4708 attr->la_mode, lname->ln_name, handle);
4712 rc = mdo_declare_ref_del(env, obj, handle);
4716 rc = mdo_declare_ref_del(env, obj, handle);
4720 rc = mdo_declare_destroy(env, obj, handle);
4727 /* transform one-stripe directory to a plain directory */
4728 static int mdd_1sd_collapse(const struct lu_env *env,
4729 struct mdd_object *pobj,
4730 struct mdd_object *obj,
4731 struct mdd_object *stripe,
4732 struct lu_attr *attr,
4733 struct mdd_xattrs *xattrs,
4734 struct md_layout_change *mlc,
4735 struct lu_name *lname,
4736 struct thandle *handle)
4742 /* replace 1-stripe directory with its stripe */
4743 mlc->mlc_opc = MD_LAYOUT_DETACH;
4745 mdd_write_lock(env, obj, DT_SRC_PARENT);
4746 rc = mdo_layout_change(env, obj, mlc, handle);
4747 mdd_write_unlock(env, obj);
4751 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4752 mdd_write_lock(env, obj, DT_SRC_CHILD);
4754 /* insert dotdot to stripe which points to parent */
4755 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4756 S_IFDIR, dotdot, handle);
4760 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
4765 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4769 /* don't set nlink from parent */
4770 attr->la_valid &= ~LA_NLINK;
4772 rc = mdo_attr_set(env, stripe, attr, handle);
4776 /* delete dir name from parent */
4777 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4781 /* insert stripe to parent with dir name */
4782 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4783 attr->la_mode, lname->ln_name, handle);
4787 /* destroy dir obj */
4788 rc = mdo_ref_del(env, obj, handle);
4792 rc = mdo_ref_del(env, obj, handle);
4796 rc = mdo_destroy(env, obj, handle);
4802 mdd_write_unlock(env, obj);
4803 mdd_write_unlock(env, pobj);
4809 * shrink directory stripes after migration/merge
4811 int mdd_dir_layout_shrink(const struct lu_env *env,
4812 struct md_object *md_obj,
4813 struct md_layout_change *mlc)
4815 struct mdd_device *mdd = mdo2mdd(md_obj);
4816 struct mdd_thread_info *info = mdd_env_info(env);
4817 struct mdd_object *obj = md2mdd_obj(md_obj);
4818 struct mdd_object *pobj = NULL;
4819 struct mdd_object *stripe = NULL;
4820 struct lu_attr *attr = &info->mdi_pattr;
4821 struct lu_fid *fid = &info->mdi_fid2;
4822 struct lu_name lname = { NULL };
4823 struct lu_buf lmv_buf = { NULL };
4824 struct mdd_xattrs xattrs;
4825 struct lmv_mds_md_v1 *lmv;
4826 struct lmv_user_md *lmu;
4827 struct thandle *handle;
4832 rc = mdd_la_get(env, obj, attr);
4836 if (!S_ISDIR(attr->la_mode))
4839 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4843 lmv = lmv_buf.lb_buf;
4844 if (!lmv_is_sane(lmv))
4847 lmu = mlc->mlc_buf.lb_buf;
4849 /* adjust the default value '0' to '1' */
4850 if (lmu->lum_stripe_count == 0)
4851 lmu->lum_stripe_count = cpu_to_le32(1);
4853 /* these were checked in MDT */
4854 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4855 le32_to_cpu(lmv->lmv_stripe_count));
4856 LASSERT(!lmv_is_splitting(lmv));
4857 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4859 mdd_xattrs_init(&xattrs);
4861 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4862 * to a plain dir, which will cause FID change and namespace update.
4864 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4865 struct linkea_data *ldata = &info->mdi_link_data;
4866 char *filename = info->mdi_name;
4868 rc = mdd_links_read(env, obj, ldata);
4872 if (ldata->ld_leh->leh_reccount > 1)
4873 GOTO(out, rc = -EINVAL);
4875 linkea_first_entry(ldata);
4877 GOTO(out, rc = -ENODATA);
4879 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4882 /* Note: lname might miss \0 at the end */
4883 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4884 lname.ln_namelen, lname.ln_name);
4885 lname.ln_name = filename;
4887 pobj = mdd_object_find(env, mdd, fid);
4894 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4896 stripe = mdd_object_find(env, mdd, fid);
4897 if (IS_ERR(stripe)) {
4898 mdd_object_put(env, pobj);
4900 GOTO(out, rc = PTR_ERR(stripe));
4903 if (!lmv_is_fixed(lmv))
4904 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, false,
4908 handle = mdd_trans_create(env, mdd);
4910 GOTO(out, rc = PTR_ERR(handle));
4912 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4913 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4915 GOTO(stop_trans, rc);
4917 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4918 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
4919 &xattrs, mlc, &lname, handle);
4921 GOTO(stop_trans, rc);
4924 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4927 GOTO(stop_trans, rc);
4929 rc = mdd_trans_start(env, mdd, handle);
4931 GOTO(stop_trans, rc);
4933 mdd_write_lock(env, obj, DT_SRC_PARENT);
4934 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4935 rc = mdo_layout_change(env, obj, mlc, handle);
4936 mdd_write_unlock(env, obj);
4938 GOTO(stop_trans, rc);
4940 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4941 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
4942 mlc, &lname, handle);
4944 GOTO(stop_trans, rc);
4947 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4948 XATTR_NAME_LMV, handle);
4949 GOTO(stop_trans, rc);
4952 rc = mdd_trans_stop(env, mdd, rc, handle);
4954 mdd_xattrs_fini(&xattrs);
4956 mdd_object_put(env, stripe);
4957 mdd_object_put(env, pobj);
4959 lu_buf_free(&lmv_buf);
4963 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4964 struct mdd_device *mdd,
4965 struct mdd_object *pobj,
4966 struct mdd_object *obj,
4967 struct mdd_object *tobj,
4968 struct mdd_xattrs *xattrs,
4969 struct md_layout_change *mlc,
4970 struct dt_allocation_hint *hint,
4971 struct thandle *handle)
4973 struct mdd_thread_info *info = mdd_env_info(env);
4974 const struct lu_name *lname = mlc->mlc_name;
4975 struct lu_attr *la = &info->mdi_la_for_fix;
4976 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4977 struct linkea_data *ldata = &info->mdi_link_data;
4978 struct lmv_mds_md_v1 *lmv;
4982 mlc->mlc_opc = MD_LAYOUT_DETACH;
4983 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4987 memset(ldata, 0, sizeof(*ldata));
4988 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
4989 lname, 1, 0, ldata);
4993 count = lum->lum_stripe_count;
4994 lum->lum_stripe_count = 0;
4995 /* don't set default LMV since it will become a striped dir */
4996 lum->lum_max_inherit = LMV_INHERIT_NONE;
4997 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
4999 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
5000 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
5001 ldata, NULL, NULL, NULL, hint);
5005 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
5008 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
5010 lmv = (typeof(lmv))info->mdi_key;
5011 memset(lmv, 0, sizeof(*lmv));
5012 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
5013 lmv->lmv_stripe_count = cpu_to_le32(1);
5014 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
5015 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
5017 mlc->mlc_opc = MD_LAYOUT_ATTACH;
5018 mlc->mlc_buf.lb_buf = lmv;
5019 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
5020 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5024 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
5025 mdo_declare_xattr_set);
5029 lum->lum_stripe_count = count;
5030 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5031 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5035 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5039 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
5040 S_IFDIR, lname->ln_name, handle);
5044 la->la_valid = LA_CTIME | LA_MTIME;
5045 rc = mdo_declare_attr_set(env, obj, la, handle);
5049 rc = mdo_declare_attr_set(env, pobj, la, handle);
5053 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
5059 * plain directory split:
5060 * 1. create \a tobj as plain directory.
5061 * 2. append \a obj as first stripe of \a tobj.
5062 * 3. migrate xattrs from \a obj to \a tobj.
5063 * 4. split \a tobj to specific stripe count.
5065 static int mdd_dir_split_plain(const struct lu_env *env,
5066 struct mdd_device *mdd,
5067 struct mdd_object *pobj,
5068 struct mdd_object *obj,
5069 struct mdd_object *tobj,
5070 struct mdd_xattrs *xattrs,
5071 struct md_layout_change *mlc,
5072 struct dt_allocation_hint *hint,
5073 struct thandle *handle)
5075 struct mdd_thread_info *info = mdd_env_info(env);
5076 struct lu_attr *pattr = &info->mdi_pattr;
5077 struct lu_attr *la = &info->mdi_la_for_fix;
5078 const struct lu_name *lname = mlc->mlc_name;
5079 struct linkea_data *ldata = &info->mdi_link_data;
5084 /* copy linkea out and set on target later */
5085 rc = mdd_links_read(env, obj, ldata);
5089 mlc->mlc_opc = MD_LAYOUT_DETACH;
5090 rc = mdo_layout_change(env, obj, mlc, handle);
5094 /* don't set nlink from obj */
5095 mlc->mlc_attr->la_valid &= ~LA_NLINK;
5097 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5098 NULL, NULL, NULL, hint, handle, false);
5102 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
5106 rc = mdd_links_write(env, tobj, ldata, handle);
5110 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
5114 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
5115 lname->ln_name, handle);
5119 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
5120 la->la_valid = LA_CTIME | LA_MTIME;
5122 mdd_write_lock(env, obj, DT_SRC_CHILD);
5123 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
5124 mdd_write_unlock(env, obj);
5128 rc = mdd_la_get(env, pobj, pattr);
5132 la->la_valid = LA_CTIME | LA_MTIME;
5134 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5135 rc = mdd_update_time(env, pobj, pattr, la, handle);
5136 mdd_write_unlock(env, pobj);
5140 /* FID changes, record it as CL_MIGRATE */
5141 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
5142 pobj, pattr, mdd_object_fid(obj),
5143 pobj, pattr, lname, lname, handle);
5147 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
5148 struct md_layout_change *mlc)
5150 struct mdd_thread_info *info = mdd_env_info(env);
5151 struct mdd_device *mdd = mdo2mdd(o);
5152 struct mdd_object *obj = md2mdd_obj(o);
5153 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
5154 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
5155 struct dt_allocation_hint *hint = &info->mdi_hint;
5156 bool is_plain = false;
5157 struct mdd_xattrs xattrs;
5158 struct thandle *handle;
5163 LASSERT(S_ISDIR(mdd_object_type(obj)));
5165 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
5171 mdd_xattrs_init(&xattrs);
5173 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true, true);
5175 handle = mdd_trans_create(env, mdd);
5177 GOTO(out, rc = PTR_ERR(handle));
5180 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
5181 &xattrs, mlc, hint, handle);
5183 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5184 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5186 GOTO(stop_trans, rc);
5188 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5192 GOTO(stop_trans, rc);
5194 rc = mdd_trans_start(env, mdd, handle);
5196 GOTO(stop_trans, rc);
5199 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5202 struct lu_buf *buf = &info->mdi_buf[0];
5204 buf->lb_buf = mlc->mlc_spec->u.sp_ea.eadata;
5205 buf->lb_len = mlc->mlc_spec->u.sp_ea.eadatalen;
5207 mdd_write_lock(env, obj, DT_TGT_CHILD);
5208 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LMV,
5209 LU_XATTR_CREATE, handle);
5210 mdd_write_unlock(env, obj);
5212 GOTO(stop_trans, rc);
5214 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5215 XATTR_NAME_LMV, handle);
5218 GOTO(stop_trans, rc);
5223 rc = mdd_trans_stop(env, mdd, rc, handle);
5225 mdd_xattrs_fini(&xattrs);
5230 const struct md_dir_operations mdd_dir_ops = {
5231 .mdo_is_subdir = mdd_is_subdir,
5232 .mdo_lookup = mdd_lookup,
5233 .mdo_create = mdd_create,
5234 .mdo_rename = mdd_rename,
5235 .mdo_link = mdd_link,
5236 .mdo_unlink = mdd_unlink,
5237 .mdo_create_data = mdd_create_data,
5238 .mdo_migrate = mdd_migrate,