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, 2014, 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);
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
122 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
127 * Get parent FID of the directory
129 * Read parent FID from linkEA, if that fails, then do lookup
130 * dotdot to get the parent FID.
132 * \param[in] env execution environment
133 * \param[in] obj object from which to find the parent FID
134 * \param[in] attr attribute of the object
135 * \param[out] fid fid to get the parent FID
137 * \retval 0 if getting the parent FID succeeds.
138 * \retval negative errno if getting the parent FID fails.
140 static inline int mdd_parent_fid(const struct lu_env *env,
141 struct mdd_object *obj,
142 const struct lu_attr *attr,
145 struct mdd_thread_info *info = mdd_env_info(env);
146 struct linkea_data ldata = { NULL };
147 struct lu_buf *buf = &info->mti_link_buf;
148 struct lu_name lname;
153 LASSERT(S_ISDIR(mdd_object_type(obj)));
155 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
156 if (buf->lb_buf == NULL)
157 GOTO(lookup, rc = 0);
160 rc = mdd_links_read(env, obj, &ldata);
164 LASSERT(ldata.ld_leh != NULL);
165 /* Directory should only have 1 parent */
166 if (ldata.ld_leh->leh_reccount > 1)
169 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
171 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
172 if (likely(fid_is_sane(fid)))
175 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
180 * For root fid use special function, which does not compare version component
181 * of fid. Version component is different for root fids on all MDTs.
183 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
185 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
186 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
190 * return 1: if lf is the fid of the ancestor of p1;
193 * return -EREMOTE: if remote object is found, in this
194 * case fid of remote object is saved to @pf;
196 * otherwise: values < 0, errors.
198 static int mdd_is_parent(const struct lu_env *env,
199 struct mdd_device *mdd,
200 struct mdd_object *p1,
201 const struct lu_attr *attr,
202 const struct lu_fid *lf,
205 struct mdd_object *parent = NULL;
210 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
211 pfid = &mdd_env_info(env)->mti_fid;
213 /* Check for root first. */
214 if (mdd_is_root(mdd, mdo2fid(p1)))
218 /* this is done recursively */
219 rc = mdd_parent_fid(env, p1, attr, pfid);
222 if (mdd_is_root(mdd, pfid))
224 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
226 if (lu_fid_eq(pfid, lf))
229 mdd_object_put(env, parent);
231 parent = mdd_object_find(env, mdd, pfid);
233 GOTO(out, rc = PTR_ERR(parent));
238 if (parent && !IS_ERR(parent))
239 mdd_object_put(env, parent);
244 * No permission check is needed.
246 * returns 1: if fid is ancestor of @mo;
247 * returns 0: if fid is not an ancestor of @mo;
249 * returns EREMOTE if remote object is found, fid of remote object is saved to
252 * returns < 0: if error
254 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
255 const struct lu_fid *fid, struct lu_fid *sfid)
257 struct mdd_device *mdd = mdo2mdd(mo);
258 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
262 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
265 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
269 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
273 } else if (rc == 1) {
274 /* found @fid is parent */
282 * Check that @dir contains no entries except (possibly) dot and dotdot.
287 * -ENOTDIR not a directory object
288 * -ENOTEMPTY not empty
292 static int mdd_dir_is_empty(const struct lu_env *env,
293 struct mdd_object *dir)
296 struct dt_object *obj;
297 const struct dt_it_ops *iops;
301 obj = mdd_object_child(dir);
302 if (!dt_try_as_dir(env, obj))
305 iops = &obj->do_index_ops->dio_it;
306 it = iops->init(env, obj, LUDA_64BITHASH);
308 result = iops->get(env, it, (const struct dt_key *)"");
311 for (result = 0, i = 0; result == 0 && i < 3; ++i)
312 result = iops->next(env, it);
315 else if (result == 1)
317 } else if (result == 0)
319 * Huh? Index contains no zero key?
326 result = PTR_ERR(it);
331 * Determine if the target object can be hard linked, and right now it only
332 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
333 * directory nlink count might exceed the maximum link count(see
334 * osd_object_ref_add), so it only check nlink for non-directories.
336 * \param[in] env thread environment
337 * \param[in] obj object being linked to
338 * \param[in] la attributes of \a obj
340 * \retval 0 if \a obj can be hard linked
341 * \retval negative error if \a obj is a directory or has too
344 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
345 const struct lu_attr *la)
347 struct mdd_device *m = mdd_obj2mdd_dev(obj);
352 /* Subdir count limitation can be broken through
353 * (see osd_object_ref_add), so only check non-directory here. */
354 if (!S_ISDIR(la->la_mode) &&
355 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
362 * Check whether it may create the cobj under the pobj.
364 * \param[in] env execution environment
365 * \param[in] pobj the parent directory
366 * \param[in] pattr the attribute of the parent directory
367 * \param[in] cobj the child to be created
368 * \param[in] check_perm if check WRITE|EXEC permission for parent
370 * \retval = 0 create the child under this dir is allowed
371 * \retval negative errno create the child under this dir is
374 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
375 const struct lu_attr *pattr, struct mdd_object *cobj,
378 struct mdd_thread_info *info = mdd_env_info(env);
383 if (cobj && mdd_object_exists(cobj))
386 if (mdd_is_dead_obj(pobj))
389 /* If the parent is a sub-stripe, check whether it is dead */
390 xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
391 rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV);
392 if (unlikely(rc > 0)) {
393 struct lmv_mds_md_v1 *lmv1 = xbuf->lb_buf;
395 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
396 le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
402 rc = mdd_permission_internal_locked(env, pobj, pattr,
403 MAY_WRITE | MAY_EXEC,
409 * Check whether can unlink from the pobj in the case of "cobj == NULL".
411 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
412 const struct lu_attr *pattr, const struct lu_attr *attr)
417 if (mdd_is_dead_obj(pobj))
420 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
423 rc = mdd_permission_internal_locked(env, pobj, pattr,
424 MAY_WRITE | MAY_EXEC,
429 if (pattr->la_flags & LUSTRE_APPEND_FL)
436 * pobj == NULL is remote ops case, under such case, pobj's
437 * VTX feature has been checked already, no need check again.
439 static inline int mdd_is_sticky(const struct lu_env *env,
440 struct mdd_object *pobj,
441 const struct lu_attr *pattr,
442 struct mdd_object *cobj,
443 const struct lu_attr *cattr)
445 struct lu_ucred *uc = lu_ucred_assert(env);
448 LASSERT(pattr != NULL);
449 if (!(pattr->la_mode & S_ISVTX) ||
450 (pattr->la_uid == uc->uc_fsuid))
454 LASSERT(cattr != NULL);
455 if (cattr->la_uid == uc->uc_fsuid)
458 return !md_capable(uc, CFS_CAP_FOWNER);
461 static int mdd_may_delete_entry(const struct lu_env *env,
462 struct mdd_object *pobj,
463 const struct lu_attr *pattr,
468 LASSERT(pobj != NULL);
469 if (!mdd_object_exists(pobj))
472 if (mdd_is_dead_obj(pobj))
477 rc = mdd_permission_internal_locked(env, pobj, pattr,
478 MAY_WRITE | MAY_EXEC,
484 if (pattr->la_flags & LUSTRE_APPEND_FL)
491 * Check whether it may delete the cobj from the pobj.
494 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
495 const struct lu_attr *tpattr, struct mdd_object *tobj,
496 const struct lu_attr *tattr, const struct lu_attr *cattr,
497 int check_perm, int check_empty)
503 LASSERT(tpattr != NULL);
504 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
512 if (!mdd_object_exists(tobj))
515 if (mdd_is_dead_obj(tobj))
518 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
521 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
524 /* additional check the rename case */
526 if (S_ISDIR(cattr->la_mode)) {
527 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
529 if (!S_ISDIR(tattr->la_mode))
532 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
534 } else if (S_ISDIR(tattr->la_mode))
538 if (S_ISDIR(tattr->la_mode) && check_empty)
539 rc = mdd_dir_is_empty(env, tobj);
545 * Check whether it can create the link file(linked to @src_obj) under
546 * the target directory(@tgt_obj), and src_obj has been locked by
549 * \param[in] env execution environment
550 * \param[in] tgt_obj the target directory
551 * \param[in] tattr attributes of target directory
552 * \param[in] lname the link name
553 * \param[in] src_obj source object for link
554 * \param[in] cattr attributes for source object
556 * \retval = 0 it is allowed to create the link file under tgt_obj
557 * \retval negative error not allowed to create the link file
559 static int mdd_link_sanity_check(const struct lu_env *env,
560 struct mdd_object *tgt_obj,
561 const struct lu_attr *tattr,
562 const struct lu_name *lname,
563 struct mdd_object *src_obj,
564 const struct lu_attr *cattr)
566 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
570 if (!mdd_object_exists(src_obj))
573 if (mdd_is_dead_obj(src_obj))
576 /* Local ops, no lookup before link, check filename length here. */
577 rc = mdd_name_check(m, lname);
581 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
584 if (S_ISDIR(mdd_object_type(src_obj)))
587 LASSERT(src_obj != tgt_obj);
588 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
592 rc = __mdd_may_link(env, src_obj, cattr);
597 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
598 const char *name, struct thandle *handle)
600 struct dt_object *next = mdd_object_child(pobj);
604 if (dt_try_as_dir(env, next))
605 rc = dt_delete(env, next, (struct dt_key *)name, handle);
612 static int __mdd_index_insert_only(const struct lu_env *env,
613 struct mdd_object *pobj,
614 const struct lu_fid *lf, __u32 type,
616 struct thandle *handle)
618 struct dt_object *next = mdd_object_child(pobj);
622 if (dt_try_as_dir(env, next)) {
623 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
624 struct lu_ucred *uc = lu_ucred_check(env);
628 rec->rec_type = type;
629 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
630 rc = dt_insert(env, next, (const struct dt_rec *)rec,
631 (const struct dt_key *)name, handle,
639 /* insert named index, add reference if isdir */
640 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
641 const struct lu_fid *lf, __u32 type,
642 const char *name, struct thandle *handle)
647 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
648 if (rc == 0 && S_ISDIR(type)) {
649 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
650 mdo_ref_add(env, pobj, handle);
651 mdd_write_unlock(env, pobj);
657 /* delete named index, drop reference if isdir */
658 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
659 const char *name, int is_dir,
660 struct thandle *handle)
665 rc = __mdd_index_delete_only(env, pobj, name, handle);
666 if (rc == 0 && is_dir) {
667 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
668 mdo_ref_del(env, pobj, handle);
669 mdd_write_unlock(env, pobj);
675 static int mdd_llog_record_calc_size(const struct lu_env *env,
676 const struct lu_name *tname,
677 const struct lu_name *sname)
679 const struct lu_ucred *uc = lu_ucred(env);
680 enum changelog_rec_flags crf = 0;
681 size_t hdr_size = sizeof(struct llog_changelog_rec) -
682 sizeof(struct changelog_rec);
687 if (uc != NULL && uc->uc_jobid[0] != '\0')
690 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
691 (tname != NULL ? tname->ln_namelen : 0) +
692 (sname != NULL ? 1 + sname->ln_namelen : 0));
695 int mdd_declare_changelog_store(const struct lu_env *env,
696 struct mdd_device *mdd,
697 const struct lu_name *tname,
698 const struct lu_name *sname,
699 struct thandle *handle)
701 struct obd_device *obd = mdd2obd_dev(mdd);
702 struct llog_ctxt *ctxt;
703 struct llog_changelog_rec *rec;
705 struct thandle *llog_th;
710 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
713 reclen = mdd_llog_record_calc_size(env, tname, sname);
714 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
715 if (buf->lb_buf == NULL)
719 rec->cr_hdr.lrh_len = reclen;
720 rec->cr_hdr.lrh_type = CHANGELOG_REC;
722 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
726 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
728 GOTO(out_put, rc = PTR_ERR(llog_th));
730 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
738 /** Add a changelog entry \a rec to the changelog llog
741 * \param handle - currently ignored since llogs start their own transaction;
742 * this will hopefully be fixed in llog rewrite
745 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
746 struct llog_changelog_rec *rec, struct thandle *th)
748 struct obd_device *obd = mdd2obd_dev(mdd);
749 struct llog_ctxt *ctxt;
750 struct thandle *llog_th;
753 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
754 changelog_rec_varsize(&rec->cr));
756 /* llog_lvfs_write_rec sets the llog tail len */
757 rec->cr_hdr.lrh_type = CHANGELOG_REC;
758 rec->cr.cr_time = cl_time();
760 spin_lock(&mdd->mdd_cl.mc_lock);
761 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
762 * but as long as the MDD transactions are ordered correctly for e.g.
763 * rename conflicts, I don't think this should matter. */
764 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
765 spin_unlock(&mdd->mdd_cl.mc_lock);
767 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
771 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
773 GOTO(out_put, rc = PTR_ERR(llog_th));
775 /* nested journal transaction */
776 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
785 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
786 const struct lu_fid *sfid,
787 const struct lu_fid *spfid,
788 const struct lu_name *sname)
790 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
791 size_t extsize = sname->ln_namelen + 1;
793 LASSERT(sfid != NULL);
794 LASSERT(spfid != NULL);
795 LASSERT(sname != NULL);
797 rnm->cr_sfid = *sfid;
798 rnm->cr_spfid = *spfid;
800 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
801 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
802 rec->cr_namelen += extsize;
805 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
807 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
809 if (jobid == NULL || jobid[0] == '\0')
812 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
815 /** Store a namespace change changelog record
816 * If this fails, we must fail the whole transaction; we don't
817 * want the change to commit without the log entry.
818 * \param target - mdd_object of change
819 * \param tpfid - target parent dir/object fid
820 * \param sfid - source object fid
821 * \param spfid - source parent fid
822 * \param tname - target name string
823 * \param sname - source name string
824 * \param handle - transaction handle
826 int mdd_changelog_ns_store(const struct lu_env *env,
827 struct mdd_device *mdd,
828 enum changelog_rec_type type,
829 enum changelog_rec_flags crf,
830 struct mdd_object *target,
831 const struct lu_fid *tpfid,
832 const struct lu_fid *sfid,
833 const struct lu_fid *spfid,
834 const struct lu_name *tname,
835 const struct lu_name *sname,
836 struct thandle *handle)
838 const struct lu_ucred *uc = lu_ucred(env);
839 struct llog_changelog_rec *rec;
846 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
849 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
852 LASSERT(tpfid != NULL);
853 LASSERT(tname != NULL);
854 LASSERT(handle != NULL);
856 reclen = mdd_llog_record_calc_size(env, tname, sname);
857 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
858 if (buf->lb_buf == NULL)
864 if (uc != NULL && uc->uc_jobid[0] != '\0')
872 rec->cr.cr_flags = crf;
873 rec->cr.cr_type = (__u32)type;
874 rec->cr.cr_pfid = *tpfid;
875 rec->cr.cr_namelen = tname->ln_namelen;
876 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
878 if (crf & CLF_RENAME)
879 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
882 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
884 if (likely(target != NULL)) {
885 rec->cr.cr_tfid = *mdo2fid(target);
886 target->mod_cltime = cfs_time_current_64();
888 fid_zero(&rec->cr.cr_tfid);
891 rc = mdd_changelog_store(env, mdd, rec, handle);
893 CERROR("%s: cannot store changelog record: type = %d, "
894 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
895 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
896 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
903 static int __mdd_links_add(const struct lu_env *env,
904 struct mdd_object *mdd_obj,
905 struct linkea_data *ldata,
906 const struct lu_name *lname,
907 const struct lu_fid *pfid,
908 int first, int check)
912 if (ldata->ld_leh == NULL) {
913 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
917 rc = linkea_data_new(ldata,
918 &mdd_env_info(env)->mti_link_buf);
925 rc = linkea_links_find(ldata, lname, pfid);
926 if (rc && rc != -ENOENT)
932 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
933 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
937 linkea_add_buf(ldata, lname, tfid);
940 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
941 linkea_add_buf(ldata, lname, pfid);
943 return linkea_add_buf(ldata, lname, pfid);
946 static int __mdd_links_del(const struct lu_env *env,
947 struct mdd_object *mdd_obj,
948 struct linkea_data *ldata,
949 const struct lu_name *lname,
950 const struct lu_fid *pfid)
954 if (ldata->ld_leh == NULL) {
955 rc = mdd_links_read(env, mdd_obj, ldata);
960 rc = linkea_links_find(ldata, lname, pfid);
964 linkea_del_buf(ldata, lname);
968 static int mdd_linkea_prepare(const struct lu_env *env,
969 struct mdd_object *mdd_obj,
970 const struct lu_fid *oldpfid,
971 const struct lu_name *oldlname,
972 const struct lu_fid *newpfid,
973 const struct lu_name *newlname,
974 int first, int check,
975 struct linkea_data *ldata)
981 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
984 LASSERT(oldpfid != NULL || newpfid != NULL);
986 if (mdd_obj->mod_flags & DEAD_OBJ) {
987 /* Prevent linkea to be updated which is NOT necessary. */
988 ldata->ld_reclen = 0;
989 /* No more links, don't bother */
993 if (oldpfid != NULL) {
994 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
997 (rc != -ENODATA && rc != -ENOENT))
999 /* No changes done. */
1004 /* If renaming, add the new record */
1005 if (newpfid != NULL) {
1006 /* even if the add fails, we still delete the out-of-date
1008 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1012 rc = rc != 0 ? rc : rc2;
1017 int mdd_links_rename(const struct lu_env *env,
1018 struct mdd_object *mdd_obj,
1019 const struct lu_fid *oldpfid,
1020 const struct lu_name *oldlname,
1021 const struct lu_fid *newpfid,
1022 const struct lu_name *newlname,
1023 struct thandle *handle,
1024 struct linkea_data *ldata,
1025 int first, int check)
1030 if (ldata == NULL) {
1031 ldata = &mdd_env_info(env)->mti_link_data;
1032 memset(ldata, 0, sizeof(*ldata));
1033 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1034 newpfid, newlname, first, check,
1040 if (ldata->ld_reclen != 0)
1041 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1046 if (rc == -EOVERFLOW || rc == -ENOSPC)
1048 if (newlname == NULL)
1049 CDEBUG(error ? D_ERROR : D_OTHER,
1050 "link_ea add failed %d "DFID"\n",
1051 rc, PFID(mdd_object_fid(mdd_obj)));
1052 else if (oldpfid == NULL)
1053 CDEBUG(error ? D_ERROR : D_OTHER,
1054 "link_ea add '%.*s' failed %d "DFID"\n",
1055 newlname->ln_namelen, newlname->ln_name,
1056 rc, PFID(mdd_object_fid(mdd_obj)));
1057 else if (newpfid == NULL)
1058 CDEBUG(error ? D_ERROR : D_OTHER,
1059 "link_ea del '%.*s' failed %d "DFID"\n",
1060 oldlname->ln_namelen, oldlname->ln_name,
1061 rc, PFID(mdd_object_fid(mdd_obj)));
1063 CDEBUG(error ? D_ERROR : D_OTHER,
1064 "link_ea rename '%.*s'->'%.*s' failed %d "
1066 oldlname->ln_namelen, oldlname->ln_name,
1067 newlname->ln_namelen, newlname->ln_name,
1068 rc, PFID(mdd_object_fid(mdd_obj)));
1071 if (is_vmalloc_addr(ldata->ld_buf))
1072 /* if we vmalloced a large buffer drop it */
1073 lu_buf_free(ldata->ld_buf);
1078 static inline int mdd_links_add(const struct lu_env *env,
1079 struct mdd_object *mdd_obj,
1080 const struct lu_fid *pfid,
1081 const struct lu_name *lname,
1082 struct thandle *handle,
1083 struct linkea_data *ldata, int first)
1085 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1086 pfid, lname, handle, ldata, first, 0);
1089 static inline int mdd_links_del(const struct lu_env *env,
1090 struct mdd_object *mdd_obj,
1091 const struct lu_fid *pfid,
1092 const struct lu_name *lname,
1093 struct thandle *handle)
1095 return mdd_links_rename(env, mdd_obj, pfid, lname,
1096 NULL, NULL, handle, NULL, 0, 0);
1099 /** Read the link EA into a temp buffer.
1100 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1101 * A pointer to the buffer is stored in \a ldata::ld_buf.
1103 * \retval 0 or error
1105 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1106 struct linkea_data *ldata)
1110 if (!mdd_object_exists(mdd_obj))
1113 /* First try a small buf */
1114 LASSERT(env != NULL);
1115 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1117 if (ldata->ld_buf->lb_buf == NULL)
1120 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
1121 if (rc == -ERANGE) {
1122 /* Buf was too small, figure out what we need. */
1123 lu_buf_free(ldata->ld_buf);
1124 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1128 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1129 if (ldata->ld_buf->lb_buf == NULL)
1131 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1135 lu_buf_free(ldata->ld_buf);
1136 ldata->ld_buf = NULL;
1140 return linkea_init(ldata);
1143 /** Read the link EA into a temp buffer.
1144 * Uses the name_buf since it is generally large.
1145 * \retval IS_ERR err
1146 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1148 struct lu_buf *mdd_links_get(const struct lu_env *env,
1149 struct mdd_object *mdd_obj)
1151 struct linkea_data ldata = { NULL };
1154 rc = mdd_links_read(env, mdd_obj, &ldata);
1155 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1158 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1159 struct linkea_data *ldata, struct thandle *handle)
1161 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1162 ldata->ld_leh->leh_len);
1165 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1168 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1169 if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1170 mdd_object_remote(mdd_obj) == 0) {
1171 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1172 struct thandle *sub_th;
1174 /* XXX: If the linkEA is overflow, then we need to notify the
1175 * namespace LFSCK to skip "nlink" attribute verification
1176 * on this object to avoid the "nlink" to be shrinked by
1177 * wrong. It may be not good an interaction with LFSCK
1178 * like this. We will consider to replace it with other
1179 * mechanism in future. LU-5802. */
1180 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1181 LFSCK_TYPE_NAMESPACE);
1183 sub_th = thandle_get_sub_by_dt(env, handle,
1184 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1185 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1192 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1193 struct thandle *handle, struct linkea_data *ldata,
1194 enum mdd_links_add_overflow overflow)
1200 if (ldata != NULL && ldata->ld_leh != NULL) {
1201 ea_len = ldata->ld_leh->leh_len;
1202 linkea = ldata->ld_buf->lb_buf;
1204 ea_len = DEFAULT_LINKEA_SIZE;
1208 /* XXX: max size? */
1209 rc = mdo_declare_xattr_set(env, mdd_obj,
1210 mdd_buf_get_const(env, linkea, ea_len),
1211 XATTR_NAME_LINK, 0, handle);
1215 if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1216 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1217 struct thandle *sub_th;
1219 /* XXX: If the linkEA is overflow, then we need to notify the
1220 * namespace LFSCK to skip "nlink" attribute verification
1221 * on this object to avoid the "nlink" to be shrinked by
1222 * wrong. It may be not good an interaction with LFSCK
1223 * like this. We will consider to replace it with other
1224 * mechanism in future. LU-5802. */
1225 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1226 LFSCK_TYPE_NAMESPACE);
1228 sub_th = thandle_get_sub_by_dt(env, handle,
1229 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1230 rc = lfsck_in_notify(env,
1231 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1238 static inline int mdd_declare_links_del(const struct lu_env *env,
1239 struct mdd_object *c,
1240 struct thandle *handle)
1244 /* For directory, the linkEA will be removed together
1245 * with the object. */
1246 if (!S_ISDIR(mdd_object_type(c)))
1247 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1252 static int mdd_declare_link(const struct lu_env *env,
1253 struct mdd_device *mdd,
1254 struct mdd_object *p,
1255 struct mdd_object *c,
1256 const struct lu_name *name,
1257 struct thandle *handle,
1259 struct linkea_data *data)
1263 rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1264 name->ln_name, handle);
1268 rc = mdo_declare_ref_add(env, c, handle);
1272 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1273 rc = mdo_declare_ref_add(env, c, handle);
1278 la->la_valid = LA_CTIME | LA_MTIME;
1279 rc = mdo_declare_attr_set(env, p, la, handle);
1283 la->la_valid = LA_CTIME;
1284 rc = mdo_declare_attr_set(env, c, la, handle);
1288 rc = mdd_declare_links_add(env, c, handle, data,
1289 S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1293 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1298 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1299 struct md_object *src_obj, const struct lu_name *lname,
1302 const char *name = lname->ln_name;
1303 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1304 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1305 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1306 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1307 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1308 struct mdd_device *mdd = mdo2mdd(src_obj);
1309 struct thandle *handle;
1310 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1314 rc = mdd_la_get(env, mdd_sobj, cattr);
1318 rc = mdd_la_get(env, mdd_tobj, tattr);
1322 handle = mdd_trans_create(env, mdd);
1324 GOTO(out_pending, rc = PTR_ERR(handle));
1326 memset(ldata, 0, sizeof(*ldata));
1328 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1329 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1331 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1336 rc = mdd_trans_start(env, mdd, handle);
1340 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1341 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1344 GOTO(out_unlock, rc);
1346 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1347 rc = mdo_ref_add(env, mdd_sobj, handle);
1349 GOTO(out_unlock, rc);
1352 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1353 rc = mdo_ref_add(env, mdd_sobj, handle);
1355 GOTO(out_unlock, rc);
1358 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1359 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1362 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1363 mdd_object_type(mdd_sobj),
1366 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1367 mdd_object_type(mdd_sobj),
1372 mdo_ref_del(env, mdd_sobj, handle);
1373 GOTO(out_unlock, rc);
1376 la->la_valid = LA_CTIME | LA_MTIME;
1377 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1379 GOTO(out_unlock, rc);
1381 la->la_valid = LA_CTIME;
1382 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1384 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1385 mdo2fid(mdd_tobj), lname, 0, 0,
1388 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1389 lname, handle, ldata, 0);
1390 /* The failure of links_add should not cause the link
1391 * failure, reset rc here */
1396 mdd_write_unlock(env, mdd_sobj);
1398 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1399 mdo2fid(mdd_tobj), NULL, NULL,
1400 lname, NULL, handle);
1402 mdd_trans_stop(env, mdd, rc, handle);
1404 if (is_vmalloc_addr(ldata->ld_buf))
1405 /* if we vmalloced a large buffer drop it */
1406 lu_buf_free(ldata->ld_buf);
1411 static int mdd_mark_dead_object(const struct lu_env *env,
1412 struct mdd_object *obj, struct thandle *handle,
1415 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1419 obj->mod_flags |= DEAD_OBJ;
1421 if (!S_ISDIR(mdd_object_type(obj)))
1424 attr->la_valid = LA_FLAGS;
1425 attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1428 rc = mdo_declare_attr_set(env, obj, attr, handle);
1430 rc = mdo_attr_set(env, obj, attr, handle);
1435 static int mdd_declare_finish_unlink(const struct lu_env *env,
1436 struct mdd_object *obj,
1437 struct thandle *handle)
1441 rc = mdd_mark_dead_object(env, obj, handle, true);
1445 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1449 rc = mdo_declare_destroy(env, obj, handle);
1453 return mdd_declare_links_del(env, obj, handle);
1456 /* caller should take a lock before calling */
1457 int mdd_finish_unlink(const struct lu_env *env,
1458 struct mdd_object *obj, struct md_attr *ma,
1459 const struct mdd_object *pobj,
1460 const struct lu_name *lname,
1464 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1467 LASSERT(mdd_write_locked(env, obj) != 0);
1469 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1470 rc = mdd_mark_dead_object(env, obj, th, false);
1474 /* add new orphan and the object
1475 * will be deleted during mdd_close() */
1476 if (obj->mod_count) {
1477 rc = __mdd_orphan_add(env, obj, th);
1479 CDEBUG(D_HA, "Object "DFID" is inserted into "
1480 "orphan list, open count = %d\n",
1481 PFID(mdd_object_fid(obj)),
1484 CERROR("Object "DFID" fail to be an orphan, "
1485 "open count = %d, maybe cause failed "
1487 PFID(mdd_object_fid(obj)),
1490 rc = mdo_destroy(env, obj, th);
1492 } else if (!is_dir) {
1493 /* old files may not have link ea; ignore errors */
1494 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1502 * has mdd_write_lock on cobj already, but not on pobj yet
1504 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1505 const struct lu_attr *pattr,
1506 struct mdd_object *cobj,
1507 const struct lu_attr *cattr)
1512 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1517 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1518 struct mdd_object *p, struct mdd_object *c,
1519 const struct lu_name *name, struct md_attr *ma,
1520 struct thandle *handle, int no_name, int is_dir)
1522 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1525 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1526 if (likely(no_name == 0)) {
1527 rc = mdo_declare_index_delete(env, p, name->ln_name,
1534 rc = mdo_declare_ref_del(env, p, handle);
1540 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1541 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1542 la->la_valid = LA_CTIME | LA_MTIME;
1543 rc = mdo_declare_attr_set(env, p, la, handle);
1548 rc = mdo_declare_ref_del(env, c, handle);
1552 rc = mdo_declare_ref_del(env, c, handle);
1556 la->la_valid = LA_CTIME;
1557 rc = mdo_declare_attr_set(env, c, la, handle);
1561 rc = mdd_declare_finish_unlink(env, c, handle);
1565 /* FIXME: need changelog for remove entry */
1566 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1573 * test if a file has an HSM archive
1574 * if HSM attributes are not found in ma update them from
1577 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1578 struct mdd_object *obj,
1583 if (!(ma->ma_valid & MA_HSM)) {
1584 /* no HSM MD provided, read xattr */
1585 struct lu_buf *hsm_buf;
1586 const size_t buflen = sizeof(struct hsm_attrs);
1589 hsm_buf = mdd_buf_get(env, NULL, 0);
1590 lu_buf_alloc(hsm_buf, buflen);
1591 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1592 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1593 lu_buf_free(hsm_buf);
1597 ma->ma_valid = MA_HSM;
1599 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1605 * Delete name entry and the object.
1606 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1607 * does not exist for this object, and it could only happen during resending
1608 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1609 * is also needed in this case(needed by changelog), so we have to add another
1610 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1611 * the ENOENT failure should be able to be fixed by redo mechanism.
1613 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1614 struct md_object *cobj, const struct lu_name *lname,
1615 struct md_attr *ma, int no_name)
1617 const char *name = lname->ln_name;
1618 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1619 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1620 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1621 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1622 struct mdd_object *mdd_cobj = NULL;
1623 struct mdd_device *mdd = mdo2mdd(pobj);
1624 struct thandle *handle;
1628 /* cobj == NULL means only delete name entry */
1629 if (likely(cobj != NULL)) {
1630 mdd_cobj = md2mdd_obj(cobj);
1631 if (mdd_object_exists(mdd_cobj) == 0)
1635 rc = mdd_la_get(env, mdd_pobj, pattr);
1639 if (likely(mdd_cobj != NULL)) {
1641 rc = mdd_la_get(env, mdd_cobj, cattr);
1645 is_dir = S_ISDIR(cattr->la_mode);
1648 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1652 handle = mdd_trans_create(env, mdd);
1654 RETURN(PTR_ERR(handle));
1656 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1657 lname, ma, handle, no_name, is_dir);
1661 rc = mdd_trans_start(env, mdd, handle);
1665 if (likely(mdd_cobj != NULL))
1666 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1668 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1669 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1674 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1675 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1676 GOTO(cleanup, rc = 0);
1678 if (likely(mdd_cobj != NULL)) {
1679 rc = mdo_ref_del(env, mdd_cobj, handle);
1681 __mdd_index_insert_only(env, mdd_pobj,
1683 mdd_object_type(mdd_cobj),
1690 mdo_ref_del(env, mdd_cobj, handle);
1692 /* fetch updated nlink */
1693 rc = mdd_la_get(env, mdd_cobj, cattr);
1698 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1699 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1701 la->la_valid = LA_CTIME | LA_MTIME;
1702 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1706 /* Enough for only unlink the entry */
1707 if (unlikely(mdd_cobj == NULL))
1710 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1711 /* update ctime of an unlinked file only if it is still
1712 * opened or a link still exists */
1713 la->la_valid = LA_CTIME;
1714 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1719 /* XXX: this transfer to ma will be removed with LOD/OSP */
1720 ma->ma_attr = *cattr;
1721 ma->ma_valid |= MA_INODE;
1722 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1724 /* fetch updated nlink */
1726 rc = mdd_la_get(env, mdd_cobj, cattr);
1728 /* if object is removed then we can't get its attrs, use last get */
1729 if (cattr->la_nlink == 0) {
1730 ma->ma_attr = *cattr;
1731 ma->ma_valid |= MA_INODE;
1735 if (likely(mdd_cobj != NULL))
1736 mdd_write_unlock(env, mdd_cobj);
1741 if (cattr->la_nlink == 0) {
1742 cl_flags |= CLF_UNLINK_LAST;
1743 /* search for an existing archive */
1744 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1745 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1748 rc = mdd_changelog_ns_store(env, mdd,
1749 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1750 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1755 mdd_trans_stop(env, mdd, rc, handle);
1761 * The permission has been checked when obj created, no need check again.
1763 static int mdd_cd_sanity_check(const struct lu_env *env,
1764 struct mdd_object *obj)
1769 if (!obj || mdd_is_dead_obj(obj))
1775 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1776 struct md_object *cobj, const struct md_op_spec *spec,
1779 struct mdd_device *mdd = mdo2mdd(cobj);
1780 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1781 struct mdd_object *son = md2mdd_obj(cobj);
1782 struct thandle *handle;
1783 const struct lu_buf *buf;
1784 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1785 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1789 rc = mdd_cd_sanity_check(env, son);
1793 if (!md_should_create(spec->sp_cr_flags))
1797 * there are following use cases for this function:
1798 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1799 * striping can be specified or not
1802 rc = mdd_la_get(env, son, attr);
1806 /* calling ->ah_make_hint() is used to transfer information from parent */
1807 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1809 handle = mdd_trans_create(env, mdd);
1811 GOTO(out_free, rc = PTR_ERR(handle));
1814 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1815 * Should this be fixed?
1817 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1818 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1819 spec->sp_cr_flags, spec->no_create);
1821 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1822 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1823 spec->u.sp_ea.eadatalen);
1828 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1829 XATTR_NAME_LOV, 0, handle);
1833 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1837 rc = mdd_trans_start(env, mdd, handle);
1841 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1847 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1850 mdd_trans_stop(env, mdd, rc, handle);
1855 static int mdd_declare_object_initialize(const struct lu_env *env,
1856 struct mdd_object *parent,
1857 struct mdd_object *child,
1858 struct lu_attr *attr,
1859 struct thandle *handle)
1865 * inode mode has been set in creation time, and it's based on umask,
1866 * la_mode and acl, don't set here again! (which will go wrong
1867 * because below function doesn't consider umask).
1868 * I'd suggest set all object attributes in creation time, see above.
1870 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1871 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1872 rc = mdo_declare_attr_set(env, child, attr, handle);
1873 attr->la_valid |= LA_MODE | LA_TYPE;
1874 if (rc != 0 || !S_ISDIR(attr->la_mode))
1877 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1882 rc = mdo_declare_ref_add(env, child, handle);
1886 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1892 static int mdd_object_initialize(const struct lu_env *env,
1893 const struct lu_fid *pfid,
1894 struct mdd_object *child,
1895 struct lu_attr *attr, struct thandle *handle,
1896 const struct md_op_spec *spec)
1901 if (S_ISDIR(attr->la_mode)) {
1902 /* Add "." and ".." for newly created dir */
1903 mdo_ref_add(env, child, handle);
1904 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1905 S_IFDIR, dot, handle);
1907 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1910 mdo_ref_del(env, child, handle);
1917 * This function checks whether it can create a file/dir under the
1918 * directory(@pobj). The directory(@pobj) is not being locked by
1921 * \param[in] env execution environment
1922 * \param[in] pobj the directory to create files
1923 * \param[in] pattr the attributes of the directory
1924 * \param[in] lname the name of the created file/dir
1925 * \param[in] cattr the attributes of the file/dir
1926 * \param[in] spec create specification
1928 * \retval = 0 it is allowed to create file/dir under
1930 * \retval negative error not allowed to create file/dir
1931 * under the directory
1933 static int mdd_create_sanity_check(const struct lu_env *env,
1934 struct md_object *pobj,
1935 const struct lu_attr *pattr,
1936 const struct lu_name *lname,
1937 struct lu_attr *cattr,
1938 struct md_op_spec *spec)
1940 struct mdd_thread_info *info = mdd_env_info(env);
1941 struct lu_fid *fid = &info->mti_fid;
1942 struct mdd_object *obj = md2mdd_obj(pobj);
1943 struct mdd_device *m = mdo2mdd(pobj);
1944 bool check_perm = true;
1949 if (mdd_is_dead_obj(obj))
1953 * In some cases this lookup is not needed - we know before if name
1954 * exists or not because MDT performs lookup for it.
1955 * name length check is done in lookup.
1957 if (spec->sp_cr_lookup) {
1959 * Check if the name already exist, though it will be checked in
1960 * _index_insert also, for avoiding rolling back if exists
1963 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1964 MAY_WRITE | MAY_EXEC);
1966 RETURN(rc ? : -EEXIST);
1968 /* Permission is already being checked in mdd_lookup */
1972 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1977 if (pattr->la_mode & S_ISGID) {
1978 cattr->la_gid = pattr->la_gid;
1979 if (S_ISDIR(cattr->la_mode)) {
1980 cattr->la_mode |= S_ISGID;
1981 cattr->la_valid |= LA_MODE;
1985 rc = mdd_name_check(m, lname);
1989 switch (cattr->la_mode & S_IFMT) {
1991 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1993 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1994 RETURN(-ENAMETOOLONG);
2013 static int mdd_declare_object_create(const struct lu_env *env,
2014 struct mdd_device *mdd,
2015 struct mdd_object *p, struct mdd_object *c,
2016 struct lu_attr *attr,
2017 struct thandle *handle,
2018 const struct md_op_spec *spec,
2019 struct lu_buf *def_acl_buf,
2020 struct lu_buf *acl_buf,
2021 struct dt_allocation_hint *hint)
2025 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
2030 #ifdef CONFIG_FS_POSIX_ACL
2031 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2032 /* if dir, then can inherit default ACl */
2033 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2034 XATTR_NAME_ACL_DEFAULT,
2040 if (acl_buf->lb_len > 0) {
2041 rc = mdo_declare_attr_set(env, c, attr, handle);
2045 rc = mdo_declare_xattr_set(env, c, acl_buf,
2046 XATTR_NAME_ACL_ACCESS, 0, handle);
2051 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2055 /* replay case, create LOV EA from client data */
2056 if (spec->no_create ||
2057 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2058 const struct lu_buf *buf;
2060 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2061 spec->u.sp_ea.eadatalen);
2062 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2068 if (S_ISLNK(attr->la_mode)) {
2069 const char *target_name = spec->u.sp_symname;
2070 int sym_len = strlen(target_name);
2071 const struct lu_buf *buf;
2073 buf = mdd_buf_get_const(env, target_name, sym_len);
2074 rc = dt_declare_record_write(env, mdd_object_child(c),
2083 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2084 struct mdd_object *p, struct mdd_object *c,
2085 const struct lu_name *name,
2086 struct lu_attr *attr,
2087 struct thandle *handle,
2088 const struct md_op_spec *spec,
2089 struct linkea_data *ldata,
2090 struct lu_buf *def_acl_buf,
2091 struct lu_buf *acl_buf,
2092 struct dt_allocation_hint *hint)
2096 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2097 def_acl_buf, acl_buf, hint);
2101 if (S_ISDIR(attr->la_mode)) {
2102 rc = mdo_declare_ref_add(env, p, handle);
2107 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2108 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2112 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2114 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2115 name->ln_name, handle);
2119 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2124 la->la_valid = LA_CTIME | LA_MTIME;
2125 rc = mdo_declare_attr_set(env, p, la, handle);
2129 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2137 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2138 struct lu_attr *la, struct lu_buf *def_acl_buf,
2139 struct lu_buf *acl_buf)
2144 if (S_ISLNK(la->la_mode)) {
2145 acl_buf->lb_len = 0;
2146 def_acl_buf->lb_len = 0;
2150 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2151 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2152 XATTR_NAME_ACL_DEFAULT);
2153 mdd_read_unlock(env, pobj);
2155 /* If there are default ACL, fix mode/ACL by default ACL */
2156 def_acl_buf->lb_len = rc;
2157 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2158 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2159 acl_buf->lb_len = rc;
2160 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2163 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2164 /* If there are no default ACL, fix mode by mask */
2165 struct lu_ucred *uc = lu_ucred(env);
2167 /* The create triggered by MDT internal events, such as
2168 * LFSCK reset, will not contain valid "uc". */
2169 if (unlikely(uc != NULL))
2170 la->la_mode &= ~uc->uc_umask;
2172 acl_buf->lb_len = 0;
2173 def_acl_buf->lb_len = 0;
2180 * Create a metadata object and initialize it, set acl, xattr.
2182 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2183 struct mdd_object *son, struct lu_attr *attr,
2184 struct md_op_spec *spec, struct lu_buf *acl_buf,
2185 struct lu_buf *def_acl_buf,
2186 struct dt_allocation_hint *hint,
2187 struct thandle *handle)
2191 mdd_write_lock(env, son, MOR_TGT_CHILD);
2192 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2197 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2198 * created in declare phase, they also needs to be added to master
2199 * object as sub-directory entry. So it has to initialize the master
2200 * object, then set dir striped EA.(in mdo_xattr_set) */
2201 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2204 GOTO(err_destroy, rc);
2207 * in case of replay we just set LOVEA provided by the client
2208 * XXX: I think it would be interesting to try "old" way where
2209 * MDT calls this xattr_set(LOV) in a different transaction.
2210 * probably this way we code can be made better.
2213 /* During creation, there are only a few cases we need do xattr_set to
2215 * 1. regular file: see comments above.
2216 * 2. dir: inherit default striping or pool settings from parent.
2217 * 3. create striped directory with provided stripeEA.
2218 * 4. create striped directory because inherit default layout from the
2221 if (spec->no_create ||
2222 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2223 S_ISDIR(attr->la_mode)) {
2224 const struct lu_buf *buf;
2226 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2227 spec->u.sp_ea.eadatalen);
2228 rc = mdo_xattr_set(env, son, buf,
2229 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2233 GOTO(err_destroy, rc);
2236 #ifdef CONFIG_FS_POSIX_ACL
2237 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2238 S_ISDIR(attr->la_mode)) {
2239 /* set default acl */
2240 rc = mdo_xattr_set(env, son, def_acl_buf,
2241 XATTR_NAME_ACL_DEFAULT, 0,
2244 GOTO(err_destroy, rc);
2246 /* set its own acl */
2247 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2248 rc = mdo_xattr_set(env, son, acl_buf,
2249 XATTR_NAME_ACL_ACCESS,
2252 GOTO(err_destroy, rc);
2256 if (S_ISLNK(attr->la_mode)) {
2257 struct lu_ucred *uc = lu_ucred_assert(env);
2258 struct dt_object *dt = mdd_object_child(son);
2259 const char *target_name = spec->u.sp_symname;
2260 int sym_len = strlen(target_name);
2261 const struct lu_buf *buf;
2264 buf = mdd_buf_get_const(env, target_name, sym_len);
2265 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2267 CFS_CAP_SYS_RESOURCE_MASK);
2272 GOTO(err_initlized, rc = -EFAULT);
2276 if (unlikely(rc != 0)) {
2278 if (S_ISDIR(attr->la_mode)) {
2279 /* Drop the reference, no need to delete "."/"..",
2280 * because the object to be destroied directly. */
2281 rc2 = mdo_ref_del(env, son, handle);
2285 rc2 = mdo_ref_del(env, son, handle);
2289 mdo_destroy(env, son, handle);
2292 mdd_write_unlock(env, son);
2297 * Create object and insert it into namespace.
2299 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2300 const struct lu_name *lname, struct md_object *child,
2301 struct md_op_spec *spec, struct md_attr* ma)
2303 struct mdd_thread_info *info = mdd_env_info(env);
2304 struct lu_attr *la = &info->mti_la_for_fix;
2305 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2306 struct mdd_object *son = md2mdd_obj(child);
2307 struct mdd_device *mdd = mdo2mdd(pobj);
2308 struct lu_attr *attr = &ma->ma_attr;
2309 struct thandle *handle;
2310 struct lu_attr *pattr = &info->mti_pattr;
2311 struct lu_buf acl_buf;
2312 struct lu_buf def_acl_buf;
2313 struct linkea_data *ldata = &info->mti_link_data;
2314 const char *name = lname->ln_name;
2315 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2321 * Two operations have to be performed:
2323 * - an allocation of a new object (->do_create()), and
2325 * - an insertion into a parent index (->dio_insert()).
2327 * Due to locking, operation order is not important, when both are
2328 * successful, *but* error handling cases are quite different:
2330 * - if insertion is done first, and following object creation fails,
2331 * insertion has to be rolled back, but this operation might fail
2332 * also leaving us with dangling index entry.
2334 * - if creation is done first, is has to be undone if insertion
2335 * fails, leaving us with leaked space, which is neither good, nor
2338 * It seems that creation-first is simplest solution, but it is
2339 * sub-optimal in the frequent
2344 * case, because second mkdir is bound to create object, only to
2345 * destroy it immediately.
2347 * To avoid this follow local file systems that do double lookup:
2349 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2351 * 1. create (mdd_object_create_internal())
2353 * 2. insert (__mdd_index_insert(), lookup again)
2356 rc = mdd_la_get(env, mdd_pobj, pattr);
2360 /* Sanity checks before big job. */
2361 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2365 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2366 GOTO(out_free, rc = -EINPROGRESS);
2368 handle = mdd_trans_create(env, mdd);
2370 GOTO(out_free, rc = PTR_ERR(handle));
2372 acl_buf.lb_buf = info->mti_xattr_buf;
2373 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2374 def_acl_buf.lb_buf = info->mti_key;
2375 def_acl_buf.lb_len = sizeof(info->mti_key);
2376 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2380 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2382 memset(ldata, 0, sizeof(*ldata));
2383 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2384 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2387 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2388 &tfid, lname, 1, 0, ldata);
2390 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2391 mdd_object_fid(mdd_pobj),
2392 lname, 1, 0, ldata);
2395 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2396 handle, spec, ldata, &def_acl_buf, &acl_buf,
2401 rc = mdd_trans_start(env, mdd, handle);
2405 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2406 &def_acl_buf, hint, handle);
2410 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2411 mdd_write_lock(env, son, MOR_TGT_CHILD);
2412 rc = __mdd_orphan_add(env, son, handle);
2413 GOTO(out_volatile, rc);
2415 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2416 attr->la_mode, name, handle);
2418 GOTO(err_created, rc);
2420 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2423 /* update parent directory mtime/ctime */
2425 la->la_valid = LA_CTIME | LA_MTIME;
2426 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2428 GOTO(err_insert, rc);
2436 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2437 rc2 = __mdd_orphan_del(env, son, handle);
2439 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2440 S_ISDIR(attr->la_mode),
2446 mdd_write_lock(env, son, MOR_TGT_CHILD);
2447 if (S_ISDIR(attr->la_mode)) {
2448 /* Drop the reference, no need to delete "."/"..",
2449 * because the object is to be destroyed directly. */
2450 rc2 = mdo_ref_del(env, son, handle);
2452 mdd_write_unlock(env, son);
2457 /* For volatile files drop one link immediately, since there is
2458 * no filename in the namespace, and save any error returned. */
2459 rc2 = mdo_ref_del(env, son, handle);
2461 mdd_write_unlock(env, son);
2462 if (unlikely(rc == 0))
2467 /* Don't destroy the volatile object on success */
2468 if (likely(rc != 0))
2469 mdo_destroy(env, son, handle);
2470 mdd_write_unlock(env, son);
2473 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2474 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2475 rc = mdd_changelog_ns_store(env, mdd,
2476 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2477 S_ISREG(attr->la_mode) ? CL_CREATE :
2478 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2479 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2482 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2486 if (is_vmalloc_addr(ldata->ld_buf))
2487 /* if we vmalloced a large buffer drop it */
2488 lu_buf_free(ldata->ld_buf);
2490 /* The child object shouldn't be cached anymore */
2492 set_bit(LU_OBJECT_HEARD_BANSHEE,
2493 &child->mo_lu.lo_header->loh_flags);
2498 * Get locks on parents in proper order
2499 * RETURN: < 0 - error, rename_order if successful
2507 static int mdd_rename_order(const struct lu_env *env,
2508 struct mdd_device *mdd,
2509 struct mdd_object *src_pobj,
2510 const struct lu_attr *pattr,
2511 struct mdd_object *tgt_pobj)
2513 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2517 if (src_pobj == tgt_pobj)
2518 RETURN(MDD_RN_SAME);
2520 /* compared the parent child relationship of src_p&tgt_p */
2521 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2523 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2526 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2540 /* has not mdd_write{read}_lock on any obj yet. */
2541 static int mdd_rename_sanity_check(const struct lu_env *env,
2542 struct mdd_object *src_pobj,
2543 const struct lu_attr *pattr,
2544 struct mdd_object *tgt_pobj,
2545 const struct lu_attr *tpattr,
2546 struct mdd_object *sobj,
2547 const struct lu_attr *cattr,
2548 struct mdd_object *tobj,
2549 const struct lu_attr *tattr)
2554 /* XXX: when get here, sobj must NOT be NULL,
2555 * the other case has been processed in cld_rename
2556 * before mdd_rename and enable MDS_PERM_BYPASS. */
2559 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2563 /* XXX: when get here, "tobj == NULL" means tobj must
2564 * NOT exist (neither on remote MDS, such case has been
2565 * processed in cld_rename before mdd_rename and enable
2567 * So check may_create, but not check may_unlink. */
2569 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2570 (src_pobj != tgt_pobj));
2572 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2573 (src_pobj != tgt_pobj), 1);
2575 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2576 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2581 static int mdd_declare_rename(const struct lu_env *env,
2582 struct mdd_device *mdd,
2583 struct mdd_object *mdd_spobj,
2584 struct mdd_object *mdd_tpobj,
2585 struct mdd_object *mdd_sobj,
2586 struct mdd_object *mdd_tobj,
2587 const struct lu_name *tname,
2588 const struct lu_name *sname,
2590 struct linkea_data *ldata,
2591 struct thandle *handle)
2593 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2596 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2597 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2603 /* name from source dir */
2604 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2608 /* .. from source child */
2609 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2610 /* source child can be directory,
2611 * counted by source dir's nlink */
2612 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2615 if (mdd_spobj != mdd_tpobj) {
2616 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2621 rc = mdo_declare_index_insert(env, mdd_sobj,
2623 S_IFDIR, dotdot, handle);
2628 /* new target child can be directory,
2629 * counted by target dir's nlink */
2630 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2635 la->la_valid = LA_CTIME | LA_MTIME;
2636 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2640 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2644 la->la_valid = LA_CTIME;
2645 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2649 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2650 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2655 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2656 mdd_object_type(mdd_sobj),
2657 tname->ln_name, handle);
2661 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2662 /* delete target child in target parent directory */
2663 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2668 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2672 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2673 /* target child can be directory,
2674 * delete "." reference in target child directory */
2675 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2679 /* delete ".." reference in target parent directory */
2680 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2685 la->la_valid = LA_CTIME;
2686 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2690 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2695 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2702 /* src object can be remote that is why we use only fid and type of object */
2703 static int mdd_rename(const struct lu_env *env,
2704 struct md_object *src_pobj, struct md_object *tgt_pobj,
2705 const struct lu_fid *lf, const struct lu_name *lsname,
2706 struct md_object *tobj, const struct lu_name *ltname,
2709 const char *sname = lsname->ln_name;
2710 const char *tname = ltname->ln_name;
2711 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2712 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2713 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2714 struct mdd_device *mdd = mdo2mdd(src_pobj);
2715 struct mdd_object *mdd_sobj = NULL; /* source object */
2716 struct mdd_object *mdd_tobj = NULL;
2717 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2718 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2719 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2720 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2721 struct thandle *handle;
2722 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2723 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2724 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2727 bool tobj_locked = 0;
2728 unsigned cl_flags = 0;
2733 mdd_tobj = md2mdd_obj(tobj);
2735 mdd_sobj = mdd_object_find(env, mdd, lf);
2736 if (IS_ERR(mdd_sobj))
2737 RETURN(PTR_ERR(mdd_sobj));
2739 rc = mdd_la_get(env, mdd_sobj, cattr);
2741 GOTO(out_pending, rc);
2743 rc = mdd_la_get(env, mdd_spobj, pattr);
2745 GOTO(out_pending, rc);
2748 rc = mdd_la_get(env, mdd_tobj, tattr);
2750 GOTO(out_pending, rc);
2753 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2755 GOTO(out_pending, rc);
2757 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2758 mdd_sobj, cattr, mdd_tobj, tattr);
2760 GOTO(out_pending, rc);
2762 rc = mdd_name_check(mdd, ltname);
2764 GOTO(out_pending, rc);
2766 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2767 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2769 GOTO(out_pending, rc);
2771 handle = mdd_trans_create(env, mdd);
2773 GOTO(out_pending, rc = PTR_ERR(handle));
2775 memset(ldata, 0, sizeof(*ldata));
2776 mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj), lsname,
2777 mdd_object_fid(mdd_tpobj), ltname, 1, 0, ldata);
2778 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2779 mdd_tobj, lsname, ltname, ma, ldata, handle);
2783 rc = mdd_trans_start(env, mdd, handle);
2787 is_dir = S_ISDIR(cattr->la_mode);
2789 /* Remove source name from source directory */
2790 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2794 /* "mv dir1 dir2" needs "dir1/.." link update */
2795 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2796 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2798 GOTO(fixup_spobj2, rc);
2800 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2803 GOTO(fixup_spobj, rc);
2806 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2807 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2809 /* tname might been renamed to something else */
2810 GOTO(fixup_spobj, rc);
2813 /* Insert new fid with target name into target dir */
2814 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2817 GOTO(fixup_tpobj, rc);
2819 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2820 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2822 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2823 la->la_valid = LA_CTIME;
2824 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2826 GOTO(fixup_tpobj, rc);
2828 /* Update the linkEA for the source object */
2829 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2830 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2831 mdo2fid(mdd_tpobj), ltname, handle, ldata,
2834 /* Old files might not have EA entry */
2835 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2836 lsname, handle, NULL, 0);
2837 mdd_write_unlock(env, mdd_sobj);
2838 /* We don't fail the transaction if the link ea can't be
2839 updated -- fid2path will use alternate lookup method. */
2842 /* Remove old target object
2843 * For tobj is remote case cmm layer has processed
2844 * and set tobj to NULL then. So when tobj is NOT NULL,
2845 * it must be local one.
2847 if (tobj && mdd_object_exists(mdd_tobj)) {
2848 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2850 if (mdd_is_dead_obj(mdd_tobj)) {
2851 /* shld not be dead, something is wrong */
2852 CERROR("tobj is dead, something is wrong\n");
2856 mdo_ref_del(env, mdd_tobj, handle);
2858 /* Remove dot reference. */
2859 if (S_ISDIR(tattr->la_mode))
2860 mdo_ref_del(env, mdd_tobj, handle);
2863 /* fetch updated nlink */
2864 rc = mdd_la_get(env, mdd_tobj, tattr);
2866 CERROR("%s: Failed to get nlink for tobj "
2868 mdd2obd_dev(mdd)->obd_name,
2869 PFID(tpobj_fid), rc);
2870 GOTO(fixup_tpobj, rc);
2873 la->la_valid = LA_CTIME;
2874 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2876 CERROR("%s: Failed to set ctime for tobj "
2878 mdd2obd_dev(mdd)->obd_name,
2879 PFID(tpobj_fid), rc);
2880 GOTO(fixup_tpobj, rc);
2883 /* XXX: this transfer to ma will be removed with LOD/OSP */
2884 ma->ma_attr = *tattr;
2885 ma->ma_valid |= MA_INODE;
2886 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2889 CERROR("%s: Failed to unlink tobj "
2891 mdd2obd_dev(mdd)->obd_name,
2892 PFID(tpobj_fid), rc);
2893 GOTO(fixup_tpobj, rc);
2896 /* fetch updated nlink */
2897 rc = mdd_la_get(env, mdd_tobj, tattr);
2899 CERROR("%s: Failed to get nlink for tobj "
2901 mdd2obd_dev(mdd)->obd_name,
2902 PFID(tpobj_fid), rc);
2903 GOTO(fixup_tpobj, rc);
2905 /* XXX: this transfer to ma will be removed with LOD/OSP */
2906 ma->ma_attr = *tattr;
2907 ma->ma_valid |= MA_INODE;
2909 if (tattr->la_nlink == 0) {
2910 cl_flags |= CLF_RENAME_LAST;
2911 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2912 cl_flags |= CLF_RENAME_LAST_EXISTS;
2916 la->la_valid = LA_CTIME | LA_MTIME;
2917 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2919 GOTO(fixup_tpobj, rc);
2921 if (mdd_spobj != mdd_tpobj) {
2922 la->la_valid = LA_CTIME | LA_MTIME;
2923 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2925 GOTO(fixup_tpobj, rc);
2932 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2934 CWARN("tp obj fix error %d\n",rc2);
2936 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2937 !mdd_is_dead_obj(mdd_tobj)) {
2939 mdo_ref_add(env, mdd_tobj, handle);
2941 mdo_ref_add(env, mdd_tobj, handle);
2944 rc2 = __mdd_index_insert(env, mdd_tpobj,
2946 mdd_object_type(mdd_tobj),
2949 CWARN("tp obj fix error: rc = %d\n", rc2);
2954 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2955 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2957 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2958 mdd2obd_dev(mdd)->obd_name, rc2);
2961 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2964 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2965 mdd2obd_dev(mdd)->obd_name, rc2);
2970 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2971 mdd_object_type(mdd_sobj), sname,
2974 CWARN("sp obj fix error: rc = %d\n", rc2);
2979 mdd_write_unlock(env, mdd_tobj);
2982 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
2983 mdd_tobj, tpobj_fid, lf, spobj_fid,
2984 ltname, lsname, handle);
2987 mdd_trans_stop(env, mdd, rc, handle);
2990 mdd_object_put(env, mdd_sobj);
2995 * During migration once the parent FID has been changed,
2996 * we need update the parent FID in linkea.
2998 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2999 struct mdd_object *parent,
3000 struct mdd_object *child,
3001 const char *name, int namelen,
3002 struct thandle *handle,
3005 struct mdd_thread_info *info = mdd_env_info(env);
3006 struct linkea_data ldata = { NULL };
3007 struct lu_buf *buf = &info->mti_link_buf;
3013 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3014 if (buf->lb_buf == NULL)
3018 rc = mdd_links_read(env, child, &ldata);
3020 if (rc == -ENOENT || rc == -ENODATA)
3025 LASSERT(ldata.ld_leh != NULL);
3026 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3027 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3028 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3029 struct lu_name lname;
3032 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3035 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3036 lu_fid_eq(&fid, mdd_object_fid(parent))) {
3037 ldata.ld_lee = (struct link_ea_entry *)
3038 ((char *)ldata.ld_lee +
3043 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3044 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3045 lname.ln_namelen, lname.ln_name,
3046 PFID(mdd_object_fid(parent)));
3047 /* update to the new parent fid */
3048 linkea_entry_pack(ldata.ld_lee, &lname,
3049 mdd_object_fid(parent));
3051 rc = mdd_declare_links_add(env, child, handle, &ldata,
3054 rc = mdd_links_write(env, child, &ldata, handle);
3060 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3061 struct mdd_object *parent,
3062 struct mdd_object *child,
3063 const char *name, int namelen,
3064 struct thandle *handle)
3066 return mdd_linkea_update_child_internal(env, parent, child, name,
3067 namelen, handle, true);
3070 static int mdd_linkea_update_child(const struct lu_env *env,
3071 struct mdd_object *parent,
3072 struct mdd_object *child,
3073 const char *name, int namelen,
3074 struct thandle *handle)
3076 return mdd_linkea_update_child_internal(env, parent, child, name,
3077 namelen, handle, false);
3080 static int mdd_update_linkea_internal(const struct lu_env *env,
3081 struct mdd_object *mdd_pobj,
3082 struct mdd_object *mdd_sobj,
3083 struct mdd_object *mdd_tobj,
3084 const struct lu_name *child_name,
3085 struct linkea_data *ldata,
3086 struct thandle *handle,
3089 struct mdd_thread_info *info = mdd_env_info(env);
3094 LASSERT(ldata->ld_buf != NULL);
3097 /* If it is mulitple links file, we need update the name entry for
3099 LASSERT(ldata->ld_leh != NULL);
3100 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3101 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3102 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3103 struct mdd_object *pobj;
3104 struct lu_name lname;
3107 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3109 pobj = mdd_object_find(env, mdd, &fid);
3111 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3112 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3114 linkea_del_buf(ldata, &lname);
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));
3121 linkea_del_buf(ldata, &lname);
3122 mdd_object_put(env, pobj);
3126 if (pobj == mdd_pobj &&
3127 lname.ln_namelen == child_name->ln_namelen &&
3128 strncmp(lname.ln_name, child_name->ln_name,
3129 lname.ln_namelen) == 0) {
3130 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3131 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3133 linkea_del_buf(ldata, &lname);
3134 mdd_object_put(env, pobj);
3138 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3139 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3140 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3143 /* Remove source name from source directory */
3144 /* Insert new fid with target name into target dir */
3145 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3150 rc = mdo_declare_index_insert(env, pobj,
3151 mdd_object_fid(mdd_tobj),
3152 mdd_object_type(mdd_tobj),
3153 lname.ln_name, handle);
3157 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3161 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3165 char *tmp_name = info->mti_key;
3167 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3168 /* lnamelen is too big(> NAME_MAX + 16),
3169 * something wrong about this linkea, let's
3171 linkea_del_buf(ldata, &lname);
3172 mdd_object_put(env, pobj);
3176 /* Note: lname might be without \0 at the end, see
3177 * linkea_entry_unpack(), let's add extra \0 by
3179 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3180 lname.ln_namelen, lname.ln_name);
3181 lname.ln_name = tmp_name;
3183 /* Let's check if this linkEA still valid, before
3184 * it might be packed into the RPC buffer. */
3185 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3186 &info->mti_fid, NULL);
3188 !lu_fid_eq(&info->mti_fid,
3189 mdd_object_fid(mdd_sobj))) {
3190 /* skip invalid linkea entry */
3191 linkea_del_buf(ldata, &lname);
3192 mdd_object_put(env, pobj);
3196 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3200 rc = __mdd_index_insert(env, pobj,
3201 mdd_object_fid(mdd_tobj),
3202 mdd_object_type(mdd_tobj),
3207 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3208 rc = mdo_ref_add(env, mdd_tobj, handle);
3209 mdd_write_unlock(env, mdd_tobj);
3213 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3214 mdo_ref_del(env, mdd_sobj, handle);
3215 mdd_write_unlock(env, mdd_sobj);
3218 mdd_object_put(env, pobj);
3222 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3229 static int mdd_migrate_xattrs(const struct lu_env *env,
3230 struct mdd_object *mdd_sobj,
3231 struct mdd_object *mdd_tobj)
3233 struct mdd_thread_info *info = mdd_env_info(env);
3234 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3236 struct thandle *handle;
3242 struct lu_buf list_xbuf;
3246 /* retrieve xattr list from the old object */
3247 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3248 if (list_xsize == -ENODATA)
3254 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3255 if (info->mti_big_buf.lb_buf == NULL)
3258 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3259 list_xbuf.lb_len = list_xsize;
3260 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3265 xname = list_xbuf.lb_buf;
3267 xlen = strnlen(xname, rem - 1) + 1;
3268 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3269 strcmp(XATTR_NAME_LMA, xname) == 0 ||
3270 strcmp(XATTR_NAME_LMV, xname) == 0)
3273 /* For directory, if there are default layout, migrate here */
3274 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3275 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3278 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3279 if (xsize == -ENODATA)
3284 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3285 if (info->mti_link_buf.lb_buf == NULL)
3286 GOTO(out, rc = -ENOMEM);
3288 xbuf.lb_len = xsize;
3289 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3290 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3296 handle = mdd_trans_create(env, mdd);
3298 GOTO(out, rc = PTR_ERR(handle));
3300 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3303 GOTO(stop_trans, rc);
3304 /* Note: this transaction is part of migration, and it is not
3305 * the last step of migration, so we set th_local = 1 to avoid
3306 * update last rcvd for this transaction */
3307 handle->th_local = 1;
3308 rc = mdd_trans_start(env, mdd, handle);
3310 GOTO(stop_trans, rc);
3312 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3314 GOTO(stop_trans, rc = 0);
3317 GOTO(stop_trans, rc);
3319 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3326 memmove(xname, xname + xlen, rem);
3332 static int mdd_declare_migrate_create(const struct lu_env *env,
3333 struct mdd_object *mdd_pobj,
3334 struct mdd_object *mdd_sobj,
3335 struct mdd_object *mdd_tobj,
3336 struct md_op_spec *spec,
3338 union lmv_mds_md *mgr_ea,
3339 struct linkea_data *ldata,
3340 struct thandle *handle)
3342 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3343 const struct lu_buf *buf;
3347 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3348 handle, spec, NULL);
3352 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3357 if (S_ISLNK(la->la_mode)) {
3358 const char *target_name = spec->u.sp_symname;
3359 int sym_len = strlen(target_name);
3360 const struct lu_buf *buf;
3362 buf = mdd_buf_get_const(env, target_name, sym_len);
3363 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3367 } else if (S_ISDIR(la->la_mode)) {
3368 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3374 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3375 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3376 spec->u.sp_ea.eadatalen);
3377 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3383 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3384 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3385 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3390 la_flag->la_valid = LA_FLAGS;
3391 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3392 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3397 static int mdd_migrate_create(const struct lu_env *env,
3398 struct mdd_object *mdd_pobj,
3399 struct mdd_object *mdd_sobj,
3400 struct mdd_object *mdd_tobj,
3401 const struct lu_name *lname,
3404 struct mdd_thread_info *info = mdd_env_info(env);
3405 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3406 struct md_op_spec *spec = &info->mti_spec;
3407 struct lu_buf lmm_buf = { NULL };
3408 struct lu_buf link_buf = { NULL };
3409 const struct lu_buf *buf;
3410 struct thandle *handle;
3411 struct lmv_mds_md_v1 *mgr_ea;
3412 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3413 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3415 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3419 /* prepare spec for create */
3420 memset(spec, 0, sizeof(*spec));
3421 spec->sp_cr_lookup = 0;
3422 spec->sp_feat = &dt_directory_features;
3423 if (S_ISLNK(la->la_mode)) {
3424 buf = lu_buf_check_and_alloc(
3425 &mdd_env_info(env)->mti_big_buf,
3428 link_buf.lb_len = la->la_size + 1;
3429 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3430 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3432 rc = rc != 0 ? rc : -EFAULT;
3433 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3434 mdd2obd_dev(mdd)->obd_name,
3435 PFID(mdd_object_fid(mdd_sobj)), rc);
3438 spec->u.sp_symname = link_buf.lb_buf;
3439 } else if (S_ISREG(la->la_mode)) {
3440 /* retrieve lov of the old object */
3441 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3442 if (rc != 0 && rc != -ENODATA)
3444 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3445 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3446 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3447 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3449 } else if (S_ISDIR(la->la_mode)) {
3450 rc = mdd_links_read(env, mdd_sobj, ldata);
3451 if (rc < 0 && rc != -ENODATA)
3455 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3456 memset(mgr_ea, 0, sizeof(*mgr_ea));
3457 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3458 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3459 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3460 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3461 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3462 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3464 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3466 handle = mdd_trans_create(env, mdd);
3468 GOTO(out_free, rc = PTR_ERR(handle));
3470 /* Note: this transaction is part of migration, and it is not
3471 * the last step of migration, so we set th_local = 1 to avoid
3472 * update last rcvd for this transaction */
3473 handle->th_local = 1;
3474 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3476 (union lmv_mds_md *)info->mti_xattr_buf,
3479 GOTO(stop_trans, rc);
3481 rc = mdd_trans_start(env, mdd, handle);
3483 GOTO(stop_trans, rc);
3485 /* create the target object */
3486 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3489 GOTO(stop_trans, rc);
3491 if (S_ISDIR(la->la_mode)) {
3492 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3494 GOTO(stop_trans, rc);
3497 /* Set MIGRATE EA on the source inode, so once the migration needs
3498 * to be re-done during failover, the re-do process can locate the
3499 * target object which is already being created. */
3500 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3501 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3502 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3504 GOTO(stop_trans, rc);
3506 /* Set immutable flag, so any modification is disabled until
3507 * the migration is done. Once the migration is interrupted,
3508 * if the resume process find the migrating object has both
3509 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3510 * flag and approve the migration */
3511 la_flag->la_valid = LA_FLAGS;
3512 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3513 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3515 if (handle != NULL) {
3518 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3523 if (lmm_buf.lb_buf != NULL)
3524 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3528 static int mdd_migrate_entries(const struct lu_env *env,
3529 struct mdd_object *mdd_sobj,
3530 struct mdd_object *mdd_tobj)
3532 struct dt_object *next = mdd_object_child(mdd_sobj);
3533 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3534 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3535 struct thandle *handle;
3537 const struct dt_it_ops *iops;
3540 struct lu_dirent *ent;
3543 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3547 if (!dt_try_as_dir(env, next))
3548 GOTO(out_ent, rc = -ENOTDIR);
3550 * iterate directories
3552 iops = &next->do_index_ops->dio_it;
3553 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3555 GOTO(out_ent, rc = PTR_ERR(it));
3557 rc = iops->load(env, it, 0);
3559 rc = iops->next(env, it);
3563 * At this point and across for-loop:
3565 * rc == 0 -> ok, proceed.
3566 * rc > 0 -> end of directory.
3570 struct mdd_object *child;
3571 char *name = mdd_env_info(env)->mti_key;
3575 bool target_exist = false;
3578 len = iops->key_size(env, it);
3582 result = iops->rec(env, it, (struct dt_rec *)ent,
3583 LUDA_FID | LUDA_TYPE);
3584 if (result == -ESTALE)
3591 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3592 recsize = le16_to_cpu(ent->lde_reclen);
3594 /* Insert new fid with target name into target dir */
3595 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3596 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3597 ent->lde_name[1] == '.'))
3600 child = mdd_object_find(env, mdd, &ent->lde_fid);
3602 GOTO(out, rc = PTR_ERR(child));
3604 mdd_write_lock(env, child, MOR_SRC_CHILD);
3605 is_dir = S_ISDIR(mdd_object_type(child));
3607 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3609 /* Check whether the name has been inserted to the target */
3610 if (dt_try_as_dir(env, dt_tobj)) {
3611 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3613 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3614 (struct dt_key *)name);
3615 if (unlikely(rc == 0))
3616 target_exist = true;
3619 handle = mdd_trans_create(env, mdd);
3621 GOTO(out, rc = PTR_ERR(handle));
3623 /* Note: this transaction is part of migration, and it is not
3624 * the last step of migration, so we set th_local = 1 to avoid
3625 * updating last rcvd for this transaction */
3626 handle->th_local = 1;
3627 if (likely(!target_exist)) {
3628 rc = mdo_declare_index_insert(env, mdd_tobj,
3630 mdd_object_type(child),
3636 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3642 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3647 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3651 /* Update .. for child */
3652 rc = mdo_declare_index_delete(env, child, dotdot,
3657 rc = mdo_declare_index_insert(env, child,
3658 mdd_object_fid(mdd_tobj),
3659 S_IFDIR, dotdot, handle);
3664 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3671 rc = mdd_trans_start(env, mdd, handle);
3673 CERROR("%s: transaction start failed: rc = %d\n",
3674 mdd2obd_dev(mdd)->obd_name, rc);
3678 if (likely(!target_exist)) {
3679 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3680 mdd_object_type(child),
3686 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3691 rc = __mdd_index_delete_only(env, child, dotdot,
3696 rc = __mdd_index_insert_only(env, child,
3697 mdd_object_fid(mdd_tobj), S_IFDIR,
3703 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3704 strlen(name), handle);
3707 mdd_write_unlock(env, child);
3708 mdd_object_put(env, child);
3709 rc1 = mdd_trans_stop(env, mdd, rc, handle);
3716 result = iops->next(env, it);
3717 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3718 GOTO(out, rc = -EINTR);
3720 if (result == -ESTALE)
3722 } while (result == 0);
3725 iops->fini(env, it);
3727 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3731 static int mdd_declare_update_linkea(const struct lu_env *env,
3732 struct mdd_object *mdd_pobj,
3733 struct mdd_object *mdd_sobj,
3734 struct mdd_object *mdd_tobj,
3735 const struct lu_name *child_name,
3736 struct linkea_data *ldata,
3737 struct thandle *handle)
3739 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3740 child_name, ldata, handle, 1);
3743 static int mdd_update_linkea(const struct lu_env *env,
3744 struct mdd_object *mdd_pobj,
3745 struct mdd_object *mdd_sobj,
3746 struct mdd_object *mdd_tobj,
3747 const struct lu_name *child_name,
3748 struct linkea_data *ldata,
3749 struct thandle *handle)
3751 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3752 child_name, ldata, handle, 0);
3755 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3756 struct mdd_object *mdd_pobj,
3757 struct mdd_object *mdd_sobj,
3758 struct mdd_object *mdd_tobj,
3759 const struct lu_name *lname,
3761 struct lu_attr *parent_la,
3762 struct linkea_data *ldata,
3763 struct thandle *handle)
3765 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3768 /* Revert IMMUTABLE flag */
3769 la_flag->la_valid = LA_FLAGS;
3770 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3771 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3775 /* delete entry from source dir */
3776 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3780 if (ldata->ld_buf != NULL) {
3781 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3782 mdd_tobj, lname, ldata, handle);
3787 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3788 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3793 handle->th_complex = 1;
3794 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3796 LU_XATTR_REPLACE, handle);
3801 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3802 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3808 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3809 mdd_object_type(mdd_tobj),
3810 lname->ln_name, handle);
3814 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3818 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3819 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3824 /* delete old object */
3825 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3829 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3830 /* delete old object */
3831 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3834 /* set nlink to 0 */
3835 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3840 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3844 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3849 static int mdd_migrate_update_name(const struct lu_env *env,
3850 struct mdd_object *mdd_pobj,
3851 struct mdd_object *mdd_sobj,
3852 struct mdd_object *mdd_tobj,
3853 const struct lu_name *lname,
3856 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3857 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3858 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3859 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3860 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3861 struct thandle *handle;
3862 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3863 const char *name = lname->ln_name;
3867 /* update time for parent */
3868 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3869 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3870 p_la->la_valid = LA_CTIME;
3872 rc = mdd_la_get(env, mdd_sobj, so_attr);
3876 ldata->ld_buf = NULL;
3877 rc = mdd_links_read(env, mdd_sobj, ldata);
3878 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3881 handle = mdd_trans_create(env, mdd);
3883 RETURN(PTR_ERR(handle));
3885 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3886 lname, so_attr, p_la, ldata,
3889 /* If the migration can not be fit in one transaction, just
3890 * leave it in the original MDT */
3892 GOTO(stop_trans, rc = 0);
3894 GOTO(stop_trans, rc);
3897 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3898 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3899 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3900 PFID(mdd_object_fid(mdd_tobj)));
3902 rc = mdd_trans_start(env, mdd, handle);
3904 GOTO(stop_trans, rc);
3906 /* Revert IMMUTABLE flag */
3907 la_flag->la_valid = LA_FLAGS;
3908 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3909 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3911 GOTO(stop_trans, rc);
3913 /* Remove source name from source directory */
3914 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3916 GOTO(stop_trans, rc);
3918 if (ldata->ld_buf != NULL) {
3919 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3920 lname, ldata, handle);
3922 GOTO(stop_trans, rc);
3925 if (S_ISREG(so_attr->la_mode)) {
3926 if (so_attr->la_nlink == 1) {
3927 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3929 if (rc != 0 && rc != -ENODATA)
3930 GOTO(stop_trans, rc);
3932 rc = mdo_xattr_set(env, mdd_tobj, NULL,
3934 LU_XATTR_REPLACE, handle);
3936 GOTO(stop_trans, rc);
3940 /* Insert new fid with target name into target dir */
3941 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3942 mdd_object_type(mdd_tobj), name, handle);
3944 GOTO(stop_trans, rc);
3946 linkea_add_buf(ldata, lname, mdd_object_fid(mdd_pobj));
3947 rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3950 GOTO(stop_trans, rc);
3952 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3954 /* Increase mod_count to add the source object to the orphan list,
3955 * so if other clients still send RPC to the old object, then these
3956 * objects can help the request to find the new object, see
3957 * mdt_reint_open() */
3958 mdd_sobj->mod_count++;
3959 rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3960 mdd_sobj->mod_count--;
3962 GOTO(out_unlock, rc);
3964 mdo_ref_del(env, mdd_sobj, handle);
3966 mdo_ref_del(env, mdd_sobj, handle);
3968 /* Get the attr again after ref_del */
3969 rc = mdd_la_get(env, mdd_sobj, so_attr);
3971 GOTO(out_unlock, rc);
3973 ma->ma_attr = *so_attr;
3974 ma->ma_valid |= MA_INODE;
3976 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3978 GOTO(out_unlock, rc);
3981 mdd_write_unlock(env, mdd_sobj);
3984 mdd_trans_stop(env, mdd, rc, handle);
3990 * Check whether we should migrate the file/dir
3992 * < 0 permission check failed or other error.
3993 * = 0 the file can be migrated.
3994 * > 0 the file does not need to be migrated, mostly
3995 * for multiple link file
3997 static int mdd_migrate_sanity_check(const struct lu_env *env,
3998 struct mdd_object *pobj,
3999 const struct lu_attr *pattr,
4000 struct mdd_object *sobj,
4001 struct lu_attr *sattr)
4003 struct mdd_thread_info *info = mdd_env_info(env);
4004 struct linkea_data *ldata = &info->mti_link_data;
4006 struct lu_buf *mgr_buf;
4012 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4013 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4014 if (mgr_buf->lb_buf == NULL)
4017 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4019 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4021 /* If the object has migrateEA, it means IMMUTE flag
4022 * is being set by previous migration process, so it
4023 * needs to override the IMMUTE flag, otherwise the
4024 * following sanity check will fail */
4025 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4026 LMV_HASH_FLAG_MIGRATION) {
4027 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4029 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4030 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4031 mdd2obd_dev(mdd)->obd_name,
4032 PFID(mdd_object_fid(sobj)));
4036 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4037 sobj, sattr, NULL, NULL);
4041 /* Then it will check if the file should be migrated. If the file
4042 * has mulitple links, we only need migrate the file if all of its
4043 * entries has been migrated to the remote MDT */
4044 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4047 rc = mdd_links_read(env, sobj, ldata);
4049 /* For multiple links files, if there are no linkEA data at all,
4050 * means the file might be created before linkEA is enabled, and
4051 * all of its links should not be migrated yet, otherwise it
4052 * should have some linkEA there */
4053 if (rc == -ENOENT || rc == -ENODATA)
4058 /* If there are still links locally, then the file will not be
4060 LASSERT(ldata->ld_leh != NULL);
4061 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4062 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4063 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4064 struct mdd_object *lpobj;
4065 struct lu_name lname;
4068 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4070 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4072 lpobj = mdd_object_find(env, mdd, &fid);
4073 if (IS_ERR(lpobj)) {
4074 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
4075 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
4080 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
4081 CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
4082 PFID(&fid), lname.ln_namelen, lname.ln_name);
4083 mdd_object_put(env, lpobj);
4087 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4088 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4089 lname.ln_name, PFID(&fid));
4090 mdd_object_put(env, lpobj);
4098 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4099 struct md_object *sobj, const struct lu_name *lname,
4100 struct md_object *tobj, struct md_attr *ma)
4102 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4103 struct mdd_device *mdd = mdo2mdd(pobj);
4104 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4105 struct mdd_object *mdd_tobj = NULL;
4106 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4107 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4111 /* If the file will being migrated, it will check whether
4112 * the file is being opened by someone else right now */
4113 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4114 if (mdd_sobj->mod_count > 0) {
4115 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4116 mdd2obd_dev(mdd)->obd_name,
4117 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4118 mdd_sobj->mod_count, -EBUSY);
4119 mdd_read_unlock(env, mdd_sobj);
4120 GOTO(put, rc = -EBUSY);
4122 mdd_read_unlock(env, mdd_sobj);
4124 rc = mdd_la_get(env, mdd_sobj, so_attr);
4128 rc = mdd_la_get(env, mdd_pobj, pattr);
4132 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4139 /* Sigh, it is impossible to finish all of migration in a single
4140 * transaction, for example migrating big directory entries to the
4141 * new MDT, it needs insert all of name entries of children in the
4144 * So migration will be done in multiple steps and transactions.
4146 * 1. create an orphan object on the remote MDT in one transaction.
4147 * 2. migrate extend attributes to the new target file/directory.
4148 * 3. For directory, migrate the entries to the new MDT and update
4149 * linkEA of each children. Because we can not migrate all entries
4150 * in a single transaction, so the migrating directory will become
4151 * a striped directory during migration, so once the process is
4152 * interrupted, the directory is still accessible. (During lookup,
4153 * client will locate the name by searching both original and target
4155 * 4. Finally, update the name/FID to point to the new file/directory
4156 * in a separate transaction.
4159 /* step 1: Check whether the orphan object has been created, and create
4160 * orphan object on the remote MDT if needed */
4161 mdd_tobj = md2mdd_obj(tobj);
4162 if (!mdd_object_exists(mdd_tobj)) {
4163 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4169 LASSERT(mdd_object_exists(mdd_tobj));
4170 /* step 2: migrate xattr */
4171 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4175 /* step 3: migrate name entries to the orphan object */
4176 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4177 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4180 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4181 OBD_FAIL_MDS_REINT_NET_REP)))
4184 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4187 LASSERT(mdd_object_exists(mdd_tobj));
4188 /* step 4: update name entry to the new object */
4189 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4197 const struct md_dir_operations mdd_dir_ops = {
4198 .mdo_is_subdir = mdd_is_subdir,
4199 .mdo_lookup = mdd_lookup,
4200 .mdo_create = mdd_create,
4201 .mdo_rename = mdd_rename,
4202 .mdo_link = mdd_link,
4203 .mdo_unlink = mdd_unlink,
4204 .mdo_create_data = mdd_create_data,
4205 .mdo_migrate = mdd_migrate,