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 lf is the fid of the ancestor of p1;
261 * return -EREMOTE: if remote object is found, in this
262 * case fid of remote object is saved to @pf;
264 * otherwise: values < 0, errors.
266 static int mdd_is_parent(const struct lu_env *env,
267 struct mdd_device *mdd,
268 struct mdd_object *p1,
269 const struct lu_attr *attr,
270 const struct lu_fid *lf,
273 struct mdd_object *parent = NULL;
278 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
279 pfid = &mdd_env_info(env)->mti_fid;
281 /* Check for root first. */
282 if (mdd_is_root(mdd, mdo2fid(p1)))
286 /* this is done recursively */
287 rc = mdd_parent_fid(env, p1, attr, pfid);
290 if (mdd_is_root(mdd, pfid))
292 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
294 if (lu_fid_eq(pfid, lf))
297 mdd_object_put(env, parent);
299 parent = mdd_object_find(env, mdd, pfid);
301 GOTO(out, rc = PTR_ERR(parent));
303 if (!mdd_object_exists(parent))
304 GOTO(out, rc = -EINVAL);
310 if (parent && !IS_ERR(parent))
311 mdd_object_put(env, parent);
316 * No permission check is needed.
318 * returns 1: if fid is ancestor of @mo;
319 * returns 0: if fid is not an ancestor of @mo;
321 * returns EREMOTE if remote object is found, fid of remote object is saved to
324 * returns < 0: if error
326 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
327 const struct lu_fid *fid, struct lu_fid *sfid)
329 struct mdd_device *mdd = mdo2mdd(mo);
330 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
334 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
337 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
341 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
345 } else if (rc == 1) {
346 /* found @fid is parent */
354 * Check that @dir contains no entries except (possibly) dot and dotdot.
359 * -ENOTDIR not a directory object
360 * -ENOTEMPTY not empty
364 static int mdd_dir_is_empty(const struct lu_env *env,
365 struct mdd_object *dir)
368 struct dt_object *obj;
369 const struct dt_it_ops *iops;
373 obj = mdd_object_child(dir);
374 if (!dt_try_as_dir(env, obj))
377 iops = &obj->do_index_ops->dio_it;
378 it = iops->init(env, obj, LUDA_64BITHASH);
380 result = iops->get(env, it, (const struct dt_key *)"");
383 for (result = 0, i = 0; result == 0 && i < 3; ++i)
384 result = iops->next(env, it);
387 else if (result == 1)
389 } else if (result == 0)
391 * Huh? Index contains no zero key?
398 result = PTR_ERR(it);
403 * Determine if the target object can be hard linked, and right now it only
404 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
405 * directory nlink count might exceed the maximum link count(see
406 * osd_object_ref_add), so it only check nlink for non-directories.
408 * \param[in] env thread environment
409 * \param[in] obj object being linked to
410 * \param[in] la attributes of \a obj
412 * \retval 0 if \a obj can be hard linked
413 * \retval negative error if \a obj is a directory or has too
416 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
417 const struct lu_attr *la)
419 struct mdd_device *m = mdd_obj2mdd_dev(obj);
424 /* Subdir count limitation can be broken through
425 * (see osd_object_ref_add), so only check non-directory here. */
426 if (!S_ISDIR(la->la_mode) &&
427 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
434 * Check whether it may create the cobj under the pobj.
436 * \param[in] env execution environment
437 * \param[in] pobj the parent directory
438 * \param[in] pattr the attribute of the parent directory
439 * \param[in] cobj the child to be created
440 * \param[in] check_perm if check WRITE|EXEC permission for parent
442 * \retval = 0 create the child under this dir is allowed
443 * \retval negative errno create the child under this dir is
446 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
447 const struct lu_attr *pattr, struct mdd_object *cobj,
453 if (cobj && mdd_object_exists(cobj))
456 if (mdd_is_dead_obj(pobj))
460 rc = mdd_permission_internal_locked(env, pobj, pattr,
461 MAY_WRITE | MAY_EXEC,
467 * Check whether can unlink from the pobj in the case of "cobj == NULL".
469 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
470 const struct lu_attr *pattr, const struct lu_attr *attr)
475 if (mdd_is_dead_obj(pobj))
478 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
481 rc = mdd_permission_internal_locked(env, pobj, pattr,
482 MAY_WRITE | MAY_EXEC,
487 if (pattr->la_flags & LUSTRE_APPEND_FL)
494 * pobj == NULL is remote ops case, under such case, pobj's
495 * VTX feature has been checked already, no need check again.
497 static inline int mdd_is_sticky(const struct lu_env *env,
498 struct mdd_object *pobj,
499 const struct lu_attr *pattr,
500 struct mdd_object *cobj,
501 const struct lu_attr *cattr)
503 struct lu_ucred *uc = lu_ucred_assert(env);
506 LASSERT(pattr != NULL);
507 if (!(pattr->la_mode & S_ISVTX) ||
508 (pattr->la_uid == uc->uc_fsuid))
512 LASSERT(cattr != NULL);
513 if (cattr->la_uid == uc->uc_fsuid)
516 return !md_capable(uc, CFS_CAP_FOWNER);
519 static int mdd_may_delete_entry(const struct lu_env *env,
520 struct mdd_object *pobj,
521 const struct lu_attr *pattr,
526 LASSERT(pobj != NULL);
527 if (!mdd_object_exists(pobj))
530 if (mdd_is_dead_obj(pobj))
535 rc = mdd_permission_internal_locked(env, pobj, pattr,
536 MAY_WRITE | MAY_EXEC,
542 if (pattr->la_flags & LUSTRE_APPEND_FL)
549 * Check whether it may delete the cobj from the pobj.
552 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
553 const struct lu_attr *tpattr, struct mdd_object *tobj,
554 const struct lu_attr *tattr, const struct lu_attr *cattr,
555 int check_perm, int check_empty)
561 LASSERT(tpattr != NULL);
562 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
570 if (!mdd_object_exists(tobj))
573 if (mdd_is_dead_obj(tobj))
576 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
579 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
582 /* additional check the rename case */
584 if (S_ISDIR(cattr->la_mode)) {
585 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
587 if (!S_ISDIR(tattr->la_mode))
590 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
592 } else if (S_ISDIR(tattr->la_mode))
596 if (S_ISDIR(tattr->la_mode) && check_empty)
597 rc = mdd_dir_is_empty(env, tobj);
603 * Check whether it can create the link file(linked to @src_obj) under
604 * the target directory(@tgt_obj), and src_obj has been locked by
607 * \param[in] env execution environment
608 * \param[in] tgt_obj the target directory
609 * \param[in] tattr attributes of target directory
610 * \param[in] lname the link name
611 * \param[in] src_obj source object for link
612 * \param[in] cattr attributes for source object
614 * \retval = 0 it is allowed to create the link file under tgt_obj
615 * \retval negative error not allowed to create the link file
617 static int mdd_link_sanity_check(const struct lu_env *env,
618 struct mdd_object *tgt_obj,
619 const struct lu_attr *tattr,
620 const struct lu_name *lname,
621 struct mdd_object *src_obj,
622 const struct lu_attr *cattr)
624 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
628 if (!mdd_object_exists(src_obj))
631 if (mdd_is_dead_obj(src_obj))
634 /* Local ops, no lookup before link, check filename length here. */
635 rc = mdd_name_check(m, lname);
639 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
642 if (S_ISDIR(mdd_object_type(src_obj)))
645 LASSERT(src_obj != tgt_obj);
646 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
650 rc = __mdd_may_link(env, src_obj, cattr);
655 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
656 const char *name, struct thandle *handle)
658 struct dt_object *next = mdd_object_child(pobj);
662 if (dt_try_as_dir(env, next))
663 rc = dt_delete(env, next, (struct dt_key *)name, handle);
670 static int __mdd_index_insert_only(const struct lu_env *env,
671 struct mdd_object *pobj,
672 const struct lu_fid *lf, __u32 type,
674 struct thandle *handle)
676 struct dt_object *next = mdd_object_child(pobj);
680 if (dt_try_as_dir(env, next)) {
681 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
682 struct lu_ucred *uc = lu_ucred_check(env);
686 rec->rec_type = type;
687 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
688 rc = dt_insert(env, next, (const struct dt_rec *)rec,
689 (const struct dt_key *)name, handle,
697 /* insert named index, add reference if isdir */
698 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
699 const struct lu_fid *lf, __u32 type,
700 const char *name, struct thandle *handle)
705 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
706 if (rc == 0 && S_ISDIR(type)) {
707 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
708 mdo_ref_add(env, pobj, handle);
709 mdd_write_unlock(env, pobj);
715 /* delete named index, drop reference if isdir */
716 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
717 const char *name, int is_dir,
718 struct thandle *handle)
723 rc = __mdd_index_delete_only(env, pobj, name, handle);
724 if (rc == 0 && is_dir) {
725 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
726 mdo_ref_del(env, pobj, handle);
727 mdd_write_unlock(env, pobj);
733 static int mdd_llog_record_calc_size(const struct lu_env *env,
734 const struct lu_name *tname,
735 const struct lu_name *sname)
737 const struct lu_ucred *uc = lu_ucred(env);
738 enum changelog_rec_flags crf = CLF_EXTRA_FLAGS;
739 enum changelog_rec_extra_flags crfe = CLFE_UIDGID | CLFE_NID;
744 if (uc != NULL && uc->uc_jobid[0] != '\0')
747 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
748 changelog_rec_offset(crf, crfe) +
749 (tname != NULL ? tname->ln_namelen : 0) +
750 (sname != NULL ? 1 + sname->ln_namelen : 0));
753 int mdd_declare_changelog_store(const struct lu_env *env,
754 struct mdd_device *mdd,
755 enum changelog_rec_type type,
756 const struct lu_name *tname,
757 const struct lu_name *sname,
758 struct thandle *handle)
760 struct obd_device *obd = mdd2obd_dev(mdd);
761 struct llog_ctxt *ctxt;
762 struct llog_changelog_rec *rec;
764 struct thandle *llog_th;
768 if (!mdd_changelog_enabled(env, mdd, type))
771 reclen = mdd_llog_record_calc_size(env, tname, sname);
772 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
773 if (buf->lb_buf == NULL)
777 rec->cr_hdr.lrh_len = reclen;
778 rec->cr_hdr.lrh_type = CHANGELOG_REC;
780 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
784 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
786 GOTO(out_put, rc = PTR_ERR(llog_th));
788 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
796 /** Add a changelog entry \a rec to the changelog llog
799 * \param handle - currently ignored since llogs start their own transaction;
800 * this will hopefully be fixed in llog rewrite
803 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
804 struct llog_changelog_rec *rec, struct thandle *th)
806 struct obd_device *obd = mdd2obd_dev(mdd);
807 struct llog_ctxt *ctxt;
808 struct thandle *llog_th;
811 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
812 changelog_rec_varsize(&rec->cr));
814 /* llog_lvfs_write_rec sets the llog tail len */
815 rec->cr_hdr.lrh_type = CHANGELOG_REC;
816 rec->cr.cr_time = cl_time();
818 spin_lock(&mdd->mdd_cl.mc_lock);
819 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
820 * but as long as the MDD transactions are ordered correctly for e.g.
821 * rename conflicts, I don't think this should matter. */
822 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
823 spin_unlock(&mdd->mdd_cl.mc_lock);
825 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
829 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
831 GOTO(out_put, rc = PTR_ERR(llog_th));
833 /* nested journal transaction */
834 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
836 /* time to recover some space ?? */
837 if (likely(!mdd->mdd_changelog_gc ||
838 mdd->mdd_cl.mc_gc_task != MDD_CHLG_GC_NONE ||
839 mdd->mdd_changelog_min_gc_interval >=
840 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time))
841 /* save a spin_lock trip */
843 spin_lock(&mdd->mdd_cl.mc_lock);
844 if (likely(mdd->mdd_changelog_gc &&
845 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE &&
846 ktime_get_real_seconds() - mdd->mdd_cl.mc_gc_time >
847 mdd->mdd_changelog_min_gc_interval)) {
848 if (unlikely(llog_cat_free_space(ctxt->loc_handle) <=
849 mdd->mdd_changelog_min_free_cat_entries ||
850 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD))) {
851 CWARN("%s:%s low on changelog_catalog free entries, "
852 "starting ChangeLog garbage collection thread\n",
854 OBD_FAIL_CHECK(OBD_FAIL_FORCE_GC_THREAD) ?
857 /* indicate further kthread run will occur outside
858 * right after current journal transaction filling has
861 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NEED;
863 /* next check in mdd_changelog_min_gc_interval anyway
865 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
867 spin_unlock(&mdd->mdd_cl.mc_lock);
875 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
876 const struct lu_fid *sfid,
877 const struct lu_fid *spfid,
878 const struct lu_name *sname)
880 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
883 LASSERT(sfid != NULL);
884 LASSERT(spfid != NULL);
885 LASSERT(sname != NULL);
887 extsize = sname->ln_namelen + 1;
889 rnm->cr_sfid = *sfid;
890 rnm->cr_spfid = *spfid;
892 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
893 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
894 rec->cr_namelen += extsize;
897 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
899 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
901 if (jobid == NULL || jobid[0] == '\0')
904 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
907 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
909 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
911 ef->cr_extra_flags = eflags;
914 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
915 __u64 uid, __u64 gid)
917 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
919 uidgid->cr_uid = uid;
920 uidgid->cr_gid = gid;
923 void mdd_changelog_rec_extra_nid(struct changelog_rec *rec,
926 struct changelog_ext_nid *clnid = changelog_rec_nid(rec);
931 void mdd_changelog_rec_extra_omode(struct changelog_rec *rec, int flags)
933 struct changelog_ext_openmode *omd = changelog_rec_openmode(rec);
935 omd->cr_openflags = (__u32)flags;
938 void mdd_changelog_rec_extra_xattr(struct changelog_rec *rec,
939 const char *xattr_name)
941 struct changelog_ext_xattr *xattr = changelog_rec_xattr(rec);
943 strlcpy(xattr->cr_xattr, xattr_name, sizeof(xattr->cr_xattr));
946 /** Store a namespace change changelog record
947 * If this fails, we must fail the whole transaction; we don't
948 * want the change to commit without the log entry.
949 * \param target - mdd_object of change
950 * \param tpfid - target parent dir/object fid
951 * \param sfid - source object fid
952 * \param spfid - source parent fid
953 * \param tname - target name string
954 * \param sname - source name string
955 * \param handle - transaction handle
957 int mdd_changelog_ns_store(const struct lu_env *env,
958 struct mdd_device *mdd,
959 enum changelog_rec_type type,
960 enum changelog_rec_flags crf,
961 struct mdd_object *target,
962 const struct lu_fid *tpfid,
963 const struct lu_fid *sfid,
964 const struct lu_fid *spfid,
965 const struct lu_name *tname,
966 const struct lu_name *sname,
967 struct thandle *handle)
969 const struct lu_ucred *uc = lu_ucred(env);
970 struct llog_changelog_rec *rec;
973 __u64 xflags = CLFE_INVALID;
977 if (!mdd_changelog_enabled(env, mdd, type))
980 LASSERT(tpfid != NULL);
981 LASSERT(tname != NULL);
982 LASSERT(handle != NULL);
984 reclen = mdd_llog_record_calc_size(env, tname, sname);
985 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
986 if (buf->lb_buf == NULL)
991 crf |= CLF_EXTRA_FLAGS;
994 if (uc->uc_jobid[0] != '\0')
996 xflags |= CLFE_UIDGID;
1005 rec->cr.cr_flags = crf;
1007 if (crf & CLF_EXTRA_FLAGS) {
1008 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1009 if (xflags & CLFE_UIDGID)
1010 mdd_changelog_rec_extra_uidgid(&rec->cr,
1011 uc->uc_uid, uc->uc_gid);
1012 if (xflags & CLFE_NID)
1013 mdd_changelog_rec_extra_nid(&rec->cr, uc->uc_nid);
1016 rec->cr.cr_type = (__u32)type;
1017 rec->cr.cr_pfid = *tpfid;
1018 rec->cr.cr_namelen = tname->ln_namelen;
1019 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1021 if (crf & CLF_RENAME)
1022 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1024 if (crf & CLF_JOBID)
1025 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1027 if (likely(target != NULL)) {
1028 rec->cr.cr_tfid = *mdo2fid(target);
1029 target->mod_cltime = ktime_get();
1031 fid_zero(&rec->cr.cr_tfid);
1034 rc = mdd_changelog_store(env, mdd, rec, handle);
1036 CERROR("%s: cannot store changelog record: type = %d, "
1037 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1038 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1039 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1046 static int __mdd_links_add(const struct lu_env *env,
1047 struct mdd_object *mdd_obj,
1048 struct linkea_data *ldata,
1049 const struct lu_name *lname,
1050 const struct lu_fid *pfid,
1051 int first, int check)
1055 if (ldata->ld_leh == NULL) {
1056 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1060 rc = linkea_data_new(ldata,
1061 &mdd_env_info(env)->mti_link_buf);
1068 rc = linkea_links_find(ldata, lname, pfid);
1069 if (rc && rc != -ENOENT)
1075 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1076 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1080 linkea_add_buf(ldata, lname, tfid);
1083 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1084 linkea_add_buf(ldata, lname, pfid);
1086 return linkea_add_buf(ldata, lname, pfid);
1089 static int __mdd_links_del(const struct lu_env *env,
1090 struct mdd_object *mdd_obj,
1091 struct linkea_data *ldata,
1092 const struct lu_name *lname,
1093 const struct lu_fid *pfid)
1097 if (ldata->ld_leh == NULL) {
1098 rc = mdd_links_read(env, mdd_obj, ldata);
1103 rc = linkea_links_find(ldata, lname, pfid);
1107 linkea_del_buf(ldata, lname);
1111 static int mdd_linkea_prepare(const struct lu_env *env,
1112 struct mdd_object *mdd_obj,
1113 const struct lu_fid *oldpfid,
1114 const struct lu_name *oldlname,
1115 const struct lu_fid *newpfid,
1116 const struct lu_name *newlname,
1117 int first, int check,
1118 struct linkea_data *ldata)
1123 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1126 LASSERT(oldpfid != NULL || newpfid != NULL);
1128 if (mdd_obj->mod_flags & DEAD_OBJ)
1129 /* Unnecessary to update linkEA for dead object. */
1132 if (oldpfid != NULL) {
1133 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1135 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1138 /* No changes done. */
1143 /* If renaming, add the new record */
1144 if (newpfid != NULL)
1145 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1151 int mdd_links_rename(const struct lu_env *env,
1152 struct mdd_object *mdd_obj,
1153 const struct lu_fid *oldpfid,
1154 const struct lu_name *oldlname,
1155 const struct lu_fid *newpfid,
1156 const struct lu_name *newlname,
1157 struct thandle *handle,
1158 struct linkea_data *ldata,
1159 int first, int check)
1164 if (ldata == NULL) {
1165 ldata = &mdd_env_info(env)->mti_link_data;
1166 memset(ldata, 0, sizeof(*ldata));
1167 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1168 newpfid, newlname, first, check, ldata);
1173 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1174 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1180 if (newlname == NULL)
1181 CERROR("link_ea add failed %d "DFID"\n",
1182 rc, PFID(mdd_object_fid(mdd_obj)));
1183 else if (oldpfid == NULL)
1184 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1185 newlname->ln_namelen, newlname->ln_name, rc,
1186 PFID(mdd_object_fid(mdd_obj)));
1187 else if (newpfid == NULL)
1188 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1189 oldlname->ln_namelen, oldlname->ln_name, rc,
1190 PFID(mdd_object_fid(mdd_obj)));
1192 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1193 "\n", oldlname->ln_namelen, oldlname->ln_name,
1194 newlname->ln_namelen, newlname->ln_name, rc,
1195 PFID(mdd_object_fid(mdd_obj)));
1198 if (is_vmalloc_addr(ldata->ld_buf))
1199 /* if we vmalloced a large buffer drop it */
1200 lu_buf_free(ldata->ld_buf);
1205 static inline int mdd_links_add(const struct lu_env *env,
1206 struct mdd_object *mdd_obj,
1207 const struct lu_fid *pfid,
1208 const struct lu_name *lname,
1209 struct thandle *handle,
1210 struct linkea_data *ldata, int first)
1212 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1213 pfid, lname, handle, ldata, first, 0);
1216 static inline int mdd_links_del(const struct lu_env *env,
1217 struct mdd_object *mdd_obj,
1218 const struct lu_fid *pfid,
1219 const struct lu_name *lname,
1220 struct thandle *handle)
1222 return mdd_links_rename(env, mdd_obj, pfid, lname,
1223 NULL, NULL, handle, NULL, 0, 0);
1226 /** Read the link EA into a temp buffer.
1227 * Uses the name_buf since it is generally large.
1228 * \retval IS_ERR err
1229 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1231 struct lu_buf *mdd_links_get(const struct lu_env *env,
1232 struct mdd_object *mdd_obj)
1234 struct linkea_data ldata = { NULL };
1237 rc = mdd_links_read(env, mdd_obj, &ldata);
1238 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1241 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1242 struct linkea_data *ldata, struct thandle *handle)
1244 const struct lu_buf *buf;
1247 if (ldata == NULL || ldata->ld_buf == NULL ||
1248 ldata->ld_leh == NULL)
1251 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1255 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1256 ldata->ld_leh->leh_len);
1257 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1258 if (unlikely(rc == -ENOSPC)) {
1259 rc = linkea_overflow_shrink(ldata);
1267 static int mdd_declare_links_add(const struct lu_env *env,
1268 struct mdd_object *mdd_obj,
1269 struct thandle *handle,
1270 struct linkea_data *ldata)
1276 if (ldata != NULL && ldata->ld_leh != NULL) {
1277 ea_len = ldata->ld_leh->leh_len;
1278 linkea = ldata->ld_buf->lb_buf;
1280 ea_len = MAX_LINKEA_SIZE;
1284 rc = mdo_declare_xattr_set(env, mdd_obj,
1285 mdd_buf_get_const(env, linkea, ea_len),
1286 XATTR_NAME_LINK, 0, handle);
1291 static inline int mdd_declare_links_del(const struct lu_env *env,
1292 struct mdd_object *c,
1293 struct thandle *handle)
1297 /* For directory, the linkEA will be removed together
1298 * with the object. */
1299 if (!S_ISDIR(mdd_object_type(c)))
1300 rc = mdd_declare_links_add(env, c, handle, NULL);
1305 static int mdd_declare_link(const struct lu_env *env,
1306 struct mdd_device *mdd,
1307 struct mdd_object *p,
1308 struct mdd_object *c,
1309 const struct lu_name *name,
1310 struct thandle *handle,
1312 struct linkea_data *data)
1314 struct lu_fid tfid = *mdo2fid(c);
1317 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1318 tfid.f_oid = cfs_fail_val;
1320 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1321 name->ln_name, handle);
1325 rc = mdo_declare_ref_add(env, c, handle);
1329 la->la_valid = LA_CTIME | LA_MTIME;
1330 rc = mdo_declare_attr_set(env, p, la, handle);
1334 la->la_valid = LA_CTIME;
1335 rc = mdo_declare_attr_set(env, c, la, handle);
1339 rc = mdd_declare_links_add(env, c, handle, data);
1343 rc = mdd_declare_changelog_store(env, mdd, CL_HARDLINK, name, NULL,
1349 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1350 struct md_object *src_obj, const struct lu_name *lname,
1353 const char *name = lname->ln_name;
1354 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1355 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1356 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1357 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1358 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1359 struct mdd_device *mdd = mdo2mdd(src_obj);
1360 struct thandle *handle;
1361 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1362 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1366 rc = mdd_la_get(env, mdd_sobj, cattr);
1370 rc = mdd_la_get(env, mdd_tobj, tattr);
1375 * If we are using project inheritance, we only allow hard link
1376 * creation in our tree when the project IDs are the same;
1377 * otherwise the tree quota mechanism could be circumvented.
1379 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1380 (tattr->la_projid != cattr->la_projid))
1383 handle = mdd_trans_create(env, mdd);
1385 GOTO(out_pending, rc = PTR_ERR(handle));
1387 memset(ldata, 0, sizeof(*ldata));
1389 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1390 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1392 /* Note: even this function will change ldata, but it comes from
1393 * thread_info, which is completely temporary and only seen in
1394 * this function, so we do not need reset ldata once it fails.*/
1395 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
1396 lname, 0, 0, ldata);
1400 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1405 rc = mdd_trans_start(env, mdd, handle);
1409 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1410 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1413 GOTO(out_unlock, rc);
1415 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1416 rc = mdo_ref_add(env, mdd_sobj, handle);
1418 GOTO(out_unlock, rc);
1421 *tfid = *mdo2fid(mdd_sobj);
1422 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1423 tfid->f_oid = cfs_fail_val;
1425 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1426 mdd_object_type(mdd_sobj), name, handle);
1428 mdo_ref_del(env, mdd_sobj, handle);
1429 GOTO(out_unlock, rc);
1432 la->la_valid = LA_CTIME | LA_MTIME;
1433 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1435 GOTO(out_unlock, rc);
1437 la->la_valid = LA_CTIME;
1438 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1440 /* Note: The failure of links_add should not cause the
1441 * link failure, so do not check return value. */
1442 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1443 lname, handle, ldata, 0);
1447 mdd_write_unlock(env, mdd_sobj);
1449 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1450 mdo2fid(mdd_tobj), NULL, NULL,
1451 lname, NULL, handle);
1453 rc = mdd_trans_stop(env, mdd, rc, handle);
1454 if (is_vmalloc_addr(ldata->ld_buf))
1455 /* if we vmalloced a large buffer drop it */
1456 lu_buf_free(ldata->ld_buf);
1461 static int mdd_mark_orphan_object(const struct lu_env *env,
1462 struct mdd_object *obj, struct thandle *handle,
1465 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1468 if (!S_ISDIR(mdd_object_type(obj)))
1471 attr->la_valid = LA_FLAGS;
1472 attr->la_flags = LUSTRE_ORPHAN_FL;
1475 rc = mdo_declare_attr_set(env, obj, attr, handle);
1477 rc = mdo_attr_set(env, obj, attr, handle);
1482 static int mdd_declare_finish_unlink(const struct lu_env *env,
1483 struct mdd_object *obj,
1484 struct thandle *handle)
1488 /* Sigh, we do not know if the unlink object will become orphan in
1489 * declare phase, but fortunately the flags here does not matter
1490 * in current declare implementation */
1491 rc = mdd_mark_orphan_object(env, obj, handle, true);
1495 rc = mdo_declare_destroy(env, obj, handle);
1499 rc = mdd_orphan_declare_insert(env, obj, mdd_object_type(obj), handle);
1503 return mdd_declare_links_del(env, obj, handle);
1506 /* caller should take a lock before calling */
1507 int mdd_finish_unlink(const struct lu_env *env,
1508 struct mdd_object *obj, struct md_attr *ma,
1509 const struct mdd_object *pobj,
1510 const struct lu_name *lname,
1514 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1517 LASSERT(mdd_write_locked(env, obj) != 0);
1519 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1520 /* add new orphan and the object
1521 * will be deleted during mdd_close() */
1522 obj->mod_flags |= DEAD_OBJ;
1523 if (obj->mod_count) {
1524 rc = mdd_orphan_insert(env, obj, th);
1526 CDEBUG(D_HA, "Object "DFID" is inserted into "
1527 "orphan list, open count = %d\n",
1528 PFID(mdd_object_fid(obj)),
1531 CERROR("Object "DFID" fail to be an orphan, "
1532 "open count = %d, maybe cause failed "
1534 PFID(mdd_object_fid(obj)),
1537 /* mark object as an orphan here, not
1538 * before mdd_orphan_insert() as racing
1539 * mdd_la_get() may propagate ORPHAN_OBJ
1540 * causing the asserition */
1541 rc = mdd_mark_orphan_object(env, obj, th, false);
1543 rc = mdo_destroy(env, obj, th);
1545 } else if (!is_dir) {
1546 /* old files may not have link ea; ignore errors */
1547 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1555 * has mdd_write_lock on cobj already, but not on pobj yet
1557 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1558 const struct lu_attr *pattr,
1559 struct mdd_object *cobj,
1560 const struct lu_attr *cattr)
1565 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1570 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1571 struct mdd_object *p, struct mdd_object *c,
1572 const struct lu_name *name, struct md_attr *ma,
1573 struct thandle *handle, int no_name, int is_dir)
1575 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1578 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1579 if (likely(no_name == 0)) {
1580 rc = mdo_declare_index_delete(env, p, name->ln_name,
1587 rc = mdo_declare_ref_del(env, p, handle);
1593 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1594 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1595 la->la_valid = LA_CTIME | LA_MTIME;
1596 rc = mdo_declare_attr_set(env, p, la, handle);
1601 rc = mdo_declare_ref_del(env, c, handle);
1605 rc = mdo_declare_ref_del(env, c, handle);
1609 la->la_valid = LA_CTIME;
1610 rc = mdo_declare_attr_set(env, c, la, handle);
1614 rc = mdd_declare_finish_unlink(env, c, handle);
1618 /* FIXME: need changelog for remove entry */
1619 rc = mdd_declare_changelog_store(env, mdd, CL_UNLINK, name,
1627 * test if a file has an HSM archive
1628 * if HSM attributes are not found in ma update them from
1631 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1632 struct mdd_object *obj,
1637 if (!(ma->ma_valid & MA_HSM)) {
1638 /* no HSM MD provided, read xattr */
1639 struct lu_buf *hsm_buf;
1640 const size_t buflen = sizeof(struct hsm_attrs);
1643 hsm_buf = mdd_buf_get(env, NULL, 0);
1644 lu_buf_alloc(hsm_buf, buflen);
1645 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1646 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1647 lu_buf_free(hsm_buf);
1651 ma->ma_valid |= MA_HSM;
1653 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1659 * Delete name entry and the object.
1660 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1661 * does not exist for this object, and it could only happen during resending
1662 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1663 * is also needed in this case(needed by changelog), so we have to add another
1664 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1665 * the ENOENT failure should be able to be fixed by redo mechanism.
1667 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1668 struct md_object *cobj, const struct lu_name *lname,
1669 struct md_attr *ma, int no_name)
1671 const char *name = lname->ln_name;
1672 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1673 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1674 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1675 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1676 struct mdd_object *mdd_cobj = NULL;
1677 struct mdd_device *mdd = mdo2mdd(pobj);
1678 struct thandle *handle;
1679 int rc, is_dir = 0, cl_flags = 0;
1682 /* cobj == NULL means only delete name entry */
1683 if (likely(cobj != NULL)) {
1684 mdd_cobj = md2mdd_obj(cobj);
1685 if (mdd_object_exists(mdd_cobj) == 0)
1689 rc = mdd_la_get(env, mdd_pobj, pattr);
1693 if (likely(mdd_cobj != NULL)) {
1695 rc = mdd_la_get(env, mdd_cobj, cattr);
1699 is_dir = S_ISDIR(cattr->la_mode);
1700 /* search for an existing archive.
1701 * we should check ahead as the object
1702 * can be destroyed in this transaction */
1703 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1704 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1707 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1711 handle = mdd_trans_create(env, mdd);
1713 RETURN(PTR_ERR(handle));
1715 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1716 lname, ma, handle, no_name, is_dir);
1720 rc = mdd_trans_start(env, mdd, handle);
1724 if (likely(mdd_cobj != NULL))
1725 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1727 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1728 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1733 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1734 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1735 GOTO(cleanup, rc = 0);
1737 if (likely(mdd_cobj != NULL)) {
1738 rc = mdo_ref_del(env, mdd_cobj, handle);
1740 __mdd_index_insert_only(env, mdd_pobj,
1742 mdd_object_type(mdd_cobj),
1749 mdo_ref_del(env, mdd_cobj, handle);
1751 /* fetch updated nlink */
1752 rc = mdd_la_get(env, mdd_cobj, cattr);
1757 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1758 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1760 la->la_valid = LA_CTIME | LA_MTIME;
1761 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1765 /* Enough for only unlink the entry */
1766 if (unlikely(mdd_cobj == NULL))
1769 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1770 /* update ctime of an unlinked file only if it is still
1771 * opened or a link still exists */
1772 la->la_valid = LA_CTIME;
1773 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1778 /* XXX: this transfer to ma will be removed with LOD/OSP */
1779 ma->ma_attr = *cattr;
1780 ma->ma_valid |= MA_INODE;
1781 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1785 /* fetch updated nlink */
1786 rc = mdd_la_get(env, mdd_cobj, cattr);
1787 /* if object is removed then we can't get its attrs,
1789 if (rc == -ENOENT) {
1790 cattr->la_nlink = 0;
1794 if (cattr->la_nlink == 0) {
1795 ma->ma_attr = *cattr;
1796 ma->ma_valid |= MA_INODE;
1801 if (likely(mdd_cobj != NULL))
1802 mdd_write_unlock(env, mdd_cobj);
1805 if (cattr->la_nlink == 0)
1806 cl_flags |= CLF_UNLINK_LAST;
1808 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1810 rc = mdd_changelog_ns_store(env, mdd,
1811 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1812 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1817 rc = mdd_trans_stop(env, mdd, rc, handle);
1823 * The permission has been checked when obj created, no need check again.
1825 static int mdd_cd_sanity_check(const struct lu_env *env,
1826 struct mdd_object *obj)
1831 if (!obj || mdd_is_dead_obj(obj))
1837 static int mdd_create_data(const struct lu_env *env,
1838 struct md_object *pobj,
1839 struct md_object *cobj,
1840 const struct md_op_spec *spec,
1843 struct mdd_device *mdd = mdo2mdd(cobj);
1844 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1845 struct mdd_object *son = md2mdd_obj(cobj);
1846 struct thandle *handle;
1847 const struct lu_buf *buf;
1848 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1849 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1853 rc = mdd_cd_sanity_check(env, son);
1857 if (!md_should_create(spec->sp_cr_flags))
1861 * there are following use cases for this function:
1862 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1863 * striping can be specified or not
1866 rc = mdd_la_get(env, son, attr);
1870 /* calling ->ah_make_hint() is used to transfer information from parent */
1871 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1873 handle = mdd_trans_create(env, mdd);
1875 GOTO(out_free, rc = PTR_ERR(handle));
1878 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1879 * Should this be fixed?
1881 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1882 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1883 spec->sp_cr_flags, spec->no_create);
1885 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1886 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1887 spec->u.sp_ea.eadatalen);
1892 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1893 XATTR_NAME_LOV, 0, handle);
1897 rc = mdd_declare_changelog_store(env, mdd, CL_LAYOUT, NULL, NULL,
1902 rc = mdd_trans_start(env, mdd, handle);
1906 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1912 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1915 rc = mdd_trans_stop(env, mdd, rc, handle);
1921 static int mdd_declare_object_initialize(const struct lu_env *env,
1922 struct mdd_object *parent,
1923 struct mdd_object *child,
1924 const struct lu_attr *attr,
1925 struct thandle *handle)
1930 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1931 if (!S_ISDIR(attr->la_mode))
1934 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1939 rc = mdo_declare_ref_add(env, child, handle);
1943 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1949 static int mdd_object_initialize(const struct lu_env *env,
1950 const struct lu_fid *pfid,
1951 struct mdd_object *child,
1952 struct lu_attr *attr, struct thandle *handle,
1953 const struct md_op_spec *spec)
1958 if (S_ISDIR(attr->la_mode)) {
1959 /* Add "." and ".." for newly created dir */
1960 mdo_ref_add(env, child, handle);
1961 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1962 S_IFDIR, dot, handle);
1964 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1967 mdo_ref_del(env, child, handle);
1974 * This function checks whether it can create a file/dir under the
1975 * directory(@pobj). The directory(@pobj) is not being locked by
1978 * \param[in] env execution environment
1979 * \param[in] pobj the directory to create files
1980 * \param[in] pattr the attributes of the directory
1981 * \param[in] lname the name of the created file/dir
1982 * \param[in] cattr the attributes of the file/dir
1983 * \param[in] spec create specification
1985 * \retval = 0 it is allowed to create file/dir under
1987 * \retval negative error not allowed to create file/dir
1988 * under the directory
1990 static int mdd_create_sanity_check(const struct lu_env *env,
1991 struct md_object *pobj,
1992 const struct lu_attr *pattr,
1993 const struct lu_name *lname,
1994 struct lu_attr *cattr,
1995 struct md_op_spec *spec)
1997 struct mdd_thread_info *info = mdd_env_info(env);
1998 struct lu_fid *fid = &info->mti_fid;
1999 struct mdd_object *obj = md2mdd_obj(pobj);
2000 struct mdd_device *m = mdo2mdd(pobj);
2001 bool check_perm = true;
2006 if (mdd_is_dead_obj(obj))
2010 * In some cases this lookup is not needed - we know before if name
2011 * exists or not because MDT performs lookup for it.
2012 * name length check is done in lookup.
2014 if (spec->sp_cr_lookup) {
2016 * Check if the name already exist, though it will be checked in
2017 * _index_insert also, for avoiding rolling back if exists
2020 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2021 MAY_WRITE | MAY_EXEC);
2023 RETURN(rc ? : -EEXIST);
2025 /* Permission is already being checked in mdd_lookup */
2029 if (S_ISDIR(cattr->la_mode) &&
2030 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2031 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2032 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2034 if (le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC &&
2035 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_SPECIFIC &&
2036 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2038 CERROR("%s: invalid lmv_user_md: magic = %x, "
2039 "stripe_offset = %d, stripe_count = %u: "
2040 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2041 le32_to_cpu(lum->lum_magic),
2042 (int)le32_to_cpu(lum->lum_stripe_offset),
2043 le32_to_cpu(lum->lum_stripe_count), rc);
2048 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2053 if (pattr->la_mode & S_ISGID) {
2054 cattr->la_gid = pattr->la_gid;
2055 if (S_ISDIR(cattr->la_mode)) {
2056 cattr->la_mode |= S_ISGID;
2057 cattr->la_valid |= LA_MODE;
2061 /* Inherit project ID from parent directory */
2062 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2063 cattr->la_projid = pattr->la_projid;
2064 if (S_ISDIR(cattr->la_mode)) {
2065 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2066 cattr->la_valid |= LA_FLAGS;
2068 cattr->la_valid |= LA_PROJID;
2071 rc = mdd_name_check(m, lname);
2075 switch (cattr->la_mode & S_IFMT) {
2077 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2079 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2080 RETURN(-ENAMETOOLONG);
2099 static int mdd_declare_create_object(const struct lu_env *env,
2100 struct mdd_device *mdd,
2101 struct mdd_object *p, struct mdd_object *c,
2102 struct lu_attr *attr,
2103 struct thandle *handle,
2104 const struct md_op_spec *spec,
2105 struct lu_buf *def_acl_buf,
2106 struct lu_buf *acl_buf,
2107 struct dt_allocation_hint *hint)
2109 const struct lu_buf *buf;
2112 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2117 #ifdef CONFIG_FS_POSIX_ACL
2118 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2119 /* if dir, then can inherit default ACl */
2120 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2121 XATTR_NAME_ACL_DEFAULT,
2127 if (acl_buf->lb_len > 0) {
2128 rc = mdo_declare_attr_set(env, c, attr, handle);
2132 rc = mdo_declare_xattr_set(env, c, acl_buf,
2133 XATTR_NAME_ACL_ACCESS, 0, handle);
2138 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2142 /* replay case, create LOV EA from client data */
2143 if ((!(spec->sp_cr_flags & MDS_OPEN_DELAY_CREATE) && spec->no_create) ||
2144 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2145 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2146 spec->u.sp_ea.eadatalen);
2147 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2153 if (S_ISLNK(attr->la_mode)) {
2154 const char *target_name = spec->u.sp_symname;
2155 int sym_len = strlen(target_name);
2156 const struct lu_buf *buf;
2158 buf = mdd_buf_get_const(env, target_name, sym_len);
2159 rc = dt_declare_record_write(env, mdd_object_child(c),
2165 if (spec->sp_cr_file_secctx_name != NULL) {
2166 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2167 spec->sp_cr_file_secctx_size);
2168 rc = mdo_declare_xattr_set(env, c, buf,
2169 spec->sp_cr_file_secctx_name, 0,
2178 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2179 struct mdd_object *p, struct mdd_object *c,
2180 const struct lu_name *name,
2181 struct lu_attr *attr,
2182 struct thandle *handle,
2183 const struct md_op_spec *spec,
2184 struct linkea_data *ldata,
2185 struct lu_buf *def_acl_buf,
2186 struct lu_buf *acl_buf,
2187 struct dt_allocation_hint *hint)
2191 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2192 def_acl_buf, acl_buf, hint);
2196 if (S_ISDIR(attr->la_mode)) {
2197 rc = mdo_declare_ref_add(env, p, handle);
2202 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2203 rc = mdd_orphan_declare_insert(env, c, attr->la_mode, handle);
2207 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2208 enum changelog_rec_type type;
2210 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2211 name->ln_name, handle);
2215 rc = mdd_declare_links_add(env, c, handle, ldata);
2220 la->la_valid = LA_CTIME | LA_MTIME;
2221 rc = mdo_declare_attr_set(env, p, la, handle);
2225 type = S_ISDIR(attr->la_mode) ? CL_MKDIR :
2226 S_ISREG(attr->la_mode) ? CL_CREATE :
2227 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD;
2229 rc = mdd_declare_changelog_store(env, mdd, type, name, NULL,
2238 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2239 struct lu_attr *la, struct lu_buf *def_acl_buf,
2240 struct lu_buf *acl_buf)
2245 if (S_ISLNK(la->la_mode)) {
2246 acl_buf->lb_len = 0;
2247 def_acl_buf->lb_len = 0;
2251 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2252 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2253 XATTR_NAME_ACL_DEFAULT);
2254 mdd_read_unlock(env, pobj);
2256 /* If there are default ACL, fix mode/ACL by default ACL */
2257 def_acl_buf->lb_len = rc;
2258 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2259 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2260 acl_buf->lb_len = rc;
2261 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2264 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2265 /* If there are no default ACL, fix mode by mask */
2266 struct lu_ucred *uc = lu_ucred(env);
2268 /* The create triggered by MDT internal events, such as
2269 * LFSCK reset, will not contain valid "uc". */
2270 if (unlikely(uc != NULL))
2271 la->la_mode &= ~uc->uc_umask;
2273 acl_buf->lb_len = 0;
2274 def_acl_buf->lb_len = 0;
2281 * Create a metadata object and initialize it, set acl, xattr.
2283 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2284 struct mdd_object *son, struct lu_attr *attr,
2285 struct md_op_spec *spec, struct lu_buf *acl_buf,
2286 struct lu_buf *def_acl_buf,
2287 struct dt_allocation_hint *hint,
2288 struct thandle *handle)
2290 const struct lu_buf *buf;
2293 mdd_write_lock(env, son, MOR_TGT_CHILD);
2294 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2299 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2300 * created in declare phase, they also needs to be added to master
2301 * object as sub-directory entry. So it has to initialize the master
2302 * object, then set dir striped EA.(in mdo_xattr_set) */
2303 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2306 GOTO(err_destroy, rc);
2309 * in case of replay we just set LOVEA provided by the client
2310 * XXX: I think it would be interesting to try "old" way where
2311 * MDT calls this xattr_set(LOV) in a different transaction.
2312 * probably this way we code can be made better.
2315 /* During creation, there are only a few cases we need do xattr_set to
2317 * 1. regular file: see comments above.
2318 * 2. dir: inherit default striping or pool settings from parent.
2319 * 3. create striped directory with provided stripeEA.
2320 * 4. create striped directory because inherit default layout from the
2323 if (spec->no_create ||
2324 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2325 S_ISDIR(attr->la_mode)) {
2326 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2327 spec->u.sp_ea.eadatalen);
2328 rc = mdo_xattr_set(env, son, buf,
2329 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2333 GOTO(err_destroy, rc);
2336 #ifdef CONFIG_FS_POSIX_ACL
2337 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2338 S_ISDIR(attr->la_mode)) {
2339 /* set default acl */
2340 rc = mdo_xattr_set(env, son, def_acl_buf,
2341 XATTR_NAME_ACL_DEFAULT, 0,
2344 GOTO(err_destroy, rc);
2346 /* set its own acl */
2347 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2348 rc = mdo_xattr_set(env, son, acl_buf,
2349 XATTR_NAME_ACL_ACCESS,
2352 GOTO(err_destroy, rc);
2356 if (S_ISLNK(attr->la_mode)) {
2357 struct lu_ucred *uc = lu_ucred_assert(env);
2358 struct dt_object *dt = mdd_object_child(son);
2359 const char *target_name = spec->u.sp_symname;
2360 int sym_len = strlen(target_name);
2363 buf = mdd_buf_get_const(env, target_name, sym_len);
2364 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2366 CFS_CAP_SYS_RESOURCE_MASK);
2371 GOTO(err_initlized, rc = -EFAULT);
2374 if (spec->sp_cr_file_secctx_name != NULL) {
2375 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2376 spec->sp_cr_file_secctx_size);
2377 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2380 GOTO(err_initlized, rc);
2384 if (unlikely(rc != 0)) {
2386 if (S_ISDIR(attr->la_mode)) {
2387 /* Drop the reference, no need to delete "."/"..",
2388 * because the object to be destroied directly. */
2389 rc2 = mdo_ref_del(env, son, handle);
2393 rc2 = mdo_ref_del(env, son, handle);
2397 mdo_destroy(env, son, handle);
2400 mdd_write_unlock(env, son);
2404 static int mdd_index_delete(const struct lu_env *env,
2405 struct mdd_object *mdd_pobj,
2406 struct lu_attr *cattr,
2407 const struct lu_name *lname)
2409 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2410 struct thandle *handle;
2414 handle = mdd_trans_create(env, mdd);
2416 RETURN(PTR_ERR(handle));
2418 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2423 if (S_ISDIR(cattr->la_mode)) {
2424 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2429 /* Since this will only be used in the error handler path,
2430 * Let's set the thandle to be local and not mess the transno */
2431 handle->th_local = 1;
2432 rc = mdd_trans_start(env, mdd, handle);
2436 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2437 S_ISDIR(cattr->la_mode), handle);
2441 rc = mdd_trans_stop(env, mdd, rc, handle);
2447 * Create object and insert it into namespace.
2449 * Two operations have to be performed:
2451 * - an allocation of a new object (->do_create()), and
2452 * - an insertion into a parent index (->dio_insert()).
2454 * Due to locking, operation order is not important, when both are
2455 * successful, *but* error handling cases are quite different:
2457 * - if insertion is done first, and following object creation fails,
2458 * insertion has to be rolled back, but this operation might fail
2459 * also leaving us with dangling index entry.
2461 * - if creation is done first, is has to be undone if insertion fails,
2462 * leaving us with leaked space, which is not good but not fatal.
2464 * It seems that creation-first is simplest solution, but it is sub-optimal
2470 * case, because second mkdir is bound to create object, only to
2471 * destroy it immediately.
2473 * To avoid this follow local file systems that do double lookup:
2475 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2476 * 1. create (mdd_create_object_internal())
2477 * 2. insert (__mdd_index_insert(), lookup again)
2479 * \param[in] pobj parent object
2480 * \param[in] lname name of child being created
2481 * \param[in,out] child child object being created
2482 * \param[in] spec additional create parameters
2483 * \param[in] ma attributes for new child object
2485 * \retval 0 on success
2486 * \retval negative errno on failure
2488 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2489 const struct lu_name *lname, struct md_object *child,
2490 struct md_op_spec *spec, struct md_attr *ma)
2492 struct mdd_thread_info *info = mdd_env_info(env);
2493 struct lu_attr *la = &info->mti_la_for_fix;
2494 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2495 struct mdd_object *son = md2mdd_obj(child);
2496 struct mdd_device *mdd = mdo2mdd(pobj);
2497 struct lu_attr *attr = &ma->ma_attr;
2498 struct thandle *handle;
2499 struct lu_attr *pattr = &info->mti_pattr;
2500 struct lu_buf acl_buf;
2501 struct lu_buf def_acl_buf;
2502 struct linkea_data *ldata = &info->mti_link_data;
2503 const char *name = lname->ln_name;
2504 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2509 rc = mdd_la_get(env, mdd_pobj, pattr);
2513 /* Sanity checks before big job. */
2514 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2518 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2519 GOTO(out_free, rc = -EINPROGRESS);
2521 handle = mdd_trans_create(env, mdd);
2523 GOTO(out_free, rc = PTR_ERR(handle));
2525 lu_buf_check_and_alloc(&info->mti_xattr_buf,
2526 mdd->mdd_dt_conf.ddp_max_ea_size);
2527 acl_buf = info->mti_xattr_buf;
2528 def_acl_buf.lb_buf = info->mti_key;
2529 def_acl_buf.lb_len = sizeof(info->mti_key);
2530 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2534 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2536 memset(ldata, 0, sizeof(*ldata));
2537 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2538 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2541 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2542 &tfid, lname, 1, 0, ldata);
2544 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2545 mdd_object_fid(mdd_pobj),
2546 lname, 1, 0, ldata);
2549 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2550 handle, spec, ldata, &def_acl_buf, &acl_buf,
2555 rc = mdd_trans_start(env, mdd, handle);
2559 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2560 &def_acl_buf, hint, handle);
2564 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2565 mdd_write_lock(env, son, MOR_TGT_CHILD);
2566 son->mod_flags |= VOLATILE_OBJ;
2567 rc = mdd_orphan_insert(env, son, handle);
2568 GOTO(out_volatile, rc);
2570 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2571 attr->la_mode, name, handle);
2573 GOTO(err_created, rc);
2575 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2578 /* update parent directory mtime/ctime */
2580 la->la_valid = LA_CTIME | LA_MTIME;
2581 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2583 GOTO(err_insert, rc);
2589 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2590 rc2 = mdd_orphan_delete(env, son, handle);
2592 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2593 S_ISDIR(attr->la_mode),
2599 mdd_write_lock(env, son, MOR_TGT_CHILD);
2600 if (S_ISDIR(attr->la_mode)) {
2601 /* Drop the reference, no need to delete "."/"..",
2602 * because the object is to be destroyed directly. */
2603 rc2 = mdo_ref_del(env, son, handle);
2605 mdd_write_unlock(env, son);
2610 /* For volatile files drop one link immediately, since there is
2611 * no filename in the namespace, and save any error returned. */
2612 rc2 = mdo_ref_del(env, son, handle);
2614 mdd_write_unlock(env, son);
2615 if (unlikely(rc == 0))
2620 /* Don't destroy the volatile object on success */
2621 if (likely(rc != 0))
2622 mdo_destroy(env, son, handle);
2623 mdd_write_unlock(env, son);
2626 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2627 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2628 rc = mdd_changelog_ns_store(env, mdd,
2629 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2630 S_ISREG(attr->la_mode) ? CL_CREATE :
2631 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2632 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2635 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2637 /* If creation fails, it is most likely due to the remote update
2638 * failure, because local transaction will mostly succeed at
2639 * this stage. There is no easy way to rollback all of previous
2640 * updates, so let's remove the object from namespace, and
2641 * LFSCK should handle the orphan object. */
2642 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2643 mdd_index_delete(env, mdd_pobj, attr, lname);
2647 if (is_vmalloc_addr(ldata->ld_buf))
2648 /* if we vmalloced a large buffer drop it */
2649 lu_buf_free(ldata->ld_buf);
2651 /* The child object shouldn't be cached anymore */
2653 set_bit(LU_OBJECT_HEARD_BANSHEE,
2654 &child->mo_lu.lo_header->loh_flags);
2659 * Get locks on parents in proper order
2660 * RETURN: < 0 - error, rename_order if successful
2668 static int mdd_rename_order(const struct lu_env *env,
2669 struct mdd_device *mdd,
2670 struct mdd_object *src_pobj,
2671 const struct lu_attr *pattr,
2672 struct mdd_object *tgt_pobj)
2674 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2678 if (src_pobj == tgt_pobj)
2679 RETURN(MDD_RN_SAME);
2681 /* compared the parent child relationship of src_p&tgt_p */
2682 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2684 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2687 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2701 /* has not mdd_write{read}_lock on any obj yet. */
2702 static int mdd_rename_sanity_check(const struct lu_env *env,
2703 struct mdd_object *src_pobj,
2704 const struct lu_attr *pattr,
2705 struct mdd_object *tgt_pobj,
2706 const struct lu_attr *tpattr,
2707 struct mdd_object *sobj,
2708 const struct lu_attr *cattr,
2709 struct mdd_object *tobj,
2710 const struct lu_attr *tattr)
2715 /* XXX: when get here, sobj must NOT be NULL,
2716 * the other case has been processed in cld_rename
2717 * before mdd_rename and enable MDS_PERM_BYPASS. */
2721 * If we are using project inheritance, we only allow renames
2722 * into our tree when the project IDs are the same; otherwise
2723 * tree quota mechanism would be circumvented.
2725 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2726 tpattr->la_projid != cattr->la_projid) ||
2727 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2728 (pattr->la_projid != tpattr->la_projid)))
2731 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2735 /* XXX: when get here, "tobj == NULL" means tobj must
2736 * NOT exist (neither on remote MDS, such case has been
2737 * processed in cld_rename before mdd_rename and enable
2739 * So check may_create, but not check may_unlink. */
2741 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2742 (src_pobj != tgt_pobj));
2744 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2745 (src_pobj != tgt_pobj), 1);
2747 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2748 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2753 static int mdd_declare_rename(const struct lu_env *env,
2754 struct mdd_device *mdd,
2755 struct mdd_object *mdd_spobj,
2756 struct mdd_object *mdd_tpobj,
2757 struct mdd_object *mdd_sobj,
2758 struct mdd_object *mdd_tobj,
2759 const struct lu_name *sname,
2760 const struct lu_name *tname,
2762 struct linkea_data *ldata,
2763 struct thandle *handle)
2765 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2768 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2769 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2775 /* name from source dir */
2776 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2780 /* .. from source child */
2781 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2782 /* source child can be directory,
2783 * counted by source dir's nlink */
2784 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2787 if (mdd_spobj != mdd_tpobj) {
2788 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2793 rc = mdo_declare_index_insert(env, mdd_sobj,
2795 S_IFDIR, dotdot, handle);
2800 /* new target child can be directory,
2801 * counted by target dir's nlink */
2802 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2807 la->la_valid = LA_CTIME | LA_MTIME;
2808 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2812 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2816 la->la_valid = LA_CTIME;
2817 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2821 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2826 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2827 mdd_object_type(mdd_sobj),
2828 tname->ln_name, handle);
2832 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2833 /* delete target child in target parent directory */
2834 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2839 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2843 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2844 /* target child can be directory,
2845 * delete "." reference in target child directory */
2846 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2850 /* delete ".." reference in target parent directory */
2851 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2856 la->la_valid = LA_CTIME;
2857 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2861 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2866 rc = mdd_declare_changelog_store(env, mdd, CL_RENAME, tname, sname,
2874 /* src object can be remote that is why we use only fid and type of object */
2875 static int mdd_rename(const struct lu_env *env,
2876 struct md_object *src_pobj, struct md_object *tgt_pobj,
2877 const struct lu_fid *lf, const struct lu_name *lsname,
2878 struct md_object *tobj, const struct lu_name *ltname,
2881 const char *sname = lsname->ln_name;
2882 const char *tname = ltname->ln_name;
2883 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2884 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2885 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2886 struct mdd_device *mdd = mdo2mdd(src_pobj);
2887 struct mdd_object *mdd_sobj = NULL; /* source object */
2888 struct mdd_object *mdd_tobj = NULL;
2889 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2890 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2891 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2892 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2893 struct thandle *handle;
2894 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2895 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2896 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2899 bool tobj_locked = 0;
2900 unsigned cl_flags = 0;
2905 mdd_tobj = md2mdd_obj(tobj);
2907 mdd_sobj = mdd_object_find(env, mdd, lf);
2908 if (IS_ERR(mdd_sobj))
2909 RETURN(PTR_ERR(mdd_sobj));
2911 rc = mdd_la_get(env, mdd_sobj, cattr);
2913 GOTO(out_pending, rc);
2915 rc = mdd_la_get(env, mdd_spobj, pattr);
2917 GOTO(out_pending, rc);
2920 rc = mdd_la_get(env, mdd_tobj, tattr);
2922 GOTO(out_pending, rc);
2923 /* search for an existing archive.
2924 * we should check ahead as the object
2925 * can be destroyed in this transaction */
2926 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2927 cl_flags |= CLF_RENAME_LAST_EXISTS;
2930 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2932 GOTO(out_pending, rc);
2934 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2935 mdd_sobj, cattr, mdd_tobj, tattr);
2937 GOTO(out_pending, rc);
2939 rc = mdd_name_check(mdd, ltname);
2941 GOTO(out_pending, rc);
2943 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2944 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2946 GOTO(out_pending, rc);
2948 handle = mdd_trans_create(env, mdd);
2950 GOTO(out_pending, rc = PTR_ERR(handle));
2952 memset(ldata, 0, sizeof(*ldata));
2953 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
2954 lsname, mdd_object_fid(mdd_tpobj), ltname,
2959 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2960 mdd_tobj, lsname, ltname, ma, ldata, handle);
2964 rc = mdd_trans_start(env, mdd, handle);
2968 is_dir = S_ISDIR(cattr->la_mode);
2970 /* Remove source name from source directory */
2971 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2975 /* "mv dir1 dir2" needs "dir1/.." link update */
2976 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2977 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2979 GOTO(fixup_spobj2, rc);
2981 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2984 GOTO(fixup_spobj, rc);
2987 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2988 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2990 /* tname might been renamed to something else */
2991 GOTO(fixup_spobj, rc);
2994 /* Insert new fid with target name into target dir */
2995 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2998 GOTO(fixup_tpobj, rc);
3000 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3001 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3003 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3004 la->la_valid = LA_CTIME;
3005 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3007 GOTO(fixup_tpobj, rc);
3009 /* Update the linkEA for the source object */
3010 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3011 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
3012 mdo2fid(mdd_tpobj), ltname, handle, ldata,
3015 /* Old files might not have EA entry */
3016 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
3017 lsname, handle, NULL, 0);
3018 mdd_write_unlock(env, mdd_sobj);
3019 /* We don't fail the transaction if the link ea can't be
3020 updated -- fid2path will use alternate lookup method. */
3023 /* Remove old target object
3024 * For tobj is remote case cmm layer has processed
3025 * and set tobj to NULL then. So when tobj is NOT NULL,
3026 * it must be local one.
3028 if (tobj && mdd_object_exists(mdd_tobj)) {
3029 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
3031 if (mdd_is_dead_obj(mdd_tobj)) {
3032 /* shld not be dead, something is wrong */
3033 CERROR("tobj is dead, something is wrong\n");
3037 mdo_ref_del(env, mdd_tobj, handle);
3039 /* Remove dot reference. */
3040 if (S_ISDIR(tattr->la_mode))
3041 mdo_ref_del(env, mdd_tobj, handle);
3044 /* fetch updated nlink */
3045 rc = mdd_la_get(env, mdd_tobj, tattr);
3047 CERROR("%s: Failed to get nlink for tobj "
3049 mdd2obd_dev(mdd)->obd_name,
3050 PFID(tpobj_fid), rc);
3051 GOTO(fixup_tpobj, rc);
3054 la->la_valid = LA_CTIME;
3055 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3057 CERROR("%s: Failed to set ctime for tobj "
3059 mdd2obd_dev(mdd)->obd_name,
3060 PFID(tpobj_fid), rc);
3061 GOTO(fixup_tpobj, rc);
3064 /* XXX: this transfer to ma will be removed with LOD/OSP */
3065 ma->ma_attr = *tattr;
3066 ma->ma_valid |= MA_INODE;
3067 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3070 CERROR("%s: Failed to unlink tobj "
3072 mdd2obd_dev(mdd)->obd_name,
3073 PFID(tpobj_fid), rc);
3074 GOTO(fixup_tpobj, rc);
3077 /* fetch updated nlink */
3078 rc = mdd_la_get(env, mdd_tobj, tattr);
3079 if (rc == -ENOENT) {
3080 /* the object got removed, let's
3081 * return the latest known attributes */
3082 tattr->la_nlink = 0;
3084 } else if (rc != 0) {
3085 CERROR("%s: Failed to get nlink for tobj "
3087 mdd2obd_dev(mdd)->obd_name,
3088 PFID(tpobj_fid), rc);
3089 GOTO(fixup_tpobj, rc);
3091 /* XXX: this transfer to ma will be removed with LOD/OSP */
3092 ma->ma_attr = *tattr;
3093 ma->ma_valid |= MA_INODE;
3095 if (tattr->la_nlink == 0)
3096 cl_flags |= CLF_RENAME_LAST;
3098 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3101 la->la_valid = LA_CTIME | LA_MTIME;
3102 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3104 GOTO(fixup_tpobj, rc);
3106 if (mdd_spobj != mdd_tpobj) {
3107 la->la_valid = LA_CTIME | LA_MTIME;
3108 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3110 GOTO(fixup_tpobj, rc);
3117 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3119 CWARN("tp obj fix error %d\n",rc2);
3121 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3122 !mdd_is_dead_obj(mdd_tobj)) {
3124 mdo_ref_add(env, mdd_tobj, handle);
3126 mdo_ref_add(env, mdd_tobj, handle);
3129 rc2 = __mdd_index_insert(env, mdd_tpobj,
3131 mdd_object_type(mdd_tobj),
3134 CWARN("tp obj fix error: rc = %d\n", rc2);
3139 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3140 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3142 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3143 mdd2obd_dev(mdd)->obd_name, rc2);
3146 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3149 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3150 mdd2obd_dev(mdd)->obd_name, rc2);
3155 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3156 mdd_object_type(mdd_sobj), sname,
3159 CWARN("sp obj fix error: rc = %d\n", rc2);
3164 mdd_write_unlock(env, mdd_tobj);
3167 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3168 mdd_tobj, tpobj_fid, lf, spobj_fid,
3169 ltname, lsname, handle);
3172 rc = mdd_trans_stop(env, mdd, rc, handle);
3175 mdd_object_put(env, mdd_sobj);
3180 * During migration once the parent FID has been changed,
3181 * we need update the parent FID in linkea.
3183 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3184 struct mdd_object *parent,
3185 struct mdd_object *newparent,
3186 struct mdd_object *child,
3187 const char *name, int namelen,
3188 struct thandle *handle,
3191 struct mdd_thread_info *info = mdd_env_info(env);
3192 struct linkea_data ldata = { NULL };
3193 struct lu_buf *buf = &info->mti_link_buf;
3199 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3200 if (buf->lb_buf == NULL)
3204 rc = mdd_links_read(env, child, &ldata);
3206 if (rc == -ENOENT || rc == -ENODATA)
3211 LASSERT(ldata.ld_leh != NULL);
3212 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3213 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3214 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3215 struct lu_name lname;
3218 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3221 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3222 !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3223 ldata.ld_lee = (struct link_ea_entry *)
3224 ((char *)ldata.ld_lee +
3229 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3230 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3231 lname.ln_namelen, lname.ln_name,
3232 PFID(mdd_object_fid(newparent)));
3233 /* update to the new parent fid */
3234 linkea_entry_pack(ldata.ld_lee, &lname,
3235 mdd_object_fid(newparent));
3237 rc = mdd_declare_links_add(env, child, handle, &ldata);
3239 rc = mdd_links_write(env, child, &ldata, handle);
3245 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3246 struct mdd_object *parent,
3247 struct mdd_object *newparent,
3248 struct mdd_object *child,
3249 const char *name, int namelen,
3250 struct thandle *handle)
3252 return mdd_linkea_update_child_internal(env, parent, newparent,
3254 namelen, handle, true);
3257 static int mdd_linkea_update_child(const struct lu_env *env,
3258 struct mdd_object *parent,
3259 struct mdd_object *newparent,
3260 struct mdd_object *child,
3261 const char *name, int namelen,
3262 struct thandle *handle)
3264 return mdd_linkea_update_child_internal(env, parent, newparent,
3266 namelen, handle, false);
3269 static int mdd_update_linkea_internal(const struct lu_env *env,
3270 struct mdd_object *mdd_pobj,
3271 struct mdd_object *mdd_sobj,
3272 struct mdd_object *mdd_tobj,
3273 const struct lu_name *child_name,
3274 struct linkea_data *ldata,
3275 struct thandle *handle,
3278 struct mdd_thread_info *info = mdd_env_info(env);
3283 LASSERT(ldata->ld_buf != NULL);
3284 LASSERT(ldata->ld_leh != NULL);
3286 /* If it is mulitple links file, we need update the name entry for
3288 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3289 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3290 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3291 struct mdd_object *pobj;
3292 struct lu_name lname;
3295 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3297 pobj = mdd_object_find(env, mdd, &fid);
3299 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3300 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3305 if (!mdd_object_exists(pobj)) {
3306 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3307 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3311 if (pobj == mdd_pobj &&
3312 lname.ln_namelen == child_name->ln_namelen &&
3313 strncmp(lname.ln_name, child_name->ln_name,
3314 lname.ln_namelen) == 0) {
3315 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3316 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3321 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3322 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3323 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3326 /* Remove source name from source directory */
3327 /* Insert new fid with target name into target dir */
3328 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3333 rc = mdo_declare_index_insert(env, pobj,
3334 mdd_object_fid(mdd_tobj),
3335 mdd_object_type(mdd_tobj),
3336 lname.ln_name, handle);
3340 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3344 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3348 char *tmp_name = info->mti_key;
3350 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3351 /* lnamelen is too big(> NAME_MAX + 16),
3352 * something wrong about this linkea, let's
3354 CWARN("%s: the name %.*s is too long under "
3355 DFID"\n", mdd2obd_dev(mdd)->obd_name,
3356 lname.ln_namelen, lname.ln_name,
3361 /* Note: lname might be without \0 at the end, see
3362 * linkea_entry_unpack(), let's add extra \0 by
3364 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3365 lname.ln_namelen, lname.ln_name);
3366 lname.ln_name = tmp_name;
3368 /* Let's check if this linkEA still valid, before
3369 * it might be packed into the RPC buffer. */
3370 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3371 &info->mti_fid, NULL);
3372 if (rc < 0 || !lu_fid_eq(&info->mti_fid,
3373 mdd_object_fid(mdd_sobj)))
3374 GOTO(next_put, rc == -ENOENT ? 0 : rc);
3376 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3380 rc = __mdd_index_insert(env, pobj,
3381 mdd_object_fid(mdd_tobj),
3382 mdd_object_type(mdd_tobj),
3387 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3388 rc = mdo_ref_add(env, mdd_tobj, handle);
3389 mdd_write_unlock(env, mdd_tobj);
3393 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3394 mdo_ref_del(env, mdd_sobj, handle);
3395 mdd_write_unlock(env, mdd_sobj);
3398 mdd_object_put(env, pobj);
3402 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3409 static int mdd_migrate_xattrs(const struct lu_env *env,
3410 struct mdd_object *mdd_sobj,
3411 struct mdd_object *mdd_tobj)
3413 struct mdd_thread_info *info = mdd_env_info(env);
3414 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3416 struct thandle *handle;
3422 struct lu_buf list_xbuf;
3425 /* retrieve xattr list from the old object */
3426 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3427 if (list_xsize == -ENODATA)
3433 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3434 if (info->mti_big_buf.lb_buf == NULL)
3437 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3438 list_xbuf.lb_len = list_xsize;
3439 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3444 xname = list_xbuf.lb_buf;
3446 xlen = strnlen(xname, rem - 1) + 1;
3447 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3448 strcmp(XATTR_NAME_LMV, xname) == 0)
3451 /* For directory, if there are default layout, migrate here */
3452 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3453 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3456 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3457 if (xsize == -ENODATA)
3462 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3463 if (info->mti_link_buf.lb_buf == NULL)
3464 GOTO(out, rc = -ENOMEM);
3466 xbuf.lb_len = xsize;
3467 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3468 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3474 handle = mdd_trans_create(env, mdd);
3476 GOTO(out, rc = PTR_ERR(handle));
3478 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3481 GOTO(stop_trans, rc);
3482 /* Note: this transaction is part of migration, and it is not
3483 * the last step of migration, so we set th_local = 1 to avoid
3484 * update last rcvd for this transaction */
3485 handle->th_local = 1;
3486 rc = mdd_trans_start(env, mdd, handle);
3488 GOTO(stop_trans, rc);
3491 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3493 GOTO(stop_trans, rc = 0);
3495 if (unlikely(rc == -ENOSPC &&
3496 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3497 rc = linkea_overflow_shrink(
3498 (struct linkea_data *)(xbuf.lb_buf));
3499 if (likely(rc > 0)) {
3506 GOTO(stop_trans, rc);
3508 rc = mdd_trans_stop(env, mdd, rc, handle);
3513 memmove(xname, xname + xlen, rem);
3519 static int mdd_declare_migrate_create(const struct lu_env *env,
3520 struct mdd_object *mdd_pobj,
3521 struct mdd_object *mdd_sobj,
3522 struct mdd_object *mdd_tobj,
3523 struct md_op_spec *spec,
3525 union lmv_mds_md *mgr_ea,
3526 struct linkea_data *ldata,
3527 struct thandle *handle)
3529 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3530 const struct lu_buf *buf;
3534 rc = mdd_declare_create_object_internal(env, mdd_pobj, mdd_tobj, la,
3535 handle, spec, NULL);
3539 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3544 if (S_ISLNK(la->la_mode)) {
3545 const char *target_name = spec->u.sp_symname;
3546 int sym_len = strlen(target_name);
3547 const struct lu_buf *buf;
3549 buf = mdd_buf_get_const(env, target_name, sym_len);
3550 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3554 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3555 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3560 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3561 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3562 spec->u.sp_ea.eadatalen);
3563 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3569 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3570 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3571 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3576 la_flag->la_valid = LA_FLAGS;
3577 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3578 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3583 static int mdd_migrate_create(const struct lu_env *env,
3584 struct mdd_object *mdd_pobj,
3585 struct mdd_object *mdd_sobj,
3586 struct mdd_object *mdd_tobj,
3587 const struct lu_name *lname,
3590 struct mdd_thread_info *info = mdd_env_info(env);
3591 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3592 struct md_op_spec *spec = &info->mti_spec;
3593 struct lu_buf lmm_buf = { NULL };
3594 struct lu_buf link_buf = { NULL };
3595 struct lu_buf mgr_buf;
3596 struct thandle *handle;
3597 struct lmv_mds_md_v1 *mgr_ea;
3598 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3599 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3601 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3605 /* prepare spec for create */
3606 memset(spec, 0, sizeof(*spec));
3607 spec->sp_cr_lookup = 0;
3608 spec->sp_feat = &dt_directory_features;
3609 if (S_ISLNK(la->la_mode)) {
3610 const struct lu_buf *buf;
3612 buf = lu_buf_check_and_alloc(
3613 &mdd_env_info(env)->mti_big_buf,
3616 link_buf.lb_len = la->la_size + 1;
3617 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3618 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3620 rc = rc != 0 ? rc : -EFAULT;
3621 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3622 mdd2obd_dev(mdd)->obd_name,
3623 PFID(mdd_object_fid(mdd_sobj)), rc);
3626 spec->u.sp_symname = link_buf.lb_buf;
3627 } else if (S_ISREG(la->la_mode)) {
3628 /* retrieve lov of the old object */
3629 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3630 if (rc != 0 && rc != -ENODATA)
3632 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3633 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3634 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3635 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3637 } else if (S_ISDIR(la->la_mode)) {
3638 rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
3639 if (rc == -ENODATA) {
3640 /* ignore the non-linkEA error */
3648 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3649 lu_buf_check_and_alloc(&info->mti_xattr_buf, mgr_easize);
3650 mgr_buf.lb_buf = info->mti_xattr_buf.lb_buf;
3651 mgr_buf.lb_len = mgr_easize;
3652 mgr_ea = mgr_buf.lb_buf;
3653 memset(mgr_ea, 0, sizeof(*mgr_ea));
3654 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3655 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3656 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3657 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3658 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3659 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3661 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3663 handle = mdd_trans_create(env, mdd);
3665 GOTO(out_free, rc = PTR_ERR(handle));
3667 /* Note: this transaction is part of migration, and it is not
3668 * the last step of migration, so we set th_local = 1 to avoid
3669 * update last rcvd for this transaction */
3670 handle->th_local = 1;
3671 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj, spec,
3672 la, mgr_buf.lb_buf, ldata, handle);
3674 GOTO(stop_trans, rc);
3676 rc = mdd_trans_start(env, mdd, handle);
3678 GOTO(stop_trans, rc);
3680 /* don't set nlink from the original object */
3681 la->la_valid &= ~LA_NLINK;
3683 /* create the target object */
3684 rc = mdd_create_object(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3687 GOTO(stop_trans, rc);
3689 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3690 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3692 GOTO(stop_trans, rc);
3695 /* Set MIGRATE EA on the source inode, so once the migration needs
3696 * to be re-done during failover, the re-do process can locate the
3697 * target object which is already being created. */
3698 rc = mdo_xattr_set(env, mdd_sobj, &mgr_buf, XATTR_NAME_LMV, 0, handle);
3700 GOTO(stop_trans, rc);
3702 /* Set immutable flag, so any modification is disabled until
3703 * the migration is done. Once the migration is interrupted,
3704 * if the resume process find the migrating object has both
3705 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3706 * flag and approve the migration */
3707 la_flag->la_valid = LA_FLAGS;
3708 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3709 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3712 rc = mdd_trans_stop(env, mdd, rc, handle);
3714 if (lmm_buf.lb_buf != NULL)
3715 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3719 static int mdd_migrate_entries(const struct lu_env *env,
3720 struct mdd_object *mdd_sobj,
3721 struct mdd_object *mdd_tobj)
3723 struct dt_object *next = mdd_object_child(mdd_sobj);
3724 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3725 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3726 struct thandle *handle;
3728 const struct dt_it_ops *iops;
3730 struct lu_dirent *ent;
3734 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3738 if (!dt_try_as_dir(env, next))
3739 GOTO(out_ent, rc = -ENOTDIR);
3741 * iterate directories
3743 iops = &next->do_index_ops->dio_it;
3744 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3746 GOTO(out_ent, rc = PTR_ERR(it));
3748 rc = iops->load(env, it, 0);
3750 rc = iops->next(env, it);
3754 * At this point and across for-loop:
3756 * rc == 0 -> ok, proceed.
3757 * rc > 0 -> end of directory.
3761 struct mdd_object *child;
3762 char *name = mdd_env_info(env)->mti_key;
3765 bool target_exist = false;
3767 len = iops->key_size(env, it);
3771 result = iops->rec(env, it, (struct dt_rec *)ent,
3772 LUDA_FID | LUDA_TYPE);
3773 if (result == -ESTALE)
3780 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3782 /* Insert new fid with target name into target dir */
3783 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3784 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3785 ent->lde_name[1] == '.'))
3788 child = mdd_object_find(env, mdd, &ent->lde_fid);
3790 GOTO(out, rc = PTR_ERR(child));
3792 /* child may not exist, but lu_object_attr will assert this,
3793 * get type from loh_attr directly */
3794 is_dir = S_ISDIR(child->mod_obj.mo_lu.lo_header->loh_attr);
3796 mdd_write_lock(env, child, MOR_SRC_CHILD);
3798 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3800 /* Check whether the name has been inserted to the target */
3801 if (dt_try_as_dir(env, dt_tobj)) {
3802 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3804 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3805 (struct dt_key *)name);
3806 if (unlikely(rc == 0))
3807 target_exist = true;
3810 handle = mdd_trans_create(env, mdd);
3812 GOTO(out_put, rc = PTR_ERR(handle));
3814 /* Note: this transaction is part of migration, and it is not
3815 * the last step of migration, so we set th_local = 1 to avoid
3816 * updating last rcvd for this transaction */
3817 handle->th_local = 1;
3818 if (likely(!target_exist)) {
3819 rc = mdo_declare_index_insert(env, mdd_tobj,
3821 child->mod_obj.mo_lu.lo_header->loh_attr,
3827 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3833 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3838 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3842 /* Update .. for child */
3843 rc = mdo_declare_index_delete(env, child, dotdot,
3848 rc = mdo_declare_index_insert(env, child,
3849 mdd_object_fid(mdd_tobj),
3850 S_IFDIR, dotdot, handle);
3855 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3862 rc = mdd_trans_start(env, mdd, handle);
3864 CERROR("%s: transaction start failed: rc = %d\n",
3865 mdd2obd_dev(mdd)->obd_name, rc);
3869 if (likely(!target_exist)) {
3870 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3871 child->mod_obj.mo_lu.lo_header->loh_attr, name,
3877 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3882 rc = __mdd_index_delete_only(env, child, dotdot,
3887 rc = __mdd_index_insert_only(env, child,
3888 mdd_object_fid(mdd_tobj), S_IFDIR,
3894 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3896 strlen(name), handle);
3899 mdd_write_unlock(env, child);
3900 mdd_object_put(env, child);
3901 rc = mdd_trans_stop(env, mdd, rc, handle);
3905 result = iops->next(env, it);
3906 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3907 GOTO(out, rc = -EINTR);
3909 if (result == -ESTALE)
3911 } while (result == 0);
3914 iops->fini(env, it);
3916 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3920 static int mdd_declare_update_linkea(const struct lu_env *env,
3921 struct mdd_object *mdd_pobj,
3922 struct mdd_object *mdd_sobj,
3923 struct mdd_object *mdd_tobj,
3924 const struct lu_name *child_name,
3925 struct linkea_data *ldata,
3926 struct thandle *handle)
3928 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3929 child_name, ldata, handle, 1);
3932 static int mdd_update_linkea(const struct lu_env *env,
3933 struct mdd_object *mdd_pobj,
3934 struct mdd_object *mdd_sobj,
3935 struct mdd_object *mdd_tobj,
3936 const struct lu_name *child_name,
3937 struct linkea_data *ldata,
3938 struct thandle *handle)
3940 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3941 child_name, ldata, handle, 0);
3944 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3945 struct mdd_object *mdd_pobj,
3946 struct mdd_object *mdd_sobj,
3947 struct mdd_object *mdd_tobj,
3948 const struct lu_name *lname,
3950 struct lu_attr *parent_la,
3951 struct linkea_data *ldata,
3952 struct thandle *handle)
3954 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3955 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3958 /* Revert IMMUTABLE flag */
3959 la_flag->la_valid = LA_FLAGS;
3960 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3961 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3965 /* delete entry from source dir */
3966 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3970 if (ldata->ld_buf != NULL) {
3971 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3972 mdd_tobj, lname, ldata, handle);
3977 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3978 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3983 handle->th_complex = 1;
3984 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3986 LU_XATTR_REPLACE, handle);
3991 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3992 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3998 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3999 mdd_object_type(mdd_tobj),
4000 lname->ln_name, handle);
4004 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
4008 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4009 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
4014 /* delete old object */
4015 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4019 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4020 /* delete old object */
4021 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4024 /* set nlink to 0 */
4025 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
4030 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
4034 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
4038 rc = mdd_declare_changelog_store(env, mdd, CL_MIGRATE, lname, NULL,
4044 static int mdd_migrate_update_name(const struct lu_env *env,
4045 struct mdd_object *mdd_pobj,
4046 struct mdd_object *mdd_sobj,
4047 struct mdd_object *mdd_tobj,
4048 const struct lu_name *lname,
4051 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
4052 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4053 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
4054 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
4055 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
4056 struct thandle *handle;
4057 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
4058 const char *name = lname->ln_name;
4062 /* update time for parent */
4063 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
4064 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
4065 p_la->la_valid = LA_CTIME;
4067 rc = mdd_la_get(env, mdd_sobj, so_attr);
4071 ldata->ld_buf = NULL;
4072 rc = mdd_links_read(env, mdd_sobj, ldata);
4073 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
4076 handle = mdd_trans_create(env, mdd);
4078 RETURN(PTR_ERR(handle));
4080 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
4081 lname, so_attr, p_la, ldata,
4084 /* If the migration can not be fit in one transaction, just
4085 * leave it in the original MDT */
4087 GOTO(stop_trans, rc = 0);
4089 GOTO(stop_trans, rc);
4092 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
4093 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
4094 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4095 PFID(mdd_object_fid(mdd_tobj)));
4097 rc = mdd_trans_start(env, mdd, handle);
4099 GOTO(stop_trans, rc);
4101 /* Revert IMMUTABLE flag */
4102 la_flag->la_valid = LA_FLAGS;
4103 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
4104 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
4106 GOTO(stop_trans, rc);
4108 /* Remove source name from source directory */
4109 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
4111 GOTO(stop_trans, rc);
4113 if (ldata->ld_buf != NULL) {
4114 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
4115 lname, ldata, handle);
4117 GOTO(stop_trans, rc);
4119 /* linkea update might decrease the source object
4120 * nlink, let's get the attr again after ref_del */
4121 rc = mdd_la_get(env, mdd_sobj, so_attr);
4123 GOTO(stop_trans, rc);
4126 if (S_ISREG(so_attr->la_mode)) {
4127 if (so_attr->la_nlink == 1) {
4128 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4130 if (rc != 0 && rc != -ENODATA)
4131 GOTO(stop_trans, rc);
4133 rc = mdo_xattr_set(env, mdd_tobj, NULL,
4135 LU_XATTR_REPLACE, handle);
4137 GOTO(stop_trans, rc);
4141 /* Insert new fid with target name into target dir */
4142 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4143 mdd_object_type(mdd_tobj), name, handle);
4145 GOTO(stop_trans, rc);
4147 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4149 mdd_sobj->mod_flags |= DEAD_OBJ;
4150 rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4152 GOTO(out_unlock, rc);
4154 rc = mdd_orphan_insert(env, mdd_sobj, handle);
4156 GOTO(out_unlock, rc);
4158 mdo_ref_del(env, mdd_sobj, handle);
4160 mdo_ref_del(env, mdd_sobj, handle);
4162 /* Get the attr again after ref_del */
4163 rc = mdd_la_get(env, mdd_sobj, so_attr);
4165 GOTO(out_unlock, rc);
4167 ma->ma_attr = *so_attr;
4168 ma->ma_valid |= MA_INODE;
4170 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4172 GOTO(out_unlock, rc);
4174 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4175 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4176 mdo2fid(mdd_pobj), lname, lname, handle);
4178 CWARN("%s: changelog for migrate %s "DFID
4179 "under "DFID" failed: rc = %d\n",
4180 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4181 PFID(mdd_object_fid(mdd_sobj)),
4182 PFID(mdd_object_fid(mdd_pobj)), rc);
4183 /* Sigh, there are no easy way to migrate back the object, so
4184 * let's reset the result to 0 for now XXX */
4188 mdd_write_unlock(env, mdd_sobj);
4191 rc = mdd_trans_stop(env, mdd, rc, handle);
4196 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4197 const struct lu_fid *fid, __u32 *mdt_index)
4199 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4200 struct seq_server_site *ss;
4203 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4205 range->lsr_flags = LU_SEQ_RANGE_MDT;
4206 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4210 *mdt_index = range->lsr_index;
4215 * Check whether we should migrate the file/dir
4217 * < 0 permission check failed or other error.
4218 * = 0 the file can be migrated.
4219 * > 0 the file does not need to be migrated, mostly
4220 * for multiple link file
4222 static int mdd_migrate_sanity_check(const struct lu_env *env,
4223 struct mdd_object *pobj,
4224 const struct lu_attr *pattr,
4225 struct mdd_object *sobj,
4226 struct lu_attr *sattr)
4228 struct mdd_thread_info *info = mdd_env_info(env);
4229 struct linkea_data *ldata = &info->mti_link_data;
4230 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4232 struct lu_buf *mgr_buf;
4238 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4239 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4240 if (mgr_buf->lb_buf == NULL)
4243 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4245 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4247 /* If the object has migrateEA, it means IMMUTE flag
4248 * is being set by previous migration process, so it
4249 * needs to override the IMMUTE flag, otherwise the
4250 * following sanity check will fail */
4251 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4252 LMV_HASH_FLAG_MIGRATION) {
4253 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4255 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4256 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4257 mdd2obd_dev(mdd)->obd_name,
4258 PFID(mdd_object_fid(sobj)));
4262 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4263 sobj, sattr, NULL, NULL);
4267 /* Then it will check if the file should be migrated. If the file
4268 * has mulitple links, we only need migrate the file if all of its
4269 * entries has been migrated to the remote MDT */
4270 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4273 rc = mdd_links_read(env, sobj, ldata);
4275 /* For multiple links files, if there are no linkEA data at all,
4276 * means the file might be created before linkEA is enabled, and
4277 * all of its links should not be migrated yet, otherwise it
4278 * should have some linkEA there */
4279 if (rc == -ENOENT || rc == -ENODATA)
4284 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4285 /* If there are still links locally, then the file will not be
4287 LASSERT(ldata->ld_leh != NULL);
4289 /* If the linkEA is overflow, then means there are some unknown name
4290 * entries under unknown parents, that will prevent the migration. */
4291 if (unlikely(ldata->ld_leh->leh_overflow_time))
4294 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4295 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4296 struct lu_name lname;
4298 __u32 parent_mdt_index;
4300 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4302 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4305 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4309 /* Migrate the object only if none of its parents are on the
4311 if (parent_mdt_index != mdt_index)
4314 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4315 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4316 lname.ln_name, PFID(&fid));
4324 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4325 struct md_object *sobj, const struct lu_name *lname,
4326 struct md_object *tobj, struct md_attr *ma)
4328 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4329 struct mdd_device *mdd = mdo2mdd(pobj);
4330 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4331 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4332 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4333 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4334 bool created = false;
4338 /* If the file will being migrated, it will check whether
4339 * the file is being opened by someone else right now */
4340 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4341 if (mdd_sobj->mod_count > 0) {
4343 "%s: "DFID"%s is already opened count %d: rc = %d\n",
4344 mdd2obd_dev(mdd)->obd_name,
4345 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4346 mdd_sobj->mod_count, -EBUSY);
4347 mdd_read_unlock(env, mdd_sobj);
4348 GOTO(put, rc = -EBUSY);
4350 mdd_read_unlock(env, mdd_sobj);
4352 rc = mdd_la_get(env, mdd_sobj, so_attr);
4356 rc = mdd_la_get(env, mdd_pobj, pattr);
4360 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4367 /* Sigh, it is impossible to finish all of migration in a single
4368 * transaction, for example migrating big directory entries to the
4369 * new MDT, it needs insert all of name entries of children in the
4372 * So migration will be done in multiple steps and transactions.
4374 * 1. create an orphan object on the remote MDT in one transaction.
4375 * 2. migrate extend attributes to the new target file/directory.
4376 * 3. For directory, migrate the entries to the new MDT and update
4377 * linkEA of each children. Because we can not migrate all entries
4378 * in a single transaction, so the migrating directory will become
4379 * a striped directory during migration, so once the process is
4380 * interrupted, the directory is still accessible. (During lookup,
4381 * client will locate the name by searching both original and target
4383 * 4. Finally, update the name/FID to point to the new file/directory
4384 * in a separate transaction.
4387 /* step 1: Check whether the orphan object has been created, and create
4388 * orphan object on the remote MDT if needed */
4389 if (!mdd_object_exists(mdd_tobj)) {
4390 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4397 LASSERT(mdd_object_exists(mdd_tobj));
4398 /* step 2: migrate xattr */
4399 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4403 /* step 3: migrate name entries to the orphan object */
4404 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4405 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4408 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4409 OBD_FAIL_MDS_REINT_NET_REP)))
4412 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4415 LASSERT(mdd_object_exists(mdd_tobj));
4416 /* step 4: update name entry to the new object */
4417 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4422 /* newly created target was not locked, don't cache its attributes */
4424 mdd_invalidate(env, tobj);
4429 const struct md_dir_operations mdd_dir_ops = {
4430 .mdo_is_subdir = mdd_is_subdir,
4431 .mdo_lookup = mdd_lookup,
4432 .mdo_create = mdd_create,
4433 .mdo_rename = mdd_rename,
4434 .mdo_link = mdd_link,
4435 .mdo_unlink = mdd_unlink,
4436 .mdo_create_data = mdd_create_data,
4437 .mdo_migrate = mdd_migrate,