aboutsummaryrefslogtreecommitdiffstats
path: root/net/sunrpc/auth.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/sunrpc/auth.c')
-rw-r--r--net/sunrpc/auth.c185
1 files changed, 127 insertions, 58 deletions
diff --git a/net/sunrpc/auth.c b/net/sunrpc/auth.c
index f394fc190a49..e9eaaf7d43c1 100644
--- a/net/sunrpc/auth.c
+++ b/net/sunrpc/auth.c
@@ -19,6 +19,15 @@
19# define RPCDBG_FACILITY RPCDBG_AUTH 19# define RPCDBG_FACILITY RPCDBG_AUTH
20#endif 20#endif
21 21
22#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
23struct rpc_cred_cache {
24 struct hlist_head *hashtable;
25 unsigned int hashbits;
26 spinlock_t lock;
27};
28
29static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
30
22static DEFINE_SPINLOCK(rpc_authflavor_lock); 31static DEFINE_SPINLOCK(rpc_authflavor_lock);
23static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { 32static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
24 &authnull_ops, /* AUTH_NULL */ 33 &authnull_ops, /* AUTH_NULL */
@@ -29,6 +38,47 @@ static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
29static LIST_HEAD(cred_unused); 38static LIST_HEAD(cred_unused);
30static unsigned long number_cred_unused; 39static unsigned long number_cred_unused;
31 40
41#define MAX_HASHTABLE_BITS (14)
42static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
43{
44 unsigned long num;
45 unsigned int nbits;
46 int ret;
47
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;
60out_inval:
61 return -EINVAL;
62}
63
64static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
65{
66 unsigned int nbits;
67
68 nbits = *(unsigned int *)kp->arg;
69 return sprintf(buffer, "%u", 1U << nbits);
70}
71
72#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
73
74static struct kernel_param_ops param_ops_hashtbl_sz = {
75 .set = param_set_hashtbl_sz,
76 .get = param_get_hashtbl_sz,
77};
78
79module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
80MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
81
32static u32 82static u32
33pseudoflavor_to_flavor(u32 flavor) { 83pseudoflavor_to_flavor(u32 flavor) {
34 if (flavor >= RPC_AUTH_MAXFLAVOR) 84 if (flavor >= RPC_AUTH_MAXFLAVOR)
@@ -145,16 +195,23 @@ int
145rpcauth_init_credcache(struct rpc_auth *auth) 195rpcauth_init_credcache(struct rpc_auth *auth)
146{ 196{
147 struct rpc_cred_cache *new; 197 struct rpc_cred_cache *new;
148 int i; 198 unsigned int hashsize;
149 199
150 new = kmalloc(sizeof(*new), GFP_KERNEL); 200 new = kmalloc(sizeof(*new), GFP_KERNEL);
151 if (!new) 201 if (!new)
152 return -ENOMEM; 202 goto out_nocache;
153 for (i = 0; i < RPC_CREDCACHE_NR; i++) 203 new->hashbits = auth_hashbits;
154 INIT_HLIST_HEAD(&new->hashtable[i]); 204 hashsize = 1U << new->hashbits;
205 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
206 if (!new->hashtable)
207 goto out_nohashtbl;
155 spin_lock_init(&new->lock); 208 spin_lock_init(&new->lock);
156 auth->au_credcache = new; 209 auth->au_credcache = new;
157 return 0; 210 return 0;
211out_nohashtbl:
212 kfree(new);
213out_nocache:
214 return -ENOMEM;
158} 215}
159EXPORT_SYMBOL_GPL(rpcauth_init_credcache); 216EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
160 217
@@ -183,11 +240,12 @@ rpcauth_clear_credcache(struct rpc_cred_cache *cache)
183 LIST_HEAD(free); 240 LIST_HEAD(free);
184 struct hlist_head *head; 241 struct hlist_head *head;
185 struct rpc_cred *cred; 242 struct rpc_cred *cred;
243 unsigned int hashsize = 1U << cache->hashbits;
186 int i; 244 int i;
187 245
188 spin_lock(&rpc_credcache_lock); 246 spin_lock(&rpc_credcache_lock);
189 spin_lock(&cache->lock); 247 spin_lock(&cache->lock);
190 for (i = 0; i < RPC_CREDCACHE_NR; i++) { 248 for (i = 0; i < hashsize; i++) {
191 head = &cache->hashtable[i]; 249 head = &cache->hashtable[i];
192 while (!hlist_empty(head)) { 250 while (!hlist_empty(head)) {
193 cred = hlist_entry(head->first, struct rpc_cred, cr_hash); 251 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
@@ -216,6 +274,7 @@ rpcauth_destroy_credcache(struct rpc_auth *auth)
216 if (cache) { 274 if (cache) {
217 auth->au_credcache = NULL; 275 auth->au_credcache = NULL;
218 rpcauth_clear_credcache(cache); 276 rpcauth_clear_credcache(cache);
277 kfree(cache->hashtable);
219 kfree(cache); 278 kfree(cache);
220 } 279 }
221} 280}
@@ -236,10 +295,15 @@ rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
236 295
237 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { 296 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
238 297
239 /* Enforce a 60 second garbage collection moratorium */ 298 if (nr_to_scan-- == 0)
240 if (time_in_range_open(cred->cr_expire, expired, jiffies) && 299 break;
300 /*
301 * Enforce a 60 second garbage collection moratorium
302 * Note that the cred_unused list must be time-ordered.
303 */
304 if (time_in_range(cred->cr_expire, expired, jiffies) &&
241 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) 305 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
242 continue; 306 return 0;
243 307
244 list_del_init(&cred->cr_lru); 308 list_del_init(&cred->cr_lru);
245 number_cred_unused--; 309 number_cred_unused--;
@@ -252,29 +316,27 @@ rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
252 get_rpccred(cred); 316 get_rpccred(cred);
253 list_add_tail(&cred->cr_lru, free); 317 list_add_tail(&cred->cr_lru, free);
254 rpcauth_unhash_cred_locked(cred); 318 rpcauth_unhash_cred_locked(cred);
255 nr_to_scan--;
256 } 319 }
257 spin_unlock(cache_lock); 320 spin_unlock(cache_lock);
258 if (nr_to_scan == 0)
259 break;
260 } 321 }
261 return nr_to_scan; 322 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
262} 323}
263 324
264/* 325/*
265 * Run memory cache shrinker. 326 * Run memory cache shrinker.
266 */ 327 */
267static int 328static int
268rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask) 329rpcauth_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
269{ 330{
270 LIST_HEAD(free); 331 LIST_HEAD(free);
271 int res; 332 int res;
272 333
334 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
335 return (nr_to_scan == 0) ? 0 : -1;
273 if (list_empty(&cred_unused)) 336 if (list_empty(&cred_unused))
274 return 0; 337 return 0;
275 spin_lock(&rpc_credcache_lock); 338 spin_lock(&rpc_credcache_lock);
276 nr_to_scan = rpcauth_prune_expired(&free, nr_to_scan); 339 res = rpcauth_prune_expired(&free, nr_to_scan);
277 res = (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
278 spin_unlock(&rpc_credcache_lock); 340 spin_unlock(&rpc_credcache_lock);
279 rpcauth_destroy_credlist(&free); 341 rpcauth_destroy_credlist(&free);
280 return res; 342 return res;
@@ -294,7 +356,7 @@ rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
294 *entry, *new; 356 *entry, *new;
295 unsigned int nr; 357 unsigned int nr;
296 358
297 nr = hash_long(acred->uid, RPC_CREDCACHE_HASHBITS); 359 nr = hash_long(acred->uid, cache->hashbits);
298 360
299 rcu_read_lock(); 361 rcu_read_lock();
300 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { 362 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
@@ -387,16 +449,16 @@ rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
387} 449}
388EXPORT_SYMBOL_GPL(rpcauth_init_cred); 450EXPORT_SYMBOL_GPL(rpcauth_init_cred);
389 451
390void 452struct rpc_cred *
391rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) 453rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
392{ 454{
393 task->tk_msg.rpc_cred = get_rpccred(cred);
394 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, 455 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
395 cred->cr_auth->au_ops->au_name, cred); 456 cred->cr_auth->au_ops->au_name, cred);
457 return get_rpccred(cred);
396} 458}
397EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); 459EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
398 460
399static void 461static struct rpc_cred *
400rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) 462rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
401{ 463{
402 struct rpc_auth *auth = task->tk_client->cl_auth; 464 struct rpc_auth *auth = task->tk_client->cl_auth;
@@ -404,45 +466,43 @@ rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
404 .uid = 0, 466 .uid = 0,
405 .gid = 0, 467 .gid = 0,
406 }; 468 };
407 struct rpc_cred *ret;
408 469
409 dprintk("RPC: %5u looking up %s cred\n", 470 dprintk("RPC: %5u looking up %s cred\n",
410 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); 471 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
411 ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags); 472 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
412 if (!IS_ERR(ret))
413 task->tk_msg.rpc_cred = ret;
414 else
415 task->tk_status = PTR_ERR(ret);
416} 473}
417 474
418static void 475static struct rpc_cred *
419rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) 476rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
420{ 477{
421 struct rpc_auth *auth = task->tk_client->cl_auth; 478 struct rpc_auth *auth = task->tk_client->cl_auth;
422 struct rpc_cred *ret;
423 479
424 dprintk("RPC: %5u looking up %s cred\n", 480 dprintk("RPC: %5u looking up %s cred\n",
425 task->tk_pid, auth->au_ops->au_name); 481 task->tk_pid, auth->au_ops->au_name);
426 ret = rpcauth_lookupcred(auth, lookupflags); 482 return rpcauth_lookupcred(auth, lookupflags);
427 if (!IS_ERR(ret))
428 task->tk_msg.rpc_cred = ret;
429 else
430 task->tk_status = PTR_ERR(ret);
431} 483}
432 484
433void 485static int
434rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) 486rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
435{ 487{
488 struct rpc_rqst *req = task->tk_rqstp;
489 struct rpc_cred *new;
436 int lookupflags = 0; 490 int lookupflags = 0;
437 491
438 if (flags & RPC_TASK_ASYNC) 492 if (flags & RPC_TASK_ASYNC)
439 lookupflags |= RPCAUTH_LOOKUP_NEW; 493 lookupflags |= RPCAUTH_LOOKUP_NEW;
440 if (cred != NULL) 494 if (cred != NULL)
441 cred->cr_ops->crbind(task, cred, lookupflags); 495 new = cred->cr_ops->crbind(task, cred, lookupflags);
442 else if (flags & RPC_TASK_ROOTCREDS) 496 else if (flags & RPC_TASK_ROOTCREDS)
443 rpcauth_bind_root_cred(task, lookupflags); 497 new = rpcauth_bind_root_cred(task, lookupflags);
444 else 498 else
445 rpcauth_bind_new_cred(task, lookupflags); 499 new = rpcauth_bind_new_cred(task, lookupflags);
500 if (IS_ERR(new))
501 return PTR_ERR(new);
502 if (req->rq_cred != NULL)
503 put_rpccred(req->rq_cred);
504 req->rq_cred = new;
505 return 0;
446} 506}
447 507
448void 508void
@@ -481,22 +541,10 @@ out_nodestroy:
481} 541}
482EXPORT_SYMBOL_GPL(put_rpccred); 542EXPORT_SYMBOL_GPL(put_rpccred);
483 543
484void
485rpcauth_unbindcred(struct rpc_task *task)
486{
487 struct rpc_cred *cred = task->tk_msg.rpc_cred;
488
489 dprintk("RPC: %5u releasing %s cred %p\n",
490 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
491
492 put_rpccred(cred);
493 task->tk_msg.rpc_cred = NULL;
494}
495
496__be32 * 544__be32 *
497rpcauth_marshcred(struct rpc_task *task, __be32 *p) 545rpcauth_marshcred(struct rpc_task *task, __be32 *p)
498{ 546{
499 struct rpc_cred *cred = task->tk_msg.rpc_cred; 547 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
500 548
501 dprintk("RPC: %5u marshaling %s cred %p\n", 549 dprintk("RPC: %5u marshaling %s cred %p\n",
502 task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 550 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
@@ -507,7 +555,7 @@ rpcauth_marshcred(struct rpc_task *task, __be32 *p)
507__be32 * 555__be32 *
508rpcauth_checkverf(struct rpc_task *task, __be32 *p) 556rpcauth_checkverf(struct rpc_task *task, __be32 *p)
509{ 557{
510 struct rpc_cred *cred = task->tk_msg.rpc_cred; 558 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
511 559
512 dprintk("RPC: %5u validating %s cred %p\n", 560 dprintk("RPC: %5u validating %s cred %p\n",
513 task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 561 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
@@ -519,7 +567,7 @@ int
519rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, 567rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
520 __be32 *data, void *obj) 568 __be32 *data, void *obj)
521{ 569{
522 struct rpc_cred *cred = task->tk_msg.rpc_cred; 570 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
523 571
524 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", 572 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
525 task->tk_pid, cred->cr_ops->cr_name, cred); 573 task->tk_pid, cred->cr_ops->cr_name, cred);
@@ -533,7 +581,7 @@ int
533rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, 581rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
534 __be32 *data, void *obj) 582 __be32 *data, void *obj)
535{ 583{
536 struct rpc_cred *cred = task->tk_msg.rpc_cred; 584 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
537 585
538 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", 586 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
539 task->tk_pid, cred->cr_ops->cr_name, cred); 587 task->tk_pid, cred->cr_ops->cr_name, cred);
@@ -547,13 +595,21 @@ rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
547int 595int
548rpcauth_refreshcred(struct rpc_task *task) 596rpcauth_refreshcred(struct rpc_task *task)
549{ 597{
550 struct rpc_cred *cred = task->tk_msg.rpc_cred; 598 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
551 int err; 599 int err;
552 600
601 cred = task->tk_rqstp->rq_cred;
602 if (cred == NULL) {
603 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
604 if (err < 0)
605 goto out;
606 cred = task->tk_rqstp->rq_cred;
607 };
553 dprintk("RPC: %5u refreshing %s cred %p\n", 608 dprintk("RPC: %5u refreshing %s cred %p\n",
554 task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 609 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
555 610
556 err = cred->cr_ops->crrefresh(task); 611 err = cred->cr_ops->crrefresh(task);
612out:
557 if (err < 0) 613 if (err < 0)
558 task->tk_status = err; 614 task->tk_status = err;
559 return err; 615 return err;
@@ -562,7 +618,7 @@ rpcauth_refreshcred(struct rpc_task *task)
562void 618void
563rpcauth_invalcred(struct rpc_task *task) 619rpcauth_invalcred(struct rpc_task *task)
564{ 620{
565 struct rpc_cred *cred = task->tk_msg.rpc_cred; 621 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
566 622
567 dprintk("RPC: %5u invalidating %s cred %p\n", 623 dprintk("RPC: %5u invalidating %s cred %p\n",
568 task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 624 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
@@ -573,7 +629,7 @@ rpcauth_invalcred(struct rpc_task *task)
573int 629int
574rpcauth_uptodatecred(struct rpc_task *task) 630rpcauth_uptodatecred(struct rpc_task *task)
575{ 631{
576 struct rpc_cred *cred = task->tk_msg.rpc_cred; 632 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
577 633
578 return cred == NULL || 634 return cred == NULL ||
579 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; 635 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
@@ -584,14 +640,27 @@ static struct shrinker rpc_cred_shrinker = {
584 .seeks = DEFAULT_SEEKS, 640 .seeks = DEFAULT_SEEKS,
585}; 641};
586 642
587void __init rpcauth_init_module(void) 643int __init rpcauth_init_module(void)
588{ 644{
589 rpc_init_authunix(); 645 int err;
590 rpc_init_generic_auth(); 646
647 err = rpc_init_authunix();
648 if (err < 0)
649 goto out1;
650 err = rpc_init_generic_auth();
651 if (err < 0)
652 goto out2;
591 register_shrinker(&rpc_cred_shrinker); 653 register_shrinker(&rpc_cred_shrinker);
654 return 0;
655out2:
656 rpc_destroy_authunix();
657out1:
658 return err;
592} 659}
593 660
594void __exit rpcauth_remove_module(void) 661void __exit rpcauth_remove_module(void)
595{ 662{
663 rpc_destroy_authunix();
664 rpc_destroy_generic_auth();
596 unregister_shrinker(&rpc_cred_shrinker); 665 unregister_shrinker(&rpc_cred_shrinker);
597} 666}