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