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 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
880 linkea_add_buf(ldata, lname, pfid);
882 return linkea_add_buf(ldata, lname, pfid);
885 static int __mdd_links_del(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)
893 if (ldata->ld_leh == NULL) {
894 rc = mdd_links_read(env, mdd_obj, ldata);
899 rc = linkea_links_find(ldata, lname, pfid);
903 linkea_del_buf(ldata, lname);
907 static int mdd_linkea_prepare(const struct lu_env *env,
908 struct mdd_object *mdd_obj,
909 const struct lu_fid *oldpfid,
910 const struct lu_name *oldlname,
911 const struct lu_fid *newpfid,
912 const struct lu_name *newlname,
913 int first, int check,
914 struct linkea_data *ldata)
920 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
923 LASSERT(oldpfid != NULL || newpfid != NULL);
925 if (mdd_obj->mod_flags & DEAD_OBJ) {
926 /* Prevent linkea to be updated which is NOT necessary. */
927 ldata->ld_reclen = 0;
928 /* No more links, don't bother */
932 if (oldpfid != NULL) {
933 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
936 (rc != -ENODATA && rc != -ENOENT))
938 /* No changes done. */
943 /* If renaming, add the new record */
944 if (newpfid != NULL) {
945 /* even if the add fails, we still delete the out-of-date
947 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
951 rc = rc != 0 ? rc : rc2;
956 int mdd_links_rename(const struct lu_env *env,
957 struct mdd_object *mdd_obj,
958 const struct lu_fid *oldpfid,
959 const struct lu_name *oldlname,
960 const struct lu_fid *newpfid,
961 const struct lu_name *newlname,
962 struct thandle *handle,
963 struct linkea_data *ldata,
964 int first, int check)
971 ldata = &mdd_env_info(env)->mti_link_data;
972 memset(ldata, 0, sizeof(*ldata));
973 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
974 newpfid, newlname, first, check,
980 if (ldata->ld_reclen != 0)
981 rc = mdd_links_write(env, mdd_obj, ldata, handle);
988 if (rc == -EOVERFLOW || rc == -ENOSPC)
991 CDEBUG(error ? D_ERROR : D_OTHER,
992 "link_ea add '%.*s' failed %d "DFID"\n",
993 newlname->ln_namelen, newlname->ln_name,
994 rc, PFID(mdd_object_fid(mdd_obj)));
995 else if (newpfid == NULL)
996 CDEBUG(error ? D_ERROR : D_OTHER,
997 "link_ea del '%.*s' failed %d "DFID"\n",
998 oldlname->ln_namelen, oldlname->ln_name,
999 rc, PFID(mdd_object_fid(mdd_obj)));
1001 CDEBUG(error ? D_ERROR : D_OTHER,
1002 "link_ea rename '%.*s'->'%.*s' failed %d "
1004 oldlname->ln_namelen, oldlname->ln_name,
1005 newlname->ln_namelen, newlname->ln_name,
1006 rc, PFID(mdd_object_fid(mdd_obj)));
1009 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1010 /* if we vmalloced a large buffer drop it */
1011 lu_buf_free(ldata->ld_buf);
1016 static inline int mdd_links_add(const struct lu_env *env,
1017 struct mdd_object *mdd_obj,
1018 const struct lu_fid *pfid,
1019 const struct lu_name *lname,
1020 struct thandle *handle,
1021 struct linkea_data *data, int first)
1023 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1024 pfid, lname, handle, data, first, 0);
1027 static inline int mdd_links_del(const struct lu_env *env,
1028 struct mdd_object *mdd_obj,
1029 const struct lu_fid *pfid,
1030 const struct lu_name *lname,
1031 struct thandle *handle)
1033 return mdd_links_rename(env, mdd_obj, pfid, lname,
1034 NULL, NULL, handle, NULL, 0, 0);
1037 /** Read the link EA into a temp buffer.
1038 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1039 * A pointer to the buffer is stored in \a ldata::ld_buf.
1041 * \retval 0 or error
1043 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1044 struct linkea_data *ldata)
1048 /* First try a small buf */
1049 LASSERT(env != NULL);
1050 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1052 if (ldata->ld_buf->lb_buf == NULL)
1055 if (!mdd_object_exists(mdd_obj))
1058 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1060 if (rc == -ERANGE) {
1061 /* Buf was too small, figure out what we need. */
1062 lu_buf_free(ldata->ld_buf);
1063 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1064 XATTR_NAME_LINK, BYPASS_CAPA);
1067 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1068 if (ldata->ld_buf->lb_buf == NULL)
1070 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1071 XATTR_NAME_LINK, BYPASS_CAPA);
1076 return linkea_init(ldata);
1079 /** Read the link EA into a temp buffer.
1080 * Uses the name_buf since it is generally large.
1081 * \retval IS_ERR err
1082 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1084 struct lu_buf *mdd_links_get(const struct lu_env *env,
1085 struct mdd_object *mdd_obj)
1087 struct linkea_data ldata = { 0 };
1090 rc = mdd_links_read(env, mdd_obj, &ldata);
1091 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1094 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1095 struct linkea_data *ldata, struct thandle *handle)
1097 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1098 ldata->ld_leh->leh_len);
1099 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1100 mdd_object_capa(env, mdd_obj));
1103 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1104 struct thandle *handle, struct linkea_data *ldata)
1110 if (ldata != NULL && ldata->ld_lee != NULL) {
1111 ea_len = ldata->ld_leh->leh_len;
1112 linkea = ldata->ld_buf->lb_buf;
1114 ea_len = DEFAULT_LINKEA_SIZE;
1118 /* XXX: max size? */
1119 rc = mdo_declare_xattr_set(env, mdd_obj,
1120 mdd_buf_get_const(env, linkea, ea_len),
1121 XATTR_NAME_LINK, 0, handle);
1125 static inline int mdd_declare_links_del(const struct lu_env *env,
1126 struct mdd_object *c,
1127 struct thandle *handle)
1131 /* For directory, the linkEA will be removed together
1132 * with the object. */
1133 if (!S_ISDIR(mdd_object_type(c)))
1134 rc = mdd_declare_links_add(env, c, handle, NULL);
1139 static int mdd_declare_link(const struct lu_env *env,
1140 struct mdd_device *mdd,
1141 struct mdd_object *p,
1142 struct mdd_object *c,
1143 const struct lu_name *name,
1144 struct thandle *handle,
1146 struct linkea_data *data)
1150 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1154 rc = mdo_declare_ref_add(env, c, handle);
1158 la->la_valid = LA_CTIME | LA_MTIME;
1159 rc = mdo_declare_attr_set(env, p, la, handle);
1163 la->la_valid = LA_CTIME;
1164 rc = mdo_declare_attr_set(env, c, la, handle);
1168 rc = mdd_declare_links_add(env, c, handle, data);
1172 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1177 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1178 struct md_object *src_obj, const struct lu_name *lname,
1181 const char *name = lname->ln_name;
1182 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1183 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1184 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1185 struct mdd_device *mdd = mdo2mdd(src_obj);
1186 struct dynlock_handle *dlh;
1187 struct thandle *handle;
1188 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1192 handle = mdd_trans_create(env, mdd);
1194 GOTO(out_pending, rc = PTR_ERR(handle));
1196 memset(ldata, 0, sizeof(*ldata));
1198 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1199 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1201 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1206 rc = mdd_trans_start(env, mdd, handle);
1210 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
1212 GOTO(out_trans, rc = -ENOMEM);
1213 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1215 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
1217 GOTO(out_unlock, rc);
1219 rc = mdo_ref_add(env, mdd_sobj, handle);
1221 GOTO(out_unlock, rc);
1224 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1226 mdd_object_capa(env, mdd_tobj));
1228 mdo_ref_del(env, mdd_sobj, handle);
1229 GOTO(out_unlock, rc);
1232 la->la_valid = LA_CTIME | LA_MTIME;
1233 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1235 GOTO(out_unlock, rc);
1237 la->la_valid = LA_CTIME;
1238 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1240 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1241 mdo2fid(mdd_tobj), lname, 0, 0,
1244 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1245 lname, handle, ldata, 0);
1246 /* The failure of links_add should not cause the link
1247 * failure, reset rc here */
1252 mdd_write_unlock(env, mdd_sobj);
1253 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1256 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1257 mdd_tobj, lname, handle);
1259 mdd_trans_stop(env, mdd, rc, handle);
1261 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1262 /* if we vmalloced a large buffer drop it */
1263 lu_buf_free(ldata->ld_buf);
1268 int mdd_declare_finish_unlink(const struct lu_env *env,
1269 struct mdd_object *obj,
1271 struct thandle *handle)
1275 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1279 return mdo_declare_destroy(env, obj, handle);
1282 /* caller should take a lock before calling */
1283 int mdd_finish_unlink(const struct lu_env *env,
1284 struct mdd_object *obj, struct md_attr *ma,
1288 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1291 LASSERT(mdd_write_locked(env, obj) != 0);
1293 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1294 obj->mod_flags |= DEAD_OBJ;
1296 /* add new orphan and the object
1297 * will be deleted during mdd_close() */
1298 if (obj->mod_count) {
1299 rc = __mdd_orphan_add(env, obj, th);
1301 CDEBUG(D_HA, "Object "DFID" is inserted into "
1302 "orphan list, open count = %d\n",
1303 PFID(mdd_object_fid(obj)),
1306 CERROR("Object "DFID" fail to be an orphan, "
1307 "open count = %d, maybe cause failed "
1309 PFID(mdd_object_fid(obj)),
1312 rc = mdo_destroy(env, obj, th);
1321 * has mdd_write_lock on cobj already, but not on pobj yet
1323 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1324 struct mdd_object *cobj, struct lu_attr *cattr)
1329 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1334 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1335 struct mdd_object *p, struct mdd_object *c,
1336 const struct lu_name *name, struct md_attr *ma,
1337 struct thandle *handle, int no_name)
1339 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1342 if (likely(no_name == 0)) {
1343 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1348 rc = mdo_declare_ref_del(env, p, handle);
1352 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1353 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1354 la->la_valid = LA_CTIME | LA_MTIME;
1355 rc = mdo_declare_attr_set(env, p, la, handle);
1360 rc = mdo_declare_ref_del(env, c, handle);
1364 rc = mdo_declare_ref_del(env, c, handle);
1368 la->la_valid = LA_CTIME;
1369 rc = mdo_declare_attr_set(env, c, la, handle);
1373 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1377 rc = mdd_declare_links_del(env, c, handle);
1381 /* FIXME: need changelog for remove entry */
1382 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1389 * Delete name entry and the object.
1390 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1391 * does not exist for this object, and it could only happen during resending
1392 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1393 * is also needed in this case(needed by changelog), so we have to add another
1394 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1395 * the ENOENT failure should be able to be fixed by redo mechanism.
1397 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1398 struct md_object *cobj, const struct lu_name *lname,
1399 struct md_attr *ma, int no_name)
1401 const char *name = lname->ln_name;
1402 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1403 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1404 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1405 struct mdd_object *mdd_cobj = NULL;
1406 struct mdd_device *mdd = mdo2mdd(pobj);
1407 struct dynlock_handle *dlh;
1408 struct thandle *handle;
1412 /* cobj == NULL means only delete name entry */
1413 if (likely(cobj != NULL)) {
1414 mdd_cobj = md2mdd_obj(cobj);
1415 if (mdd_object_exists(mdd_cobj) == 0)
1417 /* currently it is assume, it could only delete
1418 * name entry of remote directory */
1422 handle = mdd_trans_create(env, mdd);
1424 RETURN(PTR_ERR(handle));
1426 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1427 lname, ma, handle, no_name);
1431 rc = mdd_trans_start(env, mdd, handle);
1435 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1437 GOTO(stop, rc = -ENOMEM);
1439 if (likely(mdd_cobj != NULL)) {
1440 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1443 rc = mdd_la_get(env, mdd_cobj, cattr,
1444 mdd_object_capa(env, mdd_cobj));
1448 is_dir = S_ISDIR(cattr->la_mode);
1452 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1456 if (likely(no_name == 0)) {
1457 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1458 mdd_object_capa(env, mdd_pobj));
1463 if (likely(mdd_cobj != NULL)) {
1464 rc = mdo_ref_del(env, mdd_cobj, handle);
1466 __mdd_index_insert_only(env, mdd_pobj,
1469 mdd_object_capa(env, mdd_pobj));
1475 mdo_ref_del(env, mdd_cobj, handle);
1477 /* fetch updated nlink */
1478 rc = mdd_la_get(env, mdd_cobj, cattr,
1479 mdd_object_capa(env, mdd_cobj));
1484 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1485 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1487 la->la_valid = LA_CTIME | LA_MTIME;
1488 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1492 /* Enough for only unlink the entry */
1493 if (unlikely(mdd_cobj == NULL)) {
1494 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1498 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1499 /* update ctime of an unlinked file only if it is still
1500 * opened or a link still exists */
1501 la->la_valid = LA_CTIME;
1502 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1507 /* XXX: this transfer to ma will be removed with LOD/OSP */
1508 ma->ma_attr = *cattr;
1509 ma->ma_valid |= MA_INODE;
1510 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1512 /* fetch updated nlink */
1514 rc = mdd_la_get(env, mdd_cobj, cattr,
1515 mdd_object_capa(env, mdd_cobj));
1518 /* old files may not have link ea; ignore errors */
1519 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1521 /* if object is removed then we can't get its attrs, use last get */
1522 if (cattr->la_nlink == 0) {
1523 ma->ma_attr = *cattr;
1524 ma->ma_valid |= MA_INODE;
1528 mdd_write_unlock(env, mdd_cobj);
1529 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1533 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1534 if ((ma->ma_valid & MA_HSM) &&
1535 (ma->ma_hsm.mh_flags & HS_EXISTS))
1536 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1538 rc = mdd_changelog_ns_store(env, mdd,
1539 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1540 mdd_cobj, mdd_pobj, lname, handle);
1544 mdd_trans_stop(env, mdd, rc, handle);
1550 * The permission has been checked when obj created, no need check again.
1552 static int mdd_cd_sanity_check(const struct lu_env *env,
1553 struct mdd_object *obj)
1558 if (!obj || mdd_is_dead_obj(obj))
1565 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1566 struct md_object *cobj, const struct md_op_spec *spec,
1569 struct mdd_device *mdd = mdo2mdd(cobj);
1570 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1571 struct mdd_object *son = md2mdd_obj(cobj);
1572 struct thandle *handle;
1573 const struct lu_buf *buf;
1574 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1578 rc = mdd_cd_sanity_check(env, son);
1582 if (!md_should_create(spec->sp_cr_flags))
1586 * there are following use cases for this function:
1587 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1588 * striping can be specified or not
1591 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1595 /* calling ->ah_make_hint() is used to transfer information from parent */
1596 mdd_object_make_hint(env, mdd_pobj, son, attr);
1598 handle = mdd_trans_create(env, mdd);
1600 GOTO(out_free, rc = PTR_ERR(handle));
1603 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1604 * Should this be fixed?
1606 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1607 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1608 spec->sp_cr_flags, spec->no_create);
1610 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1611 /* replay case or lfs setstripe */
1612 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1613 spec->u.sp_ea.eadatalen);
1618 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1619 XATTR_NAME_LOV, 0, handle);
1623 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1627 rc = mdd_trans_start(env, mdd, handle);
1631 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1632 0, handle, mdd_object_capa(env, son));
1637 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1640 mdd_trans_stop(env, mdd, rc, handle);
1645 /* Get fid from name and parent */
1647 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1648 const struct lu_name *lname, struct lu_fid* fid, int mask)
1650 const char *name = lname->ln_name;
1651 const struct dt_key *key = (const struct dt_key *)name;
1652 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1653 struct mdd_device *m = mdo2mdd(pobj);
1654 struct dt_object *dir = mdd_object_child(mdd_obj);
1658 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1661 if (mdd_object_remote(mdd_obj)) {
1662 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1663 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1664 } else if (!mdd_object_exists(mdd_obj)) {
1668 /* The common filename length check. */
1669 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1670 RETURN(-ENAMETOOLONG);
1672 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1677 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1678 dt_try_as_dir(env, dir))) {
1680 rc = dir->do_index_ops->dio_lookup(env, dir,
1681 (struct dt_rec *)fid, key,
1682 mdd_object_capa(env, mdd_obj));
1693 static int mdd_declare_object_initialize(const struct lu_env *env,
1694 struct mdd_object *parent,
1695 struct mdd_object *child,
1696 struct lu_attr *attr,
1697 struct thandle *handle,
1698 struct linkea_data *ldata)
1704 * inode mode has been set in creation time, and it's based on umask,
1705 * la_mode and acl, don't set here again! (which will go wrong
1706 * because below function doesn't consider umask).
1707 * I'd suggest set all object attributes in creation time, see above.
1709 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1710 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1711 rc = mdo_declare_attr_set(env, child, attr, handle);
1712 attr->la_valid |= LA_MODE | LA_TYPE;
1713 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1714 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1717 rc = mdo_declare_ref_add(env, child, handle);
1719 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1724 mdd_declare_links_add(env, child, handle, ldata);
1729 static int mdd_object_initialize(const struct lu_env *env,
1730 const struct lu_fid *pfid,
1731 const struct lu_name *lname,
1732 struct mdd_object *child,
1733 struct lu_attr *attr, struct thandle *handle,
1734 const struct md_op_spec *spec,
1735 struct linkea_data *ldata)
1741 * Update attributes for child.
1744 * (1) the valid bits should be converted between Lustre and Linux;
1745 * (2) maybe, the child attributes should be set in OSD when creation.
1748 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1749 /* arguments are supposed to stay the same */
1750 if (S_ISDIR(attr->la_mode)) {
1751 /* Add "." and ".." for newly created dir */
1752 mdo_ref_add(env, child, handle);
1753 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1754 dot, handle, BYPASS_CAPA);
1756 rc = __mdd_index_insert_only(env, child, pfid,
1760 mdo_ref_del(env, child, handle);
1764 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1769 /* has not lock on pobj yet */
1770 static int mdd_create_sanity_check(const struct lu_env *env,
1771 struct md_object *pobj,
1772 struct lu_attr *pattr,
1773 const struct lu_name *lname,
1774 struct lu_attr *cattr,
1775 struct md_op_spec *spec)
1777 struct mdd_thread_info *info = mdd_env_info(env);
1778 struct lu_fid *fid = &info->mti_fid;
1779 struct mdd_object *obj = md2mdd_obj(pobj);
1780 struct mdd_device *m = mdo2mdd(pobj);
1785 if (mdd_is_dead_obj(obj))
1789 * In some cases this lookup is not needed - we know before if name
1790 * exists or not because MDT performs lookup for it.
1791 * name length check is done in lookup.
1793 if (spec->sp_cr_lookup) {
1795 * Check if the name already exist, though it will be checked in
1796 * _index_insert also, for avoiding rolling back if exists
1799 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1800 MAY_WRITE | MAY_EXEC);
1802 RETURN(rc ? : -EEXIST);
1805 * Check WRITE permission for the parent.
1806 * EXEC permission have been checked
1807 * when lookup before create already.
1809 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1816 if (pattr->la_mode & S_ISGID) {
1817 cattr->la_gid = pattr->la_gid;
1818 if (S_ISDIR(cattr->la_mode)) {
1819 cattr->la_mode |= S_ISGID;
1820 cattr->la_valid |= LA_MODE;
1824 switch (cattr->la_mode & S_IFMT) {
1826 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1828 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1829 RETURN(-ENAMETOOLONG);
1848 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1849 struct mdd_object *p, struct mdd_object *c,
1850 const struct lu_name *name,
1851 struct lu_attr *attr,
1852 struct thandle *handle,
1853 const struct md_op_spec *spec,
1854 struct linkea_data *ldata,
1855 struct lu_buf *def_acl_buf,
1856 struct lu_buf *acl_buf)
1860 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1864 #ifdef CONFIG_FS_POSIX_ACL
1865 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1866 /* if dir, then can inherit default ACl */
1867 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1868 XATTR_NAME_ACL_DEFAULT,
1874 if (acl_buf->lb_len > 0) {
1875 rc = mdo_declare_attr_set(env, c, attr, handle);
1879 rc = mdo_declare_xattr_set(env, c, acl_buf,
1880 XATTR_NAME_ACL_ACCESS, 0, handle);
1886 if (S_ISDIR(attr->la_mode)) {
1887 rc = mdo_declare_ref_add(env, p, handle);
1892 rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1896 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1897 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1899 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1900 name->ln_name, handle);
1904 /* replay case, create LOV EA from client data */
1905 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1906 const struct lu_buf *buf;
1908 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1909 spec->u.sp_ea.eadatalen);
1910 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1916 if (S_ISLNK(attr->la_mode)) {
1917 rc = dt_declare_record_write(env, mdd_object_child(c),
1918 strlen(spec->u.sp_symname), 0,
1924 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1925 rc = mdo_declare_attr_set(env, p, attr, handle);
1930 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1938 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1939 struct lu_attr *la, struct lu_buf *def_acl_buf,
1940 struct lu_buf *acl_buf)
1945 if (S_ISLNK(la->la_mode)) {
1946 acl_buf->lb_len = 0;
1947 def_acl_buf->lb_len = 0;
1951 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1952 rc = mdo_xattr_get(env, pobj, def_acl_buf,
1953 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1954 mdd_read_unlock(env, pobj);
1956 /* If there are default ACL, fix mode/ACL by default ACL */
1957 def_acl_buf->lb_len = rc;
1958 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
1959 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
1960 acl_buf->lb_len = rc;
1961 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1964 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1965 /* If there are no default ACL, fix mode by mask */
1966 struct lu_ucred *uc = lu_ucred(env);
1968 /* The create triggered by MDT internal events, such as
1969 * LFSCK reset, will not contain valid "uc". */
1970 if (unlikely(uc != NULL))
1971 la->la_mode &= ~uc->uc_umask;
1973 acl_buf->lb_len = 0;
1974 def_acl_buf->lb_len = 0;
1981 * Create object and insert it into namespace.
1983 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1984 const struct lu_name *lname, struct md_object *child,
1985 struct md_op_spec *spec, struct md_attr* ma)
1987 struct mdd_thread_info *info = mdd_env_info(env);
1988 struct lu_attr *la = &info->mti_la_for_fix;
1989 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1990 struct mdd_object *son = md2mdd_obj(child);
1991 struct mdd_device *mdd = mdo2mdd(pobj);
1992 struct lu_attr *attr = &ma->ma_attr;
1993 struct thandle *handle;
1994 struct lu_attr *pattr = &info->mti_pattr;
1995 struct lu_buf acl_buf;
1996 struct lu_buf def_acl_buf;
1997 struct linkea_data *ldata = &info->mti_link_data;
1998 struct dynlock_handle *dlh;
1999 const char *name = lname->ln_name;
2000 int rc, created = 0, initialized = 0, inserted = 0;
2004 * Two operations have to be performed:
2006 * - an allocation of a new object (->do_create()), and
2008 * - an insertion into a parent index (->dio_insert()).
2010 * Due to locking, operation order is not important, when both are
2011 * successful, *but* error handling cases are quite different:
2013 * - if insertion is done first, and following object creation fails,
2014 * insertion has to be rolled back, but this operation might fail
2015 * also leaving us with dangling index entry.
2017 * - if creation is done first, is has to be undone if insertion
2018 * fails, leaving us with leaked space, which is neither good, nor
2021 * It seems that creation-first is simplest solution, but it is
2022 * sub-optimal in the frequent
2027 * case, because second mkdir is bound to create object, only to
2028 * destroy it immediately.
2030 * To avoid this follow local file systems that do double lookup:
2032 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2034 * 1. create (mdd_object_create_internal())
2036 * 2. insert (__mdd_index_insert(), lookup again)
2039 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2043 /* Sanity checks before big job. */
2044 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2048 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2049 GOTO(out_free, rc = -EINPROGRESS);
2051 acl_buf.lb_buf = info->mti_xattr_buf;
2052 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2053 def_acl_buf.lb_buf = info->mti_key;
2054 def_acl_buf.lb_len = sizeof(info->mti_key);
2055 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2059 mdd_object_make_hint(env, mdd_pobj, son, attr);
2061 handle = mdd_trans_create(env, mdd);
2063 GOTO(out_free, rc = PTR_ERR(handle));
2065 memset(ldata, 0, sizeof(*ldata));
2066 mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2067 lname, 1, 0, ldata);
2069 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2070 handle, spec, ldata, &def_acl_buf, &acl_buf);
2074 rc = mdd_trans_start(env, mdd, handle);
2078 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2080 GOTO(out_trans, rc = -ENOMEM);
2082 mdd_write_lock(env, son, MOR_TGT_CHILD);
2083 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
2085 mdd_write_unlock(env, son);
2091 #ifdef CONFIG_FS_POSIX_ACL
2092 if (def_acl_buf.lb_len > 0 && S_ISDIR(attr->la_mode)) {
2093 /* set default acl */
2094 rc = mdo_xattr_set(env, son, &def_acl_buf,
2095 XATTR_NAME_ACL_DEFAULT, 0,
2096 handle, BYPASS_CAPA);
2098 mdd_write_unlock(env, son);
2102 /* set its own acl */
2103 if (acl_buf.lb_len > 0) {
2104 rc = mdo_xattr_set(env, son, &acl_buf,
2105 XATTR_NAME_ACL_ACCESS,
2106 0, handle, BYPASS_CAPA);
2108 mdd_write_unlock(env, son);
2114 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2115 son, attr, handle, spec, ldata);
2118 * in case of replay we just set LOVEA provided by the client
2119 * XXX: I think it would be interesting to try "old" way where
2120 * MDT calls this xattr_set(LOV) in a different transaction.
2121 * probably this way we code can be made better.
2123 if (rc == 0 && (spec->no_create ||
2124 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
2125 const struct lu_buf *buf;
2127 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2128 spec->u.sp_ea.eadatalen);
2129 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2133 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2134 rc = __mdd_orphan_add(env, son, handle);
2136 mdd_write_unlock(env, son);
2140 * Object has no links, so it will be destroyed when last
2141 * reference is released. (XXX not now.)
2147 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
2148 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2149 name, S_ISDIR(attr->la_mode), handle,
2150 mdd_object_capa(env, mdd_pobj));
2157 if (S_ISLNK(attr->la_mode)) {
2158 struct lu_ucred *uc = lu_ucred_assert(env);
2159 struct dt_object *dt = mdd_object_child(son);
2160 const char *target_name = spec->u.sp_symname;
2161 int sym_len = strlen(target_name);
2162 const struct lu_buf *buf;
2165 buf = mdd_buf_get_const(env, target_name, sym_len);
2166 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2167 mdd_object_capa(env, son),
2169 CFS_CAP_SYS_RESOURCE_MASK);
2174 GOTO(cleanup, rc = -EFAULT);
2177 /* volatile file creation does not update parent directory times */
2178 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2179 GOTO(cleanup, rc = 0);
2181 /* update parent directory mtime/ctime */
2183 la->la_valid = LA_CTIME | LA_MTIME;
2184 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
2190 if (rc != 0 && created != 0) {
2193 if (inserted != 0) {
2194 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2195 rc2 = __mdd_orphan_del(env, son, handle);
2197 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2198 S_ISDIR(attr->la_mode),
2199 handle, BYPASS_CAPA);
2204 mdd_write_lock(env, son, MOR_TGT_CHILD);
2205 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2206 /* Drop the reference, no need to delete "."/"..",
2207 * because the object to be destroied directly. */
2208 rc2 = mdo_ref_del(env, son, handle);
2210 mdd_write_unlock(env, son);
2215 rc2 = mdo_ref_del(env, son, handle);
2217 mdd_write_unlock(env, son);
2221 mdo_destroy(env, son, handle);
2222 mdd_write_unlock(env, son);
2225 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2227 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2228 rc = mdd_changelog_ns_store(env, mdd,
2229 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2230 S_ISREG(attr->la_mode) ? CL_CREATE :
2231 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2232 0, son, mdd_pobj, lname, handle);
2234 mdd_trans_stop(env, mdd, rc, handle);
2236 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2237 /* if we vmalloced a large buffer drop it */
2238 lu_buf_free(ldata->ld_buf);
2240 /* The child object shouldn't be cached anymore */
2242 set_bit(LU_OBJECT_HEARD_BANSHEE,
2243 &child->mo_lu.lo_header->loh_flags);
2248 * Get locks on parents in proper order
2249 * RETURN: < 0 - error, rename_order if successful
2257 static int mdd_rename_order(const struct lu_env *env,
2258 struct mdd_device *mdd,
2259 struct mdd_object *src_pobj,
2260 struct mdd_object *tgt_pobj)
2262 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2266 if (src_pobj == tgt_pobj)
2267 RETURN(MDD_RN_SAME);
2269 /* compared the parent child relationship of src_p&tgt_p */
2270 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2272 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2275 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2288 /* has not mdd_write{read}_lock on any obj yet. */
2289 static int mdd_rename_sanity_check(const struct lu_env *env,
2290 struct mdd_object *src_pobj,
2291 struct mdd_object *tgt_pobj,
2292 struct mdd_object *sobj,
2293 struct mdd_object *tobj,
2294 struct lu_attr *so_attr,
2295 struct lu_attr *tg_attr)
2300 /* XXX: when get here, sobj must NOT be NULL,
2301 * the other case has been processed in cld_rename
2302 * before mdd_rename and enable MDS_PERM_BYPASS. */
2305 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2309 /* XXX: when get here, "tobj == NULL" means tobj must
2310 * NOT exist (neither on remote MDS, such case has been
2311 * processed in cld_rename before mdd_rename and enable
2313 * So check may_create, but not check may_unlink. */
2315 rc = mdd_may_create(env, tgt_pobj, NULL,
2316 (src_pobj != tgt_pobj), 0);
2318 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2319 (src_pobj != tgt_pobj), 1);
2321 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2322 S_ISDIR(so_attr->la_mode))
2323 rc = __mdd_may_link(env, tgt_pobj);
2328 static int mdd_declare_rename(const struct lu_env *env,
2329 struct mdd_device *mdd,
2330 struct mdd_object *mdd_spobj,
2331 struct mdd_object *mdd_tpobj,
2332 struct mdd_object *mdd_sobj,
2333 struct mdd_object *mdd_tobj,
2334 const struct lu_name *tname,
2335 const struct lu_name *sname,
2337 struct linkea_data *ldata,
2338 struct thandle *handle)
2340 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2343 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2344 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2350 /* name from source dir */
2351 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2355 /* .. from source child */
2356 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2357 /* source child can be directory,
2358 * counted by source dir's nlink */
2359 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2362 if (mdd_spobj != mdd_tpobj) {
2363 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2368 rc = mdo_declare_index_insert(env, mdd_sobj,
2374 /* new target child can be directory,
2375 * counted by target dir's nlink */
2376 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2382 la->la_valid = LA_CTIME | LA_MTIME;
2383 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2387 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2391 la->la_valid = LA_CTIME;
2392 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2396 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2401 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2402 tname->ln_name, handle);
2406 /* name from target dir (old name), we declare it unconditionally
2407 * as mdd_rename() calls delete unconditionally as well. so just
2408 * to balance declarations vs calls to change ... */
2409 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2413 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2414 /* delete target child in target parent directory */
2415 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2419 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2420 /* target child can be directory,
2421 * delete "." reference in target child directory */
2422 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2426 /* delete ".." reference in target parent directory */
2427 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2432 la->la_valid = LA_CTIME;
2433 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2437 mdd_declare_links_del(env, mdd_tobj, handle);
2441 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2446 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2453 /* src object can be remote that is why we use only fid and type of object */
2454 static int mdd_rename(const struct lu_env *env,
2455 struct md_object *src_pobj, struct md_object *tgt_pobj,
2456 const struct lu_fid *lf, const struct lu_name *lsname,
2457 struct md_object *tobj, const struct lu_name *ltname,
2460 const char *sname = lsname->ln_name;
2461 const char *tname = ltname->ln_name;
2462 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2463 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2464 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2465 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2466 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2467 struct mdd_device *mdd = mdo2mdd(src_pobj);
2468 struct mdd_object *mdd_sobj = NULL; /* source object */
2469 struct mdd_object *mdd_tobj = NULL;
2470 struct dynlock_handle *sdlh = NULL, *tdlh = NULL;
2471 struct thandle *handle;
2472 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2473 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2474 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2477 bool tobj_locked = 0;
2478 unsigned cl_flags = 0;
2483 mdd_tobj = md2mdd_obj(tobj);
2485 mdd_sobj = mdd_object_find(env, mdd, lf);
2487 rc = mdd_la_get(env, mdd_sobj, so_attr,
2488 mdd_object_capa(env, mdd_sobj));
2490 GOTO(out_pending, rc);
2493 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2494 mdd_object_capa(env, mdd_tobj));
2496 GOTO(out_pending, rc);
2499 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2500 mdd_tobj, so_attr, tg_attr);
2502 GOTO(out_pending, rc);
2504 handle = mdd_trans_create(env, mdd);
2506 GOTO(out_pending, rc = PTR_ERR(handle));
2508 memset(ldata, 0, sizeof(*ldata));
2509 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2510 ltname, 1, 0, ldata);
2511 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2512 mdd_tobj, lsname, ltname, ma, ldata, handle);
2516 rc = mdd_trans_start(env, mdd, handle);
2520 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2521 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2523 GOTO(cleanup_unlocked, rc);
2525 /* Get locks in determined order */
2526 if (rc == MDD_RN_SAME) {
2527 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2528 sname, MOR_SRC_PARENT);
2529 /* check hashes to determine do we need one lock or two */
2530 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2531 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2535 } else if (rc == MDD_RN_SRCTGT) {
2536 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2537 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2539 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2540 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2542 if (sdlh == NULL || tdlh == NULL)
2543 GOTO(cleanup, rc = -ENOMEM);
2545 is_dir = S_ISDIR(so_attr->la_mode);
2547 /* Remove source name from source directory */
2548 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2549 mdd_object_capa(env, mdd_spobj));
2553 /* "mv dir1 dir2" needs "dir1/.." link update */
2554 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2555 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2556 mdd_object_capa(env, mdd_sobj));
2558 GOTO(fixup_spobj2, rc);
2560 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2561 handle, mdd_object_capa(env, mdd_sobj));
2563 GOTO(fixup_spobj, rc);
2566 /* Remove target name from target directory
2567 * Here tobj can be remote one, so we do index_delete unconditionally
2568 * and -ENOENT is allowed.
2570 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2571 mdd_object_capa(env, mdd_tpobj));
2574 /* tname might been renamed to something else */
2575 GOTO(fixup_spobj, rc);
2578 GOTO(fixup_spobj, rc);
2581 /* Insert new fid with target name into target dir */
2582 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2583 mdd_object_capa(env, mdd_tpobj));
2585 GOTO(fixup_tpobj, rc);
2587 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2588 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2590 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2592 la->la_valid = LA_CTIME;
2593 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2595 GOTO(fixup_tpobj, rc);
2598 /* Remove old target object
2599 * For tobj is remote case cmm layer has processed
2600 * and set tobj to NULL then. So when tobj is NOT NULL,
2601 * it must be local one.
2603 if (tobj && mdd_object_exists(mdd_tobj)) {
2604 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2606 if (mdd_is_dead_obj(mdd_tobj)) {
2607 /* shld not be dead, something is wrong */
2608 CERROR("tobj is dead, something is wrong\n");
2612 mdo_ref_del(env, mdd_tobj, handle);
2614 /* Remove dot reference. */
2615 if (S_ISDIR(tg_attr->la_mode))
2616 mdo_ref_del(env, mdd_tobj, handle);
2619 /* fetch updated nlink */
2620 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2621 mdd_object_capa(env, mdd_tobj));
2623 CERROR("%s: Failed to get nlink for tobj "
2625 mdd2obd_dev(mdd)->obd_name,
2626 PFID(tpobj_fid), rc);
2627 GOTO(fixup_tpobj, rc);
2630 la->la_valid = LA_CTIME;
2631 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2633 CERROR("%s: Failed to set ctime for tobj "
2635 mdd2obd_dev(mdd)->obd_name,
2636 PFID(tpobj_fid), rc);
2637 GOTO(fixup_tpobj, rc);
2640 /* XXX: this transfer to ma will be removed with LOD/OSP */
2641 ma->ma_attr = *tg_attr;
2642 ma->ma_valid |= MA_INODE;
2643 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2645 CERROR("%s: Failed to unlink tobj "
2647 mdd2obd_dev(mdd)->obd_name,
2648 PFID(tpobj_fid), rc);
2649 GOTO(fixup_tpobj, rc);
2652 /* fetch updated nlink */
2653 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2654 mdd_object_capa(env, mdd_tobj));
2656 CERROR("%s: Failed to get nlink for tobj "
2658 mdd2obd_dev(mdd)->obd_name,
2659 PFID(tpobj_fid), rc);
2660 GOTO(fixup_tpobj, rc);
2662 /* XXX: this transfer to ma will be removed with LOD/OSP */
2663 ma->ma_attr = *tg_attr;
2664 ma->ma_valid |= MA_INODE;
2666 if (tg_attr->la_nlink == 0)
2667 cl_flags |= CLF_RENAME_LAST;
2670 la->la_valid = LA_CTIME | LA_MTIME;
2671 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2673 GOTO(fixup_tpobj, rc);
2675 if (mdd_spobj != mdd_tpobj) {
2676 la->la_valid = LA_CTIME | LA_MTIME;
2677 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2681 if (rc == 0 && mdd_sobj) {
2682 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2683 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2684 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2687 /* Old files might not have EA entry */
2688 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2689 lsname, handle, NULL, 0);
2690 mdd_write_unlock(env, mdd_sobj);
2691 /* We don't fail the transaction if the link ea can't be
2692 updated -- fid2path will use alternate lookup method. */
2700 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2703 CWARN("tp obj fix error %d\n",rc2);
2705 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2706 !mdd_is_dead_obj(mdd_tobj)) {
2708 mdo_ref_add(env, mdd_tobj, handle);
2710 mdo_ref_add(env, mdd_tobj, handle);
2713 rc2 = __mdd_index_insert(env, mdd_tpobj,
2714 mdo2fid(mdd_tobj), tname,
2719 CWARN("tp obj fix error %d\n",rc2);
2724 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2725 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2729 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2730 mdd2obd_dev(mdd)->obd_name, rc2);
2733 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2734 dotdot, handle, BYPASS_CAPA);
2736 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2737 mdd2obd_dev(mdd)->obd_name, rc2);
2742 rc2 = __mdd_index_insert(env, mdd_spobj,
2743 lf, sname, is_dir, handle, BYPASS_CAPA);
2745 CWARN("sp obj fix error %d\n",rc2);
2749 mdd_write_unlock(env, mdd_tobj);
2750 if (likely(tdlh) && sdlh != tdlh)
2751 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2753 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2756 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2757 mdd_tobj, tpobj_fid, lf,
2758 spobj_fid, ltname, lsname,
2762 mdd_trans_stop(env, mdd, rc, handle);
2764 mdd_object_put(env, mdd_sobj);
2768 const struct md_dir_operations mdd_dir_ops = {
2769 .mdo_is_subdir = mdd_is_subdir,
2770 .mdo_lookup = mdd_lookup,
2771 .mdo_create = mdd_create,
2772 .mdo_rename = mdd_rename,
2773 .mdo_link = mdd_link,
2774 .mdo_unlink = mdd_unlink,
2775 .mdo_create_data = mdd_create_data,