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