Home | History | Annotate | Line # | Download | only in nfs
nfs_export.c revision 1.41
      1 /*	$NetBSD: nfs_export.c,v 1.41 2008/11/25 14:04:23 pooka 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.41 2008/11/25 14:04:23 pooka 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 	size_t fid_size;
    217 
    218 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
    219 	    KAUTH_REQ_NETWORK_NFS_EXPORT, NULL, NULL, NULL) != 0)
    220 		return EPERM;
    221 
    222 	/* Lookup the file system path. */
    223 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path);
    224 	error = namei(&nd);
    225 	if (error != 0)
    226 		return error;
    227 	vp = nd.ni_vp;
    228 	mp = vp->v_mount;
    229 
    230 	/*
    231 	 * Make sure the file system can do vptofh.  If the file system
    232 	 * knows the handle's size, just trust it's able to do the
    233 	 * actual translation also (otherwise we should check fhtovp
    234 	 * also, and that's getting a wee bit ridiculous).
    235 	 */
    236 	fid_size = 0;
    237 	if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) != E2BIG) {
    238 		vput(vp);
    239 		return EOPNOTSUPP;
    240 	}
    241 
    242 	/* Mark the file system busy. */
    243 	error = vfs_busy(mp, NULL);
    244 	vput(vp);
    245 	if (error != 0)
    246 		return error;
    247 
    248 	netexport_wrlock();
    249 	ne = netexport_lookup(mp);
    250 	if (ne == NULL) {
    251 		error = init_exports(mp, &ne);
    252 		if (error != 0) {
    253 			goto out;
    254 		}
    255 	}
    256 
    257 	KASSERT(ne != NULL);
    258 	KASSERT(ne->ne_mount == mp);
    259 
    260 	/*
    261 	 * XXX: The part marked as 'notyet' works fine from the kernel's
    262 	 * point of view, in the sense that it is able to atomically update
    263 	 * the complete exports list for a file system.  However, supporting
    264 	 * this in mountd(8) requires a lot of work; so, for now, keep the
    265 	 * old behavior of updating a single entry per call.
    266 	 *
    267 	 * When mountd(8) is fixed, just remove the second branch of this
    268 	 * preprocessor conditional and enable the first one.
    269 	 */
    270 #ifdef notyet
    271 	netexport_clear(ne);
    272 	for (i = 0; error == 0 && i < mel->mel_nexports; i++)
    273 		error = export(ne, &mel->mel_exports[i]);
    274 #else
    275 	if (mel->mel_nexports == 0)
    276 		netexport_clear(ne);
    277 	else if (mel->mel_nexports == 1)
    278 		error = export(ne, &mel->mel_exports[0]);
    279 	else {
    280 		printf("mountd_set_exports_list: Cannot set more than one "
    281 		    "entry at once (unimplemented)\n");
    282 		error = EOPNOTSUPP;
    283 	}
    284 #endif
    285 
    286 out:
    287 	netexport_wrunlock();
    288 	vfs_unbusy(mp, false, NULL);
    289 	return error;
    290 }
    291 
    292 static void
    293 netexport_insert(struct netexport *ne)
    294 {
    295 
    296 	CIRCLEQ_INSERT_HEAD(&netexport_list, ne, ne_list);
    297 }
    298 
    299 static void
    300 netexport_remove(struct netexport *ne)
    301 {
    302 
    303 	CIRCLEQ_REMOVE(&netexport_list, ne, ne_list);
    304 }
    305 
    306 static struct netexport *
    307 netexport_lookup(const struct mount *mp)
    308 {
    309 	struct netexport *ne;
    310 
    311 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
    312 		if (ne->ne_mount == mp) {
    313 			goto done;
    314 		}
    315 	}
    316 	ne = NULL;
    317 done:
    318 	return ne;
    319 }
    320 
    321 static struct netexport *
    322 netexport_lookup_byfsid(const fsid_t *fsid)
    323 {
    324 	struct netexport *ne;
    325 
    326 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
    327 		const struct mount *mp = ne->ne_mount;
    328 
    329 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
    330 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
    331 			goto done;
    332 		}
    333 	}
    334 	ne = NULL;
    335 done:
    336 
    337 	return ne;
    338 }
    339 
    340 /*
    341  * Check if the file system specified by the 'mp' mount structure is
    342  * exported to a client with 'anon' anonymous credentials.  The 'mb'
    343  * argument is an mbuf containing the network address of the client.
    344  * The return parameters for the export flags for the client are returned
    345  * in the address specified by 'wh'.
    346  *
    347  * This function is used exclusively by the NFS server.  It is generally
    348  * invoked before VFS_FHTOVP to validate that client has access to the
    349  * file system.
    350  */
    351 
    352 int
    353 netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
    354     int *wh, kauth_cred_t *anon)
    355 {
    356 	struct netexport *ne;
    357 	struct netcred *np;
    358 
    359 	ne = netexport_lookup_byfsid(fsid);
    360 	if (ne == NULL) {
    361 		return EACCES;
    362 	}
    363 	np = netcred_lookup(ne, mb);
    364 	if (np == NULL) {
    365 		return EACCES;
    366 	}
    367 
    368 	*mpp = ne->ne_mount;
    369 	*wh = np->netc_exflags;
    370 	*anon = np->netc_anon;
    371 
    372 	return 0;
    373 }
    374 
    375 /*
    376  * Handles legacy export requests.  In this case, the export information
    377  * is hardcoded in a specific place of the mount arguments structure (given
    378  * in data); the request for an update is given through the fspec field
    379  * (also in a known location), which must be a null pointer.
    380  *
    381  * Returns EJUSTRETURN if the given command was not a export request.
    382  * Otherwise, returns 0 on success or an appropriate error code otherwise.
    383  */
    384 static int
    385 nfs_export_update_30(struct mount *mp, const char *path, void *data)
    386 {
    387 	struct mountd_exports_list mel;
    388 	struct mnt_export_args30 *args;
    389 
    390 	args = data;
    391 	mel.mel_path = path;
    392 
    393 	if (args->fspec != NULL)
    394 		return EJUSTRETURN;
    395 
    396 	if (args->eargs.ex_flags & 0x00020000) {
    397 		/* Request to delete exports.  The mask above holds the
    398 		 * value that used to be in MNT_DELEXPORT. */
    399 		mel.mel_nexports = 0;
    400 	} else {
    401 		/* The following assumes export_args has not changed since
    402 		 * export_args30 - typedef checks sizes. */
    403 		typedef char x[sizeof args->eargs == sizeof *mel.mel_exports ? 1 : -1];
    404 
    405 		mel.mel_nexports = 1;
    406 		mel.mel_exports = (void *)&args->eargs;
    407 	}
    408 
    409 	return mountd_set_exports_list(&mel, curlwp);
    410 }
    411 
    412 /*
    413  * INTERNAL FUNCTIONS
    414  */
    415 
    416 /*
    417  * Initializes NFS exports for the file system given in 'mp' if it supports
    418  * file handles; this is determined by checking whether mp's vfs_vptofh and
    419  * vfs_fhtovp operations are NULL or not.
    420  *
    421  * If successful, returns 0 and sets *mnpp to the address of the new
    422  * mount_netexport_pair item; otherwise returns an appropriate error code
    423  * and *mnpp remains unmodified.
    424  */
    425 static int
    426 init_exports(struct mount *mp, struct netexport **nep)
    427 {
    428 	int error;
    429 	struct export_args ea;
    430 	struct netexport *ne;
    431 
    432 	KASSERT(mp != NULL);
    433 
    434 	/* Ensure that we do not already have this mount point. */
    435 	KASSERT(netexport_lookup(mp) == NULL);
    436 
    437 	ne = malloc(sizeof(*ne), M_NFS_EXPORT, M_WAITOK | M_ZERO);
    438 	ne->ne_mount = mp;
    439 
    440 	/* Set the default export entry.  Handled internally by export upon
    441 	 * first call. */
    442 	memset(&ea, 0, sizeof(ea));
    443 	ea.ex_root = -2;
    444 	if (mp->mnt_flag & MNT_RDONLY)
    445 		ea.ex_flags |= MNT_EXRDONLY;
    446 	error = export(ne, &ea);
    447 	if (error != 0) {
    448 		free(ne, M_NFS_EXPORT);
    449 	} else {
    450 		netexport_insert(ne);
    451 		*nep = ne;
    452 	}
    453 
    454 	return error;
    455 }
    456 
    457 /*
    458  * Build hash lists of net addresses and hang them off the mount point.
    459  * Called by export() to set up a new entry in the lists of export
    460  * addresses.
    461  */
    462 static int
    463 hang_addrlist(struct mount *mp, struct netexport *nep,
    464     const struct export_args *argp)
    465 {
    466 	int error, i;
    467 	struct netcred *np, *enp;
    468 	struct radix_node_head *rnh;
    469 	struct sockaddr *saddr, *smask;
    470 	struct domain *dom;
    471 
    472 	smask = NULL;
    473 
    474 	if (argp->ex_addrlen == 0) {
    475 		if (mp->mnt_flag & MNT_DEFEXPORTED)
    476 			return EPERM;
    477 		np = &nep->ne_defexported;
    478 		KASSERT(np->netc_anon == NULL);
    479 		np->netc_anon = kauth_cred_alloc();
    480 		np->netc_exflags = argp->ex_flags;
    481 		kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
    482 		mp->mnt_flag |= MNT_DEFEXPORTED;
    483 		return 0;
    484 	}
    485 
    486 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
    487 		return EINVAL;
    488 
    489 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
    490 	np = malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
    491 	np->netc_anon = kauth_cred_alloc();
    492 	saddr = (struct sockaddr *)(np + 1);
    493 	error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
    494 	if (error)
    495 		goto out;
    496 	if (saddr->sa_len > argp->ex_addrlen)
    497 		saddr->sa_len = argp->ex_addrlen;
    498 	if (sacheck(saddr) == -1)
    499 		return EINVAL;
    500 	if (argp->ex_masklen) {
    501 		smask = (struct sockaddr *)((char *)saddr + argp->ex_addrlen);
    502 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
    503 		if (error)
    504 			goto out;
    505 		if (smask->sa_len > argp->ex_masklen)
    506 			smask->sa_len = argp->ex_masklen;
    507 		if (smask->sa_family != saddr->sa_family)
    508 			return EINVAL;
    509 		if (sacheck(smask) == -1)
    510 			return EINVAL;
    511 	}
    512 	i = saddr->sa_family;
    513 	if ((rnh = nep->ne_rtable[i]) == 0) {
    514 		/*
    515 		 * Seems silly to initialize every AF when most are not
    516 		 * used, do so on demand here
    517 		 */
    518 		DOMAIN_FOREACH(dom) {
    519 			if (dom->dom_family == i && dom->dom_rtattach) {
    520 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
    521 					dom->dom_rtoffset);
    522 				break;
    523 			}
    524 		}
    525 		if ((rnh = nep->ne_rtable[i]) == 0) {
    526 			error = ENOBUFS;
    527 			goto out;
    528 		}
    529 	}
    530 
    531 	enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
    532 	    np->netc_rnodes);
    533 	if (enp != np) {
    534 		if (enp == NULL) {
    535 			enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
    536 			    smask, rnh);
    537 			if (enp == NULL) {
    538 				error = EPERM;
    539 				goto out;
    540 			}
    541 		} else
    542 			enp->netc_refcnt++;
    543 
    544 		goto check;
    545 	} else
    546 		enp->netc_refcnt = 1;
    547 
    548 	np->netc_exflags = argp->ex_flags;
    549 	kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
    550 	return 0;
    551 check:
    552 	if (enp->netc_exflags != argp->ex_flags ||
    553 	    kauth_cred_uucmp(enp->netc_anon, &argp->ex_anon) != 0)
    554 		error = EPERM;
    555 	else
    556 		error = 0;
    557 out:
    558 	KASSERT(np->netc_anon != NULL);
    559 	kauth_cred_free(np->netc_anon);
    560 	free(np, M_NETADDR);
    561 	return error;
    562 }
    563 
    564 /*
    565  * Ensure that the address stored in 'sa' is valid.
    566  * Returns zero on success, otherwise -1.
    567  */
    568 static int
    569 sacheck(struct sockaddr *sa)
    570 {
    571 
    572 	switch (sa->sa_family) {
    573 	case AF_INET: {
    574 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    575 		char *p = (char *)sin->sin_zero;
    576 		size_t i;
    577 
    578 		if (sin->sin_len != sizeof(*sin))
    579 			return -1;
    580 		if (sin->sin_port != 0)
    581 			return -1;
    582 		for (i = 0; i < sizeof(sin->sin_zero); i++)
    583 			if (*p++ != '\0')
    584 				return -1;
    585 		return 0;
    586 	}
    587 	case AF_INET6: {
    588 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    589 
    590 		if (sin6->sin6_len != sizeof(*sin6))
    591 			return -1;
    592 		if (sin6->sin6_port != 0)
    593 			return -1;
    594 		return 0;
    595 	}
    596 	default:
    597 		return -1;
    598 	}
    599 }
    600 
    601 /*
    602  * Free the netcred object pointed to by the 'rn' radix node.
    603  * 'w' holds a pointer to the radix tree head.
    604  */
    605 static int
    606 free_netcred(struct radix_node *rn, void *w)
    607 {
    608 	struct radix_node_head *rnh = (struct radix_node_head *)w;
    609 	struct netcred *np = (struct netcred *)(void *)rn;
    610 
    611 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
    612 	if (--(np->netc_refcnt) <= 0) {
    613 		KASSERT(np->netc_anon != NULL);
    614 		kauth_cred_free(np->netc_anon);
    615 		free(np, M_NETADDR);
    616 	}
    617 	return 0;
    618 }
    619 
    620 /*
    621  * Clears the exports list for a given file system.
    622  */
    623 static void
    624 netexport_clear(struct netexport *ne)
    625 {
    626 	struct radix_node_head *rnh;
    627 	struct mount *mp = ne->ne_mount;
    628 	int i;
    629 
    630 	if (mp->mnt_flag & MNT_EXPUBLIC) {
    631 		setpublicfs(NULL, NULL, NULL);
    632 		mp->mnt_flag &= ~MNT_EXPUBLIC;
    633 	}
    634 
    635 	for (i = 0; i <= AF_MAX; i++) {
    636 		if ((rnh = ne->ne_rtable[i]) != NULL) {
    637 			rn_walktree(rnh, free_netcred, rnh);
    638 			free(rnh, M_RTABLE);
    639 			ne->ne_rtable[i] = NULL;
    640 		}
    641 	}
    642 
    643 	if ((mp->mnt_flag & MNT_DEFEXPORTED) != 0) {
    644 		struct netcred *np = &ne->ne_defexported;
    645 
    646 		KASSERT(np->netc_anon != NULL);
    647 		kauth_cred_free(np->netc_anon);
    648 		np->netc_anon = NULL;
    649 	} else {
    650 		KASSERT(ne->ne_defexported.netc_anon == NULL);
    651 	}
    652 
    653 	mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
    654 }
    655 
    656 /*
    657  * Add a new export entry (described by an export_args structure) to the
    658  * given file system.
    659  */
    660 static int
    661 export(struct netexport *nep, const struct export_args *argp)
    662 {
    663 	struct mount *mp = nep->ne_mount;
    664 	int error;
    665 
    666 	if (argp->ex_flags & MNT_EXPORTED) {
    667 		if (argp->ex_flags & MNT_EXPUBLIC) {
    668 			if ((error = setpublicfs(mp, nep, argp)) != 0)
    669 				return error;
    670 			mp->mnt_flag |= MNT_EXPUBLIC;
    671 		}
    672 		if ((error = hang_addrlist(mp, nep, argp)) != 0)
    673 			return error;
    674 		mp->mnt_flag |= MNT_EXPORTED;
    675 	}
    676 	return 0;
    677 }
    678 
    679 /*
    680  * Set the publicly exported filesystem (WebNFS).  Currently, only
    681  * one public filesystem is possible in the spec (RFC 2054 and 2055)
    682  */
    683 static int
    684 setpublicfs(struct mount *mp, struct netexport *nep,
    685     const struct export_args *argp)
    686 {
    687 	char *cp;
    688 	int error;
    689 	struct vnode *rvp;
    690 	size_t fhsize;
    691 
    692 	/*
    693 	 * mp == NULL -> invalidate the current info, the FS is
    694 	 * no longer exported. May be called from either export
    695 	 * or unmount, so check if it hasn't already been done.
    696 	 */
    697 	if (mp == NULL) {
    698 		if (nfs_pub.np_valid) {
    699 			nfs_pub.np_valid = 0;
    700 			if (nfs_pub.np_handle != NULL) {
    701 				free(nfs_pub.np_handle, M_TEMP);
    702 				nfs_pub.np_handle = NULL;
    703 			}
    704 			if (nfs_pub.np_index != NULL) {
    705 				FREE(nfs_pub.np_index, M_TEMP);
    706 				nfs_pub.np_index = NULL;
    707 			}
    708 		}
    709 		return 0;
    710 	}
    711 
    712 	/*
    713 	 * Only one allowed at a time.
    714 	 */
    715 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
    716 		return EBUSY;
    717 
    718 	/*
    719 	 * Get real filehandle for root of exported FS.
    720 	 */
    721 	if ((error = VFS_ROOT(mp, &rvp)))
    722 		return error;
    723 
    724 	fhsize = 0;
    725 	error = vfs_composefh(rvp, NULL, &fhsize);
    726 	if (error != E2BIG)
    727 		return error;
    728 	nfs_pub.np_handle = malloc(fhsize, M_TEMP, M_NOWAIT);
    729 	if (nfs_pub.np_handle == NULL)
    730 		error = ENOMEM;
    731 	else
    732 		error = vfs_composefh(rvp, nfs_pub.np_handle, &fhsize);
    733 	if (error)
    734 		return error;
    735 
    736 	vput(rvp);
    737 
    738 	/*
    739 	 * If an indexfile was specified, pull it in.
    740 	 */
    741 	if (argp->ex_indexfile != NULL) {
    742 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
    743 		    M_WAITOK);
    744 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
    745 		    MAXNAMLEN, (size_t *)0);
    746 		if (!error) {
    747 			/*
    748 			 * Check for illegal filenames.
    749 			 */
    750 			for (cp = nfs_pub.np_index; *cp; cp++) {
    751 				if (*cp == '/') {
    752 					error = EINVAL;
    753 					break;
    754 				}
    755 			}
    756 		}
    757 		if (error) {
    758 			FREE(nfs_pub.np_index, M_TEMP);
    759 			return error;
    760 		}
    761 	}
    762 
    763 	nfs_pub.np_mount = mp;
    764 	nfs_pub.np_valid = 1;
    765 	return 0;
    766 }
    767 
    768 /*
    769  * Lookup an export entry in the exports list that matches the address
    770  * stored in 'nam'.  If no entry is found, the default one is used instead
    771  * (if available).
    772  */
    773 static struct netcred *
    774 netcred_lookup(struct netexport *ne, struct mbuf *nam)
    775 {
    776 	struct netcred *np;
    777 	struct radix_node_head *rnh;
    778 	struct sockaddr *saddr;
    779 
    780 	if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
    781 		return NULL;
    782 	}
    783 
    784 	/*
    785 	 * Lookup in the export list first.
    786 	 */
    787 	np = NULL;
    788 	if (nam != NULL) {
    789 		saddr = mtod(nam, struct sockaddr *);
    790 		rnh = ne->ne_rtable[saddr->sa_family];
    791 		if (rnh != NULL) {
    792 			np = (struct netcred *)
    793 				(*rnh->rnh_matchaddr)((void *)saddr,
    794 						      rnh);
    795 			if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
    796 				np = NULL;
    797 		}
    798 	}
    799 	/*
    800 	 * If no address match, use the default if it exists.
    801 	 */
    802 	if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
    803 		np = &ne->ne_defexported;
    804 
    805 	return np;
    806 }
    807 
    808 krwlock_t netexport_lock;
    809 
    810 void
    811 netexport_rdlock(void)
    812 {
    813 
    814 	rw_enter(&netexport_lock, RW_READER);
    815 }
    816 
    817 void
    818 netexport_rdunlock(void)
    819 {
    820 
    821 	rw_exit(&netexport_lock);
    822 }
    823 
    824 static void
    825 netexport_wrlock(void)
    826 {
    827 
    828 	rw_enter(&netexport_lock, RW_WRITER);
    829 }
    830 
    831 static void
    832 netexport_wrunlock(void)
    833 {
    834 
    835 	rw_exit(&netexport_lock);
    836 }
    837