Whamcloud - gitweb
Land b_head_quota onto HEAD (20081116_0105)
[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 #include <linux/jbd.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55
56 #include <linux/ldiskfs_fs.h>
57 #include <lustre_mds.h>
58 #include <lustre/lustre_idl.h>
59
60 #include "mdd_internal.h"
61
62 static int dto_txn_credits[DTO_NR];
63
64 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
65                      void *cookie)
66 {
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
70          * this */
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;
74
75         /* add lov objids credits */
76         param->tp_credits += blk * dto_txn_credits[DTO_WRITE_BLOCK] +
77                              dto_txn_credits[DTO_WRITE_BASE];
78
79         return 0;
80 }
81
82 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
83                     void *cookie)
84 {
85         struct mdd_device *mdd = cookie;
86         struct obd_device *obd = mdd2obd_dev(mdd);
87
88         LASSERT(obd);
89         return mds_lov_write_objids(obd);
90 }
91
92 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
93                       void *cookie)
94 {
95         return 0;
96 }
97
98 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
99                          enum mdd_txn_op op)
100 {
101         LASSERT(0 <= op && op < MDD_TXN_LAST_OP);
102
103         txn_param_init(&mdd_env_info(env)->mti_param,
104                        mdd->mdd_tod[op].mod_credits);
105 }
106
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)
109 {
110         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
111         int rc, log_credits, stripe;
112         ENTRY;
113
114         mdd_txn_param_build(env, mdd, op);
115
116         if (S_ISDIR(lu_object_attr(&obj->mo_lu)))
117                 RETURN(0);
118
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))
122                 RETURN(rc);
123
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));
127
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;
130         else
131                 stripe = le32_to_cpu(ma->ma_lmm->lmm_stripe_count);
132
133         log_credits = stripe * dto_txn_credits[DTO_LOG_REC];
134         mdd_env_info(env)->mti_param.tp_credits += log_credits;
135         RETURN(rc);
136 }
137
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)
140 {
141         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
142         ENTRY;
143
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];
148
149         RETURN(0);
150 }
151
152 static void mdd_txn_init_dto_credits(const struct lu_env *env,
153                                      struct mdd_device *mdd, int *dto_credits)
154 {
155         int op, credits;
156         for (op = 0; op < DTO_NR; op++) {
157                 credits = mdd_child_ops(mdd)->dt_credit_get(env, mdd->mdd_child,
158                                                             op);
159                 LASSERT(credits > 0);
160                 dto_txn_credits[op] = credits;
161         }
162 }
163
164 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd)
165 {
166         int op;
167
168         /* Init credits for each ops. */
169         mdd_txn_init_dto_credits(env, mdd, dto_txn_credits);
170
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;
176                 switch(op) {
177                         case MDD_TXN_OBJECT_DESTROY_OP:
178                                 /* Unused now */
179                                 *c = dt[DTO_OBJECT_DELETE];
180                                 break;
181                         case MDD_TXN_OBJECT_CREATE_OP:
182                                 /* OI INSERT + CREATE OBJECT */
183                                 *c = dt[DTO_INDEX_INSERT] +
184                                      dt[DTO_OBJECT_CREATE];
185                                 break;
186                         case MDD_TXN_ATTR_SET_OP:
187                                 /* ATTR set + XATTR(lsm, lmv) set */
188                                 *c = dt[DTO_ATTR_SET_BASE] +
189                                      dt[DTO_XATTR_SET];
190                                 break;
191                         case MDD_TXN_XATTR_SET_OP:
192                                 *c = dt[DTO_XATTR_SET];
193                                 break;
194                         case MDD_TXN_INDEX_INSERT_OP:
195                                 *c = dt[DTO_INDEX_INSERT];
196                                 break;
197                         case MDD_TXN_INDEX_DELETE_OP:
198                                 *c = dt[DTO_INDEX_DELETE];
199                                 break;
200                         case MDD_TXN_LINK_OP:
201                                 *c = dt[DTO_INDEX_INSERT];
202                                 break;
203                         case MDD_TXN_UNLINK_OP:
204                                 /* delete index + Unlink log */
205                                 *c = dt[DTO_INDEX_DELETE];
206                                 break;
207                         case MDD_TXN_RENAME_OP:
208                                 /* 2 delete index + 1 insert + Unlink log */
209                                 *c = 2 * dt[DTO_INDEX_DELETE] +
210                                          dt[DTO_INDEX_INSERT];
211                                 break;
212                         case MDD_TXN_RENAME_TGT_OP:
213                                 /* index insert + index delete */
214                                 *c = dt[DTO_INDEX_DELETE] +
215                                      dt[DTO_INDEX_INSERT];
216                                 break;
217                         case MDD_TXN_CREATE_DATA_OP:
218                                 /* same as set xattr(lsm) */
219                                 *c = dt[DTO_XATTR_SET];
220                                 break;
221                         case MDD_TXN_MKDIR_OP:
222                                 /* INDEX INSERT + OI INSERT +
223                                  * CREATE_OBJECT_CREDITS
224                                  * SET_MD CREDITS is already counted in
225                                  * CREATE_OBJECT CREDITS
226                                  */
227                                  *c = 2 * dt[DTO_INDEX_INSERT] +
228                                           dt[DTO_OBJECT_CREATE];
229                                 break;
230                         default:
231                                 CERROR("Invalid op %d init its credit\n", op);
232                                 LBUG();
233                 }
234         }
235         RETURN(0);
236 }
237
238 struct thandle* mdd_trans_start(const struct lu_env *env,
239                                 struct mdd_device *mdd)
240 {
241         struct txn_param *p = &mdd_env_info(env)->mti_param;
242         struct thandle *th;
243         
244         th = mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, p);
245         return th;
246 }
247
248 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
249                     int result, struct thandle *handle)
250 {
251         handle->th_result = result;
252         mdd_child_ops(mdd)->dt_trans_stop(env, handle);
253 }