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