4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/ptlrpc/sec_null.c
33 * Author: Eric Mei <ericm@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_SEC
39 #include <obd_support.h>
40 #include <obd_cksum.h>
41 #include <obd_class.h>
42 #include <lustre_net.h>
43 #include <lustre_sec.h>
45 #include "ptlrpc_internal.h"
47 static struct ptlrpc_sec_policy null_policy;
48 static struct ptlrpc_sec null_sec;
49 static struct ptlrpc_cli_ctx null_cli_ctx;
50 static struct ptlrpc_svc_ctx null_svc_ctx;
53 * we can temporarily use the topmost 8-bits of lm_secflvr to identify
54 * the source sec part.
57 void null_encode_sec_part(struct lustre_msg *msg, enum lustre_sec_part sp)
59 msg->lm_secflvr |= (((__u32) sp) & 0xFF) << 24;
63 enum lustre_sec_part null_decode_sec_part(struct lustre_msg *msg)
65 return (msg->lm_secflvr >> 24) & 0xFF;
69 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
71 req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
73 if (!req->rq_import->imp_dlm_fake) {
74 struct obd_device *obd = req->rq_import->imp_obd;
76 null_encode_sec_part(req->rq_reqbuf,
79 req->rq_reqdata_len = req->rq_reqlen;
84 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
88 LASSERT(req->rq_repdata);
90 req->rq_repmsg = req->rq_repdata;
91 req->rq_replen = req->rq_repdata_len;
94 cksums = lustre_msg_get_cksum(req->rq_repdata);
95 cksumc = lustre_msg_calc_cksum(req->rq_repmsg,
98 if (cksumc != cksums) {
100 "early reply checksum mismatch: %08x != %08x\n",
110 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
111 struct ptlrpc_svc_ctx *svc_ctx,
112 struct sptlrpc_flavor *sf)
114 LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
117 * general layer has take a module reference for us, because we never
118 * really destroy the sec, simply release the reference here.
120 sptlrpc_policy_put(&null_policy);
125 void null_destroy_sec(struct ptlrpc_sec *sec)
127 LASSERT(sec == &null_sec);
131 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
132 struct vfs_cred *vcred,
133 int create, int remove_dead)
135 atomic_inc(&null_cli_ctx.cc_refcount);
136 return &null_cli_ctx;
140 int null_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid, int grace,
147 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
148 struct ptlrpc_request *req,
151 if (!req->rq_reqbuf) {
152 int alloc_size = size_roundup_power2(msgsize);
154 LASSERT(!req->rq_pool);
155 OBD_ALLOC_LARGE(req->rq_reqbuf, alloc_size);
159 req->rq_reqbuf_len = alloc_size;
161 LASSERT(req->rq_pool);
162 LASSERT(req->rq_reqbuf_len >= msgsize);
163 memset(req->rq_reqbuf, 0, msgsize);
166 req->rq_reqmsg = req->rq_reqbuf;
171 void null_free_reqbuf(struct ptlrpc_sec *sec,
172 struct ptlrpc_request *req)
175 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
176 "req %p: reqmsg %p is not reqbuf %p in null sec\n",
177 req, req->rq_reqmsg, req->rq_reqbuf);
178 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
179 "req %p: reqlen %d should smaller than buflen %d\n",
180 req, req->rq_reqlen, req->rq_reqbuf_len);
182 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
183 req->rq_reqbuf = NULL;
184 req->rq_reqbuf_len = 0;
189 int null_alloc_repbuf(struct ptlrpc_sec *sec,
190 struct ptlrpc_request *req,
193 /* add space for early replied */
194 msgsize += lustre_msg_early_size;
196 msgsize = size_roundup_power2(msgsize);
198 OBD_ALLOC_LARGE(req->rq_repbuf, msgsize);
202 req->rq_repbuf_len = msgsize;
207 void null_free_repbuf(struct ptlrpc_sec *sec,
208 struct ptlrpc_request *req)
210 LASSERT(req->rq_repbuf);
212 OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
213 req->rq_repbuf = NULL;
214 req->rq_repbuf_len = 0;
218 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
219 struct ptlrpc_request *req,
220 int segment, int newsize)
222 struct lustre_msg *newbuf;
223 struct lustre_msg *oldbuf = req->rq_reqmsg;
224 int oldsize, newmsg_size, alloc_size;
226 LASSERT(req->rq_reqbuf);
227 LASSERT(req->rq_reqbuf == req->rq_reqmsg);
228 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
229 LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
231 /* compute new message size */
232 oldsize = req->rq_reqbuf->lm_buflens[segment];
233 req->rq_reqbuf->lm_buflens[segment] = newsize;
234 newmsg_size = lustre_packed_msg_size(oldbuf);
235 req->rq_reqbuf->lm_buflens[segment] = oldsize;
237 /* request from pool should always have enough buffer */
238 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
240 if (req->rq_reqbuf_len < newmsg_size) {
241 alloc_size = size_roundup_power2(newmsg_size);
243 OBD_ALLOC_LARGE(newbuf, alloc_size);
248 * Must lock this, so that otherwise unprotected change of
249 * rq_reqmsg is not racing with parallel processing of
250 * imp_replay_list traversing threads. See LU-3333
251 * This is a bandaid at best, we really need to deal with this
252 * in request enlarging code before unpacking that's already
256 spin_lock(&req->rq_import->imp_lock);
257 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
259 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
260 req->rq_reqbuf = req->rq_reqmsg = newbuf;
261 req->rq_reqbuf_len = alloc_size;
264 spin_unlock(&req->rq_import->imp_lock);
267 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
268 req->rq_reqlen = newmsg_size;
273 static struct ptlrpc_svc_ctx null_svc_ctx = {
274 .sc_refcount = ATOMIC_INIT(1),
275 .sc_policy = &null_policy,
279 int null_accept(struct ptlrpc_request *req)
281 LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
282 SPTLRPC_POLICY_NULL);
284 if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
285 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
289 req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
291 req->rq_reqmsg = req->rq_reqbuf;
292 req->rq_reqlen = req->rq_reqdata_len;
294 req->rq_svc_ctx = &null_svc_ctx;
295 atomic_inc(&req->rq_svc_ctx->sc_refcount);
301 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
303 struct ptlrpc_reply_state *rs;
304 int rs_size = sizeof(*rs) + msgsize;
306 LASSERT(msgsize % 8 == 0);
308 rs = req->rq_reply_state;
312 LASSERT(rs->rs_size >= rs_size);
314 OBD_ALLOC_LARGE(rs, rs_size);
318 rs->rs_size = rs_size;
321 rs->rs_svc_ctx = req->rq_svc_ctx;
322 atomic_inc(&req->rq_svc_ctx->sc_refcount);
324 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
325 rs->rs_repbuf_len = rs_size - sizeof(*rs);
326 rs->rs_msg = rs->rs_repbuf;
328 req->rq_reply_state = rs;
333 void null_free_rs(struct ptlrpc_reply_state *rs)
335 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
336 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
338 if (!rs->rs_prealloc)
339 OBD_FREE_LARGE(rs, rs->rs_size);
343 int null_authorize(struct ptlrpc_request *req)
345 struct ptlrpc_reply_state *rs = req->rq_reply_state;
349 rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
350 rs->rs_repdata_len = req->rq_replen;
351 req->rq_reply_off = 0;
353 if (likely(req->rq_packed_final)) {
354 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
355 req->rq_reply_off = lustre_msg_early_size;
359 cksum = lustre_msg_calc_cksum(rs->rs_repbuf,
360 MSG_PTLRPC_BODY_OFF);
361 lustre_msg_set_cksum(rs->rs_repbuf, cksum);
367 static struct ptlrpc_ctx_ops null_ctx_ops = {
368 .sign = null_ctx_sign,
369 .verify = null_ctx_verify,
372 static struct ptlrpc_sec_cops null_sec_cops = {
373 .create_sec = null_create_sec,
374 .destroy_sec = null_destroy_sec,
375 .lookup_ctx = null_lookup_ctx,
376 .flush_ctx_cache = null_flush_ctx_cache,
377 .alloc_reqbuf = null_alloc_reqbuf,
378 .alloc_repbuf = null_alloc_repbuf,
379 .free_reqbuf = null_free_reqbuf,
380 .free_repbuf = null_free_repbuf,
381 .enlarge_reqbuf = null_enlarge_reqbuf,
384 static struct ptlrpc_sec_sops null_sec_sops = {
385 .accept = null_accept,
386 .alloc_rs = null_alloc_rs,
387 .authorize = null_authorize,
388 .free_rs = null_free_rs,
391 static struct ptlrpc_sec_policy null_policy = {
392 .sp_owner = THIS_MODULE,
393 .sp_name = "sec.null",
394 .sp_policy = SPTLRPC_POLICY_NULL,
395 .sp_cops = &null_sec_cops,
396 .sp_sops = &null_sec_sops,
399 static void null_init_internal(void)
401 static HLIST_HEAD(__list);
403 null_sec.ps_policy = &null_policy;
404 atomic_set(&null_sec.ps_refcount, 1); /* always busy */
406 null_sec.ps_import = NULL;
407 null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
408 null_sec.ps_flvr.sf_flags = 0;
409 null_sec.ps_part = LUSTRE_SP_ANY;
410 null_sec.ps_dying = 0;
411 spin_lock_init(&null_sec.ps_lock);
412 atomic_set(&null_sec.ps_nctx, 1); /* for "null_cli_ctx" */
413 INIT_LIST_HEAD(&null_sec.ps_gc_list);
414 null_sec.ps_gc_interval = 0;
415 null_sec.ps_gc_next = 0;
417 hlist_add_head(&null_cli_ctx.cc_cache, &__list);
418 atomic_set(&null_cli_ctx.cc_refcount, 1); /* for hash */
419 null_cli_ctx.cc_sec = &null_sec;
420 null_cli_ctx.cc_ops = &null_ctx_ops;
421 null_cli_ctx.cc_expire = 0;
422 null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
424 null_cli_ctx.cc_vcred.vc_uid = 0;
425 spin_lock_init(&null_cli_ctx.cc_lock);
426 INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
427 INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
430 int sptlrpc_null_init(void)
434 null_init_internal();
436 rc = sptlrpc_register_policy(&null_policy);
438 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
443 void sptlrpc_null_fini(void)
447 rc = sptlrpc_unregister_policy(&null_policy);
449 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,