NFS: build fixed in case of NFS_USE_NEW_IDMAPPER is undefined
[deliverable/linux.git] / fs / nfs / idmap.c
CommitLineData
1da177e4
LT
1/*
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 */
5cf36cfd
TM
36#include <linux/types.h>
37#include <linux/string.h>
38#include <linux/kernel.h>
e44ba033
VI
39#include <linux/slab.h>
40#include <linux/nfs_idmap.h>
6926afd1 41#include <linux/nfs_fs.h>
3cd0f37a
BS
42#include <linux/cred.h>
43#include <linux/sunrpc/sched.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs_sb.h>
46#include <linux/keyctl.h>
47#include <linux/key-type.h>
48#include <linux/rcupdate.h>
49#include <linux/err.h>
50#include <keys/user-type.h>
51
52/* include files needed by legacy idmapper */
53#include <linux/module.h>
54#include <linux/mutex.h>
55#include <linux/init.h>
56#include <linux/socket.h>
57#include <linux/in.h>
58#include <linux/sched.h>
59#include <linux/sunrpc/clnt.h>
60#include <linux/workqueue.h>
61#include <linux/sunrpc/rpc_pipe_fs.h>
62#include <linux/nfs_fs.h>
63#include "nfs4_fs.h"
64#include "internal.h"
17347d03 65#include "netns.h"
3cd0f37a
BS
66
67#define NFS_UINT_MAXLEN 11
68#define IDMAP_HASH_SZ 128
69
70/* Default cache timeout is 10 minutes */
71unsigned int nfs_idmap_cache_timeout = 600 * HZ;
72const struct cred *id_resolver_cache;
73
6926afd1
TM
74
75/**
76 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
77 * @fattr: fully initialised struct nfs_fattr
78 * @owner_name: owner name string cache
79 * @group_name: group name string cache
80 */
81void nfs_fattr_init_names(struct nfs_fattr *fattr,
82 struct nfs4_string *owner_name,
83 struct nfs4_string *group_name)
84{
85 fattr->owner_name = owner_name;
86 fattr->group_name = group_name;
87}
88
89static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
90{
91 fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
92 kfree(fattr->owner_name->data);
93}
94
95static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
96{
97 fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
98 kfree(fattr->group_name->data);
99}
100
101static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
102{
103 struct nfs4_string *owner = fattr->owner_name;
104 __u32 uid;
105
106 if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
107 return false;
108 if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
109 fattr->uid = uid;
110 fattr->valid |= NFS_ATTR_FATTR_OWNER;
111 }
112 return true;
113}
114
115static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
116{
117 struct nfs4_string *group = fattr->group_name;
118 __u32 gid;
119
120 if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
121 return false;
122 if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
123 fattr->gid = gid;
124 fattr->valid |= NFS_ATTR_FATTR_GROUP;
125 }
126 return true;
127}
128
129/**
130 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
131 * @fattr: a fully initialised nfs_fattr structure
132 */
133void nfs_fattr_free_names(struct nfs_fattr *fattr)
134{
135 if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
136 nfs_fattr_free_owner_name(fattr);
137 if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
138 nfs_fattr_free_group_name(fattr);
139}
140
141/**
142 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
143 * @server: pointer to the filesystem nfs_server structure
144 * @fattr: a fully initialised nfs_fattr structure
145 *
146 * This helper maps the cached NFSv4 owner/group strings in fattr into
147 * their numeric uid/gid equivalents, and then frees the cached strings.
148 */
149void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
150{
151 if (nfs_fattr_map_owner_name(server, fattr))
152 nfs_fattr_free_owner_name(fattr);
153 if (nfs_fattr_map_group_name(server, fattr))
154 nfs_fattr_free_group_name(fattr);
155}
5cf36cfd
TM
156
157static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
158{
159 unsigned long val;
160 char buf[16];
161
162 if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
163 return 0;
164 memcpy(buf, name, namelen);
165 buf[namelen] = '\0';
166 if (strict_strtoul(buf, 0, &val) != 0)
167 return 0;
168 *res = val;
169 return 1;
170}
1da177e4 171
f0b85168
TM
172static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
173{
174 return snprintf(buf, buflen, "%u", id);
175}
176
955a857e
BS
177struct key_type key_type_id_resolver = {
178 .name = "id_resolver",
179 .instantiate = user_instantiate,
180 .match = user_match,
181 .revoke = user_revoke,
182 .destroy = user_destroy,
183 .describe = user_describe,
184 .read = user_read,
185};
186
e6499c6f 187static int nfs_idmap_init_keyring(void)
955a857e
BS
188{
189 struct cred *cred;
190 struct key *keyring;
191 int ret = 0;
192
f9fd2d9c
WAA
193 printk(KERN_NOTICE "NFS: Registering the %s key type\n",
194 key_type_id_resolver.name);
955a857e
BS
195
196 cred = prepare_kernel_cred(NULL);
197 if (!cred)
198 return -ENOMEM;
199
200 keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
201 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
202 KEY_USR_VIEW | KEY_USR_READ,
203 KEY_ALLOC_NOT_IN_QUOTA);
204 if (IS_ERR(keyring)) {
205 ret = PTR_ERR(keyring);
206 goto failed_put_cred;
207 }
208
209 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
210 if (ret < 0)
211 goto failed_put_key;
212
213 ret = register_key_type(&key_type_id_resolver);
214 if (ret < 0)
215 goto failed_put_key;
216
217 cred->thread_keyring = keyring;
218 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
219 id_resolver_cache = cred;
220 return 0;
221
222failed_put_key:
223 key_put(keyring);
224failed_put_cred:
225 put_cred(cred);
226 return ret;
227}
228
e6499c6f 229static void nfs_idmap_quit_keyring(void)
955a857e
BS
230{
231 key_revoke(id_resolver_cache->thread_keyring);
232 unregister_key_type(&key_type_id_resolver);
233 put_cred(id_resolver_cache);
234}
235
236/*
237 * Assemble the description to pass to request_key()
238 * This function will allocate a new string and update dest to point
239 * at it. The caller is responsible for freeing dest.
240 *
241 * On error 0 is returned. Otherwise, the length of dest is returned.
242 */
243static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
244 const char *type, size_t typelen, char **desc)
245{
246 char *cp;
247 size_t desclen = typelen + namelen + 2;
248
249 *desc = kmalloc(desclen, GFP_KERNEL);
8f0d97b4 250 if (!*desc)
955a857e
BS
251 return -ENOMEM;
252
253 cp = *desc;
254 memcpy(cp, type, typelen);
255 cp += typelen;
256 *cp++ = ':';
257
258 memcpy(cp, name, namelen);
259 cp += namelen;
260 *cp = '\0';
261 return desclen;
262}
263
264static ssize_t nfs_idmap_request_key(const char *name, size_t namelen,
265 const char *type, void *data, size_t data_size)
266{
267 const struct cred *saved_cred;
268 struct key *rkey;
269 char *desc;
270 struct user_key_payload *payload;
271 ssize_t ret;
272
273 ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
274 if (ret <= 0)
275 goto out;
276
277 saved_cred = override_creds(id_resolver_cache);
278 rkey = request_key(&key_type_id_resolver, desc, "");
279 revert_creds(saved_cred);
280 kfree(desc);
281 if (IS_ERR(rkey)) {
282 ret = PTR_ERR(rkey);
283 goto out;
284 }
285
286 rcu_read_lock();
287 rkey->perm |= KEY_USR_VIEW;
288
289 ret = key_validate(rkey);
290 if (ret < 0)
291 goto out_up;
292
293 payload = rcu_dereference(rkey->payload.data);
294 if (IS_ERR_OR_NULL(payload)) {
295 ret = PTR_ERR(payload);
296 goto out_up;
297 }
298
299 ret = payload->datalen;
300 if (ret > 0 && ret <= data_size)
301 memcpy(data, payload->data, ret);
302 else
303 ret = -EINVAL;
304
305out_up:
306 rcu_read_unlock();
307 key_put(rkey);
308out:
309 return ret;
310}
311
312
313/* ID -> Name */
314static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen)
315{
316 char id_str[NFS_UINT_MAXLEN];
317 int id_len;
318 ssize_t ret;
319
320 id_len = snprintf(id_str, sizeof(id_str), "%u", id);
321 ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen);
322 if (ret < 0)
323 return -EINVAL;
324 return ret;
325}
326
327/* Name -> ID */
328static int nfs_idmap_lookup_id(const char *name, size_t namelen,
329 const char *type, __u32 *id)
330{
331 char id_str[NFS_UINT_MAXLEN];
332 long id_long;
333 ssize_t data_size;
334 int ret = 0;
335
336 data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN);
337 if (data_size <= 0) {
338 ret = -EINVAL;
339 } else {
340 ret = strict_strtol(id_str, 10, &id_long);
341 *id = (__u32)id_long;
342 }
343 return ret;
344}
345
e6499c6f 346/* idmap classic begins here */
7d4e2747
DH
347static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
348{
349 char *endp;
350 int num = simple_strtol(val, &endp, 0);
351 int jif = num * HZ;
352 if (endp == val || *endp || num < 0 || jif < num)
353 return -EINVAL;
354 *((int *)kp->arg) = jif;
355 return 0;
356}
357
358module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
359 &nfs_idmap_cache_timeout, 0644);
360
1da177e4 361struct idmap_hashent {
369af0f1
CL
362 unsigned long ih_expires;
363 __u32 ih_id;
d24aae41 364 size_t ih_namelen;
369af0f1 365 char ih_name[IDMAP_NAMESZ];
1da177e4
LT
366};
367
368struct idmap_hashtable {
369af0f1
CL
369 __u8 h_type;
370 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
1da177e4
LT
371};
372
373struct idmap {
c239d83b 374 struct rpc_pipe *idmap_pipe;
369af0f1
CL
375 wait_queue_head_t idmap_wq;
376 struct idmap_msg idmap_im;
377 struct mutex idmap_lock; /* Serializes upcalls */
378 struct mutex idmap_im_lock; /* Protects the hashtable */
379 struct idmap_hashtable idmap_user_hash;
380 struct idmap_hashtable idmap_group_hash;
1da177e4
LT
381};
382
369af0f1
CL
383static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
384 size_t);
385static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
1da177e4
LT
386
387static unsigned int fnvhash32(const void *, size_t);
388
b693ba4a 389static const struct rpc_pipe_ops idmap_upcall_ops = {
c1225158 390 .upcall = rpc_pipe_generic_upcall,
369af0f1
CL
391 .downcall = idmap_pipe_downcall,
392 .destroy_msg = idmap_pipe_destroy_msg,
1da177e4
LT
393};
394
4929d1d3
SK
395static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
396{
397 if (pipe->dentry)
398 rpc_unlink(pipe->dentry);
399}
400
401static int __nfs_idmap_register(struct dentry *dir,
402 struct idmap *idmap,
403 struct rpc_pipe *pipe)
404{
405 struct dentry *dentry;
406
407 dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
408 if (IS_ERR(dentry))
409 return PTR_ERR(dentry);
410 pipe->dentry = dentry;
411 return 0;
412}
413
414static void nfs_idmap_unregister(struct nfs_client *clp,
415 struct rpc_pipe *pipe)
416{
417 struct net *net = clp->net;
418 struct super_block *pipefs_sb;
419
420 pipefs_sb = rpc_get_sb_net(net);
421 if (pipefs_sb) {
422 __nfs_idmap_unregister(pipe);
423 rpc_put_sb_net(net);
424 }
425}
426
427static int nfs_idmap_register(struct nfs_client *clp,
428 struct idmap *idmap,
429 struct rpc_pipe *pipe)
430{
431 struct net *net = clp->net;
432 struct super_block *pipefs_sb;
433 int err = 0;
434
435 pipefs_sb = rpc_get_sb_net(net);
436 if (pipefs_sb) {
437 if (clp->cl_rpcclient->cl_dentry)
438 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
439 idmap, pipe);
440 rpc_put_sb_net(net);
441 }
442 return err;
443}
444
b7162792 445int
adfa6f98 446nfs_idmap_new(struct nfs_client *clp)
1da177e4
LT
447{
448 struct idmap *idmap;
c239d83b 449 struct rpc_pipe *pipe;
b7162792 450 int error;
1da177e4 451
54ceac45 452 BUG_ON(clp->cl_idmap != NULL);
b7162792 453
369af0f1
CL
454 idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
455 if (idmap == NULL)
456 return -ENOMEM;
1da177e4 457
c239d83b
SK
458 pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
459 if (IS_ERR(pipe)) {
460 error = PTR_ERR(pipe);
1da177e4 461 kfree(idmap);
b7162792 462 return error;
1da177e4 463 }
4929d1d3
SK
464 error = nfs_idmap_register(clp, idmap, pipe);
465 if (error) {
c239d83b
SK
466 rpc_destroy_pipe_data(pipe);
467 kfree(idmap);
468 return error;
469 }
470 idmap->idmap_pipe = pipe;
369af0f1
CL
471 mutex_init(&idmap->idmap_lock);
472 mutex_init(&idmap->idmap_im_lock);
1da177e4
LT
473 init_waitqueue_head(&idmap->idmap_wq);
474 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
475 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
476
477 clp->cl_idmap = idmap;
b7162792 478 return 0;
1da177e4
LT
479}
480
481void
adfa6f98 482nfs_idmap_delete(struct nfs_client *clp)
1da177e4
LT
483{
484 struct idmap *idmap = clp->cl_idmap;
485
486 if (!idmap)
487 return;
4929d1d3 488 nfs_idmap_unregister(clp, idmap->idmap_pipe);
c239d83b 489 rpc_destroy_pipe_data(idmap->idmap_pipe);
1da177e4
LT
490 clp->cl_idmap = NULL;
491 kfree(idmap);
492}
493
eee17325
SK
494static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
495 struct super_block *sb)
496{
497 int err = 0;
498
499 switch (event) {
500 case RPC_PIPEFS_MOUNT:
501 BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
502 err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
503 clp->cl_idmap,
504 clp->cl_idmap->idmap_pipe);
505 break;
506 case RPC_PIPEFS_UMOUNT:
507 if (clp->cl_idmap->idmap_pipe) {
508 struct dentry *parent;
509
510 parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
511 __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
512 /*
513 * Note: This is a dirty hack. SUNRPC hook has been
514 * called already but simple_rmdir() call for the
515 * directory returned with error because of idmap pipe
516 * inside. Thus now we have to remove this directory
517 * here.
518 */
519 if (rpc_rmdir(parent))
a030889a
WAA
520 printk(KERN_ERR "NFS: %s: failed to remove "
521 "clnt dir!\n", __func__);
eee17325
SK
522 }
523 break;
524 default:
a030889a
WAA
525 printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
526 event);
eee17325
SK
527 return -ENOTSUPP;
528 }
529 return err;
530}
531
532static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
533 void *ptr)
534{
535 struct super_block *sb = ptr;
6b13168b 536 struct nfs_net *nn = net_generic(sb->s_fs_info, nfs_net_id);
eee17325
SK
537 struct nfs_client *clp;
538 int error = 0;
539
dc030858 540 spin_lock(&nn->nfs_client_lock);
6b13168b 541 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
eee17325
SK
542 if (clp->rpc_ops != &nfs_v4_clientops)
543 continue;
544 error = __rpc_pipefs_event(clp, event, sb);
545 if (error)
546 break;
547 }
dc030858 548 spin_unlock(&nn->nfs_client_lock);
eee17325
SK
549 return error;
550}
551
552#define PIPEFS_NFS_PRIO 1
553
554static struct notifier_block nfs_idmap_block = {
555 .notifier_call = rpc_pipefs_event,
556 .priority = SUNRPC_PIPEFS_NFS_PRIO,
557};
558
559int nfs_idmap_init(void)
560{
e6499c6f
BS
561 int ret;
562 ret = nfs_idmap_init_keyring();
563 if (ret != 0)
564 goto out;
565 ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
566 if (ret != 0)
567 nfs_idmap_quit_keyring();
568out:
569 return ret;
eee17325
SK
570}
571
572void nfs_idmap_quit(void)
573{
574 rpc_pipefs_notifier_unregister(&nfs_idmap_block);
e6499c6f 575 nfs_idmap_quit_keyring();
eee17325
SK
576}
577
1da177e4
LT
578/*
579 * Helper routines for manipulating the hashtable
580 */
581static inline struct idmap_hashent *
582idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
583{
584 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
585}
586
587static struct idmap_hashent *
588idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
589{
590 struct idmap_hashent *he = idmap_name_hash(h, name, len);
591
592 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
593 return NULL;
58df095b
TM
594 if (time_after(jiffies, he->ih_expires))
595 return NULL;
1da177e4
LT
596 return he;
597}
598
599static inline struct idmap_hashent *
600idmap_id_hash(struct idmap_hashtable* h, __u32 id)
601{
602 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
603}
604
605static struct idmap_hashent *
606idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
607{
608 struct idmap_hashent *he = idmap_id_hash(h, id);
609 if (he->ih_id != id || he->ih_namelen == 0)
610 return NULL;
58df095b
TM
611 if (time_after(jiffies, he->ih_expires))
612 return NULL;
1da177e4
LT
613 return he;
614}
615
616/*
617 * Routines for allocating new entries in the hashtable.
618 * For now, we just have 1 entry per bucket, so it's all
619 * pretty trivial.
620 */
621static inline struct idmap_hashent *
d24aae41 622idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len)
1da177e4
LT
623{
624 return idmap_name_hash(h, name, len);
625}
626
627static inline struct idmap_hashent *
628idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
629{
630 return idmap_id_hash(h, id);
631}
632
633static void
634idmap_update_entry(struct idmap_hashent *he, const char *name,
635 size_t namelen, __u32 id)
636{
637 he->ih_id = id;
638 memcpy(he->ih_name, name, namelen);
639 he->ih_name[namelen] = '\0';
640 he->ih_namelen = namelen;
58df095b 641 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
1da177e4
LT
642}
643
644/*
645 * Name -> ID
646 */
647static int
648nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
649 const char *name, size_t namelen, __u32 *id)
650{
651 struct rpc_pipe_msg msg;
652 struct idmap_msg *im;
653 struct idmap_hashent *he;
654 DECLARE_WAITQUEUE(wq, current);
655 int ret = -EIO;
656
657 im = &idmap->idmap_im;
658
659 /*
660 * String sanity checks
661 * Note that the userland daemon expects NUL terminated strings
662 */
663 for (;;) {
664 if (namelen == 0)
665 return -EINVAL;
666 if (name[namelen-1] != '\0')
667 break;
668 namelen--;
669 }
670 if (namelen >= IDMAP_NAMESZ)
671 return -EINVAL;
672
c9d5128a
IM
673 mutex_lock(&idmap->idmap_lock);
674 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
675
676 he = idmap_lookup_name(h, name, namelen);
677 if (he != NULL) {
678 *id = he->ih_id;
679 ret = 0;
680 goto out;
681 }
682
683 memset(im, 0, sizeof(*im));
684 memcpy(im->im_name, name, namelen);
685
686 im->im_type = h->h_type;
687 im->im_conv = IDMAP_CONV_NAMETOID;
688
689 memset(&msg, 0, sizeof(msg));
690 msg.data = im;
691 msg.len = sizeof(*im);
692
693 add_wait_queue(&idmap->idmap_wq, &wq);
c239d83b 694 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
1da177e4
LT
695 remove_wait_queue(&idmap->idmap_wq, &wq);
696 goto out;
697 }
698
699 set_current_state(TASK_UNINTERRUPTIBLE);
c9d5128a 700 mutex_unlock(&idmap->idmap_im_lock);
1da177e4 701 schedule();
fee7f23f 702 __set_current_state(TASK_RUNNING);
1da177e4 703 remove_wait_queue(&idmap->idmap_wq, &wq);
c9d5128a 704 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
705
706 if (im->im_status & IDMAP_STATUS_SUCCESS) {
707 *id = im->im_id;
708 ret = 0;
709 }
710
711 out:
712 memset(im, 0, sizeof(*im));
c9d5128a
IM
713 mutex_unlock(&idmap->idmap_im_lock);
714 mutex_unlock(&idmap->idmap_lock);
369af0f1 715 return ret;
1da177e4
LT
716}
717
718/*
719 * ID -> Name
720 */
721static int
722nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
723 __u32 id, char *name)
724{
725 struct rpc_pipe_msg msg;
726 struct idmap_msg *im;
727 struct idmap_hashent *he;
728 DECLARE_WAITQUEUE(wq, current);
729 int ret = -EIO;
730 unsigned int len;
731
732 im = &idmap->idmap_im;
733
c9d5128a
IM
734 mutex_lock(&idmap->idmap_lock);
735 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
736
737 he = idmap_lookup_id(h, id);
90dc7d27 738 if (he) {
1da177e4
LT
739 memcpy(name, he->ih_name, he->ih_namelen);
740 ret = he->ih_namelen;
741 goto out;
742 }
743
744 memset(im, 0, sizeof(*im));
745 im->im_type = h->h_type;
746 im->im_conv = IDMAP_CONV_IDTONAME;
747 im->im_id = id;
748
749 memset(&msg, 0, sizeof(msg));
750 msg.data = im;
751 msg.len = sizeof(*im);
752
753 add_wait_queue(&idmap->idmap_wq, &wq);
754
c239d83b 755 if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) {
1da177e4
LT
756 remove_wait_queue(&idmap->idmap_wq, &wq);
757 goto out;
758 }
759
760 set_current_state(TASK_UNINTERRUPTIBLE);
c9d5128a 761 mutex_unlock(&idmap->idmap_im_lock);
1da177e4 762 schedule();
fee7f23f 763 __set_current_state(TASK_RUNNING);
1da177e4 764 remove_wait_queue(&idmap->idmap_wq, &wq);
c9d5128a 765 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
766
767 if (im->im_status & IDMAP_STATUS_SUCCESS) {
768 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
769 goto out;
770 memcpy(name, im->im_name, len);
771 ret = len;
772 }
773
774 out:
775 memset(im, 0, sizeof(*im));
c9d5128a
IM
776 mutex_unlock(&idmap->idmap_im_lock);
777 mutex_unlock(&idmap->idmap_lock);
1da177e4
LT
778 return ret;
779}
780
1da177e4
LT
781static ssize_t
782idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
783{
369af0f1 784 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
1da177e4
LT
785 struct idmap *idmap = (struct idmap *)rpci->private;
786 struct idmap_msg im_in, *im = &idmap->idmap_im;
787 struct idmap_hashtable *h;
788 struct idmap_hashent *he = NULL;
d24aae41 789 size_t namelen_in;
1da177e4
LT
790 int ret;
791
369af0f1
CL
792 if (mlen != sizeof(im_in))
793 return -ENOSPC;
1da177e4 794
369af0f1
CL
795 if (copy_from_user(&im_in, src, mlen) != 0)
796 return -EFAULT;
1da177e4 797
c9d5128a 798 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
799
800 ret = mlen;
801 im->im_status = im_in.im_status;
802 /* If we got an error, terminate now, and wake up pending upcalls */
803 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
804 wake_up(&idmap->idmap_wq);
805 goto out;
806 }
807
808 /* Sanity checking of strings */
809 ret = -EINVAL;
810 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
811 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
812 goto out;
813
814 switch (im_in.im_type) {
815 case IDMAP_TYPE_USER:
816 h = &idmap->idmap_user_hash;
817 break;
818 case IDMAP_TYPE_GROUP:
819 h = &idmap->idmap_group_hash;
820 break;
821 default:
822 goto out;
823 }
824
825 switch (im_in.im_conv) {
826 case IDMAP_CONV_IDTONAME:
827 /* Did we match the current upcall? */
828 if (im->im_conv == IDMAP_CONV_IDTONAME
829 && im->im_type == im_in.im_type
830 && im->im_id == im_in.im_id) {
831 /* Yes: copy string, including the terminating '\0' */
832 memcpy(im->im_name, im_in.im_name, namelen_in);
833 im->im_name[namelen_in] = '\0';
834 wake_up(&idmap->idmap_wq);
835 }
836 he = idmap_alloc_id(h, im_in.im_id);
837 break;
838 case IDMAP_CONV_NAMETOID:
839 /* Did we match the current upcall? */
840 if (im->im_conv == IDMAP_CONV_NAMETOID
841 && im->im_type == im_in.im_type
842 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
843 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
844 im->im_id = im_in.im_id;
845 wake_up(&idmap->idmap_wq);
846 }
847 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
848 break;
849 default:
850 goto out;
851 }
852
853 /* If the entry is valid, also copy it to the cache */
854 if (he != NULL)
855 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
856 ret = mlen;
857out:
c9d5128a 858 mutex_unlock(&idmap->idmap_im_lock);
1da177e4
LT
859 return ret;
860}
861
75c96f85 862static void
1da177e4
LT
863idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
864{
865 struct idmap_msg *im = msg->data;
866 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
867
868 if (msg->errno >= 0)
869 return;
c9d5128a 870 mutex_lock(&idmap->idmap_im_lock);
1da177e4
LT
871 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
872 wake_up(&idmap->idmap_wq);
c9d5128a 873 mutex_unlock(&idmap->idmap_im_lock);
1da177e4
LT
874}
875
876/*
877 * Fowler/Noll/Vo hash
878 * http://www.isthe.com/chongo/tech/comp/fnv/
879 */
880
881#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
882#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
883
884static unsigned int fnvhash32(const void *buf, size_t buflen)
885{
886 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
887 unsigned int hash = FNV_1_32;
888
889 for (p = buf; p < end; p++) {
890 hash *= FNV_P_32;
891 hash ^= (unsigned int)*p;
892 }
893
369af0f1 894 return hash;
1da177e4
LT
895}
896
e4fd72a1 897int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
1da177e4 898{
e4fd72a1 899 struct idmap *idmap = server->nfs_client->cl_idmap;
e6499c6f 900 int ret = -EINVAL;
1da177e4 901
5cf36cfd
TM
902 if (nfs_map_string_to_numeric(name, namelen, uid))
903 return 0;
e6499c6f
BS
904 ret = nfs_idmap_lookup_id(name, namelen, "uid", uid);
905 if (ret < 0)
906 ret = nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
907 return ret;
1da177e4
LT
908}
909
e6499c6f 910int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
1da177e4 911{
e4fd72a1 912 struct idmap *idmap = server->nfs_client->cl_idmap;
e6499c6f 913 int ret = -EINVAL;
1da177e4 914
e6499c6f 915 if (nfs_map_string_to_numeric(name, namelen, gid))
5cf36cfd 916 return 0;
e6499c6f
BS
917 ret = nfs_idmap_lookup_id(name, namelen, "gid", gid);
918 if (ret < 0)
919 ret = nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, gid);
920 return ret;
1da177e4
LT
921}
922
e4fd72a1 923int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
1da177e4 924{
e4fd72a1 925 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 926 int ret = -EINVAL;
1da177e4 927
e6499c6f
BS
928 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
929 ret = nfs_idmap_lookup_name(uid, "user", buf, buflen);
930 if (ret < 0)
931 ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
932 }
f0b85168
TM
933 if (ret < 0)
934 ret = nfs_map_numeric_to_string(uid, buf, buflen);
935 return ret;
1da177e4 936}
e6499c6f 937int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
1da177e4 938{
e4fd72a1 939 struct idmap *idmap = server->nfs_client->cl_idmap;
b064eca2 940 int ret = -EINVAL;
1da177e4 941
e6499c6f
BS
942 if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) {
943 ret = nfs_idmap_lookup_name(gid, "group", buf, buflen);
944 if (ret < 0)
945 ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, gid, buf);
946 }
f0b85168 947 if (ret < 0)
e6499c6f 948 ret = nfs_map_numeric_to_string(gid, buf, buflen);
f0b85168 949 return ret;
1da177e4 950}
This page took 0.587356 seconds and 5 git commands to generate.