blob: f96dfac7dc9a6ade7807a5f1820ebce75d21e1c6 [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
David Howells54ceac42006-08-22 20:06:13 -0400117 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400118
Eric Sesterhennbd647542006-03-20 13:44:10 -0500119 if ((idmap = kzalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
David Howellsb7162792006-08-22 20:06:09 -0400120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 snprintf(idmap->idmap_path, sizeof(idmap->idmap_path),
123 "%s/idmap", clp->cl_rpcclient->cl_pathname);
124
125 idmap->idmap_dentry = rpc_mkpipe(idmap->idmap_path,
126 idmap, &idmap_upcall_ops, 0);
127 if (IS_ERR(idmap->idmap_dentry)) {
David Howellsb7162792006-08-22 20:06:09 -0400128 error = PTR_ERR(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400130 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
Ingo Molnarc9d51282006-03-20 13:44:11 -0500133 mutex_init(&idmap->idmap_lock);
134 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 init_waitqueue_head(&idmap->idmap_wq);
136 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
137 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
138
139 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143void
David Howellsadfa6f92006-08-22 20:06:08 -0400144nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct idmap *idmap = clp->cl_idmap;
147
148 if (!idmap)
149 return;
Trond Myklebust5d674762006-07-31 14:11:48 -0700150 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 clp->cl_idmap = NULL;
152 kfree(idmap);
153}
154
155/*
156 * Helper routines for manipulating the hashtable
157 */
158static inline struct idmap_hashent *
159idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
160{
161 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
162}
163
164static struct idmap_hashent *
165idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
166{
167 struct idmap_hashent *he = idmap_name_hash(h, name, len);
168
169 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
170 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100171 if (time_after(jiffies, he->ih_expires))
172 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return he;
174}
175
176static inline struct idmap_hashent *
177idmap_id_hash(struct idmap_hashtable* h, __u32 id)
178{
179 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
180}
181
182static struct idmap_hashent *
183idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
184{
185 struct idmap_hashent *he = idmap_id_hash(h, id);
186 if (he->ih_id != id || he->ih_namelen == 0)
187 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100188 if (time_after(jiffies, he->ih_expires))
189 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return he;
191}
192
193/*
194 * Routines for allocating new entries in the hashtable.
195 * For now, we just have 1 entry per bucket, so it's all
196 * pretty trivial.
197 */
198static inline struct idmap_hashent *
199idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
200{
201 return idmap_name_hash(h, name, len);
202}
203
204static inline struct idmap_hashent *
205idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
206{
207 return idmap_id_hash(h, id);
208}
209
210static void
211idmap_update_entry(struct idmap_hashent *he, const char *name,
212 size_t namelen, __u32 id)
213{
214 he->ih_id = id;
215 memcpy(he->ih_name, name, namelen);
216 he->ih_name[namelen] = '\0';
217 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100218 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
222 * Name -> ID
223 */
224static int
225nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
226 const char *name, size_t namelen, __u32 *id)
227{
228 struct rpc_pipe_msg msg;
229 struct idmap_msg *im;
230 struct idmap_hashent *he;
231 DECLARE_WAITQUEUE(wq, current);
232 int ret = -EIO;
233
234 im = &idmap->idmap_im;
235
236 /*
237 * String sanity checks
238 * Note that the userland daemon expects NUL terminated strings
239 */
240 for (;;) {
241 if (namelen == 0)
242 return -EINVAL;
243 if (name[namelen-1] != '\0')
244 break;
245 namelen--;
246 }
247 if (namelen >= IDMAP_NAMESZ)
248 return -EINVAL;
249
Ingo Molnarc9d51282006-03-20 13:44:11 -0500250 mutex_lock(&idmap->idmap_lock);
251 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 he = idmap_lookup_name(h, name, namelen);
254 if (he != NULL) {
255 *id = he->ih_id;
256 ret = 0;
257 goto out;
258 }
259
260 memset(im, 0, sizeof(*im));
261 memcpy(im->im_name, name, namelen);
262
263 im->im_type = h->h_type;
264 im->im_conv = IDMAP_CONV_NAMETOID;
265
266 memset(&msg, 0, sizeof(msg));
267 msg.data = im;
268 msg.len = sizeof(*im);
269
270 add_wait_queue(&idmap->idmap_wq, &wq);
271 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
272 remove_wait_queue(&idmap->idmap_wq, &wq);
273 goto out;
274 }
275
276 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500277 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 schedule();
279 current->state = TASK_RUNNING;
280 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500281 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (im->im_status & IDMAP_STATUS_SUCCESS) {
284 *id = im->im_id;
285 ret = 0;
286 }
287
288 out:
289 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500290 mutex_unlock(&idmap->idmap_im_lock);
291 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return (ret);
293}
294
295/*
296 * ID -> Name
297 */
298static int
299nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
300 __u32 id, char *name)
301{
302 struct rpc_pipe_msg msg;
303 struct idmap_msg *im;
304 struct idmap_hashent *he;
305 DECLARE_WAITQUEUE(wq, current);
306 int ret = -EIO;
307 unsigned int len;
308
309 im = &idmap->idmap_im;
310
Ingo Molnarc9d51282006-03-20 13:44:11 -0500311 mutex_lock(&idmap->idmap_lock);
312 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 he = idmap_lookup_id(h, id);
315 if (he != 0) {
316 memcpy(name, he->ih_name, he->ih_namelen);
317 ret = he->ih_namelen;
318 goto out;
319 }
320
321 memset(im, 0, sizeof(*im));
322 im->im_type = h->h_type;
323 im->im_conv = IDMAP_CONV_IDTONAME;
324 im->im_id = id;
325
326 memset(&msg, 0, sizeof(msg));
327 msg.data = im;
328 msg.len = sizeof(*im);
329
330 add_wait_queue(&idmap->idmap_wq, &wq);
331
332 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
333 remove_wait_queue(&idmap->idmap_wq, &wq);
334 goto out;
335 }
336
337 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500338 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 schedule();
340 current->state = TASK_RUNNING;
341 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500342 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (im->im_status & IDMAP_STATUS_SUCCESS) {
345 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
346 goto out;
347 memcpy(name, im->im_name, len);
348 ret = len;
349 }
350
351 out:
352 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500353 mutex_unlock(&idmap->idmap_im_lock);
354 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return ret;
356}
357
358/* RPC pipefs upcall/downcall routines */
359static ssize_t
360idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
361 char __user *dst, size_t buflen)
362{
363 char *data = (char *)msg->data + msg->copied;
364 ssize_t mlen = msg->len - msg->copied;
365 ssize_t left;
366
367 if (mlen > buflen)
368 mlen = buflen;
369
370 left = copy_to_user(dst, data, mlen);
371 if (left < 0) {
372 msg->errno = left;
373 return left;
374 }
375 mlen -= left;
376 msg->copied += mlen;
377 msg->errno = 0;
378 return mlen;
379}
380
381static ssize_t
382idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
383{
384 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
385 struct idmap *idmap = (struct idmap *)rpci->private;
386 struct idmap_msg im_in, *im = &idmap->idmap_im;
387 struct idmap_hashtable *h;
388 struct idmap_hashent *he = NULL;
389 int namelen_in;
390 int ret;
391
392 if (mlen != sizeof(im_in))
393 return (-ENOSPC);
394
395 if (copy_from_user(&im_in, src, mlen) != 0)
396 return (-EFAULT);
397
Ingo Molnarc9d51282006-03-20 13:44:11 -0500398 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 ret = mlen;
401 im->im_status = im_in.im_status;
402 /* If we got an error, terminate now, and wake up pending upcalls */
403 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
404 wake_up(&idmap->idmap_wq);
405 goto out;
406 }
407
408 /* Sanity checking of strings */
409 ret = -EINVAL;
410 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
411 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
412 goto out;
413
414 switch (im_in.im_type) {
415 case IDMAP_TYPE_USER:
416 h = &idmap->idmap_user_hash;
417 break;
418 case IDMAP_TYPE_GROUP:
419 h = &idmap->idmap_group_hash;
420 break;
421 default:
422 goto out;
423 }
424
425 switch (im_in.im_conv) {
426 case IDMAP_CONV_IDTONAME:
427 /* Did we match the current upcall? */
428 if (im->im_conv == IDMAP_CONV_IDTONAME
429 && im->im_type == im_in.im_type
430 && im->im_id == im_in.im_id) {
431 /* Yes: copy string, including the terminating '\0' */
432 memcpy(im->im_name, im_in.im_name, namelen_in);
433 im->im_name[namelen_in] = '\0';
434 wake_up(&idmap->idmap_wq);
435 }
436 he = idmap_alloc_id(h, im_in.im_id);
437 break;
438 case IDMAP_CONV_NAMETOID:
439 /* Did we match the current upcall? */
440 if (im->im_conv == IDMAP_CONV_NAMETOID
441 && im->im_type == im_in.im_type
442 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
443 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
444 im->im_id = im_in.im_id;
445 wake_up(&idmap->idmap_wq);
446 }
447 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
448 break;
449 default:
450 goto out;
451 }
452
453 /* If the entry is valid, also copy it to the cache */
454 if (he != NULL)
455 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
456 ret = mlen;
457out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500458 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return ret;
460}
461
Adrian Bunk75c96f82005-05-05 16:16:09 -0700462static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
464{
465 struct idmap_msg *im = msg->data;
466 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
467
468 if (msg->errno >= 0)
469 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500470 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
472 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500473 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
477 * Fowler/Noll/Vo hash
478 * http://www.isthe.com/chongo/tech/comp/fnv/
479 */
480
481#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
482#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
483
484static unsigned int fnvhash32(const void *buf, size_t buflen)
485{
486 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
487 unsigned int hash = FNV_1_32;
488
489 for (p = buf; p < end; p++) {
490 hash *= FNV_P_32;
491 hash ^= (unsigned int)*p;
492 }
493
494 return (hash);
495}
496
David Howellsadfa6f92006-08-22 20:06:08 -0400497int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct idmap *idmap = clp->cl_idmap;
500
501 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
502}
503
David Howellsadfa6f92006-08-22 20:06:08 -0400504int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct idmap *idmap = clp->cl_idmap;
507
508 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
509}
510
David Howellsadfa6f92006-08-22 20:06:08 -0400511int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct idmap *idmap = clp->cl_idmap;
514
515 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
516}
David Howellsadfa6f92006-08-22 20:06:08 -0400517int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct idmap *idmap = clp->cl_idmap;
520
521 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
522}
523