4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_dir.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@intel.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_mds.h>
44 #include <lustre_fid.h>
46 #include "mdd_internal.h"
48 static const char dot[] = ".";
49 static const char dotdot[] = "..";
51 static struct lu_name lname_dotdot = {
52 .ln_name = (char *) dotdot,
53 .ln_namelen = sizeof(dotdot) - 1,
57 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
59 if (!lu_name_is_valid(ln))
61 else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
67 /* Get FID from name and parent */
69 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
70 const struct lu_attr *pattr, const struct lu_name *lname,
71 struct lu_fid* fid, int mask)
73 const char *name = lname->ln_name;
74 const struct dt_key *key = (const struct dt_key *)name;
75 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
76 struct mdd_device *m = mdo2mdd(pobj);
77 struct dt_object *dir = mdd_object_child(mdd_obj);
81 if (unlikely(mdd_is_dead_obj(mdd_obj)))
84 if (!mdd_object_exists(mdd_obj))
87 if (mdd_object_remote(mdd_obj)) {
88 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
89 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
92 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
97 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
98 dt_try_as_dir(env, dir)))
99 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
106 int mdd_lookup(const struct lu_env *env,
107 struct md_object *pobj, const struct lu_name *lname,
108 struct lu_fid *fid, struct md_op_spec *spec)
110 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
114 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr);
118 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
119 (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
123 /** Read the link EA into a temp buffer.
124 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
125 * A pointer to the buffer is stored in \a ldata::ld_buf.
129 static int __mdd_links_read(const struct lu_env *env,
130 struct mdd_object *mdd_obj,
131 struct linkea_data *ldata)
135 if (!mdd_object_exists(mdd_obj))
138 /* First try a small buf */
139 LASSERT(env != NULL);
140 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
142 if (ldata->ld_buf->lb_buf == NULL)
145 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
147 /* Buf was too small, figure out what we need. */
148 lu_buf_free(ldata->ld_buf);
149 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
153 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
154 if (ldata->ld_buf->lb_buf == NULL)
156 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
160 lu_buf_free(ldata->ld_buf);
161 ldata->ld_buf = NULL;
165 return linkea_init(ldata);
168 static int mdd_links_read(const struct lu_env *env,
169 struct mdd_object *mdd_obj,
170 struct linkea_data *ldata)
174 rc = __mdd_links_read(env, mdd_obj, ldata);
176 rc = linkea_init(ldata);
181 static int mdd_links_read_with_rec(const struct lu_env *env,
182 struct mdd_object *mdd_obj,
183 struct linkea_data *ldata)
187 rc = __mdd_links_read(env, mdd_obj, ldata);
189 rc = linkea_init_with_rec(ldata);
195 * Get parent FID of the directory
197 * Read parent FID from linkEA, if that fails, then do lookup
198 * dotdot to get the parent FID.
200 * \param[in] env execution environment
201 * \param[in] obj object from which to find the parent FID
202 * \param[in] attr attribute of the object
203 * \param[out] fid fid to get the parent FID
205 * \retval 0 if getting the parent FID succeeds.
206 * \retval negative errno if getting the parent FID fails.
208 static inline int mdd_parent_fid(const struct lu_env *env,
209 struct mdd_object *obj,
210 const struct lu_attr *attr,
213 struct mdd_thread_info *info = mdd_env_info(env);
214 struct linkea_data ldata = { NULL };
215 struct lu_buf *buf = &info->mti_link_buf;
216 struct lu_name lname;
221 LASSERT(S_ISDIR(mdd_object_type(obj)));
223 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
224 if (buf->lb_buf == NULL)
225 GOTO(lookup, rc = 0);
228 rc = mdd_links_read_with_rec(env, obj, &ldata);
232 LASSERT(ldata.ld_leh != NULL);
233 /* Directory should only have 1 parent */
234 if (ldata.ld_leh->leh_reccount > 1)
237 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
239 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, &lname, fid);
240 if (likely(fid_is_sane(fid)))
243 rc = __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
248 * For root fid use special function, which does not compare version component
249 * of fid. Version component is different for root fids on all MDTs.
251 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
253 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
254 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
258 * return 1: if lf is the fid of the ancestor of p1;
261 * return -EREMOTE: if remote object is found, in this
262 * case fid of remote object is saved to @pf;
264 * otherwise: values < 0, errors.
266 static int mdd_is_parent(const struct lu_env *env,
267 struct mdd_device *mdd,
268 struct mdd_object *p1,
269 const struct lu_attr *attr,
270 const struct lu_fid *lf,
273 struct mdd_object *parent = NULL;
278 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
279 pfid = &mdd_env_info(env)->mti_fid;
281 /* Check for root first. */
282 if (mdd_is_root(mdd, mdo2fid(p1)))
286 /* this is done recursively */
287 rc = mdd_parent_fid(env, p1, attr, pfid);
290 if (mdd_is_root(mdd, pfid))
292 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
294 if (lu_fid_eq(pfid, lf))
297 mdd_object_put(env, parent);
299 parent = mdd_object_find(env, mdd, pfid);
301 GOTO(out, rc = PTR_ERR(parent));
303 if (!mdd_object_exists(parent))
304 GOTO(out, rc = -EINVAL);
310 if (parent && !IS_ERR(parent))
311 mdd_object_put(env, parent);
316 * No permission check is needed.
318 * returns 1: if fid is ancestor of @mo;
319 * returns 0: if fid is not an ancestor of @mo;
321 * returns EREMOTE if remote object is found, fid of remote object is saved to
324 * returns < 0: if error
326 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
327 const struct lu_fid *fid, struct lu_fid *sfid)
329 struct mdd_device *mdd = mdo2mdd(mo);
330 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
334 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
337 rc = mdd_la_get(env, md2mdd_obj(mo), attr);
341 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
345 } else if (rc == 1) {
346 /* found @fid is parent */
354 * Check that @dir contains no entries except (possibly) dot and dotdot.
359 * -ENOTDIR not a directory object
360 * -ENOTEMPTY not empty
364 static int mdd_dir_is_empty(const struct lu_env *env,
365 struct mdd_object *dir)
368 struct dt_object *obj;
369 const struct dt_it_ops *iops;
373 obj = mdd_object_child(dir);
374 if (!dt_try_as_dir(env, obj))
377 iops = &obj->do_index_ops->dio_it;
378 it = iops->init(env, obj, LUDA_64BITHASH);
380 result = iops->get(env, it, (const struct dt_key *)"");
383 for (result = 0, i = 0; result == 0 && i < 3; ++i)
384 result = iops->next(env, it);
387 else if (result == 1)
389 } else if (result == 0)
391 * Huh? Index contains no zero key?
398 result = PTR_ERR(it);
403 * Determine if the target object can be hard linked, and right now it only
404 * checks if the link count reach the maximum limit. Note: for ldiskfs, the
405 * directory nlink count might exceed the maximum link count(see
406 * osd_object_ref_add), so it only check nlink for non-directories.
408 * \param[in] env thread environment
409 * \param[in] obj object being linked to
410 * \param[in] la attributes of \a obj
412 * \retval 0 if \a obj can be hard linked
413 * \retval negative error if \a obj is a directory or has too
416 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
417 const struct lu_attr *la)
419 struct mdd_device *m = mdd_obj2mdd_dev(obj);
424 /* Subdir count limitation can be broken through
425 * (see osd_object_ref_add), so only check non-directory here. */
426 if (!S_ISDIR(la->la_mode) &&
427 la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
434 * Check whether it may create the cobj under the pobj.
436 * \param[in] env execution environment
437 * \param[in] pobj the parent directory
438 * \param[in] pattr the attribute of the parent directory
439 * \param[in] cobj the child to be created
440 * \param[in] check_perm if check WRITE|EXEC permission for parent
442 * \retval = 0 create the child under this dir is allowed
443 * \retval negative errno create the child under this dir is
446 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
447 const struct lu_attr *pattr, struct mdd_object *cobj,
453 if (cobj && mdd_object_exists(cobj))
456 if (mdd_is_dead_obj(pobj))
460 rc = mdd_permission_internal_locked(env, pobj, pattr,
461 MAY_WRITE | MAY_EXEC,
467 * Check whether can unlink from the pobj in the case of "cobj == NULL".
469 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
470 const struct lu_attr *pattr, const struct lu_attr *attr)
475 if (mdd_is_dead_obj(pobj))
478 if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
481 rc = mdd_permission_internal_locked(env, pobj, pattr,
482 MAY_WRITE | MAY_EXEC,
487 if (pattr->la_flags & LUSTRE_APPEND_FL)
494 * pobj == NULL is remote ops case, under such case, pobj's
495 * VTX feature has been checked already, no need check again.
497 static inline int mdd_is_sticky(const struct lu_env *env,
498 struct mdd_object *pobj,
499 const struct lu_attr *pattr,
500 struct mdd_object *cobj,
501 const struct lu_attr *cattr)
503 struct lu_ucred *uc = lu_ucred_assert(env);
506 LASSERT(pattr != NULL);
507 if (!(pattr->la_mode & S_ISVTX) ||
508 (pattr->la_uid == uc->uc_fsuid))
512 LASSERT(cattr != NULL);
513 if (cattr->la_uid == uc->uc_fsuid)
516 return !md_capable(uc, CFS_CAP_FOWNER);
519 static int mdd_may_delete_entry(const struct lu_env *env,
520 struct mdd_object *pobj,
521 const struct lu_attr *pattr,
526 LASSERT(pobj != NULL);
527 if (!mdd_object_exists(pobj))
530 if (mdd_is_dead_obj(pobj))
535 rc = mdd_permission_internal_locked(env, pobj, pattr,
536 MAY_WRITE | MAY_EXEC,
542 if (pattr->la_flags & LUSTRE_APPEND_FL)
549 * Check whether it may delete the cobj from the pobj.
552 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
553 const struct lu_attr *tpattr, struct mdd_object *tobj,
554 const struct lu_attr *tattr, const struct lu_attr *cattr,
555 int check_perm, int check_empty)
561 LASSERT(tpattr != NULL);
562 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
570 if (!mdd_object_exists(tobj))
573 if (mdd_is_dead_obj(tobj))
576 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
579 if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
582 /* additional check the rename case */
584 if (S_ISDIR(cattr->la_mode)) {
585 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
587 if (!S_ISDIR(tattr->la_mode))
590 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
592 } else if (S_ISDIR(tattr->la_mode))
596 if (S_ISDIR(tattr->la_mode) && check_empty)
597 rc = mdd_dir_is_empty(env, tobj);
603 * Check whether it can create the link file(linked to @src_obj) under
604 * the target directory(@tgt_obj), and src_obj has been locked by
607 * \param[in] env execution environment
608 * \param[in] tgt_obj the target directory
609 * \param[in] tattr attributes of target directory
610 * \param[in] lname the link name
611 * \param[in] src_obj source object for link
612 * \param[in] cattr attributes for source object
614 * \retval = 0 it is allowed to create the link file under tgt_obj
615 * \retval negative error not allowed to create the link file
617 static int mdd_link_sanity_check(const struct lu_env *env,
618 struct mdd_object *tgt_obj,
619 const struct lu_attr *tattr,
620 const struct lu_name *lname,
621 struct mdd_object *src_obj,
622 const struct lu_attr *cattr)
624 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
628 if (!mdd_object_exists(src_obj))
631 if (mdd_is_dead_obj(src_obj))
634 /* Local ops, no lookup before link, check filename length here. */
635 rc = mdd_name_check(m, lname);
639 if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
642 if (S_ISDIR(mdd_object_type(src_obj)))
645 LASSERT(src_obj != tgt_obj);
646 rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
650 rc = __mdd_may_link(env, src_obj, cattr);
655 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
656 const char *name, struct thandle *handle)
658 struct dt_object *next = mdd_object_child(pobj);
662 if (dt_try_as_dir(env, next))
663 rc = dt_delete(env, next, (struct dt_key *)name, handle);
670 static int __mdd_index_insert_only(const struct lu_env *env,
671 struct mdd_object *pobj,
672 const struct lu_fid *lf, __u32 type,
674 struct thandle *handle)
676 struct dt_object *next = mdd_object_child(pobj);
680 if (dt_try_as_dir(env, next)) {
681 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
682 struct lu_ucred *uc = lu_ucred_check(env);
686 rec->rec_type = type;
687 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
688 rc = dt_insert(env, next, (const struct dt_rec *)rec,
689 (const struct dt_key *)name, handle,
697 /* insert named index, add reference if isdir */
698 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
699 const struct lu_fid *lf, __u32 type,
700 const char *name, struct thandle *handle)
705 rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
706 if (rc == 0 && S_ISDIR(type)) {
707 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
708 mdo_ref_add(env, pobj, handle);
709 mdd_write_unlock(env, pobj);
715 /* delete named index, drop reference if isdir */
716 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
717 const char *name, int is_dir,
718 struct thandle *handle)
723 rc = __mdd_index_delete_only(env, pobj, name, handle);
724 if (rc == 0 && is_dir) {
725 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
726 mdo_ref_del(env, pobj, handle);
727 mdd_write_unlock(env, pobj);
733 static int mdd_llog_record_calc_size(const struct lu_env *env,
734 const struct lu_name *tname,
735 const struct lu_name *sname)
737 const struct lu_ucred *uc = lu_ucred(env);
738 enum changelog_rec_flags crf = 0;
739 size_t hdr_size = sizeof(struct llog_changelog_rec) -
740 sizeof(struct changelog_rec);
745 if (uc != NULL && uc->uc_jobid[0] != '\0')
748 return llog_data_len(hdr_size + changelog_rec_offset(crf) +
749 (tname != NULL ? tname->ln_namelen : 0) +
750 (sname != NULL ? 1 + sname->ln_namelen : 0));
753 int mdd_declare_changelog_store(const struct lu_env *env,
754 struct mdd_device *mdd,
755 const struct lu_name *tname,
756 const struct lu_name *sname,
757 struct thandle *handle)
759 struct obd_device *obd = mdd2obd_dev(mdd);
760 struct llog_ctxt *ctxt;
761 struct llog_changelog_rec *rec;
763 struct thandle *llog_th;
768 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
771 reclen = mdd_llog_record_calc_size(env, tname, sname);
772 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
773 if (buf->lb_buf == NULL)
777 rec->cr_hdr.lrh_len = reclen;
778 rec->cr_hdr.lrh_type = CHANGELOG_REC;
780 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
784 llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
786 GOTO(out_put, rc = PTR_ERR(llog_th));
788 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
796 /** Add a changelog entry \a rec to the changelog llog
799 * \param handle - currently ignored since llogs start their own transaction;
800 * this will hopefully be fixed in llog rewrite
803 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
804 struct llog_changelog_rec *rec, struct thandle *th)
806 struct obd_device *obd = mdd2obd_dev(mdd);
807 struct llog_ctxt *ctxt;
808 struct thandle *llog_th;
811 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
812 changelog_rec_varsize(&rec->cr));
814 /* llog_lvfs_write_rec sets the llog tail len */
815 rec->cr_hdr.lrh_type = CHANGELOG_REC;
816 rec->cr.cr_time = cl_time();
818 spin_lock(&mdd->mdd_cl.mc_lock);
819 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
820 * but as long as the MDD transactions are ordered correctly for e.g.
821 * rename conflicts, I don't think this should matter. */
822 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
823 spin_unlock(&mdd->mdd_cl.mc_lock);
825 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
829 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
831 GOTO(out_put, rc = PTR_ERR(llog_th));
833 /* nested journal transaction */
834 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
843 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
844 const struct lu_fid *sfid,
845 const struct lu_fid *spfid,
846 const struct lu_name *sname)
848 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
849 size_t extsize = sname->ln_namelen + 1;
851 LASSERT(sfid != NULL);
852 LASSERT(spfid != NULL);
853 LASSERT(sname != NULL);
855 rnm->cr_sfid = *sfid;
856 rnm->cr_spfid = *spfid;
858 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
859 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
860 rec->cr_namelen += extsize;
863 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
865 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
867 if (jobid == NULL || jobid[0] == '\0')
870 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
873 /** Store a namespace change changelog record
874 * If this fails, we must fail the whole transaction; we don't
875 * want the change to commit without the log entry.
876 * \param target - mdd_object of change
877 * \param tpfid - target parent dir/object fid
878 * \param sfid - source object fid
879 * \param spfid - source parent fid
880 * \param tname - target name string
881 * \param sname - source name string
882 * \param handle - transaction handle
884 int mdd_changelog_ns_store(const struct lu_env *env,
885 struct mdd_device *mdd,
886 enum changelog_rec_type type,
887 enum changelog_rec_flags crf,
888 struct mdd_object *target,
889 const struct lu_fid *tpfid,
890 const struct lu_fid *sfid,
891 const struct lu_fid *spfid,
892 const struct lu_name *tname,
893 const struct lu_name *sname,
894 struct thandle *handle)
896 const struct lu_ucred *uc = lu_ucred(env);
897 struct llog_changelog_rec *rec;
904 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
907 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
910 LASSERT(tpfid != NULL);
911 LASSERT(tname != NULL);
912 LASSERT(handle != NULL);
914 reclen = mdd_llog_record_calc_size(env, tname, sname);
915 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
916 if (buf->lb_buf == NULL)
922 if (uc != NULL && uc->uc_jobid[0] != '\0')
930 rec->cr.cr_flags = crf;
931 rec->cr.cr_type = (__u32)type;
932 rec->cr.cr_pfid = *tpfid;
933 rec->cr.cr_namelen = tname->ln_namelen;
934 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
936 if (crf & CLF_RENAME)
937 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
940 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
942 if (likely(target != NULL)) {
943 rec->cr.cr_tfid = *mdo2fid(target);
944 target->mod_cltime = cfs_time_current_64();
946 fid_zero(&rec->cr.cr_tfid);
949 rc = mdd_changelog_store(env, mdd, rec, handle);
951 CERROR("%s: cannot store changelog record: type = %d, "
952 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
953 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
954 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
961 static int __mdd_links_add(const struct lu_env *env,
962 struct mdd_object *mdd_obj,
963 struct linkea_data *ldata,
964 const struct lu_name *lname,
965 const struct lu_fid *pfid,
966 int first, int check)
970 if (ldata->ld_leh == NULL) {
971 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
975 rc = linkea_data_new(ldata,
976 &mdd_env_info(env)->mti_link_buf);
983 rc = linkea_links_find(ldata, lname, pfid);
984 if (rc && rc != -ENOENT)
990 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
991 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
995 linkea_add_buf(ldata, lname, tfid);
998 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
999 linkea_add_buf(ldata, lname, pfid);
1001 return linkea_add_buf(ldata, lname, pfid);
1004 static int __mdd_links_del(const struct lu_env *env,
1005 struct mdd_object *mdd_obj,
1006 struct linkea_data *ldata,
1007 const struct lu_name *lname,
1008 const struct lu_fid *pfid)
1012 if (ldata->ld_leh == NULL) {
1013 rc = mdd_links_read(env, mdd_obj, ldata);
1018 rc = linkea_links_find(ldata, lname, pfid);
1022 linkea_del_buf(ldata, lname);
1026 static int mdd_linkea_prepare(const struct lu_env *env,
1027 struct mdd_object *mdd_obj,
1028 const struct lu_fid *oldpfid,
1029 const struct lu_name *oldlname,
1030 const struct lu_fid *newpfid,
1031 const struct lu_name *newlname,
1032 int first, int check,
1033 struct linkea_data *ldata)
1038 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1041 LASSERT(oldpfid != NULL || newpfid != NULL);
1043 if (mdd_obj->mod_flags & DEAD_OBJ)
1044 /* Unnecessary to update linkEA for dead object. */
1047 if (oldpfid != NULL) {
1048 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1050 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1053 /* No changes done. */
1058 /* If renaming, add the new record */
1059 if (newpfid != NULL)
1060 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1066 int mdd_links_rename(const struct lu_env *env,
1067 struct mdd_object *mdd_obj,
1068 const struct lu_fid *oldpfid,
1069 const struct lu_name *oldlname,
1070 const struct lu_fid *newpfid,
1071 const struct lu_name *newlname,
1072 struct thandle *handle,
1073 struct linkea_data *ldata,
1074 int first, int check)
1079 if (ldata == NULL) {
1080 ldata = &mdd_env_info(env)->mti_link_data;
1081 memset(ldata, 0, sizeof(*ldata));
1082 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1083 newpfid, newlname, first, check, ldata);
1088 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1089 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1095 if (newlname == NULL)
1096 CERROR("link_ea add failed %d "DFID"\n",
1097 rc, PFID(mdd_object_fid(mdd_obj)));
1098 else if (oldpfid == NULL)
1099 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1100 newlname->ln_namelen, newlname->ln_name, rc,
1101 PFID(mdd_object_fid(mdd_obj)));
1102 else if (newpfid == NULL)
1103 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1104 oldlname->ln_namelen, oldlname->ln_name, rc,
1105 PFID(mdd_object_fid(mdd_obj)));
1107 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1108 "\n", oldlname->ln_namelen, oldlname->ln_name,
1109 newlname->ln_namelen, newlname->ln_name, rc,
1110 PFID(mdd_object_fid(mdd_obj)));
1113 if (is_vmalloc_addr(ldata->ld_buf))
1114 /* if we vmalloced a large buffer drop it */
1115 lu_buf_free(ldata->ld_buf);
1120 static inline int mdd_links_add(const struct lu_env *env,
1121 struct mdd_object *mdd_obj,
1122 const struct lu_fid *pfid,
1123 const struct lu_name *lname,
1124 struct thandle *handle,
1125 struct linkea_data *ldata, int first)
1127 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1128 pfid, lname, handle, ldata, first, 0);
1131 static inline int mdd_links_del(const struct lu_env *env,
1132 struct mdd_object *mdd_obj,
1133 const struct lu_fid *pfid,
1134 const struct lu_name *lname,
1135 struct thandle *handle)
1137 return mdd_links_rename(env, mdd_obj, pfid, lname,
1138 NULL, NULL, handle, NULL, 0, 0);
1141 /** Read the link EA into a temp buffer.
1142 * Uses the name_buf since it is generally large.
1143 * \retval IS_ERR err
1144 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1146 struct lu_buf *mdd_links_get(const struct lu_env *env,
1147 struct mdd_object *mdd_obj)
1149 struct linkea_data ldata = { NULL };
1152 rc = mdd_links_read(env, mdd_obj, &ldata);
1153 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1156 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1157 struct linkea_data *ldata, struct thandle *handle)
1159 const struct lu_buf *buf;
1162 if (ldata == NULL || ldata->ld_buf == NULL ||
1163 ldata->ld_leh == NULL)
1166 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1170 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1171 ldata->ld_leh->leh_len);
1172 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1173 if (unlikely(rc == -ENOSPC)) {
1174 rc = linkea_overflow_shrink(ldata);
1182 static int mdd_declare_links_add(const struct lu_env *env,
1183 struct mdd_object *mdd_obj,
1184 struct thandle *handle,
1185 struct linkea_data *ldata)
1191 if (ldata != NULL && ldata->ld_leh != NULL) {
1192 ea_len = ldata->ld_leh->leh_len;
1193 linkea = ldata->ld_buf->lb_buf;
1195 ea_len = MAX_LINKEA_SIZE;
1199 rc = mdo_declare_xattr_set(env, mdd_obj,
1200 mdd_buf_get_const(env, linkea, ea_len),
1201 XATTR_NAME_LINK, 0, handle);
1206 static inline int mdd_declare_links_del(const struct lu_env *env,
1207 struct mdd_object *c,
1208 struct thandle *handle)
1212 /* For directory, the linkEA will be removed together
1213 * with the object. */
1214 if (!S_ISDIR(mdd_object_type(c)))
1215 rc = mdd_declare_links_add(env, c, handle, NULL);
1220 static int mdd_declare_link(const struct lu_env *env,
1221 struct mdd_device *mdd,
1222 struct mdd_object *p,
1223 struct mdd_object *c,
1224 const struct lu_name *name,
1225 struct thandle *handle,
1227 struct linkea_data *data)
1229 struct lu_fid tfid = *mdo2fid(c);
1232 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1233 tfid.f_oid = cfs_fail_val;
1235 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1236 name->ln_name, handle);
1240 rc = mdo_declare_ref_add(env, c, handle);
1244 la->la_valid = LA_CTIME | LA_MTIME;
1245 rc = mdo_declare_attr_set(env, p, la, handle);
1249 la->la_valid = LA_CTIME;
1250 rc = mdo_declare_attr_set(env, c, la, handle);
1254 rc = mdd_declare_links_add(env, c, handle, data);
1258 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1263 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1264 struct md_object *src_obj, const struct lu_name *lname,
1267 const char *name = lname->ln_name;
1268 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1269 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1270 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1271 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1272 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1273 struct mdd_device *mdd = mdo2mdd(src_obj);
1274 struct thandle *handle;
1275 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1276 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1280 rc = mdd_la_get(env, mdd_sobj, cattr);
1284 rc = mdd_la_get(env, mdd_tobj, tattr);
1288 handle = mdd_trans_create(env, mdd);
1290 GOTO(out_pending, rc = PTR_ERR(handle));
1292 memset(ldata, 0, sizeof(*ldata));
1294 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1295 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1297 /* Note: even this function will change ldata, but it comes from
1298 * thread_info, which is completely temporary and only seen in
1299 * this function, so we do not need reset ldata once it fails.*/
1300 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
1301 lname, 0, 0, ldata);
1305 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1310 rc = mdd_trans_start(env, mdd, handle);
1314 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1315 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1318 GOTO(out_unlock, rc);
1320 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1321 rc = mdo_ref_add(env, mdd_sobj, handle);
1323 GOTO(out_unlock, rc);
1326 *tfid = *mdo2fid(mdd_sobj);
1327 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1328 tfid->f_oid = cfs_fail_val;
1330 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1331 mdd_object_type(mdd_sobj), name, handle);
1333 mdo_ref_del(env, mdd_sobj, handle);
1334 GOTO(out_unlock, rc);
1337 la->la_valid = LA_CTIME | LA_MTIME;
1338 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1340 GOTO(out_unlock, rc);
1342 la->la_valid = LA_CTIME;
1343 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1345 /* Note: The failure of links_add should not cause the
1346 * link failure, so do not check return value. */
1347 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1348 lname, handle, ldata, 0);
1352 mdd_write_unlock(env, mdd_sobj);
1354 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1355 mdo2fid(mdd_tobj), NULL, NULL,
1356 lname, NULL, handle);
1358 rc = mdd_trans_stop(env, mdd, rc, handle);
1359 if (is_vmalloc_addr(ldata->ld_buf))
1360 /* if we vmalloced a large buffer drop it */
1361 lu_buf_free(ldata->ld_buf);
1366 static int mdd_mark_orphan_object(const struct lu_env *env,
1367 struct mdd_object *obj, struct thandle *handle,
1370 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1373 if (!S_ISDIR(mdd_object_type(obj)))
1376 attr->la_valid = LA_FLAGS;
1377 attr->la_flags = LUSTRE_ORPHAN_FL;
1380 rc = mdo_declare_attr_set(env, obj, attr, handle);
1382 rc = mdo_attr_set(env, obj, attr, handle);
1387 static int mdd_declare_finish_unlink(const struct lu_env *env,
1388 struct mdd_object *obj,
1389 struct thandle *handle)
1393 /* Sigh, we do not know if the unlink object will become orphan in
1394 * declare phase, but fortunately the flags here does not matter
1395 * in current declare implementation */
1396 rc = mdd_mark_orphan_object(env, obj, handle, true);
1400 rc = mdo_declare_destroy(env, obj, handle);
1404 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1408 return mdd_declare_links_del(env, obj, handle);
1411 /* caller should take a lock before calling */
1412 int mdd_finish_unlink(const struct lu_env *env,
1413 struct mdd_object *obj, struct md_attr *ma,
1414 const struct mdd_object *pobj,
1415 const struct lu_name *lname,
1419 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1422 LASSERT(mdd_write_locked(env, obj) != 0);
1424 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1425 /* add new orphan and the object
1426 * will be deleted during mdd_close() */
1427 obj->mod_flags |= DEAD_OBJ;
1428 if (obj->mod_count) {
1429 rc = __mdd_orphan_add(env, obj, th);
1431 CDEBUG(D_HA, "Object "DFID" is inserted into "
1432 "orphan list, open count = %d\n",
1433 PFID(mdd_object_fid(obj)),
1436 CERROR("Object "DFID" fail to be an orphan, "
1437 "open count = %d, maybe cause failed "
1439 PFID(mdd_object_fid(obj)),
1442 /* mark object as an orphan here, not
1443 * before __mdd_orphan_add() as racing
1444 * mdd_la_get() may propagate ORPHAN_OBJ
1445 * causing the asserition */
1446 rc = mdd_mark_orphan_object(env, obj, th, false);
1448 rc = mdo_destroy(env, obj, th);
1450 } else if (!is_dir) {
1451 /* old files may not have link ea; ignore errors */
1452 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1460 * has mdd_write_lock on cobj already, but not on pobj yet
1462 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1463 const struct lu_attr *pattr,
1464 struct mdd_object *cobj,
1465 const struct lu_attr *cattr)
1470 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1475 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1476 struct mdd_object *p, struct mdd_object *c,
1477 const struct lu_name *name, struct md_attr *ma,
1478 struct thandle *handle, int no_name, int is_dir)
1480 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1483 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1484 if (likely(no_name == 0)) {
1485 rc = mdo_declare_index_delete(env, p, name->ln_name,
1492 rc = mdo_declare_ref_del(env, p, handle);
1498 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1499 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1500 la->la_valid = LA_CTIME | LA_MTIME;
1501 rc = mdo_declare_attr_set(env, p, la, handle);
1506 rc = mdo_declare_ref_del(env, c, handle);
1510 rc = mdo_declare_ref_del(env, c, handle);
1514 la->la_valid = LA_CTIME;
1515 rc = mdo_declare_attr_set(env, c, la, handle);
1519 rc = mdd_declare_finish_unlink(env, c, handle);
1523 /* FIXME: need changelog for remove entry */
1524 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1531 * test if a file has an HSM archive
1532 * if HSM attributes are not found in ma update them from
1535 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1536 struct mdd_object *obj,
1541 if (!(ma->ma_valid & MA_HSM)) {
1542 /* no HSM MD provided, read xattr */
1543 struct lu_buf *hsm_buf;
1544 const size_t buflen = sizeof(struct hsm_attrs);
1547 hsm_buf = mdd_buf_get(env, NULL, 0);
1548 lu_buf_alloc(hsm_buf, buflen);
1549 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1550 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1551 lu_buf_free(hsm_buf);
1555 ma->ma_valid |= MA_HSM;
1557 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1563 * Delete name entry and the object.
1564 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1565 * does not exist for this object, and it could only happen during resending
1566 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1567 * is also needed in this case(needed by changelog), so we have to add another
1568 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1569 * the ENOENT failure should be able to be fixed by redo mechanism.
1571 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1572 struct md_object *cobj, const struct lu_name *lname,
1573 struct md_attr *ma, int no_name)
1575 const char *name = lname->ln_name;
1576 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1577 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1578 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1579 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1580 struct mdd_object *mdd_cobj = NULL;
1581 struct mdd_device *mdd = mdo2mdd(pobj);
1582 struct thandle *handle;
1583 int rc, is_dir = 0, cl_flags = 0;
1586 /* cobj == NULL means only delete name entry */
1587 if (likely(cobj != NULL)) {
1588 mdd_cobj = md2mdd_obj(cobj);
1589 if (mdd_object_exists(mdd_cobj) == 0)
1593 rc = mdd_la_get(env, mdd_pobj, pattr);
1597 if (likely(mdd_cobj != NULL)) {
1599 rc = mdd_la_get(env, mdd_cobj, cattr);
1603 is_dir = S_ISDIR(cattr->la_mode);
1604 /* search for an existing archive.
1605 * we should check ahead as the object
1606 * can be destroyed in this transaction */
1607 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1608 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1611 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1615 handle = mdd_trans_create(env, mdd);
1617 RETURN(PTR_ERR(handle));
1619 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1620 lname, ma, handle, no_name, is_dir);
1624 rc = mdd_trans_start(env, mdd, handle);
1628 if (likely(mdd_cobj != NULL))
1629 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1631 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1632 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1637 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1638 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1639 GOTO(cleanup, rc = 0);
1641 if (likely(mdd_cobj != NULL)) {
1642 rc = mdo_ref_del(env, mdd_cobj, handle);
1644 __mdd_index_insert_only(env, mdd_pobj,
1646 mdd_object_type(mdd_cobj),
1653 mdo_ref_del(env, mdd_cobj, handle);
1655 /* fetch updated nlink */
1656 rc = mdd_la_get(env, mdd_cobj, cattr);
1661 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1662 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1664 la->la_valid = LA_CTIME | LA_MTIME;
1665 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1669 /* Enough for only unlink the entry */
1670 if (unlikely(mdd_cobj == NULL))
1673 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1674 /* update ctime of an unlinked file only if it is still
1675 * opened or a link still exists */
1676 la->la_valid = LA_CTIME;
1677 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1682 /* XXX: this transfer to ma will be removed with LOD/OSP */
1683 ma->ma_attr = *cattr;
1684 ma->ma_valid |= MA_INODE;
1685 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1689 /* fetch updated nlink */
1690 rc = mdd_la_get(env, mdd_cobj, cattr);
1691 /* if object is removed then we can't get its attrs,
1693 if (rc == -ENOENT) {
1694 cattr->la_nlink = 0;
1698 if (cattr->la_nlink == 0) {
1699 ma->ma_attr = *cattr;
1700 ma->ma_valid |= MA_INODE;
1705 if (likely(mdd_cobj != NULL))
1706 mdd_write_unlock(env, mdd_cobj);
1709 if (cattr->la_nlink == 0)
1710 cl_flags |= CLF_UNLINK_LAST;
1712 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1714 rc = mdd_changelog_ns_store(env, mdd,
1715 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1716 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1721 rc = mdd_trans_stop(env, mdd, rc, handle);
1727 * The permission has been checked when obj created, no need check again.
1729 static int mdd_cd_sanity_check(const struct lu_env *env,
1730 struct mdd_object *obj)
1735 if (!obj || mdd_is_dead_obj(obj))
1741 static int mdd_create_data(const struct lu_env *env,
1742 struct md_object *pobj,
1743 struct md_object *cobj,
1744 const struct md_op_spec *spec,
1747 struct mdd_device *mdd = mdo2mdd(cobj);
1748 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1749 struct mdd_object *son = md2mdd_obj(cobj);
1750 struct thandle *handle;
1751 const struct lu_buf *buf;
1752 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1753 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1757 rc = mdd_cd_sanity_check(env, son);
1761 if (!md_should_create(spec->sp_cr_flags))
1765 * there are following use cases for this function:
1766 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1767 * striping can be specified or not
1770 rc = mdd_la_get(env, son, attr);
1774 /* calling ->ah_make_hint() is used to transfer information from parent */
1775 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1777 handle = mdd_trans_create(env, mdd);
1779 GOTO(out_free, rc = PTR_ERR(handle));
1782 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1783 * Should this be fixed?
1785 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1786 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1787 spec->sp_cr_flags, spec->no_create);
1789 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1790 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1791 spec->u.sp_ea.eadatalen);
1796 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1797 XATTR_NAME_LOV, 0, handle);
1801 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1805 rc = mdd_trans_start(env, mdd, handle);
1809 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1815 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1818 rc = mdd_trans_stop(env, mdd, rc, handle);
1824 static int mdd_declare_object_initialize(const struct lu_env *env,
1825 struct mdd_object *parent,
1826 struct mdd_object *child,
1827 const struct lu_attr *attr,
1828 struct thandle *handle)
1833 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1834 if (!S_ISDIR(attr->la_mode))
1837 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1842 rc = mdo_declare_ref_add(env, child, handle);
1846 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1852 static int mdd_object_initialize(const struct lu_env *env,
1853 const struct lu_fid *pfid,
1854 struct mdd_object *child,
1855 struct lu_attr *attr, struct thandle *handle,
1856 const struct md_op_spec *spec)
1861 if (S_ISDIR(attr->la_mode)) {
1862 /* Add "." and ".." for newly created dir */
1863 mdo_ref_add(env, child, handle);
1864 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1865 S_IFDIR, dot, handle);
1867 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1870 mdo_ref_del(env, child, handle);
1877 * This function checks whether it can create a file/dir under the
1878 * directory(@pobj). The directory(@pobj) is not being locked by
1881 * \param[in] env execution environment
1882 * \param[in] pobj the directory to create files
1883 * \param[in] pattr the attributes of the directory
1884 * \param[in] lname the name of the created file/dir
1885 * \param[in] cattr the attributes of the file/dir
1886 * \param[in] spec create specification
1888 * \retval = 0 it is allowed to create file/dir under
1890 * \retval negative error not allowed to create file/dir
1891 * under the directory
1893 static int mdd_create_sanity_check(const struct lu_env *env,
1894 struct md_object *pobj,
1895 const struct lu_attr *pattr,
1896 const struct lu_name *lname,
1897 struct lu_attr *cattr,
1898 struct md_op_spec *spec)
1900 struct mdd_thread_info *info = mdd_env_info(env);
1901 struct lu_fid *fid = &info->mti_fid;
1902 struct mdd_object *obj = md2mdd_obj(pobj);
1903 struct mdd_device *m = mdo2mdd(pobj);
1904 bool check_perm = true;
1909 if (mdd_is_dead_obj(obj))
1913 * In some cases this lookup is not needed - we know before if name
1914 * exists or not because MDT performs lookup for it.
1915 * name length check is done in lookup.
1917 if (spec->sp_cr_lookup) {
1919 * Check if the name already exist, though it will be checked in
1920 * _index_insert also, for avoiding rolling back if exists
1923 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1924 MAY_WRITE | MAY_EXEC);
1926 RETURN(rc ? : -EEXIST);
1928 /* Permission is already being checked in mdd_lookup */
1932 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1937 if (pattr->la_mode & S_ISGID) {
1938 cattr->la_gid = pattr->la_gid;
1939 if (S_ISDIR(cattr->la_mode)) {
1940 cattr->la_mode |= S_ISGID;
1941 cattr->la_valid |= LA_MODE;
1945 rc = mdd_name_check(m, lname);
1949 switch (cattr->la_mode & S_IFMT) {
1951 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1953 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
1954 RETURN(-ENAMETOOLONG);
1973 static int mdd_declare_object_create(const struct lu_env *env,
1974 struct mdd_device *mdd,
1975 struct mdd_object *p, struct mdd_object *c,
1976 struct lu_attr *attr,
1977 struct thandle *handle,
1978 const struct md_op_spec *spec,
1979 struct lu_buf *def_acl_buf,
1980 struct lu_buf *acl_buf,
1981 struct dt_allocation_hint *hint)
1983 const struct lu_buf *buf;
1986 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1991 #ifdef CONFIG_FS_POSIX_ACL
1992 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1993 /* if dir, then can inherit default ACl */
1994 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1995 XATTR_NAME_ACL_DEFAULT,
2001 if (acl_buf->lb_len > 0) {
2002 rc = mdo_declare_attr_set(env, c, attr, handle);
2006 rc = mdo_declare_xattr_set(env, c, acl_buf,
2007 XATTR_NAME_ACL_ACCESS, 0, handle);
2012 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2016 /* replay case, create LOV EA from client data */
2017 if (spec->no_create ||
2018 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2019 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2020 spec->u.sp_ea.eadatalen);
2021 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2027 if (S_ISLNK(attr->la_mode)) {
2028 const char *target_name = spec->u.sp_symname;
2029 int sym_len = strlen(target_name);
2030 const struct lu_buf *buf;
2032 buf = mdd_buf_get_const(env, target_name, sym_len);
2033 rc = dt_declare_record_write(env, mdd_object_child(c),
2039 if (spec->sp_cr_file_secctx_name != NULL) {
2040 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2041 spec->sp_cr_file_secctx_size);
2042 rc = mdo_declare_xattr_set(env, c, buf,
2043 spec->sp_cr_file_secctx_name, 0,
2052 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2053 struct mdd_object *p, struct mdd_object *c,
2054 const struct lu_name *name,
2055 struct lu_attr *attr,
2056 struct thandle *handle,
2057 const struct md_op_spec *spec,
2058 struct linkea_data *ldata,
2059 struct lu_buf *def_acl_buf,
2060 struct lu_buf *acl_buf,
2061 struct dt_allocation_hint *hint)
2065 rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2066 def_acl_buf, acl_buf, hint);
2070 if (S_ISDIR(attr->la_mode)) {
2071 rc = mdo_declare_ref_add(env, p, handle);
2076 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2077 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2081 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2083 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2084 name->ln_name, handle);
2088 rc = mdd_declare_links_add(env, c, handle, ldata);
2093 la->la_valid = LA_CTIME | LA_MTIME;
2094 rc = mdo_declare_attr_set(env, p, la, handle);
2098 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2106 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2107 struct lu_attr *la, struct lu_buf *def_acl_buf,
2108 struct lu_buf *acl_buf)
2113 if (S_ISLNK(la->la_mode)) {
2114 acl_buf->lb_len = 0;
2115 def_acl_buf->lb_len = 0;
2119 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2120 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2121 XATTR_NAME_ACL_DEFAULT);
2122 mdd_read_unlock(env, pobj);
2124 /* If there are default ACL, fix mode/ACL by default ACL */
2125 def_acl_buf->lb_len = rc;
2126 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2127 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2128 acl_buf->lb_len = rc;
2129 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2132 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2133 /* If there are no default ACL, fix mode by mask */
2134 struct lu_ucred *uc = lu_ucred(env);
2136 /* The create triggered by MDT internal events, such as
2137 * LFSCK reset, will not contain valid "uc". */
2138 if (unlikely(uc != NULL))
2139 la->la_mode &= ~uc->uc_umask;
2141 acl_buf->lb_len = 0;
2142 def_acl_buf->lb_len = 0;
2149 * Create a metadata object and initialize it, set acl, xattr.
2151 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2152 struct mdd_object *son, struct lu_attr *attr,
2153 struct md_op_spec *spec, struct lu_buf *acl_buf,
2154 struct lu_buf *def_acl_buf,
2155 struct dt_allocation_hint *hint,
2156 struct thandle *handle)
2158 const struct lu_buf *buf;
2161 mdd_write_lock(env, son, MOR_TGT_CHILD);
2162 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2167 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2168 * created in declare phase, they also needs to be added to master
2169 * object as sub-directory entry. So it has to initialize the master
2170 * object, then set dir striped EA.(in mdo_xattr_set) */
2171 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2174 GOTO(err_destroy, rc);
2177 * in case of replay we just set LOVEA provided by the client
2178 * XXX: I think it would be interesting to try "old" way where
2179 * MDT calls this xattr_set(LOV) in a different transaction.
2180 * probably this way we code can be made better.
2183 /* During creation, there are only a few cases we need do xattr_set to
2185 * 1. regular file: see comments above.
2186 * 2. dir: inherit default striping or pool settings from parent.
2187 * 3. create striped directory with provided stripeEA.
2188 * 4. create striped directory because inherit default layout from the
2191 if (spec->no_create ||
2192 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2193 S_ISDIR(attr->la_mode)) {
2194 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2195 spec->u.sp_ea.eadatalen);
2196 rc = mdo_xattr_set(env, son, buf,
2197 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2201 GOTO(err_destroy, rc);
2204 #ifdef CONFIG_FS_POSIX_ACL
2205 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2206 S_ISDIR(attr->la_mode)) {
2207 /* set default acl */
2208 rc = mdo_xattr_set(env, son, def_acl_buf,
2209 XATTR_NAME_ACL_DEFAULT, 0,
2212 GOTO(err_destroy, rc);
2214 /* set its own acl */
2215 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2216 rc = mdo_xattr_set(env, son, acl_buf,
2217 XATTR_NAME_ACL_ACCESS,
2220 GOTO(err_destroy, rc);
2224 if (S_ISLNK(attr->la_mode)) {
2225 struct lu_ucred *uc = lu_ucred_assert(env);
2226 struct dt_object *dt = mdd_object_child(son);
2227 const char *target_name = spec->u.sp_symname;
2228 int sym_len = strlen(target_name);
2229 const struct lu_buf *buf;
2232 buf = mdd_buf_get_const(env, target_name, sym_len);
2233 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2235 CFS_CAP_SYS_RESOURCE_MASK);
2240 GOTO(err_initlized, rc = -EFAULT);
2243 if (spec->sp_cr_file_secctx_name != NULL) {
2244 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2245 spec->sp_cr_file_secctx_size);
2246 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2249 GOTO(err_initlized, rc);
2253 if (unlikely(rc != 0)) {
2255 if (S_ISDIR(attr->la_mode)) {
2256 /* Drop the reference, no need to delete "."/"..",
2257 * because the object to be destroied directly. */
2258 rc2 = mdo_ref_del(env, son, handle);
2262 rc2 = mdo_ref_del(env, son, handle);
2266 mdo_destroy(env, son, handle);
2269 mdd_write_unlock(env, son);
2273 static int mdd_index_delete(const struct lu_env *env,
2274 struct mdd_object *mdd_pobj,
2275 struct lu_attr *cattr,
2276 const struct lu_name *lname)
2278 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2279 struct thandle *handle;
2283 handle = mdd_trans_create(env, mdd);
2285 RETURN(PTR_ERR(handle));
2287 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2292 if (S_ISDIR(cattr->la_mode)) {
2293 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2298 /* Since this will only be used in the error handler path,
2299 * Let's set the thandle to be local and not mess the transno */
2300 handle->th_local = 1;
2301 rc = mdd_trans_start(env, mdd, handle);
2305 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2306 S_ISDIR(cattr->la_mode), handle);
2310 rc = mdd_trans_stop(env, mdd, rc, handle);
2316 * Create object and insert it into namespace.
2318 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2319 const struct lu_name *lname, struct md_object *child,
2320 struct md_op_spec *spec, struct md_attr* ma)
2322 struct mdd_thread_info *info = mdd_env_info(env);
2323 struct lu_attr *la = &info->mti_la_for_fix;
2324 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2325 struct mdd_object *son = md2mdd_obj(child);
2326 struct mdd_device *mdd = mdo2mdd(pobj);
2327 struct lu_attr *attr = &ma->ma_attr;
2328 struct thandle *handle;
2329 struct lu_attr *pattr = &info->mti_pattr;
2330 struct lu_buf acl_buf;
2331 struct lu_buf def_acl_buf;
2332 struct linkea_data *ldata = &info->mti_link_data;
2333 const char *name = lname->ln_name;
2334 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2340 * Two operations have to be performed:
2342 * - an allocation of a new object (->do_create()), and
2344 * - an insertion into a parent index (->dio_insert()).
2346 * Due to locking, operation order is not important, when both are
2347 * successful, *but* error handling cases are quite different:
2349 * - if insertion is done first, and following object creation fails,
2350 * insertion has to be rolled back, but this operation might fail
2351 * also leaving us with dangling index entry.
2353 * - if creation is done first, is has to be undone if insertion
2354 * fails, leaving us with leaked space, which is neither good, nor
2357 * It seems that creation-first is simplest solution, but it is
2358 * sub-optimal in the frequent
2363 * case, because second mkdir is bound to create object, only to
2364 * destroy it immediately.
2366 * To avoid this follow local file systems that do double lookup:
2368 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2370 * 1. create (mdd_object_create_internal())
2372 * 2. insert (__mdd_index_insert(), lookup again)
2375 rc = mdd_la_get(env, mdd_pobj, pattr);
2379 /* Sanity checks before big job. */
2380 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2384 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2385 GOTO(out_free, rc = -EINPROGRESS);
2387 handle = mdd_trans_create(env, mdd);
2389 GOTO(out_free, rc = PTR_ERR(handle));
2391 acl_buf.lb_buf = info->mti_xattr_buf;
2392 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2393 def_acl_buf.lb_buf = info->mti_key;
2394 def_acl_buf.lb_len = sizeof(info->mti_key);
2395 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2399 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2401 memset(ldata, 0, sizeof(*ldata));
2402 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2403 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2406 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2407 &tfid, lname, 1, 0, ldata);
2409 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2410 mdd_object_fid(mdd_pobj),
2411 lname, 1, 0, ldata);
2414 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2415 handle, spec, ldata, &def_acl_buf, &acl_buf,
2420 rc = mdd_trans_start(env, mdd, handle);
2424 rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2425 &def_acl_buf, hint, handle);
2429 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2430 mdd_write_lock(env, son, MOR_TGT_CHILD);
2431 rc = __mdd_orphan_add(env, son, handle);
2432 GOTO(out_volatile, rc);
2434 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2435 attr->la_mode, name, handle);
2437 GOTO(err_created, rc);
2439 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2442 /* update parent directory mtime/ctime */
2444 la->la_valid = LA_CTIME | LA_MTIME;
2445 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2447 GOTO(err_insert, rc);
2453 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2454 rc2 = __mdd_orphan_del(env, son, handle);
2456 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2457 S_ISDIR(attr->la_mode),
2463 mdd_write_lock(env, son, MOR_TGT_CHILD);
2464 if (S_ISDIR(attr->la_mode)) {
2465 /* Drop the reference, no need to delete "."/"..",
2466 * because the object is to be destroyed directly. */
2467 rc2 = mdo_ref_del(env, son, handle);
2469 mdd_write_unlock(env, son);
2474 /* For volatile files drop one link immediately, since there is
2475 * no filename in the namespace, and save any error returned. */
2476 rc2 = mdo_ref_del(env, son, handle);
2478 mdd_write_unlock(env, son);
2479 if (unlikely(rc == 0))
2484 /* Don't destroy the volatile object on success */
2485 if (likely(rc != 0))
2486 mdo_destroy(env, son, handle);
2487 mdd_write_unlock(env, son);
2490 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2491 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2492 rc = mdd_changelog_ns_store(env, mdd,
2493 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2494 S_ISREG(attr->la_mode) ? CL_CREATE :
2495 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2496 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2499 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2501 /* If creation fails, it is most likely due to the remote update
2502 * failure, because local transaction will mostly succeed at
2503 * this stage. There is no easy way to rollback all of previous
2504 * updates, so let's remove the object from namespace, and
2505 * LFSCK should handle the orphan object. */
2506 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2507 mdd_index_delete(env, mdd_pobj, attr, lname);
2511 if (is_vmalloc_addr(ldata->ld_buf))
2512 /* if we vmalloced a large buffer drop it */
2513 lu_buf_free(ldata->ld_buf);
2515 /* The child object shouldn't be cached anymore */
2517 set_bit(LU_OBJECT_HEARD_BANSHEE,
2518 &child->mo_lu.lo_header->loh_flags);
2523 * Get locks on parents in proper order
2524 * RETURN: < 0 - error, rename_order if successful
2532 static int mdd_rename_order(const struct lu_env *env,
2533 struct mdd_device *mdd,
2534 struct mdd_object *src_pobj,
2535 const struct lu_attr *pattr,
2536 struct mdd_object *tgt_pobj)
2538 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2542 if (src_pobj == tgt_pobj)
2543 RETURN(MDD_RN_SAME);
2545 /* compared the parent child relationship of src_p&tgt_p */
2546 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2548 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2551 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2565 /* has not mdd_write{read}_lock on any obj yet. */
2566 static int mdd_rename_sanity_check(const struct lu_env *env,
2567 struct mdd_object *src_pobj,
2568 const struct lu_attr *pattr,
2569 struct mdd_object *tgt_pobj,
2570 const struct lu_attr *tpattr,
2571 struct mdd_object *sobj,
2572 const struct lu_attr *cattr,
2573 struct mdd_object *tobj,
2574 const struct lu_attr *tattr)
2579 /* XXX: when get here, sobj must NOT be NULL,
2580 * the other case has been processed in cld_rename
2581 * before mdd_rename and enable MDS_PERM_BYPASS. */
2584 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2588 /* XXX: when get here, "tobj == NULL" means tobj must
2589 * NOT exist (neither on remote MDS, such case has been
2590 * processed in cld_rename before mdd_rename and enable
2592 * So check may_create, but not check may_unlink. */
2594 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2595 (src_pobj != tgt_pobj));
2597 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2598 (src_pobj != tgt_pobj), 1);
2600 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2601 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2606 static int mdd_declare_rename(const struct lu_env *env,
2607 struct mdd_device *mdd,
2608 struct mdd_object *mdd_spobj,
2609 struct mdd_object *mdd_tpobj,
2610 struct mdd_object *mdd_sobj,
2611 struct mdd_object *mdd_tobj,
2612 const struct lu_name *sname,
2613 const struct lu_name *tname,
2615 struct linkea_data *ldata,
2616 struct thandle *handle)
2618 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2621 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2622 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2628 /* name from source dir */
2629 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2633 /* .. from source child */
2634 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2635 /* source child can be directory,
2636 * counted by source dir's nlink */
2637 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2640 if (mdd_spobj != mdd_tpobj) {
2641 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2646 rc = mdo_declare_index_insert(env, mdd_sobj,
2648 S_IFDIR, dotdot, handle);
2653 /* new target child can be directory,
2654 * counted by target dir's nlink */
2655 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2660 la->la_valid = LA_CTIME | LA_MTIME;
2661 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2665 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2669 la->la_valid = LA_CTIME;
2670 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2674 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2679 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2680 mdd_object_type(mdd_sobj),
2681 tname->ln_name, handle);
2685 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2686 /* delete target child in target parent directory */
2687 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2692 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2696 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2697 /* target child can be directory,
2698 * delete "." reference in target child directory */
2699 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2703 /* delete ".." reference in target parent directory */
2704 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2709 la->la_valid = LA_CTIME;
2710 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2714 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2719 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2726 /* src object can be remote that is why we use only fid and type of object */
2727 static int mdd_rename(const struct lu_env *env,
2728 struct md_object *src_pobj, struct md_object *tgt_pobj,
2729 const struct lu_fid *lf, const struct lu_name *lsname,
2730 struct md_object *tobj, const struct lu_name *ltname,
2733 const char *sname = lsname->ln_name;
2734 const char *tname = ltname->ln_name;
2735 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2736 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2737 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2738 struct mdd_device *mdd = mdo2mdd(src_pobj);
2739 struct mdd_object *mdd_sobj = NULL; /* source object */
2740 struct mdd_object *mdd_tobj = NULL;
2741 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2742 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2743 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2744 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2745 struct thandle *handle;
2746 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2747 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2748 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2751 bool tobj_locked = 0;
2752 unsigned cl_flags = 0;
2757 mdd_tobj = md2mdd_obj(tobj);
2759 mdd_sobj = mdd_object_find(env, mdd, lf);
2760 if (IS_ERR(mdd_sobj))
2761 RETURN(PTR_ERR(mdd_sobj));
2763 rc = mdd_la_get(env, mdd_sobj, cattr);
2765 GOTO(out_pending, rc);
2767 rc = mdd_la_get(env, mdd_spobj, pattr);
2769 GOTO(out_pending, rc);
2772 rc = mdd_la_get(env, mdd_tobj, tattr);
2774 GOTO(out_pending, rc);
2775 /* search for an existing archive.
2776 * we should check ahead as the object
2777 * can be destroyed in this transaction */
2778 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2779 cl_flags |= CLF_RENAME_LAST_EXISTS;
2782 rc = mdd_la_get(env, mdd_tpobj, tpattr);
2784 GOTO(out_pending, rc);
2786 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2787 mdd_sobj, cattr, mdd_tobj, tattr);
2789 GOTO(out_pending, rc);
2791 rc = mdd_name_check(mdd, ltname);
2793 GOTO(out_pending, rc);
2795 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2796 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2798 GOTO(out_pending, rc);
2800 handle = mdd_trans_create(env, mdd);
2802 GOTO(out_pending, rc = PTR_ERR(handle));
2804 memset(ldata, 0, sizeof(*ldata));
2805 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
2806 lsname, mdd_object_fid(mdd_tpobj), ltname,
2811 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2812 mdd_tobj, lsname, ltname, ma, ldata, handle);
2816 rc = mdd_trans_start(env, mdd, handle);
2820 is_dir = S_ISDIR(cattr->la_mode);
2822 /* Remove source name from source directory */
2823 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2827 /* "mv dir1 dir2" needs "dir1/.." link update */
2828 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2829 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2831 GOTO(fixup_spobj2, rc);
2833 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2836 GOTO(fixup_spobj, rc);
2839 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
2840 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2842 /* tname might been renamed to something else */
2843 GOTO(fixup_spobj, rc);
2846 /* Insert new fid with target name into target dir */
2847 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2850 GOTO(fixup_tpobj, rc);
2852 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2853 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2855 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2856 la->la_valid = LA_CTIME;
2857 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2859 GOTO(fixup_tpobj, rc);
2861 /* Update the linkEA for the source object */
2862 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2863 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2864 mdo2fid(mdd_tpobj), ltname, handle, ldata,
2867 /* Old files might not have EA entry */
2868 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2869 lsname, handle, NULL, 0);
2870 mdd_write_unlock(env, mdd_sobj);
2871 /* We don't fail the transaction if the link ea can't be
2872 updated -- fid2path will use alternate lookup method. */
2875 /* Remove old target object
2876 * For tobj is remote case cmm layer has processed
2877 * and set tobj to NULL then. So when tobj is NOT NULL,
2878 * it must be local one.
2880 if (tobj && mdd_object_exists(mdd_tobj)) {
2881 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2883 if (mdd_is_dead_obj(mdd_tobj)) {
2884 /* shld not be dead, something is wrong */
2885 CERROR("tobj is dead, something is wrong\n");
2889 mdo_ref_del(env, mdd_tobj, handle);
2891 /* Remove dot reference. */
2892 if (S_ISDIR(tattr->la_mode))
2893 mdo_ref_del(env, mdd_tobj, handle);
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);
2906 la->la_valid = LA_CTIME;
2907 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2909 CERROR("%s: Failed to set ctime for tobj "
2911 mdd2obd_dev(mdd)->obd_name,
2912 PFID(tpobj_fid), rc);
2913 GOTO(fixup_tpobj, rc);
2916 /* XXX: this transfer to ma will be removed with LOD/OSP */
2917 ma->ma_attr = *tattr;
2918 ma->ma_valid |= MA_INODE;
2919 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2922 CERROR("%s: Failed to unlink tobj "
2924 mdd2obd_dev(mdd)->obd_name,
2925 PFID(tpobj_fid), rc);
2926 GOTO(fixup_tpobj, rc);
2929 /* fetch updated nlink */
2930 rc = mdd_la_get(env, mdd_tobj, tattr);
2931 if (rc == -ENOENT) {
2932 /* the object got removed, let's
2933 * return the latest known attributes */
2934 tattr->la_nlink = 0;
2936 } else if (rc != 0) {
2937 CERROR("%s: Failed to get nlink for tobj "
2939 mdd2obd_dev(mdd)->obd_name,
2940 PFID(tpobj_fid), rc);
2941 GOTO(fixup_tpobj, rc);
2943 /* XXX: this transfer to ma will be removed with LOD/OSP */
2944 ma->ma_attr = *tattr;
2945 ma->ma_valid |= MA_INODE;
2947 if (tattr->la_nlink == 0)
2948 cl_flags |= CLF_RENAME_LAST;
2950 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
2953 la->la_valid = LA_CTIME | LA_MTIME;
2954 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2956 GOTO(fixup_tpobj, rc);
2958 if (mdd_spobj != mdd_tpobj) {
2959 la->la_valid = LA_CTIME | LA_MTIME;
2960 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2962 GOTO(fixup_tpobj, rc);
2969 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2971 CWARN("tp obj fix error %d\n",rc2);
2973 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2974 !mdd_is_dead_obj(mdd_tobj)) {
2976 mdo_ref_add(env, mdd_tobj, handle);
2978 mdo_ref_add(env, mdd_tobj, handle);
2981 rc2 = __mdd_index_insert(env, mdd_tpobj,
2983 mdd_object_type(mdd_tobj),
2986 CWARN("tp obj fix error: rc = %d\n", rc2);
2991 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2992 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2994 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2995 mdd2obd_dev(mdd)->obd_name, rc2);
2998 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3001 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3002 mdd2obd_dev(mdd)->obd_name, rc2);
3007 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3008 mdd_object_type(mdd_sobj), sname,
3011 CWARN("sp obj fix error: rc = %d\n", rc2);
3016 mdd_write_unlock(env, mdd_tobj);
3019 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3020 mdd_tobj, tpobj_fid, lf, spobj_fid,
3021 ltname, lsname, handle);
3024 rc = mdd_trans_stop(env, mdd, rc, handle);
3027 mdd_object_put(env, mdd_sobj);
3032 * During migration once the parent FID has been changed,
3033 * we need update the parent FID in linkea.
3035 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3036 struct mdd_object *parent,
3037 struct mdd_object *newparent,
3038 struct mdd_object *child,
3039 const char *name, int namelen,
3040 struct thandle *handle,
3043 struct mdd_thread_info *info = mdd_env_info(env);
3044 struct linkea_data ldata = { NULL };
3045 struct lu_buf *buf = &info->mti_link_buf;
3051 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3052 if (buf->lb_buf == NULL)
3056 rc = mdd_links_read(env, child, &ldata);
3058 if (rc == -ENOENT || rc == -ENODATA)
3063 LASSERT(ldata.ld_leh != NULL);
3064 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3065 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3066 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3067 struct lu_name lname;
3070 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3073 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3074 !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3075 ldata.ld_lee = (struct link_ea_entry *)
3076 ((char *)ldata.ld_lee +
3081 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3082 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3083 lname.ln_namelen, lname.ln_name,
3084 PFID(mdd_object_fid(newparent)));
3085 /* update to the new parent fid */
3086 linkea_entry_pack(ldata.ld_lee, &lname,
3087 mdd_object_fid(newparent));
3089 rc = mdd_declare_links_add(env, child, handle, &ldata);
3091 rc = mdd_links_write(env, child, &ldata, handle);
3097 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3098 struct mdd_object *parent,
3099 struct mdd_object *newparent,
3100 struct mdd_object *child,
3101 const char *name, int namelen,
3102 struct thandle *handle)
3104 return mdd_linkea_update_child_internal(env, parent, newparent,
3106 namelen, handle, true);
3109 static int mdd_linkea_update_child(const struct lu_env *env,
3110 struct mdd_object *parent,
3111 struct mdd_object *newparent,
3112 struct mdd_object *child,
3113 const char *name, int namelen,
3114 struct thandle *handle)
3116 return mdd_linkea_update_child_internal(env, parent, newparent,
3118 namelen, handle, false);
3121 static int mdd_update_linkea_internal(const struct lu_env *env,
3122 struct mdd_object *mdd_pobj,
3123 struct mdd_object *mdd_sobj,
3124 struct mdd_object *mdd_tobj,
3125 const struct lu_name *child_name,
3126 struct linkea_data *ldata,
3127 struct thandle *handle,
3130 struct mdd_thread_info *info = mdd_env_info(env);
3135 LASSERT(ldata->ld_buf != NULL);
3136 LASSERT(ldata->ld_leh != NULL);
3138 /* If it is mulitple links file, we need update the name entry for
3140 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3141 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3142 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3143 struct mdd_object *pobj;
3144 struct lu_name lname;
3147 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3149 pobj = mdd_object_find(env, mdd, &fid);
3151 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3152 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3157 if (!mdd_object_exists(pobj)) {
3158 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3159 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3163 if (pobj == mdd_pobj &&
3164 lname.ln_namelen == child_name->ln_namelen &&
3165 strncmp(lname.ln_name, child_name->ln_name,
3166 lname.ln_namelen) == 0) {
3167 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3168 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3173 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3174 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3175 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3178 /* Remove source name from source directory */
3179 /* Insert new fid with target name into target dir */
3180 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3185 rc = mdo_declare_index_insert(env, pobj,
3186 mdd_object_fid(mdd_tobj),
3187 mdd_object_type(mdd_tobj),
3188 lname.ln_name, handle);
3192 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3196 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3200 char *tmp_name = info->mti_key;
3202 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3203 /* lnamelen is too big(> NAME_MAX + 16),
3204 * something wrong about this linkea, let's
3206 CWARN("%s: the name %.*s is too long under "
3207 DFID"\n", mdd2obd_dev(mdd)->obd_name,
3208 lname.ln_namelen, lname.ln_name,
3213 /* Note: lname might be without \0 at the end, see
3214 * linkea_entry_unpack(), let's add extra \0 by
3216 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3217 lname.ln_namelen, lname.ln_name);
3218 lname.ln_name = tmp_name;
3220 /* Let's check if this linkEA still valid, before
3221 * it might be packed into the RPC buffer. */
3222 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3223 &info->mti_fid, NULL);
3224 if (rc < 0 || !lu_fid_eq(&info->mti_fid,
3225 mdd_object_fid(mdd_sobj)))
3226 GOTO(next_put, rc == -ENOENT ? 0 : rc);
3228 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3232 rc = __mdd_index_insert(env, pobj,
3233 mdd_object_fid(mdd_tobj),
3234 mdd_object_type(mdd_tobj),
3239 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3240 rc = mdo_ref_add(env, mdd_tobj, handle);
3241 mdd_write_unlock(env, mdd_tobj);
3245 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3246 mdo_ref_del(env, mdd_sobj, handle);
3247 mdd_write_unlock(env, mdd_sobj);
3250 mdd_object_put(env, pobj);
3254 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3261 static int mdd_migrate_xattrs(const struct lu_env *env,
3262 struct mdd_object *mdd_sobj,
3263 struct mdd_object *mdd_tobj)
3265 struct mdd_thread_info *info = mdd_env_info(env);
3266 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3268 struct thandle *handle;
3274 struct lu_buf list_xbuf;
3277 /* retrieve xattr list from the old object */
3278 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3279 if (list_xsize == -ENODATA)
3285 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3286 if (info->mti_big_buf.lb_buf == NULL)
3289 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3290 list_xbuf.lb_len = list_xsize;
3291 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3296 xname = list_xbuf.lb_buf;
3298 xlen = strnlen(xname, rem - 1) + 1;
3299 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3300 strcmp(XATTR_NAME_LMV, xname) == 0)
3303 /* For directory, if there are default layout, migrate here */
3304 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3305 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3308 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3309 if (xsize == -ENODATA)
3314 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3315 if (info->mti_link_buf.lb_buf == NULL)
3316 GOTO(out, rc = -ENOMEM);
3318 xbuf.lb_len = xsize;
3319 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3320 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3326 handle = mdd_trans_create(env, mdd);
3328 GOTO(out, rc = PTR_ERR(handle));
3330 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3333 GOTO(stop_trans, rc);
3334 /* Note: this transaction is part of migration, and it is not
3335 * the last step of migration, so we set th_local = 1 to avoid
3336 * update last rcvd for this transaction */
3337 handle->th_local = 1;
3338 rc = mdd_trans_start(env, mdd, handle);
3340 GOTO(stop_trans, rc);
3343 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3345 GOTO(stop_trans, rc = 0);
3347 if (unlikely(rc == -ENOSPC &&
3348 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3349 rc = linkea_overflow_shrink(
3350 (struct linkea_data *)(xbuf.lb_buf));
3351 if (likely(rc > 0)) {
3358 GOTO(stop_trans, rc);
3360 rc = mdd_trans_stop(env, mdd, rc, handle);
3365 memmove(xname, xname + xlen, rem);
3371 static int mdd_declare_migrate_create(const struct lu_env *env,
3372 struct mdd_object *mdd_pobj,
3373 struct mdd_object *mdd_sobj,
3374 struct mdd_object *mdd_tobj,
3375 struct md_op_spec *spec,
3377 union lmv_mds_md *mgr_ea,
3378 struct linkea_data *ldata,
3379 struct thandle *handle)
3381 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3382 const struct lu_buf *buf;
3386 rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3387 handle, spec, NULL);
3391 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3396 if (S_ISLNK(la->la_mode)) {
3397 const char *target_name = spec->u.sp_symname;
3398 int sym_len = strlen(target_name);
3399 const struct lu_buf *buf;
3401 buf = mdd_buf_get_const(env, target_name, sym_len);
3402 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3406 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3407 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3412 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3413 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3414 spec->u.sp_ea.eadatalen);
3415 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3421 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3422 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3423 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3428 la_flag->la_valid = LA_FLAGS;
3429 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3430 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3435 static int mdd_migrate_create(const struct lu_env *env,
3436 struct mdd_object *mdd_pobj,
3437 struct mdd_object *mdd_sobj,
3438 struct mdd_object *mdd_tobj,
3439 const struct lu_name *lname,
3442 struct mdd_thread_info *info = mdd_env_info(env);
3443 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3444 struct md_op_spec *spec = &info->mti_spec;
3445 struct lu_buf lmm_buf = { NULL };
3446 struct lu_buf link_buf = { NULL };
3447 const struct lu_buf *buf;
3448 struct thandle *handle;
3449 struct lmv_mds_md_v1 *mgr_ea;
3450 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3451 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3453 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3457 /* prepare spec for create */
3458 memset(spec, 0, sizeof(*spec));
3459 spec->sp_cr_lookup = 0;
3460 spec->sp_feat = &dt_directory_features;
3461 if (S_ISLNK(la->la_mode)) {
3462 buf = lu_buf_check_and_alloc(
3463 &mdd_env_info(env)->mti_big_buf,
3466 link_buf.lb_len = la->la_size + 1;
3467 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3468 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3470 rc = rc != 0 ? rc : -EFAULT;
3471 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3472 mdd2obd_dev(mdd)->obd_name,
3473 PFID(mdd_object_fid(mdd_sobj)), rc);
3476 spec->u.sp_symname = link_buf.lb_buf;
3477 } else if (S_ISREG(la->la_mode)) {
3478 /* retrieve lov of the old object */
3479 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3480 if (rc != 0 && rc != -ENODATA)
3482 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3483 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3484 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3485 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3487 } else if (S_ISDIR(la->la_mode)) {
3488 rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
3489 if (rc == -ENODATA) {
3490 /* ignore the non-linkEA error */
3498 mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3499 memset(mgr_ea, 0, sizeof(*mgr_ea));
3500 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3501 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3502 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3503 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3504 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3505 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3507 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3509 handle = mdd_trans_create(env, mdd);
3511 GOTO(out_free, rc = PTR_ERR(handle));
3513 /* Note: this transaction is part of migration, and it is not
3514 * the last step of migration, so we set th_local = 1 to avoid
3515 * update last rcvd for this transaction */
3516 handle->th_local = 1;
3517 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3519 (union lmv_mds_md *)info->mti_xattr_buf,
3522 GOTO(stop_trans, rc);
3524 rc = mdd_trans_start(env, mdd, handle);
3526 GOTO(stop_trans, rc);
3528 /* don't set nlink from the original object */
3529 la->la_valid &= ~LA_NLINK;
3531 /* create the target object */
3532 rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3535 GOTO(stop_trans, rc);
3537 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3538 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3540 GOTO(stop_trans, rc);
3543 /* Set MIGRATE EA on the source inode, so once the migration needs
3544 * to be re-done during failover, the re-do process can locate the
3545 * target object which is already being created. */
3546 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3547 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3548 rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3550 GOTO(stop_trans, rc);
3552 /* Set immutable flag, so any modification is disabled until
3553 * the migration is done. Once the migration is interrupted,
3554 * if the resume process find the migrating object has both
3555 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3556 * flag and approve the migration */
3557 la_flag->la_valid = LA_FLAGS;
3558 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3559 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3562 rc = mdd_trans_stop(env, mdd, rc, handle);
3564 if (lmm_buf.lb_buf != NULL)
3565 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3569 static int mdd_migrate_entries(const struct lu_env *env,
3570 struct mdd_object *mdd_sobj,
3571 struct mdd_object *mdd_tobj)
3573 struct dt_object *next = mdd_object_child(mdd_sobj);
3574 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3575 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3576 struct thandle *handle;
3578 const struct dt_it_ops *iops;
3580 struct lu_dirent *ent;
3584 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3588 if (!dt_try_as_dir(env, next))
3589 GOTO(out_ent, rc = -ENOTDIR);
3591 * iterate directories
3593 iops = &next->do_index_ops->dio_it;
3594 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3596 GOTO(out_ent, rc = PTR_ERR(it));
3598 rc = iops->load(env, it, 0);
3600 rc = iops->next(env, it);
3604 * At this point and across for-loop:
3606 * rc == 0 -> ok, proceed.
3607 * rc > 0 -> end of directory.
3611 struct mdd_object *child;
3612 char *name = mdd_env_info(env)->mti_key;
3615 bool target_exist = false;
3617 len = iops->key_size(env, it);
3621 result = iops->rec(env, it, (struct dt_rec *)ent,
3622 LUDA_FID | LUDA_TYPE);
3623 if (result == -ESTALE)
3630 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3632 /* Insert new fid with target name into target dir */
3633 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3634 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3635 ent->lde_name[1] == '.'))
3638 child = mdd_object_find(env, mdd, &ent->lde_fid);
3640 GOTO(out, rc = PTR_ERR(child));
3642 mdd_write_lock(env, child, MOR_SRC_CHILD);
3643 is_dir = S_ISDIR(mdd_object_type(child));
3645 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3647 /* Check whether the name has been inserted to the target */
3648 if (dt_try_as_dir(env, dt_tobj)) {
3649 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3651 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3652 (struct dt_key *)name);
3653 if (unlikely(rc == 0))
3654 target_exist = true;
3657 handle = mdd_trans_create(env, mdd);
3659 GOTO(out_put, rc = PTR_ERR(handle));
3661 /* Note: this transaction is part of migration, and it is not
3662 * the last step of migration, so we set th_local = 1 to avoid
3663 * updating last rcvd for this transaction */
3664 handle->th_local = 1;
3665 if (likely(!target_exist)) {
3666 rc = mdo_declare_index_insert(env, mdd_tobj,
3668 mdd_object_type(child),
3674 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3680 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3685 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3689 /* Update .. for child */
3690 rc = mdo_declare_index_delete(env, child, dotdot,
3695 rc = mdo_declare_index_insert(env, child,
3696 mdd_object_fid(mdd_tobj),
3697 S_IFDIR, dotdot, handle);
3702 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3709 rc = mdd_trans_start(env, mdd, handle);
3711 CERROR("%s: transaction start failed: rc = %d\n",
3712 mdd2obd_dev(mdd)->obd_name, rc);
3716 if (likely(!target_exist)) {
3717 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3718 mdd_object_type(child),
3724 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3729 rc = __mdd_index_delete_only(env, child, dotdot,
3734 rc = __mdd_index_insert_only(env, child,
3735 mdd_object_fid(mdd_tobj), S_IFDIR,
3741 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3743 strlen(name), handle);
3746 mdd_write_unlock(env, child);
3747 mdd_object_put(env, child);
3748 rc = mdd_trans_stop(env, mdd, rc, handle);
3752 result = iops->next(env, it);
3753 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3754 GOTO(out, rc = -EINTR);
3756 if (result == -ESTALE)
3758 } while (result == 0);
3761 iops->fini(env, it);
3763 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3767 static int mdd_declare_update_linkea(const struct lu_env *env,
3768 struct mdd_object *mdd_pobj,
3769 struct mdd_object *mdd_sobj,
3770 struct mdd_object *mdd_tobj,
3771 const struct lu_name *child_name,
3772 struct linkea_data *ldata,
3773 struct thandle *handle)
3775 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3776 child_name, ldata, handle, 1);
3779 static int mdd_update_linkea(const struct lu_env *env,
3780 struct mdd_object *mdd_pobj,
3781 struct mdd_object *mdd_sobj,
3782 struct mdd_object *mdd_tobj,
3783 const struct lu_name *child_name,
3784 struct linkea_data *ldata,
3785 struct thandle *handle)
3787 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3788 child_name, ldata, handle, 0);
3791 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3792 struct mdd_object *mdd_pobj,
3793 struct mdd_object *mdd_sobj,
3794 struct mdd_object *mdd_tobj,
3795 const struct lu_name *lname,
3797 struct lu_attr *parent_la,
3798 struct linkea_data *ldata,
3799 struct thandle *handle)
3801 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3802 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3805 /* Revert IMMUTABLE flag */
3806 la_flag->la_valid = LA_FLAGS;
3807 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3808 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3812 /* delete entry from source dir */
3813 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3817 if (ldata->ld_buf != NULL) {
3818 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3819 mdd_tobj, lname, ldata, handle);
3824 if (S_ISREG(mdd_object_type(mdd_sobj))) {
3825 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3830 handle->th_complex = 1;
3831 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
3833 LU_XATTR_REPLACE, handle);
3838 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3839 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3845 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3846 mdd_object_type(mdd_tobj),
3847 lname->ln_name, handle);
3851 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
3855 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3856 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3861 /* delete old object */
3862 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3866 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3867 /* delete old object */
3868 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3871 /* set nlink to 0 */
3872 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3877 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3881 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3885 rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
3890 static int mdd_migrate_update_name(const struct lu_env *env,
3891 struct mdd_object *mdd_pobj,
3892 struct mdd_object *mdd_sobj,
3893 struct mdd_object *mdd_tobj,
3894 const struct lu_name *lname,
3897 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
3898 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
3899 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
3900 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3901 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3902 struct thandle *handle;
3903 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3904 const char *name = lname->ln_name;
3908 /* update time for parent */
3909 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3910 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3911 p_la->la_valid = LA_CTIME;
3913 rc = mdd_la_get(env, mdd_sobj, so_attr);
3917 ldata->ld_buf = NULL;
3918 rc = mdd_links_read(env, mdd_sobj, ldata);
3919 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
3922 handle = mdd_trans_create(env, mdd);
3924 RETURN(PTR_ERR(handle));
3926 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3927 lname, so_attr, p_la, ldata,
3930 /* If the migration can not be fit in one transaction, just
3931 * leave it in the original MDT */
3933 GOTO(stop_trans, rc = 0);
3935 GOTO(stop_trans, rc);
3938 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3939 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3940 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3941 PFID(mdd_object_fid(mdd_tobj)));
3943 rc = mdd_trans_start(env, mdd, handle);
3945 GOTO(stop_trans, rc);
3947 /* Revert IMMUTABLE flag */
3948 la_flag->la_valid = LA_FLAGS;
3949 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3950 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3952 GOTO(stop_trans, rc);
3954 /* Remove source name from source directory */
3955 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3957 GOTO(stop_trans, rc);
3959 if (ldata->ld_buf != NULL) {
3960 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3961 lname, ldata, handle);
3963 GOTO(stop_trans, rc);
3965 /* linkea update might decrease the source object
3966 * nlink, let's get the attr again after ref_del */
3967 rc = mdd_la_get(env, mdd_sobj, so_attr);
3969 GOTO(stop_trans, rc);
3972 if (S_ISREG(so_attr->la_mode)) {
3973 if (so_attr->la_nlink == 1) {
3974 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3976 if (rc != 0 && rc != -ENODATA)
3977 GOTO(stop_trans, rc);
3979 rc = mdo_xattr_set(env, mdd_tobj, NULL,
3981 LU_XATTR_REPLACE, handle);
3983 GOTO(stop_trans, rc);
3987 /* Insert new fid with target name into target dir */
3988 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3989 mdd_object_type(mdd_tobj), name, handle);
3991 GOTO(stop_trans, rc);
3993 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3995 mdd_sobj->mod_flags |= DEAD_OBJ;
3996 rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
3998 GOTO(out_unlock, rc);
4000 rc = __mdd_orphan_add(env, mdd_sobj, handle);
4002 GOTO(out_unlock, rc);
4004 mdo_ref_del(env, mdd_sobj, handle);
4006 mdo_ref_del(env, mdd_sobj, handle);
4008 /* Get the attr again after ref_del */
4009 rc = mdd_la_get(env, mdd_sobj, so_attr);
4011 GOTO(out_unlock, rc);
4013 ma->ma_attr = *so_attr;
4014 ma->ma_valid |= MA_INODE;
4016 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4018 GOTO(out_unlock, rc);
4020 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4021 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4022 mdo2fid(mdd_pobj), lname, lname, handle);
4024 CWARN("%s: changelog for migrate %s "DFID
4025 "under "DFID" failed: rc = %d\n",
4026 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4027 PFID(mdd_object_fid(mdd_sobj)),
4028 PFID(mdd_object_fid(mdd_pobj)), rc);
4029 /* Sigh, there are no easy way to migrate back the object, so
4030 * let's reset the result to 0 for now XXX */
4034 mdd_write_unlock(env, mdd_sobj);
4037 rc = mdd_trans_stop(env, mdd, rc, handle);
4042 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4043 const struct lu_fid *fid, __u32 *mdt_index)
4045 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4046 struct seq_server_site *ss;
4049 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4051 range->lsr_flags = LU_SEQ_RANGE_MDT;
4052 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4056 *mdt_index = range->lsr_index;
4061 * Check whether we should migrate the file/dir
4063 * < 0 permission check failed or other error.
4064 * = 0 the file can be migrated.
4065 * > 0 the file does not need to be migrated, mostly
4066 * for multiple link file
4068 static int mdd_migrate_sanity_check(const struct lu_env *env,
4069 struct mdd_object *pobj,
4070 const struct lu_attr *pattr,
4071 struct mdd_object *sobj,
4072 struct lu_attr *sattr)
4074 struct mdd_thread_info *info = mdd_env_info(env);
4075 struct linkea_data *ldata = &info->mti_link_data;
4076 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4078 struct lu_buf *mgr_buf;
4084 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4085 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4086 if (mgr_buf->lb_buf == NULL)
4089 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4091 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4093 /* If the object has migrateEA, it means IMMUTE flag
4094 * is being set by previous migration process, so it
4095 * needs to override the IMMUTE flag, otherwise the
4096 * following sanity check will fail */
4097 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4098 LMV_HASH_FLAG_MIGRATION) {
4099 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4101 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4102 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4103 mdd2obd_dev(mdd)->obd_name,
4104 PFID(mdd_object_fid(sobj)));
4108 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4109 sobj, sattr, NULL, NULL);
4113 /* Then it will check if the file should be migrated. If the file
4114 * has mulitple links, we only need migrate the file if all of its
4115 * entries has been migrated to the remote MDT */
4116 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4119 rc = mdd_links_read(env, sobj, ldata);
4121 /* For multiple links files, if there are no linkEA data at all,
4122 * means the file might be created before linkEA is enabled, and
4123 * all of its links should not be migrated yet, otherwise it
4124 * should have some linkEA there */
4125 if (rc == -ENOENT || rc == -ENODATA)
4130 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4131 /* If there are still links locally, then the file will not be
4133 LASSERT(ldata->ld_leh != NULL);
4135 /* If the linkEA is overflow, then means there are some unknown name
4136 * entries under unknown parents, that will prevent the migration. */
4137 if (unlikely(ldata->ld_leh->leh_overflow_time))
4140 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4141 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4142 struct lu_name lname;
4144 __u32 parent_mdt_index;
4146 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4148 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4151 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4155 /* Migrate the object only if none of its parents are on the
4157 if (parent_mdt_index != mdt_index)
4160 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4161 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4162 lname.ln_name, PFID(&fid));
4170 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4171 struct md_object *sobj, const struct lu_name *lname,
4172 struct md_object *tobj, struct md_attr *ma)
4174 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4175 struct mdd_device *mdd = mdo2mdd(pobj);
4176 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4177 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4178 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4179 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4180 bool created = false;
4184 /* If the file will being migrated, it will check whether
4185 * the file is being opened by someone else right now */
4186 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4187 if (mdd_sobj->mod_count > 0) {
4189 "%s: "DFID"%s is already opened count %d: rc = %d\n",
4190 mdd2obd_dev(mdd)->obd_name,
4191 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4192 mdd_sobj->mod_count, -EBUSY);
4193 mdd_read_unlock(env, mdd_sobj);
4194 GOTO(put, rc = -EBUSY);
4196 mdd_read_unlock(env, mdd_sobj);
4198 rc = mdd_la_get(env, mdd_sobj, so_attr);
4202 rc = mdd_la_get(env, mdd_pobj, pattr);
4206 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4213 /* Sigh, it is impossible to finish all of migration in a single
4214 * transaction, for example migrating big directory entries to the
4215 * new MDT, it needs insert all of name entries of children in the
4218 * So migration will be done in multiple steps and transactions.
4220 * 1. create an orphan object on the remote MDT in one transaction.
4221 * 2. migrate extend attributes to the new target file/directory.
4222 * 3. For directory, migrate the entries to the new MDT and update
4223 * linkEA of each children. Because we can not migrate all entries
4224 * in a single transaction, so the migrating directory will become
4225 * a striped directory during migration, so once the process is
4226 * interrupted, the directory is still accessible. (During lookup,
4227 * client will locate the name by searching both original and target
4229 * 4. Finally, update the name/FID to point to the new file/directory
4230 * in a separate transaction.
4233 /* step 1: Check whether the orphan object has been created, and create
4234 * orphan object on the remote MDT if needed */
4235 if (!mdd_object_exists(mdd_tobj)) {
4236 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4243 LASSERT(mdd_object_exists(mdd_tobj));
4244 /* step 2: migrate xattr */
4245 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4249 /* step 3: migrate name entries to the orphan object */
4250 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4251 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4254 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4255 OBD_FAIL_MDS_REINT_NET_REP)))
4258 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4261 LASSERT(mdd_object_exists(mdd_tobj));
4262 /* step 4: update name entry to the new object */
4263 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4268 /* newly created target was not locked, don't cache its attributes */
4270 mdd_invalidate(env, tobj);
4275 const struct md_dir_operations mdd_dir_ops = {
4276 .mdo_is_subdir = mdd_is_subdir,
4277 .mdo_lookup = mdd_lookup,
4278 .mdo_create = mdd_create,
4279 .mdo_rename = mdd_rename,
4280 .mdo_link = mdd_link,
4281 .mdo_unlink = mdd_unlink,
4282 .mdo_create_data = mdd_create_data,
4283 .mdo_migrate = mdd_migrate,