blob: 91b1e2a82146b5a8aa5f221d42179820d3d8720f [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
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500178 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
179 key_type_id_resolver.name);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400180
181 cred = prepare_kernel_cred(NULL);
182 if (!cred)
183 return -ENOMEM;
184
185 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
186 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
187 KEY_USR_VIEW | KEY_USR_READ,
188 KEY_ALLOC_NOT_IN_QUOTA);
189 if (IS_ERR(keyring)) {
190 ret = PTR_ERR(keyring);
191 goto failed_put_cred;
192 }
193
194 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
195 if (ret < 0)
196 goto failed_put_key;
197
198 ret = register_key_type(&key_type_id_resolver);
199 if (ret < 0)
200 goto failed_put_key;
201
202 cred->thread_keyring = keyring;
203 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
204 id_resolver_cache = cred;
205 return 0;
206
207failed_put_key:
208 key_put(keyring);
209failed_put_cred:
210 put_cred(cred);
211 return ret;
212}
213
214void nfs_idmap_quit(void)
215{
216 key_revoke(id_resolver_cache->thread_keyring);
217 unregister_key_type(&key_type_id_resolver);
218 put_cred(id_resolver_cache);
219}
220
221/*
222 * Assemble the description to pass to request_key()
223 * This function will allocate a new string and update dest to point
224 * at it. The caller is responsible for freeing dest.
225 *
226 * On error 0 is returned. Otherwise, the length of dest is returned.
227 */
228static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
229 const char *type, size_t typelen, char **desc)
230{
231 char *cp;
232 size_t desclen = typelen + namelen + 2;
233
234 *desc = kmalloc(desclen, GFP_KERNEL);
Dan Carpenter8f0d97b2010-10-28 08:05:57 +0200235 if (!*desc)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400236 return -ENOMEM;
237
238 cp = *desc;
239 memcpy(cp, type, typelen);
240 cp += typelen;
241 *cp++ = ':';
242
243 memcpy(cp, name, namelen);
244 cp += namelen;
245 *cp = '\0';
246 return desclen;
247}
248
249static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
250 const char *type, void *data, size_t data_size)
251{
252 const struct cred *saved_cred;
253 struct key *rkey;
254 char *desc;
255 struct user_key_payload *payload;
256 ssize_t ret;
257
258 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
259 if (ret <= 0)
260 goto out;
261
262 saved_cred = override_creds(id_resolver_cache);
263 rkey = request_key(&key_type_id_resolver, desc, "");
264 revert_creds(saved_cred);
265 kfree(desc);
266 if (IS_ERR(rkey)) {
267 ret = PTR_ERR(rkey);
268 goto out;
269 }
270
271 rcu_read_lock();
272 rkey->perm |= KEY_USR_VIEW;
273
274 ret = key_validate(rkey);
275 if (ret < 0)
276 goto out_up;
277
278 payload = rcu_dereference(rkey->payload.data);
279 if (IS_ERR_OR_NULL(payload)) {
280 ret = PTR_ERR(payload);
281 goto out_up;
282 }
283
284 ret = payload->datalen;
285 if (ret > 0 && ret <= data_size)
286 memcpy(data, payload->data, ret);
287 else
288 ret = -EINVAL;
289
290out_up:
291 rcu_read_unlock();
292 key_put(rkey);
293out:
294 return ret;
295}
296
297
298/* ID -> Name */
299static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
300{
301 char id_str[NFS_UINT_MAXLEN];
302 int id_len;
303 ssize_t ret;
304
305 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
306 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
307 if (ret < 0)
308 return -EINVAL;
309 return ret;
310}
311
312/* Name -> ID */
313static int nfs_idmap_lookup_id(const char *name, size_t namelen,
314 const char *type, __u32 *id)
315{
316 char id_str[NFS_UINT_MAXLEN];
317 long id_long;
318 ssize_t data_size;
319 int ret = 0;
320
321 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
322 if (data_size <= 0) {
323 ret = -EINVAL;
324 } else {
325 ret = strict_strtol(id_str, 10, &id_long);
326 *id = (__u32)id_long;
327 }
328 return ret;
329}
330
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800331int 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 -0400332{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800333 if (nfs_map_string_to_numeric(name, namelen, uid))
334 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400335 return nfs_idmap_lookup_id(name, namelen, "uid", uid);
336}
337
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800338int 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 -0400339{
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800340 if (nfs_map_string_to_numeric(name, namelen, gid))
341 return 0;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400342 return nfs_idmap_lookup_id(name, namelen, "gid", gid);
343}
344
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800345int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400346{
Trond Myklebustb064eca22011-02-22 15:44:32 -0800347 int ret = -EINVAL;
348
349 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
350 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800351 if (ret < 0)
352 ret = nfs_map_numeric_to_string(uid, buf, buflen);
353 return ret;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400354}
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800355int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400356{
Trond Myklebustb064eca22011-02-22 15:44:32 -0800357 int ret = -EINVAL;
Trond Myklebustf0b85162011-02-22 15:44:31 -0800358
Trond Myklebustb064eca22011-02-22 15:44:32 -0800359 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
360 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800361 if (ret < 0)
362 ret = nfs_map_numeric_to_string(gid, buf, buflen);
363 return ret;
Bryan Schumaker955a8572010-09-29 15:41:49 -0400364}
365
J. Bruce Fields5f3e97c2010-12-21 23:49:34 +0000366#else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */
Bryan Schumaker955a8572010-09-29 15:41:49 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#include <linux/module.h>
Ingo Molnarc9d51282006-03-20 13:44:11 -0500369#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#include <linux/socket.h>
372#include <linux/in.h>
373#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#include <linux/sunrpc/clnt.h>
375#include <linux/workqueue.h>
376#include <linux/sunrpc/rpc_pipe_fs.h>
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#include <linux/nfs_fs.h>
379
Trond Myklebust4ce79712005-06-22 17:16:21 +0000380#include "nfs4_fs.h"
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400381#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383#define IDMAP_HASH_SZ 128
384
Trond Myklebust58df0952006-01-03 09:55:57 +0100385/* Default cache timeout is 10 minutes */
386unsigned int nfs_idmap_cache_timeout = 600 * HZ;
387
David Howells7d4e2742006-08-22 20:06:07 -0400388static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
389{
390 char *endp;
391 int num = simple_strtol(val, &endp, 0);
392 int jif = num * HZ;
393 if (endp == val || *endp || num < 0 || jif < num)
394 return -EINVAL;
395 *((int *)kp->arg) = jif;
396 return 0;
397}
398
399module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
400 &nfs_idmap_cache_timeout, 0644);
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402struct idmap_hashent {
Chuck Lever369af0f2007-12-20 14:54:35 -0500403 unsigned long ih_expires;
404 __u32 ih_id;
Chuck Leverd24aae42007-12-20 14:54:49 -0500405 size_t ih_namelen;
Chuck Lever369af0f2007-12-20 14:54:35 -0500406 char ih_name[IDMAP_NAMESZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
409struct idmap_hashtable {
Chuck Lever369af0f2007-12-20 14:54:35 -0500410 __u8 h_type;
411 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
414struct idmap {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300415 struct rpc_pipe *idmap_pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500416 wait_queue_head_t idmap_wq;
417 struct idmap_msg idmap_im;
418 struct mutex idmap_lock; /* Serializes upcalls */
419 struct mutex idmap_im_lock; /* Protects the hashtable */
420 struct idmap_hashtable idmap_user_hash;
421 struct idmap_hashtable idmap_group_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Chuck Lever369af0f2007-12-20 14:54:35 -0500424static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
425 size_t);
426static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428static unsigned int fnvhash32(const void *, size_t);
429
Trond Myklebustb693ba42009-08-09 15:14:15 -0400430static const struct rpc_pipe_ops idmap_upcall_ops = {
Peng Taoc1225152011-09-22 21:50:10 -0400431 .upcall = rpc_pipe_generic_upcall,
Chuck Lever369af0f2007-12-20 14:54:35 -0500432 .downcall = idmap_pipe_downcall,
433 .destroy_msg = idmap_pipe_destroy_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400436static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
437{
438 if (pipe->dentry)
439 rpc_unlink(pipe->dentry);
440}
441
442static int __nfs_idmap_register(struct dentry *dir,
443 struct idmap *idmap,
444 struct rpc_pipe *pipe)
445{
446 struct dentry *dentry;
447
448 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
449 if (IS_ERR(dentry))
450 return PTR_ERR(dentry);
451 pipe->dentry = dentry;
452 return 0;
453}
454
455static void nfs_idmap_unregister(struct nfs_client *clp,
456 struct rpc_pipe *pipe)
457{
458 struct net *net = clp->net;
459 struct super_block *pipefs_sb;
460
461 pipefs_sb = rpc_get_sb_net(net);
462 if (pipefs_sb) {
463 __nfs_idmap_unregister(pipe);
464 rpc_put_sb_net(net);
465 }
466}
467
468static int nfs_idmap_register(struct nfs_client *clp,
469 struct idmap *idmap,
470 struct rpc_pipe *pipe)
471{
472 struct net *net = clp->net;
473 struct super_block *pipefs_sb;
474 int err = 0;
475
476 pipefs_sb = rpc_get_sb_net(net);
477 if (pipefs_sb) {
478 if (clp->cl_rpcclient->cl_dentry)
479 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
480 idmap, pipe);
481 rpc_put_sb_net(net);
482 }
483 return err;
484}
485
David Howellsb7162792006-08-22 20:06:09 -0400486int
David Howellsadfa6f92006-08-22 20:06:08 -0400487nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct idmap *idmap;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300490 struct rpc_pipe *pipe;
David Howellsb7162792006-08-22 20:06:09 -0400491 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Howells54ceac42006-08-22 20:06:13 -0400493 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400494
Chuck Lever369af0f2007-12-20 14:54:35 -0500495 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
496 if (idmap == NULL)
497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300499 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
500 if (IS_ERR(pipe)) {
501 error = PTR_ERR(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400503 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400505 error = nfs_idmap_register(clp, idmap, pipe);
506 if (error) {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300507 rpc_destroy_pipe_data(pipe);
508 kfree(idmap);
509 return error;
510 }
511 idmap->idmap_pipe = pipe;
Chuck Lever369af0f2007-12-20 14:54:35 -0500512 mutex_init(&idmap->idmap_lock);
513 mutex_init(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 init_waitqueue_head(&idmap->idmap_wq);
515 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
516 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
517
518 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522void
David Howellsadfa6f92006-08-22 20:06:08 -0400523nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct idmap *idmap = clp->cl_idmap;
526
527 if (!idmap)
528 return;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400529 nfs_idmap_unregister(clp, idmap->idmap_pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300530 rpc_destroy_pipe_data(idmap->idmap_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 clp->cl_idmap = NULL;
532 kfree(idmap);
533}
534
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400535static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
536 struct super_block *sb)
537{
538 int err = 0;
539
540 switch (event) {
541 case RPC_PIPEFS_MOUNT:
542 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
543 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
544 clp->cl_idmap,
545 clp->cl_idmap->idmap_pipe);
546 break;
547 case RPC_PIPEFS_UMOUNT:
548 if (clp->cl_idmap->idmap_pipe) {
549 struct dentry *parent;
550
551 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
552 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
553 /*
554 * Note: This is a dirty hack. SUNRPC hook has been
555 * called already but simple_rmdir() call for the
556 * directory returned with error because of idmap pipe
557 * inside. Thus now we have to remove this directory
558 * here.
559 */
560 if (rpc_rmdir(parent))
561 printk(KERN_ERR "%s: failed to remove clnt dir!\n", __func__);
562 }
563 break;
564 default:
565 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
566 return -ENOTSUPP;
567 }
568 return err;
569}
570
571static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
572 void *ptr)
573{
574 struct super_block *sb = ptr;
575 struct nfs_client *clp;
576 int error = 0;
577
578 spin_lock(&nfs_client_lock);
579 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
580 if (clp->net != sb->s_fs_info)
581 continue;
582 if (clp->rpc_ops != &nfs_v4_clientops)
583 continue;
584 error = __rpc_pipefs_event(clp, event, sb);
585 if (error)
586 break;
587 }
588 spin_unlock(&nfs_client_lock);
589 return error;
590}
591
592#define PIPEFS_NFS_PRIO 1
593
594static struct notifier_block nfs_idmap_block = {
595 .notifier_call = rpc_pipefs_event,
596 .priority = SUNRPC_PIPEFS_NFS_PRIO,
597};
598
599int nfs_idmap_init(void)
600{
601 return rpc_pipefs_notifier_register(&nfs_idmap_block);
602}
603
604void nfs_idmap_quit(void)
605{
606 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * Helper routines for manipulating the hashtable
611 */
612static inline struct idmap_hashent *
613idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
614{
615 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
616}
617
618static struct idmap_hashent *
619idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
620{
621 struct idmap_hashent *he = idmap_name_hash(h, name, len);
622
623 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
624 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100625 if (time_after(jiffies, he->ih_expires))
626 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return he;
628}
629
630static inline struct idmap_hashent *
631idmap_id_hash(struct idmap_hashtable* h, __u32 id)
632{
633 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
634}
635
636static struct idmap_hashent *
637idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
638{
639 struct idmap_hashent *he = idmap_id_hash(h, id);
640 if (he->ih_id != id || he->ih_namelen == 0)
641 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100642 if (time_after(jiffies, he->ih_expires))
643 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return he;
645}
646
647/*
648 * Routines for allocating new entries in the hashtable.
649 * For now, we just have 1 entry per bucket, so it's all
650 * pretty trivial.
651 */
652static inline struct idmap_hashent *
Chuck Leverd24aae42007-12-20 14:54:49 -0500653idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 return idmap_name_hash(h, name, len);
656}
657
658static inline struct idmap_hashent *
659idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
660{
661 return idmap_id_hash(h, id);
662}
663
664static void
665idmap_update_entry(struct idmap_hashent *he, const char *name,
666 size_t namelen, __u32 id)
667{
668 he->ih_id = id;
669 memcpy(he->ih_name, name, namelen);
670 he->ih_name[namelen] = '\0';
671 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100672 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Name -> ID
677 */
678static int
679nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
680 const char *name, size_t namelen, __u32 *id)
681{
682 struct rpc_pipe_msg msg;
683 struct idmap_msg *im;
684 struct idmap_hashent *he;
685 DECLARE_WAITQUEUE(wq, current);
686 int ret = -EIO;
687
688 im = &idmap->idmap_im;
689
690 /*
691 * String sanity checks
692 * Note that the userland daemon expects NUL terminated strings
693 */
694 for (;;) {
695 if (namelen == 0)
696 return -EINVAL;
697 if (name[namelen-1] != '\0')
698 break;
699 namelen--;
700 }
701 if (namelen >= IDMAP_NAMESZ)
702 return -EINVAL;
703
Ingo Molnarc9d51282006-03-20 13:44:11 -0500704 mutex_lock(&idmap->idmap_lock);
705 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 he = idmap_lookup_name(h, name, namelen);
708 if (he != NULL) {
709 *id = he->ih_id;
710 ret = 0;
711 goto out;
712 }
713
714 memset(im, 0, sizeof(*im));
715 memcpy(im->im_name, name, namelen);
716
717 im->im_type = h->h_type;
718 im->im_conv = IDMAP_CONV_NAMETOID;
719
720 memset(&msg, 0, sizeof(msg));
721 msg.data = im;
722 msg.len = sizeof(*im);
723
724 add_wait_queue(&idmap->idmap_wq, &wq);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300725 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 remove_wait_queue(&idmap->idmap_wq, &wq);
727 goto out;
728 }
729
730 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500731 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700733 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500735 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (im->im_status & IDMAP_STATUS_SUCCESS) {
738 *id = im->im_id;
739 ret = 0;
740 }
741
742 out:
743 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500744 mutex_unlock(&idmap->idmap_im_lock);
745 mutex_unlock(&idmap->idmap_lock);
Chuck Lever369af0f2007-12-20 14:54:35 -0500746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * ID -> Name
751 */
752static int
753nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
754 __u32 id, char *name)
755{
756 struct rpc_pipe_msg msg;
757 struct idmap_msg *im;
758 struct idmap_hashent *he;
759 DECLARE_WAITQUEUE(wq, current);
760 int ret = -EIO;
761 unsigned int len;
762
763 im = &idmap->idmap_im;
764
Ingo Molnarc9d51282006-03-20 13:44:11 -0500765 mutex_lock(&idmap->idmap_lock);
766 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 he = idmap_lookup_id(h, id);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800769 if (he) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 memcpy(name, he->ih_name, he->ih_namelen);
771 ret = he->ih_namelen;
772 goto out;
773 }
774
775 memset(im, 0, sizeof(*im));
776 im->im_type = h->h_type;
777 im->im_conv = IDMAP_CONV_IDTONAME;
778 im->im_id = id;
779
780 memset(&msg, 0, sizeof(msg));
781 msg.data = im;
782 msg.len = sizeof(*im);
783
784 add_wait_queue(&idmap->idmap_wq, &wq);
785
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300786 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 remove_wait_queue(&idmap->idmap_wq, &wq);
788 goto out;
789 }
790
791 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500792 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 schedule();
Milind Arun Choudharyfee7f232007-04-26 00:29:03 -0700794 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500796 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (im->im_status & IDMAP_STATUS_SUCCESS) {
799 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
800 goto out;
801 memcpy(name, im->im_name, len);
802 ret = len;
803 }
804
805 out:
806 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500807 mutex_unlock(&idmap->idmap_im_lock);
808 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return ret;
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812static ssize_t
813idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
814{
Chuck Lever369af0f2007-12-20 14:54:35 -0500815 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct idmap *idmap = (struct idmap *)rpci->private;
817 struct idmap_msg im_in, *im = &idmap->idmap_im;
818 struct idmap_hashtable *h;
819 struct idmap_hashent *he = NULL;
Chuck Leverd24aae42007-12-20 14:54:49 -0500820 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int ret;
822
Chuck Lever369af0f2007-12-20 14:54:35 -0500823 if (mlen != sizeof(im_in))
824 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Chuck Lever369af0f2007-12-20 14:54:35 -0500826 if (copy_from_user(&im_in, src, mlen) != 0)
827 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Ingo Molnarc9d51282006-03-20 13:44:11 -0500829 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 ret = mlen;
832 im->im_status = im_in.im_status;
833 /* If we got an error, terminate now, and wake up pending upcalls */
834 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
835 wake_up(&idmap->idmap_wq);
836 goto out;
837 }
838
839 /* Sanity checking of strings */
840 ret = -EINVAL;
841 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
842 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
843 goto out;
844
845 switch (im_in.im_type) {
846 case IDMAP_TYPE_USER:
847 h = &idmap->idmap_user_hash;
848 break;
849 case IDMAP_TYPE_GROUP:
850 h = &idmap->idmap_group_hash;
851 break;
852 default:
853 goto out;
854 }
855
856 switch (im_in.im_conv) {
857 case IDMAP_CONV_IDTONAME:
858 /* Did we match the current upcall? */
859 if (im->im_conv == IDMAP_CONV_IDTONAME
860 && im->im_type == im_in.im_type
861 && im->im_id == im_in.im_id) {
862 /* Yes: copy string, including the terminating '\0' */
863 memcpy(im->im_name, im_in.im_name, namelen_in);
864 im->im_name[namelen_in] = '\0';
865 wake_up(&idmap->idmap_wq);
866 }
867 he = idmap_alloc_id(h, im_in.im_id);
868 break;
869 case IDMAP_CONV_NAMETOID:
870 /* Did we match the current upcall? */
871 if (im->im_conv == IDMAP_CONV_NAMETOID
872 && im->im_type == im_in.im_type
873 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
874 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
875 im->im_id = im_in.im_id;
876 wake_up(&idmap->idmap_wq);
877 }
878 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
879 break;
880 default:
881 goto out;
882 }
883
884 /* If the entry is valid, also copy it to the cache */
885 if (he != NULL)
886 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
887 ret = mlen;
888out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500889 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return ret;
891}
892
Adrian Bunk75c96f82005-05-05 16:16:09 -0700893static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
895{
896 struct idmap_msg *im = msg->data;
897 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
898
899 if (msg->errno >= 0)
900 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500901 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
903 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500904 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907/*
908 * Fowler/Noll/Vo hash
909 * http://www.isthe.com/chongo/tech/comp/fnv/
910 */
911
912#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
913#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
914
915static unsigned int fnvhash32(const void *buf, size_t buflen)
916{
917 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
918 unsigned int hash = FNV_1_32;
919
920 for (p = buf; p < end; p++) {
921 hash *= FNV_P_32;
922 hash ^= (unsigned int)*p;
923 }
924
Chuck Lever369af0f2007-12-20 14:54:35 -0500925 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800928int 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 -0700929{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800930 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800932 if (nfs_map_string_to_numeric(name, namelen, uid))
933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
935}
936
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800937int 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 -0700938{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800939 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800941 if (nfs_map_string_to_numeric(name, namelen, uid))
942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
944}
945
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800946int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800948 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800949 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Trond Myklebustb064eca22011-02-22 15:44:32 -0800951 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
952 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800953 if (ret < 0)
954 ret = nfs_map_numeric_to_string(uid, buf, buflen);
955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800957int nfs_map_gid_to_group(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800959 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800960 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Trond Myklebustb064eca22011-02-22 15:44:32 -0800962 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
963 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800964 if (ret < 0)
965 ret = nfs_map_numeric_to_string(uid, buf, buflen);
966 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Bryan Schumaker955a8572010-09-29 15:41:49 -0400969#endif /* CONFIG_NFS_USE_NEW_IDMAPPER */