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_wireflavor2policy(__u32 flavor)
123 static DECLARE_MUTEX(load_mutex);
124 static atomic_t loaded = ATOMIC_INIT(0);
125 struct ptlrpc_sec_policy *policy;
126 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
129 if (number >= SPTLRPC_POLICY_MAX)
133 read_lock(&policy_lock);
134 policy = policies[number];
135 if (policy && !try_module_get(policy->sp_owner))
138 flag = atomic_read(&loaded);
139 read_unlock(&policy_lock);
141 if (policy != NULL || flag != 0 ||
142 number != SPTLRPC_POLICY_GSS)
145 /* try to load gss module, once */
146 mutex_down(&load_mutex);
147 if (atomic_read(&loaded) == 0) {
148 if (request_module("ptlrpc_gss") == 0)
149 CWARN("module ptlrpc_gss loaded on demand\n");
151 CERROR("Unable to load module ptlrpc_gss\n");
153 atomic_set(&loaded, 1);
155 mutex_up(&load_mutex);
161 __u32 sptlrpc_name2flavor_base(const char *name)
163 if (!strcmp(name, "null"))
164 return SPTLRPC_FLVR_NULL;
165 if (!strcmp(name, "plain"))
166 return SPTLRPC_FLVR_PLAIN;
167 if (!strcmp(name, "krb5n"))
168 return SPTLRPC_FLVR_KRB5N;
169 if (!strcmp(name, "krb5a"))
170 return SPTLRPC_FLVR_KRB5A;
171 if (!strcmp(name, "krb5i"))
172 return SPTLRPC_FLVR_KRB5I;
173 if (!strcmp(name, "krb5p"))
174 return SPTLRPC_FLVR_KRB5P;
176 return SPTLRPC_FLVR_INVALID;
178 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
180 const char *sptlrpc_flavor2name_base(__u32 flvr)
182 __u32 base = SPTLRPC_FLVR_BASE(flvr);
184 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
186 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
188 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
190 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
192 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
194 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
197 CERROR("invalid wire flavor 0x%x\n", flvr);
200 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
202 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
203 char *buf, int bufsize)
205 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
206 snprintf(buf, bufsize, "hash:%s",
207 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
209 snprintf(buf, bufsize, "%s",
210 sptlrpc_flavor2name_base(sf->sf_rpc));
212 buf[bufsize - 1] = '\0';
215 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
217 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
219 snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
222 * currently we don't support customized bulk specification for
223 * flavors other than plain
225 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
229 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
230 strncat(buf, bspec, bufsize);
233 buf[bufsize - 1] = '\0';
236 EXPORT_SYMBOL(sptlrpc_flavor2name);
238 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
242 if (flags & PTLRPC_SEC_FL_REVERSE)
243 strncat(buf, "reverse,", bufsize);
244 if (flags & PTLRPC_SEC_FL_ROOTONLY)
245 strncat(buf, "rootonly,", bufsize);
246 if (flags & PTLRPC_SEC_FL_UDESC)
247 strncat(buf, "udesc,", bufsize);
248 if (flags & PTLRPC_SEC_FL_BULK)
249 strncat(buf, "bulk,", bufsize);
251 strncat(buf, "-,", bufsize);
253 buf[bufsize - 1] = '\0';
256 EXPORT_SYMBOL(sptlrpc_secflags2str);
258 /**************************************************
259 * client context APIs *
260 **************************************************/
263 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
265 struct vfs_cred vcred;
266 int create = 1, remove_dead = 1;
269 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
271 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
272 PTLRPC_SEC_FL_ROOTONLY)) {
275 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
280 vcred.vc_uid = cfs_current()->uid;
281 vcred.vc_gid = cfs_current()->gid;
284 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
285 create, remove_dead);
288 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
290 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
291 atomic_inc(&ctx->cc_refcount);
294 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
296 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
298 struct ptlrpc_sec *sec = ctx->cc_sec;
301 LASSERT(atomic_read(&ctx->cc_refcount));
303 if (!atomic_dec_and_test(&ctx->cc_refcount))
306 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
308 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
311 * expire the context immediately.
312 * the caller must hold at least 1 ref on the ctx.
314 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
316 LASSERT(ctx->cc_ops->die);
317 ctx->cc_ops->die(ctx, 0);
319 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
321 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
323 struct ptlrpc_request *req, *next;
325 spin_lock(&ctx->cc_lock);
326 list_for_each_entry_safe(req, next, &ctx->cc_req_list, rq_ctx_chain) {
327 list_del_init(&req->rq_ctx_chain);
328 ptlrpc_client_wake_req(req);
330 spin_unlock(&ctx->cc_lock);
332 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
334 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
336 LASSERT(ctx->cc_ops);
338 if (ctx->cc_ops->display == NULL)
341 return ctx->cc_ops->display(ctx, buf, bufsize);
344 static int import_sec_check_expire(struct obd_import *imp)
348 spin_lock(&imp->imp_lock);
349 if (imp->imp_sec_expire &&
350 imp->imp_sec_expire < cfs_time_current_sec()) {
352 imp->imp_sec_expire = 0;
354 spin_unlock(&imp->imp_lock);
359 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
360 return sptlrpc_import_sec_adapt(imp, NULL, 0);
363 static int import_sec_validate_get(struct obd_import *imp,
364 struct ptlrpc_sec **sec)
368 if (unlikely(imp->imp_sec_expire)) {
369 rc = import_sec_check_expire(imp);
374 *sec = sptlrpc_import_sec_ref(imp);
376 CERROR("import %p (%s) with no sec\n",
377 imp, ptlrpc_import_state_name(imp->imp_state));
381 if (unlikely((*sec)->ps_dying)) {
382 CERROR("attempt to use dying sec %p\n", sec);
383 sptlrpc_sec_put(*sec);
390 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
392 struct obd_import *imp = req->rq_import;
393 struct ptlrpc_sec *sec;
397 LASSERT(!req->rq_cli_ctx);
400 rc = import_sec_validate_get(imp, &sec);
404 req->rq_cli_ctx = get_my_ctx(sec);
406 sptlrpc_sec_put(sec);
408 if (!req->rq_cli_ctx) {
409 CERROR("req %p: fail to get context\n", req);
417 * if @sync == 0, this function should return quickly without sleep;
418 * otherwise might trigger ctx destroying rpc to server.
420 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
425 LASSERT(req->rq_cli_ctx);
427 /* request might be asked to release earlier while still
428 * in the context waiting list.
430 if (!list_empty(&req->rq_ctx_chain)) {
431 spin_lock(&req->rq_cli_ctx->cc_lock);
432 list_del_init(&req->rq_ctx_chain);
433 spin_unlock(&req->rq_cli_ctx->cc_lock);
436 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
437 req->rq_cli_ctx = NULL;
442 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
443 struct ptlrpc_cli_ctx *oldctx,
444 struct ptlrpc_cli_ctx *newctx)
446 struct sptlrpc_flavor old_flvr;
451 LASSERT(req->rq_reqmsg);
452 LASSERT(req->rq_reqlen);
453 LASSERT(req->rq_replen);
455 CWARN("req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
456 "switch sec %p(%s) -> %p(%s)\n", req,
457 oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
458 newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
459 oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
460 newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
463 old_flvr = req->rq_flvr;
465 /* save request message */
466 reqmsg_size = req->rq_reqlen;
467 OBD_ALLOC(reqmsg, reqmsg_size);
470 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
472 /* release old req/rep buf */
473 req->rq_cli_ctx = oldctx;
474 sptlrpc_cli_free_reqbuf(req);
475 sptlrpc_cli_free_repbuf(req);
476 req->rq_cli_ctx = newctx;
478 /* recalculate the flavor */
479 sptlrpc_req_set_flavor(req, 0);
481 /* alloc new request buffer
482 * we don't need to alloc reply buffer here, leave it to the
483 * rest procedure of ptlrpc
485 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
487 LASSERT(req->rq_reqmsg);
488 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
490 CWARN("failed to alloc reqbuf: %d\n", rc);
491 req->rq_flvr = old_flvr;
494 OBD_FREE(reqmsg, reqmsg_size);
499 * if current context has died, or if we resend after flavor switched,
500 * call this func to switch context. if no switch is needed, request
501 * will end up with the same context.
503 * request must have a context. in any case of failure, restore the
504 * restore the old one - a request must have a context.
506 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
508 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
509 struct ptlrpc_cli_ctx *newctx;
515 sptlrpc_cli_ctx_get(oldctx);
516 sptlrpc_req_put_ctx(req, 0);
518 rc = sptlrpc_req_get_ctx(req);
520 LASSERT(!req->rq_cli_ctx);
522 /* restore old ctx */
523 req->rq_cli_ctx = oldctx;
527 newctx = req->rq_cli_ctx;
530 if (unlikely(newctx == oldctx &&
531 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
533 * still get the old dead ctx, usually means system too busy
535 CWARN("ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
536 newctx, newctx->cc_flags);
538 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE, HZ);
541 * it's possible newctx == oldctx if we're switching
542 * subflavor with the same sec.
544 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
546 /* restore old ctx */
547 sptlrpc_req_put_ctx(req, 0);
548 req->rq_cli_ctx = oldctx;
552 LASSERT(req->rq_cli_ctx == newctx);
555 sptlrpc_cli_ctx_put(oldctx, 1);
558 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
561 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
563 if (cli_ctx_is_refreshed(ctx))
569 int ctx_refresh_timeout(void *data)
571 struct ptlrpc_request *req = data;
574 /* conn_cnt is needed in expire_one_request */
575 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
577 rc = ptlrpc_expire_one_request(req, 1);
578 /* if we started recovery, we should mark this ctx dead; otherwise
579 * in case of lgssd died nobody would retire this ctx, following
580 * connecting will still find the same ctx thus cause deadlock.
581 * there's an assumption that expire time of the request should be
582 * later than the context refresh expire time.
585 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
590 void ctx_refresh_interrupt(void *data)
592 struct ptlrpc_request *req = data;
594 spin_lock(&req->rq_lock);
596 spin_unlock(&req->rq_lock);
600 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
602 spin_lock(&ctx->cc_lock);
603 if (!list_empty(&req->rq_ctx_chain))
604 list_del_init(&req->rq_ctx_chain);
605 spin_unlock(&ctx->cc_lock);
609 * the status of context could be subject to be changed by other threads at any
610 * time. we allow this race. but once we return with 0, the caller will
611 * suppose it's uptodated and keep using it until the owning rpc is done.
615 * = 0 - wait until success or fatal error occur
616 * > 0 - timeout value
618 * return 0 only if the context is uptodated.
620 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
622 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
623 struct ptlrpc_sec *sec;
624 struct l_wait_info lwi;
630 if (req->rq_ctx_init || req->rq_ctx_fini)
634 * during the process a request's context might change type even
635 * (e.g. from gss ctx to plain ctx), so each loop we need to re-check
639 rc = import_sec_validate_get(req->rq_import, &sec);
643 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
644 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
645 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
646 req_off_ctx_list(req, ctx);
647 sptlrpc_req_replace_dead_ctx(req);
648 ctx = req->rq_cli_ctx;
650 sptlrpc_sec_put(sec);
652 if (cli_ctx_is_eternal(ctx))
655 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
656 LASSERT(ctx->cc_ops->refresh);
657 ctx->cc_ops->refresh(ctx);
659 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
661 LASSERT(ctx->cc_ops->validate);
662 if (ctx->cc_ops->validate(ctx) == 0) {
663 req_off_ctx_list(req, ctx);
667 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
669 req_off_ctx_list(req, ctx);
673 /* This is subtle. For resent message we have to keep original
674 * context to survive following situation:
675 * 1. the request sent to server
676 * 2. recovery was kick start
677 * 3. recovery finished, the request marked as resent
678 * 4. resend the request
679 * 5. old reply from server received (because xid is the same)
680 * 6. verify reply (has to be success)
681 * 7. new reply from server received, lnet drop it
683 * Note we can't simply change xid for resent request because
684 * server reply on it for reply reconstruction.
686 * Commonly the original context should be uptodate because we
687 * have a expiry nice time; And server will keep their half part
688 * context because we at least hold a ref of old context which
689 * prevent the context destroy RPC be sent. So server still can
690 * accept the request and finish RPC. Two cases:
691 * 1. If server side context has been trimmed, a NO_CONTEXT will
692 * be returned, gss_cli_ctx_verify/unseal will switch to new
694 * 2. Current context never be refreshed, then we are fine: we
695 * never really send request with old context before.
697 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
698 unlikely(req->rq_reqmsg) &&
699 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
700 req_off_ctx_list(req, ctx);
704 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
705 req_off_ctx_list(req, ctx);
707 * don't switch ctx if import was deactivated
709 if (req->rq_import->imp_deactive) {
714 rc = sptlrpc_req_replace_dead_ctx(req);
716 LASSERT(ctx == req->rq_cli_ctx);
717 CERROR("req %p: failed to replace dead ctx %p: %d\n",
723 ctx = req->rq_cli_ctx;
727 /* Now we're sure this context is during upcall, add myself into
730 spin_lock(&ctx->cc_lock);
731 if (list_empty(&req->rq_ctx_chain))
732 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
733 spin_unlock(&ctx->cc_lock);
736 RETURN(-EWOULDBLOCK);
738 /* Clear any flags that may be present from previous sends */
739 LASSERT(req->rq_receiving_reply == 0);
740 spin_lock(&req->rq_lock);
742 req->rq_timedout = 0;
745 spin_unlock(&req->rq_lock);
747 lwi = LWI_TIMEOUT_INTR(timeout * HZ, ctx_refresh_timeout,
748 ctx_refresh_interrupt, req);
749 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
751 /* following cases we could be here:
752 * - successfully refreshed;
754 * - timedout, and we don't want recover from the failure;
755 * - timedout, and waked up upon recovery finished;
756 * - someone else mark this ctx dead by force;
757 * - someone invalidate the req and call ptlrpc_client_wake_req(),
758 * e.g. ptlrpc_abort_inflight();
760 if (!cli_ctx_is_refreshed(ctx)) {
761 /* timed out or interruptted */
762 req_off_ctx_list(req, ctx);
772 * Note this could be called in two situations:
773 * - new request from ptlrpc_pre_req(), with proper @opcode
774 * - old request which changed ctx in the middle, with @opcode == 0
776 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
778 struct ptlrpc_sec *sec;
780 LASSERT(req->rq_import);
781 LASSERT(req->rq_cli_ctx);
782 LASSERT(req->rq_cli_ctx->cc_sec);
783 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
785 /* special security flags accoding to opcode */
789 req->rq_bulk_read = 1;
793 req->rq_bulk_write = 1;
796 req->rq_ctx_init = 1;
799 req->rq_ctx_fini = 1;
802 /* init/fini rpc won't be resend, so can't be here */
803 LASSERT(req->rq_ctx_init == 0);
804 LASSERT(req->rq_ctx_fini == 0);
806 /* cleanup flags, which should be recalculated */
807 req->rq_pack_udesc = 0;
808 req->rq_pack_bulk = 0;
812 sec = req->rq_cli_ctx->cc_sec;
814 spin_lock(&sec->ps_lock);
815 req->rq_flvr = sec->ps_flvr;
816 spin_unlock(&sec->ps_lock);
818 /* force SVC_NULL for context initiation rpc, SVC_INTG for context
820 if (unlikely(req->rq_ctx_init))
821 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
822 else if (unlikely(req->rq_ctx_fini))
823 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
825 /* user descriptor flag, null security can't do it anyway */
826 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
827 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
828 req->rq_pack_udesc = 1;
830 /* bulk security flag */
831 if ((req->rq_bulk_read || req->rq_bulk_write) &&
832 sptlrpc_flavor_has_bulk(&req->rq_flvr))
833 req->rq_pack_bulk = 1;
836 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
838 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
841 LASSERT(req->rq_clrbuf);
842 if (req->rq_pool || !req->rq_reqbuf)
845 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
846 req->rq_reqbuf = NULL;
847 req->rq_reqbuf_len = 0;
851 * check whether current user have valid context for an import or not.
852 * might repeatedly try in case of non-fatal errors.
853 * return 0 on success, < 0 on failure
855 int sptlrpc_import_check_ctx(struct obd_import *imp)
857 struct ptlrpc_sec *sec;
858 struct ptlrpc_cli_ctx *ctx;
859 struct ptlrpc_request *req = NULL;
865 sec = sptlrpc_import_sec_ref(imp);
866 ctx = get_my_ctx(sec);
867 sptlrpc_sec_put(sec);
872 if (cli_ctx_is_eternal(ctx) ||
873 ctx->cc_ops->validate(ctx) == 0) {
874 sptlrpc_cli_ctx_put(ctx, 1);
878 if (cli_ctx_is_error(ctx)) {
879 sptlrpc_cli_ctx_put(ctx, 1);
887 spin_lock_init(&req->rq_lock);
888 atomic_set(&req->rq_refcount, 10000);
889 CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
890 cfs_waitq_init(&req->rq_reply_waitq);
891 req->rq_import = imp;
892 req->rq_flvr = sec->ps_flvr;
893 req->rq_cli_ctx = ctx;
895 rc = sptlrpc_req_refresh_ctx(req, 0);
896 LASSERT(list_empty(&req->rq_ctx_chain));
897 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
903 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
905 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
910 LASSERT(ctx->cc_sec);
911 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
913 /* we wrap bulk request here because now we can be sure
914 * the context is uptodate.
917 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
922 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
923 case SPTLRPC_SVC_NULL:
924 case SPTLRPC_SVC_AUTH:
925 case SPTLRPC_SVC_INTG:
926 LASSERT(ctx->cc_ops->sign);
927 rc = ctx->cc_ops->sign(ctx, req);
929 case SPTLRPC_SVC_PRIV:
930 LASSERT(ctx->cc_ops->seal);
931 rc = ctx->cc_ops->seal(ctx, req);
938 LASSERT(req->rq_reqdata_len);
939 LASSERT(req->rq_reqdata_len % 8 == 0);
940 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
946 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
948 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
954 LASSERT(ctx->cc_sec);
955 LASSERT(req->rq_repbuf);
956 LASSERT(req->rq_repdata);
957 LASSERT(req->rq_repmsg == NULL);
959 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
960 CERROR("replied data length %d too small\n",
961 req->rq_repdata_len);
965 /* v2 message, check request/reply policy match */
966 flvr = WIRE_FLVR(req->rq_repdata->lm_secflvr);
968 if (req->rq_repdata->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
971 if (SPTLRPC_FLVR_POLICY(flvr) !=
972 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
973 CERROR("request policy was %u while reply with %u\n",
974 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc),
975 SPTLRPC_FLVR_POLICY(flvr));
979 /* do nothing if it's null policy; otherwise unpack the
981 if (SPTLRPC_FLVR_POLICY(flvr) != SPTLRPC_POLICY_NULL &&
982 lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len))
985 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
986 case SPTLRPC_SVC_NULL:
987 case SPTLRPC_SVC_AUTH:
988 case SPTLRPC_SVC_INTG:
989 LASSERT(ctx->cc_ops->verify);
990 rc = ctx->cc_ops->verify(ctx, req);
992 case SPTLRPC_SVC_PRIV:
993 LASSERT(ctx->cc_ops->unseal);
994 rc = ctx->cc_ops->unseal(ctx, req);
1000 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1005 * upon this be called, the reply buffer should have been un-posted,
1006 * so nothing is going to change.
1008 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1010 LASSERT(req->rq_repbuf);
1011 LASSERT(req->rq_repdata == NULL);
1012 LASSERT(req->rq_repmsg == NULL);
1013 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1015 if (req->rq_reply_off == 0 &&
1016 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1017 CERROR("real reply with offset 0\n");
1021 if (req->rq_reply_off % 8 != 0) {
1022 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1026 req->rq_repdata = (struct lustre_msg *)
1027 (req->rq_repbuf + req->rq_reply_off);
1028 req->rq_repdata_len = req->rq_nob_received;
1030 return do_cli_unwrap_reply(req);
1034 * Upon called, the receive buffer might be still posted, so the reply data
1035 * might be changed at any time, no matter we're holding rq_lock or not. we
1036 * expect the rq_reply_off be 0, rq_nob_received is the early reply size.
1038 * we allocate separate ptlrpc_request and reply buffer for early reply
1039 * processing, return 0 and @req_ret is a duplicated ptlrpc_request. caller
1040 * must call sptlrpc_cli_finish_early_reply() on the returned request to
1041 * release it. if anything goes wrong @req_ret will not be set.
1043 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1044 struct ptlrpc_request **req_ret)
1046 struct ptlrpc_request *early_req;
1048 int early_bufsz, early_size;
1052 OBD_ALLOC_PTR(early_req);
1053 if (early_req == NULL)
1056 early_size = req->rq_nob_received;
1057 early_bufsz = size_roundup_power2(early_size);
1058 OBD_ALLOC(early_buf, early_bufsz);
1059 if (early_buf == NULL)
1060 GOTO(err_req, rc = -ENOMEM);
1062 /* sanity checkings and copy data out, do it inside spinlock */
1063 spin_lock(&req->rq_lock);
1065 if (req->rq_replied) {
1066 spin_unlock(&req->rq_lock);
1067 GOTO(err_buf, rc = -EALREADY);
1070 LASSERT(req->rq_repbuf);
1071 LASSERT(req->rq_repdata == NULL);
1072 LASSERT(req->rq_repmsg == NULL);
1074 if (req->rq_reply_off != 0) {
1075 CERROR("early reply with offset %u\n", req->rq_reply_off);
1076 spin_unlock(&req->rq_lock);
1077 GOTO(err_buf, rc = -EPROTO);
1080 if (req->rq_nob_received != early_size) {
1081 /* even another early arrived the size should be the same */
1082 CERROR("data size has changed from %u to %u\n",
1083 early_size, req->rq_nob_received);
1084 spin_unlock(&req->rq_lock);
1085 GOTO(err_buf, rc = -EINVAL);
1088 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1089 CERROR("early reply length %d too small\n",
1090 req->rq_nob_received);
1091 spin_unlock(&req->rq_lock);
1092 GOTO(err_buf, rc = -EALREADY);
1095 memcpy(early_buf, req->rq_repbuf, early_size);
1096 spin_unlock(&req->rq_lock);
1098 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1099 early_req->rq_flvr = req->rq_flvr;
1100 early_req->rq_repbuf = early_buf;
1101 early_req->rq_repbuf_len = early_bufsz;
1102 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1103 early_req->rq_repdata_len = early_size;
1104 early_req->rq_early = 1;
1106 rc = do_cli_unwrap_reply(early_req);
1108 DEBUG_REQ(D_ADAPTTO, early_req,
1109 "error %d unwrap early reply", rc);
1113 LASSERT(early_req->rq_repmsg);
1114 *req_ret = early_req;
1118 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1120 OBD_FREE(early_buf, early_bufsz);
1122 OBD_FREE_PTR(early_req);
1126 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1128 LASSERT(early_req->rq_repbuf);
1129 LASSERT(early_req->rq_repdata);
1130 LASSERT(early_req->rq_repmsg);
1132 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1133 OBD_FREE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1134 OBD_FREE_PTR(early_req);
1137 /**************************************************
1139 **************************************************/
1142 * "fixed" sec (e.g. null) use sec_id < 0
1144 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1146 int sptlrpc_get_next_secid(void)
1148 return atomic_inc_return(&sptlrpc_sec_id);
1150 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1152 /**************************************************
1153 * client side high-level security APIs *
1154 **************************************************/
1156 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1157 int grace, int force)
1159 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1161 LASSERT(policy->sp_cops);
1162 LASSERT(policy->sp_cops->flush_ctx_cache);
1164 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1167 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1169 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1171 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1172 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1173 LASSERT(policy->sp_cops->destroy_sec);
1175 CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1177 policy->sp_cops->destroy_sec(sec);
1178 sptlrpc_policy_put(policy);
1181 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1183 sec_cop_destroy_sec(sec);
1185 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1187 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1189 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1191 if (sec->ps_policy->sp_cops->kill_sec) {
1192 sec->ps_policy->sp_cops->kill_sec(sec);
1194 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1198 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1201 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1202 atomic_inc(&sec->ps_refcount);
1207 EXPORT_SYMBOL(sptlrpc_sec_get);
1209 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1212 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1214 if (atomic_dec_and_test(&sec->ps_refcount)) {
1215 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1217 sptlrpc_gc_del_sec(sec);
1218 sec_cop_destroy_sec(sec);
1222 EXPORT_SYMBOL(sptlrpc_sec_put);
1225 * policy module is responsible for taking refrence of import
1228 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1229 struct ptlrpc_svc_ctx *svc_ctx,
1230 struct sptlrpc_flavor *sf,
1231 enum lustre_sec_part sp)
1233 struct ptlrpc_sec_policy *policy;
1234 struct ptlrpc_sec *sec;
1239 LASSERT(imp->imp_dlm_fake == 1);
1241 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1242 imp->imp_obd->obd_type->typ_name,
1243 imp->imp_obd->obd_name,
1244 sptlrpc_flavor2name(sf, str, sizeof(str)));
1246 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1247 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1249 LASSERT(imp->imp_dlm_fake == 0);
1251 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1252 imp->imp_obd->obd_type->typ_name,
1253 imp->imp_obd->obd_name,
1254 sptlrpc_flavor2name(sf, str, sizeof(str)));
1256 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1258 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1263 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1265 atomic_inc(&sec->ps_refcount);
1269 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1270 sptlrpc_gc_add_sec(sec);
1272 sptlrpc_policy_put(policy);
1278 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1280 struct ptlrpc_sec *sec;
1282 spin_lock(&imp->imp_lock);
1283 sec = sptlrpc_sec_get(imp->imp_sec);
1284 spin_unlock(&imp->imp_lock);
1288 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1290 static void sptlrpc_import_sec_install(struct obd_import *imp,
1291 struct ptlrpc_sec *sec)
1293 struct ptlrpc_sec *old_sec;
1295 LASSERT(atomic_read(&sec->ps_refcount) > 0);
1297 spin_lock(&imp->imp_lock);
1298 old_sec = imp->imp_sec;
1300 spin_unlock(&imp->imp_lock);
1303 sptlrpc_sec_kill(old_sec);
1305 /* balance the ref taken by this import */
1306 sptlrpc_sec_put(old_sec);
1311 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1313 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1317 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1322 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1323 struct ptlrpc_sec *sec,
1324 struct sptlrpc_flavor *sf)
1326 char str1[32], str2[32];
1328 if (sec->ps_flvr.sf_flags != sf->sf_flags)
1329 CWARN("changing sec flags: %s -> %s\n",
1330 sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1331 str1, sizeof(str1)),
1332 sptlrpc_secflags2str(sf->sf_flags,
1333 str2, sizeof(str2)));
1335 spin_lock(&sec->ps_lock);
1336 flavor_copy(&sec->ps_flvr, sf);
1337 spin_unlock(&sec->ps_lock);
1341 * for normal import, @svc_ctx should be NULL and @flvr is ignored;
1342 * for reverse import, @svc_ctx and @flvr is from incoming request.
1344 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1345 struct ptlrpc_svc_ctx *svc_ctx,
1346 struct sptlrpc_flavor *flvr)
1348 struct ptlrpc_connection *conn;
1349 struct sptlrpc_flavor sf;
1350 struct ptlrpc_sec *sec, *newsec;
1351 enum lustre_sec_part sp;
1361 conn = imp->imp_connection;
1363 if (svc_ctx == NULL) {
1364 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1366 * normal import, determine flavor from rule set, except
1367 * for mgc the flavor is predetermined.
1369 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1370 sf = cliobd->cl_flvr_mgc;
1372 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1374 &cliobd->cl_target_uuid,
1377 sp = imp->imp_obd->u.cli.cl_sp_me;
1379 /* reverse import, determine flavor from incoming reqeust */
1382 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1383 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1384 PTLRPC_SEC_FL_ROOTONLY;
1386 sp = sptlrpc_target_sec_part(imp->imp_obd);
1389 sec = sptlrpc_import_sec_ref(imp);
1393 if (flavor_equal(&sf, &sec->ps_flvr))
1396 CWARN("import %s->%s: changing flavor %s -> %s\n",
1397 imp->imp_obd->obd_name,
1398 obd_uuid2str(&conn->c_remote_uuid),
1399 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1400 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1402 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1403 SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1404 SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1405 SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1406 sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1410 CWARN("import %s->%s netid %x: select flavor %s\n",
1411 imp->imp_obd->obd_name,
1412 obd_uuid2str(&conn->c_remote_uuid),
1413 LNET_NIDNET(conn->c_self),
1414 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1417 mutex_down(&imp->imp_sec_mutex);
1419 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1421 sptlrpc_import_sec_install(imp, newsec);
1423 CERROR("import %s->%s: failed to create new sec\n",
1424 imp->imp_obd->obd_name,
1425 obd_uuid2str(&conn->c_remote_uuid));
1429 mutex_up(&imp->imp_sec_mutex);
1431 sptlrpc_sec_put(sec);
1435 void sptlrpc_import_sec_put(struct obd_import *imp)
1438 sptlrpc_sec_kill(imp->imp_sec);
1440 sptlrpc_sec_put(imp->imp_sec);
1441 imp->imp_sec = NULL;
1445 static void import_flush_ctx_common(struct obd_import *imp,
1446 uid_t uid, int grace, int force)
1448 struct ptlrpc_sec *sec;
1453 sec = sptlrpc_import_sec_ref(imp);
1457 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1458 sptlrpc_sec_put(sec);
1461 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1463 /* it's important to use grace mode, see explain in
1464 * sptlrpc_req_refresh_ctx() */
1465 import_flush_ctx_common(imp, 0, 1, 1);
1468 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1470 import_flush_ctx_common(imp, cfs_current()->uid, 1, 1);
1472 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1474 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1476 import_flush_ctx_common(imp, -1, 1, 1);
1478 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1481 * when complete successfully, req->rq_reqmsg should point to the
1484 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1486 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1487 struct ptlrpc_sec_policy *policy;
1491 LASSERT(atomic_read(&ctx->cc_refcount));
1492 LASSERT(ctx->cc_sec);
1493 LASSERT(ctx->cc_sec->ps_policy);
1494 LASSERT(req->rq_reqmsg == NULL);
1496 policy = ctx->cc_sec->ps_policy;
1497 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1499 LASSERT(req->rq_reqmsg);
1500 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1502 /* zeroing preallocated buffer */
1504 memset(req->rq_reqmsg, 0, msgsize);
1510 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1512 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1513 struct ptlrpc_sec_policy *policy;
1516 LASSERT(atomic_read(&ctx->cc_refcount));
1517 LASSERT(ctx->cc_sec);
1518 LASSERT(ctx->cc_sec->ps_policy);
1520 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1523 policy = ctx->cc_sec->ps_policy;
1524 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1528 * NOTE caller must guarantee the buffer size is enough for the enlargement
1530 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1531 int segment, int newsize)
1534 int oldsize, oldmsg_size, movesize;
1536 LASSERT(segment < msg->lm_bufcount);
1537 LASSERT(msg->lm_buflens[segment] <= newsize);
1539 if (msg->lm_buflens[segment] == newsize)
1542 /* nothing to do if we are enlarging the last segment */
1543 if (segment == msg->lm_bufcount - 1) {
1544 msg->lm_buflens[segment] = newsize;
1548 oldsize = msg->lm_buflens[segment];
1550 src = lustre_msg_buf(msg, segment + 1, 0);
1551 msg->lm_buflens[segment] = newsize;
1552 dst = lustre_msg_buf(msg, segment + 1, 0);
1553 msg->lm_buflens[segment] = oldsize;
1555 /* move from segment + 1 to end segment */
1556 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1557 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1558 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1559 LASSERT(movesize >= 0);
1562 memmove(dst, src, movesize);
1564 /* note we don't clear the ares where old data live, not secret */
1566 /* finally set new segment size */
1567 msg->lm_buflens[segment] = newsize;
1569 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1572 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1573 * will be preserved after enlargement. this must be called after rq_reqmsg has
1574 * been intialized at least.
1576 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1579 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1580 int segment, int newsize)
1582 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1583 struct ptlrpc_sec_cops *cops;
1584 struct lustre_msg *msg = req->rq_reqmsg;
1588 LASSERT(msg->lm_bufcount > segment);
1589 LASSERT(msg->lm_buflens[segment] <= newsize);
1591 if (msg->lm_buflens[segment] == newsize)
1594 cops = ctx->cc_sec->ps_policy->sp_cops;
1595 LASSERT(cops->enlarge_reqbuf);
1596 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1598 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1600 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1602 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1603 struct ptlrpc_sec_policy *policy;
1607 LASSERT(atomic_read(&ctx->cc_refcount));
1608 LASSERT(ctx->cc_sec);
1609 LASSERT(ctx->cc_sec->ps_policy);
1614 policy = ctx->cc_sec->ps_policy;
1615 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1618 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1620 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1621 struct ptlrpc_sec_policy *policy;
1625 LASSERT(atomic_read(&ctx->cc_refcount));
1626 LASSERT(ctx->cc_sec);
1627 LASSERT(ctx->cc_sec->ps_policy);
1629 if (req->rq_repbuf == NULL)
1631 LASSERT(req->rq_repbuf_len);
1633 policy = ctx->cc_sec->ps_policy;
1634 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1638 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1639 struct ptlrpc_cli_ctx *ctx)
1641 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1643 if (!policy->sp_cops->install_rctx)
1645 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1648 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1649 struct ptlrpc_svc_ctx *ctx)
1651 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1653 if (!policy->sp_sops->install_rctx)
1655 return policy->sp_sops->install_rctx(imp, ctx);
1658 /****************************************
1659 * server side security *
1660 ****************************************/
1662 static int flavor_allowed(struct sptlrpc_flavor *exp,
1663 struct ptlrpc_request *req)
1665 struct sptlrpc_flavor *flvr = &req->rq_flvr;
1667 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1670 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1671 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1672 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1673 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1679 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
1681 int sptlrpc_target_export_check(struct obd_export *exp,
1682 struct ptlrpc_request *req)
1684 struct sptlrpc_flavor flavor;
1689 /* client side export has no imp_reverse, skip
1690 * FIXME maybe we should check flavor this as well??? */
1691 if (exp->exp_imp_reverse == NULL)
1694 /* don't care about ctx fini rpc */
1695 if (req->rq_ctx_fini)
1698 spin_lock(&exp->exp_lock);
1700 /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1701 * the first req with the new flavor, then treat it as current flavor,
1702 * adapt reverse sec according to it.
1703 * note the first rpc with new flavor might not be with root ctx, in
1704 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1705 if (unlikely(exp->exp_flvr_changed) &&
1706 flavor_allowed(&exp->exp_flvr_old[1], req)) {
1707 /* make the new flavor as "current", and old ones as
1708 * about-to-expire */
1709 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1710 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1711 flavor = exp->exp_flvr_old[1];
1712 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1713 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1714 exp->exp_flvr_old[0] = exp->exp_flvr;
1715 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1716 EXP_FLVR_UPDATE_EXPIRE;
1717 exp->exp_flvr = flavor;
1719 /* flavor change finished */
1720 exp->exp_flvr_changed = 0;
1721 LASSERT(exp->exp_flvr_adapt == 1);
1723 /* if it's gss, we only interested in root ctx init */
1724 if (req->rq_auth_gss &&
1725 !(req->rq_ctx_init && (req->rq_auth_usr_root ||
1726 req->rq_auth_usr_mdt))) {
1727 spin_unlock(&exp->exp_lock);
1728 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d)\n",
1729 req->rq_auth_gss, req->rq_ctx_init,
1730 req->rq_auth_usr_root, req->rq_auth_usr_mdt);
1734 exp->exp_flvr_adapt = 0;
1735 spin_unlock(&exp->exp_lock);
1737 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1738 req->rq_svc_ctx, &flavor);
1741 /* if it equals to the current flavor, we accept it, but need to
1742 * dealing with reverse sec/ctx */
1743 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1744 /* most cases should return here, we only interested in
1745 * gss root ctx init */
1746 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1747 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt)) {
1748 spin_unlock(&exp->exp_lock);
1752 /* if flavor just changed, we should not proceed, just leave
1753 * it and current flavor will be discovered and replaced
1754 * shortly, and let _this_ rpc pass through */
1755 if (exp->exp_flvr_changed) {
1756 LASSERT(exp->exp_flvr_adapt);
1757 spin_unlock(&exp->exp_lock);
1761 if (exp->exp_flvr_adapt) {
1762 exp->exp_flvr_adapt = 0;
1763 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1764 exp, exp->exp_flvr.sf_rpc,
1765 exp->exp_flvr_old[0].sf_rpc,
1766 exp->exp_flvr_old[1].sf_rpc);
1767 flavor = exp->exp_flvr;
1768 spin_unlock(&exp->exp_lock);
1770 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1774 CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1775 "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1776 exp->exp_flvr_old[0].sf_rpc,
1777 exp->exp_flvr_old[1].sf_rpc);
1778 spin_unlock(&exp->exp_lock);
1780 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1785 if (exp->exp_flvr_expire[0]) {
1786 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1787 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1788 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1789 "middle one ("CFS_DURATION_T")\n", exp,
1790 exp->exp_flvr.sf_rpc,
1791 exp->exp_flvr_old[0].sf_rpc,
1792 exp->exp_flvr_old[1].sf_rpc,
1793 exp->exp_flvr_expire[0] -
1794 cfs_time_current_sec());
1795 spin_unlock(&exp->exp_lock);
1799 CDEBUG(D_SEC, "mark middle expired\n");
1800 exp->exp_flvr_expire[0] = 0;
1802 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1803 exp->exp_flvr.sf_rpc,
1804 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1805 req->rq_flvr.sf_rpc);
1808 /* now it doesn't match the current flavor, the only chance we can
1809 * accept it is match the old flavors which is not expired. */
1810 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1811 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1812 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1813 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1814 "oldest one ("CFS_DURATION_T")\n", exp,
1815 exp->exp_flvr.sf_rpc,
1816 exp->exp_flvr_old[0].sf_rpc,
1817 exp->exp_flvr_old[1].sf_rpc,
1818 exp->exp_flvr_expire[1] -
1819 cfs_time_current_sec());
1820 spin_unlock(&exp->exp_lock);
1824 CDEBUG(D_SEC, "mark oldest expired\n");
1825 exp->exp_flvr_expire[1] = 0;
1827 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1828 exp, exp->exp_flvr.sf_rpc,
1829 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1830 req->rq_flvr.sf_rpc);
1832 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1833 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1834 exp->exp_flvr_old[1].sf_rpc);
1837 spin_unlock(&exp->exp_lock);
1839 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u) with "
1840 "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1841 exp, exp->exp_obd->obd_name,
1842 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1843 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_flvr.sf_rpc,
1844 exp->exp_flvr.sf_rpc,
1845 exp->exp_flvr_old[0].sf_rpc,
1846 exp->exp_flvr_expire[0] ?
1847 (unsigned long) (exp->exp_flvr_expire[0] -
1848 cfs_time_current_sec()) : 0,
1849 exp->exp_flvr_old[1].sf_rpc,
1850 exp->exp_flvr_expire[1] ?
1851 (unsigned long) (exp->exp_flvr_expire[1] -
1852 cfs_time_current_sec()) : 0);
1855 EXPORT_SYMBOL(sptlrpc_target_export_check);
1857 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1858 struct sptlrpc_rule_set *rset)
1860 struct obd_export *exp;
1861 struct sptlrpc_flavor new_flvr;
1865 spin_lock(&obd->obd_dev_lock);
1867 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1868 if (exp->exp_connection == NULL)
1871 /* note if this export had just been updated flavor
1872 * (exp_flvr_changed == 1), this will override the
1874 spin_lock(&exp->exp_lock);
1875 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1876 exp->exp_connection->c_peer.nid,
1878 if (exp->exp_flvr_changed ||
1879 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1880 exp->exp_flvr_old[1] = new_flvr;
1881 exp->exp_flvr_expire[1] = 0;
1882 exp->exp_flvr_changed = 1;
1883 exp->exp_flvr_adapt = 1;
1885 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1886 exp, sptlrpc_part2name(exp->exp_sp_peer),
1887 exp->exp_flvr.sf_rpc,
1888 exp->exp_flvr_old[1].sf_rpc);
1890 spin_unlock(&exp->exp_lock);
1893 spin_unlock(&obd->obd_dev_lock);
1895 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1897 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1899 if (svc_rc == SECSVC_DROP)
1902 switch (req->rq_sp_from) {
1911 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
1915 if (!req->rq_auth_gss)
1918 if (unlikely(req->rq_sp_from == LUSTRE_SP_ANY)) {
1919 CERROR("not specific part\n");
1923 /* from MDT, must be authenticated as MDT */
1924 if (unlikely(req->rq_sp_from == LUSTRE_SP_MDT &&
1925 !req->rq_auth_usr_mdt)) {
1926 DEBUG_REQ(D_ERROR, req, "fake source MDT");
1930 /* from OST, must be callback to MDT and CLI, the reverse sec
1931 * was from mdt/root keytab, so it should be MDT or root FIXME */
1932 if (unlikely(req->rq_sp_from == LUSTRE_SP_OST &&
1933 !req->rq_auth_usr_mdt && !req->rq_auth_usr_root)) {
1934 DEBUG_REQ(D_ERROR, req, "fake source OST");
1941 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1943 struct ptlrpc_sec_policy *policy;
1944 struct lustre_msg *msg = req->rq_reqbuf;
1949 LASSERT(req->rq_reqmsg == NULL);
1950 LASSERT(req->rq_repmsg == NULL);
1951 LASSERT(req->rq_svc_ctx == NULL);
1953 req->rq_sp_from = LUSTRE_SP_ANY;
1954 req->rq_auth_uid = INVALID_UID;
1955 req->rq_auth_mapped_uid = INVALID_UID;
1957 if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1958 CERROR("request size %d too small\n", req->rq_reqdata_len);
1959 RETURN(SECSVC_DROP);
1963 * only expect v2 message.
1965 switch (msg->lm_magic) {
1966 case LUSTRE_MSG_MAGIC_V2:
1967 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
1969 case LUSTRE_MSG_MAGIC_V2_SWABBED:
1970 req->rq_flvr.sf_rpc = WIRE_FLVR(__swab32(msg->lm_secflvr));
1973 CERROR("invalid magic %x\n", msg->lm_magic);
1974 RETURN(SECSVC_DROP);
1977 /* unpack the wrapper message if the policy is not null */
1978 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1979 lustre_unpack_msg(msg, req->rq_reqdata_len)) {
1980 CERROR("invalid wrapper msg format\n");
1981 RETURN(SECSVC_DROP);
1984 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
1986 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
1987 RETURN(SECSVC_DROP);
1990 LASSERT(policy->sp_sops->accept);
1991 rc = policy->sp_sops->accept(req);
1993 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1994 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1995 sptlrpc_policy_put(policy);
1997 /* sanity check for the request source */
1998 rc = sptlrpc_svc_check_from(req, rc);
2002 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
2005 struct ptlrpc_sec_policy *policy;
2006 struct ptlrpc_reply_state *rs;
2010 LASSERT(req->rq_svc_ctx);
2011 LASSERT(req->rq_svc_ctx->sc_policy);
2013 policy = req->rq_svc_ctx->sc_policy;
2014 LASSERT(policy->sp_sops->alloc_rs);
2016 rc = policy->sp_sops->alloc_rs(req, msglen);
2017 if (unlikely(rc == -ENOMEM)) {
2018 /* failed alloc, try emergency pool */
2019 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
2023 req->rq_reply_state = rs;
2024 rc = policy->sp_sops->alloc_rs(req, msglen);
2026 lustre_put_emerg_rs(rs);
2027 req->rq_reply_state = NULL;
2032 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2037 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2039 struct ptlrpc_sec_policy *policy;
2043 LASSERT(req->rq_svc_ctx);
2044 LASSERT(req->rq_svc_ctx->sc_policy);
2046 policy = req->rq_svc_ctx->sc_policy;
2047 LASSERT(policy->sp_sops->authorize);
2049 rc = policy->sp_sops->authorize(req);
2050 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2055 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2057 struct ptlrpc_sec_policy *policy;
2058 unsigned int prealloc;
2061 LASSERT(rs->rs_svc_ctx);
2062 LASSERT(rs->rs_svc_ctx->sc_policy);
2064 policy = rs->rs_svc_ctx->sc_policy;
2065 LASSERT(policy->sp_sops->free_rs);
2067 prealloc = rs->rs_prealloc;
2068 policy->sp_sops->free_rs(rs);
2071 lustre_put_emerg_rs(rs);
2075 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2077 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2082 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2083 atomic_inc(&ctx->sc_refcount);
2086 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2088 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2093 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2094 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2095 if (ctx->sc_policy->sp_sops->free_ctx)
2096 ctx->sc_policy->sp_sops->free_ctx(ctx);
2098 req->rq_svc_ctx = NULL;
2101 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2103 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2108 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
2109 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2110 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2112 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2114 /****************************************
2116 ****************************************/
2118 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2119 struct ptlrpc_bulk_desc *desc)
2121 struct ptlrpc_cli_ctx *ctx;
2123 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2125 if (!req->rq_pack_bulk)
2128 ctx = req->rq_cli_ctx;
2129 if (ctx->cc_ops->wrap_bulk)
2130 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2133 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2136 * return nob of actual plain text size received, or error code.
2138 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2139 struct ptlrpc_bulk_desc *desc,
2142 struct ptlrpc_cli_ctx *ctx;
2145 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2147 if (!req->rq_pack_bulk)
2148 return desc->bd_nob_transferred;
2150 ctx = req->rq_cli_ctx;
2151 if (ctx->cc_ops->unwrap_bulk) {
2152 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2156 return desc->bd_nob_transferred;
2158 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2161 * return 0 for success or error code.
2163 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2164 struct ptlrpc_bulk_desc *desc)
2166 struct ptlrpc_cli_ctx *ctx;
2169 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2171 if (!req->rq_pack_bulk)
2174 ctx = req->rq_cli_ctx;
2175 if (ctx->cc_ops->unwrap_bulk) {
2176 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2182 * if everything is going right, nob should equals to nob_transferred.
2183 * in case of privacy mode, nob_transferred needs to be adjusted.
2185 if (desc->bd_nob != desc->bd_nob_transferred) {
2186 CERROR("nob %d doesn't match transferred nob %d",
2187 desc->bd_nob, desc->bd_nob_transferred);
2193 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2195 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2196 struct ptlrpc_bulk_desc *desc)
2198 struct ptlrpc_svc_ctx *ctx;
2200 LASSERT(req->rq_bulk_read);
2202 if (!req->rq_pack_bulk)
2205 ctx = req->rq_svc_ctx;
2206 if (ctx->sc_policy->sp_sops->wrap_bulk)
2207 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2211 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2213 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2214 struct ptlrpc_bulk_desc *desc)
2216 struct ptlrpc_svc_ctx *ctx;
2219 LASSERT(req->rq_bulk_write);
2222 * if it's in privacy mode, transferred should >= expected; otherwise
2223 * transferred should == expected.
2225 if (desc->bd_nob_transferred < desc->bd_nob ||
2226 (desc->bd_nob_transferred > desc->bd_nob &&
2227 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2228 SPTLRPC_BULK_SVC_PRIV)) {
2229 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2230 desc->bd_nob_transferred, desc->bd_nob);
2234 if (!req->rq_pack_bulk)
2237 ctx = req->rq_svc_ctx;
2238 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2239 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2241 CERROR("error unwrap bulk: %d\n", rc);
2244 /* return 0 to allow reply be sent */
2247 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2249 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2250 struct ptlrpc_bulk_desc *desc)
2252 struct ptlrpc_svc_ctx *ctx;
2254 LASSERT(req->rq_bulk_write);
2256 if (!req->rq_pack_bulk)
2259 ctx = req->rq_svc_ctx;
2260 if (ctx->sc_policy->sp_sops->prep_bulk)
2261 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2265 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2267 /****************************************
2268 * user descriptor helpers *
2269 ****************************************/
2271 int sptlrpc_current_user_desc_size(void)
2276 ngroups = current_ngroups;
2278 if (ngroups > LUSTRE_MAX_GROUPS)
2279 ngroups = LUSTRE_MAX_GROUPS;
2283 return sptlrpc_user_desc_size(ngroups);
2285 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2287 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2289 struct ptlrpc_user_desc *pud;
2291 pud = lustre_msg_buf(msg, offset, 0);
2293 pud->pud_uid = cfs_current()->uid;
2294 pud->pud_gid = cfs_current()->gid;
2295 pud->pud_fsuid = cfs_current()->fsuid;
2296 pud->pud_fsgid = cfs_current()->fsgid;
2297 pud->pud_cap = cfs_curproc_cap_pack();
2298 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2302 if (pud->pud_ngroups > current_ngroups)
2303 pud->pud_ngroups = current_ngroups;
2304 memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
2305 pud->pud_ngroups * sizeof(__u32));
2306 task_unlock(current);
2311 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2313 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
2315 struct ptlrpc_user_desc *pud;
2318 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2322 if (lustre_msg_swabbed(msg)) {
2323 __swab32s(&pud->pud_uid);
2324 __swab32s(&pud->pud_gid);
2325 __swab32s(&pud->pud_fsuid);
2326 __swab32s(&pud->pud_fsgid);
2327 __swab32s(&pud->pud_cap);
2328 __swab32s(&pud->pud_ngroups);
2331 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2332 CERROR("%u groups is too large\n", pud->pud_ngroups);
2336 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2337 msg->lm_buflens[offset]) {
2338 CERROR("%u groups are claimed but bufsize only %u\n",
2339 pud->pud_ngroups, msg->lm_buflens[offset]);
2343 if (lustre_msg_swabbed(msg)) {
2344 for (i = 0; i < pud->pud_ngroups; i++)
2345 __swab32s(&pud->pud_groups[i]);
2350 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2352 /****************************************
2354 ****************************************/
2356 const char * sec2target_str(struct ptlrpc_sec *sec)
2358 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2360 if (sec_is_reverse(sec))
2362 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2364 EXPORT_SYMBOL(sec2target_str);
2367 * return true if the bulk data is protected
2369 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2371 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2372 case SPTLRPC_BULK_SVC_INTG:
2373 case SPTLRPC_BULK_SVC_PRIV:
2379 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2381 /****************************************
2382 * crypto API helper/alloc blkciper *
2383 ****************************************/
2385 /****************************************
2386 * initialize/finalize *
2387 ****************************************/
2389 int __init sptlrpc_init(void)
2393 rwlock_init(&policy_lock);
2395 rc = sptlrpc_gc_init();
2399 rc = sptlrpc_conf_init();
2403 rc = sptlrpc_enc_pool_init();
2407 rc = sptlrpc_null_init();
2411 rc = sptlrpc_plain_init();
2415 rc = sptlrpc_lproc_init();
2422 sptlrpc_plain_fini();
2424 sptlrpc_null_fini();
2426 sptlrpc_enc_pool_fini();
2428 sptlrpc_conf_fini();
2435 void __exit sptlrpc_fini(void)
2437 sptlrpc_lproc_fini();
2438 sptlrpc_plain_fini();
2439 sptlrpc_null_fini();
2440 sptlrpc_enc_pool_fini();
2441 sptlrpc_conf_fini();