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, 2012, 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@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
61 const struct lu_name *lname, struct lu_fid* fid,
63 static inline int mdd_links_add(const struct lu_env *env,
64 struct mdd_object *mdd_obj,
65 const struct lu_fid *pfid,
66 const struct lu_name *lname,
67 struct thandle *handle, int first);
68 static inline int mdd_links_del(const struct lu_env *env,
69 struct mdd_object *mdd_obj,
70 const struct lu_fid *pfid,
71 const struct lu_name *lname,
72 struct thandle *handle);
73 static int mdd_links_rename(const struct lu_env *env,
74 struct mdd_object *mdd_obj,
75 const struct lu_fid *oldpfid,
76 const struct lu_name *oldlname,
77 const struct lu_fid *newpfid,
78 const struct lu_name *newlname,
79 struct thandle *handle,
80 int first, int check);
83 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
84 const struct lu_name *lname, struct lu_fid* fid, int mask)
86 const char *name = lname->ln_name;
87 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
88 struct dynlock_handle *dlh;
91 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
92 if (unlikely(dlh == NULL))
94 rc = __mdd_lookup(env, pobj, lname, fid, mask);
95 mdd_pdo_read_unlock(env, mdd_obj, dlh);
100 int mdd_lookup(const struct lu_env *env,
101 struct md_object *pobj, const struct lu_name *lname,
102 struct lu_fid* fid, struct md_op_spec *spec)
106 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
110 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
113 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
117 * For root fid use special function, which does not compare version component
118 * of fid. Version component is different for root fids on all MDTs.
120 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
122 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
123 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
127 * return 1: if lf is the fid of the ancestor of p1;
130 * return -EREMOTE: if remote object is found, in this
131 * case fid of remote object is saved to @pf;
133 * otherwise: values < 0, errors.
135 static int mdd_is_parent(const struct lu_env *env,
136 struct mdd_device *mdd,
137 struct mdd_object *p1,
138 const struct lu_fid *lf,
141 struct mdd_object *parent = NULL;
146 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
147 pfid = &mdd_env_info(env)->mti_fid;
149 /* Check for root first. */
150 if (mdd_is_root(mdd, mdo2fid(p1)))
154 /* this is done recursively, bypass capa for each obj */
155 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
156 rc = mdd_parent_fid(env, p1, pfid);
159 if (mdd_is_root(mdd, pfid))
161 if (lu_fid_eq(pfid, lf))
164 mdd_object_put(env, parent);
166 parent = mdd_object_find(env, mdd, pfid);
167 if (IS_ERR(parent)) {
168 GOTO(out, rc = PTR_ERR(parent));
169 } else if (mdd_object_remote(parent)) {
170 /*FIXME: Because of the restriction of rename in Phase I.
171 * If the parent is remote, we just assumed lf is not the
172 * parent of P1 for now */
179 if (parent && !IS_ERR(parent))
180 mdd_object_put(env, parent);
185 * No permission check is needed.
187 * returns 1: if fid is ancestor of @mo;
188 * returns 0: if fid is not a ancestor of @mo;
190 * returns EREMOTE if remote object is found, fid of remote object is saved to
193 * returns < 0: if error
195 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
196 const struct lu_fid *fid, struct lu_fid *sfid)
198 struct mdd_device *mdd = mdo2mdd(mo);
202 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
205 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
209 } else if (rc == 1) {
210 /* found @fid is parent */
218 * Check that @dir contains no entries except (possibly) dot and dotdot.
223 * -ENOTDIR not a directory object
224 * -ENOTEMPTY not empty
228 static int mdd_dir_is_empty(const struct lu_env *env,
229 struct mdd_object *dir)
232 struct dt_object *obj;
233 const struct dt_it_ops *iops;
237 obj = mdd_object_child(dir);
238 if (!dt_try_as_dir(env, obj))
241 iops = &obj->do_index_ops->dio_it;
242 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
244 result = iops->get(env, it, (const void *)"");
247 for (result = 0, i = 0; result == 0 && i < 3; ++i)
248 result = iops->next(env, it);
251 else if (result == +1)
253 } else if (result == 0)
255 * Huh? Index contains no zero key?
262 result = PTR_ERR(it);
266 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
268 struct mdd_device *m = mdd_obj2mdd_dev(obj);
269 struct lu_attr *la = &mdd_env_info(env)->mti_la;
273 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
278 * Subdir count limitation can be broken through.
280 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
281 !S_ISDIR(la->la_mode))
288 * Check whether it may create the cobj under the pobj.
291 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
292 struct mdd_object *cobj, int check_perm, int check_nlink)
297 if (cobj && mdd_object_exists(cobj))
300 if (mdd_is_dead_obj(pobj))
304 rc = mdd_permission_internal_locked(env, pobj, NULL,
305 MAY_WRITE | MAY_EXEC,
307 if (!rc && check_nlink)
308 rc = __mdd_may_link(env, pobj);
314 * Check whether can unlink from the pobj in the case of "cobj == NULL".
316 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
317 const struct lu_attr *attr)
322 if (mdd_is_dead_obj(pobj))
325 if ((attr->la_valid & LA_FLAGS) &&
326 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
329 rc = mdd_permission_internal_locked(env, pobj, NULL,
330 MAY_WRITE | MAY_EXEC,
335 if (mdd_is_append(pobj))
342 * pobj == NULL is remote ops case, under such case, pobj's
343 * VTX feature has been checked already, no need check again.
345 static inline int mdd_is_sticky(const struct lu_env *env,
346 struct mdd_object *pobj,
347 struct mdd_object *cobj)
349 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
350 struct lu_ucred *uc = lu_ucred_assert(env);
354 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
358 if (!(tmp_la->la_mode & S_ISVTX) ||
359 (tmp_la->la_uid == uc->uc_fsuid))
363 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
367 if (tmp_la->la_uid == uc->uc_fsuid)
370 return !md_capable(uc, CFS_CAP_FOWNER);
373 static int mdd_may_delete_entry(const struct lu_env *env,
374 struct mdd_object *pobj, int check_perm)
378 LASSERT(pobj != NULL);
379 if (!mdd_object_exists(pobj))
382 if (mdd_is_dead_obj(pobj))
387 rc = mdd_permission_internal_locked(env, pobj, NULL,
388 MAY_WRITE | MAY_EXEC,
394 if (mdd_is_append(pobj))
401 * Check whether it may delete the cobj from the pobj.
404 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
405 struct mdd_object *cobj, struct lu_attr *cattr,
406 struct lu_attr *src_attr, int check_perm, int check_empty)
412 rc = mdd_may_delete_entry(env, pobj, check_perm);
420 if (!mdd_object_exists(cobj))
423 if (mdd_is_dead_obj(cobj))
427 if (mdd_is_sticky(env, pobj, cobj))
430 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
433 if ((cattr->la_valid & LA_FLAGS) &&
434 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
437 /* additional check the rename case */
439 if (S_ISDIR(src_attr->la_mode)) {
440 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
442 if (!S_ISDIR(cattr->la_mode))
445 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
447 } else if (S_ISDIR(cattr->la_mode))
451 if (S_ISDIR(cattr->la_mode) && check_empty)
452 rc = mdd_dir_is_empty(env, cobj);
459 * has mdd_write_lock on src already, but not on tgt yet
461 int mdd_link_sanity_check(const struct lu_env *env,
462 struct mdd_object *tgt_obj,
463 const struct lu_name *lname,
464 struct mdd_object *src_obj)
466 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
470 if (!mdd_object_exists(src_obj))
473 if (mdd_is_dead_obj(src_obj))
476 /* Local ops, no lookup before link, check filename length here. */
477 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
478 RETURN(-ENAMETOOLONG);
480 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
483 if (S_ISDIR(mdd_object_type(src_obj)))
486 LASSERT(src_obj != tgt_obj);
488 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
493 rc = __mdd_may_link(env, src_obj);
498 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
499 const char *name, struct thandle *handle,
500 struct lustre_capa *capa)
502 struct dt_object *next = mdd_object_child(pobj);
506 if (dt_try_as_dir(env, next)) {
507 rc = next->do_index_ops->dio_delete(env, next,
508 (struct dt_key *)name,
516 static int __mdd_index_insert_only(const struct lu_env *env,
517 struct mdd_object *pobj,
518 const struct lu_fid *lf, const char *name,
519 struct thandle *handle,
520 struct lustre_capa *capa)
522 struct dt_object *next = mdd_object_child(pobj);
526 if (dt_try_as_dir(env, next)) {
527 struct lu_ucred *uc = lu_ucred_check(env);
530 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
531 rc = next->do_index_ops->dio_insert(env, next,
533 (const struct dt_key *)name,
534 handle, capa, ignore_quota);
541 /* insert named index, add reference if isdir */
542 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
543 const struct lu_fid *lf, const char *name, int is_dir,
544 struct thandle *handle, struct lustre_capa *capa)
549 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
550 if (rc == 0 && is_dir) {
551 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
552 mdo_ref_add(env, pobj, handle);
553 mdd_write_unlock(env, pobj);
558 /* delete named index, drop reference if isdir */
559 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
560 const char *name, int is_dir, struct thandle *handle,
561 struct lustre_capa *capa)
566 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
567 if (rc == 0 && is_dir) {
568 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
569 mdo_ref_del(env, pobj, handle);
570 mdd_write_unlock(env, pobj);
576 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
577 int reclen, struct thandle *handle)
581 /* XXX: this is a temporary solution to declare llog changes
582 * will be fixed in 2.3 with new llog implementation */
584 LASSERT(mdd->mdd_capa);
586 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
587 * have to set the parameter 'size' as INT_MAX or 0 to inform
588 * OSD that this record write is for a llog write or catalog
589 * header update, and osd declare function will reserve less
590 * credits for optimization purpose.
592 * Reserve 6 blocks for a llog write, since the llog file is
593 * usually small, reserve 2 blocks for catalog header update,
594 * because we know for sure that catalog header is already
597 * This hack should be removed in 2.3.
601 rc = dt_declare_record_write(env, mdd->mdd_capa,
602 DECLARE_LLOG_WRITE, 0, handle);
606 /* header will be updated as well */
607 rc = dt_declare_record_write(env, mdd->mdd_capa,
608 DECLARE_LLOG_WRITE, 0, handle);
612 /* also we should be able to create new plain log */
613 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
617 /* new record referencing new plain llog */
618 rc = dt_declare_record_write(env, mdd->mdd_capa,
619 DECLARE_LLOG_WRITE, 0, handle);
623 /* catalog's header will be updated as well */
624 rc = dt_declare_record_write(env, mdd->mdd_capa,
625 DECLARE_LLOG_REWRITE, 0, handle);
630 int mdd_declare_changelog_store(const struct lu_env *env,
631 struct mdd_device *mdd,
632 const struct lu_name *fname,
633 struct thandle *handle)
635 struct obd_device *obd = mdd2obd_dev(mdd);
636 struct llog_ctxt *ctxt;
637 struct llog_changelog_rec *rec;
643 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
646 reclen = llog_data_len(sizeof(*rec) +
647 (fname != NULL ? fname->ln_namelen : 0));
648 buf = mdd_buf_alloc(env, reclen);
649 if (buf->lb_buf == NULL)
653 rec->cr_hdr.lrh_len = reclen;
654 rec->cr_hdr.lrh_type = CHANGELOG_REC;
656 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
660 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
666 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
667 struct mdd_device *mdd,
668 const struct lu_name *tname,
669 const struct lu_name *sname,
670 struct thandle *handle)
672 struct obd_device *obd = mdd2obd_dev(mdd);
673 struct llog_ctxt *ctxt;
674 struct llog_changelog_ext_rec *rec;
680 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
683 reclen = llog_data_len(sizeof(*rec) +
684 (tname != NULL ? tname->ln_namelen : 0) +
685 (sname != NULL ? 1 + sname->ln_namelen : 0));
686 buf = mdd_buf_alloc(env, reclen);
687 if (buf->lb_buf == NULL)
691 rec->cr_hdr.lrh_len = reclen;
692 rec->cr_hdr.lrh_type = CHANGELOG_REC;
694 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
698 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
704 /** Add a changelog entry \a rec to the changelog llog
707 * \param handle - currently ignored since llogs start their own transaction;
708 * this will hopefully be fixed in llog rewrite
711 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
712 struct llog_changelog_rec *rec, struct thandle *th)
714 struct obd_device *obd = mdd2obd_dev(mdd);
715 struct llog_ctxt *ctxt;
718 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
719 rec->cr_hdr.lrh_type = CHANGELOG_REC;
720 rec->cr.cr_time = cl_time();
722 spin_lock(&mdd->mdd_cl.mc_lock);
723 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
724 * but as long as the MDD transactions are ordered correctly for e.g.
725 * rename conflicts, I don't think this should matter. */
726 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
727 spin_unlock(&mdd->mdd_cl.mc_lock);
729 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
733 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
740 /** Add a changelog_ext entry \a rec to the changelog llog
743 * \param handle - currently ignored since llogs start their own transaction;
744 * this will hopefully be fixed in llog rewrite
747 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
748 struct llog_changelog_ext_rec *rec,
751 struct obd_device *obd = mdd2obd_dev(mdd);
752 struct llog_ctxt *ctxt;
755 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
756 /* llog_lvfs_write_rec sets the llog tail len */
757 rec->cr_hdr.lrh_type = CHANGELOG_REC;
758 rec->cr.cr_time = cl_time();
760 spin_lock(&mdd->mdd_cl.mc_lock);
761 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
762 * but as long as the MDD transactions are ordered correctly for e.g.
763 * rename conflicts, I don't think this should matter. */
764 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
765 spin_unlock(&mdd->mdd_cl.mc_lock);
767 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
771 /* nested journal transaction */
772 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
780 /** Store a namespace change changelog record
781 * If this fails, we must fail the whole transaction; we don't
782 * want the change to commit without the log entry.
783 * \param target - mdd_object of change
784 * \param parent - parent dir/object
785 * \param tname - target name string
786 * \param handle - transacion handle
788 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
789 enum changelog_rec_type type, unsigned flags,
790 struct mdd_object *target, struct mdd_object *parent,
791 const struct lu_name *tname, struct thandle *handle)
793 struct llog_changelog_rec *rec;
800 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
802 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
805 LASSERT(target != NULL);
806 LASSERT(parent != NULL);
807 LASSERT(tname != NULL);
808 LASSERT(handle != NULL);
810 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
811 buf = mdd_buf_alloc(env, reclen);
812 if (buf->lb_buf == NULL)
816 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
817 rec->cr.cr_type = (__u32)type;
818 rec->cr.cr_tfid = *mdo2fid(target);
819 rec->cr.cr_pfid = *mdo2fid(parent);
820 rec->cr.cr_namelen = tname->ln_namelen;
821 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
823 target->mod_cltime = cfs_time_current_64();
825 rc = mdd_changelog_store(env, mdd, rec, handle);
827 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
828 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
829 PFID(&rec->cr.cr_pfid));
837 /** Store a namespace change changelog record
838 * If this fails, we must fail the whole transaction; we don't
839 * want the change to commit without the log entry.
840 * \param target - mdd_object of change
841 * \param tpfid - target parent dir/object fid
842 * \param sfid - source object fid
843 * \param spfid - source parent fid
844 * \param tname - target name string
845 * \param sname - source name string
846 * \param handle - transacion handle
848 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
849 struct mdd_device *mdd,
850 enum changelog_rec_type type,
852 struct mdd_object *target,
853 const struct lu_fid *tpfid,
854 const struct lu_fid *sfid,
855 const struct lu_fid *spfid,
856 const struct lu_name *tname,
857 const struct lu_name *sname,
858 struct thandle *handle)
860 struct llog_changelog_ext_rec *rec;
867 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
869 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
872 LASSERT(sfid != NULL);
873 LASSERT(tpfid != NULL);
874 LASSERT(tname != NULL);
875 LASSERT(handle != NULL);
877 reclen = llog_data_len(sizeof(*rec) +
878 sname != NULL ? 1 + sname->ln_namelen : 0);
879 buf = mdd_buf_alloc(env, reclen);
880 if (buf->lb_buf == NULL)
884 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
885 rec->cr.cr_type = (__u32)type;
886 rec->cr.cr_pfid = *tpfid;
887 rec->cr.cr_sfid = *sfid;
888 rec->cr.cr_spfid = *spfid;
889 rec->cr.cr_namelen = tname->ln_namelen;
890 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
892 rec->cr.cr_name[tname->ln_namelen] = '\0';
893 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
895 rec->cr.cr_namelen += 1 + sname->ln_namelen;
898 if (likely(target != NULL)) {
899 rec->cr.cr_tfid = *mdo2fid(target);
900 target->mod_cltime = cfs_time_current_64();
902 fid_zero(&rec->cr.cr_tfid);
905 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
907 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
908 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
915 static int mdd_declare_link(const struct lu_env *env,
916 struct mdd_device *mdd,
917 struct mdd_object *p,
918 struct mdd_object *c,
919 const struct lu_name *name,
920 struct thandle *handle)
924 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
928 rc = mdo_declare_ref_add(env, c, handle);
932 rc = mdo_declare_attr_set(env, p, NULL, handle);
936 rc = mdo_declare_attr_set(env, c, NULL, handle);
940 rc = mdd_declare_links_add(env, c, handle);
944 rc = mdd_declare_changelog_store(env, mdd, name, handle);
949 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
950 struct md_object *src_obj, const struct lu_name *lname,
953 const char *name = lname->ln_name;
954 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
955 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
956 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
957 struct mdd_device *mdd = mdo2mdd(src_obj);
958 struct dynlock_handle *dlh;
959 struct thandle *handle;
963 handle = mdd_trans_create(env, mdd);
965 GOTO(out_pending, rc = PTR_ERR(handle));
967 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
971 rc = mdd_trans_start(env, mdd, handle);
975 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
977 GOTO(out_trans, rc = -ENOMEM);
978 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
980 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
982 GOTO(out_unlock, rc);
984 rc = mdo_ref_add(env, mdd_sobj, handle);
986 GOTO(out_unlock, rc);
989 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
991 mdd_object_capa(env, mdd_tobj));
993 mdo_ref_del(env, mdd_sobj, handle);
994 GOTO(out_unlock, rc);
997 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
998 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1000 la->la_valid = LA_CTIME | LA_MTIME;
1001 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1003 GOTO(out_unlock, rc);
1005 la->la_valid = LA_CTIME;
1006 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1008 mdd_links_add(env, mdd_sobj,
1009 mdo2fid(mdd_tobj), lname, handle, 0);
1014 mdd_write_unlock(env, mdd_sobj);
1015 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1018 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1019 mdd_tobj, lname, handle);
1021 mdd_trans_stop(env, mdd, rc, handle);
1026 int mdd_declare_finish_unlink(const struct lu_env *env,
1027 struct mdd_object *obj,
1029 struct thandle *handle)
1033 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1037 return mdo_declare_destroy(env, obj, handle);
1040 /* caller should take a lock before calling */
1041 int mdd_finish_unlink(const struct lu_env *env,
1042 struct mdd_object *obj, struct md_attr *ma,
1046 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1049 LASSERT(mdd_write_locked(env, obj) != 0);
1051 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
1052 obj->mod_flags |= DEAD_OBJ;
1053 /* add new orphan and the object
1054 * will be deleted during mdd_close() */
1055 if (obj->mod_count) {
1056 rc = __mdd_orphan_add(env, obj, th);
1058 CDEBUG(D_HA, "Object "DFID" is inserted into "
1059 "orphan list, open count = %d\n",
1060 PFID(mdd_object_fid(obj)),
1063 CERROR("Object "DFID" fail to be an orphan, "
1064 "open count = %d, maybe cause failed "
1066 PFID(mdd_object_fid(obj)),
1069 rc = mdo_destroy(env, obj, th);
1078 * has mdd_write_lock on cobj already, but not on pobj yet
1080 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1081 struct mdd_object *cobj, struct lu_attr *cattr)
1086 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1091 static inline int mdd_declare_links_del(const struct lu_env *env,
1092 struct mdd_object *c,
1093 struct thandle *handle)
1097 /* For directory, the linkEA will be removed together with the object. */
1098 if (!S_ISDIR(mdd_object_type(c)))
1099 rc = mdd_declare_links_add(env, c, handle);
1104 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1105 struct mdd_object *p, struct mdd_object *c,
1106 const struct lu_name *name, struct md_attr *ma,
1107 struct thandle *handle)
1109 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1112 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1116 rc = mdo_declare_ref_del(env, p, handle);
1120 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1121 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1122 la->la_valid = LA_CTIME | LA_MTIME;
1123 rc = mdo_declare_attr_set(env, p, la, handle);
1128 rc = mdo_declare_ref_del(env, c, handle);
1132 rc = mdo_declare_ref_del(env, c, handle);
1136 rc = mdo_declare_attr_set(env, c, NULL, handle);
1140 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1144 rc = mdd_declare_links_del(env, c, handle);
1148 /* FIXME: need changelog for remove entry */
1149 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1155 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1156 struct md_object *cobj, const struct lu_name *lname,
1159 const char *name = lname->ln_name;
1160 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1161 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1162 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1163 struct mdd_object *mdd_cobj = NULL;
1164 struct mdd_device *mdd = mdo2mdd(pobj);
1165 struct dynlock_handle *dlh;
1166 struct thandle *handle;
1170 /* cobj == NULL means only delete name entry */
1171 if (likely(cobj != NULL)) {
1172 mdd_cobj = md2mdd_obj(cobj);
1173 if (mdd_object_exists(mdd_cobj) == 0)
1175 /* currently it is assume, it could only delete
1176 * name entry of remote directory */
1180 handle = mdd_trans_create(env, mdd);
1182 RETURN(PTR_ERR(handle));
1184 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1189 rc = mdd_trans_start(env, mdd, handle);
1193 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1195 GOTO(stop, rc = -ENOMEM);
1197 if (likely(mdd_cobj != NULL)) {
1198 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1201 rc = mdd_la_get(env, mdd_cobj, cattr,
1202 mdd_object_capa(env, mdd_cobj));
1206 is_dir = S_ISDIR(cattr->la_mode);
1210 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1214 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1215 mdd_object_capa(env, mdd_pobj));
1219 if (likely(mdd_cobj != NULL)) {
1220 rc = mdo_ref_del(env, mdd_cobj, handle);
1222 __mdd_index_insert_only(env, mdd_pobj,
1225 mdd_object_capa(env, mdd_pobj));
1231 mdo_ref_del(env, mdd_cobj, handle);
1233 /* fetch updated nlink */
1234 rc = mdd_la_get(env, mdd_cobj, cattr,
1235 mdd_object_capa(env, mdd_cobj));
1240 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1241 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1243 la->la_valid = LA_CTIME | LA_MTIME;
1244 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1248 /* Enough for only unlink the entry */
1249 if (unlikely(mdd_cobj == NULL)) {
1250 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1254 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1255 /* update ctime of an unlinked file only if it is still
1256 * opened or a link still exists */
1257 la->la_valid = LA_CTIME;
1258 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1263 /* XXX: this transfer to ma will be removed with LOD/OSP */
1264 ma->ma_attr = *cattr;
1265 ma->ma_valid |= MA_INODE;
1266 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1268 /* fetch updated nlink */
1270 rc = mdd_la_get(env, mdd_cobj, cattr,
1271 mdd_object_capa(env, mdd_cobj));
1274 /* old files may not have link ea; ignore errors */
1275 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1277 /* if object is removed then we can't get its attrs, use last get */
1278 if (cattr->la_nlink == 0) {
1279 ma->ma_attr = *cattr;
1280 ma->ma_valid |= MA_INODE;
1284 mdd_write_unlock(env, mdd_cobj);
1285 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1289 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1290 if ((ma->ma_valid & MA_HSM) &&
1291 (ma->ma_hsm.mh_flags & HS_EXISTS))
1292 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1294 rc = mdd_changelog_ns_store(env, mdd,
1295 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1296 mdd_cobj, mdd_pobj, lname, handle);
1300 mdd_trans_stop(env, mdd, rc, handle);
1306 * The permission has been checked when obj created, no need check again.
1308 static int mdd_cd_sanity_check(const struct lu_env *env,
1309 struct mdd_object *obj)
1314 if (!obj || mdd_is_dead_obj(obj))
1321 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1322 struct md_object *cobj, const struct md_op_spec *spec,
1325 struct mdd_device *mdd = mdo2mdd(cobj);
1326 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1327 struct mdd_object *son = md2mdd_obj(cobj);
1328 struct thandle *handle;
1329 const struct lu_buf *buf;
1330 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1334 /* do not let users to create stripes via .lustre/
1335 * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1336 if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1339 rc = mdd_cd_sanity_check(env, son);
1343 if (!md_should_create(spec->sp_cr_flags))
1347 * there are following use cases for this function:
1348 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1349 * striping can be specified or not
1352 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1356 /* calling ->ah_make_hint() is used to transfer information from parent */
1357 mdd_object_make_hint(env, mdd_pobj, son, attr);
1359 handle = mdd_trans_create(env, mdd);
1361 GOTO(out_free, rc = PTR_ERR(handle));
1364 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1365 * Should this be fixed?
1367 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1368 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1369 spec->sp_cr_flags, spec->no_create);
1371 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1372 /* replay case or lfs setstripe */
1373 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1374 spec->u.sp_ea.eadatalen);
1379 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1380 XATTR_NAME_LOV, 0, handle);
1384 rc = mdd_trans_start(env, mdd, handle);
1388 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1389 0, handle, mdd_object_capa(env, son));
1391 mdd_trans_stop(env, mdd, rc, handle);
1396 /* Get fid from name and parent */
1398 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1399 const struct lu_name *lname, struct lu_fid* fid, int mask)
1401 const char *name = lname->ln_name;
1402 const struct dt_key *key = (const struct dt_key *)name;
1403 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1404 struct mdd_device *m = mdo2mdd(pobj);
1405 struct dt_object *dir = mdd_object_child(mdd_obj);
1409 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1412 if (mdd_object_remote(mdd_obj)) {
1413 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1414 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1415 } else if (!mdd_object_exists(mdd_obj)) {
1419 /* The common filename length check. */
1420 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1421 RETURN(-ENAMETOOLONG);
1423 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1428 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1429 dt_try_as_dir(env, dir))) {
1431 rc = dir->do_index_ops->dio_lookup(env, dir,
1432 (struct dt_rec *)fid, key,
1433 mdd_object_capa(env, mdd_obj));
1444 static int mdd_declare_object_initialize(const struct lu_env *env,
1445 struct mdd_object *parent,
1446 struct mdd_object *child,
1447 struct lu_attr *attr,
1448 struct thandle *handle)
1454 * inode mode has been set in creation time, and it's based on umask,
1455 * la_mode and acl, don't set here again! (which will go wrong
1456 * because below function doesn't consider umask).
1457 * I'd suggest set all object attributes in creation time, see above.
1459 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1460 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1461 rc = mdo_declare_attr_set(env, child, attr, handle);
1462 attr->la_valid |= LA_MODE | LA_TYPE;
1463 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1464 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1467 rc = mdo_declare_ref_add(env, child, handle);
1469 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1473 if (rc == 0 && (fid_is_norm(mdo2fid(child)) ||
1474 fid_is_dot_lustre(mdo2fid(child)) ||
1475 fid_is_root(mdo2fid(child))))
1476 mdd_declare_links_add(env, child, handle);
1481 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1482 const struct lu_name *lname, struct mdd_object *child,
1483 struct lu_attr *attr, struct thandle *handle,
1484 const struct md_op_spec *spec)
1490 * Update attributes for child.
1493 * (1) the valid bits should be converted between Lustre and Linux;
1494 * (2) maybe, the child attributes should be set in OSD when creation.
1498 * inode mode has been set in creation time, and it's based on umask,
1499 * la_mode and acl, don't set here again! (which will go wrong
1500 * because below function doesn't consider umask).
1501 * I'd suggest set all object attributes in creation time, see above.
1503 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1504 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1505 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1506 /* arguments are supposed to stay the same */
1507 attr->la_valid |= LA_MODE | LA_TYPE;
1511 if (S_ISDIR(attr->la_mode)) {
1512 /* Add "." and ".." for newly created dir */
1513 mdo_ref_add(env, child, handle);
1514 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1515 dot, handle, BYPASS_CAPA);
1517 rc = __mdd_index_insert_only(env, child, pfid,
1521 mdo_ref_del(env, child, handle);
1524 if (rc == 0 && (fid_is_norm(mdo2fid(child)) ||
1525 fid_is_dot_lustre(mdo2fid(child)) ||
1526 fid_is_root(mdo2fid(child))))
1527 mdd_links_add(env, child, pfid, lname, handle, 1);
1532 /* has not lock on pobj yet */
1533 static int mdd_create_sanity_check(const struct lu_env *env,
1534 struct md_object *pobj,
1535 struct lu_attr *pattr,
1536 const struct lu_name *lname,
1537 struct lu_attr *cattr,
1538 struct md_op_spec *spec)
1540 struct mdd_thread_info *info = mdd_env_info(env);
1541 struct lu_fid *fid = &info->mti_fid;
1542 struct mdd_object *obj = md2mdd_obj(pobj);
1543 struct mdd_device *m = mdo2mdd(pobj);
1548 if (mdd_is_dead_obj(obj))
1552 * In some cases this lookup is not needed - we know before if name
1553 * exists or not because MDT performs lookup for it.
1554 * name length check is done in lookup.
1556 if (spec->sp_cr_lookup) {
1558 * Check if the name already exist, though it will be checked in
1559 * _index_insert also, for avoiding rolling back if exists
1562 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1563 MAY_WRITE | MAY_EXEC);
1565 RETURN(rc ? : -EEXIST);
1568 * Check WRITE permission for the parent.
1569 * EXEC permission have been checked
1570 * when lookup before create already.
1572 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1579 if (pattr->la_mode & S_ISGID) {
1580 cattr->la_gid = pattr->la_gid;
1581 if (S_ISDIR(cattr->la_mode)) {
1582 cattr->la_mode |= S_ISGID;
1583 cattr->la_valid |= LA_MODE;
1587 switch (cattr->la_mode & S_IFMT) {
1589 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1591 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1592 RETURN(-ENAMETOOLONG);
1611 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1612 struct mdd_object *p, struct mdd_object *c,
1613 const struct lu_name *name,
1614 struct lu_attr *attr,
1616 struct thandle *handle,
1617 const struct md_op_spec *spec)
1621 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1625 #ifdef CONFIG_FS_POSIX_ACL
1626 if (got_def_acl > 0) {
1627 struct lu_buf *acl_buf;
1629 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1630 /* if dir, then can inherit default ACl */
1631 if (S_ISDIR(attr->la_mode)) {
1632 rc = mdo_declare_xattr_set(env, c, acl_buf,
1633 XATTR_NAME_ACL_DEFAULT,
1639 rc = mdo_declare_attr_set(env, c, attr, handle);
1643 rc = mdo_declare_xattr_set(env, c, acl_buf,
1644 XATTR_NAME_ACL_ACCESS, 0, handle);
1650 if (S_ISDIR(attr->la_mode)) {
1651 rc = mdo_declare_ref_add(env, p, handle);
1656 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1660 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1661 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1663 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1664 name->ln_name, handle);
1668 /* replay case, create LOV EA from client data */
1669 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1670 const struct lu_buf *buf;
1672 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1673 spec->u.sp_ea.eadatalen);
1674 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1680 if (S_ISLNK(attr->la_mode)) {
1681 rc = dt_declare_record_write(env, mdd_object_child(c),
1682 strlen(spec->u.sp_symname), 0,
1688 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1689 rc = mdo_declare_attr_set(env, p, attr, handle);
1694 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1703 * Create object and insert it into namespace.
1705 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1706 const struct lu_name *lname, struct md_object *child,
1707 struct md_op_spec *spec, struct md_attr* ma)
1709 struct mdd_thread_info *info = mdd_env_info(env);
1710 struct lu_attr *la = &info->mti_la_for_fix;
1711 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1712 struct mdd_object *son = md2mdd_obj(child);
1713 struct mdd_device *mdd = mdo2mdd(pobj);
1714 struct lu_attr *attr = &ma->ma_attr;
1715 struct thandle *handle;
1716 struct lu_attr *pattr = &info->mti_pattr;
1717 struct dynlock_handle *dlh;
1718 const char *name = lname->ln_name;
1719 int rc, created = 0, initialized = 0, inserted = 0;
1720 int got_def_acl = 0;
1724 * Two operations have to be performed:
1726 * - an allocation of a new object (->do_create()), and
1728 * - an insertion into a parent index (->dio_insert()).
1730 * Due to locking, operation order is not important, when both are
1731 * successful, *but* error handling cases are quite different:
1733 * - if insertion is done first, and following object creation fails,
1734 * insertion has to be rolled back, but this operation might fail
1735 * also leaving us with dangling index entry.
1737 * - if creation is done first, is has to be undone if insertion
1738 * fails, leaving us with leaked space, which is neither good, nor
1741 * It seems that creation-first is simplest solution, but it is
1742 * sub-optimal in the frequent
1747 * case, because second mkdir is bound to create object, only to
1748 * destroy it immediately.
1750 * To avoid this follow local file systems that do double lookup:
1752 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1754 * 1. create (mdd_object_create_internal())
1756 * 2. insert (__mdd_index_insert(), lookup again)
1759 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1763 /* Sanity checks before big job. */
1764 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1768 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1769 GOTO(out_free, rc = -EINPROGRESS);
1771 if (!S_ISLNK(attr->la_mode)) {
1772 struct lu_buf *acl_buf;
1774 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1775 sizeof(info->mti_xattr_buf));
1776 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1777 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1778 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1779 mdd_read_unlock(env, mdd_pobj);
1782 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1786 mdd_object_make_hint(env, mdd_pobj, son, attr);
1788 handle = mdd_trans_create(env, mdd);
1790 GOTO(out_free, rc = PTR_ERR(handle));
1792 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1793 got_def_acl, handle, spec);
1797 rc = mdd_trans_start(env, mdd, handle);
1801 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1803 GOTO(out_trans, rc = -ENOMEM);
1805 mdd_write_lock(env, son, MOR_TGT_CHILD);
1806 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
1808 mdd_write_unlock(env, son);
1814 #ifdef CONFIG_FS_POSIX_ACL
1816 struct lu_buf *acl_buf;
1818 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1819 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1821 mdd_write_unlock(env, son);
1827 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1828 son, attr, handle, spec);
1831 * in case of replay we just set LOVEA provided by the client
1832 * XXX: I think it would be interesting to try "old" way where
1833 * MDT calls this xattr_set(LOV) in a different transaction.
1834 * probably this way we code can be made better.
1836 if (rc == 0 && (spec->no_create ||
1837 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1838 const struct lu_buf *buf;
1840 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1841 spec->u.sp_ea.eadatalen);
1842 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1846 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1847 rc = __mdd_orphan_add(env, son, handle);
1849 mdd_write_unlock(env, son);
1853 * Object has no links, so it will be destroyed when last
1854 * reference is released. (XXX not now.)
1860 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
1861 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1862 name, S_ISDIR(attr->la_mode), handle,
1863 mdd_object_capa(env, mdd_pobj));
1870 if (S_ISLNK(attr->la_mode)) {
1871 struct lu_ucred *uc = lu_ucred_assert(env);
1872 struct dt_object *dt = mdd_object_child(son);
1873 const char *target_name = spec->u.sp_symname;
1874 int sym_len = strlen(target_name);
1875 const struct lu_buf *buf;
1878 buf = mdd_buf_get_const(env, target_name, sym_len);
1879 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1880 mdd_object_capa(env, son),
1882 CFS_CAP_SYS_RESOURCE_MASK);
1887 GOTO(cleanup, rc = -EFAULT);
1890 /* volatile file creation does not update parent directory times */
1891 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1892 GOTO(cleanup, rc = 0);
1894 /* update parent directory mtime/ctime */
1896 la->la_valid = LA_CTIME | LA_MTIME;
1897 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1903 if (rc != 0 && created != 0) {
1906 if (inserted != 0) {
1907 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1908 rc2 = __mdd_orphan_del(env, son, handle);
1910 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1911 S_ISDIR(attr->la_mode),
1912 handle, BYPASS_CAPA);
1917 mdd_write_lock(env, son, MOR_TGT_CHILD);
1918 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1919 /* Drop the reference, no need to delete "."/"..",
1920 * because the object to be destroied directly. */
1921 rc2 = mdo_ref_del(env, son, handle);
1923 mdd_write_unlock(env, son);
1928 rc2 = mdo_ref_del(env, son, handle);
1930 mdd_write_unlock(env, son);
1934 mdo_destroy(env, son, handle);
1935 mdd_write_unlock(env, son);
1938 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1940 if (rc == 0 && fid_is_client_mdt_visible(mdo2fid(son)))
1941 rc = mdd_changelog_ns_store(env, mdd,
1942 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1943 S_ISREG(attr->la_mode) ? CL_CREATE :
1944 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1945 0, son, mdd_pobj, lname, handle);
1947 mdd_trans_stop(env, mdd, rc, handle);
1949 /* The child object shouldn't be cached anymore */
1951 set_bit(LU_OBJECT_HEARD_BANSHEE,
1952 &child->mo_lu.lo_header->loh_flags);
1957 * Get locks on parents in proper order
1958 * RETURN: < 0 - error, rename_order if successful
1966 static int mdd_rename_order(const struct lu_env *env,
1967 struct mdd_device *mdd,
1968 struct mdd_object *src_pobj,
1969 struct mdd_object *tgt_pobj)
1971 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1975 if (src_pobj == tgt_pobj)
1976 RETURN(MDD_RN_SAME);
1978 /* compared the parent child relationship of src_p&tgt_p */
1979 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1981 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1984 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1997 /* has not mdd_write{read}_lock on any obj yet. */
1998 static int mdd_rename_sanity_check(const struct lu_env *env,
1999 struct mdd_object *src_pobj,
2000 struct mdd_object *tgt_pobj,
2001 struct mdd_object *sobj,
2002 struct mdd_object *tobj,
2003 struct lu_attr *so_attr,
2004 struct lu_attr *tg_attr)
2009 /* XXX: when get here, sobj must NOT be NULL,
2010 * the other case has been processed in cml_rename
2011 * before mdd_rename and enable MDS_PERM_BYPASS. */
2014 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2018 /* XXX: when get here, "tobj == NULL" means tobj must
2019 * NOT exist (neither on remote MDS, such case has been
2020 * processed in cml_rename before mdd_rename and enable
2022 * So check may_create, but not check may_unlink. */
2024 rc = mdd_may_create(env, tgt_pobj, NULL,
2025 (src_pobj != tgt_pobj), 0);
2027 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2028 (src_pobj != tgt_pobj), 1);
2030 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2031 S_ISDIR(so_attr->la_mode))
2032 rc = __mdd_may_link(env, tgt_pobj);
2037 static int mdd_declare_rename(const struct lu_env *env,
2038 struct mdd_device *mdd,
2039 struct mdd_object *mdd_spobj,
2040 struct mdd_object *mdd_tpobj,
2041 struct mdd_object *mdd_sobj,
2042 struct mdd_object *mdd_tobj,
2043 const struct lu_name *tname,
2044 const struct lu_name *sname,
2046 struct thandle *handle)
2054 /* name from source dir */
2055 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2059 /* .. from source child */
2060 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2061 /* source child can be directory,
2062 * counted by source dir's nlink */
2063 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2067 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2071 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2076 /* new target child can be directory,
2077 * counted by target dir's nlink */
2078 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2084 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2088 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2091 mdd_declare_links_add(env, mdd_sobj, handle);
2095 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2100 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2101 tname->ln_name, handle);
2105 /* name from target dir (old name), we declare it unconditionally
2106 * as mdd_rename() calls delete unconditionally as well. so just
2107 * to balance declarations vs calls to change ... */
2108 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2112 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2113 /* delete target child in target parent directory */
2114 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2118 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2119 /* target child can be directory,
2120 * delete "." reference in target child directory */
2121 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2125 /* delete ".." reference in target parent directory */
2126 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2131 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2135 mdd_declare_links_add(env, mdd_tobj, handle);
2139 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2144 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2151 /* src object can be remote that is why we use only fid and type of object */
2152 static int mdd_rename(const struct lu_env *env,
2153 struct md_object *src_pobj, struct md_object *tgt_pobj,
2154 const struct lu_fid *lf, const struct lu_name *lsname,
2155 struct md_object *tobj, const struct lu_name *ltname,
2158 const char *sname = lsname->ln_name;
2159 const char *tname = ltname->ln_name;
2160 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2161 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2162 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2163 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2164 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2165 struct mdd_device *mdd = mdo2mdd(src_pobj);
2166 struct mdd_object *mdd_sobj = NULL; /* source object */
2167 struct mdd_object *mdd_tobj = NULL;
2168 struct dynlock_handle *sdlh, *tdlh;
2169 struct thandle *handle;
2170 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2171 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2174 bool tobj_locked = 0;
2175 unsigned cl_flags = 0;
2180 mdd_tobj = md2mdd_obj(tobj);
2182 mdd_sobj = mdd_object_find(env, mdd, lf);
2184 handle = mdd_trans_create(env, mdd);
2186 GOTO(out_pending, rc = PTR_ERR(handle));
2188 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2189 mdd_tobj, lsname, ltname, ma, handle);
2193 rc = mdd_trans_start(env, mdd, handle);
2197 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2198 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2200 GOTO(cleanup_unlocked, rc);
2202 /* Get locks in determined order */
2203 if (rc == MDD_RN_SAME) {
2204 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2205 sname, MOR_SRC_PARENT);
2206 /* check hashes to determine do we need one lock or two */
2207 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2208 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2212 } else if (rc == MDD_RN_SRCTGT) {
2213 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2214 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2216 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2217 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2219 if (sdlh == NULL || tdlh == NULL)
2220 GOTO(cleanup, rc = -ENOMEM);
2222 rc = mdd_la_get(env, mdd_sobj, so_attr,
2223 mdd_object_capa(env, mdd_sobj));
2228 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2229 mdd_object_capa(env, mdd_tobj));
2234 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2235 mdd_tobj, so_attr, tg_attr);
2239 is_dir = S_ISDIR(so_attr->la_mode);
2241 /* Remove source name from source directory */
2242 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2243 mdd_object_capa(env, mdd_spobj));
2247 /* "mv dir1 dir2" needs "dir1/.." link update */
2248 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2249 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2250 mdd_object_capa(env, mdd_sobj));
2252 GOTO(fixup_spobj2, rc);
2254 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2255 handle, mdd_object_capa(env, mdd_sobj));
2257 GOTO(fixup_spobj, rc);
2260 /* Remove target name from target directory
2261 * Here tobj can be remote one, so we do index_delete unconditionally
2262 * and -ENOENT is allowed.
2264 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2265 mdd_object_capa(env, mdd_tpobj));
2268 /* tname might been renamed to something else */
2269 GOTO(fixup_spobj, rc);
2272 GOTO(fixup_spobj, rc);
2275 /* Insert new fid with target name into target dir */
2276 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2277 mdd_object_capa(env, mdd_tpobj));
2279 GOTO(fixup_tpobj, rc);
2281 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2282 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2284 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2286 la->la_valid = LA_CTIME;
2287 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2289 GOTO(fixup_tpobj, rc);
2292 /* Remove old target object
2293 * For tobj is remote case cmm layer has processed
2294 * and set tobj to NULL then. So when tobj is NOT NULL,
2295 * it must be local one.
2297 if (tobj && mdd_object_exists(mdd_tobj)) {
2298 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2300 if (mdd_is_dead_obj(mdd_tobj)) {
2301 /* shld not be dead, something is wrong */
2302 CERROR("tobj is dead, something is wrong\n");
2306 mdo_ref_del(env, mdd_tobj, handle);
2308 /* Remove dot reference. */
2309 if (S_ISDIR(tg_attr->la_mode))
2310 mdo_ref_del(env, mdd_tobj, handle);
2313 /* fetch updated nlink */
2314 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2315 mdd_object_capa(env, mdd_tobj));
2317 CERROR("%s: Failed to get nlink for tobj "
2319 mdd2obd_dev(mdd)->obd_name,
2320 PFID(tpobj_fid), rc);
2321 GOTO(fixup_tpobj, rc);
2324 la->la_valid = LA_CTIME;
2325 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2327 CERROR("%s: Failed to set ctime for tobj "
2329 mdd2obd_dev(mdd)->obd_name,
2330 PFID(tpobj_fid), rc);
2331 GOTO(fixup_tpobj, rc);
2334 /* XXX: this transfer to ma will be removed with LOD/OSP */
2335 ma->ma_attr = *tg_attr;
2336 ma->ma_valid |= MA_INODE;
2337 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2339 CERROR("%s: Failed to unlink tobj "
2341 mdd2obd_dev(mdd)->obd_name,
2342 PFID(tpobj_fid), rc);
2343 GOTO(fixup_tpobj, rc);
2346 /* fetch updated nlink */
2347 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2348 mdd_object_capa(env, mdd_tobj));
2350 CERROR("%s: Failed to get nlink for tobj "
2352 mdd2obd_dev(mdd)->obd_name,
2353 PFID(tpobj_fid), rc);
2354 GOTO(fixup_tpobj, rc);
2356 /* XXX: this transfer to ma will be removed with LOD/OSP */
2357 ma->ma_attr = *tg_attr;
2358 ma->ma_valid |= MA_INODE;
2360 if (so_attr->la_nlink == 0)
2361 cl_flags |= CLF_RENAME_LAST;
2364 la->la_valid = LA_CTIME | LA_MTIME;
2365 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2367 GOTO(fixup_tpobj, rc);
2369 if (mdd_spobj != mdd_tpobj) {
2370 la->la_valid = LA_CTIME | LA_MTIME;
2371 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2375 if (rc == 0 && mdd_sobj) {
2376 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2377 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2378 mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
2380 /* Old files might not have EA entry */
2381 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2383 mdd_write_unlock(env, mdd_sobj);
2384 /* We don't fail the transaction if the link ea can't be
2385 updated -- fid2path will use alternate lookup method. */
2393 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2396 CWARN("tp obj fix error %d\n",rc2);
2398 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2399 !mdd_is_dead_obj(mdd_tobj)) {
2401 mdo_ref_add(env, mdd_tobj, handle);
2403 mdo_ref_add(env, mdd_tobj, handle);
2406 rc2 = __mdd_index_insert(env, mdd_tpobj,
2407 mdo2fid(mdd_tobj), tname,
2412 CWARN("tp obj fix error %d\n",rc2);
2417 if (rc && is_dir && mdd_sobj) {
2418 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2422 CWARN("sp obj dotdot delete error %d\n",rc2);
2425 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2426 dotdot, handle, BYPASS_CAPA);
2428 CWARN("sp obj dotdot insert error %d\n",rc2);
2433 rc2 = __mdd_index_insert(env, mdd_spobj,
2434 lf, sname, is_dir, handle, BYPASS_CAPA);
2436 CWARN("sp obj fix error %d\n",rc2);
2440 mdd_write_unlock(env, mdd_tobj);
2441 if (likely(tdlh) && sdlh != tdlh)
2442 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2444 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2447 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2448 mdd_tobj, tpobj_fid, lf,
2449 spobj_fid, ltname, lsname,
2453 mdd_trans_stop(env, mdd, rc, handle);
2455 mdd_object_put(env, mdd_sobj);
2459 int mdd_links_new(const struct lu_env *env, struct mdd_link_data *ldata)
2461 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2462 if (ldata->ml_buf->lb_buf == NULL)
2464 ldata->ml_leh = ldata->ml_buf->lb_buf;
2465 ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
2466 ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
2467 ldata->ml_leh->leh_reccount = 0;
2471 /** Read the link EA into a temp buffer.
2472 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
2473 * A pointer to the buffer is stored in \a ldata::ml_buf.
2475 * \retval 0 or error
2477 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
2478 struct mdd_link_data *ldata)
2480 struct lustre_capa *capa;
2481 struct link_ea_header *leh;
2484 /* First try a small buf */
2485 LASSERT(env != NULL);
2486 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2487 if (ldata->ml_buf->lb_buf == NULL)
2490 if (!mdd_object_exists(mdd_obj))
2493 capa = mdd_object_capa(env, mdd_obj);
2494 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2495 XATTR_NAME_LINK, capa);
2496 if (rc == -ERANGE) {
2497 /* Buf was too small, figure out what we need. */
2498 mdd_buf_put(ldata->ml_buf);
2499 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2500 XATTR_NAME_LINK, capa);
2503 ldata->ml_buf = mdd_buf_alloc(env, rc);
2504 if (ldata->ml_buf->lb_buf == NULL)
2506 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2507 XATTR_NAME_LINK, capa);
2512 leh = ldata->ml_buf->lb_buf;
2513 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2514 leh->leh_magic = LINK_EA_MAGIC;
2515 leh->leh_reccount = __swab32(leh->leh_reccount);
2516 leh->leh_len = __swab64(leh->leh_len);
2517 /* entries are swabbed by mdd_lee_unpack */
2519 if (leh->leh_magic != LINK_EA_MAGIC)
2521 if (leh->leh_reccount == 0)
2524 ldata->ml_leh = leh;
2528 /** Read the link EA into a temp buffer.
2529 * Uses the name_buf since it is generally large.
2530 * \retval IS_ERR err
2531 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2533 struct lu_buf *mdd_links_get(const struct lu_env *env,
2534 struct mdd_object *mdd_obj)
2536 struct mdd_link_data ldata = { 0 };
2539 rc = mdd_links_read(env, mdd_obj, &ldata);
2540 return rc ? ERR_PTR(rc) : ldata.ml_buf;
2543 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
2544 struct mdd_link_data *ldata, struct thandle *handle)
2546 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
2547 ldata->ml_leh->leh_len);
2548 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
2549 mdd_object_capa(env, mdd_obj));
2552 /** Pack a link_ea_entry.
2553 * All elements are stored as chars to avoid alignment issues.
2554 * Numbers are always big-endian
2555 * \retval record length
2557 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2558 const struct lu_fid *pfid)
2560 struct lu_fid tmpfid;
2563 fid_cpu_to_be(&tmpfid, pfid);
2564 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_CRASH))
2566 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2567 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2568 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2570 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2571 lee->lee_reclen[1] = reclen & 0xff;
2575 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2576 struct lu_name *lname, struct lu_fid *pfid)
2578 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2579 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2580 fid_be_to_cpu(pfid, pfid);
2581 lname->ln_name = lee->lee_name;
2582 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2585 int mdd_declare_links_add(const struct lu_env *env,
2586 struct mdd_object *mdd_obj,
2587 struct thandle *handle)
2591 /* XXX: max size? */
2592 rc = mdo_declare_xattr_set(env, mdd_obj,
2593 mdd_buf_get_const(env, NULL, 4096),
2594 XATTR_NAME_LINK, 0, handle);
2599 /** Add a record to the end of link ea buf */
2600 int mdd_links_add_buf(const struct lu_env *env, struct mdd_link_data *ldata,
2601 const struct lu_name *lname, const struct lu_fid *pfid)
2603 LASSERT(ldata->ml_leh != NULL);
2605 if (lname == NULL || pfid == NULL)
2608 ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2609 if (ldata->ml_leh->leh_len + ldata->ml_reclen >
2610 ldata->ml_buf->lb_len) {
2611 if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
2612 ldata->ml_reclen) < 0)
2616 ldata->ml_leh = ldata->ml_buf->lb_buf;
2617 ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
2618 ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
2619 ldata->ml_leh->leh_len += ldata->ml_reclen;
2620 ldata->ml_leh->leh_reccount++;
2621 CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
2622 lname->ln_namelen, lname->ln_name);
2626 /** Del the current record from the link ea buf */
2627 void mdd_links_del_buf(const struct lu_env *env, struct mdd_link_data *ldata,
2628 const struct lu_name *lname)
2630 LASSERT(ldata->ml_leh != NULL);
2632 ldata->ml_leh->leh_reccount--;
2633 ldata->ml_leh->leh_len -= ldata->ml_reclen;
2634 memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
2635 (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
2636 (char *)ldata->ml_lee);
2637 CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
2638 lname->ln_namelen, lname->ln_name);
2643 * Check if such a link exists in linkEA.
2645 * \param mdd_obj object being handled
2646 * \param pfid parent fid the link to be found for
2647 * \param lname name in the parent's directory entry pointing to this object
2648 * \param ldata link data the search to be done on
2651 * \retval -ENOENT link does not exist
2652 * \retval -ve on error
2654 int mdd_links_find(const struct lu_env *env, struct mdd_object *mdd_obj,
2655 struct mdd_link_data *ldata, const struct lu_name *lname,
2656 const struct lu_fid *pfid)
2658 struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
2659 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2662 LASSERT(ldata->ml_leh != NULL);
2665 ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
2667 for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
2668 mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
2670 if (tmpname->ln_namelen == lname->ln_namelen &&
2671 lu_fid_eq(tmpfid, pfid) &&
2672 (strncmp(tmpname->ln_name, lname->ln_name,
2673 tmpname->ln_namelen) == 0))
2675 ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
2679 if (count == ldata->ml_leh->leh_reccount) {
2680 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2681 lname->ln_namelen, lname->ln_name);
2687 static int __mdd_links_add(const struct lu_env *env,
2688 struct mdd_object *mdd_obj,
2689 struct mdd_link_data *ldata,
2690 const struct lu_name *lname,
2691 const struct lu_fid *pfid,
2692 int first, int check)
2696 if (ldata->ml_leh == NULL) {
2697 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
2701 rc = mdd_links_new(env, ldata);
2708 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2709 if (rc && rc != -ENOENT)
2715 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
2716 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
2720 mdd_links_add_buf(env, ldata, lname, tfid);
2723 return mdd_links_add_buf(env, ldata, lname, pfid);
2726 static int __mdd_links_del(const struct lu_env *env,
2727 struct mdd_object *mdd_obj,
2728 struct mdd_link_data *ldata,
2729 const struct lu_name *lname,
2730 const struct lu_fid *pfid)
2734 if (ldata->ml_leh == NULL) {
2735 rc = mdd_links_read(env, mdd_obj, ldata);
2740 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2744 mdd_links_del_buf(env, ldata, lname);
2748 static int mdd_links_rename(const struct lu_env *env,
2749 struct mdd_object *mdd_obj,
2750 const struct lu_fid *oldpfid,
2751 const struct lu_name *oldlname,
2752 const struct lu_fid *newpfid,
2753 const struct lu_name *newlname,
2754 struct thandle *handle,
2755 int first, int check)
2757 struct mdd_link_data ldata = { 0 };
2763 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
2766 LASSERT(oldpfid != NULL || newpfid != NULL);
2768 if (mdd_obj->mod_flags & DEAD_OBJ)
2769 /* No more links, don't bother */
2772 if (oldpfid != NULL) {
2773 rc = __mdd_links_del(env, mdd_obj, &ldata,
2777 (rc != -ENODATA && rc != -ENOENT))
2779 /* No changes done. */
2786 /* If renaming, add the new record */
2787 if (newpfid != NULL) {
2788 /* even if the add fails, we still delete the out-of-date
2790 rc2 = __mdd_links_add(env, mdd_obj, &ldata,
2791 newlname, newpfid, first, check);
2799 rc = mdd_links_write(env, mdd_obj, &ldata, handle);
2806 if (rc == -EOVERFLOW || rc == - ENOENT)
2808 if (oldpfid == NULL)
2809 CDEBUG(error ? D_ERROR : D_OTHER,
2810 "link_ea add '%.*s' failed %d "DFID"\n",
2811 newlname->ln_namelen, newlname->ln_name,
2812 rc, PFID(mdd_object_fid(mdd_obj)));
2813 else if (newpfid == NULL)
2814 CDEBUG(error ? D_ERROR : D_OTHER,
2815 "link_ea del '%.*s' failed %d "DFID"\n",
2816 oldlname->ln_namelen, oldlname->ln_name,
2817 rc, PFID(mdd_object_fid(mdd_obj)));
2819 CDEBUG(error ? D_ERROR : D_OTHER,
2820 "link_ea rename '%.*s'->'%.*s' failed %d "
2822 oldlname->ln_namelen, oldlname->ln_name,
2823 newlname->ln_namelen, newlname->ln_name,
2824 rc, PFID(mdd_object_fid(mdd_obj)));
2827 if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
2828 /* if we vmalloced a large buffer drop it */
2829 mdd_buf_put(ldata.ml_buf);
2834 static inline int mdd_links_add(const struct lu_env *env,
2835 struct mdd_object *mdd_obj,
2836 const struct lu_fid *pfid,
2837 const struct lu_name *lname,
2838 struct thandle *handle, int first)
2840 return mdd_links_rename(env, mdd_obj, NULL, NULL,
2841 pfid, lname, handle, first, 0);
2844 static inline int mdd_links_del(const struct lu_env *env,
2845 struct mdd_object *mdd_obj,
2846 const struct lu_fid *pfid,
2847 const struct lu_name *lname,
2848 struct thandle *handle)
2850 return mdd_links_rename(env, mdd_obj, pfid, lname,
2851 NULL, NULL, handle, 0, 0);
2854 const struct md_dir_operations mdd_dir_ops = {
2855 .mdo_is_subdir = mdd_is_subdir,
2856 .mdo_lookup = mdd_lookup,
2857 .mdo_create = mdd_create,
2858 .mdo_rename = mdd_rename,
2859 .mdo_link = mdd_link,
2860 .mdo_unlink = mdd_unlink,
2861 .mdo_create_data = mdd_create_data,