| 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> | 
 | 39 |  | 
 | 40 | static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res) | 
 | 41 | { | 
 | 42 | 	unsigned long val; | 
 | 43 | 	char buf[16]; | 
 | 44 |  | 
 | 45 | 	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf)) | 
 | 46 | 		return 0; | 
 | 47 | 	memcpy(buf, name, namelen); | 
 | 48 | 	buf[namelen] = '\0'; | 
 | 49 | 	if (strict_strtoul(buf, 0, &val) != 0) | 
 | 50 | 		return 0; | 
 | 51 | 	*res = val; | 
 | 52 | 	return 1; | 
 | 53 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
| Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 55 | static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen) | 
 | 56 | { | 
 | 57 | 	return snprintf(buf, buflen, "%u", id); | 
 | 58 | } | 
 | 59 |  | 
| Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 60 | #ifdef CONFIG_NFS_USE_NEW_IDMAPPER | 
 | 61 |  | 
 | 62 | #include <linux/slab.h> | 
 | 63 | #include <linux/cred.h> | 
| Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 64 | #include <linux/sunrpc/sched.h> | 
 | 65 | #include <linux/nfs4.h> | 
 | 66 | #include <linux/nfs_fs_sb.h> | 
| Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 67 | #include <linux/nfs_idmap.h> | 
 | 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/slab.h> | 
 | 288 | #include <linux/socket.h> | 
 | 289 | #include <linux/in.h> | 
 | 290 | #include <linux/sched.h> | 
 | 291 |  | 
 | 292 | #include <linux/sunrpc/clnt.h> | 
 | 293 | #include <linux/workqueue.h> | 
 | 294 | #include <linux/sunrpc/rpc_pipe_fs.h> | 
 | 295 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | #include <linux/nfs_fs.h> | 
 | 297 |  | 
 | 298 | #include <linux/nfs_idmap.h> | 
| Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 299 | #include "nfs4_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 |  | 
 | 301 | #define IDMAP_HASH_SZ          128 | 
 | 302 |  | 
| Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 303 | /* Default cache timeout is 10 minutes */ | 
 | 304 | unsigned int nfs_idmap_cache_timeout = 600 * HZ; | 
 | 305 |  | 
| David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 306 | static int param_set_idmap_timeout(const char *val, struct kernel_param *kp) | 
 | 307 | { | 
 | 308 | 	char *endp; | 
 | 309 | 	int num = simple_strtol(val, &endp, 0); | 
 | 310 | 	int jif = num * HZ; | 
 | 311 | 	if (endp == val || *endp || num < 0 || jif < num) | 
 | 312 | 		return -EINVAL; | 
 | 313 | 	*((int *)kp->arg) = jif; | 
 | 314 | 	return 0; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | module_param_call(idmap_cache_timeout, param_set_idmap_timeout, param_get_int, | 
 | 318 | 		 &nfs_idmap_cache_timeout, 0644); | 
 | 319 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct idmap_hashent { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 321 | 	unsigned long		ih_expires; | 
 | 322 | 	__u32			ih_id; | 
| Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 323 | 	size_t			ih_namelen; | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 324 | 	char			ih_name[IDMAP_NAMESZ]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | }; | 
 | 326 |  | 
 | 327 | struct idmap_hashtable { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 328 | 	__u8			h_type; | 
 | 329 | 	struct idmap_hashent	h_entries[IDMAP_HASH_SZ]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | }; | 
 | 331 |  | 
 | 332 | struct idmap { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 333 | 	struct dentry		*idmap_dentry; | 
 | 334 | 	wait_queue_head_t	idmap_wq; | 
 | 335 | 	struct idmap_msg	idmap_im; | 
 | 336 | 	struct mutex		idmap_lock;	/* Serializes upcalls */ | 
 | 337 | 	struct mutex		idmap_im_lock;	/* Protects the hashtable */ | 
 | 338 | 	struct idmap_hashtable	idmap_user_hash; | 
 | 339 | 	struct idmap_hashtable	idmap_group_hash; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | }; | 
 | 341 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 342 | static ssize_t idmap_pipe_upcall(struct file *, struct rpc_pipe_msg *, | 
 | 343 | 				 char __user *, size_t); | 
 | 344 | static ssize_t idmap_pipe_downcall(struct file *, const char __user *, | 
 | 345 | 				   size_t); | 
 | 346 | static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 |  | 
 | 348 | static unsigned int fnvhash32(const void *, size_t); | 
 | 349 |  | 
| Trond Myklebust | b693ba4 | 2009-08-09 15:14:15 -0400 | [diff] [blame] | 350 | static const struct rpc_pipe_ops idmap_upcall_ops = { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 351 | 	.upcall		= idmap_pipe_upcall, | 
 | 352 | 	.downcall	= idmap_pipe_downcall, | 
 | 353 | 	.destroy_msg	= idmap_pipe_destroy_msg, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | }; | 
 | 355 |  | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 356 | int | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 357 | nfs_idmap_new(struct nfs_client *clp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { | 
 | 359 | 	struct idmap *idmap; | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 360 | 	int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 |  | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 362 | 	BUG_ON(clp->cl_idmap != NULL); | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 363 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 364 | 	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL); | 
 | 365 | 	if (idmap == NULL) | 
 | 366 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 |  | 
| Trond Myklebust | 7d217ca | 2009-08-09 15:14:24 -0400 | [diff] [blame] | 368 | 	idmap->idmap_dentry = rpc_mkpipe(clp->cl_rpcclient->cl_path.dentry, | 
 | 369 | 			"idmap", idmap, &idmap_upcall_ops, 0); | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 370 | 	if (IS_ERR(idmap->idmap_dentry)) { | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 371 | 		error = PTR_ERR(idmap->idmap_dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 		kfree(idmap); | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 373 | 		return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | 	} | 
 | 375 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 376 | 	mutex_init(&idmap->idmap_lock); | 
 | 377 | 	mutex_init(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | 	init_waitqueue_head(&idmap->idmap_wq); | 
 | 379 | 	idmap->idmap_user_hash.h_type = IDMAP_TYPE_USER; | 
 | 380 | 	idmap->idmap_group_hash.h_type = IDMAP_TYPE_GROUP; | 
 | 381 |  | 
 | 382 | 	clp->cl_idmap = idmap; | 
| David Howells | b716279 | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 383 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } | 
 | 385 |  | 
 | 386 | void | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 387 | nfs_idmap_delete(struct nfs_client *clp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { | 
 | 389 | 	struct idmap *idmap = clp->cl_idmap; | 
 | 390 |  | 
 | 391 | 	if (!idmap) | 
 | 392 | 		return; | 
| Trond Myklebust | 5d67476 | 2006-07-31 14:11:48 -0700 | [diff] [blame] | 393 | 	rpc_unlink(idmap->idmap_dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | 	clp->cl_idmap = NULL; | 
 | 395 | 	kfree(idmap); | 
 | 396 | } | 
 | 397 |  | 
 | 398 | /* | 
 | 399 |  * Helper routines for manipulating the hashtable | 
 | 400 |  */ | 
 | 401 | static inline struct idmap_hashent * | 
 | 402 | idmap_name_hash(struct idmap_hashtable* h, const char *name, size_t len) | 
 | 403 | { | 
 | 404 | 	return &h->h_entries[fnvhash32(name, len) % IDMAP_HASH_SZ]; | 
 | 405 | } | 
 | 406 |  | 
 | 407 | static struct idmap_hashent * | 
 | 408 | idmap_lookup_name(struct idmap_hashtable *h, const char *name, size_t len) | 
 | 409 | { | 
 | 410 | 	struct idmap_hashent *he = idmap_name_hash(h, name, len); | 
 | 411 |  | 
 | 412 | 	if (he->ih_namelen != len || memcmp(he->ih_name, name, len) != 0) | 
 | 413 | 		return NULL; | 
| Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 414 | 	if (time_after(jiffies, he->ih_expires)) | 
 | 415 | 		return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | 	return he; | 
 | 417 | } | 
 | 418 |  | 
 | 419 | static inline struct idmap_hashent * | 
 | 420 | idmap_id_hash(struct idmap_hashtable* h, __u32 id) | 
 | 421 | { | 
 | 422 | 	return &h->h_entries[fnvhash32(&id, sizeof(id)) % IDMAP_HASH_SZ]; | 
 | 423 | } | 
 | 424 |  | 
 | 425 | static struct idmap_hashent * | 
 | 426 | idmap_lookup_id(struct idmap_hashtable *h, __u32 id) | 
 | 427 | { | 
 | 428 | 	struct idmap_hashent *he = idmap_id_hash(h, id); | 
 | 429 | 	if (he->ih_id != id || he->ih_namelen == 0) | 
 | 430 | 		return NULL; | 
| Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 431 | 	if (time_after(jiffies, he->ih_expires)) | 
 | 432 | 		return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | 	return he; | 
 | 434 | } | 
 | 435 |  | 
 | 436 | /* | 
 | 437 |  * Routines for allocating new entries in the hashtable. | 
 | 438 |  * For now, we just have 1 entry per bucket, so it's all | 
 | 439 |  * pretty trivial. | 
 | 440 |  */ | 
 | 441 | static inline struct idmap_hashent * | 
| Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 442 | idmap_alloc_name(struct idmap_hashtable *h, char *name, size_t len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { | 
 | 444 | 	return idmap_name_hash(h, name, len); | 
 | 445 | } | 
 | 446 |  | 
 | 447 | static inline struct idmap_hashent * | 
 | 448 | idmap_alloc_id(struct idmap_hashtable *h, __u32 id) | 
 | 449 | { | 
 | 450 | 	return idmap_id_hash(h, id); | 
 | 451 | } | 
 | 452 |  | 
 | 453 | static void | 
 | 454 | idmap_update_entry(struct idmap_hashent *he, const char *name, | 
 | 455 | 		size_t namelen, __u32 id) | 
 | 456 | { | 
 | 457 | 	he->ih_id = id; | 
 | 458 | 	memcpy(he->ih_name, name, namelen); | 
 | 459 | 	he->ih_name[namelen] = '\0'; | 
 | 460 | 	he->ih_namelen = namelen; | 
| Trond Myklebust | 58df095 | 2006-01-03 09:55:57 +0100 | [diff] [blame] | 461 | 	he->ih_expires = jiffies + nfs_idmap_cache_timeout; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } | 
 | 463 |  | 
 | 464 | /* | 
 | 465 |  * Name -> ID | 
 | 466 |  */ | 
 | 467 | static int | 
 | 468 | nfs_idmap_id(struct idmap *idmap, struct idmap_hashtable *h, | 
 | 469 | 		const char *name, size_t namelen, __u32 *id) | 
 | 470 | { | 
 | 471 | 	struct rpc_pipe_msg msg; | 
 | 472 | 	struct idmap_msg *im; | 
 | 473 | 	struct idmap_hashent *he; | 
 | 474 | 	DECLARE_WAITQUEUE(wq, current); | 
 | 475 | 	int ret = -EIO; | 
 | 476 |  | 
 | 477 | 	im = &idmap->idmap_im; | 
 | 478 |  | 
 | 479 | 	/* | 
 | 480 | 	 * String sanity checks | 
 | 481 | 	 * Note that the userland daemon expects NUL terminated strings | 
 | 482 | 	 */ | 
 | 483 | 	for (;;) { | 
 | 484 | 		if (namelen == 0) | 
 | 485 | 			return -EINVAL; | 
 | 486 | 		if (name[namelen-1] != '\0') | 
 | 487 | 			break; | 
 | 488 | 		namelen--; | 
 | 489 | 	} | 
 | 490 | 	if (namelen >= IDMAP_NAMESZ) | 
 | 491 | 		return -EINVAL; | 
 | 492 |  | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 493 | 	mutex_lock(&idmap->idmap_lock); | 
 | 494 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 |  | 
 | 496 | 	he = idmap_lookup_name(h, name, namelen); | 
 | 497 | 	if (he != NULL) { | 
 | 498 | 		*id = he->ih_id; | 
 | 499 | 		ret = 0; | 
 | 500 | 		goto out; | 
 | 501 | 	} | 
 | 502 |  | 
 | 503 | 	memset(im, 0, sizeof(*im)); | 
 | 504 | 	memcpy(im->im_name, name, namelen); | 
 | 505 |  | 
 | 506 | 	im->im_type = h->h_type; | 
 | 507 | 	im->im_conv = IDMAP_CONV_NAMETOID; | 
 | 508 |  | 
 | 509 | 	memset(&msg, 0, sizeof(msg)); | 
 | 510 | 	msg.data = im; | 
 | 511 | 	msg.len = sizeof(*im); | 
 | 512 |  | 
 | 513 | 	add_wait_queue(&idmap->idmap_wq, &wq); | 
 | 514 | 	if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { | 
 | 515 | 		remove_wait_queue(&idmap->idmap_wq, &wq); | 
 | 516 | 		goto out; | 
 | 517 | 	} | 
 | 518 |  | 
 | 519 | 	set_current_state(TASK_UNINTERRUPTIBLE); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 520 | 	mutex_unlock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | 	schedule(); | 
| Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 522 | 	__set_current_state(TASK_RUNNING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | 	remove_wait_queue(&idmap->idmap_wq, &wq); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 524 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
 | 526 | 	if (im->im_status & IDMAP_STATUS_SUCCESS) { | 
 | 527 | 		*id = im->im_id; | 
 | 528 | 		ret = 0; | 
 | 529 | 	} | 
 | 530 |  | 
 | 531 |  out: | 
 | 532 | 	memset(im, 0, sizeof(*im)); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 533 | 	mutex_unlock(&idmap->idmap_im_lock); | 
 | 534 | 	mutex_unlock(&idmap->idmap_lock); | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 535 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } | 
 | 537 |  | 
 | 538 | /* | 
 | 539 |  * ID -> Name | 
 | 540 |  */ | 
 | 541 | static int | 
 | 542 | nfs_idmap_name(struct idmap *idmap, struct idmap_hashtable *h, | 
 | 543 | 		__u32 id, char *name) | 
 | 544 | { | 
 | 545 | 	struct rpc_pipe_msg msg; | 
 | 546 | 	struct idmap_msg *im; | 
 | 547 | 	struct idmap_hashent *he; | 
 | 548 | 	DECLARE_WAITQUEUE(wq, current); | 
 | 549 | 	int ret = -EIO; | 
 | 550 | 	unsigned int len; | 
 | 551 |  | 
 | 552 | 	im = &idmap->idmap_im; | 
 | 553 |  | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 554 | 	mutex_lock(&idmap->idmap_lock); | 
 | 555 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 |  | 
 | 557 | 	he = idmap_lookup_id(h, id); | 
| Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 558 | 	if (he) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | 		memcpy(name, he->ih_name, he->ih_namelen); | 
 | 560 | 		ret = he->ih_namelen; | 
 | 561 | 		goto out; | 
 | 562 | 	} | 
 | 563 |  | 
 | 564 | 	memset(im, 0, sizeof(*im)); | 
 | 565 | 	im->im_type = h->h_type; | 
 | 566 | 	im->im_conv = IDMAP_CONV_IDTONAME; | 
 | 567 | 	im->im_id = id; | 
 | 568 |  | 
 | 569 | 	memset(&msg, 0, sizeof(msg)); | 
 | 570 | 	msg.data = im; | 
 | 571 | 	msg.len = sizeof(*im); | 
 | 572 |  | 
 | 573 | 	add_wait_queue(&idmap->idmap_wq, &wq); | 
 | 574 |  | 
 | 575 | 	if (rpc_queue_upcall(idmap->idmap_dentry->d_inode, &msg) < 0) { | 
 | 576 | 		remove_wait_queue(&idmap->idmap_wq, &wq); | 
 | 577 | 		goto out; | 
 | 578 | 	} | 
 | 579 |  | 
 | 580 | 	set_current_state(TASK_UNINTERRUPTIBLE); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 581 | 	mutex_unlock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | 	schedule(); | 
| Milind Arun Choudhary | fee7f23 | 2007-04-26 00:29:03 -0700 | [diff] [blame] | 583 | 	__set_current_state(TASK_RUNNING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | 	remove_wait_queue(&idmap->idmap_wq, &wq); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 585 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 |  | 
 | 587 | 	if (im->im_status & IDMAP_STATUS_SUCCESS) { | 
 | 588 | 		if ((len = strnlen(im->im_name, IDMAP_NAMESZ)) == 0) | 
 | 589 | 			goto out; | 
 | 590 | 		memcpy(name, im->im_name, len); | 
 | 591 | 		ret = len; | 
 | 592 | 	} | 
 | 593 |  | 
 | 594 |  out: | 
 | 595 | 	memset(im, 0, sizeof(*im)); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 596 | 	mutex_unlock(&idmap->idmap_im_lock); | 
 | 597 | 	mutex_unlock(&idmap->idmap_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | 	return ret; | 
 | 599 | } | 
 | 600 |  | 
 | 601 | /* RPC pipefs upcall/downcall routines */ | 
 | 602 | static ssize_t | 
 | 603 | idmap_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg, | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 604 | 		  char __user *dst, size_t buflen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 606 | 	char *data = (char *)msg->data + msg->copied; | 
| Chuck Lever | a661b77 | 2007-12-20 14:54:42 -0500 | [diff] [blame] | 607 | 	size_t mlen = min(msg->len, buflen); | 
 | 608 | 	unsigned long left; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 610 | 	left = copy_to_user(dst, data, mlen); | 
| Chuck Lever | a661b77 | 2007-12-20 14:54:42 -0500 | [diff] [blame] | 611 | 	if (left == mlen) { | 
 | 612 | 		msg->errno = -EFAULT; | 
 | 613 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | 	} | 
| Chuck Lever | a661b77 | 2007-12-20 14:54:42 -0500 | [diff] [blame] | 615 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | 	mlen -= left; | 
 | 617 | 	msg->copied += mlen; | 
 | 618 | 	msg->errno = 0; | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 619 | 	return mlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } | 
 | 621 |  | 
 | 622 | static ssize_t | 
 | 623 | idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) | 
 | 624 | { | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 625 | 	struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | 	struct idmap *idmap = (struct idmap *)rpci->private; | 
 | 627 | 	struct idmap_msg im_in, *im = &idmap->idmap_im; | 
 | 628 | 	struct idmap_hashtable *h; | 
 | 629 | 	struct idmap_hashent *he = NULL; | 
| Chuck Lever | d24aae4 | 2007-12-20 14:54:49 -0500 | [diff] [blame] | 630 | 	size_t namelen_in; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | 	int ret; | 
 | 632 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 633 | 	if (mlen != sizeof(im_in)) | 
 | 634 | 		return -ENOSPC; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 636 | 	if (copy_from_user(&im_in, src, mlen) != 0) | 
 | 637 | 		return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 639 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 |  | 
 | 641 | 	ret = mlen; | 
 | 642 | 	im->im_status = im_in.im_status; | 
 | 643 | 	/* If we got an error, terminate now, and wake up pending upcalls */ | 
 | 644 | 	if (!(im_in.im_status & IDMAP_STATUS_SUCCESS)) { | 
 | 645 | 		wake_up(&idmap->idmap_wq); | 
 | 646 | 		goto out; | 
 | 647 | 	} | 
 | 648 |  | 
 | 649 | 	/* Sanity checking of strings */ | 
 | 650 | 	ret = -EINVAL; | 
 | 651 | 	namelen_in = strnlen(im_in.im_name, IDMAP_NAMESZ); | 
 | 652 | 	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) | 
 | 653 | 		goto out; | 
 | 654 |  | 
 | 655 | 	switch (im_in.im_type) { | 
 | 656 | 		case IDMAP_TYPE_USER: | 
 | 657 | 			h = &idmap->idmap_user_hash; | 
 | 658 | 			break; | 
 | 659 | 		case IDMAP_TYPE_GROUP: | 
 | 660 | 			h = &idmap->idmap_group_hash; | 
 | 661 | 			break; | 
 | 662 | 		default: | 
 | 663 | 			goto out; | 
 | 664 | 	} | 
 | 665 |  | 
 | 666 | 	switch (im_in.im_conv) { | 
 | 667 | 	case IDMAP_CONV_IDTONAME: | 
 | 668 | 		/* Did we match the current upcall? */ | 
 | 669 | 		if (im->im_conv == IDMAP_CONV_IDTONAME | 
 | 670 | 				&& im->im_type == im_in.im_type | 
 | 671 | 				&& im->im_id == im_in.im_id) { | 
 | 672 | 			/* Yes: copy string, including the terminating '\0'  */ | 
 | 673 | 			memcpy(im->im_name, im_in.im_name, namelen_in); | 
 | 674 | 			im->im_name[namelen_in] = '\0'; | 
 | 675 | 			wake_up(&idmap->idmap_wq); | 
 | 676 | 		} | 
 | 677 | 		he = idmap_alloc_id(h, im_in.im_id); | 
 | 678 | 		break; | 
 | 679 | 	case IDMAP_CONV_NAMETOID: | 
 | 680 | 		/* Did we match the current upcall? */ | 
 | 681 | 		if (im->im_conv == IDMAP_CONV_NAMETOID | 
 | 682 | 				&& im->im_type == im_in.im_type | 
 | 683 | 				&& strnlen(im->im_name, IDMAP_NAMESZ) == namelen_in | 
 | 684 | 				&& memcmp(im->im_name, im_in.im_name, namelen_in) == 0) { | 
 | 685 | 			im->im_id = im_in.im_id; | 
 | 686 | 			wake_up(&idmap->idmap_wq); | 
 | 687 | 		} | 
 | 688 | 		he = idmap_alloc_name(h, im_in.im_name, namelen_in); | 
 | 689 | 		break; | 
 | 690 | 	default: | 
 | 691 | 		goto out; | 
 | 692 | 	} | 
 | 693 |  | 
 | 694 | 	/* If the entry is valid, also copy it to the cache */ | 
 | 695 | 	if (he != NULL) | 
 | 696 | 		idmap_update_entry(he, im_in.im_name, namelen_in, im_in.im_id); | 
 | 697 | 	ret = mlen; | 
 | 698 | out: | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 699 | 	mutex_unlock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | 	return ret; | 
 | 701 | } | 
 | 702 |  | 
| Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 703 | static void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg) | 
 | 705 | { | 
 | 706 | 	struct idmap_msg *im = msg->data; | 
 | 707 | 	struct idmap *idmap = container_of(im, struct idmap, idmap_im);  | 
 | 708 |  | 
 | 709 | 	if (msg->errno >= 0) | 
 | 710 | 		return; | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 711 | 	mutex_lock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | 	im->im_status = IDMAP_STATUS_LOOKUPFAIL; | 
 | 713 | 	wake_up(&idmap->idmap_wq); | 
| Ingo Molnar | c9d5128 | 2006-03-20 13:44:11 -0500 | [diff] [blame] | 714 | 	mutex_unlock(&idmap->idmap_im_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } | 
 | 716 |  | 
 | 717 | /*  | 
 | 718 |  * Fowler/Noll/Vo hash | 
 | 719 |  *    http://www.isthe.com/chongo/tech/comp/fnv/ | 
 | 720 |  */ | 
 | 721 |  | 
 | 722 | #define FNV_P_32 ((unsigned int)0x01000193) /* 16777619 */ | 
 | 723 | #define FNV_1_32 ((unsigned int)0x811c9dc5) /* 2166136261 */ | 
 | 724 |  | 
 | 725 | static unsigned int fnvhash32(const void *buf, size_t buflen) | 
 | 726 | { | 
 | 727 | 	const unsigned char *p, *end = (const unsigned char *)buf + buflen; | 
 | 728 | 	unsigned int hash = FNV_1_32; | 
 | 729 |  | 
 | 730 | 	for (p = buf; p < end; p++) { | 
 | 731 | 		hash *= FNV_P_32; | 
 | 732 | 		hash ^= (unsigned int)*p; | 
 | 733 | 	} | 
 | 734 |  | 
| Chuck Lever | 369af0f | 2007-12-20 14:54:35 -0500 | [diff] [blame] | 735 | 	return hash; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } | 
 | 737 |  | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 738 | 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] | 739 | { | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 740 | 	struct idmap *idmap = server->nfs_client->cl_idmap; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 |  | 
| Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 742 | 	if (nfs_map_string_to_numeric(name, namelen, uid)) | 
 | 743 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | 	return nfs_idmap_id(idmap, &idmap->idmap_user_hash, name, namelen, uid); | 
 | 745 | } | 
 | 746 |  | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 747 | 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] | 748 | { | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 749 | 	struct idmap *idmap = server->nfs_client->cl_idmap; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 |  | 
| Trond Myklebust | 5cf36cf | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 751 | 	if (nfs_map_string_to_numeric(name, namelen, uid)) | 
 | 752 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | 	return nfs_idmap_id(idmap, &idmap->idmap_group_hash, name, namelen, uid); | 
 | 754 | } | 
 | 755 |  | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 756 | 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] | 757 | { | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 758 | 	struct idmap *idmap = server->nfs_client->cl_idmap; | 
| Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 759 | 	int ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 |  | 
| Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 761 | 	if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) | 
 | 762 | 		ret = nfs_idmap_name(idmap, &idmap->idmap_user_hash, uid, buf); | 
| Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 763 | 	if (ret < 0) | 
 | 764 | 		ret = nfs_map_numeric_to_string(uid, buf, buflen); | 
 | 765 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 767 | 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] | 768 | { | 
| Trond Myklebust | e4fd72a | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 769 | 	struct idmap *idmap = server->nfs_client->cl_idmap; | 
| Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 770 | 	int ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 |  | 
| Trond Myklebust | b064eca2 | 2011-02-22 15:44:32 -0800 | [diff] [blame] | 772 | 	if (!(server->caps & NFS_CAP_UIDGID_NOMAP)) | 
 | 773 | 		ret = nfs_idmap_name(idmap, &idmap->idmap_group_hash, uid, buf); | 
| Trond Myklebust | f0b8516 | 2011-02-22 15:44:31 -0800 | [diff] [blame] | 774 | 	if (ret < 0) | 
 | 775 | 		ret = nfs_map_numeric_to_string(uid, buf, buflen); | 
 | 776 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } | 
 | 778 |  | 
| Bryan Schumaker | 955a857 | 2010-09-29 15:41:49 -0400 | [diff] [blame] | 779 | #endif /* CONFIG_NFS_USE_NEW_IDMAPPER */ |