Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.13.7.
  1/*
  2 * fs/nfs/idmap.c
  3 *
  4 *  UID and GID to name mapping for clients.
  5 *
  6 *  Copyright (c) 2002 The Regents of the University of Michigan.
  7 *  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#include <linux/types.h>
 37#include <linux/parser.h>
 38#include <linux/fs.h>
 39#include <linux/nfs_idmap.h>
 40#include <net/net_namespace.h>
 41#include <linux/sunrpc/rpc_pipe_fs.h>
 42#include <linux/nfs_fs.h>
 43#include <linux/nfs_fs_sb.h>
 44#include <linux/key.h>
 45#include <linux/keyctl.h>
 46#include <linux/key-type.h>
 47#include <keys/user-type.h>
 48#include <linux/module.h>
 49
 50#include "internal.h"
 51#include "netns.h"
 52
 53#define NFS_UINT_MAXLEN 11
 54
 55/* Default cache timeout is 10 minutes */
 56unsigned int nfs_idmap_cache_timeout = 600;
 57static const struct cred *id_resolver_cache;
 58static struct key_type key_type_id_resolver_legacy;
 59
 60struct idmap {
 61	struct rpc_pipe		*idmap_pipe;
 62	struct key_construction	*idmap_key_cons;
 63	struct mutex		idmap_mutex;
 64};
 65
 66struct idmap_legacy_upcalldata {
 67	struct rpc_pipe_msg pipe_msg;
 68	struct idmap_msg idmap_msg;
 69	struct idmap *idmap;
 70};
 71
 72/**
 73 * nfs_fattr_init_names - initialise the nfs_fattr owner_name/group_name fields
 74 * @fattr: fully initialised struct nfs_fattr
 75 * @owner_name: owner name string cache
 76 * @group_name: group name string cache
 77 */
 78void nfs_fattr_init_names(struct nfs_fattr *fattr,
 79		struct nfs4_string *owner_name,
 80		struct nfs4_string *group_name)
 81{
 82	fattr->owner_name = owner_name;
 83	fattr->group_name = group_name;
 84}
 85
 86static void nfs_fattr_free_owner_name(struct nfs_fattr *fattr)
 87{
 88	fattr->valid &= ~NFS_ATTR_FATTR_OWNER_NAME;
 89	kfree(fattr->owner_name->data);
 90}
 91
 92static void nfs_fattr_free_group_name(struct nfs_fattr *fattr)
 93{
 94	fattr->valid &= ~NFS_ATTR_FATTR_GROUP_NAME;
 95	kfree(fattr->group_name->data);
 96}
 97
 98static bool nfs_fattr_map_owner_name(struct nfs_server *server, struct nfs_fattr *fattr)
 99{
100	struct nfs4_string *owner = fattr->owner_name;
101	__u32 uid;
102
103	if (!(fattr->valid & NFS_ATTR_FATTR_OWNER_NAME))
104		return false;
105	if (nfs_map_name_to_uid(server, owner->data, owner->len, &uid) == 0) {
106		fattr->uid = uid;
107		fattr->valid |= NFS_ATTR_FATTR_OWNER;
108	}
109	return true;
110}
111
112static bool nfs_fattr_map_group_name(struct nfs_server *server, struct nfs_fattr *fattr)
113{
114	struct nfs4_string *group = fattr->group_name;
115	__u32 gid;
116
117	if (!(fattr->valid & NFS_ATTR_FATTR_GROUP_NAME))
118		return false;
119	if (nfs_map_group_to_gid(server, group->data, group->len, &gid) == 0) {
120		fattr->gid = gid;
121		fattr->valid |= NFS_ATTR_FATTR_GROUP;
122	}
123	return true;
124}
125
126/**
127 * nfs_fattr_free_names - free up the NFSv4 owner and group strings
128 * @fattr: a fully initialised nfs_fattr structure
129 */
130void nfs_fattr_free_names(struct nfs_fattr *fattr)
131{
132	if (fattr->valid & NFS_ATTR_FATTR_OWNER_NAME)
133		nfs_fattr_free_owner_name(fattr);
134	if (fattr->valid & NFS_ATTR_FATTR_GROUP_NAME)
135		nfs_fattr_free_group_name(fattr);
136}
137
138/**
139 * nfs_fattr_map_and_free_names - map owner/group strings into uid/gid and free
140 * @server: pointer to the filesystem nfs_server structure
141 * @fattr: a fully initialised nfs_fattr structure
142 *
143 * This helper maps the cached NFSv4 owner/group strings in fattr into
144 * their numeric uid/gid equivalents, and then frees the cached strings.
145 */
146void nfs_fattr_map_and_free_names(struct nfs_server *server, struct nfs_fattr *fattr)
147{
148	if (nfs_fattr_map_owner_name(server, fattr))
149		nfs_fattr_free_owner_name(fattr);
150	if (nfs_fattr_map_group_name(server, fattr))
151		nfs_fattr_free_group_name(fattr);
152}
153
154static int nfs_map_string_to_numeric(const char *name, size_t namelen, __u32 *res)
155{
156	unsigned long val;
157	char buf[16];
158
159	if (memchr(name, '@', namelen) != NULL || namelen >= sizeof(buf))
160		return 0;
161	memcpy(buf, name, namelen);
162	buf[namelen] = '\0';
163	if (strict_strtoul(buf, 0, &val) != 0)
164		return 0;
165	*res = val;
166	return 1;
167}
168
169static int nfs_map_numeric_to_string(__u32 id, char *buf, size_t buflen)
170{
171	return snprintf(buf, buflen, "%u", id);
172}
173
174static struct key_type key_type_id_resolver = {
175	.name		= "id_resolver",
176	.instantiate	= user_instantiate,
177	.match		= user_match,
178	.revoke		= user_revoke,
179	.destroy	= user_destroy,
180	.describe	= user_describe,
181	.read		= user_read,
182};
183
184static int nfs_idmap_init_keyring(void)
185{
186	struct cred *cred;
187	struct key *keyring;
188	int ret = 0;
189
190	printk(KERN_NOTICE "NFS: Registering the %s key type\n",
191		key_type_id_resolver.name);
192
193	cred = prepare_kernel_cred(NULL);
194	if (!cred)
195		return -ENOMEM;
196
197	keyring = key_alloc(&key_type_keyring, ".id_resolver", 0, 0, cred,
198			     (KEY_POS_ALL & ~KEY_POS_SETATTR) |
199			     KEY_USR_VIEW | KEY_USR_READ,
200			     KEY_ALLOC_NOT_IN_QUOTA);
201	if (IS_ERR(keyring)) {
202		ret = PTR_ERR(keyring);
203		goto failed_put_cred;
204	}
205
206	ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
207	if (ret < 0)
208		goto failed_put_key;
209
210	ret = register_key_type(&key_type_id_resolver);
211	if (ret < 0)
212		goto failed_put_key;
213
214	ret = register_key_type(&key_type_id_resolver_legacy);
215	if (ret < 0)
216		goto failed_reg_legacy;
217
218	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
219	cred->thread_keyring = keyring;
220	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
221	id_resolver_cache = cred;
222	return 0;
223
224failed_reg_legacy:
225	unregister_key_type(&key_type_id_resolver);
226failed_put_key:
227	key_put(keyring);
228failed_put_cred:
229	put_cred(cred);
230	return ret;
231}
232
233static void nfs_idmap_quit_keyring(void)
234{
235	key_revoke(id_resolver_cache->thread_keyring);
236	unregister_key_type(&key_type_id_resolver);
237	unregister_key_type(&key_type_id_resolver_legacy);
238	put_cred(id_resolver_cache);
239}
240
241/*
242 * Assemble the description to pass to request_key()
243 * This function will allocate a new string and update dest to point
244 * at it.  The caller is responsible for freeing dest.
245 *
246 * On error 0 is returned.  Otherwise, the length of dest is returned.
247 */
248static ssize_t nfs_idmap_get_desc(const char *name, size_t namelen,
249				const char *type, size_t typelen, char **desc)
250{
251	char *cp;
252	size_t desclen = typelen + namelen + 2;
253
254	*desc = kmalloc(desclen, GFP_KERNEL);
255	if (!*desc)
256		return -ENOMEM;
257
258	cp = *desc;
259	memcpy(cp, type, typelen);
260	cp += typelen;
261	*cp++ = ':';
262
263	memcpy(cp, name, namelen);
264	cp += namelen;
265	*cp = '\0';
266	return desclen;
267}
268
269static ssize_t nfs_idmap_request_key(struct key_type *key_type,
270				     const char *name, size_t namelen,
271				     const char *type, void *data,
272				     size_t data_size, struct idmap *idmap)
273{
274	const struct cred *saved_cred;
275	struct key *rkey;
276	char *desc;
277	struct user_key_payload *payload;
278	ssize_t ret;
279
280	ret = nfs_idmap_get_desc(name, namelen, type, strlen(type), &desc);
281	if (ret <= 0)
282		goto out;
283
284	saved_cred = override_creds(id_resolver_cache);
285	if (idmap)
286		rkey = request_key_with_auxdata(key_type, desc, "", 0, idmap);
287	else
288		rkey = request_key(&key_type_id_resolver, desc, "");
289	revert_creds(saved_cred);
290
291	kfree(desc);
292	if (IS_ERR(rkey)) {
293		ret = PTR_ERR(rkey);
294		goto out;
295	}
296
297	rcu_read_lock();
298	rkey->perm |= KEY_USR_VIEW;
299
300	ret = key_validate(rkey);
301	if (ret < 0)
302		goto out_up;
303
304	payload = rcu_dereference(rkey->payload.data);
305	if (IS_ERR_OR_NULL(payload)) {
306		ret = PTR_ERR(payload);
307		goto out_up;
308	}
309
310	ret = payload->datalen;
311	if (ret > 0 && ret <= data_size)
312		memcpy(data, payload->data, ret);
313	else
314		ret = -EINVAL;
315
316out_up:
317	rcu_read_unlock();
318	key_put(rkey);
319out:
320	return ret;
321}
322
323static ssize_t nfs_idmap_get_key(const char *name, size_t namelen,
324				 const char *type, void *data,
325				 size_t data_size, struct idmap *idmap)
326{
327	ssize_t ret = nfs_idmap_request_key(&key_type_id_resolver,
328					    name, namelen, type, data,
329					    data_size, NULL);
330	if (ret < 0) {
331		mutex_lock(&idmap->idmap_mutex);
332		ret = nfs_idmap_request_key(&key_type_id_resolver_legacy,
333					    name, namelen, type, data,
334					    data_size, idmap);
335		idmap->idmap_key_cons = NULL;
336		mutex_unlock(&idmap->idmap_mutex);
337	}
338	return ret;
339}
340
341/* ID -> Name */
342static ssize_t nfs_idmap_lookup_name(__u32 id, const char *type, char *buf,
343				     size_t buflen, struct idmap *idmap)
344{
345	char id_str[NFS_UINT_MAXLEN];
346	int id_len;
347	ssize_t ret;
348
349	id_len = snprintf(id_str, sizeof(id_str), "%u", id);
350	ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap);
351	if (ret < 0)
352		return -EINVAL;
353	return ret;
354}
355
356/* Name -> ID */
357static int nfs_idmap_lookup_id(const char *name, size_t namelen, const char *type,
358			       __u32 *id, struct idmap *idmap)
359{
360	char id_str[NFS_UINT_MAXLEN];
361	long id_long;
362	ssize_t data_size;
363	int ret = 0;
364
365	data_size = nfs_idmap_get_key(name, namelen, type, id_str, NFS_UINT_MAXLEN, idmap);
366	if (data_size <= 0) {
367		ret = -EINVAL;
368	} else {
369		ret = strict_strtol(id_str, 10, &id_long);
370		*id = (__u32)id_long;
371	}
372	return ret;
373}
374
375/* idmap classic begins here */
376module_param(nfs_idmap_cache_timeout, int, 0644);
377
378enum {
379	Opt_find_uid, Opt_find_gid, Opt_find_user, Opt_find_group, Opt_find_err
380};
381
382static const match_table_t nfs_idmap_tokens = {
383	{ Opt_find_uid, "uid:%s" },
384	{ Opt_find_gid, "gid:%s" },
385	{ Opt_find_user, "user:%s" },
386	{ Opt_find_group, "group:%s" },
387	{ Opt_find_err, NULL }
388};
389
390static int nfs_idmap_legacy_upcall(struct key_construction *, const char *, void *);
391static ssize_t idmap_pipe_downcall(struct file *, const char __user *,
392				   size_t);
393static void idmap_release_pipe(struct inode *);
394static void idmap_pipe_destroy_msg(struct rpc_pipe_msg *);
395
396static const struct rpc_pipe_ops idmap_upcall_ops = {
397	.upcall		= rpc_pipe_generic_upcall,
398	.downcall	= idmap_pipe_downcall,
399	.release_pipe	= idmap_release_pipe,
400	.destroy_msg	= idmap_pipe_destroy_msg,
401};
402
403static struct key_type key_type_id_resolver_legacy = {
404	.name		= "id_legacy",
405	.instantiate	= user_instantiate,
406	.match		= user_match,
407	.revoke		= user_revoke,
408	.destroy	= user_destroy,
409	.describe	= user_describe,
410	.read		= user_read,
411	.request_key	= nfs_idmap_legacy_upcall,
412};
413
414static void __nfs_idmap_unregister(struct rpc_pipe *pipe)
415{
416	if (pipe->dentry)
417		rpc_unlink(pipe->dentry);
418}
419
420static int __nfs_idmap_register(struct dentry *dir,
421				     struct idmap *idmap,
422				     struct rpc_pipe *pipe)
423{
424	struct dentry *dentry;
425
426	dentry = rpc_mkpipe_dentry(dir, "idmap", idmap, pipe);
427	if (IS_ERR(dentry))
428		return PTR_ERR(dentry);
429	pipe->dentry = dentry;
430	return 0;
431}
432
433static void nfs_idmap_unregister(struct nfs_client *clp,
434				      struct rpc_pipe *pipe)
435{
436	struct net *net = clp->cl_net;
437	struct super_block *pipefs_sb;
438
439	pipefs_sb = rpc_get_sb_net(net);
440	if (pipefs_sb) {
441		__nfs_idmap_unregister(pipe);
442		rpc_put_sb_net(net);
443	}
444}
445
446static int nfs_idmap_register(struct nfs_client *clp,
447				   struct idmap *idmap,
448				   struct rpc_pipe *pipe)
449{
450	struct net *net = clp->cl_net;
451	struct super_block *pipefs_sb;
452	int err = 0;
453
454	pipefs_sb = rpc_get_sb_net(net);
455	if (pipefs_sb) {
456		if (clp->cl_rpcclient->cl_dentry)
457			err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
458						   idmap, pipe);
459		rpc_put_sb_net(net);
460	}
461	return err;
462}
463
464int
465nfs_idmap_new(struct nfs_client *clp)
466{
467	struct idmap *idmap;
468	struct rpc_pipe *pipe;
469	int error;
470
471	BUG_ON(clp->cl_idmap != NULL);
472
473	idmap = kzalloc(sizeof(*idmap), GFP_KERNEL);
474	if (idmap == NULL)
475		return -ENOMEM;
476
477	pipe = rpc_mkpipe_data(&idmap_upcall_ops, 0);
478	if (IS_ERR(pipe)) {
479		error = PTR_ERR(pipe);
480		kfree(idmap);
481		return error;
482	}
483	error = nfs_idmap_register(clp, idmap, pipe);
484	if (error) {
485		rpc_destroy_pipe_data(pipe);
486		kfree(idmap);
487		return error;
488	}
489	idmap->idmap_pipe = pipe;
490	mutex_init(&idmap->idmap_mutex);
491
492	clp->cl_idmap = idmap;
493	return 0;
494}
495
496void
497nfs_idmap_delete(struct nfs_client *clp)
498{
499	struct idmap *idmap = clp->cl_idmap;
500
501	if (!idmap)
502		return;
503	nfs_idmap_unregister(clp, idmap->idmap_pipe);
504	rpc_destroy_pipe_data(idmap->idmap_pipe);
505	clp->cl_idmap = NULL;
506	kfree(idmap);
507}
508
509static int __rpc_pipefs_event(struct nfs_client *clp, unsigned long event,
510			      struct super_block *sb)
511{
512	int err = 0;
513
514	switch (event) {
515	case RPC_PIPEFS_MOUNT:
516		BUG_ON(clp->cl_rpcclient->cl_dentry == NULL);
517		err = __nfs_idmap_register(clp->cl_rpcclient->cl_dentry,
518						clp->cl_idmap,
519						clp->cl_idmap->idmap_pipe);
520		break;
521	case RPC_PIPEFS_UMOUNT:
522		if (clp->cl_idmap->idmap_pipe) {
523			struct dentry *parent;
524
525			parent = clp->cl_idmap->idmap_pipe->dentry->d_parent;
526			__nfs_idmap_unregister(clp->cl_idmap->idmap_pipe);
527			/*
528			 * Note: This is a dirty hack. SUNRPC hook has been
529			 * called already but simple_rmdir() call for the
530			 * directory returned with error because of idmap pipe
531			 * inside. Thus now we have to remove this directory
532			 * here.
533			 */
534			if (rpc_rmdir(parent))
535				printk(KERN_ERR "NFS: %s: failed to remove "
536					"clnt dir!\n", __func__);
537		}
538		break;
539	default:
540		printk(KERN_ERR "NFS: %s: unknown event: %ld\n", __func__,
541			event);
542		return -ENOTSUPP;
543	}
544	return err;
545}
546
547static struct nfs_client *nfs_get_client_for_event(struct net *net, int event)
548{
549	struct nfs_net *nn = net_generic(net, nfs_net_id);
550	struct dentry *cl_dentry;
551	struct nfs_client *clp;
552	int err;
553
554restart:
555	spin_lock(&nn->nfs_client_lock);
556	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
557		/* Wait for initialisation to finish */
558		if (clp->cl_cons_state == NFS_CS_INITING) {
559			atomic_inc(&clp->cl_count);
560			spin_unlock(&nn->nfs_client_lock);
561			err = nfs_wait_client_init_complete(clp);
562			nfs_put_client(clp);
563			if (err)
564				return NULL;
565			goto restart;
566		}
567		/* Skip nfs_clients that failed to initialise */
568		if (clp->cl_cons_state < 0)
569			continue;
570		smp_rmb();
571		if (clp->rpc_ops != &nfs_v4_clientops)
572			continue;
573		cl_dentry = clp->cl_idmap->idmap_pipe->dentry;
574		if (((event == RPC_PIPEFS_MOUNT) && cl_dentry) ||
575		    ((event == RPC_PIPEFS_UMOUNT) && !cl_dentry))
576			continue;
577		atomic_inc(&clp->cl_count);
578		spin_unlock(&nn->nfs_client_lock);
579		return clp;
580	}
581	spin_unlock(&nn->nfs_client_lock);
582	return NULL;
583}
584
585static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
586			    void *ptr)
587{
588	struct super_block *sb = ptr;
589	struct nfs_client *clp;
590	int error = 0;
591
592	if (!try_module_get(THIS_MODULE))
593		return 0;
594
595	while ((clp = nfs_get_client_for_event(sb->s_fs_info, event))) {
596		error = __rpc_pipefs_event(clp, event, sb);
597		nfs_put_client(clp);
598		if (error)
599			break;
600	}
601	module_put(THIS_MODULE);
602	return error;
603}
604
605#define PIPEFS_NFS_PRIO		1
606
607static struct notifier_block nfs_idmap_block = {
608	.notifier_call	= rpc_pipefs_event,
609	.priority	= SUNRPC_PIPEFS_NFS_PRIO,
610};
611
612int nfs_idmap_init(void)
613{
614	int ret;
615	ret = nfs_idmap_init_keyring();
616	if (ret != 0)
617		goto out;
618	ret = rpc_pipefs_notifier_register(&nfs_idmap_block);
619	if (ret != 0)
620		nfs_idmap_quit_keyring();
621out:
622	return ret;
623}
624
625void nfs_idmap_quit(void)
626{
627	rpc_pipefs_notifier_unregister(&nfs_idmap_block);
628	nfs_idmap_quit_keyring();
629}
630
631static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
632				     struct idmap_msg *im,
633				     struct rpc_pipe_msg *msg)
634{
635	substring_t substr;
636	int token, ret;
637
638	memset(im,  0, sizeof(*im));
639	memset(msg, 0, sizeof(*msg));
640
641	im->im_type = IDMAP_TYPE_GROUP;
642	token = match_token(desc, nfs_idmap_tokens, &substr);
643
644	switch (token) {
645	case Opt_find_uid:
646		im->im_type = IDMAP_TYPE_USER;
647	case Opt_find_gid:
648		im->im_conv = IDMAP_CONV_NAMETOID;
649		ret = match_strlcpy(im->im_name, &substr, IDMAP_NAMESZ);
650		break;
651
652	case Opt_find_user:
653		im->im_type = IDMAP_TYPE_USER;
654	case Opt_find_group:
655		im->im_conv = IDMAP_CONV_IDTONAME;
656		ret = match_int(&substr, &im->im_id);
657		break;
658
659	default:
660		ret = -EINVAL;
661		goto out;
662	}
663
664	msg->data = im;
665	msg->len  = sizeof(struct idmap_msg);
666
667out:
668	return ret;
669}
670
671static int nfs_idmap_legacy_upcall(struct key_construction *cons,
672				   const char *op,
673				   void *aux)
674{
675	struct idmap_legacy_upcalldata *data;
676	struct rpc_pipe_msg *msg;
677	struct idmap_msg *im;
678	struct idmap *idmap = (struct idmap *)aux;
679	struct key *key = cons->key;
680	int ret = -ENOMEM;
681
682	/* msg and im are freed in idmap_pipe_destroy_msg */
683	data = kmalloc(sizeof(*data), GFP_KERNEL);
684	if (!data)
685		goto out1;
686
687	msg = &data->pipe_msg;
688	im = &data->idmap_msg;
689	data->idmap = idmap;
690
691	ret = nfs_idmap_prepare_message(key->description, idmap, im, msg);
692	if (ret < 0)
693		goto out2;
694
695	BUG_ON(idmap->idmap_key_cons != NULL);
696	idmap->idmap_key_cons = cons;
697
698	ret = rpc_queue_upcall(idmap->idmap_pipe, msg);
699	if (ret < 0)
700		goto out3;
701
702	return ret;
703
704out3:
705	idmap->idmap_key_cons = NULL;
706out2:
707	kfree(data);
708out1:
709	complete_request_key(cons, ret);
710	return ret;
711}
712
713static int nfs_idmap_instantiate(struct key *key, struct key *authkey, char *data)
714{
715	return key_instantiate_and_link(key, data, strlen(data) + 1,
716					id_resolver_cache->thread_keyring,
717					authkey);
718}
719
720static int nfs_idmap_read_message(struct idmap_msg *im, struct key *key, struct key *authkey)
721{
722	char id_str[NFS_UINT_MAXLEN];
723	int ret = -EINVAL;
724
725	switch (im->im_conv) {
726	case IDMAP_CONV_NAMETOID:
727		sprintf(id_str, "%d", im->im_id);
728		ret = nfs_idmap_instantiate(key, authkey, id_str);
729		break;
730	case IDMAP_CONV_IDTONAME:
731		ret = nfs_idmap_instantiate(key, authkey, im->im_name);
732		break;
733	}
734
735	return ret;
736}
737
738static ssize_t
739idmap_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
740{
741	struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
742	struct idmap *idmap = (struct idmap *)rpci->private;
743	struct key_construction *cons;
744	struct idmap_msg im;
745	size_t namelen_in;
746	int ret;
747
748	/* If instantiation is successful, anyone waiting for key construction
749	 * will have been woken up and someone else may now have used
750	 * idmap_key_cons - so after this point we may no longer touch it.
751	 */
752	cons = ACCESS_ONCE(idmap->idmap_key_cons);
753	idmap->idmap_key_cons = NULL;
754
755	if (mlen != sizeof(im)) {
756		ret = -ENOSPC;
757		goto out;
758	}
759
760	if (copy_from_user(&im, src, mlen) != 0) {
761		ret = -EFAULT;
762		goto out;
763	}
764
765	if (!(im.im_status & IDMAP_STATUS_SUCCESS)) {
766		ret = -ENOKEY;
767		goto out;
768	}
769
770	namelen_in = strnlen(im.im_name, IDMAP_NAMESZ);
771	if (namelen_in == 0 || namelen_in == IDMAP_NAMESZ) {
772		ret = -EINVAL;
773		goto out;
774	}
775
776	ret = nfs_idmap_read_message(&im, cons->key, cons->authkey);
777	if (ret >= 0) {
778		key_set_timeout(cons->key, nfs_idmap_cache_timeout);
779		ret = mlen;
780	}
781
782out:
783	complete_request_key(cons, ret);
784	return ret;
785}
786
787static void
788idmap_pipe_destroy_msg(struct rpc_pipe_msg *msg)
789{
790	struct idmap_legacy_upcalldata *data = container_of(msg,
791			struct idmap_legacy_upcalldata,
792			pipe_msg);
793	struct idmap *idmap = data->idmap;
794	struct key_construction *cons;
795	if (msg->errno) {
796		cons = ACCESS_ONCE(idmap->idmap_key_cons);
797		idmap->idmap_key_cons = NULL;
798		complete_request_key(cons, msg->errno);
799	}
800	/* Free memory allocated in nfs_idmap_legacy_upcall() */
801	kfree(data);
802}
803
804static void
805idmap_release_pipe(struct inode *inode)
806{
807	struct rpc_inode *rpci = RPC_I(inode);
808	struct idmap *idmap = (struct idmap *)rpci->private;
809	idmap->idmap_key_cons = NULL;
810}
811
812int nfs_map_name_to_uid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *uid)
813{
814	struct idmap *idmap = server->nfs_client->cl_idmap;
815
816	if (nfs_map_string_to_numeric(name, namelen, uid))
817		return 0;
818	return nfs_idmap_lookup_id(name, namelen, "uid", uid, idmap);
819}
820
821int nfs_map_group_to_gid(const struct nfs_server *server, const char *name, size_t namelen, __u32 *gid)
822{
823	struct idmap *idmap = server->nfs_client->cl_idmap;
824
825	if (nfs_map_string_to_numeric(name, namelen, gid))
826		return 0;
827	return nfs_idmap_lookup_id(name, namelen, "gid", gid, idmap);
828}
829
830int nfs_map_uid_to_name(const struct nfs_server *server, __u32 uid, char *buf, size_t buflen)
831{
832	struct idmap *idmap = server->nfs_client->cl_idmap;
833	int ret = -EINVAL;
834
835	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
836		ret = nfs_idmap_lookup_name(uid, "user", buf, buflen, idmap);
837	if (ret < 0)
838		ret = nfs_map_numeric_to_string(uid, buf, buflen);
839	return ret;
840}
841int nfs_map_gid_to_group(const struct nfs_server *server, __u32 gid, char *buf, size_t buflen)
842{
843	struct idmap *idmap = server->nfs_client->cl_idmap;
844	int ret = -EINVAL;
845
846	if (!(server->caps & NFS_CAP_UIDGID_NOMAP))
847		ret = nfs_idmap_lookup_name(gid, "group", buf, buflen, idmap);
848	if (ret < 0)
849		ret = nfs_map_numeric_to_string(gid, buf, buflen);
850	return ret;
851}