blob: 231c20ffc0ff7ad74eb45657a33be1fc91e27547 [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 {
87 char idmap_path[48];
88 struct dentry *idmap_dentry;
89 wait_queue_head_t idmap_wq;
90 struct idmap_msg idmap_im;
Ingo Molnarc9d51282006-03-20 13:44:11 -050091 struct mutex idmap_lock; /* Serializes upcalls */
92 struct mutex idmap_im_lock; /* Protects the hashtable */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct idmap_hashtable idmap_user_hash;
94 struct idmap_hashtable idmap_group_hash;
95};
96
97static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
98 char __user *, size_t);
99static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
100 size_t);
Adrian Bunk75c96f82005-05-05 16:16:09 -0700101static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static unsigned int fnvhash32(const void *, size_t);
104
105static struct rpc_pipe_ops idmap_upcall_ops = {
106 .upcall = idmap_pipe_upcall,
107 .downcall = idmap_pipe_downcall,
108 .destroy_msg = idmap_pipe_destroy_msg,
109};
110
David Howellsb7162792006-08-22 20:06:09 -0400111int
David Howellsadfa6f92006-08-22 20:06:08 -0400112nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct idmap *idmap;
David Howellsb7162792006-08-22 20:06:09 -0400115 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (clp->cl_idmap != NULL)
David Howellsb7162792006-08-22 20:06:09 -0400118 return 0;
119
Eric Sesterhennbd647542006-03-20 13:44:10 -0500120 if ((idmap = kzalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
David Howellsb7162792006-08-22 20:06:09 -0400121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 snprintf(idmap->idmap_path, sizeof(idmap->idmap_path),
124 "%s/idmap", clp->cl_rpcclient->cl_pathname);
125
126 idmap->idmap_dentry = rpc_mkpipe(idmap->idmap_path,
127 idmap, &idmap_upcall_ops, 0);
128 if (IS_ERR(idmap->idmap_dentry)) {
David Howellsb7162792006-08-22 20:06:09 -0400129 error = PTR_ERR(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400131 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Ingo Molnarc9d51282006-03-20 13:44:11 -0500134 mutex_init(&idmap->idmap_lock);
135 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 init_waitqueue_head(&idmap->idmap_wq);
137 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
138 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
139
140 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144void
David Howellsadfa6f92006-08-22 20:06:08 -0400145nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct idmap *idmap = clp->cl_idmap;
148
149 if (!idmap)
150 return;
Trond Myklebust5d674762006-07-31 14:11:48 -0700151 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 clp->cl_idmap = NULL;
153 kfree(idmap);
154}
155
156/*
157 * Helper routines for manipulating the hashtable
158 */
159static inline struct idmap_hashent *
160idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
161{
162 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
163}
164
165static struct idmap_hashent *
166idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
167{
168 struct idmap_hashent *he = idmap_name_hash(h, name, len);
169
170 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
171 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100172 if (time_after(jiffies, he->ih_expires))
173 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return he;
175}
176
177static inline struct idmap_hashent *
178idmap_id_hash(struct idmap_hashtable* h, __u32 id)
179{
180 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
181}
182
183static struct idmap_hashent *
184idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
185{
186 struct idmap_hashent *he = idmap_id_hash(h, id);
187 if (he->ih_id != id || he->ih_namelen == 0)
188 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100189 if (time_after(jiffies, he->ih_expires))
190 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return he;
192}
193
194/*
195 * Routines for allocating new entries in the hashtable.
196 * For now, we just have 1 entry per bucket, so it's all
197 * pretty trivial.
198 */
199static inline struct idmap_hashent *
200idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
201{
202 return idmap_name_hash(h, name, len);
203}
204
205static inline struct idmap_hashent *
206idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
207{
208 return idmap_id_hash(h, id);
209}
210
211static void
212idmap_update_entry(struct idmap_hashent *he, const char *name,
213 size_t namelen, __u32 id)
214{
215 he->ih_id = id;
216 memcpy(he->ih_name, name, namelen);
217 he->ih_name[namelen] = '\0';
218 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100219 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/*
223 * Name -> ID
224 */
225static int
226nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
227 const char *name, size_t namelen, __u32 *id)
228{
229 struct rpc_pipe_msg msg;
230 struct idmap_msg *im;
231 struct idmap_hashent *he;
232 DECLARE_WAITQUEUE(wq, current);
233 int ret = -EIO;
234
235 im = &idmap->idmap_im;
236
237 /*
238 * String sanity checks
239 * Note that the userland daemon expects NUL terminated strings
240 */
241 for (;;) {
242 if (namelen == 0)
243 return -EINVAL;
244 if (name[namelen-1] != '\0')
245 break;
246 namelen--;
247 }
248 if (namelen >= IDMAP_NAMESZ)
249 return -EINVAL;
250
Ingo Molnarc9d51282006-03-20 13:44:11 -0500251 mutex_lock(&idmap->idmap_lock);
252 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 he = idmap_lookup_name(h, name, namelen);
255 if (he != NULL) {
256 *id = he->ih_id;
257 ret = 0;
258 goto out;
259 }
260
261 memset(im, 0, sizeof(*im));
262 memcpy(im->im_name, name, namelen);
263
264 im->im_type = h->h_type;
265 im->im_conv = IDMAP_CONV_NAMETOID;
266
267 memset(&msg, 0, sizeof(msg));
268 msg.data = im;
269 msg.len = sizeof(*im);
270
271 add_wait_queue(&idmap->idmap_wq, &wq);
272 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
273 remove_wait_queue(&idmap->idmap_wq, &wq);
274 goto out;
275 }
276
277 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500278 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 schedule();
280 current->state = TASK_RUNNING;
281 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500282 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (im->im_status & IDMAP_STATUS_SUCCESS) {
285 *id = im->im_id;
286 ret = 0;
287 }
288
289 out:
290 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500291 mutex_unlock(&idmap->idmap_im_lock);
292 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return (ret);
294}
295
296/*
297 * ID -> Name
298 */
299static int
300nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
301 __u32 id, char *name)
302{
303 struct rpc_pipe_msg msg;
304 struct idmap_msg *im;
305 struct idmap_hashent *he;
306 DECLARE_WAITQUEUE(wq, current);
307 int ret = -EIO;
308 unsigned int len;
309
310 im = &idmap->idmap_im;
311
Ingo Molnarc9d51282006-03-20 13:44:11 -0500312 mutex_lock(&idmap->idmap_lock);
313 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 he = idmap_lookup_id(h, id);
316 if (he != 0) {
317 memcpy(name, he->ih_name, he->ih_namelen);
318 ret = he->ih_namelen;
319 goto out;
320 }
321
322 memset(im, 0, sizeof(*im));
323 im->im_type = h->h_type;
324 im->im_conv = IDMAP_CONV_IDTONAME;
325 im->im_id = id;
326
327 memset(&msg, 0, sizeof(msg));
328 msg.data = im;
329 msg.len = sizeof(*im);
330
331 add_wait_queue(&idmap->idmap_wq, &wq);
332
333 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
334 remove_wait_queue(&idmap->idmap_wq, &wq);
335 goto out;
336 }
337
338 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500339 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 schedule();
341 current->state = TASK_RUNNING;
342 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500343 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (im->im_status & IDMAP_STATUS_SUCCESS) {
346 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
347 goto out;
348 memcpy(name, im->im_name, len);
349 ret = len;
350 }
351
352 out:
353 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500354 mutex_unlock(&idmap->idmap_im_lock);
355 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return ret;
357}
358
359/* RPC pipefs upcall/downcall routines */
360static ssize_t
361idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
362 char __user *dst, size_t buflen)
363{
364 char *data = (char *)msg->data + msg->copied;
365 ssize_t mlen = msg->len - msg->copied;
366 ssize_t left;
367
368 if (mlen > buflen)
369 mlen = buflen;
370
371 left = copy_to_user(dst, data, mlen);
372 if (left < 0) {
373 msg->errno = left;
374 return left;
375 }
376 mlen -= left;
377 msg->copied += mlen;
378 msg->errno = 0;
379 return mlen;
380}
381
382static ssize_t
383idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
384{
385 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
386 struct idmap *idmap = (struct idmap *)rpci->private;
387 struct idmap_msg im_in, *im = &idmap->idmap_im;
388 struct idmap_hashtable *h;
389 struct idmap_hashent *he = NULL;
390 int namelen_in;
391 int ret;
392
393 if (mlen != sizeof(im_in))
394 return (-ENOSPC);
395
396 if (copy_from_user(&im_in, src, mlen) != 0)
397 return (-EFAULT);
398
Ingo Molnarc9d51282006-03-20 13:44:11 -0500399 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 ret = mlen;
402 im->im_status = im_in.im_status;
403 /* If we got an error, terminate now, and wake up pending upcalls */
404 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
405 wake_up(&idmap->idmap_wq);
406 goto out;
407 }
408
409 /* Sanity checking of strings */
410 ret = -EINVAL;
411 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
412 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
413 goto out;
414
415 switch (im_in.im_type) {
416 case IDMAP_TYPE_USER:
417 h = &idmap->idmap_user_hash;
418 break;
419 case IDMAP_TYPE_GROUP:
420 h = &idmap->idmap_group_hash;
421 break;
422 default:
423 goto out;
424 }
425
426 switch (im_in.im_conv) {
427 case IDMAP_CONV_IDTONAME:
428 /* Did we match the current upcall? */
429 if (im->im_conv == IDMAP_CONV_IDTONAME
430 && im->im_type == im_in.im_type
431 && im->im_id == im_in.im_id) {
432 /* Yes: copy string, including the terminating '\0' */
433 memcpy(im->im_name, im_in.im_name, namelen_in);
434 im->im_name[namelen_in] = '\0';
435 wake_up(&idmap->idmap_wq);
436 }
437 he = idmap_alloc_id(h, im_in.im_id);
438 break;
439 case IDMAP_CONV_NAMETOID:
440 /* Did we match the current upcall? */
441 if (im->im_conv == IDMAP_CONV_NAMETOID
442 && im->im_type == im_in.im_type
443 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
444 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
445 im->im_id = im_in.im_id;
446 wake_up(&idmap->idmap_wq);
447 }
448 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
449 break;
450 default:
451 goto out;
452 }
453
454 /* If the entry is valid, also copy it to the cache */
455 if (he != NULL)
456 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
457 ret = mlen;
458out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500459 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return ret;
461}
462
Adrian Bunk75c96f82005-05-05 16:16:09 -0700463static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
465{
466 struct idmap_msg *im = msg->data;
467 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
468
469 if (msg->errno >= 0)
470 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500471 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
473 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500474 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * Fowler/Noll/Vo hash
479 * http://www.isthe.com/chongo/tech/comp/fnv/
480 */
481
482#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
483#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
484
485static unsigned int fnvhash32(const void *buf, size_t buflen)
486{
487 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
488 unsigned int hash = FNV_1_32;
489
490 for (p = buf; p < end; p++) {
491 hash *= FNV_P_32;
492 hash ^= (unsigned int)*p;
493 }
494
495 return (hash);
496}
497
David Howellsadfa6f92006-08-22 20:06:08 -0400498int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct idmap *idmap = clp->cl_idmap;
501
502 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
503}
504
David Howellsadfa6f92006-08-22 20:06:08 -0400505int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct idmap *idmap = clp->cl_idmap;
508
509 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
510}
511
David Howellsadfa6f92006-08-22 20:06:08 -0400512int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct idmap *idmap = clp->cl_idmap;
515
516 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
517}
David Howellsadfa6f92006-08-22 20:06:08 -0400518int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct idmap *idmap = clp->cl_idmap;
521
522 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
523}
524