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, 2013, 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_remote(mdd_obj)) {
89 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
90 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
91 } else if (!mdd_object_exists(mdd_obj)) {
95 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
100 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101 dt_try_as_dir(env, dir)))
102 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key,
103 mdd_object_capa(env, mdd_obj));
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, BYPASS_CAPA);
122 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
126 static inline int mdd_parent_fid(const struct lu_env *env,
127 struct mdd_object *obj,
128 const struct lu_attr *attr,
131 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
135 * For root fid use special function, which does not compare version component
136 * of fid. Version component is different for root fids on all MDTs.
138 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
140 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
141 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
145 * return 1: if lf is the fid of the ancestor of p1;
148 * return -EREMOTE: if remote object is found, in this
149 * case fid of remote object is saved to @pf;
151 * otherwise: values < 0, errors.
153 static int mdd_is_parent(const struct lu_env *env,
154 struct mdd_device *mdd,
155 struct mdd_object *p1,
156 const struct lu_attr *attr,
157 const struct lu_fid *lf,
160 struct mdd_object *parent = NULL;
165 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
166 pfid = &mdd_env_info(env)->mti_fid;
168 /* Check for root first. */
169 if (mdd_is_root(mdd, mdo2fid(p1)))
173 /* this is done recursively, bypass capa for each obj */
174 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
175 rc = mdd_parent_fid(env, p1, attr, pfid);
178 if (mdd_is_root(mdd, pfid))
180 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
182 if (lu_fid_eq(pfid, lf))
185 mdd_object_put(env, parent);
187 parent = mdd_object_find(env, mdd, pfid);
188 if (IS_ERR(parent)) {
189 GOTO(out, rc = PTR_ERR(parent));
190 } else if (mdd_object_remote(parent)) {
191 /*FIXME: Because of the restriction of rename in Phase I.
192 * If the parent is remote, we just assumed lf is not the
193 * parent of P1 for now */
200 if (parent && !IS_ERR(parent))
201 mdd_object_put(env, parent);
206 * No permission check is needed.
208 * returns 1: if fid is ancestor of @mo;
209 * returns 0: if fid is not a ancestor of @mo;
211 * returns EREMOTE if remote object is found, fid of remote object is saved to
214 * returns < 0: if error
216 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
217 const struct lu_fid *fid, struct lu_fid *sfid)
219 struct mdd_device *mdd = mdo2mdd(mo);
220 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
224 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
227 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
231 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
235 } else if (rc == 1) {
236 /* found @fid is parent */
244 * Check that @dir contains no entries except (possibly) dot and dotdot.
249 * -ENOTDIR not a directory object
250 * -ENOTEMPTY not empty
254 static int mdd_dir_is_empty(const struct lu_env *env,
255 struct mdd_object *dir)
258 struct dt_object *obj;
259 const struct dt_it_ops *iops;
263 obj = mdd_object_child(dir);
264 if (!dt_try_as_dir(env, obj))
267 iops = &obj->do_index_ops->dio_it;
268 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
270 result = iops->get(env, it, (const struct dt_key *)"");
273 for (result = 0, i = 0; result == 0 && i < 3; ++i)
274 result = iops->next(env, it);
277 else if (result == 1)
279 } else if (result == 0)
281 * Huh? Index contains no zero key?
288 result = PTR_ERR(it);
293 * Determine if the target object can be hard linked, and right now it only
294 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
295 * directory nlink count might exceed the maximum link count(see
296 * osd_object_ref_add), so it only check nlink for non-directories.
298 * \param[in] env thread environment
299 * \param[in] obj object being linked to
300 * \param[in] la attributes of \a obj
302 * \retval 0 if \a obj can be hard linked
303 * \retval negative error if \a obj is a directory or has too
306 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
307 const struct lu_attr *la)
309 struct mdd_device *m = mdd_obj2mdd_dev(obj);
314 /* Subdir count limitation can be broken through
315 * (see osd_object_ref_add), so only check non-directory here. */
316 if (!S_ISDIR(la->la_mode) &&
317 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
324 * Check whether it may create the cobj under the pobj.
326 * \param[in] env execution environment
327 * \param[in] pobj the parent directory
328 * \param[in] pattr the attribute of the parent directory
329 * \param[in] cobj the child to be created
330 * \param[in] check_perm if check WRITE|EXEC permission for parent
332 * \retval = 0 create the child under this dir is allowed
333 * \retval negative errno create the child under this dir is
336 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
337 const struct lu_attr *pattr, struct mdd_object *cobj,
340 struct mdd_thread_info *info = mdd_env_info(env);
345 if (cobj && mdd_object_exists(cobj))
348 if (mdd_is_dead_obj(pobj))
351 /* If the parent is a sub-stripe, check whether it is dead */
352 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
353 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV,
354 mdd_object_capa(env, pobj));
355 if (unlikely(rc > 0)) {
356 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
358 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
359 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
365 rc = mdd_permission_internal_locked(env, pobj, pattr,
366 MAY_WRITE | MAY_EXEC,
372 * Check whether can unlink from the pobj in the case of "cobj == NULL".
374 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
375 const struct lu_attr *pattr, const struct lu_attr *attr)
380 if (mdd_is_dead_obj(pobj))
383 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
386 rc = mdd_permission_internal_locked(env, pobj, pattr,
387 MAY_WRITE | MAY_EXEC,
392 if (pattr->la_flags & LUSTRE_APPEND_FL)
399 * pobj == NULL is remote ops case, under such case, pobj's
400 * VTX feature has been checked already, no need check again.
402 static inline int mdd_is_sticky(const struct lu_env *env,
403 struct mdd_object *pobj,
404 const struct lu_attr *pattr,
405 struct mdd_object *cobj,
406 const struct lu_attr *cattr)
408 struct lu_ucred *uc = lu_ucred_assert(env);
411 LASSERT(pattr != NULL);
412 if (!(pattr->la_mode & S_ISVTX) ||
413 (pattr->la_uid == uc->uc_fsuid))
417 LASSERT(cattr != NULL);
418 if (cattr->la_uid == uc->uc_fsuid)
421 return !md_capable(uc, CFS_CAP_FOWNER);
424 static int mdd_may_delete_entry(const struct lu_env *env,
425 struct mdd_object *pobj,
426 const struct lu_attr *pattr,
431 LASSERT(pobj != NULL);
432 if (!mdd_object_exists(pobj))
435 if (mdd_is_dead_obj(pobj))
440 rc = mdd_permission_internal_locked(env, pobj, pattr,
441 MAY_WRITE | MAY_EXEC,
447 if (pattr->la_flags & LUSTRE_APPEND_FL)
454 * Check whether it may delete the cobj from the pobj.
457 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
458 const struct lu_attr *tpattr, struct mdd_object *tobj,
459 const struct lu_attr *tattr, const struct lu_attr *cattr,
460 int check_perm, int check_empty)
466 LASSERT(tpattr != NULL);
467 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
475 if (!mdd_object_exists(tobj))
478 if (mdd_is_dead_obj(tobj))
481 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
484 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
487 /* additional check the rename case */
489 if (S_ISDIR(cattr->la_mode)) {
490 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
492 if (!S_ISDIR(tattr->la_mode))
495 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
497 } else if (S_ISDIR(tattr->la_mode))
501 if (S_ISDIR(tattr->la_mode) && check_empty)
502 rc = mdd_dir_is_empty(env, tobj);
508 * Check whether it can create the link file(linked to @src_obj) under
509 * the target directory(@tgt_obj), and src_obj has been locked by
512 * \param[in] env execution environment
513 * \param[in] tgt_obj the target directory
514 * \param[in] tattr attributes of target directory
515 * \param[in] lname the link name
516 * \param[in] src_obj source object for link
517 * \param[in] cattr attributes for source object
519 * \retval = 0 it is allowed to create the link file under tgt_obj
520 * \retval negative error not allowed to create the link file
522 static int mdd_link_sanity_check(const struct lu_env *env,
523 struct mdd_object *tgt_obj,
524 const struct lu_attr *tattr,
525 const struct lu_name *lname,
526 struct mdd_object *src_obj,
527 const struct lu_attr *cattr)
529 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
533 if (!mdd_object_exists(src_obj))
536 if (mdd_is_dead_obj(src_obj))
539 /* Local ops, no lookup before link, check filename length here. */
540 rc = mdd_name_check(m, lname);
544 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
547 if (S_ISDIR(mdd_object_type(src_obj)))
550 LASSERT(src_obj != tgt_obj);
551 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
555 rc = __mdd_may_link(env, src_obj, cattr);
560 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
561 const char *name, struct thandle *handle,
562 struct lustre_capa *capa)
564 struct dt_object *next = mdd_object_child(pobj);
568 if (dt_try_as_dir(env, next))
569 rc = dt_delete(env, next, (struct dt_key *)name, handle, capa);
576 static int __mdd_index_insert_only(const struct lu_env *env,
577 struct mdd_object *pobj,
578 const struct lu_fid *lf, __u32 type,
580 struct thandle *handle,
581 struct lustre_capa *capa)
583 struct dt_object *next = mdd_object_child(pobj);
587 if (dt_try_as_dir(env, next)) {
588 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
589 struct lu_ucred *uc = lu_ucred_check(env);
593 rec->rec_type = type;
594 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
595 rc = dt_insert(env, next, (const struct dt_rec *)rec,
596 (const struct dt_key *)name, handle, capa,
604 /* insert named index, add reference if isdir */
605 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
606 const struct lu_fid *lf, __u32 type,
607 const char *name, struct thandle *handle,
608 struct lustre_capa *capa)
613 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle, capa);
614 if (rc == 0 && S_ISDIR(type)) {
615 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
616 mdo_ref_add(env, pobj, handle);
617 mdd_write_unlock(env, pobj);
623 /* delete named index, drop reference if isdir */
624 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
625 const char *name, int is_dir, struct thandle *handle,
626 struct lustre_capa *capa)
631 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
632 if (rc == 0 && is_dir) {
633 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
634 mdo_ref_del(env, pobj, handle);
635 mdd_write_unlock(env, pobj);
641 static int mdd_llog_record_calc_size(const struct lu_env *env,
642 const struct lu_name *tname,
643 const struct lu_name *sname)
645 const struct lu_ucred *uc = lu_ucred(env);
646 enum changelog_rec_flags crf = 0;
647 size_t hdr_size = sizeof(struct llog_changelog_rec) -
648 sizeof(struct changelog_rec);
653 if (uc != NULL && uc->uc_jobid[0] != '\0')
656 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
657 (tname != NULL ? tname->ln_namelen : 0) +
658 (sname != NULL ? 1 + sname->ln_namelen : 0));
661 int mdd_declare_changelog_store(const struct lu_env *env,
662 struct mdd_device *mdd,
663 const struct lu_name *tname,
664 const struct lu_name *sname,
665 struct thandle *handle)
667 struct obd_device *obd = mdd2obd_dev(mdd);
668 struct llog_ctxt *ctxt;
669 struct llog_changelog_rec *rec;
675 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
678 reclen = mdd_llog_record_calc_size(env, tname, sname);
679 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
680 if (buf->lb_buf == NULL)
684 rec->cr_hdr.lrh_len = reclen;
685 rec->cr_hdr.lrh_type = CHANGELOG_REC;
687 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
691 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
697 /** Add a changelog entry \a rec to the changelog llog
700 * \param handle - currently ignored since llogs start their own transaction;
701 * this will hopefully be fixed in llog rewrite
704 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
705 struct llog_changelog_rec *rec, struct thandle *th)
707 struct obd_device *obd = mdd2obd_dev(mdd);
708 struct llog_ctxt *ctxt;
711 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
712 changelog_rec_varsize(&rec->cr));
714 /* llog_lvfs_write_rec sets the llog tail len */
715 rec->cr_hdr.lrh_type = CHANGELOG_REC;
716 rec->cr.cr_time = cl_time();
718 spin_lock(&mdd->mdd_cl.mc_lock);
719 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
720 * but as long as the MDD transactions are ordered correctly for e.g.
721 * rename conflicts, I don't think this should matter. */
722 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
723 spin_unlock(&mdd->mdd_cl.mc_lock);
725 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
729 /* nested journal transaction */
730 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
738 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
739 const struct lu_fid *sfid,
740 const struct lu_fid *spfid,
741 const struct lu_name *sname)
743 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
744 size_t extsize = sname->ln_namelen + 1;
746 LASSERT(sfid != NULL);
747 LASSERT(spfid != NULL);
748 LASSERT(sname != NULL);
750 rnm->cr_sfid = *sfid;
751 rnm->cr_spfid = *spfid;
753 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
754 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
755 rec->cr_namelen += extsize;
758 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
760 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
762 if (jobid == NULL || jobid[0] == '\0')
765 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
768 /** Store a namespace change changelog record
769 * If this fails, we must fail the whole transaction; we don't
770 * want the change to commit without the log entry.
771 * \param target - mdd_object of change
772 * \param tpfid - target parent dir/object fid
773 * \param sfid - source object fid
774 * \param spfid - source parent fid
775 * \param tname - target name string
776 * \param sname - source name string
777 * \param handle - transacion handle
779 int mdd_changelog_ns_store(const struct lu_env *env,
780 struct mdd_device *mdd,
781 enum changelog_rec_type type,
782 enum changelog_rec_flags crf,
783 struct mdd_object *target,
784 const struct lu_fid *tpfid,
785 const struct lu_fid *sfid,
786 const struct lu_fid *spfid,
787 const struct lu_name *tname,
788 const struct lu_name *sname,
789 struct thandle *handle)
791 const struct lu_ucred *uc = lu_ucred(env);
792 struct llog_changelog_rec *rec;
799 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
802 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
805 LASSERT(tpfid != NULL);
806 LASSERT(tname != NULL);
807 LASSERT(handle != NULL);
809 reclen = mdd_llog_record_calc_size(env, tname, sname);
810 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
811 if (buf->lb_buf == NULL)
815 crf = (crf & CLF_FLAGMASK);
817 if (uc->uc_jobid[0] != '\0')
825 rec->cr.cr_flags = crf;
826 rec->cr.cr_type = (__u32)type;
827 rec->cr.cr_pfid = *tpfid;
828 rec->cr.cr_namelen = tname->ln_namelen;
829 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
831 if (crf & CLF_RENAME)
832 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
835 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
837 if (likely(target != NULL)) {
838 rec->cr.cr_tfid = *mdo2fid(target);
839 target->mod_cltime = cfs_time_current_64();
841 fid_zero(&rec->cr.cr_tfid);
844 rc = mdd_changelog_store(env, mdd, rec, handle);
846 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
847 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
854 static int __mdd_links_add(const struct lu_env *env,
855 struct mdd_object *mdd_obj,
856 struct linkea_data *ldata,
857 const struct lu_name *lname,
858 const struct lu_fid *pfid,
859 int first, int check)
863 if (ldata->ld_leh == NULL) {
864 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
868 rc = linkea_data_new(ldata,
869 &mdd_env_info(env)->mti_link_buf);
876 rc = linkea_links_find(ldata, lname, pfid);
877 if (rc && rc != -ENOENT)
883 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
884 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
888 linkea_add_buf(ldata, lname, tfid);
891 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
892 linkea_add_buf(ldata, lname, pfid);
894 return linkea_add_buf(ldata, lname, pfid);
897 static int __mdd_links_del(const struct lu_env *env,
898 struct mdd_object *mdd_obj,
899 struct linkea_data *ldata,
900 const struct lu_name *lname,
901 const struct lu_fid *pfid)
905 if (ldata->ld_leh == NULL) {
906 rc = mdd_links_read(env, mdd_obj, ldata);
911 rc = linkea_links_find(ldata, lname, pfid);
915 linkea_del_buf(ldata, lname);
919 static int mdd_linkea_prepare(const struct lu_env *env,
920 struct mdd_object *mdd_obj,
921 const struct lu_fid *oldpfid,
922 const struct lu_name *oldlname,
923 const struct lu_fid *newpfid,
924 const struct lu_name *newlname,
925 int first, int check,
926 struct linkea_data *ldata)
932 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
935 LASSERT(oldpfid != NULL || newpfid != NULL);
937 if (mdd_obj->mod_flags & DEAD_OBJ) {
938 /* Prevent linkea to be updated which is NOT necessary. */
939 ldata->ld_reclen = 0;
940 /* No more links, don't bother */
944 if (oldpfid != NULL) {
945 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
948 (rc != -ENODATA && rc != -ENOENT))
950 /* No changes done. */
955 /* If renaming, add the new record */
956 if (newpfid != NULL) {
957 /* even if the add fails, we still delete the out-of-date
959 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
963 rc = rc != 0 ? rc : rc2;
968 int mdd_links_rename(const struct lu_env *env,
969 struct mdd_object *mdd_obj,
970 const struct lu_fid *oldpfid,
971 const struct lu_name *oldlname,
972 const struct lu_fid *newpfid,
973 const struct lu_name *newlname,
974 struct thandle *handle,
975 struct linkea_data *ldata,
976 int first, int check)
982 ldata = &mdd_env_info(env)->mti_link_data;
983 memset(ldata, 0, sizeof(*ldata));
984 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
985 newpfid, newlname, first, check,
991 if (ldata->ld_reclen != 0)
992 rc = mdd_links_write(env, mdd_obj, ldata, handle);
997 if (rc == -EOVERFLOW || rc == -ENOSPC)
1000 CDEBUG(error ? D_ERROR : D_OTHER,
1001 "link_ea add '%.*s' failed %d "DFID"\n",
1002 newlname->ln_namelen, newlname->ln_name,
1003 rc, PFID(mdd_object_fid(mdd_obj)));
1004 else if (newpfid == NULL)
1005 CDEBUG(error ? D_ERROR : D_OTHER,
1006 "link_ea del '%.*s' failed %d "DFID"\n",
1007 oldlname->ln_namelen, oldlname->ln_name,
1008 rc, PFID(mdd_object_fid(mdd_obj)));
1010 CDEBUG(error ? D_ERROR : D_OTHER,
1011 "link_ea rename '%.*s'->'%.*s' failed %d "
1013 oldlname->ln_namelen, oldlname->ln_name,
1014 newlname->ln_namelen, newlname->ln_name,
1015 rc, PFID(mdd_object_fid(mdd_obj)));
1018 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1019 /* if we vmalloced a large buffer drop it */
1020 lu_buf_free(ldata->ld_buf);
1025 static inline int mdd_links_add(const struct lu_env *env,
1026 struct mdd_object *mdd_obj,
1027 const struct lu_fid *pfid,
1028 const struct lu_name *lname,
1029 struct thandle *handle,
1030 struct linkea_data *ldata, int first)
1032 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1033 pfid, lname, handle, ldata, first, 0);
1036 static inline int mdd_links_del(const struct lu_env *env,
1037 struct mdd_object *mdd_obj,
1038 const struct lu_fid *pfid,
1039 const struct lu_name *lname,
1040 struct thandle *handle)
1042 return mdd_links_rename(env, mdd_obj, pfid, lname,
1043 NULL, NULL, handle, NULL, 0, 0);
1046 /** Read the link EA into a temp buffer.
1047 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1048 * A pointer to the buffer is stored in \a ldata::ld_buf.
1050 * \retval 0 or error
1052 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1053 struct linkea_data *ldata)
1057 if (!mdd_object_exists(mdd_obj))
1060 /* First try a small buf */
1061 LASSERT(env != NULL);
1062 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1064 if (ldata->ld_buf->lb_buf == NULL)
1067 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1069 if (rc == -ERANGE) {
1070 /* Buf was too small, figure out what we need. */
1071 lu_buf_free(ldata->ld_buf);
1072 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1073 XATTR_NAME_LINK, BYPASS_CAPA);
1076 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1077 if (ldata->ld_buf->lb_buf == NULL)
1079 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1080 XATTR_NAME_LINK, BYPASS_CAPA);
1083 lu_buf_free(ldata->ld_buf);
1084 ldata->ld_buf = NULL;
1088 return linkea_init(ldata);
1091 /** Read the link EA into a temp buffer.
1092 * Uses the name_buf since it is generally large.
1093 * \retval IS_ERR err
1094 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1096 struct lu_buf *mdd_links_get(const struct lu_env *env,
1097 struct mdd_object *mdd_obj)
1099 struct linkea_data ldata = { 0 };
1102 rc = mdd_links_read(env, mdd_obj, &ldata);
1103 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1106 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1107 struct linkea_data *ldata, struct thandle *handle)
1109 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1110 ldata->ld_leh->leh_len);
1113 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1116 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1117 mdd_object_capa(env, mdd_obj));
1118 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1119 mdd_object_remote(mdd_obj) == 0) {
1120 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1122 /* XXX: If the linkEA is overflow, then we need to notify the
1123 * namespace LFSCK to skip "nlink" attribute verification
1124 * on this object to avoid the "nlink" to be shrinked by
1125 * wrong. It may be not good an interaction with LFSCK
1126 * like this. We will consider to replace it with other
1127 * mechanism in future. LU-5802. */
1128 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1129 LFSCK_TYPE_NAMESPACE);
1130 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1137 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1138 struct thandle *handle, struct linkea_data *ldata,
1139 enum mdd_links_add_overflow overflow)
1145 if (ldata != NULL && ldata->ld_leh != NULL) {
1146 ea_len = ldata->ld_leh->leh_len;
1147 linkea = ldata->ld_buf->lb_buf;
1149 ea_len = DEFAULT_LINKEA_SIZE;
1153 /* XXX: max size? */
1154 rc = mdo_declare_xattr_set(env, mdd_obj,
1155 mdd_buf_get_const(env, linkea, ea_len),
1156 XATTR_NAME_LINK, 0, handle);
1160 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1161 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1163 /* XXX: If the linkEA is overflow, then we need to notify the
1164 * namespace LFSCK to skip "nlink" attribute verification
1165 * on this object to avoid the "nlink" to be shrinked by
1166 * wrong. It may be not good an interaction with LFSCK
1167 * like this. We will consider to replace it with other
1168 * mechanism in future. LU-5802. */
1169 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1170 LFSCK_TYPE_NAMESPACE);
1171 rc = lfsck_in_notify(env,
1172 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1179 static inline int mdd_declare_links_del(const struct lu_env *env,
1180 struct mdd_object *c,
1181 struct thandle *handle)
1185 /* For directory, the linkEA will be removed together
1186 * with the object. */
1187 if (!S_ISDIR(mdd_object_type(c)))
1188 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1193 static int mdd_declare_link(const struct lu_env *env,
1194 struct mdd_device *mdd,
1195 struct mdd_object *p,
1196 struct mdd_object *c,
1197 const struct lu_name *name,
1198 struct thandle *handle,
1200 struct linkea_data *data)
1204 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1205 name->ln_name, handle);
1209 rc = mdo_declare_ref_add(env, c, handle);
1213 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1214 rc = mdo_declare_ref_add(env, c, handle);
1219 la->la_valid = LA_CTIME | LA_MTIME;
1220 rc = mdo_declare_attr_set(env, p, la, handle);
1224 la->la_valid = LA_CTIME;
1225 rc = mdo_declare_attr_set(env, c, la, handle);
1229 rc = mdd_declare_links_add(env, c, handle, data,
1230 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1234 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1239 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1240 struct md_object *src_obj, const struct lu_name *lname,
1243 const char *name = lname->ln_name;
1244 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1245 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1246 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1247 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1248 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1249 struct mdd_device *mdd = mdo2mdd(src_obj);
1250 struct thandle *handle;
1251 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1255 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1259 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1263 handle = mdd_trans_create(env, mdd);
1265 GOTO(out_pending, rc = PTR_ERR(handle));
1267 memset(ldata, 0, sizeof(*ldata));
1269 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1270 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1272 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1277 rc = mdd_trans_start(env, mdd, handle);
1281 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1282 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1285 GOTO(out_unlock, rc);
1287 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1288 rc = mdo_ref_add(env, mdd_sobj, handle);
1290 GOTO(out_unlock, rc);
1293 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1294 rc = mdo_ref_add(env, mdd_sobj, handle);
1296 GOTO(out_unlock, rc);
1299 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1300 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1303 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1304 mdd_object_type(mdd_sobj),
1306 mdd_object_capa(env, mdd_tobj));
1308 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1309 mdd_object_type(mdd_sobj),
1311 mdd_object_capa(env, mdd_tobj));
1315 mdo_ref_del(env, mdd_sobj, handle);
1316 GOTO(out_unlock, rc);
1319 la->la_valid = LA_CTIME | LA_MTIME;
1320 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1322 GOTO(out_unlock, rc);
1324 la->la_valid = LA_CTIME;
1325 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1327 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1328 mdo2fid(mdd_tobj), lname, 0, 0,
1331 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1332 lname, handle, ldata, 0);
1333 /* The failure of links_add should not cause the link
1334 * failure, reset rc here */
1339 mdd_write_unlock(env, mdd_sobj);
1341 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1342 mdo2fid(mdd_tobj), NULL, NULL,
1343 lname, NULL, handle);
1345 mdd_trans_stop(env, mdd, rc, handle);
1347 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1348 /* if we vmalloced a large buffer drop it */
1349 lu_buf_free(ldata->ld_buf);
1354 static int mdd_mark_dead_object(const struct lu_env *env,
1355 struct mdd_object *obj, struct thandle *handle,
1358 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1362 obj->mod_flags |= DEAD_OBJ;
1364 if (!S_ISDIR(mdd_object_type(obj)))
1367 attr->la_valid = LA_FLAGS;
1368 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1371 rc = mdo_declare_attr_set(env, obj, attr, handle);
1373 rc = mdo_attr_set(env, obj, attr, handle,
1374 mdd_object_capa(env, obj));
1379 static int mdd_declare_finish_unlink(const struct lu_env *env,
1380 struct mdd_object *obj,
1381 struct thandle *handle)
1385 rc = mdd_mark_dead_object(env, obj, handle, true);
1389 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1393 rc = mdo_declare_destroy(env, obj, handle);
1397 return mdd_declare_links_del(env, obj, handle);
1400 /* caller should take a lock before calling */
1401 int mdd_finish_unlink(const struct lu_env *env,
1402 struct mdd_object *obj, struct md_attr *ma,
1403 const struct mdd_object *pobj,
1404 const struct lu_name *lname,
1408 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1411 LASSERT(mdd_write_locked(env, obj) != 0);
1413 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1414 rc = mdd_mark_dead_object(env, obj, th, false);
1418 /* add new orphan and the object
1419 * will be deleted during mdd_close() */
1420 if (obj->mod_count) {
1421 rc = __mdd_orphan_add(env, obj, th);
1423 CDEBUG(D_HA, "Object "DFID" is inserted into "
1424 "orphan list, open count = %d\n",
1425 PFID(mdd_object_fid(obj)),
1428 CERROR("Object "DFID" fail to be an orphan, "
1429 "open count = %d, maybe cause failed "
1431 PFID(mdd_object_fid(obj)),
1434 rc = mdo_destroy(env, obj, th);
1436 } else if (!is_dir) {
1437 /* old files may not have link ea; ignore errors */
1438 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1446 * has mdd_write_lock on cobj already, but not on pobj yet
1448 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1449 const struct lu_attr *pattr,
1450 struct mdd_object *cobj,
1451 const struct lu_attr *cattr)
1456 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1461 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1462 struct mdd_object *p, struct mdd_object *c,
1463 const struct lu_name *name, struct md_attr *ma,
1464 struct thandle *handle, int no_name, int is_dir)
1466 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1469 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1470 if (likely(no_name == 0)) {
1471 rc = mdo_declare_index_delete(env, p, name->ln_name,
1478 rc = mdo_declare_ref_del(env, p, handle);
1484 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1485 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1486 la->la_valid = LA_CTIME | LA_MTIME;
1487 rc = mdo_declare_attr_set(env, p, la, handle);
1492 rc = mdo_declare_ref_del(env, c, handle);
1496 rc = mdo_declare_ref_del(env, c, handle);
1500 la->la_valid = LA_CTIME;
1501 rc = mdo_declare_attr_set(env, c, la, handle);
1505 rc = mdd_declare_finish_unlink(env, c, handle);
1509 /* FIXME: need changelog for remove entry */
1510 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1517 * test if a file has an HSM archive
1518 * if HSM attributes are not found in ma update them from
1521 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1522 struct mdd_object *obj,
1527 if (!(ma->ma_valid & MA_HSM)) {
1528 /* no HSM MD provided, read xattr */
1529 struct lu_buf *hsm_buf;
1530 const size_t buflen = sizeof(struct hsm_attrs);
1533 hsm_buf = mdd_buf_get(env, NULL, 0);
1534 lu_buf_alloc(hsm_buf, buflen);
1535 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1536 mdd_object_capa(env, obj));
1537 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1538 lu_buf_free(hsm_buf);
1542 ma->ma_valid = MA_HSM;
1544 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1550 * Delete name entry and the object.
1551 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1552 * does not exist for this object, and it could only happen during resending
1553 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1554 * is also needed in this case(needed by changelog), so we have to add another
1555 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1556 * the ENOENT failure should be able to be fixed by redo mechanism.
1558 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1559 struct md_object *cobj, const struct lu_name *lname,
1560 struct md_attr *ma, int no_name)
1562 const char *name = lname->ln_name;
1563 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1564 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1565 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1566 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1567 struct mdd_object *mdd_cobj = NULL;
1568 struct mdd_device *mdd = mdo2mdd(pobj);
1569 struct thandle *handle;
1573 /* cobj == NULL means only delete name entry */
1574 if (likely(cobj != NULL)) {
1575 mdd_cobj = md2mdd_obj(cobj);
1576 if (mdd_object_exists(mdd_cobj) == 0)
1580 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1584 if (likely(mdd_cobj != NULL)) {
1586 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1590 is_dir = S_ISDIR(cattr->la_mode);
1593 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1597 handle = mdd_trans_create(env, mdd);
1599 RETURN(PTR_ERR(handle));
1601 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1602 lname, ma, handle, no_name, is_dir);
1606 rc = mdd_trans_start(env, mdd, handle);
1610 if (likely(mdd_cobj != NULL))
1611 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1613 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1614 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1615 mdd_object_capa(env, mdd_pobj));
1620 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1621 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1622 GOTO(cleanup, rc = 0);
1624 if (likely(mdd_cobj != NULL)) {
1625 rc = mdo_ref_del(env, mdd_cobj, handle);
1627 __mdd_index_insert_only(env, mdd_pobj,
1629 mdd_object_type(mdd_cobj),
1631 mdd_object_capa(env, mdd_pobj));
1637 mdo_ref_del(env, mdd_cobj, handle);
1639 /* fetch updated nlink */
1640 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1645 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1646 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1648 la->la_valid = LA_CTIME | LA_MTIME;
1649 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1653 /* Enough for only unlink the entry */
1654 if (unlikely(mdd_cobj == NULL))
1657 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1658 /* update ctime of an unlinked file only if it is still
1659 * opened or a link still exists */
1660 la->la_valid = LA_CTIME;
1661 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1666 /* XXX: this transfer to ma will be removed with LOD/OSP */
1667 ma->ma_attr = *cattr;
1668 ma->ma_valid |= MA_INODE;
1669 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1671 /* fetch updated nlink */
1673 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1675 /* if object is removed then we can't get its attrs, use last get */
1676 if (cattr->la_nlink == 0) {
1677 ma->ma_attr = *cattr;
1678 ma->ma_valid |= MA_INODE;
1682 if (likely(mdd_cobj != NULL))
1683 mdd_write_unlock(env, mdd_cobj);
1688 if (cattr->la_nlink == 0) {
1689 cl_flags |= CLF_UNLINK_LAST;
1690 /* search for an existing archive */
1691 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1692 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1695 rc = mdd_changelog_ns_store(env, mdd,
1696 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1697 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1702 mdd_trans_stop(env, mdd, rc, handle);
1708 * The permission has been checked when obj created, no need check again.
1710 static int mdd_cd_sanity_check(const struct lu_env *env,
1711 struct mdd_object *obj)
1716 if (!obj || mdd_is_dead_obj(obj))
1722 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1723 struct md_object *cobj, const struct md_op_spec *spec,
1726 struct mdd_device *mdd = mdo2mdd(cobj);
1727 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1728 struct mdd_object *son = md2mdd_obj(cobj);
1729 struct thandle *handle;
1730 const struct lu_buf *buf;
1731 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1732 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1736 rc = mdd_cd_sanity_check(env, son);
1740 if (!md_should_create(spec->sp_cr_flags))
1744 * there are following use cases for this function:
1745 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1746 * striping can be specified or not
1749 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1753 /* calling ->ah_make_hint() is used to transfer information from parent */
1754 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1756 handle = mdd_trans_create(env, mdd);
1758 GOTO(out_free, rc = PTR_ERR(handle));
1761 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1762 * Should this be fixed?
1764 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1765 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1766 spec->sp_cr_flags, spec->no_create);
1768 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1769 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1770 spec->u.sp_ea.eadatalen);
1775 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1776 XATTR_NAME_LOV, 0, handle);
1780 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1784 rc = mdd_trans_start(env, mdd, handle);
1788 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1789 0, handle, mdd_object_capa(env, son));
1794 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1797 mdd_trans_stop(env, mdd, rc, handle);
1802 static int mdd_declare_object_initialize(const struct lu_env *env,
1803 struct mdd_object *parent,
1804 struct mdd_object *child,
1805 struct lu_attr *attr,
1806 struct thandle *handle)
1812 * inode mode has been set in creation time, and it's based on umask,
1813 * la_mode and acl, don't set here again! (which will go wrong
1814 * because below function doesn't consider umask).
1815 * I'd suggest set all object attributes in creation time, see above.
1817 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1818 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1819 rc = mdo_declare_attr_set(env, child, attr, handle);
1820 attr->la_valid |= LA_MODE | LA_TYPE;
1821 if (rc != 0 || !S_ISDIR(attr->la_mode))
1824 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1829 rc = mdo_declare_ref_add(env, child, handle);
1833 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1839 static int mdd_object_initialize(const struct lu_env *env,
1840 const struct lu_fid *pfid,
1841 struct mdd_object *child,
1842 struct lu_attr *attr, struct thandle *handle,
1843 const struct md_op_spec *spec)
1849 * Update attributes for child.
1852 * (1) the valid bits should be converted between Lustre and Linux;
1853 * (2) maybe, the child attributes should be set in OSD when creation.
1856 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1857 /* arguments are supposed to stay the same */
1858 if (S_ISDIR(attr->la_mode)) {
1859 /* Add "." and ".." for newly created dir */
1860 mdo_ref_add(env, child, handle);
1861 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1862 S_IFDIR, dot, handle, BYPASS_CAPA);
1864 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1868 mdo_ref_del(env, child, handle);
1875 * This function checks whether it can create a file/dir under the
1876 * directory(@pobj). The directory(@pobj) is not being locked by
1879 * \param[in] env execution environment
1880 * \param[in] pobj the directory to create files
1881 * \param[in] pattr the attributes of the directory
1882 * \param[in] lname the name of the created file/dir
1883 * \param[in] cattr the attributes of the file/dir
1884 * \param[in] spec create specification
1886 * \retval = 0 it is allowed to create file/dir under
1888 * \retval negative error not allowed to create file/dir
1889 * under the directory
1891 static int mdd_create_sanity_check(const struct lu_env *env,
1892 struct md_object *pobj,
1893 const struct lu_attr *pattr,
1894 const struct lu_name *lname,
1895 struct lu_attr *cattr,
1896 struct md_op_spec *spec)
1898 struct mdd_thread_info *info = mdd_env_info(env);
1899 struct lu_fid *fid = &info->mti_fid;
1900 struct mdd_object *obj = md2mdd_obj(pobj);
1901 struct mdd_device *m = mdo2mdd(pobj);
1902 bool check_perm = true;
1907 if (mdd_is_dead_obj(obj))
1911 * In some cases this lookup is not needed - we know before if name
1912 * exists or not because MDT performs lookup for it.
1913 * name length check is done in lookup.
1915 if (spec->sp_cr_lookup) {
1917 * Check if the name already exist, though it will be checked in
1918 * _index_insert also, for avoiding rolling back if exists
1921 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1922 MAY_WRITE | MAY_EXEC);
1924 RETURN(rc ? : -EEXIST);
1926 /* Permission is already being checked in mdd_lookup */
1930 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1935 if (pattr->la_mode & S_ISGID) {
1936 cattr->la_gid = pattr->la_gid;
1937 if (S_ISDIR(cattr->la_mode)) {
1938 cattr->la_mode |= S_ISGID;
1939 cattr->la_valid |= LA_MODE;
1943 rc = mdd_name_check(m, lname);
1947 switch (cattr->la_mode & S_IFMT) {
1949 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1951 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1952 RETURN(-ENAMETOOLONG);
1971 static int mdd_declare_object_create(const struct lu_env *env,
1972 struct mdd_device *mdd,
1973 struct mdd_object *p, struct mdd_object *c,
1974 struct lu_attr *attr,
1975 struct thandle *handle,
1976 const struct md_op_spec *spec,
1977 struct lu_buf *def_acl_buf,
1978 struct lu_buf *acl_buf,
1979 struct dt_allocation_hint *hint)
1983 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1988 #ifdef CONFIG_FS_POSIX_ACL
1989 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1990 /* if dir, then can inherit default ACl */
1991 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1992 XATTR_NAME_ACL_DEFAULT,
1998 if (acl_buf->lb_len > 0) {
1999 rc = mdo_declare_attr_set(env, c, attr, handle);
2003 rc = mdo_declare_xattr_set(env, c, acl_buf,
2004 XATTR_NAME_ACL_ACCESS, 0, handle);
2009 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2013 /* replay case, create LOV EA from client data */
2014 if (spec->no_create ||
2015 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2016 const struct lu_buf *buf;
2018 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2019 spec->u.sp_ea.eadatalen);
2020 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2026 if (S_ISLNK(attr->la_mode)) {
2027 const char *target_name = spec->u.sp_symname;
2028 int sym_len = strlen(target_name);
2029 const struct lu_buf *buf;
2031 buf = mdd_buf_get_const(env, target_name, sym_len);
2032 rc = dt_declare_record_write(env, mdd_object_child(c),
2041 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2042 struct mdd_object *p, struct mdd_object *c,
2043 const struct lu_name *name,
2044 struct lu_attr *attr,
2045 struct thandle *handle,
2046 const struct md_op_spec *spec,
2047 struct linkea_data *ldata,
2048 struct lu_buf *def_acl_buf,
2049 struct lu_buf *acl_buf,
2050 struct dt_allocation_hint *hint)
2054 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2055 def_acl_buf, acl_buf, hint);
2059 if (S_ISDIR(attr->la_mode)) {
2060 rc = mdo_declare_ref_add(env, p, handle);
2065 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2066 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2070 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2072 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2073 name->ln_name, handle);
2077 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2082 la->la_valid = LA_CTIME | LA_MTIME;
2083 rc = mdo_declare_attr_set(env, p, la, handle);
2087 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2092 /* XXX: For remote create, it should indicate the remote RPC
2093 * will be sent after local transaction is finished, which
2094 * is not very nice, but it will be removed once we fully support
2096 if (mdd_object_remote(p) && handle->th_update != NULL)
2097 handle->th_update->tu_sent_after_local_trans = 1;
2102 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2103 struct lu_attr *la, struct lu_buf *def_acl_buf,
2104 struct lu_buf *acl_buf)
2109 if (S_ISLNK(la->la_mode)) {
2110 acl_buf->lb_len = 0;
2111 def_acl_buf->lb_len = 0;
2115 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2116 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2117 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2118 mdd_read_unlock(env, pobj);
2120 /* If there are default ACL, fix mode/ACL by default ACL */
2121 def_acl_buf->lb_len = rc;
2122 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2123 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2124 acl_buf->lb_len = rc;
2125 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2128 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2129 /* If there are no default ACL, fix mode by mask */
2130 struct lu_ucred *uc = lu_ucred(env);
2132 /* The create triggered by MDT internal events, such as
2133 * LFSCK reset, will not contain valid "uc". */
2134 if (unlikely(uc != NULL))
2135 la->la_mode &= ~uc->uc_umask;
2137 acl_buf->lb_len = 0;
2138 def_acl_buf->lb_len = 0;
2145 * Create a metadata object and initialize it, set acl, xattr.
2147 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2148 struct mdd_object *son, struct lu_attr *attr,
2149 struct md_op_spec *spec, struct lu_buf *acl_buf,
2150 struct lu_buf *def_acl_buf,
2151 struct dt_allocation_hint *hint,
2152 struct thandle *handle)
2156 mdd_write_lock(env, son, MOR_TGT_CHILD);
2157 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2162 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2163 * created in declare phase, they also needs to be added to master
2164 * object as sub-directory entry. So it has to initialize the master
2165 * object, then set dir striped EA.(in mdo_xattr_set) */
2166 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2169 GOTO(err_destroy, rc);
2172 * in case of replay we just set LOVEA provided by the client
2173 * XXX: I think it would be interesting to try "old" way where
2174 * MDT calls this xattr_set(LOV) in a different transaction.
2175 * probably this way we code can be made better.
2178 /* During creation, there are only a few cases we need do xattr_set to
2180 * 1. regular file: see comments above.
2181 * 2. create striped directory with provided stripeEA.
2182 * 3. create striped directory because inherit default layout from the
2184 if (spec->no_create ||
2185 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2186 S_ISDIR(attr->la_mode)) {
2187 const struct lu_buf *buf;
2189 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2190 spec->u.sp_ea.eadatalen);
2191 rc = mdo_xattr_set(env, son, buf,
2192 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2194 handle, BYPASS_CAPA);
2196 GOTO(err_destroy, rc);
2199 #ifdef CONFIG_FS_POSIX_ACL
2200 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2201 S_ISDIR(attr->la_mode)) {
2202 /* set default acl */
2203 rc = mdo_xattr_set(env, son, def_acl_buf,
2204 XATTR_NAME_ACL_DEFAULT, 0,
2205 handle, BYPASS_CAPA);
2207 GOTO(err_destroy, rc);
2209 /* set its own acl */
2210 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2211 rc = mdo_xattr_set(env, son, acl_buf,
2212 XATTR_NAME_ACL_ACCESS,
2213 0, handle, BYPASS_CAPA);
2215 GOTO(err_destroy, rc);
2219 if (S_ISLNK(attr->la_mode)) {
2220 struct lu_ucred *uc = lu_ucred_assert(env);
2221 struct dt_object *dt = mdd_object_child(son);
2222 const char *target_name = spec->u.sp_symname;
2223 int sym_len = strlen(target_name);
2224 const struct lu_buf *buf;
2227 buf = mdd_buf_get_const(env, target_name, sym_len);
2228 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2229 mdd_object_capa(env, son),
2231 CFS_CAP_SYS_RESOURCE_MASK);
2236 GOTO(err_initlized, rc = -EFAULT);
2240 if (unlikely(rc != 0)) {
2242 if (S_ISDIR(attr->la_mode)) {
2243 /* Drop the reference, no need to delete "."/"..",
2244 * because the object to be destroied directly. */
2245 rc2 = mdo_ref_del(env, son, handle);
2249 rc2 = mdo_ref_del(env, son, handle);
2253 mdo_destroy(env, son, handle);
2256 mdd_write_unlock(env, son);
2261 * Create object and insert it into namespace.
2263 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2264 const struct lu_name *lname, struct md_object *child,
2265 struct md_op_spec *spec, struct md_attr* ma)
2267 struct mdd_thread_info *info = mdd_env_info(env);
2268 struct lu_attr *la = &info->mti_la_for_fix;
2269 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2270 struct mdd_object *son = md2mdd_obj(child);
2271 struct mdd_device *mdd = mdo2mdd(pobj);
2272 struct lu_attr *attr = &ma->ma_attr;
2273 struct thandle *handle;
2274 struct lu_attr *pattr = &info->mti_pattr;
2275 struct lu_buf acl_buf;
2276 struct lu_buf def_acl_buf;
2277 struct linkea_data *ldata = &info->mti_link_data;
2278 const char *name = lname->ln_name;
2279 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2285 * Two operations have to be performed:
2287 * - an allocation of a new object (->do_create()), and
2289 * - an insertion into a parent index (->dio_insert()).
2291 * Due to locking, operation order is not important, when both are
2292 * successful, *but* error handling cases are quite different:
2294 * - if insertion is done first, and following object creation fails,
2295 * insertion has to be rolled back, but this operation might fail
2296 * also leaving us with dangling index entry.
2298 * - if creation is done first, is has to be undone if insertion
2299 * fails, leaving us with leaked space, which is neither good, nor
2302 * It seems that creation-first is simplest solution, but it is
2303 * sub-optimal in the frequent
2308 * case, because second mkdir is bound to create object, only to
2309 * destroy it immediately.
2311 * To avoid this follow local file systems that do double lookup:
2313 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2315 * 1. create (mdd_object_create_internal())
2317 * 2. insert (__mdd_index_insert(), lookup again)
2320 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2324 /* Sanity checks before big job. */
2325 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2329 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2330 GOTO(out_free, rc = -EINPROGRESS);
2332 handle = mdd_trans_create(env, mdd);
2334 GOTO(out_free, rc = PTR_ERR(handle));
2336 acl_buf.lb_buf = info->mti_xattr_buf;
2337 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2338 def_acl_buf.lb_buf = info->mti_key;
2339 def_acl_buf.lb_len = sizeof(info->mti_key);
2340 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2344 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2346 memset(ldata, 0, sizeof(*ldata));
2347 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT2)) {
2348 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2351 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2352 &tfid, lname, 1, 0, ldata);
2354 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2355 mdd_object_fid(mdd_pobj),
2356 lname, 1, 0, ldata);
2359 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2360 handle, spec, ldata, &def_acl_buf, &acl_buf,
2365 rc = mdd_trans_start(env, mdd, handle);
2369 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2370 &def_acl_buf, hint, handle);
2374 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2375 mdd_write_lock(env, son, MOR_TGT_CHILD);
2376 rc = __mdd_orphan_add(env, son, handle);
2377 GOTO(out_volatile, rc);
2379 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2380 attr->la_mode, name, handle,
2381 mdd_object_capa(env, mdd_pobj));
2383 GOTO(err_created, rc);
2385 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2388 /* update parent directory mtime/ctime */
2390 la->la_valid = LA_CTIME | LA_MTIME;
2391 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2393 GOTO(err_insert, rc);
2401 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2402 rc2 = __mdd_orphan_del(env, son, handle);
2404 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2405 S_ISDIR(attr->la_mode),
2406 handle, BYPASS_CAPA);
2411 mdd_write_lock(env, son, MOR_TGT_CHILD);
2412 if (S_ISDIR(attr->la_mode)) {
2413 /* Drop the reference, no need to delete "."/"..",
2414 * because the object is to be destroyed directly. */
2415 rc2 = mdo_ref_del(env, son, handle);
2417 mdd_write_unlock(env, son);
2422 /* For volatile files drop one link immediately, since there is
2423 * no filename in the namespace, and save any error returned. */
2424 rc2 = mdo_ref_del(env, son, handle);
2426 mdd_write_unlock(env, son);
2427 if (unlikely(rc == 0))
2432 /* Don't destroy the volatile object on success */
2433 if (likely(rc != 0))
2434 mdo_destroy(env, son, handle);
2435 mdd_write_unlock(env, son);
2438 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2439 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2440 rc = mdd_changelog_ns_store(env, mdd,
2441 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2442 S_ISREG(attr->la_mode) ? CL_CREATE :
2443 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2444 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2447 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2451 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2452 /* if we vmalloced a large buffer drop it */
2453 lu_buf_free(ldata->ld_buf);
2455 /* The child object shouldn't be cached anymore */
2457 set_bit(LU_OBJECT_HEARD_BANSHEE,
2458 &child->mo_lu.lo_header->loh_flags);
2463 * Get locks on parents in proper order
2464 * RETURN: < 0 - error, rename_order if successful
2472 static int mdd_rename_order(const struct lu_env *env,
2473 struct mdd_device *mdd,
2474 struct mdd_object *src_pobj,
2475 const struct lu_attr *pattr,
2476 struct mdd_object *tgt_pobj)
2478 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2482 if (src_pobj == tgt_pobj)
2483 RETURN(MDD_RN_SAME);
2485 /* compared the parent child relationship of src_p&tgt_p */
2486 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2488 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2491 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2505 /* has not mdd_write{read}_lock on any obj yet. */
2506 static int mdd_rename_sanity_check(const struct lu_env *env,
2507 struct mdd_object *src_pobj,
2508 const struct lu_attr *pattr,
2509 struct mdd_object *tgt_pobj,
2510 const struct lu_attr *tpattr,
2511 struct mdd_object *sobj,
2512 const struct lu_attr *cattr,
2513 struct mdd_object *tobj,
2514 const struct lu_attr *tattr)
2519 /* XXX: when get here, sobj must NOT be NULL,
2520 * the other case has been processed in cld_rename
2521 * before mdd_rename and enable MDS_PERM_BYPASS. */
2524 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2528 /* XXX: when get here, "tobj == NULL" means tobj must
2529 * NOT exist (neither on remote MDS, such case has been
2530 * processed in cld_rename before mdd_rename and enable
2532 * So check may_create, but not check may_unlink. */
2534 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2535 (src_pobj != tgt_pobj));
2537 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2538 (src_pobj != tgt_pobj), 1);
2540 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2541 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2546 static int mdd_declare_rename(const struct lu_env *env,
2547 struct mdd_device *mdd,
2548 struct mdd_object *mdd_spobj,
2549 struct mdd_object *mdd_tpobj,
2550 struct mdd_object *mdd_sobj,
2551 struct mdd_object *mdd_tobj,
2552 const struct lu_name *tname,
2553 const struct lu_name *sname,
2555 struct linkea_data *ldata,
2556 struct thandle *handle)
2558 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2561 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2562 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2568 /* name from source dir */
2569 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2573 /* .. from source child */
2574 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2575 /* source child can be directory,
2576 * counted by source dir's nlink */
2577 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2580 if (mdd_spobj != mdd_tpobj) {
2581 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2586 rc = mdo_declare_index_insert(env, mdd_sobj,
2588 S_IFDIR, dotdot, handle);
2593 /* new target child can be directory,
2594 * counted by target dir's nlink */
2595 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2600 la->la_valid = LA_CTIME | LA_MTIME;
2601 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2605 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2609 la->la_valid = LA_CTIME;
2610 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2614 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2615 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2620 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2621 mdd_object_type(mdd_sobj),
2622 tname->ln_name, handle);
2626 /* name from target dir (old name), we declare it unconditionally
2627 * as mdd_rename() calls delete unconditionally as well. so just
2628 * to balance declarations vs calls to change ... */
2629 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2633 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2634 /* delete target child in target parent directory */
2635 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2639 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2640 /* target child can be directory,
2641 * delete "." reference in target child directory */
2642 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2646 /* delete ".." reference in target parent directory */
2647 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2652 la->la_valid = LA_CTIME;
2653 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2657 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2662 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2669 /* src object can be remote that is why we use only fid and type of object */
2670 static int mdd_rename(const struct lu_env *env,
2671 struct md_object *src_pobj, struct md_object *tgt_pobj,
2672 const struct lu_fid *lf, const struct lu_name *lsname,
2673 struct md_object *tobj, const struct lu_name *ltname,
2676 const char *sname = lsname->ln_name;
2677 const char *tname = ltname->ln_name;
2678 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2679 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2680 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2681 struct mdd_device *mdd = mdo2mdd(src_pobj);
2682 struct mdd_object *mdd_sobj = NULL; /* source object */
2683 struct mdd_object *mdd_tobj = NULL;
2684 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2685 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2686 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2687 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2688 struct thandle *handle;
2689 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2690 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2691 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2694 bool tobj_locked = 0;
2695 unsigned cl_flags = 0;
2700 mdd_tobj = md2mdd_obj(tobj);
2702 mdd_sobj = mdd_object_find(env, mdd, lf);
2704 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2706 GOTO(out_pending, rc);
2708 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2710 GOTO(out_pending, rc);
2713 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2715 GOTO(out_pending, rc);
2718 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2720 GOTO(out_pending, rc);
2722 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2723 mdd_sobj, cattr, mdd_tobj, tattr);
2725 GOTO(out_pending, rc);
2727 rc = mdd_name_check(mdd, ltname);
2729 GOTO(out_pending, rc);
2731 handle = mdd_trans_create(env, mdd);
2733 GOTO(out_pending, rc = PTR_ERR(handle));
2735 memset(ldata, 0, sizeof(*ldata));
2736 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2737 ltname, 1, 0, ldata);
2738 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2739 mdd_tobj, lsname, ltname, ma, ldata, handle);
2743 rc = mdd_trans_start(env, mdd, handle);
2747 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2748 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2750 GOTO(cleanup_unlocked, rc);
2752 is_dir = S_ISDIR(cattr->la_mode);
2754 /* Remove source name from source directory */
2755 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2756 mdd_object_capa(env, mdd_spobj));
2760 /* "mv dir1 dir2" needs "dir1/.." link update */
2761 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2762 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2763 mdd_object_capa(env, mdd_sobj));
2765 GOTO(fixup_spobj2, rc);
2767 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2769 mdd_object_capa(env, mdd_sobj));
2771 GOTO(fixup_spobj, rc);
2774 /* Remove target name from target directory
2775 * Here tobj can be remote one, so we do index_delete unconditionally
2776 * and -ENOENT is allowed.
2778 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2779 mdd_object_capa(env, mdd_tpobj));
2782 /* tname might been renamed to something else */
2783 GOTO(fixup_spobj, rc);
2786 GOTO(fixup_spobj, rc);
2789 /* Insert new fid with target name into target dir */
2790 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2791 tname, handle, mdd_object_capa(env, mdd_tpobj));
2793 GOTO(fixup_tpobj, rc);
2795 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2796 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2798 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2800 la->la_valid = LA_CTIME;
2801 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2803 GOTO(fixup_tpobj, rc);
2806 /* Remove old target object
2807 * For tobj is remote case cmm layer has processed
2808 * and set tobj to NULL then. So when tobj is NOT NULL,
2809 * it must be local one.
2811 if (tobj && mdd_object_exists(mdd_tobj)) {
2812 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2814 if (mdd_is_dead_obj(mdd_tobj)) {
2815 /* shld not be dead, something is wrong */
2816 CERROR("tobj is dead, something is wrong\n");
2820 mdo_ref_del(env, mdd_tobj, handle);
2822 /* Remove dot reference. */
2823 if (S_ISDIR(tattr->la_mode))
2824 mdo_ref_del(env, mdd_tobj, handle);
2827 /* fetch updated nlink */
2828 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2830 CERROR("%s: Failed to get nlink for tobj "
2832 mdd2obd_dev(mdd)->obd_name,
2833 PFID(tpobj_fid), rc);
2834 GOTO(fixup_tpobj, rc);
2837 la->la_valid = LA_CTIME;
2838 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2840 CERROR("%s: Failed to set ctime for tobj "
2842 mdd2obd_dev(mdd)->obd_name,
2843 PFID(tpobj_fid), rc);
2844 GOTO(fixup_tpobj, rc);
2847 /* XXX: this transfer to ma will be removed with LOD/OSP */
2848 ma->ma_attr = *tattr;
2849 ma->ma_valid |= MA_INODE;
2850 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2853 CERROR("%s: Failed to unlink tobj "
2855 mdd2obd_dev(mdd)->obd_name,
2856 PFID(tpobj_fid), rc);
2857 GOTO(fixup_tpobj, rc);
2860 /* fetch updated nlink */
2861 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2863 CERROR("%s: Failed to get nlink for tobj "
2865 mdd2obd_dev(mdd)->obd_name,
2866 PFID(tpobj_fid), rc);
2867 GOTO(fixup_tpobj, rc);
2869 /* XXX: this transfer to ma will be removed with LOD/OSP */
2870 ma->ma_attr = *tattr;
2871 ma->ma_valid |= MA_INODE;
2873 if (tattr->la_nlink == 0) {
2874 cl_flags |= CLF_RENAME_LAST;
2875 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2876 cl_flags |= CLF_RENAME_LAST_EXISTS;
2880 la->la_valid = LA_CTIME | LA_MTIME;
2881 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2883 GOTO(fixup_tpobj, rc);
2885 if (mdd_spobj != mdd_tpobj) {
2886 la->la_valid = LA_CTIME | LA_MTIME;
2887 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2890 if (rc == 0 && mdd_sobj) {
2891 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2892 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2893 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2896 /* Old files might not have EA entry */
2897 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2898 lsname, handle, NULL, 0);
2899 mdd_write_unlock(env, mdd_sobj);
2900 /* We don't fail the transaction if the link ea can't be
2901 updated -- fid2path will use alternate lookup method. */
2909 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2912 CWARN("tp obj fix error %d\n",rc2);
2914 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2915 !mdd_is_dead_obj(mdd_tobj)) {
2917 mdo_ref_add(env, mdd_tobj, handle);
2919 mdo_ref_add(env, mdd_tobj, handle);
2922 rc2 = __mdd_index_insert(env, mdd_tpobj,
2924 mdd_object_type(mdd_tobj),
2925 tname, handle, BYPASS_CAPA);
2927 CWARN("tp obj fix error: rc = %d\n", rc2);
2932 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2933 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2937 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2938 mdd2obd_dev(mdd)->obd_name, rc2);
2941 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2942 dotdot, handle, BYPASS_CAPA);
2944 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2945 mdd2obd_dev(mdd)->obd_name, rc2);
2950 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2951 mdd_object_type(mdd_sobj), sname,
2952 handle, BYPASS_CAPA);
2954 CWARN("sp obj fix error: rc = %d\n", rc2);
2959 mdd_write_unlock(env, mdd_tobj);
2963 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
2964 mdd_tobj, tpobj_fid, lf, spobj_fid,
2965 ltname, lsname, handle);
2968 mdd_trans_stop(env, mdd, rc, handle);
2971 mdd_object_put(env, mdd_sobj);
2976 * During migration once the parent FID has been changed,
2977 * we need update the parent FID in linkea.
2979 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2980 struct mdd_object *parent,
2981 struct mdd_object *child,
2982 const char *name, int namelen,
2983 struct thandle *handle,
2986 struct mdd_thread_info *info = mdd_env_info(env);
2987 struct linkea_data ldata = {0};
2988 struct lu_buf *buf = &info->mti_link_buf;
2994 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2995 if (buf->lb_buf == NULL)
2999 rc = mdd_links_read(env, child, &ldata);
3001 if (rc == -ENOENT || rc == -ENODATA)
3006 LASSERT(ldata.ld_leh != NULL);
3007 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3008 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3009 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3010 struct lu_name lname;
3013 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3016 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3017 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3018 ldata.ld_lee = (struct link_ea_entry *)
3019 ((char *)ldata.ld_lee +
3024 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3025 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3026 lname.ln_namelen, lname.ln_name,
3027 PFID(mdd_object_fid(parent)));
3028 /* update to the new parent fid */
3029 linkea_entry_pack(ldata.ld_lee, &lname,
3030 mdd_object_fid(parent));
3032 rc = mdd_declare_links_add(env, child, handle, &ldata,
3035 rc = mdd_links_write(env, child, &ldata, handle);
3041 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3042 struct mdd_object *parent,
3043 struct mdd_object *child,
3044 const char *name, int namelen,
3045 struct thandle *handle)
3047 return mdd_linkea_update_child_internal(env, parent, child, name,
3048 namelen, handle, true);
3051 static int mdd_linkea_update_child(const struct lu_env *env,
3052 struct mdd_object *parent,
3053 struct mdd_object *child,
3054 const char *name, int namelen,
3055 struct thandle *handle)
3057 return mdd_linkea_update_child_internal(env, parent, child, name,
3058 namelen, handle, false);
3061 static int mdd_update_linkea_internal(const struct lu_env *env,
3062 struct mdd_object *mdd_pobj,
3063 struct mdd_object *mdd_sobj,
3064 struct mdd_object *mdd_tobj,
3065 const struct lu_name *child_name,
3066 struct thandle *handle,
3069 struct mdd_thread_info *info = mdd_env_info(env);
3070 struct linkea_data *ldata = &info->mti_link_data;
3075 rc = mdd_links_read(env, mdd_sobj, ldata);
3077 if (rc == -ENOENT || rc == -ENODATA)
3083 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3086 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3091 /* If it is mulitple links file, we need update the name entry for
3093 LASSERT(ldata->ld_leh != NULL);
3094 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3095 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3096 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3097 struct mdd_object *pobj;
3098 struct lu_name lname;
3101 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3103 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3105 pobj = mdd_object_find(env, mdd, &fid);
3107 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3108 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3113 if (!mdd_object_exists(pobj)) {
3114 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3115 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3119 if (pobj == mdd_pobj &&
3120 lname.ln_namelen == child_name->ln_namelen &&
3121 strncmp(lname.ln_name, child_name->ln_name,
3122 lname.ln_namelen) == 0) {
3123 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3124 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3129 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3130 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3131 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3134 /* Remove source name from source directory */
3135 /* Insert new fid with target name into target dir */
3136 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3141 rc = mdo_declare_index_insert(env, pobj,
3142 mdd_object_fid(mdd_tobj),
3143 mdd_object_type(mdd_tobj),
3144 lname.ln_name, handle);
3148 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3152 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3156 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3158 mdd_object_capa(env, pobj));
3162 rc = __mdd_index_insert(env, pobj,
3163 mdd_object_fid(mdd_tobj),
3164 mdd_object_type(mdd_tobj),
3165 lname.ln_name, handle,
3166 mdd_object_capa(env, pobj));
3170 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3171 rc = mdo_ref_add(env, mdd_tobj, handle);
3172 mdd_write_unlock(env, mdd_tobj);
3176 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3177 mdo_ref_del(env, mdd_sobj, handle);
3178 mdd_write_unlock(env, mdd_sobj);
3181 mdd_object_put(env, pobj);
3189 static int mdd_migrate_xattrs(const struct lu_env *env,
3190 struct mdd_object *mdd_sobj,
3191 struct mdd_object *mdd_tobj)
3193 struct mdd_thread_info *info = mdd_env_info(env);
3194 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3196 struct thandle *handle;
3202 struct lu_buf list_xbuf;
3205 /* retrieve xattr list from the old object */
3206 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3207 mdd_object_capa(env, mdd_sobj));
3208 if (list_xsize == -ENODATA)
3214 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3215 if (info->mti_big_buf.lb_buf == NULL)
3218 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3219 list_xbuf.lb_len = list_xsize;
3220 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3221 mdd_object_capa(env, mdd_sobj));
3226 xname = list_xbuf.lb_buf;
3228 xlen = strnlen(xname, rem - 1) + 1;
3229 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3230 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3231 strcmp(XATTR_NAME_LMV, xname) == 0)
3234 /* For directory, if there are default layout, migrate here */
3235 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3236 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3239 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3241 mdd_object_capa(env, mdd_sobj));
3242 if (xsize == -ENODATA)
3247 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3248 if (info->mti_link_buf.lb_buf == NULL)
3249 GOTO(out, rc = -ENOMEM);
3251 xbuf.lb_len = xsize;
3252 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3253 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3254 mdd_object_capa(env, mdd_sobj));
3260 handle = mdd_trans_create(env, mdd);
3262 GOTO(out, rc = PTR_ERR(handle));
3264 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3267 GOTO(stop_trans, rc);
3268 /* Note: this transaction is part of migration, and it is not
3269 * the last step of migration, so we set th_local = 1 to avoid
3270 * update last rcvd for this transaction */
3271 handle->th_local = 1;
3272 rc = mdd_trans_start(env, mdd, handle);
3274 GOTO(stop_trans, rc);
3276 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3277 mdd_object_capa(env, mdd_sobj));
3279 GOTO(stop_trans, rc = 0);
3282 GOTO(stop_trans, rc);
3284 mdd_trans_stop(env, mdd, rc, handle);
3289 memmove(xname, xname + xlen, rem);
3295 static int mdd_declare_migrate_create(const struct lu_env *env,
3296 struct mdd_object *mdd_pobj,
3297 struct mdd_object *mdd_sobj,
3298 struct mdd_object *mdd_tobj,
3299 struct md_op_spec *spec,
3301 union lmv_mds_md *mgr_ea,
3302 struct thandle *handle)
3304 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3305 const struct lu_buf *buf;
3309 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3310 handle, spec, NULL);
3314 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3319 if (S_ISLNK(la->la_mode)) {
3320 const char *target_name = spec->u.sp_symname;
3321 int sym_len = strlen(target_name);
3322 const struct lu_buf *buf;
3324 buf = mdd_buf_get_const(env, target_name, sym_len);
3325 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3331 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3332 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3333 spec->u.sp_ea.eadatalen);
3334 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3340 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3341 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3342 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3347 la_flag->la_valid = LA_FLAGS;
3348 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3349 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3354 static int mdd_migrate_create(const struct lu_env *env,
3355 struct mdd_object *mdd_pobj,
3356 struct mdd_object *mdd_sobj,
3357 struct mdd_object *mdd_tobj,
3360 struct mdd_thread_info *info = mdd_env_info(env);
3361 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3362 struct md_op_spec *spec = &info->mti_spec;
3363 struct lu_buf lmm_buf = { 0 };
3364 struct lu_buf link_buf = { 0 };
3365 const struct lu_buf *buf;
3366 struct thandle *handle;
3367 struct lmv_mds_md_v1 *mgr_ea;
3368 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3369 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3374 /* prepare spec for create */
3375 memset(spec, 0, sizeof(*spec));
3376 spec->sp_cr_lookup = 0;
3377 spec->sp_feat = &dt_directory_features;
3378 if (S_ISLNK(la->la_mode)) {
3379 buf = lu_buf_check_and_alloc(
3380 &mdd_env_info(env)->mti_big_buf,
3383 link_buf.lb_len = la->la_size + 1;
3384 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3386 rc = rc != 0 ? rc : -EFAULT;
3387 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3388 mdd2obd_dev(mdd)->obd_name,
3389 PFID(mdd_object_fid(mdd_sobj)), rc);
3392 spec->u.sp_symname = link_buf.lb_buf;
3393 } else if S_ISREG(la->la_mode) {
3394 /* retrieve lov of the old object */
3395 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3396 if (rc != 0 && rc != -ENODATA)
3398 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3399 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3400 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3401 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3405 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3406 memset(mgr_ea, 0, sizeof(*mgr_ea));
3407 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3408 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3409 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3410 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3411 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3412 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3414 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3416 handle = mdd_trans_create(env, mdd);
3418 GOTO(out_free, rc = PTR_ERR(handle));
3420 /* Note: this transaction is part of migration, and it is not
3421 * the last step of migration, so we set th_local = 1 to avoid
3422 * update last rcvd for this transaction */
3423 handle->th_local = 1;
3424 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3426 (union lmv_mds_md *)info->mti_xattr_buf,
3429 GOTO(stop_trans, rc);
3431 rc = mdd_trans_start(env, mdd, handle);
3433 GOTO(stop_trans, rc);
3435 /* create the target object */
3436 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3439 GOTO(stop_trans, rc);
3441 /* Set MIGRATE EA on the source inode, so once the migration needs
3442 * to be re-done during failover, the re-do process can locate the
3443 * target object which is already being created. */
3444 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3445 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3446 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3447 handle, mdd_object_capa(env, mdd_sobj));
3449 GOTO(stop_trans, rc);
3451 /* Set immutable flag, so any modification is disabled until
3452 * the migration is done. Once the migration is interrupted,
3453 * if the resume process find the migrating object has both
3454 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3455 * flag and approve the migration */
3456 la_flag->la_valid = LA_FLAGS;
3457 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3458 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3459 mdd_object_capa(env, mdd_sobj));
3462 mdd_trans_stop(env, mdd, rc, handle);
3464 if (lmm_buf.lb_buf != NULL)
3465 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3469 static int mdd_migrate_entries(const struct lu_env *env,
3470 struct mdd_object *mdd_sobj,
3471 struct mdd_object *mdd_tobj)
3473 struct dt_object *next = mdd_object_child(mdd_sobj);
3474 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3475 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3476 struct thandle *handle;
3478 const struct dt_it_ops *iops;
3481 struct lu_dirent *ent;
3484 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3488 if (!dt_try_as_dir(env, next))
3489 GOTO(out_ent, rc = -ENOTDIR);
3491 * iterate directories
3493 iops = &next->do_index_ops->dio_it;
3494 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3495 mdd_object_capa(env, mdd_sobj));
3497 GOTO(out_ent, rc = PTR_ERR(it));
3499 rc = iops->load(env, it, 0);
3501 rc = iops->next(env, it);
3505 * At this point and across for-loop:
3507 * rc == 0 -> ok, proceed.
3508 * rc > 0 -> end of directory.
3512 struct mdd_object *child;
3513 char *name = mdd_env_info(env)->mti_key;
3517 bool target_exist = false;
3519 len = iops->key_size(env, it);
3523 result = iops->rec(env, it, (struct dt_rec *)ent,
3524 LUDA_FID | LUDA_TYPE);
3525 if (result == -ESTALE)
3532 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3533 recsize = le16_to_cpu(ent->lde_reclen);
3535 /* Insert new fid with target name into target dir */
3536 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3537 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3538 ent->lde_name[1] == '.'))
3541 child = mdd_object_find(env, mdd, &ent->lde_fid);
3543 GOTO(out, rc = PTR_ERR(child));
3545 is_dir = S_ISDIR(mdd_object_type(child));
3547 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3549 /* Check whether the name has been inserted to the target */
3550 if (dt_try_as_dir(env, dt_tobj)) {
3551 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3553 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3554 (struct dt_key *)name,
3555 mdd_object_capa(env, mdd_tobj));
3556 if (unlikely(rc == 0))
3557 target_exist = true;
3560 handle = mdd_trans_create(env, mdd);
3562 GOTO(out, rc = PTR_ERR(handle));
3564 /* Note: this transaction is part of migration, and it is not
3565 * the last step of migration, so we set th_local = 1 to avoid
3566 * updating last rcvd for this transaction */
3567 handle->th_local = 1;
3568 if (likely(!target_exist)) {
3569 rc = mdo_declare_index_insert(env, mdd_tobj,
3571 mdd_object_type(child),
3577 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3583 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3588 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3592 /* Update .. for child */
3593 rc = mdo_declare_index_delete(env, child, dotdot,
3598 rc = mdo_declare_index_insert(env, child,
3599 mdd_object_fid(mdd_tobj),
3600 S_IFDIR, dotdot, handle);
3605 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3612 rc = mdd_trans_start(env, mdd, handle);
3614 CERROR("%s: transaction start failed: rc = %d\n",
3615 mdd2obd_dev(mdd)->obd_name, rc);
3619 if (likely(!target_exist)) {
3620 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3621 mdd_object_type(child),
3623 mdd_object_capa(env, mdd_tobj));
3628 rc = mdo_ref_add(env, mdd_tobj, handle);
3635 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3636 mdd_object_capa(env, mdd_sobj));
3641 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3642 mdd_object_capa(env, child));
3646 rc = __mdd_index_insert_only(env, child,
3647 mdd_object_fid(mdd_tobj), S_IFDIR,
3649 mdd_object_capa(env, child));
3654 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3655 strlen(name), handle);
3658 mdd_object_put(env, child);
3659 mdd_trans_stop(env, mdd, rc, handle);
3663 result = iops->next(env, it);
3664 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3665 GOTO(out, rc = -EINTR);
3667 if (result == -ESTALE)
3669 } while (result == 0);
3672 iops->fini(env, it);
3674 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3678 static int mdd_declare_update_linkea(const struct lu_env *env,
3679 struct mdd_object *mdd_pobj,
3680 struct mdd_object *mdd_sobj,
3681 struct mdd_object *mdd_tobj,
3682 struct thandle *handle,
3683 const struct lu_name *child_name)
3685 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3686 child_name, handle, 1);
3689 static int mdd_update_linkea(const struct lu_env *env,
3690 struct mdd_object *mdd_pobj,
3691 struct mdd_object *mdd_sobj,
3692 struct mdd_object *mdd_tobj,
3693 struct thandle *handle,
3694 const struct lu_name *child_name)
3696 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3697 child_name, handle, 0);
3700 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3701 struct mdd_object *mdd_pobj,
3702 struct mdd_object *mdd_sobj,
3703 struct mdd_object *mdd_tobj,
3704 const struct lu_name *lname,
3706 struct lu_attr *parent_la,
3707 struct thandle *handle)
3709 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3712 /* Revert IMMUTABLE flag */
3713 la_flag->la_valid = LA_FLAGS;
3714 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3715 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3719 /* delete entry from source dir */
3720 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3724 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3725 mdd_tobj, handle, lname);
3729 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3730 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3736 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3737 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3743 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3744 mdd_object_type(mdd_tobj),
3745 lname->ln_name, handle);
3749 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3750 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3755 /* delete old object */
3756 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3760 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3761 /* delete old object */
3762 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3765 /* set nlink to 0 */
3766 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3771 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3775 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3780 static int mdd_migrate_update_name(const struct lu_env *env,
3781 struct mdd_object *mdd_pobj,
3782 struct mdd_object *mdd_sobj,
3783 struct mdd_object *mdd_tobj,
3784 const struct lu_name *lname,
3787 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3788 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3789 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3790 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3791 struct thandle *handle;
3792 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3793 const char *name = lname->ln_name;
3797 /* update time for parent */
3798 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3799 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3800 p_la->la_valid = LA_CTIME;
3802 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3806 handle = mdd_trans_create(env, mdd);
3808 RETURN(PTR_ERR(handle));
3810 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3811 lname, so_attr, p_la, handle);
3813 /* If the migration can not be fit in one transaction, just
3814 * leave it in the original MDT */
3816 GOTO(stop_trans, rc = 0);
3818 GOTO(stop_trans, rc);
3821 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3822 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3823 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3824 PFID(mdd_object_fid(mdd_tobj)));
3826 rc = mdd_trans_start(env, mdd, handle);
3828 GOTO(stop_trans, rc);
3830 /* Revert IMMUTABLE flag */
3831 la_flag->la_valid = LA_FLAGS;
3832 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3833 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3834 mdd_object_capa(env, mdd_pobj));
3836 GOTO(stop_trans, rc);
3838 /* Remove source name from source directory */
3839 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3840 mdd_object_capa(env, mdd_pobj));
3842 GOTO(stop_trans, rc);
3844 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3847 GOTO(stop_trans, rc);
3849 if (S_ISREG(so_attr->la_mode)) {
3850 if (so_attr->la_nlink == 1) {
3851 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3853 mdd_object_capa(env, mdd_sobj));
3854 if (rc != 0 && rc != -ENODATA)
3855 GOTO(stop_trans, rc);
3859 /* Insert new fid with target name into target dir */
3860 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3861 mdd_object_type(mdd_tobj), name,
3862 handle, mdd_object_capa(env, mdd_pobj));
3864 GOTO(stop_trans, rc);
3866 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3869 GOTO(stop_trans, rc);
3871 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3872 mdo_ref_del(env, mdd_sobj, handle);
3874 mdo_ref_del(env, mdd_sobj, handle);
3876 /* Get the attr again after ref_del */
3877 rc = mdd_la_get(env, mdd_sobj, so_attr,
3878 mdd_object_capa(env, mdd_sobj));
3880 GOTO(out_unlock, rc);
3882 ma->ma_attr = *so_attr;
3883 ma->ma_valid |= MA_INODE;
3884 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3886 GOTO(out_unlock, rc);
3888 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3890 GOTO(out_unlock, rc);
3893 mdd_write_unlock(env, mdd_sobj);
3896 mdd_trans_stop(env, mdd, rc, handle);
3902 * Check whether we should migrate the file/dir
3904 * < 0 permission check failed or other error.
3905 * = 0 the file can be migrated.
3906 * > 0 the file does not need to be migrated, mostly
3907 * for multiple link file
3909 static int mdd_migrate_sanity_check(const struct lu_env *env,
3910 struct mdd_object *pobj,
3911 const struct lu_attr *pattr,
3912 struct mdd_object *sobj,
3913 struct lu_attr *sattr)
3915 struct mdd_thread_info *info = mdd_env_info(env);
3916 struct linkea_data *ldata = &info->mti_link_data;
3918 struct lu_buf *mgr_buf;
3924 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3925 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3926 if (mgr_buf->lb_buf == NULL)
3929 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3930 mdd_object_capa(env, sobj));
3932 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3934 /* If the object has migrateEA, it means IMMUTE flag
3935 * is being set by previous migration process, so it
3936 * needs to override the IMMUTE flag, otherwise the
3937 * following sanity check will fail */
3938 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3939 LMV_HASH_FLAG_MIGRATION) {
3940 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3942 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3943 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3944 mdd2obd_dev(mdd)->obd_name,
3945 PFID(mdd_object_fid(sobj)));
3949 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3950 sobj, sattr, NULL, NULL);
3954 /* Then it will check if the file should be migrated. If the file
3955 * has mulitple links, we only need migrate the file if all of its
3956 * entries has been migrated to the remote MDT */
3957 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3960 rc = mdd_links_read(env, sobj, ldata);
3962 /* For multiple links files, if there are no linkEA data at all,
3963 * means the file might be created before linkEA is enabled, and
3964 * all all of its links should not be migrated yet, otherwise
3965 * it should have some linkEA there */
3966 if (rc == -ENOENT || rc == -ENODATA)
3971 /* If it is mulitple links file, we need update the name entry for
3973 LASSERT(ldata->ld_leh != NULL);
3974 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3975 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3976 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3977 struct mdd_object *lpobj;
3978 struct lu_name lname;
3981 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3983 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3985 lpobj = mdd_object_find(env, mdd, &fid);
3986 if (IS_ERR(lpobj)) {
3987 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3988 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3993 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3994 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3995 PFID(&fid), lname.ln_namelen, lname.ln_name);
3996 mdd_object_put(env, lpobj);
4000 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4001 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4002 lname.ln_name, PFID(&fid));
4003 mdd_object_put(env, lpobj);
4011 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4012 struct md_object *sobj, const struct lu_name *lname,
4013 struct md_object *tobj, struct md_attr *ma)
4015 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4016 struct mdd_device *mdd = mdo2mdd(pobj);
4017 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4018 struct mdd_object *mdd_tobj = NULL;
4019 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4020 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4024 /* If the file will being migrated, it will check whether
4025 * the file is being opened by someone else right now */
4026 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4027 if (mdd_sobj->mod_count >= 1) {
4028 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4029 mdd2obd_dev(mdd)->obd_name,
4030 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4031 mdd_sobj->mod_count, -EBUSY);
4032 mdd_read_unlock(env, mdd_sobj);
4033 GOTO(put, rc = -EBUSY);
4035 mdd_read_unlock(env, mdd_sobj);
4037 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
4041 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
4045 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4052 /* Sigh, it is impossible to finish all of migration in a single
4053 * transaction, for example migrating big directory entries to the
4054 * new MDT, it needs insert all of name entries of children in the
4057 * So migration will be done in multiple steps and transactions.
4059 * 1. create an orphan object on the remote MDT in one transaction.
4060 * 2. migrate extend attributes to the new target file/directory.
4061 * 3. For directory, migrate the entries to the new MDT and update
4062 * linkEA of each children. Because we can not migrate all entries
4063 * in a single transaction, so the migrating directory will become
4064 * a striped directory during migration, so once the process is
4065 * interrupted, the directory is still accessible. (During lookup,
4066 * client will locate the name by searching both original and target
4068 * 4. Finally, update the name/FID to point to the new file/directory
4069 * in a separate transaction.
4072 /* step 1: Check whether the orphan object has been created, and create
4073 * orphan object on the remote MDT if needed */
4074 mdd_tobj = md2mdd_obj(tobj);
4075 if (!mdd_object_exists(mdd_tobj)) {
4076 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4082 /* step 2: migrate xattr */
4083 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4087 /* step 3: migrate name entries to the orphan object */
4088 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4089 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4092 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4093 OBD_FAIL_MDS_REINT_NET_REP)))
4097 /* step 4: update name entry to the new object */
4098 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4106 const struct md_dir_operations mdd_dir_ops = {
4107 .mdo_is_subdir = mdd_is_subdir,
4108 .mdo_lookup = mdd_lookup,
4109 .mdo_create = mdd_create,
4110 .mdo_rename = mdd_rename,
4111 .mdo_link = mdd_link,
4112 .mdo_unlink = mdd_unlink,
4113 .mdo_create_data = mdd_create_data,
4114 .mdo_migrate = mdd_migrate,