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 struct lustre_msg *oldbuf = req->rq_reqmsg;
184 int oldsize, newmsg_size, alloc_size;
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));
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;
197 /* request from pool should always have enough buffer */
198 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
200 if (req->rq_reqbuf_len < newmsg_size) {
201 alloc_size = size_roundup_power2(newmsg_size);
203 OBD_ALLOC(newbuf, alloc_size);
207 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
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;
214 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
215 req->rq_reqlen = newmsg_size;
221 int null_accept(struct ptlrpc_request *req)
223 LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
225 if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
226 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
230 req->rq_reqmsg = req->rq_reqbuf;
231 req->rq_reqlen = req->rq_reqdata_len;
233 req->rq_svc_ctx = &null_svc_ctx;
234 atomic_inc(&req->rq_svc_ctx->sc_refcount);
240 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
242 struct ptlrpc_reply_state *rs;
243 int rs_size = sizeof(*rs) + msgsize;
245 LASSERT(msgsize % 8 == 0);
247 rs = req->rq_reply_state;
251 LASSERT(rs->rs_size >= rs_size);
253 OBD_ALLOC(rs, rs_size);
257 rs->rs_size = rs_size;
260 rs->rs_svc_ctx = req->rq_svc_ctx;
261 atomic_inc(&req->rq_svc_ctx->sc_refcount);
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;
267 req->rq_reply_state = rs;
272 void null_free_rs(struct ptlrpc_reply_state *rs)
274 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
275 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
277 if (!rs->rs_prealloc)
278 OBD_FREE(rs, rs->rs_size);
282 int null_authorize(struct ptlrpc_request *req)
284 struct ptlrpc_reply_state *rs = req->rq_reply_state;
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;
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,
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,
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,
321 void null_init_internal(void)
323 static HLIST_HEAD(__list);
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;
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 |
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);
349 int sptlrpc_null_init(void)
353 null_init_internal();
355 rc = sptlrpc_register_policy(&null_policy);
357 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
362 void sptlrpc_null_fini(void)
366 rc = sptlrpc_unregister_policy(&null_policy);
368 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);