4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 /* Get FID from name and parent */
62 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
63 const struct lu_attr *pattr, const struct lu_name *lname,
64 struct lu_fid* fid, int mask)
66 const char *name = lname->ln_name;
67 const struct dt_key *key = (const struct dt_key *)name;
68 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
69 struct mdd_device *m = mdo2mdd(pobj);
70 struct dt_object *dir = mdd_object_child(mdd_obj);
74 if (unlikely(mdd_is_dead_obj(mdd_obj)))
77 if (mdd_object_remote(mdd_obj)) {
78 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
79 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
80 } else if (!mdd_object_exists(mdd_obj)) {
84 /* The common filename length check. */
85 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
86 RETURN(-ENAMETOOLONG);
88 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
93 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
94 dt_try_as_dir(env, dir))) {
96 rc = dir->do_index_ops->dio_lookup(env, dir,
97 (struct dt_rec *)fid, key,
98 mdd_object_capa(env, mdd_obj));
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
125 static inline int mdd_parent_fid(const struct lu_env *env,
126 struct mdd_object *obj,
127 const struct lu_attr *attr,
130 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
134 * For root fid use special function, which does not compare version component
135 * of fid. Version component is different for root fids on all MDTs.
137 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
139 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
140 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
144 * return 1: if lf is the fid of the ancestor of p1;
147 * return -EREMOTE: if remote object is found, in this
148 * case fid of remote object is saved to @pf;
150 * otherwise: values < 0, errors.
152 static int mdd_is_parent(const struct lu_env *env,
153 struct mdd_device *mdd,
154 struct mdd_object *p1,
155 const struct lu_attr *attr,
156 const struct lu_fid *lf,
159 struct mdd_object *parent = NULL;
164 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
165 pfid = &mdd_env_info(env)->mti_fid;
167 /* Check for root first. */
168 if (mdd_is_root(mdd, mdo2fid(p1)))
172 /* this is done recursively, bypass capa for each obj */
173 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
174 rc = mdd_parent_fid(env, p1, attr, pfid);
177 if (mdd_is_root(mdd, pfid))
179 if (lu_fid_eq(pfid, &mdd->mdd_local_root_fid))
181 if (lu_fid_eq(pfid, lf))
184 mdd_object_put(env, parent);
186 parent = mdd_object_find(env, mdd, pfid);
187 if (IS_ERR(parent)) {
188 GOTO(out, rc = PTR_ERR(parent));
189 } else if (mdd_object_remote(parent)) {
190 /*FIXME: Because of the restriction of rename in Phase I.
191 * If the parent is remote, we just assumed lf is not the
192 * parent of P1 for now */
199 if (parent && !IS_ERR(parent))
200 mdd_object_put(env, parent);
205 * No permission check is needed.
207 * returns 1: if fid is ancestor of @mo;
208 * returns 0: if fid is not a ancestor of @mo;
210 * returns EREMOTE if remote object is found, fid of remote object is saved to
213 * returns < 0: if error
215 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
216 const struct lu_fid *fid, struct lu_fid *sfid)
218 struct mdd_device *mdd = mdo2mdd(mo);
219 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
223 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
226 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
230 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
234 } else if (rc == 1) {
235 /* found @fid is parent */
243 * Check that @dir contains no entries except (possibly) dot and dotdot.
248 * -ENOTDIR not a directory object
249 * -ENOTEMPTY not empty
253 static int mdd_dir_is_empty(const struct lu_env *env,
254 struct mdd_object *dir)
257 struct dt_object *obj;
258 const struct dt_it_ops *iops;
262 obj = mdd_object_child(dir);
263 if (!dt_try_as_dir(env, obj))
266 iops = &obj->do_index_ops->dio_it;
267 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
269 result = iops->get(env, it, (const void *)"");
272 for (result = 0, i = 0; result == 0 && i < 3; ++i)
273 result = iops->next(env, it);
276 else if (result == +1)
278 } else if (result == 0)
280 * Huh? Index contains no zero key?
287 result = PTR_ERR(it);
291 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
292 const struct lu_attr *la)
294 struct mdd_device *m = mdd_obj2mdd_dev(obj);
299 if (!S_ISDIR(la->la_mode))
303 * Subdir count limitation can be broken through.
305 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
312 * Check whether it may create the cobj under the pobj.
315 int mdd_may_create(const struct lu_env *env,
316 struct mdd_object *pobj, const struct lu_attr *pattr,
317 struct mdd_object *cobj, int check_perm, int check_nlink)
322 if (cobj && mdd_object_exists(cobj))
325 if (mdd_is_dead_obj(pobj))
329 rc = mdd_permission_internal_locked(env, pobj, pattr,
330 MAY_WRITE | MAY_EXEC,
332 if (!rc && check_nlink)
333 rc = __mdd_may_link(env, pobj, pattr);
339 * Check whether can unlink from the pobj in the case of "cobj == NULL".
341 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
342 const struct lu_attr *pattr, const struct lu_attr *attr)
347 if (mdd_is_dead_obj(pobj))
350 if ((attr->la_valid & LA_FLAGS) &&
351 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
354 rc = mdd_permission_internal_locked(env, pobj, pattr,
355 MAY_WRITE | MAY_EXEC,
360 if (mdd_is_append(pobj))
367 * pobj == NULL is remote ops case, under such case, pobj's
368 * VTX feature has been checked already, no need check again.
370 static inline int mdd_is_sticky(const struct lu_env *env,
371 struct mdd_object *pobj,
372 const struct lu_attr *pattr,
373 struct mdd_object *cobj,
374 const struct lu_attr *cattr)
376 struct lu_ucred *uc = lu_ucred_assert(env);
379 LASSERT(pattr != NULL);
380 if (!(pattr->la_mode & S_ISVTX) ||
381 (pattr->la_uid == uc->uc_fsuid))
385 LASSERT(cattr != NULL);
386 if (cattr->la_uid == uc->uc_fsuid)
389 return !md_capable(uc, CFS_CAP_FOWNER);
392 static int mdd_may_delete_entry(const struct lu_env *env,
393 struct mdd_object *pobj,
394 const struct lu_attr *pattr,
399 LASSERT(pobj != NULL);
400 if (!mdd_object_exists(pobj))
403 if (mdd_is_dead_obj(pobj))
408 rc = mdd_permission_internal_locked(env, pobj, pattr,
409 MAY_WRITE | MAY_EXEC,
415 if (mdd_is_append(pobj))
422 * Check whether it may delete the cobj from the pobj.
425 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
426 const struct lu_attr *tpattr, struct mdd_object *tobj,
427 const struct lu_attr *tattr, const struct lu_attr *cattr,
428 int check_perm, int check_empty)
434 LASSERT(tpattr != NULL);
435 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
443 if (!mdd_object_exists(tobj))
446 if (mdd_is_dead_obj(tobj))
449 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
452 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
455 if ((tattr->la_valid & LA_FLAGS) &&
456 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
459 /* additional check the rename case */
461 if (S_ISDIR(cattr->la_mode)) {
462 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
464 if (!S_ISDIR(tattr->la_mode))
467 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
469 } else if (S_ISDIR(tattr->la_mode))
473 if (S_ISDIR(tattr->la_mode) && check_empty)
474 rc = mdd_dir_is_empty(env, tobj);
481 * has mdd_write_lock on src already, but not on tgt yet
483 static int mdd_link_sanity_check(const struct lu_env *env,
484 struct mdd_object *tgt_obj,
485 const struct lu_attr *tattr,
486 const struct lu_name *lname,
487 struct mdd_object *src_obj,
488 const struct lu_attr *cattr)
490 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
494 if (!mdd_object_exists(src_obj))
497 if (mdd_is_dead_obj(src_obj))
500 /* Local ops, no lookup before link, check filename length here. */
501 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
502 RETURN(-ENAMETOOLONG);
504 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
507 if (S_ISDIR(mdd_object_type(src_obj)))
510 LASSERT(src_obj != tgt_obj);
512 rc = mdd_may_create(env, tgt_obj, tattr, NULL, 1, 0);
517 rc = __mdd_may_link(env, src_obj, cattr);
522 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
523 const char *name, struct thandle *handle,
524 struct lustre_capa *capa)
526 struct dt_object *next = mdd_object_child(pobj);
530 if (dt_try_as_dir(env, next)) {
531 rc = next->do_index_ops->dio_delete(env, next,
532 (struct dt_key *)name,
540 static int __mdd_index_insert_only(const struct lu_env *env,
541 struct mdd_object *pobj,
542 const struct lu_fid *lf, const char *name,
543 struct thandle *handle,
544 struct lustre_capa *capa)
546 struct dt_object *next = mdd_object_child(pobj);
550 if (dt_try_as_dir(env, next)) {
551 struct lu_ucred *uc = lu_ucred_check(env);
554 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
555 rc = next->do_index_ops->dio_insert(env, next,
557 (const struct dt_key *)name,
558 handle, capa, ignore_quota);
565 /* insert named index, add reference if isdir */
566 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
567 const struct lu_fid *lf, const char *name, int is_dir,
568 struct thandle *handle, struct lustre_capa *capa)
573 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
574 if (rc == 0 && is_dir) {
575 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
576 mdo_ref_add(env, pobj, handle);
577 mdd_write_unlock(env, pobj);
582 /* delete named index, drop reference if isdir */
583 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
584 const char *name, int is_dir, struct thandle *handle,
585 struct lustre_capa *capa)
590 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
591 if (rc == 0 && is_dir) {
592 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
593 mdo_ref_del(env, pobj, handle);
594 mdd_write_unlock(env, pobj);
600 int mdd_declare_changelog_store(const struct lu_env *env,
601 struct mdd_device *mdd,
602 const struct lu_name *fname,
603 struct thandle *handle)
605 struct obd_device *obd = mdd2obd_dev(mdd);
606 struct llog_ctxt *ctxt;
607 struct llog_changelog_rec *rec;
613 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
616 reclen = llog_data_len(sizeof(*rec) +
617 (fname != NULL ? fname->ln_namelen : 0));
618 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
619 if (buf->lb_buf == NULL)
623 rec->cr_hdr.lrh_len = reclen;
624 rec->cr_hdr.lrh_type = CHANGELOG_REC;
626 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
630 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
636 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
637 struct mdd_device *mdd,
638 const struct lu_name *tname,
639 const struct lu_name *sname,
640 struct thandle *handle)
642 struct obd_device *obd = mdd2obd_dev(mdd);
643 struct llog_ctxt *ctxt;
644 struct llog_changelog_ext_rec *rec;
650 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
653 reclen = llog_data_len(sizeof(*rec) +
654 (tname != NULL ? tname->ln_namelen : 0) +
655 (sname != NULL ? 1 + sname->ln_namelen : 0));
656 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
657 if (buf->lb_buf == NULL)
661 rec->cr_hdr.lrh_len = reclen;
662 rec->cr_hdr.lrh_type = CHANGELOG_REC;
664 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
668 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
674 /** Add a changelog entry \a rec to the changelog llog
677 * \param handle - currently ignored since llogs start their own transaction;
678 * this will hopefully be fixed in llog rewrite
681 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
682 struct llog_changelog_rec *rec, struct thandle *th)
684 struct obd_device *obd = mdd2obd_dev(mdd);
685 struct llog_ctxt *ctxt;
688 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
689 rec->cr_hdr.lrh_type = CHANGELOG_REC;
690 rec->cr.cr_time = cl_time();
692 spin_lock(&mdd->mdd_cl.mc_lock);
693 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
694 * but as long as the MDD transactions are ordered correctly for e.g.
695 * rename conflicts, I don't think this should matter. */
696 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
697 spin_unlock(&mdd->mdd_cl.mc_lock);
699 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
703 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
710 /** Add a changelog_ext entry \a rec to the changelog llog
713 * \param handle - currently ignored since llogs start their own transaction;
714 * this will hopefully be fixed in llog rewrite
717 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
718 struct llog_changelog_ext_rec *rec,
721 struct obd_device *obd = mdd2obd_dev(mdd);
722 struct llog_ctxt *ctxt;
725 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
726 /* llog_lvfs_write_rec sets the llog tail len */
727 rec->cr_hdr.lrh_type = CHANGELOG_REC;
728 rec->cr.cr_time = cl_time();
730 spin_lock(&mdd->mdd_cl.mc_lock);
731 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
732 * but as long as the MDD transactions are ordered correctly for e.g.
733 * rename conflicts, I don't think this should matter. */
734 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
735 spin_unlock(&mdd->mdd_cl.mc_lock);
737 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
741 /* nested journal transaction */
742 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
750 /** Store a namespace change changelog record
751 * If this fails, we must fail the whole transaction; we don't
752 * want the change to commit without the log entry.
753 * \param target - mdd_object of change
754 * \param parent - parent dir/object
755 * \param tname - target name string
756 * \param handle - transacion handle
758 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
759 enum changelog_rec_type type, unsigned flags,
760 struct mdd_object *target, struct mdd_object *parent,
761 const struct lu_name *tname, struct thandle *handle)
763 struct llog_changelog_rec *rec;
770 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
772 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
775 LASSERT(target != NULL);
776 LASSERT(parent != NULL);
777 LASSERT(tname != NULL);
778 LASSERT(handle != NULL);
780 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
781 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
782 if (buf->lb_buf == NULL)
786 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
787 rec->cr.cr_type = (__u32)type;
788 rec->cr.cr_tfid = *mdo2fid(target);
789 rec->cr.cr_pfid = *mdo2fid(parent);
790 rec->cr.cr_namelen = tname->ln_namelen;
791 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
793 target->mod_cltime = cfs_time_current_64();
795 rc = mdd_changelog_store(env, mdd, rec, handle);
797 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
798 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
799 PFID(&rec->cr.cr_pfid));
807 /** Store a namespace change changelog record
808 * If this fails, we must fail the whole transaction; we don't
809 * want the change to commit without the log entry.
810 * \param target - mdd_object of change
811 * \param tpfid - target parent dir/object fid
812 * \param sfid - source object fid
813 * \param spfid - source parent fid
814 * \param tname - target name string
815 * \param sname - source name string
816 * \param handle - transacion handle
818 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
819 struct mdd_device *mdd,
820 enum changelog_rec_type type,
822 struct mdd_object *target,
823 const struct lu_fid *tpfid,
824 const struct lu_fid *sfid,
825 const struct lu_fid *spfid,
826 const struct lu_name *tname,
827 const struct lu_name *sname,
828 struct thandle *handle)
830 struct llog_changelog_ext_rec *rec;
837 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
839 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
842 LASSERT(sfid != NULL);
843 LASSERT(tpfid != NULL);
844 LASSERT(tname != NULL);
845 LASSERT(handle != NULL);
847 reclen = llog_data_len(sizeof(*rec) +
848 sname != NULL ? 1 + sname->ln_namelen : 0);
849 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
850 if (buf->lb_buf == NULL)
854 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
855 rec->cr.cr_type = (__u32)type;
856 rec->cr.cr_pfid = *tpfid;
857 rec->cr.cr_sfid = *sfid;
858 rec->cr.cr_spfid = *spfid;
859 rec->cr.cr_namelen = tname->ln_namelen;
860 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
862 rec->cr.cr_name[tname->ln_namelen] = '\0';
863 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
865 rec->cr.cr_namelen += 1 + sname->ln_namelen;
868 if (likely(target != NULL)) {
869 rec->cr.cr_tfid = *mdo2fid(target);
870 target->mod_cltime = cfs_time_current_64();
872 fid_zero(&rec->cr.cr_tfid);
875 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
877 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
878 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
885 static int __mdd_links_add(const struct lu_env *env,
886 struct mdd_object *mdd_obj,
887 struct linkea_data *ldata,
888 const struct lu_name *lname,
889 const struct lu_fid *pfid,
890 int first, int check)
894 if (ldata->ld_leh == NULL) {
895 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
899 rc = linkea_data_new(ldata,
900 &mdd_env_info(env)->mti_link_buf);
907 rc = linkea_links_find(ldata, lname, pfid);
908 if (rc && rc != -ENOENT)
914 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
915 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
919 linkea_add_buf(ldata, lname, tfid);
922 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
923 linkea_add_buf(ldata, lname, pfid);
925 return linkea_add_buf(ldata, lname, pfid);
928 static int __mdd_links_del(const struct lu_env *env,
929 struct mdd_object *mdd_obj,
930 struct linkea_data *ldata,
931 const struct lu_name *lname,
932 const struct lu_fid *pfid)
936 if (ldata->ld_leh == NULL) {
937 rc = mdd_links_read(env, mdd_obj, ldata);
942 rc = linkea_links_find(ldata, lname, pfid);
946 linkea_del_buf(ldata, lname);
950 static int mdd_linkea_prepare(const struct lu_env *env,
951 struct mdd_object *mdd_obj,
952 const struct lu_fid *oldpfid,
953 const struct lu_name *oldlname,
954 const struct lu_fid *newpfid,
955 const struct lu_name *newlname,
956 int first, int check,
957 struct linkea_data *ldata)
963 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
966 LASSERT(oldpfid != NULL || newpfid != NULL);
968 if (mdd_obj->mod_flags & DEAD_OBJ) {
969 /* Prevent linkea to be updated which is NOT necessary. */
970 ldata->ld_reclen = 0;
971 /* No more links, don't bother */
975 if (oldpfid != NULL) {
976 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
979 (rc != -ENODATA && rc != -ENOENT))
981 /* No changes done. */
986 /* If renaming, add the new record */
987 if (newpfid != NULL) {
988 /* even if the add fails, we still delete the out-of-date
990 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
994 rc = rc != 0 ? rc : rc2;
999 int mdd_links_rename(const struct lu_env *env,
1000 struct mdd_object *mdd_obj,
1001 const struct lu_fid *oldpfid,
1002 const struct lu_name *oldlname,
1003 const struct lu_fid *newpfid,
1004 const struct lu_name *newlname,
1005 struct thandle *handle,
1006 struct linkea_data *ldata,
1007 int first, int check)
1013 if (ldata == NULL) {
1014 ldata = &mdd_env_info(env)->mti_link_data;
1015 memset(ldata, 0, sizeof(*ldata));
1016 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1017 newpfid, newlname, first, check,
1023 if (ldata->ld_reclen != 0)
1024 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1031 if (rc == -EOVERFLOW || rc == -ENOSPC)
1033 if (oldpfid == NULL)
1034 CDEBUG(error ? D_ERROR : D_OTHER,
1035 "link_ea add '%.*s' failed %d "DFID"\n",
1036 newlname->ln_namelen, newlname->ln_name,
1037 rc, PFID(mdd_object_fid(mdd_obj)));
1038 else if (newpfid == NULL)
1039 CDEBUG(error ? D_ERROR : D_OTHER,
1040 "link_ea del '%.*s' failed %d "DFID"\n",
1041 oldlname->ln_namelen, oldlname->ln_name,
1042 rc, PFID(mdd_object_fid(mdd_obj)));
1044 CDEBUG(error ? D_ERROR : D_OTHER,
1045 "link_ea rename '%.*s'->'%.*s' failed %d "
1047 oldlname->ln_namelen, oldlname->ln_name,
1048 newlname->ln_namelen, newlname->ln_name,
1049 rc, PFID(mdd_object_fid(mdd_obj)));
1052 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1053 /* if we vmalloced a large buffer drop it */
1054 lu_buf_free(ldata->ld_buf);
1059 static inline int mdd_links_add(const struct lu_env *env,
1060 struct mdd_object *mdd_obj,
1061 const struct lu_fid *pfid,
1062 const struct lu_name *lname,
1063 struct thandle *handle,
1064 struct linkea_data *data, int first)
1066 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1067 pfid, lname, handle, data, first, 0);
1070 static inline int mdd_links_del(const struct lu_env *env,
1071 struct mdd_object *mdd_obj,
1072 const struct lu_fid *pfid,
1073 const struct lu_name *lname,
1074 struct thandle *handle)
1076 return mdd_links_rename(env, mdd_obj, pfid, lname,
1077 NULL, NULL, handle, NULL, 0, 0);
1080 /** Read the link EA into a temp buffer.
1081 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1082 * A pointer to the buffer is stored in \a ldata::ld_buf.
1084 * \retval 0 or error
1086 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1087 struct linkea_data *ldata)
1091 /* First try a small buf */
1092 LASSERT(env != NULL);
1093 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1095 if (ldata->ld_buf->lb_buf == NULL)
1098 if (!mdd_object_exists(mdd_obj))
1101 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1103 if (rc == -ERANGE) {
1104 /* Buf was too small, figure out what we need. */
1105 lu_buf_free(ldata->ld_buf);
1106 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1107 XATTR_NAME_LINK, BYPASS_CAPA);
1110 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1111 if (ldata->ld_buf->lb_buf == NULL)
1113 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1114 XATTR_NAME_LINK, BYPASS_CAPA);
1119 return linkea_init(ldata);
1122 /** Read the link EA into a temp buffer.
1123 * Uses the name_buf since it is generally large.
1124 * \retval IS_ERR err
1125 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1127 struct lu_buf *mdd_links_get(const struct lu_env *env,
1128 struct mdd_object *mdd_obj)
1130 struct linkea_data ldata = { 0 };
1133 rc = mdd_links_read(env, mdd_obj, &ldata);
1134 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1137 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1138 struct linkea_data *ldata, struct thandle *handle)
1140 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1141 ldata->ld_leh->leh_len);
1142 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1143 mdd_object_capa(env, mdd_obj));
1146 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1147 struct thandle *handle, struct linkea_data *ldata)
1153 if (ldata != NULL && ldata->ld_lee != NULL) {
1154 ea_len = ldata->ld_leh->leh_len;
1155 linkea = ldata->ld_buf->lb_buf;
1157 ea_len = DEFAULT_LINKEA_SIZE;
1161 /* XXX: max size? */
1162 rc = mdo_declare_xattr_set(env, mdd_obj,
1163 mdd_buf_get_const(env, linkea, ea_len),
1164 XATTR_NAME_LINK, 0, handle);
1168 static inline int mdd_declare_links_del(const struct lu_env *env,
1169 struct mdd_object *c,
1170 struct thandle *handle)
1174 /* For directory, the linkEA will be removed together
1175 * with the object. */
1176 if (!S_ISDIR(mdd_object_type(c)))
1177 rc = mdd_declare_links_add(env, c, handle, NULL);
1182 static int mdd_declare_link(const struct lu_env *env,
1183 struct mdd_device *mdd,
1184 struct mdd_object *p,
1185 struct mdd_object *c,
1186 const struct lu_name *name,
1187 struct thandle *handle,
1189 struct linkea_data *data)
1193 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1197 rc = mdo_declare_ref_add(env, c, handle);
1201 la->la_valid = LA_CTIME | LA_MTIME;
1202 rc = mdo_declare_attr_set(env, p, la, handle);
1206 la->la_valid = LA_CTIME;
1207 rc = mdo_declare_attr_set(env, c, la, handle);
1211 rc = mdd_declare_links_add(env, c, handle, data);
1215 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1220 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1221 struct md_object *src_obj, const struct lu_name *lname,
1224 const char *name = lname->ln_name;
1225 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1226 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1227 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1228 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1229 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1230 struct mdd_device *mdd = mdo2mdd(src_obj);
1231 struct thandle *handle;
1232 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1236 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1240 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1244 handle = mdd_trans_create(env, mdd);
1246 GOTO(out_pending, rc = PTR_ERR(handle));
1248 memset(ldata, 0, sizeof(*ldata));
1250 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1251 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1253 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1258 rc = mdd_trans_start(env, mdd, handle);
1262 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1263 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1266 GOTO(out_unlock, rc);
1268 rc = mdo_ref_add(env, mdd_sobj, handle);
1270 GOTO(out_unlock, rc);
1273 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1275 mdd_object_capa(env, mdd_tobj));
1277 mdo_ref_del(env, mdd_sobj, handle);
1278 GOTO(out_unlock, rc);
1281 la->la_valid = LA_CTIME | LA_MTIME;
1282 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1284 GOTO(out_unlock, rc);
1286 la->la_valid = LA_CTIME;
1287 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1289 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1290 mdo2fid(mdd_tobj), lname, 0, 0,
1293 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1294 lname, handle, ldata, 0);
1295 /* The failure of links_add should not cause the link
1296 * failure, reset rc here */
1301 mdd_write_unlock(env, mdd_sobj);
1303 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1304 mdd_tobj, lname, handle);
1306 mdd_trans_stop(env, mdd, rc, handle);
1308 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1309 /* if we vmalloced a large buffer drop it */
1310 lu_buf_free(ldata->ld_buf);
1315 int mdd_declare_finish_unlink(const struct lu_env *env,
1316 struct mdd_object *obj,
1318 struct thandle *handle)
1322 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1326 return mdo_declare_destroy(env, obj, handle);
1329 /* caller should take a lock before calling */
1330 int mdd_finish_unlink(const struct lu_env *env,
1331 struct mdd_object *obj, struct md_attr *ma,
1335 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1338 LASSERT(mdd_write_locked(env, obj) != 0);
1340 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1341 obj->mod_flags |= DEAD_OBJ;
1343 /* add new orphan and the object
1344 * will be deleted during mdd_close() */
1345 if (obj->mod_count) {
1346 rc = __mdd_orphan_add(env, obj, th);
1348 CDEBUG(D_HA, "Object "DFID" is inserted into "
1349 "orphan list, open count = %d\n",
1350 PFID(mdd_object_fid(obj)),
1353 CERROR("Object "DFID" fail to be an orphan, "
1354 "open count = %d, maybe cause failed "
1356 PFID(mdd_object_fid(obj)),
1359 rc = mdo_destroy(env, obj, th);
1368 * has mdd_write_lock on cobj already, but not on pobj yet
1370 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1371 const struct lu_attr *pattr,
1372 struct mdd_object *cobj,
1373 const struct lu_attr *cattr)
1378 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1383 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1384 struct mdd_object *p, struct mdd_object *c,
1385 const struct lu_name *name, struct md_attr *ma,
1386 struct thandle *handle, int no_name)
1388 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1391 if (likely(no_name == 0)) {
1392 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1397 rc = mdo_declare_ref_del(env, p, handle);
1401 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1402 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1403 la->la_valid = LA_CTIME | LA_MTIME;
1404 rc = mdo_declare_attr_set(env, p, la, handle);
1409 rc = mdo_declare_ref_del(env, c, handle);
1413 rc = mdo_declare_ref_del(env, c, handle);
1417 la->la_valid = LA_CTIME;
1418 rc = mdo_declare_attr_set(env, c, la, handle);
1422 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1426 rc = mdd_declare_links_del(env, c, handle);
1430 /* FIXME: need changelog for remove entry */
1431 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1438 * test if a file has an HSM archive
1439 * if HSM attributes are not found in ma update them from
1442 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1443 struct mdd_object *obj,
1448 if (!(ma->ma_valid & MA_HSM)) {
1449 /* no HSM MD provided, read xattr */
1450 struct lu_buf *hsm_buf;
1451 const size_t buflen = sizeof(struct hsm_attrs);
1454 hsm_buf = mdd_buf_get(env, NULL, 0);
1455 lu_buf_alloc(hsm_buf, buflen);
1456 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1457 mdd_object_capa(env, obj));
1458 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1459 lu_buf_free(hsm_buf);
1463 ma->ma_valid = MA_HSM;
1465 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1471 * Delete name entry and the object.
1472 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1473 * does not exist for this object, and it could only happen during resending
1474 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1475 * is also needed in this case(needed by changelog), so we have to add another
1476 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1477 * the ENOENT failure should be able to be fixed by redo mechanism.
1479 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1480 struct md_object *cobj, const struct lu_name *lname,
1481 struct md_attr *ma, int no_name)
1483 const char *name = lname->ln_name;
1484 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1485 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1486 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1487 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1488 struct mdd_object *mdd_cobj = NULL;
1489 struct mdd_device *mdd = mdo2mdd(pobj);
1490 struct thandle *handle;
1494 /* cobj == NULL means only delete name entry */
1495 if (likely(cobj != NULL)) {
1496 mdd_cobj = md2mdd_obj(cobj);
1497 if (mdd_object_exists(mdd_cobj) == 0)
1501 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1505 if (likely(mdd_cobj != NULL)) {
1507 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1511 is_dir = S_ISDIR(cattr->la_mode);
1514 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1518 handle = mdd_trans_create(env, mdd);
1520 RETURN(PTR_ERR(handle));
1522 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1523 lname, ma, handle, no_name);
1527 rc = mdd_trans_start(env, mdd, handle);
1531 if (likely(mdd_cobj != NULL))
1532 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1534 if (likely(no_name == 0)) {
1535 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1536 mdd_object_capa(env, mdd_pobj));
1541 if (likely(mdd_cobj != NULL)) {
1542 rc = mdo_ref_del(env, mdd_cobj, handle);
1544 __mdd_index_insert_only(env, mdd_pobj,
1547 mdd_object_capa(env, mdd_pobj));
1553 mdo_ref_del(env, mdd_cobj, handle);
1555 /* fetch updated nlink */
1556 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1561 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1562 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1564 la->la_valid = LA_CTIME | LA_MTIME;
1565 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1569 /* Enough for only unlink the entry */
1570 if (unlikely(mdd_cobj == NULL))
1573 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1574 /* update ctime of an unlinked file only if it is still
1575 * opened or a link still exists */
1576 la->la_valid = LA_CTIME;
1577 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1582 /* XXX: this transfer to ma will be removed with LOD/OSP */
1583 ma->ma_attr = *cattr;
1584 ma->ma_valid |= MA_INODE;
1585 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1587 /* fetch updated nlink */
1589 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1592 /* old files may not have link ea; ignore errors */
1593 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1595 /* if object is removed then we can't get its attrs, use last get */
1596 if (cattr->la_nlink == 0) {
1597 ma->ma_attr = *cattr;
1598 ma->ma_valid |= MA_INODE;
1602 mdd_write_unlock(env, mdd_cobj);
1606 if (cattr->la_nlink == 0) {
1607 cl_flags |= CLF_UNLINK_LAST;
1608 /* search for an existing archive */
1609 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1610 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1613 rc = mdd_changelog_ns_store(env, mdd,
1614 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1615 mdd_cobj, mdd_pobj, lname, handle);
1619 mdd_trans_stop(env, mdd, rc, handle);
1625 * The permission has been checked when obj created, no need check again.
1627 static int mdd_cd_sanity_check(const struct lu_env *env,
1628 struct mdd_object *obj)
1633 if (!obj || mdd_is_dead_obj(obj))
1639 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1640 struct md_object *cobj, const struct md_op_spec *spec,
1643 struct mdd_device *mdd = mdo2mdd(cobj);
1644 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1645 struct mdd_object *son = md2mdd_obj(cobj);
1646 struct thandle *handle;
1647 const struct lu_buf *buf;
1648 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1652 rc = mdd_cd_sanity_check(env, son);
1656 if (!md_should_create(spec->sp_cr_flags))
1660 * there are following use cases for this function:
1661 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1662 * striping can be specified or not
1665 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1669 /* calling ->ah_make_hint() is used to transfer information from parent */
1670 mdd_object_make_hint(env, mdd_pobj, son, attr, spec);
1672 handle = mdd_trans_create(env, mdd);
1674 GOTO(out_free, rc = PTR_ERR(handle));
1677 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1678 * Should this be fixed?
1680 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1681 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1682 spec->sp_cr_flags, spec->no_create);
1684 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1685 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1686 spec->u.sp_ea.eadatalen);
1691 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1692 XATTR_NAME_LOV, 0, handle);
1696 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1700 rc = mdd_trans_start(env, mdd, handle);
1704 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1705 0, handle, mdd_object_capa(env, son));
1710 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1713 mdd_trans_stop(env, mdd, rc, handle);
1718 static int mdd_declare_object_initialize(const struct lu_env *env,
1719 struct mdd_object *parent,
1720 struct mdd_object *child,
1721 struct lu_attr *attr,
1722 struct thandle *handle,
1723 struct linkea_data *ldata)
1729 * inode mode has been set in creation time, and it's based on umask,
1730 * la_mode and acl, don't set here again! (which will go wrong
1731 * because below function doesn't consider umask).
1732 * I'd suggest set all object attributes in creation time, see above.
1734 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1735 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1736 rc = mdo_declare_attr_set(env, child, attr, handle);
1737 attr->la_valid |= LA_MODE | LA_TYPE;
1738 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1739 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1742 rc = mdo_declare_ref_add(env, child, handle);
1744 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1749 mdd_declare_links_add(env, child, handle, ldata);
1754 static int mdd_object_initialize(const struct lu_env *env,
1755 const struct lu_fid *pfid,
1756 const struct lu_name *lname,
1757 struct mdd_object *child,
1758 struct lu_attr *attr, struct thandle *handle,
1759 const struct md_op_spec *spec,
1760 struct linkea_data *ldata)
1766 * Update attributes for child.
1769 * (1) the valid bits should be converted between Lustre and Linux;
1770 * (2) maybe, the child attributes should be set in OSD when creation.
1773 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1774 /* arguments are supposed to stay the same */
1775 if (S_ISDIR(attr->la_mode)) {
1776 /* Add "." and ".." for newly created dir */
1777 mdo_ref_add(env, child, handle);
1778 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1779 dot, handle, BYPASS_CAPA);
1781 rc = __mdd_index_insert_only(env, child, pfid,
1785 mdo_ref_del(env, child, handle);
1789 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1794 /* has not lock on pobj yet */
1795 static int mdd_create_sanity_check(const struct lu_env *env,
1796 struct md_object *pobj,
1797 const struct lu_attr *pattr,
1798 const struct lu_name *lname,
1799 struct lu_attr *cattr,
1800 struct md_op_spec *spec)
1802 struct mdd_thread_info *info = mdd_env_info(env);
1803 struct lu_fid *fid = &info->mti_fid;
1804 struct mdd_object *obj = md2mdd_obj(pobj);
1805 struct mdd_device *m = mdo2mdd(pobj);
1810 if (mdd_is_dead_obj(obj))
1814 * In some cases this lookup is not needed - we know before if name
1815 * exists or not because MDT performs lookup for it.
1816 * name length check is done in lookup.
1818 if (spec->sp_cr_lookup) {
1820 * Check if the name already exist, though it will be checked in
1821 * _index_insert also, for avoiding rolling back if exists
1824 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1825 MAY_WRITE | MAY_EXEC);
1827 RETURN(rc ? : -EEXIST);
1830 * Check WRITE permission for the parent.
1831 * EXEC permission have been checked
1832 * when lookup before create already.
1834 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1841 if (pattr->la_mode & S_ISGID) {
1842 cattr->la_gid = pattr->la_gid;
1843 if (S_ISDIR(cattr->la_mode)) {
1844 cattr->la_mode |= S_ISGID;
1845 cattr->la_valid |= LA_MODE;
1849 switch (cattr->la_mode & S_IFMT) {
1851 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1853 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1854 RETURN(-ENAMETOOLONG);
1873 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1874 struct mdd_object *p, struct mdd_object *c,
1875 const struct lu_name *name,
1876 struct lu_attr *attr,
1877 struct thandle *handle,
1878 const struct md_op_spec *spec,
1879 struct linkea_data *ldata,
1880 struct lu_buf *def_acl_buf,
1881 struct lu_buf *acl_buf)
1885 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1889 #ifdef CONFIG_FS_POSIX_ACL
1890 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1891 /* if dir, then can inherit default ACl */
1892 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1893 XATTR_NAME_ACL_DEFAULT,
1899 if (acl_buf->lb_len > 0) {
1900 rc = mdo_declare_attr_set(env, c, attr, handle);
1904 rc = mdo_declare_xattr_set(env, c, acl_buf,
1905 XATTR_NAME_ACL_ACCESS, 0, handle);
1911 if (S_ISDIR(attr->la_mode)) {
1912 rc = mdo_declare_ref_add(env, p, handle);
1917 rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1921 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1922 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1924 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1925 name->ln_name, handle);
1929 /* replay case, create LOV EA from client data */
1930 if (spec->no_create ||
1931 (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
1932 const struct lu_buf *buf;
1934 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1935 spec->u.sp_ea.eadatalen);
1936 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
1942 if (S_ISLNK(attr->la_mode)) {
1943 rc = dt_declare_record_write(env, mdd_object_child(c),
1944 strlen(spec->u.sp_symname), 0,
1950 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1951 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1954 la->la_valid = LA_CTIME | LA_MTIME;
1955 rc = mdo_declare_attr_set(env, p, la, handle);
1960 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1964 /* XXX: For remote create, it should indicate the remote RPC
1965 * will be sent after local transaction is finished, which
1966 * is not very nice, but it will be removed once we fully support
1968 if (mdd_object_remote(p) && handle->th_update != NULL)
1969 handle->th_update->tu_sent_after_local_trans = 1;
1974 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1975 struct lu_attr *la, struct lu_buf *def_acl_buf,
1976 struct lu_buf *acl_buf)
1981 if (S_ISLNK(la->la_mode)) {
1982 acl_buf->lb_len = 0;
1983 def_acl_buf->lb_len = 0;
1987 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1988 rc = mdo_xattr_get(env, pobj, def_acl_buf,
1989 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1990 mdd_read_unlock(env, pobj);
1992 /* If there are default ACL, fix mode/ACL by default ACL */
1993 def_acl_buf->lb_len = rc;
1994 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
1995 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
1996 acl_buf->lb_len = rc;
1997 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2000 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2001 /* If there are no default ACL, fix mode by mask */
2002 struct lu_ucred *uc = lu_ucred(env);
2004 /* The create triggered by MDT internal events, such as
2005 * LFSCK reset, will not contain valid "uc". */
2006 if (unlikely(uc != NULL))
2007 la->la_mode &= ~uc->uc_umask;
2009 acl_buf->lb_len = 0;
2010 def_acl_buf->lb_len = 0;
2017 * Create object and insert it into namespace.
2019 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2020 const struct lu_name *lname, struct md_object *child,
2021 struct md_op_spec *spec, struct md_attr* ma)
2023 struct mdd_thread_info *info = mdd_env_info(env);
2024 struct lu_attr *la = &info->mti_la_for_fix;
2025 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2026 struct mdd_object *son = md2mdd_obj(child);
2027 struct mdd_device *mdd = mdo2mdd(pobj);
2028 struct lu_attr *attr = &ma->ma_attr;
2029 struct thandle *handle;
2030 struct lu_attr *pattr = &info->mti_pattr;
2031 struct lu_buf acl_buf;
2032 struct lu_buf def_acl_buf;
2033 struct linkea_data *ldata = &info->mti_link_data;
2034 const char *name = lname->ln_name;
2035 int rc, created = 0, initialized = 0, inserted = 0;
2039 * Two operations have to be performed:
2041 * - an allocation of a new object (->do_create()), and
2043 * - an insertion into a parent index (->dio_insert()).
2045 * Due to locking, operation order is not important, when both are
2046 * successful, *but* error handling cases are quite different:
2048 * - if insertion is done first, and following object creation fails,
2049 * insertion has to be rolled back, but this operation might fail
2050 * also leaving us with dangling index entry.
2052 * - if creation is done first, is has to be undone if insertion
2053 * fails, leaving us with leaked space, which is neither good, nor
2056 * It seems that creation-first is simplest solution, but it is
2057 * sub-optimal in the frequent
2062 * case, because second mkdir is bound to create object, only to
2063 * destroy it immediately.
2065 * To avoid this follow local file systems that do double lookup:
2067 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2069 * 1. create (mdd_object_create_internal())
2071 * 2. insert (__mdd_index_insert(), lookup again)
2074 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2078 /* Sanity checks before big job. */
2079 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2083 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2084 GOTO(out_free, rc = -EINPROGRESS);
2086 acl_buf.lb_buf = info->mti_xattr_buf;
2087 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2088 def_acl_buf.lb_buf = info->mti_key;
2089 def_acl_buf.lb_len = sizeof(info->mti_key);
2090 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2094 mdd_object_make_hint(env, mdd_pobj, son, attr, spec);
2096 handle = mdd_trans_create(env, mdd);
2098 GOTO(out_free, rc = PTR_ERR(handle));
2100 memset(ldata, 0, sizeof(*ldata));
2101 mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2102 lname, 1, 0, ldata);
2104 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2105 handle, spec, ldata, &def_acl_buf, &acl_buf);
2109 rc = mdd_trans_start(env, mdd, handle);
2113 mdd_write_lock(env, son, MOR_TGT_CHILD);
2114 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
2116 mdd_write_unlock(env, son);
2122 #ifdef CONFIG_FS_POSIX_ACL
2123 if (def_acl_buf.lb_len > 0 && S_ISDIR(attr->la_mode)) {
2124 /* set default acl */
2125 rc = mdo_xattr_set(env, son, &def_acl_buf,
2126 XATTR_NAME_ACL_DEFAULT, 0,
2127 handle, BYPASS_CAPA);
2129 mdd_write_unlock(env, son);
2133 /* set its own acl */
2134 if (acl_buf.lb_len > 0) {
2135 rc = mdo_xattr_set(env, son, &acl_buf,
2136 XATTR_NAME_ACL_ACCESS,
2137 0, handle, BYPASS_CAPA);
2139 mdd_write_unlock(env, son);
2145 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2146 son, attr, handle, spec, ldata);
2149 * in case of replay we just set LOVEA provided by the client
2150 * XXX: I think it would be interesting to try "old" way where
2151 * MDT calls this xattr_set(LOV) in a different transaction.
2152 * probably this way we code can be made better.
2154 if (rc == 0 && (spec->no_create ||
2155 (spec->sp_cr_flags & MDS_OPEN_HAS_EA &&
2156 S_ISREG(attr->la_mode)))) {
2157 const struct lu_buf *buf;
2159 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2160 spec->u.sp_ea.eadatalen);
2161 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2165 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2166 rc = __mdd_orphan_add(env, son, handle);
2168 mdd_write_unlock(env, son);
2172 * Object has no links, so it will be destroyed when last
2173 * reference is released. (XXX not now.)
2179 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
2180 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2181 name, S_ISDIR(attr->la_mode), handle,
2182 mdd_object_capa(env, mdd_pobj));
2189 if (S_ISLNK(attr->la_mode)) {
2190 struct lu_ucred *uc = lu_ucred_assert(env);
2191 struct dt_object *dt = mdd_object_child(son);
2192 const char *target_name = spec->u.sp_symname;
2193 int sym_len = strlen(target_name);
2194 const struct lu_buf *buf;
2197 buf = mdd_buf_get_const(env, target_name, sym_len);
2198 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2199 mdd_object_capa(env, son),
2201 CFS_CAP_SYS_RESOURCE_MASK);
2206 GOTO(cleanup, rc = -EFAULT);
2209 /* volatile file creation does not update parent directory times */
2210 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2211 GOTO(cleanup, rc = 0);
2213 /* update parent directory mtime/ctime */
2215 la->la_valid = LA_CTIME | LA_MTIME;
2216 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2222 if (rc != 0 && created != 0) {
2225 if (inserted != 0) {
2226 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2227 rc2 = __mdd_orphan_del(env, son, handle);
2229 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2230 S_ISDIR(attr->la_mode),
2231 handle, BYPASS_CAPA);
2236 mdd_write_lock(env, son, MOR_TGT_CHILD);
2237 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2238 /* Drop the reference, no need to delete "."/"..",
2239 * because the object to be destroied directly. */
2240 rc2 = mdo_ref_del(env, son, handle);
2242 mdd_write_unlock(env, son);
2247 rc2 = mdo_ref_del(env, son, handle);
2249 mdd_write_unlock(env, son);
2253 mdo_destroy(env, son, handle);
2254 mdd_write_unlock(env, son);
2257 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2258 rc = mdd_changelog_ns_store(env, mdd,
2259 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2260 S_ISREG(attr->la_mode) ? CL_CREATE :
2261 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2262 0, son, mdd_pobj, lname, handle);
2264 mdd_trans_stop(env, mdd, rc, handle);
2266 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2267 /* if we vmalloced a large buffer drop it */
2268 lu_buf_free(ldata->ld_buf);
2270 /* The child object shouldn't be cached anymore */
2272 set_bit(LU_OBJECT_HEARD_BANSHEE,
2273 &child->mo_lu.lo_header->loh_flags);
2278 * Get locks on parents in proper order
2279 * RETURN: < 0 - error, rename_order if successful
2287 static int mdd_rename_order(const struct lu_env *env,
2288 struct mdd_device *mdd,
2289 struct mdd_object *src_pobj,
2290 const struct lu_attr *pattr,
2291 struct mdd_object *tgt_pobj)
2293 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2297 if (src_pobj == tgt_pobj)
2298 RETURN(MDD_RN_SAME);
2300 /* compared the parent child relationship of src_p&tgt_p */
2301 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2303 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2306 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2320 /* has not mdd_write{read}_lock on any obj yet. */
2321 static int mdd_rename_sanity_check(const struct lu_env *env,
2322 struct mdd_object *src_pobj,
2323 const struct lu_attr *pattr,
2324 struct mdd_object *tgt_pobj,
2325 const struct lu_attr *tpattr,
2326 struct mdd_object *sobj,
2327 const struct lu_attr *cattr,
2328 struct mdd_object *tobj,
2329 const struct lu_attr *tattr)
2334 /* XXX: when get here, sobj must NOT be NULL,
2335 * the other case has been processed in cld_rename
2336 * before mdd_rename and enable MDS_PERM_BYPASS. */
2339 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2343 /* XXX: when get here, "tobj == NULL" means tobj must
2344 * NOT exist (neither on remote MDS, such case has been
2345 * processed in cld_rename before mdd_rename and enable
2347 * So check may_create, but not check may_unlink. */
2349 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2350 (src_pobj != tgt_pobj), 0);
2352 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2353 (src_pobj != tgt_pobj), 1);
2355 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2356 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2361 static int mdd_declare_rename(const struct lu_env *env,
2362 struct mdd_device *mdd,
2363 struct mdd_object *mdd_spobj,
2364 struct mdd_object *mdd_tpobj,
2365 struct mdd_object *mdd_sobj,
2366 struct mdd_object *mdd_tobj,
2367 const struct lu_name *tname,
2368 const struct lu_name *sname,
2370 struct linkea_data *ldata,
2371 struct thandle *handle)
2373 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2376 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2377 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2383 /* name from source dir */
2384 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2388 /* .. from source child */
2389 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2390 /* source child can be directory,
2391 * counted by source dir's nlink */
2392 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2395 if (mdd_spobj != mdd_tpobj) {
2396 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2401 rc = mdo_declare_index_insert(env, mdd_sobj,
2407 /* new target child can be directory,
2408 * counted by target dir's nlink */
2409 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2414 la->la_valid = LA_CTIME | LA_MTIME;
2415 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2419 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2423 la->la_valid = LA_CTIME;
2424 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2428 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2433 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2434 tname->ln_name, handle);
2438 /* name from target dir (old name), we declare it unconditionally
2439 * as mdd_rename() calls delete unconditionally as well. so just
2440 * to balance declarations vs calls to change ... */
2441 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2445 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2446 /* delete target child in target parent directory */
2447 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2451 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2452 /* target child can be directory,
2453 * delete "." reference in target child directory */
2454 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2458 /* delete ".." reference in target parent directory */
2459 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2464 la->la_valid = LA_CTIME;
2465 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2469 rc = mdd_declare_links_del(env, mdd_tobj, handle);
2473 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2478 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2485 /* src object can be remote that is why we use only fid and type of object */
2486 static int mdd_rename(const struct lu_env *env,
2487 struct md_object *src_pobj, struct md_object *tgt_pobj,
2488 const struct lu_fid *lf, const struct lu_name *lsname,
2489 struct md_object *tobj, const struct lu_name *ltname,
2492 const char *sname = lsname->ln_name;
2493 const char *tname = ltname->ln_name;
2494 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2495 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2496 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2497 struct mdd_device *mdd = mdo2mdd(src_pobj);
2498 struct mdd_object *mdd_sobj = NULL; /* source object */
2499 struct mdd_object *mdd_tobj = NULL;
2500 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2501 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2502 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2503 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2504 struct thandle *handle;
2505 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2506 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2507 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2510 bool tobj_locked = 0;
2511 unsigned cl_flags = 0;
2516 mdd_tobj = md2mdd_obj(tobj);
2518 mdd_sobj = mdd_object_find(env, mdd, lf);
2520 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2522 GOTO(out_pending, rc);
2524 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2526 GOTO(out_pending, rc);
2529 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2531 GOTO(out_pending, rc);
2534 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2536 GOTO(out_pending, rc);
2538 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2539 mdd_sobj, cattr, mdd_tobj, tattr);
2541 GOTO(out_pending, rc);
2543 handle = mdd_trans_create(env, mdd);
2545 GOTO(out_pending, rc = PTR_ERR(handle));
2547 memset(ldata, 0, sizeof(*ldata));
2548 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2549 ltname, 1, 0, ldata);
2550 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2551 mdd_tobj, lsname, ltname, ma, ldata, handle);
2555 rc = mdd_trans_start(env, mdd, handle);
2559 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2560 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2562 GOTO(cleanup_unlocked, rc);
2564 is_dir = S_ISDIR(cattr->la_mode);
2566 /* Remove source name from source directory */
2567 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2568 mdd_object_capa(env, mdd_spobj));
2572 /* "mv dir1 dir2" needs "dir1/.." link update */
2573 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2574 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2575 mdd_object_capa(env, mdd_sobj));
2577 GOTO(fixup_spobj2, rc);
2579 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2580 handle, mdd_object_capa(env, mdd_sobj));
2582 GOTO(fixup_spobj, rc);
2585 /* Remove target name from target directory
2586 * Here tobj can be remote one, so we do index_delete unconditionally
2587 * and -ENOENT is allowed.
2589 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2590 mdd_object_capa(env, mdd_tpobj));
2593 /* tname might been renamed to something else */
2594 GOTO(fixup_spobj, rc);
2597 GOTO(fixup_spobj, rc);
2600 /* Insert new fid with target name into target dir */
2601 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2602 mdd_object_capa(env, mdd_tpobj));
2604 GOTO(fixup_tpobj, rc);
2606 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2607 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2609 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2611 la->la_valid = LA_CTIME;
2612 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2614 GOTO(fixup_tpobj, rc);
2617 /* Remove old target object
2618 * For tobj is remote case cmm layer has processed
2619 * and set tobj to NULL then. So when tobj is NOT NULL,
2620 * it must be local one.
2622 if (tobj && mdd_object_exists(mdd_tobj)) {
2623 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2625 if (mdd_is_dead_obj(mdd_tobj)) {
2626 /* shld not be dead, something is wrong */
2627 CERROR("tobj is dead, something is wrong\n");
2631 mdo_ref_del(env, mdd_tobj, handle);
2633 /* Remove dot reference. */
2634 if (S_ISDIR(tattr->la_mode))
2635 mdo_ref_del(env, mdd_tobj, handle);
2638 /* fetch updated nlink */
2639 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2641 CERROR("%s: Failed to get nlink for tobj "
2643 mdd2obd_dev(mdd)->obd_name,
2644 PFID(tpobj_fid), rc);
2645 GOTO(fixup_tpobj, rc);
2648 la->la_valid = LA_CTIME;
2649 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2651 CERROR("%s: Failed to set ctime for tobj "
2653 mdd2obd_dev(mdd)->obd_name,
2654 PFID(tpobj_fid), rc);
2655 GOTO(fixup_tpobj, rc);
2658 /* XXX: this transfer to ma will be removed with LOD/OSP */
2659 ma->ma_attr = *tattr;
2660 ma->ma_valid |= MA_INODE;
2661 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2663 CERROR("%s: Failed to unlink tobj "
2665 mdd2obd_dev(mdd)->obd_name,
2666 PFID(tpobj_fid), rc);
2667 GOTO(fixup_tpobj, rc);
2670 /* fetch updated nlink */
2671 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2673 CERROR("%s: Failed to get nlink for tobj "
2675 mdd2obd_dev(mdd)->obd_name,
2676 PFID(tpobj_fid), rc);
2677 GOTO(fixup_tpobj, rc);
2679 /* XXX: this transfer to ma will be removed with LOD/OSP */
2680 ma->ma_attr = *tattr;
2681 ma->ma_valid |= MA_INODE;
2683 if (tattr->la_nlink == 0) {
2684 cl_flags |= CLF_RENAME_LAST;
2685 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2686 cl_flags |= CLF_RENAME_LAST_EXISTS;
2690 la->la_valid = LA_CTIME | LA_MTIME;
2691 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2693 GOTO(fixup_tpobj, rc);
2695 if (mdd_spobj != mdd_tpobj) {
2696 la->la_valid = LA_CTIME | LA_MTIME;
2697 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2700 if (rc == 0 && mdd_sobj) {
2701 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2702 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2703 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2706 /* Old files might not have EA entry */
2707 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2708 lsname, handle, NULL, 0);
2709 mdd_write_unlock(env, mdd_sobj);
2710 /* We don't fail the transaction if the link ea can't be
2711 updated -- fid2path will use alternate lookup method. */
2719 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2722 CWARN("tp obj fix error %d\n",rc2);
2724 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2725 !mdd_is_dead_obj(mdd_tobj)) {
2727 mdo_ref_add(env, mdd_tobj, handle);
2729 mdo_ref_add(env, mdd_tobj, handle);
2732 rc2 = __mdd_index_insert(env, mdd_tpobj,
2733 mdo2fid(mdd_tobj), tname,
2738 CWARN("tp obj fix error %d\n",rc2);
2743 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2744 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2748 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2749 mdd2obd_dev(mdd)->obd_name, rc2);
2752 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2753 dotdot, handle, BYPASS_CAPA);
2755 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2756 mdd2obd_dev(mdd)->obd_name, rc2);
2761 rc2 = __mdd_index_insert(env, mdd_spobj,
2762 lf, sname, is_dir, handle, BYPASS_CAPA);
2764 CWARN("sp obj fix error %d\n",rc2);
2768 mdd_write_unlock(env, mdd_tobj);
2771 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2772 mdd_tobj, tpobj_fid, lf,
2773 spobj_fid, ltname, lsname,
2777 mdd_trans_stop(env, mdd, rc, handle);
2779 mdd_object_put(env, mdd_sobj);
2783 const struct md_dir_operations mdd_dir_ops = {
2784 .mdo_is_subdir = mdd_is_subdir,
2785 .mdo_lookup = mdd_lookup,
2786 .mdo_create = mdd_create,
2787 .mdo_rename = mdd_rename,
2788 .mdo_link = mdd_link,
2789 .mdo_unlink = mdd_unlink,
2790 .mdo_create_data = mdd_create_data,