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 <linux/libcfs/libcfs.h>
44 #include <linux/crypto.h>
45 #include <linux/key.h>
48 #include <obd_class.h>
49 #include <obd_support.h>
50 #include <lustre_net.h>
51 #include <lustre_import.h>
52 #include <lustre_dlm.h>
53 #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 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
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 strncat(buf, bspec, bufsize);
226 buf[bufsize - 1] = '\0';
229 EXPORT_SYMBOL(sptlrpc_flavor2name);
231 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
235 if (flags & PTLRPC_SEC_FL_REVERSE)
236 strlcat(buf, "reverse,", bufsize);
237 if (flags & PTLRPC_SEC_FL_ROOTONLY)
238 strlcat(buf, "rootonly,", bufsize);
239 if (flags & PTLRPC_SEC_FL_UDESC)
240 strlcat(buf, "udesc,", bufsize);
241 if (flags & PTLRPC_SEC_FL_BULK)
242 strlcat(buf, "bulk,", bufsize);
244 strlcat(buf, "-,", bufsize);
248 EXPORT_SYMBOL(sptlrpc_secflags2str);
250 /**************************************************
251 * client context APIs *
252 **************************************************/
255 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
257 struct vfs_cred vcred;
258 int create = 1, remove_dead = 1;
261 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
263 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
264 PTLRPC_SEC_FL_ROOTONLY)) {
267 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
272 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
273 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
276 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
277 create, remove_dead);
280 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
282 atomic_inc(&ctx->cc_refcount);
285 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
287 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
289 struct ptlrpc_sec *sec = ctx->cc_sec;
292 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
294 if (!atomic_dec_and_test(&ctx->cc_refcount))
297 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
299 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
302 * Expire the client context immediately.
304 * \pre Caller must hold at least 1 reference on the \a ctx.
306 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
308 LASSERT(ctx->cc_ops->die);
309 ctx->cc_ops->die(ctx, 0);
311 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
314 * To wake up the threads who are waiting for this client context. Called
315 * after some status change happened on \a ctx.
317 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
319 struct ptlrpc_request *req, *next;
321 spin_lock(&ctx->cc_lock);
322 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
324 list_del_init(&req->rq_ctx_chain);
325 ptlrpc_client_wake_req(req);
327 spin_unlock(&ctx->cc_lock);
329 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
331 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
333 LASSERT(ctx->cc_ops);
335 if (ctx->cc_ops->display == NULL)
338 return ctx->cc_ops->display(ctx, buf, bufsize);
341 static int import_sec_check_expire(struct obd_import *imp)
345 spin_lock(&imp->imp_lock);
346 if (imp->imp_sec_expire &&
347 imp->imp_sec_expire < cfs_time_current_sec()) {
349 imp->imp_sec_expire = 0;
351 spin_unlock(&imp->imp_lock);
356 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
357 return sptlrpc_import_sec_adapt(imp, NULL, 0);
360 static int import_sec_validate_get(struct obd_import *imp,
361 struct ptlrpc_sec **sec)
365 if (unlikely(imp->imp_sec_expire)) {
366 rc = import_sec_check_expire(imp);
371 *sec = sptlrpc_import_sec_ref(imp);
373 CERROR("import %p (%s) with no sec\n",
374 imp, ptlrpc_import_state_name(imp->imp_state));
378 if (unlikely((*sec)->ps_dying)) {
379 CERROR("attempt to use dying sec %p\n", sec);
380 sptlrpc_sec_put(*sec);
388 * Given a \a req, find or allocate a appropriate context for it.
389 * \pre req->rq_cli_ctx == NULL.
391 * \retval 0 succeed, and req->rq_cli_ctx is set.
392 * \retval -ev error number, and req->rq_cli_ctx == NULL.
394 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
396 struct obd_import *imp = req->rq_import;
397 struct ptlrpc_sec *sec;
401 LASSERT(!req->rq_cli_ctx);
404 rc = import_sec_validate_get(imp, &sec);
408 req->rq_cli_ctx = get_my_ctx(sec);
410 sptlrpc_sec_put(sec);
412 if (!req->rq_cli_ctx) {
413 CERROR("req %p: fail to get context\n", req);
421 * Drop the context for \a req.
422 * \pre req->rq_cli_ctx != NULL.
423 * \post req->rq_cli_ctx == NULL.
425 * If \a sync == 0, this function should return quickly without sleep;
426 * otherwise it might trigger and wait for the whole process of sending
427 * an context-destroying rpc to server.
429 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
434 LASSERT(req->rq_cli_ctx);
436 /* request might be asked to release earlier while still
437 * in the context waiting list.
439 if (!list_empty(&req->rq_ctx_chain)) {
440 spin_lock(&req->rq_cli_ctx->cc_lock);
441 list_del_init(&req->rq_ctx_chain);
442 spin_unlock(&req->rq_cli_ctx->cc_lock);
445 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
446 req->rq_cli_ctx = NULL;
451 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
452 struct ptlrpc_cli_ctx *oldctx,
453 struct ptlrpc_cli_ctx *newctx)
455 struct sptlrpc_flavor old_flvr;
456 char *reqmsg = NULL; /* to workaround old gcc */
460 LASSERT(req->rq_reqmsg);
461 LASSERT(req->rq_reqlen);
462 LASSERT(req->rq_replen);
464 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
465 "switch sec %p(%s) -> %p(%s)\n", req,
466 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
467 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
468 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
469 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
472 old_flvr = req->rq_flvr;
474 /* save request message */
475 reqmsg_size = req->rq_reqlen;
476 if (reqmsg_size != 0) {
477 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
480 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
483 /* release old req/rep buf */
484 req->rq_cli_ctx = oldctx;
485 sptlrpc_cli_free_reqbuf(req);
486 sptlrpc_cli_free_repbuf(req);
487 req->rq_cli_ctx = newctx;
489 /* recalculate the flavor */
490 sptlrpc_req_set_flavor(req, 0);
492 /* alloc new request buffer
493 * we don't need to alloc reply buffer here, leave it to the
494 * rest procedure of ptlrpc */
495 if (reqmsg_size != 0) {
496 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
498 LASSERT(req->rq_reqmsg);
499 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
501 CWARN("failed to alloc reqbuf: %d\n", rc);
502 req->rq_flvr = old_flvr;
505 OBD_FREE_LARGE(reqmsg, reqmsg_size);
511 * If current context of \a req is dead somehow, e.g. we just switched flavor
512 * thus marked original contexts dead, we'll find a new context for it. if
513 * no switch is needed, \a req will end up with the same context.
515 * \note a request must have a context, to keep other parts of code happy.
516 * In any case of failure during the switching, we must restore the old one.
518 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
520 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
521 struct ptlrpc_cli_ctx *newctx;
527 sptlrpc_cli_ctx_get(oldctx);
528 sptlrpc_req_put_ctx(req, 0);
530 rc = sptlrpc_req_get_ctx(req);
532 LASSERT(!req->rq_cli_ctx);
534 /* restore old ctx */
535 req->rq_cli_ctx = oldctx;
539 newctx = req->rq_cli_ctx;
542 if (unlikely(newctx == oldctx &&
543 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
545 * still get the old dead ctx, usually means system too busy
548 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
549 newctx, newctx->cc_flags);
551 schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,
555 * it's possible newctx == oldctx if we're switching
556 * subflavor with the same sec.
558 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
560 /* restore old ctx */
561 sptlrpc_req_put_ctx(req, 0);
562 req->rq_cli_ctx = oldctx;
566 LASSERT(req->rq_cli_ctx == newctx);
569 sptlrpc_cli_ctx_put(oldctx, 1);
572 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
575 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
577 if (cli_ctx_is_refreshed(ctx))
583 int ctx_refresh_timeout(void *data)
585 struct ptlrpc_request *req = data;
588 /* conn_cnt is needed in expire_one_request */
589 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
591 rc = ptlrpc_expire_one_request(req, 1);
592 /* if we started recovery, we should mark this ctx dead; otherwise
593 * in case of lgssd died nobody would retire this ctx, following
594 * connecting will still find the same ctx thus cause deadlock.
595 * there's an assumption that expire time of the request should be
596 * later than the context refresh expire time.
599 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
604 void ctx_refresh_interrupt(void *data)
606 struct ptlrpc_request *req = data;
608 spin_lock(&req->rq_lock);
610 spin_unlock(&req->rq_lock);
614 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
616 spin_lock(&ctx->cc_lock);
617 if (!list_empty(&req->rq_ctx_chain))
618 list_del_init(&req->rq_ctx_chain);
619 spin_unlock(&ctx->cc_lock);
623 * To refresh the context of \req, if it's not up-to-date.
626 * - = 0: wait until success or fatal error occur
627 * - > 0: timeout value (in seconds)
629 * The status of the context could be subject to be changed by other threads
630 * at any time. We allow this race, but once we return with 0, the caller will
631 * suppose it's uptodated and keep using it until the owning rpc is done.
633 * \retval 0 only if the context is uptodated.
634 * \retval -ev error number.
636 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
638 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
639 struct ptlrpc_sec *sec;
640 struct l_wait_info lwi;
646 if (req->rq_ctx_init || req->rq_ctx_fini)
650 * during the process a request's context might change type even
651 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
655 rc = import_sec_validate_get(req->rq_import, &sec);
659 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
660 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
661 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
662 req_off_ctx_list(req, ctx);
663 sptlrpc_req_replace_dead_ctx(req);
664 ctx = req->rq_cli_ctx;
666 sptlrpc_sec_put(sec);
668 if (cli_ctx_is_eternal(ctx))
671 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
672 LASSERT(ctx->cc_ops->refresh);
673 ctx->cc_ops->refresh(ctx);
675 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
677 LASSERT(ctx->cc_ops->validate);
678 if (ctx->cc_ops->validate(ctx) == 0) {
679 req_off_ctx_list(req, ctx);
683 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
684 spin_lock(&req->rq_lock);
686 spin_unlock(&req->rq_lock);
687 req_off_ctx_list(req, ctx);
692 * There's a subtle issue for resending RPCs, suppose following
694 * 1. the request was sent to server.
695 * 2. recovery was kicked start, after finished the request was
697 * 3. resend the request.
698 * 4. old reply from server received, we accept and verify the reply.
699 * this has to be success, otherwise the error will be aware
701 * 5. new reply from server received, dropped by LNet.
703 * Note the xid of old & new request is the same. We can't simply
704 * change xid for the resent request because the server replies on
705 * it for reply reconstruction.
707 * Commonly the original context should be uptodate because we
708 * have a expiry nice time; server will keep its context because
709 * we at least hold a ref of old context which prevent context
710 * destroying RPC being sent. So server still can accept the request
711 * and finish the RPC. But if that's not the case:
712 * 1. If server side context has been trimmed, a NO_CONTEXT will
713 * be returned, gss_cli_ctx_verify/unseal will switch to new
715 * 2. Current context never be refreshed, then we are fine: we
716 * never really send request with old context before.
718 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
719 unlikely(req->rq_reqmsg) &&
720 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
721 req_off_ctx_list(req, ctx);
725 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
726 req_off_ctx_list(req, ctx);
728 * don't switch ctx if import was deactivated
730 if (req->rq_import->imp_deactive) {
731 spin_lock(&req->rq_lock);
733 spin_unlock(&req->rq_lock);
737 rc = sptlrpc_req_replace_dead_ctx(req);
739 LASSERT(ctx == req->rq_cli_ctx);
740 CERROR("req %p: failed to replace dead ctx %p: %d\n",
742 spin_lock(&req->rq_lock);
744 spin_unlock(&req->rq_lock);
748 ctx = req->rq_cli_ctx;
753 * Now we're sure this context is during upcall, add myself into
756 spin_lock(&ctx->cc_lock);
757 if (list_empty(&req->rq_ctx_chain))
758 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
759 spin_unlock(&ctx->cc_lock);
762 RETURN(-EWOULDBLOCK);
764 /* Clear any flags that may be present from previous sends */
765 LASSERT(req->rq_receiving_reply == 0);
766 spin_lock(&req->rq_lock);
768 req->rq_timedout = 0;
771 spin_unlock(&req->rq_lock);
773 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
774 ctx_refresh_interrupt, req);
775 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
778 * following cases could lead us here:
779 * - successfully refreshed;
781 * - timedout, and we don't want recover from the failure;
782 * - timedout, and waked up upon recovery finished;
783 * - someone else mark this ctx dead by force;
784 * - someone invalidate the req and call ptlrpc_client_wake_req(),
785 * e.g. ptlrpc_abort_inflight();
787 if (!cli_ctx_is_refreshed(ctx)) {
788 /* timed out or interruptted */
789 req_off_ctx_list(req, ctx);
799 * Initialize flavor settings for \a req, according to \a opcode.
801 * \note this could be called in two situations:
802 * - new request from ptlrpc_pre_req(), with proper @opcode
803 * - old request which changed ctx in the middle, with @opcode == 0
805 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
807 struct ptlrpc_sec *sec;
809 LASSERT(req->rq_import);
810 LASSERT(req->rq_cli_ctx);
811 LASSERT(req->rq_cli_ctx->cc_sec);
812 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
814 /* special security flags accoding to opcode */
818 case MGS_CONFIG_READ:
820 req->rq_bulk_read = 1;
824 req->rq_bulk_write = 1;
827 req->rq_ctx_init = 1;
830 req->rq_ctx_fini = 1;
833 /* init/fini rpc won't be resend, so can't be here */
834 LASSERT(req->rq_ctx_init == 0);
835 LASSERT(req->rq_ctx_fini == 0);
837 /* cleanup flags, which should be recalculated */
838 req->rq_pack_udesc = 0;
839 req->rq_pack_bulk = 0;
843 sec = req->rq_cli_ctx->cc_sec;
845 spin_lock(&sec->ps_lock);
846 req->rq_flvr = sec->ps_flvr;
847 spin_unlock(&sec->ps_lock);
849 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
851 if (unlikely(req->rq_ctx_init))
852 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
853 else if (unlikely(req->rq_ctx_fini))
854 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
856 /* user descriptor flag, null security can't do it anyway */
857 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
858 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
859 req->rq_pack_udesc = 1;
861 /* bulk security flag */
862 if ((req->rq_bulk_read || req->rq_bulk_write) &&
863 sptlrpc_flavor_has_bulk(&req->rq_flvr))
864 req->rq_pack_bulk = 1;
867 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
869 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
872 LASSERT(req->rq_clrbuf);
873 if (req->rq_pool || !req->rq_reqbuf)
876 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
877 req->rq_reqbuf = NULL;
878 req->rq_reqbuf_len = 0;
882 * Given an import \a imp, check whether current user has a valid context
883 * or not. We may create a new context and try to refresh it, and try
884 * repeatedly try in case of non-fatal errors. Return 0 means success.
886 int sptlrpc_import_check_ctx(struct obd_import *imp)
888 struct ptlrpc_sec *sec;
889 struct ptlrpc_cli_ctx *ctx;
890 struct ptlrpc_request *req = NULL;
896 sec = sptlrpc_import_sec_ref(imp);
897 ctx = get_my_ctx(sec);
898 sptlrpc_sec_put(sec);
903 if (cli_ctx_is_eternal(ctx) ||
904 ctx->cc_ops->validate(ctx) == 0) {
905 sptlrpc_cli_ctx_put(ctx, 1);
909 if (cli_ctx_is_error(ctx)) {
910 sptlrpc_cli_ctx_put(ctx, 1);
918 spin_lock_init(&req->rq_lock);
919 atomic_set(&req->rq_refcount, 10000);
920 INIT_LIST_HEAD(&req->rq_ctx_chain);
921 init_waitqueue_head(&req->rq_reply_waitq);
922 init_waitqueue_head(&req->rq_set_waitq);
923 req->rq_import = imp;
924 req->rq_flvr = sec->ps_flvr;
925 req->rq_cli_ctx = ctx;
927 rc = sptlrpc_req_refresh_ctx(req, 0);
928 LASSERT(list_empty(&req->rq_ctx_chain));
929 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
936 * Used by ptlrpc client, to perform the pre-defined security transformation
937 * upon the request message of \a req. After this function called,
938 * req->rq_reqmsg is still accessible as clear text.
940 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
942 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
947 LASSERT(ctx->cc_sec);
948 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
950 /* we wrap bulk request here because now we can be sure
951 * the context is uptodate.
954 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
959 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
960 case SPTLRPC_SVC_NULL:
961 case SPTLRPC_SVC_AUTH:
962 case SPTLRPC_SVC_INTG:
963 LASSERT(ctx->cc_ops->sign);
964 rc = ctx->cc_ops->sign(ctx, req);
966 case SPTLRPC_SVC_PRIV:
967 LASSERT(ctx->cc_ops->seal);
968 rc = ctx->cc_ops->seal(ctx, req);
975 LASSERT(req->rq_reqdata_len);
976 LASSERT(req->rq_reqdata_len % 8 == 0);
977 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
983 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
985 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
990 LASSERT(ctx->cc_sec);
991 LASSERT(req->rq_repbuf);
992 LASSERT(req->rq_repdata);
993 LASSERT(req->rq_repmsg == NULL);
995 req->rq_rep_swab_mask = 0;
997 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1000 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1004 CERROR("failed unpack reply: x"LPU64"\n", req->rq_xid);
1008 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1009 CERROR("replied data length %d too small\n",
1010 req->rq_repdata_len);
1014 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1015 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1016 CERROR("reply policy %u doesn't match request policy %u\n",
1017 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1018 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1022 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1023 case SPTLRPC_SVC_NULL:
1024 case SPTLRPC_SVC_AUTH:
1025 case SPTLRPC_SVC_INTG:
1026 LASSERT(ctx->cc_ops->verify);
1027 rc = ctx->cc_ops->verify(ctx, req);
1029 case SPTLRPC_SVC_PRIV:
1030 LASSERT(ctx->cc_ops->unseal);
1031 rc = ctx->cc_ops->unseal(ctx, req);
1036 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1038 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1040 req->rq_rep_swab_mask = 0;
1045 * Used by ptlrpc client, to perform security transformation upon the reply
1046 * message of \a req. After return successfully, req->rq_repmsg points to
1047 * the reply message in clear text.
1049 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1052 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1054 LASSERT(req->rq_repbuf);
1055 LASSERT(req->rq_repdata == NULL);
1056 LASSERT(req->rq_repmsg == NULL);
1057 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1059 if (req->rq_reply_off == 0 &&
1060 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1061 CERROR("real reply with offset 0\n");
1065 if (req->rq_reply_off % 8 != 0) {
1066 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1070 req->rq_repdata = (struct lustre_msg *)
1071 (req->rq_repbuf + req->rq_reply_off);
1072 req->rq_repdata_len = req->rq_nob_received;
1074 return do_cli_unwrap_reply(req);
1078 * Used by ptlrpc client, to perform security transformation upon the early
1079 * reply message of \a req. We expect the rq_reply_off is 0, and
1080 * rq_nob_received is the early reply size.
1082 * Because the receive buffer might be still posted, the reply data might be
1083 * changed at any time, no matter we're holding rq_lock or not. For this reason
1084 * we allocate a separate ptlrpc_request and reply buffer for early reply
1087 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1088 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1089 * \a *req_ret to release it.
1090 * \retval -ev error number, and \a req_ret will not be set.
1092 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1093 struct ptlrpc_request **req_ret)
1095 struct ptlrpc_request *early_req;
1097 int early_bufsz, early_size;
1101 OBD_ALLOC_PTR(early_req);
1102 if (early_req == NULL)
1105 early_size = req->rq_nob_received;
1106 early_bufsz = size_roundup_power2(early_size);
1107 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1108 if (early_buf == NULL)
1109 GOTO(err_req, rc = -ENOMEM);
1111 /* sanity checkings and copy data out, do it inside spinlock */
1112 spin_lock(&req->rq_lock);
1114 if (req->rq_replied) {
1115 spin_unlock(&req->rq_lock);
1116 GOTO(err_buf, rc = -EALREADY);
1119 LASSERT(req->rq_repbuf);
1120 LASSERT(req->rq_repdata == NULL);
1121 LASSERT(req->rq_repmsg == NULL);
1123 if (req->rq_reply_off != 0) {
1124 CERROR("early reply with offset %u\n", req->rq_reply_off);
1125 spin_unlock(&req->rq_lock);
1126 GOTO(err_buf, rc = -EPROTO);
1129 if (req->rq_nob_received != early_size) {
1130 /* even another early arrived the size should be the same */
1131 CERROR("data size has changed from %u to %u\n",
1132 early_size, req->rq_nob_received);
1133 spin_unlock(&req->rq_lock);
1134 GOTO(err_buf, rc = -EINVAL);
1137 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1138 CERROR("early reply length %d too small\n",
1139 req->rq_nob_received);
1140 spin_unlock(&req->rq_lock);
1141 GOTO(err_buf, rc = -EALREADY);
1144 memcpy(early_buf, req->rq_repbuf, early_size);
1145 spin_unlock(&req->rq_lock);
1147 spin_lock_init(&early_req->rq_lock);
1148 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1149 early_req->rq_flvr = req->rq_flvr;
1150 early_req->rq_repbuf = early_buf;
1151 early_req->rq_repbuf_len = early_bufsz;
1152 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1153 early_req->rq_repdata_len = early_size;
1154 early_req->rq_early = 1;
1155 early_req->rq_reqmsg = req->rq_reqmsg;
1157 rc = do_cli_unwrap_reply(early_req);
1159 DEBUG_REQ(D_ADAPTTO, early_req,
1160 "error %d unwrap early reply", rc);
1164 LASSERT(early_req->rq_repmsg);
1165 *req_ret = early_req;
1169 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1171 OBD_FREE_LARGE(early_buf, early_bufsz);
1173 OBD_FREE_PTR(early_req);
1178 * Used by ptlrpc client, to release a processed early reply \a early_req.
1180 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1182 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1184 LASSERT(early_req->rq_repbuf);
1185 LASSERT(early_req->rq_repdata);
1186 LASSERT(early_req->rq_repmsg);
1188 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1189 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1190 OBD_FREE_PTR(early_req);
1193 /**************************************************
1195 **************************************************/
1198 * "fixed" sec (e.g. null) use sec_id < 0
1200 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1202 int sptlrpc_get_next_secid(void)
1204 return atomic_inc_return(&sptlrpc_sec_id);
1206 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1208 /**************************************************
1209 * client side high-level security APIs *
1210 **************************************************/
1212 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1213 int grace, int force)
1215 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1217 LASSERT(policy->sp_cops);
1218 LASSERT(policy->sp_cops->flush_ctx_cache);
1220 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1223 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1225 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1227 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1228 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1229 LASSERT(policy->sp_cops->destroy_sec);
1231 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1233 policy->sp_cops->destroy_sec(sec);
1234 sptlrpc_policy_put(policy);
1237 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1239 sec_cop_destroy_sec(sec);
1241 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1243 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1245 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1247 if (sec->ps_policy->sp_cops->kill_sec) {
1248 sec->ps_policy->sp_cops->kill_sec(sec);
1250 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1254 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1257 atomic_inc(&sec->ps_refcount);
1261 EXPORT_SYMBOL(sptlrpc_sec_get);
1263 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1266 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1268 if (atomic_dec_and_test(&sec->ps_refcount)) {
1269 sptlrpc_gc_del_sec(sec);
1270 sec_cop_destroy_sec(sec);
1274 EXPORT_SYMBOL(sptlrpc_sec_put);
1277 * policy module is responsible for taking refrence of import
1280 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1281 struct ptlrpc_svc_ctx *svc_ctx,
1282 struct sptlrpc_flavor *sf,
1283 enum lustre_sec_part sp)
1285 struct ptlrpc_sec_policy *policy;
1286 struct ptlrpc_sec *sec;
1291 LASSERT(imp->imp_dlm_fake == 1);
1293 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1294 imp->imp_obd->obd_type->typ_name,
1295 imp->imp_obd->obd_name,
1296 sptlrpc_flavor2name(sf, str, sizeof(str)));
1298 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1299 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1301 LASSERT(imp->imp_dlm_fake == 0);
1303 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1304 imp->imp_obd->obd_type->typ_name,
1305 imp->imp_obd->obd_name,
1306 sptlrpc_flavor2name(sf, str, sizeof(str)));
1308 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1310 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1315 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1317 atomic_inc(&sec->ps_refcount);
1321 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1322 sptlrpc_gc_add_sec(sec);
1324 sptlrpc_policy_put(policy);
1330 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1332 struct ptlrpc_sec *sec;
1334 spin_lock(&imp->imp_lock);
1335 sec = sptlrpc_sec_get(imp->imp_sec);
1336 spin_unlock(&imp->imp_lock);
1340 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1342 static void sptlrpc_import_sec_install(struct obd_import *imp,
1343 struct ptlrpc_sec *sec)
1345 struct ptlrpc_sec *old_sec;
1347 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1349 spin_lock(&imp->imp_lock);
1350 old_sec = imp->imp_sec;
1352 spin_unlock(&imp->imp_lock);
1355 sptlrpc_sec_kill(old_sec);
1357 /* balance the ref taken by this import */
1358 sptlrpc_sec_put(old_sec);
1363 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1365 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1369 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1374 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1375 struct ptlrpc_sec *sec,
1376 struct sptlrpc_flavor *sf)
1378 char str1[32], str2[32];
1380 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1381 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1382 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1383 str1, sizeof(str1)),
1384 sptlrpc_secflags2str(sf->sf_flags,
1385 str2, sizeof(str2)));
1387 spin_lock(&sec->ps_lock);
1388 flavor_copy(&sec->ps_flvr, sf);
1389 spin_unlock(&sec->ps_lock);
1393 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1394 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1396 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1397 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1399 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1400 struct ptlrpc_svc_ctx *svc_ctx,
1401 struct sptlrpc_flavor *flvr)
1403 struct ptlrpc_connection *conn;
1404 struct sptlrpc_flavor sf;
1405 struct ptlrpc_sec *sec, *newsec;
1406 enum lustre_sec_part sp;
1416 conn = imp->imp_connection;
1418 if (svc_ctx == NULL) {
1419 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1421 * normal import, determine flavor from rule set, except
1422 * for mgc the flavor is predetermined.
1424 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1425 sf = cliobd->cl_flvr_mgc;
1427 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1429 &cliobd->cl_target_uuid,
1432 sp = imp->imp_obd->u.cli.cl_sp_me;
1434 /* reverse import, determine flavor from incoming reqeust */
1437 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1438 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1439 PTLRPC_SEC_FL_ROOTONLY;
1441 sp = sptlrpc_target_sec_part(imp->imp_obd);
1444 sec = sptlrpc_import_sec_ref(imp);
1448 if (flavor_equal(&sf, &sec->ps_flvr))
1451 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1452 imp->imp_obd->obd_name,
1453 obd_uuid2str(&conn->c_remote_uuid),
1454 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1455 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1457 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1458 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1459 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1460 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1461 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1464 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1465 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1466 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1467 imp->imp_obd->obd_name,
1468 obd_uuid2str(&conn->c_remote_uuid),
1469 LNET_NIDNET(conn->c_self),
1470 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1473 mutex_lock(&imp->imp_sec_mutex);
1475 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1477 sptlrpc_import_sec_install(imp, newsec);
1479 CERROR("import %s->%s: failed to create new sec\n",
1480 imp->imp_obd->obd_name,
1481 obd_uuid2str(&conn->c_remote_uuid));
1485 mutex_unlock(&imp->imp_sec_mutex);
1487 sptlrpc_sec_put(sec);
1491 void sptlrpc_import_sec_put(struct obd_import *imp)
1494 sptlrpc_sec_kill(imp->imp_sec);
1496 sptlrpc_sec_put(imp->imp_sec);
1497 imp->imp_sec = NULL;
1501 static void import_flush_ctx_common(struct obd_import *imp,
1502 uid_t uid, int grace, int force)
1504 struct ptlrpc_sec *sec;
1509 sec = sptlrpc_import_sec_ref(imp);
1513 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1514 sptlrpc_sec_put(sec);
1517 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1519 /* it's important to use grace mode, see explain in
1520 * sptlrpc_req_refresh_ctx() */
1521 import_flush_ctx_common(imp, 0, 1, 1);
1524 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1526 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1529 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1531 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1533 import_flush_ctx_common(imp, -1, 1, 1);
1535 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1538 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1539 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1541 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1543 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1544 struct ptlrpc_sec_policy *policy;
1548 LASSERT(ctx->cc_sec);
1549 LASSERT(ctx->cc_sec->ps_policy);
1550 LASSERT(req->rq_reqmsg == NULL);
1551 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1553 policy = ctx->cc_sec->ps_policy;
1554 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1556 LASSERT(req->rq_reqmsg);
1557 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1559 /* zeroing preallocated buffer */
1561 memset(req->rq_reqmsg, 0, msgsize);
1568 * Used by ptlrpc client to free request buffer of \a req. After this
1569 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1571 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1573 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1574 struct ptlrpc_sec_policy *policy;
1577 LASSERT(ctx->cc_sec);
1578 LASSERT(ctx->cc_sec->ps_policy);
1579 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1581 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1584 policy = ctx->cc_sec->ps_policy;
1585 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1586 req->rq_reqmsg = NULL;
1590 * NOTE caller must guarantee the buffer size is enough for the enlargement
1592 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1593 int segment, int newsize)
1596 int oldsize, oldmsg_size, movesize;
1598 LASSERT(segment < msg->lm_bufcount);
1599 LASSERT(msg->lm_buflens[segment] <= newsize);
1601 if (msg->lm_buflens[segment] == newsize)
1604 /* nothing to do if we are enlarging the last segment */
1605 if (segment == msg->lm_bufcount - 1) {
1606 msg->lm_buflens[segment] = newsize;
1610 oldsize = msg->lm_buflens[segment];
1612 src = lustre_msg_buf(msg, segment + 1, 0);
1613 msg->lm_buflens[segment] = newsize;
1614 dst = lustre_msg_buf(msg, segment + 1, 0);
1615 msg->lm_buflens[segment] = oldsize;
1617 /* move from segment + 1 to end segment */
1618 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1619 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1620 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1621 LASSERT(movesize >= 0);
1624 memmove(dst, src, movesize);
1626 /* note we don't clear the ares where old data live, not secret */
1628 /* finally set new segment size */
1629 msg->lm_buflens[segment] = newsize;
1631 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1634 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1635 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1636 * preserved after the enlargement. this must be called after original request
1637 * buffer being allocated.
1639 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1640 * so caller should refresh its local pointers if needed.
1642 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1643 int segment, int newsize)
1645 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1646 struct ptlrpc_sec_cops *cops;
1647 struct lustre_msg *msg = req->rq_reqmsg;
1651 LASSERT(msg->lm_bufcount > segment);
1652 LASSERT(msg->lm_buflens[segment] <= newsize);
1654 if (msg->lm_buflens[segment] == newsize)
1657 cops = ctx->cc_sec->ps_policy->sp_cops;
1658 LASSERT(cops->enlarge_reqbuf);
1659 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1661 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1664 * Used by ptlrpc client to allocate reply buffer of \a req.
1666 * \note After this, req->rq_repmsg is still not accessible.
1668 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1670 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1671 struct ptlrpc_sec_policy *policy;
1675 LASSERT(ctx->cc_sec);
1676 LASSERT(ctx->cc_sec->ps_policy);
1681 policy = ctx->cc_sec->ps_policy;
1682 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1686 * Used by ptlrpc client to free reply buffer of \a req. After this
1687 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1689 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1691 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1692 struct ptlrpc_sec_policy *policy;
1696 LASSERT(ctx->cc_sec);
1697 LASSERT(ctx->cc_sec->ps_policy);
1698 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1700 if (req->rq_repbuf == NULL)
1702 LASSERT(req->rq_repbuf_len);
1704 policy = ctx->cc_sec->ps_policy;
1705 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1706 req->rq_repmsg = NULL;
1710 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1711 struct ptlrpc_cli_ctx *ctx)
1713 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1715 if (!policy->sp_cops->install_rctx)
1717 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1720 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1721 struct ptlrpc_svc_ctx *ctx)
1723 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1725 if (!policy->sp_sops->install_rctx)
1727 return policy->sp_sops->install_rctx(imp, ctx);
1730 /****************************************
1731 * server side security *
1732 ****************************************/
1734 static int flavor_allowed(struct sptlrpc_flavor *exp,
1735 struct ptlrpc_request *req)
1737 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1739 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1742 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1743 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1744 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1745 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1751 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1754 * Given an export \a exp, check whether the flavor of incoming \a req
1755 * is allowed by the export \a exp. Main logic is about taking care of
1756 * changing configurations. Return 0 means success.
1758 int sptlrpc_target_export_check(struct obd_export *exp,
1759 struct ptlrpc_request *req)
1761 struct sptlrpc_flavor flavor;
1766 /* client side export has no imp_reverse, skip
1767 * FIXME maybe we should check flavor this as well??? */
1768 if (exp->exp_imp_reverse == NULL)
1771 /* don't care about ctx fini rpc */
1772 if (req->rq_ctx_fini)
1775 spin_lock(&exp->exp_lock);
1777 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1778 * the first req with the new flavor, then treat it as current flavor,
1779 * adapt reverse sec according to it.
1780 * note the first rpc with new flavor might not be with root ctx, in
1781 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1782 if (unlikely(exp->exp_flvr_changed) &&
1783 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1784 /* make the new flavor as "current", and old ones as
1785 * about-to-expire */
1786 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1787 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1788 flavor = exp->exp_flvr_old[1];
1789 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1790 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1791 exp->exp_flvr_old[0] = exp->exp_flvr;
1792 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1793 EXP_FLVR_UPDATE_EXPIRE;
1794 exp->exp_flvr = flavor;
1796 /* flavor change finished */
1797 exp->exp_flvr_changed = 0;
1798 LASSERT(exp->exp_flvr_adapt == 1);
1800 /* if it's gss, we only interested in root ctx init */
1801 if (req->rq_auth_gss &&
1802 !(req->rq_ctx_init &&
1803 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1804 req->rq_auth_usr_ost))) {
1805 spin_unlock(&exp->exp_lock);
1806 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1807 req->rq_auth_gss, req->rq_ctx_init,
1808 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1809 req->rq_auth_usr_ost);
1813 exp->exp_flvr_adapt = 0;
1814 spin_unlock(&exp->exp_lock);
1816 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1817 req->rq_svc_ctx, &flavor);
1820 /* if it equals to the current flavor, we accept it, but need to
1821 * dealing with reverse sec/ctx */
1822 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1823 /* most cases should return here, we only interested in
1824 * gss root ctx init */
1825 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1826 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1827 !req->rq_auth_usr_ost)) {
1828 spin_unlock(&exp->exp_lock);
1832 /* if flavor just changed, we should not proceed, just leave
1833 * it and current flavor will be discovered and replaced
1834 * shortly, and let _this_ rpc pass through */
1835 if (exp->exp_flvr_changed) {
1836 LASSERT(exp->exp_flvr_adapt);
1837 spin_unlock(&exp->exp_lock);
1841 if (exp->exp_flvr_adapt) {
1842 exp->exp_flvr_adapt = 0;
1843 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1844 exp, exp->exp_flvr.sf_rpc,
1845 exp->exp_flvr_old[0].sf_rpc,
1846 exp->exp_flvr_old[1].sf_rpc);
1847 flavor = exp->exp_flvr;
1848 spin_unlock(&exp->exp_lock);
1850 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1854 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1855 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1856 exp->exp_flvr_old[0].sf_rpc,
1857 exp->exp_flvr_old[1].sf_rpc);
1858 spin_unlock(&exp->exp_lock);
1860 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1865 if (exp->exp_flvr_expire[0]) {
1866 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1867 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1868 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1869 "middle one ("CFS_DURATION_T")\n", exp,
1870 exp->exp_flvr.sf_rpc,
1871 exp->exp_flvr_old[0].sf_rpc,
1872 exp->exp_flvr_old[1].sf_rpc,
1873 exp->exp_flvr_expire[0] -
1874 cfs_time_current_sec());
1875 spin_unlock(&exp->exp_lock);
1879 CDEBUG(D_SEC, "mark middle expired\n");
1880 exp->exp_flvr_expire[0] = 0;
1882 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1883 exp->exp_flvr.sf_rpc,
1884 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1885 req->rq_flvr.sf_rpc);
1888 /* now it doesn't match the current flavor, the only chance we can
1889 * accept it is match the old flavors which is not expired. */
1890 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1891 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1892 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1893 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1894 "oldest one ("CFS_DURATION_T")\n", exp,
1895 exp->exp_flvr.sf_rpc,
1896 exp->exp_flvr_old[0].sf_rpc,
1897 exp->exp_flvr_old[1].sf_rpc,
1898 exp->exp_flvr_expire[1] -
1899 cfs_time_current_sec());
1900 spin_unlock(&exp->exp_lock);
1904 CDEBUG(D_SEC, "mark oldest expired\n");
1905 exp->exp_flvr_expire[1] = 0;
1907 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1908 exp, exp->exp_flvr.sf_rpc,
1909 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1910 req->rq_flvr.sf_rpc);
1912 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1913 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1914 exp->exp_flvr_old[1].sf_rpc);
1917 spin_unlock(&exp->exp_lock);
1919 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with "
1920 "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1921 exp, exp->exp_obd->obd_name,
1922 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1923 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1924 req->rq_flvr.sf_rpc,
1925 exp->exp_flvr.sf_rpc,
1926 exp->exp_flvr_old[0].sf_rpc,
1927 exp->exp_flvr_expire[0] ?
1928 (unsigned long) (exp->exp_flvr_expire[0] -
1929 cfs_time_current_sec()) : 0,
1930 exp->exp_flvr_old[1].sf_rpc,
1931 exp->exp_flvr_expire[1] ?
1932 (unsigned long) (exp->exp_flvr_expire[1] -
1933 cfs_time_current_sec()) : 0);
1936 EXPORT_SYMBOL(sptlrpc_target_export_check);
1938 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1939 struct sptlrpc_rule_set *rset)
1941 struct obd_export *exp;
1942 struct sptlrpc_flavor new_flvr;
1946 spin_lock(&obd->obd_dev_lock);
1948 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1949 if (exp->exp_connection == NULL)
1952 /* note if this export had just been updated flavor
1953 * (exp_flvr_changed == 1), this will override the
1955 spin_lock(&exp->exp_lock);
1956 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1957 exp->exp_connection->c_peer.nid,
1959 if (exp->exp_flvr_changed ||
1960 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1961 exp->exp_flvr_old[1] = new_flvr;
1962 exp->exp_flvr_expire[1] = 0;
1963 exp->exp_flvr_changed = 1;
1964 exp->exp_flvr_adapt = 1;
1966 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1967 exp, sptlrpc_part2name(exp->exp_sp_peer),
1968 exp->exp_flvr.sf_rpc,
1969 exp->exp_flvr_old[1].sf_rpc);
1971 spin_unlock(&exp->exp_lock);
1974 spin_unlock(&obd->obd_dev_lock);
1976 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1978 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1980 /* peer's claim is unreliable unless gss is being used */
1981 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1984 switch (req->rq_sp_from) {
1986 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1987 DEBUG_REQ(D_ERROR, req, "faked source CLI");
1988 svc_rc = SECSVC_DROP;
1992 if (!req->rq_auth_usr_mdt) {
1993 DEBUG_REQ(D_ERROR, req, "faked source MDT");
1994 svc_rc = SECSVC_DROP;
1998 if (!req->rq_auth_usr_ost) {
1999 DEBUG_REQ(D_ERROR, req, "faked source OST");
2000 svc_rc = SECSVC_DROP;
2005 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2006 !req->rq_auth_usr_ost) {
2007 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2008 svc_rc = SECSVC_DROP;
2013 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2014 svc_rc = SECSVC_DROP;
2021 * Used by ptlrpc server, to perform transformation upon request message of
2022 * incoming \a req. This must be the first thing to do with a incoming
2023 * request in ptlrpc layer.
2025 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2026 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2027 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2028 * reply message has been prepared.
2029 * \retval SECSVC_DROP failed, this request should be dropped.
2031 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2033 struct ptlrpc_sec_policy *policy;
2034 struct lustre_msg *msg = req->rq_reqbuf;
2039 LASSERT(req->rq_reqmsg == NULL);
2040 LASSERT(req->rq_repmsg == NULL);
2041 LASSERT(req->rq_svc_ctx == NULL);
2043 req->rq_req_swab_mask = 0;
2045 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2048 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2052 CERROR("error unpacking request from %s x"LPU64"\n",
2053 libcfs_id2str(req->rq_peer), req->rq_xid);
2054 RETURN(SECSVC_DROP);
2057 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2058 req->rq_sp_from = LUSTRE_SP_ANY;
2059 req->rq_auth_uid = -1;
2060 req->rq_auth_mapped_uid = -1;
2062 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2064 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2065 RETURN(SECSVC_DROP);
2068 LASSERT(policy->sp_sops->accept);
2069 rc = policy->sp_sops->accept(req);
2070 sptlrpc_policy_put(policy);
2071 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2072 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2075 * if it's not null flavor (which means embedded packing msg),
2076 * reset the swab mask for the comming inner msg unpacking.
2078 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2079 req->rq_req_swab_mask = 0;
2081 /* sanity check for the request source */
2082 rc = sptlrpc_svc_check_from(req, rc);
2087 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2088 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2089 * a buffer of \a msglen size.
2091 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2093 struct ptlrpc_sec_policy *policy;
2094 struct ptlrpc_reply_state *rs;
2098 LASSERT(req->rq_svc_ctx);
2099 LASSERT(req->rq_svc_ctx->sc_policy);
2101 policy = req->rq_svc_ctx->sc_policy;
2102 LASSERT(policy->sp_sops->alloc_rs);
2104 rc = policy->sp_sops->alloc_rs(req, msglen);
2105 if (unlikely(rc == -ENOMEM)) {
2106 /* failed alloc, try emergency pool */
2107 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_svcpt);
2111 req->rq_reply_state = rs;
2112 rc = policy->sp_sops->alloc_rs(req, msglen);
2114 lustre_put_emerg_rs(rs);
2115 req->rq_reply_state = NULL;
2120 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2126 * Used by ptlrpc server, to perform transformation upon reply message.
2128 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2129 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2131 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2133 struct ptlrpc_sec_policy *policy;
2137 LASSERT(req->rq_svc_ctx);
2138 LASSERT(req->rq_svc_ctx->sc_policy);
2140 policy = req->rq_svc_ctx->sc_policy;
2141 LASSERT(policy->sp_sops->authorize);
2143 rc = policy->sp_sops->authorize(req);
2144 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2150 * Used by ptlrpc server, to free reply_state.
2152 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2154 struct ptlrpc_sec_policy *policy;
2155 unsigned int prealloc;
2158 LASSERT(rs->rs_svc_ctx);
2159 LASSERT(rs->rs_svc_ctx->sc_policy);
2161 policy = rs->rs_svc_ctx->sc_policy;
2162 LASSERT(policy->sp_sops->free_rs);
2164 prealloc = rs->rs_prealloc;
2165 policy->sp_sops->free_rs(rs);
2168 lustre_put_emerg_rs(rs);
2172 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2174 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2177 atomic_inc(&ctx->sc_refcount);
2180 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2182 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2187 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2188 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2189 if (ctx->sc_policy->sp_sops->free_ctx)
2190 ctx->sc_policy->sp_sops->free_ctx(ctx);
2192 req->rq_svc_ctx = NULL;
2195 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2197 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2202 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2203 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2204 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2206 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2208 /****************************************
2210 ****************************************/
2213 * Perform transformation upon bulk data pointed by \a desc. This is called
2214 * before transforming the request message.
2216 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2217 struct ptlrpc_bulk_desc *desc)
2219 struct ptlrpc_cli_ctx *ctx;
2221 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2223 if (!req->rq_pack_bulk)
2226 ctx = req->rq_cli_ctx;
2227 if (ctx->cc_ops->wrap_bulk)
2228 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2231 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2234 * This is called after unwrap the reply message.
2235 * return nob of actual plain text size received, or error code.
2237 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2238 struct ptlrpc_bulk_desc *desc,
2241 struct ptlrpc_cli_ctx *ctx;
2244 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2246 if (!req->rq_pack_bulk)
2247 return desc->bd_nob_transferred;
2249 ctx = req->rq_cli_ctx;
2250 if (ctx->cc_ops->unwrap_bulk) {
2251 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2255 return desc->bd_nob_transferred;
2257 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2260 * This is called after unwrap the reply message.
2261 * return 0 for success or error code.
2263 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2264 struct ptlrpc_bulk_desc *desc)
2266 struct ptlrpc_cli_ctx *ctx;
2269 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2271 if (!req->rq_pack_bulk)
2274 ctx = req->rq_cli_ctx;
2275 if (ctx->cc_ops->unwrap_bulk) {
2276 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2282 * if everything is going right, nob should equals to nob_transferred.
2283 * in case of privacy mode, nob_transferred needs to be adjusted.
2285 if (desc->bd_nob != desc->bd_nob_transferred) {
2286 CERROR("nob %d doesn't match transferred nob %d",
2287 desc->bd_nob, desc->bd_nob_transferred);
2293 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2296 /****************************************
2297 * user descriptor helpers *
2298 ****************************************/
2300 int sptlrpc_current_user_desc_size(void)
2304 ngroups = current_ngroups;
2306 if (ngroups > LUSTRE_MAX_GROUPS)
2307 ngroups = LUSTRE_MAX_GROUPS;
2308 return sptlrpc_user_desc_size(ngroups);
2310 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2312 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2314 struct ptlrpc_user_desc *pud;
2316 pud = lustre_msg_buf(msg, offset, 0);
2318 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2319 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2320 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2321 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2322 pud->pud_cap = cfs_curproc_cap_pack();
2323 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2326 if (pud->pud_ngroups > current_ngroups)
2327 pud->pud_ngroups = current_ngroups;
2328 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2329 pud->pud_ngroups * sizeof(__u32));
2330 task_unlock(current);
2334 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2336 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2338 struct ptlrpc_user_desc *pud;
2341 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2346 __swab32s(&pud->pud_uid);
2347 __swab32s(&pud->pud_gid);
2348 __swab32s(&pud->pud_fsuid);
2349 __swab32s(&pud->pud_fsgid);
2350 __swab32s(&pud->pud_cap);
2351 __swab32s(&pud->pud_ngroups);
2354 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2355 CERROR("%u groups is too large\n", pud->pud_ngroups);
2359 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2360 msg->lm_buflens[offset]) {
2361 CERROR("%u groups are claimed but bufsize only %u\n",
2362 pud->pud_ngroups, msg->lm_buflens[offset]);
2367 for (i = 0; i < pud->pud_ngroups; i++)
2368 __swab32s(&pud->pud_groups[i]);
2373 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2375 /****************************************
2377 ****************************************/
2379 const char * sec2target_str(struct ptlrpc_sec *sec)
2381 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2383 if (sec_is_reverse(sec))
2385 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2387 EXPORT_SYMBOL(sec2target_str);
2390 * return true if the bulk data is protected
2392 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2394 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2395 case SPTLRPC_BULK_SVC_INTG:
2396 case SPTLRPC_BULK_SVC_PRIV:
2402 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2404 /****************************************
2405 * crypto API helper/alloc blkciper *
2406 ****************************************/
2408 /****************************************
2409 * initialize/finalize *
2410 ****************************************/
2412 int sptlrpc_init(void)
2416 rwlock_init(&policy_lock);
2418 rc = sptlrpc_gc_init();
2422 rc = sptlrpc_conf_init();
2426 rc = sptlrpc_enc_pool_init();
2430 rc = sptlrpc_null_init();
2434 rc = sptlrpc_plain_init();
2438 rc = sptlrpc_lproc_init();
2445 sptlrpc_plain_fini();
2447 sptlrpc_null_fini();
2449 sptlrpc_enc_pool_fini();
2451 sptlrpc_conf_fini();
2458 void sptlrpc_fini(void)
2460 sptlrpc_lproc_fini();
2461 sptlrpc_plain_fini();
2462 sptlrpc_null_fini();
2463 sptlrpc_enc_pool_fini();
2464 sptlrpc_conf_fini();