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