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