4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 /* Get FID from name and parent */
62 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
63 const struct lu_name *lname, struct lu_fid* fid, int mask)
65 const char *name = lname->ln_name;
66 const struct dt_key *key = (const struct dt_key *)name;
67 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
68 struct mdd_device *m = mdo2mdd(pobj);
69 struct dt_object *dir = mdd_object_child(mdd_obj);
73 if (unlikely(mdd_is_dead_obj(mdd_obj)))
76 if (mdd_object_remote(mdd_obj)) {
77 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
78 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
79 } else if (!mdd_object_exists(mdd_obj)) {
83 /* The common filename length check. */
84 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
85 RETURN(-ENAMETOOLONG);
87 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
92 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
93 dt_try_as_dir(env, dir))) {
95 rc = dir->do_index_ops->dio_lookup(env, dir,
96 (struct dt_rec *)fid, key,
97 mdd_object_capa(env, mdd_obj));
108 int mdd_lookup(const struct lu_env *env,
109 struct md_object *pobj, const struct lu_name *lname,
110 struct lu_fid* fid, struct md_op_spec *spec)
114 rc = __mdd_lookup(env, pobj, lname, fid, MAY_EXEC);
118 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
121 return __mdd_lookup(env, &obj->mod_obj, &lname_dotdot, fid, 0);
125 * For root fid use special function, which does not compare version component
126 * of fid. Version component is different for root fids on all MDTs.
128 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
130 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
131 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
135 * return 1: if lf is the fid of the ancestor of p1;
138 * return -EREMOTE: if remote object is found, in this
139 * case fid of remote object is saved to @pf;
141 * otherwise: values < 0, errors.
143 static int mdd_is_parent(const struct lu_env *env,
144 struct mdd_device *mdd,
145 struct mdd_object *p1,
146 const struct lu_fid *lf,
149 struct mdd_object *parent = NULL;
154 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
155 pfid = &mdd_env_info(env)->mti_fid;
157 /* Check for root first. */
158 if (mdd_is_root(mdd, mdo2fid(p1)))
162 /* this is done recursively, bypass capa for each obj */
163 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
164 rc = mdd_parent_fid(env, p1, pfid);
167 if (mdd_is_root(mdd, pfid))
169 if (lu_fid_eq(pfid, lf))
172 mdd_object_put(env, parent);
174 parent = mdd_object_find(env, mdd, pfid);
175 if (IS_ERR(parent)) {
176 GOTO(out, rc = PTR_ERR(parent));
177 } else if (mdd_object_remote(parent)) {
178 /*FIXME: Because of the restriction of rename in Phase I.
179 * If the parent is remote, we just assumed lf is not the
180 * parent of P1 for now */
187 if (parent && !IS_ERR(parent))
188 mdd_object_put(env, parent);
193 * No permission check is needed.
195 * returns 1: if fid is ancestor of @mo;
196 * returns 0: if fid is not a ancestor of @mo;
198 * returns EREMOTE if remote object is found, fid of remote object is saved to
201 * returns < 0: if error
203 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
204 const struct lu_fid *fid, struct lu_fid *sfid)
206 struct mdd_device *mdd = mdo2mdd(mo);
210 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
213 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
217 } else if (rc == 1) {
218 /* found @fid is parent */
226 * Check that @dir contains no entries except (possibly) dot and dotdot.
231 * -ENOTDIR not a directory object
232 * -ENOTEMPTY not empty
236 static int mdd_dir_is_empty(const struct lu_env *env,
237 struct mdd_object *dir)
240 struct dt_object *obj;
241 const struct dt_it_ops *iops;
245 obj = mdd_object_child(dir);
246 if (!dt_try_as_dir(env, obj))
249 iops = &obj->do_index_ops->dio_it;
250 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
252 result = iops->get(env, it, (const void *)"");
255 for (result = 0, i = 0; result == 0 && i < 3; ++i)
256 result = iops->next(env, it);
259 else if (result == +1)
261 } else if (result == 0)
263 * Huh? Index contains no zero key?
270 result = PTR_ERR(it);
274 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
276 struct mdd_device *m = mdd_obj2mdd_dev(obj);
277 struct lu_attr *la = &mdd_env_info(env)->mti_la;
281 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
286 * Subdir count limitation can be broken through.
288 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
289 !S_ISDIR(la->la_mode))
296 * Check whether it may create the cobj under the pobj.
299 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
300 struct mdd_object *cobj, int check_perm, int check_nlink)
305 if (cobj && mdd_object_exists(cobj))
308 if (mdd_is_dead_obj(pobj))
312 rc = mdd_permission_internal_locked(env, pobj, NULL,
313 MAY_WRITE | MAY_EXEC,
315 if (!rc && check_nlink)
316 rc = __mdd_may_link(env, pobj);
322 * Check whether can unlink from the pobj in the case of "cobj == NULL".
324 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
325 const struct lu_attr *attr)
330 if (mdd_is_dead_obj(pobj))
333 if ((attr->la_valid & LA_FLAGS) &&
334 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
337 rc = mdd_permission_internal_locked(env, pobj, NULL,
338 MAY_WRITE | MAY_EXEC,
343 if (mdd_is_append(pobj))
350 * pobj == NULL is remote ops case, under such case, pobj's
351 * VTX feature has been checked already, no need check again.
353 static inline int mdd_is_sticky(const struct lu_env *env,
354 struct mdd_object *pobj,
355 struct mdd_object *cobj)
357 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
358 struct lu_ucred *uc = lu_ucred_assert(env);
362 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
366 if (!(tmp_la->la_mode & S_ISVTX) ||
367 (tmp_la->la_uid == uc->uc_fsuid))
371 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
375 if (tmp_la->la_uid == uc->uc_fsuid)
378 return !md_capable(uc, CFS_CAP_FOWNER);
381 static int mdd_may_delete_entry(const struct lu_env *env,
382 struct mdd_object *pobj, int check_perm)
386 LASSERT(pobj != NULL);
387 if (!mdd_object_exists(pobj))
390 if (mdd_is_dead_obj(pobj))
395 rc = mdd_permission_internal_locked(env, pobj, NULL,
396 MAY_WRITE | MAY_EXEC,
402 if (mdd_is_append(pobj))
409 * Check whether it may delete the cobj from the pobj.
412 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
413 struct mdd_object *cobj, struct lu_attr *cattr,
414 struct lu_attr *src_attr, int check_perm, int check_empty)
420 rc = mdd_may_delete_entry(env, pobj, check_perm);
428 if (!mdd_object_exists(cobj))
431 if (mdd_is_dead_obj(cobj))
435 if (mdd_is_sticky(env, pobj, cobj))
438 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
441 if ((cattr->la_valid & LA_FLAGS) &&
442 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
445 /* additional check the rename case */
447 if (S_ISDIR(src_attr->la_mode)) {
448 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
450 if (!S_ISDIR(cattr->la_mode))
453 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
455 } else if (S_ISDIR(cattr->la_mode))
459 if (S_ISDIR(cattr->la_mode) && check_empty)
460 rc = mdd_dir_is_empty(env, cobj);
467 * has mdd_write_lock on src already, but not on tgt yet
469 int mdd_link_sanity_check(const struct lu_env *env,
470 struct mdd_object *tgt_obj,
471 const struct lu_name *lname,
472 struct mdd_object *src_obj)
474 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
478 if (!mdd_object_exists(src_obj))
481 if (mdd_is_dead_obj(src_obj))
484 /* Local ops, no lookup before link, check filename length here. */
485 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
486 RETURN(-ENAMETOOLONG);
488 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
491 if (S_ISDIR(mdd_object_type(src_obj)))
494 LASSERT(src_obj != tgt_obj);
496 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
501 rc = __mdd_may_link(env, src_obj);
506 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
507 const char *name, struct thandle *handle,
508 struct lustre_capa *capa)
510 struct dt_object *next = mdd_object_child(pobj);
514 if (dt_try_as_dir(env, next)) {
515 rc = next->do_index_ops->dio_delete(env, next,
516 (struct dt_key *)name,
524 static int __mdd_index_insert_only(const struct lu_env *env,
525 struct mdd_object *pobj,
526 const struct lu_fid *lf, const char *name,
527 struct thandle *handle,
528 struct lustre_capa *capa)
530 struct dt_object *next = mdd_object_child(pobj);
534 if (dt_try_as_dir(env, next)) {
535 struct lu_ucred *uc = lu_ucred_check(env);
538 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
539 rc = next->do_index_ops->dio_insert(env, next,
541 (const struct dt_key *)name,
542 handle, capa, ignore_quota);
549 /* insert named index, add reference if isdir */
550 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
551 const struct lu_fid *lf, const char *name, int is_dir,
552 struct thandle *handle, struct lustre_capa *capa)
557 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
558 if (rc == 0 && is_dir) {
559 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
560 mdo_ref_add(env, pobj, handle);
561 mdd_write_unlock(env, pobj);
566 /* delete named index, drop reference if isdir */
567 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
568 const char *name, int is_dir, struct thandle *handle,
569 struct lustre_capa *capa)
574 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
575 if (rc == 0 && is_dir) {
576 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
577 mdo_ref_del(env, pobj, handle);
578 mdd_write_unlock(env, pobj);
584 int mdd_declare_changelog_store(const struct lu_env *env,
585 struct mdd_device *mdd,
586 const struct lu_name *fname,
587 struct thandle *handle)
589 struct obd_device *obd = mdd2obd_dev(mdd);
590 struct llog_ctxt *ctxt;
591 struct llog_changelog_rec *rec;
597 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
600 reclen = llog_data_len(sizeof(*rec) +
601 (fname != NULL ? fname->ln_namelen : 0));
602 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
603 if (buf->lb_buf == NULL)
607 rec->cr_hdr.lrh_len = reclen;
608 rec->cr_hdr.lrh_type = CHANGELOG_REC;
610 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
614 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
620 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
621 struct mdd_device *mdd,
622 const struct lu_name *tname,
623 const struct lu_name *sname,
624 struct thandle *handle)
626 struct obd_device *obd = mdd2obd_dev(mdd);
627 struct llog_ctxt *ctxt;
628 struct llog_changelog_ext_rec *rec;
634 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
637 reclen = llog_data_len(sizeof(*rec) +
638 (tname != NULL ? tname->ln_namelen : 0) +
639 (sname != NULL ? 1 + sname->ln_namelen : 0));
640 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
641 if (buf->lb_buf == NULL)
645 rec->cr_hdr.lrh_len = reclen;
646 rec->cr_hdr.lrh_type = CHANGELOG_REC;
648 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
652 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
658 /** Add a changelog entry \a rec to the changelog llog
661 * \param handle - currently ignored since llogs start their own transaction;
662 * this will hopefully be fixed in llog rewrite
665 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
666 struct llog_changelog_rec *rec, struct thandle *th)
668 struct obd_device *obd = mdd2obd_dev(mdd);
669 struct llog_ctxt *ctxt;
672 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
673 rec->cr_hdr.lrh_type = CHANGELOG_REC;
674 rec->cr.cr_time = cl_time();
676 spin_lock(&mdd->mdd_cl.mc_lock);
677 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
678 * but as long as the MDD transactions are ordered correctly for e.g.
679 * rename conflicts, I don't think this should matter. */
680 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
681 spin_unlock(&mdd->mdd_cl.mc_lock);
683 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
687 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
694 /** Add a changelog_ext entry \a rec to the changelog llog
697 * \param handle - currently ignored since llogs start their own transaction;
698 * this will hopefully be fixed in llog rewrite
701 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
702 struct llog_changelog_ext_rec *rec,
705 struct obd_device *obd = mdd2obd_dev(mdd);
706 struct llog_ctxt *ctxt;
709 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
710 /* llog_lvfs_write_rec sets the llog tail len */
711 rec->cr_hdr.lrh_type = CHANGELOG_REC;
712 rec->cr.cr_time = cl_time();
714 spin_lock(&mdd->mdd_cl.mc_lock);
715 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
716 * but as long as the MDD transactions are ordered correctly for e.g.
717 * rename conflicts, I don't think this should matter. */
718 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
719 spin_unlock(&mdd->mdd_cl.mc_lock);
721 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
725 /* nested journal transaction */
726 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
734 /** Store a namespace change changelog record
735 * If this fails, we must fail the whole transaction; we don't
736 * want the change to commit without the log entry.
737 * \param target - mdd_object of change
738 * \param parent - parent dir/object
739 * \param tname - target name string
740 * \param handle - transacion handle
742 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
743 enum changelog_rec_type type, unsigned flags,
744 struct mdd_object *target, struct mdd_object *parent,
745 const struct lu_name *tname, struct thandle *handle)
747 struct llog_changelog_rec *rec;
754 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
756 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
759 LASSERT(target != NULL);
760 LASSERT(parent != NULL);
761 LASSERT(tname != NULL);
762 LASSERT(handle != NULL);
764 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
765 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
766 if (buf->lb_buf == NULL)
770 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
771 rec->cr.cr_type = (__u32)type;
772 rec->cr.cr_tfid = *mdo2fid(target);
773 rec->cr.cr_pfid = *mdo2fid(parent);
774 rec->cr.cr_namelen = tname->ln_namelen;
775 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
777 target->mod_cltime = cfs_time_current_64();
779 rc = mdd_changelog_store(env, mdd, rec, handle);
781 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
782 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
783 PFID(&rec->cr.cr_pfid));
791 /** Store a namespace change changelog record
792 * If this fails, we must fail the whole transaction; we don't
793 * want the change to commit without the log entry.
794 * \param target - mdd_object of change
795 * \param tpfid - target parent dir/object fid
796 * \param sfid - source object fid
797 * \param spfid - source parent fid
798 * \param tname - target name string
799 * \param sname - source name string
800 * \param handle - transacion handle
802 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
803 struct mdd_device *mdd,
804 enum changelog_rec_type type,
806 struct mdd_object *target,
807 const struct lu_fid *tpfid,
808 const struct lu_fid *sfid,
809 const struct lu_fid *spfid,
810 const struct lu_name *tname,
811 const struct lu_name *sname,
812 struct thandle *handle)
814 struct llog_changelog_ext_rec *rec;
821 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
823 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
826 LASSERT(sfid != NULL);
827 LASSERT(tpfid != NULL);
828 LASSERT(tname != NULL);
829 LASSERT(handle != NULL);
831 reclen = llog_data_len(sizeof(*rec) +
832 sname != NULL ? 1 + sname->ln_namelen : 0);
833 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
834 if (buf->lb_buf == NULL)
838 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
839 rec->cr.cr_type = (__u32)type;
840 rec->cr.cr_pfid = *tpfid;
841 rec->cr.cr_sfid = *sfid;
842 rec->cr.cr_spfid = *spfid;
843 rec->cr.cr_namelen = tname->ln_namelen;
844 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
846 rec->cr.cr_name[tname->ln_namelen] = '\0';
847 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
849 rec->cr.cr_namelen += 1 + sname->ln_namelen;
852 if (likely(target != NULL)) {
853 rec->cr.cr_tfid = *mdo2fid(target);
854 target->mod_cltime = cfs_time_current_64();
856 fid_zero(&rec->cr.cr_tfid);
859 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
861 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
862 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
869 static int __mdd_links_add(const struct lu_env *env,
870 struct mdd_object *mdd_obj,
871 struct linkea_data *ldata,
872 const struct lu_name *lname,
873 const struct lu_fid *pfid,
874 int first, int check)
878 if (ldata->ld_leh == NULL) {
879 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
883 rc = linkea_data_new(ldata,
884 &mdd_env_info(env)->mti_link_buf);
891 rc = linkea_links_find(ldata, lname, pfid);
892 if (rc && rc != -ENOENT)
898 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
899 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
903 linkea_add_buf(ldata, lname, tfid);
906 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
907 linkea_add_buf(ldata, lname, pfid);
909 return linkea_add_buf(ldata, lname, pfid);
912 static int __mdd_links_del(const struct lu_env *env,
913 struct mdd_object *mdd_obj,
914 struct linkea_data *ldata,
915 const struct lu_name *lname,
916 const struct lu_fid *pfid)
920 if (ldata->ld_leh == NULL) {
921 rc = mdd_links_read(env, mdd_obj, ldata);
926 rc = linkea_links_find(ldata, lname, pfid);
930 linkea_del_buf(ldata, lname);
934 static int mdd_linkea_prepare(const struct lu_env *env,
935 struct mdd_object *mdd_obj,
936 const struct lu_fid *oldpfid,
937 const struct lu_name *oldlname,
938 const struct lu_fid *newpfid,
939 const struct lu_name *newlname,
940 int first, int check,
941 struct linkea_data *ldata)
947 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
950 LASSERT(oldpfid != NULL || newpfid != NULL);
952 if (mdd_obj->mod_flags & DEAD_OBJ) {
953 /* Prevent linkea to be updated which is NOT necessary. */
954 ldata->ld_reclen = 0;
955 /* No more links, don't bother */
959 if (oldpfid != NULL) {
960 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
963 (rc != -ENODATA && rc != -ENOENT))
965 /* No changes done. */
970 /* If renaming, add the new record */
971 if (newpfid != NULL) {
972 /* even if the add fails, we still delete the out-of-date
974 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
978 rc = rc != 0 ? rc : rc2;
983 int mdd_links_rename(const struct lu_env *env,
984 struct mdd_object *mdd_obj,
985 const struct lu_fid *oldpfid,
986 const struct lu_name *oldlname,
987 const struct lu_fid *newpfid,
988 const struct lu_name *newlname,
989 struct thandle *handle,
990 struct linkea_data *ldata,
991 int first, int check)
998 ldata = &mdd_env_info(env)->mti_link_data;
999 memset(ldata, 0, sizeof(*ldata));
1000 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1001 newpfid, newlname, first, check,
1007 if (ldata->ld_reclen != 0)
1008 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1015 if (rc == -EOVERFLOW || rc == -ENOSPC)
1017 if (oldpfid == NULL)
1018 CDEBUG(error ? D_ERROR : D_OTHER,
1019 "link_ea add '%.*s' failed %d "DFID"\n",
1020 newlname->ln_namelen, newlname->ln_name,
1021 rc, PFID(mdd_object_fid(mdd_obj)));
1022 else if (newpfid == NULL)
1023 CDEBUG(error ? D_ERROR : D_OTHER,
1024 "link_ea del '%.*s' failed %d "DFID"\n",
1025 oldlname->ln_namelen, oldlname->ln_name,
1026 rc, PFID(mdd_object_fid(mdd_obj)));
1028 CDEBUG(error ? D_ERROR : D_OTHER,
1029 "link_ea rename '%.*s'->'%.*s' failed %d "
1031 oldlname->ln_namelen, oldlname->ln_name,
1032 newlname->ln_namelen, newlname->ln_name,
1033 rc, PFID(mdd_object_fid(mdd_obj)));
1036 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1037 /* if we vmalloced a large buffer drop it */
1038 lu_buf_free(ldata->ld_buf);
1043 static inline int mdd_links_add(const struct lu_env *env,
1044 struct mdd_object *mdd_obj,
1045 const struct lu_fid *pfid,
1046 const struct lu_name *lname,
1047 struct thandle *handle,
1048 struct linkea_data *data, int first)
1050 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1051 pfid, lname, handle, data, first, 0);
1054 static inline int mdd_links_del(const struct lu_env *env,
1055 struct mdd_object *mdd_obj,
1056 const struct lu_fid *pfid,
1057 const struct lu_name *lname,
1058 struct thandle *handle)
1060 return mdd_links_rename(env, mdd_obj, pfid, lname,
1061 NULL, NULL, handle, NULL, 0, 0);
1064 /** Read the link EA into a temp buffer.
1065 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1066 * A pointer to the buffer is stored in \a ldata::ld_buf.
1068 * \retval 0 or error
1070 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1071 struct linkea_data *ldata)
1075 /* First try a small buf */
1076 LASSERT(env != NULL);
1077 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1079 if (ldata->ld_buf->lb_buf == NULL)
1082 if (!mdd_object_exists(mdd_obj))
1085 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1087 if (rc == -ERANGE) {
1088 /* Buf was too small, figure out what we need. */
1089 lu_buf_free(ldata->ld_buf);
1090 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1091 XATTR_NAME_LINK, BYPASS_CAPA);
1094 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1095 if (ldata->ld_buf->lb_buf == NULL)
1097 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1098 XATTR_NAME_LINK, BYPASS_CAPA);
1103 return linkea_init(ldata);
1106 /** Read the link EA into a temp buffer.
1107 * Uses the name_buf since it is generally large.
1108 * \retval IS_ERR err
1109 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1111 struct lu_buf *mdd_links_get(const struct lu_env *env,
1112 struct mdd_object *mdd_obj)
1114 struct linkea_data ldata = { 0 };
1117 rc = mdd_links_read(env, mdd_obj, &ldata);
1118 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1121 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1122 struct linkea_data *ldata, struct thandle *handle)
1124 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1125 ldata->ld_leh->leh_len);
1126 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1127 mdd_object_capa(env, mdd_obj));
1130 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1131 struct thandle *handle, struct linkea_data *ldata)
1137 if (ldata != NULL && ldata->ld_lee != NULL) {
1138 ea_len = ldata->ld_leh->leh_len;
1139 linkea = ldata->ld_buf->lb_buf;
1141 ea_len = DEFAULT_LINKEA_SIZE;
1145 /* XXX: max size? */
1146 rc = mdo_declare_xattr_set(env, mdd_obj,
1147 mdd_buf_get_const(env, linkea, ea_len),
1148 XATTR_NAME_LINK, 0, handle);
1152 static inline int mdd_declare_links_del(const struct lu_env *env,
1153 struct mdd_object *c,
1154 struct thandle *handle)
1158 /* For directory, the linkEA will be removed together
1159 * with the object. */
1160 if (!S_ISDIR(mdd_object_type(c)))
1161 rc = mdd_declare_links_add(env, c, handle, NULL);
1166 static int mdd_declare_link(const struct lu_env *env,
1167 struct mdd_device *mdd,
1168 struct mdd_object *p,
1169 struct mdd_object *c,
1170 const struct lu_name *name,
1171 struct thandle *handle,
1173 struct linkea_data *data)
1177 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1181 rc = mdo_declare_ref_add(env, c, handle);
1185 la->la_valid = LA_CTIME | LA_MTIME;
1186 rc = mdo_declare_attr_set(env, p, la, handle);
1190 la->la_valid = LA_CTIME;
1191 rc = mdo_declare_attr_set(env, c, la, handle);
1195 rc = mdd_declare_links_add(env, c, handle, data);
1199 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1204 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1205 struct md_object *src_obj, const struct lu_name *lname,
1208 const char *name = lname->ln_name;
1209 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1210 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1211 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1212 struct mdd_device *mdd = mdo2mdd(src_obj);
1213 struct thandle *handle;
1214 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1218 handle = mdd_trans_create(env, mdd);
1220 GOTO(out_pending, rc = PTR_ERR(handle));
1222 memset(ldata, 0, sizeof(*ldata));
1224 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1225 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1227 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1232 rc = mdd_trans_start(env, mdd, handle);
1236 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1237 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
1239 GOTO(out_unlock, rc);
1241 rc = mdo_ref_add(env, mdd_sobj, handle);
1243 GOTO(out_unlock, rc);
1246 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1248 mdd_object_capa(env, mdd_tobj));
1250 mdo_ref_del(env, mdd_sobj, handle);
1251 GOTO(out_unlock, rc);
1254 la->la_valid = LA_CTIME | LA_MTIME;
1255 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1257 GOTO(out_unlock, rc);
1259 la->la_valid = LA_CTIME;
1260 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1262 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1263 mdo2fid(mdd_tobj), lname, 0, 0,
1266 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1267 lname, handle, ldata, 0);
1268 /* The failure of links_add should not cause the link
1269 * failure, reset rc here */
1274 mdd_write_unlock(env, mdd_sobj);
1276 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1277 mdd_tobj, lname, handle);
1279 mdd_trans_stop(env, mdd, rc, handle);
1281 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1282 /* if we vmalloced a large buffer drop it */
1283 lu_buf_free(ldata->ld_buf);
1288 int mdd_declare_finish_unlink(const struct lu_env *env,
1289 struct mdd_object *obj,
1291 struct thandle *handle)
1295 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1299 return mdo_declare_destroy(env, obj, handle);
1302 /* caller should take a lock before calling */
1303 int mdd_finish_unlink(const struct lu_env *env,
1304 struct mdd_object *obj, struct md_attr *ma,
1308 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1311 LASSERT(mdd_write_locked(env, obj) != 0);
1313 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1314 obj->mod_flags |= DEAD_OBJ;
1316 /* add new orphan and the object
1317 * will be deleted during mdd_close() */
1318 if (obj->mod_count) {
1319 rc = __mdd_orphan_add(env, obj, th);
1321 CDEBUG(D_HA, "Object "DFID" is inserted into "
1322 "orphan list, open count = %d\n",
1323 PFID(mdd_object_fid(obj)),
1326 CERROR("Object "DFID" fail to be an orphan, "
1327 "open count = %d, maybe cause failed "
1329 PFID(mdd_object_fid(obj)),
1332 rc = mdo_destroy(env, obj, th);
1341 * has mdd_write_lock on cobj already, but not on pobj yet
1343 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1344 struct mdd_object *cobj, struct lu_attr *cattr)
1349 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1354 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1355 struct mdd_object *p, struct mdd_object *c,
1356 const struct lu_name *name, struct md_attr *ma,
1357 struct thandle *handle, int no_name)
1359 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1362 if (likely(no_name == 0)) {
1363 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1368 rc = mdo_declare_ref_del(env, p, handle);
1372 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1373 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1374 la->la_valid = LA_CTIME | LA_MTIME;
1375 rc = mdo_declare_attr_set(env, p, la, handle);
1380 rc = mdo_declare_ref_del(env, c, handle);
1384 rc = mdo_declare_ref_del(env, c, handle);
1388 la->la_valid = LA_CTIME;
1389 rc = mdo_declare_attr_set(env, c, la, handle);
1393 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1397 rc = mdd_declare_links_del(env, c, handle);
1401 /* FIXME: need changelog for remove entry */
1402 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1409 * test if a file has an HSM archive
1410 * if HSM attributes are not found in ma update them from
1413 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1414 struct mdd_object *obj,
1419 if (!(ma->ma_valid & MA_HSM)) {
1420 /* no HSM MD provided, read xattr */
1421 struct lu_buf *hsm_buf;
1422 const size_t buflen = sizeof(struct hsm_attrs);
1425 hsm_buf = mdd_buf_get(env, NULL, 0);
1426 lu_buf_alloc(hsm_buf, buflen);
1427 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1428 mdd_object_capa(env, obj));
1429 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1430 lu_buf_free(hsm_buf);
1434 ma->ma_valid = MA_HSM;
1436 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1442 * Delete name entry and the object.
1443 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1444 * does not exist for this object, and it could only happen during resending
1445 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1446 * is also needed in this case(needed by changelog), so we have to add another
1447 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1448 * the ENOENT failure should be able to be fixed by redo mechanism.
1450 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1451 struct md_object *cobj, const struct lu_name *lname,
1452 struct md_attr *ma, int no_name)
1454 const char *name = lname->ln_name;
1455 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1456 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1457 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1458 struct mdd_object *mdd_cobj = NULL;
1459 struct mdd_device *mdd = mdo2mdd(pobj);
1460 struct thandle *handle;
1464 /* cobj == NULL means only delete name entry */
1465 if (likely(cobj != NULL)) {
1466 mdd_cobj = md2mdd_obj(cobj);
1467 if (mdd_object_exists(mdd_cobj) == 0)
1469 /* currently it is assume, it could only delete
1470 * name entry of remote directory */
1474 handle = mdd_trans_create(env, mdd);
1476 RETURN(PTR_ERR(handle));
1478 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1479 lname, ma, handle, no_name);
1483 rc = mdd_trans_start(env, mdd, handle);
1487 if (likely(mdd_cobj != NULL)) {
1488 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1491 rc = mdd_la_get(env, mdd_cobj, cattr,
1492 mdd_object_capa(env, mdd_cobj));
1496 is_dir = S_ISDIR(cattr->la_mode);
1500 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1504 if (likely(no_name == 0)) {
1505 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1506 mdd_object_capa(env, mdd_pobj));
1511 if (likely(mdd_cobj != NULL)) {
1512 rc = mdo_ref_del(env, mdd_cobj, handle);
1514 __mdd_index_insert_only(env, mdd_pobj,
1517 mdd_object_capa(env, mdd_pobj));
1523 mdo_ref_del(env, mdd_cobj, handle);
1525 /* fetch updated nlink */
1526 rc = mdd_la_get(env, mdd_cobj, cattr,
1527 mdd_object_capa(env, mdd_cobj));
1532 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1533 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1535 la->la_valid = LA_CTIME | LA_MTIME;
1536 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1540 /* Enough for only unlink the entry */
1541 if (unlikely(mdd_cobj == NULL))
1544 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1545 /* update ctime of an unlinked file only if it is still
1546 * opened or a link still exists */
1547 la->la_valid = LA_CTIME;
1548 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1553 /* XXX: this transfer to ma will be removed with LOD/OSP */
1554 ma->ma_attr = *cattr;
1555 ma->ma_valid |= MA_INODE;
1556 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1558 /* fetch updated nlink */
1560 rc = mdd_la_get(env, mdd_cobj, cattr,
1561 mdd_object_capa(env, mdd_cobj));
1564 /* old files may not have link ea; ignore errors */
1565 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1567 /* if object is removed then we can't get its attrs, use last get */
1568 if (cattr->la_nlink == 0) {
1569 ma->ma_attr = *cattr;
1570 ma->ma_valid |= MA_INODE;
1574 mdd_write_unlock(env, mdd_cobj);
1578 if (cattr->la_nlink == 0) {
1579 cl_flags |= CLF_UNLINK_LAST;
1580 /* search for an existing archive */
1581 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1582 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1585 rc = mdd_changelog_ns_store(env, mdd,
1586 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1587 mdd_cobj, mdd_pobj, lname, handle);
1591 mdd_trans_stop(env, mdd, rc, handle);
1597 * The permission has been checked when obj created, no need check again.
1599 static int mdd_cd_sanity_check(const struct lu_env *env,
1600 struct mdd_object *obj)
1605 if (!obj || mdd_is_dead_obj(obj))
1612 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1613 struct md_object *cobj, const struct md_op_spec *spec,
1616 struct mdd_device *mdd = mdo2mdd(cobj);
1617 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1618 struct mdd_object *son = md2mdd_obj(cobj);
1619 struct thandle *handle;
1620 const struct lu_buf *buf;
1621 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1625 rc = mdd_cd_sanity_check(env, son);
1629 if (!md_should_create(spec->sp_cr_flags))
1633 * there are following use cases for this function:
1634 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1635 * striping can be specified or not
1638 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1642 /* calling ->ah_make_hint() is used to transfer information from parent */
1643 mdd_object_make_hint(env, mdd_pobj, son, attr);
1645 handle = mdd_trans_create(env, mdd);
1647 GOTO(out_free, rc = PTR_ERR(handle));
1650 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1651 * Should this be fixed?
1653 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1654 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1655 spec->sp_cr_flags, spec->no_create);
1657 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1658 /* replay case or lfs setstripe */
1659 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1660 spec->u.sp_ea.eadatalen);
1665 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1666 XATTR_NAME_LOV, 0, handle);
1670 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1674 rc = mdd_trans_start(env, mdd, handle);
1678 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1679 0, handle, mdd_object_capa(env, son));
1684 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1687 mdd_trans_stop(env, mdd, rc, handle);
1692 static int mdd_declare_object_initialize(const struct lu_env *env,
1693 struct mdd_object *parent,
1694 struct mdd_object *child,
1695 struct lu_attr *attr,
1696 struct thandle *handle,
1697 struct linkea_data *ldata)
1703 * inode mode has been set in creation time, and it's based on umask,
1704 * la_mode and acl, don't set here again! (which will go wrong
1705 * because below function doesn't consider umask).
1706 * I'd suggest set all object attributes in creation time, see above.
1708 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1709 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1710 rc = mdo_declare_attr_set(env, child, attr, handle);
1711 attr->la_valid |= LA_MODE | LA_TYPE;
1712 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1713 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1716 rc = mdo_declare_ref_add(env, child, handle);
1718 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1723 mdd_declare_links_add(env, child, handle, ldata);
1728 static int mdd_object_initialize(const struct lu_env *env,
1729 const struct lu_fid *pfid,
1730 const struct lu_name *lname,
1731 struct mdd_object *child,
1732 struct lu_attr *attr, struct thandle *handle,
1733 const struct md_op_spec *spec,
1734 struct linkea_data *ldata)
1740 * Update attributes for child.
1743 * (1) the valid bits should be converted between Lustre and Linux;
1744 * (2) maybe, the child attributes should be set in OSD when creation.
1747 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1748 /* arguments are supposed to stay the same */
1749 if (S_ISDIR(attr->la_mode)) {
1750 /* Add "." and ".." for newly created dir */
1751 mdo_ref_add(env, child, handle);
1752 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1753 dot, handle, BYPASS_CAPA);
1755 rc = __mdd_index_insert_only(env, child, pfid,
1759 mdo_ref_del(env, child, handle);
1763 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1768 /* has not lock on pobj yet */
1769 static int mdd_create_sanity_check(const struct lu_env *env,
1770 struct md_object *pobj,
1771 struct lu_attr *pattr,
1772 const struct lu_name *lname,
1773 struct lu_attr *cattr,
1774 struct md_op_spec *spec)
1776 struct mdd_thread_info *info = mdd_env_info(env);
1777 struct lu_fid *fid = &info->mti_fid;
1778 struct mdd_object *obj = md2mdd_obj(pobj);
1779 struct mdd_device *m = mdo2mdd(pobj);
1784 if (mdd_is_dead_obj(obj))
1788 * In some cases this lookup is not needed - we know before if name
1789 * exists or not because MDT performs lookup for it.
1790 * name length check is done in lookup.
1792 if (spec->sp_cr_lookup) {
1794 * Check if the name already exist, though it will be checked in
1795 * _index_insert also, for avoiding rolling back if exists
1798 rc = __mdd_lookup(env, pobj, lname, fid, MAY_WRITE | MAY_EXEC);
1800 RETURN(rc ? : -EEXIST);
1803 * Check WRITE permission for the parent.
1804 * EXEC permission have been checked
1805 * when lookup before create already.
1807 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1814 if (pattr->la_mode & S_ISGID) {
1815 cattr->la_gid = pattr->la_gid;
1816 if (S_ISDIR(cattr->la_mode)) {
1817 cattr->la_mode |= S_ISGID;
1818 cattr->la_valid |= LA_MODE;
1822 switch (cattr->la_mode & S_IFMT) {
1824 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1826 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1827 RETURN(-ENAMETOOLONG);
1846 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1847 struct mdd_object *p, struct mdd_object *c,
1848 const struct lu_name *name,
1849 struct lu_attr *attr,
1850 struct thandle *handle,
1851 const struct md_op_spec *spec,
1852 struct linkea_data *ldata,
1853 struct lu_buf *def_acl_buf,
1854 struct lu_buf *acl_buf)
1858 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1862 #ifdef CONFIG_FS_POSIX_ACL
1863 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1864 /* if dir, then can inherit default ACl */
1865 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1866 XATTR_NAME_ACL_DEFAULT,
1872 if (acl_buf->lb_len > 0) {
1873 rc = mdo_declare_attr_set(env, c, attr, handle);
1877 rc = mdo_declare_xattr_set(env, c, acl_buf,
1878 XATTR_NAME_ACL_ACCESS, 0, handle);
1884 if (S_ISDIR(attr->la_mode)) {
1885 rc = mdo_declare_ref_add(env, p, handle);
1890 rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1894 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1895 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1897 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1898 name->ln_name, handle);
1902 /* replay case, create LOV EA from client data */
1903 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1904 const struct lu_buf *buf;
1906 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1907 spec->u.sp_ea.eadatalen);
1908 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1914 if (S_ISLNK(attr->la_mode)) {
1915 rc = dt_declare_record_write(env, mdd_object_child(c),
1916 strlen(spec->u.sp_symname), 0,
1922 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1923 rc = mdo_declare_attr_set(env, p, attr, handle);
1928 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1936 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1937 struct lu_attr *la, struct lu_buf *def_acl_buf,
1938 struct lu_buf *acl_buf)
1943 if (S_ISLNK(la->la_mode)) {
1944 acl_buf->lb_len = 0;
1945 def_acl_buf->lb_len = 0;
1949 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1950 rc = mdo_xattr_get(env, pobj, def_acl_buf,
1951 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1952 mdd_read_unlock(env, pobj);
1954 /* If there are default ACL, fix mode/ACL by default ACL */
1955 def_acl_buf->lb_len = rc;
1956 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
1957 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
1958 acl_buf->lb_len = rc;
1959 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1962 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1963 /* If there are no default ACL, fix mode by mask */
1964 struct lu_ucred *uc = lu_ucred(env);
1966 /* The create triggered by MDT internal events, such as
1967 * LFSCK reset, will not contain valid "uc". */
1968 if (unlikely(uc != NULL))
1969 la->la_mode &= ~uc->uc_umask;
1971 acl_buf->lb_len = 0;
1972 def_acl_buf->lb_len = 0;
1979 * Create object and insert it into namespace.
1981 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1982 const struct lu_name *lname, struct md_object *child,
1983 struct md_op_spec *spec, struct md_attr* ma)
1985 struct mdd_thread_info *info = mdd_env_info(env);
1986 struct lu_attr *la = &info->mti_la_for_fix;
1987 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1988 struct mdd_object *son = md2mdd_obj(child);
1989 struct mdd_device *mdd = mdo2mdd(pobj);
1990 struct lu_attr *attr = &ma->ma_attr;
1991 struct thandle *handle;
1992 struct lu_attr *pattr = &info->mti_pattr;
1993 struct lu_buf acl_buf;
1994 struct lu_buf def_acl_buf;
1995 struct linkea_data *ldata = &info->mti_link_data;
1996 const char *name = lname->ln_name;
1997 int rc, created = 0, initialized = 0, inserted = 0;
2001 * Two operations have to be performed:
2003 * - an allocation of a new object (->do_create()), and
2005 * - an insertion into a parent index (->dio_insert()).
2007 * Due to locking, operation order is not important, when both are
2008 * successful, *but* error handling cases are quite different:
2010 * - if insertion is done first, and following object creation fails,
2011 * insertion has to be rolled back, but this operation might fail
2012 * also leaving us with dangling index entry.
2014 * - if creation is done first, is has to be undone if insertion
2015 * fails, leaving us with leaked space, which is neither good, nor
2018 * It seems that creation-first is simplest solution, but it is
2019 * sub-optimal in the frequent
2024 * case, because second mkdir is bound to create object, only to
2025 * destroy it immediately.
2027 * To avoid this follow local file systems that do double lookup:
2029 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2031 * 1. create (mdd_object_create_internal())
2033 * 2. insert (__mdd_index_insert(), lookup again)
2036 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2040 /* Sanity checks before big job. */
2041 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2045 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2046 GOTO(out_free, rc = -EINPROGRESS);
2048 acl_buf.lb_buf = info->mti_xattr_buf;
2049 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2050 def_acl_buf.lb_buf = info->mti_key;
2051 def_acl_buf.lb_len = sizeof(info->mti_key);
2052 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2056 mdd_object_make_hint(env, mdd_pobj, son, attr);
2058 handle = mdd_trans_create(env, mdd);
2060 GOTO(out_free, rc = PTR_ERR(handle));
2062 memset(ldata, 0, sizeof(*ldata));
2063 mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2064 lname, 1, 0, ldata);
2066 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2067 handle, spec, ldata, &def_acl_buf, &acl_buf);
2071 rc = mdd_trans_start(env, mdd, handle);
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 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2219 rc = mdd_changelog_ns_store(env, mdd,
2220 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2221 S_ISREG(attr->la_mode) ? CL_CREATE :
2222 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2223 0, son, mdd_pobj, lname, handle);
2225 mdd_trans_stop(env, mdd, rc, handle);
2227 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2228 /* if we vmalloced a large buffer drop it */
2229 lu_buf_free(ldata->ld_buf);
2231 /* The child object shouldn't be cached anymore */
2233 set_bit(LU_OBJECT_HEARD_BANSHEE,
2234 &child->mo_lu.lo_header->loh_flags);
2239 * Get locks on parents in proper order
2240 * RETURN: < 0 - error, rename_order if successful
2248 static int mdd_rename_order(const struct lu_env *env,
2249 struct mdd_device *mdd,
2250 struct mdd_object *src_pobj,
2251 struct mdd_object *tgt_pobj)
2253 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2257 if (src_pobj == tgt_pobj)
2258 RETURN(MDD_RN_SAME);
2260 /* compared the parent child relationship of src_p&tgt_p */
2261 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2263 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2266 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2279 /* has not mdd_write{read}_lock on any obj yet. */
2280 static int mdd_rename_sanity_check(const struct lu_env *env,
2281 struct mdd_object *src_pobj,
2282 struct mdd_object *tgt_pobj,
2283 struct mdd_object *sobj,
2284 struct mdd_object *tobj,
2285 struct lu_attr *so_attr,
2286 struct lu_attr *tg_attr)
2291 /* XXX: when get here, sobj must NOT be NULL,
2292 * the other case has been processed in cld_rename
2293 * before mdd_rename and enable MDS_PERM_BYPASS. */
2296 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2300 /* XXX: when get here, "tobj == NULL" means tobj must
2301 * NOT exist (neither on remote MDS, such case has been
2302 * processed in cld_rename before mdd_rename and enable
2304 * So check may_create, but not check may_unlink. */
2306 rc = mdd_may_create(env, tgt_pobj, NULL,
2307 (src_pobj != tgt_pobj), 0);
2309 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2310 (src_pobj != tgt_pobj), 1);
2312 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2313 S_ISDIR(so_attr->la_mode))
2314 rc = __mdd_may_link(env, tgt_pobj);
2319 static int mdd_declare_rename(const struct lu_env *env,
2320 struct mdd_device *mdd,
2321 struct mdd_object *mdd_spobj,
2322 struct mdd_object *mdd_tpobj,
2323 struct mdd_object *mdd_sobj,
2324 struct mdd_object *mdd_tobj,
2325 const struct lu_name *tname,
2326 const struct lu_name *sname,
2328 struct linkea_data *ldata,
2329 struct thandle *handle)
2331 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2334 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2335 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2341 /* name from source dir */
2342 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2346 /* .. from source child */
2347 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2348 /* source child can be directory,
2349 * counted by source dir's nlink */
2350 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2353 if (mdd_spobj != mdd_tpobj) {
2354 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2359 rc = mdo_declare_index_insert(env, mdd_sobj,
2365 /* new target child can be directory,
2366 * counted by target dir's nlink */
2367 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2373 la->la_valid = LA_CTIME | LA_MTIME;
2374 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2378 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2382 la->la_valid = LA_CTIME;
2383 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2387 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2392 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2393 tname->ln_name, handle);
2397 /* name from target dir (old name), we declare it unconditionally
2398 * as mdd_rename() calls delete unconditionally as well. so just
2399 * to balance declarations vs calls to change ... */
2400 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2404 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2405 /* delete target child in target parent directory */
2406 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2410 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2411 /* target child can be directory,
2412 * delete "." reference in target child directory */
2413 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2417 /* delete ".." reference in target parent directory */
2418 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2423 la->la_valid = LA_CTIME;
2424 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2428 rc = mdd_declare_links_del(env, mdd_tobj, handle);
2432 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2437 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2444 /* src object can be remote that is why we use only fid and type of object */
2445 static int mdd_rename(const struct lu_env *env,
2446 struct md_object *src_pobj, struct md_object *tgt_pobj,
2447 const struct lu_fid *lf, const struct lu_name *lsname,
2448 struct md_object *tobj, const struct lu_name *ltname,
2451 const char *sname = lsname->ln_name;
2452 const char *tname = ltname->ln_name;
2453 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2454 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2455 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2456 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2457 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2458 struct mdd_device *mdd = mdo2mdd(src_pobj);
2459 struct mdd_object *mdd_sobj = NULL; /* source object */
2460 struct mdd_object *mdd_tobj = NULL;
2461 struct thandle *handle;
2462 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2463 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2464 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2467 bool tobj_locked = 0;
2468 unsigned cl_flags = 0;
2473 mdd_tobj = md2mdd_obj(tobj);
2475 mdd_sobj = mdd_object_find(env, mdd, lf);
2477 rc = mdd_la_get(env, mdd_sobj, so_attr,
2478 mdd_object_capa(env, mdd_sobj));
2480 GOTO(out_pending, rc);
2483 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2484 mdd_object_capa(env, mdd_tobj));
2486 GOTO(out_pending, rc);
2489 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2490 mdd_tobj, so_attr, tg_attr);
2492 GOTO(out_pending, rc);
2494 handle = mdd_trans_create(env, mdd);
2496 GOTO(out_pending, rc = PTR_ERR(handle));
2498 memset(ldata, 0, sizeof(*ldata));
2499 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2500 ltname, 1, 0, ldata);
2501 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2502 mdd_tobj, lsname, ltname, ma, ldata, handle);
2506 rc = mdd_trans_start(env, mdd, handle);
2510 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2511 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2513 GOTO(cleanup_unlocked, rc);
2515 is_dir = S_ISDIR(so_attr->la_mode);
2517 /* Remove source name from source directory */
2518 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2519 mdd_object_capa(env, mdd_spobj));
2523 /* "mv dir1 dir2" needs "dir1/.." link update */
2524 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2525 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2526 mdd_object_capa(env, mdd_sobj));
2528 GOTO(fixup_spobj2, rc);
2530 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2531 handle, mdd_object_capa(env, mdd_sobj));
2533 GOTO(fixup_spobj, rc);
2536 /* Remove target name from target directory
2537 * Here tobj can be remote one, so we do index_delete unconditionally
2538 * and -ENOENT is allowed.
2540 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2541 mdd_object_capa(env, mdd_tpobj));
2544 /* tname might been renamed to something else */
2545 GOTO(fixup_spobj, rc);
2548 GOTO(fixup_spobj, rc);
2551 /* Insert new fid with target name into target dir */
2552 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2553 mdd_object_capa(env, mdd_tpobj));
2555 GOTO(fixup_tpobj, rc);
2557 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2558 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2560 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2562 la->la_valid = LA_CTIME;
2563 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2565 GOTO(fixup_tpobj, rc);
2568 /* Remove old target object
2569 * For tobj is remote case cmm layer has processed
2570 * and set tobj to NULL then. So when tobj is NOT NULL,
2571 * it must be local one.
2573 if (tobj && mdd_object_exists(mdd_tobj)) {
2574 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2576 if (mdd_is_dead_obj(mdd_tobj)) {
2577 /* shld not be dead, something is wrong */
2578 CERROR("tobj is dead, something is wrong\n");
2582 mdo_ref_del(env, mdd_tobj, handle);
2584 /* Remove dot reference. */
2585 if (S_ISDIR(tg_attr->la_mode))
2586 mdo_ref_del(env, mdd_tobj, handle);
2589 /* fetch updated nlink */
2590 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2591 mdd_object_capa(env, mdd_tobj));
2593 CERROR("%s: Failed to get nlink for tobj "
2595 mdd2obd_dev(mdd)->obd_name,
2596 PFID(tpobj_fid), rc);
2597 GOTO(fixup_tpobj, rc);
2600 la->la_valid = LA_CTIME;
2601 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2603 CERROR("%s: Failed to set ctime for tobj "
2605 mdd2obd_dev(mdd)->obd_name,
2606 PFID(tpobj_fid), rc);
2607 GOTO(fixup_tpobj, rc);
2610 /* XXX: this transfer to ma will be removed with LOD/OSP */
2611 ma->ma_attr = *tg_attr;
2612 ma->ma_valid |= MA_INODE;
2613 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2615 CERROR("%s: Failed to unlink tobj "
2617 mdd2obd_dev(mdd)->obd_name,
2618 PFID(tpobj_fid), rc);
2619 GOTO(fixup_tpobj, rc);
2622 /* fetch updated nlink */
2623 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2624 mdd_object_capa(env, mdd_tobj));
2626 CERROR("%s: Failed to get nlink for tobj "
2628 mdd2obd_dev(mdd)->obd_name,
2629 PFID(tpobj_fid), rc);
2630 GOTO(fixup_tpobj, rc);
2632 /* XXX: this transfer to ma will be removed with LOD/OSP */
2633 ma->ma_attr = *tg_attr;
2634 ma->ma_valid |= MA_INODE;
2636 if (tg_attr->la_nlink == 0) {
2637 cl_flags |= CLF_RENAME_LAST;
2638 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2639 cl_flags |= CLF_RENAME_LAST_EXISTS;
2643 la->la_valid = LA_CTIME | LA_MTIME;
2644 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2646 GOTO(fixup_tpobj, rc);
2648 if (mdd_spobj != mdd_tpobj) {
2649 la->la_valid = LA_CTIME | LA_MTIME;
2650 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2654 if (rc == 0 && mdd_sobj) {
2655 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2656 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2657 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2660 /* Old files might not have EA entry */
2661 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2662 lsname, handle, NULL, 0);
2663 mdd_write_unlock(env, mdd_sobj);
2664 /* We don't fail the transaction if the link ea can't be
2665 updated -- fid2path will use alternate lookup method. */
2673 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2676 CWARN("tp obj fix error %d\n",rc2);
2678 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2679 !mdd_is_dead_obj(mdd_tobj)) {
2681 mdo_ref_add(env, mdd_tobj, handle);
2683 mdo_ref_add(env, mdd_tobj, handle);
2686 rc2 = __mdd_index_insert(env, mdd_tpobj,
2687 mdo2fid(mdd_tobj), tname,
2692 CWARN("tp obj fix error %d\n",rc2);
2697 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2698 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2702 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2703 mdd2obd_dev(mdd)->obd_name, rc2);
2706 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2707 dotdot, handle, BYPASS_CAPA);
2709 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2710 mdd2obd_dev(mdd)->obd_name, rc2);
2715 rc2 = __mdd_index_insert(env, mdd_spobj,
2716 lf, sname, is_dir, handle, BYPASS_CAPA);
2718 CWARN("sp obj fix error %d\n",rc2);
2722 mdd_write_unlock(env, mdd_tobj);
2725 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2726 mdd_tobj, tpobj_fid, lf,
2727 spobj_fid, ltname, lsname,
2731 mdd_trans_stop(env, mdd, rc, handle);
2733 mdd_object_put(env, mdd_sobj);
2737 const struct md_dir_operations mdd_dir_ops = {
2738 .mdo_is_subdir = mdd_is_subdir,
2739 .mdo_lookup = mdd_lookup,
2740 .mdo_create = mdd_create,
2741 .mdo_rename = mdd_rename,
2742 .mdo_link = mdd_link,
2743 .mdo_unlink = mdd_unlink,
2744 .mdo_create_data = mdd_create_data,