Whamcloud - gitweb
LU-1302 llog: pass lu_env as parametr in llog functions
[fs/lustre-release.git] / 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, Whamcloud, Inc.
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 #ifndef __KERNEL__
40 #include <liblustre.h>
41 #endif
42
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <libcfs/list.h>
46 #include "llog_internal.h"
47
48 /* helper functions for calling the llog obd methods */
49 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
50 {
51         struct llog_ctxt *ctxt;
52
53         OBD_ALLOC_PTR(ctxt);
54         if (!ctxt)
55                 return NULL;
56
57         ctxt->loc_obd = obd;
58         cfs_atomic_set(&ctxt->loc_refcount, 1);
59
60         return ctxt;
61 }
62
63 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
64 {
65         if (ctxt->loc_exp) {
66                 class_export_put(ctxt->loc_exp);
67                 ctxt->loc_exp = NULL;
68         }
69         if (ctxt->loc_imp) {
70                 class_import_put(ctxt->loc_imp);
71                 ctxt->loc_imp = NULL;
72         }
73         LASSERT(ctxt->loc_llcd == NULL);
74         OBD_FREE_PTR(ctxt);
75 }
76
77 int __llog_ctxt_put(struct llog_ctxt *ctxt)
78 {
79         struct obd_llog_group *olg = ctxt->loc_olg;
80         struct obd_device *obd;
81         int rc = 0;
82
83         cfs_spin_lock(&olg->olg_lock);
84         if (!cfs_atomic_dec_and_test(&ctxt->loc_refcount)) {
85                 cfs_spin_unlock(&olg->olg_lock);
86                 return rc;
87         }
88         olg->olg_ctxts[ctxt->loc_idx] = NULL;
89         cfs_spin_unlock(&olg->olg_lock);
90
91         if (ctxt->loc_lcm)
92                 lcm_put(ctxt->loc_lcm);
93
94         obd = ctxt->loc_obd;
95         cfs_spin_lock(&obd->obd_dev_lock);
96         /* sync with llog ctxt user thread */
97         cfs_spin_unlock(&obd->obd_dev_lock);
98
99         /* obd->obd_starting is needed for the case of cleanup
100          * in error case while obd is starting up. */
101         LASSERTF(obd->obd_starting == 1 ||
102                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
103                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
104                  !!obd->obd_stopping, !!obd->obd_set_up);
105
106         /* cleanup the llog ctxt here */
107         if (CTXTP(ctxt, cleanup))
108                 rc = CTXTP(ctxt, cleanup)(NULL, ctxt);
109
110         llog_ctxt_destroy(ctxt);
111         cfs_waitq_signal(&olg->olg_waitq);
112         return rc;
113 }
114 EXPORT_SYMBOL(__llog_ctxt_put);
115
116 int llog_cleanup(struct llog_ctxt *ctxt)
117 {
118         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
119         struct obd_llog_group *olg;
120         int rc, idx;
121         ENTRY;
122
123         LASSERT(ctxt != NULL);
124         LASSERT(ctxt != LP_POISON);
125
126         olg = ctxt->loc_olg;
127         LASSERT(olg != NULL);
128         LASSERT(olg != LP_POISON);
129
130         idx = ctxt->loc_idx;
131
132         /* 
133          * Banlance the ctxt get when calling llog_cleanup()
134          */
135         LASSERT(cfs_atomic_read(&ctxt->loc_refcount) < LI_POISON);
136         LASSERT(cfs_atomic_read(&ctxt->loc_refcount) > 1);
137         llog_ctxt_put(ctxt);
138
139         /* 
140          * Try to free the ctxt. 
141          */
142         rc = __llog_ctxt_put(ctxt);
143         if (rc)
144                 CERROR("Error %d while cleaning up ctxt %p\n",
145                        rc, ctxt);
146
147         l_wait_event(olg->olg_waitq,
148                      llog_group_ctxt_null(olg, idx), &lwi);
149
150         RETURN(rc);
151 }
152 EXPORT_SYMBOL(llog_cleanup);
153
154 int llog_setup_named(struct obd_device *obd,  struct obd_llog_group *olg,
155                      int index, struct obd_device *disk_obd, int count,
156                      struct llog_logid *logid, const char *logname,
157                      struct llog_operations *op)
158 {
159         struct llog_ctxt *ctxt;
160         int rc = 0;
161         ENTRY;
162
163         if (index < 0 || index >= LLOG_MAX_CTXTS)
164                 RETURN(-EINVAL);
165
166         LASSERT(olg != NULL);
167
168         ctxt = llog_new_ctxt(obd);
169         if (!ctxt)
170                 RETURN(-ENOMEM);
171
172         ctxt->loc_obd = obd;
173         ctxt->loc_olg = olg;
174         ctxt->loc_idx = index;
175         ctxt->loc_logops = op;
176         cfs_mutex_init(&ctxt->loc_mutex);
177         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
178         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
179
180         rc = llog_group_set_ctxt(olg, ctxt, index);
181         if (rc) {
182                 llog_ctxt_destroy(ctxt);
183                 if (rc == -EEXIST) {
184                         ctxt = llog_group_get_ctxt(olg, index);
185                         if (ctxt) {
186                                 /*
187                                  * mds_lov_update_desc() might call here multiple
188                                  * times. So if the llog is already set up then
189                                  * don't to do it again. 
190                                  */
191                                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
192                                        obd->obd_name, index);
193                                 LASSERT(ctxt->loc_olg == olg);
194                                 LASSERT(ctxt->loc_obd == obd);
195                                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
196                                 LASSERT(ctxt->loc_logops == op);
197                                 llog_ctxt_put(ctxt);
198                         }
199                         rc = 0;
200                 }
201                 RETURN(rc);
202         }
203
204         if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP)) {
205                 rc = -ENOTSUPP;
206         } else {
207                 if (op->lop_setup)
208                         rc = op->lop_setup(NULL, obd, olg, index, disk_obd,
209                                            count, logid, logname);
210         }
211
212         if (rc) {
213                 CERROR("obd %s ctxt %d lop_setup=%p failed %d\n",
214                        obd->obd_name, index, op->lop_setup, rc);
215                 llog_ctxt_put(ctxt);
216         } else {
217                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
218                        obd->obd_name, index);
219                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
220         }
221
222         RETURN(rc);
223 }
224 EXPORT_SYMBOL(llog_setup_named);
225
226 int llog_setup(struct obd_device *obd,  struct obd_llog_group *olg,
227                int index, struct obd_device *disk_obd, int count,
228                struct llog_logid *logid, struct llog_operations *op)
229 {
230         return llog_setup_named(obd,olg,index,disk_obd,count,logid,NULL,op);
231 }
232 EXPORT_SYMBOL(llog_setup);
233
234 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
235 {
236         int rc = 0;
237         ENTRY;
238
239         if (!ctxt)
240                 RETURN(0);
241
242         if (CTXTP(ctxt, sync))
243                 rc = CTXTP(ctxt, sync)(ctxt, exp, flags);
244
245         RETURN(rc);
246 }
247 EXPORT_SYMBOL(llog_sync);
248
249 int llog_add(const struct lu_env *env, struct llog_ctxt *ctxt,
250              struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
251              struct llog_cookie *logcookies, int numcookies)
252 {
253         int raised, rc;
254         ENTRY;
255
256         if (!ctxt) {
257                 CERROR("No ctxt\n");
258                 RETURN(-ENODEV);
259         }
260
261         if (ctxt->loc_flags & LLOG_CTXT_FLAG_UNINITIALIZED)
262                 RETURN(-ENXIO);
263
264
265         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
266         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
267         if (!raised)
268                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
269         rc = CTXTP(ctxt, add)(env, ctxt, rec, lsm, logcookies,
270                               numcookies);
271         if (!raised)
272                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
273         RETURN(rc);
274 }
275 EXPORT_SYMBOL(llog_add);
276
277 int llog_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
278                 struct lov_stripe_md *lsm, int count,
279                 struct llog_cookie *cookies, int flags)
280 {
281         int rc;
282         ENTRY;
283
284         if (!ctxt) {
285                 CERROR("No ctxt\n");
286                 RETURN(-ENODEV);
287         }
288
289         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
290         rc = CTXTP(ctxt, cancel)(env, ctxt, lsm, count, cookies, flags);
291         RETURN(rc);
292 }
293 EXPORT_SYMBOL(llog_cancel);
294
295 /* lop_setup method for filter/osc */
296 // XXX how to set exports
297 int llog_obd_origin_setup(const struct lu_env *env, struct obd_device *obd,
298                           struct obd_llog_group *olg, int index,
299                           struct obd_device *disk_obd, int count,
300                           struct llog_logid *logid, const char *name)
301 {
302         struct llog_ctxt *ctxt;
303         struct llog_handle *handle;
304         struct lvfs_run_ctxt saved;
305         int rc;
306         ENTRY;
307
308         if (count == 0)
309                 RETURN(0);
310
311         LASSERT(count == 1);
312
313         LASSERT(olg != NULL);
314         ctxt = llog_group_get_ctxt(olg, index);
315         if (!ctxt)
316                 RETURN(-ENODEV);
317
318         if (logid && logid->lgl_oid) {
319                 rc = llog_create(env, ctxt, &handle, logid, NULL);
320         } else {
321                 rc = llog_create(env, ctxt, &handle, NULL, (char *)name);
322                 if (!rc && logid)
323                         *logid = handle->lgh_id;
324         }
325         if (rc)
326                 GOTO(out, rc);
327
328         ctxt->loc_handle = handle;
329         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
330         rc = llog_init_handle(env, handle, LLOG_F_IS_CAT, NULL);
331         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
332         if (rc)
333                 GOTO(out, rc);
334
335         rc = llog_process(env, handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
336         if (rc)
337                 CERROR("llog_process() with cat_cancel_cb failed: %d\n", rc);
338         GOTO(out, rc);
339 out:
340         llog_ctxt_put(ctxt);
341         return rc;
342 }
343 EXPORT_SYMBOL(llog_obd_origin_setup);
344
345 int llog_obd_origin_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
346 {
347         struct llog_handle *cathandle, *n, *loghandle;
348         struct llog_log_hdr *llh;
349         int rc, index;
350         ENTRY;
351
352         if (!ctxt)
353                 RETURN(0);
354
355         cathandle = ctxt->loc_handle;
356         if (cathandle) {
357                 cfs_list_for_each_entry_safe(loghandle, n,
358                                              &cathandle->u.chd.chd_head,
359                                              u.phd.phd_entry) {
360                         llh = loghandle->lgh_hdr;
361                         if ((llh->llh_flags &
362                                 LLOG_F_ZAP_WHEN_EMPTY) &&
363                             (llh->llh_count == 1)) {
364                                 rc = llog_destroy(env, loghandle);
365                                 if (rc)
366                                         CERROR("failure destroying log during "
367                                                "cleanup: %d\n", rc);
368
369                                 index = loghandle->u.phd.phd_cookie.lgc_index;
370                                 llog_free_handle(loghandle);
371
372                                 LASSERT(index);
373                                 llog_cat_set_first_idx(cathandle, index);
374                                 rc = llog_cancel_rec(env, cathandle, index);
375                                 if (rc == 0)
376                                         CDEBUG(D_RPCTRACE, "cancel plain log at"
377                                                "index %u of catalog "LPX64"\n",
378                                                index,cathandle->lgh_id.lgl_oid);
379                         }
380                 }
381                 llog_cat_put(env, ctxt->loc_handle);
382         }
383         RETURN(0);
384 }
385 EXPORT_SYMBOL(llog_obd_origin_cleanup);
386
387 /* add for obdfilter/sz and mds/unlink */
388 int llog_obd_origin_add(const struct lu_env *env, struct llog_ctxt *ctxt,
389                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
390                         struct llog_cookie *logcookies, int numcookies)
391 {
392         struct llog_handle *cathandle;
393         int rc;
394         ENTRY;
395
396         cathandle = ctxt->loc_handle;
397         LASSERT(cathandle != NULL);
398         rc = llog_cat_add_rec(env, cathandle, rec, logcookies, NULL);
399         if (rc != 0 && rc != 1)
400                 CERROR("write one catalog record failed: %d\n", rc);
401         RETURN(rc);
402 }
403 EXPORT_SYMBOL(llog_obd_origin_add);
404
405 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
406                   struct obd_device *disk_obd, int *index)
407 {
408         int rc;
409         ENTRY;
410         OBD_CHECK_DT_OP(obd, llog_init, 0);
411         OBD_COUNTER_INCREMENT(obd, llog_init);
412
413         rc = OBP(obd, llog_init)(obd, olg, disk_obd, index);
414         RETURN(rc);
415 }
416 EXPORT_SYMBOL(obd_llog_init);
417
418 int obd_llog_finish(struct obd_device *obd, int count)
419 {
420         int rc;
421         ENTRY;
422         OBD_CHECK_DT_OP(obd, llog_finish, 0);
423         OBD_COUNTER_INCREMENT(obd, llog_finish);
424
425         rc = OBP(obd, llog_finish)(obd, count);
426         RETURN(rc);
427 }
428 EXPORT_SYMBOL(obd_llog_finish);
429
430 /* context key constructor/destructor: llog_key_init, llog_key_fini */
431 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
432 /* context key: llog_thread_key */
433 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
434 LU_KEY_INIT_GENERIC(llog);
435 EXPORT_SYMBOL(llog_thread_key);
436
437 int llog_info_init(void)
438 {
439         llog_key_init_generic(&llog_thread_key, NULL);
440         lu_context_key_register(&llog_thread_key);
441         return 0;
442 }
443
444 void llog_info_fini(void)
445 {
446         lu_context_key_degister(&llog_thread_key);
447 }