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