xref: /linux/fs/nfsd/nfs4idmap.c (revision 8ca3b74c0c447af17d0e27e76658a3d4cae3c5ba)
1 /*
2  *  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 
35 #include <linux/module.h>
36 #include <linux/seq_file.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/sunrpc/svc_xprt.h>
40 #include <net/net_namespace.h>
41 #include "auth.h"
42 #include "idmap.h"
43 #include "nfsd.h"
44 #include "nfserr.h"
45 #include "netns.h"
46 #include "vfs.h"
47 
48 /*
49  * Turn off idmapping when using AUTH_SYS.
50  */
51 static bool nfs4_disable_idmapping = true;
52 module_param(nfs4_disable_idmapping, bool, 0644);
53 MODULE_PARM_DESC(nfs4_disable_idmapping,
54 		"Turn off server's NFSv4 idmapping when using 'sec=sys'");
55 
56 /*
57  * Cache entry
58  */
59 
60 /*
61  * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
62  * that.
63  */
64 
65 struct ent {
66 	struct cache_head h;
67 	int               type;		       /* User / Group */
68 	u32               id;
69 	char              name[IDMAP_NAMESZ];
70 	char              authname[IDMAP_NAMESZ];
71 	struct rcu_head	  rcu_head;
72 };
73 
74 /* Common entry handling */
75 
76 #define ENT_HASHBITS          8
77 #define ENT_HASHMAX           (1 << ENT_HASHBITS)
78 
79 static void
80 ent_init(struct cache_head *cnew, struct cache_head *citm)
81 {
82 	struct ent *new = container_of(cnew, struct ent, h);
83 	struct ent *itm = container_of(citm, struct ent, h);
84 
85 	new->id = itm->id;
86 	new->type = itm->type;
87 
88 	strscpy(new->name, itm->name, sizeof(new->name));
89 	strscpy(new->authname, itm->authname, sizeof(new->authname));
90 }
91 
92 static void
93 ent_put(struct kref *ref)
94 {
95 	struct ent *map = container_of(ref, struct ent, h.ref);
96 	kfree_rcu(map, rcu_head);
97 }
98 
99 static struct cache_head *
100 ent_alloc(void)
101 {
102 	struct ent *e = kmalloc_obj(*e);
103 	if (e)
104 		return &e->h;
105 	else
106 		return NULL;
107 }
108 
109 /*
110  * ID -> Name cache
111  */
112 
113 static uint32_t
114 idtoname_hash(struct ent *ent)
115 {
116 	uint32_t hash;
117 
118 	hash = hash_str(ent->authname, ENT_HASHBITS);
119 	hash = hash_long(hash ^ ent->id, ENT_HASHBITS);
120 
121 	/* Flip LSB for user/group */
122 	if (ent->type == IDMAP_TYPE_GROUP)
123 		hash ^= 1;
124 
125 	return hash;
126 }
127 
128 static int
129 idtoname_upcall(struct cache_detail *cd, struct cache_head *h)
130 {
131 	return sunrpc_cache_upcall_warn(cd, h);
132 }
133 
134 static void
135 idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
136     int *blen)
137 {
138  	struct ent *ent = container_of(ch, struct ent, h);
139 	char idstr[11];
140 
141 	qword_add(bpp, blen, ent->authname);
142 	snprintf(idstr, sizeof(idstr), "%u", ent->id);
143 	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
144 	qword_add(bpp, blen, idstr);
145 
146 	(*bpp)[-1] = '\n';
147 }
148 
149 static int
150 idtoname_match(struct cache_head *ca, struct cache_head *cb)
151 {
152 	struct ent *a = container_of(ca, struct ent, h);
153 	struct ent *b = container_of(cb, struct ent, h);
154 
155 	return (a->id == b->id && a->type == b->type &&
156 	    strcmp(a->authname, b->authname) == 0);
157 }
158 
159 static int
160 idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
161 {
162 	struct ent *ent;
163 
164 	if (h == NULL) {
165 		seq_puts(m, "#domain type id [name]\n");
166 		return 0;
167 	}
168 	ent = container_of(h, struct ent, h);
169 	seq_printf(m, "%s %s %u", ent->authname,
170 			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
171 			ent->id);
172 	if (test_bit(CACHE_VALID, &h->flags))
173 		seq_printf(m, " %s", ent->name);
174 	seq_putc(m, '\n');
175 	return 0;
176 }
177 
178 static void
179 warn_no_idmapd(struct cache_detail *detail, int has_died)
180 {
181 	printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
182 			has_died ? "died" : "not been started");
183 }
184 
185 
186 static int         idtoname_parse(struct cache_detail *, char *, int);
187 static struct ent *idtoname_lookup(struct cache_detail *, struct ent *);
188 static struct ent *idtoname_update(struct cache_detail *, struct ent *,
189 				   struct ent *);
190 
191 static const struct cache_detail idtoname_cache_template = {
192 	.owner		= THIS_MODULE,
193 	.hash_size	= ENT_HASHMAX,
194 	.name		= "nfs4.idtoname",
195 	.cache_put	= ent_put,
196 	.cache_upcall	= idtoname_upcall,
197 	.cache_request	= idtoname_request,
198 	.cache_parse	= idtoname_parse,
199 	.cache_show	= idtoname_show,
200 	.warn_no_listener = warn_no_idmapd,
201 	.match		= idtoname_match,
202 	.init		= ent_init,
203 	.update		= ent_init,
204 	.alloc		= ent_alloc,
205 };
206 
207 static int
208 idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
209 {
210 	struct ent ent, *res;
211 	char *buf1, *bp;
212 	int len;
213 	int error = -EINVAL;
214 
215 	if (buf[buflen - 1] != '\n')
216 		return (-EINVAL);
217 	buf[buflen - 1]= '\0';
218 
219 	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
220 	if (buf1 == NULL)
221 		return (-ENOMEM);
222 
223 	memset(&ent, 0, sizeof(ent));
224 
225 	/* Authentication name */
226 	len = qword_get(&buf, buf1, PAGE_SIZE);
227 	if (len <= 0 || len >= IDMAP_NAMESZ)
228 		goto out;
229 	memcpy(ent.authname, buf1, sizeof(ent.authname));
230 
231 	/* Type */
232 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
233 		goto out;
234 	ent.type = strcmp(buf1, "user") == 0 ?
235 		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
236 
237 	/* ID */
238 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
239 		goto out;
240 	ent.id = simple_strtoul(buf1, &bp, 10);
241 	if (bp == buf1)
242 		goto out;
243 
244 	/* expiry */
245 	error = get_expiry(&buf, &ent.h.expiry_time);
246 	if (error)
247 		goto out;
248 
249 	error = -ENOMEM;
250 	res = idtoname_lookup(cd, &ent);
251 	if (!res)
252 		goto out;
253 
254 	/* Name */
255 	error = -EINVAL;
256 	len = qword_get(&buf, buf1, PAGE_SIZE);
257 	if (len < 0 || len >= IDMAP_NAMESZ) {
258 		cache_put(&res->h, cd);
259 		goto out;
260 	}
261 	if (len == 0)
262 		set_bit(CACHE_NEGATIVE, &ent.h.flags);
263 	else
264 		memcpy(ent.name, buf1, sizeof(ent.name));
265 	error = -ENOMEM;
266 	res = idtoname_update(cd, &ent, res);
267 	if (res == NULL)
268 		goto out;
269 
270 	cache_put(&res->h, cd);
271 	error = 0;
272 out:
273 	kfree(buf1);
274 	return error;
275 }
276 
277 static struct ent *
278 idtoname_lookup(struct cache_detail *cd, struct ent *item)
279 {
280 	struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
281 							idtoname_hash(item));
282 	if (ch)
283 		return container_of(ch, struct ent, h);
284 	else
285 		return NULL;
286 }
287 
288 static struct ent *
289 idtoname_update(struct cache_detail *cd, struct ent *new, struct ent *old)
290 {
291 	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
292 						    idtoname_hash(new));
293 	if (ch)
294 		return container_of(ch, struct ent, h);
295 	else
296 		return NULL;
297 }
298 
299 
300 /*
301  * Name -> ID cache
302  */
303 
304 static inline int
305 nametoid_hash(struct ent *ent)
306 {
307 	return hash_str(ent->name, ENT_HASHBITS);
308 }
309 
310 static int
311 nametoid_upcall(struct cache_detail *cd, struct cache_head *h)
312 {
313 	return sunrpc_cache_upcall_warn(cd, h);
314 }
315 
316 static void
317 nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
318     int *blen)
319 {
320  	struct ent *ent = container_of(ch, struct ent, h);
321 
322 	qword_add(bpp, blen, ent->authname);
323 	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
324 	qword_add(bpp, blen, ent->name);
325 
326 	(*bpp)[-1] = '\n';
327 }
328 
329 static int
330 nametoid_match(struct cache_head *ca, struct cache_head *cb)
331 {
332 	struct ent *a = container_of(ca, struct ent, h);
333 	struct ent *b = container_of(cb, struct ent, h);
334 
335 	return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
336 	    strcmp(a->authname, b->authname) == 0);
337 }
338 
339 static int
340 nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
341 {
342 	struct ent *ent;
343 
344 	if (h == NULL) {
345 		seq_puts(m, "#domain type name [id]\n");
346 		return 0;
347 	}
348 	ent = container_of(h, struct ent, h);
349 	seq_printf(m, "%s %s %s", ent->authname,
350 			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
351 			ent->name);
352 	if (test_bit(CACHE_VALID, &h->flags))
353 		seq_printf(m, " %u", ent->id);
354 	seq_putc(m, '\n');
355 	return 0;
356 }
357 
358 static struct ent *nametoid_lookup(struct cache_detail *, struct ent *);
359 static struct ent *nametoid_update(struct cache_detail *, struct ent *,
360 				   struct ent *);
361 static int         nametoid_parse(struct cache_detail *, char *, int);
362 
363 static const struct cache_detail nametoid_cache_template = {
364 	.owner		= THIS_MODULE,
365 	.hash_size	= ENT_HASHMAX,
366 	.name		= "nfs4.nametoid",
367 	.cache_put	= ent_put,
368 	.cache_upcall	= nametoid_upcall,
369 	.cache_request	= nametoid_request,
370 	.cache_parse	= nametoid_parse,
371 	.cache_show	= nametoid_show,
372 	.warn_no_listener = warn_no_idmapd,
373 	.match		= nametoid_match,
374 	.init		= ent_init,
375 	.update		= ent_init,
376 	.alloc		= ent_alloc,
377 };
378 
379 static int
380 nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
381 {
382 	struct ent ent, *res;
383 	char *buf1;
384 	int len, error = -EINVAL;
385 
386 	if (buf[buflen - 1] != '\n')
387 		return (-EINVAL);
388 	buf[buflen - 1]= '\0';
389 
390 	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
391 	if (buf1 == NULL)
392 		return (-ENOMEM);
393 
394 	memset(&ent, 0, sizeof(ent));
395 
396 	/* Authentication name */
397 	len = qword_get(&buf, buf1, PAGE_SIZE);
398 	if (len <= 0 || len >= IDMAP_NAMESZ)
399 		goto out;
400 	memcpy(ent.authname, buf1, sizeof(ent.authname));
401 
402 	/* Type */
403 	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
404 		goto out;
405 	ent.type = strcmp(buf1, "user") == 0 ?
406 		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
407 
408 	/* Name */
409 	len = qword_get(&buf, buf1, PAGE_SIZE);
410 	if (len <= 0 || len >= IDMAP_NAMESZ)
411 		goto out;
412 	memcpy(ent.name, buf1, sizeof(ent.name));
413 
414 	/* expiry */
415 	error = get_expiry(&buf, &ent.h.expiry_time);
416 	if (error)
417 		goto out;
418 
419 	/* ID */
420 	error = get_int(&buf, &ent.id);
421 	if (error == -EINVAL)
422 		goto out;
423 	if (error == -ENOENT)
424 		set_bit(CACHE_NEGATIVE, &ent.h.flags);
425 
426 	error = -ENOMEM;
427 	res = nametoid_lookup(cd, &ent);
428 	if (res == NULL)
429 		goto out;
430 	res = nametoid_update(cd, &ent, res);
431 	if (res == NULL)
432 		goto out;
433 
434 	cache_put(&res->h, cd);
435 	error = 0;
436 out:
437 	kfree(buf1);
438 	return (error);
439 }
440 
441 
442 static struct ent *
443 nametoid_lookup(struct cache_detail *cd, struct ent *item)
444 {
445 	struct cache_head *ch = sunrpc_cache_lookup_rcu(cd, &item->h,
446 							nametoid_hash(item));
447 	if (ch)
448 		return container_of(ch, struct ent, h);
449 	else
450 		return NULL;
451 }
452 
453 static struct ent *
454 nametoid_update(struct cache_detail *cd, struct ent *new, struct ent *old)
455 {
456 	struct cache_head *ch = sunrpc_cache_update(cd, &new->h, &old->h,
457 						    nametoid_hash(new));
458 	if (ch)
459 		return container_of(ch, struct ent, h);
460 	else
461 		return NULL;
462 }
463 
464 /*
465  * Exported API
466  */
467 
468 int
469 nfsd_idmap_init(struct net *net)
470 {
471 	int rv;
472 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
473 
474 	nn->idtoname_cache = cache_create_net(&idtoname_cache_template, net);
475 	if (IS_ERR(nn->idtoname_cache))
476 		return PTR_ERR(nn->idtoname_cache);
477 	rv = cache_register_net(nn->idtoname_cache, net);
478 	if (rv)
479 		goto destroy_idtoname_cache;
480 	nn->nametoid_cache = cache_create_net(&nametoid_cache_template, net);
481 	if (IS_ERR(nn->nametoid_cache)) {
482 		rv = PTR_ERR(nn->nametoid_cache);
483 		goto unregister_idtoname_cache;
484 	}
485 	rv = cache_register_net(nn->nametoid_cache, net);
486 	if (rv)
487 		goto destroy_nametoid_cache;
488 	return 0;
489 
490 destroy_nametoid_cache:
491 	cache_destroy_net(nn->nametoid_cache, net);
492 unregister_idtoname_cache:
493 	cache_unregister_net(nn->idtoname_cache, net);
494 destroy_idtoname_cache:
495 	cache_destroy_net(nn->idtoname_cache, net);
496 	return rv;
497 }
498 
499 void
500 nfsd_idmap_shutdown(struct net *net)
501 {
502 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
503 
504 	cache_unregister_net(nn->idtoname_cache, net);
505 	cache_unregister_net(nn->nametoid_cache, net);
506 	cache_destroy_net(nn->idtoname_cache, net);
507 	cache_destroy_net(nn->nametoid_cache, net);
508 }
509 
510 static int
511 idmap_lookup(struct svc_rqst *rqstp,
512 		struct ent *(*lookup_fn)(struct cache_detail *, struct ent *),
513 		struct ent *key, struct cache_detail *detail, struct ent **item)
514 {
515 	int ret;
516 
517 	*item = lookup_fn(detail, key);
518 	if (!*item)
519 		return -ENOMEM;
520  retry:
521 	ret = cache_check(detail, &(*item)->h, &rqstp->rq_chandle);
522 
523 	if (ret == -ETIMEDOUT) {
524 		struct ent *prev_item = *item;
525 		*item = lookup_fn(detail, key);
526 		if (*item != prev_item)
527 			goto retry;
528 		cache_put(&(*item)->h, detail);
529 	}
530 	return ret;
531 }
532 
533 static char *
534 rqst_authname(struct svc_rqst *rqstp)
535 {
536 	struct auth_domain *clp;
537 
538 	clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
539 	return clp->name;
540 }
541 
542 static __be32
543 idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
544 		u32 *id)
545 {
546 	struct ent *item, key = {
547 		.type = type,
548 	};
549 	int ret;
550 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
551 
552 	if (namelen + 1 > sizeof(key.name))
553 		return nfserr_badowner;
554 	memcpy(key.name, name, namelen);
555 	key.name[namelen] = '\0';
556 	strscpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
557 	ret = idmap_lookup(rqstp, nametoid_lookup, &key, nn->nametoid_cache, &item);
558 	if (ret == -ENOENT)
559 		return nfserr_badowner;
560 	if (ret)
561 		return nfserrno(ret);
562 	*id = item->id;
563 	cache_put(&item->h, nn->nametoid_cache);
564 	return 0;
565 }
566 
567 static __be32 encode_ascii_id(struct xdr_stream *xdr, u32 id)
568 {
569 	char buf[11];
570 	int len;
571 	__be32 *p;
572 
573 	len = sprintf(buf, "%u", id);
574 	p = xdr_reserve_space(xdr, len + 4);
575 	if (!p)
576 		return nfserr_resource;
577 	p = xdr_encode_opaque(p, buf, len);
578 	return 0;
579 }
580 
581 static __be32 idmap_id_to_name(struct xdr_stream *xdr,
582 			       struct svc_rqst *rqstp, int type, u32 id)
583 {
584 	struct ent *item, key = {
585 		.id = id,
586 		.type = type,
587 	};
588 	__be32 status = nfs_ok;
589 	__be32 *p;
590 	int ret;
591 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
592 
593 	strscpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
594 	ret = idmap_lookup(rqstp, idtoname_lookup, &key, nn->idtoname_cache, &item);
595 	if (ret == -ENOENT)
596 		return encode_ascii_id(xdr, id);
597 	if (ret)
598 		return nfserrno(ret);
599 	ret = strlen(item->name);
600 	WARN_ON_ONCE(ret > IDMAP_NAMESZ);
601 
602 	p = xdr_reserve_space(xdr, ret + 4);
603 	if (unlikely(!p)) {
604 		status = nfserr_resource;
605 		goto out_put;
606 	}
607 	xdr_encode_opaque(p, item->name, ret);
608 out_put:
609 	cache_put(&item->h, nn->idtoname_cache);
610 	return status;
611 }
612 
613 static bool
614 numeric_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
615 {
616 	int ret;
617 	char buf[11];
618 
619 	if (namelen + 1 > sizeof(buf))
620 		/* too long to represent a 32-bit id: */
621 		return false;
622 	/* Just to make sure it's null-terminated: */
623 	memcpy(buf, name, namelen);
624 	buf[namelen] = '\0';
625 	ret = kstrtouint(buf, 10, id);
626 	return ret == 0;
627 }
628 
629 static __be32
630 do_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen, u32 *id)
631 {
632 	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
633 		if (numeric_name_to_id(rqstp, type, name, namelen, id))
634 			return 0;
635 		/*
636 		 * otherwise, fall through and try idmapping, for
637 		 * backwards compatibility with clients sending names:
638 		 */
639 	return idmap_name_to_id(rqstp, type, name, namelen, id);
640 }
641 
642 static __be32 encode_name_from_id(struct xdr_stream *xdr,
643 				  struct svc_rqst *rqstp, int type, u32 id)
644 {
645 	if (nfs4_disable_idmapping && rqstp->rq_cred.cr_flavor < RPC_AUTH_GSS)
646 		return encode_ascii_id(xdr, id);
647 	return idmap_id_to_name(xdr, rqstp, type, id);
648 }
649 
650 /**
651  * nfsd_map_name_to_uid - Map user@domain to local UID
652  * @rqstp: RPC execution context
653  * @name: user@domain name to be mapped
654  * @namelen: length of name, in bytes
655  * @uid: OUT: mapped local UID value
656  *
657  * Returns nfs_ok on success or an NFSv4 status code on failure.
658  */
659 __be32 nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name,
660 			    size_t namelen, kuid_t *uid)
661 {
662 	__be32 status;
663 	u32 id = -1;
664 
665 	/*
666 	 * The idmap lookup below triggers an upcall that invokes
667 	 * cache_check(). RQ_USEDEFERRAL must be clear to prevent
668 	 * cache_check() from setting RQ_DROPME via svc_defer().
669 	 * NFSv4 servers are not permitted to drop requests. Also
670 	 * RQ_DROPME will force NFSv4.1 session slot processing to
671 	 * be skipped.
672 	 */
673 	WARN_ON_ONCE(test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags));
674 
675 	if (name == NULL || namelen == 0)
676 		return nfserr_inval;
677 
678 	status = do_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, &id);
679 	if (status)
680 		return status;
681 	*uid = make_kuid(nfsd_user_namespace(rqstp), id);
682 	if (!uid_valid(*uid))
683 		status = nfserr_badowner;
684 	return status;
685 }
686 
687 /**
688  * nfsd_map_name_to_gid - Map user@domain to local GID
689  * @rqstp: RPC execution context
690  * @name: user@domain name to be mapped
691  * @namelen: length of name, in bytes
692  * @gid: OUT: mapped local GID value
693  *
694  * Returns nfs_ok on success or an NFSv4 status code on failure.
695  */
696 __be32 nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name,
697 			    size_t namelen, kgid_t *gid)
698 {
699 	__be32 status;
700 	u32 id = -1;
701 
702 	/*
703 	 * The idmap lookup below triggers an upcall that invokes
704 	 * cache_check(). RQ_USEDEFERRAL must be clear to prevent
705 	 * cache_check() from setting RQ_DROPME via svc_defer().
706 	 * NFSv4 servers are not permitted to drop requests. Also
707 	 * RQ_DROPME will force NFSv4.1 session slot processing to
708 	 * be skipped.
709 	 */
710 	WARN_ON_ONCE(test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags));
711 
712 	if (name == NULL || namelen == 0)
713 		return nfserr_inval;
714 
715 	status = do_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, &id);
716 	if (status)
717 		return status;
718 	*gid = make_kgid(nfsd_user_namespace(rqstp), id);
719 	if (!gid_valid(*gid))
720 		status = nfserr_badowner;
721 	return status;
722 }
723 
724 __be32 nfsd4_encode_user(struct xdr_stream *xdr, struct svc_rqst *rqstp,
725 			 kuid_t uid)
726 {
727 	u32 id = from_kuid_munged(nfsd_user_namespace(rqstp), uid);
728 	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_USER, id);
729 }
730 
731 __be32 nfsd4_encode_group(struct xdr_stream *xdr, struct svc_rqst *rqstp,
732 			  kgid_t gid)
733 {
734 	u32 id = from_kgid_munged(nfsd_user_namespace(rqstp), gid);
735 	return encode_name_from_id(xdr, rqstp, IDMAP_TYPE_GROUP, id);
736 }
737