blob: 415159061cd0bf8e9a3de956b7d53c585f623744 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/auth.c
3 *
4 * Generic RPC client authentication API.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
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>
Trond Myklebust25337fd2008-03-12 14:40:14 -040014#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Chuck Lever6a1a1e32012-07-11 16:31:08 -040016#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18
19#ifdef RPC_DEBUG
20# define RPCDBG_FACILITY RPCDBG_AUTH
21#endif
22
Trond Myklebust241269b2010-07-31 14:29:08 -040023#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
24struct rpc_cred_cache {
25 struct hlist_head *hashtable;
26 unsigned int hashbits;
27 spinlock_t lock;
28};
29
30static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31
Trond Myklebustfc1b3562007-06-09 16:15:46 -040032static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040033static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 &authnull_ops, /* AUTH_NULL */
35 &authunix_ops, /* AUTH_UNIX */
36 NULL, /* others can be loadable modules */
37};
38
Trond Myklebuste092bdc2007-06-23 19:45:36 -040039static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040040static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040041
Miquel van Smoorenburgdb5fe262010-09-12 19:55:26 -040042#define MAX_HASHTABLE_BITS (14)
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100043static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040044{
45 unsigned long num;
46 unsigned int nbits;
47 int ret;
48
49 if (!val)
50 goto out_inval;
51 ret = strict_strtoul(val, 0, &num);
52 if (ret == -EINVAL)
53 goto out_inval;
54 nbits = fls(num);
55 if (num > (1U << nbits))
56 nbits++;
57 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
58 goto out_inval;
59 *(unsigned int *)kp->arg = nbits;
60 return 0;
61out_inval:
62 return -EINVAL;
63}
64
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100065static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040066{
67 unsigned int nbits;
68
69 nbits = *(unsigned int *)kp->arg;
70 return sprintf(buffer, "%u", 1U << nbits);
71}
72
73#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100075static struct kernel_param_ops param_ops_hashtbl_sz = {
76 .set = param_set_hashtbl_sz,
77 .get = param_get_hashtbl_sz,
78};
79
Trond Myklebust241269b2010-07-31 14:29:08 -040080module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static u32
84pseudoflavor_to_flavor(u32 flavor) {
Chuck Lever1c74a242013-03-22 12:53:08 -040085 if (flavor > RPC_AUTH_MAXFLAVOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return RPC_AUTH_GSS;
87 return flavor;
88}
89
90int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040091rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040094 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
97 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040098 spin_lock(&rpc_authflavor_lock);
99 if (auth_flavors[flavor] == NULL) {
100 auth_flavors[flavor] = ops;
101 ret = 0;
102 }
103 spin_unlock(&rpc_authflavor_lock);
104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400106EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400109rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400112 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
115 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400116 spin_lock(&rpc_authflavor_lock);
117 if (auth_flavors[flavor] == ops) {
118 auth_flavors[flavor] = NULL;
119 ret = 0;
120 }
121 spin_unlock(&rpc_authflavor_lock);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400124EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400126/**
Chuck Lever9568c5e2013-03-16 15:54:43 -0400127 * rpcauth_get_pseudoflavor - check if security flavor is supported
128 * @flavor: a security flavor
129 * @info: a GSS mech OID, quality of protection, and service value
130 *
131 * Verifies that an appropriate kernel module is available or already loaded.
132 * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
133 * not supported locally.
134 */
135rpc_authflavor_t
136rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
137{
138 const struct rpc_authops *ops;
139 rpc_authflavor_t pseudoflavor;
140
141 ops = auth_flavors[flavor];
142 if (ops == NULL)
143 request_module("rpc-auth-%u", flavor);
144 spin_lock(&rpc_authflavor_lock);
145 ops = auth_flavors[flavor];
146 if (ops == NULL || !try_module_get(ops->owner)) {
147 spin_unlock(&rpc_authflavor_lock);
148 return RPC_AUTH_MAXFLAVOR;
149 }
150 spin_unlock(&rpc_authflavor_lock);
151
152 pseudoflavor = flavor;
153 if (ops->info2flavor != NULL)
154 pseudoflavor = ops->info2flavor(info);
155
156 module_put(ops->owner);
157 return pseudoflavor;
158}
159EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
160
161/**
Chuck Levera77c8062013-03-16 15:55:10 -0400162 * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
163 * @pseudoflavor: GSS pseudoflavor to match
164 * @info: rpcsec_gss_info structure to fill in
165 *
166 * Returns zero and fills in "info" if pseudoflavor matches a
167 * supported mechanism.
168 */
169int
170rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
171{
172 rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
173 const struct rpc_authops *ops;
174 int result;
175
Chuck Lever1c74a242013-03-22 12:53:08 -0400176 if (flavor >= RPC_AUTH_MAXFLAVOR)
177 return -EINVAL;
178
Chuck Levera77c8062013-03-16 15:55:10 -0400179 ops = auth_flavors[flavor];
180 if (ops == NULL)
181 request_module("rpc-auth-%u", flavor);
182 spin_lock(&rpc_authflavor_lock);
183 ops = auth_flavors[flavor];
184 if (ops == NULL || !try_module_get(ops->owner)) {
185 spin_unlock(&rpc_authflavor_lock);
186 return -ENOENT;
187 }
188 spin_unlock(&rpc_authflavor_lock);
189
190 result = -ENOENT;
191 if (ops->flavor2info != NULL)
192 result = ops->flavor2info(pseudoflavor, info);
193
194 module_put(ops->owner);
195 return result;
196}
197EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
198
199/**
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400200 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
201 * @array: array to fill in
202 * @size: size of "array"
203 *
204 * Returns the number of array items filled in, or a negative errno.
205 *
206 * The returned array is not sorted by any policy. Callers should not
207 * rely on the order of the items in the returned array.
208 */
209int
210rpcauth_list_flavors(rpc_authflavor_t *array, int size)
211{
212 rpc_authflavor_t flavor;
213 int result = 0;
214
215 spin_lock(&rpc_authflavor_lock);
216 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
217 const struct rpc_authops *ops = auth_flavors[flavor];
218 rpc_authflavor_t pseudos[4];
219 int i, len;
220
221 if (result >= size) {
222 result = -ENOMEM;
223 break;
224 }
225
226 if (ops == NULL)
227 continue;
228 if (ops->list_pseudoflavors == NULL) {
229 array[result++] = ops->au_flavor;
230 continue;
231 }
232 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
233 if (len < 0) {
234 result = len;
235 break;
236 }
237 for (i = 0; i < len; i++) {
238 if (result >= size) {
239 result = -ENOMEM;
240 break;
241 }
242 array[result++] = pseudos[i];
243 }
244 }
245 spin_unlock(&rpc_authflavor_lock);
246
247 dprintk("RPC: %s returns %d\n", __func__, result);
248 return result;
249}
250EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252struct rpc_auth *
Trond Myklebustc2190662013-08-26 19:23:04 -0400253rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400256 const struct rpc_authops *ops;
Trond Myklebustc2190662013-08-26 19:23:04 -0400257 u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500259 auth = ERR_PTR(-EINVAL);
260 if (flavor >= RPC_AUTH_MAXFLAVOR)
261 goto out;
262
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500263 if ((ops = auth_flavors[flavor]) == NULL)
264 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400265 spin_lock(&rpc_authflavor_lock);
266 ops = auth_flavors[flavor];
267 if (ops == NULL || !try_module_get(ops->owner)) {
268 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500269 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400270 }
271 spin_unlock(&rpc_authflavor_lock);
Trond Myklebustc2190662013-08-26 19:23:04 -0400272 auth = ops->create(args, clnt);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400273 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000274 if (IS_ERR(auth))
275 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400277 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500279
280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return auth;
282}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400283EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400286rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 if (!atomic_dec_and_test(&auth->au_count))
289 return;
290 auth->au_ops->destroy(auth);
291}
292
293static DEFINE_SPINLOCK(rpc_credcache_lock);
294
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400295static void
296rpcauth_unhash_cred_locked(struct rpc_cred *cred)
297{
298 hlist_del_rcu(&cred->cr_hash);
299 smp_mb__before_clear_bit();
300 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
301}
302
Trond Myklebustf0380f32009-12-03 08:10:17 -0500303static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400304rpcauth_unhash_cred(struct rpc_cred *cred)
305{
306 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500307 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400308
309 cache_lock = &cred->cr_auth->au_credcache->lock;
310 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500311 ret = atomic_read(&cred->cr_count) == 0;
312 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400313 rpcauth_unhash_cred_locked(cred);
314 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500315 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Initialize RPC credential cache
320 */
321int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400322rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400325 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800327 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400329 goto out_nocache;
330 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400331 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400332 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
333 if (!new->hashtable)
334 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400335 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 auth->au_credcache = new;
337 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400338out_nohashtbl:
339 kfree(new);
340out_nocache:
341 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400343EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345/*
Andy Adamson4de6caa2013-08-14 11:59:15 -0400346 * Setup a credential key lifetime timeout notification
347 */
348int
349rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
350{
351 if (!cred->cr_auth->au_ops->key_timeout)
352 return 0;
353 return cred->cr_auth->au_ops->key_timeout(auth, cred);
354}
355EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
356
357bool
358rpcauth_cred_key_to_expire(struct rpc_cred *cred)
359{
360 if (!cred->cr_ops->crkey_to_expire)
361 return false;
362 return cred->cr_ops->crkey_to_expire(cred);
363}
364EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
365
366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Destroy a list of credentials
368 */
369static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400370void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct rpc_cred *cred;
373
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400374 while (!list_empty(head)) {
375 cred = list_entry(head->next, struct rpc_cred, cr_lru);
376 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 put_rpccred(cred);
378 }
379}
380
381/*
382 * Clear the RPC credential cache, and delete those credentials
383 * that are not referenced.
384 */
385void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400386rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400388 LIST_HEAD(free);
389 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400391 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int i;
393
394 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400395 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400396 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400397 head = &cache->hashtable[i];
398 while (!hlist_empty(head)) {
399 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
400 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400401 if (!list_empty(&cred->cr_lru)) {
402 list_del(&cred->cr_lru);
403 number_cred_unused--;
404 }
405 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400406 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400409 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 spin_unlock(&rpc_credcache_lock);
411 rpcauth_destroy_credlist(&free);
412}
413
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400414/*
415 * Destroy the RPC credential cache
416 */
417void
418rpcauth_destroy_credcache(struct rpc_auth *auth)
419{
420 struct rpc_cred_cache *cache = auth->au_credcache;
421
422 if (cache) {
423 auth->au_credcache = NULL;
424 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400425 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400426 kfree(cache);
427 }
428}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400429EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400430
Trond Myklebustd2b83142008-04-14 18:13:37 -0400431
432#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Remove stale credentials. Avoid sleeping inside the loop.
436 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400437static int
438rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Trond Myklebust9499b432007-06-24 15:57:57 -0400440 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400441 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400442 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Trond Myklebusteac0d182008-10-28 15:21:41 -0400444 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
445
Trond Myklebust20673402010-05-13 12:51:06 -0400446 if (nr_to_scan-- == 0)
447 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400448 /*
449 * Enforce a 60 second garbage collection moratorium
450 * Note that the cred_unused list must be time-ordered.
451 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400452 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400453 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400454 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400455
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400456 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400457 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400458 if (atomic_read(&cred->cr_count) != 0)
459 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400460
Trond Myklebust9499b432007-06-24 15:57:57 -0400461 cache_lock = &cred->cr_auth->au_credcache->lock;
462 spin_lock(cache_lock);
463 if (atomic_read(&cred->cr_count) == 0) {
464 get_rpccred(cred);
465 list_add_tail(&cred->cr_lru, free);
466 rpcauth_unhash_cred_locked(cred);
467 }
468 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400470 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400471}
472
473/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400474 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400475 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400476static int
Ying Han1495f232011-05-24 17:12:27 -0700477rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400478{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400479 LIST_HEAD(free);
480 int res;
Ying Han1495f232011-05-24 17:12:27 -0700481 int nr_to_scan = sc->nr_to_scan;
482 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400483
Trond Myklebustd300a412010-05-13 12:51:03 -0400484 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
485 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400486 if (list_empty(&cred_unused))
487 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400488 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400489 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400490 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400491 rpcauth_destroy_credlist(&free);
492 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495/*
496 * Look up a process' credentials in the authentication cache
497 */
498struct rpc_cred *
499rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500500 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400502 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400504 struct rpc_cred *cred = NULL,
505 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400506 unsigned int nr;
507
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800508 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400510 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800511 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400512 if (!entry->cr_ops->crmatch(acred, entry, flags))
513 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400514 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400515 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400516 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400517 continue;
518 }
519 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400520 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400521 break;
522 }
523 rcu_read_unlock();
524
Trond Myklebust9499b432007-06-24 15:57:57 -0400525 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400526 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400527
528 new = auth->au_ops->crcreate(auth, acred, flags);
529 if (IS_ERR(new)) {
530 cred = new;
531 goto out;
532 }
533
Trond Myklebust9499b432007-06-24 15:57:57 -0400534 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800535 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400536 if (!entry->cr_ops->crmatch(acred, entry, flags))
537 continue;
538 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400541 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400542 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400543 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
544 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
545 } else
546 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400547 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400548found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800549 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
550 cred->cr_ops->cr_init != NULL &&
551 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500552 int res = cred->cr_ops->cr_init(auth, cred);
553 if (res < 0) {
554 put_rpccred(cred);
555 cred = ERR_PTR(res);
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400558 rpcauth_destroy_credlist(&free);
559out:
560 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400562EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500565rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
David Howells86a264a2008-11-14 10:39:18 +1100567 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100569 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Chuck Lever46121cf2007-01-31 12:14:08 -0500571 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100573
574 memset(&acred, 0, sizeof(acred));
575 acred.uid = cred->fsuid;
576 acred.gid = cred->fsgid;
577 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
578
Trond Myklebust8a317762006-02-01 12:18:36 -0500579 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 put_group_info(acred.group_info);
581 return ret;
582}
583
Trond Myklebust5fe47552007-06-23 19:55:31 -0400584void
585rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
586 struct rpc_auth *auth, const struct rpc_credops *ops)
587{
588 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400589 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400590 atomic_set(&cred->cr_count, 1);
591 cred->cr_auth = auth;
592 cred->cr_ops = ops;
593 cred->cr_expire = jiffies;
594#ifdef RPC_DEBUG
595 cred->cr_magic = RPCAUTH_CRED_MAGIC;
596#endif
597 cred->cr_uid = acred->uid;
598}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400599EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400600
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400601struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400602rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400603{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400604 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
605 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400606 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400607}
Trond Myklebust5c691042008-03-12 16:21:07 -0400608EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400609
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400610static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400611rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400613 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800615 .uid = GLOBAL_ROOT_UID,
616 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Chuck Lever46121cf2007-01-31 12:14:08 -0500619 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400620 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400621 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400622}
623
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400624static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400625rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400626{
627 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400628
629 dprintk("RPC: %5u looking up %s cred\n",
630 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400631 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Trond Myklebusta17c2152010-07-31 14:29:08 -0400634static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400635rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400637 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400638 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400639 int lookupflags = 0;
640
641 if (flags & RPC_TASK_ASYNC)
642 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400643 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400644 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400645 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400646 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400647 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400648 new = rpcauth_bind_new_cred(task, lookupflags);
649 if (IS_ERR(new))
650 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400651 if (req->rq_cred != NULL)
652 put_rpccred(req->rq_cred);
653 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657void
658put_rpccred(struct rpc_cred *cred)
659{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400660 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500661 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
662 if (atomic_dec_and_test(&cred->cr_count))
663 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500665 }
666
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400667 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
668 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400669 if (!list_empty(&cred->cr_lru)) {
670 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400671 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400672 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400673 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500674 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
675 cred->cr_expire = jiffies;
676 list_add_tail(&cred->cr_lru, &cred_unused);
677 number_cred_unused++;
678 goto out_nodestroy;
679 }
680 if (!rpcauth_unhash_cred(cred)) {
681 /* We were hashed and someone looked us up... */
682 goto out_nodestroy;
683 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400684 }
685 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500687 return;
688out_nodestroy:
689 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400691EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700693__be32 *
694rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400696 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Chuck Lever46121cf2007-01-31 12:14:08 -0500698 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400699 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return cred->cr_ops->crmarshal(task, p);
702}
703
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700704__be32 *
705rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400707 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Chuck Lever46121cf2007-01-31 12:14:08 -0500709 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400710 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return cred->cr_ops->crvalidate(task, p);
713}
714
Chuck Lever9f06c712010-12-14 14:59:18 +0000715static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
716 __be32 *data, void *obj)
717{
718 struct xdr_stream xdr;
719
720 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
721 encode(rqstp, &xdr, obj);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724int
Chuck Lever9f06c712010-12-14 14:59:18 +0000725rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700726 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400728 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Chuck Lever46121cf2007-01-31 12:14:08 -0500730 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 task->tk_pid, cred->cr_ops->cr_name, cred);
732 if (cred->cr_ops->crwrap_req)
733 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
734 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000735 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Chuck Leverbf269552010-12-14 14:59:29 +0000739static int
740rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
741 __be32 *data, void *obj)
742{
743 struct xdr_stream xdr;
744
745 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
746 return decode(rqstp, &xdr, obj);
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749int
Chuck Leverbf269552010-12-14 14:59:29 +0000750rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700751 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400753 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Chuck Lever46121cf2007-01-31 12:14:08 -0500755 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 task->tk_pid, cred->cr_ops->cr_name, cred);
757 if (cred->cr_ops->crunwrap_resp)
758 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
759 data, obj);
760 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000761 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764int
765rpcauth_refreshcred(struct rpc_task *task)
766{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400767 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int err;
769
Trond Myklebusta17c2152010-07-31 14:29:08 -0400770 cred = task->tk_rqstp->rq_cred;
771 if (cred == NULL) {
772 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
773 if (err < 0)
774 goto out;
775 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000776 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500777 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400778 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400781out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (err < 0)
783 task->tk_status = err;
784 return err;
785}
786
787void
788rpcauth_invalcred(struct rpc_task *task)
789{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400790 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400791
Chuck Lever46121cf2007-01-31 12:14:08 -0500792 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400793 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400794 if (cred)
795 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798int
799rpcauth_uptodatecred(struct rpc_task *task)
800{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400801 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400802
803 return cred == NULL ||
804 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400806
Rusty Russell8e1f9362007-07-17 04:03:17 -0700807static struct shrinker rpc_cred_shrinker = {
808 .shrink = rpcauth_cache_shrinker,
809 .seeks = DEFAULT_SEEKS,
810};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400811
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400812int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400813{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400814 int err;
815
816 err = rpc_init_authunix();
817 if (err < 0)
818 goto out1;
819 err = rpc_init_generic_auth();
820 if (err < 0)
821 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700822 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400823 return 0;
824out2:
825 rpc_destroy_authunix();
826out1:
827 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400828}
829
Stephen Rothwellc135e842010-09-29 14:16:57 +1000830void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400831{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400832 rpc_destroy_authunix();
833 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700834 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400835}