Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 int mdd_txn_start_cb(const struct lu_env *env, struct txn_param *param,
48                      void *cookie)
49 {
50         return 0;
51 }
52
53 int mdd_txn_stop_cb(const struct lu_env *env, struct thandle *txn,
54                     void *cookie)
55 {
56         struct mdd_device *mdd = cookie;
57         struct obd_device *obd = mdd2obd_dev(mdd);
58
59         LASSERT(obd);
60         return mds_lov_write_objids(obd);
61 }
62
63 int mdd_txn_commit_cb(const struct lu_env *env, struct thandle *txn,
64                       void *cookie)
65 {
66         return 0;
67 }
68
69 static int dto_txn_credits[DTO_NR];
70 void mdd_txn_param_build(const struct lu_env *env, struct mdd_device *mdd,
71                          enum mdd_txn_op op)
72 {
73         LASSERT(0 <= op && op < MDD_TXN_LAST_OP);
74
75         txn_param_init(&mdd_env_info(env)->mti_param,
76                        mdd->mdd_tod[op].mod_credits);
77 }
78
79 int mdd_log_txn_param_build(const struct lu_env *env, struct md_object *obj,
80                             struct md_attr *ma, enum mdd_txn_op op)
81 {
82         struct mdd_device *mdd = mdo2mdd(&md2mdd_obj(obj)->mod_obj);
83         int rc, log_credits, stripe;
84         ENTRY;
85
86         mdd_txn_param_build(env, mdd, op);
87
88         if (S_ISDIR(lu_object_attr(&obj->mo_lu)))
89                 RETURN(0);
90
91         LASSERT(op == MDD_TXN_UNLINK_OP || op == MDD_TXN_RENAME_OP);
92         rc = mdd_lmm_get_locked(env, md2mdd_obj(obj), ma);
93         if (rc || !(ma->ma_valid & MA_LOV))
94                 RETURN(rc);
95
96         LASSERT(le32_to_cpu(ma->ma_lmm->lmm_magic) == LOV_MAGIC);
97         if ((int)le32_to_cpu(ma->ma_lmm->lmm_stripe_count) < 0)
98                 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
99         else
100                 stripe = le32_to_cpu(ma->ma_lmm->lmm_stripe_count);
101
102         log_credits = stripe * dto_txn_credits[DTO_LOG_REC];
103         mdd_env_info(env)->mti_param.tp_credits += log_credits;
104         RETURN(rc);
105 }
106
107 static void mdd_txn_init_dto_credits(const struct lu_env *env,
108                                      struct mdd_device *mdd, int *dto_credits)
109 {
110         int op, credits;
111         for (op = 0; op < DTO_NR; op++) {
112                 credits = mdd_child_ops(mdd)->dt_credit_get(env, mdd->mdd_child,
113                                                             op);
114                 LASSERT(credits > 0);
115                 dto_txn_credits[op] = credits;
116         }
117 }
118
119 int mdd_txn_init_credits(const struct lu_env *env, struct mdd_device *mdd)
120 {
121         int op;
122
123         /* Init credits for each ops. */
124         mdd_txn_init_dto_credits(env, mdd, dto_txn_credits);
125
126         /* Calculate the mdd credits. */
127         for (op = MDD_TXN_OBJECT_DESTROY_OP; op < MDD_TXN_LAST_OP; op++) {
128                 int *c = &mdd->mdd_tod[op].mod_credits;
129                 int *dt = dto_txn_credits;
130                 mdd->mdd_tod[op].mod_op = op;
131                 switch(op) {
132                         case MDD_TXN_OBJECT_DESTROY_OP:
133                                 *c = dt[DTO_OBJECT_DELETE];
134                                 break;
135                         case MDD_TXN_OBJECT_CREATE_OP:
136                                 /* OI_INSERT + CREATE OBJECT */
137                                 *c = dt[DTO_INDEX_INSERT] +
138                                         dt[DTO_OBJECT_CREATE];
139                                 break;
140                         case MDD_TXN_ATTR_SET_OP:
141                                 /* ATTR set + XATTR(lsm, lmv) set */
142                                 *c = dt[DTO_ATTR_SET] + dt[DTO_XATTR_SET];
143                                 break;
144                         case MDD_TXN_XATTR_SET_OP:
145                                 *c = dt[DTO_XATTR_SET];
146                                 break;
147                         case MDD_TXN_INDEX_INSERT_OP:
148                                 *c = dt[DTO_INDEX_INSERT];
149                                 break;
150                         case MDD_TXN_INDEX_DELETE_OP:
151                                 *c = dt[DTO_INDEX_DELETE];
152                                 break;
153                         case MDD_TXN_LINK_OP:
154                                 *c = dt[DTO_INDEX_INSERT];
155                                 break;
156                         case MDD_TXN_UNLINK_OP:
157                                 /* delete index + Unlink log */
158                                 *c = dt[DTO_INDEX_DELETE];
159                                 break;
160                         case MDD_TXN_RENAME_OP:
161                                 /* 2 delete index + 1 insert + Unlink log */
162                                 *c = 2 * dt[DTO_INDEX_DELETE] +
163                                         dt[DTO_INDEX_INSERT];
164                                 break;
165                         case MDD_TXN_RENAME_TGT_OP:
166                                 /* index insert + index delete */
167                                 *c = dt[DTO_INDEX_DELETE] +
168                                      dt[DTO_INDEX_INSERT];
169                                 break;
170                         case MDD_TXN_CREATE_DATA_OP:
171                                 /* same as set xattr(lsm) */
172                                 *c = dt[DTO_XATTR_SET];
173                                 break;
174                         case MDD_TXN_MKDIR_OP:
175                                 /* INDEX INSERT + OI INSERT +
176                                  * CREATE_OBJECT_CREDITS
177                                  * SET_MD CREDITS is already counted in
178                                  * CREATE_OBJECT CREDITS
179                                  */
180                                  *c = 2 * dt[DTO_INDEX_INSERT] +
181                                          dt[DTO_OBJECT_CREATE];
182                                 break;
183                         default:
184                                 CERROR("Invalid op %d init its credit\n", op);
185                                 LBUG();
186                 }
187         }
188         RETURN(0);
189 }
190
191 struct thandle* mdd_trans_start(const struct lu_env *env,
192                                 struct mdd_device *mdd)
193 {
194         struct txn_param *p = &mdd_env_info(env)->mti_param;
195         struct thandle *th;
196
197         th = mdd_child_ops(mdd)->dt_trans_start(env, mdd->mdd_child, p);
198         return th;
199 }
200
201 void mdd_trans_stop(const struct lu_env *env, struct mdd_device *mdd,
202                     int result, struct thandle *handle)
203 {
204         handle->th_result = result;
205         mdd_child_ops(mdd)->dt_trans_stop(env, handle);
206 }