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