1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2011, 2017, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
13 * Lustre Metadata Server (mdd) routines
15 * Author: Wang Di <wangdi@intel.com>
18 #define DEBUG_SUBSYSTEM S_MDS
20 #include <obd_class.h>
21 #include <obd_support.h>
22 #include <lustre_mds.h>
23 #include <lustre_fid.h>
24 #include <lustre_lmv.h>
25 #include <lustre_idmap.h>
26 #include <lustre_crypto.h>
27 #include <uapi/linux/lustre/lgss.h>
29 #include "mdd_internal.h"
31 static const char dot[] = ".";
32 static const char dotdot[] = "..";
34 static struct lu_name lname_dotdot = {
35 .ln_name = (char *) dotdot,
36 .ln_namelen = sizeof(dotdot) - 1,
40 mdd_name_check(const struct lu_env *env, struct mdd_device *m,
41 const struct lu_name *ln)
43 struct mdd_thread_info *info = mdd_env_info(env);
44 bool enc = info->mdi_pattr.la_valid & LA_FLAGS &&
45 info->mdi_pattr.la_flags & LUSTRE_ENCRYPT_FL;
47 if (!lu_name_is_valid(ln))
49 else if (!enc && ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
55 /* Get FID from name and parent */
57 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
58 const struct lu_attr *pattr, const struct lu_name *lname,
59 struct lu_fid *fid, unsigned int may_mask)
61 const char *name = lname->ln_name;
62 const struct dt_key *key = (const struct dt_key *)name;
63 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
64 struct dt_object *dir = mdd_object_child(mdd_obj);
69 if (unlikely(mdd_is_dead_obj(mdd_obj)))
72 if (!mdd_object_exists(mdd_obj))
75 if (mdd_object_remote(mdd_obj)) {
76 CDEBUG(D_INFO, "%s: Object "DFID" located on remote server\n",
77 mdd_obj_dev_name(mdd_obj),
78 PFID(mdd_object_fid(mdd_obj)));
81 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, may_mask,
86 if (likely(dt_try_as_dir(env, dir, true)))
87 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
94 int mdd_lookup(const struct lu_env *env,
95 struct md_object *pobj, const struct lu_name *lname,
96 struct lu_fid *fid, struct md_op_spec *spec)
98 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
103 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
107 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
108 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
112 /** Read the link EA into a temp buffer.
113 * Uses the mdd_thread_info::mdi_link_buf since it is generally large.
114 * A pointer to the buffer is stored in \a ldata::ld_buf.
118 static int __mdd_links_read(const struct lu_env *env,
119 struct mdd_object *mdd_obj,
120 struct linkea_data *ldata)
124 if (!mdd_object_exists(mdd_obj))
127 /* First try a small buf */
128 LASSERT(env != NULL);
129 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_link_buf,
131 if (ldata->ld_buf->lb_buf == NULL)
134 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
136 /* Buf was too small, figure out what we need. */
137 lu_buf_free(ldata->ld_buf);
138 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
142 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
143 if (ldata->ld_buf->lb_buf == NULL)
145 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
149 lu_buf_free(ldata->ld_buf);
150 ldata->ld_buf = NULL;
154 return linkea_init(ldata);
157 int mdd_links_read(const struct lu_env *env,
158 struct mdd_object *mdd_obj,
159 struct linkea_data *ldata)
163 rc = __mdd_links_read(env, mdd_obj, ldata);
165 rc = linkea_init(ldata);
170 static int mdd_links_read_with_rec(const struct lu_env *env,
171 struct mdd_object *mdd_obj,
172 struct linkea_data *ldata)
176 rc = __mdd_links_read(env, mdd_obj, ldata);
178 rc = linkea_init_with_rec(ldata);
184 * Get parent FID of the directory
186 * Read parent FID from linkEA, if that fails, then do lookup
187 * dotdot to get the parent FID.
189 * \param[in] env execution environment
190 * \param[in] obj object from which to find the parent FID
191 * \param[in] attr attribute of the object
192 * \param[out] fid fid to get the parent FID
194 * \retval 0 if getting the parent FID succeeds.
195 * \retval negative errno if getting the parent FID fails.
197 static inline int mdd_parent_fid(const struct lu_env *env,
198 struct mdd_object *obj,
199 const struct lu_attr *attr,
202 struct mdd_thread_info *info = mdd_env_info(env);
203 struct linkea_data ldata = { NULL };
204 struct lu_buf *buf = &info->mdi_link_buf;
205 struct lu_name lname;
210 LASSERTF(S_ISDIR(mdd_object_type(obj)),
211 "%s: FID "DFID" is not a directory type = %o\n",
212 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)),
213 mdd_object_type(obj));
215 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
216 if (buf->lb_buf == NULL)
217 GOTO(lookup, rc = 0);
220 rc = mdd_links_read_with_rec(env, obj, &ldata);
224 /* the obj is not locked, don't cache attributes */
225 mdd_invalidate(env, &obj->mod_obj);
227 LASSERT(ldata.ld_leh != NULL);
228 /* Directory should only have 1 parent */
229 if (ldata.ld_leh->leh_reccount > 1)
232 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
234 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
235 if (likely(fid_is_sane(fid)))
238 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
243 * For root fid use special function, which does not compare version component
244 * of fid. Version component is different for root fids on all MDTs.
246 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
248 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
249 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
253 * return 1: if \a tfid is the fid of the ancestor of \a mo;
255 * otherwise: values < 0, errors.
257 static int mdd_is_parent(const struct lu_env *env,
258 struct mdd_device *mdd,
259 struct mdd_object *mo,
260 const struct lu_attr *attr,
261 const struct lu_fid *tfid)
263 struct mdd_object *mp;
267 LASSERT(!lu_fid_eq(mdd_object_fid(mo), tfid));
268 pfid = &mdd_env_info(env)->mdi_fid;
270 if (mdd_is_root(mdd, mdd_object_fid(mo)))
273 if (mdd_is_root(mdd, tfid))
276 rc = mdd_parent_fid(env, mo, attr, pfid);
281 if (lu_fid_eq(pfid, tfid))
284 if (mdd_is_root(mdd, pfid))
287 mp = mdd_object_find(env, mdd, pfid);
291 if (!mdd_object_exists(mp)) {
292 mdd_object_put(env, mp);
296 rc = mdd_parent_fid(env, mp, attr, pfid);
297 mdd_object_put(env, mp);
306 * No permission check is needed.
308 * returns 1: if fid is ancestor of @mo;
309 * returns 0: if fid is not an ancestor of @mo;
310 * returns < 0: if error
312 static int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
313 const struct lu_fid *fid)
315 struct mdd_device *mdd = mdo2mdd(mo);
316 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
321 if (!mdd_object_exists(md2mdd_obj(mo)))
324 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
327 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
331 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
336 * Check that @dir contains no entries except (possibly) dot and dotdot.
341 * -ENOTDIR not a directory object
342 * -ENOTEMPTY not empty
346 int mdd_dir_is_empty(const struct lu_env *env, struct mdd_object *dir)
349 struct dt_object *obj;
350 const struct dt_it_ops *iops;
355 obj = mdd_object_child(dir);
356 if (!dt_try_as_dir(env, obj, true))
359 iops = &obj->do_index_ops->dio_it;
360 it = iops->init(env, obj, LUDA_64BITHASH);
362 result = iops->get(env, it, (const struct dt_key *)"");
366 for (result = 0, i = 0; result == 0 && i < 3; ++i)
367 result = iops->next(env, it);
370 else if (result == 1)
372 } else if (result == 0)
374 * Huh? Index contains no zero key?
381 result = PTR_ERR(it);
382 /* -ENODEV means no valid stripe */
383 if (result == -ENODEV)
390 * Determine if the target object can be hard linked, and right now it only
391 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
392 * directory nlink count might exceed the maximum link count(see
393 * osd_object_ref_add), so it only check nlink for non-directories.
395 * \param[in] env thread environment
396 * \param[in] obj object being linked to
397 * \param[in] la attributes of \a obj
399 * \retval 0 if \a obj can be hard linked
400 * \retval negative error if \a obj is a directory or has too
403 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
404 const struct lu_attr *la)
406 struct mdd_device *m = mdd_obj2mdd_dev(obj);
412 /* Subdir count limitation can be broken through
413 * (see osd_object_ref_add), so only check non-directory here.
415 if (!S_ISDIR(la->la_mode) &&
416 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
423 * Check whether it may create the cobj under the pobj.
425 * \param[in] env execution environment
426 * \param[in] pobj the parent directory
427 * \param[in] pattr the attribute of the parent directory
428 * \param[in] cobj the child to be created
429 * \param[in] check_perm if check WRITE|EXEC permission for parent
431 * \retval = 0 create the child under this dir is allowed
432 * \retval negative errno create the child under this dir is
435 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
436 const struct lu_attr *pattr, struct mdd_object *cobj,
443 if (cobj && mdd_object_exists(cobj))
446 if (mdd_is_dead_obj(pobj))
450 rc = mdd_permission_internal_locked(env, pobj, pattr,
451 MAY_WRITE | MAY_EXEC,
456 /* Check whether can unlink from the pobj in the case of "cobj == NULL". */
457 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
458 const struct lu_attr *pattr, const struct lu_attr *attr)
464 if (mdd_is_dead_obj(pobj))
467 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
470 rc = mdd_permission_internal_locked(env, pobj, pattr,
471 MAY_WRITE | MAY_EXEC,
476 if (pattr->la_flags & LUSTRE_APPEND_FL)
482 /* pobj == NULL is remote ops case, under such case, pobj's
483 * VTX feature has been checked already, no need check again.
485 static inline int mdd_is_sticky(const struct lu_env *env,
486 struct mdd_object *pobj,
487 const struct lu_attr *pattr,
488 struct mdd_object *cobj,
489 const struct lu_attr *cattr)
491 struct lu_ucred *uc = lu_ucred_assert(env);
494 LASSERT(pattr != NULL);
495 if (!(pattr->la_mode & S_ISVTX) ||
496 (pattr->la_uid == uc->uc_fsuid))
500 LASSERT(cattr != NULL);
501 if (cattr->la_uid == uc->uc_fsuid)
504 return !cap_raised(uc->uc_cap, CAP_FOWNER);
507 static int mdd_may_delete_entry(const struct lu_env *env,
508 struct mdd_object *pobj,
509 const struct lu_attr *pattr,
514 LASSERT(pobj != NULL);
515 if (!mdd_object_exists(pobj))
518 if (mdd_is_dead_obj(pobj))
524 rc = mdd_permission_internal_locked(env, pobj, pattr,
525 MAY_WRITE | MAY_EXEC,
531 if (pattr->la_flags & LUSTRE_APPEND_FL)
538 * Check whether it may delete the cobj from the pobj.
541 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
542 const struct lu_attr *tpattr, struct mdd_object *tobj,
543 const struct lu_attr *tattr, const struct lu_attr *cattr,
544 int check_perm, int check_empty)
551 LASSERT(tpattr != NULL);
552 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
560 if (!mdd_object_exists(tobj))
563 if (mdd_is_dead_obj(tobj))
566 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
569 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
572 /* additional check the rename case */
574 if (S_ISDIR(cattr->la_mode)) {
575 if (!S_ISDIR(tattr->la_mode))
578 if (mdd_is_root(mdo2mdd(&tobj->mod_obj),
579 mdd_object_fid(tobj)))
581 } else if (S_ISDIR(tattr->la_mode))
585 if (S_ISDIR(tattr->la_mode) && check_empty)
586 rc = mdd_dir_is_empty(env, tobj);
592 * Check whether it can create the link file(linked to @src_obj) under
593 * the target directory(@tgt_obj), and src_obj has been locked by
596 * \param[in] env execution environment
597 * \param[in] tgt_obj the target directory
598 * \param[in] tattr attributes of target directory
599 * \param[in] lname the link name
600 * \param[in] src_obj source object for link
601 * \param[in] cattr attributes for source object
603 * \retval = 0 it is allowed to create the link file under tgt_obj
604 * \retval negative error not allowed to create the link file
606 static int mdd_link_sanity_check(const struct lu_env *env,
607 struct mdd_object *tgt_obj,
608 const struct lu_attr *tattr,
609 const struct lu_name *lname,
610 struct mdd_object *src_obj,
611 const struct lu_attr *cattr)
613 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
618 if (!mdd_object_exists(src_obj))
621 if (mdd_is_dead_obj(src_obj))
624 /* Local ops, no lookup before link, check filename length here. */
625 rc = mdd_name_check(env, m, lname);
629 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
632 if (S_ISDIR(mdd_object_type(src_obj)))
635 LASSERT(src_obj != tgt_obj);
636 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
640 rc = __mdd_may_link(env, src_obj, cattr);
645 static int __mdd_index_delete_only(const struct lu_env *env,
646 struct mdd_object *pobj,
647 const char *name, struct thandle *handle)
649 struct dt_object *next = mdd_object_child(pobj);
654 if (dt_try_as_dir(env, next, true))
655 rc = dt_delete(env, next, (struct dt_key *)name, handle);
662 static int __mdd_index_insert_only(const struct lu_env *env,
663 struct mdd_object *pobj,
664 const struct lu_fid *lf, __u32 type,
665 const char *name, struct thandle *handle)
667 struct dt_object *next = mdd_object_child(pobj);
672 if (dt_try_as_dir(env, next, true)) {
673 struct dt_insert_rec *rec = &mdd_env_info(env)->mdi_dt_rec;
676 rec->rec_type = type;
677 rc = dt_insert(env, next, (const struct dt_rec *)rec,
678 (const struct dt_key *)name, handle);
685 /* insert named index, add reference if isdir */
686 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
687 const struct lu_fid *lf, __u32 type,
688 const char *name, struct thandle *handle)
694 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
695 if (rc == 0 && S_ISDIR(type)) {
696 mdd_write_lock(env, pobj, DT_TGT_PARENT);
697 mdo_ref_add(env, pobj, handle);
698 mdd_write_unlock(env, pobj);
704 /* delete named index, drop reference if isdir */
705 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
706 const char *name, int is_dir,
707 struct thandle *handle)
713 rc = __mdd_index_delete_only(env, pobj, name, handle);
714 if (rc == 0 && is_dir) {
715 mdd_write_lock(env, pobj, DT_TGT_PARENT);
716 mdo_ref_del(env, pobj, handle);
717 mdd_write_unlock(env, pobj);
723 static int mdd_llog_record_calc_size(const struct lu_env *env,
724 const struct lu_name *tname,
725 const struct lu_name *sname)
727 const struct lu_ucred *uc = lu_ucred(env);
728 enum changelog_rec_flags clf_flags = CLF_EXTRA_FLAGS;
729 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
732 clf_flags |= CLF_RENAME;
734 if (uc != NULL && uc->uc_jobid[0] != '\0')
735 clf_flags |= CLF_JOBID;
737 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
738 changelog_rec_offset(clf_flags, crfe) +
739 (tname != NULL ? tname->ln_namelen : 0) +
740 (sname != NULL ? 1 + sname->ln_namelen : 0));
743 int mdd_declare_changelog_store(const struct lu_env *env,
744 struct mdd_device *mdd,
745 enum changelog_rec_type type,
746 const struct lu_name *tname,
747 const struct lu_name *sname,
748 struct thandle *handle)
750 struct obd_device *obd = mdd2obd_dev(mdd);
751 struct llog_ctxt *ctxt;
752 struct llog_rec_hdr rec_hdr;
753 struct thandle *llog_th;
756 if (!mdd_changelog_enabled(env, mdd, type))
759 rec_hdr.lrh_len = mdd_llog_record_calc_size(env, tname, sname);
760 rec_hdr.lrh_type = CHANGELOG_REC;
762 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
766 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
768 GOTO(out_put, rc = PTR_ERR(llog_th));
770 rc = llog_declare_add(env, ctxt->loc_handle, &rec_hdr, llog_th);
778 /* The locking here is a bit tricky. For a CHANGELOG_REC the function
779 * drops loghandle->lgh_lock for a performance reasons. All dt_write()
780 * are used own offset, so it is safe.
781 * For other records general function is called and it doesnot drop
782 * a semaphore. The callers are changelog catalog records and initialisation
783 * records. llog_cat_new_log->llog_write_rec->mdd_changelog_write_rec()
785 * Since dt_record_write() could be reordered, rec1|rec2|0x0|rec4 could be
786 * at memory, reader should care about it. When the th is commited it is
787 * impossible to have a hole, since reordered records have the same th.
789 int mdd_changelog_write_rec(const struct lu_env *env,
790 struct llog_handle *loghandle,
791 struct llog_rec_hdr *r,
792 struct llog_cookie *cookie,
793 int idx, struct thandle *th)
796 static struct thandle *saved_th;
798 CDEBUG(D_TRACE, "Adding rec %u type %u to "DFID" flags %x count %d\n",
799 idx, r->lrh_type, PLOGID(&loghandle->lgh_id),
800 loghandle->lgh_hdr->llh_flags, loghandle->lgh_hdr->llh_count);
802 if (r->lrh_type == CHANGELOG_REC) {
803 struct mdd_device *mdd;
804 struct llog_changelog_rec *rec;
806 __u32 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
807 struct dt_object *o = loghandle->lgh_obj;
809 struct lu_buf lgi_buf;
811 left = chunk_size - (loghandle->lgh_cur_offset &
814 mdd = lu2mdd_dev(loghandle->lgh_ctxt->loc_obd->obd_lu_dev);
815 rec = container_of(r, struct llog_changelog_rec, cr_hdr);
817 /* Don't use padding records because it require a slot at header
818 * so previous result of checking llog_is_full(loghandle)
819 * would be invalid, leave zeroes at the end of block.
820 * A reader would care about it.
822 if (left != 0 && left < r->lrh_len)
823 loghandle->lgh_cur_offset += left;
825 offset = loghandle->lgh_cur_offset;
826 loghandle->lgh_cur_offset += r->lrh_len;
827 r->lrh_index = ++loghandle->lgh_last_idx;
829 spin_lock(&mdd->mdd_cl.mc_lock);
830 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
831 spin_unlock(&mdd->mdd_cl.mc_lock);
833 /* drop the loghandle semaphore for parallel writes */
834 up_write(&loghandle->lgh_lock);
836 REC_TAIL(r)->lrt_len = r->lrh_len;
837 REC_TAIL(r)->lrt_index = r->lrh_index;
839 lgi_buf.lb_len = rec->cr_hdr.lrh_len;
840 lgi_buf.lb_buf = rec;
842 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_FAIL_WRITE) &&
843 (rec->cr.cr_index % (cfs_fail_val + 1)) == 0)
846 rc = dt_record_write(env, o, &lgi_buf, &offset, th);
849 CERROR("%s: failed to write changelog record file "DFID" rec idx %u off %llu chnlg idx %llu: rc = %d\n",
850 loghandle->lgh_ctxt->loc_obd->obd_name,
851 PFID(lu_object_fid(&o->do_lu)), r->lrh_index,
852 offset, rec->cr.cr_index, rc);
856 /* mark index at bitmap after successful write, increment count,
857 * and lrt_index with a last index. Use a lgh_hdr_lock for
858 * a synchronization with llog_cancel.
860 spin_lock(&loghandle->lgh_hdr_lock);
861 rc = __test_and_set_bit_le(r->lrh_index,
862 LLOG_HDR_BITMAP(loghandle->lgh_hdr));
864 "%s: index %u already set in llog bitmap "DFID"\n",
865 loghandle->lgh_ctxt->loc_obd->obd_name,
866 r->lrh_index, PLOGID(&loghandle->lgh_id));
867 loghandle->lgh_hdr->llh_count++;
868 if (LLOG_HDR_TAIL(loghandle->lgh_hdr)->lrt_index < r->lrh_index)
869 LLOG_HDR_TAIL(loghandle->lgh_hdr)->lrt_index =
871 spin_unlock(&loghandle->lgh_hdr_lock);
873 if (unlikely(th != saved_th)) {
874 CDEBUG(D_OTHER, "%s: wrote rec %u "DFID" count %d\n",
875 loghandle->lgh_ctxt->loc_obd->obd_name,
876 r->lrh_index, PLOGID(&loghandle->lgh_id),
877 loghandle->lgh_hdr->llh_count);
880 lgi_buf.lb_len = loghandle->lgh_hdr_size;
881 lgi_buf.lb_buf = loghandle->lgh_hdr;
883 CDEBUG(D_TRACE, "%s: writing header "DFID"\n",
884 loghandle->lgh_ctxt->loc_obd->obd_name,
885 PLOGID(&loghandle->lgh_id));
886 /* full header write, it is a local. For a mapped bh
887 * it is memcpy() only. Probably it could be delayed as work.
889 rc = dt_record_write(env, o, &lgi_buf, &offset, th);
891 rc = llog_osd_ops.lop_write_rec(env, loghandle, r,
895 CERROR("%s: failed to write changelog record file "DFID" count %d offset %llu: rc = %d\n",
896 loghandle->lgh_ctxt->loc_obd->obd_name,
897 PLOGID(&loghandle->lgh_id),
898 loghandle->lgh_hdr->llh_count, loghandle->lgh_cur_offset,
905 * Checks that changelog consumes safe amount of space comparing
908 * \param env - current lu_env
909 * \param mdd - current MDD device
910 * \param lgh - changelog catalog llog handle
911 * \param estimate - get exact llog size or estimate it.
915 bool mdd_changelog_is_space_safe(const struct lu_env *env,
916 struct mdd_device *mdd,
917 struct llog_handle *lgh,
920 struct obd_statfs sfs;
921 unsigned long long free_space_limit;
922 unsigned long long llog_size;
925 rc = dt_statfs(env, mdd->mdd_bottom, &sfs);
927 /* check is ignored if OSD is not healthy for any reason */
930 /* if changelog consumes more than 1/4 of available space then start
933 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_ENOSPC))
934 free_space_limit = cfs_fail_val;
936 free_space_limit = (sfs.os_bfree * sfs.os_bsize) >> 2;
938 /* if \estimate parameter is used then calculate llog size from
939 * number of used catalog entries and plain llog maximum size.
940 * Plain llog maximum size if set as 1/64 of FS free space limited
941 * by 128MB as maximum and 2MB as minimum, see llog_cat_new_log()
942 * Estimation helps to avoid full llog processing to get exact size
943 * by llog_cat_size().
946 /* use 1/64 of FS size but keep it between 2MB and 128MB */
947 llog_size = clamp_t(unsigned long long,
948 (sfs.os_blocks * sfs.os_bsize) >> 6,
950 /* llog_cat_free_space() gives free slots, we need occupied,
951 * so subtruct free from total slots minus one for header
953 llog_size *= LLOG_HDR_BITMAP_SIZE(lgh->lgh_hdr) - 1 -
954 llog_cat_free_space(lgh);
956 /* get exact llog size */
957 llog_size = llog_cat_size(env, lgh);
959 CDEBUG(D_HA, "%s:%s changelog size is %lluMB, space limit is %lluMB\n",
960 mdd2obd_dev(mdd)->obd_name, estimate ? " estimated" : "",
961 llog_size >> 20, free_space_limit >> 20);
963 if (llog_size > free_space_limit) {
964 CWARN("%s: changelog uses %lluMB with %lluMB space limit\n",
965 mdd2obd_dev(mdd)->obd_name, llog_size >> 20,
966 free_space_limit >> 20);
974 * Checks if there is enough space in changelog itself and in FS and force
975 * emergency changelog cleanup if needed. It will purge users one by one
976 * from the oldest one while emergency conditions are true.
978 * \param env - current lu_env
979 * \param mdd - current MDD device
980 * \param lgh - changelog catalog llog handle
982 * \retval true if emergency cleanup is needed for changelog
984 static bool mdd_changelog_emrg_cleanup(const struct lu_env *env,
985 struct mdd_device *mdd,
986 struct llog_handle *lgh)
988 unsigned long free_entries = llog_cat_free_space(lgh);
990 /* free space GC is disabled or is in progress already */
991 if (!mdd->mdd_changelog_free_space_gc || mdd->mdd_changelog_emrg_gc)
994 if (free_entries <= mdd->mdd_changelog_min_free_cat_entries) {
995 CWARN("%s: changelog has only %lu free catalog entries\n",
996 mdd2obd_dev(mdd)->obd_name, free_entries);
997 mdd->mdd_changelog_emrg_gc = true;
1001 if (!mdd_changelog_is_space_safe(env, mdd, lgh, true)) {
1002 mdd->mdd_changelog_emrg_gc = true;
1009 static bool mdd_changelog_need_gc(const struct lu_env *env,
1010 struct mdd_device *mdd,
1011 struct llog_handle *lgh)
1013 struct mdd_changelog *mc = &mdd->mdd_cl;
1015 return mdd_changelog_emrg_cleanup(env, mdd, lgh) ||
1016 mdd_changelog_is_too_idle(mdd, mc->mc_minrec, mc->mc_mintime) ||
1017 CFS_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD);
1020 /** Add a changelog entry \a rec to the changelog llog
1023 * \param handle - currently ignored since llogs start their own transaction;
1024 * this will hopefully be fixed in llog rewrite
1027 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
1028 struct llog_changelog_rec *rec, struct thandle *th)
1030 struct obd_device *obd = mdd2obd_dev(mdd);
1031 struct llog_ctxt *ctxt;
1032 struct thandle *llog_th;
1036 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
1037 changelog_rec_varsize(&rec->cr));
1039 /* llog_lvfs_write_rec sets the llog tail len */
1040 rec->cr_hdr.lrh_type = CHANGELOG_REC;
1041 rec->cr.cr_time = cl_time();
1043 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
1047 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
1048 if (IS_ERR(llog_th))
1049 GOTO(out_put, rc = PTR_ERR(llog_th));
1051 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_CHANGELOG_REORDER, cfs_fail_val);
1052 /* nested journal transaction */
1053 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
1055 /* time to recover some space ?? */
1056 if (likely(!mdd->mdd_changelog_gc ||
1057 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
1058 mdd->mdd_changelog_min_gc_interval >=
1059 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
1060 /* save a spin_lock trip */
1063 if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_IDX_PUMP)) {
1064 spin_lock(&mdd->mdd_cl.mc_lock);
1065 mdd->mdd_cl.mc_index += cfs_fail_val;
1066 spin_unlock(&mdd->mdd_cl.mc_lock);
1069 need_gc = mdd_changelog_need_gc(env, mdd, ctxt->loc_handle);
1070 spin_lock(&mdd->mdd_cl.mc_lock);
1071 if (likely(mdd->mdd_changelog_gc &&
1072 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
1073 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
1074 mdd->mdd_changelog_min_gc_interval)) {
1075 if (unlikely(need_gc)) {
1076 CWARN("%s: %s starting changelog garbage collection\n",
1078 CFS_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
1080 /* indicate further kthread run will occur outside
1081 * right after current journal transaction filling has
1084 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
1086 /* next check in mdd_changelog_min_gc_interval anyway */
1087 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
1089 spin_unlock(&mdd->mdd_cl.mc_lock);
1091 llog_ctxt_put(ctxt);
1097 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
1098 const struct lu_fid *sfid,
1099 const struct lu_fid *spfid,
1100 const struct lu_name *sname)
1102 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
1105 LASSERT(sfid != NULL);
1106 LASSERT(spfid != NULL);
1107 LASSERT(sname != NULL);
1109 extsize = sname->ln_namelen + 1;
1111 rnm->cr_sfid = *sfid;
1112 rnm->cr_spfid = *spfid;
1114 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
1115 strscpy(changelog_rec_sname(rec), sname->ln_name, extsize);
1116 rec->cr_namelen += extsize;
1119 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
1121 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
1123 if (jobid == NULL || jobid[0] == '\0')
1126 strscpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
1129 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
1131 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
1133 ef->cr_extra_flags = eflags;
1136 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
1137 __u64 uid, __u64 gid)
1139 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
1141 uidgid->cr_uid = uid;
1142 uidgid->cr_gid = gid;
1145 /* To support the new large NID structure we use all the space in
1146 * struct changelog_ext_nid to store struct lnet_nid.
1148 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
1149 const struct lnet_nid *nid)
1151 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
1153 BUILD_BUG_ON(sizeof(*clnid) < sizeof(*nid));
1154 memcpy(clnid, nid, sizeof(*nid));
1157 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, u32 flags)
1159 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
1161 omd->cr_openflags = flags;
1164 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
1165 const char *xattr_name)
1167 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
1169 strscpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
1173 * Set the parent FID at \a pfid for a namespace change changelog record, using
1174 * XATTR_NAME_LMV and linkEA from the remote object to obtain the correct
1175 * parent FID for striped directories
1177 * \param[in] env - environment
1178 * \param[in] mdd - mdd device
1179 * \param[in] parent - parent object
1180 * \param[in] pattr - parent attribute
1181 * \param[out] pfid - parent fid
1184 * \retval -errno failure
1186 static int mdd_changelog_ns_pfid_set(const struct lu_env *env,
1187 struct mdd_device *mdd,
1188 struct mdd_object *parent,
1189 const struct lu_attr *pattr,
1190 struct lu_fid *pfid)
1194 /* Certain userspace tools might rely on the previous behavior of
1195 * displaying the shard's parent FID, on some changelog records related
1196 * to striped directories, so use that for compatibility if needed
1198 if (mdd->mdd_cl.mc_enable_shard_pfid) {
1199 *pfid = *mdd_object_fid(parent);
1203 if (!fid_is_zero(&parent->mod_striped_pfid)) {
1204 *pfid = parent->mod_striped_pfid;
1208 /* is the parent dir striped? */
1209 rc = mdo_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LMV);
1210 if (rc == -ENODATA) {
1211 *pfid = *mdd_object_fid(parent);
1212 parent->mod_striped_pfid = *pfid;
1219 LASSERT(!mdd_is_root(mdo2mdd(&parent->mod_obj),
1220 mdd_object_fid(parent)));
1222 /* hide shard FID */
1223 rc = mdd_parent_fid(env, parent, pattr, pfid);
1225 parent->mod_striped_pfid = *pfid;
1230 /* The digested form is made of a FID (16 bytes) followed by the second-to-last
1231 * ciphertext block (16 bytes), so a total length of 32 bytes.
1233 /* Must be identical to ll_digest_filename in llite_internal.h */
1234 struct changelog_digest_filename {
1235 struct lu_fid cdf_fid;
1236 char cdf_excerpt[LL_CRYPTO_BLOCK_SIZE];
1240 * Utility function to process filename in changelog
1242 * \param[in] name file name
1243 * \param[in] namelen file name len
1244 * \param[in] fid file FID
1245 * \param[in] enc is object encrypted?
1246 * \param[out]ln pointer to the struct lu_name to hold the real name
1248 * If file is not encrypted, output name is just the file name.
1249 * If file is encrypted, file name needs to be decoded then digested if the name
1250 * is also encrypted. In this case a new buffer is allocated, and ln->ln_name
1251 * needs to be freed by the caller.
1253 * \retval 0, on success
1254 * \retval -ve, on error
1256 static int changelog_name2digest(const char *name, int namelen,
1257 const struct lu_fid *fid,
1258 bool enc, struct lu_name *ln)
1260 struct changelog_digest_filename *digest = NULL;
1261 char *buf = NULL, *bufout = NULL, *p, *q;
1268 ln->ln_namelen = namelen;
1273 /* now we know file is encrypted */
1274 if (strnchr(name, namelen, '=')) {
1275 /* only proceed to critical decode if
1276 * encrypted name contains espace char '='
1278 buf = kmalloc(namelen, GFP_NOFS);
1280 GOTO(out, rc = -ENOMEM);
1282 namelen = critical_decode(name, namelen, buf);
1284 ln->ln_namelen = namelen;
1287 p = (char *)ln->ln_name;
1294 /* len == -1 means we went through the whole decoded name without
1295 * finding any non-printable character, so consider it is not encrypted
1300 /* now we know the name has some non-printable characters */
1301 if (namelen > LL_CRYPTO_BLOCK_SIZE * 2) {
1303 GOTO(out, rc = -EPROTO);
1305 OBD_ALLOC_PTR(digest);
1307 GOTO(out, rc = -ENOMEM);
1309 digest->cdf_fid = *fid;
1310 memcpy(digest->cdf_excerpt,
1311 LLCRYPT_EXTRACT_DIGEST(ln->ln_name, ln->ln_namelen),
1312 LL_CRYPTO_BLOCK_SIZE);
1314 len = sizeof(*digest);
1316 p = (char *)ln->ln_name;
1317 len = ln->ln_namelen;
1320 bufoutlen = BASE64URL_CHARS(len) + 2;
1321 bufout = kmalloc(digest ? bufoutlen + 1 : bufoutlen, GFP_NOFS);
1323 GOTO(free_digest, rc = -ENOMEM);
1327 *q++ = LLCRYPT_DIGESTED_CHAR;
1328 /* beware that gss_base64url_encode adds a trailing space */
1329 gss_base64url_encode(&q, &bufoutlen, (__u8 *)p, len);
1330 if (bufoutlen == -1) {
1334 ln->ln_name = bufout;
1335 ln->ln_namelen = q - bufout - 1;
1339 OBD_FREE_PTR(digest);
1344 /** Store a namespace change changelog record
1345 * If this fails, we must fail the whole transaction; we don't
1346 * want the change to commit without the log entry.
1347 * \param target - mdd_object of change
1348 * \param parent - target parent object
1349 * \param pattr - target parent attribute
1350 * \param sfid - source object fid
1351 * \param sparent - source parent object
1352 * \param spattr - source parent attribute
1353 * \param tname - target name string
1354 * \param sname - source name string
1355 * \param handle - transaction handle
1357 int mdd_changelog_ns_store(const struct lu_env *env,
1358 struct mdd_device *mdd,
1359 enum changelog_rec_type type,
1360 enum changelog_rec_flags clf_flags,
1361 struct mdd_object *target,
1362 struct mdd_object *parent,
1363 const struct lu_attr *pattr,
1364 const struct lu_fid *sfid,
1365 struct mdd_object *sparent,
1366 const struct lu_attr *spattr,
1367 const struct lu_name *tname,
1368 const struct lu_name *sname,
1369 struct thandle *handle)
1371 struct mdd_thread_info *info = mdd_env_info(env);
1372 struct lu_name *ltname = NULL, *lsname = NULL;
1373 const struct lu_ucred *uc = lu_ucred(env);
1374 struct llog_changelog_rec *rec;
1375 __u64 xflags = CLFE_INVALID;
1376 struct lu_fid *tfid = NULL;
1384 if (!mdd_changelog_enabled(env, mdd, type))
1387 LASSERT(S_ISDIR(mdd_object_type(parent)));
1388 LASSERT(tname != NULL);
1389 LASSERT(handle != NULL);
1392 OBD_ALLOC_PTR(ltname);
1394 GOTO(out, rc = -ENOMEM);
1397 enc = info->mdi_tpattr.la_valid & LA_FLAGS &&
1398 info->mdi_tpattr.la_flags & LUSTRE_ENCRYPT_FL;
1399 tfid = (struct lu_fid *)sfid;
1401 enc = info->mdi_pattr.la_valid & LA_FLAGS &&
1402 info->mdi_pattr.la_flags & LUSTRE_ENCRYPT_FL;
1403 tfid = (struct lu_fid *)mdd_object_fid(target);
1405 rc = changelog_name2digest(tname->ln_name, tname->ln_namelen,
1408 GOTO(out_ltname, rc);
1411 OBD_ALLOC_PTR(lsname);
1413 GOTO(out_ltname, rc = -ENOMEM);
1415 enc = info->mdi_pattr.la_valid & LA_FLAGS &&
1416 info->mdi_pattr.la_flags & LUSTRE_ENCRYPT_FL;
1417 rc = changelog_name2digest(sname->ln_name, sname->ln_namelen,
1420 GOTO(out_lsname, rc);
1423 reclen = mdd_llog_record_calc_size(env, ltname, lsname);
1424 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
1425 if (buf->lb_buf == NULL)
1426 GOTO(out_lsname, rc = -ENOMEM);
1429 clf_flags &= CLF_FLAGMASK;
1430 clf_flags |= CLF_EXTRA_FLAGS;
1433 if (uc->uc_jobid[0] != '\0')
1434 clf_flags |= CLF_JOBID;
1435 xflags |= CLFE_UIDGID;
1437 xflags |= CLFE_NID_BE;
1441 clf_flags |= CLF_RENAME;
1443 clf_flags |= CLF_VERSION;
1445 rec->cr.cr_flags = clf_flags;
1447 if (clf_flags & CLF_EXTRA_FLAGS) {
1448 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1449 if (xflags & CLFE_UIDGID)
1450 mdd_changelog_rec_extra_uidgid(&rec->cr,
1451 uc->uc_uid, uc->uc_gid);
1452 if (xflags & CLFE_NID)
1453 mdd_changelog_rec_extra_nid(&rec->cr, &uc->uc_nid);
1456 rec->cr.cr_type = (__u32)type;
1458 rc = mdd_changelog_ns_pfid_set(env, mdd, parent, pattr,
1461 GOTO(out_lsname, rc);
1463 rec->cr.cr_namelen = ltname->ln_namelen;
1464 memcpy(changelog_rec_name(&rec->cr), ltname->ln_name,
1465 ltname->ln_namelen);
1467 if (clf_flags & CLF_RENAME) {
1468 struct lu_fid spfid;
1470 rc = mdd_changelog_ns_pfid_set(env, mdd, sparent, spattr,
1473 GOTO(out_lsname, rc);
1475 mdd_changelog_rec_ext_rename(&rec->cr, sfid, &spfid, lsname);
1478 if (clf_flags & CLF_JOBID)
1479 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1481 if (likely(target != NULL)) {
1482 rec->cr.cr_tfid = *mdd_object_fid(target);
1483 target->mod_cltime = ktime_get();
1485 fid_zero(&rec->cr.cr_tfid);
1488 rc = mdd_changelog_store(env, mdd, rec, handle);
1490 CERROR("%s: cannot store changelog record: type = %d, name = '%s', t = "
1491 DFID", p = "DFID": rc = %d\n",
1492 mdd2obd_dev(mdd)->obd_name, type, ltname->ln_name,
1493 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1494 GOTO(out_lsname, rc = -EFAULT);
1498 if (lsname && lsname->ln_name != sname->ln_name)
1499 kfree(lsname->ln_name);
1500 OBD_FREE_PTR(lsname);
1502 if (ltname && ltname->ln_name != tname->ln_name)
1503 kfree(ltname->ln_name);
1504 OBD_FREE_PTR(ltname);
1509 static int __mdd_links_add(const struct lu_env *env,
1510 struct mdd_object *mdd_obj,
1511 struct linkea_data *ldata,
1512 const struct lu_name *lname,
1513 const struct lu_fid *pfid,
1514 int first, int check)
1516 /* cattr is set in mdd_link */
1517 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1520 if (ldata->ld_leh == NULL) {
1521 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1525 rc = linkea_data_new(ldata,
1526 &mdd_env_info(env)->mdi_link_buf);
1533 rc = linkea_links_find(ldata, lname, pfid);
1534 if (rc && rc != -ENOENT)
1540 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1541 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1545 linkea_add_buf(ldata, lname, tfid, false);
1548 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1549 linkea_add_buf(ldata, lname, pfid, false);
1551 /* For encrypted file, we want to limit number of hard links to what
1552 * linkEA can contain. So ask to return error in case of overflow.
1553 * Currently linkEA stores 4KiB of links, that is 14 NAME_MAX links,
1554 * or 119 16-byte names.
1556 return linkea_add_buf(ldata, lname, pfid,
1557 cattr->la_valid & LA_FLAGS &&
1558 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1561 static int __mdd_links_del(const struct lu_env *env,
1562 struct mdd_object *mdd_obj,
1563 struct linkea_data *ldata,
1564 const struct lu_name *lname,
1565 const struct lu_fid *pfid)
1567 /* cattr is set in mdd_link */
1568 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1571 if (ldata->ld_leh == NULL) {
1572 rc = mdd_links_read(env, mdd_obj, ldata);
1577 rc = linkea_links_find(ldata, lname, pfid);
1581 linkea_del_buf(ldata, lname,
1582 cattr->la_valid & LA_FLAGS &&
1583 cattr->la_flags & LUSTRE_ENCRYPT_FL);
1587 static int mdd_linkea_prepare(const struct lu_env *env,
1588 struct mdd_object *mdd_obj,
1589 const struct lu_fid *oldpfid,
1590 const struct lu_name *oldlname,
1591 const struct lu_fid *newpfid,
1592 const struct lu_name *newlname,
1593 int first, int check,
1594 struct linkea_data *ldata)
1600 if (CFS_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1603 LASSERT(oldpfid != NULL || newpfid != NULL);
1605 if (mdd_obj->mod_flags & DEAD_OBJ)
1606 /* Unnecessary to update linkEA for dead object. */
1609 if (oldpfid != NULL) {
1610 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1612 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1615 /* No changes done. */
1620 /* If renaming, add the new record */
1621 if (newpfid != NULL)
1622 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1628 int mdd_links_rename(const struct lu_env *env,
1629 struct mdd_object *mdd_obj,
1630 const struct lu_fid *oldpfid,
1631 const struct lu_name *oldlname,
1632 const struct lu_fid *newpfid,
1633 const struct lu_name *newlname,
1634 struct thandle *handle,
1635 struct linkea_data *ldata,
1636 int first, int check)
1642 if (ldata == NULL) {
1643 ldata = &mdd_env_info(env)->mdi_link_data;
1644 memset(ldata, 0, sizeof(*ldata));
1645 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1646 newpfid, newlname, first, check, ldata);
1651 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1652 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1658 if (newlname == NULL)
1659 CERROR("link_ea add failed "DFID": rc = %d\n",
1660 PFID(mdd_object_fid(mdd_obj)), rc);
1661 else if (oldpfid == NULL)
1662 CERROR("link_ea add '%.*s' failed "DFID": rc = %d\n",
1663 newlname->ln_namelen, newlname->ln_name,
1664 PFID(mdd_object_fid(mdd_obj)), rc);
1665 else if (newpfid == NULL)
1666 CERROR("link_ea del '%.*s' failed "DFID": rc = %d\n",
1667 oldlname->ln_namelen, oldlname->ln_name,
1668 PFID(mdd_object_fid(mdd_obj)), rc);
1670 CERROR("link_ea rename '%.*s'->'%.*s' failed "DFID": rc = %d\n",
1671 oldlname->ln_namelen, oldlname->ln_name,
1672 newlname->ln_namelen, newlname->ln_name,
1673 PFID(mdd_object_fid(mdd_obj)), rc);
1676 if (is_vmalloc_addr(ldata->ld_buf))
1677 /* if we vmalloced a large buffer drop it */
1678 lu_buf_free(ldata->ld_buf);
1683 static inline int mdd_links_add(const struct lu_env *env,
1684 struct mdd_object *mdd_obj,
1685 const struct lu_fid *pfid,
1686 const struct lu_name *lname,
1687 struct thandle *handle,
1688 struct linkea_data *ldata, int first)
1690 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1691 pfid, lname, handle, ldata, first, 0);
1694 static inline int mdd_links_del(const struct lu_env *env,
1695 struct mdd_object *mdd_obj,
1696 const struct lu_fid *pfid,
1697 const struct lu_name *lname,
1698 struct thandle *handle)
1700 return mdd_links_rename(env, mdd_obj, pfid, lname,
1701 NULL, NULL, handle, NULL, 0, 0);
1704 /** Read the link EA into a temp buffer.
1705 * Uses the name_buf since it is generally large.
1706 * \retval IS_ERR err
1707 * \retval ptr to \a lu_buf (always \a mdi_link_buf)
1709 struct lu_buf *mdd_links_get(const struct lu_env *env,
1710 struct mdd_object *mdd_obj)
1712 struct linkea_data ldata = { NULL };
1715 rc = mdd_links_read(env, mdd_obj, &ldata);
1716 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1719 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1720 struct linkea_data *ldata, struct thandle *handle)
1722 const struct lu_buf *buf;
1725 if (ldata == NULL || ldata->ld_buf == NULL ||
1726 ldata->ld_leh == NULL)
1729 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1733 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1734 ldata->ld_leh->leh_len);
1735 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1736 if (unlikely(rc == -ENOSPC)) {
1737 rc = linkea_overflow_shrink(ldata);
1745 static int mdd_declare_links_add(const struct lu_env *env,
1746 struct mdd_object *mdd_obj,
1747 struct thandle *handle,
1748 struct linkea_data *ldata)
1754 if (ldata != NULL && ldata->ld_leh != NULL) {
1755 ea_len = ldata->ld_leh->leh_len;
1756 linkea = ldata->ld_buf->lb_buf;
1758 ea_len = MAX_LINKEA_SIZE;
1762 rc = mdo_declare_xattr_set(env, mdd_obj,
1763 mdd_buf_get_const(env, linkea, ea_len),
1764 XATTR_NAME_LINK, 0, handle);
1769 static inline int mdd_declare_links_del(const struct lu_env *env,
1770 struct mdd_object *c,
1771 struct thandle *handle)
1775 /* For directory, linkEA will be removed together with the object. */
1776 if (!S_ISDIR(mdd_object_type(c)))
1777 rc = mdd_declare_links_add(env, c, handle, NULL);
1782 static int mdd_declare_link(const struct lu_env *env,
1783 struct mdd_device *mdd,
1784 struct mdd_object *p,
1785 struct mdd_object *c,
1786 const struct lu_name *name,
1787 struct thandle *handle,
1789 struct linkea_data *data)
1791 struct lu_fid tfid = *mdd_object_fid(c);
1794 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1795 tfid.f_oid = cfs_fail_val;
1797 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1798 name->ln_name, handle);
1802 rc = mdo_declare_ref_add(env, c, handle);
1806 la->la_valid = LA_CTIME | LA_MTIME;
1807 rc = mdo_declare_attr_set(env, p, la, handle);
1811 la->la_valid = LA_CTIME;
1812 rc = mdo_declare_attr_set(env, c, la, handle);
1816 rc = mdd_declare_links_add(env, c, handle, data);
1820 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1826 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1827 struct md_object *src_obj, const struct lu_name *lname,
1830 const char *name = lname->ln_name;
1831 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
1832 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1833 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1834 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1835 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1836 struct mdd_device *mdd = mdo2mdd(src_obj);
1837 struct thandle *handle;
1838 struct lu_fid *tfid = &mdd_env_info(env)->mdi_fid2;
1839 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
1844 rc = mdd_la_get(env, mdd_sobj, cattr);
1848 rc = mdd_la_get(env, mdd_tobj, tattr);
1852 /* If we are using project inheritance, we only allow hard link
1853 * creation in our tree when the project IDs are the same;
1854 * otherwise the tree quota mechanism could be circumvented.
1856 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1857 (tattr->la_projid != cattr->la_projid))
1860 handle = mdd_trans_create(env, mdd);
1862 GOTO(out_pending, rc = PTR_ERR(handle));
1864 memset(ldata, 0, sizeof(*ldata));
1866 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1867 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1869 /* Note: even this function will change ldata, but it comes from
1870 * thread_info, which is completely temporary and only seen in
1871 * this function, so we do not need reset ldata once it fails.
1873 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1874 mdd_object_fid(mdd_tobj), lname, 0, 0, ldata);
1878 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1883 rc = mdd_trans_start(env, mdd, handle);
1887 mdd_write_lock(env, mdd_sobj, DT_TGT_CHILD);
1888 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1891 GOTO(out_unlock, rc);
1893 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1894 rc = mdo_ref_add(env, mdd_sobj, handle);
1896 GOTO(out_unlock, rc);
1899 *tfid = *mdd_object_fid(mdd_sobj);
1900 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1901 tfid->f_oid = cfs_fail_val;
1903 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1904 mdd_object_type(mdd_sobj), name, handle);
1906 mdo_ref_del(env, mdd_sobj, handle);
1907 GOTO(out_unlock, rc);
1910 la->la_valid = LA_CTIME | LA_MTIME;
1911 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1913 GOTO(out_unlock, rc);
1915 la->la_valid = LA_CTIME;
1916 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1918 /* Note: The failure of links_add should not cause the
1919 * link failure, so do not check return value.
1921 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_tobj),
1922 lname, handle, ldata, 0);
1926 mdd_write_unlock(env, mdd_sobj);
1928 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1929 mdd_tobj, tattr, NULL,
1930 NULL, NULL, lname, NULL, handle);
1932 rc = mdd_trans_stop(env, mdd, rc, handle);
1933 if (is_vmalloc_addr(ldata->ld_buf))
1934 /* if we vmalloced a large buffer drop it */
1935 lu_buf_free(ldata->ld_buf);
1940 static int mdd_mark_orphan_object(const struct lu_env *env,
1941 struct mdd_object *obj, struct thandle *handle,
1944 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1947 attr->la_valid = LA_FLAGS;
1948 attr->la_flags = LUSTRE_ORPHAN_FL;
1951 rc = mdo_declare_attr_set(env, obj, attr, handle);
1953 rc = mdo_attr_set(env, obj, attr, handle);
1958 static int mdd_declare_finish_unlink(const struct lu_env *env,
1959 struct mdd_object *obj,
1960 struct thandle *handle)
1964 /* Sigh, we do not know if the unlink object will become orphan in
1965 * declare phase, but fortunately the flags here does not matter
1966 * in current declare implementation
1968 rc = mdd_mark_orphan_object(env, obj, handle, true);
1972 rc = mdo_declare_destroy(env, obj, handle);
1976 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1980 return mdd_declare_links_del(env, obj, handle);
1983 /* caller should take a lock before calling */
1984 int mdd_finish_unlink(const struct lu_env *env,
1985 struct mdd_object *obj, struct md_attr *ma,
1986 struct mdd_object *pobj,
1987 const struct lu_name *lname,
1991 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1995 LASSERT(mdd_write_locked(env, obj) != 0);
1997 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1998 /* add new orphan, object will be deleted during mdd_close() */
1999 obj->mod_flags |= DEAD_OBJ;
2000 if (obj->mod_count) {
2001 rc = mdd_orphan_insert(env, obj, th);
2004 "Object "DFID" is inserted into orphan list, open count = %d\n",
2005 PFID(mdd_object_fid(obj)),
2008 CERROR("Object "DFID" fail to be an orphan, open count = %d, maybe cause failed open replay\n",
2009 PFID(mdd_object_fid(obj)),
2012 /* mark object as an orphan here, not before
2013 * mdd_orphan_insert() as racing mdd_la_get() may
2014 * propagate ORPHAN_OBJ causing the asserition
2016 rc = mdd_mark_orphan_object(env, obj, th, false);
2018 rc = mdo_destroy(env, obj, th);
2020 } else if (!is_dir) {
2021 /* old files may not have link ea; ignore errors */
2022 mdd_links_del(env, obj, mdd_object_fid(pobj), lname, th);
2030 * has mdd_write_lock on cobj already, but not on pobj yet
2032 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
2033 const struct lu_attr *pattr,
2034 struct mdd_object *cobj,
2035 const struct lu_attr *cattr)
2041 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
2046 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
2047 struct mdd_object *p, struct mdd_object *c,
2048 const struct lu_name *name, struct md_attr *ma,
2049 struct thandle *handle, int no_name, int is_dir)
2051 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2054 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
2055 if (likely(no_name == 0)) {
2056 rc = mdo_declare_index_delete(env, p, name->ln_name,
2063 rc = mdo_declare_ref_del(env, p, handle);
2069 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2070 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2071 la->la_valid = LA_CTIME | LA_MTIME;
2072 rc = mdo_declare_attr_set(env, p, la, handle);
2077 rc = mdo_declare_ref_del(env, c, handle);
2081 rc = mdo_declare_ref_del(env, c, handle);
2085 la->la_valid = LA_CTIME;
2086 rc = mdo_declare_attr_set(env, c, la, handle);
2090 rc = mdd_declare_finish_unlink(env, c, handle);
2094 /* FIXME: need changelog for remove entry */
2095 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
2103 * test if a file has an HSM archive
2104 * if HSM attributes are not found in ma update them from
2107 static bool mdd_hsm_archive_exists(const struct lu_env *env,
2108 struct mdd_object *obj,
2113 if (!(ma->ma_valid & MA_HSM)) {
2114 /* no HSM MD provided, read xattr */
2115 struct lu_buf *hsm_buf;
2116 const size_t buflen = sizeof(struct hsm_attrs);
2119 hsm_buf = mdd_buf_get(env, NULL, 0);
2120 lu_buf_alloc(hsm_buf, buflen);
2121 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
2122 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
2123 lu_buf_free(hsm_buf);
2127 ma->ma_valid |= MA_HSM;
2129 if (ma->ma_hsm.mh_flags & HS_EXISTS)
2135 * Delete name entry and the object.
2136 * Note: no_name == 1 means it only destory the object, i.e. name_entry
2137 * does not exist for this object, and it could only happen during resending
2138 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
2139 * is also needed in this case(needed by changelog), so we have to add another
2140 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
2141 * the ENOENT failure should be able to be fixed by redo mechanism.
2143 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
2144 struct md_object *cobj, const struct lu_name *lname,
2145 struct md_attr *ma, int no_name)
2147 char *name = (char *)lname->ln_name;
2148 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2149 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2150 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2151 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2152 struct mdd_object *mdd_cobj = NULL;
2153 struct mdd_device *mdd = mdo2mdd(pobj);
2154 struct thandle *handle;
2155 int rc, is_dir = 0, cl_flags = 0;
2159 /* let shutdown to start */
2160 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
2162 /* cobj == NULL means only delete name entry */
2163 if (likely(cobj != NULL)) {
2164 mdd_cobj = md2mdd_obj(cobj);
2165 if (mdd_object_exists(mdd_cobj) == 0)
2169 rc = mdd_la_get(env, mdd_pobj, pattr);
2173 if (likely(mdd_cobj != NULL)) {
2175 rc = mdd_la_get(env, mdd_cobj, cattr);
2179 is_dir = S_ISDIR(cattr->la_mode);
2180 /* search for an existing archive. We should check ahead as the
2181 * object can be destroyed in this transaction
2183 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
2184 cl_flags |= CLF_UNLINK_HSM_EXISTS;
2187 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
2191 handle = mdd_trans_create(env, mdd);
2193 RETURN(PTR_ERR(handle));
2195 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
2196 lname, ma, handle, no_name, is_dir);
2200 rc = mdd_trans_start(env, mdd, handle);
2204 if (likely(mdd_cobj != NULL))
2205 mdd_write_lock(env, mdd_cobj, DT_TGT_CHILD);
2207 if (lname->ln_name[lname->ln_namelen] != '\0') {
2208 /* lname->ln_name is not necessarily NUL terminated */
2209 name = kmalloc(lname->ln_namelen + 1, GFP_NOFS);
2211 GOTO(cleanup, rc = -ENOMEM);
2213 memcpy(name, lname->ln_name, lname->ln_namelen);
2214 name[lname->ln_namelen] = '\0';
2217 if (likely(no_name == 0) && !CFS_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
2218 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
2223 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
2224 CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
2225 GOTO(cleanup, rc = 0);
2227 if (likely(mdd_cobj != NULL)) {
2228 rc = mdo_ref_del(env, mdd_cobj, handle);
2230 __mdd_index_insert_only(env, mdd_pobj,
2231 mdd_object_fid(mdd_cobj),
2232 mdd_object_type(mdd_cobj),
2239 mdo_ref_del(env, mdd_cobj, handle);
2241 /* fetch updated nlink */
2242 rc = mdd_la_get(env, mdd_cobj, cattr);
2247 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2248 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2250 la->la_valid = LA_CTIME | LA_MTIME;
2251 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2255 /* Enough for only unlink the entry */
2256 if (unlikely(mdd_cobj == NULL))
2259 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
2260 /* update ctime of an unlinked file only if it is still opened
2261 * or a link still exists
2263 la->la_valid = LA_CTIME;
2264 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
2269 /* XXX: this transfer to ma will be removed with LOD/OSP */
2270 ma->ma_attr = *cattr;
2271 ma->ma_valid |= MA_INODE;
2272 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
2276 /* fetch updated nlink */
2277 rc = mdd_la_get(env, mdd_cobj, cattr);
2278 /* if object is removed then we can't get its attrs, use last get */
2279 if (rc == -ENOENT) {
2280 cattr->la_nlink = 0;
2284 if (cattr->la_nlink == 0) {
2285 ma->ma_attr = *cattr;
2286 ma->ma_valid |= MA_INODE;
2291 if (name != lname->ln_name)
2294 if (likely(mdd_cobj != NULL))
2295 mdd_write_unlock(env, mdd_cobj);
2298 if (mdd_is_dead_obj(mdd_cobj))
2299 cl_flags |= CLF_UNLINK_LAST;
2301 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
2303 rc = mdd_changelog_ns_store(env, mdd,
2304 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
2305 mdd_cobj, mdd_pobj, pattr, NULL,
2306 NULL, NULL, lname, NULL, handle);
2310 rc = mdd_trans_stop(env, mdd, rc, handle);
2316 * The permission has been checked when obj created, no need check again.
2318 static int mdd_cd_sanity_check(const struct lu_env *env,
2319 struct mdd_object *obj)
2324 if (!obj || mdd_is_dead_obj(obj))
2330 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
2331 struct md_object *cobj,
2332 const struct md_op_spec *spec, struct md_attr *ma)
2334 struct mdd_device *mdd = mdo2mdd(cobj);
2335 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2336 struct mdd_object *son = md2mdd_obj(cobj);
2337 struct thandle *handle;
2338 const struct lu_buf *buf;
2339 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
2340 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
2345 rc = mdd_cd_sanity_check(env, son);
2349 if (!md_should_create(spec->sp_cr_flags))
2353 * there are following use cases for this function:
2354 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
2355 * striping can be specified or not
2358 rc = mdd_la_get(env, son, attr);
2362 /* calling ->ah_make_hint(), used to transfer information from parent */
2363 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2365 handle = mdd_trans_create(env, mdd);
2367 GOTO(out_free, rc = PTR_ERR(handle));
2370 * XXX: Setting the lov ea is not locked but setting the attr is locked?
2371 * Should this be fixed?
2373 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#lo, no_create %u\n",
2374 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
2375 spec->sp_cr_flags, spec->no_create);
2377 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
2378 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2379 spec->u.sp_ea.eadatalen);
2384 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
2385 XATTR_NAME_LOV, 0, handle);
2389 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
2394 rc = mdd_trans_start(env, mdd, handle);
2398 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
2404 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle,
2408 rc = mdd_trans_stop(env, mdd, rc, handle);
2414 static int mdd_declare_object_initialize(const struct lu_env *env,
2415 struct mdd_object *parent,
2416 struct mdd_object *child,
2417 const struct lu_attr *attr,
2418 struct thandle *handle)
2424 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
2425 if (!S_ISDIR(attr->la_mode))
2428 rc = mdo_declare_index_insert(env, child, mdd_object_fid(child),
2429 S_IFDIR, dot, handle);
2433 rc = mdo_declare_ref_add(env, child, handle);
2437 rc = mdo_declare_index_insert(env, child, mdd_object_fid(parent),
2438 S_IFDIR, dotdot, handle);
2443 static int mdd_object_initialize(const struct lu_env *env,
2444 const struct lu_fid *pfid,
2445 struct mdd_object *child,
2446 struct lu_attr *attr,
2447 struct thandle *handle)
2453 if (S_ISDIR(attr->la_mode)) {
2454 /* Add "." and ".." for newly created dir */
2455 mdo_ref_add(env, child, handle);
2456 rc = __mdd_index_insert_only(env, child, mdd_object_fid(child),
2457 S_IFDIR, dot, handle);
2459 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2462 mdo_ref_del(env, child, handle);
2469 * This function checks whether it can create a file/dir under the
2470 * directory(@pobj). The directory(@pobj) is not being locked by
2473 * \param[in] env execution environment
2474 * \param[in] pobj the directory to create files
2475 * \param[in] pattr the attributes of the directory
2476 * \param[in] lname the name of the created file/dir
2477 * \param[in] cattr the attributes of the file/dir
2478 * \param[in] spec create specification
2480 * \retval = 0 it is allowed to create file/dir under
2482 * \retval negative error not allowed to create file/dir
2483 * under the directory
2485 static int mdd_create_sanity_check(const struct lu_env *env,
2486 struct md_object *pobj,
2487 const struct lu_attr *pattr,
2488 const struct lu_name *lname,
2489 struct lu_attr *cattr,
2490 struct md_op_spec *spec)
2492 struct mdd_thread_info *info = mdd_env_info(env);
2493 struct lu_fid *fid = &info->mdi_fid;
2494 struct mdd_object *obj = md2mdd_obj(pobj);
2495 struct mdd_device *m = mdo2mdd(pobj);
2496 bool check_perm = true;
2502 if (mdd_is_dead_obj(obj))
2506 * In some cases this lookup is not needed - we know before if name
2507 * exists or not because MDT performs lookup for it.
2508 * name length check is done in lookup.
2510 if (spec->sp_cr_lookup) {
2512 * Check if the name already exist, though it will be checked in
2513 * _index_insert also, for avoiding rolling back if exists
2516 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2517 MAY_WRITE | MAY_EXEC);
2519 RETURN(rc ? : -EEXIST);
2521 /* Permission is already being checked in mdd_lookup */
2525 if (S_ISDIR(cattr->la_mode) &&
2526 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2527 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2528 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2530 if (!lmv_user_magic_supported(le32_to_cpu(lum->lum_magic)) &&
2531 !(spec->sp_replay &&
2532 lum->lum_magic == cpu_to_le32(LMV_MAGIC_V1))) {
2534 CERROR("%s: invalid lmv_user_md: magic=%x hash=%x stripe_offset=%d stripe_count=%u: rc = %d\n",
2535 mdd2obd_dev(m)->obd_name,
2536 le32_to_cpu(lum->lum_magic),
2537 le32_to_cpu(lum->lum_hash_type),
2538 (int)le32_to_cpu(lum->lum_stripe_offset),
2539 le32_to_cpu(lum->lum_stripe_count), rc);
2544 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2549 if (pattr->la_mode & S_ISGID) {
2550 struct lu_ucred *uc = lu_ucred(env);
2552 cattr->la_gid = pattr->la_gid;
2554 /* Directories are special, and always inherit S_ISGID */
2555 if (S_ISDIR(cattr->la_mode)) {
2556 cattr->la_mode |= S_ISGID;
2557 cattr->la_valid |= LA_MODE;
2558 } else if ((cattr->la_mode & (S_ISGID | 0010))
2559 == (S_ISGID | 0010) &&
2560 !lustre_in_group_p(uc,
2561 (cattr->la_valid & LA_GID) ?
2562 cattr->la_gid : pattr->la_gid) &&
2563 !cap_raised(uc->uc_cap, CAP_FSETID)) {
2564 cattr->la_mode &= ~S_ISGID;
2565 cattr->la_valid |= LA_MODE;
2569 /* Inherit project ID from parent directory */
2570 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2571 cattr->la_projid = pattr->la_projid;
2572 if (S_ISDIR(cattr->la_mode)) {
2573 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2574 cattr->la_valid |= LA_FLAGS;
2576 cattr->la_valid |= LA_PROJID;
2579 rc = mdd_name_check(env, m, lname);
2583 switch (cattr->la_mode & S_IFMT) {
2585 unsigned int symlen = spec->u.sp_symname.ln_namelen + 1;
2587 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2588 RETURN(-ENAMETOOLONG);
2607 static int mdd_declare_create_object(const struct lu_env *env,
2608 struct mdd_device *mdd,
2609 struct mdd_object *p, struct mdd_object *c,
2610 struct lu_attr *attr,
2611 struct thandle *handle,
2612 const struct md_op_spec *spec,
2613 struct lu_buf *def_acl_buf,
2614 struct lu_buf *acl_buf,
2615 struct lu_buf *hsm_buf,
2616 struct dt_allocation_hint *hint)
2618 const struct lu_buf *buf;
2621 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2622 /* ldiskfs OSD needs this information for credit allocation */
2624 hint->dah_acl_len = def_acl_buf->lb_len;
2626 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2631 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2632 if (def_acl_buf && def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2633 /* if dir, then can inherit default ACl */
2634 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2635 XATTR_NAME_ACL_DEFAULT,
2641 if (acl_buf && acl_buf->lb_len > 0) {
2642 rc = mdo_declare_attr_set(env, c, attr, handle);
2646 rc = mdo_declare_xattr_set(env, c, acl_buf,
2647 XATTR_NAME_ACL_ACCESS, 0, handle);
2652 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2656 /* replay case, create LOV EA from client data */
2657 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2658 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2659 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2660 spec->u.sp_ea.eadatalen);
2661 rc = mdo_declare_xattr_set(env, c, buf,
2662 S_ISDIR(attr->la_mode) ?
2663 XATTR_NAME_LMV : XATTR_NAME_LOV,
2664 LU_XATTR_CREATE, handle);
2668 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
2669 rc = mdo_declare_xattr_set(env, c, hsm_buf,
2677 if (S_ISLNK(attr->la_mode)) {
2678 const char *target_name = spec->u.sp_symname.ln_name;
2679 int sym_len = spec->u.sp_symname.ln_namelen;
2680 const struct lu_buf *buf;
2682 buf = mdd_buf_get_const(env, target_name, sym_len);
2683 rc = dt_declare_record_write(env, mdd_object_child(c),
2689 if (spec->sp_cr_file_secctx_name != NULL) {
2690 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2691 spec->sp_cr_file_secctx_size);
2692 rc = mdo_declare_xattr_set(env, c, buf,
2693 spec->sp_cr_file_secctx_name, 0,
2699 if (spec->sp_cr_file_encctx != NULL) {
2700 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2701 spec->sp_cr_file_encctx_size);
2702 rc = mdo_declare_xattr_set(env, c, buf,
2703 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2712 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2713 struct mdd_object *p, struct mdd_object *c,
2714 const struct lu_name *name,
2715 struct lu_attr *attr,
2716 struct thandle *handle,
2717 const struct md_op_spec *spec,
2718 struct linkea_data *ldata,
2719 struct lu_buf *def_acl_buf,
2720 struct lu_buf *acl_buf,
2721 struct lu_buf *hsm_buf,
2722 struct dt_allocation_hint *hint)
2726 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2727 def_acl_buf, acl_buf, hsm_buf, hint);
2731 if (S_ISDIR(attr->la_mode)) {
2732 rc = mdo_declare_ref_add(env, p, handle);
2737 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2738 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2742 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
2743 enum changelog_rec_type type;
2745 rc = mdo_declare_index_insert(env, p, mdd_object_fid(c),
2746 attr->la_mode, name->ln_name,
2751 rc = mdd_declare_links_add(env, c, handle, ldata);
2756 la->la_valid = LA_CTIME | LA_MTIME;
2757 rc = mdo_declare_attr_set(env, p, la, handle);
2761 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2762 S_ISREG(attr->la_mode) ? CL_CREATE :
2763 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2765 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2774 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2775 struct lu_attr *la, struct lu_buf *def_acl_buf,
2776 struct lu_buf *acl_buf)
2782 if (S_ISLNK(la->la_mode)) {
2783 acl_buf->lb_len = 0;
2784 def_acl_buf->lb_len = 0;
2788 mdd_read_lock(env, pobj, DT_TGT_PARENT);
2789 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2790 XATTR_NAME_ACL_DEFAULT);
2791 mdd_read_unlock(env, pobj);
2793 /* ACL buffer size is not enough, need realloc */
2794 if (rc > acl_buf->lb_len)
2797 /* If there are default ACL, fix mode/ACL by default ACL */
2798 def_acl_buf->lb_len = rc;
2799 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2800 acl_buf->lb_len = rc;
2801 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2804 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2805 /* If there are no default ACL, fix mode by mask */
2806 struct lu_ucred *uc = lu_ucred(env);
2808 /* The create triggered by MDT internal events, such as
2809 * LFSCK reset, will not contain valid "uc".
2811 if (unlikely(uc != NULL))
2812 la->la_mode &= ~uc->uc_umask;
2814 acl_buf->lb_len = 0;
2815 def_acl_buf->lb_len = 0;
2822 * Create a metadata object and initialize it, set acl, xattr.
2824 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2825 struct mdd_object *son, struct lu_attr *attr,
2826 struct md_op_spec *spec, struct lu_buf *acl_buf,
2827 struct lu_buf *def_acl_buf,
2828 struct lu_buf *hsm_buf,
2829 struct dt_allocation_hint *hint,
2830 struct thandle *handle, bool initial_create)
2832 const struct lu_fid *son_fid = mdd_object_fid(son);
2833 const struct lu_ucred *uc = lu_ucred(env);
2834 const char *jobid = uc->uc_jobid;
2835 const struct lu_buf *buf;
2839 mdd_write_lock(env, son, DT_TGT_CHILD);
2840 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2845 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2846 * created in declare phase, they also needs to be added to master
2847 * object as sub-directory entry. So it has to initialize the master
2848 * object, then set dir striped EA.(in mdo_xattr_set)
2850 rc = mdd_object_initialize(env, mdd_object_fid(pobj), son, attr,
2853 GOTO(err_destroy, rc);
2856 * in case of replay we just set LOVEA provided by the client
2857 * XXX: I think it would be interesting to try "old" way where
2858 * MDT calls this xattr_set(LOV) in a different transaction.
2859 * probably this way we code can be made better.
2862 /* During creation, there are only a few cases we need do xattr_set to
2864 * 1. regular file: see comments above.
2865 * 2. dir: inherit default striping or pool settings from parent.
2866 * 3. create striped directory with provided stripeEA.
2867 * 4. create striped directory because inherit default layout from the
2870 if (spec->no_create ||
2871 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2872 S_ISDIR(attr->la_mode)) {
2873 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2874 spec->u.sp_ea.eadatalen);
2875 rc = mdo_xattr_set(env, son, buf,
2876 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2878 LU_XATTR_CREATE, handle);
2880 GOTO(err_destroy, rc);
2883 if (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_PCC) {
2886 memset(&mh, 0, sizeof(mh));
2887 mh.mh_flags = HS_EXISTS | HS_ARCHIVED | HS_RELEASED;
2888 mh.mh_arch_id = spec->sp_archive_id;
2889 lustre_hsm2buf(hsm_buf->lb_buf, &mh);
2890 rc = mdo_xattr_set(env, son, hsm_buf, XATTR_NAME_HSM,
2893 GOTO(err_destroy, rc);
2896 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
2897 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2898 S_ISDIR(attr->la_mode)) {
2899 /* set default acl */
2900 rc = mdo_xattr_set(env, son, def_acl_buf,
2901 XATTR_NAME_ACL_DEFAULT, 0,
2904 GOTO(err_destroy, rc);
2906 /* set its own acl */
2907 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2908 rc = mdo_xattr_set(env, son, acl_buf,
2909 XATTR_NAME_ACL_ACCESS,
2912 GOTO(err_destroy, rc);
2916 if (S_ISLNK(attr->la_mode)) {
2917 struct dt_object *dt = mdd_object_child(son);
2918 const char *target_name = spec->u.sp_symname.ln_name;
2919 int sym_len = spec->u.sp_symname.ln_namelen;
2922 buf = mdd_buf_get_const(env, target_name, sym_len);
2923 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle);
2927 GOTO(err_initlized, rc = -EFAULT);
2930 if (initial_create && spec->sp_cr_file_secctx_name != NULL) {
2931 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2932 spec->sp_cr_file_secctx_size);
2933 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2936 GOTO(err_initlized, rc);
2939 if (spec->sp_cr_file_encctx != NULL) {
2940 buf = mdd_buf_get_const(env, spec->sp_cr_file_encctx,
2941 spec->sp_cr_file_encctx_size);
2942 rc = mdo_xattr_set(env, son, buf,
2943 LL_XATTR_NAME_ENCRYPTION_CONTEXT, 0,
2946 GOTO(err_initlized, rc);
2949 if (initial_create &&
2950 spec->sp_cr_job_xattr[0] != '\0' &&
2952 (S_ISREG(attr->la_mode) || S_ISDIR(attr->la_mode))) {
2953 jobid_len = strnlen(jobid, LUSTRE_JOBID_SIZE);
2954 buf = mdd_buf_get_const(env, jobid, jobid_len);
2956 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_job_xattr, 0,
2958 /* this xattr is nonessential, so ignore errors. */
2961 DFID" failed to set xattr '%s': rc = %d\n",
2962 PFID(son_fid), spec->sp_cr_job_xattr, rc);
2968 if (unlikely(rc != 0)) {
2971 if (S_ISDIR(attr->la_mode)) {
2972 /* Drop the reference, no need to delete "."/"..",
2973 * because the object to be destroyed directly.
2975 rc2 = mdo_ref_del(env, son, handle);
2979 rc2 = mdo_ref_del(env, son, handle);
2983 mdo_destroy(env, son, handle);
2986 mdd_write_unlock(env, son);
2990 static int mdd_index_delete(const struct lu_env *env,
2991 struct mdd_object *mdd_pobj,
2992 struct lu_attr *cattr,
2993 const struct lu_name *lname)
2995 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2996 struct thandle *handle;
3001 handle = mdd_trans_create(env, mdd);
3003 RETURN(PTR_ERR(handle));
3005 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
3010 if (S_ISDIR(cattr->la_mode)) {
3011 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3016 /* Since this will only be used in the error handler path,
3017 * Let's set the thandle to be local and not mess the transno
3019 handle->th_local = 1;
3020 rc = mdd_trans_start(env, mdd, handle);
3024 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
3025 S_ISDIR(cattr->la_mode), handle);
3029 rc = mdd_trans_stop(env, mdd, rc, handle);
3035 * Create object and insert it into namespace.
3037 * Two operations have to be performed:
3039 * - an allocation of a new object (->do_create()), and
3040 * - an insertion into a parent index (->dio_insert()).
3042 * Due to locking, operation order is not important, when both are
3043 * successful, *but* error handling cases are quite different:
3045 * - if insertion is done first, and following object creation fails,
3046 * insertion has to be rolled back, but this operation might fail
3047 * also leaving us with dangling index entry.
3049 * - if creation is done first, is has to be undone if insertion fails,
3050 * leaving us with leaked space, which is not good but not fatal.
3052 * It seems that creation-first is simplest solution, but it is sub-optimal
3058 * case, because second mkdir is bound to create object, only to
3059 * destroy it immediately.
3061 * To avoid this follow local file systems that do double lookup:
3063 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
3064 * 1. create (mdd_create_object_internal())
3065 * 2. insert (__mdd_index_insert(), lookup again)
3067 * \param[in] pobj parent object
3068 * \param[in] lname name of child being created
3069 * \param[in,out] child child object being created
3070 * \param[in] spec additional create parameters
3071 * \param[in] ma attributes for new child object
3073 * \retval 0 on success
3074 * \retval negative errno on failure
3076 int mdd_create(const struct lu_env *env, struct md_object *pobj,
3077 const struct lu_name *lname, struct md_object *child,
3078 struct md_op_spec *spec, struct md_attr *ma)
3080 struct mdd_thread_info *info = mdd_env_info(env);
3081 struct lu_attr *la = &info->mdi_la_for_fix;
3082 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3083 struct mdd_object *son = md2mdd_obj(child);
3084 struct mdd_device *mdd = mdo2mdd(pobj);
3085 struct lu_attr *attr = &ma->ma_attr;
3086 struct thandle *handle;
3087 struct lu_attr *pattr = &info->mdi_pattr;
3088 struct lu_buf acl_buf;
3089 struct lu_buf def_acl_buf;
3090 struct lu_buf hsm_buf;
3091 struct linkea_data *ldata = &info->mdi_link_data;
3092 const char *name = lname->ln_name;
3093 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
3094 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
3095 bool name_inserted = false;
3100 rc = mdd_la_get(env, mdd_pobj, pattr);
3104 /* Sanity checks before big job. */
3105 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
3106 if (unlikely(rc == -EEXIST && S_ISDIR(attr->la_mode) &&
3107 spec->sp_replay && mdd_object_remote(mdd_pobj)))
3108 /* if it's replay by client request, and name is found in
3109 * parent directory on remote MDT, it means mkdir was partially
3110 * executed: name was successfully added, but target not.
3112 name_inserted = true;
3116 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
3117 GOTO(out_free, rc = -EINPROGRESS);
3119 handle = mdd_trans_create(env, mdd);
3121 GOTO(out_free, rc = PTR_ERR(handle));
3124 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
3125 if (!acl_buf.lb_buf)
3126 GOTO(out_stop, rc = -ENOMEM);
3128 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
3129 if (!def_acl_buf.lb_buf)
3130 GOTO(out_stop, rc = -ENOMEM);
3132 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
3133 if (unlikely(rc == -ERANGE &&
3134 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
3135 /* use maximum-sized xattr buffer for too-big default ACL */
3136 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
3138 goto use_bigger_buffer;
3143 /* adjust stripe count to 0 for 'lfs mkdir -c 1 ...' to avoid creating
3144 * 1-stripe directory, MDS_OPEN_DEFAULT_LMV means ea is default LMV.
3146 if (unlikely(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata &&
3147 !(spec->sp_cr_flags & MDS_OPEN_DEFAULT_LMV))) {
3148 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
3150 /* migrate may create 1-stripe directory, adjust stripe count
3151 * before lod_ah_init().
3153 if (lmu && lmu->lum_magic == cpu_to_le32(LMV_USER_MAGIC) &&
3154 lmu->lum_stripe_count == cpu_to_le32(1))
3155 lmu->lum_stripe_count = 0;
3158 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
3160 memset(ldata, 0, sizeof(*ldata));
3161 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
3162 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
3165 rc = mdd_linkea_prepare(env, son, NULL, NULL,
3166 &tfid, lname, 1, 0, ldata);
3168 rc = mdd_linkea_prepare(env, son, NULL, NULL,
3169 mdd_object_fid(mdd_pobj),
3170 lname, 1, 0, ldata);
3173 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
3174 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
3176 memset(&hsm_buf, 0, sizeof(hsm_buf));
3177 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
3178 if (hsm_buf.lb_buf == NULL)
3179 GOTO(out_stop, rc = -ENOMEM);
3182 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
3183 handle, spec, ldata, &def_acl_buf, &acl_buf,
3188 rc = mdd_trans_start(env, mdd, handle);
3192 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
3193 &def_acl_buf, &hsm_buf, hint, handle, true);
3197 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
3198 mdd_write_lock(env, son, DT_TGT_CHILD);
3199 son->mod_flags |= VOLATILE_OBJ;
3200 rc = mdd_orphan_insert(env, son, handle);
3201 GOTO(out_volatile, rc);
3203 if (likely(!name_inserted)) {
3204 rc = __mdd_index_insert(env, mdd_pobj,
3205 mdd_object_fid(son),
3206 attr->la_mode, name, handle);
3208 GOTO(err_created, rc);
3211 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
3214 /* update parent directory mtime/ctime */
3216 la->la_valid = LA_CTIME | LA_MTIME;
3217 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
3219 GOTO(err_insert, rc);
3225 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
3226 rc2 = mdd_orphan_delete(env, son, handle);
3228 rc2 = __mdd_index_delete(env, mdd_pobj, name,
3229 S_ISDIR(attr->la_mode),
3235 mdd_write_lock(env, son, DT_TGT_CHILD);
3236 if (S_ISDIR(attr->la_mode)) {
3237 /* Drop the reference, no need to delete "."/"..",
3238 * because the object is to be destroyed directly.
3240 rc2 = mdo_ref_del(env, son, handle);
3242 mdd_write_unlock(env, son);
3247 /* For volatile files drop one link immediately, since there is
3248 * no filename in the namespace, and save any error returned.
3250 rc2 = mdo_ref_del(env, son, handle);
3252 mdd_write_unlock(env, son);
3253 if (unlikely(rc == 0))
3258 /* Don't destroy the volatile object on success */
3259 if (likely(rc != 0))
3260 mdo_destroy(env, son, handle);
3261 mdd_write_unlock(env, son);
3264 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
3265 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
3266 rc = mdd_changelog_ns_store(env, mdd,
3267 S_ISDIR(attr->la_mode) ? CL_MKDIR :
3268 S_ISREG(attr->la_mode) ? CL_CREATE :
3269 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
3270 0, son, mdd_pobj, pattr, NULL, NULL, NULL,
3271 lname, NULL, handle);
3273 rc2 = mdd_trans_stop(env, mdd, rc, handle);
3275 /* If creation fails, it is most likely due to the remote update
3276 * failure, because local transaction will mostly succeed at
3277 * this stage. There is no easy way to rollback all of previous
3278 * updates, so let's remove the object from namespace, and
3279 * LFSCK should handle the orphan object.
3281 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
3282 mdd_index_delete(env, mdd_pobj, attr, lname);
3286 if (is_vmalloc_addr(ldata->ld_buf))
3287 /* if we vmalloced a large buffer drop it */
3288 lu_buf_free(ldata->ld_buf);
3290 if (spec->sp_cr_flags & MDS_OPEN_PCC)
3291 lu_buf_free(&hsm_buf);
3293 /* The child object shouldn't be cached anymore */
3295 set_bit(LU_OBJECT_HEARD_BANSHEE,
3296 &child->mo_lu.lo_header->loh_flags);
3300 /* has not mdd_write{read}_lock on any obj yet. */
3301 static int mdd_rename_sanity_check(const struct lu_env *env,
3302 struct mdd_object *src_pobj,
3303 const struct lu_attr *spattr,
3304 struct mdd_object *tgt_pobj,
3305 const struct lu_attr *tpattr,
3306 struct mdd_object *sobj,
3307 const struct lu_attr *sattr,
3308 struct mdd_object *tobj,
3309 const struct lu_attr *tattr)
3315 /* XXX: when get here, sobj must NOT be NULL,
3316 * the other case has been processed in cld_rename
3317 * before mdd_rename and enable MDS_PERM_BYPASS.
3322 * If we are using project inheritance, we only allow renames
3323 * into our tree when the project IDs are the same; otherwise
3324 * tree quota mechanism would be circumvented.
3326 if ((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
3327 tpattr->la_projid != sattr->la_projid && S_ISDIR(sattr->la_mode))
3330 /* we prevent an encrypted file from being renamed
3331 * into an unencrypted dir
3333 if ((spattr->la_valid & LA_FLAGS &&
3334 spattr->la_flags & LUSTRE_ENCRYPT_FL) &&
3335 !(tpattr->la_valid & LA_FLAGS &&
3336 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
3339 rc = mdd_may_delete(env, src_pobj, spattr, sobj, sattr, NULL, 1, 0);
3343 /* XXX: when get here, "tobj == NULL" means tobj must
3344 * NOT exist (neither on remote MDS, such case has been
3345 * processed in cld_rename before mdd_rename and enable
3347 * So check may_create, but not check may_unlink.
3350 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
3351 (src_pobj != tgt_pobj));
3353 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, sattr,
3354 (src_pobj != tgt_pobj), 1);
3356 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(sattr->la_mode))
3357 rc = __mdd_may_link(env, tgt_pobj, tpattr);
3363 int mdd_declare_rename(const struct lu_env *env, struct mdd_device *mdd,
3364 struct mdd_object *mdd_spobj, struct mdd_object *mdd_tpobj,
3365 struct mdd_object *mdd_sobj, struct mdd_object *mdd_tobj,
3366 const struct lu_name *sname, const struct lu_name *tname,
3367 struct md_attr *ma, struct linkea_data *ldata,
3368 bool change_projid, struct thandle *handle)
3370 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3373 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3374 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3380 /* name from source dir */
3381 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
3385 /* .. from source child */
3386 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3387 /* source child can be directory, count by source dir's nlink */
3388 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
3391 if (mdd_spobj != mdd_tpobj) {
3392 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
3397 rc = mdo_declare_index_insert(env, mdd_sobj,
3398 mdd_object_fid(mdd_tpobj),
3399 S_IFDIR, dotdot, handle);
3404 /* new target child can be dir, counted by target dir's nlink */
3405 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
3410 la->la_valid = LA_CTIME | LA_MTIME;
3411 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
3415 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
3419 la->la_valid = LA_CTIME;
3421 la->la_valid |= LA_PROJID;
3422 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3426 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
3431 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
3432 mdd_object_type(mdd_sobj),
3433 tname->ln_name, handle);
3437 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
3438 /* delete target child in target parent directory */
3439 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
3444 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3448 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
3449 /* target child can be directory,
3450 * delete "." reference in target child directory
3452 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3456 /* delete ".." reference in target parent directory */
3457 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
3462 la->la_valid = LA_CTIME;
3463 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
3467 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
3472 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
3481 int mdd_migrate_object(const struct lu_env *env, struct mdd_object *spobj,
3482 struct mdd_object *tpobj, struct mdd_object *sobj,
3483 struct mdd_object *tobj, const struct lu_name *sname,
3484 const struct lu_name *tname, struct md_op_spec *spec,
3485 struct md_attr *ma);
3487 /* src object can be remote that is why we use only fid and type of object */
3488 static int mdd_rename(const struct lu_env *env, struct md_object *src_pobj,
3489 struct md_object *tgt_pobj, const struct lu_fid *lf,
3490 const struct lu_name *lsname, struct md_object *tobj,
3491 const struct lu_name *ltname, struct md_attr *ma)
3493 const char *sname = lsname->ln_name;
3494 const char *tname = ltname->ln_name;
3495 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3496 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3497 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj); /* target parent */
3498 struct mdd_object *mdd_sobj = NULL; /* source object */
3499 struct mdd_object *mdd_tobj = NULL; /* (possible) target object */
3500 struct lu_attr *sattr = MDD_ENV_VAR(env, cattr);
3501 struct lu_attr *spattr = MDD_ENV_VAR(env, pattr);
3502 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3503 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3504 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3505 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3506 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3507 struct mdd_device *mdd = mdo2mdd(src_pobj);
3508 struct thandle *handle;
3511 bool tobj_locked = 0;
3512 bool change_projid = false;
3513 unsigned int cl_flags = 0;
3518 /* let unlink to complete and commit */
3519 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3522 mdd_tobj = md2mdd_obj(tobj);
3524 mdd_sobj = mdd_object_find(env, mdd, lf);
3525 if (IS_ERR(mdd_sobj))
3526 RETURN(PTR_ERR(mdd_sobj));
3528 rc = mdd_la_get(env, mdd_sobj, sattr);
3530 GOTO(out_pending, rc);
3532 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3533 * hard links, and target doesn't exist.
3535 if (mdd_object_remote(mdd_sobj) && S_ISLNK(sattr->la_mode) &&
3536 sattr->la_nlink == 1 && !tobj) {
3537 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3538 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3541 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3544 GOTO(out_pending, rc);
3546 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3547 if (IS_ERR(mdd_tobj))
3548 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3550 memset(spec, 0, sizeof(*spec));
3551 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3552 mdd_tobj, lsname, ltname, spec, ma);
3553 mdd_object_put(env, mdd_tobj);
3554 GOTO(out_pending, rc);
3557 rc = mdd_la_get(env, mdd_spobj, spattr);
3559 GOTO(out_pending, rc);
3562 rc = mdd_la_get(env, mdd_tobj, tattr);
3564 GOTO(out_pending, rc);
3565 /* search for an existing archive. we should check ahead as the
3566 * object can be destroyed in this transaction
3568 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3569 cl_flags |= CLF_RENAME_LAST_EXISTS;
3572 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3574 GOTO(out_pending, rc);
3576 rc = mdd_rename_sanity_check(env, mdd_spobj, spattr, mdd_tpobj, tpattr,
3577 mdd_sobj, sattr, mdd_tobj, tattr);
3579 GOTO(out_pending, rc);
3581 rc = mdd_name_check(env, mdd, ltname);
3583 GOTO(out_pending, rc);
3585 handle = mdd_trans_create(env, mdd);
3587 GOTO(out_pending, rc = PTR_ERR(handle));
3589 memset(ldata, 0, sizeof(*ldata));
3590 rc = mdd_linkea_prepare(env, mdd_sobj, spobj_fid, lsname, tpobj_fid,
3591 ltname, 1, 0, ldata);
3595 if (tpattr->la_projid != sattr->la_projid &&
3596 tpattr->la_flags & LUSTRE_PROJINHERIT_FL)
3597 change_projid = true;
3599 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3600 mdd_tobj, lsname, ltname, ma, ldata,
3601 change_projid, handle);
3605 rc = mdd_trans_start(env, mdd, handle);
3609 is_dir = S_ISDIR(sattr->la_mode);
3611 /* Remove source name from source directory */
3612 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3616 /* "mv dir1 dir2" needs "dir1/.." link update */
3617 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3618 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3620 GOTO(fixup_spobj2, rc);
3622 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3625 GOTO(fixup_spobj, rc);
3628 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3629 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3631 /* tname might been renamed to something else */
3632 GOTO(fixup_spobj, rc);
3635 /* Insert new fid with target name into target dir */
3636 rc = __mdd_index_insert(env, mdd_tpobj, lf, sattr->la_mode,
3639 GOTO(fixup_tpobj, rc);
3641 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3642 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3644 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3645 la->la_valid = LA_CTIME;
3646 if (change_projid) {
3647 /* mdd_update_time honors other valid flags except TIME ones */
3648 la->la_valid |= LA_PROJID;
3649 la->la_projid = tpattr->la_projid;
3651 rc = mdd_update_time(env, mdd_sobj, sattr, la, handle);
3653 GOTO(fixup_tpobj, rc);
3655 /* Update the linkEA for the source object */
3656 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3657 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3658 lsname, mdd_object_fid(mdd_tpobj), ltname,
3659 handle, ldata, 0, 0);
3661 /* Old files might not have EA entry */
3662 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3663 lsname, handle, NULL, 0);
3664 mdd_write_unlock(env, mdd_sobj);
3665 /* We don't fail the transaction if the link ea can't be
3666 * updated -- fid2path will use alternate lookup method.
3670 /* Remove old target object
3671 * For tobj is remote case cmm layer has processed
3672 * and set tobj to NULL then. So when tobj is NOT NULL,
3673 * it must be local one.
3675 if (tobj && mdd_object_exists(mdd_tobj)) {
3676 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3678 if (mdd_is_dead_obj(mdd_tobj)) {
3679 /* should not be dead, something is wrong */
3681 CERROR("%s: something bad, dead tobj "DFID": rc = %d\n",
3682 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3685 mdo_ref_del(env, mdd_tobj, handle);
3687 /* Remove dot reference. */
3688 if (S_ISDIR(tattr->la_mode))
3689 mdo_ref_del(env, mdd_tobj, handle);
3692 /* fetch updated nlink */
3693 rc = mdd_la_get(env, mdd_tobj, tattr);
3695 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3696 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3697 GOTO(fixup_tpobj, rc);
3700 la->la_valid = LA_CTIME;
3701 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3703 CERROR("%s: failed set ctime of tobj "DFID": rc = %d\n",
3704 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3705 GOTO(fixup_tpobj, rc);
3708 /* XXX: this transfer to ma will be removed with LOD/OSP */
3709 ma->ma_attr = *tattr;
3710 ma->ma_valid |= MA_INODE;
3711 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3714 CERROR("%s: failed to unlink tobj "DFID": rc = %d\n",
3715 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3716 GOTO(fixup_tpobj, rc);
3719 /* fetch updated nlink */
3720 rc = mdd_la_get(env, mdd_tobj, tattr);
3721 if (rc == -ENOENT) {
3722 /* object removed? return the latest known attributes */
3723 tattr->la_nlink = 0;
3726 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3727 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3728 GOTO(fixup_tpobj, rc);
3730 /* XXX: this transfer to ma will be removed with LOD/OSP */
3731 ma->ma_attr = *tattr;
3732 ma->ma_valid |= MA_INODE;
3734 if (tattr->la_nlink == 0)
3735 cl_flags |= CLF_RENAME_LAST;
3737 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3740 la->la_valid = LA_CTIME | LA_MTIME;
3741 rc = mdd_update_time(env, mdd_spobj, spattr, la, handle);
3743 GOTO(fixup_tpobj, rc);
3745 if (mdd_spobj != mdd_tpobj) {
3746 la->la_valid = LA_CTIME | LA_MTIME;
3747 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3749 GOTO(fixup_tpobj, rc);
3756 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3758 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3759 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc2);
3761 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3762 !mdd_is_dead_obj(mdd_tobj)) {
3764 mdo_ref_add(env, mdd_tobj, handle);
3766 mdo_ref_add(env, mdd_tobj, handle);
3769 rc2 = __mdd_index_insert(env, mdd_tpobj,
3770 mdd_object_fid(mdd_tobj),
3771 mdd_object_type(mdd_tobj),
3774 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3775 mdd2obd_dev(mdd)->obd_name,
3776 PFID(tpobj_fid), rc2);
3781 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3782 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3784 CWARN("%s: spobj "DFID" dotdot delete error: rc = %d\n",
3785 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3788 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3791 CWARN("%s: spobj "DFID" dotdot insert error: rc = %d\n",
3792 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3797 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3798 mdd_object_type(mdd_sobj), sname,
3801 CWARN("%s: spobj "DFID" fix error: rc = %d\n",
3802 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3807 mdd_write_unlock(env, mdd_tobj);
3810 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3811 mdd_tobj, mdd_tpobj, tpattr, lf,
3812 mdd_spobj, spattr, ltname, lsname,
3816 rc = mdd_trans_stop(env, mdd, rc, handle);
3819 mdd_object_put(env, mdd_sobj);
3825 * Check whether we should migrate the file/dir
3827 * < 0 permission check failed or other error.
3828 * = 0 the file can be migrated.
3830 static int mdd_migrate_sanity_check(const struct lu_env *env,
3831 struct mdd_device *mdd,
3832 struct mdd_object *spobj,
3833 struct mdd_object *tpobj,
3834 struct mdd_object *sobj,
3835 struct mdd_object *tobj,
3836 const struct lu_attr *spattr,
3837 const struct lu_attr *tpattr,
3838 const struct lu_attr *attr,
3845 if (!nsonly && !mdd_object_remote(sobj)) {
3846 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3847 if (sobj->mod_count > 0) {
3848 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3849 mdd_obj_dev_name(sobj),
3850 PFID(mdd_object_fid(sobj)),
3852 mdd_read_unlock(env, sobj);
3855 mdd_read_unlock(env, sobj);
3858 if (mdd_object_exists(tobj))
3861 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3865 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3870 struct mdd_xattr_entry {
3871 struct list_head mxe_linkage;
3873 struct lu_buf mxe_buf;
3877 struct lu_buf mx_namebuf;
3878 struct list_head mx_list;
3881 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3883 INIT_LIST_HEAD(&xattrs->mx_list);
3884 xattrs->mx_namebuf.lb_buf = NULL;
3885 xattrs->mx_namebuf.lb_len = 0;
3888 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3890 struct mdd_xattr_entry *entry;
3891 struct mdd_xattr_entry *tmp;
3893 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3894 lu_buf_free(&entry->mxe_buf);
3895 list_del(&entry->mxe_linkage);
3896 OBD_FREE_PTR(entry);
3899 lu_buf_free(&xattrs->mx_namebuf);
3902 /* read xattrs into buf, but skip LMA, LMV, LINKEA if 'skip_linkea' is
3903 * set, and DMV if 'skip_dmv" is set.
3905 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3906 struct mdd_xattrs *xattrs,
3907 struct mdd_object *sobj,
3911 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3912 struct mdd_xattr_entry *entry;
3913 bool needencxattr = false;
3914 bool encxattrfound = false;
3924 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3925 if (list_xsize == -ENODATA)
3931 if (attr->la_valid & LA_FLAGS &&
3932 attr->la_flags & LUSTRE_ENCRYPT_FL) {
3933 needencxattr = true;
3935 strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3938 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3939 if (xattrs->mx_namebuf.lb_buf == NULL)
3942 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3948 xname = xattrs->mx_namebuf.lb_buf;
3950 for (; rem > 0; xname += xlen, rem -= xlen) {
3952 strcmp(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3953 encxattrfound = true;
3954 xlen = strnlen(xname, rem - 1) + 1;
3955 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3956 strcmp(XATTR_NAME_LMV, xname) == 0)
3960 strcmp(XATTR_NAME_LINK, xname) == 0)
3964 strcmp(XATTR_NAME_DEFAULT_LMV, xname) == 0)
3967 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3968 if (xsize == -ENODATA)
3971 GOTO(fini, rc = xsize);
3973 OBD_ALLOC_PTR(entry);
3975 GOTO(fini, rc = -ENOMEM);
3977 lu_buf_alloc(&entry->mxe_buf, xsize);
3978 if (!entry->mxe_buf.lb_buf) {
3979 OBD_FREE_PTR(entry);
3980 GOTO(fini, rc = -ENOMEM);
3983 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3985 lu_buf_free(&entry->mxe_buf);
3986 OBD_FREE_PTR(entry);
3992 entry->mxe_name = xname;
3993 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
3996 if (needencxattr && !encxattrfound) {
3997 xlen = strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3998 strncpy(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT, xlen);
4005 mdd_xattrs_fini(xattrs);
4009 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
4010 struct mdd_object *obj,
4011 const struct lu_buf *buf,
4013 int fl, struct thandle *handle);
4015 static int mdd_foreach_xattr(const struct lu_env *env,
4016 struct mdd_object *tobj,
4017 struct mdd_xattrs *xattrs,
4018 struct thandle *handle,
4021 struct mdd_xattr_entry *entry;
4024 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
4025 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
4033 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
4034 struct mdd_object *sobj,
4035 struct mdd_object *tobj,
4036 const struct lu_name *sname,
4037 const struct lu_fid *sfid,
4038 const struct lu_name *lname,
4039 const struct lu_fid *fid,
4041 struct thandle *handle);
4043 static int mdd_declare_update_link(const struct lu_env *env,
4044 struct mdd_object *sobj,
4045 struct mdd_object *tobj,
4046 const struct lu_name *tname,
4047 const struct lu_fid *tpfid,
4048 const struct lu_name *lname,
4049 const struct lu_fid *fid,
4051 struct thandle *handle)
4053 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4054 struct mdd_object *pobj;
4058 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4059 !strcmp(tname->ln_name, lname->ln_name))
4062 pobj = mdd_object_find(env, mdd, fid);
4064 return PTR_ERR(pobj);
4067 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4069 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4070 mdd_object_type(sobj),
4071 lname->ln_name, handle);
4072 mdd_object_put(env, pobj);
4076 rc = mdo_declare_ref_add(env, tobj, handle);
4080 rc = mdo_declare_ref_del(env, sobj, handle);
4084 static int mdd_update_link(const struct lu_env *env,
4085 struct mdd_object *sobj,
4086 struct mdd_object *tobj,
4087 const struct lu_name *tname,
4088 const struct lu_fid *tpfid,
4089 const struct lu_name *lname,
4090 const struct lu_fid *fid,
4092 struct thandle *handle)
4094 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4095 struct mdd_object *pobj;
4101 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4102 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
4105 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
4106 PFID(fid), encode_fn_luname(lname), PFID(mdd_object_fid(tobj)));
4108 pobj = mdd_object_find(env, mdd, fid);
4110 CWARN("%s: cannot find obj "DFID": %ld\n",
4111 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
4112 RETURN(PTR_ERR(pobj));
4115 if (!mdd_object_exists(pobj)) {
4116 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
4117 mdd_object_put(env, pobj);
4121 mdd_write_lock(env, pobj, DT_TGT_PARENT);
4122 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4124 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
4125 mdd_object_type(sobj),
4126 lname->ln_name, handle);
4127 mdd_write_unlock(env, pobj);
4128 mdd_object_put(env, pobj);
4132 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4133 rc = mdo_ref_add(env, tobj, handle);
4134 mdd_write_unlock(env, tobj);
4138 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4139 rc = mdo_ref_del(env, sobj, handle);
4140 mdd_write_unlock(env, sobj);
4145 static inline int mdd_fld_lookup(const struct lu_env *env,
4146 struct mdd_device *mdd,
4147 const struct lu_fid *fid,
4150 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
4151 struct seq_server_site *ss;
4154 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4156 range->lsr_flags = LU_SEQ_RANGE_MDT;
4157 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4161 *mdt_index = range->lsr_index;
4166 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
4167 struct mdd_object *sobj,
4168 struct mdd_object *tobj,
4169 const struct lu_name *tname,
4170 const struct lu_fid *tpfid,
4171 const struct lu_name *lname,
4172 const struct lu_fid *fid,
4174 struct thandle *handle)
4176 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4177 __u32 source_mdt_index = *(__u32 *)opaque;
4178 __u32 link_mdt_index;
4184 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4185 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
4188 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
4192 RETURN(link_mdt_index == source_mdt_index);
4195 static int mdd_iterate_linkea(const struct lu_env *env,
4196 struct mdd_object *sobj,
4197 struct mdd_object *tobj,
4198 const struct lu_name *tname,
4199 const struct lu_fid *tpfid,
4200 struct linkea_data *ldata,
4202 struct thandle *handle,
4205 struct mdd_thread_info *info = mdd_env_info(env);
4206 char *filename = info->mdi_name;
4207 struct lu_name lname;
4214 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
4215 linkea_next_entry(ldata)) {
4216 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4219 /* Note: lname might miss \0 at the end */
4220 snprintf(filename, sizeof(info->mdi_name), DNAME,
4221 lname.ln_namelen, lname.ln_name);
4222 lname.ln_name = filename;
4224 CDEBUG(D_INFO, DFID"/"DNAME"\n",
4225 PFID(&fid), encode_fn_luname(&lname));
4227 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
4235 * Prepare linkea, and check whether file needs migrate: if source still has
4236 * link on source MDT, no need to migrate, just update namespace on source and
4239 * \retval 0 do migrate
4240 * \retval 1 don't migrate
4241 * \retval -errno on failure
4243 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
4244 struct mdd_device *mdd,
4245 struct mdd_object *spobj,
4246 struct mdd_object *tpobj,
4247 struct mdd_object *sobj,
4248 const struct lu_name *sname,
4249 const struct lu_name *tname,
4250 const struct lu_attr *attr,
4251 struct linkea_data *ldata)
4253 __u32 source_mdt_index;
4258 memset(ldata, 0, sizeof(*ldata));
4259 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
4260 mdd_object_fid(tpobj), tname, 1, 0, ldata);
4265 * Then it will check if the file should be migrated. If the file has
4266 * mulitple links, we only need migrate the file if all of its entries
4267 * has been migrated to the remote MDT.
4269 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
4272 /* If there are still links locally, don't migrate this file */
4273 LASSERT(ldata->ld_leh != NULL);
4275 /* If linkEA is overflow, switch to ns-only migrate */
4276 if (unlikely(ldata->ld_leh->leh_overflow_time))
4279 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
4283 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
4284 ldata, &source_mdt_index, NULL,
4285 mdd_is_link_on_source_mdt);
4289 static int mdd_declare_migrate_update(const struct lu_env *env,
4290 struct mdd_object *spobj,
4291 struct mdd_object *tpobj,
4292 struct mdd_object *obj,
4293 const struct lu_name *sname,
4294 const struct lu_name *tname,
4295 struct lu_attr *attr,
4296 struct lu_attr *spattr,
4297 struct lu_attr *tpattr,
4298 struct linkea_data *ldata,
4300 struct thandle *handle)
4302 struct mdd_thread_info *info = mdd_env_info(env);
4303 struct lu_attr *la = &info->mdi_la_for_fix;
4306 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
4310 if (S_ISDIR(attr->la_mode)) {
4311 rc = mdo_declare_ref_del(env, spobj, handle);
4316 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
4317 attr->la_mode & S_IFMT,
4318 tname->ln_name, handle);
4322 rc = mdd_declare_links_add(env, obj, handle, ldata);
4326 if (S_ISDIR(attr->la_mode)) {
4327 rc = mdo_declare_ref_add(env, tpobj, handle);
4332 la->la_valid = LA_CTIME | LA_MTIME;
4333 rc = mdo_declare_attr_set(env, spobj, la, handle);
4337 if (tpobj != spobj) {
4338 rc = mdo_declare_attr_set(env, tpobj, la, handle);
4346 static int mdd_declare_migrate_create(const struct lu_env *env,
4347 struct mdd_object *spobj,
4348 struct mdd_object *tpobj,
4349 struct mdd_object *sobj,
4350 struct mdd_object *tobj,
4351 const struct lu_name *sname,
4352 const struct lu_name *tname,
4353 struct lu_attr *spattr,
4354 struct lu_attr *tpattr,
4355 struct lu_attr *attr,
4356 struct lu_buf *sbuf,
4357 struct linkea_data *ldata,
4358 struct mdd_xattrs *xattrs,
4360 struct md_op_spec *spec,
4361 struct dt_allocation_hint *hint,
4362 struct thandle *handle)
4364 struct mdd_thread_info *info = mdd_env_info(env);
4365 struct md_layout_change *mlc = &info->mdi_mlc;
4366 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
4371 if (S_ISDIR(attr->la_mode)) {
4372 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
4374 mlc->mlc_opc = MD_LAYOUT_DETACH;
4375 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
4379 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4380 } else if (S_ISLNK(attr->la_mode)) {
4381 spec->u.sp_symname.ln_name = sbuf->lb_buf;
4382 /* don't count NUL */
4383 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
4384 } else if (S_ISREG(attr->la_mode)) {
4385 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
4386 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
4389 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
4391 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
4392 tname, attr, handle, spec, ldata, NULL, NULL,
4398 * tobj mode will be used in mdo_declare_layout_change(), but it's not
4399 * createb yet, copy from sobj.
4401 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
4402 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
4403 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
4405 if (S_ISDIR(attr->la_mode)) {
4407 /* if sobj is not striped, fake a 1-stripe LMV */
4408 LASSERT(sizeof(info->mdi_key) >
4409 lmv_mds_md_size(1, LMV_MAGIC_V1));
4410 lmv = (typeof(lmv))info->mdi_key;
4411 memset(lmv, 0, sizeof(*lmv));
4412 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4413 lmv->lmv_stripe_count = cpu_to_le32(1);
4414 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4415 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
4416 mdd_object_fid(sobj));
4417 mlc->mlc_buf.lb_buf = lmv;
4418 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4420 mlc->mlc_buf = *sbuf;
4422 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4423 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4428 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4429 mdo_declare_xattr_set);
4433 if (S_ISREG(attr->la_mode)) {
4434 struct lu_buf fid_buf;
4436 handle->th_complex = 1;
4438 /* target may be remote, update PFID via sobj. */
4439 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4440 fid_buf.lb_len = sizeof(struct lu_fid);
4441 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
4446 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4451 if (!S_ISDIR(attr->la_mode)) {
4452 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4453 mdd_object_fid(tpobj), ldata, NULL,
4454 handle, mdd_declare_update_link);
4459 if (!S_ISDIR(attr->la_mode) || lmv) {
4460 rc = mdo_declare_ref_del(env, sobj, handle);
4464 if (S_ISDIR(attr->la_mode)) {
4465 rc = mdo_declare_ref_del(env, sobj, handle);
4470 rc = mdo_declare_destroy(env, sobj, handle);
4475 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
4476 attr, spattr, tpattr, ldata, ma,
4482 * migrate dirent from \a spobj to \a tpobj.
4484 static int mdd_migrate_update(const struct lu_env *env,
4485 struct mdd_object *spobj,
4486 struct mdd_object *tpobj,
4487 struct mdd_object *obj,
4488 const struct lu_name *sname,
4489 const struct lu_name *tname,
4490 struct lu_attr *attr,
4491 struct lu_attr *spattr,
4492 struct lu_attr *tpattr,
4493 struct linkea_data *ldata,
4495 struct thandle *handle)
4497 struct mdd_thread_info *info = mdd_env_info(env);
4498 struct lu_attr *la = &info->mdi_la_for_fix;
4503 CDEBUG(D_INFO, "update "DFID" from "DFID"/"DNAME" to "DFID"/"DNAME"\n",
4504 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
4505 encode_fn_luname(sname), PFID(mdd_object_fid(tpobj)),
4506 encode_fn_luname(tname));
4508 rc = __mdd_index_delete(env, spobj, sname->ln_name,
4509 S_ISDIR(attr->la_mode), handle);
4513 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
4514 attr->la_mode & S_IFMT,
4515 tname->ln_name, handle);
4519 rc = mdd_links_write(env, obj, ldata, handle);
4523 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4524 la->la_valid = LA_CTIME | LA_MTIME;
4525 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4526 rc = mdd_update_time(env, spobj, spattr, la, handle);
4527 mdd_write_unlock(env, spobj);
4531 if (tpobj != spobj) {
4532 la->la_valid = LA_CTIME | LA_MTIME;
4533 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4534 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4535 mdd_write_unlock(env, tpobj);
4544 * Migrate file/dir to target MDT.
4546 * Create target according to \a spec, and then migrate xattrs, if it's
4547 * directory, migrate source stripes to target.
4549 * \param[in] env execution environment
4550 * \param[in] spobj source parent object
4551 * \param[in] tpobj target parent object
4552 * \param[in] sobj source object
4553 * \param[in] tobj target object
4554 * \param[in] lname file name
4555 * \param[in] spattr source parent attributes
4556 * \param[in] tpattr target parent attributes
4557 * \param[in] attr source attributes
4558 * \param[in] sbuf source LMV buf
4559 * \param[in] spec migrate create spec
4560 * \param[in] hint target creation hint
4561 * \param[in] handle tranasction handle
4563 * \retval 0 on success
4564 * \retval -errno on failure
4566 static int mdd_migrate_create(const struct lu_env *env,
4567 struct mdd_object *spobj,
4568 struct mdd_object *tpobj,
4569 struct mdd_object *sobj,
4570 struct mdd_object *tobj,
4571 const struct lu_name *sname,
4572 const struct lu_name *tname,
4573 struct lu_attr *spattr,
4574 struct lu_attr *tpattr,
4575 struct lu_attr *attr,
4576 const struct lu_buf *sbuf,
4577 struct linkea_data *ldata,
4578 struct mdd_xattrs *xattrs,
4580 struct md_op_spec *spec,
4581 struct dt_allocation_hint *hint,
4582 struct thandle *handle)
4589 * migrate sobj stripes to tobj if it's directory:
4590 * 1. detach stripes from sobj.
4591 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4592 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4594 if (S_ISDIR(attr->la_mode)) {
4595 struct mdd_thread_info *info = mdd_env_info(env);
4596 struct md_layout_change *mlc = &info->mdi_mlc;
4598 mlc->mlc_opc = MD_LAYOUT_DETACH;
4600 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4601 rc = mdo_layout_change(env, sobj, mlc, handle);
4602 mdd_write_unlock(env, sobj);
4607 /* don't set nlink from sobj */
4608 attr->la_valid &= ~LA_NLINK;
4610 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4611 hint, handle, false);
4615 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4616 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4617 mdd_write_unlock(env, tobj);
4621 /* for regular file, update OST objects XATTR_NAME_FID */
4622 if (S_ISREG(attr->la_mode)) {
4623 struct lu_buf fid_buf;
4625 /* target may be remote, update PFID via sobj. */
4626 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4627 fid_buf.lb_len = sizeof(struct lu_fid);
4628 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4633 /* delete LOV to avoid deleting OST objs when destroying sobj */
4634 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4635 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4636 mdd_write_unlock(env, sobj);
4637 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4638 if (rc && rc != -ENODATA)
4643 /* update links FID */
4644 if (!S_ISDIR(attr->la_mode)) {
4645 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4646 mdd_object_fid(tpobj), ldata,
4647 NULL, handle, mdd_update_link);
4652 /* don't destroy sobj if it's plain directory */
4653 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4654 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4655 rc = mdo_ref_del(env, sobj, handle);
4657 if (S_ISDIR(attr->la_mode))
4658 rc = mdo_ref_del(env, sobj, handle);
4660 rc = mdo_destroy(env, sobj, handle);
4662 mdd_write_unlock(env, sobj);
4667 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4668 spattr, tpattr, ldata, ma, handle);
4673 /* NB: if user issued different migrate command, we can't adjust it silently
4674 * here, because this command will decide target MDT in subdir migration in
4677 static int mdd_migrate_cmd_check(const struct lu_env *env, struct mdd_device *mdd,
4678 struct mdd_object *sobj,
4679 const struct lmv_mds_md_v1 *lmv,
4680 const struct lmv_user_md_v1 *lum,
4681 size_t lum_len, const struct lu_name *lname)
4683 struct mdd_thread_info *info = mdd_env_info(env);
4684 __u32 lum_stripe_count = lum->lum_stripe_count;
4685 __u32 lum_hash_type = lum->lum_hash_type &
4686 cpu_to_le32(LMV_HASH_TYPE_MASK);
4687 struct md_layout_change *mlc = &info->mdi_mlc;
4688 __u32 lmv_hash_type;
4692 if (lmv && !lmv_is_sane(lmv))
4695 /* If stripe_count unspecified, set to 1 */
4696 if (!lum_stripe_count)
4697 lum_stripe_count = cpu_to_le32(1);
4699 /* Easy check for plain and single-striped dirs
4700 * if the object is on the target MDT already
4702 if (!lmv || lmv->lmv_stripe_count == cpu_to_le32(1)) {
4703 struct seq_server_site *ss = mdd_seq_site(mdd);
4704 struct lu_seq_range range = { 0 };
4706 fld_range_set_type(&range, LU_SEQ_RANGE_MDT);
4707 rc = fld_server_lookup(env, ss->ss_server_fld,
4708 fid_seq(mdd_object_fid(sobj)), &range);
4712 if (lum_stripe_count == cpu_to_le32(1) &&
4713 le32_to_cpu(lum->lum_stripe_offset) == range.lsr_index)
4718 lmv_hash_type = lmv->lmv_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK);
4720 if (lmv_is_migrating(lmv)) {
4721 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4722 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4723 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4727 /* check at top level if the target layout already applied */
4728 if ((lum_hash_type && lum_hash_type != lmv_hash_type) ||
4729 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4730 lum_stripe_count != lmv->lmv_stripe_count)
4735 mlc->mlc_buf.lb_buf = (void*)lum;
4736 mlc->mlc_buf.lb_len = lum_len;
4737 rc = mo_layout_check(env, &sobj->mod_obj, mlc);
4741 CERROR("%s: '"DNAME"' migration was interrupted, run "
4742 "'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration: rc = %d\n",
4743 mdd2obd_dev(mdd)->obd_name, encode_fn_luname(lname),
4744 le32_to_cpu(lmv->lmv_master_mdt_index),
4745 le32_to_cpu(lmv->lmv_migrate_offset),
4746 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4747 encode_fn_luname(lname), rc);
4754 * Internal function to migrate directory or file between MDTs.
4756 * migrate source to target in following steps:
4757 * 1. create target, append source stripes after target's if it's directory,
4758 * migrate xattrs and update fid of source links.
4759 * 2. update namespace: migrate dirent from source parent to target parent,
4760 * update file linkea, and destroy source if it's not needed any more.
4762 * \param[in] env execution environment
4763 * \param[in] spobj source parent object
4764 * \param[in] tpobj target parent object
4765 * \param[in] sobj source object
4766 * \param[in] tobj target object
4767 * \param[in] sname source file name
4768 * \param[in] tname target file name
4769 * \param[in] spec target creation spec
4770 * \param[in] ma used to update \a pobj mtime and ctime
4772 * \retval 0 on success
4773 * \retval -errno on failure
4775 static int mdd_migrate_object(const struct lu_env *env,
4776 struct mdd_object *spobj,
4777 struct mdd_object *tpobj,
4778 struct mdd_object *sobj,
4779 struct mdd_object *tobj,
4780 const struct lu_name *sname,
4781 const struct lu_name *tname,
4782 struct md_op_spec *spec,
4785 struct mdd_thread_info *info = mdd_env_info(env);
4786 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4787 struct lu_attr *spattr = &info->mdi_pattr;
4788 struct lu_attr *tpattr = &info->mdi_tpattr;
4789 struct lu_attr *attr = &info->mdi_cattr;
4790 struct linkea_data *ldata = &info->mdi_link_data;
4791 struct dt_allocation_hint *hint = &info->mdi_hint;
4792 struct lu_buf sbuf = { NULL };
4793 struct mdd_xattrs xattrs;
4794 struct lmv_mds_md_v1 *lmv;
4795 struct thandle *handle;
4801 CDEBUG(D_INFO, "migrate "DNAME" from "DFID"/"DFID" to "DFID"/"DFID"\n",
4802 encode_fn_luname(sname), PFID(mdd_object_fid(spobj)),
4803 PFID(mdd_object_fid(sobj)), PFID(mdd_object_fid(tpobj)),
4804 PFID(mdd_object_fid(tobj)));
4807 rc = mdd_la_get(env, sobj, attr);
4811 rc = mdd_la_get(env, spobj, spattr);
4815 rc = mdd_la_get(env, tpobj, tpattr);
4819 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4820 spattr, tpattr, attr,
4821 spec->sp_migrate_nsonly);
4822 if (rc == -EBUSY && !spec->sp_migrate_nsonly) {
4823 spec->sp_migrate_nsonly = 1;
4824 CWARN("%s: "DFID"/%s is open, migrate only dentry\n",
4825 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(spobj)),
4827 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj,
4828 tobj, spattr, tpattr, attr,
4829 spec->sp_migrate_nsonly);
4834 mdd_xattrs_init(&xattrs);
4836 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4837 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4838 size_t lum_len = spec->u.sp_ea.eadatalen;
4842 /* if user use default value '0' for stripe_count, we need to
4843 * adjust it to '1' to create a 1-stripe directory.
4845 if (lum->lum_stripe_count == 0)
4846 lum->lum_stripe_count = cpu_to_le32(1);
4848 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4849 if (rc && rc != -ENODATA)
4853 rc = mdd_migrate_cmd_check(env, mdd, sobj, lmv, lum,
4857 } else if (!S_ISDIR(attr->la_mode)) {
4858 /* update namespace only if @sobj is on MDT where @tpobj is. */
4859 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4860 spec->sp_migrate_nsonly = true;
4862 if (S_ISLNK(attr->la_mode)) {
4863 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4865 GOTO(out, rc = -ENOMEM);
4867 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4870 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4871 mdd2obd_dev(mdd)->obd_name,
4872 PFID(mdd_object_fid(sobj)), rc);
4878 /* linkea needs update upon FID or parent stripe change */
4879 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4880 tname, attr, ldata);
4882 /* update namespace only if @sobj has link on its MDT. */
4883 spec->sp_migrate_nsonly = true;
4887 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4888 * RPCs inside transaction.
4890 if (!spec->sp_migrate_nsonly) {
4891 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, true, true);
4896 handle = mdd_trans_create(env, mdd);
4898 GOTO(out, rc = PTR_ERR(handle));
4900 if (spec->sp_migrate_nsonly)
4901 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4902 tname, attr, spattr, tpattr,
4905 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4906 sname, tname, spattr, tpattr,
4907 attr, &sbuf, ldata, &xattrs, ma,
4908 spec, hint, handle);
4912 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4917 rc = mdd_trans_start(env, mdd, handle);
4921 if (spec->sp_migrate_nsonly)
4922 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4923 attr, spattr, tpattr, ldata, ma,
4926 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4927 tname, spattr, tpattr, attr, &sbuf,
4928 ldata, &xattrs, ma, spec, hint, handle);
4932 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4933 spec->sp_migrate_nsonly ? sobj : tobj,
4934 spobj, spattr, mdd_object_fid(sobj),
4935 tpobj, tpattr, tname, sname,
4939 rc = mdd_trans_stop(env, mdd, rc, handle);
4941 mdd_xattrs_fini(&xattrs);
4945 * -EAGAIN means transaction execution phase detect the layout
4946 * has been changed by others.
4948 if (rc == -EAGAIN && retried++ < MAX_TRANS_RETRIED)
4949 GOTO(retry, retried);
4955 * Migrate directory or file between MDTs.
4957 * \param[in] env execution environment
4958 * \param[in] md_spobj source parent object
4959 * \param[in] md_tpobj target parent object
4960 * \param[in] md_sobj source object
4961 * \param[in] lname file name
4962 * \param[in] md_tobj target object
4963 * \param[in] spec target creation spec
4964 * \param[in] ma used to update \a pobj mtime and ctime
4966 * \retval 0 on success
4967 * \retval -errno on failure
4969 static int mdd_migrate(const struct lu_env *env, struct md_object *md_spobj,
4970 struct md_object *md_tpobj, struct md_object *md_sobj,
4971 struct md_object *md_tobj, const struct lu_name *lname,
4972 struct md_op_spec *spec, struct md_attr *ma)
4974 return mdd_migrate_object(env, md2mdd_obj(md_spobj),
4975 md2mdd_obj(md_tpobj), md2mdd_obj(md_sobj),
4976 md2mdd_obj(md_tobj), lname, lname, spec, ma);
4979 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4980 struct mdd_object *pobj,
4981 struct mdd_object *obj,
4982 struct mdd_object *stripe,
4983 struct lu_attr *attr,
4984 struct mdd_xattrs *xattrs,
4985 struct md_layout_change *mlc,
4986 struct lu_name *lname,
4987 struct thandle *handle)
4991 mlc->mlc_opc = MD_LAYOUT_DETACH;
4992 rc = mdo_declare_layout_change(env, obj, mlc, handle);
4996 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
4997 S_IFDIR, dotdot, handle);
5001 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
5002 mdo_declare_xattr_set);
5006 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
5010 rc = mdo_declare_attr_set(env, stripe, attr, handle);
5014 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5018 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
5019 attr->la_mode, lname->ln_name, handle);
5023 rc = mdo_declare_ref_del(env, obj, handle);
5027 rc = mdo_declare_ref_del(env, obj, handle);
5031 rc = mdo_declare_destroy(env, obj, handle);
5038 /* transform one-stripe directory to a plain directory */
5039 static int mdd_1sd_collapse(const struct lu_env *env,
5040 struct mdd_object *pobj,
5041 struct mdd_object *obj,
5042 struct mdd_object *stripe,
5043 struct lu_attr *attr,
5044 struct mdd_xattrs *xattrs,
5045 struct md_layout_change *mlc,
5046 struct lu_name *lname,
5047 struct thandle *handle)
5053 /* replace 1-stripe directory with its stripe */
5054 mlc->mlc_opc = MD_LAYOUT_DETACH;
5056 mdd_write_lock(env, obj, DT_SRC_PARENT);
5057 rc = mdo_layout_change(env, obj, mlc, handle);
5058 mdd_write_unlock(env, obj);
5062 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5063 mdd_write_lock(env, obj, DT_SRC_CHILD);
5065 /* insert dotdot to stripe which points to parent */
5066 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
5067 S_IFDIR, dotdot, handle);
5071 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
5076 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
5080 /* don't set nlink from parent */
5081 attr->la_valid &= ~LA_NLINK;
5083 rc = mdo_attr_set(env, stripe, attr, handle);
5087 /* delete dir name from parent */
5088 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
5092 /* insert stripe to parent with dir name */
5093 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
5094 attr->la_mode, lname->ln_name, handle);
5098 /* destroy dir obj */
5099 rc = mdo_ref_del(env, obj, handle);
5103 rc = mdo_ref_del(env, obj, handle);
5107 rc = mdo_destroy(env, obj, handle);
5113 mdd_write_unlock(env, obj);
5114 mdd_write_unlock(env, pobj);
5120 * shrink directory stripes after migration/merge
5122 int mdd_dir_layout_shrink(const struct lu_env *env,
5123 struct md_object *md_obj,
5124 struct md_layout_change *mlc)
5126 struct mdd_device *mdd = mdo2mdd(md_obj);
5127 struct mdd_thread_info *info = mdd_env_info(env);
5128 struct mdd_object *obj = md2mdd_obj(md_obj);
5129 struct mdd_object *pobj = NULL;
5130 struct mdd_object *stripe = NULL;
5131 struct lu_attr *attr = &info->mdi_pattr;
5132 struct lu_fid *fid = &info->mdi_fid2;
5133 struct lu_name lname = { NULL };
5134 struct lu_buf lmv_buf = { NULL };
5135 struct mdd_xattrs xattrs;
5136 struct lmv_mds_md_v1 *lmv;
5137 struct lmv_user_md *lmu;
5138 struct thandle *handle;
5143 rc = mdd_la_get(env, obj, attr);
5147 if (!S_ISDIR(attr->la_mode))
5150 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
5154 lmv = lmv_buf.lb_buf;
5155 if (!lmv_is_sane(lmv))
5156 GOTO(out_lmv, rc = -EBADF);
5158 lmu = mlc->mlc_buf.lb_buf;
5160 /* adjust the default value '0' to '1' */
5161 if (lmu->lum_stripe_count == 0)
5162 lmu->lum_stripe_count = cpu_to_le32(1);
5164 /* these were checked in MDT */
5165 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
5166 le32_to_cpu(lmv->lmv_stripe_count));
5167 LASSERT(!lmv_is_splitting(lmv));
5168 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
5170 mdd_xattrs_init(&xattrs);
5172 /* if dir stripe count will be shrunk to 1, it needs to be transformed
5173 * to a plain dir, which will cause FID change and namespace update.
5175 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
5176 struct linkea_data *ldata = &info->mdi_link_data;
5177 char *filename = info->mdi_name;
5179 rc = mdd_links_read(env, obj, ldata);
5183 if (ldata->ld_leh->leh_reccount > 1)
5184 GOTO(out, rc = -EINVAL);
5186 linkea_first_entry(ldata);
5188 GOTO(out, rc = -ENODATA);
5190 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
5193 /* Note: lname might miss \0 at the end */
5194 snprintf(filename, sizeof(info->mdi_name), DNAME,
5195 lname.ln_namelen, lname.ln_name);
5196 lname.ln_name = filename;
5198 pobj = mdd_object_find(env, mdd, fid);
5205 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
5207 stripe = mdd_object_find(env, mdd, fid);
5208 if (IS_ERR(stripe)) {
5209 mdd_object_put(env, pobj);
5211 GOTO(out, rc = PTR_ERR(stripe));
5214 if (!lmv_is_fixed(lmv))
5215 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, false,
5219 handle = mdd_trans_create(env, mdd);
5221 GOTO(out, rc = PTR_ERR(handle));
5223 mlc->mlc_opc = MD_LAYOUT_SHRINK;
5224 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5226 GOTO(stop_trans, rc);
5228 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
5229 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
5230 &xattrs, mlc, &lname, handle);
5232 GOTO(stop_trans, rc);
5235 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
5238 GOTO(stop_trans, rc);
5240 rc = mdd_trans_start(env, mdd, handle);
5242 GOTO(stop_trans, rc);
5244 mdd_write_lock(env, obj, DT_SRC_PARENT);
5245 mlc->mlc_opc = MD_LAYOUT_SHRINK;
5246 rc = mdo_layout_change(env, obj, mlc, handle);
5247 mdd_write_unlock(env, obj);
5249 GOTO(stop_trans, rc);
5251 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
5252 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
5253 mlc, &lname, handle);
5255 GOTO(stop_trans, rc);
5258 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5259 XATTR_NAME_LMV, handle);
5260 GOTO(stop_trans, rc);
5263 rc = mdd_trans_stop(env, mdd, rc, handle);
5265 mdd_xattrs_fini(&xattrs);
5267 mdd_object_put(env, stripe);
5268 mdd_object_put(env, pobj);
5271 lu_buf_free(&lmv_buf);
5275 static int mdd_dir_declare_split_plain(const struct lu_env *env,
5276 struct mdd_device *mdd,
5277 struct mdd_object *pobj,
5278 struct mdd_object *obj,
5279 struct mdd_object *tobj,
5280 struct mdd_xattrs *xattrs,
5281 struct md_layout_change *mlc,
5282 struct dt_allocation_hint *hint,
5283 struct thandle *handle)
5285 struct mdd_thread_info *info = mdd_env_info(env);
5286 const struct lu_name *lname = mlc->mlc_name;
5287 struct lu_attr *la = &info->mdi_la_for_fix;
5288 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
5289 struct linkea_data *ldata = &info->mdi_link_data;
5290 struct lmv_mds_md_v1 *lmv;
5294 mlc->mlc_opc = MD_LAYOUT_DETACH;
5295 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5299 memset(ldata, 0, sizeof(*ldata));
5300 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
5301 lname, 1, 0, ldata);
5305 count = lum->lum_stripe_count;
5306 lum->lum_stripe_count = 0;
5307 /* don't set default LMV since it will become a striped dir */
5308 lum->lum_max_inherit = LMV_INHERIT_NONE;
5309 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5311 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
5312 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
5313 ldata, NULL, NULL, NULL, hint);
5317 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
5320 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
5322 lmv = (typeof(lmv))info->mdi_key;
5323 memset(lmv, 0, sizeof(*lmv));
5324 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
5325 lmv->lmv_stripe_count = cpu_to_le32(1);
5326 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
5327 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
5329 mlc->mlc_opc = MD_LAYOUT_ATTACH;
5330 mlc->mlc_buf.lb_buf = lmv;
5331 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
5332 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5336 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
5337 mdo_declare_xattr_set);
5341 lum->lum_stripe_count = count;
5342 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5343 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5347 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5351 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
5352 S_IFDIR, lname->ln_name, handle);
5356 la->la_valid = LA_CTIME | LA_MTIME;
5357 rc = mdo_declare_attr_set(env, obj, la, handle);
5361 rc = mdo_declare_attr_set(env, pobj, la, handle);
5365 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
5371 * plain directory split:
5372 * 1. create \a tobj as plain directory.
5373 * 2. append \a obj as first stripe of \a tobj.
5374 * 3. migrate xattrs from \a obj to \a tobj.
5375 * 4. split \a tobj to specific stripe count.
5377 static int mdd_dir_split_plain(const struct lu_env *env,
5378 struct mdd_device *mdd,
5379 struct mdd_object *pobj,
5380 struct mdd_object *obj,
5381 struct mdd_object *tobj,
5382 struct mdd_xattrs *xattrs,
5383 struct md_layout_change *mlc,
5384 struct dt_allocation_hint *hint,
5385 struct thandle *handle)
5387 struct mdd_thread_info *info = mdd_env_info(env);
5388 struct lu_attr *pattr = &info->mdi_pattr;
5389 struct lu_attr *la = &info->mdi_la_for_fix;
5390 const struct lu_name *lname = mlc->mlc_name;
5391 struct linkea_data *ldata = &info->mdi_link_data;
5396 /* copy linkea out and set on target later */
5397 rc = mdd_links_read(env, obj, ldata);
5401 mlc->mlc_opc = MD_LAYOUT_DETACH;
5402 rc = mdo_layout_change(env, obj, mlc, handle);
5406 /* don't set nlink from obj */
5407 mlc->mlc_attr->la_valid &= ~LA_NLINK;
5409 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5410 NULL, NULL, NULL, hint, handle, false);
5414 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
5418 rc = mdd_links_write(env, tobj, ldata, handle);
5422 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
5426 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
5427 lname->ln_name, handle);
5431 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
5432 la->la_valid = LA_CTIME | LA_MTIME;
5434 mdd_write_lock(env, obj, DT_SRC_CHILD);
5435 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
5436 mdd_write_unlock(env, obj);
5440 rc = mdd_la_get(env, pobj, pattr);
5444 la->la_valid = LA_CTIME | LA_MTIME;
5446 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5447 rc = mdd_update_time(env, pobj, pattr, la, handle);
5448 mdd_write_unlock(env, pobj);
5452 /* FID changes, record it as CL_MIGRATE */
5453 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
5454 pobj, pattr, mdd_object_fid(obj),
5455 pobj, pattr, lname, lname, handle);
5459 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
5460 struct md_layout_change *mlc)
5462 struct mdd_thread_info *info = mdd_env_info(env);
5463 struct mdd_device *mdd = mdo2mdd(o);
5464 struct mdd_object *obj = md2mdd_obj(o);
5465 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
5466 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
5467 struct dt_allocation_hint *hint = &info->mdi_hint;
5468 bool is_plain = false;
5469 struct mdd_xattrs xattrs;
5470 struct thandle *handle;
5475 LASSERT(S_ISDIR(mdd_object_type(obj)));
5477 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
5483 mdd_xattrs_init(&xattrs);
5485 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true, true);
5487 handle = mdd_trans_create(env, mdd);
5489 GOTO(out, rc = PTR_ERR(handle));
5492 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
5493 &xattrs, mlc, hint, handle);
5495 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5496 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5498 GOTO(stop_trans, rc);
5500 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5504 GOTO(stop_trans, rc);
5506 rc = mdd_trans_start(env, mdd, handle);
5508 GOTO(stop_trans, rc);
5511 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5514 struct lu_buf *buf = &info->mdi_buf[0];
5516 buf->lb_buf = mlc->mlc_spec->u.sp_ea.eadata;
5517 buf->lb_len = mlc->mlc_spec->u.sp_ea.eadatalen;
5519 mdd_write_lock(env, obj, DT_TGT_CHILD);
5520 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LMV,
5521 LU_XATTR_CREATE, handle);
5522 mdd_write_unlock(env, obj);
5524 GOTO(stop_trans, rc);
5526 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5527 XATTR_NAME_LMV, handle);
5530 GOTO(stop_trans, rc);
5535 rc = mdd_trans_stop(env, mdd, rc, handle);
5537 mdd_xattrs_fini(&xattrs);
5542 const struct md_dir_operations mdd_dir_ops = {
5543 .mdo_is_subdir = mdd_is_subdir,
5544 .mdo_lookup = mdd_lookup,
5545 .mdo_create = mdd_create,
5546 .mdo_rename = mdd_rename,
5547 .mdo_link = mdd_link,
5548 .mdo_unlink = mdd_unlink,
5549 .mdo_create_data = mdd_create_data,
5550 .mdo_migrate = mdd_migrate,