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