Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  */
31
32 #define DEBUG_SUBSYSTEM S_LOG
33
34
35 #include <obd_class.h>
36 #include <lustre_log.h>
37 #include "llog_internal.h"
38
39 /* helper functions for calling the llog obd methods */
40 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
41 {
42         struct llog_ctxt *ctxt;
43
44         OBD_ALLOC_PTR(ctxt);
45         if (!ctxt)
46                 return NULL;
47
48         ctxt->loc_obd = obd;
49         atomic_set(&ctxt->loc_refcount, 1);
50
51         return ctxt;
52 }
53
54 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
55 {
56         if (ctxt->loc_exp) {
57                 class_export_put(ctxt->loc_exp);
58                 ctxt->loc_exp = NULL;
59         }
60         if (ctxt->loc_imp) {
61                 class_import_put(ctxt->loc_imp);
62                 ctxt->loc_imp = NULL;
63         }
64         OBD_FREE_PTR(ctxt);
65 }
66
67 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt)
68 {
69         struct obd_llog_group *olg = ctxt->loc_olg;
70         struct obd_device *obd;
71         int rc = 0;
72
73         spin_lock(&olg->olg_lock);
74         if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
75                 spin_unlock(&olg->olg_lock);
76                 return rc;
77         }
78         olg->olg_ctxts[ctxt->loc_idx] = NULL;
79         spin_unlock(&olg->olg_lock);
80
81         obd = ctxt->loc_obd;
82         spin_lock(&obd->obd_dev_lock);
83         /* sync with llog ctxt user thread */
84         spin_unlock(&obd->obd_dev_lock);
85
86         /*
87          * obd->obd_starting is needed for the case of cleanup
88          * in error case while obd is starting up.
89          */
90         LASSERTF(obd->obd_starting == 1 ||
91                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
92                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
93                  !!obd->obd_stopping, !!obd->obd_set_up);
94
95         /* cleanup the llog ctxt here */
96         if (ctxt->loc_logops->lop_cleanup)
97                 rc = ctxt->loc_logops->lop_cleanup(env, ctxt);
98
99         llog_ctxt_destroy(ctxt);
100         wake_up(&olg->olg_waitq);
101         return rc;
102 }
103 EXPORT_SYMBOL(__llog_ctxt_put);
104
105 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
106 {
107         struct obd_llog_group *olg;
108         int rc, idx;
109
110         ENTRY;
111
112         LASSERT(ctxt != NULL);
113         LASSERT(ctxt != LP_POISON);
114
115         olg = ctxt->loc_olg;
116         LASSERT(olg != NULL);
117         LASSERT(olg != LP_POISON);
118
119         idx = ctxt->loc_idx;
120
121         /*
122          * Banlance the ctxt get when calling llog_cleanup()
123          */
124         LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
125         LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
126         llog_ctxt_put(ctxt);
127
128         /*
129          * Try to free the ctxt.
130          */
131         rc = __llog_ctxt_put(env, ctxt);
132         if (rc)
133                 CERROR("Error %d while cleaning up ctxt %p\n",
134                         rc, ctxt);
135
136         l_wait_event_abortable(olg->olg_waitq,
137                                llog_group_ctxt_null(olg, idx));
138
139         RETURN(rc);
140 }
141 EXPORT_SYMBOL(llog_cleanup);
142
143 int llog_setup(const struct lu_env *env, struct obd_device *obd,
144                struct obd_llog_group *olg, int index,
145                struct obd_device *disk_obd, const struct llog_operations *op)
146 {
147         struct llog_ctxt *ctxt;
148         int rc = 0;
149
150         ENTRY;
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         if (disk_obd != NULL)
167                 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
168         else
169                 ctxt->loc_exp = class_export_get(obd->obd_self_export);
170
171         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
172         ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE;
173
174         rc = llog_group_set_ctxt(olg, ctxt, index);
175         if (rc) {
176                 llog_ctxt_destroy(ctxt);
177                 if (rc == -EEXIST) {
178                         ctxt = llog_group_get_ctxt(olg, index);
179                         if (ctxt) {
180                                 CDEBUG(D_CONFIG, "%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                                 if (disk_obd != NULL)
185                                         LASSERT(ctxt->loc_exp ==
186                                                 disk_obd->obd_self_export);
187                                 else
188                                         LASSERT(ctxt->loc_exp ==
189                                                 obd->obd_self_export);
190                                 LASSERT(ctxt->loc_logops == op);
191                                 llog_ctxt_put(ctxt);
192                         }
193                         rc = 0;
194                 }
195                 RETURN(rc);
196         }
197
198         if (op->lop_setup) {
199                 if (CFS_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
200                         rc = -EOPNOTSUPP;
201                 else
202                         rc = op->lop_setup(env, obd, olg, index, disk_obd);
203         }
204
205         if (rc) {
206                 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
207                        obd->obd_name, index, op->lop_setup, rc);
208                 llog_group_clear_ctxt(olg, index);
209                 llog_ctxt_destroy(ctxt);
210         } else {
211                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
212                        obd->obd_name, index);
213                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
214         }
215
216         RETURN(rc);
217 }
218 EXPORT_SYMBOL(llog_setup);
219
220 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
221 {
222         int rc = 0;
223
224         ENTRY;
225         if (ctxt && ctxt->loc_logops->lop_sync)
226                 rc = ctxt->loc_logops->lop_sync(ctxt, exp, flags);
227
228         RETURN(rc);
229 }
230 EXPORT_SYMBOL(llog_sync);
231
232 /* context key constructor/destructor: llog_key_init, llog_key_fini */
233 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
234 /* context key: llog_thread_key */
235 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
236 LU_KEY_INIT_GENERIC(llog);
237
238 int llog_info_init(void)
239 {
240         llog_key_init_generic(&llog_thread_key, NULL);
241         lu_context_key_register(&llog_thread_key);
242         return 0;
243 }
244
245 void llog_info_fini(void)
246 {
247         lu_context_key_degister(&llog_thread_key);
248 }