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, Whamcloud, Inc.
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 cfs_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 cfs_write_lock(&policy_lock);
83 if (unlikely(policies[number])) {
84 cfs_write_unlock(&policy_lock);
87 policies[number] = policy;
88 cfs_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 cfs_write_lock(&policy_lock);
102 if (unlikely(policies[number] == NULL)) {
103 cfs_write_unlock(&policy_lock);
104 CERROR("%s: already unregistered\n", policy->sp_name);
108 LASSERT(policies[number] == policy);
109 policies[number] = NULL;
110 cfs_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 CFS_DEFINE_MUTEX(load_mutex);
121 static cfs_atomic_t loaded = CFS_ATOMIC_INIT(0);
122 struct ptlrpc_sec_policy *policy;
123 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
126 if (number >= SPTLRPC_POLICY_MAX)
130 cfs_read_lock(&policy_lock);
131 policy = policies[number];
132 if (policy && !cfs_try_module_get(policy->sp_owner))
135 flag = cfs_atomic_read(&loaded);
136 cfs_read_unlock(&policy_lock);
138 if (policy != NULL || flag != 0 ||
139 number != SPTLRPC_POLICY_GSS)
142 /* try to load gss module, once */
143 cfs_mutex_lock(&load_mutex);
144 if (cfs_atomic_read(&loaded) == 0) {
145 if (cfs_request_module("ptlrpc_gss") == 0)
147 "module ptlrpc_gss loaded on demand\n");
149 CERROR("Unable to load module ptlrpc_gss\n");
151 cfs_atomic_set(&loaded, 1);
153 cfs_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, "krb5n"))
166 return SPTLRPC_FLVR_KRB5N;
167 if (!strcmp(name, "krb5a"))
168 return SPTLRPC_FLVR_KRB5A;
169 if (!strcmp(name, "krb5i"))
170 return SPTLRPC_FLVR_KRB5I;
171 if (!strcmp(name, "krb5p"))
172 return SPTLRPC_FLVR_KRB5P;
174 return SPTLRPC_FLVR_INVALID;
176 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
178 const char *sptlrpc_flavor2name_base(__u32 flvr)
180 __u32 base = SPTLRPC_FLVR_BASE(flvr);
182 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
184 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
186 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
188 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
190 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
192 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
195 CERROR("invalid wire flavor 0x%x\n", flvr);
198 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
200 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
201 char *buf, int bufsize)
203 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
204 snprintf(buf, bufsize, "hash:%s",
205 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
207 snprintf(buf, bufsize, "%s",
208 sptlrpc_flavor2name_base(sf->sf_rpc));
210 buf[bufsize - 1] = '\0';
213 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
215 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
217 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
220 * currently we don't support customized bulk specification for
221 * flavors other than plain
223 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
227 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
228 strncat(buf, bspec, bufsize);
231 buf[bufsize - 1] = '\0';
234 EXPORT_SYMBOL(sptlrpc_flavor2name);
236 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
240 if (flags & PTLRPC_SEC_FL_REVERSE)
241 strncat(buf, "reverse,", bufsize);
242 if (flags & PTLRPC_SEC_FL_ROOTONLY)
243 strncat(buf, "rootonly,", bufsize);
244 if (flags & PTLRPC_SEC_FL_UDESC)
245 strncat(buf, "udesc,", bufsize);
246 if (flags & PTLRPC_SEC_FL_BULK)
247 strncat(buf, "bulk,", bufsize);
249 strncat(buf, "-,", bufsize);
251 buf[bufsize - 1] = '\0';
254 EXPORT_SYMBOL(sptlrpc_secflags2str);
256 /**************************************************
257 * client context APIs *
258 **************************************************/
261 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
263 struct vfs_cred vcred;
264 int create = 1, remove_dead = 1;
267 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
269 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
270 PTLRPC_SEC_FL_ROOTONLY)) {
273 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
278 vcred.vc_uid = cfs_curproc_uid();
279 vcred.vc_gid = cfs_curproc_gid();
282 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
283 create, remove_dead);
286 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
288 cfs_atomic_inc(&ctx->cc_refcount);
291 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
293 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
295 struct ptlrpc_sec *sec = ctx->cc_sec;
298 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
300 if (!cfs_atomic_dec_and_test(&ctx->cc_refcount))
303 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
305 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
308 * Expire the client context immediately.
310 * \pre Caller must hold at least 1 reference on the \a ctx.
312 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
314 LASSERT(ctx->cc_ops->die);
315 ctx->cc_ops->die(ctx, 0);
317 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
320 * To wake up the threads who are waiting for this client context. Called
321 * after some status change happened on \a ctx.
323 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
325 struct ptlrpc_request *req, *next;
327 cfs_spin_lock(&ctx->cc_lock);
328 cfs_list_for_each_entry_safe(req, next, &ctx->cc_req_list,
330 cfs_list_del_init(&req->rq_ctx_chain);
331 ptlrpc_client_wake_req(req);
333 cfs_spin_unlock(&ctx->cc_lock);
335 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
337 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
339 LASSERT(ctx->cc_ops);
341 if (ctx->cc_ops->display == NULL)
344 return ctx->cc_ops->display(ctx, buf, bufsize);
347 static int import_sec_check_expire(struct obd_import *imp)
351 cfs_spin_lock(&imp->imp_lock);
352 if (imp->imp_sec_expire &&
353 imp->imp_sec_expire < cfs_time_current_sec()) {
355 imp->imp_sec_expire = 0;
357 cfs_spin_unlock(&imp->imp_lock);
362 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
363 return sptlrpc_import_sec_adapt(imp, NULL, 0);
366 static int import_sec_validate_get(struct obd_import *imp,
367 struct ptlrpc_sec **sec)
371 if (unlikely(imp->imp_sec_expire)) {
372 rc = import_sec_check_expire(imp);
377 *sec = sptlrpc_import_sec_ref(imp);
379 CERROR("import %p (%s) with no sec\n",
380 imp, ptlrpc_import_state_name(imp->imp_state));
384 if (unlikely((*sec)->ps_dying)) {
385 CERROR("attempt to use dying sec %p\n", sec);
386 sptlrpc_sec_put(*sec);
394 * Given a \a req, find or allocate a appropriate context for it.
395 * \pre req->rq_cli_ctx == NULL.
397 * \retval 0 succeed, and req->rq_cli_ctx is set.
398 * \retval -ev error number, and req->rq_cli_ctx == NULL.
400 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
402 struct obd_import *imp = req->rq_import;
403 struct ptlrpc_sec *sec;
407 LASSERT(!req->rq_cli_ctx);
410 rc = import_sec_validate_get(imp, &sec);
414 req->rq_cli_ctx = get_my_ctx(sec);
416 sptlrpc_sec_put(sec);
418 if (!req->rq_cli_ctx) {
419 CERROR("req %p: fail to get context\n", req);
427 * Drop the context for \a req.
428 * \pre req->rq_cli_ctx != NULL.
429 * \post req->rq_cli_ctx == NULL.
431 * If \a sync == 0, this function should return quickly without sleep;
432 * otherwise it might trigger and wait for the whole process of sending
433 * an context-destroying rpc to server.
435 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
440 LASSERT(req->rq_cli_ctx);
442 /* request might be asked to release earlier while still
443 * in the context waiting list.
445 if (!cfs_list_empty(&req->rq_ctx_chain)) {
446 cfs_spin_lock(&req->rq_cli_ctx->cc_lock);
447 cfs_list_del_init(&req->rq_ctx_chain);
448 cfs_spin_unlock(&req->rq_cli_ctx->cc_lock);
451 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
452 req->rq_cli_ctx = NULL;
457 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
458 struct ptlrpc_cli_ctx *oldctx,
459 struct ptlrpc_cli_ctx *newctx)
461 struct sptlrpc_flavor old_flvr;
462 char *reqmsg = NULL; /* to workaround old gcc */
466 LASSERT(req->rq_reqmsg);
467 LASSERT(req->rq_reqlen);
468 LASSERT(req->rq_replen);
470 CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
471 "switch sec %p(%s) -> %p(%s)\n", req,
472 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
473 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
474 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
475 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
478 old_flvr = req->rq_flvr;
480 /* save request message */
481 reqmsg_size = req->rq_reqlen;
482 if (reqmsg_size != 0) {
483 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
486 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
489 /* release old req/rep buf */
490 req->rq_cli_ctx = oldctx;
491 sptlrpc_cli_free_reqbuf(req);
492 sptlrpc_cli_free_repbuf(req);
493 req->rq_cli_ctx = newctx;
495 /* recalculate the flavor */
496 sptlrpc_req_set_flavor(req, 0);
498 /* alloc new request buffer
499 * we don't need to alloc reply buffer here, leave it to the
500 * rest procedure of ptlrpc */
501 if (reqmsg_size != 0) {
502 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
504 LASSERT(req->rq_reqmsg);
505 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
507 CWARN("failed to alloc reqbuf: %d\n", rc);
508 req->rq_flvr = old_flvr;
511 OBD_FREE_LARGE(reqmsg, reqmsg_size);
517 * If current context of \a req is dead somehow, e.g. we just switched flavor
518 * thus marked original contexts dead, we'll find a new context for it. if
519 * no switch is needed, \a req will end up with the same context.
521 * \note a request must have a context, to keep other parts of code happy.
522 * In any case of failure during the switching, we must restore the old one.
524 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
526 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
527 struct ptlrpc_cli_ctx *newctx;
533 sptlrpc_cli_ctx_get(oldctx);
534 sptlrpc_req_put_ctx(req, 0);
536 rc = sptlrpc_req_get_ctx(req);
538 LASSERT(!req->rq_cli_ctx);
540 /* restore old ctx */
541 req->rq_cli_ctx = oldctx;
545 newctx = req->rq_cli_ctx;
548 if (unlikely(newctx == oldctx &&
549 cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
551 * still get the old dead ctx, usually means system too busy
554 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
555 newctx, newctx->cc_flags);
557 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
561 * it's possible newctx == oldctx if we're switching
562 * subflavor with the same sec.
564 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
566 /* restore old ctx */
567 sptlrpc_req_put_ctx(req, 0);
568 req->rq_cli_ctx = oldctx;
572 LASSERT(req->rq_cli_ctx == newctx);
575 sptlrpc_cli_ctx_put(oldctx, 1);
578 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
581 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
583 if (cli_ctx_is_refreshed(ctx))
589 int ctx_refresh_timeout(void *data)
591 struct ptlrpc_request *req = data;
594 /* conn_cnt is needed in expire_one_request */
595 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
597 rc = ptlrpc_expire_one_request(req, 1);
598 /* if we started recovery, we should mark this ctx dead; otherwise
599 * in case of lgssd died nobody would retire this ctx, following
600 * connecting will still find the same ctx thus cause deadlock.
601 * there's an assumption that expire time of the request should be
602 * later than the context refresh expire time.
605 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
610 void ctx_refresh_interrupt(void *data)
612 struct ptlrpc_request *req = data;
614 cfs_spin_lock(&req->rq_lock);
616 cfs_spin_unlock(&req->rq_lock);
620 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
622 cfs_spin_lock(&ctx->cc_lock);
623 if (!cfs_list_empty(&req->rq_ctx_chain))
624 cfs_list_del_init(&req->rq_ctx_chain);
625 cfs_spin_unlock(&ctx->cc_lock);
629 * To refresh the context of \req, if it's not up-to-date.
632 * - = 0: wait until success or fatal error occur
633 * - > 0: timeout value (in seconds)
635 * The status of the context could be subject to be changed by other threads
636 * at any time. We allow this race, but once we return with 0, the caller will
637 * suppose it's uptodated and keep using it until the owning rpc is done.
639 * \retval 0 only if the context is uptodated.
640 * \retval -ev error number.
642 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
644 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
645 struct ptlrpc_sec *sec;
646 struct l_wait_info lwi;
652 if (req->rq_ctx_init || req->rq_ctx_fini)
656 * during the process a request's context might change type even
657 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
661 rc = import_sec_validate_get(req->rq_import, &sec);
665 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
666 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
667 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
668 req_off_ctx_list(req, ctx);
669 sptlrpc_req_replace_dead_ctx(req);
670 ctx = req->rq_cli_ctx;
672 sptlrpc_sec_put(sec);
674 if (cli_ctx_is_eternal(ctx))
677 if (unlikely(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
678 LASSERT(ctx->cc_ops->refresh);
679 ctx->cc_ops->refresh(ctx);
681 LASSERT(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
683 LASSERT(ctx->cc_ops->validate);
684 if (ctx->cc_ops->validate(ctx) == 0) {
685 req_off_ctx_list(req, ctx);
689 if (unlikely(cfs_test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
690 cfs_spin_lock(&req->rq_lock);
692 cfs_spin_unlock(&req->rq_lock);
693 req_off_ctx_list(req, ctx);
698 * There's a subtle issue for resending RPCs, suppose following
700 * 1. the request was sent to server.
701 * 2. recovery was kicked start, after finished the request was
703 * 3. resend the request.
704 * 4. old reply from server received, we accept and verify the reply.
705 * this has to be success, otherwise the error will be aware
707 * 5. new reply from server received, dropped by LNet.
709 * Note the xid of old & new request is the same. We can't simply
710 * change xid for the resent request because the server replies on
711 * it for reply reconstruction.
713 * Commonly the original context should be uptodate because we
714 * have a expiry nice time; server will keep its context because
715 * we at least hold a ref of old context which prevent context
716 * destroying RPC being sent. So server still can accept the request
717 * and finish the RPC. But if that's not the case:
718 * 1. If server side context has been trimmed, a NO_CONTEXT will
719 * be returned, gss_cli_ctx_verify/unseal will switch to new
721 * 2. Current context never be refreshed, then we are fine: we
722 * never really send request with old context before.
724 if (cfs_test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
725 unlikely(req->rq_reqmsg) &&
726 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
727 req_off_ctx_list(req, ctx);
731 if (unlikely(cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
732 req_off_ctx_list(req, ctx);
734 * don't switch ctx if import was deactivated
736 if (req->rq_import->imp_deactive) {
737 cfs_spin_lock(&req->rq_lock);
739 cfs_spin_unlock(&req->rq_lock);
743 rc = sptlrpc_req_replace_dead_ctx(req);
745 LASSERT(ctx == req->rq_cli_ctx);
746 CERROR("req %p: failed to replace dead ctx %p: %d\n",
748 cfs_spin_lock(&req->rq_lock);
750 cfs_spin_unlock(&req->rq_lock);
754 ctx = req->rq_cli_ctx;
759 * Now we're sure this context is during upcall, add myself into
762 cfs_spin_lock(&ctx->cc_lock);
763 if (cfs_list_empty(&req->rq_ctx_chain))
764 cfs_list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
765 cfs_spin_unlock(&ctx->cc_lock);
768 RETURN(-EWOULDBLOCK);
770 /* Clear any flags that may be present from previous sends */
771 LASSERT(req->rq_receiving_reply == 0);
772 cfs_spin_lock(&req->rq_lock);
774 req->rq_timedout = 0;
777 cfs_spin_unlock(&req->rq_lock);
779 lwi = LWI_TIMEOUT_INTR(timeout * CFS_HZ, ctx_refresh_timeout,
780 ctx_refresh_interrupt, req);
781 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
784 * following cases could lead us here:
785 * - successfully refreshed;
787 * - timedout, and we don't want recover from the failure;
788 * - timedout, and waked up upon recovery finished;
789 * - someone else mark this ctx dead by force;
790 * - someone invalidate the req and call ptlrpc_client_wake_req(),
791 * e.g. ptlrpc_abort_inflight();
793 if (!cli_ctx_is_refreshed(ctx)) {
794 /* timed out or interruptted */
795 req_off_ctx_list(req, ctx);
805 * Initialize flavor settings for \a req, according to \a opcode.
807 * \note this could be called in two situations:
808 * - new request from ptlrpc_pre_req(), with proper @opcode
809 * - old request which changed ctx in the middle, with @opcode == 0
811 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
813 struct ptlrpc_sec *sec;
815 LASSERT(req->rq_import);
816 LASSERT(req->rq_cli_ctx);
817 LASSERT(req->rq_cli_ctx->cc_sec);
818 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
820 /* special security flags accoding to opcode */
824 case MGS_CONFIG_READ:
826 req->rq_bulk_read = 1;
830 req->rq_bulk_write = 1;
833 req->rq_ctx_init = 1;
836 req->rq_ctx_fini = 1;
839 /* init/fini rpc won't be resend, so can't be here */
840 LASSERT(req->rq_ctx_init == 0);
841 LASSERT(req->rq_ctx_fini == 0);
843 /* cleanup flags, which should be recalculated */
844 req->rq_pack_udesc = 0;
845 req->rq_pack_bulk = 0;
849 sec = req->rq_cli_ctx->cc_sec;
851 cfs_spin_lock(&sec->ps_lock);
852 req->rq_flvr = sec->ps_flvr;
853 cfs_spin_unlock(&sec->ps_lock);
855 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
857 if (unlikely(req->rq_ctx_init))
858 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
859 else if (unlikely(req->rq_ctx_fini))
860 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
862 /* user descriptor flag, null security can't do it anyway */
863 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
864 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
865 req->rq_pack_udesc = 1;
867 /* bulk security flag */
868 if ((req->rq_bulk_read || req->rq_bulk_write) &&
869 sptlrpc_flavor_has_bulk(&req->rq_flvr))
870 req->rq_pack_bulk = 1;
873 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
875 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
878 LASSERT(req->rq_clrbuf);
879 if (req->rq_pool || !req->rq_reqbuf)
882 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
883 req->rq_reqbuf = NULL;
884 req->rq_reqbuf_len = 0;
888 * Given an import \a imp, check whether current user has a valid context
889 * or not. We may create a new context and try to refresh it, and try
890 * repeatedly try in case of non-fatal errors. Return 0 means success.
892 int sptlrpc_import_check_ctx(struct obd_import *imp)
894 struct ptlrpc_sec *sec;
895 struct ptlrpc_cli_ctx *ctx;
896 struct ptlrpc_request *req = NULL;
902 sec = sptlrpc_import_sec_ref(imp);
903 ctx = get_my_ctx(sec);
904 sptlrpc_sec_put(sec);
909 if (cli_ctx_is_eternal(ctx) ||
910 ctx->cc_ops->validate(ctx) == 0) {
911 sptlrpc_cli_ctx_put(ctx, 1);
915 if (cli_ctx_is_error(ctx)) {
916 sptlrpc_cli_ctx_put(ctx, 1);
924 cfs_spin_lock_init(&req->rq_lock);
925 cfs_atomic_set(&req->rq_refcount, 10000);
926 CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
927 cfs_waitq_init(&req->rq_reply_waitq);
928 cfs_waitq_init(&req->rq_set_waitq);
929 req->rq_import = imp;
930 req->rq_flvr = sec->ps_flvr;
931 req->rq_cli_ctx = ctx;
933 rc = sptlrpc_req_refresh_ctx(req, 0);
934 LASSERT(cfs_list_empty(&req->rq_ctx_chain));
935 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
942 * Used by ptlrpc client, to perform the pre-defined security transformation
943 * upon the request message of \a req. After this function called,
944 * req->rq_reqmsg is still accessible as clear text.
946 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
948 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
953 LASSERT(ctx->cc_sec);
954 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
956 /* we wrap bulk request here because now we can be sure
957 * the context is uptodate.
960 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
965 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
966 case SPTLRPC_SVC_NULL:
967 case SPTLRPC_SVC_AUTH:
968 case SPTLRPC_SVC_INTG:
969 LASSERT(ctx->cc_ops->sign);
970 rc = ctx->cc_ops->sign(ctx, req);
972 case SPTLRPC_SVC_PRIV:
973 LASSERT(ctx->cc_ops->seal);
974 rc = ctx->cc_ops->seal(ctx, req);
981 LASSERT(req->rq_reqdata_len);
982 LASSERT(req->rq_reqdata_len % 8 == 0);
983 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
989 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
991 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
996 LASSERT(ctx->cc_sec);
997 LASSERT(req->rq_repbuf);
998 LASSERT(req->rq_repdata);
999 LASSERT(req->rq_repmsg == NULL);
1001 req->rq_rep_swab_mask = 0;
1003 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1006 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1010 CERROR("failed unpack reply: x"LPU64"\n", req->rq_xid);
1014 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1015 CERROR("replied data length %d too small\n",
1016 req->rq_repdata_len);
1020 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1021 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1022 CERROR("reply policy %u doesn't match request policy %u\n",
1023 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1024 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1028 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1029 case SPTLRPC_SVC_NULL:
1030 case SPTLRPC_SVC_AUTH:
1031 case SPTLRPC_SVC_INTG:
1032 LASSERT(ctx->cc_ops->verify);
1033 rc = ctx->cc_ops->verify(ctx, req);
1035 case SPTLRPC_SVC_PRIV:
1036 LASSERT(ctx->cc_ops->unseal);
1037 rc = ctx->cc_ops->unseal(ctx, req);
1042 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1044 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1046 req->rq_rep_swab_mask = 0;
1051 * Used by ptlrpc client, to perform security transformation upon the reply
1052 * message of \a req. After return successfully, req->rq_repmsg points to
1053 * the reply message in clear text.
1055 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1058 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1060 LASSERT(req->rq_repbuf);
1061 LASSERT(req->rq_repdata == NULL);
1062 LASSERT(req->rq_repmsg == NULL);
1063 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1065 if (req->rq_reply_off == 0 &&
1066 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1067 CERROR("real reply with offset 0\n");
1071 if (req->rq_reply_off % 8 != 0) {
1072 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1076 req->rq_repdata = (struct lustre_msg *)
1077 (req->rq_repbuf + req->rq_reply_off);
1078 req->rq_repdata_len = req->rq_nob_received;
1080 return do_cli_unwrap_reply(req);
1084 * Used by ptlrpc client, to perform security transformation upon the early
1085 * reply message of \a req. We expect the rq_reply_off is 0, and
1086 * rq_nob_received is the early reply size.
1088 * Because the receive buffer might be still posted, the reply data might be
1089 * changed at any time, no matter we're holding rq_lock or not. For this reason
1090 * we allocate a separate ptlrpc_request and reply buffer for early reply
1093 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1094 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1095 * \a *req_ret to release it.
1096 * \retval -ev error number, and \a req_ret will not be set.
1098 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1099 struct ptlrpc_request **req_ret)
1101 struct ptlrpc_request *early_req;
1103 int early_bufsz, early_size;
1107 OBD_ALLOC_PTR(early_req);
1108 if (early_req == NULL)
1111 early_size = req->rq_nob_received;
1112 early_bufsz = size_roundup_power2(early_size);
1113 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1114 if (early_buf == NULL)
1115 GOTO(err_req, rc = -ENOMEM);
1117 /* sanity checkings and copy data out, do it inside spinlock */
1118 cfs_spin_lock(&req->rq_lock);
1120 if (req->rq_replied) {
1121 cfs_spin_unlock(&req->rq_lock);
1122 GOTO(err_buf, rc = -EALREADY);
1125 LASSERT(req->rq_repbuf);
1126 LASSERT(req->rq_repdata == NULL);
1127 LASSERT(req->rq_repmsg == NULL);
1129 if (req->rq_reply_off != 0) {
1130 CERROR("early reply with offset %u\n", req->rq_reply_off);
1131 cfs_spin_unlock(&req->rq_lock);
1132 GOTO(err_buf, rc = -EPROTO);
1135 if (req->rq_nob_received != early_size) {
1136 /* even another early arrived the size should be the same */
1137 CERROR("data size has changed from %u to %u\n",
1138 early_size, req->rq_nob_received);
1139 cfs_spin_unlock(&req->rq_lock);
1140 GOTO(err_buf, rc = -EINVAL);
1143 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1144 CERROR("early reply length %d too small\n",
1145 req->rq_nob_received);
1146 cfs_spin_unlock(&req->rq_lock);
1147 GOTO(err_buf, rc = -EALREADY);
1150 memcpy(early_buf, req->rq_repbuf, early_size);
1151 cfs_spin_unlock(&req->rq_lock);
1153 cfs_spin_lock_init(&early_req->rq_lock);
1154 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1155 early_req->rq_flvr = req->rq_flvr;
1156 early_req->rq_repbuf = early_buf;
1157 early_req->rq_repbuf_len = early_bufsz;
1158 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1159 early_req->rq_repdata_len = early_size;
1160 early_req->rq_early = 1;
1161 early_req->rq_reqmsg = req->rq_reqmsg;
1163 rc = do_cli_unwrap_reply(early_req);
1165 DEBUG_REQ(D_ADAPTTO, early_req,
1166 "error %d unwrap early reply", rc);
1170 LASSERT(early_req->rq_repmsg);
1171 *req_ret = early_req;
1175 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1177 OBD_FREE_LARGE(early_buf, early_bufsz);
1179 OBD_FREE_PTR(early_req);
1184 * Used by ptlrpc client, to release a processed early reply \a early_req.
1186 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1188 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1190 LASSERT(early_req->rq_repbuf);
1191 LASSERT(early_req->rq_repdata);
1192 LASSERT(early_req->rq_repmsg);
1194 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1195 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1196 OBD_FREE_PTR(early_req);
1199 /**************************************************
1201 **************************************************/
1204 * "fixed" sec (e.g. null) use sec_id < 0
1206 static cfs_atomic_t sptlrpc_sec_id = CFS_ATOMIC_INIT(1);
1208 int sptlrpc_get_next_secid(void)
1210 return cfs_atomic_inc_return(&sptlrpc_sec_id);
1212 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1214 /**************************************************
1215 * client side high-level security APIs *
1216 **************************************************/
1218 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1219 int grace, int force)
1221 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1223 LASSERT(policy->sp_cops);
1224 LASSERT(policy->sp_cops->flush_ctx_cache);
1226 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1229 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1231 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1233 LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1234 LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1235 LASSERT(policy->sp_cops->destroy_sec);
1237 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1239 policy->sp_cops->destroy_sec(sec);
1240 sptlrpc_policy_put(policy);
1243 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1245 sec_cop_destroy_sec(sec);
1247 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1249 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1251 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1253 if (sec->ps_policy->sp_cops->kill_sec) {
1254 sec->ps_policy->sp_cops->kill_sec(sec);
1256 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1260 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1263 cfs_atomic_inc(&sec->ps_refcount);
1267 EXPORT_SYMBOL(sptlrpc_sec_get);
1269 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1272 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1274 if (cfs_atomic_dec_and_test(&sec->ps_refcount)) {
1275 sptlrpc_gc_del_sec(sec);
1276 sec_cop_destroy_sec(sec);
1280 EXPORT_SYMBOL(sptlrpc_sec_put);
1283 * policy module is responsible for taking refrence of import
1286 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1287 struct ptlrpc_svc_ctx *svc_ctx,
1288 struct sptlrpc_flavor *sf,
1289 enum lustre_sec_part sp)
1291 struct ptlrpc_sec_policy *policy;
1292 struct ptlrpc_sec *sec;
1297 LASSERT(imp->imp_dlm_fake == 1);
1299 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1300 imp->imp_obd->obd_type->typ_name,
1301 imp->imp_obd->obd_name,
1302 sptlrpc_flavor2name(sf, str, sizeof(str)));
1304 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1305 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1307 LASSERT(imp->imp_dlm_fake == 0);
1309 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1310 imp->imp_obd->obd_type->typ_name,
1311 imp->imp_obd->obd_name,
1312 sptlrpc_flavor2name(sf, str, sizeof(str)));
1314 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1316 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1321 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1323 cfs_atomic_inc(&sec->ps_refcount);
1327 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1328 sptlrpc_gc_add_sec(sec);
1330 sptlrpc_policy_put(policy);
1336 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1338 struct ptlrpc_sec *sec;
1340 cfs_spin_lock(&imp->imp_lock);
1341 sec = sptlrpc_sec_get(imp->imp_sec);
1342 cfs_spin_unlock(&imp->imp_lock);
1346 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1348 static void sptlrpc_import_sec_install(struct obd_import *imp,
1349 struct ptlrpc_sec *sec)
1351 struct ptlrpc_sec *old_sec;
1353 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1355 cfs_spin_lock(&imp->imp_lock);
1356 old_sec = imp->imp_sec;
1358 cfs_spin_unlock(&imp->imp_lock);
1361 sptlrpc_sec_kill(old_sec);
1363 /* balance the ref taken by this import */
1364 sptlrpc_sec_put(old_sec);
1369 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1371 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1375 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1380 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1381 struct ptlrpc_sec *sec,
1382 struct sptlrpc_flavor *sf)
1384 char str1[32], str2[32];
1386 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1387 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1388 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1389 str1, sizeof(str1)),
1390 sptlrpc_secflags2str(sf->sf_flags,
1391 str2, sizeof(str2)));
1393 cfs_spin_lock(&sec->ps_lock);
1394 flavor_copy(&sec->ps_flvr, sf);
1395 cfs_spin_unlock(&sec->ps_lock);
1399 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1400 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1402 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1403 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1405 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1406 struct ptlrpc_svc_ctx *svc_ctx,
1407 struct sptlrpc_flavor *flvr)
1409 struct ptlrpc_connection *conn;
1410 struct sptlrpc_flavor sf;
1411 struct ptlrpc_sec *sec, *newsec;
1412 enum lustre_sec_part sp;
1422 conn = imp->imp_connection;
1424 if (svc_ctx == NULL) {
1425 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1427 * normal import, determine flavor from rule set, except
1428 * for mgc the flavor is predetermined.
1430 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1431 sf = cliobd->cl_flvr_mgc;
1433 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1435 &cliobd->cl_target_uuid,
1438 sp = imp->imp_obd->u.cli.cl_sp_me;
1440 /* reverse import, determine flavor from incoming reqeust */
1443 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1444 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1445 PTLRPC_SEC_FL_ROOTONLY;
1447 sp = sptlrpc_target_sec_part(imp->imp_obd);
1450 sec = sptlrpc_import_sec_ref(imp);
1454 if (flavor_equal(&sf, &sec->ps_flvr))
1457 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1458 imp->imp_obd->obd_name,
1459 obd_uuid2str(&conn->c_remote_uuid),
1460 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1461 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1463 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1464 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1465 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1466 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1467 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1470 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1471 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1472 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1473 imp->imp_obd->obd_name,
1474 obd_uuid2str(&conn->c_remote_uuid),
1475 LNET_NIDNET(conn->c_self),
1476 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1479 cfs_mutex_lock(&imp->imp_sec_mutex);
1481 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1483 sptlrpc_import_sec_install(imp, newsec);
1485 CERROR("import %s->%s: failed to create new sec\n",
1486 imp->imp_obd->obd_name,
1487 obd_uuid2str(&conn->c_remote_uuid));
1491 cfs_mutex_unlock(&imp->imp_sec_mutex);
1493 sptlrpc_sec_put(sec);
1497 void sptlrpc_import_sec_put(struct obd_import *imp)
1500 sptlrpc_sec_kill(imp->imp_sec);
1502 sptlrpc_sec_put(imp->imp_sec);
1503 imp->imp_sec = NULL;
1507 static void import_flush_ctx_common(struct obd_import *imp,
1508 uid_t uid, int grace, int force)
1510 struct ptlrpc_sec *sec;
1515 sec = sptlrpc_import_sec_ref(imp);
1519 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1520 sptlrpc_sec_put(sec);
1523 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1525 /* it's important to use grace mode, see explain in
1526 * sptlrpc_req_refresh_ctx() */
1527 import_flush_ctx_common(imp, 0, 1, 1);
1530 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1532 import_flush_ctx_common(imp, cfs_curproc_uid(), 1, 1);
1534 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1536 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1538 import_flush_ctx_common(imp, -1, 1, 1);
1540 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1543 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1544 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1546 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1548 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1549 struct ptlrpc_sec_policy *policy;
1553 LASSERT(ctx->cc_sec);
1554 LASSERT(ctx->cc_sec->ps_policy);
1555 LASSERT(req->rq_reqmsg == NULL);
1556 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1558 policy = ctx->cc_sec->ps_policy;
1559 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1561 LASSERT(req->rq_reqmsg);
1562 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1564 /* zeroing preallocated buffer */
1566 memset(req->rq_reqmsg, 0, msgsize);
1573 * Used by ptlrpc client to free request buffer of \a req. After this
1574 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1576 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1578 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1579 struct ptlrpc_sec_policy *policy;
1582 LASSERT(ctx->cc_sec);
1583 LASSERT(ctx->cc_sec->ps_policy);
1584 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1586 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1589 policy = ctx->cc_sec->ps_policy;
1590 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1591 req->rq_reqmsg = NULL;
1595 * NOTE caller must guarantee the buffer size is enough for the enlargement
1597 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1598 int segment, int newsize)
1601 int oldsize, oldmsg_size, movesize;
1603 LASSERT(segment < msg->lm_bufcount);
1604 LASSERT(msg->lm_buflens[segment] <= newsize);
1606 if (msg->lm_buflens[segment] == newsize)
1609 /* nothing to do if we are enlarging the last segment */
1610 if (segment == msg->lm_bufcount - 1) {
1611 msg->lm_buflens[segment] = newsize;
1615 oldsize = msg->lm_buflens[segment];
1617 src = lustre_msg_buf(msg, segment + 1, 0);
1618 msg->lm_buflens[segment] = newsize;
1619 dst = lustre_msg_buf(msg, segment + 1, 0);
1620 msg->lm_buflens[segment] = oldsize;
1622 /* move from segment + 1 to end segment */
1623 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1624 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1625 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1626 LASSERT(movesize >= 0);
1629 memmove(dst, src, movesize);
1631 /* note we don't clear the ares where old data live, not secret */
1633 /* finally set new segment size */
1634 msg->lm_buflens[segment] = newsize;
1636 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1639 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1640 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1641 * preserved after the enlargement. this must be called after original request
1642 * buffer being allocated.
1644 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1645 * so caller should refresh its local pointers if needed.
1647 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1648 int segment, int newsize)
1650 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1651 struct ptlrpc_sec_cops *cops;
1652 struct lustre_msg *msg = req->rq_reqmsg;
1656 LASSERT(msg->lm_bufcount > segment);
1657 LASSERT(msg->lm_buflens[segment] <= newsize);
1659 if (msg->lm_buflens[segment] == newsize)
1662 cops = ctx->cc_sec->ps_policy->sp_cops;
1663 LASSERT(cops->enlarge_reqbuf);
1664 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1666 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1669 * Used by ptlrpc client to allocate reply buffer of \a req.
1671 * \note After this, req->rq_repmsg is still not accessible.
1673 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1675 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1676 struct ptlrpc_sec_policy *policy;
1680 LASSERT(ctx->cc_sec);
1681 LASSERT(ctx->cc_sec->ps_policy);
1686 policy = ctx->cc_sec->ps_policy;
1687 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1691 * Used by ptlrpc client to free reply buffer of \a req. After this
1692 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1694 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1696 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1697 struct ptlrpc_sec_policy *policy;
1701 LASSERT(ctx->cc_sec);
1702 LASSERT(ctx->cc_sec->ps_policy);
1703 LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1705 if (req->rq_repbuf == NULL)
1707 LASSERT(req->rq_repbuf_len);
1709 policy = ctx->cc_sec->ps_policy;
1710 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1711 req->rq_repmsg = NULL;
1715 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1716 struct ptlrpc_cli_ctx *ctx)
1718 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1720 if (!policy->sp_cops->install_rctx)
1722 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1725 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1726 struct ptlrpc_svc_ctx *ctx)
1728 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1730 if (!policy->sp_sops->install_rctx)
1732 return policy->sp_sops->install_rctx(imp, ctx);
1735 /****************************************
1736 * server side security *
1737 ****************************************/
1739 static int flavor_allowed(struct sptlrpc_flavor *exp,
1740 struct ptlrpc_request *req)
1742 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1744 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1747 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1748 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1749 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1750 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1756 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1759 * Given an export \a exp, check whether the flavor of incoming \a req
1760 * is allowed by the export \a exp. Main logic is about taking care of
1761 * changing configurations. Return 0 means success.
1763 int sptlrpc_target_export_check(struct obd_export *exp,
1764 struct ptlrpc_request *req)
1766 struct sptlrpc_flavor flavor;
1771 /* client side export has no imp_reverse, skip
1772 * FIXME maybe we should check flavor this as well??? */
1773 if (exp->exp_imp_reverse == NULL)
1776 /* don't care about ctx fini rpc */
1777 if (req->rq_ctx_fini)
1780 cfs_spin_lock(&exp->exp_lock);
1782 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1783 * the first req with the new flavor, then treat it as current flavor,
1784 * adapt reverse sec according to it.
1785 * note the first rpc with new flavor might not be with root ctx, in
1786 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1787 if (unlikely(exp->exp_flvr_changed) &&
1788 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1789 /* make the new flavor as "current", and old ones as
1790 * about-to-expire */
1791 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1792 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1793 flavor = exp->exp_flvr_old[1];
1794 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1795 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1796 exp->exp_flvr_old[0] = exp->exp_flvr;
1797 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1798 EXP_FLVR_UPDATE_EXPIRE;
1799 exp->exp_flvr = flavor;
1801 /* flavor change finished */
1802 exp->exp_flvr_changed = 0;
1803 LASSERT(exp->exp_flvr_adapt == 1);
1805 /* if it's gss, we only interested in root ctx init */
1806 if (req->rq_auth_gss &&
1807 !(req->rq_ctx_init &&
1808 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1809 req->rq_auth_usr_ost))) {
1810 cfs_spin_unlock(&exp->exp_lock);
1811 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1812 req->rq_auth_gss, req->rq_ctx_init,
1813 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1814 req->rq_auth_usr_ost);
1818 exp->exp_flvr_adapt = 0;
1819 cfs_spin_unlock(&exp->exp_lock);
1821 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1822 req->rq_svc_ctx, &flavor);
1825 /* if it equals to the current flavor, we accept it, but need to
1826 * dealing with reverse sec/ctx */
1827 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1828 /* most cases should return here, we only interested in
1829 * gss root ctx init */
1830 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1831 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1832 !req->rq_auth_usr_ost)) {
1833 cfs_spin_unlock(&exp->exp_lock);
1837 /* if flavor just changed, we should not proceed, just leave
1838 * it and current flavor will be discovered and replaced
1839 * shortly, and let _this_ rpc pass through */
1840 if (exp->exp_flvr_changed) {
1841 LASSERT(exp->exp_flvr_adapt);
1842 cfs_spin_unlock(&exp->exp_lock);
1846 if (exp->exp_flvr_adapt) {
1847 exp->exp_flvr_adapt = 0;
1848 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1849 exp, exp->exp_flvr.sf_rpc,
1850 exp->exp_flvr_old[0].sf_rpc,
1851 exp->exp_flvr_old[1].sf_rpc);
1852 flavor = exp->exp_flvr;
1853 cfs_spin_unlock(&exp->exp_lock);
1855 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1859 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1860 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1861 exp->exp_flvr_old[0].sf_rpc,
1862 exp->exp_flvr_old[1].sf_rpc);
1863 cfs_spin_unlock(&exp->exp_lock);
1865 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1870 if (exp->exp_flvr_expire[0]) {
1871 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1872 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1873 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1874 "middle one ("CFS_DURATION_T")\n", exp,
1875 exp->exp_flvr.sf_rpc,
1876 exp->exp_flvr_old[0].sf_rpc,
1877 exp->exp_flvr_old[1].sf_rpc,
1878 exp->exp_flvr_expire[0] -
1879 cfs_time_current_sec());
1880 cfs_spin_unlock(&exp->exp_lock);
1884 CDEBUG(D_SEC, "mark middle expired\n");
1885 exp->exp_flvr_expire[0] = 0;
1887 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1888 exp->exp_flvr.sf_rpc,
1889 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1890 req->rq_flvr.sf_rpc);
1893 /* now it doesn't match the current flavor, the only chance we can
1894 * accept it is match the old flavors which is not expired. */
1895 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1896 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1897 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1898 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1899 "oldest one ("CFS_DURATION_T")\n", exp,
1900 exp->exp_flvr.sf_rpc,
1901 exp->exp_flvr_old[0].sf_rpc,
1902 exp->exp_flvr_old[1].sf_rpc,
1903 exp->exp_flvr_expire[1] -
1904 cfs_time_current_sec());
1905 cfs_spin_unlock(&exp->exp_lock);
1909 CDEBUG(D_SEC, "mark oldest expired\n");
1910 exp->exp_flvr_expire[1] = 0;
1912 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1913 exp, exp->exp_flvr.sf_rpc,
1914 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1915 req->rq_flvr.sf_rpc);
1917 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1918 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1919 exp->exp_flvr_old[1].sf_rpc);
1922 cfs_spin_unlock(&exp->exp_lock);
1924 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with "
1925 "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1926 exp, exp->exp_obd->obd_name,
1927 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1928 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1929 req->rq_flvr.sf_rpc,
1930 exp->exp_flvr.sf_rpc,
1931 exp->exp_flvr_old[0].sf_rpc,
1932 exp->exp_flvr_expire[0] ?
1933 (unsigned long) (exp->exp_flvr_expire[0] -
1934 cfs_time_current_sec()) : 0,
1935 exp->exp_flvr_old[1].sf_rpc,
1936 exp->exp_flvr_expire[1] ?
1937 (unsigned long) (exp->exp_flvr_expire[1] -
1938 cfs_time_current_sec()) : 0);
1941 EXPORT_SYMBOL(sptlrpc_target_export_check);
1943 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1944 struct sptlrpc_rule_set *rset)
1946 struct obd_export *exp;
1947 struct sptlrpc_flavor new_flvr;
1951 cfs_spin_lock(&obd->obd_dev_lock);
1953 cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1954 if (exp->exp_connection == NULL)
1957 /* note if this export had just been updated flavor
1958 * (exp_flvr_changed == 1), this will override the
1960 cfs_spin_lock(&exp->exp_lock);
1961 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1962 exp->exp_connection->c_peer.nid,
1964 if (exp->exp_flvr_changed ||
1965 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1966 exp->exp_flvr_old[1] = new_flvr;
1967 exp->exp_flvr_expire[1] = 0;
1968 exp->exp_flvr_changed = 1;
1969 exp->exp_flvr_adapt = 1;
1971 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1972 exp, sptlrpc_part2name(exp->exp_sp_peer),
1973 exp->exp_flvr.sf_rpc,
1974 exp->exp_flvr_old[1].sf_rpc);
1976 cfs_spin_unlock(&exp->exp_lock);
1979 cfs_spin_unlock(&obd->obd_dev_lock);
1981 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1983 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1985 /* peer's claim is unreliable unless gss is being used */
1986 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1989 switch (req->rq_sp_from) {
1991 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1992 DEBUG_REQ(D_ERROR, req, "faked source CLI");
1993 svc_rc = SECSVC_DROP;
1997 if (!req->rq_auth_usr_mdt) {
1998 DEBUG_REQ(D_ERROR, req, "faked source MDT");
1999 svc_rc = SECSVC_DROP;
2003 if (!req->rq_auth_usr_ost) {
2004 DEBUG_REQ(D_ERROR, req, "faked source OST");
2005 svc_rc = SECSVC_DROP;
2010 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2011 !req->rq_auth_usr_ost) {
2012 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2013 svc_rc = SECSVC_DROP;
2018 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2019 svc_rc = SECSVC_DROP;
2026 * Used by ptlrpc server, to perform transformation upon request message of
2027 * incoming \a req. This must be the first thing to do with a incoming
2028 * request in ptlrpc layer.
2030 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2031 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2032 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2033 * reply message has been prepared.
2034 * \retval SECSVC_DROP failed, this request should be dropped.
2036 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2038 struct ptlrpc_sec_policy *policy;
2039 struct lustre_msg *msg = req->rq_reqbuf;
2044 LASSERT(req->rq_reqmsg == NULL);
2045 LASSERT(req->rq_repmsg == NULL);
2046 LASSERT(req->rq_svc_ctx == NULL);
2048 req->rq_req_swab_mask = 0;
2050 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2053 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2057 CERROR("error unpacking request from %s x"LPU64"\n",
2058 libcfs_id2str(req->rq_peer), req->rq_xid);
2059 RETURN(SECSVC_DROP);
2062 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2063 req->rq_sp_from = LUSTRE_SP_ANY;
2064 req->rq_auth_uid = INVALID_UID;
2065 req->rq_auth_mapped_uid = INVALID_UID;
2067 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2069 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2070 RETURN(SECSVC_DROP);
2073 LASSERT(policy->sp_sops->accept);
2074 rc = policy->sp_sops->accept(req);
2075 sptlrpc_policy_put(policy);
2076 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2077 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2080 * if it's not null flavor (which means embedded packing msg),
2081 * reset the swab mask for the comming inner msg unpacking.
2083 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2084 req->rq_req_swab_mask = 0;
2086 /* sanity check for the request source */
2087 rc = sptlrpc_svc_check_from(req, rc);
2092 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2093 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2094 * a buffer of \a msglen size.
2096 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2098 struct ptlrpc_sec_policy *policy;
2099 struct ptlrpc_reply_state *rs;
2103 LASSERT(req->rq_svc_ctx);
2104 LASSERT(req->rq_svc_ctx->sc_policy);
2106 policy = req->rq_svc_ctx->sc_policy;
2107 LASSERT(policy->sp_sops->alloc_rs);
2109 rc = policy->sp_sops->alloc_rs(req, msglen);
2110 if (unlikely(rc == -ENOMEM)) {
2111 /* failed alloc, try emergency pool */
2112 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_svcpt);
2116 req->rq_reply_state = rs;
2117 rc = policy->sp_sops->alloc_rs(req, msglen);
2119 lustre_put_emerg_rs(rs);
2120 req->rq_reply_state = NULL;
2125 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2131 * Used by ptlrpc server, to perform transformation upon reply message.
2133 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2134 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2136 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2138 struct ptlrpc_sec_policy *policy;
2142 LASSERT(req->rq_svc_ctx);
2143 LASSERT(req->rq_svc_ctx->sc_policy);
2145 policy = req->rq_svc_ctx->sc_policy;
2146 LASSERT(policy->sp_sops->authorize);
2148 rc = policy->sp_sops->authorize(req);
2149 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2155 * Used by ptlrpc server, to free reply_state.
2157 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2159 struct ptlrpc_sec_policy *policy;
2160 unsigned int prealloc;
2163 LASSERT(rs->rs_svc_ctx);
2164 LASSERT(rs->rs_svc_ctx->sc_policy);
2166 policy = rs->rs_svc_ctx->sc_policy;
2167 LASSERT(policy->sp_sops->free_rs);
2169 prealloc = rs->rs_prealloc;
2170 policy->sp_sops->free_rs(rs);
2173 lustre_put_emerg_rs(rs);
2177 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2179 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2182 cfs_atomic_inc(&ctx->sc_refcount);
2185 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2187 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2192 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2193 if (cfs_atomic_dec_and_test(&ctx->sc_refcount)) {
2194 if (ctx->sc_policy->sp_sops->free_ctx)
2195 ctx->sc_policy->sp_sops->free_ctx(ctx);
2197 req->rq_svc_ctx = NULL;
2200 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2202 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2207 LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2208 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2209 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2211 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2213 /****************************************
2215 ****************************************/
2218 * Perform transformation upon bulk data pointed by \a desc. This is called
2219 * before transforming the request message.
2221 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2222 struct ptlrpc_bulk_desc *desc)
2224 struct ptlrpc_cli_ctx *ctx;
2226 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2228 if (!req->rq_pack_bulk)
2231 ctx = req->rq_cli_ctx;
2232 if (ctx->cc_ops->wrap_bulk)
2233 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2236 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2239 * This is called after unwrap the reply message.
2240 * return nob of actual plain text size received, or error code.
2242 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2243 struct ptlrpc_bulk_desc *desc,
2246 struct ptlrpc_cli_ctx *ctx;
2249 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2251 if (!req->rq_pack_bulk)
2252 return desc->bd_nob_transferred;
2254 ctx = req->rq_cli_ctx;
2255 if (ctx->cc_ops->unwrap_bulk) {
2256 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2260 return desc->bd_nob_transferred;
2262 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2265 * This is called after unwrap the reply message.
2266 * return 0 for success or error code.
2268 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2269 struct ptlrpc_bulk_desc *desc)
2271 struct ptlrpc_cli_ctx *ctx;
2274 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2276 if (!req->rq_pack_bulk)
2279 ctx = req->rq_cli_ctx;
2280 if (ctx->cc_ops->unwrap_bulk) {
2281 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2287 * if everything is going right, nob should equals to nob_transferred.
2288 * in case of privacy mode, nob_transferred needs to be adjusted.
2290 if (desc->bd_nob != desc->bd_nob_transferred) {
2291 CERROR("nob %d doesn't match transferred nob %d",
2292 desc->bd_nob, desc->bd_nob_transferred);
2298 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2300 #ifdef HAVE_SERVER_SUPPORT
2302 * Performe transformation upon outgoing bulk read.
2304 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2305 struct ptlrpc_bulk_desc *desc)
2307 struct ptlrpc_svc_ctx *ctx;
2309 LASSERT(req->rq_bulk_read);
2311 if (!req->rq_pack_bulk)
2314 ctx = req->rq_svc_ctx;
2315 if (ctx->sc_policy->sp_sops->wrap_bulk)
2316 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2320 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2323 * Performe transformation upon incoming bulk write.
2325 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2326 struct ptlrpc_bulk_desc *desc)
2328 struct ptlrpc_svc_ctx *ctx;
2331 LASSERT(req->rq_bulk_write);
2334 * if it's in privacy mode, transferred should >= expected; otherwise
2335 * transferred should == expected.
2337 if (desc->bd_nob_transferred < desc->bd_nob ||
2338 (desc->bd_nob_transferred > desc->bd_nob &&
2339 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2340 SPTLRPC_BULK_SVC_PRIV)) {
2341 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2342 desc->bd_nob_transferred, desc->bd_nob);
2346 if (!req->rq_pack_bulk)
2349 ctx = req->rq_svc_ctx;
2350 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2351 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2353 CERROR("error unwrap bulk: %d\n", rc);
2356 /* return 0 to allow reply be sent */
2359 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2362 * Prepare buffers for incoming bulk write.
2364 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2365 struct ptlrpc_bulk_desc *desc)
2367 struct ptlrpc_svc_ctx *ctx;
2369 LASSERT(req->rq_bulk_write);
2371 if (!req->rq_pack_bulk)
2374 ctx = req->rq_svc_ctx;
2375 if (ctx->sc_policy->sp_sops->prep_bulk)
2376 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2380 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2382 #endif /* HAVE_SERVER_SUPPORT */
2384 /****************************************
2385 * user descriptor helpers *
2386 ****************************************/
2388 int sptlrpc_current_user_desc_size(void)
2393 ngroups = current_ngroups;
2395 if (ngroups > LUSTRE_MAX_GROUPS)
2396 ngroups = LUSTRE_MAX_GROUPS;
2400 return sptlrpc_user_desc_size(ngroups);
2402 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2404 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2406 struct ptlrpc_user_desc *pud;
2408 pud = lustre_msg_buf(msg, offset, 0);
2410 pud->pud_uid = cfs_curproc_uid();
2411 pud->pud_gid = cfs_curproc_gid();
2412 pud->pud_fsuid = cfs_curproc_fsuid();
2413 pud->pud_fsgid = cfs_curproc_fsgid();
2414 pud->pud_cap = cfs_curproc_cap_pack();
2415 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2419 if (pud->pud_ngroups > current_ngroups)
2420 pud->pud_ngroups = current_ngroups;
2421 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2422 pud->pud_ngroups * sizeof(__u32));
2423 task_unlock(current);
2428 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2430 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2432 struct ptlrpc_user_desc *pud;
2435 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2440 __swab32s(&pud->pud_uid);
2441 __swab32s(&pud->pud_gid);
2442 __swab32s(&pud->pud_fsuid);
2443 __swab32s(&pud->pud_fsgid);
2444 __swab32s(&pud->pud_cap);
2445 __swab32s(&pud->pud_ngroups);
2448 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2449 CERROR("%u groups is too large\n", pud->pud_ngroups);
2453 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2454 msg->lm_buflens[offset]) {
2455 CERROR("%u groups are claimed but bufsize only %u\n",
2456 pud->pud_ngroups, msg->lm_buflens[offset]);
2461 for (i = 0; i < pud->pud_ngroups; i++)
2462 __swab32s(&pud->pud_groups[i]);
2467 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2469 /****************************************
2471 ****************************************/
2473 const char * sec2target_str(struct ptlrpc_sec *sec)
2475 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2477 if (sec_is_reverse(sec))
2479 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2481 EXPORT_SYMBOL(sec2target_str);
2484 * return true if the bulk data is protected
2486 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2488 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2489 case SPTLRPC_BULK_SVC_INTG:
2490 case SPTLRPC_BULK_SVC_PRIV:
2496 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2498 /****************************************
2499 * crypto API helper/alloc blkciper *
2500 ****************************************/
2502 /****************************************
2503 * initialize/finalize *
2504 ****************************************/
2506 int sptlrpc_init(void)
2510 cfs_rwlock_init(&policy_lock);
2512 rc = sptlrpc_gc_init();
2516 rc = sptlrpc_conf_init();
2520 rc = sptlrpc_enc_pool_init();
2524 rc = sptlrpc_null_init();
2528 rc = sptlrpc_plain_init();
2532 rc = sptlrpc_lproc_init();
2539 sptlrpc_plain_fini();
2541 sptlrpc_null_fini();
2543 sptlrpc_enc_pool_fini();
2545 sptlrpc_conf_fini();
2552 void sptlrpc_fini(void)
2554 sptlrpc_lproc_fini();
2555 sptlrpc_plain_fini();
2556 sptlrpc_null_fini();
2557 sptlrpc_enc_pool_fini();
2558 sptlrpc_conf_fini();