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