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