blob: cbe6e2fa8cefc922a423c26126144f10931dfb89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Trond Myklebust5cf36cf2011-02-22 15:44:31 -080036#include <linux/types.h>
37#include <linux/string.h>
38#include <linux/kernel.h>
39
40static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Bryan Schumaker955a8572010-09-29 15:41:49 -040055#ifdef CONFIG_NFS_USE_NEW_IDMAPPER
56
57#include <linux/slab.h>
58#include <linux/cred.h>
59#include <linux/nfs_idmap.h>
60#include <linux/keyctl.h>
61#include <linux/key-type.h>
62#include <linux/rcupdate.h>
Bryan Schumaker955a8572010-09-29 15:41:49 -040063#include <linux/err.h>
64
65#include <keys/user-type.h>
66
67#define NFS_UINT_MAXLEN 11
68
69const struct cred *id_resolver_cache;
70
71struct key_type key_type_id_resolver = {
72 .name = "id_resolver",
73 .instantiate = user_instantiate,
74 .match = user_match,
75 .revoke = user_revoke,
76 .destroy = user_destroy,
77 .describe = user_describe,
78 .read = user_read,
79};
80
81int nfs_idmap_init(void)
82{
83 struct cred *cred;
84 struct key *keyring;
85 int ret = 0;
86
87 printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name);
88
89 cred = prepare_kernel_cred(NULL);
90 if (!cred)
91 return -ENOMEM;
92
93 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
94 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
95 KEY_USR_VIEW | KEY_USR_READ,
96 KEY_ALLOC_NOT_IN_QUOTA);
97 if (IS_ERR(keyring)) {
98 ret = PTR_ERR(keyring);
99 goto failed_put_cred;
100 }
101
102 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
103 if (ret < 0)
104 goto failed_put_key;
105
106 ret = register_key_type(&key_type_id_resolver);
107 if (ret < 0)
108 goto failed_put_key;
109
110 cred->thread_keyring = keyring;
111 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
112 id_resolver_cache = cred;
113 return 0;
114
115failed_put_key:
116 key_put(keyring);
117failed_put_cred:
118 put_cred(cred);
119 return ret;
120}
121
122void nfs_idmap_quit(void)
123{
124 key_revoke(id_resolver_cache->thread_keyring);
125 unregister_key_type(&key_type_id_resolver);
126 put_cred(id_resolver_cache);
127}
128
129/*
130 * Assemble the description to pass to request_key()
131 * This function will allocate a new string and update dest to point
132 * at it. The caller is responsible for freeing dest.
133 *
134 * On error 0 is returned. Otherwise, the length of dest is returned.
135 */
136static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
137 const char *type, size_t typelen, char **desc)
138{
139 char *cp;
140 size_t desclen = typelen + namelen + 2;
141
142 *desc = kmalloc(desclen, GFP_KERNEL);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200143 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400144 return -ENOMEM;
145
146 cp = *desc;
147 memcpy(cp, type, typelen);
148 cp += typelen;
149 *cp++ = ':';
150
151 memcpy(cp, name, namelen);
152 cp += namelen;
153 *cp = '\0';
154 return desclen;
155}
156
157static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
158 const char *type, void *data, size_t data_size)
159{
160 const struct cred *saved_cred;
161 struct key *rkey;
162 char *desc;
163 struct user_key_payload *payload;
164 ssize_t ret;
165
166 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
167 if (ret <= 0)
168 goto out;
169
170 saved_cred = override_creds(id_resolver_cache);
171 rkey = request_key(&key_type_id_resolver, desc, "");
172 revert_creds(saved_cred);
173 kfree(desc);
174 if (IS_ERR(rkey)) {
175 ret = PTR_ERR(rkey);
176 goto out;
177 }
178
179 rcu_read_lock();
180 rkey->perm |= KEY_USR_VIEW;
181
182 ret = key_validate(rkey);
183 if (ret < 0)
184 goto out_up;
185
186 payload = rcu_dereference(rkey->payload.data);
187 if (IS_ERR_OR_NULL(payload)) {
188 ret = PTR_ERR(payload);
189 goto out_up;
190 }
191
192 ret = payload->datalen;
193 if (ret > 0 && ret <= data_size)
194 memcpy(data, payload->data, ret);
195 else
196 ret = -EINVAL;
197
198out_up:
199 rcu_read_unlock();
200 key_put(rkey);
201out:
202 return ret;
203}
204
205
206/* ID -> Name */
207static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
208{
209 char id_str[NFS_UINT_MAXLEN];
210 int id_len;
211 ssize_t ret;
212
213 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
214 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
215 if (ret < 0)
216 return -EINVAL;
217 return ret;
218}
219
220/* Name -> ID */
221static int nfs_idmap_lookup_id(const char *name, size_t namelen,
222 const char *type, __u32 *id)
223{
224 char id_str[NFS_UINT_MAXLEN];
225 long id_long;
226 ssize_t data_size;
227 int ret = 0;
228
229 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
230 if (data_size <= 0) {
231 ret = -EINVAL;
232 } else {
233 ret = strict_strtol(id_str, 10, &id_long);
234 *id = (__u32)id_long;
235 }
236 return ret;
237}
238
239int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
240{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800241 if (nfs_map_string_to_numeric(name, namelen, uid))
242 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400243 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
244}
245
246int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *gid)
247{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800248 if (nfs_map_string_to_numeric(name, namelen, gid))
249 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400250 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
251}
252
253int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
254{
255 return nfs_idmap_lookup_name(uid, "user", buf, buflen);
256}
257int nfs_map_gid_to_group(struct nfs_client *clp, __u32 gid, char *buf, size_t buflen)
258{
259 return nfs_idmap_lookup_name(gid, "group", buf, buflen);
260}
261
J. Bruce Fields5f3e97c2010-12-21 23:49:34 +0000262#else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
Bryan Schumaker955a8572010-09-29 15:41:49 -0400263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#include <linux/module.h>
Ingo Molnarc9d51282006-03-20 13:44:11 -0500265#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#include <linux/slab.h>
268#include <linux/socket.h>
269#include <linux/in.h>
270#include <linux/sched.h>
271
272#include <linux/sunrpc/clnt.h>
273#include <linux/workqueue.h>
274#include <linux/sunrpc/rpc_pipe_fs.h>
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#include <linux/nfs_fs.h>
277
278#include <linux/nfs_idmap.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +0000279#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281#define IDMAP_HASH_SZ 128
282
Trond Myklebust58df0952006-01-03 09:55:57 +0100283/* Default cache timeout is 10 minutes */
284unsigned int nfs_idmap_cache_timeout = 600 * HZ;
285
David Howells7d4e2742006-08-22 20:06:07 -0400286static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
287{
288 char *endp;
289 int num = simple_strtol(val, &endp, 0);
290 int jif = num * HZ;
291 if (endp == val || *endp || num < 0 || jif < num)
292 return -EINVAL;
293 *((int *)kp->arg) = jif;
294 return 0;
295}
296
297module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
298 &nfs_idmap_cache_timeout, 0644);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300struct idmap_hashent {
Chuck Lever369af0f2007-12-20 14:54:35 -0500301 unsigned long ih_expires;
302 __u32 ih_id;
Chuck Leverd24aae42007-12-20 14:54:49 -0500303 size_t ih_namelen;
Chuck Lever369af0f2007-12-20 14:54:35 -0500304 char ih_name[IDMAP_NAMESZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
307struct idmap_hashtable {
Chuck Lever369af0f2007-12-20 14:54:35 -0500308 __u8 h_type;
309 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312struct idmap {
Chuck Lever369af0f2007-12-20 14:54:35 -0500313 struct dentry *idmap_dentry;
314 wait_queue_head_t idmap_wq;
315 struct idmap_msg idmap_im;
316 struct mutex idmap_lock; /* Serializes upcalls */
317 struct mutex idmap_im_lock; /* Protects the hashtable */
318 struct idmap_hashtable idmap_user_hash;
319 struct idmap_hashtable idmap_group_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Chuck Lever369af0f2007-12-20 14:54:35 -0500322static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
323 char __user *, size_t);
324static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
325 size_t);
326static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328static unsigned int fnvhash32(const void *, size_t);
329
Trond Myklebustb693ba42009-08-09 15:14:15 -0400330static const struct rpc_pipe_ops idmap_upcall_ops = {
Chuck Lever369af0f2007-12-20 14:54:35 -0500331 .upcall = idmap_pipe_upcall,
332 .downcall = idmap_pipe_downcall,
333 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
David Howellsb7162792006-08-22 20:06:09 -0400336int
David Howellsadfa6f92006-08-22 20:06:08 -0400337nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct idmap *idmap;
David Howellsb7162792006-08-22 20:06:09 -0400340 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Howells54ceac42006-08-22 20:06:13 -0400342 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400343
Chuck Lever369af0f2007-12-20 14:54:35 -0500344 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
345 if (idmap == NULL)
346 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Trond Myklebust7d217ca2009-08-09 15:14:24 -0400348 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
349 "idmap", idmap, &idmap_upcall_ops, 0);
Chuck Lever369af0f2007-12-20 14:54:35 -0500350 if (IS_ERR(idmap->idmap_dentry)) {
David Howellsb7162792006-08-22 20:06:09 -0400351 error = PTR_ERR(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400353 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
Chuck Lever369af0f2007-12-20 14:54:35 -0500356 mutex_init(&idmap->idmap_lock);
357 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 init_waitqueue_head(&idmap->idmap_wq);
359 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
360 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
361
362 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366void
David Howellsadfa6f92006-08-22 20:06:08 -0400367nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct idmap *idmap = clp->cl_idmap;
370
371 if (!idmap)
372 return;
Trond Myklebust5d674762006-07-31 14:11:48 -0700373 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 clp->cl_idmap = NULL;
375 kfree(idmap);
376}
377
378/*
379 * Helper routines for manipulating the hashtable
380 */
381static inline struct idmap_hashent *
382idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
383{
384 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
385}
386
387static struct idmap_hashent *
388idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
389{
390 struct idmap_hashent *he = idmap_name_hash(h, name, len);
391
392 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
393 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100394 if (time_after(jiffies, he->ih_expires))
395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return he;
397}
398
399static inline struct idmap_hashent *
400idmap_id_hash(struct idmap_hashtable* h, __u32 id)
401{
402 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
403}
404
405static struct idmap_hashent *
406idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
407{
408 struct idmap_hashent *he = idmap_id_hash(h, id);
409 if (he->ih_id != id || he->ih_namelen == 0)
410 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100411 if (time_after(jiffies, he->ih_expires))
412 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return he;
414}
415
416/*
417 * Routines for allocating new entries in the hashtable.
418 * For now, we just have 1 entry per bucket, so it's all
419 * pretty trivial.
420 */
421static inline struct idmap_hashent *
Chuck Leverd24aae42007-12-20 14:54:49 -0500422idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 return idmap_name_hash(h, name, len);
425}
426
427static inline struct idmap_hashent *
428idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
429{
430 return idmap_id_hash(h, id);
431}
432
433static void
434idmap_update_entry(struct idmap_hashent *he, const char *name,
435 size_t namelen, __u32 id)
436{
437 he->ih_id = id;
438 memcpy(he->ih_name, name, namelen);
439 he->ih_name[namelen] = '\0';
440 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100441 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/*
445 * Name -> ID
446 */
447static int
448nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
449 const char *name, size_t namelen, __u32 *id)
450{
451 struct rpc_pipe_msg msg;
452 struct idmap_msg *im;
453 struct idmap_hashent *he;
454 DECLARE_WAITQUEUE(wq, current);
455 int ret = -EIO;
456
457 im = &idmap->idmap_im;
458
459 /*
460 * String sanity checks
461 * Note that the userland daemon expects NUL terminated strings
462 */
463 for (;;) {
464 if (namelen == 0)
465 return -EINVAL;
466 if (name[namelen-1] != '\0')
467 break;
468 namelen--;
469 }
470 if (namelen >= IDMAP_NAMESZ)
471 return -EINVAL;
472
Ingo Molnarc9d51282006-03-20 13:44:11 -0500473 mutex_lock(&idmap->idmap_lock);
474 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 he = idmap_lookup_name(h, name, namelen);
477 if (he != NULL) {
478 *id = he->ih_id;
479 ret = 0;
480 goto out;
481 }
482
483 memset(im, 0, sizeof(*im));
484 memcpy(im->im_name, name, namelen);
485
486 im->im_type = h->h_type;
487 im->im_conv = IDMAP_CONV_NAMETOID;
488
489 memset(&msg, 0, sizeof(msg));
490 msg.data = im;
491 msg.len = sizeof(*im);
492
493 add_wait_queue(&idmap->idmap_wq, &wq);
494 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
495 remove_wait_queue(&idmap->idmap_wq, &wq);
496 goto out;
497 }
498
499 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500500 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700502 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500504 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (im->im_status & IDMAP_STATUS_SUCCESS) {
507 *id = im->im_id;
508 ret = 0;
509 }
510
511 out:
512 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500513 mutex_unlock(&idmap->idmap_im_lock);
514 mutex_unlock(&idmap->idmap_lock);
Chuck Lever369af0f2007-12-20 14:54:35 -0500515 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * ID -> Name
520 */
521static int
522nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
523 __u32 id, char *name)
524{
525 struct rpc_pipe_msg msg;
526 struct idmap_msg *im;
527 struct idmap_hashent *he;
528 DECLARE_WAITQUEUE(wq, current);
529 int ret = -EIO;
530 unsigned int len;
531
532 im = &idmap->idmap_im;
533
Ingo Molnarc9d51282006-03-20 13:44:11 -0500534 mutex_lock(&idmap->idmap_lock);
535 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 he = idmap_lookup_id(h, id);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800538 if (he) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 memcpy(name, he->ih_name, he->ih_namelen);
540 ret = he->ih_namelen;
541 goto out;
542 }
543
544 memset(im, 0, sizeof(*im));
545 im->im_type = h->h_type;
546 im->im_conv = IDMAP_CONV_IDTONAME;
547 im->im_id = id;
548
549 memset(&msg, 0, sizeof(msg));
550 msg.data = im;
551 msg.len = sizeof(*im);
552
553 add_wait_queue(&idmap->idmap_wq, &wq);
554
555 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
556 remove_wait_queue(&idmap->idmap_wq, &wq);
557 goto out;
558 }
559
560 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500561 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700563 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500565 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (im->im_status & IDMAP_STATUS_SUCCESS) {
568 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
569 goto out;
570 memcpy(name, im->im_name, len);
571 ret = len;
572 }
573
574 out:
575 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500576 mutex_unlock(&idmap->idmap_im_lock);
577 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return ret;
579}
580
581/* RPC pipefs upcall/downcall routines */
582static ssize_t
583idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
Chuck Lever369af0f2007-12-20 14:54:35 -0500584 char __user *dst, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Chuck Lever369af0f2007-12-20 14:54:35 -0500586 char *data = (char *)msg->data + msg->copied;
Chuck Levera661b772007-12-20 14:54:42 -0500587 size_t mlen = min(msg->len, buflen);
588 unsigned long left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Chuck Lever369af0f2007-12-20 14:54:35 -0500590 left = copy_to_user(dst, data, mlen);
Chuck Levera661b772007-12-20 14:54:42 -0500591 if (left == mlen) {
592 msg->errno = -EFAULT;
593 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Chuck Levera661b772007-12-20 14:54:42 -0500595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 mlen -= left;
597 msg->copied += mlen;
598 msg->errno = 0;
Chuck Lever369af0f2007-12-20 14:54:35 -0500599 return mlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static ssize_t
603idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
604{
Chuck Lever369af0f2007-12-20 14:54:35 -0500605 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct idmap *idmap = (struct idmap *)rpci->private;
607 struct idmap_msg im_in, *im = &idmap->idmap_im;
608 struct idmap_hashtable *h;
609 struct idmap_hashent *he = NULL;
Chuck Leverd24aae42007-12-20 14:54:49 -0500610 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int ret;
612
Chuck Lever369af0f2007-12-20 14:54:35 -0500613 if (mlen != sizeof(im_in))
614 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Chuck Lever369af0f2007-12-20 14:54:35 -0500616 if (copy_from_user(&im_in, src, mlen) != 0)
617 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Ingo Molnarc9d51282006-03-20 13:44:11 -0500619 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 ret = mlen;
622 im->im_status = im_in.im_status;
623 /* If we got an error, terminate now, and wake up pending upcalls */
624 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
625 wake_up(&idmap->idmap_wq);
626 goto out;
627 }
628
629 /* Sanity checking of strings */
630 ret = -EINVAL;
631 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
632 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
633 goto out;
634
635 switch (im_in.im_type) {
636 case IDMAP_TYPE_USER:
637 h = &idmap->idmap_user_hash;
638 break;
639 case IDMAP_TYPE_GROUP:
640 h = &idmap->idmap_group_hash;
641 break;
642 default:
643 goto out;
644 }
645
646 switch (im_in.im_conv) {
647 case IDMAP_CONV_IDTONAME:
648 /* Did we match the current upcall? */
649 if (im->im_conv == IDMAP_CONV_IDTONAME
650 && im->im_type == im_in.im_type
651 && im->im_id == im_in.im_id) {
652 /* Yes: copy string, including the terminating '\0' */
653 memcpy(im->im_name, im_in.im_name, namelen_in);
654 im->im_name[namelen_in] = '\0';
655 wake_up(&idmap->idmap_wq);
656 }
657 he = idmap_alloc_id(h, im_in.im_id);
658 break;
659 case IDMAP_CONV_NAMETOID:
660 /* Did we match the current upcall? */
661 if (im->im_conv == IDMAP_CONV_NAMETOID
662 && im->im_type == im_in.im_type
663 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
664 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
665 im->im_id = im_in.im_id;
666 wake_up(&idmap->idmap_wq);
667 }
668 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
669 break;
670 default:
671 goto out;
672 }
673
674 /* If the entry is valid, also copy it to the cache */
675 if (he != NULL)
676 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
677 ret = mlen;
678out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500679 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return ret;
681}
682
Adrian Bunk75c96f82005-05-05 16:16:09 -0700683static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
685{
686 struct idmap_msg *im = msg->data;
687 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
688
689 if (msg->errno >= 0)
690 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500691 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
693 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500694 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*
698 * Fowler/Noll/Vo hash
699 * http://www.isthe.com/chongo/tech/comp/fnv/
700 */
701
702#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
703#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
704
705static unsigned int fnvhash32(const void *buf, size_t buflen)
706{
707 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
708 unsigned int hash = FNV_1_32;
709
710 for (p = buf; p < end; p++) {
711 hash *= FNV_P_32;
712 hash ^= (unsigned int)*p;
713 }
714
Chuck Lever369af0f2007-12-20 14:54:35 -0500715 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
David Howellsadfa6f92006-08-22 20:06:08 -0400718int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct idmap *idmap = clp->cl_idmap;
721
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800722 if (nfs_map_string_to_numeric(name, namelen, uid))
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
725}
726
David Howellsadfa6f92006-08-22 20:06:08 -0400727int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct idmap *idmap = clp->cl_idmap;
730
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800731 if (nfs_map_string_to_numeric(name, namelen, uid))
732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
734}
735
Bryan Schumaker955a8572010-09-29 15:41:49 -0400736int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct idmap *idmap = clp->cl_idmap;
739
740 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
741}
Bryan Schumaker955a8572010-09-29 15:41:49 -0400742int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct idmap *idmap = clp->cl_idmap;
745
746 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
747}
748
Bryan Schumaker955a8572010-09-29 15:41:49 -0400749#endif /* CONFIG_NFS_USE_NEW_IDMAPPER */