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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/ptlrpc/sec_plain.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"
48 struct ptlrpc_sec pls_base;
50 struct ptlrpc_cli_ctx *pls_ctx;
53 static inline struct plain_sec *sec2plsec(struct ptlrpc_sec *sec)
55 return container_of(sec, struct plain_sec, pls_base);
58 static struct ptlrpc_sec_policy plain_policy;
59 static struct ptlrpc_ctx_ops plain_ctx_ops;
60 static struct ptlrpc_svc_ctx plain_svc_ctx;
62 static unsigned int plain_at_offset;
65 * for simplicity, plain policy rpc use fixed layout.
67 #define PLAIN_PACK_SEGMENTS (4)
69 #define PLAIN_PACK_HDR_OFF (0)
70 #define PLAIN_PACK_MSG_OFF (1)
71 #define PLAIN_PACK_USER_OFF (2)
72 #define PLAIN_PACK_BULK_OFF (3)
74 #define PLAIN_FL_USER (0x01)
75 #define PLAIN_FL_BULK (0x02)
80 __u8 ph_sp; /* source */
81 __u8 ph_bulk_hash_alg; /* complete flavor desc */
85 struct plain_bulk_token {
89 #define PLAIN_BSD_SIZE \
90 (sizeof(struct ptlrpc_bulk_sec_desc) + sizeof(struct plain_bulk_token))
93 * bulk checksum helpers
96 static int plain_unpack_bsd(struct lustre_msg *msg, int swabbed)
98 struct ptlrpc_bulk_sec_desc *bsd;
100 if (bulk_sec_desc_unpack(msg, PLAIN_PACK_BULK_OFF, swabbed))
103 bsd = lustre_msg_buf(msg, PLAIN_PACK_BULK_OFF, PLAIN_BSD_SIZE);
105 CERROR("bulk sec desc has short size %d\n",
106 lustre_msg_buflen(msg, PLAIN_PACK_BULK_OFF));
110 if (bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
111 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG) {
112 CERROR("invalid bulk svc %u\n", bsd->bsd_svc);
119 static int plain_generate_bulk_csum(struct ptlrpc_bulk_desc *desc,
121 struct plain_bulk_token *token)
123 if (hash_alg == BULK_HASH_ALG_NULL)
126 memset(token->pbt_hash, 0, sizeof(token->pbt_hash));
127 return sptlrpc_get_bulk_checksum(desc, hash_alg, token->pbt_hash,
128 sizeof(token->pbt_hash));
131 static int plain_verify_bulk_csum(struct ptlrpc_bulk_desc *desc,
133 struct plain_bulk_token *tokenr)
135 struct plain_bulk_token tokenv;
138 if (hash_alg == BULK_HASH_ALG_NULL)
141 memset(&tokenv.pbt_hash, 0, sizeof(tokenv.pbt_hash));
142 rc = sptlrpc_get_bulk_checksum(desc, hash_alg, tokenv.pbt_hash,
143 sizeof(tokenv.pbt_hash));
147 if (memcmp(tokenr->pbt_hash, tokenv.pbt_hash, sizeof(tokenr->pbt_hash)))
152 static void corrupt_bulk_data(struct ptlrpc_bulk_desc *desc)
157 for (i = 0; i < desc->bd_iov_count; i++) {
158 if (desc->bd_vec[i].bv_len == 0)
161 ptr = kmap(desc->bd_vec[i].bv_page);
162 off = desc->bd_vec[i].bv_offset & ~PAGE_MASK;
164 kunmap(desc->bd_vec[i].bv_page);
174 int plain_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
176 /* should never reach here */
182 int plain_ctx_validate(struct ptlrpc_cli_ctx *ctx)
188 int plain_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
190 struct lustre_msg *msg = req->rq_reqbuf;
191 struct plain_header *phdr;
195 msg->lm_secflvr = req->rq_flvr.sf_rpc;
197 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
200 phdr->ph_sp = ctx->cc_sec->ps_part;
201 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
203 if (req->rq_pack_udesc)
204 phdr->ph_flags |= PLAIN_FL_USER;
205 if (req->rq_pack_bulk)
206 phdr->ph_flags |= PLAIN_FL_BULK;
208 req->rq_reqdata_len = lustre_msg_size_v2(msg->lm_bufcount,
214 int plain_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
216 struct lustre_msg *msg = req->rq_repdata;
217 struct plain_header *phdr;
221 if (msg->lm_bufcount != PLAIN_PACK_SEGMENTS) {
222 CERROR("unexpected reply buf count %u\n", msg->lm_bufcount);
226 swabbed = req_capsule_rep_need_swab(&req->rq_pill);
228 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
230 CERROR("missing plain header\n");
234 if (phdr->ph_ver != 0) {
235 CERROR("Invalid header version\n");
239 /* expect no user desc in reply */
240 if (phdr->ph_flags & PLAIN_FL_USER) {
241 CERROR("Unexpected udesc flag in reply\n");
245 if (phdr->ph_bulk_hash_alg != req->rq_flvr.u_bulk.hash.hash_alg) {
246 CERROR("reply bulk flavor %u != %u\n", phdr->ph_bulk_hash_alg,
247 req->rq_flvr.u_bulk.hash.hash_alg);
251 if (unlikely(req->rq_early)) {
252 __u32 cksum = lustre_msg_calc_cksum(msg, PLAIN_PACK_MSG_OFF);
254 if (cksum != msg->lm_cksum) {
256 "early reply checksum mismatch: %08x != %08x\n",
257 cpu_to_le32(cksum), msg->lm_cksum);
262 * whether we sent with bulk or not, we expect the same
263 * in reply, except for early reply
265 if (!req->rq_early &&
266 !equi(req->rq_pack_bulk == 1,
267 phdr->ph_flags & PLAIN_FL_BULK)) {
268 CERROR("%s bulk checksum in reply\n",
269 req->rq_pack_bulk ? "Missing" : "Unexpected");
273 if (phdr->ph_flags & PLAIN_FL_BULK) {
274 if (plain_unpack_bsd(msg, swabbed))
279 req->rq_repmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
280 req->rq_replen = lustre_msg_buflen(msg, PLAIN_PACK_MSG_OFF);
285 int plain_cli_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
286 struct ptlrpc_request *req,
287 struct ptlrpc_bulk_desc *desc)
289 struct ptlrpc_bulk_sec_desc *bsd;
290 struct plain_bulk_token *token;
293 LASSERT(req->rq_pack_bulk);
294 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
296 bsd = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
297 token = (struct plain_bulk_token *) bsd->bsd_data;
299 bsd->bsd_version = 0;
301 bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
302 bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
304 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
307 if (req->rq_bulk_read)
310 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
313 CERROR("bulk write: failed to compute checksum: %d\n", rc);
316 * for sending we only compute the wrong checksum instead
317 * of corrupting the data so it is still correct on a redo
319 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_SEND) &&
320 req->rq_flvr.u_bulk.hash.hash_alg != BULK_HASH_ALG_NULL)
321 token->pbt_hash[0] ^= 0x1;
328 int plain_cli_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
329 struct ptlrpc_request *req,
330 struct ptlrpc_bulk_desc *desc)
332 struct ptlrpc_bulk_sec_desc *bsdv;
333 struct plain_bulk_token *tokenv;
337 LASSERT(req->rq_pack_bulk);
338 LASSERT(req->rq_reqbuf->lm_bufcount == PLAIN_PACK_SEGMENTS);
339 LASSERT(req->rq_repdata->lm_bufcount == PLAIN_PACK_SEGMENTS);
341 bsdv = lustre_msg_buf(req->rq_repdata, PLAIN_PACK_BULK_OFF, 0);
342 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
344 if (req->rq_bulk_write) {
345 if (bsdv->bsd_flags & BSD_FL_ERR)
350 /* fix the actual data size */
351 for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
352 if (desc->bd_vec[i].bv_len +
353 nob > desc->bd_nob_transferred) {
354 desc->bd_vec[i].bv_len =
355 desc->bd_nob_transferred - nob;
357 nob += desc->bd_vec[i].bv_len;
360 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
363 CERROR("bulk read: client verify failed: %d\n", rc);
373 struct ptlrpc_cli_ctx *plain_sec_install_ctx(struct plain_sec *plsec)
375 struct ptlrpc_cli_ctx *ctx, *ctx_new;
377 OBD_ALLOC_PTR(ctx_new);
379 write_lock(&plsec->pls_lock);
381 ctx = plsec->pls_ctx;
383 atomic_inc(&ctx->cc_refcount);
386 OBD_FREE_PTR(ctx_new);
387 } else if (ctx_new) {
390 atomic_set(&ctx->cc_refcount, 1); /* for cache */
391 ctx->cc_sec = &plsec->pls_base;
392 ctx->cc_ops = &plain_ctx_ops;
394 ctx->cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_UPTODATE;
395 ctx->cc_vcred.vc_uid = 0;
396 spin_lock_init(&ctx->cc_lock);
397 INIT_LIST_HEAD(&ctx->cc_req_list);
398 INIT_LIST_HEAD(&ctx->cc_gc_chain);
400 plsec->pls_ctx = ctx;
401 atomic_inc(&plsec->pls_base.ps_nctx);
402 atomic_inc(&plsec->pls_base.ps_refcount);
404 atomic_inc(&ctx->cc_refcount); /* for caller */
407 write_unlock(&plsec->pls_lock);
413 void plain_destroy_sec(struct ptlrpc_sec *sec)
415 struct plain_sec *plsec = sec2plsec(sec);
419 LASSERT(sec->ps_policy == &plain_policy);
420 LASSERT(sec->ps_import);
421 LASSERT(atomic_read(&sec->ps_refcount) == 0);
422 LASSERT(atomic_read(&sec->ps_nctx) == 0);
423 LASSERT(plsec->pls_ctx == NULL);
425 class_import_put(sec->ps_import);
432 void plain_kill_sec(struct ptlrpc_sec *sec)
438 struct ptlrpc_sec *plain_create_sec(struct obd_import *imp,
439 struct ptlrpc_svc_ctx *svc_ctx,
440 struct sptlrpc_flavor *sf)
442 struct plain_sec *plsec;
443 struct ptlrpc_sec *sec;
444 struct ptlrpc_cli_ctx *ctx;
448 LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN);
450 OBD_ALLOC_PTR(plsec);
455 * initialize plain_sec
457 rwlock_init(&plsec->pls_lock);
458 plsec->pls_ctx = NULL;
460 sec = &plsec->pls_base;
461 sec->ps_policy = &plain_policy;
462 atomic_set(&sec->ps_refcount, 0);
463 atomic_set(&sec->ps_nctx, 0);
464 sec->ps_id = sptlrpc_get_next_secid();
465 sec->ps_import = class_import_get(imp);
467 spin_lock_init(&sec->ps_lock);
468 INIT_LIST_HEAD(&sec->ps_gc_list);
469 sec->ps_gc_interval = 0;
472 /* install ctx immediately if this is a reverse sec */
474 ctx = plain_sec_install_ctx(plsec);
476 plain_destroy_sec(sec);
479 sptlrpc_cli_ctx_put(ctx, 1);
486 struct ptlrpc_cli_ctx *plain_lookup_ctx(struct ptlrpc_sec *sec,
487 struct vfs_cred *vcred,
488 int create, int remove_dead)
490 struct plain_sec *plsec = sec2plsec(sec);
491 struct ptlrpc_cli_ctx *ctx;
495 read_lock(&plsec->pls_lock);
496 ctx = plsec->pls_ctx;
498 atomic_inc(&ctx->cc_refcount);
499 read_unlock(&plsec->pls_lock);
501 if (unlikely(ctx == NULL))
502 ctx = plain_sec_install_ctx(plsec);
508 void plain_release_ctx(struct ptlrpc_sec *sec,
509 struct ptlrpc_cli_ctx *ctx, int sync)
511 LASSERT(atomic_read(&sec->ps_refcount) > 0);
512 LASSERT(atomic_read(&sec->ps_nctx) > 0);
513 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
514 LASSERT(ctx->cc_sec == sec);
518 atomic_dec(&sec->ps_nctx);
519 sptlrpc_sec_put(sec);
523 int plain_flush_ctx_cache(struct ptlrpc_sec *sec,
524 uid_t uid, int grace, int force)
526 struct plain_sec *plsec = sec2plsec(sec);
527 struct ptlrpc_cli_ctx *ctx;
531 /* do nothing unless caller want to flush for 'all' */
535 write_lock(&plsec->pls_lock);
536 ctx = plsec->pls_ctx;
537 plsec->pls_ctx = NULL;
538 write_unlock(&plsec->pls_lock);
541 sptlrpc_cli_ctx_put(ctx, 1);
546 int plain_alloc_reqbuf(struct ptlrpc_sec *sec,
547 struct ptlrpc_request *req,
550 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
555 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
556 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
558 if (req->rq_pack_udesc)
559 buflens[PLAIN_PACK_USER_OFF] = sptlrpc_current_user_desc_size();
561 if (req->rq_pack_bulk) {
562 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
563 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
566 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
568 if (!req->rq_reqbuf) {
569 LASSERT(!req->rq_pool);
571 alloc_len = size_roundup_power2(alloc_len);
572 OBD_ALLOC_LARGE(req->rq_reqbuf, alloc_len);
576 req->rq_reqbuf_len = alloc_len;
578 LASSERT(req->rq_pool);
579 LASSERT(req->rq_reqbuf_len >= alloc_len);
580 memset(req->rq_reqbuf, 0, alloc_len);
583 lustre_init_msg_v2(req->rq_reqbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
584 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0);
586 if (req->rq_pack_udesc)
587 sptlrpc_pack_user_desc(req->rq_reqbuf, PLAIN_PACK_USER_OFF);
593 void plain_free_reqbuf(struct ptlrpc_sec *sec,
594 struct ptlrpc_request *req)
598 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
599 req->rq_reqbuf = NULL;
600 req->rq_reqbuf_len = 0;
606 int plain_alloc_repbuf(struct ptlrpc_sec *sec,
607 struct ptlrpc_request *req,
610 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
615 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
616 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
618 if (req->rq_pack_bulk) {
619 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
620 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
623 alloc_len = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
625 /* add space for early reply */
626 alloc_len += plain_at_offset;
628 alloc_len = size_roundup_power2(alloc_len);
630 OBD_ALLOC_LARGE(req->rq_repbuf, alloc_len);
634 req->rq_repbuf_len = alloc_len;
639 void plain_free_repbuf(struct ptlrpc_sec *sec,
640 struct ptlrpc_request *req)
643 OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
644 req->rq_repbuf = NULL;
645 req->rq_repbuf_len = 0;
650 int plain_enlarge_reqbuf(struct ptlrpc_sec *sec,
651 struct ptlrpc_request *req,
652 int segment, int newsize)
654 struct lustre_msg *newbuf;
656 int newmsg_size, newbuf_size;
660 LASSERT(req->rq_reqbuf);
661 LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
662 LASSERT(lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_MSG_OFF, 0) ==
665 /* compute new embedded msg size. */
666 oldsize = req->rq_reqmsg->lm_buflens[segment];
667 req->rq_reqmsg->lm_buflens[segment] = newsize;
668 newmsg_size = lustre_msg_size_v2(req->rq_reqmsg->lm_bufcount,
669 req->rq_reqmsg->lm_buflens);
670 req->rq_reqmsg->lm_buflens[segment] = oldsize;
672 /* compute new wrapper msg size. */
673 oldsize = req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF];
674 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = newmsg_size;
675 newbuf_size = lustre_msg_size_v2(req->rq_reqbuf->lm_bufcount,
676 req->rq_reqbuf->lm_buflens);
677 req->rq_reqbuf->lm_buflens[PLAIN_PACK_MSG_OFF] = oldsize;
679 /* request from pool should always have enough buffer */
680 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
682 if (req->rq_reqbuf_len < newbuf_size) {
683 newbuf_size = size_roundup_power2(newbuf_size);
685 OBD_ALLOC_LARGE(newbuf, newbuf_size);
690 * Must lock this, so that otherwise unprotected change of
691 * rq_reqmsg is not racing with parallel processing of
692 * imp_replay_list traversing threads. See LU-3333
693 * This is a bandaid at best, we really need to deal with this
694 * in request enlarging code before unpacking that's already
698 spin_lock(&req->rq_import->imp_lock);
700 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
702 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
703 req->rq_reqbuf = newbuf;
704 req->rq_reqbuf_len = newbuf_size;
705 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf,
706 PLAIN_PACK_MSG_OFF, 0);
709 spin_unlock(&req->rq_import->imp_lock);
712 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, PLAIN_PACK_MSG_OFF,
714 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
716 req->rq_reqlen = newmsg_size;
724 static struct ptlrpc_svc_ctx plain_svc_ctx = {
725 .sc_refcount = ATOMIC_INIT(1),
726 .sc_policy = &plain_policy,
729 static int plain_accept(struct ptlrpc_request *req)
731 struct lustre_msg *msg = req->rq_reqbuf;
732 struct plain_header *phdr;
736 LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
737 SPTLRPC_POLICY_PLAIN);
739 if (SPTLRPC_FLVR_BASE(req->rq_flvr.sf_rpc) !=
740 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN) ||
741 SPTLRPC_FLVR_BULK_TYPE(req->rq_flvr.sf_rpc) !=
742 SPTLRPC_FLVR_BULK_TYPE(SPTLRPC_FLVR_PLAIN)) {
743 CERROR("Invalid rpc flavor %x\n", req->rq_flvr.sf_rpc);
747 if (msg->lm_bufcount < PLAIN_PACK_SEGMENTS) {
748 CERROR("unexpected request buf count %u\n", msg->lm_bufcount);
752 swabbed = req_capsule_req_need_swab(&req->rq_pill);
754 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, sizeof(*phdr));
756 CERROR("missing plain header\n");
760 if (phdr->ph_ver != 0) {
761 CERROR("Invalid header version\n");
765 if (phdr->ph_bulk_hash_alg >= BULK_HASH_ALG_MAX) {
766 CERROR("invalid hash algorithm: %u\n", phdr->ph_bulk_hash_alg);
770 req->rq_sp_from = phdr->ph_sp;
771 req->rq_flvr.u_bulk.hash.hash_alg = phdr->ph_bulk_hash_alg;
773 if (phdr->ph_flags & PLAIN_FL_USER) {
774 if (sptlrpc_unpack_user_desc(msg, PLAIN_PACK_USER_OFF,
776 CERROR("Mal-formed user descriptor\n");
780 req->rq_pack_udesc = 1;
781 req->rq_user_desc = lustre_msg_buf(msg, PLAIN_PACK_USER_OFF, 0);
784 if (phdr->ph_flags & PLAIN_FL_BULK) {
785 if (plain_unpack_bsd(msg, swabbed))
788 req->rq_pack_bulk = 1;
791 req->rq_reqmsg = lustre_msg_buf(msg, PLAIN_PACK_MSG_OFF, 0);
792 req->rq_reqlen = msg->lm_buflens[PLAIN_PACK_MSG_OFF];
794 req->rq_svc_ctx = &plain_svc_ctx;
795 atomic_inc(&req->rq_svc_ctx->sc_refcount);
801 int plain_alloc_rs(struct ptlrpc_request *req, int msgsize)
803 struct ptlrpc_reply_state *rs;
804 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
805 int rs_size = sizeof(*rs);
809 LASSERT(msgsize % 8 == 0);
811 buflens[PLAIN_PACK_HDR_OFF] = sizeof(struct plain_header);
812 buflens[PLAIN_PACK_MSG_OFF] = msgsize;
814 if (req->rq_pack_bulk && (req->rq_bulk_read || req->rq_bulk_write))
815 buflens[PLAIN_PACK_BULK_OFF] = PLAIN_BSD_SIZE;
817 rs_size += lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
819 rs = req->rq_reply_state;
823 LASSERT(rs->rs_size >= rs_size);
825 OBD_ALLOC_LARGE(rs, rs_size);
829 rs->rs_size = rs_size;
832 rs->rs_svc_ctx = req->rq_svc_ctx;
833 atomic_inc(&req->rq_svc_ctx->sc_refcount);
834 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
835 rs->rs_repbuf_len = rs_size - sizeof(*rs);
837 lustre_init_msg_v2(rs->rs_repbuf, PLAIN_PACK_SEGMENTS, buflens, NULL);
838 rs->rs_msg = lustre_msg_buf_v2(rs->rs_repbuf, PLAIN_PACK_MSG_OFF, 0);
840 req->rq_reply_state = rs;
845 void plain_free_rs(struct ptlrpc_reply_state *rs)
849 LASSERT(atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
850 atomic_dec(&rs->rs_svc_ctx->sc_refcount);
852 if (!rs->rs_prealloc)
853 OBD_FREE_LARGE(rs, rs->rs_size);
858 int plain_authorize(struct ptlrpc_request *req)
860 struct ptlrpc_reply_state *rs = req->rq_reply_state;
861 struct lustre_msg_v2 *msg = rs->rs_repbuf;
862 struct plain_header *phdr;
870 if (req->rq_replen != msg->lm_buflens[PLAIN_PACK_MSG_OFF])
871 len = lustre_shrink_msg(msg, PLAIN_PACK_MSG_OFF,
874 len = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
876 msg->lm_secflvr = req->rq_flvr.sf_rpc;
878 phdr = lustre_msg_buf(msg, PLAIN_PACK_HDR_OFF, 0);
881 phdr->ph_bulk_hash_alg = req->rq_flvr.u_bulk.hash.hash_alg;
883 if (req->rq_pack_bulk)
884 phdr->ph_flags |= PLAIN_FL_BULK;
886 rs->rs_repdata_len = len;
887 req->rq_reply_off = 0;
889 if (likely(req->rq_packed_final)) {
890 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
891 req->rq_reply_off = plain_at_offset;
893 msg->lm_cksum = lustre_msg_calc_cksum(msg, PLAIN_PACK_MSG_OFF);
900 int plain_svc_unwrap_bulk(struct ptlrpc_request *req,
901 struct ptlrpc_bulk_desc *desc)
903 struct ptlrpc_reply_state *rs = req->rq_reply_state;
904 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
905 struct plain_bulk_token *tokenr;
908 LASSERT(req->rq_bulk_write);
909 LASSERT(req->rq_pack_bulk);
911 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
912 tokenr = (struct plain_bulk_token *) bsdr->bsd_data;
913 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
915 bsdv->bsd_version = 0;
916 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
917 bsdv->bsd_svc = bsdr->bsd_svc;
920 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
923 rc = plain_verify_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
926 bsdv->bsd_flags |= BSD_FL_ERR;
927 CERROR("bulk write: server verify failed: %d\n", rc);
934 int plain_svc_wrap_bulk(struct ptlrpc_request *req,
935 struct ptlrpc_bulk_desc *desc)
937 struct ptlrpc_reply_state *rs = req->rq_reply_state;
938 struct ptlrpc_bulk_sec_desc *bsdr, *bsdv;
939 struct plain_bulk_token *tokenv;
942 LASSERT(req->rq_bulk_read);
943 LASSERT(req->rq_pack_bulk);
945 bsdr = lustre_msg_buf(req->rq_reqbuf, PLAIN_PACK_BULK_OFF, 0);
946 bsdv = lustre_msg_buf(rs->rs_repbuf, PLAIN_PACK_BULK_OFF, 0);
947 tokenv = (struct plain_bulk_token *) bsdv->bsd_data;
949 bsdv->bsd_version = 0;
950 bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
951 bsdv->bsd_svc = bsdr->bsd_svc;
954 if (bsdr->bsd_svc == SPTLRPC_BULK_SVC_NULL)
957 rc = plain_generate_bulk_csum(desc, req->rq_flvr.u_bulk.hash.hash_alg,
960 CERROR("bulk read: server failed to compute checksum: %d\n",
963 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CHECKSUM_RECEIVE))
964 corrupt_bulk_data(desc);
970 static struct ptlrpc_ctx_ops plain_ctx_ops = {
971 .refresh = plain_ctx_refresh,
972 .validate = plain_ctx_validate,
973 .sign = plain_ctx_sign,
974 .verify = plain_ctx_verify,
975 .wrap_bulk = plain_cli_wrap_bulk,
976 .unwrap_bulk = plain_cli_unwrap_bulk,
979 static struct ptlrpc_sec_cops plain_sec_cops = {
980 .create_sec = plain_create_sec,
981 .destroy_sec = plain_destroy_sec,
982 .kill_sec = plain_kill_sec,
983 .lookup_ctx = plain_lookup_ctx,
984 .release_ctx = plain_release_ctx,
985 .flush_ctx_cache = plain_flush_ctx_cache,
986 .alloc_reqbuf = plain_alloc_reqbuf,
987 .free_reqbuf = plain_free_reqbuf,
988 .alloc_repbuf = plain_alloc_repbuf,
989 .free_repbuf = plain_free_repbuf,
990 .enlarge_reqbuf = plain_enlarge_reqbuf,
993 static struct ptlrpc_sec_sops plain_sec_sops = {
994 .accept = plain_accept,
995 .alloc_rs = plain_alloc_rs,
996 .authorize = plain_authorize,
997 .free_rs = plain_free_rs,
998 .unwrap_bulk = plain_svc_unwrap_bulk,
999 .wrap_bulk = plain_svc_wrap_bulk,
1002 static struct ptlrpc_sec_policy plain_policy = {
1003 .sp_owner = THIS_MODULE,
1005 .sp_policy = SPTLRPC_POLICY_PLAIN,
1006 .sp_cops = &plain_sec_cops,
1007 .sp_sops = &plain_sec_sops,
1010 int sptlrpc_plain_init(void)
1012 __u32 buflens[PLAIN_PACK_SEGMENTS] = { 0, };
1015 buflens[PLAIN_PACK_MSG_OFF] = lustre_msg_early_size;
1016 plain_at_offset = lustre_msg_size_v2(PLAIN_PACK_SEGMENTS, buflens);
1018 rc = sptlrpc_register_policy(&plain_policy);
1020 CERROR("failed to register: %d\n", rc);
1025 void sptlrpc_plain_fini(void)
1029 rc = sptlrpc_unregister_policy(&plain_policy);
1031 CERROR("cannot unregister: %d\n", rc);