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