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;
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_client_wake_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, 1);
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 ptlrpc_client_wake_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 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
938 CERROR("real reply with offset 0\n");
942 if (req->rq_reply_off % 8 != 0) {
943 CERROR("reply at odd offset %u\n", req->rq_reply_off);
947 req->rq_repdata = (struct lustre_msg *)
948 (req->rq_repbuf + req->rq_reply_off);
949 req->rq_repdata_len = req->rq_nob_received;
951 return do_cli_unwrap_reply(req);
955 * Upon called, the receive buffer might be still posted, so the reply data
956 * might be changed at any time, no matter we're holding rq_lock or not. we
957 * expect the rq_reply_off be 0, rq_nob_received is the early reply size.
959 * we allocate separate ptlrpc_request and reply buffer for early reply
960 * processing, return 0 and @req_ret is a duplicated ptlrpc_request. caller
961 * must call sptlrpc_cli_finish_early_reply() on the returned request to
962 * release it. if anything goes wrong @req_ret will not be set.
964 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
965 struct ptlrpc_request **req_ret)
967 struct ptlrpc_request *early_req;
969 int early_bufsz, early_size;
973 OBD_ALLOC_PTR(early_req);
974 if (early_req == NULL)
977 early_size = req->rq_nob_received;
978 early_bufsz = size_roundup_power2(early_size);
979 OBD_ALLOC(early_buf, early_bufsz);
980 if (early_buf == NULL)
981 GOTO(err_req, rc = -ENOMEM);
983 /* sanity checkings and copy data out, do it inside spinlock */
984 spin_lock(&req->rq_lock);
986 if (req->rq_replied) {
987 spin_unlock(&req->rq_lock);
988 GOTO(err_buf, rc = -EALREADY);
991 LASSERT(req->rq_repbuf);
992 LASSERT(req->rq_repdata == NULL);
993 LASSERT(req->rq_repmsg == NULL);
995 if (req->rq_reply_off != 0) {
996 CERROR("early reply with offset %u\n", req->rq_reply_off);
997 spin_unlock(&req->rq_lock);
998 GOTO(err_buf, rc = -EPROTO);
1001 if (req->rq_nob_received != early_size) {
1002 /* even another early arrived the size should be the same */
1003 CERROR("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_buf, 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_buf, rc = -EALREADY);
1016 memcpy(early_buf, req->rq_repbuf, early_size);
1017 spin_unlock(&req->rq_lock);
1019 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1020 early_req->rq_flvr = req->rq_flvr;
1021 early_req->rq_repbuf = early_buf;
1022 early_req->rq_repbuf_len = early_bufsz;
1023 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1024 early_req->rq_repdata_len = early_size;
1025 early_req->rq_early = 1;
1027 rc = do_cli_unwrap_reply(early_req);
1029 DEBUG_REQ(D_ADAPTTO, early_req,
1030 "error %d unwrap early reply", rc);
1034 LASSERT(early_req->rq_repmsg);
1035 *req_ret = early_req;
1039 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1041 OBD_FREE(early_buf, early_bufsz);
1043 OBD_FREE_PTR(early_req);
1047 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1049 LASSERT(early_req->rq_repbuf);
1050 LASSERT(early_req->rq_repdata);
1051 LASSERT(early_req->rq_repmsg);
1053 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1054 OBD_FREE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1055 OBD_FREE_PTR(early_req);
1058 /**************************************************
1060 **************************************************/
1063 * "fixed" sec (e.g. null) use sec_id < 0
1065 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1067 int sptlrpc_get_next_secid(void)
1069 return atomic_inc_return(&sptlrpc_sec_id);
1071 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1073 /**************************************************
1074 * client side high-level security APIs *
1075 **************************************************/
1077 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1078 int grace, int force)
1080 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1082 LASSERT(policy->sp_cops);
1083 LASSERT(policy->sp_cops->flush_ctx_cache);
1085 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1088 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1090 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1092 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1093 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1094 LASSERT(policy->sp_cops->destroy_sec);
1096 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1098 policy->sp_cops->destroy_sec(sec);
1099 sptlrpc_policy_put(policy);
1102 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1104 sec_cop_destroy_sec(sec);
1106 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1108 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1110 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1112 if (sec->ps_policy->sp_cops->kill_sec) {
1113 sec->ps_policy->sp_cops->kill_sec(sec);
1115 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1119 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1122 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1123 atomic_inc(&sec->ps_refcount);
1128 EXPORT_SYMBOL(sptlrpc_sec_get);
1130 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1133 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1135 if (atomic_dec_and_test(&sec->ps_refcount)) {
1136 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1138 sptlrpc_gc_del_sec(sec);
1139 sec_cop_destroy_sec(sec);
1143 EXPORT_SYMBOL(sptlrpc_sec_put);
1146 * it's policy module responsible for taking refrence of import
1149 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1150 struct ptlrpc_svc_ctx *svc_ctx,
1151 struct sptlrpc_flavor *sf,
1152 enum lustre_sec_part sp)
1154 struct ptlrpc_sec_policy *policy;
1155 struct ptlrpc_sec *sec;
1159 LASSERT(imp->imp_dlm_fake == 1);
1161 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1162 imp->imp_obd->obd_type->typ_name,
1163 imp->imp_obd->obd_name,
1164 sptlrpc_rpcflavor2name(sf->sf_rpc));
1166 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1167 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1169 LASSERT(imp->imp_dlm_fake == 0);
1171 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1172 imp->imp_obd->obd_type->typ_name,
1173 imp->imp_obd->obd_name,
1174 sptlrpc_rpcflavor2name(sf->sf_rpc));
1176 policy = sptlrpc_rpcflavor2policy(sf->sf_rpc);
1178 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1183 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1185 atomic_inc(&sec->ps_refcount);
1189 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1190 sptlrpc_gc_add_sec(sec);
1192 sptlrpc_policy_put(policy);
1198 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1200 struct ptlrpc_sec *sec;
1202 spin_lock(&imp->imp_lock);
1203 sec = sptlrpc_sec_get(imp->imp_sec);
1204 spin_unlock(&imp->imp_lock);
1208 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1210 static void sptlrpc_import_sec_install(struct obd_import *imp,
1211 struct ptlrpc_sec *sec)
1213 struct ptlrpc_sec *old_sec;
1215 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1217 spin_lock(&imp->imp_lock);
1218 old_sec = imp->imp_sec;
1220 spin_unlock(&imp->imp_lock);
1223 sptlrpc_sec_kill(old_sec);
1225 /* balance the ref taken by this import */
1226 sptlrpc_sec_put(old_sec);
1230 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1231 struct ptlrpc_sec *sec,
1232 struct sptlrpc_flavor *sf)
1234 if (sf->sf_bulk_ciph != sec->ps_flvr.sf_bulk_ciph ||
1235 sf->sf_bulk_hash != sec->ps_flvr.sf_bulk_hash) {
1236 CWARN("imp %p (%s->%s): changing bulk flavor %s/%s -> %s/%s\n",
1237 imp, imp->imp_obd->obd_name,
1238 obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1239 sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1240 sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1241 sptlrpc_get_ciph_name(sf->sf_bulk_ciph),
1242 sptlrpc_get_hash_name(sf->sf_bulk_hash));
1244 spin_lock(&sec->ps_lock);
1245 sec->ps_flvr.sf_bulk_ciph = sf->sf_bulk_ciph;
1246 sec->ps_flvr.sf_bulk_hash = sf->sf_bulk_hash;
1247 spin_unlock(&sec->ps_lock);
1250 if (!equi(sf->sf_flags & PTLRPC_SEC_FL_UDESC,
1251 sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC)) {
1252 CWARN("imp %p (%s->%s): %s shipping user descriptor\n",
1253 imp, imp->imp_obd->obd_name,
1254 obd_uuid2str(&imp->imp_connection->c_remote_uuid),
1255 (sf->sf_flags & PTLRPC_SEC_FL_UDESC) ? "start" : "stop");
1257 spin_lock(&sec->ps_lock);
1258 sec->ps_flvr.sf_flags &= ~PTLRPC_SEC_FL_UDESC;
1259 sec->ps_flvr.sf_flags |= sf->sf_flags & PTLRPC_SEC_FL_UDESC;
1260 spin_unlock(&sec->ps_lock);
1265 * for normal import, @svc_ctx should be NULL and @rpc_flavor is ignored;
1266 * for reverse import, @svc_ctx and @rpc_flavor is from incoming request.
1268 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1269 struct ptlrpc_svc_ctx *svc_ctx,
1272 struct ptlrpc_connection *conn;
1273 struct sptlrpc_flavor sf;
1274 struct ptlrpc_sec *sec, *newsec;
1275 enum lustre_sec_part sp;
1281 conn = imp->imp_connection;
1283 if (svc_ctx == NULL) {
1284 /* normal import, determine flavor from rule set */
1285 sptlrpc_rule_set_choose(&imp->imp_obd->u.cli.cl_sptlrpc_rset,
1286 LUSTRE_SP_ANY, conn->c_self, &sf);
1288 sp = imp->imp_obd->u.cli.cl_sec_part;
1290 /* reverse import, determine flavor from incoming reqeust */
1291 sf.sf_rpc = rpc_flavor;
1292 sf.sf_bulk_ciph = BULK_CIPH_ALG_NULL;
1293 sf.sf_bulk_hash = BULK_HASH_ALG_NULL;
1294 sf.sf_flags = PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1296 sp = sptlrpc_target_sec_part(imp->imp_obd);
1299 sec = sptlrpc_import_sec_ref(imp);
1301 if (svc_ctx == NULL) {
1302 /* normal import, only check rpc flavor, if just bulk
1303 * flavor or flags changed, we can handle it on the fly
1304 * without switching sec. */
1305 if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1306 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1312 /* reverse import, do not compare bulk flavor */
1313 if (sf.sf_rpc == sec->ps_flvr.sf_rpc) {
1319 CWARN("%simport %p (%s%s%s): changing flavor "
1320 "(%s, %s/%s) -> (%s, %s/%s)\n",
1321 svc_ctx ? "reverse " : "",
1322 imp, imp->imp_obd->obd_name,
1323 svc_ctx == NULL ? "->" : "<-",
1324 obd_uuid2str(&conn->c_remote_uuid),
1325 sptlrpc_rpcflavor2name(sec->ps_flvr.sf_rpc),
1326 sptlrpc_get_hash_name(sec->ps_flvr.sf_bulk_hash),
1327 sptlrpc_get_ciph_name(sec->ps_flvr.sf_bulk_ciph),
1328 sptlrpc_rpcflavor2name(sf.sf_rpc),
1329 sptlrpc_get_hash_name(sf.sf_bulk_hash),
1330 sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1332 CWARN("%simport %p (%s%s%s) netid %x: "
1333 "select initial flavor (%s, %s/%s)\n",
1334 svc_ctx == NULL ? "" : "reverse ",
1335 imp, imp->imp_obd->obd_name,
1336 svc_ctx == NULL ? "->" : "<-",
1337 obd_uuid2str(&conn->c_remote_uuid),
1338 LNET_NIDNET(conn->c_self),
1339 sptlrpc_rpcflavor2name(sf.sf_rpc),
1340 sptlrpc_get_hash_name(sf.sf_bulk_hash),
1341 sptlrpc_get_ciph_name(sf.sf_bulk_ciph));
1344 mutex_down(&imp->imp_sec_mutex);
1346 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1348 sptlrpc_import_sec_install(imp, newsec);
1351 CERROR("%simport %p (%s): failed to create new sec\n",
1352 svc_ctx == NULL ? "" : "reverse ",
1353 imp, obd_uuid2str(&conn->c_remote_uuid));
1357 mutex_up(&imp->imp_sec_mutex);
1360 sptlrpc_sec_put(sec);
1364 void sptlrpc_import_sec_put(struct obd_import *imp)
1367 sptlrpc_sec_kill(imp->imp_sec);
1369 sptlrpc_sec_put(imp->imp_sec);
1370 imp->imp_sec = NULL;
1374 static void import_flush_ctx_common(struct obd_import *imp,
1375 uid_t uid, int grace, int force)
1377 struct ptlrpc_sec *sec;
1382 sec = sptlrpc_import_sec_ref(imp);
1386 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1387 sptlrpc_sec_put(sec);
1390 void sptlrpc_import_inval_all_ctx(struct obd_import *imp)
1392 /* use grace == 0 */
1393 import_flush_ctx_common(imp, -1, 0, 1);
1396 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1398 /* it's important to use grace mode, see explain in
1399 * sptlrpc_req_refresh_ctx() */
1400 import_flush_ctx_common(imp, 0, 1, 1);
1403 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1405 import_flush_ctx_common(imp, cfs_current()->uid, 1, 1);
1407 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1409 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1411 import_flush_ctx_common(imp, -1, 1, 1);
1413 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1416 * when complete successfully, req->rq_reqmsg should point to the
1419 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1421 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1422 struct ptlrpc_sec_policy *policy;
1426 LASSERT(atomic_read(&ctx->cc_refcount));
1427 LASSERT(ctx->cc_sec);
1428 LASSERT(ctx->cc_sec->ps_policy);
1429 LASSERT(req->rq_reqmsg == NULL);
1431 policy = ctx->cc_sec->ps_policy;
1432 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1434 LASSERT(req->rq_reqmsg);
1435 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1437 /* zeroing preallocated buffer */
1439 memset(req->rq_reqmsg, 0, msgsize);
1445 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1447 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1448 struct ptlrpc_sec_policy *policy;
1451 LASSERT(atomic_read(&ctx->cc_refcount));
1452 LASSERT(ctx->cc_sec);
1453 LASSERT(ctx->cc_sec->ps_policy);
1455 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1458 policy = ctx->cc_sec->ps_policy;
1459 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1463 * NOTE caller must guarantee the buffer size is enough for the enlargement
1465 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1466 int segment, int newsize)
1469 int oldsize, oldmsg_size, movesize;
1471 LASSERT(segment < msg->lm_bufcount);
1472 LASSERT(msg->lm_buflens[segment] <= newsize);
1474 if (msg->lm_buflens[segment] == newsize)
1477 /* nothing to do if we are enlarging the last segment */
1478 if (segment == msg->lm_bufcount - 1) {
1479 msg->lm_buflens[segment] = newsize;
1483 oldsize = msg->lm_buflens[segment];
1485 src = lustre_msg_buf(msg, segment + 1, 0);
1486 msg->lm_buflens[segment] = newsize;
1487 dst = lustre_msg_buf(msg, segment + 1, 0);
1488 msg->lm_buflens[segment] = oldsize;
1490 /* move from segment + 1 to end segment */
1491 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1492 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1493 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1494 LASSERT(movesize >= 0);
1497 memmove(dst, src, movesize);
1499 /* note we don't clear the ares where old data live, not secret */
1501 /* finally set new segment size */
1502 msg->lm_buflens[segment] = newsize;
1504 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1507 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1508 * will be preserved after enlargement. this must be called after rq_reqmsg has
1509 * been intialized at least.
1511 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1514 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1515 int segment, int newsize)
1517 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1518 struct ptlrpc_sec_cops *cops;
1519 struct lustre_msg *msg = req->rq_reqmsg;
1523 LASSERT(msg->lm_bufcount > segment);
1524 LASSERT(msg->lm_buflens[segment] <= newsize);
1526 if (msg->lm_buflens[segment] == newsize)
1529 cops = ctx->cc_sec->ps_policy->sp_cops;
1530 LASSERT(cops->enlarge_reqbuf);
1531 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1533 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1535 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1537 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1538 struct ptlrpc_sec_policy *policy;
1542 LASSERT(atomic_read(&ctx->cc_refcount));
1543 LASSERT(ctx->cc_sec);
1544 LASSERT(ctx->cc_sec->ps_policy);
1549 policy = ctx->cc_sec->ps_policy;
1550 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1553 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1555 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1556 struct ptlrpc_sec_policy *policy;
1560 LASSERT(atomic_read(&ctx->cc_refcount));
1561 LASSERT(ctx->cc_sec);
1562 LASSERT(ctx->cc_sec->ps_policy);
1564 if (req->rq_repbuf == NULL)
1566 LASSERT(req->rq_repbuf_len);
1568 policy = ctx->cc_sec->ps_policy;
1569 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1573 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1574 struct ptlrpc_cli_ctx *ctx)
1576 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1578 if (!policy->sp_cops->install_rctx)
1580 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1583 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1584 struct ptlrpc_svc_ctx *ctx)
1586 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1588 if (!policy->sp_sops->install_rctx)
1590 return policy->sp_sops->install_rctx(imp, ctx);
1593 /****************************************
1594 * server side security *
1595 ****************************************/
1597 static int flavor_allowed(struct sptlrpc_flavor *exp,
1598 struct ptlrpc_request *req)
1600 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1602 if (exp->sf_rpc == flvr->sf_rpc)
1605 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1606 RPC_FLVR_POLICY(exp->sf_rpc) == RPC_FLVR_POLICY(flvr->sf_rpc) &&
1607 RPC_FLVR_MECH(exp->sf_rpc) == RPC_FLVR_MECH(flvr->sf_rpc))
1613 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1615 int sptlrpc_target_export_check(struct obd_export *exp,
1616 struct ptlrpc_request *req)
1618 struct sptlrpc_flavor flavor;
1623 /* client side export has no imp_reverse, skip
1624 * FIXME maybe we should check flavor this as well??? */
1625 if (exp->exp_imp_reverse == NULL)
1628 /* don't care about ctx fini rpc */
1629 if (req->rq_ctx_fini)
1632 spin_lock(&exp->exp_lock);
1634 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1635 * the first req with the new flavor, then treat it as current flavor,
1636 * adapt reverse sec according to it.
1637 * note the first rpc with new flavor might not be with root ctx, in
1638 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1639 if (unlikely(exp->exp_flvr_changed) &&
1640 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1641 /* make the new flavor as "current", and old ones as
1642 * about-to-expire */
1643 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1644 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1645 flavor = exp->exp_flvr_old[1];
1646 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1647 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1648 exp->exp_flvr_old[0] = exp->exp_flvr;
1649 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1650 EXP_FLVR_UPDATE_EXPIRE;
1651 exp->exp_flvr = flavor;
1653 /* flavor change finished */
1654 exp->exp_flvr_changed = 0;
1655 LASSERT(exp->exp_flvr_adapt == 1);
1657 /* if it's gss, we only interested in root ctx init */
1658 if (req->rq_auth_gss &&
1659 !(req->rq_ctx_init && (req->rq_auth_usr_root ||
1660 req->rq_auth_usr_mdt))) {
1661 spin_unlock(&exp->exp_lock);
1662 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d)\n",
1663 req->rq_auth_gss, req->rq_ctx_init,
1664 req->rq_auth_usr_root, req->rq_auth_usr_mdt);
1668 exp->exp_flvr_adapt = 0;
1669 spin_unlock(&exp->exp_lock);
1671 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1672 req->rq_svc_ctx, flavor.sf_rpc);
1675 /* if it equals to the current flavor, we accept it, but need to
1676 * dealing with reverse sec/ctx */
1677 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1678 /* most cases should return here, we only interested in
1679 * gss root ctx init */
1680 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1681 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt)) {
1682 spin_unlock(&exp->exp_lock);
1686 /* if flavor just changed, we should not proceed, just leave
1687 * it and current flavor will be discovered and replaced
1688 * shortly, and let _this_ rpc pass through */
1689 if (exp->exp_flvr_changed) {
1690 LASSERT(exp->exp_flvr_adapt);
1691 spin_unlock(&exp->exp_lock);
1695 if (exp->exp_flvr_adapt) {
1696 exp->exp_flvr_adapt = 0;
1697 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1698 exp, exp->exp_flvr.sf_rpc,
1699 exp->exp_flvr_old[0].sf_rpc,
1700 exp->exp_flvr_old[1].sf_rpc);
1701 flavor = exp->exp_flvr;
1702 spin_unlock(&exp->exp_lock);
1704 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1708 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1709 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1710 exp->exp_flvr_old[0].sf_rpc,
1711 exp->exp_flvr_old[1].sf_rpc);
1712 spin_unlock(&exp->exp_lock);
1714 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1719 if (exp->exp_flvr_expire[0]) {
1720 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1721 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1722 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1723 "middle one ("CFS_DURATION_T")\n", exp,
1724 exp->exp_flvr.sf_rpc,
1725 exp->exp_flvr_old[0].sf_rpc,
1726 exp->exp_flvr_old[1].sf_rpc,
1727 exp->exp_flvr_expire[0] -
1728 cfs_time_current_sec());
1729 spin_unlock(&exp->exp_lock);
1733 CDEBUG(D_SEC, "mark middle expired\n");
1734 exp->exp_flvr_expire[0] = 0;
1736 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1737 exp->exp_flvr.sf_rpc,
1738 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1739 req->rq_flvr.sf_rpc);
1742 /* now it doesn't match the current flavor, the only chance we can
1743 * accept it is match the old flavors which is not expired. */
1744 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1745 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1746 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1747 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1748 "oldest one ("CFS_DURATION_T")\n", exp,
1749 exp->exp_flvr.sf_rpc,
1750 exp->exp_flvr_old[0].sf_rpc,
1751 exp->exp_flvr_old[1].sf_rpc,
1752 exp->exp_flvr_expire[1] -
1753 cfs_time_current_sec());
1754 spin_unlock(&exp->exp_lock);
1758 CDEBUG(D_SEC, "mark oldest expired\n");
1759 exp->exp_flvr_expire[1] = 0;
1761 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1762 exp, exp->exp_flvr.sf_rpc,
1763 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1764 req->rq_flvr.sf_rpc);
1766 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1767 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1768 exp->exp_flvr_old[1].sf_rpc);
1771 spin_unlock(&exp->exp_lock);
1773 CWARN("req %p: (%u|%u|%u|%u|%u) with unauthorized flavor %x\n",
1774 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1775 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_flvr.sf_rpc);
1779 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1780 struct sptlrpc_rule_set *rset)
1782 struct obd_export *exp;
1783 struct sptlrpc_flavor new_flvr;
1787 spin_lock(&obd->obd_dev_lock);
1789 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1790 if (exp->exp_connection == NULL)
1793 /* note if this export had just been updated flavor
1794 * (exp_flvr_changed == 1), this will override the
1796 spin_lock(&exp->exp_lock);
1797 sptlrpc_rule_set_choose(rset, exp->exp_sp_peer,
1798 exp->exp_connection->c_peer.nid,
1800 if (exp->exp_flvr_changed ||
1801 memcmp(&new_flvr, &exp->exp_flvr, sizeof(new_flvr))) {
1802 exp->exp_flvr_old[1] = new_flvr;
1803 exp->exp_flvr_expire[1] = 0;
1804 exp->exp_flvr_changed = 1;
1805 exp->exp_flvr_adapt = 1;
1806 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1807 exp, sptlrpc_part2name(exp->exp_sp_peer),
1808 exp->exp_flvr.sf_rpc,
1809 exp->exp_flvr_old[1].sf_rpc);
1811 spin_unlock(&exp->exp_lock);
1814 spin_unlock(&obd->obd_dev_lock);
1816 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1818 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1820 if (svc_rc == SECSVC_DROP)
1823 switch (req->rq_sp_from) {
1831 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1835 if (!req->rq_auth_gss)
1838 if (unlikely(req->rq_sp_from == LUSTRE_SP_ANY)) {
1839 CERROR("not specific part\n");
1843 /* from MDT, must be authenticated as MDT */
1844 if (unlikely(req->rq_sp_from == LUSTRE_SP_MDT &&
1845 !req->rq_auth_usr_mdt)) {
1846 DEBUG_REQ(D_ERROR, req, "fake source MDT");
1850 /* from OST, must be callback to MDT and CLI, the reverse sec
1851 * was from mdt/root keytab, so it should be MDT or root FIXME */
1852 if (unlikely(req->rq_sp_from == LUSTRE_SP_OST &&
1853 !req->rq_auth_usr_mdt && !req->rq_auth_usr_root)) {
1854 DEBUG_REQ(D_ERROR, req, "fake source OST");
1861 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1863 struct ptlrpc_sec_policy *policy;
1864 struct lustre_msg *msg = req->rq_reqbuf;
1869 LASSERT(req->rq_reqmsg == NULL);
1870 LASSERT(req->rq_repmsg == NULL);
1872 req->rq_sp_from = LUSTRE_SP_ANY;
1873 req->rq_auth_uid = INVALID_UID;
1874 req->rq_auth_mapped_uid = INVALID_UID;
1876 if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1877 CERROR("request size %d too small\n", req->rq_reqdata_len);
1878 RETURN(SECSVC_DROP);
1884 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2)
1885 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(msg->lm_secflvr);
1887 req->rq_flvr.sf_rpc = WIRE_FLVR_RPC(__swab32(msg->lm_secflvr));
1889 /* unpack the wrapper message if the policy is not null */
1890 if ((RPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) &&
1891 lustre_unpack_msg(msg, req->rq_reqdata_len))
1892 RETURN(SECSVC_DROP);
1894 policy = sptlrpc_rpcflavor2policy(req->rq_flvr.sf_rpc);
1896 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1897 RETURN(SECSVC_DROP);
1900 LASSERT(policy->sp_sops->accept);
1901 rc = policy->sp_sops->accept(req);
1903 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1904 sptlrpc_policy_put(policy);
1906 /* sanity check for the request source */
1907 rc = sptlrpc_svc_check_from(req, rc);
1909 /* FIXME move to proper place */
1910 if (rc == SECSVC_OK) {
1911 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1913 if (opc == OST_WRITE)
1914 req->rq_bulk_write = 1;
1915 else if (opc == OST_READ)
1916 req->rq_bulk_read = 1;
1919 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1923 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1926 struct ptlrpc_sec_policy *policy;
1927 struct ptlrpc_reply_state *rs;
1931 LASSERT(req->rq_svc_ctx);
1932 LASSERT(req->rq_svc_ctx->sc_policy);
1934 policy = req->rq_svc_ctx->sc_policy;
1935 LASSERT(policy->sp_sops->alloc_rs);
1937 rc = policy->sp_sops->alloc_rs(req, msglen);
1938 if (unlikely(rc == -ENOMEM)) {
1939 /* failed alloc, try emergency pool */
1940 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
1944 req->rq_reply_state = rs;
1945 rc = policy->sp_sops->alloc_rs(req, msglen);
1947 lustre_put_emerg_rs(rs);
1948 req->rq_reply_state = NULL;
1953 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1958 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1960 struct ptlrpc_sec_policy *policy;
1964 LASSERT(req->rq_svc_ctx);
1965 LASSERT(req->rq_svc_ctx->sc_policy);
1967 policy = req->rq_svc_ctx->sc_policy;
1968 LASSERT(policy->sp_sops->authorize);
1970 rc = policy->sp_sops->authorize(req);
1971 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1976 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1978 struct ptlrpc_sec_policy *policy;
1979 unsigned int prealloc;
1982 LASSERT(rs->rs_svc_ctx);
1983 LASSERT(rs->rs_svc_ctx->sc_policy);
1985 policy = rs->rs_svc_ctx->sc_policy;
1986 LASSERT(policy->sp_sops->free_rs);
1988 prealloc = rs->rs_prealloc;
1989 policy->sp_sops->free_rs(rs);
1992 lustre_put_emerg_rs(rs);
1996 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
1998 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2003 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2004 atomic_inc(&ctx->sc_refcount);
2007 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2009 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2014 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2015 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2016 if (ctx->sc_policy->sp_sops->free_ctx)
2017 ctx->sc_policy->sp_sops->free_ctx(ctx);
2019 req->rq_svc_ctx = NULL;
2022 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2024 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2029 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2030 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2031 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2033 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2035 /****************************************
2037 ****************************************/
2039 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2040 struct ptlrpc_bulk_desc *desc)
2042 struct ptlrpc_cli_ctx *ctx;
2044 if (!req->rq_pack_bulk)
2047 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2049 ctx = req->rq_cli_ctx;
2050 if (ctx->cc_ops->wrap_bulk)
2051 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2054 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2057 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
2058 struct ptlrpc_bulk_desc *desc)
2065 for (i = 0; i < pg_count && nob > 0; i++) {
2067 desc->bd_iov[i].kiov_page = pga[i]->pg;
2068 desc->bd_iov[i].kiov_len = pga[i]->count > nob ?
2069 nob : pga[i]->count;
2070 desc->bd_iov[i].kiov_offset = pga[i]->off & ~CFS_PAGE_MASK;
2072 /* FIXME currently liblustre doesn't support bulk encryption.
2073 * if we do, check again following may not be right. */
2074 LASSERTF(0, "Bulk encryption not implemented for liblustre\n");
2075 desc->bd_iov[i].iov_base = pga[i]->pg->addr;
2076 desc->bd_iov[i].iov_len = pga[i]->count > nob ?
2077 nob : pga[i]->count;
2080 desc->bd_iov_count++;
2081 nob -= pga[i]->count;
2085 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2086 int nob, obd_count pg_count,
2087 struct brw_page **pga)
2089 struct ptlrpc_bulk_desc *desc;
2090 struct ptlrpc_cli_ctx *ctx;
2093 if (!req->rq_pack_bulk)
2096 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2098 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2100 CERROR("out of memory, can't verify bulk read data\n");
2104 pga_to_bulk_desc(nob, pg_count, pga, desc);
2106 ctx = req->rq_cli_ctx;
2107 if (ctx->cc_ops->unwrap_bulk)
2108 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2110 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
2114 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2116 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2117 struct ptlrpc_bulk_desc *desc)
2119 struct ptlrpc_cli_ctx *ctx;
2121 if (!req->rq_pack_bulk)
2124 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2126 ctx = req->rq_cli_ctx;
2127 if (ctx->cc_ops->unwrap_bulk)
2128 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2132 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2134 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2135 struct ptlrpc_bulk_desc *desc)
2137 struct ptlrpc_svc_ctx *ctx;
2139 if (!req->rq_pack_bulk)
2142 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2144 ctx = req->rq_svc_ctx;
2145 if (ctx->sc_policy->sp_sops->wrap_bulk)
2146 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2150 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2152 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2153 struct ptlrpc_bulk_desc *desc)
2155 struct ptlrpc_svc_ctx *ctx;
2157 if (!req->rq_pack_bulk)
2160 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2162 ctx = req->rq_svc_ctx;
2163 if (ctx->sc_policy->sp_sops->unwrap_bulk);
2164 return ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2168 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2171 /****************************************
2172 * user descriptor helpers *
2173 ****************************************/
2175 int sptlrpc_current_user_desc_size(void)
2180 ngroups = current_ngroups;
2182 if (ngroups > LUSTRE_MAX_GROUPS)
2183 ngroups = LUSTRE_MAX_GROUPS;
2187 return sptlrpc_user_desc_size(ngroups);
2189 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2191 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2193 struct ptlrpc_user_desc *pud;
2195 pud = lustre_msg_buf(msg, offset, 0);
2197 pud->pud_uid = cfs_current()->uid;
2198 pud->pud_gid = cfs_current()->gid;
2199 pud->pud_fsuid = cfs_current()->fsuid;
2200 pud->pud_fsgid = cfs_current()->fsgid;
2201 pud->pud_cap = cfs_curproc_cap_pack();
2202 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2206 if (pud->pud_ngroups > current_ngroups)
2207 pud->pud_ngroups = current_ngroups;
2208 memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
2209 pud->pud_ngroups * sizeof(__u32));
2210 task_unlock(current);
2215 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2217 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
2219 struct ptlrpc_user_desc *pud;
2222 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2226 if (lustre_msg_swabbed(msg)) {
2227 __swab32s(&pud->pud_uid);
2228 __swab32s(&pud->pud_gid);
2229 __swab32s(&pud->pud_fsuid);
2230 __swab32s(&pud->pud_fsgid);
2231 __swab32s(&pud->pud_cap);
2232 __swab32s(&pud->pud_ngroups);
2235 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2236 CERROR("%u groups is too large\n", pud->pud_ngroups);
2240 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2241 msg->lm_buflens[offset]) {
2242 CERROR("%u groups are claimed but bufsize only %u\n",
2243 pud->pud_ngroups, msg->lm_buflens[offset]);
2247 if (lustre_msg_swabbed(msg)) {
2248 for (i = 0; i < pud->pud_ngroups; i++)
2249 __swab32s(&pud->pud_groups[i]);
2254 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2256 /****************************************
2258 ****************************************/
2260 const char * sec2target_str(struct ptlrpc_sec *sec)
2262 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2264 if (sec_is_reverse(sec))
2266 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2268 EXPORT_SYMBOL(sec2target_str);
2270 /****************************************
2271 * crypto API helper/alloc blkciper *
2272 ****************************************/
2275 #ifndef HAVE_ASYNC_BLOCK_CIPHER
2276 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char * algname,
2279 char buf[CRYPTO_MAX_ALG_NAME + 1];
2280 const char *pan = algname;
2283 if (strncmp("cbc(", algname, 4) == 0)
2284 flag |= CRYPTO_TFM_MODE_CBC;
2285 else if (strncmp("ecb(", algname, 4) == 0)
2286 flag |= CRYPTO_TFM_MODE_ECB;
2288 char *vp = strnchr(algname, CRYPTO_MAX_ALG_NAME, ')');
2290 memcpy(buf, algname + 4, vp - algname - 4);
2291 buf[vp - algname - 4] = '\0';
2297 return crypto_alloc_tfm(pan, flag);
2299 EXPORT_SYMBOL(ll_crypto_alloc_blkcipher);
2303 /****************************************
2304 * initialize/finalize *
2305 ****************************************/
2307 int __init sptlrpc_init(void)
2311 rwlock_init(&policy_lock);
2313 rc = sptlrpc_gc_start_thread();
2317 rc = sptlrpc_enc_pool_init();
2321 rc = sptlrpc_null_init();
2325 rc = sptlrpc_plain_init();
2329 rc = sptlrpc_lproc_init();
2336 sptlrpc_plain_fini();
2338 sptlrpc_null_fini();
2340 sptlrpc_enc_pool_fini();
2342 sptlrpc_gc_stop_thread();
2347 void __exit sptlrpc_fini(void)
2349 sptlrpc_lproc_fini();
2350 sptlrpc_plain_fini();
2351 sptlrpc_null_fini();
2352 sptlrpc_enc_pool_fini();
2353 sptlrpc_gc_stop_thread();