xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_export.c (revision 15359501f7d4b9abebd7b7bf6efd5982a8e7eb27)
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 = fh.fh3_length;
1832 			logptr = logbuf;
1833 			if (!error) {
1834 				i = 0;
1835 				sz = sizeof (fsid_t);
1836 				bcopy(&fh.fh3_fsid, &buf[i], sz);
1837 				i += sz;
1838 
1839 				/*
1840 				 * For backwards compatibility, the
1841 				 * fid length may be less than
1842 				 * NFS_FHMAXDATA, but it was always
1843 				 * encoded as NFS_FHMAXDATA bytes.
1844 				 */
1845 
1846 				sz = sizeof (ushort_t);
1847 				bcopy(&fh.fh3_len, &buf[i], sz);
1848 				i += sz;
1849 				bcopy(fh.fh3_data, &buf[i], fh.fh3_len);
1850 				i += fh.fh3_len;
1851 				pad = (NFS_FHMAXDATA - fh.fh3_len);
1852 				if (pad > 0) {
1853 					bzero(&buf[i], pad);
1854 					i += pad;
1855 					l += pad;
1856 				}
1857 
1858 				sz = sizeof (ushort_t);
1859 				bcopy(&fh.fh3_xlen, &buf[i], sz);
1860 				i += sz;
1861 				bcopy(fh.fh3_xdata, &buf[i], fh.fh3_xlen);
1862 				i += fh.fh3_xlen;
1863 				pad = (NFS_FHMAXDATA - fh.fh3_xlen);
1864 				if (pad > 0) {
1865 					bzero(&buf[i], pad);
1866 					i += pad;
1867 					l += pad;
1868 				}
1869 			}
1870 			/*
1871 			 * If we need to do NFS logging, the filehandle
1872 			 * must be downsized to 32 bytes.
1873 			 */
1874 			if (!error && exi->exi_export.ex_flags & EX_LOG) {
1875 				i = 0;
1876 				sz = sizeof (fsid_t);
1877 				bcopy(&fh.fh3_fsid, &logbuf[i], sz);
1878 				i += sz;
1879 				sz = sizeof (ushort_t);
1880 				bcopy(&fh.fh3_len, &logbuf[i], sz);
1881 				i += sz;
1882 				sz = NFS_FHMAXDATA;
1883 				bcopy(fh.fh3_data, &logbuf[i], sz);
1884 				i += sz;
1885 				sz = sizeof (ushort_t);
1886 				bcopy(&fh.fh3_xlen, &logbuf[i], sz);
1887 				i += sz;
1888 				sz = NFS_FHMAXDATA;
1889 				bcopy(fh.fh3_xdata, &logbuf[i], sz);
1890 				i += sz;
1891 			}
1892 		}
1893 		if (!error && exi->exi_export.ex_flags & EX_LOG) {
1894 			nfslog_getfh(exi, (fhandle_t *)logptr,
1895 			    STRUCT_FGETP(uap, fname), UIO_USERSPACE, cr);
1896 		}
1897 		exi_rele(exi);
1898 		if (!error) {
1899 			if (copyout(&l, STRUCT_FGETP(uap, lenp), sizeof (int)))
1900 				error = EFAULT;
1901 			if (copyout(buf, STRUCT_FGETP(uap, fhp), l))
1902 				error = EFAULT;
1903 		}
1904 	}
1905 	VN_RELE(vp);
1906 	if (dvp != NULL) {
1907 		VN_RELE(dvp);
1908 	}
1909 	return (error);
1910 }
1911 
1912 /*
1913  * Strategy: if vp is in the export list, then
1914  * return the associated file handle. Otherwise, ".."
1915  * once up the vp and try again, until the root of the
1916  * filesystem is reached.
1917  */
1918 struct   exportinfo *
1919 nfs_vptoexi(vnode_t *dvp, vnode_t *vp, cred_t *cr, int *walk,
1920 	int *err,  bool_t v4srv)
1921 {
1922 	fid_t fid;
1923 	int error;
1924 	struct exportinfo *exi;
1925 
1926 	ASSERT(vp);
1927 	VN_HOLD(vp);
1928 	if (dvp != NULL) {
1929 		VN_HOLD(dvp);
1930 	}
1931 	if (walk != NULL)
1932 		*walk = 0;
1933 
1934 	for (;;) {
1935 		bzero(&fid, sizeof (fid));
1936 		fid.fid_len = MAXFIDSZ;
1937 		error = vop_fid_pseudo(vp, &fid);
1938 		if (error) {
1939 			/*
1940 			 * If vop_fid_pseudo returns ENOSPC then the fid
1941 			 * supplied is too small. For now we simply
1942 			 * return EREMOTE.
1943 			 */
1944 			if (error == ENOSPC)
1945 				error = EREMOTE;
1946 			break;
1947 		}
1948 
1949 		if (v4srv)
1950 			exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
1951 		else
1952 			exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid);
1953 
1954 		if (exi != NULL) {
1955 			/*
1956 			 * Found the export info
1957 			 */
1958 			break;
1959 		}
1960 
1961 		/*
1962 		 * We have just failed finding a matching export.
1963 		 * If we're at the root of this filesystem, then
1964 		 * it's time to stop (with failure).
1965 		 */
1966 		if (vp->v_flag & VROOT) {
1967 			error = EINVAL;
1968 			break;
1969 		}
1970 
1971 		if (walk != NULL)
1972 			(*walk)++;
1973 
1974 		/*
1975 		 * Now, do a ".." up vp. If dvp is supplied, use it,
1976 		 * otherwise, look it up.
1977 		 */
1978 		if (dvp == NULL) {
1979 			error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, cr,
1980 			    NULL, NULL, NULL);
1981 			if (error)
1982 				break;
1983 		}
1984 		VN_RELE(vp);
1985 		vp = dvp;
1986 		dvp = NULL;
1987 	}
1988 	VN_RELE(vp);
1989 	if (dvp != NULL) {
1990 		VN_RELE(dvp);
1991 	}
1992 	if (error != 0) {
1993 		if (err != NULL)
1994 			*err = error;
1995 		return (NULL);
1996 	}
1997 	return (exi);
1998 }
1999 
2000 int
2001 chk_clnt_sec(exportinfo_t *exi, struct svc_req *req)
2002 {
2003 	int i, nfsflavor;
2004 	struct secinfo *sp;
2005 
2006 	/*
2007 	 *  Get the nfs flavor number from xprt.
2008 	 */
2009 	nfsflavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
2010 
2011 	sp = exi->exi_export.ex_secinfo;
2012 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2013 		if ((nfsflavor == sp[i].s_secinfo.sc_nfsnum) &&
2014 		    SEC_REF_EXPORTED(sp + i))
2015 			return (TRUE);
2016 	}
2017 	return (FALSE);
2018 }
2019 
2020 /*
2021  * Make an fhandle from a vnode
2022  */
2023 int
2024 makefh(fhandle_t *fh, vnode_t *vp, exportinfo_t *exi)
2025 {
2026 	int error;
2027 
2028 	*fh = exi->exi_fh;	/* struct copy */
2029 
2030 	error = VOP_FID(vp, (fid_t *)&fh->fh_len, NULL);
2031 	if (error) {
2032 		/*
2033 		 * Should be something other than EREMOTE
2034 		 */
2035 		return (EREMOTE);
2036 	}
2037 	return (0);
2038 }
2039 
2040 /*
2041  * This routine makes an overloaded V2 fhandle which contains
2042  * sec modes.
2043  *
2044  * Note that the first four octets contain the length octet,
2045  * the status octet, and two padded octets to make them XDR
2046  * four-octet aligned.
2047  *
2048  *   1   2   3   4                                          32
2049  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2050  * | l | s |   |   |     sec_1     |...|     sec_n     |...|   |
2051  * +---+---+---+---+---+---+---+---+   +---+---+---+---+   +---+
2052  *
2053  * where
2054  *
2055  *   the status octet s indicates whether there are more security
2056  *   flavors (1 means yes, 0 means no) that require the client to
2057  *   perform another 0x81 LOOKUP to get them,
2058  *
2059  *   the length octet l is the length describing the number of
2060  *   valid octets that follow.  (l = 4 * n, where n is the number
2061  *   of security flavors sent in the current overloaded filehandle.)
2062  *
2063  *   sec_index should always be in the inclusive range: [1 - ex_seccnt],
2064  *   and it tells server where to start within the secinfo array.
2065  *   Usually it will always be 1; however, if more flavors are used
2066  *   for the public export than can be encoded in the overloaded FH
2067  *   (7 for NFS2), subsequent SNEGO MCLs will have a larger index
2068  *   so the server will pick up where it left off from the previous
2069  *   MCL reply.
2070  *
2071  *   With NFS4 support, implicitly allowed flavors are also in
2072  *   the secinfo array; however, they should not be returned in
2073  *   SNEGO MCL replies.
2074  */
2075 int
2076 makefh_ol(fhandle_t *fh, exportinfo_t *exi, uint_t sec_index)
2077 {
2078 	secinfo_t sec[MAX_FLAVORS];
2079 	int totalcnt, i, *ipt, cnt, seccnt, secidx, fh_max_cnt;
2080 	char *c;
2081 
2082 	if (fh == NULL || exi == NULL || sec_index < 1)
2083 		return (EREMOTE);
2084 
2085 	/*
2086 	 * WebNFS clients need to know the unique set of explicitly
2087 	 * shared flavors in used for the public export. When
2088 	 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2089 	 * shared flavors are included in the list.
2090 	 */
2091 	seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2092 	if (sec_index > seccnt)
2093 		return (EREMOTE);
2094 
2095 	fh_max_cnt = (NFS_FHSIZE / sizeof (int)) - 1;
2096 	totalcnt = seccnt - sec_index + 1;
2097 	cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2098 
2099 	c = (char *)fh;
2100 	/*
2101 	 * Encode the length octet representing the number of
2102 	 * security flavors (in bytes) in this overloaded fh.
2103 	 */
2104 	*c = cnt * sizeof (int);
2105 
2106 	/*
2107 	 * Encode the status octet that indicates whether there
2108 	 * are more security flavors the client needs to get.
2109 	 */
2110 	*(c + 1) = totalcnt > fh_max_cnt;
2111 
2112 	/*
2113 	 * put security flavors in the overloaded fh
2114 	 */
2115 	ipt = (int *)(c + sizeof (int32_t));
2116 	secidx = sec_index - 1;
2117 	for (i = 0; i < cnt; i++) {
2118 		ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2119 	}
2120 	return (0);
2121 }
2122 
2123 /*
2124  * Make an nfs_fh3 from a vnode
2125  */
2126 int
2127 makefh3(nfs_fh3 *fh, vnode_t *vp, struct exportinfo *exi)
2128 {
2129 	int error;
2130 	fid_t fid;
2131 
2132 	bzero(&fid, sizeof (fid));
2133 	fid.fid_len = MAXFIDSZ;
2134 	error = VOP_FID(vp, &fid, NULL);
2135 	if (error)
2136 		return (EREMOTE);
2137 
2138 	bzero(fh, sizeof (nfs_fh3));
2139 	fh->fh3_fsid = exi->exi_fsid;
2140 	fh->fh3_len = fid.fid_len;
2141 	bcopy(fid.fid_data, fh->fh3_data, fh->fh3_len);
2142 	fh->fh3_xlen = exi->exi_fid.fid_len;
2143 	bcopy(exi->exi_fid.fid_data, fh->fh3_xdata, fh->fh3_xlen);
2144 	fh->fh3_length = sizeof (fsid_t)
2145 	    + sizeof (ushort_t) + fh->fh3_len
2146 	    + sizeof (ushort_t) + fh->fh3_xlen;
2147 	fh->fh3_flags = 0;
2148 	return (0);
2149 }
2150 
2151 /*
2152  * This routine makes an overloaded V3 fhandle which contains
2153  * sec modes.
2154  *
2155  *  1        4
2156  * +--+--+--+--+
2157  * |    len    |
2158  * +--+--+--+--+
2159  *                                               up to 64
2160  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2161  * |s |  |  |  |   sec_1   |   sec_2   | ... |   sec_n   |
2162  * +--+--+--+--+--+--+--+--+--+--+--+--+     +--+--+--+--+
2163  *
2164  * len = 4 * (n+1), where n is the number of security flavors
2165  * sent in the current overloaded filehandle.
2166  *
2167  * the status octet s indicates whether there are more security
2168  * mechanisms (1 means yes, 0 means no) that require the client
2169  * to perform another 0x81 LOOKUP to get them.
2170  *
2171  * Three octets are padded after the status octet.
2172  */
2173 int
2174 makefh3_ol(nfs_fh3 *fh, struct exportinfo *exi, uint_t sec_index)
2175 {
2176 	secinfo_t sec[MAX_FLAVORS];
2177 	int totalcnt, cnt, *ipt, i, seccnt, fh_max_cnt, secidx;
2178 	char *c;
2179 
2180 	if (fh == NULL || exi == NULL || sec_index < 1)
2181 		return (EREMOTE);
2182 
2183 	/*
2184 	 * WebNFS clients need to know the unique set of explicitly
2185 	 * shared flavors in used for the public export. When
2186 	 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2187 	 * shared flavors are included in the list.
2188 	 */
2189 	seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2190 
2191 	if (sec_index > seccnt)
2192 		return (EREMOTE);
2193 
2194 	fh_max_cnt = (NFS3_FHSIZE / sizeof (int)) - 1;
2195 	totalcnt = seccnt - sec_index + 1;
2196 	cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2197 
2198 	/*
2199 	 * Place the length in fh3_length representing the number
2200 	 * of security flavors (in bytes) in this overloaded fh.
2201 	 */
2202 	fh->fh3_flags = FH_WEBNFS;
2203 	fh->fh3_length = (cnt+1) * sizeof (int32_t);
2204 
2205 	c = (char *)&fh->fh3_u.nfs_fh3_i.fh3_i;
2206 	/*
2207 	 * Encode the status octet that indicates whether there
2208 	 * are more security flavors the client needs to get.
2209 	 */
2210 	*c = totalcnt > fh_max_cnt;
2211 
2212 	/*
2213 	 * put security flavors in the overloaded fh
2214 	 */
2215 	secidx = sec_index - 1;
2216 	ipt = (int *)(c + sizeof (int32_t));
2217 	for (i = 0; i < cnt; i++) {
2218 		ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2219 	}
2220 	return (0);
2221 }
2222 
2223 /*
2224  * Make an nfs_fh4 from a vnode
2225  */
2226 int
2227 makefh4(nfs_fh4 *fh, vnode_t *vp, struct exportinfo *exi)
2228 {
2229 	int error;
2230 	nfs_fh4_fmt_t *fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2231 	fid_t fid;
2232 
2233 	bzero(&fid, sizeof (fid));
2234 	fid.fid_len = MAXFIDSZ;
2235 	/*
2236 	 * vop_fid_pseudo() is used to set up NFSv4 namespace, so
2237 	 * use vop_fid_pseudo() here to get the fid instead of VOP_FID.
2238 	 */
2239 	error = vop_fid_pseudo(vp, &fid);
2240 	if (error)
2241 		return (error);
2242 
2243 	fh->nfs_fh4_len = NFS_FH4_LEN;
2244 
2245 	fh_fmtp->fh4_i.fhx_fsid = exi->exi_fh.fh_fsid;
2246 	fh_fmtp->fh4_i.fhx_xlen = exi->exi_fh.fh_xlen;
2247 
2248 	bzero(fh_fmtp->fh4_i.fhx_data, sizeof (fh_fmtp->fh4_i.fhx_data));
2249 	bzero(fh_fmtp->fh4_i.fhx_xdata, sizeof (fh_fmtp->fh4_i.fhx_xdata));
2250 	bcopy(exi->exi_fh.fh_xdata, fh_fmtp->fh4_i.fhx_xdata,
2251 	    exi->exi_fh.fh_xlen);
2252 
2253 	fh_fmtp->fh4_len = fid.fid_len;
2254 	ASSERT(fid.fid_len <= sizeof (fh_fmtp->fh4_data));
2255 	bcopy(fid.fid_data, fh_fmtp->fh4_data, fid.fid_len);
2256 	fh_fmtp->fh4_flag = 0;
2257 
2258 #ifdef VOLATILE_FH_TEST
2259 	/*
2260 	 * XXX (temporary?)
2261 	 * Use the rnode volatile_id value to add volatility to the fh.
2262 	 *
2263 	 * For testing purposes there are currently two scenarios, based
2264 	 * on whether the filesystem was shared with "volatile_fh"
2265 	 * or "expire_on_rename". In the first case, use the value of
2266 	 * export struct share_time as the volatile_id. In the second
2267 	 * case use the vnode volatile_id value (which is set to the
2268 	 * time in which the file was renamed).
2269 	 *
2270 	 * Note that the above are temporary constructs for testing only
2271 	 * XXX
2272 	 */
2273 	if (exi->exi_export.ex_flags & EX_VOLRNM) {
2274 		fh_fmtp->fh4_volatile_id = find_volrnm_fh_id(exi, fh);
2275 	} else if (exi->exi_export.ex_flags & EX_VOLFH) {
2276 		fh_fmtp->fh4_volatile_id = exi->exi_volatile_id;
2277 	} else {
2278 		fh_fmtp->fh4_volatile_id = 0;
2279 	}
2280 #endif /* VOLATILE_FH_TEST */
2281 
2282 	return (0);
2283 }
2284 
2285 /*
2286  * Convert an fhandle into a vnode.
2287  * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2288  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2289  * are done with it.
2290  */
2291 vnode_t *
2292 nfs_fhtovp(fhandle_t *fh, struct exportinfo *exi)
2293 {
2294 	vfs_t *vfsp;
2295 	vnode_t *vp;
2296 	int error;
2297 	fid_t *fidp;
2298 
2299 	TRACE_0(TR_FAC_NFS, TR_FHTOVP_START,
2300 	    "fhtovp_start");
2301 
2302 	if (exi == NULL) {
2303 		TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2304 		    "fhtovp_end:(%S)", "exi NULL");
2305 		return (NULL);	/* not exported */
2306 	}
2307 
2308 	ASSERT(exi->exi_vp != NULL);
2309 
2310 	if (PUBLIC_FH2(fh)) {
2311 		if (exi->exi_export.ex_flags & EX_PUBLIC) {
2312 			TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2313 			    "fhtovp_end:(%S)", "root not exported");
2314 			return (NULL);
2315 		}
2316 		vp = exi->exi_vp;
2317 		VN_HOLD(vp);
2318 		return (vp);
2319 	}
2320 
2321 	vfsp = exi->exi_vp->v_vfsp;
2322 	ASSERT(vfsp != NULL);
2323 	fidp = (fid_t *)&fh->fh_len;
2324 
2325 	error = VFS_VGET(vfsp, &vp, fidp);
2326 	if (error || vp == NULL) {
2327 		TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2328 		    "fhtovp_end:(%S)", "VFS_GET failed or vp NULL");
2329 		return (NULL);
2330 	}
2331 	TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2332 	    "fhtovp_end:(%S)", "end");
2333 	return (vp);
2334 }
2335 
2336 /*
2337  * Convert an fhandle into a vnode.
2338  * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2339  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2340  * are done with it.
2341  * This is just like nfs_fhtovp() but without the exportinfo argument.
2342  */
2343 
2344 vnode_t *
2345 lm_fhtovp(fhandle_t *fh)
2346 {
2347 	register vfs_t *vfsp;
2348 	vnode_t *vp;
2349 	int error;
2350 
2351 	vfsp = getvfs(&fh->fh_fsid);
2352 	if (vfsp == NULL)
2353 		return (NULL);
2354 
2355 	error = VFS_VGET(vfsp, &vp, (fid_t *)&(fh->fh_len));
2356 	VFS_RELE(vfsp);
2357 	if (error || vp == NULL)
2358 		return (NULL);
2359 
2360 	return (vp);
2361 }
2362 
2363 /*
2364  * Convert an nfs_fh3 into a vnode.
2365  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2366  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2367  * are done with it.
2368  */
2369 vnode_t *
2370 nfs3_fhtovp(nfs_fh3 *fh, struct exportinfo *exi)
2371 {
2372 	vfs_t *vfsp;
2373 	vnode_t *vp;
2374 	int error;
2375 	fid_t *fidp;
2376 
2377 	if (exi == NULL)
2378 		return (NULL);	/* not exported */
2379 
2380 	ASSERT(exi->exi_vp != NULL);
2381 
2382 	if (PUBLIC_FH3(fh)) {
2383 		if (exi->exi_export.ex_flags & EX_PUBLIC)
2384 			return (NULL);
2385 		vp = exi->exi_vp;
2386 		VN_HOLD(vp);
2387 		return (vp);
2388 	}
2389 
2390 	if (fh->fh3_length < NFS3_OLDFHSIZE ||
2391 	    fh->fh3_length > NFS3_MAXFHSIZE)
2392 		return (NULL);
2393 
2394 	vfsp = exi->exi_vp->v_vfsp;
2395 	ASSERT(vfsp != NULL);
2396 	fidp = FH3TOFIDP(fh);
2397 
2398 	error = VFS_VGET(vfsp, &vp, fidp);
2399 	if (error || vp == NULL)
2400 		return (NULL);
2401 
2402 	return (vp);
2403 }
2404 
2405 /*
2406  * Convert an nfs_fh3 into a vnode.
2407  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2408  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2409  * are done with it.
2410  * BTW: This is just like nfs3_fhtovp() but without the exportinfo arg.
2411  * Also, vfsp is accessed through getvfs() rather using exportinfo !!
2412  */
2413 
2414 vnode_t *
2415 lm_nfs3_fhtovp(nfs_fh3 *fh)
2416 {
2417 	vfs_t *vfsp;
2418 	vnode_t *vp;
2419 	int error;
2420 	fid_t *fidp;
2421 
2422 	if (fh->fh3_length < NFS3_OLDFHSIZE ||
2423 	    fh->fh3_length > NFS3_MAXFHSIZE)
2424 		return (NULL);
2425 
2426 	vfsp = getvfs(&fh->fh3_fsid);
2427 	if (vfsp == NULL)
2428 		return (NULL);
2429 	fidp = FH3TOFIDP(fh);
2430 
2431 	error = VFS_VGET(vfsp, &vp, fidp);
2432 	VFS_RELE(vfsp);
2433 	if (error || vp == NULL)
2434 		return (NULL);
2435 
2436 	return (vp);
2437 }
2438 
2439 /*
2440  * Convert an nfs_fh4 into a vnode.
2441  * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2442  * WARNING: users of this routine must do a VN_RELE on the vnode when they
2443  * are done with it.
2444  */
2445 vnode_t *
2446 nfs4_fhtovp(nfs_fh4 *fh, struct exportinfo *exi, nfsstat4 *statp)
2447 {
2448 	vfs_t *vfsp;
2449 	vnode_t *vp = NULL;
2450 	int error;
2451 	fid_t *fidp;
2452 	nfs_fh4_fmt_t *fh_fmtp;
2453 #ifdef VOLATILE_FH_TEST
2454 	uint32_t volatile_id = 0;
2455 #endif /* VOLATILE_FH_TEST */
2456 
2457 	if (exi == NULL) {
2458 		*statp = NFS4ERR_STALE;
2459 		return (NULL);	/* not exported */
2460 	}
2461 	ASSERT(exi->exi_vp != NULL);
2462 
2463 	/* caller should have checked this */
2464 	ASSERT(fh->nfs_fh4_len >= NFS_FH4_LEN);
2465 
2466 	fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2467 	vfsp = exi->exi_vp->v_vfsp;
2468 	ASSERT(vfsp != NULL);
2469 	fidp = (fid_t *)&fh_fmtp->fh4_len;
2470 
2471 #ifdef VOLATILE_FH_TEST
2472 	/* XXX check if volatile - should be changed later */
2473 	if (exi->exi_export.ex_flags & (EX_VOLRNM | EX_VOLFH)) {
2474 		/*
2475 		 * Filesystem is shared with volatile filehandles
2476 		 */
2477 		if (exi->exi_export.ex_flags & EX_VOLRNM)
2478 			volatile_id = find_volrnm_fh_id(exi, fh);
2479 		else
2480 			volatile_id = exi->exi_volatile_id;
2481 
2482 		if (fh_fmtp->fh4_volatile_id != volatile_id) {
2483 			*statp = NFS4ERR_FHEXPIRED;
2484 			return (NULL);
2485 		}
2486 	}
2487 	/*
2488 	 * XXX even if test_volatile_fh false, the fh may contain a
2489 	 * volatile id if obtained when the test was set.
2490 	 */
2491 	fh_fmtp->fh4_volatile_id = (uchar_t)0;
2492 #endif /* VOLATILE_FH_TEST */
2493 
2494 	error = VFS_VGET(vfsp, &vp, fidp);
2495 	/*
2496 	 * If we can not get vp from VFS_VGET, perhaps this is
2497 	 * an nfs v2/v3/v4 node in an nfsv4 pseudo filesystem.
2498 	 * Check it out.
2499 	 */
2500 	if (error && PSEUDO(exi))
2501 		error = nfs4_vget_pseudo(exi, &vp, fidp);
2502 
2503 	if (error || vp == NULL) {
2504 		*statp = NFS4ERR_STALE;
2505 		return (NULL);
2506 	}
2507 	/* XXX - disgusting hack */
2508 	if (vp->v_type == VNON && vp->v_flag & V_XATTRDIR)
2509 		vp->v_type = VDIR;
2510 	*statp = NFS4_OK;
2511 	return (vp);
2512 }
2513 
2514 /*
2515  * Find the export structure associated with the given filesystem.
2516  * If found, then increment the ref count (exi_count).
2517  */
2518 struct exportinfo *
2519 checkexport(fsid_t *fsid, fid_t *fid)
2520 {
2521 	struct exportinfo *exi;
2522 
2523 	rw_enter(&exported_lock, RW_READER);
2524 	for (exi = exptable[exptablehash(fsid, fid)];
2525 	    exi != NULL;
2526 	    exi = exi->exi_hash) {
2527 		if (exportmatch(exi, fsid, fid)) {
2528 			/*
2529 			 * If this is the place holder for the
2530 			 * public file handle, then return the
2531 			 * real export entry for the public file
2532 			 * handle.
2533 			 */
2534 			if (exi->exi_export.ex_flags & EX_PUBLIC) {
2535 				exi = exi_public;
2536 			}
2537 
2538 			exi_hold(exi);
2539 			rw_exit(&exported_lock);
2540 			return (exi);
2541 		}
2542 	}
2543 	rw_exit(&exported_lock);
2544 	return (NULL);
2545 }
2546 
2547 
2548 /*
2549  * "old school" version of checkexport() for NFS4.  NFS4
2550  * rfs4_compound holds exported_lock for duration of compound
2551  * processing.  This version doesn't manipulate exi_count
2552  * since NFS4 breaks fundamental assumptions in the exi_count
2553  * design.
2554  */
2555 struct exportinfo *
2556 checkexport4(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2557 {
2558 	struct exportinfo *exi;
2559 
2560 	ASSERT(RW_LOCK_HELD(&exported_lock));
2561 
2562 	for (exi = exptable[exptablehash(fsid, fid)];
2563 	    exi != NULL;
2564 	    exi = exi->exi_hash) {
2565 		if (exportmatch(exi, fsid, fid)) {
2566 			/*
2567 			 * If this is the place holder for the
2568 			 * public file handle, then return the
2569 			 * real export entry for the public file
2570 			 * handle.
2571 			 */
2572 			if (exi->exi_export.ex_flags & EX_PUBLIC) {
2573 				exi = exi_public;
2574 			}
2575 
2576 			/*
2577 			 * If vp is given, check if vp is the
2578 			 * same vnode as the exported node.
2579 			 *
2580 			 * Since VOP_FID of a lofs node returns the
2581 			 * fid of its real node (ufs), the exported
2582 			 * node for lofs and (pseudo) ufs may have
2583 			 * the same fsid and fid.
2584 			 */
2585 			if (vp == NULL || vp == exi->exi_vp)
2586 				return (exi);
2587 		}
2588 	}
2589 
2590 	return (NULL);
2591 }
2592 
2593 /*
2594  * Free an entire export list node
2595  */
2596 void
2597 exportfree(struct exportinfo *exi)
2598 {
2599 	struct exportdata *ex;
2600 	struct charset_cache *cache;
2601 
2602 	ex = &exi->exi_export;
2603 
2604 	ASSERT(exi->exi_vp != NULL && !(exi->exi_export.ex_flags & EX_PUBLIC));
2605 	VN_RELE(exi->exi_vp);
2606 	if (exi->exi_dvp != NULL)
2607 		VN_RELE(exi->exi_dvp);
2608 
2609 	if (ex->ex_flags & EX_INDEX)
2610 		kmem_free(ex->ex_index, strlen(ex->ex_index) + 1);
2611 
2612 	kmem_free(ex->ex_path, ex->ex_pathlen + 1);
2613 	nfsauth_cache_free(exi);
2614 
2615 	/*
2616 	 * if there is a character set mapping cached, clean it up.
2617 	 */
2618 	for (cache = exi->exi_charset; cache != NULL;
2619 	    cache = exi->exi_charset) {
2620 		if (cache->inbound != (kiconv_t)-1)
2621 			(void) kiconv_close(cache->inbound);
2622 		if (cache->outbound != (kiconv_t)-1)
2623 			(void) kiconv_close(cache->outbound);
2624 		exi->exi_charset = cache->next;
2625 		kmem_free(cache, sizeof (struct charset_cache));
2626 	}
2627 
2628 	if (exi->exi_logbuffer != NULL)
2629 		nfslog_disable(exi);
2630 
2631 	if (ex->ex_flags & EX_LOG) {
2632 		kmem_free(ex->ex_log_buffer, ex->ex_log_bufferlen + 1);
2633 		kmem_free(ex->ex_tag, ex->ex_taglen + 1);
2634 	}
2635 
2636 	if (exi->exi_visible)
2637 		free_visible(exi->exi_visible);
2638 
2639 	srv_secinfo_list_free(ex->ex_secinfo, ex->ex_seccnt);
2640 
2641 #ifdef VOLATILE_FH_TEST
2642 	free_volrnm_list(exi);
2643 	mutex_destroy(&exi->exi_vol_rename_lock);
2644 #endif /* VOLATILE_FH_TEST */
2645 
2646 	mutex_destroy(&exi->exi_lock);
2647 	rw_destroy(&exi->exi_cache_lock);
2648 
2649 	kmem_free(exi, sizeof (*exi));
2650 }
2651 
2652 /*
2653  * load the index file from user space into kernel space.
2654  */
2655 static int
2656 loadindex(struct exportdata *kex)
2657 {
2658 	int error;
2659 	char index[MAXNAMELEN+1];
2660 	size_t len;
2661 
2662 	/*
2663 	 * copyinstr copies the complete string including the NULL and
2664 	 * returns the len with the NULL byte included in the calculation
2665 	 * as long as the max length is not exceeded.
2666 	 */
2667 	if (error = copyinstr(kex->ex_index, index, sizeof (index), &len))
2668 		return (error);
2669 
2670 	kex->ex_index = kmem_alloc(len, KM_SLEEP);
2671 	bcopy(index, kex->ex_index, len);
2672 
2673 	return (0);
2674 }
2675 
2676 void
2677 exi_hold(struct exportinfo *exi)
2678 {
2679 	mutex_enter(&exi->exi_lock);
2680 	exi->exi_count++;
2681 	mutex_exit(&exi->exi_lock);
2682 }
2683 
2684 /*
2685  * When a thread completes using exi, it should call exi_rele().
2686  * exi_rele() decrements exi_count. It releases exi if exi_count == 0, i.e.
2687  * if this is the last user of exi and exi is not on exportinfo list anymore
2688  */
2689 void
2690 exi_rele(struct exportinfo *exi)
2691 {
2692 	mutex_enter(&exi->exi_lock);
2693 	exi->exi_count--;
2694 	if (exi->exi_count == 0) {
2695 		mutex_exit(&exi->exi_lock);
2696 		exportfree(exi);
2697 	} else
2698 		mutex_exit(&exi->exi_lock);
2699 }
2700 
2701 #ifdef VOLATILE_FH_TEST
2702 /*
2703  * Test for volatile fh's - add file handle to list and set its volatile id
2704  * to time it was renamed. If EX_VOLFH is also on and the fs is reshared,
2705  * the vol_rename queue is purged.
2706  *
2707  * XXX This code is for unit testing purposes only... To correctly use it, it
2708  * needs to tie a rename list to the export struct and (more
2709  * important), protect access to the exi rename list using a write lock.
2710  */
2711 
2712 /*
2713  * get the fh vol record if it's in the volatile on rename list. Don't check
2714  * volatile_id in the file handle - compare only the file handles.
2715  */
2716 static struct ex_vol_rename *
2717 find_volrnm_fh(struct exportinfo *exi, nfs_fh4 *fh4p)
2718 {
2719 	struct ex_vol_rename *p = NULL;
2720 	fhandle4_t *fhp;
2721 
2722 	/* XXX shouldn't we assert &exported_lock held? */
2723 	ASSERT(MUTEX_HELD(&exi->exi_vol_rename_lock));
2724 
2725 	if (fh4p->nfs_fh4_len != NFS_FH4_LEN) {
2726 		return (NULL);
2727 	}
2728 	fhp = &((nfs_fh4_fmt_t *)fh4p->nfs_fh4_val)->fh4_i;
2729 	for (p = exi->exi_vol_rename; p != NULL; p = p->vrn_next) {
2730 		if (bcmp(fhp, &p->vrn_fh_fmt.fh4_i,
2731 		    sizeof (fhandle4_t)) == 0)
2732 			break;
2733 	}
2734 	return (p);
2735 }
2736 
2737 /*
2738  * get the volatile id for the fh (if there is - else return 0). Ignore the
2739  * volatile_id in the file handle - compare only the file handles.
2740  */
2741 static uint32_t
2742 find_volrnm_fh_id(struct exportinfo *exi, nfs_fh4 *fh4p)
2743 {
2744 	struct ex_vol_rename *p;
2745 	uint32_t volatile_id;
2746 
2747 	mutex_enter(&exi->exi_vol_rename_lock);
2748 	p = find_volrnm_fh(exi, fh4p);
2749 	volatile_id = (p ? p->vrn_fh_fmt.fh4_volatile_id :
2750 	    exi->exi_volatile_id);
2751 	mutex_exit(&exi->exi_vol_rename_lock);
2752 	return (volatile_id);
2753 }
2754 
2755 /*
2756  * Free the volatile on rename list - will be called if a filesystem is
2757  * unshared or reshared without EX_VOLRNM
2758  */
2759 static void
2760 free_volrnm_list(struct exportinfo *exi)
2761 {
2762 	struct ex_vol_rename *p, *pnext;
2763 
2764 	/* no need to hold mutex lock - this one is called from exportfree */
2765 	for (p = exi->exi_vol_rename; p != NULL; p = pnext) {
2766 		pnext = p->vrn_next;
2767 		kmem_free(p, sizeof (*p));
2768 	}
2769 	exi->exi_vol_rename = NULL;
2770 }
2771 
2772 /*
2773  * Add a file handle to the volatile on rename list.
2774  */
2775 void
2776 add_volrnm_fh(struct exportinfo *exi, vnode_t *vp)
2777 {
2778 	struct ex_vol_rename *p;
2779 	char fhbuf[NFS4_FHSIZE];
2780 	nfs_fh4 fh4;
2781 	int error;
2782 
2783 	fh4.nfs_fh4_val = fhbuf;
2784 	error = makefh4(&fh4, vp, exi);
2785 	if ((error) || (fh4.nfs_fh4_len != sizeof (p->vrn_fh_fmt))) {
2786 		return;
2787 	}
2788 
2789 	mutex_enter(&exi->exi_vol_rename_lock);
2790 
2791 	p = find_volrnm_fh(exi, &fh4);
2792 
2793 	if (p == NULL) {
2794 		p = kmem_alloc(sizeof (*p), KM_SLEEP);
2795 		bcopy(fh4.nfs_fh4_val, &p->vrn_fh_fmt, sizeof (p->vrn_fh_fmt));
2796 		p->vrn_next = exi->exi_vol_rename;
2797 		exi->exi_vol_rename = p;
2798 	}
2799 
2800 	p->vrn_fh_fmt.fh4_volatile_id = gethrestime_sec();
2801 	mutex_exit(&exi->exi_vol_rename_lock);
2802 }
2803 
2804 #endif /* VOLATILE_FH_TEST */
2805