Whamcloud - gitweb
- take credits for objids update correctly
[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  *  mdd/mdd_handler.c
5  *  Lustre Metadata Server (mdd) routines
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/module.h>
34 #include <linux/jbd.h>
35 #include <obd.h>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
40
41 #include <linux/ldiskfs_fs.h>
42 #include <lustre_mds.h>
43 #include <lustre/lustre_idl.h>
44
45 #include "mdd_internal.h"
46
47 static int dto_txn_credits[DTO_NR];
48
49 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
50                      void *cookie)
51 {
52         struct mdd_device *mdd = cookie;
53         struct obd_device *obd = mdd2obd_dev(mdd);
54         /* Each transaction updates lov objids, the credits should be added for
55          * this */
56         int blk, shift = mdd->mdd_dt_conf.ddp_block_shift;
57         blk = ((obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id) +
58                (1 << shift) - 1) >> shift) + 1;
59
60         /* add lov objids credits */
61         param->tp_credits += blk * dto_txn_credits[DTO_WRITE_BLOCK] +
62                              dto_txn_credits[DTO_WRITE_BASE];
63
64         return 0;
65 }
66
67 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
68                     void *cookie)
69 {
70         struct mdd_device *mdd = cookie;
71         struct obd_device *obd = mdd2obd_dev(mdd);
72
73         LASSERT(obd);
74         return mds_lov_write_objids(obd);
75 }
76
77 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
78                       void *cookie)
79 {
80         return 0;
81 }
82
83 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
84                          enum mdd_txn_op op)
85 {
86         LASSERT(0 <= op && op < MDD_TXN_LAST_OP);
87
88         txn_param_init(&mdd_env_info(env)->mti_param,
89                        mdd->mdd_tod[op].mod_credits);
90 }
91
92 int mdd_log_txn_param_build(const struct lu_env *env, struct md_object *obj,
93                             struct md_attr *ma, enum mdd_txn_op op)
94 {
95         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
96         int rc, log_credits, stripe;
97         ENTRY;
98
99         mdd_txn_param_build(env, mdd, op);
100
101         if (S_ISDIR(lu_object_attr(&obj->mo_lu)))
102                 RETURN(0);
103
104         LASSERT(op == MDD_TXN_UNLINK_OP || op == MDD_TXN_RENAME_OP);
105         rc = mdd_lmm_get_locked(env, md2mdd_obj(obj), ma);
106         if (rc || !(ma->ma_valid & MA_LOV))
107                 RETURN(rc);
108
109         LASSERT(le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC);
110         if ((int)le32_to_cpu(ma->ma_lmm->lmm_stripe_count) < 0)
111                 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
112         else
113                 stripe = le32_to_cpu(ma->ma_lmm->lmm_stripe_count);
114
115         log_credits = stripe * dto_txn_credits[DTO_LOG_REC];
116         mdd_env_info(env)->mti_param.tp_credits += log_credits;
117         RETURN(rc);
118 }
119
120 static void mdd_txn_init_dto_credits(const struct lu_env *env,
121                                      struct mdd_device *mdd, int *dto_credits)
122 {
123         int op, credits;
124         for (op = 0; op < DTO_NR; op++) {
125                 credits = mdd_child_ops(mdd)->dt_credit_get(env, mdd->mdd_child,
126                                                             op);
127                 LASSERT(credits > 0);
128                 dto_txn_credits[op] = credits;
129         }
130 }
131
132 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd)
133 {
134         int op;
135
136         /* Init credits for each ops. */
137         mdd_txn_init_dto_credits(env, mdd, dto_txn_credits);
138
139         /* Calculate the mdd credits. */
140         for (op = MDD_TXN_OBJECT_DESTROY_OP; op < MDD_TXN_LAST_OP; op++) {
141                 int *c = &mdd->mdd_tod[op].mod_credits;
142                 int *dt = dto_txn_credits;
143                 mdd->mdd_tod[op].mod_op = op;
144                 switch(op) {
145                         case MDD_TXN_OBJECT_DESTROY_OP:
146                                 *c = dt[DTO_OBJECT_DELETE];
147                                 break;
148                         case MDD_TXN_OBJECT_CREATE_OP:
149                                 /* OI_INSERT + CREATE OBJECT */
150                                 *c = dt[DTO_INDEX_INSERT] +
151                                         dt[DTO_OBJECT_CREATE];
152                                 break;
153                         case MDD_TXN_ATTR_SET_OP:
154                                 /* ATTR set + XATTR(lsm, lmv) set */
155                                 *c = dt[DTO_ATTR_SET] + dt[DTO_XATTR_SET];
156                                 break;
157                         case MDD_TXN_XATTR_SET_OP:
158                                 *c = dt[DTO_XATTR_SET];
159                                 break;
160                         case MDD_TXN_INDEX_INSERT_OP:
161                                 *c = dt[DTO_INDEX_INSERT];
162                                 break;
163                         case MDD_TXN_INDEX_DELETE_OP:
164                                 *c = dt[DTO_INDEX_DELETE];
165                                 break;
166                         case MDD_TXN_LINK_OP:
167                                 *c = dt[DTO_INDEX_INSERT];
168                                 break;
169                         case MDD_TXN_UNLINK_OP:
170                                 /* delete index + Unlink log */
171                                 *c = dt[DTO_INDEX_DELETE];
172                                 break;
173                         case MDD_TXN_RENAME_OP:
174                                 /* 2 delete index + 1 insert + Unlink log */
175                                 *c = 2 * dt[DTO_INDEX_DELETE] +
176                                         dt[DTO_INDEX_INSERT];
177                                 break;
178                         case MDD_TXN_RENAME_TGT_OP:
179                                 /* index insert + index delete */
180                                 *c = dt[DTO_INDEX_DELETE] +
181                                      dt[DTO_INDEX_INSERT];
182                                 break;
183                         case MDD_TXN_CREATE_DATA_OP:
184                                 /* same as set xattr(lsm) */
185                                 *c = dt[DTO_XATTR_SET];
186                                 break;
187                         case MDD_TXN_MKDIR_OP:
188                                 /* INDEX INSERT + OI INSERT +
189                                  * CREATE_OBJECT_CREDITS
190                                  * SET_MD CREDITS is already counted in
191                                  * CREATE_OBJECT CREDITS
192                                  */
193                                  *c = 2 * dt[DTO_INDEX_INSERT] +
194                                          dt[DTO_OBJECT_CREATE];
195                                 break;
196                         default:
197                                 CERROR("Invalid op %d init its credit\n", op);
198                                 LBUG();
199                 }
200         }
201         RETURN(0);
202 }
203
204 struct thandle* mdd_trans_start(const struct lu_env *env,
205                                 struct mdd_device *mdd)
206 {
207         struct txn_param *p = &mdd_env_info(env)->mti_param;
208         struct thandle *th;
209
210         th = mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, p);
211         return th;
212 }
213
214 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
215                     int result, struct thandle *handle)
216 {
217         handle->th_result = result;
218         mdd_child_ops(mdd)->dt_trans_stop(env, handle);
219 }