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 static int __mdd_lookup(const struct lu_env *, struct md_object *,
61 const struct lu_name *, struct lu_fid*, int);
64 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
65 const struct lu_name *lname, struct lu_fid* fid, int mask)
67 const char *name = lname->ln_name;
68 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
69 struct dynlock_handle *dlh;
72 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
73 if (unlikely(dlh == NULL))
75 rc = __mdd_lookup(env, pobj, lname, fid, mask);
76 mdd_pdo_read_unlock(env, mdd_obj, dlh);
81 int mdd_lookup(const struct lu_env *env,
82 struct md_object *pobj, const struct lu_name *lname,
83 struct lu_fid* fid, struct md_op_spec *spec)
87 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
91 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
94 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
98 * For root fid use special function, which does not compare version component
99 * of fid. Version component is different for root fids on all MDTs.
101 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
103 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
104 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
108 * return 1: if lf is the fid of the ancestor of p1;
111 * return -EREMOTE: if remote object is found, in this
112 * case fid of remote object is saved to @pf;
114 * otherwise: values < 0, errors.
116 static int mdd_is_parent(const struct lu_env *env,
117 struct mdd_device *mdd,
118 struct mdd_object *p1,
119 const struct lu_fid *lf,
122 struct mdd_object *parent = NULL;
127 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
128 pfid = &mdd_env_info(env)->mti_fid;
130 /* Check for root first. */
131 if (mdd_is_root(mdd, mdo2fid(p1)))
135 /* this is done recursively, bypass capa for each obj */
136 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
137 rc = mdd_parent_fid(env, p1, pfid);
140 if (mdd_is_root(mdd, pfid))
142 if (lu_fid_eq(pfid, lf))
145 mdd_object_put(env, parent);
147 parent = mdd_object_find(env, mdd, pfid);
148 if (IS_ERR(parent)) {
149 GOTO(out, rc = PTR_ERR(parent));
150 } else if (mdd_object_remote(parent)) {
151 /*FIXME: Because of the restriction of rename in Phase I.
152 * If the parent is remote, we just assumed lf is not the
153 * parent of P1 for now */
160 if (parent && !IS_ERR(parent))
161 mdd_object_put(env, parent);
166 * No permission check is needed.
168 * returns 1: if fid is ancestor of @mo;
169 * returns 0: if fid is not a ancestor of @mo;
171 * returns EREMOTE if remote object is found, fid of remote object is saved to
174 * returns < 0: if error
176 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
177 const struct lu_fid *fid, struct lu_fid *sfid)
179 struct mdd_device *mdd = mdo2mdd(mo);
183 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
186 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
190 } else if (rc == 1) {
191 /* found @fid is parent */
199 * Check that @dir contains no entries except (possibly) dot and dotdot.
204 * -ENOTDIR not a directory object
205 * -ENOTEMPTY not empty
209 static int mdd_dir_is_empty(const struct lu_env *env,
210 struct mdd_object *dir)
213 struct dt_object *obj;
214 const struct dt_it_ops *iops;
218 obj = mdd_object_child(dir);
219 if (!dt_try_as_dir(env, obj))
222 iops = &obj->do_index_ops->dio_it;
223 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
225 result = iops->get(env, it, (const void *)"");
228 for (result = 0, i = 0; result == 0 && i < 3; ++i)
229 result = iops->next(env, it);
232 else if (result == +1)
234 } else if (result == 0)
236 * Huh? Index contains no zero key?
243 result = PTR_ERR(it);
247 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
249 struct mdd_device *m = mdd_obj2mdd_dev(obj);
250 struct lu_attr *la = &mdd_env_info(env)->mti_la;
254 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
259 * Subdir count limitation can be broken through.
261 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
262 !S_ISDIR(la->la_mode))
269 * Check whether it may create the cobj under the pobj.
272 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
273 struct mdd_object *cobj, int check_perm, int check_nlink)
278 if (cobj && mdd_object_exists(cobj))
281 if (mdd_is_dead_obj(pobj))
285 rc = mdd_permission_internal_locked(env, pobj, NULL,
286 MAY_WRITE | MAY_EXEC,
288 if (!rc && check_nlink)
289 rc = __mdd_may_link(env, pobj);
295 * Check whether can unlink from the pobj in the case of "cobj == NULL".
297 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
298 const struct lu_attr *attr)
303 if (mdd_is_dead_obj(pobj))
306 if ((attr->la_valid & LA_FLAGS) &&
307 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
310 rc = mdd_permission_internal_locked(env, pobj, NULL,
311 MAY_WRITE | MAY_EXEC,
316 if (mdd_is_append(pobj))
323 * pobj == NULL is remote ops case, under such case, pobj's
324 * VTX feature has been checked already, no need check again.
326 static inline int mdd_is_sticky(const struct lu_env *env,
327 struct mdd_object *pobj,
328 struct mdd_object *cobj)
330 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
331 struct lu_ucred *uc = lu_ucred_assert(env);
335 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
339 if (!(tmp_la->la_mode & S_ISVTX) ||
340 (tmp_la->la_uid == uc->uc_fsuid))
344 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
348 if (tmp_la->la_uid == uc->uc_fsuid)
351 return !md_capable(uc, CFS_CAP_FOWNER);
354 static int mdd_may_delete_entry(const struct lu_env *env,
355 struct mdd_object *pobj, int check_perm)
359 LASSERT(pobj != NULL);
360 if (!mdd_object_exists(pobj))
363 if (mdd_is_dead_obj(pobj))
368 rc = mdd_permission_internal_locked(env, pobj, NULL,
369 MAY_WRITE | MAY_EXEC,
375 if (mdd_is_append(pobj))
382 * Check whether it may delete the cobj from the pobj.
385 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
386 struct mdd_object *cobj, struct lu_attr *cattr,
387 struct lu_attr *src_attr, int check_perm, int check_empty)
393 rc = mdd_may_delete_entry(env, pobj, check_perm);
401 if (!mdd_object_exists(cobj))
404 if (mdd_is_dead_obj(cobj))
408 if (mdd_is_sticky(env, pobj, cobj))
411 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
414 if ((cattr->la_valid & LA_FLAGS) &&
415 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
418 /* additional check the rename case */
420 if (S_ISDIR(src_attr->la_mode)) {
421 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
423 if (!S_ISDIR(cattr->la_mode))
426 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
428 } else if (S_ISDIR(cattr->la_mode))
432 if (S_ISDIR(cattr->la_mode) && check_empty)
433 rc = mdd_dir_is_empty(env, cobj);
440 * has mdd_write_lock on src already, but not on tgt yet
442 int mdd_link_sanity_check(const struct lu_env *env,
443 struct mdd_object *tgt_obj,
444 const struct lu_name *lname,
445 struct mdd_object *src_obj)
447 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
451 if (!mdd_object_exists(src_obj))
454 if (mdd_is_dead_obj(src_obj))
457 /* Local ops, no lookup before link, check filename length here. */
458 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
459 RETURN(-ENAMETOOLONG);
461 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
464 if (S_ISDIR(mdd_object_type(src_obj)))
467 LASSERT(src_obj != tgt_obj);
469 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
474 rc = __mdd_may_link(env, src_obj);
479 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
480 const char *name, struct thandle *handle,
481 struct lustre_capa *capa)
483 struct dt_object *next = mdd_object_child(pobj);
487 if (dt_try_as_dir(env, next)) {
488 rc = next->do_index_ops->dio_delete(env, next,
489 (struct dt_key *)name,
497 static int __mdd_index_insert_only(const struct lu_env *env,
498 struct mdd_object *pobj,
499 const struct lu_fid *lf, const char *name,
500 struct thandle *handle,
501 struct lustre_capa *capa)
503 struct dt_object *next = mdd_object_child(pobj);
507 if (dt_try_as_dir(env, next)) {
508 struct lu_ucred *uc = lu_ucred_check(env);
511 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
512 rc = next->do_index_ops->dio_insert(env, next,
514 (const struct dt_key *)name,
515 handle, capa, ignore_quota);
522 /* insert named index, add reference if isdir */
523 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
524 const struct lu_fid *lf, const char *name, int is_dir,
525 struct thandle *handle, struct lustre_capa *capa)
530 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
531 if (rc == 0 && is_dir) {
532 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
533 mdo_ref_add(env, pobj, handle);
534 mdd_write_unlock(env, pobj);
539 /* delete named index, drop reference if isdir */
540 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
541 const char *name, int is_dir, struct thandle *handle,
542 struct lustre_capa *capa)
547 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
548 if (rc == 0 && is_dir) {
549 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
550 mdo_ref_del(env, pobj, handle);
551 mdd_write_unlock(env, pobj);
557 int mdd_declare_changelog_store(const struct lu_env *env,
558 struct mdd_device *mdd,
559 const struct lu_name *fname,
560 struct thandle *handle)
562 struct obd_device *obd = mdd2obd_dev(mdd);
563 struct llog_ctxt *ctxt;
564 struct llog_changelog_rec *rec;
570 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
573 reclen = llog_data_len(sizeof(*rec) +
574 (fname != NULL ? fname->ln_namelen : 0));
575 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
576 if (buf->lb_buf == NULL)
580 rec->cr_hdr.lrh_len = reclen;
581 rec->cr_hdr.lrh_type = CHANGELOG_REC;
583 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
587 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
593 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
594 struct mdd_device *mdd,
595 const struct lu_name *tname,
596 const struct lu_name *sname,
597 struct thandle *handle)
599 struct obd_device *obd = mdd2obd_dev(mdd);
600 struct llog_ctxt *ctxt;
601 struct llog_changelog_ext_rec *rec;
607 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
610 reclen = llog_data_len(sizeof(*rec) +
611 (tname != NULL ? tname->ln_namelen : 0) +
612 (sname != NULL ? 1 + sname->ln_namelen : 0));
613 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
614 if (buf->lb_buf == NULL)
618 rec->cr_hdr.lrh_len = reclen;
619 rec->cr_hdr.lrh_type = CHANGELOG_REC;
621 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
625 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
631 /** Add a changelog entry \a rec to the changelog llog
634 * \param handle - currently ignored since llogs start their own transaction;
635 * this will hopefully be fixed in llog rewrite
638 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
639 struct llog_changelog_rec *rec, struct thandle *th)
641 struct obd_device *obd = mdd2obd_dev(mdd);
642 struct llog_ctxt *ctxt;
645 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
646 rec->cr_hdr.lrh_type = CHANGELOG_REC;
647 rec->cr.cr_time = cl_time();
649 spin_lock(&mdd->mdd_cl.mc_lock);
650 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
651 * but as long as the MDD transactions are ordered correctly for e.g.
652 * rename conflicts, I don't think this should matter. */
653 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
654 spin_unlock(&mdd->mdd_cl.mc_lock);
656 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
660 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
667 /** Add a changelog_ext entry \a rec to the changelog llog
670 * \param handle - currently ignored since llogs start their own transaction;
671 * this will hopefully be fixed in llog rewrite
674 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
675 struct llog_changelog_ext_rec *rec,
678 struct obd_device *obd = mdd2obd_dev(mdd);
679 struct llog_ctxt *ctxt;
682 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
683 /* llog_lvfs_write_rec sets the llog tail len */
684 rec->cr_hdr.lrh_type = CHANGELOG_REC;
685 rec->cr.cr_time = cl_time();
687 spin_lock(&mdd->mdd_cl.mc_lock);
688 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
689 * but as long as the MDD transactions are ordered correctly for e.g.
690 * rename conflicts, I don't think this should matter. */
691 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
692 spin_unlock(&mdd->mdd_cl.mc_lock);
694 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
698 /* nested journal transaction */
699 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
707 /** Store a namespace change changelog record
708 * If this fails, we must fail the whole transaction; we don't
709 * want the change to commit without the log entry.
710 * \param target - mdd_object of change
711 * \param parent - parent dir/object
712 * \param tname - target name string
713 * \param handle - transacion handle
715 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
716 enum changelog_rec_type type, unsigned flags,
717 struct mdd_object *target, struct mdd_object *parent,
718 const struct lu_name *tname, struct thandle *handle)
720 struct llog_changelog_rec *rec;
727 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
729 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
732 LASSERT(target != NULL);
733 LASSERT(parent != NULL);
734 LASSERT(tname != NULL);
735 LASSERT(handle != NULL);
737 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
738 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
739 if (buf->lb_buf == NULL)
743 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
744 rec->cr.cr_type = (__u32)type;
745 rec->cr.cr_tfid = *mdo2fid(target);
746 rec->cr.cr_pfid = *mdo2fid(parent);
747 rec->cr.cr_namelen = tname->ln_namelen;
748 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
750 target->mod_cltime = cfs_time_current_64();
752 rc = mdd_changelog_store(env, mdd, rec, handle);
754 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
755 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
756 PFID(&rec->cr.cr_pfid));
764 /** Store a namespace change changelog record
765 * If this fails, we must fail the whole transaction; we don't
766 * want the change to commit without the log entry.
767 * \param target - mdd_object of change
768 * \param tpfid - target parent dir/object fid
769 * \param sfid - source object fid
770 * \param spfid - source parent fid
771 * \param tname - target name string
772 * \param sname - source name string
773 * \param handle - transacion handle
775 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
776 struct mdd_device *mdd,
777 enum changelog_rec_type type,
779 struct mdd_object *target,
780 const struct lu_fid *tpfid,
781 const struct lu_fid *sfid,
782 const struct lu_fid *spfid,
783 const struct lu_name *tname,
784 const struct lu_name *sname,
785 struct thandle *handle)
787 struct llog_changelog_ext_rec *rec;
794 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
796 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
799 LASSERT(sfid != NULL);
800 LASSERT(tpfid != NULL);
801 LASSERT(tname != NULL);
802 LASSERT(handle != NULL);
804 reclen = llog_data_len(sizeof(*rec) +
805 sname != NULL ? 1 + sname->ln_namelen : 0);
806 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
807 if (buf->lb_buf == NULL)
811 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
812 rec->cr.cr_type = (__u32)type;
813 rec->cr.cr_pfid = *tpfid;
814 rec->cr.cr_sfid = *sfid;
815 rec->cr.cr_spfid = *spfid;
816 rec->cr.cr_namelen = tname->ln_namelen;
817 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
819 rec->cr.cr_name[tname->ln_namelen] = '\0';
820 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
822 rec->cr.cr_namelen += 1 + sname->ln_namelen;
825 if (likely(target != NULL)) {
826 rec->cr.cr_tfid = *mdo2fid(target);
827 target->mod_cltime = cfs_time_current_64();
829 fid_zero(&rec->cr.cr_tfid);
832 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
834 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
835 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
842 static int __mdd_links_add(const struct lu_env *env,
843 struct mdd_object *mdd_obj,
844 struct linkea_data *ldata,
845 const struct lu_name *lname,
846 const struct lu_fid *pfid,
847 int first, int check)
851 if (ldata->ld_leh == NULL) {
852 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
856 rc = linkea_data_new(ldata,
857 &mdd_env_info(env)->mti_link_buf);
864 rc = linkea_links_find(ldata, lname, pfid);
865 if (rc && rc != -ENOENT)
871 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
872 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
876 linkea_add_buf(ldata, lname, tfid);
879 return linkea_add_buf(ldata, lname, pfid);
882 static int __mdd_links_del(const struct lu_env *env,
883 struct mdd_object *mdd_obj,
884 struct linkea_data *ldata,
885 const struct lu_name *lname,
886 const struct lu_fid *pfid)
890 if (ldata->ld_leh == NULL) {
891 rc = mdd_links_read(env, mdd_obj, ldata);
896 rc = linkea_links_find(ldata, lname, pfid);
900 linkea_del_buf(ldata, lname);
904 static int mdd_linkea_prepare(const struct lu_env *env,
905 struct mdd_object *mdd_obj,
906 const struct lu_fid *oldpfid,
907 const struct lu_name *oldlname,
908 const struct lu_fid *newpfid,
909 const struct lu_name *newlname,
910 int first, int check,
911 struct linkea_data *ldata)
917 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
920 LASSERT(oldpfid != NULL || newpfid != NULL);
922 if (mdd_obj->mod_flags & DEAD_OBJ)
923 /* No more links, don't bother */
926 if (oldpfid != NULL) {
927 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
930 (rc != -ENODATA && rc != -ENOENT))
932 /* No changes done. */
937 /* If renaming, add the new record */
938 if (newpfid != NULL) {
939 /* even if the add fails, we still delete the out-of-date
941 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
945 rc = rc != 0 ? rc : rc2;
950 int mdd_links_rename(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 struct thandle *handle,
957 struct linkea_data *ldata,
958 int first, int check)
965 ldata = &mdd_env_info(env)->mti_link_data;
966 memset(ldata, 0, sizeof(*ldata));
967 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
968 newpfid, newlname, first, check,
974 if (ldata->ld_lee != NULL)
975 rc = mdd_links_write(env, mdd_obj, ldata, handle);
982 if (rc == -EOVERFLOW || rc == -ENOSPC)
985 CDEBUG(error ? D_ERROR : D_OTHER,
986 "link_ea add '%.*s' failed %d "DFID"\n",
987 newlname->ln_namelen, newlname->ln_name,
988 rc, PFID(mdd_object_fid(mdd_obj)));
989 else if (newpfid == NULL)
990 CDEBUG(error ? D_ERROR : D_OTHER,
991 "link_ea del '%.*s' failed %d "DFID"\n",
992 oldlname->ln_namelen, oldlname->ln_name,
993 rc, PFID(mdd_object_fid(mdd_obj)));
995 CDEBUG(error ? D_ERROR : D_OTHER,
996 "link_ea rename '%.*s'->'%.*s' failed %d "
998 oldlname->ln_namelen, oldlname->ln_name,
999 newlname->ln_namelen, newlname->ln_name,
1000 rc, PFID(mdd_object_fid(mdd_obj)));
1003 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1004 /* if we vmalloced a large buffer drop it */
1005 lu_buf_free(ldata->ld_buf);
1010 static inline int mdd_links_add(const struct lu_env *env,
1011 struct mdd_object *mdd_obj,
1012 const struct lu_fid *pfid,
1013 const struct lu_name *lname,
1014 struct thandle *handle,
1015 struct linkea_data *data, int first)
1017 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1018 pfid, lname, handle, data, first, 0);
1021 static inline int mdd_links_del(const struct lu_env *env,
1022 struct mdd_object *mdd_obj,
1023 const struct lu_fid *pfid,
1024 const struct lu_name *lname,
1025 struct thandle *handle)
1027 return mdd_links_rename(env, mdd_obj, pfid, lname,
1028 NULL, NULL, handle, NULL, 0, 0);
1031 /** Read the link EA into a temp buffer.
1032 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1033 * A pointer to the buffer is stored in \a ldata::ld_buf.
1035 * \retval 0 or error
1037 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1038 struct linkea_data *ldata)
1042 /* First try a small buf */
1043 LASSERT(env != NULL);
1044 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1046 if (ldata->ld_buf->lb_buf == NULL)
1049 if (!mdd_object_exists(mdd_obj))
1052 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1054 if (rc == -ERANGE) {
1055 /* Buf was too small, figure out what we need. */
1056 lu_buf_free(ldata->ld_buf);
1057 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1058 XATTR_NAME_LINK, BYPASS_CAPA);
1061 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1062 if (ldata->ld_buf->lb_buf == NULL)
1064 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1065 XATTR_NAME_LINK, BYPASS_CAPA);
1070 return linkea_init(ldata);
1073 /** Read the link EA into a temp buffer.
1074 * Uses the name_buf since it is generally large.
1075 * \retval IS_ERR err
1076 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1078 struct lu_buf *mdd_links_get(const struct lu_env *env,
1079 struct mdd_object *mdd_obj)
1081 struct linkea_data ldata = { 0 };
1084 rc = mdd_links_read(env, mdd_obj, &ldata);
1085 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1088 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1089 struct linkea_data *ldata, struct thandle *handle)
1091 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1092 ldata->ld_leh->leh_len);
1093 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1094 mdd_object_capa(env, mdd_obj));
1097 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1098 struct thandle *handle, struct linkea_data *ldata)
1104 if (ldata != NULL && ldata->ld_lee != NULL) {
1105 ea_len = ldata->ld_leh->leh_len;
1106 linkea = ldata->ld_buf->lb_buf;
1112 /* XXX: max size? */
1113 rc = mdo_declare_xattr_set(env, mdd_obj,
1114 mdd_buf_get_const(env, linkea, ea_len),
1115 XATTR_NAME_LINK, 0, handle);
1119 static inline int mdd_declare_links_del(const struct lu_env *env,
1120 struct mdd_object *c,
1121 struct thandle *handle)
1125 /* For directory, the linkEA will be removed together
1126 * with the object. */
1127 if (!S_ISDIR(mdd_object_type(c)))
1128 rc = mdd_declare_links_add(env, c, handle, NULL);
1133 static int mdd_declare_link(const struct lu_env *env,
1134 struct mdd_device *mdd,
1135 struct mdd_object *p,
1136 struct mdd_object *c,
1137 const struct lu_name *name,
1138 struct thandle *handle,
1140 struct linkea_data *data)
1144 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1148 rc = mdo_declare_ref_add(env, c, handle);
1152 la->la_valid = LA_CTIME | LA_MTIME;
1153 rc = mdo_declare_attr_set(env, p, la, handle);
1157 la->la_valid = LA_CTIME;
1158 rc = mdo_declare_attr_set(env, c, la, handle);
1162 rc = mdd_declare_links_add(env, c, handle, data);
1166 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1171 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1172 struct md_object *src_obj, const struct lu_name *lname,
1175 const char *name = lname->ln_name;
1176 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1177 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1178 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1179 struct mdd_device *mdd = mdo2mdd(src_obj);
1180 struct dynlock_handle *dlh;
1181 struct thandle *handle;
1182 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1186 handle = mdd_trans_create(env, mdd);
1188 GOTO(out_pending, rc = PTR_ERR(handle));
1190 memset(ldata, 0, sizeof(*ldata));
1192 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1193 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1195 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1200 rc = mdd_trans_start(env, mdd, handle);
1204 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
1206 GOTO(out_trans, rc = -ENOMEM);
1207 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1209 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
1211 GOTO(out_unlock, rc);
1213 rc = mdo_ref_add(env, mdd_sobj, handle);
1215 GOTO(out_unlock, rc);
1218 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1220 mdd_object_capa(env, mdd_tobj));
1222 mdo_ref_del(env, mdd_sobj, handle);
1223 GOTO(out_unlock, rc);
1226 la->la_valid = LA_CTIME | LA_MTIME;
1227 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1229 GOTO(out_unlock, rc);
1231 la->la_valid = LA_CTIME;
1232 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1234 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1235 mdo2fid(mdd_tobj), lname, 0, 0,
1238 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1239 lname, handle, ldata, 0);
1240 /* The failure of links_add should not cause the link
1241 * failure, reset rc here */
1246 mdd_write_unlock(env, mdd_sobj);
1247 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1250 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1251 mdd_tobj, lname, handle);
1253 mdd_trans_stop(env, mdd, rc, handle);
1255 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1256 /* if we vmalloced a large buffer drop it */
1257 lu_buf_free(ldata->ld_buf);
1262 int mdd_declare_finish_unlink(const struct lu_env *env,
1263 struct mdd_object *obj,
1265 struct thandle *handle)
1269 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1273 return mdo_declare_destroy(env, obj, handle);
1276 /* caller should take a lock before calling */
1277 int mdd_finish_unlink(const struct lu_env *env,
1278 struct mdd_object *obj, struct md_attr *ma,
1282 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1285 LASSERT(mdd_write_locked(env, obj) != 0);
1287 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1288 obj->mod_flags |= DEAD_OBJ;
1289 /* add new orphan and the object
1290 * will be deleted during mdd_close() */
1291 if (obj->mod_count) {
1292 rc = __mdd_orphan_add(env, obj, th);
1294 CDEBUG(D_HA, "Object "DFID" is inserted into "
1295 "orphan list, open count = %d\n",
1296 PFID(mdd_object_fid(obj)),
1299 CERROR("Object "DFID" fail to be an orphan, "
1300 "open count = %d, maybe cause failed "
1302 PFID(mdd_object_fid(obj)),
1305 rc = mdo_destroy(env, obj, th);
1314 * has mdd_write_lock on cobj already, but not on pobj yet
1316 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1317 struct mdd_object *cobj, struct lu_attr *cattr)
1322 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1327 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1328 struct mdd_object *p, struct mdd_object *c,
1329 const struct lu_name *name, struct md_attr *ma,
1330 struct thandle *handle, int no_name)
1332 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1335 if (likely(no_name == 0)) {
1336 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1341 rc = mdo_declare_ref_del(env, p, handle);
1345 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1346 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1347 la->la_valid = LA_CTIME | LA_MTIME;
1348 rc = mdo_declare_attr_set(env, p, la, handle);
1353 rc = mdo_declare_ref_del(env, c, handle);
1357 rc = mdo_declare_ref_del(env, c, handle);
1361 la->la_valid = LA_CTIME;
1362 rc = mdo_declare_attr_set(env, c, la, handle);
1366 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1370 rc = mdd_declare_links_del(env, c, handle);
1374 /* FIXME: need changelog for remove entry */
1375 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1382 * Delete name entry and the object.
1383 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1384 * does not exist for this object, and it could only happen during resending
1385 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1386 * is also needed in this case(needed by changelog), so we have to add another
1387 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1388 * the ENOENT failure should be able to be fixed by redo mechanism.
1390 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1391 struct md_object *cobj, const struct lu_name *lname,
1392 struct md_attr *ma, int no_name)
1394 const char *name = lname->ln_name;
1395 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1396 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1397 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1398 struct mdd_object *mdd_cobj = NULL;
1399 struct mdd_device *mdd = mdo2mdd(pobj);
1400 struct dynlock_handle *dlh;
1401 struct thandle *handle;
1405 /* cobj == NULL means only delete name entry */
1406 if (likely(cobj != NULL)) {
1407 mdd_cobj = md2mdd_obj(cobj);
1408 if (mdd_object_exists(mdd_cobj) == 0)
1410 /* currently it is assume, it could only delete
1411 * name entry of remote directory */
1415 handle = mdd_trans_create(env, mdd);
1417 RETURN(PTR_ERR(handle));
1419 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1420 lname, ma, handle, no_name);
1424 rc = mdd_trans_start(env, mdd, handle);
1428 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1430 GOTO(stop, rc = -ENOMEM);
1432 if (likely(mdd_cobj != NULL)) {
1433 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1436 rc = mdd_la_get(env, mdd_cobj, cattr,
1437 mdd_object_capa(env, mdd_cobj));
1441 is_dir = S_ISDIR(cattr->la_mode);
1445 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1449 if (likely(no_name == 0)) {
1450 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1451 mdd_object_capa(env, mdd_pobj));
1456 if (likely(mdd_cobj != NULL)) {
1457 rc = mdo_ref_del(env, mdd_cobj, handle);
1459 __mdd_index_insert_only(env, mdd_pobj,
1462 mdd_object_capa(env, mdd_pobj));
1468 mdo_ref_del(env, mdd_cobj, handle);
1470 /* fetch updated nlink */
1471 rc = mdd_la_get(env, mdd_cobj, cattr,
1472 mdd_object_capa(env, mdd_cobj));
1477 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1478 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1480 la->la_valid = LA_CTIME | LA_MTIME;
1481 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1485 /* Enough for only unlink the entry */
1486 if (unlikely(mdd_cobj == NULL)) {
1487 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1491 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1492 /* update ctime of an unlinked file only if it is still
1493 * opened or a link still exists */
1494 la->la_valid = LA_CTIME;
1495 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1500 /* XXX: this transfer to ma will be removed with LOD/OSP */
1501 ma->ma_attr = *cattr;
1502 ma->ma_valid |= MA_INODE;
1503 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1505 /* fetch updated nlink */
1507 rc = mdd_la_get(env, mdd_cobj, cattr,
1508 mdd_object_capa(env, mdd_cobj));
1511 /* old files may not have link ea; ignore errors */
1512 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1514 /* if object is removed then we can't get its attrs, use last get */
1515 if (cattr->la_nlink == 0) {
1516 ma->ma_attr = *cattr;
1517 ma->ma_valid |= MA_INODE;
1521 mdd_write_unlock(env, mdd_cobj);
1522 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1526 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1527 if ((ma->ma_valid & MA_HSM) &&
1528 (ma->ma_hsm.mh_flags & HS_EXISTS))
1529 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1531 rc = mdd_changelog_ns_store(env, mdd,
1532 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1533 mdd_cobj, mdd_pobj, lname, handle);
1537 mdd_trans_stop(env, mdd, rc, handle);
1543 * The permission has been checked when obj created, no need check again.
1545 static int mdd_cd_sanity_check(const struct lu_env *env,
1546 struct mdd_object *obj)
1551 if (!obj || mdd_is_dead_obj(obj))
1558 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1559 struct md_object *cobj, const struct md_op_spec *spec,
1562 struct mdd_device *mdd = mdo2mdd(cobj);
1563 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1564 struct mdd_object *son = md2mdd_obj(cobj);
1565 struct thandle *handle;
1566 const struct lu_buf *buf;
1567 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1571 rc = mdd_cd_sanity_check(env, son);
1575 if (!md_should_create(spec->sp_cr_flags))
1579 * there are following use cases for this function:
1580 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1581 * striping can be specified or not
1584 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1588 /* calling ->ah_make_hint() is used to transfer information from parent */
1589 mdd_object_make_hint(env, mdd_pobj, son, attr);
1591 handle = mdd_trans_create(env, mdd);
1593 GOTO(out_free, rc = PTR_ERR(handle));
1596 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1597 * Should this be fixed?
1599 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1600 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1601 spec->sp_cr_flags, spec->no_create);
1603 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1604 /* replay case or lfs setstripe */
1605 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1606 spec->u.sp_ea.eadatalen);
1611 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1612 XATTR_NAME_LOV, 0, handle);
1616 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1620 rc = mdd_trans_start(env, mdd, handle);
1624 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1625 0, handle, mdd_object_capa(env, son));
1630 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1633 mdd_trans_stop(env, mdd, rc, handle);
1638 /* Get fid from name and parent */
1640 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1641 const struct lu_name *lname, struct lu_fid* fid, int mask)
1643 const char *name = lname->ln_name;
1644 const struct dt_key *key = (const struct dt_key *)name;
1645 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1646 struct mdd_device *m = mdo2mdd(pobj);
1647 struct dt_object *dir = mdd_object_child(mdd_obj);
1651 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1654 if (mdd_object_remote(mdd_obj)) {
1655 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1656 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1657 } else if (!mdd_object_exists(mdd_obj)) {
1661 /* The common filename length check. */
1662 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1663 RETURN(-ENAMETOOLONG);
1665 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1670 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1671 dt_try_as_dir(env, dir))) {
1673 rc = dir->do_index_ops->dio_lookup(env, dir,
1674 (struct dt_rec *)fid, key,
1675 mdd_object_capa(env, mdd_obj));
1686 static int mdd_declare_object_initialize(const struct lu_env *env,
1687 struct mdd_object *parent,
1688 struct mdd_object *child,
1689 struct lu_attr *attr,
1690 struct thandle *handle,
1691 struct linkea_data *ldata)
1697 * inode mode has been set in creation time, and it's based on umask,
1698 * la_mode and acl, don't set here again! (which will go wrong
1699 * because below function doesn't consider umask).
1700 * I'd suggest set all object attributes in creation time, see above.
1702 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1703 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1704 rc = mdo_declare_attr_set(env, child, attr, handle);
1705 attr->la_valid |= LA_MODE | LA_TYPE;
1706 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1707 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1710 rc = mdo_declare_ref_add(env, child, handle);
1712 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1717 mdd_declare_links_add(env, child, handle, ldata);
1722 static int mdd_object_initialize(const struct lu_env *env,
1723 const struct lu_fid *pfid,
1724 const struct lu_name *lname,
1725 struct mdd_object *child,
1726 struct lu_attr *attr, struct thandle *handle,
1727 const struct md_op_spec *spec,
1728 struct linkea_data *ldata)
1734 * Update attributes for child.
1737 * (1) the valid bits should be converted between Lustre and Linux;
1738 * (2) maybe, the child attributes should be set in OSD when creation.
1741 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1742 /* arguments are supposed to stay the same */
1743 if (S_ISDIR(attr->la_mode)) {
1744 /* Add "." and ".." for newly created dir */
1745 mdo_ref_add(env, child, handle);
1746 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1747 dot, handle, BYPASS_CAPA);
1749 rc = __mdd_index_insert_only(env, child, pfid,
1753 mdo_ref_del(env, child, handle);
1757 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1762 /* has not lock on pobj yet */
1763 static int mdd_create_sanity_check(const struct lu_env *env,
1764 struct md_object *pobj,
1765 struct lu_attr *pattr,
1766 const struct lu_name *lname,
1767 struct lu_attr *cattr,
1768 struct md_op_spec *spec)
1770 struct mdd_thread_info *info = mdd_env_info(env);
1771 struct lu_fid *fid = &info->mti_fid;
1772 struct mdd_object *obj = md2mdd_obj(pobj);
1773 struct mdd_device *m = mdo2mdd(pobj);
1778 if (mdd_is_dead_obj(obj))
1782 * In some cases this lookup is not needed - we know before if name
1783 * exists or not because MDT performs lookup for it.
1784 * name length check is done in lookup.
1786 if (spec->sp_cr_lookup) {
1788 * Check if the name already exist, though it will be checked in
1789 * _index_insert also, for avoiding rolling back if exists
1792 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1793 MAY_WRITE | MAY_EXEC);
1795 RETURN(rc ? : -EEXIST);
1798 * Check WRITE permission for the parent.
1799 * EXEC permission have been checked
1800 * when lookup before create already.
1802 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1809 if (pattr->la_mode & S_ISGID) {
1810 cattr->la_gid = pattr->la_gid;
1811 if (S_ISDIR(cattr->la_mode)) {
1812 cattr->la_mode |= S_ISGID;
1813 cattr->la_valid |= LA_MODE;
1817 switch (cattr->la_mode & S_IFMT) {
1819 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1821 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1822 RETURN(-ENAMETOOLONG);
1841 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1842 struct mdd_object *p, struct mdd_object *c,
1843 const struct lu_name *name,
1844 struct lu_attr *attr,
1845 struct thandle *handle,
1846 const struct md_op_spec *spec,
1847 struct linkea_data *ldata,
1848 struct lu_buf *def_acl_buf,
1849 struct lu_buf *acl_buf)
1853 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1857 #ifdef CONFIG_FS_POSIX_ACL
1858 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1859 /* if dir, then can inherit default ACl */
1860 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1861 XATTR_NAME_ACL_DEFAULT,
1867 if (acl_buf->lb_len > 0) {
1868 rc = mdo_declare_attr_set(env, c, attr, handle);
1872 rc = mdo_declare_xattr_set(env, c, acl_buf,
1873 XATTR_NAME_ACL_ACCESS, 0, handle);
1879 if (S_ISDIR(attr->la_mode)) {
1880 rc = mdo_declare_ref_add(env, p, handle);
1885 rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1889 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1890 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1892 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1893 name->ln_name, handle);
1897 /* replay case, create LOV EA from client data */
1898 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1899 const struct lu_buf *buf;
1901 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1902 spec->u.sp_ea.eadatalen);
1903 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1909 if (S_ISLNK(attr->la_mode)) {
1910 rc = dt_declare_record_write(env, mdd_object_child(c),
1911 strlen(spec->u.sp_symname), 0,
1917 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1918 rc = mdo_declare_attr_set(env, p, attr, handle);
1923 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1931 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1932 struct lu_attr *la, struct lu_buf *def_acl_buf,
1933 struct lu_buf *acl_buf)
1938 if (S_ISLNK(la->la_mode)) {
1939 acl_buf->lb_len = 0;
1940 def_acl_buf->lb_len = 0;
1944 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1945 rc = mdo_xattr_get(env, pobj, def_acl_buf,
1946 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1947 mdd_read_unlock(env, pobj);
1949 /* If there are default ACL, fix mode/ACL by default ACL */
1950 def_acl_buf->lb_len = rc;
1951 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
1952 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
1953 acl_buf->lb_len = rc;
1954 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1957 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1958 /* If there are no default ACL, fix mode by mask */
1959 struct lu_ucred *uc = lu_ucred(env);
1961 /* The create triggered by MDT internal events, such as
1962 * LFSCK reset, will not contain valid "uc". */
1963 if (unlikely(uc != NULL))
1964 la->la_mode &= ~uc->uc_umask;
1966 acl_buf->lb_len = 0;
1967 def_acl_buf->lb_len = 0;
1974 * Create object and insert it into namespace.
1976 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1977 const struct lu_name *lname, struct md_object *child,
1978 struct md_op_spec *spec, struct md_attr* ma)
1980 struct mdd_thread_info *info = mdd_env_info(env);
1981 struct lu_attr *la = &info->mti_la_for_fix;
1982 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1983 struct mdd_object *son = md2mdd_obj(child);
1984 struct mdd_device *mdd = mdo2mdd(pobj);
1985 struct lu_attr *attr = &ma->ma_attr;
1986 struct thandle *handle;
1987 struct lu_attr *pattr = &info->mti_pattr;
1988 struct lu_buf acl_buf;
1989 struct lu_buf def_acl_buf;
1990 struct linkea_data *ldata = &info->mti_link_data;
1991 struct dynlock_handle *dlh;
1992 const char *name = lname->ln_name;
1993 int rc, created = 0, initialized = 0, inserted = 0;
1997 * Two operations have to be performed:
1999 * - an allocation of a new object (->do_create()), and
2001 * - an insertion into a parent index (->dio_insert()).
2003 * Due to locking, operation order is not important, when both are
2004 * successful, *but* error handling cases are quite different:
2006 * - if insertion is done first, and following object creation fails,
2007 * insertion has to be rolled back, but this operation might fail
2008 * also leaving us with dangling index entry.
2010 * - if creation is done first, is has to be undone if insertion
2011 * fails, leaving us with leaked space, which is neither good, nor
2014 * It seems that creation-first is simplest solution, but it is
2015 * sub-optimal in the frequent
2020 * case, because second mkdir is bound to create object, only to
2021 * destroy it immediately.
2023 * To avoid this follow local file systems that do double lookup:
2025 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2027 * 1. create (mdd_object_create_internal())
2029 * 2. insert (__mdd_index_insert(), lookup again)
2032 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2036 /* Sanity checks before big job. */
2037 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2041 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2042 GOTO(out_free, rc = -EINPROGRESS);
2044 acl_buf.lb_buf = info->mti_xattr_buf;
2045 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2046 def_acl_buf.lb_buf = info->mti_key;
2047 def_acl_buf.lb_len = sizeof(info->mti_key);
2048 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2052 mdd_object_make_hint(env, mdd_pobj, son, attr);
2054 handle = mdd_trans_create(env, mdd);
2056 GOTO(out_free, rc = PTR_ERR(handle));
2058 memset(ldata, 0, sizeof(*ldata));
2059 mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2060 lname, 1, 0, ldata);
2062 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2063 handle, spec, ldata, &def_acl_buf, &acl_buf);
2067 rc = mdd_trans_start(env, mdd, handle);
2071 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2073 GOTO(out_trans, rc = -ENOMEM);
2075 mdd_write_lock(env, son, MOR_TGT_CHILD);
2076 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
2078 mdd_write_unlock(env, son);
2084 #ifdef CONFIG_FS_POSIX_ACL
2085 if (def_acl_buf.lb_len > 0 && S_ISDIR(attr->la_mode)) {
2086 /* set default acl */
2087 rc = mdo_xattr_set(env, son, &def_acl_buf,
2088 XATTR_NAME_ACL_DEFAULT, 0,
2089 handle, BYPASS_CAPA);
2091 mdd_write_unlock(env, son);
2095 /* set its own acl */
2096 if (acl_buf.lb_len > 0) {
2097 rc = mdo_xattr_set(env, son, &acl_buf,
2098 XATTR_NAME_ACL_ACCESS,
2099 0, handle, BYPASS_CAPA);
2101 mdd_write_unlock(env, son);
2107 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2108 son, attr, handle, spec, ldata);
2111 * in case of replay we just set LOVEA provided by the client
2112 * XXX: I think it would be interesting to try "old" way where
2113 * MDT calls this xattr_set(LOV) in a different transaction.
2114 * probably this way we code can be made better.
2116 if (rc == 0 && (spec->no_create ||
2117 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
2118 const struct lu_buf *buf;
2120 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2121 spec->u.sp_ea.eadatalen);
2122 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2126 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2127 rc = __mdd_orphan_add(env, son, handle);
2129 mdd_write_unlock(env, son);
2133 * Object has no links, so it will be destroyed when last
2134 * reference is released. (XXX not now.)
2140 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
2141 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2142 name, S_ISDIR(attr->la_mode), handle,
2143 mdd_object_capa(env, mdd_pobj));
2150 if (S_ISLNK(attr->la_mode)) {
2151 struct lu_ucred *uc = lu_ucred_assert(env);
2152 struct dt_object *dt = mdd_object_child(son);
2153 const char *target_name = spec->u.sp_symname;
2154 int sym_len = strlen(target_name);
2155 const struct lu_buf *buf;
2158 buf = mdd_buf_get_const(env, target_name, sym_len);
2159 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2160 mdd_object_capa(env, son),
2162 CFS_CAP_SYS_RESOURCE_MASK);
2167 GOTO(cleanup, rc = -EFAULT);
2170 /* volatile file creation does not update parent directory times */
2171 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2172 GOTO(cleanup, rc = 0);
2174 /* update parent directory mtime/ctime */
2176 la->la_valid = LA_CTIME | LA_MTIME;
2177 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
2183 if (rc != 0 && created != 0) {
2186 if (inserted != 0) {
2187 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2188 rc2 = __mdd_orphan_del(env, son, handle);
2190 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2191 S_ISDIR(attr->la_mode),
2192 handle, BYPASS_CAPA);
2197 mdd_write_lock(env, son, MOR_TGT_CHILD);
2198 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2199 /* Drop the reference, no need to delete "."/"..",
2200 * because the object to be destroied directly. */
2201 rc2 = mdo_ref_del(env, son, handle);
2203 mdd_write_unlock(env, son);
2208 rc2 = mdo_ref_del(env, son, handle);
2210 mdd_write_unlock(env, son);
2214 mdo_destroy(env, son, handle);
2215 mdd_write_unlock(env, son);
2218 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2220 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2221 rc = mdd_changelog_ns_store(env, mdd,
2222 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2223 S_ISREG(attr->la_mode) ? CL_CREATE :
2224 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2225 0, son, mdd_pobj, lname, handle);
2227 mdd_trans_stop(env, mdd, rc, handle);
2229 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2230 /* if we vmalloced a large buffer drop it */
2231 lu_buf_free(ldata->ld_buf);
2233 /* The child object shouldn't be cached anymore */
2235 set_bit(LU_OBJECT_HEARD_BANSHEE,
2236 &child->mo_lu.lo_header->loh_flags);
2241 * Get locks on parents in proper order
2242 * RETURN: < 0 - error, rename_order if successful
2250 static int mdd_rename_order(const struct lu_env *env,
2251 struct mdd_device *mdd,
2252 struct mdd_object *src_pobj,
2253 struct mdd_object *tgt_pobj)
2255 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2259 if (src_pobj == tgt_pobj)
2260 RETURN(MDD_RN_SAME);
2262 /* compared the parent child relationship of src_p&tgt_p */
2263 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2265 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2268 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2281 /* has not mdd_write{read}_lock on any obj yet. */
2282 static int mdd_rename_sanity_check(const struct lu_env *env,
2283 struct mdd_object *src_pobj,
2284 struct mdd_object *tgt_pobj,
2285 struct mdd_object *sobj,
2286 struct mdd_object *tobj,
2287 struct lu_attr *so_attr,
2288 struct lu_attr *tg_attr)
2293 /* XXX: when get here, sobj must NOT be NULL,
2294 * the other case has been processed in cld_rename
2295 * before mdd_rename and enable MDS_PERM_BYPASS. */
2298 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2302 /* XXX: when get here, "tobj == NULL" means tobj must
2303 * NOT exist (neither on remote MDS, such case has been
2304 * processed in cld_rename before mdd_rename and enable
2306 * So check may_create, but not check may_unlink. */
2308 rc = mdd_may_create(env, tgt_pobj, NULL,
2309 (src_pobj != tgt_pobj), 0);
2311 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2312 (src_pobj != tgt_pobj), 1);
2314 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2315 S_ISDIR(so_attr->la_mode))
2316 rc = __mdd_may_link(env, tgt_pobj);
2321 static int mdd_declare_rename(const struct lu_env *env,
2322 struct mdd_device *mdd,
2323 struct mdd_object *mdd_spobj,
2324 struct mdd_object *mdd_tpobj,
2325 struct mdd_object *mdd_sobj,
2326 struct mdd_object *mdd_tobj,
2327 const struct lu_name *tname,
2328 const struct lu_name *sname,
2330 struct linkea_data *ldata,
2331 struct thandle *handle)
2333 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2336 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2337 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2343 /* name from source dir */
2344 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2348 /* .. from source child */
2349 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2350 /* source child can be directory,
2351 * counted by source dir's nlink */
2352 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2355 if (mdd_spobj != mdd_tpobj) {
2356 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2361 rc = mdo_declare_index_insert(env, mdd_sobj,
2367 /* new target child can be directory,
2368 * counted by target dir's nlink */
2369 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2375 la->la_valid = LA_CTIME | LA_MTIME;
2376 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2380 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2384 la->la_valid = LA_CTIME;
2385 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2389 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2394 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2395 tname->ln_name, handle);
2399 /* name from target dir (old name), we declare it unconditionally
2400 * as mdd_rename() calls delete unconditionally as well. so just
2401 * to balance declarations vs calls to change ... */
2402 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2406 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2407 /* delete target child in target parent directory */
2408 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2412 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2413 /* target child can be directory,
2414 * delete "." reference in target child directory */
2415 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2419 /* delete ".." reference in target parent directory */
2420 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2425 la->la_valid = LA_CTIME;
2426 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2430 mdd_declare_links_del(env, mdd_tobj, handle);
2434 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2439 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2446 /* src object can be remote that is why we use only fid and type of object */
2447 static int mdd_rename(const struct lu_env *env,
2448 struct md_object *src_pobj, struct md_object *tgt_pobj,
2449 const struct lu_fid *lf, const struct lu_name *lsname,
2450 struct md_object *tobj, const struct lu_name *ltname,
2453 const char *sname = lsname->ln_name;
2454 const char *tname = ltname->ln_name;
2455 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2456 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2457 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2458 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2459 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2460 struct mdd_device *mdd = mdo2mdd(src_pobj);
2461 struct mdd_object *mdd_sobj = NULL; /* source object */
2462 struct mdd_object *mdd_tobj = NULL;
2463 struct dynlock_handle *sdlh = NULL, *tdlh = NULL;
2464 struct thandle *handle;
2465 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2466 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2467 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2470 bool tobj_locked = 0;
2471 unsigned cl_flags = 0;
2476 mdd_tobj = md2mdd_obj(tobj);
2478 mdd_sobj = mdd_object_find(env, mdd, lf);
2480 rc = mdd_la_get(env, mdd_sobj, so_attr,
2481 mdd_object_capa(env, mdd_sobj));
2483 GOTO(out_pending, rc);
2486 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2487 mdd_object_capa(env, mdd_tobj));
2489 GOTO(out_pending, rc);
2492 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2493 mdd_tobj, so_attr, tg_attr);
2495 GOTO(out_pending, rc);
2497 handle = mdd_trans_create(env, mdd);
2499 GOTO(out_pending, rc = PTR_ERR(handle));
2501 memset(ldata, 0, sizeof(*ldata));
2502 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2503 ltname, 1, 0, ldata);
2504 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2505 mdd_tobj, lsname, ltname, ma, ldata, handle);
2509 rc = mdd_trans_start(env, mdd, handle);
2513 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2514 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2516 GOTO(cleanup_unlocked, rc);
2518 /* Get locks in determined order */
2519 if (rc == MDD_RN_SAME) {
2520 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2521 sname, MOR_SRC_PARENT);
2522 /* check hashes to determine do we need one lock or two */
2523 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2524 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2528 } else if (rc == MDD_RN_SRCTGT) {
2529 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2530 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2532 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2533 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2535 if (sdlh == NULL || tdlh == NULL)
2536 GOTO(cleanup, rc = -ENOMEM);
2538 is_dir = S_ISDIR(so_attr->la_mode);
2540 /* Remove source name from source directory */
2541 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2542 mdd_object_capa(env, mdd_spobj));
2546 /* "mv dir1 dir2" needs "dir1/.." link update */
2547 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2548 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2549 mdd_object_capa(env, mdd_sobj));
2551 GOTO(fixup_spobj2, rc);
2553 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2554 handle, mdd_object_capa(env, mdd_sobj));
2556 GOTO(fixup_spobj, rc);
2559 /* Remove target name from target directory
2560 * Here tobj can be remote one, so we do index_delete unconditionally
2561 * and -ENOENT is allowed.
2563 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2564 mdd_object_capa(env, mdd_tpobj));
2567 /* tname might been renamed to something else */
2568 GOTO(fixup_spobj, rc);
2571 GOTO(fixup_spobj, rc);
2574 /* Insert new fid with target name into target dir */
2575 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2576 mdd_object_capa(env, mdd_tpobj));
2578 GOTO(fixup_tpobj, rc);
2580 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2581 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2583 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2585 la->la_valid = LA_CTIME;
2586 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2588 GOTO(fixup_tpobj, rc);
2591 /* Remove old target object
2592 * For tobj is remote case cmm layer has processed
2593 * and set tobj to NULL then. So when tobj is NOT NULL,
2594 * it must be local one.
2596 if (tobj && mdd_object_exists(mdd_tobj)) {
2597 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2599 if (mdd_is_dead_obj(mdd_tobj)) {
2600 /* shld not be dead, something is wrong */
2601 CERROR("tobj is dead, something is wrong\n");
2605 mdo_ref_del(env, mdd_tobj, handle);
2607 /* Remove dot reference. */
2608 if (S_ISDIR(tg_attr->la_mode))
2609 mdo_ref_del(env, mdd_tobj, handle);
2612 /* fetch updated nlink */
2613 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2614 mdd_object_capa(env, mdd_tobj));
2616 CERROR("%s: Failed to get nlink for tobj "
2618 mdd2obd_dev(mdd)->obd_name,
2619 PFID(tpobj_fid), rc);
2620 GOTO(fixup_tpobj, rc);
2623 la->la_valid = LA_CTIME;
2624 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2626 CERROR("%s: Failed to set ctime for tobj "
2628 mdd2obd_dev(mdd)->obd_name,
2629 PFID(tpobj_fid), rc);
2630 GOTO(fixup_tpobj, rc);
2633 /* XXX: this transfer to ma will be removed with LOD/OSP */
2634 ma->ma_attr = *tg_attr;
2635 ma->ma_valid |= MA_INODE;
2636 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2638 CERROR("%s: Failed to unlink tobj "
2640 mdd2obd_dev(mdd)->obd_name,
2641 PFID(tpobj_fid), rc);
2642 GOTO(fixup_tpobj, rc);
2645 /* fetch updated nlink */
2646 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2647 mdd_object_capa(env, mdd_tobj));
2649 CERROR("%s: Failed to get nlink for tobj "
2651 mdd2obd_dev(mdd)->obd_name,
2652 PFID(tpobj_fid), rc);
2653 GOTO(fixup_tpobj, rc);
2655 /* XXX: this transfer to ma will be removed with LOD/OSP */
2656 ma->ma_attr = *tg_attr;
2657 ma->ma_valid |= MA_INODE;
2659 if (tg_attr->la_nlink == 0)
2660 cl_flags |= CLF_RENAME_LAST;
2663 la->la_valid = LA_CTIME | LA_MTIME;
2664 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2666 GOTO(fixup_tpobj, rc);
2668 if (mdd_spobj != mdd_tpobj) {
2669 la->la_valid = LA_CTIME | LA_MTIME;
2670 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2674 if (rc == 0 && mdd_sobj) {
2675 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2676 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2677 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2680 /* Old files might not have EA entry */
2681 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2682 lsname, handle, NULL, 0);
2683 mdd_write_unlock(env, mdd_sobj);
2684 /* We don't fail the transaction if the link ea can't be
2685 updated -- fid2path will use alternate lookup method. */
2693 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2696 CWARN("tp obj fix error %d\n",rc2);
2698 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2699 !mdd_is_dead_obj(mdd_tobj)) {
2701 mdo_ref_add(env, mdd_tobj, handle);
2703 mdo_ref_add(env, mdd_tobj, handle);
2706 rc2 = __mdd_index_insert(env, mdd_tpobj,
2707 mdo2fid(mdd_tobj), tname,
2712 CWARN("tp obj fix error %d\n",rc2);
2717 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2718 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2722 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2723 mdd2obd_dev(mdd)->obd_name, rc2);
2726 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2727 dotdot, handle, BYPASS_CAPA);
2729 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2730 mdd2obd_dev(mdd)->obd_name, rc2);
2735 rc2 = __mdd_index_insert(env, mdd_spobj,
2736 lf, sname, is_dir, handle, BYPASS_CAPA);
2738 CWARN("sp obj fix error %d\n",rc2);
2742 mdd_write_unlock(env, mdd_tobj);
2743 if (likely(tdlh) && sdlh != tdlh)
2744 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2746 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2749 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2750 mdd_tobj, tpobj_fid, lf,
2751 spobj_fid, ltname, lsname,
2755 mdd_trans_stop(env, mdd, rc, handle);
2757 mdd_object_put(env, mdd_sobj);
2761 const struct md_dir_operations mdd_dir_ops = {
2762 .mdo_is_subdir = mdd_is_subdir,
2763 .mdo_lookup = mdd_lookup,
2764 .mdo_create = mdd_create,
2765 .mdo_rename = mdd_rename,
2766 .mdo_link = mdd_link,
2767 .mdo_unlink = mdd_unlink,
2768 .mdo_create_data = mdd_create_data,