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 int mdd_declare_links_add(const struct lu_env *env,
64 struct mdd_object *mdd_obj,
65 struct thandle *handle);
66 static inline int mdd_links_add(const struct lu_env *env,
67 struct mdd_object *mdd_obj,
68 const struct lu_fid *pfid,
69 const struct lu_name *lname,
70 struct thandle *handle, int first);
71 static inline int mdd_links_del(const struct lu_env *env,
72 struct mdd_object *mdd_obj,
73 const struct lu_fid *pfid,
74 const struct lu_name *lname,
75 struct thandle *handle);
76 static int mdd_links_rename(const struct lu_env *env,
77 struct mdd_object *mdd_obj,
78 const struct lu_fid *oldpfid,
79 const struct lu_name *oldlname,
80 const struct lu_fid *newpfid,
81 const struct lu_name *newlname,
82 struct thandle *handle,
83 int first, int check);
86 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
87 const struct lu_name *lname, struct lu_fid* fid, int mask)
89 const char *name = lname->ln_name;
90 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
91 struct dynlock_handle *dlh;
94 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
95 if (unlikely(dlh == NULL))
97 rc = __mdd_lookup(env, pobj, lname, fid, mask);
98 mdd_pdo_read_unlock(env, mdd_obj, dlh);
103 int mdd_lookup(const struct lu_env *env,
104 struct md_object *pobj, const struct lu_name *lname,
105 struct lu_fid* fid, struct md_op_spec *spec)
109 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
113 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
116 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
120 * For root fid use special function, which does not compare version component
121 * of fid. Version component is different for root fids on all MDTs.
123 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
125 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
126 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
130 * return 1: if lf is the fid of the ancestor of p1;
133 * return -EREMOTE: if remote object is found, in this
134 * case fid of remote object is saved to @pf;
136 * otherwise: values < 0, errors.
138 static int mdd_is_parent(const struct lu_env *env,
139 struct mdd_device *mdd,
140 struct mdd_object *p1,
141 const struct lu_fid *lf,
144 struct mdd_object *parent = NULL;
149 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
150 pfid = &mdd_env_info(env)->mti_fid;
152 /* Check for root first. */
153 if (mdd_is_root(mdd, mdo2fid(p1)))
157 /* this is done recursively, bypass capa for each obj */
158 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
159 rc = mdd_parent_fid(env, p1, pfid);
162 if (mdd_is_root(mdd, pfid))
164 if (lu_fid_eq(pfid, lf))
167 mdd_object_put(env, parent);
169 parent = mdd_object_find(env, mdd, pfid);
170 if (IS_ERR(parent)) {
171 GOTO(out, rc = PTR_ERR(parent));
172 } else if (mdd_object_remote(parent)) {
173 /*FIXME: Because of the restriction of rename in Phase I.
174 * If the parent is remote, we just assumed lf is not the
175 * parent of P1 for now */
182 if (parent && !IS_ERR(parent))
183 mdd_object_put(env, parent);
188 * No permission check is needed.
190 * returns 1: if fid is ancestor of @mo;
191 * returns 0: if fid is not a ancestor of @mo;
193 * returns EREMOTE if remote object is found, fid of remote object is saved to
196 * returns < 0: if error
198 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
199 const struct lu_fid *fid, struct lu_fid *sfid)
201 struct mdd_device *mdd = mdo2mdd(mo);
205 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
208 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
212 } else if (rc == 1) {
213 /* found @fid is parent */
221 * Check that @dir contains no entries except (possibly) dot and dotdot.
226 * -ENOTDIR not a directory object
227 * -ENOTEMPTY not empty
231 static int mdd_dir_is_empty(const struct lu_env *env,
232 struct mdd_object *dir)
235 struct dt_object *obj;
236 const struct dt_it_ops *iops;
240 obj = mdd_object_child(dir);
241 if (!dt_try_as_dir(env, obj))
244 iops = &obj->do_index_ops->dio_it;
245 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
247 result = iops->get(env, it, (const void *)"");
250 for (result = 0, i = 0; result == 0 && i < 3; ++i)
251 result = iops->next(env, it);
254 else if (result == +1)
256 } else if (result == 0)
258 * Huh? Index contains no zero key?
265 result = PTR_ERR(it);
269 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
271 struct mdd_device *m = mdd_obj2mdd_dev(obj);
272 struct lu_attr *la = &mdd_env_info(env)->mti_la;
276 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
281 * Subdir count limitation can be broken through.
283 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
284 !S_ISDIR(la->la_mode))
291 * Check whether it may create the cobj under the pobj.
294 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
295 struct mdd_object *cobj, int check_perm, int check_nlink)
300 if (cobj && mdd_object_exists(cobj))
303 if (mdd_is_dead_obj(pobj))
307 rc = mdd_permission_internal_locked(env, pobj, NULL,
308 MAY_WRITE | MAY_EXEC,
310 if (!rc && check_nlink)
311 rc = __mdd_may_link(env, pobj);
317 * Check whether can unlink from the pobj in the case of "cobj == NULL".
319 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
320 const struct lu_attr *attr)
325 if (mdd_is_dead_obj(pobj))
328 if ((attr->la_valid & LA_FLAGS) &&
329 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
332 rc = mdd_permission_internal_locked(env, pobj, NULL,
333 MAY_WRITE | MAY_EXEC,
338 if (mdd_is_append(pobj))
345 * pobj == NULL is remote ops case, under such case, pobj's
346 * VTX feature has been checked already, no need check again.
348 static inline int mdd_is_sticky(const struct lu_env *env,
349 struct mdd_object *pobj,
350 struct mdd_object *cobj)
352 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
353 struct lu_ucred *uc = lu_ucred_assert(env);
357 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
361 if (!(tmp_la->la_mode & S_ISVTX) ||
362 (tmp_la->la_uid == uc->uc_fsuid))
366 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
370 if (tmp_la->la_uid == uc->uc_fsuid)
373 return !md_capable(uc, CFS_CAP_FOWNER);
376 static int mdd_may_delete_entry(const struct lu_env *env,
377 struct mdd_object *pobj, int check_perm)
381 LASSERT(pobj != NULL);
382 if (!mdd_object_exists(pobj))
385 if (mdd_is_dead_obj(pobj))
390 rc = mdd_permission_internal_locked(env, pobj, NULL,
391 MAY_WRITE | MAY_EXEC,
397 if (mdd_is_append(pobj))
404 * Check whether it may delete the cobj from the pobj.
407 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
408 struct mdd_object *cobj, struct lu_attr *cattr,
409 struct lu_attr *src_attr, int check_perm, int check_empty)
415 rc = mdd_may_delete_entry(env, pobj, check_perm);
423 if (!mdd_object_exists(cobj))
426 if (mdd_is_dead_obj(cobj))
430 if (mdd_is_sticky(env, pobj, cobj))
433 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
436 if ((cattr->la_valid & LA_FLAGS) &&
437 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
440 /* additional check the rename case */
442 if (S_ISDIR(src_attr->la_mode)) {
443 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
445 if (!S_ISDIR(cattr->la_mode))
448 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
450 } else if (S_ISDIR(cattr->la_mode))
454 if (S_ISDIR(cattr->la_mode) && check_empty)
455 rc = mdd_dir_is_empty(env, cobj);
462 * has mdd_write_lock on src already, but not on tgt yet
464 int mdd_link_sanity_check(const struct lu_env *env,
465 struct mdd_object *tgt_obj,
466 const struct lu_name *lname,
467 struct mdd_object *src_obj)
469 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
473 if (!mdd_object_exists(src_obj))
476 if (mdd_is_dead_obj(src_obj))
479 /* Local ops, no lookup before link, check filename length here. */
480 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
481 RETURN(-ENAMETOOLONG);
483 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
486 if (S_ISDIR(mdd_object_type(src_obj)))
489 LASSERT(src_obj != tgt_obj);
491 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
496 rc = __mdd_may_link(env, src_obj);
501 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
502 const char *name, struct thandle *handle,
503 struct lustre_capa *capa)
505 struct dt_object *next = mdd_object_child(pobj);
509 if (dt_try_as_dir(env, next)) {
510 rc = next->do_index_ops->dio_delete(env, next,
511 (struct dt_key *)name,
519 static int __mdd_index_insert_only(const struct lu_env *env,
520 struct mdd_object *pobj,
521 const struct lu_fid *lf, const char *name,
522 struct thandle *handle,
523 struct lustre_capa *capa)
525 struct dt_object *next = mdd_object_child(pobj);
529 if (dt_try_as_dir(env, next)) {
530 struct lu_ucred *uc = lu_ucred_check(env);
533 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
534 rc = next->do_index_ops->dio_insert(env, next,
536 (const struct dt_key *)name,
537 handle, capa, ignore_quota);
544 /* insert named index, add reference if isdir */
545 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
546 const struct lu_fid *lf, const char *name, int is_dir,
547 struct thandle *handle, struct lustre_capa *capa)
552 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
553 if (rc == 0 && is_dir) {
554 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
555 mdo_ref_add(env, pobj, handle);
556 mdd_write_unlock(env, pobj);
561 /* delete named index, drop reference if isdir */
562 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
563 const char *name, int is_dir, struct thandle *handle,
564 struct lustre_capa *capa)
569 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
570 if (rc == 0 && is_dir) {
571 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
572 mdo_ref_del(env, pobj, handle);
573 mdd_write_unlock(env, pobj);
579 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
580 int reclen, struct thandle *handle)
584 /* XXX: this is a temporary solution to declare llog changes
585 * will be fixed in 2.3 with new llog implementation */
587 LASSERT(mdd->mdd_capa);
589 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
590 * have to set the parameter 'size' as INT_MAX or 0 to inform
591 * OSD that this record write is for a llog write or catalog
592 * header update, and osd declare function will reserve less
593 * credits for optimization purpose.
595 * Reserve 6 blocks for a llog write, since the llog file is
596 * usually small, reserve 2 blocks for catalog header update,
597 * because we know for sure that catalog header is already
600 * This hack should be removed in 2.3.
604 rc = dt_declare_record_write(env, mdd->mdd_capa,
605 DECLARE_LLOG_WRITE, 0, handle);
609 /* header will be updated as well */
610 rc = dt_declare_record_write(env, mdd->mdd_capa,
611 DECLARE_LLOG_WRITE, 0, handle);
615 /* also we should be able to create new plain log */
616 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
620 /* new record referencing new plain llog */
621 rc = dt_declare_record_write(env, mdd->mdd_capa,
622 DECLARE_LLOG_WRITE, 0, handle);
626 /* catalog's header will be updated as well */
627 rc = dt_declare_record_write(env, mdd->mdd_capa,
628 DECLARE_LLOG_REWRITE, 0, handle);
633 int mdd_declare_changelog_store(const struct lu_env *env,
634 struct mdd_device *mdd,
635 const struct lu_name *fname,
636 struct thandle *handle)
638 struct obd_device *obd = mdd2obd_dev(mdd);
639 struct llog_ctxt *ctxt;
640 struct llog_changelog_rec *rec;
646 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
649 reclen = llog_data_len(sizeof(*rec) +
650 (fname != NULL ? fname->ln_namelen : 0));
651 buf = mdd_buf_alloc(env, reclen);
652 if (buf->lb_buf == NULL)
656 rec->cr_hdr.lrh_len = reclen;
657 rec->cr_hdr.lrh_type = CHANGELOG_REC;
659 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
663 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
669 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
670 struct mdd_device *mdd,
671 const struct lu_name *tname,
672 const struct lu_name *sname,
673 struct thandle *handle)
675 struct obd_device *obd = mdd2obd_dev(mdd);
676 struct llog_ctxt *ctxt;
677 struct llog_changelog_ext_rec *rec;
683 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
686 reclen = llog_data_len(sizeof(*rec) +
687 (tname != NULL ? tname->ln_namelen : 0) +
688 (sname != NULL ? 1 + sname->ln_namelen : 0));
689 buf = mdd_buf_alloc(env, reclen);
690 if (buf->lb_buf == NULL)
694 rec->cr_hdr.lrh_len = reclen;
695 rec->cr_hdr.lrh_type = CHANGELOG_REC;
697 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
701 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
707 /** Add a changelog entry \a rec to the changelog llog
710 * \param handle - currently ignored since llogs start their own transaction;
711 * this will hopefully be fixed in llog rewrite
714 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
715 struct llog_changelog_rec *rec, struct thandle *th)
717 struct obd_device *obd = mdd2obd_dev(mdd);
718 struct llog_ctxt *ctxt;
721 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
722 rec->cr_hdr.lrh_type = CHANGELOG_REC;
723 rec->cr.cr_time = cl_time();
725 spin_lock(&mdd->mdd_cl.mc_lock);
726 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
727 * but as long as the MDD transactions are ordered correctly for e.g.
728 * rename conflicts, I don't think this should matter. */
729 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
730 spin_unlock(&mdd->mdd_cl.mc_lock);
732 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
736 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
743 /** Add a changelog_ext entry \a rec to the changelog llog
746 * \param handle - currently ignored since llogs start their own transaction;
747 * this will hopefully be fixed in llog rewrite
750 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
751 struct llog_changelog_ext_rec *rec,
754 struct obd_device *obd = mdd2obd_dev(mdd);
755 struct llog_ctxt *ctxt;
758 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
759 /* llog_lvfs_write_rec sets the llog tail len */
760 rec->cr_hdr.lrh_type = CHANGELOG_REC;
761 rec->cr.cr_time = cl_time();
763 spin_lock(&mdd->mdd_cl.mc_lock);
764 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
765 * but as long as the MDD transactions are ordered correctly for e.g.
766 * rename conflicts, I don't think this should matter. */
767 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
768 spin_unlock(&mdd->mdd_cl.mc_lock);
770 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
774 /* nested journal transaction */
775 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
783 /** Store a namespace change changelog record
784 * If this fails, we must fail the whole transaction; we don't
785 * want the change to commit without the log entry.
786 * \param target - mdd_object of change
787 * \param parent - parent dir/object
788 * \param tname - target name string
789 * \param handle - transacion handle
791 static int mdd_changelog_ns_store(const struct lu_env *env,
792 struct mdd_device *mdd,
793 enum changelog_rec_type type,
795 struct mdd_object *target,
796 struct mdd_object *parent,
797 const struct lu_name *tname,
798 struct thandle *handle)
800 struct llog_changelog_rec *rec;
807 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
809 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
812 LASSERT(target != NULL);
813 LASSERT(parent != NULL);
814 LASSERT(tname != NULL);
815 LASSERT(handle != NULL);
817 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
818 buf = mdd_buf_alloc(env, reclen);
819 if (buf->lb_buf == NULL)
823 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
824 rec->cr.cr_type = (__u32)type;
825 rec->cr.cr_tfid = *mdo2fid(target);
826 rec->cr.cr_pfid = *mdo2fid(parent);
827 rec->cr.cr_namelen = tname->ln_namelen;
828 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
830 target->mod_cltime = cfs_time_current_64();
832 rc = mdd_changelog_store(env, mdd, rec, handle);
834 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
835 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
836 PFID(&rec->cr.cr_pfid));
844 /** Store a namespace change changelog record
845 * If this fails, we must fail the whole transaction; we don't
846 * want the change to commit without the log entry.
847 * \param target - mdd_object of change
848 * \param tpfid - target parent dir/object fid
849 * \param sfid - source object fid
850 * \param spfid - source parent fid
851 * \param tname - target name string
852 * \param sname - source name string
853 * \param handle - transacion handle
855 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
856 struct mdd_device *mdd,
857 enum changelog_rec_type type,
859 struct mdd_object *target,
860 const struct lu_fid *tpfid,
861 const struct lu_fid *sfid,
862 const struct lu_fid *spfid,
863 const struct lu_name *tname,
864 const struct lu_name *sname,
865 struct thandle *handle)
867 struct llog_changelog_ext_rec *rec;
874 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
876 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
879 LASSERT(sfid != NULL);
880 LASSERT(tpfid != NULL);
881 LASSERT(tname != NULL);
882 LASSERT(handle != NULL);
884 reclen = llog_data_len(sizeof(*rec) +
885 sname != NULL ? 1 + sname->ln_namelen : 0);
886 buf = mdd_buf_alloc(env, reclen);
887 if (buf->lb_buf == NULL)
891 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
892 rec->cr.cr_type = (__u32)type;
893 rec->cr.cr_pfid = *tpfid;
894 rec->cr.cr_sfid = *sfid;
895 rec->cr.cr_spfid = *spfid;
896 rec->cr.cr_namelen = tname->ln_namelen;
897 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
899 rec->cr.cr_name[tname->ln_namelen] = '\0';
900 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
902 rec->cr.cr_namelen += 1 + sname->ln_namelen;
905 if (likely(target != NULL)) {
906 rec->cr.cr_tfid = *mdo2fid(target);
907 target->mod_cltime = cfs_time_current_64();
909 fid_zero(&rec->cr.cr_tfid);
912 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
914 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
915 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
922 static int mdd_declare_link(const struct lu_env *env,
923 struct mdd_device *mdd,
924 struct mdd_object *p,
925 struct mdd_object *c,
926 const struct lu_name *name,
927 struct thandle *handle)
931 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
935 rc = mdo_declare_ref_add(env, c, handle);
939 rc = mdo_declare_attr_set(env, p, NULL, handle);
943 rc = mdo_declare_attr_set(env, c, NULL, handle);
947 rc = mdd_declare_links_add(env, c, handle);
951 rc = mdd_declare_changelog_store(env, mdd, name, handle);
956 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
957 struct md_object *src_obj, const struct lu_name *lname,
960 const char *name = lname->ln_name;
961 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
962 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
963 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
964 struct mdd_device *mdd = mdo2mdd(src_obj);
965 struct dynlock_handle *dlh;
966 struct thandle *handle;
970 handle = mdd_trans_create(env, mdd);
972 GOTO(out_pending, rc = PTR_ERR(handle));
974 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
978 rc = mdd_trans_start(env, mdd, handle);
982 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
984 GOTO(out_trans, rc = -ENOMEM);
985 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
987 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
989 GOTO(out_unlock, rc);
991 rc = mdo_ref_add(env, mdd_sobj, handle);
993 GOTO(out_unlock, rc);
996 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
998 mdd_object_capa(env, mdd_tobj));
1000 mdo_ref_del(env, mdd_sobj, handle);
1001 GOTO(out_unlock, rc);
1004 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1005 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1007 la->la_valid = LA_CTIME | LA_MTIME;
1008 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1010 GOTO(out_unlock, rc);
1012 la->la_valid = LA_CTIME;
1013 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1015 mdd_links_add(env, mdd_sobj,
1016 mdo2fid(mdd_tobj), lname, handle, 0);
1021 mdd_write_unlock(env, mdd_sobj);
1022 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1025 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1026 mdd_tobj, lname, handle);
1028 mdd_trans_stop(env, mdd, rc, handle);
1033 int mdd_declare_finish_unlink(const struct lu_env *env,
1034 struct mdd_object *obj,
1036 struct thandle *handle)
1040 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1044 return mdo_declare_destroy(env, obj, handle);
1047 /* caller should take a lock before calling */
1048 int mdd_finish_unlink(const struct lu_env *env,
1049 struct mdd_object *obj, struct md_attr *ma,
1053 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1056 LASSERT(mdd_write_locked(env, obj) != 0);
1058 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
1059 obj->mod_flags |= DEAD_OBJ;
1060 /* add new orphan and the object
1061 * will be deleted during mdd_close() */
1062 if (obj->mod_count) {
1063 rc = __mdd_orphan_add(env, obj, th);
1065 CDEBUG(D_HA, "Object "DFID" is inserted into "
1066 "orphan list, open count = %d\n",
1067 PFID(mdd_object_fid(obj)),
1070 CERROR("Object "DFID" fail to be an orphan, "
1071 "open count = %d, maybe cause failed "
1073 PFID(mdd_object_fid(obj)),
1076 rc = mdo_destroy(env, obj, th);
1085 * has mdd_write_lock on cobj already, but not on pobj yet
1087 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1088 struct mdd_object *cobj, struct lu_attr *cattr)
1093 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1098 static inline int mdd_declare_links_del(const struct lu_env *env,
1099 struct mdd_object *c,
1100 struct thandle *handle)
1104 /* For directory, the linkEA will be removed together with the object. */
1105 if (!S_ISDIR(mdd_object_type(c)))
1106 rc = mdd_declare_links_add(env, c, handle);
1111 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1112 struct mdd_object *p, struct mdd_object *c,
1113 const struct lu_name *name, struct md_attr *ma,
1114 struct thandle *handle)
1116 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1119 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1123 rc = mdo_declare_ref_del(env, p, handle);
1127 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1128 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1129 la->la_valid = LA_CTIME | LA_MTIME;
1130 rc = mdo_declare_attr_set(env, p, la, handle);
1135 rc = mdo_declare_ref_del(env, c, handle);
1139 rc = mdo_declare_ref_del(env, c, handle);
1143 rc = mdo_declare_attr_set(env, c, NULL, handle);
1147 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1151 rc = mdd_declare_links_del(env, c, handle);
1155 /* FIXME: need changelog for remove entry */
1156 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1162 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1163 struct md_object *cobj, const struct lu_name *lname,
1166 const char *name = lname->ln_name;
1167 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1168 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1169 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1170 struct mdd_object *mdd_cobj = NULL;
1171 struct mdd_device *mdd = mdo2mdd(pobj);
1172 struct dynlock_handle *dlh;
1173 struct thandle *handle;
1177 /* cobj == NULL means only delete name entry */
1178 if (likely(cobj != NULL)) {
1179 mdd_cobj = md2mdd_obj(cobj);
1180 if (mdd_object_exists(mdd_cobj) == 0)
1182 /* currently it is assume, it could only delete
1183 * name entry of remote directory */
1187 handle = mdd_trans_create(env, mdd);
1189 RETURN(PTR_ERR(handle));
1191 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1196 rc = mdd_trans_start(env, mdd, handle);
1200 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1202 GOTO(stop, rc = -ENOMEM);
1204 if (likely(mdd_cobj != NULL)) {
1205 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1208 rc = mdd_la_get(env, mdd_cobj, cattr,
1209 mdd_object_capa(env, mdd_cobj));
1213 is_dir = S_ISDIR(cattr->la_mode);
1217 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1221 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1222 mdd_object_capa(env, mdd_pobj));
1226 if (likely(mdd_cobj != NULL)) {
1227 rc = mdo_ref_del(env, mdd_cobj, handle);
1229 __mdd_index_insert_only(env, mdd_pobj,
1232 mdd_object_capa(env, mdd_pobj));
1238 mdo_ref_del(env, mdd_cobj, handle);
1240 /* fetch updated nlink */
1241 rc = mdd_la_get(env, mdd_cobj, cattr,
1242 mdd_object_capa(env, mdd_cobj));
1247 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1248 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1250 la->la_valid = LA_CTIME | LA_MTIME;
1251 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1255 /* Enough for only unlink the entry */
1256 if (unlikely(mdd_cobj == NULL)) {
1257 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1261 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1262 /* update ctime of an unlinked file only if it is still
1263 * opened or a link still exists */
1264 la->la_valid = LA_CTIME;
1265 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1270 /* XXX: this transfer to ma will be removed with LOD/OSP */
1271 ma->ma_attr = *cattr;
1272 ma->ma_valid |= MA_INODE;
1273 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1275 /* fetch updated nlink */
1277 rc = mdd_la_get(env, mdd_cobj, cattr,
1278 mdd_object_capa(env, mdd_cobj));
1281 /* old files may not have link ea; ignore errors */
1282 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1284 /* if object is removed then we can't get its attrs, use last get */
1285 if (cattr->la_nlink == 0) {
1286 ma->ma_attr = *cattr;
1287 ma->ma_valid |= MA_INODE;
1291 mdd_write_unlock(env, mdd_cobj);
1292 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1296 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1297 if ((ma->ma_valid & MA_HSM) &&
1298 (ma->ma_hsm.mh_flags & HS_EXISTS))
1299 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1301 rc = mdd_changelog_ns_store(env, mdd,
1302 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1303 mdd_cobj, mdd_pobj, lname, handle);
1307 mdd_trans_stop(env, mdd, rc, handle);
1313 * The permission has been checked when obj created, no need check again.
1315 static int mdd_cd_sanity_check(const struct lu_env *env,
1316 struct mdd_object *obj)
1321 if (!obj || mdd_is_dead_obj(obj))
1328 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1329 struct md_object *cobj, const struct md_op_spec *spec,
1332 struct mdd_device *mdd = mdo2mdd(cobj);
1333 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1334 struct mdd_object *son = md2mdd_obj(cobj);
1335 struct thandle *handle;
1336 const struct lu_buf *buf;
1337 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1341 /* do not let users to create stripes via .lustre/
1342 * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1343 if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1346 rc = mdd_cd_sanity_check(env, son);
1350 if (!md_should_create(spec->sp_cr_flags))
1354 * there are following use cases for this function:
1355 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1356 * striping can be specified or not
1359 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1363 /* calling ->ah_make_hint() is used to transfer information from parent */
1364 mdd_object_make_hint(env, mdd_pobj, son, attr);
1366 handle = mdd_trans_create(env, mdd);
1368 GOTO(out_free, rc = PTR_ERR(handle));
1371 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1372 * Should this be fixed?
1374 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1375 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1376 spec->sp_cr_flags, spec->no_create);
1378 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1379 /* replay case or lfs setstripe */
1380 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1381 spec->u.sp_ea.eadatalen);
1386 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1387 XATTR_NAME_LOV, 0, handle);
1391 rc = mdd_trans_start(env, mdd, handle);
1395 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1396 0, handle, mdd_object_capa(env, son));
1398 mdd_trans_stop(env, mdd, rc, handle);
1403 /* Get fid from name and parent */
1405 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1406 const struct lu_name *lname, struct lu_fid* fid, int mask)
1408 const char *name = lname->ln_name;
1409 const struct dt_key *key = (const struct dt_key *)name;
1410 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1411 struct mdd_device *m = mdo2mdd(pobj);
1412 struct dt_object *dir = mdd_object_child(mdd_obj);
1416 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1419 if (mdd_object_remote(mdd_obj)) {
1420 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1421 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1422 } else if (!mdd_object_exists(mdd_obj)) {
1426 /* The common filename length check. */
1427 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1428 RETURN(-ENAMETOOLONG);
1430 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1435 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1436 dt_try_as_dir(env, dir))) {
1438 rc = dir->do_index_ops->dio_lookup(env, dir,
1439 (struct dt_rec *)fid, key,
1440 mdd_object_capa(env, mdd_obj));
1451 static int mdd_declare_object_initialize(const struct lu_env *env,
1452 struct mdd_object *parent,
1453 struct mdd_object *child,
1454 struct lu_attr *attr,
1455 struct thandle *handle)
1461 * inode mode has been set in creation time, and it's based on umask,
1462 * la_mode and acl, don't set here again! (which will go wrong
1463 * because below function doesn't consider umask).
1464 * I'd suggest set all object attributes in creation time, see above.
1466 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1467 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1468 rc = mdo_declare_attr_set(env, child, attr, handle);
1469 attr->la_valid |= LA_MODE | LA_TYPE;
1470 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1471 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1474 rc = mdo_declare_ref_add(env, child, handle);
1476 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1481 mdd_declare_links_add(env, child, handle);
1486 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1487 const struct lu_name *lname, struct mdd_object *child,
1488 struct lu_attr *attr, struct thandle *handle,
1489 const struct md_op_spec *spec)
1495 * Update attributes for child.
1498 * (1) the valid bits should be converted between Lustre and Linux;
1499 * (2) maybe, the child attributes should be set in OSD when creation.
1503 * inode mode has been set in creation time, and it's based on umask,
1504 * la_mode and acl, don't set here again! (which will go wrong
1505 * because below function doesn't consider umask).
1506 * I'd suggest set all object attributes in creation time, see above.
1508 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1509 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1510 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1511 /* arguments are supposed to stay the same */
1512 attr->la_valid |= LA_MODE | LA_TYPE;
1516 if (S_ISDIR(attr->la_mode)) {
1517 /* Add "." and ".." for newly created dir */
1518 mdo_ref_add(env, child, handle);
1519 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1520 dot, handle, BYPASS_CAPA);
1522 rc = __mdd_index_insert_only(env, child, pfid,
1526 mdo_ref_del(env, child, handle);
1529 mdd_links_add(env, child, pfid, lname, handle, 1);
1534 /* has not lock on pobj yet */
1535 static int mdd_create_sanity_check(const struct lu_env *env,
1536 struct md_object *pobj,
1537 struct lu_attr *pattr,
1538 const struct lu_name *lname,
1539 struct lu_attr *cattr,
1540 struct md_op_spec *spec)
1542 struct mdd_thread_info *info = mdd_env_info(env);
1543 struct lu_fid *fid = &info->mti_fid;
1544 struct mdd_object *obj = md2mdd_obj(pobj);
1545 struct mdd_device *m = mdo2mdd(pobj);
1550 if (mdd_is_dead_obj(obj))
1554 * In some cases this lookup is not needed - we know before if name
1555 * exists or not because MDT performs lookup for it.
1556 * name length check is done in lookup.
1558 if (spec->sp_cr_lookup) {
1560 * Check if the name already exist, though it will be checked in
1561 * _index_insert also, for avoiding rolling back if exists
1564 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1565 MAY_WRITE | MAY_EXEC);
1567 RETURN(rc ? : -EEXIST);
1570 * Check WRITE permission for the parent.
1571 * EXEC permission have been checked
1572 * when lookup before create already.
1574 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1581 if (pattr->la_mode & S_ISGID) {
1582 cattr->la_gid = pattr->la_gid;
1583 if (S_ISDIR(cattr->la_mode)) {
1584 cattr->la_mode |= S_ISGID;
1585 cattr->la_valid |= LA_MODE;
1589 switch (cattr->la_mode & S_IFMT) {
1591 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1593 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1594 RETURN(-ENAMETOOLONG);
1613 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1614 struct mdd_object *p, struct mdd_object *c,
1615 const struct lu_name *name,
1616 struct lu_attr *attr,
1618 struct thandle *handle,
1619 const struct md_op_spec *spec)
1623 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1627 #ifdef CONFIG_FS_POSIX_ACL
1628 if (got_def_acl > 0) {
1629 struct lu_buf *acl_buf;
1631 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1632 /* if dir, then can inherit default ACl */
1633 if (S_ISDIR(attr->la_mode)) {
1634 rc = mdo_declare_xattr_set(env, c, acl_buf,
1635 XATTR_NAME_ACL_DEFAULT,
1641 rc = mdo_declare_attr_set(env, c, attr, handle);
1645 rc = mdo_declare_xattr_set(env, c, acl_buf,
1646 XATTR_NAME_ACL_ACCESS, 0, handle);
1652 if (S_ISDIR(attr->la_mode)) {
1653 rc = mdo_declare_ref_add(env, p, handle);
1658 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1662 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1663 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1665 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1666 name->ln_name, handle);
1670 /* replay case, create LOV EA from client data */
1671 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1672 const struct lu_buf *buf;
1674 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1675 spec->u.sp_ea.eadatalen);
1676 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1682 if (S_ISLNK(attr->la_mode)) {
1683 rc = dt_declare_record_write(env, mdd_object_child(c),
1684 strlen(spec->u.sp_symname), 0,
1690 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1691 rc = mdo_declare_attr_set(env, p, attr, handle);
1696 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1705 * Create object and insert it into namespace.
1707 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1708 const struct lu_name *lname, struct md_object *child,
1709 struct md_op_spec *spec, struct md_attr* ma)
1711 struct mdd_thread_info *info = mdd_env_info(env);
1712 struct lu_attr *la = &info->mti_la_for_fix;
1713 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1714 struct mdd_object *son = md2mdd_obj(child);
1715 struct mdd_device *mdd = mdo2mdd(pobj);
1716 struct lu_attr *attr = &ma->ma_attr;
1717 struct thandle *handle;
1718 struct lu_attr *pattr = &info->mti_pattr;
1719 struct dynlock_handle *dlh;
1720 const char *name = lname->ln_name;
1721 int rc, created = 0, initialized = 0, inserted = 0;
1722 int got_def_acl = 0;
1726 * Two operations have to be performed:
1728 * - an allocation of a new object (->do_create()), and
1730 * - an insertion into a parent index (->dio_insert()).
1732 * Due to locking, operation order is not important, when both are
1733 * successful, *but* error handling cases are quite different:
1735 * - if insertion is done first, and following object creation fails,
1736 * insertion has to be rolled back, but this operation might fail
1737 * also leaving us with dangling index entry.
1739 * - if creation is done first, is has to be undone if insertion
1740 * fails, leaving us with leaked space, which is neither good, nor
1743 * It seems that creation-first is simplest solution, but it is
1744 * sub-optimal in the frequent
1749 * case, because second mkdir is bound to create object, only to
1750 * destroy it immediately.
1752 * To avoid this follow local file systems that do double lookup:
1754 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1756 * 1. create (mdd_object_create_internal())
1758 * 2. insert (__mdd_index_insert(), lookup again)
1761 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1765 /* Sanity checks before big job. */
1766 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1770 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1771 GOTO(out_free, rc = -EINPROGRESS);
1773 if (!S_ISLNK(attr->la_mode)) {
1774 struct lu_buf *acl_buf;
1776 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1777 sizeof(info->mti_xattr_buf));
1778 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1779 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1780 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1781 mdd_read_unlock(env, mdd_pobj);
1784 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1788 mdd_object_make_hint(env, mdd_pobj, son, attr);
1790 handle = mdd_trans_create(env, mdd);
1792 GOTO(out_free, rc = PTR_ERR(handle));
1794 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1795 got_def_acl, handle, spec);
1799 rc = mdd_trans_start(env, mdd, handle);
1803 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1805 GOTO(out_trans, rc = -ENOMEM);
1807 mdd_write_lock(env, son, MOR_TGT_CHILD);
1808 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
1810 mdd_write_unlock(env, son);
1816 #ifdef CONFIG_FS_POSIX_ACL
1818 struct lu_buf *acl_buf;
1820 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1821 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1823 mdd_write_unlock(env, son);
1829 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1830 son, attr, handle, spec);
1833 * in case of replay we just set LOVEA provided by the client
1834 * XXX: I think it would be interesting to try "old" way where
1835 * MDT calls this xattr_set(LOV) in a different transaction.
1836 * probably this way we code can be made better.
1838 if (rc == 0 && (spec->no_create ||
1839 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1840 const struct lu_buf *buf;
1842 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1843 spec->u.sp_ea.eadatalen);
1844 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1848 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1849 rc = __mdd_orphan_add(env, son, handle);
1851 mdd_write_unlock(env, son);
1855 * Object has no links, so it will be destroyed when last
1856 * reference is released. (XXX not now.)
1862 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
1863 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1864 name, S_ISDIR(attr->la_mode), handle,
1865 mdd_object_capa(env, mdd_pobj));
1872 if (S_ISLNK(attr->la_mode)) {
1873 struct lu_ucred *uc = lu_ucred_assert(env);
1874 struct dt_object *dt = mdd_object_child(son);
1875 const char *target_name = spec->u.sp_symname;
1876 int sym_len = strlen(target_name);
1877 const struct lu_buf *buf;
1880 buf = mdd_buf_get_const(env, target_name, sym_len);
1881 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1882 mdd_object_capa(env, son),
1884 CFS_CAP_SYS_RESOURCE_MASK);
1889 GOTO(cleanup, rc = -EFAULT);
1892 /* volatile file creation does not update parent directory times */
1893 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1894 GOTO(cleanup, rc = 0);
1896 /* update parent directory mtime/ctime */
1898 la->la_valid = LA_CTIME | LA_MTIME;
1899 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1905 if (rc != 0 && created != 0) {
1908 if (inserted != 0) {
1909 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1910 rc2 = __mdd_orphan_del(env, son, handle);
1912 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1913 S_ISDIR(attr->la_mode),
1914 handle, BYPASS_CAPA);
1919 mdd_write_lock(env, son, MOR_TGT_CHILD);
1920 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1921 /* Drop the reference, no need to delete "."/"..",
1922 * because the object to be destroied directly. */
1923 rc2 = mdo_ref_del(env, son, handle);
1925 mdd_write_unlock(env, son);
1930 rc2 = mdo_ref_del(env, son, handle);
1932 mdd_write_unlock(env, son);
1936 mdo_destroy(env, son, handle);
1937 mdd_write_unlock(env, son);
1940 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1942 if (rc == 0 && fid_is_client_mdt_visible(mdo2fid(son)))
1943 rc = mdd_changelog_ns_store(env, mdd,
1944 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1945 S_ISREG(attr->la_mode) ? CL_CREATE :
1946 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1947 0, son, mdd_pobj, lname, handle);
1949 mdd_trans_stop(env, mdd, rc, handle);
1951 /* The child object shouldn't be cached anymore */
1953 set_bit(LU_OBJECT_HEARD_BANSHEE,
1954 &child->mo_lu.lo_header->loh_flags);
1959 * Get locks on parents in proper order
1960 * RETURN: < 0 - error, rename_order if successful
1968 static int mdd_rename_order(const struct lu_env *env,
1969 struct mdd_device *mdd,
1970 struct mdd_object *src_pobj,
1971 struct mdd_object *tgt_pobj)
1973 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1977 if (src_pobj == tgt_pobj)
1978 RETURN(MDD_RN_SAME);
1980 /* compared the parent child relationship of src_p&tgt_p */
1981 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1983 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1986 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1999 /* has not mdd_write{read}_lock on any obj yet. */
2000 static int mdd_rename_sanity_check(const struct lu_env *env,
2001 struct mdd_object *src_pobj,
2002 struct mdd_object *tgt_pobj,
2003 struct mdd_object *sobj,
2004 struct mdd_object *tobj,
2005 struct lu_attr *so_attr,
2006 struct lu_attr *tg_attr)
2011 /* XXX: when get here, sobj must NOT be NULL,
2012 * the other case has been processed in cml_rename
2013 * before mdd_rename and enable MDS_PERM_BYPASS. */
2016 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2020 /* XXX: when get here, "tobj == NULL" means tobj must
2021 * NOT exist (neither on remote MDS, such case has been
2022 * processed in cml_rename before mdd_rename and enable
2024 * So check may_create, but not check may_unlink. */
2026 rc = mdd_may_create(env, tgt_pobj, NULL,
2027 (src_pobj != tgt_pobj), 0);
2029 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2030 (src_pobj != tgt_pobj), 1);
2032 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2033 S_ISDIR(so_attr->la_mode))
2034 rc = __mdd_may_link(env, tgt_pobj);
2039 static int mdd_declare_rename(const struct lu_env *env,
2040 struct mdd_device *mdd,
2041 struct mdd_object *mdd_spobj,
2042 struct mdd_object *mdd_tpobj,
2043 struct mdd_object *mdd_sobj,
2044 struct mdd_object *mdd_tobj,
2045 const struct lu_name *tname,
2046 const struct lu_name *sname,
2048 struct thandle *handle)
2056 /* name from source dir */
2057 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2061 /* .. from source child */
2062 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2063 /* source child can be directory,
2064 * counted by source dir's nlink */
2065 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2069 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2073 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2078 /* new target child can be directory,
2079 * counted by target dir's nlink */
2080 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2086 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2090 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2093 mdd_declare_links_add(env, mdd_sobj, handle);
2097 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2102 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2103 tname->ln_name, handle);
2107 /* name from target dir (old name), we declare it unconditionally
2108 * as mdd_rename() calls delete unconditionally as well. so just
2109 * to balance declarations vs calls to change ... */
2110 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2114 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2115 /* delete target child in target parent directory */
2116 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2120 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2121 /* target child can be directory,
2122 * delete "." reference in target child directory */
2123 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2127 /* delete ".." reference in target parent directory */
2128 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2133 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2137 mdd_declare_links_add(env, mdd_tobj, handle);
2141 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2146 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2153 /* src object can be remote that is why we use only fid and type of object */
2154 static int mdd_rename(const struct lu_env *env,
2155 struct md_object *src_pobj, struct md_object *tgt_pobj,
2156 const struct lu_fid *lf, const struct lu_name *lsname,
2157 struct md_object *tobj, const struct lu_name *ltname,
2160 const char *sname = lsname->ln_name;
2161 const char *tname = ltname->ln_name;
2162 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2163 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2164 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2165 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2166 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2167 struct mdd_device *mdd = mdo2mdd(src_pobj);
2168 struct mdd_object *mdd_sobj = NULL; /* source object */
2169 struct mdd_object *mdd_tobj = NULL;
2170 struct dynlock_handle *sdlh, *tdlh;
2171 struct thandle *handle;
2172 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2173 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2176 bool tobj_locked = 0;
2177 unsigned cl_flags = 0;
2182 mdd_tobj = md2mdd_obj(tobj);
2184 mdd_sobj = mdd_object_find(env, mdd, lf);
2186 handle = mdd_trans_create(env, mdd);
2188 GOTO(out_pending, rc = PTR_ERR(handle));
2190 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2191 mdd_tobj, lsname, ltname, ma, handle);
2195 rc = mdd_trans_start(env, mdd, handle);
2199 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2200 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2202 GOTO(cleanup_unlocked, rc);
2204 /* Get locks in determined order */
2205 if (rc == MDD_RN_SAME) {
2206 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2207 sname, MOR_SRC_PARENT);
2208 /* check hashes to determine do we need one lock or two */
2209 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2210 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2214 } else if (rc == MDD_RN_SRCTGT) {
2215 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2216 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2218 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2219 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2221 if (sdlh == NULL || tdlh == NULL)
2222 GOTO(cleanup, rc = -ENOMEM);
2224 rc = mdd_la_get(env, mdd_sobj, so_attr,
2225 mdd_object_capa(env, mdd_sobj));
2230 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2231 mdd_object_capa(env, mdd_tobj));
2236 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2237 mdd_tobj, so_attr, tg_attr);
2241 is_dir = S_ISDIR(so_attr->la_mode);
2243 /* Remove source name from source directory */
2244 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2245 mdd_object_capa(env, mdd_spobj));
2249 /* "mv dir1 dir2" needs "dir1/.." link update */
2250 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2251 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2252 mdd_object_capa(env, mdd_sobj));
2254 GOTO(fixup_spobj2, rc);
2256 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2257 handle, mdd_object_capa(env, mdd_sobj));
2259 GOTO(fixup_spobj, rc);
2262 /* Remove target name from target directory
2263 * Here tobj can be remote one, so we do index_delete unconditionally
2264 * and -ENOENT is allowed.
2266 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2267 mdd_object_capa(env, mdd_tpobj));
2270 /* tname might been renamed to something else */
2271 GOTO(fixup_spobj, rc);
2274 GOTO(fixup_spobj, rc);
2277 /* Insert new fid with target name into target dir */
2278 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2279 mdd_object_capa(env, mdd_tpobj));
2281 GOTO(fixup_tpobj, rc);
2283 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2284 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2286 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2288 la->la_valid = LA_CTIME;
2289 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2291 GOTO(fixup_tpobj, rc);
2294 /* Remove old target object
2295 * For tobj is remote case cmm layer has processed
2296 * and set tobj to NULL then. So when tobj is NOT NULL,
2297 * it must be local one.
2299 if (tobj && mdd_object_exists(mdd_tobj)) {
2300 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2302 if (mdd_is_dead_obj(mdd_tobj)) {
2303 /* shld not be dead, something is wrong */
2304 CERROR("tobj is dead, something is wrong\n");
2308 mdo_ref_del(env, mdd_tobj, handle);
2310 /* Remove dot reference. */
2311 if (S_ISDIR(tg_attr->la_mode))
2312 mdo_ref_del(env, mdd_tobj, handle);
2315 /* fetch updated nlink */
2316 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2317 mdd_object_capa(env, mdd_tobj));
2319 CERROR("%s: Failed to get nlink for tobj "
2321 mdd2obd_dev(mdd)->obd_name,
2322 PFID(tpobj_fid), rc);
2323 GOTO(fixup_tpobj, rc);
2326 la->la_valid = LA_CTIME;
2327 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2329 CERROR("%s: Failed to set ctime for tobj "
2331 mdd2obd_dev(mdd)->obd_name,
2332 PFID(tpobj_fid), rc);
2333 GOTO(fixup_tpobj, rc);
2336 /* XXX: this transfer to ma will be removed with LOD/OSP */
2337 ma->ma_attr = *tg_attr;
2338 ma->ma_valid |= MA_INODE;
2339 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2341 CERROR("%s: Failed to unlink tobj "
2343 mdd2obd_dev(mdd)->obd_name,
2344 PFID(tpobj_fid), rc);
2345 GOTO(fixup_tpobj, rc);
2348 /* fetch updated nlink */
2349 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2350 mdd_object_capa(env, mdd_tobj));
2352 CERROR("%s: Failed to get nlink for tobj "
2354 mdd2obd_dev(mdd)->obd_name,
2355 PFID(tpobj_fid), rc);
2356 GOTO(fixup_tpobj, rc);
2358 /* XXX: this transfer to ma will be removed with LOD/OSP */
2359 ma->ma_attr = *tg_attr;
2360 ma->ma_valid |= MA_INODE;
2362 if (so_attr->la_nlink == 0)
2363 cl_flags |= CLF_RENAME_LAST;
2366 la->la_valid = LA_CTIME | LA_MTIME;
2367 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2369 GOTO(fixup_tpobj, rc);
2371 if (mdd_spobj != mdd_tpobj) {
2372 la->la_valid = LA_CTIME | LA_MTIME;
2373 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2377 if (rc == 0 && mdd_sobj) {
2378 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2379 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2380 mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
2382 /* Old files might not have EA entry */
2383 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2385 mdd_write_unlock(env, mdd_sobj);
2386 /* We don't fail the transaction if the link ea can't be
2387 updated -- fid2path will use alternate lookup method. */
2395 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2398 CWARN("tp obj fix error %d\n",rc2);
2400 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2401 !mdd_is_dead_obj(mdd_tobj)) {
2403 mdo_ref_add(env, mdd_tobj, handle);
2405 mdo_ref_add(env, mdd_tobj, handle);
2408 rc2 = __mdd_index_insert(env, mdd_tpobj,
2409 mdo2fid(mdd_tobj), tname,
2414 CWARN("tp obj fix error %d\n",rc2);
2419 if (rc && is_dir && mdd_sobj) {
2420 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2424 CWARN("sp obj dotdot delete error %d\n",rc2);
2427 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2428 dotdot, handle, BYPASS_CAPA);
2430 CWARN("sp obj dotdot insert error %d\n",rc2);
2435 rc2 = __mdd_index_insert(env, mdd_spobj,
2436 lf, sname, is_dir, handle, BYPASS_CAPA);
2438 CWARN("sp obj fix error %d\n",rc2);
2442 mdd_write_unlock(env, mdd_tobj);
2443 if (likely(tdlh) && sdlh != tdlh)
2444 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2446 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2449 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2450 mdd_tobj, tpobj_fid, lf,
2451 spobj_fid, ltname, lsname,
2455 mdd_trans_stop(env, mdd, rc, handle);
2457 mdd_object_put(env, mdd_sobj);
2463 * The data that link search is done on.
2465 struct mdd_link_data {
2467 * Buffer to keep link EA body.
2469 struct lu_buf *ml_buf;
2471 * The matched header, entry and its lenght in the EA
2473 struct link_ea_header *ml_leh;
2474 struct link_ea_entry *ml_lee;
2478 static int mdd_links_new(const struct lu_env *env,
2479 struct mdd_link_data *ldata)
2481 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2482 if (ldata->ml_buf->lb_buf == NULL)
2484 ldata->ml_leh = ldata->ml_buf->lb_buf;
2485 ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
2486 ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
2487 ldata->ml_leh->leh_reccount = 0;
2491 /** Read the link EA into a temp buffer.
2492 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
2493 * A pointer to the buffer is stored in \a ldata::ml_buf.
2495 * \retval 0 or error
2497 int mdd_links_read(const struct lu_env *env,
2498 struct mdd_object *mdd_obj,
2499 struct mdd_link_data *ldata)
2501 struct lustre_capa *capa;
2502 struct link_ea_header *leh;
2505 /* First try a small buf */
2506 LASSERT(env != NULL);
2507 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2508 if (ldata->ml_buf->lb_buf == NULL)
2511 if (!mdd_object_exists(mdd_obj))
2514 capa = mdd_object_capa(env, mdd_obj);
2515 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2516 XATTR_NAME_LINK, capa);
2517 if (rc == -ERANGE) {
2518 /* Buf was too small, figure out what we need. */
2519 mdd_buf_put(ldata->ml_buf);
2520 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2521 XATTR_NAME_LINK, capa);
2524 ldata->ml_buf = mdd_buf_alloc(env, rc);
2525 if (ldata->ml_buf->lb_buf == NULL)
2527 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2528 XATTR_NAME_LINK, capa);
2533 leh = ldata->ml_buf->lb_buf;
2534 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2535 leh->leh_magic = LINK_EA_MAGIC;
2536 leh->leh_reccount = __swab32(leh->leh_reccount);
2537 leh->leh_len = __swab64(leh->leh_len);
2538 /* entries are swabbed by mdd_lee_unpack */
2540 if (leh->leh_magic != LINK_EA_MAGIC)
2542 if (leh->leh_reccount == 0)
2545 ldata->ml_leh = leh;
2549 /** Read the link EA into a temp buffer.
2550 * Uses the name_buf since it is generally large.
2551 * \retval IS_ERR err
2552 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2554 struct lu_buf *mdd_links_get(const struct lu_env *env,
2555 struct mdd_object *mdd_obj)
2557 struct mdd_link_data ldata = { 0 };
2560 rc = mdd_links_read(env, mdd_obj, &ldata);
2561 return rc ? ERR_PTR(rc) : ldata.ml_buf;
2564 static int mdd_links_write(const struct lu_env *env,
2565 struct mdd_object *mdd_obj,
2566 struct mdd_link_data *ldata,
2567 struct thandle *handle)
2569 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
2570 ldata->ml_leh->leh_len);
2571 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
2572 mdd_object_capa(env, mdd_obj));
2575 /** Pack a link_ea_entry.
2576 * All elements are stored as chars to avoid alignment issues.
2577 * Numbers are always big-endian
2578 * \retval record length
2580 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2581 const struct lu_fid *pfid)
2583 struct lu_fid tmpfid;
2586 fid_cpu_to_be(&tmpfid, pfid);
2587 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2588 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2589 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2591 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2592 lee->lee_reclen[1] = reclen & 0xff;
2596 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2597 struct lu_name *lname, struct lu_fid *pfid)
2599 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2600 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2601 fid_be_to_cpu(pfid, pfid);
2602 lname->ln_name = lee->lee_name;
2603 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2606 static int mdd_declare_links_add(const struct lu_env *env,
2607 struct mdd_object *mdd_obj,
2608 struct thandle *handle)
2612 /* XXX: max size? */
2613 rc = mdo_declare_xattr_set(env, mdd_obj,
2614 mdd_buf_get_const(env, NULL, 4096),
2615 XATTR_NAME_LINK, 0, handle);
2620 /* For pathologic linkers, we don't want to spend lots of time scanning the
2621 * link ea. Limit ourseleves to something reasonable; links not in the EA
2622 * can be looked up via (slower) parent lookup.
2624 #define LINKEA_MAX_COUNT 128
2626 /** Add a record to the end of link ea buf */
2627 static int mdd_links_add_buf(const struct lu_env *env,
2628 struct mdd_link_data *ldata,
2629 const struct lu_name *lname,
2630 const struct lu_fid *pfid)
2632 LASSERT(ldata->ml_leh != NULL);
2634 if (lname == NULL || pfid == NULL)
2637 /* Make sure our buf is big enough for the new one */
2638 if (ldata->ml_leh->leh_reccount > LINKEA_MAX_COUNT)
2641 ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2642 if (ldata->ml_leh->leh_len + ldata->ml_reclen >
2643 ldata->ml_buf->lb_len) {
2644 if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
2645 ldata->ml_reclen) < 0)
2649 ldata->ml_leh = ldata->ml_buf->lb_buf;
2650 ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
2651 ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
2652 ldata->ml_leh->leh_len += ldata->ml_reclen;
2653 ldata->ml_leh->leh_reccount++;
2654 CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
2655 lname->ln_namelen, lname->ln_name);
2659 /** Del the current record from the link ea buf */
2660 static void mdd_links_del_buf(const struct lu_env *env,
2661 struct mdd_link_data *ldata,
2662 const struct lu_name *lname)
2664 LASSERT(ldata->ml_leh != NULL);
2666 ldata->ml_leh->leh_reccount--;
2667 ldata->ml_leh->leh_len -= ldata->ml_reclen;
2668 memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
2669 (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
2670 (char *)ldata->ml_lee);
2671 CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
2672 lname->ln_namelen, lname->ln_name);
2677 * Check if such a link exists in linkEA.
2679 * \param mdd_obj object being handled
2680 * \param pfid parent fid the link to be found for
2681 * \param lname name in the parent's directory entry pointing to this object
2682 * \param ldata link data the search to be done on
2685 * \retval -ENOENT link does not exist
2686 * \retval -ve on error
2688 static int mdd_links_find(const struct lu_env *env,
2689 struct mdd_object *mdd_obj,
2690 struct mdd_link_data *ldata,
2691 const struct lu_name *lname,
2692 const struct lu_fid *pfid)
2694 struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
2695 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2698 LASSERT(ldata->ml_leh != NULL);
2701 ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
2703 for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
2704 mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
2706 if (tmpname->ln_namelen == lname->ln_namelen &&
2707 lu_fid_eq(tmpfid, pfid) &&
2708 (strncmp(tmpname->ln_name, lname->ln_name,
2709 tmpname->ln_namelen) == 0))
2711 ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
2715 if (count == ldata->ml_leh->leh_reccount) {
2716 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2717 lname->ln_namelen, lname->ln_name);
2723 static int __mdd_links_add(const struct lu_env *env,
2724 struct mdd_object *mdd_obj,
2725 struct mdd_link_data *ldata,
2726 const struct lu_name *lname,
2727 const struct lu_fid *pfid,
2728 int first, int check)
2732 if (ldata->ml_leh == NULL) {
2733 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
2737 rc = mdd_links_new(env, ldata);
2744 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2745 if (rc && rc != -ENOENT)
2751 return mdd_links_add_buf(env, ldata, lname, pfid);
2754 static int __mdd_links_del(const struct lu_env *env,
2755 struct mdd_object *mdd_obj,
2756 struct mdd_link_data *ldata,
2757 const struct lu_name *lname,
2758 const struct lu_fid *pfid)
2762 if (ldata->ml_leh == NULL) {
2763 rc = mdd_links_read(env, mdd_obj, ldata);
2768 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2772 mdd_links_del_buf(env, ldata, lname);
2776 static int mdd_links_rename(const struct lu_env *env,
2777 struct mdd_object *mdd_obj,
2778 const struct lu_fid *oldpfid,
2779 const struct lu_name *oldlname,
2780 const struct lu_fid *newpfid,
2781 const struct lu_name *newlname,
2782 struct thandle *handle,
2783 int first, int check)
2785 struct mdd_link_data ldata = { 0 };
2791 LASSERT(oldpfid != NULL || newpfid != NULL);
2793 if (mdd_obj->mod_flags & DEAD_OBJ)
2794 /* No more links, don't bother */
2797 if (oldpfid != NULL) {
2798 rc = __mdd_links_del(env, mdd_obj, &ldata,
2802 (rc != -ENODATA && rc != -ENOENT))
2804 /* No changes done. */
2811 /* If renaming, add the new record */
2812 if (newpfid != NULL) {
2813 /* even if the add fails, we still delete the out-of-date
2815 rc2 = __mdd_links_add(env, mdd_obj, &ldata,
2816 newlname, newpfid, first, check);
2824 rc = mdd_links_write(env, mdd_obj, &ldata, handle);
2831 if (rc == -EOVERFLOW || rc == - ENOENT)
2833 if (oldpfid == NULL)
2834 CDEBUG(error ? D_ERROR : D_OTHER,
2835 "link_ea add '%.*s' failed %d "DFID"\n",
2836 newlname->ln_namelen, newlname->ln_name,
2837 rc, PFID(mdd_object_fid(mdd_obj)));
2838 else if (newpfid == NULL)
2839 CDEBUG(error ? D_ERROR : D_OTHER,
2840 "link_ea del '%.*s' failed %d "DFID"\n",
2841 oldlname->ln_namelen, oldlname->ln_name,
2842 rc, PFID(mdd_object_fid(mdd_obj)));
2844 CDEBUG(error ? D_ERROR : D_OTHER,
2845 "link_ea rename '%.*s'->'%.*s' failed %d "
2847 oldlname->ln_namelen, oldlname->ln_name,
2848 newlname->ln_namelen, newlname->ln_name,
2849 rc, PFID(mdd_object_fid(mdd_obj)));
2852 if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
2853 /* if we vmalloced a large buffer drop it */
2854 mdd_buf_put(ldata.ml_buf);
2859 static inline int mdd_links_add(const struct lu_env *env,
2860 struct mdd_object *mdd_obj,
2861 const struct lu_fid *pfid,
2862 const struct lu_name *lname,
2863 struct thandle *handle, int first)
2865 return mdd_links_rename(env, mdd_obj, NULL, NULL,
2866 pfid, lname, handle, first, 0);
2869 static inline int mdd_links_del(const struct lu_env *env,
2870 struct mdd_object *mdd_obj,
2871 const struct lu_fid *pfid,
2872 const struct lu_name *lname,
2873 struct thandle *handle)
2875 return mdd_links_rename(env, mdd_obj, pfid, lname,
2876 NULL, NULL, handle, 0, 0);
2879 const struct md_dir_operations mdd_dir_ops = {
2880 .mdo_is_subdir = mdd_is_subdir,
2881 .mdo_lookup = mdd_lookup,
2882 .mdo_create = mdd_create,
2883 .mdo_rename = mdd_rename,
2884 .mdo_link = mdd_link,
2885 .mdo_unlink = mdd_unlink,
2886 .mdo_create_data = mdd_create_data,