Whamcloud - gitweb
LU-3806 obdclass: add LCT_SERVER_SESSION for server session
[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, 2013, 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
39 int tgt_init(const struct lu_env *env, struct lu_target *lut,
40              struct obd_device *obd, struct dt_device *dt,
41              struct tgt_opc_slice *slice, int request_fail_id,
42              int reply_fail_id)
43 {
44         struct dt_object_format  dof;
45         struct lu_attr           attr;
46         struct lu_fid            fid;
47         struct dt_object        *o;
48         int                      rc = 0;
49
50         ENTRY;
51
52         LASSERT(lut);
53         LASSERT(obd);
54         lut->lut_obd = obd;
55         lut->lut_bottom = dt;
56         lut->lut_last_rcvd = NULL;
57         lut->lut_client_bitmap = NULL;
58         obd->u.obt.obt_lut = lut;
59         obd->u.obt.obt_magic = OBT_MAGIC;
60
61         /* set request handler slice and parameters */
62         lut->lut_slice = slice;
63         lut->lut_reply_fail_id = reply_fail_id;
64         lut->lut_request_fail_id = request_fail_id;
65
66         /* sptlrcp variables init */
67         rwlock_init(&lut->lut_sptlrpc_lock);
68         sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
69         lut->lut_mds_capa = 1;
70         lut->lut_oss_capa = 1;
71
72         /* last_rcvd initialization is needed by replayable targets only */
73         if (!obd->obd_replayable)
74                 RETURN(0);
75
76         spin_lock_init(&lut->lut_translock);
77
78         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
79         if (lut->lut_client_bitmap == NULL)
80                 RETURN(-ENOMEM);
81
82         memset(&attr, 0, sizeof(attr));
83         attr.la_valid = LA_MODE;
84         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
85         dof.dof_type = dt_mode_to_dft(S_IFREG);
86
87         lu_local_obj_fid(&fid, LAST_RECV_OID);
88
89         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
90         if (!IS_ERR(o)) {
91                 lut->lut_last_rcvd = o;
92         } else {
93                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
94                 lut->lut_client_bitmap = NULL;
95                 rc = PTR_ERR(o);
96                 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
97         }
98
99         RETURN(rc);
100 }
101 EXPORT_SYMBOL(tgt_init);
102
103 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
104 {
105         ENTRY;
106
107         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
108
109         if (lut->lut_client_bitmap) {
110                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
111                 lut->lut_client_bitmap = NULL;
112         }
113         if (lut->lut_last_rcvd) {
114                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
115                 lut->lut_last_rcvd = NULL;
116         }
117         EXIT;
118 }
119 EXPORT_SYMBOL(tgt_fini);
120
121 /* context key constructor/destructor: tg_key_init, tg_key_fini */
122 LU_KEY_INIT_FINI(tgt, struct tgt_thread_info);
123
124 /* context key: tg_thread_key */
125 LU_CONTEXT_KEY_DEFINE(tgt, LCT_MD_THREAD | LCT_DT_THREAD);
126 EXPORT_SYMBOL(tgt_thread_key);
127
128 LU_KEY_INIT_GENERIC(tgt);
129
130 /* context key constructor/destructor: tgt_ses_key_init, tgt_ses_key_fini */
131 LU_KEY_INIT_FINI(tgt_ses, struct tgt_session_info);
132
133 /* context key: tgt_session_key */
134 struct lu_context_key tgt_session_key = {
135         .lct_tags = LCT_SERVER_SESSION,
136         .lct_init = tgt_ses_key_init,
137         .lct_fini = tgt_ses_key_fini,
138 };
139 EXPORT_SYMBOL(tgt_session_key);
140
141 LU_KEY_INIT_GENERIC(tgt_ses);
142
143 struct lprocfs_vars lprocfs_srv_module_vars[] = {
144         { 0 },
145 };
146
147 int tgt_mod_init(void)
148 {
149         ENTRY;
150
151         tgt_key_init_generic(&tgt_thread_key, NULL);
152         lu_context_key_register_many(&tgt_thread_key, NULL);
153
154         tgt_ses_key_init_generic(&tgt_session_key, NULL);
155         lu_context_key_register_many(&tgt_session_key, NULL);
156
157         RETURN(0);
158 }
159
160 void tgt_mod_exit(void)
161 {
162         lu_context_key_degister(&tgt_thread_key);
163         lu_context_key_degister(&tgt_session_key);
164 }
165