blob: 1b5058b4043bacbcb44f0ebbf03191bf230bd366 [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>
Bryan Schumaker57e62322012-02-24 14:14:51 -050037#include <linux/parser.h>
38#include <linux/fs.h>
Vitaliy Ivanove44ba032011-06-20 16:08:07 +020039#include <linux/nfs_idmap.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050040#include <net/net_namespace.h>
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050041#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050042#include <linux/nfs_fs.h>
Bryan Schumaker57e62322012-02-24 14:14:51 -050043#include <linux/nfs_fs_sb.h>
44#include <linux/key.h>
45#include <linux/keyctl.h>
46#include <linux/key-type.h>
47#include <keys/user-type.h>
48#include <linux/module.h>
49
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050050#include "internal.h"
Stanislav Kinsbursky17347d02012-01-26 15:11:41 +040051#include "netns.h"
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050052
53#define NFS_UINT_MAXLEN 11
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050054
55/* Default cache timeout is 10 minutes */
Bryan Schumaker57e62322012-02-24 14:14:51 -050056unsigned int nfs_idmap_cache_timeout = 600;
Trond Myklebust17280172012-03-11 13:11:00 -040057static const struct cred *id_resolver_cache;
58static struct key_type key_type_id_resolver_legacy;
Bryan Schumaker3cd0f372012-01-26 16:54:24 -050059
Bryan Schumakerb1027432012-06-20 14:35:28 -040060struct idmap {
61 struct rpc_pipe *idmap_pipe;
62 struct key_construction *idmap_key_cons;
63 struct mutex idmap_mutex;
64};
Trond Myklebust6926afd2012-01-07 13:22:46 -050065
66/**
67 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
68 * @fattr: fully initialised struct nfs_fattr
69 * @owner_name: owner name string cache
70 * @group_name: group name string cache
71 */
72void nfs_fattr_init_names(struct nfs_fattr *fattr,
73 struct nfs4_string *owner_name,
74 struct nfs4_string *group_name)
75{
76 fattr->owner_name = owner_name;
77 fattr->group_name = group_name;
78}
79
80static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
81{
82 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
83 kfree(fattr->owner_name->data);
84}
85
86static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
87{
88 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
89 kfree(fattr->group_name->data);
90}
91
92static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
93{
94 struct nfs4_string *owner = fattr->owner_name;
95 __u32 uid;
96
97 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
98 return false;
99 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
100 fattr->uid = uid;
101 fattr->valid |= NFS_ATTR_FATTR_OWNER;
102 }
103 return true;
104}
105
106static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
107{
108 struct nfs4_string *group = fattr->group_name;
109 __u32 gid;
110
111 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
112 return false;
113 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
114 fattr->gid = gid;
115 fattr->valid |= NFS_ATTR_FATTR_GROUP;
116 }
117 return true;
118}
119
120/**
121 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
122 * @fattr: a fully initialised nfs_fattr structure
123 */
124void nfs_fattr_free_names(struct nfs_fattr *fattr)
125{
126 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
127 nfs_fattr_free_owner_name(fattr);
128 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
129 nfs_fattr_free_group_name(fattr);
130}
131
132/**
133 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
134 * @server: pointer to the filesystem nfs_server structure
135 * @fattr: a fully initialised nfs_fattr structure
136 *
137 * This helper maps the cached NFSv4 owner/group strings in fattr into
138 * their numeric uid/gid equivalents, and then frees the cached strings.
139 */
140void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
141{
142 if (nfs_fattr_map_owner_name(server, fattr))
143 nfs_fattr_free_owner_name(fattr);
144 if (nfs_fattr_map_group_name(server, fattr))
145 nfs_fattr_free_group_name(fattr);
146}
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800147
148static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
149{
150 unsigned long val;
151 char buf[16];
152
153 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
154 return 0;
155 memcpy(buf, name, namelen);
156 buf[namelen] = '\0';
157 if (strict_strtoul(buf, 0, &val) != 0)
158 return 0;
159 *res = val;
160 return 1;
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Trond Myklebustf0b85162011-02-22 15:44:31 -0800163static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
164{
165 return snprintf(buf, buflen, "%u", id);
166}
167
Trond Myklebust17280172012-03-11 13:11:00 -0400168static struct key_type key_type_id_resolver = {
Bryan Schumaker955a8572010-09-29 15:41:49 -0400169 .name = "id_resolver",
170 .instantiate = user_instantiate,
171 .match = user_match,
172 .revoke = user_revoke,
173 .destroy = user_destroy,
174 .describe = user_describe,
175 .read = user_read,
176};
177
Bryan Schumakere6499c62012-01-26 16:54:23 -0500178static int nfs_idmap_init_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400179{
180 struct cred *cred;
181 struct key *keyring;
182 int ret = 0;
183
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500184 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
185 key_type_id_resolver.name);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400186
187 cred = prepare_kernel_cred(NULL);
188 if (!cred)
189 return -ENOMEM;
190
191 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
192 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
193 KEY_USR_VIEW | KEY_USR_READ,
194 KEY_ALLOC_NOT_IN_QUOTA);
195 if (IS_ERR(keyring)) {
196 ret = PTR_ERR(keyring);
197 goto failed_put_cred;
198 }
199
200 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
201 if (ret < 0)
202 goto failed_put_key;
203
204 ret = register_key_type(&key_type_id_resolver);
205 if (ret < 0)
206 goto failed_put_key;
207
David Howellsa427b9e2012-07-25 16:53:36 +0100208 ret = register_key_type(&key_type_id_resolver_legacy);
209 if (ret < 0)
210 goto failed_reg_legacy;
211
David Howells700920e2012-01-18 15:31:45 +0000212 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400213 cred->thread_keyring = keyring;
214 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
215 id_resolver_cache = cred;
216 return 0;
217
David Howellsa427b9e2012-07-25 16:53:36 +0100218failed_reg_legacy:
219 unregister_key_type(&key_type_id_resolver);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400220failed_put_key:
221 key_put(keyring);
222failed_put_cred:
223 put_cred(cred);
224 return ret;
225}
226
Bryan Schumakere6499c62012-01-26 16:54:23 -0500227static void nfs_idmap_quit_keyring(void)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400228{
229 key_revoke(id_resolver_cache->thread_keyring);
230 unregister_key_type(&key_type_id_resolver);
David Howellsa427b9e2012-07-25 16:53:36 +0100231 unregister_key_type(&key_type_id_resolver_legacy);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400232 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
Bryan Schumaker57e62322012-02-24 14:14:51 -0500263static ssize_t nfs_idmap_request_key(struct key_type *key_type,
264 const char *name, size_t namelen,
265 const char *type, void *data,
266 size_t data_size, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400267{
268 const struct cred *saved_cred;
269 struct key *rkey;
270 char *desc;
271 struct user_key_payload *payload;
272 ssize_t ret;
273
274 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
275 if (ret <= 0)
276 goto out;
277
278 saved_cred = override_creds(id_resolver_cache);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500279 if (idmap)
280 rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
281 else
282 rkey = request_key(&key_type_id_resolver, desc, "");
Bryan Schumaker955a8572010-09-29 15:41:49 -0400283 revert_creds(saved_cred);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500284
Bryan Schumaker955a8572010-09-29 15:41:49 -0400285 kfree(desc);
286 if (IS_ERR(rkey)) {
287 ret = PTR_ERR(rkey);
288 goto out;
289 }
290
291 rcu_read_lock();
292 rkey->perm |= KEY_USR_VIEW;
293
294 ret = key_validate(rkey);
295 if (ret < 0)
296 goto out_up;
297
298 payload = rcu_dereference(rkey->payload.data);
299 if (IS_ERR_OR_NULL(payload)) {
300 ret = PTR_ERR(payload);
301 goto out_up;
302 }
303
304 ret = payload->datalen;
305 if (ret > 0 && ret <= data_size)
306 memcpy(data, payload->data, ret);
307 else
308 ret = -EINVAL;
309
310out_up:
311 rcu_read_unlock();
312 key_put(rkey);
313out:
314 return ret;
315}
316
Bryan Schumaker57e62322012-02-24 14:14:51 -0500317static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
318 const char *type, void *data,
319 size_t data_size, struct idmap *idmap)
320{
321 ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
322 name, namelen, type, data,
323 data_size, NULL);
324 if (ret < 0) {
Bryan Schumakerb1027432012-06-20 14:35:28 -0400325 mutex_lock(&idmap->idmap_mutex);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500326 ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
327 name, namelen, type, data,
328 data_size, idmap);
Bryan Schumakerb1027432012-06-20 14:35:28 -0400329 mutex_unlock(&idmap->idmap_mutex);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500330 }
331 return ret;
332}
Bryan Schumaker955a8572010-09-29 15:41:49 -0400333
334/* ID -> Name */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500335static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
336 size_t buflen, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400337{
338 char id_str[NFS_UINT_MAXLEN];
339 int id_len;
340 ssize_t ret;
341
342 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500343 ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400344 if (ret < 0)
345 return -EINVAL;
346 return ret;
347}
348
349/* Name -> ID */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500350static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
351 __u32 *id, struct idmap *idmap)
Bryan Schumaker955a8572010-09-29 15:41:49 -0400352{
353 char id_str[NFS_UINT_MAXLEN];
354 long id_long;
355 ssize_t data_size;
356 int ret = 0;
357
Bryan Schumaker57e62322012-02-24 14:14:51 -0500358 data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
Bryan Schumaker955a8572010-09-29 15:41:49 -0400359 if (data_size <= 0) {
360 ret = -EINVAL;
361 } else {
362 ret = strict_strtol(id_str, 10, &id_long);
363 *id = (__u32)id_long;
364 }
365 return ret;
366}
367
Bryan Schumakere6499c62012-01-26 16:54:23 -0500368/* idmap classic begins here */
Bryan Schumaker57e62322012-02-24 14:14:51 -0500369module_param(nfs_idmap_cache_timeout, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Bryan Schumaker57e62322012-02-24 14:14:51 -0500371enum {
372 Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
373};
374
375static const match_table_t nfs_idmap_tokens = {
376 { Opt_find_uid, "uid:%s" },
377 { Opt_find_gid, "gid:%s" },
378 { Opt_find_user, "user:%s" },
379 { Opt_find_group, "group:%s" },
380 { Opt_find_err, NULL }
381};
382
383static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
Chuck Lever369af0f2007-12-20 14:54:35 -0500384static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
385 size_t);
386static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
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
Trond Myklebust17280172012-03-11 13:11:00 -0400394static struct key_type key_type_id_resolver_legacy = {
David Howellsa427b9e2012-07-25 16:53:36 +0100395 .name = "id_legacy",
Bryan Schumaker57e62322012-02-24 14:14:51 -0500396 .instantiate = user_instantiate,
397 .match = user_match,
398 .revoke = user_revoke,
399 .destroy = user_destroy,
400 .describe = user_describe,
401 .read = user_read,
402 .request_key = nfs_idmap_legacy_upcall,
403};
404
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400405static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
406{
407 if (pipe->dentry)
408 rpc_unlink(pipe->dentry);
409}
410
411static int __nfs_idmap_register(struct dentry *dir,
412 struct idmap *idmap,
413 struct rpc_pipe *pipe)
414{
415 struct dentry *dentry;
416
417 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
418 if (IS_ERR(dentry))
419 return PTR_ERR(dentry);
420 pipe->dentry = dentry;
421 return 0;
422}
423
424static void nfs_idmap_unregister(struct nfs_client *clp,
425 struct rpc_pipe *pipe)
426{
Chuck Lever73ea6662012-05-21 22:44:50 -0400427 struct net *net = clp->cl_net;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400428 struct super_block *pipefs_sb;
429
430 pipefs_sb = rpc_get_sb_net(net);
431 if (pipefs_sb) {
432 __nfs_idmap_unregister(pipe);
433 rpc_put_sb_net(net);
434 }
435}
436
437static int nfs_idmap_register(struct nfs_client *clp,
438 struct idmap *idmap,
439 struct rpc_pipe *pipe)
440{
Chuck Lever73ea6662012-05-21 22:44:50 -0400441 struct net *net = clp->cl_net;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400442 struct super_block *pipefs_sb;
443 int err = 0;
444
445 pipefs_sb = rpc_get_sb_net(net);
446 if (pipefs_sb) {
447 if (clp->cl_rpcclient->cl_dentry)
448 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
449 idmap, pipe);
450 rpc_put_sb_net(net);
451 }
452 return err;
453}
454
David Howellsb7162792006-08-22 20:06:09 -0400455int
David Howellsadfa6f92006-08-22 20:06:08 -0400456nfs_idmap_new(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct idmap *idmap;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300459 struct rpc_pipe *pipe;
David Howellsb7162792006-08-22 20:06:09 -0400460 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David Howells54ceac42006-08-22 20:06:13 -0400462 BUG_ON(clp->cl_idmap != NULL);
David Howellsb7162792006-08-22 20:06:09 -0400463
Chuck Lever369af0f2007-12-20 14:54:35 -0500464 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
465 if (idmap == NULL)
466 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300468 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
469 if (IS_ERR(pipe)) {
470 error = PTR_ERR(pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 kfree(idmap);
David Howellsb7162792006-08-22 20:06:09 -0400472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400474 error = nfs_idmap_register(clp, idmap, pipe);
475 if (error) {
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300476 rpc_destroy_pipe_data(pipe);
477 kfree(idmap);
478 return error;
479 }
480 idmap->idmap_pipe = pipe;
Bryan Schumakerb1027432012-06-20 14:35:28 -0400481 mutex_init(&idmap->idmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 clp->cl_idmap = idmap;
David Howellsb7162792006-08-22 20:06:09 -0400484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487void
David Howellsadfa6f92006-08-22 20:06:08 -0400488nfs_idmap_delete(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct idmap *idmap = clp->cl_idmap;
491
492 if (!idmap)
493 return;
Stanislav Kinsbursky4929d1d2012-01-10 16:13:11 +0400494 nfs_idmap_unregister(clp, idmap->idmap_pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300495 rpc_destroy_pipe_data(idmap->idmap_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 clp->cl_idmap = NULL;
497 kfree(idmap);
498}
499
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400500static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
501 struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400503 int err = 0;
504
505 switch (event) {
506 case RPC_PIPEFS_MOUNT:
507 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
508 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
509 clp->cl_idmap,
510 clp->cl_idmap->idmap_pipe);
511 break;
512 case RPC_PIPEFS_UMOUNT:
513 if (clp->cl_idmap->idmap_pipe) {
514 struct dentry *parent;
515
516 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
517 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
518 /*
519 * Note: This is a dirty hack. SUNRPC hook has been
520 * called already but simple_rmdir() call for the
521 * directory returned with error because of idmap pipe
522 * inside. Thus now we have to remove this directory
523 * here.
524 */
525 if (rpc_rmdir(parent))
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500526 printk(KERN_ERR "NFS: %s: failed to remove "
527 "clnt dir!\n", __func__);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400528 }
529 break;
530 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500531 printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
532 event);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400533 return -ENOTSUPP;
534 }
535 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400538static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400540 struct nfs_net *nn = net_generic(net, nfs_net_id);
541 struct dentry *cl_dentry;
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400542 struct nfs_client *clp;
Trond Myklebust4697bd52012-05-23 13:24:36 -0400543 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Trond Myklebust4697bd52012-05-23 13:24:36 -0400545restart:
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000546 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000547 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Trond Myklebust4697bd52012-05-23 13:24:36 -0400548 /* Wait for initialisation to finish */
549 if (clp->cl_cons_state == NFS_CS_INITING) {
550 atomic_inc(&clp->cl_count);
551 spin_unlock(&nn->nfs_client_lock);
552 err = nfs_wait_client_init_complete(clp);
553 nfs_put_client(clp);
554 if (err)
555 return NULL;
556 goto restart;
557 }
558 /* Skip nfs_clients that failed to initialise */
559 if (clp->cl_cons_state < 0)
560 continue;
Trond Myklebust54ac4712012-05-23 13:26:10 -0400561 smp_rmb();
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400562 if (clp->rpc_ops != &nfs_v4_clientops)
563 continue;
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400564 cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
565 if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
566 ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
567 continue;
568 atomic_inc(&clp->cl_count);
569 spin_unlock(&nn->nfs_client_lock);
570 return clp;
571 }
572 spin_unlock(&nn->nfs_client_lock);
573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400576static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
577 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400579 struct super_block *sb = ptr;
580 struct nfs_client *clp;
581 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stanislav Kinsbursky71dfc5f2012-04-28 19:32:21 +0400583 if (!try_module_get(THIS_MODULE))
584 return 0;
585
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400586 while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400587 error = __rpc_pipefs_event(clp, event, sb);
Stanislav Kinsburskye9dbca82012-02-27 22:05:37 +0400588 nfs_put_client(clp);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400589 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Stanislav Kinsbursky71dfc5f2012-04-28 19:32:21 +0400592 module_put(THIS_MODULE);
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400593 return error;
594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400596#define PIPEFS_NFS_PRIO 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400598static struct notifier_block nfs_idmap_block = {
599 .notifier_call = rpc_pipefs_event,
600 .priority = SUNRPC_PIPEFS_NFS_PRIO,
601};
602
603int nfs_idmap_init(void)
604{
Bryan Schumakere6499c62012-01-26 16:54:23 -0500605 int ret;
606 ret = nfs_idmap_init_keyring();
607 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto out;
Bryan Schumakere6499c62012-01-26 16:54:23 -0500609 ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
610 if (ret != 0)
611 nfs_idmap_quit_keyring();
612out:
Chuck Lever369af0f2007-12-20 14:54:35 -0500613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400616void nfs_idmap_quit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400618 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
Bryan Schumakere6499c62012-01-26 16:54:23 -0500619 nfs_idmap_quit_keyring();
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400620}
621
Bryan Schumaker57e62322012-02-24 14:14:51 -0500622static int nfs_idmap_prepare_message(char *desc, struct idmap_msg *im,
623 struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Bryan Schumaker57e62322012-02-24 14:14:51 -0500625 substring_t substr;
626 int token, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Bryan Schumaker57e62322012-02-24 14:14:51 -0500628 memset(im, 0, sizeof(*im));
629 memset(msg, 0, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Bryan Schumaker57e62322012-02-24 14:14:51 -0500631 im->im_type = IDMAP_TYPE_GROUP;
632 token = match_token(desc, nfs_idmap_tokens, &substr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Bryan Schumaker57e62322012-02-24 14:14:51 -0500634 switch (token) {
635 case Opt_find_uid:
636 im->im_type = IDMAP_TYPE_USER;
637 case Opt_find_gid:
638 im->im_conv = IDMAP_CONV_NAMETOID;
639 ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Bryan Schumaker57e62322012-02-24 14:14:51 -0500642 case Opt_find_user:
643 im->im_type = IDMAP_TYPE_USER;
644 case Opt_find_group:
645 im->im_conv = IDMAP_CONV_IDTONAME;
646 ret = match_int(&substr, &im->im_id);
647 break;
Trond Myklebust685f50f2012-02-08 13:39:15 -0500648
Bryan Schumaker57e62322012-02-24 14:14:51 -0500649 default:
650 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto out;
652 }
653
Bryan Schumaker57e62322012-02-24 14:14:51 -0500654 msg->data = im;
655 msg->len = sizeof(struct idmap_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Bryan Schumaker57e62322012-02-24 14:14:51 -0500657out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return ret;
659}
660
Bryan Schumaker57e62322012-02-24 14:14:51 -0500661static int nfs_idmap_legacy_upcall(struct key_construction *cons,
662 const char *op,
663 void *aux)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Bryan Schumaker57e62322012-02-24 14:14:51 -0500665 struct rpc_pipe_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct idmap_msg *im;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500667 struct idmap *idmap = (struct idmap *)aux;
668 struct key *key = cons->key;
Dan Carpenter5abc03c2012-05-14 22:45:28 +0300669 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Bryan Schumaker57e62322012-02-24 14:14:51 -0500671 /* msg and im are freed in idmap_pipe_destroy_msg */
672 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
Dan Carpenter5abc03c2012-05-14 22:45:28 +0300673 if (!msg)
Bryan Schumaker57e62322012-02-24 14:14:51 -0500674 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Bryan Schumaker57e62322012-02-24 14:14:51 -0500676 im = kmalloc(sizeof(*im), GFP_KERNEL);
Dan Carpenter5abc03c2012-05-14 22:45:28 +0300677 if (!im)
Bryan Schumaker57e62322012-02-24 14:14:51 -0500678 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Bryan Schumaker57e62322012-02-24 14:14:51 -0500680 ret = nfs_idmap_prepare_message(key->description, im, msg);
681 if (ret < 0)
682 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howellsa427b9e2012-07-25 16:53:36 +0100684 BUG_ON(idmap->idmap_key_cons != NULL);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500685 idmap->idmap_key_cons = cons;
686
Bryan Schumaker11588f42012-03-12 11:33:00 -0400687 ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
688 if (ret < 0)
689 goto out2;
690
691 return ret;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500692
693out2:
694 kfree(im);
695out1:
696 kfree(msg);
697out0:
David Howellsa427b9e2012-07-25 16:53:36 +0100698 complete_request_key(cons, ret);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500699 return ret;
700}
701
702static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
703{
704 return key_instantiate_and_link(key, data, strlen(data) + 1,
705 id_resolver_cache->thread_keyring,
706 authkey);
707}
708
709static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
710{
711 char id_str[NFS_UINT_MAXLEN];
712 int ret = -EINVAL;
713
714 switch (im->im_conv) {
715 case IDMAP_CONV_NAMETOID:
716 sprintf(id_str, "%d", im->im_id);
717 ret = nfs_idmap_instantiate(key, authkey, id_str);
718 break;
719 case IDMAP_CONV_IDTONAME:
720 ret = nfs_idmap_instantiate(key, authkey, im->im_name);
721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return ret;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static ssize_t
728idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
729{
Chuck Lever369af0f2007-12-20 14:54:35 -0500730 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct idmap *idmap = (struct idmap *)rpci->private;
David Howellsa427b9e2012-07-25 16:53:36 +0100732 struct key_construction *cons;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500733 struct idmap_msg im;
Chuck Leverd24aae42007-12-20 14:54:49 -0500734 size_t namelen_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int ret;
736
David Howellsa427b9e2012-07-25 16:53:36 +0100737 /* If instantiation is successful, anyone waiting for key construction
738 * will have been woken up and someone else may now have used
739 * idmap_key_cons - so after this point we may no longer touch it.
740 */
741 cons = ACCESS_ONCE(idmap->idmap_key_cons);
742 idmap->idmap_key_cons = NULL;
743
Bryan Schumaker57e62322012-02-24 14:14:51 -0500744 if (mlen != sizeof(im)) {
745 ret = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
747 }
748
Bryan Schumaker57e62322012-02-24 14:14:51 -0500749 if (copy_from_user(&im, src, mlen) != 0) {
750 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto out;
752 }
753
Bryan Schumaker57e62322012-02-24 14:14:51 -0500754 if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
755 ret = mlen;
David Howellsa427b9e2012-07-25 16:53:36 +0100756 complete_request_key(cons, -ENOKEY);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500757 goto out_incomplete;
758 }
759
760 namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
761 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
762 ret = -EINVAL;
763 goto out;
764 }
765
766 ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
767 if (ret >= 0) {
768 key_set_timeout(cons->key, nfs_idmap_cache_timeout);
769 ret = mlen;
770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772out:
David Howellsa427b9e2012-07-25 16:53:36 +0100773 complete_request_key(cons, ret);
Bryan Schumaker57e62322012-02-24 14:14:51 -0500774out_incomplete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return ret;
776}
777
Adrian Bunk75c96f82005-05-05 16:16:09 -0700778static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
780{
Bryan Schumaker57e62322012-02-24 14:14:51 -0500781 /* Free memory allocated in nfs_idmap_legacy_upcall() */
782 kfree(msg->data);
783 kfree(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800786int 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 -0700787{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800788 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800790 if (nfs_map_string_to_numeric(name, namelen, uid))
791 return 0;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500792 return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Bryan Schumakere6499c62012-01-26 16:54:23 -0500795int 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 -0700796{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800797 struct idmap *idmap = server->nfs_client->cl_idmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Bryan Schumakere6499c62012-01-26 16:54:23 -0500799 if (nfs_map_string_to_numeric(name, namelen, gid))
Trond Myklebust5cf36cf2011-02-22 15:44:31 -0800800 return 0;
Bryan Schumaker57e62322012-02-24 14:14:51 -0500801 return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800804int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800806 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800807 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Trond Myklebustb064eca22011-02-22 15:44:32 -0800809 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
Bryan Schumaker57e62322012-02-24 14:14:51 -0500810 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800811 if (ret < 0)
812 ret = nfs_map_numeric_to_string(uid, buf, buflen);
813 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
Bryan Schumakere6499c62012-01-26 16:54:23 -0500815int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Trond Myklebuste4fd72a2011-02-22 15:44:31 -0800817 struct idmap *idmap = server->nfs_client->cl_idmap;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800818 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Trond Myklebustb064eca22011-02-22 15:44:32 -0800820 if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
Bryan Schumaker57e62322012-02-24 14:14:51 -0500821 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800822 if (ret < 0)
Bryan Schumakere6499c62012-01-26 16:54:23 -0500823 ret = nfs_map_numeric_to_string(gid, buf, buflen);
Trond Myklebustf0b85162011-02-22 15:44:31 -0800824 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}