blob: d11eb055265cbd80dbb5f280d0f9d118c8fe64d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/idmap.c
3 *
4 * UID and GID to name mapping for clients.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Marius Aamodt Eriksen <marius@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/module.h>
Ingo Molnarc9d51282006-03-20 13:44:11 -050038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/slab.h>
42#include <linux/socket.h>
43#include <linux/in.h>
44#include <linux/sched.h>
45
46#include <linux/sunrpc/clnt.h>
47#include <linux/workqueue.h>
48#include <linux/sunrpc/rpc_pipe_fs.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/nfs_fs.h>
51
52#include <linux/nfs_idmap.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define IDMAP_HASH_SZ 128
56
Trond Myklebust58df0952006-01-03 09:55:57 +010057/* Default cache timeout is 10 minutes */
58unsigned int nfs_idmap_cache_timeout = 600 * HZ;
59
David Howells7d4e2742006-08-22 20:06:07 -040060static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
61{
62 char *endp;
63 int num = simple_strtol(val, &endp, 0);
64 int jif = num * HZ;
65 if (endp == val || *endp || num < 0 || jif < num)
66 return -EINVAL;
67 *((int *)kp->arg) = jif;
68 return 0;
69}
70
71module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
72 &nfs_idmap_cache_timeout, 0644);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct idmap_hashent {
Trond Myklebust58df0952006-01-03 09:55:57 +010075 unsigned long ih_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 __u32 ih_id;
77 int ih_namelen;
78 char ih_name[IDMAP_NAMESZ];
79};
80
81struct idmap_hashtable {
82 __u8 h_type;
83 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
84};
85
86struct idmap {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct dentry *idmap_dentry;
88 wait_queue_head_t idmap_wq;
89 struct idmap_msg idmap_im;
Ingo Molnarc9d51282006-03-20 13:44:11 -050090 struct mutex idmap_lock; /* Serializes upcalls */
91 struct mutex idmap_im_lock; /* Protects the hashtable */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct idmap_hashtable idmap_user_hash;
93 struct idmap_hashtable idmap_group_hash;
94};
95
96static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
97 char __user *, size_t);
98static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
99 size_t);
Adrian Bunk75c96f82005-05-05 16:16:09 -0700100static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static unsigned int fnvhash32(const void *, size_t);
103
104static struct rpc_pipe_ops idmap_upcall_ops = {
105 .upcall = idmap_pipe_upcall,
106 .downcall = idmap_pipe_downcall,
107 .destroy_msg = idmap_pipe_destroy_msg,
108};
109
David Howellsb7162792006-08-22 20:06:09 -0400110int
David Howellsadfa6f92006-08-22 20:06:08 -0400111nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct idmap *idmap;
David Howellsb7162792006-08-22 20:06:09 -0400114 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
David Howells54ceac42006-08-22 20:06:13 -0400116 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400117
Eric Sesterhennbd647542006-03-20 13:44:10 -0500118 if ((idmap = kzalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
David Howellsb7162792006-08-22 20:06:09 -0400119 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Trond Myklebust158998b2006-08-24 01:03:17 -0400121 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_dentry, "idmap",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 idmap, &idmap_upcall_ops, 0);
123 if (IS_ERR(idmap->idmap_dentry)) {
David Howellsb7162792006-08-22 20:06:09 -0400124 error = PTR_ERR(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400126 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
Ingo Molnarc9d51282006-03-20 13:44:11 -0500129 mutex_init(&idmap->idmap_lock);
130 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 init_waitqueue_head(&idmap->idmap_wq);
132 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
133 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
134
135 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139void
David Howellsadfa6f92006-08-22 20:06:08 -0400140nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct idmap *idmap = clp->cl_idmap;
143
144 if (!idmap)
145 return;
Trond Myklebust5d674762006-07-31 14:11:48 -0700146 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 clp->cl_idmap = NULL;
148 kfree(idmap);
149}
150
151/*
152 * Helper routines for manipulating the hashtable
153 */
154static inline struct idmap_hashent *
155idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
156{
157 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
158}
159
160static struct idmap_hashent *
161idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
162{
163 struct idmap_hashent *he = idmap_name_hash(h, name, len);
164
165 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
166 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100167 if (time_after(jiffies, he->ih_expires))
168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return he;
170}
171
172static inline struct idmap_hashent *
173idmap_id_hash(struct idmap_hashtable* h, __u32 id)
174{
175 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
176}
177
178static struct idmap_hashent *
179idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
180{
181 struct idmap_hashent *he = idmap_id_hash(h, id);
182 if (he->ih_id != id || he->ih_namelen == 0)
183 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100184 if (time_after(jiffies, he->ih_expires))
185 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return he;
187}
188
189/*
190 * Routines for allocating new entries in the hashtable.
191 * For now, we just have 1 entry per bucket, so it's all
192 * pretty trivial.
193 */
194static inline struct idmap_hashent *
195idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
196{
197 return idmap_name_hash(h, name, len);
198}
199
200static inline struct idmap_hashent *
201idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
202{
203 return idmap_id_hash(h, id);
204}
205
206static void
207idmap_update_entry(struct idmap_hashent *he, const char *name,
208 size_t namelen, __u32 id)
209{
210 he->ih_id = id;
211 memcpy(he->ih_name, name, namelen);
212 he->ih_name[namelen] = '\0';
213 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100214 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Name -> ID
219 */
220static int
221nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
222 const char *name, size_t namelen, __u32 *id)
223{
224 struct rpc_pipe_msg msg;
225 struct idmap_msg *im;
226 struct idmap_hashent *he;
227 DECLARE_WAITQUEUE(wq, current);
228 int ret = -EIO;
229
230 im = &idmap->idmap_im;
231
232 /*
233 * String sanity checks
234 * Note that the userland daemon expects NUL terminated strings
235 */
236 for (;;) {
237 if (namelen == 0)
238 return -EINVAL;
239 if (name[namelen-1] != '\0')
240 break;
241 namelen--;
242 }
243 if (namelen >= IDMAP_NAMESZ)
244 return -EINVAL;
245
Ingo Molnarc9d51282006-03-20 13:44:11 -0500246 mutex_lock(&idmap->idmap_lock);
247 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 he = idmap_lookup_name(h, name, namelen);
250 if (he != NULL) {
251 *id = he->ih_id;
252 ret = 0;
253 goto out;
254 }
255
256 memset(im, 0, sizeof(*im));
257 memcpy(im->im_name, name, namelen);
258
259 im->im_type = h->h_type;
260 im->im_conv = IDMAP_CONV_NAMETOID;
261
262 memset(&msg, 0, sizeof(msg));
263 msg.data = im;
264 msg.len = sizeof(*im);
265
266 add_wait_queue(&idmap->idmap_wq, &wq);
267 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
268 remove_wait_queue(&idmap->idmap_wq, &wq);
269 goto out;
270 }
271
272 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500273 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700275 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500277 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (im->im_status & IDMAP_STATUS_SUCCESS) {
280 *id = im->im_id;
281 ret = 0;
282 }
283
284 out:
285 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500286 mutex_unlock(&idmap->idmap_im_lock);
287 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return (ret);
289}
290
291/*
292 * ID -> Name
293 */
294static int
295nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
296 __u32 id, char *name)
297{
298 struct rpc_pipe_msg msg;
299 struct idmap_msg *im;
300 struct idmap_hashent *he;
301 DECLARE_WAITQUEUE(wq, current);
302 int ret = -EIO;
303 unsigned int len;
304
305 im = &idmap->idmap_im;
306
Ingo Molnarc9d51282006-03-20 13:44:11 -0500307 mutex_lock(&idmap->idmap_lock);
308 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 he = idmap_lookup_id(h, id);
311 if (he != 0) {
312 memcpy(name, he->ih_name, he->ih_namelen);
313 ret = he->ih_namelen;
314 goto out;
315 }
316
317 memset(im, 0, sizeof(*im));
318 im->im_type = h->h_type;
319 im->im_conv = IDMAP_CONV_IDTONAME;
320 im->im_id = id;
321
322 memset(&msg, 0, sizeof(msg));
323 msg.data = im;
324 msg.len = sizeof(*im);
325
326 add_wait_queue(&idmap->idmap_wq, &wq);
327
328 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
329 remove_wait_queue(&idmap->idmap_wq, &wq);
330 goto out;
331 }
332
333 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500334 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700336 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500338 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (im->im_status & IDMAP_STATUS_SUCCESS) {
341 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
342 goto out;
343 memcpy(name, im->im_name, len);
344 ret = len;
345 }
346
347 out:
348 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500349 mutex_unlock(&idmap->idmap_im_lock);
350 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ret;
352}
353
354/* RPC pipefs upcall/downcall routines */
355static ssize_t
356idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
357 char __user *dst, size_t buflen)
358{
359 char *data = (char *)msg->data + msg->copied;
360 ssize_t mlen = msg->len - msg->copied;
361 ssize_t left;
362
363 if (mlen > buflen)
364 mlen = buflen;
365
366 left = copy_to_user(dst, data, mlen);
367 if (left < 0) {
368 msg->errno = left;
369 return left;
370 }
371 mlen -= left;
372 msg->copied += mlen;
373 msg->errno = 0;
374 return mlen;
375}
376
377static ssize_t
378idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
379{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800380 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct idmap *idmap = (struct idmap *)rpci->private;
382 struct idmap_msg im_in, *im = &idmap->idmap_im;
383 struct idmap_hashtable *h;
384 struct idmap_hashent *he = NULL;
385 int namelen_in;
386 int ret;
387
388 if (mlen != sizeof(im_in))
389 return (-ENOSPC);
390
391 if (copy_from_user(&im_in, src, mlen) != 0)
392 return (-EFAULT);
393
Ingo Molnarc9d51282006-03-20 13:44:11 -0500394 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 ret = mlen;
397 im->im_status = im_in.im_status;
398 /* If we got an error, terminate now, and wake up pending upcalls */
399 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
400 wake_up(&idmap->idmap_wq);
401 goto out;
402 }
403
404 /* Sanity checking of strings */
405 ret = -EINVAL;
406 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
407 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
408 goto out;
409
410 switch (im_in.im_type) {
411 case IDMAP_TYPE_USER:
412 h = &idmap->idmap_user_hash;
413 break;
414 case IDMAP_TYPE_GROUP:
415 h = &idmap->idmap_group_hash;
416 break;
417 default:
418 goto out;
419 }
420
421 switch (im_in.im_conv) {
422 case IDMAP_CONV_IDTONAME:
423 /* Did we match the current upcall? */
424 if (im->im_conv == IDMAP_CONV_IDTONAME
425 && im->im_type == im_in.im_type
426 && im->im_id == im_in.im_id) {
427 /* Yes: copy string, including the terminating '\0' */
428 memcpy(im->im_name, im_in.im_name, namelen_in);
429 im->im_name[namelen_in] = '\0';
430 wake_up(&idmap->idmap_wq);
431 }
432 he = idmap_alloc_id(h, im_in.im_id);
433 break;
434 case IDMAP_CONV_NAMETOID:
435 /* Did we match the current upcall? */
436 if (im->im_conv == IDMAP_CONV_NAMETOID
437 && im->im_type == im_in.im_type
438 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
439 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
440 im->im_id = im_in.im_id;
441 wake_up(&idmap->idmap_wq);
442 }
443 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
444 break;
445 default:
446 goto out;
447 }
448
449 /* If the entry is valid, also copy it to the cache */
450 if (he != NULL)
451 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
452 ret = mlen;
453out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500454 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return ret;
456}
457
Adrian Bunk75c96f82005-05-05 16:16:09 -0700458static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
460{
461 struct idmap_msg *im = msg->data;
462 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
463
464 if (msg->errno >= 0)
465 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500466 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
468 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500469 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472/*
473 * Fowler/Noll/Vo hash
474 * http://www.isthe.com/chongo/tech/comp/fnv/
475 */
476
477#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
478#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
479
480static unsigned int fnvhash32(const void *buf, size_t buflen)
481{
482 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
483 unsigned int hash = FNV_1_32;
484
485 for (p = buf; p < end; p++) {
486 hash *= FNV_P_32;
487 hash ^= (unsigned int)*p;
488 }
489
490 return (hash);
491}
492
David Howellsadfa6f92006-08-22 20:06:08 -0400493int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct idmap *idmap = clp->cl_idmap;
496
497 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
498}
499
David Howellsadfa6f92006-08-22 20:06:08 -0400500int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct idmap *idmap = clp->cl_idmap;
503
504 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
505}
506
David Howellsadfa6f92006-08-22 20:06:08 -0400507int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct idmap *idmap = clp->cl_idmap;
510
511 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
512}
David Howellsadfa6f92006-08-22 20:06:08 -0400513int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct idmap *idmap = clp->cl_idmap;
516
517 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
518}
519