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