]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_mount.c
Because JOB control has higher priority than single threading in
[FreeBSD/FreeBSD.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/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/libkern.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/filedesc.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60
61 #include <geom/geom.h>
62
63 #include <machine/stdarg.h>
64
65 #include "opt_rootdevname.h"
66 #include "opt_ddb.h"
67 #include "opt_mac.h"
68
69 #ifdef DDB
70 #include <ddb/ddb.h>
71 #endif
72
73 #define ROOTNAME                "root_device"
74 #define VFS_MOUNTARG_SIZE_MAX   (1024 * 64)
75
76 static int      vfs_domount(struct thread *td, const char *fstype,
77                     char *fspath, int fsflags, void *fsdata);
78 static struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp,
79                     const char *fspath, struct thread *td);
80 static int      vfs_mountroot_ask(void);
81 static int      vfs_mountroot_try(const char *mountfrom);
82 static int      vfs_donmount(struct thread *td, int fsflags,
83                     struct uio *fsoptions);
84 static void     free_mntarg(struct mntarg *ma);
85 static void     vfs_mount_destroy(struct mount *, struct thread *);
86 static int      vfs_getopt_pos(struct vfsoptlist *opts, const char *name);
87
88 static int      usermount = 0;
89 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
90     "Unprivileged users may mount and unmount file systems");
91
92 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
93 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
94
95 /* List of mounted filesystems. */
96 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
97
98 /* For any iteration/modification of mountlist */
99 struct mtx mountlist_mtx;
100 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
101
102 TAILQ_HEAD(vfsoptlist, vfsopt);
103 struct vfsopt {
104         TAILQ_ENTRY(vfsopt) link;
105         char    *name;
106         void    *value;
107         int     len;
108 };
109
110 /*
111  * The vnode of the system's root (/ in the filesystem, without chroot
112  * active.)
113  */
114 struct vnode    *rootvnode;
115
116 /*
117  * The root filesystem is detailed in the kernel environment variable
118  * vfs.root.mountfrom, which is expected to be in the general format
119  *
120  * <vfsname>:[<path>]
121  * vfsname   := the name of a VFS known to the kernel and capable
122  *              of being mounted as root
123  * path      := disk device name or other data used by the filesystem
124  *              to locate its physical store
125  */
126
127 /*
128  * Global opts, taken by all filesystems
129  */
130 static const char *global_opts[] = {
131         "errmsg",
132         "fstype",
133         "fspath",
134         "rdonly",
135         "ro",
136         "rw",
137         "suid",
138         "exec",
139         NULL
140 };
141
142 /*
143  * The root specifiers we will try if RB_CDROM is specified.
144  */
145 static char *cdrom_rootdevnames[] = {
146         "cd9660:cd0",
147         "cd9660:acd0",
148         NULL
149 };
150
151 /* legacy find-root code */
152 char            *rootdevnames[2] = {NULL, NULL};
153 #ifndef ROOTDEVNAME
154 #  define ROOTDEVNAME NULL
155 #endif
156 static const char       *ctrootdevname = ROOTDEVNAME;
157
158 /*
159  * ---------------------------------------------------------------------
160  * Functions for building and sanitizing the mount options
161  */
162
163 /* Remove one mount option. */
164 static void
165 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
166 {
167
168         TAILQ_REMOVE(opts, opt, link);
169         free(opt->name, M_MOUNT);
170         if (opt->value != NULL)
171                 free(opt->value, M_MOUNT);
172 #ifdef INVARIANTS
173         else if (opt->len != 0)
174                 panic("%s: mount option with NULL value but length != 0",
175                     __func__);
176 #endif
177         free(opt, M_MOUNT);
178 }
179
180 /* Release all resources related to the mount options. */
181 static void
182 vfs_freeopts(struct vfsoptlist *opts)
183 {
184         struct vfsopt *opt;
185
186         while (!TAILQ_EMPTY(opts)) {
187                 opt = TAILQ_FIRST(opts);
188                 vfs_freeopt(opts, opt);
189         }
190         free(opts, M_MOUNT);
191 }
192
193 /*
194  * Check if options are equal (with or without the "no" prefix).
195  */
196 static int
197 vfs_equalopts(const char *opt1, const char *opt2)
198 {
199
200         /* "opt" vs. "opt" or "noopt" vs. "noopt" */
201         if (strcmp(opt1, opt2) == 0)
202                 return (1);
203         /* "noopt" vs. "opt" */
204         if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
205                 return (1);
206         /* "opt" vs. "noopt" */
207         if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
208                 return (1);
209         return (0);
210 }
211
212 /*
213  * If a mount option is specified several times,
214  * (with or without the "no" prefix) only keep
215  * the last occurence of it.
216  */
217 static void
218 vfs_sanitizeopts(struct vfsoptlist *opts)
219 {
220         struct vfsopt *opt, *opt2, *tmp;
221
222         TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
223                 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
224                 while (opt2 != NULL) {
225                         if (vfs_equalopts(opt->name, opt2->name)) {
226                                 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
227                                 vfs_freeopt(opts, opt2);
228                                 opt2 = tmp;
229                         } else {
230                                 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
231                         }
232                 }
233         }
234 }
235
236 /*
237  * Build a linked list of mount options from a struct uio.
238  */
239 static int
240 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
241 {
242         struct vfsoptlist *opts;
243         struct vfsopt *opt;
244         size_t memused;
245         unsigned int i, iovcnt;
246         int error, namelen, optlen;
247
248         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
249         TAILQ_INIT(opts);
250         memused = 0;
251         iovcnt = auio->uio_iovcnt;
252         for (i = 0; i < iovcnt; i += 2) {
253                 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
254                 namelen = auio->uio_iov[i].iov_len;
255                 optlen = auio->uio_iov[i + 1].iov_len;
256                 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
257                 opt->value = NULL;
258                 opt->len = 0;
259
260                 /*
261                  * Do this early, so jumps to "bad" will free the current
262                  * option.
263                  */
264                 TAILQ_INSERT_TAIL(opts, opt, link);
265                 memused += sizeof(struct vfsopt) + optlen + namelen;
266
267                 /*
268                  * Avoid consuming too much memory, and attempts to overflow
269                  * memused.
270                  */
271                 if (memused > VFS_MOUNTARG_SIZE_MAX ||
272                     optlen > VFS_MOUNTARG_SIZE_MAX ||
273                     namelen > VFS_MOUNTARG_SIZE_MAX) {
274                         error = EINVAL;
275                         goto bad;
276                 }
277
278                 if (auio->uio_segflg == UIO_SYSSPACE) {
279                         bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
280                 } else {
281                         error = copyin(auio->uio_iov[i].iov_base, opt->name,
282                             namelen);
283                         if (error)
284                                 goto bad;
285                 }
286                 /* Ensure names are null-terminated strings. */
287                 if (opt->name[namelen - 1] != '\0') {
288                         error = EINVAL;
289                         goto bad;
290                 }
291                 if (optlen != 0) {
292                         opt->len = optlen;
293                         opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
294                         if (auio->uio_segflg == UIO_SYSSPACE) {
295                                 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
296                                     optlen);
297                         } else {
298                                 error = copyin(auio->uio_iov[i + 1].iov_base,
299                                     opt->value, optlen);
300                                 if (error)
301                                         goto bad;
302                         }
303                 }
304         }
305         vfs_sanitizeopts(opts);
306         *options = opts;
307         return (0);
308 bad:
309         vfs_freeopts(opts);
310         return (error);
311 }
312
313 /*
314  * Merge the old mount options with the new ones passed
315  * in the MNT_UPDATE case.
316  */
317 static void
318 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts)
319 {
320         struct vfsopt *opt, *opt2, *new;
321
322         TAILQ_FOREACH(opt, opts, link) {
323                 /*
324                  * Check that this option hasn't been redefined
325                  * nor cancelled with a "no" mount option.
326                  */
327                 opt2 = TAILQ_FIRST(toopts);
328                 while (opt2 != NULL) {
329                         if (strcmp(opt2->name, opt->name) == 0)
330                                 goto next;
331                         if (strncmp(opt2->name, "no", 2) == 0 &&
332                             strcmp(opt2->name + 2, opt->name) == 0) {
333                                 vfs_freeopt(toopts, opt2);
334                                 goto next;
335                         }
336                         opt2 = TAILQ_NEXT(opt2, link);
337                 }
338                 /* We want this option, duplicate it. */
339                 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
340                 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK);
341                 strcpy(new->name, opt->name);
342                 if (opt->len != 0) {
343                         new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
344                         bcopy(opt->value, new->value, opt->len);
345                 } else {
346                         new->value = NULL;
347                 }
348                 new->len = opt->len;
349                 TAILQ_INSERT_TAIL(toopts, new, link);
350 next:
351                 continue;
352         }
353 }
354
355 /*
356  * ---------------------------------------------------------------------
357  * Mount a filesystem
358  */
359 int
360 nmount(td, uap)
361         struct thread *td;
362         struct nmount_args /* {
363                 struct iovec *iovp;
364                 unsigned int iovcnt;
365                 int flags;
366         } */ *uap;
367 {
368         struct uio *auio;
369         struct iovec *iov;
370         unsigned int i;
371         int error;
372         u_int iovcnt;
373
374         /* Kick out MNT_ROOTFS early as it is legal internally */
375         if (uap->flags & MNT_ROOTFS)
376                 return (EINVAL);
377
378         iovcnt = uap->iovcnt;
379         /*
380          * Check that we have an even number of iovec's
381          * and that we have at least two options.
382          */
383         if ((iovcnt & 1) || (iovcnt < 4))
384                 return (EINVAL);
385
386         error = copyinuio(uap->iovp, iovcnt, &auio);
387         if (error)
388                 return (error);
389         iov = auio->uio_iov;
390         for (i = 0; i < iovcnt; i++) {
391                 if (iov->iov_len > MMAXOPTIONLEN) {
392                         free(auio, M_IOV);
393                         return (EINVAL);
394                 }
395                 iov++;
396         }
397         error = vfs_donmount(td, uap->flags, auio);
398
399         free(auio, M_IOV);
400         return (error);
401 }
402
403 /*
404  * ---------------------------------------------------------------------
405  * Various utility functions
406  */
407
408 void
409 vfs_ref(struct mount *mp)
410 {
411
412         MNT_ILOCK(mp);
413         MNT_REF(mp);
414         MNT_IUNLOCK(mp);
415 }
416
417 void
418 vfs_rel(struct mount *mp)
419 {
420
421         MNT_ILOCK(mp);
422         MNT_REL(mp);
423         MNT_IUNLOCK(mp);
424 }
425
426 /*
427  * Allocate and initialize the mount point struct.
428  */
429 static struct mount *
430 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp,
431     const char *fspath, struct thread *td)
432 {
433         struct mount *mp;
434
435         mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO);
436         TAILQ_INIT(&mp->mnt_nvnodelist);
437         mp->mnt_nvnodelistsize = 0;
438         mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
439         lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
440         (void) vfs_busy(mp, LK_NOWAIT, 0, td);
441         mp->mnt_ref = 0;
442         mp->mnt_op = vfsp->vfc_vfsops;
443         mp->mnt_vfc = vfsp;
444         vfsp->vfc_refcount++;   /* XXX Unlocked */
445         mp->mnt_stat.f_type = vfsp->vfc_typenum;
446         mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
447         strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
448         mp->mnt_vnodecovered = vp;
449         mp->mnt_cred = crdup(td->td_ucred);
450         mp->mnt_stat.f_owner = td->td_ucred->cr_uid;
451         strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
452         mp->mnt_iosize_max = DFLTPHYS;
453 #ifdef MAC
454         mac_init_mount(mp);
455         mac_create_mount(td->td_ucred, mp);
456 #endif
457         arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
458         return (mp);
459 }
460
461 /*
462  * Destroy the mount struct previously allocated by vfs_mount_alloc().
463  */
464 static void
465 vfs_mount_destroy(struct mount *mp, struct thread *td)
466 {
467         int i;
468
469         vfs_unbusy(mp, td);
470         MNT_ILOCK(mp);
471         for (i = 0; mp->mnt_ref && i < 3; i++)
472                 msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz);
473         /*
474          * This will always cause a 3 second delay in rebooting due to
475          * refs on the root mountpoint that never go away.  Most of these
476          * are held by init which never exits.
477          */
478         if (i == 3 && (!rebooting || bootverbose))
479                 printf("Mount point %s had %d dangling refs\n",
480                     mp->mnt_stat.f_mntonname, mp->mnt_ref);
481         if (mp->mnt_holdcnt != 0) {
482                 printf("Waiting for mount point to be unheld\n");
483                 while (mp->mnt_holdcnt != 0) {
484                         mp->mnt_holdcntwaiters++;
485                         msleep(&mp->mnt_holdcnt, MNT_MTX(mp),
486                                PZERO, "mntdestroy", 0);
487                         mp->mnt_holdcntwaiters--;
488                 }
489                 printf("mount point unheld\n");
490         }
491         if (mp->mnt_writeopcount > 0) {
492                 printf("Waiting for mount point write ops\n");
493                 while (mp->mnt_writeopcount > 0) {
494                         mp->mnt_kern_flag |= MNTK_SUSPEND;
495                         msleep(&mp->mnt_writeopcount,
496                                MNT_MTX(mp),
497                                PZERO, "mntdestroy2", 0);
498                 }
499                 printf("mount point write ops completed\n");
500         }
501         if (mp->mnt_secondary_writes > 0) {
502                 printf("Waiting for mount point secondary write ops\n");
503                 while (mp->mnt_secondary_writes > 0) {
504                         mp->mnt_kern_flag |= MNTK_SUSPEND;
505                         msleep(&mp->mnt_secondary_writes,
506                                MNT_MTX(mp),
507                                PZERO, "mntdestroy3", 0);
508                 }
509                 printf("mount point secondary write ops completed\n");
510         }
511         MNT_IUNLOCK(mp);
512         mp->mnt_vfc->vfc_refcount--;
513         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
514                 panic("unmount: dangling vnode");
515         lockdestroy(&mp->mnt_lock);
516         MNT_ILOCK(mp);
517         if (mp->mnt_kern_flag & MNTK_MWAIT)
518                 wakeup(mp);
519         if (mp->mnt_writeopcount != 0)
520                 panic("vfs_mount_destroy: nonzero writeopcount");
521         if (mp->mnt_secondary_writes != 0)
522                 panic("vfs_mount_destroy: nonzero secondary_writes");
523         if (mp->mnt_nvnodelistsize != 0)
524                 panic("vfs_mount_destroy: nonzero nvnodelistsize");
525         mp->mnt_writeopcount = -1000;
526         mp->mnt_nvnodelistsize = -1000;
527         mp->mnt_secondary_writes = -1000;
528         MNT_IUNLOCK(mp);
529         mtx_destroy(&mp->mnt_mtx);
530 #ifdef MAC
531         mac_destroy_mount(mp);
532 #endif
533         if (mp->mnt_opt != NULL)
534                 vfs_freeopts(mp->mnt_opt);
535         crfree(mp->mnt_cred);
536         free(mp, M_MOUNT);
537 }
538
539 static int
540 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions)
541 {
542         struct vfsoptlist *optlist;
543         struct vfsopt *opt;
544         char *fstype, *fspath, *errmsg;
545         int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
546
547         errmsg_len = 0;
548         errmsg_pos = -1;
549
550         error = vfs_buildopts(fsoptions, &optlist);
551         if (error)
552                 return (error);
553
554         if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0) 
555                 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
556         else
557                 errmsg_len = 0;
558
559         /*
560          * We need these two options before the others,
561          * and they are mandatory for any filesystem.
562          * Ensure they are NUL terminated as well.
563          */
564         fstypelen = 0;
565         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
566         if (error || fstype[fstypelen - 1] != '\0') {
567                 error = EINVAL;
568                 if (errmsg != NULL)
569                         strncpy(errmsg, "Invalid fstype", errmsg_len);
570                 goto bail;
571         }
572         fspathlen = 0;
573         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
574         if (error || fspath[fspathlen - 1] != '\0') {
575                 error = EINVAL;
576                 if (errmsg != NULL)
577                         strncpy(errmsg, "Invalid fspath", errmsg_len);
578                 goto bail;
579         }
580
581         /*
582          * We need to see if we have the "update" option
583          * before we call vfs_domount(), since vfs_domount() has special
584          * logic based on MNT_UPDATE.  This is very important
585          * when we want to update the root filesystem.
586          */ 
587         TAILQ_FOREACH(opt, optlist, link) {
588                 if (strcmp(opt->name, "update") == 0)
589                         fsflags |= MNT_UPDATE;
590                 else if (strcmp(opt->name, "async") == 0)
591                         fsflags |= MNT_ASYNC;
592                 else if (strcmp(opt->name, "force") == 0)
593                         fsflags |= MNT_FORCE;
594                 else if (strcmp(opt->name, "multilabel") == 0)
595                         fsflags |= MNT_MULTILABEL;
596                 else if (strcmp(opt->name, "noasync") == 0)
597                         fsflags &= ~MNT_ASYNC;
598                 else if (strcmp(opt->name, "noatime") == 0)
599                         fsflags |= MNT_NOATIME;
600                 else if (strcmp(opt->name, "noclusterr") == 0)
601                         fsflags |= MNT_NOCLUSTERR;
602                 else if (strcmp(opt->name, "noclusterw") == 0)
603                         fsflags |= MNT_NOCLUSTERW;
604                 else if (strcmp(opt->name, "noexec") == 0)
605                         fsflags |= MNT_NOEXEC;
606                 else if (strcmp(opt->name, "nosuid") == 0)
607                         fsflags |= MNT_NOSUID;
608                 else if (strcmp(opt->name, "nosymfollow") == 0)
609                         fsflags |= MNT_NOSYMFOLLOW;
610                 else if (strcmp(opt->name, "noro") == 0 ||
611                     strcmp(opt->name, "rw") == 0)
612                         fsflags &= ~MNT_RDONLY;
613                 else if (strcmp(opt->name, "ro") == 0 ||
614                     strcmp(opt->name, "rdonly") == 0)
615                         fsflags |= MNT_RDONLY;
616                 else if (strcmp(opt->name, "snapshot") == 0)
617                         fsflags |= MNT_SNAPSHOT;
618                 else if (strcmp(opt->name, "suiddir") == 0)
619                         fsflags |= MNT_SUIDDIR;
620                 else if (strcmp(opt->name, "sync") == 0)
621                         fsflags |= MNT_SYNCHRONOUS;
622                 else if (strcmp(opt->name, "union") == 0)
623                         fsflags |= MNT_UNION;
624         }
625
626         /*
627          * Be ultra-paranoid about making sure the type and fspath
628          * variables will fit in our mp buffers, including the
629          * terminating NUL.
630          */
631         if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) {
632                 error = ENAMETOOLONG;
633                 goto bail;
634         }
635
636         mtx_lock(&Giant);
637         error = vfs_domount(td, fstype, fspath, fsflags, optlist);
638         mtx_unlock(&Giant);
639 bail:
640         /* copyout the errmsg */
641         if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
642             && errmsg_len > 0 && errmsg != NULL) {
643                 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
644                         strncpy(fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
645                             errmsg, 
646                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
647                 } else {
648                         copystr(errmsg,
649                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
650                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len,
651                             NULL);
652                 } 
653         }
654
655         if (error != 0)
656                 vfs_freeopts(optlist);
657         return (error);
658 }
659
660 /*
661  * ---------------------------------------------------------------------
662  * Old mount API.
663  */
664 #ifndef _SYS_SYSPROTO_H_
665 struct mount_args {
666         char    *type;
667         char    *path;
668         int     flags;
669         caddr_t data;
670 };
671 #endif
672 /* ARGSUSED */
673 int
674 mount(td, uap)
675         struct thread *td;
676         struct mount_args /* {
677                 char *type;
678                 char *path;
679                 int flags;
680                 caddr_t data;
681         } */ *uap;
682 {
683         char *fstype;
684         struct vfsconf *vfsp = NULL;
685         struct mntarg *ma = NULL;
686         int error;
687
688         /* Kick out MNT_ROOTFS early as it is legal internally */
689         uap->flags &= ~MNT_ROOTFS;
690
691         if (uap->data == NULL)
692                 return (EINVAL);
693
694         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
695         error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
696         if (!error) {
697                 mtx_lock(&Giant);       /* XXX ? */
698                 vfsp = vfs_byname_kld(fstype, td, &error);
699                 mtx_unlock(&Giant);
700         }
701         free(fstype, M_TEMP);
702         if (error)
703                 return (error);
704         if (vfsp == NULL)
705                 return (ENOENT);
706         if (vfsp->vfc_vfsops->vfs_cmount == NULL)
707                 return (EOPNOTSUPP);
708
709         ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN);
710         ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
711         ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro");
712         ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid");
713         ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec");
714
715         error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td);
716         return (error);
717 }
718
719
720 /*
721  * vfs_domount(): actually attempt a filesystem mount.
722  */
723 static int
724 vfs_domount(
725         struct thread *td,      /* Flags common to all filesystems. */
726         const char *fstype,     /* Filesystem type. */
727         char *fspath,           /* Mount path. */
728         int fsflags,            /* Flags common to all filesystems. */
729         void *fsdata            /* Options local to the filesystem. */
730         )
731 {
732         struct vnode *vp;
733         struct mount *mp;
734         struct vfsconf *vfsp;
735         int error, flag = 0, kern_flag = 0;
736         struct vattr va;
737         struct nameidata nd;
738
739         mtx_assert(&Giant, MA_OWNED);
740         /*
741          * Be ultra-paranoid about making sure the type and fspath
742          * variables will fit in our mp buffers, including the
743          * terminating NUL.
744          */
745         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
746                 return (ENAMETOOLONG);
747
748         if (jailed(td->td_ucred))
749                 return (EPERM);
750         if (usermount == 0) {
751                 if ((error = suser(td)) != 0)
752                         return (error);
753         }
754
755         /*
756          * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
757          */
758         if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) {
759                 if ((error = suser(td)) != 0)
760                         return (error);
761         }
762         /*
763          * Silently enforce MNT_NOSUID and MNT_USER for
764          * unprivileged users.
765          */
766         if (suser(td) != 0)
767                 fsflags |= MNT_NOSUID | MNT_USER;
768
769         /* Load KLDs before we lock the covered vnode to avoid reversals. */
770         vfsp = NULL;
771         if ((fsflags & MNT_UPDATE) == 0) {
772                 /* Don't try to load KLDs if we're mounting the root. */
773                 if (fsflags & MNT_ROOTFS)
774                         vfsp = vfs_byname(fstype);
775                 else
776                         vfsp = vfs_byname_kld(fstype, td, &error);
777                 if (vfsp == NULL)
778                         return (ENODEV);
779         }
780         /*
781          * Get vnode to be covered
782          */
783         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td);
784         if ((error = namei(&nd)) != 0)
785                 return (error);
786         NDFREE(&nd, NDF_ONLY_PNBUF);
787         vp = nd.ni_vp;
788         if (fsflags & MNT_UPDATE) {
789                 if ((vp->v_vflag & VV_ROOT) == 0) {
790                         vput(vp);
791                         return (EINVAL);
792                 }
793                 mp = vp->v_mount;
794                 flag = mp->mnt_flag;
795                 kern_flag = mp->mnt_kern_flag;
796                 /*
797                  * We only allow the filesystem to be reloaded if it
798                  * is currently mounted read-only.
799                  */
800                 if ((fsflags & MNT_RELOAD) &&
801                     ((mp->mnt_flag & MNT_RDONLY) == 0)) {
802                         vput(vp);
803                         return (EOPNOTSUPP);    /* Needs translation */
804                 }
805                 /*
806                  * Only privileged root, or (if MNT_USER is set) the user that
807                  * did the original mount is permitted to update it.
808                  */
809                 error = vfs_suser(mp, td);
810                 if (error) {
811                         vput(vp);
812                         return (error);
813                 }
814                 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
815                         vput(vp);
816                         return (EBUSY);
817                 }
818                 VI_LOCK(vp);
819                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
820                     vp->v_mountedhere != NULL) {
821                         VI_UNLOCK(vp);
822                         vfs_unbusy(mp, td);
823                         vput(vp);
824                         return (EBUSY);
825                 }
826                 vp->v_iflag |= VI_MOUNT;
827                 VI_UNLOCK(vp);
828                 mp->mnt_flag |= fsflags &
829                     (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS);
830                 VOP_UNLOCK(vp, 0, td);
831                 mp->mnt_optnew = fsdata;
832                 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
833         } else {
834                 /*
835                  * If the user is not root, ensure that they own the directory
836                  * onto which we are attempting to mount.
837                  */
838                 error = VOP_GETATTR(vp, &va, td->td_ucred, td);
839                 if (error) {
840                         vput(vp);
841                         return (error);
842                 }
843                 if (va.va_uid != td->td_ucred->cr_uid) {
844                         if ((error = suser(td)) != 0) {
845                                 vput(vp);
846                                 return (error);
847                         }
848                 }
849                 error = vinvalbuf(vp, V_SAVE, td, 0, 0);
850                 if (error != 0) {
851                         vput(vp);
852                         return (error);
853                 }
854                 if (vp->v_type != VDIR) {
855                         vput(vp);
856                         return (ENOTDIR);
857                 }
858                 VI_LOCK(vp);
859                 if ((vp->v_iflag & VI_MOUNT) != 0 ||
860                     vp->v_mountedhere != NULL) {
861                         VI_UNLOCK(vp);
862                         vput(vp);
863                         return (EBUSY);
864                 }
865                 vp->v_iflag |= VI_MOUNT;
866                 VI_UNLOCK(vp);
867
868                 /*
869                  * Allocate and initialize the filesystem.
870                  */
871                 mp = vfs_mount_alloc(vp, vfsp, fspath, td);
872                 VOP_UNLOCK(vp, 0, td);
873
874                 /* XXXMAC: pass to vfs_mount_alloc? */
875                 mp->mnt_optnew = fsdata;
876         }
877
878         /*
879          * Set the mount level flags.
880          */
881         if (fsflags & MNT_RDONLY)
882                 mp->mnt_flag |= MNT_RDONLY;
883         mp->mnt_flag &=~ MNT_UPDATEMASK;
884         mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS);
885         /*
886          * Mount the filesystem.
887          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
888          * get.  No freeing of cn_pnbuf.
889          */
890         error = VFS_MOUNT(mp, td);
891         if (!error) {
892                 if (mp->mnt_opt != NULL)
893                         vfs_freeopts(mp->mnt_opt);
894                 mp->mnt_opt = mp->mnt_optnew;
895                 (void)VFS_STATFS(mp, &mp->mnt_stat, td);
896         }
897         /*
898          * Prevent external consumers of mount options from reading
899          * mnt_optnew.
900         */
901         mp->mnt_optnew = NULL;
902         if (mp->mnt_flag & MNT_UPDATE) {
903                 mp->mnt_flag &=
904                     ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT);
905                 if (error) {
906                         mp->mnt_flag = flag;
907                         mp->mnt_kern_flag = kern_flag;
908                 }
909                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
910                         if (mp->mnt_syncer == NULL)
911                                 error = vfs_allocate_syncvnode(mp);
912                 } else {
913                         if (mp->mnt_syncer != NULL)
914                                 vrele(mp->mnt_syncer);
915                         mp->mnt_syncer = NULL;
916                 }
917                 vfs_unbusy(mp, td);
918                 VI_LOCK(vp);
919                 vp->v_iflag &= ~VI_MOUNT;
920                 VI_UNLOCK(vp);
921                 vrele(vp);
922                 return (error);
923         }
924         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
925         /*
926          * Put the new filesystem on the mount list after root.
927          */
928         cache_purge(vp);
929         if (!error) {
930                 struct vnode *newdp;
931
932                 VI_LOCK(vp);
933                 vp->v_iflag &= ~VI_MOUNT;
934                 VI_UNLOCK(vp);
935                 vp->v_mountedhere = mp;
936                 mtx_lock(&mountlist_mtx);
937                 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
938                 mtx_unlock(&mountlist_mtx);
939                 vfs_event_signal(NULL, VQ_MOUNT, 0);
940                 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td))
941                         panic("mount: lost mount");
942                 mountcheckdirs(vp, newdp);
943                 vput(newdp);
944                 VOP_UNLOCK(vp, 0, td);
945                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
946                         error = vfs_allocate_syncvnode(mp);
947                 vfs_unbusy(mp, td);
948                 if (error)
949                         vrele(vp);
950         } else {
951                 VI_LOCK(vp);
952                 vp->v_iflag &= ~VI_MOUNT;
953                 VI_UNLOCK(vp);
954                 vfs_mount_destroy(mp, td);
955                 vput(vp);
956         }
957         return (error);
958 }
959
960 /*
961  * ---------------------------------------------------------------------
962  * Unmount a filesystem.
963  *
964  * Note: unmount takes a path to the vnode mounted on as argument,
965  * not special file (as before).
966  */
967 #ifndef _SYS_SYSPROTO_H_
968 struct unmount_args {
969         char    *path;
970         int     flags;
971 };
972 #endif
973 /* ARGSUSED */
974 int
975 unmount(td, uap)
976         struct thread *td;
977         register struct unmount_args /* {
978                 char *path;
979                 int flags;
980         } */ *uap;
981 {
982         struct mount *mp;
983         char *pathbuf;
984         int error, id0, id1;
985
986         if (jailed(td->td_ucred))
987                 return (EPERM);
988         if (usermount == 0) {
989                 if ((error = suser(td)) != 0)
990                         return (error);
991         }
992
993         pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
994         error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL);
995         if (error) {
996                 free(pathbuf, M_TEMP);
997                 return (error);
998         }
999         if (uap->flags & MNT_BYFSID) {
1000                 /* Decode the filesystem ID. */
1001                 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1002                         free(pathbuf, M_TEMP);
1003                         return (EINVAL);
1004                 }
1005
1006                 mtx_lock(&mountlist_mtx);
1007                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1008                         if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1009                             mp->mnt_stat.f_fsid.val[1] == id1)
1010                                 break;
1011                 }
1012                 mtx_unlock(&mountlist_mtx);
1013         } else {
1014                 mtx_lock(&mountlist_mtx);
1015                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1016                         if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0)
1017                                 break;
1018                 }
1019                 mtx_unlock(&mountlist_mtx);
1020         }
1021         free(pathbuf, M_TEMP);
1022         if (mp == NULL) {
1023                 /*
1024                  * Previously we returned ENOENT for a nonexistent path and
1025                  * EINVAL for a non-mountpoint.  We cannot tell these apart
1026                  * now, so in the !MNT_BYFSID case return the more likely
1027                  * EINVAL for compatibility.
1028                  */
1029                 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL);
1030         }
1031
1032         /*
1033          * Only privileged root, or (if MNT_USER is set) the user that did the
1034          * original mount is permitted to unmount this filesystem.
1035          */
1036         error = vfs_suser(mp, td);
1037         if (error)
1038                 return (error);
1039
1040         /*
1041          * Don't allow unmounting the root filesystem.
1042          */
1043         if (mp->mnt_flag & MNT_ROOTFS)
1044                 return (EINVAL);
1045         mtx_lock(&Giant);
1046         error = dounmount(mp, uap->flags, td);
1047         mtx_unlock(&Giant);
1048         return (error);
1049 }
1050
1051 /*
1052  * Do the actual filesystem unmount.
1053  */
1054 int
1055 dounmount(mp, flags, td)
1056         struct mount *mp;
1057         int flags;
1058         struct thread *td;
1059 {
1060         struct vnode *coveredvp, *fsrootvp;
1061         int error;
1062         int async_flag;
1063
1064         mtx_assert(&Giant, MA_OWNED);
1065
1066         if ((coveredvp = mp->mnt_vnodecovered) != NULL)
1067                 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY, td);
1068         MNT_ILOCK(mp);
1069         if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
1070                 MNT_IUNLOCK(mp);
1071                 if (coveredvp)
1072                         VOP_UNLOCK(coveredvp, 0, td);
1073                 return (EBUSY);
1074         }
1075         mp->mnt_kern_flag |= MNTK_UNMOUNT;
1076         /* Allow filesystems to detect that a forced unmount is in progress. */
1077         if (flags & MNT_FORCE)
1078                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1079         error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
1080             ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td);
1081         if (error) {
1082                 MNT_ILOCK(mp);
1083                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1084                 if (mp->mnt_kern_flag & MNTK_MWAIT)
1085                         wakeup(mp);
1086                 MNT_IUNLOCK(mp);
1087                 if (coveredvp)
1088                         VOP_UNLOCK(coveredvp, 0, td);
1089                 return (error);
1090         }
1091         vn_start_write(NULL, &mp, V_WAIT);
1092
1093         if (mp->mnt_flag & MNT_EXPUBLIC)
1094                 vfs_setpublicfs(NULL, NULL, NULL);
1095
1096         vfs_msync(mp, MNT_WAIT);
1097         async_flag = mp->mnt_flag & MNT_ASYNC;
1098         mp->mnt_flag &= ~MNT_ASYNC;
1099         cache_purgevfs(mp);     /* remove cache entries for this file sys */
1100         if (mp->mnt_syncer != NULL)
1101                 vrele(mp->mnt_syncer);
1102         /*
1103          * For forced unmounts, move process cdir/rdir refs on the fs root
1104          * vnode to the covered vnode.  For non-forced unmounts we want
1105          * such references to cause an EBUSY error.
1106          */
1107         if ((flags & MNT_FORCE) &&
1108             VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1109                 if (mp->mnt_vnodecovered != NULL)
1110                         mountcheckdirs(fsrootvp, mp->mnt_vnodecovered);
1111                 if (fsrootvp == rootvnode) {
1112                         vrele(rootvnode);
1113                         rootvnode = NULL;
1114                 }
1115                 vput(fsrootvp);
1116         }
1117         if (((mp->mnt_flag & MNT_RDONLY) ||
1118              (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
1119             (flags & MNT_FORCE)) {
1120                 error = VFS_UNMOUNT(mp, flags, td);
1121         }
1122         vn_finished_write(mp);
1123         if (error) {
1124                 /* Undo cdir/rdir and rootvnode changes made above. */
1125                 if ((flags & MNT_FORCE) &&
1126                     VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) {
1127                         if (mp->mnt_vnodecovered != NULL)
1128                                 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp);
1129                         if (rootvnode == NULL) {
1130                                 rootvnode = fsrootvp;
1131                                 vref(rootvnode);
1132                         }
1133                         vput(fsrootvp);
1134                 }
1135                 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
1136                         (void) vfs_allocate_syncvnode(mp);
1137                 MNT_ILOCK(mp);
1138                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1139                 mp->mnt_flag |= async_flag;
1140                 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
1141                 if (mp->mnt_kern_flag & MNTK_MWAIT)
1142                         wakeup(mp);
1143                 MNT_IUNLOCK(mp);
1144                 if (coveredvp)
1145                         VOP_UNLOCK(coveredvp, 0, td);
1146                 return (error);
1147         }
1148         mtx_lock(&mountlist_mtx);
1149         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1150         mtx_unlock(&mountlist_mtx);
1151         if (coveredvp != NULL) {
1152                 coveredvp->v_mountedhere = NULL;
1153                 vput(coveredvp);
1154         }
1155         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1156         vfs_mount_destroy(mp, td);
1157         return (0);
1158 }
1159
1160 /*
1161  * ---------------------------------------------------------------------
1162  * Mounting of root filesystem
1163  *
1164  */
1165
1166 struct root_hold_token {
1167         const char                      *who;
1168         LIST_ENTRY(root_hold_token)     list;
1169 };
1170
1171 static LIST_HEAD(, root_hold_token)     root_holds =
1172     LIST_HEAD_INITIALIZER(&root_holds);
1173
1174 struct root_hold_token *
1175 root_mount_hold(const char *identifier)
1176 {
1177         struct root_hold_token *h;
1178
1179         h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
1180         h->who = identifier;
1181         mtx_lock(&mountlist_mtx);
1182         LIST_INSERT_HEAD(&root_holds, h, list);
1183         mtx_unlock(&mountlist_mtx);
1184         return (h);
1185 }
1186
1187 void
1188 root_mount_rel(struct root_hold_token *h)
1189 {
1190
1191         mtx_lock(&mountlist_mtx);
1192         LIST_REMOVE(h, list);
1193         wakeup(&root_holds);
1194         mtx_unlock(&mountlist_mtx);
1195         free(h, M_DEVBUF);
1196 }
1197
1198 static void
1199 root_mount_wait(void)
1200 {
1201         struct root_hold_token *h;
1202
1203         for (;;) {
1204                 DROP_GIANT();
1205                 g_waitidle();
1206                 PICKUP_GIANT();
1207                 mtx_lock(&mountlist_mtx);
1208                 if (LIST_EMPTY(&root_holds)) {
1209                         mtx_unlock(&mountlist_mtx);
1210                         break;
1211                 }
1212                 printf("Root mount waiting for:");
1213                 LIST_FOREACH(h, &root_holds, list)
1214                         printf(" %s", h->who);
1215                 printf("\n");
1216                 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
1217                     hz);
1218         }
1219 }
1220
1221 static void
1222 set_rootvnode(struct thread *td)
1223 {
1224         struct proc *p;
1225
1226         if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td))
1227                 panic("Cannot find root vnode");
1228
1229         p = td->td_proc;
1230         FILEDESC_LOCK(p->p_fd);
1231
1232         if (p->p_fd->fd_cdir != NULL)
1233                 vrele(p->p_fd->fd_cdir);
1234         p->p_fd->fd_cdir = rootvnode;
1235         VREF(rootvnode);
1236
1237         if (p->p_fd->fd_rdir != NULL)
1238                 vrele(p->p_fd->fd_rdir);
1239         p->p_fd->fd_rdir = rootvnode;
1240         VREF(rootvnode);
1241
1242         FILEDESC_UNLOCK(p->p_fd);
1243
1244         VOP_UNLOCK(rootvnode, 0, td);
1245 }
1246
1247 /*
1248  * Mount /devfs as our root filesystem, but do not put it on the mountlist
1249  * yet.  Create a /dev -> / symlink so that absolute pathnames will lookup.
1250  */
1251
1252 static void
1253 devfs_first(void)
1254 {
1255         struct thread *td = curthread;
1256         struct vfsoptlist *opts;
1257         struct vfsconf *vfsp;
1258         struct mount *mp = NULL;
1259         int error;
1260
1261         vfsp = vfs_byname("devfs");
1262         KASSERT(vfsp != NULL, ("Could not find devfs by name"));
1263         if (vfsp == NULL) 
1264                 return;
1265
1266         mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td);
1267
1268         error = VFS_MOUNT(mp, td);
1269         KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
1270         if (error)
1271                 return;
1272
1273         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
1274         TAILQ_INIT(opts);
1275         mp->mnt_opt = opts;
1276
1277         mtx_lock(&mountlist_mtx);
1278         TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
1279         mtx_unlock(&mountlist_mtx);
1280
1281         set_rootvnode(td);
1282
1283         error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
1284         if (error)
1285                 printf("kern_symlink /dev -> / returns %d\n", error);
1286 }
1287
1288 /*
1289  * Surgically move our devfs to be mounted on /dev.
1290  */
1291
1292 static void
1293 devfs_fixup(struct thread *td)
1294 {
1295         struct nameidata nd;
1296         int error;
1297         struct vnode *vp, *dvp;
1298         struct mount *mp;
1299
1300         /* Remove our devfs mount from the mountlist and purge the cache */
1301         mtx_lock(&mountlist_mtx);
1302         mp = TAILQ_FIRST(&mountlist);
1303         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1304         mtx_unlock(&mountlist_mtx);
1305         cache_purgevfs(mp);
1306
1307         VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
1308         VI_LOCK(dvp);
1309         dvp->v_iflag &= ~VI_MOUNT;
1310         dvp->v_mountedhere = NULL;
1311         VI_UNLOCK(dvp);
1312
1313         /* Set up the real rootvnode, and purge the cache */
1314         TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL;
1315         set_rootvnode(td);
1316         cache_purgevfs(rootvnode->v_mount);
1317
1318         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
1319         error = namei(&nd);
1320         if (error) {
1321                 printf("Lookup of /dev for devfs, error: %d\n", error);
1322                 return;
1323         }
1324         NDFREE(&nd, NDF_ONLY_PNBUF);
1325         vp = nd.ni_vp;
1326         if (vp->v_type != VDIR) {
1327                 vput(vp);
1328         }
1329         error = vinvalbuf(vp, V_SAVE, td, 0, 0);
1330         if (error) {
1331                 vput(vp);
1332         }
1333         cache_purge(vp);
1334         mp->mnt_vnodecovered = vp;
1335         vp->v_mountedhere = mp;
1336         mtx_lock(&mountlist_mtx);
1337         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1338         mtx_unlock(&mountlist_mtx);
1339         VOP_UNLOCK(vp, 0, td);
1340         vput(dvp);
1341         vfs_unbusy(mp, td);
1342
1343         /* Unlink the no longer needed /dev/dev -> / symlink */
1344         kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
1345 }
1346
1347 /*
1348  * Report errors during filesystem mounting. 
1349  */
1350 void
1351 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1352 {
1353         struct vfsoptlist *moptlist = mp->mnt_optnew;
1354         va_list ap;
1355         int error, len;
1356         char *errmsg;
1357
1358         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1359         if (error || errmsg == NULL || len <= 0)
1360                 return;
1361
1362         va_start(ap, fmt);
1363         vsnprintf(errmsg, (size_t)len, fmt, ap);
1364         va_end(ap);
1365 }
1366
1367 /*
1368  * Find and mount the root filesystem
1369  */
1370 void
1371 vfs_mountroot(void)
1372 {
1373         char *cp;
1374         int error, i, asked = 0;
1375
1376         root_mount_wait();
1377
1378         devfs_first();
1379
1380         /*
1381          * We are booted with instructions to prompt for the root filesystem.
1382          */
1383         if (boothowto & RB_ASKNAME) {
1384                 if (!vfs_mountroot_ask())
1385                         return;
1386                 asked = 1;
1387         }
1388
1389         /*
1390          * The root filesystem information is compiled in, and we are
1391          * booted with instructions to use it.
1392          */
1393         if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) {
1394                 if (!vfs_mountroot_try(ctrootdevname))
1395                         return;
1396                 ctrootdevname = NULL;
1397         }
1398
1399         /*
1400          * We've been given the generic "use CDROM as root" flag.  This is
1401          * necessary because one media may be used in many different
1402          * devices, so we need to search for them.
1403          */
1404         if (boothowto & RB_CDROM) {
1405                 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
1406                         if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
1407                                 return;
1408                 }
1409         }
1410
1411         /*
1412          * Try to use the value read by the loader from /etc/fstab, or
1413          * supplied via some other means.  This is the preferred
1414          * mechanism.
1415          */
1416         cp = getenv("vfs.root.mountfrom");
1417         if (cp != NULL) {
1418                 error = vfs_mountroot_try(cp);
1419                 freeenv(cp);
1420                 if (!error)
1421                         return;
1422         }
1423
1424         /*
1425          * Try values that may have been computed by code during boot
1426          */
1427         if (!vfs_mountroot_try(rootdevnames[0]))
1428                 return;
1429         if (!vfs_mountroot_try(rootdevnames[1]))
1430                 return;
1431
1432         /*
1433          * If we (still) have a compiled-in default, try it.
1434          */
1435         if (ctrootdevname != NULL)
1436                 if (!vfs_mountroot_try(ctrootdevname))
1437                         return;
1438         /*
1439          * Everything so far has failed, prompt on the console if we haven't
1440          * already tried that.
1441          */
1442         if (!asked)
1443                 if (!vfs_mountroot_ask())
1444                         return;
1445
1446         panic("Root mount failed, startup aborted.");
1447 }
1448
1449 /*
1450  * Mount (mountfrom) as the root filesystem.
1451  */
1452 static int
1453 vfs_mountroot_try(const char *mountfrom)
1454 {
1455         struct mount    *mp;
1456         char            *vfsname, *path;
1457         time_t          timebase;
1458         int             error;
1459         char            patt[32];
1460
1461         vfsname = NULL;
1462         path    = NULL;
1463         mp      = NULL;
1464         error   = EINVAL;
1465
1466         if (mountfrom == NULL)
1467                 return (error);         /* don't complain */
1468         printf("Trying to mount root from %s\n", mountfrom);
1469
1470         /* parse vfs name and path */
1471         vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
1472         path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
1473         vfsname[0] = path[0] = 0;
1474         sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
1475         if (sscanf(mountfrom, patt, vfsname, path) < 1)
1476                 goto out;
1477
1478         if (path[0] == '\0')
1479                 strcpy(path, ROOTNAME);
1480
1481         error = kernel_vmount(
1482             MNT_RDONLY | MNT_ROOTFS,
1483             "fstype", vfsname,
1484             "fspath", "/",
1485             "from", path,
1486             NULL);
1487         if (error == 0) {
1488                 /*
1489                  * We mount devfs prior to mounting the / FS, so the first
1490                  * entry will typically be devfs.
1491                  */
1492                 mp = TAILQ_FIRST(&mountlist);
1493                 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__));
1494
1495                 /*
1496                  * Iterate over all currently mounted file systems and use
1497                  * the time stamp found to check and/or initialize the RTC.
1498                  * Typically devfs has no time stamp and the only other FS
1499                  * is the actual / FS.
1500                  * Call inittodr() only once and pass it the largest of the
1501                  * timestamps we encounter.
1502                  */
1503                 timebase = 0;
1504                 do {
1505                         if (mp->mnt_time > timebase)
1506                                 timebase = mp->mnt_time;
1507                         mp = TAILQ_NEXT(mp, mnt_list);
1508                 } while (mp != NULL);
1509                 inittodr(timebase);
1510
1511                 devfs_fixup(curthread);
1512         }
1513 out:
1514         free(path, M_MOUNT);
1515         free(vfsname, M_MOUNT);
1516         return (error);
1517 }
1518
1519 /*
1520  * ---------------------------------------------------------------------
1521  * Interactive root filesystem selection code.
1522  */
1523
1524 static int
1525 vfs_mountroot_ask(void)
1526 {
1527         char name[128];
1528
1529         for(;;) {
1530                 printf("\nManual root filesystem specification:\n");
1531                 printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
1532 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
1533                 printf("                       eg. ufs:da0s1a\n");
1534 #else
1535                 printf("                       eg. ufs:/dev/da0a\n");
1536 #endif
1537                 printf("  ?                  List valid disk boot devices\n");
1538                 printf("  <empty line>       Abort manual input\n");
1539                 printf("\nmountroot> ");
1540                 gets(name, sizeof(name), 1);
1541                 if (name[0] == '\0')
1542                         return (1);
1543                 if (name[0] == '?') {
1544                         printf("\nList of GEOM managed disk devices:\n  ");
1545                         g_dev_print();
1546                         continue;
1547                 }
1548                 if (!vfs_mountroot_try(name))
1549                         return (0);
1550         }
1551 }
1552
1553 /*
1554  * ---------------------------------------------------------------------
1555  * Functions for querying mount options/arguments from filesystems.
1556  */
1557
1558 /*
1559  * Check that no unknown options are given
1560  */
1561 int
1562 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1563 {
1564         struct vfsopt *opt;
1565         const char **t, *p;
1566         
1567
1568         TAILQ_FOREACH(opt, opts, link) {
1569                 p = opt->name;
1570                 if (p[0] == 'n' && p[1] == 'o')
1571                         p += 2;
1572                 for(t = global_opts; *t != NULL; t++)
1573                         if (!strcmp(*t, p))
1574                                 break;
1575                 if (*t != NULL)
1576                         continue;
1577                 for(t = legal; *t != NULL; t++)
1578                         if (!strcmp(*t, p))
1579                                 break;
1580                 if (*t != NULL)
1581                         continue;
1582                 printf("mount option <%s> is unknown\n", p);
1583                 return (EINVAL);
1584         }
1585         return (0);
1586 }
1587
1588 /*
1589  * Get a mount option by its name.
1590  *
1591  * Return 0 if the option was found, ENOENT otherwise.
1592  * If len is non-NULL it will be filled with the length
1593  * of the option. If buf is non-NULL, it will be filled
1594  * with the address of the option.
1595  */
1596 int
1597 vfs_getopt(opts, name, buf, len)
1598         struct vfsoptlist *opts;
1599         const char *name;
1600         void **buf;
1601         int *len;
1602 {
1603         struct vfsopt *opt;
1604
1605         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1606
1607         TAILQ_FOREACH(opt, opts, link) {
1608                 if (strcmp(name, opt->name) == 0) {
1609                         if (len != NULL)
1610                                 *len = opt->len;
1611                         if (buf != NULL)
1612                                 *buf = opt->value;
1613                         return (0);
1614                 }
1615         }
1616         return (ENOENT);
1617 }
1618
1619 static int
1620 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
1621 {
1622         struct vfsopt *opt;
1623         int i;
1624
1625         if (opts == NULL)
1626                 return (-1);
1627
1628         i = 0;
1629         TAILQ_FOREACH(opt, opts, link) {
1630                 if (strcmp(name, opt->name) == 0)
1631                         return (i);
1632                 ++i;
1633         }
1634         return (-1);
1635 }
1636
1637 char *
1638 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
1639 {
1640         struct vfsopt *opt;
1641
1642         *error = 0;
1643         TAILQ_FOREACH(opt, opts, link) {
1644                 if (strcmp(name, opt->name) != 0)
1645                         continue;
1646                 if (((char *)opt->value)[opt->len - 1] != '\0') {
1647                         *error = EINVAL;
1648                         return (NULL);
1649                 }
1650                 return (opt->value);
1651         }
1652         return (NULL);
1653 }
1654
1655 int
1656 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val)
1657 {
1658         struct vfsopt *opt;
1659
1660         TAILQ_FOREACH(opt, opts, link) {
1661                 if (strcmp(name, opt->name) == 0) {
1662                         if (w != NULL)
1663                                 *w |= val;
1664                         return (1);
1665                 }
1666         }
1667         if (w != NULL)
1668                 *w &= ~val;
1669         return (0);
1670 }
1671
1672 int
1673 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
1674 {
1675         va_list ap;
1676         struct vfsopt *opt;
1677         int ret;
1678
1679         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
1680
1681         TAILQ_FOREACH(opt, opts, link) {
1682                 if (strcmp(name, opt->name) != 0)
1683                         continue;
1684                 if (((char *)opt->value)[opt->len - 1] != '\0')
1685                         return (0);
1686                 va_start(ap, fmt);
1687                 ret = vsscanf(opt->value, fmt, ap);
1688                 va_end(ap);
1689                 return (ret);
1690         }
1691         return (0);
1692 }
1693
1694 /*
1695  * Find and copy a mount option.
1696  *
1697  * The size of the buffer has to be specified
1698  * in len, if it is not the same length as the
1699  * mount option, EINVAL is returned.
1700  * Returns ENOENT if the option is not found.
1701  */
1702 int
1703 vfs_copyopt(opts, name, dest, len)
1704         struct vfsoptlist *opts;
1705         const char *name;
1706         void *dest;
1707         int len;
1708 {
1709         struct vfsopt *opt;
1710
1711         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
1712
1713         TAILQ_FOREACH(opt, opts, link) {
1714                 if (strcmp(name, opt->name) == 0) {
1715                         if (len != opt->len)
1716                                 return (EINVAL);
1717                         bcopy(opt->value, dest, opt->len);
1718                         return (0);
1719                 }
1720         }
1721         return (ENOENT);
1722 }
1723
1724 /*
1725  * This is a helper function for filesystems to traverse their
1726  * vnodes.  See MNT_VNODE_FOREACH() in sys/mount.h
1727  */
1728
1729 struct vnode *
1730 __mnt_vnode_next(struct vnode **mvp, struct mount *mp)
1731 {
1732         struct vnode *vp;
1733
1734         mtx_assert(MNT_MTX(mp), MA_OWNED);
1735
1736         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1737         vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
1738         while (vp != NULL && vp->v_type == VMARKER) 
1739                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1740         
1741         /* Check if we are done */
1742         if (vp == NULL) {
1743                 __mnt_vnode_markerfree(mvp, mp);
1744                 return (NULL);
1745         }
1746         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1747         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1748         return (vp);
1749 }
1750
1751 struct vnode *
1752 __mnt_vnode_first(struct vnode **mvp, struct mount *mp)
1753 {
1754         struct vnode *vp;
1755
1756         mtx_assert(MNT_MTX(mp), MA_OWNED);
1757
1758         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1759         while (vp != NULL && vp->v_type == VMARKER) 
1760                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1761         
1762         /* Check if we are done */
1763         if (vp == NULL) {
1764                 *mvp = NULL;
1765                 return (NULL);
1766         }
1767         mp->mnt_holdcnt++;
1768         MNT_IUNLOCK(mp);
1769         *mvp = (struct vnode *) malloc(sizeof(struct vnode),
1770                                        M_VNODE_MARKER,
1771                                        M_WAITOK | M_ZERO);
1772         MNT_ILOCK(mp);
1773         (*mvp)->v_type = VMARKER;
1774
1775         vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1776         while (vp != NULL && vp->v_type == VMARKER) 
1777                 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1778         
1779         /* Check if we are done */
1780         if (vp == NULL) {
1781                 MNT_IUNLOCK(mp);
1782                 free(*mvp, M_VNODE_MARKER);
1783                 MNT_ILOCK(mp);
1784                 *mvp = NULL;
1785                 mp->mnt_holdcnt--;
1786                 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1787                         wakeup(&mp->mnt_holdcnt);
1788                 return (NULL);
1789         }
1790         mp->mnt_markercnt++;
1791         (*mvp)->v_mount = mp;
1792         TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
1793         return (vp);
1794 }
1795
1796
1797 void
1798 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp)
1799 {
1800
1801         if (*mvp == NULL)
1802                 return;
1803         
1804         mtx_assert(MNT_MTX(mp), MA_OWNED);
1805
1806         KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
1807         TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
1808         MNT_IUNLOCK(mp);
1809         free(*mvp, M_VNODE_MARKER);
1810         MNT_ILOCK(mp);
1811         *mvp = NULL;
1812
1813         mp->mnt_markercnt--;
1814         mp->mnt_holdcnt--;
1815         if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0)
1816                 wakeup(&mp->mnt_holdcnt);
1817 }
1818
1819
1820 int
1821 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
1822 {
1823         int error;
1824
1825         error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td);
1826         if (sbp != &mp->mnt_stat)
1827                 *sbp = mp->mnt_stat;
1828         return (error);
1829 }
1830
1831 void
1832 vfs_mountedfrom(struct mount *mp, const char *from)
1833 {
1834
1835         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
1836         strlcpy(mp->mnt_stat.f_mntfromname, from,
1837             sizeof mp->mnt_stat.f_mntfromname);
1838 }
1839
1840 /*
1841  * ---------------------------------------------------------------------
1842  * This is the api for building mount args and mounting filesystems from
1843  * inside the kernel.
1844  *
1845  * The API works by accumulation of individual args.  First error is
1846  * latched.
1847  *
1848  * XXX: should be documented in new manpage kernel_mount(9)
1849  */
1850
1851 /* A memory allocation which must be freed when we are done */
1852 struct mntaarg {
1853         SLIST_ENTRY(mntaarg)    next;
1854 };
1855
1856 /* The header for the mount arguments */
1857 struct mntarg {
1858         struct iovec *v;
1859         int len;
1860         int error;
1861         SLIST_HEAD(, mntaarg)   list;
1862 };
1863
1864 /*
1865  * Add a boolean argument.
1866  *
1867  * flag is the boolean value.
1868  * name must start with "no".
1869  */
1870 struct mntarg *
1871 mount_argb(struct mntarg *ma, int flag, const char *name)
1872 {
1873
1874         KASSERT(name[0] == 'n' && name[1] == 'o',
1875             ("mount_argb(...,%s): name must start with 'no'", name));
1876
1877         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
1878 }
1879
1880 /*
1881  * Add an argument printf style
1882  */
1883 struct mntarg *
1884 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
1885 {
1886         va_list ap;
1887         struct mntaarg *maa;
1888         struct sbuf *sb;
1889         int len;
1890
1891         if (ma == NULL) {
1892                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1893                 SLIST_INIT(&ma->list);
1894         }
1895         if (ma->error)
1896                 return (ma);
1897
1898         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1899             M_MOUNT, M_WAITOK);
1900         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1901         ma->v[ma->len].iov_len = strlen(name) + 1;
1902         ma->len++;
1903
1904         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
1905         va_start(ap, fmt);
1906         sbuf_vprintf(sb, fmt, ap);
1907         va_end(ap);
1908         sbuf_finish(sb);
1909         len = sbuf_len(sb) + 1;
1910         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1911         SLIST_INSERT_HEAD(&ma->list, maa, next);
1912         bcopy(sbuf_data(sb), maa + 1, len);
1913         sbuf_delete(sb);
1914
1915         ma->v[ma->len].iov_base = maa + 1;
1916         ma->v[ma->len].iov_len = len;
1917         ma->len++;
1918
1919         return (ma);
1920 }
1921
1922 /*
1923  * Add an argument which is a userland string.
1924  */
1925 struct mntarg *
1926 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
1927 {
1928         struct mntaarg *maa;
1929         char *tbuf;
1930
1931         if (val == NULL)
1932                 return (ma);
1933         if (ma == NULL) {
1934                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1935                 SLIST_INIT(&ma->list);
1936         }
1937         if (ma->error)
1938                 return (ma);
1939         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
1940         SLIST_INSERT_HEAD(&ma->list, maa, next);
1941         tbuf = (void *)(maa + 1);
1942         ma->error = copyinstr(val, tbuf, len, NULL);
1943         return (mount_arg(ma, name, tbuf, -1));
1944 }
1945
1946 /*
1947  * Plain argument.
1948  *
1949  * If length is -1, use printf.
1950  */
1951 struct mntarg *
1952 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
1953 {
1954
1955         if (ma == NULL) {
1956                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
1957                 SLIST_INIT(&ma->list);
1958         }
1959         if (ma->error)
1960                 return (ma);
1961
1962         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
1963             M_MOUNT, M_WAITOK);
1964         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
1965         ma->v[ma->len].iov_len = strlen(name) + 1;
1966         ma->len++;
1967
1968         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
1969         if (len < 0)
1970                 ma->v[ma->len].iov_len = strlen(val) + 1;
1971         else
1972                 ma->v[ma->len].iov_len = len;
1973         ma->len++;
1974         return (ma);
1975 }
1976
1977 /*
1978  * Free a mntarg structure
1979  */
1980 static void
1981 free_mntarg(struct mntarg *ma)
1982 {
1983         struct mntaarg *maa;
1984
1985         while (!SLIST_EMPTY(&ma->list)) {
1986                 maa = SLIST_FIRST(&ma->list);
1987                 SLIST_REMOVE_HEAD(&ma->list, next);
1988                 free(maa, M_MOUNT);
1989         }
1990         free(ma->v, M_MOUNT);
1991         free(ma, M_MOUNT);
1992 }
1993
1994 /*
1995  * Mount a filesystem
1996  */
1997 int
1998 kernel_mount(struct mntarg *ma, int flags)
1999 {
2000         struct uio auio;
2001         int error;
2002
2003         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2004         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2005         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2006
2007         auio.uio_iov = ma->v;
2008         auio.uio_iovcnt = ma->len;
2009         auio.uio_segflg = UIO_SYSSPACE;
2010
2011         error = ma->error;
2012         if (!error)
2013                 error = vfs_donmount(curthread, flags, &auio);
2014         free_mntarg(ma);
2015         return (error);
2016 }
2017
2018 /*
2019  * A printflike function to mount a filesystem.
2020  */
2021 int
2022 kernel_vmount(int flags, ...)
2023 {
2024         struct mntarg *ma = NULL;
2025         va_list ap;
2026         const char *cp;
2027         const void *vp;
2028         int error;
2029
2030         va_start(ap, flags);
2031         for (;;) {
2032                 cp = va_arg(ap, const char *);
2033                 if (cp == NULL)
2034                         break;
2035                 vp = va_arg(ap, const void *);
2036                 ma = mount_arg(ma, cp, vp, -1);
2037         }
2038         va_end(ap);
2039
2040         error = kernel_mount(ma, flags);
2041         return (error);
2042 }