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         int                     oldsize, newmsg_size, alloc_size;
184
185         LASSERT(req->rq_reqbuf);
186         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
187         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
188         LASSERT(req->rq_reqlen == lustre_msg_size(req->rq_reqmsg->lm_magic,
189                                                   req->rq_reqmsg->lm_bufcount,
190                                                   req->rq_reqmsg->lm_buflens));
191
192         /* compute new message size */
193         oldsize = req->rq_reqbuf->lm_buflens[segment];
194         req->rq_reqbuf->lm_buflens[segment] = newsize;
195         newmsg_size = lustre_msg_size(req->rq_reqbuf->lm_magic,
196                                       req->rq_reqbuf->lm_bufcount,
197                                       req->rq_reqbuf->lm_buflens);
198         req->rq_reqbuf->lm_buflens[segment] = oldsize;
199
200         /* request from pool should always have enough buffer */
201         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
202
203         if (req->rq_reqbuf_len < newmsg_size) {
204                 alloc_size = size_roundup_power2(newmsg_size);
205
206                 OBD_ALLOC(newbuf, alloc_size);
207                 if (newbuf == NULL)
208                         return -ENOMEM;
209
210                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
211
212                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
213                 req->rq_reqbuf = req->rq_reqmsg = newbuf;
214                 req->rq_reqbuf_len = alloc_size;
215         }
216
217         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
218         req->rq_reqlen = newmsg_size;
219
220         return 0;
221 }
222
223 static
224 int null_accept(struct ptlrpc_request *req)
225 {
226         LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
227
228         if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
229                 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
230                 return SECSVC_DROP;
231         }
232
233         req->rq_reqmsg = req->rq_reqbuf;
234         req->rq_reqlen = req->rq_reqdata_len;
235
236         req->rq_svc_ctx = &null_svc_ctx;
237         atomic_inc(&req->rq_svc_ctx->sc_refcount);
238
239         return SECSVC_OK;
240 }
241
242 static
243 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
244 {
245         struct ptlrpc_reply_state *rs;
246         int rs_size = sizeof(*rs) + msgsize;
247
248         LASSERT(msgsize % 8 == 0);
249
250         rs = req->rq_reply_state;
251
252         if (rs) {
253                 /* pre-allocated */
254                 LASSERT(rs->rs_size >= rs_size);
255         } else {
256                 OBD_ALLOC(rs, rs_size);
257                 if (rs == NULL)
258                         return -ENOMEM;
259
260                 rs->rs_size = rs_size;
261         }
262
263         rs->rs_svc_ctx = req->rq_svc_ctx;
264         atomic_inc(&req->rq_svc_ctx->sc_refcount);
265
266         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
267         rs->rs_repbuf_len = rs_size - sizeof(*rs);
268         rs->rs_msg = rs->rs_repbuf;
269
270         req->rq_reply_state = rs;
271         return 0;
272 }
273
274 static
275 void null_free_rs(struct ptlrpc_reply_state *rs)
276 {
277         LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
278         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
279
280         if (!rs->rs_prealloc)
281                 OBD_FREE(rs, rs->rs_size);
282 }
283
284 static
285 int null_authorize(struct ptlrpc_request *req)
286 {
287         struct ptlrpc_reply_state *rs = req->rq_reply_state;
288
289         LASSERT(rs);
290         if (rs->rs_repbuf->lm_magic != LUSTRE_MSG_MAGIC_V1)
291                 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
292         rs->rs_repdata_len = req->rq_replen;
293         return 0;
294 }
295
296 static struct ptlrpc_sec_cops null_sec_cops = {
297         .create_sec             = null_create_sec,
298         .destroy_sec            = null_destroy_sec,
299         .lookup_ctx             = null_lookup_ctx,
300         .flush_ctx_cache        = null_flush_ctx_cache,
301         .alloc_reqbuf           = null_alloc_reqbuf,
302         .alloc_repbuf           = null_alloc_repbuf,
303         .free_reqbuf            = null_free_reqbuf,
304         .free_repbuf            = null_free_repbuf,
305         .enlarge_reqbuf         = null_enlarge_reqbuf,
306 };
307
308 static struct ptlrpc_sec_sops null_sec_sops = {
309         .accept                 = null_accept,
310         .alloc_rs               = null_alloc_rs,
311         .authorize              = null_authorize,
312         .free_rs                = null_free_rs,
313 };
314
315 static struct ptlrpc_sec_policy null_policy = {
316         .sp_owner               = THIS_MODULE,
317         .sp_name                = "sec.null",
318         .sp_policy              = SPTLRPC_POLICY_NULL,
319         .sp_cops                = &null_sec_cops,
320         .sp_sops                = &null_sec_sops,
321 };
322
323 static
324 void null_init_internal(void)
325 {
326         static HLIST_HEAD(__list);
327
328         null_sec.ps_policy = &null_policy;
329         atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
330         null_sec.ps_import = NULL;
331         null_sec.ps_flavor = SPTLRPC_FLVR_NULL;
332         null_sec.ps_flags = 0;
333         spin_lock_init(&null_sec.ps_lock);
334         atomic_set(&null_sec.ps_busy, 1);         /* for "null_cli_ctx" */
335         INIT_LIST_HEAD(&null_sec.ps_gc_list);
336         null_sec.ps_gc_interval = 0;
337         null_sec.ps_gc_next = 0;
338
339         hlist_add_head(&null_cli_ctx.cc_cache, &__list);
340         atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
341         null_cli_ctx.cc_sec = &null_sec;
342         null_cli_ctx.cc_ops = &null_ctx_ops;
343         null_cli_ctx.cc_expire = 0;
344         null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
345                                 PTLRPC_CTX_UPTODATE;
346         null_cli_ctx.cc_vcred.vc_uid = 0;
347         spin_lock_init(&null_cli_ctx.cc_lock);
348         INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
349         INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
350 }
351
352 int sptlrpc_null_init(void)
353 {
354         int rc;
355
356         null_init_internal();
357
358         rc = sptlrpc_register_policy(&null_policy);
359         if (rc)
360                 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
361
362         return rc;
363 }
364
365 void sptlrpc_null_fini(void)
366 {
367         int rc;
368
369         rc = sptlrpc_unregister_policy(&null_policy);
370         if (rc)
371                 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);
372 }