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