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