xref: /titanic_52/usr/src/uts/common/fs/nfs/nfs4_idmap.c (revision 0616fd7f2fe52dfe4b6189a7f510069a5b2aed73)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58ad9cc09Sth199096  * Common Development and Distribution License (the "License").
68ad9cc09Sth199096  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*0616fd7fSPavel Filipensky  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * There are well defined policies for mapping uid and gid values to and
287c478bd9Sstevel@tonic-gate  * from utf8 strings, as specified in RFC 3530. The protocol ops that are
297c478bd9Sstevel@tonic-gate  * most significantly affected by any changes in policy are GETATTR and
307c478bd9Sstevel@tonic-gate  * SETATTR, as these have different behavior depending on whether the id
317c478bd9Sstevel@tonic-gate  * mapping code is executing on the client or server. Thus, the following
327c478bd9Sstevel@tonic-gate  * rules represents the latest incantation of the id mapping policies.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * 1) For the case in which the nfsmapid(1m) daemon has _never_ been
357c478bd9Sstevel@tonic-gate  *    started, the policy is to _always_ work with stringified uid's
367c478bd9Sstevel@tonic-gate  *    and gid's
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * 2) For the case in which the nfsmapid(1m) daemon _was_ started but
397c478bd9Sstevel@tonic-gate  *    has either died or become unresponsive, the mapping policies are
407c478bd9Sstevel@tonic-gate  *    as follows:
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *                      Server                             Client
437c478bd9Sstevel@tonic-gate  *         .-------------------------------.---------------------------------.
447c478bd9Sstevel@tonic-gate  *         |                               |                                 |
457c478bd9Sstevel@tonic-gate  *         | . Respond to req by replying  | . If attr string does not have  |
467c478bd9Sstevel@tonic-gate  *         |   success and map the [u/g]id |   '@' sign, attempt to decode   |
477c478bd9Sstevel@tonic-gate  *         |   into its literal id string  |   a stringified id; map to      |
487c478bd9Sstevel@tonic-gate  *         |                               |   *ID_NOBODY if not an encoded  |
497c478bd9Sstevel@tonic-gate  *         |                               |   id.                           |
507c478bd9Sstevel@tonic-gate  *         |                               |                                 |
517c478bd9Sstevel@tonic-gate  * GETATTR |                               | . If attr string _does_ have    |
527c478bd9Sstevel@tonic-gate  *         |                               |   '@' sign			     |
537c478bd9Sstevel@tonic-gate  *         |                               |   Map to *ID_NOBODY on failure. |
547c478bd9Sstevel@tonic-gate  *         |                               |                                 |
557c478bd9Sstevel@tonic-gate  *         | nfs_idmap_*id_str             | nfs_idmap_str_*id               |
567c478bd9Sstevel@tonic-gate  *         +-------------------------------+---------------------------------+
577c478bd9Sstevel@tonic-gate  *         |                               |                                 |
587c478bd9Sstevel@tonic-gate  *         | . Respond to req by returning | . _Must_ map the user's passed  |
597c478bd9Sstevel@tonic-gate  *         |  ECOMM, which will be mapped  |  in [u/g]id into it's network   |
607c478bd9Sstevel@tonic-gate  *         |  to NFS4ERR_DELAY to clnt     |  attr string, so contact the    |
617c478bd9Sstevel@tonic-gate  *         |                               |  daemon, retrying forever if    |
627c478bd9Sstevel@tonic-gate  *         |   Server must not allow the   |  necessary, unless interrupted  |
637c478bd9Sstevel@tonic-gate  * SETATTR |   mapping to *ID_NOBODY upon  |                                 |
647c478bd9Sstevel@tonic-gate  *         |   lack of communication with  |   Client _should_ specify the   |
657c478bd9Sstevel@tonic-gate  *         |   the daemon, which could     |   correct attr string for a     |
667c478bd9Sstevel@tonic-gate  *         |   result in the file being    |   SETATTR operation, otherwise  |
677c478bd9Sstevel@tonic-gate  *         |   inadvertently given away !  |   it can also result in the     |
687c478bd9Sstevel@tonic-gate  *         |                               |   file being inadvertently      |
697c478bd9Sstevel@tonic-gate  *         |                               |   given away !                  |
707c478bd9Sstevel@tonic-gate  *         |                               |                                 |
717c478bd9Sstevel@tonic-gate  *         | nfs_idmap_str_*id             |   nfs_idmap_*id_str             |
727c478bd9Sstevel@tonic-gate  *         `-------------------------------'---------------------------------'
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * 3) Lastly, in order to leverage better cache utilization whenever
757c478bd9Sstevel@tonic-gate  *    communication with nfsmapid(1m) is currently hindered, cache
767c478bd9Sstevel@tonic-gate  *    entry eviction is throttled whenever nfsidmap_daemon_dh == NULL.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  *  Server-side behavior for upcall communication errors
807c478bd9Sstevel@tonic-gate  *  ====================================================
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  *   GETATTR - Server-side GETATTR *id to attr string conversion policies
837c478bd9Sstevel@tonic-gate  *             for unresponsive/dead nfsmapid(1m) daemon
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *	a) If the *id is *ID_NOBODY, the string "nobody" is returned
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *	b) If the *id is not *ID_NOBODY _and_ the nfsmapid(1m) daemon
887c478bd9Sstevel@tonic-gate  *	   _is_ operational, the daemon is contacted to convert the
897c478bd9Sstevel@tonic-gate  *	   [u/g]id into a string of type "[user/group]@domain"
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  *	c) If the nfsmapid(1m) daemon has died or has become unresponsive,
927c478bd9Sstevel@tonic-gate  *	   the server returns status == NFS4_OK for the GETATTR operation,
937c478bd9Sstevel@tonic-gate  *	   and returns a strigified [u/g]id to let the client map it into
947c478bd9Sstevel@tonic-gate  *	   the appropriate value.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *   SETATTR - Server-side SETATTR attr string to *id conversion policies
977c478bd9Sstevel@tonic-gate  *             for unresponsive/dead nfsmapid(1m) daemon
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  *	a) If the otw string is a stringified uid (ie. does _not_ contain
1007c478bd9Sstevel@tonic-gate  *	   an '@' sign and is of the form "12345") then the literal uid is
1017c478bd9Sstevel@tonic-gate  *	   decoded and it is used to perform the mapping.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  *	b) If, on the other hand, the otw string _is_ of the form
1047c478bd9Sstevel@tonic-gate  *	   "[user/group]@domain" and problems arise contacting nfsmapid(1m),
1057c478bd9Sstevel@tonic-gate  *	   the SETATTR operation _must_ fail w/NFS4ERR_DELAY, as the server
1067c478bd9Sstevel@tonic-gate  *	   cannot default to *ID_NOBODY, which would allow a file to be
1077c478bd9Sstevel@tonic-gate  *	   given away by setting it's owner or owner_group to "nobody".
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate #include <sys/param.h>
1107c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1117c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1127c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
1137c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
1147c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1157c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1167c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1177c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1187c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
1197c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
1207c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1217c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1227c478bd9Sstevel@tonic-gate #include <sys/list.h>
1237c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1247c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
1257c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
126*0616fd7fSPavel Filipensky #include <sys/pkp_hash.h>
1277c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
1287c478bd9Sstevel@tonic-gate #include <nfs/rnode4.h>
1297c478bd9Sstevel@tonic-gate #include <nfs/nfsid_map.h>
1307c478bd9Sstevel@tonic-gate #include <nfs/nfs4_idmap_impl.h>
1317c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Truly global modular globals
1357c478bd9Sstevel@tonic-gate  */
1362f172c55SRobert Thurlow zone_key_t			nfsidmap_zone_key;
1377c478bd9Sstevel@tonic-gate static list_t			nfsidmap_globals_list;
1387c478bd9Sstevel@tonic-gate static kmutex_t			nfsidmap_globals_lock;
1397c478bd9Sstevel@tonic-gate static kmem_cache_t		*nfsidmap_cache;
1407c478bd9Sstevel@tonic-gate static int			nfs4_idcache_tout;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Some useful macros
1447c478bd9Sstevel@tonic-gate  */
145f0c710aaSjj209869 #define		MOD2(a, pow_of_2)	((a) & ((pow_of_2) - 1))
146f0c710aaSjj209869 #define		_CACHE_TOUT		(60*60)		/* secs in 1 hour */
1477c478bd9Sstevel@tonic-gate #define		TIMEOUT(x)		(gethrestime_sec() > \
148f0c710aaSjj209869 					((x) + nfs4_idcache_tout))
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * Max length of valid id string including the trailing null
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate #define		_MAXIDSTRLEN		11
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define		ID_HASH(id, hash)					\
1557c478bd9Sstevel@tonic-gate {									\
1567c478bd9Sstevel@tonic-gate 	(hash) = MOD2(((id) ^ NFSID_CACHE_ANCHORS), NFSID_CACHE_ANCHORS); \
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Prototypes
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static void	*nfs_idmap_init_zone(zoneid_t);
1647c478bd9Sstevel@tonic-gate static void	 nfs_idmap_fini_zone(zoneid_t, void *);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static int	 is_stringified_id(utf8string *);
1677c478bd9Sstevel@tonic-gate static void	 nfs_idmap_i2s_literal(uid_t, utf8string *);
1687c478bd9Sstevel@tonic-gate static int	 nfs_idmap_s2i_literal(utf8string *, uid_t *, int);
1697c478bd9Sstevel@tonic-gate static void	 nfs_idmap_reclaim(void *);
1707c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_reclaim(idmap_cache_info_t *);
1717c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_create(idmap_cache_info_t *, const char *);
1727c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_destroy(idmap_cache_info_t *);
1737c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_flush(idmap_cache_info_t *);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static uint_t	 nfs_idmap_cache_s2i_lkup(idmap_cache_info_t *, utf8string *,
1767c478bd9Sstevel@tonic-gate 			uint_t *, uid_t *);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static uint_t	 nfs_idmap_cache_i2s_lkup(idmap_cache_info_t *, uid_t,
1797c478bd9Sstevel@tonic-gate 			uint_t *, utf8string *);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_s2i_insert(idmap_cache_info_t *, uid_t,
1827c478bd9Sstevel@tonic-gate 			utf8string *, hash_stat, uint_t);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_i2s_insert(idmap_cache_info_t *, uid_t,
1857c478bd9Sstevel@tonic-gate 			utf8string *, hash_stat, uint_t);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static void	 nfs_idmap_cache_rment(nfsidmap_t *);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Initialization routine for NFSv4 id mapping
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate void
1937c478bd9Sstevel@tonic-gate nfs_idmap_init(void)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Initialize the kmem cache
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	nfsidmap_cache = kmem_cache_create("NFS_idmap_cache",
1997c478bd9Sstevel@tonic-gate 	    sizeof (nfsidmap_t), 0, NULL, NULL, nfs_idmap_reclaim, NULL,
2007c478bd9Sstevel@tonic-gate 	    NULL, 0);
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * If not set in "/etc/system", set to default value
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	if (!nfs4_idcache_tout)
2057c478bd9Sstevel@tonic-gate 		nfs4_idcache_tout = _CACHE_TOUT;
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Initialize the list of nfsidmap_globals
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	mutex_init(&nfsidmap_globals_lock, NULL, MUTEX_DEFAULT, NULL);
2107c478bd9Sstevel@tonic-gate 	list_create(&nfsidmap_globals_list, sizeof (struct nfsidmap_globals),
2117c478bd9Sstevel@tonic-gate 	    offsetof(struct nfsidmap_globals, nig_link));
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Initialize the zone_key_t for per-zone idmaps
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	zone_key_create(&nfsidmap_zone_key, nfs_idmap_init_zone, NULL,
2167c478bd9Sstevel@tonic-gate 	    nfs_idmap_fini_zone);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Called only when module was not loaded properly
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate void
2237c478bd9Sstevel@tonic-gate nfs_idmap_fini(void)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(nfsidmap_zone_key);
2267c478bd9Sstevel@tonic-gate 	list_destroy(&nfsidmap_globals_list);
2277c478bd9Sstevel@tonic-gate 	mutex_destroy(&nfsidmap_globals_lock);
2287c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(nfsidmap_cache);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2327c478bd9Sstevel@tonic-gate static void *
2337c478bd9Sstevel@tonic-gate nfs_idmap_init_zone(zoneid_t zoneid)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	nig = kmem_alloc(sizeof (*nig), KM_SLEEP);
2387c478bd9Sstevel@tonic-gate 	nig->nig_msg_done = 0;
2397c478bd9Sstevel@tonic-gate 	mutex_init(&nig->nfsidmap_daemon_lock, NULL, MUTEX_DEFAULT, NULL);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * nfsidmap certainly isn't running.
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	nig->nfsidmap_pid = NOPID;
2457c478bd9Sstevel@tonic-gate 	nig->nfsidmap_daemon_dh = NULL;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * Create the idmap caches
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_create(&nig->u2s_ci, "u2s_cache");
2517c478bd9Sstevel@tonic-gate 	nig->u2s_ci.nfsidmap_daemon_dh = &nig->nfsidmap_daemon_dh;
2527c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_create(&nig->s2u_ci, "s2u_cache");
2537c478bd9Sstevel@tonic-gate 	nig->s2u_ci.nfsidmap_daemon_dh = &nig->nfsidmap_daemon_dh;
2547c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_create(&nig->g2s_ci, "g2s_cache");
2557c478bd9Sstevel@tonic-gate 	nig->g2s_ci.nfsidmap_daemon_dh = &nig->nfsidmap_daemon_dh;
2567c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_create(&nig->s2g_ci, "s2g_cache");
2577c478bd9Sstevel@tonic-gate 	nig->s2g_ci.nfsidmap_daemon_dh = &nig->nfsidmap_daemon_dh;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * Add to global list.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	mutex_enter(&nfsidmap_globals_lock);
2637c478bd9Sstevel@tonic-gate 	list_insert_head(&nfsidmap_globals_list, nig);
2647c478bd9Sstevel@tonic-gate 	mutex_exit(&nfsidmap_globals_lock);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (nig);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2707c478bd9Sstevel@tonic-gate static void
2717c478bd9Sstevel@tonic-gate nfs_idmap_fini_zone(zoneid_t zoneid, void *arg)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig = arg;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Remove from list.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	mutex_enter(&nfsidmap_globals_lock);
2797c478bd9Sstevel@tonic-gate 	list_remove(&nfsidmap_globals_list, nig);
2807c478bd9Sstevel@tonic-gate 	/*
2817c478bd9Sstevel@tonic-gate 	 * Destroy the idmap caches
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_destroy(&nig->u2s_ci);
2847c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_destroy(&nig->s2u_ci);
2857c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_destroy(&nig->g2s_ci);
2867c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_destroy(&nig->s2g_ci);
2877c478bd9Sstevel@tonic-gate 	mutex_exit(&nfsidmap_globals_lock);
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * Cleanup
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	if (nig->nfsidmap_daemon_dh)
2927c478bd9Sstevel@tonic-gate 		door_ki_rele(nig->nfsidmap_daemon_dh);
2937c478bd9Sstevel@tonic-gate 	mutex_destroy(&nig->nfsidmap_daemon_lock);
2947c478bd9Sstevel@tonic-gate 	kmem_free(nig, sizeof (*nig));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * Convert a user utf-8 string identifier into its local uid.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate nfs_idmap_str_uid(utf8string *u8s, uid_t *uid, bool_t isserver)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	int			error;
3047c478bd9Sstevel@tonic-gate 	uint_t			hashno = 0;
3057c478bd9Sstevel@tonic-gate 	const char		*whoami = "nfs_idmap_str_uid";
3067c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig;
3077c478bd9Sstevel@tonic-gate 	struct mapid_arg	*mapargp;
3087c478bd9Sstevel@tonic-gate 	struct mapid_res	mapres;
3097c478bd9Sstevel@tonic-gate 	struct mapid_res	*mapresp = &mapres;
3107c478bd9Sstevel@tonic-gate 	struct mapid_res	*resp = mapresp;
3117c478bd9Sstevel@tonic-gate 	door_arg_t		door_args;
3127c478bd9Sstevel@tonic-gate 	door_handle_t		dh;
3137c478bd9Sstevel@tonic-gate 
314108322fbScarlsonj 	nig = zone_getspecific(nfsidmap_zone_key, nfs_zone());
3157c478bd9Sstevel@tonic-gate 	ASSERT(nig != NULL);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (!u8s || !u8s->utf8string_val || u8s->utf8string_len == 0 ||
3187c478bd9Sstevel@tonic-gate 	    (u8s->utf8string_val[0] == '\0')) {
3197c478bd9Sstevel@tonic-gate 		*uid = UID_NOBODY;
3207c478bd9Sstevel@tonic-gate 		return (isserver ? EINVAL : 0);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * If "nobody", just short circuit and bail
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if (bcmp(u8s->utf8string_val, "nobody", 6) == 0) {
3277c478bd9Sstevel@tonic-gate 		*uid = UID_NOBODY;
3287c478bd9Sstevel@tonic-gate 		return (0);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * Start-off with upcalls disabled, and once nfsmapid(1m) is up and
3337c478bd9Sstevel@tonic-gate 	 * running, we'll leverage it's first flush to let the kernel know
3347c478bd9Sstevel@tonic-gate 	 * when it's up and available to perform mappings. Also, on client
3357c478bd9Sstevel@tonic-gate 	 * only, be smarter about when to issue upcalls by checking the
3367c478bd9Sstevel@tonic-gate 	 * string for existence of an '@' sign. If no '@' sign, then we just
3377c478bd9Sstevel@tonic-gate 	 * make our best effort to decode the string ourselves.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate retry:
3407c478bd9Sstevel@tonic-gate 	mutex_enter(&nig->nfsidmap_daemon_lock);
3417c478bd9Sstevel@tonic-gate 	dh = nig->nfsidmap_daemon_dh;
3427c478bd9Sstevel@tonic-gate 	if (dh)
3437c478bd9Sstevel@tonic-gate 		door_ki_hold(dh);
3447c478bd9Sstevel@tonic-gate 	mutex_exit(&nig->nfsidmap_daemon_lock);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (dh == NULL || nig->nfsidmap_pid == curproc->p_pid ||
3477c478bd9Sstevel@tonic-gate 	    (!utf8_strchr(u8s, '@') && !isserver)) {
3487c478bd9Sstevel@tonic-gate 		if (dh)
3497c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
3507c478bd9Sstevel@tonic-gate 		error = nfs_idmap_s2i_literal(u8s, uid, isserver);
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * If we get a numeric value, but we only do so because
3537c478bd9Sstevel@tonic-gate 		 * we are nfsmapid, return ENOTSUP to indicate a valid
3547c478bd9Sstevel@tonic-gate 		 * response, but not to cache it.
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		if (!error && nig->nfsidmap_pid == curproc->p_pid)
3577c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
3587c478bd9Sstevel@tonic-gate 		return (error);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* cache hit */
3627c478bd9Sstevel@tonic-gate 	if (nfs_idmap_cache_s2i_lkup(&nig->s2u_ci, u8s, &hashno, uid)) {
3637c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
3647c478bd9Sstevel@tonic-gate 		return (0);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/* cache miss */
3687c478bd9Sstevel@tonic-gate 	mapargp = kmem_alloc(MAPID_ARG_LEN(u8s->utf8string_len), KM_SLEEP);
3697c478bd9Sstevel@tonic-gate 	mapargp->cmd = NFSMAPID_STR_UID;
3707c478bd9Sstevel@tonic-gate 	mapargp->u_arg.len = u8s->utf8string_len;
3717c478bd9Sstevel@tonic-gate 	(void) bcopy(u8s->utf8string_val, mapargp->str, mapargp->u_arg.len);
3727c478bd9Sstevel@tonic-gate 	mapargp->str[mapargp->u_arg.len] = '\0';
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	door_args.data_ptr = (char *)mapargp;
3757c478bd9Sstevel@tonic-gate 	door_args.data_size = MAPID_ARG_LEN(mapargp->u_arg.len);
3767c478bd9Sstevel@tonic-gate 	door_args.desc_ptr = NULL;
3777c478bd9Sstevel@tonic-gate 	door_args.desc_num = 0;
3787c478bd9Sstevel@tonic-gate 	door_args.rbuf = (char *)mapresp;
3797c478bd9Sstevel@tonic-gate 	door_args.rsize = sizeof (struct mapid_res);
3807c478bd9Sstevel@tonic-gate 
381323a81d9Sjwadams 	error = door_ki_upcall_limited(dh, &door_args, NULL, SIZE_MAX, 0);
3827c478bd9Sstevel@tonic-gate 	if (!error) {
3837c478bd9Sstevel@tonic-gate 		resp = (struct mapid_res *)door_args.rbuf;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		/* Should never provide daemon with bad args */
3867c478bd9Sstevel@tonic-gate 		ASSERT(resp->status != NFSMAPID_INVALID);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		switch (resp->status) {
3897c478bd9Sstevel@tonic-gate 		case NFSMAPID_OK:
3907c478bd9Sstevel@tonic-gate 			/*
3917c478bd9Sstevel@tonic-gate 			 * Valid mapping. Cache it.
3927c478bd9Sstevel@tonic-gate 			 */
3937c478bd9Sstevel@tonic-gate 			*uid = resp->u_res.uid;
3947c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_s2i_insert(&nig->s2u_ci, *uid,
3957c478bd9Sstevel@tonic-gate 			    u8s, HQ_HASH_HINT, hashno);
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		case NFSMAPID_NUMSTR:
3997c478bd9Sstevel@tonic-gate 			/*
4007c478bd9Sstevel@tonic-gate 			 * string came in as stringified id. Don't cache !
4017c478bd9Sstevel@tonic-gate 			 *
4027c478bd9Sstevel@tonic-gate 			 * nfsmapid(1m) semantics have changed in order to
4037c478bd9Sstevel@tonic-gate 			 * support diskless clients. Thus, for stringified
4047c478bd9Sstevel@tonic-gate 			 * id's that have passwd/group entries, we'll go
4057c478bd9Sstevel@tonic-gate 			 * ahead and map them, returning no error.
4067c478bd9Sstevel@tonic-gate 			 */
4077c478bd9Sstevel@tonic-gate 			*uid = resp->u_res.uid;
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADDOMAIN:
4117c478bd9Sstevel@tonic-gate 			/*
4127c478bd9Sstevel@tonic-gate 			 * Make the offending "user@domain" string readily
4137c478bd9Sstevel@tonic-gate 			 * available to D scripts that enable the probe.
4147c478bd9Sstevel@tonic-gate 			 */
4157c478bd9Sstevel@tonic-gate 			DTRACE_PROBE1(nfs4__str__uid, char *, mapargp->str);
4167c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		case NFSMAPID_INVALID:
4197c478bd9Sstevel@tonic-gate 		case NFSMAPID_UNMAPPABLE:
4207c478bd9Sstevel@tonic-gate 		case NFSMAPID_INTERNAL:
4217c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADID:
4227c478bd9Sstevel@tonic-gate 		case NFSMAPID_NOTFOUND:
4237c478bd9Sstevel@tonic-gate 		default:
4247c478bd9Sstevel@tonic-gate 			/*
4257c478bd9Sstevel@tonic-gate 			 * For now, treat all of these errors as equal.
4267c478bd9Sstevel@tonic-gate 			 *
4277c478bd9Sstevel@tonic-gate 			 * Return error on the server side, then the
4287c478bd9Sstevel@tonic-gate 			 * server returns NFS4_BADOWNER to the client.
4297c478bd9Sstevel@tonic-gate 			 * On client side, just map to UID_NOBODY.
4307c478bd9Sstevel@tonic-gate 			 */
4317c478bd9Sstevel@tonic-gate 			if (isserver)
4327c478bd9Sstevel@tonic-gate 				error = EPERM;
4337c478bd9Sstevel@tonic-gate 			else
4347c478bd9Sstevel@tonic-gate 				*uid = UID_NOBODY;
4357c478bd9Sstevel@tonic-gate 			break;
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		kmem_free(mapargp, MAPID_ARG_LEN(u8s->utf8string_len));
4387c478bd9Sstevel@tonic-gate 		if (resp != mapresp)
4397c478bd9Sstevel@tonic-gate 			kmem_free(door_args.rbuf, door_args.rsize);
4407c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
4417c478bd9Sstevel@tonic-gate 		return (error);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	kmem_free(mapargp, MAPID_ARG_LEN(u8s->utf8string_len));
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * We got some door error
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	switch (error) {
4497c478bd9Sstevel@tonic-gate 	case EINTR:
4507c478bd9Sstevel@tonic-gate 		/*
4517c478bd9Sstevel@tonic-gate 		 * If we took an interrupt we have to bail out.
4527c478bd9Sstevel@tonic-gate 		 */
4537c478bd9Sstevel@tonic-gate 		if (ttolwp(curthread) && ISSIG(curthread, JUSTLOOKING)) {
4547c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
4557c478bd9Sstevel@tonic-gate 			return (EINTR);
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * We may have gotten EINTR for other reasons like the
4607c478bd9Sstevel@tonic-gate 		 * door being revoked on us, instead of trying to
4617c478bd9Sstevel@tonic-gate 		 * extract this out of the door handle, sleep
4627c478bd9Sstevel@tonic-gate 		 * and try again, if still revoked we will get EBADF
4637c478bd9Sstevel@tonic-gate 		 * next time through.
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
4667c478bd9Sstevel@tonic-gate 	case EAGAIN:    /* process may be forking */
4677c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
4687c478bd9Sstevel@tonic-gate 		/*
4697c478bd9Sstevel@tonic-gate 		 * Back off for a bit
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		delay(hz);
4727c478bd9Sstevel@tonic-gate 		goto retry;
4737c478bd9Sstevel@tonic-gate 	default:	/* Unknown must be fatal */
4747c478bd9Sstevel@tonic-gate 	case EBADF:	/* Invalid door */
4757c478bd9Sstevel@tonic-gate 	case EINVAL:	/* Not a door, wrong target */
4767c478bd9Sstevel@tonic-gate 		/*
4777c478bd9Sstevel@tonic-gate 		 * A fatal door error, if our failing door handle is the
4787c478bd9Sstevel@tonic-gate 		 * current door handle, clean up our state and
4797c478bd9Sstevel@tonic-gate 		 * mark the server dead.
4807c478bd9Sstevel@tonic-gate 		 */
4817c478bd9Sstevel@tonic-gate 		mutex_enter(&nig->nfsidmap_daemon_lock);
4827c478bd9Sstevel@tonic-gate 		if (dh == nig->nfsidmap_daemon_dh) {
4837c478bd9Sstevel@tonic-gate 			door_ki_rele(nig->nfsidmap_daemon_dh);
4847c478bd9Sstevel@tonic-gate 			nig->nfsidmap_daemon_dh = NULL;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		mutex_exit(&nig->nfsidmap_daemon_lock);
4877c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		if (isserver)
4907c478bd9Sstevel@tonic-gate 			return (ECOMM);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		/*
4937c478bd9Sstevel@tonic-gate 		 * Note: We've already done optimizations above to check
4947c478bd9Sstevel@tonic-gate 		 *	 for '@' sign, so if we can't comm w/nfsmapid, we
4957c478bd9Sstevel@tonic-gate 		 *	 _know_ this _can't_ be a stringified uid.
4967c478bd9Sstevel@tonic-gate 		 */
4977c478bd9Sstevel@tonic-gate 		if (!nig->nig_msg_done) {
4987c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
4997c478bd9Sstevel@tonic-gate 			    "!%s: Can't communicate with mapping daemon "
5007c478bd9Sstevel@tonic-gate 			    "nfsmapid", whoami);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 			nig->nig_msg_done = 1;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		*uid = UID_NOBODY;
5057c478bd9Sstevel@tonic-gate 		return (0);
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate  * Convert a uid into its utf-8 string representation.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate int
5147c478bd9Sstevel@tonic-gate nfs_idmap_uid_str(uid_t uid, utf8string *u8s, bool_t isserver)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	int			error;
5177c478bd9Sstevel@tonic-gate 	uint_t			hashno = 0;
5187c478bd9Sstevel@tonic-gate 	const char		*whoami = "nfs_idmap_uid_str";
5197c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals	*nig;
5207c478bd9Sstevel@tonic-gate 	struct mapid_arg	maparg;
5217c478bd9Sstevel@tonic-gate 	struct mapid_res	mapres;
5227c478bd9Sstevel@tonic-gate 	struct mapid_res	*mapresp = &mapres;
5237c478bd9Sstevel@tonic-gate 	struct mapid_res	*resp = mapresp;
5247c478bd9Sstevel@tonic-gate 	door_arg_t		door_args;
5257c478bd9Sstevel@tonic-gate 	door_handle_t		dh;
5267c478bd9Sstevel@tonic-gate 
527108322fbScarlsonj 	nig = zone_getspecific(nfsidmap_zone_key, nfs_zone());
5287c478bd9Sstevel@tonic-gate 	ASSERT(nig != NULL);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/*
5317c478bd9Sstevel@tonic-gate 	 * If the supplied uid is "nobody", then we don't look at the
5327c478bd9Sstevel@tonic-gate 	 * cache, since we DON'T cache it in the u2s_cache. We cannot
5337c478bd9Sstevel@tonic-gate 	 * tell two strings apart from caching the same uid.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (uid == UID_NOBODY) {
5367c478bd9Sstevel@tonic-gate 		(void) str_to_utf8("nobody", u8s);
5377c478bd9Sstevel@tonic-gate 		return (0);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * Start-off with upcalls disabled, and once nfsmapid(1m) is
5427c478bd9Sstevel@tonic-gate 	 * up and running, we'll leverage it's first flush to let the
5437c478bd9Sstevel@tonic-gate 	 * kernel know when it's up and available to perform mappings.
5447c478bd9Sstevel@tonic-gate 	 * We fall back to answering with stringified uid's.
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate retry:
5477c478bd9Sstevel@tonic-gate 	mutex_enter(&nig->nfsidmap_daemon_lock);
5487c478bd9Sstevel@tonic-gate 	dh = nig->nfsidmap_daemon_dh;
5497c478bd9Sstevel@tonic-gate 	if (dh)
5507c478bd9Sstevel@tonic-gate 		door_ki_hold(dh);
5517c478bd9Sstevel@tonic-gate 	mutex_exit(&nig->nfsidmap_daemon_lock);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (dh == NULL || nig->nfsidmap_pid == curproc->p_pid) {
5547c478bd9Sstevel@tonic-gate 		if (dh)
5557c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
5567c478bd9Sstevel@tonic-gate 		nfs_idmap_i2s_literal(uid, u8s);
5577c478bd9Sstevel@tonic-gate 		return (0);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/* cache hit */
5617c478bd9Sstevel@tonic-gate 	if (nfs_idmap_cache_i2s_lkup(&nig->u2s_ci, uid, &hashno, u8s)) {
5627c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
5637c478bd9Sstevel@tonic-gate 		return (0);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* cache miss */
5677c478bd9Sstevel@tonic-gate 	maparg.cmd = NFSMAPID_UID_STR;
5687c478bd9Sstevel@tonic-gate 	maparg.u_arg.uid = uid;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	door_args.data_ptr = (char *)&maparg;
5717c478bd9Sstevel@tonic-gate 	door_args.data_size = sizeof (struct mapid_arg);
5727c478bd9Sstevel@tonic-gate 	door_args.desc_ptr = NULL;
5737c478bd9Sstevel@tonic-gate 	door_args.desc_num = 0;
5747c478bd9Sstevel@tonic-gate 	door_args.rbuf = (char *)mapresp;
5757c478bd9Sstevel@tonic-gate 	door_args.rsize = sizeof (struct mapid_res);
5767c478bd9Sstevel@tonic-gate 
577323a81d9Sjwadams 	error = door_ki_upcall_limited(dh, &door_args, NULL, SIZE_MAX, 0);
5787c478bd9Sstevel@tonic-gate 	if (!error) {
5797c478bd9Sstevel@tonic-gate 		resp = (struct mapid_res *)door_args.rbuf;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/* Should never provide daemon with bad args */
5827c478bd9Sstevel@tonic-gate 		ASSERT(resp->status != NFSMAPID_INVALID);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		switch (resp->status) {
5857c478bd9Sstevel@tonic-gate 		case NFSMAPID_OK:
5867c478bd9Sstevel@tonic-gate 			/*
5877c478bd9Sstevel@tonic-gate 			 * We now have a valid result from the
5887c478bd9Sstevel@tonic-gate 			 * user-land daemon, so cache the result (if need be).
5897c478bd9Sstevel@tonic-gate 			 * Load return value first then do the caches.
5907c478bd9Sstevel@tonic-gate 			 */
5917c478bd9Sstevel@tonic-gate 			(void) str_to_utf8(resp->str, u8s);
5927c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_i2s_insert(&nig->u2s_ci, uid,
5937c478bd9Sstevel@tonic-gate 			    u8s, HQ_HASH_HINT, hashno);
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		case NFSMAPID_INVALID:
5977c478bd9Sstevel@tonic-gate 		case NFSMAPID_UNMAPPABLE:
5987c478bd9Sstevel@tonic-gate 		case NFSMAPID_INTERNAL:
5997c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADDOMAIN:
6007c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADID:
6017c478bd9Sstevel@tonic-gate 		case NFSMAPID_NOTFOUND:
6027c478bd9Sstevel@tonic-gate 		default:
6037c478bd9Sstevel@tonic-gate 			/*
6047c478bd9Sstevel@tonic-gate 			 * For now, treat all of these errors as equal.
6057c478bd9Sstevel@tonic-gate 			 */
6067c478bd9Sstevel@tonic-gate 			error = EPERM;
6077c478bd9Sstevel@tonic-gate 			break;
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		if (resp != mapresp)
6117c478bd9Sstevel@tonic-gate 			kmem_free(door_args.rbuf, door_args.rsize);
6127c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
6137c478bd9Sstevel@tonic-gate 		return (error);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * We got some door error
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	switch (error) {
6207c478bd9Sstevel@tonic-gate 	case EINTR:
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * If we took an interrupt we have to bail out.
6237c478bd9Sstevel@tonic-gate 		 */
6247c478bd9Sstevel@tonic-gate 		if (ttolwp(curthread) && ISSIG(curthread, JUSTLOOKING)) {
6257c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
6267c478bd9Sstevel@tonic-gate 			return (EINTR);
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * We may have gotten EINTR for other reasons like the
6317c478bd9Sstevel@tonic-gate 		 * door being revoked on us, instead of trying to
6327c478bd9Sstevel@tonic-gate 		 * extract this out of the door handle, sleep
6337c478bd9Sstevel@tonic-gate 		 * and try again, if still revoked we will get EBADF
6347c478bd9Sstevel@tonic-gate 		 * next time through.
6357c478bd9Sstevel@tonic-gate 		 */
6367c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
6377c478bd9Sstevel@tonic-gate 	case EAGAIN:    /* process may be forking */
6387c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
6397c478bd9Sstevel@tonic-gate 		/*
6407c478bd9Sstevel@tonic-gate 		 * Back off for a bit
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate 		delay(hz);
6437c478bd9Sstevel@tonic-gate 		goto retry;
6447c478bd9Sstevel@tonic-gate 	default:	/* Unknown must be fatal */
6457c478bd9Sstevel@tonic-gate 	case EBADF:	/* Invalid door */
6467c478bd9Sstevel@tonic-gate 	case EINVAL:	/* Not a door, wrong target */
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * A fatal door error, if our failing door handle is the
6497c478bd9Sstevel@tonic-gate 		 * current door handle, clean up our state and
6507c478bd9Sstevel@tonic-gate 		 * mark the server dead.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		mutex_enter(&nig->nfsidmap_daemon_lock);
6537c478bd9Sstevel@tonic-gate 		if (dh == nig->nfsidmap_daemon_dh) {
6547c478bd9Sstevel@tonic-gate 			door_ki_rele(nig->nfsidmap_daemon_dh);
6557c478bd9Sstevel@tonic-gate 			nig->nfsidmap_daemon_dh = NULL;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		mutex_exit(&nig->nfsidmap_daemon_lock);
6587c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Log error on client-side only
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (!nig->nig_msg_done && !isserver) {
6647c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
6657c478bd9Sstevel@tonic-gate 			    "!%s: Can't communicate with mapping daemon "
6667c478bd9Sstevel@tonic-gate 			    "nfsmapid", whoami);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 			nig->nig_msg_done = 1;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		nfs_idmap_i2s_literal(uid, u8s);
6717c478bd9Sstevel@tonic-gate 		return (0);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Convert a group utf-8 string identifier into its local gid.
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate int
6807c478bd9Sstevel@tonic-gate nfs_idmap_str_gid(utf8string *u8s, gid_t *gid, bool_t isserver)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	int			error;
6837c478bd9Sstevel@tonic-gate 	uint_t			hashno = 0;
6847c478bd9Sstevel@tonic-gate 	const char		*whoami = "nfs_idmap_str_gid";
6857c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig;
6867c478bd9Sstevel@tonic-gate 	struct mapid_arg	*mapargp;
6877c478bd9Sstevel@tonic-gate 	struct mapid_res	mapres;
6887c478bd9Sstevel@tonic-gate 	struct mapid_res	*mapresp = &mapres;
6897c478bd9Sstevel@tonic-gate 	struct mapid_res	*resp = mapresp;
6907c478bd9Sstevel@tonic-gate 	door_arg_t		door_args;
6917c478bd9Sstevel@tonic-gate 	door_handle_t		dh;
6927c478bd9Sstevel@tonic-gate 
693108322fbScarlsonj 	nig = zone_getspecific(nfsidmap_zone_key, nfs_zone());
6947c478bd9Sstevel@tonic-gate 	ASSERT(nig != NULL);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (!u8s || !u8s->utf8string_val || u8s->utf8string_len == 0 ||
6977c478bd9Sstevel@tonic-gate 	    (u8s->utf8string_val[0] == '\0')) {
6987c478bd9Sstevel@tonic-gate 		*gid = GID_NOBODY;
6997c478bd9Sstevel@tonic-gate 		return (isserver ? EINVAL : 0);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * If "nobody", just short circuit and bail
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	if (bcmp(u8s->utf8string_val, "nobody", 6) == 0) {
7067c478bd9Sstevel@tonic-gate 		*gid = GID_NOBODY;
7077c478bd9Sstevel@tonic-gate 		return (0);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Start-off with upcalls disabled, and once nfsmapid(1m) is up and
7127c478bd9Sstevel@tonic-gate 	 * running, we'll leverage it's first flush to let the kernel know
7137c478bd9Sstevel@tonic-gate 	 * when it's up and available to perform mappings. Also, on client
7147c478bd9Sstevel@tonic-gate 	 * only, be smarter about when to issue upcalls by checking the
7157c478bd9Sstevel@tonic-gate 	 * string for existence of an '@' sign. If no '@' sign, then we just
7167c478bd9Sstevel@tonic-gate 	 * make our best effort to decode the string ourselves.
7177c478bd9Sstevel@tonic-gate 	 */
7187c478bd9Sstevel@tonic-gate retry:
7197c478bd9Sstevel@tonic-gate 	mutex_enter(&nig->nfsidmap_daemon_lock);
7207c478bd9Sstevel@tonic-gate 	dh = nig->nfsidmap_daemon_dh;
7217c478bd9Sstevel@tonic-gate 	if (dh)
7227c478bd9Sstevel@tonic-gate 		door_ki_hold(dh);
7237c478bd9Sstevel@tonic-gate 	mutex_exit(&nig->nfsidmap_daemon_lock);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (dh == NULL || nig->nfsidmap_pid == curproc->p_pid ||
7267c478bd9Sstevel@tonic-gate 	    (!utf8_strchr(u8s, '@') && !isserver)) {
7277c478bd9Sstevel@tonic-gate 		if (dh)
7287c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
7297c478bd9Sstevel@tonic-gate 		error = nfs_idmap_s2i_literal(u8s, gid, isserver);
7307c478bd9Sstevel@tonic-gate 		/*
7317c478bd9Sstevel@tonic-gate 		 * If we get a numeric value, but we only do so because
7327c478bd9Sstevel@tonic-gate 		 * we are nfsmapid, return ENOTSUP to indicate a valid
7337c478bd9Sstevel@tonic-gate 		 * response, but not to cache it.
7347c478bd9Sstevel@tonic-gate 		 */
7357c478bd9Sstevel@tonic-gate 		if (!error && nig->nfsidmap_pid == curproc->p_pid)
7367c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
7377c478bd9Sstevel@tonic-gate 		return (error);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/* cache hit */
7417c478bd9Sstevel@tonic-gate 	if (nfs_idmap_cache_s2i_lkup(&nig->s2g_ci, u8s, &hashno, gid)) {
7427c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
7437c478bd9Sstevel@tonic-gate 		return (0);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/* cache miss */
7477c478bd9Sstevel@tonic-gate 	mapargp = kmem_alloc(MAPID_ARG_LEN(u8s->utf8string_len), KM_SLEEP);
7487c478bd9Sstevel@tonic-gate 	mapargp->cmd = NFSMAPID_STR_GID;
7497c478bd9Sstevel@tonic-gate 	mapargp->u_arg.len = u8s->utf8string_len;
7507c478bd9Sstevel@tonic-gate 	(void) bcopy(u8s->utf8string_val, mapargp->str, mapargp->u_arg.len);
7517c478bd9Sstevel@tonic-gate 	mapargp->str[mapargp->u_arg.len] = '\0';
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	door_args.data_ptr = (char *)mapargp;
7547c478bd9Sstevel@tonic-gate 	door_args.data_size = MAPID_ARG_LEN(mapargp->u_arg.len);
7557c478bd9Sstevel@tonic-gate 	door_args.desc_ptr = NULL;
7567c478bd9Sstevel@tonic-gate 	door_args.desc_num = 0;
7577c478bd9Sstevel@tonic-gate 	door_args.rbuf = (char *)mapresp;
7587c478bd9Sstevel@tonic-gate 	door_args.rsize = sizeof (struct mapid_res);
7597c478bd9Sstevel@tonic-gate 
760323a81d9Sjwadams 	error = door_ki_upcall_limited(dh, &door_args, NULL, SIZE_MAX, 0);
7617c478bd9Sstevel@tonic-gate 	if (!error) {
7627c478bd9Sstevel@tonic-gate 		resp = (struct mapid_res *)door_args.rbuf;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		/* Should never provide daemon with bad args */
7657c478bd9Sstevel@tonic-gate 		ASSERT(resp->status != NFSMAPID_INVALID);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		switch (resp->status) {
7687c478bd9Sstevel@tonic-gate 		case NFSMAPID_OK:
7697c478bd9Sstevel@tonic-gate 			/*
7707c478bd9Sstevel@tonic-gate 			 * Valid mapping. Cache it.
7717c478bd9Sstevel@tonic-gate 			 */
7727c478bd9Sstevel@tonic-gate 			*gid = resp->u_res.gid;
7737c478bd9Sstevel@tonic-gate 			error = 0;
7747c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_s2i_insert(&nig->s2g_ci, *gid,
7757c478bd9Sstevel@tonic-gate 			    u8s, HQ_HASH_HINT, hashno);
7767c478bd9Sstevel@tonic-gate 			break;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		case NFSMAPID_NUMSTR:
7797c478bd9Sstevel@tonic-gate 			/*
7807c478bd9Sstevel@tonic-gate 			 * string came in as stringified id. Don't cache !
7817c478bd9Sstevel@tonic-gate 			 *
7827c478bd9Sstevel@tonic-gate 			 * nfsmapid(1m) semantics have changed in order to
7837c478bd9Sstevel@tonic-gate 			 * support diskless clients. Thus, for stringified
7847c478bd9Sstevel@tonic-gate 			 * id's that have passwd/group entries, we'll go
7857c478bd9Sstevel@tonic-gate 			 * ahead and map them, returning no error.
7867c478bd9Sstevel@tonic-gate 			 */
7877c478bd9Sstevel@tonic-gate 			*gid = resp->u_res.gid;
7887c478bd9Sstevel@tonic-gate 			break;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADDOMAIN:
7917c478bd9Sstevel@tonic-gate 			/*
7927c478bd9Sstevel@tonic-gate 			 * Make the offending "group@domain" string readily
7937c478bd9Sstevel@tonic-gate 			 * available to D scripts that enable the probe.
7947c478bd9Sstevel@tonic-gate 			 */
7957c478bd9Sstevel@tonic-gate 			DTRACE_PROBE1(nfs4__str__gid, char *, mapargp->str);
7967c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		case NFSMAPID_INVALID:
7997c478bd9Sstevel@tonic-gate 		case NFSMAPID_UNMAPPABLE:
8007c478bd9Sstevel@tonic-gate 		case NFSMAPID_INTERNAL:
8017c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADID:
8027c478bd9Sstevel@tonic-gate 		case NFSMAPID_NOTFOUND:
8037c478bd9Sstevel@tonic-gate 		default:
8047c478bd9Sstevel@tonic-gate 			/*
8057c478bd9Sstevel@tonic-gate 			 * For now, treat all of these errors as equal.
8067c478bd9Sstevel@tonic-gate 			 *
8077c478bd9Sstevel@tonic-gate 			 * Return error on the server side, then the
8087c478bd9Sstevel@tonic-gate 			 * server returns NFS4_BADOWNER to the client.
8097c478bd9Sstevel@tonic-gate 			 * On client side, just map to GID_NOBODY.
8107c478bd9Sstevel@tonic-gate 			 */
8117c478bd9Sstevel@tonic-gate 			if (isserver)
8127c478bd9Sstevel@tonic-gate 				error = EPERM;
8137c478bd9Sstevel@tonic-gate 			else
8147c478bd9Sstevel@tonic-gate 				*gid = GID_NOBODY;
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		kmem_free(mapargp, MAPID_ARG_LEN(u8s->utf8string_len));
8187c478bd9Sstevel@tonic-gate 		if (resp != mapresp)
8197c478bd9Sstevel@tonic-gate 			kmem_free(door_args.rbuf, door_args.rsize);
8207c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
8217c478bd9Sstevel@tonic-gate 		return (error);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	kmem_free(mapargp, MAPID_ARG_LEN(u8s->utf8string_len));
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * We got some door error
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	switch (error) {
8297c478bd9Sstevel@tonic-gate 	case EINTR:
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * If we took an interrupt we have to bail out.
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		if (ttolwp(curthread) && ISSIG(curthread, JUSTLOOKING)) {
8347c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
8357c478bd9Sstevel@tonic-gate 			return (EINTR);
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		/*
8397c478bd9Sstevel@tonic-gate 		 * We may have gotten EINTR for other reasons like the
8407c478bd9Sstevel@tonic-gate 		 * door being revoked on us, instead of trying to
8417c478bd9Sstevel@tonic-gate 		 * extract this out of the door handle, sleep
8427c478bd9Sstevel@tonic-gate 		 * and try again, if still revoked we will get EBADF
8437c478bd9Sstevel@tonic-gate 		 * next time through.
8447c478bd9Sstevel@tonic-gate 		 */
8457c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
8467c478bd9Sstevel@tonic-gate 	case EAGAIN:    /* process may be forking */
8477c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
8487c478bd9Sstevel@tonic-gate 		/*
8497c478bd9Sstevel@tonic-gate 		 * Back off for a bit
8507c478bd9Sstevel@tonic-gate 		 */
8517c478bd9Sstevel@tonic-gate 		delay(hz);
8527c478bd9Sstevel@tonic-gate 		goto retry;
8537c478bd9Sstevel@tonic-gate 	default:	/* Unknown must be fatal */
8547c478bd9Sstevel@tonic-gate 	case EBADF:	/* Invalid door */
8557c478bd9Sstevel@tonic-gate 	case EINVAL:	/* Not a door, wrong target */
8567c478bd9Sstevel@tonic-gate 		/*
8577c478bd9Sstevel@tonic-gate 		 * A fatal door error, clean up our state and
8587c478bd9Sstevel@tonic-gate 		 * mark the server dead.
8597c478bd9Sstevel@tonic-gate 		 */
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		mutex_enter(&nig->nfsidmap_daemon_lock);
8627c478bd9Sstevel@tonic-gate 		if (dh == nig->nfsidmap_daemon_dh) {
8637c478bd9Sstevel@tonic-gate 			door_ki_rele(nig->nfsidmap_daemon_dh);
8647c478bd9Sstevel@tonic-gate 			nig->nfsidmap_daemon_dh = NULL;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 		mutex_exit(&nig->nfsidmap_daemon_lock);
8677c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		if (isserver)
8707c478bd9Sstevel@tonic-gate 			return (ECOMM);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		/*
8737c478bd9Sstevel@tonic-gate 		 * Note: We've already done optimizations above to check
8747c478bd9Sstevel@tonic-gate 		 *	 for '@' sign, so if we can't comm w/nfsmapid, we
8757c478bd9Sstevel@tonic-gate 		 *	 _know_ this _can't_ be a stringified gid.
8767c478bd9Sstevel@tonic-gate 		 */
8777c478bd9Sstevel@tonic-gate 		if (!nig->nig_msg_done) {
8787c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
8797c478bd9Sstevel@tonic-gate 			    "!%s: Can't communicate with mapping daemon "
8807c478bd9Sstevel@tonic-gate 			    "nfsmapid", whoami);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			nig->nig_msg_done = 1;
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 		*gid = GID_NOBODY;
8857c478bd9Sstevel@tonic-gate 		return (0);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Convert a gid into its utf-8 string representation.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate int
8947c478bd9Sstevel@tonic-gate nfs_idmap_gid_str(gid_t gid, utf8string *u8s, bool_t isserver)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	int			error;
8977c478bd9Sstevel@tonic-gate 	uint_t			hashno = 0;
8987c478bd9Sstevel@tonic-gate 	const char		*whoami = "nfs_idmap_gid_str";
8997c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals	*nig;
9007c478bd9Sstevel@tonic-gate 	struct mapid_arg	maparg;
9017c478bd9Sstevel@tonic-gate 	struct mapid_res	mapres;
9027c478bd9Sstevel@tonic-gate 	struct mapid_res	*mapresp = &mapres;
9037c478bd9Sstevel@tonic-gate 	struct mapid_res	*resp = mapresp;
9047c478bd9Sstevel@tonic-gate 	door_arg_t		door_args;
9057c478bd9Sstevel@tonic-gate 	door_handle_t		dh;
9067c478bd9Sstevel@tonic-gate 
907108322fbScarlsonj 	nig = zone_getspecific(nfsidmap_zone_key, nfs_zone());
9087c478bd9Sstevel@tonic-gate 	ASSERT(nig != NULL);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * If the supplied gid is "nobody", then we don't look at the
9127c478bd9Sstevel@tonic-gate 	 * cache, since we DON'T cache it in the u2s_cache. We cannot
9137c478bd9Sstevel@tonic-gate 	 * tell two strings apart from caching the same gid.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	if (gid == GID_NOBODY) {
9167c478bd9Sstevel@tonic-gate 		(void) str_to_utf8("nobody", u8s);
9177c478bd9Sstevel@tonic-gate 		return (0);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * Start-off with upcalls disabled, and once nfsmapid(1m) is
9227c478bd9Sstevel@tonic-gate 	 * up and running, we'll leverage it's first flush to let the
9237c478bd9Sstevel@tonic-gate 	 * kernel know when it's up and available to perform mappings.
9247c478bd9Sstevel@tonic-gate 	 * We fall back to answering with stringified gid's.
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate retry:
9277c478bd9Sstevel@tonic-gate 	mutex_enter(&nig->nfsidmap_daemon_lock);
9287c478bd9Sstevel@tonic-gate 	dh = nig->nfsidmap_daemon_dh;
9297c478bd9Sstevel@tonic-gate 	if (dh)
9307c478bd9Sstevel@tonic-gate 		door_ki_hold(dh);
9317c478bd9Sstevel@tonic-gate 	mutex_exit(&nig->nfsidmap_daemon_lock);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (dh == NULL || nig->nfsidmap_pid == curproc->p_pid) {
9347c478bd9Sstevel@tonic-gate 		if (dh)
9357c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
9367c478bd9Sstevel@tonic-gate 		nfs_idmap_i2s_literal(gid, u8s);
9377c478bd9Sstevel@tonic-gate 		return (0);
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/* cache hit */
9417c478bd9Sstevel@tonic-gate 	if (nfs_idmap_cache_i2s_lkup(&nig->g2s_ci, gid, &hashno, u8s)) {
9427c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
9437c478bd9Sstevel@tonic-gate 		return (0);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/* cache miss */
9477c478bd9Sstevel@tonic-gate 	maparg.cmd = NFSMAPID_GID_STR;
9487c478bd9Sstevel@tonic-gate 	maparg.u_arg.gid = gid;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	door_args.data_ptr = (char *)&maparg;
9517c478bd9Sstevel@tonic-gate 	door_args.data_size = sizeof (struct mapid_arg);
9527c478bd9Sstevel@tonic-gate 	door_args.desc_ptr = NULL;
9537c478bd9Sstevel@tonic-gate 	door_args.desc_num = 0;
9547c478bd9Sstevel@tonic-gate 	door_args.rbuf = (char *)mapresp;
9557c478bd9Sstevel@tonic-gate 	door_args.rsize = sizeof (struct mapid_res);
9567c478bd9Sstevel@tonic-gate 
957323a81d9Sjwadams 	error = door_ki_upcall_limited(dh, &door_args, NULL, SIZE_MAX, 0);
9587c478bd9Sstevel@tonic-gate 	if (!error) {
9597c478bd9Sstevel@tonic-gate 		resp = (struct mapid_res *)door_args.rbuf;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		/* Should never provide daemon with bad args */
9627c478bd9Sstevel@tonic-gate 		ASSERT(resp->status != NFSMAPID_INVALID);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 		switch (resp->status) {
9657c478bd9Sstevel@tonic-gate 		case NFSMAPID_OK:
9667c478bd9Sstevel@tonic-gate 			/*
9677c478bd9Sstevel@tonic-gate 			 * We now have a valid result from the
9687c478bd9Sstevel@tonic-gate 			 * user-land daemon, so cache the result (if need be).
9697c478bd9Sstevel@tonic-gate 			 * Load return value first then do the caches.
9707c478bd9Sstevel@tonic-gate 			 */
9717c478bd9Sstevel@tonic-gate 			(void) str_to_utf8(resp->str, u8s);
9727c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_i2s_insert(&nig->g2s_ci, gid,
9737c478bd9Sstevel@tonic-gate 			    u8s, HQ_HASH_HINT, hashno);
9747c478bd9Sstevel@tonic-gate 			break;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		case NFSMAPID_INVALID:
9777c478bd9Sstevel@tonic-gate 		case NFSMAPID_UNMAPPABLE:
9787c478bd9Sstevel@tonic-gate 		case NFSMAPID_INTERNAL:
9797c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADDOMAIN:
9807c478bd9Sstevel@tonic-gate 		case NFSMAPID_BADID:
9817c478bd9Sstevel@tonic-gate 		case NFSMAPID_NOTFOUND:
9827c478bd9Sstevel@tonic-gate 		default:
9837c478bd9Sstevel@tonic-gate 			/*
9847c478bd9Sstevel@tonic-gate 			 * For now, treat all of these errors as equal.
9857c478bd9Sstevel@tonic-gate 			 */
9867c478bd9Sstevel@tonic-gate 			error = EPERM;
9877c478bd9Sstevel@tonic-gate 			break;
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		if (resp != mapresp)
9917c478bd9Sstevel@tonic-gate 			kmem_free(door_args.rbuf, door_args.rsize);
9927c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
9937c478bd9Sstevel@tonic-gate 		return (error);
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * We got some door error
9987c478bd9Sstevel@tonic-gate 	 */
9997c478bd9Sstevel@tonic-gate 	switch (error) {
10007c478bd9Sstevel@tonic-gate 	case EINTR:
10017c478bd9Sstevel@tonic-gate 		/*
10027c478bd9Sstevel@tonic-gate 		 * If we took an interrupt we have to bail out.
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if (ttolwp(curthread) && ISSIG(curthread, JUSTLOOKING)) {
10057c478bd9Sstevel@tonic-gate 			door_ki_rele(dh);
10067c478bd9Sstevel@tonic-gate 			return (EINTR);
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		/*
10107c478bd9Sstevel@tonic-gate 		 * We may have gotten EINTR for other reasons like the
10117c478bd9Sstevel@tonic-gate 		 * door being revoked on us, instead of trying to
10127c478bd9Sstevel@tonic-gate 		 * extract this out of the door handle, sleep
10137c478bd9Sstevel@tonic-gate 		 * and try again, if still revoked we will get EBADF
10147c478bd9Sstevel@tonic-gate 		 * next time through.
10157c478bd9Sstevel@tonic-gate 		 */
10167c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
10177c478bd9Sstevel@tonic-gate 	case EAGAIN:    /* process may be forking */
10187c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
10197c478bd9Sstevel@tonic-gate 		/*
10207c478bd9Sstevel@tonic-gate 		 * Back off for a bit
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 		delay(hz);
10237c478bd9Sstevel@tonic-gate 		goto retry;
10247c478bd9Sstevel@tonic-gate 	default:	/* Unknown must be fatal */
10257c478bd9Sstevel@tonic-gate 	case EBADF:	/* Invalid door */
10267c478bd9Sstevel@tonic-gate 	case EINVAL:	/* Not a door, wrong target */
10277c478bd9Sstevel@tonic-gate 		/*
10287c478bd9Sstevel@tonic-gate 		 * A fatal door error, if our failing door handle is the
10297c478bd9Sstevel@tonic-gate 		 * current door handle, clean up our state and
10307c478bd9Sstevel@tonic-gate 		 * mark the server dead.
10317c478bd9Sstevel@tonic-gate 		 */
10327c478bd9Sstevel@tonic-gate 		mutex_enter(&nig->nfsidmap_daemon_lock);
10337c478bd9Sstevel@tonic-gate 		if (dh == nig->nfsidmap_daemon_dh) {
10347c478bd9Sstevel@tonic-gate 			door_ki_rele(nig->nfsidmap_daemon_dh);
10357c478bd9Sstevel@tonic-gate 			nig->nfsidmap_daemon_dh = NULL;
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 		door_ki_rele(dh);
10387c478bd9Sstevel@tonic-gate 		mutex_exit(&nig->nfsidmap_daemon_lock);
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		/*
10417c478bd9Sstevel@tonic-gate 		 * Log error on client-side only
10427c478bd9Sstevel@tonic-gate 		 */
10437c478bd9Sstevel@tonic-gate 		if (!nig->nig_msg_done && !isserver) {
10447c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
10457c478bd9Sstevel@tonic-gate 			    "!%s: Can't communicate with mapping daemon "
10467c478bd9Sstevel@tonic-gate 			    "nfsmapid", whoami);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			nig->nig_msg_done = 1;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		nfs_idmap_i2s_literal(gid, u8s);
10517c478bd9Sstevel@tonic-gate 		return (0);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /* -- idmap cache management -- */
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate  * Cache creation and initialization routine
10607c478bd9Sstevel@tonic-gate  */
10617c478bd9Sstevel@tonic-gate static void
10627c478bd9Sstevel@tonic-gate nfs_idmap_cache_create(idmap_cache_info_t *cip, const char *name)
10637c478bd9Sstevel@tonic-gate {
10647c478bd9Sstevel@tonic-gate 	int		 i;
10657c478bd9Sstevel@tonic-gate 	nfsidhq_t	*hq = NULL;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	cip->table = kmem_alloc((NFSID_CACHE_ANCHORS * sizeof (nfsidhq_t)),
10687c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	for (i = 0, hq = cip->table; i < NFSID_CACHE_ANCHORS; i++, hq++) {
10717c478bd9Sstevel@tonic-gate 		hq->hq_que_forw = hq;
10727c478bd9Sstevel@tonic-gate 		hq->hq_que_back = hq;
10737c478bd9Sstevel@tonic-gate 		mutex_init(&(hq->hq_lock), NULL, MUTEX_DEFAULT, NULL);
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 	cip->name = name;
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate  * Cache destruction routine
10807c478bd9Sstevel@tonic-gate  *
10817c478bd9Sstevel@tonic-gate  * Ops per hash queue
10827c478bd9Sstevel@tonic-gate  *
10837c478bd9Sstevel@tonic-gate  * - dequeue cache entries
10847c478bd9Sstevel@tonic-gate  * - release string storage per entry
10857c478bd9Sstevel@tonic-gate  * - release cache entry storage
10867c478bd9Sstevel@tonic-gate  * - destroy HQ lock when HQ is empty
10877c478bd9Sstevel@tonic-gate  * - once all HQ's empty, release HQ storage
10887c478bd9Sstevel@tonic-gate  */
10897c478bd9Sstevel@tonic-gate static void
10907c478bd9Sstevel@tonic-gate nfs_idmap_cache_destroy(idmap_cache_info_t *cip)
10917c478bd9Sstevel@tonic-gate {
10927c478bd9Sstevel@tonic-gate 	int		 i;
10937c478bd9Sstevel@tonic-gate 	nfsidhq_t	*hq;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&nfsidmap_globals_lock));
10967c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_flush(cip);
10977c478bd9Sstevel@tonic-gate 	/*
10987c478bd9Sstevel@tonic-gate 	 * We can safely destroy per-queue locks since the only
10997c478bd9Sstevel@tonic-gate 	 * other entity that could be mucking with this table is the
11007c478bd9Sstevel@tonic-gate 	 * kmem reaper thread which does everything under
11017c478bd9Sstevel@tonic-gate 	 * nfsidmap_globals_lock (which we're holding).
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 	for (i = 0, hq = cip->table; i < NFSID_CACHE_ANCHORS; i++, hq++)
11047c478bd9Sstevel@tonic-gate 		mutex_destroy(&(hq->hq_lock));
11057c478bd9Sstevel@tonic-gate 	kmem_free(cip->table, NFSID_CACHE_ANCHORS * sizeof (nfsidhq_t));
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate void
11097c478bd9Sstevel@tonic-gate nfs_idmap_args(struct nfsidmap_args *idmp)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig;
11127c478bd9Sstevel@tonic-gate 
1113108322fbScarlsonj 	nig = zone_getspecific(nfsidmap_zone_key, nfs_zone());
11147c478bd9Sstevel@tonic-gate 	ASSERT(nig != NULL);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_flush(&nig->u2s_ci);
11177c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_flush(&nig->s2u_ci);
11187c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_flush(&nig->g2s_ci);
11197c478bd9Sstevel@tonic-gate 	nfs_idmap_cache_flush(&nig->s2g_ci);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/*
11227c478bd9Sstevel@tonic-gate 	 * nfsmapid(1m) up and running; enable upcalls
11237c478bd9Sstevel@tonic-gate 	 * State:
11247c478bd9Sstevel@tonic-gate 	 *	0	Just flush caches
11257c478bd9Sstevel@tonic-gate 	 *	1	Re-establish door knob
11267c478bd9Sstevel@tonic-gate 	 */
11277c478bd9Sstevel@tonic-gate 	if (idmp->state) {
11287c478bd9Sstevel@tonic-gate 		/*
11297c478bd9Sstevel@tonic-gate 		 * When reestablishing the nfsmapid we need to
11307c478bd9Sstevel@tonic-gate 		 * not only purge the idmap cache but also
11317c478bd9Sstevel@tonic-gate 		 * the dnlc as it will have cached uid/gid's.
11327c478bd9Sstevel@tonic-gate 		 * While heavyweight, this should almost never happen
11337c478bd9Sstevel@tonic-gate 		 */
11347c478bd9Sstevel@tonic-gate 		dnlc_purge();
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		/*
11377c478bd9Sstevel@tonic-gate 		 * Invalidate the attrs of all rnodes to force new uid and gids
11387c478bd9Sstevel@tonic-gate 		 */
11397c478bd9Sstevel@tonic-gate 		nfs4_rnode_invalidate(NULL);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		mutex_enter(&nig->nfsidmap_daemon_lock);
11427c478bd9Sstevel@tonic-gate 		if (nig->nfsidmap_daemon_dh)
11437c478bd9Sstevel@tonic-gate 			door_ki_rele(nig->nfsidmap_daemon_dh);
11447c478bd9Sstevel@tonic-gate 		nig->nfsidmap_daemon_dh = door_ki_lookup(idmp->did);
11457c478bd9Sstevel@tonic-gate 		nig->nfsidmap_pid = curproc->p_pid;
11467c478bd9Sstevel@tonic-gate 		nig->nig_msg_done = 0;
11477c478bd9Sstevel@tonic-gate 		mutex_exit(&nig->nfsidmap_daemon_lock);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate  * Cache flush routine
11537c478bd9Sstevel@tonic-gate  *
11547c478bd9Sstevel@tonic-gate  *	The only serialization required it to hold the hash chain lock
11557c478bd9Sstevel@tonic-gate  *	when destroying cache entries.  There is no need to prevent access
11567c478bd9Sstevel@tonic-gate  *	to all hash chains while flushing.  It is possible that (valid)
11577c478bd9Sstevel@tonic-gate  *	entries could be cached in later hash chains after we start flushing.
11587c478bd9Sstevel@tonic-gate  *	It is unfortunate that the entry will be instantly destroyed, but
11597c478bd9Sstevel@tonic-gate  *	it isn't a major concern.  This is only a cache.  It'll be repopulated.
11607c478bd9Sstevel@tonic-gate  *
11617c478bd9Sstevel@tonic-gate  * Ops per hash queue
11627c478bd9Sstevel@tonic-gate  *
11637c478bd9Sstevel@tonic-gate  * - dequeue cache entries
11647c478bd9Sstevel@tonic-gate  * - release string storage per entry
11657c478bd9Sstevel@tonic-gate  * - release cache entry storage
11667c478bd9Sstevel@tonic-gate  */
11677c478bd9Sstevel@tonic-gate static void
11687c478bd9Sstevel@tonic-gate nfs_idmap_cache_flush(idmap_cache_info_t *cip)
11697c478bd9Sstevel@tonic-gate {
11707c478bd9Sstevel@tonic-gate 	int		 i;
11717c478bd9Sstevel@tonic-gate 	nfsidmap_t	*p, *next;
11727c478bd9Sstevel@tonic-gate 	nfsidhq_t	*hq;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	for (i = 0, hq = cip->table; i < NFSID_CACHE_ANCHORS; i++, hq++) {
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		mutex_enter(&(hq->hq_lock));
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/*
11797c478bd9Sstevel@tonic-gate 		 * remove list from hash header so we can release
11807c478bd9Sstevel@tonic-gate 		 * the lock early.
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		p = hq->hq_lru_forw;
11837c478bd9Sstevel@tonic-gate 		hq->hq_que_forw = hq;
11847c478bd9Sstevel@tonic-gate 		hq->hq_que_back = hq;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		mutex_exit(&(hq->hq_lock));
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 		/*
11897c478bd9Sstevel@tonic-gate 		 * Iterate over the orphan'd list and free all elements.
11907c478bd9Sstevel@tonic-gate 		 * There's no need to bother with remque since we're
11917c478bd9Sstevel@tonic-gate 		 * freeing the entire list.
11927c478bd9Sstevel@tonic-gate 		 */
11937c478bd9Sstevel@tonic-gate 		while (p != (nfsidmap_t *)hq) {
11947c478bd9Sstevel@tonic-gate 			next = p->id_forw;
11957c478bd9Sstevel@tonic-gate 			if (p->id_val != 0)
11967c478bd9Sstevel@tonic-gate 				kmem_free(p->id_val, p->id_len);
11977c478bd9Sstevel@tonic-gate 			kmem_cache_free(nfsidmap_cache, p);
11987c478bd9Sstevel@tonic-gate 			p = next;
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate static void
12057c478bd9Sstevel@tonic-gate nfs_idmap_cache_reclaim(idmap_cache_info_t *cip)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	nfsidhq_t		*hq;
12087c478bd9Sstevel@tonic-gate 	nfsidmap_t		*pprev = NULL;
12097c478bd9Sstevel@tonic-gate 	int			 i;
12107c478bd9Sstevel@tonic-gate 	nfsidmap_t		*p;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	ASSERT(cip != NULL && cip->table != NULL);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/*
12157c478bd9Sstevel@tonic-gate 	 * If the daemon is down, do not flush anything
12167c478bd9Sstevel@tonic-gate 	 */
12177c478bd9Sstevel@tonic-gate 	if ((*cip->nfsidmap_daemon_dh) == NULL)
12187c478bd9Sstevel@tonic-gate 		return;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	for (i = 0, hq = cip->table; i < NFSID_CACHE_ANCHORS; i++, hq++) {
12217c478bd9Sstevel@tonic-gate 		if (!mutex_tryenter(&(hq->hq_lock)))
12227c478bd9Sstevel@tonic-gate 			continue;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		/*
12257c478bd9Sstevel@tonic-gate 		 * Start at end of list and work backwards since LRU
12267c478bd9Sstevel@tonic-gate 		 */
12277c478bd9Sstevel@tonic-gate 		for (p = hq->hq_lru_back; p != (nfsidmap_t *)hq; p = pprev) {
12287c478bd9Sstevel@tonic-gate 			pprev = p->id_back;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 			/*
12317c478bd9Sstevel@tonic-gate 			 * List is LRU. If trailing end does not
12327c478bd9Sstevel@tonic-gate 			 * contain stale entries, then no need to
12337c478bd9Sstevel@tonic-gate 			 * continue.
12347c478bd9Sstevel@tonic-gate 			 */
12357c478bd9Sstevel@tonic-gate 			if (!TIMEOUT(p->id_time))
12367c478bd9Sstevel@tonic-gate 				break;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_rment(p);
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		mutex_exit(&(hq->hq_lock));
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate /*
12457c478bd9Sstevel@tonic-gate  * Callback reclaim function for VM.  We reap timed-out entries from all hash
12467c478bd9Sstevel@tonic-gate  * tables in all zones.
12477c478bd9Sstevel@tonic-gate  */
12487c478bd9Sstevel@tonic-gate /* ARGSUSED */
12497c478bd9Sstevel@tonic-gate void
12507c478bd9Sstevel@tonic-gate nfs_idmap_reclaim(void *arg)
12517c478bd9Sstevel@tonic-gate {
12527c478bd9Sstevel@tonic-gate 	struct nfsidmap_globals *nig;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	mutex_enter(&nfsidmap_globals_lock);
12557c478bd9Sstevel@tonic-gate 	for (nig = list_head(&nfsidmap_globals_list); nig != NULL;
12567c478bd9Sstevel@tonic-gate 	    nig = list_next(&nfsidmap_globals_list, nig)) {
12577c478bd9Sstevel@tonic-gate 		nfs_idmap_cache_reclaim(&nig->u2s_ci);
12587c478bd9Sstevel@tonic-gate 		nfs_idmap_cache_reclaim(&nig->s2u_ci);
12597c478bd9Sstevel@tonic-gate 		nfs_idmap_cache_reclaim(&nig->g2s_ci);
12607c478bd9Sstevel@tonic-gate 		nfs_idmap_cache_reclaim(&nig->s2g_ci);
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 	mutex_exit(&nfsidmap_globals_lock);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate /*
12667c478bd9Sstevel@tonic-gate  * Search the specified cache for the existence of the specified utf-8
12677c478bd9Sstevel@tonic-gate  * string. If found, the corresponding mapping is returned in id_buf and
12687c478bd9Sstevel@tonic-gate  * the cache entry is updated to the head of the LRU list. The computed
12697c478bd9Sstevel@tonic-gate  * hash queue number, is returned in hashno.
12707c478bd9Sstevel@tonic-gate  */
12717c478bd9Sstevel@tonic-gate static uint_t
12727c478bd9Sstevel@tonic-gate nfs_idmap_cache_s2i_lkup(idmap_cache_info_t *cip,	/* cache info ptr */
12737c478bd9Sstevel@tonic-gate 			utf8string *u8s,	/* utf8 string to resolve */
12747c478bd9Sstevel@tonic-gate 			uint_t *hashno,		/* hash number, retval */
12757c478bd9Sstevel@tonic-gate 			uid_t *id_buf)		/* if found, id for u8s */
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	nfsidmap_t	*p;
12787c478bd9Sstevel@tonic-gate 	nfsidmap_t	*pnext;
12797c478bd9Sstevel@tonic-gate 	nfsidhq_t	*hq;
12807c478bd9Sstevel@tonic-gate 	char		*rqst_c_str;
12817c478bd9Sstevel@tonic-gate 	uint_t		 rqst_len;
12827c478bd9Sstevel@tonic-gate 	uint_t		 found_stat = 0;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	if ((rqst_c_str = utf8_to_str(u8s, &rqst_len, NULL)) == NULL) {
12857c478bd9Sstevel@tonic-gate 		/*
12867c478bd9Sstevel@tonic-gate 		 * Illegal string, return not found.
12877c478bd9Sstevel@tonic-gate 		 */
12887c478bd9Sstevel@tonic-gate 		return (0);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/*
12927c478bd9Sstevel@tonic-gate 	 * Compute hash queue
12937c478bd9Sstevel@tonic-gate 	 */
1294*0616fd7fSPavel Filipensky 	*hashno = pkp_tab_hash(rqst_c_str, rqst_len - 1);
1295*0616fd7fSPavel Filipensky 	hq = &cip->table[*hashno];
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * Look for the entry in the HQ
12997c478bd9Sstevel@tonic-gate 	 */
13007c478bd9Sstevel@tonic-gate 	mutex_enter(&(hq->hq_lock));
13017c478bd9Sstevel@tonic-gate 	for (p = hq->hq_lru_forw; p != (nfsidmap_t *)hq; p = pnext) {
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 		pnext = p->id_forw;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		/*
13067c478bd9Sstevel@tonic-gate 		 * Check entry for staleness first, as user's id
13077c478bd9Sstevel@tonic-gate 		 * may have changed and may need to be remapped.
13087c478bd9Sstevel@tonic-gate 		 * Note that we don't evict entries from the cache
13097c478bd9Sstevel@tonic-gate 		 * if we're having trouble contacting nfsmapid(1m)
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		if (TIMEOUT(p->id_time) && (*cip->nfsidmap_daemon_dh) != NULL) {
13127c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_rment(p);
13137c478bd9Sstevel@tonic-gate 			continue;
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * Compare equal length strings
13187c478bd9Sstevel@tonic-gate 		 */
13197c478bd9Sstevel@tonic-gate 		if (p->id_len == (rqst_len - 1)) {
13207c478bd9Sstevel@tonic-gate 			if (bcmp(p->id_val, rqst_c_str, (rqst_len - 1)) == 0) {
13217c478bd9Sstevel@tonic-gate 				/*
13227c478bd9Sstevel@tonic-gate 				 * Found it. Update it and load return value.
13237c478bd9Sstevel@tonic-gate 				 */
13247c478bd9Sstevel@tonic-gate 				*id_buf = p->id_no;
13257c478bd9Sstevel@tonic-gate 				remque(p);
13267c478bd9Sstevel@tonic-gate 				insque(p, hq);
13277c478bd9Sstevel@tonic-gate 				p->id_time = gethrestime_sec();
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 				found_stat = 1;
13307c478bd9Sstevel@tonic-gate 				break;
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 	mutex_exit(&(hq->hq_lock));
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if (rqst_c_str != NULL)
13377c478bd9Sstevel@tonic-gate 		kmem_free(rqst_c_str, rqst_len);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	return (found_stat);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Search the specified cache for the existence of the specified utf8
13447c478bd9Sstevel@tonic-gate  * string, as it may have been inserted before this instance got a chance
13457c478bd9Sstevel@tonic-gate  * to do it. If NOT found, then a new entry is allocated for the specified
13467c478bd9Sstevel@tonic-gate  * cache, and inserted. The hash queue number is obtained from hash_number
13477c478bd9Sstevel@tonic-gate  * if the behavior is HQ_HASH_HINT, or computed otherwise.
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate static void
13507c478bd9Sstevel@tonic-gate nfs_idmap_cache_s2i_insert(idmap_cache_info_t *cip,	/* cache info ptr */
13517c478bd9Sstevel@tonic-gate 			uid_t id,		/* id result from upcall */
13527c478bd9Sstevel@tonic-gate 			utf8string *u8s,	/* utf8 string to resolve */
13537c478bd9Sstevel@tonic-gate 			hash_stat behavior,	/* hash algorithm behavior */
13547c478bd9Sstevel@tonic-gate 			uint_t hash_number)	/* hash number iff hint */
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	uint_t			 hashno;
13577c478bd9Sstevel@tonic-gate 	char			*c_str;
13587c478bd9Sstevel@tonic-gate 	nfsidhq_t		*hq;
13597c478bd9Sstevel@tonic-gate 	nfsidmap_t		*newp;
13607c478bd9Sstevel@tonic-gate 	nfsidmap_t		*p;
13617c478bd9Sstevel@tonic-gate 	nfsidmap_t		*pnext;
13627c478bd9Sstevel@tonic-gate 	uint_t			 c_len;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	/*
13657c478bd9Sstevel@tonic-gate 	 * This shouldn't fail, since already successful at lkup.
13667c478bd9Sstevel@tonic-gate 	 * So, if it does happen, just drop the request-to-insert
13677c478bd9Sstevel@tonic-gate 	 * on the floor.
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	if ((c_str = utf8_to_str(u8s, &c_len, NULL)) == NULL)
13707c478bd9Sstevel@tonic-gate 		return;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/*
13737c478bd9Sstevel@tonic-gate 	 * Obtain correct hash queue to insert new entry in
13747c478bd9Sstevel@tonic-gate 	 */
13757c478bd9Sstevel@tonic-gate 	switch (behavior) {
13767c478bd9Sstevel@tonic-gate 		case HQ_HASH_HINT:
13777c478bd9Sstevel@tonic-gate 			hashno = hash_number;
13787c478bd9Sstevel@tonic-gate 			break;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		case HQ_HASH_FIND:
13817c478bd9Sstevel@tonic-gate 		default:
1382*0616fd7fSPavel Filipensky 			hashno = pkp_tab_hash(c_str, c_len - 1);
13837c478bd9Sstevel@tonic-gate 			break;
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 	hq = &cip->table[hashno];
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/*
13897c478bd9Sstevel@tonic-gate 	 * Look for an existing entry in the cache. If one exists
13907c478bd9Sstevel@tonic-gate 	 * update it, and return. Otherwise, allocate a new cache
13917c478bd9Sstevel@tonic-gate 	 * entry, initialize it and insert it.
13927c478bd9Sstevel@tonic-gate 	 */
13937c478bd9Sstevel@tonic-gate 	mutex_enter(&(hq->hq_lock));
13947c478bd9Sstevel@tonic-gate 	for (p = hq->hq_lru_forw; p != (nfsidmap_t *)hq; p = pnext) {
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		pnext = p->id_forw;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 		/*
13997c478bd9Sstevel@tonic-gate 		 * Check entry for staleness first, as user's id
14007c478bd9Sstevel@tonic-gate 		 * may have changed and may need to be remapped.
14017c478bd9Sstevel@tonic-gate 		 * Note that we don't evict entries from the cache
14027c478bd9Sstevel@tonic-gate 		 * if we're having trouble contacting nfsmapid(1m)
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		if (TIMEOUT(p->id_time) && (*cip->nfsidmap_daemon_dh) != NULL) {
14057c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_rment(p);
14067c478bd9Sstevel@tonic-gate 			continue;
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		/*
14107c478bd9Sstevel@tonic-gate 		 * Compare equal length strings
14117c478bd9Sstevel@tonic-gate 		 */
14127c478bd9Sstevel@tonic-gate 		if (p->id_len == (c_len - 1)) {
14137c478bd9Sstevel@tonic-gate 			if (bcmp(p->id_val, c_str, (c_len - 1)) == 0) {
14147c478bd9Sstevel@tonic-gate 				/*
14157c478bd9Sstevel@tonic-gate 				 * Move to front, and update time.
14167c478bd9Sstevel@tonic-gate 				 */
14177c478bd9Sstevel@tonic-gate 				remque(p);
14187c478bd9Sstevel@tonic-gate 				insque(p, hq);
14197c478bd9Sstevel@tonic-gate 				p->id_time = gethrestime_sec();
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 				mutex_exit(&(hq->hq_lock));
14227c478bd9Sstevel@tonic-gate 				kmem_free(c_str, c_len);
14237c478bd9Sstevel@tonic-gate 				return;
14247c478bd9Sstevel@tonic-gate 			}
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * Not found ! Alloc, init and insert new entry
14307c478bd9Sstevel@tonic-gate 	 */
14317c478bd9Sstevel@tonic-gate 	newp = kmem_cache_alloc(nfsidmap_cache, KM_SLEEP);
14327c478bd9Sstevel@tonic-gate 	newp->id_len = u8s->utf8string_len;
14337c478bd9Sstevel@tonic-gate 	newp->id_val = kmem_alloc(u8s->utf8string_len, KM_SLEEP);
14347c478bd9Sstevel@tonic-gate 	bcopy(u8s->utf8string_val, newp->id_val, u8s->utf8string_len);
14357c478bd9Sstevel@tonic-gate 	newp->id_no = id;
14367c478bd9Sstevel@tonic-gate 	newp->id_time = gethrestime_sec();
14377c478bd9Sstevel@tonic-gate 	insque(newp, hq);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	mutex_exit(&(hq->hq_lock));
14407c478bd9Sstevel@tonic-gate 	kmem_free(c_str, c_len);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /*
14447c478bd9Sstevel@tonic-gate  * Search the specified cache for the existence of the specified id.
14457c478bd9Sstevel@tonic-gate  * If found, the corresponding mapping is returned in u8s and the
14467c478bd9Sstevel@tonic-gate  * cache entry is updated to the head of the LRU list. The computed
14477c478bd9Sstevel@tonic-gate  * hash queue number, is returned in hashno.
14487c478bd9Sstevel@tonic-gate  */
14497c478bd9Sstevel@tonic-gate static uint_t
14507c478bd9Sstevel@tonic-gate nfs_idmap_cache_i2s_lkup(idmap_cache_info_t *cip,   /* cache info ptr */
14517c478bd9Sstevel@tonic-gate 			uid_t id,		    /* id to resolve */
14527c478bd9Sstevel@tonic-gate 			uint_t *hashno,		    /* hash number, retval */
14537c478bd9Sstevel@tonic-gate 			utf8string *u8s)	/* if found, utf8 str for id */
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	uint_t			 found_stat = 0;
14567c478bd9Sstevel@tonic-gate 	nfsidmap_t		*p;
14577c478bd9Sstevel@tonic-gate 	nfsidmap_t		*pnext;
14587c478bd9Sstevel@tonic-gate 	nfsidhq_t		*hq;
14597c478bd9Sstevel@tonic-gate 	uint_t			 hash;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	 * Compute hash queue
14637c478bd9Sstevel@tonic-gate 	 */
14647c478bd9Sstevel@tonic-gate 	ID_HASH(id, hash);
14657c478bd9Sstevel@tonic-gate 	*hashno = hash;
14667c478bd9Sstevel@tonic-gate 	hq = &cip->table[hash];
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Look for the entry in the HQ
14707c478bd9Sstevel@tonic-gate 	 */
14717c478bd9Sstevel@tonic-gate 	mutex_enter(&(hq->hq_lock));
14727c478bd9Sstevel@tonic-gate 	for (p = hq->hq_lru_forw; p != (nfsidmap_t *)hq; p = pnext) {
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		pnext = p->id_forw;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		/*
14777c478bd9Sstevel@tonic-gate 		 * Check entry for staleness first, as user's id
14787c478bd9Sstevel@tonic-gate 		 * may have changed and may need to be remapped.
14797c478bd9Sstevel@tonic-gate 		 * Note that we don't evict entries from the cache
14807c478bd9Sstevel@tonic-gate 		 * if we're having trouble contacting nfsmapid(1m)
14817c478bd9Sstevel@tonic-gate 		 */
14827c478bd9Sstevel@tonic-gate 		if (TIMEOUT(p->id_time) && (*cip->nfsidmap_daemon_dh) != NULL) {
14837c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_rment(p);
14847c478bd9Sstevel@tonic-gate 			continue;
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 		if (p->id_no == id) {
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 			/*
14907c478bd9Sstevel@tonic-gate 			 * Found it. Load return value and move to head
14917c478bd9Sstevel@tonic-gate 			 */
14927c478bd9Sstevel@tonic-gate 			ASSERT(u8s->utf8string_val == NULL);
14937c478bd9Sstevel@tonic-gate 			u8s->utf8string_len = p->id_len;
14947c478bd9Sstevel@tonic-gate 			u8s->utf8string_val = kmem_alloc(p->id_len, KM_SLEEP);
14957c478bd9Sstevel@tonic-gate 			bcopy(p->id_val, u8s->utf8string_val, p->id_len);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 			remque(p);
14987c478bd9Sstevel@tonic-gate 			insque(p, hq);
14997c478bd9Sstevel@tonic-gate 			p->id_time = gethrestime_sec();
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 			found_stat = 1;
15027c478bd9Sstevel@tonic-gate 			break;
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 	mutex_exit(&(hq->hq_lock));
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	return (found_stat);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*
15117c478bd9Sstevel@tonic-gate  * Search the specified cache for the existence of the specified id,
15127c478bd9Sstevel@tonic-gate  * as it may have been inserted before this instance got a chance to
15137c478bd9Sstevel@tonic-gate  * do it. If NOT found, then a new entry is allocated for the specified
15147c478bd9Sstevel@tonic-gate  * cache, and inserted. The hash queue number is obtained from hash_number
15157c478bd9Sstevel@tonic-gate  * if the behavior is HQ_HASH_HINT, or computed otherwise.
15167c478bd9Sstevel@tonic-gate  */
15177c478bd9Sstevel@tonic-gate static void
15187c478bd9Sstevel@tonic-gate nfs_idmap_cache_i2s_insert(idmap_cache_info_t *cip, /* cache info ptr */
15197c478bd9Sstevel@tonic-gate 			uid_t id,		    /* id to resolve */
15207c478bd9Sstevel@tonic-gate 			utf8string *u8s,	/* utf8 result from upcall */
15217c478bd9Sstevel@tonic-gate 			hash_stat behavior,	/* has algorithm behavior */
15227c478bd9Sstevel@tonic-gate 			uint_t hash_number)	/* hash number iff hint */
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	uint_t		 hashno;
15257c478bd9Sstevel@tonic-gate 	nfsidhq_t	*hq;
15267c478bd9Sstevel@tonic-gate 	nfsidmap_t	*newp;
15277c478bd9Sstevel@tonic-gate 	nfsidmap_t	*pnext;
15287c478bd9Sstevel@tonic-gate 	nfsidmap_t	*p;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	/*
15327c478bd9Sstevel@tonic-gate 	 * Obtain correct hash queue to insert new entry in
15337c478bd9Sstevel@tonic-gate 	 */
15347c478bd9Sstevel@tonic-gate 	switch (behavior) {
15357c478bd9Sstevel@tonic-gate 		case HQ_HASH_HINT:
15367c478bd9Sstevel@tonic-gate 			hashno = hash_number;
15377c478bd9Sstevel@tonic-gate 			break;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 		case HQ_HASH_FIND:
15407c478bd9Sstevel@tonic-gate 		default:
15417c478bd9Sstevel@tonic-gate 			ID_HASH(id, hashno);
15427c478bd9Sstevel@tonic-gate 			break;
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 	hq = &cip->table[hashno];
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Look for an existing entry in the cache. If one exists
15497c478bd9Sstevel@tonic-gate 	 * update it, and return. Otherwise, allocate a new cache
15507c478bd9Sstevel@tonic-gate 	 * entry, initialize and insert it.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	mutex_enter(&(hq->hq_lock));
15537c478bd9Sstevel@tonic-gate 	for (p = hq->hq_lru_forw; p != (nfsidmap_t *)hq; p = pnext) {
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 		pnext = p->id_forw;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 		/*
15587c478bd9Sstevel@tonic-gate 		 * Check entry for staleness first, as user's id
15597c478bd9Sstevel@tonic-gate 		 * may have changed and may need to be remapped.
15607c478bd9Sstevel@tonic-gate 		 * Note that we don't evict entries from the cache
15617c478bd9Sstevel@tonic-gate 		 * if we're having trouble contacting nfsmapid(1m)
15627c478bd9Sstevel@tonic-gate 		 */
15637c478bd9Sstevel@tonic-gate 		if (TIMEOUT(p->id_time) && (*cip->nfsidmap_daemon_dh) != NULL) {
15647c478bd9Sstevel@tonic-gate 			nfs_idmap_cache_rment(p);
15657c478bd9Sstevel@tonic-gate 			continue;
15667c478bd9Sstevel@tonic-gate 		}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		if ((p->id_no == id) && (p->id_len == u8s->utf8string_len)) {
15707c478bd9Sstevel@tonic-gate 			/*
15717c478bd9Sstevel@tonic-gate 			 * Found It ! Move to front, and update time.
15727c478bd9Sstevel@tonic-gate 			 */
15737c478bd9Sstevel@tonic-gate 			remque(p);
15747c478bd9Sstevel@tonic-gate 			insque(p, hq);
15757c478bd9Sstevel@tonic-gate 			p->id_time = gethrestime_sec();
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 			mutex_exit(&(hq->hq_lock));
15787c478bd9Sstevel@tonic-gate 			return;
15797c478bd9Sstevel@tonic-gate 		}
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/*
15837c478bd9Sstevel@tonic-gate 	 * Not found ! Alloc, init and insert new entry
15847c478bd9Sstevel@tonic-gate 	 */
15857c478bd9Sstevel@tonic-gate 	newp = kmem_cache_alloc(nfsidmap_cache, KM_SLEEP);
15867c478bd9Sstevel@tonic-gate 	newp->id_len = u8s->utf8string_len;
15877c478bd9Sstevel@tonic-gate 	newp->id_val = kmem_alloc(u8s->utf8string_len, KM_SLEEP);
15887c478bd9Sstevel@tonic-gate 	bcopy(u8s->utf8string_val, newp->id_val, u8s->utf8string_len);
15897c478bd9Sstevel@tonic-gate 	newp->id_no = id;
15907c478bd9Sstevel@tonic-gate 	newp->id_time = gethrestime_sec();
15917c478bd9Sstevel@tonic-gate 	insque(newp, hq);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	mutex_exit(&(hq->hq_lock));
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * Remove and free one cache entry
15987c478bd9Sstevel@tonic-gate  */
15997c478bd9Sstevel@tonic-gate static void
16007c478bd9Sstevel@tonic-gate nfs_idmap_cache_rment(nfsidmap_t *p)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	remque(p);
16037c478bd9Sstevel@tonic-gate 	if (p->id_val != 0)
16047c478bd9Sstevel@tonic-gate 		kmem_free(p->id_val, p->id_len);
16057c478bd9Sstevel@tonic-gate 	kmem_cache_free(nfsidmap_cache, p);
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate #ifndef		UID_MAX
16097c478bd9Sstevel@tonic-gate #define		UID_MAX		2147483647		/* see limits.h */
16107c478bd9Sstevel@tonic-gate #endif
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate #ifndef		isdigit
16137c478bd9Sstevel@tonic-gate #define		isdigit(c)	((c) >= '0' && (c) <= '9')
16147c478bd9Sstevel@tonic-gate #endif
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate static int
16177c478bd9Sstevel@tonic-gate is_stringified_id(utf8string *u8s)
16187c478bd9Sstevel@tonic-gate {
16197c478bd9Sstevel@tonic-gate 	int	i;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	for (i = 0; i < u8s->utf8string_len; i++)
16227c478bd9Sstevel@tonic-gate 		if (!isdigit(u8s->utf8string_val[i]))
16237c478bd9Sstevel@tonic-gate 			return (0);
16247c478bd9Sstevel@tonic-gate 	return (1);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate int
16287c478bd9Sstevel@tonic-gate nfs_idmap_s2i_literal(utf8string *u8s, uid_t *id, int isserver)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	long	tmp;
16317c478bd9Sstevel@tonic-gate 	int	convd;
16327c478bd9Sstevel@tonic-gate 	char	ids[_MAXIDSTRLEN];
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	/*
16357c478bd9Sstevel@tonic-gate 	 * "nobody" unless we can actually decode it.
16367c478bd9Sstevel@tonic-gate 	 */
16377c478bd9Sstevel@tonic-gate 	*id = UID_NOBODY;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * We're here because it has already been determined that the
16417c478bd9Sstevel@tonic-gate 	 * string contains no '@' _or_ the nfsmapid daemon has yet to
16427c478bd9Sstevel@tonic-gate 	 * be started.
16437c478bd9Sstevel@tonic-gate 	 */
16447c478bd9Sstevel@tonic-gate 	if (!is_stringified_id(u8s))
16457c478bd9Sstevel@tonic-gate 		return (0);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	/*
16487c478bd9Sstevel@tonic-gate 	 * If utf8string_len is greater than _MAXIDSTRLEN-1, then the id
16497c478bd9Sstevel@tonic-gate 	 * is going to be greater than UID_MAX. Return id of "nobody"
16507c478bd9Sstevel@tonic-gate 	 * right away.
16517c478bd9Sstevel@tonic-gate 	 */
16527c478bd9Sstevel@tonic-gate 	if (u8s->utf8string_len >= _MAXIDSTRLEN)
16537c478bd9Sstevel@tonic-gate 		return (isserver ? EPERM : 0);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * Make sure we pass a NULL terminated 'C' string to ddi_strtol
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	bcopy(u8s->utf8string_val, ids, u8s->utf8string_len);
16597c478bd9Sstevel@tonic-gate 	ids[u8s->utf8string_len] = '\0';
16607c478bd9Sstevel@tonic-gate 	convd = ddi_strtol(ids, NULL, 10, &tmp);
16617c478bd9Sstevel@tonic-gate 	if (convd == 0 && tmp >= 0 && tmp <= UID_MAX) {
16627c478bd9Sstevel@tonic-gate 		*id = tmp;
16637c478bd9Sstevel@tonic-gate 		return (0);
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 	return (isserver ? EPERM : 0);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate static void
16697c478bd9Sstevel@tonic-gate nfs_idmap_i2s_literal(uid_t id, utf8string *u8s)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	char	ids[_MAXIDSTRLEN];
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	(void) snprintf(ids, _MAXIDSTRLEN, "%d", id);
16747c478bd9Sstevel@tonic-gate 	(void) str_to_utf8(ids, u8s);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate /* -- Utility functions -- */
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate char *
16807c478bd9Sstevel@tonic-gate utf8_strchr(utf8string *u8s, const char c)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	int	i;
16837c478bd9Sstevel@tonic-gate 	char	*u8p = u8s->utf8string_val;
16847c478bd9Sstevel@tonic-gate 	int	len = u8s->utf8string_len;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
16877c478bd9Sstevel@tonic-gate 		if (u8p[i] == c)
16887c478bd9Sstevel@tonic-gate 			return (&u8p[i]);
16897c478bd9Sstevel@tonic-gate 	return (NULL);
16907c478bd9Sstevel@tonic-gate }
1691