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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Eric Mei <ericm@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_SEC
39 #include "../../include/linux/libcfs/libcfs.h"
40 #include <linux/crypto.h>
41 #include <linux/cred.h>
42 #include <linux/key.h>
44 #include "../include/obd.h"
45 #include "../include/obd_class.h"
46 #include "../include/obd_support.h"
47 #include "../include/lustre_net.h"
48 #include "../include/lustre_import.h"
49 #include "../include/lustre_dlm.h"
50 #include "../include/lustre_sec.h"
52 #include "ptlrpc_internal.h"
54 /***********************************************
56 ***********************************************/
58 static rwlock_t policy_lock
;
59 static struct ptlrpc_sec_policy
*policies
[SPTLRPC_POLICY_MAX
] = {
63 int sptlrpc_register_policy(struct ptlrpc_sec_policy
*policy
)
65 __u16 number
= policy
->sp_policy
;
67 LASSERT(policy
->sp_name
);
68 LASSERT(policy
->sp_cops
);
69 LASSERT(policy
->sp_sops
);
71 if (number
>= SPTLRPC_POLICY_MAX
)
74 write_lock(&policy_lock
);
75 if (unlikely(policies
[number
])) {
76 write_unlock(&policy_lock
);
79 policies
[number
] = policy
;
80 write_unlock(&policy_lock
);
82 CDEBUG(D_SEC
, "%s: registered\n", policy
->sp_name
);
85 EXPORT_SYMBOL(sptlrpc_register_policy
);
87 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy
*policy
)
89 __u16 number
= policy
->sp_policy
;
91 LASSERT(number
< SPTLRPC_POLICY_MAX
);
93 write_lock(&policy_lock
);
94 if (unlikely(!policies
[number
])) {
95 write_unlock(&policy_lock
);
96 CERROR("%s: already unregistered\n", policy
->sp_name
);
100 LASSERT(policies
[number
] == policy
);
101 policies
[number
] = NULL
;
102 write_unlock(&policy_lock
);
104 CDEBUG(D_SEC
, "%s: unregistered\n", policy
->sp_name
);
107 EXPORT_SYMBOL(sptlrpc_unregister_policy
);
110 struct ptlrpc_sec_policy
*sptlrpc_wireflavor2policy(__u32 flavor
)
112 static DEFINE_MUTEX(load_mutex
);
113 static atomic_t loaded
= ATOMIC_INIT(0);
114 struct ptlrpc_sec_policy
*policy
;
115 __u16 number
= SPTLRPC_FLVR_POLICY(flavor
);
118 if (number
>= SPTLRPC_POLICY_MAX
)
122 read_lock(&policy_lock
);
123 policy
= policies
[number
];
124 if (policy
&& !try_module_get(policy
->sp_owner
))
127 flag
= atomic_read(&loaded
);
128 read_unlock(&policy_lock
);
130 if (policy
|| flag
!= 0 ||
131 number
!= SPTLRPC_POLICY_GSS
)
134 /* try to load gss module, once */
135 mutex_lock(&load_mutex
);
136 if (atomic_read(&loaded
) == 0) {
137 if (request_module("ptlrpc_gss") == 0)
139 "module ptlrpc_gss loaded on demand\n");
141 CERROR("Unable to load module ptlrpc_gss\n");
143 atomic_set(&loaded
, 1);
145 mutex_unlock(&load_mutex
);
151 __u32
sptlrpc_name2flavor_base(const char *name
)
153 if (!strcmp(name
, "null"))
154 return SPTLRPC_FLVR_NULL
;
155 if (!strcmp(name
, "plain"))
156 return SPTLRPC_FLVR_PLAIN
;
157 if (!strcmp(name
, "krb5n"))
158 return SPTLRPC_FLVR_KRB5N
;
159 if (!strcmp(name
, "krb5a"))
160 return SPTLRPC_FLVR_KRB5A
;
161 if (!strcmp(name
, "krb5i"))
162 return SPTLRPC_FLVR_KRB5I
;
163 if (!strcmp(name
, "krb5p"))
164 return SPTLRPC_FLVR_KRB5P
;
166 return SPTLRPC_FLVR_INVALID
;
168 EXPORT_SYMBOL(sptlrpc_name2flavor_base
);
170 const char *sptlrpc_flavor2name_base(__u32 flvr
)
172 __u32 base
= SPTLRPC_FLVR_BASE(flvr
);
174 if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL
))
176 else if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN
))
178 else if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N
))
180 else if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A
))
182 else if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I
))
184 else if (base
== SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P
))
187 CERROR("invalid wire flavor 0x%x\n", flvr
);
190 EXPORT_SYMBOL(sptlrpc_flavor2name_base
);
192 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor
*sf
,
193 char *buf
, int bufsize
)
195 if (SPTLRPC_FLVR_POLICY(sf
->sf_rpc
) == SPTLRPC_POLICY_PLAIN
)
196 snprintf(buf
, bufsize
, "hash:%s",
197 sptlrpc_get_hash_name(sf
->u_bulk
.hash
.hash_alg
));
199 snprintf(buf
, bufsize
, "%s",
200 sptlrpc_flavor2name_base(sf
->sf_rpc
));
202 buf
[bufsize
- 1] = '\0';
205 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk
);
207 char *sptlrpc_flavor2name(struct sptlrpc_flavor
*sf
, char *buf
, int bufsize
)
209 strlcpy(buf
, sptlrpc_flavor2name_base(sf
->sf_rpc
), bufsize
);
212 * currently we don't support customized bulk specification for
213 * flavors other than plain
215 if (SPTLRPC_FLVR_POLICY(sf
->sf_rpc
) == SPTLRPC_POLICY_PLAIN
) {
219 sptlrpc_flavor2name_bulk(sf
, &bspec
[1], sizeof(bspec
) - 1);
220 strlcat(buf
, bspec
, bufsize
);
225 EXPORT_SYMBOL(sptlrpc_flavor2name
);
227 static char *sptlrpc_secflags2str(__u32 flags
, char *buf
, int bufsize
)
231 if (flags
& PTLRPC_SEC_FL_REVERSE
)
232 strlcat(buf
, "reverse,", bufsize
);
233 if (flags
& PTLRPC_SEC_FL_ROOTONLY
)
234 strlcat(buf
, "rootonly,", bufsize
);
235 if (flags
& PTLRPC_SEC_FL_UDESC
)
236 strlcat(buf
, "udesc,", bufsize
);
237 if (flags
& PTLRPC_SEC_FL_BULK
)
238 strlcat(buf
, "bulk,", bufsize
);
240 strlcat(buf
, "-,", bufsize
);
245 /**************************************************
246 * client context APIs *
247 **************************************************/
250 struct ptlrpc_cli_ctx
*get_my_ctx(struct ptlrpc_sec
*sec
)
252 struct vfs_cred vcred
;
253 int create
= 1, remove_dead
= 1;
256 LASSERT(sec
->ps_policy
->sp_cops
->lookup_ctx
);
258 if (sec
->ps_flvr
.sf_flags
& (PTLRPC_SEC_FL_REVERSE
|
259 PTLRPC_SEC_FL_ROOTONLY
)) {
262 if (sec
->ps_flvr
.sf_flags
& PTLRPC_SEC_FL_REVERSE
) {
267 vcred
.vc_uid
= from_kuid(&init_user_ns
, current_uid());
268 vcred
.vc_gid
= from_kgid(&init_user_ns
, current_gid());
271 return sec
->ps_policy
->sp_cops
->lookup_ctx(sec
, &vcred
,
272 create
, remove_dead
);
275 struct ptlrpc_cli_ctx
*sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx
*ctx
)
277 atomic_inc(&ctx
->cc_refcount
);
280 EXPORT_SYMBOL(sptlrpc_cli_ctx_get
);
282 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx
*ctx
, int sync
)
284 struct ptlrpc_sec
*sec
= ctx
->cc_sec
;
287 LASSERT_ATOMIC_POS(&ctx
->cc_refcount
);
289 if (!atomic_dec_and_test(&ctx
->cc_refcount
))
292 sec
->ps_policy
->sp_cops
->release_ctx(sec
, ctx
, sync
);
294 EXPORT_SYMBOL(sptlrpc_cli_ctx_put
);
296 static int import_sec_check_expire(struct obd_import
*imp
)
300 spin_lock(&imp
->imp_lock
);
301 if (imp
->imp_sec_expire
&&
302 imp
->imp_sec_expire
< ktime_get_real_seconds()) {
304 imp
->imp_sec_expire
= 0;
306 spin_unlock(&imp
->imp_lock
);
311 CDEBUG(D_SEC
, "found delayed sec adapt expired, do it now\n");
312 return sptlrpc_import_sec_adapt(imp
, NULL
, NULL
);
316 * Get and validate the client side ptlrpc security facilities from
317 * \a imp. There is a race condition on client reconnect when the import is
318 * being destroyed while there are outstanding client bound requests. In
319 * this case do not output any error messages if import secuity is not
322 * \param[in] imp obd import associated with client
323 * \param[out] sec client side ptlrpc security
325 * \retval 0 if security retrieved successfully
326 * \retval -ve errno if there was a problem
328 static int import_sec_validate_get(struct obd_import
*imp
,
329 struct ptlrpc_sec
**sec
)
333 if (unlikely(imp
->imp_sec_expire
)) {
334 rc
= import_sec_check_expire(imp
);
339 *sec
= sptlrpc_import_sec_ref(imp
);
340 /* Only output an error when the import is still active */
342 if (list_empty(&imp
->imp_zombie_chain
))
343 CERROR("import %p (%s) with no sec\n",
344 imp
, ptlrpc_import_state_name(imp
->imp_state
));
348 if (unlikely((*sec
)->ps_dying
)) {
349 CERROR("attempt to use dying sec %p\n", sec
);
350 sptlrpc_sec_put(*sec
);
358 * Given a \a req, find or allocate a appropriate context for it.
359 * \pre req->rq_cli_ctx == NULL.
361 * \retval 0 succeed, and req->rq_cli_ctx is set.
362 * \retval -ev error number, and req->rq_cli_ctx == NULL.
364 int sptlrpc_req_get_ctx(struct ptlrpc_request
*req
)
366 struct obd_import
*imp
= req
->rq_import
;
367 struct ptlrpc_sec
*sec
;
370 LASSERT(!req
->rq_cli_ctx
);
373 rc
= import_sec_validate_get(imp
, &sec
);
377 req
->rq_cli_ctx
= get_my_ctx(sec
);
379 sptlrpc_sec_put(sec
);
381 if (!req
->rq_cli_ctx
) {
382 CERROR("req %p: fail to get context\n", req
);
383 return -ECONNREFUSED
;
390 * Drop the context for \a req.
391 * \pre req->rq_cli_ctx != NULL.
392 * \post req->rq_cli_ctx == NULL.
394 * If \a sync == 0, this function should return quickly without sleep;
395 * otherwise it might trigger and wait for the whole process of sending
396 * an context-destroying rpc to server.
398 void sptlrpc_req_put_ctx(struct ptlrpc_request
*req
, int sync
)
401 LASSERT(req
->rq_cli_ctx
);
403 /* request might be asked to release earlier while still
404 * in the context waiting list.
406 if (!list_empty(&req
->rq_ctx_chain
)) {
407 spin_lock(&req
->rq_cli_ctx
->cc_lock
);
408 list_del_init(&req
->rq_ctx_chain
);
409 spin_unlock(&req
->rq_cli_ctx
->cc_lock
);
412 sptlrpc_cli_ctx_put(req
->rq_cli_ctx
, sync
);
413 req
->rq_cli_ctx
= NULL
;
417 int sptlrpc_req_ctx_switch(struct ptlrpc_request
*req
,
418 struct ptlrpc_cli_ctx
*oldctx
,
419 struct ptlrpc_cli_ctx
*newctx
)
421 struct sptlrpc_flavor old_flvr
;
422 char *reqmsg
= NULL
; /* to workaround old gcc */
426 LASSERT(req
->rq_reqmsg
);
427 LASSERT(req
->rq_reqlen
);
428 LASSERT(req
->rq_replen
);
430 CDEBUG(D_SEC
, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
432 oldctx
, oldctx
->cc_vcred
.vc_uid
, sec2target_str(oldctx
->cc_sec
),
433 newctx
, newctx
->cc_vcred
.vc_uid
, sec2target_str(newctx
->cc_sec
),
434 oldctx
->cc_sec
, oldctx
->cc_sec
->ps_policy
->sp_name
,
435 newctx
->cc_sec
, newctx
->cc_sec
->ps_policy
->sp_name
);
438 old_flvr
= req
->rq_flvr
;
440 /* save request message */
441 reqmsg_size
= req
->rq_reqlen
;
442 if (reqmsg_size
!= 0) {
443 reqmsg
= libcfs_kvzalloc(reqmsg_size
, GFP_NOFS
);
446 memcpy(reqmsg
, req
->rq_reqmsg
, reqmsg_size
);
449 /* release old req/rep buf */
450 req
->rq_cli_ctx
= oldctx
;
451 sptlrpc_cli_free_reqbuf(req
);
452 sptlrpc_cli_free_repbuf(req
);
453 req
->rq_cli_ctx
= newctx
;
455 /* recalculate the flavor */
456 sptlrpc_req_set_flavor(req
, 0);
458 /* alloc new request buffer
459 * we don't need to alloc reply buffer here, leave it to the
460 * rest procedure of ptlrpc
462 if (reqmsg_size
!= 0) {
463 rc
= sptlrpc_cli_alloc_reqbuf(req
, reqmsg_size
);
465 LASSERT(req
->rq_reqmsg
);
466 memcpy(req
->rq_reqmsg
, reqmsg
, reqmsg_size
);
468 CWARN("failed to alloc reqbuf: %d\n", rc
);
469 req
->rq_flvr
= old_flvr
;
478 * If current context of \a req is dead somehow, e.g. we just switched flavor
479 * thus marked original contexts dead, we'll find a new context for it. if
480 * no switch is needed, \a req will end up with the same context.
482 * \note a request must have a context, to keep other parts of code happy.
483 * In any case of failure during the switching, we must restore the old one.
485 static int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request
*req
)
487 struct ptlrpc_cli_ctx
*oldctx
= req
->rq_cli_ctx
;
488 struct ptlrpc_cli_ctx
*newctx
;
493 sptlrpc_cli_ctx_get(oldctx
);
494 sptlrpc_req_put_ctx(req
, 0);
496 rc
= sptlrpc_req_get_ctx(req
);
498 LASSERT(!req
->rq_cli_ctx
);
500 /* restore old ctx */
501 req
->rq_cli_ctx
= oldctx
;
505 newctx
= req
->rq_cli_ctx
;
508 if (unlikely(newctx
== oldctx
&&
509 test_bit(PTLRPC_CTX_DEAD_BIT
, &oldctx
->cc_flags
))) {
511 * still get the old dead ctx, usually means system too busy
514 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
515 newctx
, newctx
->cc_flags
);
517 set_current_state(TASK_INTERRUPTIBLE
);
518 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC
));
519 } else if (unlikely(!test_bit(PTLRPC_CTX_UPTODATE_BIT
, &newctx
->cc_flags
))) {
521 * new ctx not up to date yet
524 "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
525 newctx
, newctx
->cc_flags
);
528 * it's possible newctx == oldctx if we're switching
529 * subflavor with the same sec.
531 rc
= sptlrpc_req_ctx_switch(req
, oldctx
, newctx
);
533 /* restore old ctx */
534 sptlrpc_req_put_ctx(req
, 0);
535 req
->rq_cli_ctx
= oldctx
;
539 LASSERT(req
->rq_cli_ctx
== newctx
);
542 sptlrpc_cli_ctx_put(oldctx
, 1);
547 int ctx_check_refresh(struct ptlrpc_cli_ctx
*ctx
)
549 if (cli_ctx_is_refreshed(ctx
))
555 int ctx_refresh_timeout(void *data
)
557 struct ptlrpc_request
*req
= data
;
560 /* conn_cnt is needed in expire_one_request */
561 lustre_msg_set_conn_cnt(req
->rq_reqmsg
, req
->rq_import
->imp_conn_cnt
);
563 rc
= ptlrpc_expire_one_request(req
, 1);
564 /* if we started recovery, we should mark this ctx dead; otherwise
565 * in case of lgssd died nobody would retire this ctx, following
566 * connecting will still find the same ctx thus cause deadlock.
567 * there's an assumption that expire time of the request should be
568 * later than the context refresh expire time.
571 req
->rq_cli_ctx
->cc_ops
->force_die(req
->rq_cli_ctx
, 0);
576 void ctx_refresh_interrupt(void *data
)
578 struct ptlrpc_request
*req
= data
;
580 spin_lock(&req
->rq_lock
);
582 spin_unlock(&req
->rq_lock
);
586 void req_off_ctx_list(struct ptlrpc_request
*req
, struct ptlrpc_cli_ctx
*ctx
)
588 spin_lock(&ctx
->cc_lock
);
589 if (!list_empty(&req
->rq_ctx_chain
))
590 list_del_init(&req
->rq_ctx_chain
);
591 spin_unlock(&ctx
->cc_lock
);
595 * To refresh the context of \req, if it's not up-to-date.
598 * - = 0: wait until success or fatal error occur
599 * - > 0: timeout value (in seconds)
601 * The status of the context could be subject to be changed by other threads
602 * at any time. We allow this race, but once we return with 0, the caller will
603 * suppose it's uptodated and keep using it until the owning rpc is done.
605 * \retval 0 only if the context is uptodated.
606 * \retval -ev error number.
608 int sptlrpc_req_refresh_ctx(struct ptlrpc_request
*req
, long timeout
)
610 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
611 struct ptlrpc_sec
*sec
;
612 struct l_wait_info lwi
;
617 if (req
->rq_ctx_init
|| req
->rq_ctx_fini
)
621 * during the process a request's context might change type even
622 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
626 rc
= import_sec_validate_get(req
->rq_import
, &sec
);
630 if (sec
->ps_flvr
.sf_rpc
!= req
->rq_flvr
.sf_rpc
) {
631 CDEBUG(D_SEC
, "req %p: flavor has changed %x -> %x\n",
632 req
, req
->rq_flvr
.sf_rpc
, sec
->ps_flvr
.sf_rpc
);
633 req_off_ctx_list(req
, ctx
);
634 sptlrpc_req_replace_dead_ctx(req
);
635 ctx
= req
->rq_cli_ctx
;
637 sptlrpc_sec_put(sec
);
639 if (cli_ctx_is_eternal(ctx
))
642 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT
, &ctx
->cc_flags
))) {
643 LASSERT(ctx
->cc_ops
->refresh
);
644 ctx
->cc_ops
->refresh(ctx
);
646 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT
, &ctx
->cc_flags
) == 0);
648 LASSERT(ctx
->cc_ops
->validate
);
649 if (ctx
->cc_ops
->validate(ctx
) == 0) {
650 req_off_ctx_list(req
, ctx
);
654 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT
, &ctx
->cc_flags
))) {
655 spin_lock(&req
->rq_lock
);
657 spin_unlock(&req
->rq_lock
);
658 req_off_ctx_list(req
, ctx
);
663 * There's a subtle issue for resending RPCs, suppose following
665 * 1. the request was sent to server.
666 * 2. recovery was kicked start, after finished the request was
668 * 3. resend the request.
669 * 4. old reply from server received, we accept and verify the reply.
670 * this has to be success, otherwise the error will be aware
672 * 5. new reply from server received, dropped by LNet.
674 * Note the xid of old & new request is the same. We can't simply
675 * change xid for the resent request because the server replies on
676 * it for reply reconstruction.
678 * Commonly the original context should be uptodate because we
679 * have a expiry nice time; server will keep its context because
680 * we at least hold a ref of old context which prevent context
681 * destroying RPC being sent. So server still can accept the request
682 * and finish the RPC. But if that's not the case:
683 * 1. If server side context has been trimmed, a NO_CONTEXT will
684 * be returned, gss_cli_ctx_verify/unseal will switch to new
686 * 2. Current context never be refreshed, then we are fine: we
687 * never really send request with old context before.
689 if (test_bit(PTLRPC_CTX_UPTODATE_BIT
, &ctx
->cc_flags
) &&
690 unlikely(req
->rq_reqmsg
) &&
691 lustre_msg_get_flags(req
->rq_reqmsg
) & MSG_RESENT
) {
692 req_off_ctx_list(req
, ctx
);
696 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT
, &ctx
->cc_flags
))) {
697 req_off_ctx_list(req
, ctx
);
699 * don't switch ctx if import was deactivated
701 if (req
->rq_import
->imp_deactive
) {
702 spin_lock(&req
->rq_lock
);
704 spin_unlock(&req
->rq_lock
);
708 rc
= sptlrpc_req_replace_dead_ctx(req
);
710 LASSERT(ctx
== req
->rq_cli_ctx
);
711 CERROR("req %p: failed to replace dead ctx %p: %d\n",
713 spin_lock(&req
->rq_lock
);
715 spin_unlock(&req
->rq_lock
);
719 ctx
= req
->rq_cli_ctx
;
724 * Now we're sure this context is during upcall, add myself into
727 spin_lock(&ctx
->cc_lock
);
728 if (list_empty(&req
->rq_ctx_chain
))
729 list_add(&req
->rq_ctx_chain
, &ctx
->cc_req_list
);
730 spin_unlock(&ctx
->cc_lock
);
735 /* Clear any flags that may be present from previous sends */
736 LASSERT(req
->rq_receiving_reply
== 0);
737 spin_lock(&req
->rq_lock
);
739 req
->rq_timedout
= 0;
742 spin_unlock(&req
->rq_lock
);
744 lwi
= LWI_TIMEOUT_INTR(msecs_to_jiffies(timeout
* MSEC_PER_SEC
),
745 ctx_refresh_timeout
, ctx_refresh_interrupt
,
747 rc
= l_wait_event(req
->rq_reply_waitq
, ctx_check_refresh(ctx
), &lwi
);
750 * following cases could lead us here:
751 * - successfully refreshed;
753 * - timedout, and we don't want recover from the failure;
754 * - timedout, and waked up upon recovery finished;
755 * - someone else mark this ctx dead by force;
756 * - someone invalidate the req and call ptlrpc_client_wake_req(),
757 * e.g. ptlrpc_abort_inflight();
759 if (!cli_ctx_is_refreshed(ctx
)) {
760 /* timed out or interrupted */
761 req_off_ctx_list(req
, ctx
);
771 * Initialize flavor settings for \a req, according to \a opcode.
773 * \note this could be called in two situations:
774 * - new request from ptlrpc_pre_req(), with proper @opcode
775 * - old request which changed ctx in the middle, with @opcode == 0
777 void sptlrpc_req_set_flavor(struct ptlrpc_request
*req
, int opcode
)
779 struct ptlrpc_sec
*sec
;
781 LASSERT(req
->rq_import
);
782 LASSERT(req
->rq_cli_ctx
);
783 LASSERT(req
->rq_cli_ctx
->cc_sec
);
784 LASSERT(req
->rq_bulk_read
== 0 || req
->rq_bulk_write
== 0);
786 /* special security flags according to opcode */
790 case MGS_CONFIG_READ
:
792 req
->rq_bulk_read
= 1;
796 req
->rq_bulk_write
= 1;
799 req
->rq_ctx_init
= 1;
802 req
->rq_ctx_fini
= 1;
805 /* init/fini rpc won't be resend, so can't be here */
806 LASSERT(req
->rq_ctx_init
== 0);
807 LASSERT(req
->rq_ctx_fini
== 0);
809 /* cleanup flags, which should be recalculated */
810 req
->rq_pack_udesc
= 0;
811 req
->rq_pack_bulk
= 0;
815 sec
= req
->rq_cli_ctx
->cc_sec
;
817 spin_lock(&sec
->ps_lock
);
818 req
->rq_flvr
= sec
->ps_flvr
;
819 spin_unlock(&sec
->ps_lock
);
821 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
824 if (unlikely(req
->rq_ctx_init
))
825 flvr_set_svc(&req
->rq_flvr
.sf_rpc
, SPTLRPC_SVC_NULL
);
826 else if (unlikely(req
->rq_ctx_fini
))
827 flvr_set_svc(&req
->rq_flvr
.sf_rpc
, SPTLRPC_SVC_INTG
);
829 /* user descriptor flag, null security can't do it anyway */
830 if ((sec
->ps_flvr
.sf_flags
& PTLRPC_SEC_FL_UDESC
) &&
831 (req
->rq_flvr
.sf_rpc
!= SPTLRPC_FLVR_NULL
))
832 req
->rq_pack_udesc
= 1;
834 /* bulk security flag */
835 if ((req
->rq_bulk_read
|| req
->rq_bulk_write
) &&
836 sptlrpc_flavor_has_bulk(&req
->rq_flvr
))
837 req
->rq_pack_bulk
= 1;
840 void sptlrpc_request_out_callback(struct ptlrpc_request
*req
)
842 if (SPTLRPC_FLVR_SVC(req
->rq_flvr
.sf_rpc
) != SPTLRPC_SVC_PRIV
)
845 LASSERT(req
->rq_clrbuf
);
846 if (req
->rq_pool
|| !req
->rq_reqbuf
)
849 kfree(req
->rq_reqbuf
);
850 req
->rq_reqbuf
= NULL
;
851 req
->rq_reqbuf_len
= 0;
855 * Given an import \a imp, check whether current user has a valid context
856 * or not. We may create a new context and try to refresh it, and try
857 * repeatedly try in case of non-fatal errors. Return 0 means success.
859 int sptlrpc_import_check_ctx(struct obd_import
*imp
)
861 struct ptlrpc_sec
*sec
;
862 struct ptlrpc_cli_ctx
*ctx
;
863 struct ptlrpc_request
*req
= NULL
;
868 sec
= sptlrpc_import_sec_ref(imp
);
869 ctx
= get_my_ctx(sec
);
870 sptlrpc_sec_put(sec
);
875 if (cli_ctx_is_eternal(ctx
) ||
876 ctx
->cc_ops
->validate(ctx
) == 0) {
877 sptlrpc_cli_ctx_put(ctx
, 1);
881 if (cli_ctx_is_error(ctx
)) {
882 sptlrpc_cli_ctx_put(ctx
, 1);
886 req
= ptlrpc_request_cache_alloc(GFP_NOFS
);
890 ptlrpc_cli_req_init(req
);
891 atomic_set(&req
->rq_refcount
, 10000);
893 req
->rq_import
= imp
;
894 req
->rq_flvr
= sec
->ps_flvr
;
895 req
->rq_cli_ctx
= ctx
;
897 rc
= sptlrpc_req_refresh_ctx(req
, 0);
898 LASSERT(list_empty(&req
->rq_ctx_chain
));
899 sptlrpc_cli_ctx_put(req
->rq_cli_ctx
, 1);
900 ptlrpc_request_cache_free(req
);
906 * Used by ptlrpc client, to perform the pre-defined security transformation
907 * upon the request message of \a req. After this function called,
908 * req->rq_reqmsg is still accessible as clear text.
910 int sptlrpc_cli_wrap_request(struct ptlrpc_request
*req
)
912 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
916 LASSERT(ctx
->cc_sec
);
917 LASSERT(req
->rq_reqbuf
|| req
->rq_clrbuf
);
919 /* we wrap bulk request here because now we can be sure
920 * the context is uptodate.
923 rc
= sptlrpc_cli_wrap_bulk(req
, req
->rq_bulk
);
928 switch (SPTLRPC_FLVR_SVC(req
->rq_flvr
.sf_rpc
)) {
929 case SPTLRPC_SVC_NULL
:
930 case SPTLRPC_SVC_AUTH
:
931 case SPTLRPC_SVC_INTG
:
932 LASSERT(ctx
->cc_ops
->sign
);
933 rc
= ctx
->cc_ops
->sign(ctx
, req
);
935 case SPTLRPC_SVC_PRIV
:
936 LASSERT(ctx
->cc_ops
->seal
);
937 rc
= ctx
->cc_ops
->seal(ctx
, req
);
944 LASSERT(req
->rq_reqdata_len
);
945 LASSERT(req
->rq_reqdata_len
% 8 == 0);
946 LASSERT(req
->rq_reqdata_len
<= req
->rq_reqbuf_len
);
952 static int do_cli_unwrap_reply(struct ptlrpc_request
*req
)
954 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
958 LASSERT(ctx
->cc_sec
);
959 LASSERT(req
->rq_repbuf
);
960 LASSERT(req
->rq_repdata
);
961 LASSERT(!req
->rq_repmsg
);
963 req
->rq_rep_swab_mask
= 0;
965 rc
= __lustre_unpack_msg(req
->rq_repdata
, req
->rq_repdata_len
);
968 lustre_set_rep_swabbed(req
, MSG_PTLRPC_HEADER_OFF
);
972 CERROR("failed unpack reply: x%llu\n", req
->rq_xid
);
976 if (req
->rq_repdata_len
< sizeof(struct lustre_msg
)) {
977 CERROR("replied data length %d too small\n",
978 req
->rq_repdata_len
);
982 if (SPTLRPC_FLVR_POLICY(req
->rq_repdata
->lm_secflvr
) !=
983 SPTLRPC_FLVR_POLICY(req
->rq_flvr
.sf_rpc
)) {
984 CERROR("reply policy %u doesn't match request policy %u\n",
985 SPTLRPC_FLVR_POLICY(req
->rq_repdata
->lm_secflvr
),
986 SPTLRPC_FLVR_POLICY(req
->rq_flvr
.sf_rpc
));
990 switch (SPTLRPC_FLVR_SVC(req
->rq_flvr
.sf_rpc
)) {
991 case SPTLRPC_SVC_NULL
:
992 case SPTLRPC_SVC_AUTH
:
993 case SPTLRPC_SVC_INTG
:
994 LASSERT(ctx
->cc_ops
->verify
);
995 rc
= ctx
->cc_ops
->verify(ctx
, req
);
997 case SPTLRPC_SVC_PRIV
:
998 LASSERT(ctx
->cc_ops
->unseal
);
999 rc
= ctx
->cc_ops
->unseal(ctx
, req
);
1004 LASSERT(rc
|| req
->rq_repmsg
|| req
->rq_resend
);
1006 if (SPTLRPC_FLVR_POLICY(req
->rq_flvr
.sf_rpc
) != SPTLRPC_POLICY_NULL
&&
1008 req
->rq_rep_swab_mask
= 0;
1013 * Used by ptlrpc client, to perform security transformation upon the reply
1014 * message of \a req. After return successfully, req->rq_repmsg points to
1015 * the reply message in clear text.
1017 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1020 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request
*req
)
1022 LASSERT(req
->rq_repbuf
);
1023 LASSERT(!req
->rq_repdata
);
1024 LASSERT(!req
->rq_repmsg
);
1025 LASSERT(req
->rq_reply_off
+ req
->rq_nob_received
<= req
->rq_repbuf_len
);
1027 if (req
->rq_reply_off
== 0 &&
1028 (lustre_msghdr_get_flags(req
->rq_reqmsg
) & MSGHDR_AT_SUPPORT
)) {
1029 CERROR("real reply with offset 0\n");
1033 if (req
->rq_reply_off
% 8 != 0) {
1034 CERROR("reply at odd offset %u\n", req
->rq_reply_off
);
1038 req
->rq_repdata
= (struct lustre_msg
*)
1039 (req
->rq_repbuf
+ req
->rq_reply_off
);
1040 req
->rq_repdata_len
= req
->rq_nob_received
;
1042 return do_cli_unwrap_reply(req
);
1046 * Used by ptlrpc client, to perform security transformation upon the early
1047 * reply message of \a req. We expect the rq_reply_off is 0, and
1048 * rq_nob_received is the early reply size.
1050 * Because the receive buffer might be still posted, the reply data might be
1051 * changed at any time, no matter we're holding rq_lock or not. For this reason
1052 * we allocate a separate ptlrpc_request and reply buffer for early reply
1055 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1056 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1057 * \a *req_ret to release it.
1058 * \retval -ev error number, and \a req_ret will not be set.
1060 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request
*req
,
1061 struct ptlrpc_request
**req_ret
)
1063 struct ptlrpc_request
*early_req
;
1065 int early_bufsz
, early_size
;
1068 early_req
= ptlrpc_request_cache_alloc(GFP_NOFS
);
1072 ptlrpc_cli_req_init(early_req
);
1074 early_size
= req
->rq_nob_received
;
1075 early_bufsz
= size_roundup_power2(early_size
);
1076 early_buf
= libcfs_kvzalloc(early_bufsz
, GFP_NOFS
);
1082 /* sanity checkings and copy data out, do it inside spinlock */
1083 spin_lock(&req
->rq_lock
);
1085 if (req
->rq_replied
) {
1086 spin_unlock(&req
->rq_lock
);
1091 LASSERT(req
->rq_repbuf
);
1092 LASSERT(!req
->rq_repdata
);
1093 LASSERT(!req
->rq_repmsg
);
1095 if (req
->rq_reply_off
!= 0) {
1096 CERROR("early reply with offset %u\n", req
->rq_reply_off
);
1097 spin_unlock(&req
->rq_lock
);
1102 if (req
->rq_nob_received
!= early_size
) {
1103 /* even another early arrived the size should be the same */
1104 CERROR("data size has changed from %u to %u\n",
1105 early_size
, req
->rq_nob_received
);
1106 spin_unlock(&req
->rq_lock
);
1111 if (req
->rq_nob_received
< sizeof(struct lustre_msg
)) {
1112 CERROR("early reply length %d too small\n",
1113 req
->rq_nob_received
);
1114 spin_unlock(&req
->rq_lock
);
1119 memcpy(early_buf
, req
->rq_repbuf
, early_size
);
1120 spin_unlock(&req
->rq_lock
);
1122 early_req
->rq_cli_ctx
= sptlrpc_cli_ctx_get(req
->rq_cli_ctx
);
1123 early_req
->rq_flvr
= req
->rq_flvr
;
1124 early_req
->rq_repbuf
= early_buf
;
1125 early_req
->rq_repbuf_len
= early_bufsz
;
1126 early_req
->rq_repdata
= (struct lustre_msg
*)early_buf
;
1127 early_req
->rq_repdata_len
= early_size
;
1128 early_req
->rq_early
= 1;
1129 early_req
->rq_reqmsg
= req
->rq_reqmsg
;
1131 rc
= do_cli_unwrap_reply(early_req
);
1133 DEBUG_REQ(D_ADAPTTO
, early_req
,
1134 "error %d unwrap early reply", rc
);
1138 LASSERT(early_req
->rq_repmsg
);
1139 *req_ret
= early_req
;
1143 sptlrpc_cli_ctx_put(early_req
->rq_cli_ctx
, 1);
1147 ptlrpc_request_cache_free(early_req
);
1152 * Used by ptlrpc client, to release a processed early reply \a early_req.
1154 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1156 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request
*early_req
)
1158 LASSERT(early_req
->rq_repbuf
);
1159 LASSERT(early_req
->rq_repdata
);
1160 LASSERT(early_req
->rq_repmsg
);
1162 sptlrpc_cli_ctx_put(early_req
->rq_cli_ctx
, 1);
1163 kvfree(early_req
->rq_repbuf
);
1164 ptlrpc_request_cache_free(early_req
);
1167 /**************************************************
1169 **************************************************/
1172 * "fixed" sec (e.g. null) use sec_id < 0
1174 static atomic_t sptlrpc_sec_id
= ATOMIC_INIT(1);
1176 int sptlrpc_get_next_secid(void)
1178 return atomic_inc_return(&sptlrpc_sec_id
);
1180 EXPORT_SYMBOL(sptlrpc_get_next_secid
);
1182 /**************************************************
1183 * client side high-level security APIs *
1184 **************************************************/
1186 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec
*sec
, uid_t uid
,
1187 int grace
, int force
)
1189 struct ptlrpc_sec_policy
*policy
= sec
->ps_policy
;
1191 LASSERT(policy
->sp_cops
);
1192 LASSERT(policy
->sp_cops
->flush_ctx_cache
);
1194 return policy
->sp_cops
->flush_ctx_cache(sec
, uid
, grace
, force
);
1197 static void sec_cop_destroy_sec(struct ptlrpc_sec
*sec
)
1199 struct ptlrpc_sec_policy
*policy
= sec
->ps_policy
;
1201 LASSERT_ATOMIC_ZERO(&sec
->ps_refcount
);
1202 LASSERT_ATOMIC_ZERO(&sec
->ps_nctx
);
1203 LASSERT(policy
->sp_cops
->destroy_sec
);
1205 CDEBUG(D_SEC
, "%s@%p: being destroyed\n", sec
->ps_policy
->sp_name
, sec
);
1207 policy
->sp_cops
->destroy_sec(sec
);
1208 sptlrpc_policy_put(policy
);
1211 static void sptlrpc_sec_kill(struct ptlrpc_sec
*sec
)
1213 LASSERT_ATOMIC_POS(&sec
->ps_refcount
);
1215 if (sec
->ps_policy
->sp_cops
->kill_sec
) {
1216 sec
->ps_policy
->sp_cops
->kill_sec(sec
);
1218 sec_cop_flush_ctx_cache(sec
, -1, 1, 1);
1222 static struct ptlrpc_sec
*sptlrpc_sec_get(struct ptlrpc_sec
*sec
)
1225 atomic_inc(&sec
->ps_refcount
);
1230 void sptlrpc_sec_put(struct ptlrpc_sec
*sec
)
1233 LASSERT_ATOMIC_POS(&sec
->ps_refcount
);
1235 if (atomic_dec_and_test(&sec
->ps_refcount
)) {
1236 sptlrpc_gc_del_sec(sec
);
1237 sec_cop_destroy_sec(sec
);
1241 EXPORT_SYMBOL(sptlrpc_sec_put
);
1244 * policy module is responsible for taking reference of import
1247 struct ptlrpc_sec
*sptlrpc_sec_create(struct obd_import
*imp
,
1248 struct ptlrpc_svc_ctx
*svc_ctx
,
1249 struct sptlrpc_flavor
*sf
,
1250 enum lustre_sec_part sp
)
1252 struct ptlrpc_sec_policy
*policy
;
1253 struct ptlrpc_sec
*sec
;
1257 LASSERT(imp
->imp_dlm_fake
== 1);
1259 CDEBUG(D_SEC
, "%s %s: reverse sec using flavor %s\n",
1260 imp
->imp_obd
->obd_type
->typ_name
,
1261 imp
->imp_obd
->obd_name
,
1262 sptlrpc_flavor2name(sf
, str
, sizeof(str
)));
1264 policy
= sptlrpc_policy_get(svc_ctx
->sc_policy
);
1265 sf
->sf_flags
|= PTLRPC_SEC_FL_REVERSE
| PTLRPC_SEC_FL_ROOTONLY
;
1267 LASSERT(imp
->imp_dlm_fake
== 0);
1269 CDEBUG(D_SEC
, "%s %s: select security flavor %s\n",
1270 imp
->imp_obd
->obd_type
->typ_name
,
1271 imp
->imp_obd
->obd_name
,
1272 sptlrpc_flavor2name(sf
, str
, sizeof(str
)));
1274 policy
= sptlrpc_wireflavor2policy(sf
->sf_rpc
);
1276 CERROR("invalid flavor 0x%x\n", sf
->sf_rpc
);
1281 sec
= policy
->sp_cops
->create_sec(imp
, svc_ctx
, sf
);
1283 atomic_inc(&sec
->ps_refcount
);
1287 if (sec
->ps_gc_interval
&& policy
->sp_cops
->gc_ctx
)
1288 sptlrpc_gc_add_sec(sec
);
1290 sptlrpc_policy_put(policy
);
1296 struct ptlrpc_sec
*sptlrpc_import_sec_ref(struct obd_import
*imp
)
1298 struct ptlrpc_sec
*sec
;
1300 spin_lock(&imp
->imp_lock
);
1301 sec
= sptlrpc_sec_get(imp
->imp_sec
);
1302 spin_unlock(&imp
->imp_lock
);
1306 EXPORT_SYMBOL(sptlrpc_import_sec_ref
);
1308 static void sptlrpc_import_sec_install(struct obd_import
*imp
,
1309 struct ptlrpc_sec
*sec
)
1311 struct ptlrpc_sec
*old_sec
;
1313 LASSERT_ATOMIC_POS(&sec
->ps_refcount
);
1315 spin_lock(&imp
->imp_lock
);
1316 old_sec
= imp
->imp_sec
;
1318 spin_unlock(&imp
->imp_lock
);
1321 sptlrpc_sec_kill(old_sec
);
1323 /* balance the ref taken by this import */
1324 sptlrpc_sec_put(old_sec
);
1329 int flavor_equal(struct sptlrpc_flavor
*sf1
, struct sptlrpc_flavor
*sf2
)
1331 return (memcmp(sf1
, sf2
, sizeof(*sf1
)) == 0);
1335 void flavor_copy(struct sptlrpc_flavor
*dst
, struct sptlrpc_flavor
*src
)
1340 static void sptlrpc_import_sec_adapt_inplace(struct obd_import
*imp
,
1341 struct ptlrpc_sec
*sec
,
1342 struct sptlrpc_flavor
*sf
)
1344 char str1
[32], str2
[32];
1346 if (sec
->ps_flvr
.sf_flags
!= sf
->sf_flags
)
1347 CDEBUG(D_SEC
, "changing sec flags: %s -> %s\n",
1348 sptlrpc_secflags2str(sec
->ps_flvr
.sf_flags
,
1349 str1
, sizeof(str1
)),
1350 sptlrpc_secflags2str(sf
->sf_flags
,
1351 str2
, sizeof(str2
)));
1353 spin_lock(&sec
->ps_lock
);
1354 flavor_copy(&sec
->ps_flvr
, sf
);
1355 spin_unlock(&sec
->ps_lock
);
1359 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1360 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1362 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1363 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1365 int sptlrpc_import_sec_adapt(struct obd_import
*imp
,
1366 struct ptlrpc_svc_ctx
*svc_ctx
,
1367 struct sptlrpc_flavor
*flvr
)
1369 struct ptlrpc_connection
*conn
;
1370 struct sptlrpc_flavor sf
;
1371 struct ptlrpc_sec
*sec
, *newsec
;
1372 enum lustre_sec_part sp
;
1381 conn
= imp
->imp_connection
;
1384 struct client_obd
*cliobd
= &imp
->imp_obd
->u
.cli
;
1386 * normal import, determine flavor from rule set, except
1387 * for mgc the flavor is predetermined.
1389 if (cliobd
->cl_sp_me
== LUSTRE_SP_MGC
)
1390 sf
= cliobd
->cl_flvr_mgc
;
1392 sptlrpc_conf_choose_flavor(cliobd
->cl_sp_me
,
1394 &cliobd
->cl_target_uuid
,
1397 sp
= imp
->imp_obd
->u
.cli
.cl_sp_me
;
1399 /* reverse import, determine flavor from incoming request */
1402 if (sf
.sf_rpc
!= SPTLRPC_FLVR_NULL
)
1403 sf
.sf_flags
= PTLRPC_SEC_FL_REVERSE
|
1404 PTLRPC_SEC_FL_ROOTONLY
;
1406 sp
= sptlrpc_target_sec_part(imp
->imp_obd
);
1409 sec
= sptlrpc_import_sec_ref(imp
);
1413 if (flavor_equal(&sf
, &sec
->ps_flvr
))
1416 CDEBUG(D_SEC
, "import %s->%s: changing flavor %s -> %s\n",
1417 imp
->imp_obd
->obd_name
,
1418 obd_uuid2str(&conn
->c_remote_uuid
),
1419 sptlrpc_flavor2name(&sec
->ps_flvr
, str
, sizeof(str
)),
1420 sptlrpc_flavor2name(&sf
, str2
, sizeof(str2
)));
1422 if (SPTLRPC_FLVR_POLICY(sf
.sf_rpc
) ==
1423 SPTLRPC_FLVR_POLICY(sec
->ps_flvr
.sf_rpc
) &&
1424 SPTLRPC_FLVR_MECH(sf
.sf_rpc
) ==
1425 SPTLRPC_FLVR_MECH(sec
->ps_flvr
.sf_rpc
)) {
1426 sptlrpc_import_sec_adapt_inplace(imp
, sec
, &sf
);
1429 } else if (SPTLRPC_FLVR_BASE(sf
.sf_rpc
) !=
1430 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL
)) {
1431 CDEBUG(D_SEC
, "import %s->%s netid %x: select flavor %s\n",
1432 imp
->imp_obd
->obd_name
,
1433 obd_uuid2str(&conn
->c_remote_uuid
),
1434 LNET_NIDNET(conn
->c_self
),
1435 sptlrpc_flavor2name(&sf
, str
, sizeof(str
)));
1438 mutex_lock(&imp
->imp_sec_mutex
);
1440 newsec
= sptlrpc_sec_create(imp
, svc_ctx
, &sf
, sp
);
1442 sptlrpc_import_sec_install(imp
, newsec
);
1444 CERROR("import %s->%s: failed to create new sec\n",
1445 imp
->imp_obd
->obd_name
,
1446 obd_uuid2str(&conn
->c_remote_uuid
));
1450 mutex_unlock(&imp
->imp_sec_mutex
);
1452 sptlrpc_sec_put(sec
);
1456 void sptlrpc_import_sec_put(struct obd_import
*imp
)
1459 sptlrpc_sec_kill(imp
->imp_sec
);
1461 sptlrpc_sec_put(imp
->imp_sec
);
1462 imp
->imp_sec
= NULL
;
1466 static void import_flush_ctx_common(struct obd_import
*imp
,
1467 uid_t uid
, int grace
, int force
)
1469 struct ptlrpc_sec
*sec
;
1474 sec
= sptlrpc_import_sec_ref(imp
);
1478 sec_cop_flush_ctx_cache(sec
, uid
, grace
, force
);
1479 sptlrpc_sec_put(sec
);
1482 void sptlrpc_import_flush_my_ctx(struct obd_import
*imp
)
1484 import_flush_ctx_common(imp
, from_kuid(&init_user_ns
, current_uid()),
1487 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx
);
1489 void sptlrpc_import_flush_all_ctx(struct obd_import
*imp
)
1491 import_flush_ctx_common(imp
, -1, 1, 1);
1493 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx
);
1496 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1497 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1499 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request
*req
, int msgsize
)
1501 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
1502 struct ptlrpc_sec_policy
*policy
;
1506 LASSERT(ctx
->cc_sec
);
1507 LASSERT(ctx
->cc_sec
->ps_policy
);
1508 LASSERT(!req
->rq_reqmsg
);
1509 LASSERT_ATOMIC_POS(&ctx
->cc_refcount
);
1511 policy
= ctx
->cc_sec
->ps_policy
;
1512 rc
= policy
->sp_cops
->alloc_reqbuf(ctx
->cc_sec
, req
, msgsize
);
1514 LASSERT(req
->rq_reqmsg
);
1515 LASSERT(req
->rq_reqbuf
|| req
->rq_clrbuf
);
1517 /* zeroing preallocated buffer */
1519 memset(req
->rq_reqmsg
, 0, msgsize
);
1526 * Used by ptlrpc client to free request buffer of \a req. After this
1527 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1529 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request
*req
)
1531 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
1532 struct ptlrpc_sec_policy
*policy
;
1535 LASSERT(ctx
->cc_sec
);
1536 LASSERT(ctx
->cc_sec
->ps_policy
);
1537 LASSERT_ATOMIC_POS(&ctx
->cc_refcount
);
1539 if (!req
->rq_reqbuf
&& !req
->rq_clrbuf
)
1542 policy
= ctx
->cc_sec
->ps_policy
;
1543 policy
->sp_cops
->free_reqbuf(ctx
->cc_sec
, req
);
1544 req
->rq_reqmsg
= NULL
;
1548 * NOTE caller must guarantee the buffer size is enough for the enlargement
1550 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg
*msg
,
1551 int segment
, int newsize
)
1554 int oldsize
, oldmsg_size
, movesize
;
1556 LASSERT(segment
< msg
->lm_bufcount
);
1557 LASSERT(msg
->lm_buflens
[segment
] <= newsize
);
1559 if (msg
->lm_buflens
[segment
] == newsize
)
1562 /* nothing to do if we are enlarging the last segment */
1563 if (segment
== msg
->lm_bufcount
- 1) {
1564 msg
->lm_buflens
[segment
] = newsize
;
1568 oldsize
= msg
->lm_buflens
[segment
];
1570 src
= lustre_msg_buf(msg
, segment
+ 1, 0);
1571 msg
->lm_buflens
[segment
] = newsize
;
1572 dst
= lustre_msg_buf(msg
, segment
+ 1, 0);
1573 msg
->lm_buflens
[segment
] = oldsize
;
1575 /* move from segment + 1 to end segment */
1576 LASSERT(msg
->lm_magic
== LUSTRE_MSG_MAGIC_V2
);
1577 oldmsg_size
= lustre_msg_size_v2(msg
->lm_bufcount
, msg
->lm_buflens
);
1578 movesize
= oldmsg_size
- ((unsigned long)src
- (unsigned long)msg
);
1579 LASSERT(movesize
>= 0);
1582 memmove(dst
, src
, movesize
);
1584 /* note we don't clear the ares where old data live, not secret */
1586 /* finally set new segment size */
1587 msg
->lm_buflens
[segment
] = newsize
;
1589 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace
);
1592 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1593 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1594 * preserved after the enlargement. this must be called after original request
1595 * buffer being allocated.
1597 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1598 * so caller should refresh its local pointers if needed.
1600 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request
*req
,
1601 int segment
, int newsize
)
1603 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
1604 struct ptlrpc_sec_cops
*cops
;
1605 struct lustre_msg
*msg
= req
->rq_reqmsg
;
1609 LASSERT(msg
->lm_bufcount
> segment
);
1610 LASSERT(msg
->lm_buflens
[segment
] <= newsize
);
1612 if (msg
->lm_buflens
[segment
] == newsize
)
1615 cops
= ctx
->cc_sec
->ps_policy
->sp_cops
;
1616 LASSERT(cops
->enlarge_reqbuf
);
1617 return cops
->enlarge_reqbuf(ctx
->cc_sec
, req
, segment
, newsize
);
1619 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf
);
1622 * Used by ptlrpc client to allocate reply buffer of \a req.
1624 * \note After this, req->rq_repmsg is still not accessible.
1626 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request
*req
, int msgsize
)
1628 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
1629 struct ptlrpc_sec_policy
*policy
;
1632 LASSERT(ctx
->cc_sec
);
1633 LASSERT(ctx
->cc_sec
->ps_policy
);
1638 policy
= ctx
->cc_sec
->ps_policy
;
1639 return policy
->sp_cops
->alloc_repbuf(ctx
->cc_sec
, req
, msgsize
);
1643 * Used by ptlrpc client to free reply buffer of \a req. After this
1644 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1646 void sptlrpc_cli_free_repbuf(struct ptlrpc_request
*req
)
1648 struct ptlrpc_cli_ctx
*ctx
= req
->rq_cli_ctx
;
1649 struct ptlrpc_sec_policy
*policy
;
1652 LASSERT(ctx
->cc_sec
);
1653 LASSERT(ctx
->cc_sec
->ps_policy
);
1654 LASSERT_ATOMIC_POS(&ctx
->cc_refcount
);
1656 if (!req
->rq_repbuf
)
1658 LASSERT(req
->rq_repbuf_len
);
1660 policy
= ctx
->cc_sec
->ps_policy
;
1661 policy
->sp_cops
->free_repbuf(ctx
->cc_sec
, req
);
1662 req
->rq_repmsg
= NULL
;
1665 static int sptlrpc_svc_install_rvs_ctx(struct obd_import
*imp
,
1666 struct ptlrpc_svc_ctx
*ctx
)
1668 struct ptlrpc_sec_policy
*policy
= ctx
->sc_policy
;
1670 if (!policy
->sp_sops
->install_rctx
)
1672 return policy
->sp_sops
->install_rctx(imp
, ctx
);
1675 /****************************************
1676 * server side security *
1677 ****************************************/
1679 static int flavor_allowed(struct sptlrpc_flavor
*exp
,
1680 struct ptlrpc_request
*req
)
1682 struct sptlrpc_flavor
*flvr
= &req
->rq_flvr
;
1684 if (exp
->sf_rpc
== SPTLRPC_FLVR_ANY
|| exp
->sf_rpc
== flvr
->sf_rpc
)
1687 if ((req
->rq_ctx_init
|| req
->rq_ctx_fini
) &&
1688 SPTLRPC_FLVR_POLICY(exp
->sf_rpc
) ==
1689 SPTLRPC_FLVR_POLICY(flvr
->sf_rpc
) &&
1690 SPTLRPC_FLVR_MECH(exp
->sf_rpc
) == SPTLRPC_FLVR_MECH(flvr
->sf_rpc
))
1696 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1699 * Given an export \a exp, check whether the flavor of incoming \a req
1700 * is allowed by the export \a exp. Main logic is about taking care of
1701 * changing configurations. Return 0 means success.
1703 int sptlrpc_target_export_check(struct obd_export
*exp
,
1704 struct ptlrpc_request
*req
)
1706 struct sptlrpc_flavor flavor
;
1711 /* client side export has no imp_reverse, skip
1712 * FIXME maybe we should check flavor this as well???
1714 if (!exp
->exp_imp_reverse
)
1717 /* don't care about ctx fini rpc */
1718 if (req
->rq_ctx_fini
)
1721 spin_lock(&exp
->exp_lock
);
1723 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1724 * the first req with the new flavor, then treat it as current flavor,
1725 * adapt reverse sec according to it.
1726 * note the first rpc with new flavor might not be with root ctx, in
1727 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1.
1729 if (unlikely(exp
->exp_flvr_changed
) &&
1730 flavor_allowed(&exp
->exp_flvr_old
[1], req
)) {
1731 /* make the new flavor as "current", and old ones as
1734 CDEBUG(D_SEC
, "exp %p: just changed: %x->%x\n", exp
,
1735 exp
->exp_flvr
.sf_rpc
, exp
->exp_flvr_old
[1].sf_rpc
);
1736 flavor
= exp
->exp_flvr_old
[1];
1737 exp
->exp_flvr_old
[1] = exp
->exp_flvr_old
[0];
1738 exp
->exp_flvr_expire
[1] = exp
->exp_flvr_expire
[0];
1739 exp
->exp_flvr_old
[0] = exp
->exp_flvr
;
1740 exp
->exp_flvr_expire
[0] = ktime_get_real_seconds() +
1741 EXP_FLVR_UPDATE_EXPIRE
;
1742 exp
->exp_flvr
= flavor
;
1744 /* flavor change finished */
1745 exp
->exp_flvr_changed
= 0;
1746 LASSERT(exp
->exp_flvr_adapt
== 1);
1748 /* if it's gss, we only interested in root ctx init */
1749 if (req
->rq_auth_gss
&&
1750 !(req
->rq_ctx_init
&&
1751 (req
->rq_auth_usr_root
|| req
->rq_auth_usr_mdt
||
1752 req
->rq_auth_usr_ost
))) {
1753 spin_unlock(&exp
->exp_lock
);
1754 CDEBUG(D_SEC
, "is good but not root(%d:%d:%d:%d:%d)\n",
1755 req
->rq_auth_gss
, req
->rq_ctx_init
,
1756 req
->rq_auth_usr_root
, req
->rq_auth_usr_mdt
,
1757 req
->rq_auth_usr_ost
);
1761 exp
->exp_flvr_adapt
= 0;
1762 spin_unlock(&exp
->exp_lock
);
1764 return sptlrpc_import_sec_adapt(exp
->exp_imp_reverse
,
1765 req
->rq_svc_ctx
, &flavor
);
1768 /* if it equals to the current flavor, we accept it, but need to
1769 * dealing with reverse sec/ctx
1771 if (likely(flavor_allowed(&exp
->exp_flvr
, req
))) {
1772 /* most cases should return here, we only interested in
1775 if (!req
->rq_auth_gss
|| !req
->rq_ctx_init
||
1776 (!req
->rq_auth_usr_root
&& !req
->rq_auth_usr_mdt
&&
1777 !req
->rq_auth_usr_ost
)) {
1778 spin_unlock(&exp
->exp_lock
);
1782 /* if flavor just changed, we should not proceed, just leave
1783 * it and current flavor will be discovered and replaced
1784 * shortly, and let _this_ rpc pass through
1786 if (exp
->exp_flvr_changed
) {
1787 LASSERT(exp
->exp_flvr_adapt
);
1788 spin_unlock(&exp
->exp_lock
);
1792 if (exp
->exp_flvr_adapt
) {
1793 exp
->exp_flvr_adapt
= 0;
1794 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): do delayed adapt\n",
1795 exp
, exp
->exp_flvr
.sf_rpc
,
1796 exp
->exp_flvr_old
[0].sf_rpc
,
1797 exp
->exp_flvr_old
[1].sf_rpc
);
1798 flavor
= exp
->exp_flvr
;
1799 spin_unlock(&exp
->exp_lock
);
1801 return sptlrpc_import_sec_adapt(exp
->exp_imp_reverse
,
1805 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
1806 exp
, exp
->exp_flvr
.sf_rpc
,
1807 exp
->exp_flvr_old
[0].sf_rpc
,
1808 exp
->exp_flvr_old
[1].sf_rpc
);
1809 spin_unlock(&exp
->exp_lock
);
1811 return sptlrpc_svc_install_rvs_ctx(exp
->exp_imp_reverse
,
1816 if (exp
->exp_flvr_expire
[0]) {
1817 if (exp
->exp_flvr_expire
[0] >= ktime_get_real_seconds()) {
1818 if (flavor_allowed(&exp
->exp_flvr_old
[0], req
)) {
1819 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): match the middle one (%lld)\n", exp
,
1820 exp
->exp_flvr
.sf_rpc
,
1821 exp
->exp_flvr_old
[0].sf_rpc
,
1822 exp
->exp_flvr_old
[1].sf_rpc
,
1823 (s64
)(exp
->exp_flvr_expire
[0] -
1824 ktime_get_real_seconds()));
1825 spin_unlock(&exp
->exp_lock
);
1829 CDEBUG(D_SEC
, "mark middle expired\n");
1830 exp
->exp_flvr_expire
[0] = 0;
1832 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): %x not match middle\n", exp
,
1833 exp
->exp_flvr
.sf_rpc
,
1834 exp
->exp_flvr_old
[0].sf_rpc
, exp
->exp_flvr_old
[1].sf_rpc
,
1835 req
->rq_flvr
.sf_rpc
);
1838 /* now it doesn't match the current flavor, the only chance we can
1839 * accept it is match the old flavors which is not expired.
1841 if (exp
->exp_flvr_changed
== 0 && exp
->exp_flvr_expire
[1]) {
1842 if (exp
->exp_flvr_expire
[1] >= ktime_get_real_seconds()) {
1843 if (flavor_allowed(&exp
->exp_flvr_old
[1], req
)) {
1844 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
1846 exp
->exp_flvr
.sf_rpc
,
1847 exp
->exp_flvr_old
[0].sf_rpc
,
1848 exp
->exp_flvr_old
[1].sf_rpc
,
1849 (s64
)(exp
->exp_flvr_expire
[1] -
1850 ktime_get_real_seconds()));
1851 spin_unlock(&exp
->exp_lock
);
1855 CDEBUG(D_SEC
, "mark oldest expired\n");
1856 exp
->exp_flvr_expire
[1] = 0;
1858 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): %x not match found\n",
1859 exp
, exp
->exp_flvr
.sf_rpc
,
1860 exp
->exp_flvr_old
[0].sf_rpc
, exp
->exp_flvr_old
[1].sf_rpc
,
1861 req
->rq_flvr
.sf_rpc
);
1863 CDEBUG(D_SEC
, "exp %p (%x|%x|%x): skip the last one\n",
1864 exp
, exp
->exp_flvr
.sf_rpc
, exp
->exp_flvr_old
[0].sf_rpc
,
1865 exp
->exp_flvr_old
[1].sf_rpc
);
1868 spin_unlock(&exp
->exp_lock
);
1870 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
1871 exp
, exp
->exp_obd
->obd_name
,
1872 req
, req
->rq_auth_gss
, req
->rq_ctx_init
, req
->rq_ctx_fini
,
1873 req
->rq_auth_usr_root
, req
->rq_auth_usr_mdt
, req
->rq_auth_usr_ost
,
1874 req
->rq_flvr
.sf_rpc
,
1875 exp
->exp_flvr
.sf_rpc
,
1876 exp
->exp_flvr_old
[0].sf_rpc
,
1877 exp
->exp_flvr_expire
[0] ?
1878 (s64
)(exp
->exp_flvr_expire
[0] - ktime_get_real_seconds()) : 0,
1879 exp
->exp_flvr_old
[1].sf_rpc
,
1880 exp
->exp_flvr_expire
[1] ?
1881 (s64
)(exp
->exp_flvr_expire
[1] - ktime_get_real_seconds()) : 0);
1884 EXPORT_SYMBOL(sptlrpc_target_export_check
);
1886 static int sptlrpc_svc_check_from(struct ptlrpc_request
*req
, int svc_rc
)
1888 /* peer's claim is unreliable unless gss is being used */
1889 if (!req
->rq_auth_gss
|| svc_rc
== SECSVC_DROP
)
1892 switch (req
->rq_sp_from
) {
1894 if (req
->rq_auth_usr_mdt
|| req
->rq_auth_usr_ost
) {
1895 DEBUG_REQ(D_ERROR
, req
, "faked source CLI");
1896 svc_rc
= SECSVC_DROP
;
1900 if (!req
->rq_auth_usr_mdt
) {
1901 DEBUG_REQ(D_ERROR
, req
, "faked source MDT");
1902 svc_rc
= SECSVC_DROP
;
1906 if (!req
->rq_auth_usr_ost
) {
1907 DEBUG_REQ(D_ERROR
, req
, "faked source OST");
1908 svc_rc
= SECSVC_DROP
;
1913 if (!req
->rq_auth_usr_root
&& !req
->rq_auth_usr_mdt
&&
1914 !req
->rq_auth_usr_ost
) {
1915 DEBUG_REQ(D_ERROR
, req
, "faked source MGC/MGS");
1916 svc_rc
= SECSVC_DROP
;
1921 DEBUG_REQ(D_ERROR
, req
, "invalid source %u", req
->rq_sp_from
);
1922 svc_rc
= SECSVC_DROP
;
1929 * Used by ptlrpc server, to perform transformation upon request message of
1930 * incoming \a req. This must be the first thing to do with a incoming
1931 * request in ptlrpc layer.
1933 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
1934 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
1935 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
1936 * reply message has been prepared.
1937 * \retval SECSVC_DROP failed, this request should be dropped.
1939 int sptlrpc_svc_unwrap_request(struct ptlrpc_request
*req
)
1941 struct ptlrpc_sec_policy
*policy
;
1942 struct lustre_msg
*msg
= req
->rq_reqbuf
;
1946 LASSERT(!req
->rq_reqmsg
);
1947 LASSERT(!req
->rq_repmsg
);
1948 LASSERT(!req
->rq_svc_ctx
);
1950 req
->rq_req_swab_mask
= 0;
1952 rc
= __lustre_unpack_msg(msg
, req
->rq_reqdata_len
);
1955 lustre_set_req_swabbed(req
, MSG_PTLRPC_HEADER_OFF
);
1959 CERROR("error unpacking request from %s x%llu\n",
1960 libcfs_id2str(req
->rq_peer
), req
->rq_xid
);
1964 req
->rq_flvr
.sf_rpc
= WIRE_FLVR(msg
->lm_secflvr
);
1965 req
->rq_sp_from
= LUSTRE_SP_ANY
;
1966 req
->rq_auth_uid
= -1;
1967 req
->rq_auth_mapped_uid
= -1;
1969 policy
= sptlrpc_wireflavor2policy(req
->rq_flvr
.sf_rpc
);
1971 CERROR("unsupported rpc flavor %x\n", req
->rq_flvr
.sf_rpc
);
1975 LASSERT(policy
->sp_sops
->accept
);
1976 rc
= policy
->sp_sops
->accept(req
);
1977 sptlrpc_policy_put(policy
);
1978 LASSERT(req
->rq_reqmsg
|| rc
!= SECSVC_OK
);
1979 LASSERT(req
->rq_svc_ctx
|| rc
== SECSVC_DROP
);
1982 * if it's not null flavor (which means embedded packing msg),
1983 * reset the swab mask for the coming inner msg unpacking.
1985 if (SPTLRPC_FLVR_POLICY(req
->rq_flvr
.sf_rpc
) != SPTLRPC_POLICY_NULL
)
1986 req
->rq_req_swab_mask
= 0;
1988 /* sanity check for the request source */
1989 rc
= sptlrpc_svc_check_from(req
, rc
);
1994 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
1995 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
1996 * a buffer of \a msglen size.
1998 int sptlrpc_svc_alloc_rs(struct ptlrpc_request
*req
, int msglen
)
2000 struct ptlrpc_sec_policy
*policy
;
2001 struct ptlrpc_reply_state
*rs
;
2004 LASSERT(req
->rq_svc_ctx
);
2005 LASSERT(req
->rq_svc_ctx
->sc_policy
);
2007 policy
= req
->rq_svc_ctx
->sc_policy
;
2008 LASSERT(policy
->sp_sops
->alloc_rs
);
2010 rc
= policy
->sp_sops
->alloc_rs(req
, msglen
);
2011 if (unlikely(rc
== -ENOMEM
)) {
2012 struct ptlrpc_service_part
*svcpt
= req
->rq_rqbd
->rqbd_svcpt
;
2014 if (svcpt
->scp_service
->srv_max_reply_size
<
2015 msglen
+ sizeof(struct ptlrpc_reply_state
)) {
2016 /* Just return failure if the size is too big */
2017 CERROR("size of message is too big (%zd), %d allowed\n",
2018 msglen
+ sizeof(struct ptlrpc_reply_state
),
2019 svcpt
->scp_service
->srv_max_reply_size
);
2023 /* failed alloc, try emergency pool */
2024 rs
= lustre_get_emerg_rs(svcpt
);
2028 req
->rq_reply_state
= rs
;
2029 rc
= policy
->sp_sops
->alloc_rs(req
, msglen
);
2031 lustre_put_emerg_rs(rs
);
2032 req
->rq_reply_state
= NULL
;
2037 (req
->rq_reply_state
&& req
->rq_reply_state
->rs_msg
));
2043 * Used by ptlrpc server, to perform transformation upon reply message.
2045 * \post req->rq_reply_off is set to appropriate server-controlled reply offset.
2046 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2048 int sptlrpc_svc_wrap_reply(struct ptlrpc_request
*req
)
2050 struct ptlrpc_sec_policy
*policy
;
2053 LASSERT(req
->rq_svc_ctx
);
2054 LASSERT(req
->rq_svc_ctx
->sc_policy
);
2056 policy
= req
->rq_svc_ctx
->sc_policy
;
2057 LASSERT(policy
->sp_sops
->authorize
);
2059 rc
= policy
->sp_sops
->authorize(req
);
2060 LASSERT(rc
|| req
->rq_reply_state
->rs_repdata_len
);
2066 * Used by ptlrpc server, to free reply_state.
2068 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state
*rs
)
2070 struct ptlrpc_sec_policy
*policy
;
2071 unsigned int prealloc
;
2073 LASSERT(rs
->rs_svc_ctx
);
2074 LASSERT(rs
->rs_svc_ctx
->sc_policy
);
2076 policy
= rs
->rs_svc_ctx
->sc_policy
;
2077 LASSERT(policy
->sp_sops
->free_rs
);
2079 prealloc
= rs
->rs_prealloc
;
2080 policy
->sp_sops
->free_rs(rs
);
2083 lustre_put_emerg_rs(rs
);
2086 void sptlrpc_svc_ctx_addref(struct ptlrpc_request
*req
)
2088 struct ptlrpc_svc_ctx
*ctx
= req
->rq_svc_ctx
;
2091 atomic_inc(&ctx
->sc_refcount
);
2094 void sptlrpc_svc_ctx_decref(struct ptlrpc_request
*req
)
2096 struct ptlrpc_svc_ctx
*ctx
= req
->rq_svc_ctx
;
2101 LASSERT_ATOMIC_POS(&ctx
->sc_refcount
);
2102 if (atomic_dec_and_test(&ctx
->sc_refcount
)) {
2103 if (ctx
->sc_policy
->sp_sops
->free_ctx
)
2104 ctx
->sc_policy
->sp_sops
->free_ctx(ctx
);
2106 req
->rq_svc_ctx
= NULL
;
2109 /****************************************
2111 ****************************************/
2114 * Perform transformation upon bulk data pointed by \a desc. This is called
2115 * before transforming the request message.
2117 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request
*req
,
2118 struct ptlrpc_bulk_desc
*desc
)
2120 struct ptlrpc_cli_ctx
*ctx
;
2122 LASSERT(req
->rq_bulk_read
|| req
->rq_bulk_write
);
2124 if (!req
->rq_pack_bulk
)
2127 ctx
= req
->rq_cli_ctx
;
2128 if (ctx
->cc_ops
->wrap_bulk
)
2129 return ctx
->cc_ops
->wrap_bulk(ctx
, req
, desc
);
2132 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk
);
2135 * This is called after unwrap the reply message.
2136 * return nob of actual plain text size received, or error code.
2138 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request
*req
,
2139 struct ptlrpc_bulk_desc
*desc
,
2142 struct ptlrpc_cli_ctx
*ctx
;
2145 LASSERT(req
->rq_bulk_read
&& !req
->rq_bulk_write
);
2147 if (!req
->rq_pack_bulk
)
2148 return desc
->bd_nob_transferred
;
2150 ctx
= req
->rq_cli_ctx
;
2151 if (ctx
->cc_ops
->unwrap_bulk
) {
2152 rc
= ctx
->cc_ops
->unwrap_bulk(ctx
, req
, desc
);
2156 return desc
->bd_nob_transferred
;
2158 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read
);
2161 * This is called after unwrap the reply message.
2162 * return 0 for success or error code.
2164 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request
*req
,
2165 struct ptlrpc_bulk_desc
*desc
)
2167 struct ptlrpc_cli_ctx
*ctx
;
2170 LASSERT(!req
->rq_bulk_read
&& req
->rq_bulk_write
);
2172 if (!req
->rq_pack_bulk
)
2175 ctx
= req
->rq_cli_ctx
;
2176 if (ctx
->cc_ops
->unwrap_bulk
) {
2177 rc
= ctx
->cc_ops
->unwrap_bulk(ctx
, req
, desc
);
2183 * if everything is going right, nob should equals to nob_transferred.
2184 * in case of privacy mode, nob_transferred needs to be adjusted.
2186 if (desc
->bd_nob
!= desc
->bd_nob_transferred
) {
2187 CERROR("nob %d doesn't match transferred nob %d\n",
2188 desc
->bd_nob
, desc
->bd_nob_transferred
);
2194 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write
);
2196 /****************************************
2197 * user descriptor helpers *
2198 ****************************************/
2200 int sptlrpc_current_user_desc_size(void)
2204 ngroups
= current_ngroups
;
2206 if (ngroups
> LUSTRE_MAX_GROUPS
)
2207 ngroups
= LUSTRE_MAX_GROUPS
;
2208 return sptlrpc_user_desc_size(ngroups
);
2210 EXPORT_SYMBOL(sptlrpc_current_user_desc_size
);
2212 int sptlrpc_pack_user_desc(struct lustre_msg
*msg
, int offset
)
2214 struct ptlrpc_user_desc
*pud
;
2216 pud
= lustre_msg_buf(msg
, offset
, 0);
2221 pud
->pud_uid
= from_kuid(&init_user_ns
, current_uid());
2222 pud
->pud_gid
= from_kgid(&init_user_ns
, current_gid());
2223 pud
->pud_fsuid
= from_kuid(&init_user_ns
, current_fsuid());
2224 pud
->pud_fsgid
= from_kgid(&init_user_ns
, current_fsgid());
2225 pud
->pud_cap
= cfs_curproc_cap_pack();
2226 pud
->pud_ngroups
= (msg
->lm_buflens
[offset
] - sizeof(*pud
)) / 4;
2229 if (pud
->pud_ngroups
> current_ngroups
)
2230 pud
->pud_ngroups
= current_ngroups
;
2231 memcpy(pud
->pud_groups
, current_cred()->group_info
->gid
,
2232 pud
->pud_ngroups
* sizeof(__u32
));
2233 task_unlock(current
);
2237 EXPORT_SYMBOL(sptlrpc_pack_user_desc
);
2239 int sptlrpc_unpack_user_desc(struct lustre_msg
*msg
, int offset
, int swabbed
)
2241 struct ptlrpc_user_desc
*pud
;
2244 pud
= lustre_msg_buf(msg
, offset
, sizeof(*pud
));
2249 __swab32s(&pud
->pud_uid
);
2250 __swab32s(&pud
->pud_gid
);
2251 __swab32s(&pud
->pud_fsuid
);
2252 __swab32s(&pud
->pud_fsgid
);
2253 __swab32s(&pud
->pud_cap
);
2254 __swab32s(&pud
->pud_ngroups
);
2257 if (pud
->pud_ngroups
> LUSTRE_MAX_GROUPS
) {
2258 CERROR("%u groups is too large\n", pud
->pud_ngroups
);
2262 if (sizeof(*pud
) + pud
->pud_ngroups
* sizeof(__u32
) >
2263 msg
->lm_buflens
[offset
]) {
2264 CERROR("%u groups are claimed but bufsize only %u\n",
2265 pud
->pud_ngroups
, msg
->lm_buflens
[offset
]);
2270 for (i
= 0; i
< pud
->pud_ngroups
; i
++)
2271 __swab32s(&pud
->pud_groups
[i
]);
2276 EXPORT_SYMBOL(sptlrpc_unpack_user_desc
);
2278 /****************************************
2280 ****************************************/
2282 const char *sec2target_str(struct ptlrpc_sec
*sec
)
2284 if (!sec
|| !sec
->ps_import
|| !sec
->ps_import
->imp_obd
)
2286 if (sec_is_reverse(sec
))
2288 return obd_uuid2str(&sec
->ps_import
->imp_obd
->u
.cli
.cl_target_uuid
);
2290 EXPORT_SYMBOL(sec2target_str
);
2293 * return true if the bulk data is protected
2295 bool sptlrpc_flavor_has_bulk(struct sptlrpc_flavor
*flvr
)
2297 switch (SPTLRPC_FLVR_BULK_SVC(flvr
->sf_rpc
)) {
2298 case SPTLRPC_BULK_SVC_INTG
:
2299 case SPTLRPC_BULK_SVC_PRIV
:
2305 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk
);
2307 /****************************************
2308 * crypto API helper/alloc blkciper *
2309 ****************************************/
2311 /****************************************
2312 * initialize/finalize *
2313 ****************************************/
2315 int sptlrpc_init(void)
2319 rwlock_init(&policy_lock
);
2321 rc
= sptlrpc_gc_init();
2325 rc
= sptlrpc_conf_init();
2329 rc
= sptlrpc_enc_pool_init();
2333 rc
= sptlrpc_null_init();
2337 rc
= sptlrpc_plain_init();
2341 rc
= sptlrpc_lproc_init();
2348 sptlrpc_plain_fini();
2350 sptlrpc_null_fini();
2352 sptlrpc_enc_pool_fini();
2354 sptlrpc_conf_fini();
2361 void sptlrpc_fini(void)
2363 sptlrpc_lproc_fini();
2364 sptlrpc_plain_fini();
2365 sptlrpc_null_fini();
2366 sptlrpc_enc_pool_fini();
2367 sptlrpc_conf_fini();