blob: 2f78f0ce266432c4a04f496e72a35467d203262f [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>
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050042#include <linux/cred.h>
43#include <linux/sunrpc/sched.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs_sb.h>
46#include <linux/keyctl.h>
47#include <linux/key-type.h>
48#include <linux/rcupdate.h>
49#include <linux/err.h>
50#include <keys/user-type.h>
51
52/* include files needed by legacy idmapper */
53#include <linux/module.h>
54#include <linux/mutex.h>
55#include <linux/init.h>
56#include <linux/socket.h>
57#include <linux/in.h>
58#include <linux/sched.h>
59#include <linux/sunrpc/clnt.h>
60#include <linux/workqueue.h>
61#include <linux/sunrpc/rpc_pipe_fs.h>
62#include <linux/nfs_fs.h>
63#include "nfs4_fs.h"
64#include "internal.h"
65
66#define NFS_UINT_MAXLEN 11
67#define IDMAP_HASH_SZ 128
68
69/* Default cache timeout is 10 minutes */
70unsigned int nfs_idmap_cache_timeout = 600 * HZ;
71const struct cred *id_resolver_cache;
72
Trond Myklebust6926afd2012-01-07 13:22:46 -050073
74/**
75 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
76 * @fattr: fully initialised struct nfs_fattr
77 * @owner_name: owner name string cache
78 * @group_name: group name string cache
79 */
80void nfs_fattr_init_names(struct nfs_fattr *fattr,
81 struct nfs4_string *owner_name,
82 struct nfs4_string *group_name)
83{
84 fattr->owner_name = owner_name;
85 fattr->group_name = group_name;
86}
87
88static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
89{
90 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
91 kfree(fattr->owner_name->data);
92}
93
94static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
95{
96 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
97 kfree(fattr->group_name->data);
98}
99
100static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
101{
102 struct nfs4_string *owner = fattr->owner_name;
103 __u32 uid;
104
105 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
106 return false;
107 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
108 fattr->uid = uid;
109 fattr->valid |= NFS_ATTR_FATTR_OWNER;
110 }
111 return true;
112}
113
114static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
115{
116 struct nfs4_string *group = fattr->group_name;
117 __u32 gid;
118
119 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
120 return false;
121 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
122 fattr->gid = gid;
123 fattr->valid |= NFS_ATTR_FATTR_GROUP;
124 }
125 return true;
126}
127
128/**
129 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
130 * @fattr: a fully initialised nfs_fattr structure
131 */
132void nfs_fattr_free_names(struct nfs_fattr *fattr)
133{
134 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
135 nfs_fattr_free_owner_name(fattr);
136 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
137 nfs_fattr_free_group_name(fattr);
138}
139
140/**
141 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
142 * @server: pointer to the filesystem nfs_server structure
143 * @fattr: a fully initialised nfs_fattr structure
144 *
145 * This helper maps the cached NFSv4 owner/group strings in fattr into
146 * their numeric uid/gid equivalents, and then frees the cached strings.
147 */
148void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
149{
150 if (nfs_fattr_map_owner_name(server, fattr))
151 nfs_fattr_free_owner_name(fattr);
152 if (nfs_fattr_map_group_name(server, fattr))
153 nfs_fattr_free_group_name(fattr);
154}
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800155
156static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
157{
158 unsigned long val;
159 char buf[16];
160
161 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
162 return 0;
163 memcpy(buf, name, namelen);
164 buf[namelen] = '\0';
165 if (strict_strtoul(buf, 0, &val) != 0)
166 return 0;
167 *res = val;
168 return 1;
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Trond Myklebustf0b85162011-02-22 15:44:31 -0800171static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
172{
173 return snprintf(buf, buflen, "%u", id);
174}
175
Bryan Schumaker955a8572010-09-29 15:41:49 -0400176struct key_type key_type_id_resolver = {
177 .name = "id_resolver",
178 .instantiate = user_instantiate,
179 .match = user_match,
180 .revoke = user_revoke,
181 .destroy = user_destroy,
182 .describe = user_describe,
183 .read = user_read,
184};
185
Bryan Schumakere6499c62012-01-26 16:54:23 -0500186static int nfs_idmap_init_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400187{
188 struct cred *cred;
189 struct key *keyring;
190 int ret = 0;
191
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500192 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
193 key_type_id_resolver.name);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400194
195 cred = prepare_kernel_cred(NULL);
196 if (!cred)
197 return -ENOMEM;
198
199 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
200 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
201 KEY_USR_VIEW | KEY_USR_READ,
202 KEY_ALLOC_NOT_IN_QUOTA);
203 if (IS_ERR(keyring)) {
204 ret = PTR_ERR(keyring);
205 goto failed_put_cred;
206 }
207
208 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
209 if (ret < 0)
210 goto failed_put_key;
211
212 ret = register_key_type(&key_type_id_resolver);
213 if (ret < 0)
214 goto failed_put_key;
215
216 cred->thread_keyring = keyring;
217 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
218 id_resolver_cache = cred;
219 return 0;
220
221failed_put_key:
222 key_put(keyring);
223failed_put_cred:
224 put_cred(cred);
225 return ret;
226}
227
Bryan Schumakere6499c62012-01-26 16:54:23 -0500228static void nfs_idmap_quit_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400229{
230 key_revoke(id_resolver_cache->thread_keyring);
231 unregister_key_type(&key_type_id_resolver);
232 put_cred(id_resolver_cache);
233}
234
235/*
236 * Assemble the description to pass to request_key()
237 * This function will allocate a new string and update dest to point
238 * at it. The caller is responsible for freeing dest.
239 *
240 * On error 0 is returned. Otherwise, the length of dest is returned.
241 */
242static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
243 const char *type, size_t typelen, char **desc)
244{
245 char *cp;
246 size_t desclen = typelen + namelen + 2;
247
248 *desc = kmalloc(desclen, GFP_KERNEL);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200249 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400250 return -ENOMEM;
251
252 cp = *desc;
253 memcpy(cp, type, typelen);
254 cp += typelen;
255 *cp++ = ':';
256
257 memcpy(cp, name, namelen);
258 cp += namelen;
259 *cp = '\0';
260 return desclen;
261}
262
263static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
264 const char *type, void *data, size_t data_size)
265{
266 const struct cred *saved_cred;
267 struct key *rkey;
268 char *desc;
269 struct user_key_payload *payload;
270 ssize_t ret;
271
272 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
273 if (ret <= 0)
274 goto out;
275
276 saved_cred = override_creds(id_resolver_cache);
277 rkey = request_key(&key_type_id_resolver, desc, "");
278 revert_creds(saved_cred);
279 kfree(desc);
280 if (IS_ERR(rkey)) {
281 ret = PTR_ERR(rkey);
282 goto out;
283 }
284
285 rcu_read_lock();
286 rkey->perm |= KEY_USR_VIEW;
287
288 ret = key_validate(rkey);
289 if (ret < 0)
290 goto out_up;
291
292 payload = rcu_dereference(rkey->payload.data);
293 if (IS_ERR_OR_NULL(payload)) {
294 ret = PTR_ERR(payload);
295 goto out_up;
296 }
297
298 ret = payload->datalen;
299 if (ret > 0 && ret <= data_size)
300 memcpy(data, payload->data, ret);
301 else
302 ret = -EINVAL;
303
304out_up:
305 rcu_read_unlock();
306 key_put(rkey);
307out:
308 return ret;
309}
310
311
312/* ID -> Name */
313static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
314{
315 char id_str[NFS_UINT_MAXLEN];
316 int id_len;
317 ssize_t ret;
318
319 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
320 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
321 if (ret < 0)
322 return -EINVAL;
323 return ret;
324}
325
326/* Name -> ID */
327static int nfs_idmap_lookup_id(const char *name, size_t namelen,
328 const char *type, __u32 *id)
329{
330 char id_str[NFS_UINT_MAXLEN];
331 long id_long;
332 ssize_t data_size;
333 int ret = 0;
334
335 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
336 if (data_size <= 0) {
337 ret = -EINVAL;
338 } else {
339 ret = strict_strtol(id_str, 10, &id_long);
340 *id = (__u32)id_long;
341 }
342 return ret;
343}
344
Bryan Schumakere6499c62012-01-26 16:54:23 -0500345/* idmap classic begins here */
David Howells7d4e2742006-08-22 20:06:07 -0400346static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
347{
348 char *endp;
349 int num = simple_strtol(val, &endp, 0);
350 int jif = num * HZ;
351 if (endp == val || *endp || num < 0 || jif < num)
352 return -EINVAL;
353 *((int *)kp->arg) = jif;
354 return 0;
355}
356
357module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
358 &nfs_idmap_cache_timeout, 0644);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360struct idmap_hashent {
Chuck Lever369af0f2007-12-20 14:54:35 -0500361 unsigned long ih_expires;
362 __u32 ih_id;
Chuck Leverd24aae42007-12-20 14:54:49 -0500363 size_t ih_namelen;
Chuck Lever369af0f2007-12-20 14:54:35 -0500364 char ih_name[IDMAP_NAMESZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365};
366
367struct idmap_hashtable {
Chuck Lever369af0f2007-12-20 14:54:35 -0500368 __u8 h_type;
369 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
372struct idmap {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300373 struct rpc_pipe *idmap_pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500374 wait_queue_head_t idmap_wq;
375 struct idmap_msg idmap_im;
376 struct mutex idmap_lock; /* Serializes upcalls */
377 struct mutex idmap_im_lock; /* Protects the hashtable */
378 struct idmap_hashtable idmap_user_hash;
379 struct idmap_hashtable idmap_group_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381
Chuck Lever369af0f2007-12-20 14:54:35 -0500382static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
383 size_t);
384static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386static unsigned int fnvhash32(const void *, size_t);
387
Trond Myklebustb693ba42009-08-09 15:14:15 -0400388static const struct rpc_pipe_ops idmap_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400389 .upcall = rpc_pipe_generic_upcall,
Chuck Lever369af0f2007-12-20 14:54:35 -0500390 .downcall = idmap_pipe_downcall,
391 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};
393
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400394static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
395{
396 if (pipe->dentry)
397 rpc_unlink(pipe->dentry);
398}
399
400static int __nfs_idmap_register(struct dentry *dir,
401 struct idmap *idmap,
402 struct rpc_pipe *pipe)
403{
404 struct dentry *dentry;
405
406 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
407 if (IS_ERR(dentry))
408 return PTR_ERR(dentry);
409 pipe->dentry = dentry;
410 return 0;
411}
412
413static void nfs_idmap_unregister(struct nfs_client *clp,
414 struct rpc_pipe *pipe)
415{
416 struct net *net = clp->net;
417 struct super_block *pipefs_sb;
418
419 pipefs_sb = rpc_get_sb_net(net);
420 if (pipefs_sb) {
421 __nfs_idmap_unregister(pipe);
422 rpc_put_sb_net(net);
423 }
424}
425
426static int nfs_idmap_register(struct nfs_client *clp,
427 struct idmap *idmap,
428 struct rpc_pipe *pipe)
429{
430 struct net *net = clp->net;
431 struct super_block *pipefs_sb;
432 int err = 0;
433
434 pipefs_sb = rpc_get_sb_net(net);
435 if (pipefs_sb) {
436 if (clp->cl_rpcclient->cl_dentry)
437 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
438 idmap, pipe);
439 rpc_put_sb_net(net);
440 }
441 return err;
442}
443
David Howellsb7162792006-08-22 20:06:09 -0400444int
David Howellsadfa6f92006-08-22 20:06:08 -0400445nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct idmap *idmap;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300448 struct rpc_pipe *pipe;
David Howellsb7162792006-08-22 20:06:09 -0400449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David Howells54ceac42006-08-22 20:06:13 -0400451 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400452
Chuck Lever369af0f2007-12-20 14:54:35 -0500453 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
454 if (idmap == NULL)
455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300457 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
458 if (IS_ERR(pipe)) {
459 error = PTR_ERR(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400463 error = nfs_idmap_register(clp, idmap, pipe);
464 if (error) {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300465 rpc_destroy_pipe_data(pipe);
466 kfree(idmap);
467 return error;
468 }
469 idmap->idmap_pipe = pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500470 mutex_init(&idmap->idmap_lock);
471 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 init_waitqueue_head(&idmap->idmap_wq);
473 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
474 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
475
476 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480void
David Howellsadfa6f92006-08-22 20:06:08 -0400481nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct idmap *idmap = clp->cl_idmap;
484
485 if (!idmap)
486 return;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400487 nfs_idmap_unregister(clp, idmap->idmap_pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300488 rpc_destroy_pipe_data(idmap->idmap_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 clp->cl_idmap = NULL;
490 kfree(idmap);
491}
492
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400493static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
494 struct super_block *sb)
495{
496 int err = 0;
497
498 switch (event) {
499 case RPC_PIPEFS_MOUNT:
500 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
501 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
502 clp->cl_idmap,
503 clp->cl_idmap->idmap_pipe);
504 break;
505 case RPC_PIPEFS_UMOUNT:
506 if (clp->cl_idmap->idmap_pipe) {
507 struct dentry *parent;
508
509 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
510 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
511 /*
512 * Note: This is a dirty hack. SUNRPC hook has been
513 * called already but simple_rmdir() call for the
514 * directory returned with error because of idmap pipe
515 * inside. Thus now we have to remove this directory
516 * here.
517 */
518 if (rpc_rmdir(parent))
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500519 printk(KERN_ERR "NFS: %s: failed to remove "
520 "clnt dir!\n", __func__);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400521 }
522 break;
523 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500524 printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
525 event);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400526 return -ENOTSUPP;
527 }
528 return err;
529}
530
531static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
532 void *ptr)
533{
534 struct super_block *sb = ptr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000535 struct nfs_net *nn = net_generic(sb->s_fs_info, nfs_net_id);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400536 struct nfs_client *clp;
537 int error = 0;
538
539 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000540 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400541 if (clp->rpc_ops != &nfs_v4_clientops)
542 continue;
543 error = __rpc_pipefs_event(clp, event, sb);
544 if (error)
545 break;
546 }
547 spin_unlock(&nfs_client_lock);
548 return error;
549}
550
551#define PIPEFS_NFS_PRIO 1
552
553static struct notifier_block nfs_idmap_block = {
554 .notifier_call = rpc_pipefs_event,
555 .priority = SUNRPC_PIPEFS_NFS_PRIO,
556};
557
558int nfs_idmap_init(void)
559{
Bryan Schumakere6499c62012-01-26 16:54:23 -0500560 int ret;
561 ret = nfs_idmap_init_keyring();
562 if (ret != 0)
563 goto out;
564 ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
565 if (ret != 0)
566 nfs_idmap_quit_keyring();
567out:
568 return ret;
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400569}
570
571void nfs_idmap_quit(void)
572{
573 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
Bryan Schumakere6499c62012-01-26 16:54:23 -0500574 nfs_idmap_quit_keyring();
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*
578 * Helper routines for manipulating the hashtable
579 */
580static inline struct idmap_hashent *
581idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
582{
583 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
584}
585
586static struct idmap_hashent *
587idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
588{
589 struct idmap_hashent *he = idmap_name_hash(h, name, len);
590
591 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
592 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100593 if (time_after(jiffies, he->ih_expires))
594 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return he;
596}
597
598static inline struct idmap_hashent *
599idmap_id_hash(struct idmap_hashtable* h, __u32 id)
600{
601 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
602}
603
604static struct idmap_hashent *
605idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
606{
607 struct idmap_hashent *he = idmap_id_hash(h, id);
608 if (he->ih_id != id || he->ih_namelen == 0)
609 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100610 if (time_after(jiffies, he->ih_expires))
611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return he;
613}
614
615/*
616 * Routines for allocating new entries in the hashtable.
617 * For now, we just have 1 entry per bucket, so it's all
618 * pretty trivial.
619 */
620static inline struct idmap_hashent *
Chuck Leverd24aae42007-12-20 14:54:49 -0500621idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 return idmap_name_hash(h, name, len);
624}
625
626static inline struct idmap_hashent *
627idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
628{
629 return idmap_id_hash(h, id);
630}
631
632static void
633idmap_update_entry(struct idmap_hashent *he, const char *name,
634 size_t namelen, __u32 id)
635{
636 he->ih_id = id;
637 memcpy(he->ih_name, name, namelen);
638 he->ih_name[namelen] = '\0';
639 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100640 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * Name -> ID
645 */
646static int
647nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
648 const char *name, size_t namelen, __u32 *id)
649{
650 struct rpc_pipe_msg msg;
651 struct idmap_msg *im;
652 struct idmap_hashent *he;
653 DECLARE_WAITQUEUE(wq, current);
654 int ret = -EIO;
655
656 im = &idmap->idmap_im;
657
658 /*
659 * String sanity checks
660 * Note that the userland daemon expects NUL terminated strings
661 */
662 for (;;) {
663 if (namelen == 0)
664 return -EINVAL;
665 if (name[namelen-1] != '\0')
666 break;
667 namelen--;
668 }
669 if (namelen >= IDMAP_NAMESZ)
670 return -EINVAL;
671
Ingo Molnarc9d51282006-03-20 13:44:11 -0500672 mutex_lock(&idmap->idmap_lock);
673 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 he = idmap_lookup_name(h, name, namelen);
676 if (he != NULL) {
677 *id = he->ih_id;
678 ret = 0;
679 goto out;
680 }
681
682 memset(im, 0, sizeof(*im));
683 memcpy(im->im_name, name, namelen);
684
685 im->im_type = h->h_type;
686 im->im_conv = IDMAP_CONV_NAMETOID;
687
688 memset(&msg, 0, sizeof(msg));
689 msg.data = im;
690 msg.len = sizeof(*im);
691
692 add_wait_queue(&idmap->idmap_wq, &wq);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300693 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 remove_wait_queue(&idmap->idmap_wq, &wq);
695 goto out;
696 }
697
698 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500699 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700701 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500703 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (im->im_status & IDMAP_STATUS_SUCCESS) {
706 *id = im->im_id;
707 ret = 0;
708 }
709
710 out:
711 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500712 mutex_unlock(&idmap->idmap_im_lock);
713 mutex_unlock(&idmap->idmap_lock);
Chuck Lever369af0f2007-12-20 14:54:35 -0500714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
718 * ID -> Name
719 */
720static int
721nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
722 __u32 id, char *name)
723{
724 struct rpc_pipe_msg msg;
725 struct idmap_msg *im;
726 struct idmap_hashent *he;
727 DECLARE_WAITQUEUE(wq, current);
728 int ret = -EIO;
729 unsigned int len;
730
731 im = &idmap->idmap_im;
732
Ingo Molnarc9d51282006-03-20 13:44:11 -0500733 mutex_lock(&idmap->idmap_lock);
734 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 he = idmap_lookup_id(h, id);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800737 if (he) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 memcpy(name, he->ih_name, he->ih_namelen);
739 ret = he->ih_namelen;
740 goto out;
741 }
742
743 memset(im, 0, sizeof(*im));
744 im->im_type = h->h_type;
745 im->im_conv = IDMAP_CONV_IDTONAME;
746 im->im_id = id;
747
748 memset(&msg, 0, sizeof(msg));
749 msg.data = im;
750 msg.len = sizeof(*im);
751
752 add_wait_queue(&idmap->idmap_wq, &wq);
753
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300754 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 remove_wait_queue(&idmap->idmap_wq, &wq);
756 goto out;
757 }
758
759 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500760 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700762 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500764 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (im->im_status & IDMAP_STATUS_SUCCESS) {
767 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
768 goto out;
769 memcpy(name, im->im_name, len);
770 ret = len;
771 }
772
773 out:
774 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500775 mutex_unlock(&idmap->idmap_im_lock);
776 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return ret;
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static ssize_t
781idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
782{
Chuck Lever369af0f2007-12-20 14:54:35 -0500783 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct idmap *idmap = (struct idmap *)rpci->private;
785 struct idmap_msg im_in, *im = &idmap->idmap_im;
786 struct idmap_hashtable *h;
787 struct idmap_hashent *he = NULL;
Chuck Leverd24aae42007-12-20 14:54:49 -0500788 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int ret;
790
Chuck Lever369af0f2007-12-20 14:54:35 -0500791 if (mlen != sizeof(im_in))
792 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Chuck Lever369af0f2007-12-20 14:54:35 -0500794 if (copy_from_user(&im_in, src, mlen) != 0)
795 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Ingo Molnarc9d51282006-03-20 13:44:11 -0500797 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 ret = mlen;
800 im->im_status = im_in.im_status;
801 /* If we got an error, terminate now, and wake up pending upcalls */
802 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
803 wake_up(&idmap->idmap_wq);
804 goto out;
805 }
806
807 /* Sanity checking of strings */
808 ret = -EINVAL;
809 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
810 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
811 goto out;
812
813 switch (im_in.im_type) {
814 case IDMAP_TYPE_USER:
815 h = &idmap->idmap_user_hash;
816 break;
817 case IDMAP_TYPE_GROUP:
818 h = &idmap->idmap_group_hash;
819 break;
820 default:
821 goto out;
822 }
823
824 switch (im_in.im_conv) {
825 case IDMAP_CONV_IDTONAME:
826 /* Did we match the current upcall? */
827 if (im->im_conv == IDMAP_CONV_IDTONAME
828 && im->im_type == im_in.im_type
829 && im->im_id == im_in.im_id) {
830 /* Yes: copy string, including the terminating '\0' */
831 memcpy(im->im_name, im_in.im_name, namelen_in);
832 im->im_name[namelen_in] = '\0';
833 wake_up(&idmap->idmap_wq);
834 }
835 he = idmap_alloc_id(h, im_in.im_id);
836 break;
837 case IDMAP_CONV_NAMETOID:
838 /* Did we match the current upcall? */
839 if (im->im_conv == IDMAP_CONV_NAMETOID
840 && im->im_type == im_in.im_type
841 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
842 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
843 im->im_id = im_in.im_id;
844 wake_up(&idmap->idmap_wq);
845 }
846 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
847 break;
848 default:
849 goto out;
850 }
851
852 /* If the entry is valid, also copy it to the cache */
853 if (he != NULL)
854 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
855 ret = mlen;
856out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500857 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
859}
860
Adrian Bunk75c96f82005-05-05 16:16:09 -0700861static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
863{
864 struct idmap_msg *im = msg->data;
865 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
866
867 if (msg->errno >= 0)
868 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500869 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
871 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500872 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
876 * Fowler/Noll/Vo hash
877 * http://www.isthe.com/chongo/tech/comp/fnv/
878 */
879
880#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
881#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
882
883static unsigned int fnvhash32(const void *buf, size_t buflen)
884{
885 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
886 unsigned int hash = FNV_1_32;
887
888 for (p = buf; p < end; p++) {
889 hash *= FNV_P_32;
890 hash ^= (unsigned int)*p;
891 }
892
Chuck Lever369af0f2007-12-20 14:54:35 -0500893 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800896int 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 -0700897{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800898 struct idmap *idmap = server->nfs_client->cl_idmap;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500899 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800901 if (nfs_map_string_to_numeric(name, namelen, uid))
902 return 0;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500903 ret = nfs_idmap_lookup_id(name, namelen, "uid", uid);
904 if (ret < 0)
905 ret = nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Bryan Schumakere6499c62012-01-26 16:54:23 -0500909int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800911 struct idmap *idmap = server->nfs_client->cl_idmap;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500912 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Bryan Schumakere6499c62012-01-26 16:54:23 -0500914 if (nfs_map_string_to_numeric(name, namelen, gid))
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800915 return 0;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500916 ret = nfs_idmap_lookup_id(name, namelen, "gid", gid);
917 if (ret < 0)
918 ret = nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, gid);
919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800922int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800924 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800925 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Bryan Schumakere6499c62012-01-26 16:54:23 -0500927 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
928 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
929 if (ret < 0)
930 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
931 }
Trond Myklebustf0b85162011-02-22 15:44:31 -0800932 if (ret < 0)
933 ret = nfs_map_numeric_to_string(uid, buf, buflen);
934 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
Bryan Schumakere6499c62012-01-26 16:54:23 -0500936int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800938 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800939 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Bryan Schumakere6499c62012-01-26 16:54:23 -0500941 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
942 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
943 if (ret < 0)
944 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, gid, buf);
945 }
Trond Myklebustf0b85162011-02-22 15:44:31 -0800946 if (ret < 0)
Bryan Schumakere6499c62012-01-26 16:54:23 -0500947 ret = nfs_map_numeric_to_string(gid, buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800948 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}