xref: /freebsd/sys/kern/vfs_export.c (revision 10bcafe9abdb866e20e561bf567f15c0a06a0114)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34996c772fSJohn Dyson  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41057e2795SBruce Evans #include <sys/dirent.h>
42057e2795SBruce Evans #include <sys/domain.h>
43986f4ce7SBruce Evans #include <sys/kernel.h>
44057e2795SBruce Evans #include <sys/lock.h>
45a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
46bf61e266SKris Kennaway #include <sys/mbuf.h>
47df8bae1dSRodney W. Grimes #include <sys/mount.h>
48057e2795SBruce Evans #include <sys/mutex.h>
497e9e371fSJohn Baldwin #include <sys/refcount.h>
50057e2795SBruce Evans #include <sys/socket.h>
51057e2795SBruce Evans #include <sys/systm.h>
525e950839SLuoqi Chen #include <sys/vnode.h>
53df8bae1dSRodney W. Grimes 
54057e2795SBruce Evans #include <net/radix.h>
55057e2795SBruce Evans 
565bb84bc8SRobert Watson static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
5755166637SPoul-Henning Kamp 
584d77a549SAlfred Perlstein static void	vfs_free_addrlist(struct netexport *nep);
594d77a549SAlfred Perlstein static int	vfs_free_netcred(struct radix_node *rn, void *w);
604d77a549SAlfred Perlstein static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
614d77a549SAlfred Perlstein 		    struct export_args *argp);
62ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
6398d93822SBruce Evans 
64df8bae1dSRodney W. Grimes /*
65a13234bbSPoul-Henning Kamp  * Network address lookup element
66a13234bbSPoul-Henning Kamp  */
67a13234bbSPoul-Henning Kamp struct netcred {
68a13234bbSPoul-Henning Kamp 	struct	radix_node netc_rnodes[2];
69a13234bbSPoul-Henning Kamp 	int	netc_exflags;
70a13234bbSPoul-Henning Kamp 	struct	ucred netc_anon;
71a13234bbSPoul-Henning Kamp };
72a13234bbSPoul-Henning Kamp 
73a13234bbSPoul-Henning Kamp /*
74a13234bbSPoul-Henning Kamp  * Network export information
75a13234bbSPoul-Henning Kamp  */
76a13234bbSPoul-Henning Kamp struct netexport {
77a13234bbSPoul-Henning Kamp 	struct	netcred ne_defexported;		      /* Default export */
78a13234bbSPoul-Henning Kamp 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
79a13234bbSPoul-Henning Kamp };
80a13234bbSPoul-Henning Kamp 
81a13234bbSPoul-Henning Kamp /*
82df8bae1dSRodney W. Grimes  * Build hash lists of net addresses and hang them off the mount point.
83df8bae1dSRodney W. Grimes  * Called by ufs_mount() to set up the lists of export addresses.
84df8bae1dSRodney W. Grimes  */
85df8bae1dSRodney W. Grimes static int
862830e09dSCraig Rodrigues vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
872830e09dSCraig Rodrigues     struct export_args *argp)
88df8bae1dSRodney W. Grimes {
89df8bae1dSRodney W. Grimes 	register struct netcred *np;
90df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
91df8bae1dSRodney W. Grimes 	register int i;
92df8bae1dSRodney W. Grimes 	struct radix_node *rn;
93df8bae1dSRodney W. Grimes 	struct sockaddr *saddr, *smask = 0;
94df8bae1dSRodney W. Grimes 	struct domain *dom;
95df8bae1dSRodney W. Grimes 	int error;
96df8bae1dSRodney W. Grimes 
97e74d4831SDima Dorfman 	/*
98e74d4831SDima Dorfman 	 * XXX: This routine converts from a `struct xucred'
99e74d4831SDima Dorfman 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
100e74d4831SDima Dorfman 	 * operation is questionable; for example, what should be done
101e74d4831SDima Dorfman 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
102e74d4831SDima Dorfman 	 * routine does not touch them (leaves them as NULL).
103e74d4831SDima Dorfman 	 */
10461e323a2SCraig Rodrigues 	if (argp->ex_anon.cr_version != XUCRED_VERSION) {
10561e323a2SCraig Rodrigues 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
10661e323a2SCraig Rodrigues 		    argp->ex_anon.cr_version, XUCRED_VERSION);
107e74d4831SDima Dorfman 		return (EINVAL);
10861e323a2SCraig Rodrigues 	}
109e74d4831SDima Dorfman 
110df8bae1dSRodney W. Grimes 	if (argp->ex_addrlen == 0) {
11161e323a2SCraig Rodrigues 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
11261e323a2SCraig Rodrigues 			vfs_mount_error(mp,
11361e323a2SCraig Rodrigues 			    "MNT_DEFEXPORTED already set for mount %p", mp);
114df8bae1dSRodney W. Grimes 			return (EPERM);
11561e323a2SCraig Rodrigues 		}
116df8bae1dSRodney W. Grimes 		np = &nep->ne_defexported;
117df8bae1dSRodney W. Grimes 		np->netc_exflags = argp->ex_flags;
118c0511d3bSBrian Feldman 		bzero(&np->netc_anon, sizeof(np->netc_anon));
119c0511d3bSBrian Feldman 		np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
120c0511d3bSBrian Feldman 		np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
121c0511d3bSBrian Feldman 		bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
122c0511d3bSBrian Feldman 		    sizeof(np->netc_anon.cr_groups));
1237e9e371fSJohn Baldwin 		refcount_init(&np->netc_anon.cr_ref, 1);
1245da56ddbSTor Egge 		MNT_ILOCK(mp);
125df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_DEFEXPORTED;
1265da56ddbSTor Egge 		MNT_IUNLOCK(mp);
127df8bae1dSRodney W. Grimes 		return (0);
128df8bae1dSRodney W. Grimes 	}
129bf61e266SKris Kennaway 
130c43cad1aSScott Long #if MSIZE <= 256
1313a13c9ccSCraig Rodrigues 	if (argp->ex_addrlen > MLEN) {
1323a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
1333a13c9ccSCraig Rodrigues 		    argp->ex_addrlen, MLEN);
134bf61e266SKris Kennaway 		return (EINVAL);
1353a13c9ccSCraig Rodrigues 	}
136c43cad1aSScott Long #endif
137bf61e266SKris Kennaway 
138df8bae1dSRodney W. Grimes 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
139a163d034SWarner Losh 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
140df8bae1dSRodney W. Grimes 	saddr = (struct sockaddr *) (np + 1);
141210a5a71SAlfred Perlstein 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
142df8bae1dSRodney W. Grimes 		goto out;
14361e323a2SCraig Rodrigues 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
144b96e102aSColin Percival 		error = EINVAL;
14561e323a2SCraig Rodrigues 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
146b96e102aSColin Percival 		goto out;
147b96e102aSColin Percival 	}
148df8bae1dSRodney W. Grimes 	if (saddr->sa_len > argp->ex_addrlen)
149df8bae1dSRodney W. Grimes 		saddr->sa_len = argp->ex_addrlen;
150df8bae1dSRodney W. Grimes 	if (argp->ex_masklen) {
151c5e3ef7eSAlfred Perlstein 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
152210a5a71SAlfred Perlstein 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
153df8bae1dSRodney W. Grimes 		if (error)
154df8bae1dSRodney W. Grimes 			goto out;
155df8bae1dSRodney W. Grimes 		if (smask->sa_len > argp->ex_masklen)
156df8bae1dSRodney W. Grimes 			smask->sa_len = argp->ex_masklen;
157df8bae1dSRodney W. Grimes 	}
158df8bae1dSRodney W. Grimes 	i = saddr->sa_family;
159956b0b65SJeffrey Hsu 	if ((rnh = nep->ne_rtable[i]) == NULL) {
160df8bae1dSRodney W. Grimes 		/*
1610d94caffSDavid Greenman 		 * Seems silly to initialize every AF when most are not used,
1620d94caffSDavid Greenman 		 * do so on demand here
163df8bae1dSRodney W. Grimes 		 */
164df8bae1dSRodney W. Grimes 		for (dom = domains; dom; dom = dom->dom_next)
165df8bae1dSRodney W. Grimes 			if (dom->dom_family == i && dom->dom_rtattach) {
166df8bae1dSRodney W. Grimes 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
167df8bae1dSRodney W. Grimes 				    dom->dom_rtoffset);
168df8bae1dSRodney W. Grimes 				break;
169df8bae1dSRodney W. Grimes 			}
170956b0b65SJeffrey Hsu 		if ((rnh = nep->ne_rtable[i]) == NULL) {
171df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17261e323a2SCraig Rodrigues 			vfs_mount_error(mp, "%s %s %d",
17361e323a2SCraig Rodrigues 			    "Unable to initialize radix node head ",
17461e323a2SCraig Rodrigues 			    "for address family", i);
175df8bae1dSRodney W. Grimes 			goto out;
176df8bae1dSRodney W. Grimes 		}
177df8bae1dSRodney W. Grimes 	}
178956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
17997bb78acSAlfred Perlstein 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
180956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
181956b0b65SJeffrey Hsu 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
18237a6b453SAlfred Perlstein 		error = EPERM;
1833a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
1843a13c9ccSCraig Rodrigues 		    rn, np);
18537a6b453SAlfred Perlstein 		goto out;
186df8bae1dSRodney W. Grimes 	}
187df8bae1dSRodney W. Grimes 	np->netc_exflags = argp->ex_flags;
188c0511d3bSBrian Feldman 	bzero(&np->netc_anon, sizeof(np->netc_anon));
189c0511d3bSBrian Feldman 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
190c0511d3bSBrian Feldman 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
191c0511d3bSBrian Feldman 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
192c0511d3bSBrian Feldman 	    sizeof(np->netc_anon.cr_groups));
1937e9e371fSJohn Baldwin 	refcount_init(&np->netc_anon.cr_ref, 1);
194df8bae1dSRodney W. Grimes 	return (0);
195df8bae1dSRodney W. Grimes out:
196df8bae1dSRodney W. Grimes 	free(np, M_NETADDR);
197df8bae1dSRodney W. Grimes 	return (error);
198df8bae1dSRodney W. Grimes }
199df8bae1dSRodney W. Grimes 
200a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */
201df8bae1dSRodney W. Grimes /* ARGSUSED */
202df8bae1dSRodney W. Grimes static int
2032830e09dSCraig Rodrigues vfs_free_netcred(struct radix_node *rn, void *w)
204df8bae1dSRodney W. Grimes {
205df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
206df8bae1dSRodney W. Grimes 
207df8bae1dSRodney W. Grimes 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
208210a5a71SAlfred Perlstein 	free(rn, M_NETADDR);
209df8bae1dSRodney W. Grimes 	return (0);
210df8bae1dSRodney W. Grimes }
211df8bae1dSRodney W. Grimes 
212df8bae1dSRodney W. Grimes /*
213df8bae1dSRodney W. Grimes  * Free the net address hash lists that are hanging off the mount points.
214df8bae1dSRodney W. Grimes  */
215df8bae1dSRodney W. Grimes static void
2162830e09dSCraig Rodrigues vfs_free_addrlist(struct netexport *nep)
217df8bae1dSRodney W. Grimes {
218df8bae1dSRodney W. Grimes 	register int i;
219df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes 	for (i = 0; i <= AF_MAX; i++)
222bb56ec4aSPoul-Henning Kamp 		if ((rnh = nep->ne_rtable[i])) {
223956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_LOCK(rnh);
224210a5a71SAlfred Perlstein 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
225956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_DESTROY(rnh);
226210a5a71SAlfred Perlstein 			free(rnh, M_RTABLE);
227956b0b65SJeffrey Hsu 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
228df8bae1dSRodney W. Grimes 		}
229df8bae1dSRodney W. Grimes }
230df8bae1dSRodney W. Grimes 
23121a90397SAlfred Perlstein /*
23221a90397SAlfred Perlstein  * High level function to manipulate export options on a mount point
23321a90397SAlfred Perlstein  * and the passed in netexport.
23421a90397SAlfred Perlstein  * Struct export_args *argp is the variable used to twiddle options,
23521a90397SAlfred Perlstein  * the structure is described in sys/mount.h
23621a90397SAlfred Perlstein  */
237df8bae1dSRodney W. Grimes int
2382830e09dSCraig Rodrigues vfs_export(struct mount *mp, struct export_args *argp)
239df8bae1dSRodney W. Grimes {
240a13234bbSPoul-Henning Kamp 	struct netexport *nep;
241df8bae1dSRodney W. Grimes 	int error;
242df8bae1dSRodney W. Grimes 
243a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
24461e323a2SCraig Rodrigues 	error = 0;
245df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_DELEXPORT) {
24603eff583SCraig Rodrigues 		if (nep == NULL) {
24761e323a2SCraig Rodrigues 			error = ENOENT;
24861e323a2SCraig Rodrigues 			goto out;
24903eff583SCraig Rodrigues 		}
250f6b4c285SDoug Rabson 		if (mp->mnt_flag & MNT_EXPUBLIC) {
251f6b4c285SDoug Rabson 			vfs_setpublicfs(NULL, NULL, NULL);
2525da56ddbSTor Egge 			MNT_ILOCK(mp);
253f6b4c285SDoug Rabson 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2545da56ddbSTor Egge 			MNT_IUNLOCK(mp);
255f6b4c285SDoug Rabson 		}
256df8bae1dSRodney W. Grimes 		vfs_free_addrlist(nep);
257a13234bbSPoul-Henning Kamp 		mp->mnt_export = NULL;
258a13234bbSPoul-Henning Kamp 		free(nep, M_MOUNT);
2590ca9ed86SAlexander Kabaev 		nep = NULL;
2605da56ddbSTor Egge 		MNT_ILOCK(mp);
261df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2625da56ddbSTor Egge 		MNT_IUNLOCK(mp);
263df8bae1dSRodney W. Grimes 	}
264df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_EXPORTED) {
265a13234bbSPoul-Henning Kamp 		if (nep == NULL) {
266a163d034SWarner Losh 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
267a13234bbSPoul-Henning Kamp 			mp->mnt_export = nep;
268a13234bbSPoul-Henning Kamp 		}
269f6b4c285SDoug Rabson 		if (argp->ex_flags & MNT_EXPUBLIC) {
270f6b4c285SDoug Rabson 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
27161e323a2SCraig Rodrigues 				goto out;
2725da56ddbSTor Egge 			MNT_ILOCK(mp);
273f6b4c285SDoug Rabson 			mp->mnt_flag |= MNT_EXPUBLIC;
2745da56ddbSTor Egge 			MNT_IUNLOCK(mp);
275f6b4c285SDoug Rabson 		}
276bb56ec4aSPoul-Henning Kamp 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
27761e323a2SCraig Rodrigues 			goto out;
2785da56ddbSTor Egge 		MNT_ILOCK(mp);
279df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_EXPORTED;
2805da56ddbSTor Egge 		MNT_IUNLOCK(mp);
281df8bae1dSRodney W. Grimes 	}
28261e323a2SCraig Rodrigues 
28361e323a2SCraig Rodrigues out:
28461e323a2SCraig Rodrigues 	/*
28561e323a2SCraig Rodrigues 	 * Once we have executed the vfs_export() command, we do
28661e323a2SCraig Rodrigues 	 * not want to keep the "export" option around in the
28761e323a2SCraig Rodrigues 	 * options list, since that will cause subsequent MNT_UPDATE
28861e323a2SCraig Rodrigues 	 * calls to fail.  The export information is saved in
28961e323a2SCraig Rodrigues 	 * mp->mnt_export, so we can safely delete the "export" mount option
29061e323a2SCraig Rodrigues 	 * here.
29161e323a2SCraig Rodrigues 	 */
29261e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_optnew, "export");
29361e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_opt, "export");
29461e323a2SCraig Rodrigues 	return (error);
295df8bae1dSRodney W. Grimes }
296df8bae1dSRodney W. Grimes 
297f6b4c285SDoug Rabson /*
298f6b4c285SDoug Rabson  * Set the publicly exported filesystem (WebNFS). Currently, only
299f6b4c285SDoug Rabson  * one public filesystem is possible in the spec (RFC 2054 and 2055)
300f6b4c285SDoug Rabson  */
301f6b4c285SDoug Rabson int
3022830e09dSCraig Rodrigues vfs_setpublicfs(struct mount *mp, struct netexport *nep,
3032830e09dSCraig Rodrigues     struct export_args *argp)
304f6b4c285SDoug Rabson {
305f6b4c285SDoug Rabson 	int error;
306f6b4c285SDoug Rabson 	struct vnode *rvp;
307f6b4c285SDoug Rabson 	char *cp;
308f6b4c285SDoug Rabson 
309f6b4c285SDoug Rabson 	/*
310f6b4c285SDoug Rabson 	 * mp == NULL -> invalidate the current info, the FS is
311f6b4c285SDoug Rabson 	 * no longer exported. May be called from either vfs_export
312f6b4c285SDoug Rabson 	 * or unmount, so check if it hasn't already been done.
313f6b4c285SDoug Rabson 	 */
314f6b4c285SDoug Rabson 	if (mp == NULL) {
315f6b4c285SDoug Rabson 		if (nfs_pub.np_valid) {
316f6b4c285SDoug Rabson 			nfs_pub.np_valid = 0;
317f6b4c285SDoug Rabson 			if (nfs_pub.np_index != NULL) {
318f6b4c285SDoug Rabson 				FREE(nfs_pub.np_index, M_TEMP);
319f6b4c285SDoug Rabson 				nfs_pub.np_index = NULL;
320f6b4c285SDoug Rabson 			}
321f6b4c285SDoug Rabson 		}
322f6b4c285SDoug Rabson 		return (0);
323f6b4c285SDoug Rabson 	}
324f6b4c285SDoug Rabson 
325f6b4c285SDoug Rabson 	/*
326f6b4c285SDoug Rabson 	 * Only one allowed at a time.
327f6b4c285SDoug Rabson 	 */
328f6b4c285SDoug Rabson 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
329f6b4c285SDoug Rabson 		return (EBUSY);
330f6b4c285SDoug Rabson 
331f6b4c285SDoug Rabson 	/*
332f6b4c285SDoug Rabson 	 * Get real filehandle for root of exported FS.
333f6b4c285SDoug Rabson 	 */
334210a5a71SAlfred Perlstein 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
335f6b4c285SDoug Rabson 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
336f6b4c285SDoug Rabson 
337d830f828SJeff Roberson 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */)))
338f6b4c285SDoug Rabson 		return (error);
339f6b4c285SDoug Rabson 
34010bcafe9SPawel Jakub Dawidek 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
341f6b4c285SDoug Rabson 		return (error);
342f6b4c285SDoug Rabson 
343f6b4c285SDoug Rabson 	vput(rvp);
344f6b4c285SDoug Rabson 
345f6b4c285SDoug Rabson 	/*
346f6b4c285SDoug Rabson 	 * If an indexfile was specified, pull it in.
347f6b4c285SDoug Rabson 	 */
348f6b4c285SDoug Rabson 	if (argp->ex_indexfile != NULL) {
349f6b4c285SDoug Rabson 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
350a163d034SWarner Losh 		    M_WAITOK);
351f6b4c285SDoug Rabson 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
352f6b4c285SDoug Rabson 		    MAXNAMLEN, (size_t *)0);
353f6b4c285SDoug Rabson 		if (!error) {
354f6b4c285SDoug Rabson 			/*
355f6b4c285SDoug Rabson 			 * Check for illegal filenames.
356f6b4c285SDoug Rabson 			 */
357f6b4c285SDoug Rabson 			for (cp = nfs_pub.np_index; *cp; cp++) {
358f6b4c285SDoug Rabson 				if (*cp == '/') {
359f6b4c285SDoug Rabson 					error = EINVAL;
360f6b4c285SDoug Rabson 					break;
361f6b4c285SDoug Rabson 				}
362f6b4c285SDoug Rabson 			}
363f6b4c285SDoug Rabson 		}
364f6b4c285SDoug Rabson 		if (error) {
365f6b4c285SDoug Rabson 			FREE(nfs_pub.np_index, M_TEMP);
366f6b4c285SDoug Rabson 			return (error);
367f6b4c285SDoug Rabson 		}
368f6b4c285SDoug Rabson 	}
369f6b4c285SDoug Rabson 
370f6b4c285SDoug Rabson 	nfs_pub.np_mount = mp;
371f6b4c285SDoug Rabson 	nfs_pub.np_valid = 1;
372f6b4c285SDoug Rabson 	return (0);
373f6b4c285SDoug Rabson }
374f6b4c285SDoug Rabson 
37521a90397SAlfred Perlstein /*
37621a90397SAlfred Perlstein  * Used by the filesystems to determine if a given network address
37721a90397SAlfred Perlstein  * (passed in 'nam') is present in thier exports list, returns a pointer
37821a90397SAlfred Perlstein  * to struct netcred so that the filesystem can examine it for
37921a90397SAlfred Perlstein  * access rights (read/write/etc).
38021a90397SAlfred Perlstein  */
381ebbfc2f8SPoul-Henning Kamp static struct netcred *
382ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
383df8bae1dSRodney W. Grimes {
384a13234bbSPoul-Henning Kamp 	struct netexport *nep;
385df8bae1dSRodney W. Grimes 	register struct netcred *np;
386df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
387df8bae1dSRodney W. Grimes 	struct sockaddr *saddr;
388df8bae1dSRodney W. Grimes 
389a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
390a13234bbSPoul-Henning Kamp 	if (nep == NULL)
391a13234bbSPoul-Henning Kamp 		return (NULL);
392df8bae1dSRodney W. Grimes 	np = NULL;
393df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_EXPORTED) {
394df8bae1dSRodney W. Grimes 		/*
395df8bae1dSRodney W. Grimes 		 * Lookup in the export list first.
396df8bae1dSRodney W. Grimes 		 */
397df8bae1dSRodney W. Grimes 		if (nam != NULL) {
39857bf258eSGarrett Wollman 			saddr = nam;
399df8bae1dSRodney W. Grimes 			rnh = nep->ne_rtable[saddr->sa_family];
400df8bae1dSRodney W. Grimes 			if (rnh != NULL) {
401956b0b65SJeffrey Hsu 				RADIX_NODE_HEAD_LOCK(rnh);
402df8bae1dSRodney W. Grimes 				np = (struct netcred *)
40397bb78acSAlfred Perlstein 				    (*rnh->rnh_matchaddr)(saddr, rnh);
404956b0b65SJeffrey Hsu 				RADIX_NODE_HEAD_UNLOCK(rnh);
405df8bae1dSRodney W. Grimes 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
406df8bae1dSRodney W. Grimes 					np = NULL;
407df8bae1dSRodney W. Grimes 			}
408df8bae1dSRodney W. Grimes 		}
409df8bae1dSRodney W. Grimes 		/*
410df8bae1dSRodney W. Grimes 		 * If no address match, use the default if it exists.
411df8bae1dSRodney W. Grimes 		 */
412df8bae1dSRodney W. Grimes 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
413df8bae1dSRodney W. Grimes 			np = &nep->ne_defexported;
414df8bae1dSRodney W. Grimes 	}
415df8bae1dSRodney W. Grimes 	return (np);
416df8bae1dSRodney W. Grimes }
41761f5d510SDavid Greenman 
41861f5d510SDavid Greenman /*
419a13234bbSPoul-Henning Kamp  * XXX: This comment comes from the deprecated ufs_check_export()
420a13234bbSPoul-Henning Kamp  * XXX: and may not entirely apply, but lacking something better:
421a13234bbSPoul-Henning Kamp  * This is the generic part of fhtovp called after the underlying
422a13234bbSPoul-Henning Kamp  * filesystem has validated the file handle.
423a13234bbSPoul-Henning Kamp  *
424a13234bbSPoul-Henning Kamp  * Verify that a host should have access to a filesystem.
425a13234bbSPoul-Henning Kamp  */
426a13234bbSPoul-Henning Kamp 
427a13234bbSPoul-Henning Kamp int
4282830e09dSCraig Rodrigues vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
4292830e09dSCraig Rodrigues     struct ucred **credanonp)
430a13234bbSPoul-Henning Kamp {
431a13234bbSPoul-Henning Kamp 	struct netcred *np;
432a13234bbSPoul-Henning Kamp 
433a13234bbSPoul-Henning Kamp 	np = vfs_export_lookup(mp, nam);
434a13234bbSPoul-Henning Kamp 	if (np == NULL)
435a13234bbSPoul-Henning Kamp 		return (EACCES);
436a13234bbSPoul-Henning Kamp 	*extflagsp = np->netc_exflags;
437a13234bbSPoul-Henning Kamp 	*credanonp = &np->netc_anon;
438a13234bbSPoul-Henning Kamp 	return (0);
439a13234bbSPoul-Henning Kamp }
440a13234bbSPoul-Henning Kamp 
441