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