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