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