xref: /freebsd/sys/kern/vfs_export.c (revision 3120b9d4285d2c477a8d7a188597fc7613a66d47)
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>
493120b9d4SKip Macy #include <sys/rwlock.h>
507e9e371fSJohn Baldwin #include <sys/refcount.h>
51057e2795SBruce Evans #include <sys/socket.h>
52057e2795SBruce Evans #include <sys/systm.h>
535e950839SLuoqi Chen #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes 
55057e2795SBruce Evans #include <net/radix.h>
56057e2795SBruce Evans 
575bb84bc8SRobert Watson static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
5855166637SPoul-Henning Kamp 
594d77a549SAlfred Perlstein static void	vfs_free_addrlist(struct netexport *nep);
604d77a549SAlfred Perlstein static int	vfs_free_netcred(struct radix_node *rn, void *w);
614d77a549SAlfred Perlstein static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
624d77a549SAlfred Perlstein 		    struct export_args *argp);
63ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
6498d93822SBruce Evans 
65df8bae1dSRodney W. Grimes /*
66a13234bbSPoul-Henning Kamp  * Network address lookup element
67a13234bbSPoul-Henning Kamp  */
68a13234bbSPoul-Henning Kamp struct netcred {
69a13234bbSPoul-Henning Kamp 	struct	radix_node netc_rnodes[2];
70a13234bbSPoul-Henning Kamp 	int	netc_exflags;
71a13234bbSPoul-Henning Kamp 	struct	ucred netc_anon;
72a9148abdSDoug Rabson 	int	netc_numsecflavors;
73a9148abdSDoug Rabson 	int	netc_secflavors[MAXSECFLAVORS];
74a13234bbSPoul-Henning Kamp };
75a13234bbSPoul-Henning Kamp 
76a13234bbSPoul-Henning Kamp /*
77a13234bbSPoul-Henning Kamp  * Network export information
78a13234bbSPoul-Henning Kamp  */
79a13234bbSPoul-Henning Kamp struct netexport {
80a13234bbSPoul-Henning Kamp 	struct	netcred ne_defexported;		      /* Default export */
81a13234bbSPoul-Henning Kamp 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
82a13234bbSPoul-Henning Kamp };
83a13234bbSPoul-Henning Kamp 
84a13234bbSPoul-Henning Kamp /*
85df8bae1dSRodney W. Grimes  * Build hash lists of net addresses and hang them off the mount point.
86df8bae1dSRodney W. Grimes  * Called by ufs_mount() to set up the lists of export addresses.
87df8bae1dSRodney W. Grimes  */
88df8bae1dSRodney W. Grimes static int
892830e09dSCraig Rodrigues vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
902830e09dSCraig Rodrigues     struct export_args *argp)
91df8bae1dSRodney W. Grimes {
92df8bae1dSRodney W. Grimes 	register struct netcred *np;
93df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
94df8bae1dSRodney W. Grimes 	register int i;
95df8bae1dSRodney W. Grimes 	struct radix_node *rn;
96df8bae1dSRodney W. Grimes 	struct sockaddr *saddr, *smask = 0;
97df8bae1dSRodney W. Grimes 	struct domain *dom;
98df8bae1dSRodney W. Grimes 	int error;
99df8bae1dSRodney W. Grimes 
100e74d4831SDima Dorfman 	/*
101e74d4831SDima Dorfman 	 * XXX: This routine converts from a `struct xucred'
102e74d4831SDima Dorfman 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
103e74d4831SDima Dorfman 	 * operation is questionable; for example, what should be done
104e74d4831SDima Dorfman 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
105e74d4831SDima Dorfman 	 * routine does not touch them (leaves them as NULL).
106e74d4831SDima Dorfman 	 */
10761e323a2SCraig Rodrigues 	if (argp->ex_anon.cr_version != XUCRED_VERSION) {
10861e323a2SCraig Rodrigues 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
10961e323a2SCraig Rodrigues 		    argp->ex_anon.cr_version, XUCRED_VERSION);
110e74d4831SDima Dorfman 		return (EINVAL);
11161e323a2SCraig Rodrigues 	}
112e74d4831SDima Dorfman 
113df8bae1dSRodney W. Grimes 	if (argp->ex_addrlen == 0) {
11461e323a2SCraig Rodrigues 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
11561e323a2SCraig Rodrigues 			vfs_mount_error(mp,
11661e323a2SCraig Rodrigues 			    "MNT_DEFEXPORTED already set for mount %p", mp);
117df8bae1dSRodney W. Grimes 			return (EPERM);
11861e323a2SCraig Rodrigues 		}
119df8bae1dSRodney W. Grimes 		np = &nep->ne_defexported;
120df8bae1dSRodney W. Grimes 		np->netc_exflags = argp->ex_flags;
121c0511d3bSBrian Feldman 		bzero(&np->netc_anon, sizeof(np->netc_anon));
122c0511d3bSBrian Feldman 		np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
123c0511d3bSBrian Feldman 		np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
124c0511d3bSBrian Feldman 		bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
125c0511d3bSBrian Feldman 		    sizeof(np->netc_anon.cr_groups));
126a9148abdSDoug Rabson 		np->netc_numsecflavors = argp->ex_numsecflavors;
127a9148abdSDoug Rabson 		bcopy(argp->ex_secflavors, np->netc_secflavors,
128a9148abdSDoug Rabson 		    sizeof(np->netc_secflavors));
1297e9e371fSJohn Baldwin 		refcount_init(&np->netc_anon.cr_ref, 1);
1305da56ddbSTor Egge 		MNT_ILOCK(mp);
131df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_DEFEXPORTED;
1325da56ddbSTor Egge 		MNT_IUNLOCK(mp);
133df8bae1dSRodney W. Grimes 		return (0);
134df8bae1dSRodney W. Grimes 	}
135bf61e266SKris Kennaway 
136c43cad1aSScott Long #if MSIZE <= 256
1373a13c9ccSCraig Rodrigues 	if (argp->ex_addrlen > MLEN) {
1383a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
1393a13c9ccSCraig Rodrigues 		    argp->ex_addrlen, MLEN);
140bf61e266SKris Kennaway 		return (EINVAL);
1413a13c9ccSCraig Rodrigues 	}
142c43cad1aSScott Long #endif
143bf61e266SKris Kennaway 
144df8bae1dSRodney W. Grimes 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
145a163d034SWarner Losh 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
146df8bae1dSRodney W. Grimes 	saddr = (struct sockaddr *) (np + 1);
147210a5a71SAlfred Perlstein 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
148df8bae1dSRodney W. Grimes 		goto out;
14961e323a2SCraig Rodrigues 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
150b96e102aSColin Percival 		error = EINVAL;
15161e323a2SCraig Rodrigues 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
152b96e102aSColin Percival 		goto out;
153b96e102aSColin Percival 	}
154df8bae1dSRodney W. Grimes 	if (saddr->sa_len > argp->ex_addrlen)
155df8bae1dSRodney W. Grimes 		saddr->sa_len = argp->ex_addrlen;
156df8bae1dSRodney W. Grimes 	if (argp->ex_masklen) {
157c5e3ef7eSAlfred Perlstein 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
158210a5a71SAlfred Perlstein 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
159df8bae1dSRodney W. Grimes 		if (error)
160df8bae1dSRodney W. Grimes 			goto out;
161df8bae1dSRodney W. Grimes 		if (smask->sa_len > argp->ex_masklen)
162df8bae1dSRodney W. Grimes 			smask->sa_len = argp->ex_masklen;
163df8bae1dSRodney W. Grimes 	}
164df8bae1dSRodney W. Grimes 	i = saddr->sa_family;
165956b0b65SJeffrey Hsu 	if ((rnh = nep->ne_rtable[i]) == NULL) {
166df8bae1dSRodney W. Grimes 		/*
1670d94caffSDavid Greenman 		 * Seems silly to initialize every AF when most are not used,
1680d94caffSDavid Greenman 		 * do so on demand here
169df8bae1dSRodney W. Grimes 		 */
1708b07e49aSJulian Elischer 		for (dom = domains; dom; dom = dom->dom_next) {
1718b07e49aSJulian Elischer 			KASSERT(((i == AF_INET) || (i == AF_INET6)),
1728b07e49aSJulian Elischer 			    ("unexpected protocol in vfs_hang_addrlist"));
173df8bae1dSRodney W. Grimes 			if (dom->dom_family == i && dom->dom_rtattach) {
1748b07e49aSJulian Elischer 				/*
1758b07e49aSJulian Elischer 				 * XXX MRT
1768b07e49aSJulian Elischer 				 * The INET and INET6 domains know the
1778b07e49aSJulian Elischer 				 * offset already. We don't need to send it
1788b07e49aSJulian Elischer 				 * So we just use it as a flag to say that
1798b07e49aSJulian Elischer 				 * we are or are not setting up a real routing
1808b07e49aSJulian Elischer 				 * table. Only IP and IPV6 need have this
1818b07e49aSJulian Elischer 				 * be 0 so all other protocols can stay the
1828b07e49aSJulian Elischer 				 * same (ABI compatible).
1838b07e49aSJulian Elischer 				 */
1848b07e49aSJulian Elischer 				dom->dom_rtattach(
1858b07e49aSJulian Elischer 				    (void **) &nep->ne_rtable[i], 0);
186df8bae1dSRodney W. Grimes 				break;
187df8bae1dSRodney W. Grimes 			}
1888b07e49aSJulian Elischer 		}
189956b0b65SJeffrey Hsu 		if ((rnh = nep->ne_rtable[i]) == NULL) {
190df8bae1dSRodney W. Grimes 			error = ENOBUFS;
19161e323a2SCraig Rodrigues 			vfs_mount_error(mp, "%s %s %d",
19261e323a2SCraig Rodrigues 			    "Unable to initialize radix node head ",
19361e323a2SCraig Rodrigues 			    "for address family", i);
194df8bae1dSRodney W. Grimes 			goto out;
195df8bae1dSRodney W. Grimes 		}
196df8bae1dSRodney W. Grimes 	}
197956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
19897bb78acSAlfred Perlstein 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
199956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
200956b0b65SJeffrey Hsu 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
20137a6b453SAlfred Perlstein 		error = EPERM;
2023a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
2033a13c9ccSCraig Rodrigues 		    rn, np);
20437a6b453SAlfred Perlstein 		goto out;
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	np->netc_exflags = argp->ex_flags;
207c0511d3bSBrian Feldman 	bzero(&np->netc_anon, sizeof(np->netc_anon));
208c0511d3bSBrian Feldman 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
209c0511d3bSBrian Feldman 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
210c0511d3bSBrian Feldman 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
211c0511d3bSBrian Feldman 	    sizeof(np->netc_anon.cr_groups));
212a9148abdSDoug Rabson 	np->netc_numsecflavors = argp->ex_numsecflavors;
213a9148abdSDoug Rabson 	bcopy(argp->ex_secflavors, np->netc_secflavors,
214a9148abdSDoug Rabson 	    sizeof(np->netc_secflavors));
2157e9e371fSJohn Baldwin 	refcount_init(&np->netc_anon.cr_ref, 1);
216df8bae1dSRodney W. Grimes 	return (0);
217df8bae1dSRodney W. Grimes out:
218df8bae1dSRodney W. Grimes 	free(np, M_NETADDR);
219df8bae1dSRodney W. Grimes 	return (error);
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
222a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */
223df8bae1dSRodney W. Grimes /* ARGSUSED */
224df8bae1dSRodney W. Grimes static int
2252830e09dSCraig Rodrigues vfs_free_netcred(struct radix_node *rn, void *w)
226df8bae1dSRodney W. Grimes {
227df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
230210a5a71SAlfred Perlstein 	free(rn, M_NETADDR);
231df8bae1dSRodney W. Grimes 	return (0);
232df8bae1dSRodney W. Grimes }
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes /*
235df8bae1dSRodney W. Grimes  * Free the net address hash lists that are hanging off the mount points.
236df8bae1dSRodney W. Grimes  */
237df8bae1dSRodney W. Grimes static void
2382830e09dSCraig Rodrigues vfs_free_addrlist(struct netexport *nep)
239df8bae1dSRodney W. Grimes {
240df8bae1dSRodney W. Grimes 	register int i;
241df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	for (i = 0; i <= AF_MAX; i++)
244bb56ec4aSPoul-Henning Kamp 		if ((rnh = nep->ne_rtable[i])) {
245956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_LOCK(rnh);
246210a5a71SAlfred Perlstein 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
247956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_DESTROY(rnh);
248210a5a71SAlfred Perlstein 			free(rnh, M_RTABLE);
249956b0b65SJeffrey Hsu 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
250df8bae1dSRodney W. Grimes 		}
251df8bae1dSRodney W. Grimes }
252df8bae1dSRodney W. Grimes 
25321a90397SAlfred Perlstein /*
25421a90397SAlfred Perlstein  * High level function to manipulate export options on a mount point
25521a90397SAlfred Perlstein  * and the passed in netexport.
25621a90397SAlfred Perlstein  * Struct export_args *argp is the variable used to twiddle options,
25721a90397SAlfred Perlstein  * the structure is described in sys/mount.h
25821a90397SAlfred Perlstein  */
259df8bae1dSRodney W. Grimes int
2602830e09dSCraig Rodrigues vfs_export(struct mount *mp, struct export_args *argp)
261df8bae1dSRodney W. Grimes {
262a13234bbSPoul-Henning Kamp 	struct netexport *nep;
263df8bae1dSRodney W. Grimes 	int error;
264df8bae1dSRodney W. Grimes 
265a9148abdSDoug Rabson 	if (argp->ex_numsecflavors < 0
266a9148abdSDoug Rabson 	    || argp->ex_numsecflavors >= MAXSECFLAVORS)
267a9148abdSDoug Rabson 		return (EINVAL);
268a9148abdSDoug Rabson 
269a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
27061e323a2SCraig Rodrigues 	error = 0;
271a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
272df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_DELEXPORT) {
27303eff583SCraig Rodrigues 		if (nep == NULL) {
27461e323a2SCraig Rodrigues 			error = ENOENT;
27561e323a2SCraig Rodrigues 			goto out;
27603eff583SCraig Rodrigues 		}
277f6b4c285SDoug Rabson 		if (mp->mnt_flag & MNT_EXPUBLIC) {
278f6b4c285SDoug Rabson 			vfs_setpublicfs(NULL, NULL, NULL);
2795da56ddbSTor Egge 			MNT_ILOCK(mp);
280f6b4c285SDoug Rabson 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2815da56ddbSTor Egge 			MNT_IUNLOCK(mp);
282f6b4c285SDoug Rabson 		}
283df8bae1dSRodney W. Grimes 		vfs_free_addrlist(nep);
284a13234bbSPoul-Henning Kamp 		mp->mnt_export = NULL;
285a13234bbSPoul-Henning Kamp 		free(nep, M_MOUNT);
2860ca9ed86SAlexander Kabaev 		nep = NULL;
2875da56ddbSTor Egge 		MNT_ILOCK(mp);
288df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2895da56ddbSTor Egge 		MNT_IUNLOCK(mp);
290df8bae1dSRodney W. Grimes 	}
291df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_EXPORTED) {
292a13234bbSPoul-Henning Kamp 		if (nep == NULL) {
293a163d034SWarner Losh 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
294a13234bbSPoul-Henning Kamp 			mp->mnt_export = nep;
295a13234bbSPoul-Henning Kamp 		}
296f6b4c285SDoug Rabson 		if (argp->ex_flags & MNT_EXPUBLIC) {
297f6b4c285SDoug Rabson 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
29861e323a2SCraig Rodrigues 				goto out;
2995da56ddbSTor Egge 			MNT_ILOCK(mp);
300f6b4c285SDoug Rabson 			mp->mnt_flag |= MNT_EXPUBLIC;
3015da56ddbSTor Egge 			MNT_IUNLOCK(mp);
302f6b4c285SDoug Rabson 		}
303bb56ec4aSPoul-Henning Kamp 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
30461e323a2SCraig Rodrigues 			goto out;
3055da56ddbSTor Egge 		MNT_ILOCK(mp);
306df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_EXPORTED;
3075da56ddbSTor Egge 		MNT_IUNLOCK(mp);
308df8bae1dSRodney W. Grimes 	}
30961e323a2SCraig Rodrigues 
31061e323a2SCraig Rodrigues out:
311a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
31261e323a2SCraig Rodrigues 	/*
31361e323a2SCraig Rodrigues 	 * Once we have executed the vfs_export() command, we do
31461e323a2SCraig Rodrigues 	 * not want to keep the "export" option around in the
31561e323a2SCraig Rodrigues 	 * options list, since that will cause subsequent MNT_UPDATE
31661e323a2SCraig Rodrigues 	 * calls to fail.  The export information is saved in
31761e323a2SCraig Rodrigues 	 * mp->mnt_export, so we can safely delete the "export" mount option
31861e323a2SCraig Rodrigues 	 * here.
31961e323a2SCraig Rodrigues 	 */
32061e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_optnew, "export");
32161e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_opt, "export");
32261e323a2SCraig Rodrigues 	return (error);
323df8bae1dSRodney W. Grimes }
324df8bae1dSRodney W. Grimes 
325f6b4c285SDoug Rabson /*
326f6b4c285SDoug Rabson  * Set the publicly exported filesystem (WebNFS). Currently, only
327f6b4c285SDoug Rabson  * one public filesystem is possible in the spec (RFC 2054 and 2055)
328f6b4c285SDoug Rabson  */
329f6b4c285SDoug Rabson int
3302830e09dSCraig Rodrigues vfs_setpublicfs(struct mount *mp, struct netexport *nep,
3312830e09dSCraig Rodrigues     struct export_args *argp)
332f6b4c285SDoug Rabson {
333f6b4c285SDoug Rabson 	int error;
334f6b4c285SDoug Rabson 	struct vnode *rvp;
335f6b4c285SDoug Rabson 	char *cp;
336f6b4c285SDoug Rabson 
337f6b4c285SDoug Rabson 	/*
338f6b4c285SDoug Rabson 	 * mp == NULL -> invalidate the current info, the FS is
339f6b4c285SDoug Rabson 	 * no longer exported. May be called from either vfs_export
340f6b4c285SDoug Rabson 	 * or unmount, so check if it hasn't already been done.
341f6b4c285SDoug Rabson 	 */
342f6b4c285SDoug Rabson 	if (mp == NULL) {
343f6b4c285SDoug Rabson 		if (nfs_pub.np_valid) {
344f6b4c285SDoug Rabson 			nfs_pub.np_valid = 0;
345f6b4c285SDoug Rabson 			if (nfs_pub.np_index != NULL) {
3461ede983cSDag-Erling Smørgrav 				free(nfs_pub.np_index, M_TEMP);
347f6b4c285SDoug Rabson 				nfs_pub.np_index = NULL;
348f6b4c285SDoug Rabson 			}
349f6b4c285SDoug Rabson 		}
350f6b4c285SDoug Rabson 		return (0);
351f6b4c285SDoug Rabson 	}
352f6b4c285SDoug Rabson 
353f6b4c285SDoug Rabson 	/*
354f6b4c285SDoug Rabson 	 * Only one allowed at a time.
355f6b4c285SDoug Rabson 	 */
356f6b4c285SDoug Rabson 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
357f6b4c285SDoug Rabson 		return (EBUSY);
358f6b4c285SDoug Rabson 
359f6b4c285SDoug Rabson 	/*
360f6b4c285SDoug Rabson 	 * Get real filehandle for root of exported FS.
361f6b4c285SDoug Rabson 	 */
362210a5a71SAlfred Perlstein 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
363f6b4c285SDoug Rabson 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
364f6b4c285SDoug Rabson 
365d830f828SJeff Roberson 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */)))
366f6b4c285SDoug Rabson 		return (error);
367f6b4c285SDoug Rabson 
36810bcafe9SPawel Jakub Dawidek 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
369f6b4c285SDoug Rabson 		return (error);
370f6b4c285SDoug Rabson 
371f6b4c285SDoug Rabson 	vput(rvp);
372f6b4c285SDoug Rabson 
373f6b4c285SDoug Rabson 	/*
374f6b4c285SDoug Rabson 	 * If an indexfile was specified, pull it in.
375f6b4c285SDoug Rabson 	 */
376f6b4c285SDoug Rabson 	if (argp->ex_indexfile != NULL) {
3771ede983cSDag-Erling Smørgrav 		nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
378a163d034SWarner Losh 		    M_WAITOK);
379f6b4c285SDoug Rabson 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
380f6b4c285SDoug Rabson 		    MAXNAMLEN, (size_t *)0);
381f6b4c285SDoug Rabson 		if (!error) {
382f6b4c285SDoug Rabson 			/*
383f6b4c285SDoug Rabson 			 * Check for illegal filenames.
384f6b4c285SDoug Rabson 			 */
385f6b4c285SDoug Rabson 			for (cp = nfs_pub.np_index; *cp; cp++) {
386f6b4c285SDoug Rabson 				if (*cp == '/') {
387f6b4c285SDoug Rabson 					error = EINVAL;
388f6b4c285SDoug Rabson 					break;
389f6b4c285SDoug Rabson 				}
390f6b4c285SDoug Rabson 			}
391f6b4c285SDoug Rabson 		}
392f6b4c285SDoug Rabson 		if (error) {
3931ede983cSDag-Erling Smørgrav 			free(nfs_pub.np_index, M_TEMP);
394f6b4c285SDoug Rabson 			return (error);
395f6b4c285SDoug Rabson 		}
396f6b4c285SDoug Rabson 	}
397f6b4c285SDoug Rabson 
398f6b4c285SDoug Rabson 	nfs_pub.np_mount = mp;
399f6b4c285SDoug Rabson 	nfs_pub.np_valid = 1;
400f6b4c285SDoug Rabson 	return (0);
401f6b4c285SDoug Rabson }
402f6b4c285SDoug Rabson 
40321a90397SAlfred Perlstein /*
40421a90397SAlfred Perlstein  * Used by the filesystems to determine if a given network address
4056ffb78d1SEd Maste  * (passed in 'nam') is present in their exports list, returns a pointer
40621a90397SAlfred Perlstein  * to struct netcred so that the filesystem can examine it for
40721a90397SAlfred Perlstein  * access rights (read/write/etc).
40821a90397SAlfred Perlstein  */
409ebbfc2f8SPoul-Henning Kamp static struct netcred *
410ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
411df8bae1dSRodney W. Grimes {
412a13234bbSPoul-Henning Kamp 	struct netexport *nep;
413df8bae1dSRodney W. Grimes 	register struct netcred *np;
414df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
415df8bae1dSRodney W. Grimes 	struct sockaddr *saddr;
416df8bae1dSRodney W. Grimes 
417a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
418a13234bbSPoul-Henning Kamp 	if (nep == NULL)
419a13234bbSPoul-Henning Kamp 		return (NULL);
420df8bae1dSRodney W. Grimes 	np = NULL;
421df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_EXPORTED) {
422df8bae1dSRodney W. Grimes 		/*
423df8bae1dSRodney W. Grimes 		 * Lookup in the export list first.
424df8bae1dSRodney W. Grimes 		 */
425df8bae1dSRodney W. Grimes 		if (nam != NULL) {
42657bf258eSGarrett Wollman 			saddr = nam;
427df8bae1dSRodney W. Grimes 			rnh = nep->ne_rtable[saddr->sa_family];
428df8bae1dSRodney W. Grimes 			if (rnh != NULL) {
4293120b9d4SKip Macy 				RADIX_NODE_HEAD_RLOCK(rnh);
430df8bae1dSRodney W. Grimes 				np = (struct netcred *)
43197bb78acSAlfred Perlstein 				    (*rnh->rnh_matchaddr)(saddr, rnh);
4323120b9d4SKip Macy 				RADIX_NODE_HEAD_RUNLOCK(rnh);
433df8bae1dSRodney W. Grimes 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
434df8bae1dSRodney W. Grimes 					np = NULL;
435df8bae1dSRodney W. Grimes 			}
436df8bae1dSRodney W. Grimes 		}
437df8bae1dSRodney W. Grimes 		/*
438df8bae1dSRodney W. Grimes 		 * If no address match, use the default if it exists.
439df8bae1dSRodney W. Grimes 		 */
440df8bae1dSRodney W. Grimes 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
441df8bae1dSRodney W. Grimes 			np = &nep->ne_defexported;
442df8bae1dSRodney W. Grimes 	}
443df8bae1dSRodney W. Grimes 	return (np);
444df8bae1dSRodney W. Grimes }
44561f5d510SDavid Greenman 
44661f5d510SDavid Greenman /*
447a13234bbSPoul-Henning Kamp  * XXX: This comment comes from the deprecated ufs_check_export()
448a13234bbSPoul-Henning Kamp  * XXX: and may not entirely apply, but lacking something better:
449a13234bbSPoul-Henning Kamp  * This is the generic part of fhtovp called after the underlying
450a13234bbSPoul-Henning Kamp  * filesystem has validated the file handle.
451a13234bbSPoul-Henning Kamp  *
452a13234bbSPoul-Henning Kamp  * Verify that a host should have access to a filesystem.
453a13234bbSPoul-Henning Kamp  */
454a13234bbSPoul-Henning Kamp 
455a13234bbSPoul-Henning Kamp int
4562830e09dSCraig Rodrigues vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
457a9148abdSDoug Rabson     struct ucred **credanonp, int *numsecflavors, int **secflavors)
458a13234bbSPoul-Henning Kamp {
459a13234bbSPoul-Henning Kamp 	struct netcred *np;
460a13234bbSPoul-Henning Kamp 
461a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
462a13234bbSPoul-Henning Kamp 	np = vfs_export_lookup(mp, nam);
463a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
464a13234bbSPoul-Henning Kamp 	if (np == NULL)
465a13234bbSPoul-Henning Kamp 		return (EACCES);
466a13234bbSPoul-Henning Kamp 	*extflagsp = np->netc_exflags;
467a13234bbSPoul-Henning Kamp 	*credanonp = &np->netc_anon;
468a9148abdSDoug Rabson 	if (numsecflavors)
469a9148abdSDoug Rabson 		*numsecflavors = np->netc_numsecflavors;
470a9148abdSDoug Rabson 	if (secflavors)
471a9148abdSDoug Rabson 		*secflavors = np->netc_secflavors;
472a13234bbSPoul-Henning Kamp 	return (0);
473a13234bbSPoul-Henning Kamp }
474a13234bbSPoul-Henning Kamp 
475