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