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 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);
247 EXPORT_SYMBOL(sptlrpc_secflags2str);
249 /**************************************************
250 * client context APIs *
251 **************************************************/
254 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
256 struct vfs_cred vcred;
257 int create = 1, remove_dead = 1;
260 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
262 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
263 PTLRPC_SEC_FL_ROOTONLY)) {
266 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
271 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
272 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
275 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
276 create, remove_dead);
279 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
281 atomic_inc(&ctx->cc_refcount);
284 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
286 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
288 struct ptlrpc_sec *sec = ctx->cc_sec;
291 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
293 if (!atomic_dec_and_test(&ctx->cc_refcount))
296 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
298 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
301 * Expire the client context immediately.
303 * \pre Caller must hold at least 1 reference on the \a ctx.
305 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
307 LASSERT(ctx->cc_ops->force_die);
308 ctx->cc_ops->force_die(ctx, 0);
310 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
313 * To wake up the threads who are waiting for this client context. Called
314 * after some status change happened on \a ctx.
316 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
318 struct ptlrpc_request *req, *next;
320 spin_lock(&ctx->cc_lock);
321 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
323 list_del_init(&req->rq_ctx_chain);
324 ptlrpc_client_wake_req(req);
326 spin_unlock(&ctx->cc_lock);
328 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
330 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
332 LASSERT(ctx->cc_ops);
334 if (ctx->cc_ops->display == NULL)
337 return ctx->cc_ops->display(ctx, buf, bufsize);
340 static int import_sec_check_expire(struct obd_import *imp)
344 spin_lock(&imp->imp_lock);
345 if (imp->imp_sec_expire &&
346 imp->imp_sec_expire < get_seconds()) {
348 imp->imp_sec_expire = 0;
350 spin_unlock(&imp->imp_lock);
355 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
356 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
359 static int import_sec_validate_get(struct obd_import *imp,
360 struct ptlrpc_sec **sec)
364 if (unlikely(imp->imp_sec_expire)) {
365 rc = import_sec_check_expire(imp);
370 *sec = sptlrpc_import_sec_ref(imp);
372 CERROR("import %p (%s) with no sec\n",
373 imp, ptlrpc_import_state_name(imp->imp_state));
377 if (unlikely((*sec)->ps_dying)) {
378 CERROR("attempt to use dying sec %p\n", sec);
379 sptlrpc_sec_put(*sec);
387 * Given a \a req, find or allocate a appropriate context for it.
388 * \pre req->rq_cli_ctx == NULL.
390 * \retval 0 succeed, and req->rq_cli_ctx is set.
391 * \retval -ev error number, and req->rq_cli_ctx == NULL.
393 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
395 struct obd_import *imp = req->rq_import;
396 struct ptlrpc_sec *sec;
399 LASSERT(!req->rq_cli_ctx);
402 rc = import_sec_validate_get(imp, &sec);
406 req->rq_cli_ctx = get_my_ctx(sec);
408 sptlrpc_sec_put(sec);
410 if (!req->rq_cli_ctx) {
411 CERROR("req %p: fail to get context\n", req);
419 * Drop the context for \a req.
420 * \pre req->rq_cli_ctx != NULL.
421 * \post req->rq_cli_ctx == NULL.
423 * If \a sync == 0, this function should return quickly without sleep;
424 * otherwise it might trigger and wait for the whole process of sending
425 * an context-destroying rpc to server.
427 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
430 LASSERT(req->rq_cli_ctx);
432 /* request might be asked to release earlier while still
433 * in the context waiting list.
435 if (!list_empty(&req->rq_ctx_chain)) {
436 spin_lock(&req->rq_cli_ctx->cc_lock);
437 list_del_init(&req->rq_ctx_chain);
438 spin_unlock(&req->rq_cli_ctx->cc_lock);
441 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
442 req->rq_cli_ctx = NULL;
446 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
447 struct ptlrpc_cli_ctx *oldctx,
448 struct ptlrpc_cli_ctx *newctx)
450 struct sptlrpc_flavor old_flvr;
451 char *reqmsg = NULL; /* to workaround old gcc */
455 LASSERT(req->rq_reqmsg);
456 LASSERT(req->rq_reqlen);
457 LASSERT(req->rq_replen);
459 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
461 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
462 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
463 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
464 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
467 old_flvr = req->rq_flvr;
469 /* save request message */
470 reqmsg_size = req->rq_reqlen;
471 if (reqmsg_size != 0) {
472 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
475 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
478 /* release old req/rep buf */
479 req->rq_cli_ctx = oldctx;
480 sptlrpc_cli_free_reqbuf(req);
481 sptlrpc_cli_free_repbuf(req);
482 req->rq_cli_ctx = newctx;
484 /* recalculate the flavor */
485 sptlrpc_req_set_flavor(req, 0);
487 /* alloc new request buffer
488 * we don't need to alloc reply buffer here, leave it to the
489 * rest procedure of ptlrpc */
490 if (reqmsg_size != 0) {
491 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
493 LASSERT(req->rq_reqmsg);
494 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
496 CWARN("failed to alloc reqbuf: %d\n", rc);
497 req->rq_flvr = old_flvr;
500 OBD_FREE_LARGE(reqmsg, reqmsg_size);
506 * If current context of \a req is dead somehow, e.g. we just switched flavor
507 * thus marked original contexts dead, we'll find a new context for it. if
508 * no switch is needed, \a req will end up with the same context.
510 * \note a request must have a context, to keep other parts of code happy.
511 * In any case of failure during the switching, we must restore the old one.
513 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
515 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
516 struct ptlrpc_cli_ctx *newctx;
521 sptlrpc_cli_ctx_get(oldctx);
522 sptlrpc_req_put_ctx(req, 0);
524 rc = sptlrpc_req_get_ctx(req);
526 LASSERT(!req->rq_cli_ctx);
528 /* restore old ctx */
529 req->rq_cli_ctx = oldctx;
533 newctx = req->rq_cli_ctx;
536 if (unlikely(newctx == oldctx &&
537 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
539 * still get the old dead ctx, usually means system too busy
542 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
543 newctx, newctx->cc_flags);
545 set_current_state(TASK_INTERRUPTIBLE);
546 schedule_timeout(HZ);
549 * it's possible newctx == oldctx if we're switching
550 * subflavor with the same sec.
552 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
554 /* restore old ctx */
555 sptlrpc_req_put_ctx(req, 0);
556 req->rq_cli_ctx = oldctx;
560 LASSERT(req->rq_cli_ctx == newctx);
563 sptlrpc_cli_ctx_put(oldctx, 1);
566 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
569 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
571 if (cli_ctx_is_refreshed(ctx))
577 int ctx_refresh_timeout(void *data)
579 struct ptlrpc_request *req = data;
582 /* conn_cnt is needed in expire_one_request */
583 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
585 rc = ptlrpc_expire_one_request(req, 1);
586 /* if we started recovery, we should mark this ctx dead; otherwise
587 * in case of lgssd died nobody would retire this ctx, following
588 * connecting will still find the same ctx thus cause deadlock.
589 * there's an assumption that expire time of the request should be
590 * later than the context refresh expire time.
593 req->rq_cli_ctx->cc_ops->force_die(req->rq_cli_ctx, 0);
598 void ctx_refresh_interrupt(void *data)
600 struct ptlrpc_request *req = data;
602 spin_lock(&req->rq_lock);
604 spin_unlock(&req->rq_lock);
608 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
610 spin_lock(&ctx->cc_lock);
611 if (!list_empty(&req->rq_ctx_chain))
612 list_del_init(&req->rq_ctx_chain);
613 spin_unlock(&ctx->cc_lock);
617 * To refresh the context of \req, if it's not up-to-date.
620 * - = 0: wait until success or fatal error occur
621 * - > 0: timeout value (in seconds)
623 * The status of the context could be subject to be changed by other threads
624 * at any time. We allow this race, but once we return with 0, the caller will
625 * suppose it's uptodated and keep using it until the owning rpc is done.
627 * \retval 0 only if the context is uptodated.
628 * \retval -ev error number.
630 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
632 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
633 struct ptlrpc_sec *sec;
634 struct l_wait_info lwi;
639 if (req->rq_ctx_init || req->rq_ctx_fini)
643 * during the process a request's context might change type even
644 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
648 rc = import_sec_validate_get(req->rq_import, &sec);
652 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
653 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
654 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
655 req_off_ctx_list(req, ctx);
656 sptlrpc_req_replace_dead_ctx(req);
657 ctx = req->rq_cli_ctx;
659 sptlrpc_sec_put(sec);
661 if (cli_ctx_is_eternal(ctx))
664 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
665 LASSERT(ctx->cc_ops->refresh);
666 ctx->cc_ops->refresh(ctx);
668 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
670 LASSERT(ctx->cc_ops->validate);
671 if (ctx->cc_ops->validate(ctx) == 0) {
672 req_off_ctx_list(req, ctx);
676 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
677 spin_lock(&req->rq_lock);
679 spin_unlock(&req->rq_lock);
680 req_off_ctx_list(req, ctx);
685 * There's a subtle issue for resending RPCs, suppose following
687 * 1. the request was sent to server.
688 * 2. recovery was kicked start, after finished the request was
690 * 3. resend the request.
691 * 4. old reply from server received, we accept and verify the reply.
692 * this has to be success, otherwise the error will be aware
694 * 5. new reply from server received, dropped by LNet.
696 * Note the xid of old & new request is the same. We can't simply
697 * change xid for the resent request because the server replies on
698 * it for reply reconstruction.
700 * Commonly the original context should be uptodate because we
701 * have a expiry nice time; server will keep its context because
702 * we at least hold a ref of old context which prevent context
703 * destroying RPC being sent. So server still can accept the request
704 * and finish the RPC. But if that's not the case:
705 * 1. If server side context has been trimmed, a NO_CONTEXT will
706 * be returned, gss_cli_ctx_verify/unseal will switch to new
708 * 2. Current context never be refreshed, then we are fine: we
709 * never really send request with old context before.
711 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
712 unlikely(req->rq_reqmsg) &&
713 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
714 req_off_ctx_list(req, ctx);
718 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
719 req_off_ctx_list(req, ctx);
721 * don't switch ctx if import was deactivated
723 if (req->rq_import->imp_deactive) {
724 spin_lock(&req->rq_lock);
726 spin_unlock(&req->rq_lock);
730 rc = sptlrpc_req_replace_dead_ctx(req);
732 LASSERT(ctx == req->rq_cli_ctx);
733 CERROR("req %p: failed to replace dead ctx %p: %d\n",
735 spin_lock(&req->rq_lock);
737 spin_unlock(&req->rq_lock);
741 ctx = req->rq_cli_ctx;
746 * Now we're sure this context is during upcall, add myself into
749 spin_lock(&ctx->cc_lock);
750 if (list_empty(&req->rq_ctx_chain))
751 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
752 spin_unlock(&ctx->cc_lock);
757 /* Clear any flags that may be present from previous sends */
758 LASSERT(req->rq_receiving_reply == 0);
759 spin_lock(&req->rq_lock);
761 req->rq_timedout = 0;
764 spin_unlock(&req->rq_lock);
766 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
767 ctx_refresh_interrupt, req);
768 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
771 * following cases could lead us here:
772 * - successfully refreshed;
774 * - timedout, and we don't want recover from the failure;
775 * - timedout, and waked up upon recovery finished;
776 * - someone else mark this ctx dead by force;
777 * - someone invalidate the req and call ptlrpc_client_wake_req(),
778 * e.g. ptlrpc_abort_inflight();
780 if (!cli_ctx_is_refreshed(ctx)) {
781 /* timed out or interrupted */
782 req_off_ctx_list(req, ctx);
792 * Initialize flavor settings for \a req, according to \a opcode.
794 * \note this could be called in two situations:
795 * - new request from ptlrpc_pre_req(), with proper @opcode
796 * - old request which changed ctx in the middle, with @opcode == 0
798 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
800 struct ptlrpc_sec *sec;
802 LASSERT(req->rq_import);
803 LASSERT(req->rq_cli_ctx);
804 LASSERT(req->rq_cli_ctx->cc_sec);
805 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
807 /* special security flags according to opcode */
811 case MGS_CONFIG_READ:
813 req->rq_bulk_read = 1;
817 req->rq_bulk_write = 1;
820 req->rq_ctx_init = 1;
823 req->rq_ctx_fini = 1;
826 /* init/fini rpc won't be resend, so can't be here */
827 LASSERT(req->rq_ctx_init == 0);
828 LASSERT(req->rq_ctx_fini == 0);
830 /* cleanup flags, which should be recalculated */
831 req->rq_pack_udesc = 0;
832 req->rq_pack_bulk = 0;
836 sec = req->rq_cli_ctx->cc_sec;
838 spin_lock(&sec->ps_lock);
839 req->rq_flvr = sec->ps_flvr;
840 spin_unlock(&sec->ps_lock);
842 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
844 if (unlikely(req->rq_ctx_init))
845 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
846 else if (unlikely(req->rq_ctx_fini))
847 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
849 /* user descriptor flag, null security can't do it anyway */
850 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
851 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
852 req->rq_pack_udesc = 1;
854 /* bulk security flag */
855 if ((req->rq_bulk_read || req->rq_bulk_write) &&
856 sptlrpc_flavor_has_bulk(&req->rq_flvr))
857 req->rq_pack_bulk = 1;
860 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
862 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
865 LASSERT(req->rq_clrbuf);
866 if (req->rq_pool || !req->rq_reqbuf)
869 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
870 req->rq_reqbuf = NULL;
871 req->rq_reqbuf_len = 0;
875 * Given an import \a imp, check whether current user has a valid context
876 * or not. We may create a new context and try to refresh it, and try
877 * repeatedly try in case of non-fatal errors. Return 0 means success.
879 int sptlrpc_import_check_ctx(struct obd_import *imp)
881 struct ptlrpc_sec *sec;
882 struct ptlrpc_cli_ctx *ctx;
883 struct ptlrpc_request *req = NULL;
888 sec = sptlrpc_import_sec_ref(imp);
889 ctx = get_my_ctx(sec);
890 sptlrpc_sec_put(sec);
895 if (cli_ctx_is_eternal(ctx) ||
896 ctx->cc_ops->validate(ctx) == 0) {
897 sptlrpc_cli_ctx_put(ctx, 1);
901 if (cli_ctx_is_error(ctx)) {
902 sptlrpc_cli_ctx_put(ctx, 1);
906 req = ptlrpc_request_cache_alloc(GFP_NOFS);
910 spin_lock_init(&req->rq_lock);
911 atomic_set(&req->rq_refcount, 10000);
912 INIT_LIST_HEAD(&req->rq_ctx_chain);
913 init_waitqueue_head(&req->rq_reply_waitq);
914 init_waitqueue_head(&req->rq_set_waitq);
915 req->rq_import = imp;
916 req->rq_flvr = sec->ps_flvr;
917 req->rq_cli_ctx = ctx;
919 rc = sptlrpc_req_refresh_ctx(req, 0);
920 LASSERT(list_empty(&req->rq_ctx_chain));
921 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
922 ptlrpc_request_cache_free(req);
928 * Used by ptlrpc client, to perform the pre-defined security transformation
929 * upon the request message of \a req. After this function called,
930 * req->rq_reqmsg is still accessible as clear text.
932 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
934 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
938 LASSERT(ctx->cc_sec);
939 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
941 /* we wrap bulk request here because now we can be sure
942 * the context is uptodate.
945 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
950 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
951 case SPTLRPC_SVC_NULL:
952 case SPTLRPC_SVC_AUTH:
953 case SPTLRPC_SVC_INTG:
954 LASSERT(ctx->cc_ops->sign);
955 rc = ctx->cc_ops->sign(ctx, req);
957 case SPTLRPC_SVC_PRIV:
958 LASSERT(ctx->cc_ops->seal);
959 rc = ctx->cc_ops->seal(ctx, req);
966 LASSERT(req->rq_reqdata_len);
967 LASSERT(req->rq_reqdata_len % 8 == 0);
968 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
974 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
976 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
980 LASSERT(ctx->cc_sec);
981 LASSERT(req->rq_repbuf);
982 LASSERT(req->rq_repdata);
983 LASSERT(req->rq_repmsg == NULL);
985 req->rq_rep_swab_mask = 0;
987 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
990 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
994 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
998 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
999 CERROR("replied data length %d too small\n",
1000 req->rq_repdata_len);
1004 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1005 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1006 CERROR("reply policy %u doesn't match request policy %u\n",
1007 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1008 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1012 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1013 case SPTLRPC_SVC_NULL:
1014 case SPTLRPC_SVC_AUTH:
1015 case SPTLRPC_SVC_INTG:
1016 LASSERT(ctx->cc_ops->verify);
1017 rc = ctx->cc_ops->verify(ctx, req);
1019 case SPTLRPC_SVC_PRIV:
1020 LASSERT(ctx->cc_ops->unseal);
1021 rc = ctx->cc_ops->unseal(ctx, req);
1026 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1028 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1030 req->rq_rep_swab_mask = 0;
1035 * Used by ptlrpc client, to perform security transformation upon the reply
1036 * message of \a req. After return successfully, req->rq_repmsg points to
1037 * the reply message in clear text.
1039 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1042 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1044 LASSERT(req->rq_repbuf);
1045 LASSERT(req->rq_repdata == NULL);
1046 LASSERT(req->rq_repmsg == NULL);
1047 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1049 if (req->rq_reply_off == 0 &&
1050 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1051 CERROR("real reply with offset 0\n");
1055 if (req->rq_reply_off % 8 != 0) {
1056 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1060 req->rq_repdata = (struct lustre_msg *)
1061 (req->rq_repbuf + req->rq_reply_off);
1062 req->rq_repdata_len = req->rq_nob_received;
1064 return do_cli_unwrap_reply(req);
1068 * Used by ptlrpc client, to perform security transformation upon the early
1069 * reply message of \a req. We expect the rq_reply_off is 0, and
1070 * rq_nob_received is the early reply size.
1072 * Because the receive buffer might be still posted, the reply data might be
1073 * changed at any time, no matter we're holding rq_lock or not. For this reason
1074 * we allocate a separate ptlrpc_request and reply buffer for early reply
1077 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1078 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1079 * \a *req_ret to release it.
1080 * \retval -ev error number, and \a req_ret will not be set.
1082 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1083 struct ptlrpc_request **req_ret)
1085 struct ptlrpc_request *early_req;
1087 int early_bufsz, early_size;
1090 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1091 if (early_req == NULL)
1094 early_size = req->rq_nob_received;
1095 early_bufsz = size_roundup_power2(early_size);
1096 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1097 if (early_buf == NULL) {
1102 /* sanity checkings and copy data out, do it inside spinlock */
1103 spin_lock(&req->rq_lock);
1105 if (req->rq_replied) {
1106 spin_unlock(&req->rq_lock);
1111 LASSERT(req->rq_repbuf);
1112 LASSERT(req->rq_repdata == NULL);
1113 LASSERT(req->rq_repmsg == NULL);
1115 if (req->rq_reply_off != 0) {
1116 CERROR("early reply with offset %u\n", req->rq_reply_off);
1117 spin_unlock(&req->rq_lock);
1122 if (req->rq_nob_received != early_size) {
1123 /* even another early arrived the size should be the same */
1124 CERROR("data size has changed from %u to %u\n",
1125 early_size, req->rq_nob_received);
1126 spin_unlock(&req->rq_lock);
1131 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1132 CERROR("early reply length %d too small\n",
1133 req->rq_nob_received);
1134 spin_unlock(&req->rq_lock);
1139 memcpy(early_buf, req->rq_repbuf, early_size);
1140 spin_unlock(&req->rq_lock);
1142 spin_lock_init(&early_req->rq_lock);
1143 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1144 early_req->rq_flvr = req->rq_flvr;
1145 early_req->rq_repbuf = early_buf;
1146 early_req->rq_repbuf_len = early_bufsz;
1147 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1148 early_req->rq_repdata_len = early_size;
1149 early_req->rq_early = 1;
1150 early_req->rq_reqmsg = req->rq_reqmsg;
1152 rc = do_cli_unwrap_reply(early_req);
1154 DEBUG_REQ(D_ADAPTTO, early_req,
1155 "error %d unwrap early reply", rc);
1159 LASSERT(early_req->rq_repmsg);
1160 *req_ret = early_req;
1164 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1166 OBD_FREE_LARGE(early_buf, early_bufsz);
1168 ptlrpc_request_cache_free(early_req);
1173 * Used by ptlrpc client, to release a processed early reply \a early_req.
1175 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1177 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1179 LASSERT(early_req->rq_repbuf);
1180 LASSERT(early_req->rq_repdata);
1181 LASSERT(early_req->rq_repmsg);
1183 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1184 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1185 ptlrpc_request_cache_free(early_req);
1188 /**************************************************
1190 **************************************************/
1193 * "fixed" sec (e.g. null) use sec_id < 0
1195 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1197 int sptlrpc_get_next_secid(void)
1199 return atomic_inc_return(&sptlrpc_sec_id);
1201 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1203 /**************************************************
1204 * client side high-level security APIs *
1205 **************************************************/
1207 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1208 int grace, int force)
1210 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1212 LASSERT(policy->sp_cops);
1213 LASSERT(policy->sp_cops->flush_ctx_cache);
1215 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1218 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1220 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1222 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1223 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1224 LASSERT(policy->sp_cops->destroy_sec);
1226 CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1228 policy->sp_cops->destroy_sec(sec);
1229 sptlrpc_policy_put(policy);
1232 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1234 sec_cop_destroy_sec(sec);
1236 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1238 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1240 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1242 if (sec->ps_policy->sp_cops->kill_sec) {
1243 sec->ps_policy->sp_cops->kill_sec(sec);
1245 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1249 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1252 atomic_inc(&sec->ps_refcount);
1256 EXPORT_SYMBOL(sptlrpc_sec_get);
1258 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1261 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1263 if (atomic_dec_and_test(&sec->ps_refcount)) {
1264 sptlrpc_gc_del_sec(sec);
1265 sec_cop_destroy_sec(sec);
1269 EXPORT_SYMBOL(sptlrpc_sec_put);
1272 * policy module is responsible for taking reference of import
1275 struct ptlrpc_sec *sptlrpc_sec_create(struct obd_import *imp,
1276 struct ptlrpc_svc_ctx *svc_ctx,
1277 struct sptlrpc_flavor *sf,
1278 enum lustre_sec_part sp)
1280 struct ptlrpc_sec_policy *policy;
1281 struct ptlrpc_sec *sec;
1285 LASSERT(imp->imp_dlm_fake == 1);
1287 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1288 imp->imp_obd->obd_type->typ_name,
1289 imp->imp_obd->obd_name,
1290 sptlrpc_flavor2name(sf, str, sizeof(str)));
1292 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1293 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1295 LASSERT(imp->imp_dlm_fake == 0);
1297 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1298 imp->imp_obd->obd_type->typ_name,
1299 imp->imp_obd->obd_name,
1300 sptlrpc_flavor2name(sf, str, sizeof(str)));
1302 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1304 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1309 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1311 atomic_inc(&sec->ps_refcount);
1315 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1316 sptlrpc_gc_add_sec(sec);
1318 sptlrpc_policy_put(policy);
1324 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1326 struct ptlrpc_sec *sec;
1328 spin_lock(&imp->imp_lock);
1329 sec = sptlrpc_sec_get(imp->imp_sec);
1330 spin_unlock(&imp->imp_lock);
1334 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1336 static void sptlrpc_import_sec_install(struct obd_import *imp,
1337 struct ptlrpc_sec *sec)
1339 struct ptlrpc_sec *old_sec;
1341 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1343 spin_lock(&imp->imp_lock);
1344 old_sec = imp->imp_sec;
1346 spin_unlock(&imp->imp_lock);
1349 sptlrpc_sec_kill(old_sec);
1351 /* balance the ref taken by this import */
1352 sptlrpc_sec_put(old_sec);
1357 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1359 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1363 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1368 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1369 struct ptlrpc_sec *sec,
1370 struct sptlrpc_flavor *sf)
1372 char str1[32], str2[32];
1374 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1375 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1376 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1377 str1, sizeof(str1)),
1378 sptlrpc_secflags2str(sf->sf_flags,
1379 str2, sizeof(str2)));
1381 spin_lock(&sec->ps_lock);
1382 flavor_copy(&sec->ps_flvr, sf);
1383 spin_unlock(&sec->ps_lock);
1387 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1388 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1390 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1391 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1393 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1394 struct ptlrpc_svc_ctx *svc_ctx,
1395 struct sptlrpc_flavor *flvr)
1397 struct ptlrpc_connection *conn;
1398 struct sptlrpc_flavor sf;
1399 struct ptlrpc_sec *sec, *newsec;
1400 enum lustre_sec_part sp;
1409 conn = imp->imp_connection;
1411 if (svc_ctx == NULL) {
1412 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1414 * normal import, determine flavor from rule set, except
1415 * for mgc the flavor is predetermined.
1417 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1418 sf = cliobd->cl_flvr_mgc;
1420 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1422 &cliobd->cl_target_uuid,
1425 sp = imp->imp_obd->u.cli.cl_sp_me;
1427 /* reverse import, determine flavor from incoming request */
1430 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1431 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1432 PTLRPC_SEC_FL_ROOTONLY;
1434 sp = sptlrpc_target_sec_part(imp->imp_obd);
1437 sec = sptlrpc_import_sec_ref(imp);
1441 if (flavor_equal(&sf, &sec->ps_flvr))
1444 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1445 imp->imp_obd->obd_name,
1446 obd_uuid2str(&conn->c_remote_uuid),
1447 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1448 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1450 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1451 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1452 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1453 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1454 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1457 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1458 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1459 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1460 imp->imp_obd->obd_name,
1461 obd_uuid2str(&conn->c_remote_uuid),
1462 LNET_NIDNET(conn->c_self),
1463 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1466 mutex_lock(&imp->imp_sec_mutex);
1468 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1470 sptlrpc_import_sec_install(imp, newsec);
1472 CERROR("import %s->%s: failed to create new sec\n",
1473 imp->imp_obd->obd_name,
1474 obd_uuid2str(&conn->c_remote_uuid));
1478 mutex_unlock(&imp->imp_sec_mutex);
1480 sptlrpc_sec_put(sec);
1484 void sptlrpc_import_sec_put(struct obd_import *imp)
1487 sptlrpc_sec_kill(imp->imp_sec);
1489 sptlrpc_sec_put(imp->imp_sec);
1490 imp->imp_sec = NULL;
1494 static void import_flush_ctx_common(struct obd_import *imp,
1495 uid_t uid, int grace, int force)
1497 struct ptlrpc_sec *sec;
1502 sec = sptlrpc_import_sec_ref(imp);
1506 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1507 sptlrpc_sec_put(sec);
1510 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1512 /* it's important to use grace mode, see explain in
1513 * sptlrpc_req_refresh_ctx() */
1514 import_flush_ctx_common(imp, 0, 1, 1);
1517 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1519 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1522 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1524 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1526 import_flush_ctx_common(imp, -1, 1, 1);
1528 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1531 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1532 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1534 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1536 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1537 struct ptlrpc_sec_policy *policy;
1541 LASSERT(ctx->cc_sec);
1542 LASSERT(ctx->cc_sec->ps_policy);
1543 LASSERT(req->rq_reqmsg == NULL);
1544 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1546 policy = ctx->cc_sec->ps_policy;
1547 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1549 LASSERT(req->rq_reqmsg);
1550 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1552 /* zeroing preallocated buffer */
1554 memset(req->rq_reqmsg, 0, msgsize);
1561 * Used by ptlrpc client to free request buffer of \a req. After this
1562 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1564 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1566 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1567 struct ptlrpc_sec_policy *policy;
1570 LASSERT(ctx->cc_sec);
1571 LASSERT(ctx->cc_sec->ps_policy);
1572 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1574 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1577 policy = ctx->cc_sec->ps_policy;
1578 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1579 req->rq_reqmsg = NULL;
1583 * NOTE caller must guarantee the buffer size is enough for the enlargement
1585 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1586 int segment, int newsize)
1589 int oldsize, oldmsg_size, movesize;
1591 LASSERT(segment < msg->lm_bufcount);
1592 LASSERT(msg->lm_buflens[segment] <= newsize);
1594 if (msg->lm_buflens[segment] == newsize)
1597 /* nothing to do if we are enlarging the last segment */
1598 if (segment == msg->lm_bufcount - 1) {
1599 msg->lm_buflens[segment] = newsize;
1603 oldsize = msg->lm_buflens[segment];
1605 src = lustre_msg_buf(msg, segment + 1, 0);
1606 msg->lm_buflens[segment] = newsize;
1607 dst = lustre_msg_buf(msg, segment + 1, 0);
1608 msg->lm_buflens[segment] = oldsize;
1610 /* move from segment + 1 to end segment */
1611 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1612 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1613 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1614 LASSERT(movesize >= 0);
1617 memmove(dst, src, movesize);
1619 /* note we don't clear the ares where old data live, not secret */
1621 /* finally set new segment size */
1622 msg->lm_buflens[segment] = newsize;
1624 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1627 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1628 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1629 * preserved after the enlargement. this must be called after original request
1630 * buffer being allocated.
1632 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1633 * so caller should refresh its local pointers if needed.
1635 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1636 int segment, int newsize)
1638 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1639 struct ptlrpc_sec_cops *cops;
1640 struct lustre_msg *msg = req->rq_reqmsg;
1644 LASSERT(msg->lm_bufcount > segment);
1645 LASSERT(msg->lm_buflens[segment] <= newsize);
1647 if (msg->lm_buflens[segment] == newsize)
1650 cops = ctx->cc_sec->ps_policy->sp_cops;
1651 LASSERT(cops->enlarge_reqbuf);
1652 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1654 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1657 * Used by ptlrpc client to allocate reply buffer of \a req.
1659 * \note After this, req->rq_repmsg is still not accessible.
1661 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1663 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1664 struct ptlrpc_sec_policy *policy;
1667 LASSERT(ctx->cc_sec);
1668 LASSERT(ctx->cc_sec->ps_policy);
1673 policy = ctx->cc_sec->ps_policy;
1674 return policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize);
1678 * Used by ptlrpc client to free reply buffer of \a req. After this
1679 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1681 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1683 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1684 struct ptlrpc_sec_policy *policy;
1687 LASSERT(ctx->cc_sec);
1688 LASSERT(ctx->cc_sec->ps_policy);
1689 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1691 if (req->rq_repbuf == NULL)
1693 LASSERT(req->rq_repbuf_len);
1695 policy = ctx->cc_sec->ps_policy;
1696 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1697 req->rq_repmsg = NULL;
1700 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1701 struct ptlrpc_cli_ctx *ctx)
1703 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1705 if (!policy->sp_cops->install_rctx)
1707 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1710 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1711 struct ptlrpc_svc_ctx *ctx)
1713 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1715 if (!policy->sp_sops->install_rctx)
1717 return policy->sp_sops->install_rctx(imp, ctx);
1720 /****************************************
1721 * server side security *
1722 ****************************************/
1724 static int flavor_allowed(struct sptlrpc_flavor *exp,
1725 struct ptlrpc_request *req)
1727 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1729 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1732 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1733 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1734 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1735 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1741 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1744 * Given an export \a exp, check whether the flavor of incoming \a req
1745 * is allowed by the export \a exp. Main logic is about taking care of
1746 * changing configurations. Return 0 means success.
1748 int sptlrpc_target_export_check(struct obd_export *exp,
1749 struct ptlrpc_request *req)
1751 struct sptlrpc_flavor flavor;
1756 /* client side export has no imp_reverse, skip
1757 * FIXME maybe we should check flavor this as well??? */
1758 if (exp->exp_imp_reverse == NULL)
1761 /* don't care about ctx fini rpc */
1762 if (req->rq_ctx_fini)
1765 spin_lock(&exp->exp_lock);
1767 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1768 * the first req with the new flavor, then treat it as current flavor,
1769 * adapt reverse sec according to it.
1770 * note the first rpc with new flavor might not be with root ctx, in
1771 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1772 if (unlikely(exp->exp_flvr_changed) &&
1773 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1774 /* make the new flavor as "current", and old ones as
1775 * about-to-expire */
1776 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1777 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1778 flavor = exp->exp_flvr_old[1];
1779 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1780 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1781 exp->exp_flvr_old[0] = exp->exp_flvr;
1782 exp->exp_flvr_expire[0] = get_seconds() +
1783 EXP_FLVR_UPDATE_EXPIRE;
1784 exp->exp_flvr = flavor;
1786 /* flavor change finished */
1787 exp->exp_flvr_changed = 0;
1788 LASSERT(exp->exp_flvr_adapt == 1);
1790 /* if it's gss, we only interested in root ctx init */
1791 if (req->rq_auth_gss &&
1792 !(req->rq_ctx_init &&
1793 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1794 req->rq_auth_usr_ost))) {
1795 spin_unlock(&exp->exp_lock);
1796 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1797 req->rq_auth_gss, req->rq_ctx_init,
1798 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1799 req->rq_auth_usr_ost);
1803 exp->exp_flvr_adapt = 0;
1804 spin_unlock(&exp->exp_lock);
1806 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1807 req->rq_svc_ctx, &flavor);
1810 /* if it equals to the current flavor, we accept it, but need to
1811 * dealing with reverse sec/ctx */
1812 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1813 /* most cases should return here, we only interested in
1814 * gss root ctx init */
1815 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1816 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1817 !req->rq_auth_usr_ost)) {
1818 spin_unlock(&exp->exp_lock);
1822 /* if flavor just changed, we should not proceed, just leave
1823 * it and current flavor will be discovered and replaced
1824 * shortly, and let _this_ rpc pass through */
1825 if (exp->exp_flvr_changed) {
1826 LASSERT(exp->exp_flvr_adapt);
1827 spin_unlock(&exp->exp_lock);
1831 if (exp->exp_flvr_adapt) {
1832 exp->exp_flvr_adapt = 0;
1833 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1834 exp, exp->exp_flvr.sf_rpc,
1835 exp->exp_flvr_old[0].sf_rpc,
1836 exp->exp_flvr_old[1].sf_rpc);
1837 flavor = exp->exp_flvr;
1838 spin_unlock(&exp->exp_lock);
1840 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1844 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
1845 exp, exp->exp_flvr.sf_rpc,
1846 exp->exp_flvr_old[0].sf_rpc,
1847 exp->exp_flvr_old[1].sf_rpc);
1848 spin_unlock(&exp->exp_lock);
1850 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1855 if (exp->exp_flvr_expire[0]) {
1856 if (exp->exp_flvr_expire[0] >= get_seconds()) {
1857 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1858 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the middle one (" CFS_DURATION_T ")\n", exp,
1859 exp->exp_flvr.sf_rpc,
1860 exp->exp_flvr_old[0].sf_rpc,
1861 exp->exp_flvr_old[1].sf_rpc,
1862 exp->exp_flvr_expire[0] -
1864 spin_unlock(&exp->exp_lock);
1868 CDEBUG(D_SEC, "mark middle expired\n");
1869 exp->exp_flvr_expire[0] = 0;
1871 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1872 exp->exp_flvr.sf_rpc,
1873 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1874 req->rq_flvr.sf_rpc);
1877 /* now it doesn't match the current flavor, the only chance we can
1878 * accept it is match the old flavors which is not expired. */
1879 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1880 if (exp->exp_flvr_expire[1] >= get_seconds()) {
1881 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1882 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (" CFS_DURATION_T ")\n",
1884 exp->exp_flvr.sf_rpc,
1885 exp->exp_flvr_old[0].sf_rpc,
1886 exp->exp_flvr_old[1].sf_rpc,
1887 exp->exp_flvr_expire[1] -
1889 spin_unlock(&exp->exp_lock);
1893 CDEBUG(D_SEC, "mark oldest expired\n");
1894 exp->exp_flvr_expire[1] = 0;
1896 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1897 exp, exp->exp_flvr.sf_rpc,
1898 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1899 req->rq_flvr.sf_rpc);
1901 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1902 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1903 exp->exp_flvr_old[1].sf_rpc);
1906 spin_unlock(&exp->exp_lock);
1908 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1909 exp, exp->exp_obd->obd_name,
1910 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1911 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1912 req->rq_flvr.sf_rpc,
1913 exp->exp_flvr.sf_rpc,
1914 exp->exp_flvr_old[0].sf_rpc,
1915 exp->exp_flvr_expire[0] ?
1916 (unsigned long) (exp->exp_flvr_expire[0] -
1918 exp->exp_flvr_old[1].sf_rpc,
1919 exp->exp_flvr_expire[1] ?
1920 (unsigned long) (exp->exp_flvr_expire[1] -
1921 get_seconds()) : 0);
1924 EXPORT_SYMBOL(sptlrpc_target_export_check);
1926 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1927 struct sptlrpc_rule_set *rset)
1929 struct obd_export *exp;
1930 struct sptlrpc_flavor new_flvr;
1934 spin_lock(&obd->obd_dev_lock);
1936 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1937 if (exp->exp_connection == NULL)
1940 /* note if this export had just been updated flavor
1941 * (exp_flvr_changed == 1), this will override the
1943 spin_lock(&exp->exp_lock);
1944 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1945 exp->exp_connection->c_peer.nid,
1947 if (exp->exp_flvr_changed ||
1948 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1949 exp->exp_flvr_old[1] = new_flvr;
1950 exp->exp_flvr_expire[1] = 0;
1951 exp->exp_flvr_changed = 1;
1952 exp->exp_flvr_adapt = 1;
1954 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1955 exp, sptlrpc_part2name(exp->exp_sp_peer),
1956 exp->exp_flvr.sf_rpc,
1957 exp->exp_flvr_old[1].sf_rpc);
1959 spin_unlock(&exp->exp_lock);
1962 spin_unlock(&obd->obd_dev_lock);
1964 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1966 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1968 /* peer's claim is unreliable unless gss is being used */
1969 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1972 switch (req->rq_sp_from) {
1974 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1975 DEBUG_REQ(D_ERROR, req, "faked source CLI");
1976 svc_rc = SECSVC_DROP;
1980 if (!req->rq_auth_usr_mdt) {
1981 DEBUG_REQ(D_ERROR, req, "faked source MDT");
1982 svc_rc = SECSVC_DROP;
1986 if (!req->rq_auth_usr_ost) {
1987 DEBUG_REQ(D_ERROR, req, "faked source OST");
1988 svc_rc = SECSVC_DROP;
1993 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1994 !req->rq_auth_usr_ost) {
1995 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
1996 svc_rc = SECSVC_DROP;
2001 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2002 svc_rc = SECSVC_DROP;
2009 * Used by ptlrpc server, to perform transformation upon request message of
2010 * incoming \a req. This must be the first thing to do with a incoming
2011 * request in ptlrpc layer.
2013 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2014 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2015 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2016 * reply message has been prepared.
2017 * \retval SECSVC_DROP failed, this request should be dropped.
2019 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2021 struct ptlrpc_sec_policy *policy;
2022 struct lustre_msg *msg = req->rq_reqbuf;
2026 LASSERT(req->rq_reqmsg == NULL);
2027 LASSERT(req->rq_repmsg == NULL);
2028 LASSERT(req->rq_svc_ctx == NULL);
2030 req->rq_req_swab_mask = 0;
2032 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2035 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2039 CERROR("error unpacking request from %s x%llu\n",
2040 libcfs_id2str(req->rq_peer), req->rq_xid);
2044 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2045 req->rq_sp_from = LUSTRE_SP_ANY;
2046 req->rq_auth_uid = -1;
2047 req->rq_auth_mapped_uid = -1;
2049 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2051 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2055 LASSERT(policy->sp_sops->accept);
2056 rc = policy->sp_sops->accept(req);
2057 sptlrpc_policy_put(policy);
2058 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2059 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2062 * if it's not null flavor (which means embedded packing msg),
2063 * reset the swab mask for the coming inner msg unpacking.
2065 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2066 req->rq_req_swab_mask = 0;
2068 /* sanity check for the request source */
2069 rc = sptlrpc_svc_check_from(req, rc);
2074 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2075 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2076 * a buffer of \a msglen size.
2078 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2080 struct ptlrpc_sec_policy *policy;
2081 struct ptlrpc_reply_state *rs;
2084 LASSERT(req->rq_svc_ctx);
2085 LASSERT(req->rq_svc_ctx->sc_policy);
2087 policy = req->rq_svc_ctx->sc_policy;
2088 LASSERT(policy->sp_sops->alloc_rs);
2090 rc = policy->sp_sops->alloc_rs(req, msglen);
2091 if (unlikely(rc == -ENOMEM)) {
2092 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2093 if (svcpt->scp_service->srv_max_reply_size <
2094 msglen + sizeof(struct ptlrpc_reply_state)) {
2095 /* Just return failure if the size is too big */
2096 CERROR("size of message is too big (%zd), %d allowed",
2097 msglen + sizeof(struct ptlrpc_reply_state),
2098 svcpt->scp_service->srv_max_reply_size);
2102 /* failed alloc, try emergency pool */
2103 rs = lustre_get_emerg_rs(svcpt);
2107 req->rq_reply_state = rs;
2108 rc = policy->sp_sops->alloc_rs(req, msglen);
2110 lustre_put_emerg_rs(rs);
2111 req->rq_reply_state = NULL;
2116 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2122 * Used by ptlrpc server, to perform transformation upon reply message.
2124 * \post req->rq_reply_off is set to appropriate server-controlled reply offset.
2125 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2127 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2129 struct ptlrpc_sec_policy *policy;
2132 LASSERT(req->rq_svc_ctx);
2133 LASSERT(req->rq_svc_ctx->sc_policy);
2135 policy = req->rq_svc_ctx->sc_policy;
2136 LASSERT(policy->sp_sops->authorize);
2138 rc = policy->sp_sops->authorize(req);
2139 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2145 * Used by ptlrpc server, to free reply_state.
2147 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2149 struct ptlrpc_sec_policy *policy;
2150 unsigned int prealloc;
2152 LASSERT(rs->rs_svc_ctx);
2153 LASSERT(rs->rs_svc_ctx->sc_policy);
2155 policy = rs->rs_svc_ctx->sc_policy;
2156 LASSERT(policy->sp_sops->free_rs);
2158 prealloc = rs->rs_prealloc;
2159 policy->sp_sops->free_rs(rs);
2162 lustre_put_emerg_rs(rs);
2165 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2167 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2170 atomic_inc(&ctx->sc_refcount);
2173 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2175 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2180 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2181 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2182 if (ctx->sc_policy->sp_sops->free_ctx)
2183 ctx->sc_policy->sp_sops->free_ctx(ctx);
2185 req->rq_svc_ctx = NULL;
2188 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2190 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2195 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2196 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2197 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2199 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2201 /****************************************
2203 ****************************************/
2206 * Perform transformation upon bulk data pointed by \a desc. This is called
2207 * before transforming the request message.
2209 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2210 struct ptlrpc_bulk_desc *desc)
2212 struct ptlrpc_cli_ctx *ctx;
2214 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2216 if (!req->rq_pack_bulk)
2219 ctx = req->rq_cli_ctx;
2220 if (ctx->cc_ops->wrap_bulk)
2221 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2224 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2227 * This is called after unwrap the reply message.
2228 * return nob of actual plain text size received, or error code.
2230 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2231 struct ptlrpc_bulk_desc *desc,
2234 struct ptlrpc_cli_ctx *ctx;
2237 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2239 if (!req->rq_pack_bulk)
2240 return desc->bd_nob_transferred;
2242 ctx = req->rq_cli_ctx;
2243 if (ctx->cc_ops->unwrap_bulk) {
2244 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2248 return desc->bd_nob_transferred;
2250 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2253 * This is called after unwrap the reply message.
2254 * return 0 for success or error code.
2256 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2257 struct ptlrpc_bulk_desc *desc)
2259 struct ptlrpc_cli_ctx *ctx;
2262 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2264 if (!req->rq_pack_bulk)
2267 ctx = req->rq_cli_ctx;
2268 if (ctx->cc_ops->unwrap_bulk) {
2269 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2275 * if everything is going right, nob should equals to nob_transferred.
2276 * in case of privacy mode, nob_transferred needs to be adjusted.
2278 if (desc->bd_nob != desc->bd_nob_transferred) {
2279 CERROR("nob %d doesn't match transferred nob %d",
2280 desc->bd_nob, desc->bd_nob_transferred);
2286 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2289 /****************************************
2290 * user descriptor helpers *
2291 ****************************************/
2293 int sptlrpc_current_user_desc_size(void)
2297 ngroups = current_ngroups;
2299 if (ngroups > LUSTRE_MAX_GROUPS)
2300 ngroups = LUSTRE_MAX_GROUPS;
2301 return sptlrpc_user_desc_size(ngroups);
2303 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2305 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2307 struct ptlrpc_user_desc *pud;
2309 pud = lustre_msg_buf(msg, offset, 0);
2311 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2312 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2313 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2314 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2315 pud->pud_cap = cfs_curproc_cap_pack();
2316 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2319 if (pud->pud_ngroups > current_ngroups)
2320 pud->pud_ngroups = current_ngroups;
2321 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2322 pud->pud_ngroups * sizeof(__u32));
2323 task_unlock(current);
2327 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2329 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2331 struct ptlrpc_user_desc *pud;
2334 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2339 __swab32s(&pud->pud_uid);
2340 __swab32s(&pud->pud_gid);
2341 __swab32s(&pud->pud_fsuid);
2342 __swab32s(&pud->pud_fsgid);
2343 __swab32s(&pud->pud_cap);
2344 __swab32s(&pud->pud_ngroups);
2347 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2348 CERROR("%u groups is too large\n", pud->pud_ngroups);
2352 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2353 msg->lm_buflens[offset]) {
2354 CERROR("%u groups are claimed but bufsize only %u\n",
2355 pud->pud_ngroups, msg->lm_buflens[offset]);
2360 for (i = 0; i < pud->pud_ngroups; i++)
2361 __swab32s(&pud->pud_groups[i]);
2366 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2368 /****************************************
2370 ****************************************/
2372 const char *sec2target_str(struct ptlrpc_sec *sec)
2374 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2376 if (sec_is_reverse(sec))
2378 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2380 EXPORT_SYMBOL(sec2target_str);
2383 * return true if the bulk data is protected
2385 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2387 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2388 case SPTLRPC_BULK_SVC_INTG:
2389 case SPTLRPC_BULK_SVC_PRIV:
2395 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2397 /****************************************
2398 * crypto API helper/alloc blkciper *
2399 ****************************************/
2401 /****************************************
2402 * initialize/finalize *
2403 ****************************************/
2405 int sptlrpc_init(void)
2409 rwlock_init(&policy_lock);
2411 rc = sptlrpc_gc_init();
2415 rc = sptlrpc_conf_init();
2419 rc = sptlrpc_enc_pool_init();
2423 rc = sptlrpc_null_init();
2427 rc = sptlrpc_plain_init();
2431 rc = sptlrpc_lproc_init();
2438 sptlrpc_plain_fini();
2440 sptlrpc_null_fini();
2442 sptlrpc_enc_pool_fini();
2444 sptlrpc_conf_fini();
2451 void sptlrpc_fini(void)
2453 sptlrpc_lproc_fini();
2454 sptlrpc_plain_fini();
2455 sptlrpc_null_fini();
2456 sptlrpc_enc_pool_fini();
2457 sptlrpc_conf_fini();