4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_dir.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
50 #include "mdd_internal.h"
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
55 static struct lu_name lname_dotdot = {
60 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
61 const struct lu_name *lname, struct lu_fid* fid,
63 static inline int mdd_links_add(const struct lu_env *env,
64 struct mdd_object *mdd_obj,
65 const struct lu_fid *pfid,
66 const struct lu_name *lname,
67 struct thandle *handle, int first);
68 static inline int mdd_links_del(const struct lu_env *env,
69 struct mdd_object *mdd_obj,
70 const struct lu_fid *pfid,
71 const struct lu_name *lname,
72 struct thandle *handle);
75 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
76 const struct lu_name *lname, struct lu_fid* fid, int mask)
78 const char *name = lname->ln_name;
79 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
80 struct dynlock_handle *dlh;
83 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
84 if (unlikely(dlh == NULL))
86 rc = __mdd_lookup(env, pobj, lname, fid, mask);
87 mdd_pdo_read_unlock(env, mdd_obj, dlh);
92 int mdd_lookup(const struct lu_env *env,
93 struct md_object *pobj, const struct lu_name *lname,
94 struct lu_fid* fid, struct md_op_spec *spec)
98 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
102 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
105 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
109 * For root fid use special function, which does not compare version component
110 * of fid. Version component is different for root fids on all MDTs.
112 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
114 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
115 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
119 * return 1: if lf is the fid of the ancestor of p1;
122 * return -EREMOTE: if remote object is found, in this
123 * case fid of remote object is saved to @pf;
125 * otherwise: values < 0, errors.
127 static int mdd_is_parent(const struct lu_env *env,
128 struct mdd_device *mdd,
129 struct mdd_object *p1,
130 const struct lu_fid *lf,
133 struct mdd_object *parent = NULL;
138 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
139 pfid = &mdd_env_info(env)->mti_fid;
141 /* Check for root first. */
142 if (mdd_is_root(mdd, mdo2fid(p1)))
146 /* this is done recursively, bypass capa for each obj */
147 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
148 rc = mdd_parent_fid(env, p1, pfid);
151 if (mdd_is_root(mdd, pfid))
153 if (lu_fid_eq(pfid, lf))
156 mdd_object_put(env, parent);
158 parent = mdd_object_find(env, mdd, pfid);
159 if (IS_ERR(parent)) {
160 GOTO(out, rc = PTR_ERR(parent));
161 } else if (mdd_object_remote(parent)) {
162 /*FIXME: Because of the restriction of rename in Phase I.
163 * If the parent is remote, we just assumed lf is not the
164 * parent of P1 for now */
171 if (parent && !IS_ERR(parent))
172 mdd_object_put(env, parent);
177 * No permission check is needed.
179 * returns 1: if fid is ancestor of @mo;
180 * returns 0: if fid is not a ancestor of @mo;
182 * returns EREMOTE if remote object is found, fid of remote object is saved to
185 * returns < 0: if error
187 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
188 const struct lu_fid *fid, struct lu_fid *sfid)
190 struct mdd_device *mdd = mdo2mdd(mo);
194 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
197 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
201 } else if (rc == 1) {
202 /* found @fid is parent */
210 * Check that @dir contains no entries except (possibly) dot and dotdot.
215 * -ENOTDIR not a directory object
216 * -ENOTEMPTY not empty
220 static int mdd_dir_is_empty(const struct lu_env *env,
221 struct mdd_object *dir)
224 struct dt_object *obj;
225 const struct dt_it_ops *iops;
229 obj = mdd_object_child(dir);
230 if (!dt_try_as_dir(env, obj))
233 iops = &obj->do_index_ops->dio_it;
234 it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
236 result = iops->get(env, it, (const void *)"");
239 for (result = 0, i = 0; result == 0 && i < 3; ++i)
240 result = iops->next(env, it);
243 else if (result == +1)
245 } else if (result == 0)
247 * Huh? Index contains no zero key?
254 result = PTR_ERR(it);
258 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
260 struct mdd_device *m = mdd_obj2mdd_dev(obj);
261 struct lu_attr *la = &mdd_env_info(env)->mti_la;
265 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
270 * Subdir count limitation can be broken through.
272 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
273 !S_ISDIR(la->la_mode))
280 * Check whether it may create the cobj under the pobj.
283 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
284 struct mdd_object *cobj, int check_perm, int check_nlink)
289 if (cobj && mdd_object_exists(cobj))
292 if (mdd_is_dead_obj(pobj))
296 rc = mdd_permission_internal_locked(env, pobj, NULL,
297 MAY_WRITE | MAY_EXEC,
299 if (!rc && check_nlink)
300 rc = __mdd_may_link(env, pobj);
306 * Check whether can unlink from the pobj in the case of "cobj == NULL".
308 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
309 const struct lu_attr *attr)
314 if (mdd_is_dead_obj(pobj))
317 if ((attr->la_valid & LA_FLAGS) &&
318 (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
321 rc = mdd_permission_internal_locked(env, pobj, NULL,
322 MAY_WRITE | MAY_EXEC,
327 if (mdd_is_append(pobj))
334 * pobj == NULL is remote ops case, under such case, pobj's
335 * VTX feature has been checked already, no need check again.
337 static inline int mdd_is_sticky(const struct lu_env *env,
338 struct mdd_object *pobj,
339 struct mdd_object *cobj)
341 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
342 struct lu_ucred *uc = lu_ucred_assert(env);
346 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
350 if (!(tmp_la->la_mode & S_ISVTX) ||
351 (tmp_la->la_uid == uc->uc_fsuid))
355 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
359 if (tmp_la->la_uid == uc->uc_fsuid)
362 return !md_capable(uc, CFS_CAP_FOWNER);
365 static int mdd_may_delete_entry(const struct lu_env *env,
366 struct mdd_object *pobj, int check_perm)
370 LASSERT(pobj != NULL);
371 if (!mdd_object_exists(pobj))
374 if (mdd_is_dead_obj(pobj))
379 rc = mdd_permission_internal_locked(env, pobj, NULL,
380 MAY_WRITE | MAY_EXEC,
386 if (mdd_is_append(pobj))
393 * Check whether it may delete the cobj from the pobj.
396 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
397 struct mdd_object *cobj, struct lu_attr *cattr,
398 struct lu_attr *src_attr, int check_perm, int check_empty)
404 rc = mdd_may_delete_entry(env, pobj, check_perm);
412 if (!mdd_object_exists(cobj))
415 if (mdd_is_dead_obj(cobj))
419 if (mdd_is_sticky(env, pobj, cobj))
422 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
425 if ((cattr->la_valid & LA_FLAGS) &&
426 (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
429 /* additional check the rename case */
431 if (S_ISDIR(src_attr->la_mode)) {
432 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
434 if (!S_ISDIR(cattr->la_mode))
437 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
439 } else if (S_ISDIR(cattr->la_mode))
443 if (S_ISDIR(cattr->la_mode) && check_empty)
444 rc = mdd_dir_is_empty(env, cobj);
451 * has mdd_write_lock on src already, but not on tgt yet
453 int mdd_link_sanity_check(const struct lu_env *env,
454 struct mdd_object *tgt_obj,
455 const struct lu_name *lname,
456 struct mdd_object *src_obj)
458 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
462 if (!mdd_object_exists(src_obj))
465 if (mdd_is_dead_obj(src_obj))
468 /* Local ops, no lookup before link, check filename length here. */
469 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
470 RETURN(-ENAMETOOLONG);
472 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
475 if (S_ISDIR(mdd_object_type(src_obj)))
478 LASSERT(src_obj != tgt_obj);
480 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
485 rc = __mdd_may_link(env, src_obj);
490 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
491 const char *name, struct thandle *handle,
492 struct lustre_capa *capa)
494 struct dt_object *next = mdd_object_child(pobj);
498 if (dt_try_as_dir(env, next)) {
499 rc = next->do_index_ops->dio_delete(env, next,
500 (struct dt_key *)name,
508 static int __mdd_index_insert_only(const struct lu_env *env,
509 struct mdd_object *pobj,
510 const struct lu_fid *lf, const char *name,
511 struct thandle *handle,
512 struct lustre_capa *capa)
514 struct dt_object *next = mdd_object_child(pobj);
518 if (dt_try_as_dir(env, next)) {
519 struct lu_ucred *uc = lu_ucred_check(env);
522 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
523 rc = next->do_index_ops->dio_insert(env, next,
525 (const struct dt_key *)name,
526 handle, capa, ignore_quota);
533 /* insert named index, add reference if isdir */
534 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
535 const struct lu_fid *lf, const char *name, int is_dir,
536 struct thandle *handle, struct lustre_capa *capa)
541 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
542 if (rc == 0 && is_dir) {
543 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
544 mdo_ref_add(env, pobj, handle);
545 mdd_write_unlock(env, pobj);
550 /* delete named index, drop reference if isdir */
551 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
552 const char *name, int is_dir, struct thandle *handle,
553 struct lustre_capa *capa)
558 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
559 if (rc == 0 && is_dir) {
560 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
561 mdo_ref_del(env, pobj, handle);
562 mdd_write_unlock(env, pobj);
568 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
569 int reclen, struct thandle *handle)
573 /* XXX: this is a temporary solution to declare llog changes
574 * will be fixed in 2.3 with new llog implementation */
576 LASSERT(mdd->mdd_capa);
578 /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
579 * have to set the parameter 'size' as INT_MAX or 0 to inform
580 * OSD that this record write is for a llog write or catalog
581 * header update, and osd declare function will reserve less
582 * credits for optimization purpose.
584 * Reserve 6 blocks for a llog write, since the llog file is
585 * usually small, reserve 2 blocks for catalog header update,
586 * because we know for sure that catalog header is already
589 * This hack should be removed in 2.3.
593 rc = dt_declare_record_write(env, mdd->mdd_capa,
594 DECLARE_LLOG_WRITE, 0, handle);
598 /* header will be updated as well */
599 rc = dt_declare_record_write(env, mdd->mdd_capa,
600 DECLARE_LLOG_WRITE, 0, handle);
604 /* also we should be able to create new plain log */
605 rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
609 /* new record referencing new plain llog */
610 rc = dt_declare_record_write(env, mdd->mdd_capa,
611 DECLARE_LLOG_WRITE, 0, handle);
615 /* catalog's header will be updated as well */
616 rc = dt_declare_record_write(env, mdd->mdd_capa,
617 DECLARE_LLOG_REWRITE, 0, handle);
622 int mdd_declare_changelog_store(const struct lu_env *env,
623 struct mdd_device *mdd,
624 const struct lu_name *fname,
625 struct thandle *handle)
627 struct obd_device *obd = mdd2obd_dev(mdd);
628 struct llog_ctxt *ctxt;
629 struct llog_changelog_rec *rec;
635 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
638 reclen = llog_data_len(sizeof(*rec) +
639 (fname != NULL ? fname->ln_namelen : 0));
640 buf = mdd_buf_alloc(env, reclen);
641 if (buf->lb_buf == NULL)
645 rec->cr_hdr.lrh_len = reclen;
646 rec->cr_hdr.lrh_type = CHANGELOG_REC;
648 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
652 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
658 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
659 struct mdd_device *mdd,
660 const struct lu_name *tname,
661 const struct lu_name *sname,
662 struct thandle *handle)
664 struct obd_device *obd = mdd2obd_dev(mdd);
665 struct llog_ctxt *ctxt;
666 struct llog_changelog_ext_rec *rec;
672 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
675 reclen = llog_data_len(sizeof(*rec) +
676 (tname != NULL ? tname->ln_namelen : 0) +
677 (sname != NULL ? 1 + sname->ln_namelen : 0));
678 buf = mdd_buf_alloc(env, reclen);
679 if (buf->lb_buf == NULL)
683 rec->cr_hdr.lrh_len = reclen;
684 rec->cr_hdr.lrh_type = CHANGELOG_REC;
686 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
690 rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
696 /** Add a changelog entry \a rec to the changelog llog
699 * \param handle - currently ignored since llogs start their own transaction;
700 * this will hopefully be fixed in llog rewrite
703 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
704 struct llog_changelog_rec *rec, struct thandle *th)
706 struct obd_device *obd = mdd2obd_dev(mdd);
707 struct llog_ctxt *ctxt;
710 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
711 rec->cr_hdr.lrh_type = CHANGELOG_REC;
712 rec->cr.cr_time = cl_time();
714 spin_lock(&mdd->mdd_cl.mc_lock);
715 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
716 * but as long as the MDD transactions are ordered correctly for e.g.
717 * rename conflicts, I don't think this should matter. */
718 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
719 spin_unlock(&mdd->mdd_cl.mc_lock);
721 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
725 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
732 /** Add a changelog_ext entry \a rec to the changelog llog
735 * \param handle - currently ignored since llogs start their own transaction;
736 * this will hopefully be fixed in llog rewrite
739 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
740 struct llog_changelog_ext_rec *rec,
743 struct obd_device *obd = mdd2obd_dev(mdd);
744 struct llog_ctxt *ctxt;
747 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
748 /* llog_lvfs_write_rec sets the llog tail len */
749 rec->cr_hdr.lrh_type = CHANGELOG_REC;
750 rec->cr.cr_time = cl_time();
752 spin_lock(&mdd->mdd_cl.mc_lock);
753 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
754 * but as long as the MDD transactions are ordered correctly for e.g.
755 * rename conflicts, I don't think this should matter. */
756 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
757 spin_unlock(&mdd->mdd_cl.mc_lock);
759 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
763 /* nested journal transaction */
764 rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
772 /** Store a namespace change changelog record
773 * If this fails, we must fail the whole transaction; we don't
774 * want the change to commit without the log entry.
775 * \param target - mdd_object of change
776 * \param parent - parent dir/object
777 * \param tname - target name string
778 * \param handle - transacion handle
780 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
781 enum changelog_rec_type type, unsigned flags,
782 struct mdd_object *target, struct mdd_object *parent,
783 const struct lu_name *tname, struct thandle *handle)
785 struct llog_changelog_rec *rec;
792 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
794 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
797 LASSERT(target != NULL);
798 LASSERT(parent != NULL);
799 LASSERT(tname != NULL);
800 LASSERT(handle != NULL);
802 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
803 buf = mdd_buf_alloc(env, reclen);
804 if (buf->lb_buf == NULL)
808 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
809 rec->cr.cr_type = (__u32)type;
810 rec->cr.cr_tfid = *mdo2fid(target);
811 rec->cr.cr_pfid = *mdo2fid(parent);
812 rec->cr.cr_namelen = tname->ln_namelen;
813 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
815 target->mod_cltime = cfs_time_current_64();
817 rc = mdd_changelog_store(env, mdd, rec, handle);
819 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
820 rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
821 PFID(&rec->cr.cr_pfid));
829 /** Store a namespace change changelog record
830 * If this fails, we must fail the whole transaction; we don't
831 * want the change to commit without the log entry.
832 * \param target - mdd_object of change
833 * \param tpfid - target parent dir/object fid
834 * \param sfid - source object fid
835 * \param spfid - source parent fid
836 * \param tname - target name string
837 * \param sname - source name string
838 * \param handle - transacion handle
840 static int mdd_changelog_ext_ns_store(const struct lu_env *env,
841 struct mdd_device *mdd,
842 enum changelog_rec_type type,
844 struct mdd_object *target,
845 const struct lu_fid *tpfid,
846 const struct lu_fid *sfid,
847 const struct lu_fid *spfid,
848 const struct lu_name *tname,
849 const struct lu_name *sname,
850 struct thandle *handle)
852 struct llog_changelog_ext_rec *rec;
859 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
861 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
864 LASSERT(sfid != NULL);
865 LASSERT(tpfid != NULL);
866 LASSERT(tname != NULL);
867 LASSERT(handle != NULL);
869 reclen = llog_data_len(sizeof(*rec) +
870 sname != NULL ? 1 + sname->ln_namelen : 0);
871 buf = mdd_buf_alloc(env, reclen);
872 if (buf->lb_buf == NULL)
876 rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
877 rec->cr.cr_type = (__u32)type;
878 rec->cr.cr_pfid = *tpfid;
879 rec->cr.cr_sfid = *sfid;
880 rec->cr.cr_spfid = *spfid;
881 rec->cr.cr_namelen = tname->ln_namelen;
882 memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
884 rec->cr.cr_name[tname->ln_namelen] = '\0';
885 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
887 rec->cr.cr_namelen += 1 + sname->ln_namelen;
890 if (likely(target != NULL)) {
891 rec->cr.cr_tfid = *mdo2fid(target);
892 target->mod_cltime = cfs_time_current_64();
894 fid_zero(&rec->cr.cr_tfid);
897 rc = mdd_changelog_ext_store(env, mdd, rec, handle);
899 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
900 rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
907 static int mdd_declare_link(const struct lu_env *env,
908 struct mdd_device *mdd,
909 struct mdd_object *p,
910 struct mdd_object *c,
911 const struct lu_name *name,
912 struct thandle *handle)
916 rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
920 rc = mdo_declare_ref_add(env, c, handle);
924 rc = mdo_declare_attr_set(env, p, NULL, handle);
928 rc = mdo_declare_attr_set(env, c, NULL, handle);
932 rc = mdd_declare_links_add(env, c, handle);
936 rc = mdd_declare_changelog_store(env, mdd, name, handle);
941 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
942 struct md_object *src_obj, const struct lu_name *lname,
945 const char *name = lname->ln_name;
946 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
947 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
948 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
949 struct mdd_device *mdd = mdo2mdd(src_obj);
950 struct dynlock_handle *dlh;
951 struct thandle *handle;
955 handle = mdd_trans_create(env, mdd);
957 GOTO(out_pending, rc = PTR_ERR(handle));
959 rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
963 rc = mdd_trans_start(env, mdd, handle);
967 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
969 GOTO(out_trans, rc = -ENOMEM);
970 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
972 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
974 GOTO(out_unlock, rc);
976 rc = mdo_ref_add(env, mdd_sobj, handle);
978 GOTO(out_unlock, rc);
981 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
983 mdd_object_capa(env, mdd_tobj));
985 mdo_ref_del(env, mdd_sobj, handle);
986 GOTO(out_unlock, rc);
989 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
990 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
992 la->la_valid = LA_CTIME | LA_MTIME;
993 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
995 GOTO(out_unlock, rc);
997 la->la_valid = LA_CTIME;
998 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1000 mdd_links_add(env, mdd_sobj,
1001 mdo2fid(mdd_tobj), lname, handle, 0);
1006 mdd_write_unlock(env, mdd_sobj);
1007 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1010 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1011 mdd_tobj, lname, handle);
1013 mdd_trans_stop(env, mdd, rc, handle);
1018 int mdd_declare_finish_unlink(const struct lu_env *env,
1019 struct mdd_object *obj,
1021 struct thandle *handle)
1025 rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1029 return mdo_declare_destroy(env, obj, handle);
1032 /* caller should take a lock before calling */
1033 int mdd_finish_unlink(const struct lu_env *env,
1034 struct mdd_object *obj, struct md_attr *ma,
1038 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1041 LASSERT(mdd_write_locked(env, obj) != 0);
1043 if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
1044 obj->mod_flags |= DEAD_OBJ;
1045 /* add new orphan and the object
1046 * will be deleted during mdd_close() */
1047 if (obj->mod_count) {
1048 rc = __mdd_orphan_add(env, obj, th);
1050 CDEBUG(D_HA, "Object "DFID" is inserted into "
1051 "orphan list, open count = %d\n",
1052 PFID(mdd_object_fid(obj)),
1055 CERROR("Object "DFID" fail to be an orphan, "
1056 "open count = %d, maybe cause failed "
1058 PFID(mdd_object_fid(obj)),
1061 rc = mdo_destroy(env, obj, th);
1070 * has mdd_write_lock on cobj already, but not on pobj yet
1072 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1073 struct mdd_object *cobj, struct lu_attr *cattr)
1078 rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1083 static inline int mdd_declare_links_del(const struct lu_env *env,
1084 struct mdd_object *c,
1085 struct thandle *handle)
1089 /* For directory, the linkEA will be removed together with the object. */
1090 if (!S_ISDIR(mdd_object_type(c)))
1091 rc = mdd_declare_links_add(env, c, handle);
1096 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1097 struct mdd_object *p, struct mdd_object *c,
1098 const struct lu_name *name, struct md_attr *ma,
1099 struct thandle *handle)
1101 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1104 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1108 rc = mdo_declare_ref_del(env, p, handle);
1112 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1113 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1114 la->la_valid = LA_CTIME | LA_MTIME;
1115 rc = mdo_declare_attr_set(env, p, la, handle);
1120 rc = mdo_declare_ref_del(env, c, handle);
1124 rc = mdo_declare_ref_del(env, c, handle);
1128 rc = mdo_declare_attr_set(env, c, NULL, handle);
1132 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1136 rc = mdd_declare_links_del(env, c, handle);
1140 /* FIXME: need changelog for remove entry */
1141 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1147 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1148 struct md_object *cobj, const struct lu_name *lname,
1151 const char *name = lname->ln_name;
1152 struct lu_attr *cattr = &mdd_env_info(env)->mti_cattr;
1153 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1154 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1155 struct mdd_object *mdd_cobj = NULL;
1156 struct mdd_device *mdd = mdo2mdd(pobj);
1157 struct dynlock_handle *dlh;
1158 struct thandle *handle;
1162 /* cobj == NULL means only delete name entry */
1163 if (likely(cobj != NULL)) {
1164 mdd_cobj = md2mdd_obj(cobj);
1165 if (mdd_object_exists(mdd_cobj) == 0)
1167 /* currently it is assume, it could only delete
1168 * name entry of remote directory */
1172 handle = mdd_trans_create(env, mdd);
1174 RETURN(PTR_ERR(handle));
1176 rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1181 rc = mdd_trans_start(env, mdd, handle);
1185 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1187 GOTO(stop, rc = -ENOMEM);
1189 if (likely(mdd_cobj != NULL)) {
1190 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1193 rc = mdd_la_get(env, mdd_cobj, cattr,
1194 mdd_object_capa(env, mdd_cobj));
1198 is_dir = S_ISDIR(cattr->la_mode);
1202 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1206 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1207 mdd_object_capa(env, mdd_pobj));
1211 if (likely(mdd_cobj != NULL)) {
1212 rc = mdo_ref_del(env, mdd_cobj, handle);
1214 __mdd_index_insert_only(env, mdd_pobj,
1217 mdd_object_capa(env, mdd_pobj));
1223 mdo_ref_del(env, mdd_cobj, handle);
1225 /* fetch updated nlink */
1226 rc = mdd_la_get(env, mdd_cobj, cattr,
1227 mdd_object_capa(env, mdd_cobj));
1232 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1233 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1235 la->la_valid = LA_CTIME | LA_MTIME;
1236 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1240 /* Enough for only unlink the entry */
1241 if (unlikely(mdd_cobj == NULL)) {
1242 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1246 if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1247 /* update ctime of an unlinked file only if it is still
1248 * opened or a link still exists */
1249 la->la_valid = LA_CTIME;
1250 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1255 /* XXX: this transfer to ma will be removed with LOD/OSP */
1256 ma->ma_attr = *cattr;
1257 ma->ma_valid |= MA_INODE;
1258 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1260 /* fetch updated nlink */
1262 rc = mdd_la_get(env, mdd_cobj, cattr,
1263 mdd_object_capa(env, mdd_cobj));
1266 /* old files may not have link ea; ignore errors */
1267 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1269 /* if object is removed then we can't get its attrs, use last get */
1270 if (cattr->la_nlink == 0) {
1271 ma->ma_attr = *cattr;
1272 ma->ma_valid |= MA_INODE;
1276 mdd_write_unlock(env, mdd_cobj);
1277 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1281 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1282 if ((ma->ma_valid & MA_HSM) &&
1283 (ma->ma_hsm.mh_flags & HS_EXISTS))
1284 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1286 rc = mdd_changelog_ns_store(env, mdd,
1287 is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1288 mdd_cobj, mdd_pobj, lname, handle);
1292 mdd_trans_stop(env, mdd, rc, handle);
1298 * The permission has been checked when obj created, no need check again.
1300 static int mdd_cd_sanity_check(const struct lu_env *env,
1301 struct mdd_object *obj)
1306 if (!obj || mdd_is_dead_obj(obj))
1313 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1314 struct md_object *cobj, const struct md_op_spec *spec,
1317 struct mdd_device *mdd = mdo2mdd(cobj);
1318 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1319 struct mdd_object *son = md2mdd_obj(cobj);
1320 struct thandle *handle;
1321 const struct lu_buf *buf;
1322 struct lu_attr *attr = &mdd_env_info(env)->mti_cattr;
1326 /* do not let users to create stripes via .lustre/
1327 * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1328 if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1331 rc = mdd_cd_sanity_check(env, son);
1335 if (!md_should_create(spec->sp_cr_flags))
1339 * there are following use cases for this function:
1340 * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1341 * striping can be specified or not
1344 rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1348 /* calling ->ah_make_hint() is used to transfer information from parent */
1349 mdd_object_make_hint(env, mdd_pobj, son, attr);
1351 handle = mdd_trans_create(env, mdd);
1353 GOTO(out_free, rc = PTR_ERR(handle));
1356 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1357 * Should this be fixed?
1359 CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1360 spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1361 spec->sp_cr_flags, spec->no_create);
1363 if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1364 /* replay case or lfs setstripe */
1365 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1366 spec->u.sp_ea.eadatalen);
1371 rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1372 XATTR_NAME_LOV, 0, handle);
1376 rc = mdd_trans_start(env, mdd, handle);
1380 rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1381 0, handle, mdd_object_capa(env, son));
1383 mdd_trans_stop(env, mdd, rc, handle);
1388 /* Get fid from name and parent */
1390 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1391 const struct lu_name *lname, struct lu_fid* fid, int mask)
1393 const char *name = lname->ln_name;
1394 const struct dt_key *key = (const struct dt_key *)name;
1395 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1396 struct mdd_device *m = mdo2mdd(pobj);
1397 struct dt_object *dir = mdd_object_child(mdd_obj);
1401 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1404 if (mdd_object_remote(mdd_obj)) {
1405 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1406 mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1407 } else if (!mdd_object_exists(mdd_obj)) {
1411 /* The common filename length check. */
1412 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1413 RETURN(-ENAMETOOLONG);
1415 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1420 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1421 dt_try_as_dir(env, dir))) {
1423 rc = dir->do_index_ops->dio_lookup(env, dir,
1424 (struct dt_rec *)fid, key,
1425 mdd_object_capa(env, mdd_obj));
1436 static int mdd_declare_object_initialize(const struct lu_env *env,
1437 struct mdd_object *parent,
1438 struct mdd_object *child,
1439 struct lu_attr *attr,
1440 struct thandle *handle)
1446 * inode mode has been set in creation time, and it's based on umask,
1447 * la_mode and acl, don't set here again! (which will go wrong
1448 * because below function doesn't consider umask).
1449 * I'd suggest set all object attributes in creation time, see above.
1451 LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1452 attr->la_valid &= ~(LA_MODE | LA_TYPE);
1453 rc = mdo_declare_attr_set(env, child, attr, handle);
1454 attr->la_valid |= LA_MODE | LA_TYPE;
1455 if (rc == 0 && S_ISDIR(attr->la_mode)) {
1456 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1459 rc = mdo_declare_ref_add(env, child, handle);
1461 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1465 if (rc == 0 && (fid_is_norm(mdo2fid(child)) ||
1466 fid_is_dot_lustre(mdo2fid(child)) ||
1467 fid_is_root(mdo2fid(child))))
1468 mdd_declare_links_add(env, child, handle);
1473 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1474 const struct lu_name *lname, struct mdd_object *child,
1475 const struct lu_attr *attr, struct thandle *handle,
1476 const struct md_op_spec *spec)
1482 * Update attributes for child.
1485 * (1) the valid bits should be converted between Lustre and Linux;
1486 * (2) maybe, the child attributes should be set in OSD when creation.
1489 rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1490 /* arguments are supposed to stay the same */
1491 if (S_ISDIR(attr->la_mode)) {
1492 /* Add "." and ".." for newly created dir */
1493 mdo_ref_add(env, child, handle);
1494 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1495 dot, handle, BYPASS_CAPA);
1497 rc = __mdd_index_insert_only(env, child, pfid,
1501 mdo_ref_del(env, child, handle);
1504 if (rc == 0 && (fid_is_norm(mdo2fid(child)) ||
1505 fid_is_dot_lustre(mdo2fid(child)) ||
1506 fid_is_root(mdo2fid(child))))
1507 mdd_links_add(env, child, pfid, lname, handle, 1);
1512 /* has not lock on pobj yet */
1513 static int mdd_create_sanity_check(const struct lu_env *env,
1514 struct md_object *pobj,
1515 struct lu_attr *pattr,
1516 const struct lu_name *lname,
1517 struct lu_attr *cattr,
1518 struct md_op_spec *spec)
1520 struct mdd_thread_info *info = mdd_env_info(env);
1521 struct lu_fid *fid = &info->mti_fid;
1522 struct mdd_object *obj = md2mdd_obj(pobj);
1523 struct mdd_device *m = mdo2mdd(pobj);
1528 if (mdd_is_dead_obj(obj))
1532 * In some cases this lookup is not needed - we know before if name
1533 * exists or not because MDT performs lookup for it.
1534 * name length check is done in lookup.
1536 if (spec->sp_cr_lookup) {
1538 * Check if the name already exist, though it will be checked in
1539 * _index_insert also, for avoiding rolling back if exists
1542 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1543 MAY_WRITE | MAY_EXEC);
1545 RETURN(rc ? : -EEXIST);
1548 * Check WRITE permission for the parent.
1549 * EXEC permission have been checked
1550 * when lookup before create already.
1552 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1559 if (pattr->la_mode & S_ISGID) {
1560 cattr->la_gid = pattr->la_gid;
1561 if (S_ISDIR(cattr->la_mode)) {
1562 cattr->la_mode |= S_ISGID;
1563 cattr->la_valid |= LA_MODE;
1567 switch (cattr->la_mode & S_IFMT) {
1569 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1571 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1572 RETURN(-ENAMETOOLONG);
1591 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1592 struct mdd_object *p, struct mdd_object *c,
1593 const struct lu_name *name,
1594 struct lu_attr *attr,
1596 struct thandle *handle,
1597 const struct md_op_spec *spec)
1601 rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1605 #ifdef CONFIG_FS_POSIX_ACL
1606 if (got_def_acl > 0) {
1607 struct lu_buf *acl_buf;
1609 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1610 /* if dir, then can inherit default ACl */
1611 if (S_ISDIR(attr->la_mode)) {
1612 rc = mdo_declare_xattr_set(env, c, acl_buf,
1613 XATTR_NAME_ACL_DEFAULT,
1619 rc = mdo_declare_attr_set(env, c, attr, handle);
1623 rc = mdo_declare_xattr_set(env, c, acl_buf,
1624 XATTR_NAME_ACL_ACCESS, 0, handle);
1630 if (S_ISDIR(attr->la_mode)) {
1631 rc = mdo_declare_ref_add(env, p, handle);
1636 rc = mdd_declare_object_initialize(env, p, c, attr, handle);
1640 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1641 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1643 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1644 name->ln_name, handle);
1648 /* replay case, create LOV EA from client data */
1649 if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1650 const struct lu_buf *buf;
1652 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1653 spec->u.sp_ea.eadatalen);
1654 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1660 if (S_ISLNK(attr->la_mode)) {
1661 rc = dt_declare_record_write(env, mdd_object_child(c),
1662 strlen(spec->u.sp_symname), 0,
1668 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1669 rc = mdo_declare_attr_set(env, p, attr, handle);
1674 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1682 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1683 struct lu_attr *la, struct lu_buf *acl_buf,
1684 int *got_def_acl, int *reset_acl)
1689 if (S_ISLNK(la->la_mode))
1692 mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1693 rc = mdo_xattr_get(env, pobj, acl_buf,
1694 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1695 mdd_read_unlock(env, pobj);
1697 /* If there are default ACL, fix mode by default ACL */
1699 acl_buf->lb_len = rc;
1700 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1704 } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1705 /* If there are no default ACL, fix mode by mask */
1706 struct lu_ucred *uc = lu_ucred(env);
1707 la->la_mode &= ~uc->uc_umask;
1715 * Create object and insert it into namespace.
1717 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1718 const struct lu_name *lname, struct md_object *child,
1719 struct md_op_spec *spec, struct md_attr* ma)
1721 struct mdd_thread_info *info = mdd_env_info(env);
1722 struct lu_attr *la = &info->mti_la_for_fix;
1723 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1724 struct mdd_object *son = md2mdd_obj(child);
1725 struct mdd_device *mdd = mdo2mdd(pobj);
1726 struct lu_attr *attr = &ma->ma_attr;
1727 struct thandle *handle;
1728 struct lu_attr *pattr = &info->mti_pattr;
1729 struct lu_buf acl_buf;
1730 struct dynlock_handle *dlh;
1731 const char *name = lname->ln_name;
1732 int rc, created = 0, initialized = 0, inserted = 0;
1733 int got_def_acl = 0;
1738 * Two operations have to be performed:
1740 * - an allocation of a new object (->do_create()), and
1742 * - an insertion into a parent index (->dio_insert()).
1744 * Due to locking, operation order is not important, when both are
1745 * successful, *but* error handling cases are quite different:
1747 * - if insertion is done first, and following object creation fails,
1748 * insertion has to be rolled back, but this operation might fail
1749 * also leaving us with dangling index entry.
1751 * - if creation is done first, is has to be undone if insertion
1752 * fails, leaving us with leaked space, which is neither good, nor
1755 * It seems that creation-first is simplest solution, but it is
1756 * sub-optimal in the frequent
1761 * case, because second mkdir is bound to create object, only to
1762 * destroy it immediately.
1764 * To avoid this follow local file systems that do double lookup:
1766 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1768 * 1. create (mdd_object_create_internal())
1770 * 2. insert (__mdd_index_insert(), lookup again)
1773 rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1777 /* Sanity checks before big job. */
1778 rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1782 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1783 GOTO(out_free, rc = -EINPROGRESS);
1785 acl_buf.lb_buf = info->mti_xattr_buf;
1786 acl_buf.lb_len = sizeof(info->mti_xattr_buf);
1787 rc = mdd_acl_init(env, mdd_pobj, attr, &acl_buf, &got_def_acl,
1792 mdd_object_make_hint(env, mdd_pobj, son, attr);
1794 handle = mdd_trans_create(env, mdd);
1796 GOTO(out_free, rc = PTR_ERR(handle));
1798 rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1799 got_def_acl, handle, spec);
1803 rc = mdd_trans_start(env, mdd, handle);
1807 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1809 GOTO(out_trans, rc = -ENOMEM);
1811 mdd_write_lock(env, son, MOR_TGT_CHILD);
1812 rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
1814 mdd_write_unlock(env, son);
1820 #ifdef CONFIG_FS_POSIX_ACL
1822 /* set default acl */
1823 if (S_ISDIR(attr->la_mode)) {
1824 LASSERTF(acl_buf.lb_len == got_def_acl,
1825 "invalid acl_buf: %p:%d got_def %d\n",
1826 acl_buf.lb_buf, (int)acl_buf.lb_len,
1828 rc = mdo_xattr_set(env, son, &acl_buf,
1829 XATTR_NAME_ACL_DEFAULT, 0,
1830 handle, BYPASS_CAPA);
1832 mdd_write_unlock(env, son);
1837 /* set its own acl */
1839 LASSERTF(acl_buf.lb_buf != NULL && acl_buf.lb_len != 0,
1840 "invalid acl_buf %p:%d\n", acl_buf.lb_buf,
1841 (int)acl_buf.lb_len);
1842 rc = mdo_xattr_set(env, son, &acl_buf,
1843 XATTR_NAME_ACL_ACCESS,
1844 0, handle, BYPASS_CAPA);
1846 mdd_write_unlock(env, son);
1853 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1854 son, attr, handle, spec);
1857 * in case of replay we just set LOVEA provided by the client
1858 * XXX: I think it would be interesting to try "old" way where
1859 * MDT calls this xattr_set(LOV) in a different transaction.
1860 * probably this way we code can be made better.
1862 if (rc == 0 && (spec->no_create ||
1863 (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1864 const struct lu_buf *buf;
1866 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1867 spec->u.sp_ea.eadatalen);
1868 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1872 if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1873 rc = __mdd_orphan_add(env, son, handle);
1875 mdd_write_unlock(env, son);
1879 * Object has no links, so it will be destroyed when last
1880 * reference is released. (XXX not now.)
1886 if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
1887 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1888 name, S_ISDIR(attr->la_mode), handle,
1889 mdd_object_capa(env, mdd_pobj));
1896 if (S_ISLNK(attr->la_mode)) {
1897 struct lu_ucred *uc = lu_ucred_assert(env);
1898 struct dt_object *dt = mdd_object_child(son);
1899 const char *target_name = spec->u.sp_symname;
1900 int sym_len = strlen(target_name);
1901 const struct lu_buf *buf;
1904 buf = mdd_buf_get_const(env, target_name, sym_len);
1905 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1906 mdd_object_capa(env, son),
1908 CFS_CAP_SYS_RESOURCE_MASK);
1913 GOTO(cleanup, rc = -EFAULT);
1916 /* volatile file creation does not update parent directory times */
1917 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1918 GOTO(cleanup, rc = 0);
1920 /* update parent directory mtime/ctime */
1922 la->la_valid = LA_CTIME | LA_MTIME;
1923 rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1929 if (rc != 0 && created != 0) {
1932 if (inserted != 0) {
1933 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1934 rc2 = __mdd_orphan_del(env, son, handle);
1936 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1937 S_ISDIR(attr->la_mode),
1938 handle, BYPASS_CAPA);
1943 mdd_write_lock(env, son, MOR_TGT_CHILD);
1944 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1945 /* Drop the reference, no need to delete "."/"..",
1946 * because the object to be destroied directly. */
1947 rc2 = mdo_ref_del(env, son, handle);
1949 mdd_write_unlock(env, son);
1954 rc2 = mdo_ref_del(env, son, handle);
1956 mdd_write_unlock(env, son);
1960 mdo_destroy(env, son, handle);
1961 mdd_write_unlock(env, son);
1964 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1966 if (rc == 0 && fid_is_client_mdt_visible(mdo2fid(son)))
1967 rc = mdd_changelog_ns_store(env, mdd,
1968 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1969 S_ISREG(attr->la_mode) ? CL_CREATE :
1970 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1971 0, son, mdd_pobj, lname, handle);
1973 mdd_trans_stop(env, mdd, rc, handle);
1975 /* The child object shouldn't be cached anymore */
1977 set_bit(LU_OBJECT_HEARD_BANSHEE,
1978 &child->mo_lu.lo_header->loh_flags);
1983 * Get locks on parents in proper order
1984 * RETURN: < 0 - error, rename_order if successful
1992 static int mdd_rename_order(const struct lu_env *env,
1993 struct mdd_device *mdd,
1994 struct mdd_object *src_pobj,
1995 struct mdd_object *tgt_pobj)
1997 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2001 if (src_pobj == tgt_pobj)
2002 RETURN(MDD_RN_SAME);
2004 /* compared the parent child relationship of src_p&tgt_p */
2005 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2007 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2010 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2023 /* has not mdd_write{read}_lock on any obj yet. */
2024 static int mdd_rename_sanity_check(const struct lu_env *env,
2025 struct mdd_object *src_pobj,
2026 struct mdd_object *tgt_pobj,
2027 struct mdd_object *sobj,
2028 struct mdd_object *tobj,
2029 struct lu_attr *so_attr,
2030 struct lu_attr *tg_attr)
2035 /* XXX: when get here, sobj must NOT be NULL,
2036 * the other case has been processed in cml_rename
2037 * before mdd_rename and enable MDS_PERM_BYPASS. */
2040 rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2044 /* XXX: when get here, "tobj == NULL" means tobj must
2045 * NOT exist (neither on remote MDS, such case has been
2046 * processed in cml_rename before mdd_rename and enable
2048 * So check may_create, but not check may_unlink. */
2050 rc = mdd_may_create(env, tgt_pobj, NULL,
2051 (src_pobj != tgt_pobj), 0);
2053 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2054 (src_pobj != tgt_pobj), 1);
2056 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2057 S_ISDIR(so_attr->la_mode))
2058 rc = __mdd_may_link(env, tgt_pobj);
2063 static int mdd_declare_rename(const struct lu_env *env,
2064 struct mdd_device *mdd,
2065 struct mdd_object *mdd_spobj,
2066 struct mdd_object *mdd_tpobj,
2067 struct mdd_object *mdd_sobj,
2068 struct mdd_object *mdd_tobj,
2069 const struct lu_name *tname,
2070 const struct lu_name *sname,
2072 struct thandle *handle)
2080 /* name from source dir */
2081 rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2085 /* .. from source child */
2086 if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2087 /* source child can be directory,
2088 * counted by source dir's nlink */
2089 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2093 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
2097 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
2102 /* new target child can be directory,
2103 * counted by target dir's nlink */
2104 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2110 rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
2114 rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2117 mdd_declare_links_add(env, mdd_sobj, handle);
2121 rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2126 rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2127 tname->ln_name, handle);
2131 /* name from target dir (old name), we declare it unconditionally
2132 * as mdd_rename() calls delete unconditionally as well. so just
2133 * to balance declarations vs calls to change ... */
2134 rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2138 if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2139 /* delete target child in target parent directory */
2140 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2144 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2145 /* target child can be directory,
2146 * delete "." reference in target child directory */
2147 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2151 /* delete ".." reference in target parent directory */
2152 rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2157 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2161 mdd_declare_links_add(env, mdd_tobj, handle);
2165 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2170 rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2177 /* src object can be remote that is why we use only fid and type of object */
2178 static int mdd_rename(const struct lu_env *env,
2179 struct md_object *src_pobj, struct md_object *tgt_pobj,
2180 const struct lu_fid *lf, const struct lu_name *lsname,
2181 struct md_object *tobj, const struct lu_name *ltname,
2184 const char *sname = lsname->ln_name;
2185 const char *tname = ltname->ln_name;
2186 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
2187 struct lu_attr *so_attr = &mdd_env_info(env)->mti_cattr;
2188 struct lu_attr *tg_attr = &mdd_env_info(env)->mti_pattr;
2189 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2190 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2191 struct mdd_device *mdd = mdo2mdd(src_pobj);
2192 struct mdd_object *mdd_sobj = NULL; /* source object */
2193 struct mdd_object *mdd_tobj = NULL;
2194 struct dynlock_handle *sdlh, *tdlh;
2195 struct thandle *handle;
2196 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2197 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2200 bool tobj_locked = 0;
2201 unsigned cl_flags = 0;
2206 mdd_tobj = md2mdd_obj(tobj);
2208 mdd_sobj = mdd_object_find(env, mdd, lf);
2210 handle = mdd_trans_create(env, mdd);
2212 GOTO(out_pending, rc = PTR_ERR(handle));
2214 rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2215 mdd_tobj, lsname, ltname, ma, handle);
2219 rc = mdd_trans_start(env, mdd, handle);
2223 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2224 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2226 GOTO(cleanup_unlocked, rc);
2228 /* Get locks in determined order */
2229 if (rc == MDD_RN_SAME) {
2230 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2231 sname, MOR_SRC_PARENT);
2232 /* check hashes to determine do we need one lock or two */
2233 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2234 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2238 } else if (rc == MDD_RN_SRCTGT) {
2239 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2240 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2242 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2243 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2245 if (sdlh == NULL || tdlh == NULL)
2246 GOTO(cleanup, rc = -ENOMEM);
2248 rc = mdd_la_get(env, mdd_sobj, so_attr,
2249 mdd_object_capa(env, mdd_sobj));
2254 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2255 mdd_object_capa(env, mdd_tobj));
2260 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2261 mdd_tobj, so_attr, tg_attr);
2265 is_dir = S_ISDIR(so_attr->la_mode);
2267 /* Remove source name from source directory */
2268 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2269 mdd_object_capa(env, mdd_spobj));
2273 /* "mv dir1 dir2" needs "dir1/.." link update */
2274 if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2275 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2276 mdd_object_capa(env, mdd_sobj));
2278 GOTO(fixup_spobj2, rc);
2280 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2281 handle, mdd_object_capa(env, mdd_sobj));
2283 GOTO(fixup_spobj, rc);
2286 /* Remove target name from target directory
2287 * Here tobj can be remote one, so we do index_delete unconditionally
2288 * and -ENOENT is allowed.
2290 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2291 mdd_object_capa(env, mdd_tpobj));
2294 /* tname might been renamed to something else */
2295 GOTO(fixup_spobj, rc);
2298 GOTO(fixup_spobj, rc);
2301 /* Insert new fid with target name into target dir */
2302 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2303 mdd_object_capa(env, mdd_tpobj));
2305 GOTO(fixup_tpobj, rc);
2307 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2308 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2310 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2312 la->la_valid = LA_CTIME;
2313 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2315 GOTO(fixup_tpobj, rc);
2318 /* Remove old target object
2319 * For tobj is remote case cmm layer has processed
2320 * and set tobj to NULL then. So when tobj is NOT NULL,
2321 * it must be local one.
2323 if (tobj && mdd_object_exists(mdd_tobj)) {
2324 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2326 if (mdd_is_dead_obj(mdd_tobj)) {
2327 /* shld not be dead, something is wrong */
2328 CERROR("tobj is dead, something is wrong\n");
2332 mdo_ref_del(env, mdd_tobj, handle);
2334 /* Remove dot reference. */
2335 if (S_ISDIR(tg_attr->la_mode))
2336 mdo_ref_del(env, mdd_tobj, handle);
2339 /* fetch updated nlink */
2340 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2341 mdd_object_capa(env, mdd_tobj));
2343 CERROR("%s: Failed to get nlink for tobj "
2345 mdd2obd_dev(mdd)->obd_name,
2346 PFID(tpobj_fid), rc);
2347 GOTO(fixup_tpobj, rc);
2350 la->la_valid = LA_CTIME;
2351 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2353 CERROR("%s: Failed to set ctime for tobj "
2355 mdd2obd_dev(mdd)->obd_name,
2356 PFID(tpobj_fid), rc);
2357 GOTO(fixup_tpobj, rc);
2360 /* XXX: this transfer to ma will be removed with LOD/OSP */
2361 ma->ma_attr = *tg_attr;
2362 ma->ma_valid |= MA_INODE;
2363 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2365 CERROR("%s: Failed to unlink tobj "
2367 mdd2obd_dev(mdd)->obd_name,
2368 PFID(tpobj_fid), rc);
2369 GOTO(fixup_tpobj, rc);
2372 /* fetch updated nlink */
2373 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2374 mdd_object_capa(env, mdd_tobj));
2376 CERROR("%s: Failed to get nlink for tobj "
2378 mdd2obd_dev(mdd)->obd_name,
2379 PFID(tpobj_fid), rc);
2380 GOTO(fixup_tpobj, rc);
2382 /* XXX: this transfer to ma will be removed with LOD/OSP */
2383 ma->ma_attr = *tg_attr;
2384 ma->ma_valid |= MA_INODE;
2386 if (so_attr->la_nlink == 0)
2387 cl_flags |= CLF_RENAME_LAST;
2390 la->la_valid = LA_CTIME | LA_MTIME;
2391 rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2393 GOTO(fixup_tpobj, rc);
2395 if (mdd_spobj != mdd_tpobj) {
2396 la->la_valid = LA_CTIME | LA_MTIME;
2397 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2401 if (rc == 0 && mdd_sobj) {
2402 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2403 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2404 mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
2406 /* Old files might not have EA entry */
2407 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2409 mdd_write_unlock(env, mdd_sobj);
2410 /* We don't fail the transaction if the link ea can't be
2411 updated -- fid2path will use alternate lookup method. */
2419 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2422 CWARN("tp obj fix error %d\n",rc2);
2424 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2425 !mdd_is_dead_obj(mdd_tobj)) {
2427 mdo_ref_add(env, mdd_tobj, handle);
2429 mdo_ref_add(env, mdd_tobj, handle);
2432 rc2 = __mdd_index_insert(env, mdd_tpobj,
2433 mdo2fid(mdd_tobj), tname,
2438 CWARN("tp obj fix error %d\n",rc2);
2443 if (rc && is_dir && mdd_sobj) {
2444 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2448 CWARN("sp obj dotdot delete error %d\n",rc2);
2451 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2452 dotdot, handle, BYPASS_CAPA);
2454 CWARN("sp obj dotdot insert error %d\n",rc2);
2459 rc2 = __mdd_index_insert(env, mdd_spobj,
2460 lf, sname, is_dir, handle, BYPASS_CAPA);
2462 CWARN("sp obj fix error %d\n",rc2);
2466 mdd_write_unlock(env, mdd_tobj);
2467 if (likely(tdlh) && sdlh != tdlh)
2468 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2470 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2473 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2474 mdd_tobj, tpobj_fid, lf,
2475 spobj_fid, ltname, lsname,
2479 mdd_trans_stop(env, mdd, rc, handle);
2481 mdd_object_put(env, mdd_sobj);
2485 int mdd_links_new(const struct lu_env *env, struct mdd_link_data *ldata)
2487 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2488 if (ldata->ml_buf->lb_buf == NULL)
2490 ldata->ml_leh = ldata->ml_buf->lb_buf;
2491 ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
2492 ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
2493 ldata->ml_leh->leh_reccount = 0;
2497 /** Read the link EA into a temp buffer.
2498 * Uses the mdd_thread_info::mti_big_buf since it is generally large.
2499 * A pointer to the buffer is stored in \a ldata::ml_buf.
2501 * \retval 0 or error
2503 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
2504 struct mdd_link_data *ldata)
2506 struct lustre_capa *capa;
2507 struct link_ea_header *leh;
2510 /* First try a small buf */
2511 LASSERT(env != NULL);
2512 ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2513 if (ldata->ml_buf->lb_buf == NULL)
2516 if (!mdd_object_exists(mdd_obj))
2519 capa = mdd_object_capa(env, mdd_obj);
2520 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2521 XATTR_NAME_LINK, capa);
2522 if (rc == -ERANGE) {
2523 /* Buf was too small, figure out what we need. */
2524 mdd_buf_put(ldata->ml_buf);
2525 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2526 XATTR_NAME_LINK, capa);
2529 ldata->ml_buf = mdd_buf_alloc(env, rc);
2530 if (ldata->ml_buf->lb_buf == NULL)
2532 rc = mdo_xattr_get(env, mdd_obj, &LU_BUF_NULL,
2533 XATTR_NAME_LINK, capa);
2538 leh = ldata->ml_buf->lb_buf;
2539 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2540 leh->leh_magic = LINK_EA_MAGIC;
2541 leh->leh_reccount = __swab32(leh->leh_reccount);
2542 leh->leh_len = __swab64(leh->leh_len);
2543 /* entries are swabbed by mdd_lee_unpack */
2545 if (leh->leh_magic != LINK_EA_MAGIC)
2547 if (leh->leh_reccount == 0)
2550 ldata->ml_leh = leh;
2554 /** Read the link EA into a temp buffer.
2555 * Uses the name_buf since it is generally large.
2556 * \retval IS_ERR err
2557 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2559 struct lu_buf *mdd_links_get(const struct lu_env *env,
2560 struct mdd_object *mdd_obj)
2562 struct mdd_link_data ldata = { 0 };
2565 rc = mdd_links_read(env, mdd_obj, &ldata);
2566 return rc ? ERR_PTR(rc) : ldata.ml_buf;
2569 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
2570 struct mdd_link_data *ldata, struct thandle *handle)
2572 const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
2573 ldata->ml_leh->leh_len);
2574 return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
2575 mdd_object_capa(env, mdd_obj));
2578 /** Pack a link_ea_entry.
2579 * All elements are stored as chars to avoid alignment issues.
2580 * Numbers are always big-endian
2581 * \retval record length
2583 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2584 const struct lu_fid *pfid)
2586 struct lu_fid tmpfid;
2589 fid_cpu_to_be(&tmpfid, pfid);
2590 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_CRASH))
2592 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2593 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2594 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2596 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2597 lee->lee_reclen[1] = reclen & 0xff;
2601 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2602 struct lu_name *lname, struct lu_fid *pfid)
2604 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2605 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2606 fid_be_to_cpu(pfid, pfid);
2607 lname->ln_name = lee->lee_name;
2608 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2611 int mdd_declare_links_add(const struct lu_env *env,
2612 struct mdd_object *mdd_obj,
2613 struct thandle *handle)
2617 /* XXX: max size? */
2618 rc = mdo_declare_xattr_set(env, mdd_obj,
2619 mdd_buf_get_const(env, NULL, 4096),
2620 XATTR_NAME_LINK, 0, handle);
2625 /** Add a record to the end of link ea buf */
2626 int mdd_links_add_buf(const struct lu_env *env, struct mdd_link_data *ldata,
2627 const struct lu_name *lname, const struct lu_fid *pfid)
2629 LASSERT(ldata->ml_leh != NULL);
2631 if (lname == NULL || pfid == NULL)
2634 ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2635 if (ldata->ml_leh->leh_len + ldata->ml_reclen >
2636 ldata->ml_buf->lb_len) {
2637 if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
2638 ldata->ml_reclen) < 0)
2642 ldata->ml_leh = ldata->ml_buf->lb_buf;
2643 ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
2644 ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
2645 ldata->ml_leh->leh_len += ldata->ml_reclen;
2646 ldata->ml_leh->leh_reccount++;
2647 CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
2648 lname->ln_namelen, lname->ln_name);
2652 /** Del the current record from the link ea buf */
2653 void mdd_links_del_buf(const struct lu_env *env, struct mdd_link_data *ldata,
2654 const struct lu_name *lname)
2656 LASSERT(ldata->ml_leh != NULL);
2658 ldata->ml_leh->leh_reccount--;
2659 ldata->ml_leh->leh_len -= ldata->ml_reclen;
2660 memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
2661 (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
2662 (char *)ldata->ml_lee);
2663 CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
2664 lname->ln_namelen, lname->ln_name);
2669 * Check if such a link exists in linkEA.
2671 * \param mdd_obj object being handled
2672 * \param pfid parent fid the link to be found for
2673 * \param lname name in the parent's directory entry pointing to this object
2674 * \param ldata link data the search to be done on
2677 * \retval -ENOENT link does not exist
2678 * \retval -ve on error
2680 int mdd_links_find(const struct lu_env *env, struct mdd_object *mdd_obj,
2681 struct mdd_link_data *ldata, const struct lu_name *lname,
2682 const struct lu_fid *pfid)
2684 struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
2685 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2688 LASSERT(ldata->ml_leh != NULL);
2691 ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
2693 for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
2694 mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
2696 if (tmpname->ln_namelen == lname->ln_namelen &&
2697 lu_fid_eq(tmpfid, pfid) &&
2698 (strncmp(tmpname->ln_name, lname->ln_name,
2699 tmpname->ln_namelen) == 0))
2701 ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
2705 if (count == ldata->ml_leh->leh_reccount) {
2706 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2707 lname->ln_namelen, lname->ln_name);
2713 static int __mdd_links_add(const struct lu_env *env,
2714 struct mdd_object *mdd_obj,
2715 struct mdd_link_data *ldata,
2716 const struct lu_name *lname,
2717 const struct lu_fid *pfid,
2718 int first, int check)
2722 if (ldata->ml_leh == NULL) {
2723 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
2727 rc = mdd_links_new(env, ldata);
2734 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2735 if (rc && rc != -ENOENT)
2741 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
2742 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
2746 mdd_links_add_buf(env, ldata, lname, tfid);
2749 return mdd_links_add_buf(env, ldata, lname, pfid);
2752 static int __mdd_links_del(const struct lu_env *env,
2753 struct mdd_object *mdd_obj,
2754 struct mdd_link_data *ldata,
2755 const struct lu_name *lname,
2756 const struct lu_fid *pfid)
2760 if (ldata->ml_leh == NULL) {
2761 rc = mdd_links_read(env, mdd_obj, ldata);
2766 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2770 mdd_links_del_buf(env, ldata, lname);
2774 int mdd_links_rename(const struct lu_env *env,
2775 struct mdd_object *mdd_obj,
2776 const struct lu_fid *oldpfid,
2777 const struct lu_name *oldlname,
2778 const struct lu_fid *newpfid,
2779 const struct lu_name *newlname,
2780 struct thandle *handle,
2781 int first, int check)
2783 struct mdd_link_data ldata = { 0 };
2789 if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
2792 LASSERT(oldpfid != NULL || newpfid != NULL);
2794 if (mdd_obj->mod_flags & DEAD_OBJ)
2795 /* No more links, don't bother */
2798 if (oldpfid != NULL) {
2799 rc = __mdd_links_del(env, mdd_obj, &ldata,
2803 (rc != -ENODATA && rc != -ENOENT))
2805 /* No changes done. */
2812 /* If renaming, add the new record */
2813 if (newpfid != NULL) {
2814 /* even if the add fails, we still delete the out-of-date
2816 rc2 = __mdd_links_add(env, mdd_obj, &ldata,
2817 newlname, newpfid, first, check);
2825 rc = mdd_links_write(env, mdd_obj, &ldata, handle);
2832 if (rc == -EOVERFLOW || rc == - ENOENT)
2834 if (oldpfid == NULL)
2835 CDEBUG(error ? D_ERROR : D_OTHER,
2836 "link_ea add '%.*s' failed %d "DFID"\n",
2837 newlname->ln_namelen, newlname->ln_name,
2838 rc, PFID(mdd_object_fid(mdd_obj)));
2839 else if (newpfid == NULL)
2840 CDEBUG(error ? D_ERROR : D_OTHER,
2841 "link_ea del '%.*s' failed %d "DFID"\n",
2842 oldlname->ln_namelen, oldlname->ln_name,
2843 rc, PFID(mdd_object_fid(mdd_obj)));
2845 CDEBUG(error ? D_ERROR : D_OTHER,
2846 "link_ea rename '%.*s'->'%.*s' failed %d "
2848 oldlname->ln_namelen, oldlname->ln_name,
2849 newlname->ln_namelen, newlname->ln_name,
2850 rc, PFID(mdd_object_fid(mdd_obj)));
2853 if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
2854 /* if we vmalloced a large buffer drop it */
2855 mdd_buf_put(ldata.ml_buf);
2860 static inline int mdd_links_add(const struct lu_env *env,
2861 struct mdd_object *mdd_obj,
2862 const struct lu_fid *pfid,
2863 const struct lu_name *lname,
2864 struct thandle *handle, int first)
2866 return mdd_links_rename(env, mdd_obj, NULL, NULL,
2867 pfid, lname, handle, first, 0);
2870 static inline int mdd_links_del(const struct lu_env *env,
2871 struct mdd_object *mdd_obj,
2872 const struct lu_fid *pfid,
2873 const struct lu_name *lname,
2874 struct thandle *handle)
2876 return mdd_links_rename(env, mdd_obj, pfid, lname,
2877 NULL, NULL, handle, 0, 0);
2880 const struct md_dir_operations mdd_dir_ops = {
2881 .mdo_is_subdir = mdd_is_subdir,
2882 .mdo_lookup = mdd_lookup,
2883 .mdo_create = mdd_create,
2884 .mdo_rename = mdd_rename,
2885 .mdo_link = mdd_link,
2886 .mdo_unlink = mdd_unlink,
2887 .mdo_create_data = mdd_create_data,