]>
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 | */ | |
5cf36cfd TM |
36 | #include <linux/types.h> |
37 | #include <linux/string.h> | |
38 | #include <linux/kernel.h> | |
39 | ||
40 | static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res) | |
41 | { | |
42 | unsigned long val; | |
43 | char buf[16]; | |
44 | ||
45 | if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf)) | |
46 | return 0; | |
47 | memcpy(buf, name, namelen); | |
48 | buf[namelen] = '\0'; | |
49 | if (strict_strtoul(buf, 0, &val) != 0) | |
50 | return 0; | |
51 | *res = val; | |
52 | return 1; | |
53 | } | |
1da177e4 | 54 | |
955a857e BS |
55 | #ifdef CONFIG_NFS_USE_NEW_IDMAPPER |
56 | ||
57 | #include <linux/slab.h> | |
58 | #include <linux/cred.h> | |
59 | #include <linux/nfs_idmap.h> | |
60 | #include <linux/keyctl.h> | |
61 | #include <linux/key-type.h> | |
62 | #include <linux/rcupdate.h> | |
955a857e BS |
63 | #include <linux/err.h> |
64 | ||
65 | #include <keys/user-type.h> | |
66 | ||
67 | #define NFS_UINT_MAXLEN 11 | |
68 | ||
69 | const struct cred *id_resolver_cache; | |
70 | ||
71 | struct key_type key_type_id_resolver = { | |
72 | .name = "id_resolver", | |
73 | .instantiate = user_instantiate, | |
74 | .match = user_match, | |
75 | .revoke = user_revoke, | |
76 | .destroy = user_destroy, | |
77 | .describe = user_describe, | |
78 | .read = user_read, | |
79 | }; | |
80 | ||
81 | int nfs_idmap_init(void) | |
82 | { | |
83 | struct cred *cred; | |
84 | struct key *keyring; | |
85 | int ret = 0; | |
86 | ||
87 | printk(KERN_NOTICE "Registering the %s key type\n", key_type_id_resolver.name); | |
88 | ||
89 | cred = prepare_kernel_cred(NULL); | |
90 | if (!cred) | |
91 | return -ENOMEM; | |
92 | ||
93 | keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred, | |
94 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | | |
95 | KEY_USR_VIEW | KEY_USR_READ, | |
96 | KEY_ALLOC_NOT_IN_QUOTA); | |
97 | if (IS_ERR(keyring)) { | |
98 | ret = PTR_ERR(keyring); | |
99 | goto failed_put_cred; | |
100 | } | |
101 | ||
102 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); | |
103 | if (ret < 0) | |
104 | goto failed_put_key; | |
105 | ||
106 | ret = register_key_type(&key_type_id_resolver); | |
107 | if (ret < 0) | |
108 | goto failed_put_key; | |
109 | ||
110 | cred->thread_keyring = keyring; | |
111 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; | |
112 | id_resolver_cache = cred; | |
113 | return 0; | |
114 | ||
115 | failed_put_key: | |
116 | key_put(keyring); | |
117 | failed_put_cred: | |
118 | put_cred(cred); | |
119 | return ret; | |
120 | } | |
121 | ||
122 | void nfs_idmap_quit(void) | |
123 | { | |
124 | key_revoke(id_resolver_cache->thread_keyring); | |
125 | unregister_key_type(&key_type_id_resolver); | |
126 | put_cred(id_resolver_cache); | |
127 | } | |
128 | ||
129 | /* | |
130 | * Assemble the description to pass to request_key() | |
131 | * This function will allocate a new string and update dest to point | |
132 | * at it. The caller is responsible for freeing dest. | |
133 | * | |
134 | * On error 0 is returned. Otherwise, the length of dest is returned. | |
135 | */ | |
136 | static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen, | |
137 | const char *type, size_t typelen, char **desc) | |
138 | { | |
139 | char *cp; | |
140 | size_t desclen = typelen + namelen + 2; | |
141 | ||
142 | *desc = kmalloc(desclen, GFP_KERNEL); | |
8f0d97b4 | 143 | if (!*desc) |
955a857e BS |
144 | return -ENOMEM; |
145 | ||
146 | cp = *desc; | |
147 | memcpy(cp, type, typelen); | |
148 | cp += typelen; | |
149 | *cp++ = ':'; | |
150 | ||
151 | memcpy(cp, name, namelen); | |
152 | cp += namelen; | |
153 | *cp = '\0'; | |
154 | return desclen; | |
155 | } | |
156 | ||
157 | static ssize_t nfs_idmap_request_key(const char *name, size_t namelen, | |
158 | const char *type, void *data, size_t data_size) | |
159 | { | |
160 | const struct cred *saved_cred; | |
161 | struct key *rkey; | |
162 | char *desc; | |
163 | struct user_key_payload *payload; | |
164 | ssize_t ret; | |
165 | ||
166 | ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc); | |
167 | if (ret <= 0) | |
168 | goto out; | |
169 | ||
170 | saved_cred = override_creds(id_resolver_cache); | |
171 | rkey = request_key(&key_type_id_resolver, desc, ""); | |
172 | revert_creds(saved_cred); | |
173 | kfree(desc); | |
174 | if (IS_ERR(rkey)) { | |
175 | ret = PTR_ERR(rkey); | |
176 | goto out; | |
177 | } | |
178 | ||
179 | rcu_read_lock(); | |
180 | rkey->perm |= KEY_USR_VIEW; | |
181 | ||
182 | ret = key_validate(rkey); | |
183 | if (ret < 0) | |
184 | goto out_up; | |
185 | ||
186 | payload = rcu_dereference(rkey->payload.data); | |
187 | if (IS_ERR_OR_NULL(payload)) { | |
188 | ret = PTR_ERR(payload); | |
189 | goto out_up; | |
190 | } | |
191 | ||
192 | ret = payload->datalen; | |
193 | if (ret > 0 && ret <= data_size) | |
194 | memcpy(data, payload->data, ret); | |
195 | else | |
196 | ret = -EINVAL; | |
197 | ||
198 | out_up: | |
199 | rcu_read_unlock(); | |
200 | key_put(rkey); | |
201 | out: | |
202 | return ret; | |
203 | } | |
204 | ||
205 | ||
206 | /* ID -> Name */ | |
207 | static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen) | |
208 | { | |
209 | char id_str[NFS_UINT_MAXLEN]; | |
210 | int id_len; | |
211 | ssize_t ret; | |
212 | ||
213 | id_len = snprintf(id_str, sizeof(id_str), "%u", id); | |
214 | ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen); | |
215 | if (ret < 0) | |
216 | return -EINVAL; | |
217 | return ret; | |
218 | } | |
219 | ||
220 | /* Name -> ID */ | |
221 | static int nfs_idmap_lookup_id(const char *name, size_t namelen, | |
222 | const char *type, __u32 *id) | |
223 | { | |
224 | char id_str[NFS_UINT_MAXLEN]; | |
225 | long id_long; | |
226 | ssize_t data_size; | |
227 | int ret = 0; | |
228 | ||
229 | data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN); | |
230 | if (data_size <= 0) { | |
231 | ret = -EINVAL; | |
232 | } else { | |
233 | ret = strict_strtol(id_str, 10, &id_long); | |
234 | *id = (__u32)id_long; | |
235 | } | |
236 | return ret; | |
237 | } | |
238 | ||
239 | int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid) | |
240 | { | |
5cf36cfd TM |
241 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
242 | return 0; | |
955a857e BS |
243 | return nfs_idmap_lookup_id(name, namelen, "uid", uid); |
244 | } | |
245 | ||
246 | int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *gid) | |
247 | { | |
5cf36cfd TM |
248 | if (nfs_map_string_to_numeric(name, namelen, gid)) |
249 | return 0; | |
955a857e BS |
250 | return nfs_idmap_lookup_id(name, namelen, "gid", gid); |
251 | } | |
252 | ||
253 | int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen) | |
254 | { | |
255 | return nfs_idmap_lookup_name(uid, "user", buf, buflen); | |
256 | } | |
257 | int nfs_map_gid_to_group(struct nfs_client *clp, __u32 gid, char *buf, size_t buflen) | |
258 | { | |
259 | return nfs_idmap_lookup_name(gid, "group", buf, buflen); | |
260 | } | |
261 | ||
5f3e97c9 | 262 | #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */ |
955a857e | 263 | |
1da177e4 | 264 | #include <linux/module.h> |
c9d5128a | 265 | #include <linux/mutex.h> |
1da177e4 | 266 | #include <linux/init.h> |
1da177e4 LT |
267 | #include <linux/slab.h> |
268 | #include <linux/socket.h> | |
269 | #include <linux/in.h> | |
270 | #include <linux/sched.h> | |
271 | ||
272 | #include <linux/sunrpc/clnt.h> | |
273 | #include <linux/workqueue.h> | |
274 | #include <linux/sunrpc/rpc_pipe_fs.h> | |
275 | ||
1da177e4 LT |
276 | #include <linux/nfs_fs.h> |
277 | ||
278 | #include <linux/nfs_idmap.h> | |
4ce79717 | 279 | #include "nfs4_fs.h" |
1da177e4 LT |
280 | |
281 | #define IDMAP_HASH_SZ 128 | |
282 | ||
58df095b TM |
283 | /* Default cache timeout is 10 minutes */ |
284 | unsigned int nfs_idmap_cache_timeout = 600 * HZ; | |
285 | ||
7d4e2747 DH |
286 | static int param_set_idmap_timeout(const char *val, struct kernel_param *kp) |
287 | { | |
288 | char *endp; | |
289 | int num = simple_strtol(val, &endp, 0); | |
290 | int jif = num * HZ; | |
291 | if (endp == val || *endp || num < 0 || jif < num) | |
292 | return -EINVAL; | |
293 | *((int *)kp->arg) = jif; | |
294 | return 0; | |
295 | } | |
296 | ||
297 | module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int, | |
298 | &nfs_idmap_cache_timeout, 0644); | |
299 | ||
1da177e4 | 300 | struct idmap_hashent { |
369af0f1 CL |
301 | unsigned long ih_expires; |
302 | __u32 ih_id; | |
d24aae41 | 303 | size_t ih_namelen; |
369af0f1 | 304 | char ih_name[IDMAP_NAMESZ]; |
1da177e4 LT |
305 | }; |
306 | ||
307 | struct idmap_hashtable { | |
369af0f1 CL |
308 | __u8 h_type; |
309 | struct idmap_hashent h_entries[IDMAP_HASH_SZ]; | |
1da177e4 LT |
310 | }; |
311 | ||
312 | struct idmap { | |
369af0f1 CL |
313 | struct dentry *idmap_dentry; |
314 | wait_queue_head_t idmap_wq; | |
315 | struct idmap_msg idmap_im; | |
316 | struct mutex idmap_lock; /* Serializes upcalls */ | |
317 | struct mutex idmap_im_lock; /* Protects the hashtable */ | |
318 | struct idmap_hashtable idmap_user_hash; | |
319 | struct idmap_hashtable idmap_group_hash; | |
1da177e4 LT |
320 | }; |
321 | ||
369af0f1 CL |
322 | static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *, |
323 | char __user *, size_t); | |
324 | static ssize_t idmap_pipe_downcall(struct file *, const char __user *, | |
325 | size_t); | |
326 | static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *); | |
1da177e4 LT |
327 | |
328 | static unsigned int fnvhash32(const void *, size_t); | |
329 | ||
b693ba4a | 330 | static const struct rpc_pipe_ops idmap_upcall_ops = { |
369af0f1 CL |
331 | .upcall = idmap_pipe_upcall, |
332 | .downcall = idmap_pipe_downcall, | |
333 | .destroy_msg = idmap_pipe_destroy_msg, | |
1da177e4 LT |
334 | }; |
335 | ||
b7162792 | 336 | int |
adfa6f98 | 337 | nfs_idmap_new(struct nfs_client *clp) |
1da177e4 LT |
338 | { |
339 | struct idmap *idmap; | |
b7162792 | 340 | int error; |
1da177e4 | 341 | |
54ceac45 | 342 | BUG_ON(clp->cl_idmap != NULL); |
b7162792 | 343 | |
369af0f1 CL |
344 | idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); |
345 | if (idmap == NULL) | |
346 | return -ENOMEM; | |
1da177e4 | 347 | |
7d217cac TM |
348 | idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry, |
349 | "idmap", idmap, &idmap_upcall_ops, 0); | |
369af0f1 | 350 | if (IS_ERR(idmap->idmap_dentry)) { |
b7162792 | 351 | error = PTR_ERR(idmap->idmap_dentry); |
1da177e4 | 352 | kfree(idmap); |
b7162792 | 353 | return error; |
1da177e4 LT |
354 | } |
355 | ||
369af0f1 CL |
356 | mutex_init(&idmap->idmap_lock); |
357 | mutex_init(&idmap->idmap_im_lock); | |
1da177e4 LT |
358 | init_waitqueue_head(&idmap->idmap_wq); |
359 | idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER; | |
360 | idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP; | |
361 | ||
362 | clp->cl_idmap = idmap; | |
b7162792 | 363 | return 0; |
1da177e4 LT |
364 | } |
365 | ||
366 | void | |
adfa6f98 | 367 | nfs_idmap_delete(struct nfs_client *clp) |
1da177e4 LT |
368 | { |
369 | struct idmap *idmap = clp->cl_idmap; | |
370 | ||
371 | if (!idmap) | |
372 | return; | |
5d67476f | 373 | rpc_unlink(idmap->idmap_dentry); |
1da177e4 LT |
374 | clp->cl_idmap = NULL; |
375 | kfree(idmap); | |
376 | } | |
377 | ||
378 | /* | |
379 | * Helper routines for manipulating the hashtable | |
380 | */ | |
381 | static inline struct idmap_hashent * | |
382 | idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len) | |
383 | { | |
384 | return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ]; | |
385 | } | |
386 | ||
387 | static struct idmap_hashent * | |
388 | idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len) | |
389 | { | |
390 | struct idmap_hashent *he = idmap_name_hash(h, name, len); | |
391 | ||
392 | if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0) | |
393 | return NULL; | |
58df095b TM |
394 | if (time_after(jiffies, he->ih_expires)) |
395 | return NULL; | |
1da177e4 LT |
396 | return he; |
397 | } | |
398 | ||
399 | static inline struct idmap_hashent * | |
400 | idmap_id_hash(struct idmap_hashtable* h, __u32 id) | |
401 | { | |
402 | return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ]; | |
403 | } | |
404 | ||
405 | static struct idmap_hashent * | |
406 | idmap_lookup_id(struct idmap_hashtable *h, __u32 id) | |
407 | { | |
408 | struct idmap_hashent *he = idmap_id_hash(h, id); | |
409 | if (he->ih_id != id || he->ih_namelen == 0) | |
410 | return NULL; | |
58df095b TM |
411 | if (time_after(jiffies, he->ih_expires)) |
412 | return NULL; | |
1da177e4 LT |
413 | return he; |
414 | } | |
415 | ||
416 | /* | |
417 | * Routines for allocating new entries in the hashtable. | |
418 | * For now, we just have 1 entry per bucket, so it's all | |
419 | * pretty trivial. | |
420 | */ | |
421 | static inline struct idmap_hashent * | |
d24aae41 | 422 | idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len) |
1da177e4 LT |
423 | { |
424 | return idmap_name_hash(h, name, len); | |
425 | } | |
426 | ||
427 | static inline struct idmap_hashent * | |
428 | idmap_alloc_id(struct idmap_hashtable *h, __u32 id) | |
429 | { | |
430 | return idmap_id_hash(h, id); | |
431 | } | |
432 | ||
433 | static void | |
434 | idmap_update_entry(struct idmap_hashent *he, const char *name, | |
435 | size_t namelen, __u32 id) | |
436 | { | |
437 | he->ih_id = id; | |
438 | memcpy(he->ih_name, name, namelen); | |
439 | he->ih_name[namelen] = '\0'; | |
440 | he->ih_namelen = namelen; | |
58df095b | 441 | he->ih_expires = jiffies + nfs_idmap_cache_timeout; |
1da177e4 LT |
442 | } |
443 | ||
444 | /* | |
445 | * Name -> ID | |
446 | */ | |
447 | static int | |
448 | nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h, | |
449 | const char *name, size_t namelen, __u32 *id) | |
450 | { | |
451 | struct rpc_pipe_msg msg; | |
452 | struct idmap_msg *im; | |
453 | struct idmap_hashent *he; | |
454 | DECLARE_WAITQUEUE(wq, current); | |
455 | int ret = -EIO; | |
456 | ||
457 | im = &idmap->idmap_im; | |
458 | ||
459 | /* | |
460 | * String sanity checks | |
461 | * Note that the userland daemon expects NUL terminated strings | |
462 | */ | |
463 | for (;;) { | |
464 | if (namelen == 0) | |
465 | return -EINVAL; | |
466 | if (name[namelen-1] != '\0') | |
467 | break; | |
468 | namelen--; | |
469 | } | |
470 | if (namelen >= IDMAP_NAMESZ) | |
471 | return -EINVAL; | |
472 | ||
c9d5128a IM |
473 | mutex_lock(&idmap->idmap_lock); |
474 | mutex_lock(&idmap->idmap_im_lock); | |
1da177e4 LT |
475 | |
476 | he = idmap_lookup_name(h, name, namelen); | |
477 | if (he != NULL) { | |
478 | *id = he->ih_id; | |
479 | ret = 0; | |
480 | goto out; | |
481 | } | |
482 | ||
483 | memset(im, 0, sizeof(*im)); | |
484 | memcpy(im->im_name, name, namelen); | |
485 | ||
486 | im->im_type = h->h_type; | |
487 | im->im_conv = IDMAP_CONV_NAMETOID; | |
488 | ||
489 | memset(&msg, 0, sizeof(msg)); | |
490 | msg.data = im; | |
491 | msg.len = sizeof(*im); | |
492 | ||
493 | add_wait_queue(&idmap->idmap_wq, &wq); | |
494 | if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { | |
495 | remove_wait_queue(&idmap->idmap_wq, &wq); | |
496 | goto out; | |
497 | } | |
498 | ||
499 | set_current_state(TASK_UNINTERRUPTIBLE); | |
c9d5128a | 500 | mutex_unlock(&idmap->idmap_im_lock); |
1da177e4 | 501 | schedule(); |
fee7f23f | 502 | __set_current_state(TASK_RUNNING); |
1da177e4 | 503 | remove_wait_queue(&idmap->idmap_wq, &wq); |
c9d5128a | 504 | mutex_lock(&idmap->idmap_im_lock); |
1da177e4 LT |
505 | |
506 | if (im->im_status & IDMAP_STATUS_SUCCESS) { | |
507 | *id = im->im_id; | |
508 | ret = 0; | |
509 | } | |
510 | ||
511 | out: | |
512 | memset(im, 0, sizeof(*im)); | |
c9d5128a IM |
513 | mutex_unlock(&idmap->idmap_im_lock); |
514 | mutex_unlock(&idmap->idmap_lock); | |
369af0f1 | 515 | return ret; |
1da177e4 LT |
516 | } |
517 | ||
518 | /* | |
519 | * ID -> Name | |
520 | */ | |
521 | static int | |
522 | nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h, | |
523 | __u32 id, char *name) | |
524 | { | |
525 | struct rpc_pipe_msg msg; | |
526 | struct idmap_msg *im; | |
527 | struct idmap_hashent *he; | |
528 | DECLARE_WAITQUEUE(wq, current); | |
529 | int ret = -EIO; | |
530 | unsigned int len; | |
531 | ||
532 | im = &idmap->idmap_im; | |
533 | ||
c9d5128a IM |
534 | mutex_lock(&idmap->idmap_lock); |
535 | mutex_lock(&idmap->idmap_im_lock); | |
1da177e4 LT |
536 | |
537 | he = idmap_lookup_id(h, id); | |
90dc7d27 | 538 | if (he) { |
1da177e4 LT |
539 | memcpy(name, he->ih_name, he->ih_namelen); |
540 | ret = he->ih_namelen; | |
541 | goto out; | |
542 | } | |
543 | ||
544 | memset(im, 0, sizeof(*im)); | |
545 | im->im_type = h->h_type; | |
546 | im->im_conv = IDMAP_CONV_IDTONAME; | |
547 | im->im_id = id; | |
548 | ||
549 | memset(&msg, 0, sizeof(msg)); | |
550 | msg.data = im; | |
551 | msg.len = sizeof(*im); | |
552 | ||
553 | add_wait_queue(&idmap->idmap_wq, &wq); | |
554 | ||
555 | if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { | |
556 | remove_wait_queue(&idmap->idmap_wq, &wq); | |
557 | goto out; | |
558 | } | |
559 | ||
560 | set_current_state(TASK_UNINTERRUPTIBLE); | |
c9d5128a | 561 | mutex_unlock(&idmap->idmap_im_lock); |
1da177e4 | 562 | schedule(); |
fee7f23f | 563 | __set_current_state(TASK_RUNNING); |
1da177e4 | 564 | remove_wait_queue(&idmap->idmap_wq, &wq); |
c9d5128a | 565 | mutex_lock(&idmap->idmap_im_lock); |
1da177e4 LT |
566 | |
567 | if (im->im_status & IDMAP_STATUS_SUCCESS) { | |
568 | if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0) | |
569 | goto out; | |
570 | memcpy(name, im->im_name, len); | |
571 | ret = len; | |
572 | } | |
573 | ||
574 | out: | |
575 | memset(im, 0, sizeof(*im)); | |
c9d5128a IM |
576 | mutex_unlock(&idmap->idmap_im_lock); |
577 | mutex_unlock(&idmap->idmap_lock); | |
1da177e4 LT |
578 | return ret; |
579 | } | |
580 | ||
581 | /* RPC pipefs upcall/downcall routines */ | |
582 | static ssize_t | |
583 | idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg, | |
369af0f1 | 584 | char __user *dst, size_t buflen) |
1da177e4 | 585 | { |
369af0f1 | 586 | char *data = (char *)msg->data + msg->copied; |
a661b77f CL |
587 | size_t mlen = min(msg->len, buflen); |
588 | unsigned long left; | |
1da177e4 | 589 | |
369af0f1 | 590 | left = copy_to_user(dst, data, mlen); |
a661b77f CL |
591 | if (left == mlen) { |
592 | msg->errno = -EFAULT; | |
593 | return -EFAULT; | |
1da177e4 | 594 | } |
a661b77f | 595 | |
1da177e4 LT |
596 | mlen -= left; |
597 | msg->copied += mlen; | |
598 | msg->errno = 0; | |
369af0f1 | 599 | return mlen; |
1da177e4 LT |
600 | } |
601 | ||
602 | static ssize_t | |
603 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) | |
604 | { | |
369af0f1 | 605 | struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); |
1da177e4 LT |
606 | struct idmap *idmap = (struct idmap *)rpci->private; |
607 | struct idmap_msg im_in, *im = &idmap->idmap_im; | |
608 | struct idmap_hashtable *h; | |
609 | struct idmap_hashent *he = NULL; | |
d24aae41 | 610 | size_t namelen_in; |
1da177e4 LT |
611 | int ret; |
612 | ||
369af0f1 CL |
613 | if (mlen != sizeof(im_in)) |
614 | return -ENOSPC; | |
1da177e4 | 615 | |
369af0f1 CL |
616 | if (copy_from_user(&im_in, src, mlen) != 0) |
617 | return -EFAULT; | |
1da177e4 | 618 | |
c9d5128a | 619 | mutex_lock(&idmap->idmap_im_lock); |
1da177e4 LT |
620 | |
621 | ret = mlen; | |
622 | im->im_status = im_in.im_status; | |
623 | /* If we got an error, terminate now, and wake up pending upcalls */ | |
624 | if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) { | |
625 | wake_up(&idmap->idmap_wq); | |
626 | goto out; | |
627 | } | |
628 | ||
629 | /* Sanity checking of strings */ | |
630 | ret = -EINVAL; | |
631 | namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ); | |
632 | if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) | |
633 | goto out; | |
634 | ||
635 | switch (im_in.im_type) { | |
636 | case IDMAP_TYPE_USER: | |
637 | h = &idmap->idmap_user_hash; | |
638 | break; | |
639 | case IDMAP_TYPE_GROUP: | |
640 | h = &idmap->idmap_group_hash; | |
641 | break; | |
642 | default: | |
643 | goto out; | |
644 | } | |
645 | ||
646 | switch (im_in.im_conv) { | |
647 | case IDMAP_CONV_IDTONAME: | |
648 | /* Did we match the current upcall? */ | |
649 | if (im->im_conv == IDMAP_CONV_IDTONAME | |
650 | && im->im_type == im_in.im_type | |
651 | && im->im_id == im_in.im_id) { | |
652 | /* Yes: copy string, including the terminating '\0' */ | |
653 | memcpy(im->im_name, im_in.im_name, namelen_in); | |
654 | im->im_name[namelen_in] = '\0'; | |
655 | wake_up(&idmap->idmap_wq); | |
656 | } | |
657 | he = idmap_alloc_id(h, im_in.im_id); | |
658 | break; | |
659 | case IDMAP_CONV_NAMETOID: | |
660 | /* Did we match the current upcall? */ | |
661 | if (im->im_conv == IDMAP_CONV_NAMETOID | |
662 | && im->im_type == im_in.im_type | |
663 | && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in | |
664 | && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) { | |
665 | im->im_id = im_in.im_id; | |
666 | wake_up(&idmap->idmap_wq); | |
667 | } | |
668 | he = idmap_alloc_name(h, im_in.im_name, namelen_in); | |
669 | break; | |
670 | default: | |
671 | goto out; | |
672 | } | |
673 | ||
674 | /* If the entry is valid, also copy it to the cache */ | |
675 | if (he != NULL) | |
676 | idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id); | |
677 | ret = mlen; | |
678 | out: | |
c9d5128a | 679 | mutex_unlock(&idmap->idmap_im_lock); |
1da177e4 LT |
680 | return ret; |
681 | } | |
682 | ||
75c96f85 | 683 | static void |
1da177e4 LT |
684 | idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg) |
685 | { | |
686 | struct idmap_msg *im = msg->data; | |
687 | struct idmap *idmap = container_of(im, struct idmap, idmap_im); | |
688 | ||
689 | if (msg->errno >= 0) | |
690 | return; | |
c9d5128a | 691 | mutex_lock(&idmap->idmap_im_lock); |
1da177e4 LT |
692 | im->im_status = IDMAP_STATUS_LOOKUPFAIL; |
693 | wake_up(&idmap->idmap_wq); | |
c9d5128a | 694 | mutex_unlock(&idmap->idmap_im_lock); |
1da177e4 LT |
695 | } |
696 | ||
697 | /* | |
698 | * Fowler/Noll/Vo hash | |
699 | * http://www.isthe.com/chongo/tech/comp/fnv/ | |
700 | */ | |
701 | ||
702 | #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */ | |
703 | #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */ | |
704 | ||
705 | static unsigned int fnvhash32(const void *buf, size_t buflen) | |
706 | { | |
707 | const unsigned char *p, *end = (const unsigned char *)buf + buflen; | |
708 | unsigned int hash = FNV_1_32; | |
709 | ||
710 | for (p = buf; p < end; p++) { | |
711 | hash *= FNV_P_32; | |
712 | hash ^= (unsigned int)*p; | |
713 | } | |
714 | ||
369af0f1 | 715 | return hash; |
1da177e4 LT |
716 | } |
717 | ||
adfa6f98 | 718 | int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid) |
1da177e4 LT |
719 | { |
720 | struct idmap *idmap = clp->cl_idmap; | |
721 | ||
5cf36cfd TM |
722 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
723 | return 0; | |
1da177e4 LT |
724 | return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid); |
725 | } | |
726 | ||
adfa6f98 | 727 | int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid) |
1da177e4 LT |
728 | { |
729 | struct idmap *idmap = clp->cl_idmap; | |
730 | ||
5cf36cfd TM |
731 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
732 | return 0; | |
1da177e4 LT |
733 | return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid); |
734 | } | |
735 | ||
955a857e | 736 | int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen) |
1da177e4 LT |
737 | { |
738 | struct idmap *idmap = clp->cl_idmap; | |
739 | ||
740 | return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf); | |
741 | } | |
955a857e | 742 | int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen) |
1da177e4 LT |
743 | { |
744 | struct idmap *idmap = clp->cl_idmap; | |
745 | ||
746 | return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf); | |
747 | } | |
748 | ||
955a857e | 749 | #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */ |