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.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
28 #include <liblustre.h>
31 #include <obd_support.h>
32 #include <obd_class.h>
33 #include <lustre_net.h>
34 #include <lustre_sec.h>
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;
42 int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
44 /* should never reach here */
50 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
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;
59 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
61 req->rq_repmsg = req->rq_repbuf;
62 req->rq_replen = req->rq_repdata_len;
66 static struct ptlrpc_ctx_ops null_ctx_ops = {
67 .refresh = null_ctx_refresh,
68 .sign = null_ctx_sign,
69 .verify = null_ctx_verify,
72 static struct ptlrpc_svc_ctx null_svc_ctx = {
73 .sc_refcount = ATOMIC_INIT(1),
74 .sc_policy = &null_policy,
78 struct ptlrpc_sec* null_create_sec(struct obd_import *imp,
79 struct ptlrpc_svc_ctx *ctx,
83 LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_NULL);
88 void null_destroy_sec(struct ptlrpc_sec *sec)
90 LASSERT(sec == &null_sec);
94 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
95 struct vfs_cred *vcred,
96 int create, int remove_dead)
98 atomic_inc(&null_cli_ctx.cc_refcount);
103 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
105 int grace, int force)
111 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
112 struct ptlrpc_request *req,
115 if (!req->rq_reqbuf) {
116 int alloc_size = size_roundup_power2(msgsize);
118 LASSERT(!req->rq_pool);
119 OBD_ALLOC(req->rq_reqbuf, alloc_size);
123 req->rq_reqbuf_len = alloc_size;
125 LASSERT(req->rq_pool);
126 LASSERT(req->rq_reqbuf_len >= msgsize);
127 memset(req->rq_reqbuf, 0, msgsize);
130 req->rq_reqmsg = req->rq_reqbuf;
135 void null_free_reqbuf(struct ptlrpc_sec *sec,
136 struct ptlrpc_request *req)
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);
146 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
147 req->rq_reqmsg = req->rq_reqbuf = NULL;
148 req->rq_reqbuf_len = 0;
153 int null_alloc_repbuf(struct ptlrpc_sec *sec,
154 struct ptlrpc_request *req,
157 msgsize = size_roundup_power2(msgsize);
159 OBD_ALLOC(req->rq_repbuf, msgsize);
163 req->rq_repbuf_len = msgsize;
168 void null_free_repbuf(struct ptlrpc_sec *sec,
169 struct ptlrpc_request *req)
171 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
172 req->rq_repbuf = NULL;
173 req->rq_repbuf_len = 0;
177 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
178 struct ptlrpc_request *req,
179 int segment, int newsize)
181 struct lustre_msg *newbuf;
182 int oldsize, newmsg_size, alloc_size;
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));
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;
199 /* request from pool should always have enough buffer */
200 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
202 if (req->rq_reqbuf_len < newmsg_size) {
203 alloc_size = size_roundup_power2(newmsg_size);
205 OBD_ALLOC(newbuf, alloc_size);
209 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
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;
216 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
217 req->rq_reqlen = newmsg_size;
223 int null_accept(struct ptlrpc_request *req)
225 LASSERT(SEC_FLAVOR_POLICY(req->rq_sec_flavor) == SPTLRPC_POLICY_NULL);
227 if (SEC_FLAVOR_RPC(req->rq_sec_flavor) != SPTLRPC_FLVR_NULL) {
228 CERROR("Invalid flavor 0x%x\n", req->rq_sec_flavor);
232 req->rq_reqmsg = req->rq_reqbuf;
233 req->rq_reqlen = req->rq_reqdata_len;
235 req->rq_svc_ctx = &null_svc_ctx;
236 atomic_inc(&req->rq_svc_ctx->sc_refcount);
242 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
244 struct ptlrpc_reply_state *rs;
245 int rs_size = sizeof(*rs) + msgsize;
247 LASSERT(msgsize % 8 == 0);
249 rs = req->rq_reply_state;
253 LASSERT(rs->rs_size >= rs_size);
255 OBD_ALLOC(rs, rs_size);
259 rs->rs_size = rs_size;
262 rs->rs_svc_ctx = req->rq_svc_ctx;
263 atomic_inc(&req->rq_svc_ctx->sc_refcount);
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;
269 req->rq_reply_state = rs;
274 void null_free_rs(struct ptlrpc_reply_state *rs)
276 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
277 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
279 if (!rs->rs_prealloc)
280 OBD_FREE(rs, rs->rs_size);
284 int null_authorize(struct ptlrpc_request *req)
286 struct ptlrpc_reply_state *rs = req->rq_reply_state;
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;
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,
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,
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,
323 void null_init_internal(void)
325 static HLIST_HEAD(__list);
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;
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 |
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);
350 int sptlrpc_null_init(void)
354 null_init_internal();
356 rc = sptlrpc_register_policy(&null_policy);
358 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
363 void sptlrpc_null_fini(void)
367 rc = sptlrpc_unregister_policy(&null_policy);
369 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);