Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / lustre / ptlrpc / sec.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004-2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 #define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26
27 #include <libcfs/libcfs.h>
28 #ifndef __KERNEL__
29 #include <liblustre.h>
30 #include <libcfs/list.h>
31 #else
32 #include <linux/crypto.h>
33 #endif
34
35 #include <obd.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>
42
43 #include "ptlrpc_internal.h"
44
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);
49
50 /***********************************************
51  * policy registers                            *
52  ***********************************************/
53
54 static rwlock_t policy_lock = RW_LOCK_UNLOCKED;
55 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
56         NULL,
57 };
58
59 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
60 {
61         __u32 number = policy->sp_policy;
62
63         LASSERT(policy->sp_name);
64         LASSERT(policy->sp_cops);
65         LASSERT(policy->sp_sops);
66
67         if (number >= SPTLRPC_POLICY_MAX)
68                 return -EINVAL;
69
70         write_lock(&policy_lock);
71         if (unlikely(policies[number])) {
72                 write_unlock(&policy_lock);
73                 return -EALREADY;
74         }
75         policies[number] = policy;
76         write_unlock(&policy_lock);
77
78         CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
79         return 0;
80 }
81 EXPORT_SYMBOL(sptlrpc_register_policy);
82
83 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
84 {
85         __u32 number = policy->sp_policy;
86
87         LASSERT(number < SPTLRPC_POLICY_MAX);
88
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);
93                 return -EINVAL;
94         }
95
96         LASSERT(policies[number] == policy);
97         policies[number] = NULL;
98         write_unlock(&policy_lock);
99
100         CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
101         return 0;
102 }
103 EXPORT_SYMBOL(sptlrpc_unregister_policy);
104
105 static
106 struct ptlrpc_sec_policy * sptlrpc_flavor2policy(ptlrpc_sec_flavor_t flavor)
107 {
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;
112
113         if (number >= SPTLRPC_POLICY_MAX)
114                 return NULL;
115
116 again:
117         read_lock(&policy_lock);
118         policy = policies[number];
119         if (policy && !try_module_get(policy->sp_owner))
120                 policy = NULL;
121         if (policy == NULL)
122                 flag = atomic_read(&loaded);
123         read_unlock(&policy_lock);
124
125 #ifdef CONFIG_KMOD
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");
133                         else
134                                 CWARN("module ptlrpc_gss loaded\n");
135
136                         atomic_set(&loaded, 1);
137                 }
138                 mutex_up(&load_mutex);
139
140                 goto again;
141         }
142 #endif
143
144         return policy;
145 }
146
147 ptlrpc_sec_flavor_t sptlrpc_name2flavor(const char *name)
148 {
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;
159
160         return SPTLRPC_FLVR_INVALID;
161 }
162 EXPORT_SYMBOL(sptlrpc_name2flavor);
163
164 char *sptlrpc_flavor2name(ptlrpc_sec_flavor_t flavor)
165 {
166         switch (flavor) {
167         case SPTLRPC_FLVR_NULL:
168                 return "null";
169         case SPTLRPC_FLVR_PLAIN:
170                 return "plain";
171         case SPTLRPC_FLVR_KRB5:
172                 return "krb5";
173         case SPTLRPC_FLVR_KRB5I:
174                 return "krb5i";
175         case SPTLRPC_FLVR_KRB5P:
176                 return "krb5p";
177         default:
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));
181         }
182         return "UNKNOWN";
183 }
184 EXPORT_SYMBOL(sptlrpc_flavor2name);
185
186 /***********************************************
187  * context helpers                             *
188  * internal APIs                               *
189  * cache management                            *
190  ***********************************************/
191
192 static inline
193 unsigned long ctx_status(struct ptlrpc_cli_ctx *ctx)
194 {
195         smp_mb();
196         return (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK);
197 }
198
199 static inline
200 int ctx_is_uptodate(struct ptlrpc_cli_ctx *ctx)
201 {
202         return (ctx_status(ctx) == PTLRPC_CTX_UPTODATE);
203 }
204
205 static inline
206 int ctx_is_refreshed(struct ptlrpc_cli_ctx *ctx)
207 {
208         return (ctx_status(ctx) != 0);
209 }
210
211 static inline
212 int ctx_is_dead(struct ptlrpc_cli_ctx *ctx)
213 {
214         smp_mb();
215         return ((ctx->cc_flags & (PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR)) != 0);
216 }
217
218 static inline
219 int ctx_is_eternal(struct ptlrpc_cli_ctx *ctx)
220 {
221         smp_mb();
222         return ((ctx->cc_flags & PTLRPC_CTX_ETERNAL) != 0);
223 }
224
225 static
226 int ctx_expire(struct ptlrpc_cli_ctx *ctx)
227 {
228         LASSERT(atomic_read(&ctx->cc_refcount));
229
230         if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
231                 cfs_time_t now = cfs_time_current_sec();
232
233                 smp_mb();
234                 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
235
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));
241                 else
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));
247
248                 return 1;
249         }
250         return 0;
251 }
252
253 static
254 void ctx_enhash(struct ptlrpc_cli_ctx *ctx, struct hlist_head *hash)
255 {
256         set_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags);
257         atomic_inc(&ctx->cc_refcount);
258         hlist_add_head(&ctx->cc_hash, hash);
259 }
260
261 static
262 void ctx_unhash(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
263 {
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));
268
269         clear_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags);
270
271         if (atomic_dec_and_test(&ctx->cc_refcount)) {
272                 __hlist_del(&ctx->cc_hash);
273                 hlist_add_head(&ctx->cc_hash, freelist);
274         } else
275                 hlist_del_init(&ctx->cc_hash);
276 }
277
278 /*
279  * return 1 if the context is dead.
280  */
281 static
282 int ctx_check_death(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
283 {
284         if (unlikely(ctx_is_dead(ctx)))
285                 goto unhash;
286
287         /* expire is 0 means never expire. a newly created gss context
288          * which during upcall also has 0 expiration
289          */
290         smp_mb();
291         if (ctx->cc_expire == 0)
292                 return 0;
293
294         /* check real expiration */
295         smp_mb();
296         if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
297                 return 0;
298
299         ctx_expire(ctx);
300
301 unhash:
302         if (freelist)
303                 ctx_unhash(ctx, freelist);
304
305         return 1;
306 }
307
308 static inline
309 int ctx_check_death_locked(struct ptlrpc_cli_ctx *ctx,
310                            struct hlist_head *freelist)
311 {
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));
316
317         return ctx_check_death(ctx, freelist);
318 }
319
320 static
321 int ctx_check_uptodate(struct ptlrpc_cli_ctx *ctx)
322 {
323         LASSERT(ctx->cc_sec);
324         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
325
326         if (!ctx_check_death(ctx, NULL) && ctx_is_uptodate(ctx))
327                 return 1;
328         return 0;
329 }
330
331 static inline
332 int ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
333 {
334         /* a little bit optimization for null policy */
335         if (!ctx->cc_ops->match)
336                 return 1;
337
338         return ctx->cc_ops->match(ctx, vcred);
339 }
340
341 static
342 void ctx_list_destroy(struct hlist_head *head)
343 {
344         struct ptlrpc_cli_ctx *ctx;
345
346         while (!hlist_empty(head)) {
347                 ctx = hlist_entry(head->first, struct ptlrpc_cli_ctx, cc_hash);
348
349                 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
350                 LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags) == 0);
351
352                 hlist_del_init(&ctx->cc_hash);
353                 sptlrpc_sec_destroy_ctx(ctx->cc_sec, ctx);
354         }
355 }
356
357 static
358 void ctx_cache_gc(struct ptlrpc_sec *sec, struct hlist_head *freelist)
359 {
360         struct ptlrpc_cli_ctx *ctx;
361         struct hlist_node *pos, *next;
362         int i;
363         ENTRY;
364
365         CDEBUG(D_SEC, "do gc on sec %s@%p\n", sec->ps_policy->sp_name, sec);
366
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);
371         }
372
373         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
374         EXIT;
375 }
376
377 /*
378  * @uid: which user. "-1" means flush all.
379  * @grace: mark context DEAD, allow graceful destroy like notify
380  *         server side, etc.
381  * @force: also flush busy entries.
382  *
383  * return the number of busy context encountered.
384  *
385  * In any cases, never touch "eternal" contexts.
386  */
387 static
388 int ctx_cache_flush(struct ptlrpc_sec *sec, uid_t uid, int grace, int force)
389 {
390         struct ptlrpc_cli_ctx *ctx;
391         struct hlist_node *pos, *next;
392         HLIST_HEAD(freelist);
393         int i, busy = 0;
394         ENTRY;
395
396         might_sleep_if(grace);
397
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);
403
404                         if (ctx_is_eternal(ctx))
405                                 continue;
406                         if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
407                                 continue;
408
409                         if (atomic_read(&ctx->cc_refcount) > 1) {
410                                 busy++;
411                                 if (!force)
412                                         continue;
413
414                                 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
415                                       "grace %d\n",
416                                       atomic_read(&ctx->cc_refcount),
417                                       ctx, ctx->cc_vcred.vc_uid,
418                                       sec2target_str(ctx->cc_sec), grace);
419                         }
420                         ctx_unhash(ctx, &freelist);
421
422                         set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
423                         if (!grace)
424                                 clear_bit(PTLRPC_CTX_UPTODATE_BIT,
425                                           &ctx->cc_flags);
426                 }
427         }
428         spin_unlock(&sec->ps_lock);
429
430         ctx_list_destroy(&freelist);
431         RETURN(busy);
432 }
433
434 static inline
435 unsigned int ctx_hash_index(struct ptlrpc_sec *sec, __u64 key)
436 {
437         return (unsigned int) (key & (sec->ps_ccache_size - 1));
438 }
439
440 /*
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.
443  */
444 static
445 struct ptlrpc_cli_ctx * ctx_cache_lookup(struct ptlrpc_sec *sec,
446                                          struct vfs_cred *vcred,
447                                          int create, int remove_dead)
448 {
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;
454         ENTRY;
455
456         might_sleep();
457
458         hash = ctx_hash_index(sec, (__u64) vcred->vc_uid);
459         LASSERT(hash < sec->ps_ccache_size);
460         hash_head = &sec->ps_ccache[hash];
461
462 retry:
463         spin_lock(&sec->ps_lock);
464
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);
469                 gc = 1;
470         }
471
472         hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_hash) {
473                 if (gc == 0 &&
474                     ctx_check_death_locked(ctx, remove_dead ? &freelist : NULL))
475                         continue;
476
477                 if (ctx_match(ctx, vcred)) {
478                         found = 1;
479                         break;
480                 }
481         }
482
483         if (found) {
484                 if (new && new != ctx) {
485                         /* lost the race, just free it */
486                         hlist_add_head(&new->cc_hash, &freelist);
487                         new = NULL;
488                 }
489
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);
494                 }
495         } else {
496                 /* don't allocate for reverse sec */
497                 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
498                         spin_unlock(&sec->ps_lock);
499                         RETURN(NULL);
500                 }
501
502                 if (new) {
503                         ctx_enhash(new, hash_head);
504                         ctx = new;
505                 } else if (create) {
506                         spin_unlock(&sec->ps_lock);
507                         new = sec->ps_policy->sp_cops->create_ctx(sec, vcred);
508                         if (new) {
509                                 atomic_inc(&sec->ps_busy);
510                                 goto retry;
511                         }
512                 } else
513                         ctx = NULL;
514         }
515
516         /* hold a ref */
517         if (ctx)
518                 atomic_inc(&ctx->cc_refcount);
519
520         spin_unlock(&sec->ps_lock);
521
522         /* the allocator of the context must give the first push to refresh */
523         if (new) {
524                 LASSERT(new == ctx);
525                 sptlrpc_ctx_refresh(new);
526         }
527
528         ctx_list_destroy(&freelist);
529         RETURN(ctx);
530 }
531
532 static inline
533 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
534 {
535         struct vfs_cred vcred;
536         int create = 1, remove_dead = 1;
537
538         LASSERT(sec);
539
540         if (sec->ps_flags & (PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY)) {
541                 vcred.vc_uid = 0;
542                 vcred.vc_gid = 0;
543                 if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE) {
544                         create = 0;
545                         remove_dead = 0;
546                 }
547         } else {
548                 vcred.vc_uid = cfs_current()->uid;
549                 vcred.vc_gid = cfs_current()->gid;
550         }
551
552         if (sec->ps_policy->sp_cops->lookup_ctx)
553                 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred);
554         else
555                 return ctx_cache_lookup(sec, &vcred, create, remove_dead);
556 }
557
558 /**************************************************
559  * client context APIs                            *
560  **************************************************/
561
562 static
563 void sptlrpc_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
564 {
565         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
566
567         if (!ctx_is_refreshed(ctx) && ctx->cc_ops->refresh)
568                 ctx->cc_ops->refresh(ctx);
569 }
570
571 struct ptlrpc_cli_ctx *sptlrpc_ctx_get(struct ptlrpc_cli_ctx *ctx)
572 {
573         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
574         atomic_inc(&ctx->cc_refcount);
575         return ctx;
576 }
577 EXPORT_SYMBOL(sptlrpc_ctx_get);
578
579 void sptlrpc_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
580 {
581         struct ptlrpc_sec *sec = ctx->cc_sec;
582
583         LASSERT(sec);
584         LASSERT(atomic_read(&ctx->cc_refcount));
585
586         if (!atomic_dec_and_test(&ctx->cc_refcount))
587                 return;
588
589         LASSERT(test_bit(PTLRPC_CTX_HASHED_BIT, &ctx->cc_flags) == 0);
590         LASSERT(hlist_unhashed(&ctx->cc_hash));
591
592         /* if required async, we must clear the UPTODATE bit to prevent extra
593          * rpcs during destroy procedure.
594          */
595         if (!sync)
596                 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
597
598         /* destroy this context */
599         if (!sptlrpc_sec_destroy_ctx(sec, ctx))
600                 return;
601
602         CWARN("%s@%p: put last ctx, also destroy the sec\n",
603               sec->ps_policy->sp_name, sec);
604
605         sptlrpc_sec_destroy(sec);
606 }
607 EXPORT_SYMBOL(sptlrpc_ctx_put);
608
609 /*
610  * mark a ctx as DEAD, and pull it out from hash table.
611  *
612  * NOTE: the caller must hold at least 1 ref on the ctx.
613  */
614 void sptlrpc_ctx_expire(struct ptlrpc_cli_ctx *ctx)
615 {
616         LASSERT(ctx->cc_sec);
617         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
618
619         ctx_expire(ctx);
620
621         spin_lock(&ctx->cc_sec->ps_lock);
622
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);
626
627                 hlist_del_init(&ctx->cc_hash);
628                 if (atomic_dec_and_test(&ctx->cc_refcount))
629                         LBUG();
630         }
631
632         spin_unlock(&ctx->cc_sec->ps_lock);
633 }
634 EXPORT_SYMBOL(sptlrpc_ctx_expire);
635
636 void sptlrpc_ctx_replace(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *new)
637 {
638         struct ptlrpc_cli_ctx *ctx;
639         struct hlist_node *pos, *next;
640         HLIST_HEAD(freelist);
641         unsigned int hash;
642         ENTRY;
643
644         hash = ctx_hash_index(sec, (__u64) new->cc_vcred.vc_uid);
645         LASSERT(hash < sec->ps_ccache_size);
646
647         spin_lock(&sec->ps_lock);
648
649         hlist_for_each_entry_safe(ctx, pos, next,
650                                   &sec->ps_ccache[hash], cc_hash) {
651                 if (!ctx_match(ctx, &new->cc_vcred))
652                         continue;
653
654                 ctx_expire(ctx);
655                 ctx_unhash(ctx, &freelist);
656                 break;
657         }
658
659         ctx_enhash(new, &sec->ps_ccache[hash]);
660         atomic_inc(&sec->ps_busy);
661
662         spin_unlock(&sec->ps_lock);
663
664         ctx_list_destroy(&freelist);
665         EXIT;
666 }
667 EXPORT_SYMBOL(sptlrpc_ctx_replace);
668
669 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
670 {
671         struct obd_import *imp = req->rq_import;
672         ENTRY;
673
674         LASSERT(!req->rq_cli_ctx);
675         LASSERT(imp);
676
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));
680                 RETURN(-EACCES);
681         }
682
683         req->rq_cli_ctx = get_my_ctx(imp->imp_sec);
684
685         if (!req->rq_cli_ctx) {
686                 CERROR("req %p: fail to get context from cache\n", req);
687                 RETURN(-ENOMEM);
688         }
689
690         RETURN(0);
691 }
692
693 void sptlrpc_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
694 {
695         struct ptlrpc_request *req, *next;
696
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);
701         }
702         spin_unlock(&ctx->cc_lock);
703 }
704 EXPORT_SYMBOL(sptlrpc_ctx_wakeup);
705
706 int sptlrpc_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
707 {
708         LASSERT(ctx->cc_ops);
709
710         if (ctx->cc_ops->display == NULL)
711                 return 0;
712
713         return ctx->cc_ops->display(ctx, buf, bufsize);
714 }
715
716 void sptlrpc_req_put_ctx(struct ptlrpc_request *req)
717 {
718         ENTRY;
719
720         LASSERT(req);
721         LASSERT(req->rq_cli_ctx);
722
723         /* request might be asked to release earlier while still
724          * in the context waiting list.
725          */
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);
730         }
731
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;
735         EXIT;
736 }
737
738 /*
739  * request must have a context. if failed to get new context,
740  * just restore the old one
741  */
742 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
743 {
744         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
745         int rc;
746         ENTRY;
747
748         LASSERT(ctx);
749         LASSERT(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags));
750
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);
755
756         sptlrpc_ctx_get(ctx);
757         sptlrpc_req_put_ctx(req);
758         rc = sptlrpc_req_get_ctx(req);
759         if (!rc) {
760                 LASSERT(req->rq_cli_ctx);
761                 LASSERT(req->rq_cli_ctx != ctx);
762                 sptlrpc_ctx_put(ctx, 1);
763         } else {
764                 LASSERT(!req->rq_cli_ctx);
765                 req->rq_cli_ctx = ctx;
766         }
767         RETURN(rc);
768 }
769 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
770
771 static
772 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
773 {
774         smp_mb();
775         if (ctx_is_refreshed(ctx))
776                 return 1;
777         return 0;
778 }
779
780 static
781 int ctx_refresh_timeout(void *data)
782 {
783         struct ptlrpc_request *req = data;
784         int rc;
785
786         /* conn_cnt is needed in expire_one_request */
787         lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
788
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.
795          */
796         if (rc == 0)
797                 ctx_expire(req->rq_cli_ctx);
798         return rc;
799 }
800
801 static
802 void ctx_refresh_interrupt(void *data)
803 {
804         /* do nothing */
805 }
806
807 /*
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.
811  *
812  * @timeout:
813  *    < 0  - don't wait
814  *    = 0  - wait until success or fatal error occur
815  *    > 0  - timeout value
816  *
817  * return 0 only if the context is uptodated.
818  */
819 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
820 {
821         struct ptlrpc_cli_ctx  *ctx = req->rq_cli_ctx;
822         struct l_wait_info      lwi;
823         int                     rc;
824         ENTRY;
825
826         LASSERT(ctx);
827
828         /* special ctxs */
829         if (ctx_is_eternal(ctx) || req->rq_ctx_init || req->rq_ctx_fini)
830                 RETURN(0);
831
832         /* reverse ctxs, don't refresh */
833         if (ctx->cc_sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
834                 RETURN(0);
835
836         spin_lock(&ctx->cc_lock);
837 again:
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);
842                 RETURN(0);
843         }
844
845         if (test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags)) {
846                 req->rq_err = 1;
847                 if (!list_empty(&req->rq_ctx_chain))
848                         list_del_init(&req->rq_ctx_chain);
849                 spin_unlock(&ctx->cc_lock);
850                 RETURN(-EPERM);
851         }
852
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
862          *
863          * Note we can't simply change xid for resent request because
864          * server reply on it for reply reconstruction.
865          *
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
873          *     context by force.
874          *  2. Current context never be refreshed, then we are fine: we
875          *     never really send request with old context before.
876          */
877         if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
878             req->rq_reqmsg &&
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);
883                 RETURN(0);
884         }
885
886         if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
887                 spin_unlock(&ctx->cc_lock);
888
889                 /* don't have to, but we don't want to release it too soon */
890                 sptlrpc_ctx_get(ctx);
891
892                 rc = sptlrpc_req_replace_dead_ctx(req);
893                 if (rc) {
894                         LASSERT(ctx == req->rq_cli_ctx);
895                         CERROR("req %p: failed to replace dead ctx %p\n",
896                                 req, ctx);
897                         req->rq_err = 1;
898                         LASSERT(list_empty(&req->rq_ctx_chain));
899                         sptlrpc_ctx_put(ctx, 1);
900                         RETURN(-ENOMEM);
901                 }
902
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);
907
908                 sptlrpc_ctx_put(ctx, 1);
909                 ctx = req->rq_cli_ctx;
910                 LASSERT(list_empty(&req->rq_ctx_chain));
911
912                 spin_lock(&ctx->cc_lock);
913                 goto again;
914         }
915
916         /* Now we're sure this context is during upcall, add myself into
917          * waiting list
918          */
919         if (list_empty(&req->rq_ctx_chain))
920                 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
921
922         spin_unlock(&ctx->cc_lock);
923
924         if (timeout < 0) {
925                 RETURN(-EWOULDBLOCK);
926         }
927
928         /* Clear any flags that may be present from previous sends */
929         LASSERT(req->rq_receiving_reply == 0);
930         spin_lock(&req->rq_lock);
931         req->rq_err = 0;
932         req->rq_timedout = 0;
933         req->rq_resend = 0;
934         req->rq_restart = 0;
935         spin_unlock(&req->rq_lock);
936
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);
940
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;
945          * 3. interruptted;
946          * 4. timedout, and we don't want recover from the failure;
947          * 5. timedout, and waked up upon recovery finished;
948          */
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);
953
954                 LASSERT(rc != 0);
955                 RETURN(rc);
956         }
957
958         goto again;
959 }
960
961 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
962 {
963         struct sec_flavor_config *conf;
964
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);
970
971         /* special security flags accoding to opcode */
972         switch (opcode) {
973         case OST_READ:
974                 req->rq_bulk_read = 1;
975                 break;
976         case OST_WRITE:
977                 req->rq_bulk_write = 1;
978                 break;
979         case SEC_CTX_INIT:
980                 req->rq_ctx_init = 1;
981                 break;
982         case SEC_CTX_FINI:
983                 req->rq_ctx_fini = 1;
984                 break;
985         }
986
987         req->rq_sec_flavor = req->rq_cli_ctx->cc_sec->ps_flavor;
988
989         /* force SVC_NONE for context initiation rpc, SVC_AUTH for context
990          * destruction rpc
991          */
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));
1002         }
1003
1004         conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1005
1006         /* user descriptor flag, except ROOTONLY which don't need, and
1007          * null security which can't
1008          */
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;
1012
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;
1018 }
1019
1020 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
1021 {
1022         if (SEC_FLAVOR_SVC(req->rq_sec_flavor) != SPTLRPC_SVC_PRIV)
1023                 return;
1024
1025         LASSERT(req->rq_clrbuf);
1026         if (req->rq_pool || !req->rq_reqbuf)
1027                 return;
1028
1029         OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1030         req->rq_reqbuf = NULL;
1031         req->rq_reqbuf_len = 0;
1032 }
1033
1034 /*
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
1038  */
1039 int sptlrpc_import_check_ctx(struct obd_import *imp)
1040 {
1041         struct ptlrpc_cli_ctx *ctx;
1042         struct ptlrpc_request *req = NULL;
1043         int rc;
1044         ENTRY;
1045
1046         might_sleep();
1047
1048         ctx = get_my_ctx(imp->imp_sec);
1049         if (!ctx)
1050                 RETURN(1);
1051
1052         if (ctx_is_eternal(ctx)) {
1053                 sptlrpc_ctx_put(ctx, 1);
1054                 RETURN(0);
1055         }
1056
1057         OBD_ALLOC_PTR(req);
1058         if (!req)
1059                 RETURN(-ENOMEM);
1060
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;
1067
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);
1071         OBD_FREE_PTR(req);
1072
1073         RETURN(rc);
1074 }
1075
1076 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1077 {
1078         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1079         int rc = 0;
1080         ENTRY;
1081
1082         LASSERT(ctx);
1083         LASSERT(ctx->cc_sec);
1084         LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1085
1086         /* we wrap bulk request here because now we can be sure
1087          * the context is uptodate.
1088          */
1089         if (req->rq_bulk) {
1090                 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1091                 if (rc)
1092                         RETURN(rc);
1093         }
1094
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);
1100                 break;
1101         case SPTLRPC_SVC_PRIV:
1102                 LASSERT(ctx->cc_ops->seal);
1103                 rc = ctx->cc_ops->seal(ctx, req);
1104                 break;
1105         default:
1106                 LBUG();
1107         }
1108
1109         if (rc == 0) {
1110                 LASSERT(req->rq_reqdata_len);
1111                 LASSERT(req->rq_reqdata_len % 8 == 0);
1112                 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1113         }
1114
1115         RETURN(rc);
1116 }
1117
1118 /*
1119  * rq_nob_received is the actual received data length
1120  */
1121 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1122 {
1123         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1124         int rc;
1125         ENTRY;
1126
1127         LASSERT(ctx);
1128         LASSERT(ctx->cc_sec);
1129         LASSERT(ctx->cc_ops);
1130         LASSERT(req->rq_repbuf);
1131
1132         req->rq_repdata_len = req->rq_nob_received;
1133
1134         if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1135                 CERROR("replied data length %d too small\n",
1136                        req->rq_nob_received);
1137                 RETURN(-EPROTO);
1138         }
1139
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
1143                  * in null flavor */
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);
1148                         RETURN(-EPROTO);
1149                 }
1150         } else {
1151                 /* v2 message... */
1152                 ptlrpc_sec_flavor_t tmpf = req->rq_repbuf->lm_secflvr;
1153
1154                 if (req->rq_repbuf->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1155                         __swab32s(&tmpf);
1156
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));
1162                         RETURN(-EPROTO);
1163                 }
1164
1165                 if ((SEC_FLAVOR_POLICY(req->rq_sec_flavor) !=
1166                      SPTLRPC_POLICY_NULL) &&
1167                     lustre_unpack_msg(req->rq_repbuf, req->rq_nob_received))
1168                         RETURN(-EPROTO);
1169         }
1170
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);
1176                 break;
1177         case SPTLRPC_SVC_PRIV:
1178                 LASSERT(ctx->cc_ops->unseal);
1179                 rc = ctx->cc_ops->unseal(ctx, req);
1180                 break;
1181         default:
1182                 LBUG();
1183         }
1184
1185         LASSERT(rc || req->rq_repmsg || req->rq_resend);
1186         RETURN(rc);
1187 }
1188
1189 /**************************************************
1190  * security APIs                                  *
1191  **************************************************/
1192
1193 /*
1194  * let policy module to determine whether take refrence of
1195  * import or not.
1196  */
1197 static
1198 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1199                                        struct ptlrpc_svc_ctx *ctx,
1200                                        __u32 flavor,
1201                                        unsigned long flags)
1202 {
1203         struct ptlrpc_sec_policy *policy;
1204         struct ptlrpc_sec *sec;
1205         ENTRY;
1206
1207         flavor = SEC_FLAVOR_RPC(flavor);
1208
1209         if (ctx) {
1210                 LASSERT(imp->imp_dlm_fake == 1);
1211
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));
1216
1217                 policy = sptlrpc_policy_get(ctx->sc_policy);
1218                 flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1219         } else {
1220                 LASSERT(imp->imp_dlm_fake == 0);
1221
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));
1226
1227                 policy = sptlrpc_flavor2policy(flavor);
1228                 if (!policy) {
1229                         CERROR("invalid flavor 0x%x\n", flavor);
1230                         RETURN(NULL);
1231                 }
1232         }
1233
1234         sec = policy->sp_cops->create_sec(imp, ctx, flavor, flags);
1235         if (sec) {
1236                 atomic_inc(&sec->ps_refcount);
1237
1238                 /* take 1 busy count on behalf of sec itself,
1239                  * balanced in sptlrpc_set_put()
1240                  */
1241                 atomic_inc(&sec->ps_busy);
1242         } else
1243                 sptlrpc_policy_put(policy);
1244
1245         RETURN(sec);
1246 }
1247
1248 static
1249 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1250 {
1251         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1252
1253         LASSERT(policy);
1254         LASSERT(atomic_read(&sec->ps_refcount) == 0);
1255         LASSERT(atomic_read(&sec->ps_busy) == 0);
1256         LASSERT(policy->sp_cops->destroy_sec);
1257
1258         policy->sp_cops->destroy_sec(sec);
1259         sptlrpc_policy_put(policy);
1260 }
1261
1262 static
1263 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1264 {
1265         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1266
1267         if (!atomic_dec_and_test(&sec->ps_refcount)) {
1268                 sptlrpc_policy_put(policy);
1269                 return;
1270         }
1271
1272         ctx_cache_flush(sec, -1, 1, 1);
1273
1274         if (atomic_dec_and_test(&sec->ps_busy))
1275                 sptlrpc_sec_destroy(sec);
1276         else
1277                 CWARN("delay to destroy %s@%p: busy contexts\n",
1278                       policy->sp_name, sec);
1279 }
1280
1281 /*
1282  * return 1 means we should also destroy the sec structure.
1283  * normally return 0
1284  */
1285 static
1286 int sptlrpc_sec_destroy_ctx(struct ptlrpc_sec *sec,
1287                             struct ptlrpc_cli_ctx *ctx)
1288 {
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);
1295
1296         sec->ps_policy->sp_cops->destroy_ctx(sec, ctx);
1297
1298         if (atomic_dec_and_test(&sec->ps_busy)) {
1299                 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1300                 return 1;
1301         }
1302
1303         return 0;
1304 }
1305
1306 /*
1307  * when complete successfully, req->rq_reqmsg should point to the
1308  * right place.
1309  */
1310 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1311 {
1312         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1313         struct ptlrpc_sec_policy *policy;
1314         int rc;
1315
1316         LASSERT(ctx);
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);
1321
1322         policy = ctx->cc_sec->ps_policy;
1323         rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1324         if (!rc) {
1325                 LASSERT(req->rq_reqmsg);
1326                 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1327
1328                 /* zeroing preallocated buffer */
1329                 if (req->rq_pool)
1330                         memset(req->rq_reqmsg, 0, msgsize);
1331         }
1332
1333         return rc;
1334 }
1335
1336 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1337 {
1338         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1339         struct ptlrpc_sec_policy *policy;
1340
1341         LASSERT(ctx);
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);
1346
1347         policy = ctx->cc_sec->ps_policy;
1348         policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1349 }
1350
1351 /*
1352  * NOTE caller must guarantee the buffer size is enough for the enlargement
1353  */
1354 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1355                                   int segment, int newsize)
1356 {
1357         void   *src, *dst;
1358         int     oldsize, oldmsg_size, movesize;
1359
1360         LASSERT(segment < msg->lm_bufcount);
1361         LASSERT(msg->lm_buflens[segment] <= newsize);
1362
1363         if (msg->lm_buflens[segment] == newsize)
1364                 return;
1365
1366         /* nothing to do if we are enlarging the last segment */
1367         if (segment == msg->lm_bufcount - 1) {
1368                 msg->lm_buflens[segment] = newsize;
1369                 return;
1370         }
1371
1372         oldsize = msg->lm_buflens[segment];
1373
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;
1378
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);
1384
1385         if (movesize)
1386                 memmove(dst, src, movesize);
1387
1388         /* note we don't clear the ares where old data live, not secret */
1389
1390         /* finally set new segment size */
1391         msg->lm_buflens[segment] = newsize;
1392 }
1393 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1394
1395 /*
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.
1399  *
1400  * caller's attention: upon return, rq_reqmsg and rq_reqlen might have
1401  * been changed.
1402  */
1403 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1404                                int segment, int newsize)
1405 {
1406         struct ptlrpc_cli_ctx    *ctx = req->rq_cli_ctx;
1407         struct ptlrpc_sec_cops   *cops;
1408         struct lustre_msg        *msg = req->rq_reqmsg;
1409
1410         LASSERT(ctx);
1411         LASSERT(msg);
1412         LASSERT(msg->lm_bufcount > segment);
1413         LASSERT(msg->lm_buflens[segment] <= newsize);
1414
1415         if (msg->lm_buflens[segment] == newsize)
1416                 return 0;
1417
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);
1421 }
1422 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1423
1424 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1425 {
1426         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1427         struct ptlrpc_sec_policy *policy;
1428         ENTRY;
1429
1430         LASSERT(ctx);
1431         LASSERT(atomic_read(&ctx->cc_refcount));
1432         LASSERT(ctx->cc_sec);
1433         LASSERT(ctx->cc_sec->ps_policy);
1434
1435         if (req->rq_repbuf)
1436                 RETURN(0);
1437
1438         policy = ctx->cc_sec->ps_policy;
1439         RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1440 }
1441
1442 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1443 {
1444         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1445         struct ptlrpc_sec_policy *policy;
1446         ENTRY;
1447
1448         LASSERT(ctx);
1449         LASSERT(atomic_read(&ctx->cc_refcount));
1450         LASSERT(ctx->cc_sec);
1451         LASSERT(ctx->cc_sec->ps_policy);
1452         LASSERT(req->rq_repbuf);
1453
1454         policy = ctx->cc_sec->ps_policy;
1455         policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1456         EXIT;
1457 }
1458
1459 int sptlrpc_import_get_sec(struct obd_import *imp,
1460                            struct ptlrpc_svc_ctx *ctx,
1461                            __u32 flavor,
1462                            unsigned long flags)
1463 {
1464         struct obd_device *obd = imp->imp_obd;
1465         ENTRY;
1466
1467         LASSERT(obd);
1468         LASSERT(obd->obd_type);
1469
1470         /* old sec might be still there in reconnecting */
1471         if (imp->imp_sec)
1472                 RETURN(0);
1473
1474         imp->imp_sec = sptlrpc_sec_create(imp, ctx, flavor, flags);
1475         if (!imp->imp_sec)
1476                 RETURN(-EINVAL);
1477
1478         RETURN(0);
1479 }
1480
1481 void sptlrpc_import_put_sec(struct obd_import *imp)
1482 {
1483         if (imp->imp_sec == NULL)
1484                 return;
1485
1486         sptlrpc_sec_put(imp->imp_sec);
1487         imp->imp_sec = NULL;
1488 }
1489
1490 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1491 {
1492         if (imp == NULL || imp->imp_sec == NULL)
1493                 return;
1494
1495         /* use 'grace' mode, it's crutial see explain in
1496          * sptlrpc_req_refresh_ctx()
1497          */
1498         ctx_cache_flush(imp->imp_sec, 0, 1, 1);
1499 }
1500
1501 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1502 {
1503         if (imp == NULL || imp->imp_sec == NULL)
1504                 return;
1505
1506         ctx_cache_flush(imp->imp_sec, cfs_current()->uid, 1, 1);
1507 }
1508 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1509
1510 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1511 {
1512         if (imp == NULL || imp->imp_sec == NULL)
1513                 return;
1514
1515         ctx_cache_flush(imp->imp_sec, -1, 0, 1);
1516 }
1517 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1518
1519 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1520                                 struct ptlrpc_cli_ctx *ctx)
1521 {
1522         struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1523
1524         if (!policy->sp_cops->install_rctx)
1525                 return 0;
1526         return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1527 }
1528
1529 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1530                                 struct ptlrpc_svc_ctx *ctx)
1531 {
1532         struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1533
1534         if (!policy->sp_sops->install_rctx)
1535                 return 0;
1536         return policy->sp_sops->install_rctx(imp, ctx);
1537 }
1538
1539 /****************************************
1540  * server side security                 *
1541  ****************************************/
1542
1543 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
1544 {
1545         struct ptlrpc_sec_policy *policy;
1546         struct lustre_msg *msg = req->rq_reqbuf;
1547         int rc;
1548         ENTRY;
1549
1550         LASSERT(msg);
1551         LASSERT(req->rq_reqmsg == NULL);
1552         LASSERT(req->rq_repmsg == NULL);
1553
1554         /* 
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().
1557          */
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);
1561         }
1562
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;
1566         } else {
1567                 req->rq_sec_flavor = msg->lm_secflvr;
1568
1569                 if (msg->lm_magic == LUSTRE_MSG_MAGIC_V2_SWABBED)
1570                         __swab32s(&req->rq_sec_flavor);
1571
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);
1576         }
1577
1578         policy = sptlrpc_flavor2policy(req->rq_sec_flavor);
1579         if (!policy) {
1580                 CERROR("unsupported security flavor %x\n", req->rq_sec_flavor);
1581                 RETURN(SECSVC_DROP);
1582         }
1583
1584         LASSERT(policy->sp_sops->accept);
1585         rc = policy->sp_sops->accept(req);
1586
1587         LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
1588         sptlrpc_policy_put(policy);
1589
1590         /* FIXME move to proper place */
1591         if (rc == SECSVC_OK) {
1592                 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1593
1594                 if (opc == OST_WRITE)
1595                         req->rq_bulk_write = 1;
1596                 else if (opc == OST_READ)
1597                         req->rq_bulk_read = 1;
1598         }
1599
1600         LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
1601         RETURN(rc);
1602 }
1603
1604 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req,
1605                          int msglen)
1606 {
1607         struct ptlrpc_sec_policy *policy;
1608         struct ptlrpc_reply_state *rs;
1609         int rc;
1610         ENTRY;
1611
1612         LASSERT(req->rq_svc_ctx);
1613         LASSERT(req->rq_svc_ctx->sc_policy);
1614
1615         policy = req->rq_svc_ctx->sc_policy;
1616         LASSERT(policy->sp_sops->alloc_rs);
1617
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);
1622                 if (rs == NULL)
1623                         RETURN(-ENOMEM);
1624
1625                 req->rq_reply_state = rs;
1626                 rc = policy->sp_sops->alloc_rs(req, msglen);
1627                 if (rc) {
1628                         lustre_put_emerg_rs(rs);
1629                         req->rq_reply_state = NULL;
1630                 }
1631         }
1632
1633         LASSERT(rc != 0 ||
1634                 (req->rq_reply_state && req->rq_reply_state->rs_msg));
1635
1636         RETURN(rc);
1637 }
1638
1639 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
1640 {
1641         struct ptlrpc_sec_policy *policy;
1642         int rc;
1643         ENTRY;
1644
1645         LASSERT(req->rq_svc_ctx);
1646         LASSERT(req->rq_svc_ctx->sc_policy);
1647
1648         policy = req->rq_svc_ctx->sc_policy;
1649         LASSERT(policy->sp_sops->authorize);
1650
1651         rc = policy->sp_sops->authorize(req);
1652         LASSERT(rc || req->rq_reply_state->rs_repdata_len);
1653
1654         RETURN(rc);
1655 }
1656
1657 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
1658 {
1659         struct ptlrpc_sec_policy *policy;
1660         unsigned int prealloc;
1661         ENTRY;
1662
1663         LASSERT(rs->rs_svc_ctx);
1664         LASSERT(rs->rs_svc_ctx->sc_policy);
1665
1666         policy = rs->rs_svc_ctx->sc_policy;
1667         LASSERT(policy->sp_sops->free_rs);
1668
1669         prealloc = rs->rs_prealloc;
1670         policy->sp_sops->free_rs(rs);
1671
1672         if (prealloc)
1673                 lustre_put_emerg_rs(rs);
1674         EXIT;
1675 }
1676
1677 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
1678 {
1679         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1680
1681         if (ctx == NULL)
1682                 return;
1683
1684         LASSERT(atomic_read(&ctx->sc_refcount) > 0);
1685         atomic_inc(&ctx->sc_refcount);
1686 }
1687
1688 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
1689 {
1690         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1691
1692         if (ctx == NULL)
1693                 return;
1694
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);
1699         }
1700         req->rq_svc_ctx = NULL;
1701 }
1702
1703 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
1704 {
1705         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
1706
1707         if (ctx == NULL)
1708                 return;
1709
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);
1713 }
1714 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
1715
1716 /****************************************
1717  * bulk security                        *
1718  ****************************************/
1719
1720 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
1721                           struct ptlrpc_bulk_desc *desc)
1722 {
1723         struct ptlrpc_cli_ctx *ctx;
1724
1725         if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1726                 return 0;
1727
1728         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1729
1730         ctx = req->rq_cli_ctx;
1731         if (ctx->cc_ops->wrap_bulk)
1732                 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
1733         return 0;
1734 }
1735 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
1736
1737 static
1738 void pga_to_bulk_desc(int nob, obd_count pg_count, struct brw_page **pga,
1739                       struct ptlrpc_bulk_desc *desc)
1740 {
1741         int i;
1742
1743         LASSERT(pga);
1744         LASSERT(*pga);
1745
1746         for (i = 0; i < pg_count && nob > 0; i++) {
1747 #ifdef __KERNEL__
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;
1752 #else
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;
1757 #endif
1758
1759                 desc->bd_iov_count++;
1760                 nob -= pga[i]->count;
1761         }
1762 }
1763
1764 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
1765                                  int nob, obd_count pg_count,
1766                                  struct brw_page **pga)
1767 {
1768         struct ptlrpc_bulk_desc *desc;
1769         struct ptlrpc_cli_ctx *ctx;
1770         int rc = 0;
1771
1772         if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1773                 return 0;
1774
1775         LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
1776
1777         OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1778         if (desc == NULL) {
1779                 CERROR("out of memory, can't verify bulk read data\n");
1780                 return -ENOMEM;
1781         }
1782
1783         pga_to_bulk_desc(nob, pg_count, pga, desc);
1784
1785         ctx = req->rq_cli_ctx;
1786         if (ctx->cc_ops->unwrap_bulk)
1787                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1788
1789         OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[pg_count]));
1790
1791         return rc;
1792 }
1793 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
1794
1795 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
1796                                   struct ptlrpc_bulk_desc *desc)
1797 {
1798         struct ptlrpc_cli_ctx *ctx;
1799
1800         if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1801                 return 0;
1802
1803         LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
1804
1805         ctx = req->rq_cli_ctx;
1806         if (ctx->cc_ops->unwrap_bulk)
1807                 return ctx->cc_ops->unwrap_bulk(ctx, req, desc);
1808
1809         return 0;
1810 }
1811 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
1812
1813 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
1814                           struct ptlrpc_bulk_desc *desc)
1815 {
1816         struct ptlrpc_svc_ctx *ctx;
1817
1818         if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1819                 return 0;
1820
1821         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1822
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);
1826
1827         return 0;
1828 }
1829 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
1830
1831 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
1832                             struct ptlrpc_bulk_desc *desc)
1833 {
1834         struct ptlrpc_svc_ctx *ctx;
1835
1836         if (!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor))
1837                 return 0;
1838
1839         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
1840
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);
1844
1845         return 0;
1846 }
1847 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
1848
1849
1850 /****************************************
1851  * user descriptor helpers              *
1852  ****************************************/
1853
1854 int sptlrpc_current_user_desc_size(void)
1855 {
1856         int ngroups;
1857
1858 #ifdef __KERNEL__
1859         ngroups = current_ngroups;
1860
1861         if (ngroups > LUSTRE_MAX_GROUPS)
1862                 ngroups = LUSTRE_MAX_GROUPS;
1863 #else
1864         ngroups = 0;
1865 #endif
1866         return sptlrpc_user_desc_size(ngroups);
1867 }
1868 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
1869
1870 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
1871 {
1872         struct ptlrpc_user_desc *pud;
1873
1874         pud = lustre_msg_buf(msg, offset, 0);
1875
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;
1882
1883 #ifdef __KERNEL__
1884         task_lock(current);
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);
1890 #endif
1891
1892         return 0;
1893 }
1894 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
1895
1896 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset)
1897 {
1898         struct ptlrpc_user_desc *pud;
1899         int                      i;
1900
1901         pud = lustre_msg_buf(msg, offset, sizeof(*pud));
1902         if (!pud)
1903                 return -EINVAL;
1904
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);
1912         }
1913
1914         if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
1915                 CERROR("%u groups is too large\n", pud->pud_ngroups);
1916                 return -EINVAL;
1917         }
1918
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]);
1923                 return -EINVAL;
1924         }
1925
1926         if (lustre_msg_swabbed(msg)) {
1927                 for (i = 0; i < pud->pud_ngroups; i++)
1928                         __swab32s(&pud->pud_groups[i]);
1929         }
1930
1931         return 0;
1932 }
1933 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
1934
1935 /****************************************
1936  * user supplied flavor string parsing  *
1937  ****************************************/
1938
1939 static
1940 int get_default_flavor(enum lustre_part to_part, struct sec_flavor_config *conf)
1941 {
1942         conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1943         conf->sfc_bulk_csum = BULK_CSUM_ALG_NULL;
1944         conf->sfc_flags = 0;
1945
1946         switch (to_part) {
1947         case LUSTRE_MDT:
1948                 conf->sfc_rpc_flavor = SPTLRPC_FLVR_PLAIN;
1949                 return 0;
1950         case LUSTRE_OST:
1951                 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1952                 return 0;
1953         default:
1954                 CERROR("Unknown to lustre part %d, apply defaults\n", to_part);
1955                 conf->sfc_rpc_flavor = SPTLRPC_FLVR_NULL;
1956                 return -EINVAL;
1957         }
1958 }
1959
1960 static
1961 void get_flavor_by_rpc(__u32 rpc_flavor, struct sec_flavor_config *conf)
1962 {
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;
1967
1968         switch (rpc_flavor) {
1969         case SPTLRPC_FLVR_NULL:
1970         case SPTLRPC_FLVR_PLAIN:
1971                 break;
1972         case SPTLRPC_FLVR_KRB5P:
1973                 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1974                 /* fall through */
1975         case SPTLRPC_FLVR_KRB5I:
1976                 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
1977                 break;
1978         default:
1979                 LBUG();
1980         }
1981 }
1982
1983 static
1984 void get_flavor_by_rpc_bulk(__u32 rpc_flavor, int bulk_priv,
1985                             struct sec_flavor_config *conf)
1986 {
1987         if (bulk_priv)
1988                 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
1989         else
1990                 conf->sfc_bulk_priv = BULK_PRIV_ALG_NULL;
1991
1992         switch (rpc_flavor) {
1993         case SPTLRPC_FLVR_PLAIN:
1994                 conf->sfc_bulk_csum = BULK_CSUM_ALG_MD5;
1995                 break;
1996         case SPTLRPC_FLVR_KRB5I:
1997         case SPTLRPC_FLVR_KRB5P:
1998                 conf->sfc_bulk_csum = BULK_CSUM_ALG_SHA1;
1999                 break;
2000         default:
2001                 LBUG();
2002         }
2003 }
2004
2005 static __u32 __flavors[] = {
2006         SPTLRPC_FLVR_NULL,
2007         SPTLRPC_FLVR_PLAIN,
2008         SPTLRPC_FLVR_KRB5I,
2009         SPTLRPC_FLVR_KRB5P,
2010 };
2011
2012 #define __nflavors      (sizeof(__flavors)/sizeof(__u32))
2013
2014 /*
2015  * flavor string format: rpc[-bulk{n|i|p}[:cksum/enc]]
2016  * for examples:
2017  *  null
2018  *  plain-bulki
2019  *  krb5p-bulkn
2020  *  krb5i-bulkp
2021  *  krb5i-bulkp:sha512/arc4
2022  */
2023 int sptlrpc_parse_flavor(enum lustre_part from_part, enum lustre_part to_part,
2024                          char *str, struct sec_flavor_config *conf)
2025 {
2026         char   *f, *bulk, *alg, *enc;
2027         char    buf[64];
2028         int     i, bulk_priv;
2029         ENTRY;
2030
2031         if (str == NULL) {
2032                 if (get_default_flavor(to_part, conf))
2033                         return -EINVAL;
2034                 goto set_flags;
2035         }
2036
2037         for (i = 0; i < __nflavors; i++) {
2038                 f = sptlrpc_flavor2name(__flavors[i]);
2039                 if (strncmp(str, f, strlen(f)) == 0)
2040                         break;
2041         }
2042
2043         if (i >= __nflavors)
2044                 GOTO(invalid, -EINVAL);
2045
2046         /* prepare local buffer thus we can modify it as we want */
2047         strncpy(buf, str, 64);
2048         buf[64 - 1] = '\0';
2049
2050         /* find bulk string */
2051         bulk = strchr(buf, '-');
2052         if (bulk)
2053                 *bulk++ = '\0';
2054
2055         /* now the first part must equal to rpc flavor name */
2056         if (strcmp(buf, f) != 0)
2057                 GOTO(invalid, -EINVAL);
2058
2059         get_flavor_by_rpc(__flavors[i], conf);
2060
2061         if (bulk == NULL)
2062                 goto set_flags;
2063
2064         /* null flavor should not have any suffix */
2065         if (__flavors[i] == SPTLRPC_FLVR_NULL)
2066                 GOTO(invalid, -EINVAL);
2067
2068         /* find bulk algorithm string */
2069         alg = strchr(bulk, ':');
2070         if (alg)
2071                 *alg++ = '\0';
2072
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;
2077                 goto set_flags;
2078         }
2079
2080         if (strcmp(bulk, "bulki") == 0)
2081                 bulk_priv = 0;
2082         else if (strcmp(bulk, "bulkp") == 0)
2083                 bulk_priv = 1;
2084         else
2085                 GOTO(invalid, -EINVAL);
2086
2087         /* plain policy dosen't support bulk encryption */
2088         if (bulk_priv && __flavors[i] == SPTLRPC_FLVR_PLAIN)
2089                 GOTO(invalid, -EINVAL);
2090
2091         get_flavor_by_rpc_bulk(__flavors[i], bulk_priv, conf);
2092
2093         if (alg == NULL)
2094                 goto set_flags;
2095
2096         /* find encryption algorithm string */
2097         enc = strchr(alg, '/');
2098         if (enc)
2099                 *enc++ = '\0';
2100
2101         /* bulk combination sanity check */
2102         if ((bulk_priv && enc == NULL) || (bulk_priv == 0 && enc))
2103                 GOTO(invalid, -EINVAL);
2104
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;
2109                         break;
2110                 }
2111         }
2112         if (i >= BULK_CSUM_ALG_MAX)
2113                 GOTO(invalid, -EINVAL);
2114
2115         /* privacy algorithm */
2116         if (enc) {
2117                 if (strcmp(enc, "arc4") != 0)
2118                         GOTO(invalid, -EINVAL);
2119                 conf->sfc_bulk_priv = BULK_PRIV_ALG_ARC4;
2120         }
2121
2122 set_flags:
2123         /* * set ROOTONLY flag:
2124          *   - to OST
2125          *   - from MDT to MDT
2126          * * set BULK flag for:
2127          *   - from CLI to OST
2128          */
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;
2134
2135 #ifdef __BIG_ENDIAN
2136         __swab32s(&conf->sfc_rpc_flavor);
2137         __swab32s(&conf->sfc_bulk_csum);
2138         __swab32s(&conf->sfc_bulk_priv);
2139         __swab32s(&conf->sfc_flags);
2140 #endif
2141         return 0;
2142 invalid:
2143         CERROR("invalid flavor string: %s\n", str);
2144         return -EINVAL;
2145 }
2146 EXPORT_SYMBOL(sptlrpc_parse_flavor);
2147
2148 /****************************************
2149  * misc helpers                         *
2150  ****************************************/
2151
2152 const char * sec2target_str(struct ptlrpc_sec *sec)
2153 {
2154         if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2155                 return "*";
2156         if (sec->ps_flags & PTLRPC_SEC_FL_REVERSE)
2157                 return "c";
2158         return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2159 }
2160 EXPORT_SYMBOL(sec2target_str);
2161
2162 /****************************************
2163  * initialize/finalize                  *
2164  ****************************************/
2165
2166 int sptlrpc_init(void)
2167 {
2168         int rc;
2169
2170         rc = sptlrpc_enc_pool_init();
2171         if (rc)
2172                 goto out;
2173
2174         rc = sptlrpc_null_init();
2175         if (rc)
2176                 goto out_pool;
2177
2178         rc = sptlrpc_plain_init();
2179         if (rc)
2180                 goto out_null;
2181
2182         rc = sptlrpc_lproc_init();
2183         if (rc)
2184                 goto out_plain;
2185
2186         return 0;
2187
2188 out_plain:
2189         sptlrpc_plain_fini();
2190 out_null:
2191         sptlrpc_null_fini();
2192 out_pool:
2193         sptlrpc_enc_pool_fini();
2194 out:
2195         return rc;
2196 }
2197
2198 void sptlrpc_fini(void)
2199 {
2200         sptlrpc_lproc_fini();
2201         sptlrpc_plain_fini();
2202         sptlrpc_null_fini();
2203         sptlrpc_enc_pool_fini();
2204 }