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