4 * Mapping of UID/GIDs to name and vice versa.
6 * Copyright (c) 2002, 2003 The Regents of the University of
7 * Michigan. All rights reserved.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/init.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>
64 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
68 #define IDMAP_TYPE_USER 0
69 #define IDMAP_TYPE_GROUP 1
73 int type; /* User / Group */
75 char name[IDMAP_NAMESZ];
76 char authname[IDMAP_NAMESZ];
79 /* Common entry handling */
81 #define ENT_HASHBITS 8
82 #define ENT_HASHMAX (1 << ENT_HASHBITS)
83 #define ENT_HASHMASK (ENT_HASHMAX - 1)
86 ent_init(struct ent *new, struct ent *itm)
89 new->type = itm->type;
91 strlcpy(new->name, itm->name, sizeof(new->name));
92 strlcpy(new->authname, itm->authname, sizeof(new->name));
96 ent_update(struct ent *new, struct ent *itm)
102 ent_put(struct cache_head *ch, struct cache_detail *cd)
104 if (cache_put(ch, cd)) {
105 struct ent *map = container_of(ch, struct ent, h);
114 static struct cache_head *idtoname_table[ENT_HASHMAX];
117 idtoname_hash(struct ent *ent)
121 hash = hash_str(ent->authname, ENT_HASHBITS);
122 hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
124 /* Flip LSB for user/group */
125 if (ent->type == IDMAP_TYPE_GROUP)
132 idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
135 struct ent *ent = container_of(ch, struct ent, h);
138 qword_add(bpp, blen, ent->authname);
139 snprintf(idstr, sizeof(idstr), "%d", ent->id);
140 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
141 qword_add(bpp, blen, idstr);
147 idtoname_match(struct ent *a, struct ent *b)
149 return (a->id == b->id && a->type == b->type &&
150 strcmp(a->authname, b->authname) == 0);
154 idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
159 seq_puts(m, "#domain type id [name]\n");
162 ent = container_of(h, struct ent, h);
163 seq_printf(m, "%s %s %d", ent->authname,
164 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
166 if (test_bit(CACHE_VALID, &h->flags))
167 seq_printf(m, " %s", ent->name);
173 warn_no_idmapd(struct cache_detail *detail)
175 printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
176 detail->last_close? "died" : "not been started");
180 static int idtoname_parse(struct cache_detail *, char *, int);
181 static struct ent *idtoname_lookup(struct ent *, int);
183 static struct cache_detail idtoname_cache = {
184 .owner = THIS_MODULE,
185 .hash_size = ENT_HASHMAX,
186 .hash_table = idtoname_table,
187 .name = "nfs4.idtoname",
188 .cache_put = ent_put,
189 .cache_request = idtoname_request,
190 .cache_parse = idtoname_parse,
191 .cache_show = idtoname_show,
192 .warn_no_listener = warn_no_idmapd,
196 idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
198 struct ent ent, *res;
202 if (buf[buflen - 1] != '\n')
204 buf[buflen - 1]= '\0';
206 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
210 memset(&ent, 0, sizeof(ent));
212 /* Authentication name */
213 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
215 memcpy(ent.authname, buf1, sizeof(ent.authname));
218 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
220 ent.type = strcmp(buf1, "user") == 0 ?
221 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
224 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
226 ent.id = simple_strtoul(buf1, &bp, 10);
231 ent.h.expiry_time = get_expiry(&buf);
232 if (ent.h.expiry_time == 0)
236 error = qword_get(&buf, buf1, PAGE_SIZE);
237 if (error == -EINVAL)
239 if (error == -ENOENT)
240 set_bit(CACHE_NEGATIVE, &ent.h.flags);
242 if (error >= IDMAP_NAMESZ) {
246 memcpy(ent.name, buf1, sizeof(ent.name));
249 if ((res = idtoname_lookup(&ent, 1)) == NULL)
252 ent_put(&res->h, &idtoname_cache);
261 static DefineSimpleCacheLookup(ent, idtoname);
267 static struct cache_head *nametoid_table[ENT_HASHMAX];
270 nametoid_hash(struct ent *ent)
272 return hash_str(ent->name, ENT_HASHBITS);
276 nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
279 struct ent *ent = container_of(ch, struct ent, h);
281 qword_add(bpp, blen, ent->authname);
282 qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
283 qword_add(bpp, blen, ent->name);
289 nametoid_match(struct ent *a, struct ent *b)
291 return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
292 strcmp(a->authname, b->authname) == 0);
296 nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
301 seq_puts(m, "#domain type name [id]\n");
304 ent = container_of(h, struct ent, h);
305 seq_printf(m, "%s %s %s", ent->authname,
306 ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
308 if (test_bit(CACHE_VALID, &h->flags))
309 seq_printf(m, " %d", ent->id);
314 static struct ent *nametoid_lookup(struct ent *, int);
315 static int nametoid_parse(struct cache_detail *, char *, int);
317 static struct cache_detail nametoid_cache = {
318 .owner = THIS_MODULE,
319 .hash_size = ENT_HASHMAX,
320 .hash_table = nametoid_table,
321 .name = "nfs4.nametoid",
322 .cache_put = ent_put,
323 .cache_request = nametoid_request,
324 .cache_parse = nametoid_parse,
325 .cache_show = nametoid_show,
326 .warn_no_listener = warn_no_idmapd,
330 nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
332 struct ent ent, *res;
336 if (buf[buflen - 1] != '\n')
338 buf[buflen - 1]= '\0';
340 buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
344 memset(&ent, 0, sizeof(ent));
346 /* Authentication name */
347 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
349 memcpy(ent.authname, buf1, sizeof(ent.authname));
352 if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
354 ent.type = strcmp(buf1, "user") == 0 ?
355 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
358 error = qword_get(&buf, buf1, PAGE_SIZE);
359 if (error <= 0 || error >= IDMAP_NAMESZ)
361 memcpy(ent.name, buf1, sizeof(ent.name));
364 ent.h.expiry_time = get_expiry(&buf);
365 if (ent.h.expiry_time == 0)
369 error = get_int(&buf, &ent.id);
370 if (error == -EINVAL)
372 if (error == -ENOENT)
373 set_bit(CACHE_NEGATIVE, &ent.h.flags);
376 if ((res = nametoid_lookup(&ent, 1)) == NULL)
379 ent_put(&res->h, &nametoid_cache);
387 static DefineSimpleCacheLookup(ent, nametoid);
394 nfsd_idmap_init(void)
396 cache_register(&idtoname_cache);
397 cache_register(&nametoid_cache);
401 nfsd_idmap_shutdown(void)
403 if (cache_unregister(&idtoname_cache))
404 printk(KERN_ERR "nfsd: failed to unregister idtoname cache\n");
405 if (cache_unregister(&nametoid_cache))
406 printk(KERN_ERR "nfsd: failed to unregister nametoid cache\n");
410 * Deferred request handling
413 struct idmap_defer_req {
414 struct cache_req req;
415 struct cache_deferred_req deferred_req;
416 wait_queue_head_t waitq;
421 put_mdr(struct idmap_defer_req *mdr)
423 if (atomic_dec_and_test(&mdr->count))
428 get_mdr(struct idmap_defer_req *mdr)
430 atomic_inc(&mdr->count);
434 idmap_revisit(struct cache_deferred_req *dreq, int toomany)
436 struct idmap_defer_req *mdr =
437 container_of(dreq, struct idmap_defer_req, deferred_req);
439 wake_up(&mdr->waitq);
443 static struct cache_deferred_req *
444 idmap_defer(struct cache_req *req)
446 struct idmap_defer_req *mdr =
447 container_of(req, struct idmap_defer_req, req);
449 mdr->deferred_req.revisit = idmap_revisit;
451 return (&mdr->deferred_req);
455 do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
456 struct cache_detail *detail, struct ent **item,
457 struct idmap_defer_req *mdr)
459 *item = lookup_fn(key, 0);
462 return cache_check(detail, &(*item)->h, &mdr->req);
466 do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *, int),
467 struct ent *key, struct cache_detail *detail,
472 *item = lookup_fn(key, 0);
476 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
477 || (*item)->h.expiry_time < get_seconds()
478 || detail->flush_time > (*item)->h.last_refresh)
481 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
485 ent_put(&(*item)->h, detail);
492 idmap_lookup(struct svc_rqst *rqstp,
493 struct ent *(*lookup_fn)(struct ent *, int), struct ent *key,
494 struct cache_detail *detail, struct ent **item)
496 struct idmap_defer_req *mdr;
499 mdr = kmalloc(sizeof(*mdr), GFP_KERNEL);
502 memset(mdr, 0, sizeof(*mdr));
503 atomic_set(&mdr->count, 1);
504 init_waitqueue_head(&mdr->waitq);
505 mdr->req.defer = idmap_defer;
506 ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
507 if (ret == -EAGAIN) {
508 wait_event_interruptible_timeout(mdr->waitq,
509 test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
510 ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
517 idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
520 struct ent *item, key = {
525 if (namelen + 1 > sizeof(key.name))
527 memcpy(key.name, name, namelen);
528 key.name[namelen] = '\0';
529 strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
530 ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
532 ret = -ESRCH; /* nfserr_badname */
536 ent_put(&item->h, &nametoid_cache);
541 idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
543 struct ent *item, key = {
549 strlcpy(key.authname, rqstp->rq_client->name, sizeof(key.authname));
550 ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
552 return sprintf(name, "%u", id);
555 ret = strlen(item->name);
556 BUG_ON(ret > IDMAP_NAMESZ);
557 memcpy(name, item->name, ret);
558 ent_put(&item->h, &idtoname_cache);
563 nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
566 return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
570 nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
573 return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
577 nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
579 return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
583 nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
585 return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);