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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
61 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
63 if (!lu_name_is_valid(ln))
65 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
71 /* Get FID from name and parent */
73 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
74 const struct lu_attr *pattr, const struct lu_name *lname,
75 struct lu_fid* fid, int mask)
77 const char *name = lname->ln_name;
78 const struct dt_key *key = (const struct dt_key *)name;
79 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
80 struct mdd_device *m = mdo2mdd(pobj);
81 struct dt_object *dir = mdd_object_child(mdd_obj);
85 if (unlikely(mdd_is_dead_obj(mdd_obj)))
88 if (!mdd_object_exists(mdd_obj))
91 if (mdd_object_remote(mdd_obj)) {
92 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
93 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
96 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
101 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
102 dt_try_as_dir(env, dir)))
103 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
110 int mdd_lookup(const struct lu_env *env,
111 struct md_object *pobj, const struct lu_name *lname,
112 struct lu_fid *fid, struct md_op_spec *spec)
114 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
118 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
122 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
123 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
128 * Get parent FID of the directory
130 * Read parent FID from linkEA, if that fails, then do lookup
131 * dotdot to get the parent FID.
133 * \param[in] env execution environment
134 * \param[in] obj object from which to find the parent FID
135 * \param[in] attr attribute of the object
136 * \param[out] fid fid to get the parent FID
138 * \retval 0 if getting the parent FID succeeds.
139 * \retval negative errno if getting the parent FID fails.
141 static inline int mdd_parent_fid(const struct lu_env *env,
142 struct mdd_object *obj,
143 const struct lu_attr *attr,
146 struct mdd_thread_info *info = mdd_env_info(env);
147 struct linkea_data ldata = { NULL };
148 struct lu_buf *buf = &info->mti_link_buf;
149 struct lu_name lname;
154 LASSERT(S_ISDIR(mdd_object_type(obj)));
156 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
157 if (buf->lb_buf == NULL)
158 GOTO(lookup, rc = 0);
161 rc = mdd_links_read(env, obj, &ldata);
165 LASSERT(ldata.ld_leh != NULL);
166 /* Directory should only have 1 parent */
167 if (ldata.ld_leh->leh_reccount > 1)
170 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
172 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
173 if (likely(fid_is_sane(fid)))
176 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
181 * For root fid use special function, which does not compare version component
182 * of fid. Version component is different for root fids on all MDTs.
184 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
186 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
187 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
191 * return 1: if lf is the fid of the ancestor of p1;
194 * return -EREMOTE: if remote object is found, in this
195 * case fid of remote object is saved to @pf;
197 * otherwise: values < 0, errors.
199 static int mdd_is_parent(const struct lu_env *env,
200 struct mdd_device *mdd,
201 struct mdd_object *p1,
202 const struct lu_attr *attr,
203 const struct lu_fid *lf,
206 struct mdd_object *parent = NULL;
211 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
212 pfid = &mdd_env_info(env)->mti_fid;
214 /* Check for root first. */
215 if (mdd_is_root(mdd, mdo2fid(p1)))
219 /* this is done recursively */
220 rc = mdd_parent_fid(env, p1, attr, pfid);
223 if (mdd_is_root(mdd, pfid))
225 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
227 if (lu_fid_eq(pfid, lf))
230 mdd_object_put(env, parent);
232 parent = mdd_object_find(env, mdd, pfid);
234 GOTO(out, rc = PTR_ERR(parent));
236 if (!mdd_object_exists(parent))
237 GOTO(out, rc = -EINVAL);
243 if (parent && !IS_ERR(parent))
244 mdd_object_put(env, parent);
249 * No permission check is needed.
251 * returns 1: if fid is ancestor of @mo;
252 * returns 0: if fid is not an ancestor of @mo;
254 * returns EREMOTE if remote object is found, fid of remote object is saved to
257 * returns < 0: if error
259 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
260 const struct lu_fid *fid, struct lu_fid *sfid)
262 struct mdd_device *mdd = mdo2mdd(mo);
263 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
267 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
270 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
274 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
278 } else if (rc == 1) {
279 /* found @fid is parent */
287 * Check that @dir contains no entries except (possibly) dot and dotdot.
292 * -ENOTDIR not a directory object
293 * -ENOTEMPTY not empty
297 static int mdd_dir_is_empty(const struct lu_env *env,
298 struct mdd_object *dir)
301 struct dt_object *obj;
302 const struct dt_it_ops *iops;
306 obj = mdd_object_child(dir);
307 if (!dt_try_as_dir(env, obj))
310 iops = &obj->do_index_ops->dio_it;
311 it = iops->init(env, obj, LUDA_64BITHASH);
313 result = iops->get(env, it, (const struct dt_key *)"");
316 for (result = 0, i = 0; result == 0 && i < 3; ++i)
317 result = iops->next(env, it);
320 else if (result == 1)
322 } else if (result == 0)
324 * Huh? Index contains no zero key?
331 result = PTR_ERR(it);
336 * Determine if the target object can be hard linked, and right now it only
337 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
338 * directory nlink count might exceed the maximum link count(see
339 * osd_object_ref_add), so it only check nlink for non-directories.
341 * \param[in] env thread environment
342 * \param[in] obj object being linked to
343 * \param[in] la attributes of \a obj
345 * \retval 0 if \a obj can be hard linked
346 * \retval negative error if \a obj is a directory or has too
349 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
350 const struct lu_attr *la)
352 struct mdd_device *m = mdd_obj2mdd_dev(obj);
357 /* Subdir count limitation can be broken through
358 * (see osd_object_ref_add), so only check non-directory here. */
359 if (!S_ISDIR(la->la_mode) &&
360 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
367 * Check whether it may create the cobj under the pobj.
369 * \param[in] env execution environment
370 * \param[in] pobj the parent directory
371 * \param[in] pattr the attribute of the parent directory
372 * \param[in] cobj the child to be created
373 * \param[in] check_perm if check WRITE|EXEC permission for parent
375 * \retval = 0 create the child under this dir is allowed
376 * \retval negative errno create the child under this dir is
379 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
380 const struct lu_attr *pattr, struct mdd_object *cobj,
386 if (cobj && mdd_object_exists(cobj))
389 if (mdd_is_dead_obj(pobj))
393 rc = mdd_permission_internal_locked(env, pobj, pattr,
394 MAY_WRITE | MAY_EXEC,
400 * Check whether can unlink from the pobj in the case of "cobj == NULL".
402 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
403 const struct lu_attr *pattr, const struct lu_attr *attr)
408 if (mdd_is_dead_obj(pobj))
411 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
414 rc = mdd_permission_internal_locked(env, pobj, pattr,
415 MAY_WRITE | MAY_EXEC,
420 if (pattr->la_flags & LUSTRE_APPEND_FL)
427 * pobj == NULL is remote ops case, under such case, pobj's
428 * VTX feature has been checked already, no need check again.
430 static inline int mdd_is_sticky(const struct lu_env *env,
431 struct mdd_object *pobj,
432 const struct lu_attr *pattr,
433 struct mdd_object *cobj,
434 const struct lu_attr *cattr)
436 struct lu_ucred *uc = lu_ucred_assert(env);
439 LASSERT(pattr != NULL);
440 if (!(pattr->la_mode & S_ISVTX) ||
441 (pattr->la_uid == uc->uc_fsuid))
445 LASSERT(cattr != NULL);
446 if (cattr->la_uid == uc->uc_fsuid)
449 return !md_capable(uc, CFS_CAP_FOWNER);
452 static int mdd_may_delete_entry(const struct lu_env *env,
453 struct mdd_object *pobj,
454 const struct lu_attr *pattr,
459 LASSERT(pobj != NULL);
460 if (!mdd_object_exists(pobj))
463 if (mdd_is_dead_obj(pobj))
468 rc = mdd_permission_internal_locked(env, pobj, pattr,
469 MAY_WRITE | MAY_EXEC,
475 if (pattr->la_flags & LUSTRE_APPEND_FL)
482 * Check whether it may delete the cobj from the pobj.
485 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
486 const struct lu_attr *tpattr, struct mdd_object *tobj,
487 const struct lu_attr *tattr, const struct lu_attr *cattr,
488 int check_perm, int check_empty)
494 LASSERT(tpattr != NULL);
495 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
503 if (!mdd_object_exists(tobj))
506 if (mdd_is_dead_obj(tobj))
509 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
512 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
515 /* additional check the rename case */
517 if (S_ISDIR(cattr->la_mode)) {
518 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
520 if (!S_ISDIR(tattr->la_mode))
523 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
525 } else if (S_ISDIR(tattr->la_mode))
529 if (S_ISDIR(tattr->la_mode) && check_empty)
530 rc = mdd_dir_is_empty(env, tobj);
536 * Check whether it can create the link file(linked to @src_obj) under
537 * the target directory(@tgt_obj), and src_obj has been locked by
540 * \param[in] env execution environment
541 * \param[in] tgt_obj the target directory
542 * \param[in] tattr attributes of target directory
543 * \param[in] lname the link name
544 * \param[in] src_obj source object for link
545 * \param[in] cattr attributes for source object
547 * \retval = 0 it is allowed to create the link file under tgt_obj
548 * \retval negative error not allowed to create the link file
550 static int mdd_link_sanity_check(const struct lu_env *env,
551 struct mdd_object *tgt_obj,
552 const struct lu_attr *tattr,
553 const struct lu_name *lname,
554 struct mdd_object *src_obj,
555 const struct lu_attr *cattr)
557 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
561 if (!mdd_object_exists(src_obj))
564 if (mdd_is_dead_obj(src_obj))
567 /* Local ops, no lookup before link, check filename length here. */
568 rc = mdd_name_check(m, lname);
572 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
575 if (S_ISDIR(mdd_object_type(src_obj)))
578 LASSERT(src_obj != tgt_obj);
579 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
583 rc = __mdd_may_link(env, src_obj, cattr);
588 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
589 const char *name, struct thandle *handle)
591 struct dt_object *next = mdd_object_child(pobj);
595 if (dt_try_as_dir(env, next))
596 rc = dt_delete(env, next, (struct dt_key *)name, handle);
603 static int __mdd_index_insert_only(const struct lu_env *env,
604 struct mdd_object *pobj,
605 const struct lu_fid *lf, __u32 type,
607 struct thandle *handle)
609 struct dt_object *next = mdd_object_child(pobj);
613 if (dt_try_as_dir(env, next)) {
614 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
615 struct lu_ucred *uc = lu_ucred_check(env);
619 rec->rec_type = type;
620 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
621 rc = dt_insert(env, next, (const struct dt_rec *)rec,
622 (const struct dt_key *)name, handle,
630 /* insert named index, add reference if isdir */
631 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
632 const struct lu_fid *lf, __u32 type,
633 const char *name, struct thandle *handle)
638 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
639 if (rc == 0 && S_ISDIR(type)) {
640 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
641 mdo_ref_add(env, pobj, handle);
642 mdd_write_unlock(env, pobj);
648 /* delete named index, drop reference if isdir */
649 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
650 const char *name, int is_dir,
651 struct thandle *handle)
656 rc = __mdd_index_delete_only(env, pobj, name, handle);
657 if (rc == 0 && is_dir) {
658 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
659 mdo_ref_del(env, pobj, handle);
660 mdd_write_unlock(env, pobj);
666 static int mdd_llog_record_calc_size(const struct lu_env *env,
667 const struct lu_name *tname,
668 const struct lu_name *sname)
670 const struct lu_ucred *uc = lu_ucred(env);
671 enum changelog_rec_flags crf = 0;
672 size_t hdr_size = sizeof(struct llog_changelog_rec) -
673 sizeof(struct changelog_rec);
678 if (uc != NULL && uc->uc_jobid[0] != '\0')
681 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
682 (tname != NULL ? tname->ln_namelen : 0) +
683 (sname != NULL ? 1 + sname->ln_namelen : 0));
686 int mdd_declare_changelog_store(const struct lu_env *env,
687 struct mdd_device *mdd,
688 const struct lu_name *tname,
689 const struct lu_name *sname,
690 struct thandle *handle)
692 struct obd_device *obd = mdd2obd_dev(mdd);
693 struct llog_ctxt *ctxt;
694 struct llog_changelog_rec *rec;
696 struct thandle *llog_th;
701 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
704 reclen = mdd_llog_record_calc_size(env, tname, sname);
705 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
706 if (buf->lb_buf == NULL)
710 rec->cr_hdr.lrh_len = reclen;
711 rec->cr_hdr.lrh_type = CHANGELOG_REC;
713 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
717 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
719 GOTO(out_put, rc = PTR_ERR(llog_th));
721 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
729 /** Add a changelog entry \a rec to the changelog llog
732 * \param handle - currently ignored since llogs start their own transaction;
733 * this will hopefully be fixed in llog rewrite
736 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
737 struct llog_changelog_rec *rec, struct thandle *th)
739 struct obd_device *obd = mdd2obd_dev(mdd);
740 struct llog_ctxt *ctxt;
741 struct thandle *llog_th;
744 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
745 changelog_rec_varsize(&rec->cr));
747 /* llog_lvfs_write_rec sets the llog tail len */
748 rec->cr_hdr.lrh_type = CHANGELOG_REC;
749 rec->cr.cr_time = cl_time();
751 spin_lock(&mdd->mdd_cl.mc_lock);
752 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
753 * but as long as the MDD transactions are ordered correctly for e.g.
754 * rename conflicts, I don't think this should matter. */
755 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
756 spin_unlock(&mdd->mdd_cl.mc_lock);
758 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
762 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
764 GOTO(out_put, rc = PTR_ERR(llog_th));
766 /* nested journal transaction */
767 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
776 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
777 const struct lu_fid *sfid,
778 const struct lu_fid *spfid,
779 const struct lu_name *sname)
781 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
782 size_t extsize = sname->ln_namelen + 1;
784 LASSERT(sfid != NULL);
785 LASSERT(spfid != NULL);
786 LASSERT(sname != NULL);
788 rnm->cr_sfid = *sfid;
789 rnm->cr_spfid = *spfid;
791 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
792 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
793 rec->cr_namelen += extsize;
796 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
798 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
800 if (jobid == NULL || jobid[0] == '\0')
803 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
806 /** Store a namespace change changelog record
807 * If this fails, we must fail the whole transaction; we don't
808 * want the change to commit without the log entry.
809 * \param target - mdd_object of change
810 * \param tpfid - target parent dir/object fid
811 * \param sfid - source object fid
812 * \param spfid - source parent fid
813 * \param tname - target name string
814 * \param sname - source name string
815 * \param handle - transaction handle
817 int mdd_changelog_ns_store(const struct lu_env *env,
818 struct mdd_device *mdd,
819 enum changelog_rec_type type,
820 enum changelog_rec_flags crf,
821 struct mdd_object *target,
822 const struct lu_fid *tpfid,
823 const struct lu_fid *sfid,
824 const struct lu_fid *spfid,
825 const struct lu_name *tname,
826 const struct lu_name *sname,
827 struct thandle *handle)
829 const struct lu_ucred *uc = lu_ucred(env);
830 struct llog_changelog_rec *rec;
837 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
840 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
843 LASSERT(tpfid != NULL);
844 LASSERT(tname != NULL);
845 LASSERT(handle != NULL);
847 reclen = mdd_llog_record_calc_size(env, tname, sname);
848 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
849 if (buf->lb_buf == NULL)
855 if (uc != NULL && uc->uc_jobid[0] != '\0')
863 rec->cr.cr_flags = crf;
864 rec->cr.cr_type = (__u32)type;
865 rec->cr.cr_pfid = *tpfid;
866 rec->cr.cr_namelen = tname->ln_namelen;
867 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
869 if (crf & CLF_RENAME)
870 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
873 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
875 if (likely(target != NULL)) {
876 rec->cr.cr_tfid = *mdo2fid(target);
877 target->mod_cltime = cfs_time_current_64();
879 fid_zero(&rec->cr.cr_tfid);
882 rc = mdd_changelog_store(env, mdd, rec, handle);
884 CERROR("%s: cannot store changelog record: type = %d, "
885 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
886 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
887 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
894 static int __mdd_links_add(const struct lu_env *env,
895 struct mdd_object *mdd_obj,
896 struct linkea_data *ldata,
897 const struct lu_name *lname,
898 const struct lu_fid *pfid,
899 int first, int check)
903 if (ldata->ld_leh == NULL) {
904 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
908 rc = linkea_data_new(ldata,
909 &mdd_env_info(env)->mti_link_buf);
916 rc = linkea_links_find(ldata, lname, pfid);
917 if (rc && rc != -ENOENT)
923 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
924 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
928 linkea_add_buf(ldata, lname, tfid);
931 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
932 linkea_add_buf(ldata, lname, pfid);
934 return linkea_add_buf(ldata, lname, pfid);
937 static int __mdd_links_del(const struct lu_env *env,
938 struct mdd_object *mdd_obj,
939 struct linkea_data *ldata,
940 const struct lu_name *lname,
941 const struct lu_fid *pfid)
945 if (ldata->ld_leh == NULL) {
946 rc = mdd_links_read(env, mdd_obj, ldata);
951 rc = linkea_links_find(ldata, lname, pfid);
955 linkea_del_buf(ldata, lname);
959 static int mdd_linkea_prepare(const struct lu_env *env,
960 struct mdd_object *mdd_obj,
961 const struct lu_fid *oldpfid,
962 const struct lu_name *oldlname,
963 const struct lu_fid *newpfid,
964 const struct lu_name *newlname,
965 int first, int check,
966 struct linkea_data *ldata)
972 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
975 LASSERT(oldpfid != NULL || newpfid != NULL);
977 if (mdd_obj->mod_flags & DEAD_OBJ) {
978 /* Prevent linkea to be updated which is NOT necessary. */
979 ldata->ld_reclen = 0;
980 /* No more links, don't bother */
984 if (oldpfid != NULL) {
985 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
988 (rc != -ENODATA && rc != -ENOENT))
990 /* No changes done. */
995 /* If renaming, add the new record */
996 if (newpfid != NULL) {
997 /* even if the add fails, we still delete the out-of-date
999 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1003 rc = rc != 0 ? rc : rc2;
1008 int mdd_links_rename(const struct lu_env *env,
1009 struct mdd_object *mdd_obj,
1010 const struct lu_fid *oldpfid,
1011 const struct lu_name *oldlname,
1012 const struct lu_fid *newpfid,
1013 const struct lu_name *newlname,
1014 struct thandle *handle,
1015 struct linkea_data *ldata,
1016 int first, int check)
1021 if (ldata == NULL) {
1022 ldata = &mdd_env_info(env)->mti_link_data;
1023 memset(ldata, 0, sizeof(*ldata));
1024 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1025 newpfid, newlname, first, check,
1031 if (ldata->ld_reclen != 0)
1032 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1037 if (rc == -EOVERFLOW || rc == -ENOSPC)
1039 if (newlname == NULL)
1040 CDEBUG(error ? D_ERROR : D_OTHER,
1041 "link_ea add failed %d "DFID"\n",
1042 rc, PFID(mdd_object_fid(mdd_obj)));
1043 else if (oldpfid == NULL)
1044 CDEBUG(error ? D_ERROR : D_OTHER,
1045 "link_ea add '%.*s' failed %d "DFID"\n",
1046 newlname->ln_namelen, newlname->ln_name,
1047 rc, PFID(mdd_object_fid(mdd_obj)));
1048 else if (newpfid == NULL)
1049 CDEBUG(error ? D_ERROR : D_OTHER,
1050 "link_ea del '%.*s' failed %d "DFID"\n",
1051 oldlname->ln_namelen, oldlname->ln_name,
1052 rc, PFID(mdd_object_fid(mdd_obj)));
1054 CDEBUG(error ? D_ERROR : D_OTHER,
1055 "link_ea rename '%.*s'->'%.*s' failed %d "
1057 oldlname->ln_namelen, oldlname->ln_name,
1058 newlname->ln_namelen, newlname->ln_name,
1059 rc, PFID(mdd_object_fid(mdd_obj)));
1062 if (is_vmalloc_addr(ldata->ld_buf))
1063 /* if we vmalloced a large buffer drop it */
1064 lu_buf_free(ldata->ld_buf);
1069 static inline int mdd_links_add(const struct lu_env *env,
1070 struct mdd_object *mdd_obj,
1071 const struct lu_fid *pfid,
1072 const struct lu_name *lname,
1073 struct thandle *handle,
1074 struct linkea_data *ldata, int first)
1076 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1077 pfid, lname, handle, ldata, first, 0);
1080 static inline int mdd_links_del(const struct lu_env *env,
1081 struct mdd_object *mdd_obj,
1082 const struct lu_fid *pfid,
1083 const struct lu_name *lname,
1084 struct thandle *handle)
1086 return mdd_links_rename(env, mdd_obj, pfid, lname,
1087 NULL, NULL, handle, NULL, 0, 0);
1090 /** Read the link EA into a temp buffer.
1091 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1092 * A pointer to the buffer is stored in \a ldata::ld_buf.
1094 * \retval 0 or error
1096 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1097 struct linkea_data *ldata)
1101 if (!mdd_object_exists(mdd_obj))
1104 /* First try a small buf */
1105 LASSERT(env != NULL);
1106 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1108 if (ldata->ld_buf->lb_buf == NULL)
1111 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
1112 if (rc == -ERANGE) {
1113 /* Buf was too small, figure out what we need. */
1114 lu_buf_free(ldata->ld_buf);
1115 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1119 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1120 if (ldata->ld_buf->lb_buf == NULL)
1122 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1126 lu_buf_free(ldata->ld_buf);
1127 ldata->ld_buf = NULL;
1131 return linkea_init(ldata);
1134 /** Read the link EA into a temp buffer.
1135 * Uses the name_buf since it is generally large.
1136 * \retval IS_ERR err
1137 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1139 struct lu_buf *mdd_links_get(const struct lu_env *env,
1140 struct mdd_object *mdd_obj)
1142 struct linkea_data ldata = { NULL };
1145 rc = mdd_links_read(env, mdd_obj, &ldata);
1146 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1149 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1150 struct linkea_data *ldata, struct thandle *handle)
1152 const struct lu_buf *buf;
1155 if (ldata == NULL || ldata->ld_buf == NULL ||
1156 ldata->ld_leh == NULL)
1159 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1160 ldata->ld_leh->leh_len);
1161 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1164 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1165 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1166 mdd_object_remote(mdd_obj) == 0) {
1167 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1168 struct thandle *sub_th;
1170 /* XXX: If the linkEA is overflow, then we need to notify the
1171 * namespace LFSCK to skip "nlink" attribute verification
1172 * on this object to avoid the "nlink" to be shrinked by
1173 * wrong. It may be not good an interaction with LFSCK
1174 * like this. We will consider to replace it with other
1175 * mechanism in future. LU-5802. */
1176 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1177 LFSCK_TYPE_NAMESPACE);
1179 sub_th = thandle_get_sub_by_dt(env, handle,
1180 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1181 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1188 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1189 struct thandle *handle, struct linkea_data *ldata,
1190 enum mdd_links_add_overflow overflow)
1196 if (ldata != NULL && ldata->ld_leh != NULL) {
1197 ea_len = ldata->ld_leh->leh_len;
1198 linkea = ldata->ld_buf->lb_buf;
1200 ea_len = DEFAULT_LINKEA_SIZE;
1204 /* XXX: max size? */
1205 rc = mdo_declare_xattr_set(env, mdd_obj,
1206 mdd_buf_get_const(env, linkea, ea_len),
1207 XATTR_NAME_LINK, 0, handle);
1211 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1212 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1213 struct thandle *sub_th;
1215 /* XXX: If the linkEA is overflow, then we need to notify the
1216 * namespace LFSCK to skip "nlink" attribute verification
1217 * on this object to avoid the "nlink" to be shrinked by
1218 * wrong. It may be not good an interaction with LFSCK
1219 * like this. We will consider to replace it with other
1220 * mechanism in future. LU-5802. */
1221 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1222 LFSCK_TYPE_NAMESPACE);
1224 sub_th = thandle_get_sub_by_dt(env, handle,
1225 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1226 rc = lfsck_in_notify(env,
1227 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1234 static inline int mdd_declare_links_del(const struct lu_env *env,
1235 struct mdd_object *c,
1236 struct thandle *handle)
1240 /* For directory, the linkEA will be removed together
1241 * with the object. */
1242 if (!S_ISDIR(mdd_object_type(c)))
1243 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1248 static int mdd_declare_link(const struct lu_env *env,
1249 struct mdd_device *mdd,
1250 struct mdd_object *p,
1251 struct mdd_object *c,
1252 const struct lu_name *name,
1253 struct thandle *handle,
1255 struct linkea_data *data)
1259 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1260 name->ln_name, handle);
1264 rc = mdo_declare_ref_add(env, c, handle);
1268 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1269 rc = mdo_declare_ref_add(env, c, handle);
1274 la->la_valid = LA_CTIME | LA_MTIME;
1275 rc = mdo_declare_attr_set(env, p, la, handle);
1279 la->la_valid = LA_CTIME;
1280 rc = mdo_declare_attr_set(env, c, la, handle);
1284 rc = mdd_declare_links_add(env, c, handle, data,
1285 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1289 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1294 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1295 struct md_object *src_obj, const struct lu_name *lname,
1298 const char *name = lname->ln_name;
1299 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1300 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1301 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1302 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1303 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1304 struct mdd_device *mdd = mdo2mdd(src_obj);
1305 struct thandle *handle;
1306 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1310 rc = mdd_la_get(env, mdd_sobj, cattr);
1314 rc = mdd_la_get(env, mdd_tobj, tattr);
1318 handle = mdd_trans_create(env, mdd);
1320 GOTO(out_pending, rc = PTR_ERR(handle));
1322 memset(ldata, 0, sizeof(*ldata));
1324 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1325 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1327 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1332 rc = mdd_trans_start(env, mdd, handle);
1336 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1337 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1340 GOTO(out_unlock, rc);
1342 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1343 rc = mdo_ref_add(env, mdd_sobj, handle);
1345 GOTO(out_unlock, rc);
1348 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1349 rc = mdo_ref_add(env, mdd_sobj, handle);
1351 GOTO(out_unlock, rc);
1354 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1355 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1358 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1359 mdd_object_type(mdd_sobj),
1362 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1363 mdd_object_type(mdd_sobj),
1368 mdo_ref_del(env, mdd_sobj, handle);
1369 GOTO(out_unlock, rc);
1372 la->la_valid = LA_CTIME | LA_MTIME;
1373 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1375 GOTO(out_unlock, rc);
1377 la->la_valid = LA_CTIME;
1378 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1380 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1381 mdo2fid(mdd_tobj), lname, 0, 0,
1384 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1385 lname, handle, ldata, 0);
1386 /* The failure of links_add should not cause the link
1387 * failure, reset rc here */
1392 mdd_write_unlock(env, mdd_sobj);
1394 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1395 mdo2fid(mdd_tobj), NULL, NULL,
1396 lname, NULL, handle);
1398 mdd_trans_stop(env, mdd, rc, handle);
1400 if (is_vmalloc_addr(ldata->ld_buf))
1401 /* if we vmalloced a large buffer drop it */
1402 lu_buf_free(ldata->ld_buf);
1407 static int mdd_mark_orphan_object(const struct lu_env *env,
1408 struct mdd_object *obj, struct thandle *handle,
1411 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1414 if (!S_ISDIR(mdd_object_type(obj)))
1417 attr->la_valid = LA_FLAGS;
1418 attr->la_flags = LUSTRE_ORPHAN_FL;
1421 rc = mdo_declare_attr_set(env, obj, attr, handle);
1423 rc = mdo_attr_set(env, obj, attr, handle);
1428 static int mdd_declare_finish_unlink(const struct lu_env *env,
1429 struct mdd_object *obj,
1430 struct thandle *handle)
1434 /* Sigh, we do not know if the unlink object will become orphan in
1435 * declare phase, but fortunately the flags here does not matter
1436 * in current declare implementation */
1437 rc = mdd_mark_orphan_object(env, obj, handle, true);
1441 rc = mdo_declare_destroy(env, obj, handle);
1445 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1449 return mdd_declare_links_del(env, obj, handle);
1452 /* caller should take a lock before calling */
1453 int mdd_finish_unlink(const struct lu_env *env,
1454 struct mdd_object *obj, struct md_attr *ma,
1455 const struct mdd_object *pobj,
1456 const struct lu_name *lname,
1460 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1463 LASSERT(mdd_write_locked(env, obj) != 0);
1465 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1466 /* add new orphan and the object
1467 * will be deleted during mdd_close() */
1468 obj->mod_flags |= DEAD_OBJ;
1469 if (obj->mod_count) {
1470 rc = __mdd_orphan_add(env, obj, th);
1472 CDEBUG(D_HA, "Object "DFID" is inserted into "
1473 "orphan list, open count = %d\n",
1474 PFID(mdd_object_fid(obj)),
1477 CERROR("Object "DFID" fail to be an orphan, "
1478 "open count = %d, maybe cause failed "
1480 PFID(mdd_object_fid(obj)),
1483 /* mark object as an orphan here, not
1484 * before __mdd_orphan_add() as racing
1485 * mdd_la_get() may propagate ORPHAN_OBJ
1486 * causing the asserition */
1487 rc = mdd_mark_orphan_object(env, obj, th, false);
1489 rc = mdo_destroy(env, obj, th);
1491 } else if (!is_dir) {
1492 /* old files may not have link ea; ignore errors */
1493 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1501 * has mdd_write_lock on cobj already, but not on pobj yet
1503 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1504 const struct lu_attr *pattr,
1505 struct mdd_object *cobj,
1506 const struct lu_attr *cattr)
1511 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1516 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1517 struct mdd_object *p, struct mdd_object *c,
1518 const struct lu_name *name, struct md_attr *ma,
1519 struct thandle *handle, int no_name, int is_dir)
1521 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1524 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1525 if (likely(no_name == 0)) {
1526 rc = mdo_declare_index_delete(env, p, name->ln_name,
1533 rc = mdo_declare_ref_del(env, p, handle);
1539 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1540 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1541 la->la_valid = LA_CTIME | LA_MTIME;
1542 rc = mdo_declare_attr_set(env, p, la, handle);
1547 rc = mdo_declare_ref_del(env, c, handle);
1551 rc = mdo_declare_ref_del(env, c, handle);
1555 la->la_valid = LA_CTIME;
1556 rc = mdo_declare_attr_set(env, c, la, handle);
1560 rc = mdd_declare_finish_unlink(env, c, handle);
1564 /* FIXME: need changelog for remove entry */
1565 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1572 * test if a file has an HSM archive
1573 * if HSM attributes are not found in ma update them from
1576 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1577 struct mdd_object *obj,
1582 if (!(ma->ma_valid & MA_HSM)) {
1583 /* no HSM MD provided, read xattr */
1584 struct lu_buf *hsm_buf;
1585 const size_t buflen = sizeof(struct hsm_attrs);
1588 hsm_buf = mdd_buf_get(env, NULL, 0);
1589 lu_buf_alloc(hsm_buf, buflen);
1590 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1591 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1592 lu_buf_free(hsm_buf);
1596 ma->ma_valid |= MA_HSM;
1598 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1604 * Delete name entry and the object.
1605 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1606 * does not exist for this object, and it could only happen during resending
1607 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1608 * is also needed in this case(needed by changelog), so we have to add another
1609 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1610 * the ENOENT failure should be able to be fixed by redo mechanism.
1612 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1613 struct md_object *cobj, const struct lu_name *lname,
1614 struct md_attr *ma, int no_name)
1616 const char *name = lname->ln_name;
1617 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1618 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1619 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1620 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1621 struct mdd_object *mdd_cobj = NULL;
1622 struct mdd_device *mdd = mdo2mdd(pobj);
1623 struct thandle *handle;
1624 int rc, is_dir = 0, cl_flags = 0;
1627 /* cobj == NULL means only delete name entry */
1628 if (likely(cobj != NULL)) {
1629 mdd_cobj = md2mdd_obj(cobj);
1630 if (mdd_object_exists(mdd_cobj) == 0)
1634 rc = mdd_la_get(env, mdd_pobj, pattr);
1638 if (likely(mdd_cobj != NULL)) {
1640 rc = mdd_la_get(env, mdd_cobj, cattr);
1644 is_dir = S_ISDIR(cattr->la_mode);
1645 /* search for an existing archive.
1646 * we should check ahead as the object
1647 * can be destroyed in this transaction */
1648 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1649 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1652 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1656 handle = mdd_trans_create(env, mdd);
1658 RETURN(PTR_ERR(handle));
1660 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1661 lname, ma, handle, no_name, is_dir);
1665 rc = mdd_trans_start(env, mdd, handle);
1669 if (likely(mdd_cobj != NULL))
1670 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1672 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1673 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1678 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1679 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1680 GOTO(cleanup, rc = 0);
1682 if (likely(mdd_cobj != NULL)) {
1683 rc = mdo_ref_del(env, mdd_cobj, handle);
1685 __mdd_index_insert_only(env, mdd_pobj,
1687 mdd_object_type(mdd_cobj),
1694 mdo_ref_del(env, mdd_cobj, handle);
1696 /* fetch updated nlink */
1697 rc = mdd_la_get(env, mdd_cobj, cattr);
1702 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1703 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1705 la->la_valid = LA_CTIME | LA_MTIME;
1706 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1710 /* Enough for only unlink the entry */
1711 if (unlikely(mdd_cobj == NULL))
1714 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1715 /* update ctime of an unlinked file only if it is still
1716 * opened or a link still exists */
1717 la->la_valid = LA_CTIME;
1718 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1723 /* XXX: this transfer to ma will be removed with LOD/OSP */
1724 ma->ma_attr = *cattr;
1725 ma->ma_valid |= MA_INODE;
1726 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1730 /* fetch updated nlink */
1731 rc = mdd_la_get(env, mdd_cobj, cattr);
1732 /* if object is removed then we can't get its attrs,
1734 if (rc == -ENOENT) {
1735 cattr->la_nlink = 0;
1739 if (cattr->la_nlink == 0) {
1740 ma->ma_attr = *cattr;
1741 ma->ma_valid |= MA_INODE;
1746 if (likely(mdd_cobj != NULL))
1747 mdd_write_unlock(env, mdd_cobj);
1750 if (cattr->la_nlink == 0)
1751 cl_flags |= CLF_UNLINK_LAST;
1753 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1755 rc = mdd_changelog_ns_store(env, mdd,
1756 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1757 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1762 mdd_trans_stop(env, mdd, rc, handle);
1768 * The permission has been checked when obj created, no need check again.
1770 static int mdd_cd_sanity_check(const struct lu_env *env,
1771 struct mdd_object *obj)
1776 if (!obj || mdd_is_dead_obj(obj))
1782 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1783 struct md_object *cobj, const struct md_op_spec *spec,
1786 struct mdd_device *mdd = mdo2mdd(cobj);
1787 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1788 struct mdd_object *son = md2mdd_obj(cobj);
1789 struct thandle *handle;
1790 const struct lu_buf *buf;
1791 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1792 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1796 rc = mdd_cd_sanity_check(env, son);
1800 if (!md_should_create(spec->sp_cr_flags))
1804 * there are following use cases for this function:
1805 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1806 * striping can be specified or not
1809 rc = mdd_la_get(env, son, attr);
1813 /* calling ->ah_make_hint() is used to transfer information from parent */
1814 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1816 handle = mdd_trans_create(env, mdd);
1818 GOTO(out_free, rc = PTR_ERR(handle));
1821 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1822 * Should this be fixed?
1824 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1825 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1826 spec->sp_cr_flags, spec->no_create);
1828 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1829 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1830 spec->u.sp_ea.eadatalen);
1835 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1836 XATTR_NAME_LOV, 0, handle);
1840 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1844 rc = mdd_trans_start(env, mdd, handle);
1848 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1854 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1857 mdd_trans_stop(env, mdd, rc, handle);
1862 static int mdd_declare_object_initialize(const struct lu_env *env,
1863 struct mdd_object *parent,
1864 struct mdd_object *child,
1865 struct lu_attr *attr,
1866 struct thandle *handle)
1872 * inode mode has been set in creation time, and it's based on umask,
1873 * la_mode and acl, don't set here again! (which will go wrong
1874 * because below function doesn't consider umask).
1875 * I'd suggest set all object attributes in creation time, see above.
1877 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1878 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1879 rc = mdo_declare_attr_set(env, child, attr, handle);
1880 attr->la_valid |= LA_MODE | LA_TYPE;
1881 if (rc != 0 || !S_ISDIR(attr->la_mode))
1884 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1889 rc = mdo_declare_ref_add(env, child, handle);
1893 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1899 static int mdd_object_initialize(const struct lu_env *env,
1900 const struct lu_fid *pfid,
1901 struct mdd_object *child,
1902 struct lu_attr *attr, struct thandle *handle,
1903 const struct md_op_spec *spec)
1908 if (S_ISDIR(attr->la_mode)) {
1909 /* Add "." and ".." for newly created dir */
1910 mdo_ref_add(env, child, handle);
1911 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1912 S_IFDIR, dot, handle);
1914 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1917 mdo_ref_del(env, child, handle);
1924 * This function checks whether it can create a file/dir under the
1925 * directory(@pobj). The directory(@pobj) is not being locked by
1928 * \param[in] env execution environment
1929 * \param[in] pobj the directory to create files
1930 * \param[in] pattr the attributes of the directory
1931 * \param[in] lname the name of the created file/dir
1932 * \param[in] cattr the attributes of the file/dir
1933 * \param[in] spec create specification
1935 * \retval = 0 it is allowed to create file/dir under
1937 * \retval negative error not allowed to create file/dir
1938 * under the directory
1940 static int mdd_create_sanity_check(const struct lu_env *env,
1941 struct md_object *pobj,
1942 const struct lu_attr *pattr,
1943 const struct lu_name *lname,
1944 struct lu_attr *cattr,
1945 struct md_op_spec *spec)
1947 struct mdd_thread_info *info = mdd_env_info(env);
1948 struct lu_fid *fid = &info->mti_fid;
1949 struct mdd_object *obj = md2mdd_obj(pobj);
1950 struct mdd_device *m = mdo2mdd(pobj);
1951 bool check_perm = true;
1956 if (mdd_is_dead_obj(obj))
1960 * In some cases this lookup is not needed - we know before if name
1961 * exists or not because MDT performs lookup for it.
1962 * name length check is done in lookup.
1964 if (spec->sp_cr_lookup) {
1966 * Check if the name already exist, though it will be checked in
1967 * _index_insert also, for avoiding rolling back if exists
1970 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1971 MAY_WRITE | MAY_EXEC);
1973 RETURN(rc ? : -EEXIST);
1975 /* Permission is already being checked in mdd_lookup */
1979 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1984 if (pattr->la_mode & S_ISGID) {
1985 cattr->la_gid = pattr->la_gid;
1986 if (S_ISDIR(cattr->la_mode)) {
1987 cattr->la_mode |= S_ISGID;
1988 cattr->la_valid |= LA_MODE;
1992 rc = mdd_name_check(m, lname);
1996 switch (cattr->la_mode & S_IFMT) {
1998 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2000 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2001 RETURN(-ENAMETOOLONG);
2020 static int mdd_declare_object_create(const struct lu_env *env,
2021 struct mdd_device *mdd,
2022 struct mdd_object *p, struct mdd_object *c,
2023 struct lu_attr *attr,
2024 struct thandle *handle,
2025 const struct md_op_spec *spec,
2026 struct lu_buf *def_acl_buf,
2027 struct lu_buf *acl_buf,
2028 struct dt_allocation_hint *hint)
2032 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2037 #ifdef CONFIG_FS_POSIX_ACL
2038 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2039 /* if dir, then can inherit default ACl */
2040 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2041 XATTR_NAME_ACL_DEFAULT,
2047 if (acl_buf->lb_len > 0) {
2048 rc = mdo_declare_attr_set(env, c, attr, handle);
2052 rc = mdo_declare_xattr_set(env, c, acl_buf,
2053 XATTR_NAME_ACL_ACCESS, 0, handle);
2058 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2062 /* replay case, create LOV EA from client data */
2063 if (spec->no_create ||
2064 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2065 const struct lu_buf *buf;
2067 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2068 spec->u.sp_ea.eadatalen);
2069 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2075 if (S_ISLNK(attr->la_mode)) {
2076 const char *target_name = spec->u.sp_symname;
2077 int sym_len = strlen(target_name);
2078 const struct lu_buf *buf;
2080 buf = mdd_buf_get_const(env, target_name, sym_len);
2081 rc = dt_declare_record_write(env, mdd_object_child(c),
2090 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2091 struct mdd_object *p, struct mdd_object *c,
2092 const struct lu_name *name,
2093 struct lu_attr *attr,
2094 struct thandle *handle,
2095 const struct md_op_spec *spec,
2096 struct linkea_data *ldata,
2097 struct lu_buf *def_acl_buf,
2098 struct lu_buf *acl_buf,
2099 struct dt_allocation_hint *hint)
2103 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2104 def_acl_buf, acl_buf, hint);
2108 if (S_ISDIR(attr->la_mode)) {
2109 rc = mdo_declare_ref_add(env, p, handle);
2114 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2115 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2119 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2121 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2122 name->ln_name, handle);
2126 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2131 la->la_valid = LA_CTIME | LA_MTIME;
2132 rc = mdo_declare_attr_set(env, p, la, handle);
2136 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2144 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2145 struct lu_attr *la, struct lu_buf *def_acl_buf,
2146 struct lu_buf *acl_buf)
2151 if (S_ISLNK(la->la_mode)) {
2152 acl_buf->lb_len = 0;
2153 def_acl_buf->lb_len = 0;
2157 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2158 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2159 XATTR_NAME_ACL_DEFAULT);
2160 mdd_read_unlock(env, pobj);
2162 /* If there are default ACL, fix mode/ACL by default ACL */
2163 def_acl_buf->lb_len = rc;
2164 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2165 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2166 acl_buf->lb_len = rc;
2167 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2170 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2171 /* If there are no default ACL, fix mode by mask */
2172 struct lu_ucred *uc = lu_ucred(env);
2174 /* The create triggered by MDT internal events, such as
2175 * LFSCK reset, will not contain valid "uc". */
2176 if (unlikely(uc != NULL))
2177 la->la_mode &= ~uc->uc_umask;
2179 acl_buf->lb_len = 0;
2180 def_acl_buf->lb_len = 0;
2187 * Create a metadata object and initialize it, set acl, xattr.
2189 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2190 struct mdd_object *son, struct lu_attr *attr,
2191 struct md_op_spec *spec, struct lu_buf *acl_buf,
2192 struct lu_buf *def_acl_buf,
2193 struct dt_allocation_hint *hint,
2194 struct thandle *handle)
2198 mdd_write_lock(env, son, MOR_TGT_CHILD);
2199 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2204 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2205 * created in declare phase, they also needs to be added to master
2206 * object as sub-directory entry. So it has to initialize the master
2207 * object, then set dir striped EA.(in mdo_xattr_set) */
2208 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2211 GOTO(err_destroy, rc);
2214 * in case of replay we just set LOVEA provided by the client
2215 * XXX: I think it would be interesting to try "old" way where
2216 * MDT calls this xattr_set(LOV) in a different transaction.
2217 * probably this way we code can be made better.
2220 /* During creation, there are only a few cases we need do xattr_set to
2222 * 1. regular file: see comments above.
2223 * 2. dir: inherit default striping or pool settings from parent.
2224 * 3. create striped directory with provided stripeEA.
2225 * 4. create striped directory because inherit default layout from the
2228 if (spec->no_create ||
2229 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2230 S_ISDIR(attr->la_mode)) {
2231 const struct lu_buf *buf;
2233 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2234 spec->u.sp_ea.eadatalen);
2235 rc = mdo_xattr_set(env, son, buf,
2236 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2240 GOTO(err_destroy, rc);
2243 #ifdef CONFIG_FS_POSIX_ACL
2244 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2245 S_ISDIR(attr->la_mode)) {
2246 /* set default acl */
2247 rc = mdo_xattr_set(env, son, def_acl_buf,
2248 XATTR_NAME_ACL_DEFAULT, 0,
2251 GOTO(err_destroy, rc);
2253 /* set its own acl */
2254 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2255 rc = mdo_xattr_set(env, son, acl_buf,
2256 XATTR_NAME_ACL_ACCESS,
2259 GOTO(err_destroy, rc);
2263 if (S_ISLNK(attr->la_mode)) {
2264 struct lu_ucred *uc = lu_ucred_assert(env);
2265 struct dt_object *dt = mdd_object_child(son);
2266 const char *target_name = spec->u.sp_symname;
2267 int sym_len = strlen(target_name);
2268 const struct lu_buf *buf;
2271 buf = mdd_buf_get_const(env, target_name, sym_len);
2272 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2274 CFS_CAP_SYS_RESOURCE_MASK);
2279 GOTO(err_initlized, rc = -EFAULT);
2283 if (unlikely(rc != 0)) {
2285 if (S_ISDIR(attr->la_mode)) {
2286 /* Drop the reference, no need to delete "."/"..",
2287 * because the object to be destroied directly. */
2288 rc2 = mdo_ref_del(env, son, handle);
2292 rc2 = mdo_ref_del(env, son, handle);
2296 mdo_destroy(env, son, handle);
2299 mdd_write_unlock(env, son);
2303 static int mdd_index_delete(const struct lu_env *env,
2304 struct mdd_object *mdd_pobj,
2305 struct lu_attr *cattr,
2306 const struct lu_name *lname)
2308 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2309 struct thandle *handle;
2313 handle = mdd_trans_create(env, mdd);
2315 RETURN(PTR_ERR(handle));
2317 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2322 if (S_ISDIR(cattr->la_mode)) {
2323 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2328 /* Since this will only be used in the error handler path,
2329 * Let's set the thandle to be local and not mess the transno */
2330 handle->th_local = 1;
2331 rc = mdd_trans_start(env, mdd, handle);
2335 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2336 S_ISDIR(cattr->la_mode), handle);
2340 mdd_trans_stop(env, mdd, rc, handle);
2345 * Create object and insert it into namespace.
2347 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2348 const struct lu_name *lname, struct md_object *child,
2349 struct md_op_spec *spec, struct md_attr* ma)
2351 struct mdd_thread_info *info = mdd_env_info(env);
2352 struct lu_attr *la = &info->mti_la_for_fix;
2353 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2354 struct mdd_object *son = md2mdd_obj(child);
2355 struct mdd_device *mdd = mdo2mdd(pobj);
2356 struct lu_attr *attr = &ma->ma_attr;
2357 struct thandle *handle;
2358 struct lu_attr *pattr = &info->mti_pattr;
2359 struct lu_buf acl_buf;
2360 struct lu_buf def_acl_buf;
2361 struct linkea_data *ldata = &info->mti_link_data;
2362 const char *name = lname->ln_name;
2363 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2369 * Two operations have to be performed:
2371 * - an allocation of a new object (->do_create()), and
2373 * - an insertion into a parent index (->dio_insert()).
2375 * Due to locking, operation order is not important, when both are
2376 * successful, *but* error handling cases are quite different:
2378 * - if insertion is done first, and following object creation fails,
2379 * insertion has to be rolled back, but this operation might fail
2380 * also leaving us with dangling index entry.
2382 * - if creation is done first, is has to be undone if insertion
2383 * fails, leaving us with leaked space, which is neither good, nor
2386 * It seems that creation-first is simplest solution, but it is
2387 * sub-optimal in the frequent
2392 * case, because second mkdir is bound to create object, only to
2393 * destroy it immediately.
2395 * To avoid this follow local file systems that do double lookup:
2397 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2399 * 1. create (mdd_object_create_internal())
2401 * 2. insert (__mdd_index_insert(), lookup again)
2404 rc = mdd_la_get(env, mdd_pobj, pattr);
2408 /* Sanity checks before big job. */
2409 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2413 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2414 GOTO(out_free, rc = -EINPROGRESS);
2416 handle = mdd_trans_create(env, mdd);
2418 GOTO(out_free, rc = PTR_ERR(handle));
2420 acl_buf.lb_buf = info->mti_xattr_buf;
2421 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2422 def_acl_buf.lb_buf = info->mti_key;
2423 def_acl_buf.lb_len = sizeof(info->mti_key);
2424 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2428 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2430 memset(ldata, 0, sizeof(*ldata));
2431 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2432 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2435 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2436 &tfid, lname, 1, 0, ldata);
2438 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2439 mdd_object_fid(mdd_pobj),
2440 lname, 1, 0, ldata);
2443 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2444 handle, spec, ldata, &def_acl_buf, &acl_buf,
2449 rc = mdd_trans_start(env, mdd, handle);
2453 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2454 &def_acl_buf, hint, handle);
2458 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2459 mdd_write_lock(env, son, MOR_TGT_CHILD);
2460 rc = __mdd_orphan_add(env, son, handle);
2461 GOTO(out_volatile, rc);
2463 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2464 attr->la_mode, name, handle);
2466 GOTO(err_created, rc);
2468 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2471 /* update parent directory mtime/ctime */
2473 la->la_valid = LA_CTIME | LA_MTIME;
2474 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2476 GOTO(err_insert, rc);
2484 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2485 rc2 = __mdd_orphan_del(env, son, handle);
2487 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2488 S_ISDIR(attr->la_mode),
2494 mdd_write_lock(env, son, MOR_TGT_CHILD);
2495 if (S_ISDIR(attr->la_mode)) {
2496 /* Drop the reference, no need to delete "."/"..",
2497 * because the object is to be destroyed directly. */
2498 rc2 = mdo_ref_del(env, son, handle);
2500 mdd_write_unlock(env, son);
2505 /* For volatile files drop one link immediately, since there is
2506 * no filename in the namespace, and save any error returned. */
2507 rc2 = mdo_ref_del(env, son, handle);
2509 mdd_write_unlock(env, son);
2510 if (unlikely(rc == 0))
2515 /* Don't destroy the volatile object on success */
2516 if (likely(rc != 0))
2517 mdo_destroy(env, son, handle);
2518 mdd_write_unlock(env, son);
2521 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2522 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2523 rc = mdd_changelog_ns_store(env, mdd,
2524 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2525 S_ISREG(attr->la_mode) ? CL_CREATE :
2526 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2527 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2530 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2532 /* If creation fails, it is most likely due to the remote update
2533 * failure, because local transaction will mostly succeed at
2534 * this stage. There is no easy way to rollback all of previous
2535 * updates, so let's remove the object from namespace, and
2536 * LFSCK should handle the orphan object. */
2537 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2538 mdd_index_delete(env, mdd_pobj, attr, lname);
2542 if (is_vmalloc_addr(ldata->ld_buf))
2543 /* if we vmalloced a large buffer drop it */
2544 lu_buf_free(ldata->ld_buf);
2546 /* The child object shouldn't be cached anymore */
2548 set_bit(LU_OBJECT_HEARD_BANSHEE,
2549 &child->mo_lu.lo_header->loh_flags);
2554 * Get locks on parents in proper order
2555 * RETURN: < 0 - error, rename_order if successful
2563 static int mdd_rename_order(const struct lu_env *env,
2564 struct mdd_device *mdd,
2565 struct mdd_object *src_pobj,
2566 const struct lu_attr *pattr,
2567 struct mdd_object *tgt_pobj)
2569 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2573 if (src_pobj == tgt_pobj)
2574 RETURN(MDD_RN_SAME);
2576 /* compared the parent child relationship of src_p&tgt_p */
2577 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2579 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2582 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2596 /* has not mdd_write{read}_lock on any obj yet. */
2597 static int mdd_rename_sanity_check(const struct lu_env *env,
2598 struct mdd_object *src_pobj,
2599 const struct lu_attr *pattr,
2600 struct mdd_object *tgt_pobj,
2601 const struct lu_attr *tpattr,
2602 struct mdd_object *sobj,
2603 const struct lu_attr *cattr,
2604 struct mdd_object *tobj,
2605 const struct lu_attr *tattr)
2610 /* XXX: when get here, sobj must NOT be NULL,
2611 * the other case has been processed in cld_rename
2612 * before mdd_rename and enable MDS_PERM_BYPASS. */
2615 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2619 /* XXX: when get here, "tobj == NULL" means tobj must
2620 * NOT exist (neither on remote MDS, such case has been
2621 * processed in cld_rename before mdd_rename and enable
2623 * So check may_create, but not check may_unlink. */
2625 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2626 (src_pobj != tgt_pobj));
2628 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2629 (src_pobj != tgt_pobj), 1);
2631 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2632 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2637 static int mdd_declare_rename(const struct lu_env *env,
2638 struct mdd_device *mdd,
2639 struct mdd_object *mdd_spobj,
2640 struct mdd_object *mdd_tpobj,
2641 struct mdd_object *mdd_sobj,
2642 struct mdd_object *mdd_tobj,
2643 const struct lu_name *sname,
2644 const struct lu_name *tname,
2646 struct linkea_data *ldata,
2647 struct thandle *handle)
2649 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2652 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2653 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2659 /* name from source dir */
2660 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2664 /* .. from source child */
2665 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2666 /* source child can be directory,
2667 * counted by source dir's nlink */
2668 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2671 if (mdd_spobj != mdd_tpobj) {
2672 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2677 rc = mdo_declare_index_insert(env, mdd_sobj,
2679 S_IFDIR, dotdot, handle);
2684 /* new target child can be directory,
2685 * counted by target dir's nlink */
2686 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2691 la->la_valid = LA_CTIME | LA_MTIME;
2692 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2696 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2700 la->la_valid = LA_CTIME;
2701 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2705 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2706 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2711 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2712 mdd_object_type(mdd_sobj),
2713 tname->ln_name, handle);
2717 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2718 /* delete target child in target parent directory */
2719 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2724 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2728 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2729 /* target child can be directory,
2730 * delete "." reference in target child directory */
2731 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2735 /* delete ".." reference in target parent directory */
2736 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2741 la->la_valid = LA_CTIME;
2742 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2746 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2751 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2758 /* src object can be remote that is why we use only fid and type of object */
2759 static int mdd_rename(const struct lu_env *env,
2760 struct md_object *src_pobj, struct md_object *tgt_pobj,
2761 const struct lu_fid *lf, const struct lu_name *lsname,
2762 struct md_object *tobj, const struct lu_name *ltname,
2765 const char *sname = lsname->ln_name;
2766 const char *tname = ltname->ln_name;
2767 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2768 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2769 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2770 struct mdd_device *mdd = mdo2mdd(src_pobj);
2771 struct mdd_object *mdd_sobj = NULL; /* source object */
2772 struct mdd_object *mdd_tobj = NULL;
2773 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2774 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2775 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2776 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2777 struct thandle *handle;
2778 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2779 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2780 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2783 bool tobj_locked = 0;
2784 unsigned cl_flags = 0;
2789 mdd_tobj = md2mdd_obj(tobj);
2791 mdd_sobj = mdd_object_find(env, mdd, lf);
2792 if (IS_ERR(mdd_sobj))
2793 RETURN(PTR_ERR(mdd_sobj));
2795 rc = mdd_la_get(env, mdd_sobj, cattr);
2797 GOTO(out_pending, rc);
2799 rc = mdd_la_get(env, mdd_spobj, pattr);
2801 GOTO(out_pending, rc);
2804 rc = mdd_la_get(env, mdd_tobj, tattr);
2806 GOTO(out_pending, rc);
2807 /* search for an existing archive.
2808 * we should check ahead as the object
2809 * can be destroyed in this transaction */
2810 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2811 cl_flags |= CLF_RENAME_LAST_EXISTS;
2814 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2816 GOTO(out_pending, rc);
2818 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2819 mdd_sobj, cattr, mdd_tobj, tattr);
2821 GOTO(out_pending, rc);
2823 rc = mdd_name_check(mdd, ltname);
2825 GOTO(out_pending, rc);
2827 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2828 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2830 GOTO(out_pending, rc);
2832 handle = mdd_trans_create(env, mdd);
2834 GOTO(out_pending, rc = PTR_ERR(handle));
2836 memset(ldata, 0, sizeof(*ldata));
2837 mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj), lsname,
2838 mdd_object_fid(mdd_tpobj), ltname, 1, 0, ldata);
2839 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2840 mdd_tobj, lsname, ltname, ma, ldata, handle);
2844 rc = mdd_trans_start(env, mdd, handle);
2848 is_dir = S_ISDIR(cattr->la_mode);
2850 /* Remove source name from source directory */
2851 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2855 /* "mv dir1 dir2" needs "dir1/.." link update */
2856 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2857 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2859 GOTO(fixup_spobj2, rc);
2861 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2864 GOTO(fixup_spobj, rc);
2867 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2868 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2870 /* tname might been renamed to something else */
2871 GOTO(fixup_spobj, rc);
2874 /* Insert new fid with target name into target dir */
2875 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2878 GOTO(fixup_tpobj, rc);
2880 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2881 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2883 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2884 la->la_valid = LA_CTIME;
2885 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2887 GOTO(fixup_tpobj, rc);
2889 /* Update the linkEA for the source object */
2890 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2891 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2892 mdo2fid(mdd_tpobj), ltname, handle, ldata,
2895 /* Old files might not have EA entry */
2896 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2897 lsname, handle, NULL, 0);
2898 mdd_write_unlock(env, mdd_sobj);
2899 /* We don't fail the transaction if the link ea can't be
2900 updated -- fid2path will use alternate lookup method. */
2903 /* Remove old target object
2904 * For tobj is remote case cmm layer has processed
2905 * and set tobj to NULL then. So when tobj is NOT NULL,
2906 * it must be local one.
2908 if (tobj && mdd_object_exists(mdd_tobj)) {
2909 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2911 if (mdd_is_dead_obj(mdd_tobj)) {
2912 /* shld not be dead, something is wrong */
2913 CERROR("tobj is dead, something is wrong\n");
2917 mdo_ref_del(env, mdd_tobj, handle);
2919 /* Remove dot reference. */
2920 if (S_ISDIR(tattr->la_mode))
2921 mdo_ref_del(env, mdd_tobj, handle);
2924 /* fetch updated nlink */
2925 rc = mdd_la_get(env, mdd_tobj, tattr);
2927 CERROR("%s: Failed to get nlink for tobj "
2929 mdd2obd_dev(mdd)->obd_name,
2930 PFID(tpobj_fid), rc);
2931 GOTO(fixup_tpobj, rc);
2934 la->la_valid = LA_CTIME;
2935 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2937 CERROR("%s: Failed to set ctime for tobj "
2939 mdd2obd_dev(mdd)->obd_name,
2940 PFID(tpobj_fid), rc);
2941 GOTO(fixup_tpobj, rc);
2944 /* XXX: this transfer to ma will be removed with LOD/OSP */
2945 ma->ma_attr = *tattr;
2946 ma->ma_valid |= MA_INODE;
2947 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2950 CERROR("%s: Failed to unlink tobj "
2952 mdd2obd_dev(mdd)->obd_name,
2953 PFID(tpobj_fid), rc);
2954 GOTO(fixup_tpobj, rc);
2957 /* fetch updated nlink */
2958 rc = mdd_la_get(env, mdd_tobj, tattr);
2959 if (rc == -ENOENT) {
2960 /* the object got removed, let's
2961 * return the latest known attributes */
2962 tattr->la_nlink = 0;
2964 } else if (rc != 0) {
2965 CERROR("%s: Failed to get nlink for tobj "
2967 mdd2obd_dev(mdd)->obd_name,
2968 PFID(tpobj_fid), rc);
2969 GOTO(fixup_tpobj, rc);
2971 /* XXX: this transfer to ma will be removed with LOD/OSP */
2972 ma->ma_attr = *tattr;
2973 ma->ma_valid |= MA_INODE;
2975 if (tattr->la_nlink == 0)
2976 cl_flags |= CLF_RENAME_LAST;
2978 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
2981 la->la_valid = LA_CTIME | LA_MTIME;
2982 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2984 GOTO(fixup_tpobj, rc);
2986 if (mdd_spobj != mdd_tpobj) {
2987 la->la_valid = LA_CTIME | LA_MTIME;
2988 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2990 GOTO(fixup_tpobj, rc);
2997 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2999 CWARN("tp obj fix error %d\n",rc2);
3001 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3002 !mdd_is_dead_obj(mdd_tobj)) {
3004 mdo_ref_add(env, mdd_tobj, handle);
3006 mdo_ref_add(env, mdd_tobj, handle);
3009 rc2 = __mdd_index_insert(env, mdd_tpobj,
3011 mdd_object_type(mdd_tobj),
3014 CWARN("tp obj fix error: rc = %d\n", rc2);
3019 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3020 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3022 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3023 mdd2obd_dev(mdd)->obd_name, rc2);
3026 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3029 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3030 mdd2obd_dev(mdd)->obd_name, rc2);
3035 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3036 mdd_object_type(mdd_sobj), sname,
3039 CWARN("sp obj fix error: rc = %d\n", rc2);
3044 mdd_write_unlock(env, mdd_tobj);
3047 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3048 mdd_tobj, tpobj_fid, lf, spobj_fid,
3049 ltname, lsname, handle);
3052 mdd_trans_stop(env, mdd, rc, handle);
3055 mdd_object_put(env, mdd_sobj);
3060 * During migration once the parent FID has been changed,
3061 * we need update the parent FID in linkea.
3063 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3064 struct mdd_object *parent,
3065 struct mdd_object *newparent,
3066 struct mdd_object *child,
3067 const char *name, int namelen,
3068 struct thandle *handle,
3071 struct mdd_thread_info *info = mdd_env_info(env);
3072 struct linkea_data ldata = { NULL };
3073 struct lu_buf *buf = &info->mti_link_buf;
3079 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3080 if (buf->lb_buf == NULL)
3084 rc = mdd_links_read(env, child, &ldata);
3086 if (rc == -ENOENT || rc == -ENODATA)
3091 LASSERT(ldata.ld_leh != NULL);
3092 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3093 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3094 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3095 struct lu_name lname;
3098 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3101 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3102 !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3103 ldata.ld_lee = (struct link_ea_entry *)
3104 ((char *)ldata.ld_lee +
3109 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3110 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3111 lname.ln_namelen, lname.ln_name,
3112 PFID(mdd_object_fid(newparent)));
3113 /* update to the new parent fid */
3114 linkea_entry_pack(ldata.ld_lee, &lname,
3115 mdd_object_fid(newparent));
3117 rc = mdd_declare_links_add(env, child, handle, &ldata,
3120 rc = mdd_links_write(env, child, &ldata, handle);
3126 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3127 struct mdd_object *parent,
3128 struct mdd_object *newparent,
3129 struct mdd_object *child,
3130 const char *name, int namelen,
3131 struct thandle *handle)
3133 return mdd_linkea_update_child_internal(env, parent, newparent,
3135 namelen, handle, true);
3138 static int mdd_linkea_update_child(const struct lu_env *env,
3139 struct mdd_object *parent,
3140 struct mdd_object *newparent,
3141 struct mdd_object *child,
3142 const char *name, int namelen,
3143 struct thandle *handle)
3145 return mdd_linkea_update_child_internal(env, parent, newparent,
3147 namelen, handle, false);
3150 static int mdd_update_linkea_internal(const struct lu_env *env,
3151 struct mdd_object *mdd_pobj,
3152 struct mdd_object *mdd_sobj,
3153 struct mdd_object *mdd_tobj,
3154 const struct lu_name *child_name,
3155 struct linkea_data *ldata,
3156 struct thandle *handle,
3159 struct mdd_thread_info *info = mdd_env_info(env);
3164 LASSERT(ldata->ld_buf != NULL);
3167 /* If it is mulitple links file, we need update the name entry for
3169 LASSERT(ldata->ld_leh != NULL);
3170 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3171 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3172 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3173 struct mdd_object *pobj;
3174 struct lu_name lname;
3177 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3179 pobj = mdd_object_find(env, mdd, &fid);
3181 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3182 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3184 linkea_del_buf(ldata, &lname);
3188 if (!mdd_object_exists(pobj)) {
3189 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3190 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3191 linkea_del_buf(ldata, &lname);
3192 mdd_object_put(env, pobj);
3196 if (pobj == mdd_pobj &&
3197 lname.ln_namelen == child_name->ln_namelen &&
3198 strncmp(lname.ln_name, child_name->ln_name,
3199 lname.ln_namelen) == 0) {
3200 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3201 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3203 linkea_del_buf(ldata, &lname);
3204 mdd_object_put(env, pobj);
3208 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3209 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3210 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3213 /* Remove source name from source directory */
3214 /* Insert new fid with target name into target dir */
3215 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3220 rc = mdo_declare_index_insert(env, pobj,
3221 mdd_object_fid(mdd_tobj),
3222 mdd_object_type(mdd_tobj),
3223 lname.ln_name, handle);
3227 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3231 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3235 char *tmp_name = info->mti_key;
3237 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3238 /* lnamelen is too big(> NAME_MAX + 16),
3239 * something wrong about this linkea, let's
3241 linkea_del_buf(ldata, &lname);
3242 mdd_object_put(env, pobj);
3246 /* Note: lname might be without \0 at the end, see
3247 * linkea_entry_unpack(), let's add extra \0 by
3249 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3250 lname.ln_namelen, lname.ln_name);
3251 lname.ln_name = tmp_name;
3253 /* Let's check if this linkEA still valid, before
3254 * it might be packed into the RPC buffer. */
3255 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3256 &info->mti_fid, NULL);
3258 !lu_fid_eq(&info->mti_fid,
3259 mdd_object_fid(mdd_sobj))) {
3260 /* skip invalid linkea entry */
3261 linkea_del_buf(ldata, &lname);
3262 mdd_object_put(env, pobj);
3266 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3270 rc = __mdd_index_insert(env, pobj,
3271 mdd_object_fid(mdd_tobj),
3272 mdd_object_type(mdd_tobj),
3277 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3278 rc = mdo_ref_add(env, mdd_tobj, handle);
3279 mdd_write_unlock(env, mdd_tobj);
3283 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3284 mdo_ref_del(env, mdd_sobj, handle);
3285 mdd_write_unlock(env, mdd_sobj);
3288 mdd_object_put(env, pobj);
3292 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3299 static int mdd_migrate_xattrs(const struct lu_env *env,
3300 struct mdd_object *mdd_sobj,
3301 struct mdd_object *mdd_tobj)
3303 struct mdd_thread_info *info = mdd_env_info(env);
3304 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3306 struct thandle *handle;
3312 struct lu_buf list_xbuf;
3316 /* retrieve xattr list from the old object */
3317 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3318 if (list_xsize == -ENODATA)
3324 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3325 if (info->mti_big_buf.lb_buf == NULL)
3328 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3329 list_xbuf.lb_len = list_xsize;
3330 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3335 xname = list_xbuf.lb_buf;
3337 xlen = strnlen(xname, rem - 1) + 1;
3338 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3339 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3340 strcmp(XATTR_NAME_LMV, xname) == 0)
3343 /* For directory, if there are default layout, migrate here */
3344 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3345 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3348 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3349 if (xsize == -ENODATA)
3354 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3355 if (info->mti_link_buf.lb_buf == NULL)
3356 GOTO(out, rc = -ENOMEM);
3358 xbuf.lb_len = xsize;
3359 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3360 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3366 handle = mdd_trans_create(env, mdd);
3368 GOTO(out, rc = PTR_ERR(handle));
3370 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3373 GOTO(stop_trans, rc);
3374 /* Note: this transaction is part of migration, and it is not
3375 * the last step of migration, so we set th_local = 1 to avoid
3376 * update last rcvd for this transaction */
3377 handle->th_local = 1;
3378 rc = mdd_trans_start(env, mdd, handle);
3380 GOTO(stop_trans, rc);
3382 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3384 GOTO(stop_trans, rc = 0);
3387 GOTO(stop_trans, rc);
3389 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3396 memmove(xname, xname + xlen, rem);
3402 static int mdd_declare_migrate_create(const struct lu_env *env,
3403 struct mdd_object *mdd_pobj,
3404 struct mdd_object *mdd_sobj,
3405 struct mdd_object *mdd_tobj,
3406 struct md_op_spec *spec,
3408 union lmv_mds_md *mgr_ea,
3409 struct linkea_data *ldata,
3410 struct thandle *handle)
3412 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3413 const struct lu_buf *buf;
3417 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3418 handle, spec, NULL);
3422 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3427 if (S_ISLNK(la->la_mode)) {
3428 const char *target_name = spec->u.sp_symname;
3429 int sym_len = strlen(target_name);
3430 const struct lu_buf *buf;
3432 buf = mdd_buf_get_const(env, target_name, sym_len);
3433 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3437 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3438 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3444 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3445 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3446 spec->u.sp_ea.eadatalen);
3447 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3453 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3454 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3455 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3460 la_flag->la_valid = LA_FLAGS;
3461 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3462 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3467 static int mdd_migrate_create(const struct lu_env *env,
3468 struct mdd_object *mdd_pobj,
3469 struct mdd_object *mdd_sobj,
3470 struct mdd_object *mdd_tobj,
3471 const struct lu_name *lname,
3474 struct mdd_thread_info *info = mdd_env_info(env);
3475 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3476 struct md_op_spec *spec = &info->mti_spec;
3477 struct lu_buf lmm_buf = { NULL };
3478 struct lu_buf link_buf = { NULL };
3479 const struct lu_buf *buf;
3480 struct thandle *handle;
3481 struct lmv_mds_md_v1 *mgr_ea;
3482 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3483 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3485 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3489 /* prepare spec for create */
3490 memset(spec, 0, sizeof(*spec));
3491 spec->sp_cr_lookup = 0;
3492 spec->sp_feat = &dt_directory_features;
3493 if (S_ISLNK(la->la_mode)) {
3494 buf = lu_buf_check_and_alloc(
3495 &mdd_env_info(env)->mti_big_buf,
3498 link_buf.lb_len = la->la_size + 1;
3499 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3500 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3502 rc = rc != 0 ? rc : -EFAULT;
3503 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3504 mdd2obd_dev(mdd)->obd_name,
3505 PFID(mdd_object_fid(mdd_sobj)), rc);
3508 spec->u.sp_symname = link_buf.lb_buf;
3509 } else if (S_ISREG(la->la_mode)) {
3510 /* retrieve lov of the old object */
3511 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3512 if (rc != 0 && rc != -ENODATA)
3514 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3515 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3516 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3517 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3519 } else if (S_ISDIR(la->la_mode)) {
3520 rc = mdd_links_read(env, mdd_sobj, ldata);
3521 if (rc == -ENODATA) {
3522 /* ignore the non-linkEA error */
3530 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3531 memset(mgr_ea, 0, sizeof(*mgr_ea));
3532 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3533 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3534 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3535 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3536 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3537 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3539 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3541 handle = mdd_trans_create(env, mdd);
3543 GOTO(out_free, rc = PTR_ERR(handle));
3545 /* Note: this transaction is part of migration, and it is not
3546 * the last step of migration, so we set th_local = 1 to avoid
3547 * update last rcvd for this transaction */
3548 handle->th_local = 1;
3549 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3551 (union lmv_mds_md *)info->mti_xattr_buf,
3554 GOTO(stop_trans, rc);
3556 rc = mdd_trans_start(env, mdd, handle);
3558 GOTO(stop_trans, rc);
3560 /* don't set nlink from the original object */
3561 la->la_valid &= ~LA_NLINK;
3563 /* create the target object */
3564 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3567 GOTO(stop_trans, rc);
3569 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3570 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3572 GOTO(stop_trans, rc);
3575 /* Set MIGRATE EA on the source inode, so once the migration needs
3576 * to be re-done during failover, the re-do process can locate the
3577 * target object which is already being created. */
3578 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3579 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3580 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3582 GOTO(stop_trans, rc);
3584 /* Set immutable flag, so any modification is disabled until
3585 * the migration is done. Once the migration is interrupted,
3586 * if the resume process find the migrating object has both
3587 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3588 * flag and approve the migration */
3589 la_flag->la_valid = LA_FLAGS;
3590 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3591 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3593 if (handle != NULL) {
3596 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3601 if (lmm_buf.lb_buf != NULL)
3602 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3606 static int mdd_migrate_entries(const struct lu_env *env,
3607 struct mdd_object *mdd_sobj,
3608 struct mdd_object *mdd_tobj)
3610 struct dt_object *next = mdd_object_child(mdd_sobj);
3611 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3612 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3613 struct thandle *handle;
3615 const struct dt_it_ops *iops;
3618 struct lu_dirent *ent;
3621 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3625 if (!dt_try_as_dir(env, next))
3626 GOTO(out_ent, rc = -ENOTDIR);
3628 * iterate directories
3630 iops = &next->do_index_ops->dio_it;
3631 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3633 GOTO(out_ent, rc = PTR_ERR(it));
3635 rc = iops->load(env, it, 0);
3637 rc = iops->next(env, it);
3641 * At this point and across for-loop:
3643 * rc == 0 -> ok, proceed.
3644 * rc > 0 -> end of directory.
3648 struct mdd_object *child;
3649 char *name = mdd_env_info(env)->mti_key;
3653 bool target_exist = false;
3656 len = iops->key_size(env, it);
3660 result = iops->rec(env, it, (struct dt_rec *)ent,
3661 LUDA_FID | LUDA_TYPE);
3662 if (result == -ESTALE)
3669 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3670 recsize = le16_to_cpu(ent->lde_reclen);
3672 /* Insert new fid with target name into target dir */
3673 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3674 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3675 ent->lde_name[1] == '.'))
3678 child = mdd_object_find(env, mdd, &ent->lde_fid);
3680 GOTO(out, rc = PTR_ERR(child));
3682 mdd_write_lock(env, child, MOR_SRC_CHILD);
3683 is_dir = S_ISDIR(mdd_object_type(child));
3685 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3687 /* Check whether the name has been inserted to the target */
3688 if (dt_try_as_dir(env, dt_tobj)) {
3689 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3691 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3692 (struct dt_key *)name);
3693 if (unlikely(rc == 0))
3694 target_exist = true;
3697 handle = mdd_trans_create(env, mdd);
3699 GOTO(out_put, rc = PTR_ERR(handle));
3701 /* Note: this transaction is part of migration, and it is not
3702 * the last step of migration, so we set th_local = 1 to avoid
3703 * updating last rcvd for this transaction */
3704 handle->th_local = 1;
3705 if (likely(!target_exist)) {
3706 rc = mdo_declare_index_insert(env, mdd_tobj,
3708 mdd_object_type(child),
3714 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3720 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3725 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3729 /* Update .. for child */
3730 rc = mdo_declare_index_delete(env, child, dotdot,
3735 rc = mdo_declare_index_insert(env, child,
3736 mdd_object_fid(mdd_tobj),
3737 S_IFDIR, dotdot, handle);
3742 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3749 rc = mdd_trans_start(env, mdd, handle);
3751 CERROR("%s: transaction start failed: rc = %d\n",
3752 mdd2obd_dev(mdd)->obd_name, rc);
3756 if (likely(!target_exist)) {
3757 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3758 mdd_object_type(child),
3764 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3769 rc = __mdd_index_delete_only(env, child, dotdot,
3774 rc = __mdd_index_insert_only(env, child,
3775 mdd_object_fid(mdd_tobj), S_IFDIR,
3781 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3783 strlen(name), handle);
3786 mdd_write_unlock(env, child);
3787 mdd_object_put(env, child);
3788 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3795 result = iops->next(env, it);
3796 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3797 GOTO(out, rc = -EINTR);
3799 if (result == -ESTALE)
3801 } while (result == 0);
3804 iops->fini(env, it);
3806 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3810 static int mdd_declare_update_linkea(const struct lu_env *env,
3811 struct mdd_object *mdd_pobj,
3812 struct mdd_object *mdd_sobj,
3813 struct mdd_object *mdd_tobj,
3814 const struct lu_name *child_name,
3815 struct linkea_data *ldata,
3816 struct thandle *handle)
3818 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3819 child_name, ldata, handle, 1);
3822 static int mdd_update_linkea(const struct lu_env *env,
3823 struct mdd_object *mdd_pobj,
3824 struct mdd_object *mdd_sobj,
3825 struct mdd_object *mdd_tobj,
3826 const struct lu_name *child_name,
3827 struct linkea_data *ldata,
3828 struct thandle *handle)
3830 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3831 child_name, ldata, handle, 0);
3834 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3835 struct mdd_object *mdd_pobj,
3836 struct mdd_object *mdd_sobj,
3837 struct mdd_object *mdd_tobj,
3838 const struct lu_name *lname,
3840 struct lu_attr *parent_la,
3841 struct linkea_data *ldata,
3842 struct thandle *handle)
3844 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3845 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3848 /* Revert IMMUTABLE flag */
3849 la_flag->la_valid = LA_FLAGS;
3850 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3851 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3855 /* delete entry from source dir */
3856 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3860 if (ldata->ld_buf != NULL) {
3861 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3862 mdd_tobj, lname, ldata, handle);
3867 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3868 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3873 handle->th_complex = 1;
3874 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3876 LU_XATTR_REPLACE, handle);
3881 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3882 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3888 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3889 mdd_object_type(mdd_tobj),
3890 lname->ln_name, handle);
3894 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3898 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3899 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3904 /* delete old object */
3905 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3909 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3910 /* delete old object */
3911 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3914 /* set nlink to 0 */
3915 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3920 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3924 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3928 rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3933 static int mdd_migrate_update_name(const struct lu_env *env,
3934 struct mdd_object *mdd_pobj,
3935 struct mdd_object *mdd_sobj,
3936 struct mdd_object *mdd_tobj,
3937 const struct lu_name *lname,
3940 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3941 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3942 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3943 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3944 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3945 struct thandle *handle;
3946 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3947 const char *name = lname->ln_name;
3951 /* update time for parent */
3952 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3953 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3954 p_la->la_valid = LA_CTIME;
3956 rc = mdd_la_get(env, mdd_sobj, so_attr);
3960 ldata->ld_buf = NULL;
3961 rc = mdd_links_read(env, mdd_sobj, ldata);
3962 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3965 handle = mdd_trans_create(env, mdd);
3967 RETURN(PTR_ERR(handle));
3969 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3970 lname, so_attr, p_la, ldata,
3973 /* If the migration can not be fit in one transaction, just
3974 * leave it in the original MDT */
3976 GOTO(stop_trans, rc = 0);
3978 GOTO(stop_trans, rc);
3981 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3982 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3983 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3984 PFID(mdd_object_fid(mdd_tobj)));
3986 rc = mdd_trans_start(env, mdd, handle);
3988 GOTO(stop_trans, rc);
3990 /* Revert IMMUTABLE flag */
3991 la_flag->la_valid = LA_FLAGS;
3992 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3993 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3995 GOTO(stop_trans, rc);
3997 /* Remove source name from source directory */
3998 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
4000 GOTO(stop_trans, rc);
4002 if (ldata->ld_buf != NULL) {
4003 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
4004 lname, ldata, handle);
4006 GOTO(stop_trans, rc);
4008 /* linkea update might decrease the source object
4009 * nlink, let's get the attr again after ref_del */
4010 rc = mdd_la_get(env, mdd_sobj, so_attr);
4012 GOTO(stop_trans, rc);
4015 if (S_ISREG(so_attr->la_mode)) {
4016 if (so_attr->la_nlink == 1) {
4017 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4019 if (rc != 0 && rc != -ENODATA)
4020 GOTO(stop_trans, rc);
4022 rc = mdo_xattr_set(env, mdd_tobj, NULL,
4024 LU_XATTR_REPLACE, handle);
4026 GOTO(stop_trans, rc);
4030 /* Insert new fid with target name into target dir */
4031 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4032 mdd_object_type(mdd_tobj), name, handle);
4034 GOTO(stop_trans, rc);
4036 linkea_add_buf(ldata, lname, mdd_object_fid(mdd_pobj));
4037 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
4040 GOTO(stop_trans, rc);
4042 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4044 mdd_sobj->mod_flags |= DEAD_OBJ;
4045 rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4047 GOTO(out_unlock, rc);
4049 rc = __mdd_orphan_add(env, mdd_sobj, handle);
4051 GOTO(out_unlock, rc);
4053 mdo_ref_del(env, mdd_sobj, handle);
4055 mdo_ref_del(env, mdd_sobj, handle);
4057 /* Get the attr again after ref_del */
4058 rc = mdd_la_get(env, mdd_sobj, so_attr);
4060 GOTO(out_unlock, rc);
4062 ma->ma_attr = *so_attr;
4063 ma->ma_valid |= MA_INODE;
4065 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4067 GOTO(out_unlock, rc);
4069 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4070 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4071 mdo2fid(mdd_pobj), lname, lname, handle);
4073 CWARN("%s: changelog for migrate %s "DFID
4074 "under "DFID" failed: rc = %d\n",
4075 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4076 PFID(mdd_object_fid(mdd_sobj)),
4077 PFID(mdd_object_fid(mdd_pobj)), rc);
4078 /* Sigh, there are no easy way to migrate back the object, so
4079 * let's reset the result to 0 for now XXX */
4083 mdd_write_unlock(env, mdd_sobj);
4086 mdd_trans_stop(env, mdd, rc, handle);
4091 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4092 const struct lu_fid *fid, __u32 *mdt_index)
4094 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4095 struct seq_server_site *ss;
4098 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4100 range->lsr_flags = LU_SEQ_RANGE_MDT;
4101 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4105 *mdt_index = range->lsr_index;
4110 * Check whether we should migrate the file/dir
4112 * < 0 permission check failed or other error.
4113 * = 0 the file can be migrated.
4114 * > 0 the file does not need to be migrated, mostly
4115 * for multiple link file
4117 static int mdd_migrate_sanity_check(const struct lu_env *env,
4118 struct mdd_object *pobj,
4119 const struct lu_attr *pattr,
4120 struct mdd_object *sobj,
4121 struct lu_attr *sattr)
4123 struct mdd_thread_info *info = mdd_env_info(env);
4124 struct linkea_data *ldata = &info->mti_link_data;
4125 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4127 struct lu_buf *mgr_buf;
4133 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4134 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4135 if (mgr_buf->lb_buf == NULL)
4138 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4140 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4142 /* If the object has migrateEA, it means IMMUTE flag
4143 * is being set by previous migration process, so it
4144 * needs to override the IMMUTE flag, otherwise the
4145 * following sanity check will fail */
4146 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4147 LMV_HASH_FLAG_MIGRATION) {
4148 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4150 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4151 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4152 mdd2obd_dev(mdd)->obd_name,
4153 PFID(mdd_object_fid(sobj)));
4157 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4158 sobj, sattr, NULL, NULL);
4162 /* Then it will check if the file should be migrated. If the file
4163 * has mulitple links, we only need migrate the file if all of its
4164 * entries has been migrated to the remote MDT */
4165 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4168 rc = mdd_links_read(env, sobj, ldata);
4170 /* For multiple links files, if there are no linkEA data at all,
4171 * means the file might be created before linkEA is enabled, and
4172 * all of its links should not be migrated yet, otherwise it
4173 * should have some linkEA there */
4174 if (rc == -ENOENT || rc == -ENODATA)
4179 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4180 /* If there are still links locally, then the file will not be
4182 LASSERT(ldata->ld_leh != NULL);
4183 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4184 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4185 struct lu_name lname;
4187 __u32 parent_mdt_index;
4189 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4191 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4194 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4198 /* Migrate the object only if none of its parents are on the
4200 if (parent_mdt_index != mdt_index)
4203 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4204 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4205 lname.ln_name, PFID(&fid));
4213 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4214 struct md_object *sobj, const struct lu_name *lname,
4215 struct md_object *tobj, struct md_attr *ma)
4217 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4218 struct mdd_device *mdd = mdo2mdd(pobj);
4219 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4220 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4221 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4222 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4226 /* If the file will being migrated, it will check whether
4227 * the file is being opened by someone else right now */
4228 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4229 if (mdd_sobj->mod_count > 0) {
4230 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4231 mdd2obd_dev(mdd)->obd_name,
4232 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4233 mdd_sobj->mod_count, -EBUSY);
4234 mdd_read_unlock(env, mdd_sobj);
4235 GOTO(put, rc = -EBUSY);
4237 mdd_read_unlock(env, mdd_sobj);
4239 rc = mdd_la_get(env, mdd_sobj, so_attr);
4243 rc = mdd_la_get(env, mdd_pobj, pattr);
4247 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4254 /* Sigh, it is impossible to finish all of migration in a single
4255 * transaction, for example migrating big directory entries to the
4256 * new MDT, it needs insert all of name entries of children in the
4259 * So migration will be done in multiple steps and transactions.
4261 * 1. create an orphan object on the remote MDT in one transaction.
4262 * 2. migrate extend attributes to the new target file/directory.
4263 * 3. For directory, migrate the entries to the new MDT and update
4264 * linkEA of each children. Because we can not migrate all entries
4265 * in a single transaction, so the migrating directory will become
4266 * a striped directory during migration, so once the process is
4267 * interrupted, the directory is still accessible. (During lookup,
4268 * client will locate the name by searching both original and target
4270 * 4. Finally, update the name/FID to point to the new file/directory
4271 * in a separate transaction.
4274 /* step 1: Check whether the orphan object has been created, and create
4275 * orphan object on the remote MDT if needed */
4276 if (!mdd_object_exists(mdd_tobj)) {
4277 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4283 LASSERT(mdd_object_exists(mdd_tobj));
4284 /* step 2: migrate xattr */
4285 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4289 /* step 3: migrate name entries to the orphan object */
4290 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4291 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4294 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4295 OBD_FAIL_MDS_REINT_NET_REP)))
4298 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4301 LASSERT(mdd_object_exists(mdd_tobj));
4302 /* step 4: update name entry to the new object */
4303 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4311 const struct md_dir_operations mdd_dir_ops = {
4312 .mdo_is_subdir = mdd_is_subdir,
4313 .mdo_lookup = mdd_lookup,
4314 .mdo_create = mdd_create,
4315 .mdo_rename = mdd_rename,
4316 .mdo_link = mdd_link,
4317 .mdo_unlink = mdd_unlink,
4318 .mdo_create_data = mdd_create_data,
4319 .mdo_migrate = mdd_migrate,