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