Whamcloud - gitweb
LU-7028 tgt: initialize spin lock in tgt_init()
[fs/lustre-release.git] / lustre / target / tgt_main.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2014, Intel Corporation.
25  */
26 /*
27  * lustre/target/tgt_main.c
28  *
29  * Lustre Unified Target main initialization code
30  *
31  * Author: Mikhail Pershin <mike.pershin@intel.com>
32  */
33
34 #define DEBUG_SUBSYSTEM S_CLASS
35
36 #include <obd.h>
37 #include "tgt_internal.h"
38 #include "../ptlrpc/ptlrpc_internal.h"
39
40 int tgt_init(const struct lu_env *env, struct lu_target *lut,
41              struct obd_device *obd, struct dt_device *dt,
42              struct tgt_opc_slice *slice, int request_fail_id,
43              int reply_fail_id)
44 {
45         struct dt_object_format  dof;
46         struct lu_attr           attr;
47         struct lu_fid            fid;
48         struct dt_object        *o;
49         int                      rc = 0;
50
51         ENTRY;
52
53         LASSERT(lut);
54         LASSERT(obd);
55         lut->lut_obd = obd;
56         lut->lut_bottom = dt;
57         lut->lut_last_rcvd = NULL;
58         lut->lut_client_bitmap = NULL;
59         atomic_set(&lut->lut_num_clients, 0);
60         atomic_set(&lut->lut_client_generation, 0);
61         lut->lut_reply_data = NULL;
62         lut->lut_reply_bitmap = NULL;
63         obd->u.obt.obt_lut = lut;
64         obd->u.obt.obt_magic = OBT_MAGIC;
65
66         /* set request handler slice and parameters */
67         lut->lut_slice = slice;
68         lut->lut_reply_fail_id = reply_fail_id;
69         lut->lut_request_fail_id = request_fail_id;
70
71         /* sptlrcp variables init */
72         rwlock_init(&lut->lut_sptlrpc_lock);
73         sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
74
75         spin_lock_init(&lut->lut_flags_lock);
76         lut->lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
77
78         /* last_rcvd initialization is needed by replayable targets only */
79         if (!obd->obd_replayable)
80                 RETURN(0);
81
82         spin_lock_init(&lut->lut_translock);
83         spin_lock_init(&lut->lut_client_bitmap_lock);
84
85         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
86         if (lut->lut_client_bitmap == NULL)
87                 RETURN(-ENOMEM);
88
89         memset(&attr, 0, sizeof(attr));
90         attr.la_valid = LA_MODE;
91         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
92         dof.dof_type = dt_mode_to_dft(S_IFREG);
93
94         lu_local_obj_fid(&fid, LAST_RECV_OID);
95
96         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
97         if (IS_ERR(o)) {
98                 rc = PTR_ERR(o);
99                 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
100                        rc);
101                 GOTO(out, rc);
102         }
103
104         lut->lut_last_rcvd = o;
105         rc = tgt_server_data_init(env, lut);
106         if (rc < 0)
107                 GOTO(out, rc);
108
109         /* prepare transactions callbacks */
110         lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
111         lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
112         lut->lut_txn_cb.dtc_txn_commit = NULL;
113         lut->lut_txn_cb.dtc_cookie = lut;
114         lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
115         INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
116
117         dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
118         lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
119
120         /* reply_data is supported by MDT targets only for now */
121         if (strncmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) != 0)
122                 RETURN(0);
123
124         OBD_ALLOC(lut->lut_reply_bitmap,
125                   LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
126         if (lut->lut_reply_bitmap == NULL)
127                 GOTO(out, rc);
128
129         memset(&attr, 0, sizeof(attr));
130         attr.la_valid = LA_MODE;
131         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
132         dof.dof_type = dt_mode_to_dft(S_IFREG);
133
134         lu_local_obj_fid(&fid, REPLY_DATA_OID);
135
136         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
137         if (IS_ERR(o)) {
138                 rc = PTR_ERR(o);
139                 CERROR("%s: cannot open REPLY_DATA: rc = %d\n", tgt_name(lut),
140                        rc);
141                 GOTO(out, rc);
142         }
143         lut->lut_reply_data = o;
144
145         rc = tgt_reply_data_init(env, lut);
146         if (rc < 0)
147                 GOTO(out, rc);
148
149         RETURN(0);
150 out:
151         if (lut->lut_last_rcvd != NULL) {
152                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
153                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
154         }
155         lut->lut_last_rcvd = NULL;
156         if (lut->lut_client_bitmap != NULL)
157                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
158         lut->lut_client_bitmap = NULL;
159         if (lut->lut_reply_data != NULL)
160                 lu_object_put(env, &lut->lut_reply_data->do_lu);
161         lut->lut_reply_data = NULL;
162         if (lut->lut_reply_bitmap != NULL)
163                 OBD_FREE(lut->lut_reply_bitmap,
164                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
165         lut->lut_reply_bitmap = NULL;
166         return rc;
167 }
168 EXPORT_SYMBOL(tgt_init);
169
170 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
171 {
172         int i;
173         int rc;
174         ENTRY;
175
176         if (lut->lut_lsd.lsd_feature_incompat & OBD_INCOMPAT_MULTI_RPCS &&
177             atomic_read(&lut->lut_num_clients) == 0) {
178                 /* Clear MULTI RPCS incompatibility flag that prevents previous
179                  * Lustre versions to mount a target with reply_data file */
180                 lut->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
181                 rc = tgt_server_data_update(env, lut, 1);
182                 if (rc < 0)
183                         CERROR("%s: unable to clear MULTI RPCS "
184                                "incompatibility flag\n",
185                                lut->lut_obd->obd_name);
186         }
187
188         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
189
190         if (lut->lut_reply_data != NULL)
191                 lu_object_put(env, &lut->lut_reply_data->do_lu);
192         lut->lut_reply_data = NULL;
193         if (lut->lut_reply_bitmap != NULL) {
194                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
195                         if (lut->lut_reply_bitmap[i] != NULL)
196                                 OBD_FREE(lut->lut_reply_bitmap[i],
197                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
198                                     sizeof(long));
199                         lut->lut_reply_bitmap[i] = NULL;
200                 }
201                 OBD_FREE(lut->lut_reply_bitmap,
202                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
203         }
204         lut->lut_reply_bitmap = NULL;
205         if (lut->lut_client_bitmap) {
206                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
207                 lut->lut_client_bitmap = NULL;
208         }
209         if (lut->lut_last_rcvd) {
210                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
211                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
212                 lut->lut_last_rcvd = NULL;
213         }
214         EXIT;
215 }
216 EXPORT_SYMBOL(tgt_fini);
217
218 /* context key constructor/destructor: tg_key_init, tg_key_fini */
219 LU_KEY_INIT(tgt, struct tgt_thread_info);
220
221 static void tgt_key_fini(const struct lu_context *ctx,
222                          struct lu_context_key *key, void *data)
223 {
224         struct tgt_thread_info          *info = data;
225         struct thandle_exec_args        *args = &info->tti_tea;
226         int                             i;
227
228         for (i = 0; i < args->ta_alloc_args; i++) {
229                 if (args->ta_args[i] != NULL)
230                         OBD_FREE_PTR(args->ta_args[i]);
231         }
232
233         if (args->ta_args != NULL)
234                 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
235                                         args->ta_alloc_args);
236         OBD_FREE_PTR(info);
237 }
238
239 static void tgt_key_exit(const struct lu_context *ctx,
240                          struct lu_context_key *key, void *data)
241 {
242         struct tgt_thread_info *tti = data;
243
244         tti->tti_has_trans = 0;
245         tti->tti_mult_trans = 0;
246 }
247
248 /* context key: tg_thread_key */
249 struct lu_context_key tgt_thread_key = {
250         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
251         .lct_init = tgt_key_init,
252         .lct_fini = tgt_key_fini,
253         .lct_exit = tgt_key_exit,
254 };
255
256 LU_KEY_INIT_GENERIC(tgt);
257
258 /* context key constructor/destructor: tgt_ses_key_init, tgt_ses_key_fini */
259 LU_KEY_INIT_FINI(tgt_ses, struct tgt_session_info);
260
261 /* context key: tgt_session_key */
262 struct lu_context_key tgt_session_key = {
263         .lct_tags = LCT_SERVER_SESSION,
264         .lct_init = tgt_ses_key_init,
265         .lct_fini = tgt_ses_key_fini,
266 };
267 EXPORT_SYMBOL(tgt_session_key);
268
269 LU_KEY_INIT_GENERIC(tgt_ses);
270
271 /*
272  * this page is allocated statically when module is initializing
273  * it is used to simulate data corruptions, see ost_checksum_bulk()
274  * for details. as the original pages provided by the layers below
275  * can be remain in the internal cache, we do not want to modify
276  * them.
277  */
278 struct page *tgt_page_to_corrupt;
279
280 int tgt_mod_init(void)
281 {
282         ENTRY;
283
284         tgt_page_to_corrupt = alloc_page(GFP_IOFS);
285
286         tgt_key_init_generic(&tgt_thread_key, NULL);
287         lu_context_key_register_many(&tgt_thread_key, NULL);
288
289         tgt_ses_key_init_generic(&tgt_session_key, NULL);
290         lu_context_key_register_many(&tgt_session_key, NULL);
291
292         update_info_init();
293
294         RETURN(0);
295 }
296
297 void tgt_mod_exit(void)
298 {
299         if (tgt_page_to_corrupt != NULL)
300                 page_cache_release(tgt_page_to_corrupt);
301
302         lu_context_key_degister(&tgt_thread_key);
303         lu_context_key_degister(&tgt_session_key);
304         update_info_fini();
305 }
306