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