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