Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
37 | #include <linux/module.h> |
38 | #include <linux/init.h> | |
39 | ||
40 | #include <linux/mm.h> | |
1da177e4 LT |
41 | #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> | |
1da177e4 LT |
48 | #include <linux/sunrpc/cache.h> |
49 | #include <linux/nfsd_idmap.h> | |
50 | #include <linux/list.h> | |
1da177e4 LT |
51 | #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 | ||
67 | struct 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 | ||
1da177e4 LT |
75 | /* Common entry handling */ |
76 | ||
77 | #define ENT_HASHBITS 8 | |
78 | #define ENT_HASHMAX (1 << ENT_HASHBITS) | |
79 | #define ENT_HASHMASK (ENT_HASHMAX - 1) | |
80 | ||
f9ecc921 N |
81 | static void |
82 | ent_init(struct cache_head *cnew, struct cache_head *citm) | |
1da177e4 | 83 | { |
f9ecc921 N |
84 | struct ent *new = container_of(cnew, struct ent, h); |
85 | struct ent *itm = container_of(citm, struct ent, h); | |
86 | ||
1da177e4 LT |
87 | 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 | ||
fd39ca9a | 94 | static void |
baab935f | 95 | ent_put(struct kref *ref) |
1da177e4 | 96 | { |
baab935f N |
97 | struct ent *map = container_of(ref, struct ent, h.ref); |
98 | kfree(map); | |
1da177e4 LT |
99 | } |
100 | ||
f9ecc921 N |
101 | static struct cache_head * |
102 | ent_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 | ||
1da177e4 LT |
111 | /* |
112 | * ID -> Name cache | |
113 | */ | |
114 | ||
115 | static struct cache_head *idtoname_table[ENT_HASHMAX]; | |
116 | ||
117 | static uint32_t | |
118 | idtoname_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 | ||
132 | static void | |
133 | idtoname_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); | |
0a725fc4 | 140 | snprintf(idstr, sizeof(idstr), "%u", ent->id); |
1da177e4 LT |
141 | qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user"); |
142 | qword_add(bpp, blen, idstr); | |
143 | ||
144 | (*bpp)[-1] = '\n'; | |
145 | } | |
146 | ||
bc74b4f5 TM |
147 | static int |
148 | idtoname_upcall(struct cache_detail *cd, struct cache_head *ch) | |
149 | { | |
150 | return sunrpc_cache_pipe_upcall(cd, ch, idtoname_request); | |
151 | } | |
152 | ||
f9ecc921 N |
153 | static int |
154 | idtoname_match(struct cache_head *ca, struct cache_head *cb) | |
1da177e4 | 155 | { |
f9ecc921 N |
156 | struct ent *a = container_of(ca, struct ent, h); |
157 | struct ent *b = container_of(cb, struct ent, h); | |
158 | ||
1da177e4 LT |
159 | return (a->id == b->id && a->type == b->type && |
160 | strcmp(a->authname, b->authname) == 0); | |
161 | } | |
162 | ||
163 | static int | |
164 | idtoname_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); | |
0a725fc4 | 173 | seq_printf(m, "%s %s %u", ent->authname, |
1da177e4 LT |
174 | 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 | ||
182 | static void | |
2da8ca26 | 183 | warn_no_idmapd(struct cache_detail *detail, int has_died) |
1da177e4 LT |
184 | { |
185 | printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n", | |
2da8ca26 | 186 | has_died ? "died" : "not been started"); |
1da177e4 LT |
187 | } |
188 | ||
189 | ||
190 | static int idtoname_parse(struct cache_detail *, char *, int); | |
f9ecc921 N |
191 | static struct ent *idtoname_lookup(struct ent *); |
192 | static struct ent *idtoname_update(struct ent *, struct ent *); | |
1da177e4 | 193 | |
fd39ca9a | 194 | static struct cache_detail idtoname_cache = { |
f35279d3 | 195 | .owner = THIS_MODULE, |
1da177e4 LT |
196 | .hash_size = ENT_HASHMAX, |
197 | .hash_table = idtoname_table, | |
198 | .name = "nfs4.idtoname", | |
199 | .cache_put = ent_put, | |
bc74b4f5 | 200 | .cache_upcall = idtoname_upcall, |
1da177e4 LT |
201 | .cache_parse = idtoname_parse, |
202 | .cache_show = idtoname_show, | |
203 | .warn_no_listener = warn_no_idmapd, | |
f9ecc921 N |
204 | .match = idtoname_match, |
205 | .init = ent_init, | |
206 | .update = ent_init, | |
207 | .alloc = ent_alloc, | |
1da177e4 LT |
208 | }; |
209 | ||
a254b246 | 210 | static int |
1da177e4 LT |
211 | idtoname_parse(struct cache_detail *cd, char *buf, int buflen) |
212 | { | |
213 | struct ent ent, *res; | |
214 | char *buf1, *bp; | |
c9b6cbe5 | 215 | int len; |
1da177e4 LT |
216 | 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 | ||
f9ecc921 N |
251 | error = -ENOMEM; |
252 | res = idtoname_lookup(&ent); | |
253 | if (!res) | |
254 | goto out; | |
255 | ||
1da177e4 | 256 | /* Name */ |
c9b6cbe5 BF |
257 | error = -EINVAL; |
258 | len = qword_get(&buf, buf1, PAGE_SIZE); | |
259 | if (len < 0) | |
1da177e4 | 260 | goto out; |
c9b6cbe5 | 261 | if (len == 0) |
1da177e4 | 262 | set_bit(CACHE_NEGATIVE, &ent.h.flags); |
d4395e03 BF |
263 | else if (len >= IDMAP_NAMESZ) |
264 | goto out; | |
265 | else | |
1da177e4 | 266 | memcpy(ent.name, buf1, sizeof(ent.name)); |
1da177e4 | 267 | error = -ENOMEM; |
f9ecc921 N |
268 | res = idtoname_update(&ent, res); |
269 | if (res == NULL) | |
1da177e4 LT |
270 | goto out; |
271 | ||
baab935f | 272 | cache_put(&res->h, &idtoname_cache); |
1da177e4 LT |
273 | |
274 | error = 0; | |
275 | out: | |
276 | kfree(buf1); | |
277 | ||
278 | return error; | |
279 | } | |
280 | ||
f9ecc921 N |
281 | |
282 | static struct ent * | |
283 | idtoname_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 | ||
294 | static struct ent * | |
295 | idtoname_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 | ||
1da177e4 LT |
306 | |
307 | /* | |
308 | * Name -> ID cache | |
309 | */ | |
310 | ||
311 | static struct cache_head *nametoid_table[ENT_HASHMAX]; | |
312 | ||
313 | static inline int | |
314 | nametoid_hash(struct ent *ent) | |
315 | { | |
316 | return hash_str(ent->name, ENT_HASHBITS); | |
317 | } | |
318 | ||
fd39ca9a | 319 | static void |
1da177e4 LT |
320 | nametoid_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 | ||
bc74b4f5 TM |
332 | static int |
333 | nametoid_upcall(struct cache_detail *cd, struct cache_head *ch) | |
334 | { | |
335 | return sunrpc_cache_pipe_upcall(cd, ch, nametoid_request); | |
336 | } | |
337 | ||
f9ecc921 N |
338 | static int |
339 | nametoid_match(struct cache_head *ca, struct cache_head *cb) | |
1da177e4 | 340 | { |
f9ecc921 N |
341 | struct ent *a = container_of(ca, struct ent, h); |
342 | struct ent *b = container_of(cb, struct ent, h); | |
343 | ||
1da177e4 LT |
344 | return (a->type == b->type && strcmp(a->name, b->name) == 0 && |
345 | strcmp(a->authname, b->authname) == 0); | |
346 | } | |
347 | ||
348 | static int | |
349 | nametoid_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)) | |
0a725fc4 | 362 | seq_printf(m, " %u", ent->id); |
1da177e4 LT |
363 | seq_printf(m, "\n"); |
364 | return 0; | |
365 | } | |
366 | ||
f9ecc921 N |
367 | static struct ent *nametoid_lookup(struct ent *); |
368 | static struct ent *nametoid_update(struct ent *, struct ent *); | |
fd39ca9a | 369 | static int nametoid_parse(struct cache_detail *, char *, int); |
1da177e4 | 370 | |
fd39ca9a | 371 | static struct cache_detail nametoid_cache = { |
f35279d3 | 372 | .owner = THIS_MODULE, |
1da177e4 LT |
373 | .hash_size = ENT_HASHMAX, |
374 | .hash_table = nametoid_table, | |
375 | .name = "nfs4.nametoid", | |
376 | .cache_put = ent_put, | |
bc74b4f5 | 377 | .cache_upcall = nametoid_upcall, |
1da177e4 LT |
378 | .cache_parse = nametoid_parse, |
379 | .cache_show = nametoid_show, | |
380 | .warn_no_listener = warn_no_idmapd, | |
f9ecc921 N |
381 | .match = nametoid_match, |
382 | .init = ent_init, | |
383 | .update = ent_init, | |
384 | .alloc = ent_alloc, | |
1da177e4 LT |
385 | }; |
386 | ||
fd39ca9a | 387 | static int |
1da177e4 LT |
388 | nametoid_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; | |
f9ecc921 N |
434 | res = nametoid_lookup(&ent); |
435 | if (res == NULL) | |
436 | goto out; | |
437 | res = nametoid_update(&ent, res); | |
438 | if (res == NULL) | |
1da177e4 LT |
439 | goto out; |
440 | ||
baab935f | 441 | cache_put(&res->h, &nametoid_cache); |
1da177e4 LT |
442 | error = 0; |
443 | out: | |
444 | kfree(buf1); | |
445 | ||
446 | return (error); | |
447 | } | |
448 | ||
f9ecc921 N |
449 | |
450 | static struct ent * | |
451 | nametoid_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 | ||
462 | static struct ent * | |
463 | nametoid_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 | } | |
1da177e4 LT |
473 | |
474 | /* | |
475 | * Exported API | |
476 | */ | |
477 | ||
dbf847ec | 478 | int |
1da177e4 LT |
479 | nfsd_idmap_init(void) |
480 | { | |
dbf847ec BF |
481 | 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; | |
1da177e4 LT |
490 | } |
491 | ||
492 | void | |
493 | nfsd_idmap_shutdown(void) | |
494 | { | |
df95a9d4 BF |
495 | cache_unregister(&idtoname_cache); |
496 | cache_unregister(&nametoid_cache); | |
1da177e4 LT |
497 | } |
498 | ||
499 | /* | |
500 | * Deferred request handling | |
501 | */ | |
502 | ||
503 | struct 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 | ||
510 | static inline void | |
511 | put_mdr(struct idmap_defer_req *mdr) | |
512 | { | |
513 | if (atomic_dec_and_test(&mdr->count)) | |
514 | kfree(mdr); | |
515 | } | |
516 | ||
517 | static inline void | |
518 | get_mdr(struct idmap_defer_req *mdr) | |
519 | { | |
520 | atomic_inc(&mdr->count); | |
521 | } | |
522 | ||
523 | static void | |
524 | idmap_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 | ||
533 | static struct cache_deferred_req * | |
534 | idmap_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 | ||
544 | static inline int | |
f9ecc921 | 545 | do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key, |
1da177e4 LT |
546 | struct cache_detail *detail, struct ent **item, |
547 | struct idmap_defer_req *mdr) | |
548 | { | |
f9ecc921 | 549 | *item = lookup_fn(key); |
1da177e4 LT |
550 | if (!*item) |
551 | return -ENOMEM; | |
552 | return cache_check(detail, &(*item)->h, &mdr->req); | |
553 | } | |
554 | ||
555 | static inline int | |
f9ecc921 | 556 | do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *), |
1da177e4 LT |
557 | struct ent *key, struct cache_detail *detail, |
558 | struct ent **item) | |
559 | { | |
560 | int ret = -ENOMEM; | |
561 | ||
f9ecc921 | 562 | *item = lookup_fn(key); |
1da177e4 LT |
563 | 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; | |
574 | out_put: | |
baab935f | 575 | cache_put(&(*item)->h, detail); |
1da177e4 LT |
576 | out_err: |
577 | *item = NULL; | |
578 | return ret; | |
579 | } | |
580 | ||
581 | static int | |
582 | idmap_lookup(struct svc_rqst *rqstp, | |
f9ecc921 | 583 | struct ent *(*lookup_fn)(struct ent *), struct ent *key, |
1da177e4 LT |
584 | struct cache_detail *detail, struct ent **item) |
585 | { | |
586 | struct idmap_defer_req *mdr; | |
587 | int ret; | |
588 | ||
f8314dc6 | 589 | mdr = kzalloc(sizeof(*mdr), GFP_KERNEL); |
1da177e4 LT |
590 | if (!mdr) |
591 | return -ENOMEM; | |
1da177e4 LT |
592 | 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 | ||
3ab4d8b1 BF |
605 | static char * |
606 | rqst_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 | ||
1da177e4 LT |
614 | static int |
615 | idmap_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'; | |
3ab4d8b1 | 627 | strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); |
1da177e4 LT |
628 | 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; | |
baab935f | 634 | cache_put(&item->h, &nametoid_cache); |
1da177e4 LT |
635 | return 0; |
636 | } | |
637 | ||
638 | static int | |
639 | idmap_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 | ||
3ab4d8b1 | 647 | strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname)); |
1da177e4 LT |
648 | 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); | |
baab935f | 656 | cache_put(&item->h, &idtoname_cache); |
1da177e4 LT |
657 | return ret; |
658 | } | |
659 | ||
660 | int | |
661 | nfsd_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 | ||
667 | int | |
668 | nfsd_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 | ||
674 | int | |
675 | nfsd_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 | ||
680 | int | |
681 | nfsd_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 | } |