4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LOG
36 #include <obd_class.h>
37 #include <lustre_log.h>
38 #include "llog_internal.h"
40 /* helper functions for calling the llog obd methods */
41 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
43 struct llog_ctxt *ctxt;
50 atomic_set(&ctxt->loc_refcount, 1);
55 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
58 class_export_put(ctxt->loc_exp);
62 class_import_put(ctxt->loc_imp);
68 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt)
70 struct obd_llog_group *olg = ctxt->loc_olg;
71 struct obd_device *obd;
74 spin_lock(&olg->olg_lock);
75 if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
76 spin_unlock(&olg->olg_lock);
79 olg->olg_ctxts[ctxt->loc_idx] = NULL;
80 spin_unlock(&olg->olg_lock);
83 spin_lock(&obd->obd_dev_lock);
84 /* sync with llog ctxt user thread */
85 spin_unlock(&obd->obd_dev_lock);
88 * obd->obd_starting is needed for the case of cleanup
89 * in error case while obd is starting up.
91 LASSERTF(obd->obd_starting == 1 ||
92 obd->obd_stopping == 1 || obd->obd_set_up == 0,
93 "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
94 !!obd->obd_stopping, !!obd->obd_set_up);
96 /* cleanup the llog ctxt here */
97 if (ctxt->loc_logops->lop_cleanup)
98 rc = ctxt->loc_logops->lop_cleanup(env, ctxt);
100 llog_ctxt_destroy(ctxt);
101 wake_up(&olg->olg_waitq);
104 EXPORT_SYMBOL(__llog_ctxt_put);
106 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
108 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
109 struct obd_llog_group *olg;
114 LASSERT(ctxt != NULL);
115 LASSERT(ctxt != LP_POISON);
118 LASSERT(olg != NULL);
119 LASSERT(olg != LP_POISON);
124 * Banlance the ctxt get when calling llog_cleanup()
126 LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
127 LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
131 * Try to free the ctxt.
133 rc = __llog_ctxt_put(env, ctxt);
135 CERROR("Error %d while cleaning up ctxt %p\n",
138 l_wait_event(olg->olg_waitq,
139 llog_group_ctxt_null(olg, idx), &lwi);
143 EXPORT_SYMBOL(llog_cleanup);
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)
149 struct llog_ctxt *ctxt;
154 if (index < 0 || index >= LLOG_MAX_CTXTS)
157 LASSERT(olg != NULL);
159 ctxt = llog_new_ctxt(obd);
165 ctxt->loc_idx = index;
166 ctxt->loc_logops = op;
167 mutex_init(&ctxt->loc_mutex);
168 if (disk_obd != NULL)
169 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
171 ctxt->loc_exp = class_export_get(obd->obd_self_export);
173 ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
174 ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE;
176 rc = llog_group_set_ctxt(olg, ctxt, index);
178 llog_ctxt_destroy(ctxt);
180 ctxt = llog_group_get_ctxt(olg, index);
182 CDEBUG(D_CONFIG, "%s: ctxt %d already set up\n",
183 obd->obd_name, index);
184 LASSERT(ctxt->loc_olg == olg);
185 LASSERT(ctxt->loc_obd == obd);
186 if (disk_obd != NULL)
187 LASSERT(ctxt->loc_exp ==
188 disk_obd->obd_self_export);
190 LASSERT(ctxt->loc_exp ==
191 obd->obd_self_export);
192 LASSERT(ctxt->loc_logops == op);
201 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
204 rc = op->lop_setup(env, obd, olg, index, disk_obd);
208 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
209 obd->obd_name, index, op->lop_setup, rc);
210 llog_group_clear_ctxt(olg, index);
211 llog_ctxt_destroy(ctxt);
213 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
214 obd->obd_name, index);
215 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
220 EXPORT_SYMBOL(llog_setup);
222 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
227 if (ctxt && ctxt->loc_logops->lop_sync)
228 rc = ctxt->loc_logops->lop_sync(ctxt, exp, flags);
232 EXPORT_SYMBOL(llog_sync);
234 /* context key constructor/destructor: llog_key_init, llog_key_fini */
235 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
236 /* context key: llog_thread_key */
237 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
238 LU_KEY_INIT_GENERIC(llog);
240 int llog_info_init(void)
242 llog_key_init_generic(&llog_thread_key, NULL);
243 lu_context_key_register(&llog_thread_key);
247 void llog_info_fini(void)
249 lu_context_key_degister(&llog_thread_key);