Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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                 int alloc_size = size_roundup_power2(msgsize);
108
109                 LASSERT(!req->rq_pool);
110                 OBD_ALLOC(req->rq_reqbuf, alloc_size);
111                 if (!req->rq_reqbuf)
112                         return -ENOMEM;
113
114                 req->rq_reqbuf_len = alloc_size;
115         } else {
116                 LASSERT(req->rq_pool);
117                 LASSERT(req->rq_reqbuf_len >= msgsize);
118                 memset(req->rq_reqbuf, 0, msgsize);
119         }
120
121         req->rq_reqmsg = req->rq_reqbuf;
122         return 0;
123 }
124
125 static
126 void null_free_reqbuf(struct ptlrpc_sec *sec,
127                       struct ptlrpc_request *req)
128 {
129         if (!req->rq_pool) {
130                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
131                          "reqmsg %p is not reqbuf %p in null sec\n",
132                          req->rq_reqmsg, req->rq_reqbuf);
133                 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
134                          "reqlen %d should smaller than buflen %d\n",
135                          req->rq_reqlen, req->rq_reqbuf_len);
136
137                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
138                 req->rq_reqmsg = req->rq_reqbuf = NULL;
139                 req->rq_reqbuf_len = 0;
140         }
141 }
142
143 static
144 int null_alloc_repbuf(struct ptlrpc_sec *sec,
145                       struct ptlrpc_request *req,
146                       int msgsize)
147 {
148         msgsize = size_roundup_power2(msgsize);
149
150         OBD_ALLOC(req->rq_repbuf, msgsize);
151         if (!req->rq_repbuf)
152                 return -ENOMEM;
153
154         req->rq_repbuf_len = msgsize;
155         return 0;
156 }
157
158 static
159 void null_free_repbuf(struct ptlrpc_sec *sec,
160                       struct ptlrpc_request *req)
161 {
162         OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
163         req->rq_repbuf = NULL;
164         req->rq_repbuf_len = 0;
165 }
166
167 static
168 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
169                         struct ptlrpc_request *req,
170                         int segment, int newsize)
171 {
172         struct lustre_msg      *newbuf;
173         int                     oldsize, newmsg_size, alloc_size;
174
175         LASSERT(req->rq_reqbuf);
176         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
177         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
178         LASSERT(req->rq_reqlen == lustre_msg_size(req->rq_reqmsg->lm_magic,
179                                                   req->rq_reqmsg->lm_bufcount,
180                                                   req->rq_reqmsg->lm_buflens));
181
182         /* compute new message size */
183         oldsize = req->rq_reqbuf->lm_buflens[segment];
184         req->rq_reqbuf->lm_buflens[segment] = newsize;
185         newmsg_size = lustre_msg_size(req->rq_reqbuf->lm_magic,
186                                       req->rq_reqbuf->lm_bufcount,
187                                       req->rq_reqbuf->lm_buflens);
188         req->rq_reqbuf->lm_buflens[segment] = oldsize;
189
190         /* request from pool should always have enough buffer */
191         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
192
193         if (req->rq_reqbuf_len < newmsg_size) {
194                 alloc_size = size_roundup_power2(newmsg_size);
195
196                 OBD_ALLOC(newbuf, alloc_size);
197                 if (newbuf == NULL)
198                         return -ENOMEM;
199
200                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
201
202                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
203                 req->rq_reqbuf = req->rq_reqmsg = newbuf;
204                 req->rq_reqbuf_len = alloc_size;
205         }
206
207         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
208         req->rq_reqlen = newmsg_size;
209
210         return 0;
211 }
212
213 static
214 int null_accept(struct ptlrpc_request *req)
215 {
216         LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
217
218         if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
219                 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
220                 return SECSVC_DROP;
221         }
222
223         req->rq_reqmsg = req->rq_reqbuf;
224         req->rq_reqlen = req->rq_reqdata_len;
225
226         req->rq_svc_ctx = &null_svc_ctx;
227         atomic_inc(&req->rq_svc_ctx->sc_refcount);
228
229         return SECSVC_OK;
230 }
231
232 static
233 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
234 {
235         struct ptlrpc_reply_state *rs;
236         int rs_size = sizeof(*rs) + msgsize;
237
238         LASSERT(msgsize % 8 == 0);
239
240         rs = req->rq_reply_state;
241
242         if (rs) {
243                 /* pre-allocated */
244                 LASSERT(rs->rs_size >= rs_size);
245         } else {
246                 OBD_ALLOC(rs, rs_size);
247                 if (rs == NULL)
248                         return -ENOMEM;
249
250                 rs->rs_size = rs_size;
251         }
252
253         rs->rs_svc_ctx = req->rq_svc_ctx;
254         atomic_inc(&req->rq_svc_ctx->sc_refcount);
255
256         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
257         rs->rs_repbuf_len = rs_size - sizeof(*rs);
258         rs->rs_msg = rs->rs_repbuf;
259
260         req->rq_reply_state = rs;
261         return 0;
262 }
263
264 static
265 void null_free_rs(struct ptlrpc_reply_state *rs)
266 {
267         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
268         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
269
270         if (!rs->rs_prealloc)
271                 OBD_FREE(rs, rs->rs_size);
272 }
273
274 static
275 int null_authorize(struct ptlrpc_request *req)
276 {
277         struct ptlrpc_reply_state *rs = req->rq_reply_state;
278
279         LASSERT(rs);
280         if (rs->rs_repbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
281                 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
282         rs->rs_repdata_len = req->rq_replen;
283         return 0;
284 }
285
286 static struct ptlrpc_sec_cops null_sec_cops = {
287         .create_sec             = null_create_sec,
288         .destroy_sec            = null_destroy_sec,
289         .lookup_ctx             = null_lookup_ctx,
290         .alloc_reqbuf           = null_alloc_reqbuf,
291         .alloc_repbuf           = null_alloc_repbuf,
292         .free_reqbuf            = null_free_reqbuf,
293         .free_repbuf            = null_free_repbuf,
294         .enlarge_reqbuf         = null_enlarge_reqbuf,
295 };
296
297 static struct ptlrpc_sec_sops null_sec_sops = {
298         .accept                 = null_accept,
299         .alloc_rs               = null_alloc_rs,
300         .authorize              = null_authorize,
301         .free_rs                = null_free_rs,
302 };
303
304 static struct ptlrpc_sec_policy null_policy = {
305         .sp_owner               = THIS_MODULE,
306         .sp_name                = "sec.null",
307         .sp_policy              = SPTLRPC_POLICY_NULL,
308         .sp_cops                = &null_sec_cops,
309         .sp_sops                = &null_sec_sops,
310 };
311
312 static
313 void null_init_internal(void)
314 {
315         static HLIST_HEAD(__list);
316
317         null_sec.ps_policy = &null_policy;
318         atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
319         null_sec.ps_import = NULL;
320         null_sec.ps_flavor = SPTLRPC_FLVR_NULL;
321         null_sec.ps_flags = 0;
322         null_sec.ps_gc_interval = 0;
323         null_sec.ps_gc_next = 0;
324         spin_lock_init(&null_sec.ps_lock);
325         null_sec.ps_ccache_size = 1;
326         null_sec.ps_ccache = &__list;
327         atomic_set(&null_sec.ps_busy, 1);         /* for "null_cli_ctx" */
328
329         hlist_add_head(&null_cli_ctx.cc_hash, &__list);
330         atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
331         null_cli_ctx.cc_sec = &null_sec;
332         null_cli_ctx.cc_ops = &null_ctx_ops;
333         null_cli_ctx.cc_expire = 0;
334         null_cli_ctx.cc_flags = PTLRPC_CTX_HASHED | PTLRPC_CTX_ETERNAL |
335                                 PTLRPC_CTX_UPTODATE;
336         null_cli_ctx.cc_vcred.vc_uid = 0;
337         spin_lock_init(&null_cli_ctx.cc_lock);
338         INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
339 }
340
341 int sptlrpc_null_init(void)
342 {
343         int rc;
344
345         null_init_internal();
346
347         rc = sptlrpc_register_policy(&null_policy);
348         if (rc)
349                 CERROR("failed to register sec.null: %d\n", rc);
350
351         return rc;
352 }
353
354 void sptlrpc_null_fini(void)
355 {
356         int rc;
357
358         rc = sptlrpc_unregister_policy(&null_policy);
359         if (rc)
360                 CERROR("cannot unregister sec.null: %d\n", rc);
361 }