blob: ed2fdd210c0bac4f5569acbe5da9daa025012cdd [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 *
253rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
254{
255 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400256 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
258
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 auth = ops->create(clnt, pseudoflavor);
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/*
346 * Destroy a list of credentials
347 */
348static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400349void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct rpc_cred *cred;
352
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400353 while (!list_empty(head)) {
354 cred = list_entry(head->next, struct rpc_cred, cr_lru);
355 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 put_rpccred(cred);
357 }
358}
359
360/*
361 * Clear the RPC credential cache, and delete those credentials
362 * that are not referenced.
363 */
364void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400365rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400367 LIST_HEAD(free);
368 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400370 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int i;
372
373 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400374 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400375 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400376 head = &cache->hashtable[i];
377 while (!hlist_empty(head)) {
378 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
379 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400380 if (!list_empty(&cred->cr_lru)) {
381 list_del(&cred->cr_lru);
382 number_cred_unused--;
383 }
384 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400385 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400388 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_unlock(&rpc_credcache_lock);
390 rpcauth_destroy_credlist(&free);
391}
392
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400393/*
394 * Destroy the RPC credential cache
395 */
396void
397rpcauth_destroy_credcache(struct rpc_auth *auth)
398{
399 struct rpc_cred_cache *cache = auth->au_credcache;
400
401 if (cache) {
402 auth->au_credcache = NULL;
403 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400404 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400405 kfree(cache);
406 }
407}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400408EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400409
Trond Myklebustd2b83142008-04-14 18:13:37 -0400410
411#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * Remove stale credentials. Avoid sleeping inside the loop.
415 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400416static int
417rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Trond Myklebust9499b432007-06-24 15:57:57 -0400419 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400420 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400421 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Trond Myklebusteac0d182008-10-28 15:21:41 -0400423 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
424
Trond Myklebust20673402010-05-13 12:51:06 -0400425 if (nr_to_scan-- == 0)
426 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400427 /*
428 * Enforce a 60 second garbage collection moratorium
429 * Note that the cred_unused list must be time-ordered.
430 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400431 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400432 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400433 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400434
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400435 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400436 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400437 if (atomic_read(&cred->cr_count) != 0)
438 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400439
Trond Myklebust9499b432007-06-24 15:57:57 -0400440 cache_lock = &cred->cr_auth->au_credcache->lock;
441 spin_lock(cache_lock);
442 if (atomic_read(&cred->cr_count) == 0) {
443 get_rpccred(cred);
444 list_add_tail(&cred->cr_lru, free);
445 rpcauth_unhash_cred_locked(cred);
446 }
447 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400449 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400450}
451
452/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400453 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400454 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400455static int
Ying Han1495f232011-05-24 17:12:27 -0700456rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400457{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400458 LIST_HEAD(free);
459 int res;
Ying Han1495f232011-05-24 17:12:27 -0700460 int nr_to_scan = sc->nr_to_scan;
461 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400462
Trond Myklebustd300a412010-05-13 12:51:03 -0400463 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
464 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400465 if (list_empty(&cred_unused))
466 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400467 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400468 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400469 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400470 rpcauth_destroy_credlist(&free);
471 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/*
475 * Look up a process' credentials in the authentication cache
476 */
477struct rpc_cred *
478rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500479 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400481 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400483 struct rpc_cred *cred = NULL,
484 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400485 unsigned int nr;
486
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800487 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400489 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800490 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400491 if (!entry->cr_ops->crmatch(acred, entry, flags))
492 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400493 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400494 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400495 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400496 continue;
497 }
498 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400499 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400500 break;
501 }
502 rcu_read_unlock();
503
Trond Myklebust9499b432007-06-24 15:57:57 -0400504 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400505 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400506
507 new = auth->au_ops->crcreate(auth, acred, flags);
508 if (IS_ERR(new)) {
509 cred = new;
510 goto out;
511 }
512
Trond Myklebust9499b432007-06-24 15:57:57 -0400513 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800514 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400515 if (!entry->cr_ops->crmatch(acred, entry, flags))
516 continue;
517 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400520 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400521 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400522 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
523 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
524 } else
525 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400526 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400527found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800528 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
529 cred->cr_ops->cr_init != NULL &&
530 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500531 int res = cred->cr_ops->cr_init(auth, cred);
532 if (res < 0) {
533 put_rpccred(cred);
534 cred = ERR_PTR(res);
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400537 rpcauth_destroy_credlist(&free);
538out:
539 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400541EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500544rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
David Howells86a264a2008-11-14 10:39:18 +1100546 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100548 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Chuck Lever46121cf2007-01-31 12:14:08 -0500550 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100552
553 memset(&acred, 0, sizeof(acred));
554 acred.uid = cred->fsuid;
555 acred.gid = cred->fsgid;
556 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
557
Trond Myklebust8a317762006-02-01 12:18:36 -0500558 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 put_group_info(acred.group_info);
560 return ret;
561}
562
Trond Myklebust5fe47552007-06-23 19:55:31 -0400563void
564rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
565 struct rpc_auth *auth, const struct rpc_credops *ops)
566{
567 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400568 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400569 atomic_set(&cred->cr_count, 1);
570 cred->cr_auth = auth;
571 cred->cr_ops = ops;
572 cred->cr_expire = jiffies;
573#ifdef RPC_DEBUG
574 cred->cr_magic = RPCAUTH_CRED_MAGIC;
575#endif
576 cred->cr_uid = acred->uid;
577}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400578EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400579
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400580struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400581rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400582{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400583 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
584 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400585 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400586}
Trond Myklebust5c691042008-03-12 16:21:07 -0400587EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400588
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400589static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400590rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400592 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800594 .uid = GLOBAL_ROOT_UID,
595 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Chuck Lever46121cf2007-01-31 12:14:08 -0500598 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400599 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400600 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400601}
602
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400603static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400604rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400605{
606 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400607
608 dprintk("RPC: %5u looking up %s cred\n",
609 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400610 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Trond Myklebusta17c2152010-07-31 14:29:08 -0400613static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400614rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400616 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400617 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400618 int lookupflags = 0;
619
620 if (flags & RPC_TASK_ASYNC)
621 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400622 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400623 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400624 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400625 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400626 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400627 new = rpcauth_bind_new_cred(task, lookupflags);
628 if (IS_ERR(new))
629 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400630 if (req->rq_cred != NULL)
631 put_rpccred(req->rq_cred);
632 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636void
637put_rpccred(struct rpc_cred *cred)
638{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400639 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500640 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
641 if (atomic_dec_and_test(&cred->cr_count))
642 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500644 }
645
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400646 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
647 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400648 if (!list_empty(&cred->cr_lru)) {
649 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400650 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400651 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400652 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500653 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
654 cred->cr_expire = jiffies;
655 list_add_tail(&cred->cr_lru, &cred_unused);
656 number_cred_unused++;
657 goto out_nodestroy;
658 }
659 if (!rpcauth_unhash_cred(cred)) {
660 /* We were hashed and someone looked us up... */
661 goto out_nodestroy;
662 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400663 }
664 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500666 return;
667out_nodestroy:
668 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400670EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700672__be32 *
673rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400675 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Chuck Lever46121cf2007-01-31 12:14:08 -0500677 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400678 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return cred->cr_ops->crmarshal(task, p);
681}
682
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700683__be32 *
684rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400686 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Chuck Lever46121cf2007-01-31 12:14:08 -0500688 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400689 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return cred->cr_ops->crvalidate(task, p);
692}
693
Chuck Lever9f06c712010-12-14 14:59:18 +0000694static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
695 __be32 *data, void *obj)
696{
697 struct xdr_stream xdr;
698
699 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
700 encode(rqstp, &xdr, obj);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703int
Chuck Lever9f06c712010-12-14 14:59:18 +0000704rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700705 __be32 *data, void *obj)
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 using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 task->tk_pid, cred->cr_ops->cr_name, cred);
711 if (cred->cr_ops->crwrap_req)
712 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
713 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000714 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Chuck Leverbf269552010-12-14 14:59:29 +0000718static int
719rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
720 __be32 *data, void *obj)
721{
722 struct xdr_stream xdr;
723
724 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
725 return decode(rqstp, &xdr, obj);
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728int
Chuck Leverbf269552010-12-14 14:59:29 +0000729rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700730 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400732 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Chuck Lever46121cf2007-01-31 12:14:08 -0500734 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 task->tk_pid, cred->cr_ops->cr_name, cred);
736 if (cred->cr_ops->crunwrap_resp)
737 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
738 data, obj);
739 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000740 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743int
744rpcauth_refreshcred(struct rpc_task *task)
745{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400746 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int err;
748
Trond Myklebusta17c2152010-07-31 14:29:08 -0400749 cred = task->tk_rqstp->rq_cred;
750 if (cred == NULL) {
751 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
752 if (err < 0)
753 goto out;
754 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000755 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500756 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400757 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400760out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (err < 0)
762 task->tk_status = err;
763 return err;
764}
765
766void
767rpcauth_invalcred(struct rpc_task *task)
768{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400769 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400770
Chuck Lever46121cf2007-01-31 12:14:08 -0500771 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400772 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400773 if (cred)
774 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777int
778rpcauth_uptodatecred(struct rpc_task *task)
779{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400780 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400781
782 return cred == NULL ||
783 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400785
Rusty Russell8e1f9362007-07-17 04:03:17 -0700786static struct shrinker rpc_cred_shrinker = {
787 .shrink = rpcauth_cache_shrinker,
788 .seeks = DEFAULT_SEEKS,
789};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400790
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400791int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400792{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400793 int err;
794
795 err = rpc_init_authunix();
796 if (err < 0)
797 goto out1;
798 err = rpc_init_generic_auth();
799 if (err < 0)
800 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700801 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400802 return 0;
803out2:
804 rpc_destroy_authunix();
805out1:
806 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400807}
808
Stephen Rothwellc135e842010-09-29 14:16:57 +1000809void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400810{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400811 rpc_destroy_authunix();
812 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700813 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400814}