Whamcloud - gitweb
LU-6050 target: control OST-index in IDIF via ROCOMPAT flag
[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         obd->u.obt.obt_lut = lut;
60         obd->u.obt.obt_magic = OBT_MAGIC;
61
62         /* set request handler slice and parameters */
63         lut->lut_slice = slice;
64         lut->lut_reply_fail_id = reply_fail_id;
65         lut->lut_request_fail_id = request_fail_id;
66
67         /* sptlrcp variables init */
68         rwlock_init(&lut->lut_sptlrpc_lock);
69         sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
70         lut->lut_mds_capa = 1;
71         lut->lut_oss_capa = 1;
72
73         spin_lock_init(&lut->lut_flags_lock);
74         lut->lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
75
76         /* last_rcvd initialization is needed by replayable targets only */
77         if (!obd->obd_replayable)
78                 RETURN(0);
79
80         spin_lock_init(&lut->lut_translock);
81
82         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
83         if (lut->lut_client_bitmap == NULL)
84                 RETURN(-ENOMEM);
85
86         memset(&attr, 0, sizeof(attr));
87         attr.la_valid = LA_MODE;
88         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
89         dof.dof_type = dt_mode_to_dft(S_IFREG);
90
91         lu_local_obj_fid(&fid, LAST_RECV_OID);
92
93         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
94         if (IS_ERR(o)) {
95                 rc = PTR_ERR(o);
96                 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
97                        rc);
98                 GOTO(out_bitmap, rc);
99         }
100
101         lut->lut_last_rcvd = o;
102         rc = tgt_server_data_init(env, lut);
103         if (rc < 0)
104                 GOTO(out_obj, rc);
105
106         /* prepare transactions callbacks */
107         lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
108         lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
109         lut->lut_txn_cb.dtc_txn_commit = NULL;
110         lut->lut_txn_cb.dtc_cookie = lut;
111         lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
112         INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
113
114         dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
115         lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
116
117         RETURN(0);
118 out_obj:
119         lu_object_put(env, &lut->lut_last_rcvd->do_lu);
120         lut->lut_last_rcvd = NULL;
121 out_bitmap:
122         OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
123         lut->lut_client_bitmap = NULL;
124         return rc;
125 }
126 EXPORT_SYMBOL(tgt_init);
127
128 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
129 {
130         ENTRY;
131
132         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
133
134         if (lut->lut_client_bitmap) {
135                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
136                 lut->lut_client_bitmap = NULL;
137         }
138         if (lut->lut_last_rcvd) {
139                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
140                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
141                 lut->lut_last_rcvd = NULL;
142         }
143         EXIT;
144 }
145 EXPORT_SYMBOL(tgt_fini);
146
147 /* context key constructor/destructor: tg_key_init, tg_key_fini */
148 LU_KEY_INIT(tgt, struct tgt_thread_info);
149
150 static void tgt_key_fini(const struct lu_context *ctx,
151                          struct lu_context_key *key, void *data)
152 {
153         struct tgt_thread_info          *info = data;
154         struct thandle_exec_args        *args = &info->tti_tea;
155         int                             i;
156
157         for (i = 0; i < args->ta_alloc_args; i++) {
158                 if (args->ta_args[i] != NULL)
159                         OBD_FREE_PTR(args->ta_args[i]);
160         }
161
162         if (args->ta_args != NULL)
163                 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
164                                         args->ta_alloc_args);
165         OBD_FREE_PTR(info);
166 }
167
168 static void tgt_key_exit(const struct lu_context *ctx,
169                          struct lu_context_key *key, void *data)
170 {
171         struct tgt_thread_info *tti = data;
172
173         tti->tti_has_trans = 0;
174         tti->tti_mult_trans = 0;
175 }
176
177 /* context key: tg_thread_key */
178 struct lu_context_key tgt_thread_key = {
179         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
180         .lct_init = tgt_key_init,
181         .lct_fini = tgt_key_fini,
182         .lct_exit = tgt_key_exit,
183 };
184
185 LU_KEY_INIT_GENERIC(tgt);
186
187 /* context key constructor/destructor: tgt_ses_key_init, tgt_ses_key_fini */
188 LU_KEY_INIT_FINI(tgt_ses, struct tgt_session_info);
189
190 /* context key: tgt_session_key */
191 struct lu_context_key tgt_session_key = {
192         .lct_tags = LCT_SERVER_SESSION,
193         .lct_init = tgt_ses_key_init,
194         .lct_fini = tgt_ses_key_fini,
195 };
196 EXPORT_SYMBOL(tgt_session_key);
197
198 LU_KEY_INIT_GENERIC(tgt_ses);
199
200 /*
201  * this page is allocated statically when module is initializing
202  * it is used to simulate data corruptions, see ost_checksum_bulk()
203  * for details. as the original pages provided by the layers below
204  * can be remain in the internal cache, we do not want to modify
205  * them.
206  */
207 struct page *tgt_page_to_corrupt;
208
209 int tgt_mod_init(void)
210 {
211         ENTRY;
212
213         tgt_page_to_corrupt = alloc_page(GFP_IOFS);
214
215         tgt_key_init_generic(&tgt_thread_key, NULL);
216         lu_context_key_register_many(&tgt_thread_key, NULL);
217
218         tgt_ses_key_init_generic(&tgt_session_key, NULL);
219         lu_context_key_register_many(&tgt_session_key, NULL);
220
221         RETURN(0);
222 }
223
224 void tgt_mod_exit(void)
225 {
226         if (tgt_page_to_corrupt != NULL)
227                 page_cache_release(tgt_page_to_corrupt);
228
229         lu_context_key_degister(&tgt_thread_key);
230         lu_context_key_degister(&tgt_session_key);
231 }
232