Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / nfs / idmap.c
blobdd99162b5f60bf8710f3375529ceaa75310b5c7f
1 /*
2 * fs/nfs/idmap.c
4 * UID and GID to name mapping for clients.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
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.
37 #include <linux/module.h>
38 #include <linux/mutex.h>
39 #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>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/workqueue.h>
48 #include <linux/sunrpc/rpc_pipe_fs.h>
50 #include <linux/nfs_fs.h>
52 #include <linux/nfs_idmap.h>
53 #include "nfs4_fs.h"
55 #define IDMAP_HASH_SZ 128
57 /* Default cache timeout is 10 minutes */
58 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
60 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
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;
71 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
72 &nfs_idmap_cache_timeout, 0644);
74 struct idmap_hashent {
75 unsigned long ih_expires;
76 __u32 ih_id;
77 size_t ih_namelen;
78 char ih_name[IDMAP_NAMESZ];
81 struct idmap_hashtable {
82 __u8 h_type;
83 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
86 struct idmap {
87 struct dentry *idmap_dentry;
88 wait_queue_head_t idmap_wq;
89 struct idmap_msg idmap_im;
90 struct mutex idmap_lock; /* Serializes upcalls */
91 struct mutex idmap_im_lock; /* Protects the hashtable */
92 struct idmap_hashtable idmap_user_hash;
93 struct idmap_hashtable idmap_group_hash;
96 static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
97 char __user *, size_t);
98 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
99 size_t);
100 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
102 static unsigned int fnvhash32(const void *, size_t);
104 static struct rpc_pipe_ops idmap_upcall_ops = {
105 .upcall = idmap_pipe_upcall,
106 .downcall = idmap_pipe_downcall,
107 .destroy_msg = idmap_pipe_destroy_msg,
111 nfs_idmap_new(struct nfs_client *clp)
113 struct idmap *idmap;
114 int error;
116 BUG_ON(clp->cl_idmap != NULL);
118 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
119 if (idmap == NULL)
120 return -ENOMEM;
122 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_dentry, "idmap",
123 idmap, &idmap_upcall_ops, 0);
124 if (IS_ERR(idmap->idmap_dentry)) {
125 error = PTR_ERR(idmap->idmap_dentry);
126 kfree(idmap);
127 return error;
130 mutex_init(&idmap->idmap_lock);
131 mutex_init(&idmap->idmap_im_lock);
132 init_waitqueue_head(&idmap->idmap_wq);
133 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
134 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
136 clp->cl_idmap = idmap;
137 return 0;
140 void
141 nfs_idmap_delete(struct nfs_client *clp)
143 struct idmap *idmap = clp->cl_idmap;
145 if (!idmap)
146 return;
147 rpc_unlink(idmap->idmap_dentry);
148 clp->cl_idmap = NULL;
149 kfree(idmap);
153 * Helper routines for manipulating the hashtable
155 static inline struct idmap_hashent *
156 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
158 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
161 static struct idmap_hashent *
162 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
164 struct idmap_hashent *he = idmap_name_hash(h, name, len);
166 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
167 return NULL;
168 if (time_after(jiffies, he->ih_expires))
169 return NULL;
170 return he;
173 static inline struct idmap_hashent *
174 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
176 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
179 static struct idmap_hashent *
180 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
182 struct idmap_hashent *he = idmap_id_hash(h, id);
183 if (he->ih_id != id || he->ih_namelen == 0)
184 return NULL;
185 if (time_after(jiffies, he->ih_expires))
186 return NULL;
187 return he;
191 * Routines for allocating new entries in the hashtable.
192 * For now, we just have 1 entry per bucket, so it's all
193 * pretty trivial.
195 static inline struct idmap_hashent *
196 idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
198 return idmap_name_hash(h, name, len);
201 static inline struct idmap_hashent *
202 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
204 return idmap_id_hash(h, id);
207 static void
208 idmap_update_entry(struct idmap_hashent *he, const char *name,
209 size_t namelen, __u32 id)
211 he->ih_id = id;
212 memcpy(he->ih_name, name, namelen);
213 he->ih_name[namelen] = '\0';
214 he->ih_namelen = namelen;
215 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
219 * Name -> ID
221 static int
222 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
223 const char *name, size_t namelen, __u32 *id)
225 struct rpc_pipe_msg msg;
226 struct idmap_msg *im;
227 struct idmap_hashent *he;
228 DECLARE_WAITQUEUE(wq, current);
229 int ret = -EIO;
231 im = &idmap->idmap_im;
234 * String sanity checks
235 * Note that the userland daemon expects NUL terminated strings
237 for (;;) {
238 if (namelen == 0)
239 return -EINVAL;
240 if (name[namelen-1] != '\0')
241 break;
242 namelen--;
244 if (namelen >= IDMAP_NAMESZ)
245 return -EINVAL;
247 mutex_lock(&idmap->idmap_lock);
248 mutex_lock(&idmap->idmap_im_lock);
250 he = idmap_lookup_name(h, name, namelen);
251 if (he != NULL) {
252 *id = he->ih_id;
253 ret = 0;
254 goto out;
257 memset(im, 0, sizeof(*im));
258 memcpy(im->im_name, name, namelen);
260 im->im_type = h->h_type;
261 im->im_conv = IDMAP_CONV_NAMETOID;
263 memset(&msg, 0, sizeof(msg));
264 msg.data = im;
265 msg.len = sizeof(*im);
267 add_wait_queue(&idmap->idmap_wq, &wq);
268 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
269 remove_wait_queue(&idmap->idmap_wq, &wq);
270 goto out;
273 set_current_state(TASK_UNINTERRUPTIBLE);
274 mutex_unlock(&idmap->idmap_im_lock);
275 schedule();
276 __set_current_state(TASK_RUNNING);
277 remove_wait_queue(&idmap->idmap_wq, &wq);
278 mutex_lock(&idmap->idmap_im_lock);
280 if (im->im_status & IDMAP_STATUS_SUCCESS) {
281 *id = im->im_id;
282 ret = 0;
285 out:
286 memset(im, 0, sizeof(*im));
287 mutex_unlock(&idmap->idmap_im_lock);
288 mutex_unlock(&idmap->idmap_lock);
289 return ret;
293 * ID -> Name
295 static int
296 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
297 __u32 id, char *name)
299 struct rpc_pipe_msg msg;
300 struct idmap_msg *im;
301 struct idmap_hashent *he;
302 DECLARE_WAITQUEUE(wq, current);
303 int ret = -EIO;
304 unsigned int len;
306 im = &idmap->idmap_im;
308 mutex_lock(&idmap->idmap_lock);
309 mutex_lock(&idmap->idmap_im_lock);
311 he = idmap_lookup_id(h, id);
312 <<<<<<< HEAD:fs/nfs/idmap.c
313 if (he != 0) {
314 =======
315 if (he) {
316 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/nfs/idmap.c
317 memcpy(name, he->ih_name, he->ih_namelen);
318 ret = he->ih_namelen;
319 goto out;
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;
327 memset(&msg, 0, sizeof(msg));
328 msg.data = im;
329 msg.len = sizeof(*im);
331 add_wait_queue(&idmap->idmap_wq, &wq);
333 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
334 remove_wait_queue(&idmap->idmap_wq, &wq);
335 goto out;
338 set_current_state(TASK_UNINTERRUPTIBLE);
339 mutex_unlock(&idmap->idmap_im_lock);
340 schedule();
341 __set_current_state(TASK_RUNNING);
342 remove_wait_queue(&idmap->idmap_wq, &wq);
343 mutex_lock(&idmap->idmap_im_lock);
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;
352 out:
353 memset(im, 0, sizeof(*im));
354 mutex_unlock(&idmap->idmap_im_lock);
355 mutex_unlock(&idmap->idmap_lock);
356 return ret;
359 /* RPC pipefs upcall/downcall routines */
360 static ssize_t
361 idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
362 char __user *dst, size_t buflen)
364 char *data = (char *)msg->data + msg->copied;
365 size_t mlen = min(msg->len, buflen);
366 unsigned long left;
368 left = copy_to_user(dst, data, mlen);
369 if (left == mlen) {
370 msg->errno = -EFAULT;
371 return -EFAULT;
374 mlen -= left;
375 msg->copied += mlen;
376 msg->errno = 0;
377 return mlen;
380 static ssize_t
381 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
383 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
384 struct idmap *idmap = (struct idmap *)rpci->private;
385 struct idmap_msg im_in, *im = &idmap->idmap_im;
386 struct idmap_hashtable *h;
387 struct idmap_hashent *he = NULL;
388 size_t namelen_in;
389 int ret;
391 if (mlen != sizeof(im_in))
392 return -ENOSPC;
394 if (copy_from_user(&im_in, src, mlen) != 0)
395 return -EFAULT;
397 mutex_lock(&idmap->idmap_im_lock);
399 ret = mlen;
400 im->im_status = im_in.im_status;
401 /* If we got an error, terminate now, and wake up pending upcalls */
402 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
403 wake_up(&idmap->idmap_wq);
404 goto out;
407 /* Sanity checking of strings */
408 ret = -EINVAL;
409 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
410 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
411 goto out;
413 switch (im_in.im_type) {
414 case IDMAP_TYPE_USER:
415 h = &idmap->idmap_user_hash;
416 break;
417 case IDMAP_TYPE_GROUP:
418 h = &idmap->idmap_group_hash;
419 break;
420 default:
421 goto out;
424 switch (im_in.im_conv) {
425 case IDMAP_CONV_IDTONAME:
426 /* Did we match the current upcall? */
427 if (im->im_conv == IDMAP_CONV_IDTONAME
428 && im->im_type == im_in.im_type
429 && im->im_id == im_in.im_id) {
430 /* Yes: copy string, including the terminating '\0' */
431 memcpy(im->im_name, im_in.im_name, namelen_in);
432 im->im_name[namelen_in] = '\0';
433 wake_up(&idmap->idmap_wq);
435 he = idmap_alloc_id(h, im_in.im_id);
436 break;
437 case IDMAP_CONV_NAMETOID:
438 /* Did we match the current upcall? */
439 if (im->im_conv == IDMAP_CONV_NAMETOID
440 && im->im_type == im_in.im_type
441 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
442 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
443 im->im_id = im_in.im_id;
444 wake_up(&idmap->idmap_wq);
446 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
447 break;
448 default:
449 goto out;
452 /* If the entry is valid, also copy it to the cache */
453 if (he != NULL)
454 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
455 ret = mlen;
456 out:
457 mutex_unlock(&idmap->idmap_im_lock);
458 return ret;
461 static void
462 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
464 struct idmap_msg *im = msg->data;
465 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
467 if (msg->errno >= 0)
468 return;
469 mutex_lock(&idmap->idmap_im_lock);
470 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
471 wake_up(&idmap->idmap_wq);
472 mutex_unlock(&idmap->idmap_im_lock);
476 * Fowler/Noll/Vo hash
477 * http://www.isthe.com/chongo/tech/comp/fnv/
480 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
481 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
483 static unsigned int fnvhash32(const void *buf, size_t buflen)
485 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
486 unsigned int hash = FNV_1_32;
488 for (p = buf; p < end; p++) {
489 hash *= FNV_P_32;
490 hash ^= (unsigned int)*p;
493 return hash;
496 int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
498 struct idmap *idmap = clp->cl_idmap;
500 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
503 int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
505 struct idmap *idmap = clp->cl_idmap;
507 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
510 int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
512 struct idmap *idmap = clp->cl_idmap;
514 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
516 int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
518 struct idmap *idmap = clp->cl_idmap;
520 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);