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