Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_keyring.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2007 Cluster File Systems, Inc.
5  *   Author: Eric Mei <ericm@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_SEC
27 #ifdef __KERNEL__
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/dcache.h>
32 #include <linux/fs.h>
33 #include <linux/random.h>
34 #include <linux/crypto.h>
35 #include <linux/key.h>
36 #include <linux/keyctl.h>
37 #include <linux/mutex.h>
38 #include <asm/atomic.h>
39 #else
40 #include <liblustre.h>
41 #endif
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_sec.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50
51 #include "gss_err.h"
52 #include "gss_internal.h"
53 #include "gss_api.h"
54
55 static struct ptlrpc_sec_policy gss_policy_keyring;
56 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
57 static struct key_type gss_key_type;
58
59 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
60                                struct ptlrpc_svc_ctx *svc_ctx);
61
62 /*
63  * the timeout is only for the case that upcall child process die abnormally.
64  * in any other cases it should finally update kernel key. so we set this
65  * timeout value excessive long.
66  */
67 #define KEYRING_UPCALL_TIMEOUT  (obd_timeout + obd_timeout)
68
69 /****************************************
70  * internal helpers                     *
71  ****************************************/
72
73 #define DUMP_PROCESS_KEYRINGS(tsk)                                      \
74 {                                                                       \
75         CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): "                 \
76               "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n",           \
77               tsk->comm, tsk->pid, tsk->uid, tsk->fsuid,                \
78               tsk->parent->comm, tsk->parent->pid,                      \
79               tsk->parent->uid, tsk->parent->fsuid,                     \
80               task_aux(tsk)->request_key_auth ?                         \
81               task_aux(tsk)->request_key_auth->serial : 0,              \
82               task_aux(tsk)->thread_keyring ?                           \
83               task_aux(tsk)->thread_keyring->serial : 0,                \
84               tsk->signal->process_keyring ?                            \
85               tsk->signal->process_keyring->serial : 0,                 \
86               tsk->signal->session_keyring ?                            \
87               tsk->signal->session_keyring->serial : 0,                 \
88               tsk->user->uid_keyring ?                                  \
89               tsk->user->uid_keyring->serial : 0,                       \
90               tsk->user->session_keyring ?                              \
91               tsk->user->session_keyring->serial : 0,                   \
92               task_aux(tsk)->jit_keyring                                \
93              );                                                         \
94 }
95
96 #define DUMP_KEY(key)                                                   \
97 {                                                                       \
98         CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n",              \
99               key, key->serial, atomic_read(&key->usage),               \
100               key->uid, key->gid,                                       \
101               key->description ? key->description : "n/a"               \
102              );                                                         \
103 }
104
105
106 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
107 {
108 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
109         mutex_lock(&gsec_kr->gsk_uc_lock);
110 #endif
111 }
112
113 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
114 {
115 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
116         mutex_unlock(&gsec_kr->gsk_uc_lock);
117 #endif
118 }
119
120 static inline void key_revoke_locked(struct key *key)
121 {
122         set_bit(KEY_FLAG_REVOKED, &key->flags);
123 }
124
125 static void ctx_upcall_timeout_kr(unsigned long data)
126 {
127         struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
128         struct key            *key = ctx2gctx_keyring(ctx)->gck_key;
129
130         CWARN("ctx %p, key %p\n", ctx, key);
131
132         LASSERT(key);
133
134         cli_ctx_expire(ctx);
135         key_revoke_locked(key);
136         sptlrpc_cli_ctx_wakeup(ctx);
137 }
138
139 static
140 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
141 {
142         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
143         struct timer_list          *timer = gctx_kr->gck_timer;
144
145         LASSERT(timer);
146
147         CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
148         timeout = timeout * HZ + cfs_time_current();
149
150         init_timer(timer);
151         timer->expires = timeout;
152         timer->data = (unsigned long ) ctx;
153         timer->function = ctx_upcall_timeout_kr;
154
155         add_timer(timer);
156 }
157
158 /*
159  * caller should make sure no race with other threads
160  */
161 static
162 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
163 {
164         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
165         struct timer_list          *timer = gctx_kr->gck_timer;
166
167         if (timer == NULL)
168                 return;
169
170         CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
171
172         gctx_kr->gck_timer = NULL;
173
174         del_singleshot_timer_sync(timer);
175
176         OBD_FREE_PTR(timer);
177 }
178
179 static
180 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
181                                      struct vfs_cred *vcred)
182 {
183         struct ptlrpc_cli_ctx      *ctx;
184         struct gss_cli_ctx_keyring *gctx_kr;
185
186         OBD_ALLOC_PTR(gctx_kr);
187         if (gctx_kr == NULL)
188                 return NULL;
189
190         OBD_ALLOC_PTR(gctx_kr->gck_timer);
191         if (gctx_kr->gck_timer == NULL) {
192                 OBD_FREE_PTR(gctx_kr);
193                 return NULL;
194         }
195         init_timer(gctx_kr->gck_timer);
196
197         ctx = &gctx_kr->gck_base.gc_base;
198
199         if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
200                 OBD_FREE_PTR(gctx_kr->gck_timer);
201                 OBD_FREE_PTR(gctx_kr);
202                 return NULL;
203         }
204
205         ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
206         clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
207         atomic_inc(&ctx->cc_refcount); /* for the caller */
208
209         return ctx;
210 }
211
212 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
213 {
214         struct ptlrpc_sec          *sec = ctx->cc_sec;
215         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
216         int                         rc;
217
218         CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
219
220         /* at this time the association with key has been broken. */
221         LASSERT(sec);
222         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
223         LASSERT(gctx_kr->gck_key == NULL);
224
225         ctx_clear_timer_kr(ctx);
226         LASSERT(gctx_kr->gck_timer == NULL);
227
228         rc = gss_cli_ctx_fini_common(sec, ctx);
229
230         OBD_FREE_PTR(gctx_kr);
231
232         if (rc) {
233                 CWARN("released the last ctx, proceed to destroy sec %s@%p\n",
234                       sec->ps_policy->sp_name, sec);
235                 sptlrpc_sec_destroy(sec);
236         }
237 }
238
239 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx)
240 {
241         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
242
243         if (atomic_dec_and_test(&ctx->cc_refcount))
244                 ctx_destroy_kr(ctx);
245 }
246
247 /*
248  * key <-> ctx association and rules:
249  * - ctx might not bind with any key
250  * - key/ctx binding is protected by key semaphore (if the key present)
251  * - key and ctx each take a reference of the other
252  * - ctx enlist/unlist is protected by ctx spinlock
253  * - never enlist a ctx after it's been unlisted
254  * - whoever do enlist should also do bind, lock key before enlist:
255  *   - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
256  * - whoever do unlist should also do unbind:
257  *   - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
258  *   - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
259  */
260
261 static inline void spin_lock_if(spinlock_t *lock, int condition)
262 {
263         if (condition)
264                 spin_lock(lock);
265 }
266
267 static inline void spin_unlock_if(spinlock_t *lock, int condition)
268 {
269         if (condition)
270                 spin_unlock(lock);
271 }
272
273 static
274 void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
275 {
276         struct ptlrpc_sec      *sec = ctx->cc_sec;
277         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
278
279         LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
280         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
281
282         spin_lock_if(&sec->ps_lock, !locked);
283
284         atomic_inc(&ctx->cc_refcount);
285         set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
286         hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
287         if (is_root)
288                 gsec_kr->gsk_root_ctx = ctx;
289
290         spin_unlock_if(&sec->ps_lock, !locked);
291 }
292
293 /*
294  * Note after this get called, caller should not access ctx again because
295  * it might have been freed, unless caller hold at least one refcount of
296  * the ctx.
297  *
298  * return non-zero if we indeed unlist this ctx.
299  */
300 static
301 int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
302 {
303         struct ptlrpc_sec       *sec = ctx->cc_sec;
304         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
305
306         /* if hashed bit has gone, leave the job to somebody who is doing it */
307         if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
308                 return 0;
309
310         /* drop ref inside spin lock to prevent race with other operations */
311         spin_lock_if(&sec->ps_lock, !locked);
312
313         if (gsec_kr->gsk_root_ctx == ctx)
314                 gsec_kr->gsk_root_ctx = NULL;
315         hlist_del_init(&ctx->cc_cache);
316         atomic_dec(&ctx->cc_refcount);
317
318         spin_unlock_if(&sec->ps_lock, !locked);
319
320         return 1;
321 }
322
323 /*
324  * bind a key with a ctx together.
325  * caller must hold write lock of the key, as well as ref on key & ctx.
326  */
327 static
328 void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
329 {
330         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
331         LASSERT(atomic_read(&key->usage) > 0);
332         LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
333         LASSERT(key->payload.data == NULL);
334
335         /* at this time context may or may not in list. */
336         key_get(key);
337         atomic_inc(&ctx->cc_refcount);
338         ctx2gctx_keyring(ctx)->gck_key = key;
339         key->payload.data = ctx;
340 }
341
342 /*
343  * unbind a key and a ctx.
344  * caller must hold write lock, as well as a ref of the key.
345  */
346 static
347 void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
348 {
349         LASSERT(key->payload.data == ctx);
350         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
351
352         /* must revoke the key, or others may treat it as newly created */
353         key_revoke_locked(key);
354
355         key->payload.data = NULL;
356         ctx2gctx_keyring(ctx)->gck_key = NULL;
357
358         /* once ctx get split from key, the timer is meaningless */
359         ctx_clear_timer_kr(ctx);
360
361         ctx_put_kr(ctx);
362         key_put(key);
363 }
364
365 /*
366  * given a ctx, unbind with its coupled key, if any.
367  * unbind could only be called once, so we don't worry the key be released
368  * by someone else.
369  */
370 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
371 {
372         struct key      *key = ctx2gctx_keyring(ctx)->gck_key;
373
374         if (key) {
375                 LASSERT(key->payload.data == ctx);
376
377                 key_get(key);
378                 down_write(&key->sem);
379                 unbind_key_ctx(key, ctx);
380                 up_write(&key->sem);
381                 key_put(key);
382         }
383 }
384
385 /*
386  * given a key, unbind with its coupled ctx, if any.
387  * caller must hold write lock, as well as a ref of the key.
388  */
389 static void unbind_key_locked(struct key *key)
390 {
391         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
392
393         if (ctx)
394                 unbind_key_ctx(key, ctx);
395 }
396
397 /*
398  * unlist a ctx, and unbind from coupled key
399  */
400 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
401 {
402         if (ctx_unlist_kr(ctx, 0))
403                 unbind_ctx_kr(ctx);
404 }
405
406 /*
407  * given a key, unlist and unbind with the coupled ctx (if any).
408  * caller must hold write lock, as well as a ref of the key.
409  */
410 static void kill_key_locked(struct key *key)
411 {
412         struct ptlrpc_cli_ctx *ctx = key->payload.data;
413
414         if (ctx && ctx_unlist_kr(ctx, 0))
415                 unbind_key_locked(key);
416 }
417
418 /*
419  * caller should hold one ref on contexts in freelist.
420  */
421 static void dispose_ctx_list_kr(struct hlist_head *freelist)
422 {
423         struct hlist_node      *pos, *next;
424         struct ptlrpc_cli_ctx  *ctx;
425
426         hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
427                 hlist_del_init(&ctx->cc_cache);
428
429                 /* we need to wakeup waiting reqs here. the context might
430                  * be forced released before upcall finished, then the
431                  * late-arrived downcall can't find the ctx even. */
432                 sptlrpc_cli_ctx_wakeup(ctx);
433
434                 unbind_ctx_kr(ctx);
435                 ctx_put_kr(ctx);
436         }
437 }
438
439 /*
440  * lookup a root context directly in a sec, return root ctx with a
441  * reference taken or NULL.
442  */
443 static
444 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
445 {
446         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
447         struct ptlrpc_cli_ctx   *ctx = NULL;
448
449         spin_lock(&sec->ps_lock);
450
451         ctx = gsec_kr->gsk_root_ctx;
452
453         if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
454                 struct hlist_node      *node;
455                 struct ptlrpc_cli_ctx  *tmp;
456
457                 /* reverse ctx, search root ctx in list, choose the one
458                  * with shortest expire time, which is most possibly have
459                  * an established peer ctx at client side. */
460                 hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist, cc_cache) {
461                         if (ctx == NULL || ctx->cc_expire == 0 ||
462                             ctx->cc_expire > tmp->cc_expire) {
463                                 ctx = tmp;
464                                 /* promote to be root_ctx */
465                                 gsec_kr->gsk_root_ctx = ctx;
466                         }
467                 }
468         }
469
470         if (ctx) {
471                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
472                 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
473                 atomic_inc(&ctx->cc_refcount);
474         }
475
476         spin_unlock(&sec->ps_lock);
477
478         return ctx;
479 }
480
481 #define RVS_CTX_EXPIRE_NICE    (10)
482
483 static
484 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
485                                  struct ptlrpc_cli_ctx *new_ctx,
486                                  struct key *key)
487 {
488         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
489         struct hlist_node      *hnode;
490         struct ptlrpc_cli_ctx  *ctx;
491         cfs_time_t              now;
492         ENTRY;
493
494         LASSERT(sec_is_reverse(sec));
495
496         spin_lock(&sec->ps_lock);
497
498         now = cfs_time_current_sec();
499
500         /* set all existing ctxs short expiry */
501         hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
502                 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
503                         ctx->cc_early_expire = 1;
504                         ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
505                 }
506         }
507
508         /* if there's root_ctx there, instead obsolete the current
509          * immediately, we leave it continue operating for a little while.
510          * hopefully when the first backward rpc with newest ctx send out,
511          * the client side already have the peer ctx well established. */
512         ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
513
514         if (key)
515                 bind_key_ctx(key, new_ctx);
516
517         spin_unlock(&sec->ps_lock);
518 }
519
520 static void construct_key_desc(void *buf, int bufsize,
521                                struct ptlrpc_sec *sec, uid_t uid)
522 {
523         snprintf(buf, bufsize, "%d@%x", uid, sec2gsec_keyring(sec)->gsk_id);
524         ((char *)buf)[bufsize - 1] = '\0';
525 }
526
527 /****************************************
528  * sec apis                             *
529  ****************************************/
530
531 static atomic_t gss_sec_id_kr = ATOMIC_INIT(0);
532
533 static
534 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
535                                       struct ptlrpc_svc_ctx *ctx,
536                                       __u32 flavor,
537                                       unsigned long flags)
538 {
539         struct gss_sec_keyring  *gsec_kr;
540         ENTRY;
541
542         OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
543         if (gsec_kr == NULL)
544                 RETURN(NULL);
545
546         gsec_kr->gsk_id = atomic_inc_return(&gss_sec_id_kr);
547         CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
548         gsec_kr->gsk_root_ctx = NULL;
549         mutex_init(&gsec_kr->gsk_root_uc_lock);
550 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
551         mutex_init(&gsec_kr->gsk_uc_lock);
552 #endif
553
554         if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
555                                   imp, ctx, flavor, flags))
556                 goto err_free;
557
558         if (ctx != NULL) {
559                 if (sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, ctx)) {
560                         gss_sec_destroy_common(&gsec_kr->gsk_base);
561                         goto err_free;
562                 }
563         }
564
565         RETURN(&gsec_kr->gsk_base.gs_base);
566
567 err_free:
568         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
569         RETURN(NULL);
570 }
571
572 static
573 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
574 {
575         struct gss_sec          *gsec = sec2gsec(sec);
576         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
577
578         CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
579
580         LASSERT(hlist_empty(&gsec_kr->gsk_clist));
581         LASSERT(gsec_kr->gsk_root_ctx == NULL);
582
583         gss_sec_destroy_common(gsec);
584
585         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
586 }
587
588 static
589 int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
590 {
591         if (sec->ps_flags & PTLRPC_SEC_FL_ROOTONLY)
592                 return 1;
593
594         /* FIXME
595          * more precisely deal with setuid. maybe add more infomation
596          * into vfs_cred ?? */
597         return (vcred->vc_uid == 0);
598 }
599
600 /*
601  * unlink request key from it's ring, which is linked during request_key().
602  * sadly, we have to 'guess' which keyring it's linked to.
603  *
604  * FIXME this code is fragile, depend on how request_key_link() is implemented.
605  */
606 static void request_key_unlink(struct key *key)
607 {
608         struct task_struct *tsk = current;
609         struct key *ring;
610
611         switch (task_aux(tsk)->jit_keyring) {
612         case KEY_REQKEY_DEFL_DEFAULT:
613         case KEY_REQKEY_DEFL_THREAD_KEYRING:
614                 ring = key_get(task_aux(tsk)->thread_keyring);
615                 if (ring)
616                         break;
617         case KEY_REQKEY_DEFL_PROCESS_KEYRING:
618                 ring = key_get(tsk->signal->process_keyring);
619                 if (ring)
620                         break;
621         case KEY_REQKEY_DEFL_SESSION_KEYRING:
622                 rcu_read_lock();
623                 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
624                 rcu_read_unlock();
625                 if (ring)
626                         break;
627         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
628                 ring = key_get(tsk->user->session_keyring);
629                 break;
630         case KEY_REQKEY_DEFL_USER_KEYRING:
631                 ring = key_get(tsk->user->uid_keyring);
632                 break;
633         case KEY_REQKEY_DEFL_GROUP_KEYRING:
634         default:
635                 LBUG();
636         }
637
638         LASSERT(ring);
639         key_unlink(ring, key);
640         key_put(ring);
641 }
642
643 static
644 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
645                                               struct vfs_cred *vcred,
646                                               int create, int remove_dead)
647 {
648         struct obd_import       *imp = sec->ps_import;
649         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
650         struct ptlrpc_cli_ctx   *ctx = NULL;
651         unsigned int             is_root = 0, create_new = 0;
652         struct key              *key;
653         char                     desc[24];
654         char                    *coinfo;
655         const int                coinfo_size = sizeof(struct obd_uuid) + 64;
656         char                    *co_flags = "";
657         ENTRY;
658
659         LASSERT(imp != NULL);
660
661         is_root = user_is_root(sec, vcred);
662
663         /* a little bit optimization for root context */
664         if (is_root) {
665                 ctx = sec_lookup_root_ctx_kr(sec);
666                 /*
667                  * Only lookup directly for REVERSE sec, which should
668                  * always succeed.
669                  */
670                 if (ctx || sec_is_reverse(sec))
671                         RETURN(ctx);
672         }
673
674         LASSERT(create != 0);
675
676         /* for root context, obtain lock and check again, this time hold
677          * the root upcall lock, make sure nobody else populated new root
678          * context after last check. */
679         if (is_root) {
680                 mutex_lock(&gsec_kr->gsk_root_uc_lock);
681
682                 ctx = sec_lookup_root_ctx_kr(sec);
683                 if (ctx)
684                         goto out;
685
686                 /* update reverse handle for root user */
687                 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
688
689                 co_flags = "r";
690         }
691
692         construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
693
694         /* callout info: mech:flags:svc_type:peer_nid:target_uuid */
695         OBD_ALLOC(coinfo, coinfo_size);
696         if (coinfo == NULL)
697                 goto out;
698
699         snprintf(coinfo, coinfo_size, "%s:%s:%d:"LPX64":%s",
700                  sec2gsec(sec)->gs_mech->gm_name,
701                  co_flags, import_to_gss_svc(imp),
702                  imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
703
704         keyring_upcall_lock(gsec_kr);
705         key = request_key(&gss_key_type, desc, coinfo);
706         keyring_upcall_unlock(gsec_kr);
707
708         OBD_FREE(coinfo, coinfo_size);
709
710         if (IS_ERR(key)) {
711                 CERROR("failed request key: %ld\n", PTR_ERR(key));
712                 goto out;
713         }
714
715         /* once payload.data was pointed to a ctx, it never changes until
716          * we de-associate them; but parallel request_key() may return
717          * a key with payload.data == NULL at the same time. so we still
718          * need wirtelock of key->sem to serialize them. */
719         down_write(&key->sem);
720
721         if (likely(key->payload.data != NULL)) {
722                 ctx = key->payload.data;
723
724                 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
725                 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
726                 LASSERT(atomic_read(&key->usage) >= 2);
727
728                 /* simply take a ref and return. it's upper layer's
729                  * responsibility to detect & replace dead ctx. */
730                 atomic_inc(&ctx->cc_refcount);
731         } else {
732                 /* pre initialization with a cli_ctx. this can't be done in
733                  * key_instantiate() because we'v no enough information
734                  * there. */
735                 ctx = ctx_create_kr(sec, vcred);
736                 if (ctx != NULL) {
737                         ctx_enlist_kr(ctx, is_root, 0);
738                         bind_key_ctx(key, ctx);
739
740                         ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
741
742                         CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
743                                key, ctx, sec);
744                 } else {
745                         /* we'd prefer to call key_revoke(), but we more like
746                          * to revoke it within this key->sem locked period. */
747                         key_revoke_locked(key);
748                 }
749
750                 create_new = 1;
751         }
752
753         up_write(&key->sem);
754
755         if (is_root && create_new)
756                 request_key_unlink(key);
757
758         key_put(key);
759 out:
760         if (is_root)
761                 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
762         RETURN(ctx);
763 }
764
765 static
766 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
767                             struct ptlrpc_cli_ctx *ctx,
768                             int sync)
769 {
770         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
771
772         if (sync) {
773                 ctx_destroy_kr(ctx);
774         } else {
775                 atomic_inc(&ctx->cc_refcount);
776                 sptlrpc_gc_add_ctx(ctx);
777         }
778 }
779
780 /*
781  * flush context of normal user, we must resort to keyring itself to find out
782  * contexts which belong to me.
783  *
784  * Note here we suppose only to flush _my_ context, the "uid" will
785  * be ignored in the search.
786  */
787 static
788 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
789                              uid_t uid,
790                              int grace, int force)
791 {
792         struct key              *key;
793         char                     desc[24];
794
795         /* nothing to do for reverse or rootonly sec */
796         if (sec_is_reverse(sec) || sec_is_rootonly(sec))
797                 return;
798
799         construct_key_desc(desc, sizeof(desc), sec, uid);
800
801         /* there should be only one valid key, but we put it in the
802          * loop in case of any weird cases */
803         for (;;) {
804                 key = request_key(&gss_key_type, desc, NULL);
805                 if (IS_ERR(key)) {
806                         CWARN("No more key found for current user\n");
807                         break;
808                 }
809
810                 down_write(&key->sem);
811
812                 kill_key_locked(key);
813
814                 /* kill_key_locked() should usually revoke the key, but we
815                  * revoke it again to make sure, e.g. some case the key may
816                  * not well coupled with a context. */
817                 key_revoke_locked(key);
818
819                 up_write(&key->sem);
820
821                 key_put(key);
822         }
823 }
824
825 /*
826  * flush context of root or all, we iterate through the list.
827  */
828 static
829 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
830                              uid_t uid,
831                              int grace, int force)
832 {
833         struct gss_sec_keyring *gsec_kr;
834         struct hlist_head       freelist = CFS_HLIST_HEAD_INIT;
835         struct hlist_node      *pos, *next;
836         struct ptlrpc_cli_ctx  *ctx;
837         ENTRY;
838
839         gsec_kr = sec2gsec_keyring(sec);
840
841         spin_lock(&sec->ps_lock);
842         hlist_for_each_entry_safe(ctx, pos, next,
843                                   &gsec_kr->gsk_clist, cc_cache) {
844                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
845
846                 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
847                         continue;
848
849                 /* at this moment there's at least 2 base reference:
850                  * key association and in-list. */
851                 if (atomic_read(&ctx->cc_refcount) > 2) {
852                         if (!force)
853                                 continue;
854                         CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
855                               ctx, ctx->cc_vcred.vc_uid,
856                               sec2target_str(ctx->cc_sec),
857                               atomic_read(&ctx->cc_refcount) - 2);
858                 }
859
860                 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
861                 if (!grace)
862                         clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
863
864                 atomic_inc(&ctx->cc_refcount);
865
866                 if (ctx_unlist_kr(ctx, 1))
867                         hlist_add_head(&ctx->cc_cache, &freelist);
868                 else {
869                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
870                         atomic_dec(&ctx->cc_refcount);
871                 }
872
873         }
874         spin_unlock(&sec->ps_lock);
875
876         dispose_ctx_list_kr(&freelist);
877         EXIT;
878 }
879
880 static
881 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
882                                uid_t uid,
883                                int grace, int force)
884 {
885         ENTRY;
886
887         CDEBUG(D_SEC, "sec %p(%d, busy %d), uid %d, grace %d, force %d\n",
888                sec, atomic_read(&sec->ps_refcount), atomic_read(&sec->ps_busy),
889                uid, grace, force);
890
891         if (uid != -1 && uid != 0)
892                 flush_user_ctx_cache_kr(sec, uid, grace, force);
893         else
894                 flush_spec_ctx_cache_kr(sec, uid, grace, force);
895
896         RETURN(0);
897 }
898
899 static
900 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
901 {
902         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
903         struct hlist_head       freelist = CFS_HLIST_HEAD_INIT;
904         struct hlist_node      *pos, *next;
905         struct ptlrpc_cli_ctx  *ctx;
906         ENTRY;
907
908         CWARN("running gc\n");
909
910         spin_lock(&sec->ps_lock);
911         hlist_for_each_entry_safe(ctx, pos, next,
912                                   &gsec_kr->gsk_clist, cc_cache) {
913                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
914
915                 atomic_inc(&ctx->cc_refcount);
916
917                 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
918                         hlist_add_head(&ctx->cc_cache, &freelist);
919                         CWARN("unhashed ctx %p\n", ctx);
920                 } else {
921                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
922                         atomic_dec(&ctx->cc_refcount);
923                 }
924         }
925         spin_unlock(&sec->ps_lock);
926
927         dispose_ctx_list_kr(&freelist);
928         EXIT;
929         return;
930 }
931
932 static
933 int gss_sec_display_kr(struct ptlrpc_sec *sec, char *buf, int bufsize)
934 {
935         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
936         struct hlist_node      *pos, *next;
937         struct ptlrpc_cli_ctx  *ctx;
938         int                     written = 0;
939         ENTRY;
940
941         written = snprintf(buf, bufsize, "context list ===>\n");
942         bufsize -= written;
943         buf += written;
944
945         spin_lock(&sec->ps_lock);
946         hlist_for_each_entry_safe(ctx, pos, next,
947                                   &gsec_kr->gsk_clist, cc_cache) {
948                 struct gss_cli_ctx     *gctx;
949                 struct key             *key;
950                 char                    flags_str[40];
951                 int                     len;
952
953                 gctx = ctx2gctx(ctx);
954                 key = ctx2gctx_keyring(ctx)->gck_key;
955
956                 gss_cli_ctx_flags2str(ctx->cc_flags,
957                                       flags_str, sizeof(flags_str));
958
959                 len = snprintf(buf, bufsize, "%p(%d): uid %u, exp %ld(%ld)s, "
960                                "fl %s, seq %d, win %u, key %08x(%d), ",
961                                ctx, atomic_read(&ctx->cc_refcount),
962                                ctx->cc_vcred.vc_uid,
963                                ctx->cc_expire,
964                                ctx->cc_expire - cfs_time_current_sec(),
965                                flags_str,
966                                atomic_read(&gctx->gc_seq),
967                                gctx->gc_win,
968                                key ? key->serial : 0,
969                                key ? atomic_read(&key->usage) : 0);
970
971                 written += len;
972                 buf += len;
973                 bufsize -= len;
974
975                 if (bufsize <= 0)
976                         break;
977
978                 if (gctx->gc_mechctx)
979                         len = lgss_display(gctx->gc_mechctx, buf, bufsize);
980                 else
981                         len = snprintf(buf, bufsize, "mech N/A\n");
982
983                 written += len;
984                 buf += len;
985                 bufsize -= len;
986
987                 if (bufsize <= 0)
988                         break;
989         }
990         spin_unlock(&sec->ps_lock);
991
992         RETURN(written);
993 }
994
995 /****************************************
996  * cli_ctx apis                         *
997  ****************************************/
998
999 static
1000 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1001 {
1002         /* upcall is already on the way */
1003         return 0;
1004 }
1005
1006 static
1007 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1008 {
1009         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1010         LASSERT(ctx->cc_sec);
1011
1012         if (cli_ctx_check_death(ctx)) {
1013                 kill_ctx_kr(ctx);
1014                 return 1;
1015         }
1016
1017         if (cli_ctx_is_ready(ctx))
1018                 return 0;
1019         return 1;
1020 }
1021
1022 static
1023 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1024 {
1025         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1026         LASSERT(ctx->cc_sec);
1027
1028         CWARN("ctx %p(%d)\n", ctx, atomic_read(&ctx->cc_refcount));
1029         cli_ctx_expire(ctx);
1030         kill_ctx_kr(ctx);
1031 }
1032
1033 /****************************************
1034  * (reverse) service                    *
1035  ****************************************/
1036
1037 /*
1038  * reverse context could have nothing to do with keyrings. here we still keep
1039  * the version which bind to a key, for future reference.
1040  */
1041 #define HAVE_REVERSE_CTX_NOKEY
1042
1043 #ifdef HAVE_REVERSE_CTX_NOKEY
1044
1045 static
1046 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1047                         struct ptlrpc_svc_ctx *svc_ctx)
1048 {
1049         struct ptlrpc_cli_ctx   *cli_ctx;
1050         struct vfs_cred          vcred = { 0, 0 };
1051         int                      rc;
1052
1053         LASSERT(sec);
1054         LASSERT(svc_ctx);
1055
1056         cli_ctx = ctx_create_kr(sec, &vcred);
1057         if (cli_ctx == NULL)
1058                 return -ENOMEM;
1059
1060         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1061         if (rc) {
1062                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1063
1064                 ctx_put_kr(cli_ctx);
1065                 return rc;
1066         }
1067
1068         rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1069
1070         ctx_put_kr(cli_ctx);
1071
1072         return 0;
1073 }
1074
1075 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1076
1077 static
1078 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1079                         struct ptlrpc_svc_ctx *svc_ctx)
1080 {
1081         struct ptlrpc_cli_ctx   *cli_ctx = NULL;
1082         struct key              *key;
1083         struct vfs_cred          vcred = { 0, 0 };
1084         char                     desc[64];
1085         int                      rc;
1086
1087         LASSERT(sec);
1088         LASSERT(svc_ctx);
1089         CWARN("called\n");
1090
1091         construct_key_desc(desc, sizeof(desc), sec, 0);
1092
1093         key = key_alloc(&gss_key_type, desc, 0, 0,
1094                         KEY_POS_ALL | KEY_USR_ALL, 1);
1095         if (IS_ERR(key)) {
1096                 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1097                 return PTR_ERR(key);
1098         }
1099
1100         rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1101         if (rc) {
1102                 CERROR("failed to instantiate key: %d\n", rc);
1103                 goto err_revoke;
1104         }
1105
1106         down_write(&key->sem);
1107
1108         LASSERT(key->payload.data == NULL);
1109
1110         cli_ctx = ctx_create_kr(sec, &vcred);
1111         if (cli_ctx == NULL) {
1112                 rc = -ENOMEM;
1113                 goto err_up;
1114         }
1115
1116         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1117         if (rc) {
1118                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1119                 goto err_put;
1120         }
1121
1122         rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1123
1124         ctx_put_kr(cli_ctx);
1125         up_write(&key->sem);
1126
1127         rc = 0;
1128         CWARN("ok!\n");
1129 out:
1130         key_put(key);
1131         return rc;
1132
1133 err_put:
1134         ctx_put_kr(cli_ctx);
1135 err_up:
1136         up_write(&key->sem);
1137 err_revoke:
1138         key_revoke(key);
1139         goto out;
1140 }
1141
1142 #endif /* HAVE_REVERSE_CTX_NOKEY */
1143
1144 /****************************************
1145  * service apis                         *
1146  ****************************************/
1147
1148 static
1149 int gss_svc_accept_kr(struct ptlrpc_request *req)
1150 {
1151         return gss_svc_accept(&gss_policy_keyring, req);
1152 }
1153
1154 static
1155 int gss_svc_install_rctx_kr(struct obd_import *imp,
1156                             struct ptlrpc_svc_ctx *svc_ctx)
1157 {
1158         LASSERT(imp->imp_sec);
1159
1160         return sec_install_rctx_kr(imp->imp_sec, svc_ctx);
1161 }
1162
1163 /****************************************
1164  * key apis                             *
1165  ****************************************/
1166
1167 static
1168 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1169 {
1170         ENTRY;
1171
1172         if (data != NULL || datalen != 0) {
1173                 CERROR("invalid: data %p, len %d\n", data, datalen);
1174                 RETURN(-EINVAL);
1175         }
1176
1177         if (key->payload.data != 0) {
1178                 CERROR("key already have payload\n");
1179                 RETURN(-EINVAL);
1180         }
1181
1182         /* XXX */
1183         key->perm |= KEY_POS_ALL | KEY_USR_ALL;
1184         CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1185         RETURN(0);
1186 }
1187
1188 /*
1189  * called with key semaphore write locked. it means we can operate
1190  * on the context without fear of loosing refcount.
1191  */
1192 static
1193 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1194 {
1195         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
1196         struct gss_cli_ctx      *gctx;
1197         rawobj_t                 tmpobj = RAWOBJ_EMPTY;
1198         int                      rc;
1199         ENTRY;
1200
1201         if (data == NULL || datalen == 0) {
1202                 CWARN("invalid: data %p, len %d\n", data, datalen);
1203                 RETURN(-EINVAL);
1204         }
1205
1206         /* there's a race between userspace parent - child processes. if
1207          * child finish negotiation too fast and call kt_update(), the ctx
1208          * might be still NULL. but the key will finally be associate
1209          * with a context, or be revoked. if key status is fine, return
1210          * -EAGAIN to allow userspace sleep a while and call again. */
1211         if (ctx == NULL) {
1212                 CWARN("race in userspace. key %p(%x) flags %lx\n",
1213                       key, key->serial, key->flags);
1214
1215                 rc = key_validate(key);
1216                 if (rc == 0)
1217                         RETURN(-EAGAIN);
1218                 else
1219                         RETURN(rc);
1220         }
1221
1222         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1223         LASSERT(ctx->cc_sec);
1224
1225         ctx_clear_timer_kr(ctx);
1226
1227         /* don't proceed if already refreshed */
1228         if (cli_ctx_is_refreshed(ctx)) {
1229                 CWARN("ctx already done refresh\n");
1230                 sptlrpc_cli_ctx_wakeup(ctx);
1231                 RETURN(0);
1232         }
1233
1234         sptlrpc_cli_ctx_get(ctx);
1235         gctx = ctx2gctx(ctx);
1236
1237         rc = buffer_extract_bytes(&data, &datalen, &gctx->gc_win,
1238                                   sizeof(gctx->gc_win));
1239         if (rc) {
1240                 CERROR("failed extract seq_win\n");
1241                 goto out;
1242         }
1243
1244         if (gctx->gc_win == 0) {
1245                 __u32   nego_rpc_err, nego_gss_err;
1246
1247                 rc = buffer_extract_bytes(&data, &datalen, &nego_rpc_err,
1248                                           sizeof(nego_rpc_err));
1249                 if (rc) {
1250                         CERROR("failed to extrace rpc rc\n");
1251                         goto out;
1252                 }
1253
1254                 rc = buffer_extract_bytes(&data, &datalen, &nego_gss_err,
1255                                           sizeof(nego_gss_err));
1256                 if (rc) {
1257                         CERROR("failed to extrace gss rc\n");
1258                         goto out;
1259                 }
1260
1261                 CERROR("negotiation: rpc err %d, gss err %x\n",
1262                        nego_rpc_err, nego_gss_err);
1263
1264                 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1265         } else {
1266                 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1267                                                 (__u32 **) &data, &datalen);
1268                 if (rc) {
1269                         CERROR("failed extract handle\n");
1270                         goto out;
1271                 }
1272
1273                 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen);
1274                 if (rc) {
1275                         CERROR("failed extract mech\n");
1276                         goto out;
1277                 }
1278
1279                 rc = lgss_import_sec_context(&tmpobj,
1280                                              sec2gsec(ctx->cc_sec)->gs_mech,
1281                                              &gctx->gc_mechctx);
1282                 if (rc != GSS_S_COMPLETE)
1283                         CERROR("failed import context\n");
1284                 else
1285                         rc = 0;
1286         }
1287 out:
1288         /* we don't care what current status of this ctx, even someone else
1289          * is operating on the ctx at the same time. we just add up our own
1290          * opinions here. */
1291         if (rc == 0) {
1292                 gss_cli_ctx_uptodate(gctx);
1293         } else {
1294                 /* this will also revoke the key. has to be done before
1295                  * wakeup waiters otherwise they can find the stale key */
1296                 kill_key_locked(key);
1297
1298                 cli_ctx_expire(ctx);
1299
1300                 if (rc != -ERESTART)
1301                         set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1302         }
1303
1304         sptlrpc_cli_ctx_wakeup(ctx);
1305
1306         /* let user space think it's a success */
1307         sptlrpc_cli_ctx_put(ctx, 1);
1308         RETURN(0);
1309 }
1310
1311 static
1312 int gss_kt_match(const struct key *key, const void *desc)
1313 {
1314         return (strcmp(key->description, (const char *) desc) == 0);
1315 }
1316
1317 static
1318 void gss_kt_destroy(struct key *key)
1319 {
1320         ENTRY;
1321         LASSERT(key->payload.data == NULL);
1322         CDEBUG(D_SEC, "destroy key %p\n", key);
1323         EXIT;
1324 }
1325
1326 static
1327 void gss_kt_describe(const struct key *key, struct seq_file *s)
1328 {
1329         if (key->description == NULL)
1330                 seq_puts(s, "[null]");
1331         else
1332                 seq_puts(s, key->description);
1333 }
1334
1335 static struct key_type gss_key_type =
1336 {
1337         .name           = "lgssc",
1338         .def_datalen    = 0,
1339         .instantiate    = gss_kt_instantiate,
1340         .update         = gss_kt_update,
1341         .match          = gss_kt_match,
1342         .destroy        = gss_kt_destroy,
1343         .describe       = gss_kt_describe,
1344 };
1345
1346 /****************************************
1347  * lustre gss keyring policy            *
1348  ****************************************/
1349
1350 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1351         .match                  = gss_cli_ctx_match,
1352         .refresh                = gss_cli_ctx_refresh_kr,
1353         .validate               = gss_cli_ctx_validate_kr,
1354         .die                    = gss_cli_ctx_die_kr,
1355         .sign                   = gss_cli_ctx_sign,
1356         .verify                 = gss_cli_ctx_verify,
1357         .seal                   = gss_cli_ctx_seal,
1358         .unseal                 = gss_cli_ctx_unseal,
1359         .wrap_bulk              = gss_cli_ctx_wrap_bulk,
1360         .unwrap_bulk            = gss_cli_ctx_unwrap_bulk,
1361 };
1362
1363 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1364         .create_sec             = gss_sec_create_kr,
1365         .destroy_sec            = gss_sec_destroy_kr,
1366         .lookup_ctx             = gss_sec_lookup_ctx_kr,
1367         .release_ctx            = gss_sec_release_ctx_kr,
1368         .flush_ctx_cache        = gss_sec_flush_ctx_cache_kr,
1369         .gc_ctx                 = gss_sec_gc_ctx_kr,
1370         .install_rctx           = gss_sec_install_rctx,
1371         .alloc_reqbuf           = gss_alloc_reqbuf,
1372         .free_reqbuf            = gss_free_reqbuf,
1373         .alloc_repbuf           = gss_alloc_repbuf,
1374         .free_repbuf            = gss_free_repbuf,
1375         .enlarge_reqbuf         = gss_enlarge_reqbuf,
1376         .display                = gss_sec_display_kr,
1377 };
1378
1379 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1380         .accept                 = gss_svc_accept_kr,
1381         .invalidate_ctx         = gss_svc_invalidate_ctx,
1382         .alloc_rs               = gss_svc_alloc_rs,
1383         .authorize              = gss_svc_authorize,
1384         .free_rs                = gss_svc_free_rs,
1385         .free_ctx               = gss_svc_free_ctx,
1386         .unwrap_bulk            = gss_svc_unwrap_bulk,
1387         .wrap_bulk              = gss_svc_wrap_bulk,
1388         .install_rctx           = gss_svc_install_rctx_kr,
1389 };
1390
1391 static struct ptlrpc_sec_policy gss_policy_keyring = {
1392         .sp_owner               = THIS_MODULE,
1393         .sp_name                = "gss.keyring",
1394         .sp_policy              = SPTLRPC_POLICY_GSS,
1395         .sp_cops                = &gss_sec_keyring_cops,
1396         .sp_sops                = &gss_sec_keyring_sops,
1397 };
1398
1399
1400 int __init gss_init_keyring(void)
1401 {
1402         int rc;
1403
1404         rc = register_key_type(&gss_key_type);
1405         if (rc) {
1406                 CERROR("failed to register keyring type: %d\n", rc);
1407                 return rc;
1408         }
1409
1410         rc = sptlrpc_register_policy(&gss_policy_keyring);
1411         if (rc) {
1412                 unregister_key_type(&gss_key_type);
1413                 return rc;
1414         }
1415
1416         return 0;
1417 }
1418
1419 void __exit gss_exit_keyring(void)
1420 {
1421         unregister_key_type(&gss_key_type);
1422         sptlrpc_unregister_policy(&gss_policy_keyring);
1423 }