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