blob: 6e2983b27f3ca4e6db8529da74adfeaffe09f4a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Mapping of UID/GIDs to name and vice versa.
3 *
4 * Copyright (c) 2002, 2003 The Regents of the University of
5 * Michigan. All rights reserved.
6 *
7 * Marius Aamodt Eriksen <marius@umich.edu>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/nfsd_idmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/seq_file.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020038#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Cache entry
42 */
43
44/*
45 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
46 * that.
47 */
48
49#define IDMAP_TYPE_USER 0
50#define IDMAP_TYPE_GROUP 1
51
52struct ent {
53 struct cache_head h;
54 int type; /* User / Group */
55 uid_t id;
56 char name[IDMAP_NAMESZ];
57 char authname[IDMAP_NAMESZ];
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Common entry handling */
61
62#define ENT_HASHBITS 8
63#define ENT_HASHMAX (1 << ENT_HASHBITS)
64#define ENT_HASHMASK (ENT_HASHMAX - 1)
65
NeilBrownf9ecc922006-03-27 01:15:06 -080066static void
67ent_init(struct cache_head *cnew, struct cache_head *citm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
NeilBrownf9ecc922006-03-27 01:15:06 -080069 struct ent *new = container_of(cnew, struct ent, h);
70 struct ent *itm = container_of(citm, struct ent, h);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 new->id = itm->id;
73 new->type = itm->type;
74
75 strlcpy(new->name, itm->name, sizeof(new->name));
76 strlcpy(new->authname, itm->authname, sizeof(new->name));
77}
78
NeilBrownfd39ca92005-06-23 22:04:03 -070079static void
NeilBrownbaab9352006-03-27 01:15:09 -080080ent_put(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
NeilBrownbaab9352006-03-27 01:15:09 -080082 struct ent *map = container_of(ref, struct ent, h.ref);
83 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
NeilBrownf9ecc922006-03-27 01:15:06 -080086static struct cache_head *
87ent_alloc(void)
88{
89 struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
90 if (e)
91 return &e->h;
92 else
93 return NULL;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * ID -> Name cache
98 */
99
100static struct cache_head *idtoname_table[ENT_HASHMAX];
101
102static uint32_t
103idtoname_hash(struct ent *ent)
104{
105 uint32_t hash;
106
107 hash = hash_str(ent->authname, ENT_HASHBITS);
108 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
109
110 /* Flip LSB for user/group */
111 if (ent->type == IDMAP_TYPE_GROUP)
112 hash ^= 1;
113
114 return hash;
115}
116
117static void
118idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
119 int *blen)
120{
121 struct ent *ent = container_of(ch, struct ent, h);
122 char idstr[11];
123
124 qword_add(bpp, blen, ent->authname);
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700125 snprintf(idstr, sizeof(idstr), "%u", ent->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
127 qword_add(bpp, blen, idstr);
128
129 (*bpp)[-1] = '\n';
130}
131
NeilBrownf9ecc922006-03-27 01:15:06 -0800132static int
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400133idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
134{
135 return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
136}
137
138static int
NeilBrownf9ecc922006-03-27 01:15:06 -0800139idtoname_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
NeilBrownf9ecc922006-03-27 01:15:06 -0800141 struct ent *a = container_of(ca, struct ent, h);
142 struct ent *b = container_of(cb, struct ent, h);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return (a->id == b->id && a->type == b->type &&
145 strcmp(a->authname, b->authname) == 0);
146}
147
148static int
149idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
150{
151 struct ent *ent;
152
153 if (h == NULL) {
154 seq_puts(m, "#domain type id [name]\n");
155 return 0;
156 }
157 ent = container_of(h, struct ent, h);
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700158 seq_printf(m, "%s %s %u", ent->authname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
160 ent->id);
161 if (test_bit(CACHE_VALID, &h->flags))
162 seq_printf(m, " %s", ent->name);
163 seq_printf(m, "\n");
164 return 0;
165}
166
167static void
Trond Myklebust2da8ca22009-08-09 15:14:26 -0400168warn_no_idmapd(struct cache_detail *detail, int has_died)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
Trond Myklebust2da8ca22009-08-09 15:14:26 -0400171 has_died ? "died" : "not been started");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174
175static int idtoname_parse(struct cache_detail *, char *, int);
NeilBrownf9ecc922006-03-27 01:15:06 -0800176static struct ent *idtoname_lookup(struct ent *);
177static struct ent *idtoname_update(struct ent *, struct ent *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
NeilBrownfd39ca92005-06-23 22:04:03 -0700179static struct cache_detail idtoname_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700180 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .hash_size = ENT_HASHMAX,
182 .hash_table = idtoname_table,
183 .name = "nfs4.idtoname",
184 .cache_put = ent_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400185 .cache_upcall = idtoname_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .cache_parse = idtoname_parse,
187 .cache_show = idtoname_show,
188 .warn_no_listener = warn_no_idmapd,
NeilBrownf9ecc922006-03-27 01:15:06 -0800189 .match = idtoname_match,
190 .init = ent_init,
191 .update = ent_init,
192 .alloc = ent_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Harvey Harrisona254b242008-02-20 12:49:00 -0800195static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
197{
198 struct ent ent, *res;
199 char *buf1, *bp;
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400200 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int error = -EINVAL;
202
203 if (buf[buflen - 1] != '\n')
204 return (-EINVAL);
205 buf[buflen - 1]= '\0';
206
207 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
208 if (buf1 == NULL)
209 return (-ENOMEM);
210
211 memset(&ent, 0, sizeof(ent));
212
213 /* Authentication name */
214 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
215 goto out;
216 memcpy(ent.authname, buf1, sizeof(ent.authname));
217
218 /* Type */
219 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
220 goto out;
221 ent.type = strcmp(buf1, "user") == 0 ?
222 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
223
224 /* ID */
225 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
226 goto out;
227 ent.id = simple_strtoul(buf1, &bp, 10);
228 if (bp == buf1)
229 goto out;
230
231 /* expiry */
232 ent.h.expiry_time = get_expiry(&buf);
233 if (ent.h.expiry_time == 0)
234 goto out;
235
NeilBrownf9ecc922006-03-27 01:15:06 -0800236 error = -ENOMEM;
237 res = idtoname_lookup(&ent);
238 if (!res)
239 goto out;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Name */
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400242 error = -EINVAL;
243 len = qword_get(&buf, buf1, PAGE_SIZE);
244 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400246 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 set_bit(CACHE_NEGATIVE, &ent.h.flags);
J. Bruce Fieldsd4395e02007-10-26 13:32:50 -0400248 else if (len >= IDMAP_NAMESZ)
249 goto out;
250 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 memcpy(ent.name, buf1, sizeof(ent.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 error = -ENOMEM;
NeilBrownf9ecc922006-03-27 01:15:06 -0800253 res = idtoname_update(&ent, res);
254 if (res == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out;
256
NeilBrownbaab9352006-03-27 01:15:09 -0800257 cache_put(&res->h, &idtoname_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 error = 0;
260out:
261 kfree(buf1);
262
263 return error;
264}
265
NeilBrownf9ecc922006-03-27 01:15:06 -0800266
267static struct ent *
268idtoname_lookup(struct ent *item)
269{
270 struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
271 &item->h,
272 idtoname_hash(item));
273 if (ch)
274 return container_of(ch, struct ent, h);
275 else
276 return NULL;
277}
278
279static struct ent *
280idtoname_update(struct ent *new, struct ent *old)
281{
282 struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
283 &new->h, &old->h,
284 idtoname_hash(new));
285 if (ch)
286 return container_of(ch, struct ent, h);
287 else
288 return NULL;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/*
293 * Name -> ID cache
294 */
295
296static struct cache_head *nametoid_table[ENT_HASHMAX];
297
298static inline int
299nametoid_hash(struct ent *ent)
300{
301 return hash_str(ent->name, ENT_HASHBITS);
302}
303
NeilBrownfd39ca92005-06-23 22:04:03 -0700304static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
306 int *blen)
307{
308 struct ent *ent = container_of(ch, struct ent, h);
309
310 qword_add(bpp, blen, ent->authname);
311 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
312 qword_add(bpp, blen, ent->name);
313
314 (*bpp)[-1] = '\n';
315}
316
NeilBrownf9ecc922006-03-27 01:15:06 -0800317static int
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400318nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
319{
320 return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
321}
322
323static int
NeilBrownf9ecc922006-03-27 01:15:06 -0800324nametoid_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
NeilBrownf9ecc922006-03-27 01:15:06 -0800326 struct ent *a = container_of(ca, struct ent, h);
327 struct ent *b = container_of(cb, struct ent, h);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
330 strcmp(a->authname, b->authname) == 0);
331}
332
333static int
334nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
335{
336 struct ent *ent;
337
338 if (h == NULL) {
339 seq_puts(m, "#domain type name [id]\n");
340 return 0;
341 }
342 ent = container_of(h, struct ent, h);
343 seq_printf(m, "%s %s %s", ent->authname,
344 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
345 ent->name);
346 if (test_bit(CACHE_VALID, &h->flags))
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700347 seq_printf(m, " %u", ent->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 seq_printf(m, "\n");
349 return 0;
350}
351
NeilBrownf9ecc922006-03-27 01:15:06 -0800352static struct ent *nametoid_lookup(struct ent *);
353static struct ent *nametoid_update(struct ent *, struct ent *);
NeilBrownfd39ca92005-06-23 22:04:03 -0700354static int nametoid_parse(struct cache_detail *, char *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
NeilBrownfd39ca92005-06-23 22:04:03 -0700356static struct cache_detail nametoid_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700357 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 .hash_size = ENT_HASHMAX,
359 .hash_table = nametoid_table,
360 .name = "nfs4.nametoid",
361 .cache_put = ent_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400362 .cache_upcall = nametoid_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .cache_parse = nametoid_parse,
364 .cache_show = nametoid_show,
365 .warn_no_listener = warn_no_idmapd,
NeilBrownf9ecc922006-03-27 01:15:06 -0800366 .match = nametoid_match,
367 .init = ent_init,
368 .update = ent_init,
369 .alloc = ent_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
NeilBrownfd39ca92005-06-23 22:04:03 -0700372static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
374{
375 struct ent ent, *res;
376 char *buf1;
377 int error = -EINVAL;
378
379 if (buf[buflen - 1] != '\n')
380 return (-EINVAL);
381 buf[buflen - 1]= '\0';
382
383 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
384 if (buf1 == NULL)
385 return (-ENOMEM);
386
387 memset(&ent, 0, sizeof(ent));
388
389 /* Authentication name */
390 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
391 goto out;
392 memcpy(ent.authname, buf1, sizeof(ent.authname));
393
394 /* Type */
395 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
396 goto out;
397 ent.type = strcmp(buf1, "user") == 0 ?
398 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
399
400 /* Name */
401 error = qword_get(&buf, buf1, PAGE_SIZE);
402 if (error <= 0 || error >= IDMAP_NAMESZ)
403 goto out;
404 memcpy(ent.name, buf1, sizeof(ent.name));
405
406 /* expiry */
407 ent.h.expiry_time = get_expiry(&buf);
408 if (ent.h.expiry_time == 0)
409 goto out;
410
411 /* ID */
412 error = get_int(&buf, &ent.id);
413 if (error == -EINVAL)
414 goto out;
415 if (error == -ENOENT)
416 set_bit(CACHE_NEGATIVE, &ent.h.flags);
417
418 error = -ENOMEM;
NeilBrownf9ecc922006-03-27 01:15:06 -0800419 res = nametoid_lookup(&ent);
420 if (res == NULL)
421 goto out;
422 res = nametoid_update(&ent, res);
423 if (res == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto out;
425
NeilBrownbaab9352006-03-27 01:15:09 -0800426 cache_put(&res->h, &nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 error = 0;
428out:
429 kfree(buf1);
430
431 return (error);
432}
433
NeilBrownf9ecc922006-03-27 01:15:06 -0800434
435static struct ent *
436nametoid_lookup(struct ent *item)
437{
438 struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
439 &item->h,
440 nametoid_hash(item));
441 if (ch)
442 return container_of(ch, struct ent, h);
443 else
444 return NULL;
445}
446
447static struct ent *
448nametoid_update(struct ent *new, struct ent *old)
449{
450 struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
451 &new->h, &old->h,
452 nametoid_hash(new));
453 if (ch)
454 return container_of(ch, struct ent, h);
455 else
456 return NULL;
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459/*
460 * Exported API
461 */
462
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500463int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464nfsd_idmap_init(void)
465{
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500466 int rv;
467
468 rv = cache_register(&idtoname_cache);
469 if (rv)
470 return rv;
471 rv = cache_register(&nametoid_cache);
472 if (rv)
473 cache_unregister(&idtoname_cache);
474 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477void
478nfsd_idmap_shutdown(void)
479{
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500480 cache_unregister(&idtoname_cache);
481 cache_unregister(&nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484/*
485 * Deferred request handling
486 */
487
488struct idmap_defer_req {
489 struct cache_req req;
490 struct cache_deferred_req deferred_req;
491 wait_queue_head_t waitq;
492 atomic_t count;
493};
494
495static inline void
496put_mdr(struct idmap_defer_req *mdr)
497{
498 if (atomic_dec_and_test(&mdr->count))
499 kfree(mdr);
500}
501
502static inline void
503get_mdr(struct idmap_defer_req *mdr)
504{
505 atomic_inc(&mdr->count);
506}
507
508static void
509idmap_revisit(struct cache_deferred_req *dreq, int toomany)
510{
511 struct idmap_defer_req *mdr =
512 container_of(dreq, struct idmap_defer_req, deferred_req);
513
514 wake_up(&mdr->waitq);
515 put_mdr(mdr);
516}
517
518static struct cache_deferred_req *
519idmap_defer(struct cache_req *req)
520{
521 struct idmap_defer_req *mdr =
522 container_of(req, struct idmap_defer_req, req);
523
524 mdr->deferred_req.revisit = idmap_revisit;
525 get_mdr(mdr);
526 return (&mdr->deferred_req);
527}
528
529static inline int
NeilBrownf9ecc922006-03-27 01:15:06 -0800530do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct cache_detail *detail, struct ent **item,
532 struct idmap_defer_req *mdr)
533{
NeilBrownf9ecc922006-03-27 01:15:06 -0800534 *item = lookup_fn(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!*item)
536 return -ENOMEM;
537 return cache_check(detail, &(*item)->h, &mdr->req);
538}
539
540static inline int
NeilBrownf9ecc922006-03-27 01:15:06 -0800541do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct ent *key, struct cache_detail *detail,
543 struct ent **item)
544{
545 int ret = -ENOMEM;
546
NeilBrownf9ecc922006-03-27 01:15:06 -0800547 *item = lookup_fn(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!*item)
549 goto out_err;
550 ret = -ETIMEDOUT;
551 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
552 || (*item)->h.expiry_time < get_seconds()
553 || detail->flush_time > (*item)->h.last_refresh)
554 goto out_put;
555 ret = -ENOENT;
556 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
557 goto out_put;
558 return 0;
559out_put:
NeilBrownbaab9352006-03-27 01:15:09 -0800560 cache_put(&(*item)->h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561out_err:
562 *item = NULL;
563 return ret;
564}
565
566static int
567idmap_lookup(struct svc_rqst *rqstp,
NeilBrownf9ecc922006-03-27 01:15:06 -0800568 struct ent *(*lookup_fn)(struct ent *), struct ent *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct cache_detail *detail, struct ent **item)
570{
571 struct idmap_defer_req *mdr;
572 int ret;
573
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700574 mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!mdr)
576 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 atomic_set(&mdr->count, 1);
578 init_waitqueue_head(&mdr->waitq);
579 mdr->req.defer = idmap_defer;
580 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
581 if (ret == -EAGAIN) {
582 wait_event_interruptible_timeout(mdr->waitq,
583 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
584 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
585 }
586 put_mdr(mdr);
587 return ret;
588}
589
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700590static char *
591rqst_authname(struct svc_rqst *rqstp)
592{
593 struct auth_domain *clp;
594
595 clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
596 return clp->name;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static int
600idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
601 uid_t *id)
602{
603 struct ent *item, key = {
604 .type = type,
605 };
606 int ret;
607
608 if (namelen + 1 > sizeof(key.name))
609 return -EINVAL;
610 memcpy(key.name, name, namelen);
611 key.name[namelen] = '\0';
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700612 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
614 if (ret == -ENOENT)
615 ret = -ESRCH; /* nfserr_badname */
616 if (ret)
617 return ret;
618 *id = item->id;
NeilBrownbaab9352006-03-27 01:15:09 -0800619 cache_put(&item->h, &nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 0;
621}
622
623static int
624idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
625{
626 struct ent *item, key = {
627 .id = id,
628 .type = type,
629 };
630 int ret;
631
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700632 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
634 if (ret == -ENOENT)
635 return sprintf(name, "%u", id);
636 if (ret)
637 return ret;
638 ret = strlen(item->name);
639 BUG_ON(ret > IDMAP_NAMESZ);
640 memcpy(name, item->name, ret);
NeilBrownbaab9352006-03-27 01:15:09 -0800641 cache_put(&item->h, &idtoname_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return ret;
643}
644
645int
646nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
647 __u32 *id)
648{
649 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
650}
651
652int
653nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
654 __u32 *id)
655{
656 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
657}
658
659int
660nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
661{
662 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
663}
664
665int
666nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
667{
668 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
669}