2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/refcount.h>
50 #include <sys/socket.h>
51 #include <sys/systm.h>
52 #include <sys/vnode.h>
54 #include <net/radix.h>
56 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
58 static void vfs_free_addrlist(struct netexport *nep);
59 static int vfs_free_netcred(struct radix_node *rn, void *w);
60 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
61 struct export_args *argp);
62 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
65 * Network address lookup element
68 struct radix_node netc_rnodes[2];
70 struct ucred netc_anon;
74 * Network export information
77 struct netcred ne_defexported; /* Default export */
78 struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
82 * Build hash lists of net addresses and hang them off the mount point.
83 * Called by ufs_mount() to set up the lists of export addresses.
86 vfs_hang_addrlist(mp, nep, argp)
88 struct netexport *nep;
89 struct export_args *argp;
91 register struct netcred *np;
92 register struct radix_node_head *rnh;
94 struct radix_node *rn;
95 struct sockaddr *saddr, *smask = 0;
100 * XXX: This routine converts from a `struct xucred'
101 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
102 * operation is questionable; for example, what should be done
103 * with fields like cr_uidinfo and cr_prison? Currently, this
104 * routine does not touch them (leaves them as NULL).
106 if (argp->ex_anon.cr_version != XUCRED_VERSION)
109 if (argp->ex_addrlen == 0) {
110 if (mp->mnt_flag & MNT_DEFEXPORTED)
112 np = &nep->ne_defexported;
113 np->netc_exflags = argp->ex_flags;
114 bzero(&np->netc_anon, sizeof(np->netc_anon));
115 np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
116 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
117 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
118 sizeof(np->netc_anon.cr_groups));
119 refcount_init(&np->netc_anon.cr_ref, 1);
121 mp->mnt_flag |= MNT_DEFEXPORTED;
127 if (argp->ex_addrlen > MLEN) {
128 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
129 argp->ex_addrlen, MLEN);
134 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
135 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
136 saddr = (struct sockaddr *) (np + 1);
137 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
139 if (saddr->sa_family > AF_MAX) {
143 if (saddr->sa_len > argp->ex_addrlen)
144 saddr->sa_len = argp->ex_addrlen;
145 if (argp->ex_masklen) {
146 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
147 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
150 if (smask->sa_len > argp->ex_masklen)
151 smask->sa_len = argp->ex_masklen;
153 i = saddr->sa_family;
154 if ((rnh = nep->ne_rtable[i]) == NULL) {
156 * Seems silly to initialize every AF when most are not used,
157 * do so on demand here
159 for (dom = domains; dom; dom = dom->dom_next)
160 if (dom->dom_family == i && dom->dom_rtattach) {
161 dom->dom_rtattach((void **) &nep->ne_rtable[i],
165 if ((rnh = nep->ne_rtable[i]) == NULL) {
168 "Unable to initialize radix node head");
172 RADIX_NODE_HEAD_LOCK(rnh);
173 rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
174 RADIX_NODE_HEAD_UNLOCK(rnh);
175 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
177 vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
181 np->netc_exflags = argp->ex_flags;
182 bzero(&np->netc_anon, sizeof(np->netc_anon));
183 np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
184 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
185 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
186 sizeof(np->netc_anon.cr_groups));
187 refcount_init(&np->netc_anon.cr_ref, 1);
194 /* Helper for vfs_free_addrlist. */
197 vfs_free_netcred(rn, w)
198 struct radix_node *rn;
201 register struct radix_node_head *rnh = (struct radix_node_head *) w;
203 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
209 * Free the net address hash lists that are hanging off the mount points.
212 vfs_free_addrlist(nep)
213 struct netexport *nep;
216 register struct radix_node_head *rnh;
218 for (i = 0; i <= AF_MAX; i++)
219 if ((rnh = nep->ne_rtable[i])) {
220 RADIX_NODE_HEAD_LOCK(rnh);
221 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
222 RADIX_NODE_HEAD_DESTROY(rnh);
224 nep->ne_rtable[i] = NULL; /* not SMP safe XXX */
229 * High level function to manipulate export options on a mount point
230 * and the passed in netexport.
231 * Struct export_args *argp is the variable used to twiddle options,
232 * the structure is described in sys/mount.h
237 struct export_args *argp;
239 struct netexport *nep;
242 nep = mp->mnt_export;
243 if (argp->ex_flags & MNT_DELEXPORT) {
246 if (mp->mnt_flag & MNT_EXPUBLIC) {
247 vfs_setpublicfs(NULL, NULL, NULL);
249 mp->mnt_flag &= ~MNT_EXPUBLIC;
252 vfs_free_addrlist(nep);
253 mp->mnt_export = NULL;
257 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
260 if (argp->ex_flags & MNT_EXPORTED) {
262 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
263 mp->mnt_export = nep;
265 if (argp->ex_flags & MNT_EXPUBLIC) {
266 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
269 mp->mnt_flag |= MNT_EXPUBLIC;
272 if ((error = vfs_hang_addrlist(mp, nep, argp)))
275 mp->mnt_flag |= MNT_EXPORTED;
282 * Set the publicly exported filesystem (WebNFS). Currently, only
283 * one public filesystem is possible in the spec (RFC 2054 and 2055)
286 vfs_setpublicfs(mp, nep, argp)
288 struct netexport *nep;
289 struct export_args *argp;
296 * mp == NULL -> invalidate the current info, the FS is
297 * no longer exported. May be called from either vfs_export
298 * or unmount, so check if it hasn't already been done.
301 if (nfs_pub.np_valid) {
302 nfs_pub.np_valid = 0;
303 if (nfs_pub.np_index != NULL) {
304 FREE(nfs_pub.np_index, M_TEMP);
305 nfs_pub.np_index = NULL;
312 * Only one allowed at a time.
314 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
318 * Get real filehandle for root of exported FS.
320 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
321 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
323 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */)))
326 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
332 * If an indexfile was specified, pull it in.
334 if (argp->ex_indexfile != NULL) {
335 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
337 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
338 MAXNAMLEN, (size_t *)0);
341 * Check for illegal filenames.
343 for (cp = nfs_pub.np_index; *cp; cp++) {
351 FREE(nfs_pub.np_index, M_TEMP);
356 nfs_pub.np_mount = mp;
357 nfs_pub.np_valid = 1;
362 * Used by the filesystems to determine if a given network address
363 * (passed in 'nam') is present in thier exports list, returns a pointer
364 * to struct netcred so that the filesystem can examine it for
365 * access rights (read/write/etc).
367 static struct netcred *
368 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
370 struct netexport *nep;
371 register struct netcred *np;
372 register struct radix_node_head *rnh;
373 struct sockaddr *saddr;
375 nep = mp->mnt_export;
379 if (mp->mnt_flag & MNT_EXPORTED) {
381 * Lookup in the export list first.
385 rnh = nep->ne_rtable[saddr->sa_family];
387 RADIX_NODE_HEAD_LOCK(rnh);
388 np = (struct netcred *)
389 (*rnh->rnh_matchaddr)(saddr, rnh);
390 RADIX_NODE_HEAD_UNLOCK(rnh);
391 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
396 * If no address match, use the default if it exists.
398 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
399 np = &nep->ne_defexported;
405 * XXX: This comment comes from the deprecated ufs_check_export()
406 * XXX: and may not entirely apply, but lacking something better:
407 * This is the generic part of fhtovp called after the underlying
408 * filesystem has validated the file handle.
410 * Verify that a host should have access to a filesystem.
414 vfs_stdcheckexp(mp, nam, extflagsp, credanonp)
416 struct sockaddr *nam;
418 struct ucred **credanonp;
422 np = vfs_export_lookup(mp, nam);
425 *extflagsp = np->netc_exflags;
426 *credanonp = &np->netc_anon;