oom: check PF_KTHREAD instead of !mm to skip kthreads
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / auth.c
blob880d0de3f50fe14c4ce3daba30ab11266f53948c
1 /*
2 * linux/net/sunrpc/auth.c
4 * Generic RPC client authentication API.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/hash.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/spinlock.h>
18 #ifdef RPC_DEBUG
19 # define RPCDBG_FACILITY RPCDBG_AUTH
20 #endif
22 #define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
23 struct rpc_cred_cache {
24 struct hlist_head *hashtable;
25 unsigned int hashbits;
26 spinlock_t lock;
29 static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31 static DEFINE_SPINLOCK(rpc_authflavor_lock);
32 static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
33 &authnull_ops, /* AUTH_NULL */
34 &authunix_ops, /* AUTH_UNIX */
35 NULL, /* others can be loadable modules */
38 static LIST_HEAD(cred_unused);
39 static unsigned long number_cred_unused;
41 #define MAX_HASHTABLE_BITS (10)
42 static int param_set_hashtbl_sz(const char *val, struct kernel_param *kp)
44 unsigned long num;
45 unsigned int nbits;
46 int ret;
48 if (!val)
49 goto out_inval;
50 ret = strict_strtoul(val, 0, &num);
51 if (ret == -EINVAL)
52 goto out_inval;
53 nbits = fls(num);
54 if (num > (1U << nbits))
55 nbits++;
56 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
57 goto out_inval;
58 *(unsigned int *)kp->arg = nbits;
59 return 0;
60 out_inval:
61 return -EINVAL;
64 static int param_get_hashtbl_sz(char *buffer, struct kernel_param *kp)
66 unsigned int nbits;
68 nbits = *(unsigned int *)kp->arg;
69 return sprintf(buffer, "%u", 1U << nbits);
72 #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74 module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
75 MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
77 static u32
78 pseudoflavor_to_flavor(u32 flavor) {
79 if (flavor >= RPC_AUTH_MAXFLAVOR)
80 return RPC_AUTH_GSS;
81 return flavor;
84 int
85 rpcauth_register(const struct rpc_authops *ops)
87 rpc_authflavor_t flavor;
88 int ret = -EPERM;
90 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
91 return -EINVAL;
92 spin_lock(&rpc_authflavor_lock);
93 if (auth_flavors[flavor] == NULL) {
94 auth_flavors[flavor] = ops;
95 ret = 0;
97 spin_unlock(&rpc_authflavor_lock);
98 return ret;
100 EXPORT_SYMBOL_GPL(rpcauth_register);
103 rpcauth_unregister(const struct rpc_authops *ops)
105 rpc_authflavor_t flavor;
106 int ret = -EPERM;
108 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
109 return -EINVAL;
110 spin_lock(&rpc_authflavor_lock);
111 if (auth_flavors[flavor] == ops) {
112 auth_flavors[flavor] = NULL;
113 ret = 0;
115 spin_unlock(&rpc_authflavor_lock);
116 return ret;
118 EXPORT_SYMBOL_GPL(rpcauth_unregister);
120 struct rpc_auth *
121 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
123 struct rpc_auth *auth;
124 const struct rpc_authops *ops;
125 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
127 auth = ERR_PTR(-EINVAL);
128 if (flavor >= RPC_AUTH_MAXFLAVOR)
129 goto out;
131 if ((ops = auth_flavors[flavor]) == NULL)
132 request_module("rpc-auth-%u", flavor);
133 spin_lock(&rpc_authflavor_lock);
134 ops = auth_flavors[flavor];
135 if (ops == NULL || !try_module_get(ops->owner)) {
136 spin_unlock(&rpc_authflavor_lock);
137 goto out;
139 spin_unlock(&rpc_authflavor_lock);
140 auth = ops->create(clnt, pseudoflavor);
141 module_put(ops->owner);
142 if (IS_ERR(auth))
143 return auth;
144 if (clnt->cl_auth)
145 rpcauth_release(clnt->cl_auth);
146 clnt->cl_auth = auth;
148 out:
149 return auth;
151 EXPORT_SYMBOL_GPL(rpcauth_create);
153 void
154 rpcauth_release(struct rpc_auth *auth)
156 if (!atomic_dec_and_test(&auth->au_count))
157 return;
158 auth->au_ops->destroy(auth);
161 static DEFINE_SPINLOCK(rpc_credcache_lock);
163 static void
164 rpcauth_unhash_cred_locked(struct rpc_cred *cred)
166 hlist_del_rcu(&cred->cr_hash);
167 smp_mb__before_clear_bit();
168 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
171 static int
172 rpcauth_unhash_cred(struct rpc_cred *cred)
174 spinlock_t *cache_lock;
175 int ret;
177 cache_lock = &cred->cr_auth->au_credcache->lock;
178 spin_lock(cache_lock);
179 ret = atomic_read(&cred->cr_count) == 0;
180 if (ret)
181 rpcauth_unhash_cred_locked(cred);
182 spin_unlock(cache_lock);
183 return ret;
187 * Initialize RPC credential cache
190 rpcauth_init_credcache(struct rpc_auth *auth)
192 struct rpc_cred_cache *new;
193 unsigned int hashsize;
195 new = kmalloc(sizeof(*new), GFP_KERNEL);
196 if (!new)
197 goto out_nocache;
198 new->hashbits = auth_hashbits;
199 hashsize = 1U << new->hashbits;
200 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
201 if (!new->hashtable)
202 goto out_nohashtbl;
203 spin_lock_init(&new->lock);
204 auth->au_credcache = new;
205 return 0;
206 out_nohashtbl:
207 kfree(new);
208 out_nocache:
209 return -ENOMEM;
211 EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
214 * Destroy a list of credentials
216 static inline
217 void rpcauth_destroy_credlist(struct list_head *head)
219 struct rpc_cred *cred;
221 while (!list_empty(head)) {
222 cred = list_entry(head->next, struct rpc_cred, cr_lru);
223 list_del_init(&cred->cr_lru);
224 put_rpccred(cred);
229 * Clear the RPC credential cache, and delete those credentials
230 * that are not referenced.
232 void
233 rpcauth_clear_credcache(struct rpc_cred_cache *cache)
235 LIST_HEAD(free);
236 struct hlist_head *head;
237 struct rpc_cred *cred;
238 unsigned int hashsize = 1U << cache->hashbits;
239 int i;
241 spin_lock(&rpc_credcache_lock);
242 spin_lock(&cache->lock);
243 for (i = 0; i < hashsize; i++) {
244 head = &cache->hashtable[i];
245 while (!hlist_empty(head)) {
246 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
247 get_rpccred(cred);
248 if (!list_empty(&cred->cr_lru)) {
249 list_del(&cred->cr_lru);
250 number_cred_unused--;
252 list_add_tail(&cred->cr_lru, &free);
253 rpcauth_unhash_cred_locked(cred);
256 spin_unlock(&cache->lock);
257 spin_unlock(&rpc_credcache_lock);
258 rpcauth_destroy_credlist(&free);
262 * Destroy the RPC credential cache
264 void
265 rpcauth_destroy_credcache(struct rpc_auth *auth)
267 struct rpc_cred_cache *cache = auth->au_credcache;
269 if (cache) {
270 auth->au_credcache = NULL;
271 rpcauth_clear_credcache(cache);
272 kfree(cache->hashtable);
273 kfree(cache);
276 EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
279 #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
282 * Remove stale credentials. Avoid sleeping inside the loop.
284 static int
285 rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
287 spinlock_t *cache_lock;
288 struct rpc_cred *cred, *next;
289 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
291 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
293 if (nr_to_scan-- == 0)
294 break;
296 * Enforce a 60 second garbage collection moratorium
297 * Note that the cred_unused list must be time-ordered.
299 if (time_in_range(cred->cr_expire, expired, jiffies) &&
300 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
301 return 0;
303 list_del_init(&cred->cr_lru);
304 number_cred_unused--;
305 if (atomic_read(&cred->cr_count) != 0)
306 continue;
308 cache_lock = &cred->cr_auth->au_credcache->lock;
309 spin_lock(cache_lock);
310 if (atomic_read(&cred->cr_count) == 0) {
311 get_rpccred(cred);
312 list_add_tail(&cred->cr_lru, free);
313 rpcauth_unhash_cred_locked(cred);
315 spin_unlock(cache_lock);
317 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
321 * Run memory cache shrinker.
323 static int
324 rpcauth_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
326 LIST_HEAD(free);
327 int res;
329 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
330 return (nr_to_scan == 0) ? 0 : -1;
331 if (list_empty(&cred_unused))
332 return 0;
333 spin_lock(&rpc_credcache_lock);
334 res = rpcauth_prune_expired(&free, nr_to_scan);
335 spin_unlock(&rpc_credcache_lock);
336 rpcauth_destroy_credlist(&free);
337 return res;
341 * Look up a process' credentials in the authentication cache
343 struct rpc_cred *
344 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
345 int flags)
347 LIST_HEAD(free);
348 struct rpc_cred_cache *cache = auth->au_credcache;
349 struct hlist_node *pos;
350 struct rpc_cred *cred = NULL,
351 *entry, *new;
352 unsigned int nr;
354 nr = hash_long(acred->uid, cache->hashbits);
356 rcu_read_lock();
357 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
358 if (!entry->cr_ops->crmatch(acred, entry, flags))
359 continue;
360 spin_lock(&cache->lock);
361 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
362 spin_unlock(&cache->lock);
363 continue;
365 cred = get_rpccred(entry);
366 spin_unlock(&cache->lock);
367 break;
369 rcu_read_unlock();
371 if (cred != NULL)
372 goto found;
374 new = auth->au_ops->crcreate(auth, acred, flags);
375 if (IS_ERR(new)) {
376 cred = new;
377 goto out;
380 spin_lock(&cache->lock);
381 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
382 if (!entry->cr_ops->crmatch(acred, entry, flags))
383 continue;
384 cred = get_rpccred(entry);
385 break;
387 if (cred == NULL) {
388 cred = new;
389 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
390 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
391 } else
392 list_add_tail(&new->cr_lru, &free);
393 spin_unlock(&cache->lock);
394 found:
395 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
396 cred->cr_ops->cr_init != NULL &&
397 !(flags & RPCAUTH_LOOKUP_NEW)) {
398 int res = cred->cr_ops->cr_init(auth, cred);
399 if (res < 0) {
400 put_rpccred(cred);
401 cred = ERR_PTR(res);
404 rpcauth_destroy_credlist(&free);
405 out:
406 return cred;
408 EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
410 struct rpc_cred *
411 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
413 struct auth_cred acred;
414 struct rpc_cred *ret;
415 const struct cred *cred = current_cred();
417 dprintk("RPC: looking up %s cred\n",
418 auth->au_ops->au_name);
420 memset(&acred, 0, sizeof(acred));
421 acred.uid = cred->fsuid;
422 acred.gid = cred->fsgid;
423 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
425 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
426 put_group_info(acred.group_info);
427 return ret;
430 void
431 rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
432 struct rpc_auth *auth, const struct rpc_credops *ops)
434 INIT_HLIST_NODE(&cred->cr_hash);
435 INIT_LIST_HEAD(&cred->cr_lru);
436 atomic_set(&cred->cr_count, 1);
437 cred->cr_auth = auth;
438 cred->cr_ops = ops;
439 cred->cr_expire = jiffies;
440 #ifdef RPC_DEBUG
441 cred->cr_magic = RPCAUTH_CRED_MAGIC;
442 #endif
443 cred->cr_uid = acred->uid;
445 EXPORT_SYMBOL_GPL(rpcauth_init_cred);
447 struct rpc_cred *
448 rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
450 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
451 cred->cr_auth->au_ops->au_name, cred);
452 return get_rpccred(cred);
454 EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
456 static struct rpc_cred *
457 rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
459 struct rpc_auth *auth = task->tk_client->cl_auth;
460 struct auth_cred acred = {
461 .uid = 0,
462 .gid = 0,
465 dprintk("RPC: %5u looking up %s cred\n",
466 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
467 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
470 static struct rpc_cred *
471 rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
473 struct rpc_auth *auth = task->tk_client->cl_auth;
475 dprintk("RPC: %5u looking up %s cred\n",
476 task->tk_pid, auth->au_ops->au_name);
477 return rpcauth_lookupcred(auth, lookupflags);
480 static int
481 rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
483 struct rpc_rqst *req = task->tk_rqstp;
484 struct rpc_cred *new;
485 int lookupflags = 0;
487 if (flags & RPC_TASK_ASYNC)
488 lookupflags |= RPCAUTH_LOOKUP_NEW;
489 if (cred != NULL)
490 new = cred->cr_ops->crbind(task, cred, lookupflags);
491 else if (flags & RPC_TASK_ROOTCREDS)
492 new = rpcauth_bind_root_cred(task, lookupflags);
493 else
494 new = rpcauth_bind_new_cred(task, lookupflags);
495 if (IS_ERR(new))
496 return PTR_ERR(new);
497 if (req->rq_cred != NULL)
498 put_rpccred(req->rq_cred);
499 req->rq_cred = new;
500 return 0;
503 void
504 put_rpccred(struct rpc_cred *cred)
506 /* Fast path for unhashed credentials */
507 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
508 if (atomic_dec_and_test(&cred->cr_count))
509 cred->cr_ops->crdestroy(cred);
510 return;
513 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
514 return;
515 if (!list_empty(&cred->cr_lru)) {
516 number_cred_unused--;
517 list_del_init(&cred->cr_lru);
519 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
520 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
521 cred->cr_expire = jiffies;
522 list_add_tail(&cred->cr_lru, &cred_unused);
523 number_cred_unused++;
524 goto out_nodestroy;
526 if (!rpcauth_unhash_cred(cred)) {
527 /* We were hashed and someone looked us up... */
528 goto out_nodestroy;
531 spin_unlock(&rpc_credcache_lock);
532 cred->cr_ops->crdestroy(cred);
533 return;
534 out_nodestroy:
535 spin_unlock(&rpc_credcache_lock);
537 EXPORT_SYMBOL_GPL(put_rpccred);
539 __be32 *
540 rpcauth_marshcred(struct rpc_task *task, __be32 *p)
542 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
544 dprintk("RPC: %5u marshaling %s cred %p\n",
545 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
547 return cred->cr_ops->crmarshal(task, p);
550 __be32 *
551 rpcauth_checkverf(struct rpc_task *task, __be32 *p)
553 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
555 dprintk("RPC: %5u validating %s cred %p\n",
556 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
558 return cred->cr_ops->crvalidate(task, p);
562 rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
563 __be32 *data, void *obj)
565 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
567 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
568 task->tk_pid, cred->cr_ops->cr_name, cred);
569 if (cred->cr_ops->crwrap_req)
570 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
571 /* By default, we encode the arguments normally. */
572 return encode(rqstp, data, obj);
576 rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
577 __be32 *data, void *obj)
579 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
581 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
582 task->tk_pid, cred->cr_ops->cr_name, cred);
583 if (cred->cr_ops->crunwrap_resp)
584 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
585 data, obj);
586 /* By default, we decode the arguments normally. */
587 return decode(rqstp, data, obj);
591 rpcauth_refreshcred(struct rpc_task *task)
593 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
594 int err;
596 cred = task->tk_rqstp->rq_cred;
597 if (cred == NULL) {
598 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
599 if (err < 0)
600 goto out;
601 cred = task->tk_rqstp->rq_cred;
603 dprintk("RPC: %5u refreshing %s cred %p\n",
604 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
606 err = cred->cr_ops->crrefresh(task);
607 out:
608 if (err < 0)
609 task->tk_status = err;
610 return err;
613 void
614 rpcauth_invalcred(struct rpc_task *task)
616 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
618 dprintk("RPC: %5u invalidating %s cred %p\n",
619 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
620 if (cred)
621 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
625 rpcauth_uptodatecred(struct rpc_task *task)
627 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
629 return cred == NULL ||
630 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
633 static struct shrinker rpc_cred_shrinker = {
634 .shrink = rpcauth_cache_shrinker,
635 .seeks = DEFAULT_SEEKS,
638 int __init rpcauth_init_module(void)
640 int err;
642 err = rpc_init_authunix();
643 if (err < 0)
644 goto out1;
645 err = rpc_init_generic_auth();
646 if (err < 0)
647 goto out2;
648 register_shrinker(&rpc_cred_shrinker);
649 return 0;
650 out2:
651 rpc_destroy_authunix();
652 out1:
653 return err;
656 void __exit rpcauth_remove_module(void)
658 rpc_destroy_authunix();
659 rpc_destroy_generic_auth();
660 unregister_shrinker(&rpc_cred_shrinker);