Home | History | Annotate | Line # | Download | only in nfs
nfs_export.c revision 1.1
      1 /*	$NetBSD: nfs_export.c,v 1.1 2005/09/23 12:10:33 jmmv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2004, 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Charles M. Hannum.
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by Julio M. Merino Vidal.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *	This product includes software developed by the NetBSD
     26  *	Foundation, Inc. and its contributors.
     27  * 4. Neither the name of The NetBSD Foundation nor the names of its
     28  *    contributors may be used to endorse or promote products derived
     29  *    from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 /*
     45  * Copyright (c) 1989, 1993
     46  *	The Regents of the University of California.  All rights reserved.
     47  * (c) UNIX System Laboratories, Inc.
     48  * All or some portions of this file are derived from material licensed
     49  * to the University of California by American Telephone and Telegraph
     50  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     51  * the permission of UNIX System Laboratories, Inc.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  * 3. Neither the name of the University nor the names of its contributors
     62  *    may be used to endorse or promote products derived from this software
     63  *    without specific prior written permission.
     64  *
     65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     75  * SUCH DAMAGE.
     76  *
     77  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
     78  */
     79 
     80 /*
     81  * VFS exports list management.
     82  */
     83 
     84 #include <sys/cdefs.h>
     85 __KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.1 2005/09/23 12:10:33 jmmv Exp $");
     86 
     87 #include "opt_inet.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/systm.h>
     91 #include <sys/queue.h>
     92 #include <sys/proc.h>
     93 #include <sys/mount.h>
     94 #include <sys/vnode.h>
     95 #include <sys/namei.h>
     96 #include <sys/errno.h>
     97 #include <sys/malloc.h>
     98 #include <sys/domain.h>
     99 #include <sys/mbuf.h>
    100 #include <sys/dirent.h>
    101 #include <sys/socket.h>		/* XXX for AF_MAX */
    102 
    103 #include <net/radix.h>
    104 
    105 #include <netinet/in.h>
    106 
    107 #include <nfs/rpcv2.h>
    108 #include <nfs/nfsproto.h>
    109 #include <nfs/nfs.h>
    110 #include <nfs/nfs_var.h>
    111 
    112 /*
    113  * Network address lookup element.
    114  */
    115 struct netcred {
    116 	struct	radix_node netc_rnodes[2];
    117 	int	netc_refcnt;
    118 	int	netc_exflags;
    119 	struct	ucred netc_anon;
    120 };
    121 
    122 /*
    123  * Network export information.
    124  */
    125 struct netexport {
    126 	struct	netcred ne_defexported;		      /* Default export */
    127 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
    128 };
    129 
    130 /*
    131  * Structures to map between standard mount points to their corresponding
    132  * network export information.
    133  */
    134 struct mount_netexport_pair {
    135 	CIRCLEQ_ENTRY(mount_netexport_pair) mnp_entries;
    136 	const struct mount *mnp_mount;
    137 	struct netexport mnp_netexport;
    138 };
    139 CIRCLEQ_HEAD(mount_netexport_map, mount_netexport_pair)
    140     mount_netexport_map = CIRCLEQ_HEAD_INITIALIZER(mount_netexport_map);
    141 
    142 /* Malloc type used by the mount<->netexport map. */
    143 MALLOC_DEFINE(M_NFS_EXPORT, "nfs_export", "NFS export data");
    144 
    145 /* Publicly exported file system. */
    146 struct nfs_public nfs_pub;
    147 
    148 /*
    149  * Local prototypes.
    150  */
    151 static int init_exports(struct mount *, struct mount_netexport_pair **);
    152 static int hang_addrlist(struct mount *, struct netexport *,
    153     const struct export_args *);
    154 static int sacheck(struct sockaddr *);
    155 static int free_netcred(struct radix_node *, void *);
    156 static void clear_exports(struct mount *, struct netexport *);
    157 static int export(struct mount *, struct netexport *,
    158     const struct export_args *);
    159 static int setpublicfs(struct mount *, struct netexport *,
    160     const struct export_args *);
    161 static struct netcred *export_lookup(struct mount *, struct netexport *,
    162     struct mbuf *);
    163 
    164 /*
    165  * PUBLIC INTERFACE
    166  */
    167 
    168 /*
    169  * Declare and initialize the file system export hooks.
    170  */
    171 static void nfs_export_unmount(struct mount *);
    172 
    173 struct vfs_hooks nfs_export_hooks = {
    174 	nfs_export_unmount
    175 };
    176 VFS_HOOKS_ATTACH(nfs_export_hooks);
    177 
    178 /*
    179  * VFS unmount hook for NFS exports.
    180  *
    181  * Releases NFS exports list resources if the given mount point has some.
    182  * As allocation happens lazily, it may be that it doesn't has this
    183  * information, although it theorically should.
    184  */
    185 static void
    186 nfs_export_unmount(struct mount *mp)
    187 {
    188 	boolean_t found;
    189 	struct mount_netexport_pair *mnp;
    190 
    191 	KASSERT(mp != NULL);
    192 
    193 	found = FALSE;
    194 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    195 		if (mnp->mnp_mount == mp) {
    196 			found = TRUE;
    197 			break;
    198 		}
    199 	}
    200 
    201 	if (mp->mnt_op->vfs_vptofh == NULL || mp->mnt_op->vfs_fhtovp == NULL)
    202 		KASSERT(!found);
    203 	else if (found) {
    204 		if (mp->mnt_flag & MNT_EXPUBLIC)
    205 			setpublicfs(NULL, NULL, NULL);
    206 
    207 		free(mnp, M_NFS_EXPORT);
    208 	}
    209 }
    210 
    211 /*
    212  * Atomically set the NFS exports list of the given file system, replacing
    213  * it with a new list of entries.
    214  *
    215  * Returns zero on success or an appropriate error code otherwise.
    216  *
    217  * Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
    218  * command).
    219  */
    220 int
    221 mountd_set_exports_list(const struct mountd_exports_list *mel, struct proc *p)
    222 {
    223 	boolean_t found;
    224 	int error;
    225 #ifdef notyet
    226 	/* XXX: See below to see the reason why this is disabled. */
    227 	size_t i;
    228 #endif
    229 	struct mount *mp;
    230 	struct mount_netexport_pair *mnp;
    231 	struct nameidata nd;
    232 	struct vnode *vp;
    233 
    234 	if (suser(p->p_ucred, &p->p_acflag) != 0)
    235 		return EPERM;
    236 
    237 	/* Lookup the file system path. */
    238 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path, p);
    239 	error = namei(&nd);
    240 	if (error != 0)
    241 		return error;
    242 	vp = (struct vnode *)nd.ni_vp;
    243 	mp = (struct mount *)vp->v_mount;
    244 
    245 	/* The selected file system may not support NFS exports, so ensure
    246 	 * it does. */
    247 	if (mp->mnt_op->vfs_vptofh == NULL && mp->mnt_op->vfs_fhtovp == NULL) {
    248 		error = EOPNOTSUPP;
    249 		goto out_locked;
    250 	}
    251 	KASSERT(mp->mnt_op->vfs_vptofh != NULL &&
    252 	    mp->mnt_op->vfs_fhtovp != NULL);
    253 
    254 	/* Mark the file system busy. */
    255 	error = vfs_busy(mp, LK_NOWAIT, NULL);
    256 	if (error != 0)
    257 		goto out_locked;
    258 
    259 	found = FALSE;
    260 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    261 		if (mnp->mnp_mount == mp) {
    262 			found = TRUE;
    263 			break;
    264 		}
    265 	}
    266 	if (!found) {
    267 		error = init_exports(mp, &mnp);
    268 		if (error != 0) {
    269 			vfs_unbusy(mp);
    270 			goto out_locked;
    271 		}
    272 	}
    273 
    274 	/*
    275 	 * XXX: The part marked as 'notyet' works fine from the kernel's
    276 	 * point of view, in the sense that it is able to atomically update
    277 	 * the complete exports list for a file system.  However, supporting
    278 	 * this in mountd(8) requires a lot of work; so, for now, keep the
    279 	 * old behavior of updating a single entry per call.
    280 	 *
    281 	 * When mountd(8) is fixed, just remove the second branch of this
    282 	 * preprocessor conditional and enable the first one.
    283 	 */
    284 #ifdef notyet
    285 	clear_exports(mp, &mnp->mnp_netexport);
    286 	for (i = 0; error == 0 && i < mel->mel_nexports; i++)
    287 		error = export(mp, &mnp->mnp_netexport, &mel->mel_exports[i]);
    288 #else
    289 	if (mel->mel_nexports == 0)
    290 		clear_exports(mp, &mnp->mnp_netexport);
    291 	else if (mel->mel_nexports == 1)
    292 		error = export(mp, &mnp->mnp_netexport, &mel->mel_exports[0]);
    293 	else {
    294 		printf("mountd_set_exports_list: Cannot set more than one "
    295 		    "entry at once (unimplemented)\n");
    296 		error = EOPNOTSUPP;
    297 	}
    298 #endif
    299 
    300 	vfs_unbusy(mp);
    301 
    302 out_locked:
    303 	vput(vp);
    304 
    305 	return 0;
    306 }
    307 
    308 /*
    309  * Check if the file system specified by the 'mp' mount structure is
    310  * exported to a client with 'anon' anonymous credentials.  The 'mb'
    311  * argument is an mbuf containing the network address of the client.
    312  * The return parameters for the export flags for the client are returned
    313  * in the address specified by 'wh'.
    314  *
    315  * This function is used exclusively by the NFS server.  It is generally
    316  * invoked before VFS_FHTOVP to validate that client has access to the
    317  * file system.
    318  */
    319 int
    320 nfs_check_export(struct mount *mp, struct mbuf *mb, int *wh,
    321     struct ucred **anon)
    322 {
    323 	boolean_t found;
    324 	struct mount_netexport_pair *mnp;
    325 	struct netcred *np;
    326 
    327 	found = FALSE;
    328 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    329 		if (mnp->mnp_mount == mp) {
    330 			found = TRUE;
    331 			break;
    332 		}
    333 	}
    334 	if (!found)
    335 		return EACCES;
    336 
    337 	np = export_lookup(mp, &mnp->mnp_netexport, mb);
    338 	if (np != NULL) {
    339 		*wh = np->netc_exflags;
    340 		*anon = &np->netc_anon;
    341 	}
    342 
    343 	return np == NULL ? EACCES : 0;
    344 }
    345 
    346 /*
    347  * INTERNAL FUNCTIONS
    348  */
    349 
    350 /*
    351  * Initializes NFS exports for the file system given in 'mp' if it supports
    352  * file handles; this is determined by checking whether mp's vfs_vptofh and
    353  * vfs_fhtovp operations are NULL or not.
    354  *
    355  * If successful, returns 0 and sets *mnpp to the address of the new
    356  * mount_netexport_pair item; otherwise returns and appropriate error code
    357  * and *mnpp remains unmodified.
    358  */
    359 static int
    360 init_exports(struct mount *mp, struct mount_netexport_pair **mnpp)
    361 {
    362 	int error;
    363 	struct export_args ea;
    364 	struct mount_netexport_pair *mnp;
    365 
    366 	KASSERT(mp != NULL);
    367 	KASSERT(mp->mnt_op->vfs_vptofh != NULL &&
    368 	    mp->mnt_op->vfs_fhtovp != NULL);
    369 
    370 #ifdef DIAGNOSTIC
    371 	/* Ensure that we do not already have this mount point. */
    372 	CIRCLEQ_FOREACH(mnp, &mount_netexport_map, mnp_entries) {
    373 		if (mnp->mnp_mount == mp)
    374 			KASSERT(0);
    375 	}
    376 #endif
    377 
    378 	mnp = (struct mount_netexport_pair *)
    379 	    malloc(sizeof(struct mount_netexport_pair), M_NFS_EXPORT, M_WAITOK);
    380 	KASSERT(mnp != NULL);
    381 	mnp->mnp_mount = mp;
    382 	memset(&mnp->mnp_netexport, 0, sizeof(mnp->mnp_netexport));
    383 
    384 	/* Set the default export entry.  Handled internally by export upon
    385 	 * first call. */
    386 	memset(&ea, 0, sizeof(ea));
    387 	ea.ex_root = -2;
    388 	if (mp->mnt_flag & MNT_RDONLY)
    389 		ea.ex_flags |= MNT_EXRDONLY;
    390 	error = export(mp, &mnp->mnp_netexport, &ea);
    391 	if (error != 0)
    392 		free(mnp, M_NFS_EXPORT);
    393 	else {
    394 		CIRCLEQ_INSERT_TAIL(&mount_netexport_map, mnp, mnp_entries);
    395 		*mnpp = mnp;
    396 	}
    397 
    398 	return error;
    399 }
    400 
    401 /*
    402  * Build hash lists of net addresses and hang them off the mount point.
    403  * Called by export() to set up a new entry in the lists of export
    404  * addresses.
    405  */
    406 static int
    407 hang_addrlist(struct mount *mp, struct netexport *nep,
    408     const struct export_args *argp)
    409 {
    410 	int error, i;
    411 	struct netcred *np, *enp;
    412 	struct radix_node_head *rnh;
    413 	struct sockaddr *saddr, *smask;
    414 	struct domain *dom;
    415 
    416 	smask = NULL;
    417 
    418 	if (argp->ex_addrlen == 0) {
    419 		if (mp->mnt_flag & MNT_DEFEXPORTED)
    420 			return EPERM;
    421 		np = &nep->ne_defexported;
    422 		np->netc_exflags = argp->ex_flags;
    423 		crcvt(&np->netc_anon, &argp->ex_anon);
    424 		np->netc_anon.cr_ref = 1;
    425 		mp->mnt_flag |= MNT_DEFEXPORTED;
    426 		return 0;
    427 	}
    428 
    429 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
    430 		return EINVAL;
    431 
    432 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
    433 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
    434 	memset((caddr_t)np, 0, i);
    435 	saddr = (struct sockaddr *)(np + 1);
    436 	error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen);
    437 	if (error)
    438 		goto out;
    439 	if (saddr->sa_len > argp->ex_addrlen)
    440 		saddr->sa_len = argp->ex_addrlen;
    441 	if (sacheck(saddr) == -1)
    442 		return EINVAL;
    443 	if (argp->ex_masklen) {
    444 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
    445 		error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
    446 		if (error)
    447 			goto out;
    448 		if (smask->sa_len > argp->ex_masklen)
    449 			smask->sa_len = argp->ex_masklen;
    450 		if (smask->sa_family != saddr->sa_family)
    451 			return EINVAL;
    452 		if (sacheck(smask) == -1)
    453 			return EINVAL;
    454 	}
    455 	i = saddr->sa_family;
    456 	if ((rnh = nep->ne_rtable[i]) == 0) {
    457 		/*
    458 		 * Seems silly to initialize every AF when most are not
    459 		 * used, do so on demand here
    460 		 */
    461 		DOMAIN_FOREACH(dom) {
    462 			if (dom->dom_family == i && dom->dom_rtattach) {
    463 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
    464 					dom->dom_rtoffset);
    465 				break;
    466 			}
    467 		}
    468 		if ((rnh = nep->ne_rtable[i]) == 0) {
    469 			error = ENOBUFS;
    470 			goto out;
    471 		}
    472 	}
    473 
    474 	enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
    475 	    np->netc_rnodes);
    476 	if (enp != np) {
    477 		if (enp == NULL) {
    478 			enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
    479 			    smask, rnh);
    480 			if (enp == NULL) {
    481 				error = EPERM;
    482 				goto out;
    483 			}
    484 		} else
    485 			enp->netc_refcnt++;
    486 
    487 		goto check;
    488 	} else
    489 		enp->netc_refcnt = 1;
    490 
    491 	np->netc_exflags = argp->ex_flags;
    492 	crcvt(&np->netc_anon, &argp->ex_anon);
    493 	np->netc_anon.cr_ref = 1;
    494 	return 0;
    495 check:
    496 	if (enp->netc_exflags != argp->ex_flags ||
    497 	    crcmp(&enp->netc_anon, &argp->ex_anon) != 0)
    498 		error = EPERM;
    499 	else
    500 		error = 0;
    501 out:
    502 	free(np, M_NETADDR);
    503 	return error;
    504 }
    505 
    506 /*
    507  * Ensure that the address stored in 'sa' is valid.
    508  * Returns zero on success, otherwise -1.
    509  */
    510 static int
    511 sacheck(struct sockaddr *sa)
    512 {
    513 
    514 	switch (sa->sa_family) {
    515 #ifdef INET
    516 	case AF_INET: {
    517 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    518 		char *p = (char *)sin->sin_zero;
    519 		size_t i;
    520 
    521 		if (sin->sin_len != sizeof(*sin))
    522 			return -1;
    523 		if (sin->sin_port != 0)
    524 			return -1;
    525 		for (i = 0; i < sizeof(sin->sin_zero); i++)
    526 			if (*p++ != '\0')
    527 				return -1;
    528 		return 0;
    529 	}
    530 #endif
    531 #ifdef INET6
    532 	case AF_INET6: {
    533 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    534 
    535 		if (sin6->sin6_len != sizeof(*sin6))
    536 			return -1;
    537 		if (sin6->sin6_port != 0)
    538 			return -1;
    539 		return 0;
    540 	}
    541 #endif
    542 	default:
    543 		return -1;
    544 	}
    545 }
    546 
    547 /*
    548  * Free the netcred object pointed to by the 'rn' radix node.
    549  * 'w' holds a pointer to the radix tree head.
    550  */
    551 static int
    552 free_netcred(struct radix_node *rn, void *w)
    553 {
    554 	struct radix_node_head *rnh = (struct radix_node_head *)w;
    555 	struct netcred *np = (struct netcred *)(void *)rn;
    556 
    557 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
    558 	if (--(np->netc_refcnt) <= 0)
    559 		free(np, M_NETADDR);
    560 	return 0;
    561 }
    562 
    563 /*
    564  * Clears the exports list for a given file system.
    565  */
    566 static void
    567 clear_exports(struct mount *mp, struct netexport *nep)
    568 {
    569 	int i;
    570 	struct radix_node_head *rnh;
    571 
    572 	if (mp->mnt_flag & MNT_EXPUBLIC) {
    573 		setpublicfs(NULL, NULL, NULL);
    574 		mp->mnt_flag &= ~MNT_EXPUBLIC;
    575 	}
    576 
    577 	for (i = 0; i <= AF_MAX; i++) {
    578 		if ((rnh = nep->ne_rtable[i]) != NULL) {
    579 			(*rnh->rnh_walktree)(rnh, free_netcred, rnh);
    580 			free((caddr_t)rnh, M_RTABLE);
    581 			nep->ne_rtable[i] = 0;
    582 		}
    583 	}
    584 
    585 	mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
    586 }
    587 
    588 /*
    589  * Add a new export entry (described by an export_args structure) to the
    590  * given file system.
    591  */
    592 static int
    593 export(struct mount *mp, struct netexport *nep, const struct export_args *argp)
    594 {
    595 	int error;
    596 
    597 	if (argp->ex_flags & MNT_EXPORTED) {
    598 		if (argp->ex_flags & MNT_EXPUBLIC) {
    599 			if ((error = setpublicfs(mp, nep, argp)) != 0)
    600 				return error;
    601 			mp->mnt_flag |= MNT_EXPUBLIC;
    602 		}
    603 		if ((error = hang_addrlist(mp, nep, argp)) != 0)
    604 			return error;
    605 		mp->mnt_flag |= MNT_EXPORTED;
    606 	}
    607 	return 0;
    608 }
    609 
    610 /*
    611  * Set the publicly exported filesystem (WebNFS).  Currently, only
    612  * one public filesystem is possible in the spec (RFC 2054 and 2055)
    613  */
    614 static int
    615 setpublicfs(struct mount *mp, struct netexport *nep,
    616     const struct export_args *argp)
    617 {
    618 	char *cp;
    619 	int error;
    620 	struct vnode *rvp;
    621 
    622 	/*
    623 	 * mp == NULL -> invalidate the current info, the FS is
    624 	 * no longer exported. May be called from either export
    625 	 * or unmount, so check if it hasn't already been done.
    626 	 */
    627 	if (mp == NULL) {
    628 		if (nfs_pub.np_valid) {
    629 			nfs_pub.np_valid = 0;
    630 			if (nfs_pub.np_index != NULL) {
    631 				FREE(nfs_pub.np_index, M_TEMP);
    632 				nfs_pub.np_index = NULL;
    633 			}
    634 		}
    635 		return 0;
    636 	}
    637 
    638 	/*
    639 	 * Only one allowed at a time.
    640 	 */
    641 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
    642 		return EBUSY;
    643 
    644 	/*
    645 	 * Get real filehandle for root of exported FS.
    646 	 */
    647 	memset((caddr_t)&nfs_pub.np_handle, 0, sizeof(nfs_pub.np_handle));
    648 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsidx;
    649 
    650 	if ((error = VFS_ROOT(mp, &rvp)))
    651 		return error;
    652 
    653 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
    654 		return error;
    655 
    656 	vput(rvp);
    657 
    658 	/*
    659 	 * If an indexfile was specified, pull it in.
    660 	 */
    661 	if (argp->ex_indexfile != NULL) {
    662 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
    663 		    M_WAITOK);
    664 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
    665 		    MAXNAMLEN, (size_t *)0);
    666 		if (!error) {
    667 			/*
    668 			 * Check for illegal filenames.
    669 			 */
    670 			for (cp = nfs_pub.np_index; *cp; cp++) {
    671 				if (*cp == '/') {
    672 					error = EINVAL;
    673 					break;
    674 				}
    675 			}
    676 		}
    677 		if (error) {
    678 			FREE(nfs_pub.np_index, M_TEMP);
    679 			return error;
    680 		}
    681 	}
    682 
    683 	nfs_pub.np_mount = mp;
    684 	nfs_pub.np_valid = 1;
    685 	return 0;
    686 }
    687 
    688 /*
    689  * Lookup an export entry in the exports list that matches the address
    690  * stored in 'nam'.  If no entry is found, the default one is used instead
    691  * (if available).
    692  */
    693 static struct netcred *
    694 export_lookup(struct mount *mp, struct netexport *nep, struct mbuf *nam)
    695 {
    696 	struct netcred *np;
    697 	struct radix_node_head *rnh;
    698 	struct sockaddr *saddr;
    699 
    700 	np = NULL;
    701 	if (mp->mnt_flag & MNT_EXPORTED) {
    702 		/*
    703 		 * Lookup in the export list first.
    704 		 */
    705 		if (nam != NULL) {
    706 			saddr = mtod(nam, struct sockaddr *);
    707 			rnh = nep->ne_rtable[saddr->sa_family];
    708 			if (rnh != NULL) {
    709 				np = (struct netcred *)
    710 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
    711 							      rnh);
    712 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
    713 					np = NULL;
    714 			}
    715 		}
    716 		/*
    717 		 * If no address match, use the default if it exists.
    718 		 */
    719 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
    720 			np = &nep->ne_defexported;
    721 	}
    722 	return np;
    723 }
    724