4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@intel.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 /* Get FID from name and parent */
62 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
63 const struct lu_attr *pattr, const struct lu_name *lname,
64 struct lu_fid* fid, int mask)
66 const char *name = lname->ln_name;
67 const struct dt_key *key = (const struct dt_key *)name;
68 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
69 struct mdd_device *m = mdo2mdd(pobj);
70 struct dt_object *dir = mdd_object_child(mdd_obj);
74 if (unlikely(mdd_is_dead_obj(mdd_obj)))
77 if (mdd_object_remote(mdd_obj)) {
78 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
79 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
80 } else if (!mdd_object_exists(mdd_obj)) {
84 /* The common filename length check. */
85 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
86 RETURN(-ENAMETOOLONG);
88 rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
93 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
94 dt_try_as_dir(env, dir))) {
96 rc = dir->do_index_ops->dio_lookup(env, dir,
97 (struct dt_rec *)fid, key,
98 mdd_object_capa(env, mdd_obj));
109 int mdd_lookup(const struct lu_env *env,
110 struct md_object *pobj, const struct lu_name *lname,
111 struct lu_fid *fid, struct md_op_spec *spec)
113 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
117 rc = mdd_la_get(env, md2mdd_obj(pobj), pattr, BYPASS_CAPA);
121 rc = __mdd_lookup(env, pobj, pattr, lname, fid, MAY_EXEC);
125 static inline int mdd_parent_fid(const struct lu_env *env,
126 struct mdd_object *obj,
127 const struct lu_attr *attr,
130 return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
134 * For root fid use special function, which does not compare version component
135 * of fid. Version component is different for root fids on all MDTs.
137 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
139 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
140 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
144 * return 1: if lf is the fid of the ancestor of p1;
147 * return -EREMOTE: if remote object is found, in this
148 * case fid of remote object is saved to @pf;
150 * otherwise: values < 0, errors.
152 static int mdd_is_parent(const struct lu_env *env,
153 struct mdd_device *mdd,
154 struct mdd_object *p1,
155 const struct lu_attr *attr,
156 const struct lu_fid *lf,
159 struct mdd_object *parent = NULL;
164 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
165 pfid = &mdd_env_info(env)->mti_fid;
167 /* Check for root first. */
168 if (mdd_is_root(mdd, mdo2fid(p1)))
172 /* this is done recursively, bypass capa for each obj */
173 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
174 rc = mdd_parent_fid(env, p1, attr, pfid);
177 if (mdd_is_root(mdd, pfid))
179 if (lu_fid_eq(pfid, lf))
182 mdd_object_put(env, parent);
184 parent = mdd_object_find(env, mdd, pfid);
185 if (IS_ERR(parent)) {
186 GOTO(out, rc = PTR_ERR(parent));
187 } else if (mdd_object_remote(parent)) {
188 /*FIXME: Because of the restriction of rename in Phase I.
189 * If the parent is remote, we just assumed lf is not the
190 * parent of P1 for now */
197 if (parent && !IS_ERR(parent))
198 mdd_object_put(env, parent);
203 * No permission check is needed.
205 * returns 1: if fid is ancestor of @mo;
206 * returns 0: if fid is not a ancestor of @mo;
208 * returns EREMOTE if remote object is found, fid of remote object is saved to
211 * returns < 0: if error
213 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
214 const struct lu_fid *fid, struct lu_fid *sfid)
216 struct mdd_device *mdd = mdo2mdd(mo);
217 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
221 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
224 rc = mdd_la_get(env, md2mdd_obj(mo), attr, BYPASS_CAPA);
228 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), attr, fid, sfid);
232 } else if (rc == 1) {
233 /* found @fid is parent */
241 * Check that @dir contains no entries except (possibly) dot and dotdot.
246 * -ENOTDIR not a directory object
247 * -ENOTEMPTY not empty
251 static int mdd_dir_is_empty(const struct lu_env *env,
252 struct mdd_object *dir)
255 struct dt_object *obj;
256 const struct dt_it_ops *iops;
260 obj = mdd_object_child(dir);
261 if (!dt_try_as_dir(env, obj))
264 iops = &obj->do_index_ops->dio_it;
265 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
267 result = iops->get(env, it, (const void *)"");
270 for (result = 0, i = 0; result == 0 && i < 3; ++i)
271 result = iops->next(env, it);
274 else if (result == +1)
276 } else if (result == 0)
278 * Huh? Index contains no zero key?
285 result = PTR_ERR(it);
289 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
290 const struct lu_attr *la)
292 struct mdd_device *m = mdd_obj2mdd_dev(obj);
297 if (!S_ISDIR(la->la_mode))
301 * Subdir count limitation can be broken through.
303 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
310 * Check whether it may create the cobj under the pobj.
313 int mdd_may_create(const struct lu_env *env,
314 struct mdd_object *pobj, const struct lu_attr *pattr,
315 struct mdd_object *cobj, int check_perm, int check_nlink)
320 if (cobj && mdd_object_exists(cobj))
323 if (mdd_is_dead_obj(pobj))
327 rc = mdd_permission_internal_locked(env, pobj, pattr,
328 MAY_WRITE | MAY_EXEC,
330 if (!rc && check_nlink)
331 rc = __mdd_may_link(env, pobj, pattr);
337 * Check whether can unlink from the pobj in the case of "cobj == NULL".
339 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
340 const struct lu_attr *pattr, const struct lu_attr *attr)
345 if (mdd_is_dead_obj(pobj))
348 if ((attr->la_valid & LA_FLAGS) &&
349 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
352 rc = mdd_permission_internal_locked(env, pobj, pattr,
353 MAY_WRITE | MAY_EXEC,
358 if (mdd_is_append(pobj))
365 * pobj == NULL is remote ops case, under such case, pobj's
366 * VTX feature has been checked already, no need check again.
368 static inline int mdd_is_sticky(const struct lu_env *env,
369 struct mdd_object *pobj,
370 const struct lu_attr *pattr,
371 struct mdd_object *cobj,
372 const struct lu_attr *cattr)
374 struct lu_ucred *uc = lu_ucred_assert(env);
377 LASSERT(pattr != NULL);
378 if (!(pattr->la_mode & S_ISVTX) ||
379 (pattr->la_uid == uc->uc_fsuid))
383 LASSERT(cattr != NULL);
384 if (cattr->la_uid == uc->uc_fsuid)
387 return !md_capable(uc, CFS_CAP_FOWNER);
390 static int mdd_may_delete_entry(const struct lu_env *env,
391 struct mdd_object *pobj,
392 const struct lu_attr *pattr,
397 LASSERT(pobj != NULL);
398 if (!mdd_object_exists(pobj))
401 if (mdd_is_dead_obj(pobj))
406 rc = mdd_permission_internal_locked(env, pobj, pattr,
407 MAY_WRITE | MAY_EXEC,
413 if (mdd_is_append(pobj))
420 * Check whether it may delete the cobj from the pobj.
423 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
424 const struct lu_attr *tpattr, struct mdd_object *tobj,
425 const struct lu_attr *tattr, const struct lu_attr *cattr,
426 int check_perm, int check_empty)
432 LASSERT(tpattr != NULL);
433 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
441 if (!mdd_object_exists(tobj))
444 if (mdd_is_dead_obj(tobj))
447 if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
450 if (mdd_is_immutable(tobj) || mdd_is_append(tobj))
453 if ((tattr->la_valid & LA_FLAGS) &&
454 (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
457 /* additional check the rename case */
459 if (S_ISDIR(cattr->la_mode)) {
460 struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
462 if (!S_ISDIR(tattr->la_mode))
465 if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
467 } else if (S_ISDIR(tattr->la_mode))
471 if (S_ISDIR(tattr->la_mode) && check_empty)
472 rc = mdd_dir_is_empty(env, tobj);
479 * has mdd_write_lock on src already, but not on tgt yet
481 static int mdd_link_sanity_check(const struct lu_env *env,
482 struct mdd_object *tgt_obj,
483 const struct lu_attr *tattr,
484 const struct lu_name *lname,
485 struct mdd_object *src_obj,
486 const struct lu_attr *cattr)
488 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
492 if (!mdd_object_exists(src_obj))
495 if (mdd_is_dead_obj(src_obj))
498 /* Local ops, no lookup before link, check filename length here. */
499 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
500 RETURN(-ENAMETOOLONG);
502 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
505 if (S_ISDIR(mdd_object_type(src_obj)))
508 LASSERT(src_obj != tgt_obj);
510 rc = mdd_may_create(env, tgt_obj, tattr, NULL, 1, 0);
515 rc = __mdd_may_link(env, src_obj, cattr);
520 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
521 const char *name, struct thandle *handle,
522 struct lustre_capa *capa)
524 struct dt_object *next = mdd_object_child(pobj);
528 if (dt_try_as_dir(env, next)) {
529 rc = next->do_index_ops->dio_delete(env, next,
530 (struct dt_key *)name,
538 static int __mdd_index_insert_only(const struct lu_env *env,
539 struct mdd_object *pobj,
540 const struct lu_fid *lf, const char *name,
541 struct thandle *handle,
542 struct lustre_capa *capa)
544 struct dt_object *next = mdd_object_child(pobj);
548 if (dt_try_as_dir(env, next)) {
549 struct lu_ucred *uc = lu_ucred_check(env);
552 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
553 rc = next->do_index_ops->dio_insert(env, next,
555 (const struct dt_key *)name,
556 handle, capa, ignore_quota);
563 /* insert named index, add reference if isdir */
564 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
565 const struct lu_fid *lf, const char *name, int is_dir,
566 struct thandle *handle, struct lustre_capa *capa)
571 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
572 if (rc == 0 && is_dir) {
573 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
574 mdo_ref_add(env, pobj, handle);
575 mdd_write_unlock(env, pobj);
580 /* delete named index, drop reference if isdir */
581 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
582 const char *name, int is_dir, struct thandle *handle,
583 struct lustre_capa *capa)
588 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
589 if (rc == 0 && is_dir) {
590 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
591 mdo_ref_del(env, pobj, handle);
592 mdd_write_unlock(env, pobj);
598 int mdd_declare_changelog_store(const struct lu_env *env,
599 struct mdd_device *mdd,
600 const struct lu_name *fname,
601 struct thandle *handle)
603 struct obd_device *obd = mdd2obd_dev(mdd);
604 struct llog_ctxt *ctxt;
605 struct llog_changelog_rec *rec;
611 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
614 reclen = llog_data_len(sizeof(*rec) +
615 (fname != NULL ? fname->ln_namelen : 0));
616 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
617 if (buf->lb_buf == NULL)
621 rec->cr_hdr.lrh_len = reclen;
622 rec->cr_hdr.lrh_type = CHANGELOG_REC;
624 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
628 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
634 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
635 struct mdd_device *mdd,
636 const struct lu_name *tname,
637 const struct lu_name *sname,
638 struct thandle *handle)
640 struct obd_device *obd = mdd2obd_dev(mdd);
641 struct llog_ctxt *ctxt;
642 struct llog_changelog_ext_rec *rec;
648 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
651 reclen = llog_data_len(sizeof(*rec) +
652 (tname != NULL ? tname->ln_namelen : 0) +
653 (sname != NULL ? 1 + sname->ln_namelen : 0));
654 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
655 if (buf->lb_buf == NULL)
659 rec->cr_hdr.lrh_len = reclen;
660 rec->cr_hdr.lrh_type = CHANGELOG_REC;
662 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
666 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
672 /** Add a changelog entry \a rec to the changelog llog
675 * \param handle - currently ignored since llogs start their own transaction;
676 * this will hopefully be fixed in llog rewrite
679 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
680 struct llog_changelog_rec *rec, struct thandle *th)
682 struct obd_device *obd = mdd2obd_dev(mdd);
683 struct llog_ctxt *ctxt;
686 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
687 rec->cr_hdr.lrh_type = CHANGELOG_REC;
688 rec->cr.cr_time = cl_time();
690 spin_lock(&mdd->mdd_cl.mc_lock);
691 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
692 * but as long as the MDD transactions are ordered correctly for e.g.
693 * rename conflicts, I don't think this should matter. */
694 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
695 spin_unlock(&mdd->mdd_cl.mc_lock);
697 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
701 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
708 /** Add a changelog_ext entry \a rec to the changelog llog
711 * \param handle - currently ignored since llogs start their own transaction;
712 * this will hopefully be fixed in llog rewrite
715 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
716 struct llog_changelog_ext_rec *rec,
719 struct obd_device *obd = mdd2obd_dev(mdd);
720 struct llog_ctxt *ctxt;
723 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
724 /* llog_lvfs_write_rec sets the llog tail len */
725 rec->cr_hdr.lrh_type = CHANGELOG_REC;
726 rec->cr.cr_time = cl_time();
728 spin_lock(&mdd->mdd_cl.mc_lock);
729 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
730 * but as long as the MDD transactions are ordered correctly for e.g.
731 * rename conflicts, I don't think this should matter. */
732 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
733 spin_unlock(&mdd->mdd_cl.mc_lock);
735 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
739 /* nested journal transaction */
740 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
748 /** Store a namespace change changelog record
749 * If this fails, we must fail the whole transaction; we don't
750 * want the change to commit without the log entry.
751 * \param target - mdd_object of change
752 * \param parent - parent dir/object
753 * \param tname - target name string
754 * \param handle - transacion handle
756 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
757 enum changelog_rec_type type, unsigned flags,
758 struct mdd_object *target, struct mdd_object *parent,
759 const struct lu_name *tname, struct thandle *handle)
761 struct llog_changelog_rec *rec;
768 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
770 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
773 LASSERT(target != NULL);
774 LASSERT(parent != NULL);
775 LASSERT(tname != NULL);
776 LASSERT(handle != NULL);
778 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
779 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
780 if (buf->lb_buf == NULL)
784 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
785 rec->cr.cr_type = (__u32)type;
786 rec->cr.cr_tfid = *mdo2fid(target);
787 rec->cr.cr_pfid = *mdo2fid(parent);
788 rec->cr.cr_namelen = tname->ln_namelen;
789 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
791 target->mod_cltime = cfs_time_current_64();
793 rc = mdd_changelog_store(env, mdd, rec, handle);
795 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
796 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
797 PFID(&rec->cr.cr_pfid));
805 /** Store a namespace change changelog record
806 * If this fails, we must fail the whole transaction; we don't
807 * want the change to commit without the log entry.
808 * \param target - mdd_object of change
809 * \param tpfid - target parent dir/object fid
810 * \param sfid - source object fid
811 * \param spfid - source parent fid
812 * \param tname - target name string
813 * \param sname - source name string
814 * \param handle - transacion handle
816 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
817 struct mdd_device *mdd,
818 enum changelog_rec_type type,
820 struct mdd_object *target,
821 const struct lu_fid *tpfid,
822 const struct lu_fid *sfid,
823 const struct lu_fid *spfid,
824 const struct lu_name *tname,
825 const struct lu_name *sname,
826 struct thandle *handle)
828 struct llog_changelog_ext_rec *rec;
835 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
837 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
840 LASSERT(sfid != NULL);
841 LASSERT(tpfid != NULL);
842 LASSERT(tname != NULL);
843 LASSERT(handle != NULL);
845 reclen = llog_data_len(sizeof(*rec) +
846 sname != NULL ? 1 + sname->ln_namelen : 0);
847 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
848 if (buf->lb_buf == NULL)
852 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
853 rec->cr.cr_type = (__u32)type;
854 rec->cr.cr_pfid = *tpfid;
855 rec->cr.cr_sfid = *sfid;
856 rec->cr.cr_spfid = *spfid;
857 rec->cr.cr_namelen = tname->ln_namelen;
858 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
860 rec->cr.cr_name[tname->ln_namelen] = '\0';
861 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
863 rec->cr.cr_namelen += 1 + sname->ln_namelen;
866 if (likely(target != NULL)) {
867 rec->cr.cr_tfid = *mdo2fid(target);
868 target->mod_cltime = cfs_time_current_64();
870 fid_zero(&rec->cr.cr_tfid);
873 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
875 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
876 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
883 static int __mdd_links_add(const struct lu_env *env,
884 struct mdd_object *mdd_obj,
885 struct linkea_data *ldata,
886 const struct lu_name *lname,
887 const struct lu_fid *pfid,
888 int first, int check)
892 if (ldata->ld_leh == NULL) {
893 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
897 rc = linkea_data_new(ldata,
898 &mdd_env_info(env)->mti_link_buf);
905 rc = linkea_links_find(ldata, lname, pfid);
906 if (rc && rc != -ENOENT)
912 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
913 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
917 linkea_add_buf(ldata, lname, tfid);
920 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
921 linkea_add_buf(ldata, lname, pfid);
923 return linkea_add_buf(ldata, lname, pfid);
926 static int __mdd_links_del(const struct lu_env *env,
927 struct mdd_object *mdd_obj,
928 struct linkea_data *ldata,
929 const struct lu_name *lname,
930 const struct lu_fid *pfid)
934 if (ldata->ld_leh == NULL) {
935 rc = mdd_links_read(env, mdd_obj, ldata);
940 rc = linkea_links_find(ldata, lname, pfid);
944 linkea_del_buf(ldata, lname);
948 static int mdd_linkea_prepare(const struct lu_env *env,
949 struct mdd_object *mdd_obj,
950 const struct lu_fid *oldpfid,
951 const struct lu_name *oldlname,
952 const struct lu_fid *newpfid,
953 const struct lu_name *newlname,
954 int first, int check,
955 struct linkea_data *ldata)
961 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
964 LASSERT(oldpfid != NULL || newpfid != NULL);
966 if (mdd_obj->mod_flags & DEAD_OBJ) {
967 /* Prevent linkea to be updated which is NOT necessary. */
968 ldata->ld_reclen = 0;
969 /* No more links, don't bother */
973 if (oldpfid != NULL) {
974 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
977 (rc != -ENODATA && rc != -ENOENT))
979 /* No changes done. */
984 /* If renaming, add the new record */
985 if (newpfid != NULL) {
986 /* even if the add fails, we still delete the out-of-date
988 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
992 rc = rc != 0 ? rc : rc2;
997 int mdd_links_rename(const struct lu_env *env,
998 struct mdd_object *mdd_obj,
999 const struct lu_fid *oldpfid,
1000 const struct lu_name *oldlname,
1001 const struct lu_fid *newpfid,
1002 const struct lu_name *newlname,
1003 struct thandle *handle,
1004 struct linkea_data *ldata,
1005 int first, int check)
1011 if (ldata == NULL) {
1012 ldata = &mdd_env_info(env)->mti_link_data;
1013 memset(ldata, 0, sizeof(*ldata));
1014 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
1015 newpfid, newlname, first, check,
1021 if (ldata->ld_reclen != 0)
1022 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1029 if (rc == -EOVERFLOW || rc == -ENOSPC)
1031 if (oldpfid == NULL)
1032 CDEBUG(error ? D_ERROR : D_OTHER,
1033 "link_ea add '%.*s' failed %d "DFID"\n",
1034 newlname->ln_namelen, newlname->ln_name,
1035 rc, PFID(mdd_object_fid(mdd_obj)));
1036 else if (newpfid == NULL)
1037 CDEBUG(error ? D_ERROR : D_OTHER,
1038 "link_ea del '%.*s' failed %d "DFID"\n",
1039 oldlname->ln_namelen, oldlname->ln_name,
1040 rc, PFID(mdd_object_fid(mdd_obj)));
1042 CDEBUG(error ? D_ERROR : D_OTHER,
1043 "link_ea rename '%.*s'->'%.*s' failed %d "
1045 oldlname->ln_namelen, oldlname->ln_name,
1046 newlname->ln_namelen, newlname->ln_name,
1047 rc, PFID(mdd_object_fid(mdd_obj)));
1050 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1051 /* if we vmalloced a large buffer drop it */
1052 lu_buf_free(ldata->ld_buf);
1057 static inline int mdd_links_add(const struct lu_env *env,
1058 struct mdd_object *mdd_obj,
1059 const struct lu_fid *pfid,
1060 const struct lu_name *lname,
1061 struct thandle *handle,
1062 struct linkea_data *data, int first)
1064 return mdd_links_rename(env, mdd_obj, NULL, NULL,
1065 pfid, lname, handle, data, first, 0);
1068 static inline int mdd_links_del(const struct lu_env *env,
1069 struct mdd_object *mdd_obj,
1070 const struct lu_fid *pfid,
1071 const struct lu_name *lname,
1072 struct thandle *handle)
1074 return mdd_links_rename(env, mdd_obj, pfid, lname,
1075 NULL, NULL, handle, NULL, 0, 0);
1078 /** Read the link EA into a temp buffer.
1079 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1080 * A pointer to the buffer is stored in \a ldata::ld_buf.
1082 * \retval 0 or error
1084 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1085 struct linkea_data *ldata)
1089 /* First try a small buf */
1090 LASSERT(env != NULL);
1091 ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1093 if (ldata->ld_buf->lb_buf == NULL)
1096 if (!mdd_object_exists(mdd_obj))
1099 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1101 if (rc == -ERANGE) {
1102 /* Buf was too small, figure out what we need. */
1103 lu_buf_free(ldata->ld_buf);
1104 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1105 XATTR_NAME_LINK, BYPASS_CAPA);
1108 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1109 if (ldata->ld_buf->lb_buf == NULL)
1111 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1112 XATTR_NAME_LINK, BYPASS_CAPA);
1117 return linkea_init(ldata);
1120 /** Read the link EA into a temp buffer.
1121 * Uses the name_buf since it is generally large.
1122 * \retval IS_ERR err
1123 * \retval ptr to \a lu_buf (always \a mti_big_buf)
1125 struct lu_buf *mdd_links_get(const struct lu_env *env,
1126 struct mdd_object *mdd_obj)
1128 struct linkea_data ldata = { 0 };
1131 rc = mdd_links_read(env, mdd_obj, &ldata);
1132 return rc ? ERR_PTR(rc) : ldata.ld_buf;
1135 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1136 struct linkea_data *ldata, struct thandle *handle)
1138 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1139 ldata->ld_leh->leh_len);
1140 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1141 mdd_object_capa(env, mdd_obj));
1144 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1145 struct thandle *handle, struct linkea_data *ldata)
1151 if (ldata != NULL && ldata->ld_lee != NULL) {
1152 ea_len = ldata->ld_leh->leh_len;
1153 linkea = ldata->ld_buf->lb_buf;
1155 ea_len = DEFAULT_LINKEA_SIZE;
1159 /* XXX: max size? */
1160 rc = mdo_declare_xattr_set(env, mdd_obj,
1161 mdd_buf_get_const(env, linkea, ea_len),
1162 XATTR_NAME_LINK, 0, handle);
1166 static inline int mdd_declare_links_del(const struct lu_env *env,
1167 struct mdd_object *c,
1168 struct thandle *handle)
1172 /* For directory, the linkEA will be removed together
1173 * with the object. */
1174 if (!S_ISDIR(mdd_object_type(c)))
1175 rc = mdd_declare_links_add(env, c, handle, NULL);
1180 static int mdd_declare_link(const struct lu_env *env,
1181 struct mdd_device *mdd,
1182 struct mdd_object *p,
1183 struct mdd_object *c,
1184 const struct lu_name *name,
1185 struct thandle *handle,
1187 struct linkea_data *data)
1191 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1195 rc = mdo_declare_ref_add(env, c, handle);
1199 la->la_valid = LA_CTIME | LA_MTIME;
1200 rc = mdo_declare_attr_set(env, p, la, handle);
1204 la->la_valid = LA_CTIME;
1205 rc = mdo_declare_attr_set(env, c, la, handle);
1209 rc = mdd_declare_links_add(env, c, handle, data);
1213 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1218 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1219 struct md_object *src_obj, const struct lu_name *lname,
1222 const char *name = lname->ln_name;
1223 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1224 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1225 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1226 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1227 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
1228 struct mdd_device *mdd = mdo2mdd(src_obj);
1229 struct thandle *handle;
1230 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1234 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
1238 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
1242 handle = mdd_trans_create(env, mdd);
1244 GOTO(out_pending, rc = PTR_ERR(handle));
1246 memset(ldata, 0, sizeof(*ldata));
1248 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1249 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1251 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1256 rc = mdd_trans_start(env, mdd, handle);
1260 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1261 rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1264 GOTO(out_unlock, rc);
1266 rc = mdo_ref_add(env, mdd_sobj, handle);
1268 GOTO(out_unlock, rc);
1271 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1273 mdd_object_capa(env, mdd_tobj));
1275 mdo_ref_del(env, mdd_sobj, handle);
1276 GOTO(out_unlock, rc);
1279 la->la_valid = LA_CTIME | LA_MTIME;
1280 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1282 GOTO(out_unlock, rc);
1284 la->la_valid = LA_CTIME;
1285 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1287 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1288 mdo2fid(mdd_tobj), lname, 0, 0,
1291 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1292 lname, handle, ldata, 0);
1293 /* The failure of links_add should not cause the link
1294 * failure, reset rc here */
1299 mdd_write_unlock(env, mdd_sobj);
1301 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1302 mdd_tobj, lname, handle);
1304 mdd_trans_stop(env, mdd, rc, handle);
1306 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1307 /* if we vmalloced a large buffer drop it */
1308 lu_buf_free(ldata->ld_buf);
1313 int mdd_declare_finish_unlink(const struct lu_env *env,
1314 struct mdd_object *obj,
1316 struct thandle *handle)
1320 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1324 return mdo_declare_destroy(env, obj, handle);
1327 /* caller should take a lock before calling */
1328 int mdd_finish_unlink(const struct lu_env *env,
1329 struct mdd_object *obj, struct md_attr *ma,
1333 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1336 LASSERT(mdd_write_locked(env, obj) != 0);
1338 if (ma->ma_attr.la_nlink == 0 || is_dir) {
1339 obj->mod_flags |= DEAD_OBJ;
1341 /* add new orphan and the object
1342 * will be deleted during mdd_close() */
1343 if (obj->mod_count) {
1344 rc = __mdd_orphan_add(env, obj, th);
1346 CDEBUG(D_HA, "Object "DFID" is inserted into "
1347 "orphan list, open count = %d\n",
1348 PFID(mdd_object_fid(obj)),
1351 CERROR("Object "DFID" fail to be an orphan, "
1352 "open count = %d, maybe cause failed "
1354 PFID(mdd_object_fid(obj)),
1357 rc = mdo_destroy(env, obj, th);
1366 * has mdd_write_lock on cobj already, but not on pobj yet
1368 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1369 const struct lu_attr *pattr,
1370 struct mdd_object *cobj,
1371 const struct lu_attr *cattr)
1376 rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1381 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1382 struct mdd_object *p, struct mdd_object *c,
1383 const struct lu_name *name, struct md_attr *ma,
1384 struct thandle *handle, int no_name)
1386 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1389 if (likely(no_name == 0)) {
1390 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1395 rc = mdo_declare_ref_del(env, p, handle);
1399 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1400 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1401 la->la_valid = LA_CTIME | LA_MTIME;
1402 rc = mdo_declare_attr_set(env, p, la, handle);
1407 rc = mdo_declare_ref_del(env, c, handle);
1411 rc = mdo_declare_ref_del(env, c, handle);
1415 la->la_valid = LA_CTIME;
1416 rc = mdo_declare_attr_set(env, c, la, handle);
1420 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1424 rc = mdd_declare_links_del(env, c, handle);
1428 /* FIXME: need changelog for remove entry */
1429 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1436 * test if a file has an HSM archive
1437 * if HSM attributes are not found in ma update them from
1440 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1441 struct mdd_object *obj,
1446 if (!(ma->ma_valid & MA_HSM)) {
1447 /* no HSM MD provided, read xattr */
1448 struct lu_buf *hsm_buf;
1449 const size_t buflen = sizeof(struct hsm_attrs);
1452 hsm_buf = mdd_buf_get(env, NULL, 0);
1453 lu_buf_alloc(hsm_buf, buflen);
1454 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM,
1455 mdd_object_capa(env, obj));
1456 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1457 lu_buf_free(hsm_buf);
1461 ma->ma_valid = MA_HSM;
1463 if (ma->ma_hsm.mh_flags & HS_EXISTS)
1469 * Delete name entry and the object.
1470 * Note: no_name == 1 means it only destory the object, i.e. name_entry
1471 * does not exist for this object, and it could only happen during resending
1472 * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1473 * is also needed in this case(needed by changelog), so we have to add another
1474 * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1475 * the ENOENT failure should be able to be fixed by redo mechanism.
1477 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1478 struct md_object *cobj, const struct lu_name *lname,
1479 struct md_attr *ma, int no_name)
1481 const char *name = lname->ln_name;
1482 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1483 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1484 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1485 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1486 struct mdd_object *mdd_cobj = NULL;
1487 struct mdd_device *mdd = mdo2mdd(pobj);
1488 struct thandle *handle;
1492 /* cobj == NULL means only delete name entry */
1493 if (likely(cobj != NULL)) {
1494 mdd_cobj = md2mdd_obj(cobj);
1495 if (mdd_object_exists(mdd_cobj) == 0)
1499 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1503 handle = mdd_trans_create(env, mdd);
1505 RETURN(PTR_ERR(handle));
1507 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1508 lname, ma, handle, no_name);
1512 rc = mdd_trans_start(env, mdd, handle);
1516 if (likely(mdd_cobj != NULL)) {
1517 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1520 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1524 is_dir = S_ISDIR(cattr->la_mode);
1527 rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1531 if (likely(no_name == 0)) {
1532 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1533 mdd_object_capa(env, mdd_pobj));
1538 if (likely(mdd_cobj != NULL)) {
1539 rc = mdo_ref_del(env, mdd_cobj, handle);
1541 __mdd_index_insert_only(env, mdd_pobj,
1544 mdd_object_capa(env, mdd_pobj));
1550 mdo_ref_del(env, mdd_cobj, handle);
1552 /* fetch updated nlink */
1553 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1558 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1559 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1561 la->la_valid = LA_CTIME | LA_MTIME;
1562 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1566 /* Enough for only unlink the entry */
1567 if (unlikely(mdd_cobj == NULL))
1570 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1571 /* update ctime of an unlinked file only if it is still
1572 * opened or a link still exists */
1573 la->la_valid = LA_CTIME;
1574 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1579 /* XXX: this transfer to ma will be removed with LOD/OSP */
1580 ma->ma_attr = *cattr;
1581 ma->ma_valid |= MA_INODE;
1582 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1584 /* fetch updated nlink */
1586 rc = mdd_la_get(env, mdd_cobj, cattr, BYPASS_CAPA);
1589 /* old files may not have link ea; ignore errors */
1590 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1592 /* if object is removed then we can't get its attrs, use last get */
1593 if (cattr->la_nlink == 0) {
1594 ma->ma_attr = *cattr;
1595 ma->ma_valid |= MA_INODE;
1599 mdd_write_unlock(env, mdd_cobj);
1603 if (cattr->la_nlink == 0) {
1604 cl_flags |= CLF_UNLINK_LAST;
1605 /* search for an existing archive */
1606 if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1607 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1610 rc = mdd_changelog_ns_store(env, mdd,
1611 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1612 mdd_cobj, mdd_pobj, lname, handle);
1616 mdd_trans_stop(env, mdd, rc, handle);
1622 * The permission has been checked when obj created, no need check again.
1624 static int mdd_cd_sanity_check(const struct lu_env *env,
1625 struct mdd_object *obj)
1630 if (!obj || mdd_is_dead_obj(obj))
1636 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1637 struct md_object *cobj, const struct md_op_spec *spec,
1640 struct mdd_device *mdd = mdo2mdd(cobj);
1641 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1642 struct mdd_object *son = md2mdd_obj(cobj);
1643 struct thandle *handle;
1644 const struct lu_buf *buf;
1645 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1649 rc = mdd_cd_sanity_check(env, son);
1653 if (!md_should_create(spec->sp_cr_flags))
1657 * there are following use cases for this function:
1658 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1659 * striping can be specified or not
1662 rc = mdd_la_get(env, son, attr, BYPASS_CAPA);
1666 /* calling ->ah_make_hint() is used to transfer information from parent */
1667 mdd_object_make_hint(env, mdd_pobj, son, attr);
1669 handle = mdd_trans_create(env, mdd);
1671 GOTO(out_free, rc = PTR_ERR(handle));
1674 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1675 * Should this be fixed?
1677 CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1678 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1679 spec->sp_cr_flags, spec->no_create);
1681 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1682 /* replay case or lfs setstripe */
1683 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1684 spec->u.sp_ea.eadatalen);
1689 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1690 XATTR_NAME_LOV, 0, handle);
1694 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1698 rc = mdd_trans_start(env, mdd, handle);
1702 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1703 0, handle, mdd_object_capa(env, son));
1708 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1711 mdd_trans_stop(env, mdd, rc, handle);
1716 static int mdd_declare_object_initialize(const struct lu_env *env,
1717 struct mdd_object *parent,
1718 struct mdd_object *child,
1719 struct lu_attr *attr,
1720 struct thandle *handle,
1721 struct linkea_data *ldata)
1727 * inode mode has been set in creation time, and it's based on umask,
1728 * la_mode and acl, don't set here again! (which will go wrong
1729 * because below function doesn't consider umask).
1730 * I'd suggest set all object attributes in creation time, see above.
1732 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1733 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1734 rc = mdo_declare_attr_set(env, child, attr, handle);
1735 attr->la_valid |= LA_MODE | LA_TYPE;
1736 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1737 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1740 rc = mdo_declare_ref_add(env, child, handle);
1742 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1747 mdd_declare_links_add(env, child, handle, ldata);
1752 static int mdd_object_initialize(const struct lu_env *env,
1753 const struct lu_fid *pfid,
1754 const struct lu_name *lname,
1755 struct mdd_object *child,
1756 struct lu_attr *attr, struct thandle *handle,
1757 const struct md_op_spec *spec,
1758 struct linkea_data *ldata)
1764 * Update attributes for child.
1767 * (1) the valid bits should be converted between Lustre and Linux;
1768 * (2) maybe, the child attributes should be set in OSD when creation.
1771 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1772 /* arguments are supposed to stay the same */
1773 if (S_ISDIR(attr->la_mode)) {
1774 /* Add "." and ".." for newly created dir */
1775 mdo_ref_add(env, child, handle);
1776 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1777 dot, handle, BYPASS_CAPA);
1779 rc = __mdd_index_insert_only(env, child, pfid,
1783 mdo_ref_del(env, child, handle);
1787 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1792 /* has not lock on pobj yet */
1793 static int mdd_create_sanity_check(const struct lu_env *env,
1794 struct md_object *pobj,
1795 const struct lu_attr *pattr,
1796 const struct lu_name *lname,
1797 struct lu_attr *cattr,
1798 struct md_op_spec *spec)
1800 struct mdd_thread_info *info = mdd_env_info(env);
1801 struct lu_fid *fid = &info->mti_fid;
1802 struct mdd_object *obj = md2mdd_obj(pobj);
1803 struct mdd_device *m = mdo2mdd(pobj);
1808 if (mdd_is_dead_obj(obj))
1812 * In some cases this lookup is not needed - we know before if name
1813 * exists or not because MDT performs lookup for it.
1814 * name length check is done in lookup.
1816 if (spec->sp_cr_lookup) {
1818 * Check if the name already exist, though it will be checked in
1819 * _index_insert also, for avoiding rolling back if exists
1822 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1823 MAY_WRITE | MAY_EXEC);
1825 RETURN(rc ? : -EEXIST);
1828 * Check WRITE permission for the parent.
1829 * EXEC permission have been checked
1830 * when lookup before create already.
1832 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1839 if (pattr->la_mode & S_ISGID) {
1840 cattr->la_gid = pattr->la_gid;
1841 if (S_ISDIR(cattr->la_mode)) {
1842 cattr->la_mode |= S_ISGID;
1843 cattr->la_valid |= LA_MODE;
1847 switch (cattr->la_mode & S_IFMT) {
1849 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1851 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1852 RETURN(-ENAMETOOLONG);
1871 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1872 struct mdd_object *p, struct mdd_object *c,
1873 const struct lu_name *name,
1874 struct lu_attr *attr,
1875 struct thandle *handle,
1876 const struct md_op_spec *spec,
1877 struct linkea_data *ldata,
1878 struct lu_buf *def_acl_buf,
1879 struct lu_buf *acl_buf)
1883 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1887 #ifdef CONFIG_FS_POSIX_ACL
1888 if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1889 /* if dir, then can inherit default ACl */
1890 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1891 XATTR_NAME_ACL_DEFAULT,
1897 if (acl_buf->lb_len > 0) {
1898 rc = mdo_declare_attr_set(env, c, attr, handle);
1902 rc = mdo_declare_xattr_set(env, c, acl_buf,
1903 XATTR_NAME_ACL_ACCESS, 0, handle);
1909 if (S_ISDIR(attr->la_mode)) {
1910 rc = mdo_declare_ref_add(env, p, handle);
1915 rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1919 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1920 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1922 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1923 name->ln_name, handle);
1927 /* replay case, create LOV EA from client data */
1928 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1929 const struct lu_buf *buf;
1931 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1932 spec->u.sp_ea.eadatalen);
1933 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1939 if (S_ISLNK(attr->la_mode)) {
1940 rc = dt_declare_record_write(env, mdd_object_child(c),
1941 strlen(spec->u.sp_symname), 0,
1947 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1948 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1951 la->la_valid = LA_CTIME | LA_MTIME;
1952 rc = mdo_declare_attr_set(env, p, la, handle);
1957 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1965 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1966 struct lu_attr *la, struct lu_buf *def_acl_buf,
1967 struct lu_buf *acl_buf)
1972 if (S_ISLNK(la->la_mode)) {
1973 acl_buf->lb_len = 0;
1974 def_acl_buf->lb_len = 0;
1978 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1979 rc = mdo_xattr_get(env, pobj, def_acl_buf,
1980 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1981 mdd_read_unlock(env, pobj);
1983 /* If there are default ACL, fix mode/ACL by default ACL */
1984 def_acl_buf->lb_len = rc;
1985 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
1986 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
1987 acl_buf->lb_len = rc;
1988 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1991 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1992 /* If there are no default ACL, fix mode by mask */
1993 struct lu_ucred *uc = lu_ucred(env);
1995 /* The create triggered by MDT internal events, such as
1996 * LFSCK reset, will not contain valid "uc". */
1997 if (unlikely(uc != NULL))
1998 la->la_mode &= ~uc->uc_umask;
2000 acl_buf->lb_len = 0;
2001 def_acl_buf->lb_len = 0;
2008 * Create object and insert it into namespace.
2010 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2011 const struct lu_name *lname, struct md_object *child,
2012 struct md_op_spec *spec, struct md_attr* ma)
2014 struct mdd_thread_info *info = mdd_env_info(env);
2015 struct lu_attr *la = &info->mti_la_for_fix;
2016 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
2017 struct mdd_object *son = md2mdd_obj(child);
2018 struct mdd_device *mdd = mdo2mdd(pobj);
2019 struct lu_attr *attr = &ma->ma_attr;
2020 struct thandle *handle;
2021 struct lu_attr *pattr = &info->mti_pattr;
2022 struct lu_buf acl_buf;
2023 struct lu_buf def_acl_buf;
2024 struct linkea_data *ldata = &info->mti_link_data;
2025 const char *name = lname->ln_name;
2026 int rc, created = 0, initialized = 0, inserted = 0;
2030 * Two operations have to be performed:
2032 * - an allocation of a new object (->do_create()), and
2034 * - an insertion into a parent index (->dio_insert()).
2036 * Due to locking, operation order is not important, when both are
2037 * successful, *but* error handling cases are quite different:
2039 * - if insertion is done first, and following object creation fails,
2040 * insertion has to be rolled back, but this operation might fail
2041 * also leaving us with dangling index entry.
2043 * - if creation is done first, is has to be undone if insertion
2044 * fails, leaving us with leaked space, which is neither good, nor
2047 * It seems that creation-first is simplest solution, but it is
2048 * sub-optimal in the frequent
2053 * case, because second mkdir is bound to create object, only to
2054 * destroy it immediately.
2056 * To avoid this follow local file systems that do double lookup:
2058 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
2060 * 1. create (mdd_object_create_internal())
2062 * 2. insert (__mdd_index_insert(), lookup again)
2065 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2069 /* Sanity checks before big job. */
2070 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2074 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2075 GOTO(out_free, rc = -EINPROGRESS);
2077 acl_buf.lb_buf = info->mti_xattr_buf;
2078 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2079 def_acl_buf.lb_buf = info->mti_key;
2080 def_acl_buf.lb_len = sizeof(info->mti_key);
2081 rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2085 mdd_object_make_hint(env, mdd_pobj, son, attr);
2087 handle = mdd_trans_create(env, mdd);
2089 GOTO(out_free, rc = PTR_ERR(handle));
2091 memset(ldata, 0, sizeof(*ldata));
2092 mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2093 lname, 1, 0, ldata);
2095 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2096 handle, spec, ldata, &def_acl_buf, &acl_buf);
2100 rc = mdd_trans_start(env, mdd, handle);
2104 mdd_write_lock(env, son, MOR_TGT_CHILD);
2105 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
2107 mdd_write_unlock(env, son);
2113 #ifdef CONFIG_FS_POSIX_ACL
2114 if (def_acl_buf.lb_len > 0 && S_ISDIR(attr->la_mode)) {
2115 /* set default acl */
2116 rc = mdo_xattr_set(env, son, &def_acl_buf,
2117 XATTR_NAME_ACL_DEFAULT, 0,
2118 handle, BYPASS_CAPA);
2120 mdd_write_unlock(env, son);
2124 /* set its own acl */
2125 if (acl_buf.lb_len > 0) {
2126 rc = mdo_xattr_set(env, son, &acl_buf,
2127 XATTR_NAME_ACL_ACCESS,
2128 0, handle, BYPASS_CAPA);
2130 mdd_write_unlock(env, son);
2136 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2137 son, attr, handle, spec, ldata);
2140 * in case of replay we just set LOVEA provided by the client
2141 * XXX: I think it would be interesting to try "old" way where
2142 * MDT calls this xattr_set(LOV) in a different transaction.
2143 * probably this way we code can be made better.
2145 if (rc == 0 && (spec->no_create ||
2146 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
2147 const struct lu_buf *buf;
2149 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2150 spec->u.sp_ea.eadatalen);
2151 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2155 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2156 rc = __mdd_orphan_add(env, son, handle);
2158 mdd_write_unlock(env, son);
2162 * Object has no links, so it will be destroyed when last
2163 * reference is released. (XXX not now.)
2169 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
2170 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2171 name, S_ISDIR(attr->la_mode), handle,
2172 mdd_object_capa(env, mdd_pobj));
2179 if (S_ISLNK(attr->la_mode)) {
2180 struct lu_ucred *uc = lu_ucred_assert(env);
2181 struct dt_object *dt = mdd_object_child(son);
2182 const char *target_name = spec->u.sp_symname;
2183 int sym_len = strlen(target_name);
2184 const struct lu_buf *buf;
2187 buf = mdd_buf_get_const(env, target_name, sym_len);
2188 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2189 mdd_object_capa(env, son),
2191 CFS_CAP_SYS_RESOURCE_MASK);
2196 GOTO(cleanup, rc = -EFAULT);
2199 /* volatile file creation does not update parent directory times */
2200 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2201 GOTO(cleanup, rc = 0);
2203 /* update parent directory mtime/ctime */
2205 la->la_valid = LA_CTIME | LA_MTIME;
2206 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2212 if (rc != 0 && created != 0) {
2215 if (inserted != 0) {
2216 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2217 rc2 = __mdd_orphan_del(env, son, handle);
2219 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2220 S_ISDIR(attr->la_mode),
2221 handle, BYPASS_CAPA);
2226 mdd_write_lock(env, son, MOR_TGT_CHILD);
2227 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2228 /* Drop the reference, no need to delete "."/"..",
2229 * because the object to be destroied directly. */
2230 rc2 = mdo_ref_del(env, son, handle);
2232 mdd_write_unlock(env, son);
2237 rc2 = mdo_ref_del(env, son, handle);
2239 mdd_write_unlock(env, son);
2243 mdo_destroy(env, son, handle);
2244 mdd_write_unlock(env, son);
2247 if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2248 rc = mdd_changelog_ns_store(env, mdd,
2249 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2250 S_ISREG(attr->la_mode) ? CL_CREATE :
2251 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2252 0, son, mdd_pobj, lname, handle);
2254 mdd_trans_stop(env, mdd, rc, handle);
2256 if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2257 /* if we vmalloced a large buffer drop it */
2258 lu_buf_free(ldata->ld_buf);
2260 /* The child object shouldn't be cached anymore */
2262 set_bit(LU_OBJECT_HEARD_BANSHEE,
2263 &child->mo_lu.lo_header->loh_flags);
2268 * Get locks on parents in proper order
2269 * RETURN: < 0 - error, rename_order if successful
2277 static int mdd_rename_order(const struct lu_env *env,
2278 struct mdd_device *mdd,
2279 struct mdd_object *src_pobj,
2280 const struct lu_attr *pattr,
2281 struct mdd_object *tgt_pobj)
2283 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2287 if (src_pobj == tgt_pobj)
2288 RETURN(MDD_RN_SAME);
2290 /* compared the parent child relationship of src_p&tgt_p */
2291 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2293 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2296 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2310 /* has not mdd_write{read}_lock on any obj yet. */
2311 static int mdd_rename_sanity_check(const struct lu_env *env,
2312 struct mdd_object *src_pobj,
2313 const struct lu_attr *pattr,
2314 struct mdd_object *tgt_pobj,
2315 const struct lu_attr *tpattr,
2316 struct mdd_object *sobj,
2317 const struct lu_attr *cattr,
2318 struct mdd_object *tobj,
2319 const struct lu_attr *tattr)
2324 /* XXX: when get here, sobj must NOT be NULL,
2325 * the other case has been processed in cld_rename
2326 * before mdd_rename and enable MDS_PERM_BYPASS. */
2329 rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2333 /* XXX: when get here, "tobj == NULL" means tobj must
2334 * NOT exist (neither on remote MDS, such case has been
2335 * processed in cld_rename before mdd_rename and enable
2337 * So check may_create, but not check may_unlink. */
2339 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2340 (src_pobj != tgt_pobj), 0);
2342 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2343 (src_pobj != tgt_pobj), 1);
2345 if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2346 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2351 static int mdd_declare_rename(const struct lu_env *env,
2352 struct mdd_device *mdd,
2353 struct mdd_object *mdd_spobj,
2354 struct mdd_object *mdd_tpobj,
2355 struct mdd_object *mdd_sobj,
2356 struct mdd_object *mdd_tobj,
2357 const struct lu_name *tname,
2358 const struct lu_name *sname,
2360 struct linkea_data *ldata,
2361 struct thandle *handle)
2363 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2366 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2367 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2373 /* name from source dir */
2374 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2378 /* .. from source child */
2379 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2380 /* source child can be directory,
2381 * counted by source dir's nlink */
2382 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2385 if (mdd_spobj != mdd_tpobj) {
2386 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2391 rc = mdo_declare_index_insert(env, mdd_sobj,
2397 /* new target child can be directory,
2398 * counted by target dir's nlink */
2399 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2404 la->la_valid = LA_CTIME | LA_MTIME;
2405 rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2409 rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2413 la->la_valid = LA_CTIME;
2414 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2418 rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2423 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2424 tname->ln_name, handle);
2428 /* name from target dir (old name), we declare it unconditionally
2429 * as mdd_rename() calls delete unconditionally as well. so just
2430 * to balance declarations vs calls to change ... */
2431 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2435 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2436 /* delete target child in target parent directory */
2437 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2441 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2442 /* target child can be directory,
2443 * delete "." reference in target child directory */
2444 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2448 /* delete ".." reference in target parent directory */
2449 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2454 la->la_valid = LA_CTIME;
2455 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2459 rc = mdd_declare_links_del(env, mdd_tobj, handle);
2463 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2468 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2475 /* src object can be remote that is why we use only fid and type of object */
2476 static int mdd_rename(const struct lu_env *env,
2477 struct md_object *src_pobj, struct md_object *tgt_pobj,
2478 const struct lu_fid *lf, const struct lu_name *lsname,
2479 struct md_object *tobj, const struct lu_name *ltname,
2482 const char *sname = lsname->ln_name;
2483 const char *tname = ltname->ln_name;
2484 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2485 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2486 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2487 struct mdd_device *mdd = mdo2mdd(src_pobj);
2488 struct mdd_object *mdd_sobj = NULL; /* source object */
2489 struct mdd_object *mdd_tobj = NULL;
2490 struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2491 struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2492 struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2493 struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2494 struct thandle *handle;
2495 struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
2496 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2497 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2500 bool tobj_locked = 0;
2501 unsigned cl_flags = 0;
2506 mdd_tobj = md2mdd_obj(tobj);
2508 mdd_sobj = mdd_object_find(env, mdd, lf);
2510 rc = mdd_la_get(env, mdd_sobj, cattr, BYPASS_CAPA);
2512 GOTO(out_pending, rc);
2514 rc = mdd_la_get(env, mdd_spobj, pattr, BYPASS_CAPA);
2516 GOTO(out_pending, rc);
2519 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2521 GOTO(out_pending, rc);
2524 rc = mdd_la_get(env, mdd_tpobj, tpattr, BYPASS_CAPA);
2526 GOTO(out_pending, rc);
2528 rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2529 mdd_sobj, cattr, mdd_tobj, tattr);
2531 GOTO(out_pending, rc);
2533 handle = mdd_trans_create(env, mdd);
2535 GOTO(out_pending, rc = PTR_ERR(handle));
2537 memset(ldata, 0, sizeof(*ldata));
2538 mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2539 ltname, 1, 0, ldata);
2540 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2541 mdd_tobj, lsname, ltname, ma, ldata, handle);
2545 rc = mdd_trans_start(env, mdd, handle);
2549 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2550 rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2552 GOTO(cleanup_unlocked, rc);
2554 is_dir = S_ISDIR(cattr->la_mode);
2556 /* Remove source name from source directory */
2557 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2558 mdd_object_capa(env, mdd_spobj));
2562 /* "mv dir1 dir2" needs "dir1/.." link update */
2563 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2564 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2565 mdd_object_capa(env, mdd_sobj));
2567 GOTO(fixup_spobj2, rc);
2569 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2570 handle, mdd_object_capa(env, mdd_sobj));
2572 GOTO(fixup_spobj, rc);
2575 /* Remove target name from target directory
2576 * Here tobj can be remote one, so we do index_delete unconditionally
2577 * and -ENOENT is allowed.
2579 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2580 mdd_object_capa(env, mdd_tpobj));
2583 /* tname might been renamed to something else */
2584 GOTO(fixup_spobj, rc);
2587 GOTO(fixup_spobj, rc);
2590 /* Insert new fid with target name into target dir */
2591 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2592 mdd_object_capa(env, mdd_tpobj));
2594 GOTO(fixup_tpobj, rc);
2596 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2597 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2599 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2601 la->la_valid = LA_CTIME;
2602 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2604 GOTO(fixup_tpobj, rc);
2607 /* Remove old target object
2608 * For tobj is remote case cmm layer has processed
2609 * and set tobj to NULL then. So when tobj is NOT NULL,
2610 * it must be local one.
2612 if (tobj && mdd_object_exists(mdd_tobj)) {
2613 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2615 if (mdd_is_dead_obj(mdd_tobj)) {
2616 /* shld not be dead, something is wrong */
2617 CERROR("tobj is dead, something is wrong\n");
2621 mdo_ref_del(env, mdd_tobj, handle);
2623 /* Remove dot reference. */
2624 if (S_ISDIR(tattr->la_mode))
2625 mdo_ref_del(env, mdd_tobj, handle);
2628 /* fetch updated nlink */
2629 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2631 CERROR("%s: Failed to get nlink for tobj "
2633 mdd2obd_dev(mdd)->obd_name,
2634 PFID(tpobj_fid), rc);
2635 GOTO(fixup_tpobj, rc);
2638 la->la_valid = LA_CTIME;
2639 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2641 CERROR("%s: Failed to set ctime for tobj "
2643 mdd2obd_dev(mdd)->obd_name,
2644 PFID(tpobj_fid), rc);
2645 GOTO(fixup_tpobj, rc);
2648 /* XXX: this transfer to ma will be removed with LOD/OSP */
2649 ma->ma_attr = *tattr;
2650 ma->ma_valid |= MA_INODE;
2651 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2653 CERROR("%s: Failed to unlink tobj "
2655 mdd2obd_dev(mdd)->obd_name,
2656 PFID(tpobj_fid), rc);
2657 GOTO(fixup_tpobj, rc);
2660 /* fetch updated nlink */
2661 rc = mdd_la_get(env, mdd_tobj, tattr, BYPASS_CAPA);
2663 CERROR("%s: Failed to get nlink for tobj "
2665 mdd2obd_dev(mdd)->obd_name,
2666 PFID(tpobj_fid), rc);
2667 GOTO(fixup_tpobj, rc);
2669 /* XXX: this transfer to ma will be removed with LOD/OSP */
2670 ma->ma_attr = *tattr;
2671 ma->ma_valid |= MA_INODE;
2673 if (tattr->la_nlink == 0) {
2674 cl_flags |= CLF_RENAME_LAST;
2675 if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2676 cl_flags |= CLF_RENAME_LAST_EXISTS;
2680 la->la_valid = LA_CTIME | LA_MTIME;
2681 rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2683 GOTO(fixup_tpobj, rc);
2685 if (mdd_spobj != mdd_tpobj) {
2686 la->la_valid = LA_CTIME | LA_MTIME;
2687 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2690 if (rc == 0 && mdd_sobj) {
2691 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2692 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2693 mdo2fid(mdd_tpobj), ltname, handle, NULL,
2696 /* Old files might not have EA entry */
2697 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2698 lsname, handle, NULL, 0);
2699 mdd_write_unlock(env, mdd_sobj);
2700 /* We don't fail the transaction if the link ea can't be
2701 updated -- fid2path will use alternate lookup method. */
2709 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2712 CWARN("tp obj fix error %d\n",rc2);
2714 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2715 !mdd_is_dead_obj(mdd_tobj)) {
2717 mdo_ref_add(env, mdd_tobj, handle);
2719 mdo_ref_add(env, mdd_tobj, handle);
2722 rc2 = __mdd_index_insert(env, mdd_tpobj,
2723 mdo2fid(mdd_tobj), tname,
2728 CWARN("tp obj fix error %d\n",rc2);
2733 if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2734 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2738 CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2739 mdd2obd_dev(mdd)->obd_name, rc2);
2742 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2743 dotdot, handle, BYPASS_CAPA);
2745 CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2746 mdd2obd_dev(mdd)->obd_name, rc2);
2751 rc2 = __mdd_index_insert(env, mdd_spobj,
2752 lf, sname, is_dir, handle, BYPASS_CAPA);
2754 CWARN("sp obj fix error %d\n",rc2);
2758 mdd_write_unlock(env, mdd_tobj);
2761 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2762 mdd_tobj, tpobj_fid, lf,
2763 spobj_fid, ltname, lsname,
2767 mdd_trans_stop(env, mdd, rc, handle);
2769 mdd_object_put(env, mdd_sobj);
2773 const struct md_dir_operations mdd_dir_ops = {
2774 .mdo_is_subdir = mdd_is_subdir,
2775 .mdo_lookup = mdd_lookup,
2776 .mdo_create = mdd_create,
2777 .mdo_rename = mdd_rename,
2778 .mdo_link = mdd_link,
2779 .mdo_unlink = mdd_unlink,
2780 .mdo_create_data = mdd_create_data,