Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / mdd / mdd_trans.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_trans.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #ifdef HAVE_EXT4_LDISKFS
50 #include <ldiskfs/ldiskfs_jbd2.h>
51 #else
52 #include <linux/jbd.h>
53 #endif
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
59
60 #ifdef HAVE_EXT4_LDISKFS
61 #include <ldiskfs/ldiskfs.h>
62 #else
63 #include <linux/ldiskfs_fs.h>
64 #endif
65 #include <lustre_mds.h>
66 #include <lustre/lustre_idl.h>
67
68 #include "mdd_internal.h"
69
70 static int dto_txn_credits[DTO_NR];
71
72 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
73                      void *cookie)
74 {
75         struct mdd_device *mdd = cookie;
76         struct obd_device *obd = mdd2obd_dev(mdd);
77         /* Each transaction updates lov objids, the credits should be added for
78          * this */
79         int blk, shift = mdd->mdd_dt_conf.ddp_block_shift;
80         blk = ((obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id) +
81                (1 << shift) - 1) >> shift) + 1;
82
83         /* add lov objids credits */
84         param->tp_credits += blk * dto_txn_credits[DTO_WRITE_BLOCK] +
85                              dto_txn_credits[DTO_WRITE_BASE];
86
87         return 0;
88 }
89
90 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
91                     void *cookie)
92 {
93         struct mdd_device *mdd = cookie;
94         struct obd_device *obd = mdd2obd_dev(mdd);
95
96         LASSERT(obd);
97         return mds_lov_write_objids(obd);
98 }
99
100 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
101                       void *cookie)
102 {
103         return 0;
104 }
105
106 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
107                          enum mdd_txn_op op)
108 {
109         LASSERT(0 <= op && op < MDD_TXN_LAST_OP);
110
111         txn_param_init(&mdd_env_info(env)->mti_param,
112                        mdd->mdd_tod[op].mod_credits);
113 }
114
115 int mdd_log_txn_param_build(const struct lu_env *env, struct md_object *obj,
116                             struct md_attr *ma, enum mdd_txn_op op)
117 {
118         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
119         int rc, log_credits, stripe;
120         ENTRY;
121
122         mdd_txn_param_build(env, mdd, op);
123
124         if (S_ISDIR(lu_object_attr(&obj->mo_lu)))
125                 RETURN(0);
126
127         LASSERT(op == MDD_TXN_UNLINK_OP || op == MDD_TXN_RENAME_OP);
128         rc = mdd_lmm_get_locked(env, md2mdd_obj(obj), ma);
129         if (rc || !(ma->ma_valid & MA_LOV))
130                 RETURN(rc);
131
132         LASSERTF(le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V1 ||
133                  le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC_V3,
134                  "%08x", le32_to_cpu(ma->ma_lmm->lmm_magic));
135
136         if ((int)le32_to_cpu(ma->ma_lmm->lmm_stripe_count) < 0)
137                 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
138         else
139                 stripe = le32_to_cpu(ma->ma_lmm->lmm_stripe_count);
140
141         log_credits = stripe * dto_txn_credits[DTO_LOG_REC];
142         mdd_env_info(env)->mti_param.tp_credits += log_credits;
143         RETURN(rc);
144 }
145
146 int mdd_setattr_txn_param_build(const struct lu_env *env, struct md_object *obj,
147                                 struct md_attr *ma, enum mdd_txn_op op)
148 {
149         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
150         ENTRY;
151
152         mdd_txn_param_build(env, mdd, op);
153         if (ma->ma_attr.la_valid & (LA_UID | LA_GID))
154                 mdd_env_info(env)->mti_param.tp_credits =
155                                         dto_txn_credits[DTO_ATTR_SET_CHOWN];
156
157         RETURN(0);
158 }
159
160 static void mdd_txn_init_dto_credits(const struct lu_env *env,
161                                      struct mdd_device *mdd, int *dto_credits)
162 {
163         int op, credits;
164         for (op = 0; op < DTO_NR; op++) {
165                 credits = mdd_child_ops(mdd)->dt_credit_get(env, mdd->mdd_child,
166                                                             op);
167                 LASSERT(credits > 0);
168                 dto_txn_credits[op] = credits;
169         }
170 }
171
172 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd)
173 {
174         int op;
175
176         /* Init credits for each ops. */
177         mdd_txn_init_dto_credits(env, mdd, dto_txn_credits);
178
179         /* Calculate the mdd credits. */
180         for (op = MDD_TXN_OBJECT_DESTROY_OP; op < MDD_TXN_LAST_OP; op++) {
181                 int *c = &mdd->mdd_tod[op].mod_credits;
182                 int *dt = dto_txn_credits;
183                 mdd->mdd_tod[op].mod_op = op;
184                 switch(op) {
185                         case MDD_TXN_OBJECT_DESTROY_OP:
186                                 /* Unused now */
187                                 *c = dt[DTO_OBJECT_DELETE];
188                                 break;
189                         case MDD_TXN_OBJECT_CREATE_OP:
190                                 /* OI INSERT + CREATE OBJECT */
191                                 *c = dt[DTO_INDEX_INSERT] +
192                                      dt[DTO_OBJECT_CREATE];
193                                 break;
194                         case MDD_TXN_ATTR_SET_OP:
195                                 /* ATTR set + XATTR(lsm, lmv) set */
196                                 *c = dt[DTO_ATTR_SET_BASE] +
197                                      dt[DTO_XATTR_SET];
198                                 break;
199                         case MDD_TXN_XATTR_SET_OP:
200                                 *c = dt[DTO_XATTR_SET];
201                                 break;
202                         case MDD_TXN_INDEX_INSERT_OP:
203                                 *c = dt[DTO_INDEX_INSERT];
204                                 break;
205                         case MDD_TXN_INDEX_DELETE_OP:
206                                 *c = dt[DTO_INDEX_DELETE];
207                                 break;
208                         case MDD_TXN_LINK_OP:
209                                 *c = dt[DTO_INDEX_INSERT];
210                                 break;
211                         case MDD_TXN_UNLINK_OP:
212                                 /* delete index + Unlink log +
213                                  * mdd orphan handling */
214                                 *c = dt[DTO_INDEX_DELETE] +
215                                         dt[DTO_INDEX_DELETE] +
216                                         dt[DTO_INDEX_INSERT] * 2 +
217                                         dt[DTO_XATTR_SET] * 3;
218                                 break;
219                         case MDD_TXN_RENAME_OP:
220                                 /* 2 delete index + 1 insert + Unlink log */
221                                 *c = 2 * 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;
226                                 break;
227                         case MDD_TXN_RENAME_TGT_OP:
228                                 /* index insert + index delete */
229                                 *c = dt[DTO_INDEX_DELETE] +
230                                         dt[DTO_INDEX_INSERT] +
231                                         dt[DTO_INDEX_DELETE] +
232                                         dt[DTO_INDEX_INSERT] * 2 +
233                                         dt[DTO_XATTR_SET] * 3;
234                                 break;
235                         case MDD_TXN_CREATE_DATA_OP:
236                                 /* same as set xattr(lsm) */
237                                 *c = dt[DTO_XATTR_SET];
238                                 break;
239                         case MDD_TXN_MKDIR_OP:
240                                 /* INDEX INSERT + OI INSERT +
241                                  * CREATE_OBJECT_CREDITS
242                                  * SET_MD CREDITS is already counted in
243                                  * CREATE_OBJECT CREDITS
244                                  */
245                                  *c = 2 * dt[DTO_INDEX_INSERT] +
246                                           dt[DTO_OBJECT_CREATE];
247                                 break;
248                         default:
249                                 CERROR("Invalid op %d init its credit\n", op);
250                                 LBUG();
251                 }
252         }
253         RETURN(0);
254 }
255
256 struct thandle* mdd_trans_start(const struct lu_env *env,
257                                 struct mdd_device *mdd)
258 {
259         struct txn_param *p = &mdd_env_info(env)->mti_param;
260         struct thandle *th;
261         
262         th = mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, p);
263         return th;
264 }
265
266 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
267                     int result, struct thandle *handle)
268 {
269         handle->th_result = result;
270         mdd_child_ops(mdd)->dt_trans_stop(env, handle);
271 }