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