Whamcloud - gitweb
b=23728 gss: allow oss authenticate with mgs.
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_svc_upcall.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  *
6  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  * Neil Brown <neilb@cse.unsw.edu.au>
13  * J. Bruce Fields <bfields@umich.edu>
14  * Andy Adamson <andros@umich.edu>
15  * Dug Song <dugsong@monkey.org>
16  *
17  * RPCSEC_GSS server authentication.
18  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
19  * (gssapi)
20  *
21  * The RPCSEC_GSS involves three stages:
22  *  1/ context creation
23  *  2/ data exchange
24  *  3/ context destruction
25  *
26  * Context creation is handled largely by upcalls to user-space.
27  *  In particular, GSS_Accept_sec_context is handled by an upcall
28  * Data exchange is handled entirely within the kernel
29  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
30  * Context destruction is handled in-kernel
31  *  GSS_Delete_sec_context is in-kernel
32  *
33  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
34  * The context handle and gss_token are used as a key into the rpcsec_init cache.
35  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
36  * being major_status, minor_status, context_handle, reply_token.
37  * These are sent back to the client.
38  * Sequence window management is handled by the kernel.  The window size if currently
39  * a compile time constant.
40  *
41  * When user-space is happy that a context is established, it places an entry
42  * in the rpcsec_context cache. The key for this cache is the context_handle.
43  * The content includes:
44  *   uid/gidlist - for determining access rights
45  *   mechanism type
46  *   mechanism specific information, such as a key
47  *
48  */
49
50 #define DEBUG_SUBSYSTEM S_SEC
51 #ifdef __KERNEL__
52 #include <linux/types.h>
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/hash.h>
57 #include <linux/mutex.h>
58 #include <linux/sunrpc/cache.h>
59 #else
60 #include <liblustre.h>
61 #endif
62
63 #include <obd.h>
64 #include <obd_class.h>
65 #include <obd_support.h>
66 #include <lustre/lustre_idl.h>
67 #include <lustre_net.h>
68 #include <lustre_import.h>
69 #include <lustre_sec.h>
70
71 #include "gss_err.h"
72 #include "gss_internal.h"
73 #include "gss_api.h"
74
75 #define GSS_SVC_UPCALL_TIMEOUT  (20)
76
77 static cfs_spinlock_t __ctx_index_lock;
78 static __u64 __ctx_index;
79
80 __u64 gss_get_next_ctx_index(void)
81 {
82         __u64 idx;
83
84         cfs_spin_lock(&__ctx_index_lock);
85         idx = __ctx_index++;
86         cfs_spin_unlock(&__ctx_index_lock);
87
88         return idx;
89 }
90
91 static inline unsigned long hash_mem(char *buf, int length, int bits)
92 {
93         unsigned long hash = 0;
94         unsigned long l = 0;
95         int len = 0;
96         unsigned char c;
97
98         do {
99                 if (len == length) {
100                         c = (char) len;
101                         len = -1;
102                 } else
103                         c = *buf++;
104
105                 l = (l << 8) | c;
106                 len++;
107
108                 if ((len & (BITS_PER_LONG/8-1)) == 0)
109                         hash = cfs_hash_long(hash^l, BITS_PER_LONG);
110         } while (len);
111
112         return hash >> (BITS_PER_LONG - bits);
113 }
114
115 /****************************************
116  * rsi cache                            *
117  ****************************************/
118
119 #define RSI_HASHBITS    (6)
120 #define RSI_HASHMAX     (1 << RSI_HASHBITS)
121 #define RSI_HASHMASK    (RSI_HASHMAX - 1)
122
123 struct rsi {
124         struct cache_head       h;
125         __u32                   lustre_svc;
126         __u64                   nid;
127         cfs_waitq_t             waitq;
128         rawobj_t                in_handle, in_token;
129         rawobj_t                out_handle, out_token;
130         int                     major_status, minor_status;
131 };
132
133 static struct cache_head *rsi_table[RSI_HASHMAX];
134 static struct cache_detail rsi_cache;
135 #ifdef HAVE_SUNRPC_CACHE_V2
136 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
137 static struct rsi *rsi_lookup(struct rsi *item);
138 #else
139 static struct rsi *rsi_lookup(struct rsi *item, int set);
140 #endif
141
142 static inline int rsi_hash(struct rsi *item)
143 {
144         return hash_mem((char *)item->in_handle.data, item->in_handle.len,
145                         RSI_HASHBITS) ^
146                hash_mem((char *)item->in_token.data, item->in_token.len,
147                         RSI_HASHBITS);
148 }
149
150 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
151 {
152         return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
153                 rawobj_equal(&item->in_token, &tmp->in_token));
154 }
155
156 static void rsi_free(struct rsi *rsi)
157 {
158         rawobj_free(&rsi->in_handle);
159         rawobj_free(&rsi->in_token);
160         rawobj_free(&rsi->out_handle);
161         rawobj_free(&rsi->out_token);
162 }
163
164 static void rsi_request(struct cache_detail *cd,
165                         struct cache_head *h,
166                         char **bpp, int *blen)
167 {
168         struct rsi *rsi = container_of(h, struct rsi, h);
169         __u64 index = 0;
170
171         /* if in_handle is null, provide kernel suggestion */
172         if (rsi->in_handle.len == 0)
173                 index = gss_get_next_ctx_index();
174
175         qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
176                      sizeof(rsi->lustre_svc));
177         qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
178         qword_addhex(bpp, blen, (char *) &index, sizeof(index));
179         qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
180         qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
181         (*bpp)[-1] = '\n';
182 }
183
184 static inline void __rsi_init(struct rsi *new, struct rsi *item)
185 {
186         new->out_handle = RAWOBJ_EMPTY;
187         new->out_token = RAWOBJ_EMPTY;
188
189         new->in_handle = item->in_handle;
190         item->in_handle = RAWOBJ_EMPTY;
191         new->in_token = item->in_token;
192         item->in_token = RAWOBJ_EMPTY;
193
194         new->lustre_svc = item->lustre_svc;
195         new->nid = item->nid;
196         cfs_waitq_init(&new->waitq);
197 }
198
199 static inline void __rsi_update(struct rsi *new, struct rsi *item)
200 {
201         LASSERT(new->out_handle.len == 0);
202         LASSERT(new->out_token.len == 0);
203
204         new->out_handle = item->out_handle;
205         item->out_handle = RAWOBJ_EMPTY;
206         new->out_token = item->out_token;
207         item->out_token = RAWOBJ_EMPTY;
208
209         new->major_status = item->major_status;
210         new->minor_status = item->minor_status;
211 }
212
213 #ifdef HAVE_SUNRPC_CACHE_V2
214
215 static void rsi_put(struct kref *ref)
216 {
217         struct rsi *rsi = container_of(ref, struct rsi, h.ref);
218
219         LASSERT(rsi->h.next == NULL);
220         rsi_free(rsi);
221         OBD_FREE_PTR(rsi);
222 }
223
224 static int rsi_match(struct cache_head *a, struct cache_head *b)
225 {
226         struct rsi *item = container_of(a, struct rsi, h);
227         struct rsi *tmp = container_of(b, struct rsi, h);
228
229         return __rsi_match(item, tmp);
230 }
231
232 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
233 {
234         struct rsi *new = container_of(cnew, struct rsi, h);
235         struct rsi *item = container_of(citem, struct rsi, h);
236
237         __rsi_init(new, item);
238 }
239
240 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
241 {
242         struct rsi *new = container_of(cnew, struct rsi, h);
243         struct rsi *item = container_of(citem, struct rsi, h);
244
245         __rsi_update(new, item);
246 }
247
248 static struct cache_head *rsi_alloc(void)
249 {
250         struct rsi *rsi;
251
252         OBD_ALLOC_PTR(rsi);
253         if (rsi) 
254                 return &rsi->h;
255         else
256                 return NULL;
257 }
258
259 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
260 {
261         char           *buf = mesg;
262         char           *ep;
263         int             len;
264         struct rsi      rsii, *rsip = NULL;
265         time_t          expiry;
266         int             status = -EINVAL;
267         ENTRY;
268
269
270         memset(&rsii, 0, sizeof(rsii));
271
272         /* handle */
273         len = qword_get(&mesg, buf, mlen);
274         if (len < 0)
275                 goto out;
276         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
277                 status = -ENOMEM;
278                 goto out;
279         }
280
281         /* token */
282         len = qword_get(&mesg, buf, mlen);
283         if (len < 0)
284                 goto out;
285         if (rawobj_alloc(&rsii.in_token, buf, len)) {
286                 status = -ENOMEM;
287                 goto out;
288         }
289
290         rsip = rsi_lookup(&rsii);
291         if (!rsip)
292                 goto out;
293
294         rsii.h.flags = 0;
295         /* expiry */
296         expiry = get_expiry(&mesg);
297         if (expiry == 0)
298                 goto out;
299
300         len = qword_get(&mesg, buf, mlen);
301         if (len <= 0)
302                 goto out;
303
304         /* major */
305         rsii.major_status = simple_strtol(buf, &ep, 10);
306         if (*ep)
307                 goto out;
308
309         /* minor */
310         len = qword_get(&mesg, buf, mlen);
311         if (len <= 0)
312                 goto out;
313         rsii.minor_status = simple_strtol(buf, &ep, 10);
314         if (*ep)
315                 goto out;
316
317         /* out_handle */
318         len = qword_get(&mesg, buf, mlen);
319         if (len < 0)
320                 goto out;
321         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
322                 status = -ENOMEM;
323                 goto out;
324         }
325
326         /* out_token */
327         len = qword_get(&mesg, buf, mlen);
328         if (len < 0)
329                 goto out;
330         if (rawobj_alloc(&rsii.out_token, buf, len)) {
331                 status = -ENOMEM;
332                 goto out;
333         }
334
335         rsii.h.expiry_time = expiry;
336         rsip = rsi_update(&rsii, rsip);
337         status = 0;
338 out:
339         rsi_free(&rsii);
340         if (rsip) {
341                 cfs_waitq_broadcast(&rsip->waitq);
342                 cache_put(&rsip->h, &rsi_cache);
343         } else {
344                 status = -ENOMEM;
345         }
346
347         if (status)
348                 CERROR("rsi parse error %d\n", status);
349         RETURN(status);
350 }
351
352 #else /* !HAVE_SUNRPC_CACHE_V2 */
353
354 static void rsi_put(struct cache_head *item, struct cache_detail *cd)
355 {
356         struct rsi *rsi = container_of(item, struct rsi, h);
357
358         LASSERT(cfs_atomic_read(&item->refcnt) > 0);
359
360         if (cache_put(item, cd)) {
361                 LASSERT(item->next == NULL);
362                 rsi_free(rsi);
363                 kfree(rsi); /* created by cache mgmt using kmalloc */
364         }
365 }
366
367 static inline int rsi_match(struct rsi *item, struct rsi *tmp)
368 {
369         return __rsi_match(item, tmp);
370 }
371
372 static inline void rsi_init(struct rsi *new, struct rsi *item)
373 {
374         __rsi_init(new, item);
375 }
376
377 static inline void rsi_update(struct rsi *new, struct rsi *item)
378 {
379         __rsi_update(new, item);
380 }
381
382 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
383 {
384         char           *buf = mesg;
385         char           *ep;
386         int             len;
387         struct rsi      rsii, *rsip = NULL;
388         time_t          expiry;
389         int             status = -EINVAL;
390         ENTRY;
391
392
393         memset(&rsii, 0, sizeof(rsii));
394
395         /* handle */
396         len = qword_get(&mesg, buf, mlen);
397         if (len < 0)
398                 goto out;
399         if (rawobj_alloc(&rsii.in_handle, buf, len)) {
400                 status = -ENOMEM;
401                 goto out;
402         }
403
404         /* token */
405         len = qword_get(&mesg, buf, mlen);
406         if (len < 0)
407                 goto out;
408         if (rawobj_alloc(&rsii.in_token, buf, len)) {
409                 status = -ENOMEM;
410                 goto out;
411         }
412
413         /* expiry */
414         expiry = get_expiry(&mesg);
415         if (expiry == 0)
416                 goto out;
417
418         len = qword_get(&mesg, buf, mlen);
419         if (len <= 0)
420                 goto out;
421
422         /* major */
423         rsii.major_status = simple_strtol(buf, &ep, 10);
424         if (*ep)
425                 goto out;
426
427         /* minor */
428         len = qword_get(&mesg, buf, mlen);
429         if (len <= 0)
430                 goto out;
431         rsii.minor_status = simple_strtol(buf, &ep, 10);
432         if (*ep)
433                 goto out;
434
435         /* out_handle */
436         len = qword_get(&mesg, buf, mlen);
437         if (len < 0)
438                 goto out;
439         if (rawobj_alloc(&rsii.out_handle, buf, len)) {
440                 status = -ENOMEM;
441                 goto out;
442         }
443
444         /* out_token */
445         len = qword_get(&mesg, buf, mlen);
446         if (len < 0)
447                 goto out;
448         if (rawobj_alloc(&rsii.out_token, buf, len)) {
449                 status = -ENOMEM;
450                 goto out;
451         }
452
453         rsii.h.expiry_time = expiry;
454         rsip = rsi_lookup(&rsii, 1);
455         status = 0;
456 out:
457         rsi_free(&rsii);
458         if (rsip) {
459                 cfs_waitq_broadcast(&rsip->waitq);
460                 rsi_put(&rsip->h, &rsi_cache);
461         }
462
463         if (status)
464                 CERROR("rsi parse error %d\n", status);
465         RETURN(status);
466 }
467
468 #endif /* HAVE_SUNRPC_CACHE_V2 */
469
470 static struct cache_detail rsi_cache = {
471         .hash_size      = RSI_HASHMAX,
472         .hash_table     = rsi_table,
473         .name           = "auth.sptlrpc.init",
474         .cache_put      = rsi_put,
475         .cache_request  = rsi_request,
476         .cache_parse    = rsi_parse,
477 #ifdef HAVE_SUNRPC_CACHE_V2
478         .match          = rsi_match,
479         .init           = rsi_init,
480         .update         = update_rsi,
481         .alloc          = rsi_alloc,
482 #endif
483 };
484
485 #ifdef HAVE_SUNRPC_CACHE_V2
486
487 static struct rsi *rsi_lookup(struct rsi *item)
488 {
489         struct cache_head *ch;
490         int hash = rsi_hash(item);
491
492         ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
493         if (ch)
494                 return container_of(ch, struct rsi, h);
495         else
496                 return NULL;
497 }
498
499 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
500 {
501         struct cache_head *ch;
502         int hash = rsi_hash(new);
503
504         ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
505         if (ch)
506                 return container_of(ch, struct rsi, h);
507         else
508                 return NULL;
509 }
510
511 #else
512
513 static DefineSimpleCacheLookup(rsi, 0)
514
515 #endif
516
517 /****************************************
518  * rsc cache                            *
519  ****************************************/
520
521 #define RSC_HASHBITS    (10)
522 #define RSC_HASHMAX     (1 << RSC_HASHBITS)
523 #define RSC_HASHMASK    (RSC_HASHMAX - 1)
524
525 struct rsc {
526         struct cache_head       h;
527         struct obd_device      *target;
528         rawobj_t                handle;
529         struct gss_svc_ctx      ctx;
530 };
531
532 static struct cache_head *rsc_table[RSC_HASHMAX];
533 static struct cache_detail rsc_cache;
534 #ifdef HAVE_SUNRPC_CACHE_V2
535 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
536 static struct rsc *rsc_lookup(struct rsc *item);
537 #else
538 static struct rsc *rsc_lookup(struct rsc *item, int set);
539 #endif
540
541 static void rsc_free(struct rsc *rsci)
542 {
543         rawobj_free(&rsci->handle);
544         rawobj_free(&rsci->ctx.gsc_rvs_hdl);
545         lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
546 }
547
548 static inline int rsc_hash(struct rsc *rsci)
549 {
550         return hash_mem((char *)rsci->handle.data,
551                         rsci->handle.len, RSC_HASHBITS);
552 }
553
554 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
555 {
556         return rawobj_equal(&new->handle, &tmp->handle);
557 }
558
559 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
560 {
561         new->handle = tmp->handle;
562         tmp->handle = RAWOBJ_EMPTY;
563
564         new->target = NULL;
565         memset(&new->ctx, 0, sizeof(new->ctx));
566         new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
567 }
568
569 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
570 {
571         new->ctx = tmp->ctx;
572         tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
573         tmp->ctx.gsc_mechctx = NULL;
574
575         memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
576         cfs_spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
577 }
578
579 #ifdef HAVE_SUNRPC_CACHE_V2
580
581 static void rsc_put(struct kref *ref)
582 {
583         struct rsc *rsci = container_of(ref, struct rsc, h.ref);
584
585         LASSERT(rsci->h.next == NULL);
586         rsc_free(rsci);
587         OBD_FREE_PTR(rsci);
588 }
589
590 static int rsc_match(struct cache_head *a, struct cache_head *b)
591 {
592         struct rsc *new = container_of(a, struct rsc, h);
593         struct rsc *tmp = container_of(b, struct rsc, h);
594
595         return __rsc_match(new, tmp);
596 }
597
598 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
599 {
600         struct rsc *new = container_of(cnew, struct rsc, h);
601         struct rsc *tmp = container_of(ctmp, struct rsc, h);
602
603         __rsc_init(new, tmp);
604 }
605
606 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
607 {
608         struct rsc *new = container_of(cnew, struct rsc, h);
609         struct rsc *tmp = container_of(ctmp, struct rsc, h);
610
611         __rsc_update(new, tmp);
612 }
613
614 static struct cache_head * rsc_alloc(void)
615 {
616         struct rsc *rsc;
617
618         OBD_ALLOC_PTR(rsc);
619         if (rsc)
620                 return &rsc->h;
621         else
622                 return NULL;
623 }
624
625 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
626 {
627         char                *buf = mesg;
628         int                  len, rv, tmp_int;
629         struct rsc           rsci, *rscp = NULL;
630         time_t               expiry;
631         int                  status = -EINVAL;
632         struct gss_api_mech *gm = NULL;
633
634         memset(&rsci, 0, sizeof(rsci));
635
636         /* context handle */
637         len = qword_get(&mesg, buf, mlen);
638         if (len < 0) goto out;
639         status = -ENOMEM;
640         if (rawobj_alloc(&rsci.handle, buf, len))
641                 goto out;
642
643         rsci.h.flags = 0;
644         /* expiry */
645         expiry = get_expiry(&mesg);
646         status = -EINVAL;
647         if (expiry == 0)
648                 goto out;
649
650         /* remote flag */
651         rv = get_int(&mesg, &tmp_int);
652         if (rv) {
653                 CERROR("fail to get remote flag\n");
654                 goto out;
655         }
656         rsci.ctx.gsc_remote = (tmp_int != 0);
657
658         /* root user flag */
659         rv = get_int(&mesg, &tmp_int);
660         if (rv) {
661                 CERROR("fail to get oss user flag\n");
662                 goto out;
663         }
664         rsci.ctx.gsc_usr_root = (tmp_int != 0);
665
666         /* mds user flag */
667         rv = get_int(&mesg, &tmp_int);
668         if (rv) {
669                 CERROR("fail to get mds user flag\n");
670                 goto out;
671         }
672         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
673
674         /* oss user flag */
675         rv = get_int(&mesg, &tmp_int);
676         if (rv) {
677                 CERROR("fail to get oss user flag\n");
678                 goto out;
679         }
680         rsci.ctx.gsc_usr_oss = (tmp_int != 0);
681
682         /* mapped uid */
683         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
684         if (rv) {
685                 CERROR("fail to get mapped uid\n");
686                 goto out;
687         }
688
689         rscp = rsc_lookup(&rsci);
690         if (!rscp)
691                 goto out;
692
693         /* uid, or NEGATIVE */
694         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
695         if (rv == -EINVAL)
696                 goto out;
697         if (rv == -ENOENT) {
698                 CERROR("NOENT? set rsc entry negative\n");
699                 cfs_set_bit(CACHE_NEGATIVE, &rsci.h.flags);
700         } else {
701                 rawobj_t tmp_buf;
702                 unsigned long ctx_expiry;
703
704                 /* gid */
705                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
706                         goto out;
707
708                 /* mech name */
709                 len = qword_get(&mesg, buf, mlen);
710                 if (len < 0)
711                         goto out;
712                 gm = lgss_name_to_mech(buf);
713                 status = -EOPNOTSUPP;
714                 if (!gm)
715                         goto out;
716
717                 status = -EINVAL;
718                 /* mech-specific data: */
719                 len = qword_get(&mesg, buf, mlen);
720                 if (len < 0)
721                         goto out;
722
723                 tmp_buf.len = len;
724                 tmp_buf.data = (unsigned char *)buf;
725                 if (lgss_import_sec_context(&tmp_buf, gm,
726                                             &rsci.ctx.gsc_mechctx))
727                         goto out;
728
729                 /* currently the expiry time passed down from user-space
730                  * is invalid, here we retrive it from mech. */
731                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
732                         CERROR("unable to get expire time, drop it\n");
733                         goto out;
734                 }
735                 expiry = (time_t) ctx_expiry;
736         }
737
738         rsci.h.expiry_time = expiry;
739         rscp = rsc_update(&rsci, rscp);
740         status = 0;
741 out:
742         if (gm)
743                 lgss_mech_put(gm);
744         rsc_free(&rsci);
745         if (rscp)
746                 cache_put(&rscp->h, &rsc_cache);
747         else
748                 status = -ENOMEM;
749
750         if (status)
751                 CERROR("parse rsc error %d\n", status);
752         return status;
753 }
754
755 #else /* !HAVE_SUNRPC_CACHE_V2 */
756
757 static void rsc_put(struct cache_head *item, struct cache_detail *cd)
758 {
759         struct rsc *rsci = container_of(item, struct rsc, h);
760
761         LASSERT(cfs_atomic_read(&item->refcnt) > 0);
762
763         if (cache_put(item, cd)) {
764                 LASSERT(item->next == NULL);
765                 rsc_free(rsci);
766                 kfree(rsci); /* created by cache mgmt using kmalloc */
767         }
768 }
769
770 static inline int rsc_match(struct rsc *new, struct rsc *tmp)
771 {
772         return __rsc_match(new, tmp);
773 }
774
775 static inline void rsc_init(struct rsc *new, struct rsc *tmp)
776 {
777         __rsc_init(new, tmp);
778 }
779
780 static inline void rsc_update(struct rsc *new, struct rsc *tmp)
781 {
782         __rsc_update(new, tmp);
783 }
784
785 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
786 {
787         char       *buf = mesg;
788         int         len, rv, tmp_int;
789         struct rsc  rsci, *rscp = NULL;
790         time_t      expiry;
791         int         status = -EINVAL;
792
793         memset(&rsci, 0, sizeof(rsci));
794
795         /* context handle */
796         len = qword_get(&mesg, buf, mlen);
797         if (len < 0) goto out;
798         status = -ENOMEM;
799         if (rawobj_alloc(&rsci.handle, buf, len))
800                 goto out;
801
802         rsci.h.flags = 0;
803         /* expiry */
804         expiry = get_expiry(&mesg);
805         status = -EINVAL;
806         if (expiry == 0)
807                 goto out;
808
809         /* remote flag */
810         rv = get_int(&mesg, &tmp_int);
811         if (rv) {
812                 CERROR("fail to get remote flag\n");
813                 goto out;
814         }
815         rsci.ctx.gsc_remote = (tmp_int != 0);
816
817         /* root user flag */
818         rv = get_int(&mesg, &tmp_int);
819         if (rv) {
820                 CERROR("fail to get oss user flag\n");
821                 goto out;
822         }
823         rsci.ctx.gsc_usr_root = (tmp_int != 0);
824
825         /* mds user flag */
826         rv = get_int(&mesg, &tmp_int);
827         if (rv) {
828                 CERROR("fail to get mds user flag\n");
829                 goto out;
830         }
831         rsci.ctx.gsc_usr_mds = (tmp_int != 0);
832
833         /* oss user flag */
834         rv = get_int(&mesg, &tmp_int);
835         if (rv) {
836                 CERROR("fail to get oss user flag\n");
837                 goto out;
838         }
839         rsci.ctx.gsc_usr_oss = (tmp_int != 0);
840
841         /* mapped uid */
842         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
843         if (rv) {
844                 CERROR("fail to get mapped uid\n");
845                 goto out;
846         }
847
848         /* uid, or NEGATIVE */
849         rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
850         if (rv == -EINVAL)
851                 goto out;
852         if (rv == -ENOENT) {
853                 CERROR("NOENT? set rsc entry negative\n");
854                 cfs_set_bit(CACHE_NEGATIVE, &rsci.h.flags);
855         } else {
856                 struct gss_api_mech *gm;
857                 rawobj_t tmp_buf;
858                 unsigned long ctx_expiry;
859
860                 /* gid */
861                 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
862                         goto out;
863
864                 /* mech name */
865                 len = qword_get(&mesg, buf, mlen);
866                 if (len < 0)
867                         goto out;
868                 gm = lgss_name_to_mech(buf);
869                 status = -EOPNOTSUPP;
870                 if (!gm)
871                         goto out;
872
873                 status = -EINVAL;
874                 /* mech-specific data: */
875                 len = qword_get(&mesg, buf, mlen);
876                 if (len < 0) {
877                         lgss_mech_put(gm);
878                         goto out;
879                 }
880                 tmp_buf.len = len;
881                 tmp_buf.data = (unsigned char *)buf;
882                 if (lgss_import_sec_context(&tmp_buf, gm,
883                                             &rsci.ctx.gsc_mechctx)) {
884                         lgss_mech_put(gm);
885                         goto out;
886                 }
887
888                 /* currently the expiry time passed down from user-space
889                  * is invalid, here we retrive it from mech. */
890                 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
891                         CERROR("unable to get expire time, drop it\n");
892                         lgss_mech_put(gm);
893                         goto out;
894                 }
895                 expiry = (time_t) ctx_expiry;
896
897                 lgss_mech_put(gm);
898         }
899
900         rsci.h.expiry_time = expiry;
901         rscp = rsc_lookup(&rsci, 1);
902         status = 0;
903 out:
904         rsc_free(&rsci);
905         if (rscp)
906                 rsc_put(&rscp->h, &rsc_cache);
907
908         if (status)
909                 CERROR("parse rsc error %d\n", status);
910         return status;
911 }
912
913 #endif /* HAVE_SUNRPC_CACHE_V2 */
914
915
916 static struct cache_detail rsc_cache = {
917         .hash_size      = RSC_HASHMAX,
918         .hash_table     = rsc_table,
919         .name           = "auth.sptlrpc.context",
920         .cache_put      = rsc_put,
921         .cache_parse    = rsc_parse,
922 #ifdef HAVE_SUNRPC_CACHE_V2
923         .match          = rsc_match,
924         .init           = rsc_init,
925         .update         = update_rsc,
926         .alloc          = rsc_alloc,
927 #endif
928 };
929
930 #ifdef HAVE_SUNRPC_CACHE_V2
931
932 static struct rsc *rsc_lookup(struct rsc *item)
933 {
934         struct cache_head *ch;
935         int                hash = rsc_hash(item);
936
937         ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
938         if (ch)
939                 return container_of(ch, struct rsc, h);
940         else
941                 return NULL;
942 }
943
944 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
945 {
946         struct cache_head *ch;
947         int                hash = rsc_hash(new);
948
949         ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
950         if (ch)
951                 return container_of(ch, struct rsc, h);
952         else
953                 return NULL;
954 }
955
956 #define COMPAT_RSC_PUT(item, cd)        cache_put((item), (cd))
957
958 #else
959
960 static DefineSimpleCacheLookup(rsc, 0);
961
962 #define COMPAT_RSC_PUT(item, cd)        rsc_put((item), (cd))
963
964 #endif
965
966 /****************************************
967  * rsc cache flush                      *
968  ****************************************/
969
970 typedef int rsc_entry_match(struct rsc *rscp, long data);
971
972 static void rsc_flush(rsc_entry_match *match, long data)
973 {
974         struct cache_head **ch;
975         struct rsc *rscp;
976         int n;
977         ENTRY;
978
979         cfs_write_lock(&rsc_cache.hash_lock);
980         for (n = 0; n < RSC_HASHMAX; n++) {
981                 for (ch = &rsc_cache.hash_table[n]; *ch;) {
982                         rscp = container_of(*ch, struct rsc, h);
983
984                         if (!match(rscp, data)) {
985                                 ch = &((*ch)->next);
986                                 continue;
987                         }
988
989                         /* it seems simply set NEGATIVE doesn't work */
990                         *ch = (*ch)->next;
991                         rscp->h.next = NULL;
992                         cache_get(&rscp->h);
993                         cfs_set_bit(CACHE_NEGATIVE, &rscp->h.flags);
994                         COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
995                         rsc_cache.entries--;
996                 }
997         }
998         cfs_write_unlock(&rsc_cache.hash_lock);
999         EXIT;
1000 }
1001
1002 static int match_uid(struct rsc *rscp, long uid)
1003 {
1004         if ((int) uid == -1)
1005                 return 1;
1006         return ((int) rscp->ctx.gsc_uid == (int) uid);
1007 }
1008
1009 static int match_target(struct rsc *rscp, long target)
1010 {
1011         return (rscp->target == (struct obd_device *) target);
1012 }
1013
1014 static inline void rsc_flush_uid(int uid)
1015 {
1016         if (uid == -1)
1017                 CWARN("flush all gss contexts...\n");
1018
1019         rsc_flush(match_uid, (long) uid);
1020 }
1021
1022 static inline void rsc_flush_target(struct obd_device *target)
1023 {
1024         rsc_flush(match_target, (long) target);
1025 }
1026
1027 void gss_secsvc_flush(struct obd_device *target)
1028 {
1029         rsc_flush_target(target);
1030 }
1031 EXPORT_SYMBOL(gss_secsvc_flush);
1032
1033 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
1034 {
1035         struct rsc  rsci;
1036         struct rsc *found;
1037
1038         memset(&rsci, 0, sizeof(rsci));
1039         if (rawobj_dup(&rsci.handle, handle))
1040                 return NULL;
1041
1042 #ifdef HAVE_SUNRPC_CACHE_V2
1043         found = rsc_lookup(&rsci);
1044 #else
1045         found = rsc_lookup(&rsci, 0);
1046 #endif
1047         rsc_free(&rsci);
1048         if (!found)
1049                 return NULL;
1050         if (cache_check(&rsc_cache, &found->h, NULL))
1051                 return NULL;
1052         return found;
1053 }
1054
1055 #ifdef HAVE_SUNRPC_CACHE_V2
1056
1057 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1058                                    struct gss_sec *gsec,
1059                                    struct gss_cli_ctx *gctx)
1060 {
1061         struct rsc      rsci, *rscp = NULL;
1062         unsigned long   ctx_expiry;
1063         __u32           major;
1064         int             rc;
1065         ENTRY;
1066
1067         memset(&rsci, 0, sizeof(rsci));
1068
1069         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1070                          sizeof(gsec->gs_rvs_hdl)))
1071                 GOTO(out, rc = -ENOMEM);
1072
1073         rscp = rsc_lookup(&rsci);
1074         if (rscp == NULL)
1075                 GOTO(out, rc = -ENOMEM);
1076
1077         major = lgss_copy_reverse_context(gctx->gc_mechctx,
1078                                           &rsci.ctx.gsc_mechctx);
1079         if (major != GSS_S_COMPLETE)
1080                 GOTO(out, rc = -ENOMEM);
1081
1082         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1083                 CERROR("unable to get expire time, drop it\n");
1084                 GOTO(out, rc = -EINVAL);
1085         }
1086         rsci.h.expiry_time = (time_t) ctx_expiry;
1087
1088         if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)
1089                 rsci.ctx.gsc_usr_mds = 1;
1090         else if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0)
1091                 rsci.ctx.gsc_usr_oss = 1;
1092         else
1093                 rsci.ctx.gsc_usr_root = 1;
1094
1095         rscp = rsc_update(&rsci, rscp);
1096         if (rscp == NULL)
1097                 GOTO(out, rc = -ENOMEM);
1098
1099         rscp->target = imp->imp_obd;
1100         rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1101
1102         CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1103               &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1104         rc = 0;
1105 out:
1106         if (rscp)
1107                 cache_put(&rscp->h, &rsc_cache);
1108         rsc_free(&rsci);
1109
1110         if (rc)
1111                 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1112                        gsec->gs_rvs_hdl, rc);
1113         RETURN(rc);
1114 }
1115
1116 #else /* !HAVE_SUNRPC_CACHE_V2 */
1117
1118 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1119                                    struct gss_sec *gsec,
1120                                    struct gss_cli_ctx *gctx)
1121 {
1122         struct rsc      rsci, *rscp;
1123         unsigned long   ctx_expiry;
1124         __u32           major;
1125         int             rc;
1126         ENTRY;
1127
1128         memset(&rsci, 0, sizeof(rsci));
1129
1130         if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1131                          sizeof(gsec->gs_rvs_hdl)))
1132                 GOTO(out, rc = -ENOMEM);
1133
1134         major = lgss_copy_reverse_context(gctx->gc_mechctx,
1135                                           &rsci.ctx.gsc_mechctx);
1136         if (major != GSS_S_COMPLETE)
1137                 GOTO(out, rc = -ENOMEM);
1138
1139         if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1140                 CERROR("unable to get expire time, drop it\n");
1141                 GOTO(out, rc = -ENOMEM);
1142         }
1143         rsci.h.expiry_time = (time_t) ctx_expiry;
1144
1145         if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)
1146                 rsci.ctx.gsc_usr_mds = 1;
1147         else if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0)
1148                 rsci.ctx.gsc_usr_oss = 1;
1149         else
1150                 rsci.ctx.gsc_usr_root = 1;
1151
1152         rscp = rsc_lookup(&rsci, 1);
1153         if (rscp == NULL) {
1154                 CERROR("rsc lookup failed\n");
1155                 GOTO(out, rc = -ENOMEM);
1156         }
1157
1158         rscp->target = imp->imp_obd;
1159         rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1160
1161         CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1162               &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1163         rsc_put(&rscp->h, &rsc_cache);
1164         rc = 0;
1165 out:
1166         rsc_free(&rsci);
1167         if (rc)
1168                 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1169                        gsec->gs_rvs_hdl, rc);
1170         RETURN(rc);
1171 }
1172
1173 #endif /* HAVE_SUNRPC_CACHE_V2 */
1174
1175 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
1176 {
1177         const cfs_time_t        expire = 20;
1178         struct rsc             *rscp;
1179
1180         rscp = gss_svc_searchbyctx(handle);
1181         if (rscp) {
1182                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
1183                        &rscp->ctx, rscp);
1184
1185                 rscp->h.expiry_time = cfs_time_current_sec() + expire;
1186                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1187         }
1188         return 0;
1189 }
1190
1191 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
1192 {
1193         struct rsc *rscp = container_of(ctx, struct rsc, ctx);
1194
1195         return rawobj_dup(handle, &rscp->handle);
1196 }
1197
1198 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
1199 {
1200         struct rsc             *rscp;
1201
1202         rscp = gss_svc_searchbyctx(handle);
1203         if (rscp) {
1204                 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
1205                        &rscp->ctx, rscp, seq + 1);
1206
1207                 rscp->ctx.gsc_rvs_seq = seq + 1;
1208                 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1209         }
1210         return 0;
1211 }
1212
1213 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
1214 {
1215         return NULL;
1216 }
1217 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
1218
1219 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
1220                                struct gss_svc_reqctx *grctx,
1221                                struct gss_wire_ctx *gw,
1222                                struct obd_device *target,
1223                                __u32 lustre_svc,
1224                                rawobj_t *rvs_hdl,
1225                                rawobj_t *in_token)
1226 {
1227         struct ptlrpc_reply_state *rs;
1228         struct rsc                *rsci = NULL;
1229         struct rsi                *rsip = NULL, rsikey;
1230         cfs_waitlink_t             wait;
1231         int                        replen = sizeof(struct ptlrpc_body);
1232         struct gss_rep_header     *rephdr;
1233         int                        first_check = 1;
1234         int                        rc = SECSVC_DROP;
1235         ENTRY;
1236
1237         memset(&rsikey, 0, sizeof(rsikey));
1238         rsikey.lustre_svc = lustre_svc;
1239         rsikey.nid = (__u64) req->rq_peer.nid;
1240
1241         /* duplicate context handle. for INIT it always 0 */
1242         if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
1243                 CERROR("fail to dup context handle\n");
1244                 GOTO(out, rc);
1245         }
1246
1247         if (rawobj_dup(&rsikey.in_token, in_token)) {
1248                 CERROR("can't duplicate token\n");
1249                 rawobj_free(&rsikey.in_handle);
1250                 GOTO(out, rc);
1251         }
1252
1253 #ifdef HAVE_SUNRPC_CACHE_V2
1254         rsip = rsi_lookup(&rsikey);
1255 #else
1256         rsip = rsi_lookup(&rsikey, 0);
1257 #endif
1258         rsi_free(&rsikey);
1259         if (!rsip) {
1260                 CERROR("error in rsi_lookup.\n");
1261
1262                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1263                         rc = SECSVC_COMPLETE;
1264
1265                 GOTO(out, rc);
1266         }
1267
1268         cache_get(&rsip->h); /* take an extra ref */
1269         cfs_waitq_init(&rsip->waitq);
1270         cfs_waitlink_init(&wait);
1271         cfs_waitq_add(&rsip->waitq, &wait);
1272
1273 cache_check:
1274         /* Note each time cache_check() will drop a reference if return
1275          * non-zero. We hold an extra reference on initial rsip, but must
1276          * take care of following calls. */
1277         rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
1278         switch (rc) {
1279         case -EAGAIN: {
1280                 int valid;
1281
1282                 if (first_check) {
1283                         first_check = 0;
1284
1285                         read_lock(&rsi_cache.hash_lock);
1286                         valid = cfs_test_bit(CACHE_VALID, &rsip->h.flags);
1287                         if (valid == 0)
1288                                 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1289                         read_unlock(&rsi_cache.hash_lock);
1290
1291                         if (valid == 0)
1292                                 cfs_schedule_timeout(GSS_SVC_UPCALL_TIMEOUT *
1293                                                      CFS_HZ);
1294
1295                         cache_get(&rsip->h);
1296                         goto cache_check;
1297                 }
1298                 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1299                 break;
1300         }
1301         case -ENOENT:
1302                 CWARN("cache_check return ENOENT, drop\n");
1303                 break;
1304         case 0:
1305                 /* if not the first check, we have to release the extra
1306                  * reference we just added on it. */
1307                 if (!first_check)
1308                         cache_put(&rsip->h, &rsi_cache);
1309                 CDEBUG(D_SEC, "cache_check is good\n");
1310                 break;
1311         }
1312
1313         cfs_waitq_del(&rsip->waitq, &wait);
1314         cache_put(&rsip->h, &rsi_cache);
1315
1316         if (rc)
1317                 GOTO(out, rc = SECSVC_DROP);
1318
1319         rc = SECSVC_DROP;
1320         rsci = gss_svc_searchbyctx(&rsip->out_handle);
1321         if (!rsci) {
1322                 CERROR("authentication failed\n");
1323
1324                 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1325                         rc = SECSVC_COMPLETE;
1326
1327                 GOTO(out, rc);
1328         } else {
1329                 cache_get(&rsci->h);
1330                 grctx->src_ctx = &rsci->ctx;
1331         }
1332
1333         if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1334                 CERROR("failed duplicate reverse handle\n");
1335                 GOTO(out, rc);
1336         }
1337
1338         rsci->target = target;
1339
1340         CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1341                rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1342
1343         if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1344                 CERROR("handle size %u too large\n", rsip->out_handle.len);
1345                 GOTO(out, rc = SECSVC_DROP);
1346         }
1347
1348         grctx->src_init = 1;
1349         grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1350
1351         rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1352         if (rc) {
1353                 CERROR("failed to pack reply: %d\n", rc);
1354                 GOTO(out, rc = SECSVC_DROP);
1355         }
1356
1357         rs = req->rq_reply_state;
1358         LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1359         LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1360                 sizeof(*rephdr) + rsip->out_handle.len);
1361         LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1362
1363         rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1364         rephdr->gh_version = PTLRPC_GSS_VERSION;
1365         rephdr->gh_flags = 0;
1366         rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1367         rephdr->gh_major = rsip->major_status;
1368         rephdr->gh_minor = rsip->minor_status;
1369         rephdr->gh_seqwin = GSS_SEQ_WIN;
1370         rephdr->gh_handle.len = rsip->out_handle.len;
1371         memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1372                rsip->out_handle.len);
1373
1374         memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1375                rsip->out_token.len);
1376
1377         rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1378                                                rsip->out_token.len, 0);
1379
1380         rc = SECSVC_OK;
1381
1382 out:
1383         /* it looks like here we should put rsip also, but this mess up
1384          * with NFS cache mgmt code... FIXME */
1385 #if 0
1386         if (rsip)
1387                 rsi_put(&rsip->h, &rsi_cache);
1388 #endif
1389
1390         if (rsci) {
1391                 /* if anything went wrong, we don't keep the context too */
1392                 if (rc != SECSVC_OK)
1393                         cfs_set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1394                 else
1395                         CDEBUG(D_SEC, "create rsc with idx "LPX64"\n",
1396                                gss_handle_to_u64(&rsci->handle));
1397
1398                 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1399         }
1400         RETURN(rc);
1401 }
1402
1403 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1404                                            struct gss_wire_ctx *gw)
1405 {
1406         struct rsc *rsc;
1407
1408         rsc = gss_svc_searchbyctx(&gw->gw_handle);
1409         if (!rsc) {
1410                 CWARN("Invalid gss ctx idx "LPX64" from %s\n",
1411                       gss_handle_to_u64(&gw->gw_handle),
1412                       libcfs_nid2str(req->rq_peer.nid));
1413                 return NULL;
1414         }
1415
1416         return &rsc->ctx;
1417 }
1418
1419 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1420 {
1421         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1422
1423         COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1424 }
1425
1426 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1427 {
1428         struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1429
1430         /* can't be found */
1431         cfs_set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1432         /* to be removed at next scan */
1433         rsc->h.expiry_time = 1;
1434 }
1435
1436 int __init gss_init_svc_upcall(void)
1437 {
1438         int     i;
1439
1440         cfs_spin_lock_init(&__ctx_index_lock);
1441         /*
1442          * this helps reducing context index confliction. after server reboot,
1443          * conflicting request from clients might be filtered out by initial
1444          * sequence number checking, thus no chance to sent error notification
1445          * back to clients.
1446          */
1447         cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1448
1449
1450         cache_register(&rsi_cache);
1451         cache_register(&rsc_cache);
1452
1453         /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1454          * the init upcall channel, otherwise there's big chance that the first
1455          * upcall issued before the channel be opened thus nfsv4 cache code will
1456          * drop the request direclty, thus lead to unnecessary recovery time.
1457          * here we wait at miximum 1.5 seconds. */
1458         for (i = 0; i < 6; i++) {
1459                 if (atomic_read(&rsi_cache.readers) > 0)
1460                         break;
1461                 cfs_set_current_state(TASK_UNINTERRUPTIBLE);
1462                 LASSERT(CFS_HZ >= 4);
1463                 cfs_schedule_timeout(CFS_HZ / 4);
1464         }
1465
1466         if (atomic_read(&rsi_cache.readers) == 0)
1467                 CWARN("Init channel is not opened by lsvcgssd, following "
1468                       "request might be dropped until lsvcgssd is active\n");
1469
1470         return 0;
1471 }
1472
1473 void __exit gss_exit_svc_upcall(void)
1474 {
1475         cache_purge(&rsi_cache);
1476         cache_unregister(&rsi_cache);
1477
1478         cache_purge(&rsc_cache);
1479         cache_unregister(&rsc_cache);
1480 }