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 = CLF_EXTRA_FLAGS;
739 enum changelog_rec_extra_flags crfe = CLFE_UIDGID;
744 if (uc != NULL && uc->uc_jobid[0] != '\0')
747 return llog_data_len(LLOG_CHANGELOG_HDR_SZ +
748 changelog_rec_offset(crf, crfe) +
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 struct mdd_changelog_gc {
797 struct mdd_device *mcgc_mdd;
800 __u64 mcgc_maxindexes;
804 /* return first registered ChangeLog user idle since too long
805 * use ChangeLog's user plain LLOG mtime for this */
806 static int mdd_changelog_gc_cb(const struct lu_env *env,
807 struct llog_handle *llh,
808 struct llog_rec_hdr *hdr, void *data)
810 struct llog_changelog_user_rec *rec;
811 struct mdd_changelog_gc *mcgc = (struct mdd_changelog_gc *)data;
812 struct mdd_device *mdd = mcgc->mcgc_mdd;
815 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
818 rec = container_of(hdr, struct llog_changelog_user_rec,
821 /* find oldest idle user, based on last record update/cancel time (new
822 * behavior), or for old user records, last record index vs current
823 * ChangeLog index. Late users with old record format will be treated
824 * first as we assume they could be idle since longer
826 if (rec->cur_time != 0) {
827 __u32 time_now = (__u32)get_seconds();
828 __u32 time_out = rec->cur_time +
829 mdd->mdd_changelog_max_idle_time;
830 __u32 idle_time = time_now - rec->cur_time;
832 /* treat oldest idle user first, and if no old format user
833 * has been already selected
835 if (time_after32(time_now, time_out) &&
836 idle_time > mcgc->mcgc_maxtime &&
837 mcgc->mcgc_maxindexes == 0) {
838 mcgc->mcgc_maxtime = idle_time;
839 mcgc->mcgc_id = rec->cur_id;
840 mcgc->mcgc_found = true;
843 /* old user record with no idle time stamp, so use empirical
844 * method based on its current index/position
848 idle_indexes = mdd->mdd_cl.mc_index - rec->cur_endrec;
850 /* treat user with the oldest/smallest current index first */
851 if (idle_indexes >= mdd->mdd_changelog_max_idle_indexes &&
852 idle_indexes > mcgc->mcgc_maxindexes) {
853 mcgc->mcgc_maxindexes = idle_indexes;
854 mcgc->mcgc_id = rec->cur_id;
855 mcgc->mcgc_found = true;
862 /* recover space from long-term inactive ChangeLog users */
863 static int mdd_chlg_garbage_collect(void *data)
865 struct mdd_device *mdd = (struct mdd_device *)data;
866 struct lu_env *env = NULL;
868 struct llog_ctxt *ctxt;
869 struct mdd_changelog_gc mcgc = {
873 .mcgc_maxindexes = 0,
877 CDEBUG(D_HA, "%s: ChangeLog garbage collect thread start\n",
878 mdd2obd_dev(mdd)->obd_name);
882 GOTO(out, rc = -ENOMEM);
884 rc = lu_env_init(env, LCT_MD_THREAD);
889 ctxt = llog_get_context(mdd2obd_dev(mdd),
890 LLOG_CHANGELOG_USER_ORIG_CTXT);
892 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
893 GOTO(out_env, rc = -ENXIO);
895 rc = llog_cat_process(env, ctxt->loc_handle,
896 mdd_changelog_gc_cb, &mcgc, 0, 0);
897 if (rc != 0 || mcgc.mcgc_found == false)
901 CWARN("%s: Force deregister of ChangeLog user cl%d idle more "
902 "than %us\n", mdd2obd_dev(mdd)->obd_name, mcgc.mcgc_id,
905 mdd_changelog_user_purge(env, mdd, mcgc.mcgc_id);
907 /* try again to search for another candidate */
908 mcgc.mcgc_found = false;
909 mcgc.mcgc_maxtime = 0;
910 mcgc.mcgc_maxindexes = 0;
922 mdd->mdd_cl.mc_gc_task = NULL;
926 /** Add a changelog entry \a rec to the changelog llog
929 * \param handle - currently ignored since llogs start their own transaction;
930 * this will hopefully be fixed in llog rewrite
933 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
934 struct llog_changelog_rec *rec, struct thandle *th)
936 struct obd_device *obd = mdd2obd_dev(mdd);
937 struct llog_ctxt *ctxt;
938 struct thandle *llog_th;
942 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
943 changelog_rec_varsize(&rec->cr));
945 /* llog_lvfs_write_rec sets the llog tail len */
946 rec->cr_hdr.lrh_type = CHANGELOG_REC;
947 rec->cr.cr_time = cl_time();
949 spin_lock(&mdd->mdd_cl.mc_lock);
950 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
951 * but as long as the MDD transactions are ordered correctly for e.g.
952 * rename conflicts, I don't think this should matter. */
953 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
954 spin_unlock(&mdd->mdd_cl.mc_lock);
956 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
960 llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
962 GOTO(out_put, rc = PTR_ERR(llog_th));
964 /* nested journal transaction */
965 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
967 /* time to recover some space ?? */
968 spin_lock(&mdd->mdd_cl.mc_lock);
969 if (unlikely(mdd->mdd_changelog_gc && (ktime_get_real_seconds() -
970 mdd->mdd_cl.mc_gc_time > mdd->mdd_changelog_min_gc_interval) &&
971 mdd->mdd_cl.mc_gc_task == NULL &&
972 llog_cat_free_space(ctxt->loc_handle) <=
973 mdd->mdd_changelog_min_free_cat_entries)) {
974 CWARN("%s: low on changelog_catalog free entries, starting "
975 "ChangeLog garbage collection thread\n", obd->obd_name);
977 /* indicate further kthread run will occur outside right after
980 mdd->mdd_cl.mc_gc_task = (struct task_struct *)(-1);
983 spin_unlock(&mdd->mdd_cl.mc_lock);
985 struct task_struct *gc_task;
987 gc_task = kthread_run(mdd_chlg_garbage_collect, mdd,
989 if (IS_ERR(gc_task)) {
990 CERROR("%s: cannot start ChangeLog garbage collection "
991 "thread: rc = %ld\n", obd->obd_name,
993 mdd->mdd_cl.mc_gc_task = NULL;
995 CDEBUG(D_HA, "%s: ChangeLog garbage collection thread "
996 "has started with Pid %d\n", obd->obd_name,
998 mdd->mdd_cl.mc_gc_task = gc_task;
999 mdd->mdd_cl.mc_gc_time = ktime_get_real_seconds();
1003 llog_ctxt_put(ctxt);
1009 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
1010 const struct lu_fid *sfid,
1011 const struct lu_fid *spfid,
1012 const struct lu_name *sname)
1014 struct changelog_ext_rename *rnm = changelog_rec_rename(rec);
1015 size_t extsize = sname->ln_namelen + 1;
1017 LASSERT(sfid != NULL);
1018 LASSERT(spfid != NULL);
1019 LASSERT(sname != NULL);
1021 rnm->cr_sfid = *sfid;
1022 rnm->cr_spfid = *spfid;
1024 changelog_rec_name(rec)[rec->cr_namelen] = '\0';
1025 strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
1026 rec->cr_namelen += extsize;
1029 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
1031 struct changelog_ext_jobid *jid = changelog_rec_jobid(rec);
1033 if (jobid == NULL || jobid[0] == '\0')
1036 strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
1039 void mdd_changelog_rec_ext_extra_flags(struct changelog_rec *rec, __u64 eflags)
1041 struct changelog_ext_extra_flags *ef = changelog_rec_extra_flags(rec);
1043 ef->cr_extra_flags = eflags;
1046 void mdd_changelog_rec_extra_uidgid(struct changelog_rec *rec,
1047 __u64 uid, __u64 gid)
1049 struct changelog_ext_uidgid *uidgid = changelog_rec_uidgid(rec);
1051 uidgid->cr_uid = uid;
1052 uidgid->cr_gid = gid;
1055 /** Store a namespace change changelog record
1056 * If this fails, we must fail the whole transaction; we don't
1057 * want the change to commit without the log entry.
1058 * \param target - mdd_object of change
1059 * \param tpfid - target parent dir/object fid
1060 * \param sfid - source object fid
1061 * \param spfid - source parent fid
1062 * \param tname - target name string
1063 * \param sname - source name string
1064 * \param handle - transaction handle
1066 int mdd_changelog_ns_store(const struct lu_env *env,
1067 struct mdd_device *mdd,
1068 enum changelog_rec_type type,
1069 enum changelog_rec_flags crf,
1070 struct mdd_object *target,
1071 const struct lu_fid *tpfid,
1072 const struct lu_fid *sfid,
1073 const struct lu_fid *spfid,
1074 const struct lu_name *tname,
1075 const struct lu_name *sname,
1076 struct thandle *handle)
1078 const struct lu_ucred *uc = lu_ucred(env);
1079 struct llog_changelog_rec *rec;
1082 __u64 xflags = CLFE_INVALID;
1087 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
1090 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
1093 LASSERT(tpfid != NULL);
1094 LASSERT(tname != NULL);
1095 LASSERT(handle != NULL);
1097 reclen = mdd_llog_record_calc_size(env, tname, sname);
1098 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
1099 if (buf->lb_buf == NULL)
1103 crf &= CLF_FLAGMASK;
1104 crf |= CLF_EXTRA_FLAGS;
1106 if (uc != NULL && uc->uc_jobid[0] != '\0')
1114 xflags |= CLFE_UIDGID;
1116 rec->cr.cr_flags = crf;
1118 if (crf & CLF_EXTRA_FLAGS) {
1119 mdd_changelog_rec_ext_extra_flags(&rec->cr, xflags);
1120 if (xflags & CLFE_UIDGID)
1121 mdd_changelog_rec_extra_uidgid(&rec->cr,
1122 uc->uc_uid, uc->uc_gid);
1125 rec->cr.cr_type = (__u32)type;
1126 rec->cr.cr_pfid = *tpfid;
1127 rec->cr.cr_namelen = tname->ln_namelen;
1128 memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
1130 if (crf & CLF_RENAME)
1131 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
1133 if (crf & CLF_JOBID)
1134 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
1136 if (likely(target != NULL)) {
1137 rec->cr.cr_tfid = *mdo2fid(target);
1138 target->mod_cltime = ktime_get();
1140 fid_zero(&rec->cr.cr_tfid);
1143 rc = mdd_changelog_store(env, mdd, rec, handle);
1145 CERROR("%s: cannot store changelog record: type = %d, "
1146 "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
1147 mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
1148 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
1155 static int __mdd_links_add(const struct lu_env *env,
1156 struct mdd_object *mdd_obj,
1157 struct linkea_data *ldata,
1158 const struct lu_name *lname,
1159 const struct lu_fid *pfid,
1160 int first, int check)
1164 if (ldata->ld_leh == NULL) {
1165 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
1169 rc = linkea_data_new(ldata,
1170 &mdd_env_info(env)->mti_link_buf);
1177 rc = linkea_links_find(ldata, lname, pfid);
1178 if (rc && rc != -ENOENT)
1184 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
1185 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1189 linkea_add_buf(ldata, lname, tfid);
1192 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
1193 linkea_add_buf(ldata, lname, pfid);
1195 return linkea_add_buf(ldata, lname, pfid);
1198 static int __mdd_links_del(const struct lu_env *env,
1199 struct mdd_object *mdd_obj,
1200 struct linkea_data *ldata,
1201 const struct lu_name *lname,
1202 const struct lu_fid *pfid)
1206 if (ldata->ld_leh == NULL) {
1207 rc = mdd_links_read(env, mdd_obj, ldata);
1212 rc = linkea_links_find(ldata, lname, pfid);
1216 linkea_del_buf(ldata, lname);
1220 static int mdd_linkea_prepare(const struct lu_env *env,
1221 struct mdd_object *mdd_obj,
1222 const struct lu_fid *oldpfid,
1223 const struct lu_name *oldlname,
1224 const struct lu_fid *newpfid,
1225 const struct lu_name *newlname,
1226 int first, int check,
1227 struct linkea_data *ldata)
1232 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
1235 LASSERT(oldpfid != NULL || newpfid != NULL);
1237 if (mdd_obj->mod_flags & DEAD_OBJ)
1238 /* Unnecessary to update linkEA for dead object. */
1241 if (oldpfid != NULL) {
1242 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
1244 if ((check == 1) || (rc != -ENODATA && rc != -ENOENT))
1247 /* No changes done. */
1252 /* If renaming, add the new record */
1253 if (newpfid != NULL)
1254 rc = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
1260 int mdd_links_rename(const struct lu_env *env,
1261 struct mdd_object *mdd_obj,
1262 const struct lu_fid *oldpfid,
1263 const struct lu_name *oldlname,
1264 const struct lu_fid *newpfid,
1265 const struct lu_name *newlname,
1266 struct thandle *handle,
1267 struct linkea_data *ldata,
1268 int first, int check)
1273 if (ldata == NULL) {
1274 ldata = &mdd_env_info(env)->mti_link_data;
1275 memset(ldata, 0, sizeof(*ldata));
1276 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1277 newpfid, newlname, first, check, ldata);
1282 if (!(mdd_obj->mod_flags & DEAD_OBJ))
1283 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1289 if (newlname == NULL)
1290 CERROR("link_ea add failed %d "DFID"\n",
1291 rc, PFID(mdd_object_fid(mdd_obj)));
1292 else if (oldpfid == NULL)
1293 CERROR("link_ea add '%.*s' failed %d "DFID"\n",
1294 newlname->ln_namelen, newlname->ln_name, rc,
1295 PFID(mdd_object_fid(mdd_obj)));
1296 else if (newpfid == NULL)
1297 CERROR("link_ea del '%.*s' failed %d "DFID"\n",
1298 oldlname->ln_namelen, oldlname->ln_name, rc,
1299 PFID(mdd_object_fid(mdd_obj)));
1301 CERROR("link_ea rename '%.*s'->'%.*s' failed %d "DFID
1302 "\n", oldlname->ln_namelen, oldlname->ln_name,
1303 newlname->ln_namelen, newlname->ln_name, rc,
1304 PFID(mdd_object_fid(mdd_obj)));
1307 if (is_vmalloc_addr(ldata->ld_buf))
1308 /* if we vmalloced a large buffer drop it */
1309 lu_buf_free(ldata->ld_buf);
1314 static inline int mdd_links_add(const struct lu_env *env,
1315 struct mdd_object *mdd_obj,
1316 const struct lu_fid *pfid,
1317 const struct lu_name *lname,
1318 struct thandle *handle,
1319 struct linkea_data *ldata, int first)
1321 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1322 pfid, lname, handle, ldata, first, 0);
1325 static inline int mdd_links_del(const struct lu_env *env,
1326 struct mdd_object *mdd_obj,
1327 const struct lu_fid *pfid,
1328 const struct lu_name *lname,
1329 struct thandle *handle)
1331 return mdd_links_rename(env, mdd_obj, pfid, lname,
1332 NULL, NULL, handle, NULL, 0, 0);
1335 /** Read the link EA into a temp buffer.
1336 * Uses the name_buf since it is generally large.
1337 * \retval IS_ERR err
1338 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1340 struct lu_buf *mdd_links_get(const struct lu_env *env,
1341 struct mdd_object *mdd_obj)
1343 struct linkea_data ldata = { NULL };
1346 rc = mdd_links_read(env, mdd_obj, &ldata);
1347 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1350 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1351 struct linkea_data *ldata, struct thandle *handle)
1353 const struct lu_buf *buf;
1356 if (ldata == NULL || ldata->ld_buf == NULL ||
1357 ldata->ld_leh == NULL)
1360 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1364 buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1365 ldata->ld_leh->leh_len);
1366 rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1367 if (unlikely(rc == -ENOSPC)) {
1368 rc = linkea_overflow_shrink(ldata);
1376 static int mdd_declare_links_add(const struct lu_env *env,
1377 struct mdd_object *mdd_obj,
1378 struct thandle *handle,
1379 struct linkea_data *ldata)
1385 if (ldata != NULL && ldata->ld_leh != NULL) {
1386 ea_len = ldata->ld_leh->leh_len;
1387 linkea = ldata->ld_buf->lb_buf;
1389 ea_len = MAX_LINKEA_SIZE;
1393 rc = mdo_declare_xattr_set(env, mdd_obj,
1394 mdd_buf_get_const(env, linkea, ea_len),
1395 XATTR_NAME_LINK, 0, handle);
1400 static inline int mdd_declare_links_del(const struct lu_env *env,
1401 struct mdd_object *c,
1402 struct thandle *handle)
1406 /* For directory, the linkEA will be removed together
1407 * with the object. */
1408 if (!S_ISDIR(mdd_object_type(c)))
1409 rc = mdd_declare_links_add(env, c, handle, NULL);
1414 static int mdd_declare_link(const struct lu_env *env,
1415 struct mdd_device *mdd,
1416 struct mdd_object *p,
1417 struct mdd_object *c,
1418 const struct lu_name *name,
1419 struct thandle *handle,
1421 struct linkea_data *data)
1423 struct lu_fid tfid = *mdo2fid(c);
1426 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1427 tfid.f_oid = cfs_fail_val;
1429 rc = mdo_declare_index_insert(env, p, &tfid, mdd_object_type(c),
1430 name->ln_name, handle);
1434 rc = mdo_declare_ref_add(env, c, handle);
1438 la->la_valid = LA_CTIME | LA_MTIME;
1439 rc = mdo_declare_attr_set(env, p, la, handle);
1443 la->la_valid = LA_CTIME;
1444 rc = mdo_declare_attr_set(env, c, la, handle);
1448 rc = mdd_declare_links_add(env, c, handle, data);
1452 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1457 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1458 struct md_object *src_obj, const struct lu_name *lname,
1461 const char *name = lname->ln_name;
1462 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1463 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1464 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1465 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1466 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1467 struct mdd_device *mdd = mdo2mdd(src_obj);
1468 struct thandle *handle;
1469 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
1470 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1474 rc = mdd_la_get(env, mdd_sobj, cattr);
1478 rc = mdd_la_get(env, mdd_tobj, tattr);
1483 * If we are using project inheritance, we only allow hard link
1484 * creation in our tree when the project IDs are the same;
1485 * otherwise the tree quota mechanism could be circumvented.
1487 if ((tattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
1488 (tattr->la_projid != cattr->la_projid))
1491 handle = mdd_trans_create(env, mdd);
1493 GOTO(out_pending, rc = PTR_ERR(handle));
1495 memset(ldata, 0, sizeof(*ldata));
1497 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1498 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1500 /* Note: even this function will change ldata, but it comes from
1501 * thread_info, which is completely temporary and only seen in
1502 * this function, so we do not need reset ldata once it fails.*/
1503 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdo2fid(mdd_tobj),
1504 lname, 0, 0, ldata);
1508 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1513 rc = mdd_trans_start(env, mdd, handle);
1517 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1518 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1521 GOTO(out_unlock, rc);
1523 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1524 rc = mdo_ref_add(env, mdd_sobj, handle);
1526 GOTO(out_unlock, rc);
1529 *tfid = *mdo2fid(mdd_sobj);
1530 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3))
1531 tfid->f_oid = cfs_fail_val;
1533 rc = __mdd_index_insert_only(env, mdd_tobj, tfid,
1534 mdd_object_type(mdd_sobj), name, handle);
1536 mdo_ref_del(env, mdd_sobj, handle);
1537 GOTO(out_unlock, rc);
1540 la->la_valid = LA_CTIME | LA_MTIME;
1541 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1543 GOTO(out_unlock, rc);
1545 la->la_valid = LA_CTIME;
1546 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1548 /* Note: The failure of links_add should not cause the
1549 * link failure, so do not check return value. */
1550 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1551 lname, handle, ldata, 0);
1555 mdd_write_unlock(env, mdd_sobj);
1557 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1558 mdo2fid(mdd_tobj), NULL, NULL,
1559 lname, NULL, handle);
1561 rc = mdd_trans_stop(env, mdd, rc, handle);
1562 if (is_vmalloc_addr(ldata->ld_buf))
1563 /* if we vmalloced a large buffer drop it */
1564 lu_buf_free(ldata->ld_buf);
1569 static int mdd_mark_orphan_object(const struct lu_env *env,
1570 struct mdd_object *obj, struct thandle *handle,
1573 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1576 if (!S_ISDIR(mdd_object_type(obj)))
1579 attr->la_valid = LA_FLAGS;
1580 attr->la_flags = LUSTRE_ORPHAN_FL;
1583 rc = mdo_declare_attr_set(env, obj, attr, handle);
1585 rc = mdo_attr_set(env, obj, attr, handle);
1590 static int mdd_declare_finish_unlink(const struct lu_env *env,
1591 struct mdd_object *obj,
1592 struct thandle *handle)
1596 /* Sigh, we do not know if the unlink object will become orphan in
1597 * declare phase, but fortunately the flags here does not matter
1598 * in current declare implementation */
1599 rc = mdd_mark_orphan_object(env, obj, handle, true);
1603 rc = mdo_declare_destroy(env, obj, handle);
1607 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1611 return mdd_declare_links_del(env, obj, handle);
1614 /* caller should take a lock before calling */
1615 int mdd_finish_unlink(const struct lu_env *env,
1616 struct mdd_object *obj, struct md_attr *ma,
1617 const struct mdd_object *pobj,
1618 const struct lu_name *lname,
1622 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1625 LASSERT(mdd_write_locked(env, obj) != 0);
1627 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1628 /* add new orphan and the object
1629 * will be deleted during mdd_close() */
1630 obj->mod_flags |= DEAD_OBJ;
1631 if (obj->mod_count) {
1632 rc = __mdd_orphan_add(env, obj, th);
1634 CDEBUG(D_HA, "Object "DFID" is inserted into "
1635 "orphan list, open count = %d\n",
1636 PFID(mdd_object_fid(obj)),
1639 CERROR("Object "DFID" fail to be an orphan, "
1640 "open count = %d, maybe cause failed "
1642 PFID(mdd_object_fid(obj)),
1645 /* mark object as an orphan here, not
1646 * before __mdd_orphan_add() as racing
1647 * mdd_la_get() may propagate ORPHAN_OBJ
1648 * causing the asserition */
1649 rc = mdd_mark_orphan_object(env, obj, th, false);
1651 rc = mdo_destroy(env, obj, th);
1653 } else if (!is_dir) {
1654 /* old files may not have link ea; ignore errors */
1655 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1663 * has mdd_write_lock on cobj already, but not on pobj yet
1665 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1666 const struct lu_attr *pattr,
1667 struct mdd_object *cobj,
1668 const struct lu_attr *cattr)
1673 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1678 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1679 struct mdd_object *p, struct mdd_object *c,
1680 const struct lu_name *name, struct md_attr *ma,
1681 struct thandle *handle, int no_name, int is_dir)
1683 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1686 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1687 if (likely(no_name == 0)) {
1688 rc = mdo_declare_index_delete(env, p, name->ln_name,
1695 rc = mdo_declare_ref_del(env, p, handle);
1701 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1702 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1703 la->la_valid = LA_CTIME | LA_MTIME;
1704 rc = mdo_declare_attr_set(env, p, la, handle);
1709 rc = mdo_declare_ref_del(env, c, handle);
1713 rc = mdo_declare_ref_del(env, c, handle);
1717 la->la_valid = LA_CTIME;
1718 rc = mdo_declare_attr_set(env, c, la, handle);
1722 rc = mdd_declare_finish_unlink(env, c, handle);
1726 /* FIXME: need changelog for remove entry */
1727 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1734 * test if a file has an HSM archive
1735 * if HSM attributes are not found in ma update them from
1738 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1739 struct mdd_object *obj,
1744 if (!(ma->ma_valid & MA_HSM)) {
1745 /* no HSM MD provided, read xattr */
1746 struct lu_buf *hsm_buf;
1747 const size_t buflen = sizeof(struct hsm_attrs);
1750 hsm_buf = mdd_buf_get(env, NULL, 0);
1751 lu_buf_alloc(hsm_buf, buflen);
1752 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1753 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1754 lu_buf_free(hsm_buf);
1758 ma->ma_valid |= MA_HSM;
1760 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1766 * Delete name entry and the object.
1767 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1768 * does not exist for this object, and it could only happen during resending
1769 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1770 * is also needed in this case(needed by changelog), so we have to add another
1771 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1772 * the ENOENT failure should be able to be fixed by redo mechanism.
1774 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1775 struct md_object *cobj, const struct lu_name *lname,
1776 struct md_attr *ma, int no_name)
1778 const char *name = lname->ln_name;
1779 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1780 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1781 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1782 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1783 struct mdd_object *mdd_cobj = NULL;
1784 struct mdd_device *mdd = mdo2mdd(pobj);
1785 struct thandle *handle;
1786 int rc, is_dir = 0, cl_flags = 0;
1789 /* cobj == NULL means only delete name entry */
1790 if (likely(cobj != NULL)) {
1791 mdd_cobj = md2mdd_obj(cobj);
1792 if (mdd_object_exists(mdd_cobj) == 0)
1796 rc = mdd_la_get(env, mdd_pobj, pattr);
1800 if (likely(mdd_cobj != NULL)) {
1802 rc = mdd_la_get(env, mdd_cobj, cattr);
1806 is_dir = S_ISDIR(cattr->la_mode);
1807 /* search for an existing archive.
1808 * we should check ahead as the object
1809 * can be destroyed in this transaction */
1810 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1811 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1814 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1818 handle = mdd_trans_create(env, mdd);
1820 RETURN(PTR_ERR(handle));
1822 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1823 lname, ma, handle, no_name, is_dir);
1827 rc = mdd_trans_start(env, mdd, handle);
1831 if (likely(mdd_cobj != NULL))
1832 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1834 if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1835 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1840 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1841 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1842 GOTO(cleanup, rc = 0);
1844 if (likely(mdd_cobj != NULL)) {
1845 rc = mdo_ref_del(env, mdd_cobj, handle);
1847 __mdd_index_insert_only(env, mdd_pobj,
1849 mdd_object_type(mdd_cobj),
1856 mdo_ref_del(env, mdd_cobj, handle);
1858 /* fetch updated nlink */
1859 rc = mdd_la_get(env, mdd_cobj, cattr);
1864 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1865 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1867 la->la_valid = LA_CTIME | LA_MTIME;
1868 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1872 /* Enough for only unlink the entry */
1873 if (unlikely(mdd_cobj == NULL))
1876 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1877 /* update ctime of an unlinked file only if it is still
1878 * opened or a link still exists */
1879 la->la_valid = LA_CTIME;
1880 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1885 /* XXX: this transfer to ma will be removed with LOD/OSP */
1886 ma->ma_attr = *cattr;
1887 ma->ma_valid |= MA_INODE;
1888 rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1892 /* fetch updated nlink */
1893 rc = mdd_la_get(env, mdd_cobj, cattr);
1894 /* if object is removed then we can't get its attrs,
1896 if (rc == -ENOENT) {
1897 cattr->la_nlink = 0;
1901 if (cattr->la_nlink == 0) {
1902 ma->ma_attr = *cattr;
1903 ma->ma_valid |= MA_INODE;
1908 if (likely(mdd_cobj != NULL))
1909 mdd_write_unlock(env, mdd_cobj);
1912 if (cattr->la_nlink == 0)
1913 cl_flags |= CLF_UNLINK_LAST;
1915 cl_flags &= ~CLF_UNLINK_HSM_EXISTS;
1917 rc = mdd_changelog_ns_store(env, mdd,
1918 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1919 mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1924 rc = mdd_trans_stop(env, mdd, rc, handle);
1930 * The permission has been checked when obj created, no need check again.
1932 static int mdd_cd_sanity_check(const struct lu_env *env,
1933 struct mdd_object *obj)
1938 if (!obj || mdd_is_dead_obj(obj))
1944 static int mdd_create_data(const struct lu_env *env,
1945 struct md_object *pobj,
1946 struct md_object *cobj,
1947 const struct md_op_spec *spec,
1950 struct mdd_device *mdd = mdo2mdd(cobj);
1951 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1952 struct mdd_object *son = md2mdd_obj(cobj);
1953 struct thandle *handle;
1954 const struct lu_buf *buf;
1955 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1956 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1960 rc = mdd_cd_sanity_check(env, son);
1964 if (!md_should_create(spec->sp_cr_flags))
1968 * there are following use cases for this function:
1969 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1970 * striping can be specified or not
1973 rc = mdd_la_get(env, son, attr);
1977 /* calling ->ah_make_hint() is used to transfer information from parent */
1978 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1980 handle = mdd_trans_create(env, mdd);
1982 GOTO(out_free, rc = PTR_ERR(handle));
1985 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1986 * Should this be fixed?
1988 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %#llo, no_create %u\n",
1989 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1990 spec->sp_cr_flags, spec->no_create);
1992 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1993 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1994 spec->u.sp_ea.eadatalen);
1999 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
2000 XATTR_NAME_LOV, 0, handle);
2004 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
2008 rc = mdd_trans_start(env, mdd, handle);
2012 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
2018 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
2021 rc = mdd_trans_stop(env, mdd, rc, handle);
2027 static int mdd_declare_object_initialize(const struct lu_env *env,
2028 struct mdd_object *parent,
2029 struct mdd_object *child,
2030 const struct lu_attr *attr,
2031 struct thandle *handle)
2036 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
2037 if (!S_ISDIR(attr->la_mode))
2040 rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
2045 rc = mdo_declare_ref_add(env, child, handle);
2049 rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
2055 static int mdd_object_initialize(const struct lu_env *env,
2056 const struct lu_fid *pfid,
2057 struct mdd_object *child,
2058 struct lu_attr *attr, struct thandle *handle,
2059 const struct md_op_spec *spec)
2064 if (S_ISDIR(attr->la_mode)) {
2065 /* Add "." and ".." for newly created dir */
2066 mdo_ref_add(env, child, handle);
2067 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
2068 S_IFDIR, dot, handle);
2070 rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
2073 mdo_ref_del(env, child, handle);
2080 * This function checks whether it can create a file/dir under the
2081 * directory(@pobj). The directory(@pobj) is not being locked by
2084 * \param[in] env execution environment
2085 * \param[in] pobj the directory to create files
2086 * \param[in] pattr the attributes of the directory
2087 * \param[in] lname the name of the created file/dir
2088 * \param[in] cattr the attributes of the file/dir
2089 * \param[in] spec create specification
2091 * \retval = 0 it is allowed to create file/dir under
2093 * \retval negative error not allowed to create file/dir
2094 * under the directory
2096 static int mdd_create_sanity_check(const struct lu_env *env,
2097 struct md_object *pobj,
2098 const struct lu_attr *pattr,
2099 const struct lu_name *lname,
2100 struct lu_attr *cattr,
2101 struct md_op_spec *spec)
2103 struct mdd_thread_info *info = mdd_env_info(env);
2104 struct lu_fid *fid = &info->mti_fid;
2105 struct mdd_object *obj = md2mdd_obj(pobj);
2106 struct mdd_device *m = mdo2mdd(pobj);
2107 bool check_perm = true;
2112 if (mdd_is_dead_obj(obj))
2116 * In some cases this lookup is not needed - we know before if name
2117 * exists or not because MDT performs lookup for it.
2118 * name length check is done in lookup.
2120 if (spec->sp_cr_lookup) {
2122 * Check if the name already exist, though it will be checked in
2123 * _index_insert also, for avoiding rolling back if exists
2126 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
2127 MAY_WRITE | MAY_EXEC);
2129 RETURN(rc ? : -EEXIST);
2131 /* Permission is already being checked in mdd_lookup */
2135 if (S_ISDIR(cattr->la_mode) &&
2136 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA) &&
2137 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen > 0) {
2138 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
2140 if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC) &&
2141 le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC_V0) {
2143 CERROR("%s: invalid lmv_user_md: magic = %x, "
2144 "stripe_offset = %d, stripe_count = %u: "
2145 "rc = %d\n", mdd2obd_dev(m)->obd_name,
2146 le32_to_cpu(lum->lum_magic),
2147 (int)le32_to_cpu(lum->lum_stripe_offset),
2148 le32_to_cpu(lum->lum_stripe_count), rc);
2153 rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
2158 if (pattr->la_mode & S_ISGID) {
2159 cattr->la_gid = pattr->la_gid;
2160 if (S_ISDIR(cattr->la_mode)) {
2161 cattr->la_mode |= S_ISGID;
2162 cattr->la_valid |= LA_MODE;
2166 /* Inherit project ID from parent directory */
2167 if (pattr->la_flags & LUSTRE_PROJINHERIT_FL) {
2168 cattr->la_projid = pattr->la_projid;
2169 if (S_ISDIR(cattr->la_mode)) {
2170 cattr->la_flags |= LUSTRE_PROJINHERIT_FL;
2171 cattr->la_valid |= LA_FLAGS;
2173 cattr->la_valid |= LA_PROJID;
2176 rc = mdd_name_check(m, lname);
2180 switch (cattr->la_mode & S_IFMT) {
2182 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
2184 if (symlen > m->mdd_dt_conf.ddp_symlink_max)
2185 RETURN(-ENAMETOOLONG);
2204 static int mdd_declare_create_object(const struct lu_env *env,
2205 struct mdd_device *mdd,
2206 struct mdd_object *p, struct mdd_object *c,
2207 struct lu_attr *attr,
2208 struct thandle *handle,
2209 const struct md_op_spec *spec,
2210 struct lu_buf *def_acl_buf,
2211 struct lu_buf *acl_buf,
2212 struct dt_allocation_hint *hint)
2214 const struct lu_buf *buf;
2217 rc = mdd_declare_create_object_internal(env, p, c, attr, handle, spec,
2222 #ifdef CONFIG_FS_POSIX_ACL
2223 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
2224 /* if dir, then can inherit default ACl */
2225 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
2226 XATTR_NAME_ACL_DEFAULT,
2232 if (acl_buf->lb_len > 0) {
2233 rc = mdo_declare_attr_set(env, c, attr, handle);
2237 rc = mdo_declare_xattr_set(env, c, acl_buf,
2238 XATTR_NAME_ACL_ACCESS, 0, handle);
2243 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2247 /* replay case, create LOV EA from client data */
2248 if (spec->no_create ||
2249 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2250 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2251 spec->u.sp_ea.eadatalen);
2252 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2258 if (S_ISLNK(attr->la_mode)) {
2259 const char *target_name = spec->u.sp_symname;
2260 int sym_len = strlen(target_name);
2261 const struct lu_buf *buf;
2263 buf = mdd_buf_get_const(env, target_name, sym_len);
2264 rc = dt_declare_record_write(env, mdd_object_child(c),
2270 if (spec->sp_cr_file_secctx_name != NULL) {
2271 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2272 spec->sp_cr_file_secctx_size);
2273 rc = mdo_declare_xattr_set(env, c, buf,
2274 spec->sp_cr_file_secctx_name, 0,
2283 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2284 struct mdd_object *p, struct mdd_object *c,
2285 const struct lu_name *name,
2286 struct lu_attr *attr,
2287 struct thandle *handle,
2288 const struct md_op_spec *spec,
2289 struct linkea_data *ldata,
2290 struct lu_buf *def_acl_buf,
2291 struct lu_buf *acl_buf,
2292 struct dt_allocation_hint *hint)
2296 rc = mdd_declare_create_object(env, mdd, p, c, attr, handle, spec,
2297 def_acl_buf, acl_buf, hint);
2301 if (S_ISDIR(attr->la_mode)) {
2302 rc = mdo_declare_ref_add(env, p, handle);
2307 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2308 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2312 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2314 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2315 name->ln_name, handle);
2319 rc = mdd_declare_links_add(env, c, handle, ldata);
2324 la->la_valid = LA_CTIME | LA_MTIME;
2325 rc = mdo_declare_attr_set(env, p, la, handle);
2329 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2337 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2338 struct lu_attr *la, struct lu_buf *def_acl_buf,
2339 struct lu_buf *acl_buf)
2344 if (S_ISLNK(la->la_mode)) {
2345 acl_buf->lb_len = 0;
2346 def_acl_buf->lb_len = 0;
2350 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2351 rc = mdo_xattr_get(env, pobj, def_acl_buf,
2352 XATTR_NAME_ACL_DEFAULT);
2353 mdd_read_unlock(env, pobj);
2355 /* If there are default ACL, fix mode/ACL by default ACL */
2356 def_acl_buf->lb_len = rc;
2357 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2358 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2359 acl_buf->lb_len = rc;
2360 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2363 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2364 /* If there are no default ACL, fix mode by mask */
2365 struct lu_ucred *uc = lu_ucred(env);
2367 /* The create triggered by MDT internal events, such as
2368 * LFSCK reset, will not contain valid "uc". */
2369 if (unlikely(uc != NULL))
2370 la->la_mode &= ~uc->uc_umask;
2372 acl_buf->lb_len = 0;
2373 def_acl_buf->lb_len = 0;
2380 * Create a metadata object and initialize it, set acl, xattr.
2382 static int mdd_create_object(const struct lu_env *env, struct mdd_object *pobj,
2383 struct mdd_object *son, struct lu_attr *attr,
2384 struct md_op_spec *spec, struct lu_buf *acl_buf,
2385 struct lu_buf *def_acl_buf,
2386 struct dt_allocation_hint *hint,
2387 struct thandle *handle)
2389 const struct lu_buf *buf;
2392 mdd_write_lock(env, son, MOR_TGT_CHILD);
2393 rc = mdd_create_object_internal(env, NULL, son, attr, handle, spec,
2398 /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2399 * created in declare phase, they also needs to be added to master
2400 * object as sub-directory entry. So it has to initialize the master
2401 * object, then set dir striped EA.(in mdo_xattr_set) */
2402 rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2405 GOTO(err_destroy, rc);
2408 * in case of replay we just set LOVEA provided by the client
2409 * XXX: I think it would be interesting to try "old" way where
2410 * MDT calls this xattr_set(LOV) in a different transaction.
2411 * probably this way we code can be made better.
2414 /* During creation, there are only a few cases we need do xattr_set to
2416 * 1. regular file: see comments above.
2417 * 2. dir: inherit default striping or pool settings from parent.
2418 * 3. create striped directory with provided stripeEA.
2419 * 4. create striped directory because inherit default layout from the
2422 if (spec->no_create ||
2423 (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2424 S_ISDIR(attr->la_mode)) {
2425 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2426 spec->u.sp_ea.eadatalen);
2427 rc = mdo_xattr_set(env, son, buf,
2428 S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2432 GOTO(err_destroy, rc);
2435 #ifdef CONFIG_FS_POSIX_ACL
2436 if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2437 S_ISDIR(attr->la_mode)) {
2438 /* set default acl */
2439 rc = mdo_xattr_set(env, son, def_acl_buf,
2440 XATTR_NAME_ACL_DEFAULT, 0,
2443 GOTO(err_destroy, rc);
2445 /* set its own acl */
2446 if (acl_buf != NULL && acl_buf->lb_len > 0) {
2447 rc = mdo_xattr_set(env, son, acl_buf,
2448 XATTR_NAME_ACL_ACCESS,
2451 GOTO(err_destroy, rc);
2455 if (S_ISLNK(attr->la_mode)) {
2456 struct lu_ucred *uc = lu_ucred_assert(env);
2457 struct dt_object *dt = mdd_object_child(son);
2458 const char *target_name = spec->u.sp_symname;
2459 int sym_len = strlen(target_name);
2462 buf = mdd_buf_get_const(env, target_name, sym_len);
2463 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2465 CFS_CAP_SYS_RESOURCE_MASK);
2470 GOTO(err_initlized, rc = -EFAULT);
2473 if (spec->sp_cr_file_secctx_name != NULL) {
2474 buf = mdd_buf_get_const(env, spec->sp_cr_file_secctx,
2475 spec->sp_cr_file_secctx_size);
2476 rc = mdo_xattr_set(env, son, buf, spec->sp_cr_file_secctx_name,
2479 GOTO(err_initlized, rc);
2483 if (unlikely(rc != 0)) {
2485 if (S_ISDIR(attr->la_mode)) {
2486 /* Drop the reference, no need to delete "."/"..",
2487 * because the object to be destroied directly. */
2488 rc2 = mdo_ref_del(env, son, handle);
2492 rc2 = mdo_ref_del(env, son, handle);
2496 mdo_destroy(env, son, handle);
2499 mdd_write_unlock(env, son);
2503 static int mdd_index_delete(const struct lu_env *env,
2504 struct mdd_object *mdd_pobj,
2505 struct lu_attr *cattr,
2506 const struct lu_name *lname)
2508 struct mdd_device *mdd = mdo2mdd(&mdd_pobj->mod_obj);
2509 struct thandle *handle;
2513 handle = mdd_trans_create(env, mdd);
2515 RETURN(PTR_ERR(handle));
2517 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name,
2522 if (S_ISDIR(cattr->la_mode)) {
2523 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
2528 /* Since this will only be used in the error handler path,
2529 * Let's set the thandle to be local and not mess the transno */
2530 handle->th_local = 1;
2531 rc = mdd_trans_start(env, mdd, handle);
2535 rc = __mdd_index_delete(env, mdd_pobj, lname->ln_name,
2536 S_ISDIR(cattr->la_mode), handle);
2540 rc = mdd_trans_stop(env, mdd, rc, handle);
2546 * Create object and insert it into namespace.
2548 * Two operations have to be performed:
2550 * - an allocation of a new object (->do_create()), and
2551 * - an insertion into a parent index (->dio_insert()).
2553 * Due to locking, operation order is not important, when both are
2554 * successful, *but* error handling cases are quite different:
2556 * - if insertion is done first, and following object creation fails,
2557 * insertion has to be rolled back, but this operation might fail
2558 * also leaving us with dangling index entry.
2560 * - if creation is done first, is has to be undone if insertion fails,
2561 * leaving us with leaked space, which is not good but not fatal.
2563 * It seems that creation-first is simplest solution, but it is sub-optimal
2569 * case, because second mkdir is bound to create object, only to
2570 * destroy it immediately.
2572 * To avoid this follow local file systems that do double lookup:
2574 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2575 * 1. create (mdd_create_object_internal())
2576 * 2. insert (__mdd_index_insert(), lookup again)
2578 * \param[in] pobj parent object
2579 * \param[in] lname name of child being created
2580 * \param[in,out] child child object being created
2581 * \param[in] spec additional create parameters
2582 * \param[in] ma attributes for new child object
2584 * \retval 0 on success
2585 * \retval negative errno on failure
2587 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2588 const struct lu_name *lname, struct md_object *child,
2589 struct md_op_spec *spec, struct md_attr *ma)
2591 struct mdd_thread_info *info = mdd_env_info(env);
2592 struct lu_attr *la = &info->mti_la_for_fix;
2593 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2594 struct mdd_object *son = md2mdd_obj(child);
2595 struct mdd_device *mdd = mdo2mdd(pobj);
2596 struct lu_attr *attr = &ma->ma_attr;
2597 struct thandle *handle;
2598 struct lu_attr *pattr = &info->mti_pattr;
2599 struct lu_buf acl_buf;
2600 struct lu_buf def_acl_buf;
2601 struct linkea_data *ldata = &info->mti_link_data;
2602 const char *name = lname->ln_name;
2603 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2608 rc = mdd_la_get(env, mdd_pobj, pattr);
2612 /* Sanity checks before big job. */
2613 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2617 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2618 GOTO(out_free, rc = -EINPROGRESS);
2620 handle = mdd_trans_create(env, mdd);
2622 GOTO(out_free, rc = PTR_ERR(handle));
2624 lu_buf_check_and_alloc(&info->mti_xattr_buf,
2625 mdd->mdd_dt_conf.ddp_max_ea_size);
2626 acl_buf = info->mti_xattr_buf;
2627 def_acl_buf.lb_buf = info->mti_key;
2628 def_acl_buf.lb_len = sizeof(info->mti_key);
2629 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2633 mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2635 memset(ldata, 0, sizeof(*ldata));
2636 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2637 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2640 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2641 &tfid, lname, 1, 0, ldata);
2643 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2644 mdd_object_fid(mdd_pobj),
2645 lname, 1, 0, ldata);
2648 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2649 handle, spec, ldata, &def_acl_buf, &acl_buf,
2654 rc = mdd_trans_start(env, mdd, handle);
2658 rc = mdd_create_object(env, mdd_pobj, son, attr, spec, &acl_buf,
2659 &def_acl_buf, hint, handle);
2663 if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2664 mdd_write_lock(env, son, MOR_TGT_CHILD);
2665 son->mod_flags |= VOLATILE_OBJ;
2666 rc = __mdd_orphan_add(env, son, handle);
2667 GOTO(out_volatile, rc);
2669 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2670 attr->la_mode, name, handle);
2672 GOTO(err_created, rc);
2674 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2677 /* update parent directory mtime/ctime */
2679 la->la_valid = LA_CTIME | LA_MTIME;
2680 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2682 GOTO(err_insert, rc);
2688 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2689 rc2 = __mdd_orphan_del(env, son, handle);
2691 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2692 S_ISDIR(attr->la_mode),
2698 mdd_write_lock(env, son, MOR_TGT_CHILD);
2699 if (S_ISDIR(attr->la_mode)) {
2700 /* Drop the reference, no need to delete "."/"..",
2701 * because the object is to be destroyed directly. */
2702 rc2 = mdo_ref_del(env, son, handle);
2704 mdd_write_unlock(env, son);
2709 /* For volatile files drop one link immediately, since there is
2710 * no filename in the namespace, and save any error returned. */
2711 rc2 = mdo_ref_del(env, son, handle);
2713 mdd_write_unlock(env, son);
2714 if (unlikely(rc == 0))
2719 /* Don't destroy the volatile object on success */
2720 if (likely(rc != 0))
2721 mdo_destroy(env, son, handle);
2722 mdd_write_unlock(env, son);
2725 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2726 likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2727 rc = mdd_changelog_ns_store(env, mdd,
2728 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2729 S_ISREG(attr->la_mode) ? CL_CREATE :
2730 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2731 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2734 rc2 = mdd_trans_stop(env, mdd, rc, handle);
2736 /* If creation fails, it is most likely due to the remote update
2737 * failure, because local transaction will mostly succeed at
2738 * this stage. There is no easy way to rollback all of previous
2739 * updates, so let's remove the object from namespace, and
2740 * LFSCK should handle the orphan object. */
2741 if (rc2 < 0 && !mdd_object_remote(mdd_pobj))
2742 mdd_index_delete(env, mdd_pobj, attr, lname);
2746 if (is_vmalloc_addr(ldata->ld_buf))
2747 /* if we vmalloced a large buffer drop it */
2748 lu_buf_free(ldata->ld_buf);
2750 /* The child object shouldn't be cached anymore */
2752 set_bit(LU_OBJECT_HEARD_BANSHEE,
2753 &child->mo_lu.lo_header->loh_flags);
2758 * Get locks on parents in proper order
2759 * RETURN: < 0 - error, rename_order if successful
2767 static int mdd_rename_order(const struct lu_env *env,
2768 struct mdd_device *mdd,
2769 struct mdd_object *src_pobj,
2770 const struct lu_attr *pattr,
2771 struct mdd_object *tgt_pobj)
2773 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2777 if (src_pobj == tgt_pobj)
2778 RETURN(MDD_RN_SAME);
2780 /* compared the parent child relationship of src_p&tgt_p */
2781 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2783 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2786 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2800 /* has not mdd_write{read}_lock on any obj yet. */
2801 static int mdd_rename_sanity_check(const struct lu_env *env,
2802 struct mdd_object *src_pobj,
2803 const struct lu_attr *pattr,
2804 struct mdd_object *tgt_pobj,
2805 const struct lu_attr *tpattr,
2806 struct mdd_object *sobj,
2807 const struct lu_attr *cattr,
2808 struct mdd_object *tobj,
2809 const struct lu_attr *tattr)
2814 /* XXX: when get here, sobj must NOT be NULL,
2815 * the other case has been processed in cld_rename
2816 * before mdd_rename and enable MDS_PERM_BYPASS. */
2820 * If we are using project inheritance, we only allow renames
2821 * into our tree when the project IDs are the same; otherwise
2822 * tree quota mechanism would be circumvented.
2824 if (((tpattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2825 tpattr->la_projid != cattr->la_projid) ||
2826 ((pattr->la_flags & LUSTRE_PROJINHERIT_FL) &&
2827 (pattr->la_projid != tpattr->la_projid)))
2830 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2834 /* XXX: when get here, "tobj == NULL" means tobj must
2835 * NOT exist (neither on remote MDS, such case has been
2836 * processed in cld_rename before mdd_rename and enable
2838 * So check may_create, but not check may_unlink. */
2840 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2841 (src_pobj != tgt_pobj));
2843 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2844 (src_pobj != tgt_pobj), 1);
2846 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2847 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2852 static int mdd_declare_rename(const struct lu_env *env,
2853 struct mdd_device *mdd,
2854 struct mdd_object *mdd_spobj,
2855 struct mdd_object *mdd_tpobj,
2856 struct mdd_object *mdd_sobj,
2857 struct mdd_object *mdd_tobj,
2858 const struct lu_name *sname,
2859 const struct lu_name *tname,
2861 struct linkea_data *ldata,
2862 struct thandle *handle)
2864 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2867 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2868 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2874 /* name from source dir */
2875 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2879 /* .. from source child */
2880 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2881 /* source child can be directory,
2882 * counted by source dir's nlink */
2883 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2886 if (mdd_spobj != mdd_tpobj) {
2887 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2892 rc = mdo_declare_index_insert(env, mdd_sobj,
2894 S_IFDIR, dotdot, handle);
2899 /* new target child can be directory,
2900 * counted by target dir's nlink */
2901 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2906 la->la_valid = LA_CTIME | LA_MTIME;
2907 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2911 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2915 la->la_valid = LA_CTIME;
2916 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2920 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2925 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2926 mdd_object_type(mdd_sobj),
2927 tname->ln_name, handle);
2931 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2932 /* delete target child in target parent directory */
2933 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name,
2938 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2942 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2943 /* target child can be directory,
2944 * delete "." reference in target child directory */
2945 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2949 /* delete ".." reference in target parent directory */
2950 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2955 la->la_valid = LA_CTIME;
2956 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2960 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2965 rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2972 /* src object can be remote that is why we use only fid and type of object */
2973 static int mdd_rename(const struct lu_env *env,
2974 struct md_object *src_pobj, struct md_object *tgt_pobj,
2975 const struct lu_fid *lf, const struct lu_name *lsname,
2976 struct md_object *tobj, const struct lu_name *ltname,
2979 const char *sname = lsname->ln_name;
2980 const char *tname = ltname->ln_name;
2981 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2982 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2983 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2984 struct mdd_device *mdd = mdo2mdd(src_pobj);
2985 struct mdd_object *mdd_sobj = NULL; /* source object */
2986 struct mdd_object *mdd_tobj = NULL;
2987 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2988 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2989 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2990 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2991 struct thandle *handle;
2992 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2993 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2994 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2997 bool tobj_locked = 0;
2998 unsigned cl_flags = 0;
3003 mdd_tobj = md2mdd_obj(tobj);
3005 mdd_sobj = mdd_object_find(env, mdd, lf);
3006 if (IS_ERR(mdd_sobj))
3007 RETURN(PTR_ERR(mdd_sobj));
3009 rc = mdd_la_get(env, mdd_sobj, cattr);
3011 GOTO(out_pending, rc);
3013 rc = mdd_la_get(env, mdd_spobj, pattr);
3015 GOTO(out_pending, rc);
3018 rc = mdd_la_get(env, mdd_tobj, tattr);
3020 GOTO(out_pending, rc);
3021 /* search for an existing archive.
3022 * we should check ahead as the object
3023 * can be destroyed in this transaction */
3024 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
3025 cl_flags |= CLF_RENAME_LAST_EXISTS;
3028 rc = mdd_la_get(env, mdd_tpobj, tpattr);
3030 GOTO(out_pending, rc);
3032 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
3033 mdd_sobj, cattr, mdd_tobj, tattr);
3035 GOTO(out_pending, rc);
3037 rc = mdd_name_check(mdd, ltname);
3039 GOTO(out_pending, rc);
3041 /* FIXME: Should consider tobj and sobj too in rename_lock. */
3042 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
3044 GOTO(out_pending, rc);
3046 handle = mdd_trans_create(env, mdd);
3048 GOTO(out_pending, rc = PTR_ERR(handle));
3050 memset(ldata, 0, sizeof(*ldata));
3051 rc = mdd_linkea_prepare(env, mdd_sobj, mdd_object_fid(mdd_spobj),
3052 lsname, mdd_object_fid(mdd_tpobj), ltname,
3057 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
3058 mdd_tobj, lsname, ltname, ma, ldata, handle);
3062 rc = mdd_trans_start(env, mdd, handle);
3066 is_dir = S_ISDIR(cattr->la_mode);
3068 /* Remove source name from source directory */
3069 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
3073 /* "mv dir1 dir2" needs "dir1/.." link update */
3074 if (is_dir && !lu_fid_eq(spobj_fid, tpobj_fid)) {
3075 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3077 GOTO(fixup_spobj2, rc);
3079 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
3082 GOTO(fixup_spobj, rc);
3085 if (mdd_tobj != NULL && mdd_object_exists(mdd_tobj)) {
3086 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3088 /* tname might been renamed to something else */
3089 GOTO(fixup_spobj, rc);
3092 /* Insert new fid with target name into target dir */
3093 rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
3096 GOTO(fixup_tpobj, rc);
3098 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3099 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
3101 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
3102 la->la_valid = LA_CTIME;
3103 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
3105 GOTO(fixup_tpobj, rc);
3107 /* Update the linkEA for the source object */
3108 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3109 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
3110 mdo2fid(mdd_tpobj), ltname, handle, ldata,
3113 /* Old files might not have EA entry */
3114 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
3115 lsname, handle, NULL, 0);
3116 mdd_write_unlock(env, mdd_sobj);
3117 /* We don't fail the transaction if the link ea can't be
3118 updated -- fid2path will use alternate lookup method. */
3121 /* Remove old target object
3122 * For tobj is remote case cmm layer has processed
3123 * and set tobj to NULL then. So when tobj is NOT NULL,
3124 * it must be local one.
3126 if (tobj && mdd_object_exists(mdd_tobj)) {
3127 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
3129 if (mdd_is_dead_obj(mdd_tobj)) {
3130 /* shld not be dead, something is wrong */
3131 CERROR("tobj is dead, something is wrong\n");
3135 mdo_ref_del(env, mdd_tobj, handle);
3137 /* Remove dot reference. */
3138 if (S_ISDIR(tattr->la_mode))
3139 mdo_ref_del(env, mdd_tobj, handle);
3142 /* fetch updated nlink */
3143 rc = mdd_la_get(env, mdd_tobj, tattr);
3145 CERROR("%s: Failed to get nlink for tobj "
3147 mdd2obd_dev(mdd)->obd_name,
3148 PFID(tpobj_fid), rc);
3149 GOTO(fixup_tpobj, rc);
3152 la->la_valid = LA_CTIME;
3153 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
3155 CERROR("%s: Failed to set ctime for tobj "
3157 mdd2obd_dev(mdd)->obd_name,
3158 PFID(tpobj_fid), rc);
3159 GOTO(fixup_tpobj, rc);
3162 /* XXX: this transfer to ma will be removed with LOD/OSP */
3163 ma->ma_attr = *tattr;
3164 ma->ma_valid |= MA_INODE;
3165 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
3168 CERROR("%s: Failed to unlink tobj "
3170 mdd2obd_dev(mdd)->obd_name,
3171 PFID(tpobj_fid), rc);
3172 GOTO(fixup_tpobj, rc);
3175 /* fetch updated nlink */
3176 rc = mdd_la_get(env, mdd_tobj, tattr);
3177 if (rc == -ENOENT) {
3178 /* the object got removed, let's
3179 * return the latest known attributes */
3180 tattr->la_nlink = 0;
3182 } else if (rc != 0) {
3183 CERROR("%s: Failed to get nlink for tobj "
3185 mdd2obd_dev(mdd)->obd_name,
3186 PFID(tpobj_fid), rc);
3187 GOTO(fixup_tpobj, rc);
3189 /* XXX: this transfer to ma will be removed with LOD/OSP */
3190 ma->ma_attr = *tattr;
3191 ma->ma_valid |= MA_INODE;
3193 if (tattr->la_nlink == 0)
3194 cl_flags |= CLF_RENAME_LAST;
3196 cl_flags &= ~CLF_RENAME_LAST_EXISTS;
3199 la->la_valid = LA_CTIME | LA_MTIME;
3200 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
3202 GOTO(fixup_tpobj, rc);
3204 if (mdd_spobj != mdd_tpobj) {
3205 la->la_valid = LA_CTIME | LA_MTIME;
3206 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
3208 GOTO(fixup_tpobj, rc);
3215 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
3217 CWARN("tp obj fix error %d\n",rc2);
3219 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
3220 !mdd_is_dead_obj(mdd_tobj)) {
3222 mdo_ref_add(env, mdd_tobj, handle);
3224 mdo_ref_add(env, mdd_tobj, handle);
3227 rc2 = __mdd_index_insert(env, mdd_tpobj,
3229 mdd_object_type(mdd_tobj),
3232 CWARN("tp obj fix error: rc = %d\n", rc2);
3237 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
3238 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
3240 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
3241 mdd2obd_dev(mdd)->obd_name, rc2);
3244 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
3247 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
3248 mdd2obd_dev(mdd)->obd_name, rc2);
3253 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
3254 mdd_object_type(mdd_sobj), sname,
3257 CWARN("sp obj fix error: rc = %d\n", rc2);
3262 mdd_write_unlock(env, mdd_tobj);
3265 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
3266 mdd_tobj, tpobj_fid, lf, spobj_fid,
3267 ltname, lsname, handle);
3270 rc = mdd_trans_stop(env, mdd, rc, handle);
3273 mdd_object_put(env, mdd_sobj);
3278 * During migration once the parent FID has been changed,
3279 * we need update the parent FID in linkea.
3281 static int mdd_linkea_update_child_internal(const struct lu_env *env,
3282 struct mdd_object *parent,
3283 struct mdd_object *newparent,
3284 struct mdd_object *child,
3285 const char *name, int namelen,
3286 struct thandle *handle,
3289 struct mdd_thread_info *info = mdd_env_info(env);
3290 struct linkea_data ldata = { NULL };
3291 struct lu_buf *buf = &info->mti_link_buf;
3297 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
3298 if (buf->lb_buf == NULL)
3302 rc = mdd_links_read(env, child, &ldata);
3304 if (rc == -ENOENT || rc == -ENODATA)
3309 LASSERT(ldata.ld_leh != NULL);
3310 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
3311 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
3312 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
3313 struct lu_name lname;
3316 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3319 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3320 !lu_fid_eq(&fid, mdd_object_fid(parent))) {
3321 ldata.ld_lee = (struct link_ea_entry *)
3322 ((char *)ldata.ld_lee +
3327 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3328 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3329 lname.ln_namelen, lname.ln_name,
3330 PFID(mdd_object_fid(newparent)));
3331 /* update to the new parent fid */
3332 linkea_entry_pack(ldata.ld_lee, &lname,
3333 mdd_object_fid(newparent));
3335 rc = mdd_declare_links_add(env, child, handle, &ldata);
3337 rc = mdd_links_write(env, child, &ldata, handle);
3343 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3344 struct mdd_object *parent,
3345 struct mdd_object *newparent,
3346 struct mdd_object *child,
3347 const char *name, int namelen,
3348 struct thandle *handle)
3350 return mdd_linkea_update_child_internal(env, parent, newparent,
3352 namelen, handle, true);
3355 static int mdd_linkea_update_child(const struct lu_env *env,
3356 struct mdd_object *parent,
3357 struct mdd_object *newparent,
3358 struct mdd_object *child,
3359 const char *name, int namelen,
3360 struct thandle *handle)
3362 return mdd_linkea_update_child_internal(env, parent, newparent,
3364 namelen, handle, false);
3367 static int mdd_update_linkea_internal(const struct lu_env *env,
3368 struct mdd_object *mdd_pobj,
3369 struct mdd_object *mdd_sobj,
3370 struct mdd_object *mdd_tobj,
3371 const struct lu_name *child_name,
3372 struct linkea_data *ldata,
3373 struct thandle *handle,
3376 struct mdd_thread_info *info = mdd_env_info(env);
3381 LASSERT(ldata->ld_buf != NULL);
3382 LASSERT(ldata->ld_leh != NULL);
3384 /* If it is mulitple links file, we need update the name entry for
3386 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3387 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3388 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3389 struct mdd_object *pobj;
3390 struct lu_name lname;
3393 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3395 pobj = mdd_object_find(env, mdd, &fid);
3397 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3398 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3403 if (!mdd_object_exists(pobj)) {
3404 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3405 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3409 if (pobj == mdd_pobj &&
3410 lname.ln_namelen == child_name->ln_namelen &&
3411 strncmp(lname.ln_name, child_name->ln_name,
3412 lname.ln_namelen) == 0) {
3413 CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3414 mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3419 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3420 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3421 PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3424 /* Remove source name from source directory */
3425 /* Insert new fid with target name into target dir */
3426 rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3431 rc = mdo_declare_index_insert(env, pobj,
3432 mdd_object_fid(mdd_tobj),
3433 mdd_object_type(mdd_tobj),
3434 lname.ln_name, handle);
3438 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3442 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3446 char *tmp_name = info->mti_key;
3448 if (lname.ln_namelen >= sizeof(info->mti_key)) {
3449 /* lnamelen is too big(> NAME_MAX + 16),
3450 * something wrong about this linkea, let's
3452 CWARN("%s: the name %.*s is too long under "
3453 DFID"\n", mdd2obd_dev(mdd)->obd_name,
3454 lname.ln_namelen, lname.ln_name,
3459 /* Note: lname might be without \0 at the end, see
3460 * linkea_entry_unpack(), let's add extra \0 by
3462 snprintf(tmp_name, sizeof(info->mti_key), "%.*s",
3463 lname.ln_namelen, lname.ln_name);
3464 lname.ln_name = tmp_name;
3466 /* Let's check if this linkEA still valid, before
3467 * it might be packed into the RPC buffer. */
3468 rc = mdd_lookup(env, &pobj->mod_obj, &lname,
3469 &info->mti_fid, NULL);
3470 if (rc < 0 || !lu_fid_eq(&info->mti_fid,
3471 mdd_object_fid(mdd_sobj)))
3472 GOTO(next_put, rc == -ENOENT ? 0 : rc);
3474 rc = __mdd_index_delete(env, pobj, tmp_name, 0, handle);
3478 rc = __mdd_index_insert(env, pobj,
3479 mdd_object_fid(mdd_tobj),
3480 mdd_object_type(mdd_tobj),
3485 mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3486 rc = mdo_ref_add(env, mdd_tobj, handle);
3487 mdd_write_unlock(env, mdd_tobj);
3491 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3492 mdo_ref_del(env, mdd_sobj, handle);
3493 mdd_write_unlock(env, mdd_sobj);
3496 mdd_object_put(env, pobj);
3500 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3507 static int mdd_migrate_xattrs(const struct lu_env *env,
3508 struct mdd_object *mdd_sobj,
3509 struct mdd_object *mdd_tobj)
3511 struct mdd_thread_info *info = mdd_env_info(env);
3512 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3514 struct thandle *handle;
3520 struct lu_buf list_xbuf;
3523 /* retrieve xattr list from the old object */
3524 list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3525 if (list_xsize == -ENODATA)
3531 lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3532 if (info->mti_big_buf.lb_buf == NULL)
3535 list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3536 list_xbuf.lb_len = list_xsize;
3537 rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3542 xname = list_xbuf.lb_buf;
3544 xlen = strnlen(xname, rem - 1) + 1;
3545 if (strcmp(XATTR_NAME_LMA, xname) == 0 ||
3546 strcmp(XATTR_NAME_LMV, xname) == 0)
3549 /* For directory, if there are default layout, migrate here */
3550 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3551 !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3554 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3555 if (xsize == -ENODATA)
3560 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3561 if (info->mti_link_buf.lb_buf == NULL)
3562 GOTO(out, rc = -ENOMEM);
3564 xbuf.lb_len = xsize;
3565 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3566 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3572 handle = mdd_trans_create(env, mdd);
3574 GOTO(out, rc = PTR_ERR(handle));
3576 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3579 GOTO(stop_trans, rc);
3580 /* Note: this transaction is part of migration, and it is not
3581 * the last step of migration, so we set th_local = 1 to avoid
3582 * update last rcvd for this transaction */
3583 handle->th_local = 1;
3584 rc = mdd_trans_start(env, mdd, handle);
3586 GOTO(stop_trans, rc);
3589 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3591 GOTO(stop_trans, rc = 0);
3593 if (unlikely(rc == -ENOSPC &&
3594 strcmp(xname, XATTR_NAME_LINK) == 0)) {
3595 rc = linkea_overflow_shrink(
3596 (struct linkea_data *)(xbuf.lb_buf));
3597 if (likely(rc > 0)) {
3604 GOTO(stop_trans, rc);
3606 rc = mdd_trans_stop(env, mdd, rc, handle);
3611 memmove(xname, xname + xlen, rem);
3617 static int mdd_declare_migrate_create(const struct lu_env *env,
3618 struct mdd_object *mdd_pobj,
3619 struct mdd_object *mdd_sobj,
3620 struct mdd_object *mdd_tobj,
3621 struct md_op_spec *spec,
3623 union lmv_mds_md *mgr_ea,
3624 struct linkea_data *ldata,
3625 struct thandle *handle)
3627 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3628 const struct lu_buf *buf;
3632 rc = mdd_declare_create_object_internal(env, mdd_pobj, mdd_tobj, la,
3633 handle, spec, NULL);
3637 rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3642 if (S_ISLNK(la->la_mode)) {
3643 const char *target_name = spec->u.sp_symname;
3644 int sym_len = strlen(target_name);
3645 const struct lu_buf *buf;
3647 buf = mdd_buf_get_const(env, target_name, sym_len);
3648 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3652 } else if (S_ISDIR(la->la_mode) && ldata != NULL) {
3653 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata);
3658 if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3659 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3660 spec->u.sp_ea.eadatalen);
3661 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3667 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3668 buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3669 rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3674 la_flag->la_valid = LA_FLAGS;
3675 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3676 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3681 static int mdd_migrate_create(const struct lu_env *env,
3682 struct mdd_object *mdd_pobj,
3683 struct mdd_object *mdd_sobj,
3684 struct mdd_object *mdd_tobj,
3685 const struct lu_name *lname,
3688 struct mdd_thread_info *info = mdd_env_info(env);
3689 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3690 struct md_op_spec *spec = &info->mti_spec;
3691 struct lu_buf lmm_buf = { NULL };
3692 struct lu_buf link_buf = { NULL };
3693 struct lu_buf mgr_buf;
3694 struct thandle *handle;
3695 struct lmv_mds_md_v1 *mgr_ea;
3696 struct lu_attr *la_flag = MDD_ENV_VAR(env, la_for_fix);
3697 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3699 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
3703 /* prepare spec for create */
3704 memset(spec, 0, sizeof(*spec));
3705 spec->sp_cr_lookup = 0;
3706 spec->sp_feat = &dt_directory_features;
3707 if (S_ISLNK(la->la_mode)) {
3708 const struct lu_buf *buf;
3710 buf = lu_buf_check_and_alloc(
3711 &mdd_env_info(env)->mti_big_buf,
3714 link_buf.lb_len = la->la_size + 1;
3715 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3716 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3718 rc = rc != 0 ? rc : -EFAULT;
3719 CERROR("%s: "DFID" readlink failed: rc = %d\n",
3720 mdd2obd_dev(mdd)->obd_name,
3721 PFID(mdd_object_fid(mdd_sobj)), rc);
3724 spec->u.sp_symname = link_buf.lb_buf;
3725 } else if (S_ISREG(la->la_mode)) {
3726 /* retrieve lov of the old object */
3727 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3728 if (rc != 0 && rc != -ENODATA)
3730 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3731 spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3732 spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3733 spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3735 } else if (S_ISDIR(la->la_mode)) {
3736 rc = mdd_links_read_with_rec(env, mdd_sobj, ldata);
3737 if (rc == -ENODATA) {
3738 /* ignore the non-linkEA error */
3746 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3747 lu_buf_check_and_alloc(&info->mti_xattr_buf, mgr_easize);
3748 mgr_buf.lb_buf = info->mti_xattr_buf.lb_buf;
3749 mgr_buf.lb_len = mgr_easize;
3750 mgr_ea = mgr_buf.lb_buf;
3751 memset(mgr_ea, 0, sizeof(*mgr_ea));
3752 mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3753 mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3754 mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3755 mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3756 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3757 fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3759 mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3761 handle = mdd_trans_create(env, mdd);
3763 GOTO(out_free, rc = PTR_ERR(handle));
3765 /* Note: this transaction is part of migration, and it is not
3766 * the last step of migration, so we set th_local = 1 to avoid
3767 * update last rcvd for this transaction */
3768 handle->th_local = 1;
3769 rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj, spec,
3770 la, mgr_buf.lb_buf, ldata, handle);
3772 GOTO(stop_trans, rc);
3774 rc = mdd_trans_start(env, mdd, handle);
3776 GOTO(stop_trans, rc);
3778 /* don't set nlink from the original object */
3779 la->la_valid &= ~LA_NLINK;
3781 /* create the target object */
3782 rc = mdd_create_object(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3785 GOTO(stop_trans, rc);
3787 if (S_ISDIR(la->la_mode) && ldata != NULL) {
3788 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3790 GOTO(stop_trans, rc);
3793 /* Set MIGRATE EA on the source inode, so once the migration needs
3794 * to be re-done during failover, the re-do process can locate the
3795 * target object which is already being created. */
3796 rc = mdo_xattr_set(env, mdd_sobj, &mgr_buf, XATTR_NAME_LMV, 0, handle);
3798 GOTO(stop_trans, rc);
3800 /* Set immutable flag, so any modification is disabled until
3801 * the migration is done. Once the migration is interrupted,
3802 * if the resume process find the migrating object has both
3803 * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3804 * flag and approve the migration */
3805 la_flag->la_valid = LA_FLAGS;
3806 la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3807 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3810 rc = mdd_trans_stop(env, mdd, rc, handle);
3812 if (lmm_buf.lb_buf != NULL)
3813 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3817 static int mdd_migrate_entries(const struct lu_env *env,
3818 struct mdd_object *mdd_sobj,
3819 struct mdd_object *mdd_tobj)
3821 struct dt_object *next = mdd_object_child(mdd_sobj);
3822 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3823 struct dt_object *dt_tobj = mdd_object_child(mdd_tobj);
3824 struct thandle *handle;
3826 const struct dt_it_ops *iops;
3828 struct lu_dirent *ent;
3832 OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3836 if (!dt_try_as_dir(env, next))
3837 GOTO(out_ent, rc = -ENOTDIR);
3839 * iterate directories
3841 iops = &next->do_index_ops->dio_it;
3842 it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3844 GOTO(out_ent, rc = PTR_ERR(it));
3846 rc = iops->load(env, it, 0);
3848 rc = iops->next(env, it);
3852 * At this point and across for-loop:
3854 * rc == 0 -> ok, proceed.
3855 * rc > 0 -> end of directory.
3859 struct mdd_object *child;
3860 char *name = mdd_env_info(env)->mti_key;
3863 bool target_exist = false;
3865 len = iops->key_size(env, it);
3869 result = iops->rec(env, it, (struct dt_rec *)ent,
3870 LUDA_FID | LUDA_TYPE);
3871 if (result == -ESTALE)
3878 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3880 /* Insert new fid with target name into target dir */
3881 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3882 (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3883 ent->lde_name[1] == '.'))
3886 child = mdd_object_find(env, mdd, &ent->lde_fid);
3888 GOTO(out, rc = PTR_ERR(child));
3890 /* child may not exist, but lu_object_attr will assert this,
3891 * get type from loh_attr directly */
3892 is_dir = S_ISDIR(child->mod_obj.mo_lu.lo_header->loh_attr);
3894 mdd_write_lock(env, child, MOR_SRC_CHILD);
3896 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3898 /* Check whether the name has been inserted to the target */
3899 if (dt_try_as_dir(env, dt_tobj)) {
3900 struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3902 rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3903 (struct dt_key *)name);
3904 if (unlikely(rc == 0))
3905 target_exist = true;
3908 handle = mdd_trans_create(env, mdd);
3910 GOTO(out_put, rc = PTR_ERR(handle));
3912 /* Note: this transaction is part of migration, and it is not
3913 * the last step of migration, so we set th_local = 1 to avoid
3914 * updating last rcvd for this transaction */
3915 handle->th_local = 1;
3916 if (likely(!target_exist)) {
3917 rc = mdo_declare_index_insert(env, mdd_tobj,
3919 child->mod_obj.mo_lu.lo_header->loh_attr,
3925 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3931 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3936 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3940 /* Update .. for child */
3941 rc = mdo_declare_index_delete(env, child, dotdot,
3946 rc = mdo_declare_index_insert(env, child,
3947 mdd_object_fid(mdd_tobj),
3948 S_IFDIR, dotdot, handle);
3953 rc = mdd_linkea_declare_update_child(env, mdd_sobj,mdd_tobj,
3960 rc = mdd_trans_start(env, mdd, handle);
3962 CERROR("%s: transaction start failed: rc = %d\n",
3963 mdd2obd_dev(mdd)->obd_name, rc);
3967 if (likely(!target_exist)) {
3968 rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3969 child->mod_obj.mo_lu.lo_header->loh_attr, name,
3975 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3980 rc = __mdd_index_delete_only(env, child, dotdot,
3985 rc = __mdd_index_insert_only(env, child,
3986 mdd_object_fid(mdd_tobj), S_IFDIR,
3992 rc = mdd_linkea_update_child(env, mdd_sobj, mdd_tobj,
3994 strlen(name), handle);
3997 mdd_write_unlock(env, child);
3998 mdd_object_put(env, child);
3999 rc = mdd_trans_stop(env, mdd, rc, handle);
4003 result = iops->next(env, it);
4004 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
4005 GOTO(out, rc = -EINTR);
4007 if (result == -ESTALE)
4009 } while (result == 0);
4012 iops->fini(env, it);
4014 OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
4018 static int mdd_declare_update_linkea(const struct lu_env *env,
4019 struct mdd_object *mdd_pobj,
4020 struct mdd_object *mdd_sobj,
4021 struct mdd_object *mdd_tobj,
4022 const struct lu_name *child_name,
4023 struct linkea_data *ldata,
4024 struct thandle *handle)
4026 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
4027 child_name, ldata, handle, 1);
4030 static int mdd_update_linkea(const struct lu_env *env,
4031 struct mdd_object *mdd_pobj,
4032 struct mdd_object *mdd_sobj,
4033 struct mdd_object *mdd_tobj,
4034 const struct lu_name *child_name,
4035 struct linkea_data *ldata,
4036 struct thandle *handle)
4038 return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
4039 child_name, ldata, handle, 0);
4042 static int mdd_declare_migrate_update_name(const struct lu_env *env,
4043 struct mdd_object *mdd_pobj,
4044 struct mdd_object *mdd_sobj,
4045 struct mdd_object *mdd_tobj,
4046 const struct lu_name *lname,
4048 struct lu_attr *parent_la,
4049 struct linkea_data *ldata,
4050 struct thandle *handle)
4052 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
4053 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
4056 /* Revert IMMUTABLE flag */
4057 la_flag->la_valid = LA_FLAGS;
4058 la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
4059 rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
4063 /* delete entry from source dir */
4064 rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
4068 if (ldata->ld_buf != NULL) {
4069 rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
4070 mdd_tobj, lname, ldata, handle);
4075 if (S_ISREG(mdd_object_type(mdd_sobj))) {
4076 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4081 handle->th_complex = 1;
4082 rc = mdo_declare_xattr_set(env, mdd_tobj, NULL,
4084 LU_XATTR_REPLACE, handle);
4089 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4090 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
4096 rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
4097 mdd_object_type(mdd_tobj),
4098 lname->ln_name, handle);
4102 rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL);
4106 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4107 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
4112 /* delete old object */
4113 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4117 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
4118 /* delete old object */
4119 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
4122 /* set nlink to 0 */
4123 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
4128 rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
4132 rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
4136 rc = mdd_declare_changelog_store(env, mdd, lname, NULL, handle);
4141 static int mdd_migrate_update_name(const struct lu_env *env,
4142 struct mdd_object *mdd_pobj,
4143 struct mdd_object *mdd_sobj,
4144 struct mdd_object *mdd_tobj,
4145 const struct lu_name *lname,
4148 struct lu_attr *p_la = MDD_ENV_VAR(env, la_for_fix);
4149 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4150 struct lu_attr *la_flag = MDD_ENV_VAR(env, tattr);
4151 struct mdd_device *mdd = mdo2mdd(&mdd_sobj->mod_obj);
4152 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
4153 struct thandle *handle;
4154 int is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
4155 const char *name = lname->ln_name;
4159 /* update time for parent */
4160 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
4161 p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
4162 p_la->la_valid = LA_CTIME;
4164 rc = mdd_la_get(env, mdd_sobj, so_attr);
4168 ldata->ld_buf = NULL;
4169 rc = mdd_links_read(env, mdd_sobj, ldata);
4170 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
4173 handle = mdd_trans_create(env, mdd);
4175 RETURN(PTR_ERR(handle));
4177 rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
4178 lname, so_attr, p_la, ldata,
4181 /* If the migration can not be fit in one transaction, just
4182 * leave it in the original MDT */
4184 GOTO(stop_trans, rc = 0);
4186 GOTO(stop_trans, rc);
4189 CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
4190 mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
4191 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4192 PFID(mdd_object_fid(mdd_tobj)));
4194 rc = mdd_trans_start(env, mdd, handle);
4196 GOTO(stop_trans, rc);
4198 /* Revert IMMUTABLE flag */
4199 la_flag->la_valid = LA_FLAGS;
4200 la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
4201 rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
4203 GOTO(stop_trans, rc);
4205 /* Remove source name from source directory */
4206 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
4208 GOTO(stop_trans, rc);
4210 if (ldata->ld_buf != NULL) {
4211 rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
4212 lname, ldata, handle);
4214 GOTO(stop_trans, rc);
4216 /* linkea update might decrease the source object
4217 * nlink, let's get the attr again after ref_del */
4218 rc = mdd_la_get(env, mdd_sobj, so_attr);
4220 GOTO(stop_trans, rc);
4223 if (S_ISREG(so_attr->la_mode)) {
4224 if (so_attr->la_nlink == 1) {
4225 rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
4227 if (rc != 0 && rc != -ENODATA)
4228 GOTO(stop_trans, rc);
4230 rc = mdo_xattr_set(env, mdd_tobj, NULL,
4232 LU_XATTR_REPLACE, handle);
4234 GOTO(stop_trans, rc);
4238 /* Insert new fid with target name into target dir */
4239 rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
4240 mdd_object_type(mdd_tobj), name, handle);
4242 GOTO(stop_trans, rc);
4244 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
4246 mdd_sobj->mod_flags |= DEAD_OBJ;
4247 rc = mdd_mark_orphan_object(env, mdd_sobj, handle, false);
4249 GOTO(out_unlock, rc);
4251 rc = __mdd_orphan_add(env, mdd_sobj, handle);
4253 GOTO(out_unlock, rc);
4255 mdo_ref_del(env, mdd_sobj, handle);
4257 mdo_ref_del(env, mdd_sobj, handle);
4259 /* Get the attr again after ref_del */
4260 rc = mdd_la_get(env, mdd_sobj, so_attr);
4262 GOTO(out_unlock, rc);
4264 ma->ma_attr = *so_attr;
4265 ma->ma_valid |= MA_INODE;
4267 rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
4269 GOTO(out_unlock, rc);
4271 rc = mdd_changelog_ns_store(env, mdd, CL_MIGRATE, 0, mdd_tobj,
4272 mdo2fid(mdd_pobj), mdo2fid(mdd_sobj),
4273 mdo2fid(mdd_pobj), lname, lname, handle);
4275 CWARN("%s: changelog for migrate %s "DFID
4276 "under "DFID" failed: rc = %d\n",
4277 mdd2obd_dev(mdd)->obd_name, lname->ln_name,
4278 PFID(mdd_object_fid(mdd_sobj)),
4279 PFID(mdd_object_fid(mdd_pobj)), rc);
4280 /* Sigh, there are no easy way to migrate back the object, so
4281 * let's reset the result to 0 for now XXX */
4285 mdd_write_unlock(env, mdd_sobj);
4288 rc = mdd_trans_stop(env, mdd, rc, handle);
4293 static int mdd_fld_lookup(const struct lu_env *env, struct mdd_device *mdd,
4294 const struct lu_fid *fid, __u32 *mdt_index)
4296 struct lu_seq_range *range = &mdd_env_info(env)->mti_range;
4297 struct seq_server_site *ss;
4300 ss = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site;
4302 range->lsr_flags = LU_SEQ_RANGE_MDT;
4303 rc = fld_server_lookup(env, ss->ss_server_fld, fid->f_seq, range);
4307 *mdt_index = range->lsr_index;
4312 * Check whether we should migrate the file/dir
4314 * < 0 permission check failed or other error.
4315 * = 0 the file can be migrated.
4316 * > 0 the file does not need to be migrated, mostly
4317 * for multiple link file
4319 static int mdd_migrate_sanity_check(const struct lu_env *env,
4320 struct mdd_object *pobj,
4321 const struct lu_attr *pattr,
4322 struct mdd_object *sobj,
4323 struct lu_attr *sattr)
4325 struct mdd_thread_info *info = mdd_env_info(env);
4326 struct linkea_data *ldata = &info->mti_link_data;
4327 struct mdd_device *mdd = mdo2mdd(&pobj->mod_obj);
4329 struct lu_buf *mgr_buf;
4335 mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
4336 mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
4337 if (mgr_buf->lb_buf == NULL)
4340 rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
4342 union lmv_mds_md *lmm = mgr_buf->lb_buf;
4344 /* If the object has migrateEA, it means IMMUTE flag
4345 * is being set by previous migration process, so it
4346 * needs to override the IMMUTE flag, otherwise the
4347 * following sanity check will fail */
4348 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
4349 LMV_HASH_FLAG_MIGRATION) {
4350 struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
4352 sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
4353 CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
4354 mdd2obd_dev(mdd)->obd_name,
4355 PFID(mdd_object_fid(sobj)));
4359 rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
4360 sobj, sattr, NULL, NULL);
4364 /* Then it will check if the file should be migrated. If the file
4365 * has mulitple links, we only need migrate the file if all of its
4366 * entries has been migrated to the remote MDT */
4367 if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
4370 rc = mdd_links_read(env, sobj, ldata);
4372 /* For multiple links files, if there are no linkEA data at all,
4373 * means the file might be created before linkEA is enabled, and
4374 * all of its links should not be migrated yet, otherwise it
4375 * should have some linkEA there */
4376 if (rc == -ENOENT || rc == -ENODATA)
4381 mdt_index = mdd->mdd_md_dev.md_lu_dev.ld_site->ld_seq_site->ss_node_id;
4382 /* If there are still links locally, then the file will not be
4384 LASSERT(ldata->ld_leh != NULL);
4386 /* If the linkEA is overflow, then means there are some unknown name
4387 * entries under unknown parents, that will prevent the migration. */
4388 if (unlikely(ldata->ld_leh->leh_overflow_time))
4391 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
4392 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
4393 struct lu_name lname;
4395 __u32 parent_mdt_index;
4397 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
4399 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
4402 rc = mdd_fld_lookup(env, mdd, &fid, &parent_mdt_index);
4406 /* Migrate the object only if none of its parents are on the
4408 if (parent_mdt_index != mdt_index)
4411 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
4412 PFID(mdd_object_fid(sobj)), lname.ln_namelen,
4413 lname.ln_name, PFID(&fid));
4421 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
4422 struct md_object *sobj, const struct lu_name *lname,
4423 struct md_object *tobj, struct md_attr *ma)
4425 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
4426 struct mdd_device *mdd = mdo2mdd(pobj);
4427 struct mdd_object *mdd_sobj = md2mdd_obj(sobj);
4428 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
4429 struct lu_attr *so_attr = MDD_ENV_VAR(env, cattr);
4430 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
4431 bool created = false;
4435 /* If the file will being migrated, it will check whether
4436 * the file is being opened by someone else right now */
4437 mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
4438 if (mdd_sobj->mod_count > 0) {
4440 "%s: "DFID"%s is already opened count %d: rc = %d\n",
4441 mdd2obd_dev(mdd)->obd_name,
4442 PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4443 mdd_sobj->mod_count, -EBUSY);
4444 mdd_read_unlock(env, mdd_sobj);
4445 GOTO(put, rc = -EBUSY);
4447 mdd_read_unlock(env, mdd_sobj);
4449 rc = mdd_la_get(env, mdd_sobj, so_attr);
4453 rc = mdd_la_get(env, mdd_pobj, pattr);
4457 rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4464 /* Sigh, it is impossible to finish all of migration in a single
4465 * transaction, for example migrating big directory entries to the
4466 * new MDT, it needs insert all of name entries of children in the
4469 * So migration will be done in multiple steps and transactions.
4471 * 1. create an orphan object on the remote MDT in one transaction.
4472 * 2. migrate extend attributes to the new target file/directory.
4473 * 3. For directory, migrate the entries to the new MDT and update
4474 * linkEA of each children. Because we can not migrate all entries
4475 * in a single transaction, so the migrating directory will become
4476 * a striped directory during migration, so once the process is
4477 * interrupted, the directory is still accessible. (During lookup,
4478 * client will locate the name by searching both original and target
4480 * 4. Finally, update the name/FID to point to the new file/directory
4481 * in a separate transaction.
4484 /* step 1: Check whether the orphan object has been created, and create
4485 * orphan object on the remote MDT if needed */
4486 if (!mdd_object_exists(mdd_tobj)) {
4487 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4494 LASSERT(mdd_object_exists(mdd_tobj));
4495 /* step 2: migrate xattr */
4496 rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4500 /* step 3: migrate name entries to the orphan object */
4501 if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4502 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4505 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4506 OBD_FAIL_MDS_REINT_NET_REP)))
4509 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4512 LASSERT(mdd_object_exists(mdd_tobj));
4513 /* step 4: update name entry to the new object */
4514 rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4519 /* newly created target was not locked, don't cache its attributes */
4521 mdd_invalidate(env, tobj);
4526 const struct md_dir_operations mdd_dir_ops = {
4527 .mdo_is_subdir = mdd_is_subdir,
4528 .mdo_lookup = mdd_lookup,
4529 .mdo_create = mdd_create,
4530 .mdo_rename = mdd_rename,
4531 .mdo_link = mdd_link,
4532 .mdo_unlink = mdd_unlink,
4533 .mdo_create_data = mdd_create_data,
4534 .mdo_migrate = mdd_migrate,