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_valid & LA_FLAGS) &&
384 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
387 rc = mdd_permission_internal_locked(env, pobj, pattr,
388 MAY_WRITE | MAY_EXEC,
393 if (mdd_is_append(pobj))
400 * pobj == NULL is remote ops case, under such case, pobj's
401 * VTX feature has been checked already, no need check again.
403 static inline int mdd_is_sticky(const struct lu_env *env,
404 struct mdd_object *pobj,
405 const struct lu_attr *pattr,
406 struct mdd_object *cobj,
407 const struct lu_attr *cattr)
409 struct lu_ucred *uc = lu_ucred_assert(env);
412 LASSERT(pattr != NULL);
413 if (!(pattr->la_mode & S_ISVTX) ||
414 (pattr->la_uid == uc->uc_fsuid))
418 LASSERT(cattr != NULL);
419 if (cattr->la_uid == uc->uc_fsuid)
422 return !md_capable(uc, CFS_CAP_FOWNER);
425 static int mdd_may_delete_entry(const struct lu_env *env,
426 struct mdd_object *pobj,
427 const struct lu_attr *pattr,
432 LASSERT(pobj != NULL);
433 if (!mdd_object_exists(pobj))
436 if (mdd_is_dead_obj(pobj))
441 rc = mdd_permission_internal_locked(env, pobj, pattr,
442 MAY_WRITE | MAY_EXEC,
448 if (mdd_is_append(pobj))
455 * Check whether it may delete the cobj from the pobj.
458 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
459 const struct lu_attr *tpattr, struct mdd_object *tobj,
460 const struct lu_attr *tattr, const struct lu_attr *cattr,
461 int check_perm, int check_empty)
467 LASSERT(tpattr != NULL);
468 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
476 if (!mdd_object_exists(tobj))
479 if (mdd_is_dead_obj(tobj))
482 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
485 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
488 if ((tattr->la_valid & LA_FLAGS) &&
489 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
492 /* additional check the rename case */
494 if (S_ISDIR(cattr->la_mode)) {
495 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
497 if (!S_ISDIR(tattr->la_mode))
500 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
502 } else if (S_ISDIR(tattr->la_mode))
506 if (S_ISDIR(tattr->la_mode) && check_empty)
507 rc = mdd_dir_is_empty(env, tobj);
513 * Check whether it can create the link file(linked to @src_obj) under
514 * the target directory(@tgt_obj), and src_obj has been locked by
517 * \param[in] env execution environment
518 * \param[in] tgt_obj the target directory
519 * \param[in] tattr attributes of target directory
520 * \param[in] lname the link name
521 * \param[in] src_obj source object for link
522 * \param[in] cattr attributes for source object
524 * \retval = 0 it is allowed to create the link file under tgt_obj
525 * \retval negative error not allowed to create the link file
527 static int mdd_link_sanity_check(const struct lu_env *env,
528 struct mdd_object *tgt_obj,
529 const struct lu_attr *tattr,
530 const struct lu_name *lname,
531 struct mdd_object *src_obj,
532 const struct lu_attr *cattr)
534 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
538 if (!mdd_object_exists(src_obj))
541 if (mdd_is_dead_obj(src_obj))
544 /* Local ops, no lookup before link, check filename length here. */
545 rc = mdd_name_check(m, lname);
549 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
552 if (S_ISDIR(mdd_object_type(src_obj)))
555 LASSERT(src_obj != tgt_obj);
556 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
560 rc = __mdd_may_link(env, src_obj, cattr);
565 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
566 const char *name, struct thandle *handle,
567 struct lustre_capa *capa)
569 struct dt_object *next = mdd_object_child(pobj);
573 if (dt_try_as_dir(env, next))
574 rc = dt_delete(env, next, (struct dt_key *)name, handle, capa);
581 static int __mdd_index_insert_only(const struct lu_env *env,
582 struct mdd_object *pobj,
583 const struct lu_fid *lf, __u32 type,
585 struct thandle *handle,
586 struct lustre_capa *capa)
588 struct dt_object *next = mdd_object_child(pobj);
592 if (dt_try_as_dir(env, next)) {
593 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
594 struct lu_ucred *uc = lu_ucred_check(env);
598 rec->rec_type = type;
599 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
600 rc = dt_insert(env, next, (const struct dt_rec *)rec,
601 (const struct dt_key *)name, handle, capa,
609 /* insert named index, add reference if isdir */
610 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
611 const struct lu_fid *lf, __u32 type,
612 const char *name, struct thandle *handle,
613 struct lustre_capa *capa)
618 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle, capa);
619 if (rc == 0 && S_ISDIR(type)) {
620 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
621 mdo_ref_add(env, pobj, handle);
622 mdd_write_unlock(env, pobj);
628 /* delete named index, drop reference if isdir */
629 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
630 const char *name, int is_dir, struct thandle *handle,
631 struct lustre_capa *capa)
636 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
637 if (rc == 0 && is_dir) {
638 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
639 mdo_ref_del(env, pobj, handle);
640 mdd_write_unlock(env, pobj);
646 static int mdd_llog_record_calc_size(const struct lu_env *env,
647 const struct lu_name *tname,
648 const struct lu_name *sname)
650 const struct lu_ucred *uc = lu_ucred(env);
651 enum changelog_rec_flags crf = 0;
652 size_t hdr_size = sizeof(struct llog_changelog_rec) -
653 sizeof(struct changelog_rec);
658 if (uc != NULL && uc->uc_jobid[0] != '\0')
661 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
662 (tname != NULL ? tname->ln_namelen : 0) +
663 (sname != NULL ? 1 + sname->ln_namelen : 0));
666 int mdd_declare_changelog_store(const struct lu_env *env,
667 struct mdd_device *mdd,
668 const struct lu_name *tname,
669 const struct lu_name *sname,
670 struct thandle *handle)
672 struct obd_device *obd = mdd2obd_dev(mdd);
673 struct llog_ctxt *ctxt;
674 struct llog_changelog_rec *rec;
680 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
683 reclen = mdd_llog_record_calc_size(env, tname, sname);
684 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
685 if (buf->lb_buf == NULL)
689 rec->cr_hdr.lrh_len = reclen;
690 rec->cr_hdr.lrh_type = CHANGELOG_REC;
692 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
696 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
702 /** Add a changelog entry \a rec to the changelog llog
705 * \param handle - currently ignored since llogs start their own transaction;
706 * this will hopefully be fixed in llog rewrite
709 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
710 struct llog_changelog_rec *rec, struct thandle *th)
712 struct obd_device *obd = mdd2obd_dev(mdd);
713 struct llog_ctxt *ctxt;
716 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
717 changelog_rec_varsize(&rec->cr));
719 /* llog_lvfs_write_rec sets the llog tail len */
720 rec->cr_hdr.lrh_type = CHANGELOG_REC;
721 rec->cr.cr_time = cl_time();
723 spin_lock(&mdd->mdd_cl.mc_lock);
724 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
725 * but as long as the MDD transactions are ordered correctly for e.g.
726 * rename conflicts, I don't think this should matter. */
727 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
728 spin_unlock(&mdd->mdd_cl.mc_lock);
730 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
734 /* nested journal transaction */
735 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, th);
743 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
744 const struct lu_fid *sfid,
745 const struct lu_fid *spfid,
746 const struct lu_name *sname)
748 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
749 size_t extsize = sname->ln_namelen + 1;
751 LASSERT(sfid != NULL);
752 LASSERT(spfid != NULL);
753 LASSERT(sname != NULL);
755 rnm->cr_sfid = *sfid;
756 rnm->cr_spfid = *spfid;
758 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
759 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
760 rec->cr_namelen += extsize;
763 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
765 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
767 if (jobid == NULL || jobid[0] == '\0')
770 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
773 /** Store a namespace change changelog record
774 * If this fails, we must fail the whole transaction; we don't
775 * want the change to commit without the log entry.
776 * \param target - mdd_object of change
777 * \param tpfid - target parent dir/object fid
778 * \param sfid - source object fid
779 * \param spfid - source parent fid
780 * \param tname - target name string
781 * \param sname - source name string
782 * \param handle - transacion handle
784 int mdd_changelog_ns_store(const struct lu_env *env,
785 struct mdd_device *mdd,
786 enum changelog_rec_type type,
787 enum changelog_rec_flags crf,
788 struct mdd_object *target,
789 const struct lu_fid *tpfid,
790 const struct lu_fid *sfid,
791 const struct lu_fid *spfid,
792 const struct lu_name *tname,
793 const struct lu_name *sname,
794 struct thandle *handle)
796 const struct lu_ucred *uc = lu_ucred(env);
797 struct llog_changelog_rec *rec;
804 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
807 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
810 LASSERT(tpfid != NULL);
811 LASSERT(tname != NULL);
812 LASSERT(handle != NULL);
814 reclen = mdd_llog_record_calc_size(env, tname, sname);
815 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
816 if (buf->lb_buf == NULL)
820 crf = (crf & CLF_FLAGMASK);
822 if (uc->uc_jobid[0] != '\0')
830 rec->cr.cr_flags = crf;
831 rec->cr.cr_type = (__u32)type;
832 rec->cr.cr_pfid = *tpfid;
833 rec->cr.cr_namelen = tname->ln_namelen;
834 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
836 if (crf & CLF_RENAME)
837 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
840 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
842 if (likely(target != NULL)) {
843 rec->cr.cr_tfid = *mdo2fid(target);
844 target->mod_cltime = cfs_time_current_64();
846 fid_zero(&rec->cr.cr_tfid);
849 rc = mdd_changelog_store(env, mdd, rec, handle);
851 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
852 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
859 static int __mdd_links_add(const struct lu_env *env,
860 struct mdd_object *mdd_obj,
861 struct linkea_data *ldata,
862 const struct lu_name *lname,
863 const struct lu_fid *pfid,
864 int first, int check)
868 if (ldata->ld_leh == NULL) {
869 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
873 rc = linkea_data_new(ldata,
874 &mdd_env_info(env)->mti_link_buf);
881 rc = linkea_links_find(ldata, lname, pfid);
882 if (rc && rc != -ENOENT)
888 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
889 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
893 linkea_add_buf(ldata, lname, tfid);
896 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
897 linkea_add_buf(ldata, lname, pfid);
899 return linkea_add_buf(ldata, lname, pfid);
902 static int __mdd_links_del(const struct lu_env *env,
903 struct mdd_object *mdd_obj,
904 struct linkea_data *ldata,
905 const struct lu_name *lname,
906 const struct lu_fid *pfid)
910 if (ldata->ld_leh == NULL) {
911 rc = mdd_links_read(env, mdd_obj, ldata);
916 rc = linkea_links_find(ldata, lname, pfid);
920 linkea_del_buf(ldata, lname);
924 static int mdd_linkea_prepare(const struct lu_env *env,
925 struct mdd_object *mdd_obj,
926 const struct lu_fid *oldpfid,
927 const struct lu_name *oldlname,
928 const struct lu_fid *newpfid,
929 const struct lu_name *newlname,
930 int first, int check,
931 struct linkea_data *ldata)
937 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
940 LASSERT(oldpfid != NULL || newpfid != NULL);
942 if (mdd_obj->mod_flags & DEAD_OBJ) {
943 /* Prevent linkea to be updated which is NOT necessary. */
944 ldata->ld_reclen = 0;
945 /* No more links, don't bother */
949 if (oldpfid != NULL) {
950 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
953 (rc != -ENODATA && rc != -ENOENT))
955 /* No changes done. */
960 /* If renaming, add the new record */
961 if (newpfid != NULL) {
962 /* even if the add fails, we still delete the out-of-date
964 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
968 rc = rc != 0 ? rc : rc2;
973 int mdd_links_rename(const struct lu_env *env,
974 struct mdd_object *mdd_obj,
975 const struct lu_fid *oldpfid,
976 const struct lu_name *oldlname,
977 const struct lu_fid *newpfid,
978 const struct lu_name *newlname,
979 struct thandle *handle,
980 struct linkea_data *ldata,
981 int first, int check)
987 ldata = &mdd_env_info(env)->mti_link_data;
988 memset(ldata, 0, sizeof(*ldata));
989 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
990 newpfid, newlname, first, check,
996 if (ldata->ld_reclen != 0)
997 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1002 if (rc == -EOVERFLOW || rc == -ENOSPC)
1004 if (oldpfid == NULL)
1005 CDEBUG(error ? D_ERROR : D_OTHER,
1006 "link_ea add '%.*s' failed %d "DFID"\n",
1007 newlname->ln_namelen, newlname->ln_name,
1008 rc, PFID(mdd_object_fid(mdd_obj)));
1009 else if (newpfid == NULL)
1010 CDEBUG(error ? D_ERROR : D_OTHER,
1011 "link_ea del '%.*s' failed %d "DFID"\n",
1012 oldlname->ln_namelen, oldlname->ln_name,
1013 rc, PFID(mdd_object_fid(mdd_obj)));
1015 CDEBUG(error ? D_ERROR : D_OTHER,
1016 "link_ea rename '%.*s'->'%.*s' failed %d "
1018 oldlname->ln_namelen, oldlname->ln_name,
1019 newlname->ln_namelen, newlname->ln_name,
1020 rc, PFID(mdd_object_fid(mdd_obj)));
1023 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1024 /* if we vmalloced a large buffer drop it */
1025 lu_buf_free(ldata->ld_buf);
1030 static inline int mdd_links_add(const struct lu_env *env,
1031 struct mdd_object *mdd_obj,
1032 const struct lu_fid *pfid,
1033 const struct lu_name *lname,
1034 struct thandle *handle,
1035 struct linkea_data *ldata, int first)
1037 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1038 pfid, lname, handle, ldata, first, 0);
1041 static inline int mdd_links_del(const struct lu_env *env,
1042 struct mdd_object *mdd_obj,
1043 const struct lu_fid *pfid,
1044 const struct lu_name *lname,
1045 struct thandle *handle)
1047 return mdd_links_rename(env, mdd_obj, pfid, lname,
1048 NULL, NULL, handle, NULL, 0, 0);
1051 /** Read the link EA into a temp buffer.
1052 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1053 * A pointer to the buffer is stored in \a ldata::ld_buf.
1055 * \retval 0 or error
1057 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1058 struct linkea_data *ldata)
1062 if (!mdd_object_exists(mdd_obj))
1065 /* First try a small buf */
1066 LASSERT(env != NULL);
1067 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1069 if (ldata->ld_buf->lb_buf == NULL)
1072 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1074 if (rc == -ERANGE) {
1075 /* Buf was too small, figure out what we need. */
1076 lu_buf_free(ldata->ld_buf);
1077 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1078 XATTR_NAME_LINK, BYPASS_CAPA);
1081 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1082 if (ldata->ld_buf->lb_buf == NULL)
1084 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1085 XATTR_NAME_LINK, BYPASS_CAPA);
1088 lu_buf_free(ldata->ld_buf);
1089 ldata->ld_buf = NULL;
1093 return linkea_init(ldata);
1096 /** Read the link EA into a temp buffer.
1097 * Uses the name_buf since it is generally large.
1098 * \retval IS_ERR err
1099 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1101 struct lu_buf *mdd_links_get(const struct lu_env *env,
1102 struct mdd_object *mdd_obj)
1104 struct linkea_data ldata = { 0 };
1107 rc = mdd_links_read(env, mdd_obj, &ldata);
1108 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1111 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1112 struct linkea_data *ldata, struct thandle *handle)
1114 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1115 ldata->ld_leh->leh_len);
1118 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1121 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1122 mdd_object_capa(env, mdd_obj));
1123 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1124 mdd_object_remote(mdd_obj) == 0) {
1125 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1127 /* XXX: If the linkEA is overflow, then we need to notify the
1128 * namespace LFSCK to skip "nlink" attribute verification
1129 * on this object to avoid the "nlink" to be shrinked by
1130 * wrong. It may be not good an interaction with LFSCK
1131 * like this. We will consider to replace it with other
1132 * mechanism in future. LU-5802. */
1133 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1134 LFSCK_TYPE_NAMESPACE);
1135 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1142 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1143 struct thandle *handle, struct linkea_data *ldata,
1144 enum mdd_links_add_overflow overflow)
1150 if (ldata != NULL && ldata->ld_leh != NULL) {
1151 ea_len = ldata->ld_leh->leh_len;
1152 linkea = ldata->ld_buf->lb_buf;
1154 ea_len = DEFAULT_LINKEA_SIZE;
1158 /* XXX: max size? */
1159 rc = mdo_declare_xattr_set(env, mdd_obj,
1160 mdd_buf_get_const(env, linkea, ea_len),
1161 XATTR_NAME_LINK, 0, handle);
1165 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1166 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1168 /* XXX: If the linkEA is overflow, then we need to notify the
1169 * namespace LFSCK to skip "nlink" attribute verification
1170 * on this object to avoid the "nlink" to be shrinked by
1171 * wrong. It may be not good an interaction with LFSCK
1172 * like this. We will consider to replace it with other
1173 * mechanism in future. LU-5802. */
1174 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1175 LFSCK_TYPE_NAMESPACE);
1176 rc = lfsck_in_notify(env,
1177 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1184 static inline int mdd_declare_links_del(const struct lu_env *env,
1185 struct mdd_object *c,
1186 struct thandle *handle)
1190 /* For directory, the linkEA will be removed together
1191 * with the object. */
1192 if (!S_ISDIR(mdd_object_type(c)))
1193 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1198 static int mdd_declare_link(const struct lu_env *env,
1199 struct mdd_device *mdd,
1200 struct mdd_object *p,
1201 struct mdd_object *c,
1202 const struct lu_name *name,
1203 struct thandle *handle,
1205 struct linkea_data *data)
1209 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1210 name->ln_name, handle);
1214 rc = mdo_declare_ref_add(env, c, handle);
1218 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1219 rc = mdo_declare_ref_add(env, c, handle);
1224 la->la_valid = LA_CTIME | LA_MTIME;
1225 rc = mdo_declare_attr_set(env, p, la, handle);
1229 la->la_valid = LA_CTIME;
1230 rc = mdo_declare_attr_set(env, c, la, handle);
1234 rc = mdd_declare_links_add(env, c, handle, data,
1235 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1239 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1244 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1245 struct md_object *src_obj, const struct lu_name *lname,
1248 const char *name = lname->ln_name;
1249 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1250 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1251 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1252 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1253 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1254 struct mdd_device *mdd = mdo2mdd(src_obj);
1255 struct thandle *handle;
1256 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1260 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1264 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1268 handle = mdd_trans_create(env, mdd);
1270 GOTO(out_pending, rc = PTR_ERR(handle));
1272 memset(ldata, 0, sizeof(*ldata));
1274 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1275 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1277 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1282 rc = mdd_trans_start(env, mdd, handle);
1286 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1287 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1290 GOTO(out_unlock, rc);
1292 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1293 rc = mdo_ref_add(env, mdd_sobj, handle);
1295 GOTO(out_unlock, rc);
1298 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1299 rc = mdo_ref_add(env, mdd_sobj, handle);
1301 GOTO(out_unlock, rc);
1304 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1305 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1308 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1309 mdd_object_type(mdd_sobj),
1311 mdd_object_capa(env, mdd_tobj));
1313 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1314 mdd_object_type(mdd_sobj),
1316 mdd_object_capa(env, mdd_tobj));
1320 mdo_ref_del(env, mdd_sobj, handle);
1321 GOTO(out_unlock, rc);
1324 la->la_valid = LA_CTIME | LA_MTIME;
1325 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1327 GOTO(out_unlock, rc);
1329 la->la_valid = LA_CTIME;
1330 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1332 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1333 mdo2fid(mdd_tobj), lname, 0, 0,
1336 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1337 lname, handle, ldata, 0);
1338 /* The failure of links_add should not cause the link
1339 * failure, reset rc here */
1344 mdd_write_unlock(env, mdd_sobj);
1346 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1347 mdo2fid(mdd_tobj), NULL, NULL,
1348 lname, NULL, handle);
1350 mdd_trans_stop(env, mdd, rc, handle);
1352 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1353 /* if we vmalloced a large buffer drop it */
1354 lu_buf_free(ldata->ld_buf);
1359 static int mdd_mark_dead_object(const struct lu_env *env,
1360 struct mdd_object *obj, struct thandle *handle,
1363 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1367 obj->mod_flags |= DEAD_OBJ;
1369 if (!S_ISDIR(mdd_object_type(obj)))
1372 attr->la_valid = LA_FLAGS;
1373 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1376 rc = mdo_declare_attr_set(env, obj, attr, handle);
1378 rc = mdo_attr_set(env, obj, attr, handle,
1379 mdd_object_capa(env, obj));
1384 static int mdd_declare_finish_unlink(const struct lu_env *env,
1385 struct mdd_object *obj,
1386 struct thandle *handle)
1390 rc = mdd_mark_dead_object(env, obj, handle, true);
1394 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1398 rc = mdo_declare_destroy(env, obj, handle);
1402 return mdd_declare_links_del(env, obj, handle);
1405 /* caller should take a lock before calling */
1406 int mdd_finish_unlink(const struct lu_env *env,
1407 struct mdd_object *obj, struct md_attr *ma,
1408 const struct mdd_object *pobj,
1409 const struct lu_name *lname,
1413 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1416 LASSERT(mdd_write_locked(env, obj) != 0);
1418 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1419 rc = mdd_mark_dead_object(env, obj, th, false);
1423 /* add new orphan and the object
1424 * will be deleted during mdd_close() */
1425 if (obj->mod_count) {
1426 rc = __mdd_orphan_add(env, obj, th);
1428 CDEBUG(D_HA, "Object "DFID" is inserted into "
1429 "orphan list, open count = %d\n",
1430 PFID(mdd_object_fid(obj)),
1433 CERROR("Object "DFID" fail to be an orphan, "
1434 "open count = %d, maybe cause failed "
1436 PFID(mdd_object_fid(obj)),
1439 rc = mdo_destroy(env, obj, th);
1441 } else if (!is_dir) {
1442 /* old files may not have link ea; ignore errors */
1443 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1451 * has mdd_write_lock on cobj already, but not on pobj yet
1453 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1454 const struct lu_attr *pattr,
1455 struct mdd_object *cobj,
1456 const struct lu_attr *cattr)
1461 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1466 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1467 struct mdd_object *p, struct mdd_object *c,
1468 const struct lu_name *name, struct md_attr *ma,
1469 struct thandle *handle, int no_name, int is_dir)
1471 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1474 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1475 if (likely(no_name == 0)) {
1476 rc = mdo_declare_index_delete(env, p, name->ln_name,
1483 rc = mdo_declare_ref_del(env, p, handle);
1489 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1490 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1491 la->la_valid = LA_CTIME | LA_MTIME;
1492 rc = mdo_declare_attr_set(env, p, la, handle);
1497 rc = mdo_declare_ref_del(env, c, handle);
1501 rc = mdo_declare_ref_del(env, c, handle);
1505 la->la_valid = LA_CTIME;
1506 rc = mdo_declare_attr_set(env, c, la, handle);
1510 rc = mdd_declare_finish_unlink(env, c, handle);
1514 /* FIXME: need changelog for remove entry */
1515 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1522 * test if a file has an HSM archive
1523 * if HSM attributes are not found in ma update them from
1526 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1527 struct mdd_object *obj,
1532 if (!(ma->ma_valid & MA_HSM)) {
1533 /* no HSM MD provided, read xattr */
1534 struct lu_buf *hsm_buf;
1535 const size_t buflen = sizeof(struct hsm_attrs);
1538 hsm_buf = mdd_buf_get(env, NULL, 0);
1539 lu_buf_alloc(hsm_buf, buflen);
1540 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1541 mdd_object_capa(env, obj));
1542 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1543 lu_buf_free(hsm_buf);
1547 ma->ma_valid = MA_HSM;
1549 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1555 * Delete name entry and the object.
1556 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1557 * does not exist for this object, and it could only happen during resending
1558 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1559 * is also needed in this case(needed by changelog), so we have to add another
1560 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1561 * the ENOENT failure should be able to be fixed by redo mechanism.
1563 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1564 struct md_object *cobj, const struct lu_name *lname,
1565 struct md_attr *ma, int no_name)
1567 const char *name = lname->ln_name;
1568 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1569 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1570 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1571 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1572 struct mdd_object *mdd_cobj = NULL;
1573 struct mdd_device *mdd = mdo2mdd(pobj);
1574 struct thandle *handle;
1578 /* cobj == NULL means only delete name entry */
1579 if (likely(cobj != NULL)) {
1580 mdd_cobj = md2mdd_obj(cobj);
1581 if (mdd_object_exists(mdd_cobj) == 0)
1585 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1589 if (likely(mdd_cobj != NULL)) {
1591 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1595 is_dir = S_ISDIR(cattr->la_mode);
1598 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1602 handle = mdd_trans_create(env, mdd);
1604 RETURN(PTR_ERR(handle));
1606 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1607 lname, ma, handle, no_name, is_dir);
1611 rc = mdd_trans_start(env, mdd, handle);
1615 if (likely(mdd_cobj != NULL))
1616 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1618 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1619 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1620 mdd_object_capa(env, mdd_pobj));
1625 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1626 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1627 GOTO(cleanup, rc = 0);
1629 if (likely(mdd_cobj != NULL)) {
1630 rc = mdo_ref_del(env, mdd_cobj, handle);
1632 __mdd_index_insert_only(env, mdd_pobj,
1634 mdd_object_type(mdd_cobj),
1636 mdd_object_capa(env, mdd_pobj));
1642 mdo_ref_del(env, mdd_cobj, handle);
1644 /* fetch updated nlink */
1645 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1650 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1651 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1653 la->la_valid = LA_CTIME | LA_MTIME;
1654 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1658 /* Enough for only unlink the entry */
1659 if (unlikely(mdd_cobj == NULL))
1662 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1663 /* update ctime of an unlinked file only if it is still
1664 * opened or a link still exists */
1665 la->la_valid = LA_CTIME;
1666 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1671 /* XXX: this transfer to ma will be removed with LOD/OSP */
1672 ma->ma_attr = *cattr;
1673 ma->ma_valid |= MA_INODE;
1674 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1676 /* fetch updated nlink */
1678 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1680 /* if object is removed then we can't get its attrs, use last get */
1681 if (cattr->la_nlink == 0) {
1682 ma->ma_attr = *cattr;
1683 ma->ma_valid |= MA_INODE;
1687 if (likely(mdd_cobj != NULL))
1688 mdd_write_unlock(env, mdd_cobj);
1693 if (cattr->la_nlink == 0) {
1694 cl_flags |= CLF_UNLINK_LAST;
1695 /* search for an existing archive */
1696 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1697 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1700 rc = mdd_changelog_ns_store(env, mdd,
1701 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1702 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1707 mdd_trans_stop(env, mdd, rc, handle);
1713 * The permission has been checked when obj created, no need check again.
1715 static int mdd_cd_sanity_check(const struct lu_env *env,
1716 struct mdd_object *obj)
1721 if (!obj || mdd_is_dead_obj(obj))
1727 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1728 struct md_object *cobj, const struct md_op_spec *spec,
1731 struct mdd_device *mdd = mdo2mdd(cobj);
1732 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1733 struct mdd_object *son = md2mdd_obj(cobj);
1734 struct thandle *handle;
1735 const struct lu_buf *buf;
1736 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1737 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1741 rc = mdd_cd_sanity_check(env, son);
1745 if (!md_should_create(spec->sp_cr_flags))
1749 * there are following use cases for this function:
1750 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1751 * striping can be specified or not
1754 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1758 /* calling ->ah_make_hint() is used to transfer information from parent */
1759 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1761 handle = mdd_trans_create(env, mdd);
1763 GOTO(out_free, rc = PTR_ERR(handle));
1766 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1767 * Should this be fixed?
1769 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1770 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1771 spec->sp_cr_flags, spec->no_create);
1773 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1774 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1775 spec->u.sp_ea.eadatalen);
1780 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1781 XATTR_NAME_LOV, 0, handle);
1785 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1789 rc = mdd_trans_start(env, mdd, handle);
1793 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1794 0, handle, mdd_object_capa(env, son));
1799 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1802 mdd_trans_stop(env, mdd, rc, handle);
1807 static int mdd_declare_object_initialize(const struct lu_env *env,
1808 struct mdd_object *parent,
1809 struct mdd_object *child,
1810 struct lu_attr *attr,
1811 struct thandle *handle)
1817 * inode mode has been set in creation time, and it's based on umask,
1818 * la_mode and acl, don't set here again! (which will go wrong
1819 * because below function doesn't consider umask).
1820 * I'd suggest set all object attributes in creation time, see above.
1822 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1823 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1824 rc = mdo_declare_attr_set(env, child, attr, handle);
1825 attr->la_valid |= LA_MODE | LA_TYPE;
1826 if (rc != 0 || !S_ISDIR(attr->la_mode))
1829 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1834 rc = mdo_declare_ref_add(env, child, handle);
1838 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1844 static int mdd_object_initialize(const struct lu_env *env,
1845 const struct lu_fid *pfid,
1846 struct mdd_object *child,
1847 struct lu_attr *attr, struct thandle *handle,
1848 const struct md_op_spec *spec)
1854 * Update attributes for child.
1857 * (1) the valid bits should be converted between Lustre and Linux;
1858 * (2) maybe, the child attributes should be set in OSD when creation.
1861 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1862 /* arguments are supposed to stay the same */
1863 if (S_ISDIR(attr->la_mode)) {
1864 /* Add "." and ".." for newly created dir */
1865 mdo_ref_add(env, child, handle);
1866 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1867 S_IFDIR, dot, handle, BYPASS_CAPA);
1869 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1873 mdo_ref_del(env, child, handle);
1880 * This function checks whether it can create a file/dir under the
1881 * directory(@pobj). The directory(@pobj) is not being locked by
1884 * \param[in] env execution environment
1885 * \param[in] pobj the directory to create files
1886 * \param[in] pattr the attributes of the directory
1887 * \param[in] lname the name of the created file/dir
1888 * \param[in] cattr the attributes of the file/dir
1889 * \param[in] spec create specification
1891 * \retval = 0 it is allowed to create file/dir under
1893 * \retval negative error not allowed to create file/dir
1894 * under the directory
1896 static int mdd_create_sanity_check(const struct lu_env *env,
1897 struct md_object *pobj,
1898 const struct lu_attr *pattr,
1899 const struct lu_name *lname,
1900 struct lu_attr *cattr,
1901 struct md_op_spec *spec)
1903 struct mdd_thread_info *info = mdd_env_info(env);
1904 struct lu_fid *fid = &info->mti_fid;
1905 struct mdd_object *obj = md2mdd_obj(pobj);
1906 struct mdd_device *m = mdo2mdd(pobj);
1907 bool check_perm = true;
1912 if (mdd_is_dead_obj(obj))
1916 * In some cases this lookup is not needed - we know before if name
1917 * exists or not because MDT performs lookup for it.
1918 * name length check is done in lookup.
1920 if (spec->sp_cr_lookup) {
1922 * Check if the name already exist, though it will be checked in
1923 * _index_insert also, for avoiding rolling back if exists
1926 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1927 MAY_WRITE | MAY_EXEC);
1929 RETURN(rc ? : -EEXIST);
1931 /* Permission is already being checked in mdd_lookup */
1935 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1940 if (pattr->la_mode & S_ISGID) {
1941 cattr->la_gid = pattr->la_gid;
1942 if (S_ISDIR(cattr->la_mode)) {
1943 cattr->la_mode |= S_ISGID;
1944 cattr->la_valid |= LA_MODE;
1948 rc = mdd_name_check(m, lname);
1952 switch (cattr->la_mode & S_IFMT) {
1954 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1956 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1957 RETURN(-ENAMETOOLONG);
1976 static int mdd_declare_object_create(const struct lu_env *env,
1977 struct mdd_device *mdd,
1978 struct mdd_object *p, struct mdd_object *c,
1979 struct lu_attr *attr,
1980 struct thandle *handle,
1981 const struct md_op_spec *spec,
1982 struct lu_buf *def_acl_buf,
1983 struct lu_buf *acl_buf,
1984 struct dt_allocation_hint *hint)
1988 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1993 #ifdef CONFIG_FS_POSIX_ACL
1994 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1995 /* if dir, then can inherit default ACl */
1996 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1997 XATTR_NAME_ACL_DEFAULT,
2003 if (acl_buf->lb_len > 0) {
2004 rc = mdo_declare_attr_set(env, c, attr, handle);
2008 rc = mdo_declare_xattr_set(env, c, acl_buf,
2009 XATTR_NAME_ACL_ACCESS, 0, handle);
2014 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2018 /* replay case, create LOV EA from client data */
2019 if (spec->no_create ||
2020 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2021 const struct lu_buf *buf;
2023 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2024 spec->u.sp_ea.eadatalen);
2025 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2031 if (S_ISLNK(attr->la_mode)) {
2032 const char *target_name = spec->u.sp_symname;
2033 int sym_len = strlen(target_name);
2034 const struct lu_buf *buf;
2036 buf = mdd_buf_get_const(env, target_name, sym_len);
2037 rc = dt_declare_record_write(env, mdd_object_child(c),
2046 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2047 struct mdd_object *p, struct mdd_object *c,
2048 const struct lu_name *name,
2049 struct lu_attr *attr,
2050 struct thandle *handle,
2051 const struct md_op_spec *spec,
2052 struct linkea_data *ldata,
2053 struct lu_buf *def_acl_buf,
2054 struct lu_buf *acl_buf,
2055 struct dt_allocation_hint *hint)
2059 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2060 def_acl_buf, acl_buf, hint);
2064 if (S_ISDIR(attr->la_mode)) {
2065 rc = mdo_declare_ref_add(env, p, handle);
2070 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2071 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2075 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2077 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2078 name->ln_name, handle);
2082 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2087 la->la_valid = LA_CTIME | LA_MTIME;
2088 rc = mdo_declare_attr_set(env, p, la, handle);
2092 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2097 /* XXX: For remote create, it should indicate the remote RPC
2098 * will be sent after local transaction is finished, which
2099 * is not very nice, but it will be removed once we fully support
2101 if (mdd_object_remote(p) && handle->th_update != NULL)
2102 handle->th_update->tu_sent_after_local_trans = 1;
2107 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2108 struct lu_attr *la, struct lu_buf *def_acl_buf,
2109 struct lu_buf *acl_buf)
2114 if (S_ISLNK(la->la_mode)) {
2115 acl_buf->lb_len = 0;
2116 def_acl_buf->lb_len = 0;
2120 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2121 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2122 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
2123 mdd_read_unlock(env, pobj);
2125 /* If there are default ACL, fix mode/ACL by default ACL */
2126 def_acl_buf->lb_len = rc;
2127 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2128 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2129 acl_buf->lb_len = rc;
2130 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2133 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2134 /* If there are no default ACL, fix mode by mask */
2135 struct lu_ucred *uc = lu_ucred(env);
2137 /* The create triggered by MDT internal events, such as
2138 * LFSCK reset, will not contain valid "uc". */
2139 if (unlikely(uc != NULL))
2140 la->la_mode &= ~uc->uc_umask;
2142 acl_buf->lb_len = 0;
2143 def_acl_buf->lb_len = 0;
2150 * Create a metadata object and initialize it, set acl, xattr.
2152 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2153 struct mdd_object *son, struct lu_attr *attr,
2154 struct md_op_spec *spec, struct lu_buf *acl_buf,
2155 struct lu_buf *def_acl_buf,
2156 struct dt_allocation_hint *hint,
2157 struct thandle *handle)
2161 mdd_write_lock(env, son, MOR_TGT_CHILD);
2162 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2167 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2168 * created in declare phase, they also needs to be added to master
2169 * object as sub-directory entry. So it has to initialize the master
2170 * object, then set dir striped EA.(in mdo_xattr_set) */
2171 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2174 GOTO(err_destroy, rc);
2177 * in case of replay we just set LOVEA provided by the client
2178 * XXX: I think it would be interesting to try "old" way where
2179 * MDT calls this xattr_set(LOV) in a different transaction.
2180 * probably this way we code can be made better.
2183 /* During creation, there are only a few cases we need do xattr_set to
2185 * 1. regular file: see comments above.
2186 * 2. create striped directory with provided stripeEA.
2187 * 3. create striped directory because inherit default layout from the
2189 if (spec->no_create ||
2190 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2191 S_ISDIR(attr->la_mode)) {
2192 const struct lu_buf *buf;
2194 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2195 spec->u.sp_ea.eadatalen);
2196 rc = mdo_xattr_set(env, son, buf,
2197 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2199 handle, BYPASS_CAPA);
2201 GOTO(err_destroy, rc);
2204 #ifdef CONFIG_FS_POSIX_ACL
2205 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2206 S_ISDIR(attr->la_mode)) {
2207 /* set default acl */
2208 rc = mdo_xattr_set(env, son, def_acl_buf,
2209 XATTR_NAME_ACL_DEFAULT, 0,
2210 handle, BYPASS_CAPA);
2212 GOTO(err_destroy, rc);
2214 /* set its own acl */
2215 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2216 rc = mdo_xattr_set(env, son, acl_buf,
2217 XATTR_NAME_ACL_ACCESS,
2218 0, handle, BYPASS_CAPA);
2220 GOTO(err_destroy, rc);
2224 if (S_ISLNK(attr->la_mode)) {
2225 struct lu_ucred *uc = lu_ucred_assert(env);
2226 struct dt_object *dt = mdd_object_child(son);
2227 const char *target_name = spec->u.sp_symname;
2228 int sym_len = strlen(target_name);
2229 const struct lu_buf *buf;
2232 buf = mdd_buf_get_const(env, target_name, sym_len);
2233 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2234 mdd_object_capa(env, son),
2236 CFS_CAP_SYS_RESOURCE_MASK);
2241 GOTO(err_initlized, rc = -EFAULT);
2245 if (unlikely(rc != 0)) {
2247 if (S_ISDIR(attr->la_mode)) {
2248 /* Drop the reference, no need to delete "."/"..",
2249 * because the object to be destroied directly. */
2250 rc2 = mdo_ref_del(env, son, handle);
2254 rc2 = mdo_ref_del(env, son, handle);
2258 mdo_destroy(env, son, handle);
2261 mdd_write_unlock(env, son);
2266 * Create object and insert it into namespace.
2268 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2269 const struct lu_name *lname, struct md_object *child,
2270 struct md_op_spec *spec, struct md_attr* ma)
2272 struct mdd_thread_info *info = mdd_env_info(env);
2273 struct lu_attr *la = &info->mti_la_for_fix;
2274 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2275 struct mdd_object *son = md2mdd_obj(child);
2276 struct mdd_device *mdd = mdo2mdd(pobj);
2277 struct lu_attr *attr = &ma->ma_attr;
2278 struct thandle *handle;
2279 struct lu_attr *pattr = &info->mti_pattr;
2280 struct lu_buf acl_buf;
2281 struct lu_buf def_acl_buf;
2282 struct linkea_data *ldata = &info->mti_link_data;
2283 const char *name = lname->ln_name;
2284 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2290 * Two operations have to be performed:
2292 * - an allocation of a new object (->do_create()), and
2294 * - an insertion into a parent index (->dio_insert()).
2296 * Due to locking, operation order is not important, when both are
2297 * successful, *but* error handling cases are quite different:
2299 * - if insertion is done first, and following object creation fails,
2300 * insertion has to be rolled back, but this operation might fail
2301 * also leaving us with dangling index entry.
2303 * - if creation is done first, is has to be undone if insertion
2304 * fails, leaving us with leaked space, which is neither good, nor
2307 * It seems that creation-first is simplest solution, but it is
2308 * sub-optimal in the frequent
2313 * case, because second mkdir is bound to create object, only to
2314 * destroy it immediately.
2316 * To avoid this follow local file systems that do double lookup:
2318 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2320 * 1. create (mdd_object_create_internal())
2322 * 2. insert (__mdd_index_insert(), lookup again)
2325 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2329 /* Sanity checks before big job. */
2330 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2334 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2335 GOTO(out_free, rc = -EINPROGRESS);
2337 handle = mdd_trans_create(env, mdd);
2339 GOTO(out_free, rc = PTR_ERR(handle));
2341 acl_buf.lb_buf = info->mti_xattr_buf;
2342 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2343 def_acl_buf.lb_buf = info->mti_key;
2344 def_acl_buf.lb_len = sizeof(info->mti_key);
2345 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2349 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2351 memset(ldata, 0, sizeof(*ldata));
2352 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT2)) {
2353 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2356 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2357 &tfid, lname, 1, 0, ldata);
2359 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2360 mdd_object_fid(mdd_pobj),
2361 lname, 1, 0, ldata);
2364 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2365 handle, spec, ldata, &def_acl_buf, &acl_buf,
2370 rc = mdd_trans_start(env, mdd, handle);
2374 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2375 &def_acl_buf, hint, handle);
2379 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2380 mdd_write_lock(env, son, MOR_TGT_CHILD);
2381 rc = __mdd_orphan_add(env, son, handle);
2382 GOTO(out_volatile, rc);
2384 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2385 attr->la_mode, name, handle,
2386 mdd_object_capa(env, mdd_pobj));
2388 GOTO(err_created, rc);
2390 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2393 /* update parent directory mtime/ctime */
2395 la->la_valid = LA_CTIME | LA_MTIME;
2396 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2398 GOTO(err_insert, rc);
2406 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2407 rc2 = __mdd_orphan_del(env, son, handle);
2409 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2410 S_ISDIR(attr->la_mode),
2411 handle, BYPASS_CAPA);
2416 mdd_write_lock(env, son, MOR_TGT_CHILD);
2417 if (S_ISDIR(attr->la_mode)) {
2418 /* Drop the reference, no need to delete "."/"..",
2419 * because the object is to be destroyed directly. */
2420 rc2 = mdo_ref_del(env, son, handle);
2422 mdd_write_unlock(env, son);
2427 /* For volatile files drop one link immediately, since there is
2428 * no filename in the namespace, and save any error returned. */
2429 rc2 = mdo_ref_del(env, son, handle);
2431 mdd_write_unlock(env, son);
2432 if (unlikely(rc == 0))
2437 /* Don't destroy the volatile object on success */
2438 if (likely(rc != 0))
2439 mdo_destroy(env, son, handle);
2440 mdd_write_unlock(env, son);
2443 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2444 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2445 rc = mdd_changelog_ns_store(env, mdd,
2446 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2447 S_ISREG(attr->la_mode) ? CL_CREATE :
2448 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2449 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2452 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2456 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2457 /* if we vmalloced a large buffer drop it */
2458 lu_buf_free(ldata->ld_buf);
2460 /* The child object shouldn't be cached anymore */
2462 set_bit(LU_OBJECT_HEARD_BANSHEE,
2463 &child->mo_lu.lo_header->loh_flags);
2468 * Get locks on parents in proper order
2469 * RETURN: < 0 - error, rename_order if successful
2477 static int mdd_rename_order(const struct lu_env *env,
2478 struct mdd_device *mdd,
2479 struct mdd_object *src_pobj,
2480 const struct lu_attr *pattr,
2481 struct mdd_object *tgt_pobj)
2483 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2487 if (src_pobj == tgt_pobj)
2488 RETURN(MDD_RN_SAME);
2490 /* compared the parent child relationship of src_p&tgt_p */
2491 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2493 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2496 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2510 /* has not mdd_write{read}_lock on any obj yet. */
2511 static int mdd_rename_sanity_check(const struct lu_env *env,
2512 struct mdd_object *src_pobj,
2513 const struct lu_attr *pattr,
2514 struct mdd_object *tgt_pobj,
2515 const struct lu_attr *tpattr,
2516 struct mdd_object *sobj,
2517 const struct lu_attr *cattr,
2518 struct mdd_object *tobj,
2519 const struct lu_attr *tattr)
2524 /* XXX: when get here, sobj must NOT be NULL,
2525 * the other case has been processed in cld_rename
2526 * before mdd_rename and enable MDS_PERM_BYPASS. */
2529 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2533 /* XXX: when get here, "tobj == NULL" means tobj must
2534 * NOT exist (neither on remote MDS, such case has been
2535 * processed in cld_rename before mdd_rename and enable
2537 * So check may_create, but not check may_unlink. */
2539 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2540 (src_pobj != tgt_pobj));
2542 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2543 (src_pobj != tgt_pobj), 1);
2545 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2546 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2551 static int mdd_declare_rename(const struct lu_env *env,
2552 struct mdd_device *mdd,
2553 struct mdd_object *mdd_spobj,
2554 struct mdd_object *mdd_tpobj,
2555 struct mdd_object *mdd_sobj,
2556 struct mdd_object *mdd_tobj,
2557 const struct lu_name *tname,
2558 const struct lu_name *sname,
2560 struct linkea_data *ldata,
2561 struct thandle *handle)
2563 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2566 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2567 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2573 /* name from source dir */
2574 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2578 /* .. from source child */
2579 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2580 /* source child can be directory,
2581 * counted by source dir's nlink */
2582 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2585 if (mdd_spobj != mdd_tpobj) {
2586 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2591 rc = mdo_declare_index_insert(env, mdd_sobj,
2593 S_IFDIR, dotdot, handle);
2598 /* new target child can be directory,
2599 * counted by target dir's nlink */
2600 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2605 la->la_valid = LA_CTIME | LA_MTIME;
2606 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2610 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2614 la->la_valid = LA_CTIME;
2615 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2619 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2620 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2625 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2626 mdd_object_type(mdd_sobj),
2627 tname->ln_name, handle);
2631 /* name from target dir (old name), we declare it unconditionally
2632 * as mdd_rename() calls delete unconditionally as well. so just
2633 * to balance declarations vs calls to change ... */
2634 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2638 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2639 /* delete target child in target parent directory */
2640 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2644 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2645 /* target child can be directory,
2646 * delete "." reference in target child directory */
2647 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2651 /* delete ".." reference in target parent directory */
2652 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2657 la->la_valid = LA_CTIME;
2658 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2662 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2667 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2674 /* src object can be remote that is why we use only fid and type of object */
2675 static int mdd_rename(const struct lu_env *env,
2676 struct md_object *src_pobj, struct md_object *tgt_pobj,
2677 const struct lu_fid *lf, const struct lu_name *lsname,
2678 struct md_object *tobj, const struct lu_name *ltname,
2681 const char *sname = lsname->ln_name;
2682 const char *tname = ltname->ln_name;
2683 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2684 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2685 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2686 struct mdd_device *mdd = mdo2mdd(src_pobj);
2687 struct mdd_object *mdd_sobj = NULL; /* source object */
2688 struct mdd_object *mdd_tobj = NULL;
2689 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2690 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2691 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2692 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2693 struct thandle *handle;
2694 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2695 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2696 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2699 bool tobj_locked = 0;
2700 unsigned cl_flags = 0;
2705 mdd_tobj = md2mdd_obj(tobj);
2707 mdd_sobj = mdd_object_find(env, mdd, lf);
2709 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2711 GOTO(out_pending, rc);
2713 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2715 GOTO(out_pending, rc);
2718 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2720 GOTO(out_pending, rc);
2723 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2725 GOTO(out_pending, rc);
2727 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2728 mdd_sobj, cattr, mdd_tobj, tattr);
2730 GOTO(out_pending, rc);
2732 rc = mdd_name_check(mdd, ltname);
2734 GOTO(out_pending, rc);
2736 handle = mdd_trans_create(env, mdd);
2738 GOTO(out_pending, rc = PTR_ERR(handle));
2740 memset(ldata, 0, sizeof(*ldata));
2741 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2742 ltname, 1, 0, ldata);
2743 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2744 mdd_tobj, lsname, ltname, ma, ldata, handle);
2748 rc = mdd_trans_start(env, mdd, handle);
2752 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2753 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2755 GOTO(cleanup_unlocked, rc);
2757 is_dir = S_ISDIR(cattr->la_mode);
2759 /* Remove source name from source directory */
2760 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2761 mdd_object_capa(env, mdd_spobj));
2765 /* "mv dir1 dir2" needs "dir1/.." link update */
2766 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2767 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2768 mdd_object_capa(env, mdd_sobj));
2770 GOTO(fixup_spobj2, rc);
2772 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2774 mdd_object_capa(env, mdd_sobj));
2776 GOTO(fixup_spobj, rc);
2779 /* Remove target name from target directory
2780 * Here tobj can be remote one, so we do index_delete unconditionally
2781 * and -ENOENT is allowed.
2783 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2784 mdd_object_capa(env, mdd_tpobj));
2787 /* tname might been renamed to something else */
2788 GOTO(fixup_spobj, rc);
2791 GOTO(fixup_spobj, rc);
2794 /* Insert new fid with target name into target dir */
2795 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2796 tname, handle, mdd_object_capa(env, mdd_tpobj));
2798 GOTO(fixup_tpobj, rc);
2800 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2801 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2803 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2805 la->la_valid = LA_CTIME;
2806 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2808 GOTO(fixup_tpobj, rc);
2811 /* Remove old target object
2812 * For tobj is remote case cmm layer has processed
2813 * and set tobj to NULL then. So when tobj is NOT NULL,
2814 * it must be local one.
2816 if (tobj && mdd_object_exists(mdd_tobj)) {
2817 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2819 if (mdd_is_dead_obj(mdd_tobj)) {
2820 /* shld not be dead, something is wrong */
2821 CERROR("tobj is dead, something is wrong\n");
2825 mdo_ref_del(env, mdd_tobj, handle);
2827 /* Remove dot reference. */
2828 if (S_ISDIR(tattr->la_mode))
2829 mdo_ref_del(env, mdd_tobj, handle);
2832 /* fetch updated nlink */
2833 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2835 CERROR("%s: Failed to get nlink for tobj "
2837 mdd2obd_dev(mdd)->obd_name,
2838 PFID(tpobj_fid), rc);
2839 GOTO(fixup_tpobj, rc);
2842 la->la_valid = LA_CTIME;
2843 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2845 CERROR("%s: Failed to set ctime for tobj "
2847 mdd2obd_dev(mdd)->obd_name,
2848 PFID(tpobj_fid), rc);
2849 GOTO(fixup_tpobj, rc);
2852 /* XXX: this transfer to ma will be removed with LOD/OSP */
2853 ma->ma_attr = *tattr;
2854 ma->ma_valid |= MA_INODE;
2855 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2858 CERROR("%s: Failed to unlink tobj "
2860 mdd2obd_dev(mdd)->obd_name,
2861 PFID(tpobj_fid), rc);
2862 GOTO(fixup_tpobj, rc);
2865 /* fetch updated nlink */
2866 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2868 CERROR("%s: Failed to get nlink for tobj "
2870 mdd2obd_dev(mdd)->obd_name,
2871 PFID(tpobj_fid), rc);
2872 GOTO(fixup_tpobj, rc);
2874 /* XXX: this transfer to ma will be removed with LOD/OSP */
2875 ma->ma_attr = *tattr;
2876 ma->ma_valid |= MA_INODE;
2878 if (tattr->la_nlink == 0) {
2879 cl_flags |= CLF_RENAME_LAST;
2880 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2881 cl_flags |= CLF_RENAME_LAST_EXISTS;
2885 la->la_valid = LA_CTIME | LA_MTIME;
2886 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2888 GOTO(fixup_tpobj, rc);
2890 if (mdd_spobj != mdd_tpobj) {
2891 la->la_valid = LA_CTIME | LA_MTIME;
2892 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2895 if (rc == 0 && mdd_sobj) {
2896 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2897 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2898 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2901 /* Old files might not have EA entry */
2902 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2903 lsname, handle, NULL, 0);
2904 mdd_write_unlock(env, mdd_sobj);
2905 /* We don't fail the transaction if the link ea can't be
2906 updated -- fid2path will use alternate lookup method. */
2914 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2917 CWARN("tp obj fix error %d\n",rc2);
2919 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2920 !mdd_is_dead_obj(mdd_tobj)) {
2922 mdo_ref_add(env, mdd_tobj, handle);
2924 mdo_ref_add(env, mdd_tobj, handle);
2927 rc2 = __mdd_index_insert(env, mdd_tpobj,
2929 mdd_object_type(mdd_tobj),
2930 tname, handle, BYPASS_CAPA);
2932 CWARN("tp obj fix error: rc = %d\n", rc2);
2937 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2938 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2942 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2943 mdd2obd_dev(mdd)->obd_name, rc2);
2946 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2947 dotdot, handle, BYPASS_CAPA);
2949 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2950 mdd2obd_dev(mdd)->obd_name, rc2);
2955 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2956 mdd_object_type(mdd_sobj), sname,
2957 handle, BYPASS_CAPA);
2959 CWARN("sp obj fix error: rc = %d\n", rc2);
2964 mdd_write_unlock(env, mdd_tobj);
2968 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
2969 mdd_tobj, tpobj_fid, lf, spobj_fid,
2970 ltname, lsname, handle);
2973 mdd_trans_stop(env, mdd, rc, handle);
2976 mdd_object_put(env, mdd_sobj);
2981 * During migration once the parent FID has been changed,
2982 * we need update the parent FID in linkea.
2984 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2985 struct mdd_object *parent,
2986 struct mdd_object *child,
2987 const char *name, int namelen,
2988 struct thandle *handle,
2991 struct mdd_thread_info *info = mdd_env_info(env);
2992 struct linkea_data ldata = {0};
2993 struct lu_buf *buf = &info->mti_link_buf;
2999 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3000 if (buf->lb_buf == NULL)
3004 rc = mdd_links_read(env, child, &ldata);
3006 if (rc == -ENOENT || rc == -ENODATA)
3011 LASSERT(ldata.ld_leh != NULL);
3012 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3013 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3014 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3015 struct lu_name lname;
3018 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3021 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3022 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3023 ldata.ld_lee = (struct link_ea_entry *)
3024 ((char *)ldata.ld_lee +
3029 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3030 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3031 lname.ln_namelen, lname.ln_name,
3032 PFID(mdd_object_fid(parent)));
3033 /* update to the new parent fid */
3034 linkea_entry_pack(ldata.ld_lee, &lname,
3035 mdd_object_fid(parent));
3037 rc = mdd_declare_links_add(env, child, handle, &ldata,
3040 rc = mdd_links_write(env, child, &ldata, handle);
3046 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3047 struct mdd_object *parent,
3048 struct mdd_object *child,
3049 const char *name, int namelen,
3050 struct thandle *handle)
3052 return mdd_linkea_update_child_internal(env, parent, child, name,
3053 namelen, handle, true);
3056 static int mdd_linkea_update_child(const struct lu_env *env,
3057 struct mdd_object *parent,
3058 struct mdd_object *child,
3059 const char *name, int namelen,
3060 struct thandle *handle)
3062 return mdd_linkea_update_child_internal(env, parent, child, name,
3063 namelen, handle, false);
3066 static int mdd_update_linkea_internal(const struct lu_env *env,
3067 struct mdd_object *mdd_pobj,
3068 struct mdd_object *mdd_sobj,
3069 struct mdd_object *mdd_tobj,
3070 const struct lu_name *child_name,
3071 struct thandle *handle,
3074 struct mdd_thread_info *info = mdd_env_info(env);
3075 struct linkea_data *ldata = &info->mti_link_data;
3080 rc = mdd_links_read(env, mdd_sobj, ldata);
3082 if (rc == -ENOENT || rc == -ENODATA)
3088 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3091 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3096 /* If it is mulitple links file, we need update the name entry for
3098 LASSERT(ldata->ld_leh != NULL);
3099 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3100 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3101 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3102 struct mdd_object *pobj;
3103 struct lu_name lname;
3106 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3108 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3110 pobj = mdd_object_find(env, mdd, &fid);
3112 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3113 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3118 if (!mdd_object_exists(pobj)) {
3119 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3120 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3124 if (pobj == mdd_pobj &&
3125 lname.ln_namelen == child_name->ln_namelen &&
3126 strncmp(lname.ln_name, child_name->ln_name,
3127 lname.ln_namelen) == 0) {
3128 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3129 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3134 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3135 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3136 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3139 /* Remove source name from source directory */
3140 /* Insert new fid with target name into target dir */
3141 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3146 rc = mdo_declare_index_insert(env, pobj,
3147 mdd_object_fid(mdd_tobj),
3148 mdd_object_type(mdd_tobj),
3149 lname.ln_name, handle);
3153 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3157 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3161 rc = __mdd_index_delete(env, pobj, lname.ln_name,
3163 mdd_object_capa(env, pobj));
3167 rc = __mdd_index_insert(env, pobj,
3168 mdd_object_fid(mdd_tobj),
3169 mdd_object_type(mdd_tobj),
3170 lname.ln_name, handle,
3171 mdd_object_capa(env, pobj));
3175 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3176 rc = mdo_ref_add(env, mdd_tobj, handle);
3177 mdd_write_unlock(env, mdd_tobj);
3181 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3182 mdo_ref_del(env, mdd_sobj, handle);
3183 mdd_write_unlock(env, mdd_sobj);
3186 mdd_object_put(env, pobj);
3194 static int mdd_migrate_xattrs(const struct lu_env *env,
3195 struct mdd_object *mdd_sobj,
3196 struct mdd_object *mdd_tobj)
3198 struct mdd_thread_info *info = mdd_env_info(env);
3199 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3201 struct thandle *handle;
3207 struct lu_buf list_xbuf;
3210 /* retrieve xattr list from the old object */
3211 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL,
3212 mdd_object_capa(env, mdd_sobj));
3213 if (list_xsize == -ENODATA)
3219 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3220 if (info->mti_big_buf.lb_buf == NULL)
3223 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3224 list_xbuf.lb_len = list_xsize;
3225 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf,
3226 mdd_object_capa(env, mdd_sobj));
3231 xname = list_xbuf.lb_buf;
3233 xlen = strnlen(xname, rem - 1) + 1;
3234 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3235 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3236 strcmp(XATTR_NAME_LMV, xname) == 0)
3239 /* For directory, if there are default layout, migrate here */
3240 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3241 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3244 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL,
3246 mdd_object_capa(env, mdd_sobj));
3247 if (xsize == -ENODATA)
3252 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3253 if (info->mti_link_buf.lb_buf == NULL)
3254 GOTO(out, rc = -ENOMEM);
3256 xbuf.lb_len = xsize;
3257 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3258 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname,
3259 mdd_object_capa(env, mdd_sobj));
3265 handle = mdd_trans_create(env, mdd);
3267 GOTO(out, rc = PTR_ERR(handle));
3269 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3272 GOTO(stop_trans, rc);
3273 /* Note: this transaction is part of migration, and it is not
3274 * the last step of migration, so we set th_local = 1 to avoid
3275 * update last rcvd for this transaction */
3276 handle->th_local = 1;
3277 rc = mdd_trans_start(env, mdd, handle);
3279 GOTO(stop_trans, rc);
3281 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle,
3282 mdd_object_capa(env, mdd_sobj));
3284 GOTO(stop_trans, rc = 0);
3287 GOTO(stop_trans, rc);
3289 mdd_trans_stop(env, mdd, rc, handle);
3294 memmove(xname, xname + xlen, rem);
3300 static int mdd_declare_migrate_create(const struct lu_env *env,
3301 struct mdd_object *mdd_pobj,
3302 struct mdd_object *mdd_sobj,
3303 struct mdd_object *mdd_tobj,
3304 struct md_op_spec *spec,
3306 union lmv_mds_md *mgr_ea,
3307 struct thandle *handle)
3309 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3310 const struct lu_buf *buf;
3314 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3315 handle, spec, NULL);
3319 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3324 if (S_ISLNK(la->la_mode)) {
3325 const char *target_name = spec->u.sp_symname;
3326 int sym_len = strlen(target_name);
3327 const struct lu_buf *buf;
3329 buf = mdd_buf_get_const(env, target_name, sym_len);
3330 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3336 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3337 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3338 spec->u.sp_ea.eadatalen);
3339 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3345 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3346 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3347 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3352 la_flag->la_valid = LA_FLAGS;
3353 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3354 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3355 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3360 static int mdd_migrate_create(const struct lu_env *env,
3361 struct mdd_object *mdd_pobj,
3362 struct mdd_object *mdd_sobj,
3363 struct mdd_object *mdd_tobj,
3366 struct mdd_thread_info *info = mdd_env_info(env);
3367 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3368 struct md_op_spec *spec = &info->mti_spec;
3369 struct lu_buf lmm_buf = { 0 };
3370 struct lu_buf link_buf = { 0 };
3371 const struct lu_buf *buf;
3372 struct thandle *handle;
3373 struct lmv_mds_md_v1 *mgr_ea;
3374 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3375 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3380 /* prepare spec for create */
3381 memset(spec, 0, sizeof(*spec));
3382 spec->sp_cr_lookup = 0;
3383 spec->sp_feat = &dt_directory_features;
3384 if (S_ISLNK(la->la_mode)) {
3385 buf = lu_buf_check_and_alloc(
3386 &mdd_env_info(env)->mti_big_buf,
3389 link_buf.lb_len = la->la_size + 1;
3390 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3392 rc = rc != 0 ? rc : -EFAULT;
3393 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3394 mdd2obd_dev(mdd)->obd_name,
3395 PFID(mdd_object_fid(mdd_sobj)), rc);
3398 spec->u.sp_symname = link_buf.lb_buf;
3399 } else if S_ISREG(la->la_mode) {
3400 /* retrieve lov of the old object */
3401 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3402 if (rc != 0 && rc != -ENODATA)
3404 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3405 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3406 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3407 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3411 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3412 memset(mgr_ea, 0, sizeof(*mgr_ea));
3413 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3414 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3415 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3416 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3417 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3418 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3420 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3422 handle = mdd_trans_create(env, mdd);
3424 GOTO(out_free, rc = PTR_ERR(handle));
3426 /* Note: this transaction is part of migration, and it is not
3427 * the last step of migration, so we set th_local = 1 to avoid
3428 * update last rcvd for this transaction */
3429 handle->th_local = 1;
3430 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3432 (union lmv_mds_md *)info->mti_xattr_buf,
3435 GOTO(stop_trans, rc);
3437 rc = mdd_trans_start(env, mdd, handle);
3439 GOTO(stop_trans, rc);
3441 /* create the target object */
3442 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3445 GOTO(stop_trans, rc);
3447 /* Set MIGRATE EA on the source inode, so once the migration needs
3448 * to be re-done during failover, the re-do process can locate the
3449 * target object which is already being created. */
3450 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3451 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3452 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0,
3453 handle, mdd_object_capa(env, mdd_sobj));
3455 GOTO(stop_trans, rc);
3457 /* Set immutable flag, so any modification is disabled until
3458 * the migration is done. Once the migration is interrupted,
3459 * if the resume process find the migrating object has both
3460 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3461 * flag and approve the migration */
3462 la_flag->la_valid = LA_FLAGS;
3463 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3464 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3465 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3466 mdd_object_capa(env, mdd_sobj));
3469 mdd_trans_stop(env, mdd, rc, handle);
3471 if (lmm_buf.lb_buf != NULL)
3472 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3476 static int mdd_migrate_entries(const struct lu_env *env,
3477 struct mdd_object *mdd_sobj,
3478 struct mdd_object *mdd_tobj)
3480 struct dt_object *next = mdd_object_child(mdd_sobj);
3481 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3482 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3483 struct thandle *handle;
3485 const struct dt_it_ops *iops;
3488 struct lu_dirent *ent;
3491 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3495 if (!dt_try_as_dir(env, next))
3496 GOTO(out_ent, rc = -ENOTDIR);
3498 * iterate directories
3500 iops = &next->do_index_ops->dio_it;
3501 it = iops->init(env, next, LUDA_FID | LUDA_TYPE,
3502 mdd_object_capa(env, mdd_sobj));
3504 GOTO(out_ent, rc = PTR_ERR(it));
3506 rc = iops->load(env, it, 0);
3508 rc = iops->next(env, it);
3512 * At this point and across for-loop:
3514 * rc == 0 -> ok, proceed.
3515 * rc > 0 -> end of directory.
3519 struct mdd_object *child;
3520 char *name = mdd_env_info(env)->mti_key;
3524 bool target_exist = false;
3526 len = iops->key_size(env, it);
3530 result = iops->rec(env, it, (struct dt_rec *)ent,
3531 LUDA_FID | LUDA_TYPE);
3532 if (result == -ESTALE)
3539 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3540 recsize = le16_to_cpu(ent->lde_reclen);
3542 /* Insert new fid with target name into target dir */
3543 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3544 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3545 ent->lde_name[1] == '.'))
3548 child = mdd_object_find(env, mdd, &ent->lde_fid);
3550 GOTO(out, rc = PTR_ERR(child));
3552 is_dir = S_ISDIR(mdd_object_type(child));
3554 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3556 /* Check whether the name has been inserted to the target */
3557 if (dt_try_as_dir(env, dt_tobj)) {
3558 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3560 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3561 (struct dt_key *)name,
3562 mdd_object_capa(env, mdd_tobj));
3563 if (unlikely(rc == 0))
3564 target_exist = true;
3567 handle = mdd_trans_create(env, mdd);
3569 GOTO(out, rc = PTR_ERR(handle));
3571 /* Note: this transaction is part of migration, and it is not
3572 * the last step of migration, so we set th_local = 1 to avoid
3573 * updating last rcvd for this transaction */
3574 handle->th_local = 1;
3575 if (likely(!target_exist)) {
3576 rc = mdo_declare_index_insert(env, mdd_tobj,
3578 mdd_object_type(child),
3584 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3590 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3595 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3599 /* Update .. for child */
3600 rc = mdo_declare_index_delete(env, child, dotdot,
3605 rc = mdo_declare_index_insert(env, child,
3606 mdd_object_fid(mdd_tobj),
3607 S_IFDIR, dotdot, handle);
3612 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3619 rc = mdd_trans_start(env, mdd, handle);
3621 CERROR("%s: transaction start failed: rc = %d\n",
3622 mdd2obd_dev(mdd)->obd_name, rc);
3626 if (likely(!target_exist)) {
3627 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3628 mdd_object_type(child),
3630 mdd_object_capa(env, mdd_tobj));
3635 rc = mdo_ref_add(env, mdd_tobj, handle);
3642 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle,
3643 mdd_object_capa(env, mdd_sobj));
3648 rc = __mdd_index_delete_only(env, child, dotdot, handle,
3649 mdd_object_capa(env, child));
3653 rc = __mdd_index_insert_only(env, child,
3654 mdd_object_fid(mdd_tobj), S_IFDIR,
3656 mdd_object_capa(env, child));
3661 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3662 strlen(name), handle);
3665 mdd_object_put(env, child);
3666 mdd_trans_stop(env, mdd, rc, handle);
3670 result = iops->next(env, it);
3671 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3672 GOTO(out, rc = -EINTR);
3674 if (result == -ESTALE)
3676 } while (result == 0);
3679 iops->fini(env, it);
3681 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3685 static int mdd_declare_update_linkea(const struct lu_env *env,
3686 struct mdd_object *mdd_pobj,
3687 struct mdd_object *mdd_sobj,
3688 struct mdd_object *mdd_tobj,
3689 struct thandle *handle,
3690 const struct lu_name *child_name)
3692 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3693 child_name, handle, 1);
3696 static int mdd_update_linkea(const struct lu_env *env,
3697 struct mdd_object *mdd_pobj,
3698 struct mdd_object *mdd_sobj,
3699 struct mdd_object *mdd_tobj,
3700 struct thandle *handle,
3701 const struct lu_name *child_name)
3703 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3704 child_name, handle, 0);
3707 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3708 struct mdd_object *mdd_pobj,
3709 struct mdd_object *mdd_sobj,
3710 struct mdd_object *mdd_tobj,
3711 const struct lu_name *lname,
3713 struct lu_attr *parent_la,
3714 struct thandle *handle)
3716 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3719 /* Revert IMMUTABLE flag */
3720 la_flag->la_valid = LA_FLAGS;
3721 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3722 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3723 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3727 /* delete entry from source dir */
3728 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3732 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3733 mdd_tobj, handle, lname);
3737 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3738 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3744 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3745 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3751 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3752 mdd_object_type(mdd_tobj),
3753 lname->ln_name, handle);
3757 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3758 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3763 /* delete old object */
3764 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3768 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3769 /* delete old object */
3770 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3773 /* set nlink to 0 */
3774 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3779 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3783 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3788 static int mdd_migrate_update_name(const struct lu_env *env,
3789 struct mdd_object *mdd_pobj,
3790 struct mdd_object *mdd_sobj,
3791 struct mdd_object *mdd_tobj,
3792 const struct lu_name *lname,
3795 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3796 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3797 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3798 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3799 struct thandle *handle;
3800 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3801 const char *name = lname->ln_name;
3805 /* update time for parent */
3806 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3807 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3808 p_la->la_valid = LA_CTIME;
3810 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
3814 handle = mdd_trans_create(env, mdd);
3816 RETURN(PTR_ERR(handle));
3818 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3819 lname, so_attr, p_la, handle);
3821 /* If the migration can not be fit in one transaction, just
3822 * leave it in the original MDT */
3824 GOTO(stop_trans, rc = 0);
3826 GOTO(stop_trans, rc);
3829 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3830 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3831 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3832 PFID(mdd_object_fid(mdd_tobj)));
3834 rc = mdd_trans_start(env, mdd, handle);
3836 GOTO(stop_trans, rc);
3838 /* Revert IMMUTABLE flag */
3839 la_flag->la_valid = LA_FLAGS;
3840 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3841 mdd_flags_xlate(mdd_sobj, la_flag->la_flags);
3842 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle,
3843 mdd_object_capa(env, mdd_pobj));
3845 GOTO(stop_trans, rc);
3847 /* Remove source name from source directory */
3848 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
3849 mdd_object_capa(env, mdd_pobj));
3851 GOTO(stop_trans, rc);
3853 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3856 GOTO(stop_trans, rc);
3858 if (S_ISREG(so_attr->la_mode)) {
3859 if (so_attr->la_nlink == 1) {
3860 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3862 mdd_object_capa(env, mdd_sobj));
3863 if (rc != 0 && rc != -ENODATA)
3864 GOTO(stop_trans, rc);
3868 /* Insert new fid with target name into target dir */
3869 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3870 mdd_object_type(mdd_tobj), name,
3871 handle, mdd_object_capa(env, mdd_pobj));
3873 GOTO(stop_trans, rc);
3875 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3878 GOTO(stop_trans, rc);
3880 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3881 mdo_ref_del(env, mdd_sobj, handle);
3883 mdo_ref_del(env, mdd_sobj, handle);
3885 /* Get the attr again after ref_del */
3886 rc = mdd_la_get(env, mdd_sobj, so_attr,
3887 mdd_object_capa(env, mdd_sobj));
3889 GOTO(out_unlock, rc);
3891 ma->ma_attr = *so_attr;
3892 ma->ma_valid |= MA_INODE;
3893 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3895 GOTO(out_unlock, rc);
3897 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3899 GOTO(out_unlock, rc);
3902 mdd_write_unlock(env, mdd_sobj);
3905 mdd_trans_stop(env, mdd, rc, handle);
3911 * Check whether we should migrate the file/dir
3913 * < 0 permission check failed or other error.
3914 * = 0 the file can be migrated.
3915 * > 0 the file does not need to be migrated, mostly
3916 * for multiple link file
3918 static int mdd_migrate_sanity_check(const struct lu_env *env,
3919 struct mdd_object *pobj,
3920 const struct lu_attr *pattr,
3921 struct mdd_object *sobj,
3922 struct lu_attr *sattr)
3924 struct mdd_thread_info *info = mdd_env_info(env);
3925 struct linkea_data *ldata = &info->mti_link_data;
3927 struct lu_buf *mgr_buf;
3933 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3934 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3935 if (mgr_buf->lb_buf == NULL)
3938 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV,
3939 mdd_object_capa(env, sobj));
3941 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3943 /* If the object has migrateEA, it means IMMUTE flag
3944 * is being set by previous migration process, so it
3945 * needs to override the IMMUTE flag, otherwise the
3946 * following sanity check will fail */
3947 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3948 LMV_HASH_FLAG_MIGRATION) {
3949 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3951 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3952 sobj->mod_flags &= ~IMMUTE_OBJ;
3953 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3954 mdd2obd_dev(mdd)->obd_name,
3955 PFID(mdd_object_fid(sobj)));
3959 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3960 sobj, sattr, NULL, NULL);
3964 /* Then it will check if the file should be migrated. If the file
3965 * has mulitple links, we only need migrate the file if all of its
3966 * entries has been migrated to the remote MDT */
3967 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3970 rc = mdd_links_read(env, sobj, ldata);
3972 /* For multiple links files, if there are no linkEA data at all,
3973 * means the file might be created before linkEA is enabled, and
3974 * all all of its links should not be migrated yet, otherwise
3975 * it should have some linkEA there */
3976 if (rc == -ENOENT || rc == -ENODATA)
3981 /* If it is mulitple links file, we need update the name entry for
3983 LASSERT(ldata->ld_leh != NULL);
3984 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3985 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3986 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3987 struct mdd_object *lpobj;
3988 struct lu_name lname;
3991 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3993 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3995 lpobj = mdd_object_find(env, mdd, &fid);
3996 if (IS_ERR(lpobj)) {
3997 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3998 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
4003 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
4004 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
4005 PFID(&fid), lname.ln_namelen, lname.ln_name);
4006 mdd_object_put(env, lpobj);
4010 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4011 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4012 lname.ln_name, PFID(&fid));
4013 mdd_object_put(env, lpobj);
4021 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4022 struct md_object *sobj, const struct lu_name *lname,
4023 struct md_object *tobj, struct md_attr *ma)
4025 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4026 struct mdd_device *mdd = mdo2mdd(pobj);
4027 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4028 struct mdd_object *mdd_tobj = NULL;
4029 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4030 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4034 /* If the file will being migrated, it will check whether
4035 * the file is being opened by someone else right now */
4036 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4037 if (mdd_sobj->mod_count >= 1) {
4038 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4039 mdd2obd_dev(mdd)->obd_name,
4040 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4041 mdd_sobj->mod_count, -EBUSY);
4042 mdd_read_unlock(env, mdd_sobj);
4043 GOTO(put, rc = -EBUSY);
4045 mdd_read_unlock(env, mdd_sobj);
4047 rc = mdd_la_get(env, mdd_sobj, so_attr, mdd_object_capa(env, mdd_sobj));
4051 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
4055 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4062 /* Sigh, it is impossible to finish all of migration in a single
4063 * transaction, for example migrating big directory entries to the
4064 * new MDT, it needs insert all of name entries of children in the
4067 * So migration will be done in multiple steps and transactions.
4069 * 1. create an orphan object on the remote MDT in one transaction.
4070 * 2. migrate extend attributes to the new target file/directory.
4071 * 3. For directory, migrate the entries to the new MDT and update
4072 * linkEA of each children. Because we can not migrate all entries
4073 * in a single transaction, so the migrating directory will become
4074 * a striped directory during migration, so once the process is
4075 * interrupted, the directory is still accessible. (During lookup,
4076 * client will locate the name by searching both original and target
4078 * 4. Finally, update the name/FID to point to the new file/directory
4079 * in a separate transaction.
4082 /* step 1: Check whether the orphan object has been created, and create
4083 * orphan object on the remote MDT if needed */
4084 mdd_tobj = md2mdd_obj(tobj);
4085 if (!mdd_object_exists(mdd_tobj)) {
4086 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4092 /* step 2: migrate xattr */
4093 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4097 /* step 3: migrate name entries to the orphan object */
4098 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4099 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4102 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4103 OBD_FAIL_MDS_REINT_NET_REP)))
4107 /* step 4: update name entry to the new object */
4108 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4116 const struct md_dir_operations mdd_dir_ops = {
4117 .mdo_is_subdir = mdd_is_subdir,
4118 .mdo_lookup = mdd_lookup,
4119 .mdo_create = mdd_create,
4120 .mdo_rename = mdd_rename,
4121 .mdo_link = mdd_link,
4122 .mdo_unlink = mdd_unlink,
4123 .mdo_create_data = mdd_create_data,
4124 .mdo_migrate = mdd_migrate,