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