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