4 * UID and GID to name mapping for clients.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
37 #include <linux/module.h>
38 #include <linux/mutex.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/socket.h>
44 #include <linux/sched.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/workqueue.h>
48 #include <linux/sunrpc/rpc_pipe_fs.h>
50 #include <linux/nfs_fs.h>
52 #include <linux/nfs_idmap.h>
55 #define IDMAP_HASH_SZ 128
57 /* Default cache timeout is 10 minutes */
58 unsigned int nfs_idmap_cache_timeout = 600 * HZ;
60 static int param_set_idmap_timeout(const char *val, struct kernel_param *kp)
63 int num = simple_strtol(val, &endp, 0);
65 if (endp == val || *endp || num < 0 || jif < num)
67 *((int *)kp->arg) = jif;
71 module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int,
72 &nfs_idmap_cache_timeout, 0644);
74 struct idmap_hashent {
75 unsigned long ih_expires;
78 char ih_name[IDMAP_NAMESZ];
81 struct idmap_hashtable {
83 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
88 struct dentry *idmap_dentry;
89 wait_queue_head_t idmap_wq;
90 struct idmap_msg idmap_im;
91 struct mutex idmap_lock; /* Serializes upcalls */
92 struct mutex idmap_im_lock; /* Protects the hashtable */
93 struct idmap_hashtable idmap_user_hash;
94 struct idmap_hashtable idmap_group_hash;
97 static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *,
98 char __user *, size_t);
99 static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
101 static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
103 static unsigned int fnvhash32(const void *, size_t);
105 static struct rpc_pipe_ops idmap_upcall_ops = {
106 .upcall = idmap_pipe_upcall,
107 .downcall = idmap_pipe_downcall,
108 .destroy_msg = idmap_pipe_destroy_msg,
112 nfs_idmap_new(struct nfs_client *clp)
117 BUG_ON(clp->cl_idmap != NULL);
119 if ((idmap = kzalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
122 snprintf(idmap->idmap_path, sizeof(idmap->idmap_path),
123 "%s/idmap", clp->cl_rpcclient->cl_pathname);
125 idmap->idmap_dentry = rpc_mkpipe(idmap->idmap_path,
126 idmap, &idmap_upcall_ops, 0);
127 if (IS_ERR(idmap->idmap_dentry)) {
128 error = PTR_ERR(idmap->idmap_dentry);
133 mutex_init(&idmap->idmap_lock);
134 mutex_init(&idmap->idmap_im_lock);
135 init_waitqueue_head(&idmap->idmap_wq);
136 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
137 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
139 clp->cl_idmap = idmap;
144 nfs_idmap_delete(struct nfs_client *clp)
146 struct idmap *idmap = clp->cl_idmap;
150 rpc_unlink(idmap->idmap_dentry);
151 clp->cl_idmap = NULL;
156 * Helper routines for manipulating the hashtable
158 static inline struct idmap_hashent *
159 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
161 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
164 static struct idmap_hashent *
165 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
167 struct idmap_hashent *he = idmap_name_hash(h, name, len);
169 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
171 if (time_after(jiffies, he->ih_expires))
176 static inline struct idmap_hashent *
177 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
179 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
182 static struct idmap_hashent *
183 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
185 struct idmap_hashent *he = idmap_id_hash(h, id);
186 if (he->ih_id != id || he->ih_namelen == 0)
188 if (time_after(jiffies, he->ih_expires))
194 * Routines for allocating new entries in the hashtable.
195 * For now, we just have 1 entry per bucket, so it's all
198 static inline struct idmap_hashent *
199 idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
201 return idmap_name_hash(h, name, len);
204 static inline struct idmap_hashent *
205 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
207 return idmap_id_hash(h, id);
211 idmap_update_entry(struct idmap_hashent *he, const char *name,
212 size_t namelen, __u32 id)
215 memcpy(he->ih_name, name, namelen);
216 he->ih_name[namelen] = '\0';
217 he->ih_namelen = namelen;
218 he->ih_expires = jiffies + nfs_idmap_cache_timeout;
225 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
226 const char *name, size_t namelen, __u32 *id)
228 struct rpc_pipe_msg msg;
229 struct idmap_msg *im;
230 struct idmap_hashent *he;
231 DECLARE_WAITQUEUE(wq, current);
234 im = &idmap->idmap_im;
237 * String sanity checks
238 * Note that the userland daemon expects NUL terminated strings
243 if (name[namelen-1] != '\0')
247 if (namelen >= IDMAP_NAMESZ)
250 mutex_lock(&idmap->idmap_lock);
251 mutex_lock(&idmap->idmap_im_lock);
253 he = idmap_lookup_name(h, name, namelen);
260 memset(im, 0, sizeof(*im));
261 memcpy(im->im_name, name, namelen);
263 im->im_type = h->h_type;
264 im->im_conv = IDMAP_CONV_NAMETOID;
266 memset(&msg, 0, sizeof(msg));
268 msg.len = sizeof(*im);
270 add_wait_queue(&idmap->idmap_wq, &wq);
271 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
272 remove_wait_queue(&idmap->idmap_wq, &wq);
276 set_current_state(TASK_UNINTERRUPTIBLE);
277 mutex_unlock(&idmap->idmap_im_lock);
279 current->state = TASK_RUNNING;
280 remove_wait_queue(&idmap->idmap_wq, &wq);
281 mutex_lock(&idmap->idmap_im_lock);
283 if (im->im_status & IDMAP_STATUS_SUCCESS) {
289 memset(im, 0, sizeof(*im));
290 mutex_unlock(&idmap->idmap_im_lock);
291 mutex_unlock(&idmap->idmap_lock);
299 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
300 __u32 id, char *name)
302 struct rpc_pipe_msg msg;
303 struct idmap_msg *im;
304 struct idmap_hashent *he;
305 DECLARE_WAITQUEUE(wq, current);
309 im = &idmap->idmap_im;
311 mutex_lock(&idmap->idmap_lock);
312 mutex_lock(&idmap->idmap_im_lock);
314 he = idmap_lookup_id(h, id);
316 memcpy(name, he->ih_name, he->ih_namelen);
317 ret = he->ih_namelen;
321 memset(im, 0, sizeof(*im));
322 im->im_type = h->h_type;
323 im->im_conv = IDMAP_CONV_IDTONAME;
326 memset(&msg, 0, sizeof(msg));
328 msg.len = sizeof(*im);
330 add_wait_queue(&idmap->idmap_wq, &wq);
332 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
333 remove_wait_queue(&idmap->idmap_wq, &wq);
337 set_current_state(TASK_UNINTERRUPTIBLE);
338 mutex_unlock(&idmap->idmap_im_lock);
340 current->state = TASK_RUNNING;
341 remove_wait_queue(&idmap->idmap_wq, &wq);
342 mutex_lock(&idmap->idmap_im_lock);
344 if (im->im_status & IDMAP_STATUS_SUCCESS) {
345 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
347 memcpy(name, im->im_name, len);
352 memset(im, 0, sizeof(*im));
353 mutex_unlock(&idmap->idmap_im_lock);
354 mutex_unlock(&idmap->idmap_lock);
358 /* RPC pipefs upcall/downcall routines */
360 idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
361 char __user *dst, size_t buflen)
363 char *data = (char *)msg->data + msg->copied;
364 ssize_t mlen = msg->len - msg->copied;
370 left = copy_to_user(dst, data, mlen);
382 idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
384 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
385 struct idmap *idmap = (struct idmap *)rpci->private;
386 struct idmap_msg im_in, *im = &idmap->idmap_im;
387 struct idmap_hashtable *h;
388 struct idmap_hashent *he = NULL;
392 if (mlen != sizeof(im_in))
395 if (copy_from_user(&im_in, src, mlen) != 0)
398 mutex_lock(&idmap->idmap_im_lock);
401 im->im_status = im_in.im_status;
402 /* If we got an error, terminate now, and wake up pending upcalls */
403 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
404 wake_up(&idmap->idmap_wq);
408 /* Sanity checking of strings */
410 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
411 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
414 switch (im_in.im_type) {
415 case IDMAP_TYPE_USER:
416 h = &idmap->idmap_user_hash;
418 case IDMAP_TYPE_GROUP:
419 h = &idmap->idmap_group_hash;
425 switch (im_in.im_conv) {
426 case IDMAP_CONV_IDTONAME:
427 /* Did we match the current upcall? */
428 if (im->im_conv == IDMAP_CONV_IDTONAME
429 && im->im_type == im_in.im_type
430 && im->im_id == im_in.im_id) {
431 /* Yes: copy string, including the terminating '\0' */
432 memcpy(im->im_name, im_in.im_name, namelen_in);
433 im->im_name[namelen_in] = '\0';
434 wake_up(&idmap->idmap_wq);
436 he = idmap_alloc_id(h, im_in.im_id);
438 case IDMAP_CONV_NAMETOID:
439 /* Did we match the current upcall? */
440 if (im->im_conv == IDMAP_CONV_NAMETOID
441 && im->im_type == im_in.im_type
442 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
443 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
444 im->im_id = im_in.im_id;
445 wake_up(&idmap->idmap_wq);
447 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
453 /* If the entry is valid, also copy it to the cache */
455 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
458 mutex_unlock(&idmap->idmap_im_lock);
463 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
465 struct idmap_msg *im = msg->data;
466 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
470 mutex_lock(&idmap->idmap_im_lock);
471 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
472 wake_up(&idmap->idmap_wq);
473 mutex_unlock(&idmap->idmap_im_lock);
477 * Fowler/Noll/Vo hash
478 * http://www.isthe.com/chongo/tech/comp/fnv/
481 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
482 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
484 static unsigned int fnvhash32(const void *buf, size_t buflen)
486 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
487 unsigned int hash = FNV_1_32;
489 for (p = buf; p < end; p++) {
491 hash ^= (unsigned int)*p;
497 int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
499 struct idmap *idmap = clp->cl_idmap;
501 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
504 int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid)
506 struct idmap *idmap = clp->cl_idmap;
508 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
511 int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf)
513 struct idmap *idmap = clp->cl_idmap;
515 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
517 int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf)
519 struct idmap *idmap = clp->cl_idmap;
521 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);