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), NULL, 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 /* remove the quotes if present before storing it in the xattr. */
2950 if (initial_create &&
2951 spec->sp_cr_job_xattr[0] != '\0' &&
2953 (S_ISREG(attr->la_mode) ||
2954 S_ISDIR(attr->la_mode))) {
2955 jobid_len = strnlen(jobid, LUSTRE_JOBID_SIZE);
2956 if (jobid_len > 1 &&
2958 jobid[jobid_len - 1] == '"'){
2962 if (jobid_len > 0) {
2963 buf = mdd_buf_get_const(env, jobid, jobid_len);
2964 rc = mdo_xattr_set(env, son, buf,
2965 spec->sp_cr_job_xattr, 0,
2967 /* this xattr is nonessential, so ignore errors. */
2971 " failed to set xattr '%s': rc = %d\n",
2973 spec->sp_cr_job_xattr, rc);
2980 if (unlikely(rc != 0)) {
2983 if (S_ISDIR(attr->la_mode)) {
2984 /* Drop the reference, no need to delete "."/"..",
2985 * because the object to be destroyed directly.
2987 rc2 = mdo_ref_del(env, son, handle);
2991 rc2 = mdo_ref_del(env, son, handle);
2995 mdo_destroy(env, son, handle);
2998 mdd_write_unlock(env, son);
3002 static int mdd_index_delete(const struct lu_env *env,
3003 struct mdd_object *mdd_pobj,
3004 struct lu_attr *cattr,
3005 const struct lu_name *lname)
3007 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
3008 struct thandle *handle;
3013 handle = mdd_trans_create(env, mdd);
3015 RETURN(PTR_ERR(handle));
3017 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
3022 if (S_ISDIR(cattr->la_mode)) {
3023 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3028 /* Since this will only be used in the error handler path,
3029 * Let's set the thandle to be local and not mess the transno
3031 handle->th_local = 1;
3032 rc = mdd_trans_start(env, mdd, handle);
3036 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
3037 S_ISDIR(cattr->la_mode), handle);
3041 rc = mdd_trans_stop(env, mdd, rc, handle);
3047 * Create object and insert it into namespace.
3049 * Two operations have to be performed:
3051 * - an allocation of a new object (->do_create()), and
3052 * - an insertion into a parent index (->dio_insert()).
3054 * Due to locking, operation order is not important, when both are
3055 * successful, *but* error handling cases are quite different:
3057 * - if insertion is done first, and following object creation fails,
3058 * insertion has to be rolled back, but this operation might fail
3059 * also leaving us with dangling index entry.
3061 * - if creation is done first, is has to be undone if insertion fails,
3062 * leaving us with leaked space, which is not good but not fatal.
3064 * It seems that creation-first is simplest solution, but it is sub-optimal
3070 * case, because second mkdir is bound to create object, only to
3071 * destroy it immediately.
3073 * To avoid this follow local file systems that do double lookup:
3075 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
3076 * 1. create (mdd_create_object_internal())
3077 * 2. insert (__mdd_index_insert(), lookup again)
3079 * \param[in] pobj parent object
3080 * \param[in] lname name of child being created
3081 * \param[in,out] child child object being created
3082 * \param[in] spec additional create parameters
3083 * \param[in] ma attributes for new child object
3085 * \retval 0 on success
3086 * \retval negative errno on failure
3088 int mdd_create(const struct lu_env *env, struct md_object *pobj,
3089 const struct lu_name *lname, struct md_object *child,
3090 struct md_op_spec *spec, struct md_attr *ma)
3092 struct mdd_thread_info *info = mdd_env_info(env);
3093 struct lu_attr *la = &info->mdi_la_for_fix;
3094 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
3095 struct mdd_object *son = md2mdd_obj(child);
3096 struct mdd_device *mdd = mdo2mdd(pobj);
3097 struct lu_attr *attr = &ma->ma_attr;
3098 struct thandle *handle;
3099 struct lu_attr *pattr = &info->mdi_pattr;
3100 struct lu_buf acl_buf;
3101 struct lu_buf def_acl_buf;
3102 struct lu_buf hsm_buf;
3103 struct linkea_data *ldata = &info->mdi_link_data;
3104 const char *name = lname->ln_name;
3105 struct dt_allocation_hint *hint = &mdd_env_info(env)->mdi_hint;
3106 int acl_size = LUSTRE_POSIX_ACL_MAX_SIZE_OLD;
3107 bool name_inserted = false;
3112 rc = mdd_la_get(env, mdd_pobj, pattr);
3116 /* Sanity checks before big job. */
3117 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
3118 if (unlikely(rc == -EEXIST && S_ISDIR(attr->la_mode) &&
3119 spec->sp_replay && mdd_object_remote(mdd_pobj)))
3120 /* if it's replay by client request, and name is found in
3121 * parent directory on remote MDT, it means mkdir was partially
3122 * executed: name was successfully added, but target not.
3124 name_inserted = true;
3128 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
3129 GOTO(out_free, rc = -EINPROGRESS);
3131 handle = mdd_trans_create(env, mdd);
3133 GOTO(out_free, rc = PTR_ERR(handle));
3136 acl_buf = *lu_buf_check_and_alloc(&info->mdi_xattr_buf, acl_size);
3137 if (!acl_buf.lb_buf)
3138 GOTO(out_stop, rc = -ENOMEM);
3140 def_acl_buf = *lu_buf_check_and_alloc(&info->mdi_big_buf, acl_size);
3141 if (!def_acl_buf.lb_buf)
3142 GOTO(out_stop, rc = -ENOMEM);
3144 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
3145 if (unlikely(rc == -ERANGE &&
3146 acl_size == LUSTRE_POSIX_ACL_MAX_SIZE_OLD)) {
3147 /* use maximum-sized xattr buffer for too-big default ACL */
3148 acl_size = min_t(unsigned int, mdd->mdd_dt_conf.ddp_max_ea_size,
3150 goto use_bigger_buffer;
3155 /* adjust stripe count to 0 for 'lfs mkdir -c 1 ...' to avoid creating
3156 * 1-stripe directory, MDS_OPEN_DEFAULT_LMV means ea is default LMV.
3158 if (unlikely(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata &&
3159 !(spec->sp_cr_flags & MDS_OPEN_DEFAULT_LMV))) {
3160 struct lmv_user_md *lmu = spec->u.sp_ea.eadata;
3162 /* migrate may create 1-stripe directory, adjust stripe count
3163 * before lod_ah_init().
3165 if (lmu && lmu->lum_magic == cpu_to_le32(LMV_USER_MAGIC) &&
3166 lmu->lum_stripe_count == cpu_to_le32(1))
3167 lmu->lum_stripe_count = 0;
3170 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
3172 memset(ldata, 0, sizeof(*ldata));
3173 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
3174 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
3177 rc = mdd_linkea_prepare(env, son, NULL, NULL,
3178 &tfid, lname, 1, 0, ldata);
3180 rc = mdd_linkea_prepare(env, son, NULL, NULL,
3181 mdd_object_fid(mdd_pobj),
3182 lname, 1, 0, ldata);
3185 if (spec->sp_cr_flags & MDS_OPEN_PCC) {
3186 LASSERT(spec->sp_cr_flags & MDS_OPEN_HAS_EA);
3188 memset(&hsm_buf, 0, sizeof(hsm_buf));
3189 lu_buf_alloc(&hsm_buf, sizeof(struct hsm_attrs));
3190 if (hsm_buf.lb_buf == NULL)
3191 GOTO(out_stop, rc = -ENOMEM);
3194 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
3195 handle, spec, ldata, &def_acl_buf, &acl_buf,
3200 rc = mdd_trans_start(env, mdd, handle);
3204 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
3205 &def_acl_buf, &hsm_buf, hint, handle, true);
3209 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
3210 mdd_write_lock(env, son, DT_TGT_CHILD);
3211 son->mod_flags |= VOLATILE_OBJ;
3212 rc = mdd_orphan_insert(env, son, handle);
3213 GOTO(out_volatile, rc);
3215 if (likely(!name_inserted)) {
3216 rc = __mdd_index_insert(env, mdd_pobj,
3217 mdd_object_fid(son),
3218 attr->la_mode, name, handle);
3220 GOTO(err_created, rc);
3223 mdd_links_add(env, son, mdd_object_fid(mdd_pobj), lname,
3226 /* update parent directory mtime/ctime */
3228 la->la_valid = LA_CTIME | LA_MTIME;
3229 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
3231 GOTO(err_insert, rc);
3237 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
3238 rc2 = mdd_orphan_delete(env, son, handle);
3240 rc2 = __mdd_index_delete(env, mdd_pobj, name,
3241 S_ISDIR(attr->la_mode),
3247 mdd_write_lock(env, son, DT_TGT_CHILD);
3248 if (S_ISDIR(attr->la_mode)) {
3249 /* Drop the reference, no need to delete "."/"..",
3250 * because the object is to be destroyed directly.
3252 rc2 = mdo_ref_del(env, son, handle);
3254 mdd_write_unlock(env, son);
3259 /* For volatile files drop one link immediately, since there is
3260 * no filename in the namespace, and save any error returned.
3262 rc2 = mdo_ref_del(env, son, handle);
3264 mdd_write_unlock(env, son);
3265 if (unlikely(rc == 0))
3270 /* Don't destroy the volatile object on success */
3271 if (likely(rc != 0))
3272 mdo_destroy(env, son, handle);
3273 mdd_write_unlock(env, son);
3276 if (rc == 0 && fid_is_namespace_visible(mdd_object_fid(son)) &&
3277 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
3278 rc = mdd_changelog_ns_store(env, mdd,
3279 S_ISDIR(attr->la_mode) ? CL_MKDIR :
3280 S_ISREG(attr->la_mode) ? CL_CREATE :
3281 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
3282 0, son, mdd_pobj, pattr, NULL, NULL, NULL,
3283 lname, NULL, handle);
3285 rc2 = mdd_trans_stop(env, mdd, rc, handle);
3287 /* If creation fails, it is most likely due to the remote update
3288 * failure, because local transaction will mostly succeed at
3289 * this stage. There is no easy way to rollback all of previous
3290 * updates, so let's remove the object from namespace, and
3291 * LFSCK should handle the orphan object.
3293 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
3294 mdd_index_delete(env, mdd_pobj, attr, lname);
3298 if (is_vmalloc_addr(ldata->ld_buf))
3299 /* if we vmalloced a large buffer drop it */
3300 lu_buf_free(ldata->ld_buf);
3302 if (spec->sp_cr_flags & MDS_OPEN_PCC)
3303 lu_buf_free(&hsm_buf);
3305 /* The child object shouldn't be cached anymore */
3307 set_bit(LU_OBJECT_HEARD_BANSHEE,
3308 &child->mo_lu.lo_header->loh_flags);
3312 /* has not mdd_write{read}_lock on any obj yet. */
3313 static int mdd_rename_sanity_check(const struct lu_env *env,
3314 struct mdd_object *src_pobj,
3315 const struct lu_attr *spattr,
3316 struct mdd_object *tgt_pobj,
3317 const struct lu_attr *tpattr,
3318 struct mdd_object *sobj,
3319 const struct lu_attr *sattr,
3320 struct mdd_object *tobj,
3321 const struct lu_attr *tattr)
3327 /* XXX: when get here, sobj must NOT be NULL,
3328 * the other case has been processed in cld_rename
3329 * before mdd_rename and enable MDS_PERM_BYPASS.
3334 * If we are using project inheritance, we only allow renames
3335 * into our tree when the project IDs are the same; otherwise
3336 * tree quota mechanism would be circumvented.
3338 if ((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
3339 tpattr->la_projid != sattr->la_projid && S_ISDIR(sattr->la_mode))
3342 /* we prevent an encrypted file from being renamed
3343 * into an unencrypted dir
3345 if ((spattr->la_valid & LA_FLAGS &&
3346 spattr->la_flags & LUSTRE_ENCRYPT_FL) &&
3347 !(tpattr->la_valid & LA_FLAGS &&
3348 tpattr->la_flags & LUSTRE_ENCRYPT_FL))
3351 rc = mdd_may_delete(env, src_pobj, spattr, sobj, sattr, NULL, 1, 0);
3355 /* XXX: when get here, "tobj == NULL" means tobj must
3356 * NOT exist (neither on remote MDS, such case has been
3357 * processed in cld_rename before mdd_rename and enable
3359 * So check may_create, but not check may_unlink.
3362 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
3363 (src_pobj != tgt_pobj));
3365 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, sattr,
3366 (src_pobj != tgt_pobj), 1);
3368 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(sattr->la_mode))
3369 rc = __mdd_may_link(env, tgt_pobj, tpattr);
3375 int mdd_declare_rename(const struct lu_env *env, struct mdd_device *mdd,
3376 struct mdd_object *mdd_spobj, struct mdd_object *mdd_tpobj,
3377 struct mdd_object *mdd_sobj, struct mdd_object *mdd_tobj,
3378 const struct lu_name *sname, const struct lu_name *tname,
3379 struct md_attr *ma, struct linkea_data *ldata,
3380 bool change_projid, struct thandle *handle)
3382 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3385 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3386 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3392 /* name from source dir */
3393 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
3397 /* .. from source child */
3398 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3399 /* source child can be directory, count by source dir's nlink */
3400 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
3403 if (mdd_spobj != mdd_tpobj) {
3404 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
3409 rc = mdo_declare_index_insert(env, mdd_sobj,
3410 mdd_object_fid(mdd_tpobj),
3411 S_IFDIR, dotdot, handle);
3416 /* new target child can be dir, counted by target dir's nlink */
3417 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
3422 la->la_valid = LA_CTIME | LA_MTIME;
3423 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
3427 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
3431 la->la_valid = LA_CTIME;
3433 la->la_valid |= LA_PROJID;
3434 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3438 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
3443 rc = mdo_declare_index_insert(env, mdd_tpobj, mdd_object_fid(mdd_sobj),
3444 mdd_object_type(mdd_sobj),
3445 tname->ln_name, handle);
3449 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
3450 /* delete target child in target parent directory */
3451 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
3456 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3460 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
3461 /* target child can be directory,
3462 * delete "." reference in target child directory
3464 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
3468 /* delete ".." reference in target parent directory */
3469 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
3474 la->la_valid = LA_CTIME;
3475 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
3479 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
3484 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
3493 int mdd_migrate_object(const struct lu_env *env, struct mdd_object *spobj,
3494 struct mdd_object *tpobj, struct mdd_object *sobj,
3495 struct mdd_object *tobj, const struct lu_name *sname,
3496 const struct lu_name *tname, struct md_op_spec *spec,
3497 struct md_attr *ma);
3499 /* src object can be remote that is why we use only fid and type of object */
3500 static int mdd_rename(const struct lu_env *env, struct md_object *src_pobj,
3501 struct md_object *tgt_pobj, const struct lu_fid *lf,
3502 const struct lu_name *lsname, struct md_object *tobj,
3503 const struct lu_name *ltname, struct md_attr *ma)
3505 const char *sname = lsname->ln_name;
3506 const char *tname = ltname->ln_name;
3507 struct lu_attr *la = &mdd_env_info(env)->mdi_la_for_fix;
3508 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
3509 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj); /* target parent */
3510 struct mdd_object *mdd_sobj = NULL; /* source object */
3511 struct mdd_object *mdd_tobj = NULL; /* (possible) target object */
3512 struct lu_attr *sattr = MDD_ENV_VAR(env, cattr);
3513 struct lu_attr *spattr = MDD_ENV_VAR(env, pattr);
3514 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
3515 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
3516 struct linkea_data *ldata = &mdd_env_info(env)->mdi_link_data;
3517 const struct lu_fid *tpobj_fid = mdd_object_fid(mdd_tpobj);
3518 const struct lu_fid *spobj_fid = mdd_object_fid(mdd_spobj);
3519 struct mdd_device *mdd = mdo2mdd(src_pobj);
3520 struct thandle *handle;
3523 bool tobj_locked = 0;
3524 bool change_projid = false;
3525 unsigned int cl_flags = 0;
3530 /* let unlink to complete and commit */
3531 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
3534 mdd_tobj = md2mdd_obj(tobj);
3536 mdd_sobj = mdd_object_find(env, mdd, lf);
3537 if (IS_ERR(mdd_sobj))
3538 RETURN(PTR_ERR(mdd_sobj));
3540 rc = mdd_la_get(env, mdd_sobj, sattr);
3542 GOTO(out_pending, rc);
3544 /* if rename is cross MDTs, migrate symlink if it doesn't have other
3545 * hard links, and target doesn't exist.
3547 if (mdd_object_remote(mdd_sobj) && S_ISLNK(sattr->la_mode) &&
3548 sattr->la_nlink == 1 && !tobj) {
3549 struct md_op_spec *spec = &mdd_env_info(env)->mdi_spec;
3550 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
3553 rc = ld->ld_ops->ldo_fid_alloc(env, ld, &tfid, &tgt_pobj->mo_lu,
3556 GOTO(out_pending, rc);
3558 mdd_tobj = mdd_object_find(env, mdd, &tfid);
3559 if (IS_ERR(mdd_tobj))
3560 GOTO(out_pending, rc = PTR_ERR(mdd_tobj));
3562 memset(spec, 0, sizeof(*spec));
3563 rc = mdd_migrate_object(env, mdd_spobj, mdd_tpobj, mdd_sobj,
3564 mdd_tobj, lsname, ltname, spec, ma);
3565 mdd_object_put(env, mdd_tobj);
3566 GOTO(out_pending, rc);
3569 rc = mdd_la_get(env, mdd_spobj, spattr);
3571 GOTO(out_pending, rc);
3574 rc = mdd_la_get(env, mdd_tobj, tattr);
3576 GOTO(out_pending, rc);
3577 /* search for an existing archive. we should check ahead as the
3578 * object can be destroyed in this transaction
3580 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3581 cl_flags |= CLF_RENAME_LAST_EXISTS;
3584 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3586 GOTO(out_pending, rc);
3588 rc = mdd_rename_sanity_check(env, mdd_spobj, spattr, mdd_tpobj, tpattr,
3589 mdd_sobj, sattr, mdd_tobj, tattr);
3591 GOTO(out_pending, rc);
3593 rc = mdd_name_check(env, mdd, ltname);
3595 GOTO(out_pending, rc);
3597 handle = mdd_trans_create(env, mdd);
3599 GOTO(out_pending, rc = PTR_ERR(handle));
3601 memset(ldata, 0, sizeof(*ldata));
3602 rc = mdd_linkea_prepare(env, mdd_sobj, spobj_fid, lsname, tpobj_fid,
3603 ltname, 1, 0, ldata);
3607 if (tpattr->la_projid != sattr->la_projid &&
3608 tpattr->la_flags & LUSTRE_PROJINHERIT_FL)
3609 change_projid = true;
3611 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3612 mdd_tobj, lsname, ltname, ma, ldata,
3613 change_projid, handle);
3617 rc = mdd_trans_start(env, mdd, handle);
3621 is_dir = S_ISDIR(sattr->la_mode);
3623 /* Remove source name from source directory */
3624 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3628 /* "mv dir1 dir2" needs "dir1/.." link update */
3629 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3630 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3632 GOTO(fixup_spobj2, rc);
3634 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3637 GOTO(fixup_spobj, rc);
3640 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3641 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3643 /* tname might been renamed to something else */
3644 GOTO(fixup_spobj, rc);
3647 /* Insert new fid with target name into target dir */
3648 rc = __mdd_index_insert(env, mdd_tpobj, lf, sattr->la_mode,
3651 GOTO(fixup_tpobj, rc);
3653 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3654 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3656 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3657 la->la_valid = LA_CTIME;
3658 if (change_projid) {
3659 /* mdd_update_time honors other valid flags except TIME ones */
3660 la->la_valid |= LA_PROJID;
3661 la->la_projid = tpattr->la_projid;
3663 rc = mdd_update_time(env, mdd_sobj, sattr, la, handle);
3665 GOTO(fixup_tpobj, rc);
3667 /* Update the linkEA for the source object */
3668 mdd_write_lock(env, mdd_sobj, DT_SRC_CHILD);
3669 rc = mdd_links_rename(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3670 lsname, mdd_object_fid(mdd_tpobj), ltname,
3671 handle, ldata, 0, 0);
3673 /* Old files might not have EA entry */
3674 mdd_links_add(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3675 lsname, handle, NULL, 0);
3676 mdd_write_unlock(env, mdd_sobj);
3677 /* We don't fail the transaction if the link ea can't be
3678 * updated -- fid2path will use alternate lookup method.
3682 /* Remove old target object
3683 * For tobj is remote case cmm layer has processed
3684 * and set tobj to NULL then. So when tobj is NOT NULL,
3685 * it must be local one.
3687 if (tobj && mdd_object_exists(mdd_tobj)) {
3688 mdd_write_lock(env, mdd_tobj, DT_TGT_CHILD);
3690 if (mdd_is_dead_obj(mdd_tobj)) {
3691 /* should not be dead, something is wrong */
3693 CERROR("%s: something bad, dead tobj "DFID": rc = %d\n",
3694 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3697 mdo_ref_del(env, mdd_tobj, handle);
3699 /* Remove dot reference. */
3700 if (S_ISDIR(tattr->la_mode))
3701 mdo_ref_del(env, mdd_tobj, handle);
3704 /* fetch updated nlink */
3705 rc = mdd_la_get(env, mdd_tobj, tattr);
3707 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3708 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3709 GOTO(fixup_tpobj, rc);
3712 la->la_valid = LA_CTIME;
3713 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3715 CERROR("%s: failed set ctime of tobj "DFID": rc = %d\n",
3716 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3717 GOTO(fixup_tpobj, rc);
3720 /* XXX: this transfer to ma will be removed with LOD/OSP */
3721 ma->ma_attr = *tattr;
3722 ma->ma_valid |= MA_INODE;
3723 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3726 CERROR("%s: failed to unlink tobj "DFID": rc = %d\n",
3727 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3728 GOTO(fixup_tpobj, rc);
3731 /* fetch updated nlink */
3732 rc = mdd_la_get(env, mdd_tobj, tattr);
3733 if (rc == -ENOENT) {
3734 /* object removed? return the latest known attributes */
3735 tattr->la_nlink = 0;
3738 CERROR("%s: failed get nlink of tobj "DFID": rc = %d\n",
3739 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc);
3740 GOTO(fixup_tpobj, rc);
3742 /* XXX: this transfer to ma will be removed with LOD/OSP */
3743 ma->ma_attr = *tattr;
3744 ma->ma_valid |= MA_INODE;
3746 if (tattr->la_nlink == 0)
3747 cl_flags |= CLF_RENAME_LAST;
3749 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3752 la->la_valid = LA_CTIME | LA_MTIME;
3753 rc = mdd_update_time(env, mdd_spobj, spattr, la, handle);
3755 GOTO(fixup_tpobj, rc);
3757 if (mdd_spobj != mdd_tpobj) {
3758 la->la_valid = LA_CTIME | LA_MTIME;
3759 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3761 GOTO(fixup_tpobj, rc);
3768 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3770 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3771 mdd2obd_dev(mdd)->obd_name, PFID(tpobj_fid), rc2);
3773 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3774 !mdd_is_dead_obj(mdd_tobj)) {
3776 mdo_ref_add(env, mdd_tobj, handle);
3778 mdo_ref_add(env, mdd_tobj, handle);
3781 rc2 = __mdd_index_insert(env, mdd_tpobj,
3782 mdd_object_fid(mdd_tobj),
3783 mdd_object_type(mdd_tobj),
3786 CWARN("%s: tpobj "DFID" fix error: rc = %d\n",
3787 mdd2obd_dev(mdd)->obd_name,
3788 PFID(tpobj_fid), rc2);
3793 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3794 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3796 CWARN("%s: spobj "DFID" dotdot delete error: rc = %d\n",
3797 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3800 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3803 CWARN("%s: spobj "DFID" dotdot insert error: rc = %d\n",
3804 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3809 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3810 mdd_object_type(mdd_sobj), sname,
3813 CWARN("%s: spobj "DFID" fix error: rc = %d\n",
3814 mdd2obd_dev(mdd)->obd_name, PFID(spobj_fid), rc2);
3819 mdd_write_unlock(env, mdd_tobj);
3822 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3823 mdd_tobj, mdd_tpobj, tpattr, lf,
3824 mdd_spobj, spattr, ltname, lsname,
3828 rc = mdd_trans_stop(env, mdd, rc, handle);
3831 mdd_object_put(env, mdd_sobj);
3837 * Check whether we should migrate the file/dir
3839 * < 0 permission check failed or other error.
3840 * = 0 the file can be migrated.
3842 static int mdd_migrate_sanity_check(const struct lu_env *env,
3843 struct mdd_device *mdd,
3844 struct mdd_object *spobj,
3845 struct mdd_object *tpobj,
3846 struct mdd_object *sobj,
3847 struct mdd_object *tobj,
3848 const struct lu_attr *spattr,
3849 const struct lu_attr *tpattr,
3850 const struct lu_attr *attr,
3857 if (!nsonly && !mdd_object_remote(sobj)) {
3858 mdd_read_lock(env, sobj, DT_SRC_CHILD);
3859 if (sobj->mod_count > 0) {
3860 CDEBUG(D_INFO, "%s: "DFID" is opened, count %d\n",
3861 mdd_obj_dev_name(sobj),
3862 PFID(mdd_object_fid(sobj)),
3864 mdd_read_unlock(env, sobj);
3867 mdd_read_unlock(env, sobj);
3870 if (mdd_object_exists(tobj))
3873 rc = mdd_may_delete(env, spobj, spattr, sobj, attr, NULL, 1, 0);
3877 rc = mdd_may_create(env, tpobj, tpattr, NULL, true);
3882 struct mdd_xattr_entry {
3883 struct list_head mxe_linkage;
3885 struct lu_buf mxe_buf;
3889 struct lu_buf mx_namebuf;
3890 struct list_head mx_list;
3893 static inline void mdd_xattrs_init(struct mdd_xattrs *xattrs)
3895 INIT_LIST_HEAD(&xattrs->mx_list);
3896 xattrs->mx_namebuf.lb_buf = NULL;
3897 xattrs->mx_namebuf.lb_len = 0;
3900 static inline void mdd_xattrs_fini(struct mdd_xattrs *xattrs)
3902 struct mdd_xattr_entry *entry;
3903 struct mdd_xattr_entry *tmp;
3905 list_for_each_entry_safe(entry, tmp, &xattrs->mx_list, mxe_linkage) {
3906 lu_buf_free(&entry->mxe_buf);
3907 list_del(&entry->mxe_linkage);
3908 OBD_FREE_PTR(entry);
3911 lu_buf_free(&xattrs->mx_namebuf);
3914 /* read xattrs into buf, but skip LMA, LMV, LINKEA if 'skip_linkea' is
3915 * set, and DMV if 'skip_dmv" is set.
3917 static int mdd_xattrs_migrate_prep(const struct lu_env *env,
3918 struct mdd_xattrs *xattrs,
3919 struct mdd_object *sobj,
3923 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
3924 struct mdd_xattr_entry *entry;
3925 bool needencxattr = false;
3926 bool encxattrfound = false;
3936 list_xsize = mdo_xattr_list(env, sobj, &LU_BUF_NULL);
3937 if (list_xsize == -ENODATA)
3943 if (attr->la_valid & LA_FLAGS &&
3944 attr->la_flags & LUSTRE_ENCRYPT_FL) {
3945 needencxattr = true;
3947 strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
3950 lu_buf_alloc(&xattrs->mx_namebuf, list_xsize);
3951 if (xattrs->mx_namebuf.lb_buf == NULL)
3954 rc = mdo_xattr_list(env, sobj, &xattrs->mx_namebuf);
3960 xname = xattrs->mx_namebuf.lb_buf;
3962 for (; rem > 0; xname += xlen, rem -= xlen) {
3964 strcmp(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3965 encxattrfound = true;
3966 xlen = strnlen(xname, rem - 1) + 1;
3967 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3968 strcmp(XATTR_NAME_LMV, xname) == 0)
3972 strcmp(XATTR_NAME_LINK, xname) == 0)
3976 strcmp(XATTR_NAME_DEFAULT_LMV, xname) == 0)
3979 xsize = mdo_xattr_get(env, sobj, &LU_BUF_NULL, xname);
3980 if (xsize == -ENODATA)
3983 GOTO(fini, rc = xsize);
3985 OBD_ALLOC_PTR(entry);
3987 GOTO(fini, rc = -ENOMEM);
3989 lu_buf_alloc(&entry->mxe_buf, xsize);
3990 if (!entry->mxe_buf.lb_buf) {
3991 OBD_FREE_PTR(entry);
3992 GOTO(fini, rc = -ENOMEM);
3995 rc = mdo_xattr_get(env, sobj, &entry->mxe_buf, xname);
3997 lu_buf_free(&entry->mxe_buf);
3998 OBD_FREE_PTR(entry);
4004 entry->mxe_name = xname;
4005 list_add_tail(&entry->mxe_linkage, &xattrs->mx_list);
4008 if (needencxattr && !encxattrfound) {
4009 xlen = strlen(LL_XATTR_NAME_ENCRYPTION_CONTEXT) + 1;
4010 strncpy(xname, LL_XATTR_NAME_ENCRYPTION_CONTEXT, xlen);
4017 mdd_xattrs_fini(xattrs);
4021 typedef int (*mdd_xattr_cb)(const struct lu_env *env,
4022 struct mdd_object *obj,
4023 const struct lu_buf *buf,
4025 int fl, struct thandle *handle);
4027 static int mdd_foreach_xattr(const struct lu_env *env,
4028 struct mdd_object *tobj,
4029 struct mdd_xattrs *xattrs,
4030 struct thandle *handle,
4033 struct mdd_xattr_entry *entry;
4036 list_for_each_entry(entry, &xattrs->mx_list, mxe_linkage) {
4037 rc = cb(env, tobj, &entry->mxe_buf, entry->mxe_name, 0, handle);
4045 typedef int (*mdd_linkea_cb)(const struct lu_env *env,
4046 struct mdd_object *sobj,
4047 struct mdd_object *tobj,
4048 const struct lu_name *sname,
4049 const struct lu_fid *sfid,
4050 const struct lu_name *lname,
4051 const struct lu_fid *fid,
4053 struct thandle *handle);
4055 static int mdd_declare_update_link(const struct lu_env *env,
4056 struct mdd_object *sobj,
4057 struct mdd_object *tobj,
4058 const struct lu_name *tname,
4059 const struct lu_fid *tpfid,
4060 const struct lu_name *lname,
4061 const struct lu_fid *fid,
4063 struct thandle *handle)
4065 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4066 struct mdd_object *pobj;
4070 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4071 !strcmp(tname->ln_name, lname->ln_name))
4074 pobj = mdd_object_find(env, mdd, fid);
4076 return PTR_ERR(pobj);
4079 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
4081 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
4082 mdd_object_type(sobj),
4083 lname->ln_name, handle);
4084 mdd_object_put(env, pobj);
4088 rc = mdo_declare_ref_add(env, tobj, handle);
4092 rc = mdo_declare_ref_del(env, sobj, handle);
4096 static int mdd_update_link(const struct lu_env *env,
4097 struct mdd_object *sobj,
4098 struct mdd_object *tobj,
4099 const struct lu_name *tname,
4100 const struct lu_fid *tpfid,
4101 const struct lu_name *lname,
4102 const struct lu_fid *fid,
4104 struct thandle *handle)
4106 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4107 struct mdd_object *pobj;
4113 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4114 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
4117 CDEBUG(D_INFO, "update "DFID"/"DNAME":"DFID"\n",
4118 PFID(fid), encode_fn_luname(lname), PFID(mdd_object_fid(tobj)));
4120 pobj = mdd_object_find(env, mdd, fid);
4122 CWARN("%s: cannot find obj "DFID": %ld\n",
4123 mdd2obd_dev(mdd)->obd_name, PFID(fid), PTR_ERR(pobj));
4124 RETURN(PTR_ERR(pobj));
4127 if (!mdd_object_exists(pobj)) {
4128 CDEBUG(D_INFO, DFID" doesn't exist\n", PFID(fid));
4129 mdd_object_put(env, pobj);
4133 mdd_write_lock(env, pobj, DT_TGT_PARENT);
4134 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
4136 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(tobj),
4137 mdd_object_type(sobj),
4138 lname->ln_name, handle);
4139 mdd_write_unlock(env, pobj);
4140 mdd_object_put(env, pobj);
4144 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4145 rc = mdo_ref_add(env, tobj, handle);
4146 mdd_write_unlock(env, tobj);
4150 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4151 rc = mdo_ref_del(env, sobj, handle);
4152 mdd_write_unlock(env, sobj);
4157 static inline int mdd_fld_lookup(const struct lu_env *env,
4158 struct mdd_device *mdd,
4159 const struct lu_fid *fid,
4162 struct lu_seq_range *range = &mdd_env_info(env)->mdi_range;
4163 struct seq_server_site *ss;
4166 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4168 range->lsr_flags = LU_SEQ_RANGE_MDT;
4169 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4173 *mdt_index = range->lsr_index;
4178 static int mdd_is_link_on_source_mdt(const struct lu_env *env,
4179 struct mdd_object *sobj,
4180 struct mdd_object *tobj,
4181 const struct lu_name *tname,
4182 const struct lu_fid *tpfid,
4183 const struct lu_name *lname,
4184 const struct lu_fid *fid,
4186 struct thandle *handle)
4188 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4189 __u32 source_mdt_index = *(__u32 *)opaque;
4190 __u32 link_mdt_index;
4196 if (lu_fid_eq(tpfid, fid) && tname->ln_namelen == lname->ln_namelen &&
4197 !memcmp(tname->ln_name, lname->ln_name, lname->ln_namelen))
4200 rc = mdd_fld_lookup(env, mdd, fid, &link_mdt_index);
4204 RETURN(link_mdt_index == source_mdt_index);
4207 static int mdd_iterate_linkea(const struct lu_env *env,
4208 struct mdd_object *sobj,
4209 struct mdd_object *tobj,
4210 const struct lu_name *tname,
4211 const struct lu_fid *tpfid,
4212 struct linkea_data *ldata,
4214 struct thandle *handle,
4217 struct mdd_thread_info *info = mdd_env_info(env);
4218 char *filename = info->mdi_name;
4219 struct lu_name lname;
4226 for (linkea_first_entry(ldata); ldata->ld_lee && !rc;
4227 linkea_next_entry(ldata)) {
4228 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
4231 /* Note: lname might miss \0 at the end */
4232 snprintf(filename, sizeof(info->mdi_name), DNAME,
4233 lname.ln_namelen, lname.ln_name);
4234 lname.ln_name = filename;
4236 CDEBUG(D_INFO, DFID"/"DNAME"\n",
4237 PFID(&fid), encode_fn_luname(&lname));
4239 rc = cb(env, sobj, tobj, tname, tpfid, &lname, &fid, opaque,
4247 * Prepare linkea, and check whether file needs migrate: if source still has
4248 * link on source MDT, no need to migrate, just update namespace on source and
4251 * \retval 0 do migrate
4252 * \retval 1 don't migrate
4253 * \retval -errno on failure
4255 static int mdd_migrate_linkea_prepare(const struct lu_env *env,
4256 struct mdd_device *mdd,
4257 struct mdd_object *spobj,
4258 struct mdd_object *tpobj,
4259 struct mdd_object *sobj,
4260 const struct lu_name *sname,
4261 const struct lu_name *tname,
4262 const struct lu_attr *attr,
4263 struct linkea_data *ldata)
4265 __u32 source_mdt_index;
4270 memset(ldata, 0, sizeof(*ldata));
4271 rc = mdd_linkea_prepare(env, sobj, mdd_object_fid(spobj), sname,
4272 mdd_object_fid(tpobj), tname, 1, 0, ldata);
4277 * Then it will check if the file should be migrated. If the file has
4278 * mulitple links, we only need migrate the file if all of its entries
4279 * has been migrated to the remote MDT.
4281 if (S_ISDIR(attr->la_mode) || attr->la_nlink < 2)
4284 /* If there are still links locally, don't migrate this file */
4285 LASSERT(ldata->ld_leh != NULL);
4287 /* If linkEA is overflow, switch to ns-only migrate */
4288 if (unlikely(ldata->ld_leh->leh_overflow_time))
4291 rc = mdd_fld_lookup(env, mdd, mdd_object_fid(sobj), &source_mdt_index);
4295 rc = mdd_iterate_linkea(env, sobj, NULL, tname, mdd_object_fid(tpobj),
4296 ldata, &source_mdt_index, NULL,
4297 mdd_is_link_on_source_mdt);
4301 static int mdd_declare_migrate_update(const struct lu_env *env,
4302 struct mdd_object *spobj,
4303 struct mdd_object *tpobj,
4304 struct mdd_object *obj,
4305 const struct lu_name *sname,
4306 const struct lu_name *tname,
4307 struct lu_attr *attr,
4308 struct lu_attr *spattr,
4309 struct lu_attr *tpattr,
4310 struct linkea_data *ldata,
4312 struct thandle *handle)
4314 struct mdd_thread_info *info = mdd_env_info(env);
4315 struct lu_attr *la = &info->mdi_la_for_fix;
4318 rc = mdo_declare_index_delete(env, spobj, sname->ln_name, handle);
4322 if (S_ISDIR(attr->la_mode)) {
4323 rc = mdo_declare_ref_del(env, spobj, handle);
4328 rc = mdo_declare_index_insert(env, tpobj, mdd_object_fid(obj),
4329 attr->la_mode & S_IFMT,
4330 tname->ln_name, handle);
4334 rc = mdd_declare_links_add(env, obj, handle, ldata);
4338 if (S_ISDIR(attr->la_mode)) {
4339 rc = mdo_declare_ref_add(env, tpobj, handle);
4344 la->la_valid = LA_CTIME | LA_MTIME;
4345 rc = mdo_declare_attr_set(env, spobj, la, handle);
4349 if (tpobj != spobj) {
4350 rc = mdo_declare_attr_set(env, tpobj, la, handle);
4358 static int mdd_declare_migrate_create(const struct lu_env *env,
4359 struct mdd_object *spobj,
4360 struct mdd_object *tpobj,
4361 struct mdd_object *sobj,
4362 struct mdd_object *tobj,
4363 const struct lu_name *sname,
4364 const struct lu_name *tname,
4365 struct lu_attr *spattr,
4366 struct lu_attr *tpattr,
4367 struct lu_attr *attr,
4368 struct lu_buf *sbuf,
4369 struct linkea_data *ldata,
4370 struct mdd_xattrs *xattrs,
4372 struct md_op_spec *spec,
4373 struct dt_allocation_hint *hint,
4374 struct thandle *handle)
4376 struct mdd_thread_info *info = mdd_env_info(env);
4377 struct md_layout_change *mlc = &info->mdi_mlc;
4378 struct lmv_mds_md_v1 *lmv = sbuf->lb_buf;
4383 if (S_ISDIR(attr->la_mode)) {
4384 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
4386 mlc->mlc_opc = MD_LAYOUT_DETACH;
4387 rc = mdo_declare_layout_change(env, sobj, mlc, handle);
4391 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
4392 } else if (S_ISLNK(attr->la_mode)) {
4393 spec->u.sp_symname.ln_name = sbuf->lb_buf;
4394 /* don't count NUL */
4395 spec->u.sp_symname.ln_namelen = sbuf->lb_len - 1;
4396 } else if (S_ISREG(attr->la_mode)) {
4397 spec->sp_cr_flags |= MDS_OPEN_DELAY_CREATE;
4398 spec->sp_cr_flags &= ~MDS_OPEN_HAS_EA;
4401 mdd_object_make_hint(env, tpobj, tobj, attr, spec, hint);
4403 rc = mdd_declare_create(env, mdo2mdd(&tpobj->mod_obj), tpobj, tobj,
4404 tname, attr, handle, spec, ldata, NULL, NULL,
4410 * tobj mode will be used in mdo_declare_layout_change(), but it's not
4411 * createb yet, copy from sobj.
4413 tobj->mod_obj.mo_lu.lo_header->loh_attr &= ~S_IFMT;
4414 tobj->mod_obj.mo_lu.lo_header->loh_attr |=
4415 sobj->mod_obj.mo_lu.lo_header->loh_attr & S_IFMT;
4417 if (S_ISDIR(attr->la_mode)) {
4419 /* if sobj is not striped, fake a 1-stripe LMV */
4420 LASSERT(sizeof(info->mdi_key) >
4421 lmv_mds_md_size(1, LMV_MAGIC_V1));
4422 lmv = (typeof(lmv))info->mdi_key;
4423 memset(lmv, 0, sizeof(*lmv));
4424 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
4425 lmv->lmv_stripe_count = cpu_to_le32(1);
4426 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
4427 fid_le_to_cpu(&lmv->lmv_stripe_fids[0],
4428 mdd_object_fid(sobj));
4429 mlc->mlc_buf.lb_buf = lmv;
4430 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
4432 mlc->mlc_buf = *sbuf;
4434 mlc->mlc_opc = MD_LAYOUT_ATTACH;
4435 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
4440 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
4441 mdo_declare_xattr_set);
4445 if (S_ISREG(attr->la_mode)) {
4446 struct lu_buf fid_buf;
4448 handle->th_complex = 1;
4450 /* target may be remote, update PFID via sobj. */
4451 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4452 fid_buf.lb_len = sizeof(struct lu_fid);
4453 rc = mdo_declare_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID,
4458 rc = mdo_declare_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4463 if (!S_ISDIR(attr->la_mode)) {
4464 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4465 mdd_object_fid(tpobj), ldata, NULL,
4466 handle, mdd_declare_update_link);
4471 if (!S_ISDIR(attr->la_mode) || lmv) {
4472 rc = mdo_declare_ref_del(env, sobj, handle);
4476 if (S_ISDIR(attr->la_mode)) {
4477 rc = mdo_declare_ref_del(env, sobj, handle);
4482 rc = mdo_declare_destroy(env, sobj, handle);
4487 rc = mdd_declare_migrate_update(env, spobj, tpobj, tobj, sname, tname,
4488 attr, spattr, tpattr, ldata, ma,
4494 * migrate dirent from \a spobj to \a tpobj.
4496 static int mdd_migrate_update(const struct lu_env *env,
4497 struct mdd_object *spobj,
4498 struct mdd_object *tpobj,
4499 struct mdd_object *obj,
4500 const struct lu_name *sname,
4501 const struct lu_name *tname,
4502 struct lu_attr *attr,
4503 struct lu_attr *spattr,
4504 struct lu_attr *tpattr,
4505 struct linkea_data *ldata,
4507 struct thandle *handle)
4509 struct mdd_thread_info *info = mdd_env_info(env);
4510 struct lu_attr *la = &info->mdi_la_for_fix;
4515 CDEBUG(D_INFO, "update "DFID" from "DFID"/"DNAME" to "DFID"/"DNAME"\n",
4516 PFID(mdd_object_fid(obj)), PFID(mdd_object_fid(spobj)),
4517 encode_fn_luname(sname), PFID(mdd_object_fid(tpobj)),
4518 encode_fn_luname(tname));
4520 rc = __mdd_index_delete(env, spobj, sname->ln_name,
4521 S_ISDIR(attr->la_mode), handle);
4525 rc = __mdd_index_insert(env, tpobj, mdd_object_fid(obj),
4526 attr->la_mode & S_IFMT,
4527 tname->ln_name, handle);
4531 rc = mdd_links_write(env, obj, ldata, handle);
4535 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
4536 la->la_valid = LA_CTIME | LA_MTIME;
4537 mdd_write_lock(env, spobj, DT_SRC_PARENT);
4538 rc = mdd_update_time(env, spobj, spattr, la, handle);
4539 mdd_write_unlock(env, spobj);
4543 if (tpobj != spobj) {
4544 la->la_valid = LA_CTIME | LA_MTIME;
4545 mdd_write_lock(env, tpobj, DT_TGT_PARENT);
4546 rc = mdd_update_time(env, tpobj, tpattr, la, handle);
4547 mdd_write_unlock(env, tpobj);
4556 * Migrate file/dir to target MDT.
4558 * Create target according to \a spec, and then migrate xattrs, if it's
4559 * directory, migrate source stripes to target.
4561 * \param[in] env execution environment
4562 * \param[in] spobj source parent object
4563 * \param[in] tpobj target parent object
4564 * \param[in] sobj source object
4565 * \param[in] tobj target object
4566 * \param[in] lname file name
4567 * \param[in] spattr source parent attributes
4568 * \param[in] tpattr target parent attributes
4569 * \param[in] attr source attributes
4570 * \param[in] sbuf source LMV buf
4571 * \param[in] spec migrate create spec
4572 * \param[in] hint target creation hint
4573 * \param[in] handle tranasction handle
4575 * \retval 0 on success
4576 * \retval -errno on failure
4578 static int mdd_migrate_create(const struct lu_env *env,
4579 struct mdd_object *spobj,
4580 struct mdd_object *tpobj,
4581 struct mdd_object *sobj,
4582 struct mdd_object *tobj,
4583 const struct lu_name *sname,
4584 const struct lu_name *tname,
4585 struct lu_attr *spattr,
4586 struct lu_attr *tpattr,
4587 struct lu_attr *attr,
4588 const struct lu_buf *sbuf,
4589 struct linkea_data *ldata,
4590 struct mdd_xattrs *xattrs,
4592 struct md_op_spec *spec,
4593 struct dt_allocation_hint *hint,
4594 struct thandle *handle)
4601 * migrate sobj stripes to tobj if it's directory:
4602 * 1. detach stripes from sobj.
4603 * 2. attach stripes to tobj, see mdd_declare_migrate_mdt().
4604 * 3. create stripes for tobj, see lod_xattr_set_lmv().
4606 if (S_ISDIR(attr->la_mode)) {
4607 struct mdd_thread_info *info = mdd_env_info(env);
4608 struct md_layout_change *mlc = &info->mdi_mlc;
4610 mlc->mlc_opc = MD_LAYOUT_DETACH;
4612 mdd_write_lock(env, sobj, DT_SRC_PARENT);
4613 rc = mdo_layout_change(env, sobj, mlc, handle);
4614 mdd_write_unlock(env, sobj);
4619 /* don't set nlink from sobj */
4620 attr->la_valid &= ~LA_NLINK;
4622 rc = mdd_create_object(env, tpobj, tobj, attr, spec, NULL, NULL, NULL,
4623 hint, handle, false);
4627 mdd_write_lock(env, tobj, DT_TGT_CHILD);
4628 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
4629 mdd_write_unlock(env, tobj);
4633 /* for regular file, update OST objects XATTR_NAME_FID */
4634 if (S_ISREG(attr->la_mode)) {
4635 struct lu_buf fid_buf;
4637 /* target may be remote, update PFID via sobj. */
4638 fid_buf.lb_buf = (void *)mdd_object_fid(tobj);
4639 fid_buf.lb_len = sizeof(struct lu_fid);
4640 rc = mdo_xattr_set(env, sobj, &fid_buf, XATTR_NAME_FID, 0,
4645 /* delete LOV to avoid deleting OST objs when destroying sobj */
4646 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4647 rc = mdo_xattr_del(env, sobj, XATTR_NAME_LOV, handle);
4648 mdd_write_unlock(env, sobj);
4649 /* O_DELAY_CREATE file may not have LOV, ignore -ENODATA */
4650 if (rc && rc != -ENODATA)
4655 /* update links FID */
4656 if (!S_ISDIR(attr->la_mode)) {
4657 rc = mdd_iterate_linkea(env, sobj, tobj, tname,
4658 mdd_object_fid(tpobj), ldata,
4659 NULL, handle, mdd_update_link);
4664 /* don't destroy sobj if it's plain directory */
4665 if (!S_ISDIR(attr->la_mode) || sbuf->lb_buf) {
4666 mdd_write_lock(env, sobj, DT_SRC_CHILD);
4667 rc = mdo_ref_del(env, sobj, handle);
4669 if (S_ISDIR(attr->la_mode))
4670 rc = mdo_ref_del(env, sobj, handle);
4672 rc = mdo_destroy(env, sobj, handle);
4674 mdd_write_unlock(env, sobj);
4679 rc = mdd_migrate_update(env, spobj, tpobj, tobj, sname, tname, attr,
4680 spattr, tpattr, ldata, ma, handle);
4685 /* NB: if user issued different migrate command, we can't adjust it silently
4686 * here, because this command will decide target MDT in subdir migration in
4689 static int mdd_migrate_cmd_check(const struct lu_env *env, struct mdd_device *mdd,
4690 struct mdd_object *sobj,
4691 const struct lmv_mds_md_v1 *lmv,
4692 const struct lmv_user_md_v1 *lum,
4693 size_t lum_len, const struct lu_name *lname)
4695 struct mdd_thread_info *info = mdd_env_info(env);
4696 __u32 lum_stripe_count = lum->lum_stripe_count;
4697 __u32 lum_hash_type = lum->lum_hash_type &
4698 cpu_to_le32(LMV_HASH_TYPE_MASK);
4699 struct md_layout_change *mlc = &info->mdi_mlc;
4700 __u32 lmv_hash_type;
4704 if (lmv && !lmv_is_sane(lmv))
4707 /* If stripe_count unspecified, set to 1 */
4708 if (!lum_stripe_count)
4709 lum_stripe_count = cpu_to_le32(1);
4711 /* Easy check for plain and single-striped dirs
4712 * if the object is on the target MDT already
4714 if (!lmv || lmv->lmv_stripe_count == cpu_to_le32(1)) {
4715 struct seq_server_site *ss = mdd_seq_site(mdd);
4716 struct lu_seq_range range = { 0 };
4718 fld_range_set_type(&range, LU_SEQ_RANGE_MDT);
4719 rc = fld_server_lookup(env, ss->ss_server_fld,
4720 fid_seq(mdd_object_fid(sobj)), &range);
4724 if (lum_stripe_count == cpu_to_le32(1) &&
4725 le32_to_cpu(lum->lum_stripe_offset) == range.lsr_index)
4730 lmv_hash_type = lmv->lmv_hash_type & cpu_to_le32(LMV_HASH_TYPE_MASK);
4732 if (lmv_is_migrating(lmv)) {
4733 if (lum_stripe_count != lmv->lmv_migrate_offset ||
4734 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4735 (lum_hash_type && lum_hash_type != lmv_hash_type)) {
4739 /* check at top level if the target layout already applied */
4740 if ((lum_hash_type && lum_hash_type != lmv_hash_type) ||
4741 lum->lum_stripe_offset != lmv->lmv_master_mdt_index ||
4742 lum_stripe_count != lmv->lmv_stripe_count)
4747 mlc->mlc_buf.lb_buf = (void*)lum;
4748 mlc->mlc_buf.lb_len = lum_len;
4749 rc = mo_layout_check(env, &sobj->mod_obj, mlc);
4753 CERROR("%s: '"DNAME"' migration was interrupted, run "
4754 "'lfs migrate -m %d -c %d -H %s "DNAME"' to finish migration: rc = %d\n",
4755 mdd2obd_dev(mdd)->obd_name, encode_fn_luname(lname),
4756 le32_to_cpu(lmv->lmv_master_mdt_index),
4757 le32_to_cpu(lmv->lmv_migrate_offset),
4758 mdt_hash_name[le32_to_cpu(lmv_hash_type)],
4759 encode_fn_luname(lname), rc);
4766 * Internal function to migrate directory or file between MDTs.
4768 * migrate source to target in following steps:
4769 * 1. create target, append source stripes after target's if it's directory,
4770 * migrate xattrs and update fid of source links.
4771 * 2. update namespace: migrate dirent from source parent to target parent,
4772 * update file linkea, and destroy source if it's not needed any more.
4774 * \param[in] env execution environment
4775 * \param[in] spobj source parent object
4776 * \param[in] tpobj target parent object
4777 * \param[in] sobj source object
4778 * \param[in] tobj target object
4779 * \param[in] sname source file name
4780 * \param[in] tname target file name
4781 * \param[in] spec target creation spec
4782 * \param[in] ma used to update \a pobj mtime and ctime
4784 * \retval 0 on success
4785 * \retval -errno on failure
4787 static int mdd_migrate_object(const struct lu_env *env,
4788 struct mdd_object *spobj,
4789 struct mdd_object *tpobj,
4790 struct mdd_object *sobj,
4791 struct mdd_object *tobj,
4792 const struct lu_name *sname,
4793 const struct lu_name *tname,
4794 struct md_op_spec *spec,
4797 struct mdd_thread_info *info = mdd_env_info(env);
4798 struct mdd_device *mdd = mdo2mdd(&spobj->mod_obj);
4799 struct lu_attr *spattr = &info->mdi_pattr;
4800 struct lu_attr *tpattr = &info->mdi_tpattr;
4801 struct lu_attr *attr = &info->mdi_cattr;
4802 struct linkea_data *ldata = &info->mdi_link_data;
4803 struct dt_allocation_hint *hint = &info->mdi_hint;
4804 struct lu_buf sbuf = { NULL };
4805 struct mdd_xattrs xattrs;
4806 struct lmv_mds_md_v1 *lmv;
4807 struct thandle *handle;
4813 CDEBUG(D_INFO, "migrate "DNAME" from "DFID"/"DFID" to "DFID"/"DFID"\n",
4814 encode_fn_luname(sname), PFID(mdd_object_fid(spobj)),
4815 PFID(mdd_object_fid(sobj)), PFID(mdd_object_fid(tpobj)),
4816 PFID(mdd_object_fid(tobj)));
4819 rc = mdd_la_get(env, sobj, attr);
4823 rc = mdd_la_get(env, spobj, spattr);
4827 rc = mdd_la_get(env, tpobj, tpattr);
4831 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj, tobj,
4832 spattr, tpattr, attr,
4833 spec->sp_migrate_nsonly);
4834 if (rc == -EBUSY && !spec->sp_migrate_nsonly) {
4835 spec->sp_migrate_nsonly = 1;
4836 CWARN("%s: "DFID"/%s is open, migrate only dentry\n",
4837 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(spobj)),
4839 rc = mdd_migrate_sanity_check(env, mdd, spobj, tpobj, sobj,
4840 tobj, spattr, tpattr, attr,
4841 spec->sp_migrate_nsonly);
4846 mdd_xattrs_init(&xattrs);
4848 if (S_ISDIR(attr->la_mode) && !spec->sp_migrate_nsonly) {
4849 struct lmv_user_md_v1 *lum = spec->u.sp_ea.eadata;
4850 size_t lum_len = spec->u.sp_ea.eadatalen;
4854 /* if user use default value '0' for stripe_count, we need to
4855 * adjust it to '1' to create a 1-stripe directory.
4857 if (lum->lum_stripe_count == 0)
4858 lum->lum_stripe_count = cpu_to_le32(1);
4860 rc = mdd_stripe_get(env, sobj, &sbuf, XATTR_NAME_LMV);
4861 if (rc && rc != -ENODATA)
4865 rc = mdd_migrate_cmd_check(env, mdd, sobj, lmv, lum,
4869 } else if (!S_ISDIR(attr->la_mode)) {
4870 /* update namespace only if @sobj is on MDT where @tpobj is. */
4871 if (!mdd_object_remote(tpobj) && !mdd_object_remote(sobj))
4872 spec->sp_migrate_nsonly = true;
4874 if (S_ISLNK(attr->la_mode)) {
4875 lu_buf_check_and_alloc(&sbuf, attr->la_size + 1);
4877 GOTO(out, rc = -ENOMEM);
4879 rc = mdd_readlink(env, &sobj->mod_obj, &sbuf);
4882 CERROR("%s: "DFID" readlink failed: rc = %d\n",
4883 mdd2obd_dev(mdd)->obd_name,
4884 PFID(mdd_object_fid(sobj)), rc);
4890 /* linkea needs update upon FID or parent stripe change */
4891 rc = mdd_migrate_linkea_prepare(env, mdd, spobj, tpobj, sobj, sname,
4892 tname, attr, ldata);
4894 /* update namespace only if @sobj has link on its MDT. */
4895 spec->sp_migrate_nsonly = true;
4899 /* migrate inode will migrate xattrs, prepare xattrs early to avoid
4900 * RPCs inside transaction.
4902 if (!spec->sp_migrate_nsonly) {
4903 rc = mdd_xattrs_migrate_prep(env, &xattrs, sobj, true, true);
4908 handle = mdd_trans_create(env, mdd);
4910 GOTO(out, rc = PTR_ERR(handle));
4912 if (spec->sp_migrate_nsonly)
4913 rc = mdd_declare_migrate_update(env, spobj, tpobj, sobj, sname,
4914 tname, attr, spattr, tpattr,
4917 rc = mdd_declare_migrate_create(env, spobj, tpobj, sobj, tobj,
4918 sname, tname, spattr, tpattr,
4919 attr, &sbuf, ldata, &xattrs, ma,
4920 spec, hint, handle);
4924 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, tname, sname,
4929 rc = mdd_trans_start(env, mdd, handle);
4933 if (spec->sp_migrate_nsonly)
4934 rc = mdd_migrate_update(env, spobj, tpobj, sobj, sname, tname,
4935 attr, spattr, tpattr, ldata, ma,
4938 rc = mdd_migrate_create(env, spobj, tpobj, sobj, tobj, sname,
4939 tname, spattr, tpattr, attr, &sbuf,
4940 ldata, &xattrs, ma, spec, hint, handle);
4944 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0,
4945 spec->sp_migrate_nsonly ? sobj : tobj,
4946 spobj, spattr, mdd_object_fid(sobj),
4947 tpobj, tpattr, tname, sname,
4951 rc = mdd_trans_stop(env, mdd, rc, handle);
4953 mdd_xattrs_fini(&xattrs);
4957 * -EAGAIN means transaction execution phase detect the layout
4958 * has been changed by others.
4960 if (rc == -EAGAIN && retried++ < MAX_TRANS_RETRIED)
4961 GOTO(retry, retried);
4967 * Migrate directory or file between MDTs.
4969 * \param[in] env execution environment
4970 * \param[in] md_spobj source parent object
4971 * \param[in] md_tpobj target parent object
4972 * \param[in] md_sobj source object
4973 * \param[in] lname file name
4974 * \param[in] md_tobj target object
4975 * \param[in] spec target creation spec
4976 * \param[in] ma used to update \a pobj mtime and ctime
4978 * \retval 0 on success
4979 * \retval -errno on failure
4981 static int mdd_migrate(const struct lu_env *env, struct md_object *md_spobj,
4982 struct md_object *md_tpobj, struct md_object *md_sobj,
4983 struct md_object *md_tobj, const struct lu_name *lname,
4984 struct md_op_spec *spec, struct md_attr *ma)
4986 return mdd_migrate_object(env, md2mdd_obj(md_spobj),
4987 md2mdd_obj(md_tpobj), md2mdd_obj(md_sobj),
4988 md2mdd_obj(md_tobj), lname, lname, spec, ma);
4991 static int mdd_declare_1sd_collapse(const struct lu_env *env,
4992 struct mdd_object *pobj,
4993 struct mdd_object *obj,
4994 struct mdd_object *stripe,
4995 struct lu_attr *attr,
4996 struct mdd_xattrs *xattrs,
4997 struct md_layout_change *mlc,
4998 struct lu_name *lname,
4999 struct thandle *handle)
5003 mlc->mlc_opc = MD_LAYOUT_DETACH;
5004 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5008 rc = mdo_declare_index_insert(env, stripe, mdd_object_fid(pobj),
5009 S_IFDIR, dotdot, handle);
5013 rc = mdd_foreach_xattr(env, stripe, xattrs, handle,
5014 mdo_declare_xattr_set);
5018 rc = mdo_declare_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
5022 rc = mdo_declare_attr_set(env, stripe, attr, handle);
5026 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5030 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(stripe),
5031 attr->la_mode, lname->ln_name, handle);
5035 rc = mdo_declare_ref_del(env, obj, handle);
5039 rc = mdo_declare_ref_del(env, obj, handle);
5043 rc = mdo_declare_destroy(env, obj, handle);
5050 /* transform one-stripe directory to a plain directory */
5051 static int mdd_1sd_collapse(const struct lu_env *env,
5052 struct mdd_object *pobj,
5053 struct mdd_object *obj,
5054 struct mdd_object *stripe,
5055 struct lu_attr *attr,
5056 struct mdd_xattrs *xattrs,
5057 struct md_layout_change *mlc,
5058 struct lu_name *lname,
5059 struct thandle *handle)
5065 /* replace 1-stripe directory with its stripe */
5066 mlc->mlc_opc = MD_LAYOUT_DETACH;
5068 mdd_write_lock(env, obj, DT_SRC_PARENT);
5069 rc = mdo_layout_change(env, obj, mlc, handle);
5070 mdd_write_unlock(env, obj);
5074 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5075 mdd_write_lock(env, obj, DT_SRC_CHILD);
5077 /* insert dotdot to stripe which points to parent */
5078 rc = __mdd_index_insert_only(env, stripe, mdd_object_fid(pobj),
5079 S_IFDIR, dotdot, handle);
5083 rc = mdd_foreach_xattr(env, stripe, xattrs, handle, mdo_xattr_set);
5088 rc = mdo_xattr_del(env, stripe, XATTR_NAME_LMV, handle);
5092 /* don't set nlink from parent */
5093 attr->la_valid &= ~LA_NLINK;
5095 rc = mdo_attr_set(env, stripe, attr, handle);
5099 /* delete dir name from parent */
5100 rc = __mdd_index_delete_only(env, pobj, lname->ln_name, handle);
5104 /* insert stripe to parent with dir name */
5105 rc = __mdd_index_insert_only(env, pobj, mdd_object_fid(stripe),
5106 attr->la_mode, lname->ln_name, handle);
5110 /* destroy dir obj */
5111 rc = mdo_ref_del(env, obj, handle);
5115 rc = mdo_ref_del(env, obj, handle);
5119 rc = mdo_destroy(env, obj, handle);
5125 mdd_write_unlock(env, obj);
5126 mdd_write_unlock(env, pobj);
5132 * shrink directory stripes after migration/merge
5134 int mdd_dir_layout_shrink(const struct lu_env *env,
5135 struct md_object *md_obj,
5136 struct md_layout_change *mlc)
5138 struct mdd_device *mdd = mdo2mdd(md_obj);
5139 struct mdd_thread_info *info = mdd_env_info(env);
5140 struct mdd_object *obj = md2mdd_obj(md_obj);
5141 struct mdd_object *pobj = NULL;
5142 struct mdd_object *stripe = NULL;
5143 struct lu_attr *attr = &info->mdi_pattr;
5144 struct lu_fid *fid = &info->mdi_fid2;
5145 struct lu_name lname = { NULL };
5146 struct lu_buf lmv_buf = { NULL };
5147 struct mdd_xattrs xattrs;
5148 struct lmv_mds_md_v1 *lmv;
5149 struct lmv_user_md *lmu;
5150 struct thandle *handle;
5155 rc = mdd_la_get(env, obj, attr);
5159 if (!S_ISDIR(attr->la_mode))
5162 rc = mdd_stripe_get(env, obj, &lmv_buf, XATTR_NAME_LMV);
5166 lmv = lmv_buf.lb_buf;
5167 if (!lmv_is_sane(lmv))
5168 GOTO(out_lmv, rc = -EBADF);
5170 lmu = mlc->mlc_buf.lb_buf;
5172 /* adjust the default value '0' to '1' */
5173 if (lmu->lum_stripe_count == 0)
5174 lmu->lum_stripe_count = cpu_to_le32(1);
5176 /* these were checked in MDT */
5177 LASSERT(le32_to_cpu(lmu->lum_stripe_count) <
5178 le32_to_cpu(lmv->lmv_stripe_count));
5179 LASSERT(!lmv_is_splitting(lmv));
5180 LASSERT(lmv_is_migrating(lmv) || lmv_is_merging(lmv));
5182 mdd_xattrs_init(&xattrs);
5184 /* if dir stripe count will be shrunk to 1, it needs to be transformed
5185 * to a plain dir, which will cause FID change and namespace update.
5187 if (le32_to_cpu(lmu->lum_stripe_count) == 1) {
5188 struct linkea_data *ldata = &info->mdi_link_data;
5189 char *filename = info->mdi_name;
5191 rc = mdd_links_read(env, obj, ldata);
5195 if (ldata->ld_leh->leh_reccount > 1)
5196 GOTO(out, rc = -EINVAL);
5198 linkea_first_entry(ldata);
5200 GOTO(out, rc = -ENODATA);
5202 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, &lname,
5205 /* Note: lname might miss \0 at the end */
5206 snprintf(filename, sizeof(info->mdi_name), DNAME,
5207 lname.ln_namelen, lname.ln_name);
5208 lname.ln_name = filename;
5210 pobj = mdd_object_find(env, mdd, fid);
5217 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
5219 stripe = mdd_object_find(env, mdd, fid);
5220 if (IS_ERR(stripe)) {
5221 mdd_object_put(env, pobj);
5223 GOTO(out, rc = PTR_ERR(stripe));
5226 if (!lmv_is_fixed(lmv))
5227 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, false,
5231 handle = mdd_trans_create(env, mdd);
5233 GOTO(out, rc = PTR_ERR(handle));
5235 mlc->mlc_opc = MD_LAYOUT_SHRINK;
5236 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5238 GOTO(stop_trans, rc);
5240 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
5241 rc = mdd_declare_1sd_collapse(env, pobj, obj, stripe, attr,
5242 &xattrs, mlc, &lname, handle);
5244 GOTO(stop_trans, rc);
5247 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
5250 GOTO(stop_trans, rc);
5252 rc = mdd_trans_start(env, mdd, handle);
5254 GOTO(stop_trans, rc);
5256 mdd_write_lock(env, obj, DT_SRC_PARENT);
5257 mlc->mlc_opc = MD_LAYOUT_SHRINK;
5258 rc = mdo_layout_change(env, obj, mlc, handle);
5259 mdd_write_unlock(env, obj);
5261 GOTO(stop_trans, rc);
5263 if (le32_to_cpu(lmu->lum_stripe_count) == 1 && !lmv_is_fixed(lmv)) {
5264 rc = mdd_1sd_collapse(env, pobj, obj, stripe, attr, &xattrs,
5265 mlc, &lname, handle);
5267 GOTO(stop_trans, rc);
5270 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5271 XATTR_NAME_LMV, handle);
5272 GOTO(stop_trans, rc);
5275 rc = mdd_trans_stop(env, mdd, rc, handle);
5277 mdd_xattrs_fini(&xattrs);
5279 mdd_object_put(env, stripe);
5280 mdd_object_put(env, pobj);
5283 lu_buf_free(&lmv_buf);
5287 static int mdd_dir_declare_split_plain(const struct lu_env *env,
5288 struct mdd_device *mdd,
5289 struct mdd_object *pobj,
5290 struct mdd_object *obj,
5291 struct mdd_object *tobj,
5292 struct mdd_xattrs *xattrs,
5293 struct md_layout_change *mlc,
5294 struct dt_allocation_hint *hint,
5295 struct thandle *handle)
5297 struct mdd_thread_info *info = mdd_env_info(env);
5298 const struct lu_name *lname = mlc->mlc_name;
5299 struct lu_attr *la = &info->mdi_la_for_fix;
5300 struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
5301 struct linkea_data *ldata = &info->mdi_link_data;
5302 struct lmv_mds_md_v1 *lmv;
5306 mlc->mlc_opc = MD_LAYOUT_DETACH;
5307 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5311 memset(ldata, 0, sizeof(*ldata));
5312 rc = mdd_linkea_prepare(env, obj, NULL, NULL, mdd_object_fid(pobj),
5313 lname, 1, 0, ldata);
5317 count = lum->lum_stripe_count;
5318 lum->lum_stripe_count = 0;
5319 /* don't set default LMV since it will become a striped dir */
5320 lum->lum_max_inherit = LMV_INHERIT_NONE;
5321 mdd_object_make_hint(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5323 rc = mdd_declare_create(env, mdo2mdd(&pobj->mod_obj), pobj, tobj,
5324 lname, mlc->mlc_attr, handle, mlc->mlc_spec,
5325 ldata, NULL, NULL, NULL, hint);
5329 /* tobj mode will be used in lod_declare_xattr_set(), but it's not
5332 tobj->mod_obj.mo_lu.lo_header->loh_attr |= S_IFDIR;
5334 lmv = (typeof(lmv))info->mdi_key;
5335 memset(lmv, 0, sizeof(*lmv));
5336 lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
5337 lmv->lmv_stripe_count = cpu_to_le32(1);
5338 lmv->lmv_hash_type = cpu_to_le32(LMV_HASH_TYPE_DEFAULT);
5339 fid_le_to_cpu(&lmv->lmv_stripe_fids[0], mdd_object_fid(obj));
5341 mlc->mlc_opc = MD_LAYOUT_ATTACH;
5342 mlc->mlc_buf.lb_buf = lmv;
5343 mlc->mlc_buf.lb_len = lmv_mds_md_size(1, LMV_MAGIC_V1);
5344 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5348 rc = mdd_foreach_xattr(env, tobj, xattrs, handle,
5349 mdo_declare_xattr_set);
5353 lum->lum_stripe_count = count;
5354 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5355 rc = mdo_declare_layout_change(env, tobj, mlc, handle);
5359 rc = mdo_declare_index_delete(env, pobj, lname->ln_name, handle);
5363 rc = mdo_declare_index_insert(env, pobj, mdd_object_fid(tobj),
5364 S_IFDIR, lname->ln_name, handle);
5368 la->la_valid = LA_CTIME | LA_MTIME;
5369 rc = mdo_declare_attr_set(env, obj, la, handle);
5373 rc = mdo_declare_attr_set(env, pobj, la, handle);
5377 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
5383 * plain directory split:
5384 * 1. create \a tobj as plain directory.
5385 * 2. append \a obj as first stripe of \a tobj.
5386 * 3. migrate xattrs from \a obj to \a tobj.
5387 * 4. split \a tobj to specific stripe count.
5389 static int mdd_dir_split_plain(const struct lu_env *env,
5390 struct mdd_device *mdd,
5391 struct mdd_object *pobj,
5392 struct mdd_object *obj,
5393 struct mdd_object *tobj,
5394 struct mdd_xattrs *xattrs,
5395 struct md_layout_change *mlc,
5396 struct dt_allocation_hint *hint,
5397 struct thandle *handle)
5399 struct mdd_thread_info *info = mdd_env_info(env);
5400 struct lu_attr *pattr = &info->mdi_pattr;
5401 struct lu_attr *la = &info->mdi_la_for_fix;
5402 const struct lu_name *lname = mlc->mlc_name;
5403 struct linkea_data *ldata = &info->mdi_link_data;
5408 /* copy linkea out and set on target later */
5409 rc = mdd_links_read(env, obj, ldata);
5413 mlc->mlc_opc = MD_LAYOUT_DETACH;
5414 rc = mdo_layout_change(env, obj, mlc, handle);
5418 /* don't set nlink from obj */
5419 mlc->mlc_attr->la_valid &= ~LA_NLINK;
5421 rc = mdd_create_object(env, pobj, tobj, mlc->mlc_attr, mlc->mlc_spec,
5422 NULL, NULL, NULL, hint, handle, false);
5426 rc = mdd_foreach_xattr(env, tobj, xattrs, handle, mdo_xattr_set);
5430 rc = mdd_links_write(env, tobj, ldata, handle);
5434 rc = __mdd_index_delete(env, pobj, lname->ln_name, true, handle);
5438 rc = __mdd_index_insert(env, pobj, mdd_object_fid(tobj), S_IFDIR,
5439 lname->ln_name, handle);
5443 la->la_ctime = la->la_mtime = mlc->mlc_attr->la_mtime;
5444 la->la_valid = LA_CTIME | LA_MTIME;
5446 mdd_write_lock(env, obj, DT_SRC_CHILD);
5447 rc = mdd_update_time(env, tobj, mlc->mlc_attr, la, handle);
5448 mdd_write_unlock(env, obj);
5452 rc = mdd_la_get(env, pobj, pattr);
5456 la->la_valid = LA_CTIME | LA_MTIME;
5458 mdd_write_lock(env, pobj, DT_SRC_PARENT);
5459 rc = mdd_update_time(env, pobj, pattr, la, handle);
5460 mdd_write_unlock(env, pobj);
5464 /* FID changes, record it as CL_MIGRATE */
5465 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, tobj,
5466 pobj, pattr, mdd_object_fid(obj),
5467 pobj, pattr, lname, lname, handle);
5471 int mdd_dir_layout_split(const struct lu_env *env, struct md_object *o,
5472 struct md_layout_change *mlc)
5474 struct mdd_thread_info *info = mdd_env_info(env);
5475 struct mdd_device *mdd = mdo2mdd(o);
5476 struct mdd_object *obj = md2mdd_obj(o);
5477 struct mdd_object *pobj = md2mdd_obj(mlc->mlc_parent);
5478 struct mdd_object *tobj = md2mdd_obj(mlc->mlc_target);
5479 struct dt_allocation_hint *hint = &info->mdi_hint;
5480 bool is_plain = false;
5481 struct mdd_xattrs xattrs;
5482 struct thandle *handle;
5487 LASSERT(S_ISDIR(mdd_object_type(obj)));
5489 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
5495 mdd_xattrs_init(&xattrs);
5497 rc = mdd_xattrs_migrate_prep(env, &xattrs, obj, true, true);
5499 handle = mdd_trans_create(env, mdd);
5501 GOTO(out, rc = PTR_ERR(handle));
5504 rc = mdd_dir_declare_split_plain(env, mdd, pobj, obj, tobj,
5505 &xattrs, mlc, hint, handle);
5507 mlc->mlc_opc = MD_LAYOUT_SPLIT;
5508 rc = mdo_declare_layout_change(env, obj, mlc, handle);
5510 GOTO(stop_trans, rc);
5512 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL,
5516 GOTO(stop_trans, rc);
5518 rc = mdd_trans_start(env, mdd, handle);
5520 GOTO(stop_trans, rc);
5523 rc = mdd_dir_split_plain(env, mdd, pobj, obj, tobj, &xattrs,
5526 struct lu_buf *buf = &info->mdi_buf[0];
5528 buf->lb_buf = mlc->mlc_spec->u.sp_ea.eadata;
5529 buf->lb_len = mlc->mlc_spec->u.sp_ea.eadatalen;
5531 mdd_write_lock(env, obj, DT_TGT_CHILD);
5532 rc = mdo_xattr_set(env, obj, buf, XATTR_NAME_LMV,
5533 LU_XATTR_CREATE, handle);
5534 mdd_write_unlock(env, obj);
5536 GOTO(stop_trans, rc);
5538 rc = mdd_changelog_data_store_xattr(env, mdd, CL_LAYOUT, 0, obj,
5539 XATTR_NAME_LMV, handle);
5542 GOTO(stop_trans, rc);
5547 rc = mdd_trans_stop(env, mdd, rc, handle);
5549 mdd_xattrs_fini(&xattrs);
5554 const struct md_dir_operations mdd_dir_ops = {
5555 .mdo_is_subdir = mdd_is_subdir,
5556 .mdo_lookup = mdd_lookup,
5557 .mdo_create = mdd_create,
5558 .mdo_rename = mdd_rename,
5559 .mdo_link = mdd_link,
5560 .mdo_unlink = mdd_unlink,
5561 .mdo_create_data = mdd_create_data,
5562 .mdo_migrate = mdd_migrate,