1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004-2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <libcfs/libcfs.h>
29 #include <liblustre.h>
30 #include <libcfs/list.h>
32 #include <linux/crypto.h>
36 #include <obd_class.h>
37 #include <obd_support.h>
38 #include <lustre_net.h>
39 #include <lustre_import.h>
40 #include <lustre_dlm.h>
41 #include <lustre_sec.h>
43 #include "ptlrpc_internal.h"
45 static void sptlrpc_sec_destroy(struct ptlrpc_sec *sec);
46 static int sptlrpc_sec_destroy_ctx(struct ptlrpc_sec *sec,
47 struct ptlrpc_cli_ctx *ctx);
48 static void sptlrpc_ctx_refresh(struct ptlrpc_cli_ctx *ctx);
50 /***********************************************
52 ***********************************************/
54 static rwlock_t policy_lock = RW_LOCK_UNLOCKED;
55 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
59 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
61 __u32 number = policy->sp_policy;
63 LASSERT(policy->sp_name);
64 LASSERT(policy->sp_cops);
65 LASSERT(policy->sp_sops);
67 if (number >= SPTLRPC_POLICY_MAX)
70 write_lock(&policy_lock);
71 if (unlikely(policies[number])) {
72 write_unlock(&policy_lock);
75 policies[number] = policy;
76 write_unlock(&policy_lock);
78 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
81 EXPORT_SYMBOL(sptlrpc_register_policy);
83 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
85 __u32 number = policy->sp_policy;
87 LASSERT(number < SPTLRPC_POLICY_MAX);
89 write_lock(&policy_lock);
90 if (unlikely(policies[number] == NULL)) {
91 write_unlock(&policy_lock);
92 CERROR("%s: already unregistered\n", policy->sp_name);
96 LASSERT(policies[number] == policy);
97 policies[number] = NULL;
98 write_unlock(&policy_lock);
100 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
103 EXPORT_SYMBOL(sptlrpc_unregister_policy);
106 struct ptlrpc_sec_policy * sptlrpc_flavor2policy(ptlrpc_sec_flavor_t flavor)
108 static DECLARE_MUTEX(load_mutex);
109 static atomic_t loaded = ATOMIC_INIT(0);
110 struct ptlrpc_sec_policy *policy;
111 __u32 number = SEC_FLAVOR_POLICY(flavor), flag = 0;
113 if (number >= SPTLRPC_POLICY_MAX)
117 read_lock(&policy_lock);
118 policy = policies[number];
119 if (policy && !try_module_get(policy->sp_owner))
122 flag = atomic_read(&loaded);
123 read_unlock(&policy_lock);
126 /* if failure, try to load gss module, once */
127 if (unlikely(policy == NULL) &&
128 number == SPTLRPC_POLICY_GSS && flag == 0) {
129 mutex_down(&load_mutex);
130 if (atomic_read(&loaded) == 0) {
131 if (request_module("ptlrpc_gss") != 0)
132 CERROR("Unable to load module ptlrpc_gss\n");
134 CWARN("module ptlrpc_gss loaded\n");
136 atomic_set(&loaded, 1);
138 mutex_up(&load_mutex);
147 ptlrpc_sec_flavor_t sptlrpc_name2flavor(const char *name)
149 if (!strcmp(name, "null"))
150 return SPTLRPC_FLVR_NULL;
151 if (!strcmp(name, "plain"))
152 return SPTLRPC_FLVR_PLAIN;
153 if (!strcmp(name, "krb5"))
154 return SPTLRPC_FLVR_KRB5;
155 if (!strcmp(name, "krb5i"))
156 return SPTLRPC_FLVR_KRB5I;
157 if (!strcmp(name, "krb5p"))
158 return SPTLRPC_FLVR_KRB5P;
160 return SPTLRPC_FLVR_INVALID;
162 EXPORT_SYMBOL(sptlrpc_name2flavor);
164 char *sptlrpc_flavor2name(ptlrpc_sec_flavor_t flavor)
167 case SPTLRPC_FLVR_NULL:
169 case SPTLRPC_FLVR_PLAIN:
171 case SPTLRPC_FLVR_KRB5:
173 case SPTLRPC_FLVR_KRB5I:
175 case SPTLRPC_FLVR_KRB5P:
178 CERROR("invalid flavor 0x%x(p%u,s%u,v%u)\n", flavor,
179 SEC_FLAVOR_POLICY(flavor), SEC_FLAVOR_SUBPOLICY(flavor),
180 SEC_FLAVOR_SVC(flavor));
184 EXPORT_SYMBOL(sptlrpc_flavor2name);
186 /***********************************************
190 ***********************************************/
193 unsigned long ctx_status(struct ptlrpc_cli_ctx *ctx)
196 return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
200 int ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
202 return (ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
206 int ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
208 return (ctx_status(ctx) != 0);
212 int ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
215 return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
219 int ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
222 return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
226 int ctx_expire(struct ptlrpc_cli_ctx *ctx)
228 LASSERT(atomic_read(&ctx->cc_refcount));
230 if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
231 cfs_time_t now = cfs_time_current_sec();
234 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
236 if (ctx->cc_expire && cfs_time_aftereq(now, ctx->cc_expire))
237 CWARN("ctx %p(%u->%s): get expired (%lds exceeds)\n",
238 ctx, ctx->cc_vcred.vc_uid,
239 sec2target_str(ctx->cc_sec),
240 cfs_time_sub(now, ctx->cc_expire));
242 CWARN("ctx %p(%u->%s): force to die (%lds remains)\n",
243 ctx, ctx->cc_vcred.vc_uid,
244 sec2target_str(ctx->cc_sec),
245 ctx->cc_expire == 0 ? 0 :
246 cfs_time_sub(ctx->cc_expire, now));
254 void ctx_enhash(struct ptlrpc_cli_ctx *ctx, struct hlist_head *hash)
256 set_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags);
257 atomic_inc(&ctx->cc_refcount);
258 hlist_add_head(&ctx->cc_hash, hash);
262 void ctx_unhash(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
264 LASSERT_SPIN_LOCKED(&ctx->cc_sec->ps_lock);
265 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
266 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags));
267 LASSERT(!hlist_unhashed(&ctx->cc_hash));
269 clear_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags);
271 if (atomic_dec_and_test(&ctx->cc_refcount)) {
272 __hlist_del(&ctx->cc_hash);
273 hlist_add_head(&ctx->cc_hash, freelist);
275 hlist_del_init(&ctx->cc_hash);
279 * return 1 if the context is dead.
282 int ctx_check_death(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
284 if (unlikely(ctx_is_dead(ctx)))
287 /* expire is 0 means never expire. a newly created gss context
288 * which during upcall also has 0 expiration
291 if (ctx->cc_expire == 0)
294 /* check real expiration */
296 if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
303 ctx_unhash(ctx, freelist);
309 int ctx_check_death_locked(struct ptlrpc_cli_ctx *ctx,
310 struct hlist_head *freelist)
312 LASSERT(ctx->cc_sec);
313 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
314 LASSERT_SPIN_LOCKED(&ctx->cc_sec->ps_lock);
315 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags));
317 return ctx_check_death(ctx, freelist);
321 int ctx_check_uptodate(struct ptlrpc_cli_ctx *ctx)
323 LASSERT(ctx->cc_sec);
324 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
326 if (!ctx_check_death(ctx, NULL) && ctx_is_uptodate(ctx))
332 int ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
334 /* a little bit optimization for null policy */
335 if (!ctx->cc_ops->match)
338 return ctx->cc_ops->match(ctx, vcred);
342 void ctx_list_destroy(struct hlist_head *head)
344 struct ptlrpc_cli_ctx *ctx;
346 while (!hlist_empty(head)) {
347 ctx = hlist_entry(head->first, struct ptlrpc_cli_ctx, cc_hash);
349 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
350 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags) == 0);
352 hlist_del_init(&ctx->cc_hash);
353 sptlrpc_sec_destroy_ctx(ctx->cc_sec, ctx);
358 void ctx_cache_gc(struct ptlrpc_sec *sec, struct hlist_head *freelist)
360 struct ptlrpc_cli_ctx *ctx;
361 struct hlist_node *pos, *next;
365 CDEBUG(D_SEC, "do gc on sec %s@%p\n", sec->ps_policy->sp_name, sec);
367 for (i = 0; i < sec->ps_ccache_size; i++) {
368 hlist_for_each_entry_safe(ctx, pos, next,
369 &sec->ps_ccache[i], cc_hash)
370 ctx_check_death_locked(ctx, freelist);
373 sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
378 * @uid: which user. "-1" means flush all.
379 * @grace: mark context DEAD, allow graceful destroy like notify
381 * @force: also flush busy entries.
383 * return the number of busy context encountered.
385 * In any cases, never touch "eternal" contexts.
388 int ctx_cache_flush(struct ptlrpc_sec *sec, uid_t uid, int grace, int force)
390 struct ptlrpc_cli_ctx *ctx;
391 struct hlist_node *pos, *next;
392 HLIST_HEAD(freelist);
396 might_sleep_if(grace);
398 spin_lock(&sec->ps_lock);
399 for (i = 0; i < sec->ps_ccache_size; i++) {
400 hlist_for_each_entry_safe(ctx, pos, next,
401 &sec->ps_ccache[i], cc_hash) {
402 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
404 if (ctx_is_eternal(ctx))
406 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
409 if (atomic_read(&ctx->cc_refcount) > 1) {
414 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
416 atomic_read(&ctx->cc_refcount),
417 ctx, ctx->cc_vcred.vc_uid,
418 sec2target_str(ctx->cc_sec), grace);
420 ctx_unhash(ctx, &freelist);
422 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
424 clear_bit(PTLRPC_CTX_UPTODATE_BIT,
428 spin_unlock(&sec->ps_lock);
430 ctx_list_destroy(&freelist);
435 unsigned int ctx_hash_index(struct ptlrpc_sec *sec, __u64 key)
437 return (unsigned int) (key & (sec->ps_ccache_size - 1));
441 * return matched context. If it's a newly created one, we also give the
442 * first push to refresh. return NULL if error happens.
445 struct ptlrpc_cli_ctx * ctx_cache_lookup(struct ptlrpc_sec *sec,
446 struct vfs_cred *vcred,
447 int create, int remove_dead)
449 struct ptlrpc_cli_ctx *ctx = NULL, *new = NULL;
450 struct hlist_head *hash_head;
451 struct hlist_node *pos, *next;
452 HLIST_HEAD(freelist);
453 unsigned int hash, gc = 0, found = 0;
458 hash = ctx_hash_index(sec, (__u64) vcred->vc_uid);
459 LASSERT(hash < sec->ps_ccache_size);
460 hash_head = &sec->ps_ccache[hash];
463 spin_lock(&sec->ps_lock);
465 /* gc_next == 0 means never do gc */
466 if (remove_dead && sec->ps_gc_next &&
467 cfs_time_after(cfs_time_current_sec(), sec->ps_gc_next)) {
468 ctx_cache_gc(sec, &freelist);
472 hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_hash) {
474 ctx_check_death_locked(ctx, remove_dead ? &freelist : NULL))
477 if (ctx_match(ctx, vcred)) {
484 if (new && new != ctx) {
485 /* lost the race, just free it */
486 hlist_add_head(&new->cc_hash, &freelist);
490 /* hot node, move to head */
491 if (hash_head->first != &ctx->cc_hash) {
492 __hlist_del(&ctx->cc_hash);
493 hlist_add_head(&ctx->cc_hash, hash_head);
496 /* don't allocate for reverse sec */
497 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
498 spin_unlock(&sec->ps_lock);
503 ctx_enhash(new, hash_head);
506 spin_unlock(&sec->ps_lock);
507 new = sec->ps_policy->sp_cops->create_ctx(sec, vcred);
509 atomic_inc(&sec->ps_busy);
518 atomic_inc(&ctx->cc_refcount);
520 spin_unlock(&sec->ps_lock);
522 /* the allocator of the context must give the first push to refresh */
525 sptlrpc_ctx_refresh(new);
528 ctx_list_destroy(&freelist);
533 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
535 struct vfs_cred vcred;
536 int create = 1, remove_dead = 1;
540 if (sec->ps_flags & (PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY)) {
543 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
548 vcred.vc_uid = cfs_current()->uid;
549 vcred.vc_gid = cfs_current()->gid;
552 if (sec->ps_policy->sp_cops->lookup_ctx)
553 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred);
555 return ctx_cache_lookup(sec, &vcred, create, remove_dead);
558 /**************************************************
559 * client context APIs *
560 **************************************************/
563 void sptlrpc_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
565 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
567 if (!ctx_is_refreshed(ctx) && ctx->cc_ops->refresh)
568 ctx->cc_ops->refresh(ctx);
571 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx)
573 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
574 atomic_inc(&ctx->cc_refcount);
577 EXPORT_SYMBOL(sptlrpc_ctx_get);
579 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
581 struct ptlrpc_sec *sec = ctx->cc_sec;
584 LASSERT(atomic_read(&ctx->cc_refcount));
586 if (!atomic_dec_and_test(&ctx->cc_refcount))
589 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags) == 0);
590 LASSERT(hlist_unhashed(&ctx->cc_hash));
592 /* if required async, we must clear the UPTODATE bit to prevent extra
593 * rpcs during destroy procedure.
596 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
598 /* destroy this context */
599 if (!sptlrpc_sec_destroy_ctx(sec, ctx))
602 CWARN("%s@%p: put last ctx, also destroy the sec\n",
603 sec->ps_policy->sp_name, sec);
605 sptlrpc_sec_destroy(sec);
607 EXPORT_SYMBOL(sptlrpc_ctx_put);
610 * mark a ctx as DEAD, and pull it out from hash table.
612 * NOTE: the caller must hold at least 1 ref on the ctx.
614 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx)
616 LASSERT(ctx->cc_sec);
617 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
621 spin_lock(&ctx->cc_sec->ps_lock);
623 if (test_and_clear_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags)) {
624 LASSERT(!hlist_unhashed(&ctx->cc_hash));
625 LASSERT(atomic_read(&ctx->cc_refcount) > 1);
627 hlist_del_init(&ctx->cc_hash);
628 if (atomic_dec_and_test(&ctx->cc_refcount))
632 spin_unlock(&ctx->cc_sec->ps_lock);
634 EXPORT_SYMBOL(sptlrpc_ctx_expire);
636 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new)
638 struct ptlrpc_cli_ctx *ctx;
639 struct hlist_node *pos, *next;
640 HLIST_HEAD(freelist);
644 hash = ctx_hash_index(sec, (__u64) new->cc_vcred.vc_uid);
645 LASSERT(hash < sec->ps_ccache_size);
647 spin_lock(&sec->ps_lock);
649 hlist_for_each_entry_safe(ctx, pos, next,
650 &sec->ps_ccache[hash], cc_hash) {
651 if (!ctx_match(ctx, &new->cc_vcred))
655 ctx_unhash(ctx, &freelist);
659 ctx_enhash(new, &sec->ps_ccache[hash]);
660 atomic_inc(&sec->ps_busy);
662 spin_unlock(&sec->ps_lock);
664 ctx_list_destroy(&freelist);
667 EXPORT_SYMBOL(sptlrpc_ctx_replace);
669 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
671 struct obd_import *imp = req->rq_import;
674 LASSERT(!req->rq_cli_ctx);
677 if (imp->imp_sec == NULL) {
678 CERROR("import %p (%s) with no sec pointer\n",
679 imp, ptlrpc_import_state_name(imp->imp_state));
683 req->rq_cli_ctx = get_my_ctx(imp->imp_sec);
685 if (!req->rq_cli_ctx) {
686 CERROR("req %p: fail to get context from cache\n", req);
693 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
695 struct ptlrpc_request *req, *next;
697 spin_lock(&ctx->cc_lock);
698 list_for_each_entry_safe(req, next, &ctx->cc_req_list, rq_ctx_chain) {
699 list_del_init(&req->rq_ctx_chain);
700 ptlrpc_wake_client_req(req);
702 spin_unlock(&ctx->cc_lock);
704 EXPORT_SYMBOL(sptlrpc_ctx_wakeup);
706 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
708 LASSERT(ctx->cc_ops);
710 if (ctx->cc_ops->display == NULL)
713 return ctx->cc_ops->display(ctx, buf, bufsize);
716 void sptlrpc_req_put_ctx(struct ptlrpc_request *req)
721 LASSERT(req->rq_cli_ctx);
723 /* request might be asked to release earlier while still
724 * in the context waiting list.
726 if (!list_empty(&req->rq_ctx_chain)) {
727 spin_lock(&req->rq_cli_ctx->cc_lock);
728 list_del_init(&req->rq_ctx_chain);
729 spin_unlock(&req->rq_cli_ctx->cc_lock);
732 /* this could be called with spinlock hold, use async mode */
733 sptlrpc_ctx_put(req->rq_cli_ctx, 0);
734 req->rq_cli_ctx = NULL;
739 * request must have a context. if failed to get new context,
740 * just restore the old one
742 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
744 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
749 LASSERT(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags));
751 /* make sure not on context waiting list */
752 spin_lock(&ctx->cc_lock);
753 list_del_init(&req->rq_ctx_chain);
754 spin_unlock(&ctx->cc_lock);
756 sptlrpc_ctx_get(ctx);
757 sptlrpc_req_put_ctx(req);
758 rc = sptlrpc_req_get_ctx(req);
760 LASSERT(req->rq_cli_ctx);
761 LASSERT(req->rq_cli_ctx != ctx);
762 sptlrpc_ctx_put(ctx, 1);
764 LASSERT(!req->rq_cli_ctx);
765 req->rq_cli_ctx = ctx;
769 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
772 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
775 if (ctx_is_refreshed(ctx))
781 int ctx_refresh_timeout(void *data)
783 struct ptlrpc_request *req = data;
786 /* conn_cnt is needed in expire_one_request */
787 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
789 rc = ptlrpc_expire_one_request(req);
790 /* if we started recovery, we should mark this ctx dead; otherwise
791 * in case of lgssd died nobody would retire this ctx, following
792 * connecting will still find the same ctx thus cause deadlock.
793 * there's an assumption that expire time of the request should be
794 * later than the context refresh expire time.
797 ctx_expire(req->rq_cli_ctx);
802 void ctx_refresh_interrupt(void *data)
808 * the status of context could be subject to be changed by other threads at any
809 * time. we allow this race. but once we return with 0, the caller will
810 * suppose it's uptodated and keep using it until the affected rpc is done.
814 * = 0 - wait until success or fatal error occur
815 * > 0 - timeout value
817 * return 0 only if the context is uptodated.
819 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
821 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
822 struct l_wait_info lwi;
829 if (ctx_is_eternal(ctx) || req->rq_ctx_init || req->rq_ctx_fini)
832 /* reverse ctxs, don't refresh */
833 if (ctx->cc_sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
836 spin_lock(&ctx->cc_lock);
838 if (ctx_check_uptodate(ctx)) {
839 if (!list_empty(&req->rq_ctx_chain))
840 list_del_init(&req->rq_ctx_chain);
841 spin_unlock(&ctx->cc_lock);
845 if (test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags)) {
847 if (!list_empty(&req->rq_ctx_chain))
848 list_del_init(&req->rq_ctx_chain);
849 spin_unlock(&ctx->cc_lock);
853 /* This is subtle. For resent message we have to keep original
854 * context to survive following situation:
855 * 1. the request sent to server
856 * 2. recovery was kick start
857 * 3. recovery finished, the request marked as resent
858 * 4. resend the request
859 * 5. old reply from server received (because xid is the same)
860 * 6. verify reply (has to be success)
861 * 7. new reply from server received, lnet drop it
863 * Note we can't simply change xid for resent request because
864 * server reply on it for reply reconstruction.
866 * Commonly the original context should be uptodate because we
867 * have a expiry nice time; And server will keep their half part
868 * context because we at least hold a ref of old context which
869 * prevent the context detroy RPC be sent. So server still can
870 * accept the request and finish RPC. Two cases:
871 * 1. If server side context has been trimed, a NO_CONTEXT will
872 * be returned, gss_cli_ctx_verify/unseal will switch to new
874 * 2. Current context never be refreshed, then we are fine: we
875 * never really send request with old context before.
877 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
879 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
880 if (!list_empty(&req->rq_ctx_chain))
881 list_del_init(&req->rq_ctx_chain);
882 spin_unlock(&ctx->cc_lock);
886 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
887 spin_unlock(&ctx->cc_lock);
889 /* don't have to, but we don't want to release it too soon */
890 sptlrpc_ctx_get(ctx);
892 rc = sptlrpc_req_replace_dead_ctx(req);
894 LASSERT(ctx == req->rq_cli_ctx);
895 CERROR("req %p: failed to replace dead ctx %p\n",
898 LASSERT(list_empty(&req->rq_ctx_chain));
899 sptlrpc_ctx_put(ctx, 1);
903 LASSERT(ctx != req->rq_cli_ctx);
904 CWARN("req %p: replace dead ctx %p(%u->%s) => %p\n",
905 req, ctx, ctx->cc_vcred.vc_uid,
906 sec2target_str(ctx->cc_sec), req->rq_cli_ctx);
908 sptlrpc_ctx_put(ctx, 1);
909 ctx = req->rq_cli_ctx;
910 LASSERT(list_empty(&req->rq_ctx_chain));
912 spin_lock(&ctx->cc_lock);
916 /* Now we're sure this context is during upcall, add myself into
919 if (list_empty(&req->rq_ctx_chain))
920 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
922 spin_unlock(&ctx->cc_lock);
925 RETURN(-EWOULDBLOCK);
928 /* Clear any flags that may be present from previous sends */
929 LASSERT(req->rq_receiving_reply == 0);
930 spin_lock(&req->rq_lock);
932 req->rq_timedout = 0;
935 spin_unlock(&req->rq_lock);
937 lwi = LWI_TIMEOUT_INTR(timeout == 0 ? LONG_MAX : timeout * HZ,
938 ctx_refresh_timeout, ctx_refresh_interrupt, req);
939 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
941 spin_lock(&ctx->cc_lock);
942 /* five cases we are here:
943 * 1. successfully refreshed;
944 * 2. someone else mark this ctx dead by force;
946 * 4. timedout, and we don't want recover from the failure;
947 * 5. timedout, and waked up upon recovery finished;
949 if (!ctx_is_refreshed(ctx)) {
950 /* timed out or interruptted */
951 list_del_init(&req->rq_ctx_chain);
952 spin_unlock(&ctx->cc_lock);
961 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
963 struct sec_flavor_config *conf;
965 LASSERT(req->rq_import);
966 LASSERT(req->rq_import->imp_sec);
967 LASSERT(req->rq_cli_ctx);
968 LASSERT(req->rq_cli_ctx->cc_sec);
969 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
971 /* special security flags accoding to opcode */
974 req->rq_bulk_read = 1;
977 req->rq_bulk_write = 1;
980 req->rq_ctx_init = 1;
983 req->rq_ctx_fini = 1;
987 req->rq_sec_flavor = req->rq_cli_ctx->cc_sec->ps_flavor;
989 /* force SVC_NONE for context initiation rpc, SVC_AUTH for context
992 if (unlikely(req->rq_ctx_init)) {
993 req->rq_sec_flavor = SEC_MAKE_RPC_FLAVOR(
994 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
995 SEC_FLAVOR_SUBPOLICY(req->rq_sec_flavor),
996 SEC_FLAVOR_SVC(SPTLRPC_SVC_NONE));
997 } else if (unlikely(req->rq_ctx_fini)) {
998 req->rq_sec_flavor = SEC_MAKE_RPC_FLAVOR(
999 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
1000 SEC_FLAVOR_SUBPOLICY(req->rq_sec_flavor),
1001 SEC_FLAVOR_SVC(SPTLRPC_SVC_AUTH));
1004 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1006 /* user descriptor flag, except ROOTONLY which don't need, and
1007 * null security which can't
1009 if ((conf->sfc_flags & PTLRPC_SEC_FL_ROOTONLY) == 0 &&
1010 req->rq_sec_flavor != SPTLRPC_FLVR_NULL)
1011 req->rq_sec_flavor |= SEC_FLAVOR_FL_USER;
1013 /* bulk security flag */
1014 if ((req->rq_bulk_read || req->rq_bulk_write) &&
1015 (conf->sfc_bulk_priv != BULK_PRIV_ALG_NULL ||
1016 conf->sfc_bulk_csum != BULK_CSUM_ALG_NULL))
1017 req->rq_sec_flavor |= SEC_FLAVOR_FL_BULK;
1020 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
1022 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) != SPTLRPC_SVC_PRIV)
1025 LASSERT(req->rq_clrbuf);
1026 if (req->rq_pool || !req->rq_reqbuf)
1029 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1030 req->rq_reqbuf = NULL;
1031 req->rq_reqbuf_len = 0;
1035 * check whether current user have valid context for an import or not.
1036 * might repeatedly try in case of non-fatal errors.
1037 * return 0 on success, < 0 on failure
1039 int sptlrpc_import_check_ctx(struct obd_import *imp)
1041 struct ptlrpc_cli_ctx *ctx;
1042 struct ptlrpc_request *req = NULL;
1048 ctx = get_my_ctx(imp->imp_sec);
1052 if (ctx_is_eternal(ctx)) {
1053 sptlrpc_ctx_put(ctx, 1);
1061 spin_lock_init(&req->rq_lock);
1062 atomic_set(&req->rq_refcount, 10000);
1063 INIT_LIST_HEAD(&req->rq_ctx_chain);
1064 init_waitqueue_head(&req->rq_reply_waitq);
1065 req->rq_import = imp;
1066 req->rq_cli_ctx = ctx;
1068 rc = sptlrpc_req_refresh_ctx(req, 0);
1069 LASSERT(list_empty(&req->rq_ctx_chain));
1070 sptlrpc_ctx_put(req->rq_cli_ctx, 1);
1076 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1078 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1083 LASSERT(ctx->cc_sec);
1084 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1086 /* we wrap bulk request here because now we can be sure
1087 * the context is uptodate.
1090 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1095 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
1096 case SPTLRPC_SVC_NONE:
1097 case SPTLRPC_SVC_AUTH:
1098 LASSERT(ctx->cc_ops->sign);
1099 rc = ctx->cc_ops->sign(ctx, req);
1101 case SPTLRPC_SVC_PRIV:
1102 LASSERT(ctx->cc_ops->seal);
1103 rc = ctx->cc_ops->seal(ctx, req);
1110 LASSERT(req->rq_reqdata_len);
1111 LASSERT(req->rq_reqdata_len % 8 == 0);
1112 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1119 * rq_nob_received is the actual received data length
1121 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1123 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1128 LASSERT(ctx->cc_sec);
1129 LASSERT(ctx->cc_ops);
1130 LASSERT(req->rq_repbuf);
1132 req->rq_repdata_len = req->rq_nob_received;
1134 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1135 CERROR("replied data length %d too small\n",
1136 req->rq_nob_received);
1140 if (req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V1 ||
1141 req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) {
1142 /* it's must be null flavor, so our requets also should be
1144 if (SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1145 SPTLRPC_POLICY_NULL) {
1146 CERROR("request flavor is %x but reply with null\n",
1147 req->rq_sec_flavor);
1152 ptlrpc_sec_flavor_t tmpf = req->rq_repbuf->lm_secflvr;
1154 if (req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1157 if (SEC_FLAVOR_POLICY(tmpf) !=
1158 SEC_FLAVOR_POLICY(req->rq_sec_flavor)) {
1159 CERROR("request policy %u while reply with %d\n",
1160 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
1161 SEC_FLAVOR_POLICY(tmpf));
1165 if ((SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1166 SPTLRPC_POLICY_NULL) &&
1167 lustre_unpack_msg(req->rq_repbuf, req->rq_nob_received))
1171 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
1172 case SPTLRPC_SVC_NONE:
1173 case SPTLRPC_SVC_AUTH:
1174 LASSERT(ctx->cc_ops->verify);
1175 rc = ctx->cc_ops->verify(ctx, req);
1177 case SPTLRPC_SVC_PRIV:
1178 LASSERT(ctx->cc_ops->unseal);
1179 rc = ctx->cc_ops->unseal(ctx, req);
1185 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1189 /**************************************************
1191 **************************************************/
1194 * let policy module to determine whether take refrence of
1198 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1199 struct ptlrpc_svc_ctx *ctx,
1201 unsigned long flags)
1203 struct ptlrpc_sec_policy *policy;
1204 struct ptlrpc_sec *sec;
1207 flavor = SEC_FLAVOR_RPC(flavor);
1210 LASSERT(imp->imp_dlm_fake == 1);
1212 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1213 imp->imp_obd->obd_type->typ_name,
1214 imp->imp_obd->obd_name,
1215 sptlrpc_flavor2name(flavor));
1217 policy = sptlrpc_policy_get(ctx->sc_policy);
1218 flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1220 LASSERT(imp->imp_dlm_fake == 0);
1222 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1223 imp->imp_obd->obd_type->typ_name,
1224 imp->imp_obd->obd_name,
1225 sptlrpc_flavor2name(flavor));
1227 policy = sptlrpc_flavor2policy(flavor);
1229 CERROR("invalid flavor 0x%x\n", flavor);
1234 sec = policy->sp_cops->create_sec(imp, ctx, flavor, flags);
1236 atomic_inc(&sec->ps_refcount);
1238 /* take 1 busy count on behalf of sec itself,
1239 * balanced in sptlrpc_set_put()
1241 atomic_inc(&sec->ps_busy);
1243 sptlrpc_policy_put(policy);
1249 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1251 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1254 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1255 LASSERT(atomic_read(&sec->ps_busy) == 0);
1256 LASSERT(policy->sp_cops->destroy_sec);
1258 policy->sp_cops->destroy_sec(sec);
1259 sptlrpc_policy_put(policy);
1263 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1265 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1267 if (!atomic_dec_and_test(&sec->ps_refcount)) {
1268 sptlrpc_policy_put(policy);
1272 ctx_cache_flush(sec, -1, 1, 1);
1274 if (atomic_dec_and_test(&sec->ps_busy))
1275 sptlrpc_sec_destroy(sec);
1277 CWARN("delay to destroy %s@%p: busy contexts\n",
1278 policy->sp_name, sec);
1282 * return 1 means we should also destroy the sec structure.
1286 int sptlrpc_sec_destroy_ctx(struct ptlrpc_sec *sec,
1287 struct ptlrpc_cli_ctx *ctx)
1289 LASSERT(sec == ctx->cc_sec);
1290 LASSERT(atomic_read(&sec->ps_busy));
1291 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1292 LASSERT(hlist_unhashed(&ctx->cc_hash));
1293 LASSERT(list_empty(&ctx->cc_req_list));
1294 LASSERT(sec->ps_policy->sp_cops->destroy_ctx);
1296 sec->ps_policy->sp_cops->destroy_ctx(sec, ctx);
1298 if (atomic_dec_and_test(&sec->ps_busy)) {
1299 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1307 * when complete successfully, req->rq_reqmsg should point to the
1310 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1312 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1313 struct ptlrpc_sec_policy *policy;
1317 LASSERT(atomic_read(&ctx->cc_refcount));
1318 LASSERT(ctx->cc_sec);
1319 LASSERT(ctx->cc_sec->ps_policy);
1320 LASSERT(req->rq_reqmsg == NULL);
1322 policy = ctx->cc_sec->ps_policy;
1323 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1325 LASSERT(req->rq_reqmsg);
1326 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1328 /* zeroing preallocated buffer */
1330 memset(req->rq_reqmsg, 0, msgsize);
1336 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1338 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1339 struct ptlrpc_sec_policy *policy;
1342 LASSERT(atomic_read(&ctx->cc_refcount));
1343 LASSERT(ctx->cc_sec);
1344 LASSERT(ctx->cc_sec->ps_policy);
1345 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1347 policy = ctx->cc_sec->ps_policy;
1348 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1352 * NOTE caller must guarantee the buffer size is enough for the enlargement
1354 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1355 int segment, int newsize)
1358 int oldsize, oldmsg_size, movesize;
1360 LASSERT(segment < msg->lm_bufcount);
1361 LASSERT(msg->lm_buflens[segment] <= newsize);
1363 if (msg->lm_buflens[segment] == newsize)
1366 /* nothing to do if we are enlarging the last segment */
1367 if (segment == msg->lm_bufcount - 1) {
1368 msg->lm_buflens[segment] = newsize;
1372 oldsize = msg->lm_buflens[segment];
1374 src = lustre_msg_buf(msg, segment + 1, 0);
1375 msg->lm_buflens[segment] = newsize;
1376 dst = lustre_msg_buf(msg, segment + 1, 0);
1377 msg->lm_buflens[segment] = oldsize;
1379 /* move from segment + 1 to end segment */
1380 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1381 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1382 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1383 LASSERT(movesize >= 0);
1386 memmove(dst, src, movesize);
1388 /* note we don't clear the ares where old data live, not secret */
1390 /* finally set new segment size */
1391 msg->lm_buflens[segment] = newsize;
1393 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1396 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1397 * will be preserved after enlargement. this must be called after rq_reqmsg has
1398 * been intialized at least.
1400 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1403 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1404 int segment, int newsize)
1406 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1407 struct ptlrpc_sec_cops *cops;
1408 struct lustre_msg *msg = req->rq_reqmsg;
1412 LASSERT(msg->lm_bufcount > segment);
1413 LASSERT(msg->lm_buflens[segment] <= newsize);
1415 if (msg->lm_buflens[segment] == newsize)
1418 cops = ctx->cc_sec->ps_policy->sp_cops;
1419 LASSERT(cops->enlarge_reqbuf);
1420 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1422 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1424 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1426 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1427 struct ptlrpc_sec_policy *policy;
1431 LASSERT(atomic_read(&ctx->cc_refcount));
1432 LASSERT(ctx->cc_sec);
1433 LASSERT(ctx->cc_sec->ps_policy);
1438 policy = ctx->cc_sec->ps_policy;
1439 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1442 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1444 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1445 struct ptlrpc_sec_policy *policy;
1449 LASSERT(atomic_read(&ctx->cc_refcount));
1450 LASSERT(ctx->cc_sec);
1451 LASSERT(ctx->cc_sec->ps_policy);
1452 LASSERT(req->rq_repbuf);
1454 policy = ctx->cc_sec->ps_policy;
1455 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1459 int sptlrpc_import_get_sec(struct obd_import *imp,
1460 struct ptlrpc_svc_ctx *ctx,
1462 unsigned long flags)
1464 struct obd_device *obd = imp->imp_obd;
1468 LASSERT(obd->obd_type);
1470 /* old sec might be still there in reconnecting */
1474 imp->imp_sec = sptlrpc_sec_create(imp, ctx, flavor, flags);
1481 void sptlrpc_import_put_sec(struct obd_import *imp)
1483 if (imp->imp_sec == NULL)
1486 sptlrpc_sec_put(imp->imp_sec);
1487 imp->imp_sec = NULL;
1490 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1492 if (imp == NULL || imp->imp_sec == NULL)
1495 /* use 'grace' mode, it's crutial see explain in
1496 * sptlrpc_req_refresh_ctx()
1498 ctx_cache_flush(imp->imp_sec, 0, 1, 1);
1501 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1503 if (imp == NULL || imp->imp_sec == NULL)
1506 ctx_cache_flush(imp->imp_sec, cfs_current()->uid, 1, 1);
1508 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1510 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1512 if (imp == NULL || imp->imp_sec == NULL)
1515 ctx_cache_flush(imp->imp_sec, -1, 0, 1);
1517 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1519 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1520 struct ptlrpc_cli_ctx *ctx)
1522 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1524 if (!policy->sp_cops->install_rctx)
1526 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1529 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1530 struct ptlrpc_svc_ctx *ctx)
1532 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1534 if (!policy->sp_sops->install_rctx)
1536 return policy->sp_sops->install_rctx(imp, ctx);
1539 /****************************************
1540 * server side security *
1541 ****************************************/
1543 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1545 struct ptlrpc_sec_policy *policy;
1546 struct lustre_msg *msg = req->rq_reqbuf;
1551 LASSERT(req->rq_reqmsg == NULL);
1552 LASSERT(req->rq_repmsg == NULL);
1555 * in any case we avoid to call unpack_msg() for request of null flavor
1556 * which will later be done by ptlrpc_server_handle_request().
1558 if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1559 CERROR("request size %d too small\n", req->rq_reqdata_len);
1560 RETURN(SECSVC_DROP);
1563 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V1 ||
1564 msg->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) {
1565 req->rq_sec_flavor = SPTLRPC_FLVR_NULL;
1567 req->rq_sec_flavor = msg->lm_secflvr;
1569 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1570 __swab32s(&req->rq_sec_flavor);
1572 if ((SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1573 SPTLRPC_POLICY_NULL) &&
1574 lustre_unpack_msg(msg, req->rq_reqdata_len))
1575 RETURN(SECSVC_DROP);
1578 policy = sptlrpc_flavor2policy(req->rq_sec_flavor);
1580 CERROR("unsupported security flavor %x\n", req->rq_sec_flavor);
1581 RETURN(SECSVC_DROP);
1584 LASSERT(policy->sp_sops->accept);
1585 rc = policy->sp_sops->accept(req);
1587 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1588 sptlrpc_policy_put(policy);
1590 /* FIXME move to proper place */
1591 if (rc == SECSVC_OK) {
1592 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1594 if (opc == OST_WRITE)
1595 req->rq_bulk_write = 1;
1596 else if (opc == OST_READ)
1597 req->rq_bulk_read = 1;
1600 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1604 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1607 struct ptlrpc_sec_policy *policy;
1608 struct ptlrpc_reply_state *rs;
1612 LASSERT(req->rq_svc_ctx);
1613 LASSERT(req->rq_svc_ctx->sc_policy);
1615 policy = req->rq_svc_ctx->sc_policy;
1616 LASSERT(policy->sp_sops->alloc_rs);
1618 rc = policy->sp_sops->alloc_rs(req, msglen);
1619 if (unlikely(rc == -ENOMEM)) {
1620 /* failed alloc, try emergency pool */
1621 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
1625 req->rq_reply_state = rs;
1626 rc = policy->sp_sops->alloc_rs(req, msglen);
1628 lustre_put_emerg_rs(rs);
1629 req->rq_reply_state = NULL;
1634 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1639 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1641 struct ptlrpc_sec_policy *policy;
1645 LASSERT(req->rq_svc_ctx);
1646 LASSERT(req->rq_svc_ctx->sc_policy);
1648 policy = req->rq_svc_ctx->sc_policy;
1649 LASSERT(policy->sp_sops->authorize);
1651 rc = policy->sp_sops->authorize(req);
1652 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1657 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1659 struct ptlrpc_sec_policy *policy;
1660 unsigned int prealloc;
1663 LASSERT(rs->rs_svc_ctx);
1664 LASSERT(rs->rs_svc_ctx->sc_policy);
1666 policy = rs->rs_svc_ctx->sc_policy;
1667 LASSERT(policy->sp_sops->free_rs);
1669 prealloc = rs->rs_prealloc;
1670 policy->sp_sops->free_rs(rs);
1673 lustre_put_emerg_rs(rs);
1677 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
1679 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1684 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1685 atomic_inc(&ctx->sc_refcount);
1688 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
1690 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1695 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1696 if (atomic_dec_and_test(&ctx->sc_refcount)) {
1697 if (ctx->sc_policy->sp_sops->free_ctx)
1698 ctx->sc_policy->sp_sops->free_ctx(ctx);
1700 req->rq_svc_ctx = NULL;
1703 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
1705 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1710 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1711 if (ctx->sc_policy->sp_sops->invalidate_ctx)
1712 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
1714 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
1716 /****************************************
1718 ****************************************/
1720 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
1721 struct ptlrpc_bulk_desc *desc)
1723 struct ptlrpc_cli_ctx *ctx;
1725 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1728 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1730 ctx = req->rq_cli_ctx;
1731 if (ctx->cc_ops->wrap_bulk)
1732 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
1735 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
1738 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
1739 struct ptlrpc_bulk_desc *desc)
1746 for (i = 0; i < pg_count && nob > 0; i++) {
1748 desc->bd_iov[i].kiov_page = pga[i]->pg;
1749 desc->bd_iov[i].kiov_len = pga[i]->count > nob ?
1750 nob : pga[i]->count;
1751 desc->bd_iov[i].kiov_offset = pga[i]->off & ~CFS_PAGE_MASK;
1753 #warning FIXME for liblustre!
1754 desc->bd_iov[i].iov_base = pga[i]->pg->addr;
1755 desc->bd_iov[i].iov_len = pga[i]->count > nob ?
1756 nob : pga[i]->count;
1759 desc->bd_iov_count++;
1760 nob -= pga[i]->count;
1764 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
1765 int nob, obd_count pg_count,
1766 struct brw_page **pga)
1768 struct ptlrpc_bulk_desc *desc;
1769 struct ptlrpc_cli_ctx *ctx;
1772 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1775 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
1777 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1779 CERROR("out of memory, can't verify bulk read data\n");
1783 pga_to_bulk_desc(nob, pg_count, pga, desc);
1785 ctx = req->rq_cli_ctx;
1786 if (ctx->cc_ops->unwrap_bulk)
1787 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1789 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1793 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
1795 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
1796 struct ptlrpc_bulk_desc *desc)
1798 struct ptlrpc_cli_ctx *ctx;
1800 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1803 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
1805 ctx = req->rq_cli_ctx;
1806 if (ctx->cc_ops->unwrap_bulk)
1807 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1811 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
1813 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
1814 struct ptlrpc_bulk_desc *desc)
1816 struct ptlrpc_svc_ctx *ctx;
1818 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1821 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1823 ctx = req->rq_svc_ctx;
1824 if (ctx->sc_policy->sp_sops->wrap_bulk)
1825 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
1829 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
1831 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
1832 struct ptlrpc_bulk_desc *desc)
1834 struct ptlrpc_svc_ctx *ctx;
1836 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1839 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1841 ctx = req->rq_svc_ctx;
1842 if (ctx->sc_policy->sp_sops->unwrap_bulk);
1843 return ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
1847 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
1850 /****************************************
1851 * user descriptor helpers *
1852 ****************************************/
1854 int sptlrpc_current_user_desc_size(void)
1859 ngroups = current_ngroups;
1861 if (ngroups > LUSTRE_MAX_GROUPS)
1862 ngroups = LUSTRE_MAX_GROUPS;
1866 return sptlrpc_user_desc_size(ngroups);
1868 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
1870 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
1872 struct ptlrpc_user_desc *pud;
1874 pud = lustre_msg_buf(msg, offset, 0);
1876 pud->pud_uid = cfs_current()->uid;
1877 pud->pud_gid = cfs_current()->gid;
1878 pud->pud_fsuid = cfs_current()->fsuid;
1879 pud->pud_fsgid = cfs_current()->fsgid;
1880 pud->pud_cap = cfs_current()->cap_effective;
1881 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
1885 if (pud->pud_ngroups > current_ngroups)
1886 pud->pud_ngroups = current_ngroups;
1887 memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
1888 pud->pud_ngroups * sizeof(__u32));
1889 task_unlock(current);
1894 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
1896 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
1898 struct ptlrpc_user_desc *pud;
1901 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
1905 if (lustre_msg_swabbed(msg)) {
1906 __swab32s(&pud->pud_uid);
1907 __swab32s(&pud->pud_gid);
1908 __swab32s(&pud->pud_fsuid);
1909 __swab32s(&pud->pud_fsgid);
1910 __swab32s(&pud->pud_cap);
1911 __swab32s(&pud->pud_ngroups);
1914 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
1915 CERROR("%u groups is too large\n", pud->pud_ngroups);
1919 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
1920 msg->lm_buflens[offset]) {
1921 CERROR("%u groups are claimed but bufsize only %u\n",
1922 pud->pud_ngroups, msg->lm_buflens[offset]);
1926 if (lustre_msg_swabbed(msg)) {
1927 for (i = 0; i < pud->pud_ngroups; i++)
1928 __swab32s(&pud->pud_groups[i]);
1933 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
1935 /****************************************
1936 * user supplied flavor string parsing *
1937 ****************************************/
1940 int get_default_flavor(enum lustre_part to_part, struct sec_flavor_config *conf)
1942 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1943 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
1944 conf->sfc_flags = 0;
1948 conf->sfc_rpc_flavor = SPTLRPC_FLVR_PLAIN;
1951 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1954 CERROR("Unknown to lustre part %d, apply defaults\n", to_part);
1955 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1961 void get_flavor_by_rpc(__u32 rpc_flavor, struct sec_flavor_config *conf)
1963 conf->sfc_rpc_flavor = rpc_flavor;
1964 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1965 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
1966 conf->sfc_flags = 0;
1968 switch (rpc_flavor) {
1969 case SPTLRPC_FLVR_NULL:
1970 case SPTLRPC_FLVR_PLAIN:
1972 case SPTLRPC_FLVR_KRB5P:
1973 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1975 case SPTLRPC_FLVR_KRB5I:
1976 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
1984 void get_flavor_by_rpc_bulk(__u32 rpc_flavor, int bulk_priv,
1985 struct sec_flavor_config *conf)
1988 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1990 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1992 switch (rpc_flavor) {
1993 case SPTLRPC_FLVR_PLAIN:
1994 conf->sfc_bulk_csum = BULK_CSUM_ALG_MD5;
1996 case SPTLRPC_FLVR_KRB5I:
1997 case SPTLRPC_FLVR_KRB5P:
1998 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
2005 static __u32 __flavors[] = {
2012 #define __nflavors (sizeof(__flavors)/sizeof(__u32))
2015 * flavor string format: rpc[-bulk{n|i|p}[:cksum/enc]]
2021 * krb5i-bulkp:sha512/arc4
2023 int sptlrpc_parse_flavor(enum lustre_part from_part, enum lustre_part to_part,
2024 char *str, struct sec_flavor_config *conf)
2026 char *f, *bulk, *alg, *enc;
2032 if (get_default_flavor(to_part, conf))
2037 for (i = 0; i < __nflavors; i++) {
2038 f = sptlrpc_flavor2name(__flavors[i]);
2039 if (strncmp(str, f, strlen(f)) == 0)
2043 if (i >= __nflavors)
2044 GOTO(invalid, -EINVAL);
2046 /* prepare local buffer thus we can modify it as we want */
2047 strncpy(buf, str, 64);
2050 /* find bulk string */
2051 bulk = strchr(buf, '-');
2055 /* now the first part must equal to rpc flavor name */
2056 if (strcmp(buf, f) != 0)
2057 GOTO(invalid, -EINVAL);
2059 get_flavor_by_rpc(__flavors[i], conf);
2064 /* null flavor should not have any suffix */
2065 if (__flavors[i] == SPTLRPC_FLVR_NULL)
2066 GOTO(invalid, -EINVAL);
2068 /* find bulk algorithm string */
2069 alg = strchr(bulk, ':');
2073 /* verify bulk section */
2074 if (strcmp(bulk, "bulkn") == 0) {
2075 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
2076 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
2080 if (strcmp(bulk, "bulki") == 0)
2082 else if (strcmp(bulk, "bulkp") == 0)
2085 GOTO(invalid, -EINVAL);
2087 /* plain policy dosen't support bulk encryption */
2088 if (bulk_priv && __flavors[i] == SPTLRPC_FLVR_PLAIN)
2089 GOTO(invalid, -EINVAL);
2091 get_flavor_by_rpc_bulk(__flavors[i], bulk_priv, conf);
2096 /* find encryption algorithm string */
2097 enc = strchr(alg, '/');
2101 /* bulk combination sanity check */
2102 if ((bulk_priv && enc == NULL) || (bulk_priv == 0 && enc))
2103 GOTO(invalid, -EINVAL);
2105 /* checksum algorithm */
2106 for (i = 0; i < BULK_CSUM_ALG_MAX; i++) {
2107 if (strcmp(alg, sptlrpc_bulk_csum_alg2name(i)) == 0) {
2108 conf->sfc_bulk_csum = i;
2112 if (i >= BULK_CSUM_ALG_MAX)
2113 GOTO(invalid, -EINVAL);
2115 /* privacy algorithm */
2117 if (strcmp(enc, "arc4") != 0)
2118 GOTO(invalid, -EINVAL);
2119 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
2123 /* * set ROOTONLY flag:
2126 * * set BULK flag for:
2129 if (to_part == LUSTRE_OST ||
2130 (from_part == LUSTRE_MDT && to_part == LUSTRE_MDT))
2131 conf->sfc_flags |= PTLRPC_SEC_FL_ROOTONLY;
2132 if (from_part == LUSTRE_CLI && to_part == LUSTRE_OST)
2133 conf->sfc_flags |= PTLRPC_SEC_FL_BULK;
2136 __swab32s(&conf->sfc_rpc_flavor);
2137 __swab32s(&conf->sfc_bulk_csum);
2138 __swab32s(&conf->sfc_bulk_priv);
2139 __swab32s(&conf->sfc_flags);
2143 CERROR("invalid flavor string: %s\n", str);
2146 EXPORT_SYMBOL(sptlrpc_parse_flavor);
2148 /****************************************
2150 ****************************************/
2152 const char * sec2target_str(struct ptlrpc_sec *sec)
2154 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2156 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
2158 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2160 EXPORT_SYMBOL(sec2target_str);
2162 /****************************************
2163 * initialize/finalize *
2164 ****************************************/
2166 int sptlrpc_init(void)
2170 rc = sptlrpc_enc_pool_init();
2174 rc = sptlrpc_null_init();
2178 rc = sptlrpc_plain_init();
2182 rc = sptlrpc_lproc_init();
2189 sptlrpc_plain_fini();
2191 sptlrpc_null_fini();
2193 sptlrpc_enc_pool_fini();
2198 void sptlrpc_fini(void)
2200 sptlrpc_lproc_fini();
2201 sptlrpc_plain_fini();
2202 sptlrpc_null_fini();
2203 sptlrpc_enc_pool_fini();