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/init.h>
39 #include <linux/types.h>
40 #include <linux/slab.h>
41 #include <linux/socket.h>
43 #include <linux/sched.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/workqueue.h>
47 #include <linux/sunrpc/rpc_pipe_fs.h>
49 #include <linux/nfs_fs_sb.h>
50 #include <linux/nfs_fs.h>
52 #include <linux/nfs_idmap.h>
54 #define IDMAP_HASH_SZ 128
56 struct idmap_hashent {
59 char ih_name[IDMAP_NAMESZ];
62 struct idmap_hashtable {
64 struct idmap_hashent h_entries[IDMAP_HASH_SZ];
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;
78 static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *, char *,
80 static ssize_t idmap_pipe_downcall(struct file *, const char *, size_t);
81 void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
83 static unsigned int fnvhash32(const void *, size_t);
85 static struct rpc_pipe_ops idmap_upcall_ops = {
86 .upcall = idmap_pipe_upcall,
87 .downcall = idmap_pipe_downcall,
88 .destroy_msg = idmap_pipe_destroy_msg,
92 nfs_idmap_new(struct nfs4_client *clp)
96 if (clp->cl_idmap != NULL)
98 if ((idmap = kmalloc(sizeof(*idmap), GFP_KERNEL)) == NULL)
101 memset(idmap, 0, sizeof(*idmap));
103 snprintf(idmap->idmap_path, sizeof(idmap->idmap_path),
104 "%s/idmap", clp->cl_rpcclient->cl_pathname);
106 idmap->idmap_dentry = rpc_mkpipe(idmap->idmap_path,
107 idmap, &idmap_upcall_ops, 0);
108 if (IS_ERR(idmap->idmap_dentry)) {
113 init_MUTEX(&idmap->idmap_lock);
114 init_MUTEX(&idmap->idmap_im_lock);
115 init_waitqueue_head(&idmap->idmap_wq);
116 idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER;
117 idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP;
119 clp->cl_idmap = idmap;
123 nfs_idmap_delete(struct nfs4_client *clp)
125 struct idmap *idmap = clp->cl_idmap;
129 rpc_unlink(idmap->idmap_path);
130 clp->cl_idmap = NULL;
135 * Helper routines for manipulating the hashtable
137 static inline struct idmap_hashent *
138 idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len)
140 return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ];
143 static struct idmap_hashent *
144 idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len)
146 struct idmap_hashent *he = idmap_name_hash(h, name, len);
148 if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0)
153 static inline struct idmap_hashent *
154 idmap_id_hash(struct idmap_hashtable* h, __u32 id)
156 return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ];
159 static struct idmap_hashent *
160 idmap_lookup_id(struct idmap_hashtable *h, __u32 id)
162 struct idmap_hashent *he = idmap_id_hash(h, id);
163 if (he->ih_id != id || he->ih_namelen == 0)
169 * Routines for allocating new entries in the hashtable.
170 * For now, we just have 1 entry per bucket, so it's all
173 static inline struct idmap_hashent *
174 idmap_alloc_name(struct idmap_hashtable *h, char *name, unsigned len)
176 return idmap_name_hash(h, name, len);
179 static inline struct idmap_hashent *
180 idmap_alloc_id(struct idmap_hashtable *h, __u32 id)
182 return idmap_id_hash(h, id);
186 idmap_update_entry(struct idmap_hashent *he, const char *name,
187 size_t namelen, __u32 id)
190 memcpy(he->ih_name, name, namelen);
191 he->ih_name[namelen] = '\0';
192 he->ih_namelen = namelen;
199 nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h,
200 const char *name, size_t namelen, __u32 *id)
202 struct rpc_pipe_msg msg;
203 struct idmap_msg *im;
204 struct idmap_hashent *he;
205 DECLARE_WAITQUEUE(wq, current);
208 im = &idmap->idmap_im;
211 * String sanity checks
212 * Note that the userland daemon expects NUL terminated strings
217 if (name[namelen-1] != '\0')
221 if (namelen >= IDMAP_NAMESZ)
224 down(&idmap->idmap_lock);
225 down(&idmap->idmap_im_lock);
227 he = idmap_lookup_name(h, name, namelen);
234 memset(im, 0, sizeof(*im));
235 memcpy(im->im_name, name, namelen);
237 im->im_type = h->h_type;
238 im->im_conv = IDMAP_CONV_NAMETOID;
240 memset(&msg, 0, sizeof(msg));
242 msg.len = sizeof(*im);
244 add_wait_queue(&idmap->idmap_wq, &wq);
245 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
246 remove_wait_queue(&idmap->idmap_wq, &wq);
250 set_current_state(TASK_UNINTERRUPTIBLE);
251 up(&idmap->idmap_im_lock);
253 current->state = TASK_RUNNING;
254 remove_wait_queue(&idmap->idmap_wq, &wq);
255 down(&idmap->idmap_im_lock);
257 if (im->im_status & IDMAP_STATUS_SUCCESS) {
263 memset(im, 0, sizeof(*im));
264 up(&idmap->idmap_im_lock);
265 up(&idmap->idmap_lock);
273 nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h,
274 __u32 id, char *name)
276 struct rpc_pipe_msg msg;
277 struct idmap_msg *im;
278 struct idmap_hashent *he;
279 DECLARE_WAITQUEUE(wq, current);
283 im = &idmap->idmap_im;
285 down(&idmap->idmap_lock);
286 down(&idmap->idmap_im_lock);
288 he = idmap_lookup_id(h, id);
290 memcpy(name, he->ih_name, he->ih_namelen);
291 ret = he->ih_namelen;
295 memset(im, 0, sizeof(*im));
296 im->im_type = h->h_type;
297 im->im_conv = IDMAP_CONV_IDTONAME;
300 memset(&msg, 0, sizeof(msg));
302 msg.len = sizeof(*im);
304 add_wait_queue(&idmap->idmap_wq, &wq);
306 if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) {
307 remove_wait_queue(&idmap->idmap_wq, &wq);
311 set_current_state(TASK_UNINTERRUPTIBLE);
312 up(&idmap->idmap_im_lock);
314 current->state = TASK_RUNNING;
315 remove_wait_queue(&idmap->idmap_wq, &wq);
316 down(&idmap->idmap_im_lock);
318 if (im->im_status & IDMAP_STATUS_SUCCESS) {
319 if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0)
321 memcpy(name, im->im_name, len);
326 memset(im, 0, sizeof(*im));
327 up(&idmap->idmap_im_lock);
328 up(&idmap->idmap_lock);
332 /* RPC pipefs upcall/downcall routines */
334 idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
335 char *dst, size_t buflen)
337 char *data = (char *)msg->data + msg->copied;
338 ssize_t mlen = msg->len - msg->copied;
344 left = copy_to_user(dst, data, mlen);
356 idmap_pipe_downcall(struct file *filp, const char *src, size_t mlen)
358 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
359 struct idmap *idmap = (struct idmap *)rpci->private;
360 struct idmap_msg im_in, *im = &idmap->idmap_im;
361 struct idmap_hashtable *h;
362 struct idmap_hashent *he = NULL;
366 if (mlen != sizeof(im_in))
369 if (copy_from_user(&im_in, src, mlen) != 0)
372 down(&idmap->idmap_im_lock);
375 im->im_status = im_in.im_status;
376 /* If we got an error, terminate now, and wake up pending upcalls */
377 if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) {
378 wake_up(&idmap->idmap_wq);
382 /* Sanity checking of strings */
384 namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ);
385 if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ)
388 switch (im_in.im_type) {
389 case IDMAP_TYPE_USER:
390 h = &idmap->idmap_user_hash;
392 case IDMAP_TYPE_GROUP:
393 h = &idmap->idmap_group_hash;
399 switch (im_in.im_conv) {
400 case IDMAP_CONV_IDTONAME:
401 /* Did we match the current upcall? */
402 if (im->im_conv == IDMAP_CONV_IDTONAME
403 && im->im_type == im_in.im_type
404 && im->im_id == im_in.im_id) {
405 /* Yes: copy string, including the terminating '\0' */
406 memcpy(im->im_name, im_in.im_name, namelen_in);
407 im->im_name[namelen_in] = '\0';
408 wake_up(&idmap->idmap_wq);
410 he = idmap_alloc_id(h, im_in.im_id);
412 case IDMAP_CONV_NAMETOID:
413 /* Did we match the current upcall? */
414 if (im->im_conv == IDMAP_CONV_NAMETOID
415 && im->im_type == im_in.im_type
416 && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in
417 && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) {
418 im->im_id = im_in.im_id;
419 wake_up(&idmap->idmap_wq);
421 he = idmap_alloc_name(h, im_in.im_name, namelen_in);
427 /* If the entry is valid, also copy it to the cache */
429 idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id);
432 up(&idmap->idmap_im_lock);
437 idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
439 struct idmap_msg *im = msg->data;
440 struct idmap *idmap = container_of(im, struct idmap, idmap_im);
444 down(&idmap->idmap_im_lock);
445 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
446 wake_up(&idmap->idmap_wq);
447 up(&idmap->idmap_im_lock);
451 * Fowler/Noll/Vo hash
452 * http://www.isthe.com/chongo/tech/comp/fnv/
455 #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */
456 #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */
458 static unsigned int fnvhash32(const void *buf, size_t buflen)
460 const unsigned char *p, *end = (const unsigned char *)buf + buflen;
461 unsigned int hash = FNV_1_32;
463 for (p = buf; p < end; p++) {
465 hash ^= (unsigned int)*p;
471 int nfs_map_name_to_uid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
473 struct idmap *idmap = clp->cl_idmap;
475 return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid);
478 int nfs_map_group_to_gid(struct nfs4_client *clp, const char *name, size_t namelen, __u32 *uid)
480 struct idmap *idmap = clp->cl_idmap;
482 return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid);
485 int nfs_map_uid_to_name(struct nfs4_client *clp, __u32 uid, char *buf)
487 struct idmap *idmap = clp->cl_idmap;
489 return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf);
491 int nfs_map_gid_to_group(struct nfs4_client *clp, __u32 uid, char *buf)
493 struct idmap *idmap = clp->cl_idmap;
495 return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf);