1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004-2006 Cluster File Systems, Inc.
5 * Author: Eric Mei <ericm@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 # define EXPORT_SYMTAB
26 #define DEBUG_SUBSYSTEM S_SEC
29 #include <liblustre.h>
32 #include <obd_support.h>
33 #include <obd_class.h>
34 #include <lustre_net.h>
35 #include <lustre_sec.h>
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;
43 int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
45 /* should never reach here */
51 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
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;
60 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
62 req->rq_repmsg = req->rq_repbuf;
63 req->rq_replen = req->rq_repdata_len;
67 static struct ptlrpc_ctx_ops null_ctx_ops = {
68 .refresh = null_ctx_refresh,
69 .sign = null_ctx_sign,
70 .verify = null_ctx_verify,
73 static struct ptlrpc_svc_ctx null_svc_ctx = {
74 .sc_refcount = ATOMIC_INIT(1),
75 .sc_policy = &null_policy,
79 struct ptlrpc_sec* null_create_sec(struct obd_import *imp,
80 struct ptlrpc_svc_ctx *ctx,
84 LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_NULL);
89 void null_destroy_sec(struct ptlrpc_sec *sec)
91 LASSERT(sec == &null_sec);
95 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
96 struct vfs_cred *vcred,
97 int create, int remove_dead)
99 atomic_inc(&null_cli_ctx.cc_refcount);
100 return &null_cli_ctx;
104 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
106 int grace, int force)
112 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
113 struct ptlrpc_request *req,
116 if (!req->rq_reqbuf) {
117 int alloc_size = size_roundup_power2(msgsize);
119 LASSERT(!req->rq_pool);
120 OBD_ALLOC(req->rq_reqbuf, alloc_size);
124 req->rq_reqbuf_len = alloc_size;
126 LASSERT(req->rq_pool);
127 LASSERT(req->rq_reqbuf_len >= msgsize);
128 memset(req->rq_reqbuf, 0, msgsize);
131 req->rq_reqmsg = req->rq_reqbuf;
136 void null_free_reqbuf(struct ptlrpc_sec *sec,
137 struct ptlrpc_request *req)
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);
147 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
148 req->rq_reqmsg = req->rq_reqbuf = NULL;
149 req->rq_reqbuf_len = 0;
154 int null_alloc_repbuf(struct ptlrpc_sec *sec,
155 struct ptlrpc_request *req,
158 msgsize = size_roundup_power2(msgsize);
160 OBD_ALLOC(req->rq_repbuf, msgsize);
164 req->rq_repbuf_len = msgsize;
169 void null_free_repbuf(struct ptlrpc_sec *sec,
170 struct ptlrpc_request *req)
172 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
173 req->rq_repbuf = NULL;
174 req->rq_repbuf_len = 0;
178 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
179 struct ptlrpc_request *req,
180 int segment, int newsize)
182 struct lustre_msg *newbuf;
183 int oldsize, newmsg_size, alloc_size;
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));
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;
200 /* request from pool should always have enough buffer */
201 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
203 if (req->rq_reqbuf_len < newmsg_size) {
204 alloc_size = size_roundup_power2(newmsg_size);
206 OBD_ALLOC(newbuf, alloc_size);
210 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
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;
217 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
218 req->rq_reqlen = newmsg_size;
224 int null_accept(struct ptlrpc_request *req)
226 LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
228 if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
229 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
233 req->rq_reqmsg = req->rq_reqbuf;
234 req->rq_reqlen = req->rq_reqdata_len;
236 req->rq_svc_ctx = &null_svc_ctx;
237 atomic_inc(&req->rq_svc_ctx->sc_refcount);
243 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
245 struct ptlrpc_reply_state *rs;
246 int rs_size = sizeof(*rs) + msgsize;
248 LASSERT(msgsize % 8 == 0);
250 rs = req->rq_reply_state;
254 LASSERT(rs->rs_size >= rs_size);
256 OBD_ALLOC(rs, rs_size);
260 rs->rs_size = rs_size;
263 rs->rs_svc_ctx = req->rq_svc_ctx;
264 atomic_inc(&req->rq_svc_ctx->sc_refcount);
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;
270 req->rq_reply_state = rs;
275 void null_free_rs(struct ptlrpc_reply_state *rs)
277 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
278 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
280 if (!rs->rs_prealloc)
281 OBD_FREE(rs, rs->rs_size);
285 int null_authorize(struct ptlrpc_request *req)
287 struct ptlrpc_reply_state *rs = req->rq_reply_state;
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;
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,
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,
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,
324 void null_init_internal(void)
326 static HLIST_HEAD(__list);
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;
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 |
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);
352 int sptlrpc_null_init(void)
356 null_init_internal();
358 rc = sptlrpc_register_policy(&null_policy);
360 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
365 void sptlrpc_null_fini(void)
369 rc = sptlrpc_unregister_policy(&null_policy);
371 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);