xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_auth.c (revision 66597161e2ba69a84fa138bce7ac02a1e6b9746c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2015 by Delphix. All rights reserved.
25  * Copyright (c) 2015 Joyent, Inc.  All rights reserved.
26  * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/vfs.h>
32 #include <sys/vnode.h>
33 #include <sys/cred.h>
34 #include <sys/cmn_err.h>
35 #include <sys/systm.h>
36 #include <sys/kmem.h>
37 #include <sys/pathname.h>
38 #include <sys/utsname.h>
39 #include <sys/debug.h>
40 #include <sys/door.h>
41 #include <sys/sdt.h>
42 #include <sys/thread.h>
43 #include <sys/avl.h>
44 
45 #include <rpc/types.h>
46 #include <rpc/auth.h>
47 #include <rpc/clnt.h>
48 
49 #include <nfs/nfs.h>
50 #include <nfs/export.h>
51 #include <nfs/nfs_clnt.h>
52 #include <nfs/auth.h>
53 
54 static struct kmem_cache *exi_cache_handle;
55 static void exi_cache_reclaim(void *);
56 static void exi_cache_reclaim_zone(nfs_globals_t *);
57 static void exi_cache_trim(struct exportinfo *exi);
58 
59 extern pri_t minclsyspri;
60 
61 /* NFS auth cache statistics */
62 volatile uint_t nfsauth_cache_hit;
63 volatile uint_t nfsauth_cache_miss;
64 volatile uint_t nfsauth_cache_refresh;
65 volatile uint_t nfsauth_cache_reclaim;
66 volatile uint_t exi_cache_auth_reclaim_failed;
67 volatile uint_t exi_cache_clnt_reclaim_failed;
68 
69 /*
70  * The lifetime of an auth cache entry:
71  * ------------------------------------
72  *
73  * An auth cache entry is created with both the auth_time
74  * and auth_freshness times set to the current time.
75  *
76  * Upon every client access which results in a hit, the
77  * auth_time will be updated.
78  *
79  * If a client access determines that the auth_freshness
80  * indicates that the entry is STALE, then it will be
81  * refreshed. Note that this will explicitly reset
82  * auth_time.
83  *
84  * When the REFRESH successfully occurs, then the
85  * auth_freshness is updated.
86  *
87  * There are two ways for an entry to leave the cache:
88  *
89  * 1) Purged by an action on the export (remove or changed)
90  * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
91  *
92  * For 2) we check the timeout value against auth_time.
93  */
94 
95 /*
96  * Number of seconds until we mark for refresh an auth cache entry.
97  */
98 #define	NFSAUTH_CACHE_REFRESH 600
99 
100 /*
101  * Number of idle seconds until we yield to backpressure
102  * to trim a cache entry.
103  */
104 #define	NFSAUTH_CACHE_TRIM 3600
105 
106 /*
107  * While we could encapuslate the exi_list inside the
108  * exi structure, we can't do that for the auth_list.
109  * So, to keep things looking clean, we keep them both
110  * in these external lists.
111  */
112 typedef struct refreshq_exi_node {
113 	struct exportinfo	*ren_exi;
114 	list_t			ren_authlist;
115 	list_node_t		ren_node;
116 } refreshq_exi_node_t;
117 
118 typedef struct refreshq_auth_node {
119 	struct auth_cache	*ran_auth;
120 	char			*ran_netid;
121 	list_node_t		ran_node;
122 } refreshq_auth_node_t;
123 
124 /*
125  * Used to manipulate things on the refreshq_queue.  Note that the refresh
126  * thread will effectively pop a node off of the queue, at which point it
127  * will no longer need to hold the mutex.
128  */
129 static kmutex_t refreshq_lock;
130 static list_t refreshq_queue;
131 static kcondvar_t refreshq_cv;
132 
133 /*
134  * If there is ever a problem with loading the module, then nfsauth_fini()
135  * needs to be called to remove state.  In that event, since the refreshq
136  * thread has been started, they need to work together to get rid of state.
137  */
138 typedef enum nfsauth_refreshq_thread_state {
139 	REFRESHQ_THREAD_RUNNING,
140 	REFRESHQ_THREAD_FINI_REQ,
141 	REFRESHQ_THREAD_HALTED,
142 	REFRESHQ_THREAD_NEED_CREATE
143 } nfsauth_refreshq_thread_state_t;
144 
145 typedef struct nfsauth_globals {
146 	kmutex_t	mountd_lock;
147 	door_handle_t   mountd_dh;
148 
149 	/*
150 	 * Used to manipulate things on the refreshq_queue.  Note that the
151 	 * refresh thread will effectively pop a node off of the queue,
152 	 * at which point it will no longer need to hold the mutex.
153 	 */
154 	kmutex_t	refreshq_lock;
155 	list_t		refreshq_queue;
156 	kcondvar_t	refreshq_cv;
157 
158 	/*
159 	 * A list_t would be overkill.  These are auth_cache entries which are
160 	 * no longer linked to an exi.  It should be the case that all of their
161 	 * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
162 	 * list is iff their state indicated that they had been placed on the
163 	 * refreshq_queue.
164 	 *
165 	 * Note that while there is no link from the exi or back to the exi,
166 	 * the exi can not go away until these entries are harvested.
167 	 */
168 	struct auth_cache		*refreshq_dead_entries;
169 	nfsauth_refreshq_thread_state_t	refreshq_thread_state;
170 
171 } nfsauth_globals_t;
172 
173 static void nfsauth_free_node(struct auth_cache *);
174 static void nfsauth_refresh_thread(nfsauth_globals_t *);
175 
176 static int nfsauth_cache_compar(const void *, const void *);
177 
178 static nfsauth_globals_t *
179 nfsauth_get_zg(void)
180 {
181 	nfs_globals_t *ng = nfs_srv_getzg();
182 	nfsauth_globals_t *nag = ng->nfs_auth;
183 	ASSERT(nag != NULL);
184 	return (nag);
185 }
186 
187 void
188 mountd_args(uint_t did)
189 {
190 	nfsauth_globals_t *nag;
191 
192 	nag = nfsauth_get_zg();
193 	mutex_enter(&nag->mountd_lock);
194 	if (nag->mountd_dh != NULL)
195 		door_ki_rele(nag->mountd_dh);
196 	nag->mountd_dh = door_ki_lookup(did);
197 	mutex_exit(&nag->mountd_lock);
198 }
199 
200 void
201 nfsauth_init(void)
202 {
203 	exi_cache_handle = kmem_cache_create("exi_cache_handle",
204 	    sizeof (struct auth_cache), 0, NULL, NULL,
205 	    exi_cache_reclaim, NULL, NULL, 0);
206 }
207 
208 void
209 nfsauth_fini(void)
210 {
211 	kmem_cache_destroy(exi_cache_handle);
212 }
213 
214 void
215 nfsauth_zone_init(nfs_globals_t *ng)
216 {
217 	nfsauth_globals_t *nag;
218 
219 	nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
220 
221 	/*
222 	 * mountd can be restarted by smf(5).  We need to make sure
223 	 * the updated door handle will safely make it to mountd_dh.
224 	 */
225 	mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
226 	mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
227 	list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
228 	    offsetof(refreshq_exi_node_t, ren_node));
229 	cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
230 	nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
231 
232 	ng->nfs_auth = nag;
233 }
234 
235 void
236 nfsauth_zone_shutdown(nfs_globals_t *ng)
237 {
238 	refreshq_exi_node_t	*ren;
239 	nfsauth_globals_t	*nag = ng->nfs_auth;
240 
241 	/* Prevent the nfsauth_refresh_thread from getting new work */
242 	mutex_enter(&nag->refreshq_lock);
243 	if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
244 		nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
245 		cv_broadcast(&nag->refreshq_cv);
246 
247 		/* Wait for nfsauth_refresh_thread() to exit */
248 		while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
249 			cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
250 	}
251 	mutex_exit(&nag->refreshq_lock);
252 
253 	/*
254 	 * Walk the exi_list and in turn, walk the auth_lists and free all
255 	 * lists.  In addition, free INVALID auth_cache entries.
256 	 */
257 	while ((ren = list_remove_head(&nag->refreshq_queue))) {
258 		refreshq_auth_node_t *ran;
259 
260 		while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
261 			struct auth_cache *p = ran->ran_auth;
262 			if (p->auth_state == NFS_AUTH_INVALID)
263 				nfsauth_free_node(p);
264 			strfree(ran->ran_netid);
265 			kmem_free(ran, sizeof (*ran));
266 		}
267 
268 		list_destroy(&ren->ren_authlist);
269 		exi_rele(ren->ren_exi);
270 		kmem_free(ren, sizeof (*ren));
271 	}
272 }
273 
274 void
275 nfsauth_zone_fini(nfs_globals_t *ng)
276 {
277 	nfsauth_globals_t *nag = ng->nfs_auth;
278 
279 	ng->nfs_auth = NULL;
280 
281 	list_destroy(&nag->refreshq_queue);
282 	cv_destroy(&nag->refreshq_cv);
283 	mutex_destroy(&nag->refreshq_lock);
284 	mutex_destroy(&nag->mountd_lock);
285 	/* Extra cleanup. */
286 	if (nag->mountd_dh != NULL)
287 		door_ki_rele(nag->mountd_dh);
288 	kmem_free(nag, sizeof (*nag));
289 }
290 
291 /*
292  * Convert the address in a netbuf to
293  * a hash index for the auth_cache table.
294  */
295 static int
296 hash(struct netbuf *a)
297 {
298 	int i, h = 0;
299 
300 	for (i = 0; i < a->len; i++)
301 		h ^= a->buf[i];
302 
303 	return (h & (AUTH_TABLESIZE - 1));
304 }
305 
306 /*
307  * Mask out the components of an
308  * address that do not identify
309  * a host. For socket addresses the
310  * masking gets rid of the port number.
311  */
312 static void
313 addrmask(struct netbuf *addr, struct netbuf *mask)
314 {
315 	int i;
316 
317 	for (i = 0; i < addr->len; i++)
318 		addr->buf[i] &= mask->buf[i];
319 }
320 
321 /*
322  * nfsauth4_access is used for NFS V4 auth checking. Besides doing
323  * the common nfsauth_access(), it will check if the client can
324  * have a limited access to this vnode even if the security flavor
325  * used does not meet the policy.
326  */
327 int
328 nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
329     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
330 {
331 	int access;
332 
333 	access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
334 
335 	/*
336 	 * There are cases that the server needs to allow the client
337 	 * to have a limited view.
338 	 *
339 	 * e.g.
340 	 * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
341 	 * /export/home is shared as "sec=sys,rw"
342 	 *
343 	 * When the client mounts /export with sec=sys, the client
344 	 * would get a limited view with RO access on /export to see
345 	 * "home" only because the client is allowed to access
346 	 * /export/home with auth_sys.
347 	 */
348 	if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
349 		/*
350 		 * Allow ro permission with LIMITED view if there is a
351 		 * sub-dir exported under vp.
352 		 */
353 		if (has_visible(exi, vp))
354 			return (NFSAUTH_LIMITED);
355 	}
356 
357 	return (access);
358 }
359 
360 static void
361 sys_log(const char *msg)
362 {
363 	static time_t	tstamp = 0;
364 	time_t		now;
365 
366 	/*
367 	 * msg is shown (at most) once per minute
368 	 */
369 	now = gethrestime_sec();
370 	if ((tstamp + 60) < now) {
371 		tstamp = now;
372 		cmn_err(CE_WARN, msg);
373 	}
374 }
375 
376 /*
377  * Callup to the mountd to get access information in the kernel.
378  */
379 static bool_t
380 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
381     char *req_netid, int flavor, struct netbuf *addr, int *access,
382     cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
383     gid_t **srv_gids)
384 {
385 	varg_t			  varg = {0};
386 	nfsauth_res_t		  res = {0};
387 	XDR			  xdrs;
388 	size_t			  absz;
389 	caddr_t			  abuf;
390 	int			  last = 0;
391 	door_arg_t		  da;
392 	door_info_t		  di;
393 	door_handle_t		  dh;
394 	uint_t			  ntries = 0;
395 
396 	/*
397 	 * No entry in the cache for this client/flavor
398 	 * so we need to call the nfsauth service in the
399 	 * mount daemon.
400 	 */
401 
402 	varg.vers = V_PROTO;
403 	varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
404 	varg.arg_u.arg.areq.req_client.n_len = addr->len;
405 	varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
406 	varg.arg_u.arg.areq.req_netid = req_netid;
407 	varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
408 	varg.arg_u.arg.areq.req_flavor = flavor;
409 	varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
410 	varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
411 	varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
412 	varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
413 
414 	DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
415 
416 	/*
417 	 * Setup the XDR stream for encoding the arguments. Notice that
418 	 * in addition to the args having variable fields (req_netid and
419 	 * req_path), the argument data structure is itself versioned,
420 	 * so we need to make sure we can size the arguments buffer
421 	 * appropriately to encode all the args. If we can't get sizing
422 	 * info _or_ properly encode the arguments, there's really no
423 	 * point in continuting, so we fail the request.
424 	 */
425 	if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
426 		*access = NFSAUTH_DENIED;
427 		return (FALSE);
428 	}
429 
430 	abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
431 	xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
432 	if (!xdr_varg(&xdrs, &varg)) {
433 		XDR_DESTROY(&xdrs);
434 		goto fail;
435 	}
436 	XDR_DESTROY(&xdrs);
437 
438 	/*
439 	 * Prepare the door arguments
440 	 *
441 	 * We don't know the size of the message the daemon
442 	 * will pass back to us.  By setting rbuf to NULL,
443 	 * we force the door code to allocate a buf of the
444 	 * appropriate size.  We must set rsize > 0, however,
445 	 * else the door code acts as if no response was
446 	 * expected and doesn't pass the data to us.
447 	 */
448 	da.data_ptr = (char *)abuf;
449 	da.data_size = absz;
450 	da.desc_ptr = NULL;
451 	da.desc_num = 0;
452 	da.rbuf = NULL;
453 	da.rsize = 1;
454 
455 retry:
456 	mutex_enter(&nag->mountd_lock);
457 	dh = nag->mountd_dh;
458 	if (dh != NULL)
459 		door_ki_hold(dh);
460 	mutex_exit(&nag->mountd_lock);
461 
462 	if (dh == NULL) {
463 		/*
464 		 * The rendezvous point has not been established yet!
465 		 * This could mean that either mountd(1m) has not yet
466 		 * been started or that _this_ routine nuked the door
467 		 * handle after receiving an EINTR for a REVOKED door.
468 		 *
469 		 * Returning NFSAUTH_DROP will cause the NFS client
470 		 * to retransmit the request, so let's try to be more
471 		 * rescillient and attempt for ntries before we bail.
472 		 */
473 		if (++ntries % NFSAUTH_DR_TRYCNT) {
474 			delay(hz);
475 			goto retry;
476 		}
477 
478 		kmem_free(abuf, absz);
479 
480 		sys_log("nfsauth: mountd has not established door");
481 		*access = NFSAUTH_DROP;
482 		return (FALSE);
483 	}
484 
485 	ntries = 0;
486 
487 	/*
488 	 * Now that we've got what we need, place the call.
489 	 */
490 	switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
491 	case 0:				/* Success */
492 		door_ki_rele(dh);
493 
494 		if (da.data_ptr == NULL && da.data_size == 0) {
495 			/*
496 			 * The door_return that contained the data
497 			 * failed! We're here because of the 2nd
498 			 * door_return (w/o data) such that we can
499 			 * get control of the thread (and exit
500 			 * gracefully).
501 			 */
502 			DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
503 			    door_arg_t *, &da);
504 			goto fail;
505 		}
506 
507 		break;
508 
509 	case EAGAIN:
510 		/*
511 		 * Server out of resources; back off for a bit
512 		 */
513 		door_ki_rele(dh);
514 		delay(hz);
515 		goto retry;
516 		/* NOTREACHED */
517 
518 	case EINTR:
519 		if (!door_ki_info(dh, &di)) {
520 			door_ki_rele(dh);
521 
522 			if (di.di_attributes & DOOR_REVOKED) {
523 				/*
524 				 * The server barfed and revoked
525 				 * the (existing) door on us; we
526 				 * want to wait to give smf(5) a
527 				 * chance to restart mountd(1m)
528 				 * and establish a new door handle.
529 				 */
530 				mutex_enter(&nag->mountd_lock);
531 				if (dh == nag->mountd_dh) {
532 					door_ki_rele(nag->mountd_dh);
533 					nag->mountd_dh = NULL;
534 				}
535 				mutex_exit(&nag->mountd_lock);
536 				delay(hz);
537 				goto retry;
538 			}
539 			/*
540 			 * If the door was _not_ revoked on us,
541 			 * then more than likely we took an INTR,
542 			 * so we need to fail the operation.
543 			 */
544 			goto fail;
545 		}
546 		/*
547 		 * The only failure that can occur from getting
548 		 * the door info is EINVAL, so we let the code
549 		 * below handle it.
550 		 */
551 		/* FALLTHROUGH */
552 
553 	case EBADF:
554 	case EINVAL:
555 	default:
556 		/*
557 		 * If we have a stale door handle, give smf a last
558 		 * chance to start it by sleeping for a little bit.
559 		 * If we're still hosed, we'll fail the call.
560 		 *
561 		 * Since we're going to reacquire the door handle
562 		 * upon the retry, we opt to sleep for a bit and
563 		 * _not_ to clear mountd_dh. If mountd restarted
564 		 * and was able to set mountd_dh, we should see
565 		 * the new instance; if not, we won't get caught
566 		 * up in the retry/DELAY loop.
567 		 */
568 		door_ki_rele(dh);
569 		if (!last) {
570 			delay(hz);
571 			last++;
572 			goto retry;
573 		}
574 		sys_log("nfsauth: stale mountd door handle");
575 		goto fail;
576 	}
577 
578 	ASSERT(da.rbuf != NULL);
579 
580 	/*
581 	 * No door errors encountered; setup the XDR stream for decoding
582 	 * the results. If we fail to decode the results, we've got no
583 	 * other recourse than to fail the request.
584 	 */
585 	xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
586 	if (!xdr_nfsauth_res(&xdrs, &res)) {
587 		xdr_free(xdr_nfsauth_res, (char *)&res);
588 		XDR_DESTROY(&xdrs);
589 		kmem_free(da.rbuf, da.rsize);
590 		goto fail;
591 	}
592 	XDR_DESTROY(&xdrs);
593 	kmem_free(da.rbuf, da.rsize);
594 
595 	DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
596 	switch (res.stat) {
597 		case NFSAUTH_DR_OKAY:
598 			*access = res.ares.auth_perm;
599 			*srv_uid = res.ares.auth_srv_uid;
600 			*srv_gid = res.ares.auth_srv_gid;
601 
602 			if ((*srv_gids_cnt = res.ares.auth_srv_gids.len) != 0) {
603 				*srv_gids = kmem_alloc(*srv_gids_cnt *
604 				    sizeof (gid_t), KM_SLEEP);
605 				bcopy(res.ares.auth_srv_gids.val, *srv_gids,
606 				    *srv_gids_cnt * sizeof (gid_t));
607 			} else {
608 				*srv_gids = NULL;
609 			}
610 
611 			break;
612 
613 		case NFSAUTH_DR_EFAIL:
614 		case NFSAUTH_DR_DECERR:
615 		case NFSAUTH_DR_BADCMD:
616 		default:
617 			xdr_free(xdr_nfsauth_res, (char *)&res);
618 fail:
619 			*access = NFSAUTH_DENIED;
620 			kmem_free(abuf, absz);
621 			return (FALSE);
622 			/* NOTREACHED */
623 	}
624 
625 	xdr_free(xdr_nfsauth_res, (char *)&res);
626 	kmem_free(abuf, absz);
627 
628 	return (TRUE);
629 }
630 
631 static void
632 nfsauth_refresh_thread(nfsauth_globals_t *nag)
633 {
634 	refreshq_exi_node_t	*ren;
635 	refreshq_auth_node_t	*ran;
636 
637 	struct exportinfo	*exi;
638 
639 	int			access;
640 	bool_t			retrieval;
641 
642 	callb_cpr_t		cprinfo;
643 
644 	CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
645 	    "nfsauth_refresh");
646 
647 	for (;;) {
648 		mutex_enter(&nag->refreshq_lock);
649 		if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
650 			/* Keep the hold on the lock! */
651 			break;
652 		}
653 
654 		ren = list_remove_head(&nag->refreshq_queue);
655 		if (ren == NULL) {
656 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
657 			cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
658 			CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
659 			mutex_exit(&nag->refreshq_lock);
660 			continue;
661 		}
662 		mutex_exit(&nag->refreshq_lock);
663 
664 		exi = ren->ren_exi;
665 		ASSERT(exi != NULL);
666 
667 		/*
668 		 * Since the ren was removed from the refreshq_queue above,
669 		 * this is the only thread aware about the ren existence, so we
670 		 * have the exclusive ownership of it and we do not need to
671 		 * protect it by any lock.
672 		 */
673 		while ((ran = list_remove_head(&ren->ren_authlist))) {
674 			uid_t uid;
675 			gid_t gid;
676 			uint_t ngids;
677 			gid_t *gids;
678 			struct auth_cache *p = ran->ran_auth;
679 			char *netid = ran->ran_netid;
680 
681 			ASSERT(p != NULL);
682 			ASSERT(netid != NULL);
683 
684 			kmem_free(ran, sizeof (refreshq_auth_node_t));
685 
686 			mutex_enter(&p->auth_lock);
687 
688 			/*
689 			 * Once the entry goes INVALID, it can not change
690 			 * state.
691 			 *
692 			 * No need to refresh entries also in a case we are
693 			 * just shutting down.
694 			 *
695 			 * In general, there is no need to hold the
696 			 * refreshq_lock to test the refreshq_thread_state.  We
697 			 * do hold it at other places because there is some
698 			 * related thread synchronization (or some other tasks)
699 			 * close to the refreshq_thread_state check.
700 			 *
701 			 * The check for the refreshq_thread_state value here
702 			 * is purely advisory to allow the faster
703 			 * nfsauth_refresh_thread() shutdown.  In a case we
704 			 * will miss such advisory, nothing catastrophic
705 			 * happens: we will just spin longer here before the
706 			 * shutdown.
707 			 */
708 			if (p->auth_state == NFS_AUTH_INVALID ||
709 			    nag->refreshq_thread_state !=
710 			    REFRESHQ_THREAD_RUNNING) {
711 				mutex_exit(&p->auth_lock);
712 
713 				if (p->auth_state == NFS_AUTH_INVALID)
714 					nfsauth_free_node(p);
715 
716 				strfree(netid);
717 
718 				continue;
719 			}
720 
721 			/*
722 			 * Make sure the state is valid.  Note that once we
723 			 * change the state to NFS_AUTH_REFRESHING, no other
724 			 * thread will be able to work on this entry.
725 			 */
726 			ASSERT(p->auth_state == NFS_AUTH_STALE);
727 
728 			p->auth_state = NFS_AUTH_REFRESHING;
729 			mutex_exit(&p->auth_lock);
730 
731 			DTRACE_PROBE2(nfsauth__debug__cache__refresh,
732 			    struct exportinfo *, exi,
733 			    struct auth_cache *, p);
734 
735 			/*
736 			 * The first caching of the access rights
737 			 * is done with the netid pulled out of the
738 			 * request from the client. All subsequent
739 			 * users of the cache may or may not have
740 			 * the same netid. It doesn't matter. So
741 			 * when we refresh, we simply use the netid
742 			 * of the request which triggered the
743 			 * refresh attempt.
744 			 */
745 			retrieval = nfsauth_retrieve(nag, exi, netid,
746 			    p->auth_flavor, &p->auth_clnt->authc_addr, &access,
747 			    p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
748 
749 			/*
750 			 * This can only be set in one other place
751 			 * and the state has to be NFS_AUTH_FRESH.
752 			 */
753 			strfree(netid);
754 
755 			mutex_enter(&p->auth_lock);
756 			if (p->auth_state == NFS_AUTH_INVALID) {
757 				mutex_exit(&p->auth_lock);
758 				nfsauth_free_node(p);
759 				if (retrieval == TRUE)
760 					kmem_free(gids, ngids * sizeof (gid_t));
761 			} else {
762 				/*
763 				 * If we got an error, do not reset the
764 				 * time. This will cause the next access
765 				 * check for the client to reschedule this
766 				 * node.
767 				 */
768 				if (retrieval == TRUE) {
769 					p->auth_access = access;
770 
771 					p->auth_srv_uid = uid;
772 					p->auth_srv_gid = gid;
773 					kmem_free(p->auth_srv_gids,
774 					    p->auth_srv_ngids * sizeof (gid_t));
775 					p->auth_srv_ngids = ngids;
776 					p->auth_srv_gids = gids;
777 
778 					p->auth_freshness = gethrestime_sec();
779 				}
780 				p->auth_state = NFS_AUTH_FRESH;
781 
782 				cv_broadcast(&p->auth_cv);
783 				mutex_exit(&p->auth_lock);
784 			}
785 		}
786 
787 		list_destroy(&ren->ren_authlist);
788 		exi_rele(ren->ren_exi);
789 		kmem_free(ren, sizeof (refreshq_exi_node_t));
790 	}
791 
792 	nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
793 	cv_broadcast(&nag->refreshq_cv);
794 	CALLB_CPR_EXIT(&cprinfo);
795 	DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
796 	zthread_exit();
797 }
798 
799 int
800 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
801 {
802 	int c;
803 
804 	const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
805 	const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
806 
807 	if (a1->authc_addr.len < a2->authc_addr.len)
808 		return (-1);
809 	if (a1->authc_addr.len > a2->authc_addr.len)
810 		return (1);
811 
812 	c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
813 	if (c < 0)
814 		return (-1);
815 	if (c > 0)
816 		return (1);
817 
818 	return (0);
819 }
820 
821 static int
822 nfsauth_cache_compar(const void *v1, const void *v2)
823 {
824 	int c;
825 
826 	const struct auth_cache *a1 = (const struct auth_cache *)v1;
827 	const struct auth_cache *a2 = (const struct auth_cache *)v2;
828 
829 	if (a1->auth_flavor < a2->auth_flavor)
830 		return (-1);
831 	if (a1->auth_flavor > a2->auth_flavor)
832 		return (1);
833 
834 	if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
835 		return (-1);
836 	if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
837 		return (1);
838 
839 	if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
840 		return (-1);
841 	if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
842 		return (1);
843 
844 	if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
845 		return (-1);
846 	if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
847 		return (1);
848 
849 	c = memcmp(crgetgroups(a1->auth_clnt_cred),
850 	    crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
851 	if (c < 0)
852 		return (-1);
853 	if (c > 0)
854 		return (1);
855 
856 	return (0);
857 }
858 
859 /*
860  * Get the access information from the cache or callup to the mountd
861  * to get and cache the access information in the kernel.
862  */
863 static int
864 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
865     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
866 {
867 	nfsauth_globals_t	*nag;
868 	struct netbuf		*taddrmask;
869 	struct netbuf		addr;	/* temporary copy of client's address */
870 	const struct netbuf	*claddr;
871 	avl_tree_t		*tree;
872 	struct auth_cache	ac;	/* used as a template for avl_find() */
873 	struct auth_cache_clnt	*c;
874 	struct auth_cache_clnt	acc;	/* used as a template for avl_find() */
875 	struct auth_cache	*p = NULL;
876 	int			access;
877 
878 	uid_t			tmpuid;
879 	gid_t			tmpgid;
880 	uint_t			tmpngids;
881 	gid_t			*tmpgids;
882 
883 	avl_index_t		where;	/* used for avl_find()/avl_insert() */
884 
885 	ASSERT(cr != NULL);
886 
887 	ASSERT3P(curzone->zone_id, ==, exi->exi_zoneid);
888 	nag = nfsauth_get_zg();
889 
890 	/*
891 	 * Now check whether this client already
892 	 * has an entry for this flavor in the cache
893 	 * for this export.
894 	 * Get the caller's address, mask off the
895 	 * parts of the address that do not identify
896 	 * the host (port number, etc), and then hash
897 	 * it to find the chain of cache entries.
898 	 */
899 
900 	claddr = svc_getrpccaller(req->rq_xprt);
901 	addr = *claddr;
902 	if (claddr->len != 0) {
903 		addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
904 		bcopy(claddr->buf, addr.buf, claddr->len);
905 	} else {
906 		addr.buf = NULL;
907 	}
908 
909 	SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
910 	ASSERT(taddrmask != NULL);
911 	addrmask(&addr, taddrmask);
912 
913 	acc.authc_addr = addr;
914 
915 	tree = exi->exi_cache[hash(&addr)];
916 
917 	rw_enter(&exi->exi_cache_lock, RW_READER);
918 	c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
919 
920 	if (c == NULL) {
921 		struct auth_cache_clnt *nc;
922 
923 		rw_exit(&exi->exi_cache_lock);
924 
925 		nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP | KM_NORMALPRI);
926 		if (nc == NULL)
927 			goto retrieve;
928 
929 		/*
930 		 * Initialize the new auth_cache_clnt
931 		 */
932 		nc->authc_addr = addr;
933 		nc->authc_addr.buf = kmem_alloc(addr.maxlen,
934 		    KM_NOSLEEP | KM_NORMALPRI);
935 		if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
936 			kmem_free(nc, sizeof (*nc));
937 			goto retrieve;
938 		}
939 		bcopy(addr.buf, nc->authc_addr.buf, addr.len);
940 		rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
941 		avl_create(&nc->authc_tree, nfsauth_cache_compar,
942 		    sizeof (struct auth_cache),
943 		    offsetof(struct auth_cache, auth_link));
944 
945 		rw_enter(&exi->exi_cache_lock, RW_WRITER);
946 		c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
947 		if (c == NULL) {
948 			avl_insert(tree, nc, where);
949 			rw_downgrade(&exi->exi_cache_lock);
950 			c = nc;
951 		} else {
952 			rw_downgrade(&exi->exi_cache_lock);
953 
954 			avl_destroy(&nc->authc_tree);
955 			rw_destroy(&nc->authc_lock);
956 			kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
957 			kmem_free(nc, sizeof (*nc));
958 		}
959 	}
960 
961 	ASSERT(c != NULL);
962 
963 	rw_enter(&c->authc_lock, RW_READER);
964 
965 	ac.auth_flavor = flavor;
966 	ac.auth_clnt_cred = cr;
967 
968 	p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
969 
970 	if (p == NULL) {
971 		struct auth_cache *np;
972 
973 		rw_exit(&c->authc_lock);
974 
975 		np = kmem_cache_alloc(exi_cache_handle,
976 		    KM_NOSLEEP | KM_NORMALPRI);
977 		if (np == NULL) {
978 			rw_exit(&exi->exi_cache_lock);
979 			goto retrieve;
980 		}
981 
982 		/*
983 		 * Initialize the new auth_cache
984 		 */
985 		np->auth_clnt = c;
986 		np->auth_flavor = flavor;
987 		np->auth_clnt_cred = crdup(cr);
988 		np->auth_srv_ngids = 0;
989 		np->auth_srv_gids = NULL;
990 		np->auth_time = np->auth_freshness = gethrestime_sec();
991 		np->auth_state = NFS_AUTH_NEW;
992 		mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
993 		cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
994 
995 		rw_enter(&c->authc_lock, RW_WRITER);
996 		rw_exit(&exi->exi_cache_lock);
997 
998 		p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
999 		if (p == NULL) {
1000 			avl_insert(&c->authc_tree, np, where);
1001 			rw_downgrade(&c->authc_lock);
1002 			p = np;
1003 		} else {
1004 			rw_downgrade(&c->authc_lock);
1005 
1006 			cv_destroy(&np->auth_cv);
1007 			mutex_destroy(&np->auth_lock);
1008 			crfree(np->auth_clnt_cred);
1009 			kmem_cache_free(exi_cache_handle, np);
1010 		}
1011 	} else {
1012 		rw_exit(&exi->exi_cache_lock);
1013 	}
1014 
1015 	mutex_enter(&p->auth_lock);
1016 	rw_exit(&c->authc_lock);
1017 
1018 	/*
1019 	 * If the entry is in the WAITING state then some other thread is just
1020 	 * retrieving the required info.  The entry was either NEW, or the list
1021 	 * of client's supplemental groups is going to be changed (either by
1022 	 * this thread, or by some other thread).  We need to wait until the
1023 	 * nfsauth_retrieve() is done.
1024 	 */
1025 	while (p->auth_state == NFS_AUTH_WAITING)
1026 		cv_wait(&p->auth_cv, &p->auth_lock);
1027 
1028 	/*
1029 	 * Here the entry cannot be in WAITING or INVALID state.
1030 	 */
1031 	ASSERT(p->auth_state != NFS_AUTH_WAITING);
1032 	ASSERT(p->auth_state != NFS_AUTH_INVALID);
1033 
1034 	/*
1035 	 * If the cache entry is not valid yet, we need to retrieve the
1036 	 * info ourselves.
1037 	 */
1038 	if (p->auth_state == NFS_AUTH_NEW) {
1039 		bool_t res;
1040 		/*
1041 		 * NFS_AUTH_NEW is the default output auth_state value in a
1042 		 * case we failed somewhere below.
1043 		 */
1044 		auth_state_t state = NFS_AUTH_NEW;
1045 
1046 		p->auth_state = NFS_AUTH_WAITING;
1047 		mutex_exit(&p->auth_lock);
1048 		kmem_free(addr.buf, addr.maxlen);
1049 		addr = p->auth_clnt->authc_addr;
1050 
1051 		atomic_inc_uint(&nfsauth_cache_miss);
1052 
1053 		res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1054 		    flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1055 		    &tmpgids);
1056 
1057 		p->auth_access = access;
1058 		p->auth_time = p->auth_freshness = gethrestime_sec();
1059 
1060 		if (res == TRUE) {
1061 			if (uid != NULL)
1062 				*uid = tmpuid;
1063 			if (gid != NULL)
1064 				*gid = tmpgid;
1065 			if (ngids != NULL && gids != NULL) {
1066 				*ngids = tmpngids;
1067 				*gids = tmpgids;
1068 
1069 				/*
1070 				 * We need a copy of gids for the
1071 				 * auth_cache entry
1072 				 */
1073 				tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1074 				    KM_NOSLEEP | KM_NORMALPRI);
1075 				if (tmpgids != NULL)
1076 					bcopy(*gids, tmpgids,
1077 					    tmpngids * sizeof (gid_t));
1078 			}
1079 
1080 			if (tmpgids != NULL || tmpngids == 0) {
1081 				p->auth_srv_uid = tmpuid;
1082 				p->auth_srv_gid = tmpgid;
1083 				p->auth_srv_ngids = tmpngids;
1084 				p->auth_srv_gids = tmpgids;
1085 
1086 				state = NFS_AUTH_FRESH;
1087 			}
1088 		}
1089 
1090 		/*
1091 		 * Set the auth_state and notify waiters.
1092 		 */
1093 		mutex_enter(&p->auth_lock);
1094 		p->auth_state = state;
1095 		cv_broadcast(&p->auth_cv);
1096 		mutex_exit(&p->auth_lock);
1097 	} else {
1098 		uint_t nach;
1099 		time_t refresh;
1100 
1101 		refresh = gethrestime_sec() - p->auth_freshness;
1102 
1103 		p->auth_time = gethrestime_sec();
1104 
1105 		if (uid != NULL)
1106 			*uid = p->auth_srv_uid;
1107 		if (gid != NULL)
1108 			*gid = p->auth_srv_gid;
1109 		if (ngids != NULL && gids != NULL) {
1110 			if ((*ngids = p->auth_srv_ngids) != 0) {
1111 				size_t sz = *ngids * sizeof (gid_t);
1112 				*gids = kmem_alloc(sz, KM_SLEEP);
1113 				bcopy(p->auth_srv_gids, *gids, sz);
1114 			} else {
1115 				*gids = NULL;
1116 			}
1117 		}
1118 
1119 		access = p->auth_access;
1120 
1121 		if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1122 		    p->auth_state == NFS_AUTH_FRESH) {
1123 			refreshq_auth_node_t *ran;
1124 			uint_t nacr;
1125 
1126 			p->auth_state = NFS_AUTH_STALE;
1127 			mutex_exit(&p->auth_lock);
1128 
1129 			nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1130 			DTRACE_PROBE3(nfsauth__debug__cache__stale,
1131 			    struct exportinfo *, exi,
1132 			    struct auth_cache *, p,
1133 			    uint_t, nacr);
1134 
1135 			ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1136 			    KM_SLEEP);
1137 			ran->ran_auth = p;
1138 			ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1139 
1140 			mutex_enter(&nag->refreshq_lock);
1141 
1142 			if (nag->refreshq_thread_state ==
1143 			    REFRESHQ_THREAD_NEED_CREATE) {
1144 				/* Launch nfsauth refresh thread */
1145 				nag->refreshq_thread_state =
1146 				    REFRESHQ_THREAD_RUNNING;
1147 				(void) zthread_create(NULL, 0,
1148 				    nfsauth_refresh_thread, nag, 0,
1149 				    minclsyspri);
1150 			}
1151 
1152 			/*
1153 			 * We should not add a work queue item if the thread
1154 			 * is not accepting them.
1155 			 */
1156 			if (nag->refreshq_thread_state ==
1157 			    REFRESHQ_THREAD_RUNNING) {
1158 				refreshq_exi_node_t *ren;
1159 
1160 				/*
1161 				 * Is there an existing exi_list?
1162 				 */
1163 				for (ren = list_head(&nag->refreshq_queue);
1164 				    ren != NULL;
1165 				    ren = list_next(&nag->refreshq_queue,
1166 				    ren)) {
1167 					if (ren->ren_exi == exi) {
1168 						list_insert_tail(
1169 						    &ren->ren_authlist, ran);
1170 						break;
1171 					}
1172 				}
1173 
1174 				if (ren == NULL) {
1175 					ren = kmem_alloc(
1176 					    sizeof (refreshq_exi_node_t),
1177 					    KM_SLEEP);
1178 
1179 					exi_hold(exi);
1180 					ren->ren_exi = exi;
1181 
1182 					list_create(&ren->ren_authlist,
1183 					    sizeof (refreshq_auth_node_t),
1184 					    offsetof(refreshq_auth_node_t,
1185 					    ran_node));
1186 
1187 					list_insert_tail(&ren->ren_authlist,
1188 					    ran);
1189 					list_insert_tail(&nag->refreshq_queue,
1190 					    ren);
1191 				}
1192 
1193 				cv_broadcast(&nag->refreshq_cv);
1194 			} else {
1195 				strfree(ran->ran_netid);
1196 				kmem_free(ran, sizeof (refreshq_auth_node_t));
1197 			}
1198 
1199 			mutex_exit(&nag->refreshq_lock);
1200 		} else {
1201 			mutex_exit(&p->auth_lock);
1202 		}
1203 
1204 		nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1205 		DTRACE_PROBE2(nfsauth__debug__cache__hit,
1206 		    uint_t, nach,
1207 		    time_t, refresh);
1208 
1209 		kmem_free(addr.buf, addr.maxlen);
1210 	}
1211 
1212 	return (access);
1213 
1214 retrieve:
1215 
1216 	/*
1217 	 * Retrieve the required data without caching.
1218 	 */
1219 
1220 	ASSERT(p == NULL);
1221 
1222 	atomic_inc_uint(&nfsauth_cache_miss);
1223 
1224 	if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1225 	    &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1226 		if (uid != NULL)
1227 			*uid = tmpuid;
1228 		if (gid != NULL)
1229 			*gid = tmpgid;
1230 		if (ngids != NULL && gids != NULL) {
1231 			*ngids = tmpngids;
1232 			*gids = tmpgids;
1233 		} else {
1234 			kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1235 		}
1236 	}
1237 
1238 	kmem_free(addr.buf, addr.maxlen);
1239 
1240 	return (access);
1241 }
1242 
1243 /*
1244  * Check if the requesting client has access to the filesystem with
1245  * a given nfs flavor number which is an explicitly shared flavor.
1246  */
1247 int
1248 nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
1249     int flavor, int perm, cred_t *cr)
1250 {
1251 	int access;
1252 
1253 	if (! (perm & M_4SEC_EXPORTED)) {
1254 		return (NFSAUTH_DENIED);
1255 	}
1256 
1257 	/*
1258 	 * Optimize if there are no lists
1259 	 */
1260 	if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
1261 		perm &= ~M_4SEC_EXPORTED;
1262 		if (perm == M_RO)
1263 			return (NFSAUTH_RO);
1264 		if (perm == M_RW)
1265 			return (NFSAUTH_RW);
1266 	}
1267 
1268 	access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
1269 	    NULL);
1270 
1271 	return (access);
1272 }
1273 
1274 int
1275 nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
1276     uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
1277 {
1278 	int access, mapaccess;
1279 	struct secinfo *sp;
1280 	int i, flavor, perm;
1281 	int authnone_entry = -1;
1282 
1283 	/*
1284 	 * By default root is mapped to anonymous user.
1285 	 * This might get overriden later in nfsauth_cache_get().
1286 	 */
1287 	if (crgetuid(cr) == 0) {
1288 		if (uid != NULL)
1289 			*uid = exi->exi_export.ex_anon;
1290 		if (gid != NULL)
1291 			*gid = exi->exi_export.ex_anon;
1292 	} else {
1293 		if (uid != NULL)
1294 			*uid = crgetuid(cr);
1295 		if (gid != NULL)
1296 			*gid = crgetgid(cr);
1297 	}
1298 
1299 	if (ngids != NULL)
1300 		*ngids = 0;
1301 	if (gids != NULL)
1302 		*gids = NULL;
1303 
1304 	/*
1305 	 *  Get the nfs flavor number from xprt.
1306 	 */
1307 	flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1308 
1309 	/*
1310 	 * First check the access restrictions on the filesystem.  If
1311 	 * there are no lists associated with this flavor then there's no
1312 	 * need to make an expensive call to the nfsauth service or to
1313 	 * cache anything.
1314 	 */
1315 
1316 	sp = exi->exi_export.ex_secinfo;
1317 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1318 		if (flavor != sp[i].s_secinfo.sc_nfsnum) {
1319 			if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1320 				authnone_entry = i;
1321 			continue;
1322 		}
1323 		break;
1324 	}
1325 
1326 	mapaccess = 0;
1327 
1328 	if (i >= exi->exi_export.ex_seccnt) {
1329 		/*
1330 		 * Flavor not found, but use AUTH_NONE if it exists
1331 		 */
1332 		if (authnone_entry == -1)
1333 			return (NFSAUTH_DENIED);
1334 		flavor = AUTH_NONE;
1335 		mapaccess = NFSAUTH_MAPNONE;
1336 		i = authnone_entry;
1337 	}
1338 
1339 	/*
1340 	 * If the flavor is in the ex_secinfo list, but not an explicitly
1341 	 * shared flavor by the user, it is a result of the nfsv4 server
1342 	 * namespace setup. We will grant an RO permission similar for
1343 	 * a pseudo node except that this node is a shared one.
1344 	 *
1345 	 * e.g. flavor in (flavor) indicates that it is not explictly
1346 	 *	shared by the user:
1347 	 *
1348 	 *		/	(sys, krb5)
1349 	 *		|
1350 	 *		export  #share -o sec=sys (krb5)
1351 	 *		|
1352 	 *		secure  #share -o sec=krb5
1353 	 *
1354 	 *	In this case, when a krb5 request coming in to access
1355 	 *	/export, RO permission is granted.
1356 	 */
1357 	if (!(sp[i].s_flags & M_4SEC_EXPORTED))
1358 		return (mapaccess | NFSAUTH_RO);
1359 
1360 	/*
1361 	 * Optimize if there are no lists.
1362 	 * We cannot optimize for AUTH_SYS with NGRPS (16) supplemental groups.
1363 	 */
1364 	perm = sp[i].s_flags;
1365 	if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
1366 	    flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
1367 		perm &= ~M_4SEC_EXPORTED;
1368 		if (perm == M_RO)
1369 			return (mapaccess | NFSAUTH_RO);
1370 		if (perm == M_RW)
1371 			return (mapaccess | NFSAUTH_RW);
1372 	}
1373 
1374 	access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
1375 
1376 	/*
1377 	 * For both NFSAUTH_DENIED and NFSAUTH_WRONGSEC we do not care about
1378 	 * the supplemental groups.
1379 	 */
1380 	if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
1381 		if (ngids != NULL && gids != NULL) {
1382 			kmem_free(*gids, *ngids * sizeof (gid_t));
1383 			*ngids = 0;
1384 			*gids = NULL;
1385 		}
1386 	}
1387 
1388 	/*
1389 	 * Client's security flavor doesn't match with "ro" or
1390 	 * "rw" list. Try again using AUTH_NONE if present.
1391 	 */
1392 	if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
1393 		/*
1394 		 * Have we already encountered AUTH_NONE ?
1395 		 */
1396 		if (authnone_entry != -1) {
1397 			mapaccess = NFSAUTH_MAPNONE;
1398 			access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
1399 			    NULL, NULL, NULL, NULL);
1400 		} else {
1401 			/*
1402 			 * Check for AUTH_NONE presence.
1403 			 */
1404 			for (; i < exi->exi_export.ex_seccnt; i++) {
1405 				if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1406 					mapaccess = NFSAUTH_MAPNONE;
1407 					access = nfsauth_cache_get(exi, req,
1408 					    AUTH_NONE, cr, NULL, NULL, NULL,
1409 					    NULL);
1410 					break;
1411 				}
1412 			}
1413 		}
1414 	}
1415 
1416 	if (access & NFSAUTH_DENIED)
1417 		access = NFSAUTH_DENIED;
1418 
1419 	return (access | mapaccess);
1420 }
1421 
1422 static void
1423 nfsauth_free_clnt_node(struct auth_cache_clnt *p)
1424 {
1425 	void *cookie = NULL;
1426 	struct auth_cache *node;
1427 
1428 	while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
1429 		nfsauth_free_node(node);
1430 	avl_destroy(&p->authc_tree);
1431 
1432 	kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
1433 	rw_destroy(&p->authc_lock);
1434 
1435 	kmem_free(p, sizeof (*p));
1436 }
1437 
1438 static void
1439 nfsauth_free_node(struct auth_cache *p)
1440 {
1441 	crfree(p->auth_clnt_cred);
1442 	kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
1443 	mutex_destroy(&p->auth_lock);
1444 	cv_destroy(&p->auth_cv);
1445 	kmem_cache_free(exi_cache_handle, p);
1446 }
1447 
1448 /*
1449  * Free the nfsauth cache for a given export
1450  */
1451 void
1452 nfsauth_cache_free(struct exportinfo *exi)
1453 {
1454 	int i;
1455 
1456 	/*
1457 	 * The only way we got here was with an exi_rele, which means that no
1458 	 * auth cache entry is being refreshed.
1459 	 */
1460 
1461 	for (i = 0; i < AUTH_TABLESIZE; i++) {
1462 		avl_tree_t *tree = exi->exi_cache[i];
1463 		void *cookie = NULL;
1464 		struct auth_cache_clnt *node;
1465 
1466 		while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1467 			nfsauth_free_clnt_node(node);
1468 	}
1469 }
1470 
1471 /*
1472  * Called by the kernel memory allocator when memory is low.
1473  * Free unused cache entries. If that's not enough, the VM system
1474  * will call again for some more.
1475  *
1476  * This needs to operate on all zones, so we take a reader lock
1477  * on the list of zones and walk the list.  This is OK here
1478  * becuase exi_cache_trim doesn't block or cause new objects
1479  * to be allocated (basically just frees lots of stuff).
1480  * Use care if nfssrv_globals_rwl is taken as reader in any
1481  * other cases because it will block nfs_server_zone_init
1482  * and nfs_server_zone_fini, which enter as writer.
1483  */
1484 /*ARGSUSED*/
1485 void
1486 exi_cache_reclaim(void *cdrarg)
1487 {
1488 	nfs_globals_t *ng;
1489 
1490 	rw_enter(&nfssrv_globals_rwl, RW_READER);
1491 
1492 	ng = list_head(&nfssrv_globals_list);
1493 	while (ng != NULL) {
1494 		exi_cache_reclaim_zone(ng);
1495 		ng = list_next(&nfssrv_globals_list, ng);
1496 	}
1497 
1498 	rw_exit(&nfssrv_globals_rwl);
1499 }
1500 
1501 static void
1502 exi_cache_reclaim_zone(nfs_globals_t *ng)
1503 {
1504 	int i;
1505 	struct exportinfo *exi;
1506 	nfs_export_t *ne = ng->nfs_export;
1507 
1508 	rw_enter(&ne->exported_lock, RW_READER);
1509 
1510 	for (i = 0; i < EXPTABLESIZE; i++) {
1511 		for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1512 			exi_cache_trim(exi);
1513 	}
1514 
1515 	rw_exit(&ne->exported_lock);
1516 
1517 	atomic_inc_uint(&nfsauth_cache_reclaim);
1518 }
1519 
1520 static void
1521 exi_cache_trim(struct exportinfo *exi)
1522 {
1523 	struct auth_cache_clnt *c;
1524 	struct auth_cache_clnt *nextc;
1525 	struct auth_cache *p;
1526 	struct auth_cache *next;
1527 	int i;
1528 	time_t stale_time;
1529 	avl_tree_t *tree;
1530 
1531 	for (i = 0; i < AUTH_TABLESIZE; i++) {
1532 		tree = exi->exi_cache[i];
1533 		stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1534 		rw_enter(&exi->exi_cache_lock, RW_READER);
1535 
1536 		/*
1537 		 * Free entries that have not been
1538 		 * used for NFSAUTH_CACHE_TRIM seconds.
1539 		 */
1540 		for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
1541 			/*
1542 			 * We are being called by the kmem subsystem to reclaim
1543 			 * memory so don't block if we can't get the lock.
1544 			 */
1545 			if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
1546 				exi_cache_auth_reclaim_failed++;
1547 				rw_exit(&exi->exi_cache_lock);
1548 				return;
1549 			}
1550 
1551 			for (p = avl_first(&c->authc_tree); p != NULL;
1552 			    p = next) {
1553 				next = AVL_NEXT(&c->authc_tree, p);
1554 
1555 				ASSERT(p->auth_state != NFS_AUTH_INVALID);
1556 
1557 				mutex_enter(&p->auth_lock);
1558 
1559 				/*
1560 				 * We won't trim recently used and/or WAITING
1561 				 * entries.
1562 				 */
1563 				if (p->auth_time > stale_time ||
1564 				    p->auth_state == NFS_AUTH_WAITING) {
1565 					mutex_exit(&p->auth_lock);
1566 					continue;
1567 				}
1568 
1569 				DTRACE_PROBE1(nfsauth__debug__trim__state,
1570 				    auth_state_t, p->auth_state);
1571 
1572 				/*
1573 				 * STALE and REFRESHING entries needs to be
1574 				 * marked INVALID only because they are
1575 				 * referenced by some other structures or
1576 				 * threads.  They will be freed later.
1577 				 */
1578 				if (p->auth_state == NFS_AUTH_STALE ||
1579 				    p->auth_state == NFS_AUTH_REFRESHING) {
1580 					p->auth_state = NFS_AUTH_INVALID;
1581 					mutex_exit(&p->auth_lock);
1582 
1583 					avl_remove(&c->authc_tree, p);
1584 				} else {
1585 					mutex_exit(&p->auth_lock);
1586 
1587 					avl_remove(&c->authc_tree, p);
1588 					nfsauth_free_node(p);
1589 				}
1590 			}
1591 			rw_exit(&c->authc_lock);
1592 		}
1593 
1594 		if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
1595 			rw_exit(&exi->exi_cache_lock);
1596 			exi_cache_clnt_reclaim_failed++;
1597 			continue;
1598 		}
1599 
1600 		for (c = avl_first(tree); c != NULL; c = nextc) {
1601 			nextc = AVL_NEXT(tree, c);
1602 
1603 			if (avl_is_empty(&c->authc_tree) == B_FALSE)
1604 				continue;
1605 
1606 			avl_remove(tree, c);
1607 
1608 			nfsauth_free_clnt_node(c);
1609 		}
1610 
1611 		rw_exit(&exi->exi_cache_lock);
1612 	}
1613 }
1614