blob: 07a5dd57646e3c06ef9aec1c3663e676c7852d0a [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 Myklebust5d674762006-07-31 14:11:48 -0700133 rpc_unlink(idmap->idmap_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 clp->cl_idmap = NULL;
135 kfree(idmap);
136}
137
138/*
139 * Helper routines for manipulating the hashtable
140 */
141static inline struct idmap_hashent *
142idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
143{
144 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
145}
146
147static struct idmap_hashent *
148idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
149{
150 struct idmap_hashent *he = idmap_name_hash(h, name, len);
151
152 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
153 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100154 if (time_after(jiffies, he->ih_expires))
155 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return he;
157}
158
159static inline struct idmap_hashent *
160idmap_id_hash(struct idmap_hashtable* h, __u32 id)
161{
162 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
163}
164
165static struct idmap_hashent *
166idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
167{
168 struct idmap_hashent *he = idmap_id_hash(h, id);
169 if (he->ih_id != id || he->ih_namelen == 0)
170 return NULL;
Trond Myklebust58df0952006-01-03 09:55:57 +0100171 if (time_after(jiffies, he->ih_expires))
172 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return he;
174}
175
176/*
177 * Routines for allocating new entries in the hashtable.
178 * For now, we just have 1 entry per bucket, so it's all
179 * pretty trivial.
180 */
181static inline struct idmap_hashent *
182idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
183{
184 return idmap_name_hash(h, name, len);
185}
186
187static inline struct idmap_hashent *
188idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
189{
190 return idmap_id_hash(h, id);
191}
192
193static void
194idmap_update_entry(struct idmap_hashent *he, const char *name,
195 size_t namelen, __u32 id)
196{
197 he->ih_id = id;
198 memcpy(he->ih_name, name, namelen);
199 he->ih_name[namelen] = '\0';
200 he->ih_namelen = namelen;
Trond Myklebust58df0952006-01-03 09:55:57 +0100201 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
205 * Name -> ID
206 */
207static int
208nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
209 const char *name, size_t namelen, __u32 *id)
210{
211 struct rpc_pipe_msg msg;
212 struct idmap_msg *im;
213 struct idmap_hashent *he;
214 DECLARE_WAITQUEUE(wq, current);
215 int ret = -EIO;
216
217 im = &idmap->idmap_im;
218
219 /*
220 * String sanity checks
221 * Note that the userland daemon expects NUL terminated strings
222 */
223 for (;;) {
224 if (namelen == 0)
225 return -EINVAL;
226 if (name[namelen-1] != '\0')
227 break;
228 namelen--;
229 }
230 if (namelen >= IDMAP_NAMESZ)
231 return -EINVAL;
232
Ingo Molnarc9d51282006-03-20 13:44:11 -0500233 mutex_lock(&idmap->idmap_lock);
234 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 he = idmap_lookup_name(h, name, namelen);
237 if (he != NULL) {
238 *id = he->ih_id;
239 ret = 0;
240 goto out;
241 }
242
243 memset(im, 0, sizeof(*im));
244 memcpy(im->im_name, name, namelen);
245
246 im->im_type = h->h_type;
247 im->im_conv = IDMAP_CONV_NAMETOID;
248
249 memset(&msg, 0, sizeof(msg));
250 msg.data = im;
251 msg.len = sizeof(*im);
252
253 add_wait_queue(&idmap->idmap_wq, &wq);
254 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
255 remove_wait_queue(&idmap->idmap_wq, &wq);
256 goto out;
257 }
258
259 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500260 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 schedule();
262 current->state = TASK_RUNNING;
263 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500264 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (im->im_status & IDMAP_STATUS_SUCCESS) {
267 *id = im->im_id;
268 ret = 0;
269 }
270
271 out:
272 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500273 mutex_unlock(&idmap->idmap_im_lock);
274 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return (ret);
276}
277
278/*
279 * ID -> Name
280 */
281static int
282nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
283 __u32 id, char *name)
284{
285 struct rpc_pipe_msg msg;
286 struct idmap_msg *im;
287 struct idmap_hashent *he;
288 DECLARE_WAITQUEUE(wq, current);
289 int ret = -EIO;
290 unsigned int len;
291
292 im = &idmap->idmap_im;
293
Ingo Molnarc9d51282006-03-20 13:44:11 -0500294 mutex_lock(&idmap->idmap_lock);
295 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 he = idmap_lookup_id(h, id);
298 if (he != 0) {
299 memcpy(name, he->ih_name, he->ih_namelen);
300 ret = he->ih_namelen;
301 goto out;
302 }
303
304 memset(im, 0, sizeof(*im));
305 im->im_type = h->h_type;
306 im->im_conv = IDMAP_CONV_IDTONAME;
307 im->im_id = id;
308
309 memset(&msg, 0, sizeof(msg));
310 msg.data = im;
311 msg.len = sizeof(*im);
312
313 add_wait_queue(&idmap->idmap_wq, &wq);
314
315 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
316 remove_wait_queue(&idmap->idmap_wq, &wq);
317 goto out;
318 }
319
320 set_current_state(TASK_UNINTERRUPTIBLE);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500321 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 schedule();
323 current->state = TASK_RUNNING;
324 remove_wait_queue(&idmap->idmap_wq, &wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500325 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (im->im_status & IDMAP_STATUS_SUCCESS) {
328 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
329 goto out;
330 memcpy(name, im->im_name, len);
331 ret = len;
332 }
333
334 out:
335 memset(im, 0, sizeof(*im));
Ingo Molnarc9d51282006-03-20 13:44:11 -0500336 mutex_unlock(&idmap->idmap_im_lock);
337 mutex_unlock(&idmap->idmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return ret;
339}
340
341/* RPC pipefs upcall/downcall routines */
342static ssize_t
343idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
344 char __user *dst, size_t buflen)
345{
346 char *data = (char *)msg->data + msg->copied;
347 ssize_t mlen = msg->len - msg->copied;
348 ssize_t left;
349
350 if (mlen > buflen)
351 mlen = buflen;
352
353 left = copy_to_user(dst, data, mlen);
354 if (left < 0) {
355 msg->errno = left;
356 return left;
357 }
358 mlen -= left;
359 msg->copied += mlen;
360 msg->errno = 0;
361 return mlen;
362}
363
364static ssize_t
365idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
366{
367 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
368 struct idmap *idmap = (struct idmap *)rpci->private;
369 struct idmap_msg im_in, *im = &idmap->idmap_im;
370 struct idmap_hashtable *h;
371 struct idmap_hashent *he = NULL;
372 int namelen_in;
373 int ret;
374
375 if (mlen != sizeof(im_in))
376 return (-ENOSPC);
377
378 if (copy_from_user(&im_in, src, mlen) != 0)
379 return (-EFAULT);
380
Ingo Molnarc9d51282006-03-20 13:44:11 -0500381 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 ret = mlen;
384 im->im_status = im_in.im_status;
385 /* If we got an error, terminate now, and wake up pending upcalls */
386 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
387 wake_up(&idmap->idmap_wq);
388 goto out;
389 }
390
391 /* Sanity checking of strings */
392 ret = -EINVAL;
393 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
394 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
395 goto out;
396
397 switch (im_in.im_type) {
398 case IDMAP_TYPE_USER:
399 h = &idmap->idmap_user_hash;
400 break;
401 case IDMAP_TYPE_GROUP:
402 h = &idmap->idmap_group_hash;
403 break;
404 default:
405 goto out;
406 }
407
408 switch (im_in.im_conv) {
409 case IDMAP_CONV_IDTONAME:
410 /* Did we match the current upcall? */
411 if (im->im_conv == IDMAP_CONV_IDTONAME
412 && im->im_type == im_in.im_type
413 && im->im_id == im_in.im_id) {
414 /* Yes: copy string, including the terminating '\0' */
415 memcpy(im->im_name, im_in.im_name, namelen_in);
416 im->im_name[namelen_in] = '\0';
417 wake_up(&idmap->idmap_wq);
418 }
419 he = idmap_alloc_id(h, im_in.im_id);
420 break;
421 case IDMAP_CONV_NAMETOID:
422 /* Did we match the current upcall? */
423 if (im->im_conv == IDMAP_CONV_NAMETOID
424 && im->im_type == im_in.im_type
425 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
426 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
427 im->im_id = im_in.im_id;
428 wake_up(&idmap->idmap_wq);
429 }
430 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
431 break;
432 default:
433 goto out;
434 }
435
436 /* If the entry is valid, also copy it to the cache */
437 if (he != NULL)
438 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
439 ret = mlen;
440out:
Ingo Molnarc9d51282006-03-20 13:44:11 -0500441 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ret;
443}
444
Adrian Bunk75c96f82005-05-05 16:16:09 -0700445static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
447{
448 struct idmap_msg *im = msg->data;
449 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
450
451 if (msg->errno >= 0)
452 return;
Ingo Molnarc9d51282006-03-20 13:44:11 -0500453 mutex_lock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
455 wake_up(&idmap->idmap_wq);
Ingo Molnarc9d51282006-03-20 13:44:11 -0500456 mutex_unlock(&idmap->idmap_im_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459/*
460 * Fowler/Noll/Vo hash
461 * http://www.isthe.com/chongo/tech/comp/fnv/
462 */
463
464#define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
465#define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
466
467static unsigned int fnvhash32(const void *buf, size_t buflen)
468{
469 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
470 unsigned int hash = FNV_1_32;
471
472 for (p = buf; p < end; p++) {
473 hash *= FNV_P_32;
474 hash ^= (unsigned int)*p;
475 }
476
477 return (hash);
478}
479
480int nfs_map_name_to_uid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
481{
482 struct idmap *idmap = clp->cl_idmap;
483
484 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
485}
486
487int nfs_map_group_to_gid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
488{
489 struct idmap *idmap = clp->cl_idmap;
490
491 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
492}
493
494int nfs_map_uid_to_name(struct nfs4_client *clp, __u32 uid, char *buf)
495{
496 struct idmap *idmap = clp->cl_idmap;
497
498 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
499}
500int nfs_map_gid_to_group(struct nfs4_client *clp, __u32 uid, char *buf)
501{
502 struct idmap *idmap = clp->cl_idmap;
503
504 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);
505}
506