blob: f773667174200cbabb92c7fc02adfb5d57e3e61c [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);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100299 smp_mb__before_atomic();
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400300 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 */
Dave Chinner70534a72013-08-28 10:18:14 +1000437static long
Trond Myklebustf5c21872007-06-25 17:11:20 -0400438rpcauth_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;
Dave Chinner70534a72013-08-28 10:18:14 +1000443 long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Trond Myklebusteac0d182008-10-28 15:21:41 -0400445 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
446
Trond Myklebust20673402010-05-13 12:51:06 -0400447 if (nr_to_scan-- == 0)
448 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400449 /*
450 * Enforce a 60 second garbage collection moratorium
451 * Note that the cred_unused list must be time-ordered.
452 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400453 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400454 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Dave Chinner70534a72013-08-28 10:18:14 +1000455 break;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400456
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400457 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400458 number_cred_unused--;
Dave Chinner70534a72013-08-28 10:18:14 +1000459 freed++;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400460 if (atomic_read(&cred->cr_count) != 0)
461 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400462
Trond Myklebust9499b432007-06-24 15:57:57 -0400463 cache_lock = &cred->cr_auth->au_credcache->lock;
464 spin_lock(cache_lock);
465 if (atomic_read(&cred->cr_count) == 0) {
466 get_rpccred(cred);
467 list_add_tail(&cred->cr_lru, free);
468 rpcauth_unhash_cred_locked(cred);
469 }
470 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Dave Chinner70534a72013-08-28 10:18:14 +1000472 return freed;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400473}
474
475/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400476 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400477 */
Dave Chinner70534a72013-08-28 10:18:14 +1000478static unsigned long
479rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
480
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400481{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400482 LIST_HEAD(free);
Dave Chinner70534a72013-08-28 10:18:14 +1000483 unsigned long freed;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400484
Dave Chinner70534a72013-08-28 10:18:14 +1000485 if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
486 return SHRINK_STOP;
487
488 /* nothing left, don't come back */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400489 if (list_empty(&cred_unused))
Dave Chinner70534a72013-08-28 10:18:14 +1000490 return SHRINK_STOP;
491
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400492 spin_lock(&rpc_credcache_lock);
Dave Chinner70534a72013-08-28 10:18:14 +1000493 freed = rpcauth_prune_expired(&free, sc->nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400494 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400495 rpcauth_destroy_credlist(&free);
Dave Chinner70534a72013-08-28 10:18:14 +1000496
497 return freed;
498}
499
500static unsigned long
501rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
502
503{
504 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * Look up a process' credentials in the authentication cache
509 */
510struct rpc_cred *
511rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500512 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400514 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400516 struct rpc_cred *cred = NULL,
517 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400518 unsigned int nr;
519
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800520 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400522 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800523 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400524 if (!entry->cr_ops->crmatch(acred, entry, flags))
525 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400526 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400527 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400528 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400529 continue;
530 }
531 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400532 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400533 break;
534 }
535 rcu_read_unlock();
536
Trond Myklebust9499b432007-06-24 15:57:57 -0400537 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400538 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400539
540 new = auth->au_ops->crcreate(auth, acred, flags);
541 if (IS_ERR(new)) {
542 cred = new;
543 goto out;
544 }
545
Trond Myklebust9499b432007-06-24 15:57:57 -0400546 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800547 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400548 if (!entry->cr_ops->crmatch(acred, entry, flags))
549 continue;
550 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400553 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400554 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400555 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
556 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
557 } else
558 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400559 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400560found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800561 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
562 cred->cr_ops->cr_init != NULL &&
563 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500564 int res = cred->cr_ops->cr_init(auth, cred);
565 if (res < 0) {
566 put_rpccred(cred);
567 cred = ERR_PTR(res);
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400570 rpcauth_destroy_credlist(&free);
571out:
572 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400574EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500577rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
David Howells86a264a2008-11-14 10:39:18 +1100579 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100581 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Chuck Lever46121cf2007-01-31 12:14:08 -0500583 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100585
586 memset(&acred, 0, sizeof(acred));
587 acred.uid = cred->fsuid;
588 acred.gid = cred->fsgid;
589 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
590
Trond Myklebust8a317762006-02-01 12:18:36 -0500591 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 put_group_info(acred.group_info);
593 return ret;
594}
Andy Adamson66b06862014-06-12 15:02:32 -0400595EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Trond Myklebust5fe47552007-06-23 19:55:31 -0400597void
598rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
599 struct rpc_auth *auth, const struct rpc_credops *ops)
600{
601 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400602 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400603 atomic_set(&cred->cr_count, 1);
604 cred->cr_auth = auth;
605 cred->cr_ops = ops;
606 cred->cr_expire = jiffies;
607#ifdef RPC_DEBUG
608 cred->cr_magic = RPCAUTH_CRED_MAGIC;
609#endif
610 cred->cr_uid = acred->uid;
611}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400612EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400613
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400614struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400615rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400616{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400617 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
618 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400619 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400620}
Trond Myklebust5c691042008-03-12 16:21:07 -0400621EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400622
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400623static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400624rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400626 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800628 .uid = GLOBAL_ROOT_UID,
629 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Chuck Lever46121cf2007-01-31 12:14:08 -0500632 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400633 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400634 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400635}
636
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400637static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400638rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400639{
640 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400641
642 dprintk("RPC: %5u looking up %s cred\n",
643 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400644 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Trond Myklebusta17c2152010-07-31 14:29:08 -0400647static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400648rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400650 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400651 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400652 int lookupflags = 0;
653
654 if (flags & RPC_TASK_ASYNC)
655 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400656 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400657 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400658 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400659 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400660 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400661 new = rpcauth_bind_new_cred(task, lookupflags);
662 if (IS_ERR(new))
663 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400664 if (req->rq_cred != NULL)
665 put_rpccred(req->rq_cred);
666 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670void
671put_rpccred(struct rpc_cred *cred)
672{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400673 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500674 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
675 if (atomic_dec_and_test(&cred->cr_count))
676 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500678 }
679
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400680 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
681 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400682 if (!list_empty(&cred->cr_lru)) {
683 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400684 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400685 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400686 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500687 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
688 cred->cr_expire = jiffies;
689 list_add_tail(&cred->cr_lru, &cred_unused);
690 number_cred_unused++;
691 goto out_nodestroy;
692 }
693 if (!rpcauth_unhash_cred(cred)) {
694 /* We were hashed and someone looked us up... */
695 goto out_nodestroy;
696 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400697 }
698 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500700 return;
701out_nodestroy:
702 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400704EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700706__be32 *
707rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400709 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Chuck Lever46121cf2007-01-31 12:14:08 -0500711 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400712 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return cred->cr_ops->crmarshal(task, p);
715}
716
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700717__be32 *
718rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400720 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Chuck Lever46121cf2007-01-31 12:14:08 -0500722 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400723 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return cred->cr_ops->crvalidate(task, p);
726}
727
Chuck Lever9f06c712010-12-14 14:59:18 +0000728static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
729 __be32 *data, void *obj)
730{
731 struct xdr_stream xdr;
732
733 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
734 encode(rqstp, &xdr, obj);
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737int
Chuck Lever9f06c712010-12-14 14:59:18 +0000738rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700739 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400741 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Chuck Lever46121cf2007-01-31 12:14:08 -0500743 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 task->tk_pid, cred->cr_ops->cr_name, cred);
745 if (cred->cr_ops->crwrap_req)
746 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
747 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000748 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Chuck Leverbf269552010-12-14 14:59:29 +0000752static int
753rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
754 __be32 *data, void *obj)
755{
756 struct xdr_stream xdr;
757
758 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
759 return decode(rqstp, &xdr, obj);
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762int
Chuck Leverbf269552010-12-14 14:59:29 +0000763rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700764 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400766 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Chuck Lever46121cf2007-01-31 12:14:08 -0500768 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 task->tk_pid, cred->cr_ops->cr_name, cred);
770 if (cred->cr_ops->crunwrap_resp)
771 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
772 data, obj);
773 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000774 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777int
778rpcauth_refreshcred(struct rpc_task *task)
779{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400780 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int err;
782
Trond Myklebusta17c2152010-07-31 14:29:08 -0400783 cred = task->tk_rqstp->rq_cred;
784 if (cred == NULL) {
785 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
786 if (err < 0)
787 goto out;
788 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000789 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500790 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400791 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400794out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (err < 0)
796 task->tk_status = err;
797 return err;
798}
799
800void
801rpcauth_invalcred(struct rpc_task *task)
802{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400803 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400804
Chuck Lever46121cf2007-01-31 12:14:08 -0500805 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400806 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400807 if (cred)
808 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811int
812rpcauth_uptodatecred(struct rpc_task *task)
813{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400814 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400815
816 return cred == NULL ||
817 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400819
Rusty Russell8e1f9362007-07-17 04:03:17 -0700820static struct shrinker rpc_cred_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +1000821 .count_objects = rpcauth_cache_shrink_count,
822 .scan_objects = rpcauth_cache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700823 .seeks = DEFAULT_SEEKS,
824};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400825
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400826int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400827{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400828 int err;
829
830 err = rpc_init_authunix();
831 if (err < 0)
832 goto out1;
833 err = rpc_init_generic_auth();
834 if (err < 0)
835 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700836 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400837 return 0;
838out2:
839 rpc_destroy_authunix();
840out1:
841 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400842}
843
Stephen Rothwellc135e842010-09-29 14:16:57 +1000844void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400845{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400846 rpc_destroy_authunix();
847 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700848 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400849}