]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/vfs_mount.c
MFC r222454:
[FreeBSD/stable/8.git] / sys / kern / vfs_mount.c
1 /*-
2  * Copyright (c) 1999-2004 Poul-Henning Kamp
3  * Copyright (c) 1999 Michael Smith
4  * Copyright (c) 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/fcntl.h>
43 #include <sys/jail.h>
44 #include <sys/kernel.h>
45 #include <sys/libkern.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/filedesc.h>
53 #include <sys/reboot.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysproto.h>
56 #include <sys/sx.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/vnode.h>
61 #include <vm/uma.h>
62
63 #include <geom/geom.h>
64
65 #include <machine/stdarg.h>
66
67 #include <security/audit/audit.h>
68 #include <security/mac/mac_framework.h>
69
70 #include "opt_rootdevname.h"
71
72 #define ROOTNAME                "root_device"
73 #define VFS_MOUNTARG_SIZE_MAX   (1024 * 64)
74
75 static void     set_rootvnode(void);
76 static int      vfs_domount(struct thread *td, const char *fstype,
77                     char *fspath, int fsflags, void *fsdata);
78 static int      vfs_mountroot_ask(void);
79 static int      vfs_mountroot_try(const char *mountfrom, const char *options);
80 static void     free_mntarg(struct mntarg *ma);
81
82 static int      usermount = 0;
83 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
84     "Unprivileged users may mount and unmount file systems");
85
86 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
87 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
88 static uma_zone_t mount_zone;
89
90 /* List of mounted filesystems. */
91 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
92
93 /* For any iteration/modification of mountlist */
94 struct mtx mountlist_mtx;
95 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
96
97 /*
98  * The vnode of the system's root (/ in the filesystem, without chroot
99  * active.)
100  */
101 struct vnode    *rootvnode;
102
103 /*
104  * The root filesystem is detailed in the kernel environment variable
105  * vfs.root.mountfrom, which is expected to be in the general format
106  *
107  * <vfsname>:[<path>][  <vfsname>:[<path>] ...]
108  * vfsname   := the name of a VFS known to the kernel and capable
109  *              of being mounted as root
110  * path      := disk device name or other data used by the filesystem
111  *              to locate its physical store
112  *
113  * If the environment variable vfs.root.mountfrom is a space separated list,
114  * each list element is tried in turn and the root filesystem will be mounted
115  * from the first one that suceeds.
116  *
117  * The environment variable vfs.root.mountfrom.options is a comma delimited
118  * set of string mount options.  These mount options must be parseable
119  * by nmount() in the kernel.
120  */
121
122 /*
123  * Global opts, taken by all filesystems
124  */
125 static const char *global_opts[] = {
126         "errmsg",
127         "fstype",
128         "fspath",
129         "ro",
130         "rw",
131         "nosuid",
132         "noexec",
133         NULL
134 };
135
136 /*
137  * The root specifiers we will try if RB_CDROM is specified.
138  */
139 static char *cdrom_rootdevnames[] = {
140         "cd9660:cd0",
141         "cd9660:acd0",
142         NULL
143 };
144
145 /* legacy find-root code */
146 char            *rootdevnames[2] = {NULL, NULL};
147 #ifndef ROOTDEVNAME
148 #  define ROOTDEVNAME NULL
149 #endif
150 static const char       *ctrootdevname = ROOTDEVNAME;
151
152 /*
153  * ---------------------------------------------------------------------
154  * Functions for building and sanitizing the mount options
155  */
156
157 /* Remove one mount option. */
158 static void
159 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
160 {
161
162         TAILQ_REMOVE(opts, opt, link);
163         free(opt->name, M_MOUNT);
164         if (opt->value != NULL)
165                 free(opt->value, M_MOUNT);
166         free(opt, M_MOUNT);
167 }
168
169 /* Release all resources related to the mount options. */
170 void
171 vfs_freeopts(struct vfsoptlist *opts)
172 {
173         struct vfsopt *opt;
174
175         while (!TAILQ_EMPTY(opts)) {
176                 opt = TAILQ_FIRST(opts);
177                 vfs_freeopt(opts, opt);
178         }
179         free(opts, M_MOUNT);
180 }
181
182 void
183 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
184 {
185         struct vfsopt *opt, *temp;
186
187         if (opts == NULL)
188                 return;
189         TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
190                 if (strcmp(opt->name, name) == 0)
191                         vfs_freeopt(opts, opt);
192         }
193 }
194
195 static int
196 vfs_isopt_ro(const char *opt)
197 {
198
199         if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
200             strcmp(opt, "norw") == 0)
201                 return (1);
202         return (0);
203 }
204
205 static int
206 vfs_isopt_rw(const char *opt)
207 {
208
209         if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
210                 return (1);
211         return (0);
212 }
213
214 /*
215  * Check if options are equal (with or without the "no" prefix).
216  */
217 static int
218 vfs_equalopts(const char *opt1, const char *opt2)
219 {
220         char *p;
221
222         /* "opt" vs. "opt" or "noopt" vs. "noopt" */
223         if (strcmp(opt1, opt2) == 0)
224                 return (1);
225         /* "noopt" vs. "opt" */
226         if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
227                 return (1);
228         /* "opt" vs. "noopt" */
229         if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
230                 return (1);
231         while ((p = strchr(opt1, '.')) != NULL &&
232             !strncmp(opt1, opt2, ++p - opt1)) {
233                 opt2 += p - opt1;
234                 opt1 = p;
235                 /* "foo.noopt" vs. "foo.opt" */
236                 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
237                         return (1);
238                 /* "foo.opt" vs. "foo.noopt" */
239                 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
240                         return (1);
241         }
242         /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
243         if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
244             (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
245                 return (1);
246         return (0);
247 }
248
249 /*
250  * If a mount option is specified several times,
251  * (with or without the "no" prefix) only keep
252  * the last occurence of it.
253  */
254 static void
255 vfs_sanitizeopts(struct vfsoptlist *opts)
256 {
257         struct vfsopt *opt, *opt2, *tmp;
258
259         TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
260                 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
261                 while (opt2 != NULL) {
262                         if (vfs_equalopts(opt->name, opt2->name)) {
263                                 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
264                                 vfs_freeopt(opts, opt2);
265                                 opt2 = tmp;
266                         } else {
267                                 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
268                         }
269                 }
270         }
271 }
272
273 /*
274  * Build a linked list of mount options from a struct uio.
275  */
276 int
277 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
278 {
279         struct vfsoptlist *opts;
280         struct vfsopt *opt;
281         size_t memused, namelen, optlen;
282         unsigned int i, iovcnt;
283         int error;
284
285         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
286         TAILQ_INIT(opts);
287         memused = 0;
288         iovcnt = auio->uio_iovcnt;
289         for (i = 0; i < iovcnt; i += 2) {
290                 namelen = auio->uio_iov[i].iov_len;
291                 optlen = auio->uio_iov[i + 1].iov_len;
292                 memused += sizeof(struct vfsopt) + optlen + namelen;
293                 /*
294                  * Avoid consuming too much memory, and attempts to overflow
295                  * memused.
296                  */
297                 if (memused > VFS_MOUNTARG_SIZE_MAX ||
298                     optlen > VFS_MOUNTARG_SIZE_MAX ||
299                     namelen > VFS_MOUNTARG_SIZE_MAX) {
300                         error = EINVAL;
301                         goto bad;
302                 }
303
304                 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
305                 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
306                 opt->value = NULL;
307                 opt->len = 0;
308                 opt->pos = i / 2;
309                 opt->seen = 0;
310
311                 /*
312                  * Do this early, so jumps to "bad" will free the current
313                  * option.
314                  */
315                 TAILQ_INSERT_TAIL(opts, opt, link);
316
317                 if (auio->uio_segflg == UIO_SYSSPACE) {
318                         bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
319                 } else {
320                         error = copyin(auio->uio_iov[i].iov_base, opt->name,
321                             namelen);
322                         if (error)
323                                 goto bad;
324                 }
325                 /* Ensure names are null-terminated strings. */
326                 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
327                         error = EINVAL;
328                         goto bad;
329                 }
330                 if (optlen != 0) {
331                         opt->len = optlen;
332                         opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
333                         if (auio->uio_segflg == UIO_SYSSPACE) {
334                                 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
335                                     optlen);
336                         } else {
337                                 error = copyin(auio->uio_iov[i + 1].iov_base,
338                                     opt->value, optlen);
339                                 if (error)
340                                         goto bad;
341                         }
342                 }
343         }
344         vfs_sanitizeopts(opts);
345         *options = opts;
346         return (0);
347 bad:
348         vfs_freeopts(opts);
349         return (error);
350 }
351
352 /*
353  * Merge the old mount options with the new ones passed
354  * in the MNT_UPDATE case.
355  *
356  * XXX This function will keep a "nofoo" option in the
357  *     new options if there is no matching "foo" option
358  *     to be cancelled in the old options.  This is a bug
359  *     if the option's canonical name is "foo".  E.g., "noro"
360  *     shouldn't end up in the mount point's active options,
361  *     but it can.
362  */
363 static void
364 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
365 {
366         struct vfsopt *opt, *opt2, *new;
367
368         TAILQ_FOREACH(opt, opts, link) {
369                 /*
370                  * Check that this option hasn't been redefined
371                  * nor cancelled with a "no" mount option.
372                  */
373                 opt2 = TAILQ_FIRST(toopts);
374                 while (opt2 != NULL) {
375                         if (strcmp(opt2->name, opt->name) == 0)
376                                 goto next;
377                         if (strncmp(opt2->name, "no", 2) == 0 &&
378                             strcmp(opt2->name + 2, opt->name) == 0) {
379                                 vfs_freeopt(toopts, opt2);
380                                 goto next;
381                         }
382                         opt2 = TAILQ_NEXT(opt2, link);
383                 }
384                 /* We want this option, duplicate it. */
385                 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
386                 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
387                 strcpy(new->name, opt->name);
388                 if (opt->len != 0) {
389                         new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
390                         bcopy(opt->value, new->value, opt->len);
391                 } else {
392                         new->value = NULL;
393                 }
394                 new->len = opt->len;
395                 new->seen = opt->seen;
396                 TAILQ_INSERT_TAIL(toopts, new, link);
397 next:
398                 continue;
399         }
400 }
401
402 /*
403  * Mount a filesystem.
404  */
405 int
406 nmount(td, uap)
407         struct thread *td;
408         struct nmount_args /* {
409                 struct iovec *iovp;
410                 unsigned int iovcnt;
411                 int flags;
412         } */ *uap;
413 {
414         struct uio *auio;
415         int error;
416         u_int iovcnt;
417
418         AUDIT_ARG_FFLAGS(uap->flags);
419         CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
420             uap->iovp, uap->iovcnt, uap->flags);
421
422         /*
423          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
424          * userspace to set this flag, but we must filter it out if we want
425          * MNT_UPDATE on the root file system to work.
426          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
427          */
428         uap->flags &= ~MNT_ROOTFS;
429
430         iovcnt = uap->iovcnt;
431         /*
432          * Check that we have an even number of iovec's
433          * and that we have at least two options.
434          */
435         if ((iovcnt & 1) || (iovcnt < 4)) {
436                 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
437                     uap->iovcnt);
438                 return (EINVAL);
439         }
440
441         error = copyinuio(uap->iovp, iovcnt, &auio);
442         if (error) {
443                 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
444                     __func__, error);
445                 return (error);
446         }
447         error = vfs_donmount(td, uap->flags, auio);
448
449         free(auio, M_IOV);
450         return (error);
451 }
452
453 /*
454  * ---------------------------------------------------------------------
455  * Various utility functions
456  */
457
458 void
459 vfs_ref(struct mount *mp)
460 {
461
462         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
463         MNT_ILOCK(mp);
464         MNT_REF(mp);
465         MNT_IUNLOCK(mp);
466 }
467
468 void
469 vfs_rel(struct mount *mp)
470 {
471
472         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
473         MNT_ILOCK(mp);
474         MNT_REL(mp);
475         MNT_IUNLOCK(mp);
476 }
477
478 static int
479 mount_init(void *mem, int size, int flags)
480 {
481         struct mount *mp;
482
483         mp = (struct mount *)mem;
484         mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
485         lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
486         return (0);
487 }
488
489 static void
490 mount_fini(void *mem, int size)
491 {
492         struct mount *mp;
493
494         mp = (struct mount *)mem;
495         lockdestroy(&mp->mnt_explock);
496         mtx_destroy(&mp->mnt_mtx);
497 }
498
499 /*
500  * Allocate and initialize the mount point struct.
501  */
502 struct mount *
503 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
504     struct ucred *cred)
505 {
506         struct mount *mp;
507
508         mp = uma_zalloc(mount_zone, M_WAITOK);
509         bzero(&mp->mnt_startzero,
510             __rangeof(struct mount, mnt_startzero, mnt_endzero));
511         TAILQ_INIT(&mp->mnt_nvnodelist);
512         mp->mnt_nvnodelistsize = 0;
513         mp->mnt_ref = 0;
514         (void) vfs_busy(mp, MBF_NOWAIT);
515         mp->mnt_op = vfsp->vfc_vfsops;
516         mp->mnt_vfc = vfsp;
517         vfsp->vfc_refcount++;   /* XXX Unlocked */
518         mp->mnt_stat.f_type = vfsp->vfc_typenum;
519         mp->mnt_gen++;
520         strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
521         mp->mnt_vnodecovered = vp;
522         mp->mnt_cred = crdup(cred);
523         mp->mnt_stat.f_owner = cred->cr_uid;
524         strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
525         mp->mnt_iosize_max = DFLTPHYS;
526 #ifdef MAC
527         mac_mount_init(mp);
528         mac_mount_create(cred, mp);
529 #endif
530         arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
531         return (mp);
532 }
533
534 /*
535  * Destroy the mount struct previously allocated by vfs_mount_alloc().
536  */
537 void
538 vfs_mount_destroy(struct mount *mp)
539 {
540
541         MNT_ILOCK(mp);
542         mp->mnt_kern_flag |= MNTK_REFEXPIRE;
543         if (mp->mnt_kern_flag & MNTK_MWAIT) {
544                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
545                 wakeup(mp);
546         }
547         while (mp->mnt_ref)
548                 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
549         KASSERT(mp->mnt_ref == 0,
550             ("%s: invalid refcount in the drain path @ %s:%d", __func__,
551             __FILE__, __LINE__));
552         if (mp->mnt_writeopcount != 0)
553                 panic("vfs_mount_destroy: nonzero writeopcount");
554         if (mp->mnt_secondary_writes != 0)
555                 panic("vfs_mount_destroy: nonzero secondary_writes");
556         mp->mnt_vfc->vfc_refcount--;
557         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
558                 struct vnode *vp;
559
560                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
561                         vprint("", vp);
562                 panic("unmount: dangling vnode");
563         }
564         if (mp->mnt_nvnodelistsize != 0)
565                 panic("vfs_mount_destroy: nonzero nvnodelistsize");
566         if (mp->mnt_lockref != 0)
567                 panic("vfs_mount_destroy: nonzero lock refcount");
568         MNT_IUNLOCK(mp);
569 #ifdef MAC
570         mac_mount_destroy(mp);
571 #endif
572         if (mp->mnt_opt != NULL)
573                 vfs_freeopts(mp->mnt_opt);
574         crfree(mp->mnt_cred);
575         uma_zfree(mount_zone, mp);
576 }
577
578 int
579 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
580 {
581         struct vfsoptlist *optlist;
582         struct vfsopt *opt, *noro_opt, *tmp_opt;
583         char *fstype, *fspath, *errmsg;
584         int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
585         int has_rw, has_noro;
586
587         errmsg = fspath = NULL;
588         errmsg_len = has_noro = has_rw = fspathlen = 0;
589         errmsg_pos = -1;
590
591         error = vfs_buildopts(fsoptions, &optlist);
592         if (error)
593                 return (error);
594
595         if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
596                 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
597
598         /*
599          * We need these two options before the others,
600          * and they are mandatory for any filesystem.
601          * Ensure they are NUL terminated as well.
602          */
603         fstypelen = 0;
604         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
605         if (error || fstype[fstypelen - 1] != '\0') {
606                 error = EINVAL;
607                 if (errmsg != NULL)
608                         strncpy(errmsg, "Invalid fstype", errmsg_len);
609                 goto bail;
610         }
611         fspathlen = 0;
612         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
613         if (error || fspath[fspathlen - 1] != '\0') {
614                 error = EINVAL;
615                 if (errmsg != NULL)
616                         strncpy(errmsg, "Invalid fspath", errmsg_len);
617                 goto bail;
618         }
619
620         /*
621          * We need to see if we have the "update" option
622          * before we call vfs_domount(), since vfs_domount() has special
623          * logic based on MNT_UPDATE.  This is very important
624          * when we want to update the root filesystem.
625          */
626         TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
627                 if (strcmp(opt->name, "update") == 0) {
628                         fsflags |= MNT_UPDATE;
629                         vfs_freeopt(optlist, opt);
630                 }
631                 else if (strcmp(opt->name, "async") == 0)
632                         fsflags |= MNT_ASYNC;
633                 else if (strcmp(opt->name, "force") == 0) {
634                         fsflags |= MNT_FORCE;
635                         vfs_freeopt(optlist, opt);
636                 }
637                 else if (strcmp(opt->name, "reload") == 0) {
638                         fsflags |= MNT_RELOAD;
639                         vfs_freeopt(optlist, opt);
640                 }
641                 else if (strcmp(opt->name, "multilabel") == 0)
642                         fsflags |= MNT_MULTILABEL;
643                 else if (strcmp(opt->name, "noasync") == 0)
644                         fsflags &= ~MNT_ASYNC;
645                 else if (strcmp(opt->name, "noatime") == 0)
646                         fsflags |= MNT_NOATIME;
647                 else if (strcmp(opt->name, "atime") == 0) {
648                         free(opt->name, M_MOUNT);
649                         opt->name = strdup("nonoatime", M_MOUNT);
650                 }
651                 else if (strcmp(opt->name, "noclusterr") == 0)
652                         fsflags |= MNT_NOCLUSTERR;
653                 else if (strcmp(opt->name, "clusterr") == 0) {
654                         free(opt->name, M_MOUNT);
655                         opt->name = strdup("nonoclusterr", M_MOUNT);
656                 }
657                 else if (strcmp(opt->name, "noclusterw") == 0)
658                         fsflags |= MNT_NOCLUSTERW;
659                 else if (strcmp(opt->name, "clusterw") == 0) {
660                         free(opt->name, M_MOUNT);
661                         opt->name = strdup("nonoclusterw", M_MOUNT);
662                 }
663                 else if (strcmp(opt->name, "noexec") == 0)
664                         fsflags |= MNT_NOEXEC;
665                 else if (strcmp(opt->name, "exec") == 0) {
666                         free(opt->name, M_MOUNT);
667                         opt->name = strdup("nonoexec", M_MOUNT);
668                 }
669                 else if (strcmp(opt->name, "nosuid") == 0)
670                         fsflags |= MNT_NOSUID;
671                 else if (strcmp(opt->name, "suid") == 0) {
672                         free(opt->name, M_MOUNT);
673                         opt->name = strdup("nonosuid", M_MOUNT);
674                 }
675                 else if (strcmp(opt->name, "nosymfollow") == 0)
676                         fsflags |= MNT_NOSYMFOLLOW;
677                 else if (strcmp(opt->name, "symfollow") == 0) {
678                         free(opt->name, M_MOUNT);
679                         opt->name = strdup("nonosymfollow", M_MOUNT);
680                 }
681                 else if (strcmp(opt->name, "noro") == 0) {
682                         fsflags &= ~MNT_RDONLY;
683                         has_noro = 1;
684                 }
685                 else if (strcmp(opt->name, "rw") == 0) {
686                         fsflags &= ~MNT_RDONLY;
687                         has_rw = 1;
688                 }
689                 else if (strcmp(opt->name, "ro") == 0)
690                         fsflags |= MNT_RDONLY;
691                 else if (strcmp(opt->name, "rdonly") == 0) {
692                         free(opt->name, M_MOUNT);
693                         opt->name = strdup("ro", M_MOUNT);
694                         fsflags |= MNT_RDONLY;
695                 }
696                 else if (strcmp(opt->name, "suiddir") == 0)
697                         fsflags |= MNT_SUIDDIR;
698                 else if (strcmp(opt->name, "sync") == 0)
699                         fsflags |= MNT_SYNCHRONOUS;
700                 else if (strcmp(opt->name, "union") == 0)
701                         fsflags |= MNT_UNION;
702         }
703
704         /*
705          * If "rw" was specified as a mount option, and we
706          * are trying to update a mount-point from "ro" to "rw",
707          * we need a mount option "noro", since in vfs_mergeopts(),
708          * "noro" will cancel "ro", but "rw" will not do anything.
709          */
710         if (has_rw && !has_noro) {
711                 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
712                 noro_opt->name = strdup("noro", M_MOUNT);
713                 noro_opt->value = NULL;
714                 noro_opt->len = 0;
715                 noro_opt->pos = -1;
716                 noro_opt->seen = 1;
717                 TAILQ_INSERT_TAIL(optlist, noro_opt, link);
718         }
719
720         /*
721          * Be ultra-paranoid about making sure the type and fspath
722          * variables will fit in our mp buffers, including the
723          * terminating NUL.
724          */
725         if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
726                 error = ENAMETOOLONG;
727                 goto bail;
728         }
729
730         mtx_lock(&Giant);
731         error = vfs_domount(td, fstype, fspath, fsflags, optlist);
732         mtx_unlock(&Giant);
733 bail:
734         /* copyout the errmsg */
735         if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
736             && errmsg_len > 0 && errmsg != NULL) {
737                 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
738                         bcopy(errmsg,
739                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
740                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
741                 } else {
742                         copyout(errmsg,
743                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
744                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
745                 }
746         }
747
748         if (error != 0)
749                 vfs_freeopts(optlist);
750         return (error);
751 }
752
753 /*
754  * Old mount API.
755  */
756 #ifndef _SYS_SYSPROTO_H_
757 struct mount_args {
758         char    *type;
759         char    *path;
760         int     flags;
761         caddr_t data;
762 };
763 #endif
764 /* ARGSUSED */
765 int
766 mount(td, uap)
767         struct thread *td;
768         struct mount_args /* {
769                 char *type;
770                 char *path;
771                 int flags;
772                 caddr_t data;
773         } */ *uap;
774 {
775         char *fstype;
776         struct vfsconf *vfsp = NULL;
777         struct mntarg *ma = NULL;
778         int error;
779
780         AUDIT_ARG_FFLAGS(uap->flags);
781
782         /*
783          * Filter out MNT_ROOTFS.  We do not want clients of mount() in
784          * userspace to set this flag, but we must filter it out if we want
785          * MNT_UPDATE on the root file system to work.
786          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
787          */
788         uap->flags &= ~MNT_ROOTFS;
789
790         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
791         error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
792         if (error) {
793                 free(fstype, M_TEMP);
794                 return (error);
795         }
796
797         AUDIT_ARG_TEXT(fstype);
798         mtx_lock(&Giant);
799         vfsp = vfs_byname_kld(fstype, td, &error);
800         free(fstype, M_TEMP);
801         if (vfsp == NULL) {
802                 mtx_unlock(&Giant);
803                 return (ENOENT);
804         }
805         if (vfsp->vfc_vfsops->vfs_cmount == NULL) {
806                 mtx_unlock(&Giant);
807                 return (EOPNOTSUPP);
808         }
809
810         ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
811         ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
812         ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
813         ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
814         ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
815
816         error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags);
817         mtx_unlock(&Giant);
818         return (error);
819 }
820
821
822 /*
823  * vfs_domount(): actually attempt a filesystem mount.
824  */
825 static int
826 vfs_domount(
827         struct thread *td,      /* Calling thread. */
828         const char *fstype,     /* Filesystem type. */
829         char *fspath,           /* Mount path. */
830         int fsflags,            /* Flags common to all filesystems. */
831         void *fsdata            /* Options local to the filesystem. */
832         )
833 {
834         struct vnode *vp;
835         struct mount *mp;
836         struct vfsconf *vfsp;
837         struct oexport_args oexport;
838         struct export_args export;
839         int error, flag = 0;
840         struct vattr va;
841         struct nameidata nd;
842
843         mtx_assert(&Giant, MA_OWNED);
844         /*
845          * Be ultra-paranoid about making sure the type and fspath
846          * variables will fit in our mp buffers, including the
847          * terminating NUL.
848          */
849         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
850                 return (ENAMETOOLONG);
851
852         if (jailed(td->td_ucred) || usermount == 0) {
853                 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
854                         return (error);
855         }
856
857         /*
858          * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
859          */
860         if (fsflags & MNT_EXPORTED) {
861                 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
862                 if (error)
863                         return (error);
864         }
865         if (fsflags & MNT_SUIDDIR) {
866                 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
867                 if (error)
868                         return (error);
869         }
870         /*
871          * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
872          */
873         if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
874                 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
875                         fsflags |= MNT_NOSUID | MNT_USER;
876         }
877
878         /* Load KLDs before we lock the covered vnode to avoid reversals. */
879         vfsp = NULL;
880         if ((fsflags & MNT_UPDATE) == 0) {
881                 /* Don't try to load KLDs if we're mounting the root. */
882                 if (fsflags & MNT_ROOTFS)
883                         vfsp = vfs_byname(fstype);
884                 else
885                         vfsp = vfs_byname_kld(fstype, td, &error);
886                 if (vfsp == NULL)
887                         return (ENODEV);
888                 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL))
889                         return (EPERM);
890         }
891         /*
892          * Get vnode to be covered
893          */
894         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE,
895             fspath, td);
896         if ((error = namei(&nd)) != 0)
897                 return (error);
898         NDFREE(&nd, NDF_ONLY_PNBUF);
899         vp = nd.ni_vp;
900         if (fsflags & MNT_UPDATE) {
901                 if ((vp->v_vflag & VV_ROOT) == 0) {
902                         vput(vp);
903                         return (EINVAL);
904                 }
905                 mp = vp->v_mount;
906                 MNT_ILOCK(mp);
907                 flag = mp->mnt_flag;
908                 /*
909                  * We only allow the filesystem to be reloaded if it
910                  * is currently mounted read-only.
911                  */
912                 if ((fsflags & MNT_RELOAD) &&
913                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
914                         MNT_IUNLOCK(mp);
915                         vput(vp);
916                         return (EOPNOTSUPP);    /* Needs translation */
917                 }
918                 MNT_IUNLOCK(mp);
919                 /*
920                  * Only privileged root, or (if MNT_USER is set) the user that
921                  * did the original mount is permitted to update it.
922                  */
923                 error = vfs_suser(mp, td);
924                 if (error) {
925                         vput(vp);
926                         return (error);
927                 }
928                 if (vfs_busy(mp, MBF_NOWAIT)) {
929                         vput(vp);
930                         return (EBUSY);
931                 }
932                 VI_LOCK(vp);
933                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
934                     vp->v_mountedhere != NULL) {
935                         VI_UNLOCK(vp);
936                         vfs_unbusy(mp);
937                         vput(vp);
938                         return (EBUSY);
939                 }
940                 vp->v_iflag |= VI_MOUNT;
941                 VI_UNLOCK(vp);
942                 MNT_ILOCK(mp);
943                 mp->mnt_flag |= fsflags &
944                     (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
945                 MNT_IUNLOCK(mp);
946                 VOP_UNLOCK(vp, 0);
947                 mp->mnt_optnew = fsdata;
948                 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
949         } else {
950                 /*
951                  * If the user is not root, ensure that they own the directory
952                  * onto which we are attempting to mount.
953                  */
954                 error = VOP_GETATTR(vp, &va, td->td_ucred);
955                 if (error) {
956                         vput(vp);
957                         return (error);
958                 }
959                 if (va.va_uid != td->td_ucred->cr_uid) {
960                         error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN,
961                             0);
962                         if (error) {
963                                 vput(vp);
964                                 return (error);
965                         }
966                 }
967                 error = vinvalbuf(vp, V_SAVE, 0, 0);
968                 if (error != 0) {
969                         vput(vp);
970                         return (error);
971                 }
972                 if (vp->v_type != VDIR) {
973                         vput(vp);
974                         return (ENOTDIR);
975                 }
976                 VI_LOCK(vp);
977                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
978                     vp->v_mountedhere != NULL) {
979                         VI_UNLOCK(vp);
980                         vput(vp);
981                         return (EBUSY);
982                 }
983                 vp->v_iflag |= VI_MOUNT;
984                 VI_UNLOCK(vp);
985
986                 /*
987                  * Allocate and initialize the filesystem.
988                  */
989                 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
990                 VOP_UNLOCK(vp, 0);
991
992                 /* XXXMAC: pass to vfs_mount_alloc? */
993                 mp->mnt_optnew = fsdata;
994         }
995
996         /*
997          * Set the mount level flags.
998          */
999         MNT_ILOCK(mp);
1000         mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) |
1001                 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS |
1002                             MNT_RDONLY));
1003         if ((mp->mnt_flag & MNT_ASYNC) == 0)
1004                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1005         MNT_IUNLOCK(mp);
1006         /*
1007          * Mount the filesystem.
1008          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1009          * get.  No freeing of cn_pnbuf.
1010          */
1011         error = VFS_MOUNT(mp);
1012
1013         /*
1014          * Process the export option only if we are
1015          * updating mount options.
1016          */
1017         if (!error && (fsflags & MNT_UPDATE)) {
1018                 if (vfs_copyopt(mp->mnt_optnew, "export", &export,
1019                     sizeof(export)) == 0)
1020                         error = vfs_export(mp, &export);
1021                 else if (vfs_copyopt(mp->mnt_optnew, "export", &oexport,
1022                         sizeof(oexport)) == 0) {
1023                         export.ex_flags = oexport.ex_flags;
1024                         export.ex_root = oexport.ex_root;
1025                         export.ex_anon = oexport.ex_anon;
1026                         export.ex_addr = oexport.ex_addr;
1027                         export.ex_addrlen = oexport.ex_addrlen;
1028                         export.ex_mask = oexport.ex_mask;
1029                         export.ex_masklen = oexport.ex_masklen;
1030                         export.ex_indexfile = oexport.ex_indexfile;
1031                         export.ex_numsecflavors = 0;
1032                         error = vfs_export(mp, &export);
1033                 }
1034         }
1035
1036         if (!error) {
1037                 if (mp->mnt_opt != NULL)
1038                         vfs_freeopts(mp->mnt_opt);
1039                 mp->mnt_opt = mp->mnt_optnew;
1040                 (void)VFS_STATFS(mp, &mp->mnt_stat);
1041         }
1042         /*
1043          * Prevent external consumers of mount options from reading
1044          * mnt_optnew.
1045         */
1046         mp->mnt_optnew = NULL;
1047         if (mp->mnt_flag & MNT_UPDATE) {
1048                 MNT_ILOCK(mp);
1049                 if (error)
1050                         mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) |
1051                                 (flag & ~MNT_QUOTA);
1052                 else
1053                         mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD |
1054                                           MNT_FORCE | MNT_SNAPSHOT);
1055                 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1056                         mp->mnt_kern_flag |= MNTK_ASYNC;
1057                 else
1058                         mp->mnt_kern_flag &= ~MNTK_ASYNC;
1059                 MNT_IUNLOCK(mp);
1060                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1061                         if (mp->mnt_syncer == NULL)
1062                                 error = vfs_allocate_syncvnode(mp);
1063                 } else {
1064                         if (mp->mnt_syncer != NULL)
1065                                 vrele(mp->mnt_syncer);
1066                         mp->mnt_syncer = NULL;
1067                 }
1068                 vfs_unbusy(mp);
1069                 VI_LOCK(vp);
1070                 vp->v_iflag &= ~VI_MOUNT;
1071                 VI_UNLOCK(vp);
1072                 vrele(vp);
1073                 return (error);
1074         }
1075         MNT_ILOCK(mp);
1076         if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1077                 mp->mnt_kern_flag |= MNTK_ASYNC;
1078         else
1079                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1080         MNT_IUNLOCK(mp);
1081         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1082         /*
1083          * Put the new filesystem on the mount list after root.
1084          */
1085         cache_purge(vp);
1086         if (!error) {
1087                 struct vnode *newdp;
1088
1089                 VI_LOCK(vp);
1090                 vp->v_iflag &= ~VI_MOUNT;
1091                 VI_UNLOCK(vp);
1092                 vp->v_mountedhere = mp;
1093                 mtx_lock(&mountlist_mtx);
1094                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1095                 mtx_unlock(&mountlist_mtx);
1096                 vfs_event_signal(NULL, VQ_MOUNT, 0);
1097                 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp))
1098                         panic("mount: lost mount");
1099                 VOP_UNLOCK(newdp, 0);
1100                 VOP_UNLOCK(vp, 0);
1101                 mountcheckdirs(vp, newdp);
1102                 vrele(newdp);
1103                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
1104                         error = vfs_allocate_syncvnode(mp);
1105                 vfs_unbusy(mp);
1106                 if (error)
1107                         vrele(vp);
1108         } else {
1109                 VI_LOCK(vp);
1110                 vp->v_iflag &= ~VI_MOUNT;
1111                 VI_UNLOCK(vp);
1112                 vfs_unbusy(mp);
1113                 vfs_mount_destroy(mp);
1114                 vput(vp);
1115         }
1116         return (error);
1117 }
1118
1119 /*
1120  * Unmount a filesystem.
1121  *
1122  * Note: unmount takes a path to the vnode mounted on as argument, not
1123  * special file (as before).
1124  */
1125 #ifndef _SYS_SYSPROTO_H_
1126 struct unmount_args {
1127         char    *path;
1128         int     flags;
1129 };
1130 #endif
1131 /* ARGSUSED */
1132 int
1133 unmount(td, uap)
1134         struct thread *td;
1135         register struct unmount_args /* {
1136                 char *path;
1137                 int flags;
1138         } */ *uap;
1139 {
1140         struct mount *mp;
1141         char *pathbuf;
1142         int error, id0, id1;
1143
1144         AUDIT_ARG_VALUE(uap->flags);
1145         if (jailed(td->td_ucred) || usermount == 0) {
1146                 error = priv_check(td, PRIV_VFS_UNMOUNT);
1147                 if (error)
1148                         return (error);
1149         }
1150
1151         pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1152         error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
1153         if (error) {
1154                 free(pathbuf, M_TEMP);
1155                 return (error);
1156         }
1157         mtx_lock(&Giant);
1158         if (uap->flags & MNT_BYFSID) {
1159                 AUDIT_ARG_TEXT(pathbuf);
1160                 /* Decode the filesystem ID. */
1161                 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1162                         mtx_unlock(&Giant);
1163                         free(pathbuf, M_TEMP);
1164                         return (EINVAL);
1165                 }
1166
1167                 mtx_lock(&mountlist_mtx);
1168                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1169                         if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1170                             mp->mnt_stat.f_fsid.val[1] == id1)
1171                                 break;
1172                 }
1173                 mtx_unlock(&mountlist_mtx);
1174         } else {
1175                 AUDIT_ARG_UPATH1(td, pathbuf);
1176                 mtx_lock(&mountlist_mtx);
1177                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1178                         if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1179                                 break;
1180                 }
1181                 mtx_unlock(&mountlist_mtx);
1182         }
1183         free(pathbuf, M_TEMP);
1184         if (mp == NULL) {
1185                 /*
1186                  * Previously we returned ENOENT for a nonexistent path and
1187                  * EINVAL for a non-mountpoint.  We cannot tell these apart
1188                  * now, so in the !MNT_BYFSID case return the more likely
1189                  * EINVAL for compatibility.
1190                  */
1191                 mtx_unlock(&Giant);
1192                 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1193         }
1194
1195         /*
1196          * Don't allow unmounting the root filesystem.
1197          */
1198         if (mp->mnt_flag & MNT_ROOTFS) {
1199                 mtx_unlock(&Giant);
1200                 return (EINVAL);
1201         }
1202         error = dounmount(mp, uap->flags, td);
1203         mtx_unlock(&Giant);
1204         return (error);
1205 }
1206
1207 /*
1208  * Do the actual filesystem unmount.
1209  */
1210 int
1211 dounmount(mp, flags, td)
1212         struct mount *mp;
1213         int flags;
1214         struct thread *td;
1215 {
1216         struct vnode *coveredvp, *fsrootvp;
1217         int error;
1218         int async_flag;
1219         int mnt_gen_r;
1220
1221         mtx_assert(&Giant, MA_OWNED);
1222
1223         if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1224                 mnt_gen_r = mp->mnt_gen;
1225                 VI_LOCK(coveredvp);
1226                 vholdl(coveredvp);
1227                 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1228                 vdrop(coveredvp);
1229                 /*
1230                  * Check for mp being unmounted while waiting for the
1231                  * covered vnode lock.
1232                  */
1233                 if (coveredvp->v_mountedhere != mp ||
1234                     coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1235                         VOP_UNLOCK(coveredvp, 0);
1236                         return (EBUSY);
1237                 }
1238         }
1239         /*
1240          * Only privileged root, or (if MNT_USER is set) the user that did the
1241          * original mount is permitted to unmount this filesystem.
1242          */
1243         error = vfs_suser(mp, td);
1244         if (error) {
1245                 if (coveredvp)
1246                         VOP_UNLOCK(coveredvp, 0);
1247                 return (error);
1248         }
1249
1250         MNT_ILOCK(mp);
1251         if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1252                 MNT_IUNLOCK(mp);
1253                 if (coveredvp)
1254                         VOP_UNLOCK(coveredvp, 0);
1255                 return (EBUSY);
1256         }
1257         mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_NOINSMNTQ;
1258         /* Allow filesystems to detect that a forced unmount is in progress. */
1259         if (flags & MNT_FORCE)
1260                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1261         error = 0;
1262         if (mp->mnt_lockref) {
1263                 if ((flags & MNT_FORCE) == 0) {
1264                         mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_NOINSMNTQ |
1265                             MNTK_UNMOUNTF);
1266                         if (mp->mnt_kern_flag & MNTK_MWAIT) {
1267                                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1268                                 wakeup(mp);
1269                         }
1270                         MNT_IUNLOCK(mp);
1271                         if (coveredvp)
1272                                 VOP_UNLOCK(coveredvp, 0);
1273                         return (EBUSY);
1274                 }
1275                 mp->mnt_kern_flag |= MNTK_DRAINING;
1276                 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1277                     "mount drain", 0);
1278         }
1279         MNT_IUNLOCK(mp);
1280         KASSERT(mp->mnt_lockref == 0,
1281             ("%s: invalid lock refcount in the drain path @ %s:%d",
1282             __func__, __FILE__, __LINE__));
1283         KASSERT(error == 0,
1284             ("%s: invalid return value for msleep in the drain path @ %s:%d",
1285             __func__, __FILE__, __LINE__));
1286         vn_start_write(NULL, &mp, V_WAIT);
1287
1288         if (mp->mnt_flag & MNT_EXPUBLIC)
1289                 vfs_setpublicfs(NULL, NULL, NULL);
1290
1291         vfs_msync(mp, MNT_WAIT);
1292         MNT_ILOCK(mp);
1293         async_flag = mp->mnt_flag & MNT_ASYNC;
1294         mp->mnt_flag &= ~MNT_ASYNC;
1295         mp->mnt_kern_flag &= ~MNTK_ASYNC;
1296         MNT_IUNLOCK(mp);
1297         cache_purgevfs(mp);     /* remove cache entries for this file sys */
1298         if (mp->mnt_syncer != NULL)
1299                 vrele(mp->mnt_syncer);
1300         /*
1301          * For forced unmounts, move process cdir/rdir refs on the fs root
1302          * vnode to the covered vnode.  For non-forced unmounts we want
1303          * such references to cause an EBUSY error.
1304          */
1305         if ((flags & MNT_FORCE) &&
1306             VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1307                 if (mp->mnt_vnodecovered != NULL)
1308                         mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1309                 if (fsrootvp == rootvnode) {
1310                         vrele(rootvnode);
1311                         rootvnode = NULL;
1312                 }
1313                 vput(fsrootvp);
1314         }
1315         if (((mp->mnt_flag & MNT_RDONLY) ||
1316              (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || (flags & MNT_FORCE) != 0)
1317                 error = VFS_UNMOUNT(mp, flags);
1318         vn_finished_write(mp);
1319         /*
1320          * If we failed to flush the dirty blocks for this mount point,
1321          * undo all the cdir/rdir and rootvnode changes we made above.
1322          * Unless we failed to do so because the device is reporting that
1323          * it doesn't exist anymore.
1324          */
1325         if (error && error != ENXIO) {
1326                 if ((flags & MNT_FORCE) &&
1327                     VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp) == 0) {
1328                         if (mp->mnt_vnodecovered != NULL)
1329                                 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1330                         if (rootvnode == NULL) {
1331                                 rootvnode = fsrootvp;
1332                                 vref(rootvnode);
1333                         }
1334                         vput(fsrootvp);
1335                 }
1336                 MNT_ILOCK(mp);
1337                 mp->mnt_kern_flag &= ~MNTK_NOINSMNTQ;
1338                 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) {
1339                         MNT_IUNLOCK(mp);
1340                         (void) vfs_allocate_syncvnode(mp);
1341                         MNT_ILOCK(mp);
1342                 }
1343                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1344                 mp->mnt_flag |= async_flag;
1345                 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
1346                         mp->mnt_kern_flag |= MNTK_ASYNC;
1347                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1348                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
1349                         wakeup(mp);
1350                 }
1351                 MNT_IUNLOCK(mp);
1352                 if (coveredvp)
1353                         VOP_UNLOCK(coveredvp, 0);
1354                 return (error);
1355         }
1356         mtx_lock(&mountlist_mtx);
1357         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1358         mtx_unlock(&mountlist_mtx);
1359         if (coveredvp != NULL) {
1360                 coveredvp->v_mountedhere = NULL;
1361                 vput(coveredvp);
1362         }
1363         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1364         vfs_mount_destroy(mp);
1365         return (0);
1366 }
1367
1368 /*
1369  * ---------------------------------------------------------------------
1370  * Mounting of root filesystem
1371  *
1372  */
1373
1374 struct root_hold_token {
1375         const char                      *who;
1376         LIST_ENTRY(root_hold_token)     list;
1377 };
1378
1379 static LIST_HEAD(, root_hold_token)     root_holds =
1380     LIST_HEAD_INITIALIZER(root_holds);
1381
1382 static int root_mount_complete;
1383
1384 /*
1385  * Hold root mount.
1386  */
1387 struct root_hold_token *
1388 root_mount_hold(const char *identifier)
1389 {
1390         struct root_hold_token *h;
1391
1392         if (root_mounted())
1393                 return (NULL);
1394
1395         h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1396         h->who = identifier;
1397         mtx_lock(&mountlist_mtx);
1398         LIST_INSERT_HEAD(&root_holds, h, list);
1399         mtx_unlock(&mountlist_mtx);
1400         return (h);
1401 }
1402
1403 /*
1404  * Release root mount.
1405  */
1406 void
1407 root_mount_rel(struct root_hold_token *h)
1408 {
1409
1410         if (h == NULL)
1411                 return;
1412         mtx_lock(&mountlist_mtx);
1413         LIST_REMOVE(h, list);
1414         wakeup(&root_holds);
1415         mtx_unlock(&mountlist_mtx);
1416         free(h, M_DEVBUF);
1417 }
1418
1419 /*
1420  * Wait for all subsystems to release root mount.
1421  */
1422 static void
1423 root_mount_prepare(void)
1424 {
1425         struct root_hold_token *h;
1426         struct timeval lastfail;
1427         int curfail = 0;
1428
1429         for (;;) {
1430                 DROP_GIANT();
1431                 g_waitidle();
1432                 PICKUP_GIANT();
1433                 mtx_lock(&mountlist_mtx);
1434                 if (LIST_EMPTY(&root_holds)) {
1435                         mtx_unlock(&mountlist_mtx);
1436                         break;
1437                 }
1438                 if (ppsratecheck(&lastfail, &curfail, 1)) {
1439                         printf("Root mount waiting for:");
1440                         LIST_FOREACH(h, &root_holds, list)
1441                                 printf(" %s", h->who);
1442                         printf("\n");
1443                 }
1444                 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1445                     hz);
1446         }
1447 }
1448
1449 /*
1450  * Root was mounted, share the good news.
1451  */
1452 static void
1453 root_mount_done(void)
1454 {
1455
1456         /* Keep prison0's root in sync with the global rootvnode. */
1457         mtx_lock(&prison0.pr_mtx);
1458         prison0.pr_root = rootvnode;
1459         vref(prison0.pr_root);
1460         mtx_unlock(&prison0.pr_mtx);
1461         /*
1462          * Use a mutex to prevent the wakeup being missed and waiting for
1463          * an extra 1 second sleep.
1464          */
1465         mtx_lock(&mountlist_mtx);
1466         root_mount_complete = 1;
1467         wakeup(&root_mount_complete);
1468         mtx_unlock(&mountlist_mtx);
1469 }
1470
1471 /*
1472  * Return true if root is already mounted.
1473  */
1474 int
1475 root_mounted(void)
1476 {
1477
1478         /* No mutex is acquired here because int stores are atomic. */
1479         return (root_mount_complete);
1480 }
1481
1482 /*
1483  * Wait until root is mounted.
1484  */
1485 void
1486 root_mount_wait(void)
1487 {
1488
1489         /*
1490          * Panic on an obvious deadlock - the function can't be called from
1491          * a thread which is doing the whole SYSINIT stuff.
1492          */
1493         KASSERT(curthread->td_proc->p_pid != 0,
1494             ("root_mount_wait: cannot be called from the swapper thread"));
1495         mtx_lock(&mountlist_mtx);
1496         while (!root_mount_complete) {
1497                 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
1498                     hz);
1499         }
1500         mtx_unlock(&mountlist_mtx);
1501 }
1502
1503 static void
1504 set_rootvnode()
1505 {
1506         struct proc *p;
1507
1508         if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
1509                 panic("Cannot find root vnode");
1510
1511         VOP_UNLOCK(rootvnode, 0);
1512
1513         p = curthread->td_proc;
1514         FILEDESC_XLOCK(p->p_fd);
1515
1516         if (p->p_fd->fd_cdir != NULL)
1517                 vrele(p->p_fd->fd_cdir);
1518         p->p_fd->fd_cdir = rootvnode;
1519         VREF(rootvnode);
1520
1521         if (p->p_fd->fd_rdir != NULL)
1522                 vrele(p->p_fd->fd_rdir);
1523         p->p_fd->fd_rdir = rootvnode;
1524         VREF(rootvnode);
1525
1526         FILEDESC_XUNLOCK(p->p_fd);
1527
1528         EVENTHANDLER_INVOKE(mountroot);
1529 }
1530
1531 /*
1532  * Mount /devfs as our root filesystem, but do not put it on the mountlist
1533  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1534  */
1535
1536 static void
1537 devfs_first(void)
1538 {
1539         struct thread *td = curthread;
1540         struct vfsoptlist *opts;
1541         struct vfsconf *vfsp;
1542         struct mount *mp = NULL;
1543         int error;
1544
1545         vfsp = vfs_byname("devfs");
1546         KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1547         if (vfsp == NULL)
1548                 return;
1549
1550         mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
1551
1552         error = VFS_MOUNT(mp);
1553         KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1554         if (error)
1555                 return;
1556
1557         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1558         TAILQ_INIT(opts);
1559         mp->mnt_opt = opts;
1560
1561         mtx_lock(&mountlist_mtx);
1562         TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1563         mtx_unlock(&mountlist_mtx);
1564
1565         set_rootvnode();
1566
1567         error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1568         if (error)
1569                 printf("kern_symlink /dev -> / returns %d\n", error);
1570 }
1571
1572 /*
1573  * Surgically move our devfs to be mounted on /dev.
1574  */
1575
1576 static void
1577 devfs_fixup(struct thread *td)
1578 {
1579         struct nameidata nd;
1580         int error;
1581         struct vnode *vp, *dvp;
1582         struct mount *mp;
1583
1584         /* Remove our devfs mount from the mountlist and purge the cache */
1585         mtx_lock(&mountlist_mtx);
1586         mp = TAILQ_FIRST(&mountlist);
1587         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1588         mtx_unlock(&mountlist_mtx);
1589         cache_purgevfs(mp);
1590
1591         VFS_ROOT(mp, LK_EXCLUSIVE, &dvp);
1592         VI_LOCK(dvp);
1593         dvp->v_iflag &= ~VI_MOUNT;
1594         VI_UNLOCK(dvp);
1595         dvp->v_mountedhere = NULL;
1596
1597         /* Set up the real rootvnode, and purge the cache */
1598         TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1599         set_rootvnode();
1600         cache_purgevfs(rootvnode->v_mount);
1601
1602         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1603         error = namei(&nd);
1604         if (error) {
1605                 printf("Lookup of /dev for devfs, error: %d\n", error);
1606                 return;
1607         }
1608         NDFREE(&nd, NDF_ONLY_PNBUF);
1609         vp = nd.ni_vp;
1610         if (vp->v_type != VDIR) {
1611                 vput(vp);
1612         }
1613         error = vinvalbuf(vp, V_SAVE, 0, 0);
1614         if (error) {
1615                 vput(vp);
1616         }
1617         cache_purge(vp);
1618         mp->mnt_vnodecovered = vp;
1619         vp->v_mountedhere = mp;
1620         mtx_lock(&mountlist_mtx);
1621         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1622         mtx_unlock(&mountlist_mtx);
1623         VOP_UNLOCK(vp, 0);
1624         vput(dvp);
1625         vfs_unbusy(mp);
1626
1627         /* Unlink the no longer needed /dev/dev -> / symlink */
1628         kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1629 }
1630
1631 /*
1632  * Report errors during filesystem mounting.
1633  */
1634 void
1635 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1636 {
1637         struct vfsoptlist *moptlist = mp->mnt_optnew;
1638         va_list ap;
1639         int error, len;
1640         char *errmsg;
1641
1642         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1643         if (error || errmsg == NULL || len <= 0)
1644                 return;
1645
1646         va_start(ap, fmt);
1647         vsnprintf(errmsg, (size_t)len, fmt, ap);
1648         va_end(ap);
1649 }
1650
1651 void
1652 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1653 {
1654         va_list ap;
1655         int error, len;
1656         char *errmsg;
1657
1658         error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1659         if (error || errmsg == NULL || len <= 0)
1660                 return;
1661
1662         va_start(ap, fmt);
1663         vsnprintf(errmsg, (size_t)len, fmt, ap);
1664         va_end(ap);
1665 }
1666
1667 /*
1668  * Find and mount the root filesystem
1669  */
1670 void
1671 vfs_mountroot(void)
1672 {
1673         char *cp, *cpt, *options, *tmpdev;
1674         int error, i, asked = 0;
1675
1676         options = NULL;
1677
1678         root_mount_prepare();
1679
1680         mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount),
1681             NULL, NULL, mount_init, mount_fini,
1682             UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1683         devfs_first();
1684
1685         /*
1686          * We are booted with instructions to prompt for the root filesystem.
1687          */
1688         if (boothowto & RB_ASKNAME) {
1689                 if (!vfs_mountroot_ask())
1690                         goto mounted;
1691                 asked = 1;
1692         }
1693
1694         options = getenv("vfs.root.mountfrom.options");
1695
1696         /*
1697          * The root filesystem information is compiled in, and we are
1698          * booted with instructions to use it.
1699          */
1700         if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1701                 if (!vfs_mountroot_try(ctrootdevname, options))
1702                         goto mounted;
1703                 ctrootdevname = NULL;
1704         }
1705
1706         /*
1707          * We've been given the generic "use CDROM as root" flag.  This is
1708          * necessary because one media may be used in many different
1709          * devices, so we need to search for them.
1710          */
1711         if (boothowto & RB_CDROM) {
1712                 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1713                         if (!vfs_mountroot_try(cdrom_rootdevnames[i], options))
1714                                 goto mounted;
1715                 }
1716         }
1717
1718         /*
1719          * Try to use the value read by the loader from /etc/fstab, or
1720          * supplied via some other means.  This is the preferred
1721          * mechanism.
1722          */
1723         cp = getenv("vfs.root.mountfrom");
1724         if (cp != NULL) {
1725                 cpt = cp;
1726                 while ((tmpdev = strsep(&cpt, " \t")) != NULL) {
1727                         error = vfs_mountroot_try(tmpdev, options);
1728                         if (error == 0) {
1729                                 freeenv(cp);
1730                                 goto mounted;
1731                         }
1732                 }
1733                 freeenv(cp);
1734         }
1735
1736         /*
1737          * Try values that may have been computed by code during boot
1738          */
1739         if (!vfs_mountroot_try(rootdevnames[0], options))
1740                 goto mounted;
1741         if (!vfs_mountroot_try(rootdevnames[1], options))
1742                 goto mounted;
1743
1744         /*
1745          * If we (still) have a compiled-in default, try it.
1746          */
1747         if (ctrootdevname != NULL)
1748                 if (!vfs_mountroot_try(ctrootdevname, options))
1749                         goto mounted;
1750         /*
1751          * Everything so far has failed, prompt on the console if we haven't
1752          * already tried that.
1753          */
1754         if (!asked)
1755                 if (!vfs_mountroot_ask())
1756                         goto mounted;
1757
1758         panic("Root mount failed, startup aborted.");
1759
1760 mounted:
1761         root_mount_done();
1762         freeenv(options);
1763 }
1764
1765 static struct mntarg *
1766 parse_mountroot_options(struct mntarg *ma, const char *options)
1767 {
1768         char *p;
1769         char *name, *name_arg;
1770         char *val, *val_arg;
1771         char *opts;
1772
1773         if (options == NULL || options[0] == '\0')
1774                 return (ma);
1775
1776         p = opts = strdup(options, M_MOUNT);
1777         if (opts == NULL) {
1778                 return (ma);
1779         } 
1780
1781         while((name = strsep(&p, ",")) != NULL) {
1782                 if (name[0] == '\0')
1783                         break;
1784
1785                 val = strchr(name, '=');
1786                 if (val != NULL) {
1787                         *val = '\0';
1788                         ++val;
1789                 }
1790                 if( strcmp(name, "rw") == 0 ||
1791                     strcmp(name, "noro") == 0) {
1792                         /*
1793                          * The first time we mount the root file system,
1794                          * we need to mount 'ro', so We need to ignore
1795                          * 'rw' and 'noro' mount options.
1796                          */
1797                         continue;
1798                 }
1799                 name_arg = strdup(name, M_MOUNT);
1800                 val_arg = NULL;
1801                 if (val != NULL) 
1802                         val_arg = strdup(val, M_MOUNT);
1803
1804                 ma = mount_arg(ma, name_arg, val_arg,
1805                     (val_arg != NULL ? -1 : 0));
1806         }
1807         free(opts, M_MOUNT);
1808         return (ma);
1809 }
1810
1811 /*
1812  * Mount (mountfrom) as the root filesystem.
1813  */
1814 static int
1815 vfs_mountroot_try(const char *mountfrom, const char *options)
1816 {
1817         struct mount    *mp;
1818         struct mntarg   *ma;
1819         char            *vfsname, *path;
1820         time_t          timebase;
1821         int             error;
1822         char            patt[32];
1823         char            errmsg[255];
1824
1825         vfsname = NULL;
1826         path    = NULL;
1827         mp      = NULL;
1828         ma      = NULL;
1829         error   = EINVAL;
1830         bzero(errmsg, sizeof(errmsg));
1831
1832         if (mountfrom == NULL)
1833                 return (error);         /* don't complain */
1834         printf("Trying to mount root from %s\n", mountfrom);
1835
1836         /* parse vfs name and path */
1837         vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1838         path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1839         vfsname[0] = path[0] = 0;
1840         sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1841         if (sscanf(mountfrom, patt, vfsname, path) < 1)
1842                 goto out;
1843
1844         if (path[0] == '\0')
1845                 strcpy(path, ROOTNAME);
1846
1847         ma = mount_arg(ma, "fstype", vfsname, -1);
1848         ma = mount_arg(ma, "fspath", "/", -1);
1849         ma = mount_arg(ma, "from", path, -1);
1850         ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
1851         ma = mount_arg(ma, "ro", NULL, 0);
1852         ma = parse_mountroot_options(ma, options);
1853         error = kernel_mount(ma, MNT_ROOTFS);
1854
1855         if (error == 0) {
1856                 /*
1857                  * We mount devfs prior to mounting the / FS, so the first
1858                  * entry will typically be devfs.
1859                  */
1860                 mp = TAILQ_FIRST(&mountlist);
1861                 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1862
1863                 /*
1864                  * Iterate over all currently mounted file systems and use
1865                  * the time stamp found to check and/or initialize the RTC.
1866                  * Typically devfs has no time stamp and the only other FS
1867                  * is the actual / FS.
1868                  * Call inittodr() only once and pass it the largest of the
1869                  * timestamps we encounter.
1870                  */
1871                 timebase = 0;
1872                 do {
1873                         if (mp->mnt_time > timebase)
1874                                 timebase = mp->mnt_time;
1875                         mp = TAILQ_NEXT(mp, mnt_list);
1876                 } while (mp != NULL);
1877                 inittodr(timebase);
1878
1879                 devfs_fixup(curthread);
1880         }
1881
1882         if (error != 0 ) {
1883                 printf("ROOT MOUNT ERROR: %s\n", errmsg);
1884                 printf("If you have invalid mount options, reboot, and ");
1885                 printf("first try the following from\n");
1886                 printf("the loader prompt:\n\n");
1887                 printf("     set vfs.root.mountfrom.options=rw\n\n");
1888                 printf("and then remove invalid mount options from ");
1889                 printf("/etc/fstab.\n\n");
1890         }
1891 out:
1892         free(path, M_MOUNT);
1893         free(vfsname, M_MOUNT);
1894         return (error);
1895 }
1896
1897 /*
1898  * ---------------------------------------------------------------------
1899  * Interactive root filesystem selection code.
1900  */
1901
1902 static int
1903 vfs_mountroot_ask(void)
1904 {
1905         char name[128];
1906         char *mountfrom;
1907         char *options;
1908
1909         for(;;) {
1910                 printf("Loader variables:\n");
1911                 printf("vfs.root.mountfrom=");
1912                 mountfrom = getenv("vfs.root.mountfrom");
1913                 if (mountfrom != NULL) {
1914                         printf("%s", mountfrom);
1915                 }
1916                 printf("\n");
1917                 printf("vfs.root.mountfrom.options=");
1918                 options = getenv("vfs.root.mountfrom.options");
1919                 if (options != NULL) {
1920                         printf("%s", options);
1921                 }
1922                 printf("\n");
1923                 freeenv(mountfrom);
1924                 freeenv(options);
1925                 printf("\nManual root filesystem specification:\n");
1926                 printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1927                 printf("                       eg. ufs:/dev/da0s1a\n");
1928                 printf("                       eg. cd9660:/dev/acd0\n");
1929                 printf("                       This is equivalent to: ");
1930                 printf("mount -t cd9660 /dev/acd0 /\n"); 
1931                 printf("\n");
1932                 printf("  ?                  List valid disk boot devices\n");
1933                 printf("  <empty line>       Abort manual input\n");
1934                 printf("\nmountroot> ");
1935                 gets(name, sizeof(name), 1);
1936                 if (name[0] == '\0')
1937                         return (1);
1938                 if (name[0] == '?') {
1939                         printf("\nList of GEOM managed disk devices:\n  ");
1940                         g_dev_print();
1941                         continue;
1942                 }
1943                 if (!vfs_mountroot_try(name, NULL))
1944                         return (0);
1945         }
1946 }
1947
1948 /*
1949  * ---------------------------------------------------------------------
1950  * Functions for querying mount options/arguments from filesystems.
1951  */
1952
1953 /*
1954  * Check that no unknown options are given
1955  */
1956 int
1957 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1958 {
1959         struct vfsopt *opt;
1960         char errmsg[255];
1961         const char **t, *p, *q;
1962         int ret = 0;
1963
1964         TAILQ_FOREACH(opt, opts, link) {
1965                 p = opt->name;
1966                 q = NULL;
1967                 if (p[0] == 'n' && p[1] == 'o')
1968                         q = p + 2;
1969                 for(t = global_opts; *t != NULL; t++) {
1970                         if (strcmp(*t, p) == 0)
1971                                 break;
1972                         if (q != NULL) {
1973                                 if (strcmp(*t, q) == 0)
1974                                         break;
1975                         }
1976                 }
1977                 if (*t != NULL)
1978                         continue;
1979                 for(t = legal; *t != NULL; t++) {
1980                         if (strcmp(*t, p) == 0)
1981                                 break;
1982                         if (q != NULL) {
1983                                 if (strcmp(*t, q) == 0)
1984                                         break;
1985                         }
1986                 }
1987                 if (*t != NULL)
1988                         continue;
1989                 snprintf(errmsg, sizeof(errmsg),
1990                     "mount option <%s> is unknown", p);
1991                 printf("%s\n", errmsg);
1992                 ret = EINVAL;
1993         }
1994         if (ret != 0) {
1995                 TAILQ_FOREACH(opt, opts, link) {
1996                         if (strcmp(opt->name, "errmsg") == 0) {
1997                                 strncpy((char *)opt->value, errmsg, opt->len);
1998                         }
1999                 }
2000         }
2001         return (ret);
2002 }
2003
2004 /*
2005  * Get a mount option by its name.
2006  *
2007  * Return 0 if the option was found, ENOENT otherwise.
2008  * If len is non-NULL it will be filled with the length
2009  * of the option. If buf is non-NULL, it will be filled
2010  * with the address of the option.
2011  */
2012 int
2013 vfs_getopt(opts, name, buf, len)
2014         struct vfsoptlist *opts;
2015         const char *name;
2016         void **buf;
2017         int *len;
2018 {
2019         struct vfsopt *opt;
2020
2021         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2022
2023         TAILQ_FOREACH(opt, opts, link) {
2024                 if (strcmp(name, opt->name) == 0) {
2025                         opt->seen = 1;
2026                         if (len != NULL)
2027                                 *len = opt->len;
2028                         if (buf != NULL)
2029                                 *buf = opt->value;
2030                         return (0);
2031                 }
2032         }
2033         return (ENOENT);
2034 }
2035
2036 int
2037 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2038 {
2039         struct vfsopt *opt;
2040
2041         if (opts == NULL)
2042                 return (-1);
2043
2044         TAILQ_FOREACH(opt, opts, link) {
2045                 if (strcmp(name, opt->name) == 0) {
2046                         opt->seen = 1;
2047                         return (opt->pos);
2048                 }
2049         }
2050         return (-1);
2051 }
2052
2053 char *
2054 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2055 {
2056         struct vfsopt *opt;
2057
2058         *error = 0;
2059         TAILQ_FOREACH(opt, opts, link) {
2060                 if (strcmp(name, opt->name) != 0)
2061                         continue;
2062                 opt->seen = 1;
2063                 if (opt->len == 0 ||
2064                     ((char *)opt->value)[opt->len - 1] != '\0') {
2065                         *error = EINVAL;
2066                         return (NULL);
2067                 }
2068                 return (opt->value);
2069         }
2070         *error = ENOENT;
2071         return (NULL);
2072 }
2073
2074 int
2075 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
2076 {
2077         struct vfsopt *opt;
2078
2079         TAILQ_FOREACH(opt, opts, link) {
2080                 if (strcmp(name, opt->name) == 0) {
2081                         opt->seen = 1;
2082                         if (w != NULL)
2083                                 *w |= val;
2084                         return (1);
2085                 }
2086         }
2087         if (w != NULL)
2088                 *w &= ~val;
2089         return (0);
2090 }
2091
2092 int
2093 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2094 {
2095         va_list ap;
2096         struct vfsopt *opt;
2097         int ret;
2098
2099         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2100
2101         TAILQ_FOREACH(opt, opts, link) {
2102                 if (strcmp(name, opt->name) != 0)
2103                         continue;
2104                 opt->seen = 1;
2105                 if (opt->len == 0 || opt->value == NULL)
2106                         return (0);
2107                 if (((char *)opt->value)[opt->len - 1] != '\0')
2108                         return (0);
2109                 va_start(ap, fmt);
2110                 ret = vsscanf(opt->value, fmt, ap);
2111                 va_end(ap);
2112                 return (ret);
2113         }
2114         return (0);
2115 }
2116
2117 int
2118 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2119 {
2120         struct vfsopt *opt;
2121
2122         TAILQ_FOREACH(opt, opts, link) {
2123                 if (strcmp(name, opt->name) != 0)
2124                         continue;
2125                 opt->seen = 1;
2126                 if (opt->value == NULL)
2127                         opt->len = len;
2128                 else {
2129                         if (opt->len != len)
2130                                 return (EINVAL);
2131                         bcopy(value, opt->value, len);
2132                 }
2133                 return (0);
2134         }
2135         return (ENOENT);
2136 }
2137
2138 int
2139 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2140 {
2141         struct vfsopt *opt;
2142
2143         TAILQ_FOREACH(opt, opts, link) {
2144                 if (strcmp(name, opt->name) != 0)
2145                         continue;
2146                 opt->seen = 1;
2147                 if (opt->value == NULL)
2148                         opt->len = len;
2149                 else {
2150                         if (opt->len < len)
2151                                 return (EINVAL);
2152                         opt->len = len;
2153                         bcopy(value, opt->value, len);
2154                 }
2155                 return (0);
2156         }
2157         return (ENOENT);
2158 }
2159
2160 int
2161 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2162 {
2163         struct vfsopt *opt;
2164
2165         TAILQ_FOREACH(opt, opts, link) {
2166                 if (strcmp(name, opt->name) != 0)
2167                         continue;
2168                 opt->seen = 1;
2169                 if (opt->value == NULL)
2170                         opt->len = strlen(value) + 1;
2171                 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2172                         return (EINVAL);
2173                 return (0);
2174         }
2175         return (ENOENT);
2176 }
2177
2178 /*
2179  * Find and copy a mount option.
2180  *
2181  * The size of the buffer has to be specified
2182  * in len, if it is not the same length as the
2183  * mount option, EINVAL is returned.
2184  * Returns ENOENT if the option is not found.
2185  */
2186 int
2187 vfs_copyopt(opts, name, dest, len)
2188         struct vfsoptlist *opts;
2189         const char *name;
2190         void *dest;
2191         int len;
2192 {
2193         struct vfsopt *opt;
2194
2195         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2196
2197         TAILQ_FOREACH(opt, opts, link) {
2198                 if (strcmp(name, opt->name) == 0) {
2199                         opt->seen = 1;
2200                         if (len != opt->len)
2201                                 return (EINVAL);
2202                         bcopy(opt->value, dest, opt->len);
2203                         return (0);
2204                 }
2205         }
2206         return (ENOENT);
2207 }
2208
2209 /*
2210  * This is a helper function for filesystems to traverse their
2211  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
2212  */
2213
2214 struct vnode *
2215 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
2216 {
2217         struct vnode *vp;
2218
2219         mtx_assert(MNT_MTX(mp), MA_OWNED);
2220
2221         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2222         if ((*mvp)->v_yield++ == 500) {
2223                 MNT_IUNLOCK(mp);
2224                 (*mvp)->v_yield = 0;
2225                 uio_yield();
2226                 MNT_ILOCK(mp);
2227         }
2228         vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
2229         while (vp != NULL && vp->v_type == VMARKER)
2230                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2231
2232         /* Check if we are done */
2233         if (vp == NULL) {
2234                 __mnt_vnode_markerfree(mvp, mp);
2235                 return (NULL);
2236         }
2237         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2238         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2239         return (vp);
2240 }
2241
2242 struct vnode *
2243 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
2244 {
2245         struct vnode *vp;
2246
2247         mtx_assert(MNT_MTX(mp), MA_OWNED);
2248
2249         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2250         while (vp != NULL && vp->v_type == VMARKER)
2251                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2252
2253         /* Check if we are done */
2254         if (vp == NULL) {
2255                 *mvp = NULL;
2256                 return (NULL);
2257         }
2258         MNT_REF(mp);
2259         MNT_IUNLOCK(mp);
2260         *mvp = (struct vnode *) malloc(sizeof(struct vnode),
2261                                        M_VNODE_MARKER,
2262                                        M_WAITOK | M_ZERO);
2263         MNT_ILOCK(mp);
2264         (*mvp)->v_type = VMARKER;
2265
2266         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
2267         while (vp != NULL && vp->v_type == VMARKER)
2268                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
2269
2270         /* Check if we are done */
2271         if (vp == NULL) {
2272                 MNT_IUNLOCK(mp);
2273                 free(*mvp, M_VNODE_MARKER);
2274                 MNT_ILOCK(mp);
2275                 *mvp = NULL;
2276                 MNT_REL(mp);
2277                 return (NULL);
2278         }
2279         (*mvp)->v_mount = mp;
2280         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
2281         return (vp);
2282 }
2283
2284
2285 void
2286 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
2287 {
2288
2289         if (*mvp == NULL)
2290                 return;
2291
2292         mtx_assert(MNT_MTX(mp), MA_OWNED);
2293
2294         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
2295         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
2296         MNT_IUNLOCK(mp);
2297         free(*mvp, M_VNODE_MARKER);
2298         MNT_ILOCK(mp);
2299         *mvp = NULL;
2300         MNT_REL(mp);
2301 }
2302
2303
2304 int
2305 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2306 {
2307         int error;
2308
2309         error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat);
2310         if (sbp != &mp->mnt_stat)
2311                 *sbp = mp->mnt_stat;
2312         return (error);
2313 }
2314
2315 void
2316 vfs_mountedfrom(struct mount *mp, const char *from)
2317 {
2318
2319         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2320         strlcpy(mp->mnt_stat.f_mntfromname, from,
2321             sizeof mp->mnt_stat.f_mntfromname);
2322 }
2323
2324 /*
2325  * ---------------------------------------------------------------------
2326  * This is the api for building mount args and mounting filesystems from
2327  * inside the kernel.
2328  *
2329  * The API works by accumulation of individual args.  First error is
2330  * latched.
2331  *
2332  * XXX: should be documented in new manpage kernel_mount(9)
2333  */
2334
2335 /* A memory allocation which must be freed when we are done */
2336 struct mntaarg {
2337         SLIST_ENTRY(mntaarg)    next;
2338 };
2339
2340 /* The header for the mount arguments */
2341 struct mntarg {
2342         struct iovec *v;
2343         int len;
2344         int error;
2345         SLIST_HEAD(, mntaarg)   list;
2346 };
2347
2348 /*
2349  * Add a boolean argument.
2350  *
2351  * flag is the boolean value.
2352  * name must start with "no".
2353  */
2354 struct mntarg *
2355 mount_argb(struct mntarg *ma, int flag, const char *name)
2356 {
2357
2358         KASSERT(name[0] == 'n' && name[1] == 'o',
2359             ("mount_argb(...,%s): name must start with 'no'", name));
2360
2361         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2362 }
2363
2364 /*
2365  * Add an argument printf style
2366  */
2367 struct mntarg *
2368 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2369 {
2370         va_list ap;
2371         struct mntaarg *maa;
2372         struct sbuf *sb;
2373         int len;
2374
2375         if (ma == NULL) {
2376                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2377                 SLIST_INIT(&ma->list);
2378         }
2379         if (ma->error)
2380                 return (ma);
2381
2382         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2383             M_MOUNT, M_WAITOK);
2384         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2385         ma->v[ma->len].iov_len = strlen(name) + 1;
2386         ma->len++;
2387
2388         sb = sbuf_new_auto();
2389         va_start(ap, fmt);
2390         sbuf_vprintf(sb, fmt, ap);
2391         va_end(ap);
2392         sbuf_finish(sb);
2393         len = sbuf_len(sb) + 1;
2394         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2395         SLIST_INSERT_HEAD(&ma->list, maa, next);
2396         bcopy(sbuf_data(sb), maa + 1, len);
2397         sbuf_delete(sb);
2398
2399         ma->v[ma->len].iov_base = maa + 1;
2400         ma->v[ma->len].iov_len = len;
2401         ma->len++;
2402
2403         return (ma);
2404 }
2405
2406 /*
2407  * Add an argument which is a userland string.
2408  */
2409 struct mntarg *
2410 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2411 {
2412         struct mntaarg *maa;
2413         char *tbuf;
2414
2415         if (val == NULL)
2416                 return (ma);
2417         if (ma == NULL) {
2418                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2419                 SLIST_INIT(&ma->list);
2420         }
2421         if (ma->error)
2422                 return (ma);
2423         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2424         SLIST_INSERT_HEAD(&ma->list, maa, next);
2425         tbuf = (void *)(maa + 1);
2426         ma->error = copyinstr(val, tbuf, len, NULL);
2427         return (mount_arg(ma, name, tbuf, -1));
2428 }
2429
2430 /*
2431  * Plain argument.
2432  *
2433  * If length is -1, treat value as a C string.
2434  */
2435 struct mntarg *
2436 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2437 {
2438
2439         if (ma == NULL) {
2440                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2441                 SLIST_INIT(&ma->list);
2442         }
2443         if (ma->error)
2444                 return (ma);
2445
2446         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2447             M_MOUNT, M_WAITOK);
2448         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2449         ma->v[ma->len].iov_len = strlen(name) + 1;
2450         ma->len++;
2451
2452         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2453         if (len < 0)
2454                 ma->v[ma->len].iov_len = strlen(val) + 1;
2455         else
2456                 ma->v[ma->len].iov_len = len;
2457         ma->len++;
2458         return (ma);
2459 }
2460
2461 /*
2462  * Free a mntarg structure
2463  */
2464 static void
2465 free_mntarg(struct mntarg *ma)
2466 {
2467         struct mntaarg *maa;
2468
2469         while (!SLIST_EMPTY(&ma->list)) {
2470                 maa = SLIST_FIRST(&ma->list);
2471                 SLIST_REMOVE_HEAD(&ma->list, next);
2472                 free(maa, M_MOUNT);
2473         }
2474         free(ma->v, M_MOUNT);
2475         free(ma, M_MOUNT);
2476 }
2477
2478 /*
2479  * Mount a filesystem
2480  */
2481 int
2482 kernel_mount(struct mntarg *ma, int flags)
2483 {
2484         struct uio auio;
2485         int error;
2486
2487         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2488         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2489         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2490
2491         auio.uio_iov = ma->v;
2492         auio.uio_iovcnt = ma->len;
2493         auio.uio_segflg = UIO_SYSSPACE;
2494
2495         error = ma->error;
2496         if (!error)
2497                 error = vfs_donmount(curthread, flags, &auio);
2498         free_mntarg(ma);
2499         return (error);
2500 }
2501
2502 /*
2503  * A printflike function to mount a filesystem.
2504  */
2505 int
2506 kernel_vmount(int flags, ...)
2507 {
2508         struct mntarg *ma = NULL;
2509         va_list ap;
2510         const char *cp;
2511         const void *vp;
2512         int error;
2513
2514         va_start(ap, flags);
2515         for (;;) {
2516                 cp = va_arg(ap, const char *);
2517                 if (cp == NULL)
2518                         break;
2519                 vp = va_arg(ap, const void *);
2520                 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2521         }
2522         va_end(ap);
2523
2524         error = kernel_mount(ma, flags);
2525         return (error);
2526 }
2527
2528 void
2529 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp)
2530 {
2531
2532         bcopy(oexp, exp, sizeof(*oexp));
2533         exp->ex_numsecflavors = 0;
2534 }