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 rwlock_t policy_lock = RW_LOCK_UNLOCKED;
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 write_lock(&policy_lock);
86 if (unlikely(policies[number])) {
87 write_unlock(&policy_lock);
90 policies[number] = policy;
91 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 write_lock(&policy_lock);
105 if (unlikely(policies[number] == NULL)) {
106 write_unlock(&policy_lock);
107 CERROR("%s: already unregistered\n", policy->sp_name);
111 LASSERT(policies[number] == policy);
112 policies[number] = NULL;
113 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_rpcflavor2policy(__u16 flavor)
123 static DECLARE_MUTEX(load_mutex);
124 static atomic_t loaded = ATOMIC_INIT(0);
125 struct ptlrpc_sec_policy *policy;
126 __u16 number = RPC_FLVR_POLICY(flavor), flag = 0;
128 if (number >= SPTLRPC_POLICY_MAX)
132 read_lock(&policy_lock);
133 policy = policies[number];
134 if (policy && !try_module_get(policy->sp_owner))
137 flag = atomic_read(&loaded);
138 read_unlock(&policy_lock);
140 if (policy != NULL || flag != 0 ||
141 number != SPTLRPC_POLICY_GSS)
144 /* try to load gss module, once */
145 mutex_down(&load_mutex);
146 if (atomic_read(&loaded) == 0) {
147 if (request_module("ptlrpc_gss") == 0)
148 CWARN("module ptlrpc_gss loaded on demand\n");
150 CERROR("Unable to load module ptlrpc_gss\n");
152 atomic_set(&loaded, 1);
154 mutex_up(&load_mutex);
160 __u16 sptlrpc_name2rpcflavor(const char *name)
162 if (!strcmp(name, "null"))
163 return SPTLRPC_FLVR_NULL;
164 if (!strcmp(name, "plain"))
165 return SPTLRPC_FLVR_PLAIN;
166 if (!strcmp(name, "krb5n"))
167 return SPTLRPC_FLVR_KRB5N;
168 if (!strcmp(name, "krb5a"))
169 return SPTLRPC_FLVR_KRB5A;
170 if (!strcmp(name, "krb5i"))
171 return SPTLRPC_FLVR_KRB5I;
172 if (!strcmp(name, "krb5p"))
173 return SPTLRPC_FLVR_KRB5P;
175 return SPTLRPC_FLVR_INVALID;
177 EXPORT_SYMBOL(sptlrpc_name2rpcflavor);
179 const char *sptlrpc_rpcflavor2name(__u16 flavor)
182 case SPTLRPC_FLVR_NULL:
184 case SPTLRPC_FLVR_PLAIN:
186 case SPTLRPC_FLVR_KRB5N:
188 case SPTLRPC_FLVR_KRB5A:
190 case SPTLRPC_FLVR_KRB5I:
192 case SPTLRPC_FLVR_KRB5P:
195 CERROR("invalid rpc flavor 0x%x(p%u,s%u,v%u)\n", flavor,
196 RPC_FLVR_POLICY(flavor), RPC_FLVR_MECH(flavor),
197 RPC_FLVR_SVC(flavor));
201 EXPORT_SYMBOL(sptlrpc_rpcflavor2name);
203 int sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
207 if (sf->sf_bulk_ciph != BULK_CIPH_ALG_NULL)
209 else if (sf->sf_bulk_hash != BULK_HASH_ALG_NULL)
214 snprintf(buf, bufsize, "%s-%s:%s/%s",
215 sptlrpc_rpcflavor2name(sf->sf_rpc), bulk,
216 sptlrpc_get_hash_name(sf->sf_bulk_hash),
217 sptlrpc_get_ciph_name(sf->sf_bulk_ciph));
220 EXPORT_SYMBOL(sptlrpc_flavor2name);
222 /**************************************************
223 * client context APIs *
224 **************************************************/
227 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
229 struct vfs_cred vcred;
230 int create = 1, remove_dead = 1;
233 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
235 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
236 PTLRPC_SEC_FL_ROOTONLY)) {
239 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
244 vcred.vc_uid = cfs_current()->uid;
245 vcred.vc_gid = cfs_current()->gid;
248 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
249 create, remove_dead);
252 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
254 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
255 atomic_inc(&ctx->cc_refcount);
258 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
260 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
262 struct ptlrpc_sec *sec = ctx->cc_sec;
265 LASSERT(atomic_read(&ctx->cc_refcount));
267 if (!atomic_dec_and_test(&ctx->cc_refcount))
270 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
272 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
275 * expire the context immediately.
276 * the caller must hold at least 1 ref on the ctx.
278 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
280 LASSERT(ctx->cc_ops->die);
281 ctx->cc_ops->die(ctx, 0);
283 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
285 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
287 struct ptlrpc_request *req, *next;
289 spin_lock(&ctx->cc_lock);
290 list_for_each_entry_safe(req, next, &ctx->cc_req_list, rq_ctx_chain) {
291 list_del_init(&req->rq_ctx_chain);
292 ptlrpc_wake_client_req(req);
294 spin_unlock(&ctx->cc_lock);
296 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
298 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
300 LASSERT(ctx->cc_ops);
302 if (ctx->cc_ops->display == NULL)
305 return ctx->cc_ops->display(ctx, buf, bufsize);
308 static int sptlrpc_import_sec_check_expire(struct obd_import *imp)
312 spin_lock(&imp->imp_lock);
313 if (imp->imp_sec_expire &&
314 imp->imp_sec_expire < cfs_time_current_sec()) {
316 imp->imp_sec_expire = 0;
318 spin_unlock(&imp->imp_lock);
323 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
324 return sptlrpc_import_sec_adapt(imp, NULL, 0);
327 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
329 struct obd_import *imp = req->rq_import;
330 struct ptlrpc_sec *sec;
334 LASSERT(!req->rq_cli_ctx);
337 if (unlikely(imp->imp_sec_expire)) {
338 rc = sptlrpc_import_sec_check_expire(imp);
343 sec = sptlrpc_import_sec_ref(imp);
345 CERROR("import %p (%s) with no ptlrpc_sec\n",
346 imp, ptlrpc_import_state_name(imp->imp_state));
350 if (unlikely(sec->ps_dying)) {
351 CERROR("attempt to use dying sec %p\n", sec);
355 req->rq_cli_ctx = get_my_ctx(sec);
357 sptlrpc_sec_put(sec);
359 if (!req->rq_cli_ctx) {
360 CERROR("req %p: fail to get context\n", req);
368 * if @sync == 0, this function should return quickly without sleep;
369 * otherwise might trigger ctx destroying rpc to server.
371 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
376 LASSERT(req->rq_cli_ctx);
378 /* request might be asked to release earlier while still
379 * in the context waiting list.
381 if (!list_empty(&req->rq_ctx_chain)) {
382 spin_lock(&req->rq_cli_ctx->cc_lock);
383 list_del_init(&req->rq_ctx_chain);
384 spin_unlock(&req->rq_cli_ctx->cc_lock);
387 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
388 req->rq_cli_ctx = NULL;
393 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
394 struct ptlrpc_cli_ctx *oldctx,
395 struct ptlrpc_cli_ctx *newctx)
397 struct sptlrpc_flavor old_flvr;
402 if (likely(oldctx->cc_sec == newctx->cc_sec))
405 LASSERT(req->rq_reqmsg);
406 LASSERT(req->rq_reqlen);
407 LASSERT(req->rq_replen);
409 CWARN("req %p: switch ctx %p -> %p, switch sec %p(%s) -> %p(%s)\n",
411 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
412 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
415 old_flvr = req->rq_flvr;
417 /* save request message */
418 reqmsg_size = req->rq_reqlen;
419 OBD_ALLOC(reqmsg, reqmsg_size);
422 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
424 /* release old req/rep buf */
425 req->rq_cli_ctx = oldctx;
426 sptlrpc_cli_free_reqbuf(req);
427 sptlrpc_cli_free_repbuf(req);
428 req->rq_cli_ctx = newctx;
430 /* recalculate the flavor */
431 sptlrpc_req_set_flavor(req, 0);
433 /* alloc new request buffer
434 * we don't need to alloc reply buffer here, leave it to the
435 * rest procedure of ptlrpc
437 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
439 LASSERT(req->rq_reqmsg);
440 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
442 CWARN("failed to alloc reqbuf: %d\n", rc);
443 req->rq_flvr = old_flvr;
446 OBD_FREE(reqmsg, reqmsg_size);
451 * request must have a context. in any case of failure, restore the
452 * restore the old one. a request must have a ctx.
454 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
456 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
457 struct ptlrpc_cli_ctx *newctx;
462 LASSERT(test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags));
464 sptlrpc_cli_ctx_get(oldctx);
465 sptlrpc_req_put_ctx(req, 0);
467 rc = sptlrpc_req_get_ctx(req);
469 LASSERT(!req->rq_cli_ctx);
471 /* restore old ctx */
472 req->rq_cli_ctx = oldctx;
476 newctx = req->rq_cli_ctx;
479 if (unlikely(newctx == oldctx)) {
481 * still get the old ctx, usually means system busy
483 CWARN("ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
484 newctx, newctx->cc_flags);
486 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, HZ);
488 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
490 /* restore old ctx */
491 sptlrpc_req_put_ctx(req, 0);
492 req->rq_cli_ctx = oldctx;
496 LASSERT(req->rq_cli_ctx == newctx);
499 sptlrpc_cli_ctx_put(oldctx, 1);
502 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
505 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
507 if (cli_ctx_is_refreshed(ctx))
513 int ctx_refresh_timeout(void *data)
515 struct ptlrpc_request *req = data;
518 /* conn_cnt is needed in expire_one_request */
519 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
521 rc = ptlrpc_expire_one_request(req);
522 /* if we started recovery, we should mark this ctx dead; otherwise
523 * in case of lgssd died nobody would retire this ctx, following
524 * connecting will still find the same ctx thus cause deadlock.
525 * there's an assumption that expire time of the request should be
526 * later than the context refresh expire time.
529 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
534 void ctx_refresh_interrupt(void *data)
536 struct ptlrpc_request *req = data;
538 spin_lock(&req->rq_lock);
540 spin_unlock(&req->rq_lock);
544 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
546 spin_lock(&ctx->cc_lock);
547 if (!list_empty(&req->rq_ctx_chain))
548 list_del_init(&req->rq_ctx_chain);
549 spin_unlock(&ctx->cc_lock);
553 * the status of context could be subject to be changed by other threads at any
554 * time. we allow this race. but once we return with 0, the caller will
555 * suppose it's uptodated and keep using it until the owning rpc is done.
559 * = 0 - wait until success or fatal error occur
560 * > 0 - timeout value
562 * return 0 only if the context is uptodated.
564 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
566 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
567 struct l_wait_info lwi;
574 * during the process a request's context might change type even
575 * (e.g. from gss ctx to plain ctx), so each loop we need to re-check
579 /* skip special ctxs */
580 if (cli_ctx_is_eternal(ctx) || req->rq_ctx_init || req->rq_ctx_fini)
583 if (test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags)) {
584 LASSERT(ctx->cc_ops->refresh);
585 ctx->cc_ops->refresh(ctx);
587 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
589 LASSERT(ctx->cc_ops->validate);
590 if (ctx->cc_ops->validate(ctx) == 0) {
591 req_off_ctx_list(req, ctx);
595 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
597 req_off_ctx_list(req, ctx);
601 /* This is subtle. For resent message we have to keep original
602 * context to survive following situation:
603 * 1. the request sent to server
604 * 2. recovery was kick start
605 * 3. recovery finished, the request marked as resent
606 * 4. resend the request
607 * 5. old reply from server received (because xid is the same)
608 * 6. verify reply (has to be success)
609 * 7. new reply from server received, lnet drop it
611 * Note we can't simply change xid for resent request because
612 * server reply on it for reply reconstruction.
614 * Commonly the original context should be uptodate because we
615 * have a expiry nice time; And server will keep their half part
616 * context because we at least hold a ref of old context which
617 * prevent the context detroy RPC be sent. So server still can
618 * accept the request and finish RPC. Two cases:
619 * 1. If server side context has been trimed, a NO_CONTEXT will
620 * be returned, gss_cli_ctx_verify/unseal will switch to new
622 * 2. Current context never be refreshed, then we are fine: we
623 * never really send request with old context before.
625 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
626 unlikely(req->rq_reqmsg) &&
627 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
628 req_off_ctx_list(req, ctx);
632 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
633 rc = sptlrpc_req_replace_dead_ctx(req);
635 LASSERT(ctx == req->rq_cli_ctx);
636 CERROR("req %p: failed to replace dead ctx %p: %d\n",
639 LASSERT(list_empty(&req->rq_ctx_chain));
643 CWARN("req %p: replace dead ctx %p => ctx %p (%u->%s)\n",
644 req, ctx, req->rq_cli_ctx,
645 req->rq_cli_ctx->cc_vcred.vc_uid,
646 sec2target_str(req->rq_cli_ctx->cc_sec));
648 ctx = req->rq_cli_ctx;
649 LASSERT(list_empty(&req->rq_ctx_chain));
654 /* Now we're sure this context is during upcall, add myself into
657 spin_lock(&ctx->cc_lock);
658 if (list_empty(&req->rq_ctx_chain))
659 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
660 spin_unlock(&ctx->cc_lock);
663 RETURN(-EWOULDBLOCK);
666 /* Clear any flags that may be present from previous sends */
667 LASSERT(req->rq_receiving_reply == 0);
668 spin_lock(&req->rq_lock);
670 req->rq_timedout = 0;
673 spin_unlock(&req->rq_lock);
675 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
676 ctx_refresh_interrupt, req);
677 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
679 /* following cases we could be here:
680 * - successfully refreshed;
682 * - timedout, and we don't want recover from the failure;
683 * - timedout, and waked up upon recovery finished;
684 * - someone else mark this ctx dead by force;
685 * - someone invalidate the req and call wake_client_req(),
686 * e.g. ptlrpc_abort_inflight();
688 if (!cli_ctx_is_refreshed(ctx)) {
689 /* timed out or interruptted */
690 req_off_ctx_list(req, ctx);
700 * Note this could be called in two situations:
701 * - new request from ptlrpc_pre_req(), with proper @opcode
702 * - old request which changed ctx in the middle, with @opcode == 0
704 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
706 struct ptlrpc_sec *sec;
708 LASSERT(req->rq_import);
709 LASSERT(req->rq_cli_ctx);
710 LASSERT(req->rq_cli_ctx->cc_sec);
711 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
713 /* special security flags accoding to opcode */
716 req->rq_bulk_read = 1;
719 req->rq_bulk_write = 1;
722 req->rq_ctx_init = 1;
725 req->rq_ctx_fini = 1;
728 /* init/fini rpc won't be resend, so can't be here */
729 LASSERT(req->rq_ctx_init == 0);
730 LASSERT(req->rq_ctx_fini == 0);
732 /* cleanup flags, which should be recalculated */
733 req->rq_pack_udesc = 0;
734 req->rq_pack_bulk = 0;
738 sec = req->rq_cli_ctx->cc_sec;
740 spin_lock(&sec->ps_lock);
741 req->rq_flvr = sec->ps_flvr;
742 spin_unlock(&sec->ps_lock);
744 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
746 if (unlikely(req->rq_ctx_init))
747 rpc_flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
748 else if (unlikely(req->rq_ctx_fini))
749 rpc_flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
751 /* user descriptor flag, null security can't do it anyway */
752 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
753 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
754 req->rq_pack_udesc = 1;
756 /* bulk security flag */
757 if ((req->rq_bulk_read || req->rq_bulk_write) &&
758 (req->rq_flvr.sf_bulk_ciph != BULK_CIPH_ALG_NULL ||
759 req->rq_flvr.sf_bulk_hash != BULK_HASH_ALG_NULL))
760 req->rq_pack_bulk = 1;
763 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
765 if (RPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
768 LASSERT(req->rq_clrbuf);
769 if (req->rq_pool || !req->rq_reqbuf)
772 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
773 req->rq_reqbuf = NULL;
774 req->rq_reqbuf_len = 0;
778 * check whether current user have valid context for an import or not.
779 * might repeatedly try in case of non-fatal errors.
780 * return 0 on success, < 0 on failure
782 int sptlrpc_import_check_ctx(struct obd_import *imp)
784 struct ptlrpc_sec *sec;
785 struct ptlrpc_cli_ctx *ctx;
786 struct ptlrpc_request *req = NULL;
792 sec = sptlrpc_import_sec_ref(imp);
793 ctx = get_my_ctx(sec);
794 sptlrpc_sec_put(sec);
799 if (cli_ctx_is_eternal(ctx) ||
800 ctx->cc_ops->validate(ctx) == 0) {
801 sptlrpc_cli_ctx_put(ctx, 1);
809 spin_lock_init(&req->rq_lock);
810 atomic_set(&req->rq_refcount, 10000);
811 CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
812 cfs_waitq_init(&req->rq_reply_waitq);
813 req->rq_import = imp;
814 req->rq_cli_ctx = ctx;
816 rc = sptlrpc_req_refresh_ctx(req, 0);
817 LASSERT(list_empty(&req->rq_ctx_chain));
818 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
824 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
826 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
831 LASSERT(ctx->cc_sec);
832 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
834 /* we wrap bulk request here because now we can be sure
835 * the context is uptodate.
838 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
843 switch (RPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
844 case SPTLRPC_SVC_NULL:
845 case SPTLRPC_SVC_AUTH:
846 case SPTLRPC_SVC_INTG:
847 LASSERT(ctx->cc_ops->sign);
848 rc = ctx->cc_ops->sign(ctx, req);
850 case SPTLRPC_SVC_PRIV:
851 LASSERT(ctx->cc_ops->seal);
852 rc = ctx->cc_ops->seal(ctx, req);
859 LASSERT(req->rq_reqdata_len);
860 LASSERT(req->rq_reqdata_len % 8 == 0);
861 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
867 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
869 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
875 LASSERT(ctx->cc_sec);
876 LASSERT(req->rq_repbuf);
877 LASSERT(req->rq_repdata);
878 LASSERT(req->rq_repmsg == NULL);
880 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
881 CERROR("replied data length %d too small\n",
882 req->rq_repdata_len);
886 /* v2 message, check request/reply policy match */
887 rpc_flvr = WIRE_FLVR_RPC(req->rq_repdata->lm_secflvr);
889 if (req->rq_repdata->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
890 __swab16s(&rpc_flvr);
892 if (RPC_FLVR_POLICY(rpc_flvr) !=
893 RPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
894 CERROR("request policy was %u while reply with %u\n",
895 RPC_FLVR_POLICY(req->rq_flvr.sf_rpc),
896 RPC_FLVR_POLICY(rpc_flvr));
900 /* do nothing if it's null policy; otherwise unpack the
902 if (RPC_FLVR_POLICY(rpc_flvr) != SPTLRPC_POLICY_NULL &&
903 lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len))
906 switch (RPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
907 case SPTLRPC_SVC_NULL:
908 case SPTLRPC_SVC_AUTH:
909 case SPTLRPC_SVC_INTG:
910 LASSERT(ctx->cc_ops->verify);
911 rc = ctx->cc_ops->verify(ctx, req);
913 case SPTLRPC_SVC_PRIV:
914 LASSERT(ctx->cc_ops->unseal);
915 rc = ctx->cc_ops->unseal(ctx, req);
921 LASSERT(rc || req->rq_repmsg || req->rq_resend);
926 * upon this be called, the reply buffer should have been un-posted,
927 * so nothing is going to change.
929 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
931 LASSERT(req->rq_repbuf);
932 LASSERT(req->rq_repdata == NULL);
933 LASSERT(req->rq_repmsg == NULL);
934 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
936 if (req->rq_reply_off == 0) {
937 CERROR("real reply with offset 0\n");
941 if (req->rq_reply_off % 8 != 0) {
942 CERROR("reply at odd offset %u\n", req->rq_reply_off);
946 req->rq_repdata = (struct lustre_msg *)
947 (req->rq_repbuf + req->rq_reply_off);
948 req->rq_repdata_len = req->rq_nob_received;
950 return do_cli_unwrap_reply(req);
954 * Upon called, the receive buffer might be still posted, so the reply data
955 * might be changed at any time, no matter we're holding rq_lock or not. we
956 * expect the rq_reply_off be 0, rq_nob_received is the early reply size.
958 * we allocate a separate buffer to hold early reply data, pointed by
959 * rq_repdata, rq_repdata_len is the early reply size, and round up to power2
960 * is the actual buffer size.
962 * caller _must_ call sptlrpc_cli_finish_early_reply() after this, before
963 * process another early reply or real reply, to restore ptlrpc_request
966 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req)
968 struct lustre_msg *early_buf;
969 int early_bufsz, early_size;
973 LASSERT(req->rq_repbuf);
974 LASSERT(req->rq_repdata == NULL);
975 LASSERT(req->rq_repmsg == NULL);
977 early_size = req->rq_nob_received;
978 if (early_size < sizeof(struct lustre_msg)) {
979 CERROR("early reply length %d too small\n", early_size);
983 early_bufsz = size_roundup_power2(early_size);
984 OBD_ALLOC(early_buf, early_bufsz);
985 if (early_buf == NULL)
988 /* copy data out, do it inside spinlock */
989 spin_lock(&req->rq_lock);
991 if (req->rq_replied) {
992 spin_unlock(&req->rq_lock);
993 GOTO(err_free, rc = -EALREADY);
996 if (req->rq_reply_off != 0) {
997 CERROR("early reply with offset %u\n", req->rq_reply_off);
998 GOTO(err_free, rc = -EPROTO);
1001 if (req->rq_nob_received != early_size) {
1002 /* even another early arrived the size should be the same */
1003 CWARN("data size has changed from %u to %u\n",
1004 early_size, req->rq_nob_received);
1005 spin_unlock(&req->rq_lock);
1006 GOTO(err_free, rc = -EINVAL);
1009 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1010 CERROR("early reply length %d too small\n",
1011 req->rq_nob_received);
1012 spin_unlock(&req->rq_lock);
1013 GOTO(err_free, rc = -EALREADY);
1016 memcpy(early_buf, req->rq_repbuf, early_size);
1017 spin_unlock(&req->rq_lock);
1019 req->rq_repdata = early_buf;
1020 req->rq_repdata_len = early_size;
1022 rc = do_cli_unwrap_reply(req);
1024 /* treate resend as an error case. in fact server should never ask
1025 * resend via early reply. */
1026 if (req->rq_resend) {
1032 LASSERT(req->rq_repmsg == NULL);
1033 req->rq_repdata = NULL;
1034 req->rq_repdata_len = 0;
1038 LASSERT(req->rq_repmsg);
1042 OBD_FREE(early_buf, early_bufsz);
1046 int sptlrpc_cli_finish_early_reply(struct ptlrpc_request *req)
1050 LASSERT(req->rq_repdata);
1051 LASSERT(req->rq_repdata_len);
1052 LASSERT(req->rq_repmsg);
1054 early_bufsz = size_roundup_power2(req->rq_repdata_len);
1055 OBD_FREE(req->rq_repdata, early_bufsz);
1057 req->rq_repdata = NULL;
1058 req->rq_repdata_len = 0;
1059 req->rq_repmsg = NULL;
1063 /**************************************************
1065 **************************************************/
1068 * "fixed" sec (e.g. null) use sec_id < 0
1070 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1072 int sptlrpc_get_next_secid(void)
1074 return atomic_inc_return(&sptlrpc_sec_id);
1076 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1078 /**************************************************
1079 * client side high-level security APIs *
1080 **************************************************/
1082 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1083 int grace, int force)
1085 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1087 LASSERT(policy->sp_cops);
1088 LASSERT(policy->sp_cops->flush_ctx_cache);
1090 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1093 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1095 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1097 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1098 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1099 LASSERT(policy->sp_cops->destroy_sec);
1101 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1103 policy->sp_cops->destroy_sec(sec);
1104 sptlrpc_policy_put(policy);
1107 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1109 sec_cop_destroy_sec(sec);
1111 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1113 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1115 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1117 if (sec->ps_policy->sp_cops->kill_sec) {
1118 sec->ps_policy->sp_cops->kill_sec(sec);
1120 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1124 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1127 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1128 atomic_inc(&sec->ps_refcount);
1133 EXPORT_SYMBOL(sptlrpc_sec_get);
1135 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1138 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1140 if (atomic_dec_and_test(&sec->ps_refcount)) {
1141 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1143 sptlrpc_gc_del_sec(sec);
1144 sec_cop_destroy_sec(sec);
1148 EXPORT_SYMBOL(sptlrpc_sec_put);
1151 * it's policy module responsible for taking refrence of import
1154 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1155 struct ptlrpc_svc_ctx *svc_ctx,
1156 struct sptlrpc_flavor *sf,
1157 enum lustre_sec_part sp)
1159 struct ptlrpc_sec_policy *policy;
1160 struct ptlrpc_sec *sec;
1164 LASSERT(imp->imp_dlm_fake == 1);
1166 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1167 imp->imp_obd->obd_type->typ_name,
1168 imp->imp_obd->obd_name,
1169 sptlrpc_rpcflavor2name(sf->sf_rpc));
1171 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1172 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1174 LASSERT(imp->imp_dlm_fake == 0);
1176 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1177 imp->imp_obd->obd_type->typ_name,
1178 imp->imp_obd->obd_name,
1179 sptlrpc_rpcflavor2name(sf->sf_rpc));
1181 policy = sptlrpc_rpcflavor2policy(sf->sf_rpc);
1183 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1188 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1190 atomic_inc(&sec->ps_refcount);
1194 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1195 sptlrpc_gc_add_sec(sec);
1197 sptlrpc_policy_put(policy);
1203 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1205 struct ptlrpc_sec *sec;
1207 spin_lock(&imp->imp_lock);
1208 sec = sptlrpc_sec_get(imp->imp_sec);
1209 spin_unlock(&imp->imp_lock);
1213 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1215 static void sptlrpc_import_sec_install(struct obd_import *imp,
1216 struct ptlrpc_sec *sec)
1218 struct ptlrpc_sec *old_sec;
1220 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1222 spin_lock(&imp->imp_lock);
1223 old_sec = imp->imp_sec;
1225 spin_unlock(&imp->imp_lock);
1228 sptlrpc_sec_kill(old_sec);
1230 /* balance the ref taken by this import */
1231 sptlrpc_sec_put(old_sec);
1235 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1236 struct ptlrpc_sec *sec,
1237 struct sptlrpc_flavor *sf)
1239 if (sf->sf_bulk_ciph != sec->ps_flvr.sf_bulk_ciph ||
1240 sf->sf_bulk_hash != sec->ps_flvr.sf_bulk_hash) {
1241 CWARN("imp %p (%s->%s): changing bulk flavor %s/%s -> %s/%s\n",
1242 imp, imp->imp_obd->obd_name,
1243 obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1244 sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1245 sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1246 sptlrpc_get_ciph_name(sf->sf_bulk_ciph),
1247 sptlrpc_get_hash_name(sf->sf_bulk_hash));
1249 spin_lock(&sec->ps_lock);
1250 sec->ps_flvr.sf_bulk_ciph = sf->sf_bulk_ciph;
1251 sec->ps_flvr.sf_bulk_hash = sf->sf_bulk_hash;
1252 spin_unlock(&sec->ps_lock);
1255 if (!equi(sf->sf_flags & PTLRPC_SEC_FL_UDESC,
1256 sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC)) {
1257 CWARN("imp %p (%s->%s): %s shipping user descriptor\n",
1258 imp, imp->imp_obd->obd_name,
1259 obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1260 (sf->sf_flags & PTLRPC_SEC_FL_UDESC) ? "start" : "stop");
1262 spin_lock(&sec->ps_lock);
1263 sec->ps_flvr.sf_flags &= ~PTLRPC_SEC_FL_UDESC;
1264 sec->ps_flvr.sf_flags |= sf->sf_flags & PTLRPC_SEC_FL_UDESC;
1265 spin_unlock(&sec->ps_lock);
1270 * for normal import, @svc_ctx should be NULL and @rpc_flavor is ignored;
1271 * for reverse import, @svc_ctx and @rpc_flavor is from incoming request.
1273 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1274 struct ptlrpc_svc_ctx *svc_ctx,
1277 struct ptlrpc_connection *conn;
1278 struct sptlrpc_flavor sf;
1279 struct ptlrpc_sec *sec, *newsec;
1280 enum lustre_sec_part sp;
1286 conn = imp->imp_connection;
1288 if (svc_ctx == NULL) {
1289 /* normal import, determine flavor from rule set */
1290 sptlrpc_rule_set_choose(&imp->imp_obd->u.cli.cl_sptlrpc_rset,
1291 LUSTRE_SP_ANY, conn->c_self, &sf);
1293 sp = imp->imp_obd->u.cli.cl_sec_part;
1295 /* reverse import, determine flavor from incoming reqeust */
1296 sf.sf_rpc = rpc_flavor;
1297 sf.sf_bulk_ciph = BULK_CIPH_ALG_NULL;
1298 sf.sf_bulk_hash = BULK_HASH_ALG_NULL;
1299 sf.sf_flags = PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1301 sp = sptlrpc_target_sec_part(imp->imp_obd);
1304 sec = sptlrpc_import_sec_ref(imp);
1306 if (svc_ctx == NULL) {
1307 /* normal import, only check rpc flavor, if just bulk
1308 * flavor or flags changed, we can handle it on the fly
1309 * without switching sec. */
1310 if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1311 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1317 /* reverse import, do not compare bulk flavor */
1318 if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1324 CWARN("%simport %p (%s%s%s): changing flavor "
1325 "(%s, %s/%s) -> (%s, %s/%s)\n",
1326 svc_ctx ? "reverse " : "",
1327 imp, imp->imp_obd->obd_name,
1328 svc_ctx == NULL ? "->" : "<-",
1329 obd_uuid2str(&conn->c_remote_uuid),
1330 sptlrpc_rpcflavor2name(sec->ps_flvr.sf_rpc),
1331 sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1332 sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1333 sptlrpc_rpcflavor2name(sf.sf_rpc),
1334 sptlrpc_get_hash_name(sf.sf_bulk_hash),
1335 sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1337 CWARN("%simport %p (%s%s%s) netid %x: "
1338 "select initial flavor (%s, %s/%s)\n",
1339 svc_ctx == NULL ? "" : "reverse ",
1340 imp, imp->imp_obd->obd_name,
1341 svc_ctx == NULL ? "->" : "<-",
1342 obd_uuid2str(&conn->c_remote_uuid),
1343 LNET_NIDNET(conn->c_self),
1344 sptlrpc_rpcflavor2name(sf.sf_rpc),
1345 sptlrpc_get_hash_name(sf.sf_bulk_hash),
1346 sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1349 mutex_down(&imp->imp_sec_mutex);
1351 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1353 sptlrpc_import_sec_install(imp, newsec);
1356 CERROR("%simport %p (%s): failed to create new sec\n",
1357 svc_ctx == NULL ? "" : "reverse ",
1358 imp, obd_uuid2str(&conn->c_remote_uuid));
1362 mutex_up(&imp->imp_sec_mutex);
1365 sptlrpc_sec_put(sec);
1369 void sptlrpc_import_sec_put(struct obd_import *imp)
1372 sptlrpc_sec_kill(imp->imp_sec);
1374 sptlrpc_sec_put(imp->imp_sec);
1375 imp->imp_sec = NULL;
1379 static void import_flush_ctx_common(struct obd_import *imp,
1380 uid_t uid, int grace, int force)
1382 struct ptlrpc_sec *sec;
1387 sec = sptlrpc_import_sec_ref(imp);
1391 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1392 sptlrpc_sec_put(sec);
1395 void sptlrpc_import_inval_all_ctx(struct obd_import *imp)
1397 /* use grace == 0 */
1398 import_flush_ctx_common(imp, -1, 0, 1);
1401 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1403 /* it's important to use grace mode, see explain in
1404 * sptlrpc_req_refresh_ctx() */
1405 import_flush_ctx_common(imp, 0, 1, 1);
1408 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1410 import_flush_ctx_common(imp, cfs_current()->uid, 1, 1);
1412 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1414 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1416 import_flush_ctx_common(imp, -1, 1, 1);
1418 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1421 * when complete successfully, req->rq_reqmsg should point to the
1424 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1426 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1427 struct ptlrpc_sec_policy *policy;
1431 LASSERT(atomic_read(&ctx->cc_refcount));
1432 LASSERT(ctx->cc_sec);
1433 LASSERT(ctx->cc_sec->ps_policy);
1434 LASSERT(req->rq_reqmsg == NULL);
1436 policy = ctx->cc_sec->ps_policy;
1437 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1439 LASSERT(req->rq_reqmsg);
1440 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1442 /* zeroing preallocated buffer */
1444 memset(req->rq_reqmsg, 0, msgsize);
1450 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1452 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1453 struct ptlrpc_sec_policy *policy;
1456 LASSERT(atomic_read(&ctx->cc_refcount));
1457 LASSERT(ctx->cc_sec);
1458 LASSERT(ctx->cc_sec->ps_policy);
1460 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1463 policy = ctx->cc_sec->ps_policy;
1464 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1468 * NOTE caller must guarantee the buffer size is enough for the enlargement
1470 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1471 int segment, int newsize)
1474 int oldsize, oldmsg_size, movesize;
1476 LASSERT(segment < msg->lm_bufcount);
1477 LASSERT(msg->lm_buflens[segment] <= newsize);
1479 if (msg->lm_buflens[segment] == newsize)
1482 /* nothing to do if we are enlarging the last segment */
1483 if (segment == msg->lm_bufcount - 1) {
1484 msg->lm_buflens[segment] = newsize;
1488 oldsize = msg->lm_buflens[segment];
1490 src = lustre_msg_buf(msg, segment + 1, 0);
1491 msg->lm_buflens[segment] = newsize;
1492 dst = lustre_msg_buf(msg, segment + 1, 0);
1493 msg->lm_buflens[segment] = oldsize;
1495 /* move from segment + 1 to end segment */
1496 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1497 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1498 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1499 LASSERT(movesize >= 0);
1502 memmove(dst, src, movesize);
1504 /* note we don't clear the ares where old data live, not secret */
1506 /* finally set new segment size */
1507 msg->lm_buflens[segment] = newsize;
1509 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1512 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1513 * will be preserved after enlargement. this must be called after rq_reqmsg has
1514 * been intialized at least.
1516 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1519 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1520 int segment, int newsize)
1522 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1523 struct ptlrpc_sec_cops *cops;
1524 struct lustre_msg *msg = req->rq_reqmsg;
1528 LASSERT(msg->lm_bufcount > segment);
1529 LASSERT(msg->lm_buflens[segment] <= newsize);
1531 if (msg->lm_buflens[segment] == newsize)
1534 cops = ctx->cc_sec->ps_policy->sp_cops;
1535 LASSERT(cops->enlarge_reqbuf);
1536 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1538 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1540 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1542 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1543 struct ptlrpc_sec_policy *policy;
1547 LASSERT(atomic_read(&ctx->cc_refcount));
1548 LASSERT(ctx->cc_sec);
1549 LASSERT(ctx->cc_sec->ps_policy);
1554 policy = ctx->cc_sec->ps_policy;
1555 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1558 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1560 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1561 struct ptlrpc_sec_policy *policy;
1565 LASSERT(atomic_read(&ctx->cc_refcount));
1566 LASSERT(ctx->cc_sec);
1567 LASSERT(ctx->cc_sec->ps_policy);
1569 if (req->rq_repbuf == NULL)
1571 LASSERT(req->rq_repbuf_len);
1573 policy = ctx->cc_sec->ps_policy;
1574 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1578 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1579 struct ptlrpc_cli_ctx *ctx)
1581 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1583 if (!policy->sp_cops->install_rctx)
1585 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1588 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1589 struct ptlrpc_svc_ctx *ctx)
1591 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1593 if (!policy->sp_sops->install_rctx)
1595 return policy->sp_sops->install_rctx(imp, ctx);
1598 /****************************************
1599 * server side security *
1600 ****************************************/
1602 static int flavor_allowed(struct sptlrpc_flavor *exp,
1603 struct ptlrpc_request *req)
1605 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1607 if (exp->sf_rpc == flvr->sf_rpc)
1610 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1611 RPC_FLVR_POLICY(exp->sf_rpc) == RPC_FLVR_POLICY(flvr->sf_rpc) &&
1612 RPC_FLVR_MECH(exp->sf_rpc) == RPC_FLVR_MECH(flvr->sf_rpc))
1618 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1620 int sptlrpc_target_export_check(struct obd_export *exp,
1621 struct ptlrpc_request *req)
1623 struct sptlrpc_flavor flavor;
1628 /* client side export has no imp_reverse, skip
1629 * FIXME maybe we should check flavor this as well??? */
1630 if (exp->exp_imp_reverse == NULL)
1633 /* don't care about ctx fini rpc */
1634 if (req->rq_ctx_fini)
1637 spin_lock(&exp->exp_lock);
1639 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1640 * the first req with the new flavor, then treat it as current flavor,
1641 * adapt reverse sec according to it.
1642 * note the first rpc with new flavor might not be with root ctx, in
1643 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1644 if (unlikely(exp->exp_flvr_changed) &&
1645 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1646 /* make the new flavor as "current", and old ones as
1647 * about-to-expire */
1648 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1649 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1650 flavor = exp->exp_flvr_old[1];
1651 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1652 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1653 exp->exp_flvr_old[0] = exp->exp_flvr;
1654 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1655 EXP_FLVR_UPDATE_EXPIRE;
1656 exp->exp_flvr = flavor;
1658 /* flavor change finished */
1659 exp->exp_flvr_changed = 0;
1660 LASSERT(exp->exp_flvr_adapt == 1);
1662 /* if it's gss, we only interested in root ctx init */
1663 if (req->rq_auth_gss &&
1664 !(req->rq_ctx_init && (req->rq_auth_usr_root ||
1665 req->rq_auth_usr_mdt))) {
1666 spin_unlock(&exp->exp_lock);
1667 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d)\n",
1668 req->rq_auth_gss, req->rq_ctx_init,
1669 req->rq_auth_usr_root, req->rq_auth_usr_mdt);
1673 exp->exp_flvr_adapt = 0;
1674 spin_unlock(&exp->exp_lock);
1676 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1677 req->rq_svc_ctx, flavor.sf_rpc);
1680 /* if it equals to the current flavor, we accept it, but need to
1681 * dealing with reverse sec/ctx */
1682 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1683 /* most cases should return here, we only interested in
1684 * gss root ctx init */
1685 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1686 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt)) {
1687 spin_unlock(&exp->exp_lock);
1691 /* if flavor just changed, we should not proceed, just leave
1692 * it and current flavor will be discovered and replaced
1693 * shortly, and let _this_ rpc pass through */
1694 if (exp->exp_flvr_changed) {
1695 LASSERT(exp->exp_flvr_adapt);
1696 spin_unlock(&exp->exp_lock);
1700 if (exp->exp_flvr_adapt) {
1701 exp->exp_flvr_adapt = 0;
1702 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1703 exp, exp->exp_flvr.sf_rpc,
1704 exp->exp_flvr_old[0].sf_rpc,
1705 exp->exp_flvr_old[1].sf_rpc);
1706 flavor = exp->exp_flvr;
1707 spin_unlock(&exp->exp_lock);
1709 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1713 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1714 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1715 exp->exp_flvr_old[0].sf_rpc,
1716 exp->exp_flvr_old[1].sf_rpc);
1717 spin_unlock(&exp->exp_lock);
1719 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1724 if (exp->exp_flvr_expire[0]) {
1725 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1726 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1727 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1728 "middle one ("CFS_DURATION_T")\n", exp,
1729 exp->exp_flvr.sf_rpc,
1730 exp->exp_flvr_old[0].sf_rpc,
1731 exp->exp_flvr_old[1].sf_rpc,
1732 exp->exp_flvr_expire[0] -
1733 cfs_time_current_sec());
1734 spin_unlock(&exp->exp_lock);
1738 CDEBUG(D_SEC, "mark middle expired\n");
1739 exp->exp_flvr_expire[0] = 0;
1741 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1742 exp->exp_flvr.sf_rpc,
1743 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1744 req->rq_flvr.sf_rpc);
1747 /* now it doesn't match the current flavor, the only chance we can
1748 * accept it is match the old flavors which is not expired. */
1749 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1750 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1751 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1752 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1753 "oldest one ("CFS_DURATION_T")\n", exp,
1754 exp->exp_flvr.sf_rpc,
1755 exp->exp_flvr_old[0].sf_rpc,
1756 exp->exp_flvr_old[1].sf_rpc,
1757 exp->exp_flvr_expire[1] -
1758 cfs_time_current_sec());
1759 spin_unlock(&exp->exp_lock);
1763 CDEBUG(D_SEC, "mark oldest expired\n");
1764 exp->exp_flvr_expire[1] = 0;
1766 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1767 exp, exp->exp_flvr.sf_rpc,
1768 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1769 req->rq_flvr.sf_rpc);
1771 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1772 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1773 exp->exp_flvr_old[1].sf_rpc);
1776 spin_unlock(&exp->exp_lock);
1778 CWARN("req %p: (%u|%u|%u|%u|%u) with unauthorized flavor %x\n",
1779 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1780 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_flvr.sf_rpc);
1784 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1785 struct sptlrpc_rule_set *rset)
1787 struct obd_export *exp;
1788 struct sptlrpc_flavor new_flvr;
1792 spin_lock(&obd->obd_dev_lock);
1794 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1795 if (exp->exp_connection == NULL)
1798 /* note if this export had just been updated flavor
1799 * (exp_flvr_changed == 1), this will override the
1801 spin_lock(&exp->exp_lock);
1802 sptlrpc_rule_set_choose(rset, exp->exp_sp_peer,
1803 exp->exp_connection->c_peer.nid,
1805 if (exp->exp_flvr_changed ||
1806 memcmp(&new_flvr, &exp->exp_flvr, sizeof(new_flvr))) {
1807 exp->exp_flvr_old[1] = new_flvr;
1808 exp->exp_flvr_expire[1] = 0;
1809 exp->exp_flvr_changed = 1;
1810 exp->exp_flvr_adapt = 1;
1811 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1812 exp, sptlrpc_part2name(exp->exp_sp_peer),
1813 exp->exp_flvr.sf_rpc,
1814 exp->exp_flvr_old[1].sf_rpc);
1816 spin_unlock(&exp->exp_lock);
1819 spin_unlock(&obd->obd_dev_lock);
1821 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1823 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1825 if (svc_rc == SECSVC_DROP)
1828 switch (req->rq_sp_from) {
1836 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1840 if (!req->rq_auth_gss)
1843 if (unlikely(req->rq_sp_from == LUSTRE_SP_ANY)) {
1844 CERROR("not specific part\n");
1848 /* from MDT, must be authenticated as MDT */
1849 if (unlikely(req->rq_sp_from == LUSTRE_SP_MDT &&
1850 !req->rq_auth_usr_mdt)) {
1851 DEBUG_REQ(D_ERROR, req, "fake source MDT");
1855 /* from OST, must be callback to MDT and CLI, the reverse sec
1856 * was from mdt/root keytab, so it should be MDT or root FIXME */
1857 if (unlikely(req->rq_sp_from == LUSTRE_SP_OST &&
1858 !req->rq_auth_usr_mdt && !req->rq_auth_usr_root)) {
1859 DEBUG_REQ(D_ERROR, req, "fake source OST");
1866 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1868 struct ptlrpc_sec_policy *policy;
1869 struct lustre_msg *msg = req->rq_reqbuf;
1874 LASSERT(req->rq_reqmsg == NULL);
1875 LASSERT(req->rq_repmsg == NULL);
1877 req->rq_sp_from = LUSTRE_SP_ANY;
1878 req->rq_auth_uid = INVALID_UID;
1879 req->rq_auth_mapped_uid = INVALID_UID;
1881 if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1882 CERROR("request size %d too small\n", req->rq_reqdata_len);
1883 RETURN(SECSVC_DROP);
1889 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1890 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(msg->lm_secflvr);
1892 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(__swab32(msg->lm_secflvr));
1894 /* unpack the wrapper message if the policy is not null */
1895 if ((RPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) &&
1896 lustre_unpack_msg(msg, req->rq_reqdata_len))
1897 RETURN(SECSVC_DROP);
1899 policy = sptlrpc_rpcflavor2policy(req->rq_flvr.sf_rpc);
1901 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1902 RETURN(SECSVC_DROP);
1905 LASSERT(policy->sp_sops->accept);
1906 rc = policy->sp_sops->accept(req);
1908 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1909 sptlrpc_policy_put(policy);
1911 /* sanity check for the request source */
1912 rc = sptlrpc_svc_check_from(req, rc);
1914 /* FIXME move to proper place */
1915 if (rc == SECSVC_OK) {
1916 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1918 if (opc == OST_WRITE)
1919 req->rq_bulk_write = 1;
1920 else if (opc == OST_READ)
1921 req->rq_bulk_read = 1;
1924 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1928 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1931 struct ptlrpc_sec_policy *policy;
1932 struct ptlrpc_reply_state *rs;
1936 LASSERT(req->rq_svc_ctx);
1937 LASSERT(req->rq_svc_ctx->sc_policy);
1939 policy = req->rq_svc_ctx->sc_policy;
1940 LASSERT(policy->sp_sops->alloc_rs);
1942 rc = policy->sp_sops->alloc_rs(req, msglen);
1943 if (unlikely(rc == -ENOMEM)) {
1944 /* failed alloc, try emergency pool */
1945 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
1949 req->rq_reply_state = rs;
1950 rc = policy->sp_sops->alloc_rs(req, msglen);
1952 lustre_put_emerg_rs(rs);
1953 req->rq_reply_state = NULL;
1958 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1963 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1965 struct ptlrpc_sec_policy *policy;
1969 LASSERT(req->rq_svc_ctx);
1970 LASSERT(req->rq_svc_ctx->sc_policy);
1972 policy = req->rq_svc_ctx->sc_policy;
1973 LASSERT(policy->sp_sops->authorize);
1975 rc = policy->sp_sops->authorize(req);
1976 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1981 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1983 struct ptlrpc_sec_policy *policy;
1984 unsigned int prealloc;
1987 LASSERT(rs->rs_svc_ctx);
1988 LASSERT(rs->rs_svc_ctx->sc_policy);
1990 policy = rs->rs_svc_ctx->sc_policy;
1991 LASSERT(policy->sp_sops->free_rs);
1993 prealloc = rs->rs_prealloc;
1994 policy->sp_sops->free_rs(rs);
1997 lustre_put_emerg_rs(rs);
2001 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2003 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2008 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2009 atomic_inc(&ctx->sc_refcount);
2012 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2014 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2019 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2020 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2021 if (ctx->sc_policy->sp_sops->free_ctx)
2022 ctx->sc_policy->sp_sops->free_ctx(ctx);
2024 req->rq_svc_ctx = NULL;
2027 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2029 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2034 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2035 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2036 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2038 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2040 /****************************************
2042 ****************************************/
2044 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2045 struct ptlrpc_bulk_desc *desc)
2047 struct ptlrpc_cli_ctx *ctx;
2049 if (!req->rq_pack_bulk)
2052 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2054 ctx = req->rq_cli_ctx;
2055 if (ctx->cc_ops->wrap_bulk)
2056 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2059 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2062 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
2063 struct ptlrpc_bulk_desc *desc)
2070 for (i = 0; i < pg_count && nob > 0; i++) {
2072 desc->bd_iov[i].kiov_page = pga[i]->pg;
2073 desc->bd_iov[i].kiov_len = pga[i]->count > nob ?
2074 nob : pga[i]->count;
2075 desc->bd_iov[i].kiov_offset = pga[i]->off & ~CFS_PAGE_MASK;
2077 /* FIXME currently liblustre doesn't support bulk encryption.
2078 * if we do, check again following may not be right. */
2079 LASSERTF(0, "Bulk encryption not implemented for liblustre\n");
2080 desc->bd_iov[i].iov_base = pga[i]->pg->addr;
2081 desc->bd_iov[i].iov_len = pga[i]->count > nob ?
2082 nob : pga[i]->count;
2085 desc->bd_iov_count++;
2086 nob -= pga[i]->count;
2090 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2091 int nob, obd_count pg_count,
2092 struct brw_page **pga)
2094 struct ptlrpc_bulk_desc *desc;
2095 struct ptlrpc_cli_ctx *ctx;
2098 if (!req->rq_pack_bulk)
2101 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2103 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2105 CERROR("out of memory, can't verify bulk read data\n");
2109 pga_to_bulk_desc(nob, pg_count, pga, desc);
2111 ctx = req->rq_cli_ctx;
2112 if (ctx->cc_ops->unwrap_bulk)
2113 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2115 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2119 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2121 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2122 struct ptlrpc_bulk_desc *desc)
2124 struct ptlrpc_cli_ctx *ctx;
2126 if (!req->rq_pack_bulk)
2129 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2131 ctx = req->rq_cli_ctx;
2132 if (ctx->cc_ops->unwrap_bulk)
2133 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2137 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2139 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2140 struct ptlrpc_bulk_desc *desc)
2142 struct ptlrpc_svc_ctx *ctx;
2144 if (!req->rq_pack_bulk)
2147 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2149 ctx = req->rq_svc_ctx;
2150 if (ctx->sc_policy->sp_sops->wrap_bulk)
2151 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2155 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2157 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2158 struct ptlrpc_bulk_desc *desc)
2160 struct ptlrpc_svc_ctx *ctx;
2162 if (!req->rq_pack_bulk)
2165 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2167 ctx = req->rq_svc_ctx;
2168 if (ctx->sc_policy->sp_sops->unwrap_bulk);
2169 return ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2173 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2176 /****************************************
2177 * user descriptor helpers *
2178 ****************************************/
2180 int sptlrpc_current_user_desc_size(void)
2185 ngroups = current_ngroups;
2187 if (ngroups > LUSTRE_MAX_GROUPS)
2188 ngroups = LUSTRE_MAX_GROUPS;
2192 return sptlrpc_user_desc_size(ngroups);
2194 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2196 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2198 struct ptlrpc_user_desc *pud;
2200 pud = lustre_msg_buf(msg, offset, 0);
2202 pud->pud_uid = cfs_current()->uid;
2203 pud->pud_gid = cfs_current()->gid;
2204 pud->pud_fsuid = cfs_current()->fsuid;
2205 pud->pud_fsgid = cfs_current()->fsgid;
2206 pud->pud_cap = cfs_current()->cap_effective;
2207 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2211 if (pud->pud_ngroups > current_ngroups)
2212 pud->pud_ngroups = current_ngroups;
2213 memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
2214 pud->pud_ngroups * sizeof(__u32));
2215 task_unlock(current);
2220 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2222 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
2224 struct ptlrpc_user_desc *pud;
2227 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2231 if (lustre_msg_swabbed(msg)) {
2232 __swab32s(&pud->pud_uid);
2233 __swab32s(&pud->pud_gid);
2234 __swab32s(&pud->pud_fsuid);
2235 __swab32s(&pud->pud_fsgid);
2236 __swab32s(&pud->pud_cap);
2237 __swab32s(&pud->pud_ngroups);
2240 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2241 CERROR("%u groups is too large\n", pud->pud_ngroups);
2245 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2246 msg->lm_buflens[offset]) {
2247 CERROR("%u groups are claimed but bufsize only %u\n",
2248 pud->pud_ngroups, msg->lm_buflens[offset]);
2252 if (lustre_msg_swabbed(msg)) {
2253 for (i = 0; i < pud->pud_ngroups; i++)
2254 __swab32s(&pud->pud_groups[i]);
2259 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2261 /****************************************
2263 ****************************************/
2265 const char * sec2target_str(struct ptlrpc_sec *sec)
2267 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2269 if (sec_is_reverse(sec))
2271 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2273 EXPORT_SYMBOL(sec2target_str);
2275 /****************************************
2276 * crypto API helper/alloc blkciper *
2277 ****************************************/
2280 #ifndef HAVE_ASYNC_BLOCK_CIPHER
2281 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char * algname,
2284 char buf[CRYPTO_MAX_ALG_NAME + 1];
2285 const char *pan = algname;
2288 if (strncmp("cbc(", algname, 4) == 0)
2289 flag |= CRYPTO_TFM_MODE_CBC;
2290 else if (strncmp("ecb(", algname, 4) == 0)
2291 flag |= CRYPTO_TFM_MODE_ECB;
2293 char *vp = strnchr(algname, CRYPTO_MAX_ALG_NAME, ')');
2295 memcpy(buf, algname + 4, vp - algname - 4);
2296 buf[vp - algname - 4] = '\0';
2302 return crypto_alloc_tfm(pan, flag);
2304 EXPORT_SYMBOL(ll_crypto_alloc_blkcipher);
2308 /****************************************
2309 * initialize/finalize *
2310 ****************************************/
2312 int __init sptlrpc_init(void)
2316 rc = sptlrpc_gc_start_thread();
2320 rc = sptlrpc_enc_pool_init();
2324 rc = sptlrpc_null_init();
2328 rc = sptlrpc_plain_init();
2332 rc = sptlrpc_lproc_init();
2339 sptlrpc_plain_fini();
2341 sptlrpc_null_fini();
2343 sptlrpc_enc_pool_fini();
2345 sptlrpc_gc_stop_thread();
2350 void __exit sptlrpc_fini(void)
2352 sptlrpc_lproc_fini();
2353 sptlrpc_plain_fini();
2354 sptlrpc_null_fini();
2355 sptlrpc_enc_pool_fini();
2356 sptlrpc_gc_stop_thread();