blob: 2c05f1991e1e1c3664c8ffb9f0159687b53b8449 [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>
Vitaliy Ivanove44ba032011-06-20 16:08:07 +020039#include <linux/slab.h>
40#include <linux/nfs_idmap.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050041#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}
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800124
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Trond Myklebustf0b85162011-02-22 15:44:31 -0800140static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
141{
142 return snprintf(buf, buflen, "%u", id);
143}
144
Bryan Schumaker955a8572010-09-29 15:41:49 -0400145#ifdef CONFIG_NFS_USE_NEW_IDMAPPER
146
Bryan Schumaker955a8572010-09-29 15:41:49 -0400147#include <linux/cred.h>
Trond Myklebustb064eca22011-02-22 15:44:32 -0800148#include <linux/sunrpc/sched.h>
149#include <linux/nfs4.h>
150#include <linux/nfs_fs_sb.h>
Bryan Schumaker955a8572010-09-29 15:41:49 -0400151#include <linux/keyctl.h>
152#include <linux/key-type.h>
153#include <linux/rcupdate.h>
Bryan Schumaker955a8572010-09-29 15:41:49 -0400154#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);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200234 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400235 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
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800330int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400331{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800332 if (nfs_map_string_to_numeric(name, namelen, uid))
333 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400334 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
335}
336
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800337int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400338{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800339 if (nfs_map_string_to_numeric(name, namelen, gid))
340 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400341 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
342}
343
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800344int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400345{
Trond Myklebustb064eca22011-02-22 15:44:32 -0800346 int ret = -EINVAL;
347
348 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
349 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800350 if (ret < 0)
351 ret = nfs_map_numeric_to_string(uid, buf, buflen);
352 return ret;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400353}
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800354int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400355{
Trond Myklebustb064eca22011-02-22 15:44:32 -0800356 int ret = -EINVAL;
Trond Myklebustf0b85162011-02-22 15:44:31 -0800357
Trond Myklebustb064eca22011-02-22 15:44:32 -0800358 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
359 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800360 if (ret < 0)
361 ret = nfs_map_numeric_to_string(gid, buf, buflen);
362 return ret;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400363}
364
J. Bruce Fields5f3e97c2010-12-21 23:49:34 +0000365#else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
Bryan Schumaker955a8572010-09-29 15:41:49 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#include <linux/module.h>
Ingo Molnarc9d51282006-03-20 13:44:11 -0500368#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#include <linux/socket.h>
371#include <linux/in.h>
372#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#include <linux/sunrpc/clnt.h>
374#include <linux/workqueue.h>
375#include <linux/sunrpc/rpc_pipe_fs.h>
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#include <linux/nfs_fs.h>
378
Trond Myklebust4ce79712005-06-22 17:16:21 +0000379#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381#define IDMAP_HASH_SZ 128
382
Trond Myklebust58df0952006-01-03 09:55:57 +0100383/* Default cache timeout is 10 minutes */
384unsigned int nfs_idmap_cache_timeout = 600 * HZ;
385
David Howells7d4e2742006-08-22 20:06:07 -0400386static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400struct idmap_hashent {
Chuck Lever369af0f2007-12-20 14:54:35 -0500401 unsigned long ih_expires;
402 __u32 ih_id;
Chuck Leverd24aae42007-12-20 14:54:49 -0500403 size_t ih_namelen;
Chuck Lever369af0f2007-12-20 14:54:35 -0500404 char ih_name[IDMAP_NAMESZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405};
406
407struct idmap_hashtable {
Chuck Lever369af0f2007-12-20 14:54:35 -0500408 __u8 h_type;
409 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410};
411
412struct idmap {
Chuck Lever369af0f2007-12-20 14:54:35 -0500413 struct dentry *idmap_dentry;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Chuck Lever369af0f2007-12-20 14:54:35 -0500422static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
423 size_t);
424static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426static unsigned int fnvhash32(const void *, size_t);
427
Trond Myklebustb693ba42009-08-09 15:14:15 -0400428static const struct rpc_pipe_ops idmap_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400429 .upcall = rpc_pipe_generic_upcall,
Chuck Lever369af0f2007-12-20 14:54:35 -0500430 .downcall = idmap_pipe_downcall,
431 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432};
433
David Howellsb7162792006-08-22 20:06:09 -0400434int
David Howellsadfa6f92006-08-22 20:06:08 -0400435nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct idmap *idmap;
David Howellsb7162792006-08-22 20:06:09 -0400438 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David Howells54ceac42006-08-22 20:06:13 -0400440 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400441
Chuck Lever369af0f2007-12-20 14:54:35 -0500442 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
443 if (idmap == NULL)
444 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Trond Myklebust7d217ca2009-08-09 15:14:24 -0400446 idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry,
447 "idmap", idmap, &idmap_upcall_ops, 0);
Chuck Lever369af0f2007-12-20 14:54:35 -0500448 if (IS_ERR(idmap->idmap_dentry)) {
David Howellsb7162792006-08-22 20:06:09 -0400449 error = PTR_ERR(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400451 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Chuck Lever369af0f2007-12-20 14:54:35 -0500454 mutex_init(&idmap->idmap_lock);
455 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 init_waitqueue_head(&idmap->idmap_wq);
457 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
458 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
459
460 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464void
David Howellsadfa6f92006-08-22 20:06:08 -0400465nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct idmap *idmap = clp->cl_idmap;
468
469 if (!idmap)
470 return;
Trond Myklebust5d674762006-07-31 14:11:48 -0700471 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 clp->cl_idmap = NULL;
473 kfree(idmap);
474}
475
476/*
477 * Helper routines for manipulating the hashtable
478 */
479static inline struct idmap_hashent *
480idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
481{
482 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
483}
484
485static struct idmap_hashent *
486idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
487{
488 struct idmap_hashent *he = idmap_name_hash(h, name, len);
489
490 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
491 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100492 if (time_after(jiffies, he->ih_expires))
493 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return he;
495}
496
497static inline struct idmap_hashent *
498idmap_id_hash(struct idmap_hashtable* h, __u32 id)
499{
500 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
501}
502
503static struct idmap_hashent *
504idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
505{
506 struct idmap_hashent *he = idmap_id_hash(h, id);
507 if (he->ih_id != id || he->ih_namelen == 0)
508 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100509 if (time_after(jiffies, he->ih_expires))
510 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return he;
512}
513
514/*
515 * Routines for allocating new entries in the hashtable.
516 * For now, we just have 1 entry per bucket, so it's all
517 * pretty trivial.
518 */
519static inline struct idmap_hashent *
Chuck Leverd24aae42007-12-20 14:54:49 -0500520idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 return idmap_name_hash(h, name, len);
523}
524
525static inline struct idmap_hashent *
526idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
527{
528 return idmap_id_hash(h, id);
529}
530
531static void
532idmap_update_entry(struct idmap_hashent *he, const char *name,
533 size_t namelen, __u32 id)
534{
535 he->ih_id = id;
536 memcpy(he->ih_name, name, namelen);
537 he->ih_name[namelen] = '\0';
538 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100539 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/*
543 * Name -> ID
544 */
545static int
546nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
547 const char *name, size_t namelen, __u32 *id)
548{
549 struct rpc_pipe_msg msg;
550 struct idmap_msg *im;
551 struct idmap_hashent *he;
552 DECLARE_WAITQUEUE(wq, current);
553 int ret = -EIO;
554
555 im = &idmap->idmap_im;
556
557 /*
558 * String sanity checks
559 * Note that the userland daemon expects NUL terminated strings
560 */
561 for (;;) {
562 if (namelen == 0)
563 return -EINVAL;
564 if (name[namelen-1] != '\0')
565 break;
566 namelen--;
567 }
568 if (namelen >= IDMAP_NAMESZ)
569 return -EINVAL;
570
Ingo Molnarc9d51282006-03-20 13:44:11 -0500571 mutex_lock(&idmap->idmap_lock);
572 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 he = idmap_lookup_name(h, name, namelen);
575 if (he != NULL) {
576 *id = he->ih_id;
577 ret = 0;
578 goto out;
579 }
580
581 memset(im, 0, sizeof(*im));
582 memcpy(im->im_name, name, namelen);
583
584 im->im_type = h->h_type;
585 im->im_conv = IDMAP_CONV_NAMETOID;
586
587 memset(&msg, 0, sizeof(msg));
588 msg.data = im;
589 msg.len = sizeof(*im);
590
591 add_wait_queue(&idmap->idmap_wq, &wq);
592 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
593 remove_wait_queue(&idmap->idmap_wq, &wq);
594 goto out;
595 }
596
597 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500598 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700600 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500602 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (im->im_status & IDMAP_STATUS_SUCCESS) {
605 *id = im->im_id;
606 ret = 0;
607 }
608
609 out:
610 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500611 mutex_unlock(&idmap->idmap_im_lock);
612 mutex_unlock(&idmap->idmap_lock);
Chuck Lever369af0f2007-12-20 14:54:35 -0500613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * ID -> Name
618 */
619static int
620nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
621 __u32 id, char *name)
622{
623 struct rpc_pipe_msg msg;
624 struct idmap_msg *im;
625 struct idmap_hashent *he;
626 DECLARE_WAITQUEUE(wq, current);
627 int ret = -EIO;
628 unsigned int len;
629
630 im = &idmap->idmap_im;
631
Ingo Molnarc9d51282006-03-20 13:44:11 -0500632 mutex_lock(&idmap->idmap_lock);
633 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 he = idmap_lookup_id(h, id);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800636 if (he) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 memcpy(name, he->ih_name, he->ih_namelen);
638 ret = he->ih_namelen;
639 goto out;
640 }
641
642 memset(im, 0, sizeof(*im));
643 im->im_type = h->h_type;
644 im->im_conv = IDMAP_CONV_IDTONAME;
645 im->im_id = id;
646
647 memset(&msg, 0, sizeof(msg));
648 msg.data = im;
649 msg.len = sizeof(*im);
650
651 add_wait_queue(&idmap->idmap_wq, &wq);
652
653 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
654 remove_wait_queue(&idmap->idmap_wq, &wq);
655 goto out;
656 }
657
658 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500659 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700661 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500663 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (im->im_status & IDMAP_STATUS_SUCCESS) {
666 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
667 goto out;
668 memcpy(name, im->im_name, len);
669 ret = len;
670 }
671
672 out:
673 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500674 mutex_unlock(&idmap->idmap_im_lock);
675 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return ret;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static ssize_t
680idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
681{
Chuck Lever369af0f2007-12-20 14:54:35 -0500682 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct idmap *idmap = (struct idmap *)rpci->private;
684 struct idmap_msg im_in, *im = &idmap->idmap_im;
685 struct idmap_hashtable *h;
686 struct idmap_hashent *he = NULL;
Chuck Leverd24aae42007-12-20 14:54:49 -0500687 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 int ret;
689
Chuck Lever369af0f2007-12-20 14:54:35 -0500690 if (mlen != sizeof(im_in))
691 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Chuck Lever369af0f2007-12-20 14:54:35 -0500693 if (copy_from_user(&im_in, src, mlen) != 0)
694 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Ingo Molnarc9d51282006-03-20 13:44:11 -0500696 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 ret = mlen;
699 im->im_status = im_in.im_status;
700 /* If we got an error, terminate now, and wake up pending upcalls */
701 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
702 wake_up(&idmap->idmap_wq);
703 goto out;
704 }
705
706 /* Sanity checking of strings */
707 ret = -EINVAL;
708 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
709 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
710 goto out;
711
712 switch (im_in.im_type) {
713 case IDMAP_TYPE_USER:
714 h = &idmap->idmap_user_hash;
715 break;
716 case IDMAP_TYPE_GROUP:
717 h = &idmap->idmap_group_hash;
718 break;
719 default:
720 goto out;
721 }
722
723 switch (im_in.im_conv) {
724 case IDMAP_CONV_IDTONAME:
725 /* Did we match the current upcall? */
726 if (im->im_conv == IDMAP_CONV_IDTONAME
727 && im->im_type == im_in.im_type
728 && im->im_id == im_in.im_id) {
729 /* Yes: copy string, including the terminating '\0' */
730 memcpy(im->im_name, im_in.im_name, namelen_in);
731 im->im_name[namelen_in] = '\0';
732 wake_up(&idmap->idmap_wq);
733 }
734 he = idmap_alloc_id(h, im_in.im_id);
735 break;
736 case IDMAP_CONV_NAMETOID:
737 /* Did we match the current upcall? */
738 if (im->im_conv == IDMAP_CONV_NAMETOID
739 && im->im_type == im_in.im_type
740 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
741 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
742 im->im_id = im_in.im_id;
743 wake_up(&idmap->idmap_wq);
744 }
745 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
746 break;
747 default:
748 goto out;
749 }
750
751 /* If the entry is valid, also copy it to the cache */
752 if (he != NULL)
753 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
754 ret = mlen;
755out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500756 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return ret;
758}
759
Adrian Bunk75c96f82005-05-05 16:16:09 -0700760static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
762{
763 struct idmap_msg *im = msg->data;
764 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
765
766 if (msg->errno >= 0)
767 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500768 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
770 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500771 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Fowler/Noll/Vo hash
776 * http://www.isthe.com/chongo/tech/comp/fnv/
777 */
778
779#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
780#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
781
782static unsigned int fnvhash32(const void *buf, size_t buflen)
783{
784 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
785 unsigned int hash = FNV_1_32;
786
787 for (p = buf; p < end; p++) {
788 hash *= FNV_P_32;
789 hash ^= (unsigned int)*p;
790 }
791
Chuck Lever369af0f2007-12-20 14:54:35 -0500792 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800795int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800797 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800799 if (nfs_map_string_to_numeric(name, namelen, uid))
800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
802}
803
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800804int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800806 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800808 if (nfs_map_string_to_numeric(name, namelen, uid))
809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
811}
812
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800813int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800815 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800816 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Trond Myklebustb064eca22011-02-22 15:44:32 -0800818 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
819 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800820 if (ret < 0)
821 ret = nfs_map_numeric_to_string(uid, buf, buflen);
822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800824int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800826 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800827 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Trond Myklebustb064eca22011-02-22 15:44:32 -0800829 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
830 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800831 if (ret < 0)
832 ret = nfs_map_numeric_to_string(uid, buf, buflen);
833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Bryan Schumaker955a8572010-09-29 15:41:49 -0400836#endif /* CONFIG_NFS_USE_NEW_IDMAPPER */