| 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 | #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 | } |
| 54 | |
| 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> |
| 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); |
| 143 | if (!*desc) |
| 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 | { |
| 241 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 242 | return 0; |
| 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 | { |
| 248 | if (nfs_map_string_to_numeric(name, namelen, gid)) |
| 249 | return 0; |
| 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 | |
| 262 | #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */ |
| 263 | |
| 264 | #include <linux/module.h> |
| 265 | #include <linux/mutex.h> |
| 266 | #include <linux/init.h> |
| 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 | |
| 276 | #include <linux/nfs_fs.h> |
| 277 | |
| 278 | #include <linux/nfs_idmap.h> |
| 279 | #include "nfs4_fs.h" |
| 280 | |
| 281 | #define IDMAP_HASH_SZ 128 |
| 282 | |
| 283 | /* Default cache timeout is 10 minutes */ |
| 284 | unsigned int nfs_idmap_cache_timeout = 600 * HZ; |
| 285 | |
| 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 | |
| 300 | struct idmap_hashent { |
| 301 | unsigned long ih_expires; |
| 302 | __u32 ih_id; |
| 303 | size_t ih_namelen; |
| 304 | char ih_name[IDMAP_NAMESZ]; |
| 305 | }; |
| 306 | |
| 307 | struct idmap_hashtable { |
| 308 | __u8 h_type; |
| 309 | struct idmap_hashent h_entries[IDMAP_HASH_SZ]; |
| 310 | }; |
| 311 | |
| 312 | struct idmap { |
| 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; |
| 320 | }; |
| 321 | |
| 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 *); |
| 327 | |
| 328 | static unsigned int fnvhash32(const void *, size_t); |
| 329 | |
| 330 | static const struct rpc_pipe_ops idmap_upcall_ops = { |
| 331 | .upcall = idmap_pipe_upcall, |
| 332 | .downcall = idmap_pipe_downcall, |
| 333 | .destroy_msg = idmap_pipe_destroy_msg, |
| 334 | }; |
| 335 | |
| 336 | int |
| 337 | nfs_idmap_new(struct nfs_client *clp) |
| 338 | { |
| 339 | struct idmap *idmap; |
| 340 | int error; |
| 341 | |
| 342 | BUG_ON(clp->cl_idmap != NULL); |
| 343 | |
| 344 | idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); |
| 345 | if (idmap == NULL) |
| 346 | return -ENOMEM; |
| 347 | |
| 348 | idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry, |
| 349 | "idmap", idmap, &idmap_upcall_ops, 0); |
| 350 | if (IS_ERR(idmap->idmap_dentry)) { |
| 351 | error = PTR_ERR(idmap->idmap_dentry); |
| 352 | kfree(idmap); |
| 353 | return error; |
| 354 | } |
| 355 | |
| 356 | mutex_init(&idmap->idmap_lock); |
| 357 | mutex_init(&idmap->idmap_im_lock); |
| 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; |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | nfs_idmap_delete(struct nfs_client *clp) |
| 368 | { |
| 369 | struct idmap *idmap = clp->cl_idmap; |
| 370 | |
| 371 | if (!idmap) |
| 372 | return; |
| 373 | rpc_unlink(idmap->idmap_dentry); |
| 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; |
| 394 | if (time_after(jiffies, he->ih_expires)) |
| 395 | return NULL; |
| 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; |
| 411 | if (time_after(jiffies, he->ih_expires)) |
| 412 | return NULL; |
| 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 * |
| 422 | idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len) |
| 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; |
| 441 | he->ih_expires = jiffies + nfs_idmap_cache_timeout; |
| 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 | |
| 473 | mutex_lock(&idmap->idmap_lock); |
| 474 | mutex_lock(&idmap->idmap_im_lock); |
| 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); |
| 500 | mutex_unlock(&idmap->idmap_im_lock); |
| 501 | schedule(); |
| 502 | __set_current_state(TASK_RUNNING); |
| 503 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 504 | mutex_lock(&idmap->idmap_im_lock); |
| 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)); |
| 513 | mutex_unlock(&idmap->idmap_im_lock); |
| 514 | mutex_unlock(&idmap->idmap_lock); |
| 515 | return ret; |
| 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 | |
| 534 | mutex_lock(&idmap->idmap_lock); |
| 535 | mutex_lock(&idmap->idmap_im_lock); |
| 536 | |
| 537 | he = idmap_lookup_id(h, id); |
| 538 | if (he) { |
| 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); |
| 561 | mutex_unlock(&idmap->idmap_im_lock); |
| 562 | schedule(); |
| 563 | __set_current_state(TASK_RUNNING); |
| 564 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 565 | mutex_lock(&idmap->idmap_im_lock); |
| 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)); |
| 576 | mutex_unlock(&idmap->idmap_im_lock); |
| 577 | mutex_unlock(&idmap->idmap_lock); |
| 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, |
| 584 | char __user *dst, size_t buflen) |
| 585 | { |
| 586 | char *data = (char *)msg->data + msg->copied; |
| 587 | size_t mlen = min(msg->len, buflen); |
| 588 | unsigned long left; |
| 589 | |
| 590 | left = copy_to_user(dst, data, mlen); |
| 591 | if (left == mlen) { |
| 592 | msg->errno = -EFAULT; |
| 593 | return -EFAULT; |
| 594 | } |
| 595 | |
| 596 | mlen -= left; |
| 597 | msg->copied += mlen; |
| 598 | msg->errno = 0; |
| 599 | return mlen; |
| 600 | } |
| 601 | |
| 602 | static ssize_t |
| 603 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) |
| 604 | { |
| 605 | struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); |
| 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; |
| 610 | size_t namelen_in; |
| 611 | int ret; |
| 612 | |
| 613 | if (mlen != sizeof(im_in)) |
| 614 | return -ENOSPC; |
| 615 | |
| 616 | if (copy_from_user(&im_in, src, mlen) != 0) |
| 617 | return -EFAULT; |
| 618 | |
| 619 | mutex_lock(&idmap->idmap_im_lock); |
| 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: |
| 679 | mutex_unlock(&idmap->idmap_im_lock); |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static void |
| 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; |
| 691 | mutex_lock(&idmap->idmap_im_lock); |
| 692 | im->im_status = IDMAP_STATUS_LOOKUPFAIL; |
| 693 | wake_up(&idmap->idmap_wq); |
| 694 | mutex_unlock(&idmap->idmap_im_lock); |
| 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 | |
| 715 | return hash; |
| 716 | } |
| 717 | |
| 718 | int nfs_map_name_to_uid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid) |
| 719 | { |
| 720 | struct idmap *idmap = clp->cl_idmap; |
| 721 | |
| 722 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 723 | return 0; |
| 724 | return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid); |
| 725 | } |
| 726 | |
| 727 | int nfs_map_group_to_gid(struct nfs_client *clp, const char *name, size_t namelen, __u32 *uid) |
| 728 | { |
| 729 | struct idmap *idmap = clp->cl_idmap; |
| 730 | |
| 731 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 732 | return 0; |
| 733 | return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid); |
| 734 | } |
| 735 | |
| 736 | int nfs_map_uid_to_name(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen) |
| 737 | { |
| 738 | struct idmap *idmap = clp->cl_idmap; |
| 739 | |
| 740 | return nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf); |
| 741 | } |
| 742 | int nfs_map_gid_to_group(struct nfs_client *clp, __u32 uid, char *buf, size_t buflen) |
| 743 | { |
| 744 | struct idmap *idmap = clp->cl_idmap; |
| 745 | |
| 746 | return nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf); |
| 747 | } |
| 748 | |
| 749 | #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */ |