blob: e0ecd5a7e19a71125dd9c11eb4e3a6d1c03ef46a [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#include <linux/cred.h>
Trond Myklebustb064eca22011-02-22 15:44:32 -0800146#include <linux/sunrpc/sched.h>
147#include <linux/nfs4.h>
148#include <linux/nfs_fs_sb.h>
Bryan Schumaker955a8572010-09-29 15:41:49 -0400149#include <linux/keyctl.h>
150#include <linux/key-type.h>
151#include <linux/rcupdate.h>
Bryan Schumaker955a8572010-09-29 15:41:49 -0400152#include <linux/err.h>
153
154#include <keys/user-type.h>
155
156#define NFS_UINT_MAXLEN 11
157
158const struct cred *id_resolver_cache;
159
160struct key_type key_type_id_resolver = {
161 .name = "id_resolver",
162 .instantiate = user_instantiate,
163 .match = user_match,
164 .revoke = user_revoke,
165 .destroy = user_destroy,
166 .describe = user_describe,
167 .read = user_read,
168};
169
Bryan Schumakere6499c62012-01-26 16:54:23 -0500170static int nfs_idmap_init_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400171{
172 struct cred *cred;
173 struct key *keyring;
174 int ret = 0;
175
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500176 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
177 key_type_id_resolver.name);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400178
179 cred = prepare_kernel_cred(NULL);
180 if (!cred)
181 return -ENOMEM;
182
183 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
184 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
185 KEY_USR_VIEW | KEY_USR_READ,
186 KEY_ALLOC_NOT_IN_QUOTA);
187 if (IS_ERR(keyring)) {
188 ret = PTR_ERR(keyring);
189 goto failed_put_cred;
190 }
191
192 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
193 if (ret < 0)
194 goto failed_put_key;
195
196 ret = register_key_type(&key_type_id_resolver);
197 if (ret < 0)
198 goto failed_put_key;
199
200 cred->thread_keyring = keyring;
201 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
202 id_resolver_cache = cred;
203 return 0;
204
205failed_put_key:
206 key_put(keyring);
207failed_put_cred:
208 put_cred(cred);
209 return ret;
210}
211
Bryan Schumakere6499c62012-01-26 16:54:23 -0500212static void nfs_idmap_quit_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400213{
214 key_revoke(id_resolver_cache->thread_keyring);
215 unregister_key_type(&key_type_id_resolver);
216 put_cred(id_resolver_cache);
217}
218
219/*
220 * Assemble the description to pass to request_key()
221 * This function will allocate a new string and update dest to point
222 * at it. The caller is responsible for freeing dest.
223 *
224 * On error 0 is returned. Otherwise, the length of dest is returned.
225 */
226static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
227 const char *type, size_t typelen, char **desc)
228{
229 char *cp;
230 size_t desclen = typelen + namelen + 2;
231
232 *desc = kmalloc(desclen, GFP_KERNEL);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200233 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400234 return -ENOMEM;
235
236 cp = *desc;
237 memcpy(cp, type, typelen);
238 cp += typelen;
239 *cp++ = ':';
240
241 memcpy(cp, name, namelen);
242 cp += namelen;
243 *cp = '\0';
244 return desclen;
245}
246
247static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
248 const char *type, void *data, size_t data_size)
249{
250 const struct cred *saved_cred;
251 struct key *rkey;
252 char *desc;
253 struct user_key_payload *payload;
254 ssize_t ret;
255
256 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
257 if (ret <= 0)
258 goto out;
259
260 saved_cred = override_creds(id_resolver_cache);
261 rkey = request_key(&key_type_id_resolver, desc, "");
262 revert_creds(saved_cred);
263 kfree(desc);
264 if (IS_ERR(rkey)) {
265 ret = PTR_ERR(rkey);
266 goto out;
267 }
268
269 rcu_read_lock();
270 rkey->perm |= KEY_USR_VIEW;
271
272 ret = key_validate(rkey);
273 if (ret < 0)
274 goto out_up;
275
276 payload = rcu_dereference(rkey->payload.data);
277 if (IS_ERR_OR_NULL(payload)) {
278 ret = PTR_ERR(payload);
279 goto out_up;
280 }
281
282 ret = payload->datalen;
283 if (ret > 0 && ret <= data_size)
284 memcpy(data, payload->data, ret);
285 else
286 ret = -EINVAL;
287
288out_up:
289 rcu_read_unlock();
290 key_put(rkey);
291out:
292 return ret;
293}
294
295
296/* ID -> Name */
297static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
298{
299 char id_str[NFS_UINT_MAXLEN];
300 int id_len;
301 ssize_t ret;
302
303 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
304 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
305 if (ret < 0)
306 return -EINVAL;
307 return ret;
308}
309
310/* Name -> ID */
311static int nfs_idmap_lookup_id(const char *name, size_t namelen,
312 const char *type, __u32 *id)
313{
314 char id_str[NFS_UINT_MAXLEN];
315 long id_long;
316 ssize_t data_size;
317 int ret = 0;
318
319 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
320 if (data_size <= 0) {
321 ret = -EINVAL;
322 } else {
323 ret = strict_strtol(id_str, 10, &id_long);
324 *id = (__u32)id_long;
325 }
326 return ret;
327}
328
Bryan Schumakere6499c62012-01-26 16:54:23 -0500329/* idmap classic begins here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#include <linux/module.h>
Ingo Molnarc9d51282006-03-20 13:44:11 -0500331#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#include <linux/socket.h>
334#include <linux/in.h>
335#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#include <linux/sunrpc/clnt.h>
337#include <linux/workqueue.h>
338#include <linux/sunrpc/rpc_pipe_fs.h>
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#include <linux/nfs_fs.h>
341
Trond Myklebust4ce79712005-06-22 17:16:21 +0000342#include "nfs4_fs.h"
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400343#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#define IDMAP_HASH_SZ 128
346
Trond Myklebust58df0952006-01-03 09:55:57 +0100347/* Default cache timeout is 10 minutes */
348unsigned int nfs_idmap_cache_timeout = 600 * HZ;
349
David Howells7d4e2742006-08-22 20:06:07 -0400350static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
351{
352 char *endp;
353 int num = simple_strtol(val, &endp, 0);
354 int jif = num * HZ;
355 if (endp == val || *endp || num < 0 || jif < num)
356 return -EINVAL;
357 *((int *)kp->arg) = jif;
358 return 0;
359}
360
361module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
362 &nfs_idmap_cache_timeout, 0644);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364struct idmap_hashent {
Chuck Lever369af0f2007-12-20 14:54:35 -0500365 unsigned long ih_expires;
366 __u32 ih_id;
Chuck Leverd24aae42007-12-20 14:54:49 -0500367 size_t ih_namelen;
Chuck Lever369af0f2007-12-20 14:54:35 -0500368 char ih_name[IDMAP_NAMESZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371struct idmap_hashtable {
Chuck Lever369af0f2007-12-20 14:54:35 -0500372 __u8 h_type;
373 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
376struct idmap {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300377 struct rpc_pipe *idmap_pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500378 wait_queue_head_t idmap_wq;
379 struct idmap_msg idmap_im;
380 struct mutex idmap_lock; /* Serializes upcalls */
381 struct mutex idmap_im_lock; /* Protects the hashtable */
382 struct idmap_hashtable idmap_user_hash;
383 struct idmap_hashtable idmap_group_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384};
385
Chuck Lever369af0f2007-12-20 14:54:35 -0500386static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
387 size_t);
388static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390static unsigned int fnvhash32(const void *, size_t);
391
Trond Myklebustb693ba42009-08-09 15:14:15 -0400392static const struct rpc_pipe_ops idmap_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400393 .upcall = rpc_pipe_generic_upcall,
Chuck Lever369af0f2007-12-20 14:54:35 -0500394 .downcall = idmap_pipe_downcall,
395 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400398static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
399{
400 if (pipe->dentry)
401 rpc_unlink(pipe->dentry);
402}
403
404static int __nfs_idmap_register(struct dentry *dir,
405 struct idmap *idmap,
406 struct rpc_pipe *pipe)
407{
408 struct dentry *dentry;
409
410 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
411 if (IS_ERR(dentry))
412 return PTR_ERR(dentry);
413 pipe->dentry = dentry;
414 return 0;
415}
416
417static void nfs_idmap_unregister(struct nfs_client *clp,
418 struct rpc_pipe *pipe)
419{
420 struct net *net = clp->net;
421 struct super_block *pipefs_sb;
422
423 pipefs_sb = rpc_get_sb_net(net);
424 if (pipefs_sb) {
425 __nfs_idmap_unregister(pipe);
426 rpc_put_sb_net(net);
427 }
428}
429
430static int nfs_idmap_register(struct nfs_client *clp,
431 struct idmap *idmap,
432 struct rpc_pipe *pipe)
433{
434 struct net *net = clp->net;
435 struct super_block *pipefs_sb;
436 int err = 0;
437
438 pipefs_sb = rpc_get_sb_net(net);
439 if (pipefs_sb) {
440 if (clp->cl_rpcclient->cl_dentry)
441 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
442 idmap, pipe);
443 rpc_put_sb_net(net);
444 }
445 return err;
446}
447
David Howellsb7162792006-08-22 20:06:09 -0400448int
David Howellsadfa6f92006-08-22 20:06:08 -0400449nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct idmap *idmap;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300452 struct rpc_pipe *pipe;
David Howellsb7162792006-08-22 20:06:09 -0400453 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David Howells54ceac42006-08-22 20:06:13 -0400455 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400456
Chuck Lever369af0f2007-12-20 14:54:35 -0500457 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
458 if (idmap == NULL)
459 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300461 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
462 if (IS_ERR(pipe)) {
463 error = PTR_ERR(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400465 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400467 error = nfs_idmap_register(clp, idmap, pipe);
468 if (error) {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300469 rpc_destroy_pipe_data(pipe);
470 kfree(idmap);
471 return error;
472 }
473 idmap->idmap_pipe = pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500474 mutex_init(&idmap->idmap_lock);
475 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 init_waitqueue_head(&idmap->idmap_wq);
477 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
478 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
479
480 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484void
David Howellsadfa6f92006-08-22 20:06:08 -0400485nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct idmap *idmap = clp->cl_idmap;
488
489 if (!idmap)
490 return;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400491 nfs_idmap_unregister(clp, idmap->idmap_pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300492 rpc_destroy_pipe_data(idmap->idmap_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 clp->cl_idmap = NULL;
494 kfree(idmap);
495}
496
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400497static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
498 struct super_block *sb)
499{
500 int err = 0;
501
502 switch (event) {
503 case RPC_PIPEFS_MOUNT:
504 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
505 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
506 clp->cl_idmap,
507 clp->cl_idmap->idmap_pipe);
508 break;
509 case RPC_PIPEFS_UMOUNT:
510 if (clp->cl_idmap->idmap_pipe) {
511 struct dentry *parent;
512
513 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
514 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
515 /*
516 * Note: This is a dirty hack. SUNRPC hook has been
517 * called already but simple_rmdir() call for the
518 * directory returned with error because of idmap pipe
519 * inside. Thus now we have to remove this directory
520 * here.
521 */
522 if (rpc_rmdir(parent))
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500523 printk(KERN_ERR "NFS: %s: failed to remove "
524 "clnt dir!\n", __func__);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400525 }
526 break;
527 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500528 printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
529 event);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400530 return -ENOTSUPP;
531 }
532 return err;
533}
534
535static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
536 void *ptr)
537{
538 struct super_block *sb = ptr;
539 struct nfs_client *clp;
540 int error = 0;
541
542 spin_lock(&nfs_client_lock);
543 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
544 if (clp->net != sb->s_fs_info)
545 continue;
546 if (clp->rpc_ops != &nfs_v4_clientops)
547 continue;
548 error = __rpc_pipefs_event(clp, event, sb);
549 if (error)
550 break;
551 }
552 spin_unlock(&nfs_client_lock);
553 return error;
554}
555
556#define PIPEFS_NFS_PRIO 1
557
558static struct notifier_block nfs_idmap_block = {
559 .notifier_call = rpc_pipefs_event,
560 .priority = SUNRPC_PIPEFS_NFS_PRIO,
561};
562
563int nfs_idmap_init(void)
564{
Bryan Schumakere6499c62012-01-26 16:54:23 -0500565 int ret;
566 ret = nfs_idmap_init_keyring();
567 if (ret != 0)
568 goto out;
569 ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
570 if (ret != 0)
571 nfs_idmap_quit_keyring();
572out:
573 return ret;
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400574}
575
576void nfs_idmap_quit(void)
577{
578 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
Bryan Schumakere6499c62012-01-26 16:54:23 -0500579 nfs_idmap_quit_keyring();
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * Helper routines for manipulating the hashtable
584 */
585static inline struct idmap_hashent *
586idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
587{
588 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
589}
590
591static struct idmap_hashent *
592idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
593{
594 struct idmap_hashent *he = idmap_name_hash(h, name, len);
595
596 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
597 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100598 if (time_after(jiffies, he->ih_expires))
599 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return he;
601}
602
603static inline struct idmap_hashent *
604idmap_id_hash(struct idmap_hashtable* h, __u32 id)
605{
606 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
607}
608
609static struct idmap_hashent *
610idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
611{
612 struct idmap_hashent *he = idmap_id_hash(h, id);
613 if (he->ih_id != id || he->ih_namelen == 0)
614 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100615 if (time_after(jiffies, he->ih_expires))
616 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return he;
618}
619
620/*
621 * Routines for allocating new entries in the hashtable.
622 * For now, we just have 1 entry per bucket, so it's all
623 * pretty trivial.
624 */
625static inline struct idmap_hashent *
Chuck Leverd24aae42007-12-20 14:54:49 -0500626idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 return idmap_name_hash(h, name, len);
629}
630
631static inline struct idmap_hashent *
632idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
633{
634 return idmap_id_hash(h, id);
635}
636
637static void
638idmap_update_entry(struct idmap_hashent *he, const char *name,
639 size_t namelen, __u32 id)
640{
641 he->ih_id = id;
642 memcpy(he->ih_name, name, namelen);
643 he->ih_name[namelen] = '\0';
644 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100645 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Name -> ID
650 */
651static int
652nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
653 const char *name, size_t namelen, __u32 *id)
654{
655 struct rpc_pipe_msg msg;
656 struct idmap_msg *im;
657 struct idmap_hashent *he;
658 DECLARE_WAITQUEUE(wq, current);
659 int ret = -EIO;
660
661 im = &idmap->idmap_im;
662
663 /*
664 * String sanity checks
665 * Note that the userland daemon expects NUL terminated strings
666 */
667 for (;;) {
668 if (namelen == 0)
669 return -EINVAL;
670 if (name[namelen-1] != '\0')
671 break;
672 namelen--;
673 }
674 if (namelen >= IDMAP_NAMESZ)
675 return -EINVAL;
676
Ingo Molnarc9d51282006-03-20 13:44:11 -0500677 mutex_lock(&idmap->idmap_lock);
678 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 he = idmap_lookup_name(h, name, namelen);
681 if (he != NULL) {
682 *id = he->ih_id;
683 ret = 0;
684 goto out;
685 }
686
687 memset(im, 0, sizeof(*im));
688 memcpy(im->im_name, name, namelen);
689
690 im->im_type = h->h_type;
691 im->im_conv = IDMAP_CONV_NAMETOID;
692
693 memset(&msg, 0, sizeof(msg));
694 msg.data = im;
695 msg.len = sizeof(*im);
696
697 add_wait_queue(&idmap->idmap_wq, &wq);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300698 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 remove_wait_queue(&idmap->idmap_wq, &wq);
700 goto out;
701 }
702
703 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500704 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700706 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500708 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (im->im_status & IDMAP_STATUS_SUCCESS) {
711 *id = im->im_id;
712 ret = 0;
713 }
714
715 out:
716 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500717 mutex_unlock(&idmap->idmap_im_lock);
718 mutex_unlock(&idmap->idmap_lock);
Chuck Lever369af0f2007-12-20 14:54:35 -0500719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722/*
723 * ID -> Name
724 */
725static int
726nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
727 __u32 id, char *name)
728{
729 struct rpc_pipe_msg msg;
730 struct idmap_msg *im;
731 struct idmap_hashent *he;
732 DECLARE_WAITQUEUE(wq, current);
733 int ret = -EIO;
734 unsigned int len;
735
736 im = &idmap->idmap_im;
737
Ingo Molnarc9d51282006-03-20 13:44:11 -0500738 mutex_lock(&idmap->idmap_lock);
739 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 he = idmap_lookup_id(h, id);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800742 if (he) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 memcpy(name, he->ih_name, he->ih_namelen);
744 ret = he->ih_namelen;
745 goto out;
746 }
747
748 memset(im, 0, sizeof(*im));
749 im->im_type = h->h_type;
750 im->im_conv = IDMAP_CONV_IDTONAME;
751 im->im_id = id;
752
753 memset(&msg, 0, sizeof(msg));
754 msg.data = im;
755 msg.len = sizeof(*im);
756
757 add_wait_queue(&idmap->idmap_wq, &wq);
758
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300759 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 remove_wait_queue(&idmap->idmap_wq, &wq);
761 goto out;
762 }
763
764 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500765 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700767 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500769 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (im->im_status & IDMAP_STATUS_SUCCESS) {
772 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
773 goto out;
774 memcpy(name, im->im_name, len);
775 ret = len;
776 }
777
778 out:
779 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500780 mutex_unlock(&idmap->idmap_im_lock);
781 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return ret;
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static ssize_t
786idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
787{
Chuck Lever369af0f2007-12-20 14:54:35 -0500788 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct idmap *idmap = (struct idmap *)rpci->private;
790 struct idmap_msg im_in, *im = &idmap->idmap_im;
791 struct idmap_hashtable *h;
792 struct idmap_hashent *he = NULL;
Chuck Leverd24aae42007-12-20 14:54:49 -0500793 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int ret;
795
Chuck Lever369af0f2007-12-20 14:54:35 -0500796 if (mlen != sizeof(im_in))
797 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Chuck Lever369af0f2007-12-20 14:54:35 -0500799 if (copy_from_user(&im_in, src, mlen) != 0)
800 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Ingo Molnarc9d51282006-03-20 13:44:11 -0500802 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 ret = mlen;
805 im->im_status = im_in.im_status;
806 /* If we got an error, terminate now, and wake up pending upcalls */
807 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
808 wake_up(&idmap->idmap_wq);
809 goto out;
810 }
811
812 /* Sanity checking of strings */
813 ret = -EINVAL;
814 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
815 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
816 goto out;
817
818 switch (im_in.im_type) {
819 case IDMAP_TYPE_USER:
820 h = &idmap->idmap_user_hash;
821 break;
822 case IDMAP_TYPE_GROUP:
823 h = &idmap->idmap_group_hash;
824 break;
825 default:
826 goto out;
827 }
828
829 switch (im_in.im_conv) {
830 case IDMAP_CONV_IDTONAME:
831 /* Did we match the current upcall? */
832 if (im->im_conv == IDMAP_CONV_IDTONAME
833 && im->im_type == im_in.im_type
834 && im->im_id == im_in.im_id) {
835 /* Yes: copy string, including the terminating '\0' */
836 memcpy(im->im_name, im_in.im_name, namelen_in);
837 im->im_name[namelen_in] = '\0';
838 wake_up(&idmap->idmap_wq);
839 }
840 he = idmap_alloc_id(h, im_in.im_id);
841 break;
842 case IDMAP_CONV_NAMETOID:
843 /* Did we match the current upcall? */
844 if (im->im_conv == IDMAP_CONV_NAMETOID
845 && im->im_type == im_in.im_type
846 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
847 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
848 im->im_id = im_in.im_id;
849 wake_up(&idmap->idmap_wq);
850 }
851 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
852 break;
853 default:
854 goto out;
855 }
856
857 /* If the entry is valid, also copy it to the cache */
858 if (he != NULL)
859 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
860 ret = mlen;
861out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500862 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864}
865
Adrian Bunk75c96f82005-05-05 16:16:09 -0700866static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
868{
869 struct idmap_msg *im = msg->data;
870 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
871
872 if (msg->errno >= 0)
873 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500874 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
876 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500877 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880/*
881 * Fowler/Noll/Vo hash
882 * http://www.isthe.com/chongo/tech/comp/fnv/
883 */
884
885#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
886#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
887
888static unsigned int fnvhash32(const void *buf, size_t buflen)
889{
890 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
891 unsigned int hash = FNV_1_32;
892
893 for (p = buf; p < end; p++) {
894 hash *= FNV_P_32;
895 hash ^= (unsigned int)*p;
896 }
897
Chuck Lever369af0f2007-12-20 14:54:35 -0500898 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800901int 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 -0700902{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800903 struct idmap *idmap = server->nfs_client->cl_idmap;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500904 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800906 if (nfs_map_string_to_numeric(name, namelen, uid))
907 return 0;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500908 ret = nfs_idmap_lookup_id(name, namelen, "uid", uid);
909 if (ret < 0)
910 ret = nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Bryan Schumakere6499c62012-01-26 16:54:23 -0500914int 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 -0700915{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800916 struct idmap *idmap = server->nfs_client->cl_idmap;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500917 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Bryan Schumakere6499c62012-01-26 16:54:23 -0500919 if (nfs_map_string_to_numeric(name, namelen, gid))
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800920 return 0;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500921 ret = nfs_idmap_lookup_id(name, namelen, "gid", gid);
922 if (ret < 0)
923 ret = nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, gid);
924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800927int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800929 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800930 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Bryan Schumakere6499c62012-01-26 16:54:23 -0500932 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
933 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
934 if (ret < 0)
935 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
936 }
Trond Myklebustf0b85162011-02-22 15:44:31 -0800937 if (ret < 0)
938 ret = nfs_map_numeric_to_string(uid, buf, buflen);
939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
Bryan Schumakere6499c62012-01-26 16:54:23 -0500941int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800943 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800944 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Bryan Schumakere6499c62012-01-26 16:54:23 -0500946 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
947 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
948 if (ret < 0)
949 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, gid, buf);
950 }
Trond Myklebustf0b85162011-02-22 15:44:31 -0800951 if (ret < 0)
Bryan Schumakere6499c62012-01-26 16:54:23 -0500952 ret = nfs_map_numeric_to_string(gid, buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}