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