Whamcloud - gitweb
a5cdc6e1841859b633d87212ee84901a316daad5
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LOG
34
35
36 #include <obd_class.h>
37 #include <lustre_log.h>
38 #include "llog_internal.h"
39
40 /* helper functions for calling the llog obd methods */
41 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
42 {
43         struct llog_ctxt *ctxt;
44
45         OBD_ALLOC_PTR(ctxt);
46         if (!ctxt)
47                 return NULL;
48
49         ctxt->loc_obd = obd;
50         atomic_set(&ctxt->loc_refcount, 1);
51
52         return ctxt;
53 }
54
55 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
56 {
57         if (ctxt->loc_exp) {
58                 class_export_put(ctxt->loc_exp);
59                 ctxt->loc_exp = NULL;
60         }
61         if (ctxt->loc_imp) {
62                 class_import_put(ctxt->loc_imp);
63                 ctxt->loc_imp = NULL;
64         }
65         OBD_FREE_PTR(ctxt);
66 }
67
68 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt)
69 {
70         struct obd_llog_group *olg = ctxt->loc_olg;
71         struct obd_device *obd;
72         int rc = 0;
73
74         spin_lock(&olg->olg_lock);
75         if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
76                 spin_unlock(&olg->olg_lock);
77                 return rc;
78         }
79         olg->olg_ctxts[ctxt->loc_idx] = NULL;
80         spin_unlock(&olg->olg_lock);
81
82         obd = ctxt->loc_obd;
83         spin_lock(&obd->obd_dev_lock);
84         /* sync with llog ctxt user thread */
85         spin_unlock(&obd->obd_dev_lock);
86
87         /* obd->obd_starting is needed for the case of cleanup
88          * in error case while obd is starting up. */
89         LASSERTF(obd->obd_starting == 1 ||
90                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
91                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
92                  !!obd->obd_stopping, !!obd->obd_set_up);
93
94         /* cleanup the llog ctxt here */
95         if (CTXTP(ctxt, cleanup))
96                 rc = CTXTP(ctxt, cleanup)(env, ctxt);
97
98         llog_ctxt_destroy(ctxt);
99         wake_up(&olg->olg_waitq);
100         return rc;
101 }
102 EXPORT_SYMBOL(__llog_ctxt_put);
103
104 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
105 {
106         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
107         struct obd_llog_group *olg;
108         int rc, idx;
109         ENTRY;
110
111         LASSERT(ctxt != NULL);
112         LASSERT(ctxt != LP_POISON);
113
114         olg = ctxt->loc_olg;
115         LASSERT(olg != NULL);
116         LASSERT(olg != LP_POISON);
117
118         idx = ctxt->loc_idx;
119
120         /*
121          * Banlance the ctxt get when calling llog_cleanup()
122          */
123         LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
124         LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
125         llog_ctxt_put(ctxt);
126
127         /*
128          * Try to free the ctxt.
129          */
130         rc = __llog_ctxt_put(env, ctxt);
131         if (rc)
132                 CERROR("Error %d while cleaning up ctxt %p\n",
133                        rc, ctxt);
134
135         l_wait_event(olg->olg_waitq,
136                      llog_group_ctxt_null(olg, idx), &lwi);
137
138         RETURN(rc);
139 }
140 EXPORT_SYMBOL(llog_cleanup);
141
142 int llog_setup(const struct lu_env *env, struct obd_device *obd,
143                struct obd_llog_group *olg, int index,
144                struct obd_device *disk_obd, struct llog_operations *op)
145 {
146         struct llog_ctxt *ctxt;
147         int rc = 0;
148         ENTRY;
149
150         if (index < 0 || index >= LLOG_MAX_CTXTS)
151                 RETURN(-EINVAL);
152
153         LASSERT(olg != NULL);
154
155         ctxt = llog_new_ctxt(obd);
156         if (!ctxt)
157                 RETURN(-ENOMEM);
158
159         ctxt->loc_obd = obd;
160         ctxt->loc_olg = olg;
161         ctxt->loc_idx = index;
162         ctxt->loc_logops = op;
163         mutex_init(&ctxt->loc_mutex);
164         if (disk_obd != NULL)
165                 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
166         else
167                 ctxt->loc_exp = class_export_get(obd->obd_self_export);
168
169         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
170         ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE;
171
172         rc = llog_group_set_ctxt(olg, ctxt, index);
173         if (rc) {
174                 llog_ctxt_destroy(ctxt);
175                 if (rc == -EEXIST) {
176                         ctxt = llog_group_get_ctxt(olg, index);
177                         if (ctxt) {
178                                 CDEBUG(D_CONFIG, "%s: ctxt %d already set up\n",
179                                        obd->obd_name, index);
180                                 LASSERT(ctxt->loc_olg == olg);
181                                 LASSERT(ctxt->loc_obd == obd);
182                                 if (disk_obd != NULL)
183                                         LASSERT(ctxt->loc_exp ==
184                                                 disk_obd->obd_self_export);
185                                 else
186                                         LASSERT(ctxt->loc_exp ==
187                                                 obd->obd_self_export);
188                                 LASSERT(ctxt->loc_logops == op);
189                                 llog_ctxt_put(ctxt);
190                         }
191                         rc = 0;
192                 }
193                 RETURN(rc);
194         }
195
196         if (op->lop_setup) {
197                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
198                         rc = -EOPNOTSUPP;
199                 else
200                         rc = op->lop_setup(env, obd, olg, index, disk_obd);
201         }
202
203         if (rc) {
204                 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
205                        obd->obd_name, index, op->lop_setup, rc);
206                 llog_group_clear_ctxt(olg, index);
207                 llog_ctxt_destroy(ctxt);
208         } else {
209                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
210                        obd->obd_name, index);
211                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
212         }
213
214         RETURN(rc);
215 }
216 EXPORT_SYMBOL(llog_setup);
217
218 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
219 {
220         int rc = 0;
221         ENTRY;
222
223         if (!ctxt)
224                 RETURN(0);
225
226         if (CTXTP(ctxt, sync))
227                 rc = CTXTP(ctxt, sync)(ctxt, exp, flags);
228
229         RETURN(rc);
230 }
231 EXPORT_SYMBOL(llog_sync);
232
233 int llog_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
234                 struct llog_cookie *cookies, int flags)
235 {
236         int rc;
237         ENTRY;
238
239         if (!ctxt) {
240                 CERROR("No ctxt\n");
241                 RETURN(-ENODEV);
242         }
243
244         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
245         rc = CTXTP(ctxt, cancel)(env, ctxt, cookies, flags);
246         RETURN(rc);
247 }
248 EXPORT_SYMBOL(llog_cancel);
249
250 /* context key constructor/destructor: llog_key_init, llog_key_fini */
251 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
252 /* context key: llog_thread_key */
253 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
254 LU_KEY_INIT_GENERIC(llog);
255
256 int llog_info_init(void)
257 {
258         llog_key_init_generic(&llog_thread_key, NULL);
259         lu_context_key_register(&llog_thread_key);
260         return 0;
261 }
262
263 void llog_info_fini(void)
264 {
265         lu_context_key_degister(&llog_thread_key);
266 }