71817af75396faf1dd26eaf7067710545b4ad7fd
[deliverable/linux.git] / drivers / staging / lustre / lustre / obdclass / llog_obd.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 */
36
37 #define DEBUG_SUBSYSTEM S_LOG
38
39
40 #include <obd_class.h>
41 #include <lustre_log.h>
42 #include "llog_internal.h"
43
44 /* helper functions for calling the llog obd methods */
45 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
46 {
47 struct llog_ctxt *ctxt;
48
49 OBD_ALLOC_PTR(ctxt);
50 if (!ctxt)
51 return NULL;
52
53 ctxt->loc_obd = obd;
54 atomic_set(&ctxt->loc_refcount, 1);
55
56 return ctxt;
57 }
58
59 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
60 {
61 if (ctxt->loc_exp) {
62 class_export_put(ctxt->loc_exp);
63 ctxt->loc_exp = NULL;
64 }
65 if (ctxt->loc_imp) {
66 class_import_put(ctxt->loc_imp);
67 ctxt->loc_imp = NULL;
68 }
69 OBD_FREE_PTR(ctxt);
70 }
71
72 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt)
73 {
74 struct obd_llog_group *olg = ctxt->loc_olg;
75 struct obd_device *obd;
76 int rc = 0;
77
78 spin_lock(&olg->olg_lock);
79 if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
80 spin_unlock(&olg->olg_lock);
81 return rc;
82 }
83 olg->olg_ctxts[ctxt->loc_idx] = NULL;
84 spin_unlock(&olg->olg_lock);
85
86 obd = ctxt->loc_obd;
87 spin_lock(&obd->obd_dev_lock);
88 /* sync with llog ctxt user thread */
89 spin_unlock(&obd->obd_dev_lock);
90
91 /* obd->obd_starting is needed for the case of cleanup
92 * in error case while obd is starting up. */
93 LASSERTF(obd->obd_starting == 1 ||
94 obd->obd_stopping == 1 || obd->obd_set_up == 0,
95 "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
96 !!obd->obd_stopping, !!obd->obd_set_up);
97
98 /* cleanup the llog ctxt here */
99 if (CTXTP(ctxt, cleanup))
100 rc = CTXTP(ctxt, cleanup)(env, ctxt);
101
102 llog_ctxt_destroy(ctxt);
103 wake_up(&olg->olg_waitq);
104 return rc;
105 }
106 EXPORT_SYMBOL(__llog_ctxt_put);
107
108 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
109 {
110 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
111 struct obd_llog_group *olg;
112 int rc, idx;
113
114 LASSERT(ctxt != NULL);
115 LASSERT(ctxt != LP_POISON);
116
117 olg = ctxt->loc_olg;
118 LASSERT(olg != NULL);
119 LASSERT(olg != LP_POISON);
120
121 idx = ctxt->loc_idx;
122
123 /*
124 * Banlance the ctxt get when calling llog_cleanup()
125 */
126 LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
127 LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
128 llog_ctxt_put(ctxt);
129
130 /*
131 * Try to free the ctxt.
132 */
133 rc = __llog_ctxt_put(env, ctxt);
134 if (rc)
135 CERROR("Error %d while cleaning up ctxt %p\n",
136 rc, ctxt);
137
138 l_wait_event(olg->olg_waitq,
139 llog_group_ctxt_null(olg, idx), &lwi);
140
141 return rc;
142 }
143 EXPORT_SYMBOL(llog_cleanup);
144
145 int llog_setup(const struct lu_env *env, struct obd_device *obd,
146 struct obd_llog_group *olg, int index,
147 struct obd_device *disk_obd, struct llog_operations *op)
148 {
149 struct llog_ctxt *ctxt;
150 int rc = 0;
151
152 if (index < 0 || index >= LLOG_MAX_CTXTS)
153 return -EINVAL;
154
155 LASSERT(olg != NULL);
156
157 ctxt = llog_new_ctxt(obd);
158 if (!ctxt)
159 return -ENOMEM;
160
161 ctxt->loc_obd = obd;
162 ctxt->loc_olg = olg;
163 ctxt->loc_idx = index;
164 ctxt->loc_logops = op;
165 mutex_init(&ctxt->loc_mutex);
166 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
167 ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
168
169 rc = llog_group_set_ctxt(olg, ctxt, index);
170 if (rc) {
171 llog_ctxt_destroy(ctxt);
172 if (rc == -EEXIST) {
173 ctxt = llog_group_get_ctxt(olg, index);
174 if (ctxt) {
175 /*
176 * mds_lov_update_desc() might call here multiple
177 * times. So if the llog is already set up then
178 * don't to do it again.
179 */
180 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
181 obd->obd_name, index);
182 LASSERT(ctxt->loc_olg == olg);
183 LASSERT(ctxt->loc_obd == obd);
184 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
185 LASSERT(ctxt->loc_logops == op);
186 llog_ctxt_put(ctxt);
187 }
188 rc = 0;
189 }
190 return rc;
191 }
192
193 if (op->lop_setup) {
194 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
195 rc = -EOPNOTSUPP;
196 else
197 rc = op->lop_setup(env, obd, olg, index, disk_obd);
198 }
199
200 if (rc) {
201 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
202 obd->obd_name, index, op->lop_setup, rc);
203 llog_group_clear_ctxt(olg, index);
204 llog_ctxt_destroy(ctxt);
205 } else {
206 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
207 obd->obd_name, index);
208 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
209 }
210
211 return rc;
212 }
213 EXPORT_SYMBOL(llog_setup);
214
215 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
216 {
217 int rc = 0;
218
219 if (!ctxt)
220 return 0;
221
222 if (CTXTP(ctxt, sync))
223 rc = CTXTP(ctxt, sync)(ctxt, exp, flags);
224
225 return rc;
226 }
227 EXPORT_SYMBOL(llog_sync);
228
229 int llog_obd_add(const struct lu_env *env, struct llog_ctxt *ctxt,
230 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
231 struct llog_cookie *logcookies, int numcookies)
232 {
233 int raised, rc;
234
235 if (!ctxt) {
236 CERROR("No ctxt\n");
237 return -ENODEV;
238 }
239
240 if (ctxt->loc_flags & LLOG_CTXT_FLAG_UNINITIALIZED)
241 return -ENXIO;
242
243 CTXT_CHECK_OP(ctxt, obd_add, -EOPNOTSUPP);
244 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
245 if (!raised)
246 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
247 rc = CTXTP(ctxt, obd_add)(env, ctxt, rec, lsm, logcookies,
248 numcookies);
249 if (!raised)
250 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
251 return rc;
252 }
253 EXPORT_SYMBOL(llog_obd_add);
254
255 int llog_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
256 struct lov_stripe_md *lsm, int count,
257 struct llog_cookie *cookies, int flags)
258 {
259 int rc;
260
261 if (!ctxt) {
262 CERROR("No ctxt\n");
263 return -ENODEV;
264 }
265
266 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
267 rc = CTXTP(ctxt, cancel)(env, ctxt, lsm, count, cookies, flags);
268 return rc;
269 }
270 EXPORT_SYMBOL(llog_cancel);
271
272 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
273 struct obd_device *disk_obd, int *index)
274 {
275 int rc;
276
277 OBD_CHECK_DT_OP(obd, llog_init, 0);
278 OBD_COUNTER_INCREMENT(obd, llog_init);
279
280 rc = OBP(obd, llog_init)(obd, olg, disk_obd, index);
281 return rc;
282 }
283 EXPORT_SYMBOL(obd_llog_init);
284
285 int obd_llog_finish(struct obd_device *obd, int count)
286 {
287 int rc;
288
289 OBD_CHECK_DT_OP(obd, llog_finish, 0);
290 OBD_COUNTER_INCREMENT(obd, llog_finish);
291
292 rc = OBP(obd, llog_finish)(obd, count);
293 return rc;
294 }
295 EXPORT_SYMBOL(obd_llog_finish);
296
297 /* context key constructor/destructor: llog_key_init, llog_key_fini */
298 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
299 /* context key: llog_thread_key */
300 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
301 LU_KEY_INIT_GENERIC(llog);
302 EXPORT_SYMBOL(llog_thread_key);
303
304 int llog_info_init(void)
305 {
306 llog_key_init_generic(&llog_thread_key, NULL);
307 lu_context_key_register(&llog_thread_key);
308 return 0;
309 }
310
311 void llog_info_fini(void)
312 {
313 lu_context_key_degister(&llog_thread_key);
314 }
This page took 0.037425 seconds and 5 git commands to generate.