]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/nfs/idmap.c
b8c41c3f259a6b759b2837bd706c67a3c38ea1be
[karo-tx-linux.git] / fs / nfs / idmap.c
1 /*
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 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/nfs_idmap.h>
41
42 static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
43 {
44         unsigned long val;
45         char buf[16];
46
47         if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
48                 return 0;
49         memcpy(buf, name, namelen);
50         buf[namelen] = '\0';
51         if (strict_strtoul(buf, 0, &val) != 0)
52                 return 0;
53         *res = val;
54         return 1;
55 }
56
57 static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
58 {
59         return snprintf(buf, buflen, "%u", id);
60 }
61
62 #ifdef CONFIG_NFS_USE_NEW_IDMAPPER
63
64 #include <linux/cred.h>
65 #include <linux/sunrpc/sched.h>
66 #include <linux/nfs4.h>
67 #include <linux/nfs_fs_sb.h>
68 #include <linux/keyctl.h>
69 #include <linux/key-type.h>
70 #include <linux/rcupdate.h>
71 #include <linux/err.h>
72
73 #include <keys/user-type.h>
74
75 #define NFS_UINT_MAXLEN 11
76
77 const struct cred *id_resolver_cache;
78
79 struct key_type key_type_id_resolver = {
80         .name           = "id_resolver",
81         .instantiate    = user_instantiate,
82         .match          = user_match,
83         .revoke         = user_revoke,
84         .destroy        = user_destroy,
85         .describe       = user_describe,
86         .read           = user_read,
87 };
88
89 int nfs_idmap_init(void)
90 {
91         struct cred *cred;
92         struct key *keyring;
93         int ret = 0;
94
95         printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
96
97         cred = prepare_kernel_cred(NULL);
98         if (!cred)
99                 return -ENOMEM;
100
101         keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
102                              (KEY_POS_ALL & ~KEY_POS_SETATTR) |
103                              KEY_USR_VIEW | KEY_USR_READ,
104                              KEY_ALLOC_NOT_IN_QUOTA);
105         if (IS_ERR(keyring)) {
106                 ret = PTR_ERR(keyring);
107                 goto failed_put_cred;
108         }
109
110         ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
111         if (ret < 0)
112                 goto failed_put_key;
113
114         ret = register_key_type(&key_type_id_resolver);
115         if (ret < 0)
116                 goto failed_put_key;
117
118         cred->thread_keyring = keyring;
119         cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
120         id_resolver_cache = cred;
121         return 0;
122
123 failed_put_key:
124         key_put(keyring);
125 failed_put_cred:
126         put_cred(cred);
127         return ret;
128 }
129
130 void nfs_idmap_quit(void)
131 {
132         key_revoke(id_resolver_cache->thread_keyring);
133         unregister_key_type(&key_type_id_resolver);
134         put_cred(id_resolver_cache);
135 }
136
137 /*
138  * Assemble the description to pass to request_key()
139  * This function will allocate a new string and update dest to point
140  * at it.  The caller is responsible for freeing dest.
141  *
142  * On error 0 is returned.  Otherwise, the length of dest is returned.
143  */
144 static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
145                                 const char *type, size_t typelen, char **desc)
146 {
147         char *cp;
148         size_t desclen = typelen + namelen + 2;
149
150         *desc = kmalloc(desclen, GFP_KERNEL);
151         if (!*desc)
152                 return -ENOMEM;
153
154         cp = *desc;
155         memcpy(cp, type, typelen);
156         cp += typelen;
157         *cp++ = ':';
158
159         memcpy(cp, name, namelen);
160         cp += namelen;
161         *cp = '\0';
162         return desclen;
163 }
164
165 static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
166                 const char *type, void *data, size_t data_size)
167 {
168         const struct cred *saved_cred;
169         struct key *rkey;
170         char *desc;
171         struct user_key_payload *payload;
172         ssize_t ret;
173
174         ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
175         if (ret <= 0)
176                 goto out;
177
178         saved_cred = override_creds(id_resolver_cache);
179         rkey = request_key(&key_type_id_resolver, desc, "");
180         revert_creds(saved_cred);
181         kfree(desc);
182         if (IS_ERR(rkey)) {
183                 ret = PTR_ERR(rkey);
184                 goto out;
185         }
186
187         rcu_read_lock();
188         rkey->perm |= KEY_USR_VIEW;
189
190         ret = key_validate(rkey);
191         if (ret < 0)
192                 goto out_up;
193
194         payload = rcu_dereference(rkey->payload.data);
195         if (IS_ERR_OR_NULL(payload)) {
196                 ret = PTR_ERR(payload);
197                 goto out_up;
198         }
199
200         ret = payload->datalen;
201         if (ret > 0 && ret <= data_size)
202                 memcpy(data, payload->data, ret);
203         else
204                 ret = -EINVAL;
205
206 out_up:
207         rcu_read_unlock();
208         key_put(rkey);
209 out:
210         return ret;
211 }
212
213
214 /* ID -> Name */
215 static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
216 {
217         char id_str[NFS_UINT_MAXLEN];
218         int id_len;
219         ssize_t ret;
220
221         id_len = snprintf(id_str, sizeof(id_str), "%u", id);
222         ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
223         if (ret < 0)
224                 return -EINVAL;
225         return ret;
226 }
227
228 /* Name -> ID */
229 static int nfs_idmap_lookup_id(const char *name, size_t namelen,
230                                 const char *type, __u32 *id)
231 {
232         char id_str[NFS_UINT_MAXLEN];
233         long id_long;
234         ssize_t data_size;
235         int ret = 0;
236
237         data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
238         if (data_size <= 0) {
239                 ret = -EINVAL;
240         } else {
241                 ret = strict_strtol(id_str, 10, &id_long);
242                 *id = (__u32)id_long;
243         }
244         return ret;
245 }
246
247 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
248 {
249         if (nfs_map_string_to_numeric(name, namelen, uid))
250                 return 0;
251         return nfs_idmap_lookup_id(name, namelen, "uid", uid);
252 }
253
254 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
255 {
256         if (nfs_map_string_to_numeric(name, namelen, gid))
257                 return 0;
258         return nfs_idmap_lookup_id(name, namelen, "gid", gid);
259 }
260
261 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
262 {
263         int ret = -EINVAL;
264
265         if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
266                 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
267         if (ret < 0)
268                 ret = nfs_map_numeric_to_string(uid, buf, buflen);
269         return ret;
270 }
271 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
272 {
273         int ret = -EINVAL;
274
275         if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
276                 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
277         if (ret < 0)
278                 ret = nfs_map_numeric_to_string(gid, buf, buflen);
279         return ret;
280 }
281
282 #else  /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
283
284 #include <linux/module.h>
285 #include <linux/mutex.h>
286 #include <linux/init.h>
287 #include <linux/socket.h>
288 #include <linux/in.h>
289 #include <linux/sched.h>
290 #include <linux/sunrpc/clnt.h>
291 #include <linux/workqueue.h>
292 #include <linux/sunrpc/rpc_pipe_fs.h>
293
294 #include <linux/nfs_fs.h>
295
296 #include "nfs4_fs.h"
297
298 #define IDMAP_HASH_SZ          128
299
300 /* Default cache timeout is 10 minutes */
301 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
302
303 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
304 {
305         char *endp;
306         int num = simple_strtol(val, &endp, 0);
307         int jif = num * HZ;
308         if (endp == val || *endp || num < 0 || jif < num)
309                 return -EINVAL;
310         *((int *)kp->arg) = jif;
311         return 0;
312 }
313
314 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
315                  &nfs_idmap_cache_timeout, 0644);
316
317 struct idmap_hashent {
318         unsigned long           ih_expires;
319         __u32                   ih_id;
320         size_t                  ih_namelen;
321         const char              *ih_name;
322 };
323
324 struct idmap_hashtable {
325         __u8                    h_type;
326         struct idmap_hashent    h_entries[IDMAP_HASH_SZ];
327 };
328
329 struct idmap {
330         struct dentry           *idmap_dentry;
331         wait_queue_head_t       idmap_wq;
332         struct idmap_msg        idmap_im;
333         struct mutex            idmap_lock;     /* Serializes upcalls */
334         struct mutex            idmap_im_lock;  /* Protects the hashtable */
335         struct idmap_hashtable  idmap_user_hash;
336         struct idmap_hashtable  idmap_group_hash;
337 };
338
339 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
340                                    size_t);
341 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
342
343 static unsigned int fnvhash32(const void *, size_t);
344
345 static const struct rpc_pipe_ops idmap_upcall_ops = {
346         .upcall         = rpc_pipe_generic_upcall,
347         .downcall       = idmap_pipe_downcall,
348         .destroy_msg    = idmap_pipe_destroy_msg,
349 };
350
351 int
352 nfs_idmap_new(struct nfs_client *clp)
353 {
354         struct idmap *idmap;
355         int error;
356
357         BUG_ON(clp->cl_idmap != NULL);
358
359         idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
360         if (idmap == NULL)
361                 return -ENOMEM;
362
363         idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
364                         "idmap", idmap, &idmap_upcall_ops, 0);
365         if (IS_ERR(idmap->idmap_dentry)) {
366                 error = PTR_ERR(idmap->idmap_dentry);
367                 kfree(idmap);
368                 return error;
369         }
370
371         mutex_init(&idmap->idmap_lock);
372         mutex_init(&idmap->idmap_im_lock);
373         init_waitqueue_head(&idmap->idmap_wq);
374         idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
375         idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
376
377         clp->cl_idmap = idmap;
378         return 0;
379 }
380
381 void
382 nfs_idmap_delete(struct nfs_client *clp)
383 {
384         struct idmap *idmap = clp->cl_idmap;
385         int i;
386
387         if (!idmap)
388                 return;
389         rpc_unlink(idmap->idmap_dentry);
390         clp->cl_idmap = NULL;
391         for (i = 0; i < ARRAY_SIZE(idmap->idmap_user_hash.h_entries); i++)
392                 kfree(idmap->idmap_user_hash.h_entries[i].ih_name);
393         for (i = 0; i < ARRAY_SIZE(idmap->idmap_group_hash.h_entries); i++)
394                 kfree(idmap->idmap_group_hash.h_entries[i].ih_name);
395         kfree(idmap);
396 }
397
398 /*
399  * Helper routines for manipulating the hashtable
400  */
401 static inline struct idmap_hashent *
402 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
403 {
404         return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
405 }
406
407 static struct idmap_hashent *
408 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
409 {
410         struct idmap_hashent *he = idmap_name_hash(h, name, len);
411
412         if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
413                 return NULL;
414         if (time_after(jiffies, he->ih_expires))
415                 return NULL;
416         return he;
417 }
418
419 static inline struct idmap_hashent *
420 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
421 {
422         return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
423 }
424
425 static struct idmap_hashent *
426 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
427 {
428         struct idmap_hashent *he = idmap_id_hash(h, id);
429         if (he->ih_id != id || he->ih_namelen == 0)
430                 return NULL;
431         if (time_after(jiffies, he->ih_expires))
432                 return NULL;
433         return he;
434 }
435
436 /*
437  * Routines for allocating new entries in the hashtable.
438  * For now, we just have 1 entry per bucket, so it's all
439  * pretty trivial.
440  */
441 static inline struct idmap_hashent *
442 idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
443 {
444         return idmap_name_hash(h, name, len);
445 }
446
447 static inline struct idmap_hashent *
448 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
449 {
450         return idmap_id_hash(h, id);
451 }
452
453 static void
454 idmap_update_entry(struct idmap_hashent *he, const char *name,
455                 size_t namelen, __u32 id)
456 {
457         char *str = kmalloc(namelen + 1, GFP_KERNEL);
458         if (str == NULL)
459                 return;
460         kfree(he->ih_name);
461         he->ih_id = id;
462         memcpy(str, name, namelen);
463         str[namelen] = '\0';
464         he->ih_name = str;
465         he->ih_namelen = namelen;
466         he->ih_expires = jiffies + nfs_idmap_cache_timeout;
467 }
468
469 /*
470  * Name -> ID
471  */
472 static int
473 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
474                 const char *name, size_t namelen, __u32 *id)
475 {
476         struct rpc_pipe_msg msg;
477         struct idmap_msg *im;
478         struct idmap_hashent *he;
479         DECLARE_WAITQUEUE(wq, current);
480         int ret = -EIO;
481
482         im = &idmap->idmap_im;
483
484         /*
485          * String sanity checks
486          * Note that the userland daemon expects NUL terminated strings
487          */
488         for (;;) {
489                 if (namelen == 0)
490                         return -EINVAL;
491                 if (name[namelen-1] != '\0')
492                         break;
493                 namelen--;
494         }
495         if (namelen >= IDMAP_NAMESZ)
496                 return -EINVAL;
497
498         mutex_lock(&idmap->idmap_lock);
499         mutex_lock(&idmap->idmap_im_lock);
500
501         he = idmap_lookup_name(h, name, namelen);
502         if (he != NULL) {
503                 *id = he->ih_id;
504                 ret = 0;
505                 goto out;
506         }
507
508         memset(im, 0, sizeof(*im));
509         memcpy(im->im_name, name, namelen);
510
511         im->im_type = h->h_type;
512         im->im_conv = IDMAP_CONV_NAMETOID;
513
514         memset(&msg, 0, sizeof(msg));
515         msg.data = im;
516         msg.len = sizeof(*im);
517
518         add_wait_queue(&idmap->idmap_wq, &wq);
519         if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
520                 remove_wait_queue(&idmap->idmap_wq, &wq);
521                 goto out;
522         }
523
524         set_current_state(TASK_UNINTERRUPTIBLE);
525         mutex_unlock(&idmap->idmap_im_lock);
526         schedule();
527         __set_current_state(TASK_RUNNING);
528         remove_wait_queue(&idmap->idmap_wq, &wq);
529         mutex_lock(&idmap->idmap_im_lock);
530
531         if (im->im_status & IDMAP_STATUS_SUCCESS) {
532                 *id = im->im_id;
533                 ret = 0;
534         }
535
536  out:
537         memset(im, 0, sizeof(*im));
538         mutex_unlock(&idmap->idmap_im_lock);
539         mutex_unlock(&idmap->idmap_lock);
540         return ret;
541 }
542
543 /*
544  * ID -> Name
545  */
546 static int
547 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
548                 __u32 id, char *name)
549 {
550         struct rpc_pipe_msg msg;
551         struct idmap_msg *im;
552         struct idmap_hashent *he;
553         DECLARE_WAITQUEUE(wq, current);
554         int ret = -EIO;
555         unsigned int len;
556
557         im = &idmap->idmap_im;
558
559         mutex_lock(&idmap->idmap_lock);
560         mutex_lock(&idmap->idmap_im_lock);
561
562         he = idmap_lookup_id(h, id);
563         if (he) {
564                 memcpy(name, he->ih_name, he->ih_namelen);
565                 ret = he->ih_namelen;
566                 goto out;
567         }
568
569         memset(im, 0, sizeof(*im));
570         im->im_type = h->h_type;
571         im->im_conv = IDMAP_CONV_IDTONAME;
572         im->im_id = id;
573
574         memset(&msg, 0, sizeof(msg));
575         msg.data = im;
576         msg.len = sizeof(*im);
577
578         add_wait_queue(&idmap->idmap_wq, &wq);
579
580         if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
581                 remove_wait_queue(&idmap->idmap_wq, &wq);
582                 goto out;
583         }
584
585         set_current_state(TASK_UNINTERRUPTIBLE);
586         mutex_unlock(&idmap->idmap_im_lock);
587         schedule();
588         __set_current_state(TASK_RUNNING);
589         remove_wait_queue(&idmap->idmap_wq, &wq);
590         mutex_lock(&idmap->idmap_im_lock);
591
592         if (im->im_status & IDMAP_STATUS_SUCCESS) {
593                 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
594                         goto out;
595                 memcpy(name, im->im_name, len);
596                 ret = len;
597         }
598
599  out:
600         memset(im, 0, sizeof(*im));
601         mutex_unlock(&idmap->idmap_im_lock);
602         mutex_unlock(&idmap->idmap_lock);
603         return ret;
604 }
605
606 static ssize_t
607 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
608 {
609         struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
610         struct idmap *idmap = (struct idmap *)rpci->private;
611         struct idmap_msg im_in, *im = &idmap->idmap_im;
612         struct idmap_hashtable *h;
613         struct idmap_hashent *he = NULL;
614         size_t namelen_in;
615         int ret;
616
617         if (mlen != sizeof(im_in))
618                 return -ENOSPC;
619
620         if (copy_from_user(&im_in, src, mlen) != 0)
621                 return -EFAULT;
622
623         mutex_lock(&idmap->idmap_im_lock);
624
625         ret = mlen;
626         im->im_status = im_in.im_status;
627         /* If we got an error, terminate now, and wake up pending upcalls */
628         if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
629                 wake_up(&idmap->idmap_wq);
630                 goto out;
631         }
632
633         /* Sanity checking of strings */
634         ret = -EINVAL;
635         namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
636         if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
637                 goto out;
638
639         switch (im_in.im_type) {
640                 case IDMAP_TYPE_USER:
641                         h = &idmap->idmap_user_hash;
642                         break;
643                 case IDMAP_TYPE_GROUP:
644                         h = &idmap->idmap_group_hash;
645                         break;
646                 default:
647                         goto out;
648         }
649
650         switch (im_in.im_conv) {
651         case IDMAP_CONV_IDTONAME:
652                 /* Did we match the current upcall? */
653                 if (im->im_conv == IDMAP_CONV_IDTONAME
654                                 && im->im_type == im_in.im_type
655                                 && im->im_id == im_in.im_id) {
656                         /* Yes: copy string, including the terminating '\0'  */
657                         memcpy(im->im_name, im_in.im_name, namelen_in);
658                         im->im_name[namelen_in] = '\0';
659                         wake_up(&idmap->idmap_wq);
660                 }
661                 he = idmap_alloc_id(h, im_in.im_id);
662                 break;
663         case IDMAP_CONV_NAMETOID:
664                 /* Did we match the current upcall? */
665                 if (im->im_conv == IDMAP_CONV_NAMETOID
666                                 && im->im_type == im_in.im_type
667                                 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
668                                 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
669                         im->im_id = im_in.im_id;
670                         wake_up(&idmap->idmap_wq);
671                 }
672                 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
673                 break;
674         default:
675                 goto out;
676         }
677
678         /* If the entry is valid, also copy it to the cache */
679         if (he != NULL)
680                 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
681         ret = mlen;
682 out:
683         mutex_unlock(&idmap->idmap_im_lock);
684         return ret;
685 }
686
687 static void
688 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
689 {
690         struct idmap_msg *im = msg->data;
691         struct idmap *idmap = container_of(im, struct idmap, idmap_im); 
692
693         if (msg->errno >= 0)
694                 return;
695         mutex_lock(&idmap->idmap_im_lock);
696         im->im_status = IDMAP_STATUS_LOOKUPFAIL;
697         wake_up(&idmap->idmap_wq);
698         mutex_unlock(&idmap->idmap_im_lock);
699 }
700
701 /* 
702  * Fowler/Noll/Vo hash
703  *    http://www.isthe.com/chongo/tech/comp/fnv/
704  */
705
706 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
707 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
708
709 static unsigned int fnvhash32(const void *buf, size_t buflen)
710 {
711         const unsigned char *p, *end = (const unsigned char *)buf + buflen;
712         unsigned int hash = FNV_1_32;
713
714         for (p = buf; p < end; p++) {
715                 hash *= FNV_P_32;
716                 hash ^= (unsigned int)*p;
717         }
718
719         return hash;
720 }
721
722 int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
723 {
724         struct idmap *idmap = server->nfs_client->cl_idmap;
725
726         if (nfs_map_string_to_numeric(name, namelen, uid))
727                 return 0;
728         return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
729 }
730
731 int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
732 {
733         struct idmap *idmap = server->nfs_client->cl_idmap;
734
735         if (nfs_map_string_to_numeric(name, namelen, uid))
736                 return 0;
737         return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
738 }
739
740 int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
741 {
742         struct idmap *idmap = server->nfs_client->cl_idmap;
743         int ret = -EINVAL;
744
745         if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
746                 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
747         if (ret < 0)
748                 ret = nfs_map_numeric_to_string(uid, buf, buflen);
749         return ret;
750 }
751 int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
752 {
753         struct idmap *idmap = server->nfs_client->cl_idmap;
754         int ret = -EINVAL;
755
756         if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
757                 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
758         if (ret < 0)
759                 ret = nfs_map_numeric_to_string(uid, buf, buflen);
760         return ret;
761 }
762
763 #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */