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