| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  fs/nfsd/nfs4idmap.c | 
 | 3 |  * | 
 | 4 |  *  Mapping of UID/GIDs to name and vice versa. | 
 | 5 |  * | 
 | 6 |  *  Copyright (c) 2002, 2003 The Regents of the University of | 
 | 7 |  *  Michigan.  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 |  */ | 
 | 36 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> | 
 | 38 | #include <linux/init.h> | 
 | 39 |  | 
 | 40 | #include <linux/mm.h> | 
 | 41 | #include <linux/utsname.h> | 
 | 42 | #include <linux/errno.h> | 
 | 43 | #include <linux/string.h> | 
 | 44 | #include <linux/sunrpc/clnt.h> | 
 | 45 | #include <linux/nfs.h> | 
 | 46 | #include <linux/nfs4.h> | 
 | 47 | #include <linux/nfs_fs.h> | 
 | 48 | #include <linux/nfs_page.h> | 
 | 49 | #include <linux/smp_lock.h> | 
 | 50 | #include <linux/sunrpc/cache.h> | 
 | 51 | #include <linux/nfsd_idmap.h> | 
 | 52 | #include <linux/list.h> | 
 | 53 | #include <linux/sched.h> | 
 | 54 | #include <linux/time.h> | 
 | 55 | #include <linux/seq_file.h> | 
 | 56 | #include <linux/sunrpc/svcauth.h> | 
 | 57 |  | 
 | 58 | /* | 
 | 59 |  * Cache entry | 
 | 60 |  */ | 
 | 61 |  | 
 | 62 | /* | 
 | 63 |  * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on | 
 | 64 |  * that. | 
 | 65 |  */ | 
 | 66 |  | 
 | 67 | #define IDMAP_TYPE_USER  0 | 
 | 68 | #define IDMAP_TYPE_GROUP 1 | 
 | 69 |  | 
 | 70 | struct ent { | 
 | 71 | 	struct cache_head h; | 
 | 72 | 	int               type;		       /* User / Group */ | 
 | 73 | 	uid_t             id; | 
 | 74 | 	char              name[IDMAP_NAMESZ]; | 
 | 75 | 	char              authname[IDMAP_NAMESZ]; | 
 | 76 | }; | 
 | 77 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* Common entry handling */ | 
 | 79 |  | 
 | 80 | #define ENT_HASHBITS          8 | 
 | 81 | #define ENT_HASHMAX           (1 << ENT_HASHBITS) | 
 | 82 | #define ENT_HASHMASK          (ENT_HASHMAX - 1) | 
 | 83 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 84 | static void | 
 | 85 | ent_init(struct cache_head *cnew, struct cache_head *citm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 87 | 	struct ent *new = container_of(cnew, struct ent, h); | 
 | 88 | 	struct ent *itm = container_of(citm, struct ent, h); | 
 | 89 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | 	new->id = itm->id; | 
 | 91 | 	new->type = itm->type; | 
 | 92 |  | 
 | 93 | 	strlcpy(new->name, itm->name, sizeof(new->name)); | 
 | 94 | 	strlcpy(new->authname, itm->authname, sizeof(new->name)); | 
 | 95 | } | 
 | 96 |  | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 97 | static void | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 98 | ent_put(struct kref *ref) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 100 | 	struct ent *map = container_of(ref, struct ent, h.ref); | 
 | 101 | 	kfree(map); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } | 
 | 103 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 104 | static struct cache_head * | 
 | 105 | ent_alloc(void) | 
 | 106 | { | 
 | 107 | 	struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL); | 
 | 108 | 	if (e) | 
 | 109 | 		return &e->h; | 
 | 110 | 	else | 
 | 111 | 		return NULL; | 
 | 112 | } | 
 | 113 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* | 
 | 115 |  * ID -> Name cache | 
 | 116 |  */ | 
 | 117 |  | 
 | 118 | static struct cache_head *idtoname_table[ENT_HASHMAX]; | 
 | 119 |  | 
 | 120 | static uint32_t | 
 | 121 | idtoname_hash(struct ent *ent) | 
 | 122 | { | 
 | 123 | 	uint32_t hash; | 
 | 124 |  | 
 | 125 | 	hash = hash_str(ent->authname, ENT_HASHBITS); | 
 | 126 | 	hash = hash_long(hash ^ ent->id, ENT_HASHBITS); | 
 | 127 |  | 
 | 128 | 	/* Flip LSB for user/group */ | 
 | 129 | 	if (ent->type == IDMAP_TYPE_GROUP) | 
 | 130 | 		hash ^= 1; | 
 | 131 |  | 
 | 132 | 	return hash; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | static void | 
 | 136 | idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp, | 
 | 137 |     int *blen) | 
 | 138 | { | 
 | 139 |  	struct ent *ent = container_of(ch, struct ent, h); | 
 | 140 | 	char idstr[11]; | 
 | 141 |  | 
 | 142 | 	qword_add(bpp, blen, ent->authname); | 
 | 143 | 	snprintf(idstr, sizeof(idstr), "%d", ent->id); | 
 | 144 | 	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user"); | 
 | 145 | 	qword_add(bpp, blen, idstr); | 
 | 146 |  | 
 | 147 | 	(*bpp)[-1] = '\n'; | 
 | 148 | } | 
 | 149 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 150 | static int | 
 | 151 | idtoname_match(struct cache_head *ca, struct cache_head *cb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 153 | 	struct ent *a = container_of(ca, struct ent, h); | 
 | 154 | 	struct ent *b = container_of(cb, struct ent, h); | 
 | 155 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | 	return (a->id == b->id && a->type == b->type && | 
 | 157 | 	    strcmp(a->authname, b->authname) == 0); | 
 | 158 | } | 
 | 159 |  | 
 | 160 | static int | 
 | 161 | idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h) | 
 | 162 | { | 
 | 163 | 	struct ent *ent; | 
 | 164 |  | 
 | 165 | 	if (h == NULL) { | 
 | 166 | 		seq_puts(m, "#domain type id [name]\n"); | 
 | 167 | 		return 0; | 
 | 168 | 	} | 
 | 169 | 	ent = container_of(h, struct ent, h); | 
 | 170 | 	seq_printf(m, "%s %s %d", ent->authname, | 
 | 171 | 			ent->type == IDMAP_TYPE_GROUP ? "group" : "user", | 
 | 172 | 			ent->id); | 
 | 173 | 	if (test_bit(CACHE_VALID, &h->flags)) | 
 | 174 | 		seq_printf(m, " %s", ent->name); | 
 | 175 | 	seq_printf(m, "\n"); | 
 | 176 | 	return 0; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | static void | 
 | 180 | warn_no_idmapd(struct cache_detail *detail) | 
 | 181 | { | 
 | 182 | 	printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n", | 
 | 183 | 			detail->last_close? "died" : "not been started"); | 
 | 184 | } | 
 | 185 |  | 
 | 186 |  | 
 | 187 | static int         idtoname_parse(struct cache_detail *, char *, int); | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 188 | static struct ent *idtoname_lookup(struct ent *); | 
 | 189 | static struct ent *idtoname_update(struct ent *, struct ent *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 |  | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 191 | static struct cache_detail idtoname_cache = { | 
| Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 192 | 	.owner		= THIS_MODULE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | 	.hash_size	= ENT_HASHMAX, | 
 | 194 | 	.hash_table	= idtoname_table, | 
 | 195 | 	.name		= "nfs4.idtoname", | 
 | 196 | 	.cache_put	= ent_put, | 
 | 197 | 	.cache_request	= idtoname_request, | 
 | 198 | 	.cache_parse	= idtoname_parse, | 
 | 199 | 	.cache_show	= idtoname_show, | 
 | 200 | 	.warn_no_listener = warn_no_idmapd, | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 201 | 	.match		= idtoname_match, | 
 | 202 | 	.init		= ent_init, | 
 | 203 | 	.update		= ent_init, | 
 | 204 | 	.alloc		= ent_alloc, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | }; | 
 | 206 |  | 
 | 207 | int | 
 | 208 | idtoname_parse(struct cache_detail *cd, char *buf, int buflen) | 
 | 209 | { | 
 | 210 | 	struct ent ent, *res; | 
 | 211 | 	char *buf1, *bp; | 
 | 212 | 	int error = -EINVAL; | 
 | 213 |  | 
 | 214 | 	if (buf[buflen - 1] != '\n') | 
 | 215 | 		return (-EINVAL); | 
 | 216 | 	buf[buflen - 1]= '\0'; | 
 | 217 |  | 
 | 218 | 	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
 | 219 | 	if (buf1 == NULL) | 
 | 220 | 		return (-ENOMEM); | 
 | 221 |  | 
 | 222 | 	memset(&ent, 0, sizeof(ent)); | 
 | 223 |  | 
 | 224 | 	/* Authentication name */ | 
 | 225 | 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) | 
 | 226 | 		goto out; | 
 | 227 | 	memcpy(ent.authname, buf1, sizeof(ent.authname)); | 
 | 228 |  | 
 | 229 | 	/* Type */ | 
 | 230 | 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) | 
 | 231 | 		goto out; | 
 | 232 | 	ent.type = strcmp(buf1, "user") == 0 ? | 
 | 233 | 		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP; | 
 | 234 |  | 
 | 235 | 	/* ID */ | 
 | 236 | 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) | 
 | 237 | 		goto out; | 
 | 238 | 	ent.id = simple_strtoul(buf1, &bp, 10); | 
 | 239 | 	if (bp == buf1) | 
 | 240 | 		goto out; | 
 | 241 |  | 
 | 242 | 	/* expiry */ | 
 | 243 | 	ent.h.expiry_time = get_expiry(&buf); | 
 | 244 | 	if (ent.h.expiry_time == 0) | 
 | 245 | 		goto out; | 
 | 246 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 247 | 	error = -ENOMEM; | 
 | 248 | 	res = idtoname_lookup(&ent); | 
 | 249 | 	if (!res) | 
 | 250 | 		goto out; | 
 | 251 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | 	/* Name */ | 
 | 253 | 	error = qword_get(&buf, buf1, PAGE_SIZE); | 
 | 254 | 	if (error == -EINVAL) | 
 | 255 | 		goto out; | 
 | 256 | 	if (error == -ENOENT) | 
 | 257 | 		set_bit(CACHE_NEGATIVE, &ent.h.flags); | 
 | 258 | 	else { | 
 | 259 | 		if (error >= IDMAP_NAMESZ) { | 
 | 260 | 			error = -EINVAL; | 
 | 261 | 			goto out; | 
 | 262 | 		} | 
 | 263 | 		memcpy(ent.name, buf1, sizeof(ent.name)); | 
 | 264 | 	} | 
 | 265 | 	error = -ENOMEM; | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 266 | 	res = idtoname_update(&ent, res); | 
 | 267 | 	if (res == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 		goto out; | 
 | 269 |  | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 270 | 	cache_put(&res->h, &idtoname_cache); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 |  | 
 | 272 | 	error = 0; | 
 | 273 | out: | 
 | 274 | 	kfree(buf1); | 
 | 275 |  | 
 | 276 | 	return error; | 
 | 277 | } | 
 | 278 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 279 |  | 
 | 280 | static struct ent * | 
 | 281 | idtoname_lookup(struct ent *item) | 
 | 282 | { | 
 | 283 | 	struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache, | 
 | 284 | 						    &item->h, | 
 | 285 | 						    idtoname_hash(item)); | 
 | 286 | 	if (ch) | 
 | 287 | 		return container_of(ch, struct ent, h); | 
 | 288 | 	else | 
 | 289 | 		return NULL; | 
 | 290 | } | 
 | 291 |  | 
 | 292 | static struct ent * | 
 | 293 | idtoname_update(struct ent *new, struct ent *old) | 
 | 294 | { | 
 | 295 | 	struct cache_head *ch = sunrpc_cache_update(&idtoname_cache, | 
 | 296 | 						    &new->h, &old->h, | 
 | 297 | 						    idtoname_hash(new)); | 
 | 298 | 	if (ch) | 
 | 299 | 		return container_of(ch, struct ent, h); | 
 | 300 | 	else | 
 | 301 | 		return NULL; | 
 | 302 | } | 
 | 303 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 |  | 
 | 305 | /* | 
 | 306 |  * Name -> ID cache | 
 | 307 |  */ | 
 | 308 |  | 
 | 309 | static struct cache_head *nametoid_table[ENT_HASHMAX]; | 
 | 310 |  | 
 | 311 | static inline int | 
 | 312 | nametoid_hash(struct ent *ent) | 
 | 313 | { | 
 | 314 | 	return hash_str(ent->name, ENT_HASHBITS); | 
 | 315 | } | 
 | 316 |  | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 317 | static void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp, | 
 | 319 |     int *blen) | 
 | 320 | { | 
 | 321 |  	struct ent *ent = container_of(ch, struct ent, h); | 
 | 322 |  | 
 | 323 | 	qword_add(bpp, blen, ent->authname); | 
 | 324 | 	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user"); | 
 | 325 | 	qword_add(bpp, blen, ent->name); | 
 | 326 |  | 
 | 327 | 	(*bpp)[-1] = '\n'; | 
 | 328 | } | 
 | 329 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 330 | static int | 
 | 331 | nametoid_match(struct cache_head *ca, struct cache_head *cb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 333 | 	struct ent *a = container_of(ca, struct ent, h); | 
 | 334 | 	struct ent *b = container_of(cb, struct ent, h); | 
 | 335 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | 	return (a->type == b->type && strcmp(a->name, b->name) == 0 && | 
 | 337 | 	    strcmp(a->authname, b->authname) == 0); | 
 | 338 | } | 
 | 339 |  | 
 | 340 | static int | 
 | 341 | nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h) | 
 | 342 | { | 
 | 343 | 	struct ent *ent; | 
 | 344 |  | 
 | 345 | 	if (h == NULL) { | 
 | 346 | 		seq_puts(m, "#domain type name [id]\n"); | 
 | 347 | 		return 0; | 
 | 348 | 	} | 
 | 349 | 	ent = container_of(h, struct ent, h); | 
 | 350 | 	seq_printf(m, "%s %s %s", ent->authname, | 
 | 351 | 			ent->type == IDMAP_TYPE_GROUP ? "group" : "user", | 
 | 352 | 			ent->name); | 
 | 353 | 	if (test_bit(CACHE_VALID, &h->flags)) | 
 | 354 | 		seq_printf(m, " %d", ent->id); | 
 | 355 | 	seq_printf(m, "\n"); | 
 | 356 | 	return 0; | 
 | 357 | } | 
 | 358 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 359 | static struct ent *nametoid_lookup(struct ent *); | 
 | 360 | static struct ent *nametoid_update(struct ent *, struct ent *); | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 361 | static int         nametoid_parse(struct cache_detail *, char *, int); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 |  | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 363 | static struct cache_detail nametoid_cache = { | 
| Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 364 | 	.owner		= THIS_MODULE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 	.hash_size	= ENT_HASHMAX, | 
 | 366 | 	.hash_table	= nametoid_table, | 
 | 367 | 	.name		= "nfs4.nametoid", | 
 | 368 | 	.cache_put	= ent_put, | 
 | 369 | 	.cache_request	= nametoid_request, | 
 | 370 | 	.cache_parse	= nametoid_parse, | 
 | 371 | 	.cache_show	= nametoid_show, | 
 | 372 | 	.warn_no_listener = warn_no_idmapd, | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 373 | 	.match		= nametoid_match, | 
 | 374 | 	.init		= ent_init, | 
 | 375 | 	.update		= ent_init, | 
 | 376 | 	.alloc		= ent_alloc, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | }; | 
 | 378 |  | 
| NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 379 | static int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | nametoid_parse(struct cache_detail *cd, char *buf, int buflen) | 
 | 381 | { | 
 | 382 | 	struct ent ent, *res; | 
 | 383 | 	char *buf1; | 
 | 384 | 	int error = -EINVAL; | 
 | 385 |  | 
 | 386 | 	if (buf[buflen - 1] != '\n') | 
 | 387 | 		return (-EINVAL); | 
 | 388 | 	buf[buflen - 1]= '\0'; | 
 | 389 |  | 
 | 390 | 	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
 | 391 | 	if (buf1 == NULL) | 
 | 392 | 		return (-ENOMEM); | 
 | 393 |  | 
 | 394 | 	memset(&ent, 0, sizeof(ent)); | 
 | 395 |  | 
 | 396 | 	/* Authentication name */ | 
 | 397 | 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) | 
 | 398 | 		goto out; | 
 | 399 | 	memcpy(ent.authname, buf1, sizeof(ent.authname)); | 
 | 400 |  | 
 | 401 | 	/* Type */ | 
 | 402 | 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0) | 
 | 403 | 		goto out; | 
 | 404 | 	ent.type = strcmp(buf1, "user") == 0 ? | 
 | 405 | 		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP; | 
 | 406 |  | 
 | 407 | 	/* Name */ | 
 | 408 | 	error = qword_get(&buf, buf1, PAGE_SIZE); | 
 | 409 | 	if (error <= 0 || error >= IDMAP_NAMESZ) | 
 | 410 | 		goto out; | 
 | 411 | 	memcpy(ent.name, buf1, sizeof(ent.name)); | 
 | 412 |  | 
 | 413 | 	/* expiry */ | 
 | 414 | 	ent.h.expiry_time = get_expiry(&buf); | 
 | 415 | 	if (ent.h.expiry_time == 0) | 
 | 416 | 		goto out; | 
 | 417 |  | 
 | 418 | 	/* ID */ | 
 | 419 | 	error = get_int(&buf, &ent.id); | 
 | 420 | 	if (error == -EINVAL) | 
 | 421 | 		goto out; | 
 | 422 | 	if (error == -ENOENT) | 
 | 423 | 		set_bit(CACHE_NEGATIVE, &ent.h.flags); | 
 | 424 |  | 
 | 425 | 	error = -ENOMEM; | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 426 | 	res = nametoid_lookup(&ent); | 
 | 427 | 	if (res == NULL) | 
 | 428 | 		goto out; | 
 | 429 | 	res = nametoid_update(&ent, res); | 
 | 430 | 	if (res == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | 		goto out; | 
 | 432 |  | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 433 | 	cache_put(&res->h, &nametoid_cache); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | 	error = 0; | 
 | 435 | out: | 
 | 436 | 	kfree(buf1); | 
 | 437 |  | 
 | 438 | 	return (error); | 
 | 439 | } | 
 | 440 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 441 |  | 
 | 442 | static struct ent * | 
 | 443 | nametoid_lookup(struct ent *item) | 
 | 444 | { | 
 | 445 | 	struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache, | 
 | 446 | 						    &item->h, | 
 | 447 | 						    nametoid_hash(item)); | 
 | 448 | 	if (ch) | 
 | 449 | 		return container_of(ch, struct ent, h); | 
 | 450 | 	else | 
 | 451 | 		return NULL; | 
 | 452 | } | 
 | 453 |  | 
 | 454 | static struct ent * | 
 | 455 | nametoid_update(struct ent *new, struct ent *old) | 
 | 456 | { | 
 | 457 | 	struct cache_head *ch = sunrpc_cache_update(&nametoid_cache, | 
 | 458 | 						    &new->h, &old->h, | 
 | 459 | 						    nametoid_hash(new)); | 
 | 460 | 	if (ch) | 
 | 461 | 		return container_of(ch, struct ent, h); | 
 | 462 | 	else | 
 | 463 | 		return NULL; | 
 | 464 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 |  | 
 | 466 | /* | 
 | 467 |  * Exported API | 
 | 468 |  */ | 
 | 469 |  | 
 | 470 | void | 
 | 471 | nfsd_idmap_init(void) | 
 | 472 | { | 
 | 473 | 	cache_register(&idtoname_cache); | 
 | 474 | 	cache_register(&nametoid_cache); | 
 | 475 | } | 
 | 476 |  | 
 | 477 | void | 
 | 478 | nfsd_idmap_shutdown(void) | 
 | 479 | { | 
| Bruce Allan | f35279d | 2005-09-06 15:17:08 -0700 | [diff] [blame] | 480 | 	if (cache_unregister(&idtoname_cache)) | 
 | 481 | 		printk(KERN_ERR "nfsd: failed to unregister idtoname cache\n"); | 
 | 482 | 	if (cache_unregister(&nametoid_cache)) | 
 | 483 | 		printk(KERN_ERR "nfsd: failed to unregister nametoid cache\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } | 
 | 485 |  | 
 | 486 | /* | 
 | 487 |  * Deferred request handling | 
 | 488 |  */ | 
 | 489 |  | 
 | 490 | struct idmap_defer_req { | 
 | 491 |        struct cache_req		req; | 
 | 492 |        struct cache_deferred_req deferred_req; | 
 | 493 |        wait_queue_head_t	waitq; | 
 | 494 |        atomic_t			count; | 
 | 495 | }; | 
 | 496 |  | 
 | 497 | static inline void | 
 | 498 | put_mdr(struct idmap_defer_req *mdr) | 
 | 499 | { | 
 | 500 | 	if (atomic_dec_and_test(&mdr->count)) | 
 | 501 | 		kfree(mdr); | 
 | 502 | } | 
 | 503 |  | 
 | 504 | static inline void | 
 | 505 | get_mdr(struct idmap_defer_req *mdr) | 
 | 506 | { | 
 | 507 | 	atomic_inc(&mdr->count); | 
 | 508 | } | 
 | 509 |  | 
 | 510 | static void | 
 | 511 | idmap_revisit(struct cache_deferred_req *dreq, int toomany) | 
 | 512 | { | 
 | 513 | 	struct idmap_defer_req *mdr = | 
 | 514 | 		container_of(dreq, struct idmap_defer_req, deferred_req); | 
 | 515 |  | 
 | 516 | 	wake_up(&mdr->waitq); | 
 | 517 | 	put_mdr(mdr); | 
 | 518 | } | 
 | 519 |  | 
 | 520 | static struct cache_deferred_req * | 
 | 521 | idmap_defer(struct cache_req *req) | 
 | 522 | { | 
 | 523 | 	struct idmap_defer_req *mdr = | 
 | 524 | 		container_of(req, struct idmap_defer_req, req); | 
 | 525 |  | 
 | 526 | 	mdr->deferred_req.revisit = idmap_revisit; | 
 | 527 | 	get_mdr(mdr); | 
 | 528 | 	return (&mdr->deferred_req); | 
 | 529 | } | 
 | 530 |  | 
 | 531 | static inline int | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 532 | do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | 		struct cache_detail *detail, struct ent **item, | 
 | 534 | 		struct idmap_defer_req *mdr) | 
 | 535 | { | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 536 | 	*item = lookup_fn(key); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | 	if (!*item) | 
 | 538 | 		return -ENOMEM; | 
 | 539 | 	return cache_check(detail, &(*item)->h, &mdr->req); | 
 | 540 | } | 
 | 541 |  | 
 | 542 | static inline int | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 543 | do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | 			struct ent *key, struct cache_detail *detail, | 
 | 545 | 			struct ent **item) | 
 | 546 | { | 
 | 547 | 	int ret = -ENOMEM; | 
 | 548 |  | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 549 | 	*item = lookup_fn(key); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | 	if (!*item) | 
 | 551 | 		goto out_err; | 
 | 552 | 	ret = -ETIMEDOUT; | 
 | 553 | 	if (!test_bit(CACHE_VALID, &(*item)->h.flags) | 
 | 554 | 			|| (*item)->h.expiry_time < get_seconds() | 
 | 555 | 			|| detail->flush_time > (*item)->h.last_refresh) | 
 | 556 | 		goto out_put; | 
 | 557 | 	ret = -ENOENT; | 
 | 558 | 	if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags)) | 
 | 559 | 		goto out_put; | 
 | 560 | 	return 0; | 
 | 561 | out_put: | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 562 | 	cache_put(&(*item)->h, detail); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | out_err: | 
 | 564 | 	*item = NULL; | 
 | 565 | 	return ret; | 
 | 566 | } | 
 | 567 |  | 
 | 568 | static int | 
 | 569 | idmap_lookup(struct svc_rqst *rqstp, | 
| NeilBrown | f9ecc92 | 2006-03-27 01:15:06 -0800 | [diff] [blame] | 570 | 		struct ent *(*lookup_fn)(struct ent *), struct ent *key, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | 		struct cache_detail *detail, struct ent **item) | 
 | 572 | { | 
 | 573 | 	struct idmap_defer_req *mdr; | 
 | 574 | 	int ret; | 
 | 575 |  | 
| Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 576 | 	mdr = kzalloc(sizeof(*mdr), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | 	if (!mdr) | 
 | 578 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | 	atomic_set(&mdr->count, 1); | 
 | 580 | 	init_waitqueue_head(&mdr->waitq); | 
 | 581 | 	mdr->req.defer = idmap_defer; | 
 | 582 | 	ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr); | 
 | 583 | 	if (ret == -EAGAIN) { | 
 | 584 | 		wait_event_interruptible_timeout(mdr->waitq, | 
 | 585 | 			test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ); | 
 | 586 | 		ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item); | 
 | 587 | 	} | 
 | 588 | 	put_mdr(mdr); | 
 | 589 | 	return ret; | 
 | 590 | } | 
 | 591 |  | 
 | 592 | static int | 
 | 593 | idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, | 
 | 594 | 		uid_t *id) | 
 | 595 | { | 
 | 596 | 	struct ent *item, key = { | 
 | 597 | 		.type = type, | 
 | 598 | 	}; | 
 | 599 | 	int ret; | 
 | 600 |  | 
 | 601 | 	if (namelen + 1 > sizeof(key.name)) | 
 | 602 | 		return -EINVAL; | 
 | 603 | 	memcpy(key.name, name, namelen); | 
 | 604 | 	key.name[namelen] = '\0'; | 
 | 605 | 	strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname)); | 
 | 606 | 	ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item); | 
 | 607 | 	if (ret == -ENOENT) | 
 | 608 | 		ret = -ESRCH; /* nfserr_badname */ | 
 | 609 | 	if (ret) | 
 | 610 | 		return ret; | 
 | 611 | 	*id = item->id; | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 612 | 	cache_put(&item->h, &nametoid_cache); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | 	return 0; | 
 | 614 | } | 
 | 615 |  | 
 | 616 | static int | 
 | 617 | idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name) | 
 | 618 | { | 
 | 619 | 	struct ent *item, key = { | 
 | 620 | 		.id = id, | 
 | 621 | 		.type = type, | 
 | 622 | 	}; | 
 | 623 | 	int ret; | 
 | 624 |  | 
 | 625 | 	strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname)); | 
 | 626 | 	ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item); | 
 | 627 | 	if (ret == -ENOENT) | 
 | 628 | 		return sprintf(name, "%u", id); | 
 | 629 | 	if (ret) | 
 | 630 | 		return ret; | 
 | 631 | 	ret = strlen(item->name); | 
 | 632 | 	BUG_ON(ret > IDMAP_NAMESZ); | 
 | 633 | 	memcpy(name, item->name, ret); | 
| NeilBrown | baab935 | 2006-03-27 01:15:09 -0800 | [diff] [blame] | 634 | 	cache_put(&item->h, &idtoname_cache); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | 	return ret; | 
 | 636 | } | 
 | 637 |  | 
 | 638 | int | 
 | 639 | nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen, | 
 | 640 | 		__u32 *id) | 
 | 641 | { | 
 | 642 | 	return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id); | 
 | 643 | } | 
 | 644 |  | 
 | 645 | int | 
 | 646 | nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen, | 
 | 647 | 		__u32 *id) | 
 | 648 | { | 
 | 649 | 	return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id); | 
 | 650 | } | 
 | 651 |  | 
 | 652 | int | 
 | 653 | nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name) | 
 | 654 | { | 
 | 655 | 	return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name); | 
 | 656 | } | 
 | 657 |  | 
 | 658 | int | 
 | 659 | nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name) | 
 | 660 | { | 
 | 661 | 	return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name); | 
 | 662 | } |