These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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.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, Intel Corporation.
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 #include "../include/obd_class.h"
40 #include "../include/lustre_log.h"
41 #include "llog_internal.h"
42
43 /* helper functions for calling the llog obd methods */
44 static struct llog_ctxt *llog_new_ctxt(struct obd_device *obd)
45 {
46         struct llog_ctxt *ctxt;
47
48         ctxt = kzalloc(sizeof(*ctxt), GFP_NOFS);
49         if (!ctxt)
50                 return NULL;
51
52         ctxt->loc_obd = obd;
53         atomic_set(&ctxt->loc_refcount, 1);
54
55         return ctxt;
56 }
57
58 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
59 {
60         if (ctxt->loc_exp) {
61                 class_export_put(ctxt->loc_exp);
62                 ctxt->loc_exp = NULL;
63         }
64         if (ctxt->loc_imp) {
65                 class_import_put(ctxt->loc_imp);
66                 ctxt->loc_imp = NULL;
67         }
68         kfree(ctxt);
69 }
70
71 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt)
72 {
73         struct obd_llog_group *olg = ctxt->loc_olg;
74         struct obd_device *obd;
75         int rc = 0;
76
77         spin_lock(&olg->olg_lock);
78         if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
79                 spin_unlock(&olg->olg_lock);
80                 return rc;
81         }
82         olg->olg_ctxts[ctxt->loc_idx] = NULL;
83         spin_unlock(&olg->olg_lock);
84
85         obd = ctxt->loc_obd;
86         spin_lock(&obd->obd_dev_lock);
87         /* sync with llog ctxt user thread */
88         spin_unlock(&obd->obd_dev_lock);
89
90         /* obd->obd_starting is needed for the case of cleanup
91          * in error case while obd is starting up. */
92         LASSERTF(obd->obd_starting == 1 ||
93                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
94                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
95                  !!obd->obd_stopping, !!obd->obd_set_up);
96
97         /* cleanup the llog ctxt here */
98         if (CTXTP(ctxt, cleanup))
99                 rc = CTXTP(ctxt, cleanup)(env, ctxt);
100
101         llog_ctxt_destroy(ctxt);
102         wake_up(&olg->olg_waitq);
103         return rc;
104 }
105 EXPORT_SYMBOL(__llog_ctxt_put);
106
107 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
108 {
109         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
110         struct obd_llog_group *olg;
111         int rc, idx;
112
113         LASSERT(ctxt != NULL);
114         LASSERT(ctxt != LP_POISON);
115
116         olg = ctxt->loc_olg;
117         LASSERT(olg != NULL);
118         LASSERT(olg != LP_POISON);
119
120         idx = ctxt->loc_idx;
121
122         /*
123          * Banlance the ctxt get when calling llog_cleanup()
124          */
125         LASSERT(atomic_read(&ctxt->loc_refcount) < LI_POISON);
126         LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
127         llog_ctxt_put(ctxt);
128
129         /*
130          * Try to free the ctxt.
131          */
132         rc = __llog_ctxt_put(env, ctxt);
133         if (rc)
134                 CERROR("Error %d while cleaning up ctxt %p\n",
135                        rc, ctxt);
136
137         l_wait_event(olg->olg_waitq,
138                      llog_group_ctxt_null(olg, idx), &lwi);
139
140         return rc;
141 }
142 EXPORT_SYMBOL(llog_cleanup);
143
144 int llog_setup(const struct lu_env *env, struct obd_device *obd,
145                struct obd_llog_group *olg, int index,
146                struct obd_device *disk_obd, struct llog_operations *op)
147 {
148         struct llog_ctxt *ctxt;
149         int rc = 0;
150
151         if (index < 0 || index >= LLOG_MAX_CTXTS)
152                 return -EINVAL;
153
154         LASSERT(olg != NULL);
155
156         ctxt = llog_new_ctxt(obd);
157         if (!ctxt)
158                 return -ENOMEM;
159
160         ctxt->loc_obd = obd;
161         ctxt->loc_olg = olg;
162         ctxt->loc_idx = index;
163         ctxt->loc_logops = op;
164         mutex_init(&ctxt->loc_mutex);
165         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
166         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
167
168         rc = llog_group_set_ctxt(olg, ctxt, index);
169         if (rc) {
170                 llog_ctxt_destroy(ctxt);
171                 if (rc == -EEXIST) {
172                         ctxt = llog_group_get_ctxt(olg, index);
173                         if (ctxt) {
174                                 /*
175                                  * mds_lov_update_desc() might call here multiple
176                                  * times. So if the llog is already set up then
177                                  * don't to do it again.
178                                  */
179                                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
180                                        obd->obd_name, index);
181                                 LASSERT(ctxt->loc_olg == olg);
182                                 LASSERT(ctxt->loc_obd == obd);
183                                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
184                                 LASSERT(ctxt->loc_logops == op);
185                                 llog_ctxt_put(ctxt);
186                         }
187                         rc = 0;
188                 }
189                 return rc;
190         }
191
192         if (op->lop_setup) {
193                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP))
194                         rc = -EOPNOTSUPP;
195                 else
196                         rc = op->lop_setup(env, obd, olg, index, disk_obd);
197         }
198
199         if (rc) {
200                 CERROR("%s: ctxt %d lop_setup=%p failed: rc = %d\n",
201                        obd->obd_name, index, op->lop_setup, rc);
202                 llog_group_clear_ctxt(olg, index);
203                 llog_ctxt_destroy(ctxt);
204         } else {
205                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
206                        obd->obd_name, index);
207                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
208         }
209
210         return rc;
211 }
212 EXPORT_SYMBOL(llog_setup);
213
214 /* context key constructor/destructor: llog_key_init, llog_key_fini */
215 LU_KEY_INIT_FINI(llog, struct llog_thread_info);
216 /* context key: llog_thread_key */
217 LU_CONTEXT_KEY_DEFINE(llog, LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL);
218 LU_KEY_INIT_GENERIC(llog);
219 EXPORT_SYMBOL(llog_thread_key);
220
221 int llog_info_init(void)
222 {
223         llog_key_init_generic(&llog_thread_key, NULL);
224         lu_context_key_register(&llog_thread_key);
225         return 0;
226 }
227
228 void llog_info_fini(void)
229 {
230         lu_context_key_degister(&llog_thread_key);
231 }