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 <libcfs/libcfs.h>
45 #include <liblustre.h>
46 #include <libcfs/list.h>
48 #include <linux/crypto.h>
49 #include <linux/key.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_net.h>
56 #include <lustre_import.h>
57 #include <lustre_dlm.h>
58 #include <lustre_sec.h>
60 #include "ptlrpc_internal.h"
62 /***********************************************
64 ***********************************************/
66 static rwlock_t policy_lock;
67 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
71 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
73 __u16 number = policy->sp_policy;
75 LASSERT(policy->sp_name);
76 LASSERT(policy->sp_cops);
77 LASSERT(policy->sp_sops);
79 if (number >= SPTLRPC_POLICY_MAX)
82 write_lock(&policy_lock);
83 if (unlikely(policies[number])) {
84 write_unlock(&policy_lock);
87 policies[number] = policy;
88 write_unlock(&policy_lock);
90 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
93 EXPORT_SYMBOL(sptlrpc_register_policy);
95 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
97 __u16 number = policy->sp_policy;
99 LASSERT(number < SPTLRPC_POLICY_MAX);
101 write_lock(&policy_lock);
102 if (unlikely(policies[number] == NULL)) {
103 write_unlock(&policy_lock);
104 CERROR("%s: already unregistered\n", policy->sp_name);
108 LASSERT(policies[number] == policy);
109 policies[number] = NULL;
110 write_unlock(&policy_lock);
112 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
115 EXPORT_SYMBOL(sptlrpc_unregister_policy);
118 struct ptlrpc_sec_policy * sptlrpc_wireflavor2policy(__u32 flavor)
120 static DEFINE_MUTEX(load_mutex);
121 static atomic_t loaded = ATOMIC_INIT(0);
122 struct ptlrpc_sec_policy *policy;
123 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
126 if (number >= SPTLRPC_POLICY_MAX)
130 read_lock(&policy_lock);
131 policy = policies[number];
132 if (policy && !try_module_get(policy->sp_owner))
135 flag = atomic_read(&loaded);
136 read_unlock(&policy_lock);
138 if (policy != NULL || flag != 0 ||
139 number != SPTLRPC_POLICY_GSS)
142 /* try to load gss module, once */
143 mutex_lock(&load_mutex);
144 if (atomic_read(&loaded) == 0) {
145 if (request_module("ptlrpc_gss") == 0)
147 "module ptlrpc_gss loaded on demand\n");
149 CERROR("Unable to load module ptlrpc_gss\n");
151 atomic_set(&loaded, 1);
153 mutex_unlock(&load_mutex);
159 __u32 sptlrpc_name2flavor_base(const char *name)
161 if (!strcmp(name, "null"))
162 return SPTLRPC_FLVR_NULL;
163 if (!strcmp(name, "plain"))
164 return SPTLRPC_FLVR_PLAIN;
165 if (!strcmp(name, "gssnull"))
166 return SPTLRPC_FLVR_GSSNULL;
167 if (!strcmp(name, "krb5n"))
168 return SPTLRPC_FLVR_KRB5N;
169 if (!strcmp(name, "krb5a"))
170 return SPTLRPC_FLVR_KRB5A;
171 if (!strcmp(name, "krb5i"))
172 return SPTLRPC_FLVR_KRB5I;
173 if (!strcmp(name, "krb5p"))
174 return SPTLRPC_FLVR_KRB5P;
175 if (!strcmp(name, "ski"))
176 return SPTLRPC_FLVR_SKI;
177 if (!strcmp(name, "skpi"))
178 return SPTLRPC_FLVR_SKPI;
180 return SPTLRPC_FLVR_INVALID;
182 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
184 const char *sptlrpc_flavor2name_base(__u32 flvr)
186 __u32 base = SPTLRPC_FLVR_BASE(flvr);
188 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
190 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
192 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
194 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
196 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
198 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
200 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
202 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
204 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
207 CERROR("invalid wire flavor 0x%x\n", flvr);
210 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
212 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
213 char *buf, int bufsize)
215 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
216 snprintf(buf, bufsize, "hash:%s",
217 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
219 snprintf(buf, bufsize, "%s",
220 sptlrpc_flavor2name_base(sf->sf_rpc));
222 buf[bufsize - 1] = '\0';
225 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
227 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
229 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
232 * currently we don't support customized bulk specification for
233 * flavors other than plain
235 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
239 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
240 strncat(buf, bspec, bufsize);
243 buf[bufsize - 1] = '\0';
246 EXPORT_SYMBOL(sptlrpc_flavor2name);
248 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
252 if (flags & PTLRPC_SEC_FL_REVERSE)
253 strlcat(buf, "reverse,", bufsize);
254 if (flags & PTLRPC_SEC_FL_ROOTONLY)
255 strlcat(buf, "rootonly,", bufsize);
256 if (flags & PTLRPC_SEC_FL_UDESC)
257 strlcat(buf, "udesc,", bufsize);
258 if (flags & PTLRPC_SEC_FL_BULK)
259 strlcat(buf, "bulk,", bufsize);
261 strlcat(buf, "-,", bufsize);
265 EXPORT_SYMBOL(sptlrpc_secflags2str);
267 /**************************************************
268 * client context APIs *
269 **************************************************/
272 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
274 struct vfs_cred vcred;
275 int create = 1, remove_dead = 1;
278 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
280 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
281 PTLRPC_SEC_FL_ROOTONLY)) {
284 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
289 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
290 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
293 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
297 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
299 atomic_inc(&ctx->cc_refcount);
302 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
304 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
306 struct ptlrpc_sec *sec = ctx->cc_sec;
309 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
311 if (!atomic_dec_and_test(&ctx->cc_refcount))
314 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
316 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
319 * Expire the client context immediately.
321 * \pre Caller must hold at least 1 reference on the \a ctx.
323 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
325 LASSERT(ctx->cc_ops->die);
326 ctx->cc_ops->die(ctx, 0);
328 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
331 * To wake up the threads who are waiting for this client context. Called
332 * after some status change happened on \a ctx.
334 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
336 struct ptlrpc_request *req, *next;
338 spin_lock(&ctx->cc_lock);
339 cfs_list_for_each_entry_safe(req, next, &ctx->cc_req_list,
341 cfs_list_del_init(&req->rq_ctx_chain);
342 ptlrpc_client_wake_req(req);
344 spin_unlock(&ctx->cc_lock);
346 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
348 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
350 LASSERT(ctx->cc_ops);
352 if (ctx->cc_ops->display == NULL)
355 return ctx->cc_ops->display(ctx, buf, bufsize);
358 static int import_sec_check_expire(struct obd_import *imp)
362 spin_lock(&imp->imp_lock);
363 if (imp->imp_sec_expire &&
364 imp->imp_sec_expire < cfs_time_current_sec()) {
366 imp->imp_sec_expire = 0;
368 spin_unlock(&imp->imp_lock);
373 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
374 return sptlrpc_import_sec_adapt(imp, NULL, 0);
377 static int import_sec_validate_get(struct obd_import *imp,
378 struct ptlrpc_sec **sec)
382 if (unlikely(imp->imp_sec_expire)) {
383 rc = import_sec_check_expire(imp);
388 *sec = sptlrpc_import_sec_ref(imp);
390 CERROR("import %p (%s) with no sec\n",
391 imp, ptlrpc_import_state_name(imp->imp_state));
395 if (unlikely((*sec)->ps_dying)) {
396 CERROR("attempt to use dying sec %p\n", sec);
397 sptlrpc_sec_put(*sec);
405 * Given a \a req, find or allocate a appropriate context for it.
406 * \pre req->rq_cli_ctx == NULL.
408 * \retval 0 succeed, and req->rq_cli_ctx is set.
409 * \retval -ev error number, and req->rq_cli_ctx == NULL.
411 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
413 struct obd_import *imp = req->rq_import;
414 struct ptlrpc_sec *sec;
418 LASSERT(!req->rq_cli_ctx);
421 rc = import_sec_validate_get(imp, &sec);
425 req->rq_cli_ctx = get_my_ctx(sec);
427 sptlrpc_sec_put(sec);
429 if (!req->rq_cli_ctx) {
430 CERROR("req %p: fail to get context\n", req);
438 * Drop the context for \a req.
439 * \pre req->rq_cli_ctx != NULL.
440 * \post req->rq_cli_ctx == NULL.
442 * If \a sync == 0, this function should return quickly without sleep;
443 * otherwise it might trigger and wait for the whole process of sending
444 * an context-destroying rpc to server.
446 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
451 LASSERT(req->rq_cli_ctx);
453 /* request might be asked to release earlier while still
454 * in the context waiting list.
456 if (!cfs_list_empty(&req->rq_ctx_chain)) {
457 spin_lock(&req->rq_cli_ctx->cc_lock);
458 cfs_list_del_init(&req->rq_ctx_chain);
459 spin_unlock(&req->rq_cli_ctx->cc_lock);
462 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
463 req->rq_cli_ctx = NULL;
468 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
469 struct ptlrpc_cli_ctx *oldctx,
470 struct ptlrpc_cli_ctx *newctx)
472 struct sptlrpc_flavor old_flvr;
473 char *reqmsg = NULL; /* to workaround old gcc */
477 LASSERT(req->rq_reqmsg);
478 LASSERT(req->rq_reqlen);
479 LASSERT(req->rq_replen);
481 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
482 "switch sec %p(%s) -> %p(%s)\n", req,
483 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
484 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
485 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
486 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
489 old_flvr = req->rq_flvr;
491 /* save request message */
492 reqmsg_size = req->rq_reqlen;
493 if (reqmsg_size != 0) {
494 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
497 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
500 /* release old req/rep buf */
501 req->rq_cli_ctx = oldctx;
502 sptlrpc_cli_free_reqbuf(req);
503 sptlrpc_cli_free_repbuf(req);
504 req->rq_cli_ctx = newctx;
506 /* recalculate the flavor */
507 sptlrpc_req_set_flavor(req, 0);
509 /* alloc new request buffer
510 * we don't need to alloc reply buffer here, leave it to the
511 * rest procedure of ptlrpc */
512 if (reqmsg_size != 0) {
513 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
515 LASSERT(req->rq_reqmsg);
516 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
518 CWARN("failed to alloc reqbuf: %d\n", rc);
519 req->rq_flvr = old_flvr;
522 OBD_FREE_LARGE(reqmsg, reqmsg_size);
528 * If current context of \a req is dead somehow, e.g. we just switched flavor
529 * thus marked original contexts dead, we'll find a new context for it. if
530 * no switch is needed, \a req will end up with the same context.
532 * \note a request must have a context, to keep other parts of code happy.
533 * In any case of failure during the switching, we must restore the old one.
535 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
537 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
538 struct ptlrpc_cli_ctx *newctx;
544 sptlrpc_cli_ctx_get(oldctx);
545 sptlrpc_req_put_ctx(req, 0);
547 rc = sptlrpc_req_get_ctx(req);
549 LASSERT(!req->rq_cli_ctx);
551 /* restore old ctx */
552 req->rq_cli_ctx = oldctx;
556 newctx = req->rq_cli_ctx;
559 if (unlikely(newctx == oldctx &&
560 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
562 * still get the old dead ctx, usually means system too busy
565 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
566 newctx, newctx->cc_flags);
568 schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,
572 * it's possible newctx == oldctx if we're switching
573 * subflavor with the same sec.
575 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
577 /* restore old ctx */
578 sptlrpc_req_put_ctx(req, 0);
579 req->rq_cli_ctx = oldctx;
583 LASSERT(req->rq_cli_ctx == newctx);
586 sptlrpc_cli_ctx_put(oldctx, 1);
589 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
592 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
594 if (cli_ctx_is_refreshed(ctx))
600 int ctx_refresh_timeout(void *data)
602 struct ptlrpc_request *req = data;
605 /* conn_cnt is needed in expire_one_request */
606 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
608 rc = ptlrpc_expire_one_request(req, 1);
609 /* if we started recovery, we should mark this ctx dead; otherwise
610 * in case of lgssd died nobody would retire this ctx, following
611 * connecting will still find the same ctx thus cause deadlock.
612 * there's an assumption that expire time of the request should be
613 * later than the context refresh expire time.
616 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
621 void ctx_refresh_interrupt(void *data)
623 struct ptlrpc_request *req = data;
625 spin_lock(&req->rq_lock);
627 spin_unlock(&req->rq_lock);
631 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
633 spin_lock(&ctx->cc_lock);
634 if (!cfs_list_empty(&req->rq_ctx_chain))
635 cfs_list_del_init(&req->rq_ctx_chain);
636 spin_unlock(&ctx->cc_lock);
640 * To refresh the context of \req, if it's not up-to-date.
643 * - = 0: wait until success or fatal error occur
644 * - > 0: timeout value (in seconds)
646 * The status of the context could be subject to be changed by other threads
647 * at any time. We allow this race, but once we return with 0, the caller will
648 * suppose it's uptodated and keep using it until the owning rpc is done.
650 * \retval 0 only if the context is uptodated.
651 * \retval -ev error number.
653 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
655 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
656 struct ptlrpc_sec *sec;
657 struct l_wait_info lwi;
663 if (req->rq_ctx_init || req->rq_ctx_fini)
667 * during the process a request's context might change type even
668 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
672 rc = import_sec_validate_get(req->rq_import, &sec);
676 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
677 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
678 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
679 req_off_ctx_list(req, ctx);
680 sptlrpc_req_replace_dead_ctx(req);
681 ctx = req->rq_cli_ctx;
683 sptlrpc_sec_put(sec);
685 if (cli_ctx_is_eternal(ctx))
688 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
689 LASSERT(ctx->cc_ops->refresh);
690 ctx->cc_ops->refresh(ctx);
692 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
694 LASSERT(ctx->cc_ops->validate);
695 if (ctx->cc_ops->validate(ctx) == 0) {
696 req_off_ctx_list(req, ctx);
700 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
701 spin_lock(&req->rq_lock);
703 spin_unlock(&req->rq_lock);
704 req_off_ctx_list(req, ctx);
709 * There's a subtle issue for resending RPCs, suppose following
711 * 1. the request was sent to server.
712 * 2. recovery was kicked start, after finished the request was
714 * 3. resend the request.
715 * 4. old reply from server received, we accept and verify the reply.
716 * this has to be success, otherwise the error will be aware
718 * 5. new reply from server received, dropped by LNet.
720 * Note the xid of old & new request is the same. We can't simply
721 * change xid for the resent request because the server replies on
722 * it for reply reconstruction.
724 * Commonly the original context should be uptodate because we
725 * have a expiry nice time; server will keep its context because
726 * we at least hold a ref of old context which prevent context
727 * destroying RPC being sent. So server still can accept the request
728 * and finish the RPC. But if that's not the case:
729 * 1. If server side context has been trimmed, a NO_CONTEXT will
730 * be returned, gss_cli_ctx_verify/unseal will switch to new
732 * 2. Current context never be refreshed, then we are fine: we
733 * never really send request with old context before.
735 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
736 unlikely(req->rq_reqmsg) &&
737 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
738 req_off_ctx_list(req, ctx);
742 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
743 req_off_ctx_list(req, ctx);
745 * don't switch ctx if import was deactivated
747 if (req->rq_import->imp_deactive) {
748 spin_lock(&req->rq_lock);
750 spin_unlock(&req->rq_lock);
754 rc = sptlrpc_req_replace_dead_ctx(req);
756 LASSERT(ctx == req->rq_cli_ctx);
757 CERROR("req %p: failed to replace dead ctx %p: %d\n",
759 spin_lock(&req->rq_lock);
761 spin_unlock(&req->rq_lock);
765 ctx = req->rq_cli_ctx;
770 * Now we're sure this context is during upcall, add myself into
773 spin_lock(&ctx->cc_lock);
774 if (cfs_list_empty(&req->rq_ctx_chain))
775 cfs_list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
776 spin_unlock(&ctx->cc_lock);
779 RETURN(-EWOULDBLOCK);
781 /* Clear any flags that may be present from previous sends */
782 LASSERT(req->rq_receiving_reply == 0);
783 spin_lock(&req->rq_lock);
785 req->rq_timedout = 0;
788 spin_unlock(&req->rq_lock);
790 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
791 ctx_refresh_interrupt, req);
792 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
795 * following cases could lead us here:
796 * - successfully refreshed;
798 * - timedout, and we don't want recover from the failure;
799 * - timedout, and waked up upon recovery finished;
800 * - someone else mark this ctx dead by force;
801 * - someone invalidate the req and call ptlrpc_client_wake_req(),
802 * e.g. ptlrpc_abort_inflight();
804 if (!cli_ctx_is_refreshed(ctx)) {
805 /* timed out or interruptted */
806 req_off_ctx_list(req, ctx);
816 * Initialize flavor settings for \a req, according to \a opcode.
818 * \note this could be called in two situations:
819 * - new request from ptlrpc_pre_req(), with proper @opcode
820 * - old request which changed ctx in the middle, with @opcode == 0
822 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
824 struct ptlrpc_sec *sec;
826 LASSERT(req->rq_import);
827 LASSERT(req->rq_cli_ctx);
828 LASSERT(req->rq_cli_ctx->cc_sec);
829 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
831 /* special security flags accoding to opcode */
835 case MGS_CONFIG_READ:
837 req->rq_bulk_read = 1;
841 req->rq_bulk_write = 1;
844 req->rq_ctx_init = 1;
847 req->rq_ctx_fini = 1;
850 /* init/fini rpc won't be resend, so can't be here */
851 LASSERT(req->rq_ctx_init == 0);
852 LASSERT(req->rq_ctx_fini == 0);
854 /* cleanup flags, which should be recalculated */
855 req->rq_pack_udesc = 0;
856 req->rq_pack_bulk = 0;
860 sec = req->rq_cli_ctx->cc_sec;
862 spin_lock(&sec->ps_lock);
863 req->rq_flvr = sec->ps_flvr;
864 spin_unlock(&sec->ps_lock);
866 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
868 if (unlikely(req->rq_ctx_init))
869 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
870 else if (unlikely(req->rq_ctx_fini))
871 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
873 /* user descriptor flag, null security can't do it anyway */
874 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
875 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
876 req->rq_pack_udesc = 1;
878 /* bulk security flag */
879 if ((req->rq_bulk_read || req->rq_bulk_write) &&
880 sptlrpc_flavor_has_bulk(&req->rq_flvr))
881 req->rq_pack_bulk = 1;
884 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
886 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
889 LASSERT(req->rq_clrbuf);
890 if (req->rq_pool || !req->rq_reqbuf)
893 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
894 req->rq_reqbuf = NULL;
895 req->rq_reqbuf_len = 0;
899 * Given an import \a imp, check whether current user has a valid context
900 * or not. We may create a new context and try to refresh it, and try
901 * repeatedly try in case of non-fatal errors. Return 0 means success.
903 int sptlrpc_import_check_ctx(struct obd_import *imp)
905 struct ptlrpc_sec *sec;
906 struct ptlrpc_cli_ctx *ctx;
907 struct ptlrpc_request *req = NULL;
913 sec = sptlrpc_import_sec_ref(imp);
914 ctx = get_my_ctx(sec);
915 sptlrpc_sec_put(sec);
920 if (cli_ctx_is_eternal(ctx) ||
921 ctx->cc_ops->validate(ctx) == 0) {
922 sptlrpc_cli_ctx_put(ctx, 1);
926 if (cli_ctx_is_error(ctx)) {
927 sptlrpc_cli_ctx_put(ctx, 1);
931 req = ptlrpc_request_cache_alloc(GFP_NOFS);
935 spin_lock_init(&req->rq_lock);
936 atomic_set(&req->rq_refcount, 10000);
937 CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
938 init_waitqueue_head(&req->rq_reply_waitq);
939 init_waitqueue_head(&req->rq_set_waitq);
940 req->rq_import = imp;
941 req->rq_flvr = sec->ps_flvr;
942 req->rq_cli_ctx = ctx;
944 rc = sptlrpc_req_refresh_ctx(req, 0);
945 LASSERT(cfs_list_empty(&req->rq_ctx_chain));
946 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
947 ptlrpc_request_cache_free(req);
953 * Used by ptlrpc client, to perform the pre-defined security transformation
954 * upon the request message of \a req. After this function called,
955 * req->rq_reqmsg is still accessible as clear text.
957 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
959 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
964 LASSERT(ctx->cc_sec);
965 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
967 /* we wrap bulk request here because now we can be sure
968 * the context is uptodate.
971 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
976 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
977 case SPTLRPC_SVC_NULL:
978 case SPTLRPC_SVC_AUTH:
979 case SPTLRPC_SVC_INTG:
980 LASSERT(ctx->cc_ops->sign);
981 rc = ctx->cc_ops->sign(ctx, req);
983 case SPTLRPC_SVC_PRIV:
984 LASSERT(ctx->cc_ops->seal);
985 rc = ctx->cc_ops->seal(ctx, req);
992 LASSERT(req->rq_reqdata_len);
993 LASSERT(req->rq_reqdata_len % 8 == 0);
994 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1000 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1002 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1007 LASSERT(ctx->cc_sec);
1008 LASSERT(req->rq_repbuf);
1009 LASSERT(req->rq_repdata);
1010 LASSERT(req->rq_repmsg == NULL);
1012 req->rq_rep_swab_mask = 0;
1014 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1017 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1021 CERROR("failed unpack reply: x"LPU64"\n", req->rq_xid);
1025 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1026 CERROR("replied data length %d too small\n",
1027 req->rq_repdata_len);
1031 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1032 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1033 CERROR("reply policy %u doesn't match request policy %u\n",
1034 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1035 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1039 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1040 case SPTLRPC_SVC_NULL:
1041 case SPTLRPC_SVC_AUTH:
1042 case SPTLRPC_SVC_INTG:
1043 LASSERT(ctx->cc_ops->verify);
1044 rc = ctx->cc_ops->verify(ctx, req);
1046 case SPTLRPC_SVC_PRIV:
1047 LASSERT(ctx->cc_ops->unseal);
1048 rc = ctx->cc_ops->unseal(ctx, req);
1053 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1055 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1057 req->rq_rep_swab_mask = 0;
1062 * Used by ptlrpc client, to perform security transformation upon the reply
1063 * message of \a req. After return successfully, req->rq_repmsg points to
1064 * the reply message in clear text.
1066 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1069 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1071 LASSERT(req->rq_repbuf);
1072 LASSERT(req->rq_repdata == NULL);
1073 LASSERT(req->rq_repmsg == NULL);
1074 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1076 if (req->rq_reply_off == 0 &&
1077 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1078 CERROR("real reply with offset 0\n");
1082 if (req->rq_reply_off % 8 != 0) {
1083 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1087 req->rq_repdata = (struct lustre_msg *)
1088 (req->rq_repbuf + req->rq_reply_off);
1089 req->rq_repdata_len = req->rq_nob_received;
1091 return do_cli_unwrap_reply(req);
1095 * Used by ptlrpc client, to perform security transformation upon the early
1096 * reply message of \a req. We expect the rq_reply_off is 0, and
1097 * rq_nob_received is the early reply size.
1099 * Because the receive buffer might be still posted, the reply data might be
1100 * changed at any time, no matter we're holding rq_lock or not. For this reason
1101 * we allocate a separate ptlrpc_request and reply buffer for early reply
1104 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1105 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1106 * \a *req_ret to release it.
1107 * \retval -ev error number, and \a req_ret will not be set.
1109 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1110 struct ptlrpc_request **req_ret)
1112 struct ptlrpc_request *early_req;
1114 int early_bufsz, early_size;
1118 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1119 if (early_req == NULL)
1122 early_size = req->rq_nob_received;
1123 early_bufsz = size_roundup_power2(early_size);
1124 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1125 if (early_buf == NULL)
1126 GOTO(err_req, rc = -ENOMEM);
1128 /* sanity checkings and copy data out, do it inside spinlock */
1129 spin_lock(&req->rq_lock);
1131 if (req->rq_replied) {
1132 spin_unlock(&req->rq_lock);
1133 GOTO(err_buf, rc = -EALREADY);
1136 LASSERT(req->rq_repbuf);
1137 LASSERT(req->rq_repdata == NULL);
1138 LASSERT(req->rq_repmsg == NULL);
1140 if (req->rq_reply_off != 0) {
1141 CERROR("early reply with offset %u\n", req->rq_reply_off);
1142 spin_unlock(&req->rq_lock);
1143 GOTO(err_buf, rc = -EPROTO);
1146 if (req->rq_nob_received != early_size) {
1147 /* even another early arrived the size should be the same */
1148 CERROR("data size has changed from %u to %u\n",
1149 early_size, req->rq_nob_received);
1150 spin_unlock(&req->rq_lock);
1151 GOTO(err_buf, rc = -EINVAL);
1154 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1155 CERROR("early reply length %d too small\n",
1156 req->rq_nob_received);
1157 spin_unlock(&req->rq_lock);
1158 GOTO(err_buf, rc = -EALREADY);
1161 memcpy(early_buf, req->rq_repbuf, early_size);
1162 spin_unlock(&req->rq_lock);
1164 spin_lock_init(&early_req->rq_lock);
1165 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1166 early_req->rq_flvr = req->rq_flvr;
1167 early_req->rq_repbuf = early_buf;
1168 early_req->rq_repbuf_len = early_bufsz;
1169 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1170 early_req->rq_repdata_len = early_size;
1171 early_req->rq_early = 1;
1172 early_req->rq_reqmsg = req->rq_reqmsg;
1174 rc = do_cli_unwrap_reply(early_req);
1176 DEBUG_REQ(D_ADAPTTO, early_req,
1177 "error %d unwrap early reply", rc);
1181 LASSERT(early_req->rq_repmsg);
1182 *req_ret = early_req;
1186 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1188 OBD_FREE_LARGE(early_buf, early_bufsz);
1190 ptlrpc_request_cache_free(early_req);
1195 * Used by ptlrpc client, to release a processed early reply \a early_req.
1197 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1199 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1201 LASSERT(early_req->rq_repbuf);
1202 LASSERT(early_req->rq_repdata);
1203 LASSERT(early_req->rq_repmsg);
1205 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1206 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1207 ptlrpc_request_cache_free(early_req);
1210 /**************************************************
1212 **************************************************/
1215 * "fixed" sec (e.g. null) use sec_id < 0
1217 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1219 int sptlrpc_get_next_secid(void)
1221 return atomic_inc_return(&sptlrpc_sec_id);
1223 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1225 /**************************************************
1226 * client side high-level security APIs *
1227 **************************************************/
1229 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1230 int grace, int force)
1232 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1234 LASSERT(policy->sp_cops);
1235 LASSERT(policy->sp_cops->flush_ctx_cache);
1237 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1240 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1242 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1244 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1245 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1246 LASSERT(policy->sp_cops->destroy_sec);
1248 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1250 policy->sp_cops->destroy_sec(sec);
1251 sptlrpc_policy_put(policy);
1254 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1256 sec_cop_destroy_sec(sec);
1258 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1260 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1262 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1264 if (sec->ps_policy->sp_cops->kill_sec) {
1265 sec->ps_policy->sp_cops->kill_sec(sec);
1267 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1271 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1274 atomic_inc(&sec->ps_refcount);
1278 EXPORT_SYMBOL(sptlrpc_sec_get);
1280 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1283 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1285 if (atomic_dec_and_test(&sec->ps_refcount)) {
1286 sptlrpc_gc_del_sec(sec);
1287 sec_cop_destroy_sec(sec);
1291 EXPORT_SYMBOL(sptlrpc_sec_put);
1294 * policy module is responsible for taking refrence of import
1297 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1298 struct ptlrpc_svc_ctx *svc_ctx,
1299 struct sptlrpc_flavor *sf,
1300 enum lustre_sec_part sp)
1302 struct ptlrpc_sec_policy *policy;
1303 struct ptlrpc_sec *sec;
1308 LASSERT(imp->imp_dlm_fake == 1);
1310 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1311 imp->imp_obd->obd_type->typ_name,
1312 imp->imp_obd->obd_name,
1313 sptlrpc_flavor2name(sf, str, sizeof(str)));
1315 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1316 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1318 LASSERT(imp->imp_dlm_fake == 0);
1320 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1321 imp->imp_obd->obd_type->typ_name,
1322 imp->imp_obd->obd_name,
1323 sptlrpc_flavor2name(sf, str, sizeof(str)));
1325 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1327 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1332 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1334 atomic_inc(&sec->ps_refcount);
1338 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1339 sptlrpc_gc_add_sec(sec);
1341 sptlrpc_policy_put(policy);
1347 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1349 struct ptlrpc_sec *sec;
1351 spin_lock(&imp->imp_lock);
1352 sec = sptlrpc_sec_get(imp->imp_sec);
1353 spin_unlock(&imp->imp_lock);
1357 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1359 static void sptlrpc_import_sec_install(struct obd_import *imp,
1360 struct ptlrpc_sec *sec)
1362 struct ptlrpc_sec *old_sec;
1364 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1366 spin_lock(&imp->imp_lock);
1367 old_sec = imp->imp_sec;
1369 spin_unlock(&imp->imp_lock);
1372 sptlrpc_sec_kill(old_sec);
1374 /* balance the ref taken by this import */
1375 sptlrpc_sec_put(old_sec);
1380 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1382 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1386 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1391 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1392 struct ptlrpc_sec *sec,
1393 struct sptlrpc_flavor *sf)
1395 char str1[32], str2[32];
1397 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1398 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1399 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1400 str1, sizeof(str1)),
1401 sptlrpc_secflags2str(sf->sf_flags,
1402 str2, sizeof(str2)));
1404 spin_lock(&sec->ps_lock);
1405 flavor_copy(&sec->ps_flvr, sf);
1406 spin_unlock(&sec->ps_lock);
1410 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1411 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1413 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1414 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1416 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1417 struct ptlrpc_svc_ctx *svc_ctx,
1418 struct sptlrpc_flavor *flvr)
1420 struct ptlrpc_connection *conn;
1421 struct sptlrpc_flavor sf;
1422 struct ptlrpc_sec *sec, *newsec;
1423 enum lustre_sec_part sp;
1433 conn = imp->imp_connection;
1435 if (svc_ctx == NULL) {
1436 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1438 * normal import, determine flavor from rule set, except
1439 * for mgc the flavor is predetermined.
1441 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1442 sf = cliobd->cl_flvr_mgc;
1444 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1446 &cliobd->cl_target_uuid,
1449 sp = imp->imp_obd->u.cli.cl_sp_me;
1451 /* reverse import, determine flavor from incoming reqeust */
1454 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1455 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1456 PTLRPC_SEC_FL_ROOTONLY;
1458 sp = sptlrpc_target_sec_part(imp->imp_obd);
1461 sec = sptlrpc_import_sec_ref(imp);
1465 if (flavor_equal(&sf, &sec->ps_flvr))
1468 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1469 imp->imp_obd->obd_name,
1470 obd_uuid2str(&conn->c_remote_uuid),
1471 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1472 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1474 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1475 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1476 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1477 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1478 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1481 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1482 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1483 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1484 imp->imp_obd->obd_name,
1485 obd_uuid2str(&conn->c_remote_uuid),
1486 LNET_NIDNET(conn->c_self),
1487 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1490 mutex_lock(&imp->imp_sec_mutex);
1492 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1494 sptlrpc_import_sec_install(imp, newsec);
1496 CERROR("import %s->%s: failed to create new sec\n",
1497 imp->imp_obd->obd_name,
1498 obd_uuid2str(&conn->c_remote_uuid));
1502 mutex_unlock(&imp->imp_sec_mutex);
1504 sptlrpc_sec_put(sec);
1508 void sptlrpc_import_sec_put(struct obd_import *imp)
1511 sptlrpc_sec_kill(imp->imp_sec);
1513 sptlrpc_sec_put(imp->imp_sec);
1514 imp->imp_sec = NULL;
1518 static void import_flush_ctx_common(struct obd_import *imp,
1519 uid_t uid, int grace, int force)
1521 struct ptlrpc_sec *sec;
1526 sec = sptlrpc_import_sec_ref(imp);
1530 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1531 sptlrpc_sec_put(sec);
1534 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1536 /* it's important to use grace mode, see explain in
1537 * sptlrpc_req_refresh_ctx() */
1538 import_flush_ctx_common(imp, 0, 1, 1);
1541 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1543 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1546 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1548 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1550 import_flush_ctx_common(imp, -1, 1, 1);
1552 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1555 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1556 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1558 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1560 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1561 struct ptlrpc_sec_policy *policy;
1565 LASSERT(ctx->cc_sec);
1566 LASSERT(ctx->cc_sec->ps_policy);
1567 LASSERT(req->rq_reqmsg == NULL);
1568 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1570 policy = ctx->cc_sec->ps_policy;
1571 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1573 LASSERT(req->rq_reqmsg);
1574 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1576 /* zeroing preallocated buffer */
1578 memset(req->rq_reqmsg, 0, msgsize);
1585 * Used by ptlrpc client to free request buffer of \a req. After this
1586 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1588 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1590 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1591 struct ptlrpc_sec_policy *policy;
1594 LASSERT(ctx->cc_sec);
1595 LASSERT(ctx->cc_sec->ps_policy);
1596 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1598 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1601 policy = ctx->cc_sec->ps_policy;
1602 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1603 req->rq_reqmsg = NULL;
1607 * NOTE caller must guarantee the buffer size is enough for the enlargement
1609 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1610 int segment, int newsize)
1613 int oldsize, oldmsg_size, movesize;
1615 LASSERT(segment < msg->lm_bufcount);
1616 LASSERT(msg->lm_buflens[segment] <= newsize);
1618 if (msg->lm_buflens[segment] == newsize)
1621 /* nothing to do if we are enlarging the last segment */
1622 if (segment == msg->lm_bufcount - 1) {
1623 msg->lm_buflens[segment] = newsize;
1627 oldsize = msg->lm_buflens[segment];
1629 src = lustre_msg_buf(msg, segment + 1, 0);
1630 msg->lm_buflens[segment] = newsize;
1631 dst = lustre_msg_buf(msg, segment + 1, 0);
1632 msg->lm_buflens[segment] = oldsize;
1634 /* move from segment + 1 to end segment */
1635 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1636 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1637 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1638 LASSERT(movesize >= 0);
1641 memmove(dst, src, movesize);
1643 /* note we don't clear the ares where old data live, not secret */
1645 /* finally set new segment size */
1646 msg->lm_buflens[segment] = newsize;
1648 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1651 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1652 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1653 * preserved after the enlargement. this must be called after original request
1654 * buffer being allocated.
1656 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1657 * so caller should refresh its local pointers if needed.
1659 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1660 int segment, int newsize)
1662 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1663 struct ptlrpc_sec_cops *cops;
1664 struct lustre_msg *msg = req->rq_reqmsg;
1668 LASSERT(msg->lm_bufcount > segment);
1669 LASSERT(msg->lm_buflens[segment] <= newsize);
1671 if (msg->lm_buflens[segment] == newsize)
1674 cops = ctx->cc_sec->ps_policy->sp_cops;
1675 LASSERT(cops->enlarge_reqbuf);
1676 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1678 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1681 * Used by ptlrpc client to allocate reply buffer of \a req.
1683 * \note After this, req->rq_repmsg is still not accessible.
1685 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1687 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1688 struct ptlrpc_sec_policy *policy;
1692 LASSERT(ctx->cc_sec);
1693 LASSERT(ctx->cc_sec->ps_policy);
1698 policy = ctx->cc_sec->ps_policy;
1699 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1703 * Used by ptlrpc client to free reply buffer of \a req. After this
1704 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1706 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1708 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1709 struct ptlrpc_sec_policy *policy;
1713 LASSERT(ctx->cc_sec);
1714 LASSERT(ctx->cc_sec->ps_policy);
1715 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1717 if (req->rq_repbuf == NULL)
1719 LASSERT(req->rq_repbuf_len);
1721 policy = ctx->cc_sec->ps_policy;
1722 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1723 req->rq_repmsg = NULL;
1727 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1728 struct ptlrpc_cli_ctx *ctx)
1730 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1732 if (!policy->sp_cops->install_rctx)
1734 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1737 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1738 struct ptlrpc_svc_ctx *ctx)
1740 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1742 if (!policy->sp_sops->install_rctx)
1744 return policy->sp_sops->install_rctx(imp, ctx);
1747 /****************************************
1748 * server side security *
1749 ****************************************/
1751 static int flavor_allowed(struct sptlrpc_flavor *exp,
1752 struct ptlrpc_request *req)
1754 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1756 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1759 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1760 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1761 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1762 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1768 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1771 * Given an export \a exp, check whether the flavor of incoming \a req
1772 * is allowed by the export \a exp. Main logic is about taking care of
1773 * changing configurations. Return 0 means success.
1775 int sptlrpc_target_export_check(struct obd_export *exp,
1776 struct ptlrpc_request *req)
1778 struct sptlrpc_flavor flavor;
1783 /* client side export has no imp_reverse, skip
1784 * FIXME maybe we should check flavor this as well??? */
1785 if (exp->exp_imp_reverse == NULL)
1788 /* don't care about ctx fini rpc */
1789 if (req->rq_ctx_fini)
1792 spin_lock(&exp->exp_lock);
1794 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1795 * the first req with the new flavor, then treat it as current flavor,
1796 * adapt reverse sec according to it.
1797 * note the first rpc with new flavor might not be with root ctx, in
1798 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1799 if (unlikely(exp->exp_flvr_changed) &&
1800 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1801 /* make the new flavor as "current", and old ones as
1802 * about-to-expire */
1803 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1804 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1805 flavor = exp->exp_flvr_old[1];
1806 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1807 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1808 exp->exp_flvr_old[0] = exp->exp_flvr;
1809 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1810 EXP_FLVR_UPDATE_EXPIRE;
1811 exp->exp_flvr = flavor;
1813 /* flavor change finished */
1814 exp->exp_flvr_changed = 0;
1815 LASSERT(exp->exp_flvr_adapt == 1);
1817 /* if it's gss, we only interested in root ctx init */
1818 if (req->rq_auth_gss &&
1819 !(req->rq_ctx_init &&
1820 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1821 req->rq_auth_usr_ost))) {
1822 spin_unlock(&exp->exp_lock);
1823 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1824 req->rq_auth_gss, req->rq_ctx_init,
1825 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1826 req->rq_auth_usr_ost);
1830 exp->exp_flvr_adapt = 0;
1831 spin_unlock(&exp->exp_lock);
1833 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1834 req->rq_svc_ctx, &flavor);
1837 /* if it equals to the current flavor, we accept it, but need to
1838 * dealing with reverse sec/ctx */
1839 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1840 /* most cases should return here, we only interested in
1841 * gss root ctx init */
1842 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1843 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1844 !req->rq_auth_usr_ost)) {
1845 spin_unlock(&exp->exp_lock);
1849 /* if flavor just changed, we should not proceed, just leave
1850 * it and current flavor will be discovered and replaced
1851 * shortly, and let _this_ rpc pass through */
1852 if (exp->exp_flvr_changed) {
1853 LASSERT(exp->exp_flvr_adapt);
1854 spin_unlock(&exp->exp_lock);
1858 if (exp->exp_flvr_adapt) {
1859 exp->exp_flvr_adapt = 0;
1860 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1861 exp, exp->exp_flvr.sf_rpc,
1862 exp->exp_flvr_old[0].sf_rpc,
1863 exp->exp_flvr_old[1].sf_rpc);
1864 flavor = exp->exp_flvr;
1865 spin_unlock(&exp->exp_lock);
1867 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1871 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1872 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1873 exp->exp_flvr_old[0].sf_rpc,
1874 exp->exp_flvr_old[1].sf_rpc);
1875 spin_unlock(&exp->exp_lock);
1877 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1882 if (exp->exp_flvr_expire[0]) {
1883 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1884 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1885 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1886 "middle one ("CFS_DURATION_T")\n", exp,
1887 exp->exp_flvr.sf_rpc,
1888 exp->exp_flvr_old[0].sf_rpc,
1889 exp->exp_flvr_old[1].sf_rpc,
1890 exp->exp_flvr_expire[0] -
1891 cfs_time_current_sec());
1892 spin_unlock(&exp->exp_lock);
1896 CDEBUG(D_SEC, "mark middle expired\n");
1897 exp->exp_flvr_expire[0] = 0;
1899 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1900 exp->exp_flvr.sf_rpc,
1901 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1902 req->rq_flvr.sf_rpc);
1905 /* now it doesn't match the current flavor, the only chance we can
1906 * accept it is match the old flavors which is not expired. */
1907 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1908 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1909 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1910 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1911 "oldest one ("CFS_DURATION_T")\n", exp,
1912 exp->exp_flvr.sf_rpc,
1913 exp->exp_flvr_old[0].sf_rpc,
1914 exp->exp_flvr_old[1].sf_rpc,
1915 exp->exp_flvr_expire[1] -
1916 cfs_time_current_sec());
1917 spin_unlock(&exp->exp_lock);
1921 CDEBUG(D_SEC, "mark oldest expired\n");
1922 exp->exp_flvr_expire[1] = 0;
1924 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1925 exp, exp->exp_flvr.sf_rpc,
1926 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1927 req->rq_flvr.sf_rpc);
1929 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1930 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1931 exp->exp_flvr_old[1].sf_rpc);
1934 spin_unlock(&exp->exp_lock);
1936 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with "
1937 "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1938 exp, exp->exp_obd->obd_name,
1939 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1940 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1941 req->rq_flvr.sf_rpc,
1942 exp->exp_flvr.sf_rpc,
1943 exp->exp_flvr_old[0].sf_rpc,
1944 exp->exp_flvr_expire[0] ?
1945 (unsigned long) (exp->exp_flvr_expire[0] -
1946 cfs_time_current_sec()) : 0,
1947 exp->exp_flvr_old[1].sf_rpc,
1948 exp->exp_flvr_expire[1] ?
1949 (unsigned long) (exp->exp_flvr_expire[1] -
1950 cfs_time_current_sec()) : 0);
1953 EXPORT_SYMBOL(sptlrpc_target_export_check);
1955 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1956 struct sptlrpc_rule_set *rset)
1958 struct obd_export *exp;
1959 struct sptlrpc_flavor new_flvr;
1963 spin_lock(&obd->obd_dev_lock);
1965 cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1966 if (exp->exp_connection == NULL)
1969 /* note if this export had just been updated flavor
1970 * (exp_flvr_changed == 1), this will override the
1972 spin_lock(&exp->exp_lock);
1973 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1974 exp->exp_connection->c_peer.nid,
1976 if (exp->exp_flvr_changed ||
1977 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1978 exp->exp_flvr_old[1] = new_flvr;
1979 exp->exp_flvr_expire[1] = 0;
1980 exp->exp_flvr_changed = 1;
1981 exp->exp_flvr_adapt = 1;
1983 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1984 exp, sptlrpc_part2name(exp->exp_sp_peer),
1985 exp->exp_flvr.sf_rpc,
1986 exp->exp_flvr_old[1].sf_rpc);
1988 spin_unlock(&exp->exp_lock);
1991 spin_unlock(&obd->obd_dev_lock);
1993 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1995 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1997 /* peer's claim is unreliable unless gss is being used */
1998 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2001 switch (req->rq_sp_from) {
2003 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2004 DEBUG_REQ(D_ERROR, req, "faked source CLI");
2005 svc_rc = SECSVC_DROP;
2009 if (!req->rq_auth_usr_mdt) {
2010 DEBUG_REQ(D_ERROR, req, "faked source MDT");
2011 svc_rc = SECSVC_DROP;
2015 if (!req->rq_auth_usr_ost) {
2016 DEBUG_REQ(D_ERROR, req, "faked source OST");
2017 svc_rc = SECSVC_DROP;
2022 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2023 !req->rq_auth_usr_ost) {
2024 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2025 svc_rc = SECSVC_DROP;
2030 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2031 svc_rc = SECSVC_DROP;
2038 * Used by ptlrpc server, to perform transformation upon request message of
2039 * incoming \a req. This must be the first thing to do with a incoming
2040 * request in ptlrpc layer.
2042 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2043 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2044 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2045 * reply message has been prepared.
2046 * \retval SECSVC_DROP failed, this request should be dropped.
2048 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2050 struct ptlrpc_sec_policy *policy;
2051 struct lustre_msg *msg = req->rq_reqbuf;
2056 LASSERT(req->rq_reqmsg == NULL);
2057 LASSERT(req->rq_repmsg == NULL);
2058 LASSERT(req->rq_svc_ctx == NULL);
2060 req->rq_req_swab_mask = 0;
2062 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2065 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2069 CERROR("error unpacking request from %s x"LPU64"\n",
2070 libcfs_id2str(req->rq_peer), req->rq_xid);
2071 RETURN(SECSVC_DROP);
2074 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2075 req->rq_sp_from = LUSTRE_SP_ANY;
2076 req->rq_auth_uid = -1; /* set to INVALID_UID */
2077 req->rq_auth_mapped_uid = -1;
2079 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2081 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2082 RETURN(SECSVC_DROP);
2085 LASSERT(policy->sp_sops->accept);
2086 rc = policy->sp_sops->accept(req);
2087 sptlrpc_policy_put(policy);
2088 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2089 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2092 * if it's not null flavor (which means embedded packing msg),
2093 * reset the swab mask for the comming inner msg unpacking.
2095 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2096 req->rq_req_swab_mask = 0;
2098 /* sanity check for the request source */
2099 rc = sptlrpc_svc_check_from(req, rc);
2104 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2105 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2106 * a buffer of \a msglen size.
2108 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2110 struct ptlrpc_sec_policy *policy;
2111 struct ptlrpc_reply_state *rs;
2115 LASSERT(req->rq_svc_ctx);
2116 LASSERT(req->rq_svc_ctx->sc_policy);
2118 policy = req->rq_svc_ctx->sc_policy;
2119 LASSERT(policy->sp_sops->alloc_rs);
2121 rc = policy->sp_sops->alloc_rs(req, msglen);
2122 if (unlikely(rc == -ENOMEM)) {
2123 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2124 if (svcpt->scp_service->srv_max_reply_size <
2125 msglen + sizeof(struct ptlrpc_reply_state)) {
2126 /* Just return failure if the size is too big */
2127 CERROR("size of message is too big (%zd), %d allowed\n",
2128 msglen + sizeof(struct ptlrpc_reply_state),
2129 svcpt->scp_service->srv_max_reply_size);
2133 /* failed alloc, try emergency pool */
2134 rs = lustre_get_emerg_rs(svcpt);
2138 req->rq_reply_state = rs;
2139 rc = policy->sp_sops->alloc_rs(req, msglen);
2141 lustre_put_emerg_rs(rs);
2142 req->rq_reply_state = NULL;
2147 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2153 * Used by ptlrpc server, to perform transformation upon reply message.
2155 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2156 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2158 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2160 struct ptlrpc_sec_policy *policy;
2164 LASSERT(req->rq_svc_ctx);
2165 LASSERT(req->rq_svc_ctx->sc_policy);
2167 policy = req->rq_svc_ctx->sc_policy;
2168 LASSERT(policy->sp_sops->authorize);
2170 rc = policy->sp_sops->authorize(req);
2171 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2177 * Used by ptlrpc server, to free reply_state.
2179 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2181 struct ptlrpc_sec_policy *policy;
2182 unsigned int prealloc;
2185 LASSERT(rs->rs_svc_ctx);
2186 LASSERT(rs->rs_svc_ctx->sc_policy);
2188 policy = rs->rs_svc_ctx->sc_policy;
2189 LASSERT(policy->sp_sops->free_rs);
2191 prealloc = rs->rs_prealloc;
2192 policy->sp_sops->free_rs(rs);
2195 lustre_put_emerg_rs(rs);
2199 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2201 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2204 atomic_inc(&ctx->sc_refcount);
2207 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2209 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2214 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2215 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2216 if (ctx->sc_policy->sp_sops->free_ctx)
2217 ctx->sc_policy->sp_sops->free_ctx(ctx);
2219 req->rq_svc_ctx = NULL;
2222 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2224 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2229 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2230 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2231 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2233 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2235 /****************************************
2237 ****************************************/
2240 * Perform transformation upon bulk data pointed by \a desc. This is called
2241 * before transforming the request message.
2243 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2244 struct ptlrpc_bulk_desc *desc)
2246 struct ptlrpc_cli_ctx *ctx;
2248 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2250 if (!req->rq_pack_bulk)
2253 ctx = req->rq_cli_ctx;
2254 if (ctx->cc_ops->wrap_bulk)
2255 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2258 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2261 * This is called after unwrap the reply message.
2262 * return nob of actual plain text size received, or error code.
2264 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2265 struct ptlrpc_bulk_desc *desc,
2268 struct ptlrpc_cli_ctx *ctx;
2271 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2273 if (!req->rq_pack_bulk)
2274 return desc->bd_nob_transferred;
2276 ctx = req->rq_cli_ctx;
2277 if (ctx->cc_ops->unwrap_bulk) {
2278 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2282 return desc->bd_nob_transferred;
2284 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2287 * This is called after unwrap the reply message.
2288 * return 0 for success or error code.
2290 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2291 struct ptlrpc_bulk_desc *desc)
2293 struct ptlrpc_cli_ctx *ctx;
2296 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2298 if (!req->rq_pack_bulk)
2301 ctx = req->rq_cli_ctx;
2302 if (ctx->cc_ops->unwrap_bulk) {
2303 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2309 * if everything is going right, nob should equals to nob_transferred.
2310 * in case of privacy mode, nob_transferred needs to be adjusted.
2312 if (desc->bd_nob != desc->bd_nob_transferred) {
2313 CERROR("nob %d doesn't match transferred nob %d\n",
2314 desc->bd_nob, desc->bd_nob_transferred);
2320 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2322 #ifdef HAVE_SERVER_SUPPORT
2324 * Performe transformation upon outgoing bulk read.
2326 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2327 struct ptlrpc_bulk_desc *desc)
2329 struct ptlrpc_svc_ctx *ctx;
2331 LASSERT(req->rq_bulk_read);
2333 if (!req->rq_pack_bulk)
2336 ctx = req->rq_svc_ctx;
2337 if (ctx->sc_policy->sp_sops->wrap_bulk)
2338 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2342 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2345 * Performe transformation upon incoming bulk write.
2347 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2348 struct ptlrpc_bulk_desc *desc)
2350 struct ptlrpc_svc_ctx *ctx;
2353 LASSERT(req->rq_bulk_write);
2356 * if it's in privacy mode, transferred should >= expected; otherwise
2357 * transferred should == expected.
2359 if (desc->bd_nob_transferred < desc->bd_nob ||
2360 (desc->bd_nob_transferred > desc->bd_nob &&
2361 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2362 SPTLRPC_BULK_SVC_PRIV)) {
2363 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2364 desc->bd_nob_transferred, desc->bd_nob);
2368 if (!req->rq_pack_bulk)
2371 ctx = req->rq_svc_ctx;
2372 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2373 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2375 CERROR("error unwrap bulk: %d\n", rc);
2378 /* return 0 to allow reply be sent */
2381 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2384 * Prepare buffers for incoming bulk write.
2386 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2387 struct ptlrpc_bulk_desc *desc)
2389 struct ptlrpc_svc_ctx *ctx;
2391 LASSERT(req->rq_bulk_write);
2393 if (!req->rq_pack_bulk)
2396 ctx = req->rq_svc_ctx;
2397 if (ctx->sc_policy->sp_sops->prep_bulk)
2398 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2402 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2404 #endif /* HAVE_SERVER_SUPPORT */
2406 /****************************************
2407 * user descriptor helpers *
2408 ****************************************/
2410 int sptlrpc_current_user_desc_size(void)
2415 ngroups = current_ngroups;
2417 if (ngroups > LUSTRE_MAX_GROUPS)
2418 ngroups = LUSTRE_MAX_GROUPS;
2422 return sptlrpc_user_desc_size(ngroups);
2424 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2426 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2428 struct ptlrpc_user_desc *pud;
2430 pud = lustre_msg_buf(msg, offset, 0);
2432 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2433 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2434 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2435 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2436 pud->pud_cap = cfs_curproc_cap_pack();
2437 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2441 if (pud->pud_ngroups > current_ngroups)
2442 pud->pud_ngroups = current_ngroups;
2443 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2444 pud->pud_ngroups * sizeof(__u32));
2445 task_unlock(current);
2450 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2452 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2454 struct ptlrpc_user_desc *pud;
2457 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2462 __swab32s(&pud->pud_uid);
2463 __swab32s(&pud->pud_gid);
2464 __swab32s(&pud->pud_fsuid);
2465 __swab32s(&pud->pud_fsgid);
2466 __swab32s(&pud->pud_cap);
2467 __swab32s(&pud->pud_ngroups);
2470 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2471 CERROR("%u groups is too large\n", pud->pud_ngroups);
2475 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2476 msg->lm_buflens[offset]) {
2477 CERROR("%u groups are claimed but bufsize only %u\n",
2478 pud->pud_ngroups, msg->lm_buflens[offset]);
2483 for (i = 0; i < pud->pud_ngroups; i++)
2484 __swab32s(&pud->pud_groups[i]);
2489 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2491 /****************************************
2493 ****************************************/
2495 const char * sec2target_str(struct ptlrpc_sec *sec)
2497 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2499 if (sec_is_reverse(sec))
2501 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2503 EXPORT_SYMBOL(sec2target_str);
2506 * return true if the bulk data is protected
2508 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2510 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2511 case SPTLRPC_BULK_SVC_INTG:
2512 case SPTLRPC_BULK_SVC_PRIV:
2518 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2520 /****************************************
2521 * crypto API helper/alloc blkciper *
2522 ****************************************/
2524 /****************************************
2525 * initialize/finalize *
2526 ****************************************/
2528 int sptlrpc_init(void)
2532 rwlock_init(&policy_lock);
2534 rc = sptlrpc_gc_init();
2538 rc = sptlrpc_conf_init();
2542 rc = sptlrpc_enc_pool_init();
2546 rc = sptlrpc_null_init();
2550 rc = sptlrpc_plain_init();
2554 rc = sptlrpc_lproc_init();
2561 sptlrpc_plain_fini();
2563 sptlrpc_null_fini();
2565 sptlrpc_enc_pool_fini();
2567 sptlrpc_conf_fini();
2574 void sptlrpc_fini(void)
2576 sptlrpc_lproc_fini();
2577 sptlrpc_plain_fini();
2578 sptlrpc_null_fini();
2579 sptlrpc_enc_pool_fini();
2580 sptlrpc_conf_fini();