blob: ba2c199592fd450d41c9778f70cf4fbcb3d46f16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/init.h>
39
40#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sunrpc/cache.h>
49#include <linux/nfsd_idmap.h>
50#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/time.h>
52#include <linux/seq_file.h>
53#include <linux/sunrpc/svcauth.h>
54
55/*
56 * Cache entry
57 */
58
59/*
60 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
61 * that.
62 */
63
64#define IDMAP_TYPE_USER 0
65#define IDMAP_TYPE_GROUP 1
66
67struct ent {
68 struct cache_head h;
69 int type; /* User / Group */
70 uid_t id;
71 char name[IDMAP_NAMESZ];
72 char authname[IDMAP_NAMESZ];
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Common entry handling */
76
77#define ENT_HASHBITS 8
78#define ENT_HASHMAX (1 << ENT_HASHBITS)
79#define ENT_HASHMASK (ENT_HASHMAX - 1)
80
NeilBrownf9ecc922006-03-27 01:15:06 -080081static void
82ent_init(struct cache_head *cnew, struct cache_head *citm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
NeilBrownf9ecc922006-03-27 01:15:06 -080084 struct ent *new = container_of(cnew, struct ent, h);
85 struct ent *itm = container_of(citm, struct ent, h);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 new->id = itm->id;
88 new->type = itm->type;
89
90 strlcpy(new->name, itm->name, sizeof(new->name));
91 strlcpy(new->authname, itm->authname, sizeof(new->name));
92}
93
NeilBrownfd39ca92005-06-23 22:04:03 -070094static void
NeilBrownbaab9352006-03-27 01:15:09 -080095ent_put(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
NeilBrownbaab9352006-03-27 01:15:09 -080097 struct ent *map = container_of(ref, struct ent, h.ref);
98 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
NeilBrownf9ecc922006-03-27 01:15:06 -0800101static struct cache_head *
102ent_alloc(void)
103{
104 struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
105 if (e)
106 return &e->h;
107 else
108 return NULL;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * ID -> Name cache
113 */
114
115static struct cache_head *idtoname_table[ENT_HASHMAX];
116
117static uint32_t
118idtoname_hash(struct ent *ent)
119{
120 uint32_t hash;
121
122 hash = hash_str(ent->authname, ENT_HASHBITS);
123 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
124
125 /* Flip LSB for user/group */
126 if (ent->type == IDMAP_TYPE_GROUP)
127 hash ^= 1;
128
129 return hash;
130}
131
132static void
133idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
134 int *blen)
135{
136 struct ent *ent = container_of(ch, struct ent, h);
137 char idstr[11];
138
139 qword_add(bpp, blen, ent->authname);
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700140 snprintf(idstr, sizeof(idstr), "%u", ent->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
142 qword_add(bpp, blen, idstr);
143
144 (*bpp)[-1] = '\n';
145}
146
NeilBrownf9ecc922006-03-27 01:15:06 -0800147static int
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400148idtoname_upcall(struct cache_detail *cd, struct cache_head *ch)
149{
150 return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request);
151}
152
153static int
NeilBrownf9ecc922006-03-27 01:15:06 -0800154idtoname_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
NeilBrownf9ecc922006-03-27 01:15:06 -0800156 struct ent *a = container_of(ca, struct ent, h);
157 struct ent *b = container_of(cb, struct ent, h);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return (a->id == b->id && a->type == b->type &&
160 strcmp(a->authname, b->authname) == 0);
161}
162
163static int
164idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
165{
166 struct ent *ent;
167
168 if (h == NULL) {
169 seq_puts(m, "#domain type id [name]\n");
170 return 0;
171 }
172 ent = container_of(h, struct ent, h);
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700173 seq_printf(m, "%s %s %u", ent->authname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
175 ent->id);
176 if (test_bit(CACHE_VALID, &h->flags))
177 seq_printf(m, " %s", ent->name);
178 seq_printf(m, "\n");
179 return 0;
180}
181
182static void
Trond Myklebust2da8ca22009-08-09 15:14:26 -0400183warn_no_idmapd(struct cache_detail *detail, int has_died)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
Trond Myklebust2da8ca22009-08-09 15:14:26 -0400186 has_died ? "died" : "not been started");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189
190static int idtoname_parse(struct cache_detail *, char *, int);
NeilBrownf9ecc922006-03-27 01:15:06 -0800191static struct ent *idtoname_lookup(struct ent *);
192static struct ent *idtoname_update(struct ent *, struct ent *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
NeilBrownfd39ca92005-06-23 22:04:03 -0700194static struct cache_detail idtoname_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700195 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .hash_size = ENT_HASHMAX,
197 .hash_table = idtoname_table,
198 .name = "nfs4.idtoname",
199 .cache_put = ent_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400200 .cache_upcall = idtoname_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .cache_parse = idtoname_parse,
202 .cache_show = idtoname_show,
203 .warn_no_listener = warn_no_idmapd,
NeilBrownf9ecc922006-03-27 01:15:06 -0800204 .match = idtoname_match,
205 .init = ent_init,
206 .update = ent_init,
207 .alloc = ent_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Harvey Harrisona254b242008-02-20 12:49:00 -0800210static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
212{
213 struct ent ent, *res;
214 char *buf1, *bp;
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400215 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int error = -EINVAL;
217
218 if (buf[buflen - 1] != '\n')
219 return (-EINVAL);
220 buf[buflen - 1]= '\0';
221
222 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
223 if (buf1 == NULL)
224 return (-ENOMEM);
225
226 memset(&ent, 0, sizeof(ent));
227
228 /* Authentication name */
229 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
230 goto out;
231 memcpy(ent.authname, buf1, sizeof(ent.authname));
232
233 /* Type */
234 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
235 goto out;
236 ent.type = strcmp(buf1, "user") == 0 ?
237 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
238
239 /* ID */
240 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
241 goto out;
242 ent.id = simple_strtoul(buf1, &bp, 10);
243 if (bp == buf1)
244 goto out;
245
246 /* expiry */
247 ent.h.expiry_time = get_expiry(&buf);
248 if (ent.h.expiry_time == 0)
249 goto out;
250
NeilBrownf9ecc922006-03-27 01:15:06 -0800251 error = -ENOMEM;
252 res = idtoname_lookup(&ent);
253 if (!res)
254 goto out;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Name */
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400257 error = -EINVAL;
258 len = qword_get(&buf, buf1, PAGE_SIZE);
259 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
J. Bruce Fieldsc9b6cbe2007-07-27 16:36:45 -0400261 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 set_bit(CACHE_NEGATIVE, &ent.h.flags);
J. Bruce Fieldsd4395e02007-10-26 13:32:50 -0400263 else if (len >= IDMAP_NAMESZ)
264 goto out;
265 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 memcpy(ent.name, buf1, sizeof(ent.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 error = -ENOMEM;
NeilBrownf9ecc922006-03-27 01:15:06 -0800268 res = idtoname_update(&ent, res);
269 if (res == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out;
271
NeilBrownbaab9352006-03-27 01:15:09 -0800272 cache_put(&res->h, &idtoname_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 error = 0;
275out:
276 kfree(buf1);
277
278 return error;
279}
280
NeilBrownf9ecc922006-03-27 01:15:06 -0800281
282static struct ent *
283idtoname_lookup(struct ent *item)
284{
285 struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
286 &item->h,
287 idtoname_hash(item));
288 if (ch)
289 return container_of(ch, struct ent, h);
290 else
291 return NULL;
292}
293
294static struct ent *
295idtoname_update(struct ent *new, struct ent *old)
296{
297 struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
298 &new->h, &old->h,
299 idtoname_hash(new));
300 if (ch)
301 return container_of(ch, struct ent, h);
302 else
303 return NULL;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*
308 * Name -> ID cache
309 */
310
311static struct cache_head *nametoid_table[ENT_HASHMAX];
312
313static inline int
314nametoid_hash(struct ent *ent)
315{
316 return hash_str(ent->name, ENT_HASHBITS);
317}
318
NeilBrownfd39ca92005-06-23 22:04:03 -0700319static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
321 int *blen)
322{
323 struct ent *ent = container_of(ch, struct ent, h);
324
325 qword_add(bpp, blen, ent->authname);
326 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
327 qword_add(bpp, blen, ent->name);
328
329 (*bpp)[-1] = '\n';
330}
331
NeilBrownf9ecc922006-03-27 01:15:06 -0800332static int
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400333nametoid_upcall(struct cache_detail *cd, struct cache_head *ch)
334{
335 return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request);
336}
337
338static int
NeilBrownf9ecc922006-03-27 01:15:06 -0800339nametoid_match(struct cache_head *ca, struct cache_head *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
NeilBrownf9ecc922006-03-27 01:15:06 -0800341 struct ent *a = container_of(ca, struct ent, h);
342 struct ent *b = container_of(cb, struct ent, h);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
345 strcmp(a->authname, b->authname) == 0);
346}
347
348static int
349nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
350{
351 struct ent *ent;
352
353 if (h == NULL) {
354 seq_puts(m, "#domain type name [id]\n");
355 return 0;
356 }
357 ent = container_of(h, struct ent, h);
358 seq_printf(m, "%s %s %s", ent->authname,
359 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
360 ent->name);
361 if (test_bit(CACHE_VALID, &h->flags))
J. Bruce Fields0a725fc2007-07-31 00:37:52 -0700362 seq_printf(m, " %u", ent->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 seq_printf(m, "\n");
364 return 0;
365}
366
NeilBrownf9ecc922006-03-27 01:15:06 -0800367static struct ent *nametoid_lookup(struct ent *);
368static struct ent *nametoid_update(struct ent *, struct ent *);
NeilBrownfd39ca92005-06-23 22:04:03 -0700369static int nametoid_parse(struct cache_detail *, char *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
NeilBrownfd39ca92005-06-23 22:04:03 -0700371static struct cache_detail nametoid_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700372 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .hash_size = ENT_HASHMAX,
374 .hash_table = nametoid_table,
375 .name = "nfs4.nametoid",
376 .cache_put = ent_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400377 .cache_upcall = nametoid_upcall,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 .cache_parse = nametoid_parse,
379 .cache_show = nametoid_show,
380 .warn_no_listener = warn_no_idmapd,
NeilBrownf9ecc922006-03-27 01:15:06 -0800381 .match = nametoid_match,
382 .init = ent_init,
383 .update = ent_init,
384 .alloc = ent_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
NeilBrownfd39ca92005-06-23 22:04:03 -0700387static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
389{
390 struct ent ent, *res;
391 char *buf1;
392 int error = -EINVAL;
393
394 if (buf[buflen - 1] != '\n')
395 return (-EINVAL);
396 buf[buflen - 1]= '\0';
397
398 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
399 if (buf1 == NULL)
400 return (-ENOMEM);
401
402 memset(&ent, 0, sizeof(ent));
403
404 /* Authentication name */
405 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
406 goto out;
407 memcpy(ent.authname, buf1, sizeof(ent.authname));
408
409 /* Type */
410 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
411 goto out;
412 ent.type = strcmp(buf1, "user") == 0 ?
413 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
414
415 /* Name */
416 error = qword_get(&buf, buf1, PAGE_SIZE);
417 if (error <= 0 || error >= IDMAP_NAMESZ)
418 goto out;
419 memcpy(ent.name, buf1, sizeof(ent.name));
420
421 /* expiry */
422 ent.h.expiry_time = get_expiry(&buf);
423 if (ent.h.expiry_time == 0)
424 goto out;
425
426 /* ID */
427 error = get_int(&buf, &ent.id);
428 if (error == -EINVAL)
429 goto out;
430 if (error == -ENOENT)
431 set_bit(CACHE_NEGATIVE, &ent.h.flags);
432
433 error = -ENOMEM;
NeilBrownf9ecc922006-03-27 01:15:06 -0800434 res = nametoid_lookup(&ent);
435 if (res == NULL)
436 goto out;
437 res = nametoid_update(&ent, res);
438 if (res == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto out;
440
NeilBrownbaab9352006-03-27 01:15:09 -0800441 cache_put(&res->h, &nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 error = 0;
443out:
444 kfree(buf1);
445
446 return (error);
447}
448
NeilBrownf9ecc922006-03-27 01:15:06 -0800449
450static struct ent *
451nametoid_lookup(struct ent *item)
452{
453 struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
454 &item->h,
455 nametoid_hash(item));
456 if (ch)
457 return container_of(ch, struct ent, h);
458 else
459 return NULL;
460}
461
462static struct ent *
463nametoid_update(struct ent *new, struct ent *old)
464{
465 struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
466 &new->h, &old->h,
467 nametoid_hash(new));
468 if (ch)
469 return container_of(ch, struct ent, h);
470 else
471 return NULL;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/*
475 * Exported API
476 */
477
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500478int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479nfsd_idmap_init(void)
480{
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500481 int rv;
482
483 rv = cache_register(&idtoname_cache);
484 if (rv)
485 return rv;
486 rv = cache_register(&nametoid_cache);
487 if (rv)
488 cache_unregister(&idtoname_cache);
489 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492void
493nfsd_idmap_shutdown(void)
494{
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500495 cache_unregister(&idtoname_cache);
496 cache_unregister(&nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/*
500 * Deferred request handling
501 */
502
503struct idmap_defer_req {
504 struct cache_req req;
505 struct cache_deferred_req deferred_req;
506 wait_queue_head_t waitq;
507 atomic_t count;
508};
509
510static inline void
511put_mdr(struct idmap_defer_req *mdr)
512{
513 if (atomic_dec_and_test(&mdr->count))
514 kfree(mdr);
515}
516
517static inline void
518get_mdr(struct idmap_defer_req *mdr)
519{
520 atomic_inc(&mdr->count);
521}
522
523static void
524idmap_revisit(struct cache_deferred_req *dreq, int toomany)
525{
526 struct idmap_defer_req *mdr =
527 container_of(dreq, struct idmap_defer_req, deferred_req);
528
529 wake_up(&mdr->waitq);
530 put_mdr(mdr);
531}
532
533static struct cache_deferred_req *
534idmap_defer(struct cache_req *req)
535{
536 struct idmap_defer_req *mdr =
537 container_of(req, struct idmap_defer_req, req);
538
539 mdr->deferred_req.revisit = idmap_revisit;
540 get_mdr(mdr);
541 return (&mdr->deferred_req);
542}
543
544static inline int
NeilBrownf9ecc922006-03-27 01:15:06 -0800545do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct cache_detail *detail, struct ent **item,
547 struct idmap_defer_req *mdr)
548{
NeilBrownf9ecc922006-03-27 01:15:06 -0800549 *item = lookup_fn(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!*item)
551 return -ENOMEM;
552 return cache_check(detail, &(*item)->h, &mdr->req);
553}
554
555static inline int
NeilBrownf9ecc922006-03-27 01:15:06 -0800556do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct ent *key, struct cache_detail *detail,
558 struct ent **item)
559{
560 int ret = -ENOMEM;
561
NeilBrownf9ecc922006-03-27 01:15:06 -0800562 *item = lookup_fn(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!*item)
564 goto out_err;
565 ret = -ETIMEDOUT;
566 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
567 || (*item)->h.expiry_time < get_seconds()
568 || detail->flush_time > (*item)->h.last_refresh)
569 goto out_put;
570 ret = -ENOENT;
571 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
572 goto out_put;
573 return 0;
574out_put:
NeilBrownbaab9352006-03-27 01:15:09 -0800575 cache_put(&(*item)->h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out_err:
577 *item = NULL;
578 return ret;
579}
580
581static int
582idmap_lookup(struct svc_rqst *rqstp,
NeilBrownf9ecc922006-03-27 01:15:06 -0800583 struct ent *(*lookup_fn)(struct ent *), struct ent *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct cache_detail *detail, struct ent **item)
585{
586 struct idmap_defer_req *mdr;
587 int ret;
588
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700589 mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!mdr)
591 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 atomic_set(&mdr->count, 1);
593 init_waitqueue_head(&mdr->waitq);
594 mdr->req.defer = idmap_defer;
595 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
596 if (ret == -EAGAIN) {
597 wait_event_interruptible_timeout(mdr->waitq,
598 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
599 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
600 }
601 put_mdr(mdr);
602 return ret;
603}
604
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700605static char *
606rqst_authname(struct svc_rqst *rqstp)
607{
608 struct auth_domain *clp;
609
610 clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
611 return clp->name;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static int
615idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
616 uid_t *id)
617{
618 struct ent *item, key = {
619 .type = type,
620 };
621 int ret;
622
623 if (namelen + 1 > sizeof(key.name))
624 return -EINVAL;
625 memcpy(key.name, name, namelen);
626 key.name[namelen] = '\0';
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700627 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
629 if (ret == -ENOENT)
630 ret = -ESRCH; /* nfserr_badname */
631 if (ret)
632 return ret;
633 *id = item->id;
NeilBrownbaab9352006-03-27 01:15:09 -0800634 cache_put(&item->h, &nametoid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636}
637
638static int
639idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
640{
641 struct ent *item, key = {
642 .id = id,
643 .type = type,
644 };
645 int ret;
646
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700647 strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
649 if (ret == -ENOENT)
650 return sprintf(name, "%u", id);
651 if (ret)
652 return ret;
653 ret = strlen(item->name);
654 BUG_ON(ret > IDMAP_NAMESZ);
655 memcpy(name, item->name, ret);
NeilBrownbaab9352006-03-27 01:15:09 -0800656 cache_put(&item->h, &idtoname_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ret;
658}
659
660int
661nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
662 __u32 *id)
663{
664 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
665}
666
667int
668nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
669 __u32 *id)
670{
671 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
672}
673
674int
675nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
676{
677 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
678}
679
680int
681nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
682{
683 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
684}