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