GNU Linux-libre 4.9.309-gnu1
[releases.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.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 #include "../include/obd_class.h"
36 #include "../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         ctxt = kzalloc(sizeof(*ctxt), GFP_NOFS);
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         kfree(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         /* obd->obd_starting is needed for the case of cleanup
87          * in error case while obd is starting up.
88          */
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
110         olg = ctxt->loc_olg;
111         LASSERT(olg);
112         LASSERT(olg != LP_POISON);
113
114         idx = ctxt->loc_idx;
115
116         /*
117          * Banlance the ctxt get when calling llog_cleanup()
118          */
119         LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
120         LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
121         llog_ctxt_put(ctxt);
122
123         /*
124          * Try to free the ctxt.
125          */
126         rc = __llog_ctxt_put(env, ctxt);
127         if (rc)
128                 CERROR("Error %d while cleaning up ctxt %p\n",
129                        rc, ctxt);
130
131         l_wait_event(olg->olg_waitq,
132                      llog_group_ctxt_null(olg, idx), &lwi);
133
134         return rc;
135 }
136 EXPORT_SYMBOL(llog_cleanup);
137
138 int llog_setup(const struct lu_env *env, struct obd_device *obd,
139                struct obd_llog_group *olg, int index,
140                struct obd_device *disk_obd, struct llog_operations *op)
141 {
142         struct llog_ctxt *ctxt;
143         int rc = 0;
144
145         if (index < 0 || index >= LLOG_MAX_CTXTS)
146                 return -EINVAL;
147
148         LASSERT(olg);
149
150         ctxt = llog_new_ctxt(obd);
151         if (!ctxt)
152                 return -ENOMEM;
153
154         ctxt->loc_obd = obd;
155         ctxt->loc_olg = olg;
156         ctxt->loc_idx = index;
157         ctxt->loc_logops = op;
158         mutex_init(&ctxt->loc_mutex);
159         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
160         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
161
162         rc = llog_group_set_ctxt(olg, ctxt, index);
163         if (rc) {
164                 llog_ctxt_destroy(ctxt);
165                 if (rc == -EEXIST) {
166                         ctxt = llog_group_get_ctxt(olg, index);
167                         if (ctxt) {
168                                 /*
169                                  * mds_lov_update_desc() might call here multiple
170                                  * times. So if the llog is already set up then
171                                  * don't to do it again.
172                                  */
173                                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
174                                        obd->obd_name, index);
175                                 LASSERT(ctxt->loc_olg == olg);
176                                 LASSERT(ctxt->loc_obd == obd);
177                                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
178                                 LASSERT(ctxt->loc_logops == op);
179                                 llog_ctxt_put(ctxt);
180                         }
181                         rc = 0;
182                 }
183                 return rc;
184         }
185
186         if (op->lop_setup) {
187                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
188                         rc = -EOPNOTSUPP;
189                 else
190                         rc = op->lop_setup(env, obd, olg, index, disk_obd);
191         }
192
193         if (rc) {
194                 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
195                        obd->obd_name, index, op->lop_setup, rc);
196                 llog_group_clear_ctxt(olg, index);
197                 llog_ctxt_destroy(ctxt);
198         } else {
199                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
200                        obd->obd_name, index);
201                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
202         }
203
204         return rc;
205 }
206 EXPORT_SYMBOL(llog_setup);
207
208 /* context key constructor/destructor: llog_key_init, llog_key_fini */
209 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
210 /* context key: llog_thread_key */
211 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
212 LU_KEY_INIT_GENERIC(llog);
213
214 int llog_info_init(void)
215 {
216         llog_key_init_generic(&llog_thread_key, NULL);
217         lu_context_key_register(&llog_thread_key);
218         return 0;
219 }
220
221 void llog_info_fini(void)
222 {
223         lu_context_key_degister(&llog_thread_key);
224 }