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);
168 parent = mdd_object_find(env, mdd, pfid);
170 /* cross-ref parent */
171 if (parent == NULL) {
174 GOTO(out, rc = -EREMOTE);
175 } else if (IS_ERR(parent))
176 GOTO(out, rc = PTR_ERR(parent));
181 if (parent && !IS_ERR(parent))
182 mdd_object_put(env, parent);
187 * No permission check is needed.
189 * returns 1: if fid is ancestor of @mo;
190 * returns 0: if fid is not a ancestor of @mo;
192 * returns EREMOTE if remote object is found, fid of remote object is saved to
195 * returns < 0: if error
197 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
198 const struct lu_fid *fid, struct lu_fid *sfid)
200 struct mdd_device *mdd = mdo2mdd(mo);
204 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
207 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
211 } else if (rc == 1) {
212 /* found @fid is parent */
220 * Check that @dir contains no entries except (possibly) dot and dotdot.
225 * -ENOTDIR not a directory object
226 * -ENOTEMPTY not empty
230 static int mdd_dir_is_empty(const struct lu_env *env,
231 struct mdd_object *dir)
234 struct dt_object *obj;
235 const struct dt_it_ops *iops;
239 obj = mdd_object_child(dir);
240 if (!dt_try_as_dir(env, obj))
243 iops = &obj->do_index_ops->dio_it;
244 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
246 result = iops->get(env, it, (const void *)"");
249 for (result = 0, i = 0; result == 0 && i < 3; ++i)
250 result = iops->next(env, it);
253 else if (result == +1)
255 } else if (result == 0)
257 * Huh? Index contains no zero key?
264 result = PTR_ERR(it);
268 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
270 struct mdd_device *m = mdd_obj2mdd_dev(obj);
271 struct lu_attr *la = &mdd_env_info(env)->mti_la;
275 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
280 * Subdir count limitation can be broken through.
282 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
283 !S_ISDIR(la->la_mode))
290 * Check whether it may create the cobj under the pobj.
293 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
294 struct mdd_object *cobj, int check_perm, int check_nlink)
299 if (cobj && mdd_object_exists(cobj))
302 if (mdd_is_dead_obj(pobj))
306 rc = mdd_permission_internal_locked(env, pobj, NULL,
307 MAY_WRITE | MAY_EXEC,
309 if (!rc && check_nlink)
310 rc = __mdd_may_link(env, pobj);
316 * Check whether can unlink from the pobj in the case of "cobj == NULL".
318 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
319 const struct lu_attr *attr)
324 if (mdd_is_dead_obj(pobj))
327 if ((attr->la_valid & LA_FLAGS) &&
328 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
331 rc = mdd_permission_internal_locked(env, pobj, NULL,
332 MAY_WRITE | MAY_EXEC,
337 if (mdd_is_append(pobj))
344 * pobj == NULL is remote ops case, under such case, pobj's
345 * VTX feature has been checked already, no need check again.
347 static inline int mdd_is_sticky(const struct lu_env *env,
348 struct mdd_object *pobj,
349 struct mdd_object *cobj)
351 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
352 struct lu_ucred *uc = lu_ucred_assert(env);
356 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
360 if (!(tmp_la->la_mode & S_ISVTX) ||
361 (tmp_la->la_uid == uc->uc_fsuid))
365 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
369 if (tmp_la->la_uid == uc->uc_fsuid)
372 return !mdd_capable(uc, CFS_CAP_FOWNER);
376 * Check whether it may delete the cobj from the pobj.
379 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
380 struct mdd_object *cobj, struct lu_attr *cattr,
381 struct lu_attr *src_attr, int check_perm, int check_empty)
387 if (!mdd_object_exists(cobj))
390 if (mdd_is_dead_obj(cobj))
394 if (!mdd_object_exists(pobj))
397 if (mdd_is_dead_obj(pobj))
401 rc = mdd_permission_internal_locked(env, pobj, NULL,
402 MAY_WRITE | MAY_EXEC,
408 if (mdd_is_append(pobj))
412 if (mdd_is_sticky(env, pobj, cobj))
415 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
418 if ((cattr->la_valid & LA_FLAGS) &&
419 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
422 /* additional check the rename case */
424 if (S_ISDIR(src_attr->la_mode)) {
425 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
427 if (!S_ISDIR(cattr->la_mode))
430 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
432 } else if (S_ISDIR(cattr->la_mode))
436 if (S_ISDIR(cattr->la_mode) && check_empty)
437 rc = mdd_dir_is_empty(env, cobj);
444 * has mdd_write_lock on src already, but not on tgt yet
446 int mdd_link_sanity_check(const struct lu_env *env,
447 struct mdd_object *tgt_obj,
448 const struct lu_name *lname,
449 struct mdd_object *src_obj)
451 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
455 if (!mdd_object_exists(src_obj))
458 if (mdd_is_dead_obj(src_obj))
461 /* Local ops, no lookup before link, check filename length here. */
462 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
463 RETURN(-ENAMETOOLONG);
465 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
468 if (S_ISDIR(mdd_object_type(src_obj)))
471 LASSERT(src_obj != tgt_obj);
473 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
478 rc = __mdd_may_link(env, src_obj);
483 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
484 const char *name, struct thandle *handle,
485 struct lustre_capa *capa)
487 struct dt_object *next = mdd_object_child(pobj);
491 if (dt_try_as_dir(env, next)) {
492 rc = next->do_index_ops->dio_delete(env, next,
493 (struct dt_key *)name,
501 static int __mdd_index_insert_only(const struct lu_env *env,
502 struct mdd_object *pobj,
503 const struct lu_fid *lf, const char *name,
504 struct thandle *handle,
505 struct lustre_capa *capa)
507 struct dt_object *next = mdd_object_child(pobj);
511 if (dt_try_as_dir(env, next)) {
512 struct lu_ucred *uc = lu_ucred_check(env);
515 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
516 rc = next->do_index_ops->dio_insert(env, next,
518 (const struct dt_key *)name,
519 handle, capa, ignore_quota);
526 /* insert named index, add reference if isdir */
527 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
528 const struct lu_fid *lf, const char *name, int is_dir,
529 struct thandle *handle, struct lustre_capa *capa)
534 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
535 if (rc == 0 && is_dir) {
536 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
537 mdo_ref_add(env, pobj, handle);
538 mdd_write_unlock(env, pobj);
543 /* delete named index, drop reference if isdir */
544 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
545 const char *name, int is_dir, struct thandle *handle,
546 struct lustre_capa *capa)
551 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
552 if (rc == 0 && is_dir) {
553 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
554 mdo_ref_del(env, pobj, handle);
555 mdd_write_unlock(env, pobj);
561 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
562 int reclen, struct thandle *handle)
566 /* XXX: this is a temporary solution to declare llog changes
567 * will be fixed in 2.3 with new llog implementation */
569 LASSERT(mdd->mdd_capa);
571 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
572 * have to set the parameter 'size' as INT_MAX or 0 to inform
573 * OSD that this record write is for a llog write or catalog
574 * header update, and osd declare function will reserve less
575 * credits for optimization purpose.
577 * Reserve 6 blocks for a llog write, since the llog file is
578 * usually small, reserve 2 blocks for catalog header update,
579 * because we know for sure that catalog header is already
582 * This hack should be removed in 2.3.
586 rc = dt_declare_record_write(env, mdd->mdd_capa,
587 DECLARE_LLOG_WRITE, 0, handle);
591 /* header will be updated as well */
592 rc = dt_declare_record_write(env, mdd->mdd_capa,
593 DECLARE_LLOG_WRITE, 0, handle);
597 /* also we should be able to create new plain log */
598 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
602 /* new record referencing new plain llog */
603 rc = dt_declare_record_write(env, mdd->mdd_capa,
604 DECLARE_LLOG_WRITE, 0, handle);
608 /* catalog's header will be updated as well */
609 rc = dt_declare_record_write(env, mdd->mdd_capa,
610 DECLARE_LLOG_REWRITE, 0, handle);
615 int mdd_declare_changelog_store(const struct lu_env *env,
616 struct mdd_device *mdd,
617 const struct lu_name *fname,
618 struct thandle *handle)
620 struct obd_device *obd = mdd2obd_dev(mdd);
621 struct llog_ctxt *ctxt;
622 struct llog_changelog_rec *rec;
628 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
631 reclen = llog_data_len(sizeof(*rec) +
632 (fname != NULL ? fname->ln_namelen : 0));
633 buf = mdd_buf_alloc(env, reclen);
634 if (buf->lb_buf == NULL)
638 rec->cr_hdr.lrh_len = reclen;
639 rec->cr_hdr.lrh_type = CHANGELOG_REC;
641 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
645 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
651 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
652 struct mdd_device *mdd,
653 const struct lu_name *tname,
654 const struct lu_name *sname,
655 struct thandle *handle)
657 struct obd_device *obd = mdd2obd_dev(mdd);
658 struct llog_ctxt *ctxt;
659 struct llog_changelog_ext_rec *rec;
665 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
668 reclen = llog_data_len(sizeof(*rec) +
669 (tname != NULL ? tname->ln_namelen : 0) +
670 (sname != NULL ? 1 + sname->ln_namelen : 0));
671 buf = mdd_buf_alloc(env, reclen);
672 if (buf->lb_buf == NULL)
676 rec->cr_hdr.lrh_len = reclen;
677 rec->cr_hdr.lrh_type = CHANGELOG_REC;
679 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
683 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
689 /** Add a changelog entry \a rec to the changelog llog
692 * \param handle - currently ignored since llogs start their own transaction;
693 * this will hopefully be fixed in llog rewrite
696 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
697 struct llog_changelog_rec *rec, struct thandle *th)
699 struct obd_device *obd = mdd2obd_dev(mdd);
700 struct llog_ctxt *ctxt;
703 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
704 rec->cr_hdr.lrh_type = CHANGELOG_REC;
705 rec->cr.cr_time = cl_time();
707 spin_lock(&mdd->mdd_cl.mc_lock);
708 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
709 * but as long as the MDD transactions are ordered correctly for e.g.
710 * rename conflicts, I don't think this should matter. */
711 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
712 spin_unlock(&mdd->mdd_cl.mc_lock);
714 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
718 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
725 /** Add a changelog_ext entry \a rec to the changelog llog
728 * \param handle - currently ignored since llogs start their own transaction;
729 * this will hopefully be fixed in llog rewrite
732 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
733 struct llog_changelog_ext_rec *rec,
736 struct obd_device *obd = mdd2obd_dev(mdd);
737 struct llog_ctxt *ctxt;
740 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
741 /* llog_lvfs_write_rec sets the llog tail len */
742 rec->cr_hdr.lrh_type = CHANGELOG_REC;
743 rec->cr.cr_time = cl_time();
745 spin_lock(&mdd->mdd_cl.mc_lock);
746 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
747 * but as long as the MDD transactions are ordered correctly for e.g.
748 * rename conflicts, I don't think this should matter. */
749 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
750 spin_unlock(&mdd->mdd_cl.mc_lock);
752 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
756 /* nested journal transaction */
757 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
765 /** Store a namespace change changelog record
766 * If this fails, we must fail the whole transaction; we don't
767 * want the change to commit without the log entry.
768 * \param target - mdd_object of change
769 * \param parent - parent dir/object
770 * \param tname - target name string
771 * \param handle - transacion handle
773 static int mdd_changelog_ns_store(const struct lu_env *env,
774 struct mdd_device *mdd,
775 enum changelog_rec_type type,
777 struct mdd_object *target,
778 struct mdd_object *parent,
779 const struct lu_name *tname,
780 struct thandle *handle)
782 struct llog_changelog_rec *rec;
789 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
791 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
794 LASSERT(target != NULL);
795 LASSERT(parent != NULL);
796 LASSERT(tname != NULL);
797 LASSERT(handle != NULL);
799 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
800 buf = mdd_buf_alloc(env, reclen);
801 if (buf->lb_buf == NULL)
805 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
806 rec->cr.cr_type = (__u32)type;
807 rec->cr.cr_tfid = *mdo2fid(target);
808 rec->cr.cr_pfid = *mdo2fid(parent);
809 rec->cr.cr_namelen = tname->ln_namelen;
810 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
812 target->mod_cltime = cfs_time_current_64();
814 rc = mdd_changelog_store(env, mdd, rec, handle);
816 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
817 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
818 PFID(&rec->cr.cr_pfid));
826 /** Store a namespace change changelog record
827 * If this fails, we must fail the whole transaction; we don't
828 * want the change to commit without the log entry.
829 * \param target - mdd_object of change
830 * \param tpfid - target parent dir/object fid
831 * \param sfid - source object fid
832 * \param spfid - source parent fid
833 * \param tname - target name string
834 * \param sname - source name string
835 * \param handle - transacion handle
837 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
838 struct mdd_device *mdd,
839 enum changelog_rec_type type,
841 struct mdd_object *target,
842 const struct lu_fid *tpfid,
843 const struct lu_fid *sfid,
844 const struct lu_fid *spfid,
845 const struct lu_name *tname,
846 const struct lu_name *sname,
847 struct thandle *handle)
849 struct llog_changelog_ext_rec *rec;
856 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
858 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
861 LASSERT(sfid != NULL);
862 LASSERT(tpfid != NULL);
863 LASSERT(tname != NULL);
864 LASSERT(handle != NULL);
866 reclen = llog_data_len(sizeof(*rec) +
867 sname != NULL ? 1 + sname->ln_namelen : 0);
868 buf = mdd_buf_alloc(env, reclen);
869 if (buf->lb_buf == NULL)
873 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
874 rec->cr.cr_type = (__u32)type;
875 rec->cr.cr_pfid = *tpfid;
876 rec->cr.cr_sfid = *sfid;
877 rec->cr.cr_spfid = *spfid;
878 rec->cr.cr_namelen = tname->ln_namelen;
879 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
881 rec->cr.cr_name[tname->ln_namelen] = '\0';
882 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
884 rec->cr.cr_namelen += 1 + sname->ln_namelen;
887 if (likely(target != NULL)) {
888 rec->cr.cr_tfid = *mdo2fid(target);
889 target->mod_cltime = cfs_time_current_64();
891 fid_zero(&rec->cr.cr_tfid);
894 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
896 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
897 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
904 static int mdd_declare_link(const struct lu_env *env,
905 struct mdd_device *mdd,
906 struct mdd_object *p,
907 struct mdd_object *c,
908 const struct lu_name *name,
909 struct thandle *handle)
913 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
917 rc = mdo_declare_ref_add(env, c, handle);
921 rc = mdo_declare_attr_set(env, p, NULL, handle);
925 rc = mdo_declare_attr_set(env, c, NULL, handle);
929 rc = mdd_declare_links_add(env, c, handle);
933 rc = mdd_declare_changelog_store(env, mdd, name, handle);
938 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
939 struct md_object *src_obj, const struct lu_name *lname,
942 const char *name = lname->ln_name;
943 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
944 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
945 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
946 struct mdd_device *mdd = mdo2mdd(src_obj);
947 struct dynlock_handle *dlh;
948 struct thandle *handle;
952 handle = mdd_trans_create(env, mdd);
954 GOTO(out_pending, rc = PTR_ERR(handle));
956 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
960 rc = mdd_trans_start(env, mdd, handle);
964 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
966 GOTO(out_trans, rc = -ENOMEM);
967 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
969 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
971 GOTO(out_unlock, rc);
973 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
975 mdd_object_capa(env, mdd_tobj));
977 GOTO(out_unlock, rc);
979 rc = mdo_ref_add(env, mdd_sobj, handle);
981 __mdd_index_delete_only(env, mdd_tobj, name, handle,
982 mdd_object_capa(env, mdd_tobj));
983 GOTO(out_unlock, rc);
986 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
987 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
989 la->la_valid = LA_CTIME | LA_MTIME;
990 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
992 GOTO(out_unlock, rc);
994 la->la_valid = LA_CTIME;
995 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
997 mdd_links_add(env, mdd_sobj,
998 mdo2fid(mdd_tobj), lname, handle, 0);
1003 mdd_write_unlock(env, mdd_sobj);
1004 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1007 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1008 mdd_tobj, lname, handle);
1010 mdd_trans_stop(env, mdd, rc, handle);
1015 int mdd_declare_finish_unlink(const struct lu_env *env,
1016 struct mdd_object *obj,
1018 struct thandle *handle)
1022 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1026 return mdo_declare_destroy(env, obj, handle);
1029 /* caller should take a lock before calling */
1030 int mdd_finish_unlink(const struct lu_env *env,
1031 struct mdd_object *obj, struct md_attr *ma,
1035 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1038 LASSERT(mdd_write_locked(env, obj) != 0);
1040 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
1041 obj->mod_flags |= DEAD_OBJ;
1042 /* add new orphan and the object
1043 * will be deleted during mdd_close() */
1044 if (obj->mod_count) {
1045 rc = __mdd_orphan_add(env, obj, th);
1047 CDEBUG(D_HA, "Object "DFID" is inserted into "
1048 "orphan list, open count = %d\n",
1049 PFID(mdd_object_fid(obj)),
1052 CERROR("Object "DFID" fail to be an orphan, "
1053 "open count = %d, maybe cause failed "
1055 PFID(mdd_object_fid(obj)),
1058 rc = mdo_destroy(env, obj, th);
1067 * has mdd_write_lock on cobj already, but not on pobj yet
1069 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1070 struct mdd_object *cobj, struct lu_attr *cattr)
1075 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1080 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1081 struct mdd_object *p, struct mdd_object *c,
1082 const struct lu_name *name, struct md_attr *ma,
1083 struct thandle *handle)
1087 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1091 rc = mdo_declare_ref_del(env, p, handle);
1095 rc = mdo_declare_ref_del(env, c, handle);
1099 rc = mdo_declare_ref_del(env, c, handle);
1103 rc = mdo_declare_attr_set(env, p, NULL, handle);
1107 rc = mdo_declare_attr_set(env, c, NULL, handle);
1111 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1115 rc = mdd_declare_links_add(env, c, handle);
1119 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1124 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1125 struct md_object *cobj, const struct lu_name *lname,
1128 const char *name = lname->ln_name;
1129 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1130 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1131 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1132 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1133 struct mdd_device *mdd = mdo2mdd(pobj);
1134 struct dynlock_handle *dlh;
1135 struct thandle *handle;
1139 if (mdd_object_exists(mdd_cobj) <= 0)
1142 handle = mdd_trans_create(env, mdd);
1144 RETURN(PTR_ERR(handle));
1146 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1151 rc = mdd_trans_start(env, mdd, handle);
1155 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1157 GOTO(stop, rc = -ENOMEM);
1158 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1161 rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1165 is_dir = S_ISDIR(cattr->la_mode);
1167 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1171 rc = mdo_ref_del(env, mdd_cobj, handle);
1173 __mdd_index_insert_only(env, mdd_pobj, mdo2fid(mdd_cobj),
1175 mdd_object_capa(env, mdd_pobj));
1179 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1180 mdd_object_capa(env, mdd_pobj));
1186 mdo_ref_del(env, mdd_cobj, handle);
1188 /* fetch updated nlink */
1189 rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1193 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1194 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1196 la->la_valid = LA_CTIME | LA_MTIME;
1197 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1201 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1202 /* update ctime of an unlinked file only if it is still
1203 * opened or a link still exists */
1204 la->la_valid = LA_CTIME;
1205 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1210 /* XXX: this transfer to ma will be removed with LOD/OSP */
1211 ma->ma_attr = *cattr;
1212 ma->ma_valid |= MA_INODE;
1213 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1215 /* fetch updated nlink */
1217 rc = mdd_la_get(env, mdd_cobj, cattr,
1218 mdd_object_capa(env, mdd_cobj));
1221 /* old files may not have link ea; ignore errors */
1222 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1224 /* if object is removed then we can't get its attrs, use last get */
1225 if (cattr->la_nlink == 0) {
1226 ma->ma_attr = *cattr;
1227 ma->ma_valid |= MA_INODE;
1231 mdd_write_unlock(env, mdd_cobj);
1232 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1236 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1237 if ((ma->ma_valid & MA_HSM) &&
1238 (ma->ma_hsm.mh_flags & HS_EXISTS))
1239 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1241 rc = mdd_changelog_ns_store(env, mdd,
1242 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1243 mdd_cobj, mdd_pobj, lname, handle);
1247 mdd_trans_stop(env, mdd, rc, handle);
1253 * The permission has been checked when obj created, no need check again.
1255 static int mdd_cd_sanity_check(const struct lu_env *env,
1256 struct mdd_object *obj)
1261 if (!obj || mdd_is_dead_obj(obj))
1268 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1269 struct md_object *cobj, const struct md_op_spec *spec,
1272 struct mdd_device *mdd = mdo2mdd(cobj);
1273 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1274 struct mdd_object *son = md2mdd_obj(cobj);
1275 struct thandle *handle;
1276 const struct lu_buf *buf;
1277 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1281 /* do not let users to create stripes via .lustre/
1282 * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1283 if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1286 rc = mdd_cd_sanity_check(env, son);
1290 if (!md_should_create(spec->sp_cr_flags))
1294 * there are following use cases for this function:
1295 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1296 * striping can be specified or not
1299 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1303 /* calling ->ah_make_hint() is used to transfer information from parent */
1304 mdd_object_make_hint(env, mdd_pobj, son, attr);
1306 handle = mdd_trans_create(env, mdd);
1308 GOTO(out_free, rc = PTR_ERR(handle));
1311 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1312 * Should this be fixed?
1314 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1315 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1316 spec->sp_cr_flags, spec->no_create);
1318 if (spec->no_create) {
1320 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1321 spec->u.sp_ea.eadatalen);
1322 } else if (!(spec->sp_cr_flags & MDS_OPEN_HAS_OBJS)) {
1323 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1325 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1326 spec->u.sp_ea.eadatalen);
1331 /* MDS_OPEN_HAS_OBJS is not used anymore ? */
1335 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1336 XATTR_NAME_LOV, 0, handle);
1340 rc = mdd_trans_start(env, mdd, handle);
1344 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1345 0, handle, mdd_object_capa(env, son));
1347 mdd_trans_stop(env, mdd, rc, handle);
1352 /* Get fid from name and parent */
1354 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1355 const struct lu_name *lname, struct lu_fid* fid, int mask)
1357 const char *name = lname->ln_name;
1358 const struct dt_key *key = (const struct dt_key *)name;
1359 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1360 struct mdd_device *m = mdo2mdd(pobj);
1361 struct dt_object *dir = mdd_object_child(mdd_obj);
1365 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1368 rc = mdd_object_exists(mdd_obj);
1369 if (unlikely(rc == 0))
1371 else if (unlikely(rc < 0)) {
1372 CERROR("Object "DFID" locates on remote server\n",
1373 PFID(mdo2fid(mdd_obj)));
1377 /* The common filename length check. */
1378 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1379 RETURN(-ENAMETOOLONG);
1381 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1386 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1387 dt_try_as_dir(env, dir))) {
1389 rc = dir->do_index_ops->dio_lookup(env, dir,
1390 (struct dt_rec *)fid, key,
1391 mdd_object_capa(env, mdd_obj));
1402 int mdd_declare_object_initialize(const struct lu_env *env,
1403 struct mdd_object *child,
1404 struct lu_attr *attr,
1405 struct thandle *handle)
1409 rc = mdo_declare_attr_set(env, child, attr, handle);
1410 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1411 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1414 rc = mdo_declare_ref_add(env, child, handle);
1418 mdd_declare_links_add(env, child, handle);
1423 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1424 const struct lu_name *lname, struct mdd_object *child,
1425 struct lu_attr *attr, struct thandle *handle,
1426 const struct md_op_spec *spec)
1432 * Update attributes for child.
1435 * (1) the valid bits should be converted between Lustre and Linux;
1436 * (2) maybe, the child attributes should be set in OSD when creation.
1440 * inode mode has been set in creation time, and it's based on umask,
1441 * la_mode and acl, don't set here again! (which will go wrong
1442 * because below function doesn't consider umask).
1443 * I'd suggest set all object attributes in creation time, see above.
1445 LASSERT(attr->la_valid & LA_MODE);
1446 attr->la_valid &= ~LA_MODE;
1447 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1448 /* arguments are supposed to stay the same */
1449 attr->la_valid |= LA_MODE;
1453 if (S_ISDIR(attr->la_mode)) {
1454 /* Add "." and ".." for newly created dir */
1455 mdo_ref_add(env, child, handle);
1456 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1457 dot, handle, BYPASS_CAPA);
1459 rc = __mdd_index_insert_only(env, child, pfid,
1463 mdo_ref_del(env, child, handle);
1466 mdd_links_add(env, child, pfid, lname, handle, 1);
1471 /* has not lock on pobj yet */
1472 static int mdd_create_sanity_check(const struct lu_env *env,
1473 struct md_object *pobj,
1474 struct lu_attr *pattr,
1475 const struct lu_name *lname,
1476 struct lu_attr *cattr,
1477 struct md_op_spec *spec)
1479 struct mdd_thread_info *info = mdd_env_info(env);
1480 struct lu_fid *fid = &info->mti_fid;
1481 struct mdd_object *obj = md2mdd_obj(pobj);
1482 struct mdd_device *m = mdo2mdd(pobj);
1483 int lookup = spec->sp_cr_lookup;
1488 if (mdd_is_dead_obj(obj))
1492 * In some cases this lookup is not needed - we know before if name
1493 * exists or not because MDT performs lookup for it.
1494 * name length check is done in lookup.
1498 * Check if the name already exist, though it will be checked in
1499 * _index_insert also, for avoiding rolling back if exists
1502 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1503 MAY_WRITE | MAY_EXEC);
1505 RETURN(rc ? : -EEXIST);
1508 * Check WRITE permission for the parent.
1509 * EXEC permission have been checked
1510 * when lookup before create already.
1512 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1519 if (pattr->la_mode & S_ISGID) {
1520 cattr->la_gid = pattr->la_gid;
1521 if (S_ISDIR(cattr->la_mode)) {
1522 cattr->la_mode |= S_ISGID;
1523 cattr->la_valid |= LA_MODE;
1527 switch (cattr->la_mode & S_IFMT) {
1529 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1531 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1532 RETURN(-ENAMETOOLONG);
1551 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1552 struct mdd_object *p, struct mdd_object *c,
1553 const struct lu_name *name,
1554 struct lu_attr *attr,
1556 struct thandle *handle,
1557 const struct md_op_spec *spec)
1561 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1565 #ifdef CONFIG_FS_POSIX_ACL
1566 if (got_def_acl > 0) {
1567 struct lu_buf *acl_buf;
1569 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1570 /* if dir, then can inherit default ACl */
1571 if (S_ISDIR(attr->la_mode)) {
1572 rc = mdo_declare_xattr_set(env, c, acl_buf,
1573 XATTR_NAME_ACL_DEFAULT,
1579 rc = mdo_declare_attr_set(env, c, attr, handle);
1583 rc = mdo_declare_xattr_set(env, c, acl_buf,
1584 XATTR_NAME_ACL_ACCESS, 0, handle);
1590 if (S_ISDIR(attr->la_mode)) {
1591 rc = mdo_declare_ref_add(env, p, handle);
1596 rc = mdd_declare_object_initialize(env, c, attr, handle);
1600 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1601 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1603 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1604 name->ln_name, handle);
1608 /* replay case, create LOV EA from client data */
1609 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1610 const struct lu_buf *buf;
1612 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1613 spec->u.sp_ea.eadatalen);
1614 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1620 if (S_ISLNK(attr->la_mode)) {
1621 rc = dt_declare_record_write(env, mdd_object_child(c),
1622 strlen(spec->u.sp_symname), 0,
1628 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1629 rc = mdo_declare_attr_set(env, p, attr, handle);
1634 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1644 * Create object and insert it into namespace.
1646 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1647 const struct lu_name *lname, struct md_object *child,
1648 struct md_op_spec *spec, struct md_attr* ma)
1650 struct mdd_thread_info *info = mdd_env_info(env);
1651 struct lu_attr *la = &info->mti_la_for_fix;
1652 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1653 struct mdd_object *son = md2mdd_obj(child);
1654 struct mdd_device *mdd = mdo2mdd(pobj);
1655 struct lu_attr *attr = &ma->ma_attr;
1656 struct thandle *handle;
1657 struct lu_attr *pattr = &info->mti_pattr;
1658 struct dynlock_handle *dlh;
1659 const char *name = lname->ln_name;
1660 int rc, created = 0, initialized = 0, inserted = 0;
1661 int got_def_acl = 0;
1665 * Two operations have to be performed:
1667 * - an allocation of a new object (->do_create()), and
1669 * - an insertion into a parent index (->dio_insert()).
1671 * Due to locking, operation order is not important, when both are
1672 * successful, *but* error handling cases are quite different:
1674 * - if insertion is done first, and following object creation fails,
1675 * insertion has to be rolled back, but this operation might fail
1676 * also leaving us with dangling index entry.
1678 * - if creation is done first, is has to be undone if insertion
1679 * fails, leaving us with leaked space, which is neither good, nor
1682 * It seems that creation-first is simplest solution, but it is
1683 * sub-optimal in the frequent
1688 * case, because second mkdir is bound to create object, only to
1689 * destroy it immediately.
1691 * To avoid this follow local file systems that do double lookup:
1693 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1695 * 1. create (mdd_object_create_internal())
1697 * 2. insert (__mdd_index_insert(), lookup again)
1700 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1704 /* Sanity checks before big job. */
1705 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1709 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1710 GOTO(out_free, rc = -EINPROGRESS);
1712 if (!S_ISLNK(attr->la_mode)) {
1713 struct lu_buf *acl_buf;
1715 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1716 sizeof(info->mti_xattr_buf));
1717 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1718 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1719 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1720 mdd_read_unlock(env, mdd_pobj);
1723 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1727 mdd_object_make_hint(env, mdd_pobj, son, attr);
1729 handle = mdd_trans_create(env, mdd);
1731 GOTO(out_free, rc = PTR_ERR(handle));
1733 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1734 got_def_acl, handle, spec);
1738 rc = mdd_trans_start(env, mdd, handle);
1742 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1744 GOTO(out_trans, rc = -ENOMEM);
1746 mdd_write_lock(env, son, MOR_TGT_CHILD);
1747 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
1749 mdd_write_unlock(env, son);
1755 #ifdef CONFIG_FS_POSIX_ACL
1757 struct lu_buf *acl_buf;
1759 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1760 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1762 mdd_write_unlock(env, son);
1768 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1769 son, attr, handle, spec);
1772 * in case of replay we just set LOVEA provided by the client
1773 * XXX: I think it would be interesting to try "old" way where
1774 * MDT calls this xattr_set(LOV) in a different transaction.
1775 * probably this way we code can be made better.
1777 if (rc == 0 && (spec->no_create ||
1778 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1779 const struct lu_buf *buf;
1781 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1782 spec->u.sp_ea.eadatalen);
1783 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1787 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1788 rc = __mdd_orphan_add(env, son, handle);
1790 mdd_write_unlock(env, son);
1794 * Object has no links, so it will be destroyed when last
1795 * reference is released. (XXX not now.)
1801 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
1802 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1803 name, S_ISDIR(attr->la_mode), handle,
1804 mdd_object_capa(env, mdd_pobj));
1811 if (S_ISLNK(attr->la_mode)) {
1812 struct lu_ucred *uc = lu_ucred_assert(env);
1813 struct dt_object *dt = mdd_object_child(son);
1814 const char *target_name = spec->u.sp_symname;
1815 int sym_len = strlen(target_name);
1816 const struct lu_buf *buf;
1819 buf = mdd_buf_get_const(env, target_name, sym_len);
1820 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1821 mdd_object_capa(env, son),
1823 CFS_CAP_SYS_RESOURCE_MASK);
1828 GOTO(cleanup, rc = -EFAULT);
1831 /* volatile file creation does not update parent directory times */
1832 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1833 GOTO(cleanup, rc = 0);
1835 /* update parent directory mtime/ctime */
1837 la->la_valid = LA_CTIME | LA_MTIME;
1838 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1844 if (rc != 0 && created != 0) {
1847 if (inserted != 0) {
1848 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1849 rc2 = __mdd_orphan_del(env, son, handle);
1851 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1852 S_ISDIR(attr->la_mode),
1853 handle, BYPASS_CAPA);
1858 mdd_write_lock(env, son, MOR_TGT_CHILD);
1859 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1860 /* Drop the reference, no need to delete "."/"..",
1861 * because the object to be destroied directly. */
1862 rc2 = mdo_ref_del(env, son, handle);
1864 mdd_write_unlock(env, son);
1869 rc2 = mdo_ref_del(env, son, handle);
1871 mdd_write_unlock(env, son);
1875 mdo_destroy(env, son, handle);
1876 mdd_write_unlock(env, son);
1879 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1882 rc = mdd_changelog_ns_store(env, mdd,
1883 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1884 S_ISREG(attr->la_mode) ? CL_CREATE :
1885 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1886 0, son, mdd_pobj, lname, handle);
1888 mdd_trans_stop(env, mdd, rc, handle);
1890 /* The child object shouldn't be cached anymore */
1892 set_bit(LU_OBJECT_HEARD_BANSHEE,
1893 &child->mo_lu.lo_header->loh_flags);
1898 * Get locks on parents in proper order
1899 * RETURN: < 0 - error, rename_order if successful
1907 static int mdd_rename_order(const struct lu_env *env,
1908 struct mdd_device *mdd,
1909 struct mdd_object *src_pobj,
1910 struct mdd_object *tgt_pobj)
1912 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1916 if (src_pobj == tgt_pobj)
1917 RETURN(MDD_RN_SAME);
1919 /* compared the parent child relationship of src_p&tgt_p */
1920 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1922 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1925 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1938 /* has not mdd_write{read}_lock on any obj yet. */
1939 static int mdd_rename_sanity_check(const struct lu_env *env,
1940 struct mdd_object *src_pobj,
1941 struct mdd_object *tgt_pobj,
1942 struct mdd_object *sobj,
1943 struct mdd_object *tobj,
1944 struct lu_attr *so_attr,
1945 struct lu_attr *tg_attr)
1950 /* XXX: when get here, sobj must NOT be NULL,
1951 * the other case has been processed in cml_rename
1952 * before mdd_rename and enable MDS_PERM_BYPASS. */
1955 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
1959 /* XXX: when get here, "tobj == NULL" means tobj must
1960 * NOT exist (neither on remote MDS, such case has been
1961 * processed in cml_rename before mdd_rename and enable
1963 * So check may_create, but not check may_unlink. */
1965 rc = mdd_may_create(env, tgt_pobj, NULL,
1966 (src_pobj != tgt_pobj), 0);
1968 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
1969 (src_pobj != tgt_pobj), 1);
1971 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1972 S_ISDIR(so_attr->la_mode))
1973 rc = __mdd_may_link(env, tgt_pobj);
1978 static int mdd_declare_rename(const struct lu_env *env,
1979 struct mdd_device *mdd,
1980 struct mdd_object *mdd_spobj,
1981 struct mdd_object *mdd_tpobj,
1982 struct mdd_object *mdd_sobj,
1983 struct mdd_object *mdd_tobj,
1984 const struct lu_name *tname,
1985 const struct lu_name *sname,
1987 struct thandle *handle)
1995 /* name from source dir */
1996 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2000 /* .. from source child */
2001 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2002 /* source child can be directory,
2003 * counted by source dir's nlink */
2004 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2008 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2012 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2017 /* new target child can be directory,
2018 * counted by target dir's nlink */
2019 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2025 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2029 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2032 mdd_declare_links_add(env, mdd_sobj, handle);
2036 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2041 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2042 tname->ln_name, handle);
2046 /* name from target dir (old name), we declare it unconditionally
2047 * as mdd_rename() calls delete unconditionally as well. so just
2048 * to balance declarations vs calls to change ... */
2049 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2053 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2054 /* delete target child in target parent directory */
2055 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2059 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2060 /* target child can be directory,
2061 * delete "." reference in target child directory */
2062 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2066 /* delete ".." reference in target parent directory */
2067 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2072 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2076 mdd_declare_links_add(env, mdd_tobj, handle);
2080 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2085 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2092 /* src object can be remote that is why we use only fid and type of object */
2093 static int mdd_rename(const struct lu_env *env,
2094 struct md_object *src_pobj, struct md_object *tgt_pobj,
2095 const struct lu_fid *lf, const struct lu_name *lsname,
2096 struct md_object *tobj, const struct lu_name *ltname,
2099 const char *sname = lsname->ln_name;
2100 const char *tname = ltname->ln_name;
2101 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2102 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2103 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2104 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2105 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2106 struct mdd_device *mdd = mdo2mdd(src_pobj);
2107 struct mdd_object *mdd_sobj = NULL; /* source object */
2108 struct mdd_object *mdd_tobj = NULL;
2109 struct dynlock_handle *sdlh, *tdlh;
2110 struct thandle *handle;
2111 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2112 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2115 bool tobj_locked = 0;
2116 unsigned cl_flags = 0;
2121 mdd_tobj = md2mdd_obj(tobj);
2123 mdd_sobj = mdd_object_find(env, mdd, lf);
2125 handle = mdd_trans_create(env, mdd);
2127 GOTO(out_pending, rc = PTR_ERR(handle));
2129 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2130 mdd_tobj, lsname, ltname, ma, handle);
2134 rc = mdd_trans_start(env, mdd, handle);
2138 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2139 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2141 GOTO(cleanup_unlocked, rc);
2143 /* Get locks in determined order */
2144 if (rc == MDD_RN_SAME) {
2145 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2146 sname, MOR_SRC_PARENT);
2147 /* check hashes to determine do we need one lock or two */
2148 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2149 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2153 } else if (rc == MDD_RN_SRCTGT) {
2154 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2155 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2157 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2158 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2160 if (sdlh == NULL || tdlh == NULL)
2161 GOTO(cleanup, rc = -ENOMEM);
2163 rc = mdd_la_get(env, mdd_sobj, so_attr,
2164 mdd_object_capa(env, mdd_sobj));
2169 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2170 mdd_object_capa(env, mdd_tobj));
2175 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2176 mdd_tobj, so_attr, tg_attr);
2180 is_dir = S_ISDIR(so_attr->la_mode);
2182 /* Remove source name from source directory */
2183 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2184 mdd_object_capa(env, mdd_spobj));
2188 /* "mv dir1 dir2" needs "dir1/.." link update */
2189 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2190 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2191 mdd_object_capa(env, mdd_sobj));
2193 GOTO(fixup_spobj2, rc);
2195 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2196 handle, mdd_object_capa(env, mdd_sobj));
2198 GOTO(fixup_spobj, rc);
2201 /* Remove target name from target directory
2202 * Here tobj can be remote one, so we do index_delete unconditionally
2203 * and -ENOENT is allowed.
2205 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2206 mdd_object_capa(env, mdd_tpobj));
2209 /* tname might been renamed to something else */
2210 GOTO(fixup_spobj, rc);
2213 GOTO(fixup_spobj, rc);
2216 /* Insert new fid with target name into target dir */
2217 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2218 mdd_object_capa(env, mdd_tpobj));
2220 GOTO(fixup_tpobj, rc);
2222 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2223 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2225 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2227 la->la_valid = LA_CTIME;
2228 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2230 GOTO(fixup_tpobj, rc);
2233 /* Remove old target object
2234 * For tobj is remote case cmm layer has processed
2235 * and set tobj to NULL then. So when tobj is NOT NULL,
2236 * it must be local one.
2238 if (tobj && mdd_object_exists(mdd_tobj)) {
2239 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2241 if (mdd_is_dead_obj(mdd_tobj)) {
2242 /* shld not be dead, something is wrong */
2243 CERROR("tobj is dead, something is wrong\n");
2247 mdo_ref_del(env, mdd_tobj, handle);
2249 /* Remove dot reference. */
2250 if (S_ISDIR(tg_attr->la_mode))
2251 mdo_ref_del(env, mdd_tobj, handle);
2254 /* fetch updated nlink */
2255 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2256 mdd_object_capa(env, mdd_tobj));
2258 CERROR("%s: Failed to get nlink for tobj "
2260 mdd2obd_dev(mdd)->obd_name,
2261 PFID(tpobj_fid), rc);
2262 GOTO(fixup_tpobj, rc);
2265 la->la_valid = LA_CTIME;
2266 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2268 CERROR("%s: Failed to set ctime for tobj "
2270 mdd2obd_dev(mdd)->obd_name,
2271 PFID(tpobj_fid), rc);
2272 GOTO(fixup_tpobj, rc);
2275 /* XXX: this transfer to ma will be removed with LOD/OSP */
2276 ma->ma_attr = *tg_attr;
2277 ma->ma_valid |= MA_INODE;
2278 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2280 CERROR("%s: Failed to unlink tobj "
2282 mdd2obd_dev(mdd)->obd_name,
2283 PFID(tpobj_fid), rc);
2284 GOTO(fixup_tpobj, rc);
2287 /* fetch updated nlink */
2288 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2289 mdd_object_capa(env, mdd_tobj));
2291 CERROR("%s: Failed to get nlink for tobj "
2293 mdd2obd_dev(mdd)->obd_name,
2294 PFID(tpobj_fid), rc);
2295 GOTO(fixup_tpobj, rc);
2297 /* XXX: this transfer to ma will be removed with LOD/OSP */
2298 ma->ma_attr = *tg_attr;
2299 ma->ma_valid |= MA_INODE;
2301 if (so_attr->la_nlink == 0)
2302 cl_flags |= CLF_RENAME_LAST;
2305 la->la_valid = LA_CTIME | LA_MTIME;
2306 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2308 GOTO(fixup_tpobj, rc);
2310 if (mdd_spobj != mdd_tpobj) {
2311 la->la_valid = LA_CTIME | LA_MTIME;
2312 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2316 if (rc == 0 && mdd_sobj) {
2317 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2318 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2319 mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
2321 /* Old files might not have EA entry */
2322 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2324 mdd_write_unlock(env, mdd_sobj);
2325 /* We don't fail the transaction if the link ea can't be
2326 updated -- fid2path will use alternate lookup method. */
2334 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2337 CWARN("tp obj fix error %d\n",rc2);
2339 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2340 !mdd_is_dead_obj(mdd_tobj)) {
2342 mdo_ref_add(env, mdd_tobj, handle);
2344 mdo_ref_add(env, mdd_tobj, handle);
2347 rc2 = __mdd_index_insert(env, mdd_tpobj,
2348 mdo2fid(mdd_tobj), tname,
2353 CWARN("tp obj fix error %d\n",rc2);
2358 if (rc && is_dir && mdd_sobj) {
2359 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2363 CWARN("sp obj dotdot delete error %d\n",rc2);
2366 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2367 dotdot, handle, BYPASS_CAPA);
2369 CWARN("sp obj dotdot insert error %d\n",rc2);
2374 rc2 = __mdd_index_insert(env, mdd_spobj,
2375 lf, sname, is_dir, handle, BYPASS_CAPA);
2377 CWARN("sp obj fix error %d\n",rc2);
2381 mdd_write_unlock(env, mdd_tobj);
2382 if (likely(tdlh) && sdlh != tdlh)
2383 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2385 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2388 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2389 mdd_tobj, tpobj_fid, lf,
2390 spobj_fid, ltname, lsname,
2394 mdd_trans_stop(env, mdd, rc, handle);
2396 mdd_object_put(env, mdd_sobj);
2402 * The data that link search is done on.
2404 struct mdd_link_data {
2406 * Buffer to keep link EA body.
2408 struct lu_buf *ml_buf;
2410 * The matched header, entry and its lenght in the EA
2412 struct link_ea_header *ml_leh;
2413 struct link_ea_entry *ml_lee;
2417 static int mdd_links_new(const struct lu_env *env,
2418 struct mdd_link_data *ldata)
2420 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2421 if (ldata->ml_buf->lb_buf == NULL)
2423 ldata->ml_leh = ldata->ml_buf->lb_buf;
2424 ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
2425 ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
2426 ldata->ml_leh->leh_reccount = 0;
2430 /** Read the link EA into a temp buffer.
2431 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
2432 * A pointer to the buffer is stored in \a ldata::ml_buf.
2434 * \retval 0 or error
2436 int mdd_links_read(const struct lu_env *env,
2437 struct mdd_object *mdd_obj,
2438 struct mdd_link_data *ldata)
2440 struct lustre_capa *capa;
2441 struct link_ea_header *leh;
2444 /* First try a small buf */
2445 LASSERT(env != NULL);
2446 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2447 if (ldata->ml_buf->lb_buf == NULL)
2450 if (!mdd_object_exists(mdd_obj))
2453 capa = mdd_object_capa(env, mdd_obj);
2454 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2455 XATTR_NAME_LINK, capa);
2456 if (rc == -ERANGE) {
2457 /* Buf was too small, figure out what we need. */
2458 mdd_buf_put(ldata->ml_buf);
2459 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2460 XATTR_NAME_LINK, capa);
2463 ldata->ml_buf = mdd_buf_alloc(env, rc);
2464 if (ldata->ml_buf->lb_buf == NULL)
2466 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2467 XATTR_NAME_LINK, capa);
2472 leh = ldata->ml_buf->lb_buf;
2473 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2474 leh->leh_magic = LINK_EA_MAGIC;
2475 leh->leh_reccount = __swab32(leh->leh_reccount);
2476 leh->leh_len = __swab64(leh->leh_len);
2477 /* entries are swabbed by mdd_lee_unpack */
2479 if (leh->leh_magic != LINK_EA_MAGIC)
2481 if (leh->leh_reccount == 0)
2484 ldata->ml_leh = leh;
2488 /** Read the link EA into a temp buffer.
2489 * Uses the name_buf since it is generally large.
2490 * \retval IS_ERR err
2491 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2493 struct lu_buf *mdd_links_get(const struct lu_env *env,
2494 struct mdd_object *mdd_obj)
2496 struct mdd_link_data ldata = { 0 };
2499 rc = mdd_links_read(env, mdd_obj, &ldata);
2500 return rc ? ERR_PTR(rc) : ldata.ml_buf;
2503 static int mdd_links_write(const struct lu_env *env,
2504 struct mdd_object *mdd_obj,
2505 struct mdd_link_data *ldata,
2506 struct thandle *handle)
2508 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
2509 ldata->ml_leh->leh_len);
2510 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
2511 mdd_object_capa(env, mdd_obj));
2514 /** Pack a link_ea_entry.
2515 * All elements are stored as chars to avoid alignment issues.
2516 * Numbers are always big-endian
2517 * \retval record length
2519 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2520 const struct lu_fid *pfid)
2522 struct lu_fid tmpfid;
2525 fid_cpu_to_be(&tmpfid, pfid);
2526 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2527 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2528 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2530 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2531 lee->lee_reclen[1] = reclen & 0xff;
2535 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2536 struct lu_name *lname, struct lu_fid *pfid)
2538 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2539 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2540 fid_be_to_cpu(pfid, pfid);
2541 lname->ln_name = lee->lee_name;
2542 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2545 static int mdd_declare_links_add(const struct lu_env *env,
2546 struct mdd_object *mdd_obj,
2547 struct thandle *handle)
2551 /* XXX: max size? */
2552 rc = mdo_declare_xattr_set(env, mdd_obj,
2553 mdd_buf_get_const(env, NULL, 4096),
2554 XATTR_NAME_LINK, 0, handle);
2559 /* For pathologic linkers, we don't want to spend lots of time scanning the
2560 * link ea. Limit ourseleves to something reasonable; links not in the EA
2561 * can be looked up via (slower) parent lookup.
2563 #define LINKEA_MAX_COUNT 128
2565 /** Add a record to the end of link ea buf */
2566 static int mdd_links_add_buf(const struct lu_env *env,
2567 struct mdd_link_data *ldata,
2568 const struct lu_name *lname,
2569 const struct lu_fid *pfid)
2571 LASSERT(ldata->ml_leh != NULL);
2573 if (lname == NULL || pfid == NULL)
2576 /* Make sure our buf is big enough for the new one */
2577 if (ldata->ml_leh->leh_reccount > LINKEA_MAX_COUNT)
2580 ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2581 if (ldata->ml_leh->leh_len + ldata->ml_reclen >
2582 ldata->ml_buf->lb_len) {
2583 if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
2584 ldata->ml_reclen) < 0)
2588 ldata->ml_leh = ldata->ml_buf->lb_buf;
2589 ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
2590 ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
2591 ldata->ml_leh->leh_len += ldata->ml_reclen;
2592 ldata->ml_leh->leh_reccount++;
2593 CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
2594 lname->ln_namelen, lname->ln_name);
2598 /** Del the current record from the link ea buf */
2599 static void mdd_links_del_buf(const struct lu_env *env,
2600 struct mdd_link_data *ldata,
2601 const struct lu_name *lname)
2603 LASSERT(ldata->ml_leh != NULL);
2605 ldata->ml_leh->leh_reccount--;
2606 ldata->ml_leh->leh_len -= ldata->ml_reclen;
2607 memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
2608 (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
2609 (char *)ldata->ml_lee);
2610 CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
2611 lname->ln_namelen, lname->ln_name);
2616 * Check if such a link exists in linkEA.
2618 * \param mdd_obj object being handled
2619 * \param pfid parent fid the link to be found for
2620 * \param lname name in the parent's directory entry pointing to this object
2621 * \param ldata link data the search to be done on
2624 * \retval -ENOENT link does not exist
2625 * \retval -ve on error
2627 static int mdd_links_find(const struct lu_env *env,
2628 struct mdd_object *mdd_obj,
2629 struct mdd_link_data *ldata,
2630 const struct lu_name *lname,
2631 const struct lu_fid *pfid)
2633 struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
2634 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2637 LASSERT(ldata->ml_leh != NULL);
2640 ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
2642 for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
2643 mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
2645 if (tmpname->ln_namelen == lname->ln_namelen &&
2646 lu_fid_eq(tmpfid, pfid) &&
2647 (strncmp(tmpname->ln_name, lname->ln_name,
2648 tmpname->ln_namelen) == 0))
2650 ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
2654 if (count == ldata->ml_leh->leh_reccount) {
2655 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2656 lname->ln_namelen, lname->ln_name);
2662 static int __mdd_links_add(const struct lu_env *env,
2663 struct mdd_object *mdd_obj,
2664 struct mdd_link_data *ldata,
2665 const struct lu_name *lname,
2666 const struct lu_fid *pfid,
2667 int first, int check)
2671 if (ldata->ml_leh == NULL) {
2672 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
2676 rc = mdd_links_new(env, ldata);
2683 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2684 if (rc && rc != -ENOENT)
2690 return mdd_links_add_buf(env, ldata, lname, pfid);
2693 static int __mdd_links_del(const struct lu_env *env,
2694 struct mdd_object *mdd_obj,
2695 struct mdd_link_data *ldata,
2696 const struct lu_name *lname,
2697 const struct lu_fid *pfid)
2701 if (ldata->ml_leh == NULL) {
2702 rc = mdd_links_read(env, mdd_obj, ldata);
2707 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2711 mdd_links_del_buf(env, ldata, lname);
2715 static int mdd_links_rename(const struct lu_env *env,
2716 struct mdd_object *mdd_obj,
2717 const struct lu_fid *oldpfid,
2718 const struct lu_name *oldlname,
2719 const struct lu_fid *newpfid,
2720 const struct lu_name *newlname,
2721 struct thandle *handle,
2722 int first, int check)
2724 struct mdd_link_data ldata = { 0 };
2730 LASSERT(oldpfid != NULL || newpfid != NULL);
2732 if (mdd_obj->mod_flags & DEAD_OBJ)
2733 /* No more links, don't bother */
2736 if (oldpfid != NULL) {
2737 rc = __mdd_links_del(env, mdd_obj, &ldata,
2741 (rc != -ENODATA && rc != -ENOENT))
2743 /* No changes done. */
2750 /* If renaming, add the new record */
2751 if (newpfid != NULL) {
2752 /* even if the add fails, we still delete the out-of-date
2754 rc2 = __mdd_links_add(env, mdd_obj, &ldata,
2755 newlname, newpfid, first, check);
2763 rc = mdd_links_write(env, mdd_obj, &ldata, handle);
2770 if (rc == -EOVERFLOW || rc == - ENOENT)
2772 if (oldpfid == NULL)
2773 CDEBUG(error ? D_ERROR : D_OTHER,
2774 "link_ea add '%.*s' failed %d "DFID"\n",
2775 newlname->ln_namelen, newlname->ln_name,
2776 rc, PFID(mdd_object_fid(mdd_obj)));
2777 else if (newpfid == NULL)
2778 CDEBUG(error ? D_ERROR : D_OTHER,
2779 "link_ea del '%.*s' failed %d "DFID"\n",
2780 oldlname->ln_namelen, oldlname->ln_name,
2781 rc, PFID(mdd_object_fid(mdd_obj)));
2783 CDEBUG(error ? D_ERROR : D_OTHER,
2784 "link_ea rename '%.*s'->'%.*s' failed %d "
2786 oldlname->ln_namelen, oldlname->ln_name,
2787 newlname->ln_namelen, newlname->ln_name,
2788 rc, PFID(mdd_object_fid(mdd_obj)));
2791 if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
2792 /* if we vmalloced a large buffer drop it */
2793 mdd_buf_put(ldata.ml_buf);
2798 static inline int mdd_links_add(const struct lu_env *env,
2799 struct mdd_object *mdd_obj,
2800 const struct lu_fid *pfid,
2801 const struct lu_name *lname,
2802 struct thandle *handle, int first)
2804 return mdd_links_rename(env, mdd_obj, NULL, NULL,
2805 pfid, lname, handle, first, 0);
2808 static inline int mdd_links_del(const struct lu_env *env,
2809 struct mdd_object *mdd_obj,
2810 const struct lu_fid *pfid,
2811 const struct lu_name *lname,
2812 struct thandle *handle)
2814 return mdd_links_rename(env, mdd_obj, pfid, lname,
2815 NULL, NULL, handle, 0, 0);
2818 const struct md_dir_operations mdd_dir_ops = {
2819 .mdo_is_subdir = mdd_is_subdir,
2820 .mdo_lookup = mdd_lookup,
2821 .mdo_create = mdd_create,
2822 .mdo_rename = mdd_rename,
2823 .mdo_link = mdd_link,
2824 .mdo_unlink = mdd_unlink,
2825 .mdo_create_data = mdd_create_data,