Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 36 | #include <linux/types.h> |
| 37 | #include <linux/string.h> |
| 38 | #include <linux/kernel.h> |
Vitaliy Ivanov | e44ba03 | 2011-06-20 16:08:07 +0200 | [diff] [blame] | 39 | #include <linux/slab.h> |
| 40 | #include <linux/nfs_idmap.h> |
Trond Myklebust | 6926afd | 2012-01-07 13:22:46 -0500 | [diff] [blame] | 41 | #include <linux/nfs_fs.h> |
Bryan Schumaker | 3cd0f37 | 2012-01-26 16:54:24 -0500 | [diff] [blame] | 42 | #include <linux/cred.h> |
| 43 | #include <linux/sunrpc/sched.h> |
| 44 | #include <linux/nfs4.h> |
| 45 | #include <linux/nfs_fs_sb.h> |
| 46 | #include <linux/keyctl.h> |
| 47 | #include <linux/key-type.h> |
| 48 | #include <linux/rcupdate.h> |
| 49 | #include <linux/err.h> |
| 50 | #include <keys/user-type.h> |
| 51 | |
| 52 | /* include files needed by legacy idmapper */ |
| 53 | #include <linux/module.h> |
| 54 | #include <linux/mutex.h> |
| 55 | #include <linux/init.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <linux/in.h> |
| 58 | #include <linux/sched.h> |
| 59 | #include <linux/sunrpc/clnt.h> |
| 60 | #include <linux/workqueue.h> |
| 61 | #include <linux/sunrpc/rpc_pipe_fs.h> |
| 62 | #include <linux/nfs_fs.h> |
| 63 | #include "nfs4_fs.h" |
| 64 | #include "internal.h" |
Stanislav Kinsbursky | 17347d0 | 2012-01-26 15:11:41 +0400 | [diff] [blame] | 65 | #include "netns.h" |
Bryan Schumaker | 3cd0f37 | 2012-01-26 16:54:24 -0500 | [diff] [blame] | 66 | |
| 67 | #define NFS_UINT_MAXLEN 11 |
| 68 | #define IDMAP_HASH_SZ 128 |
| 69 | |
| 70 | /* Default cache timeout is 10 minutes */ |
| 71 | unsigned int nfs_idmap_cache_timeout = 600 * HZ; |
| 72 | const struct cred *id_resolver_cache; |
| 73 | |
Trond Myklebust | 6926afd | 2012-01-07 13:22:46 -0500 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields |
| 77 | * @fattr: fully initialised struct nfs_fattr |
| 78 | * @owner_name: owner name string cache |
| 79 | * @group_name: group name string cache |
| 80 | */ |
| 81 | void nfs_fattr_init_names(struct nfs_fattr *fattr, |
| 82 | struct nfs4_string *owner_name, |
| 83 | struct nfs4_string *group_name) |
| 84 | { |
| 85 | fattr->owner_name = owner_name; |
| 86 | fattr->group_name = group_name; |
| 87 | } |
| 88 | |
| 89 | static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr) |
| 90 | { |
| 91 | fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME; |
| 92 | kfree(fattr->owner_name->data); |
| 93 | } |
| 94 | |
| 95 | static void nfs_fattr_free_group_name(struct nfs_fattr *fattr) |
| 96 | { |
| 97 | fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME; |
| 98 | kfree(fattr->group_name->data); |
| 99 | } |
| 100 | |
| 101 | static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr) |
| 102 | { |
| 103 | struct nfs4_string *owner = fattr->owner_name; |
| 104 | __u32 uid; |
| 105 | |
| 106 | if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)) |
| 107 | return false; |
| 108 | if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) { |
| 109 | fattr->uid = uid; |
| 110 | fattr->valid |= NFS_ATTR_FATTR_OWNER; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr) |
| 116 | { |
| 117 | struct nfs4_string *group = fattr->group_name; |
| 118 | __u32 gid; |
| 119 | |
| 120 | if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)) |
| 121 | return false; |
| 122 | if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) { |
| 123 | fattr->gid = gid; |
| 124 | fattr->valid |= NFS_ATTR_FATTR_GROUP; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * nfs_fattr_free_names - free up the NFSv4 owner and group strings |
| 131 | * @fattr: a fully initialised nfs_fattr structure |
| 132 | */ |
| 133 | void nfs_fattr_free_names(struct nfs_fattr *fattr) |
| 134 | { |
| 135 | if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME) |
| 136 | nfs_fattr_free_owner_name(fattr); |
| 137 | if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME) |
| 138 | nfs_fattr_free_group_name(fattr); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free |
| 143 | * @server: pointer to the filesystem nfs_server structure |
| 144 | * @fattr: a fully initialised nfs_fattr structure |
| 145 | * |
| 146 | * This helper maps the cached NFSv4 owner/group strings in fattr into |
| 147 | * their numeric uid/gid equivalents, and then frees the cached strings. |
| 148 | */ |
| 149 | void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr) |
| 150 | { |
| 151 | if (nfs_fattr_map_owner_name(server, fattr)) |
| 152 | nfs_fattr_free_owner_name(fattr); |
| 153 | if (nfs_fattr_map_group_name(server, fattr)) |
| 154 | nfs_fattr_free_group_name(fattr); |
| 155 | } |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 156 | |
| 157 | static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res) |
| 158 | { |
| 159 | unsigned long val; |
| 160 | char buf[16]; |
| 161 | |
| 162 | if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf)) |
| 163 | return 0; |
| 164 | memcpy(buf, name, namelen); |
| 165 | buf[namelen] = '\0'; |
| 166 | if (strict_strtoul(buf, 0, &val) != 0) |
| 167 | return 0; |
| 168 | *res = val; |
| 169 | return 1; |
| 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 172 | static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen) |
| 173 | { |
| 174 | return snprintf(buf, buflen, "%u", id); |
| 175 | } |
| 176 | |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 177 | struct key_type key_type_id_resolver = { |
| 178 | .name = "id_resolver", |
| 179 | .instantiate = user_instantiate, |
| 180 | .match = user_match, |
| 181 | .revoke = user_revoke, |
| 182 | .destroy = user_destroy, |
| 183 | .describe = user_describe, |
| 184 | .read = user_read, |
| 185 | }; |
| 186 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 187 | static int nfs_idmap_init_keyring(void) |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 188 | { |
| 189 | struct cred *cred; |
| 190 | struct key *keyring; |
| 191 | int ret = 0; |
| 192 | |
Weston Andros Adamson | f9fd2d9 | 2012-01-26 13:32:22 -0500 | [diff] [blame] | 193 | printk(KERN_NOTICE "NFS: Registering the %s key type\n", |
| 194 | key_type_id_resolver.name); |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 195 | |
| 196 | cred = prepare_kernel_cred(NULL); |
| 197 | if (!cred) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred, |
| 201 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 202 | KEY_USR_VIEW | KEY_USR_READ, |
| 203 | KEY_ALLOC_NOT_IN_QUOTA); |
| 204 | if (IS_ERR(keyring)) { |
| 205 | ret = PTR_ERR(keyring); |
| 206 | goto failed_put_cred; |
| 207 | } |
| 208 | |
| 209 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); |
| 210 | if (ret < 0) |
| 211 | goto failed_put_key; |
| 212 | |
| 213 | ret = register_key_type(&key_type_id_resolver); |
| 214 | if (ret < 0) |
| 215 | goto failed_put_key; |
| 216 | |
| 217 | cred->thread_keyring = keyring; |
| 218 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; |
| 219 | id_resolver_cache = cred; |
| 220 | return 0; |
| 221 | |
| 222 | failed_put_key: |
| 223 | key_put(keyring); |
| 224 | failed_put_cred: |
| 225 | put_cred(cred); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 229 | static void nfs_idmap_quit_keyring(void) |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 230 | { |
| 231 | key_revoke(id_resolver_cache->thread_keyring); |
| 232 | unregister_key_type(&key_type_id_resolver); |
| 233 | put_cred(id_resolver_cache); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Assemble the description to pass to request_key() |
| 238 | * This function will allocate a new string and update dest to point |
| 239 | * at it. The caller is responsible for freeing dest. |
| 240 | * |
| 241 | * On error 0 is returned. Otherwise, the length of dest is returned. |
| 242 | */ |
| 243 | static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen, |
| 244 | const char *type, size_t typelen, char **desc) |
| 245 | { |
| 246 | char *cp; |
| 247 | size_t desclen = typelen + namelen + 2; |
| 248 | |
| 249 | *desc = kmalloc(desclen, GFP_KERNEL); |
Dan Carpenter | 8f0d97b | 2010-10-28 08:05:57 +0200 | [diff] [blame] | 250 | if (!*desc) |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 251 | return -ENOMEM; |
| 252 | |
| 253 | cp = *desc; |
| 254 | memcpy(cp, type, typelen); |
| 255 | cp += typelen; |
| 256 | *cp++ = ':'; |
| 257 | |
| 258 | memcpy(cp, name, namelen); |
| 259 | cp += namelen; |
| 260 | *cp = '\0'; |
| 261 | return desclen; |
| 262 | } |
| 263 | |
| 264 | static ssize_t nfs_idmap_request_key(const char *name, size_t namelen, |
| 265 | const char *type, void *data, size_t data_size) |
| 266 | { |
| 267 | const struct cred *saved_cred; |
| 268 | struct key *rkey; |
| 269 | char *desc; |
| 270 | struct user_key_payload *payload; |
| 271 | ssize_t ret; |
| 272 | |
| 273 | ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc); |
| 274 | if (ret <= 0) |
| 275 | goto out; |
| 276 | |
| 277 | saved_cred = override_creds(id_resolver_cache); |
| 278 | rkey = request_key(&key_type_id_resolver, desc, ""); |
| 279 | revert_creds(saved_cred); |
| 280 | kfree(desc); |
| 281 | if (IS_ERR(rkey)) { |
| 282 | ret = PTR_ERR(rkey); |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | rcu_read_lock(); |
| 287 | rkey->perm |= KEY_USR_VIEW; |
| 288 | |
| 289 | ret = key_validate(rkey); |
| 290 | if (ret < 0) |
| 291 | goto out_up; |
| 292 | |
| 293 | payload = rcu_dereference(rkey->payload.data); |
| 294 | if (IS_ERR_OR_NULL(payload)) { |
| 295 | ret = PTR_ERR(payload); |
| 296 | goto out_up; |
| 297 | } |
| 298 | |
| 299 | ret = payload->datalen; |
| 300 | if (ret > 0 && ret <= data_size) |
| 301 | memcpy(data, payload->data, ret); |
| 302 | else |
| 303 | ret = -EINVAL; |
| 304 | |
| 305 | out_up: |
| 306 | rcu_read_unlock(); |
| 307 | key_put(rkey); |
| 308 | out: |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /* ID -> Name */ |
| 314 | static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen) |
| 315 | { |
| 316 | char id_str[NFS_UINT_MAXLEN]; |
| 317 | int id_len; |
| 318 | ssize_t ret; |
| 319 | |
| 320 | id_len = snprintf(id_str, sizeof(id_str), "%u", id); |
| 321 | ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen); |
| 322 | if (ret < 0) |
| 323 | return -EINVAL; |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | /* Name -> ID */ |
| 328 | static int nfs_idmap_lookup_id(const char *name, size_t namelen, |
| 329 | const char *type, __u32 *id) |
| 330 | { |
| 331 | char id_str[NFS_UINT_MAXLEN]; |
| 332 | long id_long; |
| 333 | ssize_t data_size; |
| 334 | int ret = 0; |
| 335 | |
| 336 | data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN); |
| 337 | if (data_size <= 0) { |
| 338 | ret = -EINVAL; |
| 339 | } else { |
| 340 | ret = strict_strtol(id_str, 10, &id_long); |
| 341 | *id = (__u32)id_long; |
| 342 | } |
| 343 | return ret; |
| 344 | } |
| 345 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 346 | /* idmap classic begins here */ |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 347 | static int param_set_idmap_timeout(const char *val, struct kernel_param *kp) |
| 348 | { |
| 349 | char *endp; |
| 350 | int num = simple_strtol(val, &endp, 0); |
| 351 | int jif = num * HZ; |
| 352 | if (endp == val || *endp || num < 0 || jif < num) |
| 353 | return -EINVAL; |
| 354 | *((int *)kp->arg) = jif; |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int, |
| 359 | &nfs_idmap_cache_timeout, 0644); |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | struct idmap_hashent { |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 362 | unsigned long ih_expires; |
| 363 | __u32 ih_id; |
Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 364 | size_t ih_namelen; |
Trond Myklebust | d073e9b | 2012-02-07 14:59:05 -0500 | [diff] [blame] | 365 | const char *ih_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | struct idmap_hashtable { |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 369 | __u8 h_type; |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 370 | struct idmap_hashent *h_entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | struct idmap { |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 374 | struct rpc_pipe *idmap_pipe; |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 375 | wait_queue_head_t idmap_wq; |
| 376 | struct idmap_msg idmap_im; |
| 377 | struct mutex idmap_lock; /* Serializes upcalls */ |
| 378 | struct mutex idmap_im_lock; /* Protects the hashtable */ |
| 379 | struct idmap_hashtable idmap_user_hash; |
| 380 | struct idmap_hashtable idmap_group_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 383 | static ssize_t idmap_pipe_downcall(struct file *, const char __user *, |
| 384 | size_t); |
| 385 | static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | static unsigned int fnvhash32(const void *, size_t); |
| 388 | |
Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 389 | static const struct rpc_pipe_ops idmap_upcall_ops = { |
Peng Tao | c122515 | 2011-09-22 21:50:10 -0400 | [diff] [blame] | 390 | .upcall = rpc_pipe_generic_upcall, |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 391 | .downcall = idmap_pipe_downcall, |
| 392 | .destroy_msg = idmap_pipe_destroy_msg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
Stanislav Kinsbursky | 4929d1d | 2012-01-10 16:13:11 +0400 | [diff] [blame] | 395 | static void __nfs_idmap_unregister(struct rpc_pipe *pipe) |
| 396 | { |
| 397 | if (pipe->dentry) |
| 398 | rpc_unlink(pipe->dentry); |
| 399 | } |
| 400 | |
| 401 | static int __nfs_idmap_register(struct dentry *dir, |
| 402 | struct idmap *idmap, |
| 403 | struct rpc_pipe *pipe) |
| 404 | { |
| 405 | struct dentry *dentry; |
| 406 | |
| 407 | dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe); |
| 408 | if (IS_ERR(dentry)) |
| 409 | return PTR_ERR(dentry); |
| 410 | pipe->dentry = dentry; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static void nfs_idmap_unregister(struct nfs_client *clp, |
| 415 | struct rpc_pipe *pipe) |
| 416 | { |
| 417 | struct net *net = clp->net; |
| 418 | struct super_block *pipefs_sb; |
| 419 | |
| 420 | pipefs_sb = rpc_get_sb_net(net); |
| 421 | if (pipefs_sb) { |
| 422 | __nfs_idmap_unregister(pipe); |
| 423 | rpc_put_sb_net(net); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static int nfs_idmap_register(struct nfs_client *clp, |
| 428 | struct idmap *idmap, |
| 429 | struct rpc_pipe *pipe) |
| 430 | { |
| 431 | struct net *net = clp->net; |
| 432 | struct super_block *pipefs_sb; |
| 433 | int err = 0; |
| 434 | |
| 435 | pipefs_sb = rpc_get_sb_net(net); |
| 436 | if (pipefs_sb) { |
| 437 | if (clp->cl_rpcclient->cl_dentry) |
| 438 | err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry, |
| 439 | idmap, pipe); |
| 440 | rpc_put_sb_net(net); |
| 441 | } |
| 442 | return err; |
| 443 | } |
| 444 | |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 445 | int |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 446 | nfs_idmap_new(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | struct idmap *idmap; |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 449 | struct rpc_pipe *pipe; |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 450 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 452 | BUG_ON(clp->cl_idmap != NULL); |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 453 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 454 | idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); |
| 455 | if (idmap == NULL) |
| 456 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 458 | pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0); |
| 459 | if (IS_ERR(pipe)) { |
| 460 | error = PTR_ERR(pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | kfree(idmap); |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 462 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
Stanislav Kinsbursky | 4929d1d | 2012-01-10 16:13:11 +0400 | [diff] [blame] | 464 | error = nfs_idmap_register(clp, idmap, pipe); |
| 465 | if (error) { |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 466 | rpc_destroy_pipe_data(pipe); |
| 467 | kfree(idmap); |
| 468 | return error; |
| 469 | } |
| 470 | idmap->idmap_pipe = pipe; |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 471 | mutex_init(&idmap->idmap_lock); |
| 472 | mutex_init(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | init_waitqueue_head(&idmap->idmap_wq); |
| 474 | idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER; |
| 475 | idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP; |
| 476 | |
| 477 | clp->cl_idmap = idmap; |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 478 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 481 | static void |
| 482 | idmap_alloc_hashtable(struct idmap_hashtable *h) |
| 483 | { |
| 484 | if (h->h_entries != NULL) |
| 485 | return; |
| 486 | h->h_entries = kcalloc(IDMAP_HASH_SZ, |
| 487 | sizeof(*h->h_entries), |
| 488 | GFP_KERNEL); |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | idmap_free_hashtable(struct idmap_hashtable *h) |
| 493 | { |
| 494 | int i; |
| 495 | |
| 496 | if (h->h_entries == NULL) |
| 497 | return; |
| 498 | for (i = 0; i < IDMAP_HASH_SZ; i++) |
| 499 | kfree(h->h_entries[i].ih_name); |
| 500 | kfree(h->h_entries); |
| 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | void |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 504 | nfs_idmap_delete(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | struct idmap *idmap = clp->cl_idmap; |
| 507 | |
| 508 | if (!idmap) |
| 509 | return; |
Stanislav Kinsbursky | 4929d1d | 2012-01-10 16:13:11 +0400 | [diff] [blame] | 510 | nfs_idmap_unregister(clp, idmap->idmap_pipe); |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 511 | rpc_destroy_pipe_data(idmap->idmap_pipe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | clp->cl_idmap = NULL; |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 513 | idmap_free_hashtable(&idmap->idmap_user_hash); |
| 514 | idmap_free_hashtable(&idmap->idmap_group_hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | kfree(idmap); |
| 516 | } |
| 517 | |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 518 | static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event, |
| 519 | struct super_block *sb) |
| 520 | { |
| 521 | int err = 0; |
| 522 | |
| 523 | switch (event) { |
| 524 | case RPC_PIPEFS_MOUNT: |
| 525 | BUG_ON(clp->cl_rpcclient->cl_dentry == NULL); |
| 526 | err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry, |
| 527 | clp->cl_idmap, |
| 528 | clp->cl_idmap->idmap_pipe); |
| 529 | break; |
| 530 | case RPC_PIPEFS_UMOUNT: |
| 531 | if (clp->cl_idmap->idmap_pipe) { |
| 532 | struct dentry *parent; |
| 533 | |
| 534 | parent = clp->cl_idmap->idmap_pipe->dentry->d_parent; |
| 535 | __nfs_idmap_unregister(clp->cl_idmap->idmap_pipe); |
| 536 | /* |
| 537 | * Note: This is a dirty hack. SUNRPC hook has been |
| 538 | * called already but simple_rmdir() call for the |
| 539 | * directory returned with error because of idmap pipe |
| 540 | * inside. Thus now we have to remove this directory |
| 541 | * here. |
| 542 | */ |
| 543 | if (rpc_rmdir(parent)) |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 544 | printk(KERN_ERR "NFS: %s: failed to remove " |
| 545 | "clnt dir!\n", __func__); |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 546 | } |
| 547 | break; |
| 548 | default: |
Weston Andros Adamson | a030889 | 2012-01-26 13:32:23 -0500 | [diff] [blame] | 549 | printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__, |
| 550 | event); |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 551 | return -ENOTSUPP; |
| 552 | } |
| 553 | return err; |
| 554 | } |
| 555 | |
| 556 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
| 557 | void *ptr) |
| 558 | { |
| 559 | struct super_block *sb = ptr; |
Stanislav Kinsbursky | 6b13168 | 2012-01-23 17:26:05 +0000 | [diff] [blame] | 560 | struct nfs_net *nn = net_generic(sb->s_fs_info, nfs_net_id); |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 561 | struct nfs_client *clp; |
| 562 | int error = 0; |
| 563 | |
Stanislav Kinsbursky | dc03085 | 2012-01-23 17:26:31 +0000 | [diff] [blame] | 564 | spin_lock(&nn->nfs_client_lock); |
Stanislav Kinsbursky | 6b13168 | 2012-01-23 17:26:05 +0000 | [diff] [blame] | 565 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 566 | if (clp->rpc_ops != &nfs_v4_clientops) |
| 567 | continue; |
| 568 | error = __rpc_pipefs_event(clp, event, sb); |
| 569 | if (error) |
| 570 | break; |
| 571 | } |
Stanislav Kinsbursky | dc03085 | 2012-01-23 17:26:31 +0000 | [diff] [blame] | 572 | spin_unlock(&nn->nfs_client_lock); |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 573 | return error; |
| 574 | } |
| 575 | |
| 576 | #define PIPEFS_NFS_PRIO 1 |
| 577 | |
| 578 | static struct notifier_block nfs_idmap_block = { |
| 579 | .notifier_call = rpc_pipefs_event, |
| 580 | .priority = SUNRPC_PIPEFS_NFS_PRIO, |
| 581 | }; |
| 582 | |
| 583 | int nfs_idmap_init(void) |
| 584 | { |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 585 | int ret; |
| 586 | ret = nfs_idmap_init_keyring(); |
| 587 | if (ret != 0) |
| 588 | goto out; |
| 589 | ret = rpc_pipefs_notifier_register(&nfs_idmap_block); |
| 590 | if (ret != 0) |
| 591 | nfs_idmap_quit_keyring(); |
| 592 | out: |
| 593 | return ret; |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | void nfs_idmap_quit(void) |
| 597 | { |
| 598 | rpc_pipefs_notifier_unregister(&nfs_idmap_block); |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 599 | nfs_idmap_quit_keyring(); |
Stanislav Kinsbursky | eee1732 | 2012-01-10 16:13:19 +0400 | [diff] [blame] | 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* |
| 603 | * Helper routines for manipulating the hashtable |
| 604 | */ |
| 605 | static inline struct idmap_hashent * |
| 606 | idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len) |
| 607 | { |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 608 | if (h->h_entries == NULL) |
| 609 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ]; |
| 611 | } |
| 612 | |
| 613 | static struct idmap_hashent * |
| 614 | idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len) |
| 615 | { |
| 616 | struct idmap_hashent *he = idmap_name_hash(h, name, len); |
| 617 | |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 618 | if (he == NULL) |
| 619 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0) |
| 621 | return NULL; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 622 | if (time_after(jiffies, he->ih_expires)) |
| 623 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | return he; |
| 625 | } |
| 626 | |
| 627 | static inline struct idmap_hashent * |
| 628 | idmap_id_hash(struct idmap_hashtable* h, __u32 id) |
| 629 | { |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 630 | if (h->h_entries == NULL) |
| 631 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ]; |
| 633 | } |
| 634 | |
| 635 | static struct idmap_hashent * |
| 636 | idmap_lookup_id(struct idmap_hashtable *h, __u32 id) |
| 637 | { |
| 638 | struct idmap_hashent *he = idmap_id_hash(h, id); |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 639 | |
| 640 | if (he == NULL) |
| 641 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | if (he->ih_id != id || he->ih_namelen == 0) |
| 643 | return NULL; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 644 | if (time_after(jiffies, he->ih_expires)) |
| 645 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return he; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Routines for allocating new entries in the hashtable. |
| 651 | * For now, we just have 1 entry per bucket, so it's all |
| 652 | * pretty trivial. |
| 653 | */ |
| 654 | static inline struct idmap_hashent * |
Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 655 | idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 657 | idmap_alloc_hashtable(h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return idmap_name_hash(h, name, len); |
| 659 | } |
| 660 | |
| 661 | static inline struct idmap_hashent * |
| 662 | idmap_alloc_id(struct idmap_hashtable *h, __u32 id) |
| 663 | { |
Trond Myklebust | 685f50f | 2012-02-08 13:39:15 -0500 | [diff] [blame^] | 664 | idmap_alloc_hashtable(h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | return idmap_id_hash(h, id); |
| 666 | } |
| 667 | |
| 668 | static void |
| 669 | idmap_update_entry(struct idmap_hashent *he, const char *name, |
| 670 | size_t namelen, __u32 id) |
| 671 | { |
Trond Myklebust | d073e9b | 2012-02-07 14:59:05 -0500 | [diff] [blame] | 672 | char *str = kmalloc(namelen + 1, GFP_KERNEL); |
| 673 | if (str == NULL) |
| 674 | return; |
| 675 | kfree(he->ih_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | he->ih_id = id; |
Trond Myklebust | d073e9b | 2012-02-07 14:59:05 -0500 | [diff] [blame] | 677 | memcpy(str, name, namelen); |
| 678 | str[namelen] = '\0'; |
| 679 | he->ih_name = str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | he->ih_namelen = namelen; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 681 | he->ih_expires = jiffies + nfs_idmap_cache_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Name -> ID |
| 686 | */ |
| 687 | static int |
| 688 | nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h, |
| 689 | const char *name, size_t namelen, __u32 *id) |
| 690 | { |
| 691 | struct rpc_pipe_msg msg; |
| 692 | struct idmap_msg *im; |
| 693 | struct idmap_hashent *he; |
| 694 | DECLARE_WAITQUEUE(wq, current); |
| 695 | int ret = -EIO; |
| 696 | |
| 697 | im = &idmap->idmap_im; |
| 698 | |
| 699 | /* |
| 700 | * String sanity checks |
| 701 | * Note that the userland daemon expects NUL terminated strings |
| 702 | */ |
| 703 | for (;;) { |
| 704 | if (namelen == 0) |
| 705 | return -EINVAL; |
| 706 | if (name[namelen-1] != '\0') |
| 707 | break; |
| 708 | namelen--; |
| 709 | } |
| 710 | if (namelen >= IDMAP_NAMESZ) |
| 711 | return -EINVAL; |
| 712 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 713 | mutex_lock(&idmap->idmap_lock); |
| 714 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | he = idmap_lookup_name(h, name, namelen); |
| 717 | if (he != NULL) { |
| 718 | *id = he->ih_id; |
| 719 | ret = 0; |
| 720 | goto out; |
| 721 | } |
| 722 | |
| 723 | memset(im, 0, sizeof(*im)); |
| 724 | memcpy(im->im_name, name, namelen); |
| 725 | |
| 726 | im->im_type = h->h_type; |
| 727 | im->im_conv = IDMAP_CONV_NAMETOID; |
| 728 | |
| 729 | memset(&msg, 0, sizeof(msg)); |
| 730 | msg.data = im; |
| 731 | msg.len = sizeof(*im); |
| 732 | |
| 733 | add_wait_queue(&idmap->idmap_wq, &wq); |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 734 | if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 736 | goto out; |
| 737 | } |
| 738 | |
| 739 | set_current_state(TASK_UNINTERRUPTIBLE); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 740 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | schedule(); |
Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 742 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | remove_wait_queue(&idmap->idmap_wq, &wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 744 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | if (im->im_status & IDMAP_STATUS_SUCCESS) { |
| 747 | *id = im->im_id; |
| 748 | ret = 0; |
| 749 | } |
| 750 | |
| 751 | out: |
| 752 | memset(im, 0, sizeof(*im)); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 753 | mutex_unlock(&idmap->idmap_im_lock); |
| 754 | mutex_unlock(&idmap->idmap_lock); |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 755 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* |
| 759 | * ID -> Name |
| 760 | */ |
| 761 | static int |
| 762 | nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h, |
| 763 | __u32 id, char *name) |
| 764 | { |
| 765 | struct rpc_pipe_msg msg; |
| 766 | struct idmap_msg *im; |
| 767 | struct idmap_hashent *he; |
| 768 | DECLARE_WAITQUEUE(wq, current); |
| 769 | int ret = -EIO; |
| 770 | unsigned int len; |
| 771 | |
| 772 | im = &idmap->idmap_im; |
| 773 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 774 | mutex_lock(&idmap->idmap_lock); |
| 775 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | he = idmap_lookup_id(h, id); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 778 | if (he) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | memcpy(name, he->ih_name, he->ih_namelen); |
| 780 | ret = he->ih_namelen; |
| 781 | goto out; |
| 782 | } |
| 783 | |
| 784 | memset(im, 0, sizeof(*im)); |
| 785 | im->im_type = h->h_type; |
| 786 | im->im_conv = IDMAP_CONV_IDTONAME; |
| 787 | im->im_id = id; |
| 788 | |
| 789 | memset(&msg, 0, sizeof(msg)); |
| 790 | msg.data = im; |
| 791 | msg.len = sizeof(*im); |
| 792 | |
| 793 | add_wait_queue(&idmap->idmap_wq, &wq); |
| 794 | |
Stanislav Kinsbursky | c239d83 | 2011-12-26 15:44:06 +0300 | [diff] [blame] | 795 | if (rpc_queue_upcall(idmap->idmap_pipe, &msg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 797 | goto out; |
| 798 | } |
| 799 | |
| 800 | set_current_state(TASK_UNINTERRUPTIBLE); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 801 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | schedule(); |
Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 803 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | remove_wait_queue(&idmap->idmap_wq, &wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 805 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | if (im->im_status & IDMAP_STATUS_SUCCESS) { |
| 808 | if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0) |
| 809 | goto out; |
| 810 | memcpy(name, im->im_name, len); |
| 811 | ret = len; |
| 812 | } |
| 813 | |
| 814 | out: |
| 815 | memset(im, 0, sizeof(*im)); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 816 | mutex_unlock(&idmap->idmap_im_lock); |
| 817 | mutex_unlock(&idmap->idmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return ret; |
| 819 | } |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | static ssize_t |
| 822 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) |
| 823 | { |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 824 | struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | struct idmap *idmap = (struct idmap *)rpci->private; |
| 826 | struct idmap_msg im_in, *im = &idmap->idmap_im; |
| 827 | struct idmap_hashtable *h; |
| 828 | struct idmap_hashent *he = NULL; |
Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 829 | size_t namelen_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | int ret; |
| 831 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 832 | if (mlen != sizeof(im_in)) |
| 833 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 835 | if (copy_from_user(&im_in, src, mlen) != 0) |
| 836 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 838 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | ret = mlen; |
| 841 | im->im_status = im_in.im_status; |
| 842 | /* If we got an error, terminate now, and wake up pending upcalls */ |
| 843 | if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) { |
| 844 | wake_up(&idmap->idmap_wq); |
| 845 | goto out; |
| 846 | } |
| 847 | |
| 848 | /* Sanity checking of strings */ |
| 849 | ret = -EINVAL; |
| 850 | namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ); |
| 851 | if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) |
| 852 | goto out; |
| 853 | |
| 854 | switch (im_in.im_type) { |
| 855 | case IDMAP_TYPE_USER: |
| 856 | h = &idmap->idmap_user_hash; |
| 857 | break; |
| 858 | case IDMAP_TYPE_GROUP: |
| 859 | h = &idmap->idmap_group_hash; |
| 860 | break; |
| 861 | default: |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | switch (im_in.im_conv) { |
| 866 | case IDMAP_CONV_IDTONAME: |
| 867 | /* Did we match the current upcall? */ |
| 868 | if (im->im_conv == IDMAP_CONV_IDTONAME |
| 869 | && im->im_type == im_in.im_type |
| 870 | && im->im_id == im_in.im_id) { |
| 871 | /* Yes: copy string, including the terminating '\0' */ |
| 872 | memcpy(im->im_name, im_in.im_name, namelen_in); |
| 873 | im->im_name[namelen_in] = '\0'; |
| 874 | wake_up(&idmap->idmap_wq); |
| 875 | } |
| 876 | he = idmap_alloc_id(h, im_in.im_id); |
| 877 | break; |
| 878 | case IDMAP_CONV_NAMETOID: |
| 879 | /* Did we match the current upcall? */ |
| 880 | if (im->im_conv == IDMAP_CONV_NAMETOID |
| 881 | && im->im_type == im_in.im_type |
| 882 | && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in |
| 883 | && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) { |
| 884 | im->im_id = im_in.im_id; |
| 885 | wake_up(&idmap->idmap_wq); |
| 886 | } |
| 887 | he = idmap_alloc_name(h, im_in.im_name, namelen_in); |
| 888 | break; |
| 889 | default: |
| 890 | goto out; |
| 891 | } |
| 892 | |
| 893 | /* If the entry is valid, also copy it to the cache */ |
| 894 | if (he != NULL) |
| 895 | idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id); |
| 896 | ret = mlen; |
| 897 | out: |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 898 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | return ret; |
| 900 | } |
| 901 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 902 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg) |
| 904 | { |
| 905 | struct idmap_msg *im = msg->data; |
| 906 | struct idmap *idmap = container_of(im, struct idmap, idmap_im); |
| 907 | |
| 908 | if (msg->errno >= 0) |
| 909 | return; |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 910 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | im->im_status = IDMAP_STATUS_LOOKUPFAIL; |
| 912 | wake_up(&idmap->idmap_wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 913 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
| 917 | * Fowler/Noll/Vo hash |
| 918 | * http://www.isthe.com/chongo/tech/comp/fnv/ |
| 919 | */ |
| 920 | |
| 921 | #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */ |
| 922 | #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */ |
| 923 | |
| 924 | static unsigned int fnvhash32(const void *buf, size_t buflen) |
| 925 | { |
| 926 | const unsigned char *p, *end = (const unsigned char *)buf + buflen; |
| 927 | unsigned int hash = FNV_1_32; |
| 928 | |
| 929 | for (p = buf; p < end; p++) { |
| 930 | hash *= FNV_P_32; |
| 931 | hash ^= (unsigned int)*p; |
| 932 | } |
| 933 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 934 | return hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 937 | int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 939 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 940 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 942 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 943 | return 0; |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 944 | ret = nfs_idmap_lookup_id(name, namelen, "uid", uid); |
| 945 | if (ret < 0) |
| 946 | ret = nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid); |
| 947 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 950 | int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 952 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 953 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 955 | if (nfs_map_string_to_numeric(name, namelen, gid)) |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 956 | return 0; |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 957 | ret = nfs_idmap_lookup_id(name, namelen, "gid", gid); |
| 958 | if (ret < 0) |
| 959 | ret = nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, gid); |
| 960 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 963 | int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 965 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 966 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 968 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) { |
| 969 | ret = nfs_idmap_lookup_name(uid, "user", buf, buflen); |
| 970 | if (ret < 0) |
| 971 | ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf); |
| 972 | } |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 973 | if (ret < 0) |
| 974 | ret = nfs_map_numeric_to_string(uid, buf, buflen); |
| 975 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | } |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 977 | int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 979 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 980 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 982 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) { |
| 983 | ret = nfs_idmap_lookup_name(gid, "group", buf, buflen); |
| 984 | if (ret < 0) |
| 985 | ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, gid, buf); |
| 986 | } |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 987 | if (ret < 0) |
Bryan Schumaker | e6499c6 | 2012-01-26 16:54:23 -0500 | [diff] [blame] | 988 | ret = nfs_map_numeric_to_string(gid, buf, buflen); |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 989 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } |