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