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