1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 #define DEBUG_SUBSYSTEM S_SEC
46 #include <libcfs/libcfs.h>
48 #include <liblustre.h>
49 #include <libcfs/list.h>
51 #include <linux/crypto.h>
52 #include <linux/key.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_dlm.h>
61 #include <lustre_sec.h>
63 #include "ptlrpc_internal.h"
65 /***********************************************
67 ***********************************************/
69 static cfs_rwlock_t policy_lock;
70 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
74 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
76 __u16 number = policy->sp_policy;
78 LASSERT(policy->sp_name);
79 LASSERT(policy->sp_cops);
80 LASSERT(policy->sp_sops);
82 if (number >= SPTLRPC_POLICY_MAX)
85 cfs_write_lock(&policy_lock);
86 if (unlikely(policies[number])) {
87 cfs_write_unlock(&policy_lock);
90 policies[number] = policy;
91 cfs_write_unlock(&policy_lock);
93 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
96 EXPORT_SYMBOL(sptlrpc_register_policy);
98 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
100 __u16 number = policy->sp_policy;
102 LASSERT(number < SPTLRPC_POLICY_MAX);
104 cfs_write_lock(&policy_lock);
105 if (unlikely(policies[number] == NULL)) {
106 cfs_write_unlock(&policy_lock);
107 CERROR("%s: already unregistered\n", policy->sp_name);
111 LASSERT(policies[number] == policy);
112 policies[number] = NULL;
113 cfs_write_unlock(&policy_lock);
115 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
118 EXPORT_SYMBOL(sptlrpc_unregister_policy);
121 struct ptlrpc_sec_policy * sptlrpc_wireflavor2policy(__u32 flavor)
123 static CFS_DECLARE_MUTEX(load_mutex);
124 static cfs_atomic_t loaded = CFS_ATOMIC_INIT(0);
125 struct ptlrpc_sec_policy *policy;
126 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
129 if (number >= SPTLRPC_POLICY_MAX)
133 cfs_read_lock(&policy_lock);
134 policy = policies[number];
135 if (policy && !cfs_try_module_get(policy->sp_owner))
138 flag = cfs_atomic_read(&loaded);
139 cfs_read_unlock(&policy_lock);
141 if (policy != NULL || flag != 0 ||
142 number != SPTLRPC_POLICY_GSS)
145 /* try to load gss module, once */
146 cfs_mutex_down(&load_mutex);
147 if (cfs_atomic_read(&loaded) == 0) {
148 if (cfs_request_module("ptlrpc_gss") == 0)
149 CWARN("module ptlrpc_gss loaded on demand\n");
151 CERROR("Unable to load module ptlrpc_gss\n");
153 cfs_atomic_set(&loaded, 1);
155 cfs_mutex_up(&load_mutex);
161 __u32 sptlrpc_name2flavor_base(const char *name)
163 if (!strcmp(name, "null"))
164 return SPTLRPC_FLVR_NULL;
165 if (!strcmp(name, "plain"))
166 return SPTLRPC_FLVR_PLAIN;
167 if (!strcmp(name, "krb5n"))
168 return SPTLRPC_FLVR_KRB5N;
169 if (!strcmp(name, "krb5a"))
170 return SPTLRPC_FLVR_KRB5A;
171 if (!strcmp(name, "krb5i"))
172 return SPTLRPC_FLVR_KRB5I;
173 if (!strcmp(name, "krb5p"))
174 return SPTLRPC_FLVR_KRB5P;
176 return SPTLRPC_FLVR_INVALID;
178 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
180 const char *sptlrpc_flavor2name_base(__u32 flvr)
182 __u32 base = SPTLRPC_FLVR_BASE(flvr);
184 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
186 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
188 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
190 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
192 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
194 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
197 CERROR("invalid wire flavor 0x%x\n", flvr);
200 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
202 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
203 char *buf, int bufsize)
205 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
206 snprintf(buf, bufsize, "hash:%s",
207 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
209 snprintf(buf, bufsize, "%s",
210 sptlrpc_flavor2name_base(sf->sf_rpc));
212 buf[bufsize - 1] = '\0';
215 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
217 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
219 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
222 * currently we don't support customized bulk specification for
223 * flavors other than plain
225 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
229 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
230 strncat(buf, bspec, bufsize);
233 buf[bufsize - 1] = '\0';
236 EXPORT_SYMBOL(sptlrpc_flavor2name);
238 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
242 if (flags & PTLRPC_SEC_FL_REVERSE)
243 strncat(buf, "reverse,", bufsize);
244 if (flags & PTLRPC_SEC_FL_ROOTONLY)
245 strncat(buf, "rootonly,", bufsize);
246 if (flags & PTLRPC_SEC_FL_UDESC)
247 strncat(buf, "udesc,", bufsize);
248 if (flags & PTLRPC_SEC_FL_BULK)
249 strncat(buf, "bulk,", bufsize);
251 strncat(buf, "-,", bufsize);
253 buf[bufsize - 1] = '\0';
256 EXPORT_SYMBOL(sptlrpc_secflags2str);
258 /**************************************************
259 * client context APIs *
260 **************************************************/
263 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
265 struct vfs_cred vcred;
266 int create = 1, remove_dead = 1;
269 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
271 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
272 PTLRPC_SEC_FL_ROOTONLY)) {
275 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
280 vcred.vc_uid = cfs_curproc_uid();
281 vcred.vc_gid = cfs_curproc_gid();
284 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
285 create, remove_dead);
288 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
290 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
291 cfs_atomic_inc(&ctx->cc_refcount);
294 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
296 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
298 struct ptlrpc_sec *sec = ctx->cc_sec;
301 LASSERT(cfs_atomic_read(&ctx->cc_refcount));
303 if (!cfs_atomic_dec_and_test(&ctx->cc_refcount))
306 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
308 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
311 * expire the context immediately.
312 * the caller must hold at least 1 ref on the ctx.
314 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
316 LASSERT(ctx->cc_ops->die);
317 ctx->cc_ops->die(ctx, 0);
319 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
321 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
323 struct ptlrpc_request *req, *next;
325 cfs_spin_lock(&ctx->cc_lock);
326 cfs_list_for_each_entry_safe(req, next, &ctx->cc_req_list,
328 cfs_list_del_init(&req->rq_ctx_chain);
329 ptlrpc_client_wake_req(req);
331 cfs_spin_unlock(&ctx->cc_lock);
333 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
335 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
337 LASSERT(ctx->cc_ops);
339 if (ctx->cc_ops->display == NULL)
342 return ctx->cc_ops->display(ctx, buf, bufsize);
345 static int import_sec_check_expire(struct obd_import *imp)
349 cfs_spin_lock(&imp->imp_lock);
350 if (imp->imp_sec_expire &&
351 imp->imp_sec_expire < cfs_time_current_sec()) {
353 imp->imp_sec_expire = 0;
355 cfs_spin_unlock(&imp->imp_lock);
360 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
361 return sptlrpc_import_sec_adapt(imp, NULL, 0);
364 static int import_sec_validate_get(struct obd_import *imp,
365 struct ptlrpc_sec **sec)
369 if (unlikely(imp->imp_sec_expire)) {
370 rc = import_sec_check_expire(imp);
375 *sec = sptlrpc_import_sec_ref(imp);
377 CERROR("import %p (%s) with no sec\n",
378 imp, ptlrpc_import_state_name(imp->imp_state));
382 if (unlikely((*sec)->ps_dying)) {
383 CERROR("attempt to use dying sec %p\n", sec);
384 sptlrpc_sec_put(*sec);
391 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
393 struct obd_import *imp = req->rq_import;
394 struct ptlrpc_sec *sec;
398 LASSERT(!req->rq_cli_ctx);
401 rc = import_sec_validate_get(imp, &sec);
405 req->rq_cli_ctx = get_my_ctx(sec);
407 sptlrpc_sec_put(sec);
409 if (!req->rq_cli_ctx) {
410 CERROR("req %p: fail to get context\n", req);
418 * if @sync == 0, this function should return quickly without sleep;
419 * otherwise might trigger ctx destroying rpc to server.
421 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
426 LASSERT(req->rq_cli_ctx);
428 /* request might be asked to release earlier while still
429 * in the context waiting list.
431 if (!cfs_list_empty(&req->rq_ctx_chain)) {
432 cfs_spin_lock(&req->rq_cli_ctx->cc_lock);
433 cfs_list_del_init(&req->rq_ctx_chain);
434 cfs_spin_unlock(&req->rq_cli_ctx->cc_lock);
437 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
438 req->rq_cli_ctx = NULL;
443 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
444 struct ptlrpc_cli_ctx *oldctx,
445 struct ptlrpc_cli_ctx *newctx)
447 struct sptlrpc_flavor old_flvr;
452 LASSERT(req->rq_reqmsg);
453 LASSERT(req->rq_reqlen);
454 LASSERT(req->rq_replen);
456 CWARN("req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
457 "switch sec %p(%s) -> %p(%s)\n", req,
458 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
459 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
460 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
461 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
464 old_flvr = req->rq_flvr;
466 /* save request message */
467 reqmsg_size = req->rq_reqlen;
468 OBD_ALLOC(reqmsg, reqmsg_size);
471 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
473 /* release old req/rep buf */
474 req->rq_cli_ctx = oldctx;
475 sptlrpc_cli_free_reqbuf(req);
476 sptlrpc_cli_free_repbuf(req);
477 req->rq_cli_ctx = newctx;
479 /* recalculate the flavor */
480 sptlrpc_req_set_flavor(req, 0);
482 /* alloc new request buffer
483 * we don't need to alloc reply buffer here, leave it to the
484 * rest procedure of ptlrpc
486 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
488 LASSERT(req->rq_reqmsg);
489 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
491 CWARN("failed to alloc reqbuf: %d\n", rc);
492 req->rq_flvr = old_flvr;
495 OBD_FREE(reqmsg, reqmsg_size);
500 * if current context has died, or if we resend after flavor switched,
501 * call this func to switch context. if no switch is needed, request
502 * will end up with the same context.
504 * request must have a context. in any case of failure, restore the
505 * restore the old one - a request must have a context.
507 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
509 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
510 struct ptlrpc_cli_ctx *newctx;
516 sptlrpc_cli_ctx_get(oldctx);
517 sptlrpc_req_put_ctx(req, 0);
519 rc = sptlrpc_req_get_ctx(req);
521 LASSERT(!req->rq_cli_ctx);
523 /* restore old ctx */
524 req->rq_cli_ctx = oldctx;
528 newctx = req->rq_cli_ctx;
531 if (unlikely(newctx == oldctx &&
532 cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
534 * still get the old dead ctx, usually means system too busy
536 CWARN("ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
537 newctx, newctx->cc_flags);
539 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
543 * it's possible newctx == oldctx if we're switching
544 * subflavor with the same sec.
546 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
548 /* restore old ctx */
549 sptlrpc_req_put_ctx(req, 0);
550 req->rq_cli_ctx = oldctx;
554 LASSERT(req->rq_cli_ctx == newctx);
557 sptlrpc_cli_ctx_put(oldctx, 1);
560 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
563 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
565 if (cli_ctx_is_refreshed(ctx))
571 int ctx_refresh_timeout(void *data)
573 struct ptlrpc_request *req = data;
576 /* conn_cnt is needed in expire_one_request */
577 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
579 rc = ptlrpc_expire_one_request(req, 1);
580 /* if we started recovery, we should mark this ctx dead; otherwise
581 * in case of lgssd died nobody would retire this ctx, following
582 * connecting will still find the same ctx thus cause deadlock.
583 * there's an assumption that expire time of the request should be
584 * later than the context refresh expire time.
587 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
592 void ctx_refresh_interrupt(void *data)
594 struct ptlrpc_request *req = data;
596 cfs_spin_lock(&req->rq_lock);
598 cfs_spin_unlock(&req->rq_lock);
602 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
604 cfs_spin_lock(&ctx->cc_lock);
605 if (!cfs_list_empty(&req->rq_ctx_chain))
606 cfs_list_del_init(&req->rq_ctx_chain);
607 cfs_spin_unlock(&ctx->cc_lock);
611 * the status of context could be subject to be changed by other threads at any
612 * time. we allow this race. but once we return with 0, the caller will
613 * suppose it's uptodated and keep using it until the owning rpc is done.
617 * = 0 - wait until success or fatal error occur
618 * > 0 - timeout value
620 * return 0 only if the context is uptodated.
622 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
624 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
625 struct ptlrpc_sec *sec;
626 struct l_wait_info lwi;
632 if (req->rq_ctx_init || req->rq_ctx_fini)
636 * during the process a request's context might change type even
637 * (e.g. from gss ctx to plain ctx), so each loop we need to re-check
641 rc = import_sec_validate_get(req->rq_import, &sec);
645 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
646 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
647 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
648 req_off_ctx_list(req, ctx);
649 sptlrpc_req_replace_dead_ctx(req);
650 ctx = req->rq_cli_ctx;
652 sptlrpc_sec_put(sec);
654 if (cli_ctx_is_eternal(ctx))
657 if (unlikely(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
658 LASSERT(ctx->cc_ops->refresh);
659 ctx->cc_ops->refresh(ctx);
661 LASSERT(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
663 LASSERT(ctx->cc_ops->validate);
664 if (ctx->cc_ops->validate(ctx) == 0) {
665 req_off_ctx_list(req, ctx);
669 if (unlikely(cfs_test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
670 cfs_spin_lock(&req->rq_lock);
672 cfs_spin_unlock(&req->rq_lock);
673 req_off_ctx_list(req, ctx);
677 /* This is subtle. For resent message we have to keep original
678 * context to survive following situation:
679 * 1. the request sent to server
680 * 2. recovery was kick start
681 * 3. recovery finished, the request marked as resent
682 * 4. resend the request
683 * 5. old reply from server received (because xid is the same)
684 * 6. verify reply (has to be success)
685 * 7. new reply from server received, lnet drop it
687 * Note we can't simply change xid for resent request because
688 * server reply on it for reply reconstruction.
690 * Commonly the original context should be uptodate because we
691 * have a expiry nice time; And server will keep their half part
692 * context because we at least hold a ref of old context which
693 * prevent the context destroy RPC be sent. So server still can
694 * accept the request and finish RPC. Two cases:
695 * 1. If server side context has been trimmed, a NO_CONTEXT will
696 * be returned, gss_cli_ctx_verify/unseal will switch to new
698 * 2. Current context never be refreshed, then we are fine: we
699 * never really send request with old context before.
701 if (cfs_test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
702 unlikely(req->rq_reqmsg) &&
703 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
704 req_off_ctx_list(req, ctx);
708 if (unlikely(cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
709 req_off_ctx_list(req, ctx);
711 * don't switch ctx if import was deactivated
713 if (req->rq_import->imp_deactive) {
714 cfs_spin_lock(&req->rq_lock);
716 cfs_spin_unlock(&req->rq_lock);
720 rc = sptlrpc_req_replace_dead_ctx(req);
722 LASSERT(ctx == req->rq_cli_ctx);
723 CERROR("req %p: failed to replace dead ctx %p: %d\n",
725 cfs_spin_lock(&req->rq_lock);
727 cfs_spin_unlock(&req->rq_lock);
731 ctx = req->rq_cli_ctx;
735 /* Now we're sure this context is during upcall, add myself into
738 cfs_spin_lock(&ctx->cc_lock);
739 if (cfs_list_empty(&req->rq_ctx_chain))
740 cfs_list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
741 cfs_spin_unlock(&ctx->cc_lock);
744 RETURN(-EWOULDBLOCK);
746 /* Clear any flags that may be present from previous sends */
747 LASSERT(req->rq_receiving_reply == 0);
748 cfs_spin_lock(&req->rq_lock);
750 req->rq_timedout = 0;
753 cfs_spin_unlock(&req->rq_lock);
755 lwi = LWI_TIMEOUT_INTR(timeout * CFS_HZ, ctx_refresh_timeout,
756 ctx_refresh_interrupt, req);
757 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
759 /* following cases we could be here:
760 * - successfully refreshed;
762 * - timedout, and we don't want recover from the failure;
763 * - timedout, and waked up upon recovery finished;
764 * - someone else mark this ctx dead by force;
765 * - someone invalidate the req and call ptlrpc_client_wake_req(),
766 * e.g. ptlrpc_abort_inflight();
768 if (!cli_ctx_is_refreshed(ctx)) {
769 /* timed out or interruptted */
770 req_off_ctx_list(req, ctx);
780 * Note this could be called in two situations:
781 * - new request from ptlrpc_pre_req(), with proper @opcode
782 * - old request which changed ctx in the middle, with @opcode == 0
784 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
786 struct ptlrpc_sec *sec;
788 LASSERT(req->rq_import);
789 LASSERT(req->rq_cli_ctx);
790 LASSERT(req->rq_cli_ctx->cc_sec);
791 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
793 /* special security flags accoding to opcode */
797 req->rq_bulk_read = 1;
801 req->rq_bulk_write = 1;
804 req->rq_ctx_init = 1;
807 req->rq_ctx_fini = 1;
810 /* init/fini rpc won't be resend, so can't be here */
811 LASSERT(req->rq_ctx_init == 0);
812 LASSERT(req->rq_ctx_fini == 0);
814 /* cleanup flags, which should be recalculated */
815 req->rq_pack_udesc = 0;
816 req->rq_pack_bulk = 0;
820 sec = req->rq_cli_ctx->cc_sec;
822 cfs_spin_lock(&sec->ps_lock);
823 req->rq_flvr = sec->ps_flvr;
824 cfs_spin_unlock(&sec->ps_lock);
826 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
828 if (unlikely(req->rq_ctx_init))
829 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
830 else if (unlikely(req->rq_ctx_fini))
831 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
833 /* user descriptor flag, null security can't do it anyway */
834 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
835 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
836 req->rq_pack_udesc = 1;
838 /* bulk security flag */
839 if ((req->rq_bulk_read || req->rq_bulk_write) &&
840 sptlrpc_flavor_has_bulk(&req->rq_flvr))
841 req->rq_pack_bulk = 1;
844 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
846 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
849 LASSERT(req->rq_clrbuf);
850 if (req->rq_pool || !req->rq_reqbuf)
853 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
854 req->rq_reqbuf = NULL;
855 req->rq_reqbuf_len = 0;
859 * check whether current user have valid context for an import or not.
860 * might repeatedly try in case of non-fatal errors.
861 * return 0 on success, < 0 on failure
863 int sptlrpc_import_check_ctx(struct obd_import *imp)
865 struct ptlrpc_sec *sec;
866 struct ptlrpc_cli_ctx *ctx;
867 struct ptlrpc_request *req = NULL;
873 sec = sptlrpc_import_sec_ref(imp);
874 ctx = get_my_ctx(sec);
875 sptlrpc_sec_put(sec);
880 if (cli_ctx_is_eternal(ctx) ||
881 ctx->cc_ops->validate(ctx) == 0) {
882 sptlrpc_cli_ctx_put(ctx, 1);
886 if (cli_ctx_is_error(ctx)) {
887 sptlrpc_cli_ctx_put(ctx, 1);
895 cfs_spin_lock_init(&req->rq_lock);
896 cfs_atomic_set(&req->rq_refcount, 10000);
897 CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
898 cfs_waitq_init(&req->rq_reply_waitq);
899 cfs_waitq_init(&req->rq_set_waitq);
900 req->rq_import = imp;
901 req->rq_flvr = sec->ps_flvr;
902 req->rq_cli_ctx = ctx;
904 rc = sptlrpc_req_refresh_ctx(req, 0);
905 LASSERT(cfs_list_empty(&req->rq_ctx_chain));
906 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
912 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
914 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
919 LASSERT(ctx->cc_sec);
920 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
922 /* we wrap bulk request here because now we can be sure
923 * the context is uptodate.
926 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
931 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
932 case SPTLRPC_SVC_NULL:
933 case SPTLRPC_SVC_AUTH:
934 case SPTLRPC_SVC_INTG:
935 LASSERT(ctx->cc_ops->sign);
936 rc = ctx->cc_ops->sign(ctx, req);
938 case SPTLRPC_SVC_PRIV:
939 LASSERT(ctx->cc_ops->seal);
940 rc = ctx->cc_ops->seal(ctx, req);
947 LASSERT(req->rq_reqdata_len);
948 LASSERT(req->rq_reqdata_len % 8 == 0);
949 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
955 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
957 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
962 LASSERT(ctx->cc_sec);
963 LASSERT(req->rq_repbuf);
964 LASSERT(req->rq_repdata);
965 LASSERT(req->rq_repmsg == NULL);
967 req->rq_rep_swab_mask = 0;
969 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
972 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
976 CERROR("failed unpack reply: x"LPU64"\n", req->rq_xid);
980 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
981 CERROR("replied data length %d too small\n",
982 req->rq_repdata_len);
986 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
987 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
988 CERROR("reply policy %u doesn't match request policy %u\n",
989 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
990 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
994 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
995 case SPTLRPC_SVC_NULL:
996 case SPTLRPC_SVC_AUTH:
997 case SPTLRPC_SVC_INTG:
998 LASSERT(ctx->cc_ops->verify);
999 rc = ctx->cc_ops->verify(ctx, req);
1001 case SPTLRPC_SVC_PRIV:
1002 LASSERT(ctx->cc_ops->unseal);
1003 rc = ctx->cc_ops->unseal(ctx, req);
1008 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1010 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1012 req->rq_rep_swab_mask = 0;
1017 * upon this be called, the reply buffer should have been un-posted,
1018 * so nothing is going to change.
1020 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1022 LASSERT(req->rq_repbuf);
1023 LASSERT(req->rq_repdata == NULL);
1024 LASSERT(req->rq_repmsg == NULL);
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 * Upon called, the receive buffer might be still posted, so the reply data
1047 * might be changed at any time, no matter we're holding rq_lock or not. we
1048 * expect the rq_reply_off be 0, rq_nob_received is the early reply size.
1050 * we allocate separate ptlrpc_request and reply buffer for early reply
1051 * processing, return 0 and \a req_ret is a duplicated ptlrpc_request. caller
1052 * must call sptlrpc_cli_finish_early_reply() on the returned request to
1053 * release it. if anything goes wrong \a req_ret will not be set.
1055 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1056 struct ptlrpc_request **req_ret)
1058 struct ptlrpc_request *early_req;
1060 int early_bufsz, early_size;
1064 OBD_ALLOC_PTR(early_req);
1065 if (early_req == NULL)
1068 early_size = req->rq_nob_received;
1069 early_bufsz = size_roundup_power2(early_size);
1070 OBD_ALLOC(early_buf, early_bufsz);
1071 if (early_buf == NULL)
1072 GOTO(err_req, rc = -ENOMEM);
1074 /* sanity checkings and copy data out, do it inside spinlock */
1075 cfs_spin_lock(&req->rq_lock);
1077 if (req->rq_replied) {
1078 cfs_spin_unlock(&req->rq_lock);
1079 GOTO(err_buf, rc = -EALREADY);
1082 LASSERT(req->rq_repbuf);
1083 LASSERT(req->rq_repdata == NULL);
1084 LASSERT(req->rq_repmsg == NULL);
1086 if (req->rq_reply_off != 0) {
1087 CERROR("early reply with offset %u\n", req->rq_reply_off);
1088 cfs_spin_unlock(&req->rq_lock);
1089 GOTO(err_buf, rc = -EPROTO);
1092 if (req->rq_nob_received != early_size) {
1093 /* even another early arrived the size should be the same */
1094 CERROR("data size has changed from %u to %u\n",
1095 early_size, req->rq_nob_received);
1096 cfs_spin_unlock(&req->rq_lock);
1097 GOTO(err_buf, rc = -EINVAL);
1100 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1101 CERROR("early reply length %d too small\n",
1102 req->rq_nob_received);
1103 cfs_spin_unlock(&req->rq_lock);
1104 GOTO(err_buf, rc = -EALREADY);
1107 memcpy(early_buf, req->rq_repbuf, early_size);
1108 cfs_spin_unlock(&req->rq_lock);
1110 cfs_spin_lock_init(&early_req->rq_lock);
1111 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1112 early_req->rq_flvr = req->rq_flvr;
1113 early_req->rq_repbuf = early_buf;
1114 early_req->rq_repbuf_len = early_bufsz;
1115 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1116 early_req->rq_repdata_len = early_size;
1117 early_req->rq_early = 1;
1118 early_req->rq_reqmsg = req->rq_reqmsg;
1120 rc = do_cli_unwrap_reply(early_req);
1122 DEBUG_REQ(D_ADAPTTO, early_req,
1123 "error %d unwrap early reply", rc);
1127 LASSERT(early_req->rq_repmsg);
1128 *req_ret = early_req;
1132 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1134 OBD_FREE(early_buf, early_bufsz);
1136 OBD_FREE_PTR(early_req);
1140 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1142 LASSERT(early_req->rq_repbuf);
1143 LASSERT(early_req->rq_repdata);
1144 LASSERT(early_req->rq_repmsg);
1146 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1147 OBD_FREE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1148 OBD_FREE_PTR(early_req);
1151 /**************************************************
1153 **************************************************/
1156 * "fixed" sec (e.g. null) use sec_id < 0
1158 static cfs_atomic_t sptlrpc_sec_id = CFS_ATOMIC_INIT(1);
1160 int sptlrpc_get_next_secid(void)
1162 return cfs_atomic_inc_return(&sptlrpc_sec_id);
1164 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1166 /**************************************************
1167 * client side high-level security APIs *
1168 **************************************************/
1170 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1171 int grace, int force)
1173 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1175 LASSERT(policy->sp_cops);
1176 LASSERT(policy->sp_cops->flush_ctx_cache);
1178 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1181 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1183 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1185 LASSERT(cfs_atomic_read(&sec->ps_refcount) == 0);
1186 LASSERT(cfs_atomic_read(&sec->ps_nctx) == 0);
1187 LASSERT(policy->sp_cops->destroy_sec);
1189 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1191 policy->sp_cops->destroy_sec(sec);
1192 sptlrpc_policy_put(policy);
1195 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1197 sec_cop_destroy_sec(sec);
1199 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1201 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1203 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
1205 if (sec->ps_policy->sp_cops->kill_sec) {
1206 sec->ps_policy->sp_cops->kill_sec(sec);
1208 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1212 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1215 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
1216 cfs_atomic_inc(&sec->ps_refcount);
1221 EXPORT_SYMBOL(sptlrpc_sec_get);
1223 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1226 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
1228 if (cfs_atomic_dec_and_test(&sec->ps_refcount)) {
1229 LASSERT(cfs_atomic_read(&sec->ps_nctx) == 0);
1231 sptlrpc_gc_del_sec(sec);
1232 sec_cop_destroy_sec(sec);
1236 EXPORT_SYMBOL(sptlrpc_sec_put);
1239 * policy module is responsible for taking refrence of import
1242 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1243 struct ptlrpc_svc_ctx *svc_ctx,
1244 struct sptlrpc_flavor *sf,
1245 enum lustre_sec_part sp)
1247 struct ptlrpc_sec_policy *policy;
1248 struct ptlrpc_sec *sec;
1253 LASSERT(imp->imp_dlm_fake == 1);
1255 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1256 imp->imp_obd->obd_type->typ_name,
1257 imp->imp_obd->obd_name,
1258 sptlrpc_flavor2name(sf, str, sizeof(str)));
1260 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1261 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1263 LASSERT(imp->imp_dlm_fake == 0);
1265 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1266 imp->imp_obd->obd_type->typ_name,
1267 imp->imp_obd->obd_name,
1268 sptlrpc_flavor2name(sf, str, sizeof(str)));
1270 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1272 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1277 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1279 cfs_atomic_inc(&sec->ps_refcount);
1283 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1284 sptlrpc_gc_add_sec(sec);
1286 sptlrpc_policy_put(policy);
1292 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1294 struct ptlrpc_sec *sec;
1296 cfs_spin_lock(&imp->imp_lock);
1297 sec = sptlrpc_sec_get(imp->imp_sec);
1298 cfs_spin_unlock(&imp->imp_lock);
1302 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1304 static void sptlrpc_import_sec_install(struct obd_import *imp,
1305 struct ptlrpc_sec *sec)
1307 struct ptlrpc_sec *old_sec;
1309 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
1311 cfs_spin_lock(&imp->imp_lock);
1312 old_sec = imp->imp_sec;
1314 cfs_spin_unlock(&imp->imp_lock);
1317 sptlrpc_sec_kill(old_sec);
1319 /* balance the ref taken by this import */
1320 sptlrpc_sec_put(old_sec);
1325 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1327 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1331 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1336 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1337 struct ptlrpc_sec *sec,
1338 struct sptlrpc_flavor *sf)
1340 char str1[32], str2[32];
1342 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1343 CWARN("changing sec flags: %s -> %s\n",
1344 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1345 str1, sizeof(str1)),
1346 sptlrpc_secflags2str(sf->sf_flags,
1347 str2, sizeof(str2)));
1349 cfs_spin_lock(&sec->ps_lock);
1350 flavor_copy(&sec->ps_flvr, sf);
1351 cfs_spin_unlock(&sec->ps_lock);
1355 * for normal import, @svc_ctx should be NULL and @flvr is ignored;
1356 * for reverse import, @svc_ctx and @flvr is from incoming request.
1358 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1359 struct ptlrpc_svc_ctx *svc_ctx,
1360 struct sptlrpc_flavor *flvr)
1362 struct ptlrpc_connection *conn;
1363 struct sptlrpc_flavor sf;
1364 struct ptlrpc_sec *sec, *newsec;
1365 enum lustre_sec_part sp;
1375 conn = imp->imp_connection;
1377 if (svc_ctx == NULL) {
1378 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1380 * normal import, determine flavor from rule set, except
1381 * for mgc the flavor is predetermined.
1383 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1384 sf = cliobd->cl_flvr_mgc;
1386 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1388 &cliobd->cl_target_uuid,
1391 sp = imp->imp_obd->u.cli.cl_sp_me;
1393 /* reverse import, determine flavor from incoming reqeust */
1396 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1397 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1398 PTLRPC_SEC_FL_ROOTONLY;
1400 sp = sptlrpc_target_sec_part(imp->imp_obd);
1403 sec = sptlrpc_import_sec_ref(imp);
1407 if (flavor_equal(&sf, &sec->ps_flvr))
1410 CWARN("import %s->%s: changing flavor %s -> %s\n",
1411 imp->imp_obd->obd_name,
1412 obd_uuid2str(&conn->c_remote_uuid),
1413 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1414 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1416 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1417 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1418 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1419 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1420 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1424 CWARN("import %s->%s netid %x: select flavor %s\n",
1425 imp->imp_obd->obd_name,
1426 obd_uuid2str(&conn->c_remote_uuid),
1427 LNET_NIDNET(conn->c_self),
1428 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1431 cfs_mutex_down(&imp->imp_sec_mutex);
1433 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1435 sptlrpc_import_sec_install(imp, newsec);
1437 CERROR("import %s->%s: failed to create new sec\n",
1438 imp->imp_obd->obd_name,
1439 obd_uuid2str(&conn->c_remote_uuid));
1443 cfs_mutex_up(&imp->imp_sec_mutex);
1445 sptlrpc_sec_put(sec);
1449 void sptlrpc_import_sec_put(struct obd_import *imp)
1452 sptlrpc_sec_kill(imp->imp_sec);
1454 sptlrpc_sec_put(imp->imp_sec);
1455 imp->imp_sec = NULL;
1459 static void import_flush_ctx_common(struct obd_import *imp,
1460 uid_t uid, int grace, int force)
1462 struct ptlrpc_sec *sec;
1467 sec = sptlrpc_import_sec_ref(imp);
1471 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1472 sptlrpc_sec_put(sec);
1475 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1477 /* it's important to use grace mode, see explain in
1478 * sptlrpc_req_refresh_ctx() */
1479 import_flush_ctx_common(imp, 0, 1, 1);
1482 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1484 import_flush_ctx_common(imp, cfs_curproc_uid(), 1, 1);
1486 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1488 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1490 import_flush_ctx_common(imp, -1, 1, 1);
1492 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1495 * when complete successfully, req->rq_reqmsg should point to the
1498 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1500 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1501 struct ptlrpc_sec_policy *policy;
1505 LASSERT(cfs_atomic_read(&ctx->cc_refcount));
1506 LASSERT(ctx->cc_sec);
1507 LASSERT(ctx->cc_sec->ps_policy);
1508 LASSERT(req->rq_reqmsg == NULL);
1510 policy = ctx->cc_sec->ps_policy;
1511 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1513 LASSERT(req->rq_reqmsg);
1514 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1516 /* zeroing preallocated buffer */
1518 memset(req->rq_reqmsg, 0, msgsize);
1524 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1526 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1527 struct ptlrpc_sec_policy *policy;
1530 LASSERT(cfs_atomic_read(&ctx->cc_refcount));
1531 LASSERT(ctx->cc_sec);
1532 LASSERT(ctx->cc_sec->ps_policy);
1534 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1537 policy = ctx->cc_sec->ps_policy;
1538 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1542 * NOTE caller must guarantee the buffer size is enough for the enlargement
1544 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1545 int segment, int newsize)
1548 int oldsize, oldmsg_size, movesize;
1550 LASSERT(segment < msg->lm_bufcount);
1551 LASSERT(msg->lm_buflens[segment] <= newsize);
1553 if (msg->lm_buflens[segment] == newsize)
1556 /* nothing to do if we are enlarging the last segment */
1557 if (segment == msg->lm_bufcount - 1) {
1558 msg->lm_buflens[segment] = newsize;
1562 oldsize = msg->lm_buflens[segment];
1564 src = lustre_msg_buf(msg, segment + 1, 0);
1565 msg->lm_buflens[segment] = newsize;
1566 dst = lustre_msg_buf(msg, segment + 1, 0);
1567 msg->lm_buflens[segment] = oldsize;
1569 /* move from segment + 1 to end segment */
1570 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1571 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1572 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1573 LASSERT(movesize >= 0);
1576 memmove(dst, src, movesize);
1578 /* note we don't clear the ares where old data live, not secret */
1580 /* finally set new segment size */
1581 msg->lm_buflens[segment] = newsize;
1583 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1586 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1587 * will be preserved after enlargement. this must be called after rq_reqmsg has
1588 * been intialized at least.
1590 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1593 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1594 int segment, int newsize)
1596 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1597 struct ptlrpc_sec_cops *cops;
1598 struct lustre_msg *msg = req->rq_reqmsg;
1602 LASSERT(msg->lm_bufcount > segment);
1603 LASSERT(msg->lm_buflens[segment] <= newsize);
1605 if (msg->lm_buflens[segment] == newsize)
1608 cops = ctx->cc_sec->ps_policy->sp_cops;
1609 LASSERT(cops->enlarge_reqbuf);
1610 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1612 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1614 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1616 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1617 struct ptlrpc_sec_policy *policy;
1621 LASSERT(cfs_atomic_read(&ctx->cc_refcount));
1622 LASSERT(ctx->cc_sec);
1623 LASSERT(ctx->cc_sec->ps_policy);
1628 policy = ctx->cc_sec->ps_policy;
1629 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1632 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1634 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1635 struct ptlrpc_sec_policy *policy;
1639 LASSERT(cfs_atomic_read(&ctx->cc_refcount));
1640 LASSERT(ctx->cc_sec);
1641 LASSERT(ctx->cc_sec->ps_policy);
1643 if (req->rq_repbuf == NULL)
1645 LASSERT(req->rq_repbuf_len);
1647 policy = ctx->cc_sec->ps_policy;
1648 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1652 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1653 struct ptlrpc_cli_ctx *ctx)
1655 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1657 if (!policy->sp_cops->install_rctx)
1659 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1662 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1663 struct ptlrpc_svc_ctx *ctx)
1665 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1667 if (!policy->sp_sops->install_rctx)
1669 return policy->sp_sops->install_rctx(imp, ctx);
1672 /****************************************
1673 * server side security *
1674 ****************************************/
1676 static int flavor_allowed(struct sptlrpc_flavor *exp,
1677 struct ptlrpc_request *req)
1679 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1681 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1684 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1685 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1686 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1687 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1693 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1695 int sptlrpc_target_export_check(struct obd_export *exp,
1696 struct ptlrpc_request *req)
1698 struct sptlrpc_flavor flavor;
1703 /* client side export has no imp_reverse, skip
1704 * FIXME maybe we should check flavor this as well??? */
1705 if (exp->exp_imp_reverse == NULL)
1708 /* don't care about ctx fini rpc */
1709 if (req->rq_ctx_fini)
1712 cfs_spin_lock(&exp->exp_lock);
1714 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1715 * the first req with the new flavor, then treat it as current flavor,
1716 * adapt reverse sec according to it.
1717 * note the first rpc with new flavor might not be with root ctx, in
1718 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1719 if (unlikely(exp->exp_flvr_changed) &&
1720 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1721 /* make the new flavor as "current", and old ones as
1722 * about-to-expire */
1723 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1724 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1725 flavor = exp->exp_flvr_old[1];
1726 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1727 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1728 exp->exp_flvr_old[0] = exp->exp_flvr;
1729 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1730 EXP_FLVR_UPDATE_EXPIRE;
1731 exp->exp_flvr = flavor;
1733 /* flavor change finished */
1734 exp->exp_flvr_changed = 0;
1735 LASSERT(exp->exp_flvr_adapt == 1);
1737 /* if it's gss, we only interested in root ctx init */
1738 if (req->rq_auth_gss &&
1739 !(req->rq_ctx_init && (req->rq_auth_usr_root ||
1740 req->rq_auth_usr_mdt))) {
1741 cfs_spin_unlock(&exp->exp_lock);
1742 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d)\n",
1743 req->rq_auth_gss, req->rq_ctx_init,
1744 req->rq_auth_usr_root, req->rq_auth_usr_mdt);
1748 exp->exp_flvr_adapt = 0;
1749 cfs_spin_unlock(&exp->exp_lock);
1751 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1752 req->rq_svc_ctx, &flavor);
1755 /* if it equals to the current flavor, we accept it, but need to
1756 * dealing with reverse sec/ctx */
1757 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1758 /* most cases should return here, we only interested in
1759 * gss root ctx init */
1760 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1761 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt)) {
1762 cfs_spin_unlock(&exp->exp_lock);
1766 /* if flavor just changed, we should not proceed, just leave
1767 * it and current flavor will be discovered and replaced
1768 * shortly, and let _this_ rpc pass through */
1769 if (exp->exp_flvr_changed) {
1770 LASSERT(exp->exp_flvr_adapt);
1771 cfs_spin_unlock(&exp->exp_lock);
1775 if (exp->exp_flvr_adapt) {
1776 exp->exp_flvr_adapt = 0;
1777 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1778 exp, exp->exp_flvr.sf_rpc,
1779 exp->exp_flvr_old[0].sf_rpc,
1780 exp->exp_flvr_old[1].sf_rpc);
1781 flavor = exp->exp_flvr;
1782 cfs_spin_unlock(&exp->exp_lock);
1784 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1788 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1789 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1790 exp->exp_flvr_old[0].sf_rpc,
1791 exp->exp_flvr_old[1].sf_rpc);
1792 cfs_spin_unlock(&exp->exp_lock);
1794 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1799 if (exp->exp_flvr_expire[0]) {
1800 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1801 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1802 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1803 "middle one ("CFS_DURATION_T")\n", exp,
1804 exp->exp_flvr.sf_rpc,
1805 exp->exp_flvr_old[0].sf_rpc,
1806 exp->exp_flvr_old[1].sf_rpc,
1807 exp->exp_flvr_expire[0] -
1808 cfs_time_current_sec());
1809 cfs_spin_unlock(&exp->exp_lock);
1813 CDEBUG(D_SEC, "mark middle expired\n");
1814 exp->exp_flvr_expire[0] = 0;
1816 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1817 exp->exp_flvr.sf_rpc,
1818 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1819 req->rq_flvr.sf_rpc);
1822 /* now it doesn't match the current flavor, the only chance we can
1823 * accept it is match the old flavors which is not expired. */
1824 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1825 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1826 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1827 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1828 "oldest one ("CFS_DURATION_T")\n", exp,
1829 exp->exp_flvr.sf_rpc,
1830 exp->exp_flvr_old[0].sf_rpc,
1831 exp->exp_flvr_old[1].sf_rpc,
1832 exp->exp_flvr_expire[1] -
1833 cfs_time_current_sec());
1834 cfs_spin_unlock(&exp->exp_lock);
1838 CDEBUG(D_SEC, "mark oldest expired\n");
1839 exp->exp_flvr_expire[1] = 0;
1841 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1842 exp, exp->exp_flvr.sf_rpc,
1843 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1844 req->rq_flvr.sf_rpc);
1846 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1847 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1848 exp->exp_flvr_old[1].sf_rpc);
1851 cfs_spin_unlock(&exp->exp_lock);
1853 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u) with "
1854 "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1855 exp, exp->exp_obd->obd_name,
1856 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1857 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_flvr.sf_rpc,
1858 exp->exp_flvr.sf_rpc,
1859 exp->exp_flvr_old[0].sf_rpc,
1860 exp->exp_flvr_expire[0] ?
1861 (unsigned long) (exp->exp_flvr_expire[0] -
1862 cfs_time_current_sec()) : 0,
1863 exp->exp_flvr_old[1].sf_rpc,
1864 exp->exp_flvr_expire[1] ?
1865 (unsigned long) (exp->exp_flvr_expire[1] -
1866 cfs_time_current_sec()) : 0);
1869 EXPORT_SYMBOL(sptlrpc_target_export_check);
1871 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1872 struct sptlrpc_rule_set *rset)
1874 struct obd_export *exp;
1875 struct sptlrpc_flavor new_flvr;
1879 cfs_spin_lock(&obd->obd_dev_lock);
1881 cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1882 if (exp->exp_connection == NULL)
1885 /* note if this export had just been updated flavor
1886 * (exp_flvr_changed == 1), this will override the
1888 cfs_spin_lock(&exp->exp_lock);
1889 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1890 exp->exp_connection->c_peer.nid,
1892 if (exp->exp_flvr_changed ||
1893 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1894 exp->exp_flvr_old[1] = new_flvr;
1895 exp->exp_flvr_expire[1] = 0;
1896 exp->exp_flvr_changed = 1;
1897 exp->exp_flvr_adapt = 1;
1899 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1900 exp, sptlrpc_part2name(exp->exp_sp_peer),
1901 exp->exp_flvr.sf_rpc,
1902 exp->exp_flvr_old[1].sf_rpc);
1904 cfs_spin_unlock(&exp->exp_lock);
1907 cfs_spin_unlock(&obd->obd_dev_lock);
1909 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1911 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1913 if (svc_rc == SECSVC_DROP)
1916 switch (req->rq_sp_from) {
1925 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1929 if (!req->rq_auth_gss)
1932 if (unlikely(req->rq_sp_from == LUSTRE_SP_ANY)) {
1933 CERROR("not specific part\n");
1937 /* from MDT, must be authenticated as MDT */
1938 if (unlikely(req->rq_sp_from == LUSTRE_SP_MDT &&
1939 !req->rq_auth_usr_mdt)) {
1940 DEBUG_REQ(D_ERROR, req, "fake source MDT");
1944 /* from OST, must be callback to MDT and CLI, the reverse sec
1945 * was from mdt/root keytab, so it should be MDT or root FIXME */
1946 if (unlikely(req->rq_sp_from == LUSTRE_SP_OST &&
1947 !req->rq_auth_usr_mdt && !req->rq_auth_usr_root)) {
1948 DEBUG_REQ(D_ERROR, req, "fake source OST");
1955 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1957 struct ptlrpc_sec_policy *policy;
1958 struct lustre_msg *msg = req->rq_reqbuf;
1963 LASSERT(req->rq_reqmsg == NULL);
1964 LASSERT(req->rq_repmsg == NULL);
1965 LASSERT(req->rq_svc_ctx == NULL);
1967 req->rq_req_swab_mask = 0;
1969 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
1972 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1976 CERROR("error unpacking request from %s x"LPU64"\n",
1977 libcfs_id2str(req->rq_peer), req->rq_xid);
1978 RETURN(SECSVC_DROP);
1981 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
1982 req->rq_sp_from = LUSTRE_SP_ANY;
1983 req->rq_auth_uid = INVALID_UID;
1984 req->rq_auth_mapped_uid = INVALID_UID;
1986 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
1988 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1989 RETURN(SECSVC_DROP);
1992 LASSERT(policy->sp_sops->accept);
1993 rc = policy->sp_sops->accept(req);
1994 sptlrpc_policy_put(policy);
1995 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1996 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1999 * if it's not null flavor (which means embedded packing msg),
2000 * reset the swab mask for the comming inner msg unpacking.
2002 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2003 req->rq_req_swab_mask = 0;
2005 /* sanity check for the request source */
2006 rc = sptlrpc_svc_check_from(req, rc);
2010 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
2013 struct ptlrpc_sec_policy *policy;
2014 struct ptlrpc_reply_state *rs;
2018 LASSERT(req->rq_svc_ctx);
2019 LASSERT(req->rq_svc_ctx->sc_policy);
2021 policy = req->rq_svc_ctx->sc_policy;
2022 LASSERT(policy->sp_sops->alloc_rs);
2024 rc = policy->sp_sops->alloc_rs(req, msglen);
2025 if (unlikely(rc == -ENOMEM)) {
2026 /* failed alloc, try emergency pool */
2027 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
2031 req->rq_reply_state = rs;
2032 rc = policy->sp_sops->alloc_rs(req, msglen);
2034 lustre_put_emerg_rs(rs);
2035 req->rq_reply_state = NULL;
2040 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2045 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2047 struct ptlrpc_sec_policy *policy;
2051 LASSERT(req->rq_svc_ctx);
2052 LASSERT(req->rq_svc_ctx->sc_policy);
2054 policy = req->rq_svc_ctx->sc_policy;
2055 LASSERT(policy->sp_sops->authorize);
2057 rc = policy->sp_sops->authorize(req);
2058 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2063 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2065 struct ptlrpc_sec_policy *policy;
2066 unsigned int prealloc;
2069 LASSERT(rs->rs_svc_ctx);
2070 LASSERT(rs->rs_svc_ctx->sc_policy);
2072 policy = rs->rs_svc_ctx->sc_policy;
2073 LASSERT(policy->sp_sops->free_rs);
2075 prealloc = rs->rs_prealloc;
2076 policy->sp_sops->free_rs(rs);
2079 lustre_put_emerg_rs(rs);
2083 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2085 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2090 LASSERT(cfs_atomic_read(&ctx->sc_refcount) > 0);
2091 cfs_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(cfs_atomic_read(&ctx->sc_refcount) > 0);
2102 if (cfs_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 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2111 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2116 LASSERT(cfs_atomic_read(&ctx->sc_refcount) > 0);
2117 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2118 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2120 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2122 /****************************************
2124 ****************************************/
2126 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2127 struct ptlrpc_bulk_desc *desc)
2129 struct ptlrpc_cli_ctx *ctx;
2131 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2133 if (!req->rq_pack_bulk)
2136 ctx = req->rq_cli_ctx;
2137 if (ctx->cc_ops->wrap_bulk)
2138 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2141 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2144 * return nob of actual plain text size received, or error code.
2146 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2147 struct ptlrpc_bulk_desc *desc,
2150 struct ptlrpc_cli_ctx *ctx;
2153 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2155 if (!req->rq_pack_bulk)
2156 return desc->bd_nob_transferred;
2158 ctx = req->rq_cli_ctx;
2159 if (ctx->cc_ops->unwrap_bulk) {
2160 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2164 return desc->bd_nob_transferred;
2166 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2169 * return 0 for success or error code.
2171 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2172 struct ptlrpc_bulk_desc *desc)
2174 struct ptlrpc_cli_ctx *ctx;
2177 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2179 if (!req->rq_pack_bulk)
2182 ctx = req->rq_cli_ctx;
2183 if (ctx->cc_ops->unwrap_bulk) {
2184 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2190 * if everything is going right, nob should equals to nob_transferred.
2191 * in case of privacy mode, nob_transferred needs to be adjusted.
2193 if (desc->bd_nob != desc->bd_nob_transferred) {
2194 CERROR("nob %d doesn't match transferred nob %d",
2195 desc->bd_nob, desc->bd_nob_transferred);
2201 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2203 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2204 struct ptlrpc_bulk_desc *desc)
2206 struct ptlrpc_svc_ctx *ctx;
2208 LASSERT(req->rq_bulk_read);
2210 if (!req->rq_pack_bulk)
2213 ctx = req->rq_svc_ctx;
2214 if (ctx->sc_policy->sp_sops->wrap_bulk)
2215 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2219 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2221 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2222 struct ptlrpc_bulk_desc *desc)
2224 struct ptlrpc_svc_ctx *ctx;
2227 LASSERT(req->rq_bulk_write);
2230 * if it's in privacy mode, transferred should >= expected; otherwise
2231 * transferred should == expected.
2233 if (desc->bd_nob_transferred < desc->bd_nob ||
2234 (desc->bd_nob_transferred > desc->bd_nob &&
2235 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2236 SPTLRPC_BULK_SVC_PRIV)) {
2237 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2238 desc->bd_nob_transferred, desc->bd_nob);
2242 if (!req->rq_pack_bulk)
2245 ctx = req->rq_svc_ctx;
2246 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2247 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2249 CERROR("error unwrap bulk: %d\n", rc);
2252 /* return 0 to allow reply be sent */
2255 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2257 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2258 struct ptlrpc_bulk_desc *desc)
2260 struct ptlrpc_svc_ctx *ctx;
2262 LASSERT(req->rq_bulk_write);
2264 if (!req->rq_pack_bulk)
2267 ctx = req->rq_svc_ctx;
2268 if (ctx->sc_policy->sp_sops->prep_bulk)
2269 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2273 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2275 /****************************************
2276 * user descriptor helpers *
2277 ****************************************/
2279 int sptlrpc_current_user_desc_size(void)
2284 ngroups = current_ngroups;
2286 if (ngroups > LUSTRE_MAX_GROUPS)
2287 ngroups = LUSTRE_MAX_GROUPS;
2291 return sptlrpc_user_desc_size(ngroups);
2293 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2295 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2297 struct ptlrpc_user_desc *pud;
2299 pud = lustre_msg_buf(msg, offset, 0);
2301 pud->pud_uid = cfs_curproc_uid();
2302 pud->pud_gid = cfs_curproc_gid();
2303 pud->pud_fsuid = cfs_curproc_fsuid();
2304 pud->pud_fsgid = cfs_curproc_fsgid();
2305 pud->pud_cap = cfs_curproc_cap_pack();
2306 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2310 if (pud->pud_ngroups > current_ngroups)
2311 pud->pud_ngroups = current_ngroups;
2312 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2313 pud->pud_ngroups * sizeof(__u32));
2314 task_unlock(current);
2319 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2321 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2323 struct ptlrpc_user_desc *pud;
2326 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2331 __swab32s(&pud->pud_uid);
2332 __swab32s(&pud->pud_gid);
2333 __swab32s(&pud->pud_fsuid);
2334 __swab32s(&pud->pud_fsgid);
2335 __swab32s(&pud->pud_cap);
2336 __swab32s(&pud->pud_ngroups);
2339 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2340 CERROR("%u groups is too large\n", pud->pud_ngroups);
2344 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2345 msg->lm_buflens[offset]) {
2346 CERROR("%u groups are claimed but bufsize only %u\n",
2347 pud->pud_ngroups, msg->lm_buflens[offset]);
2352 for (i = 0; i < pud->pud_ngroups; i++)
2353 __swab32s(&pud->pud_groups[i]);
2358 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2360 /****************************************
2362 ****************************************/
2364 const char * sec2target_str(struct ptlrpc_sec *sec)
2366 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2368 if (sec_is_reverse(sec))
2370 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2372 EXPORT_SYMBOL(sec2target_str);
2375 * return true if the bulk data is protected
2377 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2379 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2380 case SPTLRPC_BULK_SVC_INTG:
2381 case SPTLRPC_BULK_SVC_PRIV:
2387 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2389 /****************************************
2390 * crypto API helper/alloc blkciper *
2391 ****************************************/
2393 /****************************************
2394 * initialize/finalize *
2395 ****************************************/
2397 int __init sptlrpc_init(void)
2401 cfs_rwlock_init(&policy_lock);
2403 rc = sptlrpc_gc_init();
2407 rc = sptlrpc_conf_init();
2411 rc = sptlrpc_enc_pool_init();
2415 rc = sptlrpc_null_init();
2419 rc = sptlrpc_plain_init();
2423 rc = sptlrpc_lproc_init();
2430 sptlrpc_plain_fini();
2432 sptlrpc_null_fini();
2434 sptlrpc_enc_pool_fini();
2436 sptlrpc_conf_fini();
2443 void __exit sptlrpc_fini(void)
2445 sptlrpc_lproc_fini();
2446 sptlrpc_plain_fini();
2447 sptlrpc_null_fini();
2448 sptlrpc_enc_pool_fini();
2449 sptlrpc_conf_fini();