xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_export.c (revision 6a074c93c5dee390d8ca2377f42e55418f0a9eb3)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *  	Copyright 1983, 1984, 1985, 1986, 1987, 1988, 1989  AT&T.
28  *		All rights reserved.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/vfs.h>
38 #include <sys/vnode.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 #include <sys/uio.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/file.h>
45 #include <sys/tiuser.h>
46 #include <sys/kmem.h>
47 #include <sys/pathname.h>
48 #include <sys/debug.h>
49 #include <sys/vtrace.h>
50 #include <sys/cmn_err.h>
51 #include <sys/acl.h>
52 #include <sys/utsname.h>
53 #include <sys/sdt.h>
54 #include <netinet/in.h>
55 
56 #include <rpc/types.h>
57 #include <rpc/auth.h>
58 #include <rpc/svc.h>
59 
60 #include <nfs/nfs.h>
61 #include <nfs/export.h>
62 #include <nfs/nfssys.h>
63 #include <nfs/nfs_clnt.h>
64 #include <nfs/nfs_acl.h>
65 #include <nfs/nfs_log.h>
66 #include <nfs/lm.h>
67 
68 #define	EXPTABLESIZE 16
69 
70 struct exportinfo *exptable[EXPTABLESIZE];
71 
72 static int	unexport(fsid_t *, fid_t *, vnode_t *);
73 static void	exportfree(exportinfo_t *);
74 static int	loadindex(exportdata_t *);
75 
76 extern void	nfsauth_cache_free(exportinfo_t *);
77 extern int	sec_svc_loadrootnames(int, int, caddr_t **, model_t);
78 extern void	sec_svc_freerootnames(int, int, caddr_t *);
79 
80 static int build_seclist_nodups(exportdata_t *, secinfo_t *, int);
81 static void srv_secinfo_add(secinfo_t **, int *, secinfo_t *, int, int);
82 static void srv_secinfo_remove(secinfo_t **, int *, secinfo_t *, int);
83 static void pseudo_secinfo_add(exportinfo_t *, exp_visible_t *, exportinfo_t *);
84 static void pseudo_secinfo_remove(exportinfo_t *, exp_visible_t *);
85 static void free_pseudoanc(struct exp_visible *);
86 static int srv_secinfo_treeclimb(exportinfo_t *, secinfo_t *, int, int);
87 
88 #ifdef VOLATILE_FH_TEST
89 static struct ex_vol_rename *find_volrnm_fh(exportinfo_t *, nfs_fh4 *);
90 static uint32_t find_volrnm_fh_id(exportinfo_t *, nfs_fh4 *);
91 static void free_volrnm_list(exportinfo_t *);
92 #endif /* VOLATILE_FH_TEST */
93 
94 /*
95  * exported_lock	Read/Write lock that protects the exportinfo list.
96  *			This lock must be held when searching or modifiying
97  *			the exportinfo list.
98  */
99 krwlock_t exported_lock;
100 
101 /*
102  * "public" and default (root) location for public filehandle
103  */
104 struct exportinfo *exi_public, *exi_root;
105 
106 fid_t exi_rootfid;	/* for checking the default public file handle */
107 
108 fhandle_t nullfh2;	/* for comparing V2 filehandles */
109 
110 /*
111  * macro for static dtrace probes to trace server namespace ref count mods.
112  */
113 #define	SECREF_TRACE(seclist, tag, flav, aftcnt) \
114 	DTRACE_PROBE4(nfss__d__nmspc__secref, struct secinfo *, (seclist), \
115 		char *, (tag), int, (int)(flav), int, (int)(aftcnt))
116 
117 
118 #define	exptablehash(fsid, fid) (nfs_fhhash((fsid), (fid)) & (EXPTABLESIZE - 1))
119 
120 /*
121  * File handle hash function, good for producing hash values 16 bits wide.
122  */
123 int
124 nfs_fhhash(fsid_t *fsid, fid_t *fid)
125 {
126 	short *data;
127 	int i, len;
128 	short h;
129 
130 	ASSERT(fid != NULL);
131 
132 	data = (short *)fid->fid_data;
133 
134 	/* fid_data must be aligned on a short */
135 	ASSERT((((uintptr_t)data) & (sizeof (short) - 1)) == 0);
136 
137 	if (fid->fid_len == 10) {
138 		/*
139 		 * probably ufs: hash on bytes 4,5 and 8,9
140 		 */
141 		return (fsid->val[0] ^ data[2] ^ data[4]);
142 	}
143 
144 	if (fid->fid_len == 6) {
145 		/*
146 		 * probably hsfs: hash on bytes 0,1 and 4,5
147 		 */
148 		return ((fsid->val[0] ^ data[0] ^ data[2]));
149 	}
150 
151 	/*
152 	 * Some other file system. Assume that every byte is
153 	 * worth hashing.
154 	 */
155 	h = (short)fsid->val[0];
156 
157 	/*
158 	 * Sanity check the length before using it
159 	 * blindly in case the client trashed it.
160 	 */
161 	if (fid->fid_len > NFS_FHMAXDATA)
162 		len = 0;
163 	else
164 		len = fid->fid_len / sizeof (short);
165 
166 	/*
167 	 * This will ignore one byte if len is not a multiple of
168 	 * of sizeof (short). No big deal since we at least get some
169 	 * variation with fsid->val[0];
170 	 */
171 	for (i = 0; i < len; i++)
172 		h ^= data[i];
173 
174 	return ((int)h);
175 }
176 
177 /*
178  * Free the memory allocated within a secinfo entry.
179  */
180 void
181 srv_secinfo_entry_free(struct secinfo *secp)
182 {
183 	if (secp->s_rootcnt > 0 && secp->s_rootnames != NULL) {
184 		sec_svc_freerootnames(secp->s_secinfo.sc_rpcnum,
185 		    secp->s_rootcnt, secp->s_rootnames);
186 		secp->s_rootcnt = 0;
187 	}
188 
189 	if ((secp->s_secinfo.sc_rpcnum == RPCSEC_GSS) &&
190 	    (secp->s_secinfo.sc_gss_mech_type)) {
191 		kmem_free(secp->s_secinfo.sc_gss_mech_type->elements,
192 		    secp->s_secinfo.sc_gss_mech_type->length);
193 		kmem_free(secp->s_secinfo.sc_gss_mech_type,
194 		    sizeof (rpc_gss_OID_desc));
195 		secp->s_secinfo.sc_gss_mech_type = NULL;
196 	}
197 
198 }
199 
200 /*
201  * Free a list of secinfo allocated in the exportdata structure.
202  */
203 void
204 srv_secinfo_list_free(struct secinfo *secinfo, int cnt)
205 {
206 	int i;
207 
208 	if (cnt == 0)
209 		return;
210 
211 	for (i = 0; i < cnt; i++)
212 		srv_secinfo_entry_free(&secinfo[i]);
213 
214 	kmem_free(secinfo, cnt * sizeof (struct secinfo));
215 }
216 
217 /*
218  * Allocate and copy a secinfo data from "from" to "to".
219  *
220  * This routine is used by srv_secinfo_add() to add a new flavor to an
221  * ancestor's export node. The rootnames are not copied because the
222  * allowable rootname access only applies to the explicit exported node,
223  * not its ancestor's.
224  *
225  * "to" should have already been allocated and zeroed before calling
226  * this routine.
227  *
228  * This routine is used under the protection of exported_lock (RW_WRITER).
229  */
230 void
231 srv_secinfo_copy(struct secinfo *from, struct secinfo *to)
232 {
233 	to->s_secinfo.sc_nfsnum = from->s_secinfo.sc_nfsnum;
234 	to->s_secinfo.sc_rpcnum = from->s_secinfo.sc_rpcnum;
235 
236 	if (from->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
237 		to->s_secinfo.sc_service = from->s_secinfo.sc_service;
238 		bcopy(from->s_secinfo.sc_name, to->s_secinfo.sc_name,
239 		    strlen(from->s_secinfo.sc_name));
240 		bcopy(from->s_secinfo.sc_gss_mech, to->s_secinfo.sc_gss_mech,
241 		    strlen(from->s_secinfo.sc_gss_mech));
242 
243 		/* copy mechanism oid */
244 		to->s_secinfo.sc_gss_mech_type =
245 		    kmem_alloc(sizeof (rpc_gss_OID_desc), KM_SLEEP);
246 		to->s_secinfo.sc_gss_mech_type->length =
247 		    from->s_secinfo.sc_gss_mech_type->length;
248 		to->s_secinfo.sc_gss_mech_type->elements =
249 		    kmem_alloc(from->s_secinfo.sc_gss_mech_type->length,
250 		    KM_SLEEP);
251 		bcopy(from->s_secinfo.sc_gss_mech_type->elements,
252 		    to->s_secinfo.sc_gss_mech_type->elements,
253 		    from->s_secinfo.sc_gss_mech_type->length);
254 	}
255 
256 	to->s_refcnt = from->s_refcnt;
257 	to->s_window = from->s_window;
258 	/* no need to copy the mode bits - s_flags */
259 }
260 
261 /*
262  * Create a secinfo array without duplicates.  The condensed
263  * flavor list is used to propagate flavor ref counts  to an
264  * export's ancestor pseudonodes.
265  */
266 static int
267 build_seclist_nodups(exportdata_t *exd, secinfo_t *nodups, int exponly)
268 {
269 	int ccnt, c;
270 	int ncnt, n;
271 	struct secinfo *cursec;
272 
273 	ncnt = 0;
274 	ccnt = exd->ex_seccnt;
275 	cursec = exd->ex_secinfo;
276 
277 	for (c = 0; c < ccnt; c++) {
278 
279 		if (exponly && ! SEC_REF_EXPORTED(&cursec[c]))
280 			continue;
281 
282 		for (n = 0; n < ncnt; n++) {
283 			if (nodups[n].s_secinfo.sc_nfsnum ==
284 			    cursec[c].s_secinfo.sc_nfsnum)
285 				break;
286 		}
287 
288 		/*
289 		 * The structure copy below also copys ptrs embedded
290 		 * within struct secinfo.  The ptrs are copied but
291 		 * they are never freed from the nodups array.  If
292 		 * an ancestor's secinfo array doesn't contain one
293 		 * of the nodups flavors, then the entry is properly
294 		 * copied into the ancestor's secinfo array.
295 		 * (see srv_secinfo_copy)
296 		 */
297 		if (n == ncnt) {
298 			nodups[n] = cursec[c];
299 			ncnt++;
300 		}
301 	}
302 	return (ncnt);
303 }
304 
305 /*
306  * Add the new security flavors from newdata to the current list, pcursec.
307  * Upon return, *pcursec has the newly merged secinfo list.
308  *
309  * There should be at least 1 secinfo entry in newsec.
310  *
311  * This routine is used under the protection of exported_lock (RW_WRITER).
312  */
313 static void
314 srv_secinfo_add(secinfo_t **pcursec, int *pcurcnt, secinfo_t *newsec,
315     int newcnt, int is_pseudo)
316 {
317 	int ccnt, c;		/* sec count in current data - curdata */
318 	int n;			/* index for newsec  - newsecinfo */
319 	int tcnt;		/* total sec count after merge */
320 	int mcnt;		/* total sec count after merge */
321 	struct secinfo *msec;	/* merged secinfo list */
322 	struct secinfo *cursec;
323 
324 	cursec = *pcursec;
325 	ccnt = *pcurcnt;
326 
327 	ASSERT(newcnt > 0);
328 	tcnt = ccnt + newcnt;
329 
330 	for (n = 0; n < newcnt; n++) {
331 		for (c = 0; c < ccnt; c++) {
332 			if (newsec[n].s_secinfo.sc_nfsnum ==
333 			    cursec[c].s_secinfo.sc_nfsnum) {
334 				cursec[c].s_refcnt++;
335 				SECREF_TRACE(cursec, "add_ref",
336 				    cursec[c].s_secinfo.sc_nfsnum,
337 				    cursec[c].s_refcnt);
338 				tcnt--;
339 				break;
340 			}
341 		}
342 	}
343 
344 	if (tcnt == ccnt)
345 		return; /* no change; no new flavors */
346 
347 	msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
348 
349 	/* move current secinfo list data to the new list */
350 	for (c = 0; c < ccnt; c++)
351 		msec[c] = cursec[c];
352 
353 	/* Add the flavor that's not in the current data */
354 	mcnt = ccnt;
355 	for (n = 0; n < newcnt; n++) {
356 		for (c = 0; c < ccnt; c++) {
357 			if (newsec[n].s_secinfo.sc_nfsnum ==
358 			    cursec[c].s_secinfo.sc_nfsnum)
359 				break;
360 		}
361 
362 		/* This is the one. Add it. */
363 		if (c == ccnt) {
364 			srv_secinfo_copy(&newsec[n], &msec[mcnt]);
365 
366 			if (is_pseudo)
367 				msec[mcnt].s_flags = M_RO;
368 
369 			SECREF_TRACE(msec, "new_ref",
370 			    msec[mcnt].s_secinfo.sc_nfsnum,
371 			    msec[mcnt].s_refcnt);
372 			mcnt++;
373 		}
374 	}
375 
376 	ASSERT(mcnt == tcnt);
377 
378 	/*
379 	 * Done. Update curdata. Free the old secinfo list in
380 	 * curdata and return the new sec array info
381 	 */
382 	if (ccnt > 0)
383 		kmem_free(cursec, ccnt * sizeof (struct secinfo));
384 	*pcurcnt = tcnt;
385 	*pcursec = msec;
386 }
387 
388 /*
389  * For NFS V4.
390  * Remove the security data of the unexported node from its ancestors.
391  * Assume there is at least one flavor entry in the current sec list
392  * (pcursec).
393  *
394  * This routine is used under the protection of exported_lock (RW_WRITER).
395  *
396  * Every element of remsec is an explicitly exported flavor.  If
397  * srv_secinfo_remove() is called fom an exportfs error path, then
398  * the flavor list was derived from the user's share cmdline,
399  * and all flavors are explicit.  If it was called from the unshare path,
400  * build_seclist_nodups() was called with the exponly flag.
401  */
402 static void
403 srv_secinfo_remove(secinfo_t **pcursec, int *pcurcnt, secinfo_t *remsec,
404     int remcnt)
405 {
406 	int ccnt, c;		/* sec count in current data - curdata */
407 	int r;			/* sec count in removal data - remsecinfo */
408 	int tcnt, mcnt;		/* total sec count after removing */
409 	struct secinfo *msec;	/* final secinfo list after removing */
410 	struct secinfo *cursec;
411 
412 	cursec = *pcursec;
413 	ccnt = *pcurcnt;
414 	tcnt = ccnt;
415 
416 	for (r = 0; r < remcnt; r++) {
417 		/*
418 		 * Remove a flavor only if the flavor was a shared flavor for
419 		 * the remsecinfo exported node that's being unshared.
420 		 * Otherwise, this flavor is for the children of remsecinfo,
421 		 * need to keep it.
422 		 */
423 		for (c = 0; c < ccnt; c++) {
424 			if (remsec[r].s_secinfo.sc_nfsnum ==
425 			    cursec[c].s_secinfo.sc_nfsnum) {
426 
427 				/*
428 				 * Decrement secinfo reference count by 1.
429 				 * If this entry is invalid after decrementing
430 				 * the count (i.e. count < 1), this entry will
431 				 * be removed.
432 				 */
433 				cursec[c].s_refcnt--;
434 
435 				SECREF_TRACE(cursec, "del_ref",
436 				    cursec[c].s_secinfo.sc_nfsnum,
437 				    cursec[c].s_refcnt);
438 
439 				ASSERT(cursec[c].s_refcnt >= 0);
440 
441 				if (SEC_REF_INVALID(&cursec[c]))
442 					tcnt--;
443 				break;
444 			}
445 		}
446 	}
447 
448 	ASSERT(tcnt >= 0);
449 	if (tcnt == ccnt)
450 		return; /* no change; no flavors to remove */
451 
452 	if (tcnt == 0) {
453 		srv_secinfo_list_free(cursec, ccnt);
454 		*pcurcnt = 0;
455 		*pcursec = NULL;
456 		return;
457 	}
458 
459 	msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
460 
461 	/* walk thru the given secinfo list to remove the flavors */
462 	mcnt = 0;
463 	for (c = 0; c < ccnt; c++) {
464 		if (SEC_REF_INVALID(&cursec[c])) {
465 			srv_secinfo_entry_free(&cursec[c]);
466 		} else {
467 			msec[mcnt] = cursec[c];
468 			mcnt++;
469 		}
470 	}
471 
472 	ASSERT(mcnt == tcnt);
473 	/*
474 	 * Done. Update curdata.
475 	 * Free the existing secinfo list in curdata. All pointers
476 	 * within the list have either been moved to msec or freed
477 	 * if it's invalid.
478 	 */
479 	kmem_free(*pcursec, ccnt * sizeof (struct secinfo));
480 	*pcursec = msec;
481 	*pcurcnt = tcnt;
482 }
483 
484 
485 /*
486  * pseudo_secinfo_add
487  *	ancexi: ancestor exportinfo
488  *	ancpseudo: linked list of ancestor pseudnodes
489  *	newexi: new export that needs to inherit implicitly allowed flavors
490  *
491  * Increment secflavor ref counts in ancestor pseudonodes.  If newexi
492  * is non-NULL, then also set newexi's implicitly allowed flavors by
493  * copy the flavors in its pseudonode.
494  *
495  * There's no work to do if either the ancexi's pseudonode list
496  * or ancpseudo is empty.
497  *
498  * ancexi->exi_visible could be NULL because sec flavor refs are
499  * propagated after the namespace has been changed for the new
500  * share/unshare.  In the unshare case, the pseudnodes leading to
501  * this export have already been dereferenced and possibly destroyed.
502  * If the parent export had no other shared descendants, then its
503  * pseudonode list would be empty.
504  */
505 static void
506 pseudo_secinfo_add(exportinfo_t *ancexi, exp_visible_t *ancpseudo,
507     exportinfo_t *newexi)
508 {
509 	exp_visible_t	*av, *v;
510 	fid_t		*nxfid = NULL;
511 	secinfo_t	**pnewsec = NULL;
512 	int		*pnewcnt = NULL;
513 	int		is_pseudo;
514 
515 
516 	if (ancexi->exi_visible == NULL || ancpseudo == NULL)
517 		return;
518 
519 	is_pseudo = PSEUDO(ancexi);
520 
521 	/*
522 	 * Prepare to set newexi's implicitly allowed flavors by
523 	 * inheriting flavors from newexi's pseudnode.
524 	 *	nxfid  : used to find the newexi's pseudonode
525 	 *	pnewsec: newexi's secinfo array
526 	 *	pnewcnt: newexi's secinfo count
527 	 */
528 	if (newexi) {
529 		nxfid = &newexi->exi_fid;
530 		pnewsec = &newexi->exi_export.ex_secinfo;
531 		pnewcnt = &newexi->exi_export.ex_seccnt;
532 	}
533 
534 	/*
535 	 * find the pseudonodes which correspond to the ones in ancvis.
536 	 */
537 	for (av = ancpseudo; av != NULL; av = av->vis_next) {
538 		for (v = ancexi->exi_visible; v != NULL; v = v->vis_next) {
539 
540 			if (EQFID(&av->vis_fid, &v->vis_fid)) {
541 				/*
542 				 * It's a match.  First check to see if newexi
543 				 * needs to get implicitly allowed flavors from
544 				 * its pseudonode.
545 				 *
546 				 * ancpseudo is built [by srv_secinfo_treeclimb]
547 				 * such that vis_exported is only set for the
548 				 * pseudonode corresponding to newexi's root.
549 				 */
550 				if (nxfid && av->vis_exported &&
551 				    v->vis_seccnt > 0) {
552 
553 					srv_secinfo_add(pnewsec, pnewcnt,
554 					    v->vis_secinfo, v->vis_seccnt,
555 					    is_pseudo);
556 
557 					nxfid = NULL;
558 				}
559 
560 				/*
561 				 * Apply flavor refs for the new export to
562 				 * each of the parexi's pseudonodes
563 				 */
564 				srv_secinfo_add(&v->vis_secinfo, &v->vis_seccnt,
565 				    av->vis_secinfo, av->vis_seccnt, is_pseudo);
566 
567 				break;
568 			}
569 		}
570 	}
571 }
572 
573 
574 /*
575  * pseudo_secinfo_remove
576  *	ancexi: ancestor exportinfo
577  *	ancpseudo: linked list of ancestor pseudnodes
578  *
579  * Decrement secflavor ref counts in ancestor pseudonodes.
580  */
581 static void
582 pseudo_secinfo_remove(exportinfo_t *ancexi, exp_visible_t *ancpseudo)
583 {
584 	exp_visible_t	*av, *v;
585 
586 	if (ancexi->exi_visible == NULL || ancpseudo == NULL)
587 		return;
588 
589 	/*
590 	 * find the pseudonodes which correspond to the ones in ancvis.
591 	 */
592 	for (av = ancpseudo; av != NULL; av = av->vis_next) {
593 		for (v = ancexi->exi_visible; v != NULL; v = v->vis_next) {
594 			if (EQFID(&av->vis_fid, &v->vis_fid)) {
595 
596 				srv_secinfo_remove(&v->vis_secinfo,
597 				    &v->vis_seccnt, av->vis_secinfo,
598 				    av->vis_seccnt);
599 
600 				break;
601 			}
602 		}
603 	}
604 }
605 
606 
607 
608 /*
609  * For the reshare case, sec flavor accounting happens in 3 steps:
610  * 1) propagate addition of new flavor refs up the ancestor tree
611  * 2) transfer flavor refs of descendants to new/reshared exportdata
612  * 3) propagate removal of old flavor refs up the ancestor tree
613  *
614  * srv_secinfo_exp2exp() implements step 2 of a reshare.  At this point,
615  * the new flavor list has already been propagated up through the
616  * ancestor tree via srv_secinfo_treeclimb().
617  *
618  * If there is more than 1 export reference to an old flavor (i.e. some
619  * of its children shared with this flavor), this flavor information
620  * needs to be transfered to the new exportdata struct.  A flavor in
621  * the old exportdata has descendant refs when s_refcnt > 1.
622  *
623  * Transferring descendant flavor refcnts happens in 2 passes:
624  * a) flavors used before (oldsecinfo) and after (curdata->ex_secinfo) reshare
625  * b) flavors used before but not after reshare
626  *
627  * This routine is used under the protection of exported_lock (RW_WRITER).
628  */
629 void
630 srv_secinfo_exp2exp(exportdata_t *curdata, secinfo_t *oldsecinfo, int ocnt)
631 {
632 	int ccnt, c;		/* sec count in current data - curdata */
633 	int o;			/* sec count in old data - oldsecinfo */
634 	int tcnt, mcnt;		/* total sec count after the transfer */
635 	struct secinfo *msec;	/* merged secinfo list */
636 
637 	ccnt = curdata->ex_seccnt;
638 
639 	ASSERT(ocnt > 0);
640 	ASSERT(!(curdata->ex_flags & EX_PSEUDO));
641 
642 	/*
643 	 * If the oldsecinfo has flavors with more than 1 reference count
644 	 * and the flavor is specified in the reshare, transfer the flavor
645 	 * refs to the new seclist (curdata.ex_secinfo).
646 	 */
647 	tcnt = ccnt + ocnt;
648 
649 	for (o = 0; o < ocnt; o++) {
650 
651 		if (SEC_REF_SELF(&oldsecinfo[o])) {
652 			tcnt--;
653 			continue;
654 		}
655 
656 		for (c = 0; c < ccnt; c++) {
657 			if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
658 			    curdata->ex_secinfo[c].s_secinfo.sc_nfsnum) {
659 
660 				/*
661 				 * add old reference to the current
662 				 * secinfo count
663 				 */
664 				curdata->ex_secinfo[c].s_refcnt +=
665 				    oldsecinfo[o].s_refcnt;
666 
667 				/*
668 				 * Delete the old export flavor
669 				 * reference.  The initial reference
670 				 * was created during srv_secinfo_add,
671 				 * and the count is decremented below
672 				 * to account for the initial reference.
673 				 */
674 				if (SEC_REF_EXPORTED(&oldsecinfo[o]))
675 					curdata->ex_secinfo[c].s_refcnt--;
676 
677 				SECREF_TRACE(curdata->ex_path,
678 				    "reshare_xfer_common_child_refs",
679 				    curdata->ex_secinfo[c].s_secinfo.sc_nfsnum,
680 				    curdata->ex_secinfo[c].s_refcnt);
681 
682 				ASSERT(curdata->ex_secinfo[c].s_refcnt >= 0);
683 
684 				tcnt--;
685 				break;
686 			}
687 		}
688 	}
689 
690 	if (tcnt == ccnt)
691 		return; /* no more transfer to do */
692 
693 	/*
694 	 * oldsecinfo has flavors referenced by its children that are not
695 	 * in the current (new) export flavor list.  Add these flavors.
696 	 */
697 	msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
698 
699 	/* move current secinfo list data to the new list */
700 	for (c = 0; c < ccnt; c++)
701 		msec[c] = curdata->ex_secinfo[c];
702 
703 	/*
704 	 * Add the flavor that's not in the new export, but still
705 	 * referenced by its children.
706 	 */
707 	mcnt = ccnt;
708 	for (o = 0; o < ocnt; o++) {
709 		if (! SEC_REF_SELF(&oldsecinfo[o])) {
710 			for (c = 0; c < ccnt; c++) {
711 				if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
712 				    curdata->ex_secinfo[c].s_secinfo.sc_nfsnum)
713 					break;
714 			}
715 
716 			/*
717 			 * This is the one. Add it. Decrement the ref count
718 			 * by 1 if the flavor is an explicitly shared flavor
719 			 * for the oldsecinfo export node.
720 			 */
721 			if (c == ccnt) {
722 				srv_secinfo_copy(&oldsecinfo[o], &msec[mcnt]);
723 				if (SEC_REF_EXPORTED(&oldsecinfo[o]))
724 					msec[mcnt].s_refcnt--;
725 
726 				SECREF_TRACE(curdata,
727 				    "reshare_xfer_implicit_child_refs",
728 				    msec[mcnt].s_secinfo.sc_nfsnum,
729 				    msec[mcnt].s_refcnt);
730 
731 				ASSERT(msec[mcnt].s_refcnt >= 0);
732 				mcnt++;
733 			}
734 		}
735 	}
736 
737 	ASSERT(mcnt == tcnt);
738 	/*
739 	 * Done. Update curdata, free the existing secinfo list in
740 	 * curdata and set the new value.
741 	 */
742 	if (ccnt > 0)
743 		kmem_free(curdata->ex_secinfo, ccnt * sizeof (struct secinfo));
744 	curdata->ex_seccnt = tcnt;
745 	curdata->ex_secinfo = msec;
746 }
747 
748 /*
749  * When unsharing an old export node and the old node becomes a pseudo node,
750  * if there is more than 1 export reference to an old flavor (i.e. some of
751  * its children shared with this flavor), this flavor information needs to
752  * be transfered to the new shared node.
753  *
754  * This routine is used under the protection of exported_lock (RW_WRITER).
755  */
756 void
757 srv_secinfo_exp2pseu(exportdata_t *curdata, exportdata_t *olddata)
758 {
759 	int ocnt, o;		/* sec count in transfer data - trandata */
760 	int tcnt, mcnt;		/* total sec count after transfer */
761 	struct secinfo *msec;	/* merged secinfo list */
762 
763 	ASSERT(curdata->ex_flags & EX_PSEUDO);
764 	ASSERT(curdata->ex_seccnt == 0);
765 
766 	ocnt = olddata->ex_seccnt;
767 
768 	/*
769 	 * If the olddata has flavors with more than 1 reference count,
770 	 * transfer the information to the curdata.
771 	 */
772 	tcnt = ocnt;
773 
774 	for (o = 0; o < ocnt; o++) {
775 		if (SEC_REF_SELF(&olddata->ex_secinfo[o]))
776 			tcnt--;
777 	}
778 
779 	if (tcnt == 0)
780 		return; /* no transfer to do */
781 
782 	msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
783 
784 	mcnt = 0;
785 	for (o = 0; o < ocnt; o++) {
786 		if (! SEC_REF_SELF(&olddata->ex_secinfo[o])) {
787 
788 			/*
789 			 * Decrement the reference count by 1 if the flavor is
790 			 * an explicitly shared flavor for the olddata export
791 			 * node.
792 			 */
793 			srv_secinfo_copy(&olddata->ex_secinfo[o], &msec[mcnt]);
794 			msec[mcnt].s_flags = M_RO;
795 			if (SEC_REF_EXPORTED(&olddata->ex_secinfo[o]))
796 				msec[mcnt].s_refcnt--;
797 
798 			SECREF_TRACE(curdata, "unshare_morph_pseudo",
799 			    msec[mcnt].s_secinfo.sc_nfsnum,
800 			    msec[mcnt].s_refcnt);
801 
802 			ASSERT(msec[mcnt].s_refcnt >= 0);
803 			mcnt++;
804 		}
805 	}
806 
807 	ASSERT(mcnt == tcnt);
808 	/*
809 	 * Done. Update curdata.
810 	 * Free up the existing secinfo list in curdata and
811 	 * set the new value.
812 	 */
813 	curdata->ex_seccnt = tcnt;
814 	curdata->ex_secinfo = msec;
815 }
816 
817 
818 /*
819  * For NFS V4.
820  * Add or remove the newly exported or unexported security flavors of the
821  * given exportinfo from its ancestors upto the system root.
822  */
823 int
824 srv_secinfo_treeclimb(exportinfo_t *exip, secinfo_t *sec, int seccnt, int isadd)
825 {
826 	vnode_t *dvp, *vp;
827 	fid_t fid;
828 	int error = 0;
829 	int exportdir, set_implicit_flav;
830 	struct exportinfo *exi = NULL;
831 	exp_visible_t *anc_head = NULL, *anc;
832 
833 	ASSERT(RW_WRITE_HELD(&exported_lock));
834 
835 	if (seccnt == 0)
836 		return (0);
837 
838 	vp = exip->exi_vp;
839 	VN_HOLD(vp);
840 	exportdir = 1;
841 
842 	/*
843 	 * If flavors are being added and the new export root isn't
844 	 * also VROOT, its implicitly allowed flavors are inherited from
845 	 * from its pseudonode.
846 	 */
847 	set_implicit_flav = (isadd && !(vp->v_flag & VROOT)) ? 1 : 0;
848 
849 	anc_head = NULL;
850 	anc = NULL;
851 	for (;;) {
852 		bzero(&fid, sizeof (fid));
853 		fid.fid_len = MAXFIDSZ;
854 		error = vop_fid_pseudo(vp, &fid);
855 		if (error)
856 			break;
857 
858 		if (! exportdir) {
859 
860 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
861 
862 			if (exi != NULL) {
863 				secinfo_t **pxsec = &exi->exi_export.ex_secinfo;
864 				int *pxcnt = &exi->exi_export.ex_seccnt;
865 
866 				if (isadd)
867 					srv_secinfo_add(pxsec, pxcnt, sec,
868 					    seccnt, PSEUDO(exi));
869 				else
870 					srv_secinfo_remove(pxsec, pxcnt, sec,
871 					    seccnt);
872 			}
873 		}
874 
875 		/*
876 		 * If we're at the root of the filesystem:
877 		 *   - manage pseudnode sec flavors
878 		 *   - traverse mountpoint and continue treeclimb from the stub
879 		 */
880 		if (vp->v_flag & VROOT) {
881 
882 			if (exi != NULL) {
883 				if (isadd) {
884 					if (set_implicit_flav) {
885 						pseudo_secinfo_add(exi,
886 						    anc_head, exip);
887 						set_implicit_flav = 0;
888 					} else
889 						pseudo_secinfo_add(exi,
890 						    anc_head, NULL);
891 				} else
892 					pseudo_secinfo_remove(exi, anc_head);
893 			}
894 
895 			if (anc_head) {
896 				free_pseudoanc(anc_head);
897 				anc_head = NULL;
898 			}
899 
900 			if (VN_CMP(vp, rootdir)) {
901 				/* at system root */
902 				break;
903 			}
904 
905 			vp = untraverse(vp);
906 			exportdir = 0;
907 			continue;
908 		}
909 
910 		/*
911 		 * Build a temporary list of ancestor pseudonodes leading to
912 		 * the ancestor VROOT export.  vis_exported identifies the
913 		 * pseudonode that corresponds to root of export being
914 		 * shared/unshared.
915 		 */
916 		anc = kmem_alloc(sizeof (exp_visible_t), KM_SLEEP);
917 		anc->vis_next = anc_head;
918 		VN_HOLD(vp);
919 		anc->vis_vp = vp;
920 		anc->vis_fid = fid;
921 		anc->vis_ino = 0;
922 		anc->vis_count = 1;
923 		anc->vis_seccnt = seccnt;
924 		anc->vis_secinfo = sec;
925 		anc->vis_exported = exportdir;
926 		anc_head = anc;
927 
928 		/*
929 		 * Now, do a ".." to find parent dir of vp.
930 		 */
931 		error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, CRED());
932 
933 		if (error == ENOTDIR && exportdir) {
934 			dvp = exip->exi_dvp;
935 			ASSERT(dvp != NULL);
936 			VN_HOLD(dvp);
937 			error = 0;
938 		}
939 
940 		if (error)
941 			break;
942 
943 		exportdir = 0;
944 		VN_RELE(vp);
945 		vp = dvp;
946 	}
947 
948 	VN_RELE(vp);
949 
950 	if (anc_head)
951 		free_pseudoanc(anc_head);
952 
953 	return (error);
954 }
955 
956 /*
957  * Free a list of visible directories
958  */
959 static void
960 free_pseudoanc(struct exp_visible *head)
961 {
962 	struct exp_visible *visp, *next;
963 
964 	for (visp = head; visp; visp = next) {
965 		if (visp->vis_vp != NULL)
966 			VN_RELE(visp->vis_vp);
967 		/*
968 		 * we don't free vis_secinfo from the temporary pseudonode
969 		 * ancestor list because it was never kmem_alloc'd.
970 		 * srv_secinfo_treeclimb set it to point to the nodups
971 		 * secinfo array (which is a stack var).
972 		 */
973 		next = visp->vis_next;
974 		kmem_free(visp, sizeof (*visp));
975 	}
976 }
977 
978 
979 void
980 export_link(exportinfo_t *exi) {
981 	int exporthash;
982 
983 	exporthash = exptablehash(&exi->exi_fsid, &exi->exi_fid);
984 	exi->exi_hash = exptable[exporthash];
985 	exptable[exporthash] = exi;
986 }
987 
988 /*
989  * Initialization routine for export routines. Should only be called once.
990  */
991 int
992 nfs_exportinit(void)
993 {
994 	int error;
995 
996 	rw_init(&exported_lock, NULL, RW_DEFAULT, NULL);
997 
998 	/*
999 	 * Allocate the place holder for the public file handle, which
1000 	 * is all zeroes. It is initially set to the root filesystem.
1001 	 */
1002 	exi_root = kmem_zalloc(sizeof (*exi_root), KM_SLEEP);
1003 	exi_public = exi_root;
1004 
1005 	exi_root->exi_export.ex_flags = EX_PUBLIC;
1006 	exi_root->exi_export.ex_pathlen = 1;	/* length of "/" */
1007 	exi_root->exi_export.ex_path =
1008 	    kmem_alloc(exi_root->exi_export.ex_pathlen + 1, KM_SLEEP);
1009 	exi_root->exi_export.ex_path[0] = '/';
1010 	exi_root->exi_export.ex_path[1] = '\0';
1011 
1012 	exi_root->exi_count = 1;
1013 	mutex_init(&exi_root->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1014 
1015 	exi_root->exi_vp = rootdir;
1016 	exi_rootfid.fid_len = MAXFIDSZ;
1017 	error = vop_fid_pseudo(exi_root->exi_vp, &exi_rootfid);
1018 	if (error) {
1019 		mutex_destroy(&exi_root->exi_lock);
1020 		kmem_free(exi_root, sizeof (*exi_root));
1021 		return (error);
1022 	}
1023 
1024 	/* setup the fhandle template */
1025 	exi_root->exi_fh.fh_fsid = rootdir->v_vfsp->vfs_fsid;
1026 	exi_root->exi_fh.fh_xlen = exi_rootfid.fid_len;
1027 	bcopy(exi_rootfid.fid_data, exi_root->exi_fh.fh_xdata,
1028 	    exi_rootfid.fid_len);
1029 	exi_root->exi_fh.fh_len = sizeof (exi_root->exi_fh.fh_data);
1030 
1031 	/*
1032 	 * Publish the exportinfo in the hash table
1033 	 */
1034 	export_link(exi_root);
1035 
1036 	nfslog_init();
1037 
1038 	return (0);
1039 }
1040 
1041 /*
1042  * Finalization routine for export routines. Called to cleanup previoulsy
1043  * initializtion work when the NFS server module could not be loaded correctly.
1044  */
1045 void
1046 nfs_exportfini(void)
1047 {
1048 	/*
1049 	 * Deallocate the place holder for the public file handle.
1050 	 */
1051 	srv_secinfo_list_free(exi_root->exi_export.ex_secinfo,
1052 	    exi_root->exi_export.ex_seccnt);
1053 	mutex_destroy(&exi_root->exi_lock);
1054 	kmem_free(exi_root, sizeof (*exi_root));
1055 
1056 	rw_destroy(&exported_lock);
1057 }
1058 
1059 /*
1060  *  Check if 2 gss mechanism identifiers are the same.
1061  *
1062  *  return FALSE if not the same.
1063  *  return TRUE if the same.
1064  */
1065 static bool_t
1066 nfs_mech_equal(rpc_gss_OID mech1, rpc_gss_OID mech2)
1067 {
1068 	if ((mech1->length == 0) && (mech2->length == 0))
1069 		return (TRUE);
1070 
1071 	if (mech1->length != mech2->length)
1072 		return (FALSE);
1073 
1074 	return (bcmp(mech1->elements, mech2->elements, mech1->length) == 0);
1075 }
1076 
1077 /*
1078  *  This routine is used by rpc to map rpc security number
1079  *  to nfs specific security flavor number.
1080  *
1081  *  The gss callback prototype is
1082  *  callback(struct svc_req *, gss_cred_id_t *, gss_ctx_id_t *,
1083  *				rpc_gss_lock_t *, void **),
1084  *  since nfs does not use the gss_cred_id_t/gss_ctx_id_t arguments
1085  *  we cast them to void.
1086  */
1087 /*ARGSUSED*/
1088 bool_t
1089 rfs_gsscallback(struct svc_req *req, gss_cred_id_t deleg, void *gss_context,
1090     rpc_gss_lock_t *lock, void **cookie)
1091 {
1092 	int i, j;
1093 	rpc_gss_rawcred_t *raw_cred;
1094 	struct exportinfo *exi;
1095 
1096 	/*
1097 	 * We don't deal with delegated credentials.
1098 	 */
1099 	if (deleg != GSS_C_NO_CREDENTIAL)
1100 		return (FALSE);
1101 
1102 	raw_cred = lock->raw_cred;
1103 	*cookie = NULL;
1104 
1105 	rw_enter(&exported_lock, RW_READER);
1106 	for (i = 0; i < EXPTABLESIZE; i++) {
1107 		exi = exptable[i];
1108 		while (exi) {
1109 			if (exi->exi_export.ex_seccnt > 0) {
1110 				struct secinfo *secp;
1111 				seconfig_t *se;
1112 				int seccnt;
1113 
1114 				secp = exi->exi_export.ex_secinfo;
1115 				seccnt = exi->exi_export.ex_seccnt;
1116 				for (j = 0; j < seccnt; j++) {
1117 					/*
1118 					 *  If there is a map of the triplet
1119 					 *  (mechanism, service, qop) between
1120 					 *  raw_cred and the exported flavor,
1121 					 *  get the psudo flavor number.
1122 					 *  Also qop should not be NULL, it
1123 					 *  should be "default" or something
1124 					 *  else.
1125 					 */
1126 					se = &secp[j].s_secinfo;
1127 					if ((se->sc_rpcnum == RPCSEC_GSS) &&
1128 
1129 					    (nfs_mech_equal(
1130 					    se->sc_gss_mech_type,
1131 					    raw_cred->mechanism)) &&
1132 
1133 					    (se->sc_service ==
1134 					    raw_cred->service) &&
1135 					    (raw_cred->qop == se->sc_qop)) {
1136 
1137 						*cookie = (void *)(uintptr_t)
1138 						    se->sc_nfsnum;
1139 						goto done;
1140 					}
1141 				}
1142 			}
1143 			exi = exi->exi_hash;
1144 		}
1145 	}
1146 done:
1147 	rw_exit(&exported_lock);
1148 
1149 	/*
1150 	 * If no nfs pseudo number mapping can be found in the export
1151 	 * table, assign the nfsflavor to NFS_FLAVOR_NOMAP. In V4, we may
1152 	 * recover the flavor mismatch from NFS layer (NFS4ERR_WRONGSEC).
1153 	 *
1154 	 * For example:
1155 	 *	server first shares with krb5i;
1156 	 *	client mounts with krb5i;
1157 	 *	server re-shares with krb5p;
1158 	 *	client tries with krb5i, but no mapping can be found;
1159 	 *	rpcsec_gss module calls this routine to do the mapping,
1160 	 *		if this routine fails, request is rejected from
1161 	 *		the rpc layer.
1162 	 *	What we need is to let the nfs layer rejects the request.
1163 	 *	For V4, we can reject with NFS4ERR_WRONGSEC and the client
1164 	 *	may recover from it by getting the new flavor via SECINFO.
1165 	 *
1166 	 * nfs pseudo number for RPCSEC_GSS mapping (see nfssec.conf)
1167 	 * is owned by IANA (see RFC 2623).
1168 	 *
1169 	 * XXX NFS_FLAVOR_NOMAP is defined in Solaris to work around
1170 	 * the implementation issue. This number should not overlap with
1171 	 * any new IANA defined pseudo flavor numbers.
1172 	 */
1173 	if (*cookie == NULL)
1174 		*cookie = (void *)NFS_FLAVOR_NOMAP;
1175 
1176 	lock->locked = TRUE;
1177 
1178 	return (TRUE);
1179 }
1180 
1181 
1182 /*
1183  * Exportfs system call; credentials should be checked before
1184  * calling this function.
1185  */
1186 int
1187 exportfs(struct exportfs_args *args, model_t model, cred_t *cr)
1188 {
1189 	vnode_t *vp;
1190 	vnode_t *dvp;
1191 	struct exportdata *kex;
1192 	struct exportinfo *exi;
1193 	struct exportinfo *ex, *prev;
1194 	fid_t fid;
1195 	fsid_t fsid;
1196 	int error;
1197 	size_t allocsize;
1198 	struct secinfo *sp;
1199 	struct secinfo *exs;
1200 	rpc_gss_callback_t cb;
1201 	char *pathbuf;
1202 	char *log_buffer;
1203 	char *tagbuf;
1204 	int callback;
1205 	int allocd_seccnt;
1206 	STRUCT_HANDLE(exportfs_args, uap);
1207 	STRUCT_DECL(exportdata, uexi);
1208 	struct secinfo newsec[MAX_FLAVORS];
1209 	int newcnt;
1210 	struct secinfo oldsec[MAX_FLAVORS];
1211 	int oldcnt;
1212 	int i;
1213 
1214 	STRUCT_SET_HANDLE(uap, model, args);
1215 
1216 	error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1217 	    FOLLOW, &dvp, &vp);
1218 	if (error == EINVAL) {
1219 		/*
1220 		 * if fname resolves to / we get EINVAL error
1221 		 * since we wanted the parent vnode. Try again
1222 		 * with NULL dvp.
1223 		 */
1224 		error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1225 		    FOLLOW, NULL, &vp);
1226 		dvp = NULL;
1227 	}
1228 	if (!error && vp == NULL) {
1229 		/*
1230 		 * Last component of fname not found
1231 		 */
1232 		if (dvp != NULL) {
1233 			VN_RELE(dvp);
1234 		}
1235 		error = ENOENT;
1236 	}
1237 	if (error)
1238 		return (error);
1239 
1240 	/*
1241 	 * 'vp' may be an AUTOFS node, so we perform a
1242 	 * VOP_ACCESS() to trigger the mount of the
1243 	 * intended filesystem, so we can share the intended
1244 	 * filesystem instead of the AUTOFS filesystem.
1245 	 */
1246 	(void) VOP_ACCESS(vp, 0, 0, cr);
1247 
1248 	/*
1249 	 * We're interested in the top most filesystem.
1250 	 * This is specially important when uap->dname is a trigger
1251 	 * AUTOFS node, since we're really interested in sharing the
1252 	 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1253 	 * call not the AUTOFS node itself.
1254 	 */
1255 	if (vn_mountedvfs(vp) != NULL) {
1256 		if (error = traverse(&vp)) {
1257 			VN_RELE(vp);
1258 			if (dvp != NULL)
1259 				VN_RELE(dvp);
1260 			return (error);
1261 		}
1262 	}
1263 
1264 	/*
1265 	 * Get the vfs id
1266 	 */
1267 	bzero(&fid, sizeof (fid));
1268 	fid.fid_len = MAXFIDSZ;
1269 	error = VOP_FID(vp, &fid);
1270 	fsid = vp->v_vfsp->vfs_fsid;
1271 	if (error) {
1272 		VN_RELE(vp);
1273 		if (dvp != NULL)
1274 			VN_RELE(dvp);
1275 		/*
1276 		 * If VOP_FID returns ENOSPC then the fid supplied
1277 		 * is too small.  For now we simply return EREMOTE.
1278 		 */
1279 		if (error == ENOSPC)
1280 			error = EREMOTE;
1281 		return (error);
1282 	}
1283 
1284 	if (STRUCT_FGETP(uap, uex) == NULL) {
1285 		error = unexport(&fsid, &fid, vp);
1286 		VN_RELE(vp);
1287 		if (dvp != NULL)
1288 		VN_RELE(dvp);
1289 		return (error);
1290 	}
1291 
1292 	exi = kmem_zalloc(sizeof (*exi), KM_SLEEP);
1293 	exi->exi_fsid = fsid;
1294 	exi->exi_fid = fid;
1295 	exi->exi_vp = vp;
1296 	exi->exi_count = 1;
1297 	exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag &
1298 	    VSW_VOLATILEDEV) ? 1 : 0;
1299 	mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1300 	exi->exi_dvp = dvp;
1301 
1302 	/*
1303 	 * Initialize auth cache lock
1304 	 */
1305 	rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL);
1306 
1307 	/*
1308 	 * Build up the template fhandle
1309 	 */
1310 	exi->exi_fh.fh_fsid = fsid;
1311 	if (exi->exi_fid.fid_len > sizeof (exi->exi_fh.fh_xdata)) {
1312 		error = EREMOTE;
1313 		goto out1;
1314 	}
1315 	exi->exi_fh.fh_xlen = exi->exi_fid.fid_len;
1316 	bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata,
1317 	    exi->exi_fid.fid_len);
1318 
1319 	exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data);
1320 
1321 	kex = &exi->exi_export;
1322 
1323 	/*
1324 	 * Load in everything, and do sanity checking
1325 	 */
1326 	STRUCT_INIT(uexi, model);
1327 	if (copyin(STRUCT_FGETP(uap, uex), STRUCT_BUF(uexi),
1328 	    STRUCT_SIZE(uexi))) {
1329 		error = EFAULT;
1330 		goto out1;
1331 	}
1332 
1333 	kex->ex_version = STRUCT_FGET(uexi, ex_version);
1334 	if (kex->ex_version != EX_CURRENT_VERSION) {
1335 		error = EINVAL;
1336 		cmn_err(CE_WARN,
1337 		    "NFS: exportfs requires export struct version 2 - got %d\n",
1338 		    kex->ex_version);
1339 		goto out1;
1340 	}
1341 
1342 	/*
1343 	 * Must have at least one security entry
1344 	 */
1345 	kex->ex_seccnt = STRUCT_FGET(uexi, ex_seccnt);
1346 	if (kex->ex_seccnt < 1) {
1347 		error = EINVAL;
1348 		goto out1;
1349 	}
1350 
1351 	kex->ex_path = STRUCT_FGETP(uexi, ex_path);
1352 	kex->ex_pathlen = STRUCT_FGET(uexi, ex_pathlen);
1353 	kex->ex_flags = STRUCT_FGET(uexi, ex_flags);
1354 	kex->ex_anon = STRUCT_FGET(uexi, ex_anon);
1355 	kex->ex_secinfo = STRUCT_FGETP(uexi, ex_secinfo);
1356 	kex->ex_index = STRUCT_FGETP(uexi, ex_index);
1357 	kex->ex_log_buffer = STRUCT_FGETP(uexi, ex_log_buffer);
1358 	kex->ex_log_bufferlen = STRUCT_FGET(uexi, ex_log_bufferlen);
1359 	kex->ex_tag = STRUCT_FGETP(uexi, ex_tag);
1360 	kex->ex_taglen = STRUCT_FGET(uexi, ex_taglen);
1361 
1362 	/*
1363 	 * Copy the exported pathname into
1364 	 * an appropriately sized buffer.
1365 	 */
1366 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1367 	if (copyinstr(kex->ex_path, pathbuf, MAXPATHLEN, &kex->ex_pathlen)) {
1368 		kmem_free(pathbuf, MAXPATHLEN);
1369 		error = EFAULT;
1370 		goto out1;
1371 	}
1372 	kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP);
1373 	bcopy(pathbuf, kex->ex_path, kex->ex_pathlen);
1374 	kex->ex_path[kex->ex_pathlen] = '\0';
1375 	kmem_free(pathbuf, MAXPATHLEN);
1376 
1377 	/*
1378 	 * Get the path to the logging buffer and the tag
1379 	 */
1380 	if (kex->ex_flags & EX_LOG) {
1381 		log_buffer = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1382 		if (copyinstr(kex->ex_log_buffer, log_buffer, MAXPATHLEN,
1383 		    &kex->ex_log_bufferlen)) {
1384 			kmem_free(log_buffer, MAXPATHLEN);
1385 			error = EFAULT;
1386 			goto out2;
1387 		}
1388 		kex->ex_log_buffer =
1389 		    kmem_alloc(kex->ex_log_bufferlen + 1, KM_SLEEP);
1390 		bcopy(log_buffer, kex->ex_log_buffer, kex->ex_log_bufferlen);
1391 		kex->ex_log_buffer[kex->ex_log_bufferlen] = '\0';
1392 		kmem_free(log_buffer, MAXPATHLEN);
1393 
1394 		tagbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1395 		if (copyinstr(kex->ex_tag, tagbuf, MAXPATHLEN,
1396 		    &kex->ex_taglen)) {
1397 			kmem_free(tagbuf, MAXPATHLEN);
1398 			error = EFAULT;
1399 			goto out3;
1400 		}
1401 		kex->ex_tag = kmem_alloc(kex->ex_taglen + 1, KM_SLEEP);
1402 		bcopy(tagbuf, kex->ex_tag, kex->ex_taglen);
1403 		kex->ex_tag[kex->ex_taglen] = '\0';
1404 		kmem_free(tagbuf, MAXPATHLEN);
1405 	}
1406 
1407 	/*
1408 	 * Load the security information for each flavor
1409 	 */
1410 	allocsize = kex->ex_seccnt * SIZEOF_STRUCT(secinfo, model);
1411 	sp = kmem_zalloc(allocsize, KM_SLEEP);
1412 	if (copyin(kex->ex_secinfo, sp, allocsize)) {
1413 		kmem_free(sp, allocsize);
1414 		error = EFAULT;
1415 		goto out4;
1416 	}
1417 
1418 	/*
1419 	 * All of these nested structures need to be converted to
1420 	 * the kernel native format.
1421 	 */
1422 	if (model != DATAMODEL_NATIVE) {
1423 		size_t allocsize2;
1424 		struct secinfo *sp2;
1425 
1426 		allocsize2 = kex->ex_seccnt * sizeof (struct secinfo);
1427 		sp2 = kmem_zalloc(allocsize2, KM_SLEEP);
1428 
1429 		for (i = 0; i < kex->ex_seccnt; i++) {
1430 			STRUCT_HANDLE(secinfo, usi);
1431 
1432 			STRUCT_SET_HANDLE(usi, model,
1433 			    (struct secinfo *)((caddr_t)sp +
1434 			    (i * SIZEOF_STRUCT(secinfo, model))));
1435 			bcopy(STRUCT_FGET(usi, s_secinfo.sc_name),
1436 			    sp2[i].s_secinfo.sc_name, MAX_NAME_LEN);
1437 			sp2[i].s_secinfo.sc_nfsnum =
1438 			    STRUCT_FGET(usi, s_secinfo.sc_nfsnum);
1439 			sp2[i].s_secinfo.sc_rpcnum =
1440 			    STRUCT_FGET(usi, s_secinfo.sc_rpcnum);
1441 			bcopy(STRUCT_FGET(usi, s_secinfo.sc_gss_mech),
1442 			    sp2[i].s_secinfo.sc_gss_mech, MAX_NAME_LEN);
1443 			sp2[i].s_secinfo.sc_gss_mech_type =
1444 			    STRUCT_FGETP(usi, s_secinfo.sc_gss_mech_type);
1445 			sp2[i].s_secinfo.sc_qop =
1446 			    STRUCT_FGET(usi, s_secinfo.sc_qop);
1447 			sp2[i].s_secinfo.sc_service =
1448 			    STRUCT_FGET(usi, s_secinfo.sc_service);
1449 
1450 			sp2[i].s_flags = STRUCT_FGET(usi, s_flags);
1451 			sp2[i].s_window = STRUCT_FGET(usi, s_window);
1452 			sp2[i].s_rootcnt = STRUCT_FGET(usi, s_rootcnt);
1453 			sp2[i].s_rootnames = STRUCT_FGETP(usi, s_rootnames);
1454 		}
1455 		kmem_free(sp, allocsize);
1456 		sp = sp2;
1457 		allocsize = allocsize2;
1458 	}
1459 
1460 	kex->ex_secinfo = sp;
1461 
1462 	/*
1463 	 * And now copy rootnames for each individual secinfo.
1464 	 */
1465 	callback = 0;
1466 	allocd_seccnt = 0;
1467 	while (allocd_seccnt < kex->ex_seccnt) {
1468 
1469 		exs = &sp[allocd_seccnt];
1470 		if (exs->s_rootcnt > 0) {
1471 			if (!sec_svc_loadrootnames(exs->s_secinfo.sc_rpcnum,
1472 			    exs->s_rootcnt, &exs->s_rootnames, model)) {
1473 				error = EFAULT;
1474 				goto out5;
1475 			}
1476 		}
1477 
1478 		if (exs->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
1479 			rpc_gss_OID mech_tmp;
1480 			STRUCT_DECL(rpc_gss_OID_s, umech_tmp);
1481 			caddr_t elements_tmp;
1482 
1483 			/* Copyin mechanism type */
1484 			STRUCT_INIT(umech_tmp, model);
1485 			mech_tmp = kmem_alloc(sizeof (*mech_tmp), KM_SLEEP);
1486 			if (copyin(exs->s_secinfo.sc_gss_mech_type,
1487 			    STRUCT_BUF(umech_tmp), STRUCT_SIZE(umech_tmp))) {
1488 				kmem_free(mech_tmp, sizeof (*mech_tmp));
1489 				error = EFAULT;
1490 				goto out5;
1491 			}
1492 			mech_tmp->length = STRUCT_FGET(umech_tmp, length);
1493 			mech_tmp->elements = STRUCT_FGETP(umech_tmp, elements);
1494 
1495 			elements_tmp = kmem_alloc(mech_tmp->length, KM_SLEEP);
1496 			if (copyin(mech_tmp->elements, elements_tmp,
1497 			    mech_tmp->length)) {
1498 				kmem_free(elements_tmp, mech_tmp->length);
1499 				kmem_free(mech_tmp, sizeof (*mech_tmp));
1500 				error = EFAULT;
1501 				goto out5;
1502 			}
1503 			mech_tmp->elements = elements_tmp;
1504 			exs->s_secinfo.sc_gss_mech_type = mech_tmp;
1505 			allocd_seccnt++;
1506 
1507 			callback = 1;
1508 		} else
1509 			allocd_seccnt++;
1510 	}
1511 
1512 	/*
1513 	 * Init the secinfo reference count and mark these flavors
1514 	 * explicitly exported flavors.
1515 	 */
1516 	for (i = 0; i < kex->ex_seccnt; i++) {
1517 		kex->ex_secinfo[i].s_flags |= M_4SEC_EXPORTED;
1518 		kex->ex_secinfo[i].s_refcnt = 1;
1519 	}
1520 
1521 	/*
1522 	 *  Set up rpcsec_gss callback routine entry if any.
1523 	 */
1524 	if (callback) {
1525 		cb.callback = rfs_gsscallback;
1526 		cb.program = NFS_ACL_PROGRAM;
1527 		for (cb.version = NFS_ACL_VERSMIN;
1528 		    cb.version <= NFS_ACL_VERSMAX; cb.version++) {
1529 			(void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1530 			    (void *)&cb);
1531 		}
1532 
1533 		cb.program = NFS_PROGRAM;
1534 		for (cb.version = NFS_VERSMIN;
1535 		    cb.version <= NFS_VERSMAX; cb.version++) {
1536 			(void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1537 			    (void *)&cb);
1538 		}
1539 	}
1540 
1541 	/*
1542 	 * Check the index flag. Do this here to avoid holding the
1543 	 * lock while dealing with the index option (as we do with
1544 	 * the public option).
1545 	 */
1546 	if (kex->ex_flags & EX_INDEX) {
1547 		if (!kex->ex_index) {	/* sanity check */
1548 			error = EINVAL;
1549 			goto out5;
1550 		}
1551 		if (error = loadindex(kex))
1552 			goto out5;
1553 	}
1554 
1555 	if (kex->ex_flags & EX_LOG) {
1556 		if (error = nfslog_setup(exi))
1557 			goto out6;
1558 	}
1559 
1560 	/*
1561 	 * Insert the new entry at the front of the export list
1562 	 */
1563 	rw_enter(&exported_lock, RW_WRITER);
1564 
1565 	export_link(exi);
1566 
1567 	/*
1568 	 * Check the rest of the list for an old entry for the fs.
1569 	 * If one is found then unlink it, wait until this is the
1570 	 * only reference and then free it.
1571 	 */
1572 	prev = exi;
1573 	for (ex = prev->exi_hash; ex != NULL; prev = ex, ex = ex->exi_hash) {
1574 		if (ex != exi_root && VN_CMP(ex->exi_vp, vp)) {
1575 			prev->exi_hash = ex->exi_hash;
1576 			break;
1577 		}
1578 	}
1579 
1580 	/*
1581 	 * If the public filehandle is pointing at the
1582 	 * old entry, then point it back at the root.
1583 	 */
1584 	if (ex != NULL && ex == exi_public)
1585 		exi_public = exi_root;
1586 
1587 	/*
1588 	 * If the public flag is on, make the global exi_public
1589 	 * point to this entry and turn off the public bit so that
1590 	 * we can distinguish it from the place holder export.
1591 	 */
1592 	if (kex->ex_flags & EX_PUBLIC) {
1593 		exi_public = exi;
1594 		kex->ex_flags &= ~EX_PUBLIC;
1595 	}
1596 
1597 #ifdef VOLATILE_FH_TEST
1598 	/*
1599 	 * Set up the volatile_id value if volatile on share.
1600 	 * The list of volatile renamed filehandles is always destroyed,
1601 	 * if the fs was reshared.
1602 	 */
1603 	if (kex->ex_flags & EX_VOLFH)
1604 		exi->exi_volatile_id = gethrestime_sec();
1605 
1606 	mutex_init(&exi->exi_vol_rename_lock, NULL, MUTEX_DEFAULT, NULL);
1607 #endif /* VOLATILE_FH_TEST */
1608 
1609 	/*
1610 	 * If this is a new export, then climb up
1611 	 * the tree and check if any pseudo exports
1612 	 * need to be created to provide a path for
1613 	 * NFS v4 clients.
1614 	 */
1615 	if (ex == NULL)
1616 		error = treeclimb_export(exi);
1617 
1618 	/*
1619 	 * build a unique flavor list from the flavors specified
1620 	 * in the share cmd.  unique means that each flavor only
1621 	 * appears once in the secinfo list -- no duplicates allowed.
1622 	 */
1623 	newcnt = build_seclist_nodups(&exi->exi_export, newsec, FALSE);
1624 
1625 	if (!error)
1626 		error = srv_secinfo_treeclimb(exi, newsec, newcnt, TRUE);
1627 
1628 	/*
1629 	 * If re-sharing an old export entry, update the secinfo data
1630 	 * depending on if the old entry is a pseudo node or not.
1631 	 */
1632 	if (!error && ex != NULL) {
1633 		oldcnt = build_seclist_nodups(&ex->exi_export, oldsec, FALSE);
1634 		if (PSEUDO(ex)) {
1635 			/*
1636 			 * The dir being shared is a pseudo export root (which
1637 			 * will be transformed into a real export root).  The
1638 			 * flavor(s) of the new share were propagated to the
1639 			 * ancestors by srv_secinfo_treeclimb() above.  Now
1640 			 * transfer the implicit flavor refs from the old
1641 			 * pseudo exprot root to the new (real) export root.
1642 			 */
1643 			srv_secinfo_add(&exi->exi_export.ex_secinfo,
1644 			    &exi->exi_export.ex_seccnt, oldsec, oldcnt, TRUE);
1645 		} else {
1646 			/*
1647 			 * First transfer implicit flavor refs to new export.
1648 			 * Remove old flavor refs last.
1649 			 */
1650 			srv_secinfo_exp2exp(&exi->exi_export, oldsec, oldcnt);
1651 			error = srv_secinfo_treeclimb(ex, oldsec, oldcnt,
1652 			    FALSE);
1653 		}
1654 	}
1655 
1656 	if (error)
1657 		goto out7;
1658 
1659 	/*
1660 	 * If it's a re-export and the old entry has a pseudnode list,
1661 	 * transfer it to the new export.
1662 	 */
1663 	if (ex != NULL && (ex->exi_visible != NULL)) {
1664 		exi->exi_visible = ex->exi_visible;
1665 		ex->exi_visible = NULL;
1666 	}
1667 
1668 	rw_exit(&exported_lock);
1669 
1670 	if (exi_public == exi || kex->ex_flags & EX_LOG) {
1671 		/*
1672 		 * Log share operation to this buffer only.
1673 		 */
1674 		nfslog_share_record(exi, cr);
1675 	}
1676 
1677 	if (ex != NULL)
1678 		exi_rele(ex);
1679 
1680 	return (0);
1681 
1682 out7:
1683 	/*
1684 	 * Cleaning up the tree. Assuming *treeclimb* routines
1685 	 * will fail at the same place in the tree.
1686 	 */
1687 	(void) treeclimb_unexport(exi);
1688 	(void) srv_secinfo_treeclimb(exi, newsec, newcnt, FALSE);
1689 
1690 	/*
1691 	 * Unlink and re-link the new and old export in exptable.
1692 	 */
1693 	(void) export_unlink(&exi->exi_fsid, &exi->exi_fid, exi->exi_vp, NULL);
1694 	if (ex != NULL)
1695 		export_link(ex);
1696 
1697 	rw_exit(&exported_lock);
1698 out6:
1699 	if (kex->ex_flags & EX_INDEX)
1700 		kmem_free(kex->ex_index, strlen(kex->ex_index) + 1);
1701 out5:
1702 	/* free partially completed allocation */
1703 	while (--allocd_seccnt >= 0) {
1704 		exs = &kex->ex_secinfo[allocd_seccnt];
1705 		srv_secinfo_entry_free(exs);
1706 	}
1707 
1708 	if (kex->ex_secinfo) {
1709 		kmem_free(kex->ex_secinfo,
1710 		    kex->ex_seccnt * sizeof (struct secinfo));
1711 	}
1712 
1713 out4:
1714 	if ((kex->ex_flags & EX_LOG) && kex->ex_tag != NULL)
1715 		kmem_free(kex->ex_tag, kex->ex_taglen + 1);
1716 out3:
1717 	if ((kex->ex_flags & EX_LOG) && kex->ex_log_buffer != NULL)
1718 		kmem_free(kex->ex_log_buffer, kex->ex_log_bufferlen + 1);
1719 out2:
1720 	kmem_free(kex->ex_path, kex->ex_pathlen + 1);
1721 out1:
1722 	VN_RELE(vp);
1723 	if (dvp != NULL)
1724 		VN_RELE(dvp);
1725 	mutex_destroy(&exi->exi_lock);
1726 	rw_destroy(&exi->exi_cache_lock);
1727 	kmem_free(exi, sizeof (*exi));
1728 	return (error);
1729 }
1730 
1731 /*
1732  * Remove the exportinfo from the export list
1733  */
1734 int
1735 export_unlink(fsid_t *fsid, fid_t *fid, vnode_t *vp, struct exportinfo **exip)
1736 {
1737 	struct exportinfo **tail;
1738 
1739 	ASSERT(RW_WRITE_HELD(&exported_lock));
1740 
1741 	tail = &exptable[exptablehash(fsid, fid)];
1742 	while (*tail != NULL) {
1743 		if (exportmatch(*tail, fsid, fid)) {
1744 			/*
1745 			 * If vp is given, check if vp is the
1746 			 * same vnode as the exported node.
1747 			 *
1748 			 * Since VOP_FID of a lofs node returns the
1749 			 * fid of its real node (ufs), the exported
1750 			 * node for lofs and (pseudo) ufs may have
1751 			 * the same fsid and fid.
1752 			 */
1753 			if (vp == NULL || vp == (*tail)->exi_vp) {
1754 
1755 				if (exip != NULL)
1756 					*exip = *tail;
1757 				*tail = (*tail)->exi_hash;
1758 
1759 				return (0);
1760 			}
1761 		}
1762 		tail = &(*tail)->exi_hash;
1763 	}
1764 
1765 	return (EINVAL);
1766 }
1767 
1768 /*
1769  * Unexport an exported filesystem
1770  */
1771 int
1772 unexport(fsid_t *fsid, fid_t *fid, vnode_t *vp)
1773 {
1774 	struct exportinfo *exi = NULL;
1775 	int error;
1776 	struct secinfo cursec[MAX_FLAVORS];
1777 	int curcnt;
1778 
1779 	rw_enter(&exported_lock, RW_WRITER);
1780 
1781 	error = export_unlink(fsid, fid, vp, &exi);
1782 
1783 	if (error) {
1784 		rw_exit(&exported_lock);
1785 		return (error);
1786 	}
1787 
1788 	/* pseudo node is not a real exported filesystem */
1789 	if (PSEUDO(exi)) {
1790 		/*
1791 		 * Put the pseudo node back into the export table
1792 		 * before erroring out.
1793 		 */
1794 		export_link(exi);
1795 		rw_exit(&exported_lock);
1796 		return (EINVAL);
1797 	}
1798 
1799 	/*
1800 	 * If there's a visible list, then need to leave
1801 	 * a pseudo export here to retain the visible list
1802 	 * for paths to exports below.
1803 	 */
1804 	if (exi->exi_visible) {
1805 		error = pseudo_exportfs(exi->exi_vp, exi->exi_visible,
1806 		    &exi->exi_export);
1807 		if (error)
1808 			goto done;
1809 
1810 		exi->exi_visible = NULL;
1811 	} else {
1812 		error = treeclimb_unexport(exi);
1813 		if (error)
1814 			goto done;
1815 	}
1816 
1817 	curcnt = build_seclist_nodups(&exi->exi_export, cursec, TRUE);
1818 
1819 	error = srv_secinfo_treeclimb(exi, cursec, curcnt, FALSE);
1820 	if (error)
1821 		goto done;
1822 
1823 	rw_exit(&exported_lock);
1824 
1825 	/*
1826 	 * Need to call into the NFSv4 server and release all data
1827 	 * held on this particular export.  This is important since
1828 	 * the v4 server may be holding file locks or vnodes under
1829 	 * this export.
1830 	 */
1831 	rfs4_clean_state_exi(exi);
1832 
1833 	/*
1834 	 * Notify the lock manager that the filesystem is being
1835 	 * unexported.
1836 	 */
1837 	lm_unexport(exi);
1838 
1839 	/*
1840 	 * If this was a public export, restore
1841 	 * the public filehandle to the root.
1842 	 */
1843 	if (exi == exi_public) {
1844 		exi_public = exi_root;
1845 
1846 		nfslog_share_record(exi_public, CRED());
1847 	}
1848 
1849 	if (exi->exi_export.ex_flags & EX_LOG) {
1850 		nfslog_unshare_record(exi, CRED());
1851 	}
1852 
1853 	exi_rele(exi);
1854 	return (error);
1855 
1856 done:
1857 	rw_exit(&exported_lock);
1858 	exi_rele(exi);
1859 	return (error);
1860 }
1861 
1862 /*
1863  * Get file handle system call.
1864  * Takes file name and returns a file handle for it.
1865  * Credentials must be verified before calling.
1866  */
1867 int
1868 nfs_getfh(struct nfs_getfh_args *args, model_t model, cred_t *cr)
1869 {
1870 	nfs_fh3 fh;
1871 	char buf[NFS3_MAXFHSIZE];
1872 	char *logptr, logbuf[NFS3_MAXFHSIZE];
1873 	int l = NFS3_MAXFHSIZE;
1874 	vnode_t *vp;
1875 	vnode_t *dvp;
1876 	struct exportinfo *exi;
1877 	int error;
1878 	int vers;
1879 	STRUCT_HANDLE(nfs_getfh_args, uap);
1880 
1881 #ifdef lint
1882 	model = model;		/* STRUCT macros don't always use it */
1883 #endif
1884 
1885 	STRUCT_SET_HANDLE(uap, model, args);
1886 
1887 	error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1888 	    FOLLOW, &dvp, &vp);
1889 	if (error == EINVAL) {
1890 		/*
1891 		 * if fname resolves to / we get EINVAL error
1892 		 * since we wanted the parent vnode. Try again
1893 		 * with NULL dvp.
1894 		 */
1895 		error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1896 		    FOLLOW, NULL, &vp);
1897 		dvp = NULL;
1898 	}
1899 	if (!error && vp == NULL) {
1900 		/*
1901 		 * Last component of fname not found
1902 		 */
1903 		if (dvp != NULL) {
1904 			VN_RELE(dvp);
1905 		}
1906 		error = ENOENT;
1907 	}
1908 	if (error)
1909 		return (error);
1910 
1911 	/*
1912 	 * 'vp' may be an AUTOFS node, so we perform a
1913 	 * VOP_ACCESS() to trigger the mount of the
1914 	 * intended filesystem, so we can share the intended
1915 	 * filesystem instead of the AUTOFS filesystem.
1916 	 */
1917 	(void) VOP_ACCESS(vp, 0, 0, cr);
1918 
1919 	/*
1920 	 * We're interested in the top most filesystem.
1921 	 * This is specially important when uap->dname is a trigger
1922 	 * AUTOFS node, since we're really interested in sharing the
1923 	 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1924 	 * call not the AUTOFS node itself.
1925 	 */
1926 	if (vn_mountedvfs(vp) != NULL) {
1927 		if (error = traverse(&vp)) {
1928 			VN_RELE(vp);
1929 			if (dvp != NULL)
1930 				VN_RELE(dvp);
1931 			return (error);
1932 		}
1933 	}
1934 
1935 	vers = STRUCT_FGET(uap, vers);
1936 	exi = nfs_vptoexi(dvp, vp, cr, NULL, &error, FALSE);
1937 	if (!error) {
1938 		if (vers == NFS_VERSION) {
1939 			error = makefh((fhandle_t *)buf, vp, exi);
1940 			l = NFS_FHSIZE;
1941 			logptr = buf;
1942 		} else if (vers == NFS_V3) {
1943 			int i, sz, pad;
1944 
1945 			error = makefh3(&fh, vp, exi);
1946 			l = fh.fh3_length;
1947 			logptr = logbuf;
1948 			if (!error) {
1949 				i = 0;
1950 				sz = sizeof (fsid_t);
1951 				bcopy(&fh.fh3_fsid, &buf[i], sz);
1952 				i += sz;
1953 
1954 				/*
1955 				 * For backwards compatability, the
1956 				 * fid length may be less than
1957 				 * NFS_FHMAXDATA, but it was always
1958 				 * encoded as NFS_FHMAXDATA bytes.
1959 				 */
1960 
1961 				sz = sizeof (ushort_t);
1962 				bcopy(&fh.fh3_len, &buf[i], sz);
1963 				i += sz;
1964 				bcopy(fh.fh3_data, &buf[i], fh.fh3_len);
1965 				i += fh.fh3_len;
1966 				pad = (NFS_FHMAXDATA - fh.fh3_len);
1967 				if (pad > 0) {
1968 					bzero(&buf[i], pad);
1969 					i += pad;
1970 					l += pad;
1971 				}
1972 
1973 				sz = sizeof (ushort_t);
1974 				bcopy(&fh.fh3_xlen, &buf[i], sz);
1975 				i += sz;
1976 				bcopy(fh.fh3_xdata, &buf[i], fh.fh3_xlen);
1977 				i += fh.fh3_xlen;
1978 				pad = (NFS_FHMAXDATA - fh.fh3_xlen);
1979 				if (pad > 0) {
1980 					bzero(&buf[i], pad);
1981 					i += pad;
1982 					l += pad;
1983 				}
1984 			}
1985 			/*
1986 			 * If we need to do NFS logging, the filehandle
1987 			 * must be downsized to 32 bytes.
1988 			 */
1989 			if (!error && exi->exi_export.ex_flags & EX_LOG) {
1990 				i = 0;
1991 				sz = sizeof (fsid_t);
1992 				bcopy(&fh.fh3_fsid, &logbuf[i], sz);
1993 				i += sz;
1994 				sz = sizeof (ushort_t);
1995 				bcopy(&fh.fh3_len, &logbuf[i], sz);
1996 				i += sz;
1997 				sz = NFS_FHMAXDATA;
1998 				bcopy(fh.fh3_data, &logbuf[i], sz);
1999 				i += sz;
2000 				sz = sizeof (ushort_t);
2001 				bcopy(&fh.fh3_xlen, &logbuf[i], sz);
2002 				i += sz;
2003 				sz = NFS_FHMAXDATA;
2004 				bcopy(fh.fh3_xdata, &logbuf[i], sz);
2005 				i += sz;
2006 			}
2007 		}
2008 		if (!error && exi->exi_export.ex_flags & EX_LOG) {
2009 			nfslog_getfh(exi, (fhandle_t *)logptr,
2010 			    STRUCT_FGETP(uap, fname), UIO_USERSPACE, cr);
2011 		}
2012 		exi_rele(exi);
2013 		if (!error) {
2014 			if (copyout(&l, STRUCT_FGETP(uap, lenp), sizeof (int)))
2015 				error = EFAULT;
2016 			if (copyout(buf, STRUCT_FGETP(uap, fhp), l))
2017 				error = EFAULT;
2018 		}
2019 	}
2020 	VN_RELE(vp);
2021 	if (dvp != NULL) {
2022 		VN_RELE(dvp);
2023 	}
2024 	return (error);
2025 }
2026 
2027 /*
2028  * Strategy: if vp is in the export list, then
2029  * return the associated file handle. Otherwise, ".."
2030  * once up the vp and try again, until the root of the
2031  * filesystem is reached.
2032  */
2033 struct   exportinfo *
2034 nfs_vptoexi(vnode_t *dvp, vnode_t *vp, cred_t *cr, int *walk,
2035 	int *err,  bool_t v4srv)
2036 {
2037 	fid_t fid;
2038 	int error;
2039 	struct exportinfo *exi;
2040 
2041 	ASSERT(vp);
2042 	VN_HOLD(vp);
2043 	if (dvp != NULL) {
2044 		VN_HOLD(dvp);
2045 	}
2046 	if (walk != NULL)
2047 		*walk = 0;
2048 
2049 	for (;;) {
2050 		bzero(&fid, sizeof (fid));
2051 		fid.fid_len = MAXFIDSZ;
2052 		error = vop_fid_pseudo(vp, &fid);
2053 		if (error) {
2054 			/*
2055 			 * If vop_fid_pseudo returns ENOSPC then the fid
2056 			 * supplied is too small. For now we simply
2057 			 * return EREMOTE.
2058 			 */
2059 			if (error == ENOSPC)
2060 				error = EREMOTE;
2061 			break;
2062 		}
2063 
2064 		if (v4srv)
2065 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
2066 		else
2067 			exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid);
2068 
2069 		if (exi != NULL) {
2070 			/*
2071 			 * Found the export info
2072 			 */
2073 			break;
2074 		}
2075 
2076 		/*
2077 		 * We have just failed finding a matching export.
2078 		 * If we're at the root of this filesystem, then
2079 		 * it's time to stop (with failure).
2080 		 */
2081 		if (vp->v_flag & VROOT) {
2082 			error = EINVAL;
2083 			break;
2084 		}
2085 
2086 		if (walk != NULL)
2087 			(*walk)++;
2088 
2089 		/*
2090 		 * Now, do a ".." up vp. If dvp is supplied, use it,
2091 		 * otherwise, look it up.
2092 		 */
2093 		if (dvp == NULL) {
2094 			error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, cr);
2095 			if (error)
2096 				break;
2097 		}
2098 		VN_RELE(vp);
2099 		vp = dvp;
2100 		dvp = NULL;
2101 	}
2102 	VN_RELE(vp);
2103 	if (dvp != NULL) {
2104 		VN_RELE(dvp);
2105 	}
2106 	if (error != 0) {
2107 		if (err != NULL)
2108 			*err = error;
2109 		return (NULL);
2110 	}
2111 	return (exi);
2112 }
2113 
2114 int
2115 chk_clnt_sec(exportinfo_t *exi, struct svc_req *req)
2116 {
2117 	int i, nfsflavor;
2118 	struct secinfo *sp;
2119 
2120 	/*
2121 	 *  Get the nfs flavor number from xprt.
2122 	 */
2123 	nfsflavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
2124 
2125 	sp = exi->exi_export.ex_secinfo;
2126 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2127 		if ((nfsflavor == sp[i].s_secinfo.sc_nfsnum) &&
2128 		    SEC_REF_EXPORTED(sp + i))
2129 			return (TRUE);
2130 	}
2131 	return (FALSE);
2132 }
2133 
2134 /*
2135  * Make an fhandle from a vnode
2136  */
2137 int
2138 makefh(fhandle_t *fh, vnode_t *vp, exportinfo_t *exi)
2139 {
2140 	int error;
2141 
2142 	*fh = exi->exi_fh;	/* struct copy */
2143 
2144 	error = VOP_FID(vp, (fid_t *)&fh->fh_len);
2145 	if (error) {
2146 		/*
2147 		 * Should be something other than EREMOTE
2148 		 */
2149 		return (EREMOTE);
2150 	}
2151 	return (0);
2152 }
2153 
2154 /*
2155  * This routine makes an overloaded V2 fhandle which contains
2156  * sec modes.
2157  *
2158  * Note that the first four octets contain the length octet,
2159  * the status octet, and two padded octets to make them XDR
2160  * four-octet aligned.
2161  *
2162  *   1   2   3   4                                          32
2163  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2164  * | l | s |   |   |     sec_1     |...|     sec_n     |...|   |
2165  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2166  *
2167  * where
2168  *
2169  *   the status octet s indicates whether there are more security
2170  *   flavors (1 means yes, 0 means no) that require the client to
2171  *   perform another 0x81 LOOKUP to get them,
2172  *
2173  *   the length octet l is the length describing the number of
2174  *   valid octets that follow.  (l = 4 * n, where n is the number
2175  *   of security flavors sent in the current overloaded filehandle.)
2176  *
2177  *   sec_index should always be in the inclusive range: [1 - ex_seccnt],
2178  *   and it tells server where to start within the secinfo array.
2179  *   Usually it will always be 1; however, if more flavors are used
2180  *   for the public export than can be encoded in the overloaded FH
2181  *   (7 for NFS2), subsequent SNEGO MCLs will have a larger index
2182  *   so the server will pick up where it left off from the previous
2183  *   MCL reply.
2184  *
2185  *   With NFS4 support, implicitly allowed flavors are also in
2186  *   the secinfo array; however, they should not be returned in
2187  *   SNEGO MCL replies.
2188  */
2189 int
2190 makefh_ol(fhandle_t *fh, exportinfo_t *exi, uint_t sec_index)
2191 {
2192 	secinfo_t sec[MAX_FLAVORS];
2193 	int totalcnt, i, *ipt, cnt, seccnt, secidx, fh_max_cnt;
2194 	char *c;
2195 
2196 	if (fh == NULL || exi == NULL || sec_index < 1)
2197 		return (EREMOTE);
2198 
2199 	/*
2200 	 * WebNFS clients need to know the unique set of explicitly
2201 	 * shared flavors in used for the public export. When
2202 	 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2203 	 * shared flavors are included in the list.
2204 	 */
2205 	seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2206 	if (sec_index > seccnt)
2207 		return (EREMOTE);
2208 
2209 	fh_max_cnt = (NFS_FHSIZE / sizeof (int)) - 1;
2210 	totalcnt = seccnt - sec_index + 1;
2211 	cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2212 
2213 	c = (char *)fh;
2214 	/*
2215 	 * Encode the length octet representing the number of
2216 	 * security flavors (in bytes) in this overloaded fh.
2217 	 */
2218 	*c = cnt * sizeof (int);
2219 
2220 	/*
2221 	 * Encode the status octet that indicates whether there
2222 	 * are more security flavors the client needs to get.
2223 	 */
2224 	*(c + 1) = totalcnt > fh_max_cnt;
2225 
2226 	/*
2227 	 * put security flavors in the overloaded fh
2228 	 */
2229 	ipt = (int *)(c + sizeof (int32_t));
2230 	secidx = sec_index - 1;
2231 	for (i = 0; i < cnt; i++) {
2232 		ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2233 	}
2234 	return (0);
2235 }
2236 
2237 /*
2238  * Make an nfs_fh3 from a vnode
2239  */
2240 int
2241 makefh3(nfs_fh3 *fh, vnode_t *vp, struct exportinfo *exi)
2242 {
2243 	int error;
2244 	fid_t fid;
2245 
2246 	bzero(&fid, sizeof (fid));
2247 	fid.fid_len = MAXFIDSZ;
2248 	error = VOP_FID(vp, &fid);
2249 	if (error)
2250 		return (EREMOTE);
2251 
2252 	bzero(fh, sizeof (nfs_fh3));
2253 	fh->fh3_fsid = exi->exi_fsid;
2254 	fh->fh3_len = fid.fid_len;
2255 	bcopy(fid.fid_data, fh->fh3_data, fh->fh3_len);
2256 	fh->fh3_xlen = exi->exi_fid.fid_len;
2257 	bcopy(exi->exi_fid.fid_data, fh->fh3_xdata, fh->fh3_xlen);
2258 	fh->fh3_length = sizeof (fsid_t)
2259 	    + sizeof (ushort_t) + fh->fh3_len
2260 	    + sizeof (ushort_t) + fh->fh3_xlen;
2261 	fh->fh3_flags = 0;
2262 	return (0);
2263 }
2264 
2265 /*
2266  * This routine makes an overloaded V3 fhandle which contains
2267  * sec modes.
2268  *
2269  *  1        4
2270  * +--+--+--+--+
2271  * |    len    |
2272  * +--+--+--+--+
2273  *                                               up to 64
2274  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2275  * |s |  |  |  |   sec_1   |   sec_2   | ... |   sec_n   |
2276  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2277  *
2278  * len = 4 * (n+1), where n is the number of security flavors
2279  * sent in the current overloaded filehandle.
2280  *
2281  * the status octet s indicates whether there are more security
2282  * mechanisms (1 means yes, 0 means no) that require the client
2283  * to perform another 0x81 LOOKUP to get them.
2284  *
2285  * Three octets are padded after the status octet.
2286  */
2287 int
2288 makefh3_ol(nfs_fh3 *fh, struct exportinfo *exi, uint_t sec_index)
2289 {
2290 	secinfo_t sec[MAX_FLAVORS];
2291 	int totalcnt, cnt, *ipt, i, seccnt, fh_max_cnt, secidx;
2292 	char *c;
2293 
2294 	if (fh == NULL || exi == NULL || sec_index < 1)
2295 		return (EREMOTE);
2296 
2297 	/*
2298 	 * WebNFS clients need to know the unique set of explicitly
2299 	 * shared flavors in used for the public export. When
2300 	 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2301 	 * shared flavors are included in the list.
2302 	 */
2303 	seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2304 
2305 	if (sec_index > seccnt)
2306 		return (EREMOTE);
2307 
2308 	fh_max_cnt = (NFS3_FHSIZE / sizeof (int)) - 1;
2309 	totalcnt = seccnt - sec_index + 1;
2310 	cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2311 
2312 	/*
2313 	 * Place the length in fh3_length representing the number
2314 	 * of security flavors (in bytes) in this overloaded fh.
2315 	 */
2316 	fh->fh3_flags = FH_WEBNFS;
2317 	fh->fh3_length = (cnt+1) * sizeof (int32_t);
2318 
2319 	c = (char *)&fh->fh3_u.nfs_fh3_i.fh3_i;
2320 	/*
2321 	 * Encode the status octet that indicates whether there
2322 	 * are more security flavors the client needs to get.
2323 	 */
2324 	*c = totalcnt > fh_max_cnt;
2325 
2326 	/*
2327 	 * put security flavors in the overloaded fh
2328 	 */
2329 	secidx = sec_index - 1;
2330 	ipt = (int *)(c + sizeof (int32_t));
2331 	for (i = 0; i < cnt; i++) {
2332 		ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2333 	}
2334 	return (0);
2335 }
2336 
2337 /*
2338  * Make an nfs_fh4 from a vnode
2339  */
2340 int
2341 makefh4(nfs_fh4 *fh, vnode_t *vp, struct exportinfo *exi)
2342 {
2343 	int error;
2344 	nfs_fh4_fmt_t *fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2345 	fid_t fid;
2346 
2347 	bzero(&fid, sizeof (fid));
2348 	fid.fid_len = MAXFIDSZ;
2349 	/*
2350 	 * vop_fid_pseudo() is used to set up NFSv4 namespace, so
2351 	 * use vop_fid_pseudo() here to get the fid instead of VOP_FID.
2352 	 */
2353 	error = vop_fid_pseudo(vp, &fid);
2354 	if (error)
2355 		return (error);
2356 
2357 	fh->nfs_fh4_len = NFS_FH4_LEN;
2358 
2359 	fh_fmtp->fh4_i.fhx_fsid = exi->exi_fh.fh_fsid;
2360 	fh_fmtp->fh4_i.fhx_xlen = exi->exi_fh.fh_xlen;
2361 
2362 	bzero(fh_fmtp->fh4_i.fhx_data, sizeof (fh_fmtp->fh4_i.fhx_data));
2363 	bzero(fh_fmtp->fh4_i.fhx_xdata, sizeof (fh_fmtp->fh4_i.fhx_xdata));
2364 	bcopy(exi->exi_fh.fh_xdata, fh_fmtp->fh4_i.fhx_xdata,
2365 	    exi->exi_fh.fh_xlen);
2366 
2367 	fh_fmtp->fh4_len = fid.fid_len;
2368 	ASSERT(fid.fid_len <= sizeof (fh_fmtp->fh4_data));
2369 	bcopy(fid.fid_data, fh_fmtp->fh4_data, fid.fid_len);
2370 	fh_fmtp->fh4_flag = 0;
2371 
2372 #ifdef VOLATILE_FH_TEST
2373 	/*
2374 	 * XXX (temporary?)
2375 	 * Use the rnode volatile_id value to add volatility to the fh.
2376 	 *
2377 	 * For testing purposes there are currently two scenarios, based
2378 	 * on whether the filesystem was shared with "volatile_fh"
2379 	 * or "expire_on_rename". In the first case, use the value of
2380 	 * export struct share_time as the volatile_id. In the second
2381 	 * case use the vnode volatile_id value (which is set to the
2382 	 * time in which the file was renamed).
2383 	 *
2384 	 * Note that the above are temporary constructs for testing only
2385 	 * XXX
2386 	 */
2387 	if (exi->exi_export.ex_flags & EX_VOLRNM) {
2388 		fh_fmtp->fh4_volatile_id = find_volrnm_fh_id(exi, fh);
2389 	} else if (exi->exi_export.ex_flags & EX_VOLFH) {
2390 		fh_fmtp->fh4_volatile_id = exi->exi_volatile_id;
2391 	} else {
2392 		fh_fmtp->fh4_volatile_id = 0;
2393 	}
2394 #endif /* VOLATILE_FH_TEST */
2395 
2396 	return (0);
2397 }
2398 
2399 /*
2400  * Convert an fhandle into a vnode.
2401  * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2402  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2403  * are done with it.
2404  */
2405 vnode_t *
2406 nfs_fhtovp(fhandle_t *fh, struct exportinfo *exi)
2407 {
2408 	vfs_t *vfsp;
2409 	vnode_t *vp;
2410 	int error;
2411 	fid_t *fidp;
2412 
2413 	TRACE_0(TR_FAC_NFS, TR_FHTOVP_START,
2414 	    "fhtovp_start");
2415 
2416 	if (exi == NULL) {
2417 		TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2418 		    "fhtovp_end:(%S)", "exi NULL");
2419 		return (NULL);	/* not exported */
2420 	}
2421 
2422 	ASSERT(exi->exi_vp != NULL);
2423 
2424 	if (PUBLIC_FH2(fh)) {
2425 		if (exi->exi_export.ex_flags & EX_PUBLIC) {
2426 			TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2427 			    "fhtovp_end:(%S)", "root not exported");
2428 			return (NULL);
2429 		}
2430 		vp = exi->exi_vp;
2431 		VN_HOLD(vp);
2432 		return (vp);
2433 	}
2434 
2435 	vfsp = exi->exi_vp->v_vfsp;
2436 	ASSERT(vfsp != NULL);
2437 	fidp = (fid_t *)&fh->fh_len;
2438 
2439 	error = VFS_VGET(vfsp, &vp, fidp);
2440 	if (error || vp == NULL) {
2441 		TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2442 		    "fhtovp_end:(%S)", "VFS_GET failed or vp NULL");
2443 		return (NULL);
2444 	}
2445 	TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2446 	    "fhtovp_end:(%S)", "end");
2447 	return (vp);
2448 }
2449 
2450 /*
2451  * Convert an fhandle into a vnode.
2452  * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2453  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2454  * are done with it.
2455  * This is just like nfs_fhtovp() but without the exportinfo argument.
2456  */
2457 
2458 vnode_t *
2459 lm_fhtovp(fhandle_t *fh)
2460 {
2461 	register vfs_t *vfsp;
2462 	vnode_t *vp;
2463 	int error;
2464 
2465 	vfsp = getvfs(&fh->fh_fsid);
2466 	if (vfsp == NULL)
2467 		return (NULL);
2468 
2469 	error = VFS_VGET(vfsp, &vp, (fid_t *)&(fh->fh_len));
2470 	VFS_RELE(vfsp);
2471 	if (error || vp == NULL)
2472 		return (NULL);
2473 
2474 	return (vp);
2475 }
2476 
2477 /*
2478  * Convert an nfs_fh3 into a vnode.
2479  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2480  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2481  * are done with it.
2482  */
2483 vnode_t *
2484 nfs3_fhtovp(nfs_fh3 *fh, struct exportinfo *exi)
2485 {
2486 	vfs_t *vfsp;
2487 	vnode_t *vp;
2488 	int error;
2489 	fid_t *fidp;
2490 
2491 	if (exi == NULL)
2492 		return (NULL);	/* not exported */
2493 
2494 	ASSERT(exi->exi_vp != NULL);
2495 
2496 	if (PUBLIC_FH3(fh)) {
2497 		if (exi->exi_export.ex_flags & EX_PUBLIC)
2498 			return (NULL);
2499 		vp = exi->exi_vp;
2500 		VN_HOLD(vp);
2501 		return (vp);
2502 	}
2503 
2504 	if (fh->fh3_length < NFS3_OLDFHSIZE ||
2505 	    fh->fh3_length > NFS3_MAXFHSIZE)
2506 		return (NULL);
2507 
2508 	vfsp = exi->exi_vp->v_vfsp;
2509 	ASSERT(vfsp != NULL);
2510 	fidp = FH3TOFIDP(fh);
2511 
2512 	error = VFS_VGET(vfsp, &vp, fidp);
2513 	if (error || vp == NULL)
2514 		return (NULL);
2515 
2516 	return (vp);
2517 }
2518 
2519 /*
2520  * Convert an nfs_fh3 into a vnode.
2521  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2522  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2523  * are done with it.
2524  * BTW: This is just like nfs3_fhtovp() but without the exportinfo arg.
2525  * Also, vfsp is accessed through getvfs() rather using exportinfo !!
2526  */
2527 
2528 vnode_t *
2529 lm_nfs3_fhtovp(nfs_fh3 *fh)
2530 {
2531 	vfs_t *vfsp;
2532 	vnode_t *vp;
2533 	int error;
2534 	fid_t *fidp;
2535 
2536 	if (fh->fh3_length < NFS3_OLDFHSIZE ||
2537 	    fh->fh3_length > NFS3_MAXFHSIZE)
2538 		return (NULL);
2539 
2540 	vfsp = getvfs(&fh->fh3_fsid);
2541 	if (vfsp == NULL)
2542 		return (NULL);
2543 	fidp = FH3TOFIDP(fh);
2544 
2545 	error = VFS_VGET(vfsp, &vp, fidp);
2546 	VFS_RELE(vfsp);
2547 	if (error || vp == NULL)
2548 		return (NULL);
2549 
2550 	return (vp);
2551 }
2552 
2553 /*
2554  * Convert an nfs_fh4 into a vnode.
2555  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2556  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2557  * are done with it.
2558  */
2559 vnode_t *
2560 nfs4_fhtovp(nfs_fh4 *fh, struct exportinfo *exi, nfsstat4 *statp)
2561 {
2562 	vfs_t *vfsp;
2563 	vnode_t *vp = NULL;
2564 	int error;
2565 	fid_t *fidp;
2566 	nfs_fh4_fmt_t *fh_fmtp;
2567 #ifdef VOLATILE_FH_TEST
2568 	uint32_t volatile_id = 0;
2569 #endif /* VOLATILE_FH_TEST */
2570 
2571 	if (exi == NULL) {
2572 		*statp = NFS4ERR_STALE;
2573 		return (NULL);	/* not exported */
2574 	}
2575 	ASSERT(exi->exi_vp != NULL);
2576 
2577 	/* caller should have checked this */
2578 	ASSERT(fh->nfs_fh4_len >= NFS_FH4_LEN);
2579 
2580 	fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2581 	vfsp = exi->exi_vp->v_vfsp;
2582 	ASSERT(vfsp != NULL);
2583 	fidp = (fid_t *)&fh_fmtp->fh4_len;
2584 
2585 #ifdef VOLATILE_FH_TEST
2586 	/* XXX check if volatile - should be changed later */
2587 	if (exi->exi_export.ex_flags & (EX_VOLRNM | EX_VOLFH)) {
2588 		/*
2589 		 * Filesystem is shared with volatile filehandles
2590 		 */
2591 		if (exi->exi_export.ex_flags & EX_VOLRNM)
2592 			volatile_id = find_volrnm_fh_id(exi, fh);
2593 		else
2594 			volatile_id = exi->exi_volatile_id;
2595 
2596 		if (fh_fmtp->fh4_volatile_id != volatile_id) {
2597 			*statp = NFS4ERR_FHEXPIRED;
2598 			return (NULL);
2599 		}
2600 	}
2601 	/*
2602 	 * XXX even if test_volatile_fh false, the fh may contain a
2603 	 * volatile id if obtained when the test was set.
2604 	 */
2605 	fh_fmtp->fh4_volatile_id = (uchar_t)0;
2606 #endif /* VOLATILE_FH_TEST */
2607 
2608 	error = VFS_VGET(vfsp, &vp, fidp);
2609 	/*
2610 	 * If we can not get vp from VFS_VGET, perhaps this is
2611 	 * an nfs v2/v3/v4 node in an nfsv4 pseudo filesystem.
2612 	 * Check it out.
2613 	 */
2614 	if (error && PSEUDO(exi))
2615 		error = nfs4_vget_pseudo(exi, &vp, fidp);
2616 
2617 	if (error || vp == NULL) {
2618 		*statp = NFS4ERR_STALE;
2619 		return (NULL);
2620 	}
2621 	/* XXX - disgusting hack */
2622 	if (vp->v_type == VNON && vp->v_flag & V_XATTRDIR)
2623 		vp->v_type = VDIR;
2624 	*statp = NFS4_OK;
2625 	return (vp);
2626 }
2627 
2628 /*
2629  * Find the export structure associated with the given filesystem.
2630  * If found, then increment the ref count (exi_count).
2631  */
2632 struct exportinfo *
2633 checkexport(fsid_t *fsid, fid_t *fid)
2634 {
2635 	struct exportinfo *exi;
2636 
2637 	rw_enter(&exported_lock, RW_READER);
2638 	for (exi = exptable[exptablehash(fsid, fid)];
2639 	    exi != NULL;
2640 	    exi = exi->exi_hash) {
2641 		if (exportmatch(exi, fsid, fid)) {
2642 			/*
2643 			 * If this is the place holder for the
2644 			 * public file handle, then return the
2645 			 * real export entry for the public file
2646 			 * handle.
2647 			 */
2648 			if (exi->exi_export.ex_flags & EX_PUBLIC) {
2649 				exi = exi_public;
2650 			}
2651 			mutex_enter(&exi->exi_lock);
2652 			exi->exi_count++;
2653 			mutex_exit(&exi->exi_lock);
2654 			rw_exit(&exported_lock);
2655 			return (exi);
2656 		}
2657 	}
2658 	rw_exit(&exported_lock);
2659 	return (NULL);
2660 }
2661 
2662 
2663 /*
2664  * "old school" version of checkexport() for NFS4.  NFS4
2665  * rfs4_compound holds exported_lock for duration of compound
2666  * processing.  This version doesn't manipulate exi_count
2667  * since NFS4 breaks fundamental assumptions in the exi_count
2668  * design.
2669  */
2670 struct exportinfo *
2671 checkexport4(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2672 {
2673 	struct exportinfo *exi;
2674 
2675 	ASSERT(RW_LOCK_HELD(&exported_lock));
2676 
2677 	for (exi = exptable[exptablehash(fsid, fid)];
2678 	    exi != NULL;
2679 	    exi = exi->exi_hash) {
2680 		if (exportmatch(exi, fsid, fid)) {
2681 			/*
2682 			 * If this is the place holder for the
2683 			 * public file handle, then return the
2684 			 * real export entry for the public file
2685 			 * handle.
2686 			 */
2687 			if (exi->exi_export.ex_flags & EX_PUBLIC) {
2688 				exi = exi_public;
2689 			}
2690 
2691 			/*
2692 			 * If vp is given, check if vp is the
2693 			 * same vnode as the exported node.
2694 			 *
2695 			 * Since VOP_FID of a lofs node returns the
2696 			 * fid of its real node (ufs), the exported
2697 			 * node for lofs and (pseudo) ufs may have
2698 			 * the same fsid and fid.
2699 			 */
2700 			if (vp == NULL || vp == exi->exi_vp)
2701 				return (exi);
2702 		}
2703 	}
2704 
2705 	return (NULL);
2706 }
2707 
2708 /*
2709  * Free an entire export list node
2710  */
2711 void
2712 exportfree(struct exportinfo *exi)
2713 {
2714 	struct exportdata *ex;
2715 
2716 	ex = &exi->exi_export;
2717 
2718 	ASSERT(exi->exi_vp != NULL && !(exi->exi_export.ex_flags & EX_PUBLIC));
2719 	VN_RELE(exi->exi_vp);
2720 	if (exi->exi_dvp != NULL)
2721 		VN_RELE(exi->exi_dvp);
2722 
2723 	if (ex->ex_flags & EX_INDEX)
2724 		kmem_free(ex->ex_index, strlen(ex->ex_index) + 1);
2725 
2726 	kmem_free(ex->ex_path, ex->ex_pathlen + 1);
2727 	nfsauth_cache_free(exi);
2728 
2729 	if (exi->exi_logbuffer != NULL)
2730 		nfslog_disable(exi);
2731 
2732 	if (ex->ex_flags & EX_LOG) {
2733 		kmem_free(ex->ex_log_buffer, ex->ex_log_bufferlen + 1);
2734 		kmem_free(ex->ex_tag, ex->ex_taglen + 1);
2735 	}
2736 
2737 	if (exi->exi_visible)
2738 		free_visible(exi->exi_visible);
2739 
2740 	srv_secinfo_list_free(ex->ex_secinfo, ex->ex_seccnt);
2741 
2742 #ifdef VOLATILE_FH_TEST
2743 	free_volrnm_list(exi);
2744 	mutex_destroy(&exi->exi_vol_rename_lock);
2745 #endif /* VOLATILE_FH_TEST */
2746 
2747 	mutex_destroy(&exi->exi_lock);
2748 	rw_destroy(&exi->exi_cache_lock);
2749 
2750 	kmem_free(exi, sizeof (*exi));
2751 }
2752 
2753 /*
2754  * load the index file from user space into kernel space.
2755  */
2756 static int
2757 loadindex(struct exportdata *kex)
2758 {
2759 	int error;
2760 	char index[MAXNAMELEN+1];
2761 	size_t len;
2762 
2763 	/*
2764 	 * copyinstr copies the complete string including the NULL and
2765 	 * returns the len with the NULL byte included in the calculation
2766 	 * as long as the max length is not exceeded.
2767 	 */
2768 	if (error = copyinstr(kex->ex_index, index, sizeof (index), &len))
2769 		return (error);
2770 
2771 	kex->ex_index = kmem_alloc(len, KM_SLEEP);
2772 	bcopy(index, kex->ex_index, len);
2773 
2774 	return (0);
2775 }
2776 
2777 /*
2778  * When a thread completes using exi, it should call exi_rele().
2779  * exi_rele() decrements exi_count. It releases exi if exi_count == 0, i.e.
2780  * if this is the last user of exi and exi is not on exportinfo list anymore
2781  */
2782 void
2783 exi_rele(struct exportinfo *exi)
2784 {
2785 	mutex_enter(&exi->exi_lock);
2786 	exi->exi_count--;
2787 	if (exi->exi_count == 0) {
2788 		mutex_exit(&exi->exi_lock);
2789 		exportfree(exi);
2790 	} else
2791 		mutex_exit(&exi->exi_lock);
2792 }
2793 
2794 #ifdef VOLATILE_FH_TEST
2795 /*
2796  * Test for volatile fh's - add file handle to list and set its volatile id
2797  * to time it was renamed. If EX_VOLFH is also on and the fs is reshared,
2798  * the vol_rename queue is purged.
2799  *
2800  * XXX This code is for unit testing purposes only... To correctly use it, it
2801  * needs to tie a rename list to the export struct and (more
2802  * important), protect access to the exi rename list using a write lock.
2803  */
2804 
2805 /*
2806  * get the fh vol record if it's in the volatile on rename list. Don't check
2807  * volatile_id in the file handle - compare only the file handles.
2808  */
2809 static struct ex_vol_rename *
2810 find_volrnm_fh(struct exportinfo *exi, nfs_fh4 *fh4p)
2811 {
2812 	struct ex_vol_rename *p = NULL;
2813 	fhandle4_t *fhp;
2814 
2815 	/* XXX shouldn't we assert &exported_lock held? */
2816 	ASSERT(MUTEX_HELD(&exi->exi_vol_rename_lock));
2817 
2818 	if (fh4p->nfs_fh4_len != NFS_FH4_LEN) {
2819 		return (NULL);
2820 	}
2821 	fhp = &((nfs_fh4_fmt_t *)fh4p->nfs_fh4_val)->fh4_i;
2822 	for (p = exi->exi_vol_rename; p != NULL; p = p->vrn_next) {
2823 		if (bcmp(fhp, &p->vrn_fh_fmt.fh4_i,
2824 		    sizeof (fhandle4_t)) == 0)
2825 			break;
2826 	}
2827 	return (p);
2828 }
2829 
2830 /*
2831  * get the volatile id for the fh (if there is - else return 0). Ignore the
2832  * volatile_id in the file handle - compare only the file handles.
2833  */
2834 static uint32_t
2835 find_volrnm_fh_id(struct exportinfo *exi, nfs_fh4 *fh4p)
2836 {
2837 	struct ex_vol_rename *p;
2838 	uint32_t volatile_id;
2839 
2840 	mutex_enter(&exi->exi_vol_rename_lock);
2841 	p = find_volrnm_fh(exi, fh4p);
2842 	volatile_id = (p ? p->vrn_fh_fmt.fh4_volatile_id :
2843 	    exi->exi_volatile_id);
2844 	mutex_exit(&exi->exi_vol_rename_lock);
2845 	return (volatile_id);
2846 }
2847 
2848 /*
2849  * Free the volatile on rename list - will be called if a filesystem is
2850  * unshared or reshared without EX_VOLRNM
2851  */
2852 static void
2853 free_volrnm_list(struct exportinfo *exi)
2854 {
2855 	struct ex_vol_rename *p, *pnext;
2856 
2857 	/* no need to hold mutex lock - this one is called from exportfree */
2858 	for (p = exi->exi_vol_rename; p != NULL; p = pnext) {
2859 		pnext = p->vrn_next;
2860 		kmem_free(p, sizeof (*p));
2861 	}
2862 	exi->exi_vol_rename = NULL;
2863 }
2864 
2865 /*
2866  * Add a file handle to the volatile on rename list.
2867  */
2868 void
2869 add_volrnm_fh(struct exportinfo *exi, vnode_t *vp)
2870 {
2871 	struct ex_vol_rename *p;
2872 	char fhbuf[NFS4_FHSIZE];
2873 	nfs_fh4 fh4;
2874 	int error;
2875 
2876 	fh4.nfs_fh4_val = fhbuf;
2877 	error = makefh4(&fh4, vp, exi);
2878 	if ((error) || (fh4.nfs_fh4_len != sizeof (p->vrn_fh_fmt))) {
2879 		return;
2880 	}
2881 
2882 	mutex_enter(&exi->exi_vol_rename_lock);
2883 
2884 	p = find_volrnm_fh(exi, &fh4);
2885 
2886 	if (p == NULL) {
2887 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
2888 		bcopy(fh4.nfs_fh4_val, &p->vrn_fh_fmt, sizeof (p->vrn_fh_fmt));
2889 		p->vrn_next = exi->exi_vol_rename;
2890 		exi->exi_vol_rename = p;
2891 	}
2892 
2893 	p->vrn_fh_fmt.fh4_volatile_id = gethrestime_sec();
2894 	mutex_exit(&exi->exi_vol_rename_lock);
2895 }
2896 
2897 #endif /* VOLATILE_FH_TEST */
2898