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