Whamcloud - gitweb
LU-5319 mdt: support multiple modify RCPs in parallel
[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
84         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
85         if (lut->lut_client_bitmap == NULL)
86                 RETURN(-ENOMEM);
87
88         memset(&attr, 0, sizeof(attr));
89         attr.la_valid = LA_MODE;
90         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
91         dof.dof_type = dt_mode_to_dft(S_IFREG);
92
93         lu_local_obj_fid(&fid, LAST_RECV_OID);
94
95         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
96         if (IS_ERR(o)) {
97                 rc = PTR_ERR(o);
98                 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
99                        rc);
100                 GOTO(out, rc);
101         }
102
103         lut->lut_last_rcvd = o;
104         rc = tgt_server_data_init(env, lut);
105         if (rc < 0)
106                 GOTO(out, rc);
107
108         /* prepare transactions callbacks */
109         lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
110         lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
111         lut->lut_txn_cb.dtc_txn_commit = NULL;
112         lut->lut_txn_cb.dtc_cookie = lut;
113         lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
114         INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
115
116         dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
117         lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
118
119         /* reply_data is supported by MDT targets only for now */
120         if (strncmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) != 0)
121                 RETURN(0);
122
123         OBD_ALLOC(lut->lut_reply_bitmap,
124                   LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
125         if (lut->lut_reply_bitmap == NULL)
126                 GOTO(out, rc);
127
128         memset(&attr, 0, sizeof(attr));
129         attr.la_valid = LA_MODE;
130         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
131         dof.dof_type = dt_mode_to_dft(S_IFREG);
132
133         lu_local_obj_fid(&fid, REPLY_DATA_OID);
134
135         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
136         if (IS_ERR(o)) {
137                 rc = PTR_ERR(o);
138                 CERROR("%s: cannot open REPLY_DATA: rc = %d\n", tgt_name(lut),
139                        rc);
140                 GOTO(out, rc);
141         }
142         lut->lut_reply_data = o;
143
144         rc = tgt_reply_data_init(env, lut);
145         if (rc < 0)
146                 GOTO(out, rc);
147
148         RETURN(0);
149 out:
150         if (lut->lut_last_rcvd != NULL)
151                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
152         lut->lut_last_rcvd = NULL;
153         if (lut->lut_client_bitmap != NULL)
154                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
155         lut->lut_client_bitmap = NULL;
156         if (lut->lut_reply_data != NULL)
157                 lu_object_put(env, &lut->lut_reply_data->do_lu);
158         lut->lut_reply_data = NULL;
159         if (lut->lut_reply_bitmap != NULL)
160                 OBD_FREE(lut->lut_reply_bitmap,
161                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
162         lut->lut_reply_bitmap = NULL;
163         return rc;
164 }
165 EXPORT_SYMBOL(tgt_init);
166
167 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
168 {
169         int i;
170         int rc;
171         ENTRY;
172
173         if (lut->lut_lsd.lsd_feature_incompat & OBD_INCOMPAT_MULTI_RPCS &&
174             atomic_read(&lut->lut_num_clients) == 0) {
175                 /* Clear MULTI RPCS incompatibility flag that prevents previous
176                  * Lustre versions to mount a target with reply_data file */
177                 lut->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
178                 rc = tgt_server_data_update(env, lut, 1);
179                 if (rc < 0)
180                         CERROR("%s: unable to clear MULTI RPCS "
181                                "incompatibility flag\n",
182                                lut->lut_obd->obd_name);
183         }
184
185         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
186
187         if (lut->lut_reply_data != NULL)
188                 lu_object_put(env, &lut->lut_reply_data->do_lu);
189         lut->lut_reply_data = NULL;
190         if (lut->lut_reply_bitmap != NULL) {
191                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
192                         if (lut->lut_reply_bitmap[i] != NULL)
193                                 OBD_FREE(lut->lut_reply_bitmap[i],
194                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
195                                     sizeof(long));
196                         lut->lut_reply_bitmap[i] = NULL;
197                 }
198                 OBD_FREE(lut->lut_reply_bitmap,
199                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
200         }
201         lut->lut_reply_bitmap = NULL;
202         if (lut->lut_client_bitmap) {
203                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
204                 lut->lut_client_bitmap = NULL;
205         }
206         if (lut->lut_last_rcvd) {
207                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
208                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
209                 lut->lut_last_rcvd = NULL;
210         }
211         EXIT;
212 }
213 EXPORT_SYMBOL(tgt_fini);
214
215 /* context key constructor/destructor: tg_key_init, tg_key_fini */
216 LU_KEY_INIT(tgt, struct tgt_thread_info);
217
218 static void tgt_key_fini(const struct lu_context *ctx,
219                          struct lu_context_key *key, void *data)
220 {
221         struct tgt_thread_info          *info = data;
222         struct thandle_exec_args        *args = &info->tti_tea;
223         int                             i;
224
225         for (i = 0; i < args->ta_alloc_args; i++) {
226                 if (args->ta_args[i] != NULL)
227                         OBD_FREE_PTR(args->ta_args[i]);
228         }
229
230         if (args->ta_args != NULL)
231                 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
232                                         args->ta_alloc_args);
233         OBD_FREE_PTR(info);
234 }
235
236 static void tgt_key_exit(const struct lu_context *ctx,
237                          struct lu_context_key *key, void *data)
238 {
239         struct tgt_thread_info *tti = data;
240
241         tti->tti_has_trans = 0;
242         tti->tti_mult_trans = 0;
243 }
244
245 /* context key: tg_thread_key */
246 struct lu_context_key tgt_thread_key = {
247         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
248         .lct_init = tgt_key_init,
249         .lct_fini = tgt_key_fini,
250         .lct_exit = tgt_key_exit,
251 };
252
253 LU_KEY_INIT_GENERIC(tgt);
254
255 /* context key constructor/destructor: tgt_ses_key_init, tgt_ses_key_fini */
256 LU_KEY_INIT_FINI(tgt_ses, struct tgt_session_info);
257
258 /* context key: tgt_session_key */
259 struct lu_context_key tgt_session_key = {
260         .lct_tags = LCT_SERVER_SESSION,
261         .lct_init = tgt_ses_key_init,
262         .lct_fini = tgt_ses_key_fini,
263 };
264 EXPORT_SYMBOL(tgt_session_key);
265
266 LU_KEY_INIT_GENERIC(tgt_ses);
267
268 /*
269  * this page is allocated statically when module is initializing
270  * it is used to simulate data corruptions, see ost_checksum_bulk()
271  * for details. as the original pages provided by the layers below
272  * can be remain in the internal cache, we do not want to modify
273  * them.
274  */
275 struct page *tgt_page_to_corrupt;
276
277 int tgt_mod_init(void)
278 {
279         ENTRY;
280
281         tgt_page_to_corrupt = alloc_page(GFP_IOFS);
282
283         tgt_key_init_generic(&tgt_thread_key, NULL);
284         lu_context_key_register_many(&tgt_thread_key, NULL);
285
286         tgt_ses_key_init_generic(&tgt_session_key, NULL);
287         lu_context_key_register_many(&tgt_session_key, NULL);
288
289         update_info_init();
290
291         RETURN(0);
292 }
293
294 void tgt_mod_exit(void)
295 {
296         if (tgt_page_to_corrupt != NULL)
297                 page_cache_release(tgt_page_to_corrupt);
298
299         lu_context_key_degister(&tgt_thread_key);
300         lu_context_key_degister(&tgt_session_key);
301         update_info_fini();
302 }
303