4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_dir.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@intel.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_mds.h>
44 #include <lustre_fid.h>
46 #include "mdd_internal.h"
48 static const char dot[] = ".";
49 static const char dotdot[] = "..";
51 static struct lu_name lname_dotdot = {
52 .ln_name = (char *) dotdot,
53 .ln_namelen = sizeof(dotdot) - 1,
57 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
59 if (!lu_name_is_valid(ln))
61 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
67 /* Get FID from name and parent */
69 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
70 const struct lu_attr *pattr, const struct lu_name *lname,
71 struct lu_fid* fid, int mask)
73 const char *name = lname->ln_name;
74 const struct dt_key *key = (const struct dt_key *)name;
75 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
76 struct mdd_device *m = mdo2mdd(pobj);
77 struct dt_object *dir = mdd_object_child(mdd_obj);
81 if (unlikely(mdd_is_dead_obj(mdd_obj)))
84 if (!mdd_object_exists(mdd_obj))
87 if (mdd_object_remote(mdd_obj)) {
88 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
89 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
92 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
97 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
98 dt_try_as_dir(env, dir)))
99 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
106 int mdd_lookup(const struct lu_env *env,
107 struct md_object *pobj, const struct lu_name *lname,
108 struct lu_fid *fid, struct md_op_spec *spec)
110 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
114 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
118 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
119 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
123 /** Read the link EA into a temp buffer.
124 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
125 * A pointer to the buffer is stored in \a ldata::ld_buf.
129 static int __mdd_links_read(const struct lu_env *env,
130 struct mdd_object *mdd_obj,
131 struct linkea_data *ldata)
135 if (!mdd_object_exists(mdd_obj))
138 /* First try a small buf */
139 LASSERT(env != NULL);
140 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
142 if (ldata->ld_buf->lb_buf == NULL)
145 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
147 /* Buf was too small, figure out what we need. */
148 lu_buf_free(ldata->ld_buf);
149 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
153 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
154 if (ldata->ld_buf->lb_buf == NULL)
156 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
160 lu_buf_free(ldata->ld_buf);
161 ldata->ld_buf = NULL;
165 return linkea_init(ldata);
168 static int mdd_links_read(const struct lu_env *env,
169 struct mdd_object *mdd_obj,
170 struct linkea_data *ldata)
174 rc = __mdd_links_read(env, mdd_obj, ldata);
176 rc = linkea_init(ldata);
181 static int mdd_links_read_with_rec(const struct lu_env *env,
182 struct mdd_object *mdd_obj,
183 struct linkea_data *ldata)
187 rc = __mdd_links_read(env, mdd_obj, ldata);
189 rc = linkea_init_with_rec(ldata);
195 * Get parent FID of the directory
197 * Read parent FID from linkEA, if that fails, then do lookup
198 * dotdot to get the parent FID.
200 * \param[in] env execution environment
201 * \param[in] obj object from which to find the parent FID
202 * \param[in] attr attribute of the object
203 * \param[out] fid fid to get the parent FID
205 * \retval 0 if getting the parent FID succeeds.
206 * \retval negative errno if getting the parent FID fails.
208 static inline int mdd_parent_fid(const struct lu_env *env,
209 struct mdd_object *obj,
210 const struct lu_attr *attr,
213 struct mdd_thread_info *info = mdd_env_info(env);
214 struct linkea_data ldata = { NULL };
215 struct lu_buf *buf = &info->mti_link_buf;
216 struct lu_name lname;
221 LASSERT(S_ISDIR(mdd_object_type(obj)));
223 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
224 if (buf->lb_buf == NULL)
225 GOTO(lookup, rc = 0);
228 rc = mdd_links_read_with_rec(env, obj, &ldata);
232 LASSERT(ldata.ld_leh != NULL);
233 /* Directory should only have 1 parent */
234 if (ldata.ld_leh->leh_reccount > 1)
237 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
239 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
240 if (likely(fid_is_sane(fid)))
243 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
248 * For root fid use special function, which does not compare version component
249 * of fid. Version component is different for root fids on all MDTs.
251 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
253 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
254 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
258 * return 1: if \a tfid is the fid of the ancestor of \a mo;
260 * otherwise: values < 0, errors.
262 static int mdd_is_parent(const struct lu_env *env,
263 struct mdd_device *mdd,
264 struct mdd_object *mo,
265 const struct lu_attr *attr,
266 const struct lu_fid *tfid)
268 struct mdd_object *mp;
272 LASSERT(!lu_fid_eq(mdo2fid(mo), tfid));
273 pfid = &mdd_env_info(env)->mti_fid;
275 if (mdd_is_root(mdd, mdo2fid(mo)))
278 if (mdd_is_root(mdd, tfid))
281 rc = mdd_parent_fid(env, mo, attr, pfid);
286 if (lu_fid_eq(pfid, tfid))
289 if (mdd_is_root(mdd, pfid))
292 mp = mdd_object_find(env, mdd, pfid);
296 if (!mdd_object_exists(mp)) {
297 mdd_object_put(env, mp);
301 rc = mdd_parent_fid(env, mp, attr, pfid);
302 mdd_object_put(env, mp);
311 * No permission check is needed.
313 * returns 1: if fid is ancestor of @mo;
314 * returns 0: if fid is not an ancestor of @mo;
315 * returns < 0: if error
317 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
318 const struct lu_fid *fid)
320 struct mdd_device *mdd = mdo2mdd(mo);
321 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
325 if (!mdd_object_exists(md2mdd_obj(mo)))
328 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
331 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
335 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid);
340 * Check that @dir contains no entries except (possibly) dot and dotdot.
345 * -ENOTDIR not a directory object
346 * -ENOTEMPTY not empty
350 static int mdd_dir_is_empty(const struct lu_env *env,
351 struct mdd_object *dir)
354 struct dt_object *obj;
355 const struct dt_it_ops *iops;
359 obj = mdd_object_child(dir);
360 if (!dt_try_as_dir(env, obj))
363 iops = &obj->do_index_ops->dio_it;
364 it = iops->init(env, obj, LUDA_64BITHASH);
366 result = iops->get(env, it, (const struct dt_key *)"");
369 for (result = 0, i = 0; result == 0 && i < 3; ++i)
370 result = iops->next(env, it);
373 else if (result == 1)
375 } else if (result == 0)
377 * Huh? Index contains no zero key?
384 result = PTR_ERR(it);
389 * Determine if the target object can be hard linked, and right now it only
390 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
391 * directory nlink count might exceed the maximum link count(see
392 * osd_object_ref_add), so it only check nlink for non-directories.
394 * \param[in] env thread environment
395 * \param[in] obj object being linked to
396 * \param[in] la attributes of \a obj
398 * \retval 0 if \a obj can be hard linked
399 * \retval negative error if \a obj is a directory or has too
402 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
403 const struct lu_attr *la)
405 struct mdd_device *m = mdd_obj2mdd_dev(obj);
410 /* Subdir count limitation can be broken through
411 * (see osd_object_ref_add), so only check non-directory here. */
412 if (!S_ISDIR(la->la_mode) &&
413 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
420 * Check whether it may create the cobj under the pobj.
422 * \param[in] env execution environment
423 * \param[in] pobj the parent directory
424 * \param[in] pattr the attribute of the parent directory
425 * \param[in] cobj the child to be created
426 * \param[in] check_perm if check WRITE|EXEC permission for parent
428 * \retval = 0 create the child under this dir is allowed
429 * \retval negative errno create the child under this dir is
432 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
433 const struct lu_attr *pattr, struct mdd_object *cobj,
439 if (cobj && mdd_object_exists(cobj))
442 if (mdd_is_dead_obj(pobj))
446 rc = mdd_permission_internal_locked(env, pobj, pattr,
447 MAY_WRITE | MAY_EXEC,
453 * Check whether can unlink from the pobj in the case of "cobj == NULL".
455 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
456 const struct lu_attr *pattr, const struct lu_attr *attr)
461 if (mdd_is_dead_obj(pobj))
464 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
467 rc = mdd_permission_internal_locked(env, pobj, pattr,
468 MAY_WRITE | MAY_EXEC,
473 if (pattr->la_flags & LUSTRE_APPEND_FL)
480 * pobj == NULL is remote ops case, under such case, pobj's
481 * VTX feature has been checked already, no need check again.
483 static inline int mdd_is_sticky(const struct lu_env *env,
484 struct mdd_object *pobj,
485 const struct lu_attr *pattr,
486 struct mdd_object *cobj,
487 const struct lu_attr *cattr)
489 struct lu_ucred *uc = lu_ucred_assert(env);
492 LASSERT(pattr != NULL);
493 if (!(pattr->la_mode & S_ISVTX) ||
494 (pattr->la_uid == uc->uc_fsuid))
498 LASSERT(cattr != NULL);
499 if (cattr->la_uid == uc->uc_fsuid)
502 return !md_capable(uc, CFS_CAP_FOWNER);
505 static int mdd_may_delete_entry(const struct lu_env *env,
506 struct mdd_object *pobj,
507 const struct lu_attr *pattr,
512 LASSERT(pobj != NULL);
513 if (!mdd_object_exists(pobj))
516 if (mdd_is_dead_obj(pobj))
521 rc = mdd_permission_internal_locked(env, pobj, pattr,
522 MAY_WRITE | MAY_EXEC,
528 if (pattr->la_flags & LUSTRE_APPEND_FL)
535 * Check whether it may delete the cobj from the pobj.
538 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
539 const struct lu_attr *tpattr, struct mdd_object *tobj,
540 const struct lu_attr *tattr, const struct lu_attr *cattr,
541 int check_perm, int check_empty)
547 LASSERT(tpattr != NULL);
548 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
556 if (!mdd_object_exists(tobj))
559 if (mdd_is_dead_obj(tobj))
562 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
565 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
568 /* additional check the rename case */
570 if (S_ISDIR(cattr->la_mode)) {
571 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
573 if (!S_ISDIR(tattr->la_mode))
576 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
578 } else if (S_ISDIR(tattr->la_mode))
582 if (S_ISDIR(tattr->la_mode) && check_empty)
583 rc = mdd_dir_is_empty(env, tobj);
589 * Check whether it can create the link file(linked to @src_obj) under
590 * the target directory(@tgt_obj), and src_obj has been locked by
593 * \param[in] env execution environment
594 * \param[in] tgt_obj the target directory
595 * \param[in] tattr attributes of target directory
596 * \param[in] lname the link name
597 * \param[in] src_obj source object for link
598 * \param[in] cattr attributes for source object
600 * \retval = 0 it is allowed to create the link file under tgt_obj
601 * \retval negative error not allowed to create the link file
603 static int mdd_link_sanity_check(const struct lu_env *env,
604 struct mdd_object *tgt_obj,
605 const struct lu_attr *tattr,
606 const struct lu_name *lname,
607 struct mdd_object *src_obj,
608 const struct lu_attr *cattr)
610 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
614 if (!mdd_object_exists(src_obj))
617 if (mdd_is_dead_obj(src_obj))
620 /* Local ops, no lookup before link, check filename length here. */
621 rc = mdd_name_check(m, lname);
625 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
628 if (S_ISDIR(mdd_object_type(src_obj)))
631 LASSERT(src_obj != tgt_obj);
632 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
636 rc = __mdd_may_link(env, src_obj, cattr);
641 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
642 const char *name, struct thandle *handle)
644 struct dt_object *next = mdd_object_child(pobj);
648 if (dt_try_as_dir(env, next))
649 rc = dt_delete(env, next, (struct dt_key *)name, handle);
656 static int __mdd_index_insert_only(const struct lu_env *env,
657 struct mdd_object *pobj,
658 const struct lu_fid *lf, __u32 type,
660 struct thandle *handle)
662 struct dt_object *next = mdd_object_child(pobj);
666 if (dt_try_as_dir(env, next)) {
667 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
668 struct lu_ucred *uc = lu_ucred_check(env);
672 rec->rec_type = type;
673 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
674 rc = dt_insert(env, next, (const struct dt_rec *)rec,
675 (const struct dt_key *)name, handle,
683 /* insert named index, add reference if isdir */
684 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
685 const struct lu_fid *lf, __u32 type,
686 const char *name, struct thandle *handle)
691 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
692 if (rc == 0 && S_ISDIR(type)) {
693 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
694 mdo_ref_add(env, pobj, handle);
695 mdd_write_unlock(env, pobj);
701 /* delete named index, drop reference if isdir */
702 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
703 const char *name, int is_dir,
704 struct thandle *handle)
709 rc = __mdd_index_delete_only(env, pobj, name, handle);
710 if (rc == 0 && is_dir) {
711 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
712 mdo_ref_del(env, pobj, handle);
713 mdd_write_unlock(env, pobj);
719 static int mdd_llog_record_calc_size(const struct lu_env *env,
720 const struct lu_name *tname,
721 const struct lu_name *sname)
723 const struct lu_ucred *uc = lu_ucred(env);
724 enum changelog_rec_flags crf = CLF_EXTRA_FLAGS;
725 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
730 if (uc != NULL && uc->uc_jobid[0] != '\0')
733 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
734 changelog_rec_offset(crf, crfe) +
735 (tname != NULL ? tname->ln_namelen : 0) +
736 (sname != NULL ? 1 + sname->ln_namelen : 0));
739 int mdd_declare_changelog_store(const struct lu_env *env,
740 struct mdd_device *mdd,
741 enum changelog_rec_type type,
742 const struct lu_name *tname,
743 const struct lu_name *sname,
744 struct thandle *handle)
746 struct obd_device *obd = mdd2obd_dev(mdd);
747 struct llog_ctxt *ctxt;
748 struct llog_changelog_rec *rec;
750 struct thandle *llog_th;
754 if (!mdd_changelog_enabled(env, mdd, type))
757 reclen = mdd_llog_record_calc_size(env, tname, sname);
758 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
759 if (buf->lb_buf == NULL)
763 rec->cr_hdr.lrh_len = reclen;
764 rec->cr_hdr.lrh_type = CHANGELOG_REC;
766 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
770 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
772 GOTO(out_put, rc = PTR_ERR(llog_th));
774 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
782 /** Add a changelog entry \a rec to the changelog llog
785 * \param handle - currently ignored since llogs start their own transaction;
786 * this will hopefully be fixed in llog rewrite
789 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
790 struct llog_changelog_rec *rec, struct thandle *th)
792 struct obd_device *obd = mdd2obd_dev(mdd);
793 struct llog_ctxt *ctxt;
794 struct thandle *llog_th;
797 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
798 changelog_rec_varsize(&rec->cr));
800 /* llog_lvfs_write_rec sets the llog tail len */
801 rec->cr_hdr.lrh_type = CHANGELOG_REC;
802 rec->cr.cr_time = cl_time();
804 spin_lock(&mdd->mdd_cl.mc_lock);
805 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
806 * but as long as the MDD transactions are ordered correctly for e.g.
807 * rename conflicts, I don't think this should matter. */
808 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
809 spin_unlock(&mdd->mdd_cl.mc_lock);
811 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
815 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
817 GOTO(out_put, rc = PTR_ERR(llog_th));
819 /* nested journal transaction */
820 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
822 /* time to recover some space ?? */
823 if (likely(!mdd->mdd_changelog_gc ||
824 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
825 mdd->mdd_changelog_min_gc_interval >=
826 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
827 /* save a spin_lock trip */
829 spin_lock(&mdd->mdd_cl.mc_lock);
830 if (likely(mdd->mdd_changelog_gc &&
831 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
832 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
833 mdd->mdd_changelog_min_gc_interval)) {
834 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
835 mdd->mdd_changelog_min_free_cat_entries ||
836 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
837 CWARN("%s:%s low on changelog_catalog free entries, "
838 "starting ChangeLog garbage collection thread\n",
840 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
843 /* indicate further kthread run will occur outside
844 * right after current journal transaction filling has
847 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
849 /* next check in mdd_changelog_min_gc_interval anyway
851 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
853 spin_unlock(&mdd->mdd_cl.mc_lock);
861 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
862 const struct lu_fid *sfid,
863 const struct lu_fid *spfid,
864 const struct lu_name *sname)
866 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
869 LASSERT(sfid != NULL);
870 LASSERT(spfid != NULL);
871 LASSERT(sname != NULL);
873 extsize = sname->ln_namelen + 1;
875 rnm->cr_sfid = *sfid;
876 rnm->cr_spfid = *spfid;
878 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
879 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
880 rec->cr_namelen += extsize;
883 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
885 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
887 if (jobid == NULL || jobid[0] == '\0')
890 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
893 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
895 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
897 ef->cr_extra_flags = eflags;
900 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
901 __u64 uid, __u64 gid)
903 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
905 uidgid->cr_uid = uid;
906 uidgid->cr_gid = gid;
909 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
912 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
917 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, int flags)
919 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
921 omd->cr_openflags = (__u32)flags;
924 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
925 const char *xattr_name)
927 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
929 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
932 /** Store a namespace change changelog record
933 * If this fails, we must fail the whole transaction; we don't
934 * want the change to commit without the log entry.
935 * \param target - mdd_object of change
936 * \param tpfid - target parent dir/object fid
937 * \param sfid - source object fid
938 * \param spfid - source parent fid
939 * \param tname - target name string
940 * \param sname - source name string
941 * \param handle - transaction handle
943 int mdd_changelog_ns_store(const struct lu_env *env,
944 struct mdd_device *mdd,
945 enum changelog_rec_type type,
946 enum changelog_rec_flags crf,
947 struct mdd_object *target,
948 const struct lu_fid *tpfid,
949 const struct lu_fid *sfid,
950 const struct lu_fid *spfid,
951 const struct lu_name *tname,
952 const struct lu_name *sname,
953 struct thandle *handle)
955 const struct lu_ucred *uc = lu_ucred(env);
956 struct llog_changelog_rec *rec;
959 __u64 xflags = CLFE_INVALID;
963 if (!mdd_changelog_enabled(env, mdd, type))
966 LASSERT(tpfid != NULL);
967 LASSERT(tname != NULL);
968 LASSERT(handle != NULL);
970 reclen = mdd_llog_record_calc_size(env, tname, sname);
971 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
972 if (buf->lb_buf == NULL)
977 crf |= CLF_EXTRA_FLAGS;
980 if (uc->uc_jobid[0] != '\0')
982 xflags |= CLFE_UIDGID;
991 rec->cr.cr_flags = crf;
993 if (crf & CLF_EXTRA_FLAGS) {
994 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
995 if (xflags & CLFE_UIDGID)
996 mdd_changelog_rec_extra_uidgid(&rec->cr,
997 uc->uc_uid, uc->uc_gid);
998 if (xflags & CLFE_NID)
999 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1002 rec->cr.cr_type = (__u32)type;
1003 rec->cr.cr_pfid = *tpfid;
1004 rec->cr.cr_namelen = tname->ln_namelen;
1005 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1007 if (crf & CLF_RENAME)
1008 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1010 if (crf & CLF_JOBID)
1011 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1013 if (likely(target != NULL)) {
1014 rec->cr.cr_tfid = *mdo2fid(target);
1015 target->mod_cltime = ktime_get();
1017 fid_zero(&rec->cr.cr_tfid);
1020 rc = mdd_changelog_store(env, mdd, rec, handle);
1022 CERROR("%s: cannot store changelog record: type = %d, "
1023 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1024 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1025 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1032 static int __mdd_links_add(const struct lu_env *env,
1033 struct mdd_object *mdd_obj,
1034 struct linkea_data *ldata,
1035 const struct lu_name *lname,
1036 const struct lu_fid *pfid,
1037 int first, int check)
1041 if (ldata->ld_leh == NULL) {
1042 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1046 rc = linkea_data_new(ldata,
1047 &mdd_env_info(env)->mti_link_buf);
1054 rc = linkea_links_find(ldata, lname, pfid);
1055 if (rc && rc != -ENOENT)
1061 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1062 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1066 linkea_add_buf(ldata, lname, tfid);
1069 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1070 linkea_add_buf(ldata, lname, pfid);
1072 return linkea_add_buf(ldata, lname, pfid);
1075 static int __mdd_links_del(const struct lu_env *env,
1076 struct mdd_object *mdd_obj,
1077 struct linkea_data *ldata,
1078 const struct lu_name *lname,
1079 const struct lu_fid *pfid)
1083 if (ldata->ld_leh == NULL) {
1084 rc = mdd_links_read(env, mdd_obj, ldata);
1089 rc = linkea_links_find(ldata, lname, pfid);
1093 linkea_del_buf(ldata, lname);
1097 static int mdd_linkea_prepare(const struct lu_env *env,
1098 struct mdd_object *mdd_obj,
1099 const struct lu_fid *oldpfid,
1100 const struct lu_name *oldlname,
1101 const struct lu_fid *newpfid,
1102 const struct lu_name *newlname,
1103 int first, int check,
1104 struct linkea_data *ldata)
1109 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1112 LASSERT(oldpfid != NULL || newpfid != NULL);
1114 if (mdd_obj->mod_flags & DEAD_OBJ)
1115 /* Unnecessary to update linkEA for dead object. */
1118 if (oldpfid != NULL) {
1119 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1121 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1124 /* No changes done. */
1129 /* If renaming, add the new record */
1130 if (newpfid != NULL)
1131 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1137 int mdd_links_rename(const struct lu_env *env,
1138 struct mdd_object *mdd_obj,
1139 const struct lu_fid *oldpfid,
1140 const struct lu_name *oldlname,
1141 const struct lu_fid *newpfid,
1142 const struct lu_name *newlname,
1143 struct thandle *handle,
1144 struct linkea_data *ldata,
1145 int first, int check)
1150 if (ldata == NULL) {
1151 ldata = &mdd_env_info(env)->mti_link_data;
1152 memset(ldata, 0, sizeof(*ldata));
1153 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1154 newpfid, newlname, first, check, ldata);
1159 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1160 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1166 if (newlname == NULL)
1167 CERROR("link_ea add failed %d "DFID"\n",
1168 rc, PFID(mdd_object_fid(mdd_obj)));
1169 else if (oldpfid == NULL)
1170 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1171 newlname->ln_namelen, newlname->ln_name, rc,
1172 PFID(mdd_object_fid(mdd_obj)));
1173 else if (newpfid == NULL)
1174 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1175 oldlname->ln_namelen, oldlname->ln_name, rc,
1176 PFID(mdd_object_fid(mdd_obj)));
1178 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1179 "\n", oldlname->ln_namelen, oldlname->ln_name,
1180 newlname->ln_namelen, newlname->ln_name, rc,
1181 PFID(mdd_object_fid(mdd_obj)));
1184 if (is_vmalloc_addr(ldata->ld_buf))
1185 /* if we vmalloced a large buffer drop it */
1186 lu_buf_free(ldata->ld_buf);
1191 static inline int mdd_links_add(const struct lu_env *env,
1192 struct mdd_object *mdd_obj,
1193 const struct lu_fid *pfid,
1194 const struct lu_name *lname,
1195 struct thandle *handle,
1196 struct linkea_data *ldata, int first)
1198 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1199 pfid, lname, handle, ldata, first, 0);
1202 static inline int mdd_links_del(const struct lu_env *env,
1203 struct mdd_object *mdd_obj,
1204 const struct lu_fid *pfid,
1205 const struct lu_name *lname,
1206 struct thandle *handle)
1208 return mdd_links_rename(env, mdd_obj, pfid, lname,
1209 NULL, NULL, handle, NULL, 0, 0);
1212 /** Read the link EA into a temp buffer.
1213 * Uses the name_buf since it is generally large.
1214 * \retval IS_ERR err
1215 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1217 struct lu_buf *mdd_links_get(const struct lu_env *env,
1218 struct mdd_object *mdd_obj)
1220 struct linkea_data ldata = { NULL };
1223 rc = mdd_links_read(env, mdd_obj, &ldata);
1224 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1227 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1228 struct linkea_data *ldata, struct thandle *handle)
1230 const struct lu_buf *buf;
1233 if (ldata == NULL || ldata->ld_buf == NULL ||
1234 ldata->ld_leh == NULL)
1237 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1241 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1242 ldata->ld_leh->leh_len);
1243 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1244 if (unlikely(rc == -ENOSPC)) {
1245 rc = linkea_overflow_shrink(ldata);
1253 static int mdd_declare_links_add(const struct lu_env *env,
1254 struct mdd_object *mdd_obj,
1255 struct thandle *handle,
1256 struct linkea_data *ldata)
1262 if (ldata != NULL && ldata->ld_leh != NULL) {
1263 ea_len = ldata->ld_leh->leh_len;
1264 linkea = ldata->ld_buf->lb_buf;
1266 ea_len = MAX_LINKEA_SIZE;
1270 rc = mdo_declare_xattr_set(env, mdd_obj,
1271 mdd_buf_get_const(env, linkea, ea_len),
1272 XATTR_NAME_LINK, 0, handle);
1277 static inline int mdd_declare_links_del(const struct lu_env *env,
1278 struct mdd_object *c,
1279 struct thandle *handle)
1283 /* For directory, the linkEA will be removed together
1284 * with the object. */
1285 if (!S_ISDIR(mdd_object_type(c)))
1286 rc = mdd_declare_links_add(env, c, handle, NULL);
1291 static int mdd_declare_link(const struct lu_env *env,
1292 struct mdd_device *mdd,
1293 struct mdd_object *p,
1294 struct mdd_object *c,
1295 const struct lu_name *name,
1296 struct thandle *handle,
1298 struct linkea_data *data)
1300 struct lu_fid tfid = *mdo2fid(c);
1303 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1304 tfid.f_oid = cfs_fail_val;
1306 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1307 name->ln_name, handle);
1311 rc = mdo_declare_ref_add(env, c, handle);
1315 la->la_valid = LA_CTIME | LA_MTIME;
1316 rc = mdo_declare_attr_set(env, p, la, handle);
1320 la->la_valid = LA_CTIME;
1321 rc = mdo_declare_attr_set(env, c, la, handle);
1325 rc = mdd_declare_links_add(env, c, handle, data);
1329 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1335 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1336 struct md_object *src_obj, const struct lu_name *lname,
1339 const char *name = lname->ln_name;
1340 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1341 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1342 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1343 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1344 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1345 struct mdd_device *mdd = mdo2mdd(src_obj);
1346 struct thandle *handle;
1347 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1348 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1352 rc = mdd_la_get(env, mdd_sobj, cattr);
1356 rc = mdd_la_get(env, mdd_tobj, tattr);
1361 * If we are using project inheritance, we only allow hard link
1362 * creation in our tree when the project IDs are the same;
1363 * otherwise the tree quota mechanism could be circumvented.
1365 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1366 (tattr->la_projid != cattr->la_projid))
1369 handle = mdd_trans_create(env, mdd);
1371 GOTO(out_pending, rc = PTR_ERR(handle));
1373 memset(ldata, 0, sizeof(*ldata));
1375 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1376 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1378 /* Note: even this function will change ldata, but it comes from
1379 * thread_info, which is completely temporary and only seen in
1380 * this function, so we do not need reset ldata once it fails.*/
1381 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
1382 lname, 0, 0, ldata);
1386 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1391 rc = mdd_trans_start(env, mdd, handle);
1395 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1396 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1399 GOTO(out_unlock, rc);
1401 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1402 rc = mdo_ref_add(env, mdd_sobj, handle);
1404 GOTO(out_unlock, rc);
1407 *tfid = *mdo2fid(mdd_sobj);
1408 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1409 tfid->f_oid = cfs_fail_val;
1411 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1412 mdd_object_type(mdd_sobj), name, handle);
1414 mdo_ref_del(env, mdd_sobj, handle);
1415 GOTO(out_unlock, rc);
1418 la->la_valid = LA_CTIME | LA_MTIME;
1419 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1421 GOTO(out_unlock, rc);
1423 la->la_valid = LA_CTIME;
1424 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1426 /* Note: The failure of links_add should not cause the
1427 * link failure, so do not check return value. */
1428 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1429 lname, handle, ldata, 0);
1433 mdd_write_unlock(env, mdd_sobj);
1435 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1436 mdo2fid(mdd_tobj), NULL, NULL,
1437 lname, NULL, handle);
1439 rc = mdd_trans_stop(env, mdd, rc, handle);
1440 if (is_vmalloc_addr(ldata->ld_buf))
1441 /* if we vmalloced a large buffer drop it */
1442 lu_buf_free(ldata->ld_buf);
1447 static int mdd_mark_orphan_object(const struct lu_env *env,
1448 struct mdd_object *obj, struct thandle *handle,
1451 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1454 if (!S_ISDIR(mdd_object_type(obj)))
1457 attr->la_valid = LA_FLAGS;
1458 attr->la_flags = LUSTRE_ORPHAN_FL;
1461 rc = mdo_declare_attr_set(env, obj, attr, handle);
1463 rc = mdo_attr_set(env, obj, attr, handle);
1468 static int mdd_declare_finish_unlink(const struct lu_env *env,
1469 struct mdd_object *obj,
1470 struct thandle *handle)
1474 /* Sigh, we do not know if the unlink object will become orphan in
1475 * declare phase, but fortunately the flags here does not matter
1476 * in current declare implementation */
1477 rc = mdd_mark_orphan_object(env, obj, handle, true);
1481 rc = mdo_declare_destroy(env, obj, handle);
1485 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1489 return mdd_declare_links_del(env, obj, handle);
1492 /* caller should take a lock before calling */
1493 int mdd_finish_unlink(const struct lu_env *env,
1494 struct mdd_object *obj, struct md_attr *ma,
1495 const struct mdd_object *pobj,
1496 const struct lu_name *lname,
1500 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1503 LASSERT(mdd_write_locked(env, obj) != 0);
1505 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1506 /* add new orphan and the object
1507 * will be deleted during mdd_close() */
1508 obj->mod_flags |= DEAD_OBJ;
1509 if (obj->mod_count) {
1510 rc = mdd_orphan_insert(env, obj, th);
1512 CDEBUG(D_HA, "Object "DFID" is inserted into "
1513 "orphan list, open count = %d\n",
1514 PFID(mdd_object_fid(obj)),
1517 CERROR("Object "DFID" fail to be an orphan, "
1518 "open count = %d, maybe cause failed "
1520 PFID(mdd_object_fid(obj)),
1523 /* mark object as an orphan here, not
1524 * before mdd_orphan_insert() as racing
1525 * mdd_la_get() may propagate ORPHAN_OBJ
1526 * causing the asserition */
1527 rc = mdd_mark_orphan_object(env, obj, th, false);
1529 rc = mdo_destroy(env, obj, th);
1531 } else if (!is_dir) {
1532 /* old files may not have link ea; ignore errors */
1533 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1541 * has mdd_write_lock on cobj already, but not on pobj yet
1543 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1544 const struct lu_attr *pattr,
1545 struct mdd_object *cobj,
1546 const struct lu_attr *cattr)
1551 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1556 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1557 struct mdd_object *p, struct mdd_object *c,
1558 const struct lu_name *name, struct md_attr *ma,
1559 struct thandle *handle, int no_name, int is_dir)
1561 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1564 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1565 if (likely(no_name == 0)) {
1566 rc = mdo_declare_index_delete(env, p, name->ln_name,
1573 rc = mdo_declare_ref_del(env, p, handle);
1579 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1580 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1581 la->la_valid = LA_CTIME | LA_MTIME;
1582 rc = mdo_declare_attr_set(env, p, la, handle);
1587 rc = mdo_declare_ref_del(env, c, handle);
1591 rc = mdo_declare_ref_del(env, c, handle);
1595 la->la_valid = LA_CTIME;
1596 rc = mdo_declare_attr_set(env, c, la, handle);
1600 rc = mdd_declare_finish_unlink(env, c, handle);
1604 /* FIXME: need changelog for remove entry */
1605 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1613 * test if a file has an HSM archive
1614 * if HSM attributes are not found in ma update them from
1617 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1618 struct mdd_object *obj,
1623 if (!(ma->ma_valid & MA_HSM)) {
1624 /* no HSM MD provided, read xattr */
1625 struct lu_buf *hsm_buf;
1626 const size_t buflen = sizeof(struct hsm_attrs);
1629 hsm_buf = mdd_buf_get(env, NULL, 0);
1630 lu_buf_alloc(hsm_buf, buflen);
1631 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1632 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1633 lu_buf_free(hsm_buf);
1637 ma->ma_valid |= MA_HSM;
1639 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1645 * Delete name entry and the object.
1646 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1647 * does not exist for this object, and it could only happen during resending
1648 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1649 * is also needed in this case(needed by changelog), so we have to add another
1650 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1651 * the ENOENT failure should be able to be fixed by redo mechanism.
1653 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1654 struct md_object *cobj, const struct lu_name *lname,
1655 struct md_attr *ma, int no_name)
1657 const char *name = lname->ln_name;
1658 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1659 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1660 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1661 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1662 struct mdd_object *mdd_cobj = NULL;
1663 struct mdd_device *mdd = mdo2mdd(pobj);
1664 struct thandle *handle;
1665 int rc, is_dir = 0, cl_flags = 0;
1668 /* let shutdown to start */
1669 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 1);
1671 /* cobj == NULL means only delete name entry */
1672 if (likely(cobj != NULL)) {
1673 mdd_cobj = md2mdd_obj(cobj);
1674 if (mdd_object_exists(mdd_cobj) == 0)
1678 rc = mdd_la_get(env, mdd_pobj, pattr);
1682 if (likely(mdd_cobj != NULL)) {
1684 rc = mdd_la_get(env, mdd_cobj, cattr);
1688 is_dir = S_ISDIR(cattr->la_mode);
1689 /* search for an existing archive.
1690 * we should check ahead as the object
1691 * can be destroyed in this transaction */
1692 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1693 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1696 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1700 handle = mdd_trans_create(env, mdd);
1702 RETURN(PTR_ERR(handle));
1704 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1705 lname, ma, handle, no_name, is_dir);
1709 rc = mdd_trans_start(env, mdd, handle);
1713 if (likely(mdd_cobj != NULL))
1714 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1716 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1717 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1722 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1723 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1724 GOTO(cleanup, rc = 0);
1726 if (likely(mdd_cobj != NULL)) {
1727 rc = mdo_ref_del(env, mdd_cobj, handle);
1729 __mdd_index_insert_only(env, mdd_pobj,
1731 mdd_object_type(mdd_cobj),
1738 mdo_ref_del(env, mdd_cobj, handle);
1740 /* fetch updated nlink */
1741 rc = mdd_la_get(env, mdd_cobj, cattr);
1746 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1747 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1749 la->la_valid = LA_CTIME | LA_MTIME;
1750 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1754 /* Enough for only unlink the entry */
1755 if (unlikely(mdd_cobj == NULL))
1758 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1759 /* update ctime of an unlinked file only if it is still
1760 * opened or a link still exists */
1761 la->la_valid = LA_CTIME;
1762 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1767 /* XXX: this transfer to ma will be removed with LOD/OSP */
1768 ma->ma_attr = *cattr;
1769 ma->ma_valid |= MA_INODE;
1770 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1774 /* fetch updated nlink */
1775 rc = mdd_la_get(env, mdd_cobj, cattr);
1776 /* if object is removed then we can't get its attrs,
1778 if (rc == -ENOENT) {
1779 cattr->la_nlink = 0;
1783 if (cattr->la_nlink == 0) {
1784 ma->ma_attr = *cattr;
1785 ma->ma_valid |= MA_INODE;
1790 if (likely(mdd_cobj != NULL))
1791 mdd_write_unlock(env, mdd_cobj);
1794 if (cattr->la_nlink == 0)
1795 cl_flags |= CLF_UNLINK_LAST;
1797 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1799 rc = mdd_changelog_ns_store(env, mdd,
1800 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1801 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1806 rc = mdd_trans_stop(env, mdd, rc, handle);
1812 * The permission has been checked when obj created, no need check again.
1814 static int mdd_cd_sanity_check(const struct lu_env *env,
1815 struct mdd_object *obj)
1820 if (!obj || mdd_is_dead_obj(obj))
1826 static int mdd_create_data(const struct lu_env *env,
1827 struct md_object *pobj,
1828 struct md_object *cobj,
1829 const struct md_op_spec *spec,
1832 struct mdd_device *mdd = mdo2mdd(cobj);
1833 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1834 struct mdd_object *son = md2mdd_obj(cobj);
1835 struct thandle *handle;
1836 const struct lu_buf *buf;
1837 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1838 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1842 rc = mdd_cd_sanity_check(env, son);
1846 if (!md_should_create(spec->sp_cr_flags))
1850 * there are following use cases for this function:
1851 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1852 * striping can be specified or not
1855 rc = mdd_la_get(env, son, attr);
1859 /* calling ->ah_make_hint() is used to transfer information from parent */
1860 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1862 handle = mdd_trans_create(env, mdd);
1864 GOTO(out_free, rc = PTR_ERR(handle));
1867 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1868 * Should this be fixed?
1870 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1871 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1872 spec->sp_cr_flags, spec->no_create);
1874 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1875 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1876 spec->u.sp_ea.eadatalen);
1881 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1882 XATTR_NAME_LOV, 0, handle);
1886 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1891 rc = mdd_trans_start(env, mdd, handle);
1895 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1901 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1904 rc = mdd_trans_stop(env, mdd, rc, handle);
1910 static int mdd_declare_object_initialize(const struct lu_env *env,
1911 struct mdd_object *parent,
1912 struct mdd_object *child,
1913 const struct lu_attr *attr,
1914 struct thandle *handle)
1919 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1920 if (!S_ISDIR(attr->la_mode))
1923 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1928 rc = mdo_declare_ref_add(env, child, handle);
1932 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1938 static int mdd_object_initialize(const struct lu_env *env,
1939 const struct lu_fid *pfid,
1940 struct mdd_object *child,
1941 struct lu_attr *attr, struct thandle *handle,
1942 const struct md_op_spec *spec)
1947 if (S_ISDIR(attr->la_mode)) {
1948 /* Add "." and ".." for newly created dir */
1949 mdo_ref_add(env, child, handle);
1950 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1951 S_IFDIR, dot, handle);
1953 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1956 mdo_ref_del(env, child, handle);
1963 * This function checks whether it can create a file/dir under the
1964 * directory(@pobj). The directory(@pobj) is not being locked by
1967 * \param[in] env execution environment
1968 * \param[in] pobj the directory to create files
1969 * \param[in] pattr the attributes of the directory
1970 * \param[in] lname the name of the created file/dir
1971 * \param[in] cattr the attributes of the file/dir
1972 * \param[in] spec create specification
1974 * \retval = 0 it is allowed to create file/dir under
1976 * \retval negative error not allowed to create file/dir
1977 * under the directory
1979 static int mdd_create_sanity_check(const struct lu_env *env,
1980 struct md_object *pobj,
1981 const struct lu_attr *pattr,
1982 const struct lu_name *lname,
1983 struct lu_attr *cattr,
1984 struct md_op_spec *spec)
1986 struct mdd_thread_info *info = mdd_env_info(env);
1987 struct lu_fid *fid = &info->mti_fid;
1988 struct mdd_object *obj = md2mdd_obj(pobj);
1989 struct mdd_device *m = mdo2mdd(pobj);
1990 bool check_perm = true;
1995 if (mdd_is_dead_obj(obj))
1999 * In some cases this lookup is not needed - we know before if name
2000 * exists or not because MDT performs lookup for it.
2001 * name length check is done in lookup.
2003 if (spec->sp_cr_lookup) {
2005 * Check if the name already exist, though it will be checked in
2006 * _index_insert also, for avoiding rolling back if exists
2009 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2010 MAY_WRITE | MAY_EXEC);
2012 RETURN(rc ? : -EEXIST);
2014 /* Permission is already being checked in mdd_lookup */
2018 if (S_ISDIR(cattr->la_mode) &&
2019 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2020 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2021 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2023 if (le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC &&
2024 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_SPECIFIC &&
2025 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2027 CERROR("%s: invalid lmv_user_md: magic = %x, "
2028 "stripe_offset = %d, stripe_count = %u: "
2029 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2030 le32_to_cpu(lum->lum_magic),
2031 (int)le32_to_cpu(lum->lum_stripe_offset),
2032 le32_to_cpu(lum->lum_stripe_count), rc);
2037 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2042 if (pattr->la_mode & S_ISGID) {
2043 cattr->la_gid = pattr->la_gid;
2044 if (S_ISDIR(cattr->la_mode)) {
2045 cattr->la_mode |= S_ISGID;
2046 cattr->la_valid |= LA_MODE;
2050 /* Inherit project ID from parent directory */
2051 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2052 cattr->la_projid = pattr->la_projid;
2053 if (S_ISDIR(cattr->la_mode)) {
2054 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2055 cattr->la_valid |= LA_FLAGS;
2057 cattr->la_valid |= LA_PROJID;
2060 rc = mdd_name_check(m, lname);
2064 switch (cattr->la_mode & S_IFMT) {
2066 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2068 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2069 RETURN(-ENAMETOOLONG);
2088 static int mdd_declare_create_object(const struct lu_env *env,
2089 struct mdd_device *mdd,
2090 struct mdd_object *p, struct mdd_object *c,
2091 struct lu_attr *attr,
2092 struct thandle *handle,
2093 const struct md_op_spec *spec,
2094 struct lu_buf *def_acl_buf,
2095 struct lu_buf *acl_buf,
2096 struct dt_allocation_hint *hint)
2098 const struct lu_buf *buf;
2101 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2106 #ifdef CONFIG_FS_POSIX_ACL
2107 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2108 /* if dir, then can inherit default ACl */
2109 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2110 XATTR_NAME_ACL_DEFAULT,
2116 if (acl_buf->lb_len > 0) {
2117 rc = mdo_declare_attr_set(env, c, attr, handle);
2121 rc = mdo_declare_xattr_set(env, c, acl_buf,
2122 XATTR_NAME_ACL_ACCESS, 0, handle);
2127 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2131 /* replay case, create LOV EA from client data */
2132 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2133 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2134 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2135 spec->u.sp_ea.eadatalen);
2136 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2142 if (S_ISLNK(attr->la_mode)) {
2143 const char *target_name = spec->u.sp_symname;
2144 int sym_len = strlen(target_name);
2145 const struct lu_buf *buf;
2147 buf = mdd_buf_get_const(env, target_name, sym_len);
2148 rc = dt_declare_record_write(env, mdd_object_child(c),
2154 if (spec->sp_cr_file_secctx_name != NULL) {
2155 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2156 spec->sp_cr_file_secctx_size);
2157 rc = mdo_declare_xattr_set(env, c, buf,
2158 spec->sp_cr_file_secctx_name, 0,
2167 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2168 struct mdd_object *p, struct mdd_object *c,
2169 const struct lu_name *name,
2170 struct lu_attr *attr,
2171 struct thandle *handle,
2172 const struct md_op_spec *spec,
2173 struct linkea_data *ldata,
2174 struct lu_buf *def_acl_buf,
2175 struct lu_buf *acl_buf,
2176 struct dt_allocation_hint *hint)
2180 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2181 def_acl_buf, acl_buf, hint);
2185 if (S_ISDIR(attr->la_mode)) {
2186 rc = mdo_declare_ref_add(env, p, handle);
2191 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2192 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2196 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2197 enum changelog_rec_type type;
2199 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2200 name->ln_name, handle);
2204 rc = mdd_declare_links_add(env, c, handle, ldata);
2209 la->la_valid = LA_CTIME | LA_MTIME;
2210 rc = mdo_declare_attr_set(env, p, la, handle);
2214 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2215 S_ISREG(attr->la_mode) ? CL_CREATE :
2216 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2218 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2227 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2228 struct lu_attr *la, struct lu_buf *def_acl_buf,
2229 struct lu_buf *acl_buf)
2234 if (S_ISLNK(la->la_mode)) {
2235 acl_buf->lb_len = 0;
2236 def_acl_buf->lb_len = 0;
2240 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2241 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2242 XATTR_NAME_ACL_DEFAULT);
2243 mdd_read_unlock(env, pobj);
2245 /* If there are default ACL, fix mode/ACL by default ACL */
2246 def_acl_buf->lb_len = rc;
2247 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2248 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2249 acl_buf->lb_len = rc;
2250 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2253 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2254 /* If there are no default ACL, fix mode by mask */
2255 struct lu_ucred *uc = lu_ucred(env);
2257 /* The create triggered by MDT internal events, such as
2258 * LFSCK reset, will not contain valid "uc". */
2259 if (unlikely(uc != NULL))
2260 la->la_mode &= ~uc->uc_umask;
2262 acl_buf->lb_len = 0;
2263 def_acl_buf->lb_len = 0;
2270 * Create a metadata object and initialize it, set acl, xattr.
2272 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2273 struct mdd_object *son, struct lu_attr *attr,
2274 struct md_op_spec *spec, struct lu_buf *acl_buf,
2275 struct lu_buf *def_acl_buf,
2276 struct dt_allocation_hint *hint,
2277 struct thandle *handle)
2279 const struct lu_buf *buf;
2282 mdd_write_lock(env, son, MOR_TGT_CHILD);
2283 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2288 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2289 * created in declare phase, they also needs to be added to master
2290 * object as sub-directory entry. So it has to initialize the master
2291 * object, then set dir striped EA.(in mdo_xattr_set) */
2292 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2295 GOTO(err_destroy, rc);
2298 * in case of replay we just set LOVEA provided by the client
2299 * XXX: I think it would be interesting to try "old" way where
2300 * MDT calls this xattr_set(LOV) in a different transaction.
2301 * probably this way we code can be made better.
2304 /* During creation, there are only a few cases we need do xattr_set to
2306 * 1. regular file: see comments above.
2307 * 2. dir: inherit default striping or pool settings from parent.
2308 * 3. create striped directory with provided stripeEA.
2309 * 4. create striped directory because inherit default layout from the
2312 if (spec->no_create ||
2313 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2314 S_ISDIR(attr->la_mode)) {
2315 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2316 spec->u.sp_ea.eadatalen);
2317 rc = mdo_xattr_set(env, son, buf,
2318 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2322 GOTO(err_destroy, rc);
2325 #ifdef CONFIG_FS_POSIX_ACL
2326 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2327 S_ISDIR(attr->la_mode)) {
2328 /* set default acl */
2329 rc = mdo_xattr_set(env, son, def_acl_buf,
2330 XATTR_NAME_ACL_DEFAULT, 0,
2333 GOTO(err_destroy, rc);
2335 /* set its own acl */
2336 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2337 rc = mdo_xattr_set(env, son, acl_buf,
2338 XATTR_NAME_ACL_ACCESS,
2341 GOTO(err_destroy, rc);
2345 if (S_ISLNK(attr->la_mode)) {
2346 struct lu_ucred *uc = lu_ucred_assert(env);
2347 struct dt_object *dt = mdd_object_child(son);
2348 const char *target_name = spec->u.sp_symname;
2349 int sym_len = strlen(target_name);
2352 buf = mdd_buf_get_const(env, target_name, sym_len);
2353 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2355 CFS_CAP_SYS_RESOURCE_MASK);
2360 GOTO(err_initlized, rc = -EFAULT);
2363 if (spec->sp_cr_file_secctx_name != NULL) {
2364 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2365 spec->sp_cr_file_secctx_size);
2366 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2369 GOTO(err_initlized, rc);
2373 if (unlikely(rc != 0)) {
2375 if (S_ISDIR(attr->la_mode)) {
2376 /* Drop the reference, no need to delete "."/"..",
2377 * because the object to be destroied directly. */
2378 rc2 = mdo_ref_del(env, son, handle);
2382 rc2 = mdo_ref_del(env, son, handle);
2386 mdo_destroy(env, son, handle);
2389 mdd_write_unlock(env, son);
2393 static int mdd_index_delete(const struct lu_env *env,
2394 struct mdd_object *mdd_pobj,
2395 struct lu_attr *cattr,
2396 const struct lu_name *lname)
2398 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2399 struct thandle *handle;
2403 handle = mdd_trans_create(env, mdd);
2405 RETURN(PTR_ERR(handle));
2407 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2412 if (S_ISDIR(cattr->la_mode)) {
2413 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2418 /* Since this will only be used in the error handler path,
2419 * Let's set the thandle to be local and not mess the transno */
2420 handle->th_local = 1;
2421 rc = mdd_trans_start(env, mdd, handle);
2425 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2426 S_ISDIR(cattr->la_mode), handle);
2430 rc = mdd_trans_stop(env, mdd, rc, handle);
2436 * Create object and insert it into namespace.
2438 * Two operations have to be performed:
2440 * - an allocation of a new object (->do_create()), and
2441 * - an insertion into a parent index (->dio_insert()).
2443 * Due to locking, operation order is not important, when both are
2444 * successful, *but* error handling cases are quite different:
2446 * - if insertion is done first, and following object creation fails,
2447 * insertion has to be rolled back, but this operation might fail
2448 * also leaving us with dangling index entry.
2450 * - if creation is done first, is has to be undone if insertion fails,
2451 * leaving us with leaked space, which is not good but not fatal.
2453 * It seems that creation-first is simplest solution, but it is sub-optimal
2459 * case, because second mkdir is bound to create object, only to
2460 * destroy it immediately.
2462 * To avoid this follow local file systems that do double lookup:
2464 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2465 * 1. create (mdd_create_object_internal())
2466 * 2. insert (__mdd_index_insert(), lookup again)
2468 * \param[in] pobj parent object
2469 * \param[in] lname name of child being created
2470 * \param[in,out] child child object being created
2471 * \param[in] spec additional create parameters
2472 * \param[in] ma attributes for new child object
2474 * \retval 0 on success
2475 * \retval negative errno on failure
2477 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2478 const struct lu_name *lname, struct md_object *child,
2479 struct md_op_spec *spec, struct md_attr *ma)
2481 struct mdd_thread_info *info = mdd_env_info(env);
2482 struct lu_attr *la = &info->mti_la_for_fix;
2483 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2484 struct mdd_object *son = md2mdd_obj(child);
2485 struct mdd_device *mdd = mdo2mdd(pobj);
2486 struct lu_attr *attr = &ma->ma_attr;
2487 struct thandle *handle;
2488 struct lu_attr *pattr = &info->mti_pattr;
2489 struct lu_buf acl_buf;
2490 struct lu_buf def_acl_buf;
2491 struct linkea_data *ldata = &info->mti_link_data;
2492 const char *name = lname->ln_name;
2493 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2498 rc = mdd_la_get(env, mdd_pobj, pattr);
2502 /* Sanity checks before big job. */
2503 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2507 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2508 GOTO(out_free, rc = -EINPROGRESS);
2510 handle = mdd_trans_create(env, mdd);
2512 GOTO(out_free, rc = PTR_ERR(handle));
2514 lu_buf_check_and_alloc(&info->mti_xattr_buf,
2515 mdd->mdd_dt_conf.ddp_max_ea_size);
2516 acl_buf = info->mti_xattr_buf;
2517 def_acl_buf.lb_buf = info->mti_key;
2518 def_acl_buf.lb_len = sizeof(info->mti_key);
2519 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2523 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2525 memset(ldata, 0, sizeof(*ldata));
2526 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2527 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2530 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2531 &tfid, lname, 1, 0, ldata);
2533 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2534 mdd_object_fid(mdd_pobj),
2535 lname, 1, 0, ldata);
2538 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2539 handle, spec, ldata, &def_acl_buf, &acl_buf,
2544 rc = mdd_trans_start(env, mdd, handle);
2548 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2549 &def_acl_buf, hint, handle);
2553 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2554 mdd_write_lock(env, son, MOR_TGT_CHILD);
2555 son->mod_flags |= VOLATILE_OBJ;
2556 rc = mdd_orphan_insert(env, son, handle);
2557 GOTO(out_volatile, rc);
2559 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2560 attr->la_mode, name, handle);
2562 GOTO(err_created, rc);
2564 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2567 /* update parent directory mtime/ctime */
2569 la->la_valid = LA_CTIME | LA_MTIME;
2570 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2572 GOTO(err_insert, rc);
2578 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2579 rc2 = mdd_orphan_delete(env, son, handle);
2581 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2582 S_ISDIR(attr->la_mode),
2588 mdd_write_lock(env, son, MOR_TGT_CHILD);
2589 if (S_ISDIR(attr->la_mode)) {
2590 /* Drop the reference, no need to delete "."/"..",
2591 * because the object is to be destroyed directly. */
2592 rc2 = mdo_ref_del(env, son, handle);
2594 mdd_write_unlock(env, son);
2599 /* For volatile files drop one link immediately, since there is
2600 * no filename in the namespace, and save any error returned. */
2601 rc2 = mdo_ref_del(env, son, handle);
2603 mdd_write_unlock(env, son);
2604 if (unlikely(rc == 0))
2609 /* Don't destroy the volatile object on success */
2610 if (likely(rc != 0))
2611 mdo_destroy(env, son, handle);
2612 mdd_write_unlock(env, son);
2615 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2616 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2617 rc = mdd_changelog_ns_store(env, mdd,
2618 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2619 S_ISREG(attr->la_mode) ? CL_CREATE :
2620 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2621 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2624 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2626 /* If creation fails, it is most likely due to the remote update
2627 * failure, because local transaction will mostly succeed at
2628 * this stage. There is no easy way to rollback all of previous
2629 * updates, so let's remove the object from namespace, and
2630 * LFSCK should handle the orphan object. */
2631 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2632 mdd_index_delete(env, mdd_pobj, attr, lname);
2636 if (is_vmalloc_addr(ldata->ld_buf))
2637 /* if we vmalloced a large buffer drop it */
2638 lu_buf_free(ldata->ld_buf);
2640 /* The child object shouldn't be cached anymore */
2642 set_bit(LU_OBJECT_HEARD_BANSHEE,
2643 &child->mo_lu.lo_header->loh_flags);
2648 * Get locks on parents in proper order
2649 * RETURN: < 0 - error, rename_order if successful
2657 static int mdd_rename_order(const struct lu_env *env,
2658 struct mdd_device *mdd,
2659 struct mdd_object *src_pobj,
2660 const struct lu_attr *pattr,
2661 struct mdd_object *tgt_pobj)
2663 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2667 if (src_pobj == tgt_pobj)
2668 RETURN(MDD_RN_SAME);
2670 /* compared the parent child relationship of src_p&tgt_p */
2671 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2673 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2676 rc = mdd_is_parent(env, mdd, src_pobj, pattr,
2690 /* has not mdd_write{read}_lock on any obj yet. */
2691 static int mdd_rename_sanity_check(const struct lu_env *env,
2692 struct mdd_object *src_pobj,
2693 const struct lu_attr *pattr,
2694 struct mdd_object *tgt_pobj,
2695 const struct lu_attr *tpattr,
2696 struct mdd_object *sobj,
2697 const struct lu_attr *cattr,
2698 struct mdd_object *tobj,
2699 const struct lu_attr *tattr)
2704 /* XXX: when get here, sobj must NOT be NULL,
2705 * the other case has been processed in cld_rename
2706 * before mdd_rename and enable MDS_PERM_BYPASS. */
2710 * If we are using project inheritance, we only allow renames
2711 * into our tree when the project IDs are the same; otherwise
2712 * tree quota mechanism would be circumvented.
2714 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2715 tpattr->la_projid != cattr->la_projid) ||
2716 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2717 (pattr->la_projid != tpattr->la_projid)))
2720 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2724 /* XXX: when get here, "tobj == NULL" means tobj must
2725 * NOT exist (neither on remote MDS, such case has been
2726 * processed in cld_rename before mdd_rename and enable
2728 * So check may_create, but not check may_unlink. */
2730 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2731 (src_pobj != tgt_pobj));
2733 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2734 (src_pobj != tgt_pobj), 1);
2736 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2737 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2742 static int mdd_declare_rename(const struct lu_env *env,
2743 struct mdd_device *mdd,
2744 struct mdd_object *mdd_spobj,
2745 struct mdd_object *mdd_tpobj,
2746 struct mdd_object *mdd_sobj,
2747 struct mdd_object *mdd_tobj,
2748 const struct lu_name *sname,
2749 const struct lu_name *tname,
2751 struct linkea_data *ldata,
2752 struct thandle *handle)
2754 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2757 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2758 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2764 /* name from source dir */
2765 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2769 /* .. from source child */
2770 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2771 /* source child can be directory,
2772 * counted by source dir's nlink */
2773 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2776 if (mdd_spobj != mdd_tpobj) {
2777 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2782 rc = mdo_declare_index_insert(env, mdd_sobj,
2784 S_IFDIR, dotdot, handle);
2789 /* new target child can be directory,
2790 * counted by target dir's nlink */
2791 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2796 la->la_valid = LA_CTIME | LA_MTIME;
2797 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2801 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2805 la->la_valid = LA_CTIME;
2806 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2810 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2815 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2816 mdd_object_type(mdd_sobj),
2817 tname->ln_name, handle);
2821 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2822 /* delete target child in target parent directory */
2823 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2828 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2832 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2833 /* target child can be directory,
2834 * delete "." reference in target child directory */
2835 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2839 /* delete ".." reference in target parent directory */
2840 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2845 la->la_valid = LA_CTIME;
2846 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2850 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2855 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2863 /* src object can be remote that is why we use only fid and type of object */
2864 static int mdd_rename(const struct lu_env *env,
2865 struct md_object *src_pobj, struct md_object *tgt_pobj,
2866 const struct lu_fid *lf, const struct lu_name *lsname,
2867 struct md_object *tobj, const struct lu_name *ltname,
2870 const char *sname = lsname->ln_name;
2871 const char *tname = ltname->ln_name;
2872 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2873 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2874 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2875 struct mdd_device *mdd = mdo2mdd(src_pobj);
2876 struct mdd_object *mdd_sobj = NULL; /* source object */
2877 struct mdd_object *mdd_tobj = NULL;
2878 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2879 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2880 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2881 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2882 struct thandle *handle;
2883 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2884 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2885 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2888 bool tobj_locked = 0;
2889 unsigned cl_flags = 0;
2893 /* let unlink to complete and commit */
2894 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLY_DATA_RACE, 2 + cfs_fail_val);
2897 mdd_tobj = md2mdd_obj(tobj);
2899 mdd_sobj = mdd_object_find(env, mdd, lf);
2900 if (IS_ERR(mdd_sobj))
2901 RETURN(PTR_ERR(mdd_sobj));
2903 rc = mdd_la_get(env, mdd_sobj, cattr);
2905 GOTO(out_pending, rc);
2907 rc = mdd_la_get(env, mdd_spobj, pattr);
2909 GOTO(out_pending, rc);
2912 rc = mdd_la_get(env, mdd_tobj, tattr);
2914 GOTO(out_pending, rc);
2915 /* search for an existing archive.
2916 * we should check ahead as the object
2917 * can be destroyed in this transaction */
2918 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2919 cl_flags |= CLF_RENAME_LAST_EXISTS;
2922 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2924 GOTO(out_pending, rc);
2926 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2927 mdd_sobj, cattr, mdd_tobj, tattr);
2929 GOTO(out_pending, rc);
2931 rc = mdd_name_check(mdd, ltname);
2933 GOTO(out_pending, rc);
2935 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2936 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2938 GOTO(out_pending, rc);
2940 handle = mdd_trans_create(env, mdd);
2942 GOTO(out_pending, rc = PTR_ERR(handle));
2944 memset(ldata, 0, sizeof(*ldata));
2945 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
2946 lsname, mdd_object_fid(mdd_tpobj), ltname,
2951 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2952 mdd_tobj, lsname, ltname, ma, ldata, handle);
2956 rc = mdd_trans_start(env, mdd, handle);
2960 is_dir = S_ISDIR(cattr->la_mode);
2962 /* Remove source name from source directory */
2963 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2967 /* "mv dir1 dir2" needs "dir1/.." link update */
2968 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2969 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2971 GOTO(fixup_spobj2, rc);
2973 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2976 GOTO(fixup_spobj, rc);
2979 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2980 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2982 /* tname might been renamed to something else */
2983 GOTO(fixup_spobj, rc);
2986 /* Insert new fid with target name into target dir */
2987 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2990 GOTO(fixup_tpobj, rc);
2992 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2993 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2995 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2996 la->la_valid = LA_CTIME;
2997 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2999 GOTO(fixup_tpobj, rc);
3001 /* Update the linkEA for the source object */
3002 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3003 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
3004 mdo2fid(mdd_tpobj), ltname, handle, ldata,
3007 /* Old files might not have EA entry */
3008 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
3009 lsname, handle, NULL, 0);
3010 mdd_write_unlock(env, mdd_sobj);
3011 /* We don't fail the transaction if the link ea can't be
3012 updated -- fid2path will use alternate lookup method. */
3015 /* Remove old target object
3016 * For tobj is remote case cmm layer has processed
3017 * and set tobj to NULL then. So when tobj is NOT NULL,
3018 * it must be local one.
3020 if (tobj && mdd_object_exists(mdd_tobj)) {
3021 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
3023 if (mdd_is_dead_obj(mdd_tobj)) {
3024 /* shld not be dead, something is wrong */
3025 CERROR("tobj is dead, something is wrong\n");
3029 mdo_ref_del(env, mdd_tobj, handle);
3031 /* Remove dot reference. */
3032 if (S_ISDIR(tattr->la_mode))
3033 mdo_ref_del(env, mdd_tobj, handle);
3036 /* fetch updated nlink */
3037 rc = mdd_la_get(env, mdd_tobj, tattr);
3039 CERROR("%s: Failed to get nlink for tobj "
3041 mdd2obd_dev(mdd)->obd_name,
3042 PFID(tpobj_fid), rc);
3043 GOTO(fixup_tpobj, rc);
3046 la->la_valid = LA_CTIME;
3047 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3049 CERROR("%s: Failed to set ctime for tobj "
3051 mdd2obd_dev(mdd)->obd_name,
3052 PFID(tpobj_fid), rc);
3053 GOTO(fixup_tpobj, rc);
3056 /* XXX: this transfer to ma will be removed with LOD/OSP */
3057 ma->ma_attr = *tattr;
3058 ma->ma_valid |= MA_INODE;
3059 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3062 CERROR("%s: Failed to unlink tobj "
3064 mdd2obd_dev(mdd)->obd_name,
3065 PFID(tpobj_fid), rc);
3066 GOTO(fixup_tpobj, rc);
3069 /* fetch updated nlink */
3070 rc = mdd_la_get(env, mdd_tobj, tattr);
3071 if (rc == -ENOENT) {
3072 /* the object got removed, let's
3073 * return the latest known attributes */
3074 tattr->la_nlink = 0;
3076 } else if (rc != 0) {
3077 CERROR("%s: Failed to get nlink for tobj "
3079 mdd2obd_dev(mdd)->obd_name,
3080 PFID(tpobj_fid), rc);
3081 GOTO(fixup_tpobj, rc);
3083 /* XXX: this transfer to ma will be removed with LOD/OSP */
3084 ma->ma_attr = *tattr;
3085 ma->ma_valid |= MA_INODE;
3087 if (tattr->la_nlink == 0)
3088 cl_flags |= CLF_RENAME_LAST;
3090 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3093 la->la_valid = LA_CTIME | LA_MTIME;
3094 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3096 GOTO(fixup_tpobj, rc);
3098 if (mdd_spobj != mdd_tpobj) {
3099 la->la_valid = LA_CTIME | LA_MTIME;
3100 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3102 GOTO(fixup_tpobj, rc);
3109 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3111 CWARN("tp obj fix error %d\n",rc2);
3113 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3114 !mdd_is_dead_obj(mdd_tobj)) {
3116 mdo_ref_add(env, mdd_tobj, handle);
3118 mdo_ref_add(env, mdd_tobj, handle);
3121 rc2 = __mdd_index_insert(env, mdd_tpobj,
3123 mdd_object_type(mdd_tobj),
3126 CWARN("tp obj fix error: rc = %d\n", rc2);
3131 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3132 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3134 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3135 mdd2obd_dev(mdd)->obd_name, rc2);
3138 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3141 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3142 mdd2obd_dev(mdd)->obd_name, rc2);
3147 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3148 mdd_object_type(mdd_sobj), sname,
3151 CWARN("sp obj fix error: rc = %d\n", rc2);
3156 mdd_write_unlock(env, mdd_tobj);
3159 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3160 mdd_tobj, tpobj_fid, lf, spobj_fid,
3161 ltname, lsname, handle);
3164 rc = mdd_trans_stop(env, mdd, rc, handle);
3167 mdd_object_put(env, mdd_sobj);
3172 * During migration once the parent FID has been changed,
3173 * we need update the parent FID in linkea.
3175 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3176 struct mdd_object *parent,
3177 struct mdd_object *newparent,
3178 struct mdd_object *child,
3179 const char *name, int namelen,
3180 struct thandle *handle,
3183 struct mdd_thread_info *info = mdd_env_info(env);
3184 struct linkea_data ldata = { NULL };
3185 struct lu_buf *buf = &info->mti_link_buf;
3191 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3192 if (buf->lb_buf == NULL)
3196 rc = mdd_links_read(env, child, &ldata);
3198 if (rc == -ENOENT || rc == -ENODATA)
3203 LASSERT(ldata.ld_leh != NULL);
3204 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3205 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3206 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3207 struct lu_name lname;
3210 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3213 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3214 !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3215 ldata.ld_lee = (struct link_ea_entry *)
3216 ((char *)ldata.ld_lee +
3221 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3222 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3223 lname.ln_namelen, lname.ln_name,
3224 PFID(mdd_object_fid(newparent)));
3225 /* update to the new parent fid */
3226 linkea_entry_pack(ldata.ld_lee, &lname,
3227 mdd_object_fid(newparent));
3229 rc = mdd_declare_links_add(env, child, handle, &ldata);
3231 rc = mdd_links_write(env, child, &ldata, handle);
3237 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3238 struct mdd_object *parent,
3239 struct mdd_object *newparent,
3240 struct mdd_object *child,
3241 const char *name, int namelen,
3242 struct thandle *handle)
3244 return mdd_linkea_update_child_internal(env, parent, newparent,
3246 namelen, handle, true);
3249 static int mdd_linkea_update_child(const struct lu_env *env,
3250 struct mdd_object *parent,
3251 struct mdd_object *newparent,
3252 struct mdd_object *child,
3253 const char *name, int namelen,
3254 struct thandle *handle)
3256 return mdd_linkea_update_child_internal(env, parent, newparent,
3258 namelen, handle, false);
3261 static int mdd_update_linkea_internal(const struct lu_env *env,
3262 struct mdd_object *mdd_pobj,
3263 struct mdd_object *mdd_sobj,
3264 struct mdd_object *mdd_tobj,
3265 const struct lu_name *child_name,
3266 struct linkea_data *ldata,
3267 struct thandle *handle,
3270 struct mdd_thread_info *info = mdd_env_info(env);
3275 LASSERT(ldata->ld_buf != NULL);
3276 LASSERT(ldata->ld_leh != NULL);
3278 /* If it is mulitple links file, we need update the name entry for
3280 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3281 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3282 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3283 struct mdd_object *pobj;
3284 struct lu_name lname;
3287 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3289 pobj = mdd_object_find(env, mdd, &fid);
3291 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3292 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3297 if (!mdd_object_exists(pobj)) {
3298 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3299 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3303 if (pobj == mdd_pobj &&
3304 lname.ln_namelen == child_name->ln_namelen &&
3305 strncmp(lname.ln_name, child_name->ln_name,
3306 lname.ln_namelen) == 0) {
3307 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3308 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3313 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3314 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3315 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3318 /* Remove source name from source directory */
3319 /* Insert new fid with target name into target dir */
3320 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3325 rc = mdo_declare_index_insert(env, pobj,
3326 mdd_object_fid(mdd_tobj),
3327 mdd_object_type(mdd_tobj),
3328 lname.ln_name, handle);
3332 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3336 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3340 char *tmp_name = info->mti_key;
3342 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3343 /* lnamelen is too big(> NAME_MAX + 16),
3344 * something wrong about this linkea, let's
3346 CWARN("%s: the name %.*s is too long under "
3347 DFID"\n", mdd2obd_dev(mdd)->obd_name,
3348 lname.ln_namelen, lname.ln_name,
3353 /* Note: lname might be without \0 at the end, see
3354 * linkea_entry_unpack(), let's add extra \0 by
3356 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3357 lname.ln_namelen, lname.ln_name);
3358 lname.ln_name = tmp_name;
3360 /* Let's check if this linkEA still valid, before
3361 * it might be packed into the RPC buffer. */
3362 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3363 &info->mti_fid, NULL);
3364 if (rc < 0 || !lu_fid_eq(&info->mti_fid,
3365 mdd_object_fid(mdd_sobj)))
3366 GOTO(next_put, rc == -ENOENT ? 0 : rc);
3368 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3372 rc = __mdd_index_insert(env, pobj,
3373 mdd_object_fid(mdd_tobj),
3374 mdd_object_type(mdd_tobj),
3379 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3380 rc = mdo_ref_add(env, mdd_tobj, handle);
3381 mdd_write_unlock(env, mdd_tobj);
3385 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3386 mdo_ref_del(env, mdd_sobj, handle);
3387 mdd_write_unlock(env, mdd_sobj);
3390 mdd_object_put(env, pobj);
3394 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3401 static int mdd_migrate_xattrs(const struct lu_env *env,
3402 struct mdd_object *mdd_sobj,
3403 struct mdd_object *mdd_tobj)
3405 struct mdd_thread_info *info = mdd_env_info(env);
3406 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3408 struct thandle *handle;
3414 struct lu_buf list_xbuf;
3417 /* retrieve xattr list from the old object */
3418 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3419 if (list_xsize == -ENODATA)
3425 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3426 if (info->mti_big_buf.lb_buf == NULL)
3429 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3430 list_xbuf.lb_len = list_xsize;
3431 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3436 xname = list_xbuf.lb_buf;
3438 xlen = strnlen(xname, rem - 1) + 1;
3439 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3440 strcmp(XATTR_NAME_LMV, xname) == 0)
3443 /* For directory, if there are default layout, migrate here */
3444 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3445 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3448 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3449 if (xsize == -ENODATA)
3454 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3455 if (info->mti_link_buf.lb_buf == NULL)
3456 GOTO(out, rc = -ENOMEM);
3458 xbuf.lb_len = xsize;
3459 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3460 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3466 handle = mdd_trans_create(env, mdd);
3468 GOTO(out, rc = PTR_ERR(handle));
3470 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3473 GOTO(stop_trans, rc);
3474 /* Note: this transaction is part of migration, and it is not
3475 * the last step of migration, so we set th_local = 1 to avoid
3476 * update last rcvd for this transaction */
3477 handle->th_local = 1;
3478 rc = mdd_trans_start(env, mdd, handle);
3480 GOTO(stop_trans, rc);
3483 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3485 GOTO(stop_trans, rc = 0);
3487 if (unlikely(rc == -ENOSPC &&
3488 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3489 rc = linkea_overflow_shrink(
3490 (struct linkea_data *)(xbuf.lb_buf));
3491 if (likely(rc > 0)) {
3498 GOTO(stop_trans, rc);
3500 rc = mdd_trans_stop(env, mdd, rc, handle);
3505 memmove(xname, xname + xlen, rem);
3511 static int mdd_declare_migrate_create(const struct lu_env *env,
3512 struct mdd_object *mdd_pobj,
3513 struct mdd_object *mdd_sobj,
3514 struct mdd_object *mdd_tobj,
3515 struct md_op_spec *spec,
3517 union lmv_mds_md *mgr_ea,
3518 struct linkea_data *ldata,
3519 struct thandle *handle)
3521 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3522 const struct lu_buf *buf;
3526 rc = mdd_declare_create_object_internal(env, mdd_pobj, mdd_tobj, la,
3527 handle, spec, NULL);
3531 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3536 if (S_ISLNK(la->la_mode)) {
3537 const char *target_name = spec->u.sp_symname;
3538 int sym_len = strlen(target_name);
3539 const struct lu_buf *buf;
3541 buf = mdd_buf_get_const(env, target_name, sym_len);
3542 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3546 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3547 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3552 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3553 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3554 spec->u.sp_ea.eadatalen);
3555 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3561 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3562 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3563 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3568 la_flag->la_valid = LA_FLAGS;
3569 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3570 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3575 static int mdd_migrate_create(const struct lu_env *env,
3576 struct mdd_object *mdd_pobj,
3577 struct mdd_object *mdd_sobj,
3578 struct mdd_object *mdd_tobj,
3579 const struct lu_name *lname,
3582 struct mdd_thread_info *info = mdd_env_info(env);
3583 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3584 struct md_op_spec *spec = &info->mti_spec;
3585 struct lu_buf lmm_buf = { NULL };
3586 struct lu_buf link_buf = { NULL };
3587 struct lu_buf mgr_buf;
3588 struct thandle *handle;
3589 struct lmv_mds_md_v1 *mgr_ea;
3590 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3591 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3593 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3597 /* prepare spec for create */
3598 memset(spec, 0, sizeof(*spec));
3599 spec->sp_cr_lookup = 0;
3600 spec->sp_feat = &dt_directory_features;
3601 if (S_ISLNK(la->la_mode)) {
3602 const struct lu_buf *buf;
3604 buf = lu_buf_check_and_alloc(
3605 &mdd_env_info(env)->mti_big_buf,
3608 link_buf.lb_len = la->la_size + 1;
3609 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3610 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3612 rc = rc != 0 ? rc : -EFAULT;
3613 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3614 mdd2obd_dev(mdd)->obd_name,
3615 PFID(mdd_object_fid(mdd_sobj)), rc);
3618 spec->u.sp_symname = link_buf.lb_buf;
3619 } else if (S_ISREG(la->la_mode)) {
3620 /* retrieve lov of the old object */
3621 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3622 if (rc != 0 && rc != -ENODATA)
3624 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3625 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3626 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3627 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3629 } else if (S_ISDIR(la->la_mode)) {
3630 rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
3631 if (rc == -ENODATA) {
3632 /* ignore the non-linkEA error */
3640 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3641 lu_buf_check_and_alloc(&info->mti_xattr_buf, mgr_easize);
3642 mgr_buf.lb_buf = info->mti_xattr_buf.lb_buf;
3643 mgr_buf.lb_len = mgr_easize;
3644 mgr_ea = mgr_buf.lb_buf;
3645 memset(mgr_ea, 0, sizeof(*mgr_ea));
3646 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3647 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3648 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3649 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3650 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3651 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3653 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3655 handle = mdd_trans_create(env, mdd);
3657 GOTO(out_free, rc = PTR_ERR(handle));
3659 /* Note: this transaction is part of migration, and it is not
3660 * the last step of migration, so we set th_local = 1 to avoid
3661 * update last rcvd for this transaction */
3662 handle->th_local = 1;
3663 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj, spec,
3664 la, mgr_buf.lb_buf, ldata, handle);
3666 GOTO(stop_trans, rc);
3668 rc = mdd_trans_start(env, mdd, handle);
3670 GOTO(stop_trans, rc);
3672 /* don't set nlink from the original object */
3673 la->la_valid &= ~LA_NLINK;
3675 /* create the target object */
3676 rc = mdd_create_object(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3679 GOTO(stop_trans, rc);
3681 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3682 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3684 GOTO(stop_trans, rc);
3687 /* Set MIGRATE EA on the source inode, so once the migration needs
3688 * to be re-done during failover, the re-do process can locate the
3689 * target object which is already being created. */
3690 rc = mdo_xattr_set(env, mdd_sobj, &mgr_buf, XATTR_NAME_LMV, 0, handle);
3692 GOTO(stop_trans, rc);
3694 /* Set immutable flag, so any modification is disabled until
3695 * the migration is done. Once the migration is interrupted,
3696 * if the resume process find the migrating object has both
3697 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3698 * flag and approve the migration */
3699 la_flag->la_valid = LA_FLAGS;
3700 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3701 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3704 rc = mdd_trans_stop(env, mdd, rc, handle);
3706 if (lmm_buf.lb_buf != NULL)
3707 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3711 static int mdd_migrate_entries(const struct lu_env *env,
3712 struct mdd_object *mdd_sobj,
3713 struct mdd_object *mdd_tobj)
3715 struct dt_object *next = mdd_object_child(mdd_sobj);
3716 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3717 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3718 struct thandle *handle;
3720 const struct dt_it_ops *iops;
3722 struct lu_dirent *ent;
3726 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3730 if (!dt_try_as_dir(env, next))
3731 GOTO(out_ent, rc = -ENOTDIR);
3733 * iterate directories
3735 iops = &next->do_index_ops->dio_it;
3736 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3738 GOTO(out_ent, rc = PTR_ERR(it));
3740 rc = iops->load(env, it, 0);
3742 rc = iops->next(env, it);
3746 * At this point and across for-loop:
3748 * rc == 0 -> ok, proceed.
3749 * rc > 0 -> end of directory.
3753 struct mdd_object *child;
3754 char *name = mdd_env_info(env)->mti_key;
3757 bool target_exist = false;
3759 len = iops->key_size(env, it);
3763 result = iops->rec(env, it, (struct dt_rec *)ent,
3764 LUDA_FID | LUDA_TYPE);
3765 if (result == -ESTALE)
3772 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3774 /* Insert new fid with target name into target dir */
3775 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3776 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3777 ent->lde_name[1] == '.'))
3780 child = mdd_object_find(env, mdd, &ent->lde_fid);
3782 GOTO(out, rc = PTR_ERR(child));
3784 /* child may not exist, but lu_object_attr will assert this,
3785 * get type from loh_attr directly */
3786 is_dir = S_ISDIR(child->mod_obj.mo_lu.lo_header->loh_attr);
3788 mdd_write_lock(env, child, MOR_SRC_CHILD);
3790 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3792 /* Check whether the name has been inserted to the target */
3793 if (dt_try_as_dir(env, dt_tobj)) {
3794 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3796 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3797 (struct dt_key *)name);
3798 if (unlikely(rc == 0))
3799 target_exist = true;
3802 handle = mdd_trans_create(env, mdd);
3804 GOTO(out_put, rc = PTR_ERR(handle));
3806 /* Note: this transaction is part of migration, and it is not
3807 * the last step of migration, so we set th_local = 1 to avoid
3808 * updating last rcvd for this transaction */
3809 handle->th_local = 1;
3810 if (likely(!target_exist)) {
3811 rc = mdo_declare_index_insert(env, mdd_tobj,
3813 child->mod_obj.mo_lu.lo_header->loh_attr,
3819 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3825 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3830 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3834 /* Update .. for child */
3835 rc = mdo_declare_index_delete(env, child, dotdot,
3840 rc = mdo_declare_index_insert(env, child,
3841 mdd_object_fid(mdd_tobj),
3842 S_IFDIR, dotdot, handle);
3847 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3854 rc = mdd_trans_start(env, mdd, handle);
3856 CERROR("%s: transaction start failed: rc = %d\n",
3857 mdd2obd_dev(mdd)->obd_name, rc);
3861 if (likely(!target_exist)) {
3862 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3863 child->mod_obj.mo_lu.lo_header->loh_attr, name,
3869 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3874 rc = __mdd_index_delete_only(env, child, dotdot,
3879 rc = __mdd_index_insert_only(env, child,
3880 mdd_object_fid(mdd_tobj), S_IFDIR,
3886 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3888 strlen(name), handle);
3891 mdd_write_unlock(env, child);
3892 mdd_object_put(env, child);
3893 rc = mdd_trans_stop(env, mdd, rc, handle);
3897 result = iops->next(env, it);
3898 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3899 GOTO(out, rc = -EINTR);
3901 if (result == -ESTALE)
3903 } while (result == 0);
3906 iops->fini(env, it);
3908 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3912 static int mdd_declare_update_linkea(const struct lu_env *env,
3913 struct mdd_object *mdd_pobj,
3914 struct mdd_object *mdd_sobj,
3915 struct mdd_object *mdd_tobj,
3916 const struct lu_name *child_name,
3917 struct linkea_data *ldata,
3918 struct thandle *handle)
3920 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3921 child_name, ldata, handle, 1);
3924 static int mdd_update_linkea(const struct lu_env *env,
3925 struct mdd_object *mdd_pobj,
3926 struct mdd_object *mdd_sobj,
3927 struct mdd_object *mdd_tobj,
3928 const struct lu_name *child_name,
3929 struct linkea_data *ldata,
3930 struct thandle *handle)
3932 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3933 child_name, ldata, handle, 0);
3936 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3937 struct mdd_object *mdd_pobj,
3938 struct mdd_object *mdd_sobj,
3939 struct mdd_object *mdd_tobj,
3940 const struct lu_name *lname,
3942 struct lu_attr *parent_la,
3943 struct linkea_data *ldata,
3944 struct thandle *handle)
3946 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3947 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3950 /* Revert IMMUTABLE flag */
3951 la_flag->la_valid = LA_FLAGS;
3952 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3953 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3957 /* delete entry from source dir */
3958 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3962 if (ldata->ld_buf != NULL) {
3963 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3964 mdd_tobj, lname, ldata, handle);
3969 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3970 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3975 handle->th_complex = 1;
3976 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3978 LU_XATTR_REPLACE, handle);
3983 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3984 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3990 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3991 mdd_object_type(mdd_tobj),
3992 lname->ln_name, handle);
3996 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
4000 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4001 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
4006 /* delete old object */
4007 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4011 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4012 /* delete old object */
4013 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4016 /* set nlink to 0 */
4017 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
4022 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
4026 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
4030 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4036 static int mdd_migrate_update_name(const struct lu_env *env,
4037 struct mdd_object *mdd_pobj,
4038 struct mdd_object *mdd_sobj,
4039 struct mdd_object *mdd_tobj,
4040 const struct lu_name *lname,
4043 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
4044 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4045 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
4046 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
4047 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
4048 struct thandle *handle;
4049 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
4050 const char *name = lname->ln_name;
4054 /* update time for parent */
4055 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
4056 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
4057 p_la->la_valid = LA_CTIME;
4059 rc = mdd_la_get(env, mdd_sobj, so_attr);
4063 ldata->ld_buf = NULL;
4064 rc = mdd_links_read(env, mdd_sobj, ldata);
4065 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
4068 handle = mdd_trans_create(env, mdd);
4070 RETURN(PTR_ERR(handle));
4072 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
4073 lname, so_attr, p_la, ldata,
4076 /* If the migration can not be fit in one transaction, just
4077 * leave it in the original MDT */
4079 GOTO(stop_trans, rc = 0);
4081 GOTO(stop_trans, rc);
4084 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
4085 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
4086 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4087 PFID(mdd_object_fid(mdd_tobj)));
4089 rc = mdd_trans_start(env, mdd, handle);
4091 GOTO(stop_trans, rc);
4093 /* Revert IMMUTABLE flag */
4094 la_flag->la_valid = LA_FLAGS;
4095 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
4096 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
4098 GOTO(stop_trans, rc);
4100 /* Remove source name from source directory */
4101 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
4103 GOTO(stop_trans, rc);
4105 if (ldata->ld_buf != NULL) {
4106 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
4107 lname, ldata, handle);
4109 GOTO(stop_trans, rc);
4111 /* linkea update might decrease the source object
4112 * nlink, let's get the attr again after ref_del */
4113 rc = mdd_la_get(env, mdd_sobj, so_attr);
4115 GOTO(stop_trans, rc);
4118 if (S_ISREG(so_attr->la_mode)) {
4119 if (so_attr->la_nlink == 1) {
4120 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4122 if (rc != 0 && rc != -ENODATA)
4123 GOTO(stop_trans, rc);
4125 rc = mdo_xattr_set(env, mdd_tobj, NULL,
4127 LU_XATTR_REPLACE, handle);
4129 GOTO(stop_trans, rc);
4133 /* Insert new fid with target name into target dir */
4134 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4135 mdd_object_type(mdd_tobj), name, handle);
4137 GOTO(stop_trans, rc);
4139 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4141 mdd_sobj->mod_flags |= DEAD_OBJ;
4142 rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4144 GOTO(out_unlock, rc);
4146 rc = mdd_orphan_insert(env, mdd_sobj, handle);
4148 GOTO(out_unlock, rc);
4150 mdo_ref_del(env, mdd_sobj, handle);
4152 mdo_ref_del(env, mdd_sobj, handle);
4154 /* Get the attr again after ref_del */
4155 rc = mdd_la_get(env, mdd_sobj, so_attr);
4157 GOTO(out_unlock, rc);
4159 ma->ma_attr = *so_attr;
4160 ma->ma_valid |= MA_INODE;
4162 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4164 GOTO(out_unlock, rc);
4166 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4167 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4168 mdo2fid(mdd_pobj), lname, lname, handle);
4170 CWARN("%s: changelog for migrate %s "DFID
4171 "under "DFID" failed: rc = %d\n",
4172 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4173 PFID(mdd_object_fid(mdd_sobj)),
4174 PFID(mdd_object_fid(mdd_pobj)), rc);
4175 /* Sigh, there are no easy way to migrate back the object, so
4176 * let's reset the result to 0 for now XXX */
4180 mdd_write_unlock(env, mdd_sobj);
4183 rc = mdd_trans_stop(env, mdd, rc, handle);
4188 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4189 const struct lu_fid *fid, __u32 *mdt_index)
4191 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4192 struct seq_server_site *ss;
4195 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4197 range->lsr_flags = LU_SEQ_RANGE_MDT;
4198 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4202 *mdt_index = range->lsr_index;
4207 * Check whether we should migrate the file/dir
4209 * < 0 permission check failed or other error.
4210 * = 0 the file can be migrated.
4211 * > 0 the file does not need to be migrated, mostly
4212 * for multiple link file
4214 static int mdd_migrate_sanity_check(const struct lu_env *env,
4215 struct mdd_object *pobj,
4216 const struct lu_attr *pattr,
4217 struct mdd_object *sobj,
4218 struct lu_attr *sattr)
4220 struct mdd_thread_info *info = mdd_env_info(env);
4221 struct linkea_data *ldata = &info->mti_link_data;
4222 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4224 struct lu_buf *mgr_buf;
4230 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4231 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4232 if (mgr_buf->lb_buf == NULL)
4235 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4237 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4239 /* If the object has migrateEA, it means IMMUTE flag
4240 * is being set by previous migration process, so it
4241 * needs to override the IMMUTE flag, otherwise the
4242 * following sanity check will fail */
4243 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4244 LMV_HASH_FLAG_MIGRATION) {
4245 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4247 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4248 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4249 mdd2obd_dev(mdd)->obd_name,
4250 PFID(mdd_object_fid(sobj)));
4254 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4255 sobj, sattr, NULL, NULL);
4259 /* Then it will check if the file should be migrated. If the file
4260 * has mulitple links, we only need migrate the file if all of its
4261 * entries has been migrated to the remote MDT */
4262 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4265 rc = mdd_links_read(env, sobj, ldata);
4267 /* For multiple links files, if there are no linkEA data at all,
4268 * means the file might be created before linkEA is enabled, and
4269 * all of its links should not be migrated yet, otherwise it
4270 * should have some linkEA there */
4271 if (rc == -ENOENT || rc == -ENODATA)
4276 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4277 /* If there are still links locally, then the file will not be
4279 LASSERT(ldata->ld_leh != NULL);
4281 /* If the linkEA is overflow, then means there are some unknown name
4282 * entries under unknown parents, that will prevent the migration. */
4283 if (unlikely(ldata->ld_leh->leh_overflow_time))
4286 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4287 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4288 struct lu_name lname;
4290 __u32 parent_mdt_index;
4292 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4294 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4297 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4301 /* Migrate the object only if none of its parents are on the
4303 if (parent_mdt_index != mdt_index)
4306 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4307 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4308 lname.ln_name, PFID(&fid));
4316 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4317 struct md_object *sobj, const struct lu_name *lname,
4318 struct md_object *tobj, struct md_attr *ma)
4320 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4321 struct mdd_device *mdd = mdo2mdd(pobj);
4322 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4323 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4324 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4325 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4326 bool created = false;
4330 /* If the file will being migrated, it will check whether
4331 * the file is being opened by someone else right now */
4332 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4333 if (mdd_sobj->mod_count > 0) {
4335 "%s: "DFID"%s is already opened count %d: rc = %d\n",
4336 mdd2obd_dev(mdd)->obd_name,
4337 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4338 mdd_sobj->mod_count, -EBUSY);
4339 mdd_read_unlock(env, mdd_sobj);
4340 GOTO(put, rc = -EBUSY);
4342 mdd_read_unlock(env, mdd_sobj);
4344 rc = mdd_la_get(env, mdd_sobj, so_attr);
4348 rc = mdd_la_get(env, mdd_pobj, pattr);
4352 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4359 /* Sigh, it is impossible to finish all of migration in a single
4360 * transaction, for example migrating big directory entries to the
4361 * new MDT, it needs insert all of name entries of children in the
4364 * So migration will be done in multiple steps and transactions.
4366 * 1. create an orphan object on the remote MDT in one transaction.
4367 * 2. migrate extend attributes to the new target file/directory.
4368 * 3. For directory, migrate the entries to the new MDT and update
4369 * linkEA of each children. Because we can not migrate all entries
4370 * in a single transaction, so the migrating directory will become
4371 * a striped directory during migration, so once the process is
4372 * interrupted, the directory is still accessible. (During lookup,
4373 * client will locate the name by searching both original and target
4375 * 4. Finally, update the name/FID to point to the new file/directory
4376 * in a separate transaction.
4379 /* step 1: Check whether the orphan object has been created, and create
4380 * orphan object on the remote MDT if needed */
4381 if (!mdd_object_exists(mdd_tobj)) {
4382 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4389 LASSERT(mdd_object_exists(mdd_tobj));
4390 /* step 2: migrate xattr */
4391 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4395 /* step 3: migrate name entries to the orphan object */
4396 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4397 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4400 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4401 OBD_FAIL_MDS_REINT_NET_REP)))
4404 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4407 LASSERT(mdd_object_exists(mdd_tobj));
4408 /* step 4: update name entry to the new object */
4409 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4414 /* newly created target was not locked, don't cache its attributes */
4416 mdd_invalidate(env, tobj);
4421 const struct md_dir_operations mdd_dir_ops = {
4422 .mdo_is_subdir = mdd_is_subdir,
4423 .mdo_lookup = mdd_lookup,
4424 .mdo_create = mdd_create,
4425 .mdo_rename = mdd_rename,
4426 .mdo_link = mdd_link,
4427 .mdo_unlink = mdd_unlink,
4428 .mdo_create_data = mdd_create_data,
4429 .mdo_migrate = mdd_migrate,