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