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_trans.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>
60 #include "mdd_internal.h"
62 static int dto_txn_credits[DTO_NR];
64 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
67 struct mdd_device *mdd = cookie;
68 struct obd_device *obd = mdd2obd_dev(mdd);
69 /* Each transaction updates lov objids, the credits should be added for
71 int blk, shift = mdd->mdd_dt_conf.ddp_block_shift;
72 blk = ((obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id) +
73 (1 << shift) - 1) >> shift) + 1;
75 /* add lov objids credits */
76 param->tp_credits += blk * dto_txn_credits[DTO_WRITE_BLOCK] +
77 dto_txn_credits[DTO_WRITE_BASE];
82 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
85 struct mdd_device *mdd = cookie;
86 struct obd_device *obd = mdd2obd_dev(mdd);
89 return mds_lov_write_objids(obd);
92 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
98 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
101 LASSERT(0 <= op && op < MDD_TXN_LAST_OP);
103 txn_param_init(&mdd_env_info(env)->mti_param,
104 mdd->mdd_tod[op].mod_credits);
107 int mdd_log_txn_param_build(const struct lu_env *env, struct md_object *obj,
108 struct md_attr *ma, enum mdd_txn_op op)
110 struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
111 int rc, log_credits, stripe;
114 mdd_txn_param_build(env, mdd, op);
116 if (S_ISDIR(lu_object_attr(&obj->mo_lu)))
119 LASSERT(op == MDD_TXN_UNLINK_OP || op == MDD_TXN_RENAME_OP);
120 rc = mdd_lmm_get_locked(env, md2mdd_obj(obj), ma);
121 if (rc || !(ma->ma_valid & MA_LOV))
124 LASSERTF(le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1 ||
125 le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3,
126 "%08x", le32_to_cpu(ma->ma_lmm->lmm_magic));
128 if ((int)le32_to_cpu(ma->ma_lmm->lmm_stripe_count) < 0)
129 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
131 stripe = le32_to_cpu(ma->ma_lmm->lmm_stripe_count);
133 log_credits = stripe * dto_txn_credits[DTO_LOG_REC];
134 mdd_env_info(env)->mti_param.tp_credits += log_credits;
138 int mdd_setattr_txn_param_build(const struct lu_env *env, struct md_object *obj,
139 struct md_attr *ma, enum mdd_txn_op op)
141 struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
144 mdd_txn_param_build(env, mdd, op);
145 if (ma->ma_attr.la_valid & (LA_UID | LA_GID))
146 mdd_env_info(env)->mti_param.tp_credits =
147 dto_txn_credits[DTO_ATTR_SET_CHOWN];
152 static void mdd_txn_init_dto_credits(const struct lu_env *env,
153 struct mdd_device *mdd, int *dto_credits)
156 for (op = 0; op < DTO_NR; op++) {
157 credits = mdd_child_ops(mdd)->dt_credit_get(env, mdd->mdd_child,
159 LASSERT(credits > 0);
160 dto_txn_credits[op] = credits;
164 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd)
168 /* Init credits for each ops. */
169 mdd_txn_init_dto_credits(env, mdd, dto_txn_credits);
171 /* Calculate the mdd credits. */
172 for (op = MDD_TXN_OBJECT_DESTROY_OP; op < MDD_TXN_LAST_OP; op++) {
173 int *c = &mdd->mdd_tod[op].mod_credits;
174 int *dt = dto_txn_credits;
175 mdd->mdd_tod[op].mod_op = op;
177 case MDD_TXN_OBJECT_DESTROY_OP:
179 *c = dt[DTO_OBJECT_DELETE];
181 case MDD_TXN_OBJECT_CREATE_OP:
182 /* OI INSERT + CREATE OBJECT */
183 *c = dt[DTO_INDEX_INSERT] +
184 dt[DTO_OBJECT_CREATE];
186 case MDD_TXN_ATTR_SET_OP:
187 /* ATTR set + XATTR(lsm, lmv) set */
188 *c = dt[DTO_ATTR_SET_BASE] +
191 case MDD_TXN_XATTR_SET_OP:
192 *c = dt[DTO_XATTR_SET];
194 case MDD_TXN_INDEX_INSERT_OP:
195 *c = dt[DTO_INDEX_INSERT];
197 case MDD_TXN_INDEX_DELETE_OP:
198 *c = dt[DTO_INDEX_DELETE];
200 case MDD_TXN_LINK_OP:
201 *c = dt[DTO_INDEX_INSERT];
203 case MDD_TXN_UNLINK_OP:
204 /* delete index + Unlink log +
205 * mdd orphan handling */
206 *c = dt[DTO_INDEX_DELETE] +
207 dt[DTO_INDEX_DELETE] +
208 dt[DTO_INDEX_INSERT] * 2 +
209 dt[DTO_XATTR_SET] * 3;
211 case MDD_TXN_RENAME_OP:
212 /* 2 delete index + 1 insert + Unlink log */
213 *c = 2 * dt[DTO_INDEX_DELETE] +
214 dt[DTO_INDEX_INSERT] +
215 dt[DTO_INDEX_DELETE] +
216 dt[DTO_INDEX_INSERT] * 2 +
217 dt[DTO_XATTR_SET] * 3;
219 case MDD_TXN_RENAME_TGT_OP:
220 /* index insert + index delete */
221 *c = dt[DTO_INDEX_DELETE] +
222 dt[DTO_INDEX_INSERT] +
223 dt[DTO_INDEX_DELETE] +
224 dt[DTO_INDEX_INSERT] * 2 +
225 dt[DTO_XATTR_SET] * 3;
227 case MDD_TXN_CREATE_DATA_OP:
228 /* same as set xattr(lsm) */
229 *c = dt[DTO_XATTR_SET];
231 case MDD_TXN_MKDIR_OP:
232 /* INDEX INSERT + OI INSERT +
233 * CREATE_OBJECT_CREDITS
234 * SET_MD CREDITS is already counted in
235 * CREATE_OBJECT CREDITS
237 *c = 2 * dt[DTO_INDEX_INSERT] +
238 dt[DTO_OBJECT_CREATE];
241 CERROR("Invalid op %d init its credit\n", op);
248 struct thandle* mdd_trans_start(const struct lu_env *env,
249 struct mdd_device *mdd)
251 struct txn_param *p = &mdd_env_info(env)->mti_param;
254 th = mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, p);
258 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
259 int result, struct thandle *handle)
261 handle->th_result = result;
262 mdd_child_ops(mdd)->dt_trans_stop(env, handle);