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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Eric Mei <ericm@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
43 #include "../../include/linux/libcfs/libcfs.h"
44 #include <linux/crypto.h>
45 #include <linux/key.h>
47 #include "../include/obd.h"
48 #include "../include/obd_class.h"
49 #include "../include/obd_support.h"
50 #include "../include/lustre_net.h"
51 #include "../include/lustre_import.h"
52 #include "../include/lustre_dlm.h"
53 #include "../include/lustre_sec.h"
55 #include "ptlrpc_internal.h"
57 /***********************************************
59 ***********************************************/
61 static rwlock_t policy_lock;
62 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
66 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
68 __u16 number = policy->sp_policy;
70 LASSERT(policy->sp_name);
71 LASSERT(policy->sp_cops);
72 LASSERT(policy->sp_sops);
74 if (number >= SPTLRPC_POLICY_MAX)
77 write_lock(&policy_lock);
78 if (unlikely(policies[number])) {
79 write_unlock(&policy_lock);
82 policies[number] = policy;
83 write_unlock(&policy_lock);
85 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
88 EXPORT_SYMBOL(sptlrpc_register_policy);
90 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
92 __u16 number = policy->sp_policy;
94 LASSERT(number < SPTLRPC_POLICY_MAX);
96 write_lock(&policy_lock);
97 if (unlikely(policies[number] == NULL)) {
98 write_unlock(&policy_lock);
99 CERROR("%s: already unregistered\n", policy->sp_name);
103 LASSERT(policies[number] == policy);
104 policies[number] = NULL;
105 write_unlock(&policy_lock);
107 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
110 EXPORT_SYMBOL(sptlrpc_unregister_policy);
113 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
115 static DEFINE_MUTEX(load_mutex);
116 static atomic_t loaded = ATOMIC_INIT(0);
117 struct ptlrpc_sec_policy *policy;
118 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
121 if (number >= SPTLRPC_POLICY_MAX)
125 read_lock(&policy_lock);
126 policy = policies[number];
127 if (policy && !try_module_get(policy->sp_owner))
130 flag = atomic_read(&loaded);
131 read_unlock(&policy_lock);
133 if (policy != NULL || flag != 0 ||
134 number != SPTLRPC_POLICY_GSS)
137 /* try to load gss module, once */
138 mutex_lock(&load_mutex);
139 if (atomic_read(&loaded) == 0) {
140 if (request_module("ptlrpc_gss") == 0)
142 "module ptlrpc_gss loaded on demand\n");
144 CERROR("Unable to load module ptlrpc_gss\n");
146 atomic_set(&loaded, 1);
148 mutex_unlock(&load_mutex);
154 __u32 sptlrpc_name2flavor_base(const char *name)
156 if (!strcmp(name, "null"))
157 return SPTLRPC_FLVR_NULL;
158 if (!strcmp(name, "plain"))
159 return SPTLRPC_FLVR_PLAIN;
160 if (!strcmp(name, "krb5n"))
161 return SPTLRPC_FLVR_KRB5N;
162 if (!strcmp(name, "krb5a"))
163 return SPTLRPC_FLVR_KRB5A;
164 if (!strcmp(name, "krb5i"))
165 return SPTLRPC_FLVR_KRB5I;
166 if (!strcmp(name, "krb5p"))
167 return SPTLRPC_FLVR_KRB5P;
169 return SPTLRPC_FLVR_INVALID;
171 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
173 const char *sptlrpc_flavor2name_base(__u32 flvr)
175 __u32 base = SPTLRPC_FLVR_BASE(flvr);
177 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
179 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
181 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
183 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
185 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
187 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
190 CERROR("invalid wire flavor 0x%x\n", flvr);
193 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
195 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
196 char *buf, int bufsize)
198 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
199 snprintf(buf, bufsize, "hash:%s",
200 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
202 snprintf(buf, bufsize, "%s",
203 sptlrpc_flavor2name_base(sf->sf_rpc));
205 buf[bufsize - 1] = '\0';
208 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
210 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
212 strlcpy(buf, sptlrpc_flavor2name_base(sf->sf_rpc), bufsize);
215 * currently we don't support customized bulk specification for
216 * flavors other than plain
218 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
222 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
223 strlcat(buf, bspec, bufsize);
228 EXPORT_SYMBOL(sptlrpc_flavor2name);
230 static char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
234 if (flags & PTLRPC_SEC_FL_REVERSE)
235 strlcat(buf, "reverse,", bufsize);
236 if (flags & PTLRPC_SEC_FL_ROOTONLY)
237 strlcat(buf, "rootonly,", bufsize);
238 if (flags & PTLRPC_SEC_FL_UDESC)
239 strlcat(buf, "udesc,", bufsize);
240 if (flags & PTLRPC_SEC_FL_BULK)
241 strlcat(buf, "bulk,", bufsize);
243 strlcat(buf, "-,", bufsize);
248 /**************************************************
249 * client context APIs *
250 **************************************************/
253 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
255 struct vfs_cred vcred;
256 int create = 1, remove_dead = 1;
259 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
261 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
262 PTLRPC_SEC_FL_ROOTONLY)) {
265 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
270 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
271 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
274 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
275 create, remove_dead);
278 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
280 atomic_inc(&ctx->cc_refcount);
283 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
285 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
287 struct ptlrpc_sec *sec = ctx->cc_sec;
290 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
292 if (!atomic_dec_and_test(&ctx->cc_refcount))
295 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
297 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
299 static int import_sec_check_expire(struct obd_import *imp)
303 spin_lock(&imp->imp_lock);
304 if (imp->imp_sec_expire &&
305 imp->imp_sec_expire < ktime_get_real_seconds()) {
307 imp->imp_sec_expire = 0;
309 spin_unlock(&imp->imp_lock);
314 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
315 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
318 static int import_sec_validate_get(struct obd_import *imp,
319 struct ptlrpc_sec **sec)
323 if (unlikely(imp->imp_sec_expire)) {
324 rc = import_sec_check_expire(imp);
329 *sec = sptlrpc_import_sec_ref(imp);
331 CERROR("import %p (%s) with no sec\n",
332 imp, ptlrpc_import_state_name(imp->imp_state));
336 if (unlikely((*sec)->ps_dying)) {
337 CERROR("attempt to use dying sec %p\n", sec);
338 sptlrpc_sec_put(*sec);
346 * Given a \a req, find or allocate a appropriate context for it.
347 * \pre req->rq_cli_ctx == NULL.
349 * \retval 0 succeed, and req->rq_cli_ctx is set.
350 * \retval -ev error number, and req->rq_cli_ctx == NULL.
352 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
354 struct obd_import *imp = req->rq_import;
355 struct ptlrpc_sec *sec;
358 LASSERT(!req->rq_cli_ctx);
361 rc = import_sec_validate_get(imp, &sec);
365 req->rq_cli_ctx = get_my_ctx(sec);
367 sptlrpc_sec_put(sec);
369 if (!req->rq_cli_ctx) {
370 CERROR("req %p: fail to get context\n", req);
378 * Drop the context for \a req.
379 * \pre req->rq_cli_ctx != NULL.
380 * \post req->rq_cli_ctx == NULL.
382 * If \a sync == 0, this function should return quickly without sleep;
383 * otherwise it might trigger and wait for the whole process of sending
384 * an context-destroying rpc to server.
386 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
389 LASSERT(req->rq_cli_ctx);
391 /* request might be asked to release earlier while still
392 * in the context waiting list.
394 if (!list_empty(&req->rq_ctx_chain)) {
395 spin_lock(&req->rq_cli_ctx->cc_lock);
396 list_del_init(&req->rq_ctx_chain);
397 spin_unlock(&req->rq_cli_ctx->cc_lock);
400 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
401 req->rq_cli_ctx = NULL;
405 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
406 struct ptlrpc_cli_ctx *oldctx,
407 struct ptlrpc_cli_ctx *newctx)
409 struct sptlrpc_flavor old_flvr;
410 char *reqmsg = NULL; /* to workaround old gcc */
414 LASSERT(req->rq_reqmsg);
415 LASSERT(req->rq_reqlen);
416 LASSERT(req->rq_replen);
418 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
420 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
421 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
422 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
423 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
426 old_flvr = req->rq_flvr;
428 /* save request message */
429 reqmsg_size = req->rq_reqlen;
430 if (reqmsg_size != 0) {
431 reqmsg = libcfs_kvzalloc(reqmsg_size, GFP_NOFS);
434 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
437 /* release old req/rep buf */
438 req->rq_cli_ctx = oldctx;
439 sptlrpc_cli_free_reqbuf(req);
440 sptlrpc_cli_free_repbuf(req);
441 req->rq_cli_ctx = newctx;
443 /* recalculate the flavor */
444 sptlrpc_req_set_flavor(req, 0);
446 /* alloc new request buffer
447 * we don't need to alloc reply buffer here, leave it to the
448 * rest procedure of ptlrpc */
449 if (reqmsg_size != 0) {
450 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
452 LASSERT(req->rq_reqmsg);
453 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
455 CWARN("failed to alloc reqbuf: %d\n", rc);
456 req->rq_flvr = old_flvr;
465 * If current context of \a req is dead somehow, e.g. we just switched flavor
466 * thus marked original contexts dead, we'll find a new context for it. if
467 * no switch is needed, \a req will end up with the same context.
469 * \note a request must have a context, to keep other parts of code happy.
470 * In any case of failure during the switching, we must restore the old one.
472 static int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
474 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
475 struct ptlrpc_cli_ctx *newctx;
480 sptlrpc_cli_ctx_get(oldctx);
481 sptlrpc_req_put_ctx(req, 0);
483 rc = sptlrpc_req_get_ctx(req);
485 LASSERT(!req->rq_cli_ctx);
487 /* restore old ctx */
488 req->rq_cli_ctx = oldctx;
492 newctx = req->rq_cli_ctx;
495 if (unlikely(newctx == oldctx &&
496 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
498 * still get the old dead ctx, usually means system too busy
501 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
502 newctx, newctx->cc_flags);
504 set_current_state(TASK_INTERRUPTIBLE);
505 schedule_timeout(HZ);
508 * it's possible newctx == oldctx if we're switching
509 * subflavor with the same sec.
511 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
513 /* restore old ctx */
514 sptlrpc_req_put_ctx(req, 0);
515 req->rq_cli_ctx = oldctx;
519 LASSERT(req->rq_cli_ctx == newctx);
522 sptlrpc_cli_ctx_put(oldctx, 1);
527 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
529 if (cli_ctx_is_refreshed(ctx))
535 int ctx_refresh_timeout(void *data)
537 struct ptlrpc_request *req = data;
540 /* conn_cnt is needed in expire_one_request */
541 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
543 rc = ptlrpc_expire_one_request(req, 1);
544 /* if we started recovery, we should mark this ctx dead; otherwise
545 * in case of lgssd died nobody would retire this ctx, following
546 * connecting will still find the same ctx thus cause deadlock.
547 * there's an assumption that expire time of the request should be
548 * later than the context refresh expire time.
551 req->rq_cli_ctx->cc_ops->force_die(req->rq_cli_ctx, 0);
556 void ctx_refresh_interrupt(void *data)
558 struct ptlrpc_request *req = data;
560 spin_lock(&req->rq_lock);
562 spin_unlock(&req->rq_lock);
566 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
568 spin_lock(&ctx->cc_lock);
569 if (!list_empty(&req->rq_ctx_chain))
570 list_del_init(&req->rq_ctx_chain);
571 spin_unlock(&ctx->cc_lock);
575 * To refresh the context of \req, if it's not up-to-date.
578 * - = 0: wait until success or fatal error occur
579 * - > 0: timeout value (in seconds)
581 * The status of the context could be subject to be changed by other threads
582 * at any time. We allow this race, but once we return with 0, the caller will
583 * suppose it's uptodated and keep using it until the owning rpc is done.
585 * \retval 0 only if the context is uptodated.
586 * \retval -ev error number.
588 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
590 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
591 struct ptlrpc_sec *sec;
592 struct l_wait_info lwi;
597 if (req->rq_ctx_init || req->rq_ctx_fini)
601 * during the process a request's context might change type even
602 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
606 rc = import_sec_validate_get(req->rq_import, &sec);
610 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
611 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
612 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
613 req_off_ctx_list(req, ctx);
614 sptlrpc_req_replace_dead_ctx(req);
615 ctx = req->rq_cli_ctx;
617 sptlrpc_sec_put(sec);
619 if (cli_ctx_is_eternal(ctx))
622 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
623 LASSERT(ctx->cc_ops->refresh);
624 ctx->cc_ops->refresh(ctx);
626 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
628 LASSERT(ctx->cc_ops->validate);
629 if (ctx->cc_ops->validate(ctx) == 0) {
630 req_off_ctx_list(req, ctx);
634 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
635 spin_lock(&req->rq_lock);
637 spin_unlock(&req->rq_lock);
638 req_off_ctx_list(req, ctx);
643 * There's a subtle issue for resending RPCs, suppose following
645 * 1. the request was sent to server.
646 * 2. recovery was kicked start, after finished the request was
648 * 3. resend the request.
649 * 4. old reply from server received, we accept and verify the reply.
650 * this has to be success, otherwise the error will be aware
652 * 5. new reply from server received, dropped by LNet.
654 * Note the xid of old & new request is the same. We can't simply
655 * change xid for the resent request because the server replies on
656 * it for reply reconstruction.
658 * Commonly the original context should be uptodate because we
659 * have a expiry nice time; server will keep its context because
660 * we at least hold a ref of old context which prevent context
661 * destroying RPC being sent. So server still can accept the request
662 * and finish the RPC. But if that's not the case:
663 * 1. If server side context has been trimmed, a NO_CONTEXT will
664 * be returned, gss_cli_ctx_verify/unseal will switch to new
666 * 2. Current context never be refreshed, then we are fine: we
667 * never really send request with old context before.
669 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
670 unlikely(req->rq_reqmsg) &&
671 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
672 req_off_ctx_list(req, ctx);
676 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
677 req_off_ctx_list(req, ctx);
679 * don't switch ctx if import was deactivated
681 if (req->rq_import->imp_deactive) {
682 spin_lock(&req->rq_lock);
684 spin_unlock(&req->rq_lock);
688 rc = sptlrpc_req_replace_dead_ctx(req);
690 LASSERT(ctx == req->rq_cli_ctx);
691 CERROR("req %p: failed to replace dead ctx %p: %d\n",
693 spin_lock(&req->rq_lock);
695 spin_unlock(&req->rq_lock);
699 ctx = req->rq_cli_ctx;
704 * Now we're sure this context is during upcall, add myself into
707 spin_lock(&ctx->cc_lock);
708 if (list_empty(&req->rq_ctx_chain))
709 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
710 spin_unlock(&ctx->cc_lock);
715 /* Clear any flags that may be present from previous sends */
716 LASSERT(req->rq_receiving_reply == 0);
717 spin_lock(&req->rq_lock);
719 req->rq_timedout = 0;
722 spin_unlock(&req->rq_lock);
724 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
725 ctx_refresh_interrupt, req);
726 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
729 * following cases could lead us here:
730 * - successfully refreshed;
732 * - timedout, and we don't want recover from the failure;
733 * - timedout, and waked up upon recovery finished;
734 * - someone else mark this ctx dead by force;
735 * - someone invalidate the req and call ptlrpc_client_wake_req(),
736 * e.g. ptlrpc_abort_inflight();
738 if (!cli_ctx_is_refreshed(ctx)) {
739 /* timed out or interrupted */
740 req_off_ctx_list(req, ctx);
750 * Initialize flavor settings for \a req, according to \a opcode.
752 * \note this could be called in two situations:
753 * - new request from ptlrpc_pre_req(), with proper @opcode
754 * - old request which changed ctx in the middle, with @opcode == 0
756 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
758 struct ptlrpc_sec *sec;
760 LASSERT(req->rq_import);
761 LASSERT(req->rq_cli_ctx);
762 LASSERT(req->rq_cli_ctx->cc_sec);
763 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
765 /* special security flags according to opcode */
769 case MGS_CONFIG_READ:
771 req->rq_bulk_read = 1;
775 req->rq_bulk_write = 1;
778 req->rq_ctx_init = 1;
781 req->rq_ctx_fini = 1;
784 /* init/fini rpc won't be resend, so can't be here */
785 LASSERT(req->rq_ctx_init == 0);
786 LASSERT(req->rq_ctx_fini == 0);
788 /* cleanup flags, which should be recalculated */
789 req->rq_pack_udesc = 0;
790 req->rq_pack_bulk = 0;
794 sec = req->rq_cli_ctx->cc_sec;
796 spin_lock(&sec->ps_lock);
797 req->rq_flvr = sec->ps_flvr;
798 spin_unlock(&sec->ps_lock);
800 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
802 if (unlikely(req->rq_ctx_init))
803 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
804 else if (unlikely(req->rq_ctx_fini))
805 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
807 /* user descriptor flag, null security can't do it anyway */
808 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
809 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
810 req->rq_pack_udesc = 1;
812 /* bulk security flag */
813 if ((req->rq_bulk_read || req->rq_bulk_write) &&
814 sptlrpc_flavor_has_bulk(&req->rq_flvr))
815 req->rq_pack_bulk = 1;
818 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
820 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
823 LASSERT(req->rq_clrbuf);
824 if (req->rq_pool || !req->rq_reqbuf)
827 kfree(req->rq_reqbuf);
828 req->rq_reqbuf = NULL;
829 req->rq_reqbuf_len = 0;
833 * Given an import \a imp, check whether current user has a valid context
834 * or not. We may create a new context and try to refresh it, and try
835 * repeatedly try in case of non-fatal errors. Return 0 means success.
837 int sptlrpc_import_check_ctx(struct obd_import *imp)
839 struct ptlrpc_sec *sec;
840 struct ptlrpc_cli_ctx *ctx;
841 struct ptlrpc_request *req = NULL;
846 sec = sptlrpc_import_sec_ref(imp);
847 ctx = get_my_ctx(sec);
848 sptlrpc_sec_put(sec);
853 if (cli_ctx_is_eternal(ctx) ||
854 ctx->cc_ops->validate(ctx) == 0) {
855 sptlrpc_cli_ctx_put(ctx, 1);
859 if (cli_ctx_is_error(ctx)) {
860 sptlrpc_cli_ctx_put(ctx, 1);
864 req = ptlrpc_request_cache_alloc(GFP_NOFS);
868 spin_lock_init(&req->rq_lock);
869 atomic_set(&req->rq_refcount, 10000);
870 INIT_LIST_HEAD(&req->rq_ctx_chain);
871 init_waitqueue_head(&req->rq_reply_waitq);
872 init_waitqueue_head(&req->rq_set_waitq);
873 req->rq_import = imp;
874 req->rq_flvr = sec->ps_flvr;
875 req->rq_cli_ctx = ctx;
877 rc = sptlrpc_req_refresh_ctx(req, 0);
878 LASSERT(list_empty(&req->rq_ctx_chain));
879 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
880 ptlrpc_request_cache_free(req);
886 * Used by ptlrpc client, to perform the pre-defined security transformation
887 * upon the request message of \a req. After this function called,
888 * req->rq_reqmsg is still accessible as clear text.
890 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
892 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
896 LASSERT(ctx->cc_sec);
897 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
899 /* we wrap bulk request here because now we can be sure
900 * the context is uptodate.
903 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
908 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
909 case SPTLRPC_SVC_NULL:
910 case SPTLRPC_SVC_AUTH:
911 case SPTLRPC_SVC_INTG:
912 LASSERT(ctx->cc_ops->sign);
913 rc = ctx->cc_ops->sign(ctx, req);
915 case SPTLRPC_SVC_PRIV:
916 LASSERT(ctx->cc_ops->seal);
917 rc = ctx->cc_ops->seal(ctx, req);
924 LASSERT(req->rq_reqdata_len);
925 LASSERT(req->rq_reqdata_len % 8 == 0);
926 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
932 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
934 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
938 LASSERT(ctx->cc_sec);
939 LASSERT(req->rq_repbuf);
940 LASSERT(req->rq_repdata);
941 LASSERT(req->rq_repmsg == NULL);
943 req->rq_rep_swab_mask = 0;
945 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
948 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
952 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
956 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
957 CERROR("replied data length %d too small\n",
958 req->rq_repdata_len);
962 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
963 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
964 CERROR("reply policy %u doesn't match request policy %u\n",
965 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
966 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
970 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
971 case SPTLRPC_SVC_NULL:
972 case SPTLRPC_SVC_AUTH:
973 case SPTLRPC_SVC_INTG:
974 LASSERT(ctx->cc_ops->verify);
975 rc = ctx->cc_ops->verify(ctx, req);
977 case SPTLRPC_SVC_PRIV:
978 LASSERT(ctx->cc_ops->unseal);
979 rc = ctx->cc_ops->unseal(ctx, req);
984 LASSERT(rc || req->rq_repmsg || req->rq_resend);
986 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
988 req->rq_rep_swab_mask = 0;
993 * Used by ptlrpc client, to perform security transformation upon the reply
994 * message of \a req. After return successfully, req->rq_repmsg points to
995 * the reply message in clear text.
997 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1000 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1002 LASSERT(req->rq_repbuf);
1003 LASSERT(req->rq_repdata == NULL);
1004 LASSERT(req->rq_repmsg == NULL);
1005 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1007 if (req->rq_reply_off == 0 &&
1008 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1009 CERROR("real reply with offset 0\n");
1013 if (req->rq_reply_off % 8 != 0) {
1014 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1018 req->rq_repdata = (struct lustre_msg *)
1019 (req->rq_repbuf + req->rq_reply_off);
1020 req->rq_repdata_len = req->rq_nob_received;
1022 return do_cli_unwrap_reply(req);
1026 * Used by ptlrpc client, to perform security transformation upon the early
1027 * reply message of \a req. We expect the rq_reply_off is 0, and
1028 * rq_nob_received is the early reply size.
1030 * Because the receive buffer might be still posted, the reply data might be
1031 * changed at any time, no matter we're holding rq_lock or not. For this reason
1032 * we allocate a separate ptlrpc_request and reply buffer for early reply
1035 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1036 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1037 * \a *req_ret to release it.
1038 * \retval -ev error number, and \a req_ret will not be set.
1040 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1041 struct ptlrpc_request **req_ret)
1043 struct ptlrpc_request *early_req;
1045 int early_bufsz, early_size;
1048 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1049 if (early_req == NULL)
1052 early_size = req->rq_nob_received;
1053 early_bufsz = size_roundup_power2(early_size);
1054 early_buf = libcfs_kvzalloc(early_bufsz, GFP_NOFS);
1055 if (early_buf == NULL) {
1060 /* sanity checkings and copy data out, do it inside spinlock */
1061 spin_lock(&req->rq_lock);
1063 if (req->rq_replied) {
1064 spin_unlock(&req->rq_lock);
1069 LASSERT(req->rq_repbuf);
1070 LASSERT(req->rq_repdata == NULL);
1071 LASSERT(req->rq_repmsg == NULL);
1073 if (req->rq_reply_off != 0) {
1074 CERROR("early reply with offset %u\n", req->rq_reply_off);
1075 spin_unlock(&req->rq_lock);
1080 if (req->rq_nob_received != early_size) {
1081 /* even another early arrived the size should be the same */
1082 CERROR("data size has changed from %u to %u\n",
1083 early_size, req->rq_nob_received);
1084 spin_unlock(&req->rq_lock);
1089 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1090 CERROR("early reply length %d too small\n",
1091 req->rq_nob_received);
1092 spin_unlock(&req->rq_lock);
1097 memcpy(early_buf, req->rq_repbuf, early_size);
1098 spin_unlock(&req->rq_lock);
1100 spin_lock_init(&early_req->rq_lock);
1101 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1102 early_req->rq_flvr = req->rq_flvr;
1103 early_req->rq_repbuf = early_buf;
1104 early_req->rq_repbuf_len = early_bufsz;
1105 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1106 early_req->rq_repdata_len = early_size;
1107 early_req->rq_early = 1;
1108 early_req->rq_reqmsg = req->rq_reqmsg;
1110 rc = do_cli_unwrap_reply(early_req);
1112 DEBUG_REQ(D_ADAPTTO, early_req,
1113 "error %d unwrap early reply", rc);
1117 LASSERT(early_req->rq_repmsg);
1118 *req_ret = early_req;
1122 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1126 ptlrpc_request_cache_free(early_req);
1131 * Used by ptlrpc client, to release a processed early reply \a early_req.
1133 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1135 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1137 LASSERT(early_req->rq_repbuf);
1138 LASSERT(early_req->rq_repdata);
1139 LASSERT(early_req->rq_repmsg);
1141 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1142 kvfree(early_req->rq_repbuf);
1143 ptlrpc_request_cache_free(early_req);
1146 /**************************************************
1148 **************************************************/
1151 * "fixed" sec (e.g. null) use sec_id < 0
1153 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1155 int sptlrpc_get_next_secid(void)
1157 return atomic_inc_return(&sptlrpc_sec_id);
1159 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1161 /**************************************************
1162 * client side high-level security APIs *
1163 **************************************************/
1165 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1166 int grace, int force)
1168 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1170 LASSERT(policy->sp_cops);
1171 LASSERT(policy->sp_cops->flush_ctx_cache);
1173 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1176 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1178 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1180 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1181 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1182 LASSERT(policy->sp_cops->destroy_sec);
1184 CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1186 policy->sp_cops->destroy_sec(sec);
1187 sptlrpc_policy_put(policy);
1190 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1192 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1194 if (sec->ps_policy->sp_cops->kill_sec) {
1195 sec->ps_policy->sp_cops->kill_sec(sec);
1197 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1201 static struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1204 atomic_inc(&sec->ps_refcount);
1209 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1212 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1214 if (atomic_dec_and_test(&sec->ps_refcount)) {
1215 sptlrpc_gc_del_sec(sec);
1216 sec_cop_destroy_sec(sec);
1220 EXPORT_SYMBOL(sptlrpc_sec_put);
1223 * policy module is responsible for taking reference of import
1226 struct ptlrpc_sec *sptlrpc_sec_create(struct obd_import *imp,
1227 struct ptlrpc_svc_ctx *svc_ctx,
1228 struct sptlrpc_flavor *sf,
1229 enum lustre_sec_part sp)
1231 struct ptlrpc_sec_policy *policy;
1232 struct ptlrpc_sec *sec;
1236 LASSERT(imp->imp_dlm_fake == 1);
1238 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1239 imp->imp_obd->obd_type->typ_name,
1240 imp->imp_obd->obd_name,
1241 sptlrpc_flavor2name(sf, str, sizeof(str)));
1243 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1244 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1246 LASSERT(imp->imp_dlm_fake == 0);
1248 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1249 imp->imp_obd->obd_type->typ_name,
1250 imp->imp_obd->obd_name,
1251 sptlrpc_flavor2name(sf, str, sizeof(str)));
1253 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1255 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1260 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1262 atomic_inc(&sec->ps_refcount);
1266 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1267 sptlrpc_gc_add_sec(sec);
1269 sptlrpc_policy_put(policy);
1275 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1277 struct ptlrpc_sec *sec;
1279 spin_lock(&imp->imp_lock);
1280 sec = sptlrpc_sec_get(imp->imp_sec);
1281 spin_unlock(&imp->imp_lock);
1285 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1287 static void sptlrpc_import_sec_install(struct obd_import *imp,
1288 struct ptlrpc_sec *sec)
1290 struct ptlrpc_sec *old_sec;
1292 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1294 spin_lock(&imp->imp_lock);
1295 old_sec = imp->imp_sec;
1297 spin_unlock(&imp->imp_lock);
1300 sptlrpc_sec_kill(old_sec);
1302 /* balance the ref taken by this import */
1303 sptlrpc_sec_put(old_sec);
1308 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1310 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1314 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1319 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1320 struct ptlrpc_sec *sec,
1321 struct sptlrpc_flavor *sf)
1323 char str1[32], str2[32];
1325 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1326 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1327 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1328 str1, sizeof(str1)),
1329 sptlrpc_secflags2str(sf->sf_flags,
1330 str2, sizeof(str2)));
1332 spin_lock(&sec->ps_lock);
1333 flavor_copy(&sec->ps_flvr, sf);
1334 spin_unlock(&sec->ps_lock);
1338 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1339 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1341 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1342 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1344 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1345 struct ptlrpc_svc_ctx *svc_ctx,
1346 struct sptlrpc_flavor *flvr)
1348 struct ptlrpc_connection *conn;
1349 struct sptlrpc_flavor sf;
1350 struct ptlrpc_sec *sec, *newsec;
1351 enum lustre_sec_part sp;
1360 conn = imp->imp_connection;
1362 if (svc_ctx == NULL) {
1363 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1365 * normal import, determine flavor from rule set, except
1366 * for mgc the flavor is predetermined.
1368 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1369 sf = cliobd->cl_flvr_mgc;
1371 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1373 &cliobd->cl_target_uuid,
1376 sp = imp->imp_obd->u.cli.cl_sp_me;
1378 /* reverse import, determine flavor from incoming request */
1381 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1382 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1383 PTLRPC_SEC_FL_ROOTONLY;
1385 sp = sptlrpc_target_sec_part(imp->imp_obd);
1388 sec = sptlrpc_import_sec_ref(imp);
1392 if (flavor_equal(&sf, &sec->ps_flvr))
1395 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1396 imp->imp_obd->obd_name,
1397 obd_uuid2str(&conn->c_remote_uuid),
1398 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1399 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1401 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1402 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1403 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1404 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1405 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1408 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1409 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1410 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1411 imp->imp_obd->obd_name,
1412 obd_uuid2str(&conn->c_remote_uuid),
1413 LNET_NIDNET(conn->c_self),
1414 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1417 mutex_lock(&imp->imp_sec_mutex);
1419 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1421 sptlrpc_import_sec_install(imp, newsec);
1423 CERROR("import %s->%s: failed to create new sec\n",
1424 imp->imp_obd->obd_name,
1425 obd_uuid2str(&conn->c_remote_uuid));
1429 mutex_unlock(&imp->imp_sec_mutex);
1431 sptlrpc_sec_put(sec);
1435 void sptlrpc_import_sec_put(struct obd_import *imp)
1438 sptlrpc_sec_kill(imp->imp_sec);
1440 sptlrpc_sec_put(imp->imp_sec);
1441 imp->imp_sec = NULL;
1445 static void import_flush_ctx_common(struct obd_import *imp,
1446 uid_t uid, int grace, int force)
1448 struct ptlrpc_sec *sec;
1453 sec = sptlrpc_import_sec_ref(imp);
1457 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1458 sptlrpc_sec_put(sec);
1461 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1463 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1466 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1468 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1470 import_flush_ctx_common(imp, -1, 1, 1);
1472 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1475 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1476 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1478 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1480 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1481 struct ptlrpc_sec_policy *policy;
1485 LASSERT(ctx->cc_sec);
1486 LASSERT(ctx->cc_sec->ps_policy);
1487 LASSERT(req->rq_reqmsg == NULL);
1488 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1490 policy = ctx->cc_sec->ps_policy;
1491 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1493 LASSERT(req->rq_reqmsg);
1494 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1496 /* zeroing preallocated buffer */
1498 memset(req->rq_reqmsg, 0, msgsize);
1505 * Used by ptlrpc client to free request buffer of \a req. After this
1506 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1508 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1510 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1511 struct ptlrpc_sec_policy *policy;
1514 LASSERT(ctx->cc_sec);
1515 LASSERT(ctx->cc_sec->ps_policy);
1516 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1518 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1521 policy = ctx->cc_sec->ps_policy;
1522 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1523 req->rq_reqmsg = NULL;
1527 * NOTE caller must guarantee the buffer size is enough for the enlargement
1529 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1530 int segment, int newsize)
1533 int oldsize, oldmsg_size, movesize;
1535 LASSERT(segment < msg->lm_bufcount);
1536 LASSERT(msg->lm_buflens[segment] <= newsize);
1538 if (msg->lm_buflens[segment] == newsize)
1541 /* nothing to do if we are enlarging the last segment */
1542 if (segment == msg->lm_bufcount - 1) {
1543 msg->lm_buflens[segment] = newsize;
1547 oldsize = msg->lm_buflens[segment];
1549 src = lustre_msg_buf(msg, segment + 1, 0);
1550 msg->lm_buflens[segment] = newsize;
1551 dst = lustre_msg_buf(msg, segment + 1, 0);
1552 msg->lm_buflens[segment] = oldsize;
1554 /* move from segment + 1 to end segment */
1555 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1556 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1557 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1558 LASSERT(movesize >= 0);
1561 memmove(dst, src, movesize);
1563 /* note we don't clear the ares where old data live, not secret */
1565 /* finally set new segment size */
1566 msg->lm_buflens[segment] = newsize;
1568 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1571 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1572 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1573 * preserved after the enlargement. this must be called after original request
1574 * buffer being allocated.
1576 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1577 * so caller should refresh its local pointers if needed.
1579 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1580 int segment, int newsize)
1582 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1583 struct ptlrpc_sec_cops *cops;
1584 struct lustre_msg *msg = req->rq_reqmsg;
1588 LASSERT(msg->lm_bufcount > segment);
1589 LASSERT(msg->lm_buflens[segment] <= newsize);
1591 if (msg->lm_buflens[segment] == newsize)
1594 cops = ctx->cc_sec->ps_policy->sp_cops;
1595 LASSERT(cops->enlarge_reqbuf);
1596 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1598 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1601 * Used by ptlrpc client to allocate reply buffer of \a req.
1603 * \note After this, req->rq_repmsg is still not accessible.
1605 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1607 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1608 struct ptlrpc_sec_policy *policy;
1611 LASSERT(ctx->cc_sec);
1612 LASSERT(ctx->cc_sec->ps_policy);
1617 policy = ctx->cc_sec->ps_policy;
1618 return policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize);
1622 * Used by ptlrpc client to free reply buffer of \a req. After this
1623 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1625 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1627 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1628 struct ptlrpc_sec_policy *policy;
1631 LASSERT(ctx->cc_sec);
1632 LASSERT(ctx->cc_sec->ps_policy);
1633 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1635 if (req->rq_repbuf == NULL)
1637 LASSERT(req->rq_repbuf_len);
1639 policy = ctx->cc_sec->ps_policy;
1640 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1641 req->rq_repmsg = NULL;
1644 static int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1645 struct ptlrpc_svc_ctx *ctx)
1647 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1649 if (!policy->sp_sops->install_rctx)
1651 return policy->sp_sops->install_rctx(imp, ctx);
1654 /****************************************
1655 * server side security *
1656 ****************************************/
1658 static int flavor_allowed(struct sptlrpc_flavor *exp,
1659 struct ptlrpc_request *req)
1661 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1663 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1666 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1667 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1668 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1669 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1675 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1678 * Given an export \a exp, check whether the flavor of incoming \a req
1679 * is allowed by the export \a exp. Main logic is about taking care of
1680 * changing configurations. Return 0 means success.
1682 int sptlrpc_target_export_check(struct obd_export *exp,
1683 struct ptlrpc_request *req)
1685 struct sptlrpc_flavor flavor;
1690 /* client side export has no imp_reverse, skip
1691 * FIXME maybe we should check flavor this as well??? */
1692 if (exp->exp_imp_reverse == NULL)
1695 /* don't care about ctx fini rpc */
1696 if (req->rq_ctx_fini)
1699 spin_lock(&exp->exp_lock);
1701 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1702 * the first req with the new flavor, then treat it as current flavor,
1703 * adapt reverse sec according to it.
1704 * note the first rpc with new flavor might not be with root ctx, in
1705 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1706 if (unlikely(exp->exp_flvr_changed) &&
1707 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1708 /* make the new flavor as "current", and old ones as
1709 * about-to-expire */
1710 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1711 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1712 flavor = exp->exp_flvr_old[1];
1713 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1714 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1715 exp->exp_flvr_old[0] = exp->exp_flvr;
1716 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
1717 EXP_FLVR_UPDATE_EXPIRE;
1718 exp->exp_flvr = flavor;
1720 /* flavor change finished */
1721 exp->exp_flvr_changed = 0;
1722 LASSERT(exp->exp_flvr_adapt == 1);
1724 /* if it's gss, we only interested in root ctx init */
1725 if (req->rq_auth_gss &&
1726 !(req->rq_ctx_init &&
1727 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1728 req->rq_auth_usr_ost))) {
1729 spin_unlock(&exp->exp_lock);
1730 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1731 req->rq_auth_gss, req->rq_ctx_init,
1732 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1733 req->rq_auth_usr_ost);
1737 exp->exp_flvr_adapt = 0;
1738 spin_unlock(&exp->exp_lock);
1740 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1741 req->rq_svc_ctx, &flavor);
1744 /* if it equals to the current flavor, we accept it, but need to
1745 * dealing with reverse sec/ctx */
1746 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1747 /* most cases should return here, we only interested in
1748 * gss root ctx init */
1749 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1750 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1751 !req->rq_auth_usr_ost)) {
1752 spin_unlock(&exp->exp_lock);
1756 /* if flavor just changed, we should not proceed, just leave
1757 * it and current flavor will be discovered and replaced
1758 * shortly, and let _this_ rpc pass through */
1759 if (exp->exp_flvr_changed) {
1760 LASSERT(exp->exp_flvr_adapt);
1761 spin_unlock(&exp->exp_lock);
1765 if (exp->exp_flvr_adapt) {
1766 exp->exp_flvr_adapt = 0;
1767 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1768 exp, exp->exp_flvr.sf_rpc,
1769 exp->exp_flvr_old[0].sf_rpc,
1770 exp->exp_flvr_old[1].sf_rpc);
1771 flavor = exp->exp_flvr;
1772 spin_unlock(&exp->exp_lock);
1774 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1778 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
1779 exp, exp->exp_flvr.sf_rpc,
1780 exp->exp_flvr_old[0].sf_rpc,
1781 exp->exp_flvr_old[1].sf_rpc);
1782 spin_unlock(&exp->exp_lock);
1784 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1789 if (exp->exp_flvr_expire[0]) {
1790 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
1791 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1792 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the middle one (%lld)\n", exp,
1793 exp->exp_flvr.sf_rpc,
1794 exp->exp_flvr_old[0].sf_rpc,
1795 exp->exp_flvr_old[1].sf_rpc,
1796 (s64)(exp->exp_flvr_expire[0] -
1797 ktime_get_real_seconds()));
1798 spin_unlock(&exp->exp_lock);
1802 CDEBUG(D_SEC, "mark middle expired\n");
1803 exp->exp_flvr_expire[0] = 0;
1805 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1806 exp->exp_flvr.sf_rpc,
1807 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1808 req->rq_flvr.sf_rpc);
1811 /* now it doesn't match the current flavor, the only chance we can
1812 * accept it is match the old flavors which is not expired. */
1813 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1814 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
1815 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1816 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
1818 exp->exp_flvr.sf_rpc,
1819 exp->exp_flvr_old[0].sf_rpc,
1820 exp->exp_flvr_old[1].sf_rpc,
1821 (s64)(exp->exp_flvr_expire[1] -
1822 ktime_get_real_seconds()));
1823 spin_unlock(&exp->exp_lock);
1827 CDEBUG(D_SEC, "mark oldest expired\n");
1828 exp->exp_flvr_expire[1] = 0;
1830 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1831 exp, exp->exp_flvr.sf_rpc,
1832 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1833 req->rq_flvr.sf_rpc);
1835 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1836 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1837 exp->exp_flvr_old[1].sf_rpc);
1840 spin_unlock(&exp->exp_lock);
1842 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
1843 exp, exp->exp_obd->obd_name,
1844 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1845 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1846 req->rq_flvr.sf_rpc,
1847 exp->exp_flvr.sf_rpc,
1848 exp->exp_flvr_old[0].sf_rpc,
1849 exp->exp_flvr_expire[0] ?
1850 (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
1851 exp->exp_flvr_old[1].sf_rpc,
1852 exp->exp_flvr_expire[1] ?
1853 (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
1856 EXPORT_SYMBOL(sptlrpc_target_export_check);
1858 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1860 /* peer's claim is unreliable unless gss is being used */
1861 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1864 switch (req->rq_sp_from) {
1866 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1867 DEBUG_REQ(D_ERROR, req, "faked source CLI");
1868 svc_rc = SECSVC_DROP;
1872 if (!req->rq_auth_usr_mdt) {
1873 DEBUG_REQ(D_ERROR, req, "faked source MDT");
1874 svc_rc = SECSVC_DROP;
1878 if (!req->rq_auth_usr_ost) {
1879 DEBUG_REQ(D_ERROR, req, "faked source OST");
1880 svc_rc = SECSVC_DROP;
1885 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1886 !req->rq_auth_usr_ost) {
1887 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
1888 svc_rc = SECSVC_DROP;
1893 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1894 svc_rc = SECSVC_DROP;
1901 * Used by ptlrpc server, to perform transformation upon request message of
1902 * incoming \a req. This must be the first thing to do with a incoming
1903 * request in ptlrpc layer.
1905 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
1906 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
1907 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
1908 * reply message has been prepared.
1909 * \retval SECSVC_DROP failed, this request should be dropped.
1911 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1913 struct ptlrpc_sec_policy *policy;
1914 struct lustre_msg *msg = req->rq_reqbuf;
1918 LASSERT(req->rq_reqmsg == NULL);
1919 LASSERT(req->rq_repmsg == NULL);
1920 LASSERT(req->rq_svc_ctx == NULL);
1922 req->rq_req_swab_mask = 0;
1924 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
1927 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1931 CERROR("error unpacking request from %s x%llu\n",
1932 libcfs_id2str(req->rq_peer), req->rq_xid);
1936 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
1937 req->rq_sp_from = LUSTRE_SP_ANY;
1938 req->rq_auth_uid = -1;
1939 req->rq_auth_mapped_uid = -1;
1941 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
1943 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1947 LASSERT(policy->sp_sops->accept);
1948 rc = policy->sp_sops->accept(req);
1949 sptlrpc_policy_put(policy);
1950 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1951 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1954 * if it's not null flavor (which means embedded packing msg),
1955 * reset the swab mask for the coming inner msg unpacking.
1957 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
1958 req->rq_req_swab_mask = 0;
1960 /* sanity check for the request source */
1961 rc = sptlrpc_svc_check_from(req, rc);
1966 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
1967 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
1968 * a buffer of \a msglen size.
1970 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
1972 struct ptlrpc_sec_policy *policy;
1973 struct ptlrpc_reply_state *rs;
1976 LASSERT(req->rq_svc_ctx);
1977 LASSERT(req->rq_svc_ctx->sc_policy);
1979 policy = req->rq_svc_ctx->sc_policy;
1980 LASSERT(policy->sp_sops->alloc_rs);
1982 rc = policy->sp_sops->alloc_rs(req, msglen);
1983 if (unlikely(rc == -ENOMEM)) {
1984 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1986 if (svcpt->scp_service->srv_max_reply_size <
1987 msglen + sizeof(struct ptlrpc_reply_state)) {
1988 /* Just return failure if the size is too big */
1989 CERROR("size of message is too big (%zd), %d allowed",
1990 msglen + sizeof(struct ptlrpc_reply_state),
1991 svcpt->scp_service->srv_max_reply_size);
1995 /* failed alloc, try emergency pool */
1996 rs = lustre_get_emerg_rs(svcpt);
2000 req->rq_reply_state = rs;
2001 rc = policy->sp_sops->alloc_rs(req, msglen);
2003 lustre_put_emerg_rs(rs);
2004 req->rq_reply_state = NULL;
2009 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2015 * Used by ptlrpc server, to perform transformation upon reply message.
2017 * \post req->rq_reply_off is set to appropriate server-controlled reply offset.
2018 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2020 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2022 struct ptlrpc_sec_policy *policy;
2025 LASSERT(req->rq_svc_ctx);
2026 LASSERT(req->rq_svc_ctx->sc_policy);
2028 policy = req->rq_svc_ctx->sc_policy;
2029 LASSERT(policy->sp_sops->authorize);
2031 rc = policy->sp_sops->authorize(req);
2032 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2038 * Used by ptlrpc server, to free reply_state.
2040 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2042 struct ptlrpc_sec_policy *policy;
2043 unsigned int prealloc;
2045 LASSERT(rs->rs_svc_ctx);
2046 LASSERT(rs->rs_svc_ctx->sc_policy);
2048 policy = rs->rs_svc_ctx->sc_policy;
2049 LASSERT(policy->sp_sops->free_rs);
2051 prealloc = rs->rs_prealloc;
2052 policy->sp_sops->free_rs(rs);
2055 lustre_put_emerg_rs(rs);
2058 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2060 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2063 atomic_inc(&ctx->sc_refcount);
2066 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2068 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2073 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2074 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2075 if (ctx->sc_policy->sp_sops->free_ctx)
2076 ctx->sc_policy->sp_sops->free_ctx(ctx);
2078 req->rq_svc_ctx = NULL;
2081 /****************************************
2083 ****************************************/
2086 * Perform transformation upon bulk data pointed by \a desc. This is called
2087 * before transforming the request message.
2089 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2090 struct ptlrpc_bulk_desc *desc)
2092 struct ptlrpc_cli_ctx *ctx;
2094 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2096 if (!req->rq_pack_bulk)
2099 ctx = req->rq_cli_ctx;
2100 if (ctx->cc_ops->wrap_bulk)
2101 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2104 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2107 * This is called after unwrap the reply message.
2108 * return nob of actual plain text size received, or error code.
2110 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2111 struct ptlrpc_bulk_desc *desc,
2114 struct ptlrpc_cli_ctx *ctx;
2117 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2119 if (!req->rq_pack_bulk)
2120 return desc->bd_nob_transferred;
2122 ctx = req->rq_cli_ctx;
2123 if (ctx->cc_ops->unwrap_bulk) {
2124 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2128 return desc->bd_nob_transferred;
2130 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2133 * This is called after unwrap the reply message.
2134 * return 0 for success or error code.
2136 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2137 struct ptlrpc_bulk_desc *desc)
2139 struct ptlrpc_cli_ctx *ctx;
2142 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2144 if (!req->rq_pack_bulk)
2147 ctx = req->rq_cli_ctx;
2148 if (ctx->cc_ops->unwrap_bulk) {
2149 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2155 * if everything is going right, nob should equals to nob_transferred.
2156 * in case of privacy mode, nob_transferred needs to be adjusted.
2158 if (desc->bd_nob != desc->bd_nob_transferred) {
2159 CERROR("nob %d doesn't match transferred nob %d",
2160 desc->bd_nob, desc->bd_nob_transferred);
2166 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2168 /****************************************
2169 * user descriptor helpers *
2170 ****************************************/
2172 int sptlrpc_current_user_desc_size(void)
2176 ngroups = current_ngroups;
2178 if (ngroups > LUSTRE_MAX_GROUPS)
2179 ngroups = LUSTRE_MAX_GROUPS;
2180 return sptlrpc_user_desc_size(ngroups);
2182 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2184 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2186 struct ptlrpc_user_desc *pud;
2188 pud = lustre_msg_buf(msg, offset, 0);
2190 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2191 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2192 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2193 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2194 pud->pud_cap = cfs_curproc_cap_pack();
2195 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2198 if (pud->pud_ngroups > current_ngroups)
2199 pud->pud_ngroups = current_ngroups;
2200 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2201 pud->pud_ngroups * sizeof(__u32));
2202 task_unlock(current);
2206 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2208 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2210 struct ptlrpc_user_desc *pud;
2213 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2218 __swab32s(&pud->pud_uid);
2219 __swab32s(&pud->pud_gid);
2220 __swab32s(&pud->pud_fsuid);
2221 __swab32s(&pud->pud_fsgid);
2222 __swab32s(&pud->pud_cap);
2223 __swab32s(&pud->pud_ngroups);
2226 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2227 CERROR("%u groups is too large\n", pud->pud_ngroups);
2231 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2232 msg->lm_buflens[offset]) {
2233 CERROR("%u groups are claimed but bufsize only %u\n",
2234 pud->pud_ngroups, msg->lm_buflens[offset]);
2239 for (i = 0; i < pud->pud_ngroups; i++)
2240 __swab32s(&pud->pud_groups[i]);
2245 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2247 /****************************************
2249 ****************************************/
2251 const char *sec2target_str(struct ptlrpc_sec *sec)
2253 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2255 if (sec_is_reverse(sec))
2257 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2259 EXPORT_SYMBOL(sec2target_str);
2262 * return true if the bulk data is protected
2264 bool sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2266 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2267 case SPTLRPC_BULK_SVC_INTG:
2268 case SPTLRPC_BULK_SVC_PRIV:
2274 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2276 /****************************************
2277 * crypto API helper/alloc blkciper *
2278 ****************************************/
2280 /****************************************
2281 * initialize/finalize *
2282 ****************************************/
2284 int sptlrpc_init(void)
2288 rwlock_init(&policy_lock);
2290 rc = sptlrpc_gc_init();
2294 rc = sptlrpc_conf_init();
2298 rc = sptlrpc_enc_pool_init();
2302 rc = sptlrpc_null_init();
2306 rc = sptlrpc_plain_init();
2310 rc = sptlrpc_lproc_init();
2317 sptlrpc_plain_fini();
2319 sptlrpc_null_fini();
2321 sptlrpc_enc_pool_fini();
2323 sptlrpc_conf_fini();
2330 void sptlrpc_fini(void)
2332 sptlrpc_lproc_fini();
2333 sptlrpc_plain_fini();
2334 sptlrpc_null_fini();
2335 sptlrpc_enc_pool_fini();
2336 sptlrpc_conf_fini();