blob: b74c4e3a64e2a435759ad2115f1af64f3a772b00 [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 */
36
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/types.h>
40#include <linux/slab.h>
41#include <linux/socket.h>
42#include <linux/in.h>
43#include <linux/sched.h>
44
45#include <linux/sunrpc/clnt.h>
46#include <linux/workqueue.h>
47#include <linux/sunrpc/rpc_pipe_fs.h>
48
49#include <linux/nfs_fs_sb.h>
50#include <linux/nfs_fs.h>
51
52#include <linux/nfs_idmap.h>
53
54#define IDMAP_HASH_SZ 128
55
56struct idmap_hashent {
57 __u32 ih_id;
58 int ih_namelen;
59 char ih_name[IDMAP_NAMESZ];
60};
61
62struct idmap_hashtable {
63 __u8 h_type;
64 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
65};
66
67struct idmap {
68 char idmap_path[48];
69 struct dentry *idmap_dentry;
70 wait_queue_head_t idmap_wq;
71 struct idmap_msg idmap_im;
72 struct semaphore idmap_lock; /* Serializes upcalls */
73 struct semaphore idmap_im_lock; /* Protects the hashtable */
74 struct idmap_hashtable idmap_user_hash;
75 struct idmap_hashtable idmap_group_hash;
76};
77
78static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
79 char __user *, size_t);
80static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
81 size_t);
82void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
83
84static unsigned int fnvhash32(const void *, size_t);
85
86static struct rpc_pipe_ops idmap_upcall_ops = {
87 .upcall = idmap_pipe_upcall,
88 .downcall = idmap_pipe_downcall,
89 .destroy_msg = idmap_pipe_destroy_msg,
90};
91
92void
93nfs_idmap_new(struct nfs4_client *clp)
94{
95 struct idmap *idmap;
96
97 if (clp->cl_idmap != NULL)
98 return;
99 if ((idmap = kmalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
100 return;
101
102 memset(idmap, 0, sizeof(*idmap));
103
104 snprintf(idmap->idmap_path, sizeof(idmap->idmap_path),
105 "%s/idmap", clp->cl_rpcclient->cl_pathname);
106
107 idmap->idmap_dentry = rpc_mkpipe(idmap->idmap_path,
108 idmap, &idmap_upcall_ops, 0);
109 if (IS_ERR(idmap->idmap_dentry)) {
110 kfree(idmap);
111 return;
112 }
113
114 init_MUTEX(&idmap->idmap_lock);
115 init_MUTEX(&idmap->idmap_im_lock);
116 init_waitqueue_head(&idmap->idmap_wq);
117 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
118 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
119
120 clp->cl_idmap = idmap;
121}
122
123void
124nfs_idmap_delete(struct nfs4_client *clp)
125{
126 struct idmap *idmap = clp->cl_idmap;
127
128 if (!idmap)
129 return;
130 rpc_unlink(idmap->idmap_path);
131 clp->cl_idmap = NULL;
132 kfree(idmap);
133}
134
135/*
136 * Helper routines for manipulating the hashtable
137 */
138static inline struct idmap_hashent *
139idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
140{
141 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
142}
143
144static struct idmap_hashent *
145idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
146{
147 struct idmap_hashent *he = idmap_name_hash(h, name, len);
148
149 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
150 return NULL;
151 return he;
152}
153
154static inline struct idmap_hashent *
155idmap_id_hash(struct idmap_hashtable* h, __u32 id)
156{
157 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
158}
159
160static struct idmap_hashent *
161idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
162{
163 struct idmap_hashent *he = idmap_id_hash(h, id);
164 if (he->ih_id != id || he->ih_namelen == 0)
165 return NULL;
166 return he;
167}
168
169/*
170 * Routines for allocating new entries in the hashtable.
171 * For now, we just have 1 entry per bucket, so it's all
172 * pretty trivial.
173 */
174static inline struct idmap_hashent *
175idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
176{
177 return idmap_name_hash(h, name, len);
178}
179
180static inline struct idmap_hashent *
181idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
182{
183 return idmap_id_hash(h, id);
184}
185
186static void
187idmap_update_entry(struct idmap_hashent *he, const char *name,
188 size_t namelen, __u32 id)
189{
190 he->ih_id = id;
191 memcpy(he->ih_name, name, namelen);
192 he->ih_name[namelen] = '\0';
193 he->ih_namelen = namelen;
194}
195
196/*
197 * Name -> ID
198 */
199static int
200nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
201 const char *name, size_t namelen, __u32 *id)
202{
203 struct rpc_pipe_msg msg;
204 struct idmap_msg *im;
205 struct idmap_hashent *he;
206 DECLARE_WAITQUEUE(wq, current);
207 int ret = -EIO;
208
209 im = &idmap->idmap_im;
210
211 /*
212 * String sanity checks
213 * Note that the userland daemon expects NUL terminated strings
214 */
215 for (;;) {
216 if (namelen == 0)
217 return -EINVAL;
218 if (name[namelen-1] != '\0')
219 break;
220 namelen--;
221 }
222 if (namelen >= IDMAP_NAMESZ)
223 return -EINVAL;
224
225 down(&idmap->idmap_lock);
226 down(&idmap->idmap_im_lock);
227
228 he = idmap_lookup_name(h, name, namelen);
229 if (he != NULL) {
230 *id = he->ih_id;
231 ret = 0;
232 goto out;
233 }
234
235 memset(im, 0, sizeof(*im));
236 memcpy(im->im_name, name, namelen);
237
238 im->im_type = h->h_type;
239 im->im_conv = IDMAP_CONV_NAMETOID;
240
241 memset(&msg, 0, sizeof(msg));
242 msg.data = im;
243 msg.len = sizeof(*im);
244
245 add_wait_queue(&idmap->idmap_wq, &wq);
246 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
247 remove_wait_queue(&idmap->idmap_wq, &wq);
248 goto out;
249 }
250
251 set_current_state(TASK_UNINTERRUPTIBLE);
252 up(&idmap->idmap_im_lock);
253 schedule();
254 current->state = TASK_RUNNING;
255 remove_wait_queue(&idmap->idmap_wq, &wq);
256 down(&idmap->idmap_im_lock);
257
258 if (im->im_status & IDMAP_STATUS_SUCCESS) {
259 *id = im->im_id;
260 ret = 0;
261 }
262
263 out:
264 memset(im, 0, sizeof(*im));
265 up(&idmap->idmap_im_lock);
266 up(&idmap->idmap_lock);
267 return (ret);
268}
269
270/*
271 * ID -> Name
272 */
273static int
274nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
275 __u32 id, char *name)
276{
277 struct rpc_pipe_msg msg;
278 struct idmap_msg *im;
279 struct idmap_hashent *he;
280 DECLARE_WAITQUEUE(wq, current);
281 int ret = -EIO;
282 unsigned int len;
283
284 im = &idmap->idmap_im;
285
286 down(&idmap->idmap_lock);
287 down(&idmap->idmap_im_lock);
288
289 he = idmap_lookup_id(h, id);
290 if (he != 0) {
291 memcpy(name, he->ih_name, he->ih_namelen);
292 ret = he->ih_namelen;
293 goto out;
294 }
295
296 memset(im, 0, sizeof(*im));
297 im->im_type = h->h_type;
298 im->im_conv = IDMAP_CONV_IDTONAME;
299 im->im_id = id;
300
301 memset(&msg, 0, sizeof(msg));
302 msg.data = im;
303 msg.len = sizeof(*im);
304
305 add_wait_queue(&idmap->idmap_wq, &wq);
306
307 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
308 remove_wait_queue(&idmap->idmap_wq, &wq);
309 goto out;
310 }
311
312 set_current_state(TASK_UNINTERRUPTIBLE);
313 up(&idmap->idmap_im_lock);
314 schedule();
315 current->state = TASK_RUNNING;
316 remove_wait_queue(&idmap->idmap_wq, &wq);
317 down(&idmap->idmap_im_lock);
318
319 if (im->im_status & IDMAP_STATUS_SUCCESS) {
320 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
321 goto out;
322 memcpy(name, im->im_name, len);
323 ret = len;
324 }
325
326 out:
327 memset(im, 0, sizeof(*im));
328 up(&idmap->idmap_im_lock);
329 up(&idmap->idmap_lock);
330 return ret;
331}
332
333/* RPC pipefs upcall/downcall routines */
334static ssize_t
335idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
336 char __user *dst, size_t buflen)
337{
338 char *data = (char *)msg->data + msg->copied;
339 ssize_t mlen = msg->len - msg->copied;
340 ssize_t left;
341
342 if (mlen > buflen)
343 mlen = buflen;
344
345 left = copy_to_user(dst, data, mlen);
346 if (left < 0) {
347 msg->errno = left;
348 return left;
349 }
350 mlen -= left;
351 msg->copied += mlen;
352 msg->errno = 0;
353 return mlen;
354}
355
356static ssize_t
357idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
358{
359 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
360 struct idmap *idmap = (struct idmap *)rpci->private;
361 struct idmap_msg im_in, *im = &idmap->idmap_im;
362 struct idmap_hashtable *h;
363 struct idmap_hashent *he = NULL;
364 int namelen_in;
365 int ret;
366
367 if (mlen != sizeof(im_in))
368 return (-ENOSPC);
369
370 if (copy_from_user(&im_in, src, mlen) != 0)
371 return (-EFAULT);
372
373 down(&idmap->idmap_im_lock);
374
375 ret = mlen;
376 im->im_status = im_in.im_status;
377 /* If we got an error, terminate now, and wake up pending upcalls */
378 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
379 wake_up(&idmap->idmap_wq);
380 goto out;
381 }
382
383 /* Sanity checking of strings */
384 ret = -EINVAL;
385 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
386 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
387 goto out;
388
389 switch (im_in.im_type) {
390 case IDMAP_TYPE_USER:
391 h = &idmap->idmap_user_hash;
392 break;
393 case IDMAP_TYPE_GROUP:
394 h = &idmap->idmap_group_hash;
395 break;
396 default:
397 goto out;
398 }
399
400 switch (im_in.im_conv) {
401 case IDMAP_CONV_IDTONAME:
402 /* Did we match the current upcall? */
403 if (im->im_conv == IDMAP_CONV_IDTONAME
404 && im->im_type == im_in.im_type
405 && im->im_id == im_in.im_id) {
406 /* Yes: copy string, including the terminating '\0' */
407 memcpy(im->im_name, im_in.im_name, namelen_in);
408 im->im_name[namelen_in] = '\0';
409 wake_up(&idmap->idmap_wq);
410 }
411 he = idmap_alloc_id(h, im_in.im_id);
412 break;
413 case IDMAP_CONV_NAMETOID:
414 /* Did we match the current upcall? */
415 if (im->im_conv == IDMAP_CONV_NAMETOID
416 && im->im_type == im_in.im_type
417 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
418 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
419 im->im_id = im_in.im_id;
420 wake_up(&idmap->idmap_wq);
421 }
422 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
423 break;
424 default:
425 goto out;
426 }
427
428 /* If the entry is valid, also copy it to the cache */
429 if (he != NULL)
430 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
431 ret = mlen;
432out:
433 up(&idmap->idmap_im_lock);
434 return ret;
435}
436
437void
438idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
439{
440 struct idmap_msg *im = msg->data;
441 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
442
443 if (msg->errno >= 0)
444 return;
445 down(&idmap->idmap_im_lock);
446 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
447 wake_up(&idmap->idmap_wq);
448 up(&idmap->idmap_im_lock);
449}
450
451/*
452 * Fowler/Noll/Vo hash
453 * http://www.isthe.com/chongo/tech/comp/fnv/
454 */
455
456#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
457#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
458
459static unsigned int fnvhash32(const void *buf, size_t buflen)
460{
461 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
462 unsigned int hash = FNV_1_32;
463
464 for (p = buf; p < end; p++) {
465 hash *= FNV_P_32;
466 hash ^= (unsigned int)*p;
467 }
468
469 return (hash);
470}
471
472int nfs_map_name_to_uid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
473{
474 struct idmap *idmap = clp->cl_idmap;
475
476 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
477}
478
479int nfs_map_group_to_gid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
480{
481 struct idmap *idmap = clp->cl_idmap;
482
483 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
484}
485
486int nfs_map_uid_to_name(struct nfs4_client *clp, __u32 uid, char *buf)
487{
488 struct idmap *idmap = clp->cl_idmap;
489
490 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
491}
492int nfs_map_gid_to_group(struct nfs4_client *clp, __u32 uid, char *buf)
493{
494 struct idmap *idmap = clp->cl_idmap;
495
496 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
497}
498