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 | |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame^] | 201 | set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags); |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 202 | cred->thread_keyring = keyring; |
| 203 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; |
| 204 | id_resolver_cache = cred; |
| 205 | return 0; |
| 206 | |
| 207 | failed_put_key: |
| 208 | key_put(keyring); |
| 209 | failed_put_cred: |
| 210 | put_cred(cred); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | void nfs_idmap_quit(void) |
| 215 | { |
| 216 | key_revoke(id_resolver_cache->thread_keyring); |
| 217 | unregister_key_type(&key_type_id_resolver); |
| 218 | put_cred(id_resolver_cache); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Assemble the description to pass to request_key() |
| 223 | * This function will allocate a new string and update dest to point |
| 224 | * at it. The caller is responsible for freeing dest. |
| 225 | * |
| 226 | * On error 0 is returned. Otherwise, the length of dest is returned. |
| 227 | */ |
| 228 | static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen, |
| 229 | const char *type, size_t typelen, char **desc) |
| 230 | { |
| 231 | char *cp; |
| 232 | size_t desclen = typelen + namelen + 2; |
| 233 | |
| 234 | *desc = kmalloc(desclen, GFP_KERNEL); |
Dan Carpenter | 8f0d97b | 2010-10-28 08:05:57 +0200 | [diff] [blame] | 235 | if (!*desc) |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 236 | return -ENOMEM; |
| 237 | |
| 238 | cp = *desc; |
| 239 | memcpy(cp, type, typelen); |
| 240 | cp += typelen; |
| 241 | *cp++ = ':'; |
| 242 | |
| 243 | memcpy(cp, name, namelen); |
| 244 | cp += namelen; |
| 245 | *cp = '\0'; |
| 246 | return desclen; |
| 247 | } |
| 248 | |
| 249 | static ssize_t nfs_idmap_request_key(const char *name, size_t namelen, |
| 250 | const char *type, void *data, size_t data_size) |
| 251 | { |
| 252 | const struct cred *saved_cred; |
| 253 | struct key *rkey; |
| 254 | char *desc; |
| 255 | struct user_key_payload *payload; |
| 256 | ssize_t ret; |
| 257 | |
| 258 | ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc); |
| 259 | if (ret <= 0) |
| 260 | goto out; |
| 261 | |
| 262 | saved_cred = override_creds(id_resolver_cache); |
| 263 | rkey = request_key(&key_type_id_resolver, desc, ""); |
| 264 | revert_creds(saved_cred); |
| 265 | kfree(desc); |
| 266 | if (IS_ERR(rkey)) { |
| 267 | ret = PTR_ERR(rkey); |
| 268 | goto out; |
| 269 | } |
| 270 | |
| 271 | rcu_read_lock(); |
| 272 | rkey->perm |= KEY_USR_VIEW; |
| 273 | |
| 274 | ret = key_validate(rkey); |
| 275 | if (ret < 0) |
| 276 | goto out_up; |
| 277 | |
| 278 | payload = rcu_dereference(rkey->payload.data); |
| 279 | if (IS_ERR_OR_NULL(payload)) { |
| 280 | ret = PTR_ERR(payload); |
| 281 | goto out_up; |
| 282 | } |
| 283 | |
| 284 | ret = payload->datalen; |
| 285 | if (ret > 0 && ret <= data_size) |
| 286 | memcpy(data, payload->data, ret); |
| 287 | else |
| 288 | ret = -EINVAL; |
| 289 | |
| 290 | out_up: |
| 291 | rcu_read_unlock(); |
| 292 | key_put(rkey); |
| 293 | out: |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* ID -> Name */ |
| 299 | static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf, size_t buflen) |
| 300 | { |
| 301 | char id_str[NFS_UINT_MAXLEN]; |
| 302 | int id_len; |
| 303 | ssize_t ret; |
| 304 | |
| 305 | id_len = snprintf(id_str, sizeof(id_str), "%u", id); |
| 306 | ret = nfs_idmap_request_key(id_str, id_len, type, buf, buflen); |
| 307 | if (ret < 0) |
| 308 | return -EINVAL; |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* Name -> ID */ |
| 313 | static int nfs_idmap_lookup_id(const char *name, size_t namelen, |
| 314 | const char *type, __u32 *id) |
| 315 | { |
| 316 | char id_str[NFS_UINT_MAXLEN]; |
| 317 | long id_long; |
| 318 | ssize_t data_size; |
| 319 | int ret = 0; |
| 320 | |
| 321 | data_size = nfs_idmap_request_key(name, namelen, type, id_str, NFS_UINT_MAXLEN); |
| 322 | if (data_size <= 0) { |
| 323 | ret = -EINVAL; |
| 324 | } else { |
| 325 | ret = strict_strtol(id_str, 10, &id_long); |
| 326 | *id = (__u32)id_long; |
| 327 | } |
| 328 | return ret; |
| 329 | } |
| 330 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 331 | 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] | 332 | { |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 333 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 334 | return 0; |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 335 | return nfs_idmap_lookup_id(name, namelen, "uid", uid); |
| 336 | } |
| 337 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 338 | 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] | 339 | { |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 340 | if (nfs_map_string_to_numeric(name, namelen, gid)) |
| 341 | return 0; |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 342 | return nfs_idmap_lookup_id(name, namelen, "gid", gid); |
| 343 | } |
| 344 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 345 | 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] | 346 | { |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 347 | int ret = -EINVAL; |
| 348 | |
| 349 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) |
| 350 | ret = nfs_idmap_lookup_name(uid, "user", buf, buflen); |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 351 | if (ret < 0) |
| 352 | ret = nfs_map_numeric_to_string(uid, buf, buflen); |
| 353 | return ret; |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 354 | } |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 355 | 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] | 356 | { |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 357 | int ret = -EINVAL; |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 358 | |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 359 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) |
| 360 | ret = nfs_idmap_lookup_name(gid, "group", buf, buflen); |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 361 | if (ret < 0) |
| 362 | ret = nfs_map_numeric_to_string(gid, buf, buflen); |
| 363 | return ret; |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 364 | } |
| 365 | |
J. Bruce Fields | 5f3e97c | 2010-12-21 23:49:34 +0000 | [diff] [blame] | 366 | #else /* CONFIG_NFS_USE_NEW_IDMAPPER not defined */ |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | #include <linux/module.h> |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 369 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | #include <linux/socket.h> |
| 372 | #include <linux/in.h> |
| 373 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | #include <linux/sunrpc/clnt.h> |
| 375 | #include <linux/workqueue.h> |
| 376 | #include <linux/sunrpc/rpc_pipe_fs.h> |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | #include <linux/nfs_fs.h> |
| 379 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 380 | #include "nfs4_fs.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 { |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 414 | struct dentry *idmap_dentry; |
| 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 | |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 435 | int |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 436 | nfs_idmap_new(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | struct idmap *idmap; |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 439 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 441 | BUG_ON(clp->cl_idmap != NULL); |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 442 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 443 | idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); |
| 444 | if (idmap == NULL) |
| 445 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Trond Myklebust | 7d217ca | 2009-08-09 15:14:24 -0400 | [diff] [blame] | 447 | idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry, |
| 448 | "idmap", idmap, &idmap_upcall_ops, 0); |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 449 | if (IS_ERR(idmap->idmap_dentry)) { |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 450 | error = PTR_ERR(idmap->idmap_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | kfree(idmap); |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 452 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 455 | mutex_init(&idmap->idmap_lock); |
| 456 | mutex_init(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | init_waitqueue_head(&idmap->idmap_wq); |
| 458 | idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER; |
| 459 | idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP; |
| 460 | |
| 461 | clp->cl_idmap = idmap; |
David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 462 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 466 | nfs_idmap_delete(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | struct idmap *idmap = clp->cl_idmap; |
| 469 | |
| 470 | if (!idmap) |
| 471 | return; |
Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 472 | rpc_unlink(idmap->idmap_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | clp->cl_idmap = NULL; |
| 474 | kfree(idmap); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Helper routines for manipulating the hashtable |
| 479 | */ |
| 480 | static inline struct idmap_hashent * |
| 481 | idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len) |
| 482 | { |
| 483 | return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ]; |
| 484 | } |
| 485 | |
| 486 | static struct idmap_hashent * |
| 487 | idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len) |
| 488 | { |
| 489 | struct idmap_hashent *he = idmap_name_hash(h, name, len); |
| 490 | |
| 491 | if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0) |
| 492 | return NULL; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 493 | if (time_after(jiffies, he->ih_expires)) |
| 494 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return he; |
| 496 | } |
| 497 | |
| 498 | static inline struct idmap_hashent * |
| 499 | idmap_id_hash(struct idmap_hashtable* h, __u32 id) |
| 500 | { |
| 501 | return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ]; |
| 502 | } |
| 503 | |
| 504 | static struct idmap_hashent * |
| 505 | idmap_lookup_id(struct idmap_hashtable *h, __u32 id) |
| 506 | { |
| 507 | struct idmap_hashent *he = idmap_id_hash(h, id); |
| 508 | if (he->ih_id != id || he->ih_namelen == 0) |
| 509 | return NULL; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 510 | if (time_after(jiffies, he->ih_expires)) |
| 511 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return he; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Routines for allocating new entries in the hashtable. |
| 517 | * For now, we just have 1 entry per bucket, so it's all |
| 518 | * pretty trivial. |
| 519 | */ |
| 520 | static inline struct idmap_hashent * |
Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 521 | idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
| 523 | return idmap_name_hash(h, name, len); |
| 524 | } |
| 525 | |
| 526 | static inline struct idmap_hashent * |
| 527 | idmap_alloc_id(struct idmap_hashtable *h, __u32 id) |
| 528 | { |
| 529 | return idmap_id_hash(h, id); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | idmap_update_entry(struct idmap_hashent *he, const char *name, |
| 534 | size_t namelen, __u32 id) |
| 535 | { |
| 536 | he->ih_id = id; |
| 537 | memcpy(he->ih_name, name, namelen); |
| 538 | he->ih_name[namelen] = '\0'; |
| 539 | he->ih_namelen = namelen; |
Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 540 | he->ih_expires = jiffies + nfs_idmap_cache_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Name -> ID |
| 545 | */ |
| 546 | static int |
| 547 | nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h, |
| 548 | const char *name, size_t namelen, __u32 *id) |
| 549 | { |
| 550 | struct rpc_pipe_msg msg; |
| 551 | struct idmap_msg *im; |
| 552 | struct idmap_hashent *he; |
| 553 | DECLARE_WAITQUEUE(wq, current); |
| 554 | int ret = -EIO; |
| 555 | |
| 556 | im = &idmap->idmap_im; |
| 557 | |
| 558 | /* |
| 559 | * String sanity checks |
| 560 | * Note that the userland daemon expects NUL terminated strings |
| 561 | */ |
| 562 | for (;;) { |
| 563 | if (namelen == 0) |
| 564 | return -EINVAL; |
| 565 | if (name[namelen-1] != '\0') |
| 566 | break; |
| 567 | namelen--; |
| 568 | } |
| 569 | if (namelen >= IDMAP_NAMESZ) |
| 570 | return -EINVAL; |
| 571 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 572 | mutex_lock(&idmap->idmap_lock); |
| 573 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | he = idmap_lookup_name(h, name, namelen); |
| 576 | if (he != NULL) { |
| 577 | *id = he->ih_id; |
| 578 | ret = 0; |
| 579 | goto out; |
| 580 | } |
| 581 | |
| 582 | memset(im, 0, sizeof(*im)); |
| 583 | memcpy(im->im_name, name, namelen); |
| 584 | |
| 585 | im->im_type = h->h_type; |
| 586 | im->im_conv = IDMAP_CONV_NAMETOID; |
| 587 | |
| 588 | memset(&msg, 0, sizeof(msg)); |
| 589 | msg.data = im; |
| 590 | msg.len = sizeof(*im); |
| 591 | |
| 592 | add_wait_queue(&idmap->idmap_wq, &wq); |
| 593 | if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { |
| 594 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 595 | goto out; |
| 596 | } |
| 597 | |
| 598 | set_current_state(TASK_UNINTERRUPTIBLE); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 599 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | schedule(); |
Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 601 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | remove_wait_queue(&idmap->idmap_wq, &wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 603 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | if (im->im_status & IDMAP_STATUS_SUCCESS) { |
| 606 | *id = im->im_id; |
| 607 | ret = 0; |
| 608 | } |
| 609 | |
| 610 | out: |
| 611 | memset(im, 0, sizeof(*im)); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 612 | mutex_unlock(&idmap->idmap_im_lock); |
| 613 | mutex_unlock(&idmap->idmap_lock); |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 614 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
| 618 | * ID -> Name |
| 619 | */ |
| 620 | static int |
| 621 | nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h, |
| 622 | __u32 id, char *name) |
| 623 | { |
| 624 | struct rpc_pipe_msg msg; |
| 625 | struct idmap_msg *im; |
| 626 | struct idmap_hashent *he; |
| 627 | DECLARE_WAITQUEUE(wq, current); |
| 628 | int ret = -EIO; |
| 629 | unsigned int len; |
| 630 | |
| 631 | im = &idmap->idmap_im; |
| 632 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 633 | mutex_lock(&idmap->idmap_lock); |
| 634 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | he = idmap_lookup_id(h, id); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 637 | if (he) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | memcpy(name, he->ih_name, he->ih_namelen); |
| 639 | ret = he->ih_namelen; |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | memset(im, 0, sizeof(*im)); |
| 644 | im->im_type = h->h_type; |
| 645 | im->im_conv = IDMAP_CONV_IDTONAME; |
| 646 | im->im_id = id; |
| 647 | |
| 648 | memset(&msg, 0, sizeof(msg)); |
| 649 | msg.data = im; |
| 650 | msg.len = sizeof(*im); |
| 651 | |
| 652 | add_wait_queue(&idmap->idmap_wq, &wq); |
| 653 | |
| 654 | if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { |
| 655 | remove_wait_queue(&idmap->idmap_wq, &wq); |
| 656 | goto out; |
| 657 | } |
| 658 | |
| 659 | set_current_state(TASK_UNINTERRUPTIBLE); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 660 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | schedule(); |
Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 662 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | remove_wait_queue(&idmap->idmap_wq, &wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 664 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | if (im->im_status & IDMAP_STATUS_SUCCESS) { |
| 667 | if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0) |
| 668 | goto out; |
| 669 | memcpy(name, im->im_name, len); |
| 670 | ret = len; |
| 671 | } |
| 672 | |
| 673 | out: |
| 674 | memset(im, 0, sizeof(*im)); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 675 | mutex_unlock(&idmap->idmap_im_lock); |
| 676 | mutex_unlock(&idmap->idmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | static ssize_t |
| 681 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) |
| 682 | { |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 683 | struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | struct idmap *idmap = (struct idmap *)rpci->private; |
| 685 | struct idmap_msg im_in, *im = &idmap->idmap_im; |
| 686 | struct idmap_hashtable *h; |
| 687 | struct idmap_hashent *he = NULL; |
Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 688 | size_t namelen_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | int ret; |
| 690 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 691 | if (mlen != sizeof(im_in)) |
| 692 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 694 | if (copy_from_user(&im_in, src, mlen) != 0) |
| 695 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 697 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | ret = mlen; |
| 700 | im->im_status = im_in.im_status; |
| 701 | /* If we got an error, terminate now, and wake up pending upcalls */ |
| 702 | if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) { |
| 703 | wake_up(&idmap->idmap_wq); |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | /* Sanity checking of strings */ |
| 708 | ret = -EINVAL; |
| 709 | namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ); |
| 710 | if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) |
| 711 | goto out; |
| 712 | |
| 713 | switch (im_in.im_type) { |
| 714 | case IDMAP_TYPE_USER: |
| 715 | h = &idmap->idmap_user_hash; |
| 716 | break; |
| 717 | case IDMAP_TYPE_GROUP: |
| 718 | h = &idmap->idmap_group_hash; |
| 719 | break; |
| 720 | default: |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | switch (im_in.im_conv) { |
| 725 | case IDMAP_CONV_IDTONAME: |
| 726 | /* Did we match the current upcall? */ |
| 727 | if (im->im_conv == IDMAP_CONV_IDTONAME |
| 728 | && im->im_type == im_in.im_type |
| 729 | && im->im_id == im_in.im_id) { |
| 730 | /* Yes: copy string, including the terminating '\0' */ |
| 731 | memcpy(im->im_name, im_in.im_name, namelen_in); |
| 732 | im->im_name[namelen_in] = '\0'; |
| 733 | wake_up(&idmap->idmap_wq); |
| 734 | } |
| 735 | he = idmap_alloc_id(h, im_in.im_id); |
| 736 | break; |
| 737 | case IDMAP_CONV_NAMETOID: |
| 738 | /* Did we match the current upcall? */ |
| 739 | if (im->im_conv == IDMAP_CONV_NAMETOID |
| 740 | && im->im_type == im_in.im_type |
| 741 | && strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in |
| 742 | && memcmp(im->im_name, im_in.im_name, namelen_in) == 0) { |
| 743 | im->im_id = im_in.im_id; |
| 744 | wake_up(&idmap->idmap_wq); |
| 745 | } |
| 746 | he = idmap_alloc_name(h, im_in.im_name, namelen_in); |
| 747 | break; |
| 748 | default: |
| 749 | goto out; |
| 750 | } |
| 751 | |
| 752 | /* If the entry is valid, also copy it to the cache */ |
| 753 | if (he != NULL) |
| 754 | idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id); |
| 755 | ret = mlen; |
| 756 | out: |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 757 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return ret; |
| 759 | } |
| 760 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 761 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg) |
| 763 | { |
| 764 | struct idmap_msg *im = msg->data; |
| 765 | struct idmap *idmap = container_of(im, struct idmap, idmap_im); |
| 766 | |
| 767 | if (msg->errno >= 0) |
| 768 | return; |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 769 | mutex_lock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | im->im_status = IDMAP_STATUS_LOOKUPFAIL; |
| 771 | wake_up(&idmap->idmap_wq); |
Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 772 | mutex_unlock(&idmap->idmap_im_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Fowler/Noll/Vo hash |
| 777 | * http://www.isthe.com/chongo/tech/comp/fnv/ |
| 778 | */ |
| 779 | |
| 780 | #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */ |
| 781 | #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */ |
| 782 | |
| 783 | static unsigned int fnvhash32(const void *buf, size_t buflen) |
| 784 | { |
| 785 | const unsigned char *p, *end = (const unsigned char *)buf + buflen; |
| 786 | unsigned int hash = FNV_1_32; |
| 787 | |
| 788 | for (p = buf; p < end; p++) { |
| 789 | hash *= FNV_P_32; |
| 790 | hash ^= (unsigned int)*p; |
| 791 | } |
| 792 | |
Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 793 | return hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 796 | 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] | 797 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 798 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 800 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 801 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid); |
| 803 | } |
| 804 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 805 | 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] | 806 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 807 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 809 | if (nfs_map_string_to_numeric(name, namelen, uid)) |
| 810 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid); |
| 812 | } |
| 813 | |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 814 | 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] | 815 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 816 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 817 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 819 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) |
| 820 | ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf); |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 821 | if (ret < 0) |
| 822 | ret = nfs_map_numeric_to_string(uid, buf, buflen); |
| 823 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 825 | 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] | 826 | { |
Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 827 | struct idmap *idmap = server->nfs_client->cl_idmap; |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 828 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 830 | if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) |
| 831 | ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf); |
Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 832 | if (ret < 0) |
| 833 | ret = nfs_map_numeric_to_string(uid, buf, buflen); |
| 834 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 837 | #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */ |