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, uint64_t pag, uid_t uid,
389 int grace, int force)
391 struct ptlrpc_cli_ctx *ctx;
392 struct hlist_node *pos, *next;
393 HLIST_HEAD(freelist);
397 might_sleep_if(grace);
399 spin_lock(&sec->ps_lock);
400 for (i = 0; i < sec->ps_ccache_size; i++) {
401 hlist_for_each_entry_safe(ctx, pos, next,
402 &sec->ps_ccache[i], cc_hash) {
403 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
405 if (ctx_is_eternal(ctx))
408 if (sec->ps_flags & PTLRPC_SEC_FL_PAG) {
409 if (pag != -1 && pag != ctx->cc_vcred.vc_pag)
412 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
416 if (atomic_read(&ctx->cc_refcount) > 1) {
421 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
423 atomic_read(&ctx->cc_refcount),
424 ctx, ctx->cc_vcred.vc_uid,
425 sec2target_str(ctx->cc_sec), grace);
427 ctx_unhash(ctx, &freelist);
429 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
431 clear_bit(PTLRPC_CTX_UPTODATE_BIT,
435 spin_unlock(&sec->ps_lock);
437 ctx_list_destroy(&freelist);
442 unsigned int ctx_hash_index(struct ptlrpc_sec *sec, __u64 key)
444 return (unsigned int) (key & (sec->ps_ccache_size - 1));
448 * return matched context. If it's a newly created one, we also give the
449 * first push to refresh. return NULL if error happens.
452 struct ptlrpc_cli_ctx * ctx_cache_lookup(struct ptlrpc_sec *sec,
453 struct vfs_cred *vcred,
454 int create, int remove_dead)
456 struct ptlrpc_cli_ctx *ctx = NULL, *new = NULL;
457 struct hlist_head *hash_head;
458 struct hlist_node *pos, *next;
459 HLIST_HEAD(freelist);
460 unsigned int hash, gc = 0, found = 0;
465 hash = ctx_hash_index(sec, vcred->vc_pag);
466 LASSERT(hash < sec->ps_ccache_size);
467 hash_head = &sec->ps_ccache[hash];
470 spin_lock(&sec->ps_lock);
472 /* gc_next == 0 means never do gc */
473 if (remove_dead && sec->ps_gc_next &&
474 cfs_time_after(cfs_time_current_sec(), sec->ps_gc_next)) {
475 ctx_cache_gc(sec, &freelist);
479 hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_hash) {
481 ctx_check_death_locked(ctx, remove_dead ? &freelist : NULL))
484 if (ctx_match(ctx, vcred)) {
491 if (new && new != ctx) {
492 /* lost the race, just free it */
493 hlist_add_head(&new->cc_hash, &freelist);
497 /* hot node, move to head */
498 if (hash_head->first != &ctx->cc_hash) {
499 __hlist_del(&ctx->cc_hash);
500 hlist_add_head(&ctx->cc_hash, hash_head);
503 /* don't allocate for reverse sec */
504 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
505 spin_unlock(&sec->ps_lock);
510 ctx_enhash(new, hash_head);
513 spin_unlock(&sec->ps_lock);
514 new = sec->ps_policy->sp_cops->create_ctx(sec, vcred);
516 atomic_inc(&sec->ps_busy);
525 atomic_inc(&ctx->cc_refcount);
527 spin_unlock(&sec->ps_lock);
529 /* the allocator of the context must give the first push to refresh */
532 sptlrpc_ctx_refresh(new);
535 ctx_list_destroy(&freelist);
540 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
542 struct vfs_cred vcred;
543 int create = 1, remove_dead = 1;
545 if (sec->ps_flags & (PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY)) {
549 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
554 vcred.vc_pag = sec->ps_flags & PTLRPC_SEC_FL_PAG ?
555 CURRENT_PAG : current->uid;
556 vcred.vc_uid = current->uid;
557 vcred.vc_gid = current->gid;
558 /* don't distinguash root from others in pag mode */
559 if (vcred.vc_uid == 0)
563 if (sec->ps_policy->sp_cops->lookup_ctx)
564 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred);
566 return ctx_cache_lookup(sec, &vcred, create, remove_dead);
569 /**************************************************
570 * client context APIs *
571 **************************************************/
574 void sptlrpc_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
576 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
578 if (!ctx_is_refreshed(ctx) && ctx->cc_ops->refresh)
579 ctx->cc_ops->refresh(ctx);
582 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx)
584 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
585 atomic_inc(&ctx->cc_refcount);
588 EXPORT_SYMBOL(sptlrpc_ctx_get);
590 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
592 struct ptlrpc_sec *sec = ctx->cc_sec;
595 LASSERT(atomic_read(&ctx->cc_refcount));
597 if (!atomic_dec_and_test(&ctx->cc_refcount))
600 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags) == 0);
601 LASSERT(hlist_unhashed(&ctx->cc_hash));
603 /* if required async, we must clear the UPTODATE bit to prevent extra
604 * rpcs during destroy procedure.
607 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
609 /* destroy this context */
610 if (!sptlrpc_sec_destroy_ctx(sec, ctx))
613 CWARN("%s@%p: put last ctx, also destroy the sec\n",
614 sec->ps_policy->sp_name, sec);
616 sptlrpc_sec_destroy(sec);
618 EXPORT_SYMBOL(sptlrpc_ctx_put);
621 * mark a ctx as DEAD, and pull it out from hash table.
623 * NOTE: the caller must hold at least 1 ref on the ctx.
625 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx)
627 LASSERT(ctx->cc_sec);
628 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
632 spin_lock(&ctx->cc_sec->ps_lock);
634 if (test_and_clear_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags)) {
635 LASSERT(!hlist_unhashed(&ctx->cc_hash));
636 LASSERT(atomic_read(&ctx->cc_refcount) > 1);
638 hlist_del_init(&ctx->cc_hash);
639 if (atomic_dec_and_test(&ctx->cc_refcount))
643 spin_unlock(&ctx->cc_sec->ps_lock);
645 EXPORT_SYMBOL(sptlrpc_ctx_expire);
647 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new)
649 struct ptlrpc_cli_ctx *ctx;
650 struct hlist_node *pos, *next;
651 HLIST_HEAD(freelist);
655 hash = ctx_hash_index(sec, new->cc_vcred.vc_pag);
656 LASSERT(hash < sec->ps_ccache_size);
658 spin_lock(&sec->ps_lock);
660 hlist_for_each_entry_safe(ctx, pos, next,
661 &sec->ps_ccache[hash], cc_hash) {
662 if (!ctx_match(ctx, &new->cc_vcred))
666 ctx_unhash(ctx, &freelist);
670 ctx_enhash(new, &sec->ps_ccache[hash]);
671 atomic_inc(&sec->ps_busy);
673 spin_unlock(&sec->ps_lock);
675 ctx_list_destroy(&freelist);
678 EXPORT_SYMBOL(sptlrpc_ctx_replace);
680 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
682 struct obd_import *imp = req->rq_import;
685 LASSERT(!req->rq_cli_ctx);
688 req->rq_cli_ctx = get_my_ctx(imp->imp_sec);
690 if (!req->rq_cli_ctx) {
691 CERROR("req %p: fail to get context from cache\n", req);
698 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
700 struct ptlrpc_request *req, *next;
702 spin_lock(&ctx->cc_lock);
703 list_for_each_entry_safe(req, next, &ctx->cc_req_list, rq_ctx_chain) {
704 list_del_init(&req->rq_ctx_chain);
705 ptlrpc_wake_client_req(req);
707 spin_unlock(&ctx->cc_lock);
709 EXPORT_SYMBOL(sptlrpc_ctx_wakeup);
711 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
713 LASSERT(ctx->cc_ops);
715 if (ctx->cc_ops->display == NULL)
718 return ctx->cc_ops->display(ctx, buf, bufsize);
721 void sptlrpc_req_put_ctx(struct ptlrpc_request *req)
726 LASSERT(req->rq_cli_ctx);
728 /* request might be asked to release earlier while still
729 * in the context waiting list.
731 if (!list_empty(&req->rq_ctx_chain)) {
732 spin_lock(&req->rq_cli_ctx->cc_lock);
733 list_del_init(&req->rq_ctx_chain);
734 spin_unlock(&req->rq_cli_ctx->cc_lock);
737 /* this could be called with spinlock hold, use async mode */
738 sptlrpc_ctx_put(req->rq_cli_ctx, 0);
739 req->rq_cli_ctx = NULL;
744 * request must have a context. if failed to get new context,
745 * just restore the old one
747 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
749 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
754 LASSERT(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags));
756 /* make sure not on context waiting list */
757 spin_lock(&ctx->cc_lock);
758 list_del_init(&req->rq_ctx_chain);
759 spin_unlock(&ctx->cc_lock);
761 sptlrpc_ctx_get(ctx);
762 sptlrpc_req_put_ctx(req);
763 rc = sptlrpc_req_get_ctx(req);
765 LASSERT(req->rq_cli_ctx);
766 LASSERT(req->rq_cli_ctx != ctx);
767 sptlrpc_ctx_put(ctx, 1);
769 LASSERT(!req->rq_cli_ctx);
770 req->rq_cli_ctx = ctx;
776 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
779 if (ctx_is_refreshed(ctx))
785 int ctx_refresh_timeout(void *data)
787 struct ptlrpc_request *req = data;
790 /* conn_cnt is needed in expire_one_request */
791 lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
793 rc = ptlrpc_expire_one_request(req);
794 /* if we started recovery, we should mark this ctx dead; otherwise
795 * in case of lgssd died nobody would retire this ctx, following
796 * connecting will still find the same ctx thus cause deadlock.
797 * there's an assumption that expire time of the request should be
798 * later than the context refresh expire time.
801 ctx_expire(req->rq_cli_ctx);
806 void ctx_refresh_interrupt(void *data)
812 * the status of context could be subject to be changed by other threads at any
813 * time. we allow this race. but once we return with 0, the caller will
814 * suppose it's uptodated and keep using it until the affected rpc is done.
818 * = 0 - wait until success or fatal error occur
819 * > 0 - timeout value
821 * return 0 only if the context is uptodated.
823 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
825 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
826 struct l_wait_info lwi;
833 if (ctx_is_eternal(ctx) || req->rq_ctx_init || req->rq_ctx_fini)
836 /* reverse ctxs, don't refresh */
837 if (ctx->cc_sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
840 spin_lock(&ctx->cc_lock);
842 if (ctx_check_uptodate(ctx)) {
843 if (!list_empty(&req->rq_ctx_chain))
844 list_del_init(&req->rq_ctx_chain);
845 spin_unlock(&ctx->cc_lock);
849 if (test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags)) {
851 if (!list_empty(&req->rq_ctx_chain))
852 list_del_init(&req->rq_ctx_chain);
853 spin_unlock(&ctx->cc_lock);
857 /* This is subtle. For resent message we have to keep original
858 * context to survive following situation:
859 * 1. the request sent to server
860 * 2. recovery was kick start
861 * 3. recovery finished, the request marked as resent
862 * 4. resend the request
863 * 5. old reply from server received (because xid is the same)
864 * 6. verify reply (has to be success)
865 * 7. new reply from server received, lnet drop it
867 * Note we can't simply change xid for resent request because
868 * server reply on it for reply reconstruction.
870 * Commonly the original context should be uptodate because we
871 * have a expiry nice time; And server will keep their half part
872 * context because we at least hold a ref of old context which
873 * prevent the context detroy RPC be sent. So server still can
874 * accept the request and finish RPC. Two cases:
875 * 1. If server side context has been trimed, a NO_CONTEXT will
876 * be returned, gss_cli_ctx_verify/unseal will switch to new
878 * 2. Current context never be refreshed, then we are fine: we
879 * never really send request with old context before.
881 if (test_bit(PTLRPC_CTX_UPTODATE, &ctx->cc_flags) &&
883 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
884 if (!list_empty(&req->rq_ctx_chain))
885 list_del_init(&req->rq_ctx_chain);
886 spin_unlock(&ctx->cc_lock);
890 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
891 spin_unlock(&ctx->cc_lock);
893 /* don't have to, but we don't want to release it too soon */
894 sptlrpc_ctx_get(ctx);
896 rc = sptlrpc_req_replace_dead_ctx(req);
898 LASSERT(ctx == req->rq_cli_ctx);
899 CERROR("req %p: failed to replace dead ctx %p\n",
902 LASSERT(list_empty(&req->rq_ctx_chain));
903 sptlrpc_ctx_put(ctx, 1);
907 LASSERT(ctx != req->rq_cli_ctx);
908 CWARN("req %p: replace dead ctx %p(%u->%s) => %p\n",
909 req, ctx, ctx->cc_vcred.vc_uid,
910 sec2target_str(ctx->cc_sec), req->rq_cli_ctx);
912 sptlrpc_ctx_put(ctx, 1);
913 ctx = req->rq_cli_ctx;
914 LASSERT(list_empty(&req->rq_ctx_chain));
916 spin_lock(&ctx->cc_lock);
920 /* Now we're sure this context is during upcall, add myself into
923 if (list_empty(&req->rq_ctx_chain))
924 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
926 spin_unlock(&ctx->cc_lock);
929 RETURN(-EWOULDBLOCK);
932 /* Clear any flags that may be present from previous sends */
933 LASSERT(req->rq_receiving_reply == 0);
934 spin_lock(&req->rq_lock);
936 req->rq_timedout = 0;
939 spin_unlock(&req->rq_lock);
941 lwi = LWI_TIMEOUT_INTR(timeout == 0 ? LONG_MAX : timeout * HZ,
942 ctx_refresh_timeout, ctx_refresh_interrupt, req);
943 rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
945 spin_lock(&ctx->cc_lock);
946 /* five cases we are here:
947 * 1. successfully refreshed;
948 * 2. someone else mark this ctx dead by force;
950 * 4. timedout, and we don't want recover from the failure;
951 * 5. timedout, and waked up upon recovery finished;
953 if (!ctx_is_refreshed(ctx)) {
954 /* timed out or interruptted */
955 list_del_init(&req->rq_ctx_chain);
956 spin_unlock(&ctx->cc_lock);
965 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
967 struct sec_flavor_config *conf;
969 LASSERT(req->rq_import);
970 LASSERT(req->rq_import->imp_sec);
971 LASSERT(req->rq_cli_ctx);
972 LASSERT(req->rq_cli_ctx->cc_sec);
973 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
975 /* special security flags accoding to opcode */
978 req->rq_bulk_read = 1;
981 req->rq_bulk_write = 1;
984 req->rq_ctx_init = 1;
987 req->rq_ctx_fini = 1;
991 req->rq_sec_flavor = req->rq_cli_ctx->cc_sec->ps_flavor;
993 /* force SVC_NONE for context initiation rpc, SVC_AUTH for context
996 if (unlikely(req->rq_ctx_init)) {
997 req->rq_sec_flavor = SEC_MAKE_RPC_FLAVOR(
998 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
999 SEC_FLAVOR_SUBPOLICY(req->rq_sec_flavor),
1000 SEC_FLAVOR_SVC(SPTLRPC_SVC_NONE));
1001 } else if (unlikely(req->rq_ctx_fini)) {
1002 req->rq_sec_flavor = SEC_MAKE_RPC_FLAVOR(
1003 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
1004 SEC_FLAVOR_SUBPOLICY(req->rq_sec_flavor),
1005 SEC_FLAVOR_SVC(SPTLRPC_SVC_AUTH));
1008 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1010 /* user descriptor flag, except ROOTONLY which don't need, and
1011 * null security which can't
1013 if ((conf->sfc_flags & PTLRPC_SEC_FL_ROOTONLY) == 0 &&
1014 req->rq_sec_flavor != SPTLRPC_FLVR_NULL)
1015 req->rq_sec_flavor |= SEC_FLAVOR_FL_USER;
1017 /* bulk security flag */
1018 if ((req->rq_bulk_read || req->rq_bulk_write) &&
1019 (conf->sfc_bulk_priv != BULK_PRIV_ALG_NULL ||
1020 conf->sfc_bulk_csum != BULK_CSUM_ALG_NULL))
1021 req->rq_sec_flavor |= SEC_FLAVOR_FL_BULK;
1024 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
1026 if (SEC_FLAVOR_SVC(req->rq_sec_flavor) != SPTLRPC_SVC_PRIV)
1029 LASSERT(req->rq_clrbuf);
1030 if (req->rq_pool || !req->rq_reqbuf)
1033 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1034 req->rq_reqbuf = NULL;
1035 req->rq_reqbuf_len = 0;
1039 * check whether current user have valid context for an import or not.
1040 * might repeatedly try in case of non-fatal errors.
1041 * return 0 on success, < 0 on failure
1043 int sptlrpc_import_check_ctx(struct obd_import *imp)
1045 struct ptlrpc_cli_ctx *ctx;
1046 struct ptlrpc_request *req = NULL;
1052 ctx = get_my_ctx(imp->imp_sec);
1056 if (ctx_is_eternal(ctx)) {
1057 sptlrpc_ctx_put(ctx, 1);
1065 spin_lock_init(&req->rq_lock);
1066 atomic_set(&req->rq_refcount, 10000);
1067 INIT_LIST_HEAD(&req->rq_ctx_chain);
1068 init_waitqueue_head(&req->rq_reply_waitq);
1069 req->rq_import = imp;
1070 req->rq_cli_ctx = ctx;
1072 rc = sptlrpc_req_refresh_ctx(req, 0);
1073 LASSERT(list_empty(&req->rq_ctx_chain));
1074 sptlrpc_ctx_put(req->rq_cli_ctx, 1);
1080 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1082 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1087 LASSERT(ctx->cc_sec);
1088 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1090 /* we wrap bulk request here because now we can be sure
1091 * the context is uptodate.
1094 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1099 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
1100 case SPTLRPC_SVC_NONE:
1101 case SPTLRPC_SVC_AUTH:
1102 LASSERT(ctx->cc_ops->sign);
1103 rc = ctx->cc_ops->sign(ctx, req);
1105 case SPTLRPC_SVC_PRIV:
1106 LASSERT(ctx->cc_ops->seal);
1107 rc = ctx->cc_ops->seal(ctx, req);
1114 LASSERT(req->rq_reqdata_len);
1115 LASSERT(req->rq_reqdata_len % 8 == 0);
1116 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1123 * rq_nob_received is the actual received data length
1125 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1127 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1132 LASSERT(ctx->cc_sec);
1133 LASSERT(ctx->cc_ops);
1134 LASSERT(req->rq_repbuf);
1136 req->rq_repdata_len = req->rq_nob_received;
1138 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1139 CERROR("replied data length %d too small\n",
1140 req->rq_nob_received);
1144 if (req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V1 ||
1145 req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) {
1146 /* it's must be null flavor, so our requets also should be
1148 if (SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1149 SPTLRPC_POLICY_NULL) {
1150 CERROR("request flavor is %x but reply with null\n",
1151 req->rq_sec_flavor);
1156 ptlrpc_sec_flavor_t tmpf = req->rq_repbuf->lm_secflvr;
1158 if (req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1161 if (SEC_FLAVOR_POLICY(tmpf) !=
1162 SEC_FLAVOR_POLICY(req->rq_sec_flavor)) {
1163 CERROR("request policy %u while reply with %d\n",
1164 SEC_FLAVOR_POLICY(req->rq_sec_flavor),
1165 SEC_FLAVOR_POLICY(tmpf));
1169 if ((SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1170 SPTLRPC_POLICY_NULL) &&
1171 lustre_unpack_msg(req->rq_repbuf, req->rq_nob_received))
1175 switch (SEC_FLAVOR_SVC(req->rq_sec_flavor)) {
1176 case SPTLRPC_SVC_NONE:
1177 case SPTLRPC_SVC_AUTH:
1178 LASSERT(ctx->cc_ops->verify);
1179 rc = ctx->cc_ops->verify(ctx, req);
1181 case SPTLRPC_SVC_PRIV:
1182 LASSERT(ctx->cc_ops->unseal);
1183 rc = ctx->cc_ops->unseal(ctx, req);
1189 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1193 /**************************************************
1195 **************************************************/
1198 * let policy module to determine whether take refrence of
1202 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1203 struct ptlrpc_svc_ctx *ctx,
1205 unsigned long flags)
1207 struct ptlrpc_sec_policy *policy;
1208 struct ptlrpc_sec *sec;
1211 flavor = SEC_FLAVOR_RPC(flavor);
1214 LASSERT(imp->imp_dlm_fake == 1);
1216 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1217 imp->imp_obd->obd_type->typ_name,
1218 imp->imp_obd->obd_name,
1219 sptlrpc_flavor2name(flavor));
1221 policy = sptlrpc_policy_get(ctx->sc_policy);
1222 flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1224 LASSERT(imp->imp_dlm_fake == 0);
1226 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1227 imp->imp_obd->obd_type->typ_name,
1228 imp->imp_obd->obd_name,
1229 sptlrpc_flavor2name(flavor));
1231 policy = sptlrpc_flavor2policy(flavor);
1233 CERROR("invalid flavor 0x%x\n", flavor);
1238 sec = policy->sp_cops->create_sec(imp, ctx, flavor, flags);
1240 atomic_inc(&sec->ps_refcount);
1242 /* take 1 busy count on behalf of sec itself,
1243 * balanced in sptlrpc_set_put()
1245 atomic_inc(&sec->ps_busy);
1247 sptlrpc_policy_put(policy);
1253 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1255 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1258 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1259 LASSERT(atomic_read(&sec->ps_busy) == 0);
1260 LASSERT(policy->sp_cops->destroy_sec);
1262 policy->sp_cops->destroy_sec(sec);
1263 sptlrpc_policy_put(policy);
1267 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1269 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1271 if (!atomic_dec_and_test(&sec->ps_refcount)) {
1272 sptlrpc_policy_put(policy);
1276 ctx_cache_flush(sec, -1, -1, 1, 1);
1278 if (atomic_dec_and_test(&sec->ps_busy))
1279 sptlrpc_sec_destroy(sec);
1281 CWARN("delay to destroy %s@%p: busy contexts\n",
1282 policy->sp_name, sec);
1286 * return 1 means we should also destroy the sec structure.
1290 int sptlrpc_sec_destroy_ctx(struct ptlrpc_sec *sec,
1291 struct ptlrpc_cli_ctx *ctx)
1293 LASSERT(sec == ctx->cc_sec);
1294 LASSERT(atomic_read(&sec->ps_busy));
1295 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1296 LASSERT(hlist_unhashed(&ctx->cc_hash));
1297 LASSERT(list_empty(&ctx->cc_req_list));
1298 LASSERT(sec->ps_policy->sp_cops->destroy_ctx);
1300 sec->ps_policy->sp_cops->destroy_ctx(sec, ctx);
1302 if (atomic_dec_and_test(&sec->ps_busy)) {
1303 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1311 * when complete successfully, req->rq_reqmsg should point to the
1314 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1316 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1317 struct ptlrpc_sec_policy *policy;
1321 LASSERT(atomic_read(&ctx->cc_refcount));
1322 LASSERT(ctx->cc_sec);
1323 LASSERT(ctx->cc_sec->ps_policy);
1324 LASSERT(req->rq_reqmsg == NULL);
1326 policy = ctx->cc_sec->ps_policy;
1327 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1329 LASSERT(req->rq_reqmsg);
1330 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1332 /* zeroing preallocated buffer */
1334 memset(req->rq_reqmsg, 0, msgsize);
1340 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1342 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1343 struct ptlrpc_sec_policy *policy;
1346 LASSERT(atomic_read(&ctx->cc_refcount));
1347 LASSERT(ctx->cc_sec);
1348 LASSERT(ctx->cc_sec->ps_policy);
1349 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1351 policy = ctx->cc_sec->ps_policy;
1352 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1356 * NOTE caller must guarantee the buffer size is enough for the enlargement
1358 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1359 int segment, int newsize)
1362 int oldsize, oldmsg_size, movesize;
1364 LASSERT(segment < msg->lm_bufcount);
1365 LASSERT(msg->lm_buflens[segment] <= newsize);
1367 if (msg->lm_buflens[segment] == newsize)
1370 /* nothing to do if we are enlarging the last segment */
1371 if (segment == msg->lm_bufcount - 1) {
1372 msg->lm_buflens[segment] = newsize;
1376 oldsize = msg->lm_buflens[segment];
1378 src = lustre_msg_buf(msg, segment + 1, 0);
1379 msg->lm_buflens[segment] = newsize;
1380 dst = lustre_msg_buf(msg, segment + 1, 0);
1381 msg->lm_buflens[segment] = oldsize;
1383 /* move from segment + 1 to end segment */
1384 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1385 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1386 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1387 LASSERT(movesize >= 0);
1390 memmove(dst, src, movesize);
1392 /* note we don't clear the ares where old data live, not secret */
1394 /* finally set new segment size */
1395 msg->lm_buflens[segment] = newsize;
1397 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1400 * enlarge @segment of upper message req->rq_reqmsg to @newsize, all data
1401 * will be preserved after enlargement. this must be called after rq_reqmsg has
1402 * been intialized at least.
1404 * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1407 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1408 int segment, int newsize)
1410 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1411 struct ptlrpc_sec_cops *cops;
1412 struct lustre_msg *msg = req->rq_reqmsg;
1416 LASSERT(msg->lm_bufcount > segment);
1417 LASSERT(msg->lm_buflens[segment] <= newsize);
1419 if (msg->lm_buflens[segment] == newsize)
1422 cops = ctx->cc_sec->ps_policy->sp_cops;
1423 LASSERT(cops->enlarge_reqbuf);
1424 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1426 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1428 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1430 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1431 struct ptlrpc_sec_policy *policy;
1435 LASSERT(atomic_read(&ctx->cc_refcount));
1436 LASSERT(ctx->cc_sec);
1437 LASSERT(ctx->cc_sec->ps_policy);
1442 policy = ctx->cc_sec->ps_policy;
1443 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1446 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1448 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1449 struct ptlrpc_sec_policy *policy;
1453 LASSERT(atomic_read(&ctx->cc_refcount));
1454 LASSERT(ctx->cc_sec);
1455 LASSERT(ctx->cc_sec->ps_policy);
1456 LASSERT(req->rq_repbuf);
1458 policy = ctx->cc_sec->ps_policy;
1459 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1463 int sptlrpc_import_get_sec(struct obd_import *imp,
1464 struct ptlrpc_svc_ctx *ctx,
1466 unsigned long flags)
1468 struct obd_device *obd = imp->imp_obd;
1472 LASSERT(obd->obd_type);
1474 /* old sec might be still there in reconnecting */
1478 imp->imp_sec = sptlrpc_sec_create(imp, ctx, flavor, flags);
1485 void sptlrpc_import_put_sec(struct obd_import *imp)
1487 if (imp->imp_sec == NULL)
1490 sptlrpc_sec_put(imp->imp_sec);
1491 imp->imp_sec = NULL;
1494 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1496 if (imp == NULL || imp->imp_sec == NULL)
1499 /* use 'grace' mode, it's crutial see explain in
1500 * sptlrpc_req_refresh_ctx()
1502 ctx_cache_flush(imp->imp_sec, 0, 0, 1, 1);
1505 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1507 uint64_t pag = cfs_current()->uid == 0 ? 0 : CURRENT_PAG;
1509 if (imp == NULL || imp->imp_sec == NULL)
1512 ctx_cache_flush(imp->imp_sec, pag, cfs_current()->uid, 1, 1);
1514 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1516 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1518 if (imp == NULL || imp->imp_sec == NULL)
1521 ctx_cache_flush(imp->imp_sec, -1, -1, 0, 1);
1523 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1525 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1526 struct ptlrpc_cli_ctx *ctx)
1528 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1530 if (!policy->sp_cops->install_rctx)
1532 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1535 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1536 struct ptlrpc_svc_ctx *ctx)
1538 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1540 if (!policy->sp_sops->install_rctx)
1542 return policy->sp_sops->install_rctx(imp, ctx);
1545 /****************************************
1546 * server side security *
1547 ****************************************/
1549 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1551 struct ptlrpc_sec_policy *policy;
1552 struct lustre_msg *msg = req->rq_reqbuf;
1557 LASSERT(req->rq_reqmsg == NULL);
1558 LASSERT(req->rq_repmsg == NULL);
1561 * in any case we avoid to call unpack_msg() for request of null flavor
1562 * which will later be done by ptlrpc_server_handle_request().
1564 if (req->rq_reqdata_len < sizeof(struct lustre_msg)) {
1565 CERROR("request size %d too small\n", req->rq_reqdata_len);
1566 RETURN(SECSVC_DROP);
1569 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V1 ||
1570 msg->lm_magic == LUSTRE_MSG_MAGIC_V1_SWABBED) {
1571 req->rq_sec_flavor = SPTLRPC_FLVR_NULL;
1573 req->rq_sec_flavor = msg->lm_secflvr;
1575 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1576 __swab32s(&req->rq_sec_flavor);
1578 if ((SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1579 SPTLRPC_POLICY_NULL) &&
1580 lustre_unpack_msg(msg, req->rq_reqdata_len))
1581 RETURN(SECSVC_DROP);
1584 policy = sptlrpc_flavor2policy(req->rq_sec_flavor);
1586 CERROR("unsupported security flavor %x\n", req->rq_sec_flavor);
1587 RETURN(SECSVC_DROP);
1590 LASSERT(policy->sp_sops->accept);
1591 rc = policy->sp_sops->accept(req);
1593 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1594 sptlrpc_policy_put(policy);
1596 /* FIXME move to proper place */
1597 if (rc == SECSVC_OK) {
1598 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1600 if (opc == OST_WRITE)
1601 req->rq_bulk_write = 1;
1602 else if (opc == OST_READ)
1603 req->rq_bulk_read = 1;
1609 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1612 struct ptlrpc_sec_policy *policy;
1613 struct ptlrpc_reply_state *rs;
1617 LASSERT(req->rq_svc_ctx);
1618 LASSERT(req->rq_svc_ctx->sc_policy);
1620 policy = req->rq_svc_ctx->sc_policy;
1621 LASSERT(policy->sp_sops->alloc_rs);
1623 rc = policy->sp_sops->alloc_rs(req, msglen);
1624 if (unlikely(rc == -ENOMEM)) {
1625 /* failed alloc, try emergency pool */
1626 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
1630 req->rq_reply_state = rs;
1631 rc = policy->sp_sops->alloc_rs(req, msglen);
1633 lustre_put_emerg_rs(rs);
1634 req->rq_reply_state = NULL;
1639 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1644 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1646 struct ptlrpc_sec_policy *policy;
1650 LASSERT(req->rq_svc_ctx);
1651 LASSERT(req->rq_svc_ctx->sc_policy);
1653 policy = req->rq_svc_ctx->sc_policy;
1654 LASSERT(policy->sp_sops->authorize);
1656 rc = policy->sp_sops->authorize(req);
1657 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1662 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1664 struct ptlrpc_sec_policy *policy;
1665 unsigned int prealloc;
1668 LASSERT(rs->rs_svc_ctx);
1669 LASSERT(rs->rs_svc_ctx->sc_policy);
1671 policy = rs->rs_svc_ctx->sc_policy;
1672 LASSERT(policy->sp_sops->free_rs);
1674 prealloc = rs->rs_prealloc;
1675 policy->sp_sops->free_rs(rs);
1678 lustre_put_emerg_rs(rs);
1682 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
1684 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1689 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1690 atomic_inc(&ctx->sc_refcount);
1693 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
1695 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1700 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1701 if (atomic_dec_and_test(&ctx->sc_refcount)) {
1702 if (ctx->sc_policy->sp_sops->free_ctx)
1703 ctx->sc_policy->sp_sops->free_ctx(ctx);
1705 req->rq_svc_ctx = NULL;
1708 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
1710 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1715 LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1716 if (ctx->sc_policy->sp_sops->invalidate_ctx)
1717 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
1719 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
1721 /****************************************
1723 ****************************************/
1725 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
1726 struct ptlrpc_bulk_desc *desc)
1728 struct ptlrpc_cli_ctx *ctx;
1730 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1733 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1735 ctx = req->rq_cli_ctx;
1736 if (ctx->cc_ops->wrap_bulk)
1737 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
1740 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
1743 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
1744 struct ptlrpc_bulk_desc *desc)
1751 for (i = 0; i < pg_count && nob > 0; i++) {
1753 desc->bd_iov[i].kiov_page = pga[i]->pg;
1754 desc->bd_iov[i].kiov_len = pga[i]->count > nob ?
1755 nob : pga[i]->count;
1756 desc->bd_iov[i].kiov_offset = pga[i]->off & ~CFS_PAGE_MASK;
1758 #warning FIXME for liblustre!
1759 desc->bd_iov[i].iov_base = pga[i]->pg->addr;
1760 desc->bd_iov[i].iov_len = pga[i]->count > nob ?
1761 nob : pga[i]->count;
1764 desc->bd_iov_count++;
1765 nob -= pga[i]->count;
1769 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
1770 int nob, obd_count pg_count,
1771 struct brw_page **pga)
1773 struct ptlrpc_bulk_desc *desc;
1774 struct ptlrpc_cli_ctx *ctx;
1777 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1780 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
1782 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1784 CERROR("out of memory, can't verify bulk read data\n");
1788 pga_to_bulk_desc(nob, pg_count, pga, desc);
1790 ctx = req->rq_cli_ctx;
1791 if (ctx->cc_ops->unwrap_bulk)
1792 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1794 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1798 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
1800 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
1801 struct ptlrpc_bulk_desc *desc)
1803 struct ptlrpc_cli_ctx *ctx;
1805 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1808 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
1810 ctx = req->rq_cli_ctx;
1811 if (ctx->cc_ops->unwrap_bulk)
1812 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1816 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
1818 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
1819 struct ptlrpc_bulk_desc *desc)
1821 struct ptlrpc_svc_ctx *ctx;
1823 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1826 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1828 ctx = req->rq_svc_ctx;
1829 if (ctx->sc_policy->sp_sops->wrap_bulk)
1830 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
1834 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
1836 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
1837 struct ptlrpc_bulk_desc *desc)
1839 struct ptlrpc_svc_ctx *ctx;
1841 if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1844 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1846 ctx = req->rq_svc_ctx;
1847 if (ctx->sc_policy->sp_sops->unwrap_bulk);
1848 return ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
1852 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
1855 /****************************************
1856 * user descriptor helpers *
1857 ****************************************/
1859 int sptlrpc_user_desc_size(void)
1862 int ngroups = current_ngroups;
1864 if (ngroups > LUSTRE_MAX_GROUPS)
1865 ngroups = LUSTRE_MAX_GROUPS;
1867 return sizeof(struct ptlrpc_user_desc) + ngroups * sizeof(__u32);
1869 return sizeof(struct ptlrpc_user_desc);
1872 EXPORT_SYMBOL(sptlrpc_user_desc_size);
1874 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
1876 struct ptlrpc_user_desc *pud;
1878 pud = lustre_msg_buf(msg, offset, 0);
1880 pud->pud_uid = cfs_current()->uid;
1881 pud->pud_gid = cfs_current()->gid;
1882 pud->pud_fsuid = cfs_current()->fsuid;
1883 pud->pud_fsgid = cfs_current()->fsgid;
1884 pud->pud_cap = cfs_current()->cap_effective;
1885 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
1889 if (pud->pud_ngroups > current_ngroups)
1890 pud->pud_ngroups = current_ngroups;
1891 memcpy(pud->pud_groups, cfs_current()->group_info->blocks[0],
1892 pud->pud_ngroups * sizeof(__u32));
1893 task_unlock(current);
1898 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
1900 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
1902 struct ptlrpc_user_desc *pud;
1905 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
1909 if (lustre_msg_swabbed(msg)) {
1910 __swab32s(&pud->pud_uid);
1911 __swab32s(&pud->pud_gid);
1912 __swab32s(&pud->pud_fsuid);
1913 __swab32s(&pud->pud_fsgid);
1914 __swab32s(&pud->pud_cap);
1915 __swab32s(&pud->pud_ngroups);
1918 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
1919 CERROR("%u groups is too large\n", pud->pud_ngroups);
1923 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
1924 msg->lm_buflens[offset]) {
1925 CERROR("%u groups are claimed but bufsize only %u\n",
1926 pud->pud_ngroups, msg->lm_buflens[offset]);
1930 if (lustre_msg_swabbed(msg)) {
1931 for (i = 0; i < pud->pud_ngroups; i++)
1932 __swab32s(&pud->pud_groups[i]);
1937 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
1939 /****************************************
1940 * user supplied flavor string parsing *
1941 ****************************************/
1944 int get_default_flavor(enum lustre_part to_part, struct sec_flavor_config *conf)
1946 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1947 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
1948 conf->sfc_flags = 0;
1952 conf->sfc_rpc_flavor = SPTLRPC_FLVR_PLAIN;
1955 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1958 CERROR("Unknown to lustre part %d, apply defaults\n", to_part);
1959 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1965 void get_flavor_by_rpc(__u32 rpc_flavor, struct sec_flavor_config *conf)
1967 conf->sfc_rpc_flavor = rpc_flavor;
1968 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1969 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
1970 conf->sfc_flags = 0;
1972 switch (rpc_flavor) {
1973 case SPTLRPC_FLVR_NULL:
1974 case SPTLRPC_FLVR_PLAIN:
1976 case SPTLRPC_FLVR_KRB5P:
1977 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1979 case SPTLRPC_FLVR_KRB5I:
1980 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
1988 void get_flavor_by_rpc_bulk(__u32 rpc_flavor, int bulk_priv,
1989 struct sec_flavor_config *conf)
1992 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1994 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1996 switch (rpc_flavor) {
1997 case SPTLRPC_FLVR_PLAIN:
1998 conf->sfc_bulk_csum = BULK_CSUM_ALG_MD5;
2000 case SPTLRPC_FLVR_KRB5I:
2001 case SPTLRPC_FLVR_KRB5P:
2002 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
2009 static __u32 __flavors[] = {
2016 #define __nflavors (sizeof(__flavors)/sizeof(__u32))
2019 * flavor string format: rpc[-bulk{n|i|p}[:cksum/enc]]
2025 * krb5i-bulkp:sha512/arc4
2027 int sptlrpc_parse_flavor(enum lustre_part from_part, enum lustre_part to_part,
2028 char *str, struct sec_flavor_config *conf)
2030 char *f, *bulk, *alg, *enc;
2036 if (get_default_flavor(to_part, conf))
2041 for (i = 0; i < __nflavors; i++) {
2042 f = sptlrpc_flavor2name(__flavors[i]);
2043 if (strncmp(str, f, strlen(f)) == 0)
2047 if (i >= __nflavors)
2048 GOTO(invalid, -EINVAL);
2050 /* prepare local buffer thus we can modify it as we want */
2051 strncpy(buf, str, 64);
2054 /* find bulk string */
2055 bulk = strchr(buf, '-');
2059 /* now the first part must equal to rpc flavor name */
2060 if (strcmp(buf, f) != 0)
2061 GOTO(invalid, -EINVAL);
2063 get_flavor_by_rpc(__flavors[i], conf);
2068 /* null flavor should not have any suffix */
2069 if (__flavors[i] == SPTLRPC_FLVR_NULL)
2070 GOTO(invalid, -EINVAL);
2072 /* find bulk algorithm string */
2073 alg = strchr(bulk, ':');
2077 /* verify bulk section */
2078 if (strcmp(bulk, "bulkn") == 0) {
2079 conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
2080 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
2084 if (strcmp(bulk, "bulki") == 0)
2086 else if (strcmp(bulk, "bulkp") == 0)
2089 GOTO(invalid, -EINVAL);
2091 /* plain policy dosen't support bulk encryption */
2092 if (bulk_priv && __flavors[i] == SPTLRPC_FLVR_PLAIN)
2093 GOTO(invalid, -EINVAL);
2095 get_flavor_by_rpc_bulk(__flavors[i], bulk_priv, conf);
2100 /* find encryption algorithm string */
2101 enc = strchr(alg, '/');
2105 /* bulk combination sanity check */
2106 if ((bulk_priv && enc == NULL) || (bulk_priv == 0 && enc))
2107 GOTO(invalid, -EINVAL);
2109 /* checksum algorithm */
2110 for (i = 0; i < BULK_CSUM_ALG_MAX; i++) {
2111 if (strcmp(alg, sptlrpc_bulk_csum_alg2name(i)) == 0) {
2112 conf->sfc_bulk_csum = i;
2116 if (i >= BULK_CSUM_ALG_MAX)
2117 GOTO(invalid, -EINVAL);
2119 /* privacy algorithm */
2121 if (strcmp(enc, "arc4") != 0)
2122 GOTO(invalid, -EINVAL);
2123 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
2127 /* * set ROOTONLY flag:
2130 * * set BULK flag for:
2133 if (to_part == LUSTRE_OST ||
2134 (from_part == LUSTRE_MDT && to_part == LUSTRE_MDT))
2135 conf->sfc_flags |= PTLRPC_SEC_FL_ROOTONLY;
2136 if (from_part == LUSTRE_CLI && to_part == LUSTRE_OST)
2137 conf->sfc_flags |= PTLRPC_SEC_FL_BULK;
2140 __swab32s(&conf->sfc_rpc_flavor);
2141 __swab32s(&conf->sfc_bulk_csum);
2142 __swab32s(&conf->sfc_bulk_priv);
2143 __swab32s(&conf->sfc_flags);
2147 CERROR("invalid flavor string: %s\n", str);
2150 EXPORT_SYMBOL(sptlrpc_parse_flavor);
2152 /****************************************
2154 ****************************************/
2156 const char * sec2target_str(struct ptlrpc_sec *sec)
2158 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2160 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
2162 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2164 EXPORT_SYMBOL(sec2target_str);
2166 /****************************************
2167 * initialize/finalize *
2168 ****************************************/
2170 int sptlrpc_init(void)
2174 rc = sptlrpc_enc_pool_init();
2178 rc = sptlrpc_null_init();
2182 rc = sptlrpc_plain_init();
2186 rc = sptlrpc_lproc_init();
2193 sptlrpc_plain_fini();
2195 sptlrpc_null_fini();
2197 sptlrpc_enc_pool_fini();
2202 void sptlrpc_fini(void)
2204 sptlrpc_lproc_fini();
2205 sptlrpc_plain_fini();
2206 sptlrpc_null_fini();
2207 sptlrpc_enc_pool_fini();