Whamcloud - gitweb
- set rq_reqmsg to NULL in null_free_req with rq_reqbuf
[fs/lustre-release.git] / lustre / ptlrpc / sec_null.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004-2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26
27 #ifndef __KERNEL__
28 #include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lustre_sec.h>
35
36 static struct ptlrpc_sec_policy null_policy;
37 static struct ptlrpc_sec        null_sec;
38 static struct ptlrpc_cli_ctx    null_cli_ctx;
39 static struct ptlrpc_svc_ctx    null_svc_ctx;
40
41 static
42 int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
43 {
44         /* should never reach here */
45         LBUG();
46         return 0;
47 }
48
49 static
50 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
51 {
52         if (req->rq_reqbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
53                 req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
54         req->rq_reqdata_len = req->rq_reqlen;
55         return 0;
56 }
57
58 static
59 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
60 {
61         req->rq_repmsg = req->rq_repbuf;
62         req->rq_replen = req->rq_repdata_len;
63         return 0;
64 }
65
66 static struct ptlrpc_ctx_ops null_ctx_ops = {
67         .refresh        = null_ctx_refresh,
68         .sign           = null_ctx_sign,
69         .verify         = null_ctx_verify,
70 };
71
72 static struct ptlrpc_svc_ctx null_svc_ctx = {
73         .sc_refcount    = ATOMIC_INIT(1),
74         .sc_policy      = &null_policy,
75 };
76
77 static
78 struct ptlrpc_sec* null_create_sec(struct obd_import *imp,
79                                    struct ptlrpc_svc_ctx *ctx,
80                                    __u32 flavor,
81                                    unsigned long flags)
82 {
83         LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_NULL);
84         return &null_sec;
85 }
86
87 static
88 void null_destroy_sec(struct ptlrpc_sec *sec)
89 {
90         LASSERT(sec == &null_sec);
91 }
92
93 static
94 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
95                                        struct vfs_cred *vcred)
96 {
97         atomic_inc(&null_cli_ctx.cc_refcount);
98         return &null_cli_ctx;
99 }
100
101 static
102 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
103                       struct ptlrpc_request *req,
104                       int msgsize)
105 {
106         if (!req->rq_reqbuf) {
107                 LASSERT(!req->rq_pool);
108                 OBD_ALLOC(req->rq_reqbuf, msgsize);
109                 if (!req->rq_reqbuf)
110                         return -ENOMEM;
111
112                 req->rq_reqbuf_len = msgsize;
113         } else {
114                 LASSERT(req->rq_pool);
115                 LASSERT(req->rq_reqbuf_len >= msgsize);
116                 memset(req->rq_reqbuf, 0, msgsize);
117         }
118
119         req->rq_reqmsg = req->rq_reqbuf;
120         return 0;
121 }
122
123 static
124 void null_free_reqbuf(struct ptlrpc_sec *sec,
125                       struct ptlrpc_request *req)
126 {
127         if (!req->rq_pool) {
128                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
129                          "reqmsg %p is not reqbuf %p in NULL sec\n",
130                          req->rq_reqmsg, req->rq_reqbuf);
131
132                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
133                 req->rq_reqmsg = req->rq_reqbuf = NULL;
134                 req->rq_reqbuf_len = 0;
135         }
136 }
137
138 static
139 int null_alloc_repbuf(struct ptlrpc_sec *sec,
140                       struct ptlrpc_request *req,
141                       int msgsize)
142 {
143         OBD_ALLOC(req->rq_repbuf, msgsize);
144         if (!req->rq_repbuf)
145                 return -ENOMEM;
146
147         req->rq_repbuf_len = msgsize;
148         return 0;
149 }
150
151 static
152 void null_free_repbuf(struct ptlrpc_sec *sec,
153                       struct ptlrpc_request *req)
154 {
155         OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
156         req->rq_repbuf = NULL;
157         req->rq_repbuf_len = 0;
158 }
159
160 static
161 int null_accept(struct ptlrpc_request *req)
162 {
163         LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
164
165         if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
166                 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
167                 return SECSVC_DROP;
168         }
169
170         req->rq_reqmsg = req->rq_reqbuf;
171         req->rq_reqlen = req->rq_reqdata_len;
172
173         req->rq_svc_ctx = &null_svc_ctx;
174         atomic_inc(&req->rq_svc_ctx->sc_refcount);
175
176         return SECSVC_OK;
177 }
178
179 static
180 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
181 {
182         struct ptlrpc_reply_state *rs;
183         int rs_size = sizeof(*rs) + msgsize;
184
185         LASSERT(msgsize % 8 == 0);
186
187         rs = req->rq_reply_state;
188
189         if (rs) {
190                 /* pre-allocated */
191                 LASSERT(rs->rs_size >= rs_size);
192         } else {
193                 OBD_ALLOC(rs, rs_size);
194                 if (rs == NULL)
195                         return -ENOMEM;
196
197                 rs->rs_size = rs_size;
198         }
199
200         rs->rs_svc_ctx = req->rq_svc_ctx;
201         atomic_inc(&req->rq_svc_ctx->sc_refcount);
202
203         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
204         rs->rs_repbuf_len = rs_size - sizeof(*rs);
205         rs->rs_msg = rs->rs_repbuf;
206
207         req->rq_reply_state = rs;
208         return 0;
209 }
210
211 static
212 void null_free_rs(struct ptlrpc_reply_state *rs)
213 {
214         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
215         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
216
217         if (!rs->rs_prealloc)
218                 OBD_FREE(rs, rs->rs_size);
219 }
220
221 static
222 int null_authorize(struct ptlrpc_request *req)
223 {
224         struct ptlrpc_reply_state *rs = req->rq_reply_state;
225
226         LASSERT(rs);
227         if (rs->rs_repbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
228                 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
229         rs->rs_repdata_len = req->rq_replen;
230         return 0;
231 }
232
233 static struct ptlrpc_sec_cops null_sec_cops = {
234         .create_sec             = null_create_sec,
235         .destroy_sec            = null_destroy_sec,
236         .lookup_ctx             = null_lookup_ctx,
237         .alloc_reqbuf           = null_alloc_reqbuf,
238         .alloc_repbuf           = null_alloc_repbuf,
239         .free_reqbuf            = null_free_reqbuf,
240         .free_repbuf            = null_free_repbuf,
241 };
242
243 static struct ptlrpc_sec_sops null_sec_sops = {
244         .accept                 = null_accept,
245         .alloc_rs               = null_alloc_rs,
246         .authorize              = null_authorize,
247         .free_rs                = null_free_rs,
248 };
249
250 static struct ptlrpc_sec_policy null_policy = {
251         .sp_owner               = THIS_MODULE,
252         .sp_name                = "sec.null",
253         .sp_policy              = SPTLRPC_POLICY_NULL,
254         .sp_cops                = &null_sec_cops,
255         .sp_sops                = &null_sec_sops,
256 };
257
258 static
259 void null_init_internal(void)
260 {
261         static HLIST_HEAD(__list);
262
263         null_sec.ps_policy = &null_policy;
264         atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
265         null_sec.ps_import = NULL;
266         null_sec.ps_flavor = SPTLRPC_FLVR_NULL;
267         null_sec.ps_flags = 0;
268         null_sec.ps_gc_interval = 0;
269         null_sec.ps_gc_next = 0;
270         spin_lock_init(&null_sec.ps_lock);
271         null_sec.ps_ccache_size = 1;
272         null_sec.ps_ccache = &__list;
273         atomic_set(&null_sec.ps_busy, 1);         /* for "null_cli_ctx" */
274
275         hlist_add_head(&null_cli_ctx.cc_hash, &__list);
276         atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
277         null_cli_ctx.cc_sec = &null_sec;
278         null_cli_ctx.cc_ops = &null_ctx_ops;
279         null_cli_ctx.cc_expire = 0;
280         null_cli_ctx.cc_flags = PTLRPC_CTX_HASHED | PTLRPC_CTX_ETERNAL |
281                                 PTLRPC_CTX_UPTODATE;
282         null_cli_ctx.cc_vcred.vc_uid = 0;
283         spin_lock_init(&null_cli_ctx.cc_lock);
284         INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
285 }
286
287 int sptlrpc_null_init(void)
288 {
289         int rc;
290
291         null_init_internal();
292
293         rc = sptlrpc_register_policy(&null_policy);
294         if (rc)
295                 CERROR("failed to register sec.null: %d\n", rc);
296
297         return rc;
298 }
299
300 void sptlrpc_null_fini(void)
301 {
302         int rc;
303
304         rc = sptlrpc_unregister_policy(&null_policy);
305         if (rc)
306                 CERROR("cannot unregister sec.null: %d\n", rc);
307 }