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 *pattr,
3042 struct mdd_object *tgt_pobj,
3043 const struct lu_attr *tpattr,
3044 struct mdd_object *sobj,
3045 const struct lu_attr *cattr,
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 != cattr->la_projid) ||
3064 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
3065 (pattr->la_projid != tpattr->la_projid))) &&
3066 S_ISDIR(cattr->la_mode))
3069 /* we prevent an encrypted file from being renamed
3070 * into an unencrypted dir
3072 if ((pattr->la_valid & LA_FLAGS &&
3073 pattr->la_flags & LUSTRE_ENCRYPT_FL) &&
3074 !(tpattr->la_valid & LA_FLAGS &&
3075 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
3078 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
3082 /* XXX: when get here, "tobj == NULL" means tobj must
3083 * NOT exist (neither on remote MDS, such case has been
3084 * processed in cld_rename before mdd_rename and enable
3086 * So check may_create, but not check may_unlink. */
3088 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
3089 (src_pobj != tgt_pobj));
3091 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
3092 (src_pobj != tgt_pobj), 1);
3094 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
3095 rc = __mdd_may_link(env, tgt_pobj, tpattr);
3100 static int mdd_declare_rename(const struct lu_env *env,
3101 struct mdd_device *mdd,
3102 struct mdd_object *mdd_spobj,
3103 struct mdd_object *mdd_tpobj,
3104 struct mdd_object *mdd_sobj,
3105 struct mdd_object *mdd_tobj,
3106 const struct lu_name *sname,
3107 const struct lu_name *tname,
3109 struct linkea_data *ldata, bool change_projid,
3110 struct thandle *handle)
3112 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3115 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3116 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3122 /* name from source dir */
3123 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
3127 /* .. from source child */
3128 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3129 /* source child can be directory, count by source dir's nlink */
3130 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
3133 if (mdd_spobj != mdd_tpobj) {
3134 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
3139 rc = mdo_declare_index_insert(env, mdd_sobj,
3140 mdd_object_fid(mdd_tpobj),
3141 S_IFDIR, dotdot, handle);
3146 /* new target child can be directory,
3147 * counted by target dir's nlink */
3148 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
3153 la->la_valid = LA_CTIME | LA_MTIME;
3154 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
3158 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
3162 la->la_valid = LA_CTIME;
3164 la->la_valid |= LA_PROJID;
3165 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3169 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
3174 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
3175 mdd_object_type(mdd_sobj),
3176 tname->ln_name, handle);
3180 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
3181 /* delete target child in target parent directory */
3182 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
3187 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3191 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
3192 /* target child can be directory,
3193 * delete "." reference in target child directory */
3194 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3198 /* delete ".." reference in target parent directory */
3199 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
3204 la->la_valid = LA_CTIME;
3205 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
3209 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
3214 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
3222 static int mdd_migrate_object(const struct lu_env *env,
3223 struct mdd_object *spobj,
3224 struct mdd_object *tpobj,
3225 struct mdd_object *sobj,
3226 struct mdd_object *tobj,
3227 const struct lu_name *sname,
3228 const struct lu_name *tname,
3229 struct md_op_spec *spec,
3230 struct md_attr *ma);
3232 /* src object can be remote that is why we use only fid and type of object */
3233 static int mdd_rename(const struct lu_env *env,
3234 struct md_object *src_pobj, struct md_object *tgt_pobj,
3235 const struct lu_fid *lf, const struct lu_name *lsname,
3236 struct md_object *tobj, const struct lu_name *ltname,
3239 const char *sname = lsname->ln_name;
3240 const char *tname = ltname->ln_name;
3241 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3242 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3243 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
3244 struct mdd_device *mdd = mdo2mdd(src_pobj);
3245 struct mdd_object *mdd_sobj = NULL; /* source object */
3246 struct mdd_object *mdd_tobj = NULL;
3247 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
3248 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
3249 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3250 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3251 struct thandle *handle;
3252 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3253 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3254 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3257 bool tobj_locked = 0;
3258 bool change_projid = false;
3259 unsigned cl_flags = 0;
3263 /* let unlink to complete and commit */
3264 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3267 mdd_tobj = md2mdd_obj(tobj);
3269 mdd_sobj = mdd_object_find(env, mdd, lf);
3270 if (IS_ERR(mdd_sobj))
3271 RETURN(PTR_ERR(mdd_sobj));
3273 rc = mdd_la_get(env, mdd_sobj, cattr);
3275 GOTO(out_pending, rc);
3277 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3278 * hard links, and target doesn't exist.
3280 if (mdd_object_remote(mdd_sobj) && S_ISLNK(cattr->la_mode) &&
3281 cattr->la_nlink == 1 && !tobj) {
3282 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3283 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3286 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3289 GOTO(out_pending, rc);
3291 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3292 if (IS_ERR(mdd_tobj))
3293 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3295 memset(spec, 0, sizeof(*spec));
3296 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3297 mdd_tobj, lsname, ltname, spec, ma);
3298 mdd_object_put(env, mdd_tobj);
3299 GOTO(out_pending, rc);
3302 rc = mdd_la_get(env, mdd_spobj, pattr);
3304 GOTO(out_pending, rc);
3307 rc = mdd_la_get(env, mdd_tobj, tattr);
3309 GOTO(out_pending, rc);
3310 /* search for an existing archive.
3311 * we should check ahead as the object
3312 * can be destroyed in this transaction */
3313 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3314 cl_flags |= CLF_RENAME_LAST_EXISTS;
3317 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3319 GOTO(out_pending, rc);
3321 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3322 mdd_sobj, cattr, mdd_tobj, tattr);
3324 GOTO(out_pending, rc);
3326 rc = mdd_name_check(env, mdd, ltname);
3328 GOTO(out_pending, rc);
3330 handle = mdd_trans_create(env, mdd);
3332 GOTO(out_pending, rc = PTR_ERR(handle));
3334 memset(ldata, 0, sizeof(*ldata));
3335 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3336 lsname, mdd_object_fid(mdd_tpobj), ltname,
3341 if (tpattr->la_projid != cattr->la_projid &&
3342 tpattr->la_flags & LUSTRE_PROJINHERIT_FL)
3343 change_projid = true;
3345 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3346 mdd_tobj, lsname, ltname, ma, ldata,
3347 change_projid, handle);
3351 rc = mdd_trans_start(env, mdd, handle);
3355 is_dir = S_ISDIR(cattr->la_mode);
3357 /* Remove source name from source directory */
3358 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3362 /* "mv dir1 dir2" needs "dir1/.." link update */
3363 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3364 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3366 GOTO(fixup_spobj2, rc);
3368 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3371 GOTO(fixup_spobj, rc);
3374 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3375 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3377 /* tname might been renamed to something else */
3378 GOTO(fixup_spobj, rc);
3381 /* Insert new fid with target name into target dir */
3382 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3385 GOTO(fixup_tpobj, rc);
3387 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3388 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3390 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3391 la->la_valid = LA_CTIME;
3392 if (change_projid) {
3393 /* mdd_update_time honors other valid flags except TIME ones */
3394 la->la_valid |= LA_PROJID;
3395 la->la_projid = tpattr->la_projid;
3397 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3399 GOTO(fixup_tpobj, rc);
3401 /* Update the linkEA for the source object */
3402 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3403 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3404 lsname, mdd_object_fid(mdd_tpobj), ltname,
3405 handle, ldata, 0, 0);
3407 /* Old files might not have EA entry */
3408 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3409 lsname, handle, NULL, 0);
3410 mdd_write_unlock(env, mdd_sobj);
3411 /* We don't fail the transaction if the link ea can't be
3412 updated -- fid2path will use alternate lookup method. */
3415 /* Remove old target object
3416 * For tobj is remote case cmm layer has processed
3417 * and set tobj to NULL then. So when tobj is NOT NULL,
3418 * it must be local one.
3420 if (tobj && mdd_object_exists(mdd_tobj)) {
3421 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3423 if (mdd_is_dead_obj(mdd_tobj)) {
3424 /* shld not be dead, something is wrong */
3425 CERROR("tobj is dead, something is wrong\n");
3429 mdo_ref_del(env, mdd_tobj, handle);
3431 /* Remove dot reference. */
3432 if (S_ISDIR(tattr->la_mode))
3433 mdo_ref_del(env, mdd_tobj, handle);
3436 /* fetch updated nlink */
3437 rc = mdd_la_get(env, mdd_tobj, tattr);
3439 CERROR("%s: Failed to get nlink for tobj "
3441 mdd2obd_dev(mdd)->obd_name,
3442 PFID(tpobj_fid), rc);
3443 GOTO(fixup_tpobj, rc);
3446 la->la_valid = LA_CTIME;
3447 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3449 CERROR("%s: Failed to set ctime for tobj "
3451 mdd2obd_dev(mdd)->obd_name,
3452 PFID(tpobj_fid), rc);
3453 GOTO(fixup_tpobj, rc);
3456 /* XXX: this transfer to ma will be removed with LOD/OSP */
3457 ma->ma_attr = *tattr;
3458 ma->ma_valid |= MA_INODE;
3459 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3462 CERROR("%s: Failed to unlink tobj "
3464 mdd2obd_dev(mdd)->obd_name,
3465 PFID(tpobj_fid), rc);
3466 GOTO(fixup_tpobj, rc);
3469 /* fetch updated nlink */
3470 rc = mdd_la_get(env, mdd_tobj, tattr);
3471 if (rc == -ENOENT) {
3472 /* the object got removed, let's
3473 * return the latest known attributes */
3474 tattr->la_nlink = 0;
3476 } else if (rc != 0) {
3477 CERROR("%s: Failed to get nlink for tobj "
3479 mdd2obd_dev(mdd)->obd_name,
3480 PFID(tpobj_fid), rc);
3481 GOTO(fixup_tpobj, rc);
3483 /* XXX: this transfer to ma will be removed with LOD/OSP */
3484 ma->ma_attr = *tattr;
3485 ma->ma_valid |= MA_INODE;
3487 if (tattr->la_nlink == 0)
3488 cl_flags |= CLF_RENAME_LAST;
3490 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3493 la->la_valid = LA_CTIME | LA_MTIME;
3494 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3496 GOTO(fixup_tpobj, rc);
3498 if (mdd_spobj != mdd_tpobj) {
3499 la->la_valid = LA_CTIME | LA_MTIME;
3500 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3502 GOTO(fixup_tpobj, rc);
3509 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3511 CWARN("tp obj fix error %d\n",rc2);
3513 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3514 !mdd_is_dead_obj(mdd_tobj)) {
3516 mdo_ref_add(env, mdd_tobj, handle);
3518 mdo_ref_add(env, mdd_tobj, handle);
3521 rc2 = __mdd_index_insert(env, mdd_tpobj,
3522 mdd_object_fid(mdd_tobj),
3523 mdd_object_type(mdd_tobj),
3526 CWARN("tp obj fix error: rc = %d\n", rc2);
3531 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3532 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3534 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3535 mdd2obd_dev(mdd)->obd_name, rc2);
3538 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3541 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3542 mdd2obd_dev(mdd)->obd_name, rc2);
3547 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3548 mdd_object_type(mdd_sobj), sname,
3551 CWARN("sp obj fix error: rc = %d\n", rc2);
3556 mdd_write_unlock(env, mdd_tobj);
3559 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3560 mdd_tobj, mdd_tpobj, tpattr, lf,
3561 mdd_spobj, pattr, ltname, lsname,
3565 rc = mdd_trans_stop(env, mdd, rc, handle);
3568 mdd_object_put(env, mdd_sobj);
3573 * Check whether we should migrate the file/dir
3575 * < 0 permission check failed or other error.
3576 * = 0 the file can be migrated.
3578 static int mdd_migrate_sanity_check(const struct lu_env *env,
3579 struct mdd_device *mdd,
3580 struct mdd_object *spobj,
3581 struct mdd_object *tpobj,
3582 struct mdd_object *sobj,
3583 struct mdd_object *tobj,
3584 const struct lu_attr *spattr,
3585 const struct lu_attr *tpattr,
3586 const struct lu_attr *attr)
3592 if (!mdd_object_remote(sobj)) {
3593 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3594 if (sobj->mod_count > 0) {
3595 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3596 mdd_obj_dev_name(sobj),
3597 PFID(mdd_object_fid(sobj)),
3599 mdd_read_unlock(env, sobj);
3602 mdd_read_unlock(env, sobj);
3605 if (mdd_object_exists(tobj))
3608 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3612 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3617 struct mdd_xattr_entry {
3618 struct list_head mxe_linkage;
3620 struct lu_buf mxe_buf;
3624 struct lu_buf mx_namebuf;
3625 struct list_head mx_list;
3628 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3630 INIT_LIST_HEAD(&xattrs->mx_list);
3631 xattrs->mx_namebuf.lb_buf = NULL;
3632 xattrs->mx_namebuf.lb_len = 0;
3635 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3637 struct mdd_xattr_entry *entry;
3638 struct mdd_xattr_entry *tmp;
3640 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3641 lu_buf_free(&entry->mxe_buf);
3642 list_del(&entry->mxe_linkage);
3643 OBD_FREE_PTR(entry);
3646 lu_buf_free(&xattrs->mx_namebuf);
3649 /* read xattrs into buf, but skip LMA, LMV, LINKEA if 'skip_linkea' is
3650 * set, and DMV if 'skip_dmv" is set.
3652 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3653 struct mdd_xattrs *xattrs,
3654 struct mdd_object *sobj,
3658 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3659 struct mdd_xattr_entry *entry;
3660 bool needencxattr = false;
3661 bool encxattrfound = false;
3671 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3672 if (list_xsize == -ENODATA)
3678 if (attr->la_valid & LA_FLAGS &&
3679 attr->la_flags & LUSTRE_ENCRYPT_FL) {
3680 needencxattr = true;
3682 strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3685 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3686 if (xattrs->mx_namebuf.lb_buf == NULL)
3689 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3695 xname = xattrs->mx_namebuf.lb_buf;
3697 for (; rem > 0; xname += xlen, rem -= xlen) {
3699 strcmp(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3700 encxattrfound = true;
3701 xlen = strnlen(xname, rem - 1) + 1;
3702 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3703 strcmp(XATTR_NAME_LMV, xname) == 0)
3707 strcmp(XATTR_NAME_LINK, xname) == 0)
3711 strcmp(XATTR_NAME_DEFAULT_LMV, xname) == 0)
3714 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3715 if (xsize == -ENODATA)
3718 GOTO(fini, rc = xsize);
3720 OBD_ALLOC_PTR(entry);
3722 GOTO(fini, rc = -ENOMEM);
3724 lu_buf_alloc(&entry->mxe_buf, xsize);
3725 if (!entry->mxe_buf.lb_buf) {
3726 OBD_FREE_PTR(entry);
3727 GOTO(fini, rc = -ENOMEM);
3730 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3732 lu_buf_free(&entry->mxe_buf);
3733 OBD_FREE_PTR(entry);
3739 entry->mxe_name = xname;
3740 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3743 if (needencxattr && !encxattrfound) {
3744 xlen = strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3745 strncpy(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT, xlen);
3752 mdd_xattrs_fini(xattrs);
3756 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
3757 struct mdd_object *obj,
3758 const struct lu_buf *buf,
3760 int fl, struct thandle *handle);
3762 static int mdd_foreach_xattr(const struct lu_env *env,
3763 struct mdd_object *tobj,
3764 struct mdd_xattrs *xattrs,
3765 struct thandle *handle,
3768 struct mdd_xattr_entry *entry;
3771 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
3772 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
3780 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
3781 struct mdd_object *sobj,
3782 struct mdd_object *tobj,
3783 const struct lu_name *sname,
3784 const struct lu_fid *sfid,
3785 const struct lu_name *lname,
3786 const struct lu_fid *fid,
3788 struct thandle *handle);
3790 static int mdd_declare_update_link(const struct lu_env *env,
3791 struct mdd_object *sobj,
3792 struct mdd_object *tobj,
3793 const struct lu_name *tname,
3794 const struct lu_fid *tpfid,
3795 const struct lu_name *lname,
3796 const struct lu_fid *fid,
3798 struct thandle *handle)
3800 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3801 struct mdd_object *pobj;
3805 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3806 !strcmp(tname->ln_name, lname->ln_name))
3809 pobj = mdd_object_find(env, mdd, fid);
3811 return PTR_ERR(pobj);
3814 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
3816 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
3817 mdd_object_type(sobj),
3818 lname->ln_name, handle);
3819 mdd_object_put(env, pobj);
3823 rc = mdo_declare_ref_add(env, tobj, handle);
3827 rc = mdo_declare_ref_del(env, sobj, handle);
3831 static int mdd_update_link(const struct lu_env *env,
3832 struct mdd_object *sobj,
3833 struct mdd_object *tobj,
3834 const struct lu_name *tname,
3835 const struct lu_fid *tpfid,
3836 const struct lu_name *lname,
3837 const struct lu_fid *fid,
3839 struct thandle *handle)
3841 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3842 struct mdd_object *pobj;
3848 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3849 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3852 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
3853 PFID(fid), PNAME(lname), PFID(mdd_object_fid(tobj)));
3855 pobj = mdd_object_find(env, mdd, fid);
3857 CWARN("%s: cannot find obj "DFID": %ld\n",
3858 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
3859 RETURN(PTR_ERR(pobj));
3862 if (!mdd_object_exists(pobj)) {
3863 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
3864 mdd_object_put(env, pobj);
3868 mdd_write_lock(env, pobj, DT_TGT_PARENT);
3869 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
3871 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
3872 mdd_object_type(sobj),
3873 lname->ln_name, handle);
3874 mdd_write_unlock(env, pobj);
3875 mdd_object_put(env, pobj);
3879 mdd_write_lock(env, tobj, DT_TGT_CHILD);
3880 rc = mdo_ref_add(env, tobj, handle);
3881 mdd_write_unlock(env, tobj);
3885 mdd_write_lock(env, sobj, DT_SRC_CHILD);
3886 rc = mdo_ref_del(env, sobj, handle);
3887 mdd_write_unlock(env, sobj);
3892 static inline int mdd_fld_lookup(const struct lu_env *env,
3893 struct mdd_device *mdd,
3894 const struct lu_fid *fid,
3897 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
3898 struct seq_server_site *ss;
3901 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
3903 range->lsr_flags = LU_SEQ_RANGE_MDT;
3904 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
3908 *mdt_index = range->lsr_index;
3913 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
3914 struct mdd_object *sobj,
3915 struct mdd_object *tobj,
3916 const struct lu_name *tname,
3917 const struct lu_fid *tpfid,
3918 const struct lu_name *lname,
3919 const struct lu_fid *fid,
3921 struct thandle *handle)
3923 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3924 __u32 source_mdt_index = *(__u32 *)opaque;
3925 __u32 link_mdt_index;
3931 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
3932 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
3935 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
3939 RETURN(link_mdt_index == source_mdt_index);
3942 static int mdd_iterate_linkea(const struct lu_env *env,
3943 struct mdd_object *sobj,
3944 struct mdd_object *tobj,
3945 const struct lu_name *tname,
3946 const struct lu_fid *tpfid,
3947 struct linkea_data *ldata,
3949 struct thandle *handle,
3952 struct mdd_thread_info *info = mdd_env_info(env);
3953 char *filename = info->mdi_name;
3954 struct lu_name lname;
3961 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
3962 linkea_next_entry(ldata)) {
3963 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
3966 /* Note: lname might miss \0 at the end */
3967 snprintf(filename, sizeof(info->mdi_name), "%.*s",
3968 lname.ln_namelen, lname.ln_name);
3969 lname.ln_name = filename;
3971 CDEBUG(D_INFO, DFID"/"DNAME"\n", PFID(&fid), PNAME(&lname));
3973 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
3981 * Prepare linkea, and check whether file needs migrate: if source still has
3982 * link on source MDT, no need to migrate, just update namespace on source and
3985 * \retval 0 do migrate
3986 * \retval 1 don't migrate
3987 * \retval -errno on failure
3989 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
3990 struct mdd_device *mdd,
3991 struct mdd_object *spobj,
3992 struct mdd_object *tpobj,
3993 struct mdd_object *sobj,
3994 const struct lu_name *sname,
3995 const struct lu_name *tname,
3996 const struct lu_attr *attr,
3997 struct linkea_data *ldata)
3999 __u32 source_mdt_index;
4004 memset(ldata, 0, sizeof(*ldata));
4005 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
4006 mdd_object_fid(tpobj), tname, 1, 0, ldata);
4011 * Then it will check if the file should be migrated. If the file has
4012 * mulitple links, we only need migrate the file if all of its entries
4013 * has been migrated to the remote MDT.
4015 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
4018 /* If there are still links locally, don't migrate this file */
4019 LASSERT(ldata->ld_leh != NULL);
4022 * If linkEA is overflow, it means there are some unknown name entries
4023 * under unknown parents, which will prevent the migration.
4025 if (unlikely(ldata->ld_leh->leh_overflow_time))
4028 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
4032 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
4033 ldata, &source_mdt_index, NULL,
4034 mdd_is_link_on_source_mdt);
4038 static int mdd_declare_migrate_update(const struct lu_env *env,
4039 struct mdd_object *spobj,
4040 struct mdd_object *tpobj,
4041 struct mdd_object *obj,
4042 const struct lu_name *sname,
4043 const struct lu_name *tname,
4044 struct lu_attr *attr,
4045 struct lu_attr *spattr,
4046 struct lu_attr *tpattr,
4047 struct linkea_data *ldata,
4049 struct thandle *handle)
4051 struct mdd_thread_info *info = mdd_env_info(env);
4052 struct lu_attr *la = &info->mdi_la_for_fix;
4055 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
4059 if (S_ISDIR(attr->la_mode)) {
4060 rc = mdo_declare_ref_del(env, spobj, handle);
4065 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
4066 attr->la_mode & S_IFMT,
4067 tname->ln_name, handle);
4071 rc = mdd_declare_links_add(env, obj, handle, ldata);
4075 if (S_ISDIR(attr->la_mode)) {
4076 rc = mdo_declare_ref_add(env, tpobj, handle);
4081 la->la_valid = LA_CTIME | LA_MTIME;
4082 rc = mdo_declare_attr_set(env, spobj, la, handle);
4086 if (tpobj != spobj) {
4087 rc = mdo_declare_attr_set(env, tpobj, la, handle);
4095 static int mdd_declare_migrate_create(const struct lu_env *env,
4096 struct mdd_object *spobj,
4097 struct mdd_object *tpobj,
4098 struct mdd_object *sobj,
4099 struct mdd_object *tobj,
4100 const struct lu_name *sname,
4101 const struct lu_name *tname,
4102 struct lu_attr *spattr,
4103 struct lu_attr *tpattr,
4104 struct lu_attr *attr,
4105 struct lu_buf *sbuf,
4106 struct linkea_data *ldata,
4107 struct mdd_xattrs *xattrs,
4109 struct md_op_spec *spec,
4110 struct dt_allocation_hint *hint,
4111 struct thandle *handle)
4113 struct mdd_thread_info *info = mdd_env_info(env);
4114 struct md_layout_change *mlc = &info->mdi_mlc;
4115 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
4120 if (S_ISDIR(attr->la_mode)) {
4121 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
4123 mlc->mlc_opc = MD_LAYOUT_DETACH;
4124 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
4128 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4129 } else if (S_ISLNK(attr->la_mode)) {
4130 spec->u.sp_symname.ln_name = sbuf->lb_buf;
4131 /* don't count NUL */
4132 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
4133 } else if (S_ISREG(attr->la_mode)) {
4134 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
4135 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
4138 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
4140 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
4141 tname, attr, handle, spec, ldata, NULL, NULL,
4147 * tobj mode will be used in mdo_declare_layout_change(), but it's not
4148 * createb yet, copy from sobj.
4150 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
4151 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
4152 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
4154 if (S_ISDIR(attr->la_mode)) {
4156 /* if sobj is not striped, fake a 1-stripe LMV */
4157 LASSERT(sizeof(info->mdi_key) >
4158 lmv_mds_md_size(1, LMV_MAGIC_V1));
4159 lmv = (typeof(lmv))info->mdi_key;
4160 memset(lmv, 0, sizeof(*lmv));
4161 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4162 lmv->lmv_stripe_count = cpu_to_le32(1);
4163 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4164 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
4165 mdd_object_fid(sobj));
4166 mlc->mlc_buf.lb_buf = lmv;
4167 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4169 mlc->mlc_buf = *sbuf;
4171 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4172 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4177 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4178 mdo_declare_xattr_set);
4182 if (S_ISREG(attr->la_mode)) {
4183 struct lu_buf fid_buf;
4185 handle->th_complex = 1;
4187 /* target may be remote, update PFID via sobj. */
4188 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4189 fid_buf.lb_len = sizeof(struct lu_fid);
4190 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
4195 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4200 if (!S_ISDIR(attr->la_mode)) {
4201 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4202 mdd_object_fid(tpobj), ldata, NULL,
4203 handle, mdd_declare_update_link);
4208 if (!S_ISDIR(attr->la_mode) || lmv) {
4209 rc = mdo_declare_ref_del(env, sobj, handle);
4213 if (S_ISDIR(attr->la_mode)) {
4214 rc = mdo_declare_ref_del(env, sobj, handle);
4219 rc = mdo_declare_destroy(env, sobj, handle);
4224 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
4225 attr, spattr, tpattr, ldata, ma,
4231 * migrate dirent from \a spobj to \a tpobj.
4233 static int mdd_migrate_update(const struct lu_env *env,
4234 struct mdd_object *spobj,
4235 struct mdd_object *tpobj,
4236 struct mdd_object *obj,
4237 const struct lu_name *sname,
4238 const struct lu_name *tname,
4239 struct lu_attr *attr,
4240 struct lu_attr *spattr,
4241 struct lu_attr *tpattr,
4242 struct linkea_data *ldata,
4244 struct thandle *handle)
4246 struct mdd_thread_info *info = mdd_env_info(env);
4247 struct lu_attr *la = &info->mdi_la_for_fix;
4252 CDEBUG(D_INFO, "update "DFID" from "DFID"/%s to "DFID"/%s\n",
4253 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
4254 sname->ln_name, PFID(mdd_object_fid(tpobj)), tname->ln_name);
4256 rc = __mdd_index_delete(env, spobj, sname->ln_name,
4257 S_ISDIR(attr->la_mode), handle);
4261 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
4262 attr->la_mode & S_IFMT,
4263 tname->ln_name, handle);
4267 rc = mdd_links_write(env, obj, ldata, handle);
4271 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4272 la->la_valid = LA_CTIME | LA_MTIME;
4273 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4274 rc = mdd_update_time(env, spobj, spattr, la, handle);
4275 mdd_write_unlock(env, spobj);
4279 if (tpobj != spobj) {
4280 la->la_valid = LA_CTIME | LA_MTIME;
4281 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4282 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4283 mdd_write_unlock(env, tpobj);
4292 * Migrate file/dir to target MDT.
4294 * Create target according to \a spec, and then migrate xattrs, if it's
4295 * directory, migrate source stripes to target.
4297 * \param[in] env execution environment
4298 * \param[in] spobj source parent object
4299 * \param[in] tpobj target parent object
4300 * \param[in] sobj source object
4301 * \param[in] tobj target object
4302 * \param[in] lname file name
4303 * \param[in] spattr source parent attributes
4304 * \param[in] tpattr target parent attributes
4305 * \param[in] attr source attributes
4306 * \param[in] sbuf source LMV buf
4307 * \param[in] spec migrate create spec
4308 * \param[in] hint target creation hint
4309 * \param[in] handle tranasction handle
4311 * \retval 0 on success
4312 * \retval -errno on failure
4314 static int mdd_migrate_create(const struct lu_env *env,
4315 struct mdd_object *spobj,
4316 struct mdd_object *tpobj,
4317 struct mdd_object *sobj,
4318 struct mdd_object *tobj,
4319 const struct lu_name *sname,
4320 const struct lu_name *tname,
4321 struct lu_attr *spattr,
4322 struct lu_attr *tpattr,
4323 struct lu_attr *attr,
4324 const struct lu_buf *sbuf,
4325 struct linkea_data *ldata,
4326 struct mdd_xattrs *xattrs,
4328 struct md_op_spec *spec,
4329 struct dt_allocation_hint *hint,
4330 struct thandle *handle)
4337 * migrate sobj stripes to tobj if it's directory:
4338 * 1. detach stripes from sobj.
4339 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4340 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4342 if (S_ISDIR(attr->la_mode)) {
4343 struct mdd_thread_info *info = mdd_env_info(env);
4344 struct md_layout_change *mlc = &info->mdi_mlc;
4346 mlc->mlc_opc = MD_LAYOUT_DETACH;
4348 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4349 rc = mdo_layout_change(env, sobj, mlc, handle);
4350 mdd_write_unlock(env, sobj);
4355 /* don't set nlink from sobj */
4356 attr->la_valid &= ~LA_NLINK;
4358 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4359 hint, handle, false);
4363 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4364 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4365 mdd_write_unlock(env, tobj);
4369 /* for regular file, update OST objects XATTR_NAME_FID */
4370 if (S_ISREG(attr->la_mode)) {
4371 struct lu_buf fid_buf;
4373 /* target may be remote, update PFID via sobj. */
4374 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4375 fid_buf.lb_len = sizeof(struct lu_fid);
4376 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4381 /* delete LOV to avoid deleting OST objs when destroying sobj */
4382 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4383 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4384 mdd_write_unlock(env, sobj);
4385 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4386 if (rc && rc != -ENODATA)
4391 /* update links FID */
4392 if (!S_ISDIR(attr->la_mode)) {
4393 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4394 mdd_object_fid(tpobj), ldata,
4395 NULL, handle, mdd_update_link);
4400 /* don't destroy sobj if it's plain directory */
4401 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4402 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4403 rc = mdo_ref_del(env, sobj, handle);
4405 if (S_ISDIR(attr->la_mode))
4406 rc = mdo_ref_del(env, sobj, handle);
4408 rc = mdo_destroy(env, sobj, handle);
4410 mdd_write_unlock(env, sobj);
4415 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4416 spattr, tpattr, ldata, ma, handle);
4421 /* NB: if user issued different migrate command, we can't adjust it silently
4422 * here, because this command will decide target MDT in subdir migration in
4425 static int mdd_migrate_cmd_check(struct mdd_device *mdd,
4426 const struct lmv_mds_md_v1 *lmv,
4427 const struct lmv_user_md_v1 *lum,
4428 const struct lu_name *lname)
4430 __u32 lum_stripe_count = lum->lum_stripe_count;
4431 __u32 lum_hash_type = lum->lum_hash_type &
4432 cpu_to_le32(LMV_HASH_TYPE_MASK);
4433 __u32 lmv_hash_type = lmv->lmv_hash_type &
4434 cpu_to_le32(LMV_HASH_TYPE_MASK);
4436 if (!lmv_is_sane(lmv))
4439 /* if stripe_count unspecified, set to 1 */
4440 if (!lum_stripe_count)
4441 lum_stripe_count = cpu_to_le32(1);
4443 /* TODO: check specific MDTs */
4444 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4445 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4446 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4447 CERROR("%s: '"DNAME"' migration was interrupted, run 'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration.\n",
4448 mdd2obd_dev(mdd)->obd_name, PNAME(lname),
4449 le32_to_cpu(lmv->lmv_master_mdt_index),
4450 le32_to_cpu(lmv->lmv_migrate_offset),
4451 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4460 * Internal function to migrate directory or file between MDTs.
4462 * migrate source to target in following steps:
4463 * 1. create target, append source stripes after target's if it's directory,
4464 * migrate xattrs and update fid of source links.
4465 * 2. update namespace: migrate dirent from source parent to target parent,
4466 * update file linkea, and destroy source if it's not needed any more.
4468 * \param[in] env execution environment
4469 * \param[in] spobj source parent object
4470 * \param[in] tpobj target parent object
4471 * \param[in] sobj source object
4472 * \param[in] tobj target object
4473 * \param[in] sname source file name
4474 * \param[in] tname target file name
4475 * \param[in] spec target creation spec
4476 * \param[in] ma used to update \a pobj mtime and ctime
4478 * \retval 0 on success
4479 * \retval -errno on failure
4481 static int mdd_migrate_object(const struct lu_env *env,
4482 struct mdd_object *spobj,
4483 struct mdd_object *tpobj,
4484 struct mdd_object *sobj,
4485 struct mdd_object *tobj,
4486 const struct lu_name *sname,
4487 const struct lu_name *tname,
4488 struct md_op_spec *spec,
4491 struct mdd_thread_info *info = mdd_env_info(env);
4492 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4493 struct lu_attr *spattr = &info->mdi_pattr;
4494 struct lu_attr *tpattr = &info->mdi_tpattr;
4495 struct lu_attr *attr = &info->mdi_cattr;
4496 struct linkea_data *ldata = &info->mdi_link_data;
4497 struct dt_allocation_hint *hint = &info->mdi_hint;
4498 struct lu_buf sbuf = { NULL };
4499 struct mdd_xattrs xattrs;
4500 struct lmv_mds_md_v1 *lmv;
4501 struct thandle *handle;
4507 CDEBUG(D_INFO, "migrate %s from "DFID"/"DFID" to "DFID"/"DFID"\n",
4508 sname->ln_name, PFID(mdd_object_fid(spobj)),
4509 PFID(mdd_object_fid(sobj)), PFID(mdd_object_fid(tpobj)),
4510 PFID(mdd_object_fid(tobj)));
4513 rc = mdd_la_get(env, sobj, attr);
4517 rc = mdd_la_get(env, spobj, spattr);
4521 rc = mdd_la_get(env, tpobj, tpattr);
4525 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4526 spattr, tpattr, attr);
4530 mdd_xattrs_init(&xattrs);
4532 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4533 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4537 /* if user use default value '0' for stripe_count, we need to
4538 * adjust it to '1' to create a 1-stripe directory.
4540 if (lum->lum_stripe_count == 0)
4541 lum->lum_stripe_count = cpu_to_le32(1);
4543 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4544 if (rc && rc != -ENODATA)
4549 if (!lmv_is_sane(lmv))
4550 GOTO(out, rc = -EBADF);
4551 if (lmv_is_migrating(lmv)) {
4552 rc = mdd_migrate_cmd_check(mdd, lmv, lum,
4557 } else if (!S_ISDIR(attr->la_mode)) {
4559 GOTO(out, rc = -EALREADY);
4561 /* update namespace only if @sobj is on MDT where @tpobj is. */
4562 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4563 spec->sp_migrate_nsonly = true;
4565 if (S_ISLNK(attr->la_mode)) {
4566 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4568 GOTO(out, rc = -ENOMEM);
4570 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4573 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4574 mdd2obd_dev(mdd)->obd_name,
4575 PFID(mdd_object_fid(sobj)), rc);
4581 /* linkea needs update upon FID or parent stripe change */
4582 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4583 tname, attr, ldata);
4585 /* update namespace only if @sobj has link on its MDT. */
4586 spec->sp_migrate_nsonly = true;
4590 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4591 * RPCs inside transaction.
4593 if (!spec->sp_migrate_nsonly) {
4594 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, true, true);
4599 handle = mdd_trans_create(env, mdd);
4601 GOTO(out, rc = PTR_ERR(handle));
4603 if (spec->sp_migrate_nsonly)
4604 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4605 tname, attr, spattr, tpattr,
4608 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4609 sname, tname, spattr, tpattr,
4610 attr, &sbuf, ldata, &xattrs, ma,
4611 spec, hint, handle);
4615 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4620 rc = mdd_trans_start(env, mdd, handle);
4624 if (spec->sp_migrate_nsonly)
4625 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4626 attr, spattr, tpattr, ldata, ma,
4629 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4630 tname, spattr, tpattr, attr, &sbuf,
4631 ldata, &xattrs, ma, spec, hint, handle);
4635 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4636 spec->sp_migrate_nsonly ? sobj : tobj,
4637 spobj, spattr, mdd_object_fid(sobj),
4638 tpobj, tpattr, tname, sname,
4642 rc = mdd_trans_stop(env, mdd, rc, handle);
4644 mdd_xattrs_fini(&xattrs);
4648 * -EAGAIN means transaction execution phase detect the layout
4649 * has been changed by others.
4651 if (rc == -EAGAIN && retried++ < MAX_TRANS_RETRIED)
4652 GOTO(retry, retried);
4658 * Migrate directory or file between MDTs.
4660 * \param[in] env execution environment
4661 * \param[in] md_spobj source parent object
4662 * \param[in] md_tpobj target parent object
4663 * \param[in] md_sobj source object
4664 * \param[in] lname file name
4665 * \param[in] md_tobj target object
4666 * \param[in] spec target creation spec
4667 * \param[in] ma used to update \a pobj mtime and ctime
4669 * \retval 0 on success
4670 * \retval -errno on failure
4672 static int mdd_migrate(const struct lu_env *env, struct md_object *md_spobj,
4673 struct md_object *md_tpobj, struct md_object *md_sobj,
4674 struct md_object *md_tobj, const struct lu_name *lname,
4675 struct md_op_spec *spec, struct md_attr *ma)
4677 return mdd_migrate_object(env, md2mdd_obj(md_spobj),
4678 md2mdd_obj(md_tpobj), md2mdd_obj(md_sobj),
4679 md2mdd_obj(md_tobj), lname, lname, spec, ma);
4682 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4683 struct mdd_object *pobj,
4684 struct mdd_object *obj,
4685 struct mdd_object *stripe,
4686 struct lu_attr *attr,
4687 struct mdd_xattrs *xattrs,
4688 struct md_layout_change *mlc,
4689 struct lu_name *lname,
4690 struct thandle *handle)
4694 mlc->mlc_opc = MD_LAYOUT_DETACH;
4695 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4699 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4700 S_IFDIR, dotdot, handle);
4704 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
4705 mdo_declare_xattr_set);
4709 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4713 rc = mdo_declare_attr_set(env, stripe, attr, handle);
4717 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4721 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
4722 attr->la_mode, lname->ln_name, handle);
4726 rc = mdo_declare_ref_del(env, obj, handle);
4730 rc = mdo_declare_ref_del(env, obj, handle);
4734 rc = mdo_declare_destroy(env, obj, handle);
4741 /* transform one-stripe directory to a plain directory */
4742 static int mdd_1sd_collapse(const struct lu_env *env,
4743 struct mdd_object *pobj,
4744 struct mdd_object *obj,
4745 struct mdd_object *stripe,
4746 struct lu_attr *attr,
4747 struct mdd_xattrs *xattrs,
4748 struct md_layout_change *mlc,
4749 struct lu_name *lname,
4750 struct thandle *handle)
4756 /* replace 1-stripe directory with its stripe */
4757 mlc->mlc_opc = MD_LAYOUT_DETACH;
4759 mdd_write_lock(env, obj, DT_SRC_PARENT);
4760 rc = mdo_layout_change(env, obj, mlc, handle);
4761 mdd_write_unlock(env, obj);
4765 mdd_write_lock(env, pobj, DT_SRC_PARENT);
4766 mdd_write_lock(env, obj, DT_SRC_CHILD);
4768 /* insert dotdot to stripe which points to parent */
4769 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
4770 S_IFDIR, dotdot, handle);
4774 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
4779 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
4783 /* don't set nlink from parent */
4784 attr->la_valid &= ~LA_NLINK;
4786 rc = mdo_attr_set(env, stripe, attr, handle);
4790 /* delete dir name from parent */
4791 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4795 /* insert stripe to parent with dir name */
4796 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
4797 attr->la_mode, lname->ln_name, handle);
4801 /* destroy dir obj */
4802 rc = mdo_ref_del(env, obj, handle);
4806 rc = mdo_ref_del(env, obj, handle);
4810 rc = mdo_destroy(env, obj, handle);
4816 mdd_write_unlock(env, obj);
4817 mdd_write_unlock(env, pobj);
4823 * shrink directory stripes after migration/merge
4825 int mdd_dir_layout_shrink(const struct lu_env *env,
4826 struct md_object *md_obj,
4827 struct md_layout_change *mlc)
4829 struct mdd_device *mdd = mdo2mdd(md_obj);
4830 struct mdd_thread_info *info = mdd_env_info(env);
4831 struct mdd_object *obj = md2mdd_obj(md_obj);
4832 struct mdd_object *pobj = NULL;
4833 struct mdd_object *stripe = NULL;
4834 struct lu_attr *attr = &info->mdi_pattr;
4835 struct lu_fid *fid = &info->mdi_fid2;
4836 struct lu_name lname = { NULL };
4837 struct lu_buf lmv_buf = { NULL };
4838 struct mdd_xattrs xattrs;
4839 struct lmv_mds_md_v1 *lmv;
4840 struct lmv_user_md *lmu;
4841 struct thandle *handle;
4846 rc = mdd_la_get(env, obj, attr);
4850 if (!S_ISDIR(attr->la_mode))
4853 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
4857 lmv = lmv_buf.lb_buf;
4858 if (!lmv_is_sane(lmv))
4861 lmu = mlc->mlc_buf.lb_buf;
4863 /* adjust the default value '0' to '1' */
4864 if (lmu->lum_stripe_count == 0)
4865 lmu->lum_stripe_count = cpu_to_le32(1);
4867 /* these were checked in MDT */
4868 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
4869 le32_to_cpu(lmv->lmv_stripe_count));
4870 LASSERT(!lmv_is_splitting(lmv));
4871 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
4873 mdd_xattrs_init(&xattrs);
4875 /* if dir stripe count will be shrunk to 1, it needs to be transformed
4876 * to a plain dir, which will cause FID change and namespace update.
4878 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
4879 struct linkea_data *ldata = &info->mdi_link_data;
4880 char *filename = info->mdi_name;
4882 rc = mdd_links_read(env, obj, ldata);
4886 if (ldata->ld_leh->leh_reccount > 1)
4887 GOTO(out, rc = -EINVAL);
4889 linkea_first_entry(ldata);
4891 GOTO(out, rc = -ENODATA);
4893 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4896 /* Note: lname might miss \0 at the end */
4897 snprintf(filename, sizeof(info->mdi_name), "%.*s",
4898 lname.ln_namelen, lname.ln_name);
4899 lname.ln_name = filename;
4901 pobj = mdd_object_find(env, mdd, fid);
4908 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
4910 stripe = mdd_object_find(env, mdd, fid);
4911 if (IS_ERR(stripe)) {
4912 mdd_object_put(env, pobj);
4914 GOTO(out, rc = PTR_ERR(stripe));
4917 if (!lmv_is_fixed(lmv))
4918 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, false,
4922 handle = mdd_trans_create(env, mdd);
4924 GOTO(out, rc = PTR_ERR(handle));
4926 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4927 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4929 GOTO(stop_trans, rc);
4931 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4932 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
4933 &xattrs, mlc, &lname, handle);
4935 GOTO(stop_trans, rc);
4938 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
4941 GOTO(stop_trans, rc);
4943 rc = mdd_trans_start(env, mdd, handle);
4945 GOTO(stop_trans, rc);
4947 mdd_write_lock(env, obj, DT_SRC_PARENT);
4948 mlc->mlc_opc = MD_LAYOUT_SHRINK;
4949 rc = mdo_layout_change(env, obj, mlc, handle);
4950 mdd_write_unlock(env, obj);
4952 GOTO(stop_trans, rc);
4954 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
4955 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
4956 mlc, &lname, handle);
4958 GOTO(stop_trans, rc);
4961 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
4962 XATTR_NAME_LMV, handle);
4963 GOTO(stop_trans, rc);
4966 rc = mdd_trans_stop(env, mdd, rc, handle);
4968 mdd_xattrs_fini(&xattrs);
4970 mdd_object_put(env, stripe);
4971 mdd_object_put(env, pobj);
4973 lu_buf_free(&lmv_buf);
4977 static int mdd_dir_declare_split_plain(const struct lu_env *env,
4978 struct mdd_device *mdd,
4979 struct mdd_object *pobj,
4980 struct mdd_object *obj,
4981 struct mdd_object *tobj,
4982 struct mdd_xattrs *xattrs,
4983 struct md_layout_change *mlc,
4984 struct dt_allocation_hint *hint,
4985 struct thandle *handle)
4987 struct mdd_thread_info *info = mdd_env_info(env);
4988 const struct lu_name *lname = mlc->mlc_name;
4989 struct lu_attr *la = &info->mdi_la_for_fix;
4990 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
4991 struct linkea_data *ldata = &info->mdi_link_data;
4992 struct lmv_mds_md_v1 *lmv;
4996 mlc->mlc_opc = MD_LAYOUT_DETACH;
4997 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5001 memset(ldata, 0, sizeof(*ldata));
5002 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
5003 lname, 1, 0, ldata);
5007 count = lum->lum_stripe_count;
5008 lum->lum_stripe_count = 0;
5009 /* don't set default LMV since it will become a striped dir */
5010 lum->lum_max_inherit = LMV_INHERIT_NONE;
5011 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5013 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
5014 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
5015 ldata, NULL, NULL, NULL, hint);
5019 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
5022 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
5024 lmv = (typeof(lmv))info->mdi_key;
5025 memset(lmv, 0, sizeof(*lmv));
5026 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
5027 lmv->lmv_stripe_count = cpu_to_le32(1);
5028 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
5029 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
5031 mlc->mlc_opc = MD_LAYOUT_ATTACH;
5032 mlc->mlc_buf.lb_buf = lmv;
5033 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
5034 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5038 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
5039 mdo_declare_xattr_set);
5043 lum->lum_stripe_count = count;
5044 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5045 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5049 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5053 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
5054 S_IFDIR, lname->ln_name, handle);
5058 la->la_valid = LA_CTIME | LA_MTIME;
5059 rc = mdo_declare_attr_set(env, obj, la, handle);
5063 rc = mdo_declare_attr_set(env, pobj, la, handle);
5067 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
5073 * plain directory split:
5074 * 1. create \a tobj as plain directory.
5075 * 2. append \a obj as first stripe of \a tobj.
5076 * 3. migrate xattrs from \a obj to \a tobj.
5077 * 4. split \a tobj to specific stripe count.
5079 static int mdd_dir_split_plain(const struct lu_env *env,
5080 struct mdd_device *mdd,
5081 struct mdd_object *pobj,
5082 struct mdd_object *obj,
5083 struct mdd_object *tobj,
5084 struct mdd_xattrs *xattrs,
5085 struct md_layout_change *mlc,
5086 struct dt_allocation_hint *hint,
5087 struct thandle *handle)
5089 struct mdd_thread_info *info = mdd_env_info(env);
5090 struct lu_attr *pattr = &info->mdi_pattr;
5091 struct lu_attr *la = &info->mdi_la_for_fix;
5092 const struct lu_name *lname = mlc->mlc_name;
5093 struct linkea_data *ldata = &info->mdi_link_data;
5098 /* copy linkea out and set on target later */
5099 rc = mdd_links_read(env, obj, ldata);
5103 mlc->mlc_opc = MD_LAYOUT_DETACH;
5104 rc = mdo_layout_change(env, obj, mlc, handle);
5108 /* don't set nlink from obj */
5109 mlc->mlc_attr->la_valid &= ~LA_NLINK;
5111 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5112 NULL, NULL, NULL, hint, handle, false);
5116 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
5120 rc = mdd_links_write(env, tobj, ldata, handle);
5124 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
5128 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
5129 lname->ln_name, handle);
5133 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
5134 la->la_valid = LA_CTIME | LA_MTIME;
5136 mdd_write_lock(env, obj, DT_SRC_CHILD);
5137 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
5138 mdd_write_unlock(env, obj);
5142 rc = mdd_la_get(env, pobj, pattr);
5146 la->la_valid = LA_CTIME | LA_MTIME;
5148 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5149 rc = mdd_update_time(env, pobj, pattr, la, handle);
5150 mdd_write_unlock(env, pobj);
5154 /* FID changes, record it as CL_MIGRATE */
5155 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
5156 pobj, pattr, mdd_object_fid(obj),
5157 pobj, pattr, lname, lname, handle);
5161 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
5162 struct md_layout_change *mlc)
5164 struct mdd_thread_info *info = mdd_env_info(env);
5165 struct mdd_device *mdd = mdo2mdd(o);
5166 struct mdd_object *obj = md2mdd_obj(o);
5167 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
5168 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
5169 struct dt_allocation_hint *hint = &info->mdi_hint;
5170 bool is_plain = false;
5171 struct mdd_xattrs xattrs;
5172 struct thandle *handle;
5177 LASSERT(S_ISDIR(mdd_object_type(obj)));
5179 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
5185 mdd_xattrs_init(&xattrs);
5187 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true, true);
5189 handle = mdd_trans_create(env, mdd);
5191 GOTO(out, rc = PTR_ERR(handle));
5194 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
5195 &xattrs, mlc, hint, handle);
5197 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5198 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5200 GOTO(stop_trans, rc);
5202 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5206 GOTO(stop_trans, rc);
5208 rc = mdd_trans_start(env, mdd, handle);
5210 GOTO(stop_trans, rc);
5213 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5216 struct lu_buf *buf = &info->mdi_buf[0];
5218 buf->lb_buf = mlc->mlc_spec->u.sp_ea.eadata;
5219 buf->lb_len = mlc->mlc_spec->u.sp_ea.eadatalen;
5221 mdd_write_lock(env, obj, DT_TGT_CHILD);
5222 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LMV,
5223 LU_XATTR_CREATE, handle);
5224 mdd_write_unlock(env, obj);
5226 GOTO(stop_trans, rc);
5228 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5229 XATTR_NAME_LMV, handle);
5232 GOTO(stop_trans, rc);
5237 rc = mdd_trans_stop(env, mdd, rc, handle);
5239 mdd_xattrs_fini(&xattrs);
5244 const struct md_dir_operations mdd_dir_ops = {
5245 .mdo_is_subdir = mdd_is_subdir,
5246 .mdo_lookup = mdd_lookup,
5247 .mdo_create = mdd_create,
5248 .mdo_rename = mdd_rename,
5249 .mdo_link = mdd_link,
5250 .mdo_unlink = mdd_unlink,
5251 .mdo_create_data = mdd_create_data,
5252 .mdo_migrate = mdd_migrate,