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