NFS: Remove pNFS bloat from the generic write path
[linux-2.6-block.git] / fs / nfs / idmap.c
CommitLineData
1da177e4
LT
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 */
5cf36cfd
TM
36#include <linux/types.h>
37#include <linux/string.h>
38#include <linux/kernel.h>
e44ba033
VI
39#include <linux/slab.h>
40#include <linux/nfs_idmap.h>
5cf36cfd
TM
41
42static 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}
1da177e4 56
f0b85168
TM
57static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
58{
59 return snprintf(buf, buflen, "%u", id);
60}
61
955a857e
BS
62#ifdef CONFIG_NFS_USE_NEW_IDMAPPER
63
955a857e 64#include <linux/cred.h>
b064eca2
TM
65#include <linux/sunrpc/sched.h>
66#include <linux/nfs4.h>
67#include <linux/nfs_fs_sb.h>
955a857e
BS
68#include <linux/keyctl.h>
69#include <linux/key-type.h>
70#include <linux/rcupdate.h>
955a857e
BS
71#include <linux/err.h>
72
73#include <keys/user-type.h>
74
75#define NFS_UINT_MAXLEN 11
76
77const struct cred *id_resolver_cache;
78
79struct 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
89int 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
123failed_put_key:
124 key_put(keyring);
125failed_put_cred:
126 put_cred(cred);
127 return ret;
128}
129
130void 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 */
144static 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);
8f0d97b4 151 if (!*desc)
955a857e
BS
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
165static 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
206out_up:
207 rcu_read_unlock();
208 key_put(rkey);
209out:
210 return ret;
211}
212
213
214/* ID -> Name */
215static 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 */
229static 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
e4fd72a1 247int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
955a857e 248{
5cf36cfd
TM
249 if (nfs_map_string_to_numeric(name, namelen, uid))
250 return 0;
955a857e
BS
251 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
252}
253
e4fd72a1 254int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
955a857e 255{
5cf36cfd
TM
256 if (nfs_map_string_to_numeric(name, namelen, gid))
257 return 0;
955a857e
BS
258 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
259}
260
e4fd72a1 261int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
955a857e 262{
b064eca2
TM
263 int ret = -EINVAL;
264
265 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
266 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
f0b85168
TM
267 if (ret < 0)
268 ret = nfs_map_numeric_to_string(uid, buf, buflen);
269 return ret;
955a857e 270}
e4fd72a1 271int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
955a857e 272{
b064eca2 273 int ret = -EINVAL;
f0b85168 274
b064eca2
TM
275 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
276 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
f0b85168
TM
277 if (ret < 0)
278 ret = nfs_map_numeric_to_string(gid, buf, buflen);
279 return ret;
955a857e
BS
280}
281
5f3e97c9 282#else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
955a857e 283
1da177e4 284#include <linux/module.h>
c9d5128a 285#include <linux/mutex.h>
1da177e4 286#include <linux/init.h>
1da177e4
LT
287#include <linux/socket.h>
288#include <linux/in.h>
289#include <linux/sched.h>
1da177e4
LT
290#include <linux/sunrpc/clnt.h>
291#include <linux/workqueue.h>
292#include <linux/sunrpc/rpc_pipe_fs.h>
293
1da177e4
LT
294#include <linux/nfs_fs.h>
295
4ce79717 296#include "nfs4_fs.h"
1da177e4
LT
297
298#define IDMAP_HASH_SZ 128
299
58df095b
TM
300/* Default cache timeout is 10 minutes */
301unsigned int nfs_idmap_cache_timeout = 600 * HZ;
302
7d4e2747
DH
303static 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
314module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
315 &nfs_idmap_cache_timeout, 0644);
316
1da177e4 317struct idmap_hashent {
369af0f1
CL
318 unsigned long ih_expires;
319 __u32 ih_id;
d24aae41 320 size_t ih_namelen;
369af0f1 321 char ih_name[IDMAP_NAMESZ];
1da177e4
LT
322};
323
324struct idmap_hashtable {
369af0f1
CL
325 __u8 h_type;
326 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
1da177e4
LT
327};
328
329struct idmap {
369af0f1
CL
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;
1da177e4
LT
337};
338
369af0f1
CL
339static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
340 size_t);
341static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
1da177e4
LT
342
343static unsigned int fnvhash32(const void *, size_t);
344
b693ba4a 345static const struct rpc_pipe_ops idmap_upcall_ops = {
c1225158 346 .upcall = rpc_pipe_generic_upcall,
369af0f1
CL
347 .downcall = idmap_pipe_downcall,
348 .destroy_msg = idmap_pipe_destroy_msg,
1da177e4
LT
349};
350
b7162792 351int
adfa6f98 352nfs_idmap_new(struct nfs_client *clp)
1da177e4
LT
353{
354 struct idmap *idmap;
b7162792 355 int error;
1da177e4 356
54ceac45 357 BUG_ON(clp->cl_idmap != NULL);
b7162792 358
369af0f1
CL
359 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
360 if (idmap == NULL)
361 return -ENOMEM;
1da177e4 362
7d217cac
TM
363 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
364 "idmap", idmap, &idmap_upcall_ops, 0);
369af0f1 365 if (IS_ERR(idmap->idmap_dentry)) {
b7162792 366 error = PTR_ERR(idmap->idmap_dentry);
1da177e4 367 kfree(idmap);
b7162792 368 return error;
1da177e4
LT
369 }
370
369af0f1
CL
371 mutex_init(&idmap->idmap_lock);
372 mutex_init(&idmap->idmap_im_lock);
1da177e4
LT
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;
b7162792 378 return 0;
1da177e4
LT
379}
380
381void
adfa6f98 382nfs_idmap_delete(struct nfs_client *clp)
1da177e4
LT
383{
384 struct idmap *idmap = clp->cl_idmap;
385
386 if (!idmap)
387 return;
5d67476f 388 rpc_unlink(idmap->idmap_dentry);
1da177e4
LT
389 clp->cl_idmap = NULL;
390 kfree(idmap);
391}
392
393/*
394 * Helper routines for manipulating the hashtable
395 */
396static inline struct idmap_hashent *
397idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
398{
399 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
400}
401
402static struct idmap_hashent *
403idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
404{
405 struct idmap_hashent *he = idmap_name_hash(h, name, len);
406
407 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
408 return NULL;
58df095b
TM
409 if (time_after(jiffies, he->ih_expires))
410 return NULL;
1da177e4
LT
411 return he;
412}
413
414static inline struct idmap_hashent *
415idmap_id_hash(struct idmap_hashtable* h, __u32 id)
416{
417 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
418}
419
420static struct idmap_hashent *
421idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
422{
423 struct idmap_hashent *he = idmap_id_hash(h, id);
424 if (he->ih_id != id || he->ih_namelen == 0)
425 return NULL;
58df095b
TM
426 if (time_after(jiffies, he->ih_expires))
427 return NULL;
1da177e4
LT
428 return he;
429}
430
431/*
432 * Routines for allocating new entries in the hashtable.
433 * For now, we just have 1 entry per bucket, so it's all
434 * pretty trivial.
435 */
436static inline struct idmap_hashent *
d24aae41 437idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
1da177e4
LT
438{
439 return idmap_name_hash(h, name, len);
440}
441
442static inline struct idmap_hashent *
443idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
444{
445 return idmap_id_hash(h, id);
446}
447
448static void
449idmap_update_entry(struct idmap_hashent *he, const char *name,
450 size_t namelen, __u32 id)
451{
452 he->ih_id = id;
453 memcpy(he->ih_name, name, namelen);
454 he->ih_name[namelen] = '\0';
455 he->ih_namelen = namelen;
58df095b 456 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
1da177e4
LT
457}
458
459/*
460 * Name -> ID
461 */
462static int
463nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
464 const char *name, size_t namelen, __u32 *id)
465{
466 struct rpc_pipe_msg msg;
467 struct idmap_msg *im;
468 struct idmap_hashent *he;
469 DECLARE_WAITQUEUE(wq, current);
470 int ret = -EIO;
471
472 im = &idmap->idmap_im;
473
474 /*
475 * String sanity checks
476 * Note that the userland daemon expects NUL terminated strings
477 */
478 for (;;) {
479 if (namelen == 0)
480 return -EINVAL;
481 if (name[namelen-1] != '\0')
482 break;
483 namelen--;
484 }
485 if (namelen >= IDMAP_NAMESZ)
486 return -EINVAL;
487
c9d5128a
IM
488 mutex_lock(&idmap->idmap_lock);
489 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
490
491 he = idmap_lookup_name(h, name, namelen);
492 if (he != NULL) {
493 *id = he->ih_id;
494 ret = 0;
495 goto out;
496 }
497
498 memset(im, 0, sizeof(*im));
499 memcpy(im->im_name, name, namelen);
500
501 im->im_type = h->h_type;
502 im->im_conv = IDMAP_CONV_NAMETOID;
503
504 memset(&msg, 0, sizeof(msg));
505 msg.data = im;
506 msg.len = sizeof(*im);
507
508 add_wait_queue(&idmap->idmap_wq, &wq);
509 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
510 remove_wait_queue(&idmap->idmap_wq, &wq);
511 goto out;
512 }
513
514 set_current_state(TASK_UNINTERRUPTIBLE);
c9d5128a 515 mutex_unlock(&idmap->idmap_im_lock);
1da177e4 516 schedule();
fee7f23f 517 __set_current_state(TASK_RUNNING);
1da177e4 518 remove_wait_queue(&idmap->idmap_wq, &wq);
c9d5128a 519 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
520
521 if (im->im_status & IDMAP_STATUS_SUCCESS) {
522 *id = im->im_id;
523 ret = 0;
524 }
525
526 out:
527 memset(im, 0, sizeof(*im));
c9d5128a
IM
528 mutex_unlock(&idmap->idmap_im_lock);
529 mutex_unlock(&idmap->idmap_lock);
369af0f1 530 return ret;
1da177e4
LT
531}
532
533/*
534 * ID -> Name
535 */
536static int
537nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
538 __u32 id, char *name)
539{
540 struct rpc_pipe_msg msg;
541 struct idmap_msg *im;
542 struct idmap_hashent *he;
543 DECLARE_WAITQUEUE(wq, current);
544 int ret = -EIO;
545 unsigned int len;
546
547 im = &idmap->idmap_im;
548
c9d5128a
IM
549 mutex_lock(&idmap->idmap_lock);
550 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
551
552 he = idmap_lookup_id(h, id);
90dc7d27 553 if (he) {
1da177e4
LT
554 memcpy(name, he->ih_name, he->ih_namelen);
555 ret = he->ih_namelen;
556 goto out;
557 }
558
559 memset(im, 0, sizeof(*im));
560 im->im_type = h->h_type;
561 im->im_conv = IDMAP_CONV_IDTONAME;
562 im->im_id = id;
563
564 memset(&msg, 0, sizeof(msg));
565 msg.data = im;
566 msg.len = sizeof(*im);
567
568 add_wait_queue(&idmap->idmap_wq, &wq);
569
570 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
571 remove_wait_queue(&idmap->idmap_wq, &wq);
572 goto out;
573 }
574
575 set_current_state(TASK_UNINTERRUPTIBLE);
c9d5128a 576 mutex_unlock(&idmap->idmap_im_lock);
1da177e4 577 schedule();
fee7f23f 578 __set_current_state(TASK_RUNNING);
1da177e4 579 remove_wait_queue(&idmap->idmap_wq, &wq);
c9d5128a 580 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
581
582 if (im->im_status & IDMAP_STATUS_SUCCESS) {
583 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
584 goto out;
585 memcpy(name, im->im_name, len);
586 ret = len;
587 }
588
589 out:
590 memset(im, 0, sizeof(*im));
c9d5128a
IM
591 mutex_unlock(&idmap->idmap_im_lock);
592 mutex_unlock(&idmap->idmap_lock);
1da177e4
LT
593 return ret;
594}
595
1da177e4
LT
596static ssize_t
597idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
598{
369af0f1 599 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
1da177e4
LT
600 struct idmap *idmap = (struct idmap *)rpci->private;
601 struct idmap_msg im_in, *im = &idmap->idmap_im;
602 struct idmap_hashtable *h;
603 struct idmap_hashent *he = NULL;
d24aae41 604 size_t namelen_in;
1da177e4
LT
605 int ret;
606
369af0f1
CL
607 if (mlen != sizeof(im_in))
608 return -ENOSPC;
1da177e4 609
369af0f1
CL
610 if (copy_from_user(&im_in, src, mlen) != 0)
611 return -EFAULT;
1da177e4 612
c9d5128a 613 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
614
615 ret = mlen;
616 im->im_status = im_in.im_status;
617 /* If we got an error, terminate now, and wake up pending upcalls */
618 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
619 wake_up(&idmap->idmap_wq);
620 goto out;
621 }
622
623 /* Sanity checking of strings */
624 ret = -EINVAL;
625 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
626 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
627 goto out;
628
629 switch (im_in.im_type) {
630 case IDMAP_TYPE_USER:
631 h = &idmap->idmap_user_hash;
632 break;
633 case IDMAP_TYPE_GROUP:
634 h = &idmap->idmap_group_hash;
635 break;
636 default:
637 goto out;
638 }
639
640 switch (im_in.im_conv) {
641 case IDMAP_CONV_IDTONAME:
642 /* Did we match the current upcall? */
643 if (im->im_conv == IDMAP_CONV_IDTONAME
644 && im->im_type == im_in.im_type
645 && im->im_id == im_in.im_id) {
646 /* Yes: copy string, including the terminating '\0' */
647 memcpy(im->im_name, im_in.im_name, namelen_in);
648 im->im_name[namelen_in] = '\0';
649 wake_up(&idmap->idmap_wq);
650 }
651 he = idmap_alloc_id(h, im_in.im_id);
652 break;
653 case IDMAP_CONV_NAMETOID:
654 /* Did we match the current upcall? */
655 if (im->im_conv == IDMAP_CONV_NAMETOID
656 && im->im_type == im_in.im_type
657 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
658 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
659 im->im_id = im_in.im_id;
660 wake_up(&idmap->idmap_wq);
661 }
662 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
663 break;
664 default:
665 goto out;
666 }
667
668 /* If the entry is valid, also copy it to the cache */
669 if (he != NULL)
670 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
671 ret = mlen;
672out:
c9d5128a 673 mutex_unlock(&idmap->idmap_im_lock);
1da177e4
LT
674 return ret;
675}
676
75c96f85 677static void
1da177e4
LT
678idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
679{
680 struct idmap_msg *im = msg->data;
681 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
682
683 if (msg->errno >= 0)
684 return;
c9d5128a 685 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
686 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
687 wake_up(&idmap->idmap_wq);
c9d5128a 688 mutex_unlock(&idmap->idmap_im_lock);
1da177e4
LT
689}
690
691/*
692 * Fowler/Noll/Vo hash
693 * http://www.isthe.com/chongo/tech/comp/fnv/
694 */
695
696#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
697#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
698
699static unsigned int fnvhash32(const void *buf, size_t buflen)
700{
701 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
702 unsigned int hash = FNV_1_32;
703
704 for (p = buf; p < end; p++) {
705 hash *= FNV_P_32;
706 hash ^= (unsigned int)*p;
707 }
708
369af0f1 709 return hash;
1da177e4
LT
710}
711
e4fd72a1 712int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
1da177e4 713{
e4fd72a1 714 struct idmap *idmap = server->nfs_client->cl_idmap;
1da177e4 715
5cf36cfd
TM
716 if (nfs_map_string_to_numeric(name, namelen, uid))
717 return 0;
1da177e4
LT
718 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
719}
720
e4fd72a1 721int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
1da177e4 722{
e4fd72a1 723 struct idmap *idmap = server->nfs_client->cl_idmap;
1da177e4 724
5cf36cfd
TM
725 if (nfs_map_string_to_numeric(name, namelen, uid))
726 return 0;
1da177e4
LT
727 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
728}
729
e4fd72a1 730int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
1da177e4 731{
e4fd72a1 732 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 733 int ret = -EINVAL;
1da177e4 734
b064eca2
TM
735 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
736 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
f0b85168
TM
737 if (ret < 0)
738 ret = nfs_map_numeric_to_string(uid, buf, buflen);
739 return ret;
1da177e4 740}
e4fd72a1 741int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
1da177e4 742{
e4fd72a1 743 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 744 int ret = -EINVAL;
1da177e4 745
b064eca2
TM
746 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
747 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
f0b85168
TM
748 if (ret < 0)
749 ret = nfs_map_numeric_to_string(uid, buf, buflen);
750 return ret;
1da177e4
LT
751}
752
955a857e 753#endif /* CONFIG_NFS_USE_NEW_IDMAPPER */