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