Whamcloud - gitweb
8cc7aeab9999ec6af0ffa45865f8e1292537e5c5
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_pipefs.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  * Copyright 2004 - 2006, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  * linux/net/sunrpc/auth_gss.c
12  *
13  * RPCSEC_GSS client authentication.
14  *
15  *  Copyright (c) 2000 The Regents of the University of Michigan.
16  *  All rights reserved.
17  *
18  *  Dug Song       <dugsong@monkey.org>
19  *  Andy Adamson   <andros@umich.edu>
20  *
21  *  Redistribution and use in source and binary forms, with or without
22  *  modification, are permitted provided that the following conditions
23  *  are met:
24  *
25  *  1. Redistributions of source code must retain the above copyright
26  *     notice, this list of conditions and the following disclaimer.
27  *  2. Redistributions in binary form must reproduce the above copyright
28  *     notice, this list of conditions and the following disclaimer in the
29  *     documentation and/or other materials provided with the distribution.
30  *  3. Neither the name of the University nor the names of its
31  *     contributors may be used to endorse or promote products derived
32  *     from this software without specific prior written permission.
33  *
34  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
35  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47
48 #ifndef EXPORT_SYMTAB
49 # define EXPORT_SYMTAB
50 #endif
51 #define DEBUG_SUBSYSTEM S_SEC
52 #ifdef __KERNEL__
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/dcache.h>
57 #include <linux/fs.h>
58 #include <linux/random.h>
59 #include <linux/mutex.h>
60 #include <linux/crypto.h>
61 #include <asm/atomic.h>
62 struct rpc_clnt; /* for rpc_pipefs */
63 #include <linux/sunrpc/rpc_pipe_fs.h>
64 #else
65 #include <liblustre.h>
66 #endif
67
68 #include <obd.h>
69 #include <obd_class.h>
70 #include <obd_support.h>
71 #include <lustre/lustre_idl.h>
72 #include <lustre_sec.h>
73 #include <lustre_net.h>
74 #include <lustre_import.h>
75
76 #include "gss_err.h"
77 #include "gss_internal.h"
78 #include "gss_api.h"
79
80 static struct ptlrpc_sec_policy gss_policy_pipefs;
81 static struct ptlrpc_ctx_ops gss_pipefs_ctxops;
82
83 static int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx);
84
85 static int gss_sec_pipe_upcall_init(struct gss_sec *gsec)
86 {
87         return 0;
88 }
89
90 static void gss_sec_pipe_upcall_fini(struct gss_sec *gsec)
91 {
92 }
93
94 /****************************************
95  * internel context helpers             *
96  ****************************************/
97
98 static
99 struct ptlrpc_cli_ctx *ctx_create_pf(struct ptlrpc_sec *sec,
100                                      struct vfs_cred *vcred)
101 {
102         struct gss_cli_ctx *gctx;
103         int                 rc;
104
105         OBD_ALLOC_PTR(gctx);
106         if (gctx == NULL)
107                 return NULL;
108
109         rc = gss_cli_ctx_init_common(sec, &gctx->gc_base,
110                                      &gss_pipefs_ctxops, vcred);
111         if (rc) {
112                 OBD_FREE_PTR(gctx);
113                 return NULL;
114         }
115
116         return &gctx->gc_base;
117 }
118
119 static
120 void ctx_destroy_pf(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *ctx)
121 {
122         struct gss_cli_ctx *gctx = ctx2gctx(ctx);
123         int                 rc;
124
125         rc = gss_cli_ctx_fini_common(sec, ctx);
126         OBD_FREE_PTR(gctx);
127
128         if (rc) {
129                 CWARN("released the last ctx, proceed to destroy sec %s@%p\n",
130                       sec->ps_policy->sp_name, sec);
131                 sptlrpc_sec_destroy(sec);
132         }
133 }
134
135 static
136 void ctx_enhash_pf(struct ptlrpc_cli_ctx *ctx, struct hlist_head *hash)
137 {
138         set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
139         atomic_inc(&ctx->cc_refcount);
140         hlist_add_head(&ctx->cc_cache, hash);
141 }
142
143 /*
144  * caller must hold spinlock
145  */
146 static
147 void ctx_unhash_pf(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
148 {
149         LASSERT_SPIN_LOCKED(&ctx->cc_sec->ps_lock);
150         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
151         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
152         LASSERT(!hlist_unhashed(&ctx->cc_cache));
153
154         clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
155
156         if (atomic_dec_and_test(&ctx->cc_refcount)) {
157                 __hlist_del(&ctx->cc_cache);
158                 hlist_add_head(&ctx->cc_cache, freelist);
159         } else {
160                 hlist_del_init(&ctx->cc_cache);
161         }
162 }
163
164 /*
165  * return 1 if the context is dead.
166  */
167 static
168 int ctx_check_death_pf(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
169 {
170         if (cli_ctx_check_death(ctx)) {
171                 if (freelist)
172                         ctx_unhash_pf(ctx, freelist);
173                 return 1;
174         }
175
176         return 0;
177 }
178
179 static inline
180 int ctx_check_death_locked_pf(struct ptlrpc_cli_ctx *ctx,
181                               struct hlist_head *freelist)
182 {
183         LASSERT(ctx->cc_sec);
184         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
185         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
186
187         return ctx_check_death_pf(ctx, freelist);
188 }
189
190 static inline
191 int ctx_match_pf(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
192 {
193         /* a little bit optimization for null policy */
194         if (!ctx->cc_ops->match)
195                 return 1;
196
197         return ctx->cc_ops->match(ctx, vcred);
198 }
199
200 static
201 void ctx_list_destroy_pf(struct hlist_head *head)
202 {
203         struct ptlrpc_cli_ctx *ctx;
204
205         while (!hlist_empty(head)) {
206                 ctx = hlist_entry(head->first, struct ptlrpc_cli_ctx, cc_cache);
207
208                 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
209                 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
210
211                 hlist_del_init(&ctx->cc_cache);
212                 ctx_destroy_pf(ctx->cc_sec, ctx);
213         }
214 }
215
216 /****************************************
217  * context apis                         *
218  ****************************************/
219
220 static
221 int gss_cli_ctx_validate_pf(struct ptlrpc_cli_ctx *ctx)
222 {
223         if (ctx_check_death_pf(ctx, NULL))
224                 return 1;
225         if (cli_ctx_is_ready(ctx))
226                 return 0;
227         return 1;
228 }
229
230 static
231 void gss_cli_ctx_die_pf(struct ptlrpc_cli_ctx *ctx, int grace)
232 {
233         LASSERT(ctx->cc_sec);
234         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
235
236         cli_ctx_expire(ctx);
237
238         spin_lock(&ctx->cc_sec->ps_lock);
239
240         if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags)) {
241                 LASSERT(!hlist_unhashed(&ctx->cc_cache));
242                 LASSERT(atomic_read(&ctx->cc_refcount) > 1);
243
244                 hlist_del_init(&ctx->cc_cache);
245                 if (atomic_dec_and_test(&ctx->cc_refcount))
246                         LBUG();
247         }
248
249         spin_unlock(&ctx->cc_sec->ps_lock);
250 }
251
252 /****************************************
253  * reverse context installation         *
254  ****************************************/
255
256 static inline
257 unsigned int ctx_hash_index(int hashsize, __u64 key)
258 {
259         return (unsigned int) (key & ((__u64) hashsize - 1));
260 }
261
262 static
263 void gss_sec_ctx_replace_pf(struct gss_sec *gsec,
264                             struct ptlrpc_cli_ctx *new)
265 {
266         struct gss_sec_pipefs *gsec_pf;
267         struct ptlrpc_cli_ctx *ctx;
268         struct hlist_node *pos, *next;
269         CFS_HLIST_HEAD(freelist);
270         unsigned int hash;
271         ENTRY;
272
273         gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
274
275         hash = ctx_hash_index(gsec_pf->gsp_chash_size,
276                               (__u64) new->cc_vcred.vc_uid);
277         LASSERT(hash < gsec_pf->gsp_chash_size);
278
279         spin_lock(&gsec->gs_base.ps_lock);
280
281         hlist_for_each_entry_safe(ctx, pos, next,
282                                   &gsec_pf->gsp_chash[hash], cc_cache) {
283                 if (!ctx_match_pf(ctx, &new->cc_vcred))
284                         continue;
285
286                 cli_ctx_expire(ctx);
287                 ctx_unhash_pf(ctx, &freelist);
288                 break;
289         }
290
291         ctx_enhash_pf(new, &gsec_pf->gsp_chash[hash]);
292         atomic_inc(&gsec->gs_base.ps_busy);
293
294         spin_unlock(&gsec->gs_base.ps_lock);
295
296         ctx_list_destroy_pf(&freelist);
297         EXIT;
298 }
299
300 static
301 int gss_install_rvs_cli_ctx_pf(struct gss_sec *gsec,
302                                struct ptlrpc_svc_ctx *svc_ctx)
303 {
304         struct vfs_cred          vcred;
305         struct ptlrpc_cli_ctx   *cli_ctx;
306         int                      rc;
307         ENTRY;
308
309         vcred.vc_uid = 0;
310         vcred.vc_gid = 0;
311
312         cli_ctx = ctx_create_pf(&gsec->gs_base, &vcred);
313         if (!cli_ctx)
314                 RETURN(-ENOMEM);
315
316         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
317         if (rc) {
318                 ctx_destroy_pf(cli_ctx->cc_sec, cli_ctx);
319                 RETURN(rc);
320         }
321
322         gss_sec_ctx_replace_pf(gsec, cli_ctx);
323         RETURN(0);
324 }
325
326 static
327 void gss_ctx_cache_gc_pf(struct gss_sec_pipefs *gsec_pf,
328                          struct hlist_head *freelist)
329 {
330         struct ptlrpc_sec       *sec;
331         struct ptlrpc_cli_ctx   *ctx;
332         struct hlist_node       *pos, *next;
333         int i;
334         ENTRY;
335
336         sec = &gsec_pf->gsp_base.gs_base;
337
338         CDEBUG(D_SEC, "do gc on sec %s@%p\n", sec->ps_policy->sp_name, sec);
339
340         for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
341                 hlist_for_each_entry_safe(ctx, pos, next,
342                                           &gsec_pf->gsp_chash[i], cc_cache)
343                         ctx_check_death_locked_pf(ctx, freelist);
344         }
345
346         sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
347         EXIT;
348 }
349
350 static
351 struct ptlrpc_sec* gss_sec_create_pf(struct obd_import *imp,
352                                      struct ptlrpc_svc_ctx *ctx,
353                                      __u32 flavor,
354                                      unsigned long flags)
355 {
356         struct gss_sec_pipefs   *gsec_pf;
357         int                      alloc_size, hash_size, i;
358         ENTRY;
359
360 #define GSS_SEC_PIPEFS_CTX_HASH_SIZE    (32)
361
362         if (ctx || flags & (PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_REVERSE))
363                 hash_size = 1;
364         else
365                 hash_size = GSS_SEC_PIPEFS_CTX_HASH_SIZE;
366
367         alloc_size = sizeof(*gsec_pf) +
368                      sizeof(struct hlist_head) * hash_size;
369
370         OBD_ALLOC(gsec_pf, alloc_size);
371         if (!gsec_pf)
372                 RETURN(NULL);
373
374         gsec_pf->gsp_chash_size = hash_size;
375         for (i = 0; i < hash_size; i++)
376                 CFS_INIT_HLIST_HEAD(&gsec_pf->gsp_chash[i]);
377
378         if (gss_sec_create_common(&gsec_pf->gsp_base, &gss_policy_pipefs,
379                                   imp, ctx, flavor, flags))
380                 goto err_free;
381
382         if (ctx == NULL) {
383                 if (gss_sec_pipe_upcall_init(&gsec_pf->gsp_base))
384                         goto err_destroy;
385         } else {
386                 if (gss_install_rvs_cli_ctx_pf(&gsec_pf->gsp_base, ctx))
387                         goto err_destroy;
388         }
389
390         RETURN(&gsec_pf->gsp_base.gs_base);
391
392 err_destroy:
393         gss_sec_destroy_common(&gsec_pf->gsp_base);
394 err_free:
395         OBD_FREE(gsec_pf, alloc_size);
396         RETURN(NULL);
397 }
398
399 static
400 void gss_sec_destroy_pf(struct ptlrpc_sec *sec)
401 {
402         struct gss_sec_pipefs   *gsec_pf;
403         struct gss_sec          *gsec;
404
405         CWARN("destroy %s@%p\n", sec->ps_policy->sp_name, sec);
406
407         gsec = container_of(sec, struct gss_sec, gs_base);
408         gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
409
410         LASSERT(gsec_pf->gsp_chash);
411         LASSERT(gsec_pf->gsp_chash_size);
412
413         gss_sec_pipe_upcall_fini(gsec);
414
415         gss_sec_destroy_common(gsec);
416
417         OBD_FREE(gsec, sizeof(*gsec_pf) +
418                        sizeof(struct hlist_head) * gsec_pf->gsp_chash_size);
419 }
420
421 static
422 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_pf(struct ptlrpc_sec *sec,
423                                               struct vfs_cred *vcred,
424                                               int create, int remove_dead)
425 {
426         struct gss_sec         *gsec;
427         struct gss_sec_pipefs  *gsec_pf;
428         struct ptlrpc_cli_ctx  *ctx = NULL, *new = NULL;
429         struct hlist_head      *hash_head;
430         struct hlist_node      *pos, *next;
431         CFS_HLIST_HEAD(freelist);
432         unsigned int            hash, gc = 0, found = 0;
433         ENTRY;
434
435         might_sleep();
436
437         gsec = container_of(sec, struct gss_sec, gs_base);
438         gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
439
440         hash = ctx_hash_index(gsec_pf->gsp_chash_size,
441                               (__u64) vcred->vc_uid);
442         hash_head = &gsec_pf->gsp_chash[hash];
443         LASSERT(hash < gsec_pf->gsp_chash_size);
444
445 retry:
446         spin_lock(&sec->ps_lock);
447
448         /* gc_next == 0 means never do gc */
449         if (remove_dead && sec->ps_gc_next &&
450             cfs_time_after(cfs_time_current_sec(), sec->ps_gc_next)) {
451                 gss_ctx_cache_gc_pf(gsec_pf, &freelist);
452                 gc = 1;
453         }
454
455         hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_cache) {
456                 if (gc == 0 &&
457                     ctx_check_death_locked_pf(ctx,
458                                               remove_dead ? &freelist : NULL))
459                         continue;
460
461                 if (ctx_match_pf(ctx, vcred)) {
462                         found = 1;
463                         break;
464                 }
465         }
466
467         if (found) {
468                 if (new && new != ctx) {
469                         /* lost the race, just free it */
470                         hlist_add_head(&new->cc_cache, &freelist);
471                         new = NULL;
472                 }
473
474                 /* hot node, move to head */
475                 if (hash_head->first != &ctx->cc_cache) {
476                         __hlist_del(&ctx->cc_cache);
477                         hlist_add_head(&ctx->cc_cache, hash_head);
478                 }
479         } else {
480                 /* don't allocate for reverse sec */
481                 if (sec_is_reverse(sec)) {
482                         spin_unlock(&sec->ps_lock);
483                         RETURN(NULL);
484                 }
485
486                 if (new) {
487                         ctx_enhash_pf(new, hash_head);
488                         ctx = new;
489                 } else if (create) {
490                         spin_unlock(&sec->ps_lock);
491                         new = ctx_create_pf(sec, vcred);
492                         if (new) {
493                                 clear_bit(PTLRPC_CTX_NEW_BIT, &new->cc_flags);
494                                 goto retry;
495                         }
496                 } else
497                         ctx = NULL;
498         }
499
500         /* hold a ref */
501         if (ctx)
502                 atomic_inc(&ctx->cc_refcount);
503
504         spin_unlock(&sec->ps_lock);
505
506         /* the allocator of the context must give the first push to refresh */
507         if (new) {
508                 LASSERT(new == ctx);
509                 gss_cli_ctx_refresh_pf(new);
510         }
511
512         ctx_list_destroy_pf(&freelist);
513         RETURN(ctx);
514 }
515
516 static
517 void gss_sec_release_ctx_pf(struct ptlrpc_sec *sec,
518                             struct ptlrpc_cli_ctx *ctx,
519                             int sync)
520 {
521         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
522         LASSERT(hlist_unhashed(&ctx->cc_cache));
523
524         /* if required async, we must clear the UPTODATE bit to prevent extra
525          * rpcs during destroy procedure. */
526         if (!sync)
527                 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
528
529         /* destroy this context */
530         ctx_destroy_pf(sec, ctx);
531 }
532
533 /*
534  * @uid: which user. "-1" means flush all.
535  * @grace: mark context DEAD, allow graceful destroy like notify
536  *         server side, etc.
537  * @force: also flush busy entries.
538  *
539  * return the number of busy context encountered.
540  *
541  * In any cases, never touch "eternal" contexts.
542  */
543 static
544 int gss_sec_flush_ctx_cache_pf(struct ptlrpc_sec *sec,
545                                uid_t uid,
546                                int grace, int force)
547 {
548         struct gss_sec          *gsec;
549         struct gss_sec_pipefs   *gsec_pf;
550         struct ptlrpc_cli_ctx   *ctx;
551         struct hlist_node *pos, *next;
552         CFS_HLIST_HEAD(freelist);
553         int i, busy = 0;
554         ENTRY;
555
556         might_sleep_if(grace);
557
558         gsec = container_of(sec, struct gss_sec, gs_base);
559         gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
560
561         spin_lock(&sec->ps_lock);
562         for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
563                 hlist_for_each_entry_safe(ctx, pos, next,
564                                           &gsec_pf->gsp_chash[i], cc_cache) {
565                         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
566
567                         if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
568                                 continue;
569
570                         if (atomic_read(&ctx->cc_refcount) > 1) {
571                                 busy++;
572                                 if (!force)
573                                         continue;
574
575                                 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
576                                       "grace %d\n",
577                                       atomic_read(&ctx->cc_refcount),
578                                       ctx, ctx->cc_vcred.vc_uid,
579                                       sec2target_str(ctx->cc_sec), grace);
580                         }
581                         ctx_unhash_pf(ctx, &freelist);
582
583                         set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
584                         if (!grace)
585                                 clear_bit(PTLRPC_CTX_UPTODATE_BIT,
586                                           &ctx->cc_flags);
587                 }
588         }
589         spin_unlock(&sec->ps_lock);
590
591         ctx_list_destroy_pf(&freelist);
592         RETURN(busy);
593 }
594
595 /****************************************
596  * service apis                         *
597  ****************************************/
598
599 static
600 int gss_svc_accept_pf(struct ptlrpc_request *req)
601 {
602         return gss_svc_accept(&gss_policy_pipefs, req);
603 }
604
605 static
606 int gss_svc_install_rctx_pf(struct obd_import *imp,
607                             struct ptlrpc_svc_ctx *ctx)
608 {
609         struct gss_sec *gsec;
610
611         LASSERT(imp->imp_sec);
612         LASSERT(ctx);
613
614         gsec = container_of(imp->imp_sec, struct gss_sec, gs_base);
615         return gss_install_rvs_cli_ctx_pf(gsec, ctx);
616 }
617
618 /****************************************
619  * rpc_pipefs definitions               *
620  ****************************************/
621
622 #define LUSTRE_PIPE_ROOT        "/lustre"
623 #define LUSTRE_PIPE_KRB5        LUSTRE_PIPE_ROOT"/krb5"
624
625 struct gss_upcall_msg_data {
626         __u32                           gum_seq;
627         __u32                           gum_uid;
628         __u32                           gum_gid;
629         __u32                           gum_svc;        /* MDS/OSS... */
630         __u64                           gum_nid;        /* peer NID */
631         __u8                            gum_obd[64];    /* client obd name */
632 };
633
634 struct gss_upcall_msg {
635         struct rpc_pipe_msg             gum_base;
636         atomic_t                        gum_refcount;
637         struct list_head                gum_list;
638         __u32                           gum_mechidx;
639         struct gss_sec                 *gum_gsec;
640         struct gss_cli_ctx             *gum_gctx;
641         struct gss_upcall_msg_data      gum_data;
642 };
643
644 static atomic_t upcall_seq = ATOMIC_INIT(0);
645
646 static inline
647 __u32 upcall_get_sequence(void)
648 {
649         return (__u32) atomic_inc_return(&upcall_seq);
650 }
651
652 enum mech_idx_t {
653         MECH_KRB5   = 0,
654         MECH_MAX
655 };
656
657 static inline
658 __u32 mech_name2idx(const char *name)
659 {
660         LASSERT(!strcmp(name, "krb5"));
661         return MECH_KRB5;
662 }
663
664 /* pipefs dentries for each mechanisms */
665 static struct dentry *de_pipes[MECH_MAX] = { NULL, };
666 /* all upcall messgaes linked here */
667 static struct list_head upcall_lists[MECH_MAX];
668 /* and protected by this */
669 static spinlock_t upcall_locks[MECH_MAX];
670
671 static inline
672 void upcall_list_lock(int idx)
673 {
674         spin_lock(&upcall_locks[idx]);
675 }
676
677 static inline
678 void upcall_list_unlock(int idx)
679 {
680         spin_unlock(&upcall_locks[idx]);
681 }
682
683 static
684 void upcall_msg_enlist(struct gss_upcall_msg *msg)
685 {
686         __u32 idx = msg->gum_mechidx;
687
688         upcall_list_lock(idx);
689         list_add(&msg->gum_list, &upcall_lists[idx]);
690         upcall_list_unlock(idx);
691 }
692
693 static
694 void upcall_msg_delist(struct gss_upcall_msg *msg)
695 {
696         __u32 idx = msg->gum_mechidx;
697
698         upcall_list_lock(idx);
699         list_del_init(&msg->gum_list);
700         upcall_list_unlock(idx);
701 }
702
703 /****************************************
704  * rpc_pipefs upcall helpers            *
705  ****************************************/
706
707 static
708 void gss_release_msg(struct gss_upcall_msg *gmsg)
709 {
710         ENTRY;
711         LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
712
713         if (!atomic_dec_and_test(&gmsg->gum_refcount)) {
714                 EXIT;
715                 return;
716         }
717
718         if (gmsg->gum_gctx) {
719                 sptlrpc_cli_ctx_wakeup(&gmsg->gum_gctx->gc_base);
720                 sptlrpc_cli_ctx_put(&gmsg->gum_gctx->gc_base, 1);
721                 gmsg->gum_gctx = NULL;
722         }
723
724         LASSERT(list_empty(&gmsg->gum_list));
725         LASSERT(list_empty(&gmsg->gum_base.list));
726         OBD_FREE_PTR(gmsg);
727         EXIT;
728 }
729
730 static
731 void gss_unhash_msg_nolock(struct gss_upcall_msg *gmsg)
732 {
733         __u32 idx = gmsg->gum_mechidx;
734
735         LASSERT(idx < MECH_MAX);
736         LASSERT_SPIN_LOCKED(&upcall_locks[idx]);
737
738         if (list_empty(&gmsg->gum_list))
739                 return;
740
741         list_del_init(&gmsg->gum_list);
742         LASSERT(atomic_read(&gmsg->gum_refcount) > 1);
743         atomic_dec(&gmsg->gum_refcount);
744 }
745
746 static
747 void gss_unhash_msg(struct gss_upcall_msg *gmsg)
748 {
749         __u32 idx = gmsg->gum_mechidx;
750
751         LASSERT(idx < MECH_MAX);
752         upcall_list_lock(idx);
753         gss_unhash_msg_nolock(gmsg);
754         upcall_list_unlock(idx);
755 }
756
757 static
758 void gss_msg_fail_ctx(struct gss_upcall_msg *gmsg)
759 {
760         if (gmsg->gum_gctx) {
761                 struct ptlrpc_cli_ctx *ctx = &gmsg->gum_gctx->gc_base;
762
763                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
764                 sptlrpc_cli_ctx_expire(ctx);
765                 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
766         }
767 }
768
769 static
770 struct gss_upcall_msg * gss_find_upcall(__u32 mechidx, __u32 seq)
771 {
772         struct gss_upcall_msg *gmsg;
773
774         upcall_list_lock(mechidx);
775         list_for_each_entry(gmsg, &upcall_lists[mechidx], gum_list) {
776                 if (gmsg->gum_data.gum_seq != seq)
777                         continue;
778
779                 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
780                 LASSERT(gmsg->gum_mechidx == mechidx);
781
782                 atomic_inc(&gmsg->gum_refcount);
783                 upcall_list_unlock(mechidx);
784                 return gmsg;
785         }
786         upcall_list_unlock(mechidx);
787         return NULL;
788 }
789
790 static
791 int simple_get_bytes(char **buf, __u32 *buflen, void *res, __u32 reslen)
792 {
793         if (*buflen < reslen) {
794                 CERROR("buflen %u < %u\n", *buflen, reslen);
795                 return -EINVAL;
796         }
797
798         memcpy(res, *buf, reslen);
799         *buf += reslen;
800         *buflen -= reslen;
801         return 0;
802 }
803
804 /****************************************
805  * rpc_pipefs apis                      *
806  ****************************************/
807
808 static
809 ssize_t gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
810                         char *dst, size_t buflen)
811 {
812         char *data = (char *)msg->data + msg->copied;
813         ssize_t mlen = msg->len;
814         ssize_t left;
815         ENTRY;
816
817         if (mlen > buflen)
818                 mlen = buflen;
819         left = copy_to_user(dst, data, mlen);
820         if (left < 0) {
821                 msg->errno = left;
822                 RETURN(left);
823         }
824         mlen -= left;
825         msg->copied += mlen;
826         msg->errno = 0;
827         RETURN(mlen);
828 }
829
830 static
831 ssize_t gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
832 {
833         struct rpc_inode        *rpci = RPC_I(filp->f_dentry->d_inode);
834         struct gss_upcall_msg   *gss_msg;
835         struct ptlrpc_cli_ctx   *ctx;
836         struct gss_cli_ctx      *gctx = NULL;
837         char                    *buf, *data;
838         int                      datalen;
839         int                      timeout, rc;
840         __u32                    mechidx, seq, gss_err;
841         ENTRY;
842
843         mechidx = (__u32) (long) rpci->private;
844         LASSERT(mechidx < MECH_MAX);
845
846         OBD_ALLOC(buf, mlen);
847         if (!buf)
848                 RETURN(-ENOMEM);
849
850         if (copy_from_user(buf, src, mlen)) {
851                 CERROR("failed copy user space data\n");
852                 GOTO(out_free, rc = -EFAULT);
853         }
854         data = buf;
855         datalen = mlen;
856
857         /* data passed down format:
858          *  - seq
859          *  - timeout
860          *  - gc_win / error
861          *  - wire_ctx (rawobj)
862          *  - mech_ctx (rawobj)
863          */
864         if (simple_get_bytes(&data, &datalen, &seq, sizeof(seq))) {
865                 CERROR("fail to get seq\n");
866                 GOTO(out_free, rc = -EFAULT);
867         }
868
869         gss_msg = gss_find_upcall(mechidx, seq);
870         if (!gss_msg) {
871                 CERROR("upcall %u has aborted earlier\n", seq);
872                 GOTO(out_free, rc = -EINVAL);
873         }
874
875         gss_unhash_msg(gss_msg);
876         gctx = gss_msg->gum_gctx;
877         LASSERT(gctx);
878         LASSERT(atomic_read(&gctx->gc_base.cc_refcount) > 0);
879
880         /* timeout is not in use for now */
881         if (simple_get_bytes(&data, &datalen, &timeout, sizeof(timeout)))
882                 GOTO(out_msg, rc = -EFAULT);
883
884         /* lgssd signal an error by gc_win == 0 */
885         if (simple_get_bytes(&data, &datalen, &gctx->gc_win,
886                              sizeof(gctx->gc_win)))
887                 GOTO(out_msg, rc = -EFAULT);
888
889         if (gctx->gc_win == 0) {
890                 /* followed by:
891                  * - rpc error
892                  * - gss error
893                  */
894                 if (simple_get_bytes(&data, &datalen, &rc, sizeof(rc)))
895                         GOTO(out_msg, rc = -EFAULT);
896                 if (simple_get_bytes(&data, &datalen, &gss_err,sizeof(gss_err)))
897                         GOTO(out_msg, rc = -EFAULT);
898
899                 if (rc == 0 && gss_err == GSS_S_COMPLETE) {
900                         CWARN("both rpc & gss error code not set\n");
901                         rc = -EPERM;
902                 }
903         } else {
904                 rawobj_t tmpobj;
905
906                 /* handle */
907                 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
908                         GOTO(out_msg, rc = -EFAULT);
909                 if (rawobj_dup(&gctx->gc_handle, &tmpobj))
910                         GOTO(out_msg, rc = -ENOMEM);
911
912                 /* mechctx */
913                 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
914                         GOTO(out_msg, rc = -EFAULT);
915                 gss_err = lgss_import_sec_context(&tmpobj,
916                                                   gss_msg->gum_gsec->gs_mech,
917                                                   &gctx->gc_mechctx);
918                 rc = 0;
919         }
920
921         if (likely(rc == 0 && gss_err == GSS_S_COMPLETE)) {
922                 gss_cli_ctx_uptodate(gctx);
923         } else {
924                 ctx = &gctx->gc_base;
925                 sptlrpc_cli_ctx_expire(ctx);
926                 if (rc != -ERESTART || gss_err != GSS_S_COMPLETE)
927                         set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
928
929                 CERROR("refresh ctx %p(uid %d) failed: %d/0x%08x: %s\n",
930                        ctx, ctx->cc_vcred.vc_uid, rc, gss_err,
931                        test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags) ?
932                        "fatal error" : "non-fatal");
933         }
934
935         rc = mlen;
936
937 out_msg:
938         gss_release_msg(gss_msg);
939
940 out_free:
941         OBD_FREE(buf, mlen);
942         /* FIXME
943          * hack pipefs: always return asked length unless all following
944          * downcalls might be messed up. */
945         rc = mlen;
946         RETURN(rc);
947 }
948
949 static
950 void gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
951 {
952         struct gss_upcall_msg          *gmsg;
953         struct gss_upcall_msg_data     *gumd;
954         static cfs_time_t               ratelimit = 0;
955         ENTRY;
956
957         LASSERT(list_empty(&msg->list));
958
959         /* normally errno is >= 0 */
960         if (msg->errno >= 0) {
961                 EXIT;
962                 return;
963         }
964
965         gmsg = container_of(msg, struct gss_upcall_msg, gum_base);
966         gumd = &gmsg->gum_data;
967         LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
968
969         CERROR("failed msg %p (seq %u, uid %u, svc %u, nid "LPX64", obd %.*s): "
970                "errno %d\n", msg, gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
971                gumd->gum_nid, (int) sizeof(gumd->gum_obd),
972                gumd->gum_obd, msg->errno);
973
974         atomic_inc(&gmsg->gum_refcount);
975         gss_unhash_msg(gmsg);
976         if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
977                 cfs_time_t now = cfs_time_current_sec();
978
979                 if (cfs_time_after(now, ratelimit)) {
980                         CWARN("upcall timed out, is lgssd running?\n");
981                         ratelimit = now + 15;
982                 }
983         }
984         gss_msg_fail_ctx(gmsg);
985         gss_release_msg(gmsg);
986         EXIT;
987 }
988
989 static
990 void gss_pipe_release(struct inode *inode)
991 {
992         struct rpc_inode *rpci = RPC_I(inode);
993         __u32             idx;
994         ENTRY;
995
996         idx = (__u32) (long) rpci->private;
997         LASSERT(idx < MECH_MAX);
998
999         upcall_list_lock(idx);
1000         while (!list_empty(&upcall_lists[idx])) {
1001                 struct gss_upcall_msg      *gmsg;
1002                 struct gss_upcall_msg_data *gumd;
1003
1004                 gmsg = list_entry(upcall_lists[idx].next,
1005                                   struct gss_upcall_msg, gum_list);
1006                 gumd = &gmsg->gum_data;
1007                 LASSERT(list_empty(&gmsg->gum_base.list));
1008
1009                 CERROR("failing remaining msg %p:seq %u, uid %u, svc %u, "
1010                        "nid "LPX64", obd %.*s\n", gmsg,
1011                        gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
1012                        gumd->gum_nid, (int) sizeof(gumd->gum_obd),
1013                        gumd->gum_obd);
1014
1015                 gmsg->gum_base.errno = -EPIPE;
1016                 atomic_inc(&gmsg->gum_refcount);
1017                 gss_unhash_msg_nolock(gmsg);
1018
1019                 gss_msg_fail_ctx(gmsg);
1020
1021                 upcall_list_unlock(idx);
1022                 gss_release_msg(gmsg);
1023                 upcall_list_lock(idx);
1024         }
1025         upcall_list_unlock(idx);
1026         EXIT;
1027 }
1028
1029 static struct rpc_pipe_ops gss_upcall_ops = {
1030         .upcall         = gss_pipe_upcall,
1031         .downcall       = gss_pipe_downcall,
1032         .destroy_msg    = gss_pipe_destroy_msg,
1033         .release_pipe   = gss_pipe_release,
1034 };
1035
1036 /****************************************
1037  * upcall helper functions              *
1038  ****************************************/
1039
1040 static
1041 int gss_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1042 {
1043         struct obd_import          *imp;
1044         struct gss_sec             *gsec;
1045         struct gss_upcall_msg      *gmsg;
1046         int                         rc = 0;
1047         ENTRY;
1048
1049         might_sleep();
1050
1051         LASSERT(ctx->cc_sec);
1052         LASSERT(ctx->cc_sec->ps_import);
1053         LASSERT(ctx->cc_sec->ps_import->imp_obd);
1054
1055         imp = ctx->cc_sec->ps_import;
1056         if (!imp->imp_connection) {
1057                 CERROR("import has no connection set\n");
1058                 RETURN(-EINVAL);
1059         }
1060
1061         gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1062
1063         OBD_ALLOC_PTR(gmsg);
1064         if (!gmsg)
1065                 RETURN(-ENOMEM);
1066
1067         /* initialize pipefs base msg */
1068         CFS_INIT_LIST_HEAD(&gmsg->gum_base.list);
1069         gmsg->gum_base.data = &gmsg->gum_data;
1070         gmsg->gum_base.len = sizeof(gmsg->gum_data);
1071         gmsg->gum_base.copied = 0;
1072         gmsg->gum_base.errno = 0;
1073
1074         /* init upcall msg */
1075         atomic_set(&gmsg->gum_refcount, 1);
1076         gmsg->gum_mechidx = mech_name2idx(gsec->gs_mech->gm_name);
1077         gmsg->gum_gsec = gsec;
1078         gmsg->gum_gctx = container_of(sptlrpc_cli_ctx_get(ctx),
1079                                       struct gss_cli_ctx, gc_base);
1080         gmsg->gum_data.gum_seq = upcall_get_sequence();
1081         gmsg->gum_data.gum_uid = ctx->cc_vcred.vc_uid;
1082         gmsg->gum_data.gum_gid = 0; /* not used for now */
1083         gmsg->gum_data.gum_svc = import_to_gss_svc(imp);
1084         gmsg->gum_data.gum_nid = imp->imp_connection->c_peer.nid;
1085         strncpy(gmsg->gum_data.gum_obd, imp->imp_obd->obd_name,
1086                 sizeof(gmsg->gum_data.gum_obd));
1087
1088         /* This only could happen when sysadmin set it dead/expired
1089          * using lctl by force. */
1090         if (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK) {
1091                 CWARN("ctx %p(%u->%s) was set flags %lx unexpectedly\n",
1092                       ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
1093                       ctx->cc_flags);
1094
1095                 LASSERT(!(ctx->cc_flags & PTLRPC_CTX_UPTODATE));
1096                 ctx->cc_flags |= PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR;
1097
1098                 rc = -EIO;
1099                 goto err_free;
1100         }
1101
1102         upcall_msg_enlist(gmsg);
1103
1104         rc = rpc_queue_upcall(de_pipes[gmsg->gum_mechidx]->d_inode,
1105                               &gmsg->gum_base);
1106         if (rc) {
1107                 CERROR("rpc_queue_upcall failed: %d\n", rc);
1108
1109                 upcall_msg_delist(gmsg);
1110                 goto err_free;
1111         }
1112
1113         RETURN(0);
1114 err_free:
1115         OBD_FREE_PTR(gmsg);
1116         RETURN(rc);
1117 }
1118
1119 static
1120 int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1121 {
1122         /* if we are refreshing for root, also update the reverse
1123          * handle index, do not confuse reverse contexts. */
1124         if (ctx->cc_vcred.vc_uid == 0) {
1125                 struct gss_sec *gsec;
1126
1127                 gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1128                 gsec->gs_rvs_hdl = gss_get_next_ctx_index();
1129         }
1130
1131         return gss_ctx_refresh_pf(ctx);
1132 }
1133
1134 /****************************************
1135  * lustre gss pipefs policy             *
1136  ****************************************/
1137
1138 static struct ptlrpc_ctx_ops gss_pipefs_ctxops = {
1139         .match                  = gss_cli_ctx_match,
1140         .refresh                = gss_cli_ctx_refresh_pf,
1141         .validate               = gss_cli_ctx_validate_pf,
1142         .die                    = gss_cli_ctx_die_pf,
1143         .sign                   = gss_cli_ctx_sign,
1144         .verify                 = gss_cli_ctx_verify,
1145         .seal                   = gss_cli_ctx_seal,
1146         .unseal                 = gss_cli_ctx_unseal,
1147         .wrap_bulk              = gss_cli_ctx_wrap_bulk,
1148         .unwrap_bulk            = gss_cli_ctx_unwrap_bulk,
1149 };
1150
1151 static struct ptlrpc_sec_cops gss_sec_pipefs_cops = {
1152         .create_sec             = gss_sec_create_pf,
1153         .destroy_sec            = gss_sec_destroy_pf,
1154         .lookup_ctx             = gss_sec_lookup_ctx_pf,
1155         .release_ctx            = gss_sec_release_ctx_pf,
1156         .flush_ctx_cache        = gss_sec_flush_ctx_cache_pf,
1157         .install_rctx           = gss_sec_install_rctx,
1158         .alloc_reqbuf           = gss_alloc_reqbuf,
1159         .free_reqbuf            = gss_free_reqbuf,
1160         .alloc_repbuf           = gss_alloc_repbuf,
1161         .free_repbuf            = gss_free_repbuf,
1162         .enlarge_reqbuf         = gss_enlarge_reqbuf,
1163 };
1164
1165 static struct ptlrpc_sec_sops gss_sec_pipefs_sops = {
1166         .accept                 = gss_svc_accept_pf,
1167         .invalidate_ctx         = gss_svc_invalidate_ctx,
1168         .alloc_rs               = gss_svc_alloc_rs,
1169         .authorize              = gss_svc_authorize,
1170         .free_rs                = gss_svc_free_rs,
1171         .free_ctx               = gss_svc_free_ctx,
1172         .unwrap_bulk            = gss_svc_unwrap_bulk,
1173         .wrap_bulk              = gss_svc_wrap_bulk,
1174         .install_rctx           = gss_svc_install_rctx_pf,
1175 };
1176
1177 static struct ptlrpc_sec_policy gss_policy_pipefs = {
1178         .sp_owner               = THIS_MODULE,
1179         .sp_name                = "gss.pipefs",
1180         .sp_policy              = SPTLRPC_POLICY_GSS_PIPEFS,
1181         .sp_cops                = &gss_sec_pipefs_cops,
1182         .sp_sops                = &gss_sec_pipefs_sops,
1183 };
1184
1185 static
1186 int __init gss_init_pipefs_upcall(void)
1187 {
1188         struct dentry   *de;
1189
1190         /* pipe dir */
1191         de = rpc_mkdir(LUSTRE_PIPE_ROOT, NULL);
1192         if (IS_ERR(de) && PTR_ERR(de) != -EEXIST) {
1193                 CERROR("Failed to create gss pipe dir: %ld\n", PTR_ERR(de));
1194                 return PTR_ERR(de);
1195         }
1196
1197         /* FIXME hack pipefs: dput will sometimes cause oops during module
1198          * unload and lgssd close the pipe fds. */
1199
1200         /* krb5 mechanism */
1201         de = rpc_mkpipe(LUSTRE_PIPE_KRB5, (void *) MECH_KRB5, &gss_upcall_ops,
1202                         RPC_PIPE_WAIT_FOR_OPEN);
1203         if (!de || IS_ERR(de)) {
1204                 CERROR("failed to make rpc_pipe %s: %ld\n",
1205                        LUSTRE_PIPE_KRB5, PTR_ERR(de));
1206                 rpc_rmdir(LUSTRE_PIPE_ROOT);
1207                 return PTR_ERR(de);
1208         }
1209
1210         de_pipes[MECH_KRB5] = de;
1211         CFS_INIT_LIST_HEAD(&upcall_lists[MECH_KRB5]);
1212         upcall_locks[MECH_KRB5] = SPIN_LOCK_UNLOCKED;
1213
1214         return 0;
1215 }
1216
1217 static
1218 void __exit gss_exit_pipefs_upcall(void)
1219 {
1220         __u32   i;
1221
1222         for (i = 0; i < MECH_MAX; i++) {
1223                 LASSERT(list_empty(&upcall_lists[i]));
1224
1225                 /* dput pipe dentry here might cause lgssd oops. */
1226                 de_pipes[i] = NULL;
1227         }
1228
1229         rpc_unlink(LUSTRE_PIPE_KRB5);
1230         rpc_rmdir(LUSTRE_PIPE_ROOT);
1231 }
1232
1233 int __init gss_init_pipefs(void)
1234 {
1235         int rc;
1236
1237         rc = gss_init_pipefs_upcall();
1238         if (rc)
1239                 return rc;
1240
1241         rc = sptlrpc_register_policy(&gss_policy_pipefs);
1242         if (rc) {
1243                 gss_exit_pipefs_upcall();
1244                 return rc;
1245         }
1246
1247         return 0;
1248 }
1249
1250 void __exit gss_exit_pipefs(void)
1251 {
1252         gss_exit_pipefs_upcall();
1253         sptlrpc_unregister_policy(&gss_policy_pipefs);
1254 }