1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_fid.h>
61 #include "mdd_internal.h"
63 static const char dot[] = ".";
64 static const char dotdot[] = "..";
66 static struct lu_name lname_dotdot = {
71 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
72 const struct lu_name *lname, struct lu_fid* fid,
74 static int mdd_links_add(const struct lu_env *env,
75 struct mdd_object *mdd_obj,
76 const struct lu_fid *pfid,
77 const struct lu_name *lname,
78 struct thandle *handle);
79 static int mdd_links_rename(const struct lu_env *env,
80 struct mdd_object *mdd_obj,
81 const struct lu_fid *oldpfid,
82 const struct lu_name *oldlname,
83 const struct lu_fid *newpfid,
84 const struct lu_name *newlname,
85 struct thandle *handle);
88 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
89 const struct lu_name *lname, struct lu_fid* fid, int mask)
91 const char *name = lname->ln_name;
92 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
93 struct dynlock_handle *dlh;
96 dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
97 if (unlikely(dlh == NULL))
99 rc = __mdd_lookup(env, pobj, lname, fid, mask);
100 mdd_pdo_read_unlock(env, mdd_obj, dlh);
105 int mdd_lookup(const struct lu_env *env,
106 struct md_object *pobj, const struct lu_name *lname,
107 struct lu_fid* fid, struct md_op_spec *spec)
111 rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
115 static int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
118 return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
122 * For root fid use special function, which does not compare version component
123 * of fid. Version component is different for root fids on all MDTs.
125 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
127 return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
128 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
132 * return 1: if lf is the fid of the ancestor of p1;
135 * return -EREMOTE: if remote object is found, in this
136 * case fid of remote object is saved to @pf;
138 * otherwise: values < 0, errors.
140 static int mdd_is_parent(const struct lu_env *env,
141 struct mdd_device *mdd,
142 struct mdd_object *p1,
143 const struct lu_fid *lf,
146 struct mdd_object *parent = NULL;
151 LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
152 pfid = &mdd_env_info(env)->mti_fid;
154 /* Check for root first. */
155 if (mdd_is_root(mdd, mdo2fid(p1)))
159 /* this is done recursively, bypass capa for each obj */
160 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
161 rc = mdd_parent_fid(env, p1, pfid);
164 if (mdd_is_root(mdd, pfid))
166 if (lu_fid_eq(pfid, lf))
169 mdd_object_put(env, parent);
170 parent = mdd_object_find(env, mdd, pfid);
172 /* cross-ref parent */
173 if (parent == NULL) {
176 GOTO(out, rc = -EREMOTE);
177 } else if (IS_ERR(parent))
178 GOTO(out, rc = PTR_ERR(parent));
183 if (parent && !IS_ERR(parent))
184 mdd_object_put(env, parent);
189 * No permission check is needed.
191 * returns 1: if fid is ancestor of @mo;
192 * returns 0: if fid is not a ancestor of @mo;
194 * returns EREMOTE if remote object is found, fid of remote object is saved to
197 * returns < 0: if error
199 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
200 const struct lu_fid *fid, struct lu_fid *sfid)
202 struct mdd_device *mdd = mdo2mdd(mo);
206 if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
209 rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
213 } else if (rc == 1) {
214 /* found @fid is parent */
222 * Check that @dir contains no entries except (possibly) dot and dotdot.
227 * -ENOTDIR not a directory object
228 * -ENOTEMPTY not empty
232 static int mdd_dir_is_empty(const struct lu_env *env,
233 struct mdd_object *dir)
236 struct dt_object *obj;
237 const struct dt_it_ops *iops;
241 obj = mdd_object_child(dir);
242 if (!dt_try_as_dir(env, obj))
245 iops = &obj->do_index_ops->dio_it;
246 it = iops->init(env, obj, BYPASS_CAPA);
248 result = iops->get(env, it, (const void *)"");
251 for (result = 0, i = 0; result == 0 && i < 3; ++i)
252 result = iops->next(env, it);
255 else if (result == +1)
257 } else if (result == 0)
259 * Huh? Index contains no zero key?
270 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
272 struct mdd_device *m = mdd_obj2mdd_dev(obj);
273 struct lu_attr *la = &mdd_env_info(env)->mti_la;
277 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
282 * Subdir count limitation can be broken through.
284 if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
285 !S_ISDIR(la->la_mode))
292 * Check whether it may create the cobj under the pobj.
295 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
296 struct mdd_object *cobj, int check_perm, int check_nlink)
301 if (cobj && mdd_object_exists(cobj))
304 if (mdd_is_dead_obj(pobj))
308 rc = mdd_permission_internal_locked(env, pobj, NULL,
309 MAY_WRITE | MAY_EXEC,
312 if (!rc && check_nlink)
313 rc = __mdd_may_link(env, pobj);
319 * Check whether can unlink from the pobj in the case of "cobj == NULL".
321 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
322 const struct md_attr *ma)
327 if (mdd_is_dead_obj(pobj))
330 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
331 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
334 rc = mdd_permission_internal_locked(env, pobj, NULL,
335 MAY_WRITE | MAY_EXEC,
340 if (mdd_is_append(pobj))
347 * pobj == NULL is remote ops case, under such case, pobj's
348 * VTX feature has been checked already, no need check again.
350 static inline int mdd_is_sticky(const struct lu_env *env,
351 struct mdd_object *pobj,
352 struct mdd_object *cobj)
354 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
355 struct md_ucred *uc = md_ucred(env);
359 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
363 if (!(tmp_la->la_mode & S_ISVTX) ||
364 (tmp_la->la_uid == uc->mu_fsuid))
368 rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
372 if (tmp_la->la_uid == uc->mu_fsuid)
375 return !mdd_capable(uc, CFS_CAP_FOWNER);
379 * Check whether it may delete the cobj from the pobj.
382 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
383 struct mdd_object *cobj, struct md_attr *ma,
384 int check_perm, int check_empty)
390 if (!mdd_object_exists(cobj))
393 if (mdd_is_dead_obj(cobj))
397 if (!mdd_object_exists(pobj))
400 if (mdd_is_dead_obj(pobj))
404 rc = mdd_permission_internal_locked(env, pobj, NULL,
405 MAY_WRITE | MAY_EXEC,
411 if (mdd_is_append(pobj))
415 if (!(ma->ma_attr_flags & MDS_VTX_BYPASS) &&
416 mdd_is_sticky(env, pobj, cobj))
419 if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
422 if ((ma->ma_attr.la_valid & LA_FLAGS) &&
423 (ma->ma_attr.la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
426 if (S_ISDIR(ma->ma_attr.la_mode)) {
427 struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
429 if (!S_ISDIR(mdd_object_type(cobj)))
432 if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
434 } else if (S_ISDIR(mdd_object_type(cobj)))
437 if (S_ISDIR(ma->ma_attr.la_mode) && check_empty)
438 rc = mdd_dir_is_empty(env, cobj);
445 * has mdd_write_lock on src already, but not on tgt yet
447 int mdd_link_sanity_check(const struct lu_env *env,
448 struct mdd_object *tgt_obj,
449 const struct lu_name *lname,
450 struct mdd_object *src_obj)
452 struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
456 if (!mdd_object_exists(src_obj))
459 if (mdd_is_dead_obj(src_obj))
462 /* Local ops, no lookup before link, check filename length here. */
463 if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
464 RETURN(-ENAMETOOLONG);
466 if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
469 if (S_ISDIR(mdd_object_type(src_obj)))
472 LASSERT(src_obj != tgt_obj);
474 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
479 rc = __mdd_may_link(env, src_obj);
485 * If subdir count is up to ddp_max_nlink, then enable MNLINK_OBJ flag and
486 * assign i_nlink to 1 which means the i_nlink for subdir count is incredible
487 * (maybe too large to be represented). It is a trick to break through the
488 * "i_nlink" limitation for subdir count.
490 void __mdd_ref_add(const struct lu_env *env, struct mdd_object *obj,
491 struct thandle *handle)
493 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
494 struct mdd_device *m = mdd_obj2mdd_dev(obj);
496 if (!mdd_is_mnlink(obj)) {
497 if (S_ISDIR(mdd_object_type(obj))) {
498 if (mdd_la_get(env, obj, tmp_la, BYPASS_CAPA))
501 if (tmp_la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink) {
502 obj->mod_flags |= MNLINK_OBJ;
503 tmp_la->la_nlink = 1;
504 tmp_la->la_valid = LA_NLINK;
505 mdd_attr_set_internal(env, obj, tmp_la, handle,
510 mdo_ref_add(env, obj, handle);
514 void __mdd_ref_del(const struct lu_env *env, struct mdd_object *obj,
515 struct thandle *handle, int is_dot)
517 if (!mdd_is_mnlink(obj) || is_dot)
518 mdo_ref_del(env, obj, handle);
521 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
522 const char *name, struct thandle *handle,
523 struct lustre_capa *capa)
525 struct dt_object *next = mdd_object_child(pobj);
529 if (dt_try_as_dir(env, next)) {
530 rc = next->do_index_ops->dio_delete(env, next,
531 (struct dt_key *)name,
539 static int __mdd_index_insert_only(const struct lu_env *env,
540 struct mdd_object *pobj,
541 const struct lu_fid *lf, const char *name,
542 struct thandle *handle,
543 struct lustre_capa *capa)
545 struct dt_object *next = mdd_object_child(pobj);
549 if (dt_try_as_dir(env, next)) {
550 struct md_ucred *uc = md_ucred(env);
552 rc = next->do_index_ops->dio_insert(env, next,
553 __mdd_fid_rec(env, lf),
554 (const struct dt_key *)name,
555 handle, capa, uc->mu_cap &
556 CFS_CAP_SYS_RESOURCE_MASK);
563 /* insert named index, add reference if isdir */
564 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
565 const struct lu_fid *lf, const char *name, int is_dir,
566 struct thandle *handle, struct lustre_capa *capa)
571 rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
572 if (rc == 0 && is_dir) {
573 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
574 __mdd_ref_add(env, pobj, handle);
575 mdd_write_unlock(env, pobj);
580 /* delete named index, drop reference if isdir */
581 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
582 const char *name, int is_dir, struct thandle *handle,
583 struct lustre_capa *capa)
588 rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
589 if (rc == 0 && is_dir) {
592 if (name != NULL && name[0] == '.' && name[1] == 0)
594 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
595 __mdd_ref_del(env, pobj, handle, is_dot);
596 mdd_write_unlock(env, pobj);
603 /** Store a namespace change changelog record
604 * If this fails, we must fail the whole transaction; we don't
605 * want the change to commit without the log entry.
606 * \param target - mdd_object of change
607 * \param parent - parent dir/object
608 * \param tf - target lu_fid, overrides fid of \a target if this is non-null
609 * \param tname - target name string
610 * \param handle - transacion handle
612 static int mdd_changelog_ns_store(const struct lu_env *env,
613 struct mdd_device *mdd,
614 enum changelog_rec_type type,
615 struct mdd_object *target,
616 struct mdd_object *parent,
617 const struct lu_fid *tf,
618 const struct lu_name *tname,
619 struct thandle *handle)
621 const struct lu_fid *tfid;
622 const struct lu_fid *tpfid = mdo2fid(parent);
623 struct llog_changelog_rec *rec;
629 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
632 LASSERT(parent != NULL);
633 LASSERT(tname != NULL);
634 LASSERT(handle != NULL);
637 reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
638 buf = mdd_buf_alloc(env, reclen);
639 if (buf->lb_buf == NULL)
641 rec = (struct llog_changelog_rec *)buf->lb_buf;
643 rec->cr_flags = CLF_VERSION;
644 rec->cr_type = (__u32)type;
645 tfid = tf ? tf : mdo2fid(target);
646 rec->cr_tfid = *tfid;
647 rec->cr_pfid = *tpfid;
648 rec->cr_namelen = tname->ln_namelen;
649 memcpy(rec->cr_name, tname->ln_name, rec->cr_namelen);
651 target->mod_cltime = cfs_time_current_64();
653 rc = mdd_changelog_llog_write(mdd, rec, handle);
655 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
656 rc, type, tname->ln_name, PFID(tfid), PFID(tpfid));
663 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
664 struct md_object *src_obj, const struct lu_name *lname,
667 const char *name = lname->ln_name;
668 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
669 struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
670 struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
671 struct mdd_device *mdd = mdo2mdd(src_obj);
672 struct dynlock_handle *dlh;
673 struct thandle *handle;
674 #ifdef HAVE_QUOTA_SUPPORT
675 struct obd_device *obd = mdd->mdd_obd_dev;
676 struct mds_obd *mds = &obd->u.mds;
677 unsigned int qids[MAXQUOTAS] = { 0, 0 };
678 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
683 #ifdef HAVE_QUOTA_SUPPORT
684 if (mds->mds_quota) {
685 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
687 rc = mdd_la_get(env, mdd_tobj, la_tmp, BYPASS_CAPA);
690 mdd_data_get(env, mdd_tobj, &data);
691 quota_opc = FSFILT_OP_LINK;
692 mdd_quota_wrapper(la_tmp, qids);
693 /* get block quota for parent */
694 lquota_chkquota(mds_quota_interface_ref, obd,
695 qids, rec_pending, 1, NULL,
696 LQUOTA_FLAGS_BLK, data, 1);
701 mdd_txn_param_build(env, mdd, MDD_TXN_LINK_OP);
702 handle = mdd_trans_start(env, mdd);
704 GOTO(out_pending, rc = PTR_ERR(handle));
706 dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
708 GOTO(out_trans, rc = -ENOMEM);
709 mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
711 rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
713 GOTO(out_unlock, rc);
715 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
717 mdd_object_capa(env, mdd_tobj));
719 GOTO(out_unlock, rc);
721 __mdd_ref_add(env, mdd_sobj, handle);
723 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
724 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
726 la->la_valid = LA_CTIME | LA_MTIME;
727 rc = mdd_attr_check_set_internal_locked(env, mdd_tobj, la, handle, 0);
729 GOTO(out_unlock, rc);
731 la->la_valid = LA_CTIME;
732 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
734 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj), lname, handle);
738 mdd_write_unlock(env, mdd_sobj);
739 mdd_pdo_write_unlock(env, mdd_tobj, dlh);
742 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, mdd_sobj,
743 mdd_tobj, NULL, lname, handle);
744 mdd_trans_stop(env, mdd, rc, handle);
746 #ifdef HAVE_QUOTA_SUPPORT
748 lquota_pending_commit(mds_quota_interface_ref, obd,
749 qids, rec_pending, 1);
750 /* Trigger dqacq for the parent owner. If failed,
751 * the next call for lquota_chkquota will process it. */
752 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
759 /* caller should take a lock before calling */
760 int mdd_finish_unlink(const struct lu_env *env,
761 struct mdd_object *obj, struct md_attr *ma,
768 LASSERT(mdd_write_locked(env, obj) != 0);
770 rc = mdd_iattr_get(env, obj, ma);
771 if (rc == 0 && ma->ma_attr.la_nlink == 0) {
772 obj->mod_flags |= DEAD_OBJ;
773 /* add new orphan and the object
774 * will be deleted during mdd_close() */
775 if (obj->mod_count) {
776 rc = __mdd_orphan_add(env, obj, th);
778 CDEBUG(D_HA, "Object "DFID" is inserted into "
779 "orphan list, open count = %d\n",
780 PFID(mdd_object_fid(obj)),
783 CERROR("Object "DFID" fail to be an orphan, "
784 "open count = %d, maybe cause failed "
786 PFID(mdd_object_fid(obj)),
789 rc = mdd_object_kill(env, obj, ma);
796 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
803 * has mdd_write_lock on cobj already, but not on pobj yet
805 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
806 struct mdd_object *cobj, struct md_attr *ma)
811 rc = mdd_may_delete(env, pobj, cobj, ma, 1, 1);
816 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
817 struct md_object *cobj, const struct lu_name *lname,
820 const char *name = lname->ln_name;
821 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
822 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
823 struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
824 struct mdd_device *mdd = mdo2mdd(pobj);
825 struct dynlock_handle *dlh;
826 struct thandle *handle;
827 #ifdef HAVE_QUOTA_SUPPORT
828 struct obd_device *obd = mdd->mdd_obd_dev;
829 struct mds_obd *mds = &obd->u.mds;
830 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
831 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
834 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
838 LASSERTF(mdd_object_exists(mdd_cobj) > 0, "FID is "DFID"\n",
839 PFID(mdd_object_fid(mdd_cobj)));
841 rc = mdd_log_txn_param_build(env, cobj, ma, MDD_TXN_UNLINK_OP);
845 handle = mdd_trans_start(env, mdd);
847 RETURN(PTR_ERR(handle));
849 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
851 GOTO(out_trans, rc = -ENOMEM);
852 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
854 rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, ma);
858 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
859 mdd_object_capa(env, mdd_pobj));
863 __mdd_ref_del(env, mdd_cobj, handle, 0);
866 __mdd_ref_del(env, mdd_cobj, handle, 1);
868 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
869 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
871 la->la_valid = LA_CTIME | LA_MTIME;
872 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
876 la->la_valid = LA_CTIME;
877 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
881 rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
882 #ifdef HAVE_QUOTA_SUPPORT
883 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
884 ma->ma_attr.la_nlink == 0) {
885 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
887 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
889 mdd_quota_wrapper(la_tmp, qpids);
890 if (mdd_cobj->mod_count == 0) {
891 quota_opc = FSFILT_OP_UNLINK;
892 mdd_quota_wrapper(&ma->ma_attr, qcids);
894 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
901 obd_set_info_async(mdd2obd_dev(mdd)->u.mds.mds_osc_exp,
902 sizeof(KEY_UNLINKED), KEY_UNLINKED, 0,
905 /* old files may not have link ea; ignore errors */
906 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
907 lname, NULL, NULL, handle);
911 mdd_write_unlock(env, mdd_cobj);
912 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
915 rc = mdd_changelog_ns_store(env, mdd,
916 is_dir ? CL_RMDIR : CL_UNLINK,
917 mdd_cobj, mdd_pobj, NULL, lname,
920 mdd_trans_stop(env, mdd, rc, handle);
921 #ifdef HAVE_QUOTA_SUPPORT
923 /* Trigger dqrel on the owner of child and parent. If failed,
924 * the next call for lquota_chkquota will process it. */
925 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
931 /* has not lock on pobj yet */
932 static int mdd_ni_sanity_check(const struct lu_env *env,
933 struct md_object *pobj,
934 const struct md_attr *ma)
936 struct mdd_object *obj = md2mdd_obj(pobj);
940 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
943 rc = mdd_may_create(env, obj, NULL, 1, S_ISDIR(ma->ma_attr.la_mode));
951 static int mdd_name_insert(const struct lu_env *env,
952 struct md_object *pobj,
953 const struct lu_name *lname,
954 const struct lu_fid *fid,
955 const struct md_attr *ma)
957 const char *name = lname->ln_name;
958 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
959 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
960 struct mdd_device *mdd = mdo2mdd(pobj);
961 struct dynlock_handle *dlh;
962 struct thandle *handle;
963 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
964 #ifdef HAVE_QUOTA_SUPPORT
965 struct md_ucred *uc = md_ucred(env);
966 struct obd_device *obd = mdd->mdd_obd_dev;
967 struct mds_obd *mds = &obd->u.mds;
968 unsigned int qids[MAXQUOTAS] = { 0, 0 };
969 int quota_opc = 0, rec_pending[MAXQUOTAS] = { 0, 0 };
970 cfs_cap_t save = uc->mu_cap;
975 #ifdef HAVE_QUOTA_SUPPORT
976 if (mds->mds_quota) {
977 if (!(ma->ma_attr_flags & MDS_QUOTA_IGNORE)) {
978 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
980 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
983 mdd_data_get(env, mdd_obj, &data);
984 quota_opc = FSFILT_OP_LINK;
985 mdd_quota_wrapper(la_tmp, qids);
986 /* get block quota for parent */
987 lquota_chkquota(mds_quota_interface_ref, obd,
988 qids, rec_pending, 1, NULL,
989 LQUOTA_FLAGS_BLK, data, 1);
992 uc->mu_cap |= CFS_CAP_SYS_RESOURCE_MASK;
996 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_INSERT_OP);
997 handle = mdd_trans_start(env, mdo2mdd(pobj));
999 GOTO(out_pending, rc = PTR_ERR(handle));
1001 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1003 GOTO(out_trans, rc = -ENOMEM);
1005 rc = mdd_ni_sanity_check(env, pobj, ma);
1007 GOTO(out_unlock, rc);
1009 rc = __mdd_index_insert(env, mdd_obj, fid, name, is_dir,
1010 handle, BYPASS_CAPA);
1012 GOTO(out_unlock, rc);
1015 * For some case, no need update obj's ctime (LA_CTIME is not set),
1017 * For other cases, update obj's ctime (LA_CTIME is set),
1020 if (ma->ma_attr.la_valid & LA_CTIME) {
1021 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1022 la->la_valid = LA_CTIME | LA_MTIME;
1023 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1028 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1030 mdd_trans_stop(env, mdo2mdd(pobj), rc, handle);
1032 #ifdef HAVE_QUOTA_SUPPORT
1033 if (mds->mds_quota) {
1035 lquota_pending_commit(mds_quota_interface_ref,
1036 obd, qids, rec_pending, 1);
1037 /* Trigger dqacq for the parent owner. If failed,
1038 * the next call for lquota_chkquota will process it*/
1039 lquota_adjust(mds_quota_interface_ref, obd, 0, qids,
1049 /* has not lock on pobj yet */
1050 static int mdd_nr_sanity_check(const struct lu_env *env,
1051 struct md_object *pobj,
1052 const struct md_attr *ma)
1054 struct mdd_object *obj = md2mdd_obj(pobj);
1058 if (ma->ma_attr_flags & MDS_PERM_BYPASS)
1061 rc = mdd_may_unlink(env, obj, ma);
1067 * Partial operation.
1069 static int mdd_name_remove(const struct lu_env *env,
1070 struct md_object *pobj,
1071 const struct lu_name *lname,
1072 const struct md_attr *ma)
1074 const char *name = lname->ln_name;
1075 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1076 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1077 struct mdd_device *mdd = mdo2mdd(pobj);
1078 struct dynlock_handle *dlh;
1079 struct thandle *handle;
1080 int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1081 #ifdef HAVE_QUOTA_SUPPORT
1082 struct obd_device *obd = mdd->mdd_obd_dev;
1083 struct mds_obd *mds = &obd->u.mds;
1084 unsigned int qids[MAXQUOTAS] = { 0, 0 };
1090 #ifdef HAVE_QUOTA_SUPPORT
1091 if (mds->mds_quota) {
1092 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1094 rc = mdd_la_get(env, mdd_obj, la_tmp, BYPASS_CAPA);
1096 quota_opc = FSFILT_OP_UNLINK_PARTIAL_PARENT;
1097 mdd_quota_wrapper(la_tmp, qids);
1101 mdd_txn_param_build(env, mdd, MDD_TXN_INDEX_DELETE_OP);
1102 handle = mdd_trans_start(env, mdd);
1104 GOTO(out_pending, rc = PTR_ERR(handle));
1106 dlh = mdd_pdo_write_lock(env, mdd_obj, name, MOR_TGT_PARENT);
1108 GOTO(out_trans, rc = -ENOMEM);
1110 rc = mdd_nr_sanity_check(env, pobj, ma);
1112 GOTO(out_unlock, rc);
1114 rc = __mdd_index_delete(env, mdd_obj, name, is_dir,
1115 handle, BYPASS_CAPA);
1117 GOTO(out_unlock, rc);
1120 * For some case, no need update obj's ctime (LA_CTIME is not set),
1122 * For other cases, update obj's ctime (LA_CTIME is set),
1125 if (ma->ma_attr.la_valid & LA_CTIME) {
1126 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1127 la->la_valid = LA_CTIME | LA_MTIME;
1128 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la,
1133 mdd_pdo_write_unlock(env, mdd_obj, dlh);
1135 mdd_trans_stop(env, mdd, rc, handle);
1137 #ifdef HAVE_QUOTA_SUPPORT
1138 /* Trigger dqrel for the parent owner.
1139 * If failed, the next call for lquota_chkquota will process it. */
1141 lquota_adjust(mds_quota_interface_ref, obd, 0, qids, rc,
1149 * has mdd_write_lock on tobj alreay, but not on tgt_pobj yet
1151 static int mdd_rt_sanity_check(const struct lu_env *env,
1152 struct mdd_object *tgt_pobj,
1153 struct mdd_object *tobj,
1159 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1162 /* XXX: for mdd_rename_tgt, "tobj == NULL" does not mean tobj not
1163 * exist. In fact, tobj must exist, otherwise the call trace will be:
1164 * mdt_reint_rename_tgt -> mdo_name_insert -> ... -> mdd_name_insert.
1165 * When get here, tobj must be NOT NULL, the other case has been
1166 * processed in cmr_rename_tgt before mdd_rename_tgt and enable
1168 * So check may_delete, but not check nlink of tgt_pobj. */
1170 rc = mdd_may_delete(env, tgt_pobj, tobj, ma, 1, 1);
1175 /* Partial rename op on slave MDD */
1176 static int mdd_rename_tgt(const struct lu_env *env,
1177 struct md_object *pobj, struct md_object *tobj,
1178 const struct lu_fid *lf, const struct lu_name *lname,
1181 const char *name = lname->ln_name;
1182 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1183 struct mdd_object *mdd_tpobj = md2mdd_obj(pobj);
1184 struct mdd_object *mdd_tobj = md2mdd_obj(tobj);
1185 struct mdd_device *mdd = mdo2mdd(pobj);
1186 struct dynlock_handle *dlh;
1187 struct thandle *handle;
1188 #ifdef HAVE_QUOTA_SUPPORT
1189 struct obd_device *obd = mdd->mdd_obd_dev;
1190 struct mds_obd *mds = &obd->u.mds;
1191 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1192 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1193 int quota_copc = 0, quota_popc = 0;
1194 int rec_pending[MAXQUOTAS] = { 0, 0 };
1199 #ifdef HAVE_QUOTA_SUPPORT
1200 if (mds->mds_quota && !tobj) {
1201 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1203 rc = mdd_la_get(env, mdd_tpobj, la_tmp, BYPASS_CAPA);
1206 mdd_data_get(env, mdd_tpobj, &data);
1207 quota_popc = FSFILT_OP_LINK;
1208 mdd_quota_wrapper(la_tmp, qpids);
1209 /* get block quota for target parent */
1210 lquota_chkquota(mds_quota_interface_ref, obd,
1211 qpids, rec_pending, 1, NULL,
1212 LQUOTA_FLAGS_BLK, data, 1);
1216 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1217 handle = mdd_trans_start(env, mdd);
1219 GOTO(out_pending, rc = PTR_ERR(handle));
1221 dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1223 GOTO(out_trans, rc = -ENOMEM);
1225 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1227 rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1232 * If rename_tgt is called then we should just re-insert name with
1233 * correct fid, no need to dec/inc parent nlink if obj is dir.
1235 rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1239 rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1244 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1245 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1247 la->la_valid = LA_CTIME | LA_MTIME;
1248 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1253 * For tobj is remote case cmm layer has processed
1254 * and pass NULL tobj to here. So when tobj is NOT NULL,
1255 * it must be local one.
1257 if (tobj && mdd_object_exists(mdd_tobj)) {
1258 __mdd_ref_del(env, mdd_tobj, handle, 0);
1260 /* Remove dot reference. */
1261 if (S_ISDIR(ma->ma_attr.la_mode))
1262 __mdd_ref_del(env, mdd_tobj, handle, 1);
1264 la->la_valid = LA_CTIME;
1265 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1269 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1273 #ifdef HAVE_QUOTA_SUPPORT
1274 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1275 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1276 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1277 mdd_quota_wrapper(&ma->ma_attr, qcids);
1284 mdd_write_unlock(env, mdd_tobj);
1285 mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1288 /* Bare EXT record with no RENAME in front of it signifies
1289 a partial slave op */
1290 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
1291 mdd_tpobj, NULL, lname, handle);
1293 mdd_trans_stop(env, mdd, rc, handle);
1295 #ifdef HAVE_QUOTA_SUPPORT
1296 if (mds->mds_quota) {
1298 lquota_pending_commit(mds_quota_interface_ref, obd,
1299 qpids, rec_pending, 1);
1302 /* Trigger dqrel on the target owner of child.
1303 * If failed, the next call for lquota_chkquota
1304 * will process it. */
1305 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1313 * The permission has been checked when obj created, no need check again.
1315 static int mdd_cd_sanity_check(const struct lu_env *env,
1316 struct mdd_object *obj)
1321 if (!obj || mdd_is_dead_obj(obj))
1328 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1329 struct md_object *cobj, const struct md_op_spec *spec,
1332 struct mdd_device *mdd = mdo2mdd(cobj);
1333 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1334 struct mdd_object *son = md2mdd_obj(cobj);
1335 struct lu_attr *attr = &ma->ma_attr;
1336 struct lov_mds_md *lmm = NULL;
1338 struct thandle *handle;
1342 rc = mdd_cd_sanity_check(env, son);
1346 if (!md_should_create(spec->sp_cr_flags))
1348 lmm_size = ma->ma_lmm_size;
1349 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1354 mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1355 handle = mdd_trans_start(env, mdd);
1357 GOTO(out_free, rc = PTR_ERR(handle));
1360 * XXX: Setting the lov ea is not locked but setting the attr is locked?
1361 * Should this be fixed?
1364 /* Replay creates has objects already */
1366 if (spec->no_create) {
1367 CDEBUG(D_INFO, "we already have lov ea\n");
1368 rc = mdd_lov_set_md(env, mdd_pobj, son,
1369 (struct lov_mds_md *)spec->u.sp_ea.eadata,
1370 spec->u.sp_ea.eadatalen, handle, 0);
1373 /* No need mdd_lsm_sanity_check here */
1374 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1375 lmm_size, handle, 0);
1378 rc = mdd_attr_get_internal_locked(env, son, ma);
1380 /* update lov_objid data, must be before transaction stop! */
1382 mdd_lov_objid_update(mdd, lmm);
1384 mdd_trans_stop(env, mdd, rc, handle);
1386 /* Finish mdd_lov_create() stuff. */
1387 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1391 /* Get fid from name and parent */
1393 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1394 const struct lu_name *lname, struct lu_fid* fid, int mask)
1396 const char *name = lname->ln_name;
1397 const struct dt_key *key = (const struct dt_key *)name;
1398 struct mdd_object *mdd_obj = md2mdd_obj(pobj);
1399 struct mdd_device *m = mdo2mdd(pobj);
1400 struct dt_object *dir = mdd_object_child(mdd_obj);
1401 struct lu_fid_pack *pack = &mdd_env_info(env)->mti_pack;
1405 if (unlikely(mdd_is_dead_obj(mdd_obj)))
1408 rc = mdd_object_exists(mdd_obj);
1409 if (unlikely(rc == 0))
1411 else if (unlikely(rc < 0)) {
1412 CERROR("Object "DFID" locates on remote server\n",
1413 PFID(mdo2fid(mdd_obj)));
1417 /* The common filename length check. */
1418 if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1419 RETURN(-ENAMETOOLONG);
1421 rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1426 if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1427 dt_try_as_dir(env, dir))) {
1429 rc = dir->do_index_ops->dio_lookup(env, dir,
1430 (struct dt_rec *)pack, key,
1431 mdd_object_capa(env, mdd_obj));
1433 rc = fid_unpack(pack, fid);
1442 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1443 const struct lu_name *lname, struct mdd_object *child,
1444 struct md_attr *ma, struct thandle *handle,
1445 const struct md_op_spec *spec)
1451 * Update attributes for child.
1454 * (1) the valid bits should be converted between Lustre and Linux;
1455 * (2) maybe, the child attributes should be set in OSD when creation.
1458 rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1462 if (S_ISDIR(ma->ma_attr.la_mode)) {
1463 /* Add "." and ".." for newly created dir */
1464 __mdd_ref_add(env, child, handle);
1465 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1466 dot, handle, BYPASS_CAPA);
1468 rc = __mdd_index_insert_only(env, child, pfid,
1474 rc2 = __mdd_index_delete(env, child, dot, 1,
1475 handle, BYPASS_CAPA);
1477 CERROR("Failure to cleanup after dotdot"
1478 " creation: %d (%d)\n", rc2, rc);
1483 mdd_links_add(env, child, pfid, lname, handle);
1488 /* has not lock on pobj yet */
1489 static int mdd_create_sanity_check(const struct lu_env *env,
1490 struct md_object *pobj,
1491 const struct lu_name *lname,
1493 struct md_op_spec *spec)
1495 struct mdd_thread_info *info = mdd_env_info(env);
1496 struct lu_attr *la = &info->mti_la;
1497 struct lu_fid *fid = &info->mti_fid;
1498 struct mdd_object *obj = md2mdd_obj(pobj);
1499 struct mdd_device *m = mdo2mdd(pobj);
1500 int lookup = spec->sp_cr_lookup;
1505 if (mdd_is_dead_obj(obj))
1509 * In some cases this lookup is not needed - we know before if name
1510 * exists or not because MDT performs lookup for it.
1511 * name length check is done in lookup.
1515 * Check if the name already exist, though it will be checked in
1516 * _index_insert also, for avoiding rolling back if exists
1519 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1520 MAY_WRITE | MAY_EXEC);
1522 RETURN(rc ? : -EEXIST);
1525 * Check WRITE permission for the parent.
1526 * EXEC permission have been checked
1527 * when lookup before create already.
1529 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1536 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1540 if (la->la_mode & S_ISGID) {
1541 ma->ma_attr.la_gid = la->la_gid;
1542 if (S_ISDIR(ma->ma_attr.la_mode)) {
1543 ma->ma_attr.la_mode |= S_ISGID;
1544 ma->ma_attr.la_valid |= LA_MODE;
1548 switch (ma->ma_attr.la_mode & S_IFMT) {
1550 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1552 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1553 RETURN(-ENAMETOOLONG);
1573 * Create object and insert it into namespace.
1575 static int mdd_create(const struct lu_env *env,
1576 struct md_object *pobj,
1577 const struct lu_name *lname,
1578 struct md_object *child,
1579 struct md_op_spec *spec,
1582 struct mdd_thread_info *info = mdd_env_info(env);
1583 struct lu_attr *la = &info->mti_la_for_fix;
1584 struct md_attr *ma_acl = &info->mti_ma;
1585 struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1586 struct mdd_object *son = md2mdd_obj(child);
1587 struct mdd_device *mdd = mdo2mdd(pobj);
1588 struct lu_attr *attr = &ma->ma_attr;
1589 struct lov_mds_md *lmm = NULL;
1590 struct thandle *handle;
1591 struct dynlock_handle *dlh;
1592 const char *name = lname->ln_name;
1593 int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1594 int got_def_acl = 0;
1595 #ifdef HAVE_QUOTA_SUPPORT
1596 struct obd_device *obd = mdd->mdd_obd_dev;
1597 struct mds_obd *mds = &obd->u.mds;
1598 unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1599 unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1600 int quota_opc = 0, block_count = 0;
1601 int inode_pending[MAXQUOTAS] = { 0, 0 };
1602 int block_pending[MAXQUOTAS] = { 0, 0 };
1603 int parent_pending[MAXQUOTAS] = { 0, 0 };
1608 * Two operations have to be performed:
1610 * - an allocation of a new object (->do_create()), and
1612 * - an insertion into a parent index (->dio_insert()).
1614 * Due to locking, operation order is not important, when both are
1615 * successful, *but* error handling cases are quite different:
1617 * - if insertion is done first, and following object creation fails,
1618 * insertion has to be rolled back, but this operation might fail
1619 * also leaving us with dangling index entry.
1621 * - if creation is done first, is has to be undone if insertion
1622 * fails, leaving us with leaked space, which is neither good, nor
1625 * It seems that creation-first is simplest solution, but it is
1626 * sub-optimal in the frequent
1631 * case, because second mkdir is bound to create object, only to
1632 * destroy it immediately.
1634 * To avoid this follow local file systems that do double lookup:
1636 * 0. lookup -> -EEXIST (mdd_create_sanity_check())
1638 * 1. create (mdd_object_create_internal())
1640 * 2. insert (__mdd_index_insert(), lookup again)
1643 /* Sanity checks before big job. */
1644 rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1648 #ifdef HAVE_QUOTA_SUPPORT
1649 if (mds->mds_quota) {
1650 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1652 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1656 quota_opc = FSFILT_OP_CREATE;
1657 mdd_quota_wrapper(&ma->ma_attr, qcids);
1658 mdd_quota_wrapper(la_tmp, qpids);
1659 /* get file quota for child */
1660 lquota_chkquota(mds_quota_interface_ref, obd, qcids,
1661 inode_pending, 1, NULL, 0, NULL, 0);
1662 switch (ma->ma_attr.la_mode & S_IFMT) {
1671 if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1672 qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1676 /* get block quota for child and parent */
1678 lquota_chkquota(mds_quota_interface_ref, obd,
1679 qcids, block_pending,
1681 LQUOTA_FLAGS_BLK, NULL, 0);
1683 lquota_chkquota(mds_quota_interface_ref, obd,
1684 qpids, parent_pending, 1, NULL,
1685 LQUOTA_FLAGS_BLK, NULL, 0);
1691 * No RPC inside the transaction, so OST objects should be created at
1694 if (S_ISREG(attr->la_mode)) {
1695 lmm_size = ma->ma_lmm_size;
1696 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1699 GOTO(out_pending, rc);
1702 if (!S_ISLNK(attr->la_mode)) {
1703 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1704 ma_acl->ma_acl = info->mti_xattr_buf;
1705 ma_acl->ma_need = MA_ACL_DEF;
1706 ma_acl->ma_valid = 0;
1708 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1709 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1710 mdd_read_unlock(env, mdd_pobj);
1713 else if (ma_acl->ma_valid & MA_ACL_DEF)
1717 mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1718 handle = mdd_trans_start(env, mdd);
1720 GOTO(out_free, rc = PTR_ERR(handle));
1722 dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1724 GOTO(out_trans, rc = -ENOMEM);
1726 mdd_write_lock(env, son, MOR_TGT_CHILD);
1727 rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1729 mdd_write_unlock(env, son);
1735 #ifdef CONFIG_FS_POSIX_ACL
1737 struct lu_buf *acl_buf = &info->mti_buf;
1738 acl_buf->lb_buf = ma_acl->ma_acl;
1739 acl_buf->lb_len = ma_acl->ma_acl_size;
1741 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1743 mdd_write_unlock(env, son);
1746 ma->ma_attr.la_valid |= LA_MODE;
1751 rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1752 son, ma, handle, spec);
1753 mdd_write_unlock(env, son);
1756 * Object has no links, so it will be destroyed when last
1757 * reference is released. (XXX not now.)
1763 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1764 name, S_ISDIR(attr->la_mode), handle,
1765 mdd_object_capa(env, mdd_pobj));
1772 /* No need mdd_lsm_sanity_check here */
1773 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1775 CERROR("error on stripe info copy %d \n", rc);
1778 if (lmm && lmm_size > 0) {
1779 /* Set Lov here, do not get lmm again later */
1780 memcpy(ma->ma_lmm, lmm, lmm_size);
1781 ma->ma_lmm_size = lmm_size;
1782 ma->ma_valid |= MA_LOV;
1785 if (S_ISLNK(attr->la_mode)) {
1786 struct md_ucred *uc = md_ucred(env);
1787 struct dt_object *dt = mdd_object_child(son);
1788 const char *target_name = spec->u.sp_symname;
1789 int sym_len = strlen(target_name);
1790 const struct lu_buf *buf;
1793 buf = mdd_buf_get_const(env, target_name, sym_len);
1794 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1795 mdd_object_capa(env, son),
1797 CFS_CAP_SYS_RESOURCE_MASK);
1802 GOTO(cleanup, rc = -EFAULT);
1806 la->la_valid = LA_CTIME | LA_MTIME;
1807 rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1811 /* Return attr back. */
1812 rc = mdd_attr_get_internal_locked(env, son, ma);
1815 if (rc && created) {
1819 rc2 = __mdd_index_delete(env, mdd_pobj, name,
1820 S_ISDIR(attr->la_mode),
1821 handle, BYPASS_CAPA);
1823 CERROR("error can not cleanup destroy %d\n",
1828 mdd_write_lock(env, son, MOR_TGT_CHILD);
1829 __mdd_ref_del(env, son, handle, 0);
1830 if (initialized && S_ISDIR(attr->la_mode))
1831 __mdd_ref_del(env, son, handle, 1);
1832 mdd_write_unlock(env, son);
1836 /* update lov_objid data, must be before transaction stop! */
1838 mdd_lov_objid_update(mdd, lmm);
1840 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1843 rc = mdd_changelog_ns_store(env, mdd,
1844 S_ISDIR(attr->la_mode) ? CL_MKDIR :
1845 S_ISREG(attr->la_mode) ? CL_CREATE :
1846 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1847 son, mdd_pobj, NULL, lname, handle);
1848 mdd_trans_stop(env, mdd, rc, handle);
1850 /* finis lov_create stuff, free all temporary data */
1851 mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1853 #ifdef HAVE_QUOTA_SUPPORT
1855 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1857 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1859 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1861 /* Trigger dqacq on the owner of child and parent. If failed,
1862 * the next call for lquota_chkquota will process it. */
1863 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1871 * Get locks on parents in proper order
1872 * RETURN: < 0 - error, rename_order if successful
1880 static int mdd_rename_order(const struct lu_env *env,
1881 struct mdd_device *mdd,
1882 struct mdd_object *src_pobj,
1883 struct mdd_object *tgt_pobj)
1885 /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1889 if (src_pobj == tgt_pobj)
1890 RETURN(MDD_RN_SAME);
1892 /* compared the parent child relationship of src_p&tgt_p */
1893 if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1895 } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1898 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1911 /* has not mdd_write{read}_lock on any obj yet. */
1912 static int mdd_rename_sanity_check(const struct lu_env *env,
1913 struct mdd_object *src_pobj,
1914 struct mdd_object *tgt_pobj,
1915 struct mdd_object *sobj,
1916 struct mdd_object *tobj,
1922 if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1925 /* XXX: when get here, sobj must NOT be NULL,
1926 * the other case has been processed in cml_rename
1927 * before mdd_rename and enable MDS_PERM_BYPASS. */
1930 rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1934 /* XXX: when get here, "tobj == NULL" means tobj must
1935 * NOT exist (neither on remote MDS, such case has been
1936 * processed in cml_rename before mdd_rename and enable
1938 * So check may_create, but not check may_unlink. */
1940 rc = mdd_may_create(env, tgt_pobj, NULL,
1941 (src_pobj != tgt_pobj), 0);
1943 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1944 (src_pobj != tgt_pobj), 1);
1946 if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1947 S_ISDIR(ma->ma_attr.la_mode))
1948 rc = __mdd_may_link(env, tgt_pobj);
1953 /* src object can be remote that is why we use only fid and type of object */
1954 static int mdd_rename(const struct lu_env *env,
1955 struct md_object *src_pobj, struct md_object *tgt_pobj,
1956 const struct lu_fid *lf, const struct lu_name *lsname,
1957 struct md_object *tobj, const struct lu_name *ltname,
1960 const char *sname = lsname->ln_name;
1961 const char *tname = ltname->ln_name;
1962 struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1963 struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1964 struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1965 struct mdd_device *mdd = mdo2mdd(src_pobj);
1966 struct mdd_object *mdd_sobj = NULL; /* source object */
1967 struct mdd_object *mdd_tobj = NULL;
1968 struct dynlock_handle *sdlh, *tdlh;
1969 struct thandle *handle;
1970 const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1971 const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1975 #ifdef HAVE_QUOTA_SUPPORT
1976 struct obd_device *obd = mdd->mdd_obd_dev;
1977 struct mds_obd *mds = &obd->u.mds;
1978 unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1979 unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1980 unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1981 int quota_copc = 0, quota_popc = 0;
1982 int rec_pending[MAXQUOTAS] = { 0, 0 };
1986 LASSERT(ma->ma_attr.la_mode & S_IFMT);
1987 is_dir = S_ISDIR(ma->ma_attr.la_mode);
1990 mdd_tobj = md2mdd_obj(tobj);
1992 #ifdef HAVE_QUOTA_SUPPORT
1993 if (mds->mds_quota) {
1994 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1996 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
1998 mdd_quota_wrapper(la_tmp, qspids);
2000 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2004 mdd_data_get(env, mdd_tpobj, &data);
2005 quota_popc = FSFILT_OP_LINK;
2006 mdd_quota_wrapper(la_tmp, qtpids);
2007 /* get block quota for target parent */
2008 lquota_chkquota(mds_quota_interface_ref,
2010 rec_pending, 1, NULL,
2018 mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2019 handle = mdd_trans_start(env, mdd);
2021 GOTO(out_pending, rc = PTR_ERR(handle));
2023 /* FIXME: Should consider tobj and sobj too in rename_lock. */
2024 rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2026 GOTO(cleanup_unlocked, rc);
2028 /* Get locks in determined order */
2029 if (rc == MDD_RN_SAME) {
2030 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2031 sname, MOR_SRC_PARENT);
2032 /* check hashes to determine do we need one lock or two */
2033 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2034 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2038 } else if (rc == MDD_RN_SRCTGT) {
2039 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2040 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2042 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2043 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2045 if (sdlh == NULL || tdlh == NULL)
2046 GOTO(cleanup, rc = -ENOMEM);
2048 mdd_sobj = mdd_object_find(env, mdd, lf);
2049 rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2050 mdd_sobj, mdd_tobj, ma);
2054 /* Remove source name from source directory */
2055 rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2056 mdd_object_capa(env, mdd_spobj));
2060 /* "mv dir1 dir2" needs "dir1/.." link update */
2061 if (is_dir && mdd_sobj) {
2062 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2063 mdd_object_capa(env, mdd_sobj));
2065 GOTO(fixup_spobj2, rc);
2067 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2068 handle, mdd_object_capa(env, mdd_sobj));
2070 GOTO(fixup_spobj, rc);
2074 /* Remove target name from target directory
2075 * Here tobj can be remote one, so we do index_delete unconditionally
2076 * and -ENOENT is allowed.
2078 rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2079 mdd_object_capa(env, mdd_tpobj));
2082 /* tname might been renamed to something else */
2083 GOTO(fixup_spobj, rc);
2086 GOTO(fixup_spobj, rc);
2089 /* Insert new fid with target name into target dir */
2090 rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2091 mdd_object_capa(env, mdd_tpobj));
2093 GOTO(fixup_tpobj, rc);
2095 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2096 la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2098 /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2100 la->la_valid = LA_CTIME;
2101 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2104 GOTO(fixup_tpobj, rc);
2107 /* Remove old target object
2108 * For tobj is remote case cmm layer has processed
2109 * and set tobj to NULL then. So when tobj is NOT NULL,
2110 * it must be local one.
2112 if (tobj && mdd_object_exists(mdd_tobj)) {
2113 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2114 if (mdd_is_dead_obj(mdd_tobj)) {
2115 mdd_write_unlock(env, mdd_tobj);
2116 /* shld not be dead, something is wrong */
2117 CERROR("tobj is dead, something is wrong\n");
2121 __mdd_ref_del(env, mdd_tobj, handle, 0);
2123 /* Remove dot reference. */
2125 __mdd_ref_del(env, mdd_tobj, handle, 1);
2127 la->la_valid = LA_CTIME;
2128 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2130 GOTO(fixup_tpobj, rc);
2132 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2133 mdd_write_unlock(env, mdd_tobj);
2135 GOTO(fixup_tpobj, rc);
2137 #ifdef HAVE_QUOTA_SUPPORT
2138 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2139 ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2140 quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2141 mdd_quota_wrapper(&ma->ma_attr, qtcids);
2146 la->la_valid = LA_CTIME | LA_MTIME;
2147 rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2149 GOTO(fixup_tpobj, rc);
2151 if (mdd_spobj != mdd_tpobj) {
2152 la->la_valid = LA_CTIME | LA_MTIME;
2153 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2157 if (rc == 0 && mdd_sobj) {
2158 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2159 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2160 mdo2fid(mdd_tpobj), ltname, handle);
2162 /* Old files might not have EA entry */
2163 mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2165 mdd_write_unlock(env, mdd_sobj);
2166 /* We don't fail the transaction if the link ea can't be
2167 updated -- fid2path will use alternate lookup method. */
2175 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2178 CWARN("tp obj fix error %d\n",rc2);
2180 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2181 !mdd_is_dead_obj(mdd_tobj)) {
2182 rc2 = __mdd_index_insert(env, mdd_tpobj,
2183 mdo2fid(mdd_tobj), tname,
2188 CWARN("tp obj fix error %d\n",rc2);
2193 if (rc && is_dir && mdd_sobj) {
2194 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2198 CWARN("sp obj dotdot delete error %d\n",rc2);
2201 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2202 dotdot, handle, BYPASS_CAPA);
2204 CWARN("sp obj dotdot insert error %d\n",rc2);
2209 rc2 = __mdd_index_insert(env, mdd_spobj,
2210 lf, sname, is_dir, handle, BYPASS_CAPA);
2212 CWARN("sp obj fix error %d\n",rc2);
2215 if (likely(tdlh) && sdlh != tdlh)
2216 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2218 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2221 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2222 mdd_spobj, lf, lsname, handle);
2224 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2225 mdd_tpobj, lf, ltname, handle);
2227 mdd_trans_stop(env, mdd, rc, handle);
2229 mdd_object_put(env, mdd_sobj);
2231 #ifdef HAVE_QUOTA_SUPPORT
2232 if (mds->mds_quota) {
2234 lquota_pending_commit(mds_quota_interface_ref, obd,
2235 qtpids, rec_pending, 1);
2238 /* Trigger dqrel on the source owner of parent.
2239 * If failed, the next call for lquota_chkquota will
2241 lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2242 FSFILT_OP_UNLINK_PARTIAL_PARENT);
2244 /* Trigger dqrel on the target owner of child.
2245 * If failed, the next call for lquota_chkquota
2246 * will process it. */
2247 lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2248 qtpids, rc, quota_copc);
2255 /** enable/disable storing of hardlink info */
2256 int mdd_linkea_enable = 1;
2257 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2258 "record hardlink info in EAs");
2260 /** Read the link EA into a temp buffer.
2261 * Uses the name_buf since it is generally large.
2262 * \retval IS_ERR err
2263 * \retval ptr to \a lu_buf (always \a mti_big_buf)
2265 struct lu_buf *mdd_links_get(const struct lu_env *env,
2266 struct mdd_object *mdd_obj)
2269 struct lustre_capa *capa;
2270 struct link_ea_header *leh;
2273 /* First try a small buf */
2274 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2275 if (buf->lb_buf == NULL)
2276 return ERR_PTR(-ENOMEM);
2278 capa = mdd_object_capa(env, mdd_obj);
2279 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2280 if (rc == -ERANGE) {
2281 /* Buf was too small, figure out what we need. */
2284 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2287 buf = mdd_buf_alloc(env, rc);
2288 if (buf->lb_buf == NULL)
2289 return ERR_PTR(-ENOMEM);
2290 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2296 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2297 leh->leh_magic = LINK_EA_MAGIC;
2298 leh->leh_reccount = __swab32(leh->leh_reccount);
2299 leh->leh_len = __swab64(leh->leh_len);
2300 /* entries are swabbed by mdd_lee_unpack */
2302 if (leh->leh_magic != LINK_EA_MAGIC)
2303 return ERR_PTR(-EINVAL);
2304 if (leh->leh_reccount == 0)
2305 return ERR_PTR(-ENODATA);
2310 /** Pack a link_ea_entry.
2311 * All elements are stored as chars to avoid alignment issues.
2312 * Numbers are always big-endian
2313 * \param packbuf is a temp fid buffer
2314 * \retval record length
2316 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2317 const struct lu_fid *pfid, struct lu_fid* packbuf)
2322 fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2323 ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2324 strncpy(ptr, lname->ln_name, lname->ln_namelen);
2325 reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2326 sizeof(lee->lee_reclen);
2327 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2328 lee->lee_reclen[1] = reclen & 0xff;
2332 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2333 struct lu_name *lname, struct lu_fid *pfid)
2335 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2336 fid_unpack(&lee->lee_parent_fid, pfid);
2337 lname->ln_name = (char *)&lee->lee_parent_fid +
2338 lee->lee_parent_fid.fp_len;
2339 lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2340 sizeof(lee->lee_reclen);
2343 /** Add a record to the end of link ea buf */
2344 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2345 const struct lu_fid *pfid,
2346 const struct lu_name *lname)
2348 struct link_ea_header *leh;
2349 struct link_ea_entry *lee;
2352 if (lname == NULL || pfid == NULL)
2355 /* Make sure our buf is big enough for the new one */
2357 reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2358 if (leh->leh_len + reclen > buf->lb_len) {
2359 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2364 lee = buf->lb_buf + leh->leh_len;
2365 reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2366 leh->leh_len += reclen;
2367 leh->leh_reccount++;
2371 /* For pathologic linkers, we don't want to spend lots of time scanning the
2372 * link ea. Limit ourseleves to something reasonable; links not in the EA
2373 * can be looked up via (slower) parent lookup.
2375 #define LINKEA_MAX_COUNT 128
2377 static int mdd_links_add(const struct lu_env *env,
2378 struct mdd_object *mdd_obj,
2379 const struct lu_fid *pfid,
2380 const struct lu_name *lname,
2381 struct thandle *handle)
2384 struct link_ea_header *leh;
2388 if (!mdd_linkea_enable)
2391 buf = mdd_links_get(env, mdd_obj);
2394 if (rc != -ENODATA) {
2395 CERROR("link_ea read failed %d "DFID"\n", rc,
2396 PFID(mdd_object_fid(mdd_obj)));
2399 /* empty EA; start one */
2400 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2401 if (buf->lb_buf == NULL)
2404 leh->leh_magic = LINK_EA_MAGIC;
2405 leh->leh_len = sizeof(struct link_ea_header);
2406 leh->leh_reccount = 0;
2410 if (leh->leh_reccount > LINKEA_MAX_COUNT)
2413 rc = __mdd_links_add(env, buf, pfid, lname);
2418 rc = __mdd_xattr_set(env, mdd_obj,
2419 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2420 XATTR_NAME_LINK, 0, handle);
2422 CERROR("link_ea add failed %d "DFID"\n", rc,
2423 PFID(mdd_object_fid(mdd_obj)));
2425 if (buf->lb_vmalloc)
2426 /* if we vmalloced a large buffer drop it */
2432 static int mdd_links_rename(const struct lu_env *env,
2433 struct mdd_object *mdd_obj,
2434 const struct lu_fid *oldpfid,
2435 const struct lu_name *oldlname,
2436 const struct lu_fid *newpfid,
2437 const struct lu_name *newlname,
2438 struct thandle *handle)
2441 struct link_ea_header *leh;
2442 struct link_ea_entry *lee;
2443 struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2444 struct lu_fid *tmpfid = &mdd_env_info(env)->mti_fid;
2450 if (!mdd_linkea_enable)
2453 if (mdd_obj->mod_flags & DEAD_OBJ)
2454 /* No more links, don't bother */
2457 buf = mdd_links_get(env, mdd_obj);
2460 CERROR("link_ea read failed %d "DFID"\n",
2461 rc, PFID(mdd_object_fid(mdd_obj)));
2465 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2467 /* Find the old record */
2468 for(count = 0; count <= leh->leh_reccount; count++) {
2469 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2470 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2471 lu_fid_eq(tmpfid, oldpfid) &&
2472 (strncmp(tmpname->ln_name, oldlname->ln_name,
2473 tmpname->ln_namelen) == 0))
2475 lee = (struct link_ea_entry *)((char *)lee + reclen);
2477 if (count > leh->leh_reccount) {
2478 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2479 oldlname->ln_namelen, oldlname->ln_name);
2480 GOTO(out, rc = -ENOENT);
2483 /* Remove the old record */
2484 leh->leh_reccount--;
2485 leh->leh_len -= reclen;
2486 memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2489 /* If renaming, add the new record */
2490 if (newpfid != NULL) {
2491 /* if the add fails, we still delete the out-of-date old link */
2492 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2496 rc = __mdd_xattr_set(env, mdd_obj,
2497 mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2498 XATTR_NAME_LINK, 0, handle);
2504 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2505 oldlname->ln_namelen, oldlname->ln_name, rc,
2506 PFID(mdd_object_fid(mdd_obj)));
2508 if (buf->lb_vmalloc)
2509 /* if we vmalloced a large buffer drop it */
2515 const struct md_dir_operations mdd_dir_ops = {
2516 .mdo_is_subdir = mdd_is_subdir,
2517 .mdo_lookup = mdd_lookup,
2518 .mdo_create = mdd_create,
2519 .mdo_rename = mdd_rename,
2520 .mdo_link = mdd_link,
2521 .mdo_unlink = mdd_unlink,
2522 .mdo_name_insert = mdd_name_insert,
2523 .mdo_name_remove = mdd_name_remove,
2524 .mdo_rename_tgt = mdd_rename_tgt,
2525 .mdo_create_data = mdd_create_data